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