Initialization.h 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. /// Initialize an aggreagate with parenthesized list of values.
  749. /// This is a C++20 feature.
  750. SK_ParenthesizedListInit
  751. };
  752. /// A single step in the initialization sequence.
  753. class Step {
  754. public:
  755. /// The kind of conversion or initialization step we are taking.
  756. StepKind Kind;
  757. // The type that results from this initialization.
  758. QualType Type;
  759. struct F {
  760. bool HadMultipleCandidates;
  761. FunctionDecl *Function;
  762. DeclAccessPair FoundDecl;
  763. };
  764. union {
  765. /// When Kind == SK_ResolvedOverloadedFunction or Kind ==
  766. /// SK_UserConversion, the function that the expression should be
  767. /// resolved to or the conversion function to call, respectively.
  768. /// When Kind == SK_ConstructorInitialization or SK_ListConstruction,
  769. /// the constructor to be called.
  770. ///
  771. /// Always a FunctionDecl, plus a Boolean flag telling if it was
  772. /// selected from an overloaded set having size greater than 1.
  773. /// For conversion decls, the naming class is the source type.
  774. /// For construct decls, the naming class is the target type.
  775. struct F Function;
  776. /// When Kind = SK_ConversionSequence, the implicit conversion
  777. /// sequence.
  778. ImplicitConversionSequence *ICS;
  779. /// When Kind = SK_RewrapInitList, the syntactic form of the
  780. /// wrapping list.
  781. InitListExpr *WrappingSyntacticList;
  782. };
  783. void Destroy();
  784. };
  785. private:
  786. /// The kind of initialization sequence computed.
  787. enum SequenceKind SequenceKind;
  788. /// Steps taken by this initialization.
  789. SmallVector<Step, 4> Steps;
  790. public:
  791. /// Describes why initialization failed.
  792. enum FailureKind {
  793. /// Too many initializers provided for a reference.
  794. FK_TooManyInitsForReference,
  795. /// Reference initialized from a parenthesized initializer list.
  796. FK_ParenthesizedListInitForReference,
  797. /// Array must be initialized with an initializer list.
  798. FK_ArrayNeedsInitList,
  799. /// Array must be initialized with an initializer list or a
  800. /// string literal.
  801. FK_ArrayNeedsInitListOrStringLiteral,
  802. /// Array must be initialized with an initializer list or a
  803. /// wide string literal.
  804. FK_ArrayNeedsInitListOrWideStringLiteral,
  805. /// Initializing a wide char array with narrow string literal.
  806. FK_NarrowStringIntoWideCharArray,
  807. /// Initializing char array with wide string literal.
  808. FK_WideStringIntoCharArray,
  809. /// Initializing wide char array with incompatible wide string
  810. /// literal.
  811. FK_IncompatWideStringIntoWideChar,
  812. /// Initializing char8_t array with plain string literal.
  813. FK_PlainStringIntoUTF8Char,
  814. /// Initializing char array with UTF-8 string literal.
  815. FK_UTF8StringIntoPlainChar,
  816. /// Array type mismatch.
  817. FK_ArrayTypeMismatch,
  818. /// Non-constant array initializer
  819. FK_NonConstantArrayInit,
  820. /// Cannot resolve the address of an overloaded function.
  821. FK_AddressOfOverloadFailed,
  822. /// Overloading due to reference initialization failed.
  823. FK_ReferenceInitOverloadFailed,
  824. /// Non-const lvalue reference binding to a temporary.
  825. FK_NonConstLValueReferenceBindingToTemporary,
  826. /// Non-const lvalue reference binding to a bit-field.
  827. FK_NonConstLValueReferenceBindingToBitfield,
  828. /// Non-const lvalue reference binding to a vector element.
  829. FK_NonConstLValueReferenceBindingToVectorElement,
  830. /// Non-const lvalue reference binding to a matrix element.
  831. FK_NonConstLValueReferenceBindingToMatrixElement,
  832. /// Non-const lvalue reference binding to an lvalue of unrelated
  833. /// type.
  834. FK_NonConstLValueReferenceBindingToUnrelated,
  835. /// Rvalue reference binding to an lvalue.
  836. FK_RValueReferenceBindingToLValue,
  837. /// Reference binding drops qualifiers.
  838. FK_ReferenceInitDropsQualifiers,
  839. /// Reference with mismatching address space binding to temporary.
  840. FK_ReferenceAddrspaceMismatchTemporary,
  841. /// Reference binding failed.
  842. FK_ReferenceInitFailed,
  843. /// Implicit conversion failed.
  844. FK_ConversionFailed,
  845. /// Implicit conversion failed.
  846. FK_ConversionFromPropertyFailed,
  847. /// Too many initializers for scalar
  848. FK_TooManyInitsForScalar,
  849. /// Scalar initialized from a parenthesized initializer list.
  850. FK_ParenthesizedListInitForScalar,
  851. /// Reference initialization from an initializer list
  852. FK_ReferenceBindingToInitList,
  853. /// Initialization of some unused destination type with an
  854. /// initializer list.
  855. FK_InitListBadDestinationType,
  856. /// Overloading for a user-defined conversion failed.
  857. FK_UserConversionOverloadFailed,
  858. /// Overloading for initialization by constructor failed.
  859. FK_ConstructorOverloadFailed,
  860. /// Overloading for list-initialization by constructor failed.
  861. FK_ListConstructorOverloadFailed,
  862. /// Default-initialization of a 'const' object.
  863. FK_DefaultInitOfConst,
  864. /// Initialization of an incomplete type.
  865. FK_Incomplete,
  866. /// Variable-length array must not have an initializer.
  867. FK_VariableLengthArrayHasInitializer,
  868. /// List initialization failed at some point.
  869. FK_ListInitializationFailed,
  870. /// Initializer has a placeholder type which cannot be
  871. /// resolved by initialization.
  872. FK_PlaceholderType,
  873. /// Trying to take the address of a function that doesn't support
  874. /// having its address taken.
  875. FK_AddressOfUnaddressableFunction,
  876. /// List-copy-initialization chose an explicit constructor.
  877. FK_ExplicitConstructor,
  878. /// Parenthesized list initialization failed at some point.
  879. /// This is a C++20 feature.
  880. FK_ParenthesizedListInitFailed,
  881. };
  882. private:
  883. /// The reason why initialization failed.
  884. FailureKind Failure;
  885. /// The failed result of overload resolution.
  886. OverloadingResult FailedOverloadResult;
  887. /// The candidate set created when initialization failed.
  888. OverloadCandidateSet FailedCandidateSet;
  889. /// The incomplete type that caused a failure.
  890. QualType FailedIncompleteType;
  891. /// The fixit that needs to be applied to make this initialization
  892. /// succeed.
  893. std::string ZeroInitializationFixit;
  894. SourceLocation ZeroInitializationFixitLoc;
  895. public:
  896. /// Call for initializations are invalid but that would be valid
  897. /// zero initialzations if Fixit was applied.
  898. void SetZeroInitializationFixit(const std::string& Fixit, SourceLocation L) {
  899. ZeroInitializationFixit = Fixit;
  900. ZeroInitializationFixitLoc = L;
  901. }
  902. private:
  903. /// Prints a follow-up note that highlights the location of
  904. /// the initialized entity, if it's remote.
  905. void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity);
  906. public:
  907. /// Try to perform initialization of the given entity, creating a
  908. /// record of the steps required to perform the initialization.
  909. ///
  910. /// The generated initialization sequence will either contain enough
  911. /// information to diagnose
  912. ///
  913. /// \param S the semantic analysis object.
  914. ///
  915. /// \param Entity the entity being initialized.
  916. ///
  917. /// \param Kind the kind of initialization being performed.
  918. ///
  919. /// \param Args the argument(s) provided for initialization.
  920. ///
  921. /// \param TopLevelOfInitList true if we are initializing from an expression
  922. /// at the top level inside an initializer list. This disallows
  923. /// narrowing conversions in C++11 onwards.
  924. /// \param TreatUnavailableAsInvalid true if we want to treat unavailable
  925. /// as invalid.
  926. InitializationSequence(Sema &S,
  927. const InitializedEntity &Entity,
  928. const InitializationKind &Kind,
  929. MultiExprArg Args,
  930. bool TopLevelOfInitList = false,
  931. bool TreatUnavailableAsInvalid = true);
  932. void InitializeFrom(Sema &S, const InitializedEntity &Entity,
  933. const InitializationKind &Kind, MultiExprArg Args,
  934. bool TopLevelOfInitList, bool TreatUnavailableAsInvalid);
  935. ~InitializationSequence();
  936. /// Perform the actual initialization of the given entity based on
  937. /// the computed initialization sequence.
  938. ///
  939. /// \param S the semantic analysis object.
  940. ///
  941. /// \param Entity the entity being initialized.
  942. ///
  943. /// \param Kind the kind of initialization being performed.
  944. ///
  945. /// \param Args the argument(s) provided for initialization, ownership of
  946. /// which is transferred into the routine.
  947. ///
  948. /// \param ResultType if non-NULL, will be set to the type of the
  949. /// initialized object, which is the type of the declaration in most
  950. /// cases. However, when the initialized object is a variable of
  951. /// incomplete array type and the initializer is an initializer
  952. /// list, this type will be set to the completed array type.
  953. ///
  954. /// \returns an expression that performs the actual object initialization, if
  955. /// the initialization is well-formed. Otherwise, emits diagnostics
  956. /// and returns an invalid expression.
  957. ExprResult Perform(Sema &S,
  958. const InitializedEntity &Entity,
  959. const InitializationKind &Kind,
  960. MultiExprArg Args,
  961. QualType *ResultType = nullptr);
  962. /// Diagnose an potentially-invalid initialization sequence.
  963. ///
  964. /// \returns true if the initialization sequence was ill-formed,
  965. /// false otherwise.
  966. bool Diagnose(Sema &S,
  967. const InitializedEntity &Entity,
  968. const InitializationKind &Kind,
  969. ArrayRef<Expr *> Args);
  970. /// Determine the kind of initialization sequence computed.
  971. enum SequenceKind getKind() const { return SequenceKind; }
  972. /// Set the kind of sequence computed.
  973. void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; }
  974. /// Determine whether the initialization sequence is valid.
  975. explicit operator bool() const { return !Failed(); }
  976. /// Determine whether the initialization sequence is invalid.
  977. bool Failed() const { return SequenceKind == FailedSequence; }
  978. using step_iterator = SmallVectorImpl<Step>::const_iterator;
  979. step_iterator step_begin() const { return Steps.begin(); }
  980. step_iterator step_end() const { return Steps.end(); }
  981. using step_range = llvm::iterator_range<step_iterator>;
  982. step_range steps() const { return {step_begin(), step_end()}; }
  983. /// Determine whether this initialization is a direct reference
  984. /// binding (C++ [dcl.init.ref]).
  985. bool isDirectReferenceBinding() const;
  986. /// Determine whether this initialization failed due to an ambiguity.
  987. bool isAmbiguous() const;
  988. /// Determine whether this initialization is direct call to a
  989. /// constructor.
  990. bool isConstructorInitialization() const;
  991. /// Add a new step in the initialization that resolves the address
  992. /// of an overloaded function to a specific function declaration.
  993. ///
  994. /// \param Function the function to which the overloaded function reference
  995. /// resolves.
  996. void AddAddressOverloadResolutionStep(FunctionDecl *Function,
  997. DeclAccessPair Found,
  998. bool HadMultipleCandidates);
  999. /// Add a new step in the initialization that performs a derived-to-
  1000. /// base cast.
  1001. ///
  1002. /// \param BaseType the base type to which we will be casting.
  1003. ///
  1004. /// \param Category Indicates whether the result will be treated as an
  1005. /// rvalue, an xvalue, or an lvalue.
  1006. void AddDerivedToBaseCastStep(QualType BaseType,
  1007. ExprValueKind Category);
  1008. /// Add a new step binding a reference to an object.
  1009. ///
  1010. /// \param BindingTemporary True if we are binding a reference to a temporary
  1011. /// object (thereby extending its lifetime); false if we are binding to an
  1012. /// lvalue or an lvalue treated as an rvalue.
  1013. void AddReferenceBindingStep(QualType T, bool BindingTemporary);
  1014. /// Add a new step that makes an extraneous copy of the input
  1015. /// to a temporary of the same class type.
  1016. ///
  1017. /// This extraneous copy only occurs during reference binding in
  1018. /// C++98/03, where we are permitted (but not required) to introduce
  1019. /// an extra copy. At a bare minimum, we must check that we could
  1020. /// call the copy constructor, and produce a diagnostic if the copy
  1021. /// constructor is inaccessible or no copy constructor matches.
  1022. //
  1023. /// \param T The type of the temporary being created.
  1024. void AddExtraneousCopyToTemporary(QualType T);
  1025. /// Add a new step that makes a copy of the input to an object of
  1026. /// the given type, as the final step in class copy-initialization.
  1027. void AddFinalCopy(QualType T);
  1028. /// Add a new step invoking a conversion function, which is either
  1029. /// a constructor or a conversion function.
  1030. void AddUserConversionStep(FunctionDecl *Function,
  1031. DeclAccessPair FoundDecl,
  1032. QualType T,
  1033. bool HadMultipleCandidates);
  1034. /// Add a new step that performs a qualification conversion to the
  1035. /// given type.
  1036. void AddQualificationConversionStep(QualType Ty,
  1037. ExprValueKind Category);
  1038. /// Add a new step that performs a function reference conversion to the
  1039. /// given type.
  1040. void AddFunctionReferenceConversionStep(QualType Ty);
  1041. /// Add a new step that performs conversion from non-atomic to atomic
  1042. /// type.
  1043. void AddAtomicConversionStep(QualType Ty);
  1044. /// Add a new step that applies an implicit conversion sequence.
  1045. void AddConversionSequenceStep(const ImplicitConversionSequence &ICS,
  1046. QualType T, bool TopLevelOfInitList = false);
  1047. /// Add a list-initialization step.
  1048. void AddListInitializationStep(QualType T);
  1049. /// Add a constructor-initialization step.
  1050. ///
  1051. /// \param FromInitList The constructor call is syntactically an initializer
  1052. /// list.
  1053. /// \param AsInitList The constructor is called as an init list constructor.
  1054. void AddConstructorInitializationStep(DeclAccessPair FoundDecl,
  1055. CXXConstructorDecl *Constructor,
  1056. QualType T,
  1057. bool HadMultipleCandidates,
  1058. bool FromInitList, bool AsInitList);
  1059. /// Add a zero-initialization step.
  1060. void AddZeroInitializationStep(QualType T);
  1061. /// Add a C assignment step.
  1062. //
  1063. // FIXME: It isn't clear whether this should ever be needed;
  1064. // ideally, we would handle everything needed in C in the common
  1065. // path. However, that isn't the case yet.
  1066. void AddCAssignmentStep(QualType T);
  1067. /// Add a string init step.
  1068. void AddStringInitStep(QualType T);
  1069. /// Add an Objective-C object conversion step, which is
  1070. /// always a no-op.
  1071. void AddObjCObjectConversionStep(QualType T);
  1072. /// Add an array initialization loop step.
  1073. void AddArrayInitLoopStep(QualType T, QualType EltTy);
  1074. /// Add an array initialization step.
  1075. void AddArrayInitStep(QualType T, bool IsGNUExtension);
  1076. /// Add a parenthesized array initialization step.
  1077. void AddParenthesizedArrayInitStep(QualType T);
  1078. /// Add a step to pass an object by indirect copy-restore.
  1079. void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy);
  1080. /// Add a step to "produce" an Objective-C object (by
  1081. /// retaining it).
  1082. void AddProduceObjCObjectStep(QualType T);
  1083. /// Add a step to construct a std::initializer_list object from an
  1084. /// initializer list.
  1085. void AddStdInitializerListConstructionStep(QualType T);
  1086. /// Add a step to initialize an OpenCL sampler from an integer
  1087. /// constant.
  1088. void AddOCLSamplerInitStep(QualType T);
  1089. /// Add a step to initialzie an OpenCL opaque type (event_t, queue_t, etc.)
  1090. /// from a zero constant.
  1091. void AddOCLZeroOpaqueTypeStep(QualType T);
  1092. void AddParenthesizedListInitStep(QualType T);
  1093. /// Add steps to unwrap a initializer list for a reference around a
  1094. /// single element and rewrap it at the end.
  1095. void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic);
  1096. /// Note that this initialization sequence failed.
  1097. void SetFailed(FailureKind Failure) {
  1098. SequenceKind = FailedSequence;
  1099. this->Failure = Failure;
  1100. assert((Failure != FK_Incomplete || !FailedIncompleteType.isNull()) &&
  1101. "Incomplete type failure requires a type!");
  1102. }
  1103. /// Note that this initialization sequence failed due to failed
  1104. /// overload resolution.
  1105. void SetOverloadFailure(FailureKind Failure, OverloadingResult Result);
  1106. /// Retrieve a reference to the candidate set when overload
  1107. /// resolution fails.
  1108. OverloadCandidateSet &getFailedCandidateSet() {
  1109. return FailedCandidateSet;
  1110. }
  1111. /// Get the overloading result, for when the initialization
  1112. /// sequence failed due to a bad overload.
  1113. OverloadingResult getFailedOverloadResult() const {
  1114. return FailedOverloadResult;
  1115. }
  1116. /// Note that this initialization sequence failed due to an
  1117. /// incomplete type.
  1118. void setIncompleteTypeFailure(QualType IncompleteType) {
  1119. FailedIncompleteType = IncompleteType;
  1120. SetFailed(FK_Incomplete);
  1121. }
  1122. /// Determine why initialization failed.
  1123. FailureKind getFailureKind() const {
  1124. assert(Failed() && "Not an initialization failure!");
  1125. return Failure;
  1126. }
  1127. /// Dump a representation of this initialization sequence to
  1128. /// the given stream, for debugging purposes.
  1129. void dump(raw_ostream &OS) const;
  1130. /// Dump a representation of this initialization sequence to
  1131. /// standard error, for debugging purposes.
  1132. void dump() const;
  1133. };
  1134. } // namespace clang
  1135. #endif // LLVM_CLANG_SEMA_INITIALIZATION_H
  1136. #ifdef __GNUC__
  1137. #pragma GCC diagnostic pop
  1138. #endif