blob: 76e98865ec712d603fa954ed6b57e69121cc5507 [file] [log] [blame]
Sebastian Redlf9463102010-06-28 15:09:07 +00001//===--- ExprClassification.cpp - Expression AST Node Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Expr::classify.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth8337ba62010-06-29 00:23:11 +000014#include "llvm/Support/ErrorHandling.h"
Sebastian Redlf9463102010-06-28 15:09:07 +000015#include "clang/AST/Expr.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/ExprObjC.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclTemplate.h"
22using namespace clang;
23
24typedef Expr::Classification Cl;
25
26static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
28static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);
29static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
30static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);
31static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
32 const ConditionalOperator *E);
33static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
34 Cl::Kinds Kind, SourceLocation &Loc);
35
John McCall8d69a212010-11-15 23:31:06 +000036static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
37 const Expr *E,
38 ExprValueKind Kind) {
39 switch (Kind) {
40 case VK_RValue:
41 return Lang.CPlusPlus && E->getType()->isRecordType() ?
42 Cl::CL_ClassTemporary : Cl::CL_PRValue;
43 case VK_LValue:
44 return Cl::CL_LValue;
45 case VK_XValue:
46 return Cl::CL_XValue;
47 }
48 llvm_unreachable("Invalid value category of implicit cast.");
49 return Cl::CL_PRValue;
50}
51
Sebastian Redlf9463102010-06-28 15:09:07 +000052Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
53 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
54
55 Cl::Kinds kind = ClassifyInternal(Ctx, this);
56 // C99 6.3.2.1: An lvalue is an expression with an object type or an
57 // incomplete type other than void.
58 if (!Ctx.getLangOptions().CPlusPlus) {
59 // Thus, no functions.
60 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
61 kind = Cl::CL_Function;
62 // No void either, but qualified void is OK because it is "other than void".
63 else if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
64 kind = Cl::CL_Void;
65 }
66
John McCall4bc41ae2010-11-18 19:01:18 +000067 // Enable this assertion for testing.
68 switch (kind) {
69 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
70 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
71 case Cl::CL_Function:
72 case Cl::CL_Void:
73 case Cl::CL_DuplicateVectorComponents:
74 case Cl::CL_MemberFunction:
75 case Cl::CL_SubObjCPropertySetting:
76 case Cl::CL_ClassTemporary:
77 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
78 }
John McCall4bc41ae2010-11-18 19:01:18 +000079
Sebastian Redlf9463102010-06-28 15:09:07 +000080 Cl::ModifiableType modifiable = Cl::CM_Untested;
81 if (Loc)
82 modifiable = IsModifiable(Ctx, this, kind, *Loc);
83 return Classification(kind, modifiable);
84}
85
86static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
87 // This function takes the first stab at classifying expressions.
88 const LangOptions &Lang = Ctx.getLangOptions();
89
90 switch (E->getStmtClass()) {
91 // First come the expressions that are always lvalues, unconditionally.
Douglas Gregor4e442502010-09-14 21:51:42 +000092 case Stmt::NoStmtClass:
93#define STMT(Kind, Base) case Expr::Kind##Class:
94#define EXPR(Kind, Base)
95#include "clang/AST/StmtNodes.inc"
96 llvm_unreachable("cannot classify a statement");
97 break;
Sebastian Redlf9463102010-06-28 15:09:07 +000098 case Expr::ObjCIsaExprClass:
99 // C++ [expr.prim.general]p1: A string literal is an lvalue.
100 case Expr::StringLiteralClass:
101 // @encode is equivalent to its string
102 case Expr::ObjCEncodeExprClass:
103 // __func__ and friends are too.
104 case Expr::PredefinedExprClass:
105 // Property references are lvalues
106 case Expr::ObjCPropertyRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000107 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
108 case Expr::CXXTypeidExprClass:
109 // Unresolved lookups get classified as lvalues.
110 // FIXME: Is this wise? Should they get their own kind?
111 case Expr::UnresolvedLookupExprClass:
112 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000113 case Expr::CXXDependentScopeMemberExprClass:
114 case Expr::CXXUnresolvedConstructExprClass:
115 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000116 // ObjC instance variables are lvalues
117 // FIXME: ObjC++0x might have different rules
118 case Expr::ObjCIvarRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000119 return Cl::CL_LValue;
Douglas Gregor4e442502010-09-14 21:51:42 +0000120 // C99 6.5.2.5p5 says that compound literals are lvalues.
121 // In C++, they're class temporaries.
122 case Expr::CompoundLiteralExprClass:
123 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
124 : Cl::CL_LValue;
125
126 // Expressions that are prvalues.
127 case Expr::CXXBoolLiteralExprClass:
128 case Expr::CXXPseudoDestructorExprClass:
129 case Expr::SizeOfAlignOfExprClass:
130 case Expr::CXXNewExprClass:
131 case Expr::CXXThisExprClass:
132 case Expr::CXXNullPtrLiteralExprClass:
133 case Expr::TypesCompatibleExprClass:
134 case Expr::ImaginaryLiteralClass:
135 case Expr::GNUNullExprClass:
136 case Expr::OffsetOfExprClass:
137 case Expr::CXXThrowExprClass:
138 case Expr::ShuffleVectorExprClass:
139 case Expr::IntegerLiteralClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000140 case Expr::CharacterLiteralClass:
141 case Expr::AddrLabelExprClass:
142 case Expr::CXXDeleteExprClass:
143 case Expr::ImplicitValueInitExprClass:
144 case Expr::BlockExprClass:
145 case Expr::FloatingLiteralClass:
146 case Expr::CXXNoexceptExprClass:
147 case Expr::CXXScalarValueInitExprClass:
148 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +0000149 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000150 case Expr::ObjCSelectorExprClass:
151 case Expr::ObjCProtocolExprClass:
152 case Expr::ObjCStringLiteralClass:
153 case Expr::ParenListExprClass:
154 case Expr::InitListExprClass:
155 return Cl::CL_PRValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000156
157 // Next come the complicated cases.
158
159 // C++ [expr.sub]p1: The result is an lvalue of type "T".
160 // However, subscripting vector types is more like member access.
161 case Expr::ArraySubscriptExprClass:
162 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
163 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
164 return Cl::CL_LValue;
165
166 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
167 // function or variable and a prvalue otherwise.
168 case Expr::DeclRefExprClass:
169 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
170 // We deal with names referenced from blocks the same way.
171 case Expr::BlockDeclRefExprClass:
172 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
173
174 // Member access is complex.
175 case Expr::MemberExprClass:
176 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
177
178 case Expr::UnaryOperatorClass:
179 switch (cast<UnaryOperator>(E)->getOpcode()) {
180 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
181 // [...] the result is an lvalue referring to the object or function
182 // to which the expression points.
John McCalle3027922010-08-25 11:45:40 +0000183 case UO_Deref:
Sebastian Redlf9463102010-06-28 15:09:07 +0000184 return Cl::CL_LValue;
185
186 // GNU extensions, simply look through them.
John McCalle3027922010-08-25 11:45:40 +0000187 case UO_Extension:
Sebastian Redlf9463102010-06-28 15:09:07 +0000188 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
189
John McCall07bb1962010-11-16 10:08:07 +0000190 // Treat _Real and _Imag basically as if they were member
191 // expressions: l-value only if the operand is a true l-value.
192 case UO_Real:
193 case UO_Imag: {
194 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
195 Cl::Kinds K = ClassifyInternal(Ctx, Op);
196 if (K != Cl::CL_LValue) return K;
197
John McCallb7bd14f2010-12-02 01:19:52 +0000198 if (isa<ObjCPropertyRefExpr>(Op))
John McCall07bb1962010-11-16 10:08:07 +0000199 return Cl::CL_SubObjCPropertySetting;
200 return Cl::CL_LValue;
201 }
202
Sebastian Redlf9463102010-06-28 15:09:07 +0000203 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
204 // lvalue, [...]
205 // Not so in C.
John McCalle3027922010-08-25 11:45:40 +0000206 case UO_PreInc:
207 case UO_PreDec:
Sebastian Redlf9463102010-06-28 15:09:07 +0000208 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
209
210 default:
211 return Cl::CL_PRValue;
212 }
213
John McCall8d69a212010-11-15 23:31:06 +0000214 case Expr::OpaqueValueExprClass:
215 return ClassifyExprValueKind(Lang, E,
216 cast<OpaqueValueExpr>(E)->getValueKind());
217
Sebastian Redlf9463102010-06-28 15:09:07 +0000218 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
219 // only specifically record class temporaries.
220 case Expr::ImplicitCastExprClass:
John McCall8d69a212010-11-15 23:31:06 +0000221 return ClassifyExprValueKind(Lang, E,
222 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redlf9463102010-06-28 15:09:07 +0000223
224 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
225 // whether the expression is an lvalue.
226 case Expr::ParenExprClass:
227 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
228
229 case Expr::BinaryOperatorClass:
230 case Expr::CompoundAssignOperatorClass:
231 // C doesn't have any binary expressions that are lvalues.
232 if (Lang.CPlusPlus)
233 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
234 return Cl::CL_PRValue;
235
236 case Expr::CallExprClass:
237 case Expr::CXXOperatorCallExprClass:
238 case Expr::CXXMemberCallExprClass:
239 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
240
241 // __builtin_choose_expr is equivalent to the chosen expression.
242 case Expr::ChooseExprClass:
243 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
244
245 // Extended vector element access is an lvalue unless there are duplicates
246 // in the shuffle expression.
247 case Expr::ExtVectorElementExprClass:
248 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
249 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
250
251 // Simply look at the actual default argument.
252 case Expr::CXXDefaultArgExprClass:
253 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
254
255 // Same idea for temporary binding.
256 case Expr::CXXBindTemporaryExprClass:
257 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
258
John McCall5d413782010-12-06 08:20:24 +0000259 // And the cleanups guard.
260 case Expr::ExprWithCleanupsClass:
261 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redlf9463102010-06-28 15:09:07 +0000262
263 // Casts depend completely on the target type. All casts work the same.
264 case Expr::CStyleCastExprClass:
265 case Expr::CXXFunctionalCastExprClass:
266 case Expr::CXXStaticCastExprClass:
267 case Expr::CXXDynamicCastExprClass:
268 case Expr::CXXReinterpretCastExprClass:
269 case Expr::CXXConstCastExprClass:
270 // Only in C++ can casts be interesting at all.
271 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
272 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
273
274 case Expr::ConditionalOperatorClass:
275 // Once again, only C++ is interesting.
276 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
277 return ClassifyConditional(Ctx, cast<ConditionalOperator>(E));
278
279 // ObjC message sends are effectively function calls, if the target function
280 // is known.
281 case Expr::ObjCMessageExprClass:
282 if (const ObjCMethodDecl *Method =
283 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
284 return ClassifyUnnamed(Ctx, Method->getResultType());
285 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000286 return Cl::CL_PRValue;
287
Sebastian Redlf9463102010-06-28 15:09:07 +0000288 // Some C++ expressions are always class temporaries.
289 case Expr::CXXConstructExprClass:
290 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000291 return Cl::CL_ClassTemporary;
292
Douglas Gregor4e442502010-09-14 21:51:42 +0000293 case Expr::VAArgExprClass:
294 return ClassifyUnnamed(Ctx, E->getType());
295
296 case Expr::DesignatedInitExprClass:
297 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
298
299 case Expr::StmtExprClass: {
300 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
301 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregore572b062010-09-15 01:37:48 +0000302 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redlf9463102010-06-28 15:09:07 +0000303 return Cl::CL_PRValue;
304 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000305
306 case Expr::CXXUuidofExprClass:
John McCall7decc9e2010-11-18 06:31:45 +0000307 return Cl::CL_PRValue;
Douglas Gregor4e442502010-09-14 21:51:42 +0000308 }
309
310 llvm_unreachable("unhandled expression kind in classification");
311 return Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000312}
313
314/// ClassifyDecl - Return the classification of an expression referencing the
315/// given declaration.
316static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
317 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
318 // function, variable, or data member and a prvalue otherwise.
319 // In C, functions are not lvalues.
320 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
321 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
322 // special-case this.
John McCall8d08b9b2010-08-27 09:08:28 +0000323
324 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
325 return Cl::CL_MemberFunction;
326
Sebastian Redlf9463102010-06-28 15:09:07 +0000327 bool islvalue;
328 if (const NonTypeTemplateParmDecl *NTTParm =
329 dyn_cast<NonTypeTemplateParmDecl>(D))
330 islvalue = NTTParm->getType()->isReferenceType();
331 else
332 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet783dd6e2010-11-21 06:08:52 +0000333 isa<IndirectFieldDecl>(D) ||
Sebastian Redlf9463102010-06-28 15:09:07 +0000334 (Ctx.getLangOptions().CPlusPlus &&
335 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
336
337 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
338}
339
340/// ClassifyUnnamed - Return the classification of an expression yielding an
341/// unnamed value of the given type. This applies in particular to function
342/// calls and casts.
343static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
344 // In C, function calls are always rvalues.
345 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
346
347 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
348 // lvalue reference type or an rvalue reference to function type, an xvalue
349 // if the result type is an rvalue refernence to object type, and a prvalue
350 // otherwise.
351 if (T->isLValueReferenceType())
352 return Cl::CL_LValue;
353 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
354 if (!RV) // Could still be a class temporary, though.
355 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
356
357 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
358}
359
360static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
361 // Handle C first, it's easier.
362 if (!Ctx.getLangOptions().CPlusPlus) {
363 // C99 6.5.2.3p3
364 // For dot access, the expression is an lvalue if the first part is. For
365 // arrow access, it always is an lvalue.
366 if (E->isArrow())
367 return Cl::CL_LValue;
368 // ObjC property accesses are not lvalues, but get special treatment.
John McCall07bb1962010-11-16 10:08:07 +0000369 Expr *Base = E->getBase()->IgnoreParens();
John McCallb7bd14f2010-12-02 01:19:52 +0000370 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redlf9463102010-06-28 15:09:07 +0000371 return Cl::CL_SubObjCPropertySetting;
372 return ClassifyInternal(Ctx, Base);
373 }
374
375 NamedDecl *Member = E->getMemberDecl();
376 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
377 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
378 // E1.E2 is an lvalue.
379 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
380 if (Value->getType()->isReferenceType())
381 return Cl::CL_LValue;
382
383 // Otherwise, one of the following rules applies.
384 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
385 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
386 return Cl::CL_LValue;
387
388 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
389 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
390 // otherwise, it is a prvalue.
391 if (isa<FieldDecl>(Member)) {
392 // *E1 is an lvalue
393 if (E->isArrow())
394 return Cl::CL_LValue;
John McCall4bc41ae2010-11-18 19:01:18 +0000395 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCallb7bd14f2010-12-02 01:19:52 +0000396 if (isa<ObjCPropertyRefExpr>(Base))
John McCall4bc41ae2010-11-18 19:01:18 +0000397 return Cl::CL_SubObjCPropertySetting;
Sebastian Redlf9463102010-06-28 15:09:07 +0000398 return ClassifyInternal(Ctx, E->getBase());
399 }
400
401 // -- If E2 is a [...] member function, [...]
402 // -- If it refers to a static member function [...], then E1.E2 is an
403 // lvalue; [...]
404 // -- Otherwise [...] E1.E2 is a prvalue.
405 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
406 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
407
408 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
409 // So is everything else we haven't handled yet.
410 return Cl::CL_PRValue;
411}
412
413static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
414 assert(Ctx.getLangOptions().CPlusPlus &&
415 "This is only relevant for C++.");
416 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCall34376a62010-12-04 03:47:34 +0000417 // Except we override this for writes to ObjC properties.
Sebastian Redlf9463102010-06-28 15:09:07 +0000418 if (E->isAssignmentOp())
John McCall34376a62010-12-04 03:47:34 +0000419 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
420 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redlf9463102010-06-28 15:09:07 +0000421
422 // C++ [expr.comma]p1: the result is of the same value category as its right
423 // operand, [...].
John McCalle3027922010-08-25 11:45:40 +0000424 if (E->getOpcode() == BO_Comma)
Sebastian Redlf9463102010-06-28 15:09:07 +0000425 return ClassifyInternal(Ctx, E->getRHS());
426
427 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
428 // is a pointer to a data member is of the same value category as its first
429 // operand.
John McCalle3027922010-08-25 11:45:40 +0000430 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redlf9463102010-06-28 15:09:07 +0000431 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
432 ClassifyInternal(Ctx, E->getLHS());
433
434 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
435 // second operand is a pointer to data member and a prvalue otherwise.
John McCalle3027922010-08-25 11:45:40 +0000436 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redlf9463102010-06-28 15:09:07 +0000437 return E->getType()->isFunctionType() ?
438 Cl::CL_MemberFunction : Cl::CL_LValue;
439
440 // All other binary operations are prvalues.
441 return Cl::CL_PRValue;
442}
443
444static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
445 const ConditionalOperator *E) {
446 assert(Ctx.getLangOptions().CPlusPlus &&
447 "This is only relevant for C++.");
448
449 Expr *True = E->getTrueExpr();
450 Expr *False = E->getFalseExpr();
451 // C++ [expr.cond]p2
452 // If either the second or the third operand has type (cv) void, [...]
453 // the result [...] is a prvalue.
454 if (True->getType()->isVoidType() || False->getType()->isVoidType())
455 return Cl::CL_PRValue;
456
457 // Note that at this point, we have already performed all conversions
458 // according to [expr.cond]p3.
459 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
460 // same value category [...], the result is of that [...] value category.
461 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
462 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
463 RCl = ClassifyInternal(Ctx, False);
464 return LCl == RCl ? LCl : Cl::CL_PRValue;
465}
466
467static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
468 Cl::Kinds Kind, SourceLocation &Loc) {
469 // As a general rule, we only care about lvalues. But there are some rvalues
470 // for which we want to generate special results.
471 if (Kind == Cl::CL_PRValue) {
472 // For the sake of better diagnostics, we want to specifically recognize
473 // use of the GCC cast-as-lvalue extension.
John McCall34376a62010-12-04 03:47:34 +0000474 if (const ExplicitCastExpr *CE =
475 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
476 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
477 Loc = CE->getExprLoc();
Sebastian Redlf9463102010-06-28 15:09:07 +0000478 return Cl::CM_LValueCast;
479 }
480 }
481 }
482 if (Kind != Cl::CL_LValue)
483 return Cl::CM_RValue;
484
485 // This is the lvalue case.
486 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
487 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
488 return Cl::CM_Function;
489
490 // You cannot assign to a variable outside a block from within the block if
491 // it is not marked __block, e.g.
492 // void takeclosure(void (^C)(void));
493 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
494 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
495 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
496 return Cl::CM_NotBlockQualified;
497 }
498
499 // Assignment to a property in ObjC is an implicit setter access. But a
500 // setter might not exist.
John McCallb7bd14f2010-12-02 01:19:52 +0000501 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
502 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redlf9463102010-06-28 15:09:07 +0000503 return Cl::CM_NoSetterProperty;
504 }
505
506 CanQualType CT = Ctx.getCanonicalType(E->getType());
507 // Const stuff is obviously not modifiable.
508 if (CT.isConstQualified())
509 return Cl::CM_ConstQualified;
510 // Arrays are not modifiable, only their elements are.
511 if (CT->isArrayType())
512 return Cl::CM_ArrayType;
513 // Incomplete types are not modifiable.
514 if (CT->isIncompleteType())
515 return Cl::CM_IncompleteType;
516
517 // Records with any const fields (recursively) are not modifiable.
518 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall622114c2010-12-06 05:26:58 +0000519 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahaniane89d03f2010-09-09 23:01:10 +0000520 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redlf9463102010-06-28 15:09:07 +0000521 "C++ struct assignment should be resolved by the "
522 "copy assignment operator.");
523 if (R->hasConstFields())
524 return Cl::CM_ConstQualified;
525 }
526
527 return Cl::CM_Modifiable;
528}
529
John McCall086a4642010-11-24 05:12:34 +0000530Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redlf9463102010-06-28 15:09:07 +0000531 Classification VC = Classify(Ctx);
532 switch (VC.getKind()) {
533 case Cl::CL_LValue: return LV_Valid;
534 case Cl::CL_XValue: return LV_InvalidExpression;
535 case Cl::CL_Function: return LV_NotObjectType;
536 case Cl::CL_Void: return LV_IncompleteVoidType;
537 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
538 case Cl::CL_MemberFunction: return LV_MemberFunction;
539 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
540 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
541 case Cl::CL_PRValue: return LV_InvalidExpression;
542 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000543 llvm_unreachable("Unhandled kind");
Sebastian Redlf9463102010-06-28 15:09:07 +0000544}
545
546Expr::isModifiableLvalueResult
547Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
548 SourceLocation dummy;
549 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
550 switch (VC.getKind()) {
551 case Cl::CL_LValue: break;
552 case Cl::CL_XValue: return MLV_InvalidExpression;
553 case Cl::CL_Function: return MLV_NotObjectType;
554 case Cl::CL_Void: return MLV_IncompleteVoidType;
555 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
556 case Cl::CL_MemberFunction: return MLV_MemberFunction;
557 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
558 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
559 case Cl::CL_PRValue:
560 return VC.getModifiable() == Cl::CM_LValueCast ?
561 MLV_LValueCast : MLV_InvalidExpression;
562 }
563 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
564 switch (VC.getModifiable()) {
Chandler Carruth8337ba62010-06-29 00:23:11 +0000565 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redlf9463102010-06-28 15:09:07 +0000566 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth8337ba62010-06-29 00:23:11 +0000567 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000568 case Cl::CM_Function: return MLV_NotObjectType;
569 case Cl::CM_LValueCast:
Chandler Carruth8337ba62010-06-29 00:23:11 +0000570 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000571 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
572 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
573 case Cl::CM_ConstQualified: return MLV_ConstQualified;
574 case Cl::CM_ArrayType: return MLV_ArrayType;
575 case Cl::CM_IncompleteType: return MLV_IncompleteType;
576 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000577 llvm_unreachable("Unhandled modifiable type");
Sebastian Redlf9463102010-06-28 15:09:07 +0000578}