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