blob: 85f3d2c1a315b065017e3daf328d2cafd327603d [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"
17#include "clang/AST/TypeLoc.h"
18#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 Gregor20093b42009-12-09 23:02:17 +000073 /// \brief The type of the object or reference being initialized along with
74 /// its location information.
75 TypeLoc TL;
76
77 union {
78 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
79 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
80 DeclaratorDecl *VariableOrMember;
81
Douglas Gregor18ef5e22009-12-18 05:02:21 +000082 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
83 /// location of the 'return', 'throw', or 'new' keyword,
84 /// respectively. When Kind == EK_Temporary, the location where
85 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000086 unsigned Location;
87
88 /// \brief When Kind == EK_Base, the base specifier that provides the
89 /// base class.
90 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000091
92 /// \brief When Kind = EK_ArrayOrVectorElement, the index of the
93 /// array or vector element being initialized.
94 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +000095 };
96
97 InitializedEntity() { }
98
99 /// \brief Create the initialization entity for a variable.
100 InitializedEntity(VarDecl *Var)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000101 : Kind(EK_Variable), Parent(0),
Douglas Gregor20093b42009-12-09 23:02:17 +0000102 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var))
103 {
104 InitDeclLoc();
105 }
106
107 /// \brief Create the initialization entity for a parameter.
108 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000109 : Kind(EK_Parameter), Parent(0),
Douglas Gregor20093b42009-12-09 23:02:17 +0000110 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm))
111 {
112 InitDeclLoc();
113 }
114
115 /// \brief Create the initialization entity for the result of a function,
116 /// throwing an object, or performing an explicit cast.
117 InitializedEntity(EntityKind Kind, SourceLocation Loc, TypeLoc TL)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000118 : Kind(Kind), Parent(0), TL(TL), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000119
120 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000121 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
122 : Kind(EK_Member), Parent(Parent),
Douglas Gregor20093b42009-12-09 23:02:17 +0000123 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member))
124 {
125 InitDeclLoc();
126 }
127
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000128 /// \brief Create the initialization entity for an array element.
129 InitializedEntity(ASTContext &Context, unsigned Index,
130 const InitializedEntity &Parent);
131
Douglas Gregor20093b42009-12-09 23:02:17 +0000132 /// \brief Initialize type-location information from a declaration.
133 void InitDeclLoc();
134
135public:
136 /// \brief Create the initialization entity for a variable.
137 static InitializedEntity InitializeVariable(VarDecl *Var) {
138 return InitializedEntity(Var);
139 }
140
141 /// \brief Create the initialization entity for a parameter.
142 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
143 return InitializedEntity(Parm);
144 }
145
146 /// \brief Create the initialization entity for the result of a function.
147 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
148 TypeLoc TL) {
149 return InitializedEntity(EK_Result, ReturnLoc, TL);
150 }
151
152 /// \brief Create the initialization entity for an exception object.
153 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
154 TypeLoc TL) {
155 return InitializedEntity(EK_Exception, ThrowLoc, TL);
156 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000157
158 /// \brief Create the initialization entity for an object allocated via new.
159 static InitializedEntity InitializeNew(SourceLocation NewLoc, TypeLoc TL) {
160 return InitializedEntity(EK_New, NewLoc, TL);
161 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000162
163 /// \brief Create the initialization entity for a temporary.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000164 static InitializedEntity InitializeTemporary(TypeLoc TL) {
165 return InitializedEntity(EK_Temporary, SourceLocation(), TL);
Douglas Gregor20093b42009-12-09 23:02:17 +0000166 }
167
168 /// \brief Create the initialization entity for a base class subobject.
169 static InitializedEntity InitializeBase(ASTContext &Context,
170 CXXBaseSpecifier *Base);
171
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000172 /// \brief Create the initialization entity for a member subobject.
173 static InitializedEntity InitializeMember(FieldDecl *Member,
174 const InitializedEntity *Parent = 0) {
175 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000176 }
177
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000178 /// \brief Create the initialization entity for an array element.
179 static InitializedEntity InitializeElement(ASTContext &Context,
180 unsigned Index,
181 const InitializedEntity &Parent) {
182 return InitializedEntity(Context, Index, Parent);
183 }
184
Douglas Gregor20093b42009-12-09 23:02:17 +0000185 /// \brief Determine the kind of initialization.
186 EntityKind getKind() const { return Kind; }
187
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000188 /// \brief Retrieve the parent of the entity being initialized, when
189 /// the initialization itself is occuring within the context of a
190 /// larger initialization.
191 const InitializedEntity *getParent() const { return Parent; }
192
Douglas Gregor20093b42009-12-09 23:02:17 +0000193 /// \brief Retrieve type being initialized.
194 TypeLoc getType() const { return TL; }
195
Douglas Gregor99a2e602009-12-16 01:38:02 +0000196 /// \brief Retrieve the name of the entity being initialized.
197 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000198
199 /// \brief Retrieve the variable, parameter, or field being
200 /// initialized.
201 DeclaratorDecl *getDecl() const;
202
Douglas Gregor20093b42009-12-09 23:02:17 +0000203 /// \brief Determine the location of the 'return' keyword when initializing
204 /// the result of a function call.
205 SourceLocation getReturnLoc() const {
206 assert(getKind() == EK_Result && "No 'return' location!");
207 return SourceLocation::getFromRawEncoding(Location);
208 }
209
210 /// \brief Determine the location of the 'throw' keyword when initializing
211 /// an exception object.
212 SourceLocation getThrowLoc() const {
213 assert(getKind() == EK_Exception && "No 'throw' location!");
214 return SourceLocation::getFromRawEncoding(Location);
215 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000216
217 /// \brief If this is already the initializer for an array or vector
218 /// element, sets the element index.
219 void setElementIndex(unsigned Index) {
220 assert(getKind() == EK_ArrayOrVectorElement);
221 this->Index = Index;
222 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000223};
224
225/// \brief Describes the kind of initialization being performed, along with
226/// location information for tokens related to the initialization (equal sign,
227/// parentheses).
228class InitializationKind {
229public:
230 /// \brief The kind of initialization being performed.
231 enum InitKind {
232 IK_Direct, ///< Direct initialization
233 IK_Copy, ///< Copy initialization
234 IK_Default, ///< Default initialization
235 IK_Value ///< Value initialization
236 };
237
238private:
239 /// \brief The kind of initialization that we're storing.
240 enum StoredInitKind {
241 SIK_Direct = IK_Direct, ///< Direct initialization
242 SIK_Copy = IK_Copy, ///< Copy initialization
243 SIK_Default = IK_Default, ///< Default initialization
244 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000245 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000246 SIK_DirectCast, ///< Direct initialization due to a cast
247 /// \brief Direct initialization due to a C-style or functional cast.
248 SIK_DirectCStyleOrFunctionalCast
249 };
250
251 /// \brief The kind of initialization being performed.
252 StoredInitKind Kind;
253
254 /// \brief The source locations involved in the initialization.
255 SourceLocation Locations[3];
256
257 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
258 SourceLocation Loc2, SourceLocation Loc3)
259 : Kind(Kind)
260 {
261 Locations[0] = Loc1;
262 Locations[1] = Loc2;
263 Locations[2] = Loc3;
264 }
265
266public:
267 /// \brief Create a direct initialization.
268 static InitializationKind CreateDirect(SourceLocation InitLoc,
269 SourceLocation LParenLoc,
270 SourceLocation RParenLoc) {
271 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
272 }
273
274 /// \brief Create a direct initialization due to a cast.
275 static InitializationKind CreateCast(SourceRange TypeRange,
276 bool IsCStyleCast) {
277 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
278 : SIK_DirectCast,
279 TypeRange.getBegin(), TypeRange.getBegin(),
280 TypeRange.getEnd());
281 }
282
283 /// \brief Create a copy initialization.
284 static InitializationKind CreateCopy(SourceLocation InitLoc,
285 SourceLocation EqualLoc) {
286 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
287 }
288
289 /// \brief Create a default initialization.
290 static InitializationKind CreateDefault(SourceLocation InitLoc) {
291 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
292 }
293
294 /// \brief Create a value initialization.
295 static InitializationKind CreateValue(SourceLocation InitLoc,
296 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000297 SourceLocation RParenLoc,
298 bool isImplicit = false) {
299 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
300 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000301 }
302
303 /// \brief Determine the initialization kind.
304 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000305 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000306 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000307 if (Kind == SIK_ImplicitValue)
308 return IK_Value;
309
Douglas Gregor20093b42009-12-09 23:02:17 +0000310 return (InitKind)Kind;
311 }
312
313 /// \brief Determine whether this initialization is an explicit cast.
314 bool isExplicitCast() const {
315 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
316 }
317
318 /// \brief Determine whether this initialization is a C-style cast.
319 bool isCStyleOrFunctionalCast() const {
320 return Kind == SIK_DirectCStyleOrFunctionalCast;
321 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000322
323 /// \brief Determine whether this initialization is an implicit
324 /// value-initialization, e.g., as occurs during aggregate
325 /// initialization.
326 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
327
Douglas Gregor20093b42009-12-09 23:02:17 +0000328 /// \brief Retrieve the location at which initialization is occurring.
329 SourceLocation getLocation() const { return Locations[0]; }
330
331 /// \brief Retrieve the source range that covers the initialization.
332 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000333 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000334 }
335
336 /// \brief Retrieve the location of the equal sign for copy initialization
337 /// (if present).
338 SourceLocation getEqualLoc() const {
339 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
340 return Locations[1];
341 }
342
343 /// \brief Retrieve the source range containing the locations of the open
344 /// and closing parentheses for value and direct initializations.
345 SourceRange getParenRange() const {
346 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
347 "Only direct- and value-initialization have parentheses");
348 return SourceRange(Locations[1], Locations[2]);
349 }
350};
351
352/// \brief Describes the sequence of initializations required to initialize
353/// a given object or reference with a set of arguments.
354class InitializationSequence {
355public:
356 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000357 ///
358 /// FIXME: Much of this information is in the initialization steps... why is
359 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000360 enum SequenceKind {
361 /// \brief A failed initialization sequence. The failure kind tells what
362 /// happened.
363 FailedSequence = 0,
364
365 /// \brief A dependent initialization, which could not be
366 /// type-checked due to the presence of dependent types or
367 /// dependently-type expressions.
368 DependentSequence,
369
Douglas Gregor4a520a22009-12-14 17:27:33 +0000370 /// \brief A user-defined conversion sequence.
371 UserDefinedConversion,
372
Douglas Gregor51c56d62009-12-14 20:49:26 +0000373 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000374 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000375
Douglas Gregor20093b42009-12-09 23:02:17 +0000376 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000377 ReferenceBinding,
378
379 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000380 ListInitialization,
381
382 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000383 ZeroInitialization,
384
385 /// \brief No initialization required.
386 NoInitialization,
387
388 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000389 StandardConversion,
390
391 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000392 CAssignment,
393
394 /// \brief String initialization
395 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000396 };
397
398 /// \brief Describes the kind of a particular step in an initialization
399 /// sequence.
400 enum StepKind {
401 /// \brief Resolve the address of an overloaded function to a specific
402 /// function declaration.
403 SK_ResolveAddressOfOverloadedFunction,
404 /// \brief Perform a derived-to-base cast, producing an rvalue.
405 SK_CastDerivedToBaseRValue,
406 /// \brief Perform a derived-to-base cast, producing an lvalue.
407 SK_CastDerivedToBaseLValue,
408 /// \brief Reference binding to an lvalue.
409 SK_BindReference,
410 /// \brief Reference binding to a temporary.
411 SK_BindReferenceToTemporary,
412 /// \brief Perform a user-defined conversion, either via a conversion
413 /// function or via a constructor.
414 SK_UserConversion,
415 /// \brief Perform a qualification conversion, producing an rvalue.
416 SK_QualificationConversionRValue,
417 /// \brief Perform a qualification conversion, producing an lvalue.
418 SK_QualificationConversionLValue,
419 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000420 SK_ConversionSequence,
421 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000422 SK_ListInitialization,
423 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000424 SK_ConstructorInitialization,
425 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000426 SK_ZeroInitialization,
427 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000428 SK_CAssignment,
429 /// \brief Initialization by string
430 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000431 };
432
433 /// \brief A single step in the initialization sequence.
434 class Step {
435 public:
436 /// \brief The kind of conversion or initialization step we are taking.
437 StepKind Kind;
438
439 // \brief The type that results from this initialization.
440 QualType Type;
441
442 union {
443 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
444 /// SK_UserConversion, the function that the expression should be
445 /// resolved to or the conversion function to call, respectively.
446 FunctionDecl *Function;
447
448 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
449 /// sequence
450 ImplicitConversionSequence *ICS;
451 };
452
453 void Destroy();
454 };
455
456private:
457 /// \brief The kind of initialization sequence computed.
458 enum SequenceKind SequenceKind;
459
460 /// \brief Steps taken by this initialization.
461 llvm::SmallVector<Step, 4> Steps;
462
463public:
464 /// \brief Describes why initialization failed.
465 enum FailureKind {
466 /// \brief Too many initializers provided for a reference.
467 FK_TooManyInitsForReference,
468 /// \brief Array must be initialized with an initializer list.
469 FK_ArrayNeedsInitList,
470 /// \brief Array must be initialized with an initializer list or a
471 /// string literal.
472 FK_ArrayNeedsInitListOrStringLiteral,
473 /// \brief Cannot resolve the address of an overloaded function.
474 FK_AddressOfOverloadFailed,
475 /// \brief Overloading due to reference initialization failed.
476 FK_ReferenceInitOverloadFailed,
477 /// \brief Non-const lvalue reference binding to a temporary.
478 FK_NonConstLValueReferenceBindingToTemporary,
479 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
480 /// type.
481 FK_NonConstLValueReferenceBindingToUnrelated,
482 /// \brief Rvalue reference binding to an lvalue.
483 FK_RValueReferenceBindingToLValue,
484 /// \brief Reference binding drops qualifiers.
485 FK_ReferenceInitDropsQualifiers,
486 /// \brief Reference binding failed.
487 FK_ReferenceInitFailed,
488 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000489 FK_ConversionFailed,
490 /// \brief Too many initializers for scalar
491 FK_TooManyInitsForScalar,
492 /// \brief Reference initialization from an initializer list
493 FK_ReferenceBindingToInitList,
494 /// \brief Initialization of some unused destination type with an
495 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000496 FK_InitListBadDestinationType,
497 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000498 FK_UserConversionOverloadFailed,
499 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000500 FK_ConstructorOverloadFailed,
501 /// \brief Default-initialization of a 'const' object.
502 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000503 };
504
505private:
506 /// \brief The reason why initialization failued.
507 FailureKind Failure;
508
509 /// \brief The failed result of overload resolution.
510 OverloadingResult FailedOverloadResult;
511
512 /// \brief The candidate set created when initialization failed.
513 OverloadCandidateSet FailedCandidateSet;
514
515public:
516 /// \brief Try to perform initialization of the given entity, creating a
517 /// record of the steps required to perform the initialization.
518 ///
519 /// The generated initialization sequence will either contain enough
520 /// information to diagnose
521 ///
522 /// \param S the semantic analysis object.
523 ///
524 /// \param Entity the entity being initialized.
525 ///
526 /// \param Kind the kind of initialization being performed.
527 ///
528 /// \param Args the argument(s) provided for initialization.
529 ///
530 /// \param NumArgs the number of arguments provided for initialization.
531 InitializationSequence(Sema &S,
532 const InitializedEntity &Entity,
533 const InitializationKind &Kind,
534 Expr **Args,
535 unsigned NumArgs);
536
537 ~InitializationSequence();
538
539 /// \brief Perform the actual initialization of the given entity based on
540 /// the computed initialization sequence.
541 ///
542 /// \param S the semantic analysis object.
543 ///
544 /// \param Entity the entity being initialized.
545 ///
546 /// \param Kind the kind of initialization being performed.
547 ///
548 /// \param Args the argument(s) provided for initialization, ownership of
549 /// which is transfered into the routine.
550 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000551 /// \param ResultType if non-NULL, will be set to the type of the
552 /// initialized object, which is the type of the declaration in most
553 /// cases. However, when the initialized object is a variable of
554 /// incomplete array type and the initializer is an initializer
555 /// list, this type will be set to the completed array type.
556 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000557 /// \returns an expression that performs the actual object initialization, if
558 /// the initialization is well-formed. Otherwise, emits diagnostics
559 /// and returns an invalid expression.
560 Action::OwningExprResult Perform(Sema &S,
561 const InitializedEntity &Entity,
562 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000563 Action::MultiExprArg Args,
564 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000565
566 /// \brief Diagnose an potentially-invalid initialization sequence.
567 ///
568 /// \returns true if the initialization sequence was ill-formed,
569 /// false otherwise.
570 bool Diagnose(Sema &S,
571 const InitializedEntity &Entity,
572 const InitializationKind &Kind,
573 Expr **Args, unsigned NumArgs);
574
575 /// \brief Determine the kind of initialization sequence computed.
576 enum SequenceKind getKind() const { return SequenceKind; }
577
578 /// \brief Set the kind of sequence computed.
579 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
580
581 /// \brief Determine whether the initialization sequence is valid.
582 operator bool() const { return SequenceKind != FailedSequence; }
583
584 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
585 step_iterator step_begin() const { return Steps.begin(); }
586 step_iterator step_end() const { return Steps.end(); }
587
588 /// \brief Add a new step in the initialization that resolves the address
589 /// of an overloaded function to a specific function declaration.
590 ///
591 /// \param Function the function to which the overloaded function reference
592 /// resolves.
593 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
594
595 /// \brief Add a new step in the initialization that performs a derived-to-
596 /// base cast.
597 ///
598 /// \param BaseType the base type to which we will be casting.
599 ///
600 /// \param IsLValue true if the result of this cast will be treated as
601 /// an lvalue.
602 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
603
604 /// \brief Add a new step binding a reference to an object.
605 ///
606 /// \param BindingTemporary true if we are binding a reference to a temporary
607 /// object (thereby extending its lifetime); false if we are binding to an
608 /// lvalue or an lvalue treated as an rvalue.
609 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
610
611 /// \brief Add a new step invoking a conversion function, which is either
612 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000613 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000614
615 /// \brief Add a new step that performs a qualification conversion to the
616 /// given type.
617 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
618
619 /// \brief Add a new step that applies an implicit conversion sequence.
620 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
621 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000622
623 /// \brief Add a list-initialiation step
624 void AddListInitializationStep(QualType T);
625
Douglas Gregor71d17402009-12-15 00:01:57 +0000626 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000627 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
628 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000629
630 /// \brief Add a zero-initialization step.
631 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000632
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000633 /// \brief Add a C assignment step.
634 //
635 // FIXME: It isn't clear whether this should ever be needed;
636 // ideally, we would handle everything needed in C in the common
637 // path. However, that isn't the case yet.
638 void AddCAssignmentStep(QualType T);
639
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000640 /// \brief Add a string init step.
641 void AddStringInitStep(QualType T);
642
Douglas Gregor20093b42009-12-09 23:02:17 +0000643 /// \brief Note that this initialization sequence failed.
644 void SetFailed(FailureKind Failure) {
645 SequenceKind = FailedSequence;
646 this->Failure = Failure;
647 }
648
649 /// \brief Note that this initialization sequence failed due to failed
650 /// overload resolution.
651 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
652
653 /// \brief Retrieve a reference to the candidate set when overload
654 /// resolution fails.
655 OverloadCandidateSet &getFailedCandidateSet() {
656 return FailedCandidateSet;
657 }
658
659 /// \brief Determine why initialization failed.
660 FailureKind getFailureKind() const {
661 assert(getKind() == FailedSequence && "Not an initialization failure!");
662 return Failure;
663 }
664};
665
666} // end namespace clang
667
668#endif // LLVM_CLANG_SEMA_INIT_H