Initialization.h revision d695d6bb7323672e29dbb1556a3dafde3d3b2732
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  ///
441  /// FIXME: Much of this information is in the initialization steps... why is
442  /// it duplicated here?
443  enum SequenceKind {
444    /// \brief A failed initialization sequence. The failure kind tells what
445    /// happened.
446    FailedSequence = 0,
447
448    /// \brief A dependent initialization, which could not be
449    /// type-checked due to the presence of dependent types or
450    /// dependently-type expressions.
451    DependentSequence,
452
453    /// \brief A user-defined conversion sequence.
454    UserDefinedConversion,
455
456    /// \brief A constructor call.
457    ConstructorInitialization,
458
459    /// \brief A reference binding.
460    ReferenceBinding,
461
462    /// \brief List initialization
463    ListInitialization,
464
465    /// \brief Zero-initialization.
466    ZeroInitialization,
467
468    /// \brief No initialization required.
469    NoInitialization,
470
471    /// \brief Standard conversion sequence.
472    StandardConversion,
473
474    /// \brief C conversion sequence.
475    CAssignment,
476
477    /// \brief String initialization
478    StringInit,
479
480    /// \brief Array initialization from another array (GNU C extension).
481    ArrayInit
482  };
483
484  /// \brief Describes the kind of a particular step in an initialization
485  /// sequence.
486  enum StepKind {
487    /// \brief Resolve the address of an overloaded function to a specific
488    /// function declaration.
489    SK_ResolveAddressOfOverloadedFunction,
490    /// \brief Perform a derived-to-base cast, producing an rvalue.
491    SK_CastDerivedToBaseRValue,
492    /// \brief Perform a derived-to-base cast, producing an xvalue.
493    SK_CastDerivedToBaseXValue,
494    /// \brief Perform a derived-to-base cast, producing an lvalue.
495    SK_CastDerivedToBaseLValue,
496    /// \brief Reference binding to an lvalue.
497    SK_BindReference,
498    /// \brief Reference binding to a temporary.
499    SK_BindReferenceToTemporary,
500    /// \brief An optional copy of a temporary object to another
501    /// temporary object, which is permitted (but not required) by
502    /// C++98/03 but not C++0x.
503    SK_ExtraneousCopyToTemporary,
504    /// \brief Perform a user-defined conversion, either via a conversion
505    /// function or via a constructor.
506    SK_UserConversion,
507    /// \brief Perform a qualification conversion, producing an rvalue.
508    SK_QualificationConversionRValue,
509    /// \brief Perform a qualification conversion, producing an xvalue.
510    SK_QualificationConversionXValue,
511    /// \brief Perform a qualification conversion, producing an lvalue.
512    SK_QualificationConversionLValue,
513    /// \brief Perform an implicit conversion sequence.
514    SK_ConversionSequence,
515    /// \brief Perform list-initialization
516    SK_ListInitialization,
517    /// \brief Perform initialization via a constructor.
518    SK_ConstructorInitialization,
519    /// \brief Zero-initialize the object
520    SK_ZeroInitialization,
521    /// \brief C assignment
522    SK_CAssignment,
523    /// \brief Initialization by string
524    SK_StringInit,
525    /// \brief An initialization that "converts" an Objective-C object
526    /// (not a point to an object) to another Objective-C object type.
527    SK_ObjCObjectConversion,
528    /// \brief Array initialization (from an array rvalue).
529    /// This is a GNU C extension.
530    SK_ArrayInit
531  };
532
533  /// \brief A single step in the initialization sequence.
534  class Step {
535  public:
536    /// \brief The kind of conversion or initialization step we are taking.
537    StepKind Kind;
538
539    // \brief The type that results from this initialization.
540    QualType Type;
541
542    union {
543      /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
544      /// SK_UserConversion, the function that the expression should be
545      /// resolved to or the conversion function to call, respectively.
546      ///
547      /// Always a FunctionDecl.
548      /// For conversion decls, the naming class is the source type.
549      /// For construct decls, the naming class is the target type.
550      struct {
551        FunctionDecl *Function;
552        DeclAccessPair FoundDecl;
553      } Function;
554
555      /// \brief When Kind = SK_ConversionSequence, the implicit conversion
556      /// sequence
557      ImplicitConversionSequence *ICS;
558    };
559
560    void Destroy();
561  };
562
563private:
564  /// \brief The kind of initialization sequence computed.
565  enum SequenceKind SequenceKind;
566
567  /// \brief Steps taken by this initialization.
568  llvm::SmallVector<Step, 4> Steps;
569
570public:
571  /// \brief Describes why initialization failed.
572  enum FailureKind {
573    /// \brief Too many initializers provided for a reference.
574    FK_TooManyInitsForReference,
575    /// \brief Array must be initialized with an initializer list.
576    FK_ArrayNeedsInitList,
577    /// \brief Array must be initialized with an initializer list or a
578    /// string literal.
579    FK_ArrayNeedsInitListOrStringLiteral,
580    /// \brief Array type mismatch.
581    FK_ArrayTypeMismatch,
582    /// \brief Non-constant array initializer
583    FK_NonConstantArrayInit,
584    /// \brief Cannot resolve the address of an overloaded function.
585    FK_AddressOfOverloadFailed,
586    /// \brief Overloading due to reference initialization failed.
587    FK_ReferenceInitOverloadFailed,
588    /// \brief Non-const lvalue reference binding to a temporary.
589    FK_NonConstLValueReferenceBindingToTemporary,
590    /// \brief Non-const lvalue reference binding to an lvalue of unrelated
591    /// type.
592    FK_NonConstLValueReferenceBindingToUnrelated,
593    /// \brief Rvalue reference binding to an lvalue.
594    FK_RValueReferenceBindingToLValue,
595    /// \brief Reference binding drops qualifiers.
596    FK_ReferenceInitDropsQualifiers,
597    /// \brief Reference binding failed.
598    FK_ReferenceInitFailed,
599    /// \brief Implicit conversion failed.
600    FK_ConversionFailed,
601    /// \brief Implicit conversion failed.
602    FK_ConversionFromPropertyFailed,
603    /// \brief Too many initializers for scalar
604    FK_TooManyInitsForScalar,
605    /// \brief Reference initialization from an initializer list
606    FK_ReferenceBindingToInitList,
607    /// \brief Initialization of some unused destination type with an
608    /// initializer list.
609    FK_InitListBadDestinationType,
610    /// \brief Overloading for a user-defined conversion failed.
611    FK_UserConversionOverloadFailed,
612    /// \brief Overloaded for initialization by constructor failed.
613    FK_ConstructorOverloadFailed,
614    /// \brief Default-initialization of a 'const' object.
615    FK_DefaultInitOfConst,
616    /// \brief Initialization of an incomplete type.
617    FK_Incomplete
618  };
619
620private:
621  /// \brief The reason why initialization failued.
622  FailureKind Failure;
623
624  /// \brief The failed result of overload resolution.
625  OverloadingResult FailedOverloadResult;
626
627  /// \brief The candidate set created when initialization failed.
628  OverloadCandidateSet FailedCandidateSet;
629
630  /// \brief Prints a follow-up note that highlights the location of
631  /// the initialized entity, if it's remote.
632  void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
633
634public:
635  /// \brief Try to perform initialization of the given entity, creating a
636  /// record of the steps required to perform the initialization.
637  ///
638  /// The generated initialization sequence will either contain enough
639  /// information to diagnose
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.
648  ///
649  /// \param NumArgs the number of arguments provided for initialization.
650  InitializationSequence(Sema &S,
651                         const InitializedEntity &Entity,
652                         const InitializationKind &Kind,
653                         Expr **Args,
654                         unsigned NumArgs);
655
656  ~InitializationSequence();
657
658  /// \brief Perform the actual initialization of the given entity based on
659  /// the computed initialization sequence.
660  ///
661  /// \param S the semantic analysis object.
662  ///
663  /// \param Entity the entity being initialized.
664  ///
665  /// \param Kind the kind of initialization being performed.
666  ///
667  /// \param Args the argument(s) provided for initialization, ownership of
668  /// which is transferred into the routine.
669  ///
670  /// \param ResultType if non-NULL, will be set to the type of the
671  /// initialized object, which is the type of the declaration in most
672  /// cases. However, when the initialized object is a variable of
673  /// incomplete array type and the initializer is an initializer
674  /// list, this type will be set to the completed array type.
675  ///
676  /// \returns an expression that performs the actual object initialization, if
677  /// the initialization is well-formed. Otherwise, emits diagnostics
678  /// and returns an invalid expression.
679  ExprResult Perform(Sema &S,
680                     const InitializedEntity &Entity,
681                     const InitializationKind &Kind,
682                     MultiExprArg Args,
683                     QualType *ResultType = 0);
684
685  /// \brief Diagnose an potentially-invalid initialization sequence.
686  ///
687  /// \returns true if the initialization sequence was ill-formed,
688  /// false otherwise.
689  bool Diagnose(Sema &S,
690                const InitializedEntity &Entity,
691                const InitializationKind &Kind,
692                Expr **Args, unsigned NumArgs);
693
694  /// \brief Determine the kind of initialization sequence computed.
695  enum SequenceKind getKind() const { return SequenceKind; }
696
697  /// \brief Set the kind of sequence computed.
698  void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
699
700  /// \brief Determine whether the initialization sequence is valid.
701  operator bool() const { return SequenceKind != FailedSequence; }
702
703  /// \brief Determine whether the initialization sequence is invalid.
704  bool Failed() const { return SequenceKind == FailedSequence; }
705
706  typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
707  step_iterator step_begin() const { return Steps.begin(); }
708  step_iterator step_end()   const { return Steps.end(); }
709
710  /// \brief Determine whether this initialization is a direct reference
711  /// binding (C++ [dcl.init.ref]).
712  bool isDirectReferenceBinding() const;
713
714  /// \brief Determine whether this initialization failed due to an ambiguity.
715  bool isAmbiguous() const;
716
717  /// \brief Determine whether this initialization is direct call to a
718  /// constructor.
719  bool isConstructorInitialization() const;
720
721  /// \brief Add a new step in the initialization that resolves the address
722  /// of an overloaded function to a specific function declaration.
723  ///
724  /// \param Function the function to which the overloaded function reference
725  /// resolves.
726  void AddAddressOverloadResolutionStep(FunctionDecl *Function,
727                                        DeclAccessPair Found);
728
729  /// \brief Add a new step in the initialization that performs a derived-to-
730  /// base cast.
731  ///
732  /// \param BaseType the base type to which we will be casting.
733  ///
734  /// \param IsLValue true if the result of this cast will be treated as
735  /// an lvalue.
736  void AddDerivedToBaseCastStep(QualType BaseType,
737                                ExprValueKind Category);
738
739  /// \brief Add a new step binding a reference to an object.
740  ///
741  /// \param BindingTemporary True if we are binding a reference to a temporary
742  /// object (thereby extending its lifetime); false if we are binding to an
743  /// lvalue or an lvalue treated as an rvalue.
744  ///
745  /// \param UnnecessaryCopy True if we should check for a copy
746  /// constructor for a completely unnecessary but
747  void AddReferenceBindingStep(QualType T, bool BindingTemporary);
748
749  /// \brief Add a new step that makes an extraneous copy of the input
750  /// to a temporary of the same class type.
751  ///
752  /// This extraneous copy only occurs during reference binding in
753  /// C++98/03, where we are permitted (but not required) to introduce
754  /// an extra copy. At a bare minimum, we must check that we could
755  /// call the copy constructor, and produce a diagnostic if the copy
756  /// constructor is inaccessible or no copy constructor matches.
757  //
758  /// \param T The type of the temporary being created.
759  void AddExtraneousCopyToTemporary(QualType T);
760
761  /// \brief Add a new step invoking a conversion function, which is either
762  /// a constructor or a conversion function.
763  void AddUserConversionStep(FunctionDecl *Function,
764                             DeclAccessPair FoundDecl,
765                             QualType T);
766
767  /// \brief Add a new step that performs a qualification conversion to the
768  /// given type.
769  void AddQualificationConversionStep(QualType Ty,
770                                     ExprValueKind Category);
771
772  /// \brief Add a new step that applies an implicit conversion sequence.
773  void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
774                                 QualType T);
775
776  /// \brief Add a list-initialiation step
777  void AddListInitializationStep(QualType T);
778
779  /// \brief Add a constructor-initialization step.
780  void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
781                                        AccessSpecifier Access,
782                                        QualType T);
783
784  /// \brief Add a zero-initialization step.
785  void AddZeroInitializationStep(QualType T);
786
787  /// \brief Add a C assignment step.
788  //
789  // FIXME: It isn't clear whether this should ever be needed;
790  // ideally, we would handle everything needed in C in the common
791  // path. However, that isn't the case yet.
792  void AddCAssignmentStep(QualType T);
793
794  /// \brief Add a string init step.
795  void AddStringInitStep(QualType T);
796
797  /// \brief Add an Objective-C object conversion step, which is
798  /// always a no-op.
799  void AddObjCObjectConversionStep(QualType T);
800
801  /// \brief Add an array initialization step.
802  void AddArrayInitStep(QualType T);
803
804  /// \brief Note that this initialization sequence failed.
805  void SetFailed(FailureKind Failure) {
806    SequenceKind = FailedSequence;
807    this->Failure = Failure;
808  }
809
810  /// \brief Note that this initialization sequence failed due to failed
811  /// overload resolution.
812  void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
813
814  /// \brief Retrieve a reference to the candidate set when overload
815  /// resolution fails.
816  OverloadCandidateSet &getFailedCandidateSet() {
817    return FailedCandidateSet;
818  }
819
820  /// brief Get the overloading result, for when the initialization
821  /// sequence failed due to a bad overload.
822  OverloadingResult getFailedOverloadResult() const {
823    return FailedOverloadResult;
824  }
825
826  /// \brief Determine why initialization failed.
827  FailureKind getFailureKind() const {
828    assert(Failed() && "Not an initialization failure!");
829    return Failure;
830  }
831
832  /// \brief Dump a representation of this initialization sequence to
833  /// the given stream, for debugging purposes.
834  void dump(llvm::raw_ostream &OS) const;
835
836  /// \brief Dump a representation of this initialization sequence to
837  /// standard error, for debugging purposes.
838  void dump() const;
839};
840
841} // end namespace clang
842
843#endif // LLVM_CLANG_SEMA_INITIALIZATION_H
844