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