blob: 1987ad0734062e7418901c5c5f216a124a3be1be [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.
392 CAssignment
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
425 SK_CAssignment
Douglas Gregor20093b42009-12-09 23:02:17 +0000426 };
427
428 /// \brief A single step in the initialization sequence.
429 class Step {
430 public:
431 /// \brief The kind of conversion or initialization step we are taking.
432 StepKind Kind;
433
434 // \brief The type that results from this initialization.
435 QualType Type;
436
437 union {
438 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
439 /// SK_UserConversion, the function that the expression should be
440 /// resolved to or the conversion function to call, respectively.
441 FunctionDecl *Function;
442
443 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
444 /// sequence
445 ImplicitConversionSequence *ICS;
446 };
447
448 void Destroy();
449 };
450
451private:
452 /// \brief The kind of initialization sequence computed.
453 enum SequenceKind SequenceKind;
454
455 /// \brief Steps taken by this initialization.
456 llvm::SmallVector<Step, 4> Steps;
457
458public:
459 /// \brief Describes why initialization failed.
460 enum FailureKind {
461 /// \brief Too many initializers provided for a reference.
462 FK_TooManyInitsForReference,
463 /// \brief Array must be initialized with an initializer list.
464 FK_ArrayNeedsInitList,
465 /// \brief Array must be initialized with an initializer list or a
466 /// string literal.
467 FK_ArrayNeedsInitListOrStringLiteral,
468 /// \brief Cannot resolve the address of an overloaded function.
469 FK_AddressOfOverloadFailed,
470 /// \brief Overloading due to reference initialization failed.
471 FK_ReferenceInitOverloadFailed,
472 /// \brief Non-const lvalue reference binding to a temporary.
473 FK_NonConstLValueReferenceBindingToTemporary,
474 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
475 /// type.
476 FK_NonConstLValueReferenceBindingToUnrelated,
477 /// \brief Rvalue reference binding to an lvalue.
478 FK_RValueReferenceBindingToLValue,
479 /// \brief Reference binding drops qualifiers.
480 FK_ReferenceInitDropsQualifiers,
481 /// \brief Reference binding failed.
482 FK_ReferenceInitFailed,
483 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000484 FK_ConversionFailed,
485 /// \brief Too many initializers for scalar
486 FK_TooManyInitsForScalar,
487 /// \brief Reference initialization from an initializer list
488 FK_ReferenceBindingToInitList,
489 /// \brief Initialization of some unused destination type with an
490 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000491 FK_InitListBadDestinationType,
492 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000493 FK_UserConversionOverloadFailed,
494 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000495 FK_ConstructorOverloadFailed,
496 /// \brief Default-initialization of a 'const' object.
497 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000498 };
499
500private:
501 /// \brief The reason why initialization failued.
502 FailureKind Failure;
503
504 /// \brief The failed result of overload resolution.
505 OverloadingResult FailedOverloadResult;
506
507 /// \brief The candidate set created when initialization failed.
508 OverloadCandidateSet FailedCandidateSet;
509
510public:
511 /// \brief Try to perform initialization of the given entity, creating a
512 /// record of the steps required to perform the initialization.
513 ///
514 /// The generated initialization sequence will either contain enough
515 /// information to diagnose
516 ///
517 /// \param S the semantic analysis object.
518 ///
519 /// \param Entity the entity being initialized.
520 ///
521 /// \param Kind the kind of initialization being performed.
522 ///
523 /// \param Args the argument(s) provided for initialization.
524 ///
525 /// \param NumArgs the number of arguments provided for initialization.
526 InitializationSequence(Sema &S,
527 const InitializedEntity &Entity,
528 const InitializationKind &Kind,
529 Expr **Args,
530 unsigned NumArgs);
531
532 ~InitializationSequence();
533
534 /// \brief Perform the actual initialization of the given entity based on
535 /// the computed initialization sequence.
536 ///
537 /// \param S the semantic analysis object.
538 ///
539 /// \param Entity the entity being initialized.
540 ///
541 /// \param Kind the kind of initialization being performed.
542 ///
543 /// \param Args the argument(s) provided for initialization, ownership of
544 /// which is transfered into the routine.
545 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000546 /// \param ResultType if non-NULL, will be set to the type of the
547 /// initialized object, which is the type of the declaration in most
548 /// cases. However, when the initialized object is a variable of
549 /// incomplete array type and the initializer is an initializer
550 /// list, this type will be set to the completed array type.
551 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000552 /// \returns an expression that performs the actual object initialization, if
553 /// the initialization is well-formed. Otherwise, emits diagnostics
554 /// and returns an invalid expression.
555 Action::OwningExprResult Perform(Sema &S,
556 const InitializedEntity &Entity,
557 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000558 Action::MultiExprArg Args,
559 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000560
561 /// \brief Diagnose an potentially-invalid initialization sequence.
562 ///
563 /// \returns true if the initialization sequence was ill-formed,
564 /// false otherwise.
565 bool Diagnose(Sema &S,
566 const InitializedEntity &Entity,
567 const InitializationKind &Kind,
568 Expr **Args, unsigned NumArgs);
569
570 /// \brief Determine the kind of initialization sequence computed.
571 enum SequenceKind getKind() const { return SequenceKind; }
572
573 /// \brief Set the kind of sequence computed.
574 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
575
576 /// \brief Determine whether the initialization sequence is valid.
577 operator bool() const { return SequenceKind != FailedSequence; }
578
579 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
580 step_iterator step_begin() const { return Steps.begin(); }
581 step_iterator step_end() const { return Steps.end(); }
582
583 /// \brief Add a new step in the initialization that resolves the address
584 /// of an overloaded function to a specific function declaration.
585 ///
586 /// \param Function the function to which the overloaded function reference
587 /// resolves.
588 void AddAddressOverloadResolutionStep(FunctionDecl *Function);
589
590 /// \brief Add a new step in the initialization that performs a derived-to-
591 /// base cast.
592 ///
593 /// \param BaseType the base type to which we will be casting.
594 ///
595 /// \param IsLValue true if the result of this cast will be treated as
596 /// an lvalue.
597 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
598
599 /// \brief Add a new step binding a reference to an object.
600 ///
601 /// \param BindingTemporary true if we are binding a reference to a temporary
602 /// object (thereby extending its lifetime); false if we are binding to an
603 /// lvalue or an lvalue treated as an rvalue.
604 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
605
606 /// \brief Add a new step invoking a conversion function, which is either
607 /// a constructor or a conversion function.
Eli Friedman03981012009-12-11 02:42:07 +0000608 void AddUserConversionStep(FunctionDecl *Function, QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000609
610 /// \brief Add a new step that performs a qualification conversion to the
611 /// given type.
612 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
613
614 /// \brief Add a new step that applies an implicit conversion sequence.
615 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
616 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000617
618 /// \brief Add a list-initialiation step
619 void AddListInitializationStep(QualType T);
620
Douglas Gregor71d17402009-12-15 00:01:57 +0000621 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000622 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
623 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000624
625 /// \brief Add a zero-initialization step.
626 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000627
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000628 /// \brief Add a C assignment step.
629 //
630 // FIXME: It isn't clear whether this should ever be needed;
631 // ideally, we would handle everything needed in C in the common
632 // path. However, that isn't the case yet.
633 void AddCAssignmentStep(QualType T);
634
Douglas Gregor20093b42009-12-09 23:02:17 +0000635 /// \brief Note that this initialization sequence failed.
636 void SetFailed(FailureKind Failure) {
637 SequenceKind = FailedSequence;
638 this->Failure = Failure;
639 }
640
641 /// \brief Note that this initialization sequence failed due to failed
642 /// overload resolution.
643 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
644
645 /// \brief Retrieve a reference to the candidate set when overload
646 /// resolution fails.
647 OverloadCandidateSet &getFailedCandidateSet() {
648 return FailedCandidateSet;
649 }
650
651 /// \brief Determine why initialization failed.
652 FailureKind getFailureKind() const {
653 assert(getKind() == FailedSequence && "Not an initialization failure!");
654 return Failure;
655 }
656};
657
658} // end namespace clang
659
660#endif // LLVM_CLANG_SEMA_INIT_H