ExprClassification.cpp 29 KB

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