1210008Srdivacky//===--- ExprClassification.cpp - Expression AST Node Implementation ------===//
2210008Srdivacky//
3210008Srdivacky//                     The LLVM Compiler Infrastructure
4210008Srdivacky//
5210008Srdivacky// This file is distributed under the University of Illinois Open Source
6210008Srdivacky// License. See LICENSE.TXT for details.
7210008Srdivacky//
8210008Srdivacky//===----------------------------------------------------------------------===//
9210008Srdivacky//
10210008Srdivacky// This file implements Expr::classify.
11210008Srdivacky//
12210008Srdivacky//===----------------------------------------------------------------------===//
13210008Srdivacky
14210008Srdivacky#include "clang/AST/Expr.h"
15210008Srdivacky#include "clang/AST/ASTContext.h"
16249423Sdim#include "clang/AST/DeclCXX.h"
17210008Srdivacky#include "clang/AST/DeclObjC.h"
18210008Srdivacky#include "clang/AST/DeclTemplate.h"
19249423Sdim#include "clang/AST/ExprCXX.h"
20249423Sdim#include "clang/AST/ExprObjC.h"
21249423Sdim#include "llvm/Support/ErrorHandling.h"
22210008Srdivackyusing namespace clang;
23210008Srdivacky
24210008Srdivackytypedef Expr::Classification Cl;
25210008Srdivacky
26210008Srdivackystatic Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27210008Srdivackystatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
28210008Srdivackystatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);
29210008Srdivackystatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
30210008Srdivackystatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);
31210008Srdivackystatic Cl::Kinds ClassifyConditional(ASTContext &Ctx,
32218893Sdim                                     const Expr *trueExpr,
33218893Sdim                                     const Expr *falseExpr);
34210008Srdivackystatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
35210008Srdivacky                                       Cl::Kinds Kind, SourceLocation &Loc);
36210008Srdivacky
37210008SrdivackyCl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38210008Srdivacky  assert(!TR->isReferenceType() && "Expressions can't have reference type.");
39210008Srdivacky
40210008Srdivacky  Cl::Kinds kind = ClassifyInternal(Ctx, this);
41210008Srdivacky  // C99 6.3.2.1: An lvalue is an expression with an object type or an
42210008Srdivacky  //   incomplete type other than void.
43234353Sdim  if (!Ctx.getLangOpts().CPlusPlus) {
44210008Srdivacky    // Thus, no functions.
45210008Srdivacky    if (TR->isFunctionType() || TR == Ctx.OverloadTy)
46210008Srdivacky      kind = Cl::CL_Function;
47210008Srdivacky    // No void either, but qualified void is OK because it is "other than void".
48221345Sdim    // Void "lvalues" are classified as addressable void values, which are void
49221345Sdim    // expressions whose address can be taken.
50221345Sdim    else if (TR->isVoidType() && !TR.hasQualifiers())
51221345Sdim      kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void);
52210008Srdivacky  }
53210008Srdivacky
54218893Sdim  // Enable this assertion for testing.
55218893Sdim  switch (kind) {
56218893Sdim  case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
57218893Sdim  case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
58218893Sdim  case Cl::CL_Function:
59218893Sdim  case Cl::CL_Void:
60221345Sdim  case Cl::CL_AddressableVoid:
61218893Sdim  case Cl::CL_DuplicateVectorComponents:
62218893Sdim  case Cl::CL_MemberFunction:
63218893Sdim  case Cl::CL_SubObjCPropertySetting:
64218893Sdim  case Cl::CL_ClassTemporary:
65239462Sdim  case Cl::CL_ArrayTemporary:
66221345Sdim  case Cl::CL_ObjCMessageRValue:
67218893Sdim  case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
68218893Sdim  }
69218893Sdim
70210008Srdivacky  Cl::ModifiableType modifiable = Cl::CM_Untested;
71210008Srdivacky  if (Loc)
72210008Srdivacky    modifiable = IsModifiable(Ctx, this, kind, *Loc);
73210008Srdivacky  return Classification(kind, modifiable);
74210008Srdivacky}
75210008Srdivacky
76239462Sdim/// Classify an expression which creates a temporary, based on its type.
77239462Sdimstatic Cl::Kinds ClassifyTemporary(QualType T) {
78239462Sdim  if (T->isRecordType())
79239462Sdim    return Cl::CL_ClassTemporary;
80239462Sdim  if (T->isArrayType())
81239462Sdim    return Cl::CL_ArrayTemporary;
82239462Sdim
83239462Sdim  // No special classification: these don't behave differently from normal
84239462Sdim  // prvalues.
85239462Sdim  return Cl::CL_PRValue;
86239462Sdim}
87239462Sdim
88249423Sdimstatic Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
89249423Sdim                                       const Expr *E,
90249423Sdim                                       ExprValueKind Kind) {
91249423Sdim  switch (Kind) {
92249423Sdim  case VK_RValue:
93249423Sdim    return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue;
94249423Sdim  case VK_LValue:
95249423Sdim    return Cl::CL_LValue;
96249423Sdim  case VK_XValue:
97249423Sdim    return Cl::CL_XValue;
98249423Sdim  }
99249423Sdim  llvm_unreachable("Invalid value category of implicit cast.");
100249423Sdim}
101249423Sdim
102210008Srdivackystatic Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
103210008Srdivacky  // This function takes the first stab at classifying expressions.
104234353Sdim  const LangOptions &Lang = Ctx.getLangOpts();
105210008Srdivacky
106210008Srdivacky  switch (E->getStmtClass()) {
107218893Sdim  case Stmt::NoStmtClass:
108218893Sdim#define ABSTRACT_STMT(Kind)
109218893Sdim#define STMT(Kind, Base) case Expr::Kind##Class:
110218893Sdim#define EXPR(Kind, Base)
111218893Sdim#include "clang/AST/StmtNodes.inc"
112218893Sdim    llvm_unreachable("cannot classify a statement");
113234353Sdim
114234353Sdim    // First come the expressions that are always lvalues, unconditionally.
115210008Srdivacky  case Expr::ObjCIsaExprClass:
116210008Srdivacky    // C++ [expr.prim.general]p1: A string literal is an lvalue.
117210008Srdivacky  case Expr::StringLiteralClass:
118210008Srdivacky    // @encode is equivalent to its string
119210008Srdivacky  case Expr::ObjCEncodeExprClass:
120210008Srdivacky    // __func__ and friends are too.
121210008Srdivacky  case Expr::PredefinedExprClass:
122210008Srdivacky    // Property references are lvalues
123234353Sdim  case Expr::ObjCSubscriptRefExprClass:
124210008Srdivacky  case Expr::ObjCPropertyRefExprClass:
125210008Srdivacky    // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
126210008Srdivacky  case Expr::CXXTypeidExprClass:
127210008Srdivacky    // Unresolved lookups get classified as lvalues.
128210008Srdivacky    // FIXME: Is this wise? Should they get their own kind?
129210008Srdivacky  case Expr::UnresolvedLookupExprClass:
130210008Srdivacky  case Expr::UnresolvedMemberExprClass:
131218893Sdim  case Expr::CXXDependentScopeMemberExprClass:
132218893Sdim  case Expr::DependentScopeDeclRefExprClass:
133210008Srdivacky    // ObjC instance variables are lvalues
134210008Srdivacky    // FIXME: ObjC++0x might have different rules
135210008Srdivacky  case Expr::ObjCIvarRefExprClass:
136243830Sdim  case Expr::FunctionParmPackExprClass:
137251662Sdim  case Expr::MSPropertyRefExprClass:
138218893Sdim    return Cl::CL_LValue;
139234353Sdim
140210008Srdivacky    // C99 6.5.2.5p5 says that compound literals are lvalues.
141239462Sdim    // In C++, they're prvalue temporaries.
142210008Srdivacky  case Expr::CompoundLiteralExprClass:
143239462Sdim    return Ctx.getLangOpts().CPlusPlus ? ClassifyTemporary(E->getType())
144239462Sdim                                       : Cl::CL_LValue;
145210008Srdivacky
146218893Sdim    // Expressions that are prvalues.
147218893Sdim  case Expr::CXXBoolLiteralExprClass:
148218893Sdim  case Expr::CXXPseudoDestructorExprClass:
149221345Sdim  case Expr::UnaryExprOrTypeTraitExprClass:
150218893Sdim  case Expr::CXXNewExprClass:
151218893Sdim  case Expr::CXXThisExprClass:
152218893Sdim  case Expr::CXXNullPtrLiteralExprClass:
153218893Sdim  case Expr::ImaginaryLiteralClass:
154218893Sdim  case Expr::GNUNullExprClass:
155218893Sdim  case Expr::OffsetOfExprClass:
156218893Sdim  case Expr::CXXThrowExprClass:
157218893Sdim  case Expr::ShuffleVectorExprClass:
158263508Sdim  case Expr::ConvertVectorExprClass:
159218893Sdim  case Expr::IntegerLiteralClass:
160218893Sdim  case Expr::CharacterLiteralClass:
161218893Sdim  case Expr::AddrLabelExprClass:
162218893Sdim  case Expr::CXXDeleteExprClass:
163218893Sdim  case Expr::ImplicitValueInitExprClass:
164218893Sdim  case Expr::BlockExprClass:
165218893Sdim  case Expr::FloatingLiteralClass:
166218893Sdim  case Expr::CXXNoexceptExprClass:
167218893Sdim  case Expr::CXXScalarValueInitExprClass:
168218893Sdim  case Expr::UnaryTypeTraitExprClass:
169218893Sdim  case Expr::BinaryTypeTraitExprClass:
170234353Sdim  case Expr::TypeTraitExprClass:
171221345Sdim  case Expr::ArrayTypeTraitExprClass:
172221345Sdim  case Expr::ExpressionTraitExprClass:
173218893Sdim  case Expr::ObjCSelectorExprClass:
174218893Sdim  case Expr::ObjCProtocolExprClass:
175218893Sdim  case Expr::ObjCStringLiteralClass:
176239462Sdim  case Expr::ObjCBoxedExprClass:
177234353Sdim  case Expr::ObjCArrayLiteralClass:
178234353Sdim  case Expr::ObjCDictionaryLiteralClass:
179234353Sdim  case Expr::ObjCBoolLiteralExprClass:
180218893Sdim  case Expr::ParenListExprClass:
181218893Sdim  case Expr::SizeOfPackExprClass:
182218893Sdim  case Expr::SubstNonTypeTemplateParmPackExprClass:
183223017Sdim  case Expr::AsTypeExprClass:
184224145Sdim  case Expr::ObjCIndirectCopyRestoreExprClass:
185226633Sdim  case Expr::AtomicExprClass:
186218893Sdim    return Cl::CL_PRValue;
187218893Sdim
188210008Srdivacky    // Next come the complicated cases.
189224145Sdim  case Expr::SubstNonTypeTemplateParmExprClass:
190224145Sdim    return ClassifyInternal(Ctx,
191224145Sdim                 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
192210008Srdivacky
193210008Srdivacky    // C++ [expr.sub]p1: The result is an lvalue of type "T".
194210008Srdivacky    // However, subscripting vector types is more like member access.
195210008Srdivacky  case Expr::ArraySubscriptExprClass:
196210008Srdivacky    if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
197210008Srdivacky      return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
198210008Srdivacky    return Cl::CL_LValue;
199210008Srdivacky
200210008Srdivacky    // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
201210008Srdivacky    //   function or variable and a prvalue otherwise.
202210008Srdivacky  case Expr::DeclRefExprClass:
203221345Sdim    if (E->getType() == Ctx.UnknownAnyTy)
204221345Sdim      return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
205221345Sdim               ? Cl::CL_PRValue : Cl::CL_LValue;
206210008Srdivacky    return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
207210008Srdivacky
208210008Srdivacky    // Member access is complex.
209210008Srdivacky  case Expr::MemberExprClass:
210210008Srdivacky    return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
211210008Srdivacky
212210008Srdivacky  case Expr::UnaryOperatorClass:
213210008Srdivacky    switch (cast<UnaryOperator>(E)->getOpcode()) {
214210008Srdivacky      // C++ [expr.unary.op]p1: The unary * operator performs indirection:
215210008Srdivacky      //   [...] the result is an lvalue referring to the object or function
216210008Srdivacky      //   to which the expression points.
217212904Sdim    case UO_Deref:
218210008Srdivacky      return Cl::CL_LValue;
219210008Srdivacky
220210008Srdivacky      // GNU extensions, simply look through them.
221212904Sdim    case UO_Extension:
222210008Srdivacky      return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
223210008Srdivacky
224218893Sdim    // Treat _Real and _Imag basically as if they were member
225218893Sdim    // expressions:  l-value only if the operand is a true l-value.
226218893Sdim    case UO_Real:
227218893Sdim    case UO_Imag: {
228218893Sdim      const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
229218893Sdim      Cl::Kinds K = ClassifyInternal(Ctx, Op);
230218893Sdim      if (K != Cl::CL_LValue) return K;
231218893Sdim
232218893Sdim      if (isa<ObjCPropertyRefExpr>(Op))
233218893Sdim        return Cl::CL_SubObjCPropertySetting;
234218893Sdim      return Cl::CL_LValue;
235218893Sdim    }
236218893Sdim
237210008Srdivacky      // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
238210008Srdivacky      //   lvalue, [...]
239210008Srdivacky      // Not so in C.
240212904Sdim    case UO_PreInc:
241212904Sdim    case UO_PreDec:
242210008Srdivacky      return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
243210008Srdivacky
244210008Srdivacky    default:
245210008Srdivacky      return Cl::CL_PRValue;
246210008Srdivacky    }
247210008Srdivacky
248218893Sdim  case Expr::OpaqueValueExprClass:
249234353Sdim    return ClassifyExprValueKind(Lang, E, E->getValueKind());
250234353Sdim
251234353Sdim    // Pseudo-object expressions can produce l-values with reference magic.
252234353Sdim  case Expr::PseudoObjectExprClass:
253218893Sdim    return ClassifyExprValueKind(Lang, E,
254234353Sdim                                 cast<PseudoObjectExpr>(E)->getValueKind());
255218893Sdim
256210008Srdivacky    // Implicit casts are lvalues if they're lvalue casts. Other than that, we
257210008Srdivacky    // only specifically record class temporaries.
258210008Srdivacky  case Expr::ImplicitCastExprClass:
259234353Sdim    return ClassifyExprValueKind(Lang, E, E->getValueKind());
260210008Srdivacky
261210008Srdivacky    // C++ [expr.prim.general]p4: The presence of parentheses does not affect
262210008Srdivacky    //   whether the expression is an lvalue.
263210008Srdivacky  case Expr::ParenExprClass:
264210008Srdivacky    return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
265210008Srdivacky
266234353Sdim    // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
267221345Sdim    // or a void expression if its result expression is, respectively, an
268221345Sdim    // lvalue, a function designator, or a void expression.
269221345Sdim  case Expr::GenericSelectionExprClass:
270221345Sdim    if (cast<GenericSelectionExpr>(E)->isResultDependent())
271221345Sdim      return Cl::CL_PRValue;
272221345Sdim    return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
273221345Sdim
274210008Srdivacky  case Expr::BinaryOperatorClass:
275210008Srdivacky  case Expr::CompoundAssignOperatorClass:
276210008Srdivacky    // C doesn't have any binary expressions that are lvalues.
277210008Srdivacky    if (Lang.CPlusPlus)
278210008Srdivacky      return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
279210008Srdivacky    return Cl::CL_PRValue;
280210008Srdivacky
281210008Srdivacky  case Expr::CallExprClass:
282210008Srdivacky  case Expr::CXXOperatorCallExprClass:
283210008Srdivacky  case Expr::CXXMemberCallExprClass:
284234353Sdim  case Expr::UserDefinedLiteralClass:
285218893Sdim  case Expr::CUDAKernelCallExprClass:
286210008Srdivacky    return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
287210008Srdivacky
288210008Srdivacky    // __builtin_choose_expr is equivalent to the chosen expression.
289210008Srdivacky  case Expr::ChooseExprClass:
290263508Sdim    return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
291210008Srdivacky
292210008Srdivacky    // Extended vector element access is an lvalue unless there are duplicates
293210008Srdivacky    // in the shuffle expression.
294210008Srdivacky  case Expr::ExtVectorElementExprClass:
295263508Sdim    if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
296263508Sdim      return Cl::CL_DuplicateVectorComponents;
297263508Sdim    if (cast<ExtVectorElementExpr>(E)->isArrow())
298263508Sdim      return Cl::CL_LValue;
299263508Sdim    return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
300210008Srdivacky
301210008Srdivacky    // Simply look at the actual default argument.
302210008Srdivacky  case Expr::CXXDefaultArgExprClass:
303210008Srdivacky    return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
304210008Srdivacky
305251662Sdim    // Same idea for default initializers.
306251662Sdim  case Expr::CXXDefaultInitExprClass:
307251662Sdim    return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
308251662Sdim
309210008Srdivacky    // Same idea for temporary binding.
310210008Srdivacky  case Expr::CXXBindTemporaryExprClass:
311210008Srdivacky    return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
312210008Srdivacky
313218893Sdim    // And the cleanups guard.
314218893Sdim  case Expr::ExprWithCleanupsClass:
315218893Sdim    return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
316210008Srdivacky
317210008Srdivacky    // Casts depend completely on the target type. All casts work the same.
318210008Srdivacky  case Expr::CStyleCastExprClass:
319210008Srdivacky  case Expr::CXXFunctionalCastExprClass:
320210008Srdivacky  case Expr::CXXStaticCastExprClass:
321210008Srdivacky  case Expr::CXXDynamicCastExprClass:
322210008Srdivacky  case Expr::CXXReinterpretCastExprClass:
323210008Srdivacky  case Expr::CXXConstCastExprClass:
324224145Sdim  case Expr::ObjCBridgedCastExprClass:
325210008Srdivacky    // Only in C++ can casts be interesting at all.
326210008Srdivacky    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
327210008Srdivacky    return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
328210008Srdivacky
329224145Sdim  case Expr::CXXUnresolvedConstructExprClass:
330224145Sdim    return ClassifyUnnamed(Ctx,
331224145Sdim                      cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
332224145Sdim
333218893Sdim  case Expr::BinaryConditionalOperatorClass: {
334218893Sdim    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
335218893Sdim    const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
336218893Sdim    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
337218893Sdim  }
338218893Sdim
339218893Sdim  case Expr::ConditionalOperatorClass: {
340210008Srdivacky    // Once again, only C++ is interesting.
341210008Srdivacky    if (!Lang.CPlusPlus) return Cl::CL_PRValue;
342218893Sdim    const ConditionalOperator *co = cast<ConditionalOperator>(E);
343218893Sdim    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
344218893Sdim  }
345210008Srdivacky
346210008Srdivacky    // ObjC message sends are effectively function calls, if the target function
347210008Srdivacky    // is known.
348210008Srdivacky  case Expr::ObjCMessageExprClass:
349210008Srdivacky    if (const ObjCMethodDecl *Method =
350210008Srdivacky          cast<ObjCMessageExpr>(E)->getMethodDecl()) {
351221345Sdim      Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
352221345Sdim      return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
353210008Srdivacky    }
354218893Sdim    return Cl::CL_PRValue;
355218893Sdim
356210008Srdivacky    // Some C++ expressions are always class temporaries.
357210008Srdivacky  case Expr::CXXConstructExprClass:
358210008Srdivacky  case Expr::CXXTemporaryObjectExprClass:
359234353Sdim  case Expr::LambdaExprClass:
360263508Sdim  case Expr::CXXStdInitializerListExprClass:
361210008Srdivacky    return Cl::CL_ClassTemporary;
362210008Srdivacky
363218893Sdim  case Expr::VAArgExprClass:
364218893Sdim    return ClassifyUnnamed(Ctx, E->getType());
365234353Sdim
366218893Sdim  case Expr::DesignatedInitExprClass:
367218893Sdim    return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
368234353Sdim
369218893Sdim  case Expr::StmtExprClass: {
370218893Sdim    const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
371218893Sdim    if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
372218893Sdim      return ClassifyUnnamed(Ctx, LastExpr->getType());
373210008Srdivacky    return Cl::CL_PRValue;
374210008Srdivacky  }
375234353Sdim
376218893Sdim  case Expr::CXXUuidofExprClass:
377218893Sdim    return Cl::CL_LValue;
378234353Sdim
379218893Sdim  case Expr::PackExpansionExprClass:
380218893Sdim    return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
381234353Sdim
382224145Sdim  case Expr::MaterializeTemporaryExprClass:
383224145Sdim    return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
384224145Sdim              ? Cl::CL_LValue
385224145Sdim              : Cl::CL_XValue;
386234353Sdim
387234353Sdim  case Expr::InitListExprClass:
388234353Sdim    // An init list can be an lvalue if it is bound to a reference and
389234353Sdim    // contains only one element. In that case, we look at that element
390234353Sdim    // for an exact classification. Init list creation takes care of the
391234353Sdim    // value kind for us, so we only need to fine-tune.
392234353Sdim    if (E->isRValue())
393234353Sdim      return ClassifyExprValueKind(Lang, E, E->getValueKind());
394234353Sdim    assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
395234353Sdim           "Only 1-element init lists can be glvalues.");
396234353Sdim    return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
397218893Sdim  }
398234353Sdim
399218893Sdim  llvm_unreachable("unhandled expression kind in classification");
400210008Srdivacky}
401210008Srdivacky
402210008Srdivacky/// ClassifyDecl - Return the classification of an expression referencing the
403210008Srdivacky/// given declaration.
404210008Srdivackystatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
405210008Srdivacky  // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
406210008Srdivacky  //   function, variable, or data member and a prvalue otherwise.
407210008Srdivacky  // In C, functions are not lvalues.
408210008Srdivacky  // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
409210008Srdivacky  // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
410210008Srdivacky  // special-case this.
411212904Sdim
412212904Sdim  if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
413212904Sdim    return Cl::CL_MemberFunction;
414212904Sdim
415210008Srdivacky  bool islvalue;
416210008Srdivacky  if (const NonTypeTemplateParmDecl *NTTParm =
417210008Srdivacky        dyn_cast<NonTypeTemplateParmDecl>(D))
418210008Srdivacky    islvalue = NTTParm->getType()->isReferenceType();
419210008Srdivacky  else
420210008Srdivacky    islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
421218893Sdim	  isa<IndirectFieldDecl>(D) ||
422234353Sdim      (Ctx.getLangOpts().CPlusPlus &&
423210008Srdivacky        (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
424210008Srdivacky
425210008Srdivacky  return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
426210008Srdivacky}
427210008Srdivacky
428210008Srdivacky/// ClassifyUnnamed - Return the classification of an expression yielding an
429210008Srdivacky/// unnamed value of the given type. This applies in particular to function
430210008Srdivacky/// calls and casts.
431210008Srdivackystatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
432210008Srdivacky  // In C, function calls are always rvalues.
433234353Sdim  if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
434210008Srdivacky
435210008Srdivacky  // C++ [expr.call]p10: A function call is an lvalue if the result type is an
436210008Srdivacky  //   lvalue reference type or an rvalue reference to function type, an xvalue
437226633Sdim  //   if the result type is an rvalue reference to object type, and a prvalue
438210008Srdivacky  //   otherwise.
439210008Srdivacky  if (T->isLValueReferenceType())
440210008Srdivacky    return Cl::CL_LValue;
441210008Srdivacky  const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
442210008Srdivacky  if (!RV) // Could still be a class temporary, though.
443239462Sdim    return ClassifyTemporary(T);
444210008Srdivacky
445210008Srdivacky  return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
446210008Srdivacky}
447210008Srdivacky
448210008Srdivackystatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
449221345Sdim  if (E->getType() == Ctx.UnknownAnyTy)
450221345Sdim    return (isa<FunctionDecl>(E->getMemberDecl())
451221345Sdim              ? Cl::CL_PRValue : Cl::CL_LValue);
452221345Sdim
453210008Srdivacky  // Handle C first, it's easier.
454234353Sdim  if (!Ctx.getLangOpts().CPlusPlus) {
455210008Srdivacky    // C99 6.5.2.3p3
456210008Srdivacky    // For dot access, the expression is an lvalue if the first part is. For
457210008Srdivacky    // arrow access, it always is an lvalue.
458210008Srdivacky    if (E->isArrow())
459210008Srdivacky      return Cl::CL_LValue;
460210008Srdivacky    // ObjC property accesses are not lvalues, but get special treatment.
461218893Sdim    Expr *Base = E->getBase()->IgnoreParens();
462218893Sdim    if (isa<ObjCPropertyRefExpr>(Base))
463210008Srdivacky      return Cl::CL_SubObjCPropertySetting;
464210008Srdivacky    return ClassifyInternal(Ctx, Base);
465210008Srdivacky  }
466210008Srdivacky
467210008Srdivacky  NamedDecl *Member = E->getMemberDecl();
468210008Srdivacky  // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
469210008Srdivacky  // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
470210008Srdivacky  //   E1.E2 is an lvalue.
471210008Srdivacky  if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
472210008Srdivacky    if (Value->getType()->isReferenceType())
473210008Srdivacky      return Cl::CL_LValue;
474210008Srdivacky
475210008Srdivacky  //   Otherwise, one of the following rules applies.
476210008Srdivacky  //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
477210008Srdivacky  if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
478210008Srdivacky    return Cl::CL_LValue;
479210008Srdivacky
480210008Srdivacky  //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
481210008Srdivacky  //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
482210008Srdivacky  //      otherwise, it is a prvalue.
483210008Srdivacky  if (isa<FieldDecl>(Member)) {
484210008Srdivacky    // *E1 is an lvalue
485210008Srdivacky    if (E->isArrow())
486210008Srdivacky      return Cl::CL_LValue;
487218893Sdim    Expr *Base = E->getBase()->IgnoreParenImpCasts();
488218893Sdim    if (isa<ObjCPropertyRefExpr>(Base))
489218893Sdim      return Cl::CL_SubObjCPropertySetting;
490210008Srdivacky    return ClassifyInternal(Ctx, E->getBase());
491210008Srdivacky  }
492210008Srdivacky
493210008Srdivacky  //   -- If E2 is a [...] member function, [...]
494210008Srdivacky  //      -- If it refers to a static member function [...], then E1.E2 is an
495210008Srdivacky  //         lvalue; [...]
496210008Srdivacky  //      -- Otherwise [...] E1.E2 is a prvalue.
497210008Srdivacky  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
498210008Srdivacky    return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
499210008Srdivacky
500210008Srdivacky  //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
501210008Srdivacky  // So is everything else we haven't handled yet.
502210008Srdivacky  return Cl::CL_PRValue;
503210008Srdivacky}
504210008Srdivacky
505210008Srdivackystatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
506234353Sdim  assert(Ctx.getLangOpts().CPlusPlus &&
507210008Srdivacky         "This is only relevant for C++.");
508210008Srdivacky  // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
509218893Sdim  // Except we override this for writes to ObjC properties.
510210008Srdivacky  if (E->isAssignmentOp())
511218893Sdim    return (E->getLHS()->getObjectKind() == OK_ObjCProperty
512218893Sdim              ? Cl::CL_PRValue : Cl::CL_LValue);
513210008Srdivacky
514210008Srdivacky  // C++ [expr.comma]p1: the result is of the same value category as its right
515210008Srdivacky  //   operand, [...].
516212904Sdim  if (E->getOpcode() == BO_Comma)
517210008Srdivacky    return ClassifyInternal(Ctx, E->getRHS());
518210008Srdivacky
519210008Srdivacky  // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
520210008Srdivacky  //   is a pointer to a data member is of the same value category as its first
521210008Srdivacky  //   operand.
522212904Sdim  if (E->getOpcode() == BO_PtrMemD)
523234353Sdim    return (E->getType()->isFunctionType() ||
524234353Sdim            E->hasPlaceholderType(BuiltinType::BoundMember))
525223017Sdim             ? Cl::CL_MemberFunction
526223017Sdim             : ClassifyInternal(Ctx, E->getLHS());
527210008Srdivacky
528210008Srdivacky  // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
529210008Srdivacky  //   second operand is a pointer to data member and a prvalue otherwise.
530212904Sdim  if (E->getOpcode() == BO_PtrMemI)
531234353Sdim    return (E->getType()->isFunctionType() ||
532234353Sdim            E->hasPlaceholderType(BuiltinType::BoundMember))
533223017Sdim             ? Cl::CL_MemberFunction
534223017Sdim             : Cl::CL_LValue;
535210008Srdivacky
536210008Srdivacky  // All other binary operations are prvalues.
537210008Srdivacky  return Cl::CL_PRValue;
538210008Srdivacky}
539210008Srdivacky
540218893Sdimstatic Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
541218893Sdim                                     const Expr *False) {
542234353Sdim  assert(Ctx.getLangOpts().CPlusPlus &&
543210008Srdivacky         "This is only relevant for C++.");
544210008Srdivacky
545210008Srdivacky  // C++ [expr.cond]p2
546210008Srdivacky  //   If either the second or the third operand has type (cv) void, [...]
547210008Srdivacky  //   the result [...] is a prvalue.
548210008Srdivacky  if (True->getType()->isVoidType() || False->getType()->isVoidType())
549210008Srdivacky    return Cl::CL_PRValue;
550210008Srdivacky
551210008Srdivacky  // Note that at this point, we have already performed all conversions
552210008Srdivacky  // according to [expr.cond]p3.
553210008Srdivacky  // C++ [expr.cond]p4: If the second and third operands are glvalues of the
554210008Srdivacky  //   same value category [...], the result is of that [...] value category.
555210008Srdivacky  // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
556210008Srdivacky  Cl::Kinds LCl = ClassifyInternal(Ctx, True),
557210008Srdivacky            RCl = ClassifyInternal(Ctx, False);
558210008Srdivacky  return LCl == RCl ? LCl : Cl::CL_PRValue;
559210008Srdivacky}
560210008Srdivacky
561210008Srdivackystatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
562210008Srdivacky                                       Cl::Kinds Kind, SourceLocation &Loc) {
563210008Srdivacky  // As a general rule, we only care about lvalues. But there are some rvalues
564210008Srdivacky  // for which we want to generate special results.
565210008Srdivacky  if (Kind == Cl::CL_PRValue) {
566210008Srdivacky    // For the sake of better diagnostics, we want to specifically recognize
567210008Srdivacky    // use of the GCC cast-as-lvalue extension.
568218893Sdim    if (const ExplicitCastExpr *CE =
569218893Sdim          dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
570218893Sdim      if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
571218893Sdim        Loc = CE->getExprLoc();
572210008Srdivacky        return Cl::CM_LValueCast;
573210008Srdivacky      }
574210008Srdivacky    }
575210008Srdivacky  }
576210008Srdivacky  if (Kind != Cl::CL_LValue)
577210008Srdivacky    return Cl::CM_RValue;
578210008Srdivacky
579210008Srdivacky  // This is the lvalue case.
580210008Srdivacky  // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
581234353Sdim  if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
582210008Srdivacky    return Cl::CM_Function;
583210008Srdivacky
584210008Srdivacky  // Assignment to a property in ObjC is an implicit setter access. But a
585210008Srdivacky  // setter might not exist.
586218893Sdim  if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
587218893Sdim    if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
588210008Srdivacky      return Cl::CM_NoSetterProperty;
589210008Srdivacky  }
590210008Srdivacky
591210008Srdivacky  CanQualType CT = Ctx.getCanonicalType(E->getType());
592210008Srdivacky  // Const stuff is obviously not modifiable.
593210008Srdivacky  if (CT.isConstQualified())
594210008Srdivacky    return Cl::CM_ConstQualified;
595263508Sdim  if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant)
596263508Sdim    return Cl::CM_ConstQualified;
597234353Sdim
598210008Srdivacky  // Arrays are not modifiable, only their elements are.
599210008Srdivacky  if (CT->isArrayType())
600210008Srdivacky    return Cl::CM_ArrayType;
601210008Srdivacky  // Incomplete types are not modifiable.
602210008Srdivacky  if (CT->isIncompleteType())
603210008Srdivacky    return Cl::CM_IncompleteType;
604210008Srdivacky
605210008Srdivacky  // Records with any const fields (recursively) are not modifiable.
606210008Srdivacky  if (const RecordType *R = CT->getAs<RecordType>()) {
607218893Sdim    assert((E->getObjectKind() == OK_ObjCProperty ||
608234353Sdim            !Ctx.getLangOpts().CPlusPlus) &&
609210008Srdivacky           "C++ struct assignment should be resolved by the "
610210008Srdivacky           "copy assignment operator.");
611210008Srdivacky    if (R->hasConstFields())
612210008Srdivacky      return Cl::CM_ConstQualified;
613210008Srdivacky  }
614210008Srdivacky
615210008Srdivacky  return Cl::CM_Modifiable;
616210008Srdivacky}
617210008Srdivacky
618218893SdimExpr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
619210008Srdivacky  Classification VC = Classify(Ctx);
620210008Srdivacky  switch (VC.getKind()) {
621210008Srdivacky  case Cl::CL_LValue: return LV_Valid;
622210008Srdivacky  case Cl::CL_XValue: return LV_InvalidExpression;
623210008Srdivacky  case Cl::CL_Function: return LV_NotObjectType;
624221345Sdim  case Cl::CL_Void: return LV_InvalidExpression;
625221345Sdim  case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
626210008Srdivacky  case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
627210008Srdivacky  case Cl::CL_MemberFunction: return LV_MemberFunction;
628210008Srdivacky  case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
629210008Srdivacky  case Cl::CL_ClassTemporary: return LV_ClassTemporary;
630239462Sdim  case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
631221345Sdim  case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
632210008Srdivacky  case Cl::CL_PRValue: return LV_InvalidExpression;
633210008Srdivacky  }
634210008Srdivacky  llvm_unreachable("Unhandled kind");
635210008Srdivacky}
636210008Srdivacky
637210008SrdivackyExpr::isModifiableLvalueResult
638210008SrdivackyExpr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
639210008Srdivacky  SourceLocation dummy;
640210008Srdivacky  Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
641210008Srdivacky  switch (VC.getKind()) {
642210008Srdivacky  case Cl::CL_LValue: break;
643210008Srdivacky  case Cl::CL_XValue: return MLV_InvalidExpression;
644210008Srdivacky  case Cl::CL_Function: return MLV_NotObjectType;
645221345Sdim  case Cl::CL_Void: return MLV_InvalidExpression;
646221345Sdim  case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
647210008Srdivacky  case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
648210008Srdivacky  case Cl::CL_MemberFunction: return MLV_MemberFunction;
649210008Srdivacky  case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
650210008Srdivacky  case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
651239462Sdim  case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;
652221345Sdim  case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
653210008Srdivacky  case Cl::CL_PRValue:
654210008Srdivacky    return VC.getModifiable() == Cl::CM_LValueCast ?
655210008Srdivacky      MLV_LValueCast : MLV_InvalidExpression;
656210008Srdivacky  }
657210008Srdivacky  assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
658210008Srdivacky  switch (VC.getModifiable()) {
659210008Srdivacky  case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
660210008Srdivacky  case Cl::CM_Modifiable: return MLV_Valid;
661210008Srdivacky  case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
662210008Srdivacky  case Cl::CM_Function: return MLV_NotObjectType;
663210008Srdivacky  case Cl::CM_LValueCast:
664210008Srdivacky    llvm_unreachable("CM_LValueCast and CL_LValue don't match");
665210008Srdivacky  case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
666210008Srdivacky  case Cl::CM_ConstQualified: return MLV_ConstQualified;
667210008Srdivacky  case Cl::CM_ArrayType: return MLV_ArrayType;
668210008Srdivacky  case Cl::CM_IncompleteType: return MLV_IncompleteType;
669210008Srdivacky  }
670210008Srdivacky  llvm_unreachable("Unhandled modifiable type");
671210008Srdivacky}
672