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