962 lines · cpp
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-exception6//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 a49 // dependent member of the current instantiation is value-dependent. An50 // expression of the form & cast-expression is also value-dependent if51 // evaluating cast-expression as a core constant expression succeeds and52 // the result of the evaluation refers to a templated entity that is an53 // 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 it56 // 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>(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 toExprDependenceAsWritten(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 be90 // 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>(NoParens))100 D = DRE->getDecl();101 else if (const auto *ME = dyn_cast<MemberExpr>(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 E->getTypeSourceInfo()->getType()->getDependence()) |127 toExprDependenceForImpliedType(E->getType()->getDependence()) |128 turnTypeToValueDependence(E->getInitializer()->getDependence());129}130 131ExprDependence clang::computeDependence(ImplicitCastExpr *E) {132 // We model implicit conversions as combining the dependence of their133 // subexpression, apart from its type, with the semantic portion of the134 // 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 is144 // dependent (C++ [temp.dep.expr]p3).145 // Cast expressions are value-dependent if the type is146 // 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 not150 // dependent, but it may be deduced as a dependent type.151 ExprDependence D =152 toExprDependenceAsWritten(153 cast<ExplicitCastExpr>(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 conditional166 // to support the GCC vector conditional extension. Additionally,167 // [temp.dep.expr] does specify that this should be dependent on ALL sub168 // 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>(E->getSubStmt()->body_back()))182 if (const Expr *ResultExpr = CompoundExprResult->getExprStmt())183 D |= ResultExpr->getDependence();184 // Note: we treat a statement-expression in a dependent context as always185 // being value- and instantiation-dependent. This matches the behavior of186 // 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 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(Active, Inactive);213 // Take type- and value- dependency from the active branch. Propagate all214 // 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 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 bool ContainsUnexpandedParameterPack) {257 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());258 if (E->getBlockDecl()->isDependentContext())259 D |= ExprDependence::Instantiation;260 if (ContainsUnexpandedParameterPack)261 D |= ExprDependence::UnexpandedPack;262 return D;263}264 265ExprDependence clang::computeDependence(AsTypeExpr *E) {266 // FIXME: AsTypeExpr doesn't store the type as written. Assume the expression267 // type has identical sugar for now, so is a type-as-written.268 auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |269 E->getSrcExpr()->getDependence();270 if (!E->getType()->isDependentType())271 D &= ~ExprDependence::Type;272 return D;273}274 275ExprDependence clang::computeDependence(CXXRewrittenBinaryOperator *E) {276 return E->getSemanticForm()->getDependence();277}278 279ExprDependence clang::computeDependence(CXXStdInitializerListExpr *E) {280 auto D = turnTypeToValueDependence(E->getSubExpr()->getDependence());281 D |= toExprDependenceForImpliedType(E->getType()->getDependence());282 return D;283}284 285ExprDependence clang::computeDependence(CXXTypeidExpr *E) {286 auto D = ExprDependence::None;287 if (E->isTypeOperand())288 D = toExprDependenceAsWritten(289 E->getTypeOperandSourceInfo()->getType()->getDependence());290 else291 D = turnTypeToValueDependence(E->getExprOperand()->getDependence());292 // typeid is never type-dependent (C++ [temp.dep.expr]p4)293 return D & ~ExprDependence::Type;294}295 296ExprDependence clang::computeDependence(MSPropertyRefExpr *E) {297 return E->getBaseExpr()->getDependence() & ~ExprDependence::Type;298}299 300ExprDependence clang::computeDependence(MSPropertySubscriptExpr *E) {301 return E->getIdx()->getDependence();302}303 304ExprDependence clang::computeDependence(CXXUuidofExpr *E) {305 if (E->isTypeOperand())306 return turnTypeToValueDependence(toExprDependenceAsWritten(307 E->getTypeOperandSourceInfo()->getType()->getDependence()));308 309 return turnTypeToValueDependence(E->getExprOperand()->getDependence());310}311 312ExprDependence clang::computeDependence(CXXThisExpr *E) {313 // 'this' is type-dependent if the class type of the enclosing314 // member function is dependent (C++ [temp.dep.expr]p2)315 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());316 317 // If a lambda with an explicit object parameter captures '*this', then318 // 'this' now refers to the captured copy of lambda, and if the lambda319 // is type-dependent, so is the object and thus 'this'.320 //321 // Note: The standard does not mention this case explicitly, but we need322 // to do this so we can mark NSDM accesses as dependent.323 if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())324 D |= ExprDependence::Type;325 326 assert(!(D & ExprDependence::UnexpandedPack));327 return D;328}329 330ExprDependence clang::computeDependence(CXXThrowExpr *E) {331 auto *Op = E->getSubExpr();332 if (!Op)333 return ExprDependence::None;334 return Op->getDependence() & ~ExprDependence::TypeValue;335}336 337ExprDependence clang::computeDependence(CXXBindTemporaryExpr *E) {338 return E->getSubExpr()->getDependence();339}340 341ExprDependence clang::computeDependence(CXXScalarValueInitExpr *E) {342 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());343 if (auto *TSI = E->getTypeSourceInfo())344 D |= toExprDependenceAsWritten(TSI->getType()->getDependence());345 return D;346}347 348ExprDependence clang::computeDependence(CXXDeleteExpr *E) {349 return turnTypeToValueDependence(E->getArgument()->getDependence());350}351 352ExprDependence clang::computeDependence(ArrayTypeTraitExpr *E) {353 auto D = toExprDependenceAsWritten(E->getQueriedType()->getDependence());354 if (auto *Dim = E->getDimensionExpression())355 D |= Dim->getDependence();356 return turnTypeToValueDependence(D);357}358 359ExprDependence clang::computeDependence(ExpressionTraitExpr *E) {360 // Never type-dependent.361 auto D = E->getQueriedExpression()->getDependence() & ~ExprDependence::Type;362 // Value-dependent if the argument is type-dependent.363 if (E->getQueriedExpression()->isTypeDependent())364 D |= ExprDependence::Value;365 return D;366}367 368ExprDependence clang::computeDependence(CXXNoexceptExpr *E, CanThrowResult CT) {369 auto D = E->getOperand()->getDependence() & ~ExprDependence::TypeValue;370 if (CT == CT_Dependent)371 D |= ExprDependence::ValueInstantiation;372 return D;373}374 375ExprDependence clang::computeDependence(PackExpansionExpr *E) {376 return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) |377 ExprDependence::TypeValueInstantiation;378}379 380ExprDependence clang::computeDependence(PackIndexingExpr *E) {381 382 ExprDependence PatternDep = E->getPackIdExpression()->getDependence() &383 ~ExprDependence::UnexpandedPack;384 385 ExprDependence D = E->getIndexExpr()->getDependence();386 if (D & ExprDependence::TypeValueInstantiation)387 D |= E->getIndexExpr()->getDependence() | PatternDep |388 ExprDependence::Instantiation;389 390 ArrayRef<Expr *> Exprs = E->getExpressions();391 if (Exprs.empty() || !E->isFullySubstituted())392 D |= PatternDep | ExprDependence::Instantiation;393 else if (!E->getIndexExpr()->isInstantiationDependent()) {394 UnsignedOrNone Index = E->getSelectedIndex();395 assert(Index && *Index < Exprs.size() && "pack index out of bound");396 D |= Exprs[*Index]->getDependence();397 }398 return D;399}400 401ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {402 return E->getReplacement()->getDependence();403}404 405ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {406 if (auto *Resume = E->getResumeExpr())407 return (Resume->getDependence() &408 (ExprDependence::TypeValue | ExprDependence::Error)) |409 (E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);410 return E->getCommonExpr()->getDependence() |411 ExprDependence::TypeValueInstantiation;412}413 414ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {415 return E->getOperand()->getDependence() |416 ExprDependence::TypeValueInstantiation;417}418 419ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {420 return E->getSubExpr()->getDependence();421}422 423ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {424 return toExprDependenceAsWritten(E->getEncodedType()->getDependence());425}426 427ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {428 return turnTypeToValueDependence(E->getBase()->getDependence());429}430 431ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {432 if (E->isObjectReceiver())433 return E->getBase()->getDependence() & ~ExprDependence::Type;434 if (E->isSuperReceiver())435 return toExprDependenceForImpliedType(436 E->getSuperReceiverType()->getDependence()) &437 ~ExprDependence::TypeValue;438 assert(E->isClassReceiver());439 return ExprDependence::None;440}441 442ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {443 return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();444}445 446ExprDependence clang::computeDependence(ObjCIsaExpr *E) {447 return E->getBase()->getDependence() & ~ExprDependence::Type &448 ~ExprDependence::UnexpandedPack;449}450 451ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {452 return E->getSubExpr()->getDependence();453}454 455ExprDependence clang::computeDependence(ArraySectionExpr *E) {456 auto D = E->getBase()->getDependence();457 if (auto *LB = E->getLowerBound())458 D |= LB->getDependence();459 if (auto *Len = E->getLength())460 D |= Len->getDependence();461 462 if (E->isOMPArraySection()) {463 if (auto *Stride = E->getStride())464 D |= Stride->getDependence();465 }466 return D;467}468 469ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {470 auto D = E->getBase()->getDependence();471 for (Expr *Dim: E->getDimensions())472 if (Dim)473 D |= turnValueToTypeDependence(Dim->getDependence());474 return D;475}476 477ExprDependence clang::computeDependence(OMPIteratorExpr *E) {478 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());479 for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {480 if (auto *DD = cast_or_null<DeclaratorDecl>(E->getIteratorDecl(I))) {481 // If the type is omitted, it's 'int', and is not dependent in any way.482 if (auto *TSI = DD->getTypeSourceInfo()) {483 D |= toExprDependenceAsWritten(TSI->getType()->getDependence());484 }485 }486 OMPIteratorExpr::IteratorRange IR = E->getIteratorRange(I);487 if (Expr *BE = IR.Begin)488 D |= BE->getDependence();489 if (Expr *EE = IR.End)490 D |= EE->getDependence();491 if (Expr *SE = IR.Step)492 D |= SE->getDependence();493 }494 return D;495}496 497/// Compute the type-, value-, and instantiation-dependence of a498/// declaration reference499/// based on the declaration being referenced.500ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {501 auto Deps = ExprDependence::None;502 503 Deps |= toExprDependence(E->getQualifier().getDependence() &504 ~NestedNameSpecifierDependence::Dependent);505 506 if (auto *FirstArg = E->getTemplateArgs()) {507 unsigned NumArgs = E->getNumTemplateArgs();508 for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)509 Deps |= toExprDependence(Arg->getArgument().getDependence());510 }511 512 auto *Decl = E->getDecl();513 auto Type = E->getType();514 515 if (Decl->isParameterPack())516 Deps |= ExprDependence::UnexpandedPack;517 Deps |= toExprDependenceForImpliedType(Type->getDependence()) &518 ExprDependence::Error;519 520 // C++ [temp.dep.expr]p3:521 // An id-expression is type-dependent if it contains:522 523 // - an identifier associated by name lookup with one or more declarations524 // declared with a dependent type525 // - an identifier associated by name lookup with an entity captured by526 // copy ([expr.prim.lambda.capture])527 // in a lambda-expression that has an explicit object parameter whose528 // type is dependent ([dcl.fct]),529 //530 // [The "or more" case is not modeled as a DeclRefExpr. There are a bunch531 // more bullets here that we handle by treating the declaration as having a532 // dependent type if they involve a placeholder type that can't be deduced.]533 if (Type->isDependentType())534 Deps |= ExprDependence::TypeValueInstantiation;535 else if (Type->isInstantiationDependentType())536 Deps |= ExprDependence::Instantiation;537 538 // - an identifier associated by name lookup with an entity captured by539 // copy ([expr.prim.lambda.capture])540 if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())541 Deps |= ExprDependence::Type;542 543 // - a conversion-function-id that specifies a dependent type544 if (Decl->getDeclName().getNameKind() ==545 DeclarationName::CXXConversionFunctionName) {546 QualType T = Decl->getDeclName().getCXXNameType();547 if (T->isDependentType())548 return Deps | ExprDependence::TypeValueInstantiation;549 550 if (T->isInstantiationDependentType())551 Deps |= ExprDependence::Instantiation;552 }553 554 // - a template-id that is dependent,555 // - a nested-name-specifier or a qualified-id that names a member of an556 // unknown specialization557 // [These are not modeled as DeclRefExprs.]558 559 // or if it names a dependent member of the current instantiation that is a560 // static data member of type "array of unknown bound of T" for some T561 // [handled below].562 563 // C++ [temp.dep.constexpr]p2:564 // An id-expression is value-dependent if:565 566 // - it is type-dependent [handled above]567 568 // - it is the name of a non-type template parameter,569 if (isa<NonTypeTemplateParmDecl>(Decl))570 return Deps | ExprDependence::ValueInstantiation;571 572 // - it names a potentially-constant variable that is initialized with an573 // expression that is value-dependent574 if (const auto *Var = dyn_cast<VarDecl>(Decl)) {575 if (const Expr *Init = Var->getAnyInitializer()) {576 if (Init->containsErrors())577 Deps |= ExprDependence::Error;578 579 if (Var->mightBeUsableInConstantExpressions(Ctx) &&580 Init->isValueDependent())581 Deps |= ExprDependence::ValueInstantiation;582 }583 584 // - it names a static data member that is a dependent member of the585 // current instantiation and is not initialized in a member-declarator,586 if (Var->isStaticDataMember() &&587 Var->getDeclContext()->isDependentContext() &&588 !Var->getFirstDecl()->hasInit()) {589 const VarDecl *First = Var->getFirstDecl();590 TypeSourceInfo *TInfo = First->getTypeSourceInfo();591 if (TInfo->getType()->isIncompleteArrayType()) {592 Deps |= ExprDependence::TypeValueInstantiation;593 } else if (!First->hasInit()) {594 Deps |= ExprDependence::ValueInstantiation;595 }596 }597 598 return Deps;599 }600 601 // - it names a static member function that is a dependent member of the602 // current instantiation603 //604 // FIXME: It's unclear that the restriction to static members here has any605 // effect: any use of a non-static member function name requires either606 // forming a pointer-to-member or providing an object parameter, either of607 // which makes the overall expression value-dependent.608 if (auto *MD = dyn_cast<CXXMethodDecl>(Decl)) {609 if (MD->isStatic() && Decl->getDeclContext()->isDependentContext())610 Deps |= ExprDependence::ValueInstantiation;611 }612 613 return Deps;614}615 616ExprDependence clang::computeDependence(RecoveryExpr *E) {617 // RecoveryExpr is618 // - always value-dependent, and therefore instantiation dependent619 // - contains errors (ExprDependence::Error), by definition620 // - type-dependent if we don't know the type (fallback to an opaque621 // dependent type), or the type is known and dependent, or it has622 // type-dependent subexpressions.623 auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |624 ExprDependence::ErrorDependent;625 // FIXME: remove the type-dependent bit from subexpressions, if the626 // RecoveryExpr has a non-dependent type.627 for (auto *S : E->subExpressions())628 D |= S->getDependence();629 return D;630}631 632ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) {633 return toExprDependenceAsWritten(634 E->getTypeSourceInfo()->getType()->getDependence());635}636 637ExprDependence clang::computeDependence(PredefinedExpr *E) {638 return toExprDependenceForImpliedType(E->getType()->getDependence());639}640 641ExprDependence clang::computeDependence(CallExpr *E, ArrayRef<Expr *> PreArgs) {642 auto D = E->getCallee()->getDependence();643 if (E->getType()->isDependentType())644 D |= ExprDependence::Type;645 for (auto *A : ArrayRef(E->getArgs(), E->getNumArgs())) {646 if (A)647 D |= A->getDependence();648 }649 for (auto *A : PreArgs)650 D |= A->getDependence();651 return D;652}653 654ExprDependence clang::computeDependence(OffsetOfExpr *E) {655 auto D = turnTypeToValueDependence(toExprDependenceAsWritten(656 E->getTypeSourceInfo()->getType()->getDependence()));657 for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)658 D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence());659 return D;660}661 662static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {663 auto D = ExprDependence::None;664 if (Name.isInstantiationDependent())665 D |= ExprDependence::Instantiation;666 if (Name.containsUnexpandedParameterPack())667 D |= ExprDependence::UnexpandedPack;668 return D;669}670 671ExprDependence clang::computeDependence(MemberExpr *E) {672 auto D = E->getBase()->getDependence();673 D |= getDependenceInExpr(E->getMemberNameInfo());674 675 D |= toExprDependence(E->getQualifier().getDependence() &676 ~NestedNameSpecifierDependence::Dependent);677 678 for (const auto &A : E->template_arguments())679 D |= toExprDependence(A.getArgument().getDependence());680 681 auto *MemberDecl = E->getMemberDecl();682 if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {683 DeclContext *DC = MemberDecl->getDeclContext();684 // dyn_cast_or_null is used to handle objC variables which do not685 // have a declaration context.686 CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);687 if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) {688 if (!E->getType()->isDependentType())689 D &= ~ExprDependence::Type;690 }691 692 // Bitfield with value-dependent width is type-dependent.693 if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {694 D |= ExprDependence::Type;695 }696 }697 return D;698}699 700ExprDependence clang::computeDependence(InitListExpr *E) {701 auto D = ExprDependence::None;702 for (auto *A : E->inits())703 D |= A->getDependence();704 return D;705}706 707ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {708 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());709 for (auto *C : ArrayRef(E->getSubExprs(), E->getNumSubExprs()))710 D |= C->getDependence();711 return D;712}713 714ExprDependence clang::computeDependence(GenericSelectionExpr *E,715 bool ContainsUnexpandedPack) {716 auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack717 : ExprDependence::None;718 for (auto *AE : E->getAssocExprs())719 D |= AE->getDependence() & ExprDependence::Error;720 721 if (E->isExprPredicate())722 D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;723 else724 D |= toExprDependenceAsWritten(725 E->getControllingType()->getType()->getDependence());726 727 if (E->isResultDependent())728 return D | ExprDependence::TypeValueInstantiation;729 return D | (E->getResultExpr()->getDependence() &730 ~ExprDependence::UnexpandedPack);731}732 733ExprDependence clang::computeDependence(DesignatedInitExpr *E) {734 auto Deps = E->getInit()->getDependence();735 for (const auto &D : E->designators()) {736 auto DesignatorDeps = ExprDependence::None;737 if (D.isArrayDesignator())738 DesignatorDeps |= E->getArrayIndex(D)->getDependence();739 else if (D.isArrayRangeDesignator())740 DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |741 E->getArrayRangeEnd(D)->getDependence();742 Deps |= DesignatorDeps;743 if (DesignatorDeps & ExprDependence::TypeValue)744 Deps |= ExprDependence::TypeValueInstantiation;745 }746 return Deps;747}748 749ExprDependence clang::computeDependence(PseudoObjectExpr *O) {750 auto D = O->getSyntacticForm()->getDependence();751 for (auto *E : O->semantics())752 D |= E->getDependence();753 return D;754}755 756ExprDependence clang::computeDependence(AtomicExpr *A) {757 auto D = ExprDependence::None;758 for (auto *E : ArrayRef(A->getSubExprs(), A->getNumSubExprs()))759 D |= E->getDependence();760 return D;761}762 763ExprDependence clang::computeDependence(CXXNewExpr *E) {764 auto D = toExprDependenceAsWritten(765 E->getAllocatedTypeSourceInfo()->getType()->getDependence());766 D |= toExprDependenceForImpliedType(E->getAllocatedType()->getDependence());767 auto Size = E->getArraySize();768 if (Size && *Size)769 D |= turnTypeToValueDependence((*Size)->getDependence());770 if (auto *I = E->getInitializer())771 D |= turnTypeToValueDependence(I->getDependence());772 for (auto *A : E->placement_arguments())773 D |= turnTypeToValueDependence(A->getDependence());774 return D;775}776 777ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {778 auto D = E->getBase()->getDependence();779 if (auto *TSI = E->getDestroyedTypeInfo())780 D |= toExprDependenceAsWritten(TSI->getType()->getDependence());781 if (auto *ST = E->getScopeTypeInfo())782 D |= turnTypeToValueDependence(783 toExprDependenceAsWritten(ST->getType()->getDependence()));784 D |= toExprDependence(E->getQualifier().getDependence() &785 ~NestedNameSpecifierDependence::Dependent);786 return D;787}788 789ExprDependence790clang::computeDependence(OverloadExpr *E, bool KnownDependent,791 bool KnownInstantiationDependent,792 bool KnownContainsUnexpandedParameterPack) {793 auto Deps = ExprDependence::None;794 if (KnownDependent)795 Deps |= ExprDependence::TypeValue;796 if (KnownInstantiationDependent)797 Deps |= ExprDependence::Instantiation;798 if (KnownContainsUnexpandedParameterPack)799 Deps |= ExprDependence::UnexpandedPack;800 Deps |= getDependenceInExpr(E->getNameInfo());801 Deps |= toExprDependence(E->getQualifier().getDependence() &802 ~NestedNameSpecifierDependence::Dependent);803 for (auto *D : E->decls()) {804 if (D->getDeclContext()->isDependentContext() ||805 isa<UnresolvedUsingValueDecl>(D) || isa<TemplateTemplateParmDecl>(D))806 Deps |= ExprDependence::TypeValueInstantiation;807 }808 // If we have explicit template arguments, check for dependent809 // template arguments and whether they contain any unexpanded pack810 // expansions.811 for (const auto &A : E->template_arguments())812 Deps |= toExprDependence(A.getArgument().getDependence());813 return Deps;814}815 816ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {817 auto D = ExprDependence::TypeValue;818 D |= getDependenceInExpr(E->getNameInfo());819 D |= toExprDependence(E->getQualifier().getDependence());820 for (const auto &A : E->template_arguments())821 D |= toExprDependence(A.getArgument().getDependence());822 return D;823}824 825ExprDependence clang::computeDependence(CXXConstructExpr *E) {826 ExprDependence D =827 toExprDependenceForImpliedType(E->getType()->getDependence());828 for (auto *A : E->arguments())829 D |= A->getDependence() & ~ExprDependence::Type;830 return D;831}832 833ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) {834 CXXConstructExpr *BaseE = E;835 return toExprDependenceAsWritten(836 E->getTypeSourceInfo()->getType()->getDependence()) |837 computeDependence(BaseE);838}839 840ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) {841 return E->getExpr()->getDependence();842}843 844ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) {845 return E->getExpr()->getDependence();846}847 848ExprDependence clang::computeDependence(LambdaExpr *E,849 bool ContainsUnexpandedParameterPack) {850 auto D = toExprDependenceForImpliedType(E->getType()->getDependence());851 if (ContainsUnexpandedParameterPack)852 D |= ExprDependence::UnexpandedPack;853 return D;854}855 856ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {857 auto D = ExprDependence::ValueInstantiation;858 D |= toExprDependenceAsWritten(E->getTypeAsWritten()->getDependence());859 D |= toExprDependenceForImpliedType(E->getType()->getDependence());860 for (auto *A : E->arguments())861 D |= A->getDependence() &862 (ExprDependence::UnexpandedPack | ExprDependence::Error);863 return D;864}865 866ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {867 auto D = ExprDependence::TypeValueInstantiation;868 if (!E->isImplicitAccess())869 D |= E->getBase()->getDependence();870 D |= toExprDependence(E->getQualifier().getDependence());871 D |= getDependenceInExpr(E->getMemberNameInfo());872 for (const auto &A : E->template_arguments())873 D |= toExprDependence(A.getArgument().getDependence());874 return D;875}876 877ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {878 return E->getSubExpr()->getDependence();879}880 881ExprDependence clang::computeDependence(CXXFoldExpr *E) {882 auto D = ExprDependence::TypeValueInstantiation;883 for (const auto *C : {E->getLHS(), E->getRHS()}) {884 if (C)885 D |= C->getDependence() & ~ExprDependence::UnexpandedPack;886 }887 return D;888}889 890ExprDependence clang::computeDependence(CXXParenListInitExpr *E) {891 auto D = ExprDependence::None;892 for (const auto *A : E->getInitExprs())893 D |= A->getDependence();894 return D;895}896 897ExprDependence clang::computeDependence(TypeTraitExpr *E) {898 auto D = ExprDependence::None;899 for (const auto *A : E->getArgs())900 D |= toExprDependenceAsWritten(A->getType()->getDependence()) &901 ~ExprDependence::Type;902 return D;903}904 905ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,906 bool ValueDependent) {907 auto TA = TemplateArgumentDependence::None;908 const auto InterestingDeps = TemplateArgumentDependence::Instantiation |909 TemplateArgumentDependence::UnexpandedPack;910 for (const TemplateArgumentLoc &ArgLoc :911 E->getTemplateArgsAsWritten()->arguments()) {912 TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;913 if (TA == InterestingDeps)914 break;915 }916 917 ExprDependence D =918 ValueDependent ? ExprDependence::Value : ExprDependence::None;919 auto Res = D | toExprDependence(TA);920 if(!ValueDependent && E->getSatisfaction().ContainsErrors)921 Res |= ExprDependence::Error;922 return Res;923}924 925ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {926 auto D = ExprDependence::None;927 Expr **Elements = E->getElements();928 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)929 D |= turnTypeToValueDependence(Elements[I]->getDependence());930 return D;931}932 933ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {934 auto Deps = ExprDependence::None;935 for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {936 auto KV = E->getKeyValueElement(I);937 auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() |938 KV.Value->getDependence());939 if (KV.EllipsisLoc.isValid())940 KVDeps &= ~ExprDependence::UnexpandedPack;941 Deps |= KVDeps;942 }943 return Deps;944}945 946ExprDependence clang::computeDependence(ObjCMessageExpr *E) {947 auto D = ExprDependence::None;948 if (auto *R = E->getInstanceReceiver())949 D |= R->getDependence();950 else951 D |= toExprDependenceForImpliedType(E->getType()->getDependence());952 for (auto *A : E->arguments())953 D |= A->getDependence();954 return D;955}956 957ExprDependence clang::computeDependence(OpenACCAsteriskSizeExpr *E) {958 // This represents a simple asterisk as typed, so cannot be dependent in any959 // way.960 return ExprDependence::None;961}962