First-principles theory and calculation of flexoelectricity