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