blob: 4025f18a3b44670abbaae8cc7e3599550a2e9a02 [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
John McCall7cd7d1a2010-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 Redl2111c852010-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
John McCall09431682010-11-18 19:01:18 +000067 // Enable this assertion for testing.
68 switch (kind) {
69 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
70 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
71 case Cl::CL_Function:
72 case Cl::CL_Void:
73 case Cl::CL_DuplicateVectorComponents:
74 case Cl::CL_MemberFunction:
75 case Cl::CL_SubObjCPropertySetting:
76 case Cl::CL_ClassTemporary:
77 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
78 }
John McCall09431682010-11-18 19:01:18 +000079
Sebastian Redl2111c852010-06-28 15:09:07 +000080 Cl::ModifiableType modifiable = Cl::CM_Untested;
81 if (Loc)
82 modifiable = IsModifiable(Ctx, this, kind, *Loc);
83 return Classification(kind, modifiable);
84}
85
86static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
87 // This function takes the first stab at classifying expressions.
88 const LangOptions &Lang = Ctx.getLangOptions();
89
90 switch (E->getStmtClass()) {
91 // First come the expressions that are always lvalues, unconditionally.
Douglas Gregor4178b572010-09-14 21:51:42 +000092 case Stmt::NoStmtClass:
93#define STMT(Kind, Base) case Expr::Kind##Class:
94#define EXPR(Kind, Base)
95#include "clang/AST/StmtNodes.inc"
96 llvm_unreachable("cannot classify a statement");
97 break;
Sebastian Redl2111c852010-06-28 15:09:07 +000098 case Expr::ObjCIsaExprClass:
99 // C++ [expr.prim.general]p1: A string literal is an lvalue.
100 case Expr::StringLiteralClass:
101 // @encode is equivalent to its string
102 case Expr::ObjCEncodeExprClass:
103 // __func__ and friends are too.
104 case Expr::PredefinedExprClass:
105 // Property references are lvalues
106 case Expr::ObjCPropertyRefExprClass:
107 case Expr::ObjCImplicitSetterGetterRefExprClass:
108 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
109 case Expr::CXXTypeidExprClass:
110 // Unresolved lookups get classified as lvalues.
111 // FIXME: Is this wise? Should they get their own kind?
112 case Expr::UnresolvedLookupExprClass:
113 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000114 case Expr::CXXDependentScopeMemberExprClass:
115 case Expr::CXXUnresolvedConstructExprClass:
116 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000117 // ObjC instance variables are lvalues
118 // FIXME: ObjC++0x might have different rules
119 case Expr::ObjCIvarRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000120 return Cl::CL_LValue;
Douglas Gregor4178b572010-09-14 21:51:42 +0000121 // C99 6.5.2.5p5 says that compound literals are lvalues.
122 // In C++, they're class temporaries.
123 case Expr::CompoundLiteralExprClass:
124 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
125 : Cl::CL_LValue;
126
127 // Expressions that are prvalues.
128 case Expr::CXXBoolLiteralExprClass:
129 case Expr::CXXPseudoDestructorExprClass:
130 case Expr::SizeOfAlignOfExprClass:
131 case Expr::CXXNewExprClass:
132 case Expr::CXXThisExprClass:
133 case Expr::CXXNullPtrLiteralExprClass:
134 case Expr::TypesCompatibleExprClass:
135 case Expr::ImaginaryLiteralClass:
136 case Expr::GNUNullExprClass:
137 case Expr::OffsetOfExprClass:
138 case Expr::CXXThrowExprClass:
139 case Expr::ShuffleVectorExprClass:
140 case Expr::IntegerLiteralClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000141 case Expr::CharacterLiteralClass:
142 case Expr::AddrLabelExprClass:
143 case Expr::CXXDeleteExprClass:
144 case Expr::ImplicitValueInitExprClass:
145 case Expr::BlockExprClass:
146 case Expr::FloatingLiteralClass:
147 case Expr::CXXNoexceptExprClass:
148 case Expr::CXXScalarValueInitExprClass:
149 case Expr::UnaryTypeTraitExprClass:
150 case Expr::ObjCSelectorExprClass:
151 case Expr::ObjCProtocolExprClass:
152 case Expr::ObjCStringLiteralClass:
153 case Expr::ParenListExprClass:
154 case Expr::InitListExprClass:
155 return Cl::CL_PRValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000156
157 // Next come the complicated cases.
158
159 // C++ [expr.sub]p1: The result is an lvalue of type "T".
160 // However, subscripting vector types is more like member access.
161 case Expr::ArraySubscriptExprClass:
162 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
163 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
164 return Cl::CL_LValue;
165
166 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
167 // function or variable and a prvalue otherwise.
168 case Expr::DeclRefExprClass:
169 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
170 // We deal with names referenced from blocks the same way.
171 case Expr::BlockDeclRefExprClass:
172 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
173
174 // Member access is complex.
175 case Expr::MemberExprClass:
176 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
177
178 case Expr::UnaryOperatorClass:
179 switch (cast<UnaryOperator>(E)->getOpcode()) {
180 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
181 // [...] the result is an lvalue referring to the object or function
182 // to which the expression points.
John McCall2de56d12010-08-25 11:45:40 +0000183 case UO_Deref:
Sebastian Redl2111c852010-06-28 15:09:07 +0000184 return Cl::CL_LValue;
185
186 // GNU extensions, simply look through them.
John McCall2de56d12010-08-25 11:45:40 +0000187 case UO_Extension:
Sebastian Redl2111c852010-06-28 15:09:07 +0000188 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
189
John McCallb418d742010-11-16 10:08:07 +0000190 // Treat _Real and _Imag basically as if they were member
191 // expressions: l-value only if the operand is a true l-value.
192 case UO_Real:
193 case UO_Imag: {
194 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
195 Cl::Kinds K = ClassifyInternal(Ctx, Op);
196 if (K != Cl::CL_LValue) return K;
197
198 if (isa<ObjCPropertyRefExpr>(Op) ||
199 isa<ObjCImplicitSetterGetterRefExpr>(Op))
200 return Cl::CL_SubObjCPropertySetting;
201 return Cl::CL_LValue;
202 }
203
Sebastian Redl2111c852010-06-28 15:09:07 +0000204 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
205 // lvalue, [...]
206 // Not so in C.
John McCall2de56d12010-08-25 11:45:40 +0000207 case UO_PreInc:
208 case UO_PreDec:
Sebastian Redl2111c852010-06-28 15:09:07 +0000209 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
210
211 default:
212 return Cl::CL_PRValue;
213 }
214
John McCall7cd7d1a2010-11-15 23:31:06 +0000215 case Expr::OpaqueValueExprClass:
216 return ClassifyExprValueKind(Lang, E,
217 cast<OpaqueValueExpr>(E)->getValueKind());
218
Sebastian Redl2111c852010-06-28 15:09:07 +0000219 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
220 // only specifically record class temporaries.
221 case Expr::ImplicitCastExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000222 return ClassifyExprValueKind(Lang, E,
223 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redl2111c852010-06-28 15:09:07 +0000224
225 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
226 // whether the expression is an lvalue.
227 case Expr::ParenExprClass:
228 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
229
230 case Expr::BinaryOperatorClass:
231 case Expr::CompoundAssignOperatorClass:
232 // C doesn't have any binary expressions that are lvalues.
233 if (Lang.CPlusPlus)
234 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
235 return Cl::CL_PRValue;
236
237 case Expr::CallExprClass:
238 case Expr::CXXOperatorCallExprClass:
239 case Expr::CXXMemberCallExprClass:
240 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
241
242 // __builtin_choose_expr is equivalent to the chosen expression.
243 case Expr::ChooseExprClass:
244 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
245
246 // Extended vector element access is an lvalue unless there are duplicates
247 // in the shuffle expression.
248 case Expr::ExtVectorElementExprClass:
249 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
250 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
251
252 // Simply look at the actual default argument.
253 case Expr::CXXDefaultArgExprClass:
254 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
255
256 // Same idea for temporary binding.
257 case Expr::CXXBindTemporaryExprClass:
258 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
259
260 // And the temporary lifetime guard.
261 case Expr::CXXExprWithTemporariesClass:
262 return ClassifyInternal(Ctx, cast<CXXExprWithTemporaries>(E)->getSubExpr());
263
264 // Casts depend completely on the target type. All casts work the same.
265 case Expr::CStyleCastExprClass:
266 case Expr::CXXFunctionalCastExprClass:
267 case Expr::CXXStaticCastExprClass:
268 case Expr::CXXDynamicCastExprClass:
269 case Expr::CXXReinterpretCastExprClass:
270 case Expr::CXXConstCastExprClass:
271 // Only in C++ can casts be interesting at all.
272 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
273 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
274
275 case Expr::ConditionalOperatorClass:
276 // Once again, only C++ is interesting.
277 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
278 return ClassifyConditional(Ctx, cast<ConditionalOperator>(E));
279
280 // ObjC message sends are effectively function calls, if the target function
281 // is known.
282 case Expr::ObjCMessageExprClass:
283 if (const ObjCMethodDecl *Method =
284 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
285 return ClassifyUnnamed(Ctx, Method->getResultType());
286 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000287 return Cl::CL_PRValue;
288
Sebastian Redl2111c852010-06-28 15:09:07 +0000289 // Some C++ expressions are always class temporaries.
290 case Expr::CXXConstructExprClass:
291 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000292 return Cl::CL_ClassTemporary;
293
Douglas Gregor4178b572010-09-14 21:51:42 +0000294 case Expr::VAArgExprClass:
295 return ClassifyUnnamed(Ctx, E->getType());
296
297 case Expr::DesignatedInitExprClass:
298 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
299
300 case Expr::StmtExprClass: {
301 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
302 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregor226cbfc2010-09-15 01:37:48 +0000303 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redl2111c852010-06-28 15:09:07 +0000304 return Cl::CL_PRValue;
305 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000306
307 case Expr::CXXUuidofExprClass:
John McCallf89e55a2010-11-18 06:31:45 +0000308 return Cl::CL_PRValue;
Douglas Gregor4178b572010-09-14 21:51:42 +0000309 }
310
311 llvm_unreachable("unhandled expression kind in classification");
312 return Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000313}
314
315/// ClassifyDecl - Return the classification of an expression referencing the
316/// given declaration.
317static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
318 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
319 // function, variable, or data member and a prvalue otherwise.
320 // In C, functions are not lvalues.
321 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
322 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
323 // special-case this.
John McCall9c72c602010-08-27 09:08:28 +0000324
325 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
326 return Cl::CL_MemberFunction;
327
Sebastian Redl2111c852010-06-28 15:09:07 +0000328 bool islvalue;
329 if (const NonTypeTemplateParmDecl *NTTParm =
330 dyn_cast<NonTypeTemplateParmDecl>(D))
331 islvalue = NTTParm->getType()->isReferenceType();
332 else
333 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet87c2e122010-11-21 06:08:52 +0000334 isa<IndirectFieldDecl>(D) ||
Sebastian Redl2111c852010-06-28 15:09:07 +0000335 (Ctx.getLangOptions().CPlusPlus &&
336 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
337
338 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
339}
340
341/// ClassifyUnnamed - Return the classification of an expression yielding an
342/// unnamed value of the given type. This applies in particular to function
343/// calls and casts.
344static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
345 // In C, function calls are always rvalues.
346 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
347
348 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
349 // lvalue reference type or an rvalue reference to function type, an xvalue
350 // if the result type is an rvalue refernence to object type, and a prvalue
351 // otherwise.
352 if (T->isLValueReferenceType())
353 return Cl::CL_LValue;
354 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
355 if (!RV) // Could still be a class temporary, though.
356 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
357
358 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
359}
360
361static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
362 // Handle C first, it's easier.
363 if (!Ctx.getLangOptions().CPlusPlus) {
364 // C99 6.5.2.3p3
365 // For dot access, the expression is an lvalue if the first part is. For
366 // arrow access, it always is an lvalue.
367 if (E->isArrow())
368 return Cl::CL_LValue;
369 // ObjC property accesses are not lvalues, but get special treatment.
John McCallb418d742010-11-16 10:08:07 +0000370 Expr *Base = E->getBase()->IgnoreParens();
Sebastian Redl2111c852010-06-28 15:09:07 +0000371 if (isa<ObjCPropertyRefExpr>(Base) ||
372 isa<ObjCImplicitSetterGetterRefExpr>(Base))
373 return Cl::CL_SubObjCPropertySetting;
374 return ClassifyInternal(Ctx, Base);
375 }
376
377 NamedDecl *Member = E->getMemberDecl();
378 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
379 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
380 // E1.E2 is an lvalue.
381 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
382 if (Value->getType()->isReferenceType())
383 return Cl::CL_LValue;
384
385 // Otherwise, one of the following rules applies.
386 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
387 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
388 return Cl::CL_LValue;
389
390 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
391 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
392 // otherwise, it is a prvalue.
393 if (isa<FieldDecl>(Member)) {
394 // *E1 is an lvalue
395 if (E->isArrow())
396 return Cl::CL_LValue;
John McCall09431682010-11-18 19:01:18 +0000397 Expr *Base = E->getBase()->IgnoreParenImpCasts();
398 if (isa<ObjCPropertyRefExpr>(Base) ||
399 isa<ObjCImplicitSetterGetterRefExpr>(Base))
400 return Cl::CL_SubObjCPropertySetting;
Sebastian Redl2111c852010-06-28 15:09:07 +0000401 return ClassifyInternal(Ctx, E->getBase());
402 }
403
404 // -- If E2 is a [...] member function, [...]
405 // -- If it refers to a static member function [...], then E1.E2 is an
406 // lvalue; [...]
407 // -- Otherwise [...] E1.E2 is a prvalue.
408 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
409 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
410
411 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
412 // So is everything else we haven't handled yet.
413 return Cl::CL_PRValue;
414}
415
416static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
417 assert(Ctx.getLangOptions().CPlusPlus &&
418 "This is only relevant for C++.");
419 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
420 if (E->isAssignmentOp())
421 return Cl::CL_LValue;
422
423 // C++ [expr.comma]p1: the result is of the same value category as its right
424 // operand, [...].
John McCall2de56d12010-08-25 11:45:40 +0000425 if (E->getOpcode() == BO_Comma)
Sebastian Redl2111c852010-06-28 15:09:07 +0000426 return ClassifyInternal(Ctx, E->getRHS());
427
428 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
429 // is a pointer to a data member is of the same value category as its first
430 // operand.
John McCall2de56d12010-08-25 11:45:40 +0000431 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redl2111c852010-06-28 15:09:07 +0000432 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
433 ClassifyInternal(Ctx, E->getLHS());
434
435 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
436 // second operand is a pointer to data member and a prvalue otherwise.
John McCall2de56d12010-08-25 11:45:40 +0000437 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redl2111c852010-06-28 15:09:07 +0000438 return E->getType()->isFunctionType() ?
439 Cl::CL_MemberFunction : Cl::CL_LValue;
440
441 // All other binary operations are prvalues.
442 return Cl::CL_PRValue;
443}
444
445static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
446 const ConditionalOperator *E) {
447 assert(Ctx.getLangOptions().CPlusPlus &&
448 "This is only relevant for C++.");
449
450 Expr *True = E->getTrueExpr();
451 Expr *False = E->getFalseExpr();
452 // C++ [expr.cond]p2
453 // If either the second or the third operand has type (cv) void, [...]
454 // the result [...] is a prvalue.
455 if (True->getType()->isVoidType() || False->getType()->isVoidType())
456 return Cl::CL_PRValue;
457
458 // Note that at this point, we have already performed all conversions
459 // according to [expr.cond]p3.
460 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
461 // same value category [...], the result is of that [...] value category.
462 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
463 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
464 RCl = ClassifyInternal(Ctx, False);
465 return LCl == RCl ? LCl : Cl::CL_PRValue;
466}
467
468static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
469 Cl::Kinds Kind, SourceLocation &Loc) {
470 // As a general rule, we only care about lvalues. But there are some rvalues
471 // for which we want to generate special results.
472 if (Kind == Cl::CL_PRValue) {
473 // For the sake of better diagnostics, we want to specifically recognize
474 // use of the GCC cast-as-lvalue extension.
475 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E->IgnoreParens())){
476 if (CE->getSubExpr()->Classify(Ctx).isLValue()) {
477 Loc = CE->getLParenLoc();
478 return Cl::CM_LValueCast;
479 }
480 }
481 }
482 if (Kind != Cl::CL_LValue)
483 return Cl::CM_RValue;
484
485 // This is the lvalue case.
486 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
487 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
488 return Cl::CM_Function;
489
490 // You cannot assign to a variable outside a block from within the block if
491 // it is not marked __block, e.g.
492 // void takeclosure(void (^C)(void));
493 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
494 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
495 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
496 return Cl::CM_NotBlockQualified;
497 }
498
499 // Assignment to a property in ObjC is an implicit setter access. But a
500 // setter might not exist.
501 if (const ObjCImplicitSetterGetterRefExpr *Expr =
502 dyn_cast<ObjCImplicitSetterGetterRefExpr>(E)) {
503 if (Expr->getSetterMethod() == 0)
504 return Cl::CM_NoSetterProperty;
505 }
506
507 CanQualType CT = Ctx.getCanonicalType(E->getType());
508 // Const stuff is obviously not modifiable.
509 if (CT.isConstQualified())
510 return Cl::CM_ConstQualified;
511 // Arrays are not modifiable, only their elements are.
512 if (CT->isArrayType())
513 return Cl::CM_ArrayType;
514 // Incomplete types are not modifiable.
515 if (CT->isIncompleteType())
516 return Cl::CM_IncompleteType;
517
518 // Records with any const fields (recursively) are not modifiable.
519 if (const RecordType *R = CT->getAs<RecordType>()) {
Fariborz Jahanianc4e1a682010-09-14 23:02:38 +0000520 assert((isa<ObjCImplicitSetterGetterRefExpr>(E) ||
521 isa<ObjCPropertyRefExpr>(E) ||
Fariborz Jahanian4088ec02010-09-09 23:01:10 +0000522 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redl2111c852010-06-28 15:09:07 +0000523 "C++ struct assignment should be resolved by the "
524 "copy assignment operator.");
525 if (R->hasConstFields())
526 return Cl::CM_ConstQualified;
527 }
528
529 return Cl::CM_Modifiable;
530}
531
John McCall7eb0a9e2010-11-24 05:12:34 +0000532Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redl2111c852010-06-28 15:09:07 +0000533 Classification VC = Classify(Ctx);
534 switch (VC.getKind()) {
535 case Cl::CL_LValue: return LV_Valid;
536 case Cl::CL_XValue: return LV_InvalidExpression;
537 case Cl::CL_Function: return LV_NotObjectType;
538 case Cl::CL_Void: return LV_IncompleteVoidType;
539 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
540 case Cl::CL_MemberFunction: return LV_MemberFunction;
541 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
542 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
543 case Cl::CL_PRValue: return LV_InvalidExpression;
544 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000545 llvm_unreachable("Unhandled kind");
Sebastian Redl2111c852010-06-28 15:09:07 +0000546}
547
548Expr::isModifiableLvalueResult
549Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
550 SourceLocation dummy;
551 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
552 switch (VC.getKind()) {
553 case Cl::CL_LValue: break;
554 case Cl::CL_XValue: return MLV_InvalidExpression;
555 case Cl::CL_Function: return MLV_NotObjectType;
556 case Cl::CL_Void: return MLV_IncompleteVoidType;
557 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
558 case Cl::CL_MemberFunction: return MLV_MemberFunction;
559 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
560 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
561 case Cl::CL_PRValue:
562 return VC.getModifiable() == Cl::CM_LValueCast ?
563 MLV_LValueCast : MLV_InvalidExpression;
564 }
565 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
566 switch (VC.getModifiable()) {
Chandler Carruth0010bca2010-06-29 00:23:11 +0000567 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redl2111c852010-06-28 15:09:07 +0000568 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth0010bca2010-06-29 00:23:11 +0000569 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000570 case Cl::CM_Function: return MLV_NotObjectType;
571 case Cl::CM_LValueCast:
Chandler Carruth0010bca2010-06-29 00:23:11 +0000572 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000573 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
574 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
575 case Cl::CM_ConstQualified: return MLV_ConstQualified;
576 case Cl::CM_ArrayType: return MLV_ArrayType;
577 case Cl::CM_IncompleteType: return MLV_IncompleteType;
578 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000579 llvm_unreachable("Unhandled modifiable type");
Sebastian Redl2111c852010-06-28 15:09:07 +0000580}