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