A state of a quantum system can be regarded as classical (quantum) with respect to measurements of a set of canonical observables if and only if there exists (does not exist) a well defined, positive phase-space distribution, the so called Glauber-Sudarshan P representation. We derive a family of classicality criteria that requires that the averages of positive functions calculated using P representation must be positive. For polynomial functions, these criteria are related to Hilbert's 17th problem, and have physical meaning of generalized squeezing conditions; alternatively, they may be interpreted as nonclassicality witnesses. We show that every generic nonclassical state can be detected by a polynomial that is a sum-of-squares of other polynomials. We introduce a very natural hierarchy of states regarding their degree of quantumness, which we relate to the minimal degree of a sum-of-squares polynomial that detects them.