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