clang API Documentation

SemaInit.h

Go to the documentation of this file.
00001 //===--- SemaInit.h - Semantic Analysis for Initializers --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides supporting data types for initialization of objects.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #ifndef LLVM_CLANG_SEMA_INIT_H
00014 #define LLVM_CLANG_SEMA_INIT_H
00015 
00016 #include "SemaOverload.h"
00017 #include "clang/AST/Type.h"
00018 #include "clang/AST/UnresolvedSet.h"
00019 #include "clang/Parse/Action.h"
00020 #include "clang/Basic/SourceLocation.h"
00021 #include "llvm/ADT/PointerIntPair.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include <cassert>
00024 
00025 namespace llvm {
00026   class raw_ostream;
00027 }
00028 
00029 namespace clang {
00030   
00031 class CXXBaseSpecifier;
00032 class DeclaratorDecl;
00033 class DeclaratorInfo;
00034 class FieldDecl;
00035 class FunctionDecl;
00036 class ParmVarDecl;
00037 class Sema;
00038 class TypeLoc;
00039 class VarDecl;
00040   
00041 /// \brief Describes an entity that is being initialized.
00042 class InitializedEntity {
00043 public:
00044   /// \brief Specifies the kind of entity being initialized.
00045   enum EntityKind {
00046     /// \brief The entity being initialized is a variable.
00047     EK_Variable,
00048     /// \brief The entity being initialized is a function parameter.
00049     EK_Parameter,
00050     /// \brief The entity being initialized is the result of a function call.
00051     EK_Result,
00052     /// \brief The entity being initialized is an exception object that
00053     /// is being thrown.
00054     EK_Exception,
00055     /// \brief The entity being initialized is a non-static data member 
00056     /// subobject.
00057     EK_Member,
00058     /// \brief The entity being initialized is an element of an array.
00059     EK_ArrayElement,
00060     /// \brief The entity being initialized is an object (or array of
00061     /// objects) allocated via new.
00062     EK_New,
00063     /// \brief The entity being initialized is a temporary object.
00064     EK_Temporary,
00065     /// \brief The entity being initialized is a base member subobject.
00066     EK_Base,
00067     /// \brief The entity being initialized is an element of a vector.
00068     /// or vector.
00069     EK_VectorElement,
00070     /// \brief The entity being initialized is a field of block descriptor for
00071     /// the copied-in c++ object.
00072     EK_BlockElement
00073   };
00074   
00075 private:
00076   /// \brief The kind of entity being initialized.
00077   EntityKind Kind;
00078 
00079   /// \brief If non-NULL, the parent entity in which this
00080   /// initialization occurs.
00081   const InitializedEntity *Parent;
00082 
00083   /// \brief The type of the object or reference being initialized.
00084   QualType Type;
00085   
00086   union {
00087     /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member, 
00088     /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
00089     DeclaratorDecl *VariableOrMember;
00090     
00091     struct {
00092       /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
00093       /// location of the 'return', 'throw', or 'new' keyword,
00094       /// respectively. When Kind == EK_Temporary, the location where
00095       /// the temporary is being created.
00096       unsigned Location;
00097       
00098       /// \brief Whether the 
00099       bool NRVO;
00100     } LocAndNRVO;
00101     
00102     /// \brief When Kind == EK_Base, the base specifier that provides the 
00103     /// base class. The lower bit specifies whether the base is an inherited
00104     /// virtual base.
00105     uintptr_t Base;
00106 
00107     /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the
00108     /// index of the array or vector element being initialized. 
00109     unsigned Index;
00110   };
00111 
00112   InitializedEntity() { }
00113 
00114   /// \brief Create the initialization entity for a variable.
00115   InitializedEntity(VarDecl *Var)
00116     : Kind(EK_Variable), Parent(0), Type(Var->getType()),
00117       VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
00118   
00119   /// \brief Create the initialization entity for a parameter.
00120   InitializedEntity(ParmVarDecl *Parm)
00121     : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
00122       VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
00123   
00124   /// \brief Create the initialization entity for the result of a
00125   /// function, throwing an object, performing an explicit cast, or
00126   /// initializing a parameter for which there is no declaration.
00127   InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
00128                     bool NRVO = false)
00129     : Kind(Kind), Parent(0), Type(Type)
00130   {
00131     LocAndNRVO.Location = Loc.getRawEncoding();
00132     LocAndNRVO.NRVO = NRVO;
00133   }
00134   
00135   /// \brief Create the initialization entity for a member subobject.
00136   InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent) 
00137     : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
00138       VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
00139   
00140   /// \brief Create the initialization entity for an array element.
00141   InitializedEntity(ASTContext &Context, unsigned Index, 
00142                     const InitializedEntity &Parent);
00143 
00144 public:
00145   /// \brief Create the initialization entity for a variable.
00146   static InitializedEntity InitializeVariable(VarDecl *Var) {
00147     return InitializedEntity(Var);
00148   }
00149   
00150   /// \brief Create the initialization entity for a parameter.
00151   static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
00152     return InitializedEntity(Parm);
00153   }
00154 
00155   /// \brief Create the initialization entity for a parameter that is
00156   /// only known by its type.
00157   static InitializedEntity InitializeParameter(QualType Type) {
00158     InitializedEntity Entity;
00159     Entity.Kind = EK_Parameter;
00160     Entity.Type = Type;
00161     Entity.Parent = 0;
00162     Entity.VariableOrMember = 0;
00163     return Entity;
00164   }
00165 
00166   /// \brief Create the initialization entity for the result of a function.
00167   static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
00168                                             QualType Type, bool NRVO) {
00169     return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
00170   }
00171 
00172   static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
00173                                            QualType Type, bool NRVO) {
00174     return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
00175   }
00176   
00177   /// \brief Create the initialization entity for an exception object.
00178   static InitializedEntity InitializeException(SourceLocation ThrowLoc,
00179                                                QualType Type, bool NRVO) {
00180     return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
00181   }
00182 
00183   /// \brief Create the initialization entity for an object allocated via new.
00184   static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
00185     return InitializedEntity(EK_New, NewLoc, Type);
00186   }
00187   
00188   /// \brief Create the initialization entity for a temporary.
00189   static InitializedEntity InitializeTemporary(QualType Type) {
00190     return InitializedEntity(EK_Temporary, SourceLocation(), Type);
00191   }
00192   
00193   /// \brief Create the initialization entity for a base class subobject.
00194   static InitializedEntity InitializeBase(ASTContext &Context,
00195                                           CXXBaseSpecifier *Base,
00196                                           bool IsInheritedVirtualBase);
00197   
00198   /// \brief Create the initialization entity for a member subobject.
00199   static InitializedEntity InitializeMember(FieldDecl *Member,
00200                                           const InitializedEntity *Parent = 0) {
00201     return InitializedEntity(Member, Parent);
00202   }
00203   
00204   /// \brief Create the initialization entity for an array element.
00205   static InitializedEntity InitializeElement(ASTContext &Context, 
00206                                              unsigned Index, 
00207                                              const InitializedEntity &Parent) {
00208     return InitializedEntity(Context, Index, Parent);
00209   }
00210 
00211   /// \brief Determine the kind of initialization.
00212   EntityKind getKind() const { return Kind; }
00213   
00214   /// \brief Retrieve the parent of the entity being initialized, when
00215   /// the initialization itself is occuring within the context of a
00216   /// larger initialization.
00217   const InitializedEntity *getParent() const { return Parent; }
00218 
00219   /// \brief Retrieve type being initialized.
00220   QualType getType() const { return Type; }
00221   
00222   /// \brief Retrieve the name of the entity being initialized.
00223   DeclarationName getName() const;
00224 
00225   /// \brief Retrieve the variable, parameter, or field being
00226   /// initialized.
00227   DeclaratorDecl *getDecl() const;
00228 
00229   /// \brief Determine whether this initialization allows the named return 
00230   /// value optimization, which also applies to thrown objects.
00231   bool allowsNRVO() const;
00232                                   
00233   /// \brief Retrieve the base specifier.
00234   CXXBaseSpecifier *getBaseSpecifier() const {
00235     assert(getKind() == EK_Base && "Not a base specifier");
00236     return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
00237   }
00238 
00239   /// \brief Return whether the base is an inherited virtual base.
00240   bool isInheritedVirtualBase() const {
00241     assert(getKind() == EK_Base && "Not a base specifier");
00242     return Base & 0x1;
00243   }
00244 
00245   /// \brief Determine the location of the 'return' keyword when initializing
00246   /// the result of a function call.
00247   SourceLocation getReturnLoc() const {
00248     assert(getKind() == EK_Result && "No 'return' location!");
00249     return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
00250   }
00251   
00252   /// \brief Determine the location of the 'throw' keyword when initializing
00253   /// an exception object.
00254   SourceLocation getThrowLoc() const {
00255     assert(getKind() == EK_Exception && "No 'throw' location!");
00256     return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
00257   }
00258 
00259   /// \brief If this is already the initializer for an array or vector
00260   /// element, sets the element index.
00261   void setElementIndex(unsigned Index) {
00262     assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
00263     this->Index = Index;
00264   }
00265 };
00266   
00267 /// \brief Describes the kind of initialization being performed, along with 
00268 /// location information for tokens related to the initialization (equal sign,
00269 /// parentheses).
00270 class InitializationKind {
00271 public:
00272   /// \brief The kind of initialization being performed.
00273   enum InitKind {
00274     IK_Direct,  ///< Direct initialization
00275     IK_Copy,    ///< Copy initialization
00276     IK_Default, ///< Default initialization
00277     IK_Value    ///< Value initialization
00278   };
00279   
00280 private:
00281   /// \brief The kind of initialization that we're storing.
00282   enum StoredInitKind {
00283     SIK_Direct = IK_Direct,   ///< Direct initialization
00284     SIK_Copy = IK_Copy,       ///< Copy initialization
00285     SIK_Default = IK_Default, ///< Default initialization
00286     SIK_Value = IK_Value,     ///< Value initialization
00287     SIK_ImplicitValue,        ///< Implicit value initialization
00288     SIK_DirectCast,  ///< Direct initialization due to a cast
00289     /// \brief Direct initialization due to a C-style or functional cast.
00290     SIK_DirectCStyleOrFunctionalCast
00291   };
00292   
00293   /// \brief The kind of initialization being performed.
00294   StoredInitKind Kind;
00295   
00296   /// \brief The source locations involved in the initialization.
00297   SourceLocation Locations[3];
00298   
00299   InitializationKind(StoredInitKind Kind, SourceLocation Loc1, 
00300                      SourceLocation Loc2, SourceLocation Loc3)
00301     : Kind(Kind) 
00302   {
00303     Locations[0] = Loc1;
00304     Locations[1] = Loc2;
00305     Locations[2] = Loc3;
00306   }
00307   
00308 public:
00309   /// \brief Create a direct initialization.
00310   static InitializationKind CreateDirect(SourceLocation InitLoc,
00311                                          SourceLocation LParenLoc,
00312                                          SourceLocation RParenLoc) {
00313     return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
00314   }
00315 
00316   /// \brief Create a direct initialization due to a cast.
00317   static InitializationKind CreateCast(SourceRange TypeRange,
00318                                        bool IsCStyleCast) {
00319     return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
00320                                           : SIK_DirectCast,
00321                               TypeRange.getBegin(), TypeRange.getBegin(), 
00322                               TypeRange.getEnd());
00323   }
00324   
00325   /// \brief Create a copy initialization.
00326   static InitializationKind CreateCopy(SourceLocation InitLoc,
00327                                        SourceLocation EqualLoc) {
00328     return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
00329   }
00330   
00331   /// \brief Create a default initialization.
00332   static InitializationKind CreateDefault(SourceLocation InitLoc) {
00333     return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
00334   }
00335   
00336   /// \brief Create a value initialization.
00337   static InitializationKind CreateValue(SourceLocation InitLoc,
00338                                         SourceLocation LParenLoc,
00339                                         SourceLocation RParenLoc,
00340                                         bool isImplicit = false) {
00341     return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value, 
00342                               InitLoc, LParenLoc, RParenLoc);
00343   }
00344   
00345   /// \brief Determine the initialization kind.
00346   InitKind getKind() const {
00347     if (Kind > SIK_ImplicitValue)
00348       return IK_Direct;
00349     if (Kind == SIK_ImplicitValue)
00350       return IK_Value;
00351 
00352     return (InitKind)Kind;
00353   }
00354   
00355   /// \brief Determine whether this initialization is an explicit cast.
00356   bool isExplicitCast() const {
00357     return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
00358   }
00359   
00360   /// \brief Determine whether this initialization is a C-style cast.
00361   bool isCStyleOrFunctionalCast() const { 
00362     return Kind == SIK_DirectCStyleOrFunctionalCast; 
00363   }
00364 
00365   /// \brief Determine whether this initialization is an implicit
00366   /// value-initialization, e.g., as occurs during aggregate
00367   /// initialization.
00368   bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
00369 
00370   /// \brief Retrieve the location at which initialization is occurring.
00371   SourceLocation getLocation() const { return Locations[0]; }
00372   
00373   /// \brief Retrieve the source range that covers the initialization.
00374   SourceRange getRange() const { 
00375     return SourceRange(Locations[0], Locations[2]);
00376   }
00377   
00378   /// \brief Retrieve the location of the equal sign for copy initialization
00379   /// (if present).
00380   SourceLocation getEqualLoc() const {
00381     assert(Kind == SIK_Copy && "Only copy initialization has an '='");
00382     return Locations[1];
00383   }
00384   
00385   bool isCopyInit() const { return Kind == SIK_Copy; }
00386   
00387   /// \brief Retrieve the source range containing the locations of the open
00388   /// and closing parentheses for value and direct initializations.
00389   SourceRange getParenRange() const {
00390     assert((getKind() == IK_Direct || Kind == SIK_Value) &&
00391            "Only direct- and value-initialization have parentheses");
00392     return SourceRange(Locations[1], Locations[2]);
00393   }
00394 };
00395 
00396 /// \brief Describes the sequence of initializations required to initialize
00397 /// a given object or reference with a set of arguments.
00398 class InitializationSequence {
00399 public:
00400   /// \brief Describes the kind of initialization sequence computed.
00401   ///
00402   /// FIXME: Much of this information is in the initialization steps... why is
00403   /// it duplicated here?
00404   enum SequenceKind {
00405     /// \brief A failed initialization sequence. The failure kind tells what
00406     /// happened.
00407     FailedSequence = 0,
00408     
00409     /// \brief A dependent initialization, which could not be
00410     /// type-checked due to the presence of dependent types or
00411     /// dependently-type expressions.
00412     DependentSequence,
00413 
00414     /// \brief A user-defined conversion sequence.
00415     UserDefinedConversion,
00416     
00417     /// \brief A constructor call.
00418     ConstructorInitialization,
00419     
00420     /// \brief A reference binding.
00421     ReferenceBinding,
00422 
00423     /// \brief List initialization
00424     ListInitialization,
00425     
00426     /// \brief Zero-initialization.
00427     ZeroInitialization,
00428     
00429     /// \brief No initialization required.
00430     NoInitialization,
00431     
00432     /// \brief Standard conversion sequence.
00433     StandardConversion,
00434 
00435     /// \brief C conversion sequence.
00436     CAssignment,
00437 
00438     /// \brief String initialization
00439     StringInit
00440   };
00441   
00442   /// \brief Describes the kind of a particular step in an initialization
00443   /// sequence.
00444   enum StepKind {
00445     /// \brief Resolve the address of an overloaded function to a specific
00446     /// function declaration.
00447     SK_ResolveAddressOfOverloadedFunction,
00448     /// \brief Perform a derived-to-base cast, producing an rvalue.
00449     SK_CastDerivedToBaseRValue,
00450     /// \brief Perform a derived-to-base cast, producing an xvalue.
00451     SK_CastDerivedToBaseXValue,
00452     /// \brief Perform a derived-to-base cast, producing an lvalue.
00453     SK_CastDerivedToBaseLValue,
00454     /// \brief Reference binding to an lvalue.
00455     SK_BindReference,
00456     /// \brief Reference binding to a temporary.
00457     SK_BindReferenceToTemporary,
00458     /// \brief An optional copy of a temporary object to another
00459     /// temporary object, which is permitted (but not required) by
00460     /// C++98/03 but not C++0x.
00461     SK_ExtraneousCopyToTemporary,
00462     /// \brief Perform a user-defined conversion, either via a conversion
00463     /// function or via a constructor.
00464     SK_UserConversion,
00465     /// \brief Perform a qualification conversion, producing an rvalue.
00466     SK_QualificationConversionRValue,
00467     /// \brief Perform a qualification conversion, producing an xvalue.
00468     SK_QualificationConversionXValue,
00469     /// \brief Perform a qualification conversion, producing an lvalue.
00470     SK_QualificationConversionLValue,
00471     /// \brief Perform an implicit conversion sequence.
00472     SK_ConversionSequence,
00473     /// \brief Perform list-initialization
00474     SK_ListInitialization,
00475     /// \brief Perform initialization via a constructor.
00476     SK_ConstructorInitialization,
00477     /// \brief Zero-initialize the object
00478     SK_ZeroInitialization,
00479     /// \brief C assignment
00480     SK_CAssignment,
00481     /// \brief Initialization by string
00482     SK_StringInit,
00483     /// \brief An initialization that "converts" an Objective-C object
00484     /// (not a point to an object) to another Objective-C object type.
00485     SK_ObjCObjectConversion
00486   };
00487   
00488   /// \brief A single step in the initialization sequence.
00489   class Step {
00490   public:
00491     /// \brief The kind of conversion or initialization step we are taking.
00492     StepKind Kind;
00493     
00494     // \brief The type that results from this initialization.
00495     QualType Type;
00496     
00497     union {
00498       /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
00499       /// SK_UserConversion, the function that the expression should be 
00500       /// resolved to or the conversion function to call, respectively.
00501       ///
00502       /// Always a FunctionDecl.
00503       /// For conversion decls, the naming class is the source type.
00504       /// For construct decls, the naming class is the target type.
00505       struct {
00506         FunctionDecl *Function;
00507         DeclAccessPair FoundDecl;
00508       } Function;
00509       
00510       /// \brief When Kind = SK_ConversionSequence, the implicit conversion
00511       /// sequence 
00512       ImplicitConversionSequence *ICS;
00513     };
00514     
00515     void Destroy();
00516   };
00517   
00518 private:
00519   /// \brief The kind of initialization sequence computed.
00520   enum SequenceKind SequenceKind;
00521   
00522   /// \brief Steps taken by this initialization.
00523   llvm::SmallVector<Step, 4> Steps;
00524   
00525 public:
00526   /// \brief Describes why initialization failed.
00527   enum FailureKind {
00528     /// \brief Too many initializers provided for a reference.
00529     FK_TooManyInitsForReference,
00530     /// \brief Array must be initialized with an initializer list.
00531     FK_ArrayNeedsInitList,
00532     /// \brief Array must be initialized with an initializer list or a 
00533     /// string literal.
00534     FK_ArrayNeedsInitListOrStringLiteral,
00535     /// \brief Cannot resolve the address of an overloaded function.
00536     FK_AddressOfOverloadFailed,
00537     /// \brief Overloading due to reference initialization failed.
00538     FK_ReferenceInitOverloadFailed,
00539     /// \brief Non-const lvalue reference binding to a temporary.
00540     FK_NonConstLValueReferenceBindingToTemporary,
00541     /// \brief Non-const lvalue reference binding to an lvalue of unrelated
00542     /// type.
00543     FK_NonConstLValueReferenceBindingToUnrelated,
00544     /// \brief Rvalue reference binding to an lvalue.
00545     FK_RValueReferenceBindingToLValue,
00546     /// \brief Reference binding drops qualifiers.
00547     FK_ReferenceInitDropsQualifiers,
00548     /// \brief Reference binding failed.
00549     FK_ReferenceInitFailed,
00550     /// \brief Implicit conversion failed.
00551     FK_ConversionFailed,
00552     /// \brief Too many initializers for scalar
00553     FK_TooManyInitsForScalar,
00554     /// \brief Reference initialization from an initializer list
00555     FK_ReferenceBindingToInitList,
00556     /// \brief Initialization of some unused destination type with an
00557     /// initializer list.
00558     FK_InitListBadDestinationType,
00559     /// \brief Overloading for a user-defined conversion failed.
00560     FK_UserConversionOverloadFailed,
00561     /// \brief Overloaded for initialization by constructor failed.
00562     FK_ConstructorOverloadFailed,
00563     /// \brief Default-initialization of a 'const' object.
00564     FK_DefaultInitOfConst,
00565     /// \brief Initialization of an incomplete type.
00566     FK_Incomplete
00567   };
00568   
00569 private:
00570   /// \brief The reason why initialization failued.
00571   FailureKind Failure;
00572 
00573   /// \brief The failed result of overload resolution.
00574   OverloadingResult FailedOverloadResult;
00575   
00576   /// \brief The candidate set created when initialization failed.
00577   OverloadCandidateSet FailedCandidateSet;
00578 
00579   /// \brief Prints a follow-up note that highlights the location of
00580   /// the initialized entity, if it's remote.
00581   void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
00582 
00583 public:
00584   /// \brief Try to perform initialization of the given entity, creating a 
00585   /// record of the steps required to perform the initialization.
00586   ///
00587   /// The generated initialization sequence will either contain enough
00588   /// information to diagnose 
00589   ///
00590   /// \param S the semantic analysis object.
00591   ///
00592   /// \param Entity the entity being initialized.
00593   ///
00594   /// \param Kind the kind of initialization being performed.
00595   ///
00596   /// \param Args the argument(s) provided for initialization.
00597   ///
00598   /// \param NumArgs the number of arguments provided for initialization.
00599   InitializationSequence(Sema &S, 
00600                          const InitializedEntity &Entity,
00601                          const InitializationKind &Kind,
00602                          Expr **Args,
00603                          unsigned NumArgs);
00604   
00605   ~InitializationSequence();
00606   
00607   /// \brief Perform the actual initialization of the given entity based on
00608   /// the computed initialization sequence.
00609   ///
00610   /// \param S the semantic analysis object.
00611   ///
00612   /// \param Entity the entity being initialized.
00613   ///
00614   /// \param Kind the kind of initialization being performed.
00615   ///
00616   /// \param Args the argument(s) provided for initialization, ownership of
00617   /// which is transfered into the routine.
00618   ///
00619   /// \param ResultType if non-NULL, will be set to the type of the
00620   /// initialized object, which is the type of the declaration in most
00621   /// cases. However, when the initialized object is a variable of
00622   /// incomplete array type and the initializer is an initializer
00623   /// list, this type will be set to the completed array type.
00624   ///
00625   /// \returns an expression that performs the actual object initialization, if
00626   /// the initialization is well-formed. Otherwise, emits diagnostics
00627   /// and returns an invalid expression.
00628   Action::OwningExprResult Perform(Sema &S,
00629                                    const InitializedEntity &Entity,
00630                                    const InitializationKind &Kind,
00631                                    Action::MultiExprArg Args,
00632                                    QualType *ResultType = 0);
00633   
00634   /// \brief Diagnose an potentially-invalid initialization sequence.
00635   ///
00636   /// \returns true if the initialization sequence was ill-formed, 
00637   /// false otherwise.
00638   bool Diagnose(Sema &S, 
00639                 const InitializedEntity &Entity,
00640                 const InitializationKind &Kind,
00641                 Expr **Args, unsigned NumArgs);
00642   
00643   /// \brief Determine the kind of initialization sequence computed.
00644   enum SequenceKind getKind() const { return SequenceKind; }
00645   
00646   /// \brief Set the kind of sequence computed.
00647   void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
00648   
00649   /// \brief Determine whether the initialization sequence is valid.
00650   operator bool() const { return SequenceKind != FailedSequence; }
00651   
00652   typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
00653   step_iterator step_begin() const { return Steps.begin(); }
00654   step_iterator step_end()   const { return Steps.end(); }
00655 
00656   /// \brief Determine whether this initialization is a direct reference 
00657   /// binding (C++ [dcl.init.ref]).
00658   bool isDirectReferenceBinding() const;
00659   
00660   /// \brief Determine whether this initialization failed due to an ambiguity.
00661   bool isAmbiguous() const;
00662   
00663   /// \brief Determine whether this initialization is direct call to a 
00664   /// constructor.
00665   bool isConstructorInitialization() const;
00666   
00667   /// \brief Add a new step in the initialization that resolves the address
00668   /// of an overloaded function to a specific function declaration.
00669   ///
00670   /// \param Function the function to which the overloaded function reference
00671   /// resolves.
00672   void AddAddressOverloadResolutionStep(FunctionDecl *Function,
00673                                         DeclAccessPair Found);
00674   
00675   /// \brief Add a new step in the initialization that performs a derived-to-
00676   /// base cast.
00677   ///
00678   /// \param BaseType the base type to which we will be casting.
00679   ///
00680   /// \param IsLValue true if the result of this cast will be treated as 
00681   /// an lvalue.
00682   void AddDerivedToBaseCastStep(QualType BaseType,
00683                                 ImplicitCastExpr::ResultCategory Category);
00684      
00685   /// \brief Add a new step binding a reference to an object.
00686   ///
00687   /// \param BindingTemporary True if we are binding a reference to a temporary
00688   /// object (thereby extending its lifetime); false if we are binding to an
00689   /// lvalue or an lvalue treated as an rvalue.
00690   ///
00691   /// \param UnnecessaryCopy True if we should check for a copy
00692   /// constructor for a completely unnecessary but
00693   void AddReferenceBindingStep(QualType T, bool BindingTemporary);
00694 
00695   /// \brief Add a new step that makes an extraneous copy of the input
00696   /// to a temporary of the same class type.
00697   ///
00698   /// This extraneous copy only occurs during reference binding in
00699   /// C++98/03, where we are permitted (but not required) to introduce
00700   /// an extra copy. At a bare minimum, we must check that we could
00701   /// call the copy constructor, and produce a diagnostic if the copy
00702   /// constructor is inaccessible or no copy constructor matches.
00703   //
00704   /// \param T The type of the temporary being created.
00705   void AddExtraneousCopyToTemporary(QualType T);
00706 
00707   /// \brief Add a new step invoking a conversion function, which is either
00708   /// a constructor or a conversion function.
00709   void AddUserConversionStep(FunctionDecl *Function,
00710                              DeclAccessPair FoundDecl,
00711                              QualType T);
00712   
00713   /// \brief Add a new step that performs a qualification conversion to the
00714   /// given type.
00715   void AddQualificationConversionStep(QualType Ty,
00716                                      ImplicitCastExpr::ResultCategory Category);
00717   
00718   /// \brief Add a new step that applies an implicit conversion sequence.
00719   void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
00720                                  QualType T);
00721 
00722   /// \brief Add a list-initialiation step  
00723   void AddListInitializationStep(QualType T);
00724 
00725   /// \brief Add a constructor-initialization step.
00726   void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
00727                                         AccessSpecifier Access,
00728                                         QualType T);
00729 
00730   /// \brief Add a zero-initialization step.
00731   void AddZeroInitializationStep(QualType T);
00732   
00733   /// \brief Add a C assignment step.
00734   //
00735   // FIXME: It isn't clear whether this should ever be needed;
00736   // ideally, we would handle everything needed in C in the common
00737   // path. However, that isn't the case yet.
00738   void AddCAssignmentStep(QualType T);
00739 
00740   /// \brief Add a string init step.
00741   void AddStringInitStep(QualType T);
00742 
00743   /// \brief Add an Objective-C object conversion step, which is
00744   /// always a no-op.
00745   void AddObjCObjectConversionStep(QualType T);
00746 
00747   /// \brief Note that this initialization sequence failed.
00748   void SetFailed(FailureKind Failure) {
00749     SequenceKind = FailedSequence;
00750     this->Failure = Failure;
00751   }
00752   
00753   /// \brief Note that this initialization sequence failed due to failed
00754   /// overload resolution.
00755   void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
00756   
00757   /// \brief Retrieve a reference to the candidate set when overload
00758   /// resolution fails.
00759   OverloadCandidateSet &getFailedCandidateSet() {
00760     return FailedCandidateSet;
00761   }
00762 
00763   /// \brief Determine why initialization failed.
00764   FailureKind getFailureKind() const {
00765     assert(getKind() == FailedSequence && "Not an initialization failure!");
00766     return Failure;
00767   }
00768   
00769   /// \brief Dump a representation of this initialization sequence to 
00770   /// the given stream, for debugging purposes.
00771   void dump(llvm::raw_ostream &OS) const;
00772   
00773   /// \brief Dump a representation of this initialization sequence to 
00774   /// standard error, for debugging purposes.
00775   void dump() const;
00776 };
00777   
00778 } // end namespace clang
00779 
00780 #endif // LLVM_CLANG_SEMA_INIT_H