blob: 2b49df28fe8647f89de9e64926cd3672acf1cd02 [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 Gregor18ef5e22009-12-18 05:02:21 +000088 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
89 /// location of the 'return', 'throw', or 'new' keyword,
90 /// respectively. When Kind == EK_Temporary, the location where
91 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000092 unsigned Location;
93
94 /// \brief When Kind == EK_Base, the base specifier that provides the
95 /// base class.
96 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000097
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000098 /// \brief When Kind = EK_ArrayElement or EK_VectorElement, the
99 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000100 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000101 };
102
103 InitializedEntity() { }
104
105 /// \brief Create the initialization entity for a variable.
106 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000107 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
108 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000109
110 /// \brief Create the initialization entity for a parameter.
111 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000112 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000113 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000114
Douglas Gregora188ff22009-12-22 16:09:06 +0000115 /// \brief Create the initialization entity for the result of a
116 /// function, throwing an object, performing an explicit cast, or
117 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000118 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
119 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000120
121 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000122 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000123 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
124 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000125
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000126 /// \brief Create the initialization entity for an array element.
127 InitializedEntity(ASTContext &Context, unsigned Index,
128 const InitializedEntity &Parent);
129
Douglas Gregor20093b42009-12-09 23:02:17 +0000130public:
131 /// \brief Create the initialization entity for a variable.
132 static InitializedEntity InitializeVariable(VarDecl *Var) {
133 return InitializedEntity(Var);
134 }
135
136 /// \brief Create the initialization entity for a parameter.
137 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
138 return InitializedEntity(Parm);
139 }
140
Douglas Gregora188ff22009-12-22 16:09:06 +0000141 /// \brief Create the initialization entity for a parameter that is
142 /// only known by its type.
143 static InitializedEntity InitializeParameter(QualType Type) {
144 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
145 }
146
Douglas Gregor20093b42009-12-09 23:02:17 +0000147 /// \brief Create the initialization entity for the result of a function.
148 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000149 QualType Type) {
150 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000151 }
152
153 /// \brief Create the initialization entity for an exception object.
154 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000155 QualType Type) {
156 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000157 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000158
159 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000160 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
161 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000162 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000163
164 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000165 static InitializedEntity InitializeTemporary(QualType Type) {
166 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000167 }
168
169 /// \brief Create the initialization entity for a base class subobject.
170 static InitializedEntity InitializeBase(ASTContext &Context,
171 CXXBaseSpecifier *Base);
172
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000173 /// \brief Create the initialization entity for a member subobject.
174 static InitializedEntity InitializeMember(FieldDecl *Member,
175 const InitializedEntity *Parent = 0) {
176 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000177 }
178
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000179 /// \brief Create the initialization entity for an array element.
180 static InitializedEntity InitializeElement(ASTContext &Context,
181 unsigned Index,
182 const InitializedEntity &Parent) {
183 return InitializedEntity(Context, Index, Parent);
184 }
185
Douglas Gregor20093b42009-12-09 23:02:17 +0000186 /// \brief Determine the kind of initialization.
187 EntityKind getKind() const { return Kind; }
188
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000189 /// \brief Retrieve the parent of the entity being initialized, when
190 /// the initialization itself is occuring within the context of a
191 /// larger initialization.
192 const InitializedEntity *getParent() const { return Parent; }
193
Douglas Gregor20093b42009-12-09 23:02:17 +0000194 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000195 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000196
Douglas Gregor99a2e602009-12-16 01:38:02 +0000197 /// \brief Retrieve the name of the entity being initialized.
198 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000199
200 /// \brief Retrieve the variable, parameter, or field being
201 /// initialized.
202 DeclaratorDecl *getDecl() const;
203
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000204 /// \brief Retrieve the base specifier.
205 CXXBaseSpecifier *getBaseSpecifier() const {
206 assert(getKind() == EK_Base && "Not a base specifier");
207 return Base;
208 }
209
Douglas Gregor20093b42009-12-09 23:02:17 +0000210 /// \brief Determine the location of the 'return' keyword when initializing
211 /// the result of a function call.
212 SourceLocation getReturnLoc() const {
213 assert(getKind() == EK_Result && "No 'return' location!");
214 return SourceLocation::getFromRawEncoding(Location);
215 }
216
217 /// \brief Determine the location of the 'throw' keyword when initializing
218 /// an exception object.
219 SourceLocation getThrowLoc() const {
220 assert(getKind() == EK_Exception && "No 'throw' location!");
221 return SourceLocation::getFromRawEncoding(Location);
222 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000223
224 /// \brief If this is already the initializer for an array or vector
225 /// element, sets the element index.
226 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000227 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000228 this->Index = Index;
229 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000230};
231
232/// \brief Describes the kind of initialization being performed, along with
233/// location information for tokens related to the initialization (equal sign,
234/// parentheses).
235class InitializationKind {
236public:
237 /// \brief The kind of initialization being performed.
238 enum InitKind {
239 IK_Direct, ///< Direct initialization
240 IK_Copy, ///< Copy initialization
241 IK_Default, ///< Default initialization
242 IK_Value ///< Value initialization
243 };
244
245private:
246 /// \brief The kind of initialization that we're storing.
247 enum StoredInitKind {
248 SIK_Direct = IK_Direct, ///< Direct initialization
249 SIK_Copy = IK_Copy, ///< Copy initialization
250 SIK_Default = IK_Default, ///< Default initialization
251 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000252 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000253 SIK_DirectCast, ///< Direct initialization due to a cast
254 /// \brief Direct initialization due to a C-style or functional cast.
255 SIK_DirectCStyleOrFunctionalCast
256 };
257
258 /// \brief The kind of initialization being performed.
259 StoredInitKind Kind;
260
261 /// \brief The source locations involved in the initialization.
262 SourceLocation Locations[3];
263
264 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
265 SourceLocation Loc2, SourceLocation Loc3)
266 : Kind(Kind)
267 {
268 Locations[0] = Loc1;
269 Locations[1] = Loc2;
270 Locations[2] = Loc3;
271 }
272
273public:
274 /// \brief Create a direct initialization.
275 static InitializationKind CreateDirect(SourceLocation InitLoc,
276 SourceLocation LParenLoc,
277 SourceLocation RParenLoc) {
278 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
279 }
280
281 /// \brief Create a direct initialization due to a cast.
282 static InitializationKind CreateCast(SourceRange TypeRange,
283 bool IsCStyleCast) {
284 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
285 : SIK_DirectCast,
286 TypeRange.getBegin(), TypeRange.getBegin(),
287 TypeRange.getEnd());
288 }
289
290 /// \brief Create a copy initialization.
291 static InitializationKind CreateCopy(SourceLocation InitLoc,
292 SourceLocation EqualLoc) {
293 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
294 }
295
296 /// \brief Create a default initialization.
297 static InitializationKind CreateDefault(SourceLocation InitLoc) {
298 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
299 }
300
301 /// \brief Create a value initialization.
302 static InitializationKind CreateValue(SourceLocation InitLoc,
303 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000304 SourceLocation RParenLoc,
305 bool isImplicit = false) {
306 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
307 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000308 }
309
310 /// \brief Determine the initialization kind.
311 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000312 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000313 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000314 if (Kind == SIK_ImplicitValue)
315 return IK_Value;
316
Douglas Gregor20093b42009-12-09 23:02:17 +0000317 return (InitKind)Kind;
318 }
319
320 /// \brief Determine whether this initialization is an explicit cast.
321 bool isExplicitCast() const {
322 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
323 }
324
325 /// \brief Determine whether this initialization is a C-style cast.
326 bool isCStyleOrFunctionalCast() const {
327 return Kind == SIK_DirectCStyleOrFunctionalCast;
328 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000329
330 /// \brief Determine whether this initialization is an implicit
331 /// value-initialization, e.g., as occurs during aggregate
332 /// initialization.
333 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
334
Douglas Gregor20093b42009-12-09 23:02:17 +0000335 /// \brief Retrieve the location at which initialization is occurring.
336 SourceLocation getLocation() const { return Locations[0]; }
337
338 /// \brief Retrieve the source range that covers the initialization.
339 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000340 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000341 }
342
343 /// \brief Retrieve the location of the equal sign for copy initialization
344 /// (if present).
345 SourceLocation getEqualLoc() const {
346 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
347 return Locations[1];
348 }
349
350 /// \brief Retrieve the source range containing the locations of the open
351 /// and closing parentheses for value and direct initializations.
352 SourceRange getParenRange() const {
353 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
354 "Only direct- and value-initialization have parentheses");
355 return SourceRange(Locations[1], Locations[2]);
356 }
357};
358
359/// \brief Describes the sequence of initializations required to initialize
360/// a given object or reference with a set of arguments.
361class InitializationSequence {
362public:
363 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000364 ///
365 /// FIXME: Much of this information is in the initialization steps... why is
366 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000367 enum SequenceKind {
368 /// \brief A failed initialization sequence. The failure kind tells what
369 /// happened.
370 FailedSequence = 0,
371
372 /// \brief A dependent initialization, which could not be
373 /// type-checked due to the presence of dependent types or
374 /// dependently-type expressions.
375 DependentSequence,
376
Douglas Gregor4a520a22009-12-14 17:27:33 +0000377 /// \brief A user-defined conversion sequence.
378 UserDefinedConversion,
379
Douglas Gregor51c56d62009-12-14 20:49:26 +0000380 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000381 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000382
Douglas Gregor20093b42009-12-09 23:02:17 +0000383 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000384 ReferenceBinding,
385
386 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000387 ListInitialization,
388
389 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000390 ZeroInitialization,
391
392 /// \brief No initialization required.
393 NoInitialization,
394
395 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000396 StandardConversion,
397
398 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000399 CAssignment,
400
401 /// \brief String initialization
402 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000403 };
404
405 /// \brief Describes the kind of a particular step in an initialization
406 /// sequence.
407 enum StepKind {
408 /// \brief Resolve the address of an overloaded function to a specific
409 /// function declaration.
410 SK_ResolveAddressOfOverloadedFunction,
411 /// \brief Perform a derived-to-base cast, producing an rvalue.
412 SK_CastDerivedToBaseRValue,
413 /// \brief Perform a derived-to-base cast, producing an lvalue.
414 SK_CastDerivedToBaseLValue,
415 /// \brief Reference binding to an lvalue.
416 SK_BindReference,
417 /// \brief Reference binding to a temporary.
418 SK_BindReferenceToTemporary,
419 /// \brief Perform a user-defined conversion, either via a conversion
420 /// function or via a constructor.
421 SK_UserConversion,
422 /// \brief Perform a qualification conversion, producing an rvalue.
423 SK_QualificationConversionRValue,
424 /// \brief Perform a qualification conversion, producing an lvalue.
425 SK_QualificationConversionLValue,
426 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000427 SK_ConversionSequence,
428 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000429 SK_ListInitialization,
430 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000431 SK_ConstructorInitialization,
432 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000433 SK_ZeroInitialization,
434 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000435 SK_CAssignment,
436 /// \brief Initialization by string
437 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000438 };
439
440 /// \brief A single step in the initialization sequence.
441 class Step {
442 public:
443 /// \brief The kind of conversion or initialization step we are taking.
444 StepKind Kind;
445
446 // \brief The type that results from this initialization.
447 QualType Type;
448
449 union {
450 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
451 /// SK_UserConversion, the function that the expression should be
452 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000453 ///
454 /// Always a FunctionDecl.
455 /// For conversion decls, the naming class is the source type.
456 /// For construct decls, the naming class is the target type.
457 DeclAccessPair Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000458
459 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
460 /// sequence
461 ImplicitConversionSequence *ICS;
462 };
463
464 void Destroy();
465 };
466
467private:
468 /// \brief The kind of initialization sequence computed.
469 enum SequenceKind SequenceKind;
470
471 /// \brief Steps taken by this initialization.
472 llvm::SmallVector<Step, 4> Steps;
473
474public:
475 /// \brief Describes why initialization failed.
476 enum FailureKind {
477 /// \brief Too many initializers provided for a reference.
478 FK_TooManyInitsForReference,
479 /// \brief Array must be initialized with an initializer list.
480 FK_ArrayNeedsInitList,
481 /// \brief Array must be initialized with an initializer list or a
482 /// string literal.
483 FK_ArrayNeedsInitListOrStringLiteral,
484 /// \brief Cannot resolve the address of an overloaded function.
485 FK_AddressOfOverloadFailed,
486 /// \brief Overloading due to reference initialization failed.
487 FK_ReferenceInitOverloadFailed,
488 /// \brief Non-const lvalue reference binding to a temporary.
489 FK_NonConstLValueReferenceBindingToTemporary,
490 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
491 /// type.
492 FK_NonConstLValueReferenceBindingToUnrelated,
493 /// \brief Rvalue reference binding to an lvalue.
494 FK_RValueReferenceBindingToLValue,
495 /// \brief Reference binding drops qualifiers.
496 FK_ReferenceInitDropsQualifiers,
497 /// \brief Reference binding failed.
498 FK_ReferenceInitFailed,
499 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000500 FK_ConversionFailed,
501 /// \brief Too many initializers for scalar
502 FK_TooManyInitsForScalar,
503 /// \brief Reference initialization from an initializer list
504 FK_ReferenceBindingToInitList,
505 /// \brief Initialization of some unused destination type with an
506 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000507 FK_InitListBadDestinationType,
508 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000509 FK_UserConversionOverloadFailed,
510 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000511 FK_ConstructorOverloadFailed,
512 /// \brief Default-initialization of a 'const' object.
513 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000514 };
515
516private:
517 /// \brief The reason why initialization failued.
518 FailureKind Failure;
519
520 /// \brief The failed result of overload resolution.
521 OverloadingResult FailedOverloadResult;
522
523 /// \brief The candidate set created when initialization failed.
524 OverloadCandidateSet FailedCandidateSet;
525
526public:
527 /// \brief Try to perform initialization of the given entity, creating a
528 /// record of the steps required to perform the initialization.
529 ///
530 /// The generated initialization sequence will either contain enough
531 /// information to diagnose
532 ///
533 /// \param S the semantic analysis object.
534 ///
535 /// \param Entity the entity being initialized.
536 ///
537 /// \param Kind the kind of initialization being performed.
538 ///
539 /// \param Args the argument(s) provided for initialization.
540 ///
541 /// \param NumArgs the number of arguments provided for initialization.
542 InitializationSequence(Sema &S,
543 const InitializedEntity &Entity,
544 const InitializationKind &Kind,
545 Expr **Args,
546 unsigned NumArgs);
547
548 ~InitializationSequence();
549
550 /// \brief Perform the actual initialization of the given entity based on
551 /// the computed initialization sequence.
552 ///
553 /// \param S the semantic analysis object.
554 ///
555 /// \param Entity the entity being initialized.
556 ///
557 /// \param Kind the kind of initialization being performed.
558 ///
559 /// \param Args the argument(s) provided for initialization, ownership of
560 /// which is transfered into the routine.
561 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000562 /// \param ResultType if non-NULL, will be set to the type of the
563 /// initialized object, which is the type of the declaration in most
564 /// cases. However, when the initialized object is a variable of
565 /// incomplete array type and the initializer is an initializer
566 /// list, this type will be set to the completed array type.
567 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000568 /// \returns an expression that performs the actual object initialization, if
569 /// the initialization is well-formed. Otherwise, emits diagnostics
570 /// and returns an invalid expression.
571 Action::OwningExprResult Perform(Sema &S,
572 const InitializedEntity &Entity,
573 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000574 Action::MultiExprArg Args,
575 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000576
577 /// \brief Diagnose an potentially-invalid initialization sequence.
578 ///
579 /// \returns true if the initialization sequence was ill-formed,
580 /// false otherwise.
581 bool Diagnose(Sema &S,
582 const InitializedEntity &Entity,
583 const InitializationKind &Kind,
584 Expr **Args, unsigned NumArgs);
585
586 /// \brief Determine the kind of initialization sequence computed.
587 enum SequenceKind getKind() const { return SequenceKind; }
588
589 /// \brief Set the kind of sequence computed.
590 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
591
592 /// \brief Determine whether the initialization sequence is valid.
593 operator bool() const { return SequenceKind != FailedSequence; }
594
595 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
596 step_iterator step_begin() const { return Steps.begin(); }
597 step_iterator step_end() const { return Steps.end(); }
598
599 /// \brief Add a new step in the initialization that resolves the address
600 /// of an overloaded function to a specific function declaration.
601 ///
602 /// \param Function the function to which the overloaded function reference
603 /// resolves.
604 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
605
606 /// \brief Add a new step in the initialization that performs a derived-to-
607 /// base cast.
608 ///
609 /// \param BaseType the base type to which we will be casting.
610 ///
611 /// \param IsLValue true if the result of this cast will be treated as
612 /// an lvalue.
613 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
614
615 /// \brief Add a new step binding a reference to an object.
616 ///
617 /// \param BindingTemporary true if we are binding a reference to a temporary
618 /// object (thereby extending its lifetime); false if we are binding to an
619 /// lvalue or an lvalue treated as an rvalue.
620 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
621
622 /// \brief Add a new step invoking a conversion function, which is either
623 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000624 void AddUserConversionStep(FunctionDecl *Function,
625 AccessSpecifier Access,
626 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000627
628 /// \brief Add a new step that performs a qualification conversion to the
629 /// given type.
630 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
631
632 /// \brief Add a new step that applies an implicit conversion sequence.
633 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
634 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000635
636 /// \brief Add a list-initialiation step
637 void AddListInitializationStep(QualType T);
638
Douglas Gregor71d17402009-12-15 00:01:57 +0000639 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000640 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000641 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000642 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000643
644 /// \brief Add a zero-initialization step.
645 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000646
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000647 /// \brief Add a C assignment step.
648 //
649 // FIXME: It isn't clear whether this should ever be needed;
650 // ideally, we would handle everything needed in C in the common
651 // path. However, that isn't the case yet.
652 void AddCAssignmentStep(QualType T);
653
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000654 /// \brief Add a string init step.
655 void AddStringInitStep(QualType T);
656
Douglas Gregor20093b42009-12-09 23:02:17 +0000657 /// \brief Note that this initialization sequence failed.
658 void SetFailed(FailureKind Failure) {
659 SequenceKind = FailedSequence;
660 this->Failure = Failure;
661 }
662
663 /// \brief Note that this initialization sequence failed due to failed
664 /// overload resolution.
665 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
666
667 /// \brief Retrieve a reference to the candidate set when overload
668 /// resolution fails.
669 OverloadCandidateSet &getFailedCandidateSet() {
670 return FailedCandidateSet;
671 }
672
673 /// \brief Determine why initialization failed.
674 FailureKind getFailureKind() const {
675 assert(getKind() == FailedSequence && "Not an initialization failure!");
676 return Failure;
677 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000678
679 /// \brief Dump a representation of this initialization sequence to
680 /// the given stream, for debugging purposes.
681 void dump(llvm::raw_ostream &OS) const;
682
683 /// \brief Dump a representation of this initialization sequence to
684 /// standard error, for debugging purposes.
685 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000686};
687
688} // end namespace clang
689
690#endif // LLVM_CLANG_SEMA_INIT_H