blob: c5ba57c72648d0e2fb8d275adeb24898b0ef51c1 [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 Gregord6542d82009-12-22 15:35:07 +0000105 : Kind(EK_Parameter), Parent(0), Type(Parm->getType()),
106 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000107
108 /// \brief Create the initialization entity for the result of a function,
109 /// throwing an object, or performing an explicit cast.
Douglas Gregord6542d82009-12-22 15:35:07 +0000110 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
111 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000112
113 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000114 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000115 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
116 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000117
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000118 /// \brief Create the initialization entity for an array element.
119 InitializedEntity(ASTContext &Context, unsigned Index,
120 const InitializedEntity &Parent);
121
Douglas Gregor20093b42009-12-09 23:02:17 +0000122public:
123 /// \brief Create the initialization entity for a variable.
124 static InitializedEntity InitializeVariable(VarDecl *Var) {
125 return InitializedEntity(Var);
126 }
127
128 /// \brief Create the initialization entity for a parameter.
129 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
130 return InitializedEntity(Parm);
131 }
132
133 /// \brief Create the initialization entity for the result of a function.
134 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000135 QualType Type) {
136 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000137 }
138
139 /// \brief Create the initialization entity for an exception object.
140 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000141 QualType Type) {
142 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000143 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000144
145 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000146 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
147 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000148 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000149
150 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000151 static InitializedEntity InitializeTemporary(QualType Type) {
152 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000153 }
154
155 /// \brief Create the initialization entity for a base class subobject.
156 static InitializedEntity InitializeBase(ASTContext &Context,
157 CXXBaseSpecifier *Base);
158
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000159 /// \brief Create the initialization entity for a member subobject.
160 static InitializedEntity InitializeMember(FieldDecl *Member,
161 const InitializedEntity *Parent = 0) {
162 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000163 }
164
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000165 /// \brief Create the initialization entity for an array element.
166 static InitializedEntity InitializeElement(ASTContext &Context,
167 unsigned Index,
168 const InitializedEntity &Parent) {
169 return InitializedEntity(Context, Index, Parent);
170 }
171
Douglas Gregor20093b42009-12-09 23:02:17 +0000172 /// \brief Determine the kind of initialization.
173 EntityKind getKind() const { return Kind; }
174
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000175 /// \brief Retrieve the parent of the entity being initialized, when
176 /// the initialization itself is occuring within the context of a
177 /// larger initialization.
178 const InitializedEntity *getParent() const { return Parent; }
179
Douglas Gregor20093b42009-12-09 23:02:17 +0000180 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000181 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000182
Douglas Gregor99a2e602009-12-16 01:38:02 +0000183 /// \brief Retrieve the name of the entity being initialized.
184 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000185
186 /// \brief Retrieve the variable, parameter, or field being
187 /// initialized.
188 DeclaratorDecl *getDecl() const;
189
Douglas Gregor20093b42009-12-09 23:02:17 +0000190 /// \brief Determine the location of the 'return' keyword when initializing
191 /// the result of a function call.
192 SourceLocation getReturnLoc() const {
193 assert(getKind() == EK_Result && "No 'return' location!");
194 return SourceLocation::getFromRawEncoding(Location);
195 }
196
197 /// \brief Determine the location of the 'throw' keyword when initializing
198 /// an exception object.
199 SourceLocation getThrowLoc() const {
200 assert(getKind() == EK_Exception && "No 'throw' location!");
201 return SourceLocation::getFromRawEncoding(Location);
202 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000203
204 /// \brief If this is already the initializer for an array or vector
205 /// element, sets the element index.
206 void setElementIndex(unsigned Index) {
207 assert(getKind() == EK_ArrayOrVectorElement);
208 this->Index = Index;
209 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000210};
211
212/// \brief Describes the kind of initialization being performed, along with
213/// location information for tokens related to the initialization (equal sign,
214/// parentheses).
215class InitializationKind {
216public:
217 /// \brief The kind of initialization being performed.
218 enum InitKind {
219 IK_Direct, ///< Direct initialization
220 IK_Copy, ///< Copy initialization
221 IK_Default, ///< Default initialization
222 IK_Value ///< Value initialization
223 };
224
225private:
226 /// \brief The kind of initialization that we're storing.
227 enum StoredInitKind {
228 SIK_Direct = IK_Direct, ///< Direct initialization
229 SIK_Copy = IK_Copy, ///< Copy initialization
230 SIK_Default = IK_Default, ///< Default initialization
231 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000232 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000233 SIK_DirectCast, ///< Direct initialization due to a cast
234 /// \brief Direct initialization due to a C-style or functional cast.
235 SIK_DirectCStyleOrFunctionalCast
236 };
237
238 /// \brief The kind of initialization being performed.
239 StoredInitKind Kind;
240
241 /// \brief The source locations involved in the initialization.
242 SourceLocation Locations[3];
243
244 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
245 SourceLocation Loc2, SourceLocation Loc3)
246 : Kind(Kind)
247 {
248 Locations[0] = Loc1;
249 Locations[1] = Loc2;
250 Locations[2] = Loc3;
251 }
252
253public:
254 /// \brief Create a direct initialization.
255 static InitializationKind CreateDirect(SourceLocation InitLoc,
256 SourceLocation LParenLoc,
257 SourceLocation RParenLoc) {
258 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
259 }
260
261 /// \brief Create a direct initialization due to a cast.
262 static InitializationKind CreateCast(SourceRange TypeRange,
263 bool IsCStyleCast) {
264 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
265 : SIK_DirectCast,
266 TypeRange.getBegin(), TypeRange.getBegin(),
267 TypeRange.getEnd());
268 }
269
270 /// \brief Create a copy initialization.
271 static InitializationKind CreateCopy(SourceLocation InitLoc,
272 SourceLocation EqualLoc) {
273 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
274 }
275
276 /// \brief Create a default initialization.
277 static InitializationKind CreateDefault(SourceLocation InitLoc) {
278 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
279 }
280
281 /// \brief Create a value initialization.
282 static InitializationKind CreateValue(SourceLocation InitLoc,
283 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000284 SourceLocation RParenLoc,
285 bool isImplicit = false) {
286 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
287 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000288 }
289
290 /// \brief Determine the initialization kind.
291 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000292 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000293 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000294 if (Kind == SIK_ImplicitValue)
295 return IK_Value;
296
Douglas Gregor20093b42009-12-09 23:02:17 +0000297 return (InitKind)Kind;
298 }
299
300 /// \brief Determine whether this initialization is an explicit cast.
301 bool isExplicitCast() const {
302 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
303 }
304
305 /// \brief Determine whether this initialization is a C-style cast.
306 bool isCStyleOrFunctionalCast() const {
307 return Kind == SIK_DirectCStyleOrFunctionalCast;
308 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000309
310 /// \brief Determine whether this initialization is an implicit
311 /// value-initialization, e.g., as occurs during aggregate
312 /// initialization.
313 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
314
Douglas Gregor20093b42009-12-09 23:02:17 +0000315 /// \brief Retrieve the location at which initialization is occurring.
316 SourceLocation getLocation() const { return Locations[0]; }
317
318 /// \brief Retrieve the source range that covers the initialization.
319 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000320 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000321 }
322
323 /// \brief Retrieve the location of the equal sign for copy initialization
324 /// (if present).
325 SourceLocation getEqualLoc() const {
326 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
327 return Locations[1];
328 }
329
330 /// \brief Retrieve the source range containing the locations of the open
331 /// and closing parentheses for value and direct initializations.
332 SourceRange getParenRange() const {
333 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
334 "Only direct- and value-initialization have parentheses");
335 return SourceRange(Locations[1], Locations[2]);
336 }
337};
338
339/// \brief Describes the sequence of initializations required to initialize
340/// a given object or reference with a set of arguments.
341class InitializationSequence {
342public:
343 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000344 ///
345 /// FIXME: Much of this information is in the initialization steps... why is
346 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000347 enum SequenceKind {
348 /// \brief A failed initialization sequence. The failure kind tells what
349 /// happened.
350 FailedSequence = 0,
351
352 /// \brief A dependent initialization, which could not be
353 /// type-checked due to the presence of dependent types or
354 /// dependently-type expressions.
355 DependentSequence,
356
Douglas Gregor4a520a22009-12-14 17:27:33 +0000357 /// \brief A user-defined conversion sequence.
358 UserDefinedConversion,
359
Douglas Gregor51c56d62009-12-14 20:49:26 +0000360 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000361 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000362
Douglas Gregor20093b42009-12-09 23:02:17 +0000363 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000364 ReferenceBinding,
365
366 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000367 ListInitialization,
368
369 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000370 ZeroInitialization,
371
372 /// \brief No initialization required.
373 NoInitialization,
374
375 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000376 StandardConversion,
377
378 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000379 CAssignment,
380
381 /// \brief String initialization
382 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000383 };
384
385 /// \brief Describes the kind of a particular step in an initialization
386 /// sequence.
387 enum StepKind {
388 /// \brief Resolve the address of an overloaded function to a specific
389 /// function declaration.
390 SK_ResolveAddressOfOverloadedFunction,
391 /// \brief Perform a derived-to-base cast, producing an rvalue.
392 SK_CastDerivedToBaseRValue,
393 /// \brief Perform a derived-to-base cast, producing an lvalue.
394 SK_CastDerivedToBaseLValue,
395 /// \brief Reference binding to an lvalue.
396 SK_BindReference,
397 /// \brief Reference binding to a temporary.
398 SK_BindReferenceToTemporary,
399 /// \brief Perform a user-defined conversion, either via a conversion
400 /// function or via a constructor.
401 SK_UserConversion,
402 /// \brief Perform a qualification conversion, producing an rvalue.
403 SK_QualificationConversionRValue,
404 /// \brief Perform a qualification conversion, producing an lvalue.
405 SK_QualificationConversionLValue,
406 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000407 SK_ConversionSequence,
408 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000409 SK_ListInitialization,
410 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000411 SK_ConstructorInitialization,
412 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000413 SK_ZeroInitialization,
414 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000415 SK_CAssignment,
416 /// \brief Initialization by string
417 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000418 };
419
420 /// \brief A single step in the initialization sequence.
421 class Step {
422 public:
423 /// \brief The kind of conversion or initialization step we are taking.
424 StepKind Kind;
425
426 // \brief The type that results from this initialization.
427 QualType Type;
428
429 union {
430 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
431 /// SK_UserConversion, the function that the expression should be
432 /// resolved to or the conversion function to call, respectively.
433 FunctionDecl *Function;
434
435 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
436 /// sequence
437 ImplicitConversionSequence *ICS;
438 };
439
440 void Destroy();
441 };
442
443private:
444 /// \brief The kind of initialization sequence computed.
445 enum SequenceKind SequenceKind;
446
447 /// \brief Steps taken by this initialization.
448 llvm::SmallVector<Step, 4> Steps;
449
450public:
451 /// \brief Describes why initialization failed.
452 enum FailureKind {
453 /// \brief Too many initializers provided for a reference.
454 FK_TooManyInitsForReference,
455 /// \brief Array must be initialized with an initializer list.
456 FK_ArrayNeedsInitList,
457 /// \brief Array must be initialized with an initializer list or a
458 /// string literal.
459 FK_ArrayNeedsInitListOrStringLiteral,
460 /// \brief Cannot resolve the address of an overloaded function.
461 FK_AddressOfOverloadFailed,
462 /// \brief Overloading due to reference initialization failed.
463 FK_ReferenceInitOverloadFailed,
464 /// \brief Non-const lvalue reference binding to a temporary.
465 FK_NonConstLValueReferenceBindingToTemporary,
466 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
467 /// type.
468 FK_NonConstLValueReferenceBindingToUnrelated,
469 /// \brief Rvalue reference binding to an lvalue.
470 FK_RValueReferenceBindingToLValue,
471 /// \brief Reference binding drops qualifiers.
472 FK_ReferenceInitDropsQualifiers,
473 /// \brief Reference binding failed.
474 FK_ReferenceInitFailed,
475 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000476 FK_ConversionFailed,
477 /// \brief Too many initializers for scalar
478 FK_TooManyInitsForScalar,
479 /// \brief Reference initialization from an initializer list
480 FK_ReferenceBindingToInitList,
481 /// \brief Initialization of some unused destination type with an
482 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000483 FK_InitListBadDestinationType,
484 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000485 FK_UserConversionOverloadFailed,
486 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000487 FK_ConstructorOverloadFailed,
488 /// \brief Default-initialization of a 'const' object.
489 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000490 };
491
492private:
493 /// \brief The reason why initialization failued.
494 FailureKind Failure;
495
496 /// \brief The failed result of overload resolution.
497 OverloadingResult FailedOverloadResult;
498
499 /// \brief The candidate set created when initialization failed.
500 OverloadCandidateSet FailedCandidateSet;
501
502public:
503 /// \brief Try to perform initialization of the given entity, creating a
504 /// record of the steps required to perform the initialization.
505 ///
506 /// The generated initialization sequence will either contain enough
507 /// information to diagnose
508 ///
509 /// \param S the semantic analysis object.
510 ///
511 /// \param Entity the entity being initialized.
512 ///
513 /// \param Kind the kind of initialization being performed.
514 ///
515 /// \param Args the argument(s) provided for initialization.
516 ///
517 /// \param NumArgs the number of arguments provided for initialization.
518 InitializationSequence(Sema &S,
519 const InitializedEntity &Entity,
520 const InitializationKind &Kind,
521 Expr **Args,
522 unsigned NumArgs);
523
524 ~InitializationSequence();
525
526 /// \brief Perform the actual initialization of the given entity based on
527 /// the computed initialization sequence.
528 ///
529 /// \param S the semantic analysis object.
530 ///
531 /// \param Entity the entity being initialized.
532 ///
533 /// \param Kind the kind of initialization being performed.
534 ///
535 /// \param Args the argument(s) provided for initialization, ownership of
536 /// which is transfered into the routine.
537 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000538 /// \param ResultType if non-NULL, will be set to the type of the
539 /// initialized object, which is the type of the declaration in most
540 /// cases. However, when the initialized object is a variable of
541 /// incomplete array type and the initializer is an initializer
542 /// list, this type will be set to the completed array type.
543 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000544 /// \returns an expression that performs the actual object initialization, if
545 /// the initialization is well-formed. Otherwise, emits diagnostics
546 /// and returns an invalid expression.
547 Action::OwningExprResult Perform(Sema &S,
548 const InitializedEntity &Entity,
549 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000550 Action::MultiExprArg Args,
551 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000552
553 /// \brief Diagnose an potentially-invalid initialization sequence.
554 ///
555 /// \returns true if the initialization sequence was ill-formed,
556 /// false otherwise.
557 bool Diagnose(Sema &S,
558 const InitializedEntity &Entity,
559 const InitializationKind &Kind,
560 Expr **Args, unsigned NumArgs);
561
562 /// \brief Determine the kind of initialization sequence computed.
563 enum SequenceKind getKind() const { return SequenceKind; }
564
565 /// \brief Set the kind of sequence computed.
566 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
567
568 /// \brief Determine whether the initialization sequence is valid.
569 operator bool() const { return SequenceKind != FailedSequence; }
570
571 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
572 step_iterator step_begin() const { return Steps.begin(); }
573 step_iterator step_end() const { return Steps.end(); }
574
575 /// \brief Add a new step in the initialization that resolves the address
576 /// of an overloaded function to a specific function declaration.
577 ///
578 /// \param Function the function to which the overloaded function reference
579 /// resolves.
580 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
581
582 /// \brief Add a new step in the initialization that performs a derived-to-
583 /// base cast.
584 ///
585 /// \param BaseType the base type to which we will be casting.
586 ///
587 /// \param IsLValue true if the result of this cast will be treated as
588 /// an lvalue.
589 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
590
591 /// \brief Add a new step binding a reference to an object.
592 ///
593 /// \param BindingTemporary true if we are binding a reference to a temporary
594 /// object (thereby extending its lifetime); false if we are binding to an
595 /// lvalue or an lvalue treated as an rvalue.
596 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
597
598 /// \brief Add a new step invoking a conversion function, which is either
599 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000600 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000601
602 /// \brief Add a new step that performs a qualification conversion to the
603 /// given type.
604 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
605
606 /// \brief Add a new step that applies an implicit conversion sequence.
607 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
608 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000609
610 /// \brief Add a list-initialiation step
611 void AddListInitializationStep(QualType T);
612
Douglas Gregor71d17402009-12-15 00:01:57 +0000613 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000614 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
615 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000616
617 /// \brief Add a zero-initialization step.
618 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000619
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000620 /// \brief Add a C assignment step.
621 //
622 // FIXME: It isn't clear whether this should ever be needed;
623 // ideally, we would handle everything needed in C in the common
624 // path. However, that isn't the case yet.
625 void AddCAssignmentStep(QualType T);
626
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000627 /// \brief Add a string init step.
628 void AddStringInitStep(QualType T);
629
Douglas Gregor20093b42009-12-09 23:02:17 +0000630 /// \brief Note that this initialization sequence failed.
631 void SetFailed(FailureKind Failure) {
632 SequenceKind = FailedSequence;
633 this->Failure = Failure;
634 }
635
636 /// \brief Note that this initialization sequence failed due to failed
637 /// overload resolution.
638 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
639
640 /// \brief Retrieve a reference to the candidate set when overload
641 /// resolution fails.
642 OverloadCandidateSet &getFailedCandidateSet() {
643 return FailedCandidateSet;
644 }
645
646 /// \brief Determine why initialization failed.
647 FailureKind getFailureKind() const {
648 assert(getKind() == FailedSequence && "Not an initialization failure!");
649 return Failure;
650 }
651};
652
653} // end namespace clang
654
655#endif // LLVM_CLANG_SEMA_INIT_H