xref: /freebsd/contrib/llvm-project/clang/lib/AST/ComputeDependence.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 = toExprDependenceForImpliedType(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                                         const ASTContext &Ctx) {
42   ExprDependence Dep =
43       // FIXME: Do we need to look at the type?
44       toExprDependenceForImpliedType(E->getType()->getDependence()) |
45       E->getSubExpr()->getDependence();
46 
47   // C++ [temp.dep.constexpr]p5:
48   //   An expression of the form & qualified-id where the qualified-id names a
49   //   dependent member of the current instantiation is value-dependent. An
50   //   expression of the form & cast-expression is also value-dependent if
51   //   evaluating cast-expression as a core constant expression succeeds and
52   //   the result of the evaluation refers to a templated entity that is an
53   //   object with static or thread storage duration or a member function.
54   //
55   // What this amounts to is: constant-evaluate the operand and check whether it
56   // refers to a templated entity other than a variable with local storage.
57   if (Ctx.getLangOpts().CPlusPlus && E->getOpcode() == UO_AddrOf &&
58       !(Dep & ExprDependence::Value)) {
59     Expr::EvalResult Result;
60     SmallVector<PartialDiagnosticAt, 8> Diag;
61     Result.Diag = &Diag;
62     // FIXME: This doesn't enforce the C++98 constant expression rules.
63     if (E->getSubExpr()->EvaluateAsConstantExpr(Result, Ctx) && Diag.empty() &&
64         Result.Val.isLValue()) {
65       auto *VD = Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
66       if (VD && VD->isTemplated()) {
67         auto *VarD = dyn_cast<VarDecl>(VD);
68         if (!VarD || !VarD->hasLocalStorage())
69           Dep |= ExprDependence::Value;
70       }
71     }
72   }
73 
74   return Dep;
75 }
76 
77 ExprDependence clang::computeDependence(UnaryExprOrTypeTraitExpr *E) {
78   // Never type-dependent (C++ [temp.dep.expr]p3).
79   // Value-dependent if the argument is type-dependent.
80   if (E->isArgumentType())
81     return turnTypeToValueDependence(
82         toExprDependenceAsWritten(E->getArgumentType()->getDependence()));
83 
84   auto ArgDeps = E->getArgumentExpr()->getDependence();
85   auto Deps = ArgDeps & ~ExprDependence::TypeValue;
86   // Value-dependent if the argument is type-dependent.
87   if (ArgDeps & ExprDependence::Type)
88     Deps |= ExprDependence::Value;
89   // Check to see if we are in the situation where alignof(decl) should be
90   // dependent because decl's alignment is dependent.
91   auto ExprKind = E->getKind();
92   if (ExprKind != UETT_AlignOf && ExprKind != UETT_PreferredAlignOf)
93     return Deps;
94   if ((Deps & ExprDependence::Value) && (Deps & ExprDependence::Instantiation))
95     return Deps;
96 
97   auto *NoParens = E->getArgumentExpr()->IgnoreParens();
98   const ValueDecl *D = nullptr;
99   if (const auto *DRE = dyn_cast<DeclRefExpr>(NoParens))
100     D = DRE->getDecl();
101   else if (const auto *ME = dyn_cast<MemberExpr>(NoParens))
102     D = ME->getMemberDecl();
103   if (!D)
104     return Deps;
105   for (const auto *I : D->specific_attrs<AlignedAttr>()) {
106     if (I->isAlignmentErrorDependent())
107       Deps |= ExprDependence::Error;
108     if (I->isAlignmentDependent())
109       Deps |= ExprDependence::ValueInstantiation;
110   }
111   return Deps;
112 }
113 
114 ExprDependence clang::computeDependence(ArraySubscriptExpr *E) {
115   return E->getLHS()->getDependence() | E->getRHS()->getDependence();
116 }
117 
118 ExprDependence clang::computeDependence(MatrixSubscriptExpr *E) {
119   return E->getBase()->getDependence() | E->getRowIdx()->getDependence() |
120          (E->getColumnIdx() ? E->getColumnIdx()->getDependence()
121                             : ExprDependence::None);
122 }
123 
124 ExprDependence clang::computeDependence(CompoundLiteralExpr *E) {
125   return toExprDependenceAsWritten(
126              E->getTypeSourceInfo()->getType()->getDependence()) |
127          toExprDependenceForImpliedType(E->getType()->getDependence()) |
128          turnTypeToValueDependence(E->getInitializer()->getDependence());
129 }
130 
131 ExprDependence clang::computeDependence(ImplicitCastExpr *E) {
132   // We model implicit conversions as combining the dependence of their
133   // subexpression, apart from its type, with the semantic portion of the
134   // target type.
135   ExprDependence D =
136       toExprDependenceForImpliedType(E->getType()->getDependence());
137   if (auto *S = E->getSubExpr())
138     D |= S->getDependence() & ~ExprDependence::Type;
139   return D;
140 }
141 
142 ExprDependence clang::computeDependence(ExplicitCastExpr *E) {
143   // Cast expressions are type-dependent if the type is
144   // dependent (C++ [temp.dep.expr]p3).
145   // Cast expressions are value-dependent if the type is
146   // dependent or if the subexpression is value-dependent.
147   //
148   // Note that we also need to consider the dependence of the actual type here,
149   // because when the type as written is a deduced type, that type is not
150   // dependent, but it may be deduced as a dependent type.
151   ExprDependence D =
152       toExprDependenceAsWritten(
153           cast<ExplicitCastExpr>(E)->getTypeAsWritten()->getDependence()) |
154       toExprDependenceForImpliedType(E->getType()->getDependence());
155   if (auto *S = E->getSubExpr())
156     D |= S->getDependence() & ~ExprDependence::Type;
157   return D;
158 }
159 
160 ExprDependence clang::computeDependence(BinaryOperator *E) {
161   return E->getLHS()->getDependence() | E->getRHS()->getDependence();
162 }
163 
164 ExprDependence clang::computeDependence(ConditionalOperator *E) {
165   // The type of the conditional operator depends on the type of the conditional
166   // to support the GCC vector conditional extension. Additionally,
167   // [temp.dep.expr] does specify state that this should be dependent on ALL sub
168   // expressions.
169   return E->getCond()->getDependence() | E->getLHS()->getDependence() |
170          E->getRHS()->getDependence();
171 }
172 
173 ExprDependence clang::computeDependence(BinaryConditionalOperator *E) {
174   return E->getCommon()->getDependence() | E->getFalseExpr()->getDependence();
175 }
176 
177 ExprDependence clang::computeDependence(StmtExpr *E, unsigned TemplateDepth) {
178   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
179   // Propagate dependence of the result.
180   if (const auto *CompoundExprResult =
181           dyn_cast_or_null<ValueStmt>(E->getSubStmt()->getStmtExprResult()))
182     if (const Expr *ResultExpr = CompoundExprResult->getExprStmt())
183       D |= ResultExpr->getDependence();
184   // Note: we treat a statement-expression in a dependent context as always
185   // being value- and instantiation-dependent. This matches the behavior of
186   // lambda-expressions and GCC.
187   if (TemplateDepth)
188     D |= ExprDependence::ValueInstantiation;
189   // A param pack cannot be expanded over stmtexpr boundaries.
190   return D & ~ExprDependence::UnexpandedPack;
191 }
192 
193 ExprDependence clang::computeDependence(ConvertVectorExpr *E) {
194   auto D = toExprDependenceAsWritten(
195                E->getTypeSourceInfo()->getType()->getDependence()) |
196            E->getSrcExpr()->getDependence();
197   if (!E->getType()->isDependentType())
198     D &= ~ExprDependence::Type;
199   return D;
200 }
201 
202 ExprDependence clang::computeDependence(ChooseExpr *E) {
203   if (E->isConditionDependent())
204     return ExprDependence::TypeValueInstantiation |
205            E->getCond()->getDependence() | E->getLHS()->getDependence() |
206            E->getRHS()->getDependence();
207 
208   auto Cond = E->getCond()->getDependence();
209   auto Active = E->getLHS()->getDependence();
210   auto Inactive = E->getRHS()->getDependence();
211   if (!E->isConditionTrue())
212     std::swap(Active, Inactive);
213   // Take type- and value- dependency from the active branch. Propagate all
214   // other flags from all branches.
215   return (Active & ExprDependence::TypeValue) |
216          ((Cond | Active | Inactive) & ~ExprDependence::TypeValue);
217 }
218 
219 ExprDependence clang::computeDependence(ParenListExpr *P) {
220   auto D = ExprDependence::None;
221   for (auto *E : P->exprs())
222     D |= E->getDependence();
223   return D;
224 }
225 
226 ExprDependence clang::computeDependence(VAArgExpr *E) {
227   auto D = toExprDependenceAsWritten(
228                E->getWrittenTypeInfo()->getType()->getDependence()) |
229            (E->getSubExpr()->getDependence() & ~ExprDependence::Type);
230   return D;
231 }
232 
233 ExprDependence clang::computeDependence(NoInitExpr *E) {
234   return toExprDependenceForImpliedType(E->getType()->getDependence()) &
235          (ExprDependence::Instantiation | ExprDependence::Error);
236 }
237 
238 ExprDependence clang::computeDependence(ArrayInitLoopExpr *E) {
239   auto D = E->getCommonExpr()->getDependence() |
240            E->getSubExpr()->getDependence() | ExprDependence::Instantiation;
241   if (!E->getType()->isInstantiationDependentType())
242     D &= ~ExprDependence::Instantiation;
243   return turnTypeToValueDependence(D);
244 }
245 
246 ExprDependence clang::computeDependence(ImplicitValueInitExpr *E) {
247   return toExprDependenceForImpliedType(E->getType()->getDependence()) &
248          ExprDependence::Instantiation;
249 }
250 
251 ExprDependence clang::computeDependence(ExtVectorElementExpr *E) {
252   return E->getBase()->getDependence();
253 }
254 
255 ExprDependence clang::computeDependence(BlockExpr *E) {
256   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
257   if (E->getBlockDecl()->isDependentContext())
258     D |= ExprDependence::Instantiation;
259   return D;
260 }
261 
262 ExprDependence clang::computeDependence(AsTypeExpr *E) {
263   // FIXME: AsTypeExpr doesn't store the type as written. Assume the expression
264   // type has identical sugar for now, so is a type-as-written.
265   auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |
266            E->getSrcExpr()->getDependence();
267   if (!E->getType()->isDependentType())
268     D &= ~ExprDependence::Type;
269   return D;
270 }
271 
272 ExprDependence clang::computeDependence(CXXRewrittenBinaryOperator *E) {
273   return E->getSemanticForm()->getDependence();
274 }
275 
276 ExprDependence clang::computeDependence(CXXStdInitializerListExpr *E) {
277   auto D = turnTypeToValueDependence(E->getSubExpr()->getDependence());
278   D |= toExprDependenceForImpliedType(E->getType()->getDependence());
279   return D;
280 }
281 
282 ExprDependence clang::computeDependence(CXXTypeidExpr *E) {
283   auto D = ExprDependence::None;
284   if (E->isTypeOperand())
285     D = toExprDependenceAsWritten(
286         E->getTypeOperandSourceInfo()->getType()->getDependence());
287   else
288     D = turnTypeToValueDependence(E->getExprOperand()->getDependence());
289   // typeid is never type-dependent (C++ [temp.dep.expr]p4)
290   return D & ~ExprDependence::Type;
291 }
292 
293 ExprDependence clang::computeDependence(MSPropertyRefExpr *E) {
294   return E->getBaseExpr()->getDependence() & ~ExprDependence::Type;
295 }
296 
297 ExprDependence clang::computeDependence(MSPropertySubscriptExpr *E) {
298   return E->getIdx()->getDependence();
299 }
300 
301 ExprDependence clang::computeDependence(CXXUuidofExpr *E) {
302   if (E->isTypeOperand())
303     return turnTypeToValueDependence(toExprDependenceAsWritten(
304         E->getTypeOperandSourceInfo()->getType()->getDependence()));
305 
306   return turnTypeToValueDependence(E->getExprOperand()->getDependence());
307 }
308 
309 ExprDependence clang::computeDependence(CXXThisExpr *E) {
310   // 'this' is type-dependent if the class type of the enclosing
311   // member function is dependent (C++ [temp.dep.expr]p2)
312   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
313   assert(!(D & ExprDependence::UnexpandedPack));
314   return D;
315 }
316 
317 ExprDependence clang::computeDependence(CXXThrowExpr *E) {
318   auto *Op = E->getSubExpr();
319   if (!Op)
320     return ExprDependence::None;
321   return Op->getDependence() & ~ExprDependence::TypeValue;
322 }
323 
324 ExprDependence clang::computeDependence(CXXBindTemporaryExpr *E) {
325   return E->getSubExpr()->getDependence();
326 }
327 
328 ExprDependence clang::computeDependence(CXXScalarValueInitExpr *E) {
329   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
330   if (auto *TSI = E->getTypeSourceInfo())
331     D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
332   return D;
333 }
334 
335 ExprDependence clang::computeDependence(CXXDeleteExpr *E) {
336   return turnTypeToValueDependence(E->getArgument()->getDependence());
337 }
338 
339 ExprDependence clang::computeDependence(ArrayTypeTraitExpr *E) {
340   auto D = toExprDependenceAsWritten(E->getQueriedType()->getDependence());
341   if (auto *Dim = E->getDimensionExpression())
342     D |= Dim->getDependence();
343   return turnTypeToValueDependence(D);
344 }
345 
346 ExprDependence clang::computeDependence(ExpressionTraitExpr *E) {
347   // Never type-dependent.
348   auto D = E->getQueriedExpression()->getDependence() & ~ExprDependence::Type;
349   // Value-dependent if the argument is type-dependent.
350   if (E->getQueriedExpression()->isTypeDependent())
351     D |= ExprDependence::Value;
352   return D;
353 }
354 
355 ExprDependence clang::computeDependence(CXXNoexceptExpr *E, CanThrowResult CT) {
356   auto D = E->getOperand()->getDependence() & ~ExprDependence::TypeValue;
357   if (CT == CT_Dependent)
358     D |= ExprDependence::ValueInstantiation;
359   return D;
360 }
361 
362 ExprDependence clang::computeDependence(PackExpansionExpr *E) {
363   return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) |
364          ExprDependence::TypeValueInstantiation;
365 }
366 
367 ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {
368   return E->getReplacement()->getDependence();
369 }
370 
371 ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {
372   if (auto *Resume = E->getResumeExpr())
373     return (Resume->getDependence() &
374             (ExprDependence::TypeValue | ExprDependence::Error)) |
375            (E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);
376   return E->getCommonExpr()->getDependence() |
377          ExprDependence::TypeValueInstantiation;
378 }
379 
380 ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {
381   return E->getOperand()->getDependence() |
382          ExprDependence::TypeValueInstantiation;
383 }
384 
385 ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {
386   return E->getSubExpr()->getDependence();
387 }
388 
389 ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {
390   return toExprDependenceAsWritten(E->getEncodedType()->getDependence());
391 }
392 
393 ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {
394   return turnTypeToValueDependence(E->getBase()->getDependence());
395 }
396 
397 ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {
398   if (E->isObjectReceiver())
399     return E->getBase()->getDependence() & ~ExprDependence::Type;
400   if (E->isSuperReceiver())
401     return toExprDependenceForImpliedType(
402                E->getSuperReceiverType()->getDependence()) &
403            ~ExprDependence::TypeValue;
404   assert(E->isClassReceiver());
405   return ExprDependence::None;
406 }
407 
408 ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {
409   return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();
410 }
411 
412 ExprDependence clang::computeDependence(ObjCIsaExpr *E) {
413   return E->getBase()->getDependence() & ~ExprDependence::Type &
414          ~ExprDependence::UnexpandedPack;
415 }
416 
417 ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {
418   return E->getSubExpr()->getDependence();
419 }
420 
421 ExprDependence clang::computeDependence(OMPArraySectionExpr *E) {
422   auto D = E->getBase()->getDependence();
423   if (auto *LB = E->getLowerBound())
424     D |= LB->getDependence();
425   if (auto *Len = E->getLength())
426     D |= Len->getDependence();
427   return D;
428 }
429 
430 ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {
431   auto D = E->getBase()->getDependence();
432   for (Expr *Dim: E->getDimensions())
433     if (Dim)
434       D |= turnValueToTypeDependence(Dim->getDependence());
435   return D;
436 }
437 
438 ExprDependence clang::computeDependence(OMPIteratorExpr *E) {
439   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
440   for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
441     if (auto *DD = cast_or_null<DeclaratorDecl>(E->getIteratorDecl(I))) {
442       // If the type is omitted, it's 'int', and is not dependent in any way.
443       if (auto *TSI = DD->getTypeSourceInfo()) {
444         D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
445       }
446     }
447     OMPIteratorExpr::IteratorRange IR = E->getIteratorRange(I);
448     if (Expr *BE = IR.Begin)
449       D |= BE->getDependence();
450     if (Expr *EE = IR.End)
451       D |= EE->getDependence();
452     if (Expr *SE = IR.Step)
453       D |= SE->getDependence();
454   }
455   return D;
456 }
457 
458 /// Compute the type-, value-, and instantiation-dependence of a
459 /// declaration reference
460 /// based on the declaration being referenced.
461 ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {
462   auto Deps = ExprDependence::None;
463 
464   if (auto *NNS = E->getQualifier())
465     Deps |= toExprDependence(NNS->getDependence() &
466                              ~NestedNameSpecifierDependence::Dependent);
467 
468   if (auto *FirstArg = E->getTemplateArgs()) {
469     unsigned NumArgs = E->getNumTemplateArgs();
470     for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)
471       Deps |= toExprDependence(Arg->getArgument().getDependence());
472   }
473 
474   auto *Decl = E->getDecl();
475   auto Type = E->getType();
476 
477   if (Decl->isParameterPack())
478     Deps |= ExprDependence::UnexpandedPack;
479   Deps |= toExprDependenceForImpliedType(Type->getDependence()) &
480           ExprDependence::Error;
481 
482   // C++ [temp.dep.expr]p3:
483   //   An id-expression is type-dependent if it contains:
484 
485   //    - an identifier associated by name lookup with one or more declarations
486   //      declared with a dependent type
487   //
488   // [The "or more" case is not modeled as a DeclRefExpr. There are a bunch
489   // more bullets here that we handle by treating the declaration as having a
490   // dependent type if they involve a placeholder type that can't be deduced.]
491   if (Type->isDependentType())
492     Deps |= ExprDependence::TypeValueInstantiation;
493   else if (Type->isInstantiationDependentType())
494     Deps |= ExprDependence::Instantiation;
495 
496   //    - a conversion-function-id that specifies a dependent type
497   if (Decl->getDeclName().getNameKind() ==
498       DeclarationName::CXXConversionFunctionName) {
499     QualType T = Decl->getDeclName().getCXXNameType();
500     if (T->isDependentType())
501       return Deps | ExprDependence::TypeValueInstantiation;
502 
503     if (T->isInstantiationDependentType())
504       Deps |= ExprDependence::Instantiation;
505   }
506 
507   //   - a template-id that is dependent,
508   //   - a nested-name-specifier or a qualified-id that names a member of an
509   //     unknown specialization
510   //   [These are not modeled as DeclRefExprs.]
511 
512   //   or if it names a dependent member of the current instantiation that is a
513   //   static data member of type "array of unknown bound of T" for some T
514   //   [handled below].
515 
516   // C++ [temp.dep.constexpr]p2:
517   //  An id-expression is value-dependent if:
518 
519   //    - it is type-dependent [handled above]
520 
521   //    - it is the name of a non-type template parameter,
522   if (isa<NonTypeTemplateParmDecl>(Decl))
523     return Deps | ExprDependence::ValueInstantiation;
524 
525   //   - it names a potentially-constant variable that is initialized with an
526   //     expression that is value-dependent
527   if (const auto *Var = dyn_cast<VarDecl>(Decl)) {
528     if (const Expr *Init = Var->getAnyInitializer()) {
529       if (Init->containsErrors())
530         Deps |= ExprDependence::Error;
531 
532       if (Var->mightBeUsableInConstantExpressions(Ctx) &&
533           Init->isValueDependent())
534         Deps |= ExprDependence::ValueInstantiation;
535     }
536 
537     // - it names a static data member that is a dependent member of the
538     //   current instantiation and is not initialized in a member-declarator,
539     if (Var->isStaticDataMember() &&
540         Var->getDeclContext()->isDependentContext() &&
541         !Var->getFirstDecl()->hasInit()) {
542       const VarDecl *First = Var->getFirstDecl();
543       TypeSourceInfo *TInfo = First->getTypeSourceInfo();
544       if (TInfo->getType()->isIncompleteArrayType()) {
545         Deps |= ExprDependence::TypeValueInstantiation;
546       } else if (!First->hasInit()) {
547         Deps |= ExprDependence::ValueInstantiation;
548       }
549     }
550 
551     return Deps;
552   }
553 
554   //   - it names a static member function that is a dependent member of the
555   //     current instantiation
556   //
557   // FIXME: It's unclear that the restriction to static members here has any
558   // effect: any use of a non-static member function name requires either
559   // forming a pointer-to-member or providing an object parameter, either of
560   // which makes the overall expression value-dependent.
561   if (auto *MD = dyn_cast<CXXMethodDecl>(Decl)) {
562     if (MD->isStatic() && Decl->getDeclContext()->isDependentContext())
563       Deps |= ExprDependence::ValueInstantiation;
564   }
565 
566   return Deps;
567 }
568 
569 ExprDependence clang::computeDependence(RecoveryExpr *E) {
570   // RecoveryExpr is
571   //   - always value-dependent, and therefore instantiation dependent
572   //   - contains errors (ExprDependence::Error), by definition
573   //   - type-dependent if we don't know the type (fallback to an opaque
574   //     dependent type), or the type is known and dependent, or it has
575   //     type-dependent subexpressions.
576   auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |
577            ExprDependence::ErrorDependent;
578   // FIXME: remove the type-dependent bit from subexpressions, if the
579   // RecoveryExpr has a non-dependent type.
580   for (auto *S : E->subExpressions())
581     D |= S->getDependence();
582   return D;
583 }
584 
585 ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) {
586   return toExprDependenceAsWritten(
587       E->getTypeSourceInfo()->getType()->getDependence());
588 }
589 
590 ExprDependence clang::computeDependence(PredefinedExpr *E) {
591   return toExprDependenceForImpliedType(E->getType()->getDependence());
592 }
593 
594 ExprDependence clang::computeDependence(CallExpr *E,
595                                         llvm::ArrayRef<Expr *> PreArgs) {
596   auto D = E->getCallee()->getDependence();
597   for (auto *A : llvm::ArrayRef(E->getArgs(), E->getNumArgs())) {
598     if (A)
599       D |= A->getDependence();
600   }
601   for (auto *A : PreArgs)
602     D |= A->getDependence();
603   return D;
604 }
605 
606 ExprDependence clang::computeDependence(OffsetOfExpr *E) {
607   auto D = turnTypeToValueDependence(toExprDependenceAsWritten(
608       E->getTypeSourceInfo()->getType()->getDependence()));
609   for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)
610     D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence());
611   return D;
612 }
613 
614 static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {
615   auto D = ExprDependence::None;
616   if (Name.isInstantiationDependent())
617     D |= ExprDependence::Instantiation;
618   if (Name.containsUnexpandedParameterPack())
619     D |= ExprDependence::UnexpandedPack;
620   return D;
621 }
622 
623 ExprDependence clang::computeDependence(MemberExpr *E) {
624   auto D = E->getBase()->getDependence();
625   D |= getDependenceInExpr(E->getMemberNameInfo());
626 
627   if (auto *NNS = E->getQualifier())
628     D |= toExprDependence(NNS->getDependence() &
629                           ~NestedNameSpecifierDependence::Dependent);
630 
631   auto *MemberDecl = E->getMemberDecl();
632   if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {
633     DeclContext *DC = MemberDecl->getDeclContext();
634     // dyn_cast_or_null is used to handle objC variables which do not
635     // have a declaration context.
636     CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
637     if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) {
638       if (!E->getType()->isDependentType())
639         D &= ~ExprDependence::Type;
640     }
641 
642     // Bitfield with value-dependent width is type-dependent.
643     if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {
644       D |= ExprDependence::Type;
645     }
646   }
647   // FIXME: move remaining dependence computation from MemberExpr::Create()
648   return D;
649 }
650 
651 ExprDependence clang::computeDependence(InitListExpr *E) {
652   auto D = ExprDependence::None;
653   for (auto *A : E->inits())
654     D |= A->getDependence();
655   return D;
656 }
657 
658 ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {
659   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
660   for (auto *C : llvm::ArrayRef(E->getSubExprs(), E->getNumSubExprs()))
661     D |= C->getDependence();
662   return D;
663 }
664 
665 ExprDependence clang::computeDependence(GenericSelectionExpr *E,
666                                         bool ContainsUnexpandedPack) {
667   auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack
668                                   : ExprDependence::None;
669   for (auto *AE : E->getAssocExprs())
670     D |= AE->getDependence() & ExprDependence::Error;
671 
672   if (E->isExprPredicate())
673     D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;
674   else
675     D |= toExprDependenceAsWritten(
676         E->getControllingType()->getType()->getDependence());
677 
678   if (E->isResultDependent())
679     return D | ExprDependence::TypeValueInstantiation;
680   return D | (E->getResultExpr()->getDependence() &
681               ~ExprDependence::UnexpandedPack);
682 }
683 
684 ExprDependence clang::computeDependence(DesignatedInitExpr *E) {
685   auto Deps = E->getInit()->getDependence();
686   for (const auto &D : E->designators()) {
687     auto DesignatorDeps = ExprDependence::None;
688     if (D.isArrayDesignator())
689       DesignatorDeps |= E->getArrayIndex(D)->getDependence();
690     else if (D.isArrayRangeDesignator())
691       DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |
692                         E->getArrayRangeEnd(D)->getDependence();
693     Deps |= DesignatorDeps;
694     if (DesignatorDeps & ExprDependence::TypeValue)
695       Deps |= ExprDependence::TypeValueInstantiation;
696   }
697   return Deps;
698 }
699 
700 ExprDependence clang::computeDependence(PseudoObjectExpr *O) {
701   auto D = O->getSyntacticForm()->getDependence();
702   for (auto *E : O->semantics())
703     D |= E->getDependence();
704   return D;
705 }
706 
707 ExprDependence clang::computeDependence(AtomicExpr *A) {
708   auto D = ExprDependence::None;
709   for (auto *E : llvm::ArrayRef(A->getSubExprs(), A->getNumSubExprs()))
710     D |= E->getDependence();
711   return D;
712 }
713 
714 ExprDependence clang::computeDependence(CXXNewExpr *E) {
715   auto D = toExprDependenceAsWritten(
716       E->getAllocatedTypeSourceInfo()->getType()->getDependence());
717   D |= toExprDependenceForImpliedType(E->getAllocatedType()->getDependence());
718   auto Size = E->getArraySize();
719   if (Size && *Size)
720     D |= turnTypeToValueDependence((*Size)->getDependence());
721   if (auto *I = E->getInitializer())
722     D |= turnTypeToValueDependence(I->getDependence());
723   for (auto *A : E->placement_arguments())
724     D |= turnTypeToValueDependence(A->getDependence());
725   return D;
726 }
727 
728 ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {
729   auto D = E->getBase()->getDependence();
730   if (auto *TSI = E->getDestroyedTypeInfo())
731     D |= toExprDependenceAsWritten(TSI->getType()->getDependence());
732   if (auto *ST = E->getScopeTypeInfo())
733     D |= turnTypeToValueDependence(
734         toExprDependenceAsWritten(ST->getType()->getDependence()));
735   if (auto *Q = E->getQualifier())
736     D |= toExprDependence(Q->getDependence() &
737                           ~NestedNameSpecifierDependence::Dependent);
738   return D;
739 }
740 
741 ExprDependence
742 clang::computeDependence(OverloadExpr *E, bool KnownDependent,
743                          bool KnownInstantiationDependent,
744                          bool KnownContainsUnexpandedParameterPack) {
745   auto Deps = ExprDependence::None;
746   if (KnownDependent)
747     Deps |= ExprDependence::TypeValue;
748   if (KnownInstantiationDependent)
749     Deps |= ExprDependence::Instantiation;
750   if (KnownContainsUnexpandedParameterPack)
751     Deps |= ExprDependence::UnexpandedPack;
752   Deps |= getDependenceInExpr(E->getNameInfo());
753   if (auto *Q = E->getQualifier())
754     Deps |= toExprDependence(Q->getDependence() &
755                              ~NestedNameSpecifierDependence::Dependent);
756   for (auto *D : E->decls()) {
757     if (D->getDeclContext()->isDependentContext() ||
758         isa<UnresolvedUsingValueDecl>(D))
759       Deps |= ExprDependence::TypeValueInstantiation;
760   }
761   // If we have explicit template arguments, check for dependent
762   // template arguments and whether they contain any unexpanded pack
763   // expansions.
764   for (const auto &A : E->template_arguments())
765     Deps |= toExprDependence(A.getArgument().getDependence());
766   return Deps;
767 }
768 
769 ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {
770   auto D = ExprDependence::TypeValue;
771   D |= getDependenceInExpr(E->getNameInfo());
772   if (auto *Q = E->getQualifier())
773     D |= toExprDependence(Q->getDependence());
774   for (const auto &A : E->template_arguments())
775     D |= toExprDependence(A.getArgument().getDependence());
776   return D;
777 }
778 
779 ExprDependence clang::computeDependence(CXXConstructExpr *E) {
780   ExprDependence D =
781       toExprDependenceForImpliedType(E->getType()->getDependence());
782   for (auto *A : E->arguments())
783     D |= A->getDependence() & ~ExprDependence::Type;
784   return D;
785 }
786 
787 ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) {
788   CXXConstructExpr *BaseE = E;
789   return toExprDependenceAsWritten(
790              E->getTypeSourceInfo()->getType()->getDependence()) |
791          computeDependence(BaseE);
792 }
793 
794 ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) {
795   return E->getExpr()->getDependence();
796 }
797 
798 ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) {
799   return E->getExpr()->getDependence();
800 }
801 
802 ExprDependence clang::computeDependence(LambdaExpr *E,
803                                         bool ContainsUnexpandedParameterPack) {
804   auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
805   if (ContainsUnexpandedParameterPack)
806     D |= ExprDependence::UnexpandedPack;
807   return D;
808 }
809 
810 ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {
811   auto D = ExprDependence::ValueInstantiation;
812   D |= toExprDependenceAsWritten(E->getTypeAsWritten()->getDependence());
813   D |= toExprDependenceForImpliedType(E->getType()->getDependence());
814   for (auto *A : E->arguments())
815     D |= A->getDependence() &
816          (ExprDependence::UnexpandedPack | ExprDependence::Error);
817   return D;
818 }
819 
820 ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {
821   auto D = ExprDependence::TypeValueInstantiation;
822   if (!E->isImplicitAccess())
823     D |= E->getBase()->getDependence();
824   if (auto *Q = E->getQualifier())
825     D |= toExprDependence(Q->getDependence());
826   D |= getDependenceInExpr(E->getMemberNameInfo());
827   for (const auto &A : E->template_arguments())
828     D |= toExprDependence(A.getArgument().getDependence());
829   return D;
830 }
831 
832 ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {
833   return E->getSubExpr()->getDependence();
834 }
835 
836 ExprDependence clang::computeDependence(CXXFoldExpr *E) {
837   auto D = ExprDependence::TypeValueInstantiation;
838   for (const auto *C : {E->getLHS(), E->getRHS()}) {
839     if (C)
840       D |= C->getDependence() & ~ExprDependence::UnexpandedPack;
841   }
842   return D;
843 }
844 
845 ExprDependence clang::computeDependence(CXXParenListInitExpr *E) {
846   auto D = ExprDependence::None;
847   for (const auto *A : E->getInitExprs())
848     D |= A->getDependence();
849   return D;
850 }
851 
852 ExprDependence clang::computeDependence(TypeTraitExpr *E) {
853   auto D = ExprDependence::None;
854   for (const auto *A : E->getArgs())
855     D |= toExprDependenceAsWritten(A->getType()->getDependence()) &
856          ~ExprDependence::Type;
857   return D;
858 }
859 
860 ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,
861                                         bool ValueDependent) {
862   auto TA = TemplateArgumentDependence::None;
863   const auto InterestingDeps = TemplateArgumentDependence::Instantiation |
864                                TemplateArgumentDependence::UnexpandedPack;
865   for (const TemplateArgumentLoc &ArgLoc :
866        E->getTemplateArgsAsWritten()->arguments()) {
867     TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;
868     if (TA == InterestingDeps)
869       break;
870   }
871 
872   ExprDependence D =
873       ValueDependent ? ExprDependence::Value : ExprDependence::None;
874   auto Res = D | toExprDependence(TA);
875   if(!ValueDependent && E->getSatisfaction().ContainsErrors)
876     Res |= ExprDependence::Error;
877   return Res;
878 }
879 
880 ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {
881   auto D = ExprDependence::None;
882   Expr **Elements = E->getElements();
883   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)
884     D |= turnTypeToValueDependence(Elements[I]->getDependence());
885   return D;
886 }
887 
888 ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {
889   auto Deps = ExprDependence::None;
890   for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {
891     auto KV = E->getKeyValueElement(I);
892     auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() |
893                                             KV.Value->getDependence());
894     if (KV.EllipsisLoc.isValid())
895       KVDeps &= ~ExprDependence::UnexpandedPack;
896     Deps |= KVDeps;
897   }
898   return Deps;
899 }
900 
901 ExprDependence clang::computeDependence(ObjCMessageExpr *E) {
902   auto D = ExprDependence::None;
903   if (auto *R = E->getInstanceReceiver())
904     D |= R->getDependence();
905   else
906     D |= toExprDependenceForImpliedType(E->getType()->getDependence());
907   for (auto *A : E->arguments())
908     D |= A->getDependence();
909   return D;
910 }
911