blob: 7c6327f8c7fab2016cac53c92c5f47f74cee2b82 [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"
John McCallb13b7372010-02-01 03:16:54 +000018#include "clang/AST/UnresolvedSet.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000019#include "clang/Parse/Action.h"
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/PointerIntPair.h"
22#include "llvm/ADT/SmallVector.h"
23#include <cassert>
24
Douglas Gregorde4b1d82010-01-29 19:14:02 +000025namespace llvm {
26 class raw_ostream;
27}
28
Douglas Gregor20093b42009-12-09 23:02:17 +000029namespace clang {
30
31class CXXBaseSpecifier;
32class DeclaratorDecl;
33class DeclaratorInfo;
34class FieldDecl;
35class FunctionDecl;
36class ParmVarDecl;
37class Sema;
38class TypeLoc;
39class VarDecl;
40
41/// \brief Describes an entity that is being initialized.
42class InitializedEntity {
43public:
44 /// \brief Specifies the kind of entity being initialized.
45 enum EntityKind {
46 /// \brief The entity being initialized is a variable.
47 EK_Variable,
48 /// \brief The entity being initialized is a function parameter.
49 EK_Parameter,
50 /// \brief The entity being initialized is the result of a function call.
51 EK_Result,
52 /// \brief The entity being initialized is an exception object that
53 /// is being thrown.
54 EK_Exception,
Anders Carlssona729bbb2010-01-23 05:47:27 +000055 /// \brief The entity being initialized is a non-static data member
56 /// subobject.
57 EK_Member,
58 /// \brief The entity being initialized is an element of an array.
59 EK_ArrayElement,
Douglas Gregor18ef5e22009-12-18 05:02:21 +000060 /// \brief The entity being initialized is an object (or array of
61 /// objects) allocated via new.
62 EK_New,
Douglas Gregor20093b42009-12-09 23:02:17 +000063 /// \brief The entity being initialized is a temporary object.
64 EK_Temporary,
65 /// \brief The entity being initialized is a base member subobject.
66 EK_Base,
Anders Carlssond3d824d2010-01-23 04:34:47 +000067 /// \brief The entity being initialized is an element of a vector.
Douglas Gregorcb57fb92009-12-16 06:35:08 +000068 /// or vector.
Anders Carlssond3d824d2010-01-23 04:34:47 +000069 EK_VectorElement
Douglas Gregor20093b42009-12-09 23:02:17 +000070 };
71
72private:
73 /// \brief The kind of entity being initialized.
74 EntityKind Kind;
75
Douglas Gregorcb57fb92009-12-16 06:35:08 +000076 /// \brief If non-NULL, the parent entity in which this
77 /// initialization occurs.
78 const InitializedEntity *Parent;
79
Douglas Gregord6542d82009-12-22 15:35:07 +000080 /// \brief The type of the object or reference being initialized.
81 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000082
83 union {
84 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
85 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
86 DeclaratorDecl *VariableOrMember;
87
Douglas Gregor18ef5e22009-12-18 05:02:21 +000088 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
89 /// location of the 'return', 'throw', or 'new' keyword,
90 /// respectively. When Kind == EK_Temporary, the location where
91 /// the temporary is being created.
Douglas Gregor20093b42009-12-09 23:02:17 +000092 unsigned Location;
93
94 /// \brief When Kind == EK_Base, the base specifier that provides the
95 /// base class.
96 CXXBaseSpecifier *Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000097
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000098 /// \brief When Kind = EK_ArrayElement or EK_VectorElement, the
99 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000100 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000101 };
102
103 InitializedEntity() { }
104
105 /// \brief Create the initialization entity for a variable.
106 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000107 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
108 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000109
110 /// \brief Create the initialization entity for a parameter.
111 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000112 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000113 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000114
Douglas Gregora188ff22009-12-22 16:09:06 +0000115 /// \brief Create the initialization entity for the result of a
116 /// function, throwing an object, performing an explicit cast, or
117 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000118 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
119 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000120
121 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000122 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000123 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
124 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000125
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000126 /// \brief Create the initialization entity for an array element.
127 InitializedEntity(ASTContext &Context, unsigned Index,
128 const InitializedEntity &Parent);
129
Douglas Gregor20093b42009-12-09 23:02:17 +0000130public:
131 /// \brief Create the initialization entity for a variable.
132 static InitializedEntity InitializeVariable(VarDecl *Var) {
133 return InitializedEntity(Var);
134 }
135
136 /// \brief Create the initialization entity for a parameter.
137 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
138 return InitializedEntity(Parm);
139 }
140
Douglas Gregora188ff22009-12-22 16:09:06 +0000141 /// \brief Create the initialization entity for a parameter that is
142 /// only known by its type.
143 static InitializedEntity InitializeParameter(QualType Type) {
144 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
145 }
146
Douglas Gregor20093b42009-12-09 23:02:17 +0000147 /// \brief Create the initialization entity for the result of a function.
148 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000149 QualType Type) {
150 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000151 }
152
153 /// \brief Create the initialization entity for an exception object.
154 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000155 QualType Type) {
156 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000157 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000158
159 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000160 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
161 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000162 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000163
164 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000165 static InitializedEntity InitializeTemporary(QualType Type) {
166 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000167 }
168
169 /// \brief Create the initialization entity for a base class subobject.
170 static InitializedEntity InitializeBase(ASTContext &Context,
171 CXXBaseSpecifier *Base);
172
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000173 /// \brief Create the initialization entity for a member subobject.
174 static InitializedEntity InitializeMember(FieldDecl *Member,
175 const InitializedEntity *Parent = 0) {
176 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000177 }
178
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000179 /// \brief Create the initialization entity for an array element.
180 static InitializedEntity InitializeElement(ASTContext &Context,
181 unsigned Index,
182 const InitializedEntity &Parent) {
183 return InitializedEntity(Context, Index, Parent);
184 }
185
Douglas Gregor20093b42009-12-09 23:02:17 +0000186 /// \brief Determine the kind of initialization.
187 EntityKind getKind() const { return Kind; }
188
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000189 /// \brief Retrieve the parent of the entity being initialized, when
190 /// the initialization itself is occuring within the context of a
191 /// larger initialization.
192 const InitializedEntity *getParent() const { return Parent; }
193
Douglas Gregor20093b42009-12-09 23:02:17 +0000194 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000195 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000196
Douglas Gregor99a2e602009-12-16 01:38:02 +0000197 /// \brief Retrieve the name of the entity being initialized.
198 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000199
200 /// \brief Retrieve the variable, parameter, or field being
201 /// initialized.
202 DeclaratorDecl *getDecl() const;
203
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000204 /// \brief Retrieve the base specifier.
205 CXXBaseSpecifier *getBaseSpecifier() const {
206 assert(getKind() == EK_Base && "Not a base specifier");
207 return Base;
208 }
209
Douglas Gregor20093b42009-12-09 23:02:17 +0000210 /// \brief Determine the location of the 'return' keyword when initializing
211 /// the result of a function call.
212 SourceLocation getReturnLoc() const {
213 assert(getKind() == EK_Result && "No 'return' location!");
214 return SourceLocation::getFromRawEncoding(Location);
215 }
216
217 /// \brief Determine the location of the 'throw' keyword when initializing
218 /// an exception object.
219 SourceLocation getThrowLoc() const {
220 assert(getKind() == EK_Exception && "No 'throw' location!");
221 return SourceLocation::getFromRawEncoding(Location);
222 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000223
224 /// \brief If this is already the initializer for an array or vector
225 /// element, sets the element index.
226 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000227 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000228 this->Index = Index;
229 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000230};
231
232/// \brief Describes the kind of initialization being performed, along with
233/// location information for tokens related to the initialization (equal sign,
234/// parentheses).
235class InitializationKind {
236public:
237 /// \brief The kind of initialization being performed.
238 enum InitKind {
239 IK_Direct, ///< Direct initialization
240 IK_Copy, ///< Copy initialization
241 IK_Default, ///< Default initialization
242 IK_Value ///< Value initialization
243 };
244
245private:
246 /// \brief The kind of initialization that we're storing.
247 enum StoredInitKind {
248 SIK_Direct = IK_Direct, ///< Direct initialization
249 SIK_Copy = IK_Copy, ///< Copy initialization
250 SIK_Default = IK_Default, ///< Default initialization
251 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000252 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000253 SIK_DirectCast, ///< Direct initialization due to a cast
254 /// \brief Direct initialization due to a C-style or functional cast.
255 SIK_DirectCStyleOrFunctionalCast
256 };
257
258 /// \brief The kind of initialization being performed.
259 StoredInitKind Kind;
260
261 /// \brief The source locations involved in the initialization.
262 SourceLocation Locations[3];
263
264 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
265 SourceLocation Loc2, SourceLocation Loc3)
266 : Kind(Kind)
267 {
268 Locations[0] = Loc1;
269 Locations[1] = Loc2;
270 Locations[2] = Loc3;
271 }
272
273public:
274 /// \brief Create a direct initialization.
275 static InitializationKind CreateDirect(SourceLocation InitLoc,
276 SourceLocation LParenLoc,
277 SourceLocation RParenLoc) {
278 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
279 }
280
281 /// \brief Create a direct initialization due to a cast.
282 static InitializationKind CreateCast(SourceRange TypeRange,
283 bool IsCStyleCast) {
284 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
285 : SIK_DirectCast,
286 TypeRange.getBegin(), TypeRange.getBegin(),
287 TypeRange.getEnd());
288 }
289
290 /// \brief Create a copy initialization.
291 static InitializationKind CreateCopy(SourceLocation InitLoc,
292 SourceLocation EqualLoc) {
293 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
294 }
295
296 /// \brief Create a default initialization.
297 static InitializationKind CreateDefault(SourceLocation InitLoc) {
298 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
299 }
300
301 /// \brief Create a value initialization.
302 static InitializationKind CreateValue(SourceLocation InitLoc,
303 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000304 SourceLocation RParenLoc,
305 bool isImplicit = false) {
306 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
307 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000308 }
309
310 /// \brief Determine the initialization kind.
311 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000312 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000313 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000314 if (Kind == SIK_ImplicitValue)
315 return IK_Value;
316
Douglas Gregor20093b42009-12-09 23:02:17 +0000317 return (InitKind)Kind;
318 }
319
320 /// \brief Determine whether this initialization is an explicit cast.
321 bool isExplicitCast() const {
322 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
323 }
324
325 /// \brief Determine whether this initialization is a C-style cast.
326 bool isCStyleOrFunctionalCast() const {
327 return Kind == SIK_DirectCStyleOrFunctionalCast;
328 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000329
330 /// \brief Determine whether this initialization is an implicit
331 /// value-initialization, e.g., as occurs during aggregate
332 /// initialization.
333 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
334
Douglas Gregor20093b42009-12-09 23:02:17 +0000335 /// \brief Retrieve the location at which initialization is occurring.
336 SourceLocation getLocation() const { return Locations[0]; }
337
338 /// \brief Retrieve the source range that covers the initialization.
339 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000340 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000341 }
342
343 /// \brief Retrieve the location of the equal sign for copy initialization
344 /// (if present).
345 SourceLocation getEqualLoc() const {
346 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
347 return Locations[1];
348 }
349
350 /// \brief Retrieve the source range containing the locations of the open
351 /// and closing parentheses for value and direct initializations.
352 SourceRange getParenRange() const {
353 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
354 "Only direct- and value-initialization have parentheses");
355 return SourceRange(Locations[1], Locations[2]);
356 }
357};
358
359/// \brief Describes the sequence of initializations required to initialize
360/// a given object or reference with a set of arguments.
361class InitializationSequence {
362public:
363 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000364 ///
365 /// FIXME: Much of this information is in the initialization steps... why is
366 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000367 enum SequenceKind {
368 /// \brief A failed initialization sequence. The failure kind tells what
369 /// happened.
370 FailedSequence = 0,
371
372 /// \brief A dependent initialization, which could not be
373 /// type-checked due to the presence of dependent types or
374 /// dependently-type expressions.
375 DependentSequence,
376
Douglas Gregor4a520a22009-12-14 17:27:33 +0000377 /// \brief A user-defined conversion sequence.
378 UserDefinedConversion,
379
Douglas Gregor51c56d62009-12-14 20:49:26 +0000380 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000381 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000382
Douglas Gregor20093b42009-12-09 23:02:17 +0000383 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000384 ReferenceBinding,
385
386 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000387 ListInitialization,
388
389 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000390 ZeroInitialization,
391
392 /// \brief No initialization required.
393 NoInitialization,
394
395 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000396 StandardConversion,
397
398 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000399 CAssignment,
400
401 /// \brief String initialization
402 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000403 };
404
405 /// \brief Describes the kind of a particular step in an initialization
406 /// sequence.
407 enum StepKind {
408 /// \brief Resolve the address of an overloaded function to a specific
409 /// function declaration.
410 SK_ResolveAddressOfOverloadedFunction,
411 /// \brief Perform a derived-to-base cast, producing an rvalue.
412 SK_CastDerivedToBaseRValue,
413 /// \brief Perform a derived-to-base cast, producing an lvalue.
414 SK_CastDerivedToBaseLValue,
415 /// \brief Reference binding to an lvalue.
416 SK_BindReference,
417 /// \brief Reference binding to a temporary.
418 SK_BindReferenceToTemporary,
419 /// \brief Perform a user-defined conversion, either via a conversion
420 /// function or via a constructor.
421 SK_UserConversion,
422 /// \brief Perform a qualification conversion, producing an rvalue.
423 SK_QualificationConversionRValue,
424 /// \brief Perform a qualification conversion, producing an lvalue.
425 SK_QualificationConversionLValue,
426 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000427 SK_ConversionSequence,
428 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000429 SK_ListInitialization,
430 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000431 SK_ConstructorInitialization,
432 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000433 SK_ZeroInitialization,
434 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000435 SK_CAssignment,
436 /// \brief Initialization by string
437 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000438 };
439
440 /// \brief A single step in the initialization sequence.
441 class Step {
442 public:
443 /// \brief The kind of conversion or initialization step we are taking.
444 StepKind Kind;
445
446 // \brief The type that results from this initialization.
447 QualType Type;
448
449 union {
450 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
451 /// SK_UserConversion, the function that the expression should be
452 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000453 ///
454 /// Always a FunctionDecl.
455 /// For conversion decls, the naming class is the source type.
456 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000457 struct {
458 FunctionDecl *Function;
459 DeclAccessPair FoundDecl;
460 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000461
462 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
463 /// sequence
464 ImplicitConversionSequence *ICS;
465 };
466
467 void Destroy();
468 };
469
470private:
471 /// \brief The kind of initialization sequence computed.
472 enum SequenceKind SequenceKind;
473
474 /// \brief Steps taken by this initialization.
475 llvm::SmallVector<Step, 4> Steps;
476
477public:
478 /// \brief Describes why initialization failed.
479 enum FailureKind {
480 /// \brief Too many initializers provided for a reference.
481 FK_TooManyInitsForReference,
482 /// \brief Array must be initialized with an initializer list.
483 FK_ArrayNeedsInitList,
484 /// \brief Array must be initialized with an initializer list or a
485 /// string literal.
486 FK_ArrayNeedsInitListOrStringLiteral,
487 /// \brief Cannot resolve the address of an overloaded function.
488 FK_AddressOfOverloadFailed,
489 /// \brief Overloading due to reference initialization failed.
490 FK_ReferenceInitOverloadFailed,
491 /// \brief Non-const lvalue reference binding to a temporary.
492 FK_NonConstLValueReferenceBindingToTemporary,
493 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
494 /// type.
495 FK_NonConstLValueReferenceBindingToUnrelated,
496 /// \brief Rvalue reference binding to an lvalue.
497 FK_RValueReferenceBindingToLValue,
498 /// \brief Reference binding drops qualifiers.
499 FK_ReferenceInitDropsQualifiers,
500 /// \brief Reference binding failed.
501 FK_ReferenceInitFailed,
502 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000503 FK_ConversionFailed,
504 /// \brief Too many initializers for scalar
505 FK_TooManyInitsForScalar,
506 /// \brief Reference initialization from an initializer list
507 FK_ReferenceBindingToInitList,
508 /// \brief Initialization of some unused destination type with an
509 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000510 FK_InitListBadDestinationType,
511 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000512 FK_UserConversionOverloadFailed,
513 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000514 FK_ConstructorOverloadFailed,
515 /// \brief Default-initialization of a 'const' object.
516 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000517 };
518
519private:
520 /// \brief The reason why initialization failued.
521 FailureKind Failure;
522
523 /// \brief The failed result of overload resolution.
524 OverloadingResult FailedOverloadResult;
525
526 /// \brief The candidate set created when initialization failed.
527 OverloadCandidateSet FailedCandidateSet;
528
529public:
530 /// \brief Try to perform initialization of the given entity, creating a
531 /// record of the steps required to perform the initialization.
532 ///
533 /// The generated initialization sequence will either contain enough
534 /// information to diagnose
535 ///
536 /// \param S the semantic analysis object.
537 ///
538 /// \param Entity the entity being initialized.
539 ///
540 /// \param Kind the kind of initialization being performed.
541 ///
542 /// \param Args the argument(s) provided for initialization.
543 ///
544 /// \param NumArgs the number of arguments provided for initialization.
545 InitializationSequence(Sema &S,
546 const InitializedEntity &Entity,
547 const InitializationKind &Kind,
548 Expr **Args,
549 unsigned NumArgs);
550
551 ~InitializationSequence();
552
553 /// \brief Perform the actual initialization of the given entity based on
554 /// the computed initialization sequence.
555 ///
556 /// \param S the semantic analysis object.
557 ///
558 /// \param Entity the entity being initialized.
559 ///
560 /// \param Kind the kind of initialization being performed.
561 ///
562 /// \param Args the argument(s) provided for initialization, ownership of
563 /// which is transfered into the routine.
564 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000565 /// \param ResultType if non-NULL, will be set to the type of the
566 /// initialized object, which is the type of the declaration in most
567 /// cases. However, when the initialized object is a variable of
568 /// incomplete array type and the initializer is an initializer
569 /// list, this type will be set to the completed array type.
570 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000571 /// \returns an expression that performs the actual object initialization, if
572 /// the initialization is well-formed. Otherwise, emits diagnostics
573 /// and returns an invalid expression.
574 Action::OwningExprResult Perform(Sema &S,
575 const InitializedEntity &Entity,
576 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000577 Action::MultiExprArg Args,
578 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000579
580 /// \brief Diagnose an potentially-invalid initialization sequence.
581 ///
582 /// \returns true if the initialization sequence was ill-formed,
583 /// false otherwise.
584 bool Diagnose(Sema &S,
585 const InitializedEntity &Entity,
586 const InitializationKind &Kind,
587 Expr **Args, unsigned NumArgs);
588
589 /// \brief Determine the kind of initialization sequence computed.
590 enum SequenceKind getKind() const { return SequenceKind; }
591
592 /// \brief Set the kind of sequence computed.
593 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
594
595 /// \brief Determine whether the initialization sequence is valid.
596 operator bool() const { return SequenceKind != FailedSequence; }
597
598 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
599 step_iterator step_begin() const { return Steps.begin(); }
600 step_iterator step_end() const { return Steps.end(); }
601
Douglas Gregorb70cf442010-03-26 20:14:36 +0000602 /// \brief Determine whether this initialization is a direct reference
603 /// binding (C++ [dcl.init.ref]).
604 bool isDirectReferenceBinding() const;
605
606 /// \brief Determine whether this initialization failed due to an ambiguity.
607 bool isAmbiguous() const;
608
Douglas Gregor20093b42009-12-09 23:02:17 +0000609 /// \brief Add a new step in the initialization that resolves the address
610 /// of an overloaded function to a specific function declaration.
611 ///
612 /// \param Function the function to which the overloaded function reference
613 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000614 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
615 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000616
617 /// \brief Add a new step in the initialization that performs a derived-to-
618 /// base cast.
619 ///
620 /// \param BaseType the base type to which we will be casting.
621 ///
622 /// \param IsLValue true if the result of this cast will be treated as
623 /// an lvalue.
624 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
625
626 /// \brief Add a new step binding a reference to an object.
627 ///
628 /// \param BindingTemporary true if we are binding a reference to a temporary
629 /// object (thereby extending its lifetime); false if we are binding to an
630 /// lvalue or an lvalue treated as an rvalue.
631 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
632
633 /// \brief Add a new step invoking a conversion function, which is either
634 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000635 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000636 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000637 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000638
639 /// \brief Add a new step that performs a qualification conversion to the
640 /// given type.
641 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
642
643 /// \brief Add a new step that applies an implicit conversion sequence.
644 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
645 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000646
647 /// \brief Add a list-initialiation step
648 void AddListInitializationStep(QualType T);
649
Douglas Gregor71d17402009-12-15 00:01:57 +0000650 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000651 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000652 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000653 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000654
655 /// \brief Add a zero-initialization step.
656 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000657
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000658 /// \brief Add a C assignment step.
659 //
660 // FIXME: It isn't clear whether this should ever be needed;
661 // ideally, we would handle everything needed in C in the common
662 // path. However, that isn't the case yet.
663 void AddCAssignmentStep(QualType T);
664
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000665 /// \brief Add a string init step.
666 void AddStringInitStep(QualType T);
667
Douglas Gregor20093b42009-12-09 23:02:17 +0000668 /// \brief Note that this initialization sequence failed.
669 void SetFailed(FailureKind Failure) {
670 SequenceKind = FailedSequence;
671 this->Failure = Failure;
672 }
673
674 /// \brief Note that this initialization sequence failed due to failed
675 /// overload resolution.
676 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
677
678 /// \brief Retrieve a reference to the candidate set when overload
679 /// resolution fails.
680 OverloadCandidateSet &getFailedCandidateSet() {
681 return FailedCandidateSet;
682 }
683
684 /// \brief Determine why initialization failed.
685 FailureKind getFailureKind() const {
686 assert(getKind() == FailedSequence && "Not an initialization failure!");
687 return Failure;
688 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000689
690 /// \brief Dump a representation of this initialization sequence to
691 /// the given stream, for debugging purposes.
692 void dump(llvm::raw_ostream &OS) const;
693
694 /// \brief Dump a representation of this initialization sequence to
695 /// standard error, for debugging purposes.
696 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000697};
698
699} // end namespace clang
700
701#endif // LLVM_CLANG_SEMA_INIT_H