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