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