Initialization.h revision 3a45c0e61dfc19f27b8ebcb15dd70159a36f1f9a
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 context of the initialization. 338 enum InitContext { 339 IC_Normal, ///< Normal context 340 IC_Implicit, ///< Implicit context (value initialization) 341 IC_StaticCast, ///< Static cast context 342 IC_CStyleCast, ///< C-style cast context 343 IC_FunctionalCast ///< Functional cast context 344 }; 345 346 /// \brief The kind of initialization being performed. 347 InitKind Kind : 8; 348 349 /// \brief The context of the initialization. 350 InitContext Context : 8; 351 352 /// \brief The source locations involved in the initialization. 353 SourceLocation Locations[3]; 354 355 InitializationKind(InitKind Kind, InitContext Context, SourceLocation Loc1, 356 SourceLocation Loc2, SourceLocation Loc3) 357 : Kind(Kind), Context(Context) 358 { 359 Locations[0] = Loc1; 360 Locations[1] = Loc2; 361 Locations[2] = Loc3; 362 } 363 364public: 365 /// \brief Create a direct initialization. 366 static InitializationKind CreateDirect(SourceLocation InitLoc, 367 SourceLocation LParenLoc, 368 SourceLocation RParenLoc) { 369 return InitializationKind(IK_Direct, IC_Normal, 370 InitLoc, LParenLoc, RParenLoc); 371 } 372 373 static InitializationKind CreateDirectList(SourceLocation InitLoc) { 374 return InitializationKind(IK_DirectList, IC_Normal, 375 InitLoc, InitLoc, InitLoc); 376 } 377 378 /// \brief Create a direct initialization due to a cast that isn't a C-style 379 /// or functional cast. 380 static InitializationKind CreateCast(SourceRange TypeRange) { 381 return InitializationKind(IK_Direct, IC_StaticCast, TypeRange.getBegin(), 382 TypeRange.getBegin(), TypeRange.getEnd()); 383 } 384 385 /// \brief Create a direct initialization for a C-style cast. 386 static InitializationKind CreateCStyleCast(SourceLocation StartLoc, 387 SourceRange TypeRange, 388 bool InitList) { 389 // C++ cast syntax doesn't permit init lists, but C compound literals are 390 // exactly that. 391 return InitializationKind(InitList ? IK_DirectList : IK_Direct, 392 IC_CStyleCast, StartLoc, TypeRange.getBegin(), 393 TypeRange.getEnd()); 394 } 395 396 /// \brief Create a direct initialization for a functional cast. 397 static InitializationKind CreateFunctionalCast(SourceRange TypeRange, 398 bool InitList) { 399 return InitializationKind(InitList ? IK_DirectList : IK_Direct, 400 IC_FunctionalCast, TypeRange.getBegin(), 401 TypeRange.getBegin(), TypeRange.getEnd()); 402 } 403 404 /// \brief Create a copy initialization. 405 static InitializationKind CreateCopy(SourceLocation InitLoc, 406 SourceLocation EqualLoc) { 407 return InitializationKind(IK_Copy, IC_Normal, InitLoc, EqualLoc, EqualLoc); 408 } 409 410 /// \brief Create a default initialization. 411 static InitializationKind CreateDefault(SourceLocation InitLoc) { 412 return InitializationKind(IK_Default, IC_Normal, InitLoc, InitLoc, InitLoc); 413 } 414 415 /// \brief Create a value initialization. 416 static InitializationKind CreateValue(SourceLocation InitLoc, 417 SourceLocation LParenLoc, 418 SourceLocation RParenLoc, 419 bool isImplicit = false) { 420 return InitializationKind(IK_Value, isImplicit ? IC_Implicit : IC_Normal, 421 InitLoc, LParenLoc, RParenLoc); 422 } 423 424 /// \brief Determine the initialization kind. 425 InitKind getKind() const { 426 return Kind; 427 } 428 429 /// \brief Determine whether this initialization is an explicit cast. 430 bool isExplicitCast() const { 431 return Context >= IC_StaticCast; 432 } 433 434 /// \brief Determine whether this initialization is a C-style cast. 435 bool isCStyleOrFunctionalCast() const { 436 return Context >= IC_CStyleCast; 437 } 438 439 /// \brief Determine whether this is a C-style cast. 440 bool isCStyleCast() const { 441 return Context == IC_CStyleCast; 442 } 443 444 /// \brief Determine whether this is a functional-style cast. 445 bool isFunctionalCast() const { 446 return Context == IC_FunctionalCast; 447 } 448 449 /// \brief Determine whether this initialization is an implicit 450 /// value-initialization, e.g., as occurs during aggregate 451 /// initialization. 452 bool isImplicitValueInit() const { return Context == IC_Implicit; } 453 454 /// \brief Retrieve the location at which initialization is occurring. 455 SourceLocation getLocation() const { return Locations[0]; } 456 457 /// \brief Retrieve the source range that covers the initialization. 458 SourceRange getRange() const { 459 return SourceRange(Locations[0], Locations[2]); 460 } 461 462 /// \brief Retrieve the location of the equal sign for copy initialization 463 /// (if present). 464 SourceLocation getEqualLoc() const { 465 assert(Kind == IK_Copy && "Only copy initialization has an '='"); 466 return Locations[1]; 467 } 468 469 bool isCopyInit() const { return Kind == IK_Copy; } 470 471 /// \brief Retrieve whether this initialization allows the use of explicit 472 /// constructors. 473 bool AllowExplicit() const { return !isCopyInit(); } 474 475 /// \brief Retrieve the source range containing the locations of the open 476 /// and closing parentheses for value and direct initializations. 477 SourceRange getParenRange() const { 478 assert((Kind == IK_Direct || Kind == IK_Value) && 479 "Only direct- and value-initialization have parentheses"); 480 return SourceRange(Locations[1], Locations[2]); 481 } 482}; 483 484/// \brief Describes the sequence of initializations required to initialize 485/// a given object or reference with a set of arguments. 486class InitializationSequence { 487public: 488 /// \brief Describes the kind of initialization sequence computed. 489 enum SequenceKind { 490 /// \brief A failed initialization sequence. The failure kind tells what 491 /// happened. 492 FailedSequence = 0, 493 494 /// \brief A dependent initialization, which could not be 495 /// type-checked due to the presence of dependent types or 496 /// dependently-typed expressions. 497 DependentSequence, 498 499 /// \brief A normal sequence. 500 NormalSequence 501 }; 502 503 /// \brief Describes the kind of a particular step in an initialization 504 /// sequence. 505 enum StepKind { 506 /// \brief Resolve the address of an overloaded function to a specific 507 /// function declaration. 508 SK_ResolveAddressOfOverloadedFunction, 509 /// \brief Perform a derived-to-base cast, producing an rvalue. 510 SK_CastDerivedToBaseRValue, 511 /// \brief Perform a derived-to-base cast, producing an xvalue. 512 SK_CastDerivedToBaseXValue, 513 /// \brief Perform a derived-to-base cast, producing an lvalue. 514 SK_CastDerivedToBaseLValue, 515 /// \brief Reference binding to an lvalue. 516 SK_BindReference, 517 /// \brief Reference binding to a temporary. 518 SK_BindReferenceToTemporary, 519 /// \brief An optional copy of a temporary object to another 520 /// temporary object, which is permitted (but not required) by 521 /// C++98/03 but not C++0x. 522 SK_ExtraneousCopyToTemporary, 523 /// \brief Perform a user-defined conversion, either via a conversion 524 /// function or via a constructor. 525 SK_UserConversion, 526 /// \brief Perform a qualification conversion, producing an rvalue. 527 SK_QualificationConversionRValue, 528 /// \brief Perform a qualification conversion, producing an xvalue. 529 SK_QualificationConversionXValue, 530 /// \brief Perform a qualification conversion, producing an lvalue. 531 SK_QualificationConversionLValue, 532 /// \brief Perform an implicit conversion sequence. 533 SK_ConversionSequence, 534 /// \brief Perform list-initialization without a constructor 535 SK_ListInitialization, 536 /// \brief Perform list-initialization with a constructor. 537 SK_ListConstructorCall, 538 /// \brief Unwrap the single-element initializer list for a reference. 539 SK_UnwrapInitList, 540 /// \brief Rewrap the single-element initializer list for a reference. 541 SK_RewrapInitList, 542 /// \brief Perform initialization via a constructor. 543 SK_ConstructorInitialization, 544 /// \brief Zero-initialize the object 545 SK_ZeroInitialization, 546 /// \brief C assignment 547 SK_CAssignment, 548 /// \brief Initialization by string 549 SK_StringInit, 550 /// \brief An initialization that "converts" an Objective-C object 551 /// (not a point to an object) to another Objective-C object type. 552 SK_ObjCObjectConversion, 553 /// \brief Array initialization (from an array rvalue). 554 /// This is a GNU C extension. 555 SK_ArrayInit, 556 /// \brief Pass an object by indirect copy-and-restore. 557 SK_PassByIndirectCopyRestore, 558 /// \brief Pass an object by indirect restore. 559 SK_PassByIndirectRestore, 560 /// \brief Produce an Objective-C object pointer. 561 SK_ProduceObjCObject, 562 /// \brief Construct a std::initializer_list from an initializer list. 563 SK_StdInitializerList 564 }; 565 566 /// \brief A single step in the initialization sequence. 567 class Step { 568 public: 569 /// \brief The kind of conversion or initialization step we are taking. 570 StepKind Kind; 571 572 // \brief The type that results from this initialization. 573 QualType Type; 574 575 union { 576 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind == 577 /// SK_UserConversion, the function that the expression should be 578 /// resolved to or the conversion function to call, respectively. 579 /// When Kind == SK_ConstructorInitialization or SK_ListConstruction, 580 /// the constructor to be called. 581 /// 582 /// Always a FunctionDecl, plus a Boolean flag telling if it was 583 /// selected from an overloaded set having size greater than 1. 584 /// For conversion decls, the naming class is the source type. 585 /// For construct decls, the naming class is the target type. 586 struct { 587 bool HadMultipleCandidates; 588 FunctionDecl *Function; 589 DeclAccessPair FoundDecl; 590 } Function; 591 592 /// \brief When Kind = SK_ConversionSequence, the implicit conversion 593 /// sequence. 594 ImplicitConversionSequence *ICS; 595 596 /// \brief When Kind = SK_RewrapInitList, the syntactic form of the 597 /// wrapping list. 598 InitListExpr *WrappingSyntacticList; 599 }; 600 601 void Destroy(); 602 }; 603 604private: 605 /// \brief The kind of initialization sequence computed. 606 enum SequenceKind SequenceKind; 607 608 /// \brief Steps taken by this initialization. 609 SmallVector<Step, 4> Steps; 610 611public: 612 /// \brief Describes why initialization failed. 613 enum FailureKind { 614 /// \brief Too many initializers provided for a reference. 615 FK_TooManyInitsForReference, 616 /// \brief Array must be initialized with an initializer list. 617 FK_ArrayNeedsInitList, 618 /// \brief Array must be initialized with an initializer list or a 619 /// string literal. 620 FK_ArrayNeedsInitListOrStringLiteral, 621 /// \brief Array type mismatch. 622 FK_ArrayTypeMismatch, 623 /// \brief Non-constant array initializer 624 FK_NonConstantArrayInit, 625 /// \brief Cannot resolve the address of an overloaded function. 626 FK_AddressOfOverloadFailed, 627 /// \brief Overloading due to reference initialization failed. 628 FK_ReferenceInitOverloadFailed, 629 /// \brief Non-const lvalue reference binding to a temporary. 630 FK_NonConstLValueReferenceBindingToTemporary, 631 /// \brief Non-const lvalue reference binding to an lvalue of unrelated 632 /// type. 633 FK_NonConstLValueReferenceBindingToUnrelated, 634 /// \brief Rvalue reference binding to an lvalue. 635 FK_RValueReferenceBindingToLValue, 636 /// \brief Reference binding drops qualifiers. 637 FK_ReferenceInitDropsQualifiers, 638 /// \brief Reference binding failed. 639 FK_ReferenceInitFailed, 640 /// \brief Implicit conversion failed. 641 FK_ConversionFailed, 642 /// \brief Implicit conversion failed. 643 FK_ConversionFromPropertyFailed, 644 /// \brief Too many initializers for scalar 645 FK_TooManyInitsForScalar, 646 /// \brief Reference initialization from an initializer list 647 FK_ReferenceBindingToInitList, 648 /// \brief Initialization of some unused destination type with an 649 /// initializer list. 650 FK_InitListBadDestinationType, 651 /// \brief Overloading for a user-defined conversion failed. 652 FK_UserConversionOverloadFailed, 653 /// \brief Overloading for initialization by constructor failed. 654 FK_ConstructorOverloadFailed, 655 /// \brief Overloading for list-initialization by constructor failed. 656 FK_ListConstructorOverloadFailed, 657 /// \brief Default-initialization of a 'const' object. 658 FK_DefaultInitOfConst, 659 /// \brief Initialization of an incomplete type. 660 FK_Incomplete, 661 /// \brief Variable-length array must not have an initializer. 662 FK_VariableLengthArrayHasInitializer, 663 /// \brief List initialization failed at some point. 664 FK_ListInitializationFailed, 665 /// \brief Initializer has a placeholder type which cannot be 666 /// resolved by initialization. 667 FK_PlaceholderType, 668 /// \brief Failed to initialize a std::initializer_list because copy 669 /// construction of some element failed. 670 FK_InitListElementCopyFailure 671 }; 672 673private: 674 /// \brief The reason why initialization failed. 675 FailureKind Failure; 676 677 /// \brief The failed result of overload resolution. 678 OverloadingResult FailedOverloadResult; 679 680 /// \brief The candidate set created when initialization failed. 681 OverloadCandidateSet FailedCandidateSet; 682 683 /// \brief Prints a follow-up note that highlights the location of 684 /// the initialized entity, if it's remote. 685 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity); 686 687public: 688 /// \brief Try to perform initialization of the given entity, creating a 689 /// record of the steps required to perform the initialization. 690 /// 691 /// The generated initialization sequence will either contain enough 692 /// information to diagnose 693 /// 694 /// \param S the semantic analysis object. 695 /// 696 /// \param Entity the entity being initialized. 697 /// 698 /// \param Kind the kind of initialization being performed. 699 /// 700 /// \param Args the argument(s) provided for initialization. 701 /// 702 /// \param NumArgs the number of arguments provided for initialization. 703 InitializationSequence(Sema &S, 704 const InitializedEntity &Entity, 705 const InitializationKind &Kind, 706 Expr **Args, 707 unsigned NumArgs); 708 709 ~InitializationSequence(); 710 711 /// \brief Perform the actual initialization of the given entity based on 712 /// the computed initialization sequence. 713 /// 714 /// \param S the semantic analysis object. 715 /// 716 /// \param Entity the entity being initialized. 717 /// 718 /// \param Kind the kind of initialization being performed. 719 /// 720 /// \param Args the argument(s) provided for initialization, ownership of 721 /// which is transferred into the routine. 722 /// 723 /// \param ResultType if non-NULL, will be set to the type of the 724 /// initialized object, which is the type of the declaration in most 725 /// cases. However, when the initialized object is a variable of 726 /// incomplete array type and the initializer is an initializer 727 /// list, this type will be set to the completed array type. 728 /// 729 /// \returns an expression that performs the actual object initialization, if 730 /// the initialization is well-formed. Otherwise, emits diagnostics 731 /// and returns an invalid expression. 732 ExprResult Perform(Sema &S, 733 const InitializedEntity &Entity, 734 const InitializationKind &Kind, 735 MultiExprArg Args, 736 QualType *ResultType = 0); 737 738 /// \brief Diagnose an potentially-invalid initialization sequence. 739 /// 740 /// \returns true if the initialization sequence was ill-formed, 741 /// false otherwise. 742 bool Diagnose(Sema &S, 743 const InitializedEntity &Entity, 744 const InitializationKind &Kind, 745 Expr **Args, unsigned NumArgs); 746 747 /// \brief Determine the kind of initialization sequence computed. 748 enum SequenceKind getKind() const { return SequenceKind; } 749 750 /// \brief Set the kind of sequence computed. 751 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } 752 753 /// \brief Determine whether the initialization sequence is valid. 754 operator bool() const { return !Failed(); } 755 756 /// \brief Determine whether the initialization sequence is invalid. 757 bool Failed() const { return SequenceKind == FailedSequence; } 758 759 typedef SmallVector<Step, 4>::const_iterator step_iterator; 760 step_iterator step_begin() const { return Steps.begin(); } 761 step_iterator step_end() const { return Steps.end(); } 762 763 /// \brief Determine whether this initialization is a direct reference 764 /// binding (C++ [dcl.init.ref]). 765 bool isDirectReferenceBinding() const; 766 767 /// \brief Determine whether this initialization failed due to an ambiguity. 768 bool isAmbiguous() const; 769 770 /// \brief Determine whether this initialization is direct call to a 771 /// constructor. 772 bool isConstructorInitialization() const; 773 774 /// \brief Returns whether the last step in this initialization sequence is a 775 /// narrowing conversion, defined by C++0x [dcl.init.list]p7. 776 /// 777 /// If this function returns true, *isInitializerConstant will be set to 778 /// describe whether *Initializer was a constant expression. If 779 /// *isInitializerConstant is set to true, *ConstantValue will be set to the 780 /// evaluated value of *Initializer. 781 bool endsWithNarrowing(ASTContext &Ctx, const Expr *Initializer, 782 bool *isInitializerConstant, 783 APValue *ConstantValue) const; 784 785 /// \brief Add a new step in the initialization that resolves the address 786 /// of an overloaded function to a specific function declaration. 787 /// 788 /// \param Function the function to which the overloaded function reference 789 /// resolves. 790 void AddAddressOverloadResolutionStep(FunctionDecl *Function, 791 DeclAccessPair Found, 792 bool HadMultipleCandidates); 793 794 /// \brief Add a new step in the initialization that performs a derived-to- 795 /// base cast. 796 /// 797 /// \param BaseType the base type to which we will be casting. 798 /// 799 /// \param IsLValue true if the result of this cast will be treated as 800 /// an lvalue. 801 void AddDerivedToBaseCastStep(QualType BaseType, 802 ExprValueKind Category); 803 804 /// \brief Add a new step binding a reference to an object. 805 /// 806 /// \param BindingTemporary True if we are binding a reference to a temporary 807 /// object (thereby extending its lifetime); false if we are binding to an 808 /// lvalue or an lvalue treated as an rvalue. 809 /// 810 /// \param UnnecessaryCopy True if we should check for a copy 811 /// constructor for a completely unnecessary but 812 void AddReferenceBindingStep(QualType T, bool BindingTemporary); 813 814 /// \brief Add a new step that makes an extraneous copy of the input 815 /// to a temporary of the same class type. 816 /// 817 /// This extraneous copy only occurs during reference binding in 818 /// C++98/03, where we are permitted (but not required) to introduce 819 /// an extra copy. At a bare minimum, we must check that we could 820 /// call the copy constructor, and produce a diagnostic if the copy 821 /// constructor is inaccessible or no copy constructor matches. 822 // 823 /// \param T The type of the temporary being created. 824 void AddExtraneousCopyToTemporary(QualType T); 825 826 /// \brief Add a new step invoking a conversion function, which is either 827 /// a constructor or a conversion function. 828 void AddUserConversionStep(FunctionDecl *Function, 829 DeclAccessPair FoundDecl, 830 QualType T, 831 bool HadMultipleCandidates); 832 833 /// \brief Add a new step that performs a qualification conversion to the 834 /// given type. 835 void AddQualificationConversionStep(QualType Ty, 836 ExprValueKind Category); 837 838 /// \brief Add a new step that applies an implicit conversion sequence. 839 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, 840 QualType T); 841 842 /// \brief Add a list-initialization step. 843 void AddListInitializationStep(QualType T); 844 845 /// \brief Add a constructor-initialization step. 846 /// 847 /// \arg FromInitList The constructor call is syntactically an initializer 848 /// list. 849 /// \arg AsInitList The constructor is called as an init list constructor. 850 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor, 851 AccessSpecifier Access, 852 QualType T, 853 bool HadMultipleCandidates, 854 bool FromInitList, bool AsInitList); 855 856 /// \brief Add a zero-initialization step. 857 void AddZeroInitializationStep(QualType T); 858 859 /// \brief Add a C assignment step. 860 // 861 // FIXME: It isn't clear whether this should ever be needed; 862 // ideally, we would handle everything needed in C in the common 863 // path. However, that isn't the case yet. 864 void AddCAssignmentStep(QualType T); 865 866 /// \brief Add a string init step. 867 void AddStringInitStep(QualType T); 868 869 /// \brief Add an Objective-C object conversion step, which is 870 /// always a no-op. 871 void AddObjCObjectConversionStep(QualType T); 872 873 /// \brief Add an array initialization step. 874 void AddArrayInitStep(QualType T); 875 876 /// \brief Add a step to pass an object by indirect copy-restore. 877 void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy); 878 879 /// \brief Add a step to "produce" an Objective-C object (by 880 /// retaining it). 881 void AddProduceObjCObjectStep(QualType T); 882 883 /// \brief Add a step to construct a std::initializer_list object from an 884 /// initializer list. 885 void AddStdInitializerListConstructionStep(QualType T); 886 887 /// \brief Add steps to unwrap a initializer list for a reference around a 888 /// single element and rewrap it at the end. 889 void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic); 890 891 /// \brief Note that this initialization sequence failed. 892 void SetFailed(FailureKind Failure) { 893 SequenceKind = FailedSequence; 894 this->Failure = Failure; 895 } 896 897 /// \brief Note that this initialization sequence failed due to failed 898 /// overload resolution. 899 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); 900 901 /// \brief Retrieve a reference to the candidate set when overload 902 /// resolution fails. 903 OverloadCandidateSet &getFailedCandidateSet() { 904 return FailedCandidateSet; 905 } 906 907 /// \brief Get the overloading result, for when the initialization 908 /// sequence failed due to a bad overload. 909 OverloadingResult getFailedOverloadResult() const { 910 return FailedOverloadResult; 911 } 912 913 /// \brief Determine why initialization failed. 914 FailureKind getFailureKind() const { 915 assert(Failed() && "Not an initialization failure!"); 916 return Failure; 917 } 918 919 /// \brief Dump a representation of this initialization sequence to 920 /// the given stream, for debugging purposes. 921 void dump(raw_ostream &OS) const; 922 923 /// \brief Dump a representation of this initialization sequence to 924 /// standard error, for debugging purposes. 925 void dump() const; 926}; 927 928} // end namespace clang 929 930#endif // LLVM_CLANG_SEMA_INITIALIZATION_H 931