blob: bfbb0f4049c28e8ccfcb5bfdcb2e352df82c6f1a [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
Anders Carlsson711f34a2010-04-21 19:52:01 +000095 /// base class. The lower bit specifies whether the base is an inherited
96 /// virtual base.
97 uintptr_t Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +000098
Anders Carlsson711f34a2010-04-21 19:52:01 +000099 /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the
100 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000101 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000102 };
103
104 InitializedEntity() { }
105
106 /// \brief Create the initialization entity for a variable.
107 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000108 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
109 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000110
111 /// \brief Create the initialization entity for a parameter.
112 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000113 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000114 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000115
Douglas Gregora188ff22009-12-22 16:09:06 +0000116 /// \brief Create the initialization entity for the result of a
117 /// function, throwing an object, performing an explicit cast, or
118 /// initializing a parameter for which there is no declaration.
Douglas Gregord6542d82009-12-22 15:35:07 +0000119 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type)
120 : Kind(Kind), Parent(0), Type(Type), Location(Loc.getRawEncoding()) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000121
122 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000123 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000124 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
125 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000126
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000127 /// \brief Create the initialization entity for an array element.
128 InitializedEntity(ASTContext &Context, unsigned Index,
129 const InitializedEntity &Parent);
130
Douglas Gregor20093b42009-12-09 23:02:17 +0000131public:
132 /// \brief Create the initialization entity for a variable.
133 static InitializedEntity InitializeVariable(VarDecl *Var) {
134 return InitializedEntity(Var);
135 }
136
137 /// \brief Create the initialization entity for a parameter.
138 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
139 return InitializedEntity(Parm);
140 }
141
Douglas Gregora188ff22009-12-22 16:09:06 +0000142 /// \brief Create the initialization entity for a parameter that is
143 /// only known by its type.
144 static InitializedEntity InitializeParameter(QualType Type) {
145 return InitializedEntity(EK_Parameter, SourceLocation(), Type);
146 }
147
Douglas Gregor20093b42009-12-09 23:02:17 +0000148 /// \brief Create the initialization entity for the result of a function.
149 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000150 QualType Type) {
151 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000152 }
153
154 /// \brief Create the initialization entity for an exception object.
155 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000156 QualType Type) {
157 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000158 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000159
160 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000161 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
162 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000163 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000164
165 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000166 static InitializedEntity InitializeTemporary(QualType Type) {
167 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000168 }
169
170 /// \brief Create the initialization entity for a base class subobject.
171 static InitializedEntity InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +0000172 CXXBaseSpecifier *Base,
173 bool IsInheritedVirtualBase);
Douglas Gregor20093b42009-12-09 23:02:17 +0000174
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000175 /// \brief Create the initialization entity for a member subobject.
176 static InitializedEntity InitializeMember(FieldDecl *Member,
177 const InitializedEntity *Parent = 0) {
178 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000179 }
180
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000181 /// \brief Create the initialization entity for an array element.
182 static InitializedEntity InitializeElement(ASTContext &Context,
183 unsigned Index,
184 const InitializedEntity &Parent) {
185 return InitializedEntity(Context, Index, Parent);
186 }
187
Douglas Gregor20093b42009-12-09 23:02:17 +0000188 /// \brief Determine the kind of initialization.
189 EntityKind getKind() const { return Kind; }
190
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000191 /// \brief Retrieve the parent of the entity being initialized, when
192 /// the initialization itself is occuring within the context of a
193 /// larger initialization.
194 const InitializedEntity *getParent() const { return Parent; }
195
Douglas Gregor20093b42009-12-09 23:02:17 +0000196 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000197 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000198
Douglas Gregor99a2e602009-12-16 01:38:02 +0000199 /// \brief Retrieve the name of the entity being initialized.
200 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000201
202 /// \brief Retrieve the variable, parameter, or field being
203 /// initialized.
204 DeclaratorDecl *getDecl() const;
205
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000206 /// \brief Retrieve the base specifier.
207 CXXBaseSpecifier *getBaseSpecifier() const {
208 assert(getKind() == EK_Base && "Not a base specifier");
Anders Carlsson711f34a2010-04-21 19:52:01 +0000209 return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
210 }
211
212 /// \brief Return whether the base is an inherited virtual base.
213 bool isInheritedVirtualBase() const {
214 assert(getKind() == EK_Base && "Not a base specifier");
215 return Base & 0x1;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000216 }
217
Douglas Gregor20093b42009-12-09 23:02:17 +0000218 /// \brief Determine the location of the 'return' keyword when initializing
219 /// the result of a function call.
220 SourceLocation getReturnLoc() const {
221 assert(getKind() == EK_Result && "No 'return' location!");
222 return SourceLocation::getFromRawEncoding(Location);
223 }
224
225 /// \brief Determine the location of the 'throw' keyword when initializing
226 /// an exception object.
227 SourceLocation getThrowLoc() const {
228 assert(getKind() == EK_Exception && "No 'throw' location!");
229 return SourceLocation::getFromRawEncoding(Location);
230 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000231
232 /// \brief If this is already the initializer for an array or vector
233 /// element, sets the element index.
234 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000235 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000236 this->Index = Index;
237 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000238};
239
240/// \brief Describes the kind of initialization being performed, along with
241/// location information for tokens related to the initialization (equal sign,
242/// parentheses).
243class InitializationKind {
244public:
245 /// \brief The kind of initialization being performed.
246 enum InitKind {
247 IK_Direct, ///< Direct initialization
248 IK_Copy, ///< Copy initialization
249 IK_Default, ///< Default initialization
250 IK_Value ///< Value initialization
251 };
252
253private:
254 /// \brief The kind of initialization that we're storing.
255 enum StoredInitKind {
256 SIK_Direct = IK_Direct, ///< Direct initialization
257 SIK_Copy = IK_Copy, ///< Copy initialization
258 SIK_Default = IK_Default, ///< Default initialization
259 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000260 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000261 SIK_DirectCast, ///< Direct initialization due to a cast
262 /// \brief Direct initialization due to a C-style or functional cast.
263 SIK_DirectCStyleOrFunctionalCast
264 };
265
266 /// \brief The kind of initialization being performed.
267 StoredInitKind Kind;
268
269 /// \brief The source locations involved in the initialization.
270 SourceLocation Locations[3];
271
272 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
273 SourceLocation Loc2, SourceLocation Loc3)
274 : Kind(Kind)
275 {
276 Locations[0] = Loc1;
277 Locations[1] = Loc2;
278 Locations[2] = Loc3;
279 }
280
281public:
282 /// \brief Create a direct initialization.
283 static InitializationKind CreateDirect(SourceLocation InitLoc,
284 SourceLocation LParenLoc,
285 SourceLocation RParenLoc) {
286 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
287 }
288
289 /// \brief Create a direct initialization due to a cast.
290 static InitializationKind CreateCast(SourceRange TypeRange,
291 bool IsCStyleCast) {
292 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
293 : SIK_DirectCast,
294 TypeRange.getBegin(), TypeRange.getBegin(),
295 TypeRange.getEnd());
296 }
297
298 /// \brief Create a copy initialization.
299 static InitializationKind CreateCopy(SourceLocation InitLoc,
300 SourceLocation EqualLoc) {
301 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
302 }
303
304 /// \brief Create a default initialization.
305 static InitializationKind CreateDefault(SourceLocation InitLoc) {
306 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
307 }
308
309 /// \brief Create a value initialization.
310 static InitializationKind CreateValue(SourceLocation InitLoc,
311 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000312 SourceLocation RParenLoc,
313 bool isImplicit = false) {
314 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
315 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000316 }
317
318 /// \brief Determine the initialization kind.
319 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000320 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000321 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000322 if (Kind == SIK_ImplicitValue)
323 return IK_Value;
324
Douglas Gregor20093b42009-12-09 23:02:17 +0000325 return (InitKind)Kind;
326 }
327
328 /// \brief Determine whether this initialization is an explicit cast.
329 bool isExplicitCast() const {
330 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
331 }
332
333 /// \brief Determine whether this initialization is a C-style cast.
334 bool isCStyleOrFunctionalCast() const {
335 return Kind == SIK_DirectCStyleOrFunctionalCast;
336 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000337
338 /// \brief Determine whether this initialization is an implicit
339 /// value-initialization, e.g., as occurs during aggregate
340 /// initialization.
341 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
342
Douglas Gregor20093b42009-12-09 23:02:17 +0000343 /// \brief Retrieve the location at which initialization is occurring.
344 SourceLocation getLocation() const { return Locations[0]; }
345
346 /// \brief Retrieve the source range that covers the initialization.
347 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000348 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000349 }
350
351 /// \brief Retrieve the location of the equal sign for copy initialization
352 /// (if present).
353 SourceLocation getEqualLoc() const {
354 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
355 return Locations[1];
356 }
357
358 /// \brief Retrieve the source range containing the locations of the open
359 /// and closing parentheses for value and direct initializations.
360 SourceRange getParenRange() const {
361 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
362 "Only direct- and value-initialization have parentheses");
363 return SourceRange(Locations[1], Locations[2]);
364 }
365};
366
367/// \brief Describes the sequence of initializations required to initialize
368/// a given object or reference with a set of arguments.
369class InitializationSequence {
370public:
371 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000372 ///
373 /// FIXME: Much of this information is in the initialization steps... why is
374 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000375 enum SequenceKind {
376 /// \brief A failed initialization sequence. The failure kind tells what
377 /// happened.
378 FailedSequence = 0,
379
380 /// \brief A dependent initialization, which could not be
381 /// type-checked due to the presence of dependent types or
382 /// dependently-type expressions.
383 DependentSequence,
384
Douglas Gregor4a520a22009-12-14 17:27:33 +0000385 /// \brief A user-defined conversion sequence.
386 UserDefinedConversion,
387
Douglas Gregor51c56d62009-12-14 20:49:26 +0000388 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000389 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000390
Douglas Gregor20093b42009-12-09 23:02:17 +0000391 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000392 ReferenceBinding,
393
394 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000395 ListInitialization,
396
397 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000398 ZeroInitialization,
399
400 /// \brief No initialization required.
401 NoInitialization,
402
403 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000404 StandardConversion,
405
406 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000407 CAssignment,
408
409 /// \brief String initialization
410 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000411 };
412
413 /// \brief Describes the kind of a particular step in an initialization
414 /// sequence.
415 enum StepKind {
416 /// \brief Resolve the address of an overloaded function to a specific
417 /// function declaration.
418 SK_ResolveAddressOfOverloadedFunction,
419 /// \brief Perform a derived-to-base cast, producing an rvalue.
420 SK_CastDerivedToBaseRValue,
421 /// \brief Perform a derived-to-base cast, producing an lvalue.
422 SK_CastDerivedToBaseLValue,
423 /// \brief Reference binding to an lvalue.
424 SK_BindReference,
425 /// \brief Reference binding to a temporary.
426 SK_BindReferenceToTemporary,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000427 /// \brief An optional copy of a temporary object to another
428 /// temporary object, which is permitted (but not required) by
429 /// C++98/03 but not C++0x.
430 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000431 /// \brief Perform a user-defined conversion, either via a conversion
432 /// function or via a constructor.
433 SK_UserConversion,
434 /// \brief Perform a qualification conversion, producing an rvalue.
435 SK_QualificationConversionRValue,
436 /// \brief Perform a qualification conversion, producing an lvalue.
437 SK_QualificationConversionLValue,
438 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000439 SK_ConversionSequence,
440 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000441 SK_ListInitialization,
442 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000443 SK_ConstructorInitialization,
444 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000445 SK_ZeroInitialization,
446 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000447 SK_CAssignment,
448 /// \brief Initialization by string
449 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000450 };
451
452 /// \brief A single step in the initialization sequence.
453 class Step {
454 public:
455 /// \brief The kind of conversion or initialization step we are taking.
456 StepKind Kind;
457
458 // \brief The type that results from this initialization.
459 QualType Type;
460
461 union {
462 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
463 /// SK_UserConversion, the function that the expression should be
464 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000465 ///
466 /// Always a FunctionDecl.
467 /// For conversion decls, the naming class is the source type.
468 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000469 struct {
470 FunctionDecl *Function;
471 DeclAccessPair FoundDecl;
472 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000473
474 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
475 /// sequence
476 ImplicitConversionSequence *ICS;
477 };
478
479 void Destroy();
480 };
481
482private:
483 /// \brief The kind of initialization sequence computed.
484 enum SequenceKind SequenceKind;
485
486 /// \brief Steps taken by this initialization.
487 llvm::SmallVector<Step, 4> Steps;
488
489public:
490 /// \brief Describes why initialization failed.
491 enum FailureKind {
492 /// \brief Too many initializers provided for a reference.
493 FK_TooManyInitsForReference,
494 /// \brief Array must be initialized with an initializer list.
495 FK_ArrayNeedsInitList,
496 /// \brief Array must be initialized with an initializer list or a
497 /// string literal.
498 FK_ArrayNeedsInitListOrStringLiteral,
499 /// \brief Cannot resolve the address of an overloaded function.
500 FK_AddressOfOverloadFailed,
501 /// \brief Overloading due to reference initialization failed.
502 FK_ReferenceInitOverloadFailed,
503 /// \brief Non-const lvalue reference binding to a temporary.
504 FK_NonConstLValueReferenceBindingToTemporary,
505 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
506 /// type.
507 FK_NonConstLValueReferenceBindingToUnrelated,
508 /// \brief Rvalue reference binding to an lvalue.
509 FK_RValueReferenceBindingToLValue,
510 /// \brief Reference binding drops qualifiers.
511 FK_ReferenceInitDropsQualifiers,
512 /// \brief Reference binding failed.
513 FK_ReferenceInitFailed,
514 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000515 FK_ConversionFailed,
516 /// \brief Too many initializers for scalar
517 FK_TooManyInitsForScalar,
518 /// \brief Reference initialization from an initializer list
519 FK_ReferenceBindingToInitList,
520 /// \brief Initialization of some unused destination type with an
521 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000522 FK_InitListBadDestinationType,
523 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000524 FK_UserConversionOverloadFailed,
525 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000526 FK_ConstructorOverloadFailed,
527 /// \brief Default-initialization of a 'const' object.
528 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000529 };
530
531private:
532 /// \brief The reason why initialization failued.
533 FailureKind Failure;
534
535 /// \brief The failed result of overload resolution.
536 OverloadingResult FailedOverloadResult;
537
538 /// \brief The candidate set created when initialization failed.
539 OverloadCandidateSet FailedCandidateSet;
540
541public:
542 /// \brief Try to perform initialization of the given entity, creating a
543 /// record of the steps required to perform the initialization.
544 ///
545 /// The generated initialization sequence will either contain enough
546 /// information to diagnose
547 ///
548 /// \param S the semantic analysis object.
549 ///
550 /// \param Entity the entity being initialized.
551 ///
552 /// \param Kind the kind of initialization being performed.
553 ///
554 /// \param Args the argument(s) provided for initialization.
555 ///
556 /// \param NumArgs the number of arguments provided for initialization.
557 InitializationSequence(Sema &S,
558 const InitializedEntity &Entity,
559 const InitializationKind &Kind,
560 Expr **Args,
561 unsigned NumArgs);
562
563 ~InitializationSequence();
564
565 /// \brief Perform the actual initialization of the given entity based on
566 /// the computed initialization sequence.
567 ///
568 /// \param S the semantic analysis object.
569 ///
570 /// \param Entity the entity being initialized.
571 ///
572 /// \param Kind the kind of initialization being performed.
573 ///
574 /// \param Args the argument(s) provided for initialization, ownership of
575 /// which is transfered into the routine.
576 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000577 /// \param ResultType if non-NULL, will be set to the type of the
578 /// initialized object, which is the type of the declaration in most
579 /// cases. However, when the initialized object is a variable of
580 /// incomplete array type and the initializer is an initializer
581 /// list, this type will be set to the completed array type.
582 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000583 /// \returns an expression that performs the actual object initialization, if
584 /// the initialization is well-formed. Otherwise, emits diagnostics
585 /// and returns an invalid expression.
586 Action::OwningExprResult Perform(Sema &S,
587 const InitializedEntity &Entity,
588 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000589 Action::MultiExprArg Args,
590 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000591
592 /// \brief Diagnose an potentially-invalid initialization sequence.
593 ///
594 /// \returns true if the initialization sequence was ill-formed,
595 /// false otherwise.
596 bool Diagnose(Sema &S,
597 const InitializedEntity &Entity,
598 const InitializationKind &Kind,
599 Expr **Args, unsigned NumArgs);
600
601 /// \brief Determine the kind of initialization sequence computed.
602 enum SequenceKind getKind() const { return SequenceKind; }
603
604 /// \brief Set the kind of sequence computed.
605 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
606
607 /// \brief Determine whether the initialization sequence is valid.
608 operator bool() const { return SequenceKind != FailedSequence; }
609
610 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
611 step_iterator step_begin() const { return Steps.begin(); }
612 step_iterator step_end() const { return Steps.end(); }
613
Douglas Gregorb70cf442010-03-26 20:14:36 +0000614 /// \brief Determine whether this initialization is a direct reference
615 /// binding (C++ [dcl.init.ref]).
616 bool isDirectReferenceBinding() const;
617
618 /// \brief Determine whether this initialization failed due to an ambiguity.
619 bool isAmbiguous() const;
620
Douglas Gregord6e44a32010-04-16 22:09:46 +0000621 /// \brief Determine whether this initialization is direct call to a
622 /// constructor.
623 bool isConstructorInitialization() const;
624
Douglas Gregor20093b42009-12-09 23:02:17 +0000625 /// \brief Add a new step in the initialization that resolves the address
626 /// of an overloaded function to a specific function declaration.
627 ///
628 /// \param Function the function to which the overloaded function reference
629 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000630 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
631 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000632
633 /// \brief Add a new step in the initialization that performs a derived-to-
634 /// base cast.
635 ///
636 /// \param BaseType the base type to which we will be casting.
637 ///
638 /// \param IsLValue true if the result of this cast will be treated as
639 /// an lvalue.
640 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
641
642 /// \brief Add a new step binding a reference to an object.
643 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000644 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000645 /// object (thereby extending its lifetime); false if we are binding to an
646 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000647 ///
648 /// \param UnnecessaryCopy True if we should check for a copy
649 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000650 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000651
652 /// \brief Add a new step that makes an extraneous copy of the input
653 /// to a temporary of the same class type.
654 ///
655 /// This extraneous copy only occurs during reference binding in
656 /// C++98/03, where we are permitted (but not required) to introduce
657 /// an extra copy. At a bare minimum, we must check that we could
658 /// call the copy constructor, and produce a diagnostic if the copy
659 /// constructor is inaccessible or no copy constructor matches.
660 //
661 /// \param T The type of the temporary being created.
662 void AddExtraneousCopyToTemporary(QualType T);
663
Douglas Gregor20093b42009-12-09 23:02:17 +0000664 /// \brief Add a new step invoking a conversion function, which is either
665 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000666 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000667 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000668 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000669
670 /// \brief Add a new step that performs a qualification conversion to the
671 /// given type.
672 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
673
674 /// \brief Add a new step that applies an implicit conversion sequence.
675 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
676 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000677
678 /// \brief Add a list-initialiation step
679 void AddListInitializationStep(QualType T);
680
Douglas Gregor71d17402009-12-15 00:01:57 +0000681 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000682 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000683 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000684 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000685
686 /// \brief Add a zero-initialization step.
687 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000688
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000689 /// \brief Add a C assignment step.
690 //
691 // FIXME: It isn't clear whether this should ever be needed;
692 // ideally, we would handle everything needed in C in the common
693 // path. However, that isn't the case yet.
694 void AddCAssignmentStep(QualType T);
695
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000696 /// \brief Add a string init step.
697 void AddStringInitStep(QualType T);
698
Douglas Gregor20093b42009-12-09 23:02:17 +0000699 /// \brief Note that this initialization sequence failed.
700 void SetFailed(FailureKind Failure) {
701 SequenceKind = FailedSequence;
702 this->Failure = Failure;
703 }
704
705 /// \brief Note that this initialization sequence failed due to failed
706 /// overload resolution.
707 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
708
709 /// \brief Retrieve a reference to the candidate set when overload
710 /// resolution fails.
711 OverloadCandidateSet &getFailedCandidateSet() {
712 return FailedCandidateSet;
713 }
714
715 /// \brief Determine why initialization failed.
716 FailureKind getFailureKind() const {
717 assert(getKind() == FailedSequence && "Not an initialization failure!");
718 return Failure;
719 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000720
721 /// \brief Dump a representation of this initialization sequence to
722 /// the given stream, for debugging purposes.
723 void dump(llvm::raw_ostream &OS) const;
724
725 /// \brief Dump a representation of this initialization sequence to
726 /// standard error, for debugging purposes.
727 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000728};
729
730} // end namespace clang
731
732#endif // LLVM_CLANG_SEMA_INIT_H