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

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