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