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