blob: 803bc561bae67e84ed59d4944e0197e2d3eb19ca [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,
John McCallc07a0c72011-02-17 10:25:35 +000032 const Expr *trueExpr,
33 const Expr *falseExpr);
Sebastian Redlf9463102010-06-28 15:09:07 +000034static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35 Cl::Kinds Kind, SourceLocation &Loc);
36
John McCall8d69a212010-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 Redlf9463102010-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 McCall4bc41ae2010-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:
Fariborz Jahanian071caef2011-03-26 19:48:30 +000078 case Cl::CL_ObjCMessageRValue:
John McCall4bc41ae2010-11-18 19:01:18 +000079 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
80 }
John McCall4bc41ae2010-11-18 19:01:18 +000081
Sebastian Redlf9463102010-06-28 15:09:07 +000082 Cl::ModifiableType modifiable = Cl::CM_Untested;
83 if (Loc)
84 modifiable = IsModifiable(Ctx, this, kind, *Loc);
85 return Classification(kind, modifiable);
86}
87
88static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
89 // This function takes the first stab at classifying expressions.
90 const LangOptions &Lang = Ctx.getLangOptions();
91
92 switch (E->getStmtClass()) {
93 // First come the expressions that are always lvalues, unconditionally.
Douglas Gregor4e442502010-09-14 21:51:42 +000094 case Stmt::NoStmtClass:
John McCallbd066782011-02-09 08:16:59 +000095#define ABSTRACT_STMT(Kind)
Douglas Gregor4e442502010-09-14 21:51:42 +000096#define STMT(Kind, Base) case Expr::Kind##Class:
97#define EXPR(Kind, Base)
98#include "clang/AST/StmtNodes.inc"
99 llvm_unreachable("cannot classify a statement");
100 break;
Sebastian Redlf9463102010-06-28 15:09:07 +0000101 case Expr::ObjCIsaExprClass:
102 // C++ [expr.prim.general]p1: A string literal is an lvalue.
103 case Expr::StringLiteralClass:
104 // @encode is equivalent to its string
105 case Expr::ObjCEncodeExprClass:
106 // __func__ and friends are too.
107 case Expr::PredefinedExprClass:
108 // Property references are lvalues
109 case Expr::ObjCPropertyRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000110 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
111 case Expr::CXXTypeidExprClass:
112 // Unresolved lookups get classified as lvalues.
113 // FIXME: Is this wise? Should they get their own kind?
114 case Expr::UnresolvedLookupExprClass:
115 case Expr::UnresolvedMemberExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000116 case Expr::CXXDependentScopeMemberExprClass:
117 case Expr::CXXUnresolvedConstructExprClass:
118 case Expr::DependentScopeDeclRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000119 // ObjC instance variables are lvalues
120 // FIXME: ObjC++0x might have different rules
121 case Expr::ObjCIvarRefExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000122 return Cl::CL_LValue;
Douglas Gregor4e442502010-09-14 21:51:42 +0000123 // C99 6.5.2.5p5 says that compound literals are lvalues.
124 // In C++, they're class temporaries.
125 case Expr::CompoundLiteralExprClass:
126 return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary
127 : Cl::CL_LValue;
128
129 // Expressions that are prvalues.
130 case Expr::CXXBoolLiteralExprClass:
131 case Expr::CXXPseudoDestructorExprClass:
Peter Collingbournee190dee2011-03-11 19:24:49 +0000132 case Expr::UnaryExprOrTypeTraitExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000133 case Expr::CXXNewExprClass:
134 case Expr::CXXThisExprClass:
135 case Expr::CXXNullPtrLiteralExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000136 case Expr::ImaginaryLiteralClass:
137 case Expr::GNUNullExprClass:
138 case Expr::OffsetOfExprClass:
139 case Expr::CXXThrowExprClass:
140 case Expr::ShuffleVectorExprClass:
141 case Expr::IntegerLiteralClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000142 case Expr::CharacterLiteralClass:
143 case Expr::AddrLabelExprClass:
144 case Expr::CXXDeleteExprClass:
145 case Expr::ImplicitValueInitExprClass:
146 case Expr::BlockExprClass:
147 case Expr::FloatingLiteralClass:
148 case Expr::CXXNoexceptExprClass:
149 case Expr::CXXScalarValueInitExprClass:
150 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +0000151 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000152 case Expr::ObjCSelectorExprClass:
153 case Expr::ObjCProtocolExprClass:
154 case Expr::ObjCStringLiteralClass:
155 case Expr::ParenListExprClass:
156 case Expr::InitListExprClass:
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000157 case Expr::SizeOfPackExprClass:
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000158 case Expr::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor4e442502010-09-14 21:51:42 +0000159 return Cl::CL_PRValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000160
161 // Next come the complicated cases.
162
163 // C++ [expr.sub]p1: The result is an lvalue of type "T".
164 // However, subscripting vector types is more like member access.
165 case Expr::ArraySubscriptExprClass:
166 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
167 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
168 return Cl::CL_LValue;
169
170 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
171 // function or variable and a prvalue otherwise.
172 case Expr::DeclRefExprClass:
John McCall2979fe02011-04-12 00:42:48 +0000173 if (E->getType() == Ctx.UnknownAnyTy)
174 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
175 ? Cl::CL_PRValue : Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000176 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
177 // We deal with names referenced from blocks the same way.
178 case Expr::BlockDeclRefExprClass:
179 return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl());
180
181 // Member access is complex.
182 case Expr::MemberExprClass:
183 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
184
185 case Expr::UnaryOperatorClass:
186 switch (cast<UnaryOperator>(E)->getOpcode()) {
187 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
188 // [...] the result is an lvalue referring to the object or function
189 // to which the expression points.
John McCalle3027922010-08-25 11:45:40 +0000190 case UO_Deref:
Sebastian Redlf9463102010-06-28 15:09:07 +0000191 return Cl::CL_LValue;
192
193 // GNU extensions, simply look through them.
John McCalle3027922010-08-25 11:45:40 +0000194 case UO_Extension:
Sebastian Redlf9463102010-06-28 15:09:07 +0000195 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
196
John McCall07bb1962010-11-16 10:08:07 +0000197 // Treat _Real and _Imag basically as if they were member
198 // expressions: l-value only if the operand is a true l-value.
199 case UO_Real:
200 case UO_Imag: {
201 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
202 Cl::Kinds K = ClassifyInternal(Ctx, Op);
203 if (K != Cl::CL_LValue) return K;
204
John McCallb7bd14f2010-12-02 01:19:52 +0000205 if (isa<ObjCPropertyRefExpr>(Op))
John McCall07bb1962010-11-16 10:08:07 +0000206 return Cl::CL_SubObjCPropertySetting;
207 return Cl::CL_LValue;
208 }
209
Sebastian Redlf9463102010-06-28 15:09:07 +0000210 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
211 // lvalue, [...]
212 // Not so in C.
John McCalle3027922010-08-25 11:45:40 +0000213 case UO_PreInc:
214 case UO_PreDec:
Sebastian Redlf9463102010-06-28 15:09:07 +0000215 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
216
217 default:
218 return Cl::CL_PRValue;
219 }
220
John McCall8d69a212010-11-15 23:31:06 +0000221 case Expr::OpaqueValueExprClass:
222 return ClassifyExprValueKind(Lang, E,
223 cast<OpaqueValueExpr>(E)->getValueKind());
224
Sebastian Redlf9463102010-06-28 15:09:07 +0000225 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
226 // only specifically record class temporaries.
227 case Expr::ImplicitCastExprClass:
John McCall8d69a212010-11-15 23:31:06 +0000228 return ClassifyExprValueKind(Lang, E,
229 cast<ImplicitCastExpr>(E)->getValueKind());
Sebastian Redlf9463102010-06-28 15:09:07 +0000230
231 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
232 // whether the expression is an lvalue.
233 case Expr::ParenExprClass:
234 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
235
Peter Collingbourne91147592011-04-15 00:35:48 +0000236 // C1X 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
237 // or a void expression if its result expression is, respectively, an
238 // lvalue, a function designator, or a void expression.
239 case Expr::GenericSelectionExprClass:
240 if (cast<GenericSelectionExpr>(E)->isResultDependent())
241 return Cl::CL_PRValue;
242 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
243
Sebastian Redlf9463102010-06-28 15:09:07 +0000244 case Expr::BinaryOperatorClass:
245 case Expr::CompoundAssignOperatorClass:
246 // C doesn't have any binary expressions that are lvalues.
247 if (Lang.CPlusPlus)
248 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
249 return Cl::CL_PRValue;
250
251 case Expr::CallExprClass:
252 case Expr::CXXOperatorCallExprClass:
253 case Expr::CXXMemberCallExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +0000254 case Expr::CUDAKernelCallExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000255 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
256
257 // __builtin_choose_expr is equivalent to the chosen expression.
258 case Expr::ChooseExprClass:
259 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
260
261 // Extended vector element access is an lvalue unless there are duplicates
262 // in the shuffle expression.
263 case Expr::ExtVectorElementExprClass:
264 return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
265 Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
266
267 // Simply look at the actual default argument.
268 case Expr::CXXDefaultArgExprClass:
269 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
270
271 // Same idea for temporary binding.
272 case Expr::CXXBindTemporaryExprClass:
273 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
274
John McCall5d413782010-12-06 08:20:24 +0000275 // And the cleanups guard.
276 case Expr::ExprWithCleanupsClass:
277 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
Sebastian Redlf9463102010-06-28 15:09:07 +0000278
279 // Casts depend completely on the target type. All casts work the same.
280 case Expr::CStyleCastExprClass:
281 case Expr::CXXFunctionalCastExprClass:
282 case Expr::CXXStaticCastExprClass:
283 case Expr::CXXDynamicCastExprClass:
284 case Expr::CXXReinterpretCastExprClass:
285 case Expr::CXXConstCastExprClass:
286 // Only in C++ can casts be interesting at all.
287 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
288 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
289
John McCallc07a0c72011-02-17 10:25:35 +0000290 case Expr::BinaryConditionalOperatorClass: {
291 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
292 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
293 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
294 }
295
296 case Expr::ConditionalOperatorClass: {
Sebastian Redlf9463102010-06-28 15:09:07 +0000297 // Once again, only C++ is interesting.
298 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
John McCallc07a0c72011-02-17 10:25:35 +0000299 const ConditionalOperator *co = cast<ConditionalOperator>(E);
300 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
301 }
Sebastian Redlf9463102010-06-28 15:09:07 +0000302
303 // ObjC message sends are effectively function calls, if the target function
304 // is known.
305 case Expr::ObjCMessageExprClass:
306 if (const ObjCMethodDecl *Method =
307 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000308 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
309 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
Sebastian Redlf9463102010-06-28 15:09:07 +0000310 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000311 return Cl::CL_PRValue;
312
Sebastian Redlf9463102010-06-28 15:09:07 +0000313 // Some C++ expressions are always class temporaries.
314 case Expr::CXXConstructExprClass:
315 case Expr::CXXTemporaryObjectExprClass:
Sebastian Redlf9463102010-06-28 15:09:07 +0000316 return Cl::CL_ClassTemporary;
317
Douglas Gregor4e442502010-09-14 21:51:42 +0000318 case Expr::VAArgExprClass:
319 return ClassifyUnnamed(Ctx, E->getType());
320
321 case Expr::DesignatedInitExprClass:
322 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
323
324 case Expr::StmtExprClass: {
325 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
326 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
Douglas Gregore572b062010-09-15 01:37:48 +0000327 return ClassifyUnnamed(Ctx, LastExpr->getType());
Sebastian Redlf9463102010-06-28 15:09:07 +0000328 return Cl::CL_PRValue;
329 }
Douglas Gregor4e442502010-09-14 21:51:42 +0000330
331 case Expr::CXXUuidofExprClass:
Francois Pichet4f64c5a2010-12-17 02:00:06 +0000332 return Cl::CL_LValue;
Douglas Gregore8e9dd62011-01-03 17:17:50 +0000333
334 case Expr::PackExpansionExprClass:
335 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
Douglas Gregor4e442502010-09-14 21:51:42 +0000336 }
337
338 llvm_unreachable("unhandled expression kind in classification");
339 return Cl::CL_LValue;
Sebastian Redlf9463102010-06-28 15:09:07 +0000340}
341
342/// ClassifyDecl - Return the classification of an expression referencing the
343/// given declaration.
344static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
345 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
346 // function, variable, or data member and a prvalue otherwise.
347 // In C, functions are not lvalues.
348 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
349 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
350 // special-case this.
John McCall8d08b9b2010-08-27 09:08:28 +0000351
352 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
353 return Cl::CL_MemberFunction;
354
Sebastian Redlf9463102010-06-28 15:09:07 +0000355 bool islvalue;
356 if (const NonTypeTemplateParmDecl *NTTParm =
357 dyn_cast<NonTypeTemplateParmDecl>(D))
358 islvalue = NTTParm->getType()->isReferenceType();
359 else
360 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
Francois Pichet783dd6e2010-11-21 06:08:52 +0000361 isa<IndirectFieldDecl>(D) ||
Sebastian Redlf9463102010-06-28 15:09:07 +0000362 (Ctx.getLangOptions().CPlusPlus &&
363 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
364
365 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
366}
367
368/// ClassifyUnnamed - Return the classification of an expression yielding an
369/// unnamed value of the given type. This applies in particular to function
370/// calls and casts.
371static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
372 // In C, function calls are always rvalues.
373 if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue;
374
375 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
376 // lvalue reference type or an rvalue reference to function type, an xvalue
377 // if the result type is an rvalue refernence to object type, and a prvalue
378 // otherwise.
379 if (T->isLValueReferenceType())
380 return Cl::CL_LValue;
381 const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
382 if (!RV) // Could still be a class temporary, though.
383 return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
384
385 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
386}
387
388static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
John McCall2979fe02011-04-12 00:42:48 +0000389 if (E->getType() == Ctx.UnknownAnyTy)
390 return (isa<FunctionDecl>(E->getMemberDecl())
391 ? Cl::CL_PRValue : Cl::CL_LValue);
392
Sebastian Redlf9463102010-06-28 15:09:07 +0000393 // Handle C first, it's easier.
394 if (!Ctx.getLangOptions().CPlusPlus) {
395 // C99 6.5.2.3p3
396 // For dot access, the expression is an lvalue if the first part is. For
397 // arrow access, it always is an lvalue.
398 if (E->isArrow())
399 return Cl::CL_LValue;
400 // ObjC property accesses are not lvalues, but get special treatment.
John McCall07bb1962010-11-16 10:08:07 +0000401 Expr *Base = E->getBase()->IgnoreParens();
John McCallb7bd14f2010-12-02 01:19:52 +0000402 if (isa<ObjCPropertyRefExpr>(Base))
Sebastian Redlf9463102010-06-28 15:09:07 +0000403 return Cl::CL_SubObjCPropertySetting;
404 return ClassifyInternal(Ctx, Base);
405 }
406
407 NamedDecl *Member = E->getMemberDecl();
408 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
409 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
410 // E1.E2 is an lvalue.
411 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
412 if (Value->getType()->isReferenceType())
413 return Cl::CL_LValue;
414
415 // Otherwise, one of the following rules applies.
416 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
417 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
418 return Cl::CL_LValue;
419
420 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
421 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
422 // otherwise, it is a prvalue.
423 if (isa<FieldDecl>(Member)) {
424 // *E1 is an lvalue
425 if (E->isArrow())
426 return Cl::CL_LValue;
John McCall4bc41ae2010-11-18 19:01:18 +0000427 Expr *Base = E->getBase()->IgnoreParenImpCasts();
John McCallb7bd14f2010-12-02 01:19:52 +0000428 if (isa<ObjCPropertyRefExpr>(Base))
John McCall4bc41ae2010-11-18 19:01:18 +0000429 return Cl::CL_SubObjCPropertySetting;
Sebastian Redlf9463102010-06-28 15:09:07 +0000430 return ClassifyInternal(Ctx, E->getBase());
431 }
432
433 // -- If E2 is a [...] member function, [...]
434 // -- If it refers to a static member function [...], then E1.E2 is an
435 // lvalue; [...]
436 // -- Otherwise [...] E1.E2 is a prvalue.
437 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
438 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
439
440 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
441 // So is everything else we haven't handled yet.
442 return Cl::CL_PRValue;
443}
444
445static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
446 assert(Ctx.getLangOptions().CPlusPlus &&
447 "This is only relevant for C++.");
448 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
John McCall34376a62010-12-04 03:47:34 +0000449 // Except we override this for writes to ObjC properties.
Sebastian Redlf9463102010-06-28 15:09:07 +0000450 if (E->isAssignmentOp())
John McCall34376a62010-12-04 03:47:34 +0000451 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
452 ? Cl::CL_PRValue : Cl::CL_LValue);
Sebastian Redlf9463102010-06-28 15:09:07 +0000453
454 // C++ [expr.comma]p1: the result is of the same value category as its right
455 // operand, [...].
John McCalle3027922010-08-25 11:45:40 +0000456 if (E->getOpcode() == BO_Comma)
Sebastian Redlf9463102010-06-28 15:09:07 +0000457 return ClassifyInternal(Ctx, E->getRHS());
458
459 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
460 // is a pointer to a data member is of the same value category as its first
461 // operand.
John McCalle3027922010-08-25 11:45:40 +0000462 if (E->getOpcode() == BO_PtrMemD)
Sebastian Redlf9463102010-06-28 15:09:07 +0000463 return E->getType()->isFunctionType() ? Cl::CL_MemberFunction :
464 ClassifyInternal(Ctx, E->getLHS());
465
466 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
467 // second operand is a pointer to data member and a prvalue otherwise.
John McCalle3027922010-08-25 11:45:40 +0000468 if (E->getOpcode() == BO_PtrMemI)
Sebastian Redlf9463102010-06-28 15:09:07 +0000469 return E->getType()->isFunctionType() ?
470 Cl::CL_MemberFunction : Cl::CL_LValue;
471
472 // All other binary operations are prvalues.
473 return Cl::CL_PRValue;
474}
475
John McCallc07a0c72011-02-17 10:25:35 +0000476static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
477 const Expr *False) {
Sebastian Redlf9463102010-06-28 15:09:07 +0000478 assert(Ctx.getLangOptions().CPlusPlus &&
479 "This is only relevant for C++.");
480
Sebastian Redlf9463102010-06-28 15:09:07 +0000481 // C++ [expr.cond]p2
482 // If either the second or the third operand has type (cv) void, [...]
483 // the result [...] is a prvalue.
484 if (True->getType()->isVoidType() || False->getType()->isVoidType())
485 return Cl::CL_PRValue;
486
487 // Note that at this point, we have already performed all conversions
488 // according to [expr.cond]p3.
489 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
490 // same value category [...], the result is of that [...] value category.
491 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
492 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
493 RCl = ClassifyInternal(Ctx, False);
494 return LCl == RCl ? LCl : Cl::CL_PRValue;
495}
496
497static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
498 Cl::Kinds Kind, SourceLocation &Loc) {
499 // As a general rule, we only care about lvalues. But there are some rvalues
500 // for which we want to generate special results.
501 if (Kind == Cl::CL_PRValue) {
502 // For the sake of better diagnostics, we want to specifically recognize
503 // use of the GCC cast-as-lvalue extension.
John McCall34376a62010-12-04 03:47:34 +0000504 if (const ExplicitCastExpr *CE =
505 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
506 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
507 Loc = CE->getExprLoc();
Sebastian Redlf9463102010-06-28 15:09:07 +0000508 return Cl::CM_LValueCast;
509 }
510 }
511 }
512 if (Kind != Cl::CL_LValue)
513 return Cl::CM_RValue;
514
515 // This is the lvalue case.
516 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
517 if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType())
518 return Cl::CM_Function;
519
520 // You cannot assign to a variable outside a block from within the block if
521 // it is not marked __block, e.g.
522 // void takeclosure(void (^C)(void));
523 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
524 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) {
525 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
526 return Cl::CM_NotBlockQualified;
527 }
528
529 // Assignment to a property in ObjC is an implicit setter access. But a
530 // setter might not exist.
John McCallb7bd14f2010-12-02 01:19:52 +0000531 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
532 if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
Sebastian Redlf9463102010-06-28 15:09:07 +0000533 return Cl::CM_NoSetterProperty;
534 }
535
536 CanQualType CT = Ctx.getCanonicalType(E->getType());
537 // Const stuff is obviously not modifiable.
538 if (CT.isConstQualified())
539 return Cl::CM_ConstQualified;
540 // Arrays are not modifiable, only their elements are.
541 if (CT->isArrayType())
542 return Cl::CM_ArrayType;
543 // Incomplete types are not modifiable.
544 if (CT->isIncompleteType())
545 return Cl::CM_IncompleteType;
546
547 // Records with any const fields (recursively) are not modifiable.
548 if (const RecordType *R = CT->getAs<RecordType>()) {
John McCall622114c2010-12-06 05:26:58 +0000549 assert((E->getObjectKind() == OK_ObjCProperty ||
Fariborz Jahaniane89d03f2010-09-09 23:01:10 +0000550 !Ctx.getLangOptions().CPlusPlus) &&
Sebastian Redlf9463102010-06-28 15:09:07 +0000551 "C++ struct assignment should be resolved by the "
552 "copy assignment operator.");
553 if (R->hasConstFields())
554 return Cl::CM_ConstQualified;
555 }
556
557 return Cl::CM_Modifiable;
558}
559
John McCall086a4642010-11-24 05:12:34 +0000560Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
Sebastian Redlf9463102010-06-28 15:09:07 +0000561 Classification VC = Classify(Ctx);
562 switch (VC.getKind()) {
563 case Cl::CL_LValue: return LV_Valid;
564 case Cl::CL_XValue: return LV_InvalidExpression;
565 case Cl::CL_Function: return LV_NotObjectType;
566 case Cl::CL_Void: return LV_IncompleteVoidType;
567 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
568 case Cl::CL_MemberFunction: return LV_MemberFunction;
569 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
570 case Cl::CL_ClassTemporary: return LV_ClassTemporary;
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000571 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
Sebastian Redlf9463102010-06-28 15:09:07 +0000572 case Cl::CL_PRValue: return LV_InvalidExpression;
573 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000574 llvm_unreachable("Unhandled kind");
Sebastian Redlf9463102010-06-28 15:09:07 +0000575}
576
577Expr::isModifiableLvalueResult
578Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
579 SourceLocation dummy;
580 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
581 switch (VC.getKind()) {
582 case Cl::CL_LValue: break;
583 case Cl::CL_XValue: return MLV_InvalidExpression;
584 case Cl::CL_Function: return MLV_NotObjectType;
585 case Cl::CL_Void: return MLV_IncompleteVoidType;
586 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
587 case Cl::CL_MemberFunction: return MLV_MemberFunction;
588 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
589 case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
Fariborz Jahanian071caef2011-03-26 19:48:30 +0000590 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
Sebastian Redlf9463102010-06-28 15:09:07 +0000591 case Cl::CL_PRValue:
592 return VC.getModifiable() == Cl::CM_LValueCast ?
593 MLV_LValueCast : MLV_InvalidExpression;
594 }
595 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
596 switch (VC.getModifiable()) {
Chandler Carruth8337ba62010-06-29 00:23:11 +0000597 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
Sebastian Redlf9463102010-06-28 15:09:07 +0000598 case Cl::CM_Modifiable: return MLV_Valid;
Chandler Carruth8337ba62010-06-29 00:23:11 +0000599 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000600 case Cl::CM_Function: return MLV_NotObjectType;
601 case Cl::CM_LValueCast:
Chandler Carruth8337ba62010-06-29 00:23:11 +0000602 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
Sebastian Redlf9463102010-06-28 15:09:07 +0000603 case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified;
604 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
605 case Cl::CM_ConstQualified: return MLV_ConstQualified;
606 case Cl::CM_ArrayType: return MLV_ArrayType;
607 case Cl::CM_IncompleteType: return MLV_IncompleteType;
608 }
Chandler Carruth8337ba62010-06-29 00:23:11 +0000609 llvm_unreachable("Unhandled modifiable type");
Sebastian Redlf9463102010-06-28 15:09:07 +0000610}