Path: blob/main/contrib/llvm-project/clang/lib/AST/ComputeDependence.cpp
35260 views
//===- ComputeDependence.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/AST/ComputeDependence.h"9#include "clang/AST/Attr.h"10#include "clang/AST/DeclCXX.h"11#include "clang/AST/DeclarationName.h"12#include "clang/AST/DependenceFlags.h"13#include "clang/AST/Expr.h"14#include "clang/AST/ExprCXX.h"15#include "clang/AST/ExprConcepts.h"16#include "clang/AST/ExprObjC.h"17#include "clang/AST/ExprOpenMP.h"18#include "clang/Basic/ExceptionSpecificationType.h"19#include "llvm/ADT/ArrayRef.h"2021using namespace clang;2223ExprDependence clang::computeDependence(FullExpr *E) {24return E->getSubExpr()->getDependence();25}2627ExprDependence clang::computeDependence(OpaqueValueExpr *E) {28auto D = toExprDependenceForImpliedType(E->getType()->getDependence());29if (auto *S = E->getSourceExpr())30D |= S->getDependence();31assert(!(D & ExprDependence::UnexpandedPack));32return D;33}3435ExprDependence clang::computeDependence(ParenExpr *E) {36return E->getSubExpr()->getDependence();37}3839ExprDependence clang::computeDependence(UnaryOperator *E,40const ASTContext &Ctx) {41ExprDependence Dep =42// FIXME: Do we need to look at the type?43toExprDependenceForImpliedType(E->getType()->getDependence()) |44E->getSubExpr()->getDependence();4546// C++ [temp.dep.constexpr]p5:47// An expression of the form & qualified-id where the qualified-id names a48// dependent member of the current instantiation is value-dependent. An49// expression of the form & cast-expression is also value-dependent if50// evaluating cast-expression as a core constant expression succeeds and51// the result of the evaluation refers to a templated entity that is an52// object with static or thread storage duration or a member function.53//54// What this amounts to is: constant-evaluate the operand and check whether it55// refers to a templated entity other than a variable with local storage.56if (Ctx.getLangOpts().CPlusPlus && E->getOpcode() == UO_AddrOf &&57!(Dep & ExprDependence::Value)) {58Expr::EvalResult Result;59SmallVector<PartialDiagnosticAt, 8> Diag;60Result.Diag = &Diag;61// FIXME: This doesn't enforce the C++98 constant expression rules.62if (E->getSubExpr()->EvaluateAsConstantExpr(Result, Ctx) && Diag.empty() &&63Result.Val.isLValue()) {64auto *VD = Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();65if (VD && VD->isTemplated()) {66auto *VarD = dyn_cast<VarDecl>(VD);67if (!VarD || !VarD->hasLocalStorage())68Dep |= ExprDependence::Value;69}70}71}7273return Dep;74}7576ExprDependence clang::computeDependence(UnaryExprOrTypeTraitExpr *E) {77// Never type-dependent (C++ [temp.dep.expr]p3).78// Value-dependent if the argument is type-dependent.79if (E->isArgumentType())80return turnTypeToValueDependence(81toExprDependenceAsWritten(E->getArgumentType()->getDependence()));8283auto ArgDeps = E->getArgumentExpr()->getDependence();84auto Deps = ArgDeps & ~ExprDependence::TypeValue;85// Value-dependent if the argument is type-dependent.86if (ArgDeps & ExprDependence::Type)87Deps |= ExprDependence::Value;88// Check to see if we are in the situation where alignof(decl) should be89// dependent because decl's alignment is dependent.90auto ExprKind = E->getKind();91if (ExprKind != UETT_AlignOf && ExprKind != UETT_PreferredAlignOf)92return Deps;93if ((Deps & ExprDependence::Value) && (Deps & ExprDependence::Instantiation))94return Deps;9596auto *NoParens = E->getArgumentExpr()->IgnoreParens();97const ValueDecl *D = nullptr;98if (const auto *DRE = dyn_cast<DeclRefExpr>(NoParens))99D = DRE->getDecl();100else if (const auto *ME = dyn_cast<MemberExpr>(NoParens))101D = ME->getMemberDecl();102if (!D)103return Deps;104for (const auto *I : D->specific_attrs<AlignedAttr>()) {105if (I->isAlignmentErrorDependent())106Deps |= ExprDependence::Error;107if (I->isAlignmentDependent())108Deps |= ExprDependence::ValueInstantiation;109}110return Deps;111}112113ExprDependence clang::computeDependence(ArraySubscriptExpr *E) {114return E->getLHS()->getDependence() | E->getRHS()->getDependence();115}116117ExprDependence clang::computeDependence(MatrixSubscriptExpr *E) {118return E->getBase()->getDependence() | E->getRowIdx()->getDependence() |119(E->getColumnIdx() ? E->getColumnIdx()->getDependence()120: ExprDependence::None);121}122123ExprDependence clang::computeDependence(CompoundLiteralExpr *E) {124return toExprDependenceAsWritten(125E->getTypeSourceInfo()->getType()->getDependence()) |126toExprDependenceForImpliedType(E->getType()->getDependence()) |127turnTypeToValueDependence(E->getInitializer()->getDependence());128}129130ExprDependence clang::computeDependence(ImplicitCastExpr *E) {131// We model implicit conversions as combining the dependence of their132// subexpression, apart from its type, with the semantic portion of the133// target type.134ExprDependence D =135toExprDependenceForImpliedType(E->getType()->getDependence());136if (auto *S = E->getSubExpr())137D |= S->getDependence() & ~ExprDependence::Type;138return D;139}140141ExprDependence clang::computeDependence(ExplicitCastExpr *E) {142// Cast expressions are type-dependent if the type is143// dependent (C++ [temp.dep.expr]p3).144// Cast expressions are value-dependent if the type is145// dependent or if the subexpression is value-dependent.146//147// Note that we also need to consider the dependence of the actual type here,148// because when the type as written is a deduced type, that type is not149// dependent, but it may be deduced as a dependent type.150ExprDependence D =151toExprDependenceAsWritten(152cast<ExplicitCastExpr>(E)->getTypeAsWritten()->getDependence()) |153toExprDependenceForImpliedType(E->getType()->getDependence());154if (auto *S = E->getSubExpr())155D |= S->getDependence() & ~ExprDependence::Type;156return D;157}158159ExprDependence clang::computeDependence(BinaryOperator *E) {160return E->getLHS()->getDependence() | E->getRHS()->getDependence();161}162163ExprDependence clang::computeDependence(ConditionalOperator *E) {164// The type of the conditional operator depends on the type of the conditional165// to support the GCC vector conditional extension. Additionally,166// [temp.dep.expr] does specify state that this should be dependent on ALL sub167// expressions.168return E->getCond()->getDependence() | E->getLHS()->getDependence() |169E->getRHS()->getDependence();170}171172ExprDependence clang::computeDependence(BinaryConditionalOperator *E) {173return E->getCommon()->getDependence() | E->getFalseExpr()->getDependence();174}175176ExprDependence clang::computeDependence(StmtExpr *E, unsigned TemplateDepth) {177auto D = toExprDependenceForImpliedType(E->getType()->getDependence());178// Propagate dependence of the result.179if (const auto *CompoundExprResult =180dyn_cast_or_null<ValueStmt>(E->getSubStmt()->getStmtExprResult()))181if (const Expr *ResultExpr = CompoundExprResult->getExprStmt())182D |= ResultExpr->getDependence();183// Note: we treat a statement-expression in a dependent context as always184// being value- and instantiation-dependent. This matches the behavior of185// lambda-expressions and GCC.186if (TemplateDepth)187D |= ExprDependence::ValueInstantiation;188// A param pack cannot be expanded over stmtexpr boundaries.189return D & ~ExprDependence::UnexpandedPack;190}191192ExprDependence clang::computeDependence(ConvertVectorExpr *E) {193auto D = toExprDependenceAsWritten(194E->getTypeSourceInfo()->getType()->getDependence()) |195E->getSrcExpr()->getDependence();196if (!E->getType()->isDependentType())197D &= ~ExprDependence::Type;198return D;199}200201ExprDependence clang::computeDependence(ChooseExpr *E) {202if (E->isConditionDependent())203return ExprDependence::TypeValueInstantiation |204E->getCond()->getDependence() | E->getLHS()->getDependence() |205E->getRHS()->getDependence();206207auto Cond = E->getCond()->getDependence();208auto Active = E->getLHS()->getDependence();209auto Inactive = E->getRHS()->getDependence();210if (!E->isConditionTrue())211std::swap(Active, Inactive);212// Take type- and value- dependency from the active branch. Propagate all213// other flags from all branches.214return (Active & ExprDependence::TypeValue) |215((Cond | Active | Inactive) & ~ExprDependence::TypeValue);216}217218ExprDependence clang::computeDependence(ParenListExpr *P) {219auto D = ExprDependence::None;220for (auto *E : P->exprs())221D |= E->getDependence();222return D;223}224225ExprDependence clang::computeDependence(VAArgExpr *E) {226auto D = toExprDependenceAsWritten(227E->getWrittenTypeInfo()->getType()->getDependence()) |228(E->getSubExpr()->getDependence() & ~ExprDependence::Type);229return D;230}231232ExprDependence clang::computeDependence(NoInitExpr *E) {233return toExprDependenceForImpliedType(E->getType()->getDependence()) &234(ExprDependence::Instantiation | ExprDependence::Error);235}236237ExprDependence clang::computeDependence(ArrayInitLoopExpr *E) {238auto D = E->getCommonExpr()->getDependence() |239E->getSubExpr()->getDependence() | ExprDependence::Instantiation;240if (!E->getType()->isInstantiationDependentType())241D &= ~ExprDependence::Instantiation;242return turnTypeToValueDependence(D);243}244245ExprDependence clang::computeDependence(ImplicitValueInitExpr *E) {246return toExprDependenceForImpliedType(E->getType()->getDependence()) &247ExprDependence::Instantiation;248}249250ExprDependence clang::computeDependence(ExtVectorElementExpr *E) {251return E->getBase()->getDependence();252}253254ExprDependence clang::computeDependence(BlockExpr *E) {255auto D = toExprDependenceForImpliedType(E->getType()->getDependence());256if (E->getBlockDecl()->isDependentContext())257D |= ExprDependence::Instantiation;258return D;259}260261ExprDependence clang::computeDependence(AsTypeExpr *E) {262// FIXME: AsTypeExpr doesn't store the type as written. Assume the expression263// type has identical sugar for now, so is a type-as-written.264auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |265E->getSrcExpr()->getDependence();266if (!E->getType()->isDependentType())267D &= ~ExprDependence::Type;268return D;269}270271ExprDependence clang::computeDependence(CXXRewrittenBinaryOperator *E) {272return E->getSemanticForm()->getDependence();273}274275ExprDependence clang::computeDependence(CXXStdInitializerListExpr *E) {276auto D = turnTypeToValueDependence(E->getSubExpr()->getDependence());277D |= toExprDependenceForImpliedType(E->getType()->getDependence());278return D;279}280281ExprDependence clang::computeDependence(CXXTypeidExpr *E) {282auto D = ExprDependence::None;283if (E->isTypeOperand())284D = toExprDependenceAsWritten(285E->getTypeOperandSourceInfo()->getType()->getDependence());286else287D = turnTypeToValueDependence(E->getExprOperand()->getDependence());288// typeid is never type-dependent (C++ [temp.dep.expr]p4)289return D & ~ExprDependence::Type;290}291292ExprDependence clang::computeDependence(MSPropertyRefExpr *E) {293return E->getBaseExpr()->getDependence() & ~ExprDependence::Type;294}295296ExprDependence clang::computeDependence(MSPropertySubscriptExpr *E) {297return E->getIdx()->getDependence();298}299300ExprDependence clang::computeDependence(CXXUuidofExpr *E) {301if (E->isTypeOperand())302return turnTypeToValueDependence(toExprDependenceAsWritten(303E->getTypeOperandSourceInfo()->getType()->getDependence()));304305return turnTypeToValueDependence(E->getExprOperand()->getDependence());306}307308ExprDependence clang::computeDependence(CXXThisExpr *E) {309// 'this' is type-dependent if the class type of the enclosing310// member function is dependent (C++ [temp.dep.expr]p2)311auto D = toExprDependenceForImpliedType(E->getType()->getDependence());312313// If a lambda with an explicit object parameter captures '*this', then314// 'this' now refers to the captured copy of lambda, and if the lambda315// is type-dependent, so is the object and thus 'this'.316//317// Note: The standard does not mention this case explicitly, but we need318// to do this so we can mark NSDM accesses as dependent.319if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())320D |= ExprDependence::Type;321322assert(!(D & ExprDependence::UnexpandedPack));323return D;324}325326ExprDependence clang::computeDependence(CXXThrowExpr *E) {327auto *Op = E->getSubExpr();328if (!Op)329return ExprDependence::None;330return Op->getDependence() & ~ExprDependence::TypeValue;331}332333ExprDependence clang::computeDependence(CXXBindTemporaryExpr *E) {334return E->getSubExpr()->getDependence();335}336337ExprDependence clang::computeDependence(CXXScalarValueInitExpr *E) {338auto D = toExprDependenceForImpliedType(E->getType()->getDependence());339if (auto *TSI = E->getTypeSourceInfo())340D |= toExprDependenceAsWritten(TSI->getType()->getDependence());341return D;342}343344ExprDependence clang::computeDependence(CXXDeleteExpr *E) {345return turnTypeToValueDependence(E->getArgument()->getDependence());346}347348ExprDependence clang::computeDependence(ArrayTypeTraitExpr *E) {349auto D = toExprDependenceAsWritten(E->getQueriedType()->getDependence());350if (auto *Dim = E->getDimensionExpression())351D |= Dim->getDependence();352return turnTypeToValueDependence(D);353}354355ExprDependence clang::computeDependence(ExpressionTraitExpr *E) {356// Never type-dependent.357auto D = E->getQueriedExpression()->getDependence() & ~ExprDependence::Type;358// Value-dependent if the argument is type-dependent.359if (E->getQueriedExpression()->isTypeDependent())360D |= ExprDependence::Value;361return D;362}363364ExprDependence clang::computeDependence(CXXNoexceptExpr *E, CanThrowResult CT) {365auto D = E->getOperand()->getDependence() & ~ExprDependence::TypeValue;366if (CT == CT_Dependent)367D |= ExprDependence::ValueInstantiation;368return D;369}370371ExprDependence clang::computeDependence(PackExpansionExpr *E) {372return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) |373ExprDependence::TypeValueInstantiation;374}375376ExprDependence clang::computeDependence(PackIndexingExpr *E) {377378ExprDependence PatternDep = E->getPackIdExpression()->getDependence() &379~ExprDependence::UnexpandedPack;380381ExprDependence D = E->getIndexExpr()->getDependence();382if (D & ExprDependence::TypeValueInstantiation)383D |= E->getIndexExpr()->getDependence() | PatternDep |384ExprDependence::Instantiation;385386ArrayRef<Expr *> Exprs = E->getExpressions();387if (Exprs.empty())388D |= PatternDep | ExprDependence::Instantiation;389390else if (!E->getIndexExpr()->isInstantiationDependent()) {391std::optional<unsigned> Index = E->getSelectedIndex();392assert(Index && *Index < Exprs.size() && "pack index out of bound");393D |= Exprs[*Index]->getDependence();394}395return D;396}397398ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) {399return E->getReplacement()->getDependence();400}401402ExprDependence clang::computeDependence(CoroutineSuspendExpr *E) {403if (auto *Resume = E->getResumeExpr())404return (Resume->getDependence() &405(ExprDependence::TypeValue | ExprDependence::Error)) |406(E->getCommonExpr()->getDependence() & ~ExprDependence::TypeValue);407return E->getCommonExpr()->getDependence() |408ExprDependence::TypeValueInstantiation;409}410411ExprDependence clang::computeDependence(DependentCoawaitExpr *E) {412return E->getOperand()->getDependence() |413ExprDependence::TypeValueInstantiation;414}415416ExprDependence clang::computeDependence(ObjCBoxedExpr *E) {417return E->getSubExpr()->getDependence();418}419420ExprDependence clang::computeDependence(ObjCEncodeExpr *E) {421return toExprDependenceAsWritten(E->getEncodedType()->getDependence());422}423424ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) {425return turnTypeToValueDependence(E->getBase()->getDependence());426}427428ExprDependence clang::computeDependence(ObjCPropertyRefExpr *E) {429if (E->isObjectReceiver())430return E->getBase()->getDependence() & ~ExprDependence::Type;431if (E->isSuperReceiver())432return toExprDependenceForImpliedType(433E->getSuperReceiverType()->getDependence()) &434~ExprDependence::TypeValue;435assert(E->isClassReceiver());436return ExprDependence::None;437}438439ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) {440return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence();441}442443ExprDependence clang::computeDependence(ObjCIsaExpr *E) {444return E->getBase()->getDependence() & ~ExprDependence::Type &445~ExprDependence::UnexpandedPack;446}447448ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) {449return E->getSubExpr()->getDependence();450}451452ExprDependence clang::computeDependence(ArraySectionExpr *E) {453auto D = E->getBase()->getDependence();454if (auto *LB = E->getLowerBound())455D |= LB->getDependence();456if (auto *Len = E->getLength())457D |= Len->getDependence();458459if (E->isOMPArraySection()) {460if (auto *Stride = E->getStride())461D |= Stride->getDependence();462}463return D;464}465466ExprDependence clang::computeDependence(OMPArrayShapingExpr *E) {467auto D = E->getBase()->getDependence();468for (Expr *Dim: E->getDimensions())469if (Dim)470D |= turnValueToTypeDependence(Dim->getDependence());471return D;472}473474ExprDependence clang::computeDependence(OMPIteratorExpr *E) {475auto D = toExprDependenceForImpliedType(E->getType()->getDependence());476for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {477if (auto *DD = cast_or_null<DeclaratorDecl>(E->getIteratorDecl(I))) {478// If the type is omitted, it's 'int', and is not dependent in any way.479if (auto *TSI = DD->getTypeSourceInfo()) {480D |= toExprDependenceAsWritten(TSI->getType()->getDependence());481}482}483OMPIteratorExpr::IteratorRange IR = E->getIteratorRange(I);484if (Expr *BE = IR.Begin)485D |= BE->getDependence();486if (Expr *EE = IR.End)487D |= EE->getDependence();488if (Expr *SE = IR.Step)489D |= SE->getDependence();490}491return D;492}493494/// Compute the type-, value-, and instantiation-dependence of a495/// declaration reference496/// based on the declaration being referenced.497ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {498auto Deps = ExprDependence::None;499500if (auto *NNS = E->getQualifier())501Deps |= toExprDependence(NNS->getDependence() &502~NestedNameSpecifierDependence::Dependent);503504if (auto *FirstArg = E->getTemplateArgs()) {505unsigned NumArgs = E->getNumTemplateArgs();506for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg)507Deps |= toExprDependence(Arg->getArgument().getDependence());508}509510auto *Decl = E->getDecl();511auto Type = E->getType();512513if (Decl->isParameterPack())514Deps |= ExprDependence::UnexpandedPack;515Deps |= toExprDependenceForImpliedType(Type->getDependence()) &516ExprDependence::Error;517518// C++ [temp.dep.expr]p3:519// An id-expression is type-dependent if it contains:520521// - an identifier associated by name lookup with one or more declarations522// declared with a dependent type523// - an identifier associated by name lookup with an entity captured by524// copy ([expr.prim.lambda.capture])525// in a lambda-expression that has an explicit object parameter whose526// type is dependent ([dcl.fct]),527//528// [The "or more" case is not modeled as a DeclRefExpr. There are a bunch529// more bullets here that we handle by treating the declaration as having a530// dependent type if they involve a placeholder type that can't be deduced.]531if (Type->isDependentType())532Deps |= ExprDependence::TypeValueInstantiation;533else if (Type->isInstantiationDependentType())534Deps |= ExprDependence::Instantiation;535536// - an identifier associated by name lookup with an entity captured by537// copy ([expr.prim.lambda.capture])538if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())539Deps |= ExprDependence::Type;540541// - a conversion-function-id that specifies a dependent type542if (Decl->getDeclName().getNameKind() ==543DeclarationName::CXXConversionFunctionName) {544QualType T = Decl->getDeclName().getCXXNameType();545if (T->isDependentType())546return Deps | ExprDependence::TypeValueInstantiation;547548if (T->isInstantiationDependentType())549Deps |= ExprDependence::Instantiation;550}551552// - a template-id that is dependent,553// - a nested-name-specifier or a qualified-id that names a member of an554// unknown specialization555// [These are not modeled as DeclRefExprs.]556557// or if it names a dependent member of the current instantiation that is a558// static data member of type "array of unknown bound of T" for some T559// [handled below].560561// C++ [temp.dep.constexpr]p2:562// An id-expression is value-dependent if:563564// - it is type-dependent [handled above]565566// - it is the name of a non-type template parameter,567if (isa<NonTypeTemplateParmDecl>(Decl))568return Deps | ExprDependence::ValueInstantiation;569570// - it names a potentially-constant variable that is initialized with an571// expression that is value-dependent572if (const auto *Var = dyn_cast<VarDecl>(Decl)) {573if (const Expr *Init = Var->getAnyInitializer()) {574if (Init->containsErrors())575Deps |= ExprDependence::Error;576577if (Var->mightBeUsableInConstantExpressions(Ctx) &&578Init->isValueDependent())579Deps |= ExprDependence::ValueInstantiation;580}581582// - it names a static data member that is a dependent member of the583// current instantiation and is not initialized in a member-declarator,584if (Var->isStaticDataMember() &&585Var->getDeclContext()->isDependentContext() &&586!Var->getFirstDecl()->hasInit()) {587const VarDecl *First = Var->getFirstDecl();588TypeSourceInfo *TInfo = First->getTypeSourceInfo();589if (TInfo->getType()->isIncompleteArrayType()) {590Deps |= ExprDependence::TypeValueInstantiation;591} else if (!First->hasInit()) {592Deps |= ExprDependence::ValueInstantiation;593}594}595596return Deps;597}598599// - it names a static member function that is a dependent member of the600// current instantiation601//602// FIXME: It's unclear that the restriction to static members here has any603// effect: any use of a non-static member function name requires either604// forming a pointer-to-member or providing an object parameter, either of605// which makes the overall expression value-dependent.606if (auto *MD = dyn_cast<CXXMethodDecl>(Decl)) {607if (MD->isStatic() && Decl->getDeclContext()->isDependentContext())608Deps |= ExprDependence::ValueInstantiation;609}610611return Deps;612}613614ExprDependence clang::computeDependence(RecoveryExpr *E) {615// RecoveryExpr is616// - always value-dependent, and therefore instantiation dependent617// - contains errors (ExprDependence::Error), by definition618// - type-dependent if we don't know the type (fallback to an opaque619// dependent type), or the type is known and dependent, or it has620// type-dependent subexpressions.621auto D = toExprDependenceAsWritten(E->getType()->getDependence()) |622ExprDependence::ErrorDependent;623// FIXME: remove the type-dependent bit from subexpressions, if the624// RecoveryExpr has a non-dependent type.625for (auto *S : E->subExpressions())626D |= S->getDependence();627return D;628}629630ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) {631return toExprDependenceAsWritten(632E->getTypeSourceInfo()->getType()->getDependence());633}634635ExprDependence clang::computeDependence(PredefinedExpr *E) {636return toExprDependenceForImpliedType(E->getType()->getDependence());637}638639ExprDependence clang::computeDependence(CallExpr *E,640llvm::ArrayRef<Expr *> PreArgs) {641auto D = E->getCallee()->getDependence();642if (E->getType()->isDependentType())643D |= ExprDependence::Type;644for (auto *A : llvm::ArrayRef(E->getArgs(), E->getNumArgs())) {645if (A)646D |= A->getDependence();647}648for (auto *A : PreArgs)649D |= A->getDependence();650return D;651}652653ExprDependence clang::computeDependence(OffsetOfExpr *E) {654auto D = turnTypeToValueDependence(toExprDependenceAsWritten(655E->getTypeSourceInfo()->getType()->getDependence()));656for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I)657D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence());658return D;659}660661static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) {662auto D = ExprDependence::None;663if (Name.isInstantiationDependent())664D |= ExprDependence::Instantiation;665if (Name.containsUnexpandedParameterPack())666D |= ExprDependence::UnexpandedPack;667return D;668}669670ExprDependence clang::computeDependence(MemberExpr *E) {671auto D = E->getBase()->getDependence();672D |= getDependenceInExpr(E->getMemberNameInfo());673674if (auto *NNS = E->getQualifier())675D |= toExprDependence(NNS->getDependence() &676~NestedNameSpecifierDependence::Dependent);677678for (const auto &A : E->template_arguments())679D |= toExprDependence(A.getArgument().getDependence());680681auto *MemberDecl = E->getMemberDecl();682if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {683DeclContext *DC = MemberDecl->getDeclContext();684// dyn_cast_or_null is used to handle objC variables which do not685// have a declaration context.686CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);687if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) {688if (!E->getType()->isDependentType())689D &= ~ExprDependence::Type;690}691692// Bitfield with value-dependent width is type-dependent.693if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) {694D |= ExprDependence::Type;695}696}697return D;698}699700ExprDependence clang::computeDependence(InitListExpr *E) {701auto D = ExprDependence::None;702for (auto *A : E->inits())703D |= A->getDependence();704return D;705}706707ExprDependence clang::computeDependence(ShuffleVectorExpr *E) {708auto D = toExprDependenceForImpliedType(E->getType()->getDependence());709for (auto *C : llvm::ArrayRef(E->getSubExprs(), E->getNumSubExprs()))710D |= C->getDependence();711return D;712}713714ExprDependence clang::computeDependence(GenericSelectionExpr *E,715bool ContainsUnexpandedPack) {716auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack717: ExprDependence::None;718for (auto *AE : E->getAssocExprs())719D |= AE->getDependence() & ExprDependence::Error;720721if (E->isExprPredicate())722D |= E->getControllingExpr()->getDependence() & ExprDependence::Error;723else724D |= toExprDependenceAsWritten(725E->getControllingType()->getType()->getDependence());726727if (E->isResultDependent())728return D | ExprDependence::TypeValueInstantiation;729return D | (E->getResultExpr()->getDependence() &730~ExprDependence::UnexpandedPack);731}732733ExprDependence clang::computeDependence(DesignatedInitExpr *E) {734auto Deps = E->getInit()->getDependence();735for (const auto &D : E->designators()) {736auto DesignatorDeps = ExprDependence::None;737if (D.isArrayDesignator())738DesignatorDeps |= E->getArrayIndex(D)->getDependence();739else if (D.isArrayRangeDesignator())740DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() |741E->getArrayRangeEnd(D)->getDependence();742Deps |= DesignatorDeps;743if (DesignatorDeps & ExprDependence::TypeValue)744Deps |= ExprDependence::TypeValueInstantiation;745}746return Deps;747}748749ExprDependence clang::computeDependence(PseudoObjectExpr *O) {750auto D = O->getSyntacticForm()->getDependence();751for (auto *E : O->semantics())752D |= E->getDependence();753return D;754}755756ExprDependence clang::computeDependence(AtomicExpr *A) {757auto D = ExprDependence::None;758for (auto *E : llvm::ArrayRef(A->getSubExprs(), A->getNumSubExprs()))759D |= E->getDependence();760return D;761}762763ExprDependence clang::computeDependence(CXXNewExpr *E) {764auto D = toExprDependenceAsWritten(765E->getAllocatedTypeSourceInfo()->getType()->getDependence());766D |= toExprDependenceForImpliedType(E->getAllocatedType()->getDependence());767auto Size = E->getArraySize();768if (Size && *Size)769D |= turnTypeToValueDependence((*Size)->getDependence());770if (auto *I = E->getInitializer())771D |= turnTypeToValueDependence(I->getDependence());772for (auto *A : E->placement_arguments())773D |= turnTypeToValueDependence(A->getDependence());774return D;775}776777ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) {778auto D = E->getBase()->getDependence();779if (auto *TSI = E->getDestroyedTypeInfo())780D |= toExprDependenceAsWritten(TSI->getType()->getDependence());781if (auto *ST = E->getScopeTypeInfo())782D |= turnTypeToValueDependence(783toExprDependenceAsWritten(ST->getType()->getDependence()));784if (auto *Q = E->getQualifier())785D |= toExprDependence(Q->getDependence() &786~NestedNameSpecifierDependence::Dependent);787return D;788}789790ExprDependence791clang::computeDependence(OverloadExpr *E, bool KnownDependent,792bool KnownInstantiationDependent,793bool KnownContainsUnexpandedParameterPack) {794auto Deps = ExprDependence::None;795if (KnownDependent)796Deps |= ExprDependence::TypeValue;797if (KnownInstantiationDependent)798Deps |= ExprDependence::Instantiation;799if (KnownContainsUnexpandedParameterPack)800Deps |= ExprDependence::UnexpandedPack;801Deps |= getDependenceInExpr(E->getNameInfo());802if (auto *Q = E->getQualifier())803Deps |= toExprDependence(Q->getDependence() &804~NestedNameSpecifierDependence::Dependent);805for (auto *D : E->decls()) {806if (D->getDeclContext()->isDependentContext() ||807isa<UnresolvedUsingValueDecl>(D))808Deps |= ExprDependence::TypeValueInstantiation;809}810// If we have explicit template arguments, check for dependent811// template arguments and whether they contain any unexpanded pack812// expansions.813for (const auto &A : E->template_arguments())814Deps |= toExprDependence(A.getArgument().getDependence());815return Deps;816}817818ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) {819auto D = ExprDependence::TypeValue;820D |= getDependenceInExpr(E->getNameInfo());821if (auto *Q = E->getQualifier())822D |= toExprDependence(Q->getDependence());823for (const auto &A : E->template_arguments())824D |= toExprDependence(A.getArgument().getDependence());825return D;826}827828ExprDependence clang::computeDependence(CXXConstructExpr *E) {829ExprDependence D =830toExprDependenceForImpliedType(E->getType()->getDependence());831for (auto *A : E->arguments())832D |= A->getDependence() & ~ExprDependence::Type;833return D;834}835836ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) {837CXXConstructExpr *BaseE = E;838return toExprDependenceAsWritten(839E->getTypeSourceInfo()->getType()->getDependence()) |840computeDependence(BaseE);841}842843ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) {844return E->getExpr()->getDependence();845}846847ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) {848return E->getExpr()->getDependence();849}850851ExprDependence clang::computeDependence(LambdaExpr *E,852bool ContainsUnexpandedParameterPack) {853auto D = toExprDependenceForImpliedType(E->getType()->getDependence());854if (ContainsUnexpandedParameterPack)855D |= ExprDependence::UnexpandedPack;856return D;857}858859ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) {860auto D = ExprDependence::ValueInstantiation;861D |= toExprDependenceAsWritten(E->getTypeAsWritten()->getDependence());862D |= toExprDependenceForImpliedType(E->getType()->getDependence());863for (auto *A : E->arguments())864D |= A->getDependence() &865(ExprDependence::UnexpandedPack | ExprDependence::Error);866return D;867}868869ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) {870auto D = ExprDependence::TypeValueInstantiation;871if (!E->isImplicitAccess())872D |= E->getBase()->getDependence();873if (auto *Q = E->getQualifier())874D |= toExprDependence(Q->getDependence());875D |= getDependenceInExpr(E->getMemberNameInfo());876for (const auto &A : E->template_arguments())877D |= toExprDependence(A.getArgument().getDependence());878return D;879}880881ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) {882return E->getSubExpr()->getDependence();883}884885ExprDependence clang::computeDependence(CXXFoldExpr *E) {886auto D = ExprDependence::TypeValueInstantiation;887for (const auto *C : {E->getLHS(), E->getRHS()}) {888if (C)889D |= C->getDependence() & ~ExprDependence::UnexpandedPack;890}891return D;892}893894ExprDependence clang::computeDependence(CXXParenListInitExpr *E) {895auto D = ExprDependence::None;896for (const auto *A : E->getInitExprs())897D |= A->getDependence();898return D;899}900901ExprDependence clang::computeDependence(TypeTraitExpr *E) {902auto D = ExprDependence::None;903for (const auto *A : E->getArgs())904D |= toExprDependenceAsWritten(A->getType()->getDependence()) &905~ExprDependence::Type;906return D;907}908909ExprDependence clang::computeDependence(ConceptSpecializationExpr *E,910bool ValueDependent) {911auto TA = TemplateArgumentDependence::None;912const auto InterestingDeps = TemplateArgumentDependence::Instantiation |913TemplateArgumentDependence::UnexpandedPack;914for (const TemplateArgumentLoc &ArgLoc :915E->getTemplateArgsAsWritten()->arguments()) {916TA |= ArgLoc.getArgument().getDependence() & InterestingDeps;917if (TA == InterestingDeps)918break;919}920921ExprDependence D =922ValueDependent ? ExprDependence::Value : ExprDependence::None;923auto Res = D | toExprDependence(TA);924if(!ValueDependent && E->getSatisfaction().ContainsErrors)925Res |= ExprDependence::Error;926return Res;927}928929ExprDependence clang::computeDependence(ObjCArrayLiteral *E) {930auto D = ExprDependence::None;931Expr **Elements = E->getElements();932for (unsigned I = 0, N = E->getNumElements(); I != N; ++I)933D |= turnTypeToValueDependence(Elements[I]->getDependence());934return D;935}936937ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) {938auto Deps = ExprDependence::None;939for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) {940auto KV = E->getKeyValueElement(I);941auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() |942KV.Value->getDependence());943if (KV.EllipsisLoc.isValid())944KVDeps &= ~ExprDependence::UnexpandedPack;945Deps |= KVDeps;946}947return Deps;948}949950ExprDependence clang::computeDependence(ObjCMessageExpr *E) {951auto D = ExprDependence::None;952if (auto *R = E->getInstanceReceiver())953D |= R->getDependence();954else955D |= toExprDependenceForImpliedType(E->getType()->getDependence());956for (auto *A : E->arguments())957D |= A->getDependence();958return D;959}960961962