ExprClassification.cpp 29 KB

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