xref: /minix3/external/bsd/llvm/dist/clang/include/clang/Sema/Initialization.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- Initialization.h - Semantic Analysis for Initializers --*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file provides supporting data types for initialization of objects.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc #ifndef LLVM_CLANG_SEMA_INITIALIZATION_H
14f4a2713aSLionel Sambuc #define LLVM_CLANG_SEMA_INITIALIZATION_H
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
18f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
19f4a2713aSLionel Sambuc #include "clang/AST/UnresolvedSet.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/SourceLocation.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/Overload.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Ownership.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/PointerIntPair.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
25f4a2713aSLionel Sambuc #include <cassert>
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc namespace clang {
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc class CXXBaseSpecifier;
30f4a2713aSLionel Sambuc class DeclaratorDecl;
31f4a2713aSLionel Sambuc class DeclaratorInfo;
32f4a2713aSLionel Sambuc class FieldDecl;
33f4a2713aSLionel Sambuc class FunctionDecl;
34f4a2713aSLionel Sambuc class ParmVarDecl;
35f4a2713aSLionel Sambuc class Sema;
36f4a2713aSLionel Sambuc class TypeLoc;
37f4a2713aSLionel Sambuc class VarDecl;
38f4a2713aSLionel Sambuc class ObjCMethodDecl;
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc /// \brief Describes an entity that is being initialized.
41f4a2713aSLionel Sambuc class InitializedEntity {
42f4a2713aSLionel Sambuc public:
43f4a2713aSLionel Sambuc   /// \brief Specifies the kind of entity being initialized.
44f4a2713aSLionel Sambuc   enum EntityKind {
45f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a variable.
46f4a2713aSLionel Sambuc     EK_Variable,
47f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a function parameter.
48f4a2713aSLionel Sambuc     EK_Parameter,
49f4a2713aSLionel Sambuc     /// \brief The entity being initialized is the result of a function call.
50f4a2713aSLionel Sambuc     EK_Result,
51f4a2713aSLionel Sambuc     /// \brief The entity being initialized is an exception object that
52f4a2713aSLionel Sambuc     /// is being thrown.
53f4a2713aSLionel Sambuc     EK_Exception,
54f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a non-static data member
55f4a2713aSLionel Sambuc     /// subobject.
56f4a2713aSLionel Sambuc     EK_Member,
57f4a2713aSLionel Sambuc     /// \brief The entity being initialized is an element of an array.
58f4a2713aSLionel Sambuc     EK_ArrayElement,
59f4a2713aSLionel Sambuc     /// \brief The entity being initialized is an object (or array of
60f4a2713aSLionel Sambuc     /// objects) allocated via new.
61f4a2713aSLionel Sambuc     EK_New,
62f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a temporary object.
63f4a2713aSLionel Sambuc     EK_Temporary,
64f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a base member subobject.
65f4a2713aSLionel Sambuc     EK_Base,
66f4a2713aSLionel Sambuc     /// \brief The initialization is being done by a delegating constructor.
67f4a2713aSLionel Sambuc     EK_Delegating,
68f4a2713aSLionel Sambuc     /// \brief The entity being initialized is an element of a vector.
69f4a2713aSLionel Sambuc     /// or vector.
70f4a2713aSLionel Sambuc     EK_VectorElement,
71f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a field of block descriptor for
72f4a2713aSLionel Sambuc     /// the copied-in c++ object.
73f4a2713aSLionel Sambuc     EK_BlockElement,
74f4a2713aSLionel Sambuc     /// \brief The entity being initialized is the real or imaginary part of a
75f4a2713aSLionel Sambuc     /// complex number.
76f4a2713aSLionel Sambuc     EK_ComplexElement,
77f4a2713aSLionel Sambuc     /// \brief The entity being initialized is the field that captures a
78f4a2713aSLionel Sambuc     /// variable in a lambda.
79f4a2713aSLionel Sambuc     EK_LambdaCapture,
80f4a2713aSLionel Sambuc     /// \brief The entity being initialized is the initializer for a compound
81f4a2713aSLionel Sambuc     /// literal.
82f4a2713aSLionel Sambuc     EK_CompoundLiteralInit,
83f4a2713aSLionel Sambuc     /// \brief The entity being implicitly initialized back to the formal
84f4a2713aSLionel Sambuc     /// result type.
85f4a2713aSLionel Sambuc     EK_RelatedResult,
86f4a2713aSLionel Sambuc     /// \brief The entity being initialized is a function parameter; function
87f4a2713aSLionel Sambuc     /// is member of group of audited CF APIs.
88f4a2713aSLionel Sambuc     EK_Parameter_CF_Audited
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc     // Note: err_init_conversion_failed in DiagnosticSemaKinds.td uses this
91f4a2713aSLionel Sambuc     // enum as an index for its first %select.  When modifying this list,
92f4a2713aSLionel Sambuc     // that diagnostic text needs to be updated as well.
93f4a2713aSLionel Sambuc   };
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc private:
96f4a2713aSLionel Sambuc   /// \brief The kind of entity being initialized.
97f4a2713aSLionel Sambuc   EntityKind Kind;
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   /// \brief If non-NULL, the parent entity in which this
100f4a2713aSLionel Sambuc   /// initialization occurs.
101f4a2713aSLionel Sambuc   const InitializedEntity *Parent;
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   /// \brief The type of the object or reference being initialized.
104f4a2713aSLionel Sambuc   QualType Type;
105f4a2713aSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc   /// \brief The mangling number for the next reference temporary to be created.
107*0a6a1f1dSLionel Sambuc   mutable unsigned ManglingNumber;
108*0a6a1f1dSLionel Sambuc 
109f4a2713aSLionel Sambuc   struct LN {
110f4a2713aSLionel Sambuc     /// \brief When Kind == EK_Result, EK_Exception, EK_New, the
111f4a2713aSLionel Sambuc     /// location of the 'return', 'throw', or 'new' keyword,
112f4a2713aSLionel Sambuc     /// respectively. When Kind == EK_Temporary, the location where
113f4a2713aSLionel Sambuc     /// the temporary is being created.
114f4a2713aSLionel Sambuc     unsigned Location;
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc     /// \brief Whether the entity being initialized may end up using the
117f4a2713aSLionel Sambuc     /// named return value optimization (NRVO).
118f4a2713aSLionel Sambuc     bool NRVO;
119f4a2713aSLionel Sambuc   };
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc   struct C {
122*0a6a1f1dSLionel Sambuc     /// \brief The name of the variable being captured by an EK_LambdaCapture.
123*0a6a1f1dSLionel Sambuc     IdentifierInfo *VarID;
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc     /// \brief The source location at which the capture occurs.
126f4a2713aSLionel Sambuc     unsigned Location;
127f4a2713aSLionel Sambuc   };
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   union {
130f4a2713aSLionel Sambuc     /// \brief When Kind == EK_Variable, or EK_Member, the VarDecl or
131f4a2713aSLionel Sambuc     /// FieldDecl, respectively.
132f4a2713aSLionel Sambuc     DeclaratorDecl *VariableOrMember;
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc     /// \brief When Kind == EK_RelatedResult, the ObjectiveC method where
135f4a2713aSLionel Sambuc     /// result type was implicitly changed to accommodate ARC semantics.
136f4a2713aSLionel Sambuc     ObjCMethodDecl *MethodDecl;
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc     /// \brief When Kind == EK_Parameter, the ParmVarDecl, with the
139f4a2713aSLionel Sambuc     /// low bit indicating whether the parameter is "consumed".
140f4a2713aSLionel Sambuc     uintptr_t Parameter;
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc     /// \brief When Kind == EK_Temporary or EK_CompoundLiteralInit, the type
143f4a2713aSLionel Sambuc     /// source information for the temporary.
144f4a2713aSLionel Sambuc     TypeSourceInfo *TypeInfo;
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc     struct LN LocAndNRVO;
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc     /// \brief When Kind == EK_Base, the base specifier that provides the
149f4a2713aSLionel Sambuc     /// base class. The lower bit specifies whether the base is an inherited
150f4a2713aSLionel Sambuc     /// virtual base.
151f4a2713aSLionel Sambuc     uintptr_t Base;
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc     /// \brief When Kind == EK_ArrayElement, EK_VectorElement, or
154f4a2713aSLionel Sambuc     /// EK_ComplexElement, the index of the array or vector element being
155f4a2713aSLionel Sambuc     /// initialized.
156f4a2713aSLionel Sambuc     unsigned Index;
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc     struct C Capture;
159f4a2713aSLionel Sambuc   };
160f4a2713aSLionel Sambuc 
InitializedEntity()161*0a6a1f1dSLionel Sambuc   InitializedEntity() : ManglingNumber(0) {}
162f4a2713aSLionel Sambuc 
163f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a variable.
InitializedEntity(VarDecl * Var)164f4a2713aSLionel Sambuc   InitializedEntity(VarDecl *Var)
165*0a6a1f1dSLionel Sambuc     : Kind(EK_Variable), Parent(nullptr), Type(Var->getType()),
166*0a6a1f1dSLionel Sambuc       ManglingNumber(0), VariableOrMember(Var) { }
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for the result of a
169f4a2713aSLionel Sambuc   /// function, throwing an object, performing an explicit cast, or
170f4a2713aSLionel Sambuc   /// initializing a parameter for which there is no declaration.
171f4a2713aSLionel Sambuc   InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
172f4a2713aSLionel Sambuc                     bool NRVO = false)
Kind(Kind)173*0a6a1f1dSLionel Sambuc     : Kind(Kind), Parent(nullptr), Type(Type), ManglingNumber(0)
174f4a2713aSLionel Sambuc   {
175f4a2713aSLionel Sambuc     LocAndNRVO.Location = Loc.getRawEncoding();
176f4a2713aSLionel Sambuc     LocAndNRVO.NRVO = NRVO;
177f4a2713aSLionel Sambuc   }
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a member subobject.
InitializedEntity(FieldDecl * Member,const InitializedEntity * Parent)180f4a2713aSLionel Sambuc   InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
181f4a2713aSLionel Sambuc     : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
182*0a6a1f1dSLionel Sambuc       ManglingNumber(0), VariableOrMember(Member) { }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for an array element.
185f4a2713aSLionel Sambuc   InitializedEntity(ASTContext &Context, unsigned Index,
186f4a2713aSLionel Sambuc                     const InitializedEntity &Parent);
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a lambda capture.
InitializedEntity(IdentifierInfo * VarID,QualType FieldType,SourceLocation Loc)189*0a6a1f1dSLionel Sambuc   InitializedEntity(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc)
190*0a6a1f1dSLionel Sambuc     : Kind(EK_LambdaCapture), Parent(nullptr), Type(FieldType),
191*0a6a1f1dSLionel Sambuc       ManglingNumber(0)
192f4a2713aSLionel Sambuc   {
193*0a6a1f1dSLionel Sambuc     Capture.VarID = VarID;
194f4a2713aSLionel Sambuc     Capture.Location = Loc.getRawEncoding();
195f4a2713aSLionel Sambuc   }
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc public:
198f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a variable.
InitializeVariable(VarDecl * Var)199f4a2713aSLionel Sambuc   static InitializedEntity InitializeVariable(VarDecl *Var) {
200f4a2713aSLionel Sambuc     return InitializedEntity(Var);
201f4a2713aSLionel Sambuc   }
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a parameter.
InitializeParameter(ASTContext & Context,ParmVarDecl * Parm)204f4a2713aSLionel Sambuc   static InitializedEntity InitializeParameter(ASTContext &Context,
205f4a2713aSLionel Sambuc                                                ParmVarDecl *Parm) {
206f4a2713aSLionel Sambuc     return InitializeParameter(Context, Parm, Parm->getType());
207f4a2713aSLionel Sambuc   }
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a parameter, but use
210f4a2713aSLionel Sambuc   /// another type.
InitializeParameter(ASTContext & Context,ParmVarDecl * Parm,QualType Type)211f4a2713aSLionel Sambuc   static InitializedEntity InitializeParameter(ASTContext &Context,
212f4a2713aSLionel Sambuc                                                ParmVarDecl *Parm,
213f4a2713aSLionel Sambuc                                                QualType Type) {
214f4a2713aSLionel Sambuc     bool Consumed = (Context.getLangOpts().ObjCAutoRefCount &&
215f4a2713aSLionel Sambuc                      Parm->hasAttr<NSConsumedAttr>());
216f4a2713aSLionel Sambuc 
217f4a2713aSLionel Sambuc     InitializedEntity Entity;
218f4a2713aSLionel Sambuc     Entity.Kind = EK_Parameter;
219f4a2713aSLionel Sambuc     Entity.Type =
220f4a2713aSLionel Sambuc       Context.getVariableArrayDecayedType(Type.getUnqualifiedType());
221*0a6a1f1dSLionel Sambuc     Entity.Parent = nullptr;
222f4a2713aSLionel Sambuc     Entity.Parameter
223f4a2713aSLionel Sambuc       = (static_cast<uintptr_t>(Consumed) | reinterpret_cast<uintptr_t>(Parm));
224f4a2713aSLionel Sambuc     return Entity;
225f4a2713aSLionel Sambuc   }
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a parameter that is
228f4a2713aSLionel Sambuc   /// only known by its type.
InitializeParameter(ASTContext & Context,QualType Type,bool Consumed)229f4a2713aSLionel Sambuc   static InitializedEntity InitializeParameter(ASTContext &Context,
230f4a2713aSLionel Sambuc                                                QualType Type,
231f4a2713aSLionel Sambuc                                                bool Consumed) {
232f4a2713aSLionel Sambuc     InitializedEntity Entity;
233f4a2713aSLionel Sambuc     Entity.Kind = EK_Parameter;
234f4a2713aSLionel Sambuc     Entity.Type = Context.getVariableArrayDecayedType(Type);
235*0a6a1f1dSLionel Sambuc     Entity.Parent = nullptr;
236f4a2713aSLionel Sambuc     Entity.Parameter = (Consumed);
237f4a2713aSLionel Sambuc     return Entity;
238f4a2713aSLionel Sambuc   }
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for the result of a function.
InitializeResult(SourceLocation ReturnLoc,QualType Type,bool NRVO)241f4a2713aSLionel Sambuc   static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
242f4a2713aSLionel Sambuc                                             QualType Type, bool NRVO) {
243f4a2713aSLionel Sambuc     return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
244f4a2713aSLionel Sambuc   }
245f4a2713aSLionel Sambuc 
InitializeBlock(SourceLocation BlockVarLoc,QualType Type,bool NRVO)246f4a2713aSLionel Sambuc   static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
247f4a2713aSLionel Sambuc                                            QualType Type, bool NRVO) {
248f4a2713aSLionel Sambuc     return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
249f4a2713aSLionel Sambuc   }
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for an exception object.
InitializeException(SourceLocation ThrowLoc,QualType Type,bool NRVO)252f4a2713aSLionel Sambuc   static InitializedEntity InitializeException(SourceLocation ThrowLoc,
253f4a2713aSLionel Sambuc                                                QualType Type, bool NRVO) {
254f4a2713aSLionel Sambuc     return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
255f4a2713aSLionel Sambuc   }
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for an object allocated via new.
InitializeNew(SourceLocation NewLoc,QualType Type)258f4a2713aSLionel Sambuc   static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
259f4a2713aSLionel Sambuc     return InitializedEntity(EK_New, NewLoc, Type);
260f4a2713aSLionel Sambuc   }
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a temporary.
InitializeTemporary(QualType Type)263f4a2713aSLionel Sambuc   static InitializedEntity InitializeTemporary(QualType Type) {
264f4a2713aSLionel Sambuc     InitializedEntity Result(EK_Temporary, SourceLocation(), Type);
265*0a6a1f1dSLionel Sambuc     Result.TypeInfo = nullptr;
266f4a2713aSLionel Sambuc     return Result;
267f4a2713aSLionel Sambuc   }
268f4a2713aSLionel Sambuc 
269f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a temporary.
InitializeTemporary(TypeSourceInfo * TypeInfo)270f4a2713aSLionel Sambuc   static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo) {
271f4a2713aSLionel Sambuc     InitializedEntity Result(EK_Temporary, SourceLocation(),
272f4a2713aSLionel Sambuc                              TypeInfo->getType());
273f4a2713aSLionel Sambuc     Result.TypeInfo = TypeInfo;
274f4a2713aSLionel Sambuc     return Result;
275f4a2713aSLionel Sambuc   }
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a related result.
InitializeRelatedResult(ObjCMethodDecl * MD,QualType Type)278f4a2713aSLionel Sambuc   static InitializedEntity InitializeRelatedResult(ObjCMethodDecl *MD,
279f4a2713aSLionel Sambuc                                                    QualType Type) {
280f4a2713aSLionel Sambuc     InitializedEntity Result(EK_RelatedResult, SourceLocation(), Type);
281f4a2713aSLionel Sambuc     Result.MethodDecl = MD;
282f4a2713aSLionel Sambuc     return Result;
283f4a2713aSLionel Sambuc   }
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc 
286f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a base class subobject.
287f4a2713aSLionel Sambuc   static InitializedEntity InitializeBase(ASTContext &Context,
288f4a2713aSLionel Sambuc                                           const CXXBaseSpecifier *Base,
289f4a2713aSLionel Sambuc                                           bool IsInheritedVirtualBase);
290f4a2713aSLionel Sambuc 
291f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a delegated constructor.
InitializeDelegation(QualType Type)292f4a2713aSLionel Sambuc   static InitializedEntity InitializeDelegation(QualType Type) {
293f4a2713aSLionel Sambuc     return InitializedEntity(EK_Delegating, SourceLocation(), Type);
294f4a2713aSLionel Sambuc   }
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a member subobject.
297*0a6a1f1dSLionel Sambuc   static InitializedEntity
298*0a6a1f1dSLionel Sambuc   InitializeMember(FieldDecl *Member,
299*0a6a1f1dSLionel Sambuc                    const InitializedEntity *Parent = nullptr) {
300f4a2713aSLionel Sambuc     return InitializedEntity(Member, Parent);
301f4a2713aSLionel Sambuc   }
302f4a2713aSLionel Sambuc 
303f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a member subobject.
304*0a6a1f1dSLionel Sambuc   static InitializedEntity
305*0a6a1f1dSLionel Sambuc   InitializeMember(IndirectFieldDecl *Member,
306*0a6a1f1dSLionel Sambuc                    const InitializedEntity *Parent = nullptr) {
307f4a2713aSLionel Sambuc     return InitializedEntity(Member->getAnonField(), Parent);
308f4a2713aSLionel Sambuc   }
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for an array element.
InitializeElement(ASTContext & Context,unsigned Index,const InitializedEntity & Parent)311f4a2713aSLionel Sambuc   static InitializedEntity InitializeElement(ASTContext &Context,
312f4a2713aSLionel Sambuc                                              unsigned Index,
313f4a2713aSLionel Sambuc                                              const InitializedEntity &Parent) {
314f4a2713aSLionel Sambuc     return InitializedEntity(Context, Index, Parent);
315f4a2713aSLionel Sambuc   }
316f4a2713aSLionel Sambuc 
317f4a2713aSLionel Sambuc   /// \brief Create the initialization entity for a lambda capture.
InitializeLambdaCapture(IdentifierInfo * VarID,QualType FieldType,SourceLocation Loc)318*0a6a1f1dSLionel Sambuc   static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID,
319*0a6a1f1dSLionel Sambuc                                                    QualType FieldType,
320f4a2713aSLionel Sambuc                                                    SourceLocation Loc) {
321*0a6a1f1dSLionel Sambuc     return InitializedEntity(VarID, FieldType, Loc);
322f4a2713aSLionel Sambuc   }
323f4a2713aSLionel Sambuc 
324f4a2713aSLionel Sambuc   /// \brief Create the entity for a compound literal initializer.
InitializeCompoundLiteralInit(TypeSourceInfo * TSI)325f4a2713aSLionel Sambuc   static InitializedEntity InitializeCompoundLiteralInit(TypeSourceInfo *TSI) {
326f4a2713aSLionel Sambuc     InitializedEntity Result(EK_CompoundLiteralInit, SourceLocation(),
327f4a2713aSLionel Sambuc                              TSI->getType());
328f4a2713aSLionel Sambuc     Result.TypeInfo = TSI;
329f4a2713aSLionel Sambuc     return Result;
330f4a2713aSLionel Sambuc   }
331f4a2713aSLionel Sambuc 
332f4a2713aSLionel Sambuc 
333f4a2713aSLionel Sambuc   /// \brief Determine the kind of initialization.
getKind()334f4a2713aSLionel Sambuc   EntityKind getKind() const { return Kind; }
335f4a2713aSLionel Sambuc 
336f4a2713aSLionel Sambuc   /// \brief Retrieve the parent of the entity being initialized, when
337f4a2713aSLionel Sambuc   /// the initialization itself is occurring within the context of a
338f4a2713aSLionel Sambuc   /// larger initialization.
getParent()339f4a2713aSLionel Sambuc   const InitializedEntity *getParent() const { return Parent; }
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc   /// \brief Retrieve type being initialized.
getType()342f4a2713aSLionel Sambuc   QualType getType() const { return Type; }
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc   /// \brief Retrieve complete type-source information for the object being
345f4a2713aSLionel Sambuc   /// constructed, if known.
getTypeSourceInfo()346f4a2713aSLionel Sambuc   TypeSourceInfo *getTypeSourceInfo() const {
347f4a2713aSLionel Sambuc     if (Kind == EK_Temporary || Kind == EK_CompoundLiteralInit)
348f4a2713aSLionel Sambuc       return TypeInfo;
349f4a2713aSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc     return nullptr;
351f4a2713aSLionel Sambuc   }
352f4a2713aSLionel Sambuc 
353f4a2713aSLionel Sambuc   /// \brief Retrieve the name of the entity being initialized.
354f4a2713aSLionel Sambuc   DeclarationName getName() const;
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc   /// \brief Retrieve the variable, parameter, or field being
357f4a2713aSLionel Sambuc   /// initialized.
358f4a2713aSLionel Sambuc   DeclaratorDecl *getDecl() const;
359f4a2713aSLionel Sambuc 
360f4a2713aSLionel Sambuc   /// \brief Retrieve the ObjectiveC method being initialized.
getMethodDecl()361f4a2713aSLionel Sambuc   ObjCMethodDecl *getMethodDecl() const { return MethodDecl; }
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization allows the named return
364f4a2713aSLionel Sambuc   /// value optimization, which also applies to thrown objects.
365f4a2713aSLionel Sambuc   bool allowsNRVO() const;
366f4a2713aSLionel Sambuc 
isParameterKind()367f4a2713aSLionel Sambuc   bool isParameterKind() const {
368f4a2713aSLionel Sambuc     return (getKind() == EK_Parameter  ||
369f4a2713aSLionel Sambuc             getKind() == EK_Parameter_CF_Audited);
370f4a2713aSLionel Sambuc   }
371f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization consumes the
372f4a2713aSLionel Sambuc   /// parameter.
isParameterConsumed()373f4a2713aSLionel Sambuc   bool isParameterConsumed() const {
374f4a2713aSLionel Sambuc     assert(isParameterKind() && "Not a parameter");
375f4a2713aSLionel Sambuc     return (Parameter & 1);
376f4a2713aSLionel Sambuc   }
377f4a2713aSLionel Sambuc 
378f4a2713aSLionel Sambuc   /// \brief Retrieve the base specifier.
getBaseSpecifier()379f4a2713aSLionel Sambuc   const CXXBaseSpecifier *getBaseSpecifier() const {
380f4a2713aSLionel Sambuc     assert(getKind() == EK_Base && "Not a base specifier");
381f4a2713aSLionel Sambuc     return reinterpret_cast<const CXXBaseSpecifier *>(Base & ~0x1);
382f4a2713aSLionel Sambuc   }
383f4a2713aSLionel Sambuc 
384f4a2713aSLionel Sambuc   /// \brief Return whether the base is an inherited virtual base.
isInheritedVirtualBase()385f4a2713aSLionel Sambuc   bool isInheritedVirtualBase() const {
386f4a2713aSLionel Sambuc     assert(getKind() == EK_Base && "Not a base specifier");
387f4a2713aSLionel Sambuc     return Base & 0x1;
388f4a2713aSLionel Sambuc   }
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   /// \brief Determine the location of the 'return' keyword when initializing
391f4a2713aSLionel Sambuc   /// the result of a function call.
getReturnLoc()392f4a2713aSLionel Sambuc   SourceLocation getReturnLoc() const {
393f4a2713aSLionel Sambuc     assert(getKind() == EK_Result && "No 'return' location!");
394f4a2713aSLionel Sambuc     return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
395f4a2713aSLionel Sambuc   }
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc   /// \brief Determine the location of the 'throw' keyword when initializing
398f4a2713aSLionel Sambuc   /// an exception object.
getThrowLoc()399f4a2713aSLionel Sambuc   SourceLocation getThrowLoc() const {
400f4a2713aSLionel Sambuc     assert(getKind() == EK_Exception && "No 'throw' location!");
401f4a2713aSLionel Sambuc     return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
402f4a2713aSLionel Sambuc   }
403f4a2713aSLionel Sambuc 
404*0a6a1f1dSLionel Sambuc   /// \brief If this is an array, vector, or complex number element, get the
405*0a6a1f1dSLionel Sambuc   /// element's index.
getElementIndex()406*0a6a1f1dSLionel Sambuc   unsigned getElementIndex() const {
407*0a6a1f1dSLionel Sambuc     assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
408*0a6a1f1dSLionel Sambuc            getKind() == EK_ComplexElement);
409*0a6a1f1dSLionel Sambuc     return Index;
410*0a6a1f1dSLionel Sambuc   }
411f4a2713aSLionel Sambuc   /// \brief If this is already the initializer for an array or vector
412f4a2713aSLionel Sambuc   /// element, sets the element index.
setElementIndex(unsigned Index)413f4a2713aSLionel Sambuc   void setElementIndex(unsigned Index) {
414f4a2713aSLionel Sambuc     assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
415f4a2713aSLionel Sambuc            getKind() == EK_ComplexElement);
416f4a2713aSLionel Sambuc     this->Index = Index;
417f4a2713aSLionel Sambuc   }
418*0a6a1f1dSLionel Sambuc   /// \brief For a lambda capture, return the capture's name.
getCapturedVarName()419*0a6a1f1dSLionel Sambuc   StringRef getCapturedVarName() const {
420f4a2713aSLionel Sambuc     assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
421*0a6a1f1dSLionel Sambuc     return Capture.VarID->getName();
422f4a2713aSLionel Sambuc   }
423f4a2713aSLionel Sambuc   /// \brief Determine the location of the capture when initializing
424f4a2713aSLionel Sambuc   /// field from a captured variable in a lambda.
getCaptureLoc()425f4a2713aSLionel Sambuc   SourceLocation getCaptureLoc() const {
426f4a2713aSLionel Sambuc     assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
427f4a2713aSLionel Sambuc     return SourceLocation::getFromRawEncoding(Capture.Location);
428f4a2713aSLionel Sambuc   }
429f4a2713aSLionel Sambuc 
setParameterCFAudited()430f4a2713aSLionel Sambuc   void setParameterCFAudited() {
431f4a2713aSLionel Sambuc     Kind = EK_Parameter_CF_Audited;
432f4a2713aSLionel Sambuc   }
433f4a2713aSLionel Sambuc 
allocateManglingNumber()434*0a6a1f1dSLionel Sambuc   unsigned allocateManglingNumber() const { return ++ManglingNumber; }
435*0a6a1f1dSLionel Sambuc 
436f4a2713aSLionel Sambuc   /// Dump a representation of the initialized entity to standard error,
437f4a2713aSLionel Sambuc   /// for debugging purposes.
438f4a2713aSLionel Sambuc   void dump() const;
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc private:
441f4a2713aSLionel Sambuc   unsigned dumpImpl(raw_ostream &OS) const;
442f4a2713aSLionel Sambuc };
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc /// \brief Describes the kind of initialization being performed, along with
445f4a2713aSLionel Sambuc /// location information for tokens related to the initialization (equal sign,
446f4a2713aSLionel Sambuc /// parentheses).
447f4a2713aSLionel Sambuc class InitializationKind {
448f4a2713aSLionel Sambuc public:
449f4a2713aSLionel Sambuc   /// \brief The kind of initialization being performed.
450f4a2713aSLionel Sambuc   enum InitKind {
451f4a2713aSLionel Sambuc     IK_Direct,       ///< Direct initialization
452f4a2713aSLionel Sambuc     IK_DirectList,   ///< Direct list-initialization
453f4a2713aSLionel Sambuc     IK_Copy,         ///< Copy initialization
454f4a2713aSLionel Sambuc     IK_Default,      ///< Default initialization
455f4a2713aSLionel Sambuc     IK_Value         ///< Value initialization
456f4a2713aSLionel Sambuc   };
457f4a2713aSLionel Sambuc 
458f4a2713aSLionel Sambuc private:
459f4a2713aSLionel Sambuc   /// \brief The context of the initialization.
460f4a2713aSLionel Sambuc   enum InitContext {
461f4a2713aSLionel Sambuc     IC_Normal,         ///< Normal context
462f4a2713aSLionel Sambuc     IC_ExplicitConvs,  ///< Normal context, but allows explicit conversion funcs
463f4a2713aSLionel Sambuc     IC_Implicit,       ///< Implicit context (value initialization)
464f4a2713aSLionel Sambuc     IC_StaticCast,     ///< Static cast context
465f4a2713aSLionel Sambuc     IC_CStyleCast,     ///< C-style cast context
466f4a2713aSLionel Sambuc     IC_FunctionalCast  ///< Functional cast context
467f4a2713aSLionel Sambuc   };
468f4a2713aSLionel Sambuc 
469f4a2713aSLionel Sambuc   /// \brief The kind of initialization being performed.
470f4a2713aSLionel Sambuc   InitKind Kind : 8;
471f4a2713aSLionel Sambuc 
472f4a2713aSLionel Sambuc   /// \brief The context of the initialization.
473f4a2713aSLionel Sambuc   InitContext Context : 8;
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc   /// \brief The source locations involved in the initialization.
476f4a2713aSLionel Sambuc   SourceLocation Locations[3];
477f4a2713aSLionel Sambuc 
InitializationKind(InitKind Kind,InitContext Context,SourceLocation Loc1,SourceLocation Loc2,SourceLocation Loc3)478f4a2713aSLionel Sambuc   InitializationKind(InitKind Kind, InitContext Context, SourceLocation Loc1,
479f4a2713aSLionel Sambuc                      SourceLocation Loc2, SourceLocation Loc3)
480f4a2713aSLionel Sambuc     : Kind(Kind), Context(Context)
481f4a2713aSLionel Sambuc   {
482f4a2713aSLionel Sambuc     Locations[0] = Loc1;
483f4a2713aSLionel Sambuc     Locations[1] = Loc2;
484f4a2713aSLionel Sambuc     Locations[2] = Loc3;
485f4a2713aSLionel Sambuc   }
486f4a2713aSLionel Sambuc 
487f4a2713aSLionel Sambuc public:
488f4a2713aSLionel Sambuc   /// \brief Create a direct initialization.
CreateDirect(SourceLocation InitLoc,SourceLocation LParenLoc,SourceLocation RParenLoc)489f4a2713aSLionel Sambuc   static InitializationKind CreateDirect(SourceLocation InitLoc,
490f4a2713aSLionel Sambuc                                          SourceLocation LParenLoc,
491f4a2713aSLionel Sambuc                                          SourceLocation RParenLoc) {
492f4a2713aSLionel Sambuc     return InitializationKind(IK_Direct, IC_Normal,
493f4a2713aSLionel Sambuc                               InitLoc, LParenLoc, RParenLoc);
494f4a2713aSLionel Sambuc   }
495f4a2713aSLionel Sambuc 
CreateDirectList(SourceLocation InitLoc)496f4a2713aSLionel Sambuc   static InitializationKind CreateDirectList(SourceLocation InitLoc) {
497f4a2713aSLionel Sambuc     return InitializationKind(IK_DirectList, IC_Normal,
498f4a2713aSLionel Sambuc                               InitLoc, InitLoc, InitLoc);
499f4a2713aSLionel Sambuc   }
500f4a2713aSLionel Sambuc 
501f4a2713aSLionel Sambuc   /// \brief Create a direct initialization due to a cast that isn't a C-style
502f4a2713aSLionel Sambuc   /// or functional cast.
CreateCast(SourceRange TypeRange)503f4a2713aSLionel Sambuc   static InitializationKind CreateCast(SourceRange TypeRange) {
504f4a2713aSLionel Sambuc     return InitializationKind(IK_Direct, IC_StaticCast, TypeRange.getBegin(),
505f4a2713aSLionel Sambuc                               TypeRange.getBegin(), TypeRange.getEnd());
506f4a2713aSLionel Sambuc   }
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   /// \brief Create a direct initialization for a C-style cast.
CreateCStyleCast(SourceLocation StartLoc,SourceRange TypeRange,bool InitList)509f4a2713aSLionel Sambuc   static InitializationKind CreateCStyleCast(SourceLocation StartLoc,
510f4a2713aSLionel Sambuc                                              SourceRange TypeRange,
511f4a2713aSLionel Sambuc                                              bool InitList) {
512f4a2713aSLionel Sambuc     // C++ cast syntax doesn't permit init lists, but C compound literals are
513f4a2713aSLionel Sambuc     // exactly that.
514f4a2713aSLionel Sambuc     return InitializationKind(InitList ? IK_DirectList : IK_Direct,
515f4a2713aSLionel Sambuc                               IC_CStyleCast, StartLoc, TypeRange.getBegin(),
516f4a2713aSLionel Sambuc                               TypeRange.getEnd());
517f4a2713aSLionel Sambuc   }
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc   /// \brief Create a direct initialization for a functional cast.
CreateFunctionalCast(SourceRange TypeRange,bool InitList)520f4a2713aSLionel Sambuc   static InitializationKind CreateFunctionalCast(SourceRange TypeRange,
521f4a2713aSLionel Sambuc                                                  bool InitList) {
522f4a2713aSLionel Sambuc     return InitializationKind(InitList ? IK_DirectList : IK_Direct,
523f4a2713aSLionel Sambuc                               IC_FunctionalCast, TypeRange.getBegin(),
524f4a2713aSLionel Sambuc                               TypeRange.getBegin(), TypeRange.getEnd());
525f4a2713aSLionel Sambuc   }
526f4a2713aSLionel Sambuc 
527f4a2713aSLionel Sambuc   /// \brief Create a copy initialization.
528f4a2713aSLionel Sambuc   static InitializationKind CreateCopy(SourceLocation InitLoc,
529f4a2713aSLionel Sambuc                                        SourceLocation EqualLoc,
530f4a2713aSLionel Sambuc                                        bool AllowExplicitConvs = false) {
531f4a2713aSLionel Sambuc     return InitializationKind(IK_Copy,
532f4a2713aSLionel Sambuc                               AllowExplicitConvs? IC_ExplicitConvs : IC_Normal,
533f4a2713aSLionel Sambuc                               InitLoc, EqualLoc, EqualLoc);
534f4a2713aSLionel Sambuc   }
535f4a2713aSLionel Sambuc 
536f4a2713aSLionel Sambuc   /// \brief Create a default initialization.
CreateDefault(SourceLocation InitLoc)537f4a2713aSLionel Sambuc   static InitializationKind CreateDefault(SourceLocation InitLoc) {
538f4a2713aSLionel Sambuc     return InitializationKind(IK_Default, IC_Normal, InitLoc, InitLoc, InitLoc);
539f4a2713aSLionel Sambuc   }
540f4a2713aSLionel Sambuc 
541f4a2713aSLionel Sambuc   /// \brief Create a value initialization.
542f4a2713aSLionel Sambuc   static InitializationKind CreateValue(SourceLocation InitLoc,
543f4a2713aSLionel Sambuc                                         SourceLocation LParenLoc,
544f4a2713aSLionel Sambuc                                         SourceLocation RParenLoc,
545f4a2713aSLionel Sambuc                                         bool isImplicit = false) {
546f4a2713aSLionel Sambuc     return InitializationKind(IK_Value, isImplicit ? IC_Implicit : IC_Normal,
547f4a2713aSLionel Sambuc                               InitLoc, LParenLoc, RParenLoc);
548f4a2713aSLionel Sambuc   }
549f4a2713aSLionel Sambuc 
550f4a2713aSLionel Sambuc   /// \brief Determine the initialization kind.
getKind()551f4a2713aSLionel Sambuc   InitKind getKind() const {
552f4a2713aSLionel Sambuc     return Kind;
553f4a2713aSLionel Sambuc   }
554f4a2713aSLionel Sambuc 
555f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization is an explicit cast.
isExplicitCast()556f4a2713aSLionel Sambuc   bool isExplicitCast() const {
557f4a2713aSLionel Sambuc     return Context >= IC_StaticCast;
558f4a2713aSLionel Sambuc   }
559f4a2713aSLionel Sambuc 
560f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization is a C-style cast.
isCStyleOrFunctionalCast()561f4a2713aSLionel Sambuc   bool isCStyleOrFunctionalCast() const {
562f4a2713aSLionel Sambuc     return Context >= IC_CStyleCast;
563f4a2713aSLionel Sambuc   }
564f4a2713aSLionel Sambuc 
565f4a2713aSLionel Sambuc   /// \brief Determine whether this is a C-style cast.
isCStyleCast()566f4a2713aSLionel Sambuc   bool isCStyleCast() const {
567f4a2713aSLionel Sambuc     return Context == IC_CStyleCast;
568f4a2713aSLionel Sambuc   }
569f4a2713aSLionel Sambuc 
570f4a2713aSLionel Sambuc   /// \brief Determine whether this is a functional-style cast.
isFunctionalCast()571f4a2713aSLionel Sambuc   bool isFunctionalCast() const {
572f4a2713aSLionel Sambuc     return Context == IC_FunctionalCast;
573f4a2713aSLionel Sambuc   }
574f4a2713aSLionel Sambuc 
575f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization is an implicit
576f4a2713aSLionel Sambuc   /// value-initialization, e.g., as occurs during aggregate
577f4a2713aSLionel Sambuc   /// initialization.
isImplicitValueInit()578f4a2713aSLionel Sambuc   bool isImplicitValueInit() const { return Context == IC_Implicit; }
579f4a2713aSLionel Sambuc 
580f4a2713aSLionel Sambuc   /// \brief Retrieve the location at which initialization is occurring.
getLocation()581f4a2713aSLionel Sambuc   SourceLocation getLocation() const { return Locations[0]; }
582f4a2713aSLionel Sambuc 
583f4a2713aSLionel Sambuc   /// \brief Retrieve the source range that covers the initialization.
getRange()584f4a2713aSLionel Sambuc   SourceRange getRange() const {
585f4a2713aSLionel Sambuc     return SourceRange(Locations[0], Locations[2]);
586f4a2713aSLionel Sambuc   }
587f4a2713aSLionel Sambuc 
588f4a2713aSLionel Sambuc   /// \brief Retrieve the location of the equal sign for copy initialization
589f4a2713aSLionel Sambuc   /// (if present).
getEqualLoc()590f4a2713aSLionel Sambuc   SourceLocation getEqualLoc() const {
591f4a2713aSLionel Sambuc     assert(Kind == IK_Copy && "Only copy initialization has an '='");
592f4a2713aSLionel Sambuc     return Locations[1];
593f4a2713aSLionel Sambuc   }
594f4a2713aSLionel Sambuc 
isCopyInit()595f4a2713aSLionel Sambuc   bool isCopyInit() const { return Kind == IK_Copy; }
596f4a2713aSLionel Sambuc 
597f4a2713aSLionel Sambuc   /// \brief Retrieve whether this initialization allows the use of explicit
598f4a2713aSLionel Sambuc   ///        constructors.
AllowExplicit()599f4a2713aSLionel Sambuc   bool AllowExplicit() const { return !isCopyInit(); }
600f4a2713aSLionel Sambuc 
601f4a2713aSLionel Sambuc   /// \brief Retrieve whether this initialization allows the use of explicit
602f4a2713aSLionel Sambuc   /// conversion functions when binding a reference. If the reference is the
603f4a2713aSLionel Sambuc   /// first parameter in a copy or move constructor, such conversions are
604f4a2713aSLionel Sambuc   /// permitted even though we are performing copy-initialization.
allowExplicitConversionFunctionsInRefBinding()605f4a2713aSLionel Sambuc   bool allowExplicitConversionFunctionsInRefBinding() const {
606f4a2713aSLionel Sambuc     return !isCopyInit() || Context == IC_ExplicitConvs;
607f4a2713aSLionel Sambuc   }
608f4a2713aSLionel Sambuc 
609f4a2713aSLionel Sambuc   /// \brief Retrieve the source range containing the locations of the open
610f4a2713aSLionel Sambuc   /// and closing parentheses for value and direct initializations.
getParenRange()611f4a2713aSLionel Sambuc   SourceRange getParenRange() const {
612f4a2713aSLionel Sambuc     assert((Kind == IK_Direct || Kind == IK_Value) &&
613f4a2713aSLionel Sambuc            "Only direct- and value-initialization have parentheses");
614f4a2713aSLionel Sambuc     return SourceRange(Locations[1], Locations[2]);
615f4a2713aSLionel Sambuc   }
616f4a2713aSLionel Sambuc };
617f4a2713aSLionel Sambuc 
618f4a2713aSLionel Sambuc /// \brief Describes the sequence of initializations required to initialize
619f4a2713aSLionel Sambuc /// a given object or reference with a set of arguments.
620f4a2713aSLionel Sambuc class InitializationSequence {
621f4a2713aSLionel Sambuc public:
622f4a2713aSLionel Sambuc   /// \brief Describes the kind of initialization sequence computed.
623f4a2713aSLionel Sambuc   enum SequenceKind {
624f4a2713aSLionel Sambuc     /// \brief A failed initialization sequence. The failure kind tells what
625f4a2713aSLionel Sambuc     /// happened.
626f4a2713aSLionel Sambuc     FailedSequence = 0,
627f4a2713aSLionel Sambuc 
628f4a2713aSLionel Sambuc     /// \brief A dependent initialization, which could not be
629f4a2713aSLionel Sambuc     /// type-checked due to the presence of dependent types or
630f4a2713aSLionel Sambuc     /// dependently-typed expressions.
631f4a2713aSLionel Sambuc     DependentSequence,
632f4a2713aSLionel Sambuc 
633f4a2713aSLionel Sambuc     /// \brief A normal sequence.
634f4a2713aSLionel Sambuc     NormalSequence
635f4a2713aSLionel Sambuc   };
636f4a2713aSLionel Sambuc 
637f4a2713aSLionel Sambuc   /// \brief Describes the kind of a particular step in an initialization
638f4a2713aSLionel Sambuc   /// sequence.
639f4a2713aSLionel Sambuc   enum StepKind {
640f4a2713aSLionel Sambuc     /// \brief Resolve the address of an overloaded function to a specific
641f4a2713aSLionel Sambuc     /// function declaration.
642f4a2713aSLionel Sambuc     SK_ResolveAddressOfOverloadedFunction,
643f4a2713aSLionel Sambuc     /// \brief Perform a derived-to-base cast, producing an rvalue.
644f4a2713aSLionel Sambuc     SK_CastDerivedToBaseRValue,
645f4a2713aSLionel Sambuc     /// \brief Perform a derived-to-base cast, producing an xvalue.
646f4a2713aSLionel Sambuc     SK_CastDerivedToBaseXValue,
647f4a2713aSLionel Sambuc     /// \brief Perform a derived-to-base cast, producing an lvalue.
648f4a2713aSLionel Sambuc     SK_CastDerivedToBaseLValue,
649f4a2713aSLionel Sambuc     /// \brief Reference binding to an lvalue.
650f4a2713aSLionel Sambuc     SK_BindReference,
651f4a2713aSLionel Sambuc     /// \brief Reference binding to a temporary.
652f4a2713aSLionel Sambuc     SK_BindReferenceToTemporary,
653f4a2713aSLionel Sambuc     /// \brief An optional copy of a temporary object to another
654f4a2713aSLionel Sambuc     /// temporary object, which is permitted (but not required) by
655f4a2713aSLionel Sambuc     /// C++98/03 but not C++0x.
656f4a2713aSLionel Sambuc     SK_ExtraneousCopyToTemporary,
657f4a2713aSLionel Sambuc     /// \brief Perform a user-defined conversion, either via a conversion
658f4a2713aSLionel Sambuc     /// function or via a constructor.
659f4a2713aSLionel Sambuc     SK_UserConversion,
660f4a2713aSLionel Sambuc     /// \brief Perform a qualification conversion, producing an rvalue.
661f4a2713aSLionel Sambuc     SK_QualificationConversionRValue,
662f4a2713aSLionel Sambuc     /// \brief Perform a qualification conversion, producing an xvalue.
663f4a2713aSLionel Sambuc     SK_QualificationConversionXValue,
664f4a2713aSLionel Sambuc     /// \brief Perform a qualification conversion, producing an lvalue.
665f4a2713aSLionel Sambuc     SK_QualificationConversionLValue,
666*0a6a1f1dSLionel Sambuc     /// \brief Perform a conversion adding _Atomic to a type.
667*0a6a1f1dSLionel Sambuc     SK_AtomicConversion,
668f4a2713aSLionel Sambuc     /// \brief Perform a load from a glvalue, producing an rvalue.
669f4a2713aSLionel Sambuc     SK_LValueToRValue,
670f4a2713aSLionel Sambuc     /// \brief Perform an implicit conversion sequence.
671f4a2713aSLionel Sambuc     SK_ConversionSequence,
672f4a2713aSLionel Sambuc     /// \brief Perform an implicit conversion sequence without narrowing.
673f4a2713aSLionel Sambuc     SK_ConversionSequenceNoNarrowing,
674*0a6a1f1dSLionel Sambuc     /// \brief Perform list-initialization without a constructor.
675f4a2713aSLionel Sambuc     SK_ListInitialization,
676f4a2713aSLionel Sambuc     /// \brief Unwrap the single-element initializer list for a reference.
677f4a2713aSLionel Sambuc     SK_UnwrapInitList,
678f4a2713aSLionel Sambuc     /// \brief Rewrap the single-element initializer list for a reference.
679f4a2713aSLionel Sambuc     SK_RewrapInitList,
680f4a2713aSLionel Sambuc     /// \brief Perform initialization via a constructor.
681f4a2713aSLionel Sambuc     SK_ConstructorInitialization,
682*0a6a1f1dSLionel Sambuc     /// \brief Perform initialization via a constructor, taking arguments from
683*0a6a1f1dSLionel Sambuc     /// a single InitListExpr.
684*0a6a1f1dSLionel Sambuc     SK_ConstructorInitializationFromList,
685f4a2713aSLionel Sambuc     /// \brief Zero-initialize the object
686f4a2713aSLionel Sambuc     SK_ZeroInitialization,
687f4a2713aSLionel Sambuc     /// \brief C assignment
688f4a2713aSLionel Sambuc     SK_CAssignment,
689f4a2713aSLionel Sambuc     /// \brief Initialization by string
690f4a2713aSLionel Sambuc     SK_StringInit,
691f4a2713aSLionel Sambuc     /// \brief An initialization that "converts" an Objective-C object
692f4a2713aSLionel Sambuc     /// (not a point to an object) to another Objective-C object type.
693f4a2713aSLionel Sambuc     SK_ObjCObjectConversion,
694f4a2713aSLionel Sambuc     /// \brief Array initialization (from an array rvalue).
695f4a2713aSLionel Sambuc     /// This is a GNU C extension.
696f4a2713aSLionel Sambuc     SK_ArrayInit,
697f4a2713aSLionel Sambuc     /// \brief Array initialization from a parenthesized initializer list.
698f4a2713aSLionel Sambuc     /// This is a GNU C++ extension.
699f4a2713aSLionel Sambuc     SK_ParenthesizedArrayInit,
700f4a2713aSLionel Sambuc     /// \brief Pass an object by indirect copy-and-restore.
701f4a2713aSLionel Sambuc     SK_PassByIndirectCopyRestore,
702f4a2713aSLionel Sambuc     /// \brief Pass an object by indirect restore.
703f4a2713aSLionel Sambuc     SK_PassByIndirectRestore,
704f4a2713aSLionel Sambuc     /// \brief Produce an Objective-C object pointer.
705f4a2713aSLionel Sambuc     SK_ProduceObjCObject,
706f4a2713aSLionel Sambuc     /// \brief Construct a std::initializer_list from an initializer list.
707f4a2713aSLionel Sambuc     SK_StdInitializerList,
708*0a6a1f1dSLionel Sambuc     /// \brief Perform initialization via a constructor taking a single
709*0a6a1f1dSLionel Sambuc     /// std::initializer_list argument.
710*0a6a1f1dSLionel Sambuc     SK_StdInitializerListConstructorCall,
711f4a2713aSLionel Sambuc     /// \brief Initialize an OpenCL sampler from an integer.
712f4a2713aSLionel Sambuc     SK_OCLSamplerInit,
713f4a2713aSLionel Sambuc     /// \brief Passing zero to a function where OpenCL event_t is expected.
714f4a2713aSLionel Sambuc     SK_OCLZeroEvent
715f4a2713aSLionel Sambuc   };
716f4a2713aSLionel Sambuc 
717f4a2713aSLionel Sambuc   /// \brief A single step in the initialization sequence.
718f4a2713aSLionel Sambuc   class Step {
719f4a2713aSLionel Sambuc   public:
720f4a2713aSLionel Sambuc     /// \brief The kind of conversion or initialization step we are taking.
721f4a2713aSLionel Sambuc     StepKind Kind;
722f4a2713aSLionel Sambuc 
723f4a2713aSLionel Sambuc     // \brief The type that results from this initialization.
724f4a2713aSLionel Sambuc     QualType Type;
725f4a2713aSLionel Sambuc 
726f4a2713aSLionel Sambuc     struct F {
727f4a2713aSLionel Sambuc       bool HadMultipleCandidates;
728f4a2713aSLionel Sambuc       FunctionDecl *Function;
729f4a2713aSLionel Sambuc       DeclAccessPair FoundDecl;
730f4a2713aSLionel Sambuc     };
731f4a2713aSLionel Sambuc 
732f4a2713aSLionel Sambuc     union {
733f4a2713aSLionel Sambuc       /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
734f4a2713aSLionel Sambuc       /// SK_UserConversion, the function that the expression should be
735f4a2713aSLionel Sambuc       /// resolved to or the conversion function to call, respectively.
736f4a2713aSLionel Sambuc       /// When Kind == SK_ConstructorInitialization or SK_ListConstruction,
737f4a2713aSLionel Sambuc       /// the constructor to be called.
738f4a2713aSLionel Sambuc       ///
739f4a2713aSLionel Sambuc       /// Always a FunctionDecl, plus a Boolean flag telling if it was
740f4a2713aSLionel Sambuc       /// selected from an overloaded set having size greater than 1.
741f4a2713aSLionel Sambuc       /// For conversion decls, the naming class is the source type.
742f4a2713aSLionel Sambuc       /// For construct decls, the naming class is the target type.
743f4a2713aSLionel Sambuc       struct F Function;
744f4a2713aSLionel Sambuc 
745f4a2713aSLionel Sambuc       /// \brief When Kind = SK_ConversionSequence, the implicit conversion
746f4a2713aSLionel Sambuc       /// sequence.
747f4a2713aSLionel Sambuc       ImplicitConversionSequence *ICS;
748f4a2713aSLionel Sambuc 
749f4a2713aSLionel Sambuc       /// \brief When Kind = SK_RewrapInitList, the syntactic form of the
750f4a2713aSLionel Sambuc       /// wrapping list.
751f4a2713aSLionel Sambuc       InitListExpr *WrappingSyntacticList;
752f4a2713aSLionel Sambuc     };
753f4a2713aSLionel Sambuc 
754f4a2713aSLionel Sambuc     void Destroy();
755f4a2713aSLionel Sambuc   };
756f4a2713aSLionel Sambuc 
757f4a2713aSLionel Sambuc private:
758f4a2713aSLionel Sambuc   /// \brief The kind of initialization sequence computed.
759f4a2713aSLionel Sambuc   enum SequenceKind SequenceKind;
760f4a2713aSLionel Sambuc 
761f4a2713aSLionel Sambuc   /// \brief Steps taken by this initialization.
762f4a2713aSLionel Sambuc   SmallVector<Step, 4> Steps;
763f4a2713aSLionel Sambuc 
764f4a2713aSLionel Sambuc public:
765f4a2713aSLionel Sambuc   /// \brief Describes why initialization failed.
766f4a2713aSLionel Sambuc   enum FailureKind {
767f4a2713aSLionel Sambuc     /// \brief Too many initializers provided for a reference.
768f4a2713aSLionel Sambuc     FK_TooManyInitsForReference,
769f4a2713aSLionel Sambuc     /// \brief Array must be initialized with an initializer list.
770f4a2713aSLionel Sambuc     FK_ArrayNeedsInitList,
771f4a2713aSLionel Sambuc     /// \brief Array must be initialized with an initializer list or a
772f4a2713aSLionel Sambuc     /// string literal.
773f4a2713aSLionel Sambuc     FK_ArrayNeedsInitListOrStringLiteral,
774f4a2713aSLionel Sambuc     /// \brief Array must be initialized with an initializer list or a
775f4a2713aSLionel Sambuc     /// wide string literal.
776f4a2713aSLionel Sambuc     FK_ArrayNeedsInitListOrWideStringLiteral,
777f4a2713aSLionel Sambuc     /// \brief Initializing a wide char array with narrow string literal.
778f4a2713aSLionel Sambuc     FK_NarrowStringIntoWideCharArray,
779f4a2713aSLionel Sambuc     /// \brief Initializing char array with wide string literal.
780f4a2713aSLionel Sambuc     FK_WideStringIntoCharArray,
781f4a2713aSLionel Sambuc     /// \brief Initializing wide char array with incompatible wide string
782f4a2713aSLionel Sambuc     /// literal.
783f4a2713aSLionel Sambuc     FK_IncompatWideStringIntoWideChar,
784f4a2713aSLionel Sambuc     /// \brief Array type mismatch.
785f4a2713aSLionel Sambuc     FK_ArrayTypeMismatch,
786f4a2713aSLionel Sambuc     /// \brief Non-constant array initializer
787f4a2713aSLionel Sambuc     FK_NonConstantArrayInit,
788f4a2713aSLionel Sambuc     /// \brief Cannot resolve the address of an overloaded function.
789f4a2713aSLionel Sambuc     FK_AddressOfOverloadFailed,
790f4a2713aSLionel Sambuc     /// \brief Overloading due to reference initialization failed.
791f4a2713aSLionel Sambuc     FK_ReferenceInitOverloadFailed,
792f4a2713aSLionel Sambuc     /// \brief Non-const lvalue reference binding to a temporary.
793f4a2713aSLionel Sambuc     FK_NonConstLValueReferenceBindingToTemporary,
794f4a2713aSLionel Sambuc     /// \brief Non-const lvalue reference binding to an lvalue of unrelated
795f4a2713aSLionel Sambuc     /// type.
796f4a2713aSLionel Sambuc     FK_NonConstLValueReferenceBindingToUnrelated,
797f4a2713aSLionel Sambuc     /// \brief Rvalue reference binding to an lvalue.
798f4a2713aSLionel Sambuc     FK_RValueReferenceBindingToLValue,
799f4a2713aSLionel Sambuc     /// \brief Reference binding drops qualifiers.
800f4a2713aSLionel Sambuc     FK_ReferenceInitDropsQualifiers,
801f4a2713aSLionel Sambuc     /// \brief Reference binding failed.
802f4a2713aSLionel Sambuc     FK_ReferenceInitFailed,
803f4a2713aSLionel Sambuc     /// \brief Implicit conversion failed.
804f4a2713aSLionel Sambuc     FK_ConversionFailed,
805f4a2713aSLionel Sambuc     /// \brief Implicit conversion failed.
806f4a2713aSLionel Sambuc     FK_ConversionFromPropertyFailed,
807f4a2713aSLionel Sambuc     /// \brief Too many initializers for scalar
808f4a2713aSLionel Sambuc     FK_TooManyInitsForScalar,
809f4a2713aSLionel Sambuc     /// \brief Reference initialization from an initializer list
810f4a2713aSLionel Sambuc     FK_ReferenceBindingToInitList,
811f4a2713aSLionel Sambuc     /// \brief Initialization of some unused destination type with an
812f4a2713aSLionel Sambuc     /// initializer list.
813f4a2713aSLionel Sambuc     FK_InitListBadDestinationType,
814f4a2713aSLionel Sambuc     /// \brief Overloading for a user-defined conversion failed.
815f4a2713aSLionel Sambuc     FK_UserConversionOverloadFailed,
816f4a2713aSLionel Sambuc     /// \brief Overloading for initialization by constructor failed.
817f4a2713aSLionel Sambuc     FK_ConstructorOverloadFailed,
818f4a2713aSLionel Sambuc     /// \brief Overloading for list-initialization by constructor failed.
819f4a2713aSLionel Sambuc     FK_ListConstructorOverloadFailed,
820f4a2713aSLionel Sambuc     /// \brief Default-initialization of a 'const' object.
821f4a2713aSLionel Sambuc     FK_DefaultInitOfConst,
822f4a2713aSLionel Sambuc     /// \brief Initialization of an incomplete type.
823f4a2713aSLionel Sambuc     FK_Incomplete,
824f4a2713aSLionel Sambuc     /// \brief Variable-length array must not have an initializer.
825f4a2713aSLionel Sambuc     FK_VariableLengthArrayHasInitializer,
826f4a2713aSLionel Sambuc     /// \brief List initialization failed at some point.
827f4a2713aSLionel Sambuc     FK_ListInitializationFailed,
828f4a2713aSLionel Sambuc     /// \brief Initializer has a placeholder type which cannot be
829f4a2713aSLionel Sambuc     /// resolved by initialization.
830f4a2713aSLionel Sambuc     FK_PlaceholderType,
831f4a2713aSLionel Sambuc     /// \brief List-copy-initialization chose an explicit constructor.
832f4a2713aSLionel Sambuc     FK_ExplicitConstructor
833f4a2713aSLionel Sambuc   };
834f4a2713aSLionel Sambuc 
835f4a2713aSLionel Sambuc private:
836f4a2713aSLionel Sambuc   /// \brief The reason why initialization failed.
837f4a2713aSLionel Sambuc   FailureKind Failure;
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc   /// \brief The failed result of overload resolution.
840f4a2713aSLionel Sambuc   OverloadingResult FailedOverloadResult;
841f4a2713aSLionel Sambuc 
842f4a2713aSLionel Sambuc   /// \brief The candidate set created when initialization failed.
843f4a2713aSLionel Sambuc   OverloadCandidateSet FailedCandidateSet;
844f4a2713aSLionel Sambuc 
845f4a2713aSLionel Sambuc   /// \brief The incomplete type that caused a failure.
846f4a2713aSLionel Sambuc   QualType FailedIncompleteType;
847f4a2713aSLionel Sambuc 
848f4a2713aSLionel Sambuc   /// \brief Prints a follow-up note that highlights the location of
849f4a2713aSLionel Sambuc   /// the initialized entity, if it's remote.
850f4a2713aSLionel Sambuc   void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
851f4a2713aSLionel Sambuc 
852f4a2713aSLionel Sambuc public:
853f4a2713aSLionel Sambuc   /// \brief Try to perform initialization of the given entity, creating a
854f4a2713aSLionel Sambuc   /// record of the steps required to perform the initialization.
855f4a2713aSLionel Sambuc   ///
856f4a2713aSLionel Sambuc   /// The generated initialization sequence will either contain enough
857f4a2713aSLionel Sambuc   /// information to diagnose
858f4a2713aSLionel Sambuc   ///
859f4a2713aSLionel Sambuc   /// \param S the semantic analysis object.
860f4a2713aSLionel Sambuc   ///
861f4a2713aSLionel Sambuc   /// \param Entity the entity being initialized.
862f4a2713aSLionel Sambuc   ///
863f4a2713aSLionel Sambuc   /// \param Kind the kind of initialization being performed.
864f4a2713aSLionel Sambuc   ///
865f4a2713aSLionel Sambuc   /// \param Args the argument(s) provided for initialization.
866f4a2713aSLionel Sambuc   ///
867*0a6a1f1dSLionel Sambuc   /// \param TopLevelOfInitList true if we are initializing from an expression
868*0a6a1f1dSLionel Sambuc   ///        at the top level inside an initializer list. This disallows
869*0a6a1f1dSLionel Sambuc   ///        narrowing conversions in C++11 onwards.
870f4a2713aSLionel Sambuc   InitializationSequence(Sema &S,
871f4a2713aSLionel Sambuc                          const InitializedEntity &Entity,
872f4a2713aSLionel Sambuc                          const InitializationKind &Kind,
873f4a2713aSLionel Sambuc                          MultiExprArg Args,
874*0a6a1f1dSLionel Sambuc                          bool TopLevelOfInitList = false);
875f4a2713aSLionel Sambuc   void InitializeFrom(Sema &S, const InitializedEntity &Entity,
876f4a2713aSLionel Sambuc                       const InitializationKind &Kind, MultiExprArg Args,
877*0a6a1f1dSLionel Sambuc                       bool TopLevelOfInitList);
878f4a2713aSLionel Sambuc 
879f4a2713aSLionel Sambuc   ~InitializationSequence();
880f4a2713aSLionel Sambuc 
881f4a2713aSLionel Sambuc   /// \brief Perform the actual initialization of the given entity based on
882f4a2713aSLionel Sambuc   /// the computed initialization sequence.
883f4a2713aSLionel Sambuc   ///
884f4a2713aSLionel Sambuc   /// \param S the semantic analysis object.
885f4a2713aSLionel Sambuc   ///
886f4a2713aSLionel Sambuc   /// \param Entity the entity being initialized.
887f4a2713aSLionel Sambuc   ///
888f4a2713aSLionel Sambuc   /// \param Kind the kind of initialization being performed.
889f4a2713aSLionel Sambuc   ///
890f4a2713aSLionel Sambuc   /// \param Args the argument(s) provided for initialization, ownership of
891f4a2713aSLionel Sambuc   /// which is transferred into the routine.
892f4a2713aSLionel Sambuc   ///
893f4a2713aSLionel Sambuc   /// \param ResultType if non-NULL, will be set to the type of the
894f4a2713aSLionel Sambuc   /// initialized object, which is the type of the declaration in most
895f4a2713aSLionel Sambuc   /// cases. However, when the initialized object is a variable of
896f4a2713aSLionel Sambuc   /// incomplete array type and the initializer is an initializer
897f4a2713aSLionel Sambuc   /// list, this type will be set to the completed array type.
898f4a2713aSLionel Sambuc   ///
899f4a2713aSLionel Sambuc   /// \returns an expression that performs the actual object initialization, if
900f4a2713aSLionel Sambuc   /// the initialization is well-formed. Otherwise, emits diagnostics
901f4a2713aSLionel Sambuc   /// and returns an invalid expression.
902f4a2713aSLionel Sambuc   ExprResult Perform(Sema &S,
903f4a2713aSLionel Sambuc                      const InitializedEntity &Entity,
904f4a2713aSLionel Sambuc                      const InitializationKind &Kind,
905f4a2713aSLionel Sambuc                      MultiExprArg Args,
906*0a6a1f1dSLionel Sambuc                      QualType *ResultType = nullptr);
907f4a2713aSLionel Sambuc 
908f4a2713aSLionel Sambuc   /// \brief Diagnose an potentially-invalid initialization sequence.
909f4a2713aSLionel Sambuc   ///
910f4a2713aSLionel Sambuc   /// \returns true if the initialization sequence was ill-formed,
911f4a2713aSLionel Sambuc   /// false otherwise.
912f4a2713aSLionel Sambuc   bool Diagnose(Sema &S,
913f4a2713aSLionel Sambuc                 const InitializedEntity &Entity,
914f4a2713aSLionel Sambuc                 const InitializationKind &Kind,
915f4a2713aSLionel Sambuc                 ArrayRef<Expr *> Args);
916f4a2713aSLionel Sambuc 
917f4a2713aSLionel Sambuc   /// \brief Determine the kind of initialization sequence computed.
getKind()918f4a2713aSLionel Sambuc   enum SequenceKind getKind() const { return SequenceKind; }
919f4a2713aSLionel Sambuc 
920f4a2713aSLionel Sambuc   /// \brief Set the kind of sequence computed.
setSequenceKind(enum SequenceKind SK)921f4a2713aSLionel Sambuc   void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
922f4a2713aSLionel Sambuc 
923f4a2713aSLionel Sambuc   /// \brief Determine whether the initialization sequence is valid.
924f4a2713aSLionel Sambuc   LLVM_EXPLICIT operator bool() const { return !Failed(); }
925f4a2713aSLionel Sambuc 
926f4a2713aSLionel Sambuc   /// \brief Determine whether the initialization sequence is invalid.
Failed()927f4a2713aSLionel Sambuc   bool Failed() const { return SequenceKind == FailedSequence; }
928f4a2713aSLionel Sambuc 
929f4a2713aSLionel Sambuc   typedef SmallVectorImpl<Step>::const_iterator step_iterator;
step_begin()930f4a2713aSLionel Sambuc   step_iterator step_begin() const { return Steps.begin(); }
step_end()931f4a2713aSLionel Sambuc   step_iterator step_end()   const { return Steps.end(); }
932f4a2713aSLionel Sambuc 
933f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization is a direct reference
934f4a2713aSLionel Sambuc   /// binding (C++ [dcl.init.ref]).
935f4a2713aSLionel Sambuc   bool isDirectReferenceBinding() const;
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization failed due to an ambiguity.
938f4a2713aSLionel Sambuc   bool isAmbiguous() const;
939f4a2713aSLionel Sambuc 
940f4a2713aSLionel Sambuc   /// \brief Determine whether this initialization is direct call to a
941f4a2713aSLionel Sambuc   /// constructor.
942f4a2713aSLionel Sambuc   bool isConstructorInitialization() const;
943f4a2713aSLionel Sambuc 
944f4a2713aSLionel Sambuc   /// \brief Returns whether the last step in this initialization sequence is a
945f4a2713aSLionel Sambuc   /// narrowing conversion, defined by C++0x [dcl.init.list]p7.
946f4a2713aSLionel Sambuc   ///
947f4a2713aSLionel Sambuc   /// If this function returns true, *isInitializerConstant will be set to
948f4a2713aSLionel Sambuc   /// describe whether *Initializer was a constant expression.  If
949f4a2713aSLionel Sambuc   /// *isInitializerConstant is set to true, *ConstantValue will be set to the
950f4a2713aSLionel Sambuc   /// evaluated value of *Initializer.
951f4a2713aSLionel Sambuc   bool endsWithNarrowing(ASTContext &Ctx, const Expr *Initializer,
952f4a2713aSLionel Sambuc                          bool *isInitializerConstant,
953f4a2713aSLionel Sambuc                          APValue *ConstantValue) const;
954f4a2713aSLionel Sambuc 
955f4a2713aSLionel Sambuc   /// \brief Add a new step in the initialization that resolves the address
956f4a2713aSLionel Sambuc   /// of an overloaded function to a specific function declaration.
957f4a2713aSLionel Sambuc   ///
958f4a2713aSLionel Sambuc   /// \param Function the function to which the overloaded function reference
959f4a2713aSLionel Sambuc   /// resolves.
960f4a2713aSLionel Sambuc   void AddAddressOverloadResolutionStep(FunctionDecl *Function,
961f4a2713aSLionel Sambuc                                         DeclAccessPair Found,
962f4a2713aSLionel Sambuc                                         bool HadMultipleCandidates);
963f4a2713aSLionel Sambuc 
964f4a2713aSLionel Sambuc   /// \brief Add a new step in the initialization that performs a derived-to-
965f4a2713aSLionel Sambuc   /// base cast.
966f4a2713aSLionel Sambuc   ///
967f4a2713aSLionel Sambuc   /// \param BaseType the base type to which we will be casting.
968f4a2713aSLionel Sambuc   ///
969f4a2713aSLionel Sambuc   /// \param Category Indicates whether the result will be treated as an
970f4a2713aSLionel Sambuc   /// rvalue, an xvalue, or an lvalue.
971f4a2713aSLionel Sambuc   void AddDerivedToBaseCastStep(QualType BaseType,
972f4a2713aSLionel Sambuc                                 ExprValueKind Category);
973f4a2713aSLionel Sambuc 
974f4a2713aSLionel Sambuc   /// \brief Add a new step binding a reference to an object.
975f4a2713aSLionel Sambuc   ///
976f4a2713aSLionel Sambuc   /// \param BindingTemporary True if we are binding a reference to a temporary
977f4a2713aSLionel Sambuc   /// object (thereby extending its lifetime); false if we are binding to an
978f4a2713aSLionel Sambuc   /// lvalue or an lvalue treated as an rvalue.
979f4a2713aSLionel Sambuc   void AddReferenceBindingStep(QualType T, bool BindingTemporary);
980f4a2713aSLionel Sambuc 
981f4a2713aSLionel Sambuc   /// \brief Add a new step that makes an extraneous copy of the input
982f4a2713aSLionel Sambuc   /// to a temporary of the same class type.
983f4a2713aSLionel Sambuc   ///
984f4a2713aSLionel Sambuc   /// This extraneous copy only occurs during reference binding in
985f4a2713aSLionel Sambuc   /// C++98/03, where we are permitted (but not required) to introduce
986f4a2713aSLionel Sambuc   /// an extra copy. At a bare minimum, we must check that we could
987f4a2713aSLionel Sambuc   /// call the copy constructor, and produce a diagnostic if the copy
988f4a2713aSLionel Sambuc   /// constructor is inaccessible or no copy constructor matches.
989f4a2713aSLionel Sambuc   //
990f4a2713aSLionel Sambuc   /// \param T The type of the temporary being created.
991f4a2713aSLionel Sambuc   void AddExtraneousCopyToTemporary(QualType T);
992f4a2713aSLionel Sambuc 
993f4a2713aSLionel Sambuc   /// \brief Add a new step invoking a conversion function, which is either
994f4a2713aSLionel Sambuc   /// a constructor or a conversion function.
995f4a2713aSLionel Sambuc   void AddUserConversionStep(FunctionDecl *Function,
996f4a2713aSLionel Sambuc                              DeclAccessPair FoundDecl,
997f4a2713aSLionel Sambuc                              QualType T,
998f4a2713aSLionel Sambuc                              bool HadMultipleCandidates);
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc   /// \brief Add a new step that performs a qualification conversion to the
1001f4a2713aSLionel Sambuc   /// given type.
1002f4a2713aSLionel Sambuc   void AddQualificationConversionStep(QualType Ty,
1003f4a2713aSLionel Sambuc                                      ExprValueKind Category);
1004f4a2713aSLionel Sambuc 
1005*0a6a1f1dSLionel Sambuc   /// \brief Add a new step that performs conversion from non-atomic to atomic
1006*0a6a1f1dSLionel Sambuc   /// type.
1007*0a6a1f1dSLionel Sambuc   void AddAtomicConversionStep(QualType Ty);
1008*0a6a1f1dSLionel Sambuc 
1009f4a2713aSLionel Sambuc   /// \brief Add a new step that performs a load of the given type.
1010f4a2713aSLionel Sambuc   ///
1011f4a2713aSLionel Sambuc   /// Although the term "LValueToRValue" is conventional, this applies to both
1012f4a2713aSLionel Sambuc   /// lvalues and xvalues.
1013f4a2713aSLionel Sambuc   void AddLValueToRValueStep(QualType Ty);
1014f4a2713aSLionel Sambuc 
1015f4a2713aSLionel Sambuc   /// \brief Add a new step that applies an implicit conversion sequence.
1016f4a2713aSLionel Sambuc   void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
1017f4a2713aSLionel Sambuc                                  QualType T, bool TopLevelOfInitList = false);
1018f4a2713aSLionel Sambuc 
1019f4a2713aSLionel Sambuc   /// \brief Add a list-initialization step.
1020f4a2713aSLionel Sambuc   void AddListInitializationStep(QualType T);
1021f4a2713aSLionel Sambuc 
1022f4a2713aSLionel Sambuc   /// \brief Add a constructor-initialization step.
1023f4a2713aSLionel Sambuc   ///
1024f4a2713aSLionel Sambuc   /// \param FromInitList The constructor call is syntactically an initializer
1025f4a2713aSLionel Sambuc   /// list.
1026f4a2713aSLionel Sambuc   /// \param AsInitList The constructor is called as an init list constructor.
1027f4a2713aSLionel Sambuc   void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
1028f4a2713aSLionel Sambuc                                         AccessSpecifier Access,
1029f4a2713aSLionel Sambuc                                         QualType T,
1030f4a2713aSLionel Sambuc                                         bool HadMultipleCandidates,
1031f4a2713aSLionel Sambuc                                         bool FromInitList, bool AsInitList);
1032f4a2713aSLionel Sambuc 
1033f4a2713aSLionel Sambuc   /// \brief Add a zero-initialization step.
1034f4a2713aSLionel Sambuc   void AddZeroInitializationStep(QualType T);
1035f4a2713aSLionel Sambuc 
1036f4a2713aSLionel Sambuc   /// \brief Add a C assignment step.
1037f4a2713aSLionel Sambuc   //
1038f4a2713aSLionel Sambuc   // FIXME: It isn't clear whether this should ever be needed;
1039f4a2713aSLionel Sambuc   // ideally, we would handle everything needed in C in the common
1040f4a2713aSLionel Sambuc   // path. However, that isn't the case yet.
1041f4a2713aSLionel Sambuc   void AddCAssignmentStep(QualType T);
1042f4a2713aSLionel Sambuc 
1043f4a2713aSLionel Sambuc   /// \brief Add a string init step.
1044f4a2713aSLionel Sambuc   void AddStringInitStep(QualType T);
1045f4a2713aSLionel Sambuc 
1046f4a2713aSLionel Sambuc   /// \brief Add an Objective-C object conversion step, which is
1047f4a2713aSLionel Sambuc   /// always a no-op.
1048f4a2713aSLionel Sambuc   void AddObjCObjectConversionStep(QualType T);
1049f4a2713aSLionel Sambuc 
1050f4a2713aSLionel Sambuc   /// \brief Add an array initialization step.
1051f4a2713aSLionel Sambuc   void AddArrayInitStep(QualType T);
1052f4a2713aSLionel Sambuc 
1053f4a2713aSLionel Sambuc   /// \brief Add a parenthesized array initialization step.
1054f4a2713aSLionel Sambuc   void AddParenthesizedArrayInitStep(QualType T);
1055f4a2713aSLionel Sambuc 
1056f4a2713aSLionel Sambuc   /// \brief Add a step to pass an object by indirect copy-restore.
1057f4a2713aSLionel Sambuc   void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy);
1058f4a2713aSLionel Sambuc 
1059f4a2713aSLionel Sambuc   /// \brief Add a step to "produce" an Objective-C object (by
1060f4a2713aSLionel Sambuc   /// retaining it).
1061f4a2713aSLionel Sambuc   void AddProduceObjCObjectStep(QualType T);
1062f4a2713aSLionel Sambuc 
1063f4a2713aSLionel Sambuc   /// \brief Add a step to construct a std::initializer_list object from an
1064f4a2713aSLionel Sambuc   /// initializer list.
1065f4a2713aSLionel Sambuc   void AddStdInitializerListConstructionStep(QualType T);
1066f4a2713aSLionel Sambuc 
1067f4a2713aSLionel Sambuc   /// \brief Add a step to initialize an OpenCL sampler from an integer
1068f4a2713aSLionel Sambuc   /// constant.
1069f4a2713aSLionel Sambuc   void AddOCLSamplerInitStep(QualType T);
1070f4a2713aSLionel Sambuc 
1071f4a2713aSLionel Sambuc   /// \brief Add a step to initialize an OpenCL event_t from a NULL
1072f4a2713aSLionel Sambuc   /// constant.
1073f4a2713aSLionel Sambuc   void AddOCLZeroEventStep(QualType T);
1074f4a2713aSLionel Sambuc 
1075f4a2713aSLionel Sambuc   /// \brief Add steps to unwrap a initializer list for a reference around a
1076f4a2713aSLionel Sambuc   /// single element and rewrap it at the end.
1077f4a2713aSLionel Sambuc   void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic);
1078f4a2713aSLionel Sambuc 
1079f4a2713aSLionel Sambuc   /// \brief Note that this initialization sequence failed.
SetFailed(FailureKind Failure)1080f4a2713aSLionel Sambuc   void SetFailed(FailureKind Failure) {
1081f4a2713aSLionel Sambuc     SequenceKind = FailedSequence;
1082f4a2713aSLionel Sambuc     this->Failure = Failure;
1083f4a2713aSLionel Sambuc     assert((Failure != FK_Incomplete || !FailedIncompleteType.isNull()) &&
1084f4a2713aSLionel Sambuc            "Incomplete type failure requires a type!");
1085f4a2713aSLionel Sambuc   }
1086f4a2713aSLionel Sambuc 
1087f4a2713aSLionel Sambuc   /// \brief Note that this initialization sequence failed due to failed
1088f4a2713aSLionel Sambuc   /// overload resolution.
1089f4a2713aSLionel Sambuc   void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
1090f4a2713aSLionel Sambuc 
1091f4a2713aSLionel Sambuc   /// \brief Retrieve a reference to the candidate set when overload
1092f4a2713aSLionel Sambuc   /// resolution fails.
getFailedCandidateSet()1093f4a2713aSLionel Sambuc   OverloadCandidateSet &getFailedCandidateSet() {
1094f4a2713aSLionel Sambuc     return FailedCandidateSet;
1095f4a2713aSLionel Sambuc   }
1096f4a2713aSLionel Sambuc 
1097f4a2713aSLionel Sambuc   /// \brief Get the overloading result, for when the initialization
1098f4a2713aSLionel Sambuc   /// sequence failed due to a bad overload.
getFailedOverloadResult()1099f4a2713aSLionel Sambuc   OverloadingResult getFailedOverloadResult() const {
1100f4a2713aSLionel Sambuc     return FailedOverloadResult;
1101f4a2713aSLionel Sambuc   }
1102f4a2713aSLionel Sambuc 
1103f4a2713aSLionel Sambuc   /// \brief Note that this initialization sequence failed due to an
1104f4a2713aSLionel Sambuc   /// incomplete type.
setIncompleteTypeFailure(QualType IncompleteType)1105f4a2713aSLionel Sambuc   void setIncompleteTypeFailure(QualType IncompleteType) {
1106f4a2713aSLionel Sambuc     FailedIncompleteType = IncompleteType;
1107f4a2713aSLionel Sambuc     SetFailed(FK_Incomplete);
1108f4a2713aSLionel Sambuc   }
1109f4a2713aSLionel Sambuc 
1110f4a2713aSLionel Sambuc   /// \brief Determine why initialization failed.
getFailureKind()1111f4a2713aSLionel Sambuc   FailureKind getFailureKind() const {
1112f4a2713aSLionel Sambuc     assert(Failed() && "Not an initialization failure!");
1113f4a2713aSLionel Sambuc     return Failure;
1114f4a2713aSLionel Sambuc   }
1115f4a2713aSLionel Sambuc 
1116f4a2713aSLionel Sambuc   /// \brief Dump a representation of this initialization sequence to
1117f4a2713aSLionel Sambuc   /// the given stream, for debugging purposes.
1118f4a2713aSLionel Sambuc   void dump(raw_ostream &OS) const;
1119f4a2713aSLionel Sambuc 
1120f4a2713aSLionel Sambuc   /// \brief Dump a representation of this initialization sequence to
1121f4a2713aSLionel Sambuc   /// standard error, for debugging purposes.
1122f4a2713aSLionel Sambuc   void dump() const;
1123f4a2713aSLionel Sambuc };
1124f4a2713aSLionel Sambuc 
1125f4a2713aSLionel Sambuc } // end namespace clang
1126f4a2713aSLionel Sambuc 
1127f4a2713aSLionel Sambuc #endif // LLVM_CLANG_SEMA_INITIALIZATION_H
1128