blob: f90d59f5052a76ffb89d72835c7cc3f42b13d20c [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,
John McCallc07a0c72011-02-17 10:25:35 +000032 const Expr *trueExpr,
33 const Expr *falseExpr);
Sebastian Redlf9463102010-06-28 15:09:07 +000034static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35 Cl::Kinds Kind, SourceLocation &Loc);
36
John McCall8d69a212010-11-15 23:31:06 +000037static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
38 const Expr *E,
39 ExprValueKind Kind) {
40 switch (Kind) {
41 case VK_RValue:
42 return Lang.CPlusPlus && E->getType()->isRecordType() ?
43 Cl::CL_ClassTemporary : Cl::CL_PRValue;
44 case VK_LValue:
45 return Cl::CL_LValue;
46 case VK_XValue:
47 return Cl::CL_XValue;
48 }
49 llvm_unreachable("Invalid value category of implicit cast.");
50 return Cl::CL_PRValue;
51}
52
Sebastian Redlf9463102010-06-28 15:09:07 +000053Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
54 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
55
56 Cl::Kinds kind = ClassifyInternal(Ctx, this);
57 // C99 6.3.2.1: An lvalue is an expression with an object type or an
58 // incomplete type other than void.
59 if (!Ctx.getLangOptions().CPlusPlus) {
60 // Thus, no functions.
61 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
62 kind = Cl::CL_Function;
63 // No void either, but qualified void is OK because it is "other than void".
64 else if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
65 kind = Cl::CL_Void;
66 }
67
John McCall4bc41ae2010-11-18 19:01:18 +000068 // Enable this assertion for testing.
69 switch (kind) {
70 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
71 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
72 case Cl::CL_Function:
73 case Cl::CL_Void:
74 case Cl::CL_DuplicateVectorComponents:
75 case Cl::CL_MemberFunction:
76 case Cl::CL_SubObjCPropertySetting:
77 case Cl::CL_ClassTemporary:
Fariborz Jahanian071caef2011-03-26 19:48:30 +000078 case Cl::CL_ObjCMessageRValue:
John McCall4bc41ae2010-11-18 19:01:18 +000079 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
80 }
John McCall4bc41ae2010-11-18 19:01:18 +000081
Sebastian Redlf9463102010-06-28 15:09:07 +000082 Cl::ModifiableType modifiable = Cl::CM_Untested;
83 if (Loc)
84 modifiable = IsModifiable(Ctx, this, kind, *Loc);
85 return Classification(kind, modifiable);
86}
87
88static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
89 // This function takes the first stab at classifying expressions.
90 const LangOptions &Lang = Ctx.getLangOptions();
91
92 switch (E->getStmtClass()) {
93 // First come the expressions that are always lvalues, unconditionally.
Douglas Gregor4e442502010-09-14 21:51:42 +000094 case Stmt::NoStmtClass:
John McCallbd066782011-02-09 08:16:59 +000095#define ABSTRACT_STMT(Kind)
Douglas Gregor4e442502010-09-14 21:51:42 +000096#define STMT(Kind, Base) case Expr::Kind##Class:
97#define EXPR(Kind, Base)
98#include "clang/AST/StmtNodes.inc"
99 llvm_unreachable("cannot classify a statement");
100 break;
Sebastian Redlf9463102010-06-28 15:09:07 +0000101 case Expr::ObjCIsaExprClass:
102 // C++ [expr.prim.general]p1: A string literal is an lvalue.
103 case Expr::StringLiteralClass:
104 // @encode is equivalent to its string
105 case Expr::ObjCEncodeExprClass:
106 // __func__ and friends are too.
107 case Expr::PredefinedExprClass:
108 // Property references are lvalues
109 case Expr::ObjCPropertyRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000110 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
111 case Expr::CXXTypeidExprClass:
112 // Unresolved lookups get classified as lvalues.
113 // FIXME: Is this wise? Should they get their own kind?
114 case Expr::UnresolvedLookupExprClass:
115 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000116 case Expr::CXXDependentScopeMemberExprClass:
117 case Expr::CXXUnresolvedConstructExprClass:
118 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000119 // ObjC instance variables are lvalues
120 // FIXME: ObjC++0x might have different rules
121 case Expr::ObjCIvarRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000122 return Cl::CL_LValue;
Douglas Gregor4e442502010-09-14 21:51:42 +0000123 // C99 6.5.2.5p5 says that compound literals are lvalues.
124 // In C++, they're class temporaries.
125 case Expr::CompoundLiteralExprClass:
126 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
127 : Cl::CL_LValue;
128
129 // Expressions that are prvalues.
130 case Expr::CXXBoolLiteralExprClass:
131 case Expr::CXXPseudoDestructorExprClass:
Peter Collingbournee190dee2011-03-11 19:24:49 +0000132 case Expr::UnaryExprOrTypeTraitExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000133 case Expr::CXXNewExprClass:
134 case Expr::CXXThisExprClass:
135 case Expr::CXXNullPtrLiteralExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000136 case Expr::ImaginaryLiteralClass:
137 case Expr::GNUNullExprClass:
138 case Expr::OffsetOfExprClass:
139 case Expr::CXXThrowExprClass:
140 case Expr::ShuffleVectorExprClass:
141 case Expr::IntegerLiteralClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000142 case Expr::CharacterLiteralClass:
143 case Expr::AddrLabelExprClass:
144 case Expr::CXXDeleteExprClass:
145 case Expr::ImplicitValueInitExprClass:
146 case Expr::BlockExprClass:
147 case Expr::FloatingLiteralClass:
148 case Expr::CXXNoexceptExprClass:
149 case Expr::CXXScalarValueInitExprClass:
150 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +0000151 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000152 case Expr::ObjCSelectorExprClass:
153 case Expr::ObjCProtocolExprClass:
154 case Expr::ObjCStringLiteralClass:
155 case Expr::ParenListExprClass:
156 case Expr::InitListExprClass:
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000157 case Expr::SizeOfPackExprClass:
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000158 case Expr::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000159 return Cl::CL_PRValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000160
161 // Next come the complicated cases.
162
163 // C++ [expr.sub]p1: The result is an lvalue of type "T".
164 // However, subscripting vector types is more like member access.
165 case Expr::ArraySubscriptExprClass:
166 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
167 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
168 return Cl::CL_LValue;
169
170 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
171 // function or variable and a prvalue otherwise.
172 case Expr::DeclRefExprClass:
173 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
174 // We deal with names referenced from blocks the same way.
175 case Expr::BlockDeclRefExprClass:
176 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
177
178 // Member access is complex.
179 case Expr::MemberExprClass:
180 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
181
182 case Expr::UnaryOperatorClass:
183 switch (cast<UnaryOperator>(E)->getOpcode()) {
184 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
185 // [...] the result is an lvalue referring to the object or function
186 // to which the expression points.
John McCalle3027922010-08-25 11:45:40 +0000187 case UO_Deref:
Sebastian Redlf9463102010-06-28 15:09:07 +0000188 return Cl::CL_LValue;
189
190 // GNU extensions, simply look through them.
John McCalle3027922010-08-25 11:45:40 +0000191 case UO_Extension:
Sebastian Redlf9463102010-06-28 15:09:07 +0000192 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
193
John McCall07bb1962010-11-16 10:08:07 +0000194 // Treat _Real and _Imag basically as if they were member
195 // expressions: l-value only if the operand is a true l-value.
196 case UO_Real:
197 case UO_Imag: {
198 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
199 Cl::Kinds K = ClassifyInternal(Ctx, Op);
200 if (K != Cl::CL_LValue) return K;
201
John McCallb7bd14f2010-12-02 01:19:52 +0000202 if (isa<ObjCPropertyRefExpr>(Op))
John McCall07bb1962010-11-16 10:08:07 +0000203 return Cl::CL_SubObjCPropertySetting;
204 return Cl::CL_LValue;
205 }
206
Sebastian Redlf9463102010-06-28 15:09:07 +0000207 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
208 // lvalue, [...]
209 // Not so in C.
John McCalle3027922010-08-25 11:45:40 +0000210 case UO_PreInc:
211 case UO_PreDec:
Sebastian Redlf9463102010-06-28 15:09:07 +0000212 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
213
214 default:
215 return Cl::CL_PRValue;
216 }
217
John McCall8d69a212010-11-15 23:31:06 +0000218 case Expr::OpaqueValueExprClass:
219 return ClassifyExprValueKind(Lang, E,
220 cast<OpaqueValueExpr>(E)->getValueKind());
221
Sebastian Redlf9463102010-06-28 15:09:07 +0000222 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
223 // only specifically record class temporaries.
224 case Expr::ImplicitCastExprClass:
John McCall8d69a212010-11-15 23:31:06 +0000225 return ClassifyExprValueKind(Lang, E,
226 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redlf9463102010-06-28 15:09:07 +0000227
228 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
229 // whether the expression is an lvalue.
230 case Expr::ParenExprClass:
231 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
232
233 case Expr::BinaryOperatorClass:
234 case Expr::CompoundAssignOperatorClass:
235 // C doesn't have any binary expressions that are lvalues.
236 if (Lang.CPlusPlus)
237 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
238 return Cl::CL_PRValue;
239
240 case Expr::CallExprClass:
241 case Expr::CXXOperatorCallExprClass:
242 case Expr::CXXMemberCallExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +0000243 case Expr::CUDAKernelCallExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000244 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
245
246 // __builtin_choose_expr is equivalent to the chosen expression.
247 case Expr::ChooseExprClass:
248 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
249
250 // Extended vector element access is an lvalue unless there are duplicates
251 // in the shuffle expression.
252 case Expr::ExtVectorElementExprClass:
253 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
254 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
255
256 // Simply look at the actual default argument.
257 case Expr::CXXDefaultArgExprClass:
258 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
259
260 // Same idea for temporary binding.
261 case Expr::CXXBindTemporaryExprClass:
262 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
263
John McCall5d413782010-12-06 08:20:24 +0000264 // And the cleanups guard.
265 case Expr::ExprWithCleanupsClass:
266 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redlf9463102010-06-28 15:09:07 +0000267
268 // Casts depend completely on the target type. All casts work the same.
269 case Expr::CStyleCastExprClass:
270 case Expr::CXXFunctionalCastExprClass:
271 case Expr::CXXStaticCastExprClass:
272 case Expr::CXXDynamicCastExprClass:
273 case Expr::CXXReinterpretCastExprClass:
274 case Expr::CXXConstCastExprClass:
275 // Only in C++ can casts be interesting at all.
276 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
277 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
278
John McCallc07a0c72011-02-17 10:25:35 +0000279 case Expr::BinaryConditionalOperatorClass: {
280 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
281 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
282 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
283 }
284
285 case Expr::ConditionalOperatorClass: {
Sebastian Redlf9463102010-06-28 15:09:07 +0000286 // Once again, only C++ is interesting.
287 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
John McCallc07a0c72011-02-17 10:25:35 +0000288 const ConditionalOperator *co = cast<ConditionalOperator>(E);
289 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
290 }
Sebastian Redlf9463102010-06-28 15:09:07 +0000291
292 // ObjC message sends are effectively function calls, if the target function
293 // is known.
294 case Expr::ObjCMessageExprClass:
295 if (const ObjCMethodDecl *Method =
296 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000297 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
298 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
Sebastian Redlf9463102010-06-28 15:09:07 +0000299 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000300 return Cl::CL_PRValue;
301
Sebastian Redlf9463102010-06-28 15:09:07 +0000302 // Some C++ expressions are always class temporaries.
303 case Expr::CXXConstructExprClass:
304 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000305 return Cl::CL_ClassTemporary;
306
Douglas Gregor4e442502010-09-14 21:51:42 +0000307 case Expr::VAArgExprClass:
308 return ClassifyUnnamed(Ctx, E->getType());
309
310 case Expr::DesignatedInitExprClass:
311 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
312
313 case Expr::StmtExprClass: {
314 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
315 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregore572b062010-09-15 01:37:48 +0000316 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redlf9463102010-06-28 15:09:07 +0000317 return Cl::CL_PRValue;
318 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000319
320 case Expr::CXXUuidofExprClass:
Francois Pichet4f64c5a2010-12-17 02:00:06 +0000321 return Cl::CL_LValue;
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000322
323 case Expr::PackExpansionExprClass:
324 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Douglas Gregor4e442502010-09-14 21:51:42 +0000325 }
326
327 llvm_unreachable("unhandled expression kind in classification");
328 return Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000329}
330
331/// ClassifyDecl - Return the classification of an expression referencing the
332/// given declaration.
333static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
334 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
335 // function, variable, or data member and a prvalue otherwise.
336 // In C, functions are not lvalues.
337 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
338 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
339 // special-case this.
John McCall8d08b9b2010-08-27 09:08:28 +0000340
341 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
342 return Cl::CL_MemberFunction;
343
Sebastian Redlf9463102010-06-28 15:09:07 +0000344 bool islvalue;
345 if (const NonTypeTemplateParmDecl *NTTParm =
346 dyn_cast<NonTypeTemplateParmDecl>(D))
347 islvalue = NTTParm->getType()->isReferenceType();
348 else
349 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet783dd6e2010-11-21 06:08:52 +0000350 isa<IndirectFieldDecl>(D) ||
Sebastian Redlf9463102010-06-28 15:09:07 +0000351 (Ctx.getLangOptions().CPlusPlus &&
352 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
353
354 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
355}
356
357/// ClassifyUnnamed - Return the classification of an expression yielding an
358/// unnamed value of the given type. This applies in particular to function
359/// calls and casts.
360static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
361 // In C, function calls are always rvalues.
362 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
363
364 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
365 // lvalue reference type or an rvalue reference to function type, an xvalue
366 // if the result type is an rvalue refernence to object type, and a prvalue
367 // otherwise.
368 if (T->isLValueReferenceType())
369 return Cl::CL_LValue;
370 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
371 if (!RV) // Could still be a class temporary, though.
372 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
373
374 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
375}
376
377static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
378 // Handle C first, it's easier.
379 if (!Ctx.getLangOptions().CPlusPlus) {
380 // C99 6.5.2.3p3
381 // For dot access, the expression is an lvalue if the first part is. For
382 // arrow access, it always is an lvalue.
383 if (E->isArrow())
384 return Cl::CL_LValue;
385 // ObjC property accesses are not lvalues, but get special treatment.
John McCall07bb1962010-11-16 10:08:07 +0000386 Expr *Base = E->getBase()->IgnoreParens();
John McCallb7bd14f2010-12-02 01:19:52 +0000387 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redlf9463102010-06-28 15:09:07 +0000388 return Cl::CL_SubObjCPropertySetting;
389 return ClassifyInternal(Ctx, Base);
390 }
391
392 NamedDecl *Member = E->getMemberDecl();
393 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
394 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
395 // E1.E2 is an lvalue.
396 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
397 if (Value->getType()->isReferenceType())
398 return Cl::CL_LValue;
399
400 // Otherwise, one of the following rules applies.
401 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
402 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
403 return Cl::CL_LValue;
404
405 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
406 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
407 // otherwise, it is a prvalue.
408 if (isa<FieldDecl>(Member)) {
409 // *E1 is an lvalue
410 if (E->isArrow())
411 return Cl::CL_LValue;
John McCall4bc41ae2010-11-18 19:01:18 +0000412 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCallb7bd14f2010-12-02 01:19:52 +0000413 if (isa<ObjCPropertyRefExpr>(Base))
John McCall4bc41ae2010-11-18 19:01:18 +0000414 return Cl::CL_SubObjCPropertySetting;
Sebastian Redlf9463102010-06-28 15:09:07 +0000415 return ClassifyInternal(Ctx, E->getBase());
416 }
417
418 // -- If E2 is a [...] member function, [...]
419 // -- If it refers to a static member function [...], then E1.E2 is an
420 // lvalue; [...]
421 // -- Otherwise [...] E1.E2 is a prvalue.
422 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
423 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
424
425 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
426 // So is everything else we haven't handled yet.
427 return Cl::CL_PRValue;
428}
429
430static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
431 assert(Ctx.getLangOptions().CPlusPlus &&
432 "This is only relevant for C++.");
433 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCall34376a62010-12-04 03:47:34 +0000434 // Except we override this for writes to ObjC properties.
Sebastian Redlf9463102010-06-28 15:09:07 +0000435 if (E->isAssignmentOp())
John McCall34376a62010-12-04 03:47:34 +0000436 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
437 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redlf9463102010-06-28 15:09:07 +0000438
439 // C++ [expr.comma]p1: the result is of the same value category as its right
440 // operand, [...].
John McCalle3027922010-08-25 11:45:40 +0000441 if (E->getOpcode() == BO_Comma)
Sebastian Redlf9463102010-06-28 15:09:07 +0000442 return ClassifyInternal(Ctx, E->getRHS());
443
444 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
445 // is a pointer to a data member is of the same value category as its first
446 // operand.
John McCalle3027922010-08-25 11:45:40 +0000447 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redlf9463102010-06-28 15:09:07 +0000448 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
449 ClassifyInternal(Ctx, E->getLHS());
450
451 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
452 // second operand is a pointer to data member and a prvalue otherwise.
John McCalle3027922010-08-25 11:45:40 +0000453 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redlf9463102010-06-28 15:09:07 +0000454 return E->getType()->isFunctionType() ?
455 Cl::CL_MemberFunction : Cl::CL_LValue;
456
457 // All other binary operations are prvalues.
458 return Cl::CL_PRValue;
459}
460
John McCallc07a0c72011-02-17 10:25:35 +0000461static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
462 const Expr *False) {
Sebastian Redlf9463102010-06-28 15:09:07 +0000463 assert(Ctx.getLangOptions().CPlusPlus &&
464 "This is only relevant for C++.");
465
Sebastian Redlf9463102010-06-28 15:09:07 +0000466 // C++ [expr.cond]p2
467 // If either the second or the third operand has type (cv) void, [...]
468 // the result [...] is a prvalue.
469 if (True->getType()->isVoidType() || False->getType()->isVoidType())
470 return Cl::CL_PRValue;
471
472 // Note that at this point, we have already performed all conversions
473 // according to [expr.cond]p3.
474 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
475 // same value category [...], the result is of that [...] value category.
476 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
477 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
478 RCl = ClassifyInternal(Ctx, False);
479 return LCl == RCl ? LCl : Cl::CL_PRValue;
480}
481
482static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
483 Cl::Kinds Kind, SourceLocation &Loc) {
484 // As a general rule, we only care about lvalues. But there are some rvalues
485 // for which we want to generate special results.
486 if (Kind == Cl::CL_PRValue) {
487 // For the sake of better diagnostics, we want to specifically recognize
488 // use of the GCC cast-as-lvalue extension.
John McCall34376a62010-12-04 03:47:34 +0000489 if (const ExplicitCastExpr *CE =
490 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
491 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
492 Loc = CE->getExprLoc();
Sebastian Redlf9463102010-06-28 15:09:07 +0000493 return Cl::CM_LValueCast;
494 }
495 }
496 }
497 if (Kind != Cl::CL_LValue)
498 return Cl::CM_RValue;
499
500 // This is the lvalue case.
501 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
502 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
503 return Cl::CM_Function;
504
505 // You cannot assign to a variable outside a block from within the block if
506 // it is not marked __block, e.g.
507 // void takeclosure(void (^C)(void));
508 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
509 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
510 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
511 return Cl::CM_NotBlockQualified;
512 }
513
514 // Assignment to a property in ObjC is an implicit setter access. But a
515 // setter might not exist.
John McCallb7bd14f2010-12-02 01:19:52 +0000516 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
517 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redlf9463102010-06-28 15:09:07 +0000518 return Cl::CM_NoSetterProperty;
519 }
520
521 CanQualType CT = Ctx.getCanonicalType(E->getType());
522 // Const stuff is obviously not modifiable.
523 if (CT.isConstQualified())
524 return Cl::CM_ConstQualified;
525 // Arrays are not modifiable, only their elements are.
526 if (CT->isArrayType())
527 return Cl::CM_ArrayType;
528 // Incomplete types are not modifiable.
529 if (CT->isIncompleteType())
530 return Cl::CM_IncompleteType;
531
532 // Records with any const fields (recursively) are not modifiable.
533 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall622114c2010-12-06 05:26:58 +0000534 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahaniane89d03f2010-09-09 23:01:10 +0000535 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redlf9463102010-06-28 15:09:07 +0000536 "C++ struct assignment should be resolved by the "
537 "copy assignment operator.");
538 if (R->hasConstFields())
539 return Cl::CM_ConstQualified;
540 }
541
542 return Cl::CM_Modifiable;
543}
544
John McCall086a4642010-11-24 05:12:34 +0000545Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redlf9463102010-06-28 15:09:07 +0000546 Classification VC = Classify(Ctx);
547 switch (VC.getKind()) {
548 case Cl::CL_LValue: return LV_Valid;
549 case Cl::CL_XValue: return LV_InvalidExpression;
550 case Cl::CL_Function: return LV_NotObjectType;
551 case Cl::CL_Void: return LV_IncompleteVoidType;
552 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
553 case Cl::CL_MemberFunction: return LV_MemberFunction;
554 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
555 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000556 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
Sebastian Redlf9463102010-06-28 15:09:07 +0000557 case Cl::CL_PRValue: return LV_InvalidExpression;
558 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000559 llvm_unreachable("Unhandled kind");
Sebastian Redlf9463102010-06-28 15:09:07 +0000560}
561
562Expr::isModifiableLvalueResult
563Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
564 SourceLocation dummy;
565 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
566 switch (VC.getKind()) {
567 case Cl::CL_LValue: break;
568 case Cl::CL_XValue: return MLV_InvalidExpression;
569 case Cl::CL_Function: return MLV_NotObjectType;
570 case Cl::CL_Void: return MLV_IncompleteVoidType;
571 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
572 case Cl::CL_MemberFunction: return MLV_MemberFunction;
573 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
574 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000575 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
Sebastian Redlf9463102010-06-28 15:09:07 +0000576 case Cl::CL_PRValue:
577 return VC.getModifiable() == Cl::CM_LValueCast ?
578 MLV_LValueCast : MLV_InvalidExpression;
579 }
580 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
581 switch (VC.getModifiable()) {
Chandler Carruth8337ba62010-06-29 00:23:11 +0000582 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redlf9463102010-06-28 15:09:07 +0000583 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth8337ba62010-06-29 00:23:11 +0000584 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000585 case Cl::CM_Function: return MLV_NotObjectType;
586 case Cl::CM_LValueCast:
Chandler Carruth8337ba62010-06-29 00:23:11 +0000587 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000588 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
589 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
590 case Cl::CM_ConstQualified: return MLV_ConstQualified;
591 case Cl::CM_ArrayType: return MLV_ArrayType;
592 case Cl::CM_IncompleteType: return MLV_IncompleteType;
593 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000594 llvm_unreachable("Unhandled modifiable type");
Sebastian Redlf9463102010-06-28 15:09:07 +0000595}