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