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