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 22 using namespace clang; 23 24 ExprDependence clang::computeDependence(FullExpr *E) { 25 return E->getSubExpr()->getDependence(); 26 } 27 28 ExprDependence 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 36 ExprDependence clang::computeDependence(ParenExpr *E) { 37 return E->getSubExpr()->getDependence(); 38 } 39 40 ExprDependence clang::computeDependence(UnaryOperator *E, 41 const ASTContext &Ctx) { 42 ExprDependence Dep = 43 // FIXME: Do we need to look at the type? 44 toExprDependenceForImpliedType(E->getType()->getDependence()) | 45 E->getSubExpr()->getDependence(); 46 47 // C++ [temp.dep.constexpr]p5: 48 // An expression of the form & qualified-id where the qualified-id names a 49 // dependent member of the current instantiation is value-dependent. An 50 // expression of the form & cast-expression is also value-dependent if 51 // evaluating cast-expression as a core constant expression succeeds and 52 // the result of the evaluation refers to a templated entity that is an 53 // object with static or thread storage duration or a member function. 54 // 55 // What this amounts to is: constant-evaluate the operand and check whether it 56 // refers to a templated entity other than a variable with local storage. 57 if (Ctx.getLangOpts().CPlusPlus && E->getOpcode() == UO_AddrOf && 58 !(Dep & ExprDependence::Value)) { 59 Expr::EvalResult Result; 60 SmallVector<PartialDiagnosticAt, 8> Diag; 61 Result.Diag = &Diag; 62 // FIXME: This doesn't enforce the C++98 constant expression rules. 63 if (E->getSubExpr()->EvaluateAsConstantExpr(Result, Ctx) && Diag.empty() && 64 Result.Val.isLValue()) { 65 auto *VD = Result.Val.getLValueBase().dyn_cast<const ValueDecl *>(); 66 if (VD && VD->isTemplated()) { 67 auto *VarD = dyn_cast<VarDecl>(VD); 68 if (!VarD || !VarD->hasLocalStorage()) 69 Dep |= ExprDependence::Value; 70 } 71 } 72 } 73 74 return Dep; 75 } 76 77 ExprDependence 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 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>(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 114 ExprDependence clang::computeDependence(ArraySubscriptExpr *E) { 115 return E->getLHS()->getDependence() | E->getRHS()->getDependence(); 116 } 117 118 ExprDependence clang::computeDependence(MatrixSubscriptExpr *E) { 119 return E->getBase()->getDependence() | E->getRowIdx()->getDependence() | 120 (E->getColumnIdx() ? E->getColumnIdx()->getDependence() 121 : ExprDependence::None); 122 } 123 124 ExprDependence clang::computeDependence(CompoundLiteralExpr *E) { 125 return toExprDependenceAsWritten( 126 E->getTypeSourceInfo()->getType()->getDependence()) | 127 toExprDependenceForImpliedType(E->getType()->getDependence()) | 128 turnTypeToValueDependence(E->getInitializer()->getDependence()); 129 } 130 131 ExprDependence clang::computeDependence(ImplicitCastExpr *E) { 132 // We model implicit conversions as combining the dependence of their 133 // subexpression, apart from its type, with the semantic portion of the 134 // target type. 135 ExprDependence D = 136 toExprDependenceForImpliedType(E->getType()->getDependence()); 137 if (auto *S = E->getSubExpr()) 138 D |= S->getDependence() & ~ExprDependence::Type; 139 return D; 140 } 141 142 ExprDependence clang::computeDependence(ExplicitCastExpr *E) { 143 // Cast expressions are type-dependent if the type is 144 // dependent (C++ [temp.dep.expr]p3). 145 // Cast expressions are value-dependent if the type is 146 // dependent or if the subexpression is value-dependent. 147 // 148 // Note that we also need to consider the dependence of the actual type here, 149 // because when the type as written is a deduced type, that type is not 150 // dependent, but it may be deduced as a dependent type. 151 ExprDependence D = 152 toExprDependenceAsWritten( 153 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 160 ExprDependence clang::computeDependence(BinaryOperator *E) { 161 return E->getLHS()->getDependence() | E->getRHS()->getDependence(); 162 } 163 164 ExprDependence clang::computeDependence(ConditionalOperator *E) { 165 // The type of the conditional operator depends on the type of the conditional 166 // to support the GCC vector conditional extension. Additionally, 167 // [temp.dep.expr] does specify that this should be dependent on ALL sub 168 // expressions. 169 return E->getCond()->getDependence() | E->getLHS()->getDependence() | 170 E->getRHS()->getDependence(); 171 } 172 173 ExprDependence clang::computeDependence(BinaryConditionalOperator *E) { 174 return E->getCommon()->getDependence() | E->getFalseExpr()->getDependence(); 175 } 176 177 ExprDependence 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()->getStmtExprResult())) 182 if (const Expr *ResultExpr = CompoundExprResult->getExprStmt()) 183 D |= ResultExpr->getDependence(); 184 // Note: we treat a statement-expression in a dependent context as always 185 // being value- and instantiation-dependent. This matches the behavior of 186 // lambda-expressions and GCC. 187 if (TemplateDepth) 188 D |= ExprDependence::ValueInstantiation; 189 // A param pack cannot be expanded over stmtexpr boundaries. 190 return D & ~ExprDependence::UnexpandedPack; 191 } 192 193 ExprDependence 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 202 ExprDependence 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 all 214 // other flags from all branches. 215 return (Active & ExprDependence::TypeValue) | 216 ((Cond | Active | Inactive) & ~ExprDependence::TypeValue); 217 } 218 219 ExprDependence clang::computeDependence(ParenListExpr *P) { 220 auto D = ExprDependence::None; 221 for (auto *E : P->exprs()) 222 D |= E->getDependence(); 223 return D; 224 } 225 226 ExprDependence clang::computeDependence(VAArgExpr *E) { 227 auto D = toExprDependenceAsWritten( 228 E->getWrittenTypeInfo()->getType()->getDependence()) | 229 (E->getSubExpr()->getDependence() & ~ExprDependence::Type); 230 return D; 231 } 232 233 ExprDependence clang::computeDependence(NoInitExpr *E) { 234 return toExprDependenceForImpliedType(E->getType()->getDependence()) & 235 (ExprDependence::Instantiation | ExprDependence::Error); 236 } 237 238 ExprDependence 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 246 ExprDependence clang::computeDependence(ImplicitValueInitExpr *E) { 247 return toExprDependenceForImpliedType(E->getType()->getDependence()) & 248 ExprDependence::Instantiation; 249 } 250 251 ExprDependence clang::computeDependence(ExtVectorElementExpr *E) { 252 return E->getBase()->getDependence(); 253 } 254 255 ExprDependence 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 265 ExprDependence clang::computeDependence(AsTypeExpr *E) { 266 // FIXME: AsTypeExpr doesn't store the type as written. Assume the expression 267 // 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 275 ExprDependence clang::computeDependence(CXXRewrittenBinaryOperator *E) { 276 return E->getSemanticForm()->getDependence(); 277 } 278 279 ExprDependence clang::computeDependence(CXXStdInitializerListExpr *E) { 280 auto D = turnTypeToValueDependence(E->getSubExpr()->getDependence()); 281 D |= toExprDependenceForImpliedType(E->getType()->getDependence()); 282 return D; 283 } 284 285 ExprDependence clang::computeDependence(CXXTypeidExpr *E) { 286 auto D = ExprDependence::None; 287 if (E->isTypeOperand()) 288 D = toExprDependenceAsWritten( 289 E->getTypeOperandSourceInfo()->getType()->getDependence()); 290 else 291 D = turnTypeToValueDependence(E->getExprOperand()->getDependence()); 292 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 293 return D & ~ExprDependence::Type; 294 } 295 296 ExprDependence clang::computeDependence(MSPropertyRefExpr *E) { 297 return E->getBaseExpr()->getDependence() & ~ExprDependence::Type; 298 } 299 300 ExprDependence clang::computeDependence(MSPropertySubscriptExpr *E) { 301 return E->getIdx()->getDependence(); 302 } 303 304 ExprDependence 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 312 ExprDependence clang::computeDependence(CXXThisExpr *E) { 313 // 'this' is type-dependent if the class type of the enclosing 314 // 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', then 318 // 'this' now refers to the captured copy of lambda, and if the lambda 319 // is type-dependent, so is the object and thus 'this'. 320 // 321 // Note: The standard does not mention this case explicitly, but we need 322 // 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 330 ExprDependence clang::computeDependence(CXXThrowExpr *E) { 331 auto *Op = E->getSubExpr(); 332 if (!Op) 333 return ExprDependence::None; 334 return Op->getDependence() & ~ExprDependence::TypeValue; 335 } 336 337 ExprDependence clang::computeDependence(CXXBindTemporaryExpr *E) { 338 return E->getSubExpr()->getDependence(); 339 } 340 341 ExprDependence 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 348 ExprDependence clang::computeDependence(CXXDeleteExpr *E) { 349 return turnTypeToValueDependence(E->getArgument()->getDependence()); 350 } 351 352 ExprDependence 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 359 ExprDependence 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 368 ExprDependence 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 375 ExprDependence clang::computeDependence(PackExpansionExpr *E) { 376 return (E->getPattern()->getDependence() & ~ExprDependence::UnexpandedPack) | 377 ExprDependence::TypeValueInstantiation; 378 } 379 380 ExprDependence 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 std::optional<unsigned> 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 401 ExprDependence clang::computeDependence(SubstNonTypeTemplateParmExpr *E) { 402 return E->getReplacement()->getDependence(); 403 } 404 405 ExprDependence 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 414 ExprDependence clang::computeDependence(DependentCoawaitExpr *E) { 415 return E->getOperand()->getDependence() | 416 ExprDependence::TypeValueInstantiation; 417 } 418 419 ExprDependence clang::computeDependence(ObjCBoxedExpr *E) { 420 return E->getSubExpr()->getDependence(); 421 } 422 423 ExprDependence clang::computeDependence(ObjCEncodeExpr *E) { 424 return toExprDependenceAsWritten(E->getEncodedType()->getDependence()); 425 } 426 427 ExprDependence clang::computeDependence(ObjCIvarRefExpr *E) { 428 return turnTypeToValueDependence(E->getBase()->getDependence()); 429 } 430 431 ExprDependence 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 442 ExprDependence clang::computeDependence(ObjCSubscriptRefExpr *E) { 443 return E->getBaseExpr()->getDependence() | E->getKeyExpr()->getDependence(); 444 } 445 446 ExprDependence clang::computeDependence(ObjCIsaExpr *E) { 447 return E->getBase()->getDependence() & ~ExprDependence::Type & 448 ~ExprDependence::UnexpandedPack; 449 } 450 451 ExprDependence clang::computeDependence(ObjCIndirectCopyRestoreExpr *E) { 452 return E->getSubExpr()->getDependence(); 453 } 454 455 ExprDependence 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 469 ExprDependence 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 477 ExprDependence 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 a 498 /// declaration reference 499 /// based on the declaration being referenced. 500 ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) { 501 auto Deps = ExprDependence::None; 502 503 if (auto *NNS = E->getQualifier()) 504 Deps |= toExprDependence(NNS->getDependence() & 505 ~NestedNameSpecifierDependence::Dependent); 506 507 if (auto *FirstArg = E->getTemplateArgs()) { 508 unsigned NumArgs = E->getNumTemplateArgs(); 509 for (auto *Arg = FirstArg, *End = FirstArg + NumArgs; Arg < End; ++Arg) 510 Deps |= toExprDependence(Arg->getArgument().getDependence()); 511 } 512 513 auto *Decl = E->getDecl(); 514 auto Type = E->getType(); 515 516 if (Decl->isParameterPack()) 517 Deps |= ExprDependence::UnexpandedPack; 518 Deps |= toExprDependenceForImpliedType(Type->getDependence()) & 519 ExprDependence::Error; 520 521 // C++ [temp.dep.expr]p3: 522 // An id-expression is type-dependent if it contains: 523 524 // - an identifier associated by name lookup with one or more declarations 525 // declared with a dependent type 526 // - an identifier associated by name lookup with an entity captured by 527 // copy ([expr.prim.lambda.capture]) 528 // in a lambda-expression that has an explicit object parameter whose 529 // type is dependent ([dcl.fct]), 530 // 531 // [The "or more" case is not modeled as a DeclRefExpr. There are a bunch 532 // more bullets here that we handle by treating the declaration as having a 533 // dependent type if they involve a placeholder type that can't be deduced.] 534 if (Type->isDependentType()) 535 Deps |= ExprDependence::TypeValueInstantiation; 536 else if (Type->isInstantiationDependentType()) 537 Deps |= ExprDependence::Instantiation; 538 539 // - an identifier associated by name lookup with an entity captured by 540 // copy ([expr.prim.lambda.capture]) 541 if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter()) 542 Deps |= ExprDependence::Type; 543 544 // - a conversion-function-id that specifies a dependent type 545 if (Decl->getDeclName().getNameKind() == 546 DeclarationName::CXXConversionFunctionName) { 547 QualType T = Decl->getDeclName().getCXXNameType(); 548 if (T->isDependentType()) 549 return Deps | ExprDependence::TypeValueInstantiation; 550 551 if (T->isInstantiationDependentType()) 552 Deps |= ExprDependence::Instantiation; 553 } 554 555 // - a template-id that is dependent, 556 // - a nested-name-specifier or a qualified-id that names a member of an 557 // unknown specialization 558 // [These are not modeled as DeclRefExprs.] 559 560 // or if it names a dependent member of the current instantiation that is a 561 // static data member of type "array of unknown bound of T" for some T 562 // [handled below]. 563 564 // C++ [temp.dep.constexpr]p2: 565 // An id-expression is value-dependent if: 566 567 // - it is type-dependent [handled above] 568 569 // - it is the name of a non-type template parameter, 570 if (isa<NonTypeTemplateParmDecl>(Decl)) 571 return Deps | ExprDependence::ValueInstantiation; 572 573 // - it names a potentially-constant variable that is initialized with an 574 // expression that is value-dependent 575 if (const auto *Var = dyn_cast<VarDecl>(Decl)) { 576 if (const Expr *Init = Var->getAnyInitializer()) { 577 if (Init->containsErrors()) 578 Deps |= ExprDependence::Error; 579 580 if (Var->mightBeUsableInConstantExpressions(Ctx) && 581 Init->isValueDependent()) 582 Deps |= ExprDependence::ValueInstantiation; 583 } 584 585 // - it names a static data member that is a dependent member of the 586 // current instantiation and is not initialized in a member-declarator, 587 if (Var->isStaticDataMember() && 588 Var->getDeclContext()->isDependentContext() && 589 !Var->getFirstDecl()->hasInit()) { 590 const VarDecl *First = Var->getFirstDecl(); 591 TypeSourceInfo *TInfo = First->getTypeSourceInfo(); 592 if (TInfo->getType()->isIncompleteArrayType()) { 593 Deps |= ExprDependence::TypeValueInstantiation; 594 } else if (!First->hasInit()) { 595 Deps |= ExprDependence::ValueInstantiation; 596 } 597 } 598 599 return Deps; 600 } 601 602 // - it names a static member function that is a dependent member of the 603 // current instantiation 604 // 605 // FIXME: It's unclear that the restriction to static members here has any 606 // effect: any use of a non-static member function name requires either 607 // forming a pointer-to-member or providing an object parameter, either of 608 // which makes the overall expression value-dependent. 609 if (auto *MD = dyn_cast<CXXMethodDecl>(Decl)) { 610 if (MD->isStatic() && Decl->getDeclContext()->isDependentContext()) 611 Deps |= ExprDependence::ValueInstantiation; 612 } 613 614 return Deps; 615 } 616 617 ExprDependence clang::computeDependence(RecoveryExpr *E) { 618 // RecoveryExpr is 619 // - always value-dependent, and therefore instantiation dependent 620 // - contains errors (ExprDependence::Error), by definition 621 // - type-dependent if we don't know the type (fallback to an opaque 622 // dependent type), or the type is known and dependent, or it has 623 // type-dependent subexpressions. 624 auto D = toExprDependenceAsWritten(E->getType()->getDependence()) | 625 ExprDependence::ErrorDependent; 626 // FIXME: remove the type-dependent bit from subexpressions, if the 627 // RecoveryExpr has a non-dependent type. 628 for (auto *S : E->subExpressions()) 629 D |= S->getDependence(); 630 return D; 631 } 632 633 ExprDependence clang::computeDependence(SYCLUniqueStableNameExpr *E) { 634 return toExprDependenceAsWritten( 635 E->getTypeSourceInfo()->getType()->getDependence()); 636 } 637 638 ExprDependence clang::computeDependence(PredefinedExpr *E) { 639 return toExprDependenceForImpliedType(E->getType()->getDependence()); 640 } 641 642 ExprDependence clang::computeDependence(CallExpr *E, 643 llvm::ArrayRef<Expr *> PreArgs) { 644 auto D = E->getCallee()->getDependence(); 645 if (E->getType()->isDependentType()) 646 D |= ExprDependence::Type; 647 for (auto *A : llvm::ArrayRef(E->getArgs(), E->getNumArgs())) { 648 if (A) 649 D |= A->getDependence(); 650 } 651 for (auto *A : PreArgs) 652 D |= A->getDependence(); 653 return D; 654 } 655 656 ExprDependence clang::computeDependence(OffsetOfExpr *E) { 657 auto D = turnTypeToValueDependence(toExprDependenceAsWritten( 658 E->getTypeSourceInfo()->getType()->getDependence())); 659 for (unsigned I = 0, N = E->getNumExpressions(); I < N; ++I) 660 D |= turnTypeToValueDependence(E->getIndexExpr(I)->getDependence()); 661 return D; 662 } 663 664 static inline ExprDependence getDependenceInExpr(DeclarationNameInfo Name) { 665 auto D = ExprDependence::None; 666 if (Name.isInstantiationDependent()) 667 D |= ExprDependence::Instantiation; 668 if (Name.containsUnexpandedParameterPack()) 669 D |= ExprDependence::UnexpandedPack; 670 return D; 671 } 672 673 ExprDependence clang::computeDependence(MemberExpr *E) { 674 auto D = E->getBase()->getDependence(); 675 D |= getDependenceInExpr(E->getMemberNameInfo()); 676 677 if (auto *NNS = E->getQualifier()) 678 D |= toExprDependence(NNS->getDependence() & 679 ~NestedNameSpecifierDependence::Dependent); 680 681 for (const auto &A : E->template_arguments()) 682 D |= toExprDependence(A.getArgument().getDependence()); 683 684 auto *MemberDecl = E->getMemberDecl(); 685 if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { 686 DeclContext *DC = MemberDecl->getDeclContext(); 687 // dyn_cast_or_null is used to handle objC variables which do not 688 // have a declaration context. 689 CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC); 690 if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC)) { 691 if (!E->getType()->isDependentType()) 692 D &= ~ExprDependence::Type; 693 } 694 695 // Bitfield with value-dependent width is type-dependent. 696 if (FD && FD->isBitField() && FD->getBitWidth()->isValueDependent()) { 697 D |= ExprDependence::Type; 698 } 699 } 700 return D; 701 } 702 703 ExprDependence clang::computeDependence(InitListExpr *E) { 704 auto D = ExprDependence::None; 705 for (auto *A : E->inits()) 706 D |= A->getDependence(); 707 return D; 708 } 709 710 ExprDependence clang::computeDependence(ShuffleVectorExpr *E) { 711 auto D = toExprDependenceForImpliedType(E->getType()->getDependence()); 712 for (auto *C : llvm::ArrayRef(E->getSubExprs(), E->getNumSubExprs())) 713 D |= C->getDependence(); 714 return D; 715 } 716 717 ExprDependence clang::computeDependence(GenericSelectionExpr *E, 718 bool ContainsUnexpandedPack) { 719 auto D = ContainsUnexpandedPack ? ExprDependence::UnexpandedPack 720 : ExprDependence::None; 721 for (auto *AE : E->getAssocExprs()) 722 D |= AE->getDependence() & ExprDependence::Error; 723 724 if (E->isExprPredicate()) 725 D |= E->getControllingExpr()->getDependence() & ExprDependence::Error; 726 else 727 D |= toExprDependenceAsWritten( 728 E->getControllingType()->getType()->getDependence()); 729 730 if (E->isResultDependent()) 731 return D | ExprDependence::TypeValueInstantiation; 732 return D | (E->getResultExpr()->getDependence() & 733 ~ExprDependence::UnexpandedPack); 734 } 735 736 ExprDependence clang::computeDependence(DesignatedInitExpr *E) { 737 auto Deps = E->getInit()->getDependence(); 738 for (const auto &D : E->designators()) { 739 auto DesignatorDeps = ExprDependence::None; 740 if (D.isArrayDesignator()) 741 DesignatorDeps |= E->getArrayIndex(D)->getDependence(); 742 else if (D.isArrayRangeDesignator()) 743 DesignatorDeps |= E->getArrayRangeStart(D)->getDependence() | 744 E->getArrayRangeEnd(D)->getDependence(); 745 Deps |= DesignatorDeps; 746 if (DesignatorDeps & ExprDependence::TypeValue) 747 Deps |= ExprDependence::TypeValueInstantiation; 748 } 749 return Deps; 750 } 751 752 ExprDependence clang::computeDependence(PseudoObjectExpr *O) { 753 auto D = O->getSyntacticForm()->getDependence(); 754 for (auto *E : O->semantics()) 755 D |= E->getDependence(); 756 return D; 757 } 758 759 ExprDependence clang::computeDependence(AtomicExpr *A) { 760 auto D = ExprDependence::None; 761 for (auto *E : llvm::ArrayRef(A->getSubExprs(), A->getNumSubExprs())) 762 D |= E->getDependence(); 763 return D; 764 } 765 766 ExprDependence clang::computeDependence(CXXNewExpr *E) { 767 auto D = toExprDependenceAsWritten( 768 E->getAllocatedTypeSourceInfo()->getType()->getDependence()); 769 D |= toExprDependenceForImpliedType(E->getAllocatedType()->getDependence()); 770 auto Size = E->getArraySize(); 771 if (Size && *Size) 772 D |= turnTypeToValueDependence((*Size)->getDependence()); 773 if (auto *I = E->getInitializer()) 774 D |= turnTypeToValueDependence(I->getDependence()); 775 for (auto *A : E->placement_arguments()) 776 D |= turnTypeToValueDependence(A->getDependence()); 777 return D; 778 } 779 780 ExprDependence clang::computeDependence(CXXPseudoDestructorExpr *E) { 781 auto D = E->getBase()->getDependence(); 782 if (auto *TSI = E->getDestroyedTypeInfo()) 783 D |= toExprDependenceAsWritten(TSI->getType()->getDependence()); 784 if (auto *ST = E->getScopeTypeInfo()) 785 D |= turnTypeToValueDependence( 786 toExprDependenceAsWritten(ST->getType()->getDependence())); 787 if (auto *Q = E->getQualifier()) 788 D |= toExprDependence(Q->getDependence() & 789 ~NestedNameSpecifierDependence::Dependent); 790 return D; 791 } 792 793 ExprDependence 794 clang::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(E->getNameInfo()); 805 if (auto *Q = E->getQualifier()) 806 Deps |= toExprDependence(Q->getDependence() & 807 ~NestedNameSpecifierDependence::Dependent); 808 for (auto *D : E->decls()) { 809 if (D->getDeclContext()->isDependentContext() || 810 isa<UnresolvedUsingValueDecl>(D)) 811 Deps |= ExprDependence::TypeValueInstantiation; 812 } 813 // If we have explicit template arguments, check for dependent 814 // template arguments and whether they contain any unexpanded pack 815 // expansions. 816 for (const auto &A : E->template_arguments()) 817 Deps |= toExprDependence(A.getArgument().getDependence()); 818 return Deps; 819 } 820 821 ExprDependence clang::computeDependence(DependentScopeDeclRefExpr *E) { 822 auto D = ExprDependence::TypeValue; 823 D |= getDependenceInExpr(E->getNameInfo()); 824 if (auto *Q = E->getQualifier()) 825 D |= toExprDependence(Q->getDependence()); 826 for (const auto &A : E->template_arguments()) 827 D |= toExprDependence(A.getArgument().getDependence()); 828 return D; 829 } 830 831 ExprDependence clang::computeDependence(CXXConstructExpr *E) { 832 ExprDependence D = 833 toExprDependenceForImpliedType(E->getType()->getDependence()); 834 for (auto *A : E->arguments()) 835 D |= A->getDependence() & ~ExprDependence::Type; 836 return D; 837 } 838 839 ExprDependence clang::computeDependence(CXXTemporaryObjectExpr *E) { 840 CXXConstructExpr *BaseE = E; 841 return toExprDependenceAsWritten( 842 E->getTypeSourceInfo()->getType()->getDependence()) | 843 computeDependence(BaseE); 844 } 845 846 ExprDependence clang::computeDependence(CXXDefaultInitExpr *E) { 847 return E->getExpr()->getDependence(); 848 } 849 850 ExprDependence clang::computeDependence(CXXDefaultArgExpr *E) { 851 return E->getExpr()->getDependence(); 852 } 853 854 ExprDependence clang::computeDependence(LambdaExpr *E, 855 bool ContainsUnexpandedParameterPack) { 856 auto D = toExprDependenceForImpliedType(E->getType()->getDependence()); 857 if (ContainsUnexpandedParameterPack) 858 D |= ExprDependence::UnexpandedPack; 859 return D; 860 } 861 862 ExprDependence clang::computeDependence(CXXUnresolvedConstructExpr *E) { 863 auto D = ExprDependence::ValueInstantiation; 864 D |= toExprDependenceAsWritten(E->getTypeAsWritten()->getDependence()); 865 D |= toExprDependenceForImpliedType(E->getType()->getDependence()); 866 for (auto *A : E->arguments()) 867 D |= A->getDependence() & 868 (ExprDependence::UnexpandedPack | ExprDependence::Error); 869 return D; 870 } 871 872 ExprDependence clang::computeDependence(CXXDependentScopeMemberExpr *E) { 873 auto D = ExprDependence::TypeValueInstantiation; 874 if (!E->isImplicitAccess()) 875 D |= E->getBase()->getDependence(); 876 if (auto *Q = E->getQualifier()) 877 D |= toExprDependence(Q->getDependence()); 878 D |= getDependenceInExpr(E->getMemberNameInfo()); 879 for (const auto &A : E->template_arguments()) 880 D |= toExprDependence(A.getArgument().getDependence()); 881 return D; 882 } 883 884 ExprDependence clang::computeDependence(MaterializeTemporaryExpr *E) { 885 return E->getSubExpr()->getDependence(); 886 } 887 888 ExprDependence clang::computeDependence(CXXFoldExpr *E) { 889 auto D = ExprDependence::TypeValueInstantiation; 890 for (const auto *C : {E->getLHS(), E->getRHS()}) { 891 if (C) 892 D |= C->getDependence() & ~ExprDependence::UnexpandedPack; 893 } 894 return D; 895 } 896 897 ExprDependence clang::computeDependence(CXXParenListInitExpr *E) { 898 auto D = ExprDependence::None; 899 for (const auto *A : E->getInitExprs()) 900 D |= A->getDependence(); 901 return D; 902 } 903 904 ExprDependence clang::computeDependence(TypeTraitExpr *E) { 905 auto D = ExprDependence::None; 906 for (const auto *A : E->getArgs()) 907 D |= toExprDependenceAsWritten(A->getType()->getDependence()) & 908 ~ExprDependence::Type; 909 return D; 910 } 911 912 ExprDependence clang::computeDependence(ConceptSpecializationExpr *E, 913 bool ValueDependent) { 914 auto TA = TemplateArgumentDependence::None; 915 const auto InterestingDeps = TemplateArgumentDependence::Instantiation | 916 TemplateArgumentDependence::UnexpandedPack; 917 for (const TemplateArgumentLoc &ArgLoc : 918 E->getTemplateArgsAsWritten()->arguments()) { 919 TA |= ArgLoc.getArgument().getDependence() & InterestingDeps; 920 if (TA == InterestingDeps) 921 break; 922 } 923 924 ExprDependence D = 925 ValueDependent ? ExprDependence::Value : ExprDependence::None; 926 auto Res = D | toExprDependence(TA); 927 if(!ValueDependent && E->getSatisfaction().ContainsErrors) 928 Res |= ExprDependence::Error; 929 return Res; 930 } 931 932 ExprDependence clang::computeDependence(ObjCArrayLiteral *E) { 933 auto D = ExprDependence::None; 934 Expr **Elements = E->getElements(); 935 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) 936 D |= turnTypeToValueDependence(Elements[I]->getDependence()); 937 return D; 938 } 939 940 ExprDependence clang::computeDependence(ObjCDictionaryLiteral *E) { 941 auto Deps = ExprDependence::None; 942 for (unsigned I = 0, N = E->getNumElements(); I < N; ++I) { 943 auto KV = E->getKeyValueElement(I); 944 auto KVDeps = turnTypeToValueDependence(KV.Key->getDependence() | 945 KV.Value->getDependence()); 946 if (KV.EllipsisLoc.isValid()) 947 KVDeps &= ~ExprDependence::UnexpandedPack; 948 Deps |= KVDeps; 949 } 950 return Deps; 951 } 952 953 ExprDependence clang::computeDependence(ObjCMessageExpr *E) { 954 auto D = ExprDependence::None; 955 if (auto *R = E->getInstanceReceiver()) 956 D |= R->getDependence(); 957 else 958 D |= toExprDependenceForImpliedType(E->getType()->getDependence()); 959 for (auto *A : E->arguments()) 960 D |= A->getDependence(); 961 return D; 962 } 963 964 ExprDependence clang::computeDependence(OpenACCAsteriskSizeExpr *E) { 965 // This represents a simple asterisk as typed, so cannot be dependent in any 966 // way. 967 return ExprDependence::None; 968 } 969