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