Initialization.h revision 5b9cc5df25c2198f270dd1d5c438fdce70d4051d
1//===--- Initialization.h - Semantic Analysis for Initializers --*- C++ -*-===// 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 provides supporting data types for initialization of objects. 11// 12//===----------------------------------------------------------------------===// 13#ifndef LLVM_CLANG_SEMA_INITIALIZATION_H 14#define LLVM_CLANG_SEMA_INITIALIZATION_H 15 16#include "clang/Sema/Ownership.h" 17#include "clang/Sema/Overload.h" 18#include "clang/AST/Type.h" 19#include "clang/AST/UnresolvedSet.h" 20#include "clang/Basic/SourceLocation.h" 21#include "llvm/ADT/PointerIntPair.h" 22#include "llvm/ADT/SmallVector.h" 23#include <cassert> 24 25namespace clang { 26 27class CXXBaseSpecifier; 28class DeclaratorDecl; 29class DeclaratorInfo; 30class FieldDecl; 31class FunctionDecl; 32class ParmVarDecl; 33class Sema; 34class TypeLoc; 35class VarDecl; 36 37/// \brief Describes an entity that is being initialized. 38class InitializedEntity { 39public: 40 /// \brief Specifies the kind of entity being initialized. 41 enum EntityKind { 42 /// \brief The entity being initialized is a variable. 43 EK_Variable, 44 /// \brief The entity being initialized is a function parameter. 45 EK_Parameter, 46 /// \brief The entity being initialized is the result of a function call. 47 EK_Result, 48 /// \brief The entity being initialized is an exception object that 49 /// is being thrown. 50 EK_Exception, 51 /// \brief The entity being initialized is a non-static data member 52 /// subobject. 53 EK_Member, 54 /// \brief The entity being initialized is an element of an array. 55 EK_ArrayElement, 56 /// \brief The entity being initialized is an object (or array of 57 /// objects) allocated via new. 58 EK_New, 59 /// \brief The entity being initialized is a temporary object. 60 EK_Temporary, 61 /// \brief The entity being initialized is a base member subobject. 62 EK_Base, 63 /// \brief The initialization is being done by a delegating constructor. 64 EK_Delegating, 65 /// \brief The entity being initialized is an element of a vector. 66 /// or vector. 67 EK_VectorElement, 68 /// \brief The entity being initialized is a field of block descriptor for 69 /// the copied-in c++ object. 70 EK_BlockElement, 71 /// \brief The entity being initialized is the real or imaginary part of a 72 /// complex number. 73 EK_ComplexElement 74 }; 75 76private: 77 /// \brief The kind of entity being initialized. 78 EntityKind Kind; 79 80 /// \brief If non-NULL, the parent entity in which this 81 /// initialization occurs. 82 const InitializedEntity *Parent; 83 84 /// \brief The type of the object or reference being initialized. 85 QualType Type; 86 87 union { 88 /// \brief When Kind == EK_Variable or EK_Member, the VarDecl or 89 /// FieldDecl, respectively. 90 DeclaratorDecl *VariableOrMember; 91 92 /// \brief When Kind == EK_Parameter, the ParmVarDecl, with the 93 /// low bit indicating whether the parameter is "consumed". 94 uintptr_t Parameter; 95 96 /// \brief When Kind == EK_Temporary, the type source information for 97 /// the temporary. 98 TypeSourceInfo *TypeInfo; 99 100 struct { 101 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the 102 /// location of the 'return', 'throw', or 'new' keyword, 103 /// respectively. When Kind == EK_Temporary, the location where 104 /// the temporary is being created. 105 unsigned Location; 106 107 /// \brief Whether the entity being initialized may end up using the 108 /// named return value optimization (NRVO). 109 bool NRVO; 110 } LocAndNRVO; 111 112 /// \brief When Kind == EK_Base, the base specifier that provides the 113 /// base class. The lower bit specifies whether the base is an inherited 114 /// virtual base. 115 uintptr_t Base; 116 117 /// \brief When Kind == EK_ArrayElement, EK_VectorElement, or 118 /// EK_ComplexElement, the index of the array or vector element being 119 /// initialized. 120 unsigned Index; 121 }; 122 123 InitializedEntity() { } 124 125 /// \brief Create the initialization entity for a variable. 126 InitializedEntity(VarDecl *Var) 127 : Kind(EK_Variable), Parent(0), Type(Var->getType()), 128 VariableOrMember(Var) { } 129 130 /// \brief Create the initialization entity for the result of a 131 /// function, throwing an object, performing an explicit cast, or 132 /// initializing a parameter for which there is no declaration. 133 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type, 134 bool NRVO = false) 135 : Kind(Kind), Parent(0), Type(Type) 136 { 137 LocAndNRVO.Location = Loc.getRawEncoding(); 138 LocAndNRVO.NRVO = NRVO; 139 } 140 141 /// \brief Create the initialization entity for a member subobject. 142 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent) 143 : Kind(EK_Member), Parent(Parent), Type(Member->getType()), 144 VariableOrMember(Member) { } 145 146 /// \brief Create the initialization entity for an array element. 147 InitializedEntity(ASTContext &Context, unsigned Index, 148 const InitializedEntity &Parent); 149 150public: 151 /// \brief Create the initialization entity for a variable. 152 static InitializedEntity InitializeVariable(VarDecl *Var) { 153 return InitializedEntity(Var); 154 } 155 156 /// \brief Create the initialization entity for a parameter. 157 static InitializedEntity InitializeParameter(ASTContext &Context, 158 ParmVarDecl *Parm) { 159 bool Consumed = (Context.getLangOptions().ObjCAutoRefCount && 160 Parm->hasAttr<NSConsumedAttr>()); 161 162 InitializedEntity Entity; 163 Entity.Kind = EK_Parameter; 164 Entity.Type = Context.getVariableArrayDecayedType( 165 Parm->getType().getUnqualifiedType()); 166 Entity.Parent = 0; 167 Entity.Parameter 168 = (static_cast<uintptr_t>(Consumed) | reinterpret_cast<uintptr_t>(Parm)); 169 return Entity; 170 } 171 172 /// \brief Create the initialization entity for a parameter that is 173 /// only known by its type. 174 static InitializedEntity InitializeParameter(ASTContext &Context, 175 QualType Type, 176 bool Consumed) { 177 InitializedEntity Entity; 178 Entity.Kind = EK_Parameter; 179 Entity.Type = Context.getVariableArrayDecayedType(Type); 180 Entity.Parent = 0; 181 Entity.Parameter = (Consumed); 182 return Entity; 183 } 184 185 /// \brief Create the initialization entity for the result of a function. 186 static InitializedEntity InitializeResult(SourceLocation ReturnLoc, 187 QualType Type, bool NRVO) { 188 return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO); 189 } 190 191 static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc, 192 QualType Type, bool NRVO) { 193 return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO); 194 } 195 196 /// \brief Create the initialization entity for an exception object. 197 static InitializedEntity InitializeException(SourceLocation ThrowLoc, 198 QualType Type, bool NRVO) { 199 return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO); 200 } 201 202 /// \brief Create the initialization entity for an object allocated via new. 203 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) { 204 return InitializedEntity(EK_New, NewLoc, Type); 205 } 206 207 /// \brief Create the initialization entity for a temporary. 208 static InitializedEntity InitializeTemporary(QualType Type) { 209 return InitializedEntity(EK_Temporary, SourceLocation(), Type); 210 } 211 212 /// \brief Create the initialization entity for a temporary. 213 static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo) { 214 InitializedEntity Result(EK_Temporary, SourceLocation(), 215 TypeInfo->getType()); 216 Result.TypeInfo = TypeInfo; 217 return Result; 218 } 219 220 /// \brief Create the initialization entity for a base class subobject. 221 static InitializedEntity InitializeBase(ASTContext &Context, 222 CXXBaseSpecifier *Base, 223 bool IsInheritedVirtualBase); 224 225 /// \brief Create the initialization entity for a delegated constructor. 226 static InitializedEntity InitializeDelegation(QualType Type) { 227 return InitializedEntity(EK_Delegating, SourceLocation(), Type); 228 } 229 230 /// \brief Create the initialization entity for a member subobject. 231 static InitializedEntity InitializeMember(FieldDecl *Member, 232 const InitializedEntity *Parent = 0) { 233 return InitializedEntity(Member, Parent); 234 } 235 236 /// \brief Create the initialization entity for a member subobject. 237 static InitializedEntity InitializeMember(IndirectFieldDecl *Member, 238 const InitializedEntity *Parent = 0) { 239 return InitializedEntity(Member->getAnonField(), Parent); 240 } 241 242 /// \brief Create the initialization entity for an array element. 243 static InitializedEntity InitializeElement(ASTContext &Context, 244 unsigned Index, 245 const InitializedEntity &Parent) { 246 return InitializedEntity(Context, Index, Parent); 247 } 248 249 /// \brief Determine the kind of initialization. 250 EntityKind getKind() const { return Kind; } 251 252 /// \brief Retrieve the parent of the entity being initialized, when 253 /// the initialization itself is occurring within the context of a 254 /// larger initialization. 255 const InitializedEntity *getParent() const { return Parent; } 256 257 /// \brief Retrieve type being initialized. 258 QualType getType() const { return Type; } 259 260 /// \brief Retrieve complete type-source information for the object being 261 /// constructed, if known. 262 TypeSourceInfo *getTypeSourceInfo() const { 263 if (Kind == EK_Temporary) 264 return TypeInfo; 265 266 return 0; 267 } 268 269 /// \brief Retrieve the name of the entity being initialized. 270 DeclarationName getName() const; 271 272 /// \brief Retrieve the variable, parameter, or field being 273 /// initialized. 274 DeclaratorDecl *getDecl() const; 275 276 /// \brief Determine whether this initialization allows the named return 277 /// value optimization, which also applies to thrown objects. 278 bool allowsNRVO() const; 279 280 /// \brief Determine whether this initialization consumes the 281 /// parameter. 282 bool isParameterConsumed() const { 283 assert(getKind() == EK_Parameter && "Not a parameter"); 284 return (Parameter & 1); 285 } 286 287 /// \brief Retrieve the base specifier. 288 CXXBaseSpecifier *getBaseSpecifier() const { 289 assert(getKind() == EK_Base && "Not a base specifier"); 290 return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1); 291 } 292 293 /// \brief Return whether the base is an inherited virtual base. 294 bool isInheritedVirtualBase() const { 295 assert(getKind() == EK_Base && "Not a base specifier"); 296 return Base & 0x1; 297 } 298 299 /// \brief Determine the location of the 'return' keyword when initializing 300 /// the result of a function call. 301 SourceLocation getReturnLoc() const { 302 assert(getKind() == EK_Result && "No 'return' location!"); 303 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location); 304 } 305 306 /// \brief Determine the location of the 'throw' keyword when initializing 307 /// an exception object. 308 SourceLocation getThrowLoc() const { 309 assert(getKind() == EK_Exception && "No 'throw' location!"); 310 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location); 311 } 312 313 /// \brief If this is already the initializer for an array or vector 314 /// element, sets the element index. 315 void setElementIndex(unsigned Index) { 316 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement || 317 EK_ComplexElement); 318 this->Index = Index; 319 } 320}; 321 322/// \brief Describes the kind of initialization being performed, along with 323/// location information for tokens related to the initialization (equal sign, 324/// parentheses). 325class InitializationKind { 326public: 327 /// \brief The kind of initialization being performed. 328 enum InitKind { 329 IK_Direct, ///< Direct initialization 330 IK_DirectList, ///< Direct list-initialization 331 IK_Copy, ///< Copy initialization 332 IK_Default, ///< Default initialization 333 IK_Value ///< Value initialization 334 }; 335 336private: 337 /// \brief The kind of initialization that we're storing. 338 enum StoredInitKind { 339 SIK_Direct = IK_Direct, ///< Direct initialization 340 SIK_DirectList = IK_DirectList, ///< Direct list-initialization 341 SIK_Copy = IK_Copy, ///< Copy initialization 342 SIK_Default = IK_Default, ///< Default initialization 343 SIK_Value = IK_Value, ///< Value initialization 344 SIK_ImplicitValue, ///< Implicit value initialization 345 SIK_DirectCast, ///< Direct initialization due to a cast 346 /// \brief Direct initialization due to a C-style cast. 347 SIK_DirectCStyleCast, 348 /// \brief Direct initialization due to a functional-style cast. 349 SIK_DirectFunctionalCast 350 }; 351 352 /// \brief The kind of initialization being performed. 353 StoredInitKind Kind; 354 355 /// \brief The source locations involved in the initialization. 356 SourceLocation Locations[3]; 357 358 InitializationKind(StoredInitKind Kind, SourceLocation Loc1, 359 SourceLocation Loc2, SourceLocation Loc3) 360 : Kind(Kind) 361 { 362 Locations[0] = Loc1; 363 Locations[1] = Loc2; 364 Locations[2] = Loc3; 365 } 366 367public: 368 /// \brief Create a direct initialization. 369 static InitializationKind CreateDirect(SourceLocation InitLoc, 370 SourceLocation LParenLoc, 371 SourceLocation RParenLoc) { 372 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc); 373 } 374 375 static InitializationKind CreateDirectList(SourceLocation InitLoc) { 376 return InitializationKind(SIK_DirectList, InitLoc, InitLoc, InitLoc); 377 } 378 379 /// \brief Create a direct initialization due to a cast that isn't a C-style 380 /// or functional cast. 381 static InitializationKind CreateCast(SourceRange TypeRange) { 382 return InitializationKind(SIK_DirectCast, 383 TypeRange.getBegin(), TypeRange.getBegin(), 384 TypeRange.getEnd()); 385 } 386 387 /// \brief Create a direct initialization for a C-style cast. 388 static InitializationKind CreateCStyleCast(SourceLocation StartLoc, 389 SourceRange TypeRange) { 390 return InitializationKind(SIK_DirectCStyleCast, 391 StartLoc, TypeRange.getBegin(), 392 TypeRange.getEnd()); 393 } 394 395 /// \brief Create a direct initialization for a functional cast. 396 static InitializationKind CreateFunctionalCast(SourceRange TypeRange) { 397 return InitializationKind(SIK_DirectFunctionalCast, 398 TypeRange.getBegin(), TypeRange.getBegin(), 399 TypeRange.getEnd()); 400 } 401 402 /// \brief Create a copy initialization. 403 static InitializationKind CreateCopy(SourceLocation InitLoc, 404 SourceLocation EqualLoc) { 405 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc); 406 } 407 408 /// \brief Create a default initialization. 409 static InitializationKind CreateDefault(SourceLocation InitLoc) { 410 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc); 411 } 412 413 /// \brief Create a value initialization. 414 static InitializationKind CreateValue(SourceLocation InitLoc, 415 SourceLocation LParenLoc, 416 SourceLocation RParenLoc, 417 bool isImplicit = false) { 418 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value, 419 InitLoc, LParenLoc, RParenLoc); 420 } 421 422 /// \brief Determine the initialization kind. 423 InitKind getKind() const { 424 if (Kind > SIK_ImplicitValue) 425 return IK_Direct; 426 if (Kind == SIK_ImplicitValue) 427 return IK_Value; 428 429 return (InitKind)Kind; 430 } 431 432 /// \brief Determine whether this initialization is an explicit cast. 433 bool isExplicitCast() const { 434 return Kind == SIK_DirectCast || 435 Kind == SIK_DirectCStyleCast || 436 Kind == SIK_DirectFunctionalCast; 437 } 438 439 /// \brief Determine whether this initialization is a C-style cast. 440 bool isCStyleOrFunctionalCast() const { 441 return Kind == SIK_DirectCStyleCast || Kind == SIK_DirectFunctionalCast; 442 } 443 444 /// brief Determine whether this is a C-style cast. 445 bool isCStyleCast() const { 446 return Kind == SIK_DirectCStyleCast; 447 } 448 449 /// brief Determine whether this is a functional-style cast. 450 bool isFunctionalCast() const { 451 return Kind == SIK_DirectFunctionalCast; 452 } 453 454 /// \brief Determine whether this initialization is an implicit 455 /// value-initialization, e.g., as occurs during aggregate 456 /// initialization. 457 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; } 458 459 /// \brief Retrieve the location at which initialization is occurring. 460 SourceLocation getLocation() const { return Locations[0]; } 461 462 /// \brief Retrieve the source range that covers the initialization. 463 SourceRange getRange() const { 464 return SourceRange(Locations[0], Locations[2]); 465 } 466 467 /// \brief Retrieve the location of the equal sign for copy initialization 468 /// (if present). 469 SourceLocation getEqualLoc() const { 470 assert(Kind == SIK_Copy && "Only copy initialization has an '='"); 471 return Locations[1]; 472 } 473 474 bool isCopyInit() const { return Kind == SIK_Copy; } 475 476 /// \brief Retrieve the source range containing the locations of the open 477 /// and closing parentheses for value and direct initializations. 478 SourceRange getParenRange() const { 479 assert((getKind() == IK_Direct || Kind == SIK_Value) && 480 "Only direct- and value-initialization have parentheses"); 481 return SourceRange(Locations[1], Locations[2]); 482 } 483}; 484 485/// \brief Describes the sequence of initializations required to initialize 486/// a given object or reference with a set of arguments. 487class InitializationSequence { 488public: 489 /// \brief Describes the kind of initialization sequence computed. 490 enum SequenceKind { 491 /// \brief A failed initialization sequence. The failure kind tells what 492 /// happened. 493 FailedSequence = 0, 494 495 /// \brief A dependent initialization, which could not be 496 /// type-checked due to the presence of dependent types or 497 /// dependently-typed expressions. 498 DependentSequence, 499 500 /// \brief A normal sequence. 501 NormalSequence 502 }; 503 504 /// \brief Describes the kind of a particular step in an initialization 505 /// sequence. 506 enum StepKind { 507 /// \brief Resolve the address of an overloaded function to a specific 508 /// function declaration. 509 SK_ResolveAddressOfOverloadedFunction, 510 /// \brief Perform a derived-to-base cast, producing an rvalue. 511 SK_CastDerivedToBaseRValue, 512 /// \brief Perform a derived-to-base cast, producing an xvalue. 513 SK_CastDerivedToBaseXValue, 514 /// \brief Perform a derived-to-base cast, producing an lvalue. 515 SK_CastDerivedToBaseLValue, 516 /// \brief Reference binding to an lvalue. 517 SK_BindReference, 518 /// \brief Reference binding to a temporary. 519 SK_BindReferenceToTemporary, 520 /// \brief An optional copy of a temporary object to another 521 /// temporary object, which is permitted (but not required) by 522 /// C++98/03 but not C++0x. 523 SK_ExtraneousCopyToTemporary, 524 /// \brief Perform a user-defined conversion, either via a conversion 525 /// function or via a constructor. 526 SK_UserConversion, 527 /// \brief Perform a qualification conversion, producing an rvalue. 528 SK_QualificationConversionRValue, 529 /// \brief Perform a qualification conversion, producing an xvalue. 530 SK_QualificationConversionXValue, 531 /// \brief Perform a qualification conversion, producing an lvalue. 532 SK_QualificationConversionLValue, 533 /// \brief Perform an implicit conversion sequence. 534 SK_ConversionSequence, 535 /// \brief Perform list-initialization without a constructor 536 SK_ListInitialization, 537 /// \brief Perform list-initialization with a constructor. 538 SK_ListConstructorCall, 539 /// \brief Unwrap the single-element initializer list for a reference. 540 SK_UnwrapInitList, 541 /// \brief Rewrap the single-element initializer list for a reference. 542 SK_RewrapInitList, 543 /// \brief Perform initialization via a constructor. 544 SK_ConstructorInitialization, 545 /// \brief Zero-initialize the object 546 SK_ZeroInitialization, 547 /// \brief C assignment 548 SK_CAssignment, 549 /// \brief Initialization by string 550 SK_StringInit, 551 /// \brief An initialization that "converts" an Objective-C object 552 /// (not a point to an object) to another Objective-C object type. 553 SK_ObjCObjectConversion, 554 /// \brief Array initialization (from an array rvalue). 555 /// This is a GNU C extension. 556 SK_ArrayInit, 557 /// \brief Pass an object by indirect copy-and-restore. 558 SK_PassByIndirectCopyRestore, 559 /// \brief Pass an object by indirect restore. 560 SK_PassByIndirectRestore, 561 /// \brief Produce an Objective-C object pointer. 562 SK_ProduceObjCObject, 563 /// \brief Construct a std::initializer_list from an initializer list. 564 SK_StdInitializerList 565 }; 566 567 /// \brief A single step in the initialization sequence. 568 class Step { 569 public: 570 /// \brief The kind of conversion or initialization step we are taking. 571 StepKind Kind; 572 573 // \brief The type that results from this initialization. 574 QualType Type; 575 576 union { 577 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind == 578 /// SK_UserConversion, the function that the expression should be 579 /// resolved to or the conversion function to call, respectively. 580 /// When Kind == SK_ConstructorInitialization or SK_ListConstruction, 581 /// the constructor to be called. 582 /// 583 /// Always a FunctionDecl, plus a Boolean flag telling if it was 584 /// selected from an overloaded set having size greater than 1. 585 /// For conversion decls, the naming class is the source type. 586 /// For construct decls, the naming class is the target type. 587 struct { 588 bool HadMultipleCandidates; 589 FunctionDecl *Function; 590 DeclAccessPair FoundDecl; 591 } Function; 592 593 /// \brief When Kind = SK_ConversionSequence, the implicit conversion 594 /// sequence. 595 ImplicitConversionSequence *ICS; 596 597 /// \brief When Kind = SK_RewrapInitList, the syntactic form of the 598 /// wrapping list. 599 InitListExpr *WrappingSyntacticList; 600 }; 601 602 void Destroy(); 603 }; 604 605private: 606 /// \brief The kind of initialization sequence computed. 607 enum SequenceKind SequenceKind; 608 609 /// \brief Steps taken by this initialization. 610 SmallVector<Step, 4> Steps; 611 612public: 613 /// \brief Describes why initialization failed. 614 enum FailureKind { 615 /// \brief Too many initializers provided for a reference. 616 FK_TooManyInitsForReference, 617 /// \brief Array must be initialized with an initializer list. 618 FK_ArrayNeedsInitList, 619 /// \brief Array must be initialized with an initializer list or a 620 /// string literal. 621 FK_ArrayNeedsInitListOrStringLiteral, 622 /// \brief Array type mismatch. 623 FK_ArrayTypeMismatch, 624 /// \brief Non-constant array initializer 625 FK_NonConstantArrayInit, 626 /// \brief Cannot resolve the address of an overloaded function. 627 FK_AddressOfOverloadFailed, 628 /// \brief Overloading due to reference initialization failed. 629 FK_ReferenceInitOverloadFailed, 630 /// \brief Non-const lvalue reference binding to a temporary. 631 FK_NonConstLValueReferenceBindingToTemporary, 632 /// \brief Non-const lvalue reference binding to an lvalue of unrelated 633 /// type. 634 FK_NonConstLValueReferenceBindingToUnrelated, 635 /// \brief Rvalue reference binding to an lvalue. 636 FK_RValueReferenceBindingToLValue, 637 /// \brief Reference binding drops qualifiers. 638 FK_ReferenceInitDropsQualifiers, 639 /// \brief Reference binding failed. 640 FK_ReferenceInitFailed, 641 /// \brief Implicit conversion failed. 642 FK_ConversionFailed, 643 /// \brief Implicit conversion failed. 644 FK_ConversionFromPropertyFailed, 645 /// \brief Too many initializers for scalar 646 FK_TooManyInitsForScalar, 647 /// \brief Reference initialization from an initializer list 648 FK_ReferenceBindingToInitList, 649 /// \brief Initialization of some unused destination type with an 650 /// initializer list. 651 FK_InitListBadDestinationType, 652 /// \brief Overloading for a user-defined conversion failed. 653 FK_UserConversionOverloadFailed, 654 /// \brief Overloading for initialization by constructor failed. 655 FK_ConstructorOverloadFailed, 656 /// \brief Overloading for list-initialization by constructor failed. 657 FK_ListConstructorOverloadFailed, 658 /// \brief Default-initialization of a 'const' object. 659 FK_DefaultInitOfConst, 660 /// \brief Initialization of an incomplete type. 661 FK_Incomplete, 662 /// \brief Variable-length array must not have an initializer. 663 FK_VariableLengthArrayHasInitializer, 664 /// \brief List initialization failed at some point. 665 FK_ListInitializationFailed, 666 /// \brief Initializer has a placeholder type which cannot be 667 /// resolved by initialization. 668 FK_PlaceholderType, 669 /// \brief Failed to initialize a std::initializer_list because copy 670 /// construction of some element failed. 671 FK_InitListElementCopyFailure 672 }; 673 674private: 675 /// \brief The reason why initialization failed. 676 FailureKind Failure; 677 678 /// \brief The failed result of overload resolution. 679 OverloadingResult FailedOverloadResult; 680 681 /// \brief The candidate set created when initialization failed. 682 OverloadCandidateSet FailedCandidateSet; 683 684 /// \brief Prints a follow-up note that highlights the location of 685 /// the initialized entity, if it's remote. 686 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity); 687 688public: 689 /// \brief Try to perform initialization of the given entity, creating a 690 /// record of the steps required to perform the initialization. 691 /// 692 /// The generated initialization sequence will either contain enough 693 /// information to diagnose 694 /// 695 /// \param S the semantic analysis object. 696 /// 697 /// \param Entity the entity being initialized. 698 /// 699 /// \param Kind the kind of initialization being performed. 700 /// 701 /// \param Args the argument(s) provided for initialization. 702 /// 703 /// \param NumArgs the number of arguments provided for initialization. 704 InitializationSequence(Sema &S, 705 const InitializedEntity &Entity, 706 const InitializationKind &Kind, 707 Expr **Args, 708 unsigned NumArgs); 709 710 ~InitializationSequence(); 711 712 /// \brief Perform the actual initialization of the given entity based on 713 /// the computed initialization sequence. 714 /// 715 /// \param S the semantic analysis object. 716 /// 717 /// \param Entity the entity being initialized. 718 /// 719 /// \param Kind the kind of initialization being performed. 720 /// 721 /// \param Args the argument(s) provided for initialization, ownership of 722 /// which is transferred into the routine. 723 /// 724 /// \param ResultType if non-NULL, will be set to the type of the 725 /// initialized object, which is the type of the declaration in most 726 /// cases. However, when the initialized object is a variable of 727 /// incomplete array type and the initializer is an initializer 728 /// list, this type will be set to the completed array type. 729 /// 730 /// \returns an expression that performs the actual object initialization, if 731 /// the initialization is well-formed. Otherwise, emits diagnostics 732 /// and returns an invalid expression. 733 ExprResult Perform(Sema &S, 734 const InitializedEntity &Entity, 735 const InitializationKind &Kind, 736 MultiExprArg Args, 737 QualType *ResultType = 0); 738 739 /// \brief Diagnose an potentially-invalid initialization sequence. 740 /// 741 /// \returns true if the initialization sequence was ill-formed, 742 /// false otherwise. 743 bool Diagnose(Sema &S, 744 const InitializedEntity &Entity, 745 const InitializationKind &Kind, 746 Expr **Args, unsigned NumArgs); 747 748 /// \brief Determine the kind of initialization sequence computed. 749 enum SequenceKind getKind() const { return SequenceKind; } 750 751 /// \brief Set the kind of sequence computed. 752 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } 753 754 /// \brief Determine whether the initialization sequence is valid. 755 operator bool() const { return !Failed(); } 756 757 /// \brief Determine whether the initialization sequence is invalid. 758 bool Failed() const { return SequenceKind == FailedSequence; } 759 760 typedef SmallVector<Step, 4>::const_iterator step_iterator; 761 step_iterator step_begin() const { return Steps.begin(); } 762 step_iterator step_end() const { return Steps.end(); } 763 764 /// \brief Determine whether this initialization is a direct reference 765 /// binding (C++ [dcl.init.ref]). 766 bool isDirectReferenceBinding() const; 767 768 /// \brief Determine whether this initialization failed due to an ambiguity. 769 bool isAmbiguous() const; 770 771 /// \brief Determine whether this initialization is direct call to a 772 /// constructor. 773 bool isConstructorInitialization() const; 774 775 /// \brief Returns whether the last step in this initialization sequence is a 776 /// narrowing conversion, defined by C++0x [dcl.init.list]p7. 777 /// 778 /// If this function returns true, *isInitializerConstant will be set to 779 /// describe whether *Initializer was a constant expression. If 780 /// *isInitializerConstant is set to true, *ConstantValue will be set to the 781 /// evaluated value of *Initializer. 782 bool endsWithNarrowing(ASTContext &Ctx, const Expr *Initializer, 783 bool *isInitializerConstant, 784 APValue *ConstantValue) const; 785 786 /// \brief Add a new step in the initialization that resolves the address 787 /// of an overloaded function to a specific function declaration. 788 /// 789 /// \param Function the function to which the overloaded function reference 790 /// resolves. 791 void AddAddressOverloadResolutionStep(FunctionDecl *Function, 792 DeclAccessPair Found, 793 bool HadMultipleCandidates); 794 795 /// \brief Add a new step in the initialization that performs a derived-to- 796 /// base cast. 797 /// 798 /// \param BaseType the base type to which we will be casting. 799 /// 800 /// \param IsLValue true if the result of this cast will be treated as 801 /// an lvalue. 802 void AddDerivedToBaseCastStep(QualType BaseType, 803 ExprValueKind Category); 804 805 /// \brief Add a new step binding a reference to an object. 806 /// 807 /// \param BindingTemporary True if we are binding a reference to a temporary 808 /// object (thereby extending its lifetime); false if we are binding to an 809 /// lvalue or an lvalue treated as an rvalue. 810 /// 811 /// \param UnnecessaryCopy True if we should check for a copy 812 /// constructor for a completely unnecessary but 813 void AddReferenceBindingStep(QualType T, bool BindingTemporary); 814 815 /// \brief Add a new step that makes an extraneous copy of the input 816 /// to a temporary of the same class type. 817 /// 818 /// This extraneous copy only occurs during reference binding in 819 /// C++98/03, where we are permitted (but not required) to introduce 820 /// an extra copy. At a bare minimum, we must check that we could 821 /// call the copy constructor, and produce a diagnostic if the copy 822 /// constructor is inaccessible or no copy constructor matches. 823 // 824 /// \param T The type of the temporary being created. 825 void AddExtraneousCopyToTemporary(QualType T); 826 827 /// \brief Add a new step invoking a conversion function, which is either 828 /// a constructor or a conversion function. 829 void AddUserConversionStep(FunctionDecl *Function, 830 DeclAccessPair FoundDecl, 831 QualType T, 832 bool HadMultipleCandidates); 833 834 /// \brief Add a new step that performs a qualification conversion to the 835 /// given type. 836 void AddQualificationConversionStep(QualType Ty, 837 ExprValueKind Category); 838 839 /// \brief Add a new step that applies an implicit conversion sequence. 840 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, 841 QualType T); 842 843 /// \brief Add a list-initialization step. 844 void AddListInitializationStep(QualType T); 845 846 /// \brief Add a constructor-initialization step. 847 /// 848 /// \arg FromInitList The constructor call is syntactically an initializer 849 /// list. 850 /// \arg AsInitList The constructor is called as an init list constructor. 851 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor, 852 AccessSpecifier Access, 853 QualType T, 854 bool HadMultipleCandidates, 855 bool FromInitList, bool AsInitList); 856 857 /// \brief Add a zero-initialization step. 858 void AddZeroInitializationStep(QualType T); 859 860 /// \brief Add a C assignment step. 861 // 862 // FIXME: It isn't clear whether this should ever be needed; 863 // ideally, we would handle everything needed in C in the common 864 // path. However, that isn't the case yet. 865 void AddCAssignmentStep(QualType T); 866 867 /// \brief Add a string init step. 868 void AddStringInitStep(QualType T); 869 870 /// \brief Add an Objective-C object conversion step, which is 871 /// always a no-op. 872 void AddObjCObjectConversionStep(QualType T); 873 874 /// \brief Add an array initialization step. 875 void AddArrayInitStep(QualType T); 876 877 /// \brief Add a step to pass an object by indirect copy-restore. 878 void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy); 879 880 /// \brief Add a step to "produce" an Objective-C object (by 881 /// retaining it). 882 void AddProduceObjCObjectStep(QualType T); 883 884 /// \brief Add a step to construct a std::initializer_list object from an 885 /// initializer list. 886 void AddStdInitializerListConstructionStep(QualType T); 887 888 /// \brief Add steps to unwrap a initializer list for a reference around a 889 /// single element and rewrap it at the end. 890 void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic); 891 892 /// \brief Note that this initialization sequence failed. 893 void SetFailed(FailureKind Failure) { 894 SequenceKind = FailedSequence; 895 this->Failure = Failure; 896 } 897 898 /// \brief Note that this initialization sequence failed due to failed 899 /// overload resolution. 900 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); 901 902 /// \brief Retrieve a reference to the candidate set when overload 903 /// resolution fails. 904 OverloadCandidateSet &getFailedCandidateSet() { 905 return FailedCandidateSet; 906 } 907 908 /// \brief Get the overloading result, for when the initialization 909 /// sequence failed due to a bad overload. 910 OverloadingResult getFailedOverloadResult() const { 911 return FailedOverloadResult; 912 } 913 914 /// \brief Determine why initialization failed. 915 FailureKind getFailureKind() const { 916 assert(Failed() && "Not an initialization failure!"); 917 return Failure; 918 } 919 920 /// \brief Dump a representation of this initialization sequence to 921 /// the given stream, for debugging purposes. 922 void dump(raw_ostream &OS) const; 923 924 /// \brief Dump a representation of this initialization sequence to 925 /// standard error, for debugging purposes. 926 void dump() const; 927}; 928 929} // end namespace clang 930 931#endif // LLVM_CLANG_SEMA_INITIALIZATION_H 932