blob: 5f2592fb77cf4cb69b65d183575986227ba6120f [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) {
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000145 InitializedEntity Entity;
146 Entity.Kind = EK_Parameter;
147 Entity.Type = Type;
148 Entity.Parent = 0;
149 Entity.VariableOrMember = 0;
150 return Entity;
Douglas Gregora188ff22009-12-22 16:09:06 +0000151 }
152
Douglas Gregor20093b42009-12-09 23:02:17 +0000153 /// \brief Create the initialization entity for the result of a function.
154 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000155 QualType Type) {
156 return InitializedEntity(EK_Result, ReturnLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000157 }
158
159 /// \brief Create the initialization entity for an exception object.
160 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregord6542d82009-12-22 15:35:07 +0000161 QualType Type) {
162 return InitializedEntity(EK_Exception, ThrowLoc, Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000163 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000164
165 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000166 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
167 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000168 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000169
170 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000171 static InitializedEntity InitializeTemporary(QualType Type) {
172 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000173 }
174
175 /// \brief Create the initialization entity for a base class subobject.
176 static InitializedEntity InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +0000177 CXXBaseSpecifier *Base,
178 bool IsInheritedVirtualBase);
Douglas Gregor20093b42009-12-09 23:02:17 +0000179
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000180 /// \brief Create the initialization entity for a member subobject.
181 static InitializedEntity InitializeMember(FieldDecl *Member,
182 const InitializedEntity *Parent = 0) {
183 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000184 }
185
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000186 /// \brief Create the initialization entity for an array element.
187 static InitializedEntity InitializeElement(ASTContext &Context,
188 unsigned Index,
189 const InitializedEntity &Parent) {
190 return InitializedEntity(Context, Index, Parent);
191 }
192
Douglas Gregor20093b42009-12-09 23:02:17 +0000193 /// \brief Determine the kind of initialization.
194 EntityKind getKind() const { return Kind; }
195
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000196 /// \brief Retrieve the parent of the entity being initialized, when
197 /// the initialization itself is occuring within the context of a
198 /// larger initialization.
199 const InitializedEntity *getParent() const { return Parent; }
200
Douglas Gregor20093b42009-12-09 23:02:17 +0000201 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000202 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000203
Douglas Gregor99a2e602009-12-16 01:38:02 +0000204 /// \brief Retrieve the name of the entity being initialized.
205 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000206
207 /// \brief Retrieve the variable, parameter, or field being
208 /// initialized.
209 DeclaratorDecl *getDecl() const;
210
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000211 /// \brief Retrieve the base specifier.
212 CXXBaseSpecifier *getBaseSpecifier() const {
213 assert(getKind() == EK_Base && "Not a base specifier");
Anders Carlsson711f34a2010-04-21 19:52:01 +0000214 return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
215 }
216
217 /// \brief Return whether the base is an inherited virtual base.
218 bool isInheritedVirtualBase() const {
219 assert(getKind() == EK_Base && "Not a base specifier");
220 return Base & 0x1;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000221 }
222
Douglas Gregor20093b42009-12-09 23:02:17 +0000223 /// \brief Determine the location of the 'return' keyword when initializing
224 /// the result of a function call.
225 SourceLocation getReturnLoc() const {
226 assert(getKind() == EK_Result && "No 'return' location!");
227 return SourceLocation::getFromRawEncoding(Location);
228 }
229
230 /// \brief Determine the location of the 'throw' keyword when initializing
231 /// an exception object.
232 SourceLocation getThrowLoc() const {
233 assert(getKind() == EK_Exception && "No 'throw' location!");
234 return SourceLocation::getFromRawEncoding(Location);
235 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000236
237 /// \brief If this is already the initializer for an array or vector
238 /// element, sets the element index.
239 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000240 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000241 this->Index = Index;
242 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000243};
244
245/// \brief Describes the kind of initialization being performed, along with
246/// location information for tokens related to the initialization (equal sign,
247/// parentheses).
248class InitializationKind {
249public:
250 /// \brief The kind of initialization being performed.
251 enum InitKind {
252 IK_Direct, ///< Direct initialization
253 IK_Copy, ///< Copy initialization
254 IK_Default, ///< Default initialization
255 IK_Value ///< Value initialization
256 };
257
258private:
259 /// \brief The kind of initialization that we're storing.
260 enum StoredInitKind {
261 SIK_Direct = IK_Direct, ///< Direct initialization
262 SIK_Copy = IK_Copy, ///< Copy initialization
263 SIK_Default = IK_Default, ///< Default initialization
264 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000265 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000266 SIK_DirectCast, ///< Direct initialization due to a cast
267 /// \brief Direct initialization due to a C-style or functional cast.
268 SIK_DirectCStyleOrFunctionalCast
269 };
270
271 /// \brief The kind of initialization being performed.
272 StoredInitKind Kind;
273
274 /// \brief The source locations involved in the initialization.
275 SourceLocation Locations[3];
276
277 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
278 SourceLocation Loc2, SourceLocation Loc3)
279 : Kind(Kind)
280 {
281 Locations[0] = Loc1;
282 Locations[1] = Loc2;
283 Locations[2] = Loc3;
284 }
285
286public:
287 /// \brief Create a direct initialization.
288 static InitializationKind CreateDirect(SourceLocation InitLoc,
289 SourceLocation LParenLoc,
290 SourceLocation RParenLoc) {
291 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
292 }
293
294 /// \brief Create a direct initialization due to a cast.
295 static InitializationKind CreateCast(SourceRange TypeRange,
296 bool IsCStyleCast) {
297 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
298 : SIK_DirectCast,
299 TypeRange.getBegin(), TypeRange.getBegin(),
300 TypeRange.getEnd());
301 }
302
303 /// \brief Create a copy initialization.
304 static InitializationKind CreateCopy(SourceLocation InitLoc,
305 SourceLocation EqualLoc) {
306 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
307 }
308
309 /// \brief Create a default initialization.
310 static InitializationKind CreateDefault(SourceLocation InitLoc) {
311 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
312 }
313
314 /// \brief Create a value initialization.
315 static InitializationKind CreateValue(SourceLocation InitLoc,
316 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000317 SourceLocation RParenLoc,
318 bool isImplicit = false) {
319 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
320 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000321 }
322
323 /// \brief Determine the initialization kind.
324 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000325 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000326 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000327 if (Kind == SIK_ImplicitValue)
328 return IK_Value;
329
Douglas Gregor20093b42009-12-09 23:02:17 +0000330 return (InitKind)Kind;
331 }
332
333 /// \brief Determine whether this initialization is an explicit cast.
334 bool isExplicitCast() const {
335 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
336 }
337
338 /// \brief Determine whether this initialization is a C-style cast.
339 bool isCStyleOrFunctionalCast() const {
340 return Kind == SIK_DirectCStyleOrFunctionalCast;
341 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000342
343 /// \brief Determine whether this initialization is an implicit
344 /// value-initialization, e.g., as occurs during aggregate
345 /// initialization.
346 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
347
Douglas Gregor20093b42009-12-09 23:02:17 +0000348 /// \brief Retrieve the location at which initialization is occurring.
349 SourceLocation getLocation() const { return Locations[0]; }
350
351 /// \brief Retrieve the source range that covers the initialization.
352 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000353 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000354 }
355
356 /// \brief Retrieve the location of the equal sign for copy initialization
357 /// (if present).
358 SourceLocation getEqualLoc() const {
359 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
360 return Locations[1];
361 }
362
363 /// \brief Retrieve the source range containing the locations of the open
364 /// and closing parentheses for value and direct initializations.
365 SourceRange getParenRange() const {
366 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
367 "Only direct- and value-initialization have parentheses");
368 return SourceRange(Locations[1], Locations[2]);
369 }
370};
371
372/// \brief Describes the sequence of initializations required to initialize
373/// a given object or reference with a set of arguments.
374class InitializationSequence {
375public:
376 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000377 ///
378 /// FIXME: Much of this information is in the initialization steps... why is
379 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000380 enum SequenceKind {
381 /// \brief A failed initialization sequence. The failure kind tells what
382 /// happened.
383 FailedSequence = 0,
384
385 /// \brief A dependent initialization, which could not be
386 /// type-checked due to the presence of dependent types or
387 /// dependently-type expressions.
388 DependentSequence,
389
Douglas Gregor4a520a22009-12-14 17:27:33 +0000390 /// \brief A user-defined conversion sequence.
391 UserDefinedConversion,
392
Douglas Gregor51c56d62009-12-14 20:49:26 +0000393 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000394 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000395
Douglas Gregor20093b42009-12-09 23:02:17 +0000396 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000397 ReferenceBinding,
398
399 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000400 ListInitialization,
401
402 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000403 ZeroInitialization,
404
405 /// \brief No initialization required.
406 NoInitialization,
407
408 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000409 StandardConversion,
410
411 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000412 CAssignment,
413
414 /// \brief String initialization
415 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000416 };
417
418 /// \brief Describes the kind of a particular step in an initialization
419 /// sequence.
420 enum StepKind {
421 /// \brief Resolve the address of an overloaded function to a specific
422 /// function declaration.
423 SK_ResolveAddressOfOverloadedFunction,
424 /// \brief Perform a derived-to-base cast, producing an rvalue.
425 SK_CastDerivedToBaseRValue,
426 /// \brief Perform a derived-to-base cast, producing an lvalue.
427 SK_CastDerivedToBaseLValue,
428 /// \brief Reference binding to an lvalue.
429 SK_BindReference,
430 /// \brief Reference binding to a temporary.
431 SK_BindReferenceToTemporary,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000432 /// \brief An optional copy of a temporary object to another
433 /// temporary object, which is permitted (but not required) by
434 /// C++98/03 but not C++0x.
435 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000436 /// \brief Perform a user-defined conversion, either via a conversion
437 /// function or via a constructor.
438 SK_UserConversion,
439 /// \brief Perform a qualification conversion, producing an rvalue.
440 SK_QualificationConversionRValue,
441 /// \brief Perform a qualification conversion, producing an lvalue.
442 SK_QualificationConversionLValue,
443 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000444 SK_ConversionSequence,
445 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000446 SK_ListInitialization,
447 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000448 SK_ConstructorInitialization,
449 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000450 SK_ZeroInitialization,
451 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000452 SK_CAssignment,
453 /// \brief Initialization by string
454 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000455 };
456
457 /// \brief A single step in the initialization sequence.
458 class Step {
459 public:
460 /// \brief The kind of conversion or initialization step we are taking.
461 StepKind Kind;
462
463 // \brief The type that results from this initialization.
464 QualType Type;
465
466 union {
467 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
468 /// SK_UserConversion, the function that the expression should be
469 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000470 ///
471 /// Always a FunctionDecl.
472 /// For conversion decls, the naming class is the source type.
473 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000474 struct {
475 FunctionDecl *Function;
476 DeclAccessPair FoundDecl;
477 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000478
479 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
480 /// sequence
481 ImplicitConversionSequence *ICS;
482 };
483
484 void Destroy();
485 };
486
487private:
488 /// \brief The kind of initialization sequence computed.
489 enum SequenceKind SequenceKind;
490
491 /// \brief Steps taken by this initialization.
492 llvm::SmallVector<Step, 4> Steps;
493
494public:
495 /// \brief Describes why initialization failed.
496 enum FailureKind {
497 /// \brief Too many initializers provided for a reference.
498 FK_TooManyInitsForReference,
499 /// \brief Array must be initialized with an initializer list.
500 FK_ArrayNeedsInitList,
501 /// \brief Array must be initialized with an initializer list or a
502 /// string literal.
503 FK_ArrayNeedsInitListOrStringLiteral,
504 /// \brief Cannot resolve the address of an overloaded function.
505 FK_AddressOfOverloadFailed,
506 /// \brief Overloading due to reference initialization failed.
507 FK_ReferenceInitOverloadFailed,
508 /// \brief Non-const lvalue reference binding to a temporary.
509 FK_NonConstLValueReferenceBindingToTemporary,
510 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
511 /// type.
512 FK_NonConstLValueReferenceBindingToUnrelated,
513 /// \brief Rvalue reference binding to an lvalue.
514 FK_RValueReferenceBindingToLValue,
515 /// \brief Reference binding drops qualifiers.
516 FK_ReferenceInitDropsQualifiers,
517 /// \brief Reference binding failed.
518 FK_ReferenceInitFailed,
519 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000520 FK_ConversionFailed,
521 /// \brief Too many initializers for scalar
522 FK_TooManyInitsForScalar,
523 /// \brief Reference initialization from an initializer list
524 FK_ReferenceBindingToInitList,
525 /// \brief Initialization of some unused destination type with an
526 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000527 FK_InitListBadDestinationType,
528 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000529 FK_UserConversionOverloadFailed,
530 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000531 FK_ConstructorOverloadFailed,
532 /// \brief Default-initialization of a 'const' object.
533 FK_DefaultInitOfConst
Douglas Gregor20093b42009-12-09 23:02:17 +0000534 };
535
536private:
537 /// \brief The reason why initialization failued.
538 FailureKind Failure;
539
540 /// \brief The failed result of overload resolution.
541 OverloadingResult FailedOverloadResult;
542
543 /// \brief The candidate set created when initialization failed.
544 OverloadCandidateSet FailedCandidateSet;
Douglas Gregora41a8c52010-04-22 00:20:18 +0000545
546 /// \brief Prints a follow-up note that highlights the location of
547 /// the initialized entity, if it's remote.
548 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
549
Douglas Gregor20093b42009-12-09 23:02:17 +0000550public:
551 /// \brief Try to perform initialization of the given entity, creating a
552 /// record of the steps required to perform the initialization.
553 ///
554 /// The generated initialization sequence will either contain enough
555 /// information to diagnose
556 ///
557 /// \param S the semantic analysis object.
558 ///
559 /// \param Entity the entity being initialized.
560 ///
561 /// \param Kind the kind of initialization being performed.
562 ///
563 /// \param Args the argument(s) provided for initialization.
564 ///
565 /// \param NumArgs the number of arguments provided for initialization.
566 InitializationSequence(Sema &S,
567 const InitializedEntity &Entity,
568 const InitializationKind &Kind,
569 Expr **Args,
570 unsigned NumArgs);
571
572 ~InitializationSequence();
573
574 /// \brief Perform the actual initialization of the given entity based on
575 /// the computed initialization sequence.
576 ///
577 /// \param S the semantic analysis object.
578 ///
579 /// \param Entity the entity being initialized.
580 ///
581 /// \param Kind the kind of initialization being performed.
582 ///
583 /// \param Args the argument(s) provided for initialization, ownership of
584 /// which is transfered into the routine.
585 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000586 /// \param ResultType if non-NULL, will be set to the type of the
587 /// initialized object, which is the type of the declaration in most
588 /// cases. However, when the initialized object is a variable of
589 /// incomplete array type and the initializer is an initializer
590 /// list, this type will be set to the completed array type.
591 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000592 /// \returns an expression that performs the actual object initialization, if
593 /// the initialization is well-formed. Otherwise, emits diagnostics
594 /// and returns an invalid expression.
595 Action::OwningExprResult Perform(Sema &S,
596 const InitializedEntity &Entity,
597 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000598 Action::MultiExprArg Args,
599 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000600
601 /// \brief Diagnose an potentially-invalid initialization sequence.
602 ///
603 /// \returns true if the initialization sequence was ill-formed,
604 /// false otherwise.
605 bool Diagnose(Sema &S,
606 const InitializedEntity &Entity,
607 const InitializationKind &Kind,
608 Expr **Args, unsigned NumArgs);
609
610 /// \brief Determine the kind of initialization sequence computed.
611 enum SequenceKind getKind() const { return SequenceKind; }
612
613 /// \brief Set the kind of sequence computed.
614 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
615
616 /// \brief Determine whether the initialization sequence is valid.
617 operator bool() const { return SequenceKind != FailedSequence; }
618
619 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
620 step_iterator step_begin() const { return Steps.begin(); }
621 step_iterator step_end() const { return Steps.end(); }
622
Douglas Gregorb70cf442010-03-26 20:14:36 +0000623 /// \brief Determine whether this initialization is a direct reference
624 /// binding (C++ [dcl.init.ref]).
625 bool isDirectReferenceBinding() const;
626
627 /// \brief Determine whether this initialization failed due to an ambiguity.
628 bool isAmbiguous() const;
629
Douglas Gregord6e44a32010-04-16 22:09:46 +0000630 /// \brief Determine whether this initialization is direct call to a
631 /// constructor.
632 bool isConstructorInitialization() const;
633
Douglas Gregor20093b42009-12-09 23:02:17 +0000634 /// \brief Add a new step in the initialization that resolves the address
635 /// of an overloaded function to a specific function declaration.
636 ///
637 /// \param Function the function to which the overloaded function reference
638 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000639 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
640 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000641
642 /// \brief Add a new step in the initialization that performs a derived-to-
643 /// base cast.
644 ///
645 /// \param BaseType the base type to which we will be casting.
646 ///
647 /// \param IsLValue true if the result of this cast will be treated as
648 /// an lvalue.
649 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
650
651 /// \brief Add a new step binding a reference to an object.
652 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000653 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000654 /// object (thereby extending its lifetime); false if we are binding to an
655 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000656 ///
657 /// \param UnnecessaryCopy True if we should check for a copy
658 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000659 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000660
661 /// \brief Add a new step that makes an extraneous copy of the input
662 /// to a temporary of the same class type.
663 ///
664 /// This extraneous copy only occurs during reference binding in
665 /// C++98/03, where we are permitted (but not required) to introduce
666 /// an extra copy. At a bare minimum, we must check that we could
667 /// call the copy constructor, and produce a diagnostic if the copy
668 /// constructor is inaccessible or no copy constructor matches.
669 //
670 /// \param T The type of the temporary being created.
671 void AddExtraneousCopyToTemporary(QualType T);
672
Douglas Gregor20093b42009-12-09 23:02:17 +0000673 /// \brief Add a new step invoking a conversion function, which is either
674 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000675 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000676 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000677 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000678
679 /// \brief Add a new step that performs a qualification conversion to the
680 /// given type.
681 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
682
683 /// \brief Add a new step that applies an implicit conversion sequence.
684 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
685 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000686
687 /// \brief Add a list-initialiation step
688 void AddListInitializationStep(QualType T);
689
Douglas Gregor71d17402009-12-15 00:01:57 +0000690 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000691 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000692 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000693 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000694
695 /// \brief Add a zero-initialization step.
696 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000697
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000698 /// \brief Add a C assignment step.
699 //
700 // FIXME: It isn't clear whether this should ever be needed;
701 // ideally, we would handle everything needed in C in the common
702 // path. However, that isn't the case yet.
703 void AddCAssignmentStep(QualType T);
704
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000705 /// \brief Add a string init step.
706 void AddStringInitStep(QualType T);
707
Douglas Gregor20093b42009-12-09 23:02:17 +0000708 /// \brief Note that this initialization sequence failed.
709 void SetFailed(FailureKind Failure) {
710 SequenceKind = FailedSequence;
711 this->Failure = Failure;
712 }
713
714 /// \brief Note that this initialization sequence failed due to failed
715 /// overload resolution.
716 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
717
718 /// \brief Retrieve a reference to the candidate set when overload
719 /// resolution fails.
720 OverloadCandidateSet &getFailedCandidateSet() {
721 return FailedCandidateSet;
722 }
723
724 /// \brief Determine why initialization failed.
725 FailureKind getFailureKind() const {
726 assert(getKind() == FailedSequence && "Not an initialization failure!");
727 return Failure;
728 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000729
730 /// \brief Dump a representation of this initialization sequence to
731 /// the given stream, for debugging purposes.
732 void dump(llvm::raw_ostream &OS) const;
733
734 /// \brief Dump a representation of this initialization sequence to
735 /// standard error, for debugging purposes.
736 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000737};
738
739} // end namespace clang
740
741#endif // LLVM_CLANG_SEMA_INIT_H