clang API Documentation
00001 //===--- ExprClassification.cpp - Expression AST Node Implementation ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements Expr::classify. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/AST/Expr.h" 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/DeclCXX.h" 00017 #include "clang/AST/DeclObjC.h" 00018 #include "clang/AST/DeclTemplate.h" 00019 #include "clang/AST/ExprCXX.h" 00020 #include "clang/AST/ExprObjC.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 using namespace clang; 00023 00024 typedef Expr::Classification Cl; 00025 00026 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E); 00027 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D); 00028 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T); 00029 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E); 00030 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E); 00031 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, 00032 const Expr *trueExpr, 00033 const Expr *falseExpr); 00034 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 00035 Cl::Kinds Kind, SourceLocation &Loc); 00036 00037 Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const { 00038 assert(!TR->isReferenceType() && "Expressions can't have reference type."); 00039 00040 Cl::Kinds kind = ClassifyInternal(Ctx, this); 00041 // C99 6.3.2.1: An lvalue is an expression with an object type or an 00042 // incomplete type other than void. 00043 if (!Ctx.getLangOpts().CPlusPlus) { 00044 // Thus, no functions. 00045 if (TR->isFunctionType() || TR == Ctx.OverloadTy) 00046 kind = Cl::CL_Function; 00047 // No void either, but qualified void is OK because it is "other than void". 00048 // Void "lvalues" are classified as addressable void values, which are void 00049 // expressions whose address can be taken. 00050 else if (TR->isVoidType() && !TR.hasQualifiers()) 00051 kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void); 00052 } 00053 00054 // Enable this assertion for testing. 00055 switch (kind) { 00056 case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break; 00057 case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break; 00058 case Cl::CL_Function: 00059 case Cl::CL_Void: 00060 case Cl::CL_AddressableVoid: 00061 case Cl::CL_DuplicateVectorComponents: 00062 case Cl::CL_MemberFunction: 00063 case Cl::CL_SubObjCPropertySetting: 00064 case Cl::CL_ClassTemporary: 00065 case Cl::CL_ArrayTemporary: 00066 case Cl::CL_ObjCMessageRValue: 00067 case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break; 00068 } 00069 00070 Cl::ModifiableType modifiable = Cl::CM_Untested; 00071 if (Loc) 00072 modifiable = IsModifiable(Ctx, this, kind, *Loc); 00073 return Classification(kind, modifiable); 00074 } 00075 00076 /// Classify an expression which creates a temporary, based on its type. 00077 static Cl::Kinds ClassifyTemporary(QualType T) { 00078 if (T->isRecordType()) 00079 return Cl::CL_ClassTemporary; 00080 if (T->isArrayType()) 00081 return Cl::CL_ArrayTemporary; 00082 00083 // No special classification: these don't behave differently from normal 00084 // prvalues. 00085 return Cl::CL_PRValue; 00086 } 00087 00088 static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, 00089 const Expr *E, 00090 ExprValueKind Kind) { 00091 switch (Kind) { 00092 case VK_RValue: 00093 return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue; 00094 case VK_LValue: 00095 return Cl::CL_LValue; 00096 case VK_XValue: 00097 return Cl::CL_XValue; 00098 } 00099 llvm_unreachable("Invalid value category of implicit cast."); 00100 } 00101 00102 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { 00103 // This function takes the first stab at classifying expressions. 00104 const LangOptions &Lang = Ctx.getLangOpts(); 00105 00106 switch (E->getStmtClass()) { 00107 case Stmt::NoStmtClass: 00108 #define ABSTRACT_STMT(Kind) 00109 #define STMT(Kind, Base) case Expr::Kind##Class: 00110 #define EXPR(Kind, Base) 00111 #include "clang/AST/StmtNodes.inc" 00112 llvm_unreachable("cannot classify a statement"); 00113 00114 // First come the expressions that are always lvalues, unconditionally. 00115 case Expr::ObjCIsaExprClass: 00116 // C++ [expr.prim.general]p1: A string literal is an lvalue. 00117 case Expr::StringLiteralClass: 00118 // @encode is equivalent to its string 00119 case Expr::ObjCEncodeExprClass: 00120 // __func__ and friends are too. 00121 case Expr::PredefinedExprClass: 00122 // Property references are lvalues 00123 case Expr::ObjCSubscriptRefExprClass: 00124 case Expr::ObjCPropertyRefExprClass: 00125 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of... 00126 case Expr::CXXTypeidExprClass: 00127 // Unresolved lookups and uncorrected typos get classified as lvalues. 00128 // FIXME: Is this wise? Should they get their own kind? 00129 case Expr::UnresolvedLookupExprClass: 00130 case Expr::UnresolvedMemberExprClass: 00131 case Expr::TypoExprClass: 00132 case Expr::CXXDependentScopeMemberExprClass: 00133 case Expr::DependentScopeDeclRefExprClass: 00134 // ObjC instance variables are lvalues 00135 // FIXME: ObjC++0x might have different rules 00136 case Expr::ObjCIvarRefExprClass: 00137 case Expr::FunctionParmPackExprClass: 00138 case Expr::MSPropertyRefExprClass: 00139 return Cl::CL_LValue; 00140 00141 // C99 6.5.2.5p5 says that compound literals are lvalues. 00142 // In C++, they're prvalue temporaries. 00143 case Expr::CompoundLiteralExprClass: 00144 return Ctx.getLangOpts().CPlusPlus ? ClassifyTemporary(E->getType()) 00145 : Cl::CL_LValue; 00146 00147 // Expressions that are prvalues. 00148 case Expr::CXXBoolLiteralExprClass: 00149 case Expr::CXXPseudoDestructorExprClass: 00150 case Expr::UnaryExprOrTypeTraitExprClass: 00151 case Expr::CXXNewExprClass: 00152 case Expr::CXXThisExprClass: 00153 case Expr::CXXNullPtrLiteralExprClass: 00154 case Expr::ImaginaryLiteralClass: 00155 case Expr::GNUNullExprClass: 00156 case Expr::OffsetOfExprClass: 00157 case Expr::CXXThrowExprClass: 00158 case Expr::ShuffleVectorExprClass: 00159 case Expr::ConvertVectorExprClass: 00160 case Expr::IntegerLiteralClass: 00161 case Expr::CharacterLiteralClass: 00162 case Expr::AddrLabelExprClass: 00163 case Expr::CXXDeleteExprClass: 00164 case Expr::ImplicitValueInitExprClass: 00165 case Expr::BlockExprClass: 00166 case Expr::FloatingLiteralClass: 00167 case Expr::CXXNoexceptExprClass: 00168 case Expr::CXXScalarValueInitExprClass: 00169 case Expr::TypeTraitExprClass: 00170 case Expr::ArrayTypeTraitExprClass: 00171 case Expr::ExpressionTraitExprClass: 00172 case Expr::ObjCSelectorExprClass: 00173 case Expr::ObjCProtocolExprClass: 00174 case Expr::ObjCStringLiteralClass: 00175 case Expr::ObjCBoxedExprClass: 00176 case Expr::ObjCArrayLiteralClass: 00177 case Expr::ObjCDictionaryLiteralClass: 00178 case Expr::ObjCBoolLiteralExprClass: 00179 case Expr::ParenListExprClass: 00180 case Expr::SizeOfPackExprClass: 00181 case Expr::SubstNonTypeTemplateParmPackExprClass: 00182 case Expr::AsTypeExprClass: 00183 case Expr::ObjCIndirectCopyRestoreExprClass: 00184 case Expr::AtomicExprClass: 00185 case Expr::CXXFoldExprClass: 00186 return Cl::CL_PRValue; 00187 00188 // Next come the complicated cases. 00189 case Expr::SubstNonTypeTemplateParmExprClass: 00190 return ClassifyInternal(Ctx, 00191 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 00192 00193 // C++ [expr.sub]p1: The result is an lvalue of type "T". 00194 // However, subscripting vector types is more like member access. 00195 case Expr::ArraySubscriptExprClass: 00196 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) 00197 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); 00198 return Cl::CL_LValue; 00199 00200 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a 00201 // function or variable and a prvalue otherwise. 00202 case Expr::DeclRefExprClass: 00203 if (E->getType() == Ctx.UnknownAnyTy) 00204 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) 00205 ? Cl::CL_PRValue : Cl::CL_LValue; 00206 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); 00207 00208 // Member access is complex. 00209 case Expr::MemberExprClass: 00210 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); 00211 00212 case Expr::UnaryOperatorClass: 00213 switch (cast<UnaryOperator>(E)->getOpcode()) { 00214 // C++ [expr.unary.op]p1: The unary * operator performs indirection: 00215 // [...] the result is an lvalue referring to the object or function 00216 // to which the expression points. 00217 case UO_Deref: 00218 return Cl::CL_LValue; 00219 00220 // GNU extensions, simply look through them. 00221 case UO_Extension: 00222 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); 00223 00224 // Treat _Real and _Imag basically as if they were member 00225 // expressions: l-value only if the operand is a true l-value. 00226 case UO_Real: 00227 case UO_Imag: { 00228 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 00229 Cl::Kinds K = ClassifyInternal(Ctx, Op); 00230 if (K != Cl::CL_LValue) return K; 00231 00232 if (isa<ObjCPropertyRefExpr>(Op)) 00233 return Cl::CL_SubObjCPropertySetting; 00234 return Cl::CL_LValue; 00235 } 00236 00237 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an 00238 // lvalue, [...] 00239 // Not so in C. 00240 case UO_PreInc: 00241 case UO_PreDec: 00242 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; 00243 00244 default: 00245 return Cl::CL_PRValue; 00246 } 00247 00248 case Expr::OpaqueValueExprClass: 00249 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 00250 00251 // Pseudo-object expressions can produce l-values with reference magic. 00252 case Expr::PseudoObjectExprClass: 00253 return ClassifyExprValueKind(Lang, E, 00254 cast<PseudoObjectExpr>(E)->getValueKind()); 00255 00256 // Implicit casts are lvalues if they're lvalue casts. Other than that, we 00257 // only specifically record class temporaries. 00258 case Expr::ImplicitCastExprClass: 00259 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 00260 00261 // C++ [expr.prim.general]p4: The presence of parentheses does not affect 00262 // whether the expression is an lvalue. 00263 case Expr::ParenExprClass: 00264 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); 00265 00266 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, 00267 // or a void expression if its result expression is, respectively, an 00268 // lvalue, a function designator, or a void expression. 00269 case Expr::GenericSelectionExprClass: 00270 if (cast<GenericSelectionExpr>(E)->isResultDependent()) 00271 return Cl::CL_PRValue; 00272 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); 00273 00274 case Expr::BinaryOperatorClass: 00275 case Expr::CompoundAssignOperatorClass: 00276 // C doesn't have any binary expressions that are lvalues. 00277 if (Lang.CPlusPlus) 00278 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); 00279 return Cl::CL_PRValue; 00280 00281 case Expr::CallExprClass: 00282 case Expr::CXXOperatorCallExprClass: 00283 case Expr::CXXMemberCallExprClass: 00284 case Expr::UserDefinedLiteralClass: 00285 case Expr::CUDAKernelCallExprClass: 00286 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType()); 00287 00288 // __builtin_choose_expr is equivalent to the chosen expression. 00289 case Expr::ChooseExprClass: 00290 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr()); 00291 00292 // Extended vector element access is an lvalue unless there are duplicates 00293 // in the shuffle expression. 00294 case Expr::ExtVectorElementExprClass: 00295 if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements()) 00296 return Cl::CL_DuplicateVectorComponents; 00297 if (cast<ExtVectorElementExpr>(E)->isArrow()) 00298 return Cl::CL_LValue; 00299 return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase()); 00300 00301 // Simply look at the actual default argument. 00302 case Expr::CXXDefaultArgExprClass: 00303 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); 00304 00305 // Same idea for default initializers. 00306 case Expr::CXXDefaultInitExprClass: 00307 return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); 00308 00309 // Same idea for temporary binding. 00310 case Expr::CXXBindTemporaryExprClass: 00311 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 00312 00313 // And the cleanups guard. 00314 case Expr::ExprWithCleanupsClass: 00315 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); 00316 00317 // Casts depend completely on the target type. All casts work the same. 00318 case Expr::CStyleCastExprClass: 00319 case Expr::CXXFunctionalCastExprClass: 00320 case Expr::CXXStaticCastExprClass: 00321 case Expr::CXXDynamicCastExprClass: 00322 case Expr::CXXReinterpretCastExprClass: 00323 case Expr::CXXConstCastExprClass: 00324 case Expr::ObjCBridgedCastExprClass: 00325 // Only in C++ can casts be interesting at all. 00326 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 00327 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); 00328 00329 case Expr::CXXUnresolvedConstructExprClass: 00330 return ClassifyUnnamed(Ctx, 00331 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); 00332 00333 case Expr::BinaryConditionalOperatorClass: { 00334 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 00335 const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E); 00336 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 00337 } 00338 00339 case Expr::ConditionalOperatorClass: { 00340 // Once again, only C++ is interesting. 00341 if (!Lang.CPlusPlus) return Cl::CL_PRValue; 00342 const ConditionalOperator *co = cast<ConditionalOperator>(E); 00343 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); 00344 } 00345 00346 // ObjC message sends are effectively function calls, if the target function 00347 // is known. 00348 case Expr::ObjCMessageExprClass: 00349 if (const ObjCMethodDecl *Method = 00350 cast<ObjCMessageExpr>(E)->getMethodDecl()) { 00351 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType()); 00352 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; 00353 } 00354 return Cl::CL_PRValue; 00355 00356 // Some C++ expressions are always class temporaries. 00357 case Expr::CXXConstructExprClass: 00358 case Expr::CXXTemporaryObjectExprClass: 00359 case Expr::LambdaExprClass: 00360 case Expr::CXXStdInitializerListExprClass: 00361 return Cl::CL_ClassTemporary; 00362 00363 case Expr::VAArgExprClass: 00364 return ClassifyUnnamed(Ctx, E->getType()); 00365 00366 case Expr::DesignatedInitExprClass: 00367 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); 00368 00369 case Expr::StmtExprClass: { 00370 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); 00371 if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) 00372 return ClassifyUnnamed(Ctx, LastExpr->getType()); 00373 return Cl::CL_PRValue; 00374 } 00375 00376 case Expr::CXXUuidofExprClass: 00377 return Cl::CL_LValue; 00378 00379 case Expr::PackExpansionExprClass: 00380 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); 00381 00382 case Expr::MaterializeTemporaryExprClass: 00383 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() 00384 ? Cl::CL_LValue 00385 : Cl::CL_XValue; 00386 00387 case Expr::InitListExprClass: 00388 // An init list can be an lvalue if it is bound to a reference and 00389 // contains only one element. In that case, we look at that element 00390 // for an exact classification. Init list creation takes care of the 00391 // value kind for us, so we only need to fine-tune. 00392 if (E->isRValue()) 00393 return ClassifyExprValueKind(Lang, E, E->getValueKind()); 00394 assert(cast<InitListExpr>(E)->getNumInits() == 1 && 00395 "Only 1-element init lists can be glvalues."); 00396 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); 00397 } 00398 00399 llvm_unreachable("unhandled expression kind in classification"); 00400 } 00401 00402 /// ClassifyDecl - Return the classification of an expression referencing the 00403 /// given declaration. 00404 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { 00405 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a 00406 // function, variable, or data member and a prvalue otherwise. 00407 // In C, functions are not lvalues. 00408 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an 00409 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to 00410 // special-case this. 00411 00412 if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 00413 return Cl::CL_MemberFunction; 00414 00415 bool islvalue; 00416 if (const NonTypeTemplateParmDecl *NTTParm = 00417 dyn_cast<NonTypeTemplateParmDecl>(D)) 00418 islvalue = NTTParm->getType()->isReferenceType(); 00419 else 00420 islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || 00421 isa<IndirectFieldDecl>(D) || 00422 (Ctx.getLangOpts().CPlusPlus && 00423 (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D))); 00424 00425 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; 00426 } 00427 00428 /// ClassifyUnnamed - Return the classification of an expression yielding an 00429 /// unnamed value of the given type. This applies in particular to function 00430 /// calls and casts. 00431 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { 00432 // In C, function calls are always rvalues. 00433 if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; 00434 00435 // C++ [expr.call]p10: A function call is an lvalue if the result type is an 00436 // lvalue reference type or an rvalue reference to function type, an xvalue 00437 // if the result type is an rvalue reference to object type, and a prvalue 00438 // otherwise. 00439 if (T->isLValueReferenceType()) 00440 return Cl::CL_LValue; 00441 const RValueReferenceType *RV = T->getAs<RValueReferenceType>(); 00442 if (!RV) // Could still be a class temporary, though. 00443 return ClassifyTemporary(T); 00444 00445 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; 00446 } 00447 00448 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { 00449 if (E->getType() == Ctx.UnknownAnyTy) 00450 return (isa<FunctionDecl>(E->getMemberDecl()) 00451 ? Cl::CL_PRValue : Cl::CL_LValue); 00452 00453 // Handle C first, it's easier. 00454 if (!Ctx.getLangOpts().CPlusPlus) { 00455 // C99 6.5.2.3p3 00456 // For dot access, the expression is an lvalue if the first part is. For 00457 // arrow access, it always is an lvalue. 00458 if (E->isArrow()) 00459 return Cl::CL_LValue; 00460 // ObjC property accesses are not lvalues, but get special treatment. 00461 Expr *Base = E->getBase()->IgnoreParens(); 00462 if (isa<ObjCPropertyRefExpr>(Base)) 00463 return Cl::CL_SubObjCPropertySetting; 00464 return ClassifyInternal(Ctx, Base); 00465 } 00466 00467 NamedDecl *Member = E->getMemberDecl(); 00468 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. 00469 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then 00470 // E1.E2 is an lvalue. 00471 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 00472 if (Value->getType()->isReferenceType()) 00473 return Cl::CL_LValue; 00474 00475 // Otherwise, one of the following rules applies. 00476 // -- If E2 is a static member [...] then E1.E2 is an lvalue. 00477 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) 00478 return Cl::CL_LValue; 00479 00480 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then 00481 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; 00482 // otherwise, it is a prvalue. 00483 if (isa<FieldDecl>(Member)) { 00484 // *E1 is an lvalue 00485 if (E->isArrow()) 00486 return Cl::CL_LValue; 00487 Expr *Base = E->getBase()->IgnoreParenImpCasts(); 00488 if (isa<ObjCPropertyRefExpr>(Base)) 00489 return Cl::CL_SubObjCPropertySetting; 00490 return ClassifyInternal(Ctx, E->getBase()); 00491 } 00492 00493 // -- If E2 is a [...] member function, [...] 00494 // -- If it refers to a static member function [...], then E1.E2 is an 00495 // lvalue; [...] 00496 // -- Otherwise [...] E1.E2 is a prvalue. 00497 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 00498 return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; 00499 00500 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. 00501 // So is everything else we haven't handled yet. 00502 return Cl::CL_PRValue; 00503 } 00504 00505 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { 00506 assert(Ctx.getLangOpts().CPlusPlus && 00507 "This is only relevant for C++."); 00508 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. 00509 // Except we override this for writes to ObjC properties. 00510 if (E->isAssignmentOp()) 00511 return (E->getLHS()->getObjectKind() == OK_ObjCProperty 00512 ? Cl::CL_PRValue : Cl::CL_LValue); 00513 00514 // C++ [expr.comma]p1: the result is of the same value category as its right 00515 // operand, [...]. 00516 if (E->getOpcode() == BO_Comma) 00517 return ClassifyInternal(Ctx, E->getRHS()); 00518 00519 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand 00520 // is a pointer to a data member is of the same value category as its first 00521 // operand. 00522 if (E->getOpcode() == BO_PtrMemD) 00523 return (E->getType()->isFunctionType() || 00524 E->hasPlaceholderType(BuiltinType::BoundMember)) 00525 ? Cl::CL_MemberFunction 00526 : ClassifyInternal(Ctx, E->getLHS()); 00527 00528 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its 00529 // second operand is a pointer to data member and a prvalue otherwise. 00530 if (E->getOpcode() == BO_PtrMemI) 00531 return (E->getType()->isFunctionType() || 00532 E->hasPlaceholderType(BuiltinType::BoundMember)) 00533 ? Cl::CL_MemberFunction 00534 : Cl::CL_LValue; 00535 00536 // All other binary operations are prvalues. 00537 return Cl::CL_PRValue; 00538 } 00539 00540 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, 00541 const Expr *False) { 00542 assert(Ctx.getLangOpts().CPlusPlus && 00543 "This is only relevant for C++."); 00544 00545 // C++ [expr.cond]p2 00546 // If either the second or the third operand has type (cv) void, 00547 // one of the following shall hold: 00548 if (True->getType()->isVoidType() || False->getType()->isVoidType()) { 00549 // The second or the third operand (but not both) is a (possibly 00550 // parenthesized) throw-expression; the result is of the [...] value 00551 // category of the other. 00552 bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts()); 00553 bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts()); 00554 if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False) 00555 : (FalseIsThrow ? True : nullptr)) 00556 return ClassifyInternal(Ctx, NonThrow); 00557 00558 // [Otherwise] the result [...] is a prvalue. 00559 return Cl::CL_PRValue; 00560 } 00561 00562 // Note that at this point, we have already performed all conversions 00563 // according to [expr.cond]p3. 00564 // C++ [expr.cond]p4: If the second and third operands are glvalues of the 00565 // same value category [...], the result is of that [...] value category. 00566 // C++ [expr.cond]p5: Otherwise, the result is a prvalue. 00567 Cl::Kinds LCl = ClassifyInternal(Ctx, True), 00568 RCl = ClassifyInternal(Ctx, False); 00569 return LCl == RCl ? LCl : Cl::CL_PRValue; 00570 } 00571 00572 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, 00573 Cl::Kinds Kind, SourceLocation &Loc) { 00574 // As a general rule, we only care about lvalues. But there are some rvalues 00575 // for which we want to generate special results. 00576 if (Kind == Cl::CL_PRValue) { 00577 // For the sake of better diagnostics, we want to specifically recognize 00578 // use of the GCC cast-as-lvalue extension. 00579 if (const ExplicitCastExpr *CE = 00580 dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) { 00581 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { 00582 Loc = CE->getExprLoc(); 00583 return Cl::CM_LValueCast; 00584 } 00585 } 00586 } 00587 if (Kind != Cl::CL_LValue) 00588 return Cl::CM_RValue; 00589 00590 // This is the lvalue case. 00591 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) 00592 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) 00593 return Cl::CM_Function; 00594 00595 // Assignment to a property in ObjC is an implicit setter access. But a 00596 // setter might not exist. 00597 if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) { 00598 if (Expr->isImplicitProperty() && 00599 Expr->getImplicitPropertySetter() == nullptr) 00600 return Cl::CM_NoSetterProperty; 00601 } 00602 00603 CanQualType CT = Ctx.getCanonicalType(E->getType()); 00604 // Const stuff is obviously not modifiable. 00605 if (CT.isConstQualified()) 00606 return Cl::CM_ConstQualified; 00607 if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) 00608 return Cl::CM_ConstQualified; 00609 00610 // Arrays are not modifiable, only their elements are. 00611 if (CT->isArrayType()) 00612 return Cl::CM_ArrayType; 00613 // Incomplete types are not modifiable. 00614 if (CT->isIncompleteType()) 00615 return Cl::CM_IncompleteType; 00616 00617 // Records with any const fields (recursively) are not modifiable. 00618 if (const RecordType *R = CT->getAs<RecordType>()) { 00619 assert((E->getObjectKind() == OK_ObjCProperty || 00620 !Ctx.getLangOpts().CPlusPlus) && 00621 "C++ struct assignment should be resolved by the " 00622 "copy assignment operator."); 00623 if (R->hasConstFields()) 00624 return Cl::CM_ConstQualified; 00625 } 00626 00627 return Cl::CM_Modifiable; 00628 } 00629 00630 Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { 00631 Classification VC = Classify(Ctx); 00632 switch (VC.getKind()) { 00633 case Cl::CL_LValue: return LV_Valid; 00634 case Cl::CL_XValue: return LV_InvalidExpression; 00635 case Cl::CL_Function: return LV_NotObjectType; 00636 case Cl::CL_Void: return LV_InvalidExpression; 00637 case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; 00638 case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; 00639 case Cl::CL_MemberFunction: return LV_MemberFunction; 00640 case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; 00641 case Cl::CL_ClassTemporary: return LV_ClassTemporary; 00642 case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; 00643 case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; 00644 case Cl::CL_PRValue: return LV_InvalidExpression; 00645 } 00646 llvm_unreachable("Unhandled kind"); 00647 } 00648 00649 Expr::isModifiableLvalueResult 00650 Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { 00651 SourceLocation dummy; 00652 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); 00653 switch (VC.getKind()) { 00654 case Cl::CL_LValue: break; 00655 case Cl::CL_XValue: return MLV_InvalidExpression; 00656 case Cl::CL_Function: return MLV_NotObjectType; 00657 case Cl::CL_Void: return MLV_InvalidExpression; 00658 case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; 00659 case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 00660 case Cl::CL_MemberFunction: return MLV_MemberFunction; 00661 case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; 00662 case Cl::CL_ClassTemporary: return MLV_ClassTemporary; 00663 case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; 00664 case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; 00665 case Cl::CL_PRValue: 00666 return VC.getModifiable() == Cl::CM_LValueCast ? 00667 MLV_LValueCast : MLV_InvalidExpression; 00668 } 00669 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); 00670 switch (VC.getModifiable()) { 00671 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); 00672 case Cl::CM_Modifiable: return MLV_Valid; 00673 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); 00674 case Cl::CM_Function: return MLV_NotObjectType; 00675 case Cl::CM_LValueCast: 00676 llvm_unreachable("CM_LValueCast and CL_LValue don't match"); 00677 case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; 00678 case Cl::CM_ConstQualified: return MLV_ConstQualified; 00679 case Cl::CM_ArrayType: return MLV_ArrayType; 00680 case Cl::CM_IncompleteType: return MLV_IncompleteType; 00681 } 00682 llvm_unreachable("Unhandled modifiable type"); 00683 }