ExprClassification.cpp 28 KB

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