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