ComputeDependence.cpp 31 KB

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