1 //===- ExprClassification.cpp - Expression AST Node Implementation --------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements Expr::classify. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/Expr.h" 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/DeclCXX.h" 16 #include "clang/AST/DeclObjC.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/ExprObjC.h" 20 #include "llvm/Support/ErrorHandling.h" 21 22 using namespace clang; 23 24 using Cl = Expr::Classification; 25 26 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E); 27 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D); 28 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T); 29 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E); 30 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E); 31 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, 32 const Expr *trueExpr, 33 const Expr *falseExpr); 34 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 35 Cl::Kinds Kind, SourceLocation &Loc); 36 37 Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const { 38 assert(!TR->isReferenceType() && "Expressions can't have reference type."); 39 40 Cl::Kinds kind = ClassifyInternal(Ctx, this); 41 // C99 6.3.2.1: An lvalue is an expression with an object type or an 42 // incomplete type other than void. 43 if (!Ctx.getLangOpts().CPlusPlus) { 44 // Thus, no functions. 45 if (TR->isFunctionType() || TR == Ctx.OverloadTy) 46 kind = Cl::CL_Function; 47 // No void either, but qualified void is OK because it is "other than void". 48 // Void "lvalues" are classified as addressable void values, which are void 49 // expressions whose address can be taken. 50 else if (TR->isVoidType() && !TR.hasQualifiers()) 51 kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void); 52 } 53 54 // Enable this assertion for testing. 55 switch (kind) { 56 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break; 57 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break; 58 case Cl::CL_Function: 59 case Cl::CL_Void: 60 case Cl::CL_AddressableVoid: 61 case Cl::CL_DuplicateVectorComponents: 62 case Cl::CL_MemberFunction: 63 case Cl::CL_SubObjCPropertySetting: 64 case Cl::CL_ClassTemporary: 65 case Cl::CL_ArrayTemporary: 66 case Cl::CL_ObjCMessageRValue: 67 case Cl::CL_PRValue: 68 assert(getValueKind() == VK_PRValue); 69 break; 70 } 71 72 Cl::ModifiableType modifiable = Cl::CM_Untested; 73 if (Loc) 74 modifiable = IsModifiable(Ctx, this, kind, *Loc); 75 return Classification(kind, modifiable); 76 } 77 78 /// Classify an expression which creates a temporary, based on its type. 79 static Cl::Kinds ClassifyTemporary(QualType T) { 80 if (T->isRecordType()) 81 return Cl::CL_ClassTemporary; 82 if (T->isArrayType()) 83 return Cl::CL_ArrayTemporary; 84 85 // No special classification: these don't behave differently from normal 86 // prvalues. 87 return Cl::CL_PRValue; 88 } 89 90 static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, 91 const Expr *E, 92 ExprValueKind Kind) { 93 switch (Kind) { 94 case VK_PRValue: 95 return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue; 96 case VK_LValue: 97 return Cl::CL_LValue; 98 case VK_XValue: 99 return Cl::CL_XValue; 100 } 101 llvm_unreachable("Invalid value category of implicit cast."); 102 } 103 104 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { 105 // This function takes the first stab at classifying expressions. 106 const LangOptions &Lang = Ctx.getLangOpts(); 107 108 switch (E->getStmtClass()) { 109 case Stmt::NoStmtClass: 110 #define ABSTRACT_STMT(Kind) 111 #define STMT(Kind, Base) case Expr::Kind##Class: 112 #define EXPR(Kind, Base) 113 #include "clang/AST/StmtNodes.inc" 114 llvm_unreachable("cannot classify a statement"); 115 116 // First come the expressions that are always lvalues, unconditionally. 117 case Expr::ObjCIsaExprClass: 118 // C++ [expr.prim.general]p1: A string literal is an lvalue. 119 case Expr::StringLiteralClass: 120 // @encode is equivalent to its string 121 case Expr::ObjCEncodeExprClass: 122 // __func__ and friends are too. 123 case Expr::PredefinedExprClass: 124 // Property references are lvalues 125 case Expr::ObjCSubscriptRefExprClass: 126 case Expr::ObjCPropertyRefExprClass: 127 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of... 128 case Expr::CXXTypeidExprClass: 129 case Expr::CXXUuidofExprClass: 130 // Unresolved lookups and uncorrected typos get classified as lvalues. 131 // FIXME: Is this wise? Should they get their own kind? 132 case Expr::UnresolvedLookupExprClass: 133 case Expr::UnresolvedMemberExprClass: 134 case Expr::TypoExprClass: 135 case Expr::DependentCoawaitExprClass: 136 case Expr::CXXDependentScopeMemberExprClass: 137 case Expr::DependentScopeDeclRefExprClass: 138 // ObjC instance variables are lvalues 139 // FIXME: ObjC++0x might have different rules 140 case Expr::ObjCIvarRefExprClass: 141 case Expr::FunctionParmPackExprClass: 142 case Expr::MSPropertyRefExprClass: 143 case Expr::MSPropertySubscriptExprClass: 144 case Expr::OMPArraySectionExprClass: 145 case Expr::OMPArrayShapingExprClass: 146 case Expr::OMPIteratorExprClass: 147 return Cl::CL_LValue; 148 149 // C99 6.5.2.5p5 says that compound literals are lvalues. 150 // In C++, they're prvalue temporaries, except for file-scope arrays. 151 case Expr::CompoundLiteralExprClass: 152 return !E->isLValue() ? ClassifyTemporary(E->getType()) : Cl::CL_LValue; 153 154 // Expressions that are prvalues. 155 case Expr::CXXBoolLiteralExprClass: 156 case Expr::CXXPseudoDestructorExprClass: 157 case Expr::UnaryExprOrTypeTraitExprClass: 158 case Expr::CXXNewExprClass: 159 case Expr::CXXThisExprClass: 160 case Expr::CXXNullPtrLiteralExprClass: 161 case Expr::ImaginaryLiteralClass: 162 case Expr::GNUNullExprClass: 163 case Expr::OffsetOfExprClass: 164 case Expr::CXXThrowExprClass: 165 case Expr::ShuffleVectorExprClass: 166 case Expr::ConvertVectorExprClass: 167 case Expr::IntegerLiteralClass: 168 case Expr::FixedPointLiteralClass: 169 case Expr::CharacterLiteralClass: 170 case Expr::AddrLabelExprClass: 171 case Expr::CXXDeleteExprClass: 172 case Expr::ImplicitValueInitExprClass: 173 case Expr::BlockExprClass: 174 case Expr::FloatingLiteralClass: 175 case Expr::CXXNoexceptExprClass: 176 case Expr::CXXScalarValueInitExprClass: 177 case Expr::TypeTraitExprClass: 178 case Expr::ArrayTypeTraitExprClass: 179 case Expr::ExpressionTraitExprClass: 180 case Expr::ObjCSelectorExprClass: 181 case Expr::ObjCProtocolExprClass: 182 case Expr::ObjCStringLiteralClass: 183 case Expr::ObjCBoxedExprClass: 184 case Expr::ObjCArrayLiteralClass: 185 case Expr::ObjCDictionaryLiteralClass: 186 case Expr::ObjCBoolLiteralExprClass: 187 case Expr::ObjCAvailabilityCheckExprClass: 188 case Expr::ParenListExprClass: 189 case Expr::SizeOfPackExprClass: 190 case Expr::SubstNonTypeTemplateParmPackExprClass: 191 case Expr::AsTypeExprClass: 192 case Expr::ObjCIndirectCopyRestoreExprClass: 193 case Expr::AtomicExprClass: 194 case Expr::CXXFoldExprClass: 195 case Expr::ArrayInitLoopExprClass: 196 case Expr::ArrayInitIndexExprClass: 197 case Expr::NoInitExprClass: 198 case Expr::DesignatedInitUpdateExprClass: 199 case Expr::SourceLocExprClass: 200 case Expr::ConceptSpecializationExprClass: 201 case Expr::RequiresExprClass: 202 return Cl::CL_PRValue; 203 204 case Expr::ConstantExprClass: 205 return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr()); 206 207 // Next come the complicated cases. 208 case Expr::SubstNonTypeTemplateParmExprClass: 209 return ClassifyInternal(Ctx, 210 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 211 212 // C, C++98 [expr.sub]p1: The result is an lvalue of type "T". 213 // C++11 (DR1213): in the case of an array operand, the result is an lvalue 214 // if that operand is an lvalue and an xvalue otherwise. 215 // Subscripting vector types is more like member access. 216 case Expr::ArraySubscriptExprClass: 217 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) 218 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); 219 if (Lang.CPlusPlus11) { 220 // Step over the array-to-pointer decay if present, but not over the 221 // temporary materialization. 222 auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts(); 223 if (Base->getType()->isArrayType()) 224 return ClassifyInternal(Ctx, Base); 225 } 226 return Cl::CL_LValue; 227 228 // Subscripting matrix types behaves like member accesses. 229 case Expr::MatrixSubscriptExprClass: 230 return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase()); 231 232 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a 233 // function or variable and a prvalue otherwise. 234 case Expr::DeclRefExprClass: 235 if (E->getType() == Ctx.UnknownAnyTy) 236 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) 237 ? Cl::CL_PRValue : Cl::CL_LValue; 238 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); 239 240 // Member access is complex. 241 case Expr::MemberExprClass: 242 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); 243 244 case Expr::UnaryOperatorClass: 245 switch (cast<UnaryOperator>(E)->getOpcode()) { 246 // C++ [expr.unary.op]p1: The unary * operator performs indirection: 247 // [...] the result is an lvalue referring to the object or function 248 // to which the expression points. 249 case UO_Deref: 250 return Cl::CL_LValue; 251 252 // GNU extensions, simply look through them. 253 case UO_Extension: 254 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); 255 256 // Treat _Real and _Imag basically as if they were member 257 // expressions: l-value only if the operand is a true l-value. 258 case UO_Real: 259 case UO_Imag: { 260 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 261 Cl::Kinds K = ClassifyInternal(Ctx, Op); 262 if (K != Cl::CL_LValue) return K; 263 264 if (isa<ObjCPropertyRefExpr>(Op)) 265 return Cl::CL_SubObjCPropertySetting; 266 return Cl::CL_LValue; 267 } 268 269 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an 270 // lvalue, [...] 271 // Not so in C. 272 case UO_PreInc: 273 case UO_PreDec: 274 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; 275 276 default: 277 return Cl::CL_PRValue; 278 } 279 280 case Expr::RecoveryExprClass: 281 case Expr::OpaqueValueExprClass: 282 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 283 284 // Pseudo-object expressions can produce l-values with reference magic. 285 case Expr::PseudoObjectExprClass: 286 return ClassifyExprValueKind(Lang, E, 287 cast<PseudoObjectExpr>(E)->getValueKind()); 288 289 // Implicit casts are lvalues if they're lvalue casts. Other than that, we 290 // only specifically record class temporaries. 291 case Expr::ImplicitCastExprClass: 292 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 293 294 // C++ [expr.prim.general]p4: The presence of parentheses does not affect 295 // whether the expression is an lvalue. 296 case Expr::ParenExprClass: 297 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); 298 299 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, 300 // or a void expression if its result expression is, respectively, an 301 // lvalue, a function designator, or a void expression. 302 case Expr::GenericSelectionExprClass: 303 if (cast<GenericSelectionExpr>(E)->isResultDependent()) 304 return Cl::CL_PRValue; 305 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); 306 307 case Expr::BinaryOperatorClass: 308 case Expr::CompoundAssignOperatorClass: 309 // C doesn't have any binary expressions that are lvalues. 310 if (Lang.CPlusPlus) 311 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); 312 return Cl::CL_PRValue; 313 314 case Expr::CallExprClass: 315 case Expr::CXXOperatorCallExprClass: 316 case Expr::CXXMemberCallExprClass: 317 case Expr::UserDefinedLiteralClass: 318 case Expr::CUDAKernelCallExprClass: 319 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx)); 320 321 case Expr::CXXRewrittenBinaryOperatorClass: 322 return ClassifyInternal( 323 Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm()); 324 325 // __builtin_choose_expr is equivalent to the chosen expression. 326 case Expr::ChooseExprClass: 327 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr()); 328 329 // Extended vector element access is an lvalue unless there are duplicates 330 // in the shuffle expression. 331 case Expr::ExtVectorElementExprClass: 332 if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements()) 333 return Cl::CL_DuplicateVectorComponents; 334 if (cast<ExtVectorElementExpr>(E)->isArrow()) 335 return Cl::CL_LValue; 336 return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase()); 337 338 // Simply look at the actual default argument. 339 case Expr::CXXDefaultArgExprClass: 340 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); 341 342 // Same idea for default initializers. 343 case Expr::CXXDefaultInitExprClass: 344 return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); 345 346 // Same idea for temporary binding. 347 case Expr::CXXBindTemporaryExprClass: 348 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 349 350 // And the cleanups guard. 351 case Expr::ExprWithCleanupsClass: 352 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); 353 354 // Casts depend completely on the target type. All casts work the same. 355 case Expr::CStyleCastExprClass: 356 case Expr::CXXFunctionalCastExprClass: 357 case Expr::CXXStaticCastExprClass: 358 case Expr::CXXDynamicCastExprClass: 359 case Expr::CXXReinterpretCastExprClass: 360 case Expr::CXXConstCastExprClass: 361 case Expr::CXXAddrspaceCastExprClass: 362 case Expr::ObjCBridgedCastExprClass: 363 case Expr::BuiltinBitCastExprClass: 364 // Only in C++ can casts be interesting at all. 365 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 366 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); 367 368 case Expr::CXXUnresolvedConstructExprClass: 369 return ClassifyUnnamed(Ctx, 370 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); 371 372 case Expr::BinaryConditionalOperatorClass: { 373 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 374 const auto *co = cast<BinaryConditionalOperator>(E); 375 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 376 } 377 378 case Expr::ConditionalOperatorClass: { 379 // Once again, only C++ is interesting. 380 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 381 const auto *co = cast<ConditionalOperator>(E); 382 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 383 } 384 385 // ObjC message sends are effectively function calls, if the target function 386 // is known. 387 case Expr::ObjCMessageExprClass: 388 if (const ObjCMethodDecl *Method = 389 cast<ObjCMessageExpr>(E)->getMethodDecl()) { 390 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType()); 391 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; 392 } 393 return Cl::CL_PRValue; 394 395 // Some C++ expressions are always class temporaries. 396 case Expr::CXXConstructExprClass: 397 case Expr::CXXInheritedCtorInitExprClass: 398 case Expr::CXXTemporaryObjectExprClass: 399 case Expr::LambdaExprClass: 400 case Expr::CXXStdInitializerListExprClass: 401 return Cl::CL_ClassTemporary; 402 403 case Expr::VAArgExprClass: 404 return ClassifyUnnamed(Ctx, E->getType()); 405 406 case Expr::DesignatedInitExprClass: 407 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); 408 409 case Expr::StmtExprClass: { 410 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); 411 if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) 412 return ClassifyUnnamed(Ctx, LastExpr->getType()); 413 return Cl::CL_PRValue; 414 } 415 416 case Expr::PackExpansionExprClass: 417 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); 418 419 case Expr::MaterializeTemporaryExprClass: 420 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() 421 ? Cl::CL_LValue 422 : Cl::CL_XValue; 423 424 case Expr::InitListExprClass: 425 // An init list can be an lvalue if it is bound to a reference and 426 // contains only one element. In that case, we look at that element 427 // for an exact classification. Init list creation takes care of the 428 // value kind for us, so we only need to fine-tune. 429 if (E->isPRValue()) 430 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 431 assert(cast<InitListExpr>(E)->getNumInits() == 1 && 432 "Only 1-element init lists can be glvalues."); 433 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); 434 435 case Expr::CoawaitExprClass: 436 case Expr::CoyieldExprClass: 437 return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr()); 438 case Expr::SYCLUniqueStableNameExprClass: 439 return Cl::CL_PRValue; 440 break; 441 } 442 443 llvm_unreachable("unhandled expression kind in classification"); 444 } 445 446 /// ClassifyDecl - Return the classification of an expression referencing the 447 /// given declaration. 448 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { 449 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a 450 // function, variable, or data member and a prvalue otherwise. 451 // In C, functions are not lvalues. 452 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an 453 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to 454 // special-case this. 455 456 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 457 return Cl::CL_MemberFunction; 458 459 bool islvalue; 460 if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D)) 461 islvalue = NTTParm->getType()->isReferenceType() || 462 NTTParm->getType()->isRecordType(); 463 else 464 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || 465 isa<IndirectFieldDecl>(D) || 466 isa<BindingDecl>(D) || 467 isa<MSGuidDecl>(D) || 468 isa<TemplateParamObjectDecl>(D) || 469 (Ctx.getLangOpts().CPlusPlus && 470 (isa<FunctionDecl>(D) || isa<MSPropertyDecl>(D) || 471 isa<FunctionTemplateDecl>(D))); 472 473 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; 474 } 475 476 /// ClassifyUnnamed - Return the classification of an expression yielding an 477 /// unnamed value of the given type. This applies in particular to function 478 /// calls and casts. 479 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { 480 // In C, function calls are always rvalues. 481 if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; 482 483 // C++ [expr.call]p10: A function call is an lvalue if the result type is an 484 // lvalue reference type or an rvalue reference to function type, an xvalue 485 // if the result type is an rvalue reference to object type, and a prvalue 486 // otherwise. 487 if (T->isLValueReferenceType()) 488 return Cl::CL_LValue; 489 const auto *RV = T->getAs<RValueReferenceType>(); 490 if (!RV) // Could still be a class temporary, though. 491 return ClassifyTemporary(T); 492 493 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; 494 } 495 496 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { 497 if (E->getType() == Ctx.UnknownAnyTy) 498 return (isa<FunctionDecl>(E->getMemberDecl()) 499 ? Cl::CL_PRValue : Cl::CL_LValue); 500 501 // Handle C first, it's easier. 502 if (!Ctx.getLangOpts().CPlusPlus) { 503 // C99 6.5.2.3p3 504 // For dot access, the expression is an lvalue if the first part is. For 505 // arrow access, it always is an lvalue. 506 if (E->isArrow()) 507 return Cl::CL_LValue; 508 // ObjC property accesses are not lvalues, but get special treatment. 509 Expr *Base = E->getBase()->IgnoreParens(); 510 if (isa<ObjCPropertyRefExpr>(Base)) 511 return Cl::CL_SubObjCPropertySetting; 512 return ClassifyInternal(Ctx, Base); 513 } 514 515 NamedDecl *Member = E->getMemberDecl(); 516 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. 517 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then 518 // E1.E2 is an lvalue. 519 if (const auto *Value = dyn_cast<ValueDecl>(Member)) 520 if (Value->getType()->isReferenceType()) 521 return Cl::CL_LValue; 522 523 // Otherwise, one of the following rules applies. 524 // -- If E2 is a static member [...] then E1.E2 is an lvalue. 525 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) 526 return Cl::CL_LValue; 527 528 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then 529 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; 530 // otherwise, it is a prvalue. 531 if (isa<FieldDecl>(Member)) { 532 // *E1 is an lvalue 533 if (E->isArrow()) 534 return Cl::CL_LValue; 535 Expr *Base = E->getBase()->IgnoreParenImpCasts(); 536 if (isa<ObjCPropertyRefExpr>(Base)) 537 return Cl::CL_SubObjCPropertySetting; 538 return ClassifyInternal(Ctx, E->getBase()); 539 } 540 541 // -- If E2 is a [...] member function, [...] 542 // -- If it refers to a static member function [...], then E1.E2 is an 543 // lvalue; [...] 544 // -- Otherwise [...] E1.E2 is a prvalue. 545 if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) 546 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; 547 548 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. 549 // So is everything else we haven't handled yet. 550 return Cl::CL_PRValue; 551 } 552 553 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { 554 assert(Ctx.getLangOpts().CPlusPlus && 555 "This is only relevant for C++."); 556 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. 557 // Except we override this for writes to ObjC properties. 558 if (E->isAssignmentOp()) 559 return (E->getLHS()->getObjectKind() == OK_ObjCProperty 560 ? Cl::CL_PRValue : Cl::CL_LValue); 561 562 // C++ [expr.comma]p1: the result is of the same value category as its right 563 // operand, [...]. 564 if (E->getOpcode() == BO_Comma) 565 return ClassifyInternal(Ctx, E->getRHS()); 566 567 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand 568 // is a pointer to a data member is of the same value category as its first 569 // operand. 570 if (E->getOpcode() == BO_PtrMemD) 571 return (E->getType()->isFunctionType() || 572 E->hasPlaceholderType(BuiltinType::BoundMember)) 573 ? Cl::CL_MemberFunction 574 : ClassifyInternal(Ctx, E->getLHS()); 575 576 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its 577 // second operand is a pointer to data member and a prvalue otherwise. 578 if (E->getOpcode() == BO_PtrMemI) 579 return (E->getType()->isFunctionType() || 580 E->hasPlaceholderType(BuiltinType::BoundMember)) 581 ? Cl::CL_MemberFunction 582 : Cl::CL_LValue; 583 584 // All other binary operations are prvalues. 585 return Cl::CL_PRValue; 586 } 587 588 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, 589 const Expr *False) { 590 assert(Ctx.getLangOpts().CPlusPlus && 591 "This is only relevant for C++."); 592 593 // C++ [expr.cond]p2 594 // If either the second or the third operand has type (cv) void, 595 // one of the following shall hold: 596 if (True->getType()->isVoidType() || False->getType()->isVoidType()) { 597 // The second or the third operand (but not both) is a (possibly 598 // parenthesized) throw-expression; the result is of the [...] value 599 // category of the other. 600 bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts()); 601 bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts()); 602 if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False) 603 : (FalseIsThrow ? True : nullptr)) 604 return ClassifyInternal(Ctx, NonThrow); 605 606 // [Otherwise] the result [...] is a prvalue. 607 return Cl::CL_PRValue; 608 } 609 610 // Note that at this point, we have already performed all conversions 611 // according to [expr.cond]p3. 612 // C++ [expr.cond]p4: If the second and third operands are glvalues of the 613 // same value category [...], the result is of that [...] value category. 614 // C++ [expr.cond]p5: Otherwise, the result is a prvalue. 615 Cl::Kinds LCl = ClassifyInternal(Ctx, True), 616 RCl = ClassifyInternal(Ctx, False); 617 return LCl == RCl ? LCl : Cl::CL_PRValue; 618 } 619 620 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 621 Cl::Kinds Kind, SourceLocation &Loc) { 622 // As a general rule, we only care about lvalues. But there are some rvalues 623 // for which we want to generate special results. 624 if (Kind == Cl::CL_PRValue) { 625 // For the sake of better diagnostics, we want to specifically recognize 626 // use of the GCC cast-as-lvalue extension. 627 if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) { 628 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { 629 Loc = CE->getExprLoc(); 630 return Cl::CM_LValueCast; 631 } 632 } 633 } 634 if (Kind != Cl::CL_LValue) 635 return Cl::CM_RValue; 636 637 // This is the lvalue case. 638 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) 639 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) 640 return Cl::CM_Function; 641 642 // Assignment to a property in ObjC is an implicit setter access. But a 643 // setter might not exist. 644 if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) { 645 if (Expr->isImplicitProperty() && 646 Expr->getImplicitPropertySetter() == nullptr) 647 return Cl::CM_NoSetterProperty; 648 } 649 650 CanQualType CT = Ctx.getCanonicalType(E->getType()); 651 // Const stuff is obviously not modifiable. 652 if (CT.isConstQualified()) 653 return Cl::CM_ConstQualified; 654 if (Ctx.getLangOpts().OpenCL && 655 CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) 656 return Cl::CM_ConstAddrSpace; 657 658 // Arrays are not modifiable, only their elements are. 659 if (CT->isArrayType()) 660 return Cl::CM_ArrayType; 661 // Incomplete types are not modifiable. 662 if (CT->isIncompleteType()) 663 return Cl::CM_IncompleteType; 664 665 // Records with any const fields (recursively) are not modifiable. 666 if (const RecordType *R = CT->getAs<RecordType>()) 667 if (R->hasConstFields()) 668 return Cl::CM_ConstQualifiedField; 669 670 return Cl::CM_Modifiable; 671 } 672 673 Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { 674 Classification VC = Classify(Ctx); 675 switch (VC.getKind()) { 676 case Cl::CL_LValue: return LV_Valid; 677 case Cl::CL_XValue: return LV_InvalidExpression; 678 case Cl::CL_Function: return LV_NotObjectType; 679 case Cl::CL_Void: return LV_InvalidExpression; 680 case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; 681 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; 682 case Cl::CL_MemberFunction: return LV_MemberFunction; 683 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; 684 case Cl::CL_ClassTemporary: return LV_ClassTemporary; 685 case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; 686 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; 687 case Cl::CL_PRValue: return LV_InvalidExpression; 688 } 689 llvm_unreachable("Unhandled kind"); 690 } 691 692 Expr::isModifiableLvalueResult 693 Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { 694 SourceLocation dummy; 695 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); 696 switch (VC.getKind()) { 697 case Cl::CL_LValue: break; 698 case Cl::CL_XValue: return MLV_InvalidExpression; 699 case Cl::CL_Function: return MLV_NotObjectType; 700 case Cl::CL_Void: return MLV_InvalidExpression; 701 case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; 702 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 703 case Cl::CL_MemberFunction: return MLV_MemberFunction; 704 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; 705 case Cl::CL_ClassTemporary: return MLV_ClassTemporary; 706 case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; 707 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; 708 case Cl::CL_PRValue: 709 return VC.getModifiable() == Cl::CM_LValueCast ? 710 MLV_LValueCast : MLV_InvalidExpression; 711 } 712 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); 713 switch (VC.getModifiable()) { 714 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); 715 case Cl::CM_Modifiable: return MLV_Valid; 716 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); 717 case Cl::CM_Function: return MLV_NotObjectType; 718 case Cl::CM_LValueCast: 719 llvm_unreachable("CM_LValueCast and CL_LValue don't match"); 720 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; 721 case Cl::CM_ConstQualified: return MLV_ConstQualified; 722 case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField; 723 case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace; 724 case Cl::CM_ArrayType: return MLV_ArrayType; 725 case Cl::CM_IncompleteType: return MLV_IncompleteType; 726 } 727 llvm_unreachable("Unhandled modifiable type"); 728 } 729