blob: 5eb819a6917652b4959a1fc92810a52f9e5cd862 [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
24namespace clang {
25
26class CXXBaseSpecifier;
27class DeclaratorDecl;
28class DeclaratorInfo;
29class FieldDecl;
30class FunctionDecl;
31class ParmVarDecl;
32class Sema;
33class TypeLoc;
34class VarDecl;
35
36/// \brief Describes an entity that is being initialized.
37class InitializedEntity {
38public:
39 /// \brief Specifies the kind of entity being initialized.
40 enum EntityKind {
41 /// \brief The entity being initialized is a variable.
42 EK_Variable,
43 /// \brief The entity being initialized is a function parameter.
44 EK_Parameter,
45 /// \brief The entity being initialized is the result of a function call.
46 EK_Result,
47 /// \brief The entity being initialized is an exception object that
48 /// is being thrown.
49 EK_Exception,
Douglas Gregor18ef5e22009-12-18 05:02:21 +000050 /// \brief The entity being initialized is an object (or array of
51 /// objects) allocated via new.
52 EK_New,
Douglas Gregor20093b42009-12-09 23:02:17 +000053 /// \brief The entity being initialized is a temporary object.
54 EK_Temporary,
55 /// \brief The entity being initialized is a base member subobject.
56 EK_Base,
57 /// \brief The entity being initialized is a non-static data member
58 /// subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000059 EK_Member,
60 /// \brief The entity being initialized is an element of an array
61 /// or vector.
62 EK_ArrayOrVectorElement
Douglas Gregor20093b42009-12-09 23:02:17 +000063 };
64
65private:
66 /// \brief The kind of entity being initialized.
67 EntityKind Kind;
68
Douglas Gregorcb57fb92009-12-16 06:35:08 +000069 /// \brief If non-NULL, the parent entity in which this
70 /// initialization occurs.
71 const InitializedEntity *Parent;
72
Douglas Gregord6542d82009-12-22 15:35:07 +000073 /// \brief The type of the object or reference being initialized.
74 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000075
76 union {
77 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
78 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
79 DeclaratorDecl *VariableOrMember;
80
Douglas Gregor18ef5e22009-12-18 05:02:21 +000081 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
82 /// location of the 'return', 'throw', or 'new' keyword,
83 /// respectively. When Kind == EK_Temporary, the location where
84 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000085 unsigned Location;
86
87 /// \brief When Kind == EK_Base, the base specifier that provides the
88 /// base class.
89 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000090
91 /// \brief When Kind = EK_ArrayOrVectorElement, the index of the
92 /// array or vector element being initialized.
93 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +000094 };
95
96 InitializedEntity() { }
97
98 /// \brief Create the initialization entity for a variable.
99 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000100 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
101 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000102
103 /// \brief Create the initialization entity for a parameter.
104 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000105 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000106 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000107
Douglas Gregora188ff22009-12-22 16:09:06 +0000108 /// \brief Create the initialization entity for the result of a
109 /// function, throwing an object, performing an explicit cast, or
110 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000111 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
112 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000113
114 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000115 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000116 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
117 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000118
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000119 /// \brief Create the initialization entity for an array element.
120 InitializedEntity(ASTContext &Context, unsigned Index,
121 const InitializedEntity &Parent);
122
Douglas Gregor20093b42009-12-09 23:02:17 +0000123public:
124 /// \brief Create the initialization entity for a variable.
125 static InitializedEntity InitializeVariable(VarDecl *Var) {
126 return InitializedEntity(Var);
127 }
128
129 /// \brief Create the initialization entity for a parameter.
130 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
131 return InitializedEntity(Parm);
132 }
133
Douglas Gregora188ff22009-12-22 16:09:06 +0000134 /// \brief Create the initialization entity for a parameter that is
135 /// only known by its type.
136 static InitializedEntity InitializeParameter(QualType Type) {
137 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
138 }
139
Douglas Gregor20093b42009-12-09 23:02:17 +0000140 /// \brief Create the initialization entity for the result of a function.
141 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000142 QualType Type) {
143 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000144 }
145
146 /// \brief Create the initialization entity for an exception object.
147 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000148 QualType Type) {
149 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000150 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000151
152 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000153 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
154 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000155 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000156
157 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000158 static InitializedEntity InitializeTemporary(QualType Type) {
159 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000160 }
161
162 /// \brief Create the initialization entity for a base class subobject.
163 static InitializedEntity InitializeBase(ASTContext &Context,
164 CXXBaseSpecifier *Base);
165
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000166 /// \brief Create the initialization entity for a member subobject.
167 static InitializedEntity InitializeMember(FieldDecl *Member,
168 const InitializedEntity *Parent = 0) {
169 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000170 }
171
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000172 /// \brief Create the initialization entity for an array element.
173 static InitializedEntity InitializeElement(ASTContext &Context,
174 unsigned Index,
175 const InitializedEntity &Parent) {
176 return InitializedEntity(Context, Index, Parent);
177 }
178
Douglas Gregor20093b42009-12-09 23:02:17 +0000179 /// \brief Determine the kind of initialization.
180 EntityKind getKind() const { return Kind; }
181
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000182 /// \brief Retrieve the parent of the entity being initialized, when
183 /// the initialization itself is occuring within the context of a
184 /// larger initialization.
185 const InitializedEntity *getParent() const { return Parent; }
186
Douglas Gregor20093b42009-12-09 23:02:17 +0000187 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000188 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000189
Douglas Gregor99a2e602009-12-16 01:38:02 +0000190 /// \brief Retrieve the name of the entity being initialized.
191 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000192
193 /// \brief Retrieve the variable, parameter, or field being
194 /// initialized.
195 DeclaratorDecl *getDecl() const;
196
Douglas Gregor20093b42009-12-09 23:02:17 +0000197 /// \brief Determine the location of the 'return' keyword when initializing
198 /// the result of a function call.
199 SourceLocation getReturnLoc() const {
200 assert(getKind() == EK_Result && "No 'return' location!");
201 return SourceLocation::getFromRawEncoding(Location);
202 }
203
204 /// \brief Determine the location of the 'throw' keyword when initializing
205 /// an exception object.
206 SourceLocation getThrowLoc() const {
207 assert(getKind() == EK_Exception && "No 'throw' location!");
208 return SourceLocation::getFromRawEncoding(Location);
209 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000210
211 /// \brief If this is already the initializer for an array or vector
212 /// element, sets the element index.
213 void setElementIndex(unsigned Index) {
214 assert(getKind() == EK_ArrayOrVectorElement);
215 this->Index = Index;
216 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000217};
218
219/// \brief Describes the kind of initialization being performed, along with
220/// location information for tokens related to the initialization (equal sign,
221/// parentheses).
222class InitializationKind {
223public:
224 /// \brief The kind of initialization being performed.
225 enum InitKind {
226 IK_Direct, ///< Direct initialization
227 IK_Copy, ///< Copy initialization
228 IK_Default, ///< Default initialization
229 IK_Value ///< Value initialization
230 };
231
232private:
233 /// \brief The kind of initialization that we're storing.
234 enum StoredInitKind {
235 SIK_Direct = IK_Direct, ///< Direct initialization
236 SIK_Copy = IK_Copy, ///< Copy initialization
237 SIK_Default = IK_Default, ///< Default initialization
238 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000239 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000240 SIK_DirectCast, ///< Direct initialization due to a cast
241 /// \brief Direct initialization due to a C-style or functional cast.
242 SIK_DirectCStyleOrFunctionalCast
243 };
244
245 /// \brief The kind of initialization being performed.
246 StoredInitKind Kind;
247
248 /// \brief The source locations involved in the initialization.
249 SourceLocation Locations[3];
250
251 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
252 SourceLocation Loc2, SourceLocation Loc3)
253 : Kind(Kind)
254 {
255 Locations[0] = Loc1;
256 Locations[1] = Loc2;
257 Locations[2] = Loc3;
258 }
259
260public:
261 /// \brief Create a direct initialization.
262 static InitializationKind CreateDirect(SourceLocation InitLoc,
263 SourceLocation LParenLoc,
264 SourceLocation RParenLoc) {
265 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
266 }
267
268 /// \brief Create a direct initialization due to a cast.
269 static InitializationKind CreateCast(SourceRange TypeRange,
270 bool IsCStyleCast) {
271 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
272 : SIK_DirectCast,
273 TypeRange.getBegin(), TypeRange.getBegin(),
274 TypeRange.getEnd());
275 }
276
277 /// \brief Create a copy initialization.
278 static InitializationKind CreateCopy(SourceLocation InitLoc,
279 SourceLocation EqualLoc) {
280 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
281 }
282
283 /// \brief Create a default initialization.
284 static InitializationKind CreateDefault(SourceLocation InitLoc) {
285 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
286 }
287
288 /// \brief Create a value initialization.
289 static InitializationKind CreateValue(SourceLocation InitLoc,
290 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000291 SourceLocation RParenLoc,
292 bool isImplicit = false) {
293 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
294 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000295 }
296
297 /// \brief Determine the initialization kind.
298 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000299 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000300 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000301 if (Kind == SIK_ImplicitValue)
302 return IK_Value;
303
Douglas Gregor20093b42009-12-09 23:02:17 +0000304 return (InitKind)Kind;
305 }
306
307 /// \brief Determine whether this initialization is an explicit cast.
308 bool isExplicitCast() const {
309 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
310 }
311
312 /// \brief Determine whether this initialization is a C-style cast.
313 bool isCStyleOrFunctionalCast() const {
314 return Kind == SIK_DirectCStyleOrFunctionalCast;
315 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000316
317 /// \brief Determine whether this initialization is an implicit
318 /// value-initialization, e.g., as occurs during aggregate
319 /// initialization.
320 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
321
Douglas Gregor20093b42009-12-09 23:02:17 +0000322 /// \brief Retrieve the location at which initialization is occurring.
323 SourceLocation getLocation() const { return Locations[0]; }
324
325 /// \brief Retrieve the source range that covers the initialization.
326 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000327 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000328 }
329
330 /// \brief Retrieve the location of the equal sign for copy initialization
331 /// (if present).
332 SourceLocation getEqualLoc() const {
333 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
334 return Locations[1];
335 }
336
337 /// \brief Retrieve the source range containing the locations of the open
338 /// and closing parentheses for value and direct initializations.
339 SourceRange getParenRange() const {
340 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
341 "Only direct- and value-initialization have parentheses");
342 return SourceRange(Locations[1], Locations[2]);
343 }
344};
345
346/// \brief Describes the sequence of initializations required to initialize
347/// a given object or reference with a set of arguments.
348class InitializationSequence {
349public:
350 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000351 ///
352 /// FIXME: Much of this information is in the initialization steps... why is
353 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000354 enum SequenceKind {
355 /// \brief A failed initialization sequence. The failure kind tells what
356 /// happened.
357 FailedSequence = 0,
358
359 /// \brief A dependent initialization, which could not be
360 /// type-checked due to the presence of dependent types or
361 /// dependently-type expressions.
362 DependentSequence,
363
Douglas Gregor4a520a22009-12-14 17:27:33 +0000364 /// \brief A user-defined conversion sequence.
365 UserDefinedConversion,
366
Douglas Gregor51c56d62009-12-14 20:49:26 +0000367 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000368 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000369
Douglas Gregor20093b42009-12-09 23:02:17 +0000370 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000371 ReferenceBinding,
372
373 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000374 ListInitialization,
375
376 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000377 ZeroInitialization,
378
379 /// \brief No initialization required.
380 NoInitialization,
381
382 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000383 StandardConversion,
384
385 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000386 CAssignment,
387
388 /// \brief String initialization
389 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000390 };
391
392 /// \brief Describes the kind of a particular step in an initialization
393 /// sequence.
394 enum StepKind {
395 /// \brief Resolve the address of an overloaded function to a specific
396 /// function declaration.
397 SK_ResolveAddressOfOverloadedFunction,
398 /// \brief Perform a derived-to-base cast, producing an rvalue.
399 SK_CastDerivedToBaseRValue,
400 /// \brief Perform a derived-to-base cast, producing an lvalue.
401 SK_CastDerivedToBaseLValue,
402 /// \brief Reference binding to an lvalue.
403 SK_BindReference,
404 /// \brief Reference binding to a temporary.
405 SK_BindReferenceToTemporary,
406 /// \brief Perform a user-defined conversion, either via a conversion
407 /// function or via a constructor.
408 SK_UserConversion,
409 /// \brief Perform a qualification conversion, producing an rvalue.
410 SK_QualificationConversionRValue,
411 /// \brief Perform a qualification conversion, producing an lvalue.
412 SK_QualificationConversionLValue,
413 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000414 SK_ConversionSequence,
415 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000416 SK_ListInitialization,
417 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000418 SK_ConstructorInitialization,
419 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000420 SK_ZeroInitialization,
421 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000422 SK_CAssignment,
423 /// \brief Initialization by string
424 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000425 };
426
427 /// \brief A single step in the initialization sequence.
428 class Step {
429 public:
430 /// \brief The kind of conversion or initialization step we are taking.
431 StepKind Kind;
432
433 // \brief The type that results from this initialization.
434 QualType Type;
435
436 union {
437 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
438 /// SK_UserConversion, the function that the expression should be
439 /// resolved to or the conversion function to call, respectively.
440 FunctionDecl *Function;
441
442 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
443 /// sequence
444 ImplicitConversionSequence *ICS;
445 };
446
447 void Destroy();
448 };
449
450private:
451 /// \brief The kind of initialization sequence computed.
452 enum SequenceKind SequenceKind;
453
454 /// \brief Steps taken by this initialization.
455 llvm::SmallVector<Step, 4> Steps;
456
457public:
458 /// \brief Describes why initialization failed.
459 enum FailureKind {
460 /// \brief Too many initializers provided for a reference.
461 FK_TooManyInitsForReference,
462 /// \brief Array must be initialized with an initializer list.
463 FK_ArrayNeedsInitList,
464 /// \brief Array must be initialized with an initializer list or a
465 /// string literal.
466 FK_ArrayNeedsInitListOrStringLiteral,
467 /// \brief Cannot resolve the address of an overloaded function.
468 FK_AddressOfOverloadFailed,
469 /// \brief Overloading due to reference initialization failed.
470 FK_ReferenceInitOverloadFailed,
471 /// \brief Non-const lvalue reference binding to a temporary.
472 FK_NonConstLValueReferenceBindingToTemporary,
473 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
474 /// type.
475 FK_NonConstLValueReferenceBindingToUnrelated,
476 /// \brief Rvalue reference binding to an lvalue.
477 FK_RValueReferenceBindingToLValue,
478 /// \brief Reference binding drops qualifiers.
479 FK_ReferenceInitDropsQualifiers,
480 /// \brief Reference binding failed.
481 FK_ReferenceInitFailed,
482 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000483 FK_ConversionFailed,
484 /// \brief Too many initializers for scalar
485 FK_TooManyInitsForScalar,
486 /// \brief Reference initialization from an initializer list
487 FK_ReferenceBindingToInitList,
488 /// \brief Initialization of some unused destination type with an
489 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000490 FK_InitListBadDestinationType,
491 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000492 FK_UserConversionOverloadFailed,
493 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000494 FK_ConstructorOverloadFailed,
495 /// \brief Default-initialization of a 'const' object.
496 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000497 };
498
499private:
500 /// \brief The reason why initialization failued.
501 FailureKind Failure;
502
503 /// \brief The failed result of overload resolution.
504 OverloadingResult FailedOverloadResult;
505
506 /// \brief The candidate set created when initialization failed.
507 OverloadCandidateSet FailedCandidateSet;
508
509public:
510 /// \brief Try to perform initialization of the given entity, creating a
511 /// record of the steps required to perform the initialization.
512 ///
513 /// The generated initialization sequence will either contain enough
514 /// information to diagnose
515 ///
516 /// \param S the semantic analysis object.
517 ///
518 /// \param Entity the entity being initialized.
519 ///
520 /// \param Kind the kind of initialization being performed.
521 ///
522 /// \param Args the argument(s) provided for initialization.
523 ///
524 /// \param NumArgs the number of arguments provided for initialization.
525 InitializationSequence(Sema &S,
526 const InitializedEntity &Entity,
527 const InitializationKind &Kind,
528 Expr **Args,
529 unsigned NumArgs);
530
531 ~InitializationSequence();
532
533 /// \brief Perform the actual initialization of the given entity based on
534 /// the computed initialization sequence.
535 ///
536 /// \param S the semantic analysis object.
537 ///
538 /// \param Entity the entity being initialized.
539 ///
540 /// \param Kind the kind of initialization being performed.
541 ///
542 /// \param Args the argument(s) provided for initialization, ownership of
543 /// which is transfered into the routine.
544 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000545 /// \param ResultType if non-NULL, will be set to the type of the
546 /// initialized object, which is the type of the declaration in most
547 /// cases. However, when the initialized object is a variable of
548 /// incomplete array type and the initializer is an initializer
549 /// list, this type will be set to the completed array type.
550 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000551 /// \returns an expression that performs the actual object initialization, if
552 /// the initialization is well-formed. Otherwise, emits diagnostics
553 /// and returns an invalid expression.
554 Action::OwningExprResult Perform(Sema &S,
555 const InitializedEntity &Entity,
556 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000557 Action::MultiExprArg Args,
558 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000559
560 /// \brief Diagnose an potentially-invalid initialization sequence.
561 ///
562 /// \returns true if the initialization sequence was ill-formed,
563 /// false otherwise.
564 bool Diagnose(Sema &S,
565 const InitializedEntity &Entity,
566 const InitializationKind &Kind,
567 Expr **Args, unsigned NumArgs);
568
569 /// \brief Determine the kind of initialization sequence computed.
570 enum SequenceKind getKind() const { return SequenceKind; }
571
572 /// \brief Set the kind of sequence computed.
573 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
574
575 /// \brief Determine whether the initialization sequence is valid.
576 operator bool() const { return SequenceKind != FailedSequence; }
577
578 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
579 step_iterator step_begin() const { return Steps.begin(); }
580 step_iterator step_end() const { return Steps.end(); }
581
582 /// \brief Add a new step in the initialization that resolves the address
583 /// of an overloaded function to a specific function declaration.
584 ///
585 /// \param Function the function to which the overloaded function reference
586 /// resolves.
587 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
588
589 /// \brief Add a new step in the initialization that performs a derived-to-
590 /// base cast.
591 ///
592 /// \param BaseType the base type to which we will be casting.
593 ///
594 /// \param IsLValue true if the result of this cast will be treated as
595 /// an lvalue.
596 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
597
598 /// \brief Add a new step binding a reference to an object.
599 ///
600 /// \param BindingTemporary true if we are binding a reference to a temporary
601 /// object (thereby extending its lifetime); false if we are binding to an
602 /// lvalue or an lvalue treated as an rvalue.
603 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
604
605 /// \brief Add a new step invoking a conversion function, which is either
606 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000607 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000608
609 /// \brief Add a new step that performs a qualification conversion to the
610 /// given type.
611 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
612
613 /// \brief Add a new step that applies an implicit conversion sequence.
614 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
615 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000616
617 /// \brief Add a list-initialiation step
618 void AddListInitializationStep(QualType T);
619
Douglas Gregor71d17402009-12-15 00:01:57 +0000620 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000621 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
622 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000623
624 /// \brief Add a zero-initialization step.
625 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000626
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000627 /// \brief Add a C assignment step.
628 //
629 // FIXME: It isn't clear whether this should ever be needed;
630 // ideally, we would handle everything needed in C in the common
631 // path. However, that isn't the case yet.
632 void AddCAssignmentStep(QualType T);
633
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000634 /// \brief Add a string init step.
635 void AddStringInitStep(QualType T);
636
Douglas Gregor20093b42009-12-09 23:02:17 +0000637 /// \brief Note that this initialization sequence failed.
638 void SetFailed(FailureKind Failure) {
639 SequenceKind = FailedSequence;
640 this->Failure = Failure;
641 }
642
643 /// \brief Note that this initialization sequence failed due to failed
644 /// overload resolution.
645 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
646
647 /// \brief Retrieve a reference to the candidate set when overload
648 /// resolution fails.
649 OverloadCandidateSet &getFailedCandidateSet() {
650 return FailedCandidateSet;
651 }
652
653 /// \brief Determine why initialization failed.
654 FailureKind getFailureKind() const {
655 assert(getKind() == FailedSequence && "Not an initialization failure!");
656 return Failure;
657 }
658};
659
660} // end namespace clang
661
662#endif // LLVM_CLANG_SEMA_INIT_H