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(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(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(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(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(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(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(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(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(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(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(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(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(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 ExprDependence D = E->getIndexExpr()->getDependence();
379 ArrayRef<Expr *> Exprs = E->getExpressions();
380 if (Exprs.empty())
381 D |= (E->getPackIdExpression()->getDependence() |
382 ExprDependence::TypeValueInstantiation) &
383 ~ExprDependence::UnexpandedPack;
384 else if (!E->getIndexExpr()->isInstantiationDependent()) {
385 std::optional<unsigned> Index = E->getSelectedIndex();
386 assert(Index && *Index < Exprs.size() && "pack index out of bound");
387 D |= Exprs[*Index]->getDependence();
388 }
389 return D;
390}
391
392ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {
393 return E->getReplacement()->getDependence();
394}
395
396ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {
397 if (auto *Resume = E->getResumeExpr())
398 return (Resume->getDependence() &
399 (ExprDependence::TypeValue | ExprDependence::Error)) |
400 (E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);
401 return E->getCommonExpr()->getDependence() |
402 ExprDependence::TypeValueInstantiation;
403}
404
405ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {
406 return E->getOperand()->getDependence() |
407 ExprDependence::TypeValueInstantiation;
408}
409
410ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {
411 return E->getSubExpr()->getDependence();
412}
413
414ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {
415 return toExprDependenceAsWritten(D: E->getEncodedType()->getDependence());
416}
417
418ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {
419 return turnTypeToValueDependence(D: E->getBase()->getDependence());
420}
421
422ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {
423 if (E->isObjectReceiver())
424 return E->getBase()->getDependence() & ~ExprDependence::Type;
425 if (E->isSuperReceiver())
426 return toExprDependenceForImpliedType(
427 D: E->getSuperReceiverType()->getDependence()) &
428 ~ExprDependence::TypeValue;
429 assert(E->isClassReceiver());
430 return ExprDependence::None;
431}
432
433ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {
434 return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();
435}
436
437ExprDependence clang::computeDependence(ObjCIsaExpr *E) {
438 return E->getBase()->getDependence() & ~ExprDependence::Type &
439 ~ExprDependence::UnexpandedPack;
440}
441
442ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {
443 return E->getSubExpr()->getDependence();
444}
445
446ExprDependence clang::computeDependence(OMPArraySectionExpr *E) {
447 auto D = E->getBase()->getDependence();
448 if (auto *LB = E->getLowerBound())
449 D |= LB->getDependence();
450 if (auto *Len = E->getLength())
451 D |= Len->getDependence();
452 return D;
453}
454
455ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {
456 auto D = E->getBase()->getDependence();
457 for (Expr *Dim: E->getDimensions())
458 if (Dim)
459 D |= turnValueToTypeDependence(D: Dim->getDependence());
460 return D;
461}
462
463ExprDependence clang::computeDependence(OMPIteratorExpr *E) {
464 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
465 for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
466 if (auto *DD = cast_or_null<DeclaratorDecl>(Val: E->getIteratorDecl(I))) {
467 // If the type is omitted, it's 'int', and is not dependent in any way.
468 if (auto *TSI = DD->getTypeSourceInfo()) {
469 D |= toExprDependenceAsWritten(D: TSI->getType()->getDependence());
470 }
471 }
472 OMPIteratorExpr::IteratorRange IR = E->getIteratorRange(I);
473 if (Expr *BE = IR.Begin)
474 D |= BE->getDependence();
475 if (Expr *EE = IR.End)
476 D |= EE->getDependence();
477 if (Expr *SE = IR.Step)
478 D |= SE->getDependence();
479 }
480 return D;
481}
482
483/// Compute the type-, value-, and instantiation-dependence of a
484/// declaration reference
485/// based on the declaration being referenced.
486ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {
487 auto Deps = ExprDependence::None;
488
489 if (auto *NNS = E->getQualifier())
490 Deps |= toExprDependence(D: NNS->getDependence() &
491 ~NestedNameSpecifierDependence::Dependent);
492
493 if (auto *FirstArg = E->getTemplateArgs()) {
494 unsigned NumArgs = E->getNumTemplateArgs();
495 for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)
496 Deps |= toExprDependence(TA: Arg->getArgument().getDependence());
497 }
498
499 auto *Decl = E->getDecl();
500 auto Type = E->getType();
501
502 if (Decl->isParameterPack())
503 Deps |= ExprDependence::UnexpandedPack;
504 Deps |= toExprDependenceForImpliedType(Type->getDependence()) &
505 ExprDependence::Error;
506
507 // C++ [temp.dep.expr]p3:
508 // An id-expression is type-dependent if it contains:
509
510 // - an identifier associated by name lookup with one or more declarations
511 // declared with a dependent type
512 // - an identifier associated by name lookup with an entity captured by
513 // copy ([expr.prim.lambda.capture])
514 // in a lambda-expression that has an explicit object parameter whose
515 // type is dependent ([dcl.fct]),
516 //
517 // [The "or more" case is not modeled as a DeclRefExpr. There are a bunch
518 // more bullets here that we handle by treating the declaration as having a
519 // dependent type if they involve a placeholder type that can't be deduced.]
520 if (Type->isDependentType())
521 Deps |= ExprDependence::TypeValueInstantiation;
522 else if (Type->isInstantiationDependentType())
523 Deps |= ExprDependence::Instantiation;
524
525 // - an identifier associated by name lookup with an entity captured by
526 // copy ([expr.prim.lambda.capture])
527 if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())
528 Deps |= ExprDependence::Type;
529
530 // - a conversion-function-id that specifies a dependent type
531 if (Decl->getDeclName().getNameKind() ==
532 DeclarationName::CXXConversionFunctionName) {
533 QualType T = Decl->getDeclName().getCXXNameType();
534 if (T->isDependentType())
535 return Deps | ExprDependence::TypeValueInstantiation;
536
537 if (T->isInstantiationDependentType())
538 Deps |= ExprDependence::Instantiation;
539 }
540
541 // - a template-id that is dependent,
542 // - a nested-name-specifier or a qualified-id that names a member of an
543 // unknown specialization
544 // [These are not modeled as DeclRefExprs.]
545
546 // or if it names a dependent member of the current instantiation that is a
547 // static data member of type "array of unknown bound of T" for some T
548 // [handled below].
549
550 // C++ [temp.dep.constexpr]p2:
551 // An id-expression is value-dependent if:
552
553 // - it is type-dependent [handled above]
554
555 // - it is the name of a non-type template parameter,
556 if (isa<NonTypeTemplateParmDecl>(Val: Decl))
557 return Deps | ExprDependence::ValueInstantiation;
558
559 // - it names a potentially-constant variable that is initialized with an
560 // expression that is value-dependent
561 if (const auto *Var = dyn_cast<VarDecl>(Val: Decl)) {
562 if (const Expr *Init = Var->getAnyInitializer()) {
563 if (Init->containsErrors())
564 Deps |= ExprDependence::Error;
565
566 if (Var->mightBeUsableInConstantExpressions(C: Ctx) &&
567 Init->isValueDependent())
568 Deps |= ExprDependence::ValueInstantiation;
569 }
570
571 // - it names a static data member that is a dependent member of the
572 // current instantiation and is not initialized in a member-declarator,
573 if (Var->isStaticDataMember() &&
574 Var->getDeclContext()->isDependentContext() &&
575 !Var->getFirstDecl()->hasInit()) {
576 const VarDecl *First = Var->getFirstDecl();
577 TypeSourceInfo *TInfo = First->getTypeSourceInfo();
578 if (TInfo->getType()->isIncompleteArrayType()) {
579 Deps |= ExprDependence::TypeValueInstantiation;
580 } else if (!First->hasInit()) {
581 Deps |= ExprDependence::ValueInstantiation;
582 }
583 }
584
585 return Deps;
586 }
587
588 // - it names a static member function that is a dependent member of the
589 // current instantiation
590 //
591 // FIXME: It's unclear that the restriction to static members here has any
592 // effect: any use of a non-static member function name requires either
593 // forming a pointer-to-member or providing an object parameter, either of
594 // which makes the overall expression value-dependent.
595 if (auto *MD = dyn_cast<CXXMethodDecl>(Val: Decl)) {
596 if (MD->isStatic() && Decl->getDeclContext()->isDependentContext())
597 Deps |= ExprDependence::ValueInstantiation;
598 }
599
600 return Deps;
601}
602
603ExprDependence clang::computeDependence(RecoveryExpr *E) {
604 // RecoveryExpr is
605 // - always value-dependent, and therefore instantiation dependent
606 // - contains errors (ExprDependence::Error), by definition
607 // - type-dependent if we don't know the type (fallback to an opaque
608 // dependent type), or the type is known and dependent, or it has
609 // type-dependent subexpressions.
610 auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |
611 ExprDependence::ErrorDependent;
612 // FIXME: remove the type-dependent bit from subexpressions, if the
613 // RecoveryExpr has a non-dependent type.
614 for (auto *S : E->subExpressions())
615 D |= S->getDependence();
616 return D;
617}
618
619ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) {
620 return toExprDependenceAsWritten(
621 D: E->getTypeSourceInfo()->getType()->getDependence());
622}
623
624ExprDependence clang::computeDependence(PredefinedExpr *E) {
625 return toExprDependenceForImpliedType(E->getType()->getDependence());
626}
627
628ExprDependence clang::computeDependence(CallExpr *E,
629 llvm::ArrayRef<Expr *> PreArgs) {
630 auto D = E->getCallee()->getDependence();
631 if (E->getType()->isDependentType())
632 D |= ExprDependence::Type;
633 for (auto *A : llvm::ArrayRef(E->getArgs(), E->getNumArgs())) {
634 if (A)
635 D |= A->getDependence();
636 }
637 for (auto *A : PreArgs)
638 D |= A->getDependence();
639 return D;
640}
641
642ExprDependence clang::computeDependence(OffsetOfExpr *E) {
643 auto D = turnTypeToValueDependence(D: toExprDependenceAsWritten(
644 D: E->getTypeSourceInfo()->getType()->getDependence()));
645 for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)
646 D |= turnTypeToValueDependence(D: E->getIndexExpr(Idx: I)->getDependence());
647 return D;
648}
649
650static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {
651 auto D = ExprDependence::None;
652 if (Name.isInstantiationDependent())
653 D |= ExprDependence::Instantiation;
654 if (Name.containsUnexpandedParameterPack())
655 D |= ExprDependence::UnexpandedPack;
656 return D;
657}
658
659ExprDependence clang::computeDependence(MemberExpr *E) {
660 auto D = E->getBase()->getDependence();
661 D |= getDependenceInExpr(Name: E->getMemberNameInfo());
662
663 if (auto *NNS = E->getQualifier())
664 D |= toExprDependence(D: NNS->getDependence() &
665 ~NestedNameSpecifierDependence::Dependent);
666
667 for (const auto &A : E->template_arguments())
668 D |= toExprDependence(TA: A.getArgument().getDependence());
669
670 auto *MemberDecl = E->getMemberDecl();
671 if (FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemberDecl)) {
672 DeclContext *DC = MemberDecl->getDeclContext();
673 // dyn_cast_or_null is used to handle objC variables which do not
674 // have a declaration context.
675 CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(Val: DC);
676 if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(CurContext: DC)) {
677 if (!E->getType()->isDependentType())
678 D &= ~ExprDependence::Type;
679 }
680
681 // Bitfield with value-dependent width is type-dependent.
682 if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {
683 D |= ExprDependence::Type;
684 }
685 }
686 return D;
687}
688
689ExprDependence clang::computeDependence(InitListExpr *E) {
690 auto D = ExprDependence::None;
691 for (auto *A : E->inits())
692 D |= A->getDependence();
693 return D;
694}
695
696ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {
697 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
698 for (auto *C : llvm::ArrayRef(E->getSubExprs(), E->getNumSubExprs()))
699 D |= C->getDependence();
700 return D;
701}
702
703ExprDependence clang::computeDependence(GenericSelectionExpr *E,
704 bool ContainsUnexpandedPack) {
705 auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack
706 : ExprDependence::None;
707 for (auto *AE : E->getAssocExprs())
708 D |= AE->getDependence() & ExprDependence::Error;
709
710 if (E->isExprPredicate())
711 D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;
712 else
713 D |= toExprDependenceAsWritten(
714 D: E->getControllingType()->getType()->getDependence());
715
716 if (E->isResultDependent())
717 return D | ExprDependence::TypeValueInstantiation;
718 return D | (E->getResultExpr()->getDependence() &
719 ~ExprDependence::UnexpandedPack);
720}
721
722ExprDependence clang::computeDependence(DesignatedInitExpr *E) {
723 auto Deps = E->getInit()->getDependence();
724 for (const auto &D : E->designators()) {
725 auto DesignatorDeps = ExprDependence::None;
726 if (D.isArrayDesignator())
727 DesignatorDeps |= E->getArrayIndex(D)->getDependence();
728 else if (D.isArrayRangeDesignator())
729 DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |
730 E->getArrayRangeEnd(D)->getDependence();
731 Deps |= DesignatorDeps;
732 if (DesignatorDeps & ExprDependence::TypeValue)
733 Deps |= ExprDependence::TypeValueInstantiation;
734 }
735 return Deps;
736}
737
738ExprDependence clang::computeDependence(PseudoObjectExpr *O) {
739 auto D = O->getSyntacticForm()->getDependence();
740 for (auto *E : O->semantics())
741 D |= E->getDependence();
742 return D;
743}
744
745ExprDependence clang::computeDependence(AtomicExpr *A) {
746 auto D = ExprDependence::None;
747 for (auto *E : llvm::ArrayRef(A->getSubExprs(), A->getNumSubExprs()))
748 D |= E->getDependence();
749 return D;
750}
751
752ExprDependence clang::computeDependence(CXXNewExpr *E) {
753 auto D = toExprDependenceAsWritten(
754 D: E->getAllocatedTypeSourceInfo()->getType()->getDependence());
755 D |= toExprDependenceForImpliedType(D: E->getAllocatedType()->getDependence());
756 auto Size = E->getArraySize();
757 if (Size && *Size)
758 D |= turnTypeToValueDependence(D: (*Size)->getDependence());
759 if (auto *I = E->getInitializer())
760 D |= turnTypeToValueDependence(D: I->getDependence());
761 for (auto *A : E->placement_arguments())
762 D |= turnTypeToValueDependence(A->getDependence());
763 return D;
764}
765
766ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {
767 auto D = E->getBase()->getDependence();
768 if (auto *TSI = E->getDestroyedTypeInfo())
769 D |= toExprDependenceAsWritten(D: TSI->getType()->getDependence());
770 if (auto *ST = E->getScopeTypeInfo())
771 D |= turnTypeToValueDependence(
772 D: toExprDependenceAsWritten(D: ST->getType()->getDependence()));
773 if (auto *Q = E->getQualifier())
774 D |= toExprDependence(D: Q->getDependence() &
775 ~NestedNameSpecifierDependence::Dependent);
776 return D;
777}
778
779ExprDependence
780clang::computeDependence(OverloadExpr *E, bool KnownDependent,
781 bool KnownInstantiationDependent,
782 bool KnownContainsUnexpandedParameterPack) {
783 auto Deps = ExprDependence::None;
784 if (KnownDependent)
785 Deps |= ExprDependence::TypeValue;
786 if (KnownInstantiationDependent)
787 Deps |= ExprDependence::Instantiation;
788 if (KnownContainsUnexpandedParameterPack)
789 Deps |= ExprDependence::UnexpandedPack;
790 Deps |= getDependenceInExpr(Name: E->getNameInfo());
791 if (auto *Q = E->getQualifier())
792 Deps |= toExprDependence(D: Q->getDependence() &
793 ~NestedNameSpecifierDependence::Dependent);
794 for (auto *D : E->decls()) {
795 if (D->getDeclContext()->isDependentContext() ||
796 isa<UnresolvedUsingValueDecl>(Val: D))
797 Deps |= ExprDependence::TypeValueInstantiation;
798 }
799 // If we have explicit template arguments, check for dependent
800 // template arguments and whether they contain any unexpanded pack
801 // expansions.
802 for (const auto &A : E->template_arguments())
803 Deps |= toExprDependence(TA: A.getArgument().getDependence());
804 return Deps;
805}
806
807ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {
808 auto D = ExprDependence::TypeValue;
809 D |= getDependenceInExpr(Name: E->getNameInfo());
810 if (auto *Q = E->getQualifier())
811 D |= toExprDependence(D: Q->getDependence());
812 for (const auto &A : E->template_arguments())
813 D |= toExprDependence(TA: A.getArgument().getDependence());
814 return D;
815}
816
817ExprDependence clang::computeDependence(CXXConstructExpr *E) {
818 ExprDependence D =
819 toExprDependenceForImpliedType(E->getType()->getDependence());
820 for (auto *A : E->arguments())
821 D |= A->getDependence() & ~ExprDependence::Type;
822 return D;
823}
824
825ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) {
826 CXXConstructExpr *BaseE = E;
827 return toExprDependenceAsWritten(
828 D: E->getTypeSourceInfo()->getType()->getDependence()) |
829 computeDependence(E: BaseE);
830}
831
832ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) {
833 return E->getExpr()->getDependence();
834}
835
836ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) {
837 return E->getExpr()->getDependence();
838}
839
840ExprDependence clang::computeDependence(LambdaExpr *E,
841 bool ContainsUnexpandedParameterPack) {
842 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
843 if (ContainsUnexpandedParameterPack)
844 D |= ExprDependence::UnexpandedPack;
845 return D;
846}
847
848ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {
849 auto D = ExprDependence::ValueInstantiation;
850 D |= toExprDependenceAsWritten(D: E->getTypeAsWritten()->getDependence());
851 D |= toExprDependenceForImpliedType(E->getType()->getDependence());
852 for (auto *A : E->arguments())
853 D |= A->getDependence() &
854 (ExprDependence::UnexpandedPack | ExprDependence::Error);
855 return D;
856}
857
858ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {
859 auto D = ExprDependence::TypeValueInstantiation;
860 if (!E->isImplicitAccess())
861 D |= E->getBase()->getDependence();
862 if (auto *Q = E->getQualifier())
863 D |= toExprDependence(D: Q->getDependence());
864 D |= getDependenceInExpr(Name: E->getMemberNameInfo());
865 for (const auto &A : E->template_arguments())
866 D |= toExprDependence(TA: A.getArgument().getDependence());
867 return D;
868}
869
870ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {
871 return E->getSubExpr()->getDependence();
872}
873
874ExprDependence clang::computeDependence(CXXFoldExpr *E) {
875 auto D = ExprDependence::TypeValueInstantiation;
876 for (const auto *C : {E->getLHS(), E->getRHS()}) {
877 if (C)
878 D |= C->getDependence() & ~ExprDependence::UnexpandedPack;
879 }
880 return D;
881}
882
883ExprDependence clang::computeDependence(CXXParenListInitExpr *E) {
884 auto D = ExprDependence::None;
885 for (const auto *A : E->getInitExprs())
886 D |= A->getDependence();
887 return D;
888}
889
890ExprDependence clang::computeDependence(TypeTraitExpr *E) {
891 auto D = ExprDependence::None;
892 for (const auto *A : E->getArgs())
893 D |= toExprDependenceAsWritten(D: A->getType()->getDependence()) &
894 ~ExprDependence::Type;
895 return D;
896}
897
898ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,
899 bool ValueDependent) {
900 auto TA = TemplateArgumentDependence::None;
901 const auto InterestingDeps = TemplateArgumentDependence::Instantiation |
902 TemplateArgumentDependence::UnexpandedPack;
903 for (const TemplateArgumentLoc &ArgLoc :
904 E->getTemplateArgsAsWritten()->arguments()) {
905 TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;
906 if (TA == InterestingDeps)
907 break;
908 }
909
910 ExprDependence D =
911 ValueDependent ? ExprDependence::Value : ExprDependence::None;
912 auto Res = D | toExprDependence(TA);
913 if(!ValueDependent && E->getSatisfaction().ContainsErrors)
914 Res |= ExprDependence::Error;
915 return Res;
916}
917
918ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {
919 auto D = ExprDependence::None;
920 Expr **Elements = E->getElements();
921 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)
922 D |= turnTypeToValueDependence(D: Elements[I]->getDependence());
923 return D;
924}
925
926ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {
927 auto Deps = ExprDependence::None;
928 for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {
929 auto KV = E->getKeyValueElement(Index: I);
930 auto KVDeps = turnTypeToValueDependence(D: KV.Key->getDependence() |
931 KV.Value->getDependence());
932 if (KV.EllipsisLoc.isValid())
933 KVDeps &= ~ExprDependence::UnexpandedPack;
934 Deps |= KVDeps;
935 }
936 return Deps;
937}
938
939ExprDependence clang::computeDependence(ObjCMessageExpr *E) {
940 auto D = ExprDependence::None;
941 if (auto *R = E->getInstanceReceiver())
942 D |= R->getDependence();
943 else
944 D |= toExprDependenceForImpliedType(E->getType()->getDependence());
945 for (auto *A : E->arguments())
946 D |= A->getDependence();
947 return D;
948}
949

source code of clang/lib/AST/ComputeDependence.cpp