blob: 44c36a735bc856efe8b15f6eef6979787992f45b [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.
Fariborz Jahanian310b1c42010-06-07 16:14:00 +000069 EK_VectorElement,
70 /// \brief The entity being initialized is a field of block descriptor for
71 /// the copied-in c++ object.
72 EK_BlockElement
Douglas Gregor20093b42009-12-09 23:02:17 +000073 };
74
75private:
76 /// \brief The kind of entity being initialized.
77 EntityKind Kind;
78
Douglas Gregorcb57fb92009-12-16 06:35:08 +000079 /// \brief If non-NULL, the parent entity in which this
80 /// initialization occurs.
81 const InitializedEntity *Parent;
82
Douglas Gregord6542d82009-12-22 15:35:07 +000083 /// \brief The type of the object or reference being initialized.
84 QualType Type;
Douglas Gregor20093b42009-12-09 23:02:17 +000085
86 union {
87 /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member,
88 /// the VarDecl, ParmVarDecl, or FieldDecl, respectively.
89 DeclaratorDecl *VariableOrMember;
90
Douglas Gregor3c9034c2010-05-15 00:13:29 +000091 struct {
92 /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the
93 /// location of the 'return', 'throw', or 'new' keyword,
94 /// respectively. When Kind == EK_Temporary, the location where
95 /// the temporary is being created.
96 unsigned Location;
97
98 /// \brief Whether the
99 bool NRVO;
100 } LocAndNRVO;
Douglas Gregor20093b42009-12-09 23:02:17 +0000101
102 /// \brief When Kind == EK_Base, the base specifier that provides the
Anders Carlsson711f34a2010-04-21 19:52:01 +0000103 /// base class. The lower bit specifies whether the base is an inherited
104 /// virtual base.
105 uintptr_t Base;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000106
Anders Carlsson711f34a2010-04-21 19:52:01 +0000107 /// \brief When Kind == EK_ArrayElement or EK_VectorElement, the
108 /// index of the array or vector element being initialized.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000109 unsigned Index;
Douglas Gregor20093b42009-12-09 23:02:17 +0000110 };
111
112 InitializedEntity() { }
113
114 /// \brief Create the initialization entity for a variable.
115 InitializedEntity(VarDecl *Var)
Douglas Gregord6542d82009-12-22 15:35:07 +0000116 : Kind(EK_Variable), Parent(0), Type(Var->getType()),
117 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000118
119 /// \brief Create the initialization entity for a parameter.
120 InitializedEntity(ParmVarDecl *Parm)
Douglas Gregor6e790ab2009-12-22 23:42:49 +0000121 : Kind(EK_Parameter), Parent(0), Type(Parm->getType().getUnqualifiedType()),
Douglas Gregord6542d82009-12-22 15:35:07 +0000122 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000123
Douglas Gregora188ff22009-12-22 16:09:06 +0000124 /// \brief Create the initialization entity for the result of a
125 /// function, throwing an object, performing an explicit cast, or
126 /// initializing a parameter for which there is no declaration.
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000127 InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
128 bool NRVO = false)
129 : Kind(Kind), Parent(0), Type(Type)
130 {
131 LocAndNRVO.Location = Loc.getRawEncoding();
132 LocAndNRVO.NRVO = NRVO;
133 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000134
135 /// \brief Create the initialization entity for a member subobject.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000136 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent)
Douglas Gregord6542d82009-12-22 15:35:07 +0000137 : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
138 VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) { }
Douglas Gregor20093b42009-12-09 23:02:17 +0000139
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000140 /// \brief Create the initialization entity for an array element.
141 InitializedEntity(ASTContext &Context, unsigned Index,
142 const InitializedEntity &Parent);
143
Douglas Gregor20093b42009-12-09 23:02:17 +0000144public:
145 /// \brief Create the initialization entity for a variable.
146 static InitializedEntity InitializeVariable(VarDecl *Var) {
147 return InitializedEntity(Var);
148 }
149
150 /// \brief Create the initialization entity for a parameter.
151 static InitializedEntity InitializeParameter(ParmVarDecl *Parm) {
152 return InitializedEntity(Parm);
153 }
154
Douglas Gregora188ff22009-12-22 16:09:06 +0000155 /// \brief Create the initialization entity for a parameter that is
156 /// only known by its type.
157 static InitializedEntity InitializeParameter(QualType Type) {
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000158 InitializedEntity Entity;
159 Entity.Kind = EK_Parameter;
160 Entity.Type = Type;
161 Entity.Parent = 0;
162 Entity.VariableOrMember = 0;
163 return Entity;
Douglas Gregora188ff22009-12-22 16:09:06 +0000164 }
165
Douglas Gregor20093b42009-12-09 23:02:17 +0000166 /// \brief Create the initialization entity for the result of a function.
167 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000168 QualType Type, bool NRVO) {
169 return InitializedEntity(EK_Result, ReturnLoc, Type, NRVO);
Douglas Gregor20093b42009-12-09 23:02:17 +0000170 }
171
Fariborz Jahanian310b1c42010-06-07 16:14:00 +0000172 static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
173 QualType Type, bool NRVO) {
174 return InitializedEntity(EK_BlockElement, BlockVarLoc, Type, NRVO);
175 }
176
Douglas Gregor20093b42009-12-09 23:02:17 +0000177 /// \brief Create the initialization entity for an exception object.
178 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000179 QualType Type, bool NRVO) {
180 return InitializedEntity(EK_Exception, ThrowLoc, Type, NRVO);
Douglas Gregor20093b42009-12-09 23:02:17 +0000181 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000182
183 /// \brief Create the initialization entity for an object allocated via new.
Douglas Gregord6542d82009-12-22 15:35:07 +0000184 static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
185 return InitializedEntity(EK_New, NewLoc, Type);
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000186 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000187
188 /// \brief Create the initialization entity for a temporary.
Douglas Gregord6542d82009-12-22 15:35:07 +0000189 static InitializedEntity InitializeTemporary(QualType Type) {
190 return InitializedEntity(EK_Temporary, SourceLocation(), Type);
Douglas Gregor20093b42009-12-09 23:02:17 +0000191 }
192
193 /// \brief Create the initialization entity for a base class subobject.
194 static InitializedEntity InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +0000195 CXXBaseSpecifier *Base,
196 bool IsInheritedVirtualBase);
Douglas Gregor20093b42009-12-09 23:02:17 +0000197
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000198 /// \brief Create the initialization entity for a member subobject.
199 static InitializedEntity InitializeMember(FieldDecl *Member,
200 const InitializedEntity *Parent = 0) {
201 return InitializedEntity(Member, Parent);
Douglas Gregor20093b42009-12-09 23:02:17 +0000202 }
203
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000204 /// \brief Create the initialization entity for an array element.
205 static InitializedEntity InitializeElement(ASTContext &Context,
206 unsigned Index,
207 const InitializedEntity &Parent) {
208 return InitializedEntity(Context, Index, Parent);
209 }
210
Douglas Gregor20093b42009-12-09 23:02:17 +0000211 /// \brief Determine the kind of initialization.
212 EntityKind getKind() const { return Kind; }
213
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000214 /// \brief Retrieve the parent of the entity being initialized, when
215 /// the initialization itself is occuring within the context of a
216 /// larger initialization.
217 const InitializedEntity *getParent() const { return Parent; }
218
Douglas Gregor20093b42009-12-09 23:02:17 +0000219 /// \brief Retrieve type being initialized.
Douglas Gregord6542d82009-12-22 15:35:07 +0000220 QualType getType() const { return Type; }
Douglas Gregor20093b42009-12-09 23:02:17 +0000221
Douglas Gregor99a2e602009-12-16 01:38:02 +0000222 /// \brief Retrieve the name of the entity being initialized.
223 DeclarationName getName() const;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000224
225 /// \brief Retrieve the variable, parameter, or field being
226 /// initialized.
227 DeclaratorDecl *getDecl() const;
228
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000229 /// \brief Determine whether this initialization allows the named return
230 /// value optimization, which also applies to thrown objects.
231 bool allowsNRVO() const;
232
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000233 /// \brief Retrieve the base specifier.
234 CXXBaseSpecifier *getBaseSpecifier() const {
235 assert(getKind() == EK_Base && "Not a base specifier");
Anders Carlsson711f34a2010-04-21 19:52:01 +0000236 return reinterpret_cast<CXXBaseSpecifier *>(Base & ~0x1);
237 }
238
239 /// \brief Return whether the base is an inherited virtual base.
240 bool isInheritedVirtualBase() const {
241 assert(getKind() == EK_Base && "Not a base specifier");
242 return Base & 0x1;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000243 }
244
Douglas Gregor20093b42009-12-09 23:02:17 +0000245 /// \brief Determine the location of the 'return' keyword when initializing
246 /// the result of a function call.
247 SourceLocation getReturnLoc() const {
248 assert(getKind() == EK_Result && "No 'return' location!");
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000249 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
Douglas Gregor20093b42009-12-09 23:02:17 +0000250 }
251
252 /// \brief Determine the location of the 'throw' keyword when initializing
253 /// an exception object.
254 SourceLocation getThrowLoc() const {
255 assert(getKind() == EK_Exception && "No 'throw' location!");
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000256 return SourceLocation::getFromRawEncoding(LocAndNRVO.Location);
Douglas Gregor20093b42009-12-09 23:02:17 +0000257 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000258
259 /// \brief If this is already the initializer for an array or vector
260 /// element, sets the element index.
261 void setElementIndex(unsigned Index) {
Anders Carlssond3d824d2010-01-23 04:34:47 +0000262 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000263 this->Index = Index;
264 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000265};
266
267/// \brief Describes the kind of initialization being performed, along with
268/// location information for tokens related to the initialization (equal sign,
269/// parentheses).
270class InitializationKind {
271public:
272 /// \brief The kind of initialization being performed.
273 enum InitKind {
274 IK_Direct, ///< Direct initialization
275 IK_Copy, ///< Copy initialization
276 IK_Default, ///< Default initialization
277 IK_Value ///< Value initialization
278 };
279
280private:
281 /// \brief The kind of initialization that we're storing.
282 enum StoredInitKind {
283 SIK_Direct = IK_Direct, ///< Direct initialization
284 SIK_Copy = IK_Copy, ///< Copy initialization
285 SIK_Default = IK_Default, ///< Default initialization
286 SIK_Value = IK_Value, ///< Value initialization
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000287 SIK_ImplicitValue, ///< Implicit value initialization
Douglas Gregor20093b42009-12-09 23:02:17 +0000288 SIK_DirectCast, ///< Direct initialization due to a cast
289 /// \brief Direct initialization due to a C-style or functional cast.
290 SIK_DirectCStyleOrFunctionalCast
291 };
292
293 /// \brief The kind of initialization being performed.
294 StoredInitKind Kind;
295
296 /// \brief The source locations involved in the initialization.
297 SourceLocation Locations[3];
298
299 InitializationKind(StoredInitKind Kind, SourceLocation Loc1,
300 SourceLocation Loc2, SourceLocation Loc3)
301 : Kind(Kind)
302 {
303 Locations[0] = Loc1;
304 Locations[1] = Loc2;
305 Locations[2] = Loc3;
306 }
307
308public:
309 /// \brief Create a direct initialization.
310 static InitializationKind CreateDirect(SourceLocation InitLoc,
311 SourceLocation LParenLoc,
312 SourceLocation RParenLoc) {
313 return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc);
314 }
315
316 /// \brief Create a direct initialization due to a cast.
317 static InitializationKind CreateCast(SourceRange TypeRange,
318 bool IsCStyleCast) {
319 return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast
320 : SIK_DirectCast,
321 TypeRange.getBegin(), TypeRange.getBegin(),
322 TypeRange.getEnd());
323 }
324
325 /// \brief Create a copy initialization.
326 static InitializationKind CreateCopy(SourceLocation InitLoc,
327 SourceLocation EqualLoc) {
328 return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc);
329 }
330
331 /// \brief Create a default initialization.
332 static InitializationKind CreateDefault(SourceLocation InitLoc) {
333 return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc);
334 }
335
336 /// \brief Create a value initialization.
337 static InitializationKind CreateValue(SourceLocation InitLoc,
338 SourceLocation LParenLoc,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000339 SourceLocation RParenLoc,
340 bool isImplicit = false) {
341 return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value,
342 InitLoc, LParenLoc, RParenLoc);
Douglas Gregor20093b42009-12-09 23:02:17 +0000343 }
344
345 /// \brief Determine the initialization kind.
346 InitKind getKind() const {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000347 if (Kind > SIK_ImplicitValue)
Douglas Gregor20093b42009-12-09 23:02:17 +0000348 return IK_Direct;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000349 if (Kind == SIK_ImplicitValue)
350 return IK_Value;
351
Douglas Gregor20093b42009-12-09 23:02:17 +0000352 return (InitKind)Kind;
353 }
354
355 /// \brief Determine whether this initialization is an explicit cast.
356 bool isExplicitCast() const {
357 return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast;
358 }
359
360 /// \brief Determine whether this initialization is a C-style cast.
361 bool isCStyleOrFunctionalCast() const {
362 return Kind == SIK_DirectCStyleOrFunctionalCast;
363 }
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000364
365 /// \brief Determine whether this initialization is an implicit
366 /// value-initialization, e.g., as occurs during aggregate
367 /// initialization.
368 bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; }
369
Douglas Gregor20093b42009-12-09 23:02:17 +0000370 /// \brief Retrieve the location at which initialization is occurring.
371 SourceLocation getLocation() const { return Locations[0]; }
372
373 /// \brief Retrieve the source range that covers the initialization.
374 SourceRange getRange() const {
Douglas Gregor71d17402009-12-15 00:01:57 +0000375 return SourceRange(Locations[0], Locations[2]);
Douglas Gregor20093b42009-12-09 23:02:17 +0000376 }
377
378 /// \brief Retrieve the location of the equal sign for copy initialization
379 /// (if present).
380 SourceLocation getEqualLoc() const {
381 assert(Kind == SIK_Copy && "Only copy initialization has an '='");
382 return Locations[1];
383 }
384
385 /// \brief Retrieve the source range containing the locations of the open
386 /// and closing parentheses for value and direct initializations.
387 SourceRange getParenRange() const {
388 assert((getKind() == IK_Direct || Kind == SIK_Value) &&
389 "Only direct- and value-initialization have parentheses");
390 return SourceRange(Locations[1], Locations[2]);
391 }
392};
393
394/// \brief Describes the sequence of initializations required to initialize
395/// a given object or reference with a set of arguments.
396class InitializationSequence {
397public:
398 /// \brief Describes the kind of initialization sequence computed.
Douglas Gregor71d17402009-12-15 00:01:57 +0000399 ///
400 /// FIXME: Much of this information is in the initialization steps... why is
401 /// it duplicated here?
Douglas Gregor20093b42009-12-09 23:02:17 +0000402 enum SequenceKind {
403 /// \brief A failed initialization sequence. The failure kind tells what
404 /// happened.
405 FailedSequence = 0,
406
407 /// \brief A dependent initialization, which could not be
408 /// type-checked due to the presence of dependent types or
409 /// dependently-type expressions.
410 DependentSequence,
411
Douglas Gregor4a520a22009-12-14 17:27:33 +0000412 /// \brief A user-defined conversion sequence.
413 UserDefinedConversion,
414
Douglas Gregor51c56d62009-12-14 20:49:26 +0000415 /// \brief A constructor call.
Douglas Gregora6ca6502009-12-14 20:57:13 +0000416 ConstructorInitialization,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000417
Douglas Gregor20093b42009-12-09 23:02:17 +0000418 /// \brief A reference binding.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000419 ReferenceBinding,
420
421 /// \brief List initialization
Douglas Gregor71d17402009-12-15 00:01:57 +0000422 ListInitialization,
423
424 /// \brief Zero-initialization.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000425 ZeroInitialization,
426
427 /// \brief No initialization required.
428 NoInitialization,
429
430 /// \brief Standard conversion sequence.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000431 StandardConversion,
432
433 /// \brief C conversion sequence.
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000434 CAssignment,
435
436 /// \brief String initialization
437 StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000438 };
439
440 /// \brief Describes the kind of a particular step in an initialization
441 /// sequence.
442 enum StepKind {
443 /// \brief Resolve the address of an overloaded function to a specific
444 /// function declaration.
445 SK_ResolveAddressOfOverloadedFunction,
446 /// \brief Perform a derived-to-base cast, producing an rvalue.
447 SK_CastDerivedToBaseRValue,
448 /// \brief Perform a derived-to-base cast, producing an lvalue.
449 SK_CastDerivedToBaseLValue,
450 /// \brief Reference binding to an lvalue.
451 SK_BindReference,
452 /// \brief Reference binding to a temporary.
453 SK_BindReferenceToTemporary,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000454 /// \brief An optional copy of a temporary object to another
455 /// temporary object, which is permitted (but not required) by
456 /// C++98/03 but not C++0x.
457 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000458 /// \brief Perform a user-defined conversion, either via a conversion
459 /// function or via a constructor.
460 SK_UserConversion,
461 /// \brief Perform a qualification conversion, producing an rvalue.
462 SK_QualificationConversionRValue,
463 /// \brief Perform a qualification conversion, producing an lvalue.
464 SK_QualificationConversionLValue,
465 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000466 SK_ConversionSequence,
467 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000468 SK_ListInitialization,
469 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000470 SK_ConstructorInitialization,
471 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000472 SK_ZeroInitialization,
473 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000474 SK_CAssignment,
475 /// \brief Initialization by string
476 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000477 };
478
479 /// \brief A single step in the initialization sequence.
480 class Step {
481 public:
482 /// \brief The kind of conversion or initialization step we are taking.
483 StepKind Kind;
484
485 // \brief The type that results from this initialization.
486 QualType Type;
487
488 union {
489 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
490 /// SK_UserConversion, the function that the expression should be
491 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000492 ///
493 /// Always a FunctionDecl.
494 /// For conversion decls, the naming class is the source type.
495 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000496 struct {
497 FunctionDecl *Function;
498 DeclAccessPair FoundDecl;
499 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000500
501 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
502 /// sequence
503 ImplicitConversionSequence *ICS;
504 };
505
506 void Destroy();
507 };
508
509private:
510 /// \brief The kind of initialization sequence computed.
511 enum SequenceKind SequenceKind;
512
513 /// \brief Steps taken by this initialization.
514 llvm::SmallVector<Step, 4> Steps;
515
516public:
517 /// \brief Describes why initialization failed.
518 enum FailureKind {
519 /// \brief Too many initializers provided for a reference.
520 FK_TooManyInitsForReference,
521 /// \brief Array must be initialized with an initializer list.
522 FK_ArrayNeedsInitList,
523 /// \brief Array must be initialized with an initializer list or a
524 /// string literal.
525 FK_ArrayNeedsInitListOrStringLiteral,
526 /// \brief Cannot resolve the address of an overloaded function.
527 FK_AddressOfOverloadFailed,
528 /// \brief Overloading due to reference initialization failed.
529 FK_ReferenceInitOverloadFailed,
530 /// \brief Non-const lvalue reference binding to a temporary.
531 FK_NonConstLValueReferenceBindingToTemporary,
532 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
533 /// type.
534 FK_NonConstLValueReferenceBindingToUnrelated,
535 /// \brief Rvalue reference binding to an lvalue.
536 FK_RValueReferenceBindingToLValue,
537 /// \brief Reference binding drops qualifiers.
538 FK_ReferenceInitDropsQualifiers,
539 /// \brief Reference binding failed.
540 FK_ReferenceInitFailed,
541 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000542 FK_ConversionFailed,
543 /// \brief Too many initializers for scalar
544 FK_TooManyInitsForScalar,
545 /// \brief Reference initialization from an initializer list
546 FK_ReferenceBindingToInitList,
547 /// \brief Initialization of some unused destination type with an
548 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000549 FK_InitListBadDestinationType,
550 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000551 FK_UserConversionOverloadFailed,
552 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000553 FK_ConstructorOverloadFailed,
554 /// \brief Default-initialization of a 'const' object.
Douglas Gregor72a43bb2010-05-20 22:12:02 +0000555 FK_DefaultInitOfConst,
556 /// \brief Initialization of an incomplete type.
557 FK_Incomplete
Douglas Gregor20093b42009-12-09 23:02:17 +0000558 };
559
560private:
561 /// \brief The reason why initialization failued.
562 FailureKind Failure;
563
564 /// \brief The failed result of overload resolution.
565 OverloadingResult FailedOverloadResult;
566
567 /// \brief The candidate set created when initialization failed.
568 OverloadCandidateSet FailedCandidateSet;
Douglas Gregora41a8c52010-04-22 00:20:18 +0000569
570 /// \brief Prints a follow-up note that highlights the location of
571 /// the initialized entity, if it's remote.
572 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
573
Douglas Gregor20093b42009-12-09 23:02:17 +0000574public:
575 /// \brief Try to perform initialization of the given entity, creating a
576 /// record of the steps required to perform the initialization.
577 ///
578 /// The generated initialization sequence will either contain enough
579 /// information to diagnose
580 ///
581 /// \param S the semantic analysis object.
582 ///
583 /// \param Entity the entity being initialized.
584 ///
585 /// \param Kind the kind of initialization being performed.
586 ///
587 /// \param Args the argument(s) provided for initialization.
588 ///
589 /// \param NumArgs the number of arguments provided for initialization.
590 InitializationSequence(Sema &S,
591 const InitializedEntity &Entity,
592 const InitializationKind &Kind,
593 Expr **Args,
594 unsigned NumArgs);
595
596 ~InitializationSequence();
597
598 /// \brief Perform the actual initialization of the given entity based on
599 /// the computed initialization sequence.
600 ///
601 /// \param S the semantic analysis object.
602 ///
603 /// \param Entity the entity being initialized.
604 ///
605 /// \param Kind the kind of initialization being performed.
606 ///
607 /// \param Args the argument(s) provided for initialization, ownership of
608 /// which is transfered into the routine.
609 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000610 /// \param ResultType if non-NULL, will be set to the type of the
611 /// initialized object, which is the type of the declaration in most
612 /// cases. However, when the initialized object is a variable of
613 /// incomplete array type and the initializer is an initializer
614 /// list, this type will be set to the completed array type.
615 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000616 /// \returns an expression that performs the actual object initialization, if
617 /// the initialization is well-formed. Otherwise, emits diagnostics
618 /// and returns an invalid expression.
619 Action::OwningExprResult Perform(Sema &S,
620 const InitializedEntity &Entity,
621 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000622 Action::MultiExprArg Args,
623 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000624
625 /// \brief Diagnose an potentially-invalid initialization sequence.
626 ///
627 /// \returns true if the initialization sequence was ill-formed,
628 /// false otherwise.
629 bool Diagnose(Sema &S,
630 const InitializedEntity &Entity,
631 const InitializationKind &Kind,
632 Expr **Args, unsigned NumArgs);
633
634 /// \brief Determine the kind of initialization sequence computed.
635 enum SequenceKind getKind() const { return SequenceKind; }
636
637 /// \brief Set the kind of sequence computed.
638 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
639
640 /// \brief Determine whether the initialization sequence is valid.
641 operator bool() const { return SequenceKind != FailedSequence; }
642
643 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
644 step_iterator step_begin() const { return Steps.begin(); }
645 step_iterator step_end() const { return Steps.end(); }
646
Douglas Gregorb70cf442010-03-26 20:14:36 +0000647 /// \brief Determine whether this initialization is a direct reference
648 /// binding (C++ [dcl.init.ref]).
649 bool isDirectReferenceBinding() const;
650
651 /// \brief Determine whether this initialization failed due to an ambiguity.
652 bool isAmbiguous() const;
653
Douglas Gregord6e44a32010-04-16 22:09:46 +0000654 /// \brief Determine whether this initialization is direct call to a
655 /// constructor.
656 bool isConstructorInitialization() const;
657
Douglas Gregor20093b42009-12-09 23:02:17 +0000658 /// \brief Add a new step in the initialization that resolves the address
659 /// of an overloaded function to a specific function declaration.
660 ///
661 /// \param Function the function to which the overloaded function reference
662 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000663 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
664 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000665
666 /// \brief Add a new step in the initialization that performs a derived-to-
667 /// base cast.
668 ///
669 /// \param BaseType the base type to which we will be casting.
670 ///
671 /// \param IsLValue true if the result of this cast will be treated as
672 /// an lvalue.
673 void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue);
674
675 /// \brief Add a new step binding a reference to an object.
676 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000677 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000678 /// object (thereby extending its lifetime); false if we are binding to an
679 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000680 ///
681 /// \param UnnecessaryCopy True if we should check for a copy
682 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000683 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000684
685 /// \brief Add a new step that makes an extraneous copy of the input
686 /// to a temporary of the same class type.
687 ///
688 /// This extraneous copy only occurs during reference binding in
689 /// C++98/03, where we are permitted (but not required) to introduce
690 /// an extra copy. At a bare minimum, we must check that we could
691 /// call the copy constructor, and produce a diagnostic if the copy
692 /// constructor is inaccessible or no copy constructor matches.
693 //
694 /// \param T The type of the temporary being created.
695 void AddExtraneousCopyToTemporary(QualType T);
696
Douglas Gregor20093b42009-12-09 23:02:17 +0000697 /// \brief Add a new step invoking a conversion function, which is either
698 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000699 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000700 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000701 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000702
703 /// \brief Add a new step that performs a qualification conversion to the
704 /// given type.
705 void AddQualificationConversionStep(QualType Ty, bool IsLValue);
706
707 /// \brief Add a new step that applies an implicit conversion sequence.
708 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
709 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000710
711 /// \brief Add a list-initialiation step
712 void AddListInitializationStep(QualType T);
713
Douglas Gregor71d17402009-12-15 00:01:57 +0000714 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000715 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000716 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000717 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000718
719 /// \brief Add a zero-initialization step.
720 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000721
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000722 /// \brief Add a C assignment step.
723 //
724 // FIXME: It isn't clear whether this should ever be needed;
725 // ideally, we would handle everything needed in C in the common
726 // path. However, that isn't the case yet.
727 void AddCAssignmentStep(QualType T);
728
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000729 /// \brief Add a string init step.
730 void AddStringInitStep(QualType T);
731
Douglas Gregor20093b42009-12-09 23:02:17 +0000732 /// \brief Note that this initialization sequence failed.
733 void SetFailed(FailureKind Failure) {
734 SequenceKind = FailedSequence;
735 this->Failure = Failure;
736 }
737
738 /// \brief Note that this initialization sequence failed due to failed
739 /// overload resolution.
740 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
741
742 /// \brief Retrieve a reference to the candidate set when overload
743 /// resolution fails.
744 OverloadCandidateSet &getFailedCandidateSet() {
745 return FailedCandidateSet;
746 }
747
748 /// \brief Determine why initialization failed.
749 FailureKind getFailureKind() const {
750 assert(getKind() == FailedSequence && "Not an initialization failure!");
751 return Failure;
752 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000753
754 /// \brief Dump a representation of this initialization sequence to
755 /// the given stream, for debugging purposes.
756 void dump(llvm::raw_ostream &OS) const;
757
758 /// \brief Dump a representation of this initialization sequence to
759 /// standard error, for debugging purposes.
760 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000761};
762
763} // end namespace clang
764
765#endif // LLVM_CLANG_SEMA_INIT_H