blob: 693e28c8a45fa8ca7b0a47e88eec013e1b01e4d4 [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:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000272 case Expr::CUDAKernelCallExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000273 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
274
275 // __builtin_choose_expr is equivalent to the chosen expression.
276 case Expr::ChooseExprClass:
277 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
278
279 // Extended vector element access is an lvalue unless there are duplicates
280 // in the shuffle expression.
281 case Expr::ExtVectorElementExprClass:
282 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
283 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
284
285 // Simply look at the actual default argument.
286 case Expr::CXXDefaultArgExprClass:
287 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
288
289 // Same idea for temporary binding.
290 case Expr::CXXBindTemporaryExprClass:
291 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
292
John McCall4765fa02010-12-06 08:20:24 +0000293 // And the cleanups guard.
294 case Expr::ExprWithCleanupsClass:
295 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redl2111c852010-06-28 15:09:07 +0000296
297 // Casts depend completely on the target type. All casts work the same.
298 case Expr::CStyleCastExprClass:
299 case Expr::CXXFunctionalCastExprClass:
300 case Expr::CXXStaticCastExprClass:
301 case Expr::CXXDynamicCastExprClass:
302 case Expr::CXXReinterpretCastExprClass:
303 case Expr::CXXConstCastExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000304 case Expr::ObjCBridgedCastExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000305 // Only in C++ can casts be interesting at all.
306 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
307 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
308
Douglas Gregor032c8692011-07-08 15:50:43 +0000309 case Expr::CXXUnresolvedConstructExprClass:
310 return ClassifyUnnamed(Ctx,
311 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
312
John McCall56ca35d2011-02-17 10:25:35 +0000313 case Expr::BinaryConditionalOperatorClass: {
314 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
315 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
316 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
317 }
318
319 case Expr::ConditionalOperatorClass: {
Sebastian Redl2111c852010-06-28 15:09:07 +0000320 // Once again, only C++ is interesting.
321 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
John McCall56ca35d2011-02-17 10:25:35 +0000322 const ConditionalOperator *co = cast<ConditionalOperator>(E);
323 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
324 }
Sebastian Redl2111c852010-06-28 15:09:07 +0000325
326 // ObjC message sends are effectively function calls, if the target function
327 // is known.
328 case Expr::ObjCMessageExprClass:
329 if (const ObjCMethodDecl *Method =
330 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000331 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
332 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
Sebastian Redl2111c852010-06-28 15:09:07 +0000333 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000334 return Cl::CL_PRValue;
335
Sebastian Redl2111c852010-06-28 15:09:07 +0000336 // Some C++ expressions are always class temporaries.
337 case Expr::CXXConstructExprClass:
338 case Expr::CXXTemporaryObjectExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +0000339 case Expr::LambdaExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000340 return Cl::CL_ClassTemporary;
341
Douglas Gregor4178b572010-09-14 21:51:42 +0000342 case Expr::VAArgExprClass:
343 return ClassifyUnnamed(Ctx, E->getType());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000344
Douglas Gregor4178b572010-09-14 21:51:42 +0000345 case Expr::DesignatedInitExprClass:
346 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000347
Douglas Gregor4178b572010-09-14 21:51:42 +0000348 case Expr::StmtExprClass: {
349 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
350 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregor226cbfc2010-09-15 01:37:48 +0000351 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redl2111c852010-06-28 15:09:07 +0000352 return Cl::CL_PRValue;
353 }
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000354
Douglas Gregor4178b572010-09-14 21:51:42 +0000355 case Expr::CXXUuidofExprClass:
Francois Pichetecea19f2010-12-17 02:00:06 +0000356 return Cl::CL_LValue;
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000357
Douglas Gregorbe230c32011-01-03 17:17:50 +0000358 case Expr::PackExpansionExprClass:
359 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000360
Douglas Gregor03e80032011-06-21 17:03:29 +0000361 case Expr::MaterializeTemporaryExprClass:
Douglas Gregor0b581082011-06-21 18:20:46 +0000362 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
Douglas Gregor03e80032011-06-21 17:03:29 +0000363 ? Cl::CL_LValue
364 : Cl::CL_XValue;
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000365
366 case Expr::InitListExprClass:
367 // An init list can be an lvalue if it is bound to a reference and
368 // contains only one element. In that case, we look at that element
369 // for an exact classification. Init list creation takes care of the
370 // value kind for us, so we only need to fine-tune.
371 if (E->isRValue())
372 return ClassifyExprValueKind(Lang, E, E->getValueKind());
373 assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
374 "Only 1-element init lists can be glvalues.");
375 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
Douglas Gregor4178b572010-09-14 21:51:42 +0000376 }
Sebastian Redl13dc8f92011-11-27 16:50:07 +0000377
Douglas Gregor4178b572010-09-14 21:51:42 +0000378 llvm_unreachable("unhandled expression kind in classification");
Sebastian Redl2111c852010-06-28 15:09:07 +0000379}
380
381/// ClassifyDecl - Return the classification of an expression referencing the
382/// given declaration.
383static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
384 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
385 // function, variable, or data member and a prvalue otherwise.
386 // In C, functions are not lvalues.
387 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
388 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
389 // special-case this.
John McCall9c72c602010-08-27 09:08:28 +0000390
391 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
392 return Cl::CL_MemberFunction;
393
Sebastian Redl2111c852010-06-28 15:09:07 +0000394 bool islvalue;
395 if (const NonTypeTemplateParmDecl *NTTParm =
396 dyn_cast<NonTypeTemplateParmDecl>(D))
397 islvalue = NTTParm->getType()->isReferenceType();
398 else
399 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet87c2e122010-11-21 06:08:52 +0000400 isa<IndirectFieldDecl>(D) ||
Sebastian Redl2111c852010-06-28 15:09:07 +0000401 (Ctx.getLangOptions().CPlusPlus &&
402 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
403
404 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
405}
406
407/// ClassifyUnnamed - Return the classification of an expression yielding an
408/// unnamed value of the given type. This applies in particular to function
409/// calls and casts.
410static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
411 // In C, function calls are always rvalues.
412 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
413
414 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
415 // lvalue reference type or an rvalue reference to function type, an xvalue
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000416 // if the result type is an rvalue reference to object type, and a prvalue
Sebastian Redl2111c852010-06-28 15:09:07 +0000417 // otherwise.
418 if (T->isLValueReferenceType())
419 return Cl::CL_LValue;
420 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
421 if (!RV) // Could still be a class temporary, though.
422 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
423
424 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
425}
426
427static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
John McCall755d8492011-04-12 00:42:48 +0000428 if (E->getType() == Ctx.UnknownAnyTy)
429 return (isa<FunctionDecl>(E->getMemberDecl())
430 ? Cl::CL_PRValue : Cl::CL_LValue);
431
Sebastian Redl2111c852010-06-28 15:09:07 +0000432 // Handle C first, it's easier.
433 if (!Ctx.getLangOptions().CPlusPlus) {
434 // C99 6.5.2.3p3
435 // For dot access, the expression is an lvalue if the first part is. For
436 // arrow access, it always is an lvalue.
437 if (E->isArrow())
438 return Cl::CL_LValue;
439 // ObjC property accesses are not lvalues, but get special treatment.
John McCallb418d742010-11-16 10:08:07 +0000440 Expr *Base = E->getBase()->IgnoreParens();
John McCall12f78a62010-12-02 01:19:52 +0000441 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redl2111c852010-06-28 15:09:07 +0000442 return Cl::CL_SubObjCPropertySetting;
443 return ClassifyInternal(Ctx, Base);
444 }
445
446 NamedDecl *Member = E->getMemberDecl();
447 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
448 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
449 // E1.E2 is an lvalue.
450 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
451 if (Value->getType()->isReferenceType())
452 return Cl::CL_LValue;
453
454 // Otherwise, one of the following rules applies.
455 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
456 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
457 return Cl::CL_LValue;
458
459 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
460 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
461 // otherwise, it is a prvalue.
462 if (isa<FieldDecl>(Member)) {
463 // *E1 is an lvalue
464 if (E->isArrow())
465 return Cl::CL_LValue;
John McCall09431682010-11-18 19:01:18 +0000466 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCall12f78a62010-12-02 01:19:52 +0000467 if (isa<ObjCPropertyRefExpr>(Base))
John McCall09431682010-11-18 19:01:18 +0000468 return Cl::CL_SubObjCPropertySetting;
Sebastian Redl2111c852010-06-28 15:09:07 +0000469 return ClassifyInternal(Ctx, E->getBase());
470 }
471
472 // -- If E2 is a [...] member function, [...]
473 // -- If it refers to a static member function [...], then E1.E2 is an
474 // lvalue; [...]
475 // -- Otherwise [...] E1.E2 is a prvalue.
476 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
477 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
478
479 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
480 // So is everything else we haven't handled yet.
481 return Cl::CL_PRValue;
482}
483
484static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
485 assert(Ctx.getLangOptions().CPlusPlus &&
486 "This is only relevant for C++.");
487 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCallf6a16482010-12-04 03:47:34 +0000488 // Except we override this for writes to ObjC properties.
Sebastian Redl2111c852010-06-28 15:09:07 +0000489 if (E->isAssignmentOp())
John McCallf6a16482010-12-04 03:47:34 +0000490 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
491 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redl2111c852010-06-28 15:09:07 +0000492
493 // C++ [expr.comma]p1: the result is of the same value category as its right
494 // operand, [...].
John McCall2de56d12010-08-25 11:45:40 +0000495 if (E->getOpcode() == BO_Comma)
Sebastian Redl2111c852010-06-28 15:09:07 +0000496 return ClassifyInternal(Ctx, E->getRHS());
497
498 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
499 // is a pointer to a data member is of the same value category as its first
500 // operand.
John McCall2de56d12010-08-25 11:45:40 +0000501 if (E->getOpcode() == BO_PtrMemD)
John McCalle0a22d02011-10-18 21:02:43 +0000502 return (E->getType()->isFunctionType() ||
503 E->hasPlaceholderType(BuiltinType::BoundMember))
Douglas Gregorb0844c62011-05-21 21:04:55 +0000504 ? Cl::CL_MemberFunction
505 : ClassifyInternal(Ctx, E->getLHS());
Sebastian Redl2111c852010-06-28 15:09:07 +0000506
507 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
508 // second operand is a pointer to data member and a prvalue otherwise.
John McCall2de56d12010-08-25 11:45:40 +0000509 if (E->getOpcode() == BO_PtrMemI)
John McCalle0a22d02011-10-18 21:02:43 +0000510 return (E->getType()->isFunctionType() ||
511 E->hasPlaceholderType(BuiltinType::BoundMember))
Douglas Gregorb0844c62011-05-21 21:04:55 +0000512 ? Cl::CL_MemberFunction
513 : Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000514
515 // All other binary operations are prvalues.
516 return Cl::CL_PRValue;
517}
518
John McCall56ca35d2011-02-17 10:25:35 +0000519static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
520 const Expr *False) {
Sebastian Redl2111c852010-06-28 15:09:07 +0000521 assert(Ctx.getLangOptions().CPlusPlus &&
522 "This is only relevant for C++.");
523
Sebastian Redl2111c852010-06-28 15:09:07 +0000524 // C++ [expr.cond]p2
525 // If either the second or the third operand has type (cv) void, [...]
526 // the result [...] is a prvalue.
527 if (True->getType()->isVoidType() || False->getType()->isVoidType())
528 return Cl::CL_PRValue;
529
530 // Note that at this point, we have already performed all conversions
531 // according to [expr.cond]p3.
532 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
533 // same value category [...], the result is of that [...] value category.
534 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
535 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
536 RCl = ClassifyInternal(Ctx, False);
537 return LCl == RCl ? LCl : Cl::CL_PRValue;
538}
539
540static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
541 Cl::Kinds Kind, SourceLocation &Loc) {
542 // As a general rule, we only care about lvalues. But there are some rvalues
543 // for which we want to generate special results.
544 if (Kind == Cl::CL_PRValue) {
545 // For the sake of better diagnostics, we want to specifically recognize
546 // use of the GCC cast-as-lvalue extension.
John McCallf6a16482010-12-04 03:47:34 +0000547 if (const ExplicitCastExpr *CE =
548 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
549 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
550 Loc = CE->getExprLoc();
Sebastian Redl2111c852010-06-28 15:09:07 +0000551 return Cl::CM_LValueCast;
552 }
553 }
554 }
555 if (Kind != Cl::CL_LValue)
556 return Cl::CM_RValue;
557
558 // This is the lvalue case.
559 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
560 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
561 return Cl::CM_Function;
562
563 // You cannot assign to a variable outside a block from within the block if
564 // it is not marked __block, e.g.
565 // void takeclosure(void (^C)(void));
566 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
567 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
568 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
569 return Cl::CM_NotBlockQualified;
570 }
571
572 // Assignment to a property in ObjC is an implicit setter access. But a
573 // setter might not exist.
John McCall12f78a62010-12-02 01:19:52 +0000574 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
575 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redl2111c852010-06-28 15:09:07 +0000576 return Cl::CM_NoSetterProperty;
577 }
578
579 CanQualType CT = Ctx.getCanonicalType(E->getType());
580 // Const stuff is obviously not modifiable.
581 if (CT.isConstQualified())
582 return Cl::CM_ConstQualified;
583 // Arrays are not modifiable, only their elements are.
584 if (CT->isArrayType())
585 return Cl::CM_ArrayType;
586 // Incomplete types are not modifiable.
587 if (CT->isIncompleteType())
588 return Cl::CM_IncompleteType;
589
590 // Records with any const fields (recursively) are not modifiable.
591 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall01b2e4e2010-12-06 05:26:58 +0000592 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahanian4088ec02010-09-09 23:01:10 +0000593 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redl2111c852010-06-28 15:09:07 +0000594 "C++ struct assignment should be resolved by the "
595 "copy assignment operator.");
596 if (R->hasConstFields())
597 return Cl::CM_ConstQualified;
598 }
599
600 return Cl::CM_Modifiable;
601}
602
John McCall7eb0a9e2010-11-24 05:12:34 +0000603Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redl2111c852010-06-28 15:09:07 +0000604 Classification VC = Classify(Ctx);
605 switch (VC.getKind()) {
606 case Cl::CL_LValue: return LV_Valid;
607 case Cl::CL_XValue: return LV_InvalidExpression;
608 case Cl::CL_Function: return LV_NotObjectType;
Peter Collingbourne1c860d52011-04-19 18:51:51 +0000609 case Cl::CL_Void: return LV_InvalidExpression;
610 case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
Sebastian Redl2111c852010-06-28 15:09:07 +0000611 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
612 case Cl::CL_MemberFunction: return LV_MemberFunction;
613 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
614 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000615 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
Sebastian Redl2111c852010-06-28 15:09:07 +0000616 case Cl::CL_PRValue: return LV_InvalidExpression;
617 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000618 llvm_unreachable("Unhandled kind");
Sebastian Redl2111c852010-06-28 15:09:07 +0000619}
620
621Expr::isModifiableLvalueResult
622Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
623 SourceLocation dummy;
624 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
625 switch (VC.getKind()) {
626 case Cl::CL_LValue: break;
627 case Cl::CL_XValue: return MLV_InvalidExpression;
628 case Cl::CL_Function: return MLV_NotObjectType;
Peter Collingbourne1c860d52011-04-19 18:51:51 +0000629 case Cl::CL_Void: return MLV_InvalidExpression;
630 case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
Sebastian Redl2111c852010-06-28 15:09:07 +0000631 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
632 case Cl::CL_MemberFunction: return MLV_MemberFunction;
633 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
634 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
Fariborz Jahanian077f4902011-03-26 19:48:30 +0000635 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
Sebastian Redl2111c852010-06-28 15:09:07 +0000636 case Cl::CL_PRValue:
637 return VC.getModifiable() == Cl::CM_LValueCast ?
638 MLV_LValueCast : MLV_InvalidExpression;
639 }
640 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
641 switch (VC.getModifiable()) {
Chandler Carruth0010bca2010-06-29 00:23:11 +0000642 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redl2111c852010-06-28 15:09:07 +0000643 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth0010bca2010-06-29 00:23:11 +0000644 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000645 case Cl::CM_Function: return MLV_NotObjectType;
646 case Cl::CM_LValueCast:
Chandler Carruth0010bca2010-06-29 00:23:11 +0000647 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000648 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
649 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
650 case Cl::CM_ConstQualified: return MLV_ConstQualified;
651 case Cl::CM_ArrayType: return MLV_ArrayType;
652 case Cl::CM_IncompleteType: return MLV_IncompleteType;
653 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000654 llvm_unreachable("Unhandled modifiable type");
Sebastian Redl2111c852010-06-28 15:09:07 +0000655}