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   case Expr::BinaryOperatorClass:
237   case Expr::CompoundAssignOperatorClass:
238     // C doesn't have any binary expressions that are lvalues.
239     if (Lang.CPlusPlus)
240       return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
241     return Cl::CL_PRValue;
242 
243   case Expr::CallExprClass:
244   case Expr::CXXOperatorCallExprClass:
245   case Expr::CXXMemberCallExprClass:
246   case Expr::CUDAKernelCallExprClass:
247     return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
248 
249     // __builtin_choose_expr is equivalent to the chosen expression.
250   case Expr::ChooseExprClass:
251     return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
252 
253     // Extended vector element access is an lvalue unless there are duplicates
254     // in the shuffle expression.
255   case Expr::ExtVectorElementExprClass:
256     return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
257       Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
258 
259     // Simply look at the actual default argument.
260   case Expr::CXXDefaultArgExprClass:
261     return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
262 
263     // Same idea for temporary binding.
264   case Expr::CXXBindTemporaryExprClass:
265     return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
266 
267     // And the cleanups guard.
268   case Expr::ExprWithCleanupsClass:
269     return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
270 
271     // Casts depend completely on the target type. All casts work the same.
272   case Expr::CStyleCastExprClass:
273   case Expr::CXXFunctionalCastExprClass:
274   case Expr::CXXStaticCastExprClass:
275   case Expr::CXXDynamicCastExprClass:
276   case Expr::CXXReinterpretCastExprClass:
277   case Expr::CXXConstCastExprClass:
278     // Only in C++ can casts be interesting at all.
279     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
280     return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
281 
282   case Expr::BinaryConditionalOperatorClass: {
283     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
284     const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
285     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
286   }
287 
288   case Expr::ConditionalOperatorClass: {
289     // Once again, only C++ is interesting.
290     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
291     const ConditionalOperator *co = cast<ConditionalOperator>(E);
292     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
293   }
294 
295     // ObjC message sends are effectively function calls, if the target function
296     // is known.
297   case Expr::ObjCMessageExprClass:
298     if (const ObjCMethodDecl *Method =
299           cast<ObjCMessageExpr>(E)->getMethodDecl()) {
300       Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
301       return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
302     }
303     return Cl::CL_PRValue;
304 
305     // Some C++ expressions are always class temporaries.
306   case Expr::CXXConstructExprClass:
307   case Expr::CXXTemporaryObjectExprClass:
308     return Cl::CL_ClassTemporary;
309 
310   case Expr::VAArgExprClass:
311     return ClassifyUnnamed(Ctx, E->getType());
312 
313   case Expr::DesignatedInitExprClass:
314     return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
315 
316   case Expr::StmtExprClass: {
317     const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
318     if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
319       return ClassifyUnnamed(Ctx, LastExpr->getType());
320     return Cl::CL_PRValue;
321   }
322 
323   case Expr::CXXUuidofExprClass:
324     return Cl::CL_LValue;
325 
326   case Expr::PackExpansionExprClass:
327     return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
328   }
329 
330   llvm_unreachable("unhandled expression kind in classification");
331   return Cl::CL_LValue;
332 }
333 
334 /// ClassifyDecl - Return the classification of an expression referencing the
335 /// given declaration.
336 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
337   // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
338   //   function, variable, or data member and a prvalue otherwise.
339   // In C, functions are not lvalues.
340   // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
341   // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
342   // special-case this.
343 
344   if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
345     return Cl::CL_MemberFunction;
346 
347   bool islvalue;
348   if (const NonTypeTemplateParmDecl *NTTParm =
349         dyn_cast<NonTypeTemplateParmDecl>(D))
350     islvalue = NTTParm->getType()->isReferenceType();
351   else
352     islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
353 	  isa<IndirectFieldDecl>(D) ||
354       (Ctx.getLangOptions().CPlusPlus &&
355         (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
356 
357   return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
358 }
359 
360 /// ClassifyUnnamed - Return the classification of an expression yielding an
361 /// unnamed value of the given type. This applies in particular to function
362 /// calls and casts.
363 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
364   // In C, function calls are always rvalues.
365   if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
366 
367   // C++ [expr.call]p10: A function call is an lvalue if the result type is an
368   //   lvalue reference type or an rvalue reference to function type, an xvalue
369   //   if the result type is an rvalue refernence to object type, and a prvalue
370   //   otherwise.
371   if (T->isLValueReferenceType())
372     return Cl::CL_LValue;
373   const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
374   if (!RV) // Could still be a class temporary, though.
375     return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
376 
377   return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
378 }
379 
380 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
381   if (E->getType() == Ctx.UnknownAnyTy)
382     return (isa<FunctionDecl>(E->getMemberDecl())
383               ? Cl::CL_PRValue : Cl::CL_LValue);
384 
385   // Handle C first, it's easier.
386   if (!Ctx.getLangOptions().CPlusPlus) {
387     // C99 6.5.2.3p3
388     // For dot access, the expression is an lvalue if the first part is. For
389     // arrow access, it always is an lvalue.
390     if (E->isArrow())
391       return Cl::CL_LValue;
392     // ObjC property accesses are not lvalues, but get special treatment.
393     Expr *Base = E->getBase()->IgnoreParens();
394     if (isa<ObjCPropertyRefExpr>(Base))
395       return Cl::CL_SubObjCPropertySetting;
396     return ClassifyInternal(Ctx, Base);
397   }
398 
399   NamedDecl *Member = E->getMemberDecl();
400   // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
401   // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
402   //   E1.E2 is an lvalue.
403   if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
404     if (Value->getType()->isReferenceType())
405       return Cl::CL_LValue;
406 
407   //   Otherwise, one of the following rules applies.
408   //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
409   if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
410     return Cl::CL_LValue;
411 
412   //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
413   //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
414   //      otherwise, it is a prvalue.
415   if (isa<FieldDecl>(Member)) {
416     // *E1 is an lvalue
417     if (E->isArrow())
418       return Cl::CL_LValue;
419     Expr *Base = E->getBase()->IgnoreParenImpCasts();
420     if (isa<ObjCPropertyRefExpr>(Base))
421       return Cl::CL_SubObjCPropertySetting;
422     return ClassifyInternal(Ctx, E->getBase());
423   }
424 
425   //   -- If E2 is a [...] member function, [...]
426   //      -- If it refers to a static member function [...], then E1.E2 is an
427   //         lvalue; [...]
428   //      -- Otherwise [...] E1.E2 is a prvalue.
429   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
430     return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
431 
432   //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
433   // So is everything else we haven't handled yet.
434   return Cl::CL_PRValue;
435 }
436 
437 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
438   assert(Ctx.getLangOptions().CPlusPlus &&
439          "This is only relevant for C++.");
440   // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
441   // Except we override this for writes to ObjC properties.
442   if (E->isAssignmentOp())
443     return (E->getLHS()->getObjectKind() == OK_ObjCProperty
444               ? Cl::CL_PRValue : Cl::CL_LValue);
445 
446   // C++ [expr.comma]p1: the result is of the same value category as its right
447   //   operand, [...].
448   if (E->getOpcode() == BO_Comma)
449     return ClassifyInternal(Ctx, E->getRHS());
450 
451   // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
452   //   is a pointer to a data member is of the same value category as its first
453   //   operand.
454   if (E->getOpcode() == BO_PtrMemD)
455     return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
456       ClassifyInternal(Ctx, E->getLHS());
457 
458   // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
459   //   second operand is a pointer to data member and a prvalue otherwise.
460   if (E->getOpcode() == BO_PtrMemI)
461     return E->getType()->isFunctionType() ?
462       Cl::CL_MemberFunction : Cl::CL_LValue;
463 
464   // All other binary operations are prvalues.
465   return Cl::CL_PRValue;
466 }
467 
468 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
469                                      const Expr *False) {
470   assert(Ctx.getLangOptions().CPlusPlus &&
471          "This is only relevant for C++.");
472 
473   // C++ [expr.cond]p2
474   //   If either the second or the third operand has type (cv) void, [...]
475   //   the result [...] is a prvalue.
476   if (True->getType()->isVoidType() || False->getType()->isVoidType())
477     return Cl::CL_PRValue;
478 
479   // Note that at this point, we have already performed all conversions
480   // according to [expr.cond]p3.
481   // C++ [expr.cond]p4: If the second and third operands are glvalues of the
482   //   same value category [...], the result is of that [...] value category.
483   // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
484   Cl::Kinds LCl = ClassifyInternal(Ctx, True),
485             RCl = ClassifyInternal(Ctx, False);
486   return LCl == RCl ? LCl : Cl::CL_PRValue;
487 }
488 
489 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
490                                        Cl::Kinds Kind, SourceLocation &Loc) {
491   // As a general rule, we only care about lvalues. But there are some rvalues
492   // for which we want to generate special results.
493   if (Kind == Cl::CL_PRValue) {
494     // For the sake of better diagnostics, we want to specifically recognize
495     // use of the GCC cast-as-lvalue extension.
496     if (const ExplicitCastExpr *CE =
497           dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
498       if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
499         Loc = CE->getExprLoc();
500         return Cl::CM_LValueCast;
501       }
502     }
503   }
504   if (Kind != Cl::CL_LValue)
505     return Cl::CM_RValue;
506 
507   // This is the lvalue case.
508   // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
509   if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
510     return Cl::CM_Function;
511 
512   // You cannot assign to a variable outside a block from within the block if
513   // it is not marked __block, e.g.
514   //   void takeclosure(void (^C)(void));
515   //   void func() { int x = 1; takeclosure(^{ x = 7; }); }
516   if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
517     if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
518       return Cl::CM_NotBlockQualified;
519   }
520 
521   // Assignment to a property in ObjC is an implicit setter access. But a
522   // setter might not exist.
523   if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
524     if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
525       return Cl::CM_NoSetterProperty;
526   }
527 
528   CanQualType CT = Ctx.getCanonicalType(E->getType());
529   // Const stuff is obviously not modifiable.
530   if (CT.isConstQualified())
531     return Cl::CM_ConstQualified;
532   // Arrays are not modifiable, only their elements are.
533   if (CT->isArrayType())
534     return Cl::CM_ArrayType;
535   // Incomplete types are not modifiable.
536   if (CT->isIncompleteType())
537     return Cl::CM_IncompleteType;
538 
539   // Records with any const fields (recursively) are not modifiable.
540   if (const RecordType *R = CT->getAs<RecordType>()) {
541     assert((E->getObjectKind() == OK_ObjCProperty ||
542             !Ctx.getLangOptions().CPlusPlus) &&
543            "C++ struct assignment should be resolved by the "
544            "copy assignment operator.");
545     if (R->hasConstFields())
546       return Cl::CM_ConstQualified;
547   }
548 
549   return Cl::CM_Modifiable;
550 }
551 
552 Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
553   Classification VC = Classify(Ctx);
554   switch (VC.getKind()) {
555   case Cl::CL_LValue: return LV_Valid;
556   case Cl::CL_XValue: return LV_InvalidExpression;
557   case Cl::CL_Function: return LV_NotObjectType;
558   case Cl::CL_Void: return LV_IncompleteVoidType;
559   case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
560   case Cl::CL_MemberFunction: return LV_MemberFunction;
561   case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
562   case Cl::CL_ClassTemporary: return LV_ClassTemporary;
563   case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
564   case Cl::CL_PRValue: return LV_InvalidExpression;
565   }
566   llvm_unreachable("Unhandled kind");
567 }
568 
569 Expr::isModifiableLvalueResult
570 Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
571   SourceLocation dummy;
572   Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
573   switch (VC.getKind()) {
574   case Cl::CL_LValue: break;
575   case Cl::CL_XValue: return MLV_InvalidExpression;
576   case Cl::CL_Function: return MLV_NotObjectType;
577   case Cl::CL_Void: return MLV_IncompleteVoidType;
578   case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
579   case Cl::CL_MemberFunction: return MLV_MemberFunction;
580   case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
581   case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
582   case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
583   case Cl::CL_PRValue:
584     return VC.getModifiable() == Cl::CM_LValueCast ?
585       MLV_LValueCast : MLV_InvalidExpression;
586   }
587   assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
588   switch (VC.getModifiable()) {
589   case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
590   case Cl::CM_Modifiable: return MLV_Valid;
591   case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
592   case Cl::CM_Function: return MLV_NotObjectType;
593   case Cl::CM_LValueCast:
594     llvm_unreachable("CM_LValueCast and CL_LValue don't match");
595   case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
596   case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
597   case Cl::CM_ConstQualified: return MLV_ConstQualified;
598   case Cl::CM_ArrayType: return MLV_ArrayType;
599   case Cl::CM_IncompleteType: return MLV_IncompleteType;
600   }
601   llvm_unreachable("Unhandled modifiable type");
602 }
603