blob: 3788dc743fd5f8878704fd99a0bbcab76f52af62 [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:
Sebastian Redl2111c852010-06-28 15:09:07 +0000107 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
108 case Expr::CXXTypeidExprClass:
109 // Unresolved lookups get classified as lvalues.
110 // FIXME: Is this wise? Should they get their own kind?
111 case Expr::UnresolvedLookupExprClass:
112 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000113 case Expr::CXXDependentScopeMemberExprClass:
114 case Expr::CXXUnresolvedConstructExprClass:
115 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000116 // ObjC instance variables are lvalues
117 // FIXME: ObjC++0x might have different rules
118 case Expr::ObjCIvarRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000119 return Cl::CL_LValue;
Douglas Gregor4178b572010-09-14 21:51:42 +0000120 // C99 6.5.2.5p5 says that compound literals are lvalues.
121 // In C++, they're class temporaries.
122 case Expr::CompoundLiteralExprClass:
123 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
124 : Cl::CL_LValue;
125
126 // Expressions that are prvalues.
127 case Expr::CXXBoolLiteralExprClass:
128 case Expr::CXXPseudoDestructorExprClass:
129 case Expr::SizeOfAlignOfExprClass:
130 case Expr::CXXNewExprClass:
131 case Expr::CXXThisExprClass:
132 case Expr::CXXNullPtrLiteralExprClass:
133 case Expr::TypesCompatibleExprClass:
134 case Expr::ImaginaryLiteralClass:
135 case Expr::GNUNullExprClass:
136 case Expr::OffsetOfExprClass:
137 case Expr::CXXThrowExprClass:
138 case Expr::ShuffleVectorExprClass:
139 case Expr::IntegerLiteralClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000140 case Expr::CharacterLiteralClass:
141 case Expr::AddrLabelExprClass:
142 case Expr::CXXDeleteExprClass:
143 case Expr::ImplicitValueInitExprClass:
144 case Expr::BlockExprClass:
145 case Expr::FloatingLiteralClass:
146 case Expr::CXXNoexceptExprClass:
147 case Expr::CXXScalarValueInitExprClass:
148 case Expr::UnaryTypeTraitExprClass:
149 case Expr::ObjCSelectorExprClass:
150 case Expr::ObjCProtocolExprClass:
151 case Expr::ObjCStringLiteralClass:
152 case Expr::ParenListExprClass:
153 case Expr::InitListExprClass:
154 return Cl::CL_PRValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000155
156 // Next come the complicated cases.
157
158 // C++ [expr.sub]p1: The result is an lvalue of type "T".
159 // However, subscripting vector types is more like member access.
160 case Expr::ArraySubscriptExprClass:
161 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
162 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
163 return Cl::CL_LValue;
164
165 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
166 // function or variable and a prvalue otherwise.
167 case Expr::DeclRefExprClass:
168 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
169 // We deal with names referenced from blocks the same way.
170 case Expr::BlockDeclRefExprClass:
171 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
172
173 // Member access is complex.
174 case Expr::MemberExprClass:
175 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
176
177 case Expr::UnaryOperatorClass:
178 switch (cast<UnaryOperator>(E)->getOpcode()) {
179 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
180 // [...] the result is an lvalue referring to the object or function
181 // to which the expression points.
John McCall2de56d12010-08-25 11:45:40 +0000182 case UO_Deref:
Sebastian Redl2111c852010-06-28 15:09:07 +0000183 return Cl::CL_LValue;
184
185 // GNU extensions, simply look through them.
John McCall2de56d12010-08-25 11:45:40 +0000186 case UO_Extension:
Sebastian Redl2111c852010-06-28 15:09:07 +0000187 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
188
John McCallb418d742010-11-16 10:08:07 +0000189 // Treat _Real and _Imag basically as if they were member
190 // expressions: l-value only if the operand is a true l-value.
191 case UO_Real:
192 case UO_Imag: {
193 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
194 Cl::Kinds K = ClassifyInternal(Ctx, Op);
195 if (K != Cl::CL_LValue) return K;
196
John McCall12f78a62010-12-02 01:19:52 +0000197 if (isa<ObjCPropertyRefExpr>(Op))
John McCallb418d742010-11-16 10:08:07 +0000198 return Cl::CL_SubObjCPropertySetting;
199 return Cl::CL_LValue;
200 }
201
Sebastian Redl2111c852010-06-28 15:09:07 +0000202 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
203 // lvalue, [...]
204 // Not so in C.
John McCall2de56d12010-08-25 11:45:40 +0000205 case UO_PreInc:
206 case UO_PreDec:
Sebastian Redl2111c852010-06-28 15:09:07 +0000207 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
208
209 default:
210 return Cl::CL_PRValue;
211 }
212
John McCall7cd7d1a2010-11-15 23:31:06 +0000213 case Expr::OpaqueValueExprClass:
214 return ClassifyExprValueKind(Lang, E,
215 cast<OpaqueValueExpr>(E)->getValueKind());
216
Sebastian Redl2111c852010-06-28 15:09:07 +0000217 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
218 // only specifically record class temporaries.
219 case Expr::ImplicitCastExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000220 return ClassifyExprValueKind(Lang, E,
221 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redl2111c852010-06-28 15:09:07 +0000222
223 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
224 // whether the expression is an lvalue.
225 case Expr::ParenExprClass:
226 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
227
228 case Expr::BinaryOperatorClass:
229 case Expr::CompoundAssignOperatorClass:
230 // C doesn't have any binary expressions that are lvalues.
231 if (Lang.CPlusPlus)
232 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
233 return Cl::CL_PRValue;
234
235 case Expr::CallExprClass:
236 case Expr::CXXOperatorCallExprClass:
237 case Expr::CXXMemberCallExprClass:
238 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
239
240 // __builtin_choose_expr is equivalent to the chosen expression.
241 case Expr::ChooseExprClass:
242 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
243
244 // Extended vector element access is an lvalue unless there are duplicates
245 // in the shuffle expression.
246 case Expr::ExtVectorElementExprClass:
247 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
248 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
249
250 // Simply look at the actual default argument.
251 case Expr::CXXDefaultArgExprClass:
252 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
253
254 // Same idea for temporary binding.
255 case Expr::CXXBindTemporaryExprClass:
256 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
257
John McCall4765fa02010-12-06 08:20:24 +0000258 // And the cleanups guard.
259 case Expr::ExprWithCleanupsClass:
260 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redl2111c852010-06-28 15:09:07 +0000261
262 // Casts depend completely on the target type. All casts work the same.
263 case Expr::CStyleCastExprClass:
264 case Expr::CXXFunctionalCastExprClass:
265 case Expr::CXXStaticCastExprClass:
266 case Expr::CXXDynamicCastExprClass:
267 case Expr::CXXReinterpretCastExprClass:
268 case Expr::CXXConstCastExprClass:
269 // Only in C++ can casts be interesting at all.
270 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
271 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
272
273 case Expr::ConditionalOperatorClass:
274 // Once again, only C++ is interesting.
275 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
276 return ClassifyConditional(Ctx, cast<ConditionalOperator>(E));
277
278 // ObjC message sends are effectively function calls, if the target function
279 // is known.
280 case Expr::ObjCMessageExprClass:
281 if (const ObjCMethodDecl *Method =
282 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
283 return ClassifyUnnamed(Ctx, Method->getResultType());
284 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000285 return Cl::CL_PRValue;
286
Sebastian Redl2111c852010-06-28 15:09:07 +0000287 // Some C++ expressions are always class temporaries.
288 case Expr::CXXConstructExprClass:
289 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000290 return Cl::CL_ClassTemporary;
291
Douglas Gregor4178b572010-09-14 21:51:42 +0000292 case Expr::VAArgExprClass:
293 return ClassifyUnnamed(Ctx, E->getType());
294
295 case Expr::DesignatedInitExprClass:
296 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
297
298 case Expr::StmtExprClass: {
299 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
300 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregor226cbfc2010-09-15 01:37:48 +0000301 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redl2111c852010-06-28 15:09:07 +0000302 return Cl::CL_PRValue;
303 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000304
305 case Expr::CXXUuidofExprClass:
John McCallf89e55a2010-11-18 06:31:45 +0000306 return Cl::CL_PRValue;
Douglas Gregor4178b572010-09-14 21:51:42 +0000307 }
308
309 llvm_unreachable("unhandled expression kind in classification");
310 return Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000311}
312
313/// ClassifyDecl - Return the classification of an expression referencing the
314/// given declaration.
315static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
316 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
317 // function, variable, or data member and a prvalue otherwise.
318 // In C, functions are not lvalues.
319 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
320 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
321 // special-case this.
John McCall9c72c602010-08-27 09:08:28 +0000322
323 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
324 return Cl::CL_MemberFunction;
325
Sebastian Redl2111c852010-06-28 15:09:07 +0000326 bool islvalue;
327 if (const NonTypeTemplateParmDecl *NTTParm =
328 dyn_cast<NonTypeTemplateParmDecl>(D))
329 islvalue = NTTParm->getType()->isReferenceType();
330 else
331 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet87c2e122010-11-21 06:08:52 +0000332 isa<IndirectFieldDecl>(D) ||
Sebastian Redl2111c852010-06-28 15:09:07 +0000333 (Ctx.getLangOptions().CPlusPlus &&
334 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
335
336 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
337}
338
339/// ClassifyUnnamed - Return the classification of an expression yielding an
340/// unnamed value of the given type. This applies in particular to function
341/// calls and casts.
342static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
343 // In C, function calls are always rvalues.
344 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
345
346 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
347 // lvalue reference type or an rvalue reference to function type, an xvalue
348 // if the result type is an rvalue refernence to object type, and a prvalue
349 // otherwise.
350 if (T->isLValueReferenceType())
351 return Cl::CL_LValue;
352 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
353 if (!RV) // Could still be a class temporary, though.
354 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
355
356 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
357}
358
359static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
360 // Handle C first, it's easier.
361 if (!Ctx.getLangOptions().CPlusPlus) {
362 // C99 6.5.2.3p3
363 // For dot access, the expression is an lvalue if the first part is. For
364 // arrow access, it always is an lvalue.
365 if (E->isArrow())
366 return Cl::CL_LValue;
367 // ObjC property accesses are not lvalues, but get special treatment.
John McCallb418d742010-11-16 10:08:07 +0000368 Expr *Base = E->getBase()->IgnoreParens();
John McCall12f78a62010-12-02 01:19:52 +0000369 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redl2111c852010-06-28 15:09:07 +0000370 return Cl::CL_SubObjCPropertySetting;
371 return ClassifyInternal(Ctx, Base);
372 }
373
374 NamedDecl *Member = E->getMemberDecl();
375 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
376 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
377 // E1.E2 is an lvalue.
378 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
379 if (Value->getType()->isReferenceType())
380 return Cl::CL_LValue;
381
382 // Otherwise, one of the following rules applies.
383 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
384 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
385 return Cl::CL_LValue;
386
387 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
388 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
389 // otherwise, it is a prvalue.
390 if (isa<FieldDecl>(Member)) {
391 // *E1 is an lvalue
392 if (E->isArrow())
393 return Cl::CL_LValue;
John McCall09431682010-11-18 19:01:18 +0000394 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCall12f78a62010-12-02 01:19:52 +0000395 if (isa<ObjCPropertyRefExpr>(Base))
John McCall09431682010-11-18 19:01:18 +0000396 return Cl::CL_SubObjCPropertySetting;
Sebastian Redl2111c852010-06-28 15:09:07 +0000397 return ClassifyInternal(Ctx, E->getBase());
398 }
399
400 // -- If E2 is a [...] member function, [...]
401 // -- If it refers to a static member function [...], then E1.E2 is an
402 // lvalue; [...]
403 // -- Otherwise [...] E1.E2 is a prvalue.
404 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
405 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
406
407 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
408 // So is everything else we haven't handled yet.
409 return Cl::CL_PRValue;
410}
411
412static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
413 assert(Ctx.getLangOptions().CPlusPlus &&
414 "This is only relevant for C++.");
415 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCallf6a16482010-12-04 03:47:34 +0000416 // Except we override this for writes to ObjC properties.
Sebastian Redl2111c852010-06-28 15:09:07 +0000417 if (E->isAssignmentOp())
John McCallf6a16482010-12-04 03:47:34 +0000418 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
419 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redl2111c852010-06-28 15:09:07 +0000420
421 // C++ [expr.comma]p1: the result is of the same value category as its right
422 // operand, [...].
John McCall2de56d12010-08-25 11:45:40 +0000423 if (E->getOpcode() == BO_Comma)
Sebastian Redl2111c852010-06-28 15:09:07 +0000424 return ClassifyInternal(Ctx, E->getRHS());
425
426 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
427 // is a pointer to a data member is of the same value category as its first
428 // operand.
John McCall2de56d12010-08-25 11:45:40 +0000429 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redl2111c852010-06-28 15:09:07 +0000430 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
431 ClassifyInternal(Ctx, E->getLHS());
432
433 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
434 // second operand is a pointer to data member and a prvalue otherwise.
John McCall2de56d12010-08-25 11:45:40 +0000435 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redl2111c852010-06-28 15:09:07 +0000436 return E->getType()->isFunctionType() ?
437 Cl::CL_MemberFunction : Cl::CL_LValue;
438
439 // All other binary operations are prvalues.
440 return Cl::CL_PRValue;
441}
442
443static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
444 const ConditionalOperator *E) {
445 assert(Ctx.getLangOptions().CPlusPlus &&
446 "This is only relevant for C++.");
447
448 Expr *True = E->getTrueExpr();
449 Expr *False = E->getFalseExpr();
450 // C++ [expr.cond]p2
451 // If either the second or the third operand has type (cv) void, [...]
452 // the result [...] is a prvalue.
453 if (True->getType()->isVoidType() || False->getType()->isVoidType())
454 return Cl::CL_PRValue;
455
456 // Note that at this point, we have already performed all conversions
457 // according to [expr.cond]p3.
458 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
459 // same value category [...], the result is of that [...] value category.
460 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
461 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
462 RCl = ClassifyInternal(Ctx, False);
463 return LCl == RCl ? LCl : Cl::CL_PRValue;
464}
465
466static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
467 Cl::Kinds Kind, SourceLocation &Loc) {
468 // As a general rule, we only care about lvalues. But there are some rvalues
469 // for which we want to generate special results.
470 if (Kind == Cl::CL_PRValue) {
471 // For the sake of better diagnostics, we want to specifically recognize
472 // use of the GCC cast-as-lvalue extension.
John McCallf6a16482010-12-04 03:47:34 +0000473 if (const ExplicitCastExpr *CE =
474 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
475 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
476 Loc = CE->getExprLoc();
Sebastian Redl2111c852010-06-28 15:09:07 +0000477 return Cl::CM_LValueCast;
478 }
479 }
480 }
481 if (Kind != Cl::CL_LValue)
482 return Cl::CM_RValue;
483
484 // This is the lvalue case.
485 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
486 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
487 return Cl::CM_Function;
488
489 // You cannot assign to a variable outside a block from within the block if
490 // it is not marked __block, e.g.
491 // void takeclosure(void (^C)(void));
492 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
493 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
494 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
495 return Cl::CM_NotBlockQualified;
496 }
497
498 // Assignment to a property in ObjC is an implicit setter access. But a
499 // setter might not exist.
John McCall12f78a62010-12-02 01:19:52 +0000500 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
501 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redl2111c852010-06-28 15:09:07 +0000502 return Cl::CM_NoSetterProperty;
503 }
504
505 CanQualType CT = Ctx.getCanonicalType(E->getType());
506 // Const stuff is obviously not modifiable.
507 if (CT.isConstQualified())
508 return Cl::CM_ConstQualified;
509 // Arrays are not modifiable, only their elements are.
510 if (CT->isArrayType())
511 return Cl::CM_ArrayType;
512 // Incomplete types are not modifiable.
513 if (CT->isIncompleteType())
514 return Cl::CM_IncompleteType;
515
516 // Records with any const fields (recursively) are not modifiable.
517 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall01b2e4e2010-12-06 05:26:58 +0000518 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahanian4088ec02010-09-09 23:01:10 +0000519 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redl2111c852010-06-28 15:09:07 +0000520 "C++ struct assignment should be resolved by the "
521 "copy assignment operator.");
522 if (R->hasConstFields())
523 return Cl::CM_ConstQualified;
524 }
525
526 return Cl::CM_Modifiable;
527}
528
John McCall7eb0a9e2010-11-24 05:12:34 +0000529Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redl2111c852010-06-28 15:09:07 +0000530 Classification VC = Classify(Ctx);
531 switch (VC.getKind()) {
532 case Cl::CL_LValue: return LV_Valid;
533 case Cl::CL_XValue: return LV_InvalidExpression;
534 case Cl::CL_Function: return LV_NotObjectType;
535 case Cl::CL_Void: return LV_IncompleteVoidType;
536 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
537 case Cl::CL_MemberFunction: return LV_MemberFunction;
538 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
539 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
540 case Cl::CL_PRValue: return LV_InvalidExpression;
541 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000542 llvm_unreachable("Unhandled kind");
Sebastian Redl2111c852010-06-28 15:09:07 +0000543}
544
545Expr::isModifiableLvalueResult
546Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
547 SourceLocation dummy;
548 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
549 switch (VC.getKind()) {
550 case Cl::CL_LValue: break;
551 case Cl::CL_XValue: return MLV_InvalidExpression;
552 case Cl::CL_Function: return MLV_NotObjectType;
553 case Cl::CL_Void: return MLV_IncompleteVoidType;
554 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
555 case Cl::CL_MemberFunction: return MLV_MemberFunction;
556 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
557 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
558 case Cl::CL_PRValue:
559 return VC.getModifiable() == Cl::CM_LValueCast ?
560 MLV_LValueCast : MLV_InvalidExpression;
561 }
562 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
563 switch (VC.getModifiable()) {
Chandler Carruth0010bca2010-06-29 00:23:11 +0000564 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redl2111c852010-06-28 15:09:07 +0000565 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth0010bca2010-06-29 00:23:11 +0000566 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000567 case Cl::CM_Function: return MLV_NotObjectType;
568 case Cl::CM_LValueCast:
Chandler Carruth0010bca2010-06-29 00:23:11 +0000569 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000570 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
571 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
572 case Cl::CM_ConstQualified: return MLV_ConstQualified;
573 case Cl::CM_ArrayType: return MLV_ArrayType;
574 case Cl::CM_IncompleteType: return MLV_IncompleteType;
575 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000576 llvm_unreachable("Unhandled modifiable type");
Sebastian Redl2111c852010-06-28 15:09:07 +0000577}