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