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