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