blob: 6ca18a21ddc6163d7fd1bb1a2d4279fc0f005a53 [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
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 Gregor4e442502010-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 Redlf9463102010-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 Gregor4e442502010-09-14 21:51:42 +000085 case Expr::CXXDependentScopeMemberExprClass:
86 case Expr::CXXUnresolvedConstructExprClass:
87 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +000088 // ObjC instance variables are lvalues
89 // FIXME: ObjC++0x might have different rules
90 case Expr::ObjCIvarRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +000091 return Cl::CL_LValue;
Douglas Gregor4e442502010-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:
112 case Expr::ObjCSuperExprClass:
113 case Expr::CharacterLiteralClass:
114 case Expr::AddrLabelExprClass:
115 case Expr::CXXDeleteExprClass:
116 case Expr::ImplicitValueInitExprClass:
117 case Expr::BlockExprClass:
118 case Expr::FloatingLiteralClass:
119 case Expr::CXXNoexceptExprClass:
120 case Expr::CXXScalarValueInitExprClass:
121 case Expr::UnaryTypeTraitExprClass:
122 case Expr::ObjCSelectorExprClass:
123 case Expr::ObjCProtocolExprClass:
124 case Expr::ObjCStringLiteralClass:
125 case Expr::ParenListExprClass:
126 case Expr::InitListExprClass:
127 return Cl::CL_PRValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000128
129 // Next come the complicated cases.
130
131 // C++ [expr.sub]p1: The result is an lvalue of type "T".
132 // However, subscripting vector types is more like member access.
133 case Expr::ArraySubscriptExprClass:
134 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
135 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
136 return Cl::CL_LValue;
137
138 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
139 // function or variable and a prvalue otherwise.
140 case Expr::DeclRefExprClass:
141 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
142 // We deal with names referenced from blocks the same way.
143 case Expr::BlockDeclRefExprClass:
144 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
145
146 // Member access is complex.
147 case Expr::MemberExprClass:
148 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
149
150 case Expr::UnaryOperatorClass:
151 switch (cast<UnaryOperator>(E)->getOpcode()) {
152 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
153 // [...] the result is an lvalue referring to the object or function
154 // to which the expression points.
John McCalle3027922010-08-25 11:45:40 +0000155 case UO_Deref:
Sebastian Redlf9463102010-06-28 15:09:07 +0000156 return Cl::CL_LValue;
157
158 // GNU extensions, simply look through them.
John McCalle3027922010-08-25 11:45:40 +0000159 case UO_Real:
160 case UO_Imag:
161 case UO_Extension:
Sebastian Redlf9463102010-06-28 15:09:07 +0000162 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
163
164 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
165 // lvalue, [...]
166 // Not so in C.
John McCalle3027922010-08-25 11:45:40 +0000167 case UO_PreInc:
168 case UO_PreDec:
Sebastian Redlf9463102010-06-28 15:09:07 +0000169 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
170
171 default:
172 return Cl::CL_PRValue;
173 }
174
175 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
176 // only specifically record class temporaries.
177 case Expr::ImplicitCastExprClass:
John McCall2536c6d2010-08-25 10:28:54 +0000178 switch (cast<ImplicitCastExpr>(E)->getValueKind()) {
179 case VK_RValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000180 return Lang.CPlusPlus && E->getType()->isRecordType() ?
181 Cl::CL_ClassTemporary : Cl::CL_PRValue;
John McCall2536c6d2010-08-25 10:28:54 +0000182 case VK_LValue:
Sebastian Redlf9463102010-06-28 15:09:07 +0000183 return Cl::CL_LValue;
John McCall2536c6d2010-08-25 10:28:54 +0000184 case VK_XValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000185 return Cl::CL_XValue;
186 }
187 llvm_unreachable("Invalid value category of implicit cast.");
Sebastian Redlf9463102010-06-28 15:09:07 +0000188
189 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
190 // whether the expression is an lvalue.
191 case Expr::ParenExprClass:
192 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
193
194 case Expr::BinaryOperatorClass:
195 case Expr::CompoundAssignOperatorClass:
196 // C doesn't have any binary expressions that are lvalues.
197 if (Lang.CPlusPlus)
198 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
199 return Cl::CL_PRValue;
200
201 case Expr::CallExprClass:
202 case Expr::CXXOperatorCallExprClass:
203 case Expr::CXXMemberCallExprClass:
204 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
205
206 // __builtin_choose_expr is equivalent to the chosen expression.
207 case Expr::ChooseExprClass:
208 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
209
210 // Extended vector element access is an lvalue unless there are duplicates
211 // in the shuffle expression.
212 case Expr::ExtVectorElementExprClass:
213 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
214 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
215
216 // Simply look at the actual default argument.
217 case Expr::CXXDefaultArgExprClass:
218 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
219
220 // Same idea for temporary binding.
221 case Expr::CXXBindTemporaryExprClass:
222 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
223
224 // And the temporary lifetime guard.
225 case Expr::CXXExprWithTemporariesClass:
226 return ClassifyInternal(Ctx, cast<CXXExprWithTemporaries>(E)->getSubExpr());
227
228 // Casts depend completely on the target type. All casts work the same.
229 case Expr::CStyleCastExprClass:
230 case Expr::CXXFunctionalCastExprClass:
231 case Expr::CXXStaticCastExprClass:
232 case Expr::CXXDynamicCastExprClass:
233 case Expr::CXXReinterpretCastExprClass:
234 case Expr::CXXConstCastExprClass:
235 // Only in C++ can casts be interesting at all.
236 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
237 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
238
239 case Expr::ConditionalOperatorClass:
240 // Once again, only C++ is interesting.
241 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
242 return ClassifyConditional(Ctx, cast<ConditionalOperator>(E));
243
244 // ObjC message sends are effectively function calls, if the target function
245 // is known.
246 case Expr::ObjCMessageExprClass:
247 if (const ObjCMethodDecl *Method =
248 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
249 return ClassifyUnnamed(Ctx, Method->getResultType());
250 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000251 return Cl::CL_PRValue;
252
Sebastian Redlf9463102010-06-28 15:09:07 +0000253 // Some C++ expressions are always class temporaries.
254 case Expr::CXXConstructExprClass:
255 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000256 return Cl::CL_ClassTemporary;
257
Douglas Gregor4e442502010-09-14 21:51:42 +0000258 case Expr::VAArgExprClass:
259 return ClassifyUnnamed(Ctx, E->getType());
260
261 case Expr::DesignatedInitExprClass:
262 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
263
264 case Expr::StmtExprClass: {
265 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
266 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
267 return ClassifyInternal(Ctx, LastExpr);
Sebastian Redlf9463102010-06-28 15:09:07 +0000268 return Cl::CL_PRValue;
269 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000270
271 case Expr::CXXUuidofExprClass:
272 // Assume that Microsoft's __uuidof returns an lvalue, like typeid does.
273 // FIXME: Is this really the case?
274 return Cl::CL_LValue;
275 }
276
277 llvm_unreachable("unhandled expression kind in classification");
278 return Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000279}
280
281/// ClassifyDecl - Return the classification of an expression referencing the
282/// given declaration.
283static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
284 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
285 // function, variable, or data member and a prvalue otherwise.
286 // In C, functions are not lvalues.
287 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
288 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
289 // special-case this.
John McCall8d08b9b2010-08-27 09:08:28 +0000290
291 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
292 return Cl::CL_MemberFunction;
293
Sebastian Redlf9463102010-06-28 15:09:07 +0000294 bool islvalue;
295 if (const NonTypeTemplateParmDecl *NTTParm =
296 dyn_cast<NonTypeTemplateParmDecl>(D))
297 islvalue = NTTParm->getType()->isReferenceType();
298 else
299 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
300 (Ctx.getLangOptions().CPlusPlus &&
301 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
302
303 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
304}
305
306/// ClassifyUnnamed - Return the classification of an expression yielding an
307/// unnamed value of the given type. This applies in particular to function
308/// calls and casts.
309static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
310 // In C, function calls are always rvalues.
311 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
312
313 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
314 // lvalue reference type or an rvalue reference to function type, an xvalue
315 // if the result type is an rvalue refernence to object type, and a prvalue
316 // otherwise.
317 if (T->isLValueReferenceType())
318 return Cl::CL_LValue;
319 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
320 if (!RV) // Could still be a class temporary, though.
321 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
322
323 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
324}
325
326static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
327 // Handle C first, it's easier.
328 if (!Ctx.getLangOptions().CPlusPlus) {
329 // C99 6.5.2.3p3
330 // For dot access, the expression is an lvalue if the first part is. For
331 // arrow access, it always is an lvalue.
332 if (E->isArrow())
333 return Cl::CL_LValue;
334 // ObjC property accesses are not lvalues, but get special treatment.
335 Expr *Base = E->getBase();
336 if (isa<ObjCPropertyRefExpr>(Base) ||
337 isa<ObjCImplicitSetterGetterRefExpr>(Base))
338 return Cl::CL_SubObjCPropertySetting;
339 return ClassifyInternal(Ctx, Base);
340 }
341
342 NamedDecl *Member = E->getMemberDecl();
343 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
344 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
345 // E1.E2 is an lvalue.
346 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
347 if (Value->getType()->isReferenceType())
348 return Cl::CL_LValue;
349
350 // Otherwise, one of the following rules applies.
351 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
352 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
353 return Cl::CL_LValue;
354
355 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
356 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
357 // otherwise, it is a prvalue.
358 if (isa<FieldDecl>(Member)) {
359 // *E1 is an lvalue
360 if (E->isArrow())
361 return Cl::CL_LValue;
362 return ClassifyInternal(Ctx, E->getBase());
363 }
364
365 // -- If E2 is a [...] member function, [...]
366 // -- If it refers to a static member function [...], then E1.E2 is an
367 // lvalue; [...]
368 // -- Otherwise [...] E1.E2 is a prvalue.
369 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
370 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
371
372 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
373 // So is everything else we haven't handled yet.
374 return Cl::CL_PRValue;
375}
376
377static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
378 assert(Ctx.getLangOptions().CPlusPlus &&
379 "This is only relevant for C++.");
380 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
381 if (E->isAssignmentOp())
382 return Cl::CL_LValue;
383
384 // C++ [expr.comma]p1: the result is of the same value category as its right
385 // operand, [...].
John McCalle3027922010-08-25 11:45:40 +0000386 if (E->getOpcode() == BO_Comma)
Sebastian Redlf9463102010-06-28 15:09:07 +0000387 return ClassifyInternal(Ctx, E->getRHS());
388
389 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
390 // is a pointer to a data member is of the same value category as its first
391 // operand.
John McCalle3027922010-08-25 11:45:40 +0000392 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redlf9463102010-06-28 15:09:07 +0000393 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
394 ClassifyInternal(Ctx, E->getLHS());
395
396 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
397 // second operand is a pointer to data member and a prvalue otherwise.
John McCalle3027922010-08-25 11:45:40 +0000398 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redlf9463102010-06-28 15:09:07 +0000399 return E->getType()->isFunctionType() ?
400 Cl::CL_MemberFunction : Cl::CL_LValue;
401
402 // All other binary operations are prvalues.
403 return Cl::CL_PRValue;
404}
405
406static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
407 const ConditionalOperator *E) {
408 assert(Ctx.getLangOptions().CPlusPlus &&
409 "This is only relevant for C++.");
410
411 Expr *True = E->getTrueExpr();
412 Expr *False = E->getFalseExpr();
413 // C++ [expr.cond]p2
414 // If either the second or the third operand has type (cv) void, [...]
415 // the result [...] is a prvalue.
416 if (True->getType()->isVoidType() || False->getType()->isVoidType())
417 return Cl::CL_PRValue;
418
419 // Note that at this point, we have already performed all conversions
420 // according to [expr.cond]p3.
421 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
422 // same value category [...], the result is of that [...] value category.
423 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
424 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
425 RCl = ClassifyInternal(Ctx, False);
426 return LCl == RCl ? LCl : Cl::CL_PRValue;
427}
428
429static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
430 Cl::Kinds Kind, SourceLocation &Loc) {
431 // As a general rule, we only care about lvalues. But there are some rvalues
432 // for which we want to generate special results.
433 if (Kind == Cl::CL_PRValue) {
434 // For the sake of better diagnostics, we want to specifically recognize
435 // use of the GCC cast-as-lvalue extension.
436 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E->IgnoreParens())){
437 if (CE->getSubExpr()->Classify(Ctx).isLValue()) {
438 Loc = CE->getLParenLoc();
439 return Cl::CM_LValueCast;
440 }
441 }
442 }
443 if (Kind != Cl::CL_LValue)
444 return Cl::CM_RValue;
445
446 // This is the lvalue case.
447 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
448 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
449 return Cl::CM_Function;
450
451 // You cannot assign to a variable outside a block from within the block if
452 // it is not marked __block, e.g.
453 // void takeclosure(void (^C)(void));
454 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
455 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
456 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
457 return Cl::CM_NotBlockQualified;
458 }
459
460 // Assignment to a property in ObjC is an implicit setter access. But a
461 // setter might not exist.
462 if (const ObjCImplicitSetterGetterRefExpr *Expr =
463 dyn_cast<ObjCImplicitSetterGetterRefExpr>(E)) {
464 if (Expr->getSetterMethod() == 0)
465 return Cl::CM_NoSetterProperty;
466 }
467
468 CanQualType CT = Ctx.getCanonicalType(E->getType());
469 // Const stuff is obviously not modifiable.
470 if (CT.isConstQualified())
471 return Cl::CM_ConstQualified;
472 // Arrays are not modifiable, only their elements are.
473 if (CT->isArrayType())
474 return Cl::CM_ArrayType;
475 // Incomplete types are not modifiable.
476 if (CT->isIncompleteType())
477 return Cl::CM_IncompleteType;
478
479 // Records with any const fields (recursively) are not modifiable.
480 if (const RecordType *R = CT->getAs<RecordType>()) {
Fariborz Jahaniane89d03f2010-09-09 23:01:10 +0000481 assert((isa<ObjCImplicitSetterGetterRefExpr>(E) ||
482 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redlf9463102010-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 Carruth8337ba62010-06-29 00:23:11 +0000505 llvm_unreachable("Unhandled kind");
Sebastian Redlf9463102010-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 Carruth8337ba62010-06-29 00:23:11 +0000527 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redlf9463102010-06-28 15:09:07 +0000528 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth8337ba62010-06-29 00:23:11 +0000529 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000530 case Cl::CM_Function: return MLV_NotObjectType;
531 case Cl::CM_LValueCast:
Chandler Carruth8337ba62010-06-29 00:23:11 +0000532 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redlf9463102010-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 Carruth8337ba62010-06-29 00:23:11 +0000539 llvm_unreachable("Unhandled modifiable type");
Sebastian Redlf9463102010-06-28 15:09:07 +0000540}