12111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl//===--- ExprClassification.cpp - Expression AST Node Implementation ------===// 22111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// 32111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// The LLVM Compiler Infrastructure 42111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// 52111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// This file is distributed under the University of Illinois Open Source 62111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// License. See LICENSE.TXT for details. 72111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// 82111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl//===----------------------------------------------------------------------===// 92111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// 102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// This file implements Expr::classify. 112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl// 122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl//===----------------------------------------------------------------------===// 132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl#include "clang/AST/Expr.h" 152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl#include "clang/AST/ASTContext.h" 162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl#include "clang/AST/DeclCXX.h" 1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclObjC.h" 182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl#include "clang/AST/DeclTemplate.h" 1955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ExprCXX.h" 2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ExprObjC.h" 2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/ErrorHandling.h" 222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlusing namespace clang; 232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redltypedef Expr::Classification Cl; 252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E); 272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D); 282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T); 292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E); 302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E); 312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyConditional(ASTContext &Ctx, 3256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const Expr *trueExpr, 3356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const Expr *falseExpr); 342111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds Kind, SourceLocation &Loc); 362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 372111c855343a0530e236bf0862358ec8d67b28f3Sebastian RedlCl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const { 382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl assert(!TR->isReferenceType() && "Expressions can't have reference type."); 392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds kind = ClassifyInternal(Ctx, this); 412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C99 6.3.2.1: An lvalue is an expression with an object type or an 422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // incomplete type other than void. 434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ctx.getLangOpts().CPlusPlus) { 442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Thus, no functions. 452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (TR->isFunctionType() || TR == Ctx.OverloadTy) 462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl kind = Cl::CL_Function; 472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // No void either, but qualified void is OK because it is "other than void". 481c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne // Void "lvalues" are classified as addressable void values, which are void 491c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne // expressions whose address can be taken. 501c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne else if (TR->isVoidType() && !TR.hasQualifiers()) 511c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void); 522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 540943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall // Enable this assertion for testing. 550943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall switch (kind) { 560943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break; 570943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break; 580943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_Function: 590943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_Void: 601c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_AddressableVoid: 610943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_DuplicateVectorComponents: 620943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_MemberFunction: 630943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_SubObjCPropertySetting: 640943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_ClassTemporary: 6536d02af300a207242f0486b4255420d8be796b21Richard Smith case Cl::CL_ArrayTemporary: 66077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian case Cl::CL_ObjCMessageRValue: 670943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break; 680943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall } 690943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall 702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::ModifiableType modifiable = Cl::CM_Untested; 712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Loc) 722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl modifiable = IsModifiable(Ctx, this, kind, *Loc); 732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Classification(kind, modifiable); 742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 7636d02af300a207242f0486b4255420d8be796b21Richard Smith/// Classify an expression which creates a temporary, based on its type. 7736d02af300a207242f0486b4255420d8be796b21Richard Smithstatic Cl::Kinds ClassifyTemporary(QualType T) { 7836d02af300a207242f0486b4255420d8be796b21Richard Smith if (T->isRecordType()) 7936d02af300a207242f0486b4255420d8be796b21Richard Smith return Cl::CL_ClassTemporary; 8036d02af300a207242f0486b4255420d8be796b21Richard Smith if (T->isArrayType()) 8136d02af300a207242f0486b4255420d8be796b21Richard Smith return Cl::CL_ArrayTemporary; 8236d02af300a207242f0486b4255420d8be796b21Richard Smith 8336d02af300a207242f0486b4255420d8be796b21Richard Smith // No special classification: these don't behave differently from normal 8436d02af300a207242f0486b4255420d8be796b21Richard Smith // prvalues. 8536d02af300a207242f0486b4255420d8be796b21Richard Smith return Cl::CL_PRValue; 8636d02af300a207242f0486b4255420d8be796b21Richard Smith} 8736d02af300a207242f0486b4255420d8be796b21Richard Smith 88821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smithstatic Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, 89821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith const Expr *E, 90821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith ExprValueKind Kind) { 91821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith switch (Kind) { 92821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith case VK_RValue: 93821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue; 94821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith case VK_LValue: 95821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith return Cl::CL_LValue; 96821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith case VK_XValue: 97821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith return Cl::CL_XValue; 98821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith } 99821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith llvm_unreachable("Invalid value category of implicit cast."); 100821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith} 101821b93eec8b58a3e320ef34e7c98906ab61cf8c3Richard Smith 1022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { 1032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // This function takes the first stab at classifying expressions. 1044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &Lang = Ctx.getLangOpts(); 1052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 1062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (E->getStmtClass()) { 1074178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Stmt::NoStmtClass: 10863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Kind) 1094178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor#define STMT(Kind, Base) case Expr::Kind##Class: 1104178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor#define EXPR(Kind, Base) 1114178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor#include "clang/AST/StmtNodes.inc" 1124178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor llvm_unreachable("cannot classify a statement"); 11313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 11413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // First come the expressions that are always lvalues, unconditionally. 1152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCIsaExprClass: 1162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p1: A string literal is an lvalue. 1172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::StringLiteralClass: 1182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // @encode is equivalent to its string 1192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCEncodeExprClass: 1202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // __func__ and friends are too. 1212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::PredefinedExprClass: 1222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Property references are lvalues 123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCSubscriptRefExprClass: 1242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCPropertyRefExprClass: 1252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of... 1262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXTypeidExprClass: 127176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Unresolved lookups and uncorrected typos get classified as lvalues. 1282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // FIXME: Is this wise? Should they get their own kind? 1292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::UnresolvedLookupExprClass: 1302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::UnresolvedMemberExprClass: 131176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case Expr::TypoExprClass: 1324178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXDependentScopeMemberExprClass: 1334178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::DependentScopeDeclRefExprClass: 1342111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // ObjC instance variables are lvalues 1352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // FIXME: ObjC++0x might have different rules 1362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCIvarRefExprClass: 1379a4db032ecd991626d236a502e770126db32bd31Richard Smith case Expr::FunctionParmPackExprClass: 13876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall case Expr::MSPropertyRefExprClass: 139a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case Expr::MSPropertySubscriptExprClass: 140a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case Expr::OMPArraySectionExprClass: 1414178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_LValue; 14213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 1432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C99 6.5.2.5p5 says that compound literals are lvalues. 14436d02af300a207242f0486b4255420d8be796b21Richard Smith // In C++, they're prvalue temporaries. 1452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CompoundLiteralExprClass: 14636d02af300a207242f0486b4255420d8be796b21Richard Smith return Ctx.getLangOpts().CPlusPlus ? ClassifyTemporary(E->getType()) 14736d02af300a207242f0486b4255420d8be796b21Richard Smith : Cl::CL_LValue; 1484178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor 1494178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor // Expressions that are prvalues. 1504178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXBoolLiteralExprClass: 1514178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXPseudoDestructorExprClass: 152f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case Expr::UnaryExprOrTypeTraitExprClass: 1534178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNewExprClass: 1544178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXThisExprClass: 1554178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNullPtrLiteralExprClass: 1564178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ImaginaryLiteralClass: 1574178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::GNUNullExprClass: 1584178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::OffsetOfExprClass: 1594178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXThrowExprClass: 1604178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ShuffleVectorExprClass: 161414a1bdbdaf250e0488589f12865c8961831b65dHal Finkel case Expr::ConvertVectorExprClass: 1624178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::IntegerLiteralClass: 1634178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CharacterLiteralClass: 1644178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::AddrLabelExprClass: 1654178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXDeleteExprClass: 1664178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ImplicitValueInitExprClass: 1674178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::BlockExprClass: 1684178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::FloatingLiteralClass: 1694178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNoexceptExprClass: 1704178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXScalarValueInitExprClass: 1714ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor case Expr::TypeTraitExprClass: 17221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley case Expr::ArrayTypeTraitExprClass: 173552622067dc45013d240f73952fece703f5e63bdJohn Wiegley case Expr::ExpressionTraitExprClass: 1744178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCSelectorExprClass: 1754178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCProtocolExprClass: 1764178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCStringLiteralClass: 177eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard case Expr::ObjCBoxedExprClass: 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCArrayLiteralClass: 179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCDictionaryLiteralClass: 180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCBoolLiteralExprClass: 1814178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ParenListExprClass: 182ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor case Expr::SizeOfPackExprClass: 183c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor case Expr::SubstNonTypeTemplateParmPackExprClass: 18461eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner case Expr::AsTypeExprClass: 185f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCIndirectCopyRestoreExprClass: 186276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Expr::AtomicExprClass: 187176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case Expr::CXXFoldExprClass: 188b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar case Expr::NoInitExprClass: 189b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar case Expr::DesignatedInitUpdateExprClass: 190a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case Expr::CoyieldExprClass: 1914178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_PRValue; 1922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 1932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Next come the complicated cases. 19491a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall case Expr::SubstNonTypeTemplateParmExprClass: 19591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return ClassifyInternal(Ctx, 19691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 1972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 1982111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.sub]p1: The result is an lvalue of type "T". 1992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // However, subscripting vector types is more like member access. 2002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ArraySubscriptExprClass: 2012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) 2022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); 2032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 2042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a 2062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // function or variable and a prvalue otherwise. 2072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::DeclRefExprClass: 208755d8497e39071aa24acc173ff07083e3256b8f8John McCall if (E->getType() == Ctx.UnknownAnyTy) 209755d8497e39071aa24acc173ff07083e3256b8f8John McCall return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) 210755d8497e39071aa24acc173ff07083e3256b8f8John McCall ? Cl::CL_PRValue : Cl::CL_LValue; 2112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); 2122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Member access is complex. 2142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::MemberExprClass: 2152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); 2162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::UnaryOperatorClass: 2182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (cast<UnaryOperator>(E)->getOpcode()) { 2192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.unary.op]p1: The unary * operator performs indirection: 2202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // [...] the result is an lvalue referring to the object or function 2212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // to which the expression points. 2222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 2232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 2242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // GNU extensions, simply look through them. 2262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 2272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); 2282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 229b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall // Treat _Real and _Imag basically as if they were member 230b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall // expressions: l-value only if the operand is a true l-value. 231b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall case UO_Real: 232b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall case UO_Imag: { 233b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 234b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall Cl::Kinds K = ClassifyInternal(Ctx, Op); 235b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall if (K != Cl::CL_LValue) return K; 236b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall 23712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Op)) 238b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall return Cl::CL_SubObjCPropertySetting; 239b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall return Cl::CL_LValue; 240b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall } 241b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall 2422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.pre.incr]p1: The result is the updated operand; it is an 2432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue, [...] 2442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Not so in C. 2452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreInc: 2462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreDec: 2472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; 2482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl default: 2502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 2512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 2522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2537cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall case Expr::OpaqueValueExprClass: 25413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 2557cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall 2564b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Pseudo-object expressions can produce l-values with reference magic. 2574b9c2d235fb9449e249d74f48ecfec601650de93John McCall case Expr::PseudoObjectExprClass: 2584b9c2d235fb9449e249d74f48ecfec601650de93John McCall return ClassifyExprValueKind(Lang, E, 2594b9c2d235fb9449e249d74f48ecfec601650de93John McCall cast<PseudoObjectExpr>(E)->getValueKind()); 2604b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Implicit casts are lvalues if they're lvalue casts. Other than that, we 2622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // only specifically record class temporaries. 2632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ImplicitCastExprClass: 26413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 2652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p4: The presence of parentheses does not affect 2672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // whether the expression is an lvalue. 2682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ParenExprClass: 2692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); 2702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 271ffbe9b9c64ab2e94b9d48ec56e511f75826fc80aBenjamin Kramer // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, 272f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne // or a void expression if its result expression is, respectively, an 273f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne // lvalue, a function designator, or a void expression. 274f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne case Expr::GenericSelectionExprClass: 275f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne if (cast<GenericSelectionExpr>(E)->isResultDependent()) 276f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return Cl::CL_PRValue; 277f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); 278f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 2792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::BinaryOperatorClass: 2802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CompoundAssignOperatorClass: 2812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C doesn't have any binary expressions that are lvalues. 2822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Lang.CPlusPlus) 2832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); 2842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 2852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CallExprClass: 2872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXOperatorCallExprClass: 2882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXMemberCallExprClass: 2899fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith case Expr::UserDefinedLiteralClass: 290e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne case Expr::CUDAKernelCallExprClass: 2910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx)); 2922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // __builtin_choose_expr is equivalent to the chosen expression. 2942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ChooseExprClass: 295a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr()); 2962111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Extended vector element access is an lvalue unless there are duplicates 2982111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // in the shuffle expression. 2992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ExtVectorElementExprClass: 30026e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements()) 30126e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return Cl::CL_DuplicateVectorComponents; 30226e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman if (cast<ExtVectorElementExpr>(E)->isArrow()) 30326e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return Cl::CL_LValue; 30426e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase()); 3052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Simply look at the actual default argument. 3072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXDefaultArgExprClass: 3082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); 3092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 310c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith // Same idea for default initializers. 311c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith case Expr::CXXDefaultInitExprClass: 312c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); 313c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith 3142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Same idea for temporary binding. 3152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXBindTemporaryExprClass: 3162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 3172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3184765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall // And the cleanups guard. 3194765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall case Expr::ExprWithCleanupsClass: 3204765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); 3212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Casts depend completely on the target type. All casts work the same. 3232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CStyleCastExprClass: 3242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXFunctionalCastExprClass: 3252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXStaticCastExprClass: 3262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXDynamicCastExprClass: 3272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXReinterpretCastExprClass: 3282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXConstCastExprClass: 329f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCBridgedCastExprClass: 3302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Only in C++ can casts be interesting at all. 3312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!Lang.CPlusPlus) return Cl::CL_PRValue; 3322111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); 3332111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 334032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor case Expr::CXXUnresolvedConstructExprClass: 335032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor return ClassifyUnnamed(Ctx, 336032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); 337032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor 33856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::BinaryConditionalOperatorClass: { 33956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (!Lang.CPlusPlus) return Cl::CL_PRValue; 34056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E); 34156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 34256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 34356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 34456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::ConditionalOperatorClass: { 3452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Once again, only C++ is interesting. 3462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!Lang.CPlusPlus) return Cl::CL_PRValue; 34756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const ConditionalOperator *co = cast<ConditionalOperator>(E); 34856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 34956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 3502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // ObjC message sends are effectively function calls, if the target function 3522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // is known. 3532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCMessageExprClass: 3542111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (const ObjCMethodDecl *Method = 3552111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl cast<ObjCMessageExpr>(E)->getMethodDecl()) { 356651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType()); 357077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; 3582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 3594178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_PRValue; 3604178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor 3612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Some C++ expressions are always class temporaries. 3622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXConstructExprClass: 3632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXTemporaryObjectExprClass: 36401d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor case Expr::LambdaExprClass: 3657c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith case Expr::CXXStdInitializerListExprClass: 3662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_ClassTemporary; 3672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3684178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::VAArgExprClass: 3694178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return ClassifyUnnamed(Ctx, E->getType()); 37013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3714178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::DesignatedInitExprClass: 3724178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); 37313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3744178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::StmtExprClass: { 3754178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); 3764178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) 377226cbfcd97e400ac1c1afc06d646424136cfe196Douglas Gregor return ClassifyUnnamed(Ctx, LastExpr->getType()); 3782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 3792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 38013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3814178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXUuidofExprClass: 382ecea19f00a911c50dc20fe94e548f488ded47adbFrancois Pichet return Cl::CL_LValue; 38313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 384be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor case Expr::PackExpansionExprClass: 385be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); 38613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 38703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Expr::MaterializeTemporaryExprClass: 3880b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() 38903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor ? Cl::CL_LValue 39003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor : Cl::CL_XValue; 39113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 39213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl case Expr::InitListExprClass: 39313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // An init list can be an lvalue if it is bound to a reference and 39413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // contains only one element. In that case, we look at that element 39513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // for an exact classification. Init list creation takes care of the 39613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // value kind for us, so we only need to fine-tune. 39713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl if (E->isRValue()) 39813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 39913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl assert(cast<InitListExpr>(E)->getNumInits() == 1 && 40013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl "Only 1-element init lists can be glvalues."); 40113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); 402a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 403a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case Expr::CoawaitExprClass: 404a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return ClassifyInternal(Ctx, cast<CoawaitExpr>(E)->getResumeExpr()); 4054178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor } 40613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 4074178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor llvm_unreachable("unhandled expression kind in classification"); 4082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// ClassifyDecl - Return the classification of an expression referencing the 4112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// given declaration. 4122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { 4132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a 4142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // function, variable, or data member and a prvalue otherwise. 4152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In C, functions are not lvalues. 4162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an 4172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to 4182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // special-case this. 4199c72c6088d591ace8503b842d39448c2040f3033John McCall 4209c72c6088d591ace8503b842d39448c2040f3033John McCall if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 4219c72c6088d591ace8503b842d39448c2040f3033John McCall return Cl::CL_MemberFunction; 4229c72c6088d591ace8503b842d39448c2040f3033John McCall 4232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl bool islvalue; 4242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (const NonTypeTemplateParmDecl *NTTParm = 4252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl dyn_cast<NonTypeTemplateParmDecl>(D)) 4262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl islvalue = NTTParm->getType()->isReferenceType(); 4272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl else 4282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || 4290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines isa<IndirectFieldDecl>(D) || 4300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines (Ctx.getLangOpts().CPlusPlus && 4310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines (isa<FunctionDecl>(D) || isa<MSPropertyDecl>(D) || 4320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines isa<FunctionTemplateDecl>(D))); 4332111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4342111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; 4352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// ClassifyUnnamed - Return the classification of an expression yielding an 4382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// unnamed value of the given type. This applies in particular to function 4392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// calls and casts. 4402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { 4412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In C, function calls are always rvalues. 4424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; 4432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.call]p10: A function call is an lvalue if the result type is an 4452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue reference type or an rvalue reference to function type, an xvalue 44685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl // if the result type is an rvalue reference to object type, and a prvalue 4472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // otherwise. 4482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (T->isLValueReferenceType()) 4492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl const RValueReferenceType *RV = T->getAs<RValueReferenceType>(); 4512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!RV) // Could still be a class temporary, though. 45236d02af300a207242f0486b4255420d8be796b21Richard Smith return ClassifyTemporary(T); 4532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4542111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; 4552111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { 458755d8497e39071aa24acc173ff07083e3256b8f8John McCall if (E->getType() == Ctx.UnknownAnyTy) 459755d8497e39071aa24acc173ff07083e3256b8f8John McCall return (isa<FunctionDecl>(E->getMemberDecl()) 460755d8497e39071aa24acc173ff07083e3256b8f8John McCall ? Cl::CL_PRValue : Cl::CL_LValue); 461755d8497e39071aa24acc173ff07083e3256b8f8John McCall 4622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Handle C first, it's easier. 4634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ctx.getLangOpts().CPlusPlus) { 4642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C99 6.5.2.3p3 4652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // For dot access, the expression is an lvalue if the first part is. For 4662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // arrow access, it always is an lvalue. 4672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isArrow()) 4682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // ObjC property accesses are not lvalues, but get special treatment. 470b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall Expr *Base = E->getBase()->IgnoreParens(); 47112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Base)) 4722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_SubObjCPropertySetting; 4732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, Base); 4742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 4752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl NamedDecl *Member = E->getMemberDecl(); 4772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. 4782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then 4792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // E1.E2 is an lvalue. 4802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 4812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Value->getType()->isReferenceType()) 4822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Otherwise, one of the following rules applies. 4852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a static member [...] then E1.E2 is an lvalue. 4862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) 4872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4892111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then 4902111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; 4912111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // otherwise, it is a prvalue. 4922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (isa<FieldDecl>(Member)) { 4932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // *E1 is an lvalue 4942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isArrow()) 4952111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4960943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall Expr *Base = E->getBase()->IgnoreParenImpCasts(); 49712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Base)) 4980943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall return Cl::CL_SubObjCPropertySetting; 4992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, E->getBase()); 5002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a [...] member function, [...] 5032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If it refers to a static member function [...], then E1.E2 is an 5042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue; [...] 5052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- Otherwise [...] E1.E2 is a prvalue. 5062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 5072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; 5082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. 5102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // So is everything else we haven't handled yet. 5112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 5122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { 5154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Ctx.getLangOpts().CPlusPlus && 5162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl "This is only relevant for C++."); 5172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. 518f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall // Except we override this for writes to ObjC properties. 5192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isAssignmentOp()) 520f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall return (E->getLHS()->getObjectKind() == OK_ObjCProperty 521f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall ? Cl::CL_PRValue : Cl::CL_LValue); 5222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.comma]p1: the result is of the same value category as its right 5242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // operand, [...]. 5252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Comma) 5262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, E->getRHS()); 5272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand 5292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // is a pointer to a data member is of the same value category as its first 5302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // operand. 5312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_PtrMemD) 532e0a22d06888c13989b3f72db319f1d498bf69153John McCall return (E->getType()->isFunctionType() || 533e0a22d06888c13989b3f72db319f1d498bf69153John McCall E->hasPlaceholderType(BuiltinType::BoundMember)) 534b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor ? Cl::CL_MemberFunction 535b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor : ClassifyInternal(Ctx, E->getLHS()); 5362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its 5382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // second operand is a pointer to data member and a prvalue otherwise. 5392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_PtrMemI) 540e0a22d06888c13989b3f72db319f1d498bf69153John McCall return (E->getType()->isFunctionType() || 541e0a22d06888c13989b3f72db319f1d498bf69153John McCall E->hasPlaceholderType(BuiltinType::BoundMember)) 542b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor ? Cl::CL_MemberFunction 543b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor : Cl::CL_LValue; 5442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // All other binary operations are prvalues. 5462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 5472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 54956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallstatic Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, 55056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const Expr *False) { 5514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Ctx.getLangOpts().CPlusPlus && 5522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl "This is only relevant for C++."); 5532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5542111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p2 555651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // If either the second or the third operand has type (cv) void, 556651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // one of the following shall hold: 557651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (True->getType()->isVoidType() || False->getType()->isVoidType()) { 558651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // The second or the third operand (but not both) is a (possibly 559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // parenthesized) throw-expression; the result is of the [...] value 560651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // category of the other. 561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts()); 562651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts()); 5636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False) 5646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines : (FalseIsThrow ? True : nullptr)) 565651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return ClassifyInternal(Ctx, NonThrow); 566651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 567651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // [Otherwise] the result [...] is a prvalue. 5682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 569651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 5702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Note that at this point, we have already performed all conversions 5722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // according to [expr.cond]p3. 5732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p4: If the second and third operands are glvalues of the 5742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // same value category [...], the result is of that [...] value category. 5752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p5: Otherwise, the result is a prvalue. 5762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds LCl = ClassifyInternal(Ctx, True), 5772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl RCl = ClassifyInternal(Ctx, False); 5782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return LCl == RCl ? LCl : Cl::CL_PRValue; 5792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 5822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds Kind, SourceLocation &Loc) { 5832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // As a general rule, we only care about lvalues. But there are some rvalues 5842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // for which we want to generate special results. 5852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Kind == Cl::CL_PRValue) { 5862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // For the sake of better diagnostics, we want to specifically recognize 5872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // use of the GCC cast-as-lvalue extension. 588f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (const ExplicitCastExpr *CE = 589f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) { 590f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { 591f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall Loc = CE->getExprLoc(); 5922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_LValueCast; 5932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5952111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5962111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Kind != Cl::CL_LValue) 5972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_RValue; 5982111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // This is the lvalue case. 6002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) 6014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) 6022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_Function; 6032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Assignment to a property in ObjC is an implicit setter access. But a 6052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // setter might not exist. 60612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) { 6076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Expr->isImplicitProperty() && 6086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Expr->getImplicitPropertySetter() == nullptr) 6092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_NoSetterProperty; 6102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl CanQualType CT = Ctx.getCanonicalType(E->getType()); 6132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Const stuff is obviously not modifiable. 61478dae24600a877f52dbb6e58bfd5778754a00974John McCall if (CT.isConstQualified()) 6152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ConstQualified; 616758c4d86bb9b2298374fce5b3ca4a35f953f2d2eJoey Gouly if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) 617b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar return Cl::CM_ConstAddrSpace; 61871930e02730f3afecd6e71e4d6831b4a07436a7fEli Friedman 6192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Arrays are not modifiable, only their elements are. 6202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CT->isArrayType()) 6212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ArrayType; 6222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Incomplete types are not modifiable. 6232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CT->isIncompleteType()) 6242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_IncompleteType; 6252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Records with any const fields (recursively) are not modifiable. 6270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines if (const RecordType *R = CT->getAs<RecordType>()) 6282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (R->hasConstFields()) 6292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ConstQualified; 6302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_Modifiable; 6322111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 6332111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6347eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCallExpr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { 6352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Classification VC = Classify(Ctx); 6362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getKind()) { 6372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_LValue: return LV_Valid; 6382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_XValue: return LV_InvalidExpression; 6392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_Function: return LV_NotObjectType; 6401c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_Void: return LV_InvalidExpression; 6411c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; 6422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; 6432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_MemberFunction: return LV_MemberFunction; 6442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; 6452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_ClassTemporary: return LV_ClassTemporary; 64636d02af300a207242f0486b4255420d8be796b21Richard Smith case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; 647077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; 6482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_PRValue: return LV_InvalidExpression; 6492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6500010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("Unhandled kind"); 6512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 6522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6532111c855343a0530e236bf0862358ec8d67b28f3Sebastian RedlExpr::isModifiableLvalueResult 6542111c855343a0530e236bf0862358ec8d67b28f3Sebastian RedlExpr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { 6552111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl SourceLocation dummy; 6562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); 6572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getKind()) { 6582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_LValue: break; 6592111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_XValue: return MLV_InvalidExpression; 6602111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_Function: return MLV_NotObjectType; 6611c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_Void: return MLV_InvalidExpression; 6621c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; 6632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 6642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_MemberFunction: return MLV_MemberFunction; 6652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; 6662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_ClassTemporary: return MLV_ClassTemporary; 66736d02af300a207242f0486b4255420d8be796b21Richard Smith case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; 668077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; 6692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_PRValue: 6702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return VC.getModifiable() == Cl::CM_LValueCast ? 6712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl MLV_LValueCast : MLV_InvalidExpression; 6722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); 6742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getModifiable()) { 6750010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); 6762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_Modifiable: return MLV_Valid; 6770010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); 6782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_Function: return MLV_NotObjectType; 6792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_LValueCast: 6800010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("CM_LValueCast and CL_LValue don't match"); 6812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; 6822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_ConstQualified: return MLV_ConstQualified; 683b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace; 6842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_ArrayType: return MLV_ArrayType; 6852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_IncompleteType: return MLV_IncompleteType; 6862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6870010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("Unhandled modifiable type"); 6882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 689