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