blob: 001ba91d546696a50d763bb6b9d6a311b961ba1e [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"
Douglas Gregor20093b42009-12-09 23:02:17 +000018#include "clang/Parse/Action.h"
19#include "clang/Basic/SourceLocation.h"
20#include "llvm/ADT/PointerIntPair.h"
21#include "llvm/ADT/SmallVector.h"
22#include <cassert>
23
Douglas Gregorde4b1d82010-01-29 19:14:02 +000024namespace llvm {
25 class raw_ostream;
26}
27
Douglas Gregor20093b42009-12-09 23:02:17 +000028namespace clang {
29
30class CXXBaseSpecifier;
31class DeclaratorDecl;
32class DeclaratorInfo;
33class FieldDecl;
34class FunctionDecl;
35class ParmVarDecl;
36class Sema;
37class TypeLoc;
38class VarDecl;
39
40/// \brief Describes an entity that is being initialized.
41class InitializedEntity {
42public:
43 /// \brief Specifies the kind of entity being initialized.
44 enum EntityKind {
45 /// \brief The entity being initialized is a variable.
46 EK_Variable,
47 /// \brief The entity being initialized is a function parameter.
48 EK_Parameter,
49 /// \brief The entity being initialized is the result of a function call.
50 EK_Result,
51 /// \brief The entity being initialized is an exception object that
52 /// is being thrown.
53 EK_Exception,
Anders Carlssona729bbb2010-01-23 05:47:27 +000054 /// \brief The entity being initialized is a non-static data member
55 /// subobject.
56 EK_Member,
57 /// \brief The entity being initialized is an element of an array.
58 EK_ArrayElement,
Douglas Gregor18ef5e22009-12-18 05:02:21 +000059 /// \brief The entity being initialized is an object (or array of
60 /// objects) allocated via new.
61 EK_New,
Douglas Gregor20093b42009-12-09 23:02:17 +000062 /// \brief The entity being initialized is a temporary object.
63 EK_Temporary,
64 /// \brief The entity being initialized is a base member subobject.
65 EK_Base,
Anders Carlssond3d824d2010-01-23 04:34:47 +000066 /// \brief The entity being initialized is an element of a vector.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000067 /// or vector.
Anders Carlssond3d824d2010-01-23 04:34:47 +000068 EK_VectorElement
Douglas Gregor20093b42009-12-09 23:02:17 +000069 };
70
71private:
72 /// \brief The kind of entity being initialized.
73 EntityKind Kind;
74
Douglas Gregorcb57fb92009-12-16 06:35:08 +000075 /// \brief If non-NULL, the parent entity in which this
76 /// initialization occurs.
77 const InitializedEntity *Parent;
78
Douglas Gregord6542d82009-12-22 15:35:07 +000079 /// \brief The type of the object or reference being initialized.
80 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000081
82 union {
83 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
84 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
85 DeclaratorDecl *VariableOrMember;
86
Douglas Gregor18ef5e22009-12-18 05:02:21 +000087 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
88 /// location of the 'return', 'throw', or 'new' keyword,
89 /// respectively. When Kind == EK_Temporary, the location where
90 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000091 unsigned Location;
92
93 /// \brief When Kind == EK_Base, the base specifier that provides the
94 /// base class.
95 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000096
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000097 /// \brief When Kind = EK_ArrayElement or EK_VectorElement, the
98 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000099 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000100 };
101
102 InitializedEntity() { }
103
104 /// \brief Create the initialization entity for a variable.
105 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000106 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
107 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000108
109 /// \brief Create the initialization entity for a parameter.
110 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000111 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000112 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000113
Douglas Gregora188ff22009-12-22 16:09:06 +0000114 /// \brief Create the initialization entity for the result of a
115 /// function, throwing an object, performing an explicit cast, or
116 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000117 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
118 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000119
120 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000121 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000122 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
123 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000124
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000125 /// \brief Create the initialization entity for an array element.
126 InitializedEntity(ASTContext &Context, unsigned Index,
127 const InitializedEntity &Parent);
128
Douglas Gregor20093b42009-12-09 23:02:17 +0000129public:
130 /// \brief Create the initialization entity for a variable.
131 static InitializedEntity InitializeVariable(VarDecl *Var) {
132 return InitializedEntity(Var);
133 }
134
135 /// \brief Create the initialization entity for a parameter.
136 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
137 return InitializedEntity(Parm);
138 }
139
Douglas Gregora188ff22009-12-22 16:09:06 +0000140 /// \brief Create the initialization entity for a parameter that is
141 /// only known by its type.
142 static InitializedEntity InitializeParameter(QualType Type) {
143 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
144 }
145
Douglas Gregor20093b42009-12-09 23:02:17 +0000146 /// \brief Create the initialization entity for the result of a function.
147 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000148 QualType Type) {
149 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000150 }
151
152 /// \brief Create the initialization entity for an exception object.
153 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000154 QualType Type) {
155 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000156 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000157
158 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000159 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
160 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000161 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000162
163 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000164 static InitializedEntity InitializeTemporary(QualType Type) {
165 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000166 }
167
168 /// \brief Create the initialization entity for a base class subobject.
169 static InitializedEntity InitializeBase(ASTContext &Context,
170 CXXBaseSpecifier *Base);
171
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000172 /// \brief Create the initialization entity for a member subobject.
173 static InitializedEntity InitializeMember(FieldDecl *Member,
174 const InitializedEntity *Parent = 0) {
175 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000176 }
177
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000178 /// \brief Create the initialization entity for an array element.
179 static InitializedEntity InitializeElement(ASTContext &Context,
180 unsigned Index,
181 const InitializedEntity &Parent) {
182 return InitializedEntity(Context, Index, Parent);
183 }
184
Douglas Gregor20093b42009-12-09 23:02:17 +0000185 /// \brief Determine the kind of initialization.
186 EntityKind getKind() const { return Kind; }
187
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000188 /// \brief Retrieve the parent of the entity being initialized, when
189 /// the initialization itself is occuring within the context of a
190 /// larger initialization.
191 const InitializedEntity *getParent() const { return Parent; }
192
Douglas Gregor20093b42009-12-09 23:02:17 +0000193 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000194 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000195
Douglas Gregor99a2e602009-12-16 01:38:02 +0000196 /// \brief Retrieve the name of the entity being initialized.
197 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000198
199 /// \brief Retrieve the variable, parameter, or field being
200 /// initialized.
201 DeclaratorDecl *getDecl() const;
202
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000203 /// \brief Retrieve the base specifier.
204 CXXBaseSpecifier *getBaseSpecifier() const {
205 assert(getKind() == EK_Base && "Not a base specifier");
206 return Base;
207 }
208
Douglas Gregor20093b42009-12-09 23:02:17 +0000209 /// \brief Determine the location of the 'return' keyword when initializing
210 /// the result of a function call.
211 SourceLocation getReturnLoc() const {
212 assert(getKind() == EK_Result && "No 'return' location!");
213 return SourceLocation::getFromRawEncoding(Location);
214 }
215
216 /// \brief Determine the location of the 'throw' keyword when initializing
217 /// an exception object.
218 SourceLocation getThrowLoc() const {
219 assert(getKind() == EK_Exception && "No 'throw' location!");
220 return SourceLocation::getFromRawEncoding(Location);
221 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000222
223 /// \brief If this is already the initializer for an array or vector
224 /// element, sets the element index.
225 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000226 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000227 this->Index = Index;
228 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000229};
230
231/// \brief Describes the kind of initialization being performed, along with
232/// location information for tokens related to the initialization (equal sign,
233/// parentheses).
234class InitializationKind {
235public:
236 /// \brief The kind of initialization being performed.
237 enum InitKind {
238 IK_Direct, ///< Direct initialization
239 IK_Copy, ///< Copy initialization
240 IK_Default, ///< Default initialization
241 IK_Value ///< Value initialization
242 };
243
244private:
245 /// \brief The kind of initialization that we're storing.
246 enum StoredInitKind {
247 SIK_Direct = IK_Direct, ///< Direct initialization
248 SIK_Copy = IK_Copy, ///< Copy initialization
249 SIK_Default = IK_Default, ///< Default initialization
250 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000251 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000252 SIK_DirectCast, ///< Direct initialization due to a cast
253 /// \brief Direct initialization due to a C-style or functional cast.
254 SIK_DirectCStyleOrFunctionalCast
255 };
256
257 /// \brief The kind of initialization being performed.
258 StoredInitKind Kind;
259
260 /// \brief The source locations involved in the initialization.
261 SourceLocation Locations[3];
262
263 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
264 SourceLocation Loc2, SourceLocation Loc3)
265 : Kind(Kind)
266 {
267 Locations[0] = Loc1;
268 Locations[1] = Loc2;
269 Locations[2] = Loc3;
270 }
271
272public:
273 /// \brief Create a direct initialization.
274 static InitializationKind CreateDirect(SourceLocation InitLoc,
275 SourceLocation LParenLoc,
276 SourceLocation RParenLoc) {
277 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
278 }
279
280 /// \brief Create a direct initialization due to a cast.
281 static InitializationKind CreateCast(SourceRange TypeRange,
282 bool IsCStyleCast) {
283 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
284 : SIK_DirectCast,
285 TypeRange.getBegin(), TypeRange.getBegin(),
286 TypeRange.getEnd());
287 }
288
289 /// \brief Create a copy initialization.
290 static InitializationKind CreateCopy(SourceLocation InitLoc,
291 SourceLocation EqualLoc) {
292 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
293 }
294
295 /// \brief Create a default initialization.
296 static InitializationKind CreateDefault(SourceLocation InitLoc) {
297 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
298 }
299
300 /// \brief Create a value initialization.
301 static InitializationKind CreateValue(SourceLocation InitLoc,
302 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000303 SourceLocation RParenLoc,
304 bool isImplicit = false) {
305 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
306 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000307 }
308
309 /// \brief Determine the initialization kind.
310 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000311 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000312 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000313 if (Kind == SIK_ImplicitValue)
314 return IK_Value;
315
Douglas Gregor20093b42009-12-09 23:02:17 +0000316 return (InitKind)Kind;
317 }
318
319 /// \brief Determine whether this initialization is an explicit cast.
320 bool isExplicitCast() const {
321 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
322 }
323
324 /// \brief Determine whether this initialization is a C-style cast.
325 bool isCStyleOrFunctionalCast() const {
326 return Kind == SIK_DirectCStyleOrFunctionalCast;
327 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000328
329 /// \brief Determine whether this initialization is an implicit
330 /// value-initialization, e.g., as occurs during aggregate
331 /// initialization.
332 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
333
Douglas Gregor20093b42009-12-09 23:02:17 +0000334 /// \brief Retrieve the location at which initialization is occurring.
335 SourceLocation getLocation() const { return Locations[0]; }
336
337 /// \brief Retrieve the source range that covers the initialization.
338 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000339 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000340 }
341
342 /// \brief Retrieve the location of the equal sign for copy initialization
343 /// (if present).
344 SourceLocation getEqualLoc() const {
345 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
346 return Locations[1];
347 }
348
349 /// \brief Retrieve the source range containing the locations of the open
350 /// and closing parentheses for value and direct initializations.
351 SourceRange getParenRange() const {
352 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
353 "Only direct- and value-initialization have parentheses");
354 return SourceRange(Locations[1], Locations[2]);
355 }
356};
357
358/// \brief Describes the sequence of initializations required to initialize
359/// a given object or reference with a set of arguments.
360class InitializationSequence {
361public:
362 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000363 ///
364 /// FIXME: Much of this information is in the initialization steps... why is
365 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000366 enum SequenceKind {
367 /// \brief A failed initialization sequence. The failure kind tells what
368 /// happened.
369 FailedSequence = 0,
370
371 /// \brief A dependent initialization, which could not be
372 /// type-checked due to the presence of dependent types or
373 /// dependently-type expressions.
374 DependentSequence,
375
Douglas Gregor4a520a22009-12-14 17:27:33 +0000376 /// \brief A user-defined conversion sequence.
377 UserDefinedConversion,
378
Douglas Gregor51c56d62009-12-14 20:49:26 +0000379 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000380 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000381
Douglas Gregor20093b42009-12-09 23:02:17 +0000382 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000383 ReferenceBinding,
384
385 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000386 ListInitialization,
387
388 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000389 ZeroInitialization,
390
391 /// \brief No initialization required.
392 NoInitialization,
393
394 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000395 StandardConversion,
396
397 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000398 CAssignment,
399
400 /// \brief String initialization
401 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000402 };
403
404 /// \brief Describes the kind of a particular step in an initialization
405 /// sequence.
406 enum StepKind {
407 /// \brief Resolve the address of an overloaded function to a specific
408 /// function declaration.
409 SK_ResolveAddressOfOverloadedFunction,
410 /// \brief Perform a derived-to-base cast, producing an rvalue.
411 SK_CastDerivedToBaseRValue,
412 /// \brief Perform a derived-to-base cast, producing an lvalue.
413 SK_CastDerivedToBaseLValue,
414 /// \brief Reference binding to an lvalue.
415 SK_BindReference,
416 /// \brief Reference binding to a temporary.
417 SK_BindReferenceToTemporary,
418 /// \brief Perform a user-defined conversion, either via a conversion
419 /// function or via a constructor.
420 SK_UserConversion,
421 /// \brief Perform a qualification conversion, producing an rvalue.
422 SK_QualificationConversionRValue,
423 /// \brief Perform a qualification conversion, producing an lvalue.
424 SK_QualificationConversionLValue,
425 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000426 SK_ConversionSequence,
427 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000428 SK_ListInitialization,
429 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000430 SK_ConstructorInitialization,
431 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000432 SK_ZeroInitialization,
433 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000434 SK_CAssignment,
435 /// \brief Initialization by string
436 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000437 };
438
439 /// \brief A single step in the initialization sequence.
440 class Step {
441 public:
442 /// \brief The kind of conversion or initialization step we are taking.
443 StepKind Kind;
444
445 // \brief The type that results from this initialization.
446 QualType Type;
447
448 union {
449 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
450 /// SK_UserConversion, the function that the expression should be
451 /// resolved to or the conversion function to call, respectively.
452 FunctionDecl *Function;
453
454 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
455 /// sequence
456 ImplicitConversionSequence *ICS;
457 };
458
459 void Destroy();
460 };
461
462private:
463 /// \brief The kind of initialization sequence computed.
464 enum SequenceKind SequenceKind;
465
466 /// \brief Steps taken by this initialization.
467 llvm::SmallVector<Step, 4> Steps;
468
469public:
470 /// \brief Describes why initialization failed.
471 enum FailureKind {
472 /// \brief Too many initializers provided for a reference.
473 FK_TooManyInitsForReference,
474 /// \brief Array must be initialized with an initializer list.
475 FK_ArrayNeedsInitList,
476 /// \brief Array must be initialized with an initializer list or a
477 /// string literal.
478 FK_ArrayNeedsInitListOrStringLiteral,
479 /// \brief Cannot resolve the address of an overloaded function.
480 FK_AddressOfOverloadFailed,
481 /// \brief Overloading due to reference initialization failed.
482 FK_ReferenceInitOverloadFailed,
483 /// \brief Non-const lvalue reference binding to a temporary.
484 FK_NonConstLValueReferenceBindingToTemporary,
485 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
486 /// type.
487 FK_NonConstLValueReferenceBindingToUnrelated,
488 /// \brief Rvalue reference binding to an lvalue.
489 FK_RValueReferenceBindingToLValue,
490 /// \brief Reference binding drops qualifiers.
491 FK_ReferenceInitDropsQualifiers,
492 /// \brief Reference binding failed.
493 FK_ReferenceInitFailed,
494 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000495 FK_ConversionFailed,
496 /// \brief Too many initializers for scalar
497 FK_TooManyInitsForScalar,
498 /// \brief Reference initialization from an initializer list
499 FK_ReferenceBindingToInitList,
500 /// \brief Initialization of some unused destination type with an
501 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000502 FK_InitListBadDestinationType,
503 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000504 FK_UserConversionOverloadFailed,
505 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000506 FK_ConstructorOverloadFailed,
507 /// \brief Default-initialization of a 'const' object.
508 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000509 };
510
511private:
512 /// \brief The reason why initialization failued.
513 FailureKind Failure;
514
515 /// \brief The failed result of overload resolution.
516 OverloadingResult FailedOverloadResult;
517
518 /// \brief The candidate set created when initialization failed.
519 OverloadCandidateSet FailedCandidateSet;
520
521public:
522 /// \brief Try to perform initialization of the given entity, creating a
523 /// record of the steps required to perform the initialization.
524 ///
525 /// The generated initialization sequence will either contain enough
526 /// information to diagnose
527 ///
528 /// \param S the semantic analysis object.
529 ///
530 /// \param Entity the entity being initialized.
531 ///
532 /// \param Kind the kind of initialization being performed.
533 ///
534 /// \param Args the argument(s) provided for initialization.
535 ///
536 /// \param NumArgs the number of arguments provided for initialization.
537 InitializationSequence(Sema &S,
538 const InitializedEntity &Entity,
539 const InitializationKind &Kind,
540 Expr **Args,
541 unsigned NumArgs);
542
543 ~InitializationSequence();
544
545 /// \brief Perform the actual initialization of the given entity based on
546 /// the computed initialization sequence.
547 ///
548 /// \param S the semantic analysis object.
549 ///
550 /// \param Entity the entity being initialized.
551 ///
552 /// \param Kind the kind of initialization being performed.
553 ///
554 /// \param Args the argument(s) provided for initialization, ownership of
555 /// which is transfered into the routine.
556 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000557 /// \param ResultType if non-NULL, will be set to the type of the
558 /// initialized object, which is the type of the declaration in most
559 /// cases. However, when the initialized object is a variable of
560 /// incomplete array type and the initializer is an initializer
561 /// list, this type will be set to the completed array type.
562 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000563 /// \returns an expression that performs the actual object initialization, if
564 /// the initialization is well-formed. Otherwise, emits diagnostics
565 /// and returns an invalid expression.
566 Action::OwningExprResult Perform(Sema &S,
567 const InitializedEntity &Entity,
568 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000569 Action::MultiExprArg Args,
570 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000571
572 /// \brief Diagnose an potentially-invalid initialization sequence.
573 ///
574 /// \returns true if the initialization sequence was ill-formed,
575 /// false otherwise.
576 bool Diagnose(Sema &S,
577 const InitializedEntity &Entity,
578 const InitializationKind &Kind,
579 Expr **Args, unsigned NumArgs);
580
581 /// \brief Determine the kind of initialization sequence computed.
582 enum SequenceKind getKind() const { return SequenceKind; }
583
584 /// \brief Set the kind of sequence computed.
585 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
586
587 /// \brief Determine whether the initialization sequence is valid.
588 operator bool() const { return SequenceKind != FailedSequence; }
589
590 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
591 step_iterator step_begin() const { return Steps.begin(); }
592 step_iterator step_end() const { return Steps.end(); }
593
594 /// \brief Add a new step in the initialization that resolves the address
595 /// of an overloaded function to a specific function declaration.
596 ///
597 /// \param Function the function to which the overloaded function reference
598 /// resolves.
599 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
600
601 /// \brief Add a new step in the initialization that performs a derived-to-
602 /// base cast.
603 ///
604 /// \param BaseType the base type to which we will be casting.
605 ///
606 /// \param IsLValue true if the result of this cast will be treated as
607 /// an lvalue.
608 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
609
610 /// \brief Add a new step binding a reference to an object.
611 ///
612 /// \param BindingTemporary true if we are binding a reference to a temporary
613 /// object (thereby extending its lifetime); false if we are binding to an
614 /// lvalue or an lvalue treated as an rvalue.
615 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
616
617 /// \brief Add a new step invoking a conversion function, which is either
618 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000619 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000620
621 /// \brief Add a new step that performs a qualification conversion to the
622 /// given type.
623 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
624
625 /// \brief Add a new step that applies an implicit conversion sequence.
626 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
627 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000628
629 /// \brief Add a list-initialiation step
630 void AddListInitializationStep(QualType T);
631
Douglas Gregor71d17402009-12-15 00:01:57 +0000632 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000633 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
634 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000635
636 /// \brief Add a zero-initialization step.
637 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000638
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000639 /// \brief Add a C assignment step.
640 //
641 // FIXME: It isn't clear whether this should ever be needed;
642 // ideally, we would handle everything needed in C in the common
643 // path. However, that isn't the case yet.
644 void AddCAssignmentStep(QualType T);
645
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000646 /// \brief Add a string init step.
647 void AddStringInitStep(QualType T);
648
Douglas Gregor20093b42009-12-09 23:02:17 +0000649 /// \brief Note that this initialization sequence failed.
650 void SetFailed(FailureKind Failure) {
651 SequenceKind = FailedSequence;
652 this->Failure = Failure;
653 }
654
655 /// \brief Note that this initialization sequence failed due to failed
656 /// overload resolution.
657 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
658
659 /// \brief Retrieve a reference to the candidate set when overload
660 /// resolution fails.
661 OverloadCandidateSet &getFailedCandidateSet() {
662 return FailedCandidateSet;
663 }
664
665 /// \brief Determine why initialization failed.
666 FailureKind getFailureKind() const {
667 assert(getKind() == FailedSequence && "Not an initialization failure!");
668 return Failure;
669 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000670
671 /// \brief Dump a representation of this initialization sequence to
672 /// the given stream, for debugging purposes.
673 void dump(llvm::raw_ostream &OS) const;
674
675 /// \brief Dump a representation of this initialization sequence to
676 /// standard error, for debugging purposes.
677 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000678};
679
680} // end namespace clang
681
682#endif // LLVM_CLANG_SEMA_INIT_H