blob: db987ec2961e43cb7561e68dd2c33b6b86961bcf [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,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000419 /// \brief An optional copy of a temporary object to another
420 /// temporary object, which is permitted (but not required) by
421 /// C++98/03 but not C++0x.
422 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000423 /// \brief Perform a user-defined conversion, either via a conversion
424 /// function or via a constructor.
425 SK_UserConversion,
426 /// \brief Perform a qualification conversion, producing an rvalue.
427 SK_QualificationConversionRValue,
428 /// \brief Perform a qualification conversion, producing an lvalue.
429 SK_QualificationConversionLValue,
430 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000431 SK_ConversionSequence,
432 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000433 SK_ListInitialization,
434 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000435 SK_ConstructorInitialization,
436 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000437 SK_ZeroInitialization,
438 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000439 SK_CAssignment,
440 /// \brief Initialization by string
441 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000442 };
443
444 /// \brief A single step in the initialization sequence.
445 class Step {
446 public:
447 /// \brief The kind of conversion or initialization step we are taking.
448 StepKind Kind;
449
450 // \brief The type that results from this initialization.
451 QualType Type;
452
453 union {
454 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
455 /// SK_UserConversion, the function that the expression should be
456 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000457 ///
458 /// Always a FunctionDecl.
459 /// For conversion decls, the naming class is the source type.
460 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000461 struct {
462 FunctionDecl *Function;
463 DeclAccessPair FoundDecl;
464 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000465
466 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
467 /// sequence
468 ImplicitConversionSequence *ICS;
469 };
470
471 void Destroy();
472 };
473
474private:
475 /// \brief The kind of initialization sequence computed.
476 enum SequenceKind SequenceKind;
477
478 /// \brief Steps taken by this initialization.
479 llvm::SmallVector<Step, 4> Steps;
480
481public:
482 /// \brief Describes why initialization failed.
483 enum FailureKind {
484 /// \brief Too many initializers provided for a reference.
485 FK_TooManyInitsForReference,
486 /// \brief Array must be initialized with an initializer list.
487 FK_ArrayNeedsInitList,
488 /// \brief Array must be initialized with an initializer list or a
489 /// string literal.
490 FK_ArrayNeedsInitListOrStringLiteral,
491 /// \brief Cannot resolve the address of an overloaded function.
492 FK_AddressOfOverloadFailed,
493 /// \brief Overloading due to reference initialization failed.
494 FK_ReferenceInitOverloadFailed,
495 /// \brief Non-const lvalue reference binding to a temporary.
496 FK_NonConstLValueReferenceBindingToTemporary,
497 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
498 /// type.
499 FK_NonConstLValueReferenceBindingToUnrelated,
500 /// \brief Rvalue reference binding to an lvalue.
501 FK_RValueReferenceBindingToLValue,
502 /// \brief Reference binding drops qualifiers.
503 FK_ReferenceInitDropsQualifiers,
504 /// \brief Reference binding failed.
505 FK_ReferenceInitFailed,
506 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000507 FK_ConversionFailed,
508 /// \brief Too many initializers for scalar
509 FK_TooManyInitsForScalar,
510 /// \brief Reference initialization from an initializer list
511 FK_ReferenceBindingToInitList,
512 /// \brief Initialization of some unused destination type with an
513 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000514 FK_InitListBadDestinationType,
515 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000516 FK_UserConversionOverloadFailed,
517 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000518 FK_ConstructorOverloadFailed,
519 /// \brief Default-initialization of a 'const' object.
520 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000521 };
522
523private:
524 /// \brief The reason why initialization failued.
525 FailureKind Failure;
526
527 /// \brief The failed result of overload resolution.
528 OverloadingResult FailedOverloadResult;
529
530 /// \brief The candidate set created when initialization failed.
531 OverloadCandidateSet FailedCandidateSet;
532
533public:
534 /// \brief Try to perform initialization of the given entity, creating a
535 /// record of the steps required to perform the initialization.
536 ///
537 /// The generated initialization sequence will either contain enough
538 /// information to diagnose
539 ///
540 /// \param S the semantic analysis object.
541 ///
542 /// \param Entity the entity being initialized.
543 ///
544 /// \param Kind the kind of initialization being performed.
545 ///
546 /// \param Args the argument(s) provided for initialization.
547 ///
548 /// \param NumArgs the number of arguments provided for initialization.
549 InitializationSequence(Sema &S,
550 const InitializedEntity &Entity,
551 const InitializationKind &Kind,
552 Expr **Args,
553 unsigned NumArgs);
554
555 ~InitializationSequence();
556
557 /// \brief Perform the actual initialization of the given entity based on
558 /// the computed initialization sequence.
559 ///
560 /// \param S the semantic analysis object.
561 ///
562 /// \param Entity the entity being initialized.
563 ///
564 /// \param Kind the kind of initialization being performed.
565 ///
566 /// \param Args the argument(s) provided for initialization, ownership of
567 /// which is transfered into the routine.
568 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000569 /// \param ResultType if non-NULL, will be set to the type of the
570 /// initialized object, which is the type of the declaration in most
571 /// cases. However, when the initialized object is a variable of
572 /// incomplete array type and the initializer is an initializer
573 /// list, this type will be set to the completed array type.
574 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000575 /// \returns an expression that performs the actual object initialization, if
576 /// the initialization is well-formed. Otherwise, emits diagnostics
577 /// and returns an invalid expression.
578 Action::OwningExprResult Perform(Sema &S,
579 const InitializedEntity &Entity,
580 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000581 Action::MultiExprArg Args,
582 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000583
584 /// \brief Diagnose an potentially-invalid initialization sequence.
585 ///
586 /// \returns true if the initialization sequence was ill-formed,
587 /// false otherwise.
588 bool Diagnose(Sema &S,
589 const InitializedEntity &Entity,
590 const InitializationKind &Kind,
591 Expr **Args, unsigned NumArgs);
592
593 /// \brief Determine the kind of initialization sequence computed.
594 enum SequenceKind getKind() const { return SequenceKind; }
595
596 /// \brief Set the kind of sequence computed.
597 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
598
599 /// \brief Determine whether the initialization sequence is valid.
600 operator bool() const { return SequenceKind != FailedSequence; }
601
602 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
603 step_iterator step_begin() const { return Steps.begin(); }
604 step_iterator step_end() const { return Steps.end(); }
605
Douglas Gregorb70cf442010-03-26 20:14:36 +0000606 /// \brief Determine whether this initialization is a direct reference
607 /// binding (C++ [dcl.init.ref]).
608 bool isDirectReferenceBinding() const;
609
610 /// \brief Determine whether this initialization failed due to an ambiguity.
611 bool isAmbiguous() const;
612
Douglas Gregord6e44a32010-04-16 22:09:46 +0000613 /// \brief Determine whether this initialization is direct call to a
614 /// constructor.
615 bool isConstructorInitialization() const;
616
Douglas Gregor20093b42009-12-09 23:02:17 +0000617 /// \brief Add a new step in the initialization that resolves the address
618 /// of an overloaded function to a specific function declaration.
619 ///
620 /// \param Function the function to which the overloaded function reference
621 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000622 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
623 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000624
625 /// \brief Add a new step in the initialization that performs a derived-to-
626 /// base cast.
627 ///
628 /// \param BaseType the base type to which we will be casting.
629 ///
630 /// \param IsLValue true if the result of this cast will be treated as
631 /// an lvalue.
632 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
633
634 /// \brief Add a new step binding a reference to an object.
635 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000636 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000637 /// object (thereby extending its lifetime); false if we are binding to an
638 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000639 ///
640 /// \param UnnecessaryCopy True if we should check for a copy
641 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000642 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000643
644 /// \brief Add a new step that makes an extraneous copy of the input
645 /// to a temporary of the same class type.
646 ///
647 /// This extraneous copy only occurs during reference binding in
648 /// C++98/03, where we are permitted (but not required) to introduce
649 /// an extra copy. At a bare minimum, we must check that we could
650 /// call the copy constructor, and produce a diagnostic if the copy
651 /// constructor is inaccessible or no copy constructor matches.
652 //
653 /// \param T The type of the temporary being created.
654 void AddExtraneousCopyToTemporary(QualType T);
655
Douglas Gregor20093b42009-12-09 23:02:17 +0000656 /// \brief Add a new step invoking a conversion function, which is either
657 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000658 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000659 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000660 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000661
662 /// \brief Add a new step that performs a qualification conversion to the
663 /// given type.
664 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
665
666 /// \brief Add a new step that applies an implicit conversion sequence.
667 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
668 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000669
670 /// \brief Add a list-initialiation step
671 void AddListInitializationStep(QualType T);
672
Douglas Gregor71d17402009-12-15 00:01:57 +0000673 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000674 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000675 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000676 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000677
678 /// \brief Add a zero-initialization step.
679 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000680
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000681 /// \brief Add a C assignment step.
682 //
683 // FIXME: It isn't clear whether this should ever be needed;
684 // ideally, we would handle everything needed in C in the common
685 // path. However, that isn't the case yet.
686 void AddCAssignmentStep(QualType T);
687
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000688 /// \brief Add a string init step.
689 void AddStringInitStep(QualType T);
690
Douglas Gregor20093b42009-12-09 23:02:17 +0000691 /// \brief Note that this initialization sequence failed.
692 void SetFailed(FailureKind Failure) {
693 SequenceKind = FailedSequence;
694 this->Failure = Failure;
695 }
696
697 /// \brief Note that this initialization sequence failed due to failed
698 /// overload resolution.
699 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
700
701 /// \brief Retrieve a reference to the candidate set when overload
702 /// resolution fails.
703 OverloadCandidateSet &getFailedCandidateSet() {
704 return FailedCandidateSet;
705 }
706
707 /// \brief Determine why initialization failed.
708 FailureKind getFailureKind() const {
709 assert(getKind() == FailedSequence && "Not an initialization failure!");
710 return Failure;
711 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000712
713 /// \brief Dump a representation of this initialization sequence to
714 /// the given stream, for debugging purposes.
715 void dump(llvm::raw_ostream &OS) const;
716
717 /// \brief Dump a representation of this initialization sequence to
718 /// standard error, for debugging purposes.
719 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000720};
721
722} // end namespace clang
723
724#endif // LLVM_CLANG_SEMA_INIT_H