Initialization.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Initialization.h - Semantic Analysis for Initializers ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides supporting data types for initialization of objects.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_SEMA_INITIALIZATION_H
  18. #define LLVM_CLANG_SEMA_INITIALIZATION_H
  19. #include "clang/AST/ASTContext.h"
  20. #include "clang/AST/Attr.h"
  21. #include "clang/AST/Decl.h"
  22. #include "clang/AST/DeclAccessPair.h"
  23. #include "clang/AST/DeclarationName.h"
  24. #include "clang/AST/Expr.h"
  25. #include "clang/AST/Type.h"
  26. #include "clang/Basic/IdentifierTable.h"
  27. #include "clang/Basic/LLVM.h"
  28. #include "clang/Basic/LangOptions.h"
  29. #include "clang/Basic/SourceLocation.h"
  30. #include "clang/Basic/Specifiers.h"
  31. #include "clang/Sema/Overload.h"
  32. #include "clang/Sema/Ownership.h"
  33. #include "llvm/ADT/ArrayRef.h"
  34. #include "llvm/ADT/SmallVector.h"
  35. #include "llvm/ADT/StringRef.h"
  36. #include "llvm/ADT/iterator_range.h"
  37. #include "llvm/Support/Casting.h"
  38. #include <cassert>
  39. #include <cstdint>
  40. #include <string>
  41. namespace clang {
  42. class CXXBaseSpecifier;
  43. class CXXConstructorDecl;
  44. class ObjCMethodDecl;
  45. class Sema;
  46. /// Describes an entity that is being initialized.
  47. class alignas(8) InitializedEntity {
  48. public:
  49. /// Specifies the kind of entity being initialized.
  50. enum EntityKind {
  51. /// The entity being initialized is a variable.
  52. EK_Variable,
  53. /// The entity being initialized is a function parameter.
  54. EK_Parameter,
  55. /// The entity being initialized is a non-type template parameter.
  56. EK_TemplateParameter,
  57. /// The entity being initialized is the result of a function call.
  58. EK_Result,
  59. /// The entity being initialized is the result of a statement expression.
  60. EK_StmtExprResult,
  61. /// The entity being initialized is an exception object that
  62. /// is being thrown.
  63. EK_Exception,
  64. /// The entity being initialized is a non-static data member
  65. /// subobject.
  66. EK_Member,
  67. /// The entity being initialized is an element of an array.
  68. EK_ArrayElement,
  69. /// The entity being initialized is an object (or array of
  70. /// objects) allocated via new.
  71. EK_New,
  72. /// The entity being initialized is a temporary object.
  73. EK_Temporary,
  74. /// The entity being initialized is a base member subobject.
  75. EK_Base,
  76. /// The initialization is being done by a delegating constructor.
  77. EK_Delegating,
  78. /// The entity being initialized is an element of a vector.
  79. /// or vector.
  80. EK_VectorElement,
  81. /// The entity being initialized is a field of block descriptor for
  82. /// the copied-in c++ object.
  83. EK_BlockElement,
  84. /// The entity being initialized is a field of block descriptor for the
  85. /// copied-in lambda object that's used in the lambda to block conversion.
  86. EK_LambdaToBlockConversionBlockElement,
  87. /// The entity being initialized is the real or imaginary part of a
  88. /// complex number.
  89. EK_ComplexElement,
  90. /// The entity being initialized is the field that captures a
  91. /// variable in a lambda.
  92. EK_LambdaCapture,
  93. /// The entity being initialized is the initializer for a compound
  94. /// literal.
  95. EK_CompoundLiteralInit,
  96. /// The entity being implicitly initialized back to the formal
  97. /// result type.
  98. EK_RelatedResult,
  99. /// The entity being initialized is a function parameter; function
  100. /// is member of group of audited CF APIs.
  101. EK_Parameter_CF_Audited,
  102. /// The entity being initialized is a structured binding of a
  103. /// decomposition declaration.
  104. EK_Binding,
  105. // Note: err_init_conversion_failed in DiagnosticSemaKinds.td uses this
  106. // enum as an index for its first %select. When modifying this list,
  107. // that diagnostic text needs to be updated as well.
  108. };
  109. private:
  110. /// The kind of entity being initialized.
  111. EntityKind Kind;
  112. /// If non-NULL, the parent entity in which this
  113. /// initialization occurs.
  114. const InitializedEntity *Parent = nullptr;
  115. /// The type of the object or reference being initialized.
  116. QualType Type;
  117. /// The mangling number for the next reference temporary to be created.
  118. mutable unsigned ManglingNumber = 0;
  119. struct LN {
  120. /// When Kind == EK_Result, EK_Exception, EK_New, the
  121. /// location of the 'return', 'throw', or 'new' keyword,
  122. /// respectively. When Kind == EK_Temporary, the location where
  123. /// the temporary is being created.
  124. SourceLocation Location;
  125. /// Whether the entity being initialized may end up using the
  126. /// named return value optimization (NRVO).
  127. bool NRVO;
  128. };
  129. struct VD {
  130. /// The VarDecl, FieldDecl, or BindingDecl being initialized.
  131. ValueDecl *VariableOrMember;
  132. /// When Kind == EK_Member, whether this is an implicit member
  133. /// initialization in a copy or move constructor. These can perform array
  134. /// copies.
  135. bool IsImplicitFieldInit;
  136. /// When Kind == EK_Member, whether this is the initial initialization
  137. /// check for a default member initializer.
  138. bool IsDefaultMemberInit;
  139. };
  140. struct C {
  141. /// The name of the variable being captured by an EK_LambdaCapture.
  142. IdentifierInfo *VarID;
  143. /// The source location at which the capture occurs.
  144. SourceLocation Location;
  145. };
  146. union {
  147. /// When Kind == EK_Variable, EK_Member, EK_Binding, or
  148. /// EK_TemplateParameter, the variable, binding, or template parameter.
  149. VD Variable;
  150. /// When Kind == EK_RelatedResult, the ObjectiveC method where
  151. /// result type was implicitly changed to accommodate ARC semantics.
  152. ObjCMethodDecl *MethodDecl;
  153. /// When Kind == EK_Parameter, the ParmVarDecl, with the
  154. /// integer indicating whether the parameter is "consumed".
  155. llvm::PointerIntPair<ParmVarDecl *, 1> Parameter;
  156. /// When Kind == EK_Temporary or EK_CompoundLiteralInit, the type
  157. /// source information for the temporary.
  158. TypeSourceInfo *TypeInfo;
  159. struct LN LocAndNRVO;
  160. /// When Kind == EK_Base, the base specifier that provides the
  161. /// base class. The integer specifies whether the base is an inherited
  162. /// virtual base.
  163. llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base;
  164. /// When Kind == EK_ArrayElement, EK_VectorElement, or
  165. /// EK_ComplexElement, the index of the array or vector element being
  166. /// initialized.
  167. unsigned Index;
  168. struct C Capture;
  169. };
  170. InitializedEntity() {};
  171. /// Create the initialization entity for a variable.
  172. InitializedEntity(VarDecl *Var, EntityKind EK = EK_Variable)
  173. : Kind(EK), Type(Var->getType()), Variable{Var, false, false} {}
  174. /// Create the initialization entity for the result of a
  175. /// function, throwing an object, performing an explicit cast, or
  176. /// initializing a parameter for which there is no declaration.
  177. InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type,
  178. bool NRVO = false)
  179. : Kind(Kind), Type(Type) {
  180. new (&LocAndNRVO) LN;
  181. LocAndNRVO.Location = Loc;
  182. LocAndNRVO.NRVO = NRVO;
  183. }
  184. /// Create the initialization entity for a member subobject.
  185. InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent,
  186. bool Implicit, bool DefaultMemberInit)
  187. : Kind(EK_Member), Parent(Parent), Type(Member->getType()),
  188. Variable{Member, Implicit, DefaultMemberInit} {}
  189. /// Create the initialization entity for an array element.
  190. InitializedEntity(ASTContext &Context, unsigned Index,
  191. const InitializedEntity &Parent);
  192. /// Create the initialization entity for a lambda capture.
  193. InitializedEntity(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc)
  194. : Kind(EK_LambdaCapture), Type(FieldType) {
  195. new (&Capture) C;
  196. Capture.VarID = VarID;
  197. Capture.Location = Loc;
  198. }
  199. public:
  200. /// Create the initialization entity for a variable.
  201. static InitializedEntity InitializeVariable(VarDecl *Var) {
  202. return InitializedEntity(Var);
  203. }
  204. /// Create the initialization entity for a parameter.
  205. static InitializedEntity InitializeParameter(ASTContext &Context,
  206. ParmVarDecl *Parm) {
  207. return InitializeParameter(Context, Parm, Parm->getType());
  208. }
  209. /// Create the initialization entity for a parameter, but use
  210. /// another type.
  211. static InitializedEntity
  212. InitializeParameter(ASTContext &Context, ParmVarDecl *Parm, QualType Type) {
  213. bool Consumed = (Context.getLangOpts().ObjCAutoRefCount &&
  214. Parm->hasAttr<NSConsumedAttr>());
  215. InitializedEntity Entity;
  216. Entity.Kind = EK_Parameter;
  217. Entity.Type =
  218. Context.getVariableArrayDecayedType(Type.getUnqualifiedType());
  219. Entity.Parent = nullptr;
  220. Entity.Parameter = {Parm, Consumed};
  221. return Entity;
  222. }
  223. /// Create the initialization entity for a parameter that is
  224. /// only known by its type.
  225. static InitializedEntity InitializeParameter(ASTContext &Context,
  226. QualType Type,
  227. bool Consumed) {
  228. InitializedEntity Entity;
  229. Entity.Kind = EK_Parameter;
  230. Entity.Type = Context.getVariableArrayDecayedType(Type);
  231. Entity.Parent = nullptr;
  232. Entity.Parameter = {nullptr, Consumed};
  233. return Entity;
  234. }
  235. /// Create the initialization entity for a template parameter.
  236. static InitializedEntity
  237. InitializeTemplateParameter(QualType T, NonTypeTemplateParmDecl *Param) {
  238. InitializedEntity Entity;
  239. Entity.Kind = EK_TemplateParameter;
  240. Entity.Type = T;
  241. Entity.Parent = nullptr;
  242. Entity.Variable = {Param, false, false};
  243. return Entity;
  244. }
  245. /// Create the initialization entity for the result of a function.
  246. static InitializedEntity InitializeResult(SourceLocation ReturnLoc,
  247. QualType Type) {
  248. return InitializedEntity(EK_Result, ReturnLoc, Type);
  249. }
  250. static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc,
  251. QualType Type) {
  252. return InitializedEntity(EK_StmtExprResult, ReturnLoc, Type);
  253. }
  254. static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc,
  255. QualType Type) {
  256. return InitializedEntity(EK_BlockElement, BlockVarLoc, Type);
  257. }
  258. static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc,
  259. QualType Type) {
  260. return InitializedEntity(EK_LambdaToBlockConversionBlockElement,
  261. BlockVarLoc, Type);
  262. }
  263. /// Create the initialization entity for an exception object.
  264. static InitializedEntity InitializeException(SourceLocation ThrowLoc,
  265. QualType Type) {
  266. return InitializedEntity(EK_Exception, ThrowLoc, Type);
  267. }
  268. /// Create the initialization entity for an object allocated via new.
  269. static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) {
  270. return InitializedEntity(EK_New, NewLoc, Type);
  271. }
  272. /// Create the initialization entity for a temporary.
  273. static InitializedEntity InitializeTemporary(QualType Type) {
  274. return InitializeTemporary(nullptr, Type);
  275. }
  276. /// Create the initialization entity for a temporary.
  277. static InitializedEntity InitializeTemporary(ASTContext &Context,
  278. TypeSourceInfo *TypeInfo) {
  279. QualType Type = TypeInfo->getType();
  280. if (Context.getLangOpts().OpenCLCPlusPlus) {
  281. assert(!Type.hasAddressSpace() && "Temporary already has address space!");
  282. Type = Context.getAddrSpaceQualType(Type, LangAS::opencl_private);
  283. }
  284. return InitializeTemporary(TypeInfo, Type);
  285. }
  286. /// Create the initialization entity for a temporary.
  287. static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo,
  288. QualType Type) {
  289. InitializedEntity Result(EK_Temporary, SourceLocation(), Type);
  290. Result.TypeInfo = TypeInfo;
  291. return Result;
  292. }
  293. /// Create the initialization entity for a related result.
  294. static InitializedEntity InitializeRelatedResult(ObjCMethodDecl *MD,
  295. QualType Type) {
  296. InitializedEntity Result(EK_RelatedResult, SourceLocation(), Type);
  297. Result.MethodDecl = MD;
  298. return Result;
  299. }
  300. /// Create the initialization entity for a base class subobject.
  301. static InitializedEntity
  302. InitializeBase(ASTContext &Context, const CXXBaseSpecifier *Base,
  303. bool IsInheritedVirtualBase,
  304. const InitializedEntity *Parent = nullptr);
  305. /// Create the initialization entity for a delegated constructor.
  306. static InitializedEntity InitializeDelegation(QualType Type) {
  307. return InitializedEntity(EK_Delegating, SourceLocation(), Type);
  308. }
  309. /// Create the initialization entity for a member subobject.
  310. static InitializedEntity
  311. InitializeMember(FieldDecl *Member,
  312. const InitializedEntity *Parent = nullptr,
  313. bool Implicit = false) {
  314. return InitializedEntity(Member, Parent, Implicit, false);
  315. }
  316. /// Create the initialization entity for a member subobject.
  317. static InitializedEntity
  318. InitializeMember(IndirectFieldDecl *Member,
  319. const InitializedEntity *Parent = nullptr,
  320. bool Implicit = false) {
  321. return InitializedEntity(Member->getAnonField(), Parent, Implicit, false);
  322. }
  323. /// Create the initialization entity for a default member initializer.
  324. static InitializedEntity
  325. InitializeMemberFromDefaultMemberInitializer(FieldDecl *Member) {
  326. return InitializedEntity(Member, nullptr, false, true);
  327. }
  328. /// Create the initialization entity for an array element.
  329. static InitializedEntity InitializeElement(ASTContext &Context,
  330. unsigned Index,
  331. const InitializedEntity &Parent) {
  332. return InitializedEntity(Context, Index, Parent);
  333. }
  334. /// Create the initialization entity for a structured binding.
  335. static InitializedEntity InitializeBinding(VarDecl *Binding) {
  336. return InitializedEntity(Binding, EK_Binding);
  337. }
  338. /// Create the initialization entity for a lambda capture.
  339. ///
  340. /// \p VarID The name of the entity being captured, or nullptr for 'this'.
  341. static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID,
  342. QualType FieldType,
  343. SourceLocation Loc) {
  344. return InitializedEntity(VarID, FieldType, Loc);
  345. }
  346. /// Create the entity for a compound literal initializer.
  347. static InitializedEntity InitializeCompoundLiteralInit(TypeSourceInfo *TSI) {
  348. InitializedEntity Result(EK_CompoundLiteralInit, SourceLocation(),
  349. TSI->getType());
  350. Result.TypeInfo = TSI;
  351. return Result;
  352. }
  353. /// Determine the kind of initialization.
  354. EntityKind getKind() const { return Kind; }
  355. /// Retrieve the parent of the entity being initialized, when
  356. /// the initialization itself is occurring within the context of a
  357. /// larger initialization.
  358. const InitializedEntity *getParent() const { return Parent; }
  359. /// Retrieve type being initialized.
  360. QualType getType() const { return Type; }
  361. /// Retrieve complete type-source information for the object being
  362. /// constructed, if known.
  363. TypeSourceInfo *getTypeSourceInfo() const {
  364. if (Kind == EK_Temporary || Kind == EK_CompoundLiteralInit)
  365. return TypeInfo;
  366. return nullptr;
  367. }
  368. /// Retrieve the name of the entity being initialized.
  369. DeclarationName getName() const;
  370. /// Retrieve the variable, parameter, or field being
  371. /// initialized.
  372. ValueDecl *getDecl() const;
  373. /// Retrieve the ObjectiveC method being initialized.
  374. ObjCMethodDecl *getMethodDecl() const { return MethodDecl; }
  375. /// Determine whether this initialization allows the named return
  376. /// value optimization, which also applies to thrown objects.
  377. bool allowsNRVO() const;
  378. bool isParameterKind() const {
  379. return (getKind() == EK_Parameter ||
  380. getKind() == EK_Parameter_CF_Audited);
  381. }
  382. bool isParamOrTemplateParamKind() const {
  383. return isParameterKind() || getKind() == EK_TemplateParameter;
  384. }
  385. /// Determine whether this initialization consumes the
  386. /// parameter.
  387. bool isParameterConsumed() const {
  388. assert(isParameterKind() && "Not a parameter");
  389. return Parameter.getInt();
  390. }
  391. /// Retrieve the base specifier.
  392. const CXXBaseSpecifier *getBaseSpecifier() const {
  393. assert(getKind() == EK_Base && "Not a base specifier");
  394. return Base.getPointer();
  395. }
  396. /// Return whether the base is an inherited virtual base.
  397. bool isInheritedVirtualBase() const {
  398. assert(getKind() == EK_Base && "Not a base specifier");
  399. return Base.getInt();
  400. }
  401. /// Determine whether this is an array new with an unknown bound.
  402. bool isVariableLengthArrayNew() const {
  403. return getKind() == EK_New && isa_and_nonnull<IncompleteArrayType>(
  404. getType()->getAsArrayTypeUnsafe());
  405. }
  406. /// Is this the implicit initialization of a member of a class from
  407. /// a defaulted constructor?
  408. bool isImplicitMemberInitializer() const {
  409. return getKind() == EK_Member && Variable.IsImplicitFieldInit;
  410. }
  411. /// Is this the default member initializer of a member (specified inside
  412. /// the class definition)?
  413. bool isDefaultMemberInitializer() const {
  414. return getKind() == EK_Member && Variable.IsDefaultMemberInit;
  415. }
  416. /// Determine the location of the 'return' keyword when initializing
  417. /// the result of a function call.
  418. SourceLocation getReturnLoc() const {
  419. assert(getKind() == EK_Result && "No 'return' location!");
  420. return LocAndNRVO.Location;
  421. }
  422. /// Determine the location of the 'throw' keyword when initializing
  423. /// an exception object.
  424. SourceLocation getThrowLoc() const {
  425. assert(getKind() == EK_Exception && "No 'throw' location!");
  426. return LocAndNRVO.Location;
  427. }
  428. /// If this is an array, vector, or complex number element, get the
  429. /// element's index.
  430. unsigned getElementIndex() const {
  431. assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
  432. getKind() == EK_ComplexElement);
  433. return Index;
  434. }
  435. /// If this is already the initializer for an array or vector
  436. /// element, sets the element index.
  437. void setElementIndex(unsigned Index) {
  438. assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
  439. getKind() == EK_ComplexElement);
  440. this->Index = Index;
  441. }
  442. /// For a lambda capture, return the capture's name.
  443. StringRef getCapturedVarName() const {
  444. assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
  445. return Capture.VarID ? Capture.VarID->getName() : "this";
  446. }
  447. /// Determine the location of the capture when initializing
  448. /// field from a captured variable in a lambda.
  449. SourceLocation getCaptureLoc() const {
  450. assert(getKind() == EK_LambdaCapture && "Not a lambda capture!");
  451. return Capture.Location;
  452. }
  453. void setParameterCFAudited() {
  454. Kind = EK_Parameter_CF_Audited;
  455. }
  456. unsigned allocateManglingNumber() const { return ++ManglingNumber; }
  457. /// Dump a representation of the initialized entity to standard error,
  458. /// for debugging purposes.
  459. void dump() const;
  460. private:
  461. unsigned dumpImpl(raw_ostream &OS) const;
  462. };
  463. /// Describes the kind of initialization being performed, along with
  464. /// location information for tokens related to the initialization (equal sign,
  465. /// parentheses).
  466. class InitializationKind {
  467. public:
  468. /// The kind of initialization being performed.
  469. enum InitKind {
  470. /// Direct initialization
  471. IK_Direct,
  472. /// Direct list-initialization
  473. IK_DirectList,
  474. /// Copy initialization
  475. IK_Copy,
  476. /// Default initialization
  477. IK_Default,
  478. /// Value initialization
  479. IK_Value
  480. };
  481. private:
  482. /// The context of the initialization.
  483. enum InitContext {
  484. /// Normal context
  485. IC_Normal,
  486. /// Normal context, but allows explicit conversion functionss
  487. IC_ExplicitConvs,
  488. /// Implicit context (value initialization)
  489. IC_Implicit,
  490. /// Static cast context
  491. IC_StaticCast,
  492. /// C-style cast context
  493. IC_CStyleCast,
  494. /// Functional cast context
  495. IC_FunctionalCast
  496. };
  497. /// The kind of initialization being performed.
  498. InitKind Kind : 8;
  499. /// The context of the initialization.
  500. InitContext Context : 8;
  501. /// The source locations involved in the initialization.
  502. SourceLocation Locations[3];
  503. InitializationKind(InitKind Kind, InitContext Context, SourceLocation Loc1,
  504. SourceLocation Loc2, SourceLocation Loc3)
  505. : Kind(Kind), Context(Context) {
  506. Locations[0] = Loc1;
  507. Locations[1] = Loc2;
  508. Locations[2] = Loc3;
  509. }
  510. public:
  511. /// Create a direct initialization.
  512. static InitializationKind CreateDirect(SourceLocation InitLoc,
  513. SourceLocation LParenLoc,
  514. SourceLocation RParenLoc) {
  515. return InitializationKind(IK_Direct, IC_Normal,
  516. InitLoc, LParenLoc, RParenLoc);
  517. }
  518. static InitializationKind CreateDirectList(SourceLocation InitLoc) {
  519. return InitializationKind(IK_DirectList, IC_Normal, InitLoc, InitLoc,
  520. InitLoc);
  521. }
  522. static InitializationKind CreateDirectList(SourceLocation InitLoc,
  523. SourceLocation LBraceLoc,
  524. SourceLocation RBraceLoc) {
  525. return InitializationKind(IK_DirectList, IC_Normal, InitLoc, LBraceLoc,
  526. RBraceLoc);
  527. }
  528. /// Create a direct initialization due to a cast that isn't a C-style
  529. /// or functional cast.
  530. static InitializationKind CreateCast(SourceRange TypeRange) {
  531. return InitializationKind(IK_Direct, IC_StaticCast, TypeRange.getBegin(),
  532. TypeRange.getBegin(), TypeRange.getEnd());
  533. }
  534. /// Create a direct initialization for a C-style cast.
  535. static InitializationKind CreateCStyleCast(SourceLocation StartLoc,
  536. SourceRange TypeRange,
  537. bool InitList) {
  538. // C++ cast syntax doesn't permit init lists, but C compound literals are
  539. // exactly that.
  540. return InitializationKind(InitList ? IK_DirectList : IK_Direct,
  541. IC_CStyleCast, StartLoc, TypeRange.getBegin(),
  542. TypeRange.getEnd());
  543. }
  544. /// Create a direct initialization for a functional cast.
  545. static InitializationKind CreateFunctionalCast(SourceRange TypeRange,
  546. bool InitList) {
  547. return InitializationKind(InitList ? IK_DirectList : IK_Direct,
  548. IC_FunctionalCast, TypeRange.getBegin(),
  549. TypeRange.getBegin(), TypeRange.getEnd());
  550. }
  551. /// Create a copy initialization.
  552. static InitializationKind CreateCopy(SourceLocation InitLoc,
  553. SourceLocation EqualLoc,
  554. bool AllowExplicitConvs = false) {
  555. return InitializationKind(IK_Copy,
  556. AllowExplicitConvs? IC_ExplicitConvs : IC_Normal,
  557. InitLoc, EqualLoc, EqualLoc);
  558. }
  559. /// Create a default initialization.
  560. static InitializationKind CreateDefault(SourceLocation InitLoc) {
  561. return InitializationKind(IK_Default, IC_Normal, InitLoc, InitLoc, InitLoc);
  562. }
  563. /// Create a value initialization.
  564. static InitializationKind CreateValue(SourceLocation InitLoc,
  565. SourceLocation LParenLoc,
  566. SourceLocation RParenLoc,
  567. bool isImplicit = false) {
  568. return InitializationKind(IK_Value, isImplicit ? IC_Implicit : IC_Normal,
  569. InitLoc, LParenLoc, RParenLoc);
  570. }
  571. /// Create an initialization from an initializer (which, for direct
  572. /// initialization from a parenthesized list, will be a ParenListExpr).
  573. static InitializationKind CreateForInit(SourceLocation Loc, bool DirectInit,
  574. Expr *Init) {
  575. if (!Init) return CreateDefault(Loc);
  576. if (!DirectInit)
  577. return CreateCopy(Loc, Init->getBeginLoc());
  578. if (isa<InitListExpr>(Init))
  579. return CreateDirectList(Loc, Init->getBeginLoc(), Init->getEndLoc());
  580. return CreateDirect(Loc, Init->getBeginLoc(), Init->getEndLoc());
  581. }
  582. /// Determine the initialization kind.
  583. InitKind getKind() const {
  584. return Kind;
  585. }
  586. /// Determine whether this initialization is an explicit cast.
  587. bool isExplicitCast() const {
  588. return Context >= IC_StaticCast;
  589. }
  590. /// Determine whether this initialization is a static cast.
  591. bool isStaticCast() const { return Context == IC_StaticCast; }
  592. /// Determine whether this initialization is a C-style cast.
  593. bool isCStyleOrFunctionalCast() const {
  594. return Context >= IC_CStyleCast;
  595. }
  596. /// Determine whether this is a C-style cast.
  597. bool isCStyleCast() const {
  598. return Context == IC_CStyleCast;
  599. }
  600. /// Determine whether this is a functional-style cast.
  601. bool isFunctionalCast() const {
  602. return Context == IC_FunctionalCast;
  603. }
  604. /// Determine whether this initialization is an implicit
  605. /// value-initialization, e.g., as occurs during aggregate
  606. /// initialization.
  607. bool isImplicitValueInit() const { return Context == IC_Implicit; }
  608. /// Retrieve the location at which initialization is occurring.
  609. SourceLocation getLocation() const { return Locations[0]; }
  610. /// Retrieve the source range that covers the initialization.
  611. SourceRange getRange() const {
  612. return SourceRange(Locations[0], Locations[2]);
  613. }
  614. /// Retrieve the location of the equal sign for copy initialization
  615. /// (if present).
  616. SourceLocation getEqualLoc() const {
  617. assert(Kind == IK_Copy && "Only copy initialization has an '='");
  618. return Locations[1];
  619. }
  620. bool isCopyInit() const { return Kind == IK_Copy; }
  621. /// Retrieve whether this initialization allows the use of explicit
  622. /// constructors.
  623. bool AllowExplicit() const { return !isCopyInit(); }
  624. /// Retrieve whether this initialization allows the use of explicit
  625. /// conversion functions when binding a reference. If the reference is the
  626. /// first parameter in a copy or move constructor, such conversions are
  627. /// permitted even though we are performing copy-initialization.
  628. bool allowExplicitConversionFunctionsInRefBinding() const {
  629. return !isCopyInit() || Context == IC_ExplicitConvs;
  630. }
  631. /// Determine whether this initialization has a source range containing the
  632. /// locations of open and closing parentheses or braces.
  633. bool hasParenOrBraceRange() const {
  634. return Kind == IK_Direct || Kind == IK_Value || Kind == IK_DirectList;
  635. }
  636. /// Retrieve the source range containing the locations of the open
  637. /// and closing parentheses or braces for value, direct, and direct list
  638. /// initializations.
  639. SourceRange getParenOrBraceRange() const {
  640. assert(hasParenOrBraceRange() && "Only direct, value, and direct-list "
  641. "initialization have parentheses or "
  642. "braces");
  643. return SourceRange(Locations[1], Locations[2]);
  644. }
  645. };
  646. /// Describes the sequence of initializations required to initialize
  647. /// a given object or reference with a set of arguments.
  648. class InitializationSequence {
  649. public:
  650. /// Describes the kind of initialization sequence computed.
  651. enum SequenceKind {
  652. /// A failed initialization sequence. The failure kind tells what
  653. /// happened.
  654. FailedSequence = 0,
  655. /// A dependent initialization, which could not be
  656. /// type-checked due to the presence of dependent types or
  657. /// dependently-typed expressions.
  658. DependentSequence,
  659. /// A normal sequence.
  660. NormalSequence
  661. };
  662. /// Describes the kind of a particular step in an initialization
  663. /// sequence.
  664. enum StepKind {
  665. /// Resolve the address of an overloaded function to a specific
  666. /// function declaration.
  667. SK_ResolveAddressOfOverloadedFunction,
  668. /// Perform a derived-to-base cast, producing an rvalue.
  669. SK_CastDerivedToBasePRValue,
  670. /// Perform a derived-to-base cast, producing an xvalue.
  671. SK_CastDerivedToBaseXValue,
  672. /// Perform a derived-to-base cast, producing an lvalue.
  673. SK_CastDerivedToBaseLValue,
  674. /// Reference binding to an lvalue.
  675. SK_BindReference,
  676. /// Reference binding to a temporary.
  677. SK_BindReferenceToTemporary,
  678. /// An optional copy of a temporary object to another
  679. /// temporary object, which is permitted (but not required) by
  680. /// C++98/03 but not C++0x.
  681. SK_ExtraneousCopyToTemporary,
  682. /// Direct-initialization from a reference-related object in the
  683. /// final stage of class copy-initialization.
  684. SK_FinalCopy,
  685. /// Perform a user-defined conversion, either via a conversion
  686. /// function or via a constructor.
  687. SK_UserConversion,
  688. /// Perform a qualification conversion, producing a prvalue.
  689. SK_QualificationConversionPRValue,
  690. /// Perform a qualification conversion, producing an xvalue.
  691. SK_QualificationConversionXValue,
  692. /// Perform a qualification conversion, producing an lvalue.
  693. SK_QualificationConversionLValue,
  694. /// Perform a function reference conversion, see [dcl.init.ref]p4.
  695. SK_FunctionReferenceConversion,
  696. /// Perform a conversion adding _Atomic to a type.
  697. SK_AtomicConversion,
  698. /// Perform an implicit conversion sequence.
  699. SK_ConversionSequence,
  700. /// Perform an implicit conversion sequence without narrowing.
  701. SK_ConversionSequenceNoNarrowing,
  702. /// Perform list-initialization without a constructor.
  703. SK_ListInitialization,
  704. /// Unwrap the single-element initializer list for a reference.
  705. SK_UnwrapInitList,
  706. /// Rewrap the single-element initializer list for a reference.
  707. SK_RewrapInitList,
  708. /// Perform initialization via a constructor.
  709. SK_ConstructorInitialization,
  710. /// Perform initialization via a constructor, taking arguments from
  711. /// a single InitListExpr.
  712. SK_ConstructorInitializationFromList,
  713. /// Zero-initialize the object
  714. SK_ZeroInitialization,
  715. /// C assignment
  716. SK_CAssignment,
  717. /// Initialization by string
  718. SK_StringInit,
  719. /// An initialization that "converts" an Objective-C object
  720. /// (not a point to an object) to another Objective-C object type.
  721. SK_ObjCObjectConversion,
  722. /// Array indexing for initialization by elementwise copy.
  723. SK_ArrayLoopIndex,
  724. /// Array initialization by elementwise copy.
  725. SK_ArrayLoopInit,
  726. /// Array initialization (from an array rvalue).
  727. SK_ArrayInit,
  728. /// Array initialization (from an array rvalue) as a GNU extension.
  729. SK_GNUArrayInit,
  730. /// Array initialization from a parenthesized initializer list.
  731. /// This is a GNU C++ extension.
  732. SK_ParenthesizedArrayInit,
  733. /// Pass an object by indirect copy-and-restore.
  734. SK_PassByIndirectCopyRestore,
  735. /// Pass an object by indirect restore.
  736. SK_PassByIndirectRestore,
  737. /// Produce an Objective-C object pointer.
  738. SK_ProduceObjCObject,
  739. /// Construct a std::initializer_list from an initializer list.
  740. SK_StdInitializerList,
  741. /// Perform initialization via a constructor taking a single
  742. /// std::initializer_list argument.
  743. SK_StdInitializerListConstructorCall,
  744. /// Initialize an OpenCL sampler from an integer.
  745. SK_OCLSamplerInit,
  746. /// Initialize an opaque OpenCL type (event_t, queue_t, etc.) with zero
  747. SK_OCLZeroOpaqueType
  748. };
  749. /// A single step in the initialization sequence.
  750. class Step {
  751. public:
  752. /// The kind of conversion or initialization step we are taking.
  753. StepKind Kind;
  754. // The type that results from this initialization.
  755. QualType Type;
  756. struct F {
  757. bool HadMultipleCandidates;
  758. FunctionDecl *Function;
  759. DeclAccessPair FoundDecl;
  760. };
  761. union {
  762. /// When Kind == SK_ResolvedOverloadedFunction or Kind ==
  763. /// SK_UserConversion, the function that the expression should be
  764. /// resolved to or the conversion function to call, respectively.
  765. /// When Kind == SK_ConstructorInitialization or SK_ListConstruction,
  766. /// the constructor to be called.
  767. ///
  768. /// Always a FunctionDecl, plus a Boolean flag telling if it was
  769. /// selected from an overloaded set having size greater than 1.
  770. /// For conversion decls, the naming class is the source type.
  771. /// For construct decls, the naming class is the target type.
  772. struct F Function;
  773. /// When Kind = SK_ConversionSequence, the implicit conversion
  774. /// sequence.
  775. ImplicitConversionSequence *ICS;
  776. /// When Kind = SK_RewrapInitList, the syntactic form of the
  777. /// wrapping list.
  778. InitListExpr *WrappingSyntacticList;
  779. };
  780. void Destroy();
  781. };
  782. private:
  783. /// The kind of initialization sequence computed.
  784. enum SequenceKind SequenceKind;
  785. /// Steps taken by this initialization.
  786. SmallVector<Step, 4> Steps;
  787. public:
  788. /// Describes why initialization failed.
  789. enum FailureKind {
  790. /// Too many initializers provided for a reference.
  791. FK_TooManyInitsForReference,
  792. /// Reference initialized from a parenthesized initializer list.
  793. FK_ParenthesizedListInitForReference,
  794. /// Array must be initialized with an initializer list.
  795. FK_ArrayNeedsInitList,
  796. /// Array must be initialized with an initializer list or a
  797. /// string literal.
  798. FK_ArrayNeedsInitListOrStringLiteral,
  799. /// Array must be initialized with an initializer list or a
  800. /// wide string literal.
  801. FK_ArrayNeedsInitListOrWideStringLiteral,
  802. /// Initializing a wide char array with narrow string literal.
  803. FK_NarrowStringIntoWideCharArray,
  804. /// Initializing char array with wide string literal.
  805. FK_WideStringIntoCharArray,
  806. /// Initializing wide char array with incompatible wide string
  807. /// literal.
  808. FK_IncompatWideStringIntoWideChar,
  809. /// Initializing char8_t array with plain string literal.
  810. FK_PlainStringIntoUTF8Char,
  811. /// Initializing char array with UTF-8 string literal.
  812. FK_UTF8StringIntoPlainChar,
  813. /// Array type mismatch.
  814. FK_ArrayTypeMismatch,
  815. /// Non-constant array initializer
  816. FK_NonConstantArrayInit,
  817. /// Cannot resolve the address of an overloaded function.
  818. FK_AddressOfOverloadFailed,
  819. /// Overloading due to reference initialization failed.
  820. FK_ReferenceInitOverloadFailed,
  821. /// Non-const lvalue reference binding to a temporary.
  822. FK_NonConstLValueReferenceBindingToTemporary,
  823. /// Non-const lvalue reference binding to a bit-field.
  824. FK_NonConstLValueReferenceBindingToBitfield,
  825. /// Non-const lvalue reference binding to a vector element.
  826. FK_NonConstLValueReferenceBindingToVectorElement,
  827. /// Non-const lvalue reference binding to a matrix element.
  828. FK_NonConstLValueReferenceBindingToMatrixElement,
  829. /// Non-const lvalue reference binding to an lvalue of unrelated
  830. /// type.
  831. FK_NonConstLValueReferenceBindingToUnrelated,
  832. /// Rvalue reference binding to an lvalue.
  833. FK_RValueReferenceBindingToLValue,
  834. /// Reference binding drops qualifiers.
  835. FK_ReferenceInitDropsQualifiers,
  836. /// Reference with mismatching address space binding to temporary.
  837. FK_ReferenceAddrspaceMismatchTemporary,
  838. /// Reference binding failed.
  839. FK_ReferenceInitFailed,
  840. /// Implicit conversion failed.
  841. FK_ConversionFailed,
  842. /// Implicit conversion failed.
  843. FK_ConversionFromPropertyFailed,
  844. /// Too many initializers for scalar
  845. FK_TooManyInitsForScalar,
  846. /// Scalar initialized from a parenthesized initializer list.
  847. FK_ParenthesizedListInitForScalar,
  848. /// Reference initialization from an initializer list
  849. FK_ReferenceBindingToInitList,
  850. /// Initialization of some unused destination type with an
  851. /// initializer list.
  852. FK_InitListBadDestinationType,
  853. /// Overloading for a user-defined conversion failed.
  854. FK_UserConversionOverloadFailed,
  855. /// Overloading for initialization by constructor failed.
  856. FK_ConstructorOverloadFailed,
  857. /// Overloading for list-initialization by constructor failed.
  858. FK_ListConstructorOverloadFailed,
  859. /// Default-initialization of a 'const' object.
  860. FK_DefaultInitOfConst,
  861. /// Initialization of an incomplete type.
  862. FK_Incomplete,
  863. /// Variable-length array must not have an initializer.
  864. FK_VariableLengthArrayHasInitializer,
  865. /// List initialization failed at some point.
  866. FK_ListInitializationFailed,
  867. /// Initializer has a placeholder type which cannot be
  868. /// resolved by initialization.
  869. FK_PlaceholderType,
  870. /// Trying to take the address of a function that doesn't support
  871. /// having its address taken.
  872. FK_AddressOfUnaddressableFunction,
  873. /// List-copy-initialization chose an explicit constructor.
  874. FK_ExplicitConstructor,
  875. };
  876. private:
  877. /// The reason why initialization failed.
  878. FailureKind Failure;
  879. /// The failed result of overload resolution.
  880. OverloadingResult FailedOverloadResult;
  881. /// The candidate set created when initialization failed.
  882. OverloadCandidateSet FailedCandidateSet;
  883. /// The incomplete type that caused a failure.
  884. QualType FailedIncompleteType;
  885. /// The fixit that needs to be applied to make this initialization
  886. /// succeed.
  887. std::string ZeroInitializationFixit;
  888. SourceLocation ZeroInitializationFixitLoc;
  889. public:
  890. /// Call for initializations are invalid but that would be valid
  891. /// zero initialzations if Fixit was applied.
  892. void SetZeroInitializationFixit(const std::string& Fixit, SourceLocation L) {
  893. ZeroInitializationFixit = Fixit;
  894. ZeroInitializationFixitLoc = L;
  895. }
  896. private:
  897. /// Prints a follow-up note that highlights the location of
  898. /// the initialized entity, if it's remote.
  899. void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
  900. public:
  901. /// Try to perform initialization of the given entity, creating a
  902. /// record of the steps required to perform the initialization.
  903. ///
  904. /// The generated initialization sequence will either contain enough
  905. /// information to diagnose
  906. ///
  907. /// \param S the semantic analysis object.
  908. ///
  909. /// \param Entity the entity being initialized.
  910. ///
  911. /// \param Kind the kind of initialization being performed.
  912. ///
  913. /// \param Args the argument(s) provided for initialization.
  914. ///
  915. /// \param TopLevelOfInitList true if we are initializing from an expression
  916. /// at the top level inside an initializer list. This disallows
  917. /// narrowing conversions in C++11 onwards.
  918. /// \param TreatUnavailableAsInvalid true if we want to treat unavailable
  919. /// as invalid.
  920. InitializationSequence(Sema &S,
  921. const InitializedEntity &Entity,
  922. const InitializationKind &Kind,
  923. MultiExprArg Args,
  924. bool TopLevelOfInitList = false,
  925. bool TreatUnavailableAsInvalid = true);
  926. void InitializeFrom(Sema &S, const InitializedEntity &Entity,
  927. const InitializationKind &Kind, MultiExprArg Args,
  928. bool TopLevelOfInitList, bool TreatUnavailableAsInvalid);
  929. ~InitializationSequence();
  930. /// Perform the actual initialization of the given entity based on
  931. /// the computed initialization sequence.
  932. ///
  933. /// \param S the semantic analysis object.
  934. ///
  935. /// \param Entity the entity being initialized.
  936. ///
  937. /// \param Kind the kind of initialization being performed.
  938. ///
  939. /// \param Args the argument(s) provided for initialization, ownership of
  940. /// which is transferred into the routine.
  941. ///
  942. /// \param ResultType if non-NULL, will be set to the type of the
  943. /// initialized object, which is the type of the declaration in most
  944. /// cases. However, when the initialized object is a variable of
  945. /// incomplete array type and the initializer is an initializer
  946. /// list, this type will be set to the completed array type.
  947. ///
  948. /// \returns an expression that performs the actual object initialization, if
  949. /// the initialization is well-formed. Otherwise, emits diagnostics
  950. /// and returns an invalid expression.
  951. ExprResult Perform(Sema &S,
  952. const InitializedEntity &Entity,
  953. const InitializationKind &Kind,
  954. MultiExprArg Args,
  955. QualType *ResultType = nullptr);
  956. /// Diagnose an potentially-invalid initialization sequence.
  957. ///
  958. /// \returns true if the initialization sequence was ill-formed,
  959. /// false otherwise.
  960. bool Diagnose(Sema &S,
  961. const InitializedEntity &Entity,
  962. const InitializationKind &Kind,
  963. ArrayRef<Expr *> Args);
  964. /// Determine the kind of initialization sequence computed.
  965. enum SequenceKind getKind() const { return SequenceKind; }
  966. /// Set the kind of sequence computed.
  967. void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
  968. /// Determine whether the initialization sequence is valid.
  969. explicit operator bool() const { return !Failed(); }
  970. /// Determine whether the initialization sequence is invalid.
  971. bool Failed() const { return SequenceKind == FailedSequence; }
  972. using step_iterator = SmallVectorImpl<Step>::const_iterator;
  973. step_iterator step_begin() const { return Steps.begin(); }
  974. step_iterator step_end() const { return Steps.end(); }
  975. using step_range = llvm::iterator_range<step_iterator>;
  976. step_range steps() const { return {step_begin(), step_end()}; }
  977. /// Determine whether this initialization is a direct reference
  978. /// binding (C++ [dcl.init.ref]).
  979. bool isDirectReferenceBinding() const;
  980. /// Determine whether this initialization failed due to an ambiguity.
  981. bool isAmbiguous() const;
  982. /// Determine whether this initialization is direct call to a
  983. /// constructor.
  984. bool isConstructorInitialization() const;
  985. /// Add a new step in the initialization that resolves the address
  986. /// of an overloaded function to a specific function declaration.
  987. ///
  988. /// \param Function the function to which the overloaded function reference
  989. /// resolves.
  990. void AddAddressOverloadResolutionStep(FunctionDecl *Function,
  991. DeclAccessPair Found,
  992. bool HadMultipleCandidates);
  993. /// Add a new step in the initialization that performs a derived-to-
  994. /// base cast.
  995. ///
  996. /// \param BaseType the base type to which we will be casting.
  997. ///
  998. /// \param Category Indicates whether the result will be treated as an
  999. /// rvalue, an xvalue, or an lvalue.
  1000. void AddDerivedToBaseCastStep(QualType BaseType,
  1001. ExprValueKind Category);
  1002. /// Add a new step binding a reference to an object.
  1003. ///
  1004. /// \param BindingTemporary True if we are binding a reference to a temporary
  1005. /// object (thereby extending its lifetime); false if we are binding to an
  1006. /// lvalue or an lvalue treated as an rvalue.
  1007. void AddReferenceBindingStep(QualType T, bool BindingTemporary);
  1008. /// Add a new step that makes an extraneous copy of the input
  1009. /// to a temporary of the same class type.
  1010. ///
  1011. /// This extraneous copy only occurs during reference binding in
  1012. /// C++98/03, where we are permitted (but not required) to introduce
  1013. /// an extra copy. At a bare minimum, we must check that we could
  1014. /// call the copy constructor, and produce a diagnostic if the copy
  1015. /// constructor is inaccessible or no copy constructor matches.
  1016. //
  1017. /// \param T The type of the temporary being created.
  1018. void AddExtraneousCopyToTemporary(QualType T);
  1019. /// Add a new step that makes a copy of the input to an object of
  1020. /// the given type, as the final step in class copy-initialization.
  1021. void AddFinalCopy(QualType T);
  1022. /// Add a new step invoking a conversion function, which is either
  1023. /// a constructor or a conversion function.
  1024. void AddUserConversionStep(FunctionDecl *Function,
  1025. DeclAccessPair FoundDecl,
  1026. QualType T,
  1027. bool HadMultipleCandidates);
  1028. /// Add a new step that performs a qualification conversion to the
  1029. /// given type.
  1030. void AddQualificationConversionStep(QualType Ty,
  1031. ExprValueKind Category);
  1032. /// Add a new step that performs a function reference conversion to the
  1033. /// given type.
  1034. void AddFunctionReferenceConversionStep(QualType Ty);
  1035. /// Add a new step that performs conversion from non-atomic to atomic
  1036. /// type.
  1037. void AddAtomicConversionStep(QualType Ty);
  1038. /// Add a new step that applies an implicit conversion sequence.
  1039. void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
  1040. QualType T, bool TopLevelOfInitList = false);
  1041. /// Add a list-initialization step.
  1042. void AddListInitializationStep(QualType T);
  1043. /// Add a constructor-initialization step.
  1044. ///
  1045. /// \param FromInitList The constructor call is syntactically an initializer
  1046. /// list.
  1047. /// \param AsInitList The constructor is called as an init list constructor.
  1048. void AddConstructorInitializationStep(DeclAccessPair FoundDecl,
  1049. CXXConstructorDecl *Constructor,
  1050. QualType T,
  1051. bool HadMultipleCandidates,
  1052. bool FromInitList, bool AsInitList);
  1053. /// Add a zero-initialization step.
  1054. void AddZeroInitializationStep(QualType T);
  1055. /// Add a C assignment step.
  1056. //
  1057. // FIXME: It isn't clear whether this should ever be needed;
  1058. // ideally, we would handle everything needed in C in the common
  1059. // path. However, that isn't the case yet.
  1060. void AddCAssignmentStep(QualType T);
  1061. /// Add a string init step.
  1062. void AddStringInitStep(QualType T);
  1063. /// Add an Objective-C object conversion step, which is
  1064. /// always a no-op.
  1065. void AddObjCObjectConversionStep(QualType T);
  1066. /// Add an array initialization loop step.
  1067. void AddArrayInitLoopStep(QualType T, QualType EltTy);
  1068. /// Add an array initialization step.
  1069. void AddArrayInitStep(QualType T, bool IsGNUExtension);
  1070. /// Add a parenthesized array initialization step.
  1071. void AddParenthesizedArrayInitStep(QualType T);
  1072. /// Add a step to pass an object by indirect copy-restore.
  1073. void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy);
  1074. /// Add a step to "produce" an Objective-C object (by
  1075. /// retaining it).
  1076. void AddProduceObjCObjectStep(QualType T);
  1077. /// Add a step to construct a std::initializer_list object from an
  1078. /// initializer list.
  1079. void AddStdInitializerListConstructionStep(QualType T);
  1080. /// Add a step to initialize an OpenCL sampler from an integer
  1081. /// constant.
  1082. void AddOCLSamplerInitStep(QualType T);
  1083. /// Add a step to initialzie an OpenCL opaque type (event_t, queue_t, etc.)
  1084. /// from a zero constant.
  1085. void AddOCLZeroOpaqueTypeStep(QualType T);
  1086. /// Add steps to unwrap a initializer list for a reference around a
  1087. /// single element and rewrap it at the end.
  1088. void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic);
  1089. /// Note that this initialization sequence failed.
  1090. void SetFailed(FailureKind Failure) {
  1091. SequenceKind = FailedSequence;
  1092. this->Failure = Failure;
  1093. assert((Failure != FK_Incomplete || !FailedIncompleteType.isNull()) &&
  1094. "Incomplete type failure requires a type!");
  1095. }
  1096. /// Note that this initialization sequence failed due to failed
  1097. /// overload resolution.
  1098. void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
  1099. /// Retrieve a reference to the candidate set when overload
  1100. /// resolution fails.
  1101. OverloadCandidateSet &getFailedCandidateSet() {
  1102. return FailedCandidateSet;
  1103. }
  1104. /// Get the overloading result, for when the initialization
  1105. /// sequence failed due to a bad overload.
  1106. OverloadingResult getFailedOverloadResult() const {
  1107. return FailedOverloadResult;
  1108. }
  1109. /// Note that this initialization sequence failed due to an
  1110. /// incomplete type.
  1111. void setIncompleteTypeFailure(QualType IncompleteType) {
  1112. FailedIncompleteType = IncompleteType;
  1113. SetFailed(FK_Incomplete);
  1114. }
  1115. /// Determine why initialization failed.
  1116. FailureKind getFailureKind() const {
  1117. assert(Failed() && "Not an initialization failure!");
  1118. return Failure;
  1119. }
  1120. /// Dump a representation of this initialization sequence to
  1121. /// the given stream, for debugging purposes.
  1122. void dump(raw_ostream &OS) const;
  1123. /// Dump a representation of this initialization sequence to
  1124. /// standard error, for debugging purposes.
  1125. void dump() const;
  1126. };
  1127. } // namespace clang
  1128. #endif // LLVM_CLANG_SEMA_INITIALIZATION_H
  1129. #ifdef __GNUC__
  1130. #pragma GCC diagnostic pop
  1131. #endif