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