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