blob: 376792223ad78992c7110d4bfd874c58fd311e09 [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.");
50 return Cl::CL_PRValue;
51}
52
Sebastian Redl2111c852010-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 McCall09431682010-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:
78 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
79 }
John McCall09431682010-11-18 19:01:18 +000080
Sebastian Redl2111c852010-06-28 15:09:07 +000081 Cl::ModifiableType modifiable = Cl::CM_Untested;
82 if (Loc)
83 modifiable = IsModifiable(Ctx, this, kind, *Loc);
84 return Classification(kind, modifiable);
85}
86
87static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
88 // This function takes the first stab at classifying expressions.
89 const LangOptions &Lang = Ctx.getLangOptions();
90
91 switch (E->getStmtClass()) {
92 // First come the expressions that are always lvalues, unconditionally.
Douglas Gregor4178b572010-09-14 21:51:42 +000093 case Stmt::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +000094#define ABSTRACT_STMT(Kind)
Douglas Gregor4178b572010-09-14 21:51:42 +000095#define STMT(Kind, Base) case Expr::Kind##Class:
96#define EXPR(Kind, Base)
97#include "clang/AST/StmtNodes.inc"
98 llvm_unreachable("cannot classify a statement");
99 break;
Sebastian Redl2111c852010-06-28 15:09:07 +0000100 case Expr::ObjCIsaExprClass:
101 // C++ [expr.prim.general]p1: A string literal is an lvalue.
102 case Expr::StringLiteralClass:
103 // @encode is equivalent to its string
104 case Expr::ObjCEncodeExprClass:
105 // __func__ and friends are too.
106 case Expr::PredefinedExprClass:
107 // Property references are lvalues
108 case Expr::ObjCPropertyRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000109 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
110 case Expr::CXXTypeidExprClass:
111 // Unresolved lookups get classified as lvalues.
112 // FIXME: Is this wise? Should they get their own kind?
113 case Expr::UnresolvedLookupExprClass:
114 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000115 case Expr::CXXDependentScopeMemberExprClass:
116 case Expr::CXXUnresolvedConstructExprClass:
117 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000118 // ObjC instance variables are lvalues
119 // FIXME: ObjC++0x might have different rules
120 case Expr::ObjCIvarRefExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000121 return Cl::CL_LValue;
Douglas Gregor4178b572010-09-14 21:51:42 +0000122 // C99 6.5.2.5p5 says that compound literals are lvalues.
123 // In C++, they're class temporaries.
124 case Expr::CompoundLiteralExprClass:
125 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
126 : Cl::CL_LValue;
127
128 // Expressions that are prvalues.
129 case Expr::CXXBoolLiteralExprClass:
130 case Expr::CXXPseudoDestructorExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000131 case Expr::UnaryExprOrTypeTraitExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000132 case Expr::CXXNewExprClass:
133 case Expr::CXXThisExprClass:
134 case Expr::CXXNullPtrLiteralExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000135 case Expr::ImaginaryLiteralClass:
136 case Expr::GNUNullExprClass:
137 case Expr::OffsetOfExprClass:
138 case Expr::CXXThrowExprClass:
139 case Expr::ShuffleVectorExprClass:
140 case Expr::IntegerLiteralClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000141 case Expr::CharacterLiteralClass:
142 case Expr::AddrLabelExprClass:
143 case Expr::CXXDeleteExprClass:
144 case Expr::ImplicitValueInitExprClass:
145 case Expr::BlockExprClass:
146 case Expr::FloatingLiteralClass:
147 case Expr::CXXNoexceptExprClass:
148 case Expr::CXXScalarValueInitExprClass:
149 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000150 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000151 case Expr::ObjCSelectorExprClass:
152 case Expr::ObjCProtocolExprClass:
153 case Expr::ObjCStringLiteralClass:
154 case Expr::ParenListExprClass:
155 case Expr::InitListExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000156 case Expr::SizeOfPackExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000157 case Expr::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor4178b572010-09-14 21:51:42 +0000158 return Cl::CL_PRValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000159
160 // Next come the complicated cases.
161
162 // C++ [expr.sub]p1: The result is an lvalue of type "T".
163 // However, subscripting vector types is more like member access.
164 case Expr::ArraySubscriptExprClass:
165 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
166 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
167 return Cl::CL_LValue;
168
169 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
170 // function or variable and a prvalue otherwise.
171 case Expr::DeclRefExprClass:
172 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
173 // We deal with names referenced from blocks the same way.
174 case Expr::BlockDeclRefExprClass:
175 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
176
177 // Member access is complex.
178 case Expr::MemberExprClass:
179 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
180
181 case Expr::UnaryOperatorClass:
182 switch (cast<UnaryOperator>(E)->getOpcode()) {
183 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
184 // [...] the result is an lvalue referring to the object or function
185 // to which the expression points.
John McCall2de56d12010-08-25 11:45:40 +0000186 case UO_Deref:
Sebastian Redl2111c852010-06-28 15:09:07 +0000187 return Cl::CL_LValue;
188
189 // GNU extensions, simply look through them.
John McCall2de56d12010-08-25 11:45:40 +0000190 case UO_Extension:
Sebastian Redl2111c852010-06-28 15:09:07 +0000191 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
192
John McCallb418d742010-11-16 10:08:07 +0000193 // Treat _Real and _Imag basically as if they were member
194 // expressions: l-value only if the operand is a true l-value.
195 case UO_Real:
196 case UO_Imag: {
197 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
198 Cl::Kinds K = ClassifyInternal(Ctx, Op);
199 if (K != Cl::CL_LValue) return K;
200
John McCall12f78a62010-12-02 01:19:52 +0000201 if (isa<ObjCPropertyRefExpr>(Op))
John McCallb418d742010-11-16 10:08:07 +0000202 return Cl::CL_SubObjCPropertySetting;
203 return Cl::CL_LValue;
204 }
205
Sebastian Redl2111c852010-06-28 15:09:07 +0000206 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
207 // lvalue, [...]
208 // Not so in C.
John McCall2de56d12010-08-25 11:45:40 +0000209 case UO_PreInc:
210 case UO_PreDec:
Sebastian Redl2111c852010-06-28 15:09:07 +0000211 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
212
213 default:
214 return Cl::CL_PRValue;
215 }
216
John McCall7cd7d1a2010-11-15 23:31:06 +0000217 case Expr::OpaqueValueExprClass:
218 return ClassifyExprValueKind(Lang, E,
219 cast<OpaqueValueExpr>(E)->getValueKind());
220
Sebastian Redl2111c852010-06-28 15:09:07 +0000221 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
222 // only specifically record class temporaries.
223 case Expr::ImplicitCastExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000224 return ClassifyExprValueKind(Lang, E,
225 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redl2111c852010-06-28 15:09:07 +0000226
227 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
228 // whether the expression is an lvalue.
229 case Expr::ParenExprClass:
230 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
231
232 case Expr::BinaryOperatorClass:
233 case Expr::CompoundAssignOperatorClass:
234 // C doesn't have any binary expressions that are lvalues.
235 if (Lang.CPlusPlus)
236 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
237 return Cl::CL_PRValue;
238
239 case Expr::CallExprClass:
240 case Expr::CXXOperatorCallExprClass:
241 case Expr::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000242 case Expr::CUDAKernelCallExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000243 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
244
245 // __builtin_choose_expr is equivalent to the chosen expression.
246 case Expr::ChooseExprClass:
247 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
248
249 // Extended vector element access is an lvalue unless there are duplicates
250 // in the shuffle expression.
251 case Expr::ExtVectorElementExprClass:
252 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
253 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
254
255 // Simply look at the actual default argument.
256 case Expr::CXXDefaultArgExprClass:
257 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
258
259 // Same idea for temporary binding.
260 case Expr::CXXBindTemporaryExprClass:
261 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
262
John McCall4765fa02010-12-06 08:20:24 +0000263 // And the cleanups guard.
264 case Expr::ExprWithCleanupsClass:
265 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redl2111c852010-06-28 15:09:07 +0000266
267 // Casts depend completely on the target type. All casts work the same.
268 case Expr::CStyleCastExprClass:
269 case Expr::CXXFunctionalCastExprClass:
270 case Expr::CXXStaticCastExprClass:
271 case Expr::CXXDynamicCastExprClass:
272 case Expr::CXXReinterpretCastExprClass:
273 case Expr::CXXConstCastExprClass:
274 // Only in C++ can casts be interesting at all.
275 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
276 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
277
John McCall56ca35d2011-02-17 10:25:35 +0000278 case Expr::BinaryConditionalOperatorClass: {
279 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
280 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
281 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
282 }
283
284 case Expr::ConditionalOperatorClass: {
Sebastian Redl2111c852010-06-28 15:09:07 +0000285 // Once again, only C++ is interesting.
286 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
John McCall56ca35d2011-02-17 10:25:35 +0000287 const ConditionalOperator *co = cast<ConditionalOperator>(E);
288 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
289 }
Sebastian Redl2111c852010-06-28 15:09:07 +0000290
291 // ObjC message sends are effectively function calls, if the target function
292 // is known.
293 case Expr::ObjCMessageExprClass:
294 if (const ObjCMethodDecl *Method =
295 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
296 return ClassifyUnnamed(Ctx, Method->getResultType());
297 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000298 return Cl::CL_PRValue;
299
Sebastian Redl2111c852010-06-28 15:09:07 +0000300 // Some C++ expressions are always class temporaries.
301 case Expr::CXXConstructExprClass:
302 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redl2111c852010-06-28 15:09:07 +0000303 return Cl::CL_ClassTemporary;
304
Douglas Gregor4178b572010-09-14 21:51:42 +0000305 case Expr::VAArgExprClass:
306 return ClassifyUnnamed(Ctx, E->getType());
307
308 case Expr::DesignatedInitExprClass:
309 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
310
311 case Expr::StmtExprClass: {
312 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
313 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregor226cbfc2010-09-15 01:37:48 +0000314 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redl2111c852010-06-28 15:09:07 +0000315 return Cl::CL_PRValue;
316 }
Douglas Gregor4178b572010-09-14 21:51:42 +0000317
318 case Expr::CXXUuidofExprClass:
Francois Pichetecea19f2010-12-17 02:00:06 +0000319 return Cl::CL_LValue;
Douglas Gregorbe230c32011-01-03 17:17:50 +0000320
321 case Expr::PackExpansionExprClass:
322 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Douglas Gregor4178b572010-09-14 21:51:42 +0000323 }
324
325 llvm_unreachable("unhandled expression kind in classification");
326 return Cl::CL_LValue;
Sebastian Redl2111c852010-06-28 15:09:07 +0000327}
328
329/// ClassifyDecl - Return the classification of an expression referencing the
330/// given declaration.
331static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
332 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
333 // function, variable, or data member and a prvalue otherwise.
334 // In C, functions are not lvalues.
335 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
336 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
337 // special-case this.
John McCall9c72c602010-08-27 09:08:28 +0000338
339 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
340 return Cl::CL_MemberFunction;
341
Sebastian Redl2111c852010-06-28 15:09:07 +0000342 bool islvalue;
343 if (const NonTypeTemplateParmDecl *NTTParm =
344 dyn_cast<NonTypeTemplateParmDecl>(D))
345 islvalue = NTTParm->getType()->isReferenceType();
346 else
347 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet87c2e122010-11-21 06:08:52 +0000348 isa<IndirectFieldDecl>(D) ||
Sebastian Redl2111c852010-06-28 15:09:07 +0000349 (Ctx.getLangOptions().CPlusPlus &&
350 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
351
352 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
353}
354
355/// ClassifyUnnamed - Return the classification of an expression yielding an
356/// unnamed value of the given type. This applies in particular to function
357/// calls and casts.
358static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
359 // In C, function calls are always rvalues.
360 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
361
362 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
363 // lvalue reference type or an rvalue reference to function type, an xvalue
364 // if the result type is an rvalue refernence to object type, and a prvalue
365 // otherwise.
366 if (T->isLValueReferenceType())
367 return Cl::CL_LValue;
368 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
369 if (!RV) // Could still be a class temporary, though.
370 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
371
372 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
373}
374
375static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
376 // Handle C first, it's easier.
377 if (!Ctx.getLangOptions().CPlusPlus) {
378 // C99 6.5.2.3p3
379 // For dot access, the expression is an lvalue if the first part is. For
380 // arrow access, it always is an lvalue.
381 if (E->isArrow())
382 return Cl::CL_LValue;
383 // ObjC property accesses are not lvalues, but get special treatment.
John McCallb418d742010-11-16 10:08:07 +0000384 Expr *Base = E->getBase()->IgnoreParens();
John McCall12f78a62010-12-02 01:19:52 +0000385 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redl2111c852010-06-28 15:09:07 +0000386 return Cl::CL_SubObjCPropertySetting;
387 return ClassifyInternal(Ctx, Base);
388 }
389
390 NamedDecl *Member = E->getMemberDecl();
391 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
392 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
393 // E1.E2 is an lvalue.
394 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
395 if (Value->getType()->isReferenceType())
396 return Cl::CL_LValue;
397
398 // Otherwise, one of the following rules applies.
399 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
400 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
401 return Cl::CL_LValue;
402
403 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
404 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
405 // otherwise, it is a prvalue.
406 if (isa<FieldDecl>(Member)) {
407 // *E1 is an lvalue
408 if (E->isArrow())
409 return Cl::CL_LValue;
John McCall09431682010-11-18 19:01:18 +0000410 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCall12f78a62010-12-02 01:19:52 +0000411 if (isa<ObjCPropertyRefExpr>(Base))
John McCall09431682010-11-18 19:01:18 +0000412 return Cl::CL_SubObjCPropertySetting;
Sebastian Redl2111c852010-06-28 15:09:07 +0000413 return ClassifyInternal(Ctx, E->getBase());
414 }
415
416 // -- If E2 is a [...] member function, [...]
417 // -- If it refers to a static member function [...], then E1.E2 is an
418 // lvalue; [...]
419 // -- Otherwise [...] E1.E2 is a prvalue.
420 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
421 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
422
423 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
424 // So is everything else we haven't handled yet.
425 return Cl::CL_PRValue;
426}
427
428static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
429 assert(Ctx.getLangOptions().CPlusPlus &&
430 "This is only relevant for C++.");
431 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCallf6a16482010-12-04 03:47:34 +0000432 // Except we override this for writes to ObjC properties.
Sebastian Redl2111c852010-06-28 15:09:07 +0000433 if (E->isAssignmentOp())
John McCallf6a16482010-12-04 03:47:34 +0000434 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
435 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redl2111c852010-06-28 15:09:07 +0000436
437 // C++ [expr.comma]p1: the result is of the same value category as its right
438 // operand, [...].
John McCall2de56d12010-08-25 11:45:40 +0000439 if (E->getOpcode() == BO_Comma)
Sebastian Redl2111c852010-06-28 15:09:07 +0000440 return ClassifyInternal(Ctx, E->getRHS());
441
442 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
443 // is a pointer to a data member is of the same value category as its first
444 // operand.
John McCall2de56d12010-08-25 11:45:40 +0000445 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redl2111c852010-06-28 15:09:07 +0000446 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
447 ClassifyInternal(Ctx, E->getLHS());
448
449 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
450 // second operand is a pointer to data member and a prvalue otherwise.
John McCall2de56d12010-08-25 11:45:40 +0000451 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redl2111c852010-06-28 15:09:07 +0000452 return E->getType()->isFunctionType() ?
453 Cl::CL_MemberFunction : Cl::CL_LValue;
454
455 // All other binary operations are prvalues.
456 return Cl::CL_PRValue;
457}
458
John McCall56ca35d2011-02-17 10:25:35 +0000459static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
460 const Expr *False) {
Sebastian Redl2111c852010-06-28 15:09:07 +0000461 assert(Ctx.getLangOptions().CPlusPlus &&
462 "This is only relevant for C++.");
463
Sebastian Redl2111c852010-06-28 15:09:07 +0000464 // C++ [expr.cond]p2
465 // If either the second or the third operand has type (cv) void, [...]
466 // the result [...] is a prvalue.
467 if (True->getType()->isVoidType() || False->getType()->isVoidType())
468 return Cl::CL_PRValue;
469
470 // Note that at this point, we have already performed all conversions
471 // according to [expr.cond]p3.
472 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
473 // same value category [...], the result is of that [...] value category.
474 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
475 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
476 RCl = ClassifyInternal(Ctx, False);
477 return LCl == RCl ? LCl : Cl::CL_PRValue;
478}
479
480static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
481 Cl::Kinds Kind, SourceLocation &Loc) {
482 // As a general rule, we only care about lvalues. But there are some rvalues
483 // for which we want to generate special results.
484 if (Kind == Cl::CL_PRValue) {
485 // For the sake of better diagnostics, we want to specifically recognize
486 // use of the GCC cast-as-lvalue extension.
John McCallf6a16482010-12-04 03:47:34 +0000487 if (const ExplicitCastExpr *CE =
488 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
489 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
490 Loc = CE->getExprLoc();
Sebastian Redl2111c852010-06-28 15:09:07 +0000491 return Cl::CM_LValueCast;
492 }
493 }
494 }
495 if (Kind != Cl::CL_LValue)
496 return Cl::CM_RValue;
497
498 // This is the lvalue case.
499 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
500 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
501 return Cl::CM_Function;
502
503 // You cannot assign to a variable outside a block from within the block if
504 // it is not marked __block, e.g.
505 // void takeclosure(void (^C)(void));
506 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
507 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
508 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
509 return Cl::CM_NotBlockQualified;
510 }
511
512 // Assignment to a property in ObjC is an implicit setter access. But a
513 // setter might not exist.
John McCall12f78a62010-12-02 01:19:52 +0000514 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
515 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redl2111c852010-06-28 15:09:07 +0000516 return Cl::CM_NoSetterProperty;
517 }
518
519 CanQualType CT = Ctx.getCanonicalType(E->getType());
520 // Const stuff is obviously not modifiable.
521 if (CT.isConstQualified())
522 return Cl::CM_ConstQualified;
523 // Arrays are not modifiable, only their elements are.
524 if (CT->isArrayType())
525 return Cl::CM_ArrayType;
526 // Incomplete types are not modifiable.
527 if (CT->isIncompleteType())
528 return Cl::CM_IncompleteType;
529
530 // Records with any const fields (recursively) are not modifiable.
531 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall01b2e4e2010-12-06 05:26:58 +0000532 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahanian4088ec02010-09-09 23:01:10 +0000533 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redl2111c852010-06-28 15:09:07 +0000534 "C++ struct assignment should be resolved by the "
535 "copy assignment operator.");
536 if (R->hasConstFields())
537 return Cl::CM_ConstQualified;
538 }
539
540 return Cl::CM_Modifiable;
541}
542
John McCall7eb0a9e2010-11-24 05:12:34 +0000543Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redl2111c852010-06-28 15:09:07 +0000544 Classification VC = Classify(Ctx);
545 switch (VC.getKind()) {
546 case Cl::CL_LValue: return LV_Valid;
547 case Cl::CL_XValue: return LV_InvalidExpression;
548 case Cl::CL_Function: return LV_NotObjectType;
549 case Cl::CL_Void: return LV_IncompleteVoidType;
550 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
551 case Cl::CL_MemberFunction: return LV_MemberFunction;
552 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
553 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
554 case Cl::CL_PRValue: return LV_InvalidExpression;
555 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000556 llvm_unreachable("Unhandled kind");
Sebastian Redl2111c852010-06-28 15:09:07 +0000557}
558
559Expr::isModifiableLvalueResult
560Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
561 SourceLocation dummy;
562 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
563 switch (VC.getKind()) {
564 case Cl::CL_LValue: break;
565 case Cl::CL_XValue: return MLV_InvalidExpression;
566 case Cl::CL_Function: return MLV_NotObjectType;
567 case Cl::CL_Void: return MLV_IncompleteVoidType;
568 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
569 case Cl::CL_MemberFunction: return MLV_MemberFunction;
570 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
571 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
572 case Cl::CL_PRValue:
573 return VC.getModifiable() == Cl::CM_LValueCast ?
574 MLV_LValueCast : MLV_InvalidExpression;
575 }
576 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
577 switch (VC.getModifiable()) {
Chandler Carruth0010bca2010-06-29 00:23:11 +0000578 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redl2111c852010-06-28 15:09:07 +0000579 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth0010bca2010-06-29 00:23:11 +0000580 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000581 case Cl::CM_Function: return MLV_NotObjectType;
582 case Cl::CM_LValueCast:
Chandler Carruth0010bca2010-06-29 00:23:11 +0000583 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redl2111c852010-06-28 15:09:07 +0000584 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
585 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
586 case Cl::CM_ConstQualified: return MLV_ConstQualified;
587 case Cl::CM_ArrayType: return MLV_ArrayType;
588 case Cl::CM_IncompleteType: return MLV_IncompleteType;
589 }
Chandler Carruth0010bca2010-06-29 00:23:11 +0000590 llvm_unreachable("Unhandled modifiable type");
Sebastian Redl2111c852010-06-28 15:09:07 +0000591}