blob: ba3b88b46524bc3eec90462e5cd02e316ac7daf7 [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:
John McCallbd066782011-02-09 08:16:59 +000093#define ABSTRACT_STMT(Kind)
Douglas Gregor4e442502010-09-14 21:51:42 +000094#define STMT(Kind, Base) case Expr::Kind##Class:
95#define EXPR(Kind, Base)
96#include "clang/AST/StmtNodes.inc"
97 llvm_unreachable("cannot classify a statement");
98 break;
Sebastian Redlf9463102010-06-28 15:09:07 +000099 case Expr::ObjCIsaExprClass:
100 // C++ [expr.prim.general]p1: A string literal is an lvalue.
101 case Expr::StringLiteralClass:
102 // @encode is equivalent to its string
103 case Expr::ObjCEncodeExprClass:
104 // __func__ and friends are too.
105 case Expr::PredefinedExprClass:
106 // Property references are lvalues
107 case Expr::ObjCPropertyRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000108 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
109 case Expr::CXXTypeidExprClass:
110 // Unresolved lookups get classified as lvalues.
111 // FIXME: Is this wise? Should they get their own kind?
112 case Expr::UnresolvedLookupExprClass:
113 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000114 case Expr::CXXDependentScopeMemberExprClass:
115 case Expr::CXXUnresolvedConstructExprClass:
116 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000117 // ObjC instance variables are lvalues
118 // FIXME: ObjC++0x might have different rules
119 case Expr::ObjCIvarRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000120 return Cl::CL_LValue;
Douglas Gregor4e442502010-09-14 21:51:42 +0000121 // C99 6.5.2.5p5 says that compound literals are lvalues.
122 // In C++, they're class temporaries.
123 case Expr::CompoundLiteralExprClass:
124 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
125 : Cl::CL_LValue;
126
127 // Expressions that are prvalues.
128 case Expr::CXXBoolLiteralExprClass:
129 case Expr::CXXPseudoDestructorExprClass:
130 case Expr::SizeOfAlignOfExprClass:
131 case Expr::CXXNewExprClass:
132 case Expr::CXXThisExprClass:
133 case Expr::CXXNullPtrLiteralExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000134 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:
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000155 case Expr::SizeOfPackExprClass:
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000156 case Expr::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000157 return Cl::CL_PRValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000158
159 // Next come the complicated cases.
160
161 // C++ [expr.sub]p1: The result is an lvalue of type "T".
162 // However, subscripting vector types is more like member access.
163 case Expr::ArraySubscriptExprClass:
164 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
165 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
166 return Cl::CL_LValue;
167
168 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
169 // function or variable and a prvalue otherwise.
170 case Expr::DeclRefExprClass:
171 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
172 // We deal with names referenced from blocks the same way.
173 case Expr::BlockDeclRefExprClass:
174 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
175
176 // Member access is complex.
177 case Expr::MemberExprClass:
178 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
179
180 case Expr::UnaryOperatorClass:
181 switch (cast<UnaryOperator>(E)->getOpcode()) {
182 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
183 // [...] the result is an lvalue referring to the object or function
184 // to which the expression points.
John McCalle3027922010-08-25 11:45:40 +0000185 case UO_Deref:
Sebastian Redlf9463102010-06-28 15:09:07 +0000186 return Cl::CL_LValue;
187
188 // GNU extensions, simply look through them.
John McCalle3027922010-08-25 11:45:40 +0000189 case UO_Extension:
Sebastian Redlf9463102010-06-28 15:09:07 +0000190 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
191
John McCall07bb1962010-11-16 10:08:07 +0000192 // Treat _Real and _Imag basically as if they were member
193 // expressions: l-value only if the operand is a true l-value.
194 case UO_Real:
195 case UO_Imag: {
196 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
197 Cl::Kinds K = ClassifyInternal(Ctx, Op);
198 if (K != Cl::CL_LValue) return K;
199
John McCallb7bd14f2010-12-02 01:19:52 +0000200 if (isa<ObjCPropertyRefExpr>(Op))
John McCall07bb1962010-11-16 10:08:07 +0000201 return Cl::CL_SubObjCPropertySetting;
202 return Cl::CL_LValue;
203 }
204
Sebastian Redlf9463102010-06-28 15:09:07 +0000205 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
206 // lvalue, [...]
207 // Not so in C.
John McCalle3027922010-08-25 11:45:40 +0000208 case UO_PreInc:
209 case UO_PreDec:
Sebastian Redlf9463102010-06-28 15:09:07 +0000210 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
211
212 default:
213 return Cl::CL_PRValue;
214 }
215
John McCall8d69a212010-11-15 23:31:06 +0000216 case Expr::OpaqueValueExprClass:
217 return ClassifyExprValueKind(Lang, E,
218 cast<OpaqueValueExpr>(E)->getValueKind());
219
Sebastian Redlf9463102010-06-28 15:09:07 +0000220 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
221 // only specifically record class temporaries.
222 case Expr::ImplicitCastExprClass:
John McCall8d69a212010-11-15 23:31:06 +0000223 return ClassifyExprValueKind(Lang, E,
224 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redlf9463102010-06-28 15:09:07 +0000225
226 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
227 // whether the expression is an lvalue.
228 case Expr::ParenExprClass:
229 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
230
231 case Expr::BinaryOperatorClass:
232 case Expr::CompoundAssignOperatorClass:
233 // C doesn't have any binary expressions that are lvalues.
234 if (Lang.CPlusPlus)
235 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
236 return Cl::CL_PRValue;
237
238 case Expr::CallExprClass:
239 case Expr::CXXOperatorCallExprClass:
240 case Expr::CXXMemberCallExprClass:
241 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
242
243 // __builtin_choose_expr is equivalent to the chosen expression.
244 case Expr::ChooseExprClass:
245 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
246
247 // Extended vector element access is an lvalue unless there are duplicates
248 // in the shuffle expression.
249 case Expr::ExtVectorElementExprClass:
250 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
251 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
252
253 // Simply look at the actual default argument.
254 case Expr::CXXDefaultArgExprClass:
255 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
256
257 // Same idea for temporary binding.
258 case Expr::CXXBindTemporaryExprClass:
259 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
260
John McCall5d413782010-12-06 08:20:24 +0000261 // And the cleanups guard.
262 case Expr::ExprWithCleanupsClass:
263 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redlf9463102010-06-28 15:09:07 +0000264
265 // Casts depend completely on the target type. All casts work the same.
266 case Expr::CStyleCastExprClass:
267 case Expr::CXXFunctionalCastExprClass:
268 case Expr::CXXStaticCastExprClass:
269 case Expr::CXXDynamicCastExprClass:
270 case Expr::CXXReinterpretCastExprClass:
271 case Expr::CXXConstCastExprClass:
272 // Only in C++ can casts be interesting at all.
273 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
274 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
275
276 case Expr::ConditionalOperatorClass:
277 // Once again, only C++ is interesting.
278 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
279 return ClassifyConditional(Ctx, cast<ConditionalOperator>(E));
280
281 // ObjC message sends are effectively function calls, if the target function
282 // is known.
283 case Expr::ObjCMessageExprClass:
284 if (const ObjCMethodDecl *Method =
285 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
286 return ClassifyUnnamed(Ctx, Method->getResultType());
287 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000288 return Cl::CL_PRValue;
289
Sebastian Redlf9463102010-06-28 15:09:07 +0000290 // Some C++ expressions are always class temporaries.
291 case Expr::CXXConstructExprClass:
292 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000293 return Cl::CL_ClassTemporary;
294
Douglas Gregor4e442502010-09-14 21:51:42 +0000295 case Expr::VAArgExprClass:
296 return ClassifyUnnamed(Ctx, E->getType());
297
298 case Expr::DesignatedInitExprClass:
299 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
300
301 case Expr::StmtExprClass: {
302 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
303 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregore572b062010-09-15 01:37:48 +0000304 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redlf9463102010-06-28 15:09:07 +0000305 return Cl::CL_PRValue;
306 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000307
308 case Expr::CXXUuidofExprClass:
Francois Pichet4f64c5a2010-12-17 02:00:06 +0000309 return Cl::CL_LValue;
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000310
311 case Expr::PackExpansionExprClass:
312 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Douglas Gregor4e442502010-09-14 21:51:42 +0000313 }
314
315 llvm_unreachable("unhandled expression kind in classification");
316 return Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000317}
318
319/// ClassifyDecl - Return the classification of an expression referencing the
320/// given declaration.
321static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
322 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
323 // function, variable, or data member and a prvalue otherwise.
324 // In C, functions are not lvalues.
325 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
326 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
327 // special-case this.
John McCall8d08b9b2010-08-27 09:08:28 +0000328
329 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
330 return Cl::CL_MemberFunction;
331
Sebastian Redlf9463102010-06-28 15:09:07 +0000332 bool islvalue;
333 if (const NonTypeTemplateParmDecl *NTTParm =
334 dyn_cast<NonTypeTemplateParmDecl>(D))
335 islvalue = NTTParm->getType()->isReferenceType();
336 else
337 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet783dd6e2010-11-21 06:08:52 +0000338 isa<IndirectFieldDecl>(D) ||
Sebastian Redlf9463102010-06-28 15:09:07 +0000339 (Ctx.getLangOptions().CPlusPlus &&
340 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
341
342 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
343}
344
345/// ClassifyUnnamed - Return the classification of an expression yielding an
346/// unnamed value of the given type. This applies in particular to function
347/// calls and casts.
348static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
349 // In C, function calls are always rvalues.
350 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
351
352 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
353 // lvalue reference type or an rvalue reference to function type, an xvalue
354 // if the result type is an rvalue refernence to object type, and a prvalue
355 // otherwise.
356 if (T->isLValueReferenceType())
357 return Cl::CL_LValue;
358 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
359 if (!RV) // Could still be a class temporary, though.
360 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
361
362 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
363}
364
365static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
366 // Handle C first, it's easier.
367 if (!Ctx.getLangOptions().CPlusPlus) {
368 // C99 6.5.2.3p3
369 // For dot access, the expression is an lvalue if the first part is. For
370 // arrow access, it always is an lvalue.
371 if (E->isArrow())
372 return Cl::CL_LValue;
373 // ObjC property accesses are not lvalues, but get special treatment.
John McCall07bb1962010-11-16 10:08:07 +0000374 Expr *Base = E->getBase()->IgnoreParens();
John McCallb7bd14f2010-12-02 01:19:52 +0000375 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redlf9463102010-06-28 15:09:07 +0000376 return Cl::CL_SubObjCPropertySetting;
377 return ClassifyInternal(Ctx, Base);
378 }
379
380 NamedDecl *Member = E->getMemberDecl();
381 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
382 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
383 // E1.E2 is an lvalue.
384 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
385 if (Value->getType()->isReferenceType())
386 return Cl::CL_LValue;
387
388 // Otherwise, one of the following rules applies.
389 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
390 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
391 return Cl::CL_LValue;
392
393 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
394 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
395 // otherwise, it is a prvalue.
396 if (isa<FieldDecl>(Member)) {
397 // *E1 is an lvalue
398 if (E->isArrow())
399 return Cl::CL_LValue;
John McCall4bc41ae2010-11-18 19:01:18 +0000400 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCallb7bd14f2010-12-02 01:19:52 +0000401 if (isa<ObjCPropertyRefExpr>(Base))
John McCall4bc41ae2010-11-18 19:01:18 +0000402 return Cl::CL_SubObjCPropertySetting;
Sebastian Redlf9463102010-06-28 15:09:07 +0000403 return ClassifyInternal(Ctx, E->getBase());
404 }
405
406 // -- If E2 is a [...] member function, [...]
407 // -- If it refers to a static member function [...], then E1.E2 is an
408 // lvalue; [...]
409 // -- Otherwise [...] E1.E2 is a prvalue.
410 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
411 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
412
413 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
414 // So is everything else we haven't handled yet.
415 return Cl::CL_PRValue;
416}
417
418static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
419 assert(Ctx.getLangOptions().CPlusPlus &&
420 "This is only relevant for C++.");
421 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCall34376a62010-12-04 03:47:34 +0000422 // Except we override this for writes to ObjC properties.
Sebastian Redlf9463102010-06-28 15:09:07 +0000423 if (E->isAssignmentOp())
John McCall34376a62010-12-04 03:47:34 +0000424 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
425 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redlf9463102010-06-28 15:09:07 +0000426
427 // C++ [expr.comma]p1: the result is of the same value category as its right
428 // operand, [...].
John McCalle3027922010-08-25 11:45:40 +0000429 if (E->getOpcode() == BO_Comma)
Sebastian Redlf9463102010-06-28 15:09:07 +0000430 return ClassifyInternal(Ctx, E->getRHS());
431
432 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
433 // is a pointer to a data member is of the same value category as its first
434 // operand.
John McCalle3027922010-08-25 11:45:40 +0000435 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redlf9463102010-06-28 15:09:07 +0000436 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
437 ClassifyInternal(Ctx, E->getLHS());
438
439 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
440 // second operand is a pointer to data member and a prvalue otherwise.
John McCalle3027922010-08-25 11:45:40 +0000441 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redlf9463102010-06-28 15:09:07 +0000442 return E->getType()->isFunctionType() ?
443 Cl::CL_MemberFunction : Cl::CL_LValue;
444
445 // All other binary operations are prvalues.
446 return Cl::CL_PRValue;
447}
448
449static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
450 const ConditionalOperator *E) {
451 assert(Ctx.getLangOptions().CPlusPlus &&
452 "This is only relevant for C++.");
453
454 Expr *True = E->getTrueExpr();
455 Expr *False = E->getFalseExpr();
456 // C++ [expr.cond]p2
457 // If either the second or the third operand has type (cv) void, [...]
458 // the result [...] is a prvalue.
459 if (True->getType()->isVoidType() || False->getType()->isVoidType())
460 return Cl::CL_PRValue;
461
462 // Note that at this point, we have already performed all conversions
463 // according to [expr.cond]p3.
464 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
465 // same value category [...], the result is of that [...] value category.
466 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
467 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
468 RCl = ClassifyInternal(Ctx, False);
469 return LCl == RCl ? LCl : Cl::CL_PRValue;
470}
471
472static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
473 Cl::Kinds Kind, SourceLocation &Loc) {
474 // As a general rule, we only care about lvalues. But there are some rvalues
475 // for which we want to generate special results.
476 if (Kind == Cl::CL_PRValue) {
477 // For the sake of better diagnostics, we want to specifically recognize
478 // use of the GCC cast-as-lvalue extension.
John McCall34376a62010-12-04 03:47:34 +0000479 if (const ExplicitCastExpr *CE =
480 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
481 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
482 Loc = CE->getExprLoc();
Sebastian Redlf9463102010-06-28 15:09:07 +0000483 return Cl::CM_LValueCast;
484 }
485 }
486 }
487 if (Kind != Cl::CL_LValue)
488 return Cl::CM_RValue;
489
490 // This is the lvalue case.
491 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
492 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
493 return Cl::CM_Function;
494
495 // You cannot assign to a variable outside a block from within the block if
496 // it is not marked __block, e.g.
497 // void takeclosure(void (^C)(void));
498 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
499 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
500 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
501 return Cl::CM_NotBlockQualified;
502 }
503
504 // Assignment to a property in ObjC is an implicit setter access. But a
505 // setter might not exist.
John McCallb7bd14f2010-12-02 01:19:52 +0000506 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
507 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redlf9463102010-06-28 15:09:07 +0000508 return Cl::CM_NoSetterProperty;
509 }
510
511 CanQualType CT = Ctx.getCanonicalType(E->getType());
512 // Const stuff is obviously not modifiable.
513 if (CT.isConstQualified())
514 return Cl::CM_ConstQualified;
515 // Arrays are not modifiable, only their elements are.
516 if (CT->isArrayType())
517 return Cl::CM_ArrayType;
518 // Incomplete types are not modifiable.
519 if (CT->isIncompleteType())
520 return Cl::CM_IncompleteType;
521
522 // Records with any const fields (recursively) are not modifiable.
523 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall622114c2010-12-06 05:26:58 +0000524 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahaniane89d03f2010-09-09 23:01:10 +0000525 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redlf9463102010-06-28 15:09:07 +0000526 "C++ struct assignment should be resolved by the "
527 "copy assignment operator.");
528 if (R->hasConstFields())
529 return Cl::CM_ConstQualified;
530 }
531
532 return Cl::CM_Modifiable;
533}
534
John McCall086a4642010-11-24 05:12:34 +0000535Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redlf9463102010-06-28 15:09:07 +0000536 Classification VC = Classify(Ctx);
537 switch (VC.getKind()) {
538 case Cl::CL_LValue: return LV_Valid;
539 case Cl::CL_XValue: return LV_InvalidExpression;
540 case Cl::CL_Function: return LV_NotObjectType;
541 case Cl::CL_Void: return LV_IncompleteVoidType;
542 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
543 case Cl::CL_MemberFunction: return LV_MemberFunction;
544 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
545 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
546 case Cl::CL_PRValue: return LV_InvalidExpression;
547 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000548 llvm_unreachable("Unhandled kind");
Sebastian Redlf9463102010-06-28 15:09:07 +0000549}
550
551Expr::isModifiableLvalueResult
552Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
553 SourceLocation dummy;
554 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
555 switch (VC.getKind()) {
556 case Cl::CL_LValue: break;
557 case Cl::CL_XValue: return MLV_InvalidExpression;
558 case Cl::CL_Function: return MLV_NotObjectType;
559 case Cl::CL_Void: return MLV_IncompleteVoidType;
560 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
561 case Cl::CL_MemberFunction: return MLV_MemberFunction;
562 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
563 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
564 case Cl::CL_PRValue:
565 return VC.getModifiable() == Cl::CM_LValueCast ?
566 MLV_LValueCast : MLV_InvalidExpression;
567 }
568 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
569 switch (VC.getModifiable()) {
Chandler Carruth8337ba62010-06-29 00:23:11 +0000570 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redlf9463102010-06-28 15:09:07 +0000571 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth8337ba62010-06-29 00:23:11 +0000572 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000573 case Cl::CM_Function: return MLV_NotObjectType;
574 case Cl::CM_LValueCast:
Chandler Carruth8337ba62010-06-29 00:23:11 +0000575 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000576 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
577 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
578 case Cl::CM_ConstQualified: return MLV_ConstQualified;
579 case Cl::CM_ArrayType: return MLV_ArrayType;
580 case Cl::CM_IncompleteType: return MLV_IncompleteType;
581 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000582 llvm_unreachable("Unhandled modifiable type");
Sebastian Redlf9463102010-06-28 15:09:07 +0000583}