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