blob: 00160a007379bd6f3df5cc362b57c19f30f249dc [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,
John McCall56ca35d2011-02-17 10:25:35 +000032 const Expr *trueExpr,
33 const Expr *falseExpr);
Sebastian Redl2111c852010-06-28 15:09:07 +000034static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35 Cl::Kinds Kind, SourceLocation &Loc);
36
John McCall7cd7d1a2010-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.");
John McCall7cd7d1a2010-11-15 23:31:06 +000050}
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".
Peter Collingbourne1c860d52011-04-19 18:51:51 +000063 // Void "lvalues" are classified as addressable void values, which are void
64 // expressions whose address can be taken.
65 else if (TR->isVoidType() && !TR.hasQualifiers())
66 kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void);
Sebastian Redl2111c852010-06-28 15:09:07 +000067 }
68
John McCall09431682010-11-18 19:01:18 +000069 // Enable this assertion for testing.
70 switch (kind) {
71 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
72 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
73 case Cl::CL_Function:
74 case Cl::CL_Void:
Peter Collingbourne1c860d52011-04-19 18:51:51 +000075 case Cl::CL_AddressableVoid:
John McCall09431682010-11-18 19:01:18 +000076 case Cl::CL_DuplicateVectorComponents:
77 case Cl::CL_MemberFunction:
78 case Cl::CL_SubObjCPropertySetting:
79 case Cl::CL_ClassTemporary:
Fariborz Jahanian077f4902011-03-26 19:48:30 +000080 case Cl::CL_ObjCMessageRValue:
John McCall09431682010-11-18 19:01:18 +000081 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
82 }
John McCall09431682010-11-18 19:01:18 +000083
Sebastian Redl2111c852010-06-28 15:09:07 +000084 Cl::ModifiableType modifiable = Cl::CM_Untested;
85 if (Loc)
86 modifiable = IsModifiable(Ctx, this, kind, *Loc);
87 return Classification(kind, modifiable);
88}
89
90static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
91 // This function takes the first stab at classifying expressions.
92 const LangOptions &Lang = Ctx.getLangOptions();
93
94 switch (E->getStmtClass()) {
Douglas Gregor4178b572010-09-14 21:51:42 +000095 case Stmt::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +000096#define ABSTRACT_STMT(Kind)
Douglas Gregor4178b572010-09-14 21:51:42 +000097#define STMT(Kind, Base) case Expr::Kind##Class:
98#define EXPR(Kind, Base)
99#include "clang/AST/StmtNodes.inc"
100 llvm_unreachable("cannot classify a statement");
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000101
102 // First come the expressions that are always lvalues, unconditionally.
Sebastian Redl2111c852010-06-28 15:09:07 +0000103 case Expr::ObjCIsaExprClass:
104 // C++ [expr.prim.general]p1: A string literal is an lvalue.
105 case Expr::StringLiteralClass:
106 // @encode is equivalent to its string
107 case Expr::ObjCEncodeExprClass:
108 // __func__ and friends are too.
109 case Expr::PredefinedExprClass:
110 // Property references are lvalues
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000111 case Expr::ObjCSubscriptRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000112 case Expr::ObjCPropertyRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000113 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
114 case Expr::CXXTypeidExprClass:
115 // Unresolved lookups get classified as lvalues.
116 // FIXME: Is this wise? Should they get their own kind?
117 case Expr::UnresolvedLookupExprClass:
118 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000119 case Expr::CXXDependentScopeMemberExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000120 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000121 // ObjC instance variables are lvalues
122 // FIXME: ObjC++0x might have different rules
123 case Expr::ObjCIvarRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000124 return Cl::CL_LValue;
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000125
Douglas Gregor4178b572010-09-14 21:51:42 +0000126 // C99 6.5.2.5p5 says that compound literals are lvalues.
127 // In C++, they're class temporaries.
128 case Expr::CompoundLiteralExprClass:
129 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
130 : Cl::CL_LValue;
131
132 // Expressions that are prvalues.
133 case Expr::CXXBoolLiteralExprClass:
134 case Expr::CXXPseudoDestructorExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000135 case Expr::UnaryExprOrTypeTraitExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000136 case Expr::CXXNewExprClass:
137 case Expr::CXXThisExprClass:
138 case Expr::CXXNullPtrLiteralExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000139 case Expr::ImaginaryLiteralClass:
140 case Expr::GNUNullExprClass:
141 case Expr::OffsetOfExprClass:
142 case Expr::CXXThrowExprClass:
143 case Expr::ShuffleVectorExprClass:
144 case Expr::IntegerLiteralClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000145 case Expr::CharacterLiteralClass:
146 case Expr::AddrLabelExprClass:
147 case Expr::CXXDeleteExprClass:
148 case Expr::ImplicitValueInitExprClass:
149 case Expr::BlockExprClass:
150 case Expr::FloatingLiteralClass:
151 case Expr::CXXNoexceptExprClass:
152 case Expr::CXXScalarValueInitExprClass:
153 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000154 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000155 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000156 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000157 case Expr::ExpressionTraitExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000158 case Expr::ObjCSelectorExprClass:
159 case Expr::ObjCProtocolExprClass:
160 case Expr::ObjCStringLiteralClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000161 case Expr::ObjCNumericLiteralClass:
162 case Expr::ObjCArrayLiteralClass:
163 case Expr::ObjCDictionaryLiteralClass:
164 case Expr::ObjCBoolLiteralExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000165 case Expr::ParenListExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000166 case Expr::SizeOfPackExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000167 case Expr::SubstNonTypeTemplateParmPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000168 case Expr::AsTypeExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000169 case Expr::ObjCIndirectCopyRestoreExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000170 case Expr::AtomicExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000171 return Cl::CL_PRValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000172
173 // Next come the complicated cases.
John McCall91a57552011-07-15 05:09:51 +0000174 case Expr::SubstNonTypeTemplateParmExprClass:
175 return ClassifyInternal(Ctx,
176 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
Sebastian Redl2111c852010-06-28 15:09:07 +0000177
178 // C++ [expr.sub]p1: The result is an lvalue of type "T".
179 // However, subscripting vector types is more like member access.
180 case Expr::ArraySubscriptExprClass:
181 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
182 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
183 return Cl::CL_LValue;
184
185 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
186 // function or variable and a prvalue otherwise.
187 case Expr::DeclRefExprClass:
John McCall755d8492011-04-12 00:42:48 +0000188 if (E->getType() == Ctx.UnknownAnyTy)
189 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
190 ? Cl::CL_PRValue : Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000191 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
192 // We deal with names referenced from blocks the same way.
193 case Expr::BlockDeclRefExprClass:
194 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
195
196 // Member access is complex.
197 case Expr::MemberExprClass:
198 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
199
200 case Expr::UnaryOperatorClass:
201 switch (cast<UnaryOperator>(E)->getOpcode()) {
202 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
203 // [...] the result is an lvalue referring to the object or function
204 // to which the expression points.
John McCall2de56d12010-08-25 11:45:40 +0000205 case UO_Deref:
Sebastian Redl2111c852010-06-28 15:09:07 +0000206 return Cl::CL_LValue;
207
208 // GNU extensions, simply look through them.
John McCall2de56d12010-08-25 11:45:40 +0000209 case UO_Extension:
Sebastian Redl2111c852010-06-28 15:09:07 +0000210 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
211
John McCallb418d742010-11-16 10:08:07 +0000212 // Treat _Real and _Imag basically as if they were member
213 // expressions: l-value only if the operand is a true l-value.
214 case UO_Real:
215 case UO_Imag: {
216 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
217 Cl::Kinds K = ClassifyInternal(Ctx, Op);
218 if (K != Cl::CL_LValue) return K;
219
John McCall12f78a62010-12-02 01:19:52 +0000220 if (isa<ObjCPropertyRefExpr>(Op))
John McCallb418d742010-11-16 10:08:07 +0000221 return Cl::CL_SubObjCPropertySetting;
222 return Cl::CL_LValue;
223 }
224
Sebastian Redl2111c852010-06-28 15:09:07 +0000225 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
226 // lvalue, [...]
227 // Not so in C.
John McCall2de56d12010-08-25 11:45:40 +0000228 case UO_PreInc:
229 case UO_PreDec:
Sebastian Redl2111c852010-06-28 15:09:07 +0000230 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
231
232 default:
233 return Cl::CL_PRValue;
234 }
235
John McCall7cd7d1a2010-11-15 23:31:06 +0000236 case Expr::OpaqueValueExprClass:
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000237 return ClassifyExprValueKind(Lang, E, E->getValueKind());
John McCall7cd7d1a2010-11-15 23:31:06 +0000238
John McCall4b9c2d22011-11-06 09:01:30 +0000239 // Pseudo-object expressions can produce l-values with reference magic.
240 case Expr::PseudoObjectExprClass:
241 return ClassifyExprValueKind(Lang, E,
242 cast<PseudoObjectExpr>(E)->getValueKind());
243
Sebastian Redl2111c852010-06-28 15:09:07 +0000244 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
245 // only specifically record class temporaries.
246 case Expr::ImplicitCastExprClass:
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000247 return ClassifyExprValueKind(Lang, E, E->getValueKind());
Sebastian Redl2111c852010-06-28 15:09:07 +0000248
249 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
250 // whether the expression is an lvalue.
251 case Expr::ParenExprClass:
252 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
253
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000254 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
Peter Collingbournef111d932011-04-15 00:35:48 +0000255 // or a void expression if its result expression is, respectively, an
256 // lvalue, a function designator, or a void expression.
257 case Expr::GenericSelectionExprClass:
258 if (cast<GenericSelectionExpr>(E)->isResultDependent())
259 return Cl::CL_PRValue;
260 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
261
Sebastian Redl2111c852010-06-28 15:09:07 +0000262 case Expr::BinaryOperatorClass:
263 case Expr::CompoundAssignOperatorClass:
264 // C doesn't have any binary expressions that are lvalues.
265 if (Lang.CPlusPlus)
266 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
267 return Cl::CL_PRValue;
268
269 case Expr::CallExprClass:
270 case Expr::CXXOperatorCallExprClass:
271 case Expr::CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000272 case Expr::UserDefinedLiteralClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000273 case Expr::CUDAKernelCallExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000274 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
275
276 // __builtin_choose_expr is equivalent to the chosen expression.
277 case Expr::ChooseExprClass:
278 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
279
280 // Extended vector element access is an lvalue unless there are duplicates
281 // in the shuffle expression.
282 case Expr::ExtVectorElementExprClass:
283 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
284 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
285
286 // Simply look at the actual default argument.
287 case Expr::CXXDefaultArgExprClass:
288 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
289
290 // Same idea for temporary binding.
291 case Expr::CXXBindTemporaryExprClass:
292 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
293
John McCall4765fa02010-12-06 08:20:24 +0000294 // And the cleanups guard.
295 case Expr::ExprWithCleanupsClass:
296 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redl2111c852010-06-28 15:09:07 +0000297
298 // Casts depend completely on the target type. All casts work the same.
299 case Expr::CStyleCastExprClass:
300 case Expr::CXXFunctionalCastExprClass:
301 case Expr::CXXStaticCastExprClass:
302 case Expr::CXXDynamicCastExprClass:
303 case Expr::CXXReinterpretCastExprClass:
304 case Expr::CXXConstCastExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000305 case Expr::ObjCBridgedCastExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000306 // Only in C++ can casts be interesting at all.
307 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
308 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
309
Douglas Gregor032c8692011-07-08 15:50:43 +0000310 case Expr::CXXUnresolvedConstructExprClass:
311 return ClassifyUnnamed(Ctx,
312 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
313
John McCall56ca35d2011-02-17 10:25:35 +0000314 case Expr::BinaryConditionalOperatorClass: {
315 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
316 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
317 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
318 }
319
320 case Expr::ConditionalOperatorClass: {
Sebastian Redl2111c852010-06-28 15:09:07 +0000321 // Once again, only C++ is interesting.
322 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
John McCall56ca35d2011-02-17 10:25:35 +0000323 const ConditionalOperator *co = cast<ConditionalOperator>(E);
324 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
325 }
Sebastian Redl2111c852010-06-28 15:09:07 +0000326
327 // ObjC message sends are effectively function calls, if the target function
328 // is known.
329 case Expr::ObjCMessageExprClass:
330 if (const ObjCMethodDecl *Method =
331 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000332 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
333 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
Sebastian Redl2111c852010-06-28 15:09:07 +0000334 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000335 return Cl::CL_PRValue;
336
Sebastian Redl2111c852010-06-28 15:09:07 +0000337 // Some C++ expressions are always class temporaries.
338 case Expr::CXXConstructExprClass:
339 case Expr::CXXTemporaryObjectExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +0000340 case Expr::LambdaExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000341 return Cl::CL_ClassTemporary;
342
Douglas Gregor4178b572010-09-14 21:51:42 +0000343 case Expr::VAArgExprClass:
344 return ClassifyUnnamed(Ctx, E->getType());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000345
Douglas Gregor4178b572010-09-14 21:51:42 +0000346 case Expr::DesignatedInitExprClass:
347 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000348
Douglas Gregor4178b572010-09-14 21:51:42 +0000349 case Expr::StmtExprClass: {
350 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
351 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregor226cbfc2010-09-15 01:37:48 +0000352 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redl2111c852010-06-28 15:09:07 +0000353 return Cl::CL_PRValue;
354 }
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000355
Douglas Gregor4178b572010-09-14 21:51:42 +0000356 case Expr::CXXUuidofExprClass:
Francois Pichetecea19f2010-12-17 02:00:06 +0000357 return Cl::CL_LValue;
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000358
Douglas Gregorbe230c32011-01-03 17:17:50 +0000359 case Expr::PackExpansionExprClass:
360 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000361
Douglas Gregor03e80032011-06-21 17:03:29 +0000362 case Expr::MaterializeTemporaryExprClass:
Douglas Gregor0b581082011-06-21 18:20:46 +0000363 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
Douglas Gregor03e80032011-06-21 17:03:29 +0000364 ? Cl::CL_LValue
365 : Cl::CL_XValue;
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000366
367 case Expr::InitListExprClass:
368 // An init list can be an lvalue if it is bound to a reference and
369 // contains only one element. In that case, we look at that element
370 // for an exact classification. Init list creation takes care of the
371 // value kind for us, so we only need to fine-tune.
372 if (E->isRValue())
373 return ClassifyExprValueKind(Lang, E, E->getValueKind());
374 assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
375 "Only 1-element init lists can be glvalues.");
376 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
Douglas Gregor4178b572010-09-14 21:51:42 +0000377 }
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000378
Douglas Gregor4178b572010-09-14 21:51:42 +0000379 llvm_unreachable("unhandled expression kind in classification");
Sebastian Redl2111c852010-06-28 15:09:07 +0000380}
381
382/// ClassifyDecl - Return the classification of an expression referencing the
383/// given declaration.
384static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
385 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
386 // function, variable, or data member and a prvalue otherwise.
387 // In C, functions are not lvalues.
388 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
389 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
390 // special-case this.
John McCall9c72c602010-08-27 09:08:28 +0000391
392 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
393 return Cl::CL_MemberFunction;
394
Sebastian Redl2111c852010-06-28 15:09:07 +0000395 bool islvalue;
396 if (const NonTypeTemplateParmDecl *NTTParm =
397 dyn_cast<NonTypeTemplateParmDecl>(D))
398 islvalue = NTTParm->getType()->isReferenceType();
399 else
400 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet87c2e122010-11-21 06:08:52 +0000401 isa<IndirectFieldDecl>(D) ||
Sebastian Redl2111c852010-06-28 15:09:07 +0000402 (Ctx.getLangOptions().CPlusPlus &&
403 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
404
405 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
406}
407
408/// ClassifyUnnamed - Return the classification of an expression yielding an
409/// unnamed value of the given type. This applies in particular to function
410/// calls and casts.
411static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
412 // In C, function calls are always rvalues.
413 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
414
415 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
416 // lvalue reference type or an rvalue reference to function type, an xvalue
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000417 // if the result type is an rvalue reference to object type, and a prvalue
Sebastian Redl2111c852010-06-28 15:09:07 +0000418 // otherwise.
419 if (T->isLValueReferenceType())
420 return Cl::CL_LValue;
421 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
422 if (!RV) // Could still be a class temporary, though.
423 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
424
425 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
426}
427
428static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
John McCall755d8492011-04-12 00:42:48 +0000429 if (E->getType() == Ctx.UnknownAnyTy)
430 return (isa<FunctionDecl>(E->getMemberDecl())
431 ? Cl::CL_PRValue : Cl::CL_LValue);
432
Sebastian Redl2111c852010-06-28 15:09:07 +0000433 // Handle C first, it's easier.
434 if (!Ctx.getLangOptions().CPlusPlus) {
435 // C99 6.5.2.3p3
436 // For dot access, the expression is an lvalue if the first part is. For
437 // arrow access, it always is an lvalue.
438 if (E->isArrow())
439 return Cl::CL_LValue;
440 // ObjC property accesses are not lvalues, but get special treatment.
John McCallb418d742010-11-16 10:08:07 +0000441 Expr *Base = E->getBase()->IgnoreParens();
John McCall12f78a62010-12-02 01:19:52 +0000442 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redl2111c852010-06-28 15:09:07 +0000443 return Cl::CL_SubObjCPropertySetting;
444 return ClassifyInternal(Ctx, Base);
445 }
446
447 NamedDecl *Member = E->getMemberDecl();
448 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
449 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
450 // E1.E2 is an lvalue.
451 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
452 if (Value->getType()->isReferenceType())
453 return Cl::CL_LValue;
454
455 // Otherwise, one of the following rules applies.
456 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
457 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
458 return Cl::CL_LValue;
459
460 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
461 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
462 // otherwise, it is a prvalue.
463 if (isa<FieldDecl>(Member)) {
464 // *E1 is an lvalue
465 if (E->isArrow())
466 return Cl::CL_LValue;
John McCall09431682010-11-18 19:01:18 +0000467 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCall12f78a62010-12-02 01:19:52 +0000468 if (isa<ObjCPropertyRefExpr>(Base))
John McCall09431682010-11-18 19:01:18 +0000469 return Cl::CL_SubObjCPropertySetting;
Sebastian Redl2111c852010-06-28 15:09:07 +0000470 return ClassifyInternal(Ctx, E->getBase());
471 }
472
473 // -- If E2 is a [...] member function, [...]
474 // -- If it refers to a static member function [...], then E1.E2 is an
475 // lvalue; [...]
476 // -- Otherwise [...] E1.E2 is a prvalue.
477 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
478 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
479
480 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
481 // So is everything else we haven't handled yet.
482 return Cl::CL_PRValue;
483}
484
485static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
486 assert(Ctx.getLangOptions().CPlusPlus &&
487 "This is only relevant for C++.");
488 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCallf6a16482010-12-04 03:47:34 +0000489 // Except we override this for writes to ObjC properties.
Sebastian Redl2111c852010-06-28 15:09:07 +0000490 if (E->isAssignmentOp())
John McCallf6a16482010-12-04 03:47:34 +0000491 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
492 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redl2111c852010-06-28 15:09:07 +0000493
494 // C++ [expr.comma]p1: the result is of the same value category as its right
495 // operand, [...].
John McCall2de56d12010-08-25 11:45:40 +0000496 if (E->getOpcode() == BO_Comma)
Sebastian Redl2111c852010-06-28 15:09:07 +0000497 return ClassifyInternal(Ctx, E->getRHS());
498
499 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
500 // is a pointer to a data member is of the same value category as its first
501 // operand.
John McCall2de56d12010-08-25 11:45:40 +0000502 if (E->getOpcode() == BO_PtrMemD)
John McCalle0a22d02011-10-18 21:02:43 +0000503 return (E->getType()->isFunctionType() ||
504 E->hasPlaceholderType(BuiltinType::BoundMember))
Douglas Gregorb0844c62011-05-21 21:04:55 +0000505 ? Cl::CL_MemberFunction
506 : ClassifyInternal(Ctx, E->getLHS());
Sebastian Redl2111c852010-06-28 15:09:07 +0000507
508 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
509 // second operand is a pointer to data member and a prvalue otherwise.
John McCall2de56d12010-08-25 11:45:40 +0000510 if (E->getOpcode() == BO_PtrMemI)
John McCalle0a22d02011-10-18 21:02:43 +0000511 return (E->getType()->isFunctionType() ||
512 E->hasPlaceholderType(BuiltinType::BoundMember))
Douglas Gregorb0844c62011-05-21 21:04:55 +0000513 ? Cl::CL_MemberFunction
514 : Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000515
516 // All other binary operations are prvalues.
517 return Cl::CL_PRValue;
518}
519
John McCall56ca35d2011-02-17 10:25:35 +0000520static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
521 const Expr *False) {
Sebastian Redl2111c852010-06-28 15:09:07 +0000522 assert(Ctx.getLangOptions().CPlusPlus &&
523 "This is only relevant for C++.");
524
Sebastian Redl2111c852010-06-28 15:09:07 +0000525 // C++ [expr.cond]p2
526 // If either the second or the third operand has type (cv) void, [...]
527 // the result [...] is a prvalue.
528 if (True->getType()->isVoidType() || False->getType()->isVoidType())
529 return Cl::CL_PRValue;
530
531 // Note that at this point, we have already performed all conversions
532 // according to [expr.cond]p3.
533 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
534 // same value category [...], the result is of that [...] value category.
535 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
536 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
537 RCl = ClassifyInternal(Ctx, False);
538 return LCl == RCl ? LCl : Cl::CL_PRValue;
539}
540
541static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
542 Cl::Kinds Kind, SourceLocation &Loc) {
543 // As a general rule, we only care about lvalues. But there are some rvalues
544 // for which we want to generate special results.
545 if (Kind == Cl::CL_PRValue) {
546 // For the sake of better diagnostics, we want to specifically recognize
547 // use of the GCC cast-as-lvalue extension.
John McCallf6a16482010-12-04 03:47:34 +0000548 if (const ExplicitCastExpr *CE =
549 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
550 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
551 Loc = CE->getExprLoc();
Sebastian Redl2111c852010-06-28 15:09:07 +0000552 return Cl::CM_LValueCast;
553 }
554 }
555 }
556 if (Kind != Cl::CL_LValue)
557 return Cl::CM_RValue;
558
559 // This is the lvalue case.
560 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
561 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
562 return Cl::CM_Function;
563
564 // You cannot assign to a variable outside a block from within the block if
565 // it is not marked __block, e.g.
566 // void takeclosure(void (^C)(void));
567 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
568 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
569 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
570 return Cl::CM_NotBlockQualified;
571 }
572
573 // Assignment to a property in ObjC is an implicit setter access. But a
574 // setter might not exist.
John McCall12f78a62010-12-02 01:19:52 +0000575 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
576 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redl2111c852010-06-28 15:09:07 +0000577 return Cl::CM_NoSetterProperty;
578 }
579
580 CanQualType CT = Ctx.getCanonicalType(E->getType());
581 // Const stuff is obviously not modifiable.
582 if (CT.isConstQualified())
583 return Cl::CM_ConstQualified;
584 // Arrays are not modifiable, only their elements are.
585 if (CT->isArrayType())
586 return Cl::CM_ArrayType;
587 // Incomplete types are not modifiable.
588 if (CT->isIncompleteType())
589 return Cl::CM_IncompleteType;
590
591 // Records with any const fields (recursively) are not modifiable.
592 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall01b2e4e2010-12-06 05:26:58 +0000593 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahanian4088ec02010-09-09 23:01:10 +0000594 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redl2111c852010-06-28 15:09:07 +0000595 "C++ struct assignment should be resolved by the "
596 "copy assignment operator.");
597 if (R->hasConstFields())
598 return Cl::CM_ConstQualified;
599 }
600
601 return Cl::CM_Modifiable;
602}
603
John McCall7eb0a9e2010-11-24 05:12:34 +0000604Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redl2111c852010-06-28 15:09:07 +0000605 Classification VC = Classify(Ctx);
606 switch (VC.getKind()) {
607 case Cl::CL_LValue: return LV_Valid;
608 case Cl::CL_XValue: return LV_InvalidExpression;
609 case Cl::CL_Function: return LV_NotObjectType;
Peter Collingbourne1c860d52011-04-19 18:51:51 +0000610 case Cl::CL_Void: return LV_InvalidExpression;
611 case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
Sebastian Redl2111c852010-06-28 15:09:07 +0000612 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
613 case Cl::CL_MemberFunction: return LV_MemberFunction;
614 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
615 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000616 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
Sebastian Redl2111c852010-06-28 15:09:07 +0000617 case Cl::CL_PRValue: return LV_InvalidExpression;
618 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000619 llvm_unreachable("Unhandled kind");
Sebastian Redl2111c852010-06-28 15:09:07 +0000620}
621
622Expr::isModifiableLvalueResult
623Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
624 SourceLocation dummy;
625 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
626 switch (VC.getKind()) {
627 case Cl::CL_LValue: break;
628 case Cl::CL_XValue: return MLV_InvalidExpression;
629 case Cl::CL_Function: return MLV_NotObjectType;
Peter Collingbourne1c860d52011-04-19 18:51:51 +0000630 case Cl::CL_Void: return MLV_InvalidExpression;
631 case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
Sebastian Redl2111c852010-06-28 15:09:07 +0000632 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
633 case Cl::CL_MemberFunction: return MLV_MemberFunction;
634 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
635 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000636 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
Sebastian Redl2111c852010-06-28 15:09:07 +0000637 case Cl::CL_PRValue:
638 return VC.getModifiable() == Cl::CM_LValueCast ?
639 MLV_LValueCast : MLV_InvalidExpression;
640 }
641 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
642 switch (VC.getModifiable()) {
Chandler Carruth0010bca2010-06-29 00:23:11 +0000643 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redl2111c852010-06-28 15:09:07 +0000644 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth0010bca2010-06-29 00:23:11 +0000645 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000646 case Cl::CM_Function: return MLV_NotObjectType;
647 case Cl::CM_LValueCast:
Chandler Carruth0010bca2010-06-29 00:23:11 +0000648 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000649 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
650 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
651 case Cl::CM_ConstQualified: return MLV_ConstQualified;
652 case Cl::CM_ArrayType: return MLV_ArrayType;
653 case Cl::CM_IncompleteType: return MLV_IncompleteType;
654 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000655 llvm_unreachable("Unhandled modifiable type");
Sebastian Redl2111c852010-06-28 15:09:07 +0000656}