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