xref: /minix3/external/bsd/llvm/dist/clang/lib/AST/ExprClassification.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ExprClassification.cpp - Expression AST Node Implementation ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements Expr::classify.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
20f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
22f4a2713aSLionel Sambuc using namespace clang;
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc typedef Expr::Classification Cl;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27f4a2713aSLionel Sambuc static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
28f4a2713aSLionel Sambuc static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);
29f4a2713aSLionel Sambuc static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
30f4a2713aSLionel Sambuc static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);
31f4a2713aSLionel Sambuc static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
32f4a2713aSLionel Sambuc                                      const Expr *trueExpr,
33f4a2713aSLionel Sambuc                                      const Expr *falseExpr);
34f4a2713aSLionel Sambuc static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35f4a2713aSLionel Sambuc                                        Cl::Kinds Kind, SourceLocation &Loc);
36f4a2713aSLionel Sambuc 
ClassifyImpl(ASTContext & Ctx,SourceLocation * Loc) const37f4a2713aSLionel Sambuc Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38f4a2713aSLionel Sambuc   assert(!TR->isReferenceType() && "Expressions can't have reference type.");
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc   Cl::Kinds kind = ClassifyInternal(Ctx, this);
41f4a2713aSLionel Sambuc   // C99 6.3.2.1: An lvalue is an expression with an object type or an
42f4a2713aSLionel Sambuc   //   incomplete type other than void.
43f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().CPlusPlus) {
44f4a2713aSLionel Sambuc     // Thus, no functions.
45f4a2713aSLionel Sambuc     if (TR->isFunctionType() || TR == Ctx.OverloadTy)
46f4a2713aSLionel Sambuc       kind = Cl::CL_Function;
47f4a2713aSLionel Sambuc     // No void either, but qualified void is OK because it is "other than void".
48f4a2713aSLionel Sambuc     // Void "lvalues" are classified as addressable void values, which are void
49f4a2713aSLionel Sambuc     // expressions whose address can be taken.
50f4a2713aSLionel Sambuc     else if (TR->isVoidType() && !TR.hasQualifiers())
51f4a2713aSLionel Sambuc       kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void);
52f4a2713aSLionel Sambuc   }
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   // Enable this assertion for testing.
55f4a2713aSLionel Sambuc   switch (kind) {
56f4a2713aSLionel Sambuc   case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
57f4a2713aSLionel Sambuc   case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
58f4a2713aSLionel Sambuc   case Cl::CL_Function:
59f4a2713aSLionel Sambuc   case Cl::CL_Void:
60f4a2713aSLionel Sambuc   case Cl::CL_AddressableVoid:
61f4a2713aSLionel Sambuc   case Cl::CL_DuplicateVectorComponents:
62f4a2713aSLionel Sambuc   case Cl::CL_MemberFunction:
63f4a2713aSLionel Sambuc   case Cl::CL_SubObjCPropertySetting:
64f4a2713aSLionel Sambuc   case Cl::CL_ClassTemporary:
65f4a2713aSLionel Sambuc   case Cl::CL_ArrayTemporary:
66f4a2713aSLionel Sambuc   case Cl::CL_ObjCMessageRValue:
67f4a2713aSLionel Sambuc   case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
68f4a2713aSLionel Sambuc   }
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   Cl::ModifiableType modifiable = Cl::CM_Untested;
71f4a2713aSLionel Sambuc   if (Loc)
72f4a2713aSLionel Sambuc     modifiable = IsModifiable(Ctx, this, kind, *Loc);
73f4a2713aSLionel Sambuc   return Classification(kind, modifiable);
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc /// Classify an expression which creates a temporary, based on its type.
ClassifyTemporary(QualType T)77f4a2713aSLionel Sambuc static Cl::Kinds ClassifyTemporary(QualType T) {
78f4a2713aSLionel Sambuc   if (T->isRecordType())
79f4a2713aSLionel Sambuc     return Cl::CL_ClassTemporary;
80f4a2713aSLionel Sambuc   if (T->isArrayType())
81f4a2713aSLionel Sambuc     return Cl::CL_ArrayTemporary;
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   // No special classification: these don't behave differently from normal
84f4a2713aSLionel Sambuc   // prvalues.
85f4a2713aSLionel Sambuc   return Cl::CL_PRValue;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc 
ClassifyExprValueKind(const LangOptions & Lang,const Expr * E,ExprValueKind Kind)88f4a2713aSLionel Sambuc static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
89f4a2713aSLionel Sambuc                                        const Expr *E,
90f4a2713aSLionel Sambuc                                        ExprValueKind Kind) {
91f4a2713aSLionel Sambuc   switch (Kind) {
92f4a2713aSLionel Sambuc   case VK_RValue:
93f4a2713aSLionel Sambuc     return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue;
94f4a2713aSLionel Sambuc   case VK_LValue:
95f4a2713aSLionel Sambuc     return Cl::CL_LValue;
96f4a2713aSLionel Sambuc   case VK_XValue:
97f4a2713aSLionel Sambuc     return Cl::CL_XValue;
98f4a2713aSLionel Sambuc   }
99f4a2713aSLionel Sambuc   llvm_unreachable("Invalid value category of implicit cast.");
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
ClassifyInternal(ASTContext & Ctx,const Expr * E)102f4a2713aSLionel Sambuc static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
103f4a2713aSLionel Sambuc   // This function takes the first stab at classifying expressions.
104f4a2713aSLionel Sambuc   const LangOptions &Lang = Ctx.getLangOpts();
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   switch (E->getStmtClass()) {
107f4a2713aSLionel Sambuc   case Stmt::NoStmtClass:
108f4a2713aSLionel Sambuc #define ABSTRACT_STMT(Kind)
109f4a2713aSLionel Sambuc #define STMT(Kind, Base) case Expr::Kind##Class:
110f4a2713aSLionel Sambuc #define EXPR(Kind, Base)
111f4a2713aSLionel Sambuc #include "clang/AST/StmtNodes.inc"
112f4a2713aSLionel Sambuc     llvm_unreachable("cannot classify a statement");
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc     // First come the expressions that are always lvalues, unconditionally.
115f4a2713aSLionel Sambuc   case Expr::ObjCIsaExprClass:
116f4a2713aSLionel Sambuc     // C++ [expr.prim.general]p1: A string literal is an lvalue.
117f4a2713aSLionel Sambuc   case Expr::StringLiteralClass:
118f4a2713aSLionel Sambuc     // @encode is equivalent to its string
119f4a2713aSLionel Sambuc   case Expr::ObjCEncodeExprClass:
120f4a2713aSLionel Sambuc     // __func__ and friends are too.
121f4a2713aSLionel Sambuc   case Expr::PredefinedExprClass:
122f4a2713aSLionel Sambuc     // Property references are lvalues
123f4a2713aSLionel Sambuc   case Expr::ObjCSubscriptRefExprClass:
124f4a2713aSLionel Sambuc   case Expr::ObjCPropertyRefExprClass:
125f4a2713aSLionel Sambuc     // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
126f4a2713aSLionel Sambuc   case Expr::CXXTypeidExprClass:
127*0a6a1f1dSLionel Sambuc     // Unresolved lookups and uncorrected typos get classified as lvalues.
128f4a2713aSLionel Sambuc     // FIXME: Is this wise? Should they get their own kind?
129f4a2713aSLionel Sambuc   case Expr::UnresolvedLookupExprClass:
130f4a2713aSLionel Sambuc   case Expr::UnresolvedMemberExprClass:
131*0a6a1f1dSLionel Sambuc   case Expr::TypoExprClass:
132f4a2713aSLionel Sambuc   case Expr::CXXDependentScopeMemberExprClass:
133f4a2713aSLionel Sambuc   case Expr::DependentScopeDeclRefExprClass:
134f4a2713aSLionel Sambuc     // ObjC instance variables are lvalues
135f4a2713aSLionel Sambuc     // FIXME: ObjC++0x might have different rules
136f4a2713aSLionel Sambuc   case Expr::ObjCIvarRefExprClass:
137f4a2713aSLionel Sambuc   case Expr::FunctionParmPackExprClass:
138f4a2713aSLionel Sambuc   case Expr::MSPropertyRefExprClass:
139f4a2713aSLionel Sambuc     return Cl::CL_LValue;
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc     // C99 6.5.2.5p5 says that compound literals are lvalues.
142f4a2713aSLionel Sambuc     // In C++, they're prvalue temporaries.
143f4a2713aSLionel Sambuc   case Expr::CompoundLiteralExprClass:
144f4a2713aSLionel Sambuc     return Ctx.getLangOpts().CPlusPlus ? ClassifyTemporary(E->getType())
145f4a2713aSLionel Sambuc                                        : Cl::CL_LValue;
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc     // Expressions that are prvalues.
148f4a2713aSLionel Sambuc   case Expr::CXXBoolLiteralExprClass:
149f4a2713aSLionel Sambuc   case Expr::CXXPseudoDestructorExprClass:
150f4a2713aSLionel Sambuc   case Expr::UnaryExprOrTypeTraitExprClass:
151f4a2713aSLionel Sambuc   case Expr::CXXNewExprClass:
152f4a2713aSLionel Sambuc   case Expr::CXXThisExprClass:
153f4a2713aSLionel Sambuc   case Expr::CXXNullPtrLiteralExprClass:
154f4a2713aSLionel Sambuc   case Expr::ImaginaryLiteralClass:
155f4a2713aSLionel Sambuc   case Expr::GNUNullExprClass:
156f4a2713aSLionel Sambuc   case Expr::OffsetOfExprClass:
157f4a2713aSLionel Sambuc   case Expr::CXXThrowExprClass:
158f4a2713aSLionel Sambuc   case Expr::ShuffleVectorExprClass:
159f4a2713aSLionel Sambuc   case Expr::ConvertVectorExprClass:
160f4a2713aSLionel Sambuc   case Expr::IntegerLiteralClass:
161f4a2713aSLionel Sambuc   case Expr::CharacterLiteralClass:
162f4a2713aSLionel Sambuc   case Expr::AddrLabelExprClass:
163f4a2713aSLionel Sambuc   case Expr::CXXDeleteExprClass:
164f4a2713aSLionel Sambuc   case Expr::ImplicitValueInitExprClass:
165f4a2713aSLionel Sambuc   case Expr::BlockExprClass:
166f4a2713aSLionel Sambuc   case Expr::FloatingLiteralClass:
167f4a2713aSLionel Sambuc   case Expr::CXXNoexceptExprClass:
168f4a2713aSLionel Sambuc   case Expr::CXXScalarValueInitExprClass:
169f4a2713aSLionel Sambuc   case Expr::TypeTraitExprClass:
170f4a2713aSLionel Sambuc   case Expr::ArrayTypeTraitExprClass:
171f4a2713aSLionel Sambuc   case Expr::ExpressionTraitExprClass:
172f4a2713aSLionel Sambuc   case Expr::ObjCSelectorExprClass:
173f4a2713aSLionel Sambuc   case Expr::ObjCProtocolExprClass:
174f4a2713aSLionel Sambuc   case Expr::ObjCStringLiteralClass:
175f4a2713aSLionel Sambuc   case Expr::ObjCBoxedExprClass:
176f4a2713aSLionel Sambuc   case Expr::ObjCArrayLiteralClass:
177f4a2713aSLionel Sambuc   case Expr::ObjCDictionaryLiteralClass:
178f4a2713aSLionel Sambuc   case Expr::ObjCBoolLiteralExprClass:
179f4a2713aSLionel Sambuc   case Expr::ParenListExprClass:
180f4a2713aSLionel Sambuc   case Expr::SizeOfPackExprClass:
181f4a2713aSLionel Sambuc   case Expr::SubstNonTypeTemplateParmPackExprClass:
182f4a2713aSLionel Sambuc   case Expr::AsTypeExprClass:
183f4a2713aSLionel Sambuc   case Expr::ObjCIndirectCopyRestoreExprClass:
184f4a2713aSLionel Sambuc   case Expr::AtomicExprClass:
185*0a6a1f1dSLionel Sambuc   case Expr::CXXFoldExprClass:
186f4a2713aSLionel Sambuc     return Cl::CL_PRValue;
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc     // Next come the complicated cases.
189f4a2713aSLionel Sambuc   case Expr::SubstNonTypeTemplateParmExprClass:
190f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx,
191f4a2713aSLionel Sambuc                  cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc     // C++ [expr.sub]p1: The result is an lvalue of type "T".
194f4a2713aSLionel Sambuc     // However, subscripting vector types is more like member access.
195f4a2713aSLionel Sambuc   case Expr::ArraySubscriptExprClass:
196f4a2713aSLionel Sambuc     if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
197f4a2713aSLionel Sambuc       return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
198f4a2713aSLionel Sambuc     return Cl::CL_LValue;
199f4a2713aSLionel Sambuc 
200f4a2713aSLionel Sambuc     // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
201f4a2713aSLionel Sambuc     //   function or variable and a prvalue otherwise.
202f4a2713aSLionel Sambuc   case Expr::DeclRefExprClass:
203f4a2713aSLionel Sambuc     if (E->getType() == Ctx.UnknownAnyTy)
204f4a2713aSLionel Sambuc       return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
205f4a2713aSLionel Sambuc                ? Cl::CL_PRValue : Cl::CL_LValue;
206f4a2713aSLionel Sambuc     return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc     // Member access is complex.
209f4a2713aSLionel Sambuc   case Expr::MemberExprClass:
210f4a2713aSLionel Sambuc     return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc   case Expr::UnaryOperatorClass:
213f4a2713aSLionel Sambuc     switch (cast<UnaryOperator>(E)->getOpcode()) {
214f4a2713aSLionel Sambuc       // C++ [expr.unary.op]p1: The unary * operator performs indirection:
215f4a2713aSLionel Sambuc       //   [...] the result is an lvalue referring to the object or function
216f4a2713aSLionel Sambuc       //   to which the expression points.
217f4a2713aSLionel Sambuc     case UO_Deref:
218f4a2713aSLionel Sambuc       return Cl::CL_LValue;
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc       // GNU extensions, simply look through them.
221f4a2713aSLionel Sambuc     case UO_Extension:
222f4a2713aSLionel Sambuc       return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc     // Treat _Real and _Imag basically as if they were member
225f4a2713aSLionel Sambuc     // expressions:  l-value only if the operand is a true l-value.
226f4a2713aSLionel Sambuc     case UO_Real:
227f4a2713aSLionel Sambuc     case UO_Imag: {
228f4a2713aSLionel Sambuc       const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
229f4a2713aSLionel Sambuc       Cl::Kinds K = ClassifyInternal(Ctx, Op);
230f4a2713aSLionel Sambuc       if (K != Cl::CL_LValue) return K;
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc       if (isa<ObjCPropertyRefExpr>(Op))
233f4a2713aSLionel Sambuc         return Cl::CL_SubObjCPropertySetting;
234f4a2713aSLionel Sambuc       return Cl::CL_LValue;
235f4a2713aSLionel Sambuc     }
236f4a2713aSLionel Sambuc 
237f4a2713aSLionel Sambuc       // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
238f4a2713aSLionel Sambuc       //   lvalue, [...]
239f4a2713aSLionel Sambuc       // Not so in C.
240f4a2713aSLionel Sambuc     case UO_PreInc:
241f4a2713aSLionel Sambuc     case UO_PreDec:
242f4a2713aSLionel Sambuc       return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc     default:
245f4a2713aSLionel Sambuc       return Cl::CL_PRValue;
246f4a2713aSLionel Sambuc     }
247f4a2713aSLionel Sambuc 
248f4a2713aSLionel Sambuc   case Expr::OpaqueValueExprClass:
249f4a2713aSLionel Sambuc     return ClassifyExprValueKind(Lang, E, E->getValueKind());
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc     // Pseudo-object expressions can produce l-values with reference magic.
252f4a2713aSLionel Sambuc   case Expr::PseudoObjectExprClass:
253f4a2713aSLionel Sambuc     return ClassifyExprValueKind(Lang, E,
254f4a2713aSLionel Sambuc                                  cast<PseudoObjectExpr>(E)->getValueKind());
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc     // Implicit casts are lvalues if they're lvalue casts. Other than that, we
257f4a2713aSLionel Sambuc     // only specifically record class temporaries.
258f4a2713aSLionel Sambuc   case Expr::ImplicitCastExprClass:
259f4a2713aSLionel Sambuc     return ClassifyExprValueKind(Lang, E, E->getValueKind());
260f4a2713aSLionel Sambuc 
261f4a2713aSLionel Sambuc     // C++ [expr.prim.general]p4: The presence of parentheses does not affect
262f4a2713aSLionel Sambuc     //   whether the expression is an lvalue.
263f4a2713aSLionel Sambuc   case Expr::ParenExprClass:
264f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc     // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
267f4a2713aSLionel Sambuc     // or a void expression if its result expression is, respectively, an
268f4a2713aSLionel Sambuc     // lvalue, a function designator, or a void expression.
269f4a2713aSLionel Sambuc   case Expr::GenericSelectionExprClass:
270f4a2713aSLionel Sambuc     if (cast<GenericSelectionExpr>(E)->isResultDependent())
271f4a2713aSLionel Sambuc       return Cl::CL_PRValue;
272f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc   case Expr::BinaryOperatorClass:
275f4a2713aSLionel Sambuc   case Expr::CompoundAssignOperatorClass:
276f4a2713aSLionel Sambuc     // C doesn't have any binary expressions that are lvalues.
277f4a2713aSLionel Sambuc     if (Lang.CPlusPlus)
278f4a2713aSLionel Sambuc       return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
279f4a2713aSLionel Sambuc     return Cl::CL_PRValue;
280f4a2713aSLionel Sambuc 
281f4a2713aSLionel Sambuc   case Expr::CallExprClass:
282f4a2713aSLionel Sambuc   case Expr::CXXOperatorCallExprClass:
283f4a2713aSLionel Sambuc   case Expr::CXXMemberCallExprClass:
284f4a2713aSLionel Sambuc   case Expr::UserDefinedLiteralClass:
285f4a2713aSLionel Sambuc   case Expr::CUDAKernelCallExprClass:
286f4a2713aSLionel Sambuc     return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
287f4a2713aSLionel Sambuc 
288f4a2713aSLionel Sambuc     // __builtin_choose_expr is equivalent to the chosen expression.
289f4a2713aSLionel Sambuc   case Expr::ChooseExprClass:
290f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc     // Extended vector element access is an lvalue unless there are duplicates
293f4a2713aSLionel Sambuc     // in the shuffle expression.
294f4a2713aSLionel Sambuc   case Expr::ExtVectorElementExprClass:
295f4a2713aSLionel Sambuc     if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
296f4a2713aSLionel Sambuc       return Cl::CL_DuplicateVectorComponents;
297f4a2713aSLionel Sambuc     if (cast<ExtVectorElementExpr>(E)->isArrow())
298f4a2713aSLionel Sambuc       return Cl::CL_LValue;
299f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
300f4a2713aSLionel Sambuc 
301f4a2713aSLionel Sambuc     // Simply look at the actual default argument.
302f4a2713aSLionel Sambuc   case Expr::CXXDefaultArgExprClass:
303f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
304f4a2713aSLionel Sambuc 
305f4a2713aSLionel Sambuc     // Same idea for default initializers.
306f4a2713aSLionel Sambuc   case Expr::CXXDefaultInitExprClass:
307f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc     // Same idea for temporary binding.
310f4a2713aSLionel Sambuc   case Expr::CXXBindTemporaryExprClass:
311f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc     // And the cleanups guard.
314f4a2713aSLionel Sambuc   case Expr::ExprWithCleanupsClass:
315f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
316f4a2713aSLionel Sambuc 
317f4a2713aSLionel Sambuc     // Casts depend completely on the target type. All casts work the same.
318f4a2713aSLionel Sambuc   case Expr::CStyleCastExprClass:
319f4a2713aSLionel Sambuc   case Expr::CXXFunctionalCastExprClass:
320f4a2713aSLionel Sambuc   case Expr::CXXStaticCastExprClass:
321f4a2713aSLionel Sambuc   case Expr::CXXDynamicCastExprClass:
322f4a2713aSLionel Sambuc   case Expr::CXXReinterpretCastExprClass:
323f4a2713aSLionel Sambuc   case Expr::CXXConstCastExprClass:
324f4a2713aSLionel Sambuc   case Expr::ObjCBridgedCastExprClass:
325f4a2713aSLionel Sambuc     // Only in C++ can casts be interesting at all.
326f4a2713aSLionel Sambuc     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
327f4a2713aSLionel Sambuc     return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
328f4a2713aSLionel Sambuc 
329f4a2713aSLionel Sambuc   case Expr::CXXUnresolvedConstructExprClass:
330f4a2713aSLionel Sambuc     return ClassifyUnnamed(Ctx,
331f4a2713aSLionel Sambuc                       cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
332f4a2713aSLionel Sambuc 
333f4a2713aSLionel Sambuc   case Expr::BinaryConditionalOperatorClass: {
334f4a2713aSLionel Sambuc     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
335f4a2713aSLionel Sambuc     const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
336f4a2713aSLionel Sambuc     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
337f4a2713aSLionel Sambuc   }
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc   case Expr::ConditionalOperatorClass: {
340f4a2713aSLionel Sambuc     // Once again, only C++ is interesting.
341f4a2713aSLionel Sambuc     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
342f4a2713aSLionel Sambuc     const ConditionalOperator *co = cast<ConditionalOperator>(E);
343f4a2713aSLionel Sambuc     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
344f4a2713aSLionel Sambuc   }
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc     // ObjC message sends are effectively function calls, if the target function
347f4a2713aSLionel Sambuc     // is known.
348f4a2713aSLionel Sambuc   case Expr::ObjCMessageExprClass:
349f4a2713aSLionel Sambuc     if (const ObjCMethodDecl *Method =
350f4a2713aSLionel Sambuc           cast<ObjCMessageExpr>(E)->getMethodDecl()) {
351*0a6a1f1dSLionel Sambuc       Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
352f4a2713aSLionel Sambuc       return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
353f4a2713aSLionel Sambuc     }
354f4a2713aSLionel Sambuc     return Cl::CL_PRValue;
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc     // Some C++ expressions are always class temporaries.
357f4a2713aSLionel Sambuc   case Expr::CXXConstructExprClass:
358f4a2713aSLionel Sambuc   case Expr::CXXTemporaryObjectExprClass:
359f4a2713aSLionel Sambuc   case Expr::LambdaExprClass:
360f4a2713aSLionel Sambuc   case Expr::CXXStdInitializerListExprClass:
361f4a2713aSLionel Sambuc     return Cl::CL_ClassTemporary;
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   case Expr::VAArgExprClass:
364f4a2713aSLionel Sambuc     return ClassifyUnnamed(Ctx, E->getType());
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc   case Expr::DesignatedInitExprClass:
367f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
368f4a2713aSLionel Sambuc 
369f4a2713aSLionel Sambuc   case Expr::StmtExprClass: {
370f4a2713aSLionel Sambuc     const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
371f4a2713aSLionel Sambuc     if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
372f4a2713aSLionel Sambuc       return ClassifyUnnamed(Ctx, LastExpr->getType());
373f4a2713aSLionel Sambuc     return Cl::CL_PRValue;
374f4a2713aSLionel Sambuc   }
375f4a2713aSLionel Sambuc 
376f4a2713aSLionel Sambuc   case Expr::CXXUuidofExprClass:
377f4a2713aSLionel Sambuc     return Cl::CL_LValue;
378f4a2713aSLionel Sambuc 
379f4a2713aSLionel Sambuc   case Expr::PackExpansionExprClass:
380f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   case Expr::MaterializeTemporaryExprClass:
383f4a2713aSLionel Sambuc     return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
384f4a2713aSLionel Sambuc               ? Cl::CL_LValue
385f4a2713aSLionel Sambuc               : Cl::CL_XValue;
386f4a2713aSLionel Sambuc 
387f4a2713aSLionel Sambuc   case Expr::InitListExprClass:
388f4a2713aSLionel Sambuc     // An init list can be an lvalue if it is bound to a reference and
389f4a2713aSLionel Sambuc     // contains only one element. In that case, we look at that element
390f4a2713aSLionel Sambuc     // for an exact classification. Init list creation takes care of the
391f4a2713aSLionel Sambuc     // value kind for us, so we only need to fine-tune.
392f4a2713aSLionel Sambuc     if (E->isRValue())
393f4a2713aSLionel Sambuc       return ClassifyExprValueKind(Lang, E, E->getValueKind());
394f4a2713aSLionel Sambuc     assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
395f4a2713aSLionel Sambuc            "Only 1-element init lists can be glvalues.");
396f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
397f4a2713aSLionel Sambuc   }
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc   llvm_unreachable("unhandled expression kind in classification");
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc /// ClassifyDecl - Return the classification of an expression referencing the
403f4a2713aSLionel Sambuc /// given declaration.
ClassifyDecl(ASTContext & Ctx,const Decl * D)404f4a2713aSLionel Sambuc static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
405f4a2713aSLionel Sambuc   // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
406f4a2713aSLionel Sambuc   //   function, variable, or data member and a prvalue otherwise.
407f4a2713aSLionel Sambuc   // In C, functions are not lvalues.
408f4a2713aSLionel Sambuc   // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
409f4a2713aSLionel Sambuc   // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
410f4a2713aSLionel Sambuc   // special-case this.
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc   if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
413f4a2713aSLionel Sambuc     return Cl::CL_MemberFunction;
414f4a2713aSLionel Sambuc 
415f4a2713aSLionel Sambuc   bool islvalue;
416f4a2713aSLionel Sambuc   if (const NonTypeTemplateParmDecl *NTTParm =
417f4a2713aSLionel Sambuc         dyn_cast<NonTypeTemplateParmDecl>(D))
418f4a2713aSLionel Sambuc     islvalue = NTTParm->getType()->isReferenceType();
419f4a2713aSLionel Sambuc   else
420f4a2713aSLionel Sambuc     islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
421f4a2713aSLionel Sambuc 	  isa<IndirectFieldDecl>(D) ||
422f4a2713aSLionel Sambuc       (Ctx.getLangOpts().CPlusPlus &&
423f4a2713aSLionel Sambuc         (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
424f4a2713aSLionel Sambuc 
425f4a2713aSLionel Sambuc   return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc 
428f4a2713aSLionel Sambuc /// ClassifyUnnamed - Return the classification of an expression yielding an
429f4a2713aSLionel Sambuc /// unnamed value of the given type. This applies in particular to function
430f4a2713aSLionel Sambuc /// calls and casts.
ClassifyUnnamed(ASTContext & Ctx,QualType T)431f4a2713aSLionel Sambuc static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
432f4a2713aSLionel Sambuc   // In C, function calls are always rvalues.
433f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
434f4a2713aSLionel Sambuc 
435f4a2713aSLionel Sambuc   // C++ [expr.call]p10: A function call is an lvalue if the result type is an
436f4a2713aSLionel Sambuc   //   lvalue reference type or an rvalue reference to function type, an xvalue
437f4a2713aSLionel Sambuc   //   if the result type is an rvalue reference to object type, and a prvalue
438f4a2713aSLionel Sambuc   //   otherwise.
439f4a2713aSLionel Sambuc   if (T->isLValueReferenceType())
440f4a2713aSLionel Sambuc     return Cl::CL_LValue;
441f4a2713aSLionel Sambuc   const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
442f4a2713aSLionel Sambuc   if (!RV) // Could still be a class temporary, though.
443f4a2713aSLionel Sambuc     return ClassifyTemporary(T);
444f4a2713aSLionel Sambuc 
445f4a2713aSLionel Sambuc   return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
446f4a2713aSLionel Sambuc }
447f4a2713aSLionel Sambuc 
ClassifyMemberExpr(ASTContext & Ctx,const MemberExpr * E)448f4a2713aSLionel Sambuc static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
449f4a2713aSLionel Sambuc   if (E->getType() == Ctx.UnknownAnyTy)
450f4a2713aSLionel Sambuc     return (isa<FunctionDecl>(E->getMemberDecl())
451f4a2713aSLionel Sambuc               ? Cl::CL_PRValue : Cl::CL_LValue);
452f4a2713aSLionel Sambuc 
453f4a2713aSLionel Sambuc   // Handle C first, it's easier.
454f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().CPlusPlus) {
455f4a2713aSLionel Sambuc     // C99 6.5.2.3p3
456f4a2713aSLionel Sambuc     // For dot access, the expression is an lvalue if the first part is. For
457f4a2713aSLionel Sambuc     // arrow access, it always is an lvalue.
458f4a2713aSLionel Sambuc     if (E->isArrow())
459f4a2713aSLionel Sambuc       return Cl::CL_LValue;
460f4a2713aSLionel Sambuc     // ObjC property accesses are not lvalues, but get special treatment.
461f4a2713aSLionel Sambuc     Expr *Base = E->getBase()->IgnoreParens();
462f4a2713aSLionel Sambuc     if (isa<ObjCPropertyRefExpr>(Base))
463f4a2713aSLionel Sambuc       return Cl::CL_SubObjCPropertySetting;
464f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, Base);
465f4a2713aSLionel Sambuc   }
466f4a2713aSLionel Sambuc 
467f4a2713aSLionel Sambuc   NamedDecl *Member = E->getMemberDecl();
468f4a2713aSLionel Sambuc   // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
469f4a2713aSLionel Sambuc   // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
470f4a2713aSLionel Sambuc   //   E1.E2 is an lvalue.
471f4a2713aSLionel Sambuc   if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
472f4a2713aSLionel Sambuc     if (Value->getType()->isReferenceType())
473f4a2713aSLionel Sambuc       return Cl::CL_LValue;
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc   //   Otherwise, one of the following rules applies.
476f4a2713aSLionel Sambuc   //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
477f4a2713aSLionel Sambuc   if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
478f4a2713aSLionel Sambuc     return Cl::CL_LValue;
479f4a2713aSLionel Sambuc 
480f4a2713aSLionel Sambuc   //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
481f4a2713aSLionel Sambuc   //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
482f4a2713aSLionel Sambuc   //      otherwise, it is a prvalue.
483f4a2713aSLionel Sambuc   if (isa<FieldDecl>(Member)) {
484f4a2713aSLionel Sambuc     // *E1 is an lvalue
485f4a2713aSLionel Sambuc     if (E->isArrow())
486f4a2713aSLionel Sambuc       return Cl::CL_LValue;
487f4a2713aSLionel Sambuc     Expr *Base = E->getBase()->IgnoreParenImpCasts();
488f4a2713aSLionel Sambuc     if (isa<ObjCPropertyRefExpr>(Base))
489f4a2713aSLionel Sambuc       return Cl::CL_SubObjCPropertySetting;
490f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, E->getBase());
491f4a2713aSLionel Sambuc   }
492f4a2713aSLionel Sambuc 
493f4a2713aSLionel Sambuc   //   -- If E2 is a [...] member function, [...]
494f4a2713aSLionel Sambuc   //      -- If it refers to a static member function [...], then E1.E2 is an
495f4a2713aSLionel Sambuc   //         lvalue; [...]
496f4a2713aSLionel Sambuc   //      -- Otherwise [...] E1.E2 is a prvalue.
497f4a2713aSLionel Sambuc   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
498f4a2713aSLionel Sambuc     return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
499f4a2713aSLionel Sambuc 
500f4a2713aSLionel Sambuc   //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
501f4a2713aSLionel Sambuc   // So is everything else we haven't handled yet.
502f4a2713aSLionel Sambuc   return Cl::CL_PRValue;
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc 
ClassifyBinaryOp(ASTContext & Ctx,const BinaryOperator * E)505f4a2713aSLionel Sambuc static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
506f4a2713aSLionel Sambuc   assert(Ctx.getLangOpts().CPlusPlus &&
507f4a2713aSLionel Sambuc          "This is only relevant for C++.");
508f4a2713aSLionel Sambuc   // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
509f4a2713aSLionel Sambuc   // Except we override this for writes to ObjC properties.
510f4a2713aSLionel Sambuc   if (E->isAssignmentOp())
511f4a2713aSLionel Sambuc     return (E->getLHS()->getObjectKind() == OK_ObjCProperty
512f4a2713aSLionel Sambuc               ? Cl::CL_PRValue : Cl::CL_LValue);
513f4a2713aSLionel Sambuc 
514f4a2713aSLionel Sambuc   // C++ [expr.comma]p1: the result is of the same value category as its right
515f4a2713aSLionel Sambuc   //   operand, [...].
516f4a2713aSLionel Sambuc   if (E->getOpcode() == BO_Comma)
517f4a2713aSLionel Sambuc     return ClassifyInternal(Ctx, E->getRHS());
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc   // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
520f4a2713aSLionel Sambuc   //   is a pointer to a data member is of the same value category as its first
521f4a2713aSLionel Sambuc   //   operand.
522f4a2713aSLionel Sambuc   if (E->getOpcode() == BO_PtrMemD)
523f4a2713aSLionel Sambuc     return (E->getType()->isFunctionType() ||
524f4a2713aSLionel Sambuc             E->hasPlaceholderType(BuiltinType::BoundMember))
525f4a2713aSLionel Sambuc              ? Cl::CL_MemberFunction
526f4a2713aSLionel Sambuc              : ClassifyInternal(Ctx, E->getLHS());
527f4a2713aSLionel Sambuc 
528f4a2713aSLionel Sambuc   // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
529f4a2713aSLionel Sambuc   //   second operand is a pointer to data member and a prvalue otherwise.
530f4a2713aSLionel Sambuc   if (E->getOpcode() == BO_PtrMemI)
531f4a2713aSLionel Sambuc     return (E->getType()->isFunctionType() ||
532f4a2713aSLionel Sambuc             E->hasPlaceholderType(BuiltinType::BoundMember))
533f4a2713aSLionel Sambuc              ? Cl::CL_MemberFunction
534f4a2713aSLionel Sambuc              : Cl::CL_LValue;
535f4a2713aSLionel Sambuc 
536f4a2713aSLionel Sambuc   // All other binary operations are prvalues.
537f4a2713aSLionel Sambuc   return Cl::CL_PRValue;
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc 
ClassifyConditional(ASTContext & Ctx,const Expr * True,const Expr * False)540f4a2713aSLionel Sambuc static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
541f4a2713aSLionel Sambuc                                      const Expr *False) {
542f4a2713aSLionel Sambuc   assert(Ctx.getLangOpts().CPlusPlus &&
543f4a2713aSLionel Sambuc          "This is only relevant for C++.");
544f4a2713aSLionel Sambuc 
545f4a2713aSLionel Sambuc   // C++ [expr.cond]p2
546*0a6a1f1dSLionel Sambuc   //   If either the second or the third operand has type (cv) void,
547*0a6a1f1dSLionel Sambuc   //   one of the following shall hold:
548*0a6a1f1dSLionel Sambuc   if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
549*0a6a1f1dSLionel Sambuc     // The second or the third operand (but not both) is a (possibly
550*0a6a1f1dSLionel Sambuc     // parenthesized) throw-expression; the result is of the [...] value
551*0a6a1f1dSLionel Sambuc     // category of the other.
552*0a6a1f1dSLionel Sambuc     bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
553*0a6a1f1dSLionel Sambuc     bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
554*0a6a1f1dSLionel Sambuc     if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
555*0a6a1f1dSLionel Sambuc                                            : (FalseIsThrow ? True : nullptr))
556*0a6a1f1dSLionel Sambuc       return ClassifyInternal(Ctx, NonThrow);
557*0a6a1f1dSLionel Sambuc 
558*0a6a1f1dSLionel Sambuc     //   [Otherwise] the result [...] is a prvalue.
559f4a2713aSLionel Sambuc     return Cl::CL_PRValue;
560*0a6a1f1dSLionel Sambuc   }
561f4a2713aSLionel Sambuc 
562f4a2713aSLionel Sambuc   // Note that at this point, we have already performed all conversions
563f4a2713aSLionel Sambuc   // according to [expr.cond]p3.
564f4a2713aSLionel Sambuc   // C++ [expr.cond]p4: If the second and third operands are glvalues of the
565f4a2713aSLionel Sambuc   //   same value category [...], the result is of that [...] value category.
566f4a2713aSLionel Sambuc   // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
567f4a2713aSLionel Sambuc   Cl::Kinds LCl = ClassifyInternal(Ctx, True),
568f4a2713aSLionel Sambuc             RCl = ClassifyInternal(Ctx, False);
569f4a2713aSLionel Sambuc   return LCl == RCl ? LCl : Cl::CL_PRValue;
570f4a2713aSLionel Sambuc }
571f4a2713aSLionel Sambuc 
IsModifiable(ASTContext & Ctx,const Expr * E,Cl::Kinds Kind,SourceLocation & Loc)572f4a2713aSLionel Sambuc static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
573f4a2713aSLionel Sambuc                                        Cl::Kinds Kind, SourceLocation &Loc) {
574f4a2713aSLionel Sambuc   // As a general rule, we only care about lvalues. But there are some rvalues
575f4a2713aSLionel Sambuc   // for which we want to generate special results.
576f4a2713aSLionel Sambuc   if (Kind == Cl::CL_PRValue) {
577f4a2713aSLionel Sambuc     // For the sake of better diagnostics, we want to specifically recognize
578f4a2713aSLionel Sambuc     // use of the GCC cast-as-lvalue extension.
579f4a2713aSLionel Sambuc     if (const ExplicitCastExpr *CE =
580f4a2713aSLionel Sambuc           dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
581f4a2713aSLionel Sambuc       if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
582f4a2713aSLionel Sambuc         Loc = CE->getExprLoc();
583f4a2713aSLionel Sambuc         return Cl::CM_LValueCast;
584f4a2713aSLionel Sambuc       }
585f4a2713aSLionel Sambuc     }
586f4a2713aSLionel Sambuc   }
587f4a2713aSLionel Sambuc   if (Kind != Cl::CL_LValue)
588f4a2713aSLionel Sambuc     return Cl::CM_RValue;
589f4a2713aSLionel Sambuc 
590f4a2713aSLionel Sambuc   // This is the lvalue case.
591f4a2713aSLionel Sambuc   // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
592f4a2713aSLionel Sambuc   if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
593f4a2713aSLionel Sambuc     return Cl::CM_Function;
594f4a2713aSLionel Sambuc 
595f4a2713aSLionel Sambuc   // Assignment to a property in ObjC is an implicit setter access. But a
596f4a2713aSLionel Sambuc   // setter might not exist.
597f4a2713aSLionel Sambuc   if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
598*0a6a1f1dSLionel Sambuc     if (Expr->isImplicitProperty() &&
599*0a6a1f1dSLionel Sambuc         Expr->getImplicitPropertySetter() == nullptr)
600f4a2713aSLionel Sambuc       return Cl::CM_NoSetterProperty;
601f4a2713aSLionel Sambuc   }
602f4a2713aSLionel Sambuc 
603f4a2713aSLionel Sambuc   CanQualType CT = Ctx.getCanonicalType(E->getType());
604f4a2713aSLionel Sambuc   // Const stuff is obviously not modifiable.
605f4a2713aSLionel Sambuc   if (CT.isConstQualified())
606f4a2713aSLionel Sambuc     return Cl::CM_ConstQualified;
607f4a2713aSLionel Sambuc   if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant)
608f4a2713aSLionel Sambuc     return Cl::CM_ConstQualified;
609f4a2713aSLionel Sambuc 
610f4a2713aSLionel Sambuc   // Arrays are not modifiable, only their elements are.
611f4a2713aSLionel Sambuc   if (CT->isArrayType())
612f4a2713aSLionel Sambuc     return Cl::CM_ArrayType;
613f4a2713aSLionel Sambuc   // Incomplete types are not modifiable.
614f4a2713aSLionel Sambuc   if (CT->isIncompleteType())
615f4a2713aSLionel Sambuc     return Cl::CM_IncompleteType;
616f4a2713aSLionel Sambuc 
617f4a2713aSLionel Sambuc   // Records with any const fields (recursively) are not modifiable.
618*0a6a1f1dSLionel Sambuc   if (const RecordType *R = CT->getAs<RecordType>())
619f4a2713aSLionel Sambuc     if (R->hasConstFields())
620f4a2713aSLionel Sambuc       return Cl::CM_ConstQualified;
621f4a2713aSLionel Sambuc 
622f4a2713aSLionel Sambuc   return Cl::CM_Modifiable;
623f4a2713aSLionel Sambuc }
624f4a2713aSLionel Sambuc 
ClassifyLValue(ASTContext & Ctx) const625f4a2713aSLionel Sambuc Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
626f4a2713aSLionel Sambuc   Classification VC = Classify(Ctx);
627f4a2713aSLionel Sambuc   switch (VC.getKind()) {
628f4a2713aSLionel Sambuc   case Cl::CL_LValue: return LV_Valid;
629f4a2713aSLionel Sambuc   case Cl::CL_XValue: return LV_InvalidExpression;
630f4a2713aSLionel Sambuc   case Cl::CL_Function: return LV_NotObjectType;
631f4a2713aSLionel Sambuc   case Cl::CL_Void: return LV_InvalidExpression;
632f4a2713aSLionel Sambuc   case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
633f4a2713aSLionel Sambuc   case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
634f4a2713aSLionel Sambuc   case Cl::CL_MemberFunction: return LV_MemberFunction;
635f4a2713aSLionel Sambuc   case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
636f4a2713aSLionel Sambuc   case Cl::CL_ClassTemporary: return LV_ClassTemporary;
637f4a2713aSLionel Sambuc   case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
638f4a2713aSLionel Sambuc   case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
639f4a2713aSLionel Sambuc   case Cl::CL_PRValue: return LV_InvalidExpression;
640f4a2713aSLionel Sambuc   }
641f4a2713aSLionel Sambuc   llvm_unreachable("Unhandled kind");
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc 
644f4a2713aSLionel Sambuc Expr::isModifiableLvalueResult
isModifiableLvalue(ASTContext & Ctx,SourceLocation * Loc) const645f4a2713aSLionel Sambuc Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
646f4a2713aSLionel Sambuc   SourceLocation dummy;
647f4a2713aSLionel Sambuc   Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
648f4a2713aSLionel Sambuc   switch (VC.getKind()) {
649f4a2713aSLionel Sambuc   case Cl::CL_LValue: break;
650f4a2713aSLionel Sambuc   case Cl::CL_XValue: return MLV_InvalidExpression;
651f4a2713aSLionel Sambuc   case Cl::CL_Function: return MLV_NotObjectType;
652f4a2713aSLionel Sambuc   case Cl::CL_Void: return MLV_InvalidExpression;
653f4a2713aSLionel Sambuc   case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
654f4a2713aSLionel Sambuc   case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
655f4a2713aSLionel Sambuc   case Cl::CL_MemberFunction: return MLV_MemberFunction;
656f4a2713aSLionel Sambuc   case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
657f4a2713aSLionel Sambuc   case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
658f4a2713aSLionel Sambuc   case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;
659f4a2713aSLionel Sambuc   case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
660f4a2713aSLionel Sambuc   case Cl::CL_PRValue:
661f4a2713aSLionel Sambuc     return VC.getModifiable() == Cl::CM_LValueCast ?
662f4a2713aSLionel Sambuc       MLV_LValueCast : MLV_InvalidExpression;
663f4a2713aSLionel Sambuc   }
664f4a2713aSLionel Sambuc   assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
665f4a2713aSLionel Sambuc   switch (VC.getModifiable()) {
666f4a2713aSLionel Sambuc   case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
667f4a2713aSLionel Sambuc   case Cl::CM_Modifiable: return MLV_Valid;
668f4a2713aSLionel Sambuc   case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
669f4a2713aSLionel Sambuc   case Cl::CM_Function: return MLV_NotObjectType;
670f4a2713aSLionel Sambuc   case Cl::CM_LValueCast:
671f4a2713aSLionel Sambuc     llvm_unreachable("CM_LValueCast and CL_LValue don't match");
672f4a2713aSLionel Sambuc   case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
673f4a2713aSLionel Sambuc   case Cl::CM_ConstQualified: return MLV_ConstQualified;
674f4a2713aSLionel Sambuc   case Cl::CM_ArrayType: return MLV_ArrayType;
675f4a2713aSLionel Sambuc   case Cl::CM_IncompleteType: return MLV_IncompleteType;
676f4a2713aSLionel Sambuc   }
677f4a2713aSLionel Sambuc   llvm_unreachable("Unhandled modifiable type");
678f4a2713aSLionel Sambuc }
679