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