Initialization.h revision 8cc488fefb2fb04bc8d5398da29f0182f97934cf
1d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor//===--- SemaInit.h - Semantic Analysis for Initializers --------*- C++ -*-===//
220093b4bf698f292c664676987541d5103b65b15Douglas Gregor//
320093b4bf698f292c664676987541d5103b65b15Douglas Gregor//                     The LLVM Compiler Infrastructure
420093b4bf698f292c664676987541d5103b65b15Douglas Gregor//
520093b4bf698f292c664676987541d5103b65b15Douglas Gregor// This file is distributed under the University of Illinois Open Source
620093b4bf698f292c664676987541d5103b65b15Douglas Gregor// License. See LICENSE.TXT for details.
720093b4bf698f292c664676987541d5103b65b15Douglas Gregor//
820093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
920093b4bf698f292c664676987541d5103b65b15Douglas Gregor//
1020093b4bf698f292c664676987541d5103b65b15Douglas Gregor// This file provides supporting data types for initialization of objects.
1120093b4bf698f292c664676987541d5103b65b15Douglas Gregor//
1220093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#ifndef LLVM_CLANG_SEMA_INITIALIZATION_H
14e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#define LLVM_CLANG_SEMA_INITIALIZATION_H
1520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
16120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall#include "clang/Sema/Ownership.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Overload.h"
18d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor#include "clang/AST/Type.h"
19b13b737a2450167c82e148590e8019b839ce6b98John McCall#include "clang/AST/UnresolvedSet.h"
2020093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "clang/Basic/SourceLocation.h"
2120093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "llvm/ADT/PointerIntPair.h"
2220093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "llvm/ADT/SmallVector.h"
2320093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include <cassert>
2420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
25de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregornamespace llvm {
26de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  class raw_ostream;
27de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor}
28de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
2920093b4bf698f292c664676987541d5103b65b15Douglas Gregornamespace clang {
3020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3120093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass CXXBaseSpecifier;
3220093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass DeclaratorDecl;
3320093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass DeclaratorInfo;
3420093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass FieldDecl;
3520093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass FunctionDecl;
3620093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass ParmVarDecl;
3720093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass Sema;
3820093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass TypeLoc;
3920093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass VarDecl;
4020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
4120093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Describes an entity that is being initialized.
4220093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass InitializedEntity {
4320093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
4420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Specifies the kind of entity being initialized.
4520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum EntityKind {
4620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is a variable.
4720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Variable,
4820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is a function parameter.
4920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Parameter,
5020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is the result of a function call.
5120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Result,
5220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is an exception object that
5320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// is being thrown.
5420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Exception,
55a729bbb739ca97a8dc52b6b0495725590a0367dcAnders Carlsson    /// \brief The entity being initialized is a non-static data member
56a729bbb739ca97a8dc52b6b0495725590a0367dcAnders Carlsson    /// subobject.
57a729bbb739ca97a8dc52b6b0495725590a0367dcAnders Carlsson    EK_Member,
58a729bbb739ca97a8dc52b6b0495725590a0367dcAnders Carlsson    /// \brief The entity being initialized is an element of an array.
59a729bbb739ca97a8dc52b6b0495725590a0367dcAnders Carlsson    EK_ArrayElement,
6018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    /// \brief The entity being initialized is an object (or array of
6118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    /// objects) allocated via new.
6218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    EK_New,
6320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is a temporary object.
6420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Temporary,
6520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The entity being initialized is a base member subobject.
6620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    EK_Base,
674171766318a2564fbc9a739be0a2851f441c0d29Sean Hunt    /// \brief The initialization is being done by a delegating constructor.
68059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt    EK_Delegating,
69d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    /// \brief The entity being initialized is an element of a vector.
70cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    /// or vector.
71310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    EK_VectorElement,
72310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    /// \brief The entity being initialized is a field of block descriptor for
73310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    /// the copied-in c++ object.
74310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    EK_BlockElement
7520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
7620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
7720093b4bf698f292c664676987541d5103b65b15Douglas Gregorprivate:
7820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The kind of entity being initialized.
7920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  EntityKind Kind;
8020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
81cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief If non-NULL, the parent entity in which this
82cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// initialization occurs.
83cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  const InitializedEntity *Parent;
84cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
85d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  /// \brief The type of the object or reference being initialized.
86d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType Type;
8720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
8820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  union {
89f85e193739c953358c865005855253af4f68a497John McCall    /// \brief When Kind == EK_Variable or EK_Member, the VarDecl or
90f85e193739c953358c865005855253af4f68a497John McCall    /// FieldDecl, respectively.
9120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    DeclaratorDecl *VariableOrMember;
92f85e193739c953358c865005855253af4f68a497John McCall
93f85e193739c953358c865005855253af4f68a497John McCall    /// \brief When Kind == EK_Parameter, the ParmVarDecl, with the
94f85e193739c953358c865005855253af4f68a497John McCall    /// low bit indicating whether the parameter is "consumed".
95f85e193739c953358c865005855253af4f68a497John McCall    uintptr_t Parameter;
9620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
97ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    /// \brief When Kind == EK_Temporary, the type source information for
98ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    /// the temporary.
99ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    TypeSourceInfo *TypeInfo;
100ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
1013c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    struct {
1023c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
1033c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      /// location of the 'return', 'throw', or 'new' keyword,
1043c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      /// respectively. When Kind == EK_Temporary, the location where
1053c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      /// the temporary is being created.
1063c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      unsigned Location;
1073c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
1084926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor      /// \brief Whether the entity being initialized may end up using the
1094926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor      /// named return value optimization (NRVO).
1103c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor      bool NRVO;
1113c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    } LocAndNRVO;
11220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
11320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief When Kind == EK_Base, the base specifier that provides the
114711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    /// base class. The lower bit specifies whether the base is an inherited
115711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    /// virtual base.
116711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    uintptr_t Base;
117cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
118711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the
119711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    /// index of the array or vector element being initialized.
120cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    unsigned Index;
12120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
12220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
12320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializedEntity() { }
12420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
12520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a variable.
12620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializedEntity(VarDecl *Var)
127d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    : Kind(EK_Variable), Parent(0), Type(Var->getType()),
128606f65665421d594458e1409b4413005fb521c25Francois Pichet      VariableOrMember(Var) { }
12920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
130a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  /// \brief Create the initialization entity for the result of a
131a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  /// function, throwing an object, performing an explicit cast, or
132a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  /// initializing a parameter for which there is no declaration.
1333c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
1343c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                    bool NRVO = false)
1353c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    : Kind(Kind), Parent(0), Type(Type)
1363c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  {
1373c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    LocAndNRVO.Location = Loc.getRawEncoding();
1383c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    LocAndNRVO.NRVO = NRVO;
1393c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  }
14020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
14120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a member subobject.
142cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
143d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
144606f65665421d594458e1409b4413005fb521c25Francois Pichet      VariableOrMember(Member) { }
14520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
146cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief Create the initialization entity for an array element.
147cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitializedEntity(ASTContext &Context, unsigned Index,
148cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                    const InitializedEntity &Parent);
149cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
15020093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
15120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a variable.
15220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializedEntity InitializeVariable(VarDecl *Var) {
15320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return InitializedEntity(Var);
15420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
15520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
15620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a parameter.
157745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  static InitializedEntity InitializeParameter(ASTContext &Context,
158745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                                               ParmVarDecl *Parm) {
159f85e193739c953358c865005855253af4f68a497John McCall    bool Consumed = (Context.getLangOptions().ObjCAutoRefCount &&
160f85e193739c953358c865005855253af4f68a497John McCall                     Parm->hasAttr<NSConsumedAttr>());
161f85e193739c953358c865005855253af4f68a497John McCall
162f85e193739c953358c865005855253af4f68a497John McCall    InitializedEntity Entity;
163f85e193739c953358c865005855253af4f68a497John McCall    Entity.Kind = EK_Parameter;
164f85e193739c953358c865005855253af4f68a497John McCall    Entity.Type = Context.getVariableArrayDecayedType(
165f85e193739c953358c865005855253af4f68a497John McCall                                       Parm->getType().getUnqualifiedType());
166f85e193739c953358c865005855253af4f68a497John McCall    Entity.Parent = 0;
16782d1cc06ea533267e24ffe8b5885062ca062b479John McCall    Entity.Parameter
16882d1cc06ea533267e24ffe8b5885062ca062b479John McCall      = (static_cast<uintptr_t>(Consumed) | reinterpret_cast<uintptr_t>(Parm));
169f85e193739c953358c865005855253af4f68a497John McCall    return Entity;
17020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
17120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
172a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  /// \brief Create the initialization entity for a parameter that is
173a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  /// only known by its type.
174745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  static InitializedEntity InitializeParameter(ASTContext &Context,
175f85e193739c953358c865005855253af4f68a497John McCall                                               QualType Type,
176f85e193739c953358c865005855253af4f68a497John McCall                                               bool Consumed) {
177688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    InitializedEntity Entity;
178688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    Entity.Kind = EK_Parameter;
179745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian    Entity.Type = Context.getVariableArrayDecayedType(Type);
180688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    Entity.Parent = 0;
181f85e193739c953358c865005855253af4f68a497John McCall    Entity.Parameter = (Consumed);
182688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    return Entity;
183a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  }
184a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor
18520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for the result of a function.
18620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
1873c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                                            QualType Type, bool NRVO) {
1883c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
18920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
19020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
191310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
192310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian                                           QualType Type, bool NRVO) {
193310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
194310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  }
195310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian
19620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for an exception object.
19720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializedEntity InitializeException(SourceLocation ThrowLoc,
1983c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                                               QualType Type, bool NRVO) {
1993c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
20020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
20118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
20218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  /// \brief Create the initialization entity for an object allocated via new.
203d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
204d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    return InitializedEntity(EK_New, NewLoc, Type);
20518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
20620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
20720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a temporary.
208d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  static InitializedEntity InitializeTemporary(QualType Type) {
209d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    return InitializedEntity(EK_Temporary, SourceLocation(), Type);
21020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
211ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
212ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  /// \brief Create the initialization entity for a temporary.
213ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo) {
214ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    InitializedEntity Result(EK_Temporary, SourceLocation(),
215ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                             TypeInfo->getType());
216ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    Result.TypeInfo = TypeInfo;
217ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    return Result;
218ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  }
219ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
22020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create the initialization entity for a base class subobject.
22120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializedEntity InitializeBase(ASTContext &Context,
222711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson                                          CXXBaseSpecifier *Base,
223711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson                                          bool IsInheritedVirtualBase);
2244171766318a2564fbc9a739be0a2851f441c0d29Sean Hunt
2254171766318a2564fbc9a739be0a2851f441c0d29Sean Hunt  /// \brief Create the initialization entity for a delegated constructor.
2264171766318a2564fbc9a739be0a2851f441c0d29Sean Hunt  static InitializedEntity InitializeDelegation(QualType Type) {
227059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt    return InitializedEntity(EK_Delegating, SourceLocation(), Type);
2284171766318a2564fbc9a739be0a2851f441c0d29Sean Hunt  }
22920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
230cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief Create the initialization entity for a member subobject.
231cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  static InitializedEntity InitializeMember(FieldDecl *Member,
232cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                          const InitializedEntity *Parent = 0) {
233cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    return InitializedEntity(Member, Parent);
23420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
23520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
23600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet  /// \brief Create the initialization entity for a member subobject.
23700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet  static InitializedEntity InitializeMember(IndirectFieldDecl *Member,
23800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                      const InitializedEntity *Parent = 0) {
23900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    return InitializedEntity(Member->getAnonField(), Parent);
24000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet  }
24100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
242cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief Create the initialization entity for an array element.
243cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  static InitializedEntity InitializeElement(ASTContext &Context,
244cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                             unsigned Index,
245cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                             const InitializedEntity &Parent) {
246cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    return InitializedEntity(Context, Index, Parent);
247cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  }
248cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
24920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine the kind of initialization.
25020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  EntityKind getKind() const { return Kind; }
25120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
252cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief Retrieve the parent of the entity being initialized, when
253fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  /// the initialization itself is occurring within the context of a
254cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// larger initialization.
255cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  const InitializedEntity *getParent() const { return Parent; }
256cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
25720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve type being initialized.
258d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType getType() const { return Type; }
25920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
260ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  /// \brief Retrieve complete type-source information for the object being
261ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  /// constructed, if known.
262ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  TypeSourceInfo *getTypeSourceInfo() const {
263ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    if (Kind == EK_Temporary)
264ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor      return TypeInfo;
265ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
266ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    return 0;
267ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  }
268ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
26999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  /// \brief Retrieve the name of the entity being initialized.
27099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  DeclarationName getName() const;
2717abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
2727abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  /// \brief Retrieve the variable, parameter, or field being
2737abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  /// initialized.
2747abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  DeclaratorDecl *getDecl() const;
2757abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
2763c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  /// \brief Determine whether this initialization allows the named return
2773c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  /// value optimization, which also applies to thrown objects.
2783c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  bool allowsNRVO() const;
279f85e193739c953358c865005855253af4f68a497John McCall
280f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Determine whether this initialization consumes the
281f85e193739c953358c865005855253af4f68a497John McCall  /// parameter.
282f85e193739c953358c865005855253af4f68a497John McCall  bool isParameterConsumed() const {
283f85e193739c953358c865005855253af4f68a497John McCall    assert(getKind() == EK_Parameter && "Not a parameter");
284f85e193739c953358c865005855253af4f68a497John McCall    return (Parameter & 1);
285f85e193739c953358c865005855253af4f68a497John McCall  }
2863c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
2879db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  /// \brief Retrieve the base specifier.
2889db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  CXXBaseSpecifier *getBaseSpecifier() const {
2899db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    assert(getKind() == EK_Base && "Not a base specifier");
290711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
291711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson  }
292711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson
293711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson  /// \brief Return whether the base is an inherited virtual base.
294711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson  bool isInheritedVirtualBase() const {
295711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    assert(getKind() == EK_Base && "Not a base specifier");
296711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    return Base & 0x1;
2979db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  }
2989db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
29920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine the location of the 'return' keyword when initializing
30020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// the result of a function call.
30120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation getReturnLoc() const {
30220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    assert(getKind() == EK_Result && "No 'return' location!");
3033c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
30420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
305f85e193739c953358c865005855253af4f68a497John McCall
30620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine the location of the 'throw' keyword when initializing
30720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// an exception object.
30820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation getThrowLoc() const {
30920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    assert(getKind() == EK_Exception && "No 'throw' location!");
3103c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
31120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
312cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
313cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief If this is already the initializer for an array or vector
314cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// element, sets the element index.
315cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  void setElementIndex(unsigned Index) {
316d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
317cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    this->Index = Index;
318cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  }
31920093b4bf698f292c664676987541d5103b65b15Douglas Gregor};
32020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
32120093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Describes the kind of initialization being performed, along with
32220093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// location information for tokens related to the initialization (equal sign,
32320093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// parentheses).
32420093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass InitializationKind {
32520093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
32620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The kind of initialization being performed.
32720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum InitKind {
32820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    IK_Direct,  ///< Direct initialization
32920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    IK_Copy,    ///< Copy initialization
33020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    IK_Default, ///< Default initialization
33120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    IK_Value    ///< Value initialization
33220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
33320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
33420093b4bf698f292c664676987541d5103b65b15Douglas Gregorprivate:
33520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The kind of initialization that we're storing.
33620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum StoredInitKind {
33720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SIK_Direct = IK_Direct,   ///< Direct initialization
33820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SIK_Copy = IK_Copy,       ///< Copy initialization
33920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SIK_Default = IK_Default, ///< Default initialization
34020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SIK_Value = IK_Value,     ///< Value initialization
341cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    SIK_ImplicitValue,        ///< Implicit value initialization
34220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SIK_DirectCast,  ///< Direct initialization due to a cast
343f85e193739c953358c865005855253af4f68a497John McCall    /// \brief Direct initialization due to a C-style cast.
344f85e193739c953358c865005855253af4f68a497John McCall    SIK_DirectCStyleCast,
345f85e193739c953358c865005855253af4f68a497John McCall    /// \brief Direct initialization due to a functional-style cast.
346f85e193739c953358c865005855253af4f68a497John McCall    SIK_DirectFunctionalCast
34720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
34820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
34920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The kind of initialization being performed.
35020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  StoredInitKind Kind;
35120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
35220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The source locations involved in the initialization.
35320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation Locations[3];
35420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
35520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
35620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                     SourceLocation Loc2, SourceLocation Loc3)
35720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    : Kind(Kind)
35820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  {
35920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Locations[0] = Loc1;
36020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Locations[1] = Loc2;
36120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Locations[2] = Loc3;
36220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
36320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
36420093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
36520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create a direct initialization.
36620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializationKind CreateDirect(SourceLocation InitLoc,
36720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         SourceLocation LParenLoc,
36820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         SourceLocation RParenLoc) {
36920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
37020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
37120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
372f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Create a direct initialization due to a cast that isn't a C-style
373f85e193739c953358c865005855253af4f68a497John McCall  /// or functional cast.
374f85e193739c953358c865005855253af4f68a497John McCall  static InitializationKind CreateCast(SourceRange TypeRange) {
375f85e193739c953358c865005855253af4f68a497John McCall    return InitializationKind(SIK_DirectCast,
37620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              TypeRange.getBegin(), TypeRange.getBegin(),
37720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                              TypeRange.getEnd());
37820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
37920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
380f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Create a direct initialization for a C-style cast.
381f85e193739c953358c865005855253af4f68a497John McCall  static InitializationKind CreateCStyleCast(SourceLocation StartLoc,
382f85e193739c953358c865005855253af4f68a497John McCall                                             SourceRange TypeRange) {
383f85e193739c953358c865005855253af4f68a497John McCall    return InitializationKind(SIK_DirectCStyleCast,
384f85e193739c953358c865005855253af4f68a497John McCall                              StartLoc, TypeRange.getBegin(),
385f85e193739c953358c865005855253af4f68a497John McCall                              TypeRange.getEnd());
386f85e193739c953358c865005855253af4f68a497John McCall  }
387f85e193739c953358c865005855253af4f68a497John McCall
388f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Create a direct initialization for a functional cast.
389f85e193739c953358c865005855253af4f68a497John McCall  static InitializationKind CreateFunctionalCast(SourceRange TypeRange) {
390f85e193739c953358c865005855253af4f68a497John McCall    return InitializationKind(SIK_DirectFunctionalCast,
391f85e193739c953358c865005855253af4f68a497John McCall                              TypeRange.getBegin(), TypeRange.getBegin(),
392f85e193739c953358c865005855253af4f68a497John McCall                              TypeRange.getEnd());
393f85e193739c953358c865005855253af4f68a497John McCall  }
394f85e193739c953358c865005855253af4f68a497John McCall
39520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create a copy initialization.
39620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializationKind CreateCopy(SourceLocation InitLoc,
39720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       SourceLocation EqualLoc) {
39820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
39920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
40020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
40120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create a default initialization.
40220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializationKind CreateDefault(SourceLocation InitLoc) {
40320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
40420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
40520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
40620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Create a value initialization.
40720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  static InitializationKind CreateValue(SourceLocation InitLoc,
40820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                        SourceLocation LParenLoc,
409cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                        SourceLocation RParenLoc,
410cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                        bool isImplicit = false) {
411cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
412cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                              InitLoc, LParenLoc, RParenLoc);
41320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
41420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
41520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine the initialization kind.
41620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitKind getKind() const {
417cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    if (Kind > SIK_ImplicitValue)
41820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return IK_Direct;
419cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    if (Kind == SIK_ImplicitValue)
420cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      return IK_Value;
421cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
42220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return (InitKind)Kind;
42320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
42420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
42520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine whether this initialization is an explicit cast.
42620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isExplicitCast() const {
427f85e193739c953358c865005855253af4f68a497John McCall    return Kind == SIK_DirectCast ||
428f85e193739c953358c865005855253af4f68a497John McCall           Kind == SIK_DirectCStyleCast ||
429f85e193739c953358c865005855253af4f68a497John McCall           Kind == SIK_DirectFunctionalCast;
43020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
43120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
43220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine whether this initialization is a C-style cast.
43320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isCStyleOrFunctionalCast() const {
434f85e193739c953358c865005855253af4f68a497John McCall    return Kind == SIK_DirectCStyleCast || Kind == SIK_DirectFunctionalCast;
435f85e193739c953358c865005855253af4f68a497John McCall  }
436f85e193739c953358c865005855253af4f68a497John McCall
437f85e193739c953358c865005855253af4f68a497John McCall  /// brief Determine whether this is a C-style cast.
438f85e193739c953358c865005855253af4f68a497John McCall  bool isCStyleCast() const {
439f85e193739c953358c865005855253af4f68a497John McCall    return Kind == SIK_DirectCStyleCast;
440f85e193739c953358c865005855253af4f68a497John McCall  }
441f85e193739c953358c865005855253af4f68a497John McCall
442f85e193739c953358c865005855253af4f68a497John McCall  /// brief Determine whether this is a functional-style cast.
443f85e193739c953358c865005855253af4f68a497John McCall  bool isFunctionalCast() const {
444f85e193739c953358c865005855253af4f68a497John McCall    return Kind == SIK_DirectFunctionalCast;
44520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
446cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
447cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// \brief Determine whether this initialization is an implicit
448cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// value-initialization, e.g., as occurs during aggregate
449cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  /// initialization.
450cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
451cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
45220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve the location at which initialization is occurring.
45320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation getLocation() const { return Locations[0]; }
45420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
45520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve the source range that covers the initialization.
45620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceRange getRange() const {
45771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    return SourceRange(Locations[0], Locations[2]);
45820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
45920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
46020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve the location of the equal sign for copy initialization
46120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// (if present).
46220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation getEqualLoc() const {
46320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    assert(Kind == SIK_Copy && "Only copy initialization has an '='");
46420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return Locations[1];
46520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
46620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
46710f8e319569c8b8b4603d8a72ce01a7a01502c85Fariborz Jahanian  bool isCopyInit() const { return Kind == SIK_Copy; }
46810f8e319569c8b8b4603d8a72ce01a7a01502c85Fariborz Jahanian
46920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve the source range containing the locations of the open
47020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// and closing parentheses for value and direct initializations.
47120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceRange getParenRange() const {
47220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    assert((getKind() == IK_Direct || Kind == SIK_Value) &&
47320093b4bf698f292c664676987541d5103b65b15Douglas Gregor           "Only direct- and value-initialization have parentheses");
47420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return SourceRange(Locations[1], Locations[2]);
47520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
47620093b4bf698f292c664676987541d5103b65b15Douglas Gregor};
47720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
47820093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Describes the sequence of initializations required to initialize
47920093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// a given object or reference with a set of arguments.
48020093b4bf698f292c664676987541d5103b65b15Douglas Gregorclass InitializationSequence {
48120093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
48220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Describes the kind of initialization sequence computed.
48320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum SequenceKind {
48420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief A failed initialization sequence. The failure kind tells what
48520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// happened.
48620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FailedSequence = 0,
4877491c499e826682e128a400038361ebcbde30eecSebastian Redl
48820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief A dependent initialization, which could not be
48920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// type-checked due to the presence of dependent types or
4907491c499e826682e128a400038361ebcbde30eecSebastian Redl    /// dependently-typed expressions.
49120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    DependentSequence,
49220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
4937491c499e826682e128a400038361ebcbde30eecSebastian Redl    /// \brief A normal sequence.
4943b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl    NormalSequence
49520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
49620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
49720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Describes the kind of a particular step in an initialization
49820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// sequence.
49920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum StepKind {
50020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Resolve the address of an overloaded function to a specific
50120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// function declaration.
50220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_ResolveAddressOfOverloadedFunction,
50320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform a derived-to-base cast, producing an rvalue.
50420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_CastDerivedToBaseRValue,
505906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    /// \brief Perform a derived-to-base cast, producing an xvalue.
506906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    SK_CastDerivedToBaseXValue,
50720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform a derived-to-base cast, producing an lvalue.
50820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_CastDerivedToBaseLValue,
50920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Reference binding to an lvalue.
51020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_BindReference,
51120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Reference binding to a temporary.
51220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_BindReferenceToTemporary,
513523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    /// \brief An optional copy of a temporary object to another
514523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    /// temporary object, which is permitted (but not required) by
515523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    /// C++98/03 but not C++0x.
516523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    SK_ExtraneousCopyToTemporary,
51720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform a user-defined conversion, either via a conversion
51820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// function or via a constructor.
51920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_UserConversion,
52020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform a qualification conversion, producing an rvalue.
52120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_QualificationConversionRValue,
522906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    /// \brief Perform a qualification conversion, producing an xvalue.
523906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    SK_QualificationConversionXValue,
52420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform a qualification conversion, producing an lvalue.
52520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SK_QualificationConversionLValue,
52620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Perform an implicit conversion sequence.
527d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    SK_ConversionSequence,
528d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    /// \brief Perform list-initialization
52951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    SK_ListInitialization,
53051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    /// \brief Perform initialization via a constructor.
53171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    SK_ConstructorInitialization,
53271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    /// \brief Zero-initialize the object
53318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    SK_ZeroInitialization,
53418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    /// \brief C assignment
535cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    SK_CAssignment,
536cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    /// \brief Initialization by string
537569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    SK_StringInit,
538569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    /// \brief An initialization that "converts" an Objective-C object
539569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    /// (not a point to an object) to another Objective-C object type.
540cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    SK_ObjCObjectConversion,
541cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    /// \brief Array initialization (from an array rvalue).
542cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    /// This is a GNU C extension.
543f85e193739c953358c865005855253af4f68a497John McCall    SK_ArrayInit,
544f85e193739c953358c865005855253af4f68a497John McCall    /// \brief Pass an object by indirect copy-and-restore.
545f85e193739c953358c865005855253af4f68a497John McCall    SK_PassByIndirectCopyRestore,
546f85e193739c953358c865005855253af4f68a497John McCall    /// \brief Pass an object by indirect restore.
547f85e193739c953358c865005855253af4f68a497John McCall    SK_PassByIndirectRestore,
548f85e193739c953358c865005855253af4f68a497John McCall    /// \brief Produce an Objective-C object pointer.
549f85e193739c953358c865005855253af4f68a497John McCall    SK_ProduceObjCObject
55020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
55120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
55220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief A single step in the initialization sequence.
55320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  class Step {
55420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  public:
55520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief The kind of conversion or initialization step we are taking.
55620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    StepKind Kind;
55720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
55820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // \brief The type that results from this initialization.
55920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    QualType Type;
56020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
56120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    union {
56220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
56320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      /// SK_UserConversion, the function that the expression should be
56420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      /// resolved to or the conversion function to call, respectively.
565b13b737a2450167c82e148590e8019b839ce6b98John McCall      ///
566b13b737a2450167c82e148590e8019b839ce6b98John McCall      /// Always a FunctionDecl.
567b13b737a2450167c82e148590e8019b839ce6b98John McCall      /// For conversion decls, the naming class is the source type.
568b13b737a2450167c82e148590e8019b839ce6b98John McCall      /// For construct decls, the naming class is the target type.
5699aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      struct {
5709aa472c45d2bd81b7b52c225e8acc560d716db97John McCall        FunctionDecl *Function;
5719aa472c45d2bd81b7b52c225e8acc560d716db97John McCall        DeclAccessPair FoundDecl;
5729aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      } Function;
57320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
57420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      /// \brief When Kind = SK_ConversionSequence, the implicit conversion
57520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      /// sequence
57620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      ImplicitConversionSequence *ICS;
57720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    };
57820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
57920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    void Destroy();
58020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
58120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
58220093b4bf698f292c664676987541d5103b65b15Douglas Gregorprivate:
58320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The kind of initialization sequence computed.
58420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum SequenceKind SequenceKind;
58520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
58620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Steps taken by this initialization.
587686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<Step, 4> Steps;
58820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
58920093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
59020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Describes why initialization failed.
59120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum FailureKind {
59220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Too many initializers provided for a reference.
59320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_TooManyInitsForReference,
59420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Array must be initialized with an initializer list.
59520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_ArrayNeedsInitList,
59620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Array must be initialized with an initializer list or a
59720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// string literal.
59820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_ArrayNeedsInitListOrStringLiteral,
599cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    /// \brief Array type mismatch.
600cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    FK_ArrayTypeMismatch,
601cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    /// \brief Non-constant array initializer
602cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    FK_NonConstantArrayInit,
60320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Cannot resolve the address of an overloaded function.
60420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_AddressOfOverloadFailed,
60520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Overloading due to reference initialization failed.
60620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_ReferenceInitOverloadFailed,
60720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Non-const lvalue reference binding to a temporary.
60820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_NonConstLValueReferenceBindingToTemporary,
60920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Non-const lvalue reference binding to an lvalue of unrelated
61020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// type.
61120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_NonConstLValueReferenceBindingToUnrelated,
61220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Rvalue reference binding to an lvalue.
61320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_RValueReferenceBindingToLValue,
61420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Reference binding drops qualifiers.
61520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_ReferenceInitDropsQualifiers,
61620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Reference binding failed.
61720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    FK_ReferenceInitFailed,
61820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    /// \brief Implicit conversion failed.
619d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    FK_ConversionFailed,
620429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    /// \brief Implicit conversion failed.
621429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    FK_ConversionFromPropertyFailed,
622d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    /// \brief Too many initializers for scalar
623d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    FK_TooManyInitsForScalar,
624d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    /// \brief Reference initialization from an initializer list
625d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    FK_ReferenceBindingToInitList,
626d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    /// \brief Initialization of some unused destination type with an
627d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    /// initializer list.
6284a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    FK_InitListBadDestinationType,
6294a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    /// \brief Overloading for a user-defined conversion failed.
63051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    FK_UserConversionOverloadFailed,
63151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    /// \brief Overloaded for initialization by constructor failed.
63299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    FK_ConstructorOverloadFailed,
63399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    /// \brief Default-initialization of a 'const' object.
63472a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor    FK_DefaultInitOfConst,
63572a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor    /// \brief Initialization of an incomplete type.
63672a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor    FK_Incomplete
63720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  };
63820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
63920093b4bf698f292c664676987541d5103b65b15Douglas Gregorprivate:
64020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The reason why initialization failued.
64120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  FailureKind Failure;
64220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
64320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The failed result of overload resolution.
64420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadingResult FailedOverloadResult;
64520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
64620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief The candidate set created when initialization failed.
64720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet FailedCandidateSet;
648a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor
649a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  /// \brief Prints a follow-up note that highlights the location of
650a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  /// the initialized entity, if it's remote.
651a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
652a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor
65320093b4bf698f292c664676987541d5103b65b15Douglas Gregorpublic:
65420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Try to perform initialization of the given entity, creating a
65520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// record of the steps required to perform the initialization.
65620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
65720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// The generated initialization sequence will either contain enough
65820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// information to diagnose
65920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
66020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param S the semantic analysis object.
66120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
66220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Entity the entity being initialized.
66320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
66420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Kind the kind of initialization being performed.
66520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
66620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Args the argument(s) provided for initialization.
66720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
66820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param NumArgs the number of arguments provided for initialization.
66920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializationSequence(Sema &S,
67020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         const InitializedEntity &Entity,
67120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         const InitializationKind &Kind,
67220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         Expr **Args,
67320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                         unsigned NumArgs);
67420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
67520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ~InitializationSequence();
67620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
67720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Perform the actual initialization of the given entity based on
67820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// the computed initialization sequence.
67920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
68020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param S the semantic analysis object.
68120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
68220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Entity the entity being initialized.
68320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
68420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Kind the kind of initialization being performed.
68520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
68620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Args the argument(s) provided for initialization, ownership of
687fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  /// which is transferred into the routine.
68820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
689d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// \param ResultType if non-NULL, will be set to the type of the
690d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// initialized object, which is the type of the declaration in most
691d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// cases. However, when the initialized object is a variable of
692d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// incomplete array type and the initializer is an initializer
693d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// list, this type will be set to the completed array type.
694d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  ///
69520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \returns an expression that performs the actual object initialization, if
69620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// the initialization is well-formed. Otherwise, emits diagnostics
69720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// and returns an invalid expression.
69860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Perform(Sema &S,
69960d7b3a319d84d688752be3870615ac0f111fb16John McCall                     const InitializedEntity &Entity,
70060d7b3a319d84d688752be3870615ac0f111fb16John McCall                     const InitializationKind &Kind,
701120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall                     MultiExprArg Args,
70260d7b3a319d84d688752be3870615ac0f111fb16John McCall                     QualType *ResultType = 0);
70320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
70420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Diagnose an potentially-invalid initialization sequence.
70520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
70620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \returns true if the initialization sequence was ill-formed,
70720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// false otherwise.
70820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool Diagnose(Sema &S,
70920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                const InitializedEntity &Entity,
71020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                const InitializationKind &Kind,
71120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                Expr **Args, unsigned NumArgs);
71220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
71320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine the kind of initialization sequence computed.
71420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  enum SequenceKind getKind() const { return SequenceKind; }
71520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
71620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Set the kind of sequence computed.
71720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
71820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
71920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine whether the initialization sequence is valid.
7207491c499e826682e128a400038361ebcbde30eecSebastian Redl  operator bool() const { return !Failed(); }
721383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl
722383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl  /// \brief Determine whether the initialization sequence is invalid.
723383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl  bool Failed() const { return SequenceKind == FailedSequence; }
72420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
725686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  typedef SmallVector<Step, 4>::const_iterator step_iterator;
72620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  step_iterator step_begin() const { return Steps.begin(); }
72720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  step_iterator step_end()   const { return Steps.end(); }
72820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
729b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  /// \brief Determine whether this initialization is a direct reference
730b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  /// binding (C++ [dcl.init.ref]).
731b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  bool isDirectReferenceBinding() const;
732b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor
733b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  /// \brief Determine whether this initialization failed due to an ambiguity.
734b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  bool isAmbiguous() const;
735b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor
736d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  /// \brief Determine whether this initialization is direct call to a
737d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  /// constructor.
738d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  bool isConstructorInitialization() const;
739d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor
74020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step in the initialization that resolves the address
74120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// of an overloaded function to a specific function declaration.
74220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
74320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param Function the function to which the overloaded function reference
74420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// resolves.
7456bb8017bb9e828d118e15e59d71c66bba323c364John McCall  void AddAddressOverloadResolutionStep(FunctionDecl *Function,
7466bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                        DeclAccessPair Found);
74720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
74820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step in the initialization that performs a derived-to-
74920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// base cast.
75020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
75120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param BaseType the base type to which we will be casting.
75220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
75320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \param IsLValue true if the result of this cast will be treated as
75420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// an lvalue.
755906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  void AddDerivedToBaseCastStep(QualType BaseType,
7565baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                ExprValueKind Category);
75720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
75820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step binding a reference to an object.
75920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  ///
760523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// \param BindingTemporary True if we are binding a reference to a temporary
76120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// object (thereby extending its lifetime); false if we are binding to an
76220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// lvalue or an lvalue treated as an rvalue.
763523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  ///
764523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// \param UnnecessaryCopy True if we should check for a copy
765523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// constructor for a completely unnecessary but
76620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  void AddReferenceBindingStep(QualType T, bool BindingTemporary);
767523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
768523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// \brief Add a new step that makes an extraneous copy of the input
769523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// to a temporary of the same class type.
770523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  ///
771523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// This extraneous copy only occurs during reference binding in
772523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// C++98/03, where we are permitted (but not required) to introduce
773523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// an extra copy. At a bare minimum, we must check that we could
774523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// call the copy constructor, and produce a diagnostic if the copy
775523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// constructor is inaccessible or no copy constructor matches.
776523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  //
777523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  /// \param T The type of the temporary being created.
778523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  void AddExtraneousCopyToTemporary(QualType T);
779523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
78020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step invoking a conversion function, which is either
78120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// a constructor or a conversion function.
782b13b737a2450167c82e148590e8019b839ce6b98John McCall  void AddUserConversionStep(FunctionDecl *Function,
7839aa472c45d2bd81b7b52c225e8acc560d716db97John McCall                             DeclAccessPair FoundDecl,
784b13b737a2450167c82e148590e8019b839ce6b98John McCall                             QualType T);
78520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
78620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step that performs a qualification conversion to the
78720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// given type.
788906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  void AddQualificationConversionStep(QualType Ty,
7895baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                     ExprValueKind Category);
79020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
79120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Add a new step that applies an implicit conversion sequence.
79220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
79320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                 QualType T);
794d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
795d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  /// \brief Add a list-initialiation step
796d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  void AddListInitializationStep(QualType T);
797d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
79871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  /// \brief Add a constructor-initialization step.
79951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
800b13b737a2450167c82e148590e8019b839ce6b98John McCall                                        AccessSpecifier Access,
80151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                                        QualType T);
80271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
80371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  /// \brief Add a zero-initialization step.
80471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  void AddZeroInitializationStep(QualType T);
80551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
80618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  /// \brief Add a C assignment step.
80718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  //
80818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  // FIXME: It isn't clear whether this should ever be needed;
80918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  // ideally, we would handle everything needed in C in the common
81018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  // path. However, that isn't the case yet.
81118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  void AddCAssignmentStep(QualType T);
81218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
813cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  /// \brief Add a string init step.
814cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  void AddStringInitStep(QualType T);
815cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
816569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  /// \brief Add an Objective-C object conversion step, which is
817569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  /// always a no-op.
818569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  void AddObjCObjectConversionStep(QualType T);
819569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
820cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  /// \brief Add an array initialization step.
821cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  void AddArrayInitStep(QualType T);
822cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
823f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Add a step to pass an object by indirect copy-restore.
824f85e193739c953358c865005855253af4f68a497John McCall  void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy);
825f85e193739c953358c865005855253af4f68a497John McCall
826f85e193739c953358c865005855253af4f68a497John McCall  /// \brief Add a step to "produce" an Objective-C object (by
827f85e193739c953358c865005855253af4f68a497John McCall  /// retaining it).
828f85e193739c953358c865005855253af4f68a497John McCall  void AddProduceObjCObjectStep(QualType T);
829f85e193739c953358c865005855253af4f68a497John McCall
83020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Note that this initialization sequence failed.
83120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  void SetFailed(FailureKind Failure) {
83220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SequenceKind = FailedSequence;
83320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    this->Failure = Failure;
83420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
83520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
83620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Note that this initialization sequence failed due to failed
83720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// overload resolution.
83820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
83920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
84020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Retrieve a reference to the candidate set when overload
84120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// resolution fails.
84220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet &getFailedCandidateSet() {
84320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return FailedCandidateSet;
84420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
84520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
84679ab2c8104ef5df233d271560ccc734836738e56John McCall  /// brief Get the overloading result, for when the initialization
84779ab2c8104ef5df233d271560ccc734836738e56John McCall  /// sequence failed due to a bad overload.
84879ab2c8104ef5df233d271560ccc734836738e56John McCall  OverloadingResult getFailedOverloadResult() const {
84979ab2c8104ef5df233d271560ccc734836738e56John McCall    return FailedOverloadResult;
85079ab2c8104ef5df233d271560ccc734836738e56John McCall  }
85179ab2c8104ef5df233d271560ccc734836738e56John McCall
85220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  /// \brief Determine why initialization failed.
85320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  FailureKind getFailureKind() const {
854d695d6bb7323672e29dbb1556a3dafde3d3b2732Sebastian Redl    assert(Failed() && "Not an initialization failure!");
85520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return Failure;
85620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
85779ab2c8104ef5df233d271560ccc734836738e56John McCall
858de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  /// \brief Dump a representation of this initialization sequence to
859de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  /// the given stream, for debugging purposes.
8608cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  void dump(raw_ostream &OS) const;
861de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
862de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  /// \brief Dump a representation of this initialization sequence to
863de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  /// standard error, for debugging purposes.
864de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  void dump() const;
86520093b4bf698f292c664676987541d5103b65b15Douglas Gregor};
86620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
86720093b4bf698f292c664676987541d5103b65b15Douglas Gregor} // end namespace clang
86820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
869e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#endif // LLVM_CLANG_SEMA_INITIALIZATION_H
870