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