ComputeDependence.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. //===- ComputeDependence.cpp ----------------------------------------------===//
  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. #include "clang/AST/ComputeDependence.h"
  9. #include "clang/AST/Attr.h"
  10. #include "clang/AST/DeclCXX.h"
  11. #include "clang/AST/DeclarationName.h"
  12. #include "clang/AST/DependenceFlags.h"
  13. #include "clang/AST/Expr.h"
  14. #include "clang/AST/ExprCXX.h"
  15. #include "clang/AST/ExprConcepts.h"
  16. #include "clang/AST/ExprObjC.h"
  17. #include "clang/AST/ExprOpenMP.h"
  18. #include "clang/Basic/ExceptionSpecificationType.h"
  19. #include "llvm/ADT/ArrayRef.h"
  20. using namespace clang;
  21. ExprDependence clang::computeDependence(FullExpr *E) {
  22. return E->getSubExpr()->getDependence();
  23. }
  24. ExprDependence clang::computeDependence(OpaqueValueExpr *E) {
  25. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  26. if (auto *S = E->getSourceExpr())
  27. D |= S->getDependence();
  28. assert(!(D & ExprDependence::UnexpandedPack));
  29. return D;
  30. }
  31. ExprDependence clang::computeDependence(ParenExpr *E) {
  32. return E->getSubExpr()->getDependence();
  33. }
  34. ExprDependence clang::computeDependence(UnaryOperator *E,
  35. const ASTContext &Ctx) {
  36. ExprDependence Dep =
  37. // FIXME: Do we need to look at the type?
  38. toExprDependenceForImpliedType(E->getType()->getDependence()) |
  39. E->getSubExpr()->getDependence();
  40. // C++ [temp.dep.constexpr]p5:
  41. // An expression of the form & qualified-id where the qualified-id names a
  42. // dependent member of the current instantiation is value-dependent. An
  43. // expression of the form & cast-expression is also value-dependent if
  44. // evaluating cast-expression as a core constant expression succeeds and
  45. // the result of the evaluation refers to a templated entity that is an
  46. // object with static or thread storage duration or a member function.
  47. //
  48. // What this amounts to is: constant-evaluate the operand and check whether it
  49. // refers to a templated entity other than a variable with local storage.
  50. if (Ctx.getLangOpts().CPlusPlus && E->getOpcode() == UO_AddrOf &&
  51. !(Dep & ExprDependence::Value)) {
  52. Expr::EvalResult Result;
  53. SmallVector<PartialDiagnosticAt, 8> Diag;
  54. Result.Diag = &Diag;
  55. // FIXME: This doesn't enforce the C++98 constant expression rules.
  56. if (E->getSubExpr()->EvaluateAsConstantExpr(Result, Ctx) && Diag.empty() &&
  57. Result.Val.isLValue()) {
  58. auto *VD = Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
  59. if (VD && VD->isTemplated()) {
  60. auto *VarD = dyn_cast<VarDecl>(VD);
  61. if (!VarD || !VarD->hasLocalStorage())
  62. Dep |= ExprDependence::Value;
  63. }
  64. }
  65. }
  66. return Dep;
  67. }
  68. ExprDependence clang::computeDependence(UnaryExprOrTypeTraitExpr *E) {
  69. // Never type-dependent (C++ [temp.dep.expr]p3).
  70. // Value-dependent if the argument is type-dependent.
  71. if (E->isArgumentType())
  72. return turnTypeToValueDependence(
  73. toExprDependenceAsWritten(E->getArgumentType()->getDependence()));
  74. auto ArgDeps = E->getArgumentExpr()->getDependence();
  75. auto Deps = ArgDeps & ~ExprDependence::TypeValue;
  76. // Value-dependent if the argument is type-dependent.
  77. if (ArgDeps & ExprDependence::Type)
  78. Deps |= ExprDependence::Value;
  79. // Check to see if we are in the situation where alignof(decl) should be
  80. // dependent because decl's alignment is dependent.
  81. auto ExprKind = E->getKind();
  82. if (ExprKind != UETT_AlignOf && ExprKind != UETT_PreferredAlignOf)
  83. return Deps;
  84. if ((Deps & ExprDependence::Value) && (Deps & ExprDependence::Instantiation))
  85. return Deps;
  86. auto *NoParens = E->getArgumentExpr()->IgnoreParens();
  87. const ValueDecl *D = nullptr;
  88. if (const auto *DRE = dyn_cast<DeclRefExpr>(NoParens))
  89. D = DRE->getDecl();
  90. else if (const auto *ME = dyn_cast<MemberExpr>(NoParens))
  91. D = ME->getMemberDecl();
  92. if (!D)
  93. return Deps;
  94. for (const auto *I : D->specific_attrs<AlignedAttr>()) {
  95. if (I->isAlignmentErrorDependent())
  96. Deps |= ExprDependence::Error;
  97. if (I->isAlignmentDependent())
  98. Deps |= ExprDependence::ValueInstantiation;
  99. }
  100. return Deps;
  101. }
  102. ExprDependence clang::computeDependence(ArraySubscriptExpr *E) {
  103. return E->getLHS()->getDependence() | E->getRHS()->getDependence();
  104. }
  105. ExprDependence clang::computeDependence(MatrixSubscriptExpr *E) {
  106. return E->getBase()->getDependence() | E->getRowIdx()->getDependence() |
  107. (E->getColumnIdx() ? E->getColumnIdx()->getDependence()
  108. : ExprDependence::None);
  109. }
  110. ExprDependence clang::computeDependence(CompoundLiteralExpr *E) {
  111. return toExprDependenceAsWritten(
  112. E->getTypeSourceInfo()->getType()->getDependence()) |
  113. toExprDependenceForImpliedType(E->getType()->getDependence()) |
  114. turnTypeToValueDependence(E->getInitializer()->getDependence());
  115. }
  116. ExprDependence clang::computeDependence(ImplicitCastExpr *E) {
  117. // We model implicit conversions as combining the dependence of their
  118. // subexpression, apart from its type, with the semantic portion of the
  119. // target type.
  120. ExprDependence D =
  121. toExprDependenceForImpliedType(E->getType()->getDependence());
  122. if (auto *S = E->getSubExpr())
  123. D |= S->getDependence() & ~ExprDependence::Type;
  124. return D;
  125. }
  126. ExprDependence clang::computeDependence(ExplicitCastExpr *E) {
  127. // Cast expressions are type-dependent if the type is
  128. // dependent (C++ [temp.dep.expr]p3).
  129. // Cast expressions are value-dependent if the type is
  130. // dependent or if the subexpression is value-dependent.
  131. //
  132. // Note that we also need to consider the dependence of the actual type here,
  133. // because when the type as written is a deduced type, that type is not
  134. // dependent, but it may be deduced as a dependent type.
  135. ExprDependence D =
  136. toExprDependenceAsWritten(
  137. cast<ExplicitCastExpr>(E)->getTypeAsWritten()->getDependence()) |
  138. toExprDependenceForImpliedType(E->getType()->getDependence());
  139. if (auto *S = E->getSubExpr())
  140. D |= S->getDependence() & ~ExprDependence::Type;
  141. return D;
  142. }
  143. ExprDependence clang::computeDependence(BinaryOperator *E) {
  144. return E->getLHS()->getDependence() | E->getRHS()->getDependence();
  145. }
  146. ExprDependence clang::computeDependence(ConditionalOperator *E) {
  147. // The type of the conditional operator depends on the type of the conditional
  148. // to support the GCC vector conditional extension. Additionally,
  149. // [temp.dep.expr] does specify state that this should be dependent on ALL sub
  150. // expressions.
  151. return E->getCond()->getDependence() | E->getLHS()->getDependence() |
  152. E->getRHS()->getDependence();
  153. }
  154. ExprDependence clang::computeDependence(BinaryConditionalOperator *E) {
  155. return E->getCommon()->getDependence() | E->getFalseExpr()->getDependence();
  156. }
  157. ExprDependence clang::computeDependence(StmtExpr *E, unsigned TemplateDepth) {
  158. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  159. // Propagate dependence of the result.
  160. if (const auto *CompoundExprResult =
  161. dyn_cast_or_null<ValueStmt>(E->getSubStmt()->getStmtExprResult()))
  162. if (const Expr *ResultExpr = CompoundExprResult->getExprStmt())
  163. D |= ResultExpr->getDependence();
  164. // Note: we treat a statement-expression in a dependent context as always
  165. // being value- and instantiation-dependent. This matches the behavior of
  166. // lambda-expressions and GCC.
  167. if (TemplateDepth)
  168. D |= ExprDependence::ValueInstantiation;
  169. // A param pack cannot be expanded over stmtexpr boundaries.
  170. return D & ~ExprDependence::UnexpandedPack;
  171. }
  172. ExprDependence clang::computeDependence(ConvertVectorExpr *E) {
  173. auto D = toExprDependenceAsWritten(
  174. E->getTypeSourceInfo()->getType()->getDependence()) |
  175. E->getSrcExpr()->getDependence();
  176. if (!E->getType()->isDependentType())
  177. D &= ~ExprDependence::Type;
  178. return D;
  179. }
  180. ExprDependence clang::computeDependence(ChooseExpr *E) {
  181. if (E->isConditionDependent())
  182. return ExprDependence::TypeValueInstantiation |
  183. E->getCond()->getDependence() | E->getLHS()->getDependence() |
  184. E->getRHS()->getDependence();
  185. auto Cond = E->getCond()->getDependence();
  186. auto Active = E->getLHS()->getDependence();
  187. auto Inactive = E->getRHS()->getDependence();
  188. if (!E->isConditionTrue())
  189. std::swap(Active, Inactive);
  190. // Take type- and value- dependency from the active branch. Propagate all
  191. // other flags from all branches.
  192. return (Active & ExprDependence::TypeValue) |
  193. ((Cond | Active | Inactive) & ~ExprDependence::TypeValue);
  194. }
  195. ExprDependence clang::computeDependence(ParenListExpr *P) {
  196. auto D = ExprDependence::None;
  197. for (auto *E : P->exprs())
  198. D |= E->getDependence();
  199. return D;
  200. }
  201. ExprDependence clang::computeDependence(VAArgExpr *E) {
  202. auto D = toExprDependenceAsWritten(
  203. E->getWrittenTypeInfo()->getType()->getDependence()) |
  204. (E->getSubExpr()->getDependence() & ~ExprDependence::Type);
  205. return D & ~ExprDependence::Value;
  206. }
  207. ExprDependence clang::computeDependence(NoInitExpr *E) {
  208. return toExprDependenceForImpliedType(E->getType()->getDependence()) &
  209. (ExprDependence::Instantiation | ExprDependence::Error);
  210. }
  211. ExprDependence clang::computeDependence(ArrayInitLoopExpr *E) {
  212. auto D = E->getCommonExpr()->getDependence() |
  213. E->getSubExpr()->getDependence() | ExprDependence::Instantiation;
  214. if (!E->getType()->isInstantiationDependentType())
  215. D &= ~ExprDependence::Instantiation;
  216. return turnTypeToValueDependence(D);
  217. }
  218. ExprDependence clang::computeDependence(ImplicitValueInitExpr *E) {
  219. return toExprDependenceForImpliedType(E->getType()->getDependence()) &
  220. ExprDependence::Instantiation;
  221. }
  222. ExprDependence clang::computeDependence(ExtVectorElementExpr *E) {
  223. return E->getBase()->getDependence();
  224. }
  225. ExprDependence clang::computeDependence(BlockExpr *E) {
  226. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  227. if (E->getBlockDecl()->isDependentContext())
  228. D |= ExprDependence::Instantiation;
  229. return D;
  230. }
  231. ExprDependence clang::computeDependence(AsTypeExpr *E) {
  232. // FIXME: AsTypeExpr doesn't store the type as written. Assume the expression
  233. // type has identical sugar for now, so is a type-as-written.
  234. auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |
  235. E->getSrcExpr()->getDependence();
  236. if (!E->getType()->isDependentType())
  237. D &= ~ExprDependence::Type;
  238. return D;
  239. }
  240. ExprDependence clang::computeDependence(CXXRewrittenBinaryOperator *E) {
  241. return E->getSemanticForm()->getDependence();
  242. }
  243. ExprDependence clang::computeDependence(CXXStdInitializerListExpr *E) {
  244. auto D = turnTypeToValueDependence(E->getSubExpr()->getDependence());
  245. D |= toExprDependenceForImpliedType(E->getType()->getDependence());
  246. return D;
  247. }
  248. ExprDependence clang::computeDependence(CXXTypeidExpr *E) {
  249. auto D = ExprDependence::None;
  250. if (E->isTypeOperand())
  251. D = toExprDependenceAsWritten(
  252. E->getTypeOperandSourceInfo()->getType()->getDependence());
  253. else
  254. D = turnTypeToValueDependence(E->getExprOperand()->getDependence());
  255. // typeid is never type-dependent (C++ [temp.dep.expr]p4)
  256. return D & ~ExprDependence::Type;
  257. }
  258. ExprDependence clang::computeDependence(MSPropertyRefExpr *E) {
  259. return E->getBaseExpr()->getDependence() & ~ExprDependence::Type;
  260. }
  261. ExprDependence clang::computeDependence(MSPropertySubscriptExpr *E) {
  262. return E->getIdx()->getDependence();
  263. }
  264. ExprDependence clang::computeDependence(CXXUuidofExpr *E) {
  265. if (E->isTypeOperand())
  266. return turnTypeToValueDependence(toExprDependenceAsWritten(
  267. E->getTypeOperandSourceInfo()->getType()->getDependence()));
  268. return turnTypeToValueDependence(E->getExprOperand()->getDependence());
  269. }
  270. ExprDependence clang::computeDependence(CXXThisExpr *E) {
  271. // 'this' is type-dependent if the class type of the enclosing
  272. // member function is dependent (C++ [temp.dep.expr]p2)
  273. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  274. assert(!(D & ExprDependence::UnexpandedPack));
  275. return D;
  276. }
  277. ExprDependence clang::computeDependence(CXXThrowExpr *E) {
  278. auto *Op = E->getSubExpr();
  279. if (!Op)
  280. return ExprDependence::None;
  281. return Op->getDependence() & ~ExprDependence::TypeValue;
  282. }
  283. ExprDependence clang::computeDependence(CXXBindTemporaryExpr *E) {
  284. return E->getSubExpr()->getDependence();
  285. }
  286. ExprDependence clang::computeDependence(CXXScalarValueInitExpr *E) {
  287. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  288. if (auto *TSI = E->getTypeSourceInfo())
  289. D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
  290. return D;
  291. }
  292. ExprDependence clang::computeDependence(CXXDeleteExpr *E) {
  293. return turnTypeToValueDependence(E->getArgument()->getDependence());
  294. }
  295. ExprDependence clang::computeDependence(ArrayTypeTraitExpr *E) {
  296. auto D = toExprDependenceAsWritten(E->getQueriedType()->getDependence());
  297. if (auto *Dim = E->getDimensionExpression())
  298. D |= Dim->getDependence();
  299. return turnTypeToValueDependence(D);
  300. }
  301. ExprDependence clang::computeDependence(ExpressionTraitExpr *E) {
  302. // Never type-dependent.
  303. auto D = E->getQueriedExpression()->getDependence() & ~ExprDependence::Type;
  304. // Value-dependent if the argument is type-dependent.
  305. if (E->getQueriedExpression()->isTypeDependent())
  306. D |= ExprDependence::Value;
  307. return D;
  308. }
  309. ExprDependence clang::computeDependence(CXXNoexceptExpr *E, CanThrowResult CT) {
  310. auto D = E->getOperand()->getDependence() & ~ExprDependence::TypeValue;
  311. if (CT == CT_Dependent)
  312. D |= ExprDependence::ValueInstantiation;
  313. return D;
  314. }
  315. ExprDependence clang::computeDependence(PackExpansionExpr *E) {
  316. return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) |
  317. ExprDependence::TypeValueInstantiation;
  318. }
  319. ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {
  320. return E->getReplacement()->getDependence();
  321. }
  322. ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {
  323. if (auto *Resume = E->getResumeExpr())
  324. return (Resume->getDependence() &
  325. (ExprDependence::TypeValue | ExprDependence::Error)) |
  326. (E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);
  327. return E->getCommonExpr()->getDependence() |
  328. ExprDependence::TypeValueInstantiation;
  329. }
  330. ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {
  331. return E->getOperand()->getDependence() |
  332. ExprDependence::TypeValueInstantiation;
  333. }
  334. ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {
  335. return E->getSubExpr()->getDependence();
  336. }
  337. ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {
  338. return toExprDependenceAsWritten(E->getEncodedType()->getDependence());
  339. }
  340. ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {
  341. return turnTypeToValueDependence(E->getBase()->getDependence());
  342. }
  343. ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {
  344. if (E->isObjectReceiver())
  345. return E->getBase()->getDependence() & ~ExprDependence::Type;
  346. if (E->isSuperReceiver())
  347. return toExprDependenceForImpliedType(
  348. E->getSuperReceiverType()->getDependence()) &
  349. ~ExprDependence::TypeValue;
  350. assert(E->isClassReceiver());
  351. return ExprDependence::None;
  352. }
  353. ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {
  354. return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();
  355. }
  356. ExprDependence clang::computeDependence(ObjCIsaExpr *E) {
  357. return E->getBase()->getDependence() & ~ExprDependence::Type &
  358. ~ExprDependence::UnexpandedPack;
  359. }
  360. ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {
  361. return E->getSubExpr()->getDependence();
  362. }
  363. ExprDependence clang::computeDependence(OMPArraySectionExpr *E) {
  364. auto D = E->getBase()->getDependence();
  365. if (auto *LB = E->getLowerBound())
  366. D |= LB->getDependence();
  367. if (auto *Len = E->getLength())
  368. D |= Len->getDependence();
  369. return D;
  370. }
  371. ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {
  372. auto D = E->getBase()->getDependence();
  373. for (Expr *Dim: E->getDimensions())
  374. if (Dim)
  375. D |= turnValueToTypeDependence(Dim->getDependence());
  376. return D;
  377. }
  378. ExprDependence clang::computeDependence(OMPIteratorExpr *E) {
  379. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  380. for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
  381. if (auto *DD = cast_or_null<DeclaratorDecl>(E->getIteratorDecl(I))) {
  382. // If the type is omitted, it's 'int', and is not dependent in any way.
  383. if (auto *TSI = DD->getTypeSourceInfo()) {
  384. D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
  385. }
  386. }
  387. OMPIteratorExpr::IteratorRange IR = E->getIteratorRange(I);
  388. if (Expr *BE = IR.Begin)
  389. D |= BE->getDependence();
  390. if (Expr *EE = IR.End)
  391. D |= EE->getDependence();
  392. if (Expr *SE = IR.Step)
  393. D |= SE->getDependence();
  394. }
  395. return D;
  396. }
  397. /// Compute the type-, value-, and instantiation-dependence of a
  398. /// declaration reference
  399. /// based on the declaration being referenced.
  400. ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {
  401. auto Deps = ExprDependence::None;
  402. if (auto *NNS = E->getQualifier())
  403. Deps |= toExprDependence(NNS->getDependence() &
  404. ~NestedNameSpecifierDependence::Dependent);
  405. if (auto *FirstArg = E->getTemplateArgs()) {
  406. unsigned NumArgs = E->getNumTemplateArgs();
  407. for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)
  408. Deps |= toExprDependence(Arg->getArgument().getDependence());
  409. }
  410. auto *Decl = E->getDecl();
  411. auto Type = E->getType();
  412. if (Decl->isParameterPack())
  413. Deps |= ExprDependence::UnexpandedPack;
  414. Deps |= toExprDependenceForImpliedType(Type->getDependence()) &
  415. ExprDependence::Error;
  416. // C++ [temp.dep.expr]p3:
  417. // An id-expression is type-dependent if it contains:
  418. // - an identifier associated by name lookup with one or more declarations
  419. // declared with a dependent type
  420. //
  421. // [The "or more" case is not modeled as a DeclRefExpr. There are a bunch
  422. // more bullets here that we handle by treating the declaration as having a
  423. // dependent type if they involve a placeholder type that can't be deduced.]
  424. if (Type->isDependentType())
  425. return Deps | ExprDependence::TypeValueInstantiation;
  426. else if (Type->isInstantiationDependentType())
  427. Deps |= ExprDependence::Instantiation;
  428. // - a conversion-function-id that specifies a dependent type
  429. if (Decl->getDeclName().getNameKind() ==
  430. DeclarationName::CXXConversionFunctionName) {
  431. QualType T = Decl->getDeclName().getCXXNameType();
  432. if (T->isDependentType())
  433. return Deps | ExprDependence::TypeValueInstantiation;
  434. if (T->isInstantiationDependentType())
  435. Deps |= ExprDependence::Instantiation;
  436. }
  437. // - a template-id that is dependent,
  438. // - a nested-name-specifier or a qualified-id that names a member of an
  439. // unknown specialization
  440. // [These are not modeled as DeclRefExprs.]
  441. // or if it names a dependent member of the current instantiation that is a
  442. // static data member of type "array of unknown bound of T" for some T
  443. // [handled below].
  444. // C++ [temp.dep.constexpr]p2:
  445. // An id-expression is value-dependent if:
  446. // - it is type-dependent [handled above]
  447. // - it is the name of a non-type template parameter,
  448. if (isa<NonTypeTemplateParmDecl>(Decl))
  449. return Deps | ExprDependence::ValueInstantiation;
  450. // - it names a potentially-constant variable that is initialized with an
  451. // expression that is value-dependent
  452. if (const auto *Var = dyn_cast<VarDecl>(Decl)) {
  453. if (Var->mightBeUsableInConstantExpressions(Ctx)) {
  454. if (const Expr *Init = Var->getAnyInitializer()) {
  455. if (Init->isValueDependent())
  456. Deps |= ExprDependence::ValueInstantiation;
  457. if (Init->containsErrors())
  458. Deps |= ExprDependence::Error;
  459. }
  460. }
  461. // - it names a static data member that is a dependent member of the
  462. // current instantiation and is not initialized in a member-declarator,
  463. if (Var->isStaticDataMember() &&
  464. Var->getDeclContext()->isDependentContext() &&
  465. !Var->getFirstDecl()->hasInit()) {
  466. const VarDecl *First = Var->getFirstDecl();
  467. TypeSourceInfo *TInfo = First->getTypeSourceInfo();
  468. if (TInfo->getType()->isIncompleteArrayType()) {
  469. Deps |= ExprDependence::TypeValueInstantiation;
  470. } else if (!First->hasInit()) {
  471. Deps |= ExprDependence::ValueInstantiation;
  472. }
  473. }
  474. return Deps;
  475. }
  476. // - it names a static member function that is a dependent member of the
  477. // current instantiation
  478. //
  479. // FIXME: It's unclear that the restriction to static members here has any
  480. // effect: any use of a non-static member function name requires either
  481. // forming a pointer-to-member or providing an object parameter, either of
  482. // which makes the overall expression value-dependent.
  483. if (auto *MD = dyn_cast<CXXMethodDecl>(Decl)) {
  484. if (MD->isStatic() && Decl->getDeclContext()->isDependentContext())
  485. Deps |= ExprDependence::ValueInstantiation;
  486. }
  487. return Deps;
  488. }
  489. ExprDependence clang::computeDependence(RecoveryExpr *E) {
  490. // RecoveryExpr is
  491. // - always value-dependent, and therefore instantiation dependent
  492. // - contains errors (ExprDependence::Error), by definition
  493. // - type-dependent if we don't know the type (fallback to an opaque
  494. // dependent type), or the type is known and dependent, or it has
  495. // type-dependent subexpressions.
  496. auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |
  497. ExprDependence::ErrorDependent;
  498. // FIXME: remove the type-dependent bit from subexpressions, if the
  499. // RecoveryExpr has a non-dependent type.
  500. for (auto *S : E->subExpressions())
  501. D |= S->getDependence();
  502. return D;
  503. }
  504. ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) {
  505. return toExprDependenceAsWritten(
  506. E->getTypeSourceInfo()->getType()->getDependence());
  507. }
  508. ExprDependence clang::computeDependence(PredefinedExpr *E) {
  509. return toExprDependenceForImpliedType(E->getType()->getDependence());
  510. }
  511. ExprDependence clang::computeDependence(CallExpr *E,
  512. llvm::ArrayRef<Expr *> PreArgs) {
  513. auto D = E->getCallee()->getDependence();
  514. for (auto *A : llvm::ArrayRef(E->getArgs(), E->getNumArgs())) {
  515. if (A)
  516. D |= A->getDependence();
  517. }
  518. for (auto *A : PreArgs)
  519. D |= A->getDependence();
  520. return D;
  521. }
  522. ExprDependence clang::computeDependence(OffsetOfExpr *E) {
  523. auto D = turnTypeToValueDependence(toExprDependenceAsWritten(
  524. E->getTypeSourceInfo()->getType()->getDependence()));
  525. for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)
  526. D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence());
  527. return D;
  528. }
  529. ExprDependence clang::computeDependence(MemberExpr *E) {
  530. auto *MemberDecl = E->getMemberDecl();
  531. auto D = E->getBase()->getDependence();
  532. if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {
  533. DeclContext *DC = MemberDecl->getDeclContext();
  534. // dyn_cast_or_null is used to handle objC variables which do not
  535. // have a declaration context.
  536. CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
  537. if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) {
  538. if (!E->getType()->isDependentType())
  539. D &= ~ExprDependence::Type;
  540. }
  541. // Bitfield with value-dependent width is type-dependent.
  542. if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {
  543. D |= ExprDependence::Type;
  544. }
  545. }
  546. // FIXME: move remaining dependence computation from MemberExpr::Create()
  547. return D;
  548. }
  549. ExprDependence clang::computeDependence(InitListExpr *E) {
  550. auto D = ExprDependence::None;
  551. for (auto *A : E->inits())
  552. D |= A->getDependence();
  553. return D;
  554. }
  555. ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {
  556. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  557. for (auto *C : llvm::ArrayRef(E->getSubExprs(), E->getNumSubExprs()))
  558. D |= C->getDependence();
  559. return D;
  560. }
  561. ExprDependence clang::computeDependence(GenericSelectionExpr *E,
  562. bool ContainsUnexpandedPack) {
  563. auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack
  564. : ExprDependence::None;
  565. for (auto *AE : E->getAssocExprs())
  566. D |= AE->getDependence() & ExprDependence::Error;
  567. D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;
  568. if (E->isResultDependent())
  569. return D | ExprDependence::TypeValueInstantiation;
  570. return D | (E->getResultExpr()->getDependence() &
  571. ~ExprDependence::UnexpandedPack);
  572. }
  573. ExprDependence clang::computeDependence(DesignatedInitExpr *E) {
  574. auto Deps = E->getInit()->getDependence();
  575. for (auto D : E->designators()) {
  576. auto DesignatorDeps = ExprDependence::None;
  577. if (D.isArrayDesignator())
  578. DesignatorDeps |= E->getArrayIndex(D)->getDependence();
  579. else if (D.isArrayRangeDesignator())
  580. DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |
  581. E->getArrayRangeEnd(D)->getDependence();
  582. Deps |= DesignatorDeps;
  583. if (DesignatorDeps & ExprDependence::TypeValue)
  584. Deps |= ExprDependence::TypeValueInstantiation;
  585. }
  586. return Deps;
  587. }
  588. ExprDependence clang::computeDependence(PseudoObjectExpr *O) {
  589. auto D = O->getSyntacticForm()->getDependence();
  590. for (auto *E : O->semantics())
  591. D |= E->getDependence();
  592. return D;
  593. }
  594. ExprDependence clang::computeDependence(AtomicExpr *A) {
  595. auto D = ExprDependence::None;
  596. for (auto *E : llvm::ArrayRef(A->getSubExprs(), A->getNumSubExprs()))
  597. D |= E->getDependence();
  598. return D;
  599. }
  600. ExprDependence clang::computeDependence(CXXNewExpr *E) {
  601. auto D = toExprDependenceAsWritten(
  602. E->getAllocatedTypeSourceInfo()->getType()->getDependence());
  603. D |= toExprDependenceForImpliedType(E->getAllocatedType()->getDependence());
  604. auto Size = E->getArraySize();
  605. if (Size && *Size)
  606. D |= turnTypeToValueDependence((*Size)->getDependence());
  607. if (auto *I = E->getInitializer())
  608. D |= turnTypeToValueDependence(I->getDependence());
  609. for (auto *A : E->placement_arguments())
  610. D |= turnTypeToValueDependence(A->getDependence());
  611. return D;
  612. }
  613. ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {
  614. auto D = E->getBase()->getDependence();
  615. if (auto *TSI = E->getDestroyedTypeInfo())
  616. D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
  617. if (auto *ST = E->getScopeTypeInfo())
  618. D |= turnTypeToValueDependence(
  619. toExprDependenceAsWritten(ST->getType()->getDependence()));
  620. if (auto *Q = E->getQualifier())
  621. D |= toExprDependence(Q->getDependence() &
  622. ~NestedNameSpecifierDependence::Dependent);
  623. return D;
  624. }
  625. static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {
  626. auto D = ExprDependence::None;
  627. if (Name.isInstantiationDependent())
  628. D |= ExprDependence::Instantiation;
  629. if (Name.containsUnexpandedParameterPack())
  630. D |= ExprDependence::UnexpandedPack;
  631. return D;
  632. }
  633. ExprDependence
  634. clang::computeDependence(OverloadExpr *E, bool KnownDependent,
  635. bool KnownInstantiationDependent,
  636. bool KnownContainsUnexpandedParameterPack) {
  637. auto Deps = ExprDependence::None;
  638. if (KnownDependent)
  639. Deps |= ExprDependence::TypeValue;
  640. if (KnownInstantiationDependent)
  641. Deps |= ExprDependence::Instantiation;
  642. if (KnownContainsUnexpandedParameterPack)
  643. Deps |= ExprDependence::UnexpandedPack;
  644. Deps |= getDependenceInExpr(E->getNameInfo());
  645. if (auto *Q = E->getQualifier())
  646. Deps |= toExprDependence(Q->getDependence() &
  647. ~NestedNameSpecifierDependence::Dependent);
  648. for (auto *D : E->decls()) {
  649. if (D->getDeclContext()->isDependentContext() ||
  650. isa<UnresolvedUsingValueDecl>(D))
  651. Deps |= ExprDependence::TypeValueInstantiation;
  652. }
  653. // If we have explicit template arguments, check for dependent
  654. // template arguments and whether they contain any unexpanded pack
  655. // expansions.
  656. for (auto A : E->template_arguments())
  657. Deps |= toExprDependence(A.getArgument().getDependence());
  658. return Deps;
  659. }
  660. ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {
  661. auto D = ExprDependence::TypeValue;
  662. D |= getDependenceInExpr(E->getNameInfo());
  663. if (auto *Q = E->getQualifier())
  664. D |= toExprDependence(Q->getDependence());
  665. for (auto A : E->template_arguments())
  666. D |= toExprDependence(A.getArgument().getDependence());
  667. return D;
  668. }
  669. ExprDependence clang::computeDependence(CXXConstructExpr *E) {
  670. ExprDependence D =
  671. toExprDependenceForImpliedType(E->getType()->getDependence());
  672. for (auto *A : E->arguments())
  673. D |= A->getDependence() & ~ExprDependence::Type;
  674. return D;
  675. }
  676. ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) {
  677. CXXConstructExpr *BaseE = E;
  678. return toExprDependenceAsWritten(
  679. E->getTypeSourceInfo()->getType()->getDependence()) |
  680. computeDependence(BaseE);
  681. }
  682. ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) {
  683. return E->getExpr()->getDependence();
  684. }
  685. ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) {
  686. return E->getExpr()->getDependence();
  687. }
  688. ExprDependence clang::computeDependence(LambdaExpr *E,
  689. bool ContainsUnexpandedParameterPack) {
  690. auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
  691. if (ContainsUnexpandedParameterPack)
  692. D |= ExprDependence::UnexpandedPack;
  693. return D;
  694. }
  695. ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {
  696. auto D = ExprDependence::ValueInstantiation;
  697. D |= toExprDependenceAsWritten(E->getTypeAsWritten()->getDependence());
  698. D |= toExprDependenceForImpliedType(E->getType()->getDependence());
  699. for (auto *A : E->arguments())
  700. D |= A->getDependence() &
  701. (ExprDependence::UnexpandedPack | ExprDependence::Error);
  702. return D;
  703. }
  704. ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {
  705. auto D = ExprDependence::TypeValueInstantiation;
  706. if (!E->isImplicitAccess())
  707. D |= E->getBase()->getDependence();
  708. if (auto *Q = E->getQualifier())
  709. D |= toExprDependence(Q->getDependence());
  710. D |= getDependenceInExpr(E->getMemberNameInfo());
  711. for (auto A : E->template_arguments())
  712. D |= toExprDependence(A.getArgument().getDependence());
  713. return D;
  714. }
  715. ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {
  716. return E->getSubExpr()->getDependence();
  717. }
  718. ExprDependence clang::computeDependence(CXXFoldExpr *E) {
  719. auto D = ExprDependence::TypeValueInstantiation;
  720. for (const auto *C : {E->getLHS(), E->getRHS()}) {
  721. if (C)
  722. D |= C->getDependence() & ~ExprDependence::UnexpandedPack;
  723. }
  724. return D;
  725. }
  726. ExprDependence clang::computeDependence(CXXParenListInitExpr *E) {
  727. auto D = ExprDependence::None;
  728. for (const auto *A : E->getInitExprs())
  729. D |= A->getDependence();
  730. return D;
  731. }
  732. ExprDependence clang::computeDependence(TypeTraitExpr *E) {
  733. auto D = ExprDependence::None;
  734. for (const auto *A : E->getArgs())
  735. D |= toExprDependenceAsWritten(A->getType()->getDependence()) &
  736. ~ExprDependence::Type;
  737. return D;
  738. }
  739. ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,
  740. bool ValueDependent) {
  741. auto TA = TemplateArgumentDependence::None;
  742. const auto InterestingDeps = TemplateArgumentDependence::Instantiation |
  743. TemplateArgumentDependence::UnexpandedPack;
  744. for (const TemplateArgumentLoc &ArgLoc :
  745. E->getTemplateArgsAsWritten()->arguments()) {
  746. TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;
  747. if (TA == InterestingDeps)
  748. break;
  749. }
  750. ExprDependence D =
  751. ValueDependent ? ExprDependence::Value : ExprDependence::None;
  752. auto Res = D | toExprDependence(TA);
  753. if(!ValueDependent && E->getSatisfaction().ContainsErrors)
  754. Res |= ExprDependence::Error;
  755. return Res;
  756. }
  757. ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {
  758. auto D = ExprDependence::None;
  759. Expr **Elements = E->getElements();
  760. for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)
  761. D |= turnTypeToValueDependence(Elements[I]->getDependence());
  762. return D;
  763. }
  764. ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {
  765. auto Deps = ExprDependence::None;
  766. for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {
  767. auto KV = E->getKeyValueElement(I);
  768. auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() |
  769. KV.Value->getDependence());
  770. if (KV.EllipsisLoc.isValid())
  771. KVDeps &= ~ExprDependence::UnexpandedPack;
  772. Deps |= KVDeps;
  773. }
  774. return Deps;
  775. }
  776. ExprDependence clang::computeDependence(ObjCMessageExpr *E) {
  777. auto D = ExprDependence::None;
  778. if (auto *R = E->getInstanceReceiver())
  779. D |= R->getDependence();
  780. else
  781. D |= toExprDependenceForImpliedType(E->getType()->getDependence());
  782. for (auto *A : E->arguments())
  783. D |= A->getDependence();
  784. return D;
  785. }