ExprClassification.cpp 28 KB

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