clang 23.0.0git
Initialization.h
Go to the documentation of this file.
1//===- Initialization.h - Semantic Analysis for Initializers ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides supporting data types for initialization of objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_SEMA_INITIALIZATION_H
14#define LLVM_CLANG_SEMA_INITIALIZATION_H
15
17#include "clang/AST/Attr.h"
18#include "clang/AST/Decl.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/Type.h"
24#include "clang/Basic/LLVM.h"
28#include "clang/Sema/Overload.h"
30#include "llvm/ADT/ArrayRef.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/iterator_range.h"
34#include "llvm/Support/Casting.h"
35#include <cassert>
36#include <cstdint>
37#include <string>
38
39namespace clang {
40
43class ObjCMethodDecl;
44class Sema;
45
46/// Describes an entity that is being initialized.
47class alignas(8) InitializedEntity {
48public:
49 /// Specifies the kind of entity being initialized.
51 /// The entity being initialized is a variable.
53
54 /// The entity being initialized is a function parameter.
56
57 /// The entity being initialized is a non-type template parameter.
59
60 /// The entity being initialized is the result of a function call.
62
63 /// The entity being initialized is the result of a statement expression.
65
66 /// The entity being initialized is an exception object that
67 /// is being thrown.
69
70 /// The entity being initialized is a non-static data member
71 /// subobject.
73
74 /// The entity being initialized is an element of an array.
76
77 /// The entity being initialized is an object (or array of
78 /// objects) allocated via new.
80
81 /// The entity being initialized is a temporary object.
83
84 /// The entity being initialized is a base member subobject.
86
87 /// The initialization is being done by a delegating constructor.
89
90 /// The entity being initialized is an element of a vector.
91 /// or vector.
93
94 /// The entity being initialized is an element of a matrix.
95 /// or matrix.
97
98 /// The entity being initialized is a field of block descriptor for
99 /// the copied-in c++ object.
101
102 /// The entity being initialized is a field of block descriptor for the
103 /// copied-in lambda object that's used in the lambda to block conversion.
105
106 /// The entity being initialized is the real or imaginary part of a
107 /// complex number.
109
110 /// The entity being initialized is the field that captures a
111 /// variable in a lambda.
113
114 /// The entity being initialized is the initializer for a compound
115 /// literal.
117
118 /// The entity being implicitly initialized back to the formal
119 /// result type.
121
122 /// The entity being initialized is a function parameter; function
123 /// is member of group of audited CF APIs.
125
126 /// The entity being initialized is a structured binding of a
127 /// decomposition declaration.
129
130 /// The entity being initialized is a non-static data member subobject of an
131 /// object initialized via parenthesized aggregate initialization.
133
134 // Note: err_init_conversion_failed in DiagnosticSemaKinds.td uses this
135 // enum as an index for its first %select. When modifying this list,
136 // that diagnostic text needs to be updated as well.
137 };
138
139 enum class NRVOKind : uint8_t { Forbidden, Allowed };
140
141 enum class NewArrayKind : uint8_t {
144 };
145
152
153private:
154 /// The kind of entity being initialized.
155 EntityKind Kind;
156
157 /// If non-NULL, the parent entity in which this
158 /// initialization occurs.
159 const InitializedEntity *Parent = nullptr;
160
161 /// The type of the object or reference being initialized.
163
164 /// The mangling number for the next reference temporary to be created.
165 mutable unsigned ManglingNumber = 0;
166
167 struct LN {
168 /// When Kind == EK_Result, EK_Exception, EK_New, the
169 /// location of the 'return', 'throw', or 'new' keyword,
170 /// respectively. When Kind == EK_Temporary, the location where
171 /// the temporary is being created.
172 SourceLocation Location;
173
174 /// Whether the entity being initialized may end up using the
175 /// named return value optimization (NRVO).
176 NRVOKind NRVO;
177
178 /// When Kind == EK_New, whether this is initializing an array of runtime
179 /// size (which needs an array filler).
180 NewArrayKind IsVariableLengthArrayNew;
181 };
182
183 struct VD {
184 /// The VarDecl, FieldDecl, TemplateParmDecl, or BindingDecl being
185 /// initialized.
186 NamedDecl *VariableOrMember;
187
188 /// When Kind == EK_Member or EK_ParenAggInitMember, whether this is:
189 /// - ImplicitField: an implicit member initialization in a copy or move
190 /// constructor. These can perform array copies.
191 /// - DefaultMember: the initial initialization check for a default member
192 /// initialize.
193 /// - ParenAgg: aggregate initialization with a parenthesized list.
194 /// - Normal: simple member initialization.
195 FieldInitKind FieldKind;
196 };
197
198 struct C {
199 /// The name of the variable being captured by an EK_LambdaCapture.
200 IdentifierInfo *VarID;
201
202 /// The source location at which the capture occurs.
203 SourceLocation Location;
204 };
205
206 union {
207 /// When Kind == EK_Variable, EK_Member, EK_Binding, or
208 /// EK_TemplateParameter, the variable, binding, or template parameter.
210
211 /// When Kind == EK_RelatedResult, the ObjectiveC method where
212 /// result type was implicitly changed to accommodate ARC semantics.
214
215 /// When Kind == EK_Parameter, the ParmVarDecl, with the
216 /// integer indicating whether the parameter is "consumed".
217 llvm::PointerIntPair<ParmVarDecl *, 1> Parameter;
218
219 /// When Kind == EK_Temporary or EK_CompoundLiteralInit, the type
220 /// source information for the temporary.
222
223 struct LN LocAndNRVO;
224
225 /// When Kind == EK_Base, the base specifier that provides the
226 /// base class. The integer specifies whether the base is an inherited
227 /// virtual base.
228 llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base;
229
230 /// When Kind == EK_ArrayElement, EK_VectorElement, EK_MatrixElement,
231 /// or EK_ComplexElement, the index of the array or vector element being
232 /// initialized.
233 unsigned Index;
234
235 struct C Capture;
236 };
237
239
240 /// Create the initialization entity for a variable.
241 InitializedEntity(VarDecl *Var, EntityKind EK = EK_Variable)
242 : Kind(EK), Type(Var->getType()), Variable{Var, FieldInitKind::Normal} {}
243
244 /// Create the initialization entity for the result of a
245 /// function, throwing an object, performing an explicit cast, or
246 /// initializing a parameter for which there is no declaration.
247 InitializedEntity(
248 EntityKind Kind, SourceLocation Loc, QualType Type,
250 NewArrayKind VariableLengthArrayNew = NewArrayKind::KnownLength)
251 : Kind(Kind), Type(Type) {
252 new (&LocAndNRVO) LN{Loc, NRVO, VariableLengthArrayNew};
253 }
254
255 /// Create the initialization entity for a member subobject.
256 InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent,
257 FieldInitKind FieldKind)
258 : Kind(FieldKind == FieldInitKind::ParenAgg ? EK_ParenAggInitMember
259 : EK_Member),
260 Parent(Parent), Type(Member->getType()), Variable{Member, FieldKind} {}
261
262 /// Create the initialization entity for an array element.
263 InitializedEntity(ASTContext &Context, unsigned Index,
264 const InitializedEntity &Parent);
265
266 /// Create the initialization entity for a lambda capture.
267 InitializedEntity(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc)
268 : Kind(EK_LambdaCapture), Type(FieldType) {
269 new (&Capture) C{VarID, Loc};
270 }
271
272public:
273 /// Create the initialization entity for a variable.
274 static InitializedEntity InitializeVariable(VarDecl *Var) {
275 return InitializedEntity(Var);
276 }
277
278 /// Create the initialization entity for a parameter.
279 static InitializedEntity InitializeParameter(ASTContext &Context,
280 ParmVarDecl *Parm) {
281 return InitializeParameter(Context, Parm, Parm->getType());
282 }
283
284 /// Create the initialization entity for a parameter, but use
285 /// another type.
286 static InitializedEntity
288 bool Consumed = (Context.getLangOpts().ObjCAutoRefCount &&
289 Parm->hasAttr<NSConsumedAttr>());
290
291 InitializedEntity Entity;
292 Entity.Kind = EK_Parameter;
293 Entity.Type =
294 Context.getVariableArrayDecayedType(Type.getUnqualifiedType());
295 Entity.Parent = nullptr;
296 Entity.Parameter = {Parm, Consumed};
297 return Entity;
298 }
299
300 /// Create the initialization entity for a parameter that is
301 /// only known by its type.
302 static InitializedEntity InitializeParameter(ASTContext &Context,
303 QualType Type,
304 bool Consumed) {
305 InitializedEntity Entity;
306 Entity.Kind = EK_Parameter;
307 Entity.Type = Context.getVariableArrayDecayedType(Type);
308 Entity.Parent = nullptr;
309 Entity.Parameter = {nullptr, Consumed};
310 return Entity;
311 }
312
313 /// Create the initialization entity for a template parameter.
314 static InitializedEntity InitializeTemplateParameter(QualType T,
315 NamedDecl *Param) {
316 InitializedEntity Entity;
317 Entity.Kind = EK_TemplateParameter;
318 Entity.Type = T;
319 Entity.Parent = nullptr;
320 Entity.Variable = {Param, FieldInitKind::Normal};
321 return Entity;
322 }
323
324 /// Create the initialization entity for the result of a function.
325 static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
326 QualType Type) {
327 return InitializedEntity(EK_Result, ReturnLoc, Type);
328 }
329
330 static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc,
331 QualType Type) {
332 return InitializedEntity(EK_StmtExprResult, ReturnLoc, Type);
333 }
334
335 static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
336 QualType Type) {
337 return InitializedEntity(EK_BlockElement, BlockVarLoc, Type);
338 }
339
340 static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc,
341 QualType Type) {
342 return InitializedEntity(EK_LambdaToBlockConversionBlockElement,
343 BlockVarLoc, Type);
344 }
345
346 /// Create the initialization entity for an exception object.
347 static InitializedEntity InitializeException(SourceLocation ThrowLoc,
348 QualType Type) {
349 return InitializedEntity(EK_Exception, ThrowLoc, Type);
350 }
351
352 /// Create the initialization entity for an object allocated via new.
353 static InitializedEntity
355 NewArrayKind IsVariableLengthArrayNew) {
356 return InitializedEntity(EK_New, NewLoc, Type, NRVOKind::Forbidden,
357 IsVariableLengthArrayNew);
358 }
359
360 /// Create the initialization entity for a temporary.
361 static InitializedEntity InitializeTemporary(QualType Type) {
362 return InitializeTemporary(nullptr, Type);
363 }
364
365 /// Create the initialization entity for a temporary.
366 static InitializedEntity InitializeTemporary(ASTContext &Context,
368 QualType Type = TypeInfo->getType();
369 if (Context.getLangOpts().OpenCLCPlusPlus) {
370 assert(!Type.hasAddressSpace() && "Temporary already has address space!");
371 Type = Context.getAddrSpaceQualType(Type, LangAS::opencl_private);
372 }
373
374 return InitializeTemporary(TypeInfo, Type);
375 }
376
377 /// Create the initialization entity for a temporary.
378 static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo,
379 QualType Type) {
380 InitializedEntity Result(EK_Temporary, SourceLocation(), Type);
381 Result.TypeInfo = TypeInfo;
382 return Result;
383 }
384
385 /// Create the initialization entity for a related result.
386 static InitializedEntity InitializeRelatedResult(ObjCMethodDecl *MD,
387 QualType Type) {
388 InitializedEntity Result(EK_RelatedResult, SourceLocation(), Type);
389 Result.MethodDecl = MD;
390 return Result;
391 }
392
393 /// Create the initialization entity for a base class subobject.
394 static InitializedEntity
396 bool IsInheritedVirtualBase,
397 const InitializedEntity *Parent = nullptr);
398
399 /// Create the initialization entity for a delegated constructor.
400 static InitializedEntity InitializeDelegation(QualType Type) {
401 return InitializedEntity(EK_Delegating, SourceLocation(), Type);
402 }
403
404 /// Create the initialization entity for a member subobject.
405 static InitializedEntity
407 const InitializedEntity *Parent = nullptr) {
408 return InitializedEntity(Member, Parent, FieldInitKind::Normal);
409 }
410
411 /// Create the initialization entity for a member subobject.
412 static InitializedEntity
414 const InitializedEntity *Parent = nullptr) {
415 return InitializedEntity(Member->getAnonField(), Parent,
417 }
418
419 /// Create the initialization entity for a member subobject with implicit
420 /// field initializer.
421 static InitializedEntity InitializeMemberImplicit(FieldDecl *Member) {
422 return InitializedEntity(Member, /*Parent=*/nullptr,
424 }
425
426 /// Create the initialization entity for a member subobject with implicit
427 /// field initializer.
429 return InitializedEntity(Member->getAnonField(), /*Parent=*/nullptr,
431 }
432
433 /// Create the initialization entity for a member subobject initialized via
434 /// parenthesized aggregate init.
436 return InitializedEntity(Member, /*Parent=*/nullptr,
438 }
439
440 /// Create the initialization entity for a default member initializer.
441 static InitializedEntity
445
446 /// Create the initialization entity for an array element.
447 static InitializedEntity InitializeElement(ASTContext &Context,
448 unsigned Index,
449 const InitializedEntity &Parent) {
450 return InitializedEntity(Context, Index, Parent);
451 }
452
453 /// Create the initialization entity for a structured binding.
454 static InitializedEntity InitializeBinding(VarDecl *Binding) {
455 return InitializedEntity(Binding, EK_Binding);
456 }
457
458 /// Create the initialization entity for a lambda capture.
459 ///
460 /// \p VarID The name of the entity being captured, or nullptr for 'this'.
461 static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID,
462 QualType FieldType,
463 SourceLocation Loc) {
464 return InitializedEntity(VarID, FieldType, Loc);
465 }
466
467 /// Create the entity for a compound literal initializer.
468 static InitializedEntity InitializeCompoundLiteralInit(TypeSourceInfo *TSI) {
469 InitializedEntity Result(EK_CompoundLiteralInit, SourceLocation(),
470 TSI->getType());
471 Result.TypeInfo = TSI;
472 return Result;
473 }
474
475 /// Determine the kind of initialization.
476 EntityKind getKind() const { return Kind; }
477
478 /// Retrieve the parent of the entity being initialized, when
479 /// the initialization itself is occurring within the context of a
480 /// larger initialization.
481 const InitializedEntity *getParent() const { return Parent; }
482
483 /// Retrieve type being initialized.
484 QualType getType() const { return Type; }
485
486 /// Retrieve complete type-source information for the object being
487 /// constructed, if known.
489 if (Kind == EK_Temporary || Kind == EK_CompoundLiteralInit)
490 return TypeInfo;
491
492 return nullptr;
493 }
494
495 /// Retrieve the name of the entity being initialized.
496 DeclarationName getName() const;
497
498 /// Retrieve the variable, parameter, or field being
499 /// initialized.
500 ValueDecl *getDecl() const;
501
502 /// Retrieve the ObjectiveC method being initialized.
504
505 /// Determine whether this initialization allows the named return
506 /// value optimization, which also applies to thrown objects.
507 bool allowsNRVO() const;
508
509 bool isParameterKind() const {
510 return (getKind() == EK_Parameter ||
512 }
513
516 }
517
518 /// Determine whether this initialization consumes the
519 /// parameter.
520 bool isParameterConsumed() const {
521 assert(isParameterKind() && "Not a parameter");
522 return Parameter.getInt();
523 }
524
525 /// Retrieve the base specifier.
527 assert(getKind() == EK_Base && "Not a base specifier");
528 return Base.getPointer();
529 }
530
531 /// Return whether the base is an inherited virtual base.
533 assert(getKind() == EK_Base && "Not a base specifier");
534 return Base.getInt();
535 }
536
537 /// Determine whether this is an array new with an unknown bound.
539 return getKind() == EK_New &&
540 LocAndNRVO.IsVariableLengthArrayNew == NewArrayKind::UnknownLength;
541 }
542
543 /// Is this the implicit initialization of a member of a class from
544 /// a defaulted constructor?
546 return getKind() == EK_Member &&
548 }
549
550 /// Is this the default member initializer of a member (specified inside
551 /// the class definition)?
553 return getKind() == EK_Member &&
555 }
556
557 /// Determine the location of the 'return' keyword when initializing
558 /// the result of a function call.
560 assert(getKind() == EK_Result && "No 'return' location!");
561 return LocAndNRVO.Location;
562 }
563
564 /// Determine the location of the 'throw' keyword when initializing
565 /// an exception object.
567 assert(getKind() == EK_Exception && "No 'throw' location!");
568 return LocAndNRVO.Location;
569 }
570
571 /// If this is an array, vector, or complex number element, get the
572 /// element's index.
573 unsigned getElementIndex() const {
574 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
576 return Index;
577 }
578
579 /// If this is already the initializer for an array or vector
580 /// element, sets the element index.
581 void setElementIndex(unsigned Index) {
582 assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
584 this->Index = Index;
585 }
586
587 /// For a lambda capture, return the capture's name.
588 StringRef getCapturedVarName() const {
589 assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
590 return Capture.VarID ? Capture.VarID->getName() : "this";
591 }
592
593 /// Determine the location of the capture when initializing
594 /// field from a captured variable in a lambda.
596 assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
597 return Capture.Location;
598 }
599
603
604 unsigned allocateManglingNumber() const { return ++ManglingNumber; }
605
606 /// Dump a representation of the initialized entity to standard error,
607 /// for debugging purposes.
608 void dump() const;
609
610private:
611 unsigned dumpImpl(raw_ostream &OS) const;
612};
613
614/// Describes the kind of initialization being performed, along with
615/// location information for tokens related to the initialization (equal sign,
616/// parentheses).
617class InitializationKind {
618public:
619 /// The kind of initialization being performed.
620 enum InitKind {
621 /// Direct initialization
623
624 /// Direct list-initialization
626
627 /// Copy initialization
629
630 /// Default initialization
632
633 /// Value initialization
635 };
636
637private:
638 /// The context of the initialization.
639 enum InitContext {
640 /// Normal context
641 IC_Normal,
642
643 /// Normal context, but allows explicit conversion functions
644 IC_ExplicitConvs,
645
646 /// Implicit context (value initialization)
647 IC_Implicit,
648
649 /// Static cast context
650 IC_StaticCast,
651
652 /// C-style cast context
653 IC_CStyleCast,
654
655 /// Functional cast context
656 IC_FunctionalCast
657 };
658
659 /// The kind of initialization being performed.
660 InitKind Kind : 8;
661
662 /// The context of the initialization.
663 InitContext Context : 8;
664
665 /// The source locations involved in the initialization.
666 SourceLocation Locations[3];
667
668 InitializationKind(InitKind Kind, InitContext Context, SourceLocation Loc1,
669 SourceLocation Loc2, SourceLocation Loc3)
670 : Kind(Kind), Context(Context) {
671 Locations[0] = Loc1;
672 Locations[1] = Loc2;
673 Locations[2] = Loc3;
674 }
675
676public:
677 /// Create a direct initialization.
678 static InitializationKind CreateDirect(SourceLocation InitLoc,
679 SourceLocation LParenLoc,
680 SourceLocation RParenLoc) {
681 return InitializationKind(IK_Direct, IC_Normal,
682 InitLoc, LParenLoc, RParenLoc);
683 }
684
685 static InitializationKind CreateDirectList(SourceLocation InitLoc) {
686 return InitializationKind(IK_DirectList, IC_Normal, InitLoc, InitLoc,
687 InitLoc);
688 }
689
690 static InitializationKind CreateDirectList(SourceLocation InitLoc,
691 SourceLocation LBraceLoc,
692 SourceLocation RBraceLoc) {
693 return InitializationKind(IK_DirectList, IC_Normal, InitLoc, LBraceLoc,
694 RBraceLoc);
695 }
696
697 /// Create a direct initialization due to a cast that isn't a C-style
698 /// or functional cast.
699 static InitializationKind CreateCast(SourceRange TypeRange) {
700 return InitializationKind(IK_Direct, IC_StaticCast, TypeRange.getBegin(),
701 TypeRange.getBegin(), TypeRange.getEnd());
702 }
703
704 /// Create a direct initialization for a C-style cast.
705 static InitializationKind CreateCStyleCast(SourceLocation StartLoc,
706 SourceRange TypeRange,
707 bool InitList) {
708 // C++ cast syntax doesn't permit init lists, but C compound literals are
709 // exactly that.
710 return InitializationKind(InitList ? IK_DirectList : IK_Direct,
711 IC_CStyleCast, StartLoc, TypeRange.getBegin(),
712 TypeRange.getEnd());
713 }
714
715 /// Create a direct initialization for a functional cast.
716 static InitializationKind CreateFunctionalCast(SourceLocation StartLoc,
717 SourceRange ParenRange,
718 bool InitList) {
719 return InitializationKind(InitList ? IK_DirectList : IK_Direct,
720 IC_FunctionalCast, StartLoc,
721 ParenRange.getBegin(), ParenRange.getEnd());
722 }
723
724 /// Create a copy initialization.
725 static InitializationKind CreateCopy(SourceLocation InitLoc,
726 SourceLocation EqualLoc,
727 bool AllowExplicitConvs = false) {
728 return InitializationKind(IK_Copy,
729 AllowExplicitConvs? IC_ExplicitConvs : IC_Normal,
730 InitLoc, EqualLoc, EqualLoc);
731 }
732
733 /// Create a default initialization.
734 static InitializationKind CreateDefault(SourceLocation InitLoc) {
735 return InitializationKind(IK_Default, IC_Normal, InitLoc, InitLoc, InitLoc);
736 }
737
738 /// Create a value initialization.
739 static InitializationKind CreateValue(SourceLocation InitLoc,
740 SourceLocation LParenLoc,
741 SourceLocation RParenLoc,
742 bool isImplicit = false) {
743 return InitializationKind(IK_Value, isImplicit ? IC_Implicit : IC_Normal,
744 InitLoc, LParenLoc, RParenLoc);
745 }
746
747 /// Create an initialization from an initializer (which, for direct
748 /// initialization from a parenthesized list, will be a ParenListExpr).
749 static InitializationKind CreateForInit(SourceLocation Loc, bool DirectInit,
750 Expr *Init) {
751 if (!Init) return CreateDefault(Loc);
752 if (!DirectInit)
753 return CreateCopy(Loc, Init->getBeginLoc());
755 return CreateDirectList(Loc, Init->getBeginLoc(), Init->getEndLoc());
756 return CreateDirect(Loc, Init->getBeginLoc(), Init->getEndLoc());
757 }
758
759 /// Determine the initialization kind.
761 return Kind;
762 }
763
764 /// Determine whether this initialization is an explicit cast.
765 bool isExplicitCast() const {
766 return Context >= IC_StaticCast;
767 }
768
769 /// Determine whether this initialization is a static cast.
770 bool isStaticCast() const { return Context == IC_StaticCast; }
771
772 /// Determine whether this initialization is a C-style cast.
774 return Context >= IC_CStyleCast;
775 }
776
777 /// Determine whether this is a C-style cast.
778 bool isCStyleCast() const {
779 return Context == IC_CStyleCast;
780 }
781
782 /// Determine whether this is a functional-style cast.
783 bool isFunctionalCast() const {
784 return Context == IC_FunctionalCast;
785 }
786
787 /// Determine whether this initialization is an implicit
788 /// value-initialization, e.g., as occurs during aggregate
789 /// initialization.
790 bool isImplicitValueInit() const { return Context == IC_Implicit; }
791
792 /// Retrieve the location at which initialization is occurring.
793 SourceLocation getLocation() const { return Locations[0]; }
794
795 /// Retrieve the source range that covers the initialization.
797 return SourceRange(Locations[0], Locations[2]);
798 }
799
800 /// Retrieve the location of the equal sign for copy initialization
801 /// (if present).
803 assert(Kind == IK_Copy && "Only copy initialization has an '='");
804 return Locations[1];
805 }
806
807 bool isCopyInit() const { return Kind == IK_Copy; }
808
809 /// Retrieve whether this initialization allows the use of explicit
810 /// constructors.
811 bool AllowExplicit() const { return !isCopyInit(); }
812
813 /// Retrieve whether this initialization allows the use of explicit
814 /// conversion functions when binding a reference. If the reference is the
815 /// first parameter in a copy or move constructor, such conversions are
816 /// permitted even though we are performing copy-initialization.
818 return !isCopyInit() || Context == IC_ExplicitConvs;
819 }
820
821 /// Determine whether this initialization has a source range containing the
822 /// locations of open and closing parentheses or braces.
823 bool hasParenOrBraceRange() const {
824 return Kind == IK_Direct || Kind == IK_Value || Kind == IK_DirectList;
825 }
826
827 /// Retrieve the source range containing the locations of the open
828 /// and closing parentheses or braces for value, direct, and direct list
829 /// initializations.
831 assert(hasParenOrBraceRange() && "Only direct, value, and direct-list "
832 "initialization have parentheses or "
833 "braces");
834 return SourceRange(Locations[1], Locations[2]);
835 }
836};
837
838/// Describes the sequence of initializations required to initialize
839/// a given object or reference with a set of arguments.
841public:
842 /// Describes the kind of initialization sequence computed.
844 /// A failed initialization sequence. The failure kind tells what
845 /// happened.
847
848 /// A dependent initialization, which could not be
849 /// type-checked due to the presence of dependent types or
850 /// dependently-typed expressions.
852
853 /// A normal sequence.
855 };
856
857 /// Describes the kind of a particular step in an initialization
858 /// sequence.
859 enum StepKind {
860 /// Resolve the address of an overloaded function to a specific
861 /// function declaration.
863
864 /// Perform a derived-to-base cast, producing an rvalue.
866
867 /// Perform a derived-to-base cast, producing an xvalue.
869
870 /// Perform a derived-to-base cast, producing an lvalue.
872
873 /// Reference binding to an lvalue.
875
876 /// Reference binding to a temporary.
878
879 /// An optional copy of a temporary object to another
880 /// temporary object, which is permitted (but not required) by
881 /// C++98/03 but not C++0x.
883
884 /// Direct-initialization from a reference-related object in the
885 /// final stage of class copy-initialization.
887
888 /// Perform a user-defined conversion, either via a conversion
889 /// function or via a constructor.
891
892 /// Perform a qualification conversion, producing a prvalue.
894
895 /// Perform a qualification conversion, producing an xvalue.
897
898 /// Perform a qualification conversion, producing an lvalue.
900
901 /// Perform a function reference conversion, see [dcl.init.ref]p4.
903
904 /// Perform a conversion adding _Atomic to a type.
906
907 /// Perform an implicit conversion sequence.
909
910 /// Perform an implicit conversion sequence without narrowing.
912
913 /// Perform list-initialization without a constructor.
915
916 /// Unwrap the single-element initializer list for a reference.
918
919 /// Rewrap the single-element initializer list for a reference.
921
922 /// Perform initialization via a constructor.
924
925 /// Perform initialization via a constructor, taking arguments from
926 /// a single InitListExpr.
928
929 /// Zero-initialize the object
931
932 /// C assignment
934
935 /// Initialization by string
937
938 /// An initialization that "converts" an Objective-C object
939 /// (not a point to an object) to another Objective-C object type.
941
942 /// Array indexing for initialization by elementwise copy.
944
945 /// Array initialization by elementwise copy.
947
948 /// Array initialization (from an array rvalue).
950
951 /// Array initialization (from an array rvalue) as a GNU extension.
953
954 /// Array initialization from a parenthesized initializer list.
955 /// This is a GNU C++ extension.
957
958 /// Pass an object by indirect copy-and-restore.
960
961 /// Pass an object by indirect restore.
963
964 /// Produce an Objective-C object pointer.
966
967 /// Construct a std::initializer_list from an initializer list.
969
970 /// Perform initialization via a constructor taking a single
971 /// std::initializer_list argument.
973
974 /// Initialize an OpenCL sampler from an integer.
976
977 /// Initialize an opaque OpenCL type (event_t, queue_t, etc.) with zero
979
980 /// Initialize an aggreagate with parenthesized list of values.
981 /// This is a C++20 feature.
983 };
984
985 /// A single step in the initialization sequence.
986 class Step {
987 public:
988 /// The kind of conversion or initialization step we are taking.
990
991 // The type that results from this initialization.
993
999
1000 union {
1001 /// When Kind == SK_ResolvedOverloadedFunction or Kind ==
1002 /// SK_UserConversion, the function that the expression should be
1003 /// resolved to or the conversion function to call, respectively.
1004 /// When Kind == SK_ConstructorInitialization or SK_ListConstruction,
1005 /// the constructor to be called.
1006 ///
1007 /// Always a FunctionDecl, plus a Boolean flag telling if it was
1008 /// selected from an overloaded set having size greater than 1.
1009 /// For conversion decls, the naming class is the source type.
1010 /// For construct decls, the naming class is the target type.
1011 struct F Function;
1012
1013 /// When Kind = SK_ConversionSequence, the implicit conversion
1014 /// sequence.
1016
1017 /// When Kind = SK_RewrapInitList, the syntactic form of the
1018 /// wrapping list.
1020 };
1021
1022 void Destroy();
1023 };
1024
1025private:
1026 /// The kind of initialization sequence computed.
1028
1029 /// Steps taken by this initialization.
1031
1032public:
1033 /// Describes why initialization failed.
1035 /// Too many initializers provided for a reference.
1037
1038 /// Reference initialized from a parenthesized initializer list.
1040
1041 /// Array must be initialized with an initializer list.
1043
1044 /// Array must be initialized with an initializer list or a
1045 /// string literal.
1047
1048 /// Array must be initialized with an initializer list or a
1049 /// wide string literal.
1051
1052 /// Initializing a wide char array with narrow string literal.
1054
1055 /// Initializing char array with wide string literal.
1057
1058 /// Initializing wide char array with incompatible wide string
1059 /// literal.
1061
1062 /// Initializing char8_t array with plain string literal.
1064
1065 /// Initializing char array with UTF-8 string literal.
1067
1068 /// Array type mismatch.
1070
1071 /// Non-constant array initializer
1073
1074 /// Cannot resolve the address of an overloaded function.
1076
1077 /// Overloading due to reference initialization failed.
1079
1080 /// Non-const lvalue reference binding to a temporary.
1082
1083 /// Non-const lvalue reference binding to a bit-field.
1085
1086 /// Non-const lvalue reference binding to a vector element.
1088
1089 /// Non-const lvalue reference binding to a matrix element.
1091
1092 /// Non-const lvalue reference binding to an lvalue of unrelated
1093 /// type.
1095
1096 /// Rvalue reference binding to an lvalue.
1098
1099 /// Reference binding drops qualifiers.
1101
1102 /// Reference with mismatching address space binding to temporary.
1104
1105 /// Reference binding failed.
1107
1108 /// Implicit conversion failed.
1110
1111 /// Implicit conversion failed.
1113
1114 /// Too many initializers for scalar
1116
1117 /// Scalar initialized from a parenthesized initializer list.
1119
1120 /// Reference initialization from an initializer list
1122
1123 /// Initialization of some unused destination type with an
1124 /// initializer list.
1126
1127 /// Overloading for a user-defined conversion failed.
1129
1130 /// Overloading for initialization by constructor failed.
1132
1133 /// Overloading for list-initialization by constructor failed.
1135
1136 /// Default-initialization of a 'const' object.
1138
1139 /// Initialization of an incomplete type.
1141
1142 /// Variable-length array must not have an initializer.
1144
1145 /// List initialization failed at some point.
1147
1148 /// Initializer has a placeholder type which cannot be
1149 /// resolved by initialization.
1151
1152 /// Trying to take the address of a function that doesn't support
1153 /// having its address taken.
1155
1156 /// List-copy-initialization chose an explicit constructor.
1158
1159 /// Parenthesized list initialization failed at some point.
1160 /// This is a C++20 feature.
1162
1163 // A designated initializer was provided for a non-aggregate type.
1165
1166 /// HLSL intialization list flattening failed.
1168 };
1169
1170private:
1171 /// The reason why initialization failed.
1172 FailureKind Failure;
1173
1174 /// The failed result of overload resolution.
1175 OverloadingResult FailedOverloadResult;
1176
1177 /// The candidate set created when initialization failed.
1178 OverloadCandidateSet FailedCandidateSet;
1179
1180 /// The incomplete type that caused a failure.
1181 QualType FailedIncompleteType;
1182
1183 /// The fixit that needs to be applied to make this initialization
1184 /// succeed.
1185 std::string ZeroInitializationFixit;
1186 SourceLocation ZeroInitializationFixitLoc;
1187
1188public:
1189 /// Call for initializations are invalid but that would be valid
1190 /// zero initialzations if Fixit was applied.
1191 void SetZeroInitializationFixit(const std::string& Fixit, SourceLocation L) {
1192 ZeroInitializationFixit = Fixit;
1193 ZeroInitializationFixitLoc = L;
1194 }
1195
1196private:
1197 /// Prints a follow-up note that highlights the location of
1198 /// the initialized entity, if it's remote.
1199 void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
1200
1201public:
1202 /// Try to perform initialization of the given entity, creating a
1203 /// record of the steps required to perform the initialization.
1204 ///
1205 /// The generated initialization sequence will either contain enough
1206 /// information to diagnose
1207 ///
1208 /// \param S the semantic analysis object.
1209 ///
1210 /// \param Entity the entity being initialized.
1211 ///
1212 /// \param Kind the kind of initialization being performed.
1213 ///
1214 /// \param Args the argument(s) provided for initialization.
1215 ///
1216 /// \param TopLevelOfInitList true if we are initializing from an expression
1217 /// at the top level inside an initializer list. This disallows
1218 /// narrowing conversions in C++11 onwards.
1219 /// \param TreatUnavailableAsInvalid true if we want to treat unavailable
1220 /// as invalid.
1222 const InitializedEntity &Entity,
1223 const InitializationKind &Kind,
1224 MultiExprArg Args,
1225 bool TopLevelOfInitList = false,
1226 bool TreatUnavailableAsInvalid = true);
1227 void InitializeFrom(Sema &S, const InitializedEntity &Entity,
1228 const InitializationKind &Kind, MultiExprArg Args,
1229 bool TopLevelOfInitList, bool TreatUnavailableAsInvalid);
1230
1232
1233 /// Perform the actual initialization of the given entity based on
1234 /// the computed initialization sequence.
1235 ///
1236 /// \param S the semantic analysis object.
1237 ///
1238 /// \param Entity the entity being initialized.
1239 ///
1240 /// \param Kind the kind of initialization being performed.
1241 ///
1242 /// \param Args the argument(s) provided for initialization, ownership of
1243 /// which is transferred into the routine.
1244 ///
1245 /// \param ResultType if non-NULL, will be set to the type of the
1246 /// initialized object, which is the type of the declaration in most
1247 /// cases. However, when the initialized object is a variable of
1248 /// incomplete array type and the initializer is an initializer
1249 /// list, this type will be set to the completed array type.
1250 ///
1251 /// \returns an expression that performs the actual object initialization, if
1252 /// the initialization is well-formed. Otherwise, emits diagnostics
1253 /// and returns an invalid expression.
1255 const InitializedEntity &Entity,
1256 const InitializationKind &Kind,
1257 MultiExprArg Args,
1258 QualType *ResultType = nullptr);
1259
1260 /// Diagnose an potentially-invalid initialization sequence.
1261 ///
1262 /// \returns true if the initialization sequence was ill-formed,
1263 /// false otherwise.
1264 bool Diagnose(Sema &S,
1265 const InitializedEntity &Entity,
1266 const InitializationKind &Kind,
1267 ArrayRef<Expr *> Args);
1268
1269 /// Determine the kind of initialization sequence computed.
1270 enum SequenceKind getKind() const { return SequenceKind; }
1271
1272 /// Set the kind of sequence computed.
1274
1275 /// Determine whether the initialization sequence is valid.
1276 explicit operator bool() const { return !Failed(); }
1277
1278 /// Determine whether the initialization sequence is invalid.
1279 bool Failed() const { return SequenceKind == FailedSequence; }
1280
1282
1283 step_iterator step_begin() const { return Steps.begin(); }
1284 step_iterator step_end() const { return Steps.end(); }
1285
1286 using step_range = llvm::iterator_range<step_iterator>;
1287
1288 step_range steps() const { return {step_begin(), step_end()}; }
1289
1290 /// Determine whether this initialization is a direct reference
1291 /// binding (C++ [dcl.init.ref]).
1292 bool isDirectReferenceBinding() const;
1293
1294 /// Determine whether this initialization failed due to an ambiguity.
1295 bool isAmbiguous() const;
1296
1297 /// Determine whether this initialization is direct call to a
1298 /// constructor.
1299 bool isConstructorInitialization() const;
1300
1301 /// Add a new step in the initialization that resolves the address
1302 /// of an overloaded function to a specific function declaration.
1303 ///
1304 /// \param Function the function to which the overloaded function reference
1305 /// resolves.
1308 bool HadMultipleCandidates);
1309
1310 /// Add a new step in the initialization that performs a derived-to-
1311 /// base cast.
1312 ///
1313 /// \param BaseType the base type to which we will be casting.
1314 ///
1315 /// \param Category Indicates whether the result will be treated as an
1316 /// rvalue, an xvalue, or an lvalue.
1317 void AddDerivedToBaseCastStep(QualType BaseType,
1318 ExprValueKind Category);
1319
1320 /// Add a new step binding a reference to an object.
1321 ///
1322 /// \param BindingTemporary True if we are binding a reference to a temporary
1323 /// object (thereby extending its lifetime); false if we are binding to an
1324 /// lvalue or an lvalue treated as an rvalue.
1325 void AddReferenceBindingStep(QualType T, bool BindingTemporary);
1326
1327 /// Add a new step that makes an extraneous copy of the input
1328 /// to a temporary of the same class type.
1329 ///
1330 /// This extraneous copy only occurs during reference binding in
1331 /// C++98/03, where we are permitted (but not required) to introduce
1332 /// an extra copy. At a bare minimum, we must check that we could
1333 /// call the copy constructor, and produce a diagnostic if the copy
1334 /// constructor is inaccessible or no copy constructor matches.
1335 //
1336 /// \param T The type of the temporary being created.
1338
1339 /// Add a new step that makes a copy of the input to an object of
1340 /// the given type, as the final step in class copy-initialization.
1341 void AddFinalCopy(QualType T);
1342
1343 /// Add a new step invoking a conversion function, which is either
1344 /// a constructor or a conversion function.
1346 DeclAccessPair FoundDecl,
1347 QualType T,
1348 bool HadMultipleCandidates);
1349
1350 /// Add a new step that performs a qualification conversion to the
1351 /// given type.
1353 ExprValueKind Category);
1354
1355 /// Add a new step that performs a function reference conversion to the
1356 /// given type.
1358
1359 /// Add a new step that performs conversion from non-atomic to atomic
1360 /// type.
1362
1363 /// Add a new step that applies an implicit conversion sequence.
1365 QualType T, bool TopLevelOfInitList = false);
1366
1367 /// Add a list-initialization step.
1369
1370 /// Add a constructor-initialization step.
1371 ///
1372 /// \param FromInitList The constructor call is syntactically an initializer
1373 /// list.
1374 /// \param AsInitList The constructor is called as an init list constructor.
1377 QualType T,
1378 bool HadMultipleCandidates,
1379 bool FromInitList, bool AsInitList);
1380
1381 /// Add a zero-initialization step.
1383
1384 /// Add a C assignment step.
1385 //
1386 // FIXME: It isn't clear whether this should ever be needed;
1387 // ideally, we would handle everything needed in C in the common
1388 // path. However, that isn't the case yet.
1390
1391 /// Add a string init step.
1393
1394 /// Add an Objective-C object conversion step, which is
1395 /// always a no-op.
1397
1398 /// Add an array initialization loop step.
1399 void AddArrayInitLoopStep(QualType T, QualType EltTy);
1400
1401 /// Add an array initialization step.
1402 void AddArrayInitStep(QualType T, bool IsGNUExtension);
1403
1404 /// Add a parenthesized array initialization step.
1406
1407 /// Add a step to pass an object by indirect copy-restore.
1408 void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy);
1409
1410 /// Add a step to "produce" an Objective-C object (by
1411 /// retaining it).
1413
1414 /// Add a step to construct a std::initializer_list object from an
1415 /// initializer list.
1417
1418 /// Add a step to initialize an OpenCL sampler from an integer
1419 /// constant.
1421
1422 /// Add a step to initialzie an OpenCL opaque type (event_t, queue_t, etc.)
1423 /// from a zero constant.
1425
1427
1428 /// Only used when initializing structured bindings from an array with
1429 /// direct-list-initialization. Unwrap the initializer list to get the array
1430 /// for array copy.
1431 void AddUnwrapInitListInitStep(InitListExpr *Syntactic);
1432
1433 /// Add steps to unwrap a initializer list for a reference around a
1434 /// single element and rewrap it at the end.
1435 void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic);
1436
1437 /// Note that this initialization sequence failed.
1438 void SetFailed(FailureKind Failure) {
1440 this->Failure = Failure;
1441 assert((Failure != FK_Incomplete || !FailedIncompleteType.isNull()) &&
1442 "Incomplete type failure requires a type!");
1443 }
1444
1445 /// Note that this initialization sequence failed due to failed
1446 /// overload resolution.
1447 void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
1448
1449 /// Retrieve a reference to the candidate set when overload
1450 /// resolution fails.
1452 return FailedCandidateSet;
1453 }
1454
1455 /// Get the overloading result, for when the initialization
1456 /// sequence failed due to a bad overload.
1458 return FailedOverloadResult;
1459 }
1460
1461 /// Note that this initialization sequence failed due to an
1462 /// incomplete type.
1463 void setIncompleteTypeFailure(QualType IncompleteType) {
1464 FailedIncompleteType = IncompleteType;
1466 }
1467
1468 /// Determine why initialization failed.
1470 assert(Failed() && "Not an initialization failure!");
1471 return Failure;
1472 }
1473
1474 /// Dump a representation of this initialization sequence to
1475 /// the given stream, for debugging purposes.
1476 void dump(raw_ostream &OS) const;
1477
1478 /// Dump a representation of this initialization sequence to
1479 /// standard error, for debugging purposes.
1480 void dump() const;
1481};
1482
1483} // namespace clang
1484
1485#endif // LLVM_CLANG_SEMA_INITIALIZATION_H
Defines the clang::ASTContext interface.
TokenType getType() const
Returns the token's type, e.g.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a C++ constructor within a class.
Definition DeclCXX.h:2624
A POD class for pairing a NamedDecl* with an access specifier.
bool hasAttr() const
Definition DeclBase.h:585
The name of a declaration.
This represents one expression.
Definition Expr.h:112
Represents a member of a struct/union/class.
Definition Decl.h:3175
Represents a function declaration or definition.
Definition Decl.h:2015
One of these records is kept for each identifier that is lexed.
ImplicitConversionSequence - Represents an implicit conversion sequence, which may be a standard conv...
Definition Overload.h:622
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3482
Describes an C or C++ initializer list.
Definition Expr.h:5302
Describes the kind of initialization being performed, along with location information for tokens rela...
SourceLocation getLocation() const
Retrieve the location at which initialization is occurring.
static InitializationKind CreateCast(SourceRange TypeRange)
Create a direct initialization due to a cast that isn't a C-style or functional cast.
InitKind
The kind of initialization being performed.
@ IK_DirectList
Direct list-initialization.
@ IK_Value
Value initialization.
@ IK_Direct
Direct initialization.
@ IK_Copy
Copy initialization.
@ IK_Default
Default initialization.
SourceLocation getEqualLoc() const
Retrieve the location of the equal sign for copy initialization (if present).
bool isCStyleOrFunctionalCast() const
Determine whether this initialization is a C-style cast.
static InitializationKind CreateDefault(SourceLocation InitLoc)
Create a default initialization.
bool hasParenOrBraceRange() const
Determine whether this initialization has a source range containing the locations of open and closing...
bool isExplicitCast() const
Determine whether this initialization is an explicit cast.
bool isFunctionalCast() const
Determine whether this is a functional-style cast.
InitKind getKind() const
Determine the initialization kind.
static InitializationKind CreateFunctionalCast(SourceLocation StartLoc, SourceRange ParenRange, bool InitList)
Create a direct initialization for a functional cast.
bool AllowExplicit() const
Retrieve whether this initialization allows the use of explicit constructors.
static InitializationKind CreateDirect(SourceLocation InitLoc, SourceLocation LParenLoc, SourceLocation RParenLoc)
Create a direct initialization.
static InitializationKind CreateForInit(SourceLocation Loc, bool DirectInit, Expr *Init)
Create an initialization from an initializer (which, for direct initialization from a parenthesized l...
bool isImplicitValueInit() const
Determine whether this initialization is an implicit value-initialization, e.g., as occurs during agg...
bool isCStyleCast() const
Determine whether this is a C-style cast.
static InitializationKind CreateCopy(SourceLocation InitLoc, SourceLocation EqualLoc, bool AllowExplicitConvs=false)
Create a copy initialization.
static InitializationKind CreateDirectList(SourceLocation InitLoc)
SourceRange getParenOrBraceRange() const
Retrieve the source range containing the locations of the open and closing parentheses or braces for ...
bool allowExplicitConversionFunctionsInRefBinding() const
Retrieve whether this initialization allows the use of explicit conversion functions when binding a r...
static InitializationKind CreateDirectList(SourceLocation InitLoc, SourceLocation LBraceLoc, SourceLocation RBraceLoc)
bool isStaticCast() const
Determine whether this initialization is a static cast.
static InitializationKind CreateValue(SourceLocation InitLoc, SourceLocation LParenLoc, SourceLocation RParenLoc, bool isImplicit=false)
Create a value initialization.
SourceRange getRange() const
Retrieve the source range that covers the initialization.
static InitializationKind CreateCStyleCast(SourceLocation StartLoc, SourceRange TypeRange, bool InitList)
Create a direct initialization for a C-style cast.
A single step in the initialization sequence.
StepKind Kind
The kind of conversion or initialization step we are taking.
InitListExpr * WrappingSyntacticList
When Kind = SK_RewrapInitList, the syntactic form of the wrapping list.
ImplicitConversionSequence * ICS
When Kind = SK_ConversionSequence, the implicit conversion sequence.
struct F Function
When Kind == SK_ResolvedOverloadedFunction or Kind == SK_UserConversion, the function that the expres...
step_iterator step_begin() const
void AddListInitializationStep(QualType T)
Add a list-initialization step.
ExprResult Perform(Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, MultiExprArg Args, QualType *ResultType=nullptr)
Perform the actual initialization of the given entity based on the computed initialization sequence.
void AddStringInitStep(QualType T)
Add a string init step.
void AddStdInitializerListConstructionStep(QualType T)
Add a step to construct a std::initializer_list object from an initializer list.
void AddConstructorInitializationStep(DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, bool HadMultipleCandidates, bool FromInitList, bool AsInitList)
Add a constructor-initialization step.
StepKind
Describes the kind of a particular step in an initialization sequence.
@ SK_StdInitializerListConstructorCall
Perform initialization via a constructor taking a single std::initializer_list argument.
@ SK_AtomicConversion
Perform a conversion adding _Atomic to a type.
@ SK_ObjCObjectConversion
An initialization that "converts" an Objective-C object (not a point to an object) to another Objecti...
@ SK_GNUArrayInit
Array initialization (from an array rvalue) as a GNU extension.
@ SK_CastDerivedToBaseLValue
Perform a derived-to-base cast, producing an lvalue.
@ SK_ProduceObjCObject
Produce an Objective-C object pointer.
@ SK_FunctionReferenceConversion
Perform a function reference conversion, see [dcl.init.ref]p4.
@ SK_BindReference
Reference binding to an lvalue.
@ SK_ArrayLoopInit
Array initialization by elementwise copy.
@ SK_ConstructorInitialization
Perform initialization via a constructor.
@ SK_OCLSamplerInit
Initialize an OpenCL sampler from an integer.
@ SK_StringInit
Initialization by string.
@ SK_ZeroInitialization
Zero-initialize the object.
@ SK_CastDerivedToBaseXValue
Perform a derived-to-base cast, producing an xvalue.
@ SK_QualificationConversionXValue
Perform a qualification conversion, producing an xvalue.
@ SK_UserConversion
Perform a user-defined conversion, either via a conversion function or via a constructor.
@ SK_CastDerivedToBasePRValue
Perform a derived-to-base cast, producing an rvalue.
@ SK_BindReferenceToTemporary
Reference binding to a temporary.
@ SK_PassByIndirectRestore
Pass an object by indirect restore.
@ SK_ParenthesizedArrayInit
Array initialization from a parenthesized initializer list.
@ SK_ParenthesizedListInit
Initialize an aggreagate with parenthesized list of values.
@ SK_ArrayInit
Array initialization (from an array rvalue).
@ SK_ExtraneousCopyToTemporary
An optional copy of a temporary object to another temporary object, which is permitted (but not requi...
@ SK_ArrayLoopIndex
Array indexing for initialization by elementwise copy.
@ SK_ConversionSequenceNoNarrowing
Perform an implicit conversion sequence without narrowing.
@ SK_RewrapInitList
Rewrap the single-element initializer list for a reference.
@ SK_ConstructorInitializationFromList
Perform initialization via a constructor, taking arguments from a single InitListExpr.
@ SK_PassByIndirectCopyRestore
Pass an object by indirect copy-and-restore.
@ SK_ResolveAddressOfOverloadedFunction
Resolve the address of an overloaded function to a specific function declaration.
@ SK_UnwrapInitList
Unwrap the single-element initializer list for a reference.
@ SK_FinalCopy
Direct-initialization from a reference-related object in the final stage of class copy-initialization...
@ SK_QualificationConversionLValue
Perform a qualification conversion, producing an lvalue.
@ SK_StdInitializerList
Construct a std::initializer_list from an initializer list.
@ SK_QualificationConversionPRValue
Perform a qualification conversion, producing a prvalue.
@ SK_ConversionSequence
Perform an implicit conversion sequence.
@ SK_ListInitialization
Perform list-initialization without a constructor.
@ SK_OCLZeroOpaqueType
Initialize an opaque OpenCL type (event_t, queue_t, etc.) with zero.
void AddUserConversionStep(FunctionDecl *Function, DeclAccessPair FoundDecl, QualType T, bool HadMultipleCandidates)
Add a new step invoking a conversion function, which is either a constructor or a conversion function...
void SetZeroInitializationFixit(const std::string &Fixit, SourceLocation L)
Call for initializations are invalid but that would be valid zero initialzations if Fixit was applied...
llvm::iterator_range< step_iterator > step_range
InitializationSequence(Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, MultiExprArg Args, bool TopLevelOfInitList=false, bool TreatUnavailableAsInvalid=true)
Try to perform initialization of the given entity, creating a record of the steps required to perform...
void AddQualificationConversionStep(QualType Ty, ExprValueKind Category)
Add a new step that performs a qualification conversion to the given type.
void AddFunctionReferenceConversionStep(QualType Ty)
Add a new step that performs a function reference conversion to the given type.
void AddDerivedToBaseCastStep(QualType BaseType, ExprValueKind Category)
Add a new step in the initialization that performs a derived-to- base cast.
FailureKind getFailureKind() const
Determine why initialization failed.
void InitializeFrom(Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, MultiExprArg Args, bool TopLevelOfInitList, bool TreatUnavailableAsInvalid)
void AddParenthesizedListInitStep(QualType T)
void SetFailed(FailureKind Failure)
Note that this initialization sequence failed.
bool isAmbiguous() const
Determine whether this initialization failed due to an ambiguity.
void AddUnwrapInitListInitStep(InitListExpr *Syntactic)
Only used when initializing structured bindings from an array with direct-list-initialization.
void AddOCLZeroOpaqueTypeStep(QualType T)
Add a step to initialzie an OpenCL opaque type (event_t, queue_t, etc.) from a zero constant.
void AddFinalCopy(QualType T)
Add a new step that makes a copy of the input to an object of the given type, as the final step in cl...
OverloadingResult getFailedOverloadResult() const
Get the overloading result, for when the initialization sequence failed due to a bad overload.
void setSequenceKind(enum SequenceKind SK)
Set the kind of sequence computed.
void AddObjCObjectConversionStep(QualType T)
Add an Objective-C object conversion step, which is always a no-op.
void SetOverloadFailure(FailureKind Failure, OverloadingResult Result)
Note that this initialization sequence failed due to failed overload resolution.
step_iterator step_end() const
void AddParenthesizedArrayInitStep(QualType T)
Add a parenthesized array initialization step.
void AddExtraneousCopyToTemporary(QualType T)
Add a new step that makes an extraneous copy of the input to a temporary of the same class type.
void setIncompleteTypeFailure(QualType IncompleteType)
Note that this initialization sequence failed due to an incomplete type.
void AddOCLSamplerInitStep(QualType T)
Add a step to initialize an OpenCL sampler from an integer constant.
void AddCAssignmentStep(QualType T)
Add a C assignment step.
void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy)
Add a step to pass an object by indirect copy-restore.
void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic)
Add steps to unwrap a initializer list for a reference around a single element and rewrap it at the e...
bool Diagnose(Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, ArrayRef< Expr * > Args)
Diagnose an potentially-invalid initialization sequence.
bool Failed() const
Determine whether the initialization sequence is invalid.
void AddAtomicConversionStep(QualType Ty)
Add a new step that performs conversion from non-atomic to atomic type.
void dump() const
Dump a representation of this initialization sequence to standard error, for debugging purposes.
void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, QualType T, bool TopLevelOfInitList=false)
Add a new step that applies an implicit conversion sequence.
void AddZeroInitializationStep(QualType T)
Add a zero-initialization step.
void AddProduceObjCObjectStep(QualType T)
Add a step to "produce" an Objective-C object (by retaining it).
enum SequenceKind getKind() const
Determine the kind of initialization sequence computed.
SequenceKind
Describes the kind of initialization sequence computed.
@ NormalSequence
A normal sequence.
@ FailedSequence
A failed initialization sequence.
@ DependentSequence
A dependent initialization, which could not be type-checked due to the presence of dependent types or...
void AddReferenceBindingStep(QualType T, bool BindingTemporary)
Add a new step binding a reference to an object.
FailureKind
Describes why initialization failed.
@ FK_UserConversionOverloadFailed
Overloading for a user-defined conversion failed.
@ FK_NarrowStringIntoWideCharArray
Initializing a wide char array with narrow string literal.
@ FK_ArrayTypeMismatch
Array type mismatch.
@ FK_ParenthesizedListInitForReference
Reference initialized from a parenthesized initializer list.
@ FK_NonConstLValueReferenceBindingToVectorElement
Non-const lvalue reference binding to a vector element.
@ FK_ReferenceInitDropsQualifiers
Reference binding drops qualifiers.
@ FK_InitListBadDestinationType
Initialization of some unused destination type with an initializer list.
@ FK_ConversionFromPropertyFailed
Implicit conversion failed.
@ FK_NonConstLValueReferenceBindingToUnrelated
Non-const lvalue reference binding to an lvalue of unrelated type.
@ FK_ListConstructorOverloadFailed
Overloading for list-initialization by constructor failed.
@ FK_ReferenceInitFailed
Reference binding failed.
@ FK_ArrayNeedsInitList
Array must be initialized with an initializer list.
@ FK_PlainStringIntoUTF8Char
Initializing char8_t array with plain string literal.
@ FK_NonConstantArrayInit
Non-constant array initializer.
@ FK_NonConstLValueReferenceBindingToTemporary
Non-const lvalue reference binding to a temporary.
@ FK_ConversionFailed
Implicit conversion failed.
@ FK_ArrayNeedsInitListOrStringLiteral
Array must be initialized with an initializer list or a string literal.
@ FK_ParenthesizedListInitForScalar
Scalar initialized from a parenthesized initializer list.
@ FK_HLSLInitListFlatteningFailed
HLSL intialization list flattening failed.
@ FK_PlaceholderType
Initializer has a placeholder type which cannot be resolved by initialization.
@ FK_IncompatWideStringIntoWideChar
Initializing wide char array with incompatible wide string literal.
@ FK_NonConstLValueReferenceBindingToMatrixElement
Non-const lvalue reference binding to a matrix element.
@ FK_TooManyInitsForReference
Too many initializers provided for a reference.
@ FK_NonConstLValueReferenceBindingToBitfield
Non-const lvalue reference binding to a bit-field.
@ FK_ReferenceAddrspaceMismatchTemporary
Reference with mismatching address space binding to temporary.
@ FK_ListInitializationFailed
List initialization failed at some point.
@ FK_TooManyInitsForScalar
Too many initializers for scalar.
@ FK_AddressOfOverloadFailed
Cannot resolve the address of an overloaded function.
@ FK_VariableLengthArrayHasInitializer
Variable-length array must not have an initializer.
@ FK_ArrayNeedsInitListOrWideStringLiteral
Array must be initialized with an initializer list or a wide string literal.
@ FK_RValueReferenceBindingToLValue
Rvalue reference binding to an lvalue.
@ FK_Incomplete
Initialization of an incomplete type.
@ FK_WideStringIntoCharArray
Initializing char array with wide string literal.
@ FK_ExplicitConstructor
List-copy-initialization chose an explicit constructor.
@ FK_ReferenceInitOverloadFailed
Overloading due to reference initialization failed.
@ FK_ConstructorOverloadFailed
Overloading for initialization by constructor failed.
@ FK_ReferenceBindingToInitList
Reference initialization from an initializer list.
@ FK_DefaultInitOfConst
Default-initialization of a 'const' object.
@ FK_ParenthesizedListInitFailed
Parenthesized list initialization failed at some point.
@ FK_AddressOfUnaddressableFunction
Trying to take the address of a function that doesn't support having its address taken.
@ FK_UTF8StringIntoPlainChar
Initializing char array with UTF-8 string literal.
bool isDirectReferenceBinding() const
Determine whether this initialization is a direct reference binding (C++ [dcl.init....
void AddArrayInitLoopStep(QualType T, QualType EltTy)
Add an array initialization loop step.
void AddAddressOverloadResolutionStep(FunctionDecl *Function, DeclAccessPair Found, bool HadMultipleCandidates)
Add a new step in the initialization that resolves the address of an overloaded function to a specifi...
void AddArrayInitStep(QualType T, bool IsGNUExtension)
Add an array initialization step.
bool isConstructorInitialization() const
Determine whether this initialization is direct call to a constructor.
SmallVectorImpl< Step >::const_iterator step_iterator
OverloadCandidateSet & getFailedCandidateSet()
Retrieve a reference to the candidate set when overload resolution fails.
Describes an entity that is being initialized.
static InitializedEntity InitializeBase(ASTContext &Context, const CXXBaseSpecifier *Base, bool IsInheritedVirtualBase, const InitializedEntity *Parent=nullptr)
Create the initialization entity for a base class subobject.
static InitializedEntity InitializeResult(SourceLocation ReturnLoc, QualType Type)
Create the initialization entity for the result of a function.
VD Variable
When Kind == EK_Variable, EK_Member, EK_Binding, or EK_TemplateParameter, the variable,...
static InitializedEntity InitializeRelatedResult(ObjCMethodDecl *MD, QualType Type)
Create the initialization entity for a related result.
static InitializedEntity InitializeMember(FieldDecl *Member, const InitializedEntity *Parent=nullptr)
Create the initialization entity for a member subobject.
EntityKind getKind() const
Determine the kind of initialization.
DeclarationName getName() const
Retrieve the name of the entity being initialized.
static InitializedEntity InitializeException(SourceLocation ThrowLoc, QualType Type)
Create the initialization entity for an exception object.
unsigned allocateManglingNumber() const
QualType getType() const
Retrieve type being initialized.
static InitializedEntity InitializeTemporary(ASTContext &Context, TypeSourceInfo *TypeInfo)
Create the initialization entity for a temporary.
ValueDecl * getDecl() const
Retrieve the variable, parameter, or field being initialized.
static InitializedEntity InitializeParameter(ASTContext &Context, QualType Type, bool Consumed)
Create the initialization entity for a parameter that is only known by its type.
static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc, QualType Type)
bool isImplicitMemberInitializer() const
Is this the implicit initialization of a member of a class from a defaulted constructor?
const InitializedEntity * getParent() const
Retrieve the parent of the entity being initialized, when the initialization itself is occurring with...
static InitializedEntity InitializeTemporary(QualType Type)
Create the initialization entity for a temporary.
bool isParameterConsumed() const
Determine whether this initialization consumes the parameter.
static InitializedEntity InitializeBinding(VarDecl *Binding)
Create the initialization entity for a structured binding.
static InitializedEntity InitializeParameter(ASTContext &Context, ParmVarDecl *Parm, QualType Type)
Create the initialization entity for a parameter, but use another type.
TypeSourceInfo * TypeInfo
When Kind == EK_Temporary or EK_CompoundLiteralInit, the type source information for the temporary.
static InitializedEntity InitializeMemberFromDefaultMemberInitializer(FieldDecl *Member)
Create the initialization entity for a default member initializer.
static InitializedEntity InitializeElement(ASTContext &Context, unsigned Index, const InitializedEntity &Parent)
Create the initialization entity for an array element.
unsigned getElementIndex() const
If this is an array, vector, or complex number element, get the element's index.
bool isInheritedVirtualBase() const
Return whether the base is an inherited virtual base.
static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc, QualType Type)
void setElementIndex(unsigned Index)
If this is already the initializer for an array or vector element, sets the element index.
SourceLocation getCaptureLoc() const
Determine the location of the capture when initializing field from a captured variable in a lambda.
static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc, QualType Type)
bool isParamOrTemplateParamKind() const
llvm::PointerIntPair< const CXXBaseSpecifier *, 1 > Base
When Kind == EK_Base, the base specifier that provides the base class.
static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo, QualType Type)
Create the initialization entity for a temporary.
static InitializedEntity InitializeVariable(VarDecl *Var)
Create the initialization entity for a variable.
static InitializedEntity InitializeMemberImplicit(IndirectFieldDecl *Member)
Create the initialization entity for a member subobject with implicit field initializer.
bool allowsNRVO() const
Determine whether this initialization allows the named return value optimization, which also applies ...
static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type, NewArrayKind IsVariableLengthArrayNew)
Create the initialization entity for an object allocated via new.
void dump() const
Dump a representation of the initialized entity to standard error, for debugging purposes.
static InitializedEntity InitializeParameter(ASTContext &Context, ParmVarDecl *Parm)
Create the initialization entity for a parameter.
static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc)
Create the initialization entity for a lambda capture.
static InitializedEntity InitializeTemplateParameter(QualType T, NamedDecl *Param)
Create the initialization entity for a template parameter.
EntityKind
Specifies the kind of entity being initialized.
@ EK_Variable
The entity being initialized is a variable.
@ EK_Temporary
The entity being initialized is a temporary object.
@ EK_Binding
The entity being initialized is a structured binding of a decomposition declaration.
@ EK_BlockElement
The entity being initialized is a field of block descriptor for the copied-in c++ object.
@ EK_MatrixElement
The entity being initialized is an element of a matrix.
@ EK_Parameter_CF_Audited
The entity being initialized is a function parameter; function is member of group of audited CF APIs.
@ EK_LambdaToBlockConversionBlockElement
The entity being initialized is a field of block descriptor for the copied-in lambda object that's us...
@ EK_Member
The entity being initialized is a non-static data member subobject.
@ EK_Base
The entity being initialized is a base member subobject.
@ EK_Result
The entity being initialized is the result of a function call.
@ EK_TemplateParameter
The entity being initialized is a non-type template parameter.
@ EK_StmtExprResult
The entity being initialized is the result of a statement expression.
@ EK_ParenAggInitMember
The entity being initialized is a non-static data member subobject of an object initialized via paren...
@ EK_VectorElement
The entity being initialized is an element of a vector.
@ EK_New
The entity being initialized is an object (or array of objects) allocated via new.
@ EK_CompoundLiteralInit
The entity being initialized is the initializer for a compound literal.
@ EK_Parameter
The entity being initialized is a function parameter.
@ EK_Delegating
The initialization is being done by a delegating constructor.
@ EK_ComplexElement
The entity being initialized is the real or imaginary part of a complex number.
@ EK_ArrayElement
The entity being initialized is an element of an array.
@ EK_LambdaCapture
The entity being initialized is the field that captures a variable in a lambda.
@ EK_Exception
The entity being initialized is an exception object that is being thrown.
@ EK_RelatedResult
The entity being implicitly initialized back to the formal result type.
StringRef getCapturedVarName() const
For a lambda capture, return the capture's name.
static InitializedEntity InitializeMemberFromParenAggInit(FieldDecl *Member)
Create the initialization entity for a member subobject initialized via parenthesized aggregate init.
SourceLocation getThrowLoc() const
Determine the location of the 'throw' keyword when initializing an exception object.
static InitializedEntity InitializeMemberImplicit(FieldDecl *Member)
Create the initialization entity for a member subobject with implicit field initializer.
static InitializedEntity InitializeDelegation(QualType Type)
Create the initialization entity for a delegated constructor.
unsigned Index
When Kind == EK_ArrayElement, EK_VectorElement, EK_MatrixElement, or EK_ComplexElement,...
static InitializedEntity InitializeCompoundLiteralInit(TypeSourceInfo *TSI)
Create the entity for a compound literal initializer.
bool isVariableLengthArrayNew() const
Determine whether this is an array new with an unknown bound.
ObjCMethodDecl * MethodDecl
When Kind == EK_RelatedResult, the ObjectiveC method where result type was implicitly changed to acco...
bool isDefaultMemberInitializer() const
Is this the default member initializer of a member (specified inside the class definition)?
llvm::PointerIntPair< ParmVarDecl *, 1 > Parameter
When Kind == EK_Parameter, the ParmVarDecl, with the integer indicating whether the parameter is "con...
const CXXBaseSpecifier * getBaseSpecifier() const
Retrieve the base specifier.
SourceLocation getReturnLoc() const
Determine the location of the 'return' keyword when initializing the result of a function call.
static InitializedEntity InitializeMember(IndirectFieldDecl *Member, const InitializedEntity *Parent=nullptr)
Create the initialization entity for a member subobject.
TypeSourceInfo * getTypeSourceInfo() const
Retrieve complete type-source information for the object being constructed, if known.
ObjCMethodDecl * getMethodDecl() const
Retrieve the ObjectiveC method being initialized.
This represents a decl that may have a name.
Definition Decl.h:274
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
OverloadCandidateSet - A set of overload candidates, used in C++ overload resolution (C++ 13....
Definition Overload.h:1160
Represents a parameter to a function.
Definition Decl.h:1805
A (possibly-)qualified type.
Definition TypeBase.h:937
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
A container of type source information.
Definition TypeBase.h:8402
QualType getType() const
Return the type wrapped by this type source info.
Definition TypeBase.h:8413
The base class of the type hierarchy.
Definition TypeBase.h:1866
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
OverloadingResult
OverloadingResult - Capture the result of performing overload resolution.
Definition Overload.h:50
MutableArrayRef< Expr * > MultiExprArg
Definition Ownership.h:259
@ Result
The result type of a method or function.
Definition TypeBase.h:905
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition Specifiers.h:132
ActionResult< Expr * > ExprResult
Definition Ownership.h:249