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