Theory of selective reflection spectroscopy