ExprClassification.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. case Expr::ConceptSpecializationExprClass:
  180. return Cl::CL_PRValue;
  181. case Expr::ConstantExprClass:
  182. return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
  183. // Next come the complicated cases.
  184. case Expr::SubstNonTypeTemplateParmExprClass:
  185. return ClassifyInternal(Ctx,
  186. cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
  187. // C, C++98 [expr.sub]p1: The result is an lvalue of type "T".
  188. // C++11 (DR1213): in the case of an array operand, the result is an lvalue
  189. // if that operand is an lvalue and an xvalue otherwise.
  190. // Subscripting vector types is more like member access.
  191. case Expr::ArraySubscriptExprClass:
  192. if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
  193. return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
  194. if (Lang.CPlusPlus11) {
  195. // Step over the array-to-pointer decay if present, but not over the
  196. // temporary materialization.
  197. auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
  198. if (Base->getType()->isArrayType())
  199. return ClassifyInternal(Ctx, Base);
  200. }
  201. return Cl::CL_LValue;
  202. // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
  203. // function or variable and a prvalue otherwise.
  204. case Expr::DeclRefExprClass:
  205. if (E->getType() == Ctx.UnknownAnyTy)
  206. return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
  207. ? Cl::CL_PRValue : Cl::CL_LValue;
  208. return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
  209. // Member access is complex.
  210. case Expr::MemberExprClass:
  211. return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
  212. case Expr::UnaryOperatorClass:
  213. switch (cast<UnaryOperator>(E)->getOpcode()) {
  214. // C++ [expr.unary.op]p1: The unary * operator performs indirection:
  215. // [...] the result is an lvalue referring to the object or function
  216. // to which the expression points.
  217. case UO_Deref:
  218. return Cl::CL_LValue;
  219. // GNU extensions, simply look through them.
  220. case UO_Extension:
  221. return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
  222. // Treat _Real and _Imag basically as if they were member
  223. // expressions: l-value only if the operand is a true l-value.
  224. case UO_Real:
  225. case UO_Imag: {
  226. const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
  227. Cl::Kinds K = ClassifyInternal(Ctx, Op);
  228. if (K != Cl::CL_LValue) return K;
  229. if (isa<ObjCPropertyRefExpr>(Op))
  230. return Cl::CL_SubObjCPropertySetting;
  231. return Cl::CL_LValue;
  232. }
  233. // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
  234. // lvalue, [...]
  235. // Not so in C.
  236. case UO_PreInc:
  237. case UO_PreDec:
  238. return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
  239. default:
  240. return Cl::CL_PRValue;
  241. }
  242. case Expr::OpaqueValueExprClass:
  243. return ClassifyExprValueKind(Lang, E, E->getValueKind());
  244. // Pseudo-object expressions can produce l-values with reference magic.
  245. case Expr::PseudoObjectExprClass:
  246. return ClassifyExprValueKind(Lang, E,
  247. cast<PseudoObjectExpr>(E)->getValueKind());
  248. // Implicit casts are lvalues if they're lvalue casts. Other than that, we
  249. // only specifically record class temporaries.
  250. case Expr::ImplicitCastExprClass:
  251. return ClassifyExprValueKind(Lang, E, E->getValueKind());
  252. // C++ [expr.prim.general]p4: The presence of parentheses does not affect
  253. // whether the expression is an lvalue.
  254. case Expr::ParenExprClass:
  255. return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
  256. // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
  257. // or a void expression if its result expression is, respectively, an
  258. // lvalue, a function designator, or a void expression.
  259. case Expr::GenericSelectionExprClass:
  260. if (cast<GenericSelectionExpr>(E)->isResultDependent())
  261. return Cl::CL_PRValue;
  262. return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
  263. case Expr::BinaryOperatorClass:
  264. case Expr::CompoundAssignOperatorClass:
  265. // C doesn't have any binary expressions that are lvalues.
  266. if (Lang.CPlusPlus)
  267. return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
  268. return Cl::CL_PRValue;
  269. case Expr::CallExprClass:
  270. case Expr::CXXOperatorCallExprClass:
  271. case Expr::CXXMemberCallExprClass:
  272. case Expr::UserDefinedLiteralClass:
  273. case Expr::CUDAKernelCallExprClass:
  274. return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
  275. // __builtin_choose_expr is equivalent to the chosen expression.
  276. case Expr::ChooseExprClass:
  277. return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
  278. // Extended vector element access is an lvalue unless there are duplicates
  279. // in the shuffle expression.
  280. case Expr::ExtVectorElementExprClass:
  281. if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
  282. return Cl::CL_DuplicateVectorComponents;
  283. if (cast<ExtVectorElementExpr>(E)->isArrow())
  284. return Cl::CL_LValue;
  285. return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
  286. // Simply look at the actual default argument.
  287. case Expr::CXXDefaultArgExprClass:
  288. return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
  289. // Same idea for default initializers.
  290. case Expr::CXXDefaultInitExprClass:
  291. return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
  292. // Same idea for temporary binding.
  293. case Expr::CXXBindTemporaryExprClass:
  294. return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
  295. // And the cleanups guard.
  296. case Expr::ExprWithCleanupsClass:
  297. return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
  298. // Casts depend completely on the target type. All casts work the same.
  299. case Expr::CStyleCastExprClass:
  300. case Expr::CXXFunctionalCastExprClass:
  301. case Expr::CXXStaticCastExprClass:
  302. case Expr::CXXDynamicCastExprClass:
  303. case Expr::CXXReinterpretCastExprClass:
  304. case Expr::CXXConstCastExprClass:
  305. case Expr::ObjCBridgedCastExprClass:
  306. case Expr::BuiltinBitCastExprClass:
  307. // Only in C++ can casts be interesting at all.
  308. if (!Lang.CPlusPlus) return Cl::CL_PRValue;
  309. return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
  310. case Expr::CXXUnresolvedConstructExprClass:
  311. return ClassifyUnnamed(Ctx,
  312. cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
  313. case Expr::BinaryConditionalOperatorClass: {
  314. if (!Lang.CPlusPlus) return Cl::CL_PRValue;
  315. const auto *co = cast<BinaryConditionalOperator>(E);
  316. return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
  317. }
  318. case Expr::ConditionalOperatorClass: {
  319. // Once again, only C++ is interesting.
  320. if (!Lang.CPlusPlus) return Cl::CL_PRValue;
  321. const auto *co = cast<ConditionalOperator>(E);
  322. return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
  323. }
  324. // ObjC message sends are effectively function calls, if the target function
  325. // is known.
  326. case Expr::ObjCMessageExprClass:
  327. if (const ObjCMethodDecl *Method =
  328. cast<ObjCMessageExpr>(E)->getMethodDecl()) {
  329. Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
  330. return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
  331. }
  332. return Cl::CL_PRValue;
  333. // Some C++ expressions are always class temporaries.
  334. case Expr::CXXConstructExprClass:
  335. case Expr::CXXInheritedCtorInitExprClass:
  336. case Expr::CXXTemporaryObjectExprClass:
  337. case Expr::LambdaExprClass:
  338. case Expr::CXXStdInitializerListExprClass:
  339. return Cl::CL_ClassTemporary;
  340. case Expr::VAArgExprClass:
  341. return ClassifyUnnamed(Ctx, E->getType());
  342. case Expr::DesignatedInitExprClass:
  343. return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
  344. case Expr::StmtExprClass: {
  345. const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
  346. if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
  347. return ClassifyUnnamed(Ctx, LastExpr->getType());
  348. return Cl::CL_PRValue;
  349. }
  350. case Expr::CXXUuidofExprClass:
  351. return Cl::CL_LValue;
  352. case Expr::PackExpansionExprClass:
  353. return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
  354. case Expr::MaterializeTemporaryExprClass:
  355. return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
  356. ? Cl::CL_LValue
  357. : Cl::CL_XValue;
  358. case Expr::InitListExprClass:
  359. // An init list can be an lvalue if it is bound to a reference and
  360. // contains only one element. In that case, we look at that element
  361. // for an exact classification. Init list creation takes care of the
  362. // value kind for us, so we only need to fine-tune.
  363. if (E->isRValue())
  364. return ClassifyExprValueKind(Lang, E, E->getValueKind());
  365. assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
  366. "Only 1-element init lists can be glvalues.");
  367. return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
  368. case Expr::CoawaitExprClass:
  369. case Expr::CoyieldExprClass:
  370. return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
  371. }
  372. llvm_unreachable("unhandled expression kind in classification");
  373. }
  374. /// ClassifyDecl - Return the classification of an expression referencing the
  375. /// given declaration.
  376. static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
  377. // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
  378. // function, variable, or data member and a prvalue otherwise.
  379. // In C, functions are not lvalues.
  380. // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
  381. // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
  382. // special-case this.
  383. if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
  384. return Cl::CL_MemberFunction;
  385. bool islvalue;
  386. if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
  387. islvalue = NTTParm->getType()->isReferenceType();
  388. else
  389. islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
  390. isa<IndirectFieldDecl>(D) ||
  391. isa<BindingDecl>(D) ||
  392. (Ctx.getLangOpts().CPlusPlus &&
  393. (isa<FunctionDecl>(D) || isa<MSPropertyDecl>(D) ||
  394. isa<FunctionTemplateDecl>(D)));
  395. return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
  396. }
  397. /// ClassifyUnnamed - Return the classification of an expression yielding an
  398. /// unnamed value of the given type. This applies in particular to function
  399. /// calls and casts.
  400. static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
  401. // In C, function calls are always rvalues.
  402. if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
  403. // C++ [expr.call]p10: A function call is an lvalue if the result type is an
  404. // lvalue reference type or an rvalue reference to function type, an xvalue
  405. // if the result type is an rvalue reference to object type, and a prvalue
  406. // otherwise.
  407. if (T->isLValueReferenceType())
  408. return Cl::CL_LValue;
  409. const auto *RV = T->getAs<RValueReferenceType>();
  410. if (!RV) // Could still be a class temporary, though.
  411. return ClassifyTemporary(T);
  412. return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
  413. }
  414. static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
  415. if (E->getType() == Ctx.UnknownAnyTy)
  416. return (isa<FunctionDecl>(E->getMemberDecl())
  417. ? Cl::CL_PRValue : Cl::CL_LValue);
  418. // Handle C first, it's easier.
  419. if (!Ctx.getLangOpts().CPlusPlus) {
  420. // C99 6.5.2.3p3
  421. // For dot access, the expression is an lvalue if the first part is. For
  422. // arrow access, it always is an lvalue.
  423. if (E->isArrow())
  424. return Cl::CL_LValue;
  425. // ObjC property accesses are not lvalues, but get special treatment.
  426. Expr *Base = E->getBase()->IgnoreParens();
  427. if (isa<ObjCPropertyRefExpr>(Base))
  428. return Cl::CL_SubObjCPropertySetting;
  429. return ClassifyInternal(Ctx, Base);
  430. }
  431. NamedDecl *Member = E->getMemberDecl();
  432. // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
  433. // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
  434. // E1.E2 is an lvalue.
  435. if (const auto *Value = dyn_cast<ValueDecl>(Member))
  436. if (Value->getType()->isReferenceType())
  437. return Cl::CL_LValue;
  438. // Otherwise, one of the following rules applies.
  439. // -- If E2 is a static member [...] then E1.E2 is an lvalue.
  440. if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
  441. return Cl::CL_LValue;
  442. // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
  443. // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
  444. // otherwise, it is a prvalue.
  445. if (isa<FieldDecl>(Member)) {
  446. // *E1 is an lvalue
  447. if (E->isArrow())
  448. return Cl::CL_LValue;
  449. Expr *Base = E->getBase()->IgnoreParenImpCasts();
  450. if (isa<ObjCPropertyRefExpr>(Base))
  451. return Cl::CL_SubObjCPropertySetting;
  452. return ClassifyInternal(Ctx, E->getBase());
  453. }
  454. // -- If E2 is a [...] member function, [...]
  455. // -- If it refers to a static member function [...], then E1.E2 is an
  456. // lvalue; [...]
  457. // -- Otherwise [...] E1.E2 is a prvalue.
  458. if (const auto *Method = dyn_cast<CXXMethodDecl>(Member))
  459. return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
  460. // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
  461. // So is everything else we haven't handled yet.
  462. return Cl::CL_PRValue;
  463. }
  464. static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
  465. assert(Ctx.getLangOpts().CPlusPlus &&
  466. "This is only relevant for C++.");
  467. // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
  468. // Except we override this for writes to ObjC properties.
  469. if (E->isAssignmentOp())
  470. return (E->getLHS()->getObjectKind() == OK_ObjCProperty
  471. ? Cl::CL_PRValue : Cl::CL_LValue);
  472. // C++ [expr.comma]p1: the result is of the same value category as its right
  473. // operand, [...].
  474. if (E->getOpcode() == BO_Comma)
  475. return ClassifyInternal(Ctx, E->getRHS());
  476. // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
  477. // is a pointer to a data member is of the same value category as its first
  478. // operand.
  479. if (E->getOpcode() == BO_PtrMemD)
  480. return (E->getType()->isFunctionType() ||
  481. E->hasPlaceholderType(BuiltinType::BoundMember))
  482. ? Cl::CL_MemberFunction
  483. : ClassifyInternal(Ctx, E->getLHS());
  484. // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
  485. // second operand is a pointer to data member and a prvalue otherwise.
  486. if (E->getOpcode() == BO_PtrMemI)
  487. return (E->getType()->isFunctionType() ||
  488. E->hasPlaceholderType(BuiltinType::BoundMember))
  489. ? Cl::CL_MemberFunction
  490. : Cl::CL_LValue;
  491. // All other binary operations are prvalues.
  492. return Cl::CL_PRValue;
  493. }
  494. static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
  495. const Expr *False) {
  496. assert(Ctx.getLangOpts().CPlusPlus &&
  497. "This is only relevant for C++.");
  498. // C++ [expr.cond]p2
  499. // If either the second or the third operand has type (cv) void,
  500. // one of the following shall hold:
  501. if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
  502. // The second or the third operand (but not both) is a (possibly
  503. // parenthesized) throw-expression; the result is of the [...] value
  504. // category of the other.
  505. bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
  506. bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
  507. if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
  508. : (FalseIsThrow ? True : nullptr))
  509. return ClassifyInternal(Ctx, NonThrow);
  510. // [Otherwise] the result [...] is a prvalue.
  511. return Cl::CL_PRValue;
  512. }
  513. // Note that at this point, we have already performed all conversions
  514. // according to [expr.cond]p3.
  515. // C++ [expr.cond]p4: If the second and third operands are glvalues of the
  516. // same value category [...], the result is of that [...] value category.
  517. // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
  518. Cl::Kinds LCl = ClassifyInternal(Ctx, True),
  519. RCl = ClassifyInternal(Ctx, False);
  520. return LCl == RCl ? LCl : Cl::CL_PRValue;
  521. }
  522. static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
  523. Cl::Kinds Kind, SourceLocation &Loc) {
  524. // As a general rule, we only care about lvalues. But there are some rvalues
  525. // for which we want to generate special results.
  526. if (Kind == Cl::CL_PRValue) {
  527. // For the sake of better diagnostics, we want to specifically recognize
  528. // use of the GCC cast-as-lvalue extension.
  529. if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
  530. if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
  531. Loc = CE->getExprLoc();
  532. return Cl::CM_LValueCast;
  533. }
  534. }
  535. }
  536. if (Kind != Cl::CL_LValue)
  537. return Cl::CM_RValue;
  538. // This is the lvalue case.
  539. // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
  540. if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
  541. return Cl::CM_Function;
  542. // Assignment to a property in ObjC is an implicit setter access. But a
  543. // setter might not exist.
  544. if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
  545. if (Expr->isImplicitProperty() &&
  546. Expr->getImplicitPropertySetter() == nullptr)
  547. return Cl::CM_NoSetterProperty;
  548. }
  549. CanQualType CT = Ctx.getCanonicalType(E->getType());
  550. // Const stuff is obviously not modifiable.
  551. if (CT.isConstQualified())
  552. return Cl::CM_ConstQualified;
  553. if (Ctx.getLangOpts().OpenCL &&
  554. CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant)
  555. return Cl::CM_ConstAddrSpace;
  556. // Arrays are not modifiable, only their elements are.
  557. if (CT->isArrayType())
  558. return Cl::CM_ArrayType;
  559. // Incomplete types are not modifiable.
  560. if (CT->isIncompleteType())
  561. return Cl::CM_IncompleteType;
  562. // Records with any const fields (recursively) are not modifiable.
  563. if (const RecordType *R = CT->getAs<RecordType>())
  564. if (R->hasConstFields())
  565. return Cl::CM_ConstQualifiedField;
  566. return Cl::CM_Modifiable;
  567. }
  568. Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
  569. Classification VC = Classify(Ctx);
  570. switch (VC.getKind()) {
  571. case Cl::CL_LValue: return LV_Valid;
  572. case Cl::CL_XValue: return LV_InvalidExpression;
  573. case Cl::CL_Function: return LV_NotObjectType;
  574. case Cl::CL_Void: return LV_InvalidExpression;
  575. case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
  576. case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
  577. case Cl::CL_MemberFunction: return LV_MemberFunction;
  578. case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
  579. case Cl::CL_ClassTemporary: return LV_ClassTemporary;
  580. case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
  581. case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
  582. case Cl::CL_PRValue: return LV_InvalidExpression;
  583. }
  584. llvm_unreachable("Unhandled kind");
  585. }
  586. Expr::isModifiableLvalueResult
  587. Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
  588. SourceLocation dummy;
  589. Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
  590. switch (VC.getKind()) {
  591. case Cl::CL_LValue: break;
  592. case Cl::CL_XValue: return MLV_InvalidExpression;
  593. case Cl::CL_Function: return MLV_NotObjectType;
  594. case Cl::CL_Void: return MLV_InvalidExpression;
  595. case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
  596. case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
  597. case Cl::CL_MemberFunction: return MLV_MemberFunction;
  598. case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
  599. case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
  600. case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;
  601. case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
  602. case Cl::CL_PRValue:
  603. return VC.getModifiable() == Cl::CM_LValueCast ?
  604. MLV_LValueCast : MLV_InvalidExpression;
  605. }
  606. assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
  607. switch (VC.getModifiable()) {
  608. case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
  609. case Cl::CM_Modifiable: return MLV_Valid;
  610. case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
  611. case Cl::CM_Function: return MLV_NotObjectType;
  612. case Cl::CM_LValueCast:
  613. llvm_unreachable("CM_LValueCast and CL_LValue don't match");
  614. case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
  615. case Cl::CM_ConstQualified: return MLV_ConstQualified;
  616. case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField;
  617. case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace;
  618. case Cl::CM_ArrayType: return MLV_ArrayType;
  619. case Cl::CM_IncompleteType: return MLV_IncompleteType;
  620. }
  621. llvm_unreachable("Unhandled modifiable type");
  622. }