blob: 5c305545b09cda85ebce875ff54d7fa226a6c1b8 [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,
Anders Carlssond3d824d2010-01-23 04:34:47 +000060 /// \brief The entity being initialized is an element of an array.
61 EK_ArrayElement,
62 /// \brief The entity being initialized is an element of a vector.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000063 /// or vector.
Anders Carlssond3d824d2010-01-23 04:34:47 +000064 EK_VectorElement
65
Douglas Gregor20093b42009-12-09 23:02:17 +000066 };
67
68private:
69 /// \brief The kind of entity being initialized.
70 EntityKind Kind;
71
Douglas Gregorcb57fb92009-12-16 06:35:08 +000072 /// \brief If non-NULL, the parent entity in which this
73 /// initialization occurs.
74 const InitializedEntity *Parent;
75
Douglas Gregord6542d82009-12-22 15:35:07 +000076 /// \brief The type of the object or reference being initialized.
77 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000078
79 union {
80 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
81 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
82 DeclaratorDecl *VariableOrMember;
83
Douglas Gregor18ef5e22009-12-18 05:02:21 +000084 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
85 /// location of the 'return', 'throw', or 'new' keyword,
86 /// respectively. When Kind == EK_Temporary, the location where
87 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000088 unsigned Location;
89
90 /// \brief When Kind == EK_Base, the base specifier that provides the
91 /// base class.
92 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000093
94 /// \brief When Kind = EK_ArrayOrVectorElement, the index of the
95 /// array or vector element being initialized.
96 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +000097 };
98
99 InitializedEntity() { }
100
101 /// \brief Create the initialization entity for a variable.
102 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000103 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
104 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000105
106 /// \brief Create the initialization entity for a parameter.
107 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000108 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000109 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000110
Douglas Gregora188ff22009-12-22 16:09:06 +0000111 /// \brief Create the initialization entity for the result of a
112 /// function, throwing an object, performing an explicit cast, or
113 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000114 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
115 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000116
117 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000118 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000119 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
120 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000121
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000122 /// \brief Create the initialization entity for an array element.
123 InitializedEntity(ASTContext &Context, unsigned Index,
124 const InitializedEntity &Parent);
125
Douglas Gregor20093b42009-12-09 23:02:17 +0000126public:
127 /// \brief Create the initialization entity for a variable.
128 static InitializedEntity InitializeVariable(VarDecl *Var) {
129 return InitializedEntity(Var);
130 }
131
132 /// \brief Create the initialization entity for a parameter.
133 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
134 return InitializedEntity(Parm);
135 }
136
Douglas Gregora188ff22009-12-22 16:09:06 +0000137 /// \brief Create the initialization entity for a parameter that is
138 /// only known by its type.
139 static InitializedEntity InitializeParameter(QualType Type) {
140 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
141 }
142
Douglas Gregor20093b42009-12-09 23:02:17 +0000143 /// \brief Create the initialization entity for the result of a function.
144 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000145 QualType Type) {
146 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000147 }
148
149 /// \brief Create the initialization entity for an exception object.
150 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000151 QualType Type) {
152 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000153 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000154
155 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000156 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
157 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000158 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000159
160 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000161 static InitializedEntity InitializeTemporary(QualType Type) {
162 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000163 }
164
165 /// \brief Create the initialization entity for a base class subobject.
166 static InitializedEntity InitializeBase(ASTContext &Context,
167 CXXBaseSpecifier *Base);
168
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000169 /// \brief Create the initialization entity for a member subobject.
170 static InitializedEntity InitializeMember(FieldDecl *Member,
171 const InitializedEntity *Parent = 0) {
172 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000173 }
174
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000175 /// \brief Create the initialization entity for an array element.
176 static InitializedEntity InitializeElement(ASTContext &Context,
177 unsigned Index,
178 const InitializedEntity &Parent) {
179 return InitializedEntity(Context, Index, Parent);
180 }
181
Douglas Gregor20093b42009-12-09 23:02:17 +0000182 /// \brief Determine the kind of initialization.
183 EntityKind getKind() const { return Kind; }
184
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000185 /// \brief Retrieve the parent of the entity being initialized, when
186 /// the initialization itself is occuring within the context of a
187 /// larger initialization.
188 const InitializedEntity *getParent() const { return Parent; }
189
Douglas Gregor20093b42009-12-09 23:02:17 +0000190 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000191 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000192
Douglas Gregor99a2e602009-12-16 01:38:02 +0000193 /// \brief Retrieve the name of the entity being initialized.
194 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000195
196 /// \brief Retrieve the variable, parameter, or field being
197 /// initialized.
198 DeclaratorDecl *getDecl() const;
199
Douglas Gregor20093b42009-12-09 23:02:17 +0000200 /// \brief Determine the location of the 'return' keyword when initializing
201 /// the result of a function call.
202 SourceLocation getReturnLoc() const {
203 assert(getKind() == EK_Result && "No 'return' location!");
204 return SourceLocation::getFromRawEncoding(Location);
205 }
206
207 /// \brief Determine the location of the 'throw' keyword when initializing
208 /// an exception object.
209 SourceLocation getThrowLoc() const {
210 assert(getKind() == EK_Exception && "No 'throw' location!");
211 return SourceLocation::getFromRawEncoding(Location);
212 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000213
214 /// \brief If this is already the initializer for an array or vector
215 /// element, sets the element index.
216 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000217 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000218 this->Index = Index;
219 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000220};
221
222/// \brief Describes the kind of initialization being performed, along with
223/// location information for tokens related to the initialization (equal sign,
224/// parentheses).
225class InitializationKind {
226public:
227 /// \brief The kind of initialization being performed.
228 enum InitKind {
229 IK_Direct, ///< Direct initialization
230 IK_Copy, ///< Copy initialization
231 IK_Default, ///< Default initialization
232 IK_Value ///< Value initialization
233 };
234
235private:
236 /// \brief The kind of initialization that we're storing.
237 enum StoredInitKind {
238 SIK_Direct = IK_Direct, ///< Direct initialization
239 SIK_Copy = IK_Copy, ///< Copy initialization
240 SIK_Default = IK_Default, ///< Default initialization
241 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000242 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000243 SIK_DirectCast, ///< Direct initialization due to a cast
244 /// \brief Direct initialization due to a C-style or functional cast.
245 SIK_DirectCStyleOrFunctionalCast
246 };
247
248 /// \brief The kind of initialization being performed.
249 StoredInitKind Kind;
250
251 /// \brief The source locations involved in the initialization.
252 SourceLocation Locations[3];
253
254 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
255 SourceLocation Loc2, SourceLocation Loc3)
256 : Kind(Kind)
257 {
258 Locations[0] = Loc1;
259 Locations[1] = Loc2;
260 Locations[2] = Loc3;
261 }
262
263public:
264 /// \brief Create a direct initialization.
265 static InitializationKind CreateDirect(SourceLocation InitLoc,
266 SourceLocation LParenLoc,
267 SourceLocation RParenLoc) {
268 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
269 }
270
271 /// \brief Create a direct initialization due to a cast.
272 static InitializationKind CreateCast(SourceRange TypeRange,
273 bool IsCStyleCast) {
274 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
275 : SIK_DirectCast,
276 TypeRange.getBegin(), TypeRange.getBegin(),
277 TypeRange.getEnd());
278 }
279
280 /// \brief Create a copy initialization.
281 static InitializationKind CreateCopy(SourceLocation InitLoc,
282 SourceLocation EqualLoc) {
283 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
284 }
285
286 /// \brief Create a default initialization.
287 static InitializationKind CreateDefault(SourceLocation InitLoc) {
288 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
289 }
290
291 /// \brief Create a value initialization.
292 static InitializationKind CreateValue(SourceLocation InitLoc,
293 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000294 SourceLocation RParenLoc,
295 bool isImplicit = false) {
296 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
297 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000298 }
299
300 /// \brief Determine the initialization kind.
301 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000302 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000303 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000304 if (Kind == SIK_ImplicitValue)
305 return IK_Value;
306
Douglas Gregor20093b42009-12-09 23:02:17 +0000307 return (InitKind)Kind;
308 }
309
310 /// \brief Determine whether this initialization is an explicit cast.
311 bool isExplicitCast() const {
312 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
313 }
314
315 /// \brief Determine whether this initialization is a C-style cast.
316 bool isCStyleOrFunctionalCast() const {
317 return Kind == SIK_DirectCStyleOrFunctionalCast;
318 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000319
320 /// \brief Determine whether this initialization is an implicit
321 /// value-initialization, e.g., as occurs during aggregate
322 /// initialization.
323 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
324
Douglas Gregor20093b42009-12-09 23:02:17 +0000325 /// \brief Retrieve the location at which initialization is occurring.
326 SourceLocation getLocation() const { return Locations[0]; }
327
328 /// \brief Retrieve the source range that covers the initialization.
329 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000330 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000331 }
332
333 /// \brief Retrieve the location of the equal sign for copy initialization
334 /// (if present).
335 SourceLocation getEqualLoc() const {
336 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
337 return Locations[1];
338 }
339
340 /// \brief Retrieve the source range containing the locations of the open
341 /// and closing parentheses for value and direct initializations.
342 SourceRange getParenRange() const {
343 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
344 "Only direct- and value-initialization have parentheses");
345 return SourceRange(Locations[1], Locations[2]);
346 }
347};
348
349/// \brief Describes the sequence of initializations required to initialize
350/// a given object or reference with a set of arguments.
351class InitializationSequence {
352public:
353 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000354 ///
355 /// FIXME: Much of this information is in the initialization steps... why is
356 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000357 enum SequenceKind {
358 /// \brief A failed initialization sequence. The failure kind tells what
359 /// happened.
360 FailedSequence = 0,
361
362 /// \brief A dependent initialization, which could not be
363 /// type-checked due to the presence of dependent types or
364 /// dependently-type expressions.
365 DependentSequence,
366
Douglas Gregor4a520a22009-12-14 17:27:33 +0000367 /// \brief A user-defined conversion sequence.
368 UserDefinedConversion,
369
Douglas Gregor51c56d62009-12-14 20:49:26 +0000370 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000371 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000372
Douglas Gregor20093b42009-12-09 23:02:17 +0000373 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000374 ReferenceBinding,
375
376 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000377 ListInitialization,
378
379 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000380 ZeroInitialization,
381
382 /// \brief No initialization required.
383 NoInitialization,
384
385 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000386 StandardConversion,
387
388 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000389 CAssignment,
390
391 /// \brief String initialization
392 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000393 };
394
395 /// \brief Describes the kind of a particular step in an initialization
396 /// sequence.
397 enum StepKind {
398 /// \brief Resolve the address of an overloaded function to a specific
399 /// function declaration.
400 SK_ResolveAddressOfOverloadedFunction,
401 /// \brief Perform a derived-to-base cast, producing an rvalue.
402 SK_CastDerivedToBaseRValue,
403 /// \brief Perform a derived-to-base cast, producing an lvalue.
404 SK_CastDerivedToBaseLValue,
405 /// \brief Reference binding to an lvalue.
406 SK_BindReference,
407 /// \brief Reference binding to a temporary.
408 SK_BindReferenceToTemporary,
409 /// \brief Perform a user-defined conversion, either via a conversion
410 /// function or via a constructor.
411 SK_UserConversion,
412 /// \brief Perform a qualification conversion, producing an rvalue.
413 SK_QualificationConversionRValue,
414 /// \brief Perform a qualification conversion, producing an lvalue.
415 SK_QualificationConversionLValue,
416 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000417 SK_ConversionSequence,
418 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000419 SK_ListInitialization,
420 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000421 SK_ConstructorInitialization,
422 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000423 SK_ZeroInitialization,
424 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000425 SK_CAssignment,
426 /// \brief Initialization by string
427 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000428 };
429
430 /// \brief A single step in the initialization sequence.
431 class Step {
432 public:
433 /// \brief The kind of conversion or initialization step we are taking.
434 StepKind Kind;
435
436 // \brief The type that results from this initialization.
437 QualType Type;
438
439 union {
440 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
441 /// SK_UserConversion, the function that the expression should be
442 /// resolved to or the conversion function to call, respectively.
443 FunctionDecl *Function;
444
445 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
446 /// sequence
447 ImplicitConversionSequence *ICS;
448 };
449
450 void Destroy();
451 };
452
453private:
454 /// \brief The kind of initialization sequence computed.
455 enum SequenceKind SequenceKind;
456
457 /// \brief Steps taken by this initialization.
458 llvm::SmallVector<Step, 4> Steps;
459
460public:
461 /// \brief Describes why initialization failed.
462 enum FailureKind {
463 /// \brief Too many initializers provided for a reference.
464 FK_TooManyInitsForReference,
465 /// \brief Array must be initialized with an initializer list.
466 FK_ArrayNeedsInitList,
467 /// \brief Array must be initialized with an initializer list or a
468 /// string literal.
469 FK_ArrayNeedsInitListOrStringLiteral,
470 /// \brief Cannot resolve the address of an overloaded function.
471 FK_AddressOfOverloadFailed,
472 /// \brief Overloading due to reference initialization failed.
473 FK_ReferenceInitOverloadFailed,
474 /// \brief Non-const lvalue reference binding to a temporary.
475 FK_NonConstLValueReferenceBindingToTemporary,
476 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
477 /// type.
478 FK_NonConstLValueReferenceBindingToUnrelated,
479 /// \brief Rvalue reference binding to an lvalue.
480 FK_RValueReferenceBindingToLValue,
481 /// \brief Reference binding drops qualifiers.
482 FK_ReferenceInitDropsQualifiers,
483 /// \brief Reference binding failed.
484 FK_ReferenceInitFailed,
485 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000486 FK_ConversionFailed,
487 /// \brief Too many initializers for scalar
488 FK_TooManyInitsForScalar,
489 /// \brief Reference initialization from an initializer list
490 FK_ReferenceBindingToInitList,
491 /// \brief Initialization of some unused destination type with an
492 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000493 FK_InitListBadDestinationType,
494 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000495 FK_UserConversionOverloadFailed,
496 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000497 FK_ConstructorOverloadFailed,
498 /// \brief Default-initialization of a 'const' object.
499 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000500 };
501
502private:
503 /// \brief The reason why initialization failued.
504 FailureKind Failure;
505
506 /// \brief The failed result of overload resolution.
507 OverloadingResult FailedOverloadResult;
508
509 /// \brief The candidate set created when initialization failed.
510 OverloadCandidateSet FailedCandidateSet;
511
512public:
513 /// \brief Try to perform initialization of the given entity, creating a
514 /// record of the steps required to perform the initialization.
515 ///
516 /// The generated initialization sequence will either contain enough
517 /// information to diagnose
518 ///
519 /// \param S the semantic analysis object.
520 ///
521 /// \param Entity the entity being initialized.
522 ///
523 /// \param Kind the kind of initialization being performed.
524 ///
525 /// \param Args the argument(s) provided for initialization.
526 ///
527 /// \param NumArgs the number of arguments provided for initialization.
528 InitializationSequence(Sema &S,
529 const InitializedEntity &Entity,
530 const InitializationKind &Kind,
531 Expr **Args,
532 unsigned NumArgs);
533
534 ~InitializationSequence();
535
536 /// \brief Perform the actual initialization of the given entity based on
537 /// the computed initialization sequence.
538 ///
539 /// \param S the semantic analysis object.
540 ///
541 /// \param Entity the entity being initialized.
542 ///
543 /// \param Kind the kind of initialization being performed.
544 ///
545 /// \param Args the argument(s) provided for initialization, ownership of
546 /// which is transfered into the routine.
547 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000548 /// \param ResultType if non-NULL, will be set to the type of the
549 /// initialized object, which is the type of the declaration in most
550 /// cases. However, when the initialized object is a variable of
551 /// incomplete array type and the initializer is an initializer
552 /// list, this type will be set to the completed array type.
553 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000554 /// \returns an expression that performs the actual object initialization, if
555 /// the initialization is well-formed. Otherwise, emits diagnostics
556 /// and returns an invalid expression.
557 Action::OwningExprResult Perform(Sema &S,
558 const InitializedEntity &Entity,
559 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000560 Action::MultiExprArg Args,
561 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000562
563 /// \brief Diagnose an potentially-invalid initialization sequence.
564 ///
565 /// \returns true if the initialization sequence was ill-formed,
566 /// false otherwise.
567 bool Diagnose(Sema &S,
568 const InitializedEntity &Entity,
569 const InitializationKind &Kind,
570 Expr **Args, unsigned NumArgs);
571
572 /// \brief Determine the kind of initialization sequence computed.
573 enum SequenceKind getKind() const { return SequenceKind; }
574
575 /// \brief Set the kind of sequence computed.
576 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
577
578 /// \brief Determine whether the initialization sequence is valid.
579 operator bool() const { return SequenceKind != FailedSequence; }
580
581 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
582 step_iterator step_begin() const { return Steps.begin(); }
583 step_iterator step_end() const { return Steps.end(); }
584
585 /// \brief Add a new step in the initialization that resolves the address
586 /// of an overloaded function to a specific function declaration.
587 ///
588 /// \param Function the function to which the overloaded function reference
589 /// resolves.
590 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
591
592 /// \brief Add a new step in the initialization that performs a derived-to-
593 /// base cast.
594 ///
595 /// \param BaseType the base type to which we will be casting.
596 ///
597 /// \param IsLValue true if the result of this cast will be treated as
598 /// an lvalue.
599 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
600
601 /// \brief Add a new step binding a reference to an object.
602 ///
603 /// \param BindingTemporary true if we are binding a reference to a temporary
604 /// object (thereby extending its lifetime); false if we are binding to an
605 /// lvalue or an lvalue treated as an rvalue.
606 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
607
608 /// \brief Add a new step invoking a conversion function, which is either
609 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000610 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000611
612 /// \brief Add a new step that performs a qualification conversion to the
613 /// given type.
614 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
615
616 /// \brief Add a new step that applies an implicit conversion sequence.
617 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
618 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000619
620 /// \brief Add a list-initialiation step
621 void AddListInitializationStep(QualType T);
622
Douglas Gregor71d17402009-12-15 00:01:57 +0000623 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000624 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
625 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000626
627 /// \brief Add a zero-initialization step.
628 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000629
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000630 /// \brief Add a C assignment step.
631 //
632 // FIXME: It isn't clear whether this should ever be needed;
633 // ideally, we would handle everything needed in C in the common
634 // path. However, that isn't the case yet.
635 void AddCAssignmentStep(QualType T);
636
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000637 /// \brief Add a string init step.
638 void AddStringInitStep(QualType T);
639
Douglas Gregor20093b42009-12-09 23:02:17 +0000640 /// \brief Note that this initialization sequence failed.
641 void SetFailed(FailureKind Failure) {
642 SequenceKind = FailedSequence;
643 this->Failure = Failure;
644 }
645
646 /// \brief Note that this initialization sequence failed due to failed
647 /// overload resolution.
648 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
649
650 /// \brief Retrieve a reference to the candidate set when overload
651 /// resolution fails.
652 OverloadCandidateSet &getFailedCandidateSet() {
653 return FailedCandidateSet;
654 }
655
656 /// \brief Determine why initialization failed.
657 FailureKind getFailureKind() const {
658 assert(getKind() == FailedSequence && "Not an initialization failure!");
659 return Failure;
660 }
661};
662
663} // end namespace clang
664
665#endif // LLVM_CLANG_SEMA_INIT_H