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