ExprClassification.cpp revision 61eee0ca33b29e102f11bab77c8b74cc00e2392b
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"
22using namespace clang;
23
24typedef Expr::Classification Cl;
25
26static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
28static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);
29static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
30static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);
31static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
32                                     const Expr *trueExpr,
33                                     const Expr *falseExpr);
34static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35                                       Cl::Kinds Kind, SourceLocation &Loc);
36
37static 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
53Cl 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
91static 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::CXXUnresolvedConstructExprClass:
121  case Expr::DependentScopeDeclRefExprClass:
122    // ObjC instance variables are lvalues
123    // FIXME: ObjC++0x might have different rules
124  case Expr::ObjCIvarRefExprClass:
125    return Cl::CL_LValue;
126    // C99 6.5.2.5p5 says that compound literals are lvalues.
127    // In C++, they're class temporaries.
128  case Expr::CompoundLiteralExprClass:
129    return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
130                                         : Cl::CL_LValue;
131
132    // Expressions that are prvalues.
133  case Expr::CXXBoolLiteralExprClass:
134  case Expr::CXXPseudoDestructorExprClass:
135  case Expr::UnaryExprOrTypeTraitExprClass:
136  case Expr::CXXNewExprClass:
137  case Expr::CXXThisExprClass:
138  case Expr::CXXNullPtrLiteralExprClass:
139  case Expr::ImaginaryLiteralClass:
140  case Expr::GNUNullExprClass:
141  case Expr::OffsetOfExprClass:
142  case Expr::CXXThrowExprClass:
143  case Expr::ShuffleVectorExprClass:
144  case Expr::IntegerLiteralClass:
145  case Expr::CharacterLiteralClass:
146  case Expr::AddrLabelExprClass:
147  case Expr::CXXDeleteExprClass:
148  case Expr::ImplicitValueInitExprClass:
149  case Expr::BlockExprClass:
150  case Expr::FloatingLiteralClass:
151  case Expr::CXXNoexceptExprClass:
152  case Expr::CXXScalarValueInitExprClass:
153  case Expr::UnaryTypeTraitExprClass:
154  case Expr::BinaryTypeTraitExprClass:
155  case Expr::ArrayTypeTraitExprClass:
156  case Expr::ExpressionTraitExprClass:
157  case Expr::ObjCSelectorExprClass:
158  case Expr::ObjCProtocolExprClass:
159  case Expr::ObjCStringLiteralClass:
160  case Expr::ParenListExprClass:
161  case Expr::InitListExprClass:
162  case Expr::SizeOfPackExprClass:
163  case Expr::SubstNonTypeTemplateParmPackExprClass:
164  case Expr::AsTypeExprClass:
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    // Only in C++ can casts be interesting at all.
293    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
294    return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
295
296  case Expr::BinaryConditionalOperatorClass: {
297    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
298    const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
299    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
300  }
301
302  case Expr::ConditionalOperatorClass: {
303    // Once again, only C++ is interesting.
304    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
305    const ConditionalOperator *co = cast<ConditionalOperator>(E);
306    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
307  }
308
309    // ObjC message sends are effectively function calls, if the target function
310    // is known.
311  case Expr::ObjCMessageExprClass:
312    if (const ObjCMethodDecl *Method =
313          cast<ObjCMessageExpr>(E)->getMethodDecl()) {
314      Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
315      return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
316    }
317    return Cl::CL_PRValue;
318
319    // Some C++ expressions are always class temporaries.
320  case Expr::CXXConstructExprClass:
321  case Expr::CXXTemporaryObjectExprClass:
322    return Cl::CL_ClassTemporary;
323
324  case Expr::VAArgExprClass:
325    return ClassifyUnnamed(Ctx, E->getType());
326
327  case Expr::DesignatedInitExprClass:
328    return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
329
330  case Expr::StmtExprClass: {
331    const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
332    if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
333      return ClassifyUnnamed(Ctx, LastExpr->getType());
334    return Cl::CL_PRValue;
335  }
336
337  case Expr::CXXUuidofExprClass:
338    return Cl::CL_LValue;
339
340  case Expr::PackExpansionExprClass:
341    return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
342  }
343
344  llvm_unreachable("unhandled expression kind in classification");
345  return Cl::CL_LValue;
346}
347
348/// ClassifyDecl - Return the classification of an expression referencing the
349/// given declaration.
350static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
351  // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
352  //   function, variable, or data member and a prvalue otherwise.
353  // In C, functions are not lvalues.
354  // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
355  // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
356  // special-case this.
357
358  if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
359    return Cl::CL_MemberFunction;
360
361  bool islvalue;
362  if (const NonTypeTemplateParmDecl *NTTParm =
363        dyn_cast<NonTypeTemplateParmDecl>(D))
364    islvalue = NTTParm->getType()->isReferenceType();
365  else
366    islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
367	  isa<IndirectFieldDecl>(D) ||
368      (Ctx.getLangOptions().CPlusPlus &&
369        (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
370
371  return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
372}
373
374/// ClassifyUnnamed - Return the classification of an expression yielding an
375/// unnamed value of the given type. This applies in particular to function
376/// calls and casts.
377static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
378  // In C, function calls are always rvalues.
379  if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
380
381  // C++ [expr.call]p10: A function call is an lvalue if the result type is an
382  //   lvalue reference type or an rvalue reference to function type, an xvalue
383  //   if the result type is an rvalue refernence to object type, and a prvalue
384  //   otherwise.
385  if (T->isLValueReferenceType())
386    return Cl::CL_LValue;
387  const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
388  if (!RV) // Could still be a class temporary, though.
389    return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
390
391  return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
392}
393
394static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
395  if (E->getType() == Ctx.UnknownAnyTy)
396    return (isa<FunctionDecl>(E->getMemberDecl())
397              ? Cl::CL_PRValue : Cl::CL_LValue);
398
399  // Handle C first, it's easier.
400  if (!Ctx.getLangOptions().CPlusPlus) {
401    // C99 6.5.2.3p3
402    // For dot access, the expression is an lvalue if the first part is. For
403    // arrow access, it always is an lvalue.
404    if (E->isArrow())
405      return Cl::CL_LValue;
406    // ObjC property accesses are not lvalues, but get special treatment.
407    Expr *Base = E->getBase()->IgnoreParens();
408    if (isa<ObjCPropertyRefExpr>(Base))
409      return Cl::CL_SubObjCPropertySetting;
410    return ClassifyInternal(Ctx, Base);
411  }
412
413  NamedDecl *Member = E->getMemberDecl();
414  // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
415  // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
416  //   E1.E2 is an lvalue.
417  if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
418    if (Value->getType()->isReferenceType())
419      return Cl::CL_LValue;
420
421  //   Otherwise, one of the following rules applies.
422  //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
423  if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
424    return Cl::CL_LValue;
425
426  //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
427  //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
428  //      otherwise, it is a prvalue.
429  if (isa<FieldDecl>(Member)) {
430    // *E1 is an lvalue
431    if (E->isArrow())
432      return Cl::CL_LValue;
433    Expr *Base = E->getBase()->IgnoreParenImpCasts();
434    if (isa<ObjCPropertyRefExpr>(Base))
435      return Cl::CL_SubObjCPropertySetting;
436    return ClassifyInternal(Ctx, E->getBase());
437  }
438
439  //   -- If E2 is a [...] member function, [...]
440  //      -- If it refers to a static member function [...], then E1.E2 is an
441  //         lvalue; [...]
442  //      -- Otherwise [...] E1.E2 is a prvalue.
443  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
444    return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
445
446  //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
447  // So is everything else we haven't handled yet.
448  return Cl::CL_PRValue;
449}
450
451static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
452  assert(Ctx.getLangOptions().CPlusPlus &&
453         "This is only relevant for C++.");
454  // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
455  // Except we override this for writes to ObjC properties.
456  if (E->isAssignmentOp())
457    return (E->getLHS()->getObjectKind() == OK_ObjCProperty
458              ? Cl::CL_PRValue : Cl::CL_LValue);
459
460  // C++ [expr.comma]p1: the result is of the same value category as its right
461  //   operand, [...].
462  if (E->getOpcode() == BO_Comma)
463    return ClassifyInternal(Ctx, E->getRHS());
464
465  // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
466  //   is a pointer to a data member is of the same value category as its first
467  //   operand.
468  if (E->getOpcode() == BO_PtrMemD)
469    return (E->getType()->isFunctionType() || E->getType() == Ctx.BoundMemberTy)
470             ? Cl::CL_MemberFunction
471             : ClassifyInternal(Ctx, E->getLHS());
472
473  // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
474  //   second operand is a pointer to data member and a prvalue otherwise.
475  if (E->getOpcode() == BO_PtrMemI)
476    return (E->getType()->isFunctionType() || E->getType() == Ctx.BoundMemberTy)
477             ? Cl::CL_MemberFunction
478             : Cl::CL_LValue;
479
480  // All other binary operations are prvalues.
481  return Cl::CL_PRValue;
482}
483
484static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
485                                     const Expr *False) {
486  assert(Ctx.getLangOptions().CPlusPlus &&
487         "This is only relevant for C++.");
488
489  // C++ [expr.cond]p2
490  //   If either the second or the third operand has type (cv) void, [...]
491  //   the result [...] is a prvalue.
492  if (True->getType()->isVoidType() || False->getType()->isVoidType())
493    return Cl::CL_PRValue;
494
495  // Note that at this point, we have already performed all conversions
496  // according to [expr.cond]p3.
497  // C++ [expr.cond]p4: If the second and third operands are glvalues of the
498  //   same value category [...], the result is of that [...] value category.
499  // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
500  Cl::Kinds LCl = ClassifyInternal(Ctx, True),
501            RCl = ClassifyInternal(Ctx, False);
502  return LCl == RCl ? LCl : Cl::CL_PRValue;
503}
504
505static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
506                                       Cl::Kinds Kind, SourceLocation &Loc) {
507  // As a general rule, we only care about lvalues. But there are some rvalues
508  // for which we want to generate special results.
509  if (Kind == Cl::CL_PRValue) {
510    // For the sake of better diagnostics, we want to specifically recognize
511    // use of the GCC cast-as-lvalue extension.
512    if (const ExplicitCastExpr *CE =
513          dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
514      if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
515        Loc = CE->getExprLoc();
516        return Cl::CM_LValueCast;
517      }
518    }
519  }
520  if (Kind != Cl::CL_LValue)
521    return Cl::CM_RValue;
522
523  // This is the lvalue case.
524  // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
525  if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
526    return Cl::CM_Function;
527
528  // You cannot assign to a variable outside a block from within the block if
529  // it is not marked __block, e.g.
530  //   void takeclosure(void (^C)(void));
531  //   void func() { int x = 1; takeclosure(^{ x = 7; }); }
532  if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
533    if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
534      return Cl::CM_NotBlockQualified;
535  }
536
537  // Assignment to a property in ObjC is an implicit setter access. But a
538  // setter might not exist.
539  if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
540    if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
541      return Cl::CM_NoSetterProperty;
542  }
543
544  CanQualType CT = Ctx.getCanonicalType(E->getType());
545  // Const stuff is obviously not modifiable.
546  if (CT.isConstQualified())
547    return Cl::CM_ConstQualified;
548  // Arrays are not modifiable, only their elements are.
549  if (CT->isArrayType())
550    return Cl::CM_ArrayType;
551  // Incomplete types are not modifiable.
552  if (CT->isIncompleteType())
553    return Cl::CM_IncompleteType;
554
555  // Records with any const fields (recursively) are not modifiable.
556  if (const RecordType *R = CT->getAs<RecordType>()) {
557    assert((E->getObjectKind() == OK_ObjCProperty ||
558            !Ctx.getLangOptions().CPlusPlus) &&
559           "C++ struct assignment should be resolved by the "
560           "copy assignment operator.");
561    if (R->hasConstFields())
562      return Cl::CM_ConstQualified;
563  }
564
565  return Cl::CM_Modifiable;
566}
567
568Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
569  Classification VC = Classify(Ctx);
570  switch (VC.getKind()) {
571  case Cl::CL_LValue: return LV_Valid;
572  case Cl::CL_XValue: return LV_InvalidExpression;
573  case Cl::CL_Function: return LV_NotObjectType;
574  case Cl::CL_Void: return LV_InvalidExpression;
575  case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
576  case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
577  case Cl::CL_MemberFunction: return LV_MemberFunction;
578  case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
579  case Cl::CL_ClassTemporary: return LV_ClassTemporary;
580  case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
581  case Cl::CL_PRValue: return LV_InvalidExpression;
582  }
583  llvm_unreachable("Unhandled kind");
584}
585
586Expr::isModifiableLvalueResult
587Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
588  SourceLocation dummy;
589  Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
590  switch (VC.getKind()) {
591  case Cl::CL_LValue: break;
592  case Cl::CL_XValue: return MLV_InvalidExpression;
593  case Cl::CL_Function: return MLV_NotObjectType;
594  case Cl::CL_Void: return MLV_InvalidExpression;
595  case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
596  case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
597  case Cl::CL_MemberFunction: return MLV_MemberFunction;
598  case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
599  case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
600  case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
601  case Cl::CL_PRValue:
602    return VC.getModifiable() == Cl::CM_LValueCast ?
603      MLV_LValueCast : MLV_InvalidExpression;
604  }
605  assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
606  switch (VC.getModifiable()) {
607  case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
608  case Cl::CM_Modifiable: return MLV_Valid;
609  case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
610  case Cl::CM_Function: return MLV_NotObjectType;
611  case Cl::CM_LValueCast:
612    llvm_unreachable("CM_LValueCast and CL_LValue don't match");
613  case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
614  case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
615  case Cl::CM_ConstQualified: return MLV_ConstQualified;
616  case Cl::CM_ArrayType: return MLV_ArrayType;
617  case Cl::CM_IncompleteType: return MLV_IncompleteType;
618  }
619  llvm_unreachable("Unhandled modifiable type");
620}
621