blob: 0b72fd4532d9db2541c7283d94436378c8b5e45c [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
69
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
98 /// \brief When Kind = EK_ArrayOrVectorElement, the index of the
99 /// array or vector element being initialized.
100 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 Gregor20093b42009-12-09 23:02:17 +0000204 /// \brief Determine the location of the 'return' keyword when initializing
205 /// the result of a function call.
206 SourceLocation getReturnLoc() const {
207 assert(getKind() == EK_Result && "No 'return' location!");
208 return SourceLocation::getFromRawEncoding(Location);
209 }
210
211 /// \brief Determine the location of the 'throw' keyword when initializing
212 /// an exception object.
213 SourceLocation getThrowLoc() const {
214 assert(getKind() == EK_Exception && "No 'throw' location!");
215 return SourceLocation::getFromRawEncoding(Location);
216 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000217
218 /// \brief If this is already the initializer for an array or vector
219 /// element, sets the element index.
220 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000221 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000222 this->Index = Index;
223 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000224};
225
226/// \brief Describes the kind of initialization being performed, along with
227/// location information for tokens related to the initialization (equal sign,
228/// parentheses).
229class InitializationKind {
230public:
231 /// \brief The kind of initialization being performed.
232 enum InitKind {
233 IK_Direct, ///< Direct initialization
234 IK_Copy, ///< Copy initialization
235 IK_Default, ///< Default initialization
236 IK_Value ///< Value initialization
237 };
238
239private:
240 /// \brief The kind of initialization that we're storing.
241 enum StoredInitKind {
242 SIK_Direct = IK_Direct, ///< Direct initialization
243 SIK_Copy = IK_Copy, ///< Copy initialization
244 SIK_Default = IK_Default, ///< Default initialization
245 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000246 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000247 SIK_DirectCast, ///< Direct initialization due to a cast
248 /// \brief Direct initialization due to a C-style or functional cast.
249 SIK_DirectCStyleOrFunctionalCast
250 };
251
252 /// \brief The kind of initialization being performed.
253 StoredInitKind Kind;
254
255 /// \brief The source locations involved in the initialization.
256 SourceLocation Locations[3];
257
258 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
259 SourceLocation Loc2, SourceLocation Loc3)
260 : Kind(Kind)
261 {
262 Locations[0] = Loc1;
263 Locations[1] = Loc2;
264 Locations[2] = Loc3;
265 }
266
267public:
268 /// \brief Create a direct initialization.
269 static InitializationKind CreateDirect(SourceLocation InitLoc,
270 SourceLocation LParenLoc,
271 SourceLocation RParenLoc) {
272 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
273 }
274
275 /// \brief Create a direct initialization due to a cast.
276 static InitializationKind CreateCast(SourceRange TypeRange,
277 bool IsCStyleCast) {
278 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
279 : SIK_DirectCast,
280 TypeRange.getBegin(), TypeRange.getBegin(),
281 TypeRange.getEnd());
282 }
283
284 /// \brief Create a copy initialization.
285 static InitializationKind CreateCopy(SourceLocation InitLoc,
286 SourceLocation EqualLoc) {
287 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
288 }
289
290 /// \brief Create a default initialization.
291 static InitializationKind CreateDefault(SourceLocation InitLoc) {
292 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
293 }
294
295 /// \brief Create a value initialization.
296 static InitializationKind CreateValue(SourceLocation InitLoc,
297 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000298 SourceLocation RParenLoc,
299 bool isImplicit = false) {
300 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
301 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000302 }
303
304 /// \brief Determine the initialization kind.
305 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000306 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000307 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000308 if (Kind == SIK_ImplicitValue)
309 return IK_Value;
310
Douglas Gregor20093b42009-12-09 23:02:17 +0000311 return (InitKind)Kind;
312 }
313
314 /// \brief Determine whether this initialization is an explicit cast.
315 bool isExplicitCast() const {
316 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
317 }
318
319 /// \brief Determine whether this initialization is a C-style cast.
320 bool isCStyleOrFunctionalCast() const {
321 return Kind == SIK_DirectCStyleOrFunctionalCast;
322 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000323
324 /// \brief Determine whether this initialization is an implicit
325 /// value-initialization, e.g., as occurs during aggregate
326 /// initialization.
327 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
328
Douglas Gregor20093b42009-12-09 23:02:17 +0000329 /// \brief Retrieve the location at which initialization is occurring.
330 SourceLocation getLocation() const { return Locations[0]; }
331
332 /// \brief Retrieve the source range that covers the initialization.
333 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000334 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000335 }
336
337 /// \brief Retrieve the location of the equal sign for copy initialization
338 /// (if present).
339 SourceLocation getEqualLoc() const {
340 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
341 return Locations[1];
342 }
343
344 /// \brief Retrieve the source range containing the locations of the open
345 /// and closing parentheses for value and direct initializations.
346 SourceRange getParenRange() const {
347 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
348 "Only direct- and value-initialization have parentheses");
349 return SourceRange(Locations[1], Locations[2]);
350 }
351};
352
353/// \brief Describes the sequence of initializations required to initialize
354/// a given object or reference with a set of arguments.
355class InitializationSequence {
356public:
357 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000358 ///
359 /// FIXME: Much of this information is in the initialization steps... why is
360 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000361 enum SequenceKind {
362 /// \brief A failed initialization sequence. The failure kind tells what
363 /// happened.
364 FailedSequence = 0,
365
366 /// \brief A dependent initialization, which could not be
367 /// type-checked due to the presence of dependent types or
368 /// dependently-type expressions.
369 DependentSequence,
370
Douglas Gregor4a520a22009-12-14 17:27:33 +0000371 /// \brief A user-defined conversion sequence.
372 UserDefinedConversion,
373
Douglas Gregor51c56d62009-12-14 20:49:26 +0000374 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000375 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000376
Douglas Gregor20093b42009-12-09 23:02:17 +0000377 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000378 ReferenceBinding,
379
380 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000381 ListInitialization,
382
383 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000384 ZeroInitialization,
385
386 /// \brief No initialization required.
387 NoInitialization,
388
389 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000390 StandardConversion,
391
392 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000393 CAssignment,
394
395 /// \brief String initialization
396 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000397 };
398
399 /// \brief Describes the kind of a particular step in an initialization
400 /// sequence.
401 enum StepKind {
402 /// \brief Resolve the address of an overloaded function to a specific
403 /// function declaration.
404 SK_ResolveAddressOfOverloadedFunction,
405 /// \brief Perform a derived-to-base cast, producing an rvalue.
406 SK_CastDerivedToBaseRValue,
407 /// \brief Perform a derived-to-base cast, producing an lvalue.
408 SK_CastDerivedToBaseLValue,
409 /// \brief Reference binding to an lvalue.
410 SK_BindReference,
411 /// \brief Reference binding to a temporary.
412 SK_BindReferenceToTemporary,
413 /// \brief Perform a user-defined conversion, either via a conversion
414 /// function or via a constructor.
415 SK_UserConversion,
416 /// \brief Perform a qualification conversion, producing an rvalue.
417 SK_QualificationConversionRValue,
418 /// \brief Perform a qualification conversion, producing an lvalue.
419 SK_QualificationConversionLValue,
420 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000421 SK_ConversionSequence,
422 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000423 SK_ListInitialization,
424 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000425 SK_ConstructorInitialization,
426 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000427 SK_ZeroInitialization,
428 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000429 SK_CAssignment,
430 /// \brief Initialization by string
431 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000432 };
433
434 /// \brief A single step in the initialization sequence.
435 class Step {
436 public:
437 /// \brief The kind of conversion or initialization step we are taking.
438 StepKind Kind;
439
440 // \brief The type that results from this initialization.
441 QualType Type;
442
443 union {
444 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
445 /// SK_UserConversion, the function that the expression should be
446 /// resolved to or the conversion function to call, respectively.
447 FunctionDecl *Function;
448
449 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
450 /// sequence
451 ImplicitConversionSequence *ICS;
452 };
453
454 void Destroy();
455 };
456
457private:
458 /// \brief The kind of initialization sequence computed.
459 enum SequenceKind SequenceKind;
460
461 /// \brief Steps taken by this initialization.
462 llvm::SmallVector<Step, 4> Steps;
463
464public:
465 /// \brief Describes why initialization failed.
466 enum FailureKind {
467 /// \brief Too many initializers provided for a reference.
468 FK_TooManyInitsForReference,
469 /// \brief Array must be initialized with an initializer list.
470 FK_ArrayNeedsInitList,
471 /// \brief Array must be initialized with an initializer list or a
472 /// string literal.
473 FK_ArrayNeedsInitListOrStringLiteral,
474 /// \brief Cannot resolve the address of an overloaded function.
475 FK_AddressOfOverloadFailed,
476 /// \brief Overloading due to reference initialization failed.
477 FK_ReferenceInitOverloadFailed,
478 /// \brief Non-const lvalue reference binding to a temporary.
479 FK_NonConstLValueReferenceBindingToTemporary,
480 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
481 /// type.
482 FK_NonConstLValueReferenceBindingToUnrelated,
483 /// \brief Rvalue reference binding to an lvalue.
484 FK_RValueReferenceBindingToLValue,
485 /// \brief Reference binding drops qualifiers.
486 FK_ReferenceInitDropsQualifiers,
487 /// \brief Reference binding failed.
488 FK_ReferenceInitFailed,
489 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000490 FK_ConversionFailed,
491 /// \brief Too many initializers for scalar
492 FK_TooManyInitsForScalar,
493 /// \brief Reference initialization from an initializer list
494 FK_ReferenceBindingToInitList,
495 /// \brief Initialization of some unused destination type with an
496 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000497 FK_InitListBadDestinationType,
498 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000499 FK_UserConversionOverloadFailed,
500 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000501 FK_ConstructorOverloadFailed,
502 /// \brief Default-initialization of a 'const' object.
503 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000504 };
505
506private:
507 /// \brief The reason why initialization failued.
508 FailureKind Failure;
509
510 /// \brief The failed result of overload resolution.
511 OverloadingResult FailedOverloadResult;
512
513 /// \brief The candidate set created when initialization failed.
514 OverloadCandidateSet FailedCandidateSet;
515
516public:
517 /// \brief Try to perform initialization of the given entity, creating a
518 /// record of the steps required to perform the initialization.
519 ///
520 /// The generated initialization sequence will either contain enough
521 /// information to diagnose
522 ///
523 /// \param S the semantic analysis object.
524 ///
525 /// \param Entity the entity being initialized.
526 ///
527 /// \param Kind the kind of initialization being performed.
528 ///
529 /// \param Args the argument(s) provided for initialization.
530 ///
531 /// \param NumArgs the number of arguments provided for initialization.
532 InitializationSequence(Sema &S,
533 const InitializedEntity &Entity,
534 const InitializationKind &Kind,
535 Expr **Args,
536 unsigned NumArgs);
537
538 ~InitializationSequence();
539
540 /// \brief Perform the actual initialization of the given entity based on
541 /// the computed initialization sequence.
542 ///
543 /// \param S the semantic analysis object.
544 ///
545 /// \param Entity the entity being initialized.
546 ///
547 /// \param Kind the kind of initialization being performed.
548 ///
549 /// \param Args the argument(s) provided for initialization, ownership of
550 /// which is transfered into the routine.
551 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000552 /// \param ResultType if non-NULL, will be set to the type of the
553 /// initialized object, which is the type of the declaration in most
554 /// cases. However, when the initialized object is a variable of
555 /// incomplete array type and the initializer is an initializer
556 /// list, this type will be set to the completed array type.
557 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000558 /// \returns an expression that performs the actual object initialization, if
559 /// the initialization is well-formed. Otherwise, emits diagnostics
560 /// and returns an invalid expression.
561 Action::OwningExprResult Perform(Sema &S,
562 const InitializedEntity &Entity,
563 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000564 Action::MultiExprArg Args,
565 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000566
567 /// \brief Diagnose an potentially-invalid initialization sequence.
568 ///
569 /// \returns true if the initialization sequence was ill-formed,
570 /// false otherwise.
571 bool Diagnose(Sema &S,
572 const InitializedEntity &Entity,
573 const InitializationKind &Kind,
574 Expr **Args, unsigned NumArgs);
575
576 /// \brief Determine the kind of initialization sequence computed.
577 enum SequenceKind getKind() const { return SequenceKind; }
578
579 /// \brief Set the kind of sequence computed.
580 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
581
582 /// \brief Determine whether the initialization sequence is valid.
583 operator bool() const { return SequenceKind != FailedSequence; }
584
585 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
586 step_iterator step_begin() const { return Steps.begin(); }
587 step_iterator step_end() const { return Steps.end(); }
588
589 /// \brief Add a new step in the initialization that resolves the address
590 /// of an overloaded function to a specific function declaration.
591 ///
592 /// \param Function the function to which the overloaded function reference
593 /// resolves.
594 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
595
596 /// \brief Add a new step in the initialization that performs a derived-to-
597 /// base cast.
598 ///
599 /// \param BaseType the base type to which we will be casting.
600 ///
601 /// \param IsLValue true if the result of this cast will be treated as
602 /// an lvalue.
603 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
604
605 /// \brief Add a new step binding a reference to an object.
606 ///
607 /// \param BindingTemporary true if we are binding a reference to a temporary
608 /// object (thereby extending its lifetime); false if we are binding to an
609 /// lvalue or an lvalue treated as an rvalue.
610 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
611
612 /// \brief Add a new step invoking a conversion function, which is either
613 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000614 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000615
616 /// \brief Add a new step that performs a qualification conversion to the
617 /// given type.
618 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
619
620 /// \brief Add a new step that applies an implicit conversion sequence.
621 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
622 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000623
624 /// \brief Add a list-initialiation step
625 void AddListInitializationStep(QualType T);
626
Douglas Gregor71d17402009-12-15 00:01:57 +0000627 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000628 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
629 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000630
631 /// \brief Add a zero-initialization step.
632 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000633
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000634 /// \brief Add a C assignment step.
635 //
636 // FIXME: It isn't clear whether this should ever be needed;
637 // ideally, we would handle everything needed in C in the common
638 // path. However, that isn't the case yet.
639 void AddCAssignmentStep(QualType T);
640
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000641 /// \brief Add a string init step.
642 void AddStringInitStep(QualType T);
643
Douglas Gregor20093b42009-12-09 23:02:17 +0000644 /// \brief Note that this initialization sequence failed.
645 void SetFailed(FailureKind Failure) {
646 SequenceKind = FailedSequence;
647 this->Failure = Failure;
648 }
649
650 /// \brief Note that this initialization sequence failed due to failed
651 /// overload resolution.
652 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
653
654 /// \brief Retrieve a reference to the candidate set when overload
655 /// resolution fails.
656 OverloadCandidateSet &getFailedCandidateSet() {
657 return FailedCandidateSet;
658 }
659
660 /// \brief Determine why initialization failed.
661 FailureKind getFailureKind() const {
662 assert(getKind() == FailedSequence && "Not an initialization failure!");
663 return Failure;
664 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000665
666 /// \brief Dump a representation of this initialization sequence to
667 /// the given stream, for debugging purposes.
668 void dump(llvm::raw_ostream &OS) const;
669
670 /// \brief Dump a representation of this initialization sequence to
671 /// standard error, for debugging purposes.
672 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000673};
674
675} // end namespace clang
676
677#endif // LLVM_CLANG_SEMA_INIT_H