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: 1394178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_LValue; 14013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 1412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C99 6.5.2.5p5 says that compound literals are lvalues. 14236d02af300a207242f0486b4255420d8be796b21Richard Smith // In C++, they're prvalue temporaries. 1432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CompoundLiteralExprClass: 14436d02af300a207242f0486b4255420d8be796b21Richard Smith return Ctx.getLangOpts().CPlusPlus ? ClassifyTemporary(E->getType()) 14536d02af300a207242f0486b4255420d8be796b21Richard Smith : Cl::CL_LValue; 1464178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor 1474178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor // Expressions that are prvalues. 1484178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXBoolLiteralExprClass: 1494178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXPseudoDestructorExprClass: 150f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case Expr::UnaryExprOrTypeTraitExprClass: 1514178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNewExprClass: 1524178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXThisExprClass: 1534178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNullPtrLiteralExprClass: 1544178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ImaginaryLiteralClass: 1554178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::GNUNullExprClass: 1564178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::OffsetOfExprClass: 1574178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXThrowExprClass: 1584178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ShuffleVectorExprClass: 159414a1bdbdaf250e0488589f12865c8961831b65dHal Finkel case Expr::ConvertVectorExprClass: 1604178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::IntegerLiteralClass: 1614178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CharacterLiteralClass: 1624178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::AddrLabelExprClass: 1634178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXDeleteExprClass: 1644178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ImplicitValueInitExprClass: 1654178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::BlockExprClass: 1664178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::FloatingLiteralClass: 1674178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXNoexceptExprClass: 1684178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXScalarValueInitExprClass: 1694ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor case Expr::TypeTraitExprClass: 17021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley case Expr::ArrayTypeTraitExprClass: 171552622067dc45013d240f73952fece703f5e63bdJohn Wiegley case Expr::ExpressionTraitExprClass: 1724178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCSelectorExprClass: 1734178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCProtocolExprClass: 1744178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ObjCStringLiteralClass: 175eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard case Expr::ObjCBoxedExprClass: 176ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCArrayLiteralClass: 177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCDictionaryLiteralClass: 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCBoolLiteralExprClass: 1794178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::ParenListExprClass: 180ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor case Expr::SizeOfPackExprClass: 181c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor case Expr::SubstNonTypeTemplateParmPackExprClass: 18261eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner case Expr::AsTypeExprClass: 183f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCIndirectCopyRestoreExprClass: 184276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Expr::AtomicExprClass: 185176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case Expr::CXXFoldExprClass: 1864178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_PRValue; 1872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 1882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Next come the complicated cases. 18991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall case Expr::SubstNonTypeTemplateParmExprClass: 19091a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return ClassifyInternal(Ctx, 19191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 1922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 1932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.sub]p1: The result is an lvalue of type "T". 1942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // However, subscripting vector types is more like member access. 1952111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ArraySubscriptExprClass: 1962111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) 1972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); 1982111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 1992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a 2012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // function or variable and a prvalue otherwise. 2022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::DeclRefExprClass: 203755d8497e39071aa24acc173ff07083e3256b8f8John McCall if (E->getType() == Ctx.UnknownAnyTy) 204755d8497e39071aa24acc173ff07083e3256b8f8John McCall return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) 205755d8497e39071aa24acc173ff07083e3256b8f8John McCall ? Cl::CL_PRValue : Cl::CL_LValue; 2062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); 2072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Member access is complex. 2092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::MemberExprClass: 2102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); 2112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::UnaryOperatorClass: 2132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (cast<UnaryOperator>(E)->getOpcode()) { 2142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.unary.op]p1: The unary * operator performs indirection: 2152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // [...] the result is an lvalue referring to the object or function 2162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // to which the expression points. 2172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 2182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 2192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // GNU extensions, simply look through them. 2212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 2222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); 2232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 224b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall // Treat _Real and _Imag basically as if they were member 225b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall // expressions: l-value only if the operand is a true l-value. 226b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall case UO_Real: 227b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall case UO_Imag: { 228b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 229b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall Cl::Kinds K = ClassifyInternal(Ctx, Op); 230b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall if (K != Cl::CL_LValue) return K; 231b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall 23212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Op)) 233b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall return Cl::CL_SubObjCPropertySetting; 234b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall return Cl::CL_LValue; 235b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall } 236b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall 2372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.pre.incr]p1: The result is the updated operand; it is an 2382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue, [...] 2392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Not so in C. 2402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreInc: 2412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreDec: 2422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; 2432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl default: 2452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 2462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 2472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2487cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall case Expr::OpaqueValueExprClass: 24913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 2507cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall 2514b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Pseudo-object expressions can produce l-values with reference magic. 2524b9c2d235fb9449e249d74f48ecfec601650de93John McCall case Expr::PseudoObjectExprClass: 2534b9c2d235fb9449e249d74f48ecfec601650de93John McCall return ClassifyExprValueKind(Lang, E, 2544b9c2d235fb9449e249d74f48ecfec601650de93John McCall cast<PseudoObjectExpr>(E)->getValueKind()); 2554b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Implicit casts are lvalues if they're lvalue casts. Other than that, we 2572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // only specifically record class temporaries. 2582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ImplicitCastExprClass: 25913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 2602111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p4: The presence of parentheses does not affect 2622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // whether the expression is an lvalue. 2632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ParenExprClass: 2642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); 2652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 266ffbe9b9c64ab2e94b9d48ec56e511f75826fc80aBenjamin Kramer // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, 267f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne // or a void expression if its result expression is, respectively, an 268f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne // lvalue, a function designator, or a void expression. 269f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne case Expr::GenericSelectionExprClass: 270f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne if (cast<GenericSelectionExpr>(E)->isResultDependent()) 271f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return Cl::CL_PRValue; 272f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); 273f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 2742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::BinaryOperatorClass: 2752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CompoundAssignOperatorClass: 2762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C doesn't have any binary expressions that are lvalues. 2772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Lang.CPlusPlus) 2782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); 2792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 2802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CallExprClass: 2822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXOperatorCallExprClass: 2832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXMemberCallExprClass: 2849fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith case Expr::UserDefinedLiteralClass: 285e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne case Expr::CUDAKernelCallExprClass: 2860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx)); 2872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // __builtin_choose_expr is equivalent to the chosen expression. 2892111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ChooseExprClass: 290a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr()); 2912111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 2922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Extended vector element access is an lvalue unless there are duplicates 2932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // in the shuffle expression. 2942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ExtVectorElementExprClass: 29526e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements()) 29626e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return Cl::CL_DuplicateVectorComponents; 29726e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman if (cast<ExtVectorElementExpr>(E)->isArrow()) 29826e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return Cl::CL_LValue; 29926e51781f9d55cdae764facf8fe773aa2adb4569Eli Friedman return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase()); 3002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Simply look at the actual default argument. 3022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXDefaultArgExprClass: 3032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); 3042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 305c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith // Same idea for default initializers. 306c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith case Expr::CXXDefaultInitExprClass: 307c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); 308c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith 3092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Same idea for temporary binding. 3102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXBindTemporaryExprClass: 3112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 3122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3134765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall // And the cleanups guard. 3144765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall case Expr::ExprWithCleanupsClass: 3154765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); 3162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Casts depend completely on the target type. All casts work the same. 3182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CStyleCastExprClass: 3192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXFunctionalCastExprClass: 3202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXStaticCastExprClass: 3212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXDynamicCastExprClass: 3222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXReinterpretCastExprClass: 3232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXConstCastExprClass: 324f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCBridgedCastExprClass: 3252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Only in C++ can casts be interesting at all. 3262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!Lang.CPlusPlus) return Cl::CL_PRValue; 3272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); 3282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 329032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor case Expr::CXXUnresolvedConstructExprClass: 330032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor return ClassifyUnnamed(Ctx, 331032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); 332032c86921177031ecbb46c398b3e710758ba915eDouglas Gregor 33356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::BinaryConditionalOperatorClass: { 33456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (!Lang.CPlusPlus) return Cl::CL_PRValue; 33556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E); 33656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 33756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 33856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 33956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::ConditionalOperatorClass: { 3402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Once again, only C++ is interesting. 3412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!Lang.CPlusPlus) return Cl::CL_PRValue; 34256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const ConditionalOperator *co = cast<ConditionalOperator>(E); 34356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 34456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 3452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // ObjC message sends are effectively function calls, if the target function 3472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // is known. 3482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::ObjCMessageExprClass: 3492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (const ObjCMethodDecl *Method = 3502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl cast<ObjCMessageExpr>(E)->getMethodDecl()) { 351651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType()); 352077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; 3532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 3544178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return Cl::CL_PRValue; 3554178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor 3562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Some C++ expressions are always class temporaries. 3572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXConstructExprClass: 3582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Expr::CXXTemporaryObjectExprClass: 35901d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor case Expr::LambdaExprClass: 3607c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith case Expr::CXXStdInitializerListExprClass: 3612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_ClassTemporary; 3622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 3634178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::VAArgExprClass: 3644178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return ClassifyUnnamed(Ctx, E->getType()); 36513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3664178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::DesignatedInitExprClass: 3674178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); 36813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3694178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::StmtExprClass: { 3704178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); 3714178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) 372226cbfcd97e400ac1c1afc06d646424136cfe196Douglas Gregor return ClassifyUnnamed(Ctx, LastExpr->getType()); 3732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 3742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 37513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3764178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor case Expr::CXXUuidofExprClass: 377ecea19f00a911c50dc20fe94e548f488ded47adbFrancois Pichet return Cl::CL_LValue; 37813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 379be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor case Expr::PackExpansionExprClass: 380be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); 38113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 38203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Expr::MaterializeTemporaryExprClass: 3830b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() 38403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor ? Cl::CL_LValue 38503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor : Cl::CL_XValue; 38613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 38713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl case Expr::InitListExprClass: 38813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // An init list can be an lvalue if it is bound to a reference and 38913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // contains only one element. In that case, we look at that element 39013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // for an exact classification. Init list creation takes care of the 39113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl // value kind for us, so we only need to fine-tune. 39213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl if (E->isRValue()) 39313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyExprValueKind(Lang, E, E->getValueKind()); 39413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl assert(cast<InitListExpr>(E)->getNumInits() == 1 && 39513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl "Only 1-element init lists can be glvalues."); 39613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); 3974178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor } 39813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl 3994178b573b82449c2d888ea1f0957a478534e118cDouglas Gregor llvm_unreachable("unhandled expression kind in classification"); 4002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// ClassifyDecl - Return the classification of an expression referencing the 4032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// given declaration. 4042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { 4052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a 4062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // function, variable, or data member and a prvalue otherwise. 4072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In C, functions are not lvalues. 4082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an 4092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to 4102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // special-case this. 4119c72c6088d591ace8503b842d39448c2040f3033John McCall 4129c72c6088d591ace8503b842d39448c2040f3033John McCall if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 4139c72c6088d591ace8503b842d39448c2040f3033John McCall return Cl::CL_MemberFunction; 4149c72c6088d591ace8503b842d39448c2040f3033John McCall 4152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl bool islvalue; 4162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (const NonTypeTemplateParmDecl *NTTParm = 4172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl dyn_cast<NonTypeTemplateParmDecl>(D)) 4182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl islvalue = NTTParm->getType()->isReferenceType(); 4192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl else 4202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || 4210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines isa<IndirectFieldDecl>(D) || 4220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines (Ctx.getLangOpts().CPlusPlus && 4230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines (isa<FunctionDecl>(D) || isa<MSPropertyDecl>(D) || 4240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines isa<FunctionTemplateDecl>(D))); 4252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; 4272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// ClassifyUnnamed - Return the classification of an expression yielding an 4302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// unnamed value of the given type. This applies in particular to function 4312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl/// calls and casts. 4322111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { 4332111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // In C, function calls are always rvalues. 4344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; 4352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.call]p10: A function call is an lvalue if the result type is an 4372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue reference type or an rvalue reference to function type, an xvalue 43885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl // if the result type is an rvalue reference to object type, and a prvalue 4392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // otherwise. 4402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (T->isLValueReferenceType()) 4412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl const RValueReferenceType *RV = T->getAs<RValueReferenceType>(); 4432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (!RV) // Could still be a class temporary, though. 44436d02af300a207242f0486b4255420d8be796b21Richard Smith return ClassifyTemporary(T); 4452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; 4472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 4482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { 450755d8497e39071aa24acc173ff07083e3256b8f8John McCall if (E->getType() == Ctx.UnknownAnyTy) 451755d8497e39071aa24acc173ff07083e3256b8f8John McCall return (isa<FunctionDecl>(E->getMemberDecl()) 452755d8497e39071aa24acc173ff07083e3256b8f8John McCall ? Cl::CL_PRValue : Cl::CL_LValue); 453755d8497e39071aa24acc173ff07083e3256b8f8John McCall 4542111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Handle C first, it's easier. 4554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Ctx.getLangOpts().CPlusPlus) { 4562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C99 6.5.2.3p3 4572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // For dot access, the expression is an lvalue if the first part is. For 4582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // arrow access, it always is an lvalue. 4592111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isArrow()) 4602111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // ObjC property accesses are not lvalues, but get special treatment. 462b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall Expr *Base = E->getBase()->IgnoreParens(); 46312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Base)) 4642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_SubObjCPropertySetting; 4652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, Base); 4662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 4672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl NamedDecl *Member = E->getMemberDecl(); 4692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. 4702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then 4712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // E1.E2 is an lvalue. 4722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 4732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Value->getType()->isReferenceType()) 4742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Otherwise, one of the following rules applies. 4772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a static member [...] then E1.E2 is an lvalue. 4782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) 4792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then 4822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; 4832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // otherwise, it is a prvalue. 4842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (isa<FieldDecl>(Member)) { 4852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // *E1 is an lvalue 4862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isArrow()) 4872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_LValue; 4880943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall Expr *Base = E->getBase()->IgnoreParenImpCasts(); 48912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (isa<ObjCPropertyRefExpr>(Base)) 4900943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall return Cl::CL_SubObjCPropertySetting; 4912111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, E->getBase()); 4922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 4932111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 4942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a [...] member function, [...] 4952111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If it refers to a static member function [...], then E1.E2 is an 4962111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // lvalue; [...] 4972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- Otherwise [...] E1.E2 is a prvalue. 4982111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 4992111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; 5002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. 5022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // So is everything else we haven't handled yet. 5032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 5042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { 5074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Ctx.getLangOpts().CPlusPlus && 5082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl "This is only relevant for C++."); 5092111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. 510f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall // Except we override this for writes to ObjC properties. 5112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (E->isAssignmentOp()) 512f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall return (E->getLHS()->getObjectKind() == OK_ObjCProperty 513f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall ? Cl::CL_PRValue : Cl::CL_LValue); 5142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.comma]p1: the result is of the same value category as its right 5162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // operand, [...]. 5172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Comma) 5182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return ClassifyInternal(Ctx, E->getRHS()); 5192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand 5212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // is a pointer to a data member is of the same value category as its first 5222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // operand. 5232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_PtrMemD) 524e0a22d06888c13989b3f72db319f1d498bf69153John McCall return (E->getType()->isFunctionType() || 525e0a22d06888c13989b3f72db319f1d498bf69153John McCall E->hasPlaceholderType(BuiltinType::BoundMember)) 526b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor ? Cl::CL_MemberFunction 527b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor : ClassifyInternal(Ctx, E->getLHS()); 5282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its 5302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // second operand is a pointer to data member and a prvalue otherwise. 5312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_PtrMemI) 532e0a22d06888c13989b3f72db319f1d498bf69153John McCall return (E->getType()->isFunctionType() || 533e0a22d06888c13989b3f72db319f1d498bf69153John McCall E->hasPlaceholderType(BuiltinType::BoundMember)) 534b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor ? Cl::CL_MemberFunction 535b0844c6f07d8c9c6c9c3095201879593611b9e79Douglas Gregor : Cl::CL_LValue; 5362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // All other binary operations are prvalues. 5382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 5392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 54156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallstatic Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, 54256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const Expr *False) { 5434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Ctx.getLangOpts().CPlusPlus && 5442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl "This is only relevant for C++."); 5452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p2 547651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // If either the second or the third operand has type (cv) void, 548651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // one of the following shall hold: 549651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (True->getType()->isVoidType() || False->getType()->isVoidType()) { 550651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // The second or the third operand (but not both) is a (possibly 551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // parenthesized) throw-expression; the result is of the [...] value 552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // category of the other. 553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts()); 554651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts()); 5556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False) 5566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines : (FalseIsThrow ? True : nullptr)) 557651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return ClassifyInternal(Ctx, NonThrow); 558651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // [Otherwise] the result [...] is a prvalue. 5602111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CL_PRValue; 561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 5622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Note that at this point, we have already performed all conversions 5642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // according to [expr.cond]p3. 5652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p4: If the second and third operands are glvalues of the 5662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // same value category [...], the result is of that [...] value category. 5672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // C++ [expr.cond]p5: Otherwise, the result is a prvalue. 5682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds LCl = ClassifyInternal(Ctx, True), 5692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl RCl = ClassifyInternal(Ctx, False); 5702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return LCl == RCl ? LCl : Cl::CL_PRValue; 5712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 5722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlstatic Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 5742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Cl::Kinds Kind, SourceLocation &Loc) { 5752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // As a general rule, we only care about lvalues. But there are some rvalues 5762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // for which we want to generate special results. 5772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Kind == Cl::CL_PRValue) { 5782111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // For the sake of better diagnostics, we want to specifically recognize 5792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // use of the GCC cast-as-lvalue extension. 580f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (const ExplicitCastExpr *CE = 581f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) { 582f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { 583f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall Loc = CE->getExprLoc(); 5842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_LValueCast; 5852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 5882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (Kind != Cl::CL_LValue) 5892111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_RValue; 5902111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5912111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // This is the lvalue case. 5922111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) 5934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) 5942111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_Function; 5952111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 5962111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Assignment to a property in ObjC is an implicit setter access. But a 5972111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // setter might not exist. 59812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) { 5996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Expr->isImplicitProperty() && 6006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Expr->getImplicitPropertySetter() == nullptr) 6012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_NoSetterProperty; 6022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl CanQualType CT = Ctx.getCanonicalType(E->getType()); 6052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Const stuff is obviously not modifiable. 60678dae24600a877f52dbb6e58bfd5778754a00974John McCall if (CT.isConstQualified()) 6072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ConstQualified; 608758c4d86bb9b2298374fce5b3ca4a35f953f2d2eJoey Gouly if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) 609758c4d86bb9b2298374fce5b3ca4a35f953f2d2eJoey Gouly return Cl::CM_ConstQualified; 61071930e02730f3afecd6e71e4d6831b4a07436a7fEli Friedman 6112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Arrays are not modifiable, only their elements are. 6122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CT->isArrayType()) 6132111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ArrayType; 6142111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Incomplete types are not modifiable. 6152111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (CT->isIncompleteType()) 6162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_IncompleteType; 6172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl // Records with any const fields (recursively) are not modifiable. 6190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines if (const RecordType *R = CT->getAs<RecordType>()) 6202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl if (R->hasConstFields()) 6212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_ConstQualified; 6222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return Cl::CM_Modifiable; 6242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 6252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6267eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCallExpr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { 6272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Classification VC = Classify(Ctx); 6282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getKind()) { 6292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_LValue: return LV_Valid; 6302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_XValue: return LV_InvalidExpression; 6312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_Function: return LV_NotObjectType; 6321c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_Void: return LV_InvalidExpression; 6331c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; 6342111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; 6352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_MemberFunction: return LV_MemberFunction; 6362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; 6372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_ClassTemporary: return LV_ClassTemporary; 63836d02af300a207242f0486b4255420d8be796b21Richard Smith case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; 639077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; 6402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_PRValue: return LV_InvalidExpression; 6412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6420010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("Unhandled kind"); 6432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 6442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl 6452111c855343a0530e236bf0862358ec8d67b28f3Sebastian RedlExpr::isModifiableLvalueResult 6462111c855343a0530e236bf0862358ec8d67b28f3Sebastian RedlExpr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { 6472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl SourceLocation dummy; 6482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); 6492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getKind()) { 6502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_LValue: break; 6512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_XValue: return MLV_InvalidExpression; 6522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_Function: return MLV_NotObjectType; 6531c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_Void: return MLV_InvalidExpression; 6541c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; 6552111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 6562111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_MemberFunction: return MLV_MemberFunction; 6572111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; 6582111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_ClassTemporary: return MLV_ClassTemporary; 65936d02af300a207242f0486b4255420d8be796b21Richard Smith case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; 660077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; 6612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CL_PRValue: 6622111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl return VC.getModifiable() == Cl::CM_LValueCast ? 6632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl MLV_LValueCast : MLV_InvalidExpression; 6642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6652111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); 6662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl switch (VC.getModifiable()) { 6670010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); 6682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_Modifiable: return MLV_Valid; 6690010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); 6702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_Function: return MLV_NotObjectType; 6712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_LValueCast: 6720010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("CM_LValueCast and CL_LValue don't match"); 6732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; 6742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_ConstQualified: return MLV_ConstQualified; 6752111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_ArrayType: return MLV_ArrayType; 6762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl case Cl::CM_IncompleteType: return MLV_IncompleteType; 6772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl } 6780010bca8afaed0236c4910640f6bc1bcf1984125Chandler Carruth llvm_unreachable("Unhandled modifiable type"); 6792111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl} 680