1 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the subclesses of Expr class declared in ExprCXX.h 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Basic/IdentifierTable.h" 15 #include "clang/AST/DeclCXX.h" 16 #include "clang/AST/DeclTemplate.h" 17 #include "clang/AST/ExprCXX.h" 18 #include "clang/AST/TypeLoc.h" 19 using namespace clang; 20 21 22 //===----------------------------------------------------------------------===// 23 // Child Iterators for iterating over subexpressions/substatements 24 //===----------------------------------------------------------------------===// 25 26 QualType CXXTypeidExpr::getTypeOperand() const { 27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType() 29 .getUnqualifiedType(); 30 } 31 32 QualType CXXUuidofExpr::getTypeOperand() const { 33 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 34 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType() 35 .getUnqualifiedType(); 36 } 37 38 // CXXScalarValueInitExpr 39 SourceRange CXXScalarValueInitExpr::getSourceRange() const { 40 SourceLocation Start = RParenLoc; 41 if (TypeInfo) 42 Start = TypeInfo->getTypeLoc().getBeginLoc(); 43 return SourceRange(Start, RParenLoc); 44 } 45 46 // CXXNewExpr 47 CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 48 FunctionDecl *operatorDelete, 49 bool usualArrayDeleteWantsSize, 50 Expr **placementArgs, unsigned numPlaceArgs, 51 SourceRange typeIdParens, Expr *arraySize, 52 InitializationStyle initializationStyle, 53 Expr *initializer, QualType ty, 54 TypeSourceInfo *allocatedTypeInfo, 55 SourceLocation startLoc, SourceRange directInitRange) 56 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, 57 ty->isDependentType(), ty->isDependentType(), 58 ty->isInstantiationDependentType(), 59 ty->containsUnexpandedParameterPack()), 60 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize), 61 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete), 62 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens), 63 StartLoc(startLoc), DirectInitRange(directInitRange) { 64 assert((initializer != 0 || initializationStyle == NoInit) && 65 "Only NoInit can have no initializer."); 66 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0; 67 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0); 68 unsigned i = 0; 69 if (Array) { 70 if (arraySize->isInstantiationDependent()) 71 ExprBits.InstantiationDependent = true; 72 73 if (arraySize->containsUnexpandedParameterPack()) 74 ExprBits.ContainsUnexpandedParameterPack = true; 75 76 SubExprs[i++] = arraySize; 77 } 78 79 if (initializer) { 80 if (initializer->isInstantiationDependent()) 81 ExprBits.InstantiationDependent = true; 82 83 if (initializer->containsUnexpandedParameterPack()) 84 ExprBits.ContainsUnexpandedParameterPack = true; 85 86 SubExprs[i++] = initializer; 87 } 88 89 for (unsigned j = 0; j < NumPlacementArgs; ++j) { 90 if (placementArgs[j]->isInstantiationDependent()) 91 ExprBits.InstantiationDependent = true; 92 if (placementArgs[j]->containsUnexpandedParameterPack()) 93 ExprBits.ContainsUnexpandedParameterPack = true; 94 95 SubExprs[i++] = placementArgs[j]; 96 } 97 } 98 99 void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray, 100 unsigned numPlaceArgs, bool hasInitializer){ 101 assert(SubExprs == 0 && "SubExprs already allocated"); 102 Array = isArray; 103 NumPlacementArgs = numPlaceArgs; 104 105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs; 106 SubExprs = new (C) Stmt*[TotalSize]; 107 } 108 109 bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const { 110 return getOperatorNew()->getType()-> 111 castAs<FunctionProtoType>()->isNothrow(Ctx); 112 } 113 114 SourceLocation CXXNewExpr::getEndLoc() const { 115 switch (getInitializationStyle()) { 116 case NoInit: 117 return AllocatedTypeInfo->getTypeLoc().getEndLoc(); 118 case CallInit: 119 return DirectInitRange.getEnd(); 120 case ListInit: 121 return getInitializer()->getSourceRange().getEnd(); 122 } 123 llvm_unreachable("bogus initialization style"); 124 } 125 126 // CXXDeleteExpr 127 QualType CXXDeleteExpr::getDestroyedType() const { 128 const Expr *Arg = getArgument(); 129 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { 130 if (ICE->getCastKind() != CK_UserDefinedConversion && 131 ICE->getType()->isVoidPointerType()) 132 Arg = ICE->getSubExpr(); 133 else 134 break; 135 } 136 // The type-to-delete may not be a pointer if it's a dependent type. 137 const QualType ArgType = Arg->getType(); 138 139 if (ArgType->isDependentType() && !ArgType->isPointerType()) 140 return QualType(); 141 142 return ArgType->getAs<PointerType>()->getPointeeType(); 143 } 144 145 // CXXPseudoDestructorExpr 146 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info) 147 : Type(Info) 148 { 149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin(); 150 } 151 152 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context, 153 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 154 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType, 155 SourceLocation ColonColonLoc, SourceLocation TildeLoc, 156 PseudoDestructorTypeStorage DestroyedType) 157 : Expr(CXXPseudoDestructorExprClass, 158 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0, 159 FunctionProtoType::ExtProtoInfo())), 160 VK_RValue, OK_Ordinary, 161 /*isTypeDependent=*/(Base->isTypeDependent() || 162 (DestroyedType.getTypeSourceInfo() && 163 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())), 164 /*isValueDependent=*/Base->isValueDependent(), 165 (Base->isInstantiationDependent() || 166 (QualifierLoc && 167 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) || 168 (ScopeType && 169 ScopeType->getType()->isInstantiationDependentType()) || 170 (DestroyedType.getTypeSourceInfo() && 171 DestroyedType.getTypeSourceInfo()->getType() 172 ->isInstantiationDependentType())), 173 // ContainsUnexpandedParameterPack 174 (Base->containsUnexpandedParameterPack() || 175 (QualifierLoc && 176 QualifierLoc.getNestedNameSpecifier() 177 ->containsUnexpandedParameterPack()) || 178 (ScopeType && 179 ScopeType->getType()->containsUnexpandedParameterPack()) || 180 (DestroyedType.getTypeSourceInfo() && 181 DestroyedType.getTypeSourceInfo()->getType() 182 ->containsUnexpandedParameterPack()))), 183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), 184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc), 186 DestroyedType(DestroyedType) { } 187 188 QualType CXXPseudoDestructorExpr::getDestroyedType() const { 189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 190 return TInfo->getType(); 191 192 return QualType(); 193 } 194 195 SourceRange CXXPseudoDestructorExpr::getSourceRange() const { 196 SourceLocation End = DestroyedType.getLocation(); 197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd(); 199 return SourceRange(Base->getLocStart(), End); 200 } 201 202 203 // UnresolvedLookupExpr 204 UnresolvedLookupExpr * 205 UnresolvedLookupExpr::Create(ASTContext &C, 206 CXXRecordDecl *NamingClass, 207 NestedNameSpecifierLoc QualifierLoc, 208 SourceLocation TemplateKWLoc, 209 const DeclarationNameInfo &NameInfo, 210 bool ADL, 211 const TemplateArgumentListInfo *Args, 212 UnresolvedSetIterator Begin, 213 UnresolvedSetIterator End) 214 { 215 assert(Args || TemplateKWLoc.isValid()); 216 unsigned num_args = Args ? Args->size() : 0; 217 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) + 218 ASTTemplateKWAndArgsInfo::sizeFor(num_args)); 219 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, 220 TemplateKWLoc, NameInfo, 221 ADL, /*Overload*/ true, Args, 222 Begin, End, /*StdIsAssociated=*/false); 223 } 224 225 UnresolvedLookupExpr * 226 UnresolvedLookupExpr::CreateEmpty(ASTContext &C, 227 bool HasTemplateKWAndArgsInfo, 228 unsigned NumTemplateArgs) { 229 std::size_t size = sizeof(UnresolvedLookupExpr); 230 if (HasTemplateKWAndArgsInfo) 231 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 232 233 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>()); 234 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell()); 235 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 236 return E; 237 } 238 239 OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, 240 NestedNameSpecifierLoc QualifierLoc, 241 SourceLocation TemplateKWLoc, 242 const DeclarationNameInfo &NameInfo, 243 const TemplateArgumentListInfo *TemplateArgs, 244 UnresolvedSetIterator Begin, 245 UnresolvedSetIterator End, 246 bool KnownDependent, 247 bool KnownInstantiationDependent, 248 bool KnownContainsUnexpandedParameterPack) 249 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent, 250 KnownDependent, 251 (KnownInstantiationDependent || 252 NameInfo.isInstantiationDependent() || 253 (QualifierLoc && 254 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), 255 (KnownContainsUnexpandedParameterPack || 256 NameInfo.containsUnexpandedParameterPack() || 257 (QualifierLoc && 258 QualifierLoc.getNestedNameSpecifier() 259 ->containsUnexpandedParameterPack()))), 260 Results(0), NumResults(End - Begin), NameInfo(NameInfo), 261 QualifierLoc(QualifierLoc), 262 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()) 263 { 264 NumResults = End - Begin; 265 if (NumResults) { 266 // Determine whether this expression is type-dependent. 267 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) { 268 if ((*I)->getDeclContext()->isDependentContext() || 269 isa<UnresolvedUsingValueDecl>(*I)) { 270 ExprBits.TypeDependent = true; 271 ExprBits.ValueDependent = true; 272 } 273 } 274 275 Results = static_cast<DeclAccessPair *>( 276 C.Allocate(sizeof(DeclAccessPair) * NumResults, 277 llvm::alignOf<DeclAccessPair>())); 278 memcpy(Results, &*Begin.getIterator(), 279 NumResults * sizeof(DeclAccessPair)); 280 } 281 282 // If we have explicit template arguments, check for dependent 283 // template arguments and whether they contain any unexpanded pack 284 // expansions. 285 if (TemplateArgs) { 286 bool Dependent = false; 287 bool InstantiationDependent = false; 288 bool ContainsUnexpandedParameterPack = false; 289 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs, 290 Dependent, 291 InstantiationDependent, 292 ContainsUnexpandedParameterPack); 293 294 if (Dependent) { 295 ExprBits.TypeDependent = true; 296 ExprBits.ValueDependent = true; 297 } 298 if (InstantiationDependent) 299 ExprBits.InstantiationDependent = true; 300 if (ContainsUnexpandedParameterPack) 301 ExprBits.ContainsUnexpandedParameterPack = true; 302 } else if (TemplateKWLoc.isValid()) { 303 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 304 } 305 306 if (isTypeDependent()) 307 setType(C.DependentTy); 308 } 309 310 void OverloadExpr::initializeResults(ASTContext &C, 311 UnresolvedSetIterator Begin, 312 UnresolvedSetIterator End) { 313 assert(Results == 0 && "Results already initialized!"); 314 NumResults = End - Begin; 315 if (NumResults) { 316 Results = static_cast<DeclAccessPair *>( 317 C.Allocate(sizeof(DeclAccessPair) * NumResults, 318 319 llvm::alignOf<DeclAccessPair>())); 320 memcpy(Results, &*Begin.getIterator(), 321 NumResults * sizeof(DeclAccessPair)); 322 } 323 } 324 325 CXXRecordDecl *OverloadExpr::getNamingClass() const { 326 if (isa<UnresolvedLookupExpr>(this)) 327 return cast<UnresolvedLookupExpr>(this)->getNamingClass(); 328 else 329 return cast<UnresolvedMemberExpr>(this)->getNamingClass(); 330 } 331 332 // DependentScopeDeclRefExpr 333 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T, 334 NestedNameSpecifierLoc QualifierLoc, 335 SourceLocation TemplateKWLoc, 336 const DeclarationNameInfo &NameInfo, 337 const TemplateArgumentListInfo *Args) 338 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary, 339 true, true, 340 (NameInfo.isInstantiationDependent() || 341 (QualifierLoc && 342 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), 343 (NameInfo.containsUnexpandedParameterPack() || 344 (QualifierLoc && 345 QualifierLoc.getNestedNameSpecifier() 346 ->containsUnexpandedParameterPack()))), 347 QualifierLoc(QualifierLoc), NameInfo(NameInfo), 348 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid()) 349 { 350 if (Args) { 351 bool Dependent = true; 352 bool InstantiationDependent = true; 353 bool ContainsUnexpandedParameterPack 354 = ExprBits.ContainsUnexpandedParameterPack; 355 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args, 356 Dependent, 357 InstantiationDependent, 358 ContainsUnexpandedParameterPack); 359 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 360 } else if (TemplateKWLoc.isValid()) { 361 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 362 } 363 } 364 365 DependentScopeDeclRefExpr * 366 DependentScopeDeclRefExpr::Create(ASTContext &C, 367 NestedNameSpecifierLoc QualifierLoc, 368 SourceLocation TemplateKWLoc, 369 const DeclarationNameInfo &NameInfo, 370 const TemplateArgumentListInfo *Args) { 371 std::size_t size = sizeof(DependentScopeDeclRefExpr); 372 if (Args) 373 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size()); 374 else if (TemplateKWLoc.isValid()) 375 size += ASTTemplateKWAndArgsInfo::sizeFor(0); 376 void *Mem = C.Allocate(size); 377 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc, 378 TemplateKWLoc, NameInfo, Args); 379 } 380 381 DependentScopeDeclRefExpr * 382 DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C, 383 bool HasTemplateKWAndArgsInfo, 384 unsigned NumTemplateArgs) { 385 std::size_t size = sizeof(DependentScopeDeclRefExpr); 386 if (HasTemplateKWAndArgsInfo) 387 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 388 void *Mem = C.Allocate(size); 389 DependentScopeDeclRefExpr *E 390 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(), 391 SourceLocation(), 392 DeclarationNameInfo(), 0); 393 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 394 return E; 395 } 396 397 SourceRange CXXConstructExpr::getSourceRange() const { 398 if (isa<CXXTemporaryObjectExpr>(this)) 399 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange(); 400 401 if (ParenRange.isValid()) 402 return SourceRange(Loc, ParenRange.getEnd()); 403 404 SourceLocation End = Loc; 405 for (unsigned I = getNumArgs(); I > 0; --I) { 406 const Expr *Arg = getArg(I-1); 407 if (!Arg->isDefaultArgument()) { 408 SourceLocation NewEnd = Arg->getLocEnd(); 409 if (NewEnd.isValid()) { 410 End = NewEnd; 411 break; 412 } 413 } 414 } 415 416 return SourceRange(Loc, End); 417 } 418 419 SourceRange CXXOperatorCallExpr::getSourceRange() const { 420 OverloadedOperatorKind Kind = getOperator(); 421 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 422 if (getNumArgs() == 1) 423 // Prefix operator 424 return SourceRange(getOperatorLoc(), 425 getArg(0)->getSourceRange().getEnd()); 426 else 427 // Postfix operator 428 return SourceRange(getArg(0)->getSourceRange().getBegin(), 429 getOperatorLoc()); 430 } else if (Kind == OO_Arrow) { 431 return getArg(0)->getSourceRange(); 432 } else if (Kind == OO_Call) { 433 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); 434 } else if (Kind == OO_Subscript) { 435 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); 436 } else if (getNumArgs() == 1) { 437 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd()); 438 } else if (getNumArgs() == 2) { 439 return SourceRange(getArg(0)->getSourceRange().getBegin(), 440 getArg(1)->getSourceRange().getEnd()); 441 } else { 442 return SourceRange(); 443 } 444 } 445 446 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const { 447 if (const MemberExpr *MemExpr = 448 dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) 449 return MemExpr->getBase(); 450 451 // FIXME: Will eventually need to cope with member pointers. 452 return 0; 453 } 454 455 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const { 456 if (const MemberExpr *MemExpr = 457 dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) 458 return cast<CXXMethodDecl>(MemExpr->getMemberDecl()); 459 460 // FIXME: Will eventually need to cope with member pointers. 461 return 0; 462 } 463 464 465 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() { 466 Expr* ThisArg = getImplicitObjectArgument(); 467 if (!ThisArg) 468 return 0; 469 470 if (ThisArg->getType()->isAnyPointerType()) 471 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl(); 472 473 return ThisArg->getType()->getAsCXXRecordDecl(); 474 } 475 476 477 //===----------------------------------------------------------------------===// 478 // Named casts 479 //===----------------------------------------------------------------------===// 480 481 /// getCastName - Get the name of the C++ cast being used, e.g., 482 /// "static_cast", "dynamic_cast", "reinterpret_cast", or 483 /// "const_cast". The returned pointer must not be freed. 484 const char *CXXNamedCastExpr::getCastName() const { 485 switch (getStmtClass()) { 486 case CXXStaticCastExprClass: return "static_cast"; 487 case CXXDynamicCastExprClass: return "dynamic_cast"; 488 case CXXReinterpretCastExprClass: return "reinterpret_cast"; 489 case CXXConstCastExprClass: return "const_cast"; 490 default: return "<invalid cast>"; 491 } 492 } 493 494 CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T, 495 ExprValueKind VK, 496 CastKind K, Expr *Op, 497 const CXXCastPath *BasePath, 498 TypeSourceInfo *WrittenTy, 499 SourceLocation L, 500 SourceLocation RParenLoc) { 501 unsigned PathSize = (BasePath ? BasePath->size() : 0); 502 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr) 503 + PathSize * sizeof(CXXBaseSpecifier*)); 504 CXXStaticCastExpr *E = 505 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 506 RParenLoc); 507 if (PathSize) E->setCastPath(*BasePath); 508 return E; 509 } 510 511 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C, 512 unsigned PathSize) { 513 void *Buffer = 514 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 515 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize); 516 } 517 518 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T, 519 ExprValueKind VK, 520 CastKind K, Expr *Op, 521 const CXXCastPath *BasePath, 522 TypeSourceInfo *WrittenTy, 523 SourceLocation L, 524 SourceLocation RParenLoc) { 525 unsigned PathSize = (BasePath ? BasePath->size() : 0); 526 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr) 527 + PathSize * sizeof(CXXBaseSpecifier*)); 528 CXXDynamicCastExpr *E = 529 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 530 RParenLoc); 531 if (PathSize) E->setCastPath(*BasePath); 532 return E; 533 } 534 535 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C, 536 unsigned PathSize) { 537 void *Buffer = 538 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 539 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize); 540 } 541 542 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven 543 /// to always be null. For example: 544 /// 545 /// struct A { }; 546 /// struct B final : A { }; 547 /// struct C { }; 548 /// 549 /// C *f(B* b) { return dynamic_cast<C*>(b); } 550 bool CXXDynamicCastExpr::isAlwaysNull() const 551 { 552 QualType SrcType = getSubExpr()->getType(); 553 QualType DestType = getType(); 554 555 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) { 556 SrcType = SrcPTy->getPointeeType(); 557 DestType = DestType->castAs<PointerType>()->getPointeeType(); 558 } 559 560 const CXXRecordDecl *SrcRD = 561 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl()); 562 563 if (!SrcRD->hasAttr<FinalAttr>()) 564 return false; 565 566 const CXXRecordDecl *DestRD = 567 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl()); 568 569 return !DestRD->isDerivedFrom(SrcRD); 570 } 571 572 CXXReinterpretCastExpr * 573 CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK, 574 CastKind K, Expr *Op, 575 const CXXCastPath *BasePath, 576 TypeSourceInfo *WrittenTy, SourceLocation L, 577 SourceLocation RParenLoc) { 578 unsigned PathSize = (BasePath ? BasePath->size() : 0); 579 void *Buffer = 580 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 581 CXXReinterpretCastExpr *E = 582 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 583 RParenLoc); 584 if (PathSize) E->setCastPath(*BasePath); 585 return E; 586 } 587 588 CXXReinterpretCastExpr * 589 CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) { 590 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr) 591 + PathSize * sizeof(CXXBaseSpecifier*)); 592 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize); 593 } 594 595 CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T, 596 ExprValueKind VK, Expr *Op, 597 TypeSourceInfo *WrittenTy, 598 SourceLocation L, 599 SourceLocation RParenLoc) { 600 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc); 601 } 602 603 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) { 604 return new (C) CXXConstCastExpr(EmptyShell()); 605 } 606 607 CXXFunctionalCastExpr * 608 CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK, 609 TypeSourceInfo *Written, SourceLocation L, 610 CastKind K, Expr *Op, const CXXCastPath *BasePath, 611 SourceLocation R) { 612 unsigned PathSize = (BasePath ? BasePath->size() : 0); 613 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr) 614 + PathSize * sizeof(CXXBaseSpecifier*)); 615 CXXFunctionalCastExpr *E = 616 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R); 617 if (PathSize) E->setCastPath(*BasePath); 618 return E; 619 } 620 621 CXXFunctionalCastExpr * 622 CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) { 623 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr) 624 + PathSize * sizeof(CXXBaseSpecifier*)); 625 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize); 626 } 627 628 629 CXXDefaultArgExpr * 630 CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc, 631 ParmVarDecl *Param, Expr *SubExpr) { 632 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *)); 633 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, 634 SubExpr); 635 } 636 637 CXXTemporary *CXXTemporary::Create(ASTContext &C, 638 const CXXDestructorDecl *Destructor) { 639 return new (C) CXXTemporary(Destructor); 640 } 641 642 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C, 643 CXXTemporary *Temp, 644 Expr* SubExpr) { 645 assert((SubExpr->getType()->isRecordType() || 646 SubExpr->getType()->isArrayType()) && 647 "Expression bound to a temporary must have record or array type!"); 648 649 return new (C) CXXBindTemporaryExpr(Temp, SubExpr); 650 } 651 652 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C, 653 CXXConstructorDecl *Cons, 654 TypeSourceInfo *Type, 655 Expr **Args, 656 unsigned NumArgs, 657 SourceRange parenRange, 658 bool HadMultipleCandidates, 659 bool ZeroInitialization) 660 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, 661 Type->getType().getNonReferenceType(), 662 Type->getTypeLoc().getBeginLoc(), 663 Cons, false, Args, NumArgs, 664 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization, 665 CXXConstructExpr::CK_Complete, parenRange), 666 Type(Type) { 667 } 668 669 SourceRange CXXTemporaryObjectExpr::getSourceRange() const { 670 return SourceRange(Type->getTypeLoc().getBeginLoc(), 671 getParenRange().getEnd()); 672 } 673 674 CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T, 675 SourceLocation Loc, 676 CXXConstructorDecl *D, bool Elidable, 677 Expr **Args, unsigned NumArgs, 678 bool HadMultipleCandidates, 679 bool ListInitialization, 680 bool ZeroInitialization, 681 ConstructionKind ConstructKind, 682 SourceRange ParenRange) { 683 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D, 684 Elidable, Args, NumArgs, 685 HadMultipleCandidates, ListInitialization, 686 ZeroInitialization, ConstructKind, 687 ParenRange); 688 } 689 690 CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 691 SourceLocation Loc, 692 CXXConstructorDecl *D, bool elidable, 693 Expr **args, unsigned numargs, 694 bool HadMultipleCandidates, 695 bool ListInitialization, 696 bool ZeroInitialization, 697 ConstructionKind ConstructKind, 698 SourceRange ParenRange) 699 : Expr(SC, T, VK_RValue, OK_Ordinary, 700 T->isDependentType(), T->isDependentType(), 701 T->isInstantiationDependentType(), 702 T->containsUnexpandedParameterPack()), 703 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs), 704 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates), 705 ListInitialization(ListInitialization), 706 ZeroInitialization(ZeroInitialization), 707 ConstructKind(ConstructKind), Args(0) 708 { 709 if (NumArgs) { 710 Args = new (C) Stmt*[NumArgs]; 711 712 for (unsigned i = 0; i != NumArgs; ++i) { 713 assert(args[i] && "NULL argument in CXXConstructExpr"); 714 715 if (args[i]->isValueDependent()) 716 ExprBits.ValueDependent = true; 717 if (args[i]->isInstantiationDependent()) 718 ExprBits.InstantiationDependent = true; 719 if (args[i]->containsUnexpandedParameterPack()) 720 ExprBits.ContainsUnexpandedParameterPack = true; 721 722 Args[i] = args[i]; 723 } 724 } 725 } 726 727 LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit, 728 LambdaCaptureKind Kind, VarDecl *Var, 729 SourceLocation EllipsisLoc) 730 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) 731 { 732 unsigned Bits = 0; 733 if (Implicit) 734 Bits |= Capture_Implicit; 735 736 switch (Kind) { 737 case LCK_This: 738 assert(Var == 0 && "'this' capture cannot have a variable!"); 739 break; 740 741 case LCK_ByCopy: 742 Bits |= Capture_ByCopy; 743 // Fall through 744 case LCK_ByRef: 745 assert(Var && "capture must have a variable!"); 746 break; 747 } 748 VarAndBits.setInt(Bits); 749 } 750 751 LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const { 752 if (capturesThis()) 753 return LCK_This; 754 755 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef; 756 } 757 758 LambdaExpr::LambdaExpr(QualType T, 759 SourceRange IntroducerRange, 760 LambdaCaptureDefault CaptureDefault, 761 ArrayRef<Capture> Captures, 762 bool ExplicitParams, 763 bool ExplicitResultType, 764 ArrayRef<Expr *> CaptureInits, 765 ArrayRef<VarDecl *> ArrayIndexVars, 766 ArrayRef<unsigned> ArrayIndexStarts, 767 SourceLocation ClosingBrace, 768 unsigned ManglingNumber, 769 Decl *ContextDecl) 770 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, 771 T->isDependentType(), T->isDependentType(), T->isDependentType(), 772 /*ContainsUnexpandedParameterPack=*/false), 773 IntroducerRange(IntroducerRange), 774 NumCaptures(Captures.size()), 775 CaptureDefault(CaptureDefault), 776 ExplicitParams(ExplicitParams), 777 ExplicitResultType(ExplicitResultType), 778 ClosingBrace(ClosingBrace) 779 { 780 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments"); 781 CXXRecordDecl *Class = getLambdaClass(); 782 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData(); 783 784 // FIXME: Propagate "has unexpanded parameter pack" bit. 785 786 // Copy captures. 787 ASTContext &Context = Class->getASTContext(); 788 Data.NumCaptures = NumCaptures; 789 Data.NumExplicitCaptures = 0; 790 Data.ManglingNumber = ManglingNumber; 791 Data.ContextDecl = ContextDecl; 792 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures); 793 Capture *ToCapture = Data.Captures; 794 for (unsigned I = 0, N = Captures.size(); I != N; ++I) { 795 if (Captures[I].isExplicit()) 796 ++Data.NumExplicitCaptures; 797 798 *ToCapture++ = Captures[I]; 799 } 800 801 // Copy initialization expressions for the non-static data members. 802 Stmt **Stored = getStoredStmts(); 803 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I) 804 *Stored++ = CaptureInits[I]; 805 806 // Copy the body of the lambda. 807 *Stored++ = getCallOperator()->getBody(); 808 809 // Copy the array index variables, if any. 810 HasArrayIndexVars = !ArrayIndexVars.empty(); 811 if (HasArrayIndexVars) { 812 assert(ArrayIndexStarts.size() == NumCaptures); 813 memcpy(getArrayIndexVars(), ArrayIndexVars.data(), 814 sizeof(VarDecl *) * ArrayIndexVars.size()); 815 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(), 816 sizeof(unsigned) * Captures.size()); 817 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size(); 818 } 819 820 if (ManglingNumber) 821 Class->ClearLinkageCache(); 822 } 823 824 LambdaExpr *LambdaExpr::Create(ASTContext &Context, 825 CXXRecordDecl *Class, 826 SourceRange IntroducerRange, 827 LambdaCaptureDefault CaptureDefault, 828 ArrayRef<Capture> Captures, 829 bool ExplicitParams, 830 bool ExplicitResultType, 831 ArrayRef<Expr *> CaptureInits, 832 ArrayRef<VarDecl *> ArrayIndexVars, 833 ArrayRef<unsigned> ArrayIndexStarts, 834 SourceLocation ClosingBrace, 835 unsigned ManglingNumber, 836 Decl *ContextDecl) { 837 // Determine the type of the expression (i.e., the type of the 838 // function object we're creating). 839 QualType T = Context.getTypeDeclType(Class); 840 841 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1); 842 if (!ArrayIndexVars.empty()) 843 Size += sizeof(VarDecl *) * ArrayIndexVars.size() 844 + sizeof(unsigned) * (Captures.size() + 1); 845 void *Mem = Context.Allocate(Size); 846 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault, 847 Captures, ExplicitParams, ExplicitResultType, 848 CaptureInits, ArrayIndexVars, ArrayIndexStarts, 849 ClosingBrace, ManglingNumber, ContextDecl); 850 } 851 852 LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures, 853 unsigned NumArrayIndexVars) { 854 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1); 855 if (NumArrayIndexVars) 856 Size += sizeof(VarDecl) * NumArrayIndexVars 857 + sizeof(unsigned) * (NumCaptures + 1); 858 void *Mem = C.Allocate(Size); 859 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0); 860 } 861 862 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const { 863 return getLambdaClass()->getLambdaData().Captures; 864 } 865 866 LambdaExpr::capture_iterator LambdaExpr::capture_end() const { 867 return capture_begin() + NumCaptures; 868 } 869 870 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const { 871 return capture_begin(); 872 } 873 874 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const { 875 struct CXXRecordDecl::LambdaDefinitionData &Data 876 = getLambdaClass()->getLambdaData(); 877 return Data.Captures + Data.NumExplicitCaptures; 878 } 879 880 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const { 881 return explicit_capture_end(); 882 } 883 884 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const { 885 return capture_end(); 886 } 887 888 ArrayRef<VarDecl *> 889 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const { 890 assert(HasArrayIndexVars && "No array index-var data?"); 891 892 unsigned Index = Iter - capture_init_begin(); 893 assert(Index < getLambdaClass()->getLambdaData().NumCaptures && 894 "Capture index out-of-range"); 895 VarDecl **IndexVars = getArrayIndexVars(); 896 unsigned *IndexStarts = getArrayIndexStarts(); 897 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index], 898 IndexVars + IndexStarts[Index + 1]); 899 } 900 901 CXXRecordDecl *LambdaExpr::getLambdaClass() const { 902 return getType()->getAsCXXRecordDecl(); 903 } 904 905 CXXMethodDecl *LambdaExpr::getCallOperator() const { 906 CXXRecordDecl *Record = getLambdaClass(); 907 DeclarationName Name 908 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call); 909 DeclContext::lookup_result Calls = Record->lookup(Name); 910 assert(Calls.first != Calls.second && "Missing lambda call operator!"); 911 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++); 912 assert(Calls.first == Calls.second && "More than lambda one call operator?"); 913 return Result; 914 } 915 916 CompoundStmt *LambdaExpr::getBody() const { 917 if (!getStoredStmts()[NumCaptures]) 918 getStoredStmts()[NumCaptures] = getCallOperator()->getBody(); 919 920 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]); 921 } 922 923 bool LambdaExpr::isMutable() const { 924 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0; 925 } 926 927 ExprWithCleanups::ExprWithCleanups(Expr *subexpr, 928 ArrayRef<CleanupObject> objects) 929 : Expr(ExprWithCleanupsClass, subexpr->getType(), 930 subexpr->getValueKind(), subexpr->getObjectKind(), 931 subexpr->isTypeDependent(), subexpr->isValueDependent(), 932 subexpr->isInstantiationDependent(), 933 subexpr->containsUnexpandedParameterPack()), 934 SubExpr(subexpr) { 935 ExprWithCleanupsBits.NumObjects = objects.size(); 936 for (unsigned i = 0, e = objects.size(); i != e; ++i) 937 getObjectsBuffer()[i] = objects[i]; 938 } 939 940 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr, 941 ArrayRef<CleanupObject> objects) { 942 size_t size = sizeof(ExprWithCleanups) 943 + objects.size() * sizeof(CleanupObject); 944 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>()); 945 return new (buffer) ExprWithCleanups(subexpr, objects); 946 } 947 948 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects) 949 : Expr(ExprWithCleanupsClass, empty) { 950 ExprWithCleanupsBits.NumObjects = numObjects; 951 } 952 953 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty, 954 unsigned numObjects) { 955 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject); 956 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>()); 957 return new (buffer) ExprWithCleanups(empty, numObjects); 958 } 959 960 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 961 SourceLocation LParenLoc, 962 Expr **Args, 963 unsigned NumArgs, 964 SourceLocation RParenLoc) 965 : Expr(CXXUnresolvedConstructExprClass, 966 Type->getType().getNonReferenceType(), 967 (Type->getType()->isLValueReferenceType() ? VK_LValue 968 :Type->getType()->isRValueReferenceType()? VK_XValue 969 :VK_RValue), 970 OK_Ordinary, 971 Type->getType()->isDependentType(), true, true, 972 Type->getType()->containsUnexpandedParameterPack()), 973 Type(Type), 974 LParenLoc(LParenLoc), 975 RParenLoc(RParenLoc), 976 NumArgs(NumArgs) { 977 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1); 978 for (unsigned I = 0; I != NumArgs; ++I) { 979 if (Args[I]->containsUnexpandedParameterPack()) 980 ExprBits.ContainsUnexpandedParameterPack = true; 981 982 StoredArgs[I] = Args[I]; 983 } 984 } 985 986 CXXUnresolvedConstructExpr * 987 CXXUnresolvedConstructExpr::Create(ASTContext &C, 988 TypeSourceInfo *Type, 989 SourceLocation LParenLoc, 990 Expr **Args, 991 unsigned NumArgs, 992 SourceLocation RParenLoc) { 993 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + 994 sizeof(Expr *) * NumArgs); 995 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, 996 Args, NumArgs, RParenLoc); 997 } 998 999 CXXUnresolvedConstructExpr * 1000 CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) { 1001 Stmt::EmptyShell Empty; 1002 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + 1003 sizeof(Expr *) * NumArgs); 1004 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs); 1005 } 1006 1007 SourceRange CXXUnresolvedConstructExpr::getSourceRange() const { 1008 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc); 1009 } 1010 1011 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C, 1012 Expr *Base, QualType BaseType, 1013 bool IsArrow, 1014 SourceLocation OperatorLoc, 1015 NestedNameSpecifierLoc QualifierLoc, 1016 SourceLocation TemplateKWLoc, 1017 NamedDecl *FirstQualifierFoundInScope, 1018 DeclarationNameInfo MemberNameInfo, 1019 const TemplateArgumentListInfo *TemplateArgs) 1020 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, 1021 VK_LValue, OK_Ordinary, true, true, true, 1022 ((Base && Base->containsUnexpandedParameterPack()) || 1023 (QualifierLoc && 1024 QualifierLoc.getNestedNameSpecifier() 1025 ->containsUnexpandedParameterPack()) || 1026 MemberNameInfo.containsUnexpandedParameterPack())), 1027 Base(Base), BaseType(BaseType), IsArrow(IsArrow), 1028 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()), 1029 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 1030 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1031 MemberNameInfo(MemberNameInfo) { 1032 if (TemplateArgs) { 1033 bool Dependent = true; 1034 bool InstantiationDependent = true; 1035 bool ContainsUnexpandedParameterPack = false; 1036 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs, 1037 Dependent, 1038 InstantiationDependent, 1039 ContainsUnexpandedParameterPack); 1040 if (ContainsUnexpandedParameterPack) 1041 ExprBits.ContainsUnexpandedParameterPack = true; 1042 } else if (TemplateKWLoc.isValid()) { 1043 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 1044 } 1045 } 1046 1047 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C, 1048 Expr *Base, QualType BaseType, 1049 bool IsArrow, 1050 SourceLocation OperatorLoc, 1051 NestedNameSpecifierLoc QualifierLoc, 1052 NamedDecl *FirstQualifierFoundInScope, 1053 DeclarationNameInfo MemberNameInfo) 1054 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, 1055 VK_LValue, OK_Ordinary, true, true, true, 1056 ((Base && Base->containsUnexpandedParameterPack()) || 1057 (QualifierLoc && 1058 QualifierLoc.getNestedNameSpecifier()-> 1059 containsUnexpandedParameterPack()) || 1060 MemberNameInfo.containsUnexpandedParameterPack())), 1061 Base(Base), BaseType(BaseType), IsArrow(IsArrow), 1062 HasTemplateKWAndArgsInfo(false), 1063 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 1064 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1065 MemberNameInfo(MemberNameInfo) { } 1066 1067 CXXDependentScopeMemberExpr * 1068 CXXDependentScopeMemberExpr::Create(ASTContext &C, 1069 Expr *Base, QualType BaseType, bool IsArrow, 1070 SourceLocation OperatorLoc, 1071 NestedNameSpecifierLoc QualifierLoc, 1072 SourceLocation TemplateKWLoc, 1073 NamedDecl *FirstQualifierFoundInScope, 1074 DeclarationNameInfo MemberNameInfo, 1075 const TemplateArgumentListInfo *TemplateArgs) { 1076 if (!TemplateArgs && !TemplateKWLoc.isValid()) 1077 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType, 1078 IsArrow, OperatorLoc, 1079 QualifierLoc, 1080 FirstQualifierFoundInScope, 1081 MemberNameInfo); 1082 1083 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0; 1084 std::size_t size = sizeof(CXXDependentScopeMemberExpr) 1085 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1086 1087 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>()); 1088 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType, 1089 IsArrow, OperatorLoc, 1090 QualifierLoc, 1091 TemplateKWLoc, 1092 FirstQualifierFoundInScope, 1093 MemberNameInfo, TemplateArgs); 1094 } 1095 1096 CXXDependentScopeMemberExpr * 1097 CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C, 1098 bool HasTemplateKWAndArgsInfo, 1099 unsigned NumTemplateArgs) { 1100 if (!HasTemplateKWAndArgsInfo) 1101 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(), 1102 0, SourceLocation(), 1103 NestedNameSpecifierLoc(), 0, 1104 DeclarationNameInfo()); 1105 1106 std::size_t size = sizeof(CXXDependentScopeMemberExpr) + 1107 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1108 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>()); 1109 CXXDependentScopeMemberExpr *E 1110 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(), 1111 0, SourceLocation(), 1112 NestedNameSpecifierLoc(), 1113 SourceLocation(), 0, 1114 DeclarationNameInfo(), 0); 1115 E->HasTemplateKWAndArgsInfo = true; 1116 return E; 1117 } 1118 1119 bool CXXDependentScopeMemberExpr::isImplicitAccess() const { 1120 if (Base == 0) 1121 return true; 1122 1123 return cast<Expr>(Base)->isImplicitCXXThis(); 1124 } 1125 1126 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin, 1127 UnresolvedSetIterator end) { 1128 do { 1129 NamedDecl *decl = *begin; 1130 if (isa<UnresolvedUsingValueDecl>(decl)) 1131 return false; 1132 if (isa<UsingShadowDecl>(decl)) 1133 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl(); 1134 1135 // Unresolved member expressions should only contain methods and 1136 // method templates. 1137 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl)); 1138 1139 if (isa<FunctionTemplateDecl>(decl)) 1140 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl(); 1141 if (cast<CXXMethodDecl>(decl)->isStatic()) 1142 return false; 1143 } while (++begin != end); 1144 1145 return true; 1146 } 1147 1148 UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, 1149 bool HasUnresolvedUsing, 1150 Expr *Base, QualType BaseType, 1151 bool IsArrow, 1152 SourceLocation OperatorLoc, 1153 NestedNameSpecifierLoc QualifierLoc, 1154 SourceLocation TemplateKWLoc, 1155 const DeclarationNameInfo &MemberNameInfo, 1156 const TemplateArgumentListInfo *TemplateArgs, 1157 UnresolvedSetIterator Begin, 1158 UnresolvedSetIterator End) 1159 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc, 1160 MemberNameInfo, TemplateArgs, Begin, End, 1161 // Dependent 1162 ((Base && Base->isTypeDependent()) || 1163 BaseType->isDependentType()), 1164 ((Base && Base->isInstantiationDependent()) || 1165 BaseType->isInstantiationDependentType()), 1166 // Contains unexpanded parameter pack 1167 ((Base && Base->containsUnexpandedParameterPack()) || 1168 BaseType->containsUnexpandedParameterPack())), 1169 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), 1170 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) { 1171 1172 // Check whether all of the members are non-static member functions, 1173 // and if so, mark give this bound-member type instead of overload type. 1174 if (hasOnlyNonStaticMemberFunctions(Begin, End)) 1175 setType(C.BoundMemberTy); 1176 } 1177 1178 bool UnresolvedMemberExpr::isImplicitAccess() const { 1179 if (Base == 0) 1180 return true; 1181 1182 return cast<Expr>(Base)->isImplicitCXXThis(); 1183 } 1184 1185 UnresolvedMemberExpr * 1186 UnresolvedMemberExpr::Create(ASTContext &C, 1187 bool HasUnresolvedUsing, 1188 Expr *Base, QualType BaseType, bool IsArrow, 1189 SourceLocation OperatorLoc, 1190 NestedNameSpecifierLoc QualifierLoc, 1191 SourceLocation TemplateKWLoc, 1192 const DeclarationNameInfo &MemberNameInfo, 1193 const TemplateArgumentListInfo *TemplateArgs, 1194 UnresolvedSetIterator Begin, 1195 UnresolvedSetIterator End) { 1196 std::size_t size = sizeof(UnresolvedMemberExpr); 1197 if (TemplateArgs) 1198 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size()); 1199 else if (TemplateKWLoc.isValid()) 1200 size += ASTTemplateKWAndArgsInfo::sizeFor(0); 1201 1202 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>()); 1203 return new (Mem) UnresolvedMemberExpr(C, 1204 HasUnresolvedUsing, Base, BaseType, 1205 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc, 1206 MemberNameInfo, TemplateArgs, Begin, End); 1207 } 1208 1209 UnresolvedMemberExpr * 1210 UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 1211 unsigned NumTemplateArgs) { 1212 std::size_t size = sizeof(UnresolvedMemberExpr); 1213 if (HasTemplateKWAndArgsInfo) 1214 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1215 1216 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>()); 1217 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell()); 1218 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 1219 return E; 1220 } 1221 1222 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const { 1223 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this. 1224 1225 // If there was a nested name specifier, it names the naming class. 1226 // It can't be dependent: after all, we were actually able to do the 1227 // lookup. 1228 CXXRecordDecl *Record = 0; 1229 if (getQualifier()) { 1230 const Type *T = getQualifier()->getAsType(); 1231 assert(T && "qualifier in member expression does not name type"); 1232 Record = T->getAsCXXRecordDecl(); 1233 assert(Record && "qualifier in member expression does not name record"); 1234 } 1235 // Otherwise the naming class must have been the base class. 1236 else { 1237 QualType BaseType = getBaseType().getNonReferenceType(); 1238 if (isArrow()) { 1239 const PointerType *PT = BaseType->getAs<PointerType>(); 1240 assert(PT && "base of arrow member access is not pointer"); 1241 BaseType = PT->getPointeeType(); 1242 } 1243 1244 Record = BaseType->getAsCXXRecordDecl(); 1245 assert(Record && "base of member expression does not name record"); 1246 } 1247 1248 return Record; 1249 } 1250 1251 SubstNonTypeTemplateParmPackExpr:: 1252 SubstNonTypeTemplateParmPackExpr(QualType T, 1253 NonTypeTemplateParmDecl *Param, 1254 SourceLocation NameLoc, 1255 const TemplateArgument &ArgPack) 1256 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary, 1257 true, true, true, true), 1258 Param(Param), Arguments(ArgPack.pack_begin()), 1259 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { } 1260 1261 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const { 1262 return TemplateArgument(Arguments, NumArguments); 1263 } 1264 1265 void ArrayTypeTraitExpr::anchor() { } 1266