blob: 4c94aa605812ca39a72d3f796346abc0854d4f4e [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,
Sebastian Redl906082e2010-07-20 04:20:21 +0000448 /// \brief Perform a derived-to-base cast, producing an xvalue.
449 SK_CastDerivedToBaseXValue,
Douglas Gregor20093b42009-12-09 23:02:17 +0000450 /// \brief Perform a derived-to-base cast, producing an lvalue.
451 SK_CastDerivedToBaseLValue,
452 /// \brief Reference binding to an lvalue.
453 SK_BindReference,
454 /// \brief Reference binding to a temporary.
455 SK_BindReferenceToTemporary,
Douglas Gregor523d46a2010-04-18 07:40:54 +0000456 /// \brief An optional copy of a temporary object to another
457 /// temporary object, which is permitted (but not required) by
458 /// C++98/03 but not C++0x.
459 SK_ExtraneousCopyToTemporary,
Douglas Gregor20093b42009-12-09 23:02:17 +0000460 /// \brief Perform a user-defined conversion, either via a conversion
461 /// function or via a constructor.
462 SK_UserConversion,
463 /// \brief Perform a qualification conversion, producing an rvalue.
464 SK_QualificationConversionRValue,
Sebastian Redl906082e2010-07-20 04:20:21 +0000465 /// \brief Perform a qualification conversion, producing an xvalue.
466 SK_QualificationConversionXValue,
Douglas Gregor20093b42009-12-09 23:02:17 +0000467 /// \brief Perform a qualification conversion, producing an lvalue.
468 SK_QualificationConversionLValue,
469 /// \brief Perform an implicit conversion sequence.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000470 SK_ConversionSequence,
471 /// \brief Perform list-initialization
Douglas Gregor51c56d62009-12-14 20:49:26 +0000472 SK_ListInitialization,
473 /// \brief Perform initialization via a constructor.
Douglas Gregor71d17402009-12-15 00:01:57 +0000474 SK_ConstructorInitialization,
475 /// \brief Zero-initialize the object
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000476 SK_ZeroInitialization,
477 /// \brief C assignment
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000478 SK_CAssignment,
479 /// \brief Initialization by string
480 SK_StringInit
Douglas Gregor20093b42009-12-09 23:02:17 +0000481 };
482
483 /// \brief A single step in the initialization sequence.
484 class Step {
485 public:
486 /// \brief The kind of conversion or initialization step we are taking.
487 StepKind Kind;
488
489 // \brief The type that results from this initialization.
490 QualType Type;
491
492 union {
493 /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind ==
494 /// SK_UserConversion, the function that the expression should be
495 /// resolved to or the conversion function to call, respectively.
John McCallb13b7372010-02-01 03:16:54 +0000496 ///
497 /// Always a FunctionDecl.
498 /// For conversion decls, the naming class is the source type.
499 /// For construct decls, the naming class is the target type.
John McCall9aa472c2010-03-19 07:35:19 +0000500 struct {
501 FunctionDecl *Function;
502 DeclAccessPair FoundDecl;
503 } Function;
Douglas Gregor20093b42009-12-09 23:02:17 +0000504
505 /// \brief When Kind = SK_ConversionSequence, the implicit conversion
506 /// sequence
507 ImplicitConversionSequence *ICS;
508 };
509
510 void Destroy();
511 };
512
513private:
514 /// \brief The kind of initialization sequence computed.
515 enum SequenceKind SequenceKind;
516
517 /// \brief Steps taken by this initialization.
518 llvm::SmallVector<Step, 4> Steps;
519
520public:
521 /// \brief Describes why initialization failed.
522 enum FailureKind {
523 /// \brief Too many initializers provided for a reference.
524 FK_TooManyInitsForReference,
525 /// \brief Array must be initialized with an initializer list.
526 FK_ArrayNeedsInitList,
527 /// \brief Array must be initialized with an initializer list or a
528 /// string literal.
529 FK_ArrayNeedsInitListOrStringLiteral,
530 /// \brief Cannot resolve the address of an overloaded function.
531 FK_AddressOfOverloadFailed,
532 /// \brief Overloading due to reference initialization failed.
533 FK_ReferenceInitOverloadFailed,
534 /// \brief Non-const lvalue reference binding to a temporary.
535 FK_NonConstLValueReferenceBindingToTemporary,
536 /// \brief Non-const lvalue reference binding to an lvalue of unrelated
537 /// type.
538 FK_NonConstLValueReferenceBindingToUnrelated,
539 /// \brief Rvalue reference binding to an lvalue.
540 FK_RValueReferenceBindingToLValue,
541 /// \brief Reference binding drops qualifiers.
542 FK_ReferenceInitDropsQualifiers,
543 /// \brief Reference binding failed.
544 FK_ReferenceInitFailed,
545 /// \brief Implicit conversion failed.
Douglas Gregord87b61f2009-12-10 17:56:55 +0000546 FK_ConversionFailed,
547 /// \brief Too many initializers for scalar
548 FK_TooManyInitsForScalar,
549 /// \brief Reference initialization from an initializer list
550 FK_ReferenceBindingToInitList,
551 /// \brief Initialization of some unused destination type with an
552 /// initializer list.
Douglas Gregor4a520a22009-12-14 17:27:33 +0000553 FK_InitListBadDestinationType,
554 /// \brief Overloading for a user-defined conversion failed.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000555 FK_UserConversionOverloadFailed,
556 /// \brief Overloaded for initialization by constructor failed.
Douglas Gregor99a2e602009-12-16 01:38:02 +0000557 FK_ConstructorOverloadFailed,
558 /// \brief Default-initialization of a 'const' object.
Douglas Gregor72a43bb2010-05-20 22:12:02 +0000559 FK_DefaultInitOfConst,
560 /// \brief Initialization of an incomplete type.
561 FK_Incomplete
Douglas Gregor20093b42009-12-09 23:02:17 +0000562 };
563
564private:
565 /// \brief The reason why initialization failued.
566 FailureKind Failure;
567
568 /// \brief The failed result of overload resolution.
569 OverloadingResult FailedOverloadResult;
570
571 /// \brief The candidate set created when initialization failed.
572 OverloadCandidateSet FailedCandidateSet;
Douglas Gregora41a8c52010-04-22 00:20:18 +0000573
574 /// \brief Prints a follow-up note that highlights the location of
575 /// the initialized entity, if it's remote.
576 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
577
Douglas Gregor20093b42009-12-09 23:02:17 +0000578public:
579 /// \brief Try to perform initialization of the given entity, creating a
580 /// record of the steps required to perform the initialization.
581 ///
582 /// The generated initialization sequence will either contain enough
583 /// information to diagnose
584 ///
585 /// \param S the semantic analysis object.
586 ///
587 /// \param Entity the entity being initialized.
588 ///
589 /// \param Kind the kind of initialization being performed.
590 ///
591 /// \param Args the argument(s) provided for initialization.
592 ///
593 /// \param NumArgs the number of arguments provided for initialization.
594 InitializationSequence(Sema &S,
595 const InitializedEntity &Entity,
596 const InitializationKind &Kind,
597 Expr **Args,
598 unsigned NumArgs);
599
600 ~InitializationSequence();
601
602 /// \brief Perform the actual initialization of the given entity based on
603 /// the computed initialization sequence.
604 ///
605 /// \param S the semantic analysis object.
606 ///
607 /// \param Entity the entity being initialized.
608 ///
609 /// \param Kind the kind of initialization being performed.
610 ///
611 /// \param Args the argument(s) provided for initialization, ownership of
612 /// which is transfered into the routine.
613 ///
Douglas Gregord87b61f2009-12-10 17:56:55 +0000614 /// \param ResultType if non-NULL, will be set to the type of the
615 /// initialized object, which is the type of the declaration in most
616 /// cases. However, when the initialized object is a variable of
617 /// incomplete array type and the initializer is an initializer
618 /// list, this type will be set to the completed array type.
619 ///
Douglas Gregor20093b42009-12-09 23:02:17 +0000620 /// \returns an expression that performs the actual object initialization, if
621 /// the initialization is well-formed. Otherwise, emits diagnostics
622 /// and returns an invalid expression.
623 Action::OwningExprResult Perform(Sema &S,
624 const InitializedEntity &Entity,
625 const InitializationKind &Kind,
Douglas Gregord87b61f2009-12-10 17:56:55 +0000626 Action::MultiExprArg Args,
627 QualType *ResultType = 0);
Douglas Gregor20093b42009-12-09 23:02:17 +0000628
629 /// \brief Diagnose an potentially-invalid initialization sequence.
630 ///
631 /// \returns true if the initialization sequence was ill-formed,
632 /// false otherwise.
633 bool Diagnose(Sema &S,
634 const InitializedEntity &Entity,
635 const InitializationKind &Kind,
636 Expr **Args, unsigned NumArgs);
637
638 /// \brief Determine the kind of initialization sequence computed.
639 enum SequenceKind getKind() const { return SequenceKind; }
640
641 /// \brief Set the kind of sequence computed.
642 void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
643
644 /// \brief Determine whether the initialization sequence is valid.
645 operator bool() const { return SequenceKind != FailedSequence; }
646
647 typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator;
648 step_iterator step_begin() const { return Steps.begin(); }
649 step_iterator step_end() const { return Steps.end(); }
650
Douglas Gregorb70cf442010-03-26 20:14:36 +0000651 /// \brief Determine whether this initialization is a direct reference
652 /// binding (C++ [dcl.init.ref]).
653 bool isDirectReferenceBinding() const;
654
655 /// \brief Determine whether this initialization failed due to an ambiguity.
656 bool isAmbiguous() const;
657
Douglas Gregord6e44a32010-04-16 22:09:46 +0000658 /// \brief Determine whether this initialization is direct call to a
659 /// constructor.
660 bool isConstructorInitialization() const;
661
Douglas Gregor20093b42009-12-09 23:02:17 +0000662 /// \brief Add a new step in the initialization that resolves the address
663 /// of an overloaded function to a specific function declaration.
664 ///
665 /// \param Function the function to which the overloaded function reference
666 /// resolves.
John McCall6bb80172010-03-30 21:47:33 +0000667 void AddAddressOverloadResolutionStep(FunctionDecl *Function,
668 DeclAccessPair Found);
Douglas Gregor20093b42009-12-09 23:02:17 +0000669
670 /// \brief Add a new step in the initialization that performs a derived-to-
671 /// base cast.
672 ///
673 /// \param BaseType the base type to which we will be casting.
674 ///
675 /// \param IsLValue true if the result of this cast will be treated as
676 /// an lvalue.
Sebastian Redl906082e2010-07-20 04:20:21 +0000677 void AddDerivedToBaseCastStep(QualType BaseType,
678 ImplicitCastExpr::ResultCategory Category);
Douglas Gregor20093b42009-12-09 23:02:17 +0000679
680 /// \brief Add a new step binding a reference to an object.
681 ///
Douglas Gregor523d46a2010-04-18 07:40:54 +0000682 /// \param BindingTemporary True if we are binding a reference to a temporary
Douglas Gregor20093b42009-12-09 23:02:17 +0000683 /// object (thereby extending its lifetime); false if we are binding to an
684 /// lvalue or an lvalue treated as an rvalue.
Douglas Gregor523d46a2010-04-18 07:40:54 +0000685 ///
686 /// \param UnnecessaryCopy True if we should check for a copy
687 /// constructor for a completely unnecessary but
Douglas Gregor20093b42009-12-09 23:02:17 +0000688 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
Douglas Gregor523d46a2010-04-18 07:40:54 +0000689
690 /// \brief Add a new step that makes an extraneous copy of the input
691 /// to a temporary of the same class type.
692 ///
693 /// This extraneous copy only occurs during reference binding in
694 /// C++98/03, where we are permitted (but not required) to introduce
695 /// an extra copy. At a bare minimum, we must check that we could
696 /// call the copy constructor, and produce a diagnostic if the copy
697 /// constructor is inaccessible or no copy constructor matches.
698 //
699 /// \param T The type of the temporary being created.
700 void AddExtraneousCopyToTemporary(QualType T);
701
Douglas Gregor20093b42009-12-09 23:02:17 +0000702 /// \brief Add a new step invoking a conversion function, which is either
703 /// a constructor or a conversion function.
John McCallb13b7372010-02-01 03:16:54 +0000704 void AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +0000705 DeclAccessPair FoundDecl,
John McCallb13b7372010-02-01 03:16:54 +0000706 QualType T);
Douglas Gregor20093b42009-12-09 23:02:17 +0000707
708 /// \brief Add a new step that performs a qualification conversion to the
709 /// given type.
Sebastian Redl906082e2010-07-20 04:20:21 +0000710 void AddQualificationConversionStep(QualType Ty,
711 ImplicitCastExpr::ResultCategory Category);
Douglas Gregor20093b42009-12-09 23:02:17 +0000712
713 /// \brief Add a new step that applies an implicit conversion sequence.
714 void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
715 QualType T);
Douglas Gregord87b61f2009-12-10 17:56:55 +0000716
717 /// \brief Add a list-initialiation step
718 void AddListInitializationStep(QualType T);
719
Douglas Gregor71d17402009-12-15 00:01:57 +0000720 /// \brief Add a constructor-initialization step.
Douglas Gregor51c56d62009-12-14 20:49:26 +0000721 void AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +0000722 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +0000723 QualType T);
Douglas Gregor71d17402009-12-15 00:01:57 +0000724
725 /// \brief Add a zero-initialization step.
726 void AddZeroInitializationStep(QualType T);
Douglas Gregor51c56d62009-12-14 20:49:26 +0000727
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000728 /// \brief Add a C assignment step.
729 //
730 // FIXME: It isn't clear whether this should ever be needed;
731 // ideally, we would handle everything needed in C in the common
732 // path. However, that isn't the case yet.
733 void AddCAssignmentStep(QualType T);
734
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000735 /// \brief Add a string init step.
736 void AddStringInitStep(QualType T);
737
Douglas Gregor20093b42009-12-09 23:02:17 +0000738 /// \brief Note that this initialization sequence failed.
739 void SetFailed(FailureKind Failure) {
740 SequenceKind = FailedSequence;
741 this->Failure = Failure;
742 }
743
744 /// \brief Note that this initialization sequence failed due to failed
745 /// overload resolution.
746 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
747
748 /// \brief Retrieve a reference to the candidate set when overload
749 /// resolution fails.
750 OverloadCandidateSet &getFailedCandidateSet() {
751 return FailedCandidateSet;
752 }
753
754 /// \brief Determine why initialization failed.
755 FailureKind getFailureKind() const {
756 assert(getKind() == FailedSequence && "Not an initialization failure!");
757 return Failure;
758 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +0000759
760 /// \brief Dump a representation of this initialization sequence to
761 /// the given stream, for debugging purposes.
762 void dump(llvm::raw_ostream &OS) const;
763
764 /// \brief Dump a representation of this initialization sequence to
765 /// standard error, for debugging purposes.
766 void dump() const;
Douglas Gregor20093b42009-12-09 23:02:17 +0000767};
768
769} // end namespace clang
770
771#endif // LLVM_CLANG_SEMA_INIT_H