Path: blob/main/contrib/llvm-project/clang/lib/AST/ExprClassification.cpp
35260 views
//===- ExprClassification.cpp - Expression AST Node Implementation --------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements Expr::classify.9//10//===----------------------------------------------------------------------===//1112#include "clang/AST/Expr.h"13#include "clang/AST/ASTContext.h"14#include "clang/AST/DeclCXX.h"15#include "clang/AST/DeclObjC.h"16#include "clang/AST/DeclTemplate.h"17#include "clang/AST/ExprCXX.h"18#include "clang/AST/ExprObjC.h"19#include "llvm/Support/ErrorHandling.h"2021using namespace clang;2223using Cl = Expr::Classification;2425static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);26static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);27static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);28static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);29static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);30static Cl::Kinds ClassifyConditional(ASTContext &Ctx,31const Expr *trueExpr,32const Expr *falseExpr);33static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,34Cl::Kinds Kind, SourceLocation &Loc);3536Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {37assert(!TR->isReferenceType() && "Expressions can't have reference type.");3839Cl::Kinds kind = ClassifyInternal(Ctx, this);40// C99 6.3.2.1: An lvalue is an expression with an object type or an41// incomplete type other than void.42if (!Ctx.getLangOpts().CPlusPlus) {43// Thus, no functions.44if (TR->isFunctionType() || TR == Ctx.OverloadTy)45kind = Cl::CL_Function;46// No void either, but qualified void is OK because it is "other than void".47// Void "lvalues" are classified as addressable void values, which are void48// expressions whose address can be taken.49else if (TR->isVoidType() && !TR.hasQualifiers())50kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void);51}5253// Enable this assertion for testing.54switch (kind) {55case Cl::CL_LValue:56assert(isLValue());57break;58case Cl::CL_XValue:59assert(isXValue());60break;61case Cl::CL_Function:62case Cl::CL_Void:63case Cl::CL_AddressableVoid:64case Cl::CL_DuplicateVectorComponents:65case Cl::CL_MemberFunction:66case Cl::CL_SubObjCPropertySetting:67case Cl::CL_ClassTemporary:68case Cl::CL_ArrayTemporary:69case Cl::CL_ObjCMessageRValue:70case Cl::CL_PRValue:71assert(isPRValue());72break;73}7475Cl::ModifiableType modifiable = Cl::CM_Untested;76if (Loc)77modifiable = IsModifiable(Ctx, this, kind, *Loc);78return Classification(kind, modifiable);79}8081/// Classify an expression which creates a temporary, based on its type.82static Cl::Kinds ClassifyTemporary(QualType T) {83if (T->isRecordType())84return Cl::CL_ClassTemporary;85if (T->isArrayType())86return Cl::CL_ArrayTemporary;8788// No special classification: these don't behave differently from normal89// prvalues.90return Cl::CL_PRValue;91}9293static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,94const Expr *E,95ExprValueKind Kind) {96switch (Kind) {97case VK_PRValue:98return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue;99case VK_LValue:100return Cl::CL_LValue;101case VK_XValue:102return Cl::CL_XValue;103}104llvm_unreachable("Invalid value category of implicit cast.");105}106107static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {108// This function takes the first stab at classifying expressions.109const LangOptions &Lang = Ctx.getLangOpts();110111switch (E->getStmtClass()) {112case Stmt::NoStmtClass:113#define ABSTRACT_STMT(Kind)114#define STMT(Kind, Base) case Expr::Kind##Class:115#define EXPR(Kind, Base)116#include "clang/AST/StmtNodes.inc"117llvm_unreachable("cannot classify a statement");118119// First come the expressions that are always lvalues, unconditionally.120case Expr::ObjCIsaExprClass:121// C++ [expr.prim.general]p1: A string literal is an lvalue.122case Expr::StringLiteralClass:123// @encode is equivalent to its string124case Expr::ObjCEncodeExprClass:125// __func__ and friends are too.126case Expr::PredefinedExprClass:127// Property references are lvalues128case Expr::ObjCSubscriptRefExprClass:129case Expr::ObjCPropertyRefExprClass:130// C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...131case Expr::CXXTypeidExprClass:132case Expr::CXXUuidofExprClass:133// Unresolved lookups and uncorrected typos get classified as lvalues.134// FIXME: Is this wise? Should they get their own kind?135case Expr::UnresolvedLookupExprClass:136case Expr::UnresolvedMemberExprClass:137case Expr::TypoExprClass:138case Expr::DependentCoawaitExprClass:139case Expr::CXXDependentScopeMemberExprClass:140case Expr::DependentScopeDeclRefExprClass:141// ObjC instance variables are lvalues142// FIXME: ObjC++0x might have different rules143case Expr::ObjCIvarRefExprClass:144case Expr::FunctionParmPackExprClass:145case Expr::MSPropertyRefExprClass:146case Expr::MSPropertySubscriptExprClass:147case Expr::ArraySectionExprClass:148case Expr::OMPArrayShapingExprClass:149case Expr::OMPIteratorExprClass:150return Cl::CL_LValue;151152// C99 6.5.2.5p5 says that compound literals are lvalues.153// In C++, they're prvalue temporaries, except for file-scope arrays.154case Expr::CompoundLiteralExprClass:155return !E->isLValue() ? ClassifyTemporary(E->getType()) : Cl::CL_LValue;156157// Expressions that are prvalues.158case Expr::CXXBoolLiteralExprClass:159case Expr::CXXPseudoDestructorExprClass:160case Expr::UnaryExprOrTypeTraitExprClass:161case Expr::CXXNewExprClass:162case Expr::CXXNullPtrLiteralExprClass:163case Expr::ImaginaryLiteralClass:164case Expr::GNUNullExprClass:165case Expr::OffsetOfExprClass:166case Expr::CXXThrowExprClass:167case Expr::ShuffleVectorExprClass:168case Expr::ConvertVectorExprClass:169case Expr::IntegerLiteralClass:170case Expr::FixedPointLiteralClass:171case Expr::CharacterLiteralClass:172case Expr::AddrLabelExprClass:173case Expr::CXXDeleteExprClass:174case Expr::ImplicitValueInitExprClass:175case Expr::BlockExprClass:176case Expr::FloatingLiteralClass:177case Expr::CXXNoexceptExprClass:178case Expr::CXXScalarValueInitExprClass:179case Expr::TypeTraitExprClass:180case Expr::ArrayTypeTraitExprClass:181case Expr::ExpressionTraitExprClass:182case Expr::ObjCSelectorExprClass:183case Expr::ObjCProtocolExprClass:184case Expr::ObjCStringLiteralClass:185case Expr::ObjCBoxedExprClass:186case Expr::ObjCArrayLiteralClass:187case Expr::ObjCDictionaryLiteralClass:188case Expr::ObjCBoolLiteralExprClass:189case Expr::ObjCAvailabilityCheckExprClass:190case Expr::ParenListExprClass:191case Expr::SizeOfPackExprClass:192case Expr::SubstNonTypeTemplateParmPackExprClass:193case Expr::AsTypeExprClass:194case Expr::ObjCIndirectCopyRestoreExprClass:195case Expr::AtomicExprClass:196case Expr::CXXFoldExprClass:197case Expr::ArrayInitLoopExprClass:198case Expr::ArrayInitIndexExprClass:199case Expr::NoInitExprClass:200case Expr::DesignatedInitUpdateExprClass:201case Expr::SourceLocExprClass:202case Expr::ConceptSpecializationExprClass:203case Expr::RequiresExprClass:204return Cl::CL_PRValue;205206case Expr::EmbedExprClass:207// Nominally, this just goes through as a PRValue until we actually expand208// it and check it.209return Cl::CL_PRValue;210211// Make HLSL this reference-like212case Expr::CXXThisExprClass:213return Lang.HLSL ? Cl::CL_LValue : Cl::CL_PRValue;214215case Expr::ConstantExprClass:216return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());217218// Next come the complicated cases.219case Expr::SubstNonTypeTemplateParmExprClass:220return ClassifyInternal(Ctx,221cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());222223case Expr::PackIndexingExprClass: {224// A pack-index-expression always expands to an id-expression.225// Consider it as an LValue expression.226if (cast<PackIndexingExpr>(E)->isInstantiationDependent())227return Cl::CL_LValue;228return ClassifyInternal(Ctx, cast<PackIndexingExpr>(E)->getSelectedExpr());229}230231// C, C++98 [expr.sub]p1: The result is an lvalue of type "T".232// C++11 (DR1213): in the case of an array operand, the result is an lvalue233// if that operand is an lvalue and an xvalue otherwise.234// Subscripting vector types is more like member access.235case Expr::ArraySubscriptExprClass:236if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())237return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());238if (Lang.CPlusPlus11) {239// Step over the array-to-pointer decay if present, but not over the240// temporary materialization.241auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();242if (Base->getType()->isArrayType())243return ClassifyInternal(Ctx, Base);244}245return Cl::CL_LValue;246247// Subscripting matrix types behaves like member accesses.248case Expr::MatrixSubscriptExprClass:249return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());250251// C++ [expr.prim.general]p3: The result is an lvalue if the entity is a252// function or variable and a prvalue otherwise.253case Expr::DeclRefExprClass:254if (E->getType() == Ctx.UnknownAnyTy)255return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())256? Cl::CL_PRValue : Cl::CL_LValue;257return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());258259// Member access is complex.260case Expr::MemberExprClass:261return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));262263case Expr::UnaryOperatorClass:264switch (cast<UnaryOperator>(E)->getOpcode()) {265// C++ [expr.unary.op]p1: The unary * operator performs indirection:266// [...] the result is an lvalue referring to the object or function267// to which the expression points.268case UO_Deref:269return Cl::CL_LValue;270271// GNU extensions, simply look through them.272case UO_Extension:273return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());274275// Treat _Real and _Imag basically as if they were member276// expressions: l-value only if the operand is a true l-value.277case UO_Real:278case UO_Imag: {279const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();280Cl::Kinds K = ClassifyInternal(Ctx, Op);281if (K != Cl::CL_LValue) return K;282283if (isa<ObjCPropertyRefExpr>(Op))284return Cl::CL_SubObjCPropertySetting;285return Cl::CL_LValue;286}287288// C++ [expr.pre.incr]p1: The result is the updated operand; it is an289// lvalue, [...]290// Not so in C.291case UO_PreInc:292case UO_PreDec:293return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;294295default:296return Cl::CL_PRValue;297}298299case Expr::RecoveryExprClass:300case Expr::OpaqueValueExprClass:301return ClassifyExprValueKind(Lang, E, E->getValueKind());302303// Pseudo-object expressions can produce l-values with reference magic.304case Expr::PseudoObjectExprClass:305return ClassifyExprValueKind(Lang, E,306cast<PseudoObjectExpr>(E)->getValueKind());307308// Implicit casts are lvalues if they're lvalue casts. Other than that, we309// only specifically record class temporaries.310case Expr::ImplicitCastExprClass:311return ClassifyExprValueKind(Lang, E, E->getValueKind());312313// C++ [expr.prim.general]p4: The presence of parentheses does not affect314// whether the expression is an lvalue.315case Expr::ParenExprClass:316return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());317318// C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,319// or a void expression if its result expression is, respectively, an320// lvalue, a function designator, or a void expression.321case Expr::GenericSelectionExprClass:322if (cast<GenericSelectionExpr>(E)->isResultDependent())323return Cl::CL_PRValue;324return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());325326case Expr::BinaryOperatorClass:327case Expr::CompoundAssignOperatorClass:328// C doesn't have any binary expressions that are lvalues.329if (Lang.CPlusPlus)330return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));331return Cl::CL_PRValue;332333case Expr::CallExprClass:334case Expr::CXXOperatorCallExprClass:335case Expr::CXXMemberCallExprClass:336case Expr::UserDefinedLiteralClass:337case Expr::CUDAKernelCallExprClass:338return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));339340case Expr::CXXRewrittenBinaryOperatorClass:341return ClassifyInternal(342Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());343344// __builtin_choose_expr is equivalent to the chosen expression.345case Expr::ChooseExprClass:346return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());347348// Extended vector element access is an lvalue unless there are duplicates349// in the shuffle expression.350case Expr::ExtVectorElementExprClass:351if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())352return Cl::CL_DuplicateVectorComponents;353if (cast<ExtVectorElementExpr>(E)->isArrow())354return Cl::CL_LValue;355return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());356357// Simply look at the actual default argument.358case Expr::CXXDefaultArgExprClass:359return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());360361// Same idea for default initializers.362case Expr::CXXDefaultInitExprClass:363return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());364365// Same idea for temporary binding.366case Expr::CXXBindTemporaryExprClass:367return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());368369// And the cleanups guard.370case Expr::ExprWithCleanupsClass:371return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());372373// Casts depend completely on the target type. All casts work the same.374case Expr::CStyleCastExprClass:375case Expr::CXXFunctionalCastExprClass:376case Expr::CXXStaticCastExprClass:377case Expr::CXXDynamicCastExprClass:378case Expr::CXXReinterpretCastExprClass:379case Expr::CXXConstCastExprClass:380case Expr::CXXAddrspaceCastExprClass:381case Expr::ObjCBridgedCastExprClass:382case Expr::BuiltinBitCastExprClass:383// Only in C++ can casts be interesting at all.384if (!Lang.CPlusPlus) return Cl::CL_PRValue;385return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());386387case Expr::CXXUnresolvedConstructExprClass:388return ClassifyUnnamed(Ctx,389cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());390391case Expr::BinaryConditionalOperatorClass: {392if (!Lang.CPlusPlus) return Cl::CL_PRValue;393const auto *co = cast<BinaryConditionalOperator>(E);394return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());395}396397case Expr::ConditionalOperatorClass: {398// Once again, only C++ is interesting.399if (!Lang.CPlusPlus) return Cl::CL_PRValue;400const auto *co = cast<ConditionalOperator>(E);401return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());402}403404// ObjC message sends are effectively function calls, if the target function405// is known.406case Expr::ObjCMessageExprClass:407if (const ObjCMethodDecl *Method =408cast<ObjCMessageExpr>(E)->getMethodDecl()) {409Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());410return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;411}412return Cl::CL_PRValue;413414// Some C++ expressions are always class temporaries.415case Expr::CXXConstructExprClass:416case Expr::CXXInheritedCtorInitExprClass:417case Expr::CXXTemporaryObjectExprClass:418case Expr::LambdaExprClass:419case Expr::CXXStdInitializerListExprClass:420return Cl::CL_ClassTemporary;421422case Expr::VAArgExprClass:423return ClassifyUnnamed(Ctx, E->getType());424425case Expr::DesignatedInitExprClass:426return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());427428case Expr::StmtExprClass: {429const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();430if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))431return ClassifyUnnamed(Ctx, LastExpr->getType());432return Cl::CL_PRValue;433}434435case Expr::PackExpansionExprClass:436return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());437438case Expr::MaterializeTemporaryExprClass:439return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()440? Cl::CL_LValue441: Cl::CL_XValue;442443case Expr::InitListExprClass:444// An init list can be an lvalue if it is bound to a reference and445// contains only one element. In that case, we look at that element446// for an exact classification. Init list creation takes care of the447// value kind for us, so we only need to fine-tune.448if (E->isPRValue())449return ClassifyExprValueKind(Lang, E, E->getValueKind());450assert(cast<InitListExpr>(E)->getNumInits() == 1 &&451"Only 1-element init lists can be glvalues.");452return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));453454case Expr::CoawaitExprClass:455case Expr::CoyieldExprClass:456return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());457case Expr::SYCLUniqueStableNameExprClass:458return Cl::CL_PRValue;459break;460461case Expr::CXXParenListInitExprClass:462if (isa<ArrayType>(E->getType()))463return Cl::CL_ArrayTemporary;464return Cl::CL_ClassTemporary;465}466467llvm_unreachable("unhandled expression kind in classification");468}469470/// ClassifyDecl - Return the classification of an expression referencing the471/// given declaration.472static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {473// C++ [expr.prim.general]p6: The result is an lvalue if the entity is a474// function, variable, or data member and a prvalue otherwise.475// In C, functions are not lvalues.476// In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an477// lvalue unless it's a reference type (C++ [temp.param]p6), so we need to478// special-case this.479480if (const auto *M = dyn_cast<CXXMethodDecl>(D)) {481if (M->isImplicitObjectMemberFunction())482return Cl::CL_MemberFunction;483if (M->isStatic())484return Cl::CL_LValue;485return Cl::CL_PRValue;486}487488bool islvalue;489if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))490islvalue = NTTParm->getType()->isReferenceType() ||491NTTParm->getType()->isRecordType();492else493islvalue =494isa<VarDecl, FieldDecl, IndirectFieldDecl, BindingDecl, MSGuidDecl,495UnnamedGlobalConstantDecl, TemplateParamObjectDecl>(D) ||496(Ctx.getLangOpts().CPlusPlus &&497(isa<FunctionDecl, MSPropertyDecl, FunctionTemplateDecl>(D)));498499return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;500}501502/// ClassifyUnnamed - Return the classification of an expression yielding an503/// unnamed value of the given type. This applies in particular to function504/// calls and casts.505static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {506// In C, function calls are always rvalues.507if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;508509// C++ [expr.call]p10: A function call is an lvalue if the result type is an510// lvalue reference type or an rvalue reference to function type, an xvalue511// if the result type is an rvalue reference to object type, and a prvalue512// otherwise.513if (T->isLValueReferenceType())514return Cl::CL_LValue;515const auto *RV = T->getAs<RValueReferenceType>();516if (!RV) // Could still be a class temporary, though.517return ClassifyTemporary(T);518519return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;520}521522static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {523if (E->getType() == Ctx.UnknownAnyTy)524return (isa<FunctionDecl>(E->getMemberDecl())525? Cl::CL_PRValue : Cl::CL_LValue);526527// Handle C first, it's easier.528if (!Ctx.getLangOpts().CPlusPlus) {529// C99 6.5.2.3p3530// For dot access, the expression is an lvalue if the first part is. For531// arrow access, it always is an lvalue.532if (E->isArrow())533return Cl::CL_LValue;534// ObjC property accesses are not lvalues, but get special treatment.535Expr *Base = E->getBase()->IgnoreParens();536if (isa<ObjCPropertyRefExpr>(Base))537return Cl::CL_SubObjCPropertySetting;538return ClassifyInternal(Ctx, Base);539}540541NamedDecl *Member = E->getMemberDecl();542// C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.543// C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then544// E1.E2 is an lvalue.545if (const auto *Value = dyn_cast<ValueDecl>(Member))546if (Value->getType()->isReferenceType())547return Cl::CL_LValue;548549// Otherwise, one of the following rules applies.550// -- If E2 is a static member [...] then E1.E2 is an lvalue.551if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())552return Cl::CL_LValue;553554// -- If E2 is a non-static data member [...]. If E1 is an lvalue, then555// E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;556// otherwise, it is a prvalue.557if (isa<FieldDecl>(Member)) {558// *E1 is an lvalue559if (E->isArrow())560return Cl::CL_LValue;561Expr *Base = E->getBase()->IgnoreParenImpCasts();562if (isa<ObjCPropertyRefExpr>(Base))563return Cl::CL_SubObjCPropertySetting;564return ClassifyInternal(Ctx, E->getBase());565}566567// -- If E2 is a [...] member function, [...]568// -- If it refers to a static member function [...], then E1.E2 is an569// lvalue; [...]570// -- Otherwise [...] E1.E2 is a prvalue.571if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) {572if (Method->isStatic())573return Cl::CL_LValue;574if (Method->isImplicitObjectMemberFunction())575return Cl::CL_MemberFunction;576return Cl::CL_PRValue;577}578579// -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.580// So is everything else we haven't handled yet.581return Cl::CL_PRValue;582}583584static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {585assert(Ctx.getLangOpts().CPlusPlus &&586"This is only relevant for C++.");587// C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.588// Except we override this for writes to ObjC properties.589if (E->isAssignmentOp())590return (E->getLHS()->getObjectKind() == OK_ObjCProperty591? Cl::CL_PRValue : Cl::CL_LValue);592593// C++ [expr.comma]p1: the result is of the same value category as its right594// operand, [...].595if (E->getOpcode() == BO_Comma)596return ClassifyInternal(Ctx, E->getRHS());597598// C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand599// is a pointer to a data member is of the same value category as its first600// operand.601if (E->getOpcode() == BO_PtrMemD)602return (E->getType()->isFunctionType() ||603E->hasPlaceholderType(BuiltinType::BoundMember))604? Cl::CL_MemberFunction605: ClassifyInternal(Ctx, E->getLHS());606607// C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its608// second operand is a pointer to data member and a prvalue otherwise.609if (E->getOpcode() == BO_PtrMemI)610return (E->getType()->isFunctionType() ||611E->hasPlaceholderType(BuiltinType::BoundMember))612? Cl::CL_MemberFunction613: Cl::CL_LValue;614615// All other binary operations are prvalues.616return Cl::CL_PRValue;617}618619static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,620const Expr *False) {621assert(Ctx.getLangOpts().CPlusPlus &&622"This is only relevant for C++.");623624// C++ [expr.cond]p2625// If either the second or the third operand has type (cv) void,626// one of the following shall hold:627if (True->getType()->isVoidType() || False->getType()->isVoidType()) {628// The second or the third operand (but not both) is a (possibly629// parenthesized) throw-expression; the result is of the [...] value630// category of the other.631bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());632bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());633if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)634: (FalseIsThrow ? True : nullptr))635return ClassifyInternal(Ctx, NonThrow);636637// [Otherwise] the result [...] is a prvalue.638return Cl::CL_PRValue;639}640641// Note that at this point, we have already performed all conversions642// according to [expr.cond]p3.643// C++ [expr.cond]p4: If the second and third operands are glvalues of the644// same value category [...], the result is of that [...] value category.645// C++ [expr.cond]p5: Otherwise, the result is a prvalue.646Cl::Kinds LCl = ClassifyInternal(Ctx, True),647RCl = ClassifyInternal(Ctx, False);648return LCl == RCl ? LCl : Cl::CL_PRValue;649}650651static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,652Cl::Kinds Kind, SourceLocation &Loc) {653// As a general rule, we only care about lvalues. But there are some rvalues654// for which we want to generate special results.655if (Kind == Cl::CL_PRValue) {656// For the sake of better diagnostics, we want to specifically recognize657// use of the GCC cast-as-lvalue extension.658if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {659if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {660Loc = CE->getExprLoc();661return Cl::CM_LValueCast;662}663}664}665if (Kind != Cl::CL_LValue)666return Cl::CM_RValue;667668// This is the lvalue case.669// Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)670if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())671return Cl::CM_Function;672673// Assignment to a property in ObjC is an implicit setter access. But a674// setter might not exist.675if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {676if (Expr->isImplicitProperty() &&677Expr->getImplicitPropertySetter() == nullptr)678return Cl::CM_NoSetterProperty;679}680681CanQualType CT = Ctx.getCanonicalType(E->getType());682// Const stuff is obviously not modifiable.683if (CT.isConstQualified())684return Cl::CM_ConstQualified;685if (Ctx.getLangOpts().OpenCL &&686CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant)687return Cl::CM_ConstAddrSpace;688689// Arrays are not modifiable, only their elements are.690if (CT->isArrayType())691return Cl::CM_ArrayType;692// Incomplete types are not modifiable.693if (CT->isIncompleteType())694return Cl::CM_IncompleteType;695696// Records with any const fields (recursively) are not modifiable.697if (const RecordType *R = CT->getAs<RecordType>())698if (R->hasConstFields())699return Cl::CM_ConstQualifiedField;700701return Cl::CM_Modifiable;702}703704Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {705Classification VC = Classify(Ctx);706switch (VC.getKind()) {707case Cl::CL_LValue: return LV_Valid;708case Cl::CL_XValue: return LV_InvalidExpression;709case Cl::CL_Function: return LV_NotObjectType;710case Cl::CL_Void: return LV_InvalidExpression;711case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;712case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;713case Cl::CL_MemberFunction: return LV_MemberFunction;714case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;715case Cl::CL_ClassTemporary: return LV_ClassTemporary;716case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;717case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;718case Cl::CL_PRValue: return LV_InvalidExpression;719}720llvm_unreachable("Unhandled kind");721}722723Expr::isModifiableLvalueResult724Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {725SourceLocation dummy;726Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);727switch (VC.getKind()) {728case Cl::CL_LValue: break;729case Cl::CL_XValue: return MLV_InvalidExpression;730case Cl::CL_Function: return MLV_NotObjectType;731case Cl::CL_Void: return MLV_InvalidExpression;732case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;733case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;734case Cl::CL_MemberFunction: return MLV_MemberFunction;735case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;736case Cl::CL_ClassTemporary: return MLV_ClassTemporary;737case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;738case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;739case Cl::CL_PRValue:740return VC.getModifiable() == Cl::CM_LValueCast ?741MLV_LValueCast : MLV_InvalidExpression;742}743assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");744switch (VC.getModifiable()) {745case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");746case Cl::CM_Modifiable: return MLV_Valid;747case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");748case Cl::CM_Function: return MLV_NotObjectType;749case Cl::CM_LValueCast:750llvm_unreachable("CM_LValueCast and CL_LValue don't match");751case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;752case Cl::CM_ConstQualified: return MLV_ConstQualified;753case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField;754case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace;755case Cl::CM_ArrayType: return MLV_ArrayType;756case Cl::CM_IncompleteType: return MLV_IncompleteType;757}758llvm_unreachable("Unhandled modifiable type");759}760761762