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