blob: 35adf9e49ee08b7e7616e9bb96dbce35645f4424 [file] [log] [blame]
Douglas Gregord87b61f2009-12-10 17:56:55 +00001//===--- SemaInit.h - Semantic Analysis for Initializers --------*- C++ -*-===//
Douglas Gregor20093b42009-12-09 23:02:17 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides supporting data types for initialization of objects.
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_CLANG_SEMA_INIT_H
14#define LLVM_CLANG_SEMA_INIT_H
15
16#include "SemaOverload.h"
Douglas Gregord6542d82009-12-22 15:35:07 +000017#include "clang/AST/Type.h"
John McCallb13b7372010-02-01 03:16:54 +000018#include "clang/AST/UnresolvedSet.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000019#include "clang/Parse/Action.h"
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/PointerIntPair.h"
22#include "llvm/ADT/SmallVector.h"
23#include <cassert>
24
Douglas Gregorde4b1d82010-01-29 19:14:02 +000025namespace llvm {
26 class raw_ostream;
27}
28
Douglas Gregor20093b42009-12-09 23:02:17 +000029namespace clang {
30
31class CXXBaseSpecifier;
32class DeclaratorDecl;
33class DeclaratorInfo;
34class FieldDecl;
35class FunctionDecl;
36class ParmVarDecl;
37class Sema;
38class TypeLoc;
39class VarDecl;
40
41/// \brief Describes an entity that is being initialized.
42class InitializedEntity {
43public:
44 /// \brief Specifies the kind of entity being initialized.
45 enum EntityKind {
46 /// \brief The entity being initialized is a variable.
47 EK_Variable,
48 /// \brief The entity being initialized is a function parameter.
49 EK_Parameter,
50 /// \brief The entity being initialized is the result of a function call.
51 EK_Result,
52 /// \brief The entity being initialized is an exception object that
53 /// is being thrown.
54 EK_Exception,
Anders Carlssona729bbb2010-01-23 05:47:27 +000055 /// \brief The entity being initialized is a non-static data member
56 /// subobject.
57 EK_Member,
58 /// \brief The entity being initialized is an element of an array.
59 EK_ArrayElement,
Douglas Gregor18ef5e22009-12-18 05:02:21 +000060 /// \brief The entity being initialized is an object (or array of
61 /// objects) allocated via new.
62 EK_New,
Douglas Gregor20093b42009-12-09 23:02:17 +000063 /// \brief The entity being initialized is a temporary object.
64 EK_Temporary,
65 /// \brief The entity being initialized is a base member subobject.
66 EK_Base,
Anders Carlssond3d824d2010-01-23 04:34:47 +000067 /// \brief The entity being initialized is an element of a vector.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000068 /// or vector.
Anders Carlssond3d824d2010-01-23 04:34:47 +000069 EK_VectorElement
Douglas Gregor20093b42009-12-09 23:02:17 +000070 };
71
72private:
73 /// \brief The kind of entity being initialized.
74 EntityKind Kind;
75
Douglas Gregorcb57fb92009-12-16 06:35:08 +000076 /// \brief If non-NULL, the parent entity in which this
77 /// initialization occurs.
78 const InitializedEntity *Parent;
79
Douglas Gregord6542d82009-12-22 15:35:07 +000080 /// \brief The type of the object or reference being initialized.
81 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000082
83 union {
84 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
85 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
86 DeclaratorDecl *VariableOrMember;
87
Douglas Gregor3c9034c2010-05-15 00:13:29 +000088 struct {
89 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
90 /// location of the 'return', 'throw', or 'new' keyword,
91 /// respectively. When Kind == EK_Temporary, the location where
92 /// the temporary is being created.
93 unsigned Location;
94
95 /// \brief Whether the
96 bool NRVO;
97 } LocAndNRVO;
Douglas Gregor20093b42009-12-09 23:02:17 +000098
99 /// \brief When Kind == EK_Base, the base specifier that provides the
Anders Carlsson711f34a2010-04-21 19:52:01 +0000100 /// base class. The lower bit specifies whether the base is an inherited
101 /// virtual base.
102 uintptr_t Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000103
Anders Carlsson711f34a2010-04-21 19:52:01 +0000104 /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the
105 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000106 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000107 };
108
109 InitializedEntity() { }
110
111 /// \brief Create the initialization entity for a variable.
112 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000113 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
114 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000115
116 /// \brief Create the initialization entity for a parameter.
117 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000118 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000119 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000120
Douglas Gregora188ff22009-12-22 16:09:06 +0000121 /// \brief Create the initialization entity for the result of a
122 /// function, throwing an object, performing an explicit cast, or
123 /// initializing a parameter for which there is no declaration.
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000124 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
125 bool NRVO = false)
126 : Kind(Kind), Parent(0), Type(Type)
127 {
128 LocAndNRVO.Location = Loc.getRawEncoding();
129 LocAndNRVO.NRVO = NRVO;
130 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000131
132 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000133 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000134 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
135 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000136
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000137 /// \brief Create the initialization entity for an array element.
138 InitializedEntity(ASTContext &Context, unsigned Index,
139 const InitializedEntity &Parent);
140
Douglas Gregor20093b42009-12-09 23:02:17 +0000141public:
142 /// \brief Create the initialization entity for a variable.
143 static InitializedEntity InitializeVariable(VarDecl *Var) {
144 return InitializedEntity(Var);
145 }
146
147 /// \brief Create the initialization entity for a parameter.
148 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
149 return InitializedEntity(Parm);
150 }
151
Douglas Gregora188ff22009-12-22 16:09:06 +0000152 /// \brief Create the initialization entity for a parameter that is
153 /// only known by its type.
154 static InitializedEntity InitializeParameter(QualType Type) {
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000155 InitializedEntity Entity;
156 Entity.Kind = EK_Parameter;
157 Entity.Type = Type;
158 Entity.Parent = 0;
159 Entity.VariableOrMember = 0;
160 return Entity;
Douglas Gregora188ff22009-12-22 16:09:06 +0000161 }
162
Douglas Gregor20093b42009-12-09 23:02:17 +0000163 /// \brief Create the initialization entity for the result of a function.
164 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000165 QualType Type, bool NRVO) {
166 return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
Douglas Gregor20093b42009-12-09 23:02:17 +0000167 }
168
169 /// \brief Create the initialization entity for an exception object.
170 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000171 QualType Type, bool NRVO) {
172 return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
Douglas Gregor20093b42009-12-09 23:02:17 +0000173 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000174
175 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000176 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
177 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000178 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000179
180 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000181 static InitializedEntity InitializeTemporary(QualType Type) {
182 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000183 }
184
185 /// \brief Create the initialization entity for a base class subobject.
186 static InitializedEntity InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +0000187 CXXBaseSpecifier *Base,
188 bool IsInheritedVirtualBase);
Douglas Gregor20093b42009-12-09 23:02:17 +0000189
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000190 /// \brief Create the initialization entity for a member subobject.
191 static InitializedEntity InitializeMember(FieldDecl *Member,
192 const InitializedEntity *Parent = 0) {
193 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000194 }
195
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000196 /// \brief Create the initialization entity for an array element.
197 static InitializedEntity InitializeElement(ASTContext &Context,
198 unsigned Index,
199 const InitializedEntity &Parent) {
200 return InitializedEntity(Context, Index, Parent);
201 }
202
Douglas Gregor20093b42009-12-09 23:02:17 +0000203 /// \brief Determine the kind of initialization.
204 EntityKind getKind() const { return Kind; }
205
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000206 /// \brief Retrieve the parent of the entity being initialized, when
207 /// the initialization itself is occuring within the context of a
208 /// larger initialization.
209 const InitializedEntity *getParent() const { return Parent; }
210
Douglas Gregor20093b42009-12-09 23:02:17 +0000211 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000212 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000213
Douglas Gregor99a2e602009-12-16 01:38:02 +0000214 /// \brief Retrieve the name of the entity being initialized.
215 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000216
217 /// \brief Retrieve the variable, parameter, or field being
218 /// initialized.
219 DeclaratorDecl *getDecl() const;
220
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000221 /// \brief Determine whether this initialization allows the named return
222 /// value optimization, which also applies to thrown objects.
223 bool allowsNRVO() const;
224
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000225 /// \brief Retrieve the base specifier.
226 CXXBaseSpecifier *getBaseSpecifier() const {
227 assert(getKind() == EK_Base && "Not a base specifier");
Anders Carlsson711f34a2010-04-21 19:52:01 +0000228 return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
229 }
230
231 /// \brief Return whether the base is an inherited virtual base.
232 bool isInheritedVirtualBase() const {
233 assert(getKind() == EK_Base && "Not a base specifier");
234 return Base & 0x1;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000235 }
236
Douglas Gregor20093b42009-12-09 23:02:17 +0000237 /// \brief Determine the location of the 'return' keyword when initializing
238 /// the result of a function call.
239 SourceLocation getReturnLoc() const {
240 assert(getKind() == EK_Result && "No 'return' location!");
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000241 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
Douglas Gregor20093b42009-12-09 23:02:17 +0000242 }
243
244 /// \brief Determine the location of the 'throw' keyword when initializing
245 /// an exception object.
246 SourceLocation getThrowLoc() const {
247 assert(getKind() == EK_Exception && "No 'throw' location!");
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000248 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
Douglas Gregor20093b42009-12-09 23:02:17 +0000249 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000250
251 /// \brief If this is already the initializer for an array or vector
252 /// element, sets the element index.
253 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000254 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000255 this->Index = Index;
256 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000257};
258
259/// \brief Describes the kind of initialization being performed, along with
260/// location information for tokens related to the initialization (equal sign,
261/// parentheses).
262class InitializationKind {
263public:
264 /// \brief The kind of initialization being performed.
265 enum InitKind {
266 IK_Direct, ///< Direct initialization
267 IK_Copy, ///< Copy initialization
268 IK_Default, ///< Default initialization
269 IK_Value ///< Value initialization
270 };
271
272private:
273 /// \brief The kind of initialization that we're storing.
274 enum StoredInitKind {
275 SIK_Direct = IK_Direct, ///< Direct initialization
276 SIK_Copy = IK_Copy, ///< Copy initialization
277 SIK_Default = IK_Default, ///< Default initialization
278 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000279 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000280 SIK_DirectCast, ///< Direct initialization due to a cast
281 /// \brief Direct initialization due to a C-style or functional cast.
282 SIK_DirectCStyleOrFunctionalCast
283 };
284
285 /// \brief The kind of initialization being performed.
286 StoredInitKind Kind;
287
288 /// \brief The source locations involved in the initialization.
289 SourceLocation Locations[3];
290
291 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
292 SourceLocation Loc2, SourceLocation Loc3)
293 : Kind(Kind)
294 {
295 Locations[0] = Loc1;
296 Locations[1] = Loc2;
297 Locations[2] = Loc3;
298 }
299
300public:
301 /// \brief Create a direct initialization.
302 static InitializationKind CreateDirect(SourceLocation InitLoc,
303 SourceLocation LParenLoc,
304 SourceLocation RParenLoc) {
305 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
306 }
307
308 /// \brief Create a direct initialization due to a cast.
309 static InitializationKind CreateCast(SourceRange TypeRange,
310 bool IsCStyleCast) {
311 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
312 : SIK_DirectCast,
313 TypeRange.getBegin(), TypeRange.getBegin(),
314 TypeRange.getEnd());
315 }
316
317 /// \brief Create a copy initialization.
318 static InitializationKind CreateCopy(SourceLocation InitLoc,
319 SourceLocation EqualLoc) {
320 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
321 }
322
323 /// \brief Create a default initialization.
324 static InitializationKind CreateDefault(SourceLocation InitLoc) {
325 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
326 }
327
328 /// \brief Create a value initialization.
329 static InitializationKind CreateValue(SourceLocation InitLoc,
330 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000331 SourceLocation RParenLoc,
332 bool isImplicit = false) {
333 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
334 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000335 }
336
337 /// \brief Determine the initialization kind.
338 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000339 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000340 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000341 if (Kind == SIK_ImplicitValue)
342 return IK_Value;
343
Douglas Gregor20093b42009-12-09 23:02:17 +0000344 return (InitKind)Kind;
345 }
346
347 /// \brief Determine whether this initialization is an explicit cast.
348 bool isExplicitCast() const {
349 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
350 }
351
352 /// \brief Determine whether this initialization is a C-style cast.
353 bool isCStyleOrFunctionalCast() const {
354 return Kind == SIK_DirectCStyleOrFunctionalCast;
355 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000356
357 /// \brief Determine whether this initialization is an implicit
358 /// value-initialization, e.g., as occurs during aggregate
359 /// initialization.
360 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
361
Douglas Gregor20093b42009-12-09 23:02:17 +0000362 /// \brief Retrieve the location at which initialization is occurring.
363 SourceLocation getLocation() const { return Locations[0]; }
364
365 /// \brief Retrieve the source range that covers the initialization.
366 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000367 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000368 }
369
370 /// \brief Retrieve the location of the equal sign for copy initialization
371 /// (if present).
372 SourceLocation getEqualLoc() const {
373 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
374 return Locations[1];
375 }
376
377 /// \brief Retrieve the source range containing the locations of the open
378 /// and closing parentheses for value and direct initializations.
379 SourceRange getParenRange() const {
380 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
381 "Only direct- and value-initialization have parentheses");
382 return SourceRange(Locations[1], Locations[2]);
383 }
384};
385
386/// \brief Describes the sequence of initializations required to initialize
387/// a given object or reference with a set of arguments.
388class InitializationSequence {
389public:
390 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000391 ///
392 /// FIXME: Much of this information is in the initialization steps... why is
393 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000394 enum SequenceKind {
395 /// \brief A failed initialization sequence. The failure kind tells what
396 /// happened.
397 FailedSequence = 0,
398
399 /// \brief A dependent initialization, which could not be
400 /// type-checked due to the presence of dependent types or
401 /// dependently-type expressions.
402 DependentSequence,
403
Douglas Gregor4a520a22009-12-14 17:27:33 +0000404 /// \brief A user-defined conversion sequence.
405 UserDefinedConversion,
406
Douglas Gregor51c56d62009-12-14 20:49:26 +0000407 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000408 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000409
Douglas Gregor20093b42009-12-09 23:02:17 +0000410 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000411 ReferenceBinding,
412
413 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000414 ListInitialization,
415
416 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000417 ZeroInitialization,
418
419 /// \brief No initialization required.
420 NoInitialization,
421
422 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000423 StandardConversion,
424
425 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000426 CAssignment,
427
428 /// \brief String initialization
429 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000430 };
431
432 /// \brief Describes the kind of a particular step in an initialization
433 /// sequence.
434 enum StepKind {
435 /// \brief Resolve the address of an overloaded function to a specific
436 /// function declaration.
437 SK_ResolveAddressOfOverloadedFunction,
438 /// \brief Perform a derived-to-base cast, producing an rvalue.
439 SK_CastDerivedToBaseRValue,
440 /// \brief Perform a derived-to-base cast, producing an lvalue.
441 SK_CastDerivedToBaseLValue,
442 /// \brief Reference binding to an lvalue.
443 SK_BindReference,
444 /// \brief Reference binding to a temporary.
445 SK_BindReferenceToTemporary,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000446 /// \brief An optional copy of a temporary object to another
447 /// temporary object, which is permitted (but not required) by
448 /// C++98/03 but not C++0x.
449 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000450 /// \brief Perform a user-defined conversion, either via a conversion
451 /// function or via a constructor.
452 SK_UserConversion,
453 /// \brief Perform a qualification conversion, producing an rvalue.
454 SK_QualificationConversionRValue,
455 /// \brief Perform a qualification conversion, producing an lvalue.
456 SK_QualificationConversionLValue,
457 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000458 SK_ConversionSequence,
459 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000460 SK_ListInitialization,
461 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000462 SK_ConstructorInitialization,
463 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000464 SK_ZeroInitialization,
465 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000466 SK_CAssignment,
467 /// \brief Initialization by string
468 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000469 };
470
471 /// \brief A single step in the initialization sequence.
472 class Step {
473 public:
474 /// \brief The kind of conversion or initialization step we are taking.
475 StepKind Kind;
476
477 // \brief The type that results from this initialization.
478 QualType Type;
479
480 union {
481 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
482 /// SK_UserConversion, the function that the expression should be
483 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000484 ///
485 /// Always a FunctionDecl.
486 /// For conversion decls, the naming class is the source type.
487 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000488 struct {
489 FunctionDecl *Function;
490 DeclAccessPair FoundDecl;
491 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000492
493 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
494 /// sequence
495 ImplicitConversionSequence *ICS;
496 };
497
498 void Destroy();
499 };
500
501private:
502 /// \brief The kind of initialization sequence computed.
503 enum SequenceKind SequenceKind;
504
505 /// \brief Steps taken by this initialization.
506 llvm::SmallVector<Step, 4> Steps;
507
508public:
509 /// \brief Describes why initialization failed.
510 enum FailureKind {
511 /// \brief Too many initializers provided for a reference.
512 FK_TooManyInitsForReference,
513 /// \brief Array must be initialized with an initializer list.
514 FK_ArrayNeedsInitList,
515 /// \brief Array must be initialized with an initializer list or a
516 /// string literal.
517 FK_ArrayNeedsInitListOrStringLiteral,
518 /// \brief Cannot resolve the address of an overloaded function.
519 FK_AddressOfOverloadFailed,
520 /// \brief Overloading due to reference initialization failed.
521 FK_ReferenceInitOverloadFailed,
522 /// \brief Non-const lvalue reference binding to a temporary.
523 FK_NonConstLValueReferenceBindingToTemporary,
524 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
525 /// type.
526 FK_NonConstLValueReferenceBindingToUnrelated,
527 /// \brief Rvalue reference binding to an lvalue.
528 FK_RValueReferenceBindingToLValue,
529 /// \brief Reference binding drops qualifiers.
530 FK_ReferenceInitDropsQualifiers,
531 /// \brief Reference binding failed.
532 FK_ReferenceInitFailed,
533 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000534 FK_ConversionFailed,
535 /// \brief Too many initializers for scalar
536 FK_TooManyInitsForScalar,
537 /// \brief Reference initialization from an initializer list
538 FK_ReferenceBindingToInitList,
539 /// \brief Initialization of some unused destination type with an
540 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000541 FK_InitListBadDestinationType,
542 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000543 FK_UserConversionOverloadFailed,
544 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000545 FK_ConstructorOverloadFailed,
546 /// \brief Default-initialization of a 'const' object.
547 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000548 };
549
550private:
551 /// \brief The reason why initialization failued.
552 FailureKind Failure;
553
554 /// \brief The failed result of overload resolution.
555 OverloadingResult FailedOverloadResult;
556
557 /// \brief The candidate set created when initialization failed.
558 OverloadCandidateSet FailedCandidateSet;
Douglas Gregora41a8c52010-04-22 00:20:18 +0000559
560 /// \brief Prints a follow-up note that highlights the location of
561 /// the initialized entity, if it's remote.
562 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
563
Douglas Gregor20093b42009-12-09 23:02:17 +0000564public:
565 /// \brief Try to perform initialization of the given entity, creating a
566 /// record of the steps required to perform the initialization.
567 ///
568 /// The generated initialization sequence will either contain enough
569 /// information to diagnose
570 ///
571 /// \param S the semantic analysis object.
572 ///
573 /// \param Entity the entity being initialized.
574 ///
575 /// \param Kind the kind of initialization being performed.
576 ///
577 /// \param Args the argument(s) provided for initialization.
578 ///
579 /// \param NumArgs the number of arguments provided for initialization.
580 InitializationSequence(Sema &S,
581 const InitializedEntity &Entity,
582 const InitializationKind &Kind,
583 Expr **Args,
584 unsigned NumArgs);
585
586 ~InitializationSequence();
587
588 /// \brief Perform the actual initialization of the given entity based on
589 /// the computed initialization sequence.
590 ///
591 /// \param S the semantic analysis object.
592 ///
593 /// \param Entity the entity being initialized.
594 ///
595 /// \param Kind the kind of initialization being performed.
596 ///
597 /// \param Args the argument(s) provided for initialization, ownership of
598 /// which is transfered into the routine.
599 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000600 /// \param ResultType if non-NULL, will be set to the type of the
601 /// initialized object, which is the type of the declaration in most
602 /// cases. However, when the initialized object is a variable of
603 /// incomplete array type and the initializer is an initializer
604 /// list, this type will be set to the completed array type.
605 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000606 /// \returns an expression that performs the actual object initialization, if
607 /// the initialization is well-formed. Otherwise, emits diagnostics
608 /// and returns an invalid expression.
609 Action::OwningExprResult Perform(Sema &S,
610 const InitializedEntity &Entity,
611 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000612 Action::MultiExprArg Args,
613 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000614
615 /// \brief Diagnose an potentially-invalid initialization sequence.
616 ///
617 /// \returns true if the initialization sequence was ill-formed,
618 /// false otherwise.
619 bool Diagnose(Sema &S,
620 const InitializedEntity &Entity,
621 const InitializationKind &Kind,
622 Expr **Args, unsigned NumArgs);
623
624 /// \brief Determine the kind of initialization sequence computed.
625 enum SequenceKind getKind() const { return SequenceKind; }
626
627 /// \brief Set the kind of sequence computed.
628 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
629
630 /// \brief Determine whether the initialization sequence is valid.
631 operator bool() const { return SequenceKind != FailedSequence; }
632
633 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
634 step_iterator step_begin() const { return Steps.begin(); }
635 step_iterator step_end() const { return Steps.end(); }
636
Douglas Gregorb70cf442010-03-26 20:14:36 +0000637 /// \brief Determine whether this initialization is a direct reference
638 /// binding (C++ [dcl.init.ref]).
639 bool isDirectReferenceBinding() const;
640
641 /// \brief Determine whether this initialization failed due to an ambiguity.
642 bool isAmbiguous() const;
643
Douglas Gregord6e44a32010-04-16 22:09:46 +0000644 /// \brief Determine whether this initialization is direct call to a
645 /// constructor.
646 bool isConstructorInitialization() const;
647
Douglas Gregor20093b42009-12-09 23:02:17 +0000648 /// \brief Add a new step in the initialization that resolves the address
649 /// of an overloaded function to a specific function declaration.
650 ///
651 /// \param Function the function to which the overloaded function reference
652 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000653 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
654 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000655
656 /// \brief Add a new step in the initialization that performs a derived-to-
657 /// base cast.
658 ///
659 /// \param BaseType the base type to which we will be casting.
660 ///
661 /// \param IsLValue true if the result of this cast will be treated as
662 /// an lvalue.
663 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
664
665 /// \brief Add a new step binding a reference to an object.
666 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000667 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000668 /// object (thereby extending its lifetime); false if we are binding to an
669 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000670 ///
671 /// \param UnnecessaryCopy True if we should check for a copy
672 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000673 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000674
675 /// \brief Add a new step that makes an extraneous copy of the input
676 /// to a temporary of the same class type.
677 ///
678 /// This extraneous copy only occurs during reference binding in
679 /// C++98/03, where we are permitted (but not required) to introduce
680 /// an extra copy. At a bare minimum, we must check that we could
681 /// call the copy constructor, and produce a diagnostic if the copy
682 /// constructor is inaccessible or no copy constructor matches.
683 //
684 /// \param T The type of the temporary being created.
685 void AddExtraneousCopyToTemporary(QualType T);
686
Douglas Gregor20093b42009-12-09 23:02:17 +0000687 /// \brief Add a new step invoking a conversion function, which is either
688 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000689 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000690 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000691 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000692
693 /// \brief Add a new step that performs a qualification conversion to the
694 /// given type.
695 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
696
697 /// \brief Add a new step that applies an implicit conversion sequence.
698 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
699 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000700
701 /// \brief Add a list-initialiation step
702 void AddListInitializationStep(QualType T);
703
Douglas Gregor71d17402009-12-15 00:01:57 +0000704 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000705 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000706 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000707 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000708
709 /// \brief Add a zero-initialization step.
710 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000711
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000712 /// \brief Add a C assignment step.
713 //
714 // FIXME: It isn't clear whether this should ever be needed;
715 // ideally, we would handle everything needed in C in the common
716 // path. However, that isn't the case yet.
717 void AddCAssignmentStep(QualType T);
718
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000719 /// \brief Add a string init step.
720 void AddStringInitStep(QualType T);
721
Douglas Gregor20093b42009-12-09 23:02:17 +0000722 /// \brief Note that this initialization sequence failed.
723 void SetFailed(FailureKind Failure) {
724 SequenceKind = FailedSequence;
725 this->Failure = Failure;
726 }
727
728 /// \brief Note that this initialization sequence failed due to failed
729 /// overload resolution.
730 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
731
732 /// \brief Retrieve a reference to the candidate set when overload
733 /// resolution fails.
734 OverloadCandidateSet &getFailedCandidateSet() {
735 return FailedCandidateSet;
736 }
737
738 /// \brief Determine why initialization failed.
739 FailureKind getFailureKind() const {
740 assert(getKind() == FailedSequence && "Not an initialization failure!");
741 return Failure;
742 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000743
744 /// \brief Dump a representation of this initialization sequence to
745 /// the given stream, for debugging purposes.
746 void dump(llvm::raw_ostream &OS) const;
747
748 /// \brief Dump a representation of this initialization sequence to
749 /// standard error, for debugging purposes.
750 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000751};
752
753} // end namespace clang
754
755#endif // LLVM_CLANG_SEMA_INIT_H