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   // FIXME: why is unexpanded-pack not propagated?
124   auto D = toExprDependence(E->getType()->getDependence()) &
125            ~ExprDependence::UnexpandedPack;
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 | ExprDependence::Error);
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   D |= toExprDependence(E->getType()->getDependence()) &
217        (ExprDependence::Type | ExprDependence::Error);
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(PackExpansionExpr *E) {
300   return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) |
301          ExprDependence::TypeValueInstantiation;
302 }
303 
304 ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {
305   return E->getReplacement()->getDependence();
306 }
307 
308 ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {
309   if (auto *Resume = E->getResumeExpr())
310     return (Resume->getDependence() &
311             (ExprDependence::TypeValue | ExprDependence::Error)) |
312            (E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);
313   return E->getCommonExpr()->getDependence() |
314          ExprDependence::TypeValueInstantiation;
315 }
316 
317 ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {
318   return E->getOperand()->getDependence() |
319          ExprDependence::TypeValueInstantiation;
320 }
321 
322 ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {
323   return E->getSubExpr()->getDependence();
324 }
325 
326 ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {
327   return toExprDependence(E->getEncodedType()->getDependence());
328 }
329 
330 ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {
331   return turnTypeToValueDependence(E->getBase()->getDependence());
332 }
333 
334 ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {
335   if (E->isObjectReceiver())
336     return E->getBase()->getDependence() & ~ExprDependence::Type;
337   if (E->isSuperReceiver())
338     return toExprDependence(E->getSuperReceiverType()->getDependence()) &
339            ~ExprDependence::TypeValue;
340   assert(E->isClassReceiver());
341   return ExprDependence::None;
342 }
343 
344 ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {
345   return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();
346 }
347 
348 ExprDependence clang::computeDependence(ObjCIsaExpr *E) {
349   return E->getBase()->getDependence() & ~ExprDependence::Type &
350          ~ExprDependence::UnexpandedPack;
351 }
352 
353 ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {
354   return E->getSubExpr()->getDependence();
355 }
356 
357 ExprDependence clang::computeDependence(OMPArraySectionExpr *E) {
358   auto D = E->getBase()->getDependence();
359   if (auto *LB = E->getLowerBound())
360     D |= LB->getDependence();
361   if (auto *Len = E->getLength())
362     D |= Len->getDependence();
363   return D;
364 }
365 
366 ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {
367   auto D = E->getBase()->getDependence() |
368            toExprDependence(E->getType()->getDependence());
369   for (Expr *Dim: E->getDimensions())
370     if (Dim)
371       D |= Dim->getDependence();
372   return D;
373 }
374 
375 /// Compute the type-, value-, and instantiation-dependence of a
376 /// declaration reference
377 /// based on the declaration being referenced.
378 ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {
379   auto Deps = ExprDependence::None;
380 
381   if (auto *NNS = E->getQualifier())
382     Deps |= toExprDependence(NNS->getDependence() &
383                              ~NestedNameSpecifierDependence::Dependent);
384 
385   if (auto *FirstArg = E->getTemplateArgs()) {
386     unsigned NumArgs = E->getNumTemplateArgs();
387     for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)
388       Deps |= toExprDependence(Arg->getArgument().getDependence());
389   }
390 
391   auto *Decl = E->getDecl();
392   auto Type = E->getType();
393 
394   if (Decl->isParameterPack())
395     Deps |= ExprDependence::UnexpandedPack;
396   Deps |= toExprDependence(Type->getDependence()) & ExprDependence::Error;
397 
398   // (TD) C++ [temp.dep.expr]p3:
399   //   An id-expression is type-dependent if it contains:
400   //
401   // and
402   //
403   // (VD) C++ [temp.dep.constexpr]p2:
404   //  An identifier is value-dependent if it is:
405 
406   //  (TD)  - an identifier that was declared with dependent type
407   //  (VD)  - a name declared with a dependent type,
408   if (Type->isDependentType())
409     return Deps | ExprDependence::TypeValueInstantiation;
410   else if (Type->isInstantiationDependentType())
411     Deps |= ExprDependence::Instantiation;
412 
413   //  (TD)  - a conversion-function-id that specifies a dependent type
414   if (Decl->getDeclName().getNameKind() ==
415       DeclarationName::CXXConversionFunctionName) {
416     QualType T = Decl->getDeclName().getCXXNameType();
417     if (T->isDependentType())
418       return Deps | ExprDependence::TypeValueInstantiation;
419 
420     if (T->isInstantiationDependentType())
421       Deps |= ExprDependence::Instantiation;
422   }
423 
424   //  (VD)  - the name of a non-type template parameter,
425   if (isa<NonTypeTemplateParmDecl>(Decl))
426     return Deps | ExprDependence::ValueInstantiation;
427 
428   //  (VD) - a constant with integral or enumeration type and is
429   //         initialized with an expression that is value-dependent.
430   //  (VD) - a constant with literal type and is initialized with an
431   //         expression that is value-dependent [C++11].
432   //  (VD) - FIXME: Missing from the standard:
433   //       -  an entity with reference type and is initialized with an
434   //          expression that is value-dependent [C++11]
435   if (VarDecl *Var = dyn_cast<VarDecl>(Decl)) {
436     if ((Ctx.getLangOpts().CPlusPlus11
437              ? Var->getType()->isLiteralType(Ctx)
438              : Var->getType()->isIntegralOrEnumerationType()) &&
439         (Var->getType().isConstQualified() ||
440          Var->getType()->isReferenceType())) {
441       if (const Expr *Init = Var->getAnyInitializer())
442         if (Init->isValueDependent()) {
443           Deps |= ExprDependence::ValueInstantiation;
444         }
445     }
446 
447     // (VD) - FIXME: Missing from the standard:
448     //      -  a member function or a static data member of the current
449     //         instantiation
450     if (Var->isStaticDataMember() &&
451         Var->getDeclContext()->isDependentContext()) {
452       Deps |= ExprDependence::ValueInstantiation;
453       TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
454       if (TInfo->getType()->isIncompleteArrayType())
455         Deps |= ExprDependence::Type;
456     }
457 
458     return Deps;
459   }
460 
461   // (VD) - FIXME: Missing from the standard:
462   //      -  a member function or a static data member of the current
463   //         instantiation
464   if (isa<CXXMethodDecl>(Decl) && Decl->getDeclContext()->isDependentContext())
465     Deps |= ExprDependence::ValueInstantiation;
466   return Deps;
467 }
468 
469 ExprDependence clang::computeDependence(RecoveryExpr *E) {
470   // FIXME: drop type+value+instantiation once Error is sufficient to suppress
471   // bogus dianostics.
472   auto D = ExprDependence::TypeValueInstantiation | ExprDependence::Error;
473   for (auto *S : E->subExpressions())
474     D |= S->getDependence();
475   return D;
476 }
477 
478 ExprDependence clang::computeDependence(PredefinedExpr *E) {
479   return toExprDependence(E->getType()->getDependence()) &
480          ~ExprDependence::UnexpandedPack;
481 }
482 
483 ExprDependence clang::computeDependence(CallExpr *E,
484                                         llvm::ArrayRef<Expr *> PreArgs) {
485   auto D = E->getCallee()->getDependence();
486   for (auto *A : llvm::makeArrayRef(E->getArgs(), E->getNumArgs())) {
487     if (A)
488       D |= A->getDependence();
489   }
490   for (auto *A : PreArgs)
491     D |= A->getDependence();
492   return D;
493 }
494 
495 ExprDependence clang::computeDependence(OffsetOfExpr *E) {
496   auto D = turnTypeToValueDependence(
497       toExprDependence(E->getTypeSourceInfo()->getType()->getDependence()));
498   for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)
499     D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence());
500   return D;
501 }
502 
503 ExprDependence clang::computeDependence(MemberExpr *E) {
504   auto *MemberDecl = E->getMemberDecl();
505   auto D = E->getBase()->getDependence();
506   if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {
507     DeclContext *DC = MemberDecl->getDeclContext();
508     // dyn_cast_or_null is used to handle objC variables which do not
509     // have a declaration context.
510     CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
511     if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) {
512       if (!E->getType()->isDependentType())
513         D &= ~ExprDependence::Type;
514     }
515 
516     // Bitfield with value-dependent width is type-dependent.
517     if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {
518       D |= ExprDependence::Type;
519     }
520   }
521   // FIXME: move remaining dependence computation from MemberExpr::Create()
522   return D;
523 }
524 
525 ExprDependence clang::computeDependence(InitListExpr *E) {
526   auto D = ExprDependence::None;
527   for (auto *A : E->inits())
528     D |= A->getDependence();
529   return D;
530 }
531 
532 ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {
533   auto D = toExprDependence(E->getType()->getDependence());
534   for (auto *C : llvm::makeArrayRef(E->getSubExprs(), E->getNumSubExprs()))
535     D |= C->getDependence();
536   return D;
537 }
538 
539 ExprDependence clang::computeDependence(GenericSelectionExpr *E,
540                                         bool ContainsUnexpandedPack) {
541   auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack
542                                   : ExprDependence::None;
543   for (auto *AE : E->getAssocExprs())
544     D |= AE->getDependence() & ExprDependence::Error;
545   D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;
546 
547   if (E->isResultDependent())
548     return D | ExprDependence::TypeValueInstantiation;
549   return D | (E->getResultExpr()->getDependence() &
550               ~ExprDependence::UnexpandedPack);
551 }
552 
553 ExprDependence clang::computeDependence(DesignatedInitExpr *E) {
554   auto Deps = E->getInit()->getDependence();
555   for (auto D : E->designators()) {
556     auto DesignatorDeps = ExprDependence::None;
557     if (D.isArrayDesignator())
558       DesignatorDeps |= E->getArrayIndex(D)->getDependence();
559     else if (D.isArrayRangeDesignator())
560       DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |
561                         E->getArrayRangeEnd(D)->getDependence();
562     Deps |= DesignatorDeps;
563     if (DesignatorDeps & ExprDependence::TypeValue)
564       Deps |= ExprDependence::TypeValueInstantiation;
565   }
566   return Deps;
567 }
568 
569 ExprDependence clang::computeDependence(PseudoObjectExpr *O) {
570   auto D = O->getSyntacticForm()->getDependence();
571   for (auto *E : O->semantics())
572     D |= E->getDependence();
573   return D;
574 }
575 
576 ExprDependence clang::computeDependence(AtomicExpr *A) {
577   auto D = ExprDependence::None;
578   for (auto *E : llvm::makeArrayRef(A->getSubExprs(), A->getNumSubExprs()))
579     D |= E->getDependence();
580   return D;
581 }
582 
583 ExprDependence clang::computeDependence(CXXNewExpr *E) {
584   auto D = toExprDependence(E->getType()->getDependence());
585   auto Size = E->getArraySize();
586   if (Size.hasValue() && *Size)
587     D |= turnTypeToValueDependence((*Size)->getDependence());
588   if (auto *I = E->getInitializer())
589     D |= turnTypeToValueDependence(I->getDependence());
590   for (auto *A : E->placement_arguments())
591     D |= turnTypeToValueDependence(A->getDependence());
592   return D;
593 }
594 
595 ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {
596   auto D = E->getBase()->getDependence();
597   if (!E->getDestroyedType().isNull())
598     D |= toExprDependence(E->getDestroyedType()->getDependence());
599   if (auto *ST = E->getScopeTypeInfo())
600     D |= turnTypeToValueDependence(
601         toExprDependence(ST->getType()->getDependence()));
602   if (auto *Q = E->getQualifier())
603     D |= toExprDependence(Q->getDependence() &
604                           ~NestedNameSpecifierDependence::Dependent);
605   return D;
606 }
607 
608 static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {
609   auto D = ExprDependence::None;
610   if (Name.isInstantiationDependent())
611     D |= ExprDependence::Instantiation;
612   if (Name.containsUnexpandedParameterPack())
613     D |= ExprDependence::UnexpandedPack;
614   return D;
615 }
616 
617 ExprDependence
618 clang::computeDependence(OverloadExpr *E, bool KnownDependent,
619                          bool KnownInstantiationDependent,
620                          bool KnownContainsUnexpandedParameterPack) {
621   auto Deps = ExprDependence::None;
622   if (KnownDependent)
623     Deps |= ExprDependence::TypeValue;
624   if (KnownInstantiationDependent)
625     Deps |= ExprDependence::Instantiation;
626   if (KnownContainsUnexpandedParameterPack)
627     Deps |= ExprDependence::UnexpandedPack;
628   Deps |= getDependenceInExpr(E->getNameInfo());
629   if (auto *Q = E->getQualifier())
630     Deps |= toExprDependence(Q->getDependence() &
631                              ~NestedNameSpecifierDependence::Dependent);
632   for (auto *D : E->decls()) {
633     if (D->getDeclContext()->isDependentContext() ||
634         isa<UnresolvedUsingValueDecl>(D))
635       Deps |= ExprDependence::TypeValueInstantiation;
636   }
637   // If we have explicit template arguments, check for dependent
638   // template arguments and whether they contain any unexpanded pack
639   // expansions.
640   for (auto A : E->template_arguments())
641     Deps |= toExprDependence(A.getArgument().getDependence());
642   return Deps;
643 }
644 
645 ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {
646   auto D = ExprDependence::TypeValue;
647   D |= getDependenceInExpr(E->getNameInfo());
648   if (auto *Q = E->getQualifier())
649     D |= toExprDependence(Q->getDependence());
650   for (auto A : E->template_arguments())
651     D |= toExprDependence(A.getArgument().getDependence());
652   return D;
653 }
654 
655 ExprDependence clang::computeDependence(CXXConstructExpr *E) {
656   auto D = toExprDependence(E->getType()->getDependence());
657   for (auto *A : E->arguments())
658     D |= A->getDependence() & ~ExprDependence::Type;
659   return D;
660 }
661 
662 ExprDependence clang::computeDependence(LambdaExpr *E,
663                                         bool ContainsUnexpandedParameterPack) {
664   auto D = toExprDependence(E->getType()->getDependence());
665   if (ContainsUnexpandedParameterPack)
666     D |= ExprDependence::UnexpandedPack;
667   return D;
668 }
669 
670 ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {
671   auto D = ExprDependence::ValueInstantiation;
672   D |= toExprDependence(E->getType()->getDependence());
673   if (E->getType()->getContainedDeducedType())
674     D |= ExprDependence::Type;
675   for (auto *A : E->arguments())
676     D |= A->getDependence() &
677          (ExprDependence::UnexpandedPack | ExprDependence::Error);
678   return D;
679 }
680 
681 ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {
682   auto D = ExprDependence::TypeValueInstantiation;
683   if (!E->isImplicitAccess())
684     D |= E->getBase()->getDependence();
685   if (auto *Q = E->getQualifier())
686     D |= toExprDependence(Q->getDependence());
687   D |= getDependenceInExpr(E->getMemberNameInfo());
688   for (auto A : E->template_arguments())
689     D |= toExprDependence(A.getArgument().getDependence());
690   return D;
691 }
692 
693 ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {
694   return E->getSubExpr()->getDependence();
695 }
696 
697 ExprDependence clang::computeDependence(CXXFoldExpr *E) {
698   auto D = ExprDependence::TypeValueInstantiation;
699   for (const auto *C : {E->getLHS(), E->getRHS()}) {
700     if (C)
701       D |= C->getDependence() & ~ExprDependence::UnexpandedPack;
702   }
703   return D;
704 }
705 
706 ExprDependence clang::computeDependence(TypeTraitExpr *E) {
707   auto D = ExprDependence::None;
708   for (const auto *A : E->getArgs())
709     D |=
710         toExprDependence(A->getType()->getDependence()) & ~ExprDependence::Type;
711   return D;
712 }
713 
714 ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,
715                                         bool ValueDependent) {
716   auto TA = TemplateArgumentDependence::None;
717   const auto InterestingDeps = TemplateArgumentDependence::Instantiation |
718                                TemplateArgumentDependence::UnexpandedPack;
719   for (const TemplateArgumentLoc &ArgLoc :
720        E->getTemplateArgsAsWritten()->arguments()) {
721     TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;
722     if (TA == InterestingDeps)
723       break;
724   }
725 
726   ExprDependence D =
727       ValueDependent ? ExprDependence::Value : ExprDependence::None;
728   return D | toExprDependence(TA);
729 }
730 
731 ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {
732   auto D = ExprDependence::None;
733   Expr **Elements = E->getElements();
734   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)
735     D |= turnTypeToValueDependence(Elements[I]->getDependence());
736   return D;
737 }
738 
739 ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {
740   auto Deps = ExprDependence::None;
741   for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {
742     auto KV = E->getKeyValueElement(I);
743     auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() |
744                                             KV.Value->getDependence());
745     if (KV.EllipsisLoc.isValid())
746       KVDeps &= ~ExprDependence::UnexpandedPack;
747     Deps |= KVDeps;
748   }
749   return Deps;
750 }
751 
752 ExprDependence clang::computeDependence(ObjCMessageExpr *E) {
753   auto D = ExprDependence::None;
754   if (auto *R = E->getInstanceReceiver())
755     D |= R->getDependence();
756   else
757     D |= toExprDependence(E->getType()->getDependence());
758   for (auto *A : E->arguments())
759     D |= A->getDependence();
760   return D;
761 }
762