ExprClassification.cpp 29 KB

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