Initialization.h revision 212904
1274201Sdim//===--- SemaInit.h - Semantic Analysis for Initializers --------*- C++ -*-===// 2274201Sdim// 3274201Sdim// The LLVM Compiler Infrastructure 4276789Sdim// 5276789Sdim// This file is distributed under the University of Illinois Open Source 6276789Sdim// License. See LICENSE.TXT for details. 7274201Sdim// 8274201Sdim//===----------------------------------------------------------------------===// 9274201Sdim// 10274201Sdim// This file provides supporting data types for initialization of objects. 11274201Sdim// 12274201Sdim//===----------------------------------------------------------------------===// 13274201Sdim#ifndef LLVM_CLANG_SEMA_INITIALIZATION_H 14274201Sdim#define LLVM_CLANG_SEMA_INITIALIZATION_H 15274201Sdim 16274201Sdim#include "clang/Sema/Ownership.h" 17274201Sdim#include "clang/Sema/Overload.h" 18274201Sdim#include "clang/AST/Type.h" 19276789Sdim#include "clang/AST/UnresolvedSet.h" 20276789Sdim#include "clang/Basic/SourceLocation.h" 21274201Sdim#include "llvm/ADT/PointerIntPair.h" 22274201Sdim#include "llvm/ADT/SmallVector.h" 23274201Sdim#include <cassert> 24274201Sdim 25274201Sdimnamespace llvm { 26274201Sdim class raw_ostream; 27276789Sdim} 28276789Sdim 29353358Sdimnamespace clang { 30353358Sdim 31274201Sdimclass CXXBaseSpecifier; 32276789Sdimclass DeclaratorDecl; 33276789Sdimclass DeclaratorInfo; 34276789Sdimclass FieldDecl; 35274201Sdimclass FunctionDecl; 36274201Sdimclass ParmVarDecl; 37274201Sdimclass Sema; 38276789Sdimclass TypeLoc; 39276789Sdimclass VarDecl; 40276789Sdim 41274201Sdim/// \brief Describes an entity that is being initialized. 42274201Sdimclass InitializedEntity { 43274201Sdimpublic: 44274201Sdim /// \brief Specifies the kind of entity being initialized. 45274201Sdim enum EntityKind { 46274201Sdim /// \brief The entity being initialized is a variable. 47274201Sdim EK_Variable, 48274201Sdim /// \brief The entity being initialized is a function parameter. 49274201Sdim EK_Parameter, 50276789Sdim /// \brief The entity being initialized is the result of a function call. 51276789Sdim EK_Result, 52276789Sdim /// \brief The entity being initialized is an exception object that 53274201Sdim /// is being thrown. 54274201Sdim EK_Exception, 55276789Sdim /// \brief The entity being initialized is a non-static data member 56276789Sdim /// subobject. 57276789Sdim EK_Member, 58276789Sdim /// \brief The entity being initialized is an element of an array. 59276789Sdim EK_ArrayElement, 60276789Sdim /// \brief The entity being initialized is an object (or array of 61274201Sdim /// objects) allocated via new. 62276789Sdim EK_New, 63276789Sdim /// \brief The entity being initialized is a temporary object. 64276789Sdim EK_Temporary, 65274201Sdim /// \brief The entity being initialized is a base member subobject. 66276789Sdim EK_Base, 67276789Sdim /// \brief The entity being initialized is an element of a vector. 68276789Sdim /// or vector. 69276789Sdim EK_VectorElement, 70276789Sdim /// \brief The entity being initialized is a field of block descriptor for 71276789Sdim /// the copied-in c++ object. 72274201Sdim EK_BlockElement 73274201Sdim }; 74276789Sdim 75276789Sdimprivate: 76276789Sdim /// \brief The kind of entity being initialized. 77276789Sdim EntityKind Kind; 78276789Sdim 79276789Sdim /// \brief If non-NULL, the parent entity in which this 80276789Sdim /// initialization occurs. 81276789Sdim const InitializedEntity *Parent; 82276789Sdim 83276789Sdim /// \brief The type of the object or reference being initialized. 84274201Sdim QualType Type; 85276789Sdim 86276789Sdim union { 87276789Sdim /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member, 88276789Sdim /// the VarDecl, ParmVarDecl, or FieldDecl, respectively. 89276789Sdim DeclaratorDecl *VariableOrMember; 90276789Sdim 91276789Sdim struct { 92276789Sdim /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the 93276789Sdim /// location of the 'return', 'throw', or 'new' keyword, 94276789Sdim /// respectively. When Kind == EK_Temporary, the location where 95274201Sdim /// the temporary is being created. 96276789Sdim unsigned Location; 97276789Sdim 98274201Sdim /// \brief Whether the 99276789Sdim bool NRVO; 100274201Sdim } LocAndNRVO; 101274201Sdim 102276789Sdim /// \brief When Kind == EK_Base, the base specifier that provides the 103276789Sdim /// base class. The lower bit specifies whether the base is an inherited 104276789Sdim /// virtual base. 105276789Sdim uintptr_t Base; 106274201Sdim 107276789Sdim /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the 108276789Sdim /// index of the array or vector element being initialized. 109276789Sdim unsigned Index; 110276789Sdim }; 111276789Sdim 112276789Sdim InitializedEntity() { } 113276789Sdim 114276789Sdim /// \brief Create the initialization entity for a variable. 115276789Sdim InitializedEntity(VarDecl *Var) 116276789Sdim : Kind(EK_Variable), Parent(0), Type(Var->getType()), 117276789Sdim VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { } 118276789Sdim 119276789Sdim /// \brief Create the initialization entity for a parameter. 120276789Sdim InitializedEntity(ParmVarDecl *Parm) 121276789Sdim : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()), 122276789Sdim VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { } 123276789Sdim 124276789Sdim /// \brief Create the initialization entity for the result of a 125276789Sdim /// function, throwing an object, performing an explicit cast, or 126276789Sdim /// initializing a parameter for which there is no declaration. 127274201Sdim InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type, 128276789Sdim bool NRVO = false) 129274201Sdim : Kind(Kind), Parent(0), Type(Type) 130276789Sdim { 131276789Sdim LocAndNRVO.Location = Loc.getRawEncoding(); 132274201Sdim LocAndNRVO.NRVO = NRVO; 133274201Sdim } 134274201Sdim 135276789Sdim /// \brief Create the initialization entity for a member subobject. 136276789Sdim InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent) 137276789Sdim : Kind(EK_Member), Parent(Parent), Type(Member->getType()), 138276789Sdim VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { } 139276789Sdim 140276789Sdim /// \brief Create the initialization entity for an array element. 141276789Sdim InitializedEntity(ASTContext &Context, unsigned Index, 142276789Sdim const InitializedEntity &Parent); 143276789Sdim 144276789Sdimpublic: 145276789Sdim /// \brief Create the initialization entity for a variable. 146276789Sdim static InitializedEntity InitializeVariable(VarDecl *Var) { 147276789Sdim return InitializedEntity(Var); 148274201Sdim } 149274201Sdim 150276789Sdim /// \brief Create the initialization entity for a parameter. 151276789Sdim static InitializedEntity InitializeParameter(ParmVarDecl *Parm) { 152276789Sdim return InitializedEntity(Parm); 153276789Sdim } 154276789Sdim 155276789Sdim /// \brief Create the initialization entity for a parameter that is 156276789Sdim /// only known by its type. 157276789Sdim static InitializedEntity InitializeParameter(QualType Type) { 158276789Sdim InitializedEntity Entity; 159276789Sdim Entity.Kind = EK_Parameter; 160276789Sdim Entity.Type = Type; 161276789Sdim Entity.Parent = 0; 162276789Sdim Entity.VariableOrMember = 0; 163276789Sdim return Entity; 164276789Sdim } 165276789Sdim 166274201Sdim /// \brief Create the initialization entity for the result of a function. 167276789Sdim static InitializedEntity InitializeResult(SourceLocation ReturnLoc, 168276789Sdim QualType Type, bool NRVO) { 169276789Sdim return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO); 170276789Sdim } 171276789Sdim 172276789Sdim static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc, 173276789Sdim QualType Type, bool NRVO) { 174274201Sdim return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO); 175274201Sdim } 176276789Sdim 177274201Sdim /// \brief Create the initialization entity for an exception object. 178274201Sdim static InitializedEntity InitializeException(SourceLocation ThrowLoc, 179276789Sdim QualType Type, bool NRVO) { 180276789Sdim return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO); 181276789Sdim } 182276789Sdim 183276789Sdim /// \brief Create the initialization entity for an object allocated via new. 184276789Sdim static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) { 185276789Sdim return InitializedEntity(EK_New, NewLoc, Type); 186276789Sdim } 187276789Sdim 188276789Sdim /// \brief Create the initialization entity for a temporary. 189274201Sdim static InitializedEntity InitializeTemporary(QualType Type) { 190274201Sdim return InitializedEntity(EK_Temporary, SourceLocation(), Type); 191276789Sdim } 192274201Sdim 193276789Sdim /// \brief Create the initialization entity for a base class subobject. 194276789Sdim static InitializedEntity InitializeBase(ASTContext &Context, 195276789Sdim CXXBaseSpecifier *Base, 196276789Sdim bool IsInheritedVirtualBase); 197276789Sdim 198276789Sdim /// \brief Create the initialization entity for a member subobject. 199276789Sdim static InitializedEntity InitializeMember(FieldDecl *Member, 200276789Sdim const InitializedEntity *Parent = 0) { 201276789Sdim return InitializedEntity(Member, Parent); 202276789Sdim } 203274201Sdim 204274201Sdim /// \brief Create the initialization entity for an array element. 205276789Sdim static InitializedEntity InitializeElement(ASTContext &Context, 206276789Sdim unsigned Index, 207276789Sdim const InitializedEntity &Parent) { 208276789Sdim return InitializedEntity(Context, Index, Parent); 209276789Sdim } 210276789Sdim 211276789Sdim /// \brief Determine the kind of initialization. 212276789Sdim EntityKind getKind() const { return Kind; } 213276789Sdim 214276789Sdim /// \brief Retrieve the parent of the entity being initialized, when 215276789Sdim /// the initialization itself is occuring within the context of a 216276789Sdim /// larger initialization. 217274201Sdim const InitializedEntity *getParent() const { return Parent; } 218274201Sdim 219274201Sdim /// \brief Retrieve type being initialized. 220276789Sdim QualType getType() const { return Type; } 221276789Sdim 222276789Sdim /// \brief Retrieve the name of the entity being initialized. 223276789Sdim DeclarationName getName() const; 224276789Sdim 225276789Sdim /// \brief Retrieve the variable, parameter, or field being 226276789Sdim /// initialized. 227276789Sdim DeclaratorDecl *getDecl() const; 228276789Sdim 229276789Sdim /// \brief Determine whether this initialization allows the named return 230276789Sdim /// value optimization, which also applies to thrown objects. 231276789Sdim bool allowsNRVO() const; 232276789Sdim 233276789Sdim /// \brief Retrieve the base specifier. 234276789Sdim CXXBaseSpecifier *getBaseSpecifier() const { 235276789Sdim assert(getKind() == EK_Base && "Not a base specifier"); 236276789Sdim return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1); 237276789Sdim } 238276789Sdim 239276789Sdim /// \brief Return whether the base is an inherited virtual base. 240276789Sdim bool isInheritedVirtualBase() const { 241276789Sdim assert(getKind() == EK_Base && "Not a base specifier"); 242276789Sdim return Base & 0x1; 243276789Sdim } 244276789Sdim 245276789Sdim /// \brief Determine the location of the 'return' keyword when initializing 246276789Sdim /// the result of a function call. 247276789Sdim SourceLocation getReturnLoc() const { 248276789Sdim assert(getKind() == EK_Result && "No 'return' location!"); 249276789Sdim return SourceLocation::getFromRawEncoding(LocAndNRVO.Location); 250276789Sdim } 251276789Sdim 252276789Sdim /// \brief Determine the location of the 'throw' keyword when initializing 253276789Sdim /// an exception object. 254276789Sdim SourceLocation getThrowLoc() const { 255276789Sdim assert(getKind() == EK_Exception && "No 'throw' location!"); 256276789Sdim return SourceLocation::getFromRawEncoding(LocAndNRVO.Location); 257276789Sdim } 258276789Sdim 259276789Sdim /// \brief If this is already the initializer for an array or vector 260276789Sdim /// element, sets the element index. 261276789Sdim void setElementIndex(unsigned Index) { 262276789Sdim assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement); 263288943Sdim this->Index = Index; 264288943Sdim } 265288943Sdim}; 266288943Sdim 267288943Sdim/// \brief Describes the kind of initialization being performed, along with 268288943Sdim/// location information for tokens related to the initialization (equal sign, 269314564Sdim/// parentheses). 270314564Sdimclass InitializationKind { 271314564Sdimpublic: 272314564Sdim /// \brief The kind of initialization being performed. 273314564Sdim enum InitKind { 274314564Sdim IK_Direct, ///< Direct initialization 275314564Sdim IK_Copy, ///< Copy initialization 276314564Sdim IK_Default, ///< Default initialization 277341825Sdim IK_Value ///< Value initialization 278341825Sdim }; 279341825Sdim 280341825Sdimprivate: 281341825Sdim /// \brief The kind of initialization that we're storing. 282341825Sdim enum StoredInitKind { 283341825Sdim SIK_Direct = IK_Direct, ///< Direct initialization 284341825Sdim SIK_Copy = IK_Copy, ///< Copy initialization 285296417Sdim SIK_Default = IK_Default, ///< Default initialization 286296417Sdim SIK_Value = IK_Value, ///< Value initialization 287296417Sdim SIK_ImplicitValue, ///< Implicit value initialization 288288943Sdim SIK_DirectCast, ///< Direct initialization due to a cast 289288943Sdim /// \brief Direct initialization due to a C-style or functional cast. 290296417Sdim SIK_DirectCStyleOrFunctionalCast 291296417Sdim }; 292296417Sdim 293296417Sdim /// \brief The kind of initialization being performed. 294296417Sdim StoredInitKind Kind; 295296417Sdim 296296417Sdim /// \brief The source locations involved in the initialization. 297296417Sdim SourceLocation Locations[3]; 298296417Sdim 299296417Sdim InitializationKind(StoredInitKind Kind, SourceLocation Loc1, 300341825Sdim SourceLocation Loc2, SourceLocation Loc3) 301341825Sdim : Kind(Kind) 302341825Sdim { 303341825Sdim Locations[0] = Loc1; 304288943Sdim Locations[1] = Loc2; 305296417Sdim Locations[2] = Loc3; 306296417Sdim } 307296417Sdim 308public: 309 /// \brief Create a direct initialization. 310 static InitializationKind CreateDirect(SourceLocation InitLoc, 311 SourceLocation LParenLoc, 312 SourceLocation RParenLoc) { 313 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc); 314 } 315 316 /// \brief Create a direct initialization due to a cast. 317 static InitializationKind CreateCast(SourceRange TypeRange, 318 bool IsCStyleCast) { 319 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast 320 : SIK_DirectCast, 321 TypeRange.getBegin(), TypeRange.getBegin(), 322 TypeRange.getEnd()); 323 } 324 325 /// \brief Create a copy initialization. 326 static InitializationKind CreateCopy(SourceLocation InitLoc, 327 SourceLocation EqualLoc) { 328 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc); 329 } 330 331 /// \brief Create a default initialization. 332 static InitializationKind CreateDefault(SourceLocation InitLoc) { 333 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc); 334 } 335 336 /// \brief Create a value initialization. 337 static InitializationKind CreateValue(SourceLocation InitLoc, 338 SourceLocation LParenLoc, 339 SourceLocation RParenLoc, 340 bool isImplicit = false) { 341 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value, 342 InitLoc, LParenLoc, RParenLoc); 343 } 344 345 /// \brief Determine the initialization kind. 346 InitKind getKind() const { 347 if (Kind > SIK_ImplicitValue) 348 return IK_Direct; 349 if (Kind == SIK_ImplicitValue) 350 return IK_Value; 351 352 return (InitKind)Kind; 353 } 354 355 /// \brief Determine whether this initialization is an explicit cast. 356 bool isExplicitCast() const { 357 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast; 358 } 359 360 /// \brief Determine whether this initialization is a C-style cast. 361 bool isCStyleOrFunctionalCast() const { 362 return Kind == SIK_DirectCStyleOrFunctionalCast; 363 } 364 365 /// \brief Determine whether this initialization is an implicit 366 /// value-initialization, e.g., as occurs during aggregate 367 /// initialization. 368 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; } 369 370 /// \brief Retrieve the location at which initialization is occurring. 371 SourceLocation getLocation() const { return Locations[0]; } 372 373 /// \brief Retrieve the source range that covers the initialization. 374 SourceRange getRange() const { 375 return SourceRange(Locations[0], Locations[2]); 376 } 377 378 /// \brief Retrieve the location of the equal sign for copy initialization 379 /// (if present). 380 SourceLocation getEqualLoc() const { 381 assert(Kind == SIK_Copy && "Only copy initialization has an '='"); 382 return Locations[1]; 383 } 384 385 bool isCopyInit() const { return Kind == SIK_Copy; } 386 387 /// \brief Retrieve the source range containing the locations of the open 388 /// and closing parentheses for value and direct initializations. 389 SourceRange getParenRange() const { 390 assert((getKind() == IK_Direct || Kind == SIK_Value) && 391 "Only direct- and value-initialization have parentheses"); 392 return SourceRange(Locations[1], Locations[2]); 393 } 394}; 395 396/// \brief Describes the sequence of initializations required to initialize 397/// a given object or reference with a set of arguments. 398class InitializationSequence { 399public: 400 /// \brief Describes the kind of initialization sequence computed. 401 /// 402 /// FIXME: Much of this information is in the initialization steps... why is 403 /// it duplicated here? 404 enum SequenceKind { 405 /// \brief A failed initialization sequence. The failure kind tells what 406 /// happened. 407 FailedSequence = 0, 408 409 /// \brief A dependent initialization, which could not be 410 /// type-checked due to the presence of dependent types or 411 /// dependently-type expressions. 412 DependentSequence, 413 414 /// \brief A user-defined conversion sequence. 415 UserDefinedConversion, 416 417 /// \brief A constructor call. 418 ConstructorInitialization, 419 420 /// \brief A reference binding. 421 ReferenceBinding, 422 423 /// \brief List initialization 424 ListInitialization, 425 426 /// \brief Zero-initialization. 427 ZeroInitialization, 428 429 /// \brief No initialization required. 430 NoInitialization, 431 432 /// \brief Standard conversion sequence. 433 StandardConversion, 434 435 /// \brief C conversion sequence. 436 CAssignment, 437 438 /// \brief String initialization 439 StringInit 440 }; 441 442 /// \brief Describes the kind of a particular step in an initialization 443 /// sequence. 444 enum StepKind { 445 /// \brief Resolve the address of an overloaded function to a specific 446 /// function declaration. 447 SK_ResolveAddressOfOverloadedFunction, 448 /// \brief Perform a derived-to-base cast, producing an rvalue. 449 SK_CastDerivedToBaseRValue, 450 /// \brief Perform a derived-to-base cast, producing an xvalue. 451 SK_CastDerivedToBaseXValue, 452 /// \brief Perform a derived-to-base cast, producing an lvalue. 453 SK_CastDerivedToBaseLValue, 454 /// \brief Reference binding to an lvalue. 455 SK_BindReference, 456 /// \brief Reference binding to a temporary. 457 SK_BindReferenceToTemporary, 458 /// \brief An optional copy of a temporary object to another 459 /// temporary object, which is permitted (but not required) by 460 /// C++98/03 but not C++0x. 461 SK_ExtraneousCopyToTemporary, 462 /// \brief Perform a user-defined conversion, either via a conversion 463 /// function or via a constructor. 464 SK_UserConversion, 465 /// \brief Perform a qualification conversion, producing an rvalue. 466 SK_QualificationConversionRValue, 467 /// \brief Perform a qualification conversion, producing an xvalue. 468 SK_QualificationConversionXValue, 469 /// \brief Perform a qualification conversion, producing an lvalue. 470 SK_QualificationConversionLValue, 471 /// \brief Perform an implicit conversion sequence. 472 SK_ConversionSequence, 473 /// \brief Perform list-initialization 474 SK_ListInitialization, 475 /// \brief Perform initialization via a constructor. 476 SK_ConstructorInitialization, 477 /// \brief Zero-initialize the object 478 SK_ZeroInitialization, 479 /// \brief C assignment 480 SK_CAssignment, 481 /// \brief Initialization by string 482 SK_StringInit, 483 /// \brief An initialization that "converts" an Objective-C object 484 /// (not a point to an object) to another Objective-C object type. 485 SK_ObjCObjectConversion 486 }; 487 488 /// \brief A single step in the initialization sequence. 489 class Step { 490 public: 491 /// \brief The kind of conversion or initialization step we are taking. 492 StepKind Kind; 493 494 // \brief The type that results from this initialization. 495 QualType Type; 496 497 union { 498 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind == 499 /// SK_UserConversion, the function that the expression should be 500 /// resolved to or the conversion function to call, respectively. 501 /// 502 /// Always a FunctionDecl. 503 /// For conversion decls, the naming class is the source type. 504 /// For construct decls, the naming class is the target type. 505 struct { 506 FunctionDecl *Function; 507 DeclAccessPair FoundDecl; 508 } Function; 509 510 /// \brief When Kind = SK_ConversionSequence, the implicit conversion 511 /// sequence 512 ImplicitConversionSequence *ICS; 513 }; 514 515 void Destroy(); 516 }; 517 518private: 519 /// \brief The kind of initialization sequence computed. 520 enum SequenceKind SequenceKind; 521 522 /// \brief Steps taken by this initialization. 523 llvm::SmallVector<Step, 4> Steps; 524 525public: 526 /// \brief Describes why initialization failed. 527 enum FailureKind { 528 /// \brief Too many initializers provided for a reference. 529 FK_TooManyInitsForReference, 530 /// \brief Array must be initialized with an initializer list. 531 FK_ArrayNeedsInitList, 532 /// \brief Array must be initialized with an initializer list or a 533 /// string literal. 534 FK_ArrayNeedsInitListOrStringLiteral, 535 /// \brief Cannot resolve the address of an overloaded function. 536 FK_AddressOfOverloadFailed, 537 /// \brief Overloading due to reference initialization failed. 538 FK_ReferenceInitOverloadFailed, 539 /// \brief Non-const lvalue reference binding to a temporary. 540 FK_NonConstLValueReferenceBindingToTemporary, 541 /// \brief Non-const lvalue reference binding to an lvalue of unrelated 542 /// type. 543 FK_NonConstLValueReferenceBindingToUnrelated, 544 /// \brief Rvalue reference binding to an lvalue. 545 FK_RValueReferenceBindingToLValue, 546 /// \brief Reference binding drops qualifiers. 547 FK_ReferenceInitDropsQualifiers, 548 /// \brief Reference binding failed. 549 FK_ReferenceInitFailed, 550 /// \brief Implicit conversion failed. 551 FK_ConversionFailed, 552 /// \brief Too many initializers for scalar 553 FK_TooManyInitsForScalar, 554 /// \brief Reference initialization from an initializer list 555 FK_ReferenceBindingToInitList, 556 /// \brief Initialization of some unused destination type with an 557 /// initializer list. 558 FK_InitListBadDestinationType, 559 /// \brief Overloading for a user-defined conversion failed. 560 FK_UserConversionOverloadFailed, 561 /// \brief Overloaded for initialization by constructor failed. 562 FK_ConstructorOverloadFailed, 563 /// \brief Default-initialization of a 'const' object. 564 FK_DefaultInitOfConst, 565 /// \brief Initialization of an incomplete type. 566 FK_Incomplete 567 }; 568 569private: 570 /// \brief The reason why initialization failued. 571 FailureKind Failure; 572 573 /// \brief The failed result of overload resolution. 574 OverloadingResult FailedOverloadResult; 575 576 /// \brief The candidate set created when initialization failed. 577 OverloadCandidateSet FailedCandidateSet; 578 579 /// \brief Prints a follow-up note that highlights the location of 580 /// the initialized entity, if it's remote. 581 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity); 582 583public: 584 /// \brief Try to perform initialization of the given entity, creating a 585 /// record of the steps required to perform the initialization. 586 /// 587 /// The generated initialization sequence will either contain enough 588 /// information to diagnose 589 /// 590 /// \param S the semantic analysis object. 591 /// 592 /// \param Entity the entity being initialized. 593 /// 594 /// \param Kind the kind of initialization being performed. 595 /// 596 /// \param Args the argument(s) provided for initialization. 597 /// 598 /// \param NumArgs the number of arguments provided for initialization. 599 InitializationSequence(Sema &S, 600 const InitializedEntity &Entity, 601 const InitializationKind &Kind, 602 Expr **Args, 603 unsigned NumArgs); 604 605 ~InitializationSequence(); 606 607 /// \brief Perform the actual initialization of the given entity based on 608 /// the computed initialization sequence. 609 /// 610 /// \param S the semantic analysis object. 611 /// 612 /// \param Entity the entity being initialized. 613 /// 614 /// \param Kind the kind of initialization being performed. 615 /// 616 /// \param Args the argument(s) provided for initialization, ownership of 617 /// which is transfered into the routine. 618 /// 619 /// \param ResultType if non-NULL, will be set to the type of the 620 /// initialized object, which is the type of the declaration in most 621 /// cases. However, when the initialized object is a variable of 622 /// incomplete array type and the initializer is an initializer 623 /// list, this type will be set to the completed array type. 624 /// 625 /// \returns an expression that performs the actual object initialization, if 626 /// the initialization is well-formed. Otherwise, emits diagnostics 627 /// and returns an invalid expression. 628 ExprResult Perform(Sema &S, 629 const InitializedEntity &Entity, 630 const InitializationKind &Kind, 631 MultiExprArg Args, 632 QualType *ResultType = 0); 633 634 /// \brief Diagnose an potentially-invalid initialization sequence. 635 /// 636 /// \returns true if the initialization sequence was ill-formed, 637 /// false otherwise. 638 bool Diagnose(Sema &S, 639 const InitializedEntity &Entity, 640 const InitializationKind &Kind, 641 Expr **Args, unsigned NumArgs); 642 643 /// \brief Determine the kind of initialization sequence computed. 644 enum SequenceKind getKind() const { return SequenceKind; } 645 646 /// \brief Set the kind of sequence computed. 647 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } 648 649 /// \brief Determine whether the initialization sequence is valid. 650 operator bool() const { return SequenceKind != FailedSequence; } 651 652 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator; 653 step_iterator step_begin() const { return Steps.begin(); } 654 step_iterator step_end() const { return Steps.end(); } 655 656 /// \brief Determine whether this initialization is a direct reference 657 /// binding (C++ [dcl.init.ref]). 658 bool isDirectReferenceBinding() const; 659 660 /// \brief Determine whether this initialization failed due to an ambiguity. 661 bool isAmbiguous() const; 662 663 /// \brief Determine whether this initialization is direct call to a 664 /// constructor. 665 bool isConstructorInitialization() const; 666 667 /// \brief Add a new step in the initialization that resolves the address 668 /// of an overloaded function to a specific function declaration. 669 /// 670 /// \param Function the function to which the overloaded function reference 671 /// resolves. 672 void AddAddressOverloadResolutionStep(FunctionDecl *Function, 673 DeclAccessPair Found); 674 675 /// \brief Add a new step in the initialization that performs a derived-to- 676 /// base cast. 677 /// 678 /// \param BaseType the base type to which we will be casting. 679 /// 680 /// \param IsLValue true if the result of this cast will be treated as 681 /// an lvalue. 682 void AddDerivedToBaseCastStep(QualType BaseType, 683 ExprValueKind Category); 684 685 /// \brief Add a new step binding a reference to an object. 686 /// 687 /// \param BindingTemporary True if we are binding a reference to a temporary 688 /// object (thereby extending its lifetime); false if we are binding to an 689 /// lvalue or an lvalue treated as an rvalue. 690 /// 691 /// \param UnnecessaryCopy True if we should check for a copy 692 /// constructor for a completely unnecessary but 693 void AddReferenceBindingStep(QualType T, bool BindingTemporary); 694 695 /// \brief Add a new step that makes an extraneous copy of the input 696 /// to a temporary of the same class type. 697 /// 698 /// This extraneous copy only occurs during reference binding in 699 /// C++98/03, where we are permitted (but not required) to introduce 700 /// an extra copy. At a bare minimum, we must check that we could 701 /// call the copy constructor, and produce a diagnostic if the copy 702 /// constructor is inaccessible or no copy constructor matches. 703 // 704 /// \param T The type of the temporary being created. 705 void AddExtraneousCopyToTemporary(QualType T); 706 707 /// \brief Add a new step invoking a conversion function, which is either 708 /// a constructor or a conversion function. 709 void AddUserConversionStep(FunctionDecl *Function, 710 DeclAccessPair FoundDecl, 711 QualType T); 712 713 /// \brief Add a new step that performs a qualification conversion to the 714 /// given type. 715 void AddQualificationConversionStep(QualType Ty, 716 ExprValueKind Category); 717 718 /// \brief Add a new step that applies an implicit conversion sequence. 719 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, 720 QualType T); 721 722 /// \brief Add a list-initialiation step 723 void AddListInitializationStep(QualType T); 724 725 /// \brief Add a constructor-initialization step. 726 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor, 727 AccessSpecifier Access, 728 QualType T); 729 730 /// \brief Add a zero-initialization step. 731 void AddZeroInitializationStep(QualType T); 732 733 /// \brief Add a C assignment step. 734 // 735 // FIXME: It isn't clear whether this should ever be needed; 736 // ideally, we would handle everything needed in C in the common 737 // path. However, that isn't the case yet. 738 void AddCAssignmentStep(QualType T); 739 740 /// \brief Add a string init step. 741 void AddStringInitStep(QualType T); 742 743 /// \brief Add an Objective-C object conversion step, which is 744 /// always a no-op. 745 void AddObjCObjectConversionStep(QualType T); 746 747 /// \brief Note that this initialization sequence failed. 748 void SetFailed(FailureKind Failure) { 749 SequenceKind = FailedSequence; 750 this->Failure = Failure; 751 } 752 753 /// \brief Note that this initialization sequence failed due to failed 754 /// overload resolution. 755 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); 756 757 /// \brief Retrieve a reference to the candidate set when overload 758 /// resolution fails. 759 OverloadCandidateSet &getFailedCandidateSet() { 760 return FailedCandidateSet; 761 } 762 763 /// \brief Determine why initialization failed. 764 FailureKind getFailureKind() const { 765 assert(getKind() == FailedSequence && "Not an initialization failure!"); 766 return Failure; 767 } 768 769 /// \brief Dump a representation of this initialization sequence to 770 /// the given stream, for debugging purposes. 771 void dump(llvm::raw_ostream &OS) const; 772 773 /// \brief Dump a representation of this initialization sequence to 774 /// standard error, for debugging purposes. 775 void dump() const; 776}; 777 778} // end namespace clang 779 780#endif // LLVM_CLANG_SEMA_INITIALIZATION_H 781