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: 158218893Sdim case Expr::IntegerLiteralClass: 159218893Sdim case Expr::CharacterLiteralClass: 160218893Sdim case Expr::AddrLabelExprClass: 161218893Sdim case Expr::CXXDeleteExprClass: 162218893Sdim case Expr::ImplicitValueInitExprClass: 163218893Sdim case Expr::BlockExprClass: 164218893Sdim case Expr::FloatingLiteralClass: 165218893Sdim case Expr::CXXNoexceptExprClass: 166218893Sdim case Expr::CXXScalarValueInitExprClass: 167218893Sdim case Expr::UnaryTypeTraitExprClass: 168218893Sdim case Expr::BinaryTypeTraitExprClass: 169234353Sdim case Expr::TypeTraitExprClass: 170221345Sdim case Expr::ArrayTypeTraitExprClass: 171221345Sdim case Expr::ExpressionTraitExprClass: 172218893Sdim case Expr::ObjCSelectorExprClass: 173218893Sdim case Expr::ObjCProtocolExprClass: 174218893Sdim case Expr::ObjCStringLiteralClass: 175239462Sdim case Expr::ObjCBoxedExprClass: 176234353Sdim case Expr::ObjCArrayLiteralClass: 177234353Sdim case Expr::ObjCDictionaryLiteralClass: 178234353Sdim case Expr::ObjCBoolLiteralExprClass: 179218893Sdim case Expr::ParenListExprClass: 180218893Sdim case Expr::SizeOfPackExprClass: 181218893Sdim case Expr::SubstNonTypeTemplateParmPackExprClass: 182223017Sdim case Expr::AsTypeExprClass: 183224145Sdim case Expr::ObjCIndirectCopyRestoreExprClass: 184226633Sdim case Expr::AtomicExprClass: 185218893Sdim return Cl::CL_PRValue; 186218893Sdim 187210008Srdivacky // Next come the complicated cases. 188224145Sdim case Expr::SubstNonTypeTemplateParmExprClass: 189224145Sdim return ClassifyInternal(Ctx, 190224145Sdim cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 191210008Srdivacky 192210008Srdivacky // C++ [expr.sub]p1: The result is an lvalue of type "T". 193210008Srdivacky // However, subscripting vector types is more like member access. 194210008Srdivacky case Expr::ArraySubscriptExprClass: 195210008Srdivacky if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) 196210008Srdivacky return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); 197210008Srdivacky return Cl::CL_LValue; 198210008Srdivacky 199210008Srdivacky // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a 200210008Srdivacky // function or variable and a prvalue otherwise. 201210008Srdivacky case Expr::DeclRefExprClass: 202221345Sdim if (E->getType() == Ctx.UnknownAnyTy) 203221345Sdim return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) 204221345Sdim ? Cl::CL_PRValue : Cl::CL_LValue; 205210008Srdivacky return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); 206210008Srdivacky 207210008Srdivacky // Member access is complex. 208210008Srdivacky case Expr::MemberExprClass: 209210008Srdivacky return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); 210210008Srdivacky 211210008Srdivacky case Expr::UnaryOperatorClass: 212210008Srdivacky switch (cast<UnaryOperator>(E)->getOpcode()) { 213210008Srdivacky // C++ [expr.unary.op]p1: The unary * operator performs indirection: 214210008Srdivacky // [...] the result is an lvalue referring to the object or function 215210008Srdivacky // to which the expression points. 216212904Sdim case UO_Deref: 217210008Srdivacky return Cl::CL_LValue; 218210008Srdivacky 219210008Srdivacky // GNU extensions, simply look through them. 220212904Sdim case UO_Extension: 221210008Srdivacky return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); 222210008Srdivacky 223218893Sdim // Treat _Real and _Imag basically as if they were member 224218893Sdim // expressions: l-value only if the operand is a true l-value. 225218893Sdim case UO_Real: 226218893Sdim case UO_Imag: { 227218893Sdim const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 228218893Sdim Cl::Kinds K = ClassifyInternal(Ctx, Op); 229218893Sdim if (K != Cl::CL_LValue) return K; 230218893Sdim 231218893Sdim if (isa<ObjCPropertyRefExpr>(Op)) 232218893Sdim return Cl::CL_SubObjCPropertySetting; 233218893Sdim return Cl::CL_LValue; 234218893Sdim } 235218893Sdim 236210008Srdivacky // C++ [expr.pre.incr]p1: The result is the updated operand; it is an 237210008Srdivacky // lvalue, [...] 238210008Srdivacky // Not so in C. 239212904Sdim case UO_PreInc: 240212904Sdim case UO_PreDec: 241210008Srdivacky return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; 242210008Srdivacky 243210008Srdivacky default: 244210008Srdivacky return Cl::CL_PRValue; 245210008Srdivacky } 246210008Srdivacky 247218893Sdim case Expr::OpaqueValueExprClass: 248234353Sdim return ClassifyExprValueKind(Lang, E, E->getValueKind()); 249234353Sdim 250234353Sdim // Pseudo-object expressions can produce l-values with reference magic. 251234353Sdim case Expr::PseudoObjectExprClass: 252218893Sdim return ClassifyExprValueKind(Lang, E, 253234353Sdim cast<PseudoObjectExpr>(E)->getValueKind()); 254218893Sdim 255210008Srdivacky // Implicit casts are lvalues if they're lvalue casts. Other than that, we 256210008Srdivacky // only specifically record class temporaries. 257210008Srdivacky case Expr::ImplicitCastExprClass: 258234353Sdim return ClassifyExprValueKind(Lang, E, E->getValueKind()); 259210008Srdivacky 260210008Srdivacky // C++ [expr.prim.general]p4: The presence of parentheses does not affect 261210008Srdivacky // whether the expression is an lvalue. 262210008Srdivacky case Expr::ParenExprClass: 263210008Srdivacky return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); 264210008Srdivacky 265234353Sdim // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, 266221345Sdim // or a void expression if its result expression is, respectively, an 267221345Sdim // lvalue, a function designator, or a void expression. 268221345Sdim case Expr::GenericSelectionExprClass: 269221345Sdim if (cast<GenericSelectionExpr>(E)->isResultDependent()) 270221345Sdim return Cl::CL_PRValue; 271221345Sdim return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); 272221345Sdim 273210008Srdivacky case Expr::BinaryOperatorClass: 274210008Srdivacky case Expr::CompoundAssignOperatorClass: 275210008Srdivacky // C doesn't have any binary expressions that are lvalues. 276210008Srdivacky if (Lang.CPlusPlus) 277210008Srdivacky return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); 278210008Srdivacky return Cl::CL_PRValue; 279210008Srdivacky 280210008Srdivacky case Expr::CallExprClass: 281210008Srdivacky case Expr::CXXOperatorCallExprClass: 282210008Srdivacky case Expr::CXXMemberCallExprClass: 283234353Sdim case Expr::UserDefinedLiteralClass: 284218893Sdim case Expr::CUDAKernelCallExprClass: 285210008Srdivacky return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType()); 286210008Srdivacky 287210008Srdivacky // __builtin_choose_expr is equivalent to the chosen expression. 288210008Srdivacky case Expr::ChooseExprClass: 289210008Srdivacky return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx)); 290210008Srdivacky 291210008Srdivacky // Extended vector element access is an lvalue unless there are duplicates 292210008Srdivacky // in the shuffle expression. 293210008Srdivacky case Expr::ExtVectorElementExprClass: 294210008Srdivacky return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ? 295210008Srdivacky Cl::CL_DuplicateVectorComponents : Cl::CL_LValue; 296210008Srdivacky 297210008Srdivacky // Simply look at the actual default argument. 298210008Srdivacky case Expr::CXXDefaultArgExprClass: 299210008Srdivacky return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); 300210008Srdivacky 301251662Sdim // Same idea for default initializers. 302251662Sdim case Expr::CXXDefaultInitExprClass: 303251662Sdim return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); 304251662Sdim 305210008Srdivacky // Same idea for temporary binding. 306210008Srdivacky case Expr::CXXBindTemporaryExprClass: 307210008Srdivacky return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 308210008Srdivacky 309218893Sdim // And the cleanups guard. 310218893Sdim case Expr::ExprWithCleanupsClass: 311218893Sdim return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); 312210008Srdivacky 313210008Srdivacky // Casts depend completely on the target type. All casts work the same. 314210008Srdivacky case Expr::CStyleCastExprClass: 315210008Srdivacky case Expr::CXXFunctionalCastExprClass: 316210008Srdivacky case Expr::CXXStaticCastExprClass: 317210008Srdivacky case Expr::CXXDynamicCastExprClass: 318210008Srdivacky case Expr::CXXReinterpretCastExprClass: 319210008Srdivacky case Expr::CXXConstCastExprClass: 320224145Sdim case Expr::ObjCBridgedCastExprClass: 321210008Srdivacky // Only in C++ can casts be interesting at all. 322210008Srdivacky if (!Lang.CPlusPlus) return Cl::CL_PRValue; 323210008Srdivacky return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); 324210008Srdivacky 325224145Sdim case Expr::CXXUnresolvedConstructExprClass: 326224145Sdim return ClassifyUnnamed(Ctx, 327224145Sdim cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); 328224145Sdim 329218893Sdim case Expr::BinaryConditionalOperatorClass: { 330218893Sdim if (!Lang.CPlusPlus) return Cl::CL_PRValue; 331218893Sdim const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E); 332218893Sdim return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 333218893Sdim } 334218893Sdim 335218893Sdim case Expr::ConditionalOperatorClass: { 336210008Srdivacky // Once again, only C++ is interesting. 337210008Srdivacky if (!Lang.CPlusPlus) return Cl::CL_PRValue; 338218893Sdim const ConditionalOperator *co = cast<ConditionalOperator>(E); 339218893Sdim return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 340218893Sdim } 341210008Srdivacky 342210008Srdivacky // ObjC message sends are effectively function calls, if the target function 343210008Srdivacky // is known. 344210008Srdivacky case Expr::ObjCMessageExprClass: 345210008Srdivacky if (const ObjCMethodDecl *Method = 346210008Srdivacky cast<ObjCMessageExpr>(E)->getMethodDecl()) { 347221345Sdim Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType()); 348221345Sdim return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; 349210008Srdivacky } 350218893Sdim return Cl::CL_PRValue; 351218893Sdim 352210008Srdivacky // Some C++ expressions are always class temporaries. 353210008Srdivacky case Expr::CXXConstructExprClass: 354210008Srdivacky case Expr::CXXTemporaryObjectExprClass: 355234353Sdim case Expr::LambdaExprClass: 356210008Srdivacky return Cl::CL_ClassTemporary; 357210008Srdivacky 358218893Sdim case Expr::VAArgExprClass: 359218893Sdim return ClassifyUnnamed(Ctx, E->getType()); 360234353Sdim 361218893Sdim case Expr::DesignatedInitExprClass: 362218893Sdim return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); 363234353Sdim 364218893Sdim case Expr::StmtExprClass: { 365218893Sdim const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); 366218893Sdim if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) 367218893Sdim return ClassifyUnnamed(Ctx, LastExpr->getType()); 368210008Srdivacky return Cl::CL_PRValue; 369210008Srdivacky } 370234353Sdim 371218893Sdim case Expr::CXXUuidofExprClass: 372218893Sdim return Cl::CL_LValue; 373234353Sdim 374218893Sdim case Expr::PackExpansionExprClass: 375218893Sdim return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); 376234353Sdim 377224145Sdim case Expr::MaterializeTemporaryExprClass: 378224145Sdim return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() 379224145Sdim ? Cl::CL_LValue 380224145Sdim : Cl::CL_XValue; 381234353Sdim 382234353Sdim case Expr::InitListExprClass: 383234353Sdim // An init list can be an lvalue if it is bound to a reference and 384234353Sdim // contains only one element. In that case, we look at that element 385234353Sdim // for an exact classification. Init list creation takes care of the 386234353Sdim // value kind for us, so we only need to fine-tune. 387234353Sdim if (E->isRValue()) 388234353Sdim return ClassifyExprValueKind(Lang, E, E->getValueKind()); 389234353Sdim assert(cast<InitListExpr>(E)->getNumInits() == 1 && 390234353Sdim "Only 1-element init lists can be glvalues."); 391234353Sdim return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); 392218893Sdim } 393234353Sdim 394218893Sdim llvm_unreachable("unhandled expression kind in classification"); 395210008Srdivacky} 396210008Srdivacky 397210008Srdivacky/// ClassifyDecl - Return the classification of an expression referencing the 398210008Srdivacky/// given declaration. 399210008Srdivackystatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { 400210008Srdivacky // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a 401210008Srdivacky // function, variable, or data member and a prvalue otherwise. 402210008Srdivacky // In C, functions are not lvalues. 403210008Srdivacky // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an 404210008Srdivacky // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to 405210008Srdivacky // special-case this. 406212904Sdim 407212904Sdim if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 408212904Sdim return Cl::CL_MemberFunction; 409212904Sdim 410210008Srdivacky bool islvalue; 411210008Srdivacky if (const NonTypeTemplateParmDecl *NTTParm = 412210008Srdivacky dyn_cast<NonTypeTemplateParmDecl>(D)) 413210008Srdivacky islvalue = NTTParm->getType()->isReferenceType(); 414210008Srdivacky else 415210008Srdivacky islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || 416218893Sdim isa<IndirectFieldDecl>(D) || 417234353Sdim (Ctx.getLangOpts().CPlusPlus && 418210008Srdivacky (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D))); 419210008Srdivacky 420210008Srdivacky return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; 421210008Srdivacky} 422210008Srdivacky 423210008Srdivacky/// ClassifyUnnamed - Return the classification of an expression yielding an 424210008Srdivacky/// unnamed value of the given type. This applies in particular to function 425210008Srdivacky/// calls and casts. 426210008Srdivackystatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { 427210008Srdivacky // In C, function calls are always rvalues. 428234353Sdim if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; 429210008Srdivacky 430210008Srdivacky // C++ [expr.call]p10: A function call is an lvalue if the result type is an 431210008Srdivacky // lvalue reference type or an rvalue reference to function type, an xvalue 432226633Sdim // if the result type is an rvalue reference to object type, and a prvalue 433210008Srdivacky // otherwise. 434210008Srdivacky if (T->isLValueReferenceType()) 435210008Srdivacky return Cl::CL_LValue; 436210008Srdivacky const RValueReferenceType *RV = T->getAs<RValueReferenceType>(); 437210008Srdivacky if (!RV) // Could still be a class temporary, though. 438239462Sdim return ClassifyTemporary(T); 439210008Srdivacky 440210008Srdivacky return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; 441210008Srdivacky} 442210008Srdivacky 443210008Srdivackystatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { 444221345Sdim if (E->getType() == Ctx.UnknownAnyTy) 445221345Sdim return (isa<FunctionDecl>(E->getMemberDecl()) 446221345Sdim ? Cl::CL_PRValue : Cl::CL_LValue); 447221345Sdim 448210008Srdivacky // Handle C first, it's easier. 449234353Sdim if (!Ctx.getLangOpts().CPlusPlus) { 450210008Srdivacky // C99 6.5.2.3p3 451210008Srdivacky // For dot access, the expression is an lvalue if the first part is. For 452210008Srdivacky // arrow access, it always is an lvalue. 453210008Srdivacky if (E->isArrow()) 454210008Srdivacky return Cl::CL_LValue; 455210008Srdivacky // ObjC property accesses are not lvalues, but get special treatment. 456218893Sdim Expr *Base = E->getBase()->IgnoreParens(); 457218893Sdim if (isa<ObjCPropertyRefExpr>(Base)) 458210008Srdivacky return Cl::CL_SubObjCPropertySetting; 459210008Srdivacky return ClassifyInternal(Ctx, Base); 460210008Srdivacky } 461210008Srdivacky 462210008Srdivacky NamedDecl *Member = E->getMemberDecl(); 463210008Srdivacky // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. 464210008Srdivacky // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then 465210008Srdivacky // E1.E2 is an lvalue. 466210008Srdivacky if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 467210008Srdivacky if (Value->getType()->isReferenceType()) 468210008Srdivacky return Cl::CL_LValue; 469210008Srdivacky 470210008Srdivacky // Otherwise, one of the following rules applies. 471210008Srdivacky // -- If E2 is a static member [...] then E1.E2 is an lvalue. 472210008Srdivacky if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) 473210008Srdivacky return Cl::CL_LValue; 474210008Srdivacky 475210008Srdivacky // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then 476210008Srdivacky // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; 477210008Srdivacky // otherwise, it is a prvalue. 478210008Srdivacky if (isa<FieldDecl>(Member)) { 479210008Srdivacky // *E1 is an lvalue 480210008Srdivacky if (E->isArrow()) 481210008Srdivacky return Cl::CL_LValue; 482218893Sdim Expr *Base = E->getBase()->IgnoreParenImpCasts(); 483218893Sdim if (isa<ObjCPropertyRefExpr>(Base)) 484218893Sdim return Cl::CL_SubObjCPropertySetting; 485210008Srdivacky return ClassifyInternal(Ctx, E->getBase()); 486210008Srdivacky } 487210008Srdivacky 488210008Srdivacky // -- If E2 is a [...] member function, [...] 489210008Srdivacky // -- If it refers to a static member function [...], then E1.E2 is an 490210008Srdivacky // lvalue; [...] 491210008Srdivacky // -- Otherwise [...] E1.E2 is a prvalue. 492210008Srdivacky if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 493210008Srdivacky return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; 494210008Srdivacky 495210008Srdivacky // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. 496210008Srdivacky // So is everything else we haven't handled yet. 497210008Srdivacky return Cl::CL_PRValue; 498210008Srdivacky} 499210008Srdivacky 500210008Srdivackystatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { 501234353Sdim assert(Ctx.getLangOpts().CPlusPlus && 502210008Srdivacky "This is only relevant for C++."); 503210008Srdivacky // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. 504218893Sdim // Except we override this for writes to ObjC properties. 505210008Srdivacky if (E->isAssignmentOp()) 506218893Sdim return (E->getLHS()->getObjectKind() == OK_ObjCProperty 507218893Sdim ? Cl::CL_PRValue : Cl::CL_LValue); 508210008Srdivacky 509210008Srdivacky // C++ [expr.comma]p1: the result is of the same value category as its right 510210008Srdivacky // operand, [...]. 511212904Sdim if (E->getOpcode() == BO_Comma) 512210008Srdivacky return ClassifyInternal(Ctx, E->getRHS()); 513210008Srdivacky 514210008Srdivacky // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand 515210008Srdivacky // is a pointer to a data member is of the same value category as its first 516210008Srdivacky // operand. 517212904Sdim if (E->getOpcode() == BO_PtrMemD) 518234353Sdim return (E->getType()->isFunctionType() || 519234353Sdim E->hasPlaceholderType(BuiltinType::BoundMember)) 520223017Sdim ? Cl::CL_MemberFunction 521223017Sdim : ClassifyInternal(Ctx, E->getLHS()); 522210008Srdivacky 523210008Srdivacky // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its 524210008Srdivacky // second operand is a pointer to data member and a prvalue otherwise. 525212904Sdim if (E->getOpcode() == BO_PtrMemI) 526234353Sdim return (E->getType()->isFunctionType() || 527234353Sdim E->hasPlaceholderType(BuiltinType::BoundMember)) 528223017Sdim ? Cl::CL_MemberFunction 529223017Sdim : Cl::CL_LValue; 530210008Srdivacky 531210008Srdivacky // All other binary operations are prvalues. 532210008Srdivacky return Cl::CL_PRValue; 533210008Srdivacky} 534210008Srdivacky 535218893Sdimstatic Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, 536218893Sdim const Expr *False) { 537234353Sdim assert(Ctx.getLangOpts().CPlusPlus && 538210008Srdivacky "This is only relevant for C++."); 539210008Srdivacky 540210008Srdivacky // C++ [expr.cond]p2 541210008Srdivacky // If either the second or the third operand has type (cv) void, [...] 542210008Srdivacky // the result [...] is a prvalue. 543210008Srdivacky if (True->getType()->isVoidType() || False->getType()->isVoidType()) 544210008Srdivacky return Cl::CL_PRValue; 545210008Srdivacky 546210008Srdivacky // Note that at this point, we have already performed all conversions 547210008Srdivacky // according to [expr.cond]p3. 548210008Srdivacky // C++ [expr.cond]p4: If the second and third operands are glvalues of the 549210008Srdivacky // same value category [...], the result is of that [...] value category. 550210008Srdivacky // C++ [expr.cond]p5: Otherwise, the result is a prvalue. 551210008Srdivacky Cl::Kinds LCl = ClassifyInternal(Ctx, True), 552210008Srdivacky RCl = ClassifyInternal(Ctx, False); 553210008Srdivacky return LCl == RCl ? LCl : Cl::CL_PRValue; 554210008Srdivacky} 555210008Srdivacky 556210008Srdivackystatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 557210008Srdivacky Cl::Kinds Kind, SourceLocation &Loc) { 558210008Srdivacky // As a general rule, we only care about lvalues. But there are some rvalues 559210008Srdivacky // for which we want to generate special results. 560210008Srdivacky if (Kind == Cl::CL_PRValue) { 561210008Srdivacky // For the sake of better diagnostics, we want to specifically recognize 562210008Srdivacky // use of the GCC cast-as-lvalue extension. 563218893Sdim if (const ExplicitCastExpr *CE = 564218893Sdim dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) { 565218893Sdim if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { 566218893Sdim Loc = CE->getExprLoc(); 567210008Srdivacky return Cl::CM_LValueCast; 568210008Srdivacky } 569210008Srdivacky } 570210008Srdivacky } 571210008Srdivacky if (Kind != Cl::CL_LValue) 572210008Srdivacky return Cl::CM_RValue; 573210008Srdivacky 574210008Srdivacky // This is the lvalue case. 575210008Srdivacky // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) 576234353Sdim if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) 577210008Srdivacky return Cl::CM_Function; 578210008Srdivacky 579210008Srdivacky // Assignment to a property in ObjC is an implicit setter access. But a 580210008Srdivacky // setter might not exist. 581218893Sdim if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) { 582218893Sdim if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0) 583210008Srdivacky return Cl::CM_NoSetterProperty; 584210008Srdivacky } 585210008Srdivacky 586210008Srdivacky CanQualType CT = Ctx.getCanonicalType(E->getType()); 587210008Srdivacky // Const stuff is obviously not modifiable. 588210008Srdivacky if (CT.isConstQualified()) 589210008Srdivacky return Cl::CM_ConstQualified; 590234353Sdim 591210008Srdivacky // Arrays are not modifiable, only their elements are. 592210008Srdivacky if (CT->isArrayType()) 593210008Srdivacky return Cl::CM_ArrayType; 594210008Srdivacky // Incomplete types are not modifiable. 595210008Srdivacky if (CT->isIncompleteType()) 596210008Srdivacky return Cl::CM_IncompleteType; 597210008Srdivacky 598210008Srdivacky // Records with any const fields (recursively) are not modifiable. 599210008Srdivacky if (const RecordType *R = CT->getAs<RecordType>()) { 600218893Sdim assert((E->getObjectKind() == OK_ObjCProperty || 601234353Sdim !Ctx.getLangOpts().CPlusPlus) && 602210008Srdivacky "C++ struct assignment should be resolved by the " 603210008Srdivacky "copy assignment operator."); 604210008Srdivacky if (R->hasConstFields()) 605210008Srdivacky return Cl::CM_ConstQualified; 606210008Srdivacky } 607210008Srdivacky 608210008Srdivacky return Cl::CM_Modifiable; 609210008Srdivacky} 610210008Srdivacky 611218893SdimExpr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { 612210008Srdivacky Classification VC = Classify(Ctx); 613210008Srdivacky switch (VC.getKind()) { 614210008Srdivacky case Cl::CL_LValue: return LV_Valid; 615210008Srdivacky case Cl::CL_XValue: return LV_InvalidExpression; 616210008Srdivacky case Cl::CL_Function: return LV_NotObjectType; 617221345Sdim case Cl::CL_Void: return LV_InvalidExpression; 618221345Sdim case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; 619210008Srdivacky case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; 620210008Srdivacky case Cl::CL_MemberFunction: return LV_MemberFunction; 621210008Srdivacky case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; 622210008Srdivacky case Cl::CL_ClassTemporary: return LV_ClassTemporary; 623239462Sdim case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; 624221345Sdim case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; 625210008Srdivacky case Cl::CL_PRValue: return LV_InvalidExpression; 626210008Srdivacky } 627210008Srdivacky llvm_unreachable("Unhandled kind"); 628210008Srdivacky} 629210008Srdivacky 630210008SrdivackyExpr::isModifiableLvalueResult 631210008SrdivackyExpr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { 632210008Srdivacky SourceLocation dummy; 633210008Srdivacky Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); 634210008Srdivacky switch (VC.getKind()) { 635210008Srdivacky case Cl::CL_LValue: break; 636210008Srdivacky case Cl::CL_XValue: return MLV_InvalidExpression; 637210008Srdivacky case Cl::CL_Function: return MLV_NotObjectType; 638221345Sdim case Cl::CL_Void: return MLV_InvalidExpression; 639221345Sdim case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; 640210008Srdivacky case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 641210008Srdivacky case Cl::CL_MemberFunction: return MLV_MemberFunction; 642210008Srdivacky case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; 643210008Srdivacky case Cl::CL_ClassTemporary: return MLV_ClassTemporary; 644239462Sdim case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; 645221345Sdim case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; 646210008Srdivacky case Cl::CL_PRValue: 647210008Srdivacky return VC.getModifiable() == Cl::CM_LValueCast ? 648210008Srdivacky MLV_LValueCast : MLV_InvalidExpression; 649210008Srdivacky } 650210008Srdivacky assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); 651210008Srdivacky switch (VC.getModifiable()) { 652210008Srdivacky case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); 653210008Srdivacky case Cl::CM_Modifiable: return MLV_Valid; 654210008Srdivacky case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); 655210008Srdivacky case Cl::CM_Function: return MLV_NotObjectType; 656210008Srdivacky case Cl::CM_LValueCast: 657210008Srdivacky llvm_unreachable("CM_LValueCast and CL_LValue don't match"); 658210008Srdivacky case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; 659210008Srdivacky case Cl::CM_ConstQualified: return MLV_ConstQualified; 660210008Srdivacky case Cl::CM_ArrayType: return MLV_ArrayType; 661210008Srdivacky case Cl::CM_IncompleteType: return MLV_IncompleteType; 662210008Srdivacky } 663210008Srdivacky llvm_unreachable("Unhandled modifiable type"); 664210008Srdivacky} 665