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