C++: Fix typeid operand evaluation context [PR125886]
Ensures `typeid` operands are correctly evaluated only when necessary, fixing parser and diagnostic issues.
This commit rectifies how GCC handles typeid operand evaluation, ensuring operands are treated as unevaluated by default as per the C++ standard. Previously, GCC always parsed typeid operands in an evaluated context, causing issues with constructs like declval, non-static data members, and function parameters, and missing lambda capture diagnostics. A two-pass parsing approach is implemented to resolve this.
In Details
The C++ standard ([expr.typeid]/4-5) specifies that typeid operands are unevaluated unless the operand is a glvalue of a polymorphic class type whose dynamic type is not known at compile time. GCC incorrectly evaluated all typeid operands. This patch introduces a two-pass parsing mechanism: an initial pass with cp_unevaluated context, followed by a re-parse in cp_evaluated context if typeid_evaluated_p indicates evaluation is required. This corrects behavior for various unevaluated contexts and diagnostic reporting for evaluated polymorphic types.
- typeid
- A C++ operator that returns a
std::type_infoobject representing the type of its operand. Its operand's evaluation behavior is specified by the C++ standard. - Unevaluated operand
- An expression or operand that is not executed or computed at runtime. This can occur in contexts like
declval, template non-type arguments, or certaintypeiduses. - Polymorphic class
- A class that has at least one virtual function. Objects of polymorphic types can have their dynamic type determined at runtime.
- Glvalue
- A C++ expression that designates an object or function. It is a generalization of 'lvalue'.
- declval
- A C++ helper function template in
<utility>that returns a prvalue of type T, allowing T's members to be accessed in unevaluated contexts, such asdecltypeorsizeof, without constructing an object of type T. - PR125886
- A Project/Problem Report number, indicating a bug filed in the GCC bug tracking system.