BfResolvedTypeUtils.h 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. #pragma once
  2. #pragma once
  3. #include "BfAst.h"
  4. #include <unordered_map>
  5. #include "BfSource.h"
  6. #include "BfIRBuilder.h"
  7. #include "BeefySysLib/util/MultiHashSet.h"
  8. NS_BF_BEGIN
  9. class BfModule;
  10. class BfType;
  11. class BfTypeInstance;
  12. class BfContext;
  13. class BfCustomAttributes;
  14. enum BfResolveTypeRefFlags
  15. {
  16. BfResolveTypeRefFlag_None = 0,
  17. BfResolveTypeRefFlag_NoResolveGenericParam = 1,
  18. BfResolveTypeRefFlag_AllowRef = 2,
  19. BfResolveTypeRefFlag_AllowRefGeneric = 4,
  20. BfResolveTypeRefFlag_IgnoreLookupError = 8,
  21. BfResolveTypeRefFlag_AllowGenericTypeParamConstValue = 0x10,
  22. BfResolveTypeRefFlag_AllowGenericMethodParamConstValue = 0x20,
  23. BfResolveTypeRefFlag_AllowGenericParamConstValue = 0x10 | 0x20,
  24. BfResolveTypeRefFlag_AutoComplete = 0x40,
  25. BfResolveTypeRefFlag_FromIndirectSource = 0x80, // Such as a type alias or a generic parameter
  26. BfResolveTypeRefFlag_Attribute = 0x100,
  27. BfResolveTypeRefFlag_NoReify = 0x200,
  28. BfResolveTypeRefFlag_NoCreate = 0x400,
  29. BfResolveTypeRefFlag_NoWarnOnMut = 0x800
  30. };
  31. enum BfTypeNameFlags : uint16
  32. {
  33. BfTypeNameFlags_None = 0,
  34. BfTypeNameFlag_ResolveGenericParamNames = 1,
  35. BfTypeNameFlag_UseUnspecializedGenericParamNames = 2,
  36. BfTypeNameFlag_OmitNamespace = 4,
  37. BfTypeNameFlag_OmitOuterType = 8,
  38. BfTypeNameFlag_ReduceName = 0x10,
  39. BfTypeNameFlag_UseArrayImplType = 0x20,
  40. BfTypeNameFlag_DisambiguateDups = 0x40, // Add a disambiguation if mDupDetectedRevision is set
  41. BfTypeNameFlag_AddGlobalContainerName = 0x80,
  42. BfTypeNameFlag_InternalName = 0x100, // Use special delimiters to remove ambiguities (ie: '+' for inner types)
  43. BfTypeNameFlag_HideGlobalName = 0x200,
  44. BfTypeNameFlag_ExtendedInfo = 0x400
  45. };
  46. enum BfMethodNameFlags : uint8
  47. {
  48. BfMethodNameFlag_None = 0,
  49. BfMethodNameFlag_ResolveGenericParamNames = 1,
  50. BfMethodNameFlag_OmitTypeName = 2,
  51. BfMethodNameFlag_IncludeReturnType = 4,
  52. BfMethodNameFlag_OmitParams = 8
  53. };
  54. enum BfGetMethodInstanceFlags : uint16
  55. {
  56. BfGetMethodInstanceFlag_None = 0,
  57. BfGetMethodInstanceFlag_UnspecializedPass = 1,
  58. BfGetMethodInstanceFlag_ExplicitSpecializedModule = 2,
  59. BfGetMethodInstanceFlag_ExplicitResolveOnlyPass = 4,
  60. BfGetMethodInstanceFlag_ForeignMethodDef = 8,
  61. BfGetMethodInstanceFlag_Unreified = 0x10,
  62. BfGetMethodInstanceFlag_NoForceReification = 0x20,
  63. BfGetMethodInstanceFlag_ResultNotUsed = 0x40,
  64. BfGetMethodInstanceFlag_ForceInline = 0x80,
  65. BfGetMethodInstanceFlag_Friend = 0x100,
  66. BfGetMethodInstanceFlag_DisableObjectAccessChecks = 0x200,
  67. BfGetMethodInstanceFlag_NoInline = 0x400,
  68. BfGetMethodInstanceFlag_DepthExceeded = 0x800
  69. };
  70. class BfDependencyMap
  71. {
  72. public:
  73. enum DependencyFlags
  74. {
  75. DependencyFlag_None = 0,
  76. DependencyFlag_Calls = 1,
  77. DependencyFlag_InlinedCall = 2,
  78. DependencyFlag_ReadFields = 4,
  79. DependencyFlag_DerivedFrom = 8,
  80. DependencyFlag_OuterType = 0x10,
  81. DependencyFlag_ImplementsInterface = 0x20,
  82. DependencyFlag_ValueTypeMemberData = 0x40,
  83. DependencyFlag_PtrMemberData = 0x80,
  84. DependencyFlag_StaticValue = 0x100,
  85. DependencyFlag_ConstValue = 0x200,
  86. DependencyFlag_MethodGenericArg = 0x400,
  87. DependencyFlag_LocalUsage = 0x800,
  88. DependencyFlag_ExprTypeReference = 0x1000,
  89. DependencyFlag_TypeGenericArg = 0x2000,
  90. DependencyFlag_UnspecializedType = 0x4000,
  91. DependencyFlag_GenericArgRef = 0x8000,
  92. DependencyFlag_ParamOrReturnValue = 0x10000,
  93. DependencyFlag_CustomAttribute = 0x20000,
  94. DependencyFlag_Constraint = 0x40000,
  95. DependencyFlag_StructElementType = 0x80000,
  96. DependencyFlag_TypeReference = 0x100000, // Explicit type reference for things like tuples, so all referencing types get passed over on symbol reference
  97. DependencyFlag_Allocates = 0x200000,
  98. DependencyFlag_NameReference = 0x400000,
  99. DependencyFlag_VirtualCall = 0x800000,
  100. DependencyFlag_WeakReference = 0x1000000, // Keeps alive but won't rebuild
  101. DependencyFlag_DependentUsageMask = ~(DependencyFlag_UnspecializedType | DependencyFlag_MethodGenericArg | DependencyFlag_GenericArgRef)
  102. };
  103. struct DependencyEntry
  104. {
  105. int mRevision;
  106. DependencyFlags mFlags;
  107. DependencyEntry(int revision, DependencyFlags flags)
  108. {
  109. mRevision = revision;
  110. mFlags = flags;
  111. }
  112. };
  113. public:
  114. typedef Dictionary<BfType*, DependencyEntry> TypeMap;
  115. TypeMap mTypeSet;
  116. int mMinDependDepth;
  117. public:
  118. BfDependencyMap()
  119. {
  120. mMinDependDepth = 0;
  121. }
  122. bool AddUsedBy(BfType* dependentType, DependencyFlags flags);
  123. bool IsEmpty();
  124. TypeMap::iterator begin();
  125. TypeMap::iterator end();
  126. TypeMap::iterator erase(TypeMap::iterator& itr);
  127. };
  128. enum BfHotDepDataKind : int8
  129. {
  130. BfHotDepDataKind_Unknown,
  131. BfHotDepDataKind_TypeVersion,
  132. BfHotDepDataKind_ThisType,
  133. BfHotDepDataKind_Allocation,
  134. BfHotDepDataKind_Method,
  135. BfHotDepDataKind_DupMethod,
  136. BfHotDepDataKind_DevirtualizedMethod,
  137. BfHotDepDataKind_InnerMethod,
  138. BfHotDepDataKind_FunctionPtr,
  139. BfHotDepDataKind_VirtualDecl
  140. };
  141. enum BfHotDepDataFlags : int8
  142. {
  143. BfHotDepDataFlag_None = 0,
  144. BfHotDepDataFlag_AlwaysCalled = 1,
  145. BfHotDepDataFlag_NotReified = 2,
  146. BfHotDepDataFlag_IsOriginalBuild = 4,
  147. BfHotDepDataFlag_IsBound = 8,
  148. BfHotDepDataFlag_HasDup = 0x10,
  149. BfHotDepDataFlag_RetainMethodWithoutBinding = 0x20,
  150. };
  151. class BfHotDepData
  152. {
  153. public:
  154. BfHotDepDataKind mDataKind;
  155. BfHotDepDataFlags mFlags;
  156. int32 mRefCount;
  157. BfHotDepData()
  158. {
  159. mDataKind = BfHotDepDataKind_Unknown;
  160. mFlags = BfHotDepDataFlag_None;
  161. mRefCount = 0;
  162. }
  163. void Deref();
  164. #ifdef _DEBUG
  165. virtual ~BfHotDepData()
  166. {
  167. BF_ASSERT(mRefCount == 0);
  168. }
  169. #endif
  170. };
  171. struct BfHotTypeDataEntry
  172. {
  173. public:
  174. String mFuncName; // Name of original virtual function, not overrides
  175. };
  176. class BfHotTypeVersion : public BfHotDepData
  177. {
  178. public:
  179. Val128 mDataHash; // Determines when the user's declaration changed - changes do not cascade into including types
  180. BfHotTypeVersion* mBaseType;
  181. Array<BfHotTypeVersion*> mMembers; // Struct members directly included (not via pointers)
  182. int mDeclHotCompileIdx;
  183. int mCommittedHotCompileIdx;
  184. int mTypeId;
  185. Array<int> mInterfaceMapping;
  186. bool mPopulatedInterfaceMapping;
  187. BfHotTypeVersion()
  188. {
  189. mBaseType = NULL;
  190. mDataKind = BfHotDepDataKind_TypeVersion;
  191. mPopulatedInterfaceMapping = 0;
  192. }
  193. ~BfHotTypeVersion();
  194. };
  195. class BfHotThisType : public BfHotDepData
  196. {
  197. public:
  198. BfHotTypeVersion* mTypeVersion;
  199. BfHotThisType(BfHotTypeVersion* typeVersion)
  200. {
  201. mDataKind = BfHotDepDataKind_ThisType;
  202. mTypeVersion = typeVersion;
  203. mTypeVersion->mRefCount++;
  204. }
  205. ~BfHotThisType()
  206. {
  207. mTypeVersion->Deref();
  208. }
  209. };
  210. class BfHotAllocation : public BfHotDepData
  211. {
  212. public:
  213. BfHotTypeVersion* mTypeVersion;
  214. BfHotAllocation(BfHotTypeVersion* typeVersion)
  215. {
  216. mDataKind = BfHotDepDataKind_Allocation;
  217. mTypeVersion = typeVersion;
  218. mTypeVersion->mRefCount++;
  219. }
  220. ~BfHotAllocation()
  221. {
  222. mTypeVersion->Deref();
  223. }
  224. };
  225. //#define BF_DBG_HOTMETHOD_NAME
  226. //#define BF_DBG_HOTMETHOD_IDX
  227. class BfHotMethod : public BfHotDepData
  228. {
  229. public:
  230. #ifdef BF_DBG_HOTMETHOD_NAME
  231. String mMangledName;
  232. #endif
  233. #ifdef BF_DBG_HOTMETHOD_IDX
  234. int mMethodIdx;
  235. #endif
  236. BfHotTypeVersion* mSrcTypeVersion;
  237. Array<BfHotDepData*> mReferences;
  238. BfHotMethod* mPrevVersion;
  239. BfHotMethod()
  240. {
  241. #ifdef BF_DBG_HOTMETHOD_IDX
  242. mMethodIdx = -1;
  243. #endif
  244. mDataKind = BfHotDepDataKind_Method;
  245. mSrcTypeVersion = NULL;
  246. mPrevVersion = NULL;
  247. }
  248. void Clear(bool keepDupMethods = false);
  249. ~BfHotMethod();
  250. };
  251. class BfHotDupMethod : public BfHotDepData
  252. {
  253. public:
  254. BfHotMethod* mMethod;
  255. public:
  256. BfHotDupMethod(BfHotMethod* hotMethod)
  257. {
  258. mDataKind = BfHotDepDataKind_DupMethod;
  259. mMethod = hotMethod;
  260. mMethod->mRefCount++;
  261. }
  262. ~BfHotDupMethod()
  263. {
  264. mMethod->Deref();
  265. }
  266. };
  267. class BfHotDevirtualizedMethod : public BfHotDepData
  268. {
  269. public:
  270. BfHotMethod* mMethod;
  271. BfHotDevirtualizedMethod(BfHotMethod* method)
  272. {
  273. mDataKind = BfHotDepDataKind_DevirtualizedMethod;
  274. mMethod = method;
  275. mMethod->mRefCount++;
  276. }
  277. ~BfHotDevirtualizedMethod()
  278. {
  279. mMethod->Deref();
  280. }
  281. };
  282. class BfHotFunctionReference : public BfHotDepData
  283. {
  284. public:
  285. BfHotMethod* mMethod;
  286. BfHotFunctionReference(BfHotMethod* method)
  287. {
  288. mDataKind = BfHotDepDataKind_FunctionPtr;
  289. mMethod = method;
  290. mMethod->mRefCount++;
  291. }
  292. ~BfHotFunctionReference()
  293. {
  294. mMethod->Deref();
  295. }
  296. };
  297. class BfHotInnerMethod : public BfHotDepData
  298. {
  299. public:
  300. BfHotMethod* mMethod;
  301. BfHotInnerMethod(BfHotMethod* method)
  302. {
  303. mDataKind = BfHotDepDataKind_InnerMethod;
  304. mMethod = method;
  305. mMethod->mRefCount++;
  306. }
  307. ~BfHotInnerMethod()
  308. {
  309. mMethod->Deref();
  310. }
  311. };
  312. class BfHotVirtualDeclaration : public BfHotDepData
  313. {
  314. public:
  315. BfHotMethod* mMethod;
  316. BfHotVirtualDeclaration(BfHotMethod* method)
  317. {
  318. mDataKind = BfHotDepDataKind_VirtualDecl;
  319. mMethod = method;
  320. mMethod->mRefCount++;
  321. }
  322. ~BfHotVirtualDeclaration()
  323. {
  324. mMethod->Deref();
  325. }
  326. };
  327. class BfHotDataReferenceBuilder
  328. {
  329. public:
  330. HashSet<BfHotTypeVersion*> mAllocatedData; // Any usage that depends on size or layout
  331. HashSet<BfHotTypeVersion*> mUsedData; // Any usage that depends on size or layout
  332. HashSet<BfHotMethod*> mCalledMethods;
  333. HashSet<BfHotMethod*> mDevirtualizedCalledMethods;
  334. HashSet<BfHotMethod*> mFunctionPtrs;
  335. HashSet<BfHotMethod*> mInnerMethods;
  336. };
  337. class BfDependedType;
  338. class BfTypeInstance;
  339. class BfTypeInstance;
  340. class BfPrimitiveType;
  341. enum BfTypeRebuildFlags
  342. {
  343. BfTypeRebuildFlag_None = 0,
  344. BfTypeRebuildFlag_StaticChange = 1,
  345. BfTypeRebuildFlag_NonStaticChange = 2,
  346. BfTypeRebuildFlag_MethodInlineInternalsChange = 4,
  347. BfTypeRebuildFlag_MethodSignatureChange = 8,
  348. BfTypeRebuildFlag_DeleteQueued = 0x10,
  349. BfTypeRebuildFlag_Deleted = 0x20,
  350. BfTypeRebuildFlag_AddedToWorkList = 0x40,
  351. BfTypeRebuildFlag_AwaitingReference = 0x80,
  352. BfTypeRebuildFlag_SpecializedMethodRebuild = 0x100, // Temporarily set
  353. BfTypeRebuildFlag_SpecializedByAutocompleteMethod = 0x200,
  354. BfTypeRebuildFlag_UnderlyingTypeDeferred = 0x400,
  355. BfTypeRebuildFlag_TypeDataSaved = 0x800,
  356. BfTypeRebuildFlag_InTempPool = 0x1000,
  357. BfTypeRebuildFlag_ResolvingBase = 0x2000
  358. };
  359. class BfTypeDIReplaceCallback;
  360. enum BfTypeDefineState : uint8
  361. {
  362. BfTypeDefineState_Undefined,
  363. BfTypeDefineState_Declared,
  364. BfTypeDefineState_ResolvingBaseType,
  365. BfTypeDefineState_HasInterfaces,
  366. BfTypeDefineState_Defined,
  367. BfTypeDefineState_DefinedAndMethodsSlotted,
  368. };
  369. class BfDelegateInfo
  370. {
  371. public:
  372. Array<BfAstNode*> mDirectAllocNodes;
  373. BfType* mReturnType;
  374. Array<BfType*> mParams;
  375. bool mHasExplicitThis;
  376. public:
  377. BfDelegateInfo()
  378. {
  379. mReturnType = NULL;
  380. mHasExplicitThis = false;
  381. }
  382. ~BfDelegateInfo()
  383. {
  384. for (auto directAllocNode : mDirectAllocNodes)
  385. delete directAllocNode;
  386. }
  387. };
  388. enum BfTypeUsage
  389. {
  390. BfTypeUsage_Unspecified,
  391. BfTypeUsage_Return_Static,
  392. BfTypeUsage_Return_NonStatic,
  393. BfTypeUsage_Parameter,
  394. };
  395. class BfType
  396. {
  397. public:
  398. BfTypeRebuildFlags mRebuildFlags;
  399. BfContext* mContext;
  400. int mTypeId;
  401. int mRevision;
  402. // For Objects, align and size is ref-sized (object*).
  403. // Use mInstSize/mInstAlign for actual data size/align
  404. int mSize;
  405. int16 mAlign;
  406. bool mDirty;
  407. BfTypeDefineState mDefineState;
  408. public:
  409. BfType();
  410. virtual ~BfType();
  411. BfTypeInstance* FindUnderlyingTypeInstance();
  412. virtual BfModule* GetModule();
  413. int GetStride() { return BF_ALIGN(mSize, mAlign); }
  414. bool IsSizeAligned() { return (mSize == 0) || (mSize % mAlign == 0); }
  415. virtual bool NeedsExplicitAlignment() { return !IsSizeAligned(); }
  416. virtual bool IsInstanceOf(BfTypeDef* typeDef) { return false; }
  417. virtual bool HasBeenReferenced() { return mDefineState != BfTypeDefineState_Undefined; }
  418. virtual bool HasTypeFailed() { return false; }
  419. virtual bool IsDataIncomplete() { return mDefineState == BfTypeDefineState_Undefined; }
  420. virtual bool IsIncomplete() { return mDefineState < BfTypeDefineState_Defined; }
  421. virtual bool IsDeleting() { return ((mRebuildFlags & (BfTypeRebuildFlag_Deleted | BfTypeRebuildFlag_DeleteQueued)) != 0); }
  422. virtual bool IsDeclared() { return mDefineState >= BfTypeDefineState_Declared; }
  423. virtual BfDependedType* ToDependedType() { return NULL; }
  424. virtual BfTypeInstance* ToTypeInstance() { return NULL; }
  425. virtual BfTypeInstance* ToGenericTypeInstance() { return NULL; }
  426. virtual BfPrimitiveType* ToPrimitiveType() { return NULL; }
  427. virtual bool IsDependendType() { return false; }
  428. virtual bool IsTypeInstance() { return false; }
  429. virtual bool IsGenericTypeInstance() { return false; }
  430. virtual bool IsUnspecializedType() { return false; }
  431. virtual bool IsReified() { return true; }
  432. virtual bool IsSpecializedType() { return false; }
  433. virtual bool IsSpecializedByAutoCompleteMethod() { return false; }
  434. virtual bool IsUnspecializedTypeVariation() { return false; }
  435. virtual bool IsSplattable() { return false; }
  436. virtual int GetSplatCount() { return 1; }
  437. virtual bool IsVoid() { return false; }
  438. virtual bool IsVoidPtr() { return false; }
  439. virtual bool CanBeValuelessType() { return false; }
  440. virtual bool IsValuelessType() { BF_ASSERT(mSize != -1); BF_ASSERT(mDefineState >= BfTypeDefineState_Defined); return mSize == 0; }
  441. virtual bool IsSelf() { return false; }
  442. virtual bool IsDot() { return false; }
  443. virtual bool IsVar() { return false; }
  444. virtual bool IsLet() { return false; }
  445. virtual bool IsNull(){ return false; }
  446. virtual bool IsNullable() { return false; }
  447. virtual bool IsBoxed() { return false; }
  448. virtual bool IsInterface() { return false; }
  449. virtual bool IsEnum() { return false; }
  450. virtual bool IsPayloadEnum() { return false; }
  451. virtual bool IsTypedPrimitive() { return false; }
  452. virtual bool IsComposite() { return IsStruct(); }
  453. virtual bool IsStruct() { return false; }
  454. virtual bool IsStructPtr() { return false; }
  455. virtual bool IsUnion() { return false; }
  456. virtual bool IsStructOrStructPtr() { return false; }
  457. virtual bool IsObject() { return false; }
  458. virtual bool IsObjectOrStruct() { return false; }
  459. virtual bool IsObjectOrInterface() { return false; }
  460. virtual bool IsString() { return false; }
  461. virtual bool IsSizedArray() { return false; }
  462. virtual bool IsUnknownSizedArray() { return false; }
  463. virtual bool IsArray() { return false; }
  464. virtual bool IsDelegate() { return false; }
  465. virtual bool IsFunction() { return false; }
  466. virtual bool IsDelegateFromTypeRef() { return false; }
  467. virtual bool IsFunctionFromTypeRef() { return false; }
  468. virtual BfDelegateInfo* GetDelegateInfo() { return NULL; }
  469. virtual bool IsValueType() { return false; }
  470. virtual bool IsValueTypeOrValueTypePtr() { return false; }
  471. virtual bool IsWrappableType() { return false; }
  472. virtual bool IsPrimitiveType() { return false; }
  473. virtual bool IsBoolean() { return false; }
  474. virtual bool IsInteger() { return false; }
  475. virtual bool IsIntegral() { return false; }
  476. virtual bool IsIntPtr() { return false; }
  477. virtual bool IsSigned() { return false; }
  478. virtual bool IsSignedInt() { return false; }
  479. virtual bool IsIntUnknown() { return false; }
  480. virtual bool IsChar() { return false; }
  481. virtual bool IsFloat() { return false; }
  482. virtual bool IsPointer() { return false; }
  483. virtual bool IsAllocType() { return false; }
  484. virtual bool IsIntPtrable() { return false; }
  485. virtual bool IsRef() { return false; }
  486. virtual bool IsGenericParam() { return false; }
  487. virtual bool IsClosure() { return false; }
  488. virtual bool IsMethodRef() { return false; }
  489. virtual bool IsTuple() { return false; }
  490. virtual bool IsOnDemand() { return false; }
  491. virtual bool IsTemporary() { return false; }
  492. virtual bool IsModifiedTypeType() { return false; }
  493. virtual bool IsConcreteInterfaceType() { return false; }
  494. virtual bool IsTypeAlias() { return false; }
  495. virtual bool HasPackingHoles() { return false; }
  496. virtual bool IsConstExprValue() { return false; }
  497. virtual bool IsDependentOnUnderlyingType() { return false; }
  498. virtual bool WantsGCMarking() { return false; }
  499. virtual bool GetLoweredType(BfTypeUsage typeUsage, BfTypeCode* outTypeCode = NULL, BfTypeCode* outTypeCode2 = NULL) { return false; }
  500. virtual BfType* GetUnderlyingType() { return NULL; }
  501. virtual bool HasWrappedRepresentation() { return IsWrappableType(); }
  502. virtual bool IsTypeMemberIncluded(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef = NULL, BfModule* module = NULL) { return true; } // May be 'false' only for generic extensions with constraints
  503. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) { return true; }
  504. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) { return true; }
  505. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProjectSet* visibleProjectSet) { return true; }
  506. virtual void ReportMemory(MemReporter* memReporter);
  507. };
  508. // This is explicitly used for generics
  509. typedef SizedArray<BfType*, 2> BfTypeVector;
  510. typedef SizedArray<BfTypeReference*, 2> BfTypeRefVector;
  511. class BfPrimitiveType : public BfType
  512. {
  513. public:
  514. BfTypeDef* mTypeDef;
  515. public:
  516. virtual bool IsPrimitiveType() override { return true; }
  517. virtual bool IsWrappableType() override { return (mTypeDef->mTypeCode >= BfTypeCode_Boolean) && (mTypeDef->mTypeCode <= BfTypeCode_Double); }
  518. virtual BfPrimitiveType* ToPrimitiveType() override { return this; }
  519. //virtual bool IsValueType() override { return mTypeDef->mTypeCode != BfTypeCode_None; }
  520. //virtual bool IsValueTypeOrValueTypePtr() override { return mTypeDef->mTypeCode != BfTypeCode_None; }
  521. virtual bool IsValueType() override { return true; }
  522. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  523. virtual bool IsBoolean() override { return mTypeDef->mTypeCode == BfTypeCode_Boolean; }
  524. virtual bool IsIntegral() override { return (mTypeDef->mTypeCode >= BfTypeCode_Int8) && (mTypeDef->mTypeCode <= BfTypeCode_Char32); }
  525. virtual bool IsInteger() override { return (mTypeDef->mTypeCode >= BfTypeCode_Int8) && (mTypeDef->mTypeCode <= BfTypeCode_UIntUnknown); }
  526. virtual bool IsIntPtr() override { return (mTypeDef->mTypeCode == BfTypeCode_IntPtr) || (mTypeDef->mTypeCode == BfTypeCode_UIntPtr); }
  527. virtual bool IsIntPtrable() override
  528. {
  529. return (mTypeDef->mTypeCode == BfTypeCode_IntPtr) || (mTypeDef->mTypeCode == BfTypeCode_UIntPtr) ||
  530. ((mTypeDef->mSystem->mPtrSize == 8) && ((mTypeDef->mTypeCode == BfTypeCode_Int64) || (mTypeDef->mTypeCode == BfTypeCode_UInt64))) ||
  531. ((mTypeDef->mSystem->mPtrSize == 4) && ((mTypeDef->mTypeCode == BfTypeCode_Int32) || (mTypeDef->mTypeCode == BfTypeCode_UInt32)));
  532. }
  533. virtual bool IsSigned() override { return (mTypeDef->mTypeCode == BfTypeCode_Int8) || (mTypeDef->mTypeCode == BfTypeCode_Int16) ||
  534. (mTypeDef->mTypeCode == BfTypeCode_Int32) || (mTypeDef->mTypeCode == BfTypeCode_Int64) || (mTypeDef->mTypeCode == BfTypeCode_IntPtr) ||
  535. (mTypeDef->mTypeCode == BfTypeCode_IntUnknown) ||
  536. (mTypeDef->mTypeCode == BfTypeCode_Float) || (mTypeDef->mTypeCode == BfTypeCode_Double); }
  537. virtual bool IsSignedInt() override { return (mTypeDef->mTypeCode == BfTypeCode_Int8) || (mTypeDef->mTypeCode == BfTypeCode_Int16) ||
  538. (mTypeDef->mTypeCode == BfTypeCode_Int32) || (mTypeDef->mTypeCode == BfTypeCode_Int64) || (mTypeDef->mTypeCode == BfTypeCode_IntPtr) ||
  539. (mTypeDef->mTypeCode == BfTypeCode_IntUnknown); }
  540. virtual bool IsIntUnknown() override { return (mTypeDef->mTypeCode == BfTypeCode_IntUnknown) || (mTypeDef->mTypeCode == BfTypeCode_UIntUnknown); }
  541. virtual bool IsChar() override { return (mTypeDef->mTypeCode == BfTypeCode_Char8) || (mTypeDef->mTypeCode == BfTypeCode_Char16) || (mTypeDef->mTypeCode == BfTypeCode_Char32); }
  542. virtual bool IsFloat() override { return (mTypeDef->mTypeCode == BfTypeCode_Float) || (mTypeDef->mTypeCode == BfTypeCode_Double); }
  543. virtual bool IsNull() override { return mTypeDef->mTypeCode == BfTypeCode_NullPtr; }
  544. virtual bool IsVoid() override { return mTypeDef->mTypeCode == BfTypeCode_None; }
  545. virtual bool CanBeValuelessType() override { return mTypeDef->mTypeCode == BfTypeCode_None; }
  546. virtual bool IsValuelessType() override { return mTypeDef->mTypeCode == BfTypeCode_None; }
  547. virtual bool IsSelf() override { return mTypeDef->mTypeCode == BfTypeCode_Self; }
  548. virtual bool IsDot() override { return mTypeDef->mTypeCode == BfTypeCode_Dot; }
  549. virtual bool IsVar() override { return mTypeDef->mTypeCode == BfTypeCode_Var; }
  550. virtual bool IsLet() override { return mTypeDef->mTypeCode == BfTypeCode_Let; }
  551. };
  552. class BfTypeInstance;
  553. class BfMethodInstanceGroup;
  554. class BfGenericParamInstance;
  555. class BfGenericMethodParamInstance;
  556. class BfDeferredMethodCallData
  557. {
  558. public:
  559. // void* mPrev, int mMethodId, <method params>
  560. BfIRType mDeferType;
  561. BfIRType mDeferTypePtr;
  562. BfIRMDNode mDeferDIType;
  563. int mAlign;
  564. int mSize;
  565. int64 mMethodId; // Usually matches methodInstance mIdHash unless there's a collision
  566. public:
  567. BfDeferredMethodCallData()
  568. {
  569. mAlign = 0;
  570. mSize = 0;
  571. mMethodId = 0;
  572. }
  573. static int64 GenerateMethodId(BfModule* module, int64 methodId);
  574. };
  575. class BfMethodCustomAttributes
  576. {
  577. public:
  578. BfCustomAttributes* mCustomAttributes;
  579. BfCustomAttributes* mReturnCustomAttributes;
  580. Array<BfCustomAttributes*> mParamCustomAttributes;
  581. public:
  582. BfMethodCustomAttributes()
  583. {
  584. mCustomAttributes = NULL;
  585. mReturnCustomAttributes = NULL;
  586. }
  587. ~BfMethodCustomAttributes();
  588. };
  589. class BfMethodParam
  590. {
  591. public:
  592. BfType* mResolvedType;
  593. int16 mParamDefIdx;
  594. int16 mDelegateParamIdx;
  595. bool mDelegateParamNameCombine; // 'false' means to directly use param name (if we can), otherwise name as <baseParamName>__<delegateParamName>
  596. bool mWasGenericParam;
  597. bool mIsSplat;
  598. bool mReferencedInConstPass;
  599. public:
  600. BfMethodParam()
  601. {
  602. mResolvedType = NULL;
  603. mParamDefIdx = -1;
  604. mDelegateParamIdx = -1;
  605. mDelegateParamNameCombine = false;
  606. mWasGenericParam = false;
  607. mIsSplat = false;
  608. mReferencedInConstPass = false;
  609. }
  610. BfMethodInstance* GetDelegateParamInvoke();
  611. };
  612. enum BfMethodOnDemandKind : int8
  613. {
  614. BfMethodOnDemandKind_NotSet,
  615. BfMethodOnDemandKind_AlwaysInclude,
  616. BfMethodOnDemandKind_NoDecl_AwaitingReference, // Won't be declared until we reference
  617. BfMethodOnDemandKind_Decl_AwaitingReference, // Will be declared immediately but not processed until referenced
  618. BfMethodOnDemandKind_Decl_AwaitingDecl, // Temporary state before we switch to BfMethodOnDemandKind_Decl_AwaitingReference
  619. BfMethodOnDemandKind_InWorkList,
  620. BfMethodOnDemandKind_Referenced
  621. };
  622. enum BfMethodChainType : int8
  623. {
  624. BfMethodChainType_None,
  625. BfMethodChainType_ChainHead,
  626. BfMethodChainType_ChainMember,
  627. BfMethodChainType_ChainSkip,
  628. };
  629. class BfMethodProcessRequest;
  630. class BfLocalMethod;
  631. class BfClosureState;
  632. struct BfClosureCapturedEntry
  633. {
  634. BfType* mType;
  635. String mName;
  636. BfIdentifierNode* mNameNode;
  637. bool mExplicitlyByReference;
  638. int mShadowIdx; // Only relative to matching nameNodes
  639. BfClosureCapturedEntry()
  640. {
  641. mNameNode = NULL;
  642. mType = NULL;
  643. mExplicitlyByReference = false;
  644. mShadowIdx = 0;
  645. }
  646. bool operator<(const BfClosureCapturedEntry& other) const
  647. {
  648. if (mName == "__this")
  649. return true;
  650. else if (other.mName == "__this")
  651. return false;
  652. return mName < other.mName;
  653. }
  654. };
  655. class BfClosureInstanceInfo
  656. {
  657. public:
  658. BfTypeInstance* mThisOverride;
  659. BfLocalMethod* mLocalMethod;
  660. Dictionary<int64, BfMethodDef*> mLocalMethodBindings; // We create binding during capture and use it during mDeferredLocalMethods processing
  661. BfClosureState* mCaptureClosureState;
  662. Array<BfClosureCapturedEntry> mCaptureEntries;
  663. public:
  664. BfClosureInstanceInfo()
  665. {
  666. mThisOverride = NULL;
  667. mLocalMethod = NULL;
  668. mCaptureClosureState = NULL;
  669. }
  670. };
  671. class BfMethodInfoEx
  672. {
  673. public:
  674. StringT<0> mMangledName; // Only populated during hot loading for virtual methods
  675. BfTypeInstance* mExplicitInterface;
  676. BfTypeInstance* mForeignType;
  677. BfClosureInstanceInfo* mClosureInstanceInfo;
  678. Array<BfGenericMethodParamInstance*> mGenericParams;
  679. BfTypeVector mMethodGenericArguments;
  680. Dictionary<int64, BfType*> mGenericTypeBindings;
  681. BfMethodCustomAttributes* mMethodCustomAttributes;
  682. int mMinDependDepth;
  683. BfMethodInfoEx()
  684. {
  685. mExplicitInterface = NULL;
  686. mForeignType = NULL;
  687. mClosureInstanceInfo = NULL;
  688. mMethodCustomAttributes = NULL;
  689. mMinDependDepth = -1;
  690. }
  691. ~BfMethodInfoEx();
  692. };
  693. enum BfImportCallKind
  694. {
  695. BfImportCallKind_None,
  696. BfImportCallKind_GlobalVar,
  697. BfImportCallKind_GlobalVar_Hot // We need to check against NULL in this case
  698. };
  699. class BfMethodInstance
  700. {
  701. public:
  702. int mVirtualTableIdx;
  703. BfIRFunction mIRFunction; // Only valid for owning module
  704. int16 mAppendAllocAlign;
  705. int16 mEndingAppendAllocAlign;
  706. bool mIsUnspecialized:1;
  707. bool mIsUnspecializedVariation:1;
  708. bool mIsReified:1;
  709. bool mHasBeenProcessed:1;
  710. bool mHasFailed:1;
  711. bool mFailedConstraints:1;
  712. bool mMangleWithIdx:1;
  713. bool mHadGenericDelegateParams:1;
  714. bool mIgnoreBody:1;
  715. bool mIsAutocompleteMethod:1;
  716. bool mRequestedByAutocomplete : 1;
  717. bool mIsClosure:1;
  718. bool mMayBeConst:1; // Only used for calcAppend currently
  719. bool mIsForeignMethodDef:1;
  720. bool mAlwaysInline:1;
  721. bool mIsIntrinsic:1;
  722. bool mHasMethodRefType:1;
  723. bool mDisallowCalling:1;
  724. bool mIsInnerOverride:1;
  725. bool mInCEMachine:1;
  726. BfMethodChainType mChainType;
  727. BfCallingConvention mCallingConvention;
  728. BfMethodInstanceGroup* mMethodInstanceGroup;
  729. BfMethodDef* mMethodDef;
  730. BfType* mReturnType;
  731. Array<BfMethodParam> mParams;
  732. Array<BfTypedValue> mDefaultValues;
  733. BfModule* mDeclModule;
  734. BfMethodProcessRequest* mMethodProcessRequest;
  735. BfMethodInfoEx* mMethodInfoEx;
  736. int64 mIdHash; // Collisions are (essentially) benign
  737. BfHotMethod* mHotMethod;
  738. public:
  739. BfMethodInstance()
  740. {
  741. mVirtualTableIdx = -1;
  742. mIsUnspecialized = false;
  743. mIsUnspecializedVariation = false;
  744. mIsReified = true;
  745. mHasBeenProcessed = false;
  746. mHasFailed = false;
  747. mFailedConstraints = false;
  748. mMangleWithIdx = false;
  749. mHadGenericDelegateParams = false;
  750. mIgnoreBody = false;
  751. mIsAutocompleteMethod = false;
  752. mRequestedByAutocomplete = false;
  753. mIsClosure = false;
  754. mMayBeConst = true;
  755. mIsForeignMethodDef = false;
  756. mAlwaysInline = false;
  757. mIsIntrinsic = false;
  758. mHasMethodRefType = false;
  759. mDisallowCalling = false;
  760. mIsInnerOverride = false;
  761. mInCEMachine = false;
  762. mChainType = BfMethodChainType_None;
  763. mCallingConvention = BfCallingConvention_Unspecified;
  764. mMethodInstanceGroup = NULL;
  765. mMethodDef = NULL;
  766. mReturnType = NULL;
  767. mIdHash = 0;
  768. mAppendAllocAlign = -1;
  769. mEndingAppendAllocAlign = -1;
  770. mDeclModule = NULL;
  771. mMethodProcessRequest = NULL;
  772. mMethodInfoEx = NULL;
  773. mHotMethod = NULL;
  774. }
  775. ~BfMethodInstance();
  776. bool IsMixin()
  777. {
  778. return mMethodDef->mMethodType == BfMethodType_Mixin;
  779. }
  780. BfImportKind GetImportKind();
  781. void UndoDeclaration(bool keepIRFunction = false);
  782. BfTypeInstance* GetOwner();
  783. BfModule* GetModule();
  784. bool IsSpecializedGenericMethod();
  785. bool IsSpecializedGenericMethodOrType();
  786. bool IsSpecializedByAutoCompleteMethod();
  787. bool IsOrInUnspecializedVariation();
  788. bool HasExternConstraints();
  789. bool HasThis();
  790. bool IsVirtual();
  791. BfType* GetThisType();
  792. int GetThisIdx();
  793. bool HasExplicitThis();
  794. bool HasParamsArray();
  795. int GetStructRetIdx(bool forceStatic = false);
  796. bool HasSelf();
  797. bool GetLoweredReturnType(BfTypeCode* loweredTypeCode = NULL, BfTypeCode* loweredTypeCode2 = NULL);
  798. bool WantsStructsAttribByVal();
  799. bool IsAutocompleteMethod() { /*return mIdHash == -1;*/ return mIsAutocompleteMethod; }
  800. bool IsSkipCall(bool bypassVirtual = false);
  801. bool IsVarArgs();
  802. bool AlwaysInline();
  803. BfImportCallKind GetImportCallKind();
  804. bool IsTestMethod();
  805. bool AllowsSplatting();
  806. bool AllowsThisSplatting();
  807. int GetParamCount();
  808. int GetImplicitParamCount();
  809. void GetParamName(int paramIdx, StringImpl& name);
  810. String GetParamName(int paramIdx);
  811. BfType* GetParamType(int paramIdx, bool returnUnderlyingParamsType = false);
  812. bool GetParamIsSplat(int paramIdx);
  813. BfParamKind GetParamKind(int paramIdx);
  814. bool WasGenericParam(int paramIdx);
  815. bool IsParamSkipped(int paramIdx); // void/zero-sized
  816. bool IsImplicitCapture(int paramIdx);
  817. BfExpression* GetParamInitializer(int paramIdx);
  818. BfTypeReference* GetParamTypeRef(int paramIdx);
  819. BfIdentifierNode* GetParamNameNode(int paramIdx);
  820. int DbgGetVirtualMethodNum();
  821. void GetIRFunctionInfo(BfModule* module, BfIRType& returnType, SizedArrayImpl<BfIRType>& paramTypes, bool forceStatic = false);
  822. int GetIRFunctionParamCount(BfModule* module);
  823. bool IsExactMatch(BfMethodInstance* other, bool ignoreImplicitParams = false, bool checkThis = false);
  824. bool IsReifiedAndImplemented();
  825. BfMethodInfoEx* GetMethodInfoEx();
  826. BfCustomAttributes* GetCustomAttributes()
  827. {
  828. if ((mMethodInfoEx != NULL) && (mMethodInfoEx->mMethodCustomAttributes != NULL))
  829. return mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes;
  830. return NULL;
  831. }
  832. int GetNumGenericParams()
  833. {
  834. if (mMethodInfoEx != NULL)
  835. return (int)mMethodInfoEx->mGenericParams.size();
  836. return 0;
  837. }
  838. int GetNumGenericArguments()
  839. {
  840. if (mMethodInfoEx != NULL)
  841. return (int)mMethodInfoEx->mMethodGenericArguments.size();
  842. return 0;
  843. }
  844. BfTypeInstance* GetExplicitInterface()
  845. {
  846. if (mMethodInfoEx != NULL)
  847. return mMethodInfoEx->mExplicitInterface;
  848. return NULL;
  849. }
  850. BfTypeInstance* GetForeignType()
  851. {
  852. if (mMethodInfoEx != NULL)
  853. return mMethodInfoEx->mForeignType;
  854. return NULL;
  855. }
  856. void ReportMemory(MemReporter* memReporter);
  857. };
  858. class BfOperatorInfo
  859. {
  860. public:
  861. BfMethodDef* mMethodDef;
  862. BfType* mReturnType;
  863. BfType* mLHSType;
  864. BfType* mRHSType;
  865. BfOperatorInfo()
  866. {
  867. mMethodDef = NULL;
  868. mReturnType = NULL;
  869. mLHSType = NULL;
  870. mRHSType = NULL;
  871. }
  872. };
  873. class BfDllImportEntry
  874. {
  875. public:
  876. BfMethodInstance* mMethodInstance;
  877. BfIRValue mFuncVar;
  878. //BfIRFunctionType mFuncType;
  879. };
  880. class BfModuleMethodInstance
  881. {
  882. public:
  883. BfMethodInstance* mMethodInstance;
  884. BfIRValue mFunc;
  885. public:
  886. BfModuleMethodInstance()
  887. {
  888. mMethodInstance = NULL;
  889. }
  890. BfModuleMethodInstance(BfMethodInstance* methodInstance);
  891. BfModuleMethodInstance(BfMethodInstance* methodInstance, BfIRValue func) : mFunc(func)
  892. {
  893. mMethodInstance = methodInstance;
  894. }
  895. operator bool() const
  896. {
  897. return mMethodInstance != NULL;
  898. }
  899. };
  900. // The way these work is a bit nuanced
  901. // When we use these as keys, we allow collisions between genericParams from different type and
  902. // method instances. That's okay, as the only constraint there is that they generate valid
  903. // generic-pass methods, which will all be the same. Within the SCOPE of a given type and
  904. // method instance, however, we cannot have collision between types (such as a List<T> and List<T2>)
  905. // because that can cause false equalties in the type checker
  906. class BfGenericParamType : public BfType
  907. {
  908. public:
  909. BfGenericParamKind mGenericParamKind;
  910. int mGenericParamIdx;
  911. public:
  912. bool IsGenericParam() override { return true; }
  913. virtual bool IsUnspecializedType() override { return true; }
  914. virtual bool IsReified() override { return false; }
  915. };
  916. // This just captures rettype(T)/nullable(T) since it can't be resolved directly
  917. class BfModifiedTypeType : public BfType
  918. {
  919. public:
  920. BfToken mModifiedKind;
  921. BfType* mElementType;
  922. virtual bool IsModifiedTypeType() override { return true; }
  923. virtual bool CanBeValuelessType() override { return true; }
  924. virtual bool IsValuelessType() override { return true; }
  925. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  926. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedType(); }
  927. virtual bool IsReified() override { return mElementType->IsReified(); }
  928. virtual bool IsDependentOnUnderlyingType() override { return true; }
  929. virtual bool IsAllocType() override { return mModifiedKind == BfToken_AllocType; }
  930. virtual BfType* GetUnderlyingType() override { return mElementType; }
  931. };
  932. class BfGenericOperatorConstraintInstance
  933. {
  934. public:
  935. BfType* mLeftType;
  936. BfBinaryOp mBinaryOp;
  937. BfUnaryOp mUnaryOp;
  938. BfToken mCastToken;
  939. BfType* mRightType;
  940. public:
  941. BfGenericOperatorConstraintInstance()
  942. {
  943. mLeftType = NULL;
  944. mBinaryOp = BfBinaryOp_None;
  945. mUnaryOp = BfUnaryOp_None;
  946. mCastToken = BfToken_None;
  947. mRightType = NULL;
  948. }
  949. bool operator==(const BfGenericOperatorConstraintInstance& other) const
  950. {
  951. return
  952. (mLeftType == other.mLeftType) &&
  953. (mBinaryOp == other.mBinaryOp) &&
  954. (mUnaryOp == other.mUnaryOp) &&
  955. (mCastToken == other.mCastToken) &&
  956. (mRightType == other.mRightType);
  957. }
  958. };
  959. class BfGenericParamInstance
  960. {
  961. public:
  962. int mGenericParamFlags;
  963. BfType* mExternType;
  964. Array<BfTypeInstance*> mInterfaceConstraints;
  965. Array<BfGenericOperatorConstraintInstance> mOperatorConstraints;
  966. BfType* mTypeConstraint;
  967. int mRefCount;
  968. BfGenericParamInstance()
  969. {
  970. mExternType = NULL;
  971. mGenericParamFlags = 0;
  972. mTypeConstraint = NULL;
  973. mRefCount = 1;
  974. }
  975. void Release()
  976. {
  977. if (--mRefCount == 0)
  978. delete this;
  979. }
  980. virtual ~BfGenericParamInstance()
  981. {
  982. }
  983. virtual BfConstraintDef* GetConstraintDef() = 0;
  984. virtual BfGenericParamDef* GetGenericParamDef() = 0;
  985. virtual BfExternalConstraintDef* GetExternConstraintDef() = 0;
  986. virtual String GetName() = 0;
  987. };
  988. class BfGenericTypeParamInstance : public BfGenericParamInstance
  989. {
  990. public:
  991. BfTypeDef* mTypeDef;
  992. int mGenericIdx;
  993. public:
  994. BfGenericTypeParamInstance(BfTypeDef* typeDef, int genericIdx)
  995. {
  996. mTypeDef = typeDef;
  997. mGenericIdx = genericIdx;
  998. mGenericParamFlags = GetConstraintDef()->mGenericParamFlags;
  999. mTypeConstraint = NULL;
  1000. }
  1001. BfGenericTypeParamInstance* AddRef()
  1002. {
  1003. mRefCount++;
  1004. return this;
  1005. }
  1006. virtual BfConstraintDef* GetConstraintDef() override
  1007. {
  1008. if (mGenericIdx < (int)mTypeDef->mGenericParamDefs.size())
  1009. return mTypeDef->mGenericParamDefs[mGenericIdx];
  1010. return &mTypeDef->mExternalConstraints[mGenericIdx - (int)mTypeDef->mGenericParamDefs.size()];
  1011. }
  1012. virtual BfGenericParamDef* GetGenericParamDef() override
  1013. {
  1014. if (mGenericIdx < (int)mTypeDef->mGenericParamDefs.size())
  1015. return mTypeDef->mGenericParamDefs[mGenericIdx];
  1016. return NULL;
  1017. }
  1018. virtual BfExternalConstraintDef* GetExternConstraintDef() override
  1019. {
  1020. if (mGenericIdx < (int)mTypeDef->mGenericParamDefs.size())
  1021. return NULL;
  1022. return &mTypeDef->mExternalConstraints[mGenericIdx - (int)mTypeDef->mGenericParamDefs.size()];
  1023. }
  1024. virtual String GetName() override
  1025. {
  1026. if (mGenericIdx < (int)mTypeDef->mGenericParamDefs.size())
  1027. return mTypeDef->mGenericParamDefs[mGenericIdx]->mName;
  1028. return mTypeDef->mExternalConstraints[mGenericIdx - (int)mTypeDef->mGenericParamDefs.size()].mTypeRef->ToString();
  1029. }
  1030. };
  1031. class BfGenericMethodParamInstance : public BfGenericParamInstance
  1032. {
  1033. public:
  1034. BfMethodDef* mMethodDef;
  1035. int mGenericIdx;
  1036. public:
  1037. BfGenericMethodParamInstance(BfMethodDef* methodDef, int genericIdx)
  1038. {
  1039. mMethodDef = methodDef;
  1040. mGenericIdx = genericIdx;
  1041. mGenericParamFlags = GetConstraintDef()->mGenericParamFlags;
  1042. mTypeConstraint = NULL;
  1043. }
  1044. BfGenericMethodParamInstance* AddRef()
  1045. {
  1046. mRefCount++;
  1047. return this;
  1048. }
  1049. virtual BfConstraintDef* GetConstraintDef() override
  1050. {
  1051. if (mGenericIdx < (int)mMethodDef->mGenericParams.size())
  1052. return mMethodDef->mGenericParams[mGenericIdx];
  1053. return &mMethodDef->mExternalConstraints[mGenericIdx - (int)mMethodDef->mGenericParams.size()];
  1054. }
  1055. virtual BfGenericParamDef* GetGenericParamDef() override
  1056. {
  1057. if (mGenericIdx < (int)mMethodDef->mGenericParams.size())
  1058. return mMethodDef->mGenericParams[mGenericIdx];
  1059. return NULL;
  1060. }
  1061. virtual BfExternalConstraintDef* GetExternConstraintDef() override
  1062. {
  1063. if (mGenericIdx < (int)mMethodDef->mGenericParams.size())
  1064. return NULL;
  1065. return &mMethodDef->mExternalConstraints[mGenericIdx - (int)mMethodDef->mGenericParams.size()];
  1066. }
  1067. virtual String GetName() override
  1068. {
  1069. if (mGenericIdx < (int)mMethodDef->mGenericParams.size())
  1070. return mMethodDef->mGenericParams[mGenericIdx]->mName;
  1071. return mMethodDef->mExternalConstraints[mGenericIdx - (int)mMethodDef->mGenericParams.size()].mTypeRef->ToString();
  1072. }
  1073. };
  1074. #define BF_VALCOMP(val) if (val != 0) return val
  1075. struct BfTypeVectorHash
  1076. {
  1077. size_t operator()(const BfTypeVector& val) const;
  1078. };
  1079. struct BfTypeVectorEquals
  1080. {
  1081. bool operator()(const BfTypeVector& lhs, const BfTypeVector& rhs) const;
  1082. };
  1083. // Specialized for Class<T> but not Method<T>
  1084. class BfMethodInstanceGroup
  1085. {
  1086. public:
  1087. BfTypeInstance* mOwner;
  1088. BfMethodInstance* mDefault;
  1089. typedef Dictionary<BfTypeVector, BfMethodInstance*> MapType;
  1090. MapType* mMethodSpecializationMap;
  1091. int mMethodIdx;
  1092. int mRefCount; // External references from BfMethodRefType
  1093. BfMethodOnDemandKind mOnDemandKind;
  1094. bool mExplicitlyReflected;
  1095. public:
  1096. BfMethodInstanceGroup()
  1097. {
  1098. mOwner = NULL;
  1099. mDefault = NULL;
  1100. mMethodSpecializationMap = NULL;
  1101. mMethodIdx = -1;
  1102. mOnDemandKind = BfMethodOnDemandKind_NotSet;
  1103. mRefCount = 0;
  1104. mExplicitlyReflected = false;
  1105. }
  1106. ~BfMethodInstanceGroup();
  1107. BfMethodInstanceGroup(BfMethodInstanceGroup&& prev) noexcept
  1108. {
  1109. mOwner = prev.mOwner;
  1110. mDefault = prev.mDefault;
  1111. mMethodSpecializationMap = prev.mMethodSpecializationMap;
  1112. mMethodIdx = prev.mMethodIdx;
  1113. mRefCount = prev.mRefCount;
  1114. mOnDemandKind = prev.mOnDemandKind;
  1115. if (mDefault != NULL)
  1116. mDefault->mMethodInstanceGroup = this;
  1117. if (mMethodSpecializationMap != NULL)
  1118. {
  1119. for (auto& pair : *mMethodSpecializationMap)
  1120. pair.mValue->mMethodInstanceGroup = this;
  1121. }
  1122. prev.mRefCount = 0;
  1123. prev.mDefault = NULL;
  1124. prev.mMethodSpecializationMap = NULL;
  1125. }
  1126. bool IsImplemented()
  1127. {
  1128. return (mOnDemandKind == BfMethodOnDemandKind_AlwaysInclude) ||
  1129. (mOnDemandKind == BfMethodOnDemandKind_Referenced) ||
  1130. (mOnDemandKind == BfMethodOnDemandKind_InWorkList);
  1131. }
  1132. };
  1133. class BfFieldInstance
  1134. {
  1135. public:
  1136. BfTypeInstance* mOwner;
  1137. BfType* mResolvedType;
  1138. BfCustomAttributes* mCustomAttributes;
  1139. int mConstIdx;
  1140. int mFieldIdx;
  1141. int mDataIdx; // mFieldIdx includes statics & consts, mDataIdx does not
  1142. int mMergedDataIdx; // Like mDataIdx but contains base class's data
  1143. int mDataOffset;
  1144. int mDataSize;
  1145. bool mFieldIncluded;
  1146. bool mIsEnumPayloadCase;
  1147. bool mIsThreadLocal;
  1148. bool mIsInferredType;
  1149. int mLastRevisionReferenced;
  1150. public:
  1151. BfFieldDef* GetFieldDef();
  1152. BfFieldInstance(const BfFieldInstance& copyFrom)
  1153. {
  1154. BF_ASSERT(copyFrom.mCustomAttributes == NULL);
  1155. mOwner = copyFrom.mOwner;
  1156. mResolvedType = copyFrom.mResolvedType;
  1157. mCustomAttributes = copyFrom.mCustomAttributes;
  1158. mConstIdx = copyFrom.mConstIdx;
  1159. mFieldIdx = copyFrom.mFieldIdx;
  1160. mDataIdx = copyFrom.mDataIdx;
  1161. mMergedDataIdx = copyFrom.mMergedDataIdx;
  1162. mDataOffset = copyFrom.mDataOffset;
  1163. mDataSize = copyFrom.mDataSize;
  1164. mFieldIncluded = copyFrom.mFieldIncluded;
  1165. mIsEnumPayloadCase = copyFrom.mIsEnumPayloadCase;
  1166. mIsThreadLocal = copyFrom.mIsThreadLocal;
  1167. mIsInferredType = copyFrom.mIsInferredType;
  1168. mLastRevisionReferenced = copyFrom.mLastRevisionReferenced;
  1169. }
  1170. BfFieldInstance()
  1171. {
  1172. mFieldIdx = -1;
  1173. mOwner = NULL;
  1174. mResolvedType = NULL;
  1175. mIsEnumPayloadCase = false;
  1176. mCustomAttributes = NULL;
  1177. mConstIdx = -1;
  1178. mDataIdx = -1;
  1179. mMergedDataIdx = -1;
  1180. mDataOffset = -1;
  1181. mDataSize = 0;
  1182. mFieldIncluded = true;
  1183. mIsThreadLocal = false;
  1184. mIsInferredType = false;
  1185. mLastRevisionReferenced = -1;
  1186. }
  1187. ~BfFieldInstance();
  1188. BfType* GetResolvedType();
  1189. void SetResolvedType(BfType* type);
  1190. void GetDataRange(int& dataIdx, int& dataCount);
  1191. };
  1192. enum BfMethodRefKind
  1193. {
  1194. BfMethodRefKind_VExtMarker = -1,
  1195. BfMethodRefKind_AmbiguousRef = -2
  1196. };
  1197. class BfNonGenericMethodRef
  1198. {
  1199. public:
  1200. BfTypeInstance* mTypeInstance;
  1201. union
  1202. {
  1203. int mMethodNum;
  1204. BfMethodRefKind mKind;
  1205. };
  1206. int mSignatureHash;
  1207. BfNonGenericMethodRef()
  1208. {
  1209. mTypeInstance = NULL;
  1210. mMethodNum = 0;
  1211. mSignatureHash = 0;
  1212. }
  1213. BfNonGenericMethodRef(BfMethodInstance* methodInstance);
  1214. operator BfMethodInstance*() const;
  1215. bool IsNull() { return mTypeInstance == NULL; };
  1216. BfMethodInstance* operator->() const;
  1217. BfNonGenericMethodRef& operator=(BfMethodInstance* methodInstance);
  1218. bool operator==(const BfNonGenericMethodRef& methodRef) const;
  1219. bool operator==(BfMethodInstance* methodInstance) const;
  1220. struct Hash
  1221. {
  1222. size_t operator()(const BfNonGenericMethodRef& val) const;
  1223. };
  1224. struct Equals
  1225. {
  1226. bool operator()(const BfNonGenericMethodRef& lhs, const BfNonGenericMethodRef& rhs) const
  1227. {
  1228. return lhs == rhs;
  1229. }
  1230. };
  1231. };
  1232. enum BfMethodRefFlags : uint8
  1233. {
  1234. BfMethodRefFlag_None = 0,
  1235. BfMethodRefFlag_AlwaysInclude = 1
  1236. };
  1237. class BfMethodRef
  1238. {
  1239. public:
  1240. BfTypeInstance* mTypeInstance;
  1241. union
  1242. {
  1243. int mMethodNum;
  1244. BfMethodRefKind mKind;
  1245. };
  1246. Array<BfType*> mMethodGenericArguments;
  1247. int mSignatureHash;
  1248. BfMethodRefFlags mMethodRefFlags;
  1249. BfMethodRef()
  1250. {
  1251. mTypeInstance = NULL;
  1252. mMethodNum = 0;
  1253. mSignatureHash = 0;
  1254. mMethodRefFlags = BfMethodRefFlag_None;
  1255. }
  1256. BfMethodRef(BfMethodInstance* methodInstance);
  1257. operator BfMethodInstance*() const;
  1258. bool IsNull() { return mTypeInstance == NULL; };
  1259. BfMethodInstance* operator->() const;
  1260. BfMethodRef& operator=(BfMethodInstance* methodInstance);
  1261. bool operator==(const BfMethodRef& methodRef) const;
  1262. bool operator==(BfMethodInstance* methodInstance) const;
  1263. struct Hash
  1264. {
  1265. size_t operator()(const BfMethodRef& val) const;
  1266. };
  1267. struct Equals
  1268. {
  1269. bool operator()(const BfMethodRef& lhs, const BfMethodRef& rhs) const
  1270. {
  1271. return lhs == rhs;
  1272. }
  1273. };
  1274. };
  1275. class BfFieldRef
  1276. {
  1277. public:
  1278. BfTypeInstance* mTypeInstance;
  1279. int mFieldIdx;
  1280. public:
  1281. BfFieldRef()
  1282. {
  1283. mTypeInstance = NULL;
  1284. mFieldIdx = 0;
  1285. }
  1286. BfFieldRef(BfTypeInstance* typeInst, BfFieldDef* fieldDef);
  1287. BfFieldRef(BfFieldInstance* fieldInstance);
  1288. bool operator==(const BfFieldRef& other) const
  1289. {
  1290. return (mTypeInstance == other.mTypeInstance) && (mFieldIdx == other.mFieldIdx);
  1291. }
  1292. operator BfFieldInstance*() const;
  1293. operator BfFieldDef*() const;
  1294. operator BfPropertyDef*() const;
  1295. };
  1296. class BfPropertyRef
  1297. {
  1298. public:
  1299. BfTypeInstance* mTypeInstance;
  1300. int mPropIdx;
  1301. public:
  1302. BfPropertyRef()
  1303. {
  1304. mTypeInstance = NULL;
  1305. mPropIdx = 0;
  1306. }
  1307. BfPropertyRef(BfTypeInstance* typeInst, BfPropertyDef* propDef);
  1308. operator BfPropertyDef*() const;
  1309. };
  1310. class BfTypeInterfaceEntry
  1311. {
  1312. public:
  1313. BfTypeDef* mDeclaringType;
  1314. BfTypeInstance* mInterfaceType;
  1315. int mStartInterfaceTableIdx;
  1316. int mStartVirtualIdx; // Relative to start of virtual interface methods
  1317. bool mIsRedeclared;
  1318. };
  1319. class BfTypeInterfaceMethodEntry
  1320. {
  1321. public:
  1322. BfNonGenericMethodRef mMethodRef;
  1323. //int mVirtualIdx;
  1324. public:
  1325. BfTypeInterfaceMethodEntry()
  1326. {
  1327. //mVirtualIdx = -1;
  1328. }
  1329. };
  1330. enum BfAttributeTargets : int32
  1331. {
  1332. BfAttributeTargets_SkipValidate = -1,
  1333. BfAttributeTargets_None = 0,
  1334. BfAttributeTargets_Assembly = 0x0001,
  1335. BfAttributeTargets_Module = 0x0002,
  1336. BfAttributeTargets_Class = 0x0004,
  1337. BfAttributeTargets_Struct = 0x0008,
  1338. BfAttributeTargets_Enum = 0x0010,
  1339. BfAttributeTargets_Constructor = 0x0020,
  1340. BfAttributeTargets_Method = 0x0040,
  1341. BfAttributeTargets_Property = 0x0080,
  1342. BfAttributeTargets_Field = 0x0100,
  1343. BfAttributeTargets_StaticField = 0x0200,
  1344. BfAttributeTargets_Interface = 0x0400,
  1345. BfAttributeTargets_Parameter = 0x0800,
  1346. BfAttributeTargets_Delegate = 0x1000,
  1347. BfAttributeTargets_Function = 0x2000,
  1348. BfAttributeTargets_ReturnValue = 0x4000,
  1349. BfAttributeTargets_GenericParameter = 0x8000,
  1350. BfAttributeTargets_Invocation = 0x10000,
  1351. BfAttributeTargets_MemberAccess = 0x20000,
  1352. BfAttributeTargets_Alloc = 0x40000,
  1353. BfAttributeTargets_Delete = 0x80000,
  1354. BfAttributeTargets_Alias = 0x100000,
  1355. BfAttributeTargets_Block = 0x200000,
  1356. BfAttributeTargets_All = 0x3FFFFF
  1357. };
  1358. class BfAttributeData
  1359. {
  1360. public:
  1361. BfAttributeTargets mAttributeTargets;
  1362. bool mInherited;
  1363. bool mAllowMultiple;
  1364. public:
  1365. BfAttributeData()
  1366. {
  1367. mAttributeTargets = BfAttributeTargets_All;
  1368. mInherited = true;
  1369. mAllowMultiple = false;
  1370. }
  1371. };
  1372. class BfHotTypeData
  1373. {
  1374. public:
  1375. Array<BfHotTypeVersion*> mTypeVersions;
  1376. Array<BfHotTypeDataEntry> mVTableEntries; // Doesn't fill in until we rebuild or delete type
  1377. int mVTableOrigLength;
  1378. int mOrigInterfaceMethodsLength;
  1379. bool mPendingDataChange;
  1380. bool mHadDataChange; // True if we have EVER had a hot data change
  1381. public:
  1382. BfHotTypeData()
  1383. {
  1384. mVTableOrigLength = -1;
  1385. mOrigInterfaceMethodsLength = -1;
  1386. mPendingDataChange = false;
  1387. mHadDataChange = false;
  1388. }
  1389. ~BfHotTypeData();
  1390. BfHotTypeVersion* GetTypeVersion(int hotCommitedIdx);
  1391. BfHotTypeVersion* GetLatestVersion();
  1392. BfHotTypeVersion* GetLatestVersionHead(); // The oldest version that has the same data
  1393. void ClearVersionsAfter(int hotIdx);
  1394. };
  1395. struct BfTypeLookupEntry
  1396. {
  1397. BfAtomComposite mName;
  1398. int mNumGenericParams;
  1399. uint32 mAtomUpdateIdx;
  1400. BfTypeDef* mUseTypeDef;
  1401. bool operator==(const BfTypeLookupEntry& rhs) const
  1402. {
  1403. return (mName == rhs.mName) &&
  1404. (mNumGenericParams == rhs.mNumGenericParams) &&
  1405. (mUseTypeDef == rhs.mUseTypeDef);
  1406. }
  1407. };
  1408. NS_BF_END;
  1409. namespace std
  1410. {
  1411. template<>
  1412. struct hash<Beefy::BfTypeLookupEntry>
  1413. {
  1414. size_t operator()(const Beefy::BfTypeLookupEntry& entry) const
  1415. {
  1416. int curHash = 0;
  1417. for (int i = 0; i < entry.mName.mSize; i++)
  1418. curHash = ((curHash ^ (int)(intptr)entry.mName.mParts[i]) << 5) - curHash;
  1419. curHash = ((curHash ^ entry.mNumGenericParams) << 5) - curHash;
  1420. curHash ^= (intptr)entry.mUseTypeDef;
  1421. return curHash;
  1422. }
  1423. };
  1424. }
  1425. NS_BF_BEGIN;
  1426. struct BfTypeLookupResult
  1427. {
  1428. BfTypeDef* mTypeDef;
  1429. bool mForceLookup;
  1430. BfTypeLookupResult()
  1431. {
  1432. mTypeDef = NULL;
  1433. mForceLookup = false;
  1434. }
  1435. };
  1436. class BfDependedType : public BfType
  1437. {
  1438. public:
  1439. BfDependencyMap mDependencyMap; // This is a list of types that depend on THIS type
  1440. public:
  1441. virtual bool IsDependendType() override { return true; }
  1442. virtual BfDependedType* ToDependedType() override { return this; }
  1443. };
  1444. struct BfVirtualMethodEntry
  1445. {
  1446. BfNonGenericMethodRef mDeclaringMethod;
  1447. BfNonGenericMethodRef mImplementingMethod;
  1448. };
  1449. struct BfSpecializedMethodRefInfo
  1450. {
  1451. bool mHasReifiedRef;
  1452. BfSpecializedMethodRefInfo()
  1453. {
  1454. mHasReifiedRef = false;
  1455. }
  1456. };
  1457. class BfStaticSearch
  1458. {
  1459. public:
  1460. Array<BfTypeInstance*> mStaticTypes;
  1461. };
  1462. class BfInternalAccessSet
  1463. {
  1464. public:
  1465. Array<BfTypeInstance*> mTypes;
  1466. Array<BfAtomComposite> mNamespaces;
  1467. };
  1468. class BfTypeInfoEx
  1469. {
  1470. public:
  1471. BfType* mUnderlyingType;
  1472. int64 mMinValue;
  1473. int64 mMaxValue;
  1474. BfTypeInfoEx()
  1475. {
  1476. mUnderlyingType = NULL;
  1477. mMinValue = 0;
  1478. mMaxValue = 0;
  1479. }
  1480. };
  1481. class BfGenericExtensionEntry
  1482. {
  1483. public:
  1484. Array<BfGenericTypeParamInstance*> mGenericParams;
  1485. bool mConstraintsPassed;
  1486. public:
  1487. BfGenericExtensionEntry(BfGenericExtensionEntry&& prev) :
  1488. mGenericParams(std::move(prev.mGenericParams)),
  1489. mConstraintsPassed(prev.mConstraintsPassed)
  1490. {
  1491. }
  1492. BfGenericExtensionEntry()
  1493. {
  1494. mConstraintsPassed = true;
  1495. }
  1496. ~BfGenericExtensionEntry();
  1497. };
  1498. class BfGenericExtensionInfo
  1499. {
  1500. public:
  1501. Dictionary<BfTypeDef*, BfGenericExtensionEntry> mExtensionMap;
  1502. };
  1503. // Note on nested generic types- mGenericParams is the accumulation of all generic params from outer to inner, so
  1504. // class ClassA<T> { class ClassB<T2> {} } will create a ClassA.ClassB<T, T2>
  1505. class BfGenericTypeInfo
  1506. {
  1507. public:
  1508. typedef Array<BfGenericTypeParamInstance*> GenericParamsVector;
  1509. Array<BfTypeReference*> mTypeGenericArgumentRefs;
  1510. BfTypeVector mTypeGenericArguments;
  1511. GenericParamsVector mGenericParams;
  1512. BfGenericExtensionInfo* mGenericExtensionInfo;
  1513. bool mIsUnspecialized;
  1514. bool mIsUnspecializedVariation;
  1515. bool mValidatedGenericConstraints;
  1516. bool mHadValidateErrors;
  1517. bool mInitializedGenericParams;
  1518. bool mFinishedGenericParams;
  1519. Array<BfProject*> mProjectsReferenced; // Generic methods that only refer to these projects don't need a specialized extension
  1520. public:
  1521. BfGenericTypeInfo()
  1522. {
  1523. mGenericExtensionInfo = NULL;
  1524. mHadValidateErrors = false;
  1525. mIsUnspecialized = false;
  1526. mIsUnspecializedVariation = false;
  1527. mValidatedGenericConstraints = false;
  1528. mInitializedGenericParams = false;
  1529. mFinishedGenericParams = false;
  1530. }
  1531. ~BfGenericTypeInfo();
  1532. void ReportMemory(MemReporter* memReporter);
  1533. };
  1534. // Instance of struct or class
  1535. class BfTypeInstance : public BfDependedType
  1536. {
  1537. public:
  1538. int mSignatureRevision;
  1539. int mLastNonGenericUsedRevision;
  1540. int mInheritanceId;
  1541. int mInheritanceCount;
  1542. BfModule* mModule;
  1543. BfTypeDef* mTypeDef;
  1544. BfTypeInstance* mBaseType;
  1545. BfCustomAttributes* mCustomAttributes;
  1546. BfAttributeData* mAttributeData;
  1547. BfTypeInfoEx* mTypeInfoEx;
  1548. BfGenericTypeInfo* mGenericTypeInfo;
  1549. Array<BfTypeInterfaceEntry> mInterfaces;
  1550. Array<BfTypeInterfaceMethodEntry> mInterfaceMethodTable;
  1551. Array<BfMethodInstanceGroup> mMethodInstanceGroups;
  1552. Array<BfOperatorInfo*> mOperatorInfo;
  1553. Array<BfVirtualMethodEntry> mVirtualMethodTable;
  1554. BfHotTypeData* mHotTypeData;
  1555. int mVirtualMethodTableSize; // With hot reloading, mVirtualMethodTableSize can be larger than mInterfaceMethodTable (live vtable versioning)
  1556. Array<BfFieldInstance> mFieldInstances;
  1557. Array<BfMethodInstance*> mInternalMethods;
  1558. Dictionary<BfTypeDef*, BfStaticSearch> mStaticSearchMap;
  1559. Dictionary<BfTypeDef*, BfInternalAccessSet> mInternalAccessMap;
  1560. bool mHasStaticInitMethod;
  1561. bool mHasStaticDtorMethod;
  1562. bool mHasStaticMarkMethod;
  1563. bool mHasTLSFindMethod;
  1564. BfIRConstHolder* mConstHolder;
  1565. Dictionary<BfMethodRef, BfSpecializedMethodRefInfo> mSpecializedMethodReferences; // Includes both specialized methods and OnDemand methods
  1566. // We store lookup results so we can rebuild this type if a name resolves differently due to a new type being created (ie: a type name found first in
  1567. Dictionary<BfTypeLookupEntry, BfTypeLookupResult> mLookupResults;
  1568. int mTypeOptionsIdx;
  1569. int mMergedFieldDataCount;
  1570. int mInstAlign;
  1571. int mInstSize;
  1572. int16 mInheritDepth;
  1573. int16 mSlotNum;
  1574. bool mHasBeenInstantiated;
  1575. bool mIncludeAllMethods;
  1576. bool mIsReified;
  1577. bool mIsTypedPrimitive;
  1578. bool mIsCRepr;
  1579. bool mIsUnion;
  1580. bool mIsPacked;
  1581. bool mIsSplattable;
  1582. bool mHasUnderlyingArray;
  1583. bool mTypeIncomplete;
  1584. bool mTypeFailed;
  1585. bool mTypeWarned;
  1586. bool mResolvingVarField;
  1587. bool mResolvingConstField;
  1588. bool mSkipTypeProtectionChecks;
  1589. bool mNeedsMethodProcessing;
  1590. bool mBaseTypeMayBeIncomplete;
  1591. bool mHasParameterizedBase; // Generic, array, etc
  1592. bool mIsFinishingType;
  1593. bool mHasPackingHoles;
  1594. bool mWantsGCMarking;
  1595. bool mHasDeclError;
  1596. public:
  1597. BfTypeInstance()
  1598. {
  1599. mModule = NULL;
  1600. mSignatureRevision = -1;
  1601. mLastNonGenericUsedRevision = -1;
  1602. mInheritanceId = 0;
  1603. mInheritanceCount = 0;
  1604. mTypeDef = NULL;
  1605. mRevision = -1;
  1606. mIsReified = true;
  1607. mIsSplattable = false;
  1608. mHasUnderlyingArray = false;
  1609. mIsPacked = false;
  1610. mBaseType = NULL;
  1611. mCustomAttributes = NULL;
  1612. mAttributeData = NULL;
  1613. mTypeInfoEx = NULL;
  1614. mGenericTypeInfo = NULL;
  1615. //mClassVData = NULL;
  1616. mVirtualMethodTableSize = 0;
  1617. mHotTypeData = NULL;
  1618. mHasStaticInitMethod = false;
  1619. mHasStaticMarkMethod = false;
  1620. mHasStaticDtorMethod = false;
  1621. mHasTLSFindMethod = false;
  1622. mSlotNum = -1;
  1623. mTypeOptionsIdx = -2; // -2 = not checked, -1 = none
  1624. mInstSize = -1;
  1625. mInstAlign = -1;
  1626. mInheritDepth = 0;
  1627. mIsTypedPrimitive = false;
  1628. mTypeIncomplete = true;
  1629. mIsCRepr = false;
  1630. mIsUnion = false;
  1631. mTypeFailed = false;
  1632. mTypeWarned = false;
  1633. mResolvingVarField = false;
  1634. mSkipTypeProtectionChecks = false;
  1635. mNeedsMethodProcessing = false;
  1636. mBaseTypeMayBeIncomplete = false;
  1637. mIsFinishingType = false;
  1638. mResolvingConstField = false;
  1639. mHasPackingHoles = false;
  1640. mHasBeenInstantiated = false;
  1641. mIncludeAllMethods = false;
  1642. mWantsGCMarking = false;
  1643. mHasParameterizedBase = false;
  1644. mHasDeclError = false;
  1645. mMergedFieldDataCount = 0;
  1646. mConstHolder = NULL;
  1647. }
  1648. ~BfTypeInstance();
  1649. void ReleaseData();
  1650. virtual bool IsInstanceOf(BfTypeDef* typeDef) override { return typeDef == mTypeDef; }
  1651. virtual BfModule* GetModule() override { return mModule; }
  1652. virtual BfTypeInstance* ToTypeInstance() override { return this; }
  1653. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1654. virtual BfPrimitiveType* ToPrimitiveType() override { return GetUnderlyingType()->ToPrimitiveType(); }
  1655. virtual bool HasWrappedRepresentation() override { return IsTypedPrimitive(); }
  1656. int GetEndingInstanceAlignment() { if (mInstSize % mInstAlign == 0) return mInstAlign; return mInstSize % mInstAlign; }
  1657. virtual bool HasTypeFailed() override { return mTypeFailed; }
  1658. virtual bool IsReified() override { return mIsReified; }
  1659. virtual bool NeedsExplicitAlignment() override { return !IsSizeAligned() || mIsPacked; }
  1660. virtual bool IsDataIncomplete() override { return ((mTypeIncomplete) || (mBaseTypeMayBeIncomplete)) && (!mNeedsMethodProcessing); }
  1661. virtual bool IsIncomplete() override { return (mTypeIncomplete) || (mBaseTypeMayBeIncomplete); }
  1662. virtual bool IsSplattable() override { BF_ASSERT((mInstSize >= 0) || (!IsComposite())); return mIsSplattable; }
  1663. virtual int GetSplatCount() override;
  1664. virtual bool IsTypeInstance() override { return true; }
  1665. virtual bool IsInterface() override { return mTypeDef->mTypeCode == BfTypeCode_Interface; }
  1666. virtual bool IsValueType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1667. virtual bool IsStruct() override { return ((mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum)) && (!mIsTypedPrimitive); }
  1668. virtual bool IsUnion() override { return mIsUnion; }
  1669. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1670. virtual bool IsFunction() override { return mTypeDef->mIsFunction; }
  1671. virtual bool IsString() override;
  1672. virtual bool IsIntPtrable() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); };
  1673. virtual bool IsEnum() override { return mTypeDef->mTypeCode == BfTypeCode_Enum; }
  1674. virtual bool IsPayloadEnum() override { return (mTypeDef->mTypeCode == BfTypeCode_Enum) && (!mIsTypedPrimitive); }
  1675. virtual bool IsTypedPrimitive() override { return mIsTypedPrimitive; }
  1676. virtual bool IsStructOrStructPtr() override { return mTypeDef->mTypeCode == BfTypeCode_Struct; }
  1677. virtual bool IsValueTypeOrValueTypePtr() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1678. virtual bool IsObject() override { return mTypeDef->mTypeCode == BfTypeCode_Object; }
  1679. virtual bool IsObjectOrStruct() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Struct); }
  1680. virtual bool IsObjectOrInterface() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); }
  1681. virtual BfType* GetUnderlyingType() override;
  1682. //virtual bool IsValuelessType() override { return (mIsTypedPrimitive) && (mInstSize == 0); }
  1683. virtual bool CanBeValuelessType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1684. virtual bool IsValuelessType() override;
  1685. virtual bool HasPackingHoles() override { return mHasPackingHoles; }
  1686. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) override;
  1687. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) override;
  1688. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProjectSet* visibleProjectSet) override;
  1689. virtual bool WantsGCMarking() override;
  1690. virtual bool GetLoweredType(BfTypeUsage typeUsage, BfTypeCode* outTypeCode = NULL, BfTypeCode* outTypeCode2 = NULL) override;
  1691. BfGenericTypeInfo* GetGenericTypeInfo() { return mGenericTypeInfo; }
  1692. virtual BfTypeInstance* ToGenericTypeInstance() override { return (mGenericTypeInfo != NULL) ? this : NULL; }
  1693. virtual bool IsGenericTypeInstance() override { return mGenericTypeInfo != NULL; }
  1694. virtual bool IsSpecializedType() override { return (mGenericTypeInfo != NULL) && (!mGenericTypeInfo->mIsUnspecialized); }
  1695. virtual bool IsSpecializedByAutoCompleteMethod() override;
  1696. virtual bool IsUnspecializedType() override { return (mGenericTypeInfo != NULL) && (mGenericTypeInfo->mIsUnspecialized); }
  1697. virtual bool IsUnspecializedTypeVariation() override { return (mGenericTypeInfo != NULL) && (mGenericTypeInfo->mIsUnspecializedVariation); }
  1698. virtual bool IsNullable() override;
  1699. virtual bool HasVarConstraints();
  1700. virtual bool IsTypeMemberIncluded(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef = NULL, BfModule* module = NULL) override;
  1701. virtual BfTypeInstance* GetImplBaseType() { return mBaseType; }
  1702. virtual bool IsIRFuncUsed(BfIRFunction func);
  1703. void CalcHotVirtualData(/*Val128& vtHash, */Array<int>* ifaceMapping);
  1704. int GetOrigVTableSize();
  1705. int GetSelfVTableSize();
  1706. int GetOrigSelfVTableSize();
  1707. int GetImplBaseVTableSize();
  1708. int GetOrigImplBaseVTableSize();
  1709. int GetIFaceVMethodSize();
  1710. BfType* GetUnionInnerType(bool* wantSplat = NULL);
  1711. BfPrimitiveType* GetDiscriminatorType(int* outDataIdx = NULL);
  1712. void GetUnderlyingArray(BfType*& type, int& size, bool& isVector);
  1713. bool HasEquivalentLayout(BfTypeInstance* compareTo);
  1714. BfIRConstHolder* GetOrCreateConstHolder();
  1715. BfIRValue CreateConst(BfConstant* fromConst, BfIRConstHolder* fromHolder);
  1716. int GetInstStride() { return BF_ALIGN(mInstSize, mInstAlign); }
  1717. bool HasOverrideMethods();
  1718. bool GetResultInfo(BfType*& valueType, int& okTagId);
  1719. BfGenericTypeInfo::GenericParamsVector* GetGenericParamsVector(BfTypeDef* declaringTypeDef);
  1720. void GenerateProjectsReferenced();
  1721. bool IsAlwaysInclude();
  1722. virtual void ReportMemory(MemReporter* memReporter) override;
  1723. };
  1724. template <typename T>
  1725. class LogAlloc
  1726. {
  1727. public:
  1728. T* allocate(intptr count)
  1729. {
  1730. auto ptr = (T*)malloc(sizeof(T) * count);
  1731. OutputDebugStrF("LogAlloc.allocate: %p\n", ptr);
  1732. return ptr;
  1733. }
  1734. void deallocate(T* ptr)
  1735. {
  1736. OutputDebugStrF("LogAlloc.deallocate: %p\n", ptr);
  1737. free(ptr);
  1738. }
  1739. void* rawAllocate(intptr size)
  1740. {
  1741. auto ptr = malloc(size);
  1742. OutputDebugStrF("LogAlloc.rawAllocate: %p\n", ptr);
  1743. return ptr;
  1744. }
  1745. void rawDeallocate(void* ptr)
  1746. {
  1747. OutputDebugStrF("LogAlloc.rawFree: %p\n", ptr);
  1748. free(ptr);
  1749. }
  1750. };
  1751. class BfBoxedType : public BfTypeInstance
  1752. {
  1753. public:
  1754. enum BoxedFlags
  1755. {
  1756. BoxedFlags_None = 0,
  1757. BoxedFlags_StructPtr = 1
  1758. };
  1759. public:
  1760. BfType* mElementType;
  1761. BfBoxedType* mBoxedBaseType;
  1762. BoxedFlags mBoxedFlags;
  1763. public:
  1764. BfBoxedType()
  1765. {
  1766. mElementType = NULL;
  1767. mBoxedBaseType = NULL;
  1768. mBoxedFlags = BoxedFlags_None;
  1769. }
  1770. virtual bool IsBoxed() override { return true; }
  1771. virtual bool IsValueType() override { return false; }
  1772. virtual bool IsStruct() override { return false; }
  1773. virtual bool IsEnum() override { return false; }
  1774. virtual bool IsStructOrStructPtr() override { return false; }
  1775. virtual bool IsObject() override { return true; }
  1776. virtual bool IsObjectOrStruct() override { return true; }
  1777. virtual bool IsObjectOrInterface() override { return true; }
  1778. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1779. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1780. virtual BfTypeInstance* ToGenericTypeInstance() override { return mElementType->ToGenericTypeInstance(); }
  1781. virtual bool IsSpecializedType() override { return !mElementType->IsUnspecializedType(); }
  1782. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1783. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1784. virtual BfTypeInstance* GetImplBaseType() override { return (mBoxedBaseType != NULL) ? mBoxedBaseType : mBaseType; }
  1785. bool IsBoxedStructPtr()
  1786. {
  1787. return (mBoxedFlags & BoxedFlags_StructPtr) != 0;
  1788. }
  1789. BfType* GetModifiedElementType();
  1790. };
  1791. class BfTypeAliasType : public BfTypeInstance
  1792. {
  1793. public:
  1794. BfType* mAliasToType;
  1795. public:
  1796. BfTypeAliasType()
  1797. {
  1798. mAliasToType = NULL;
  1799. }
  1800. virtual bool IsTypeAlias() override { return true; }
  1801. virtual BfType* GetUnderlyingType() override { return mAliasToType; }
  1802. virtual bool WantsGCMarking() override { return mAliasToType->WantsGCMarking(); }
  1803. };
  1804. enum BfCaptureType
  1805. {
  1806. BfCaptureType_None,
  1807. BfCaptureType_Copy,
  1808. BfCaptureType_Reference,
  1809. };
  1810. class BfClosureType : public BfTypeInstance
  1811. {
  1812. public:
  1813. Val128 mClosureHash; // Includes base type and capture info
  1814. BfTypeInstance* mSrcDelegate;
  1815. bool mCreatedTypeDef;
  1816. String mNameAdd;
  1817. BfSource mSource;
  1818. Array<BfAstNode*> mDirectAllocNodes;
  1819. bool mIsUnique;
  1820. public:
  1821. BfClosureType(BfTypeInstance* srcDelegate, Val128 closureHash);
  1822. ~BfClosureType();
  1823. void Init(BfProject* bfProject);
  1824. BfFieldDef* AddField(BfType* type, const StringImpl& name);
  1825. BfMethodDef* AddDtor();
  1826. void Finish();
  1827. virtual bool IsClosure() override { return true; }
  1828. virtual bool IsOnDemand() override { return true; }
  1829. };
  1830. class BfDelegateType : public BfTypeInstance
  1831. {
  1832. public:
  1833. BfDelegateInfo mDelegateInfo;
  1834. bool mIsUnspecializedType;
  1835. bool mIsUnspecializedTypeVariation;
  1836. public:
  1837. BfDelegateType()
  1838. {
  1839. mIsUnspecializedType = false;
  1840. mIsUnspecializedTypeVariation = false;
  1841. }
  1842. ~BfDelegateType();
  1843. virtual bool IsOnDemand() override { return true; }
  1844. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1845. virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }
  1846. virtual bool IsFunction() override { return !mTypeDef->mIsDelegate; }
  1847. virtual bool IsFunctionFromTypeRef() override { return !mTypeDef->mIsDelegate; }
  1848. virtual bool IsUnspecializedType() override { return mIsUnspecializedType; }
  1849. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; }
  1850. virtual BfDelegateInfo* GetDelegateInfo() override { return &mDelegateInfo; }
  1851. };
  1852. class BfTupleType : public BfTypeInstance
  1853. {
  1854. public:
  1855. bool mCreatedTypeDef;
  1856. String mNameAdd;
  1857. BfSource* mSource;
  1858. bool mIsUnspecializedType;
  1859. bool mIsUnspecializedTypeVariation;
  1860. public:
  1861. BfTupleType();
  1862. ~BfTupleType();
  1863. void Init(BfProject* bfProject, BfTypeInstance* valueTypeInstance);
  1864. BfFieldDef* AddField(const StringImpl& name);
  1865. void Finish();
  1866. virtual bool IsOnDemand() override { return true; }
  1867. virtual bool IsTuple() override { return true; }
  1868. virtual bool IsUnspecializedType() override { return mIsUnspecializedType; }
  1869. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; }
  1870. };
  1871. class BfConcreteInterfaceType : public BfType
  1872. {
  1873. public:
  1874. BfTypeInstance* mInterface;
  1875. virtual bool IsConcreteInterfaceType() override { return true; }
  1876. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1877. virtual BfType* GetUnderlyingType() override { return mInterface; }
  1878. };
  1879. class BfPointerType : public BfType
  1880. {
  1881. public:
  1882. BfType* mElementType;
  1883. public:
  1884. virtual bool IsWrappableType() override { return true; }
  1885. virtual bool IsReified() override { return mElementType->IsReified(); }
  1886. virtual bool IsPointer() override { return true; }
  1887. virtual bool IsIntPtrable() override { return true; }
  1888. virtual bool IsStructPtr() override { return mElementType->IsStruct(); }
  1889. virtual bool IsStructOrStructPtr() override { return mElementType->IsStruct(); }
  1890. virtual bool IsValueTypeOrValueTypePtr() override { return mElementType->IsValueType(); }
  1891. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1892. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1893. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1894. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1895. virtual bool IsVoidPtr() override { return mElementType->IsVoid(); }
  1896. };
  1897. // This is used for direct method references for generics so we can directly call methods rather than indirectly through delegates
  1898. class BfMethodRefType : public BfDependedType
  1899. {
  1900. public:
  1901. BfMethodInstance* mMethodRef;
  1902. String mMangledMethodName;
  1903. BfTypeInstance* mOwner;
  1904. int mOwnerRevision;
  1905. bool mIsAutoCompleteMethod;
  1906. Array<int> mParamToDataIdx;
  1907. Array<int> mDataToParamIdx;
  1908. bool mIsUnspecialized;
  1909. bool mIsUnspecializedVariation;
  1910. public:
  1911. BfMethodRefType()
  1912. {
  1913. mMethodRef = NULL;
  1914. mOwner = NULL;
  1915. mOwnerRevision = -1;
  1916. mIsAutoCompleteMethod = false;
  1917. mIsUnspecialized = false;
  1918. mIsUnspecializedVariation = false;
  1919. }
  1920. ~BfMethodRefType();
  1921. //virtual bool IsValuelessType() override { return mSize != 0; }
  1922. virtual bool IsValueType() override { return true; }
  1923. virtual bool IsComposite() override { return true; }
  1924. virtual bool IsMethodRef() override { return true; }
  1925. virtual bool IsSplattable() override { return true; }
  1926. virtual int GetSplatCount() override { return (int)mDataToParamIdx.mSize; }
  1927. virtual bool IsOnDemand() override { return true; }
  1928. virtual bool IsTemporary() override { return true; }
  1929. virtual bool IsUnspecializedType() override { return mIsUnspecialized; }
  1930. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedVariation; }
  1931. int GetCaptureDataCount();
  1932. BfType* GetCaptureType(int captureDataIdx);
  1933. int GetDataIdxFromParamIdx(int paramIdx);
  1934. int GetParamIdxFromDataIdx(int dataIdx);
  1935. bool WantsDataPassedAsSplat(int dataIdx);
  1936. //virtual BfType* GetUnderlyingType() override { return mOwner; }
  1937. };
  1938. class BfRefType : public BfType
  1939. {
  1940. public:
  1941. enum RefKind
  1942. {
  1943. RefKind_Ref,
  1944. RefKind_Out,
  1945. RefKind_Mut
  1946. };
  1947. BfType* mElementType;
  1948. RefKind mRefKind;
  1949. // When an element gets rebuild, it may become valueless which makes us valueless
  1950. void CheckElement()
  1951. {
  1952. if ((mDefineState >= BfTypeDefineState_Defined) && (mElementType->mDefineState < BfTypeDefineState_Defined) && (mElementType->CanBeValuelessType()))
  1953. mDefineState = BfTypeDefineState_Declared;
  1954. }
  1955. virtual bool IsDataIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1956. virtual bool IsIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1957. virtual bool IsReified() override { return mElementType->IsReified(); }
  1958. virtual bool IsRef() override { return true; }
  1959. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1960. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1961. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1962. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1963. virtual bool CanBeValuelessType() override { return mElementType->CanBeValuelessType(); }
  1964. virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1965. };
  1966. class BfArrayType : public BfTypeInstance
  1967. {
  1968. public:
  1969. int mDimensions;
  1970. public:
  1971. virtual bool IsArray() override { return true; }
  1972. virtual bool IsValueType() override { return false; }
  1973. virtual bool IsStruct() override { return false; }
  1974. virtual bool IsStructOrStructPtr() override { return false; }
  1975. virtual bool IsObject() override { return true; }
  1976. virtual bool IsObjectOrStruct() override { return true; }
  1977. virtual bool IsObjectOrInterface() override { return true; }
  1978. int GetLengthBitCount();
  1979. };
  1980. class BfSizedArrayType : public BfDependedType
  1981. {
  1982. public:
  1983. BfType* mElementType;
  1984. intptr mElementCount;
  1985. bool mWantsGCMarking;
  1986. public:
  1987. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  1988. virtual bool IsSizedArray() override { return true; }
  1989. virtual bool IsWrappableType() override { return true; }
  1990. virtual bool IsValueType() override { return true; } // Is a type of struct
  1991. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  1992. virtual bool IsComposite() override { return true; }
  1993. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  1994. virtual bool IsStructOrStructPtr() override { return false; }
  1995. virtual bool IsReified() override { return mElementType->IsReified(); }
  1996. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1997. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1998. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1999. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  2000. virtual bool CanBeValuelessType() override { return true; }
  2001. virtual bool WantsGCMarking() override { BF_ASSERT(mDefineState >= BfTypeDefineState_Defined); return mWantsGCMarking; }
  2002. // Leave the default "zero sized" definition
  2003. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  2004. };
  2005. // This is used when a sized array is sized by a const generic argument
  2006. class BfUnknownSizedArrayType : public BfSizedArrayType
  2007. {
  2008. public:
  2009. BfType* mElementCountSource;
  2010. public:
  2011. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  2012. virtual bool IsUnknownSizedArray() override { return true; }
  2013. virtual bool IsWrappableType() override { return true; }
  2014. virtual bool IsValueType() override { return true; } // Is a type of struct
  2015. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  2016. virtual bool IsComposite() override { return true; }
  2017. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  2018. virtual bool IsStructOrStructPtr() override { return false; }
  2019. virtual bool IsReified() override { return mElementType->IsReified(); }
  2020. virtual bool IsDependentOnUnderlyingType() override { return true; }
  2021. virtual BfType* GetUnderlyingType() override { return mElementType; }
  2022. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType() || mElementCountSource->IsUnspecializedType(); }
  2023. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation() || mElementCountSource->IsUnspecializedTypeVariation(); }
  2024. virtual bool CanBeValuelessType() override { return true; }
  2025. // Leave the default "zero sized" definition
  2026. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  2027. };
  2028. class BfConstExprValueType : public BfDependedType
  2029. {
  2030. public:
  2031. BfType* mType;
  2032. BfVariant mValue;
  2033. public:
  2034. ~BfConstExprValueType();
  2035. virtual bool IsConstExprValue() override { return true; }
  2036. virtual BfType* GetUnderlyingType() override { return mType; }
  2037. };
  2038. /*class BfCustomAttributeArgument
  2039. {
  2040. public:
  2041. llvm::Constant* mConstParam;
  2042. };*/
  2043. class BfCustomAttributeSetProperty
  2044. {
  2045. public:
  2046. BfPropertyRef mPropertyRef;
  2047. BfTypedValue mParam;
  2048. };
  2049. class BfCustomAttributeSetField
  2050. {
  2051. public:
  2052. BfFieldRef mFieldRef;
  2053. BfTypedValue mParam;
  2054. };
  2055. class BfCustomAttribute
  2056. {
  2057. public:
  2058. BfAttributeDirective* mRef;
  2059. BfTypeInstance* mType;
  2060. BfMethodDef* mCtor;
  2061. Array<BfIRValue> mCtorArgs;
  2062. Array<BfCustomAttributeSetProperty> mSetProperties;
  2063. Array<BfCustomAttributeSetField> mSetField;
  2064. bool mAwaitingValidation;
  2065. BfAstNode* GetRefNode()
  2066. {
  2067. if (mRef->mAttributeTypeRef != NULL)
  2068. return mRef->mAttributeTypeRef;
  2069. return mRef;
  2070. }
  2071. };
  2072. class BfCustomAttributes
  2073. {
  2074. public:
  2075. Array<BfCustomAttribute> mAttributes;
  2076. bool Contains(BfTypeDef* typeDef);
  2077. BfCustomAttribute* Get(BfTypeDef* typeDef);
  2078. void ReportMemory(MemReporter* memReporter);
  2079. };
  2080. class BfResolvedTypeSetFuncs : public MultiHashSetFuncs
  2081. {
  2082. };
  2083. class BfResolvedTypeSet : public MultiHashSet<BfType*, BfResolvedTypeSetFuncs>
  2084. {
  2085. public:
  2086. enum BfHashFlags
  2087. {
  2088. BfHashFlag_None = 0,
  2089. BfHashFlag_AllowRef = 1,
  2090. BfHashFlag_AllowGenericParamConstValue = 2
  2091. };
  2092. class LookupContext
  2093. {
  2094. public:
  2095. BfModule* mModule;
  2096. BfTypeReference* mRootTypeRef;
  2097. BfTypeDef* mRootTypeDef;
  2098. BfTypeInstance* mRootOuterTypeInstance;
  2099. BfType* mResolvedType;
  2100. BfResolveTypeRefFlags mResolveFlags;
  2101. bool mFailed;
  2102. public:
  2103. LookupContext()
  2104. {
  2105. mRootTypeRef = NULL;
  2106. mRootTypeDef = NULL;
  2107. mRootOuterTypeInstance = NULL;
  2108. mModule = NULL;
  2109. mResolvedType = NULL;
  2110. mFailed = false;
  2111. mResolveFlags = BfResolveTypeRefFlag_None;
  2112. }
  2113. BfType* ResolveTypeRef(BfTypeReference* typeReference);
  2114. BfTypeDef* ResolveToTypeDef(BfTypeReference* typeReference, BfType** outType = NULL);
  2115. };
  2116. public:
  2117. static BfTypeDef* FindRootCommonOuterType(BfTypeDef* outerType, LookupContext* ctx, BfTypeInstance*& outCheckTypeInstance);
  2118. static BfVariant EvaluateToVariant(LookupContext* ctx, BfExpression* expr, BfType*& outType);
  2119. static bool GenericTypeEquals(BfTypeInstance* lhsGenericType, BfTypeVector* lhsTypeGenericArguments, BfTypeReference* rhs, LookupContext* ctx, int& genericParamOffset);
  2120. static bool GenericTypeEquals(BfTypeInstance* lhsGenericType, BfTypeVector* typeGenericArguments, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
  2121. static void HashGenericArguments(BfTypeReference* typeRef, LookupContext* ctx, int& hash);
  2122. static int Hash(BfType* type, LookupContext* ctx, bool allowRef = false);
  2123. static int DirectHash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
  2124. static int Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
  2125. static bool Equals(BfType* lhs, BfType* rhs, LookupContext* ctx);
  2126. static bool Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx);
  2127. static bool Equals(BfType* lhs, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
  2128. public:
  2129. BfResolvedTypeSet()
  2130. {
  2131. mHashSize = 9973;
  2132. mHashHeads = (Entry**)AllocateZero(sizeof(Entry*) * mHashSize, alignof(Entry*));
  2133. }
  2134. ~BfResolvedTypeSet();
  2135. template <typename T>
  2136. bool Insert(T* findType, LookupContext* ctx, BfResolvedTypeSet::Entry** entryPtr)
  2137. {
  2138. CheckRehash();
  2139. int tryCount = 0;
  2140. ctx->mFailed = false;
  2141. int hashVal = Hash(findType, ctx, BfHashFlag_AllowRef);
  2142. if (ctx->mFailed)
  2143. {
  2144. return false;
  2145. }
  2146. int bucket = (hashVal & 0x7FFFFFFF) % mHashSize;
  2147. auto checkEntry = mHashHeads[bucket];
  2148. while (checkEntry != NULL)
  2149. {
  2150. // checkEntry->mType can be NULL if we're in the process of filling it in (and this Insert is from an element type)
  2151. // OR if the type resolution failed after node insertion
  2152. if ((checkEntry->mValue != NULL) && (hashVal == checkEntry->mHash) && (Equals(checkEntry->mValue, findType, ctx)))
  2153. {
  2154. *entryPtr = checkEntry;
  2155. return false;
  2156. }
  2157. checkEntry = checkEntry->mNext;
  2158. tryCount++;
  2159. // If this fires off, this may indicate that our hashes are equivalent but Equals fails
  2160. if (tryCount >= 10)
  2161. {
  2162. NOP;
  2163. }
  2164. BF_ASSERT(tryCount < 10);
  2165. }
  2166. if ((ctx->mResolveFlags & BfResolveTypeRefFlag_NoCreate) != 0)
  2167. return false;
  2168. mCount++;
  2169. Entry* entry = (Entry*)BfResolvedTypeSetFuncs::Allocate(sizeof(Entry), alignof(Entry));
  2170. entry->mValue = NULL;
  2171. // if (mHashHeads[bucket] != NULL)
  2172. // mHashHeads[bucket]->mPrev = entry;
  2173. entry->mNext = mHashHeads[bucket];
  2174. entry->mHash = hashVal;
  2175. mHashHeads[bucket] = entry;
  2176. *entryPtr = entry;
  2177. return true;
  2178. }
  2179. // Iterator begin();
  2180. // Iterator end();
  2181. // Iterator erase(Iterator& itr);
  2182. void RemoveEntry(Entry* entry);
  2183. };
  2184. class BfTypeUtils
  2185. {
  2186. public:
  2187. static String HashEncode64(uint64 val); // Note: this only encodes 60 bits
  2188. static String TypeToString(BfTypeReference* typeRef);
  2189. static String TypeToString(BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  2190. static bool TypeToString(StringImpl& str, BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  2191. static bool TypeEquals(BfType* typeA, BfType* typeB, BfType* selfType);
  2192. template <typename T>
  2193. static void GetProjectList(BfType* checkType, T* projectList, int immutableLength)
  2194. {
  2195. if (checkType->IsBoxed())
  2196. GetProjectList(((BfBoxedType*)checkType)->mElementType, projectList, immutableLength);
  2197. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  2198. if (typeInst != NULL)
  2199. {
  2200. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  2201. if (genericTypeInst != NULL)
  2202. {
  2203. for (auto genericArg : genericTypeInst->mGenericTypeInfo->mTypeGenericArguments)
  2204. GetProjectList(genericArg, projectList, immutableLength);
  2205. }
  2206. BfProject* bfProject = typeInst->mTypeDef->mProject;
  2207. if (!projectList->Contains(bfProject))
  2208. {
  2209. bool handled = false;
  2210. for (int idx = 0; idx < (int)projectList->size(); idx++)
  2211. {
  2212. auto checkProject = (*projectList)[idx];
  2213. bool isBetter = bfProject->ContainsReference(checkProject);
  2214. bool isWorse = checkProject->ContainsReference(bfProject);
  2215. if (isBetter == isWorse)
  2216. continue;
  2217. if (isBetter)
  2218. {
  2219. if (idx >= immutableLength)
  2220. {
  2221. // This is even more specific, so replace with this one
  2222. (*projectList)[idx] = bfProject;
  2223. handled = true;
  2224. }
  2225. }
  2226. else
  2227. {
  2228. // This is less specific, ignore
  2229. handled = true;
  2230. }
  2231. break;
  2232. }
  2233. if (!handled)
  2234. {
  2235. projectList->Add(bfProject);
  2236. }
  2237. }
  2238. if (checkType->IsTuple())
  2239. {
  2240. auto tupleType = (BfTupleType*)checkType;
  2241. for (auto& fieldInstance : tupleType->mFieldInstances)
  2242. GetProjectList(fieldInstance.mResolvedType, projectList, immutableLength);
  2243. }
  2244. auto delegateInfo = checkType->GetDelegateInfo();
  2245. if (delegateInfo != NULL)
  2246. {
  2247. GetProjectList(delegateInfo->mReturnType, projectList, immutableLength);
  2248. for (auto param : delegateInfo->mParams)
  2249. GetProjectList(param, projectList, immutableLength);
  2250. }
  2251. }
  2252. else if (checkType->IsPointer())
  2253. GetProjectList(((BfPointerType*)checkType)->mElementType, projectList, immutableLength);
  2254. else if (checkType->IsRef())
  2255. GetProjectList(((BfPointerType*)checkType)->mElementType, projectList, immutableLength);
  2256. else if (checkType->IsSizedArray())
  2257. GetProjectList(((BfSizedArrayType*)checkType)->mElementType, projectList, immutableLength);
  2258. else if (checkType->IsMethodRef())
  2259. GetProjectList(((BfMethodRefType*)checkType)->mOwner, projectList, immutableLength);
  2260. }
  2261. static BfPrimitiveType* GetPrimitiveType(BfModule* module, BfTypeCode typeCode);
  2262. static void PopulateType(BfModule* module, BfType* type);
  2263. template <typename T>
  2264. static void SplatIterate(const T& dataLambda, BfType* checkType)
  2265. {
  2266. auto checkTypeInstance = checkType->ToTypeInstance();
  2267. if ((checkTypeInstance != NULL) && (checkTypeInstance->IsValueType()) && (checkTypeInstance->IsDataIncomplete()))
  2268. PopulateType(checkTypeInstance->mModule, checkTypeInstance);
  2269. if (checkType->IsStruct())
  2270. {
  2271. if (checkTypeInstance->mBaseType != NULL)
  2272. SplatIterate<T>(dataLambda, checkTypeInstance->mBaseType);
  2273. if (checkTypeInstance->mIsUnion)
  2274. {
  2275. BfType* unionInnerType = checkTypeInstance->GetUnionInnerType();
  2276. SplatIterate<T>(dataLambda, unionInnerType);
  2277. }
  2278. else
  2279. {
  2280. for (int fieldIdx = 0; fieldIdx < (int)checkTypeInstance->mFieldInstances.size(); fieldIdx++)
  2281. {
  2282. auto fieldInstance = (BfFieldInstance*)&checkTypeInstance->mFieldInstances[fieldIdx];
  2283. if (fieldInstance->mDataIdx >= 0)
  2284. {
  2285. SplatIterate<T>(dataLambda, fieldInstance->GetResolvedType());
  2286. }
  2287. }
  2288. }
  2289. if (checkTypeInstance->IsEnum())
  2290. {
  2291. // Add discriminator
  2292. auto dscrType = checkTypeInstance->GetDiscriminatorType();
  2293. dataLambda(dscrType);
  2294. }
  2295. }
  2296. else if (checkType->IsMethodRef())
  2297. {
  2298. auto methodRefType = (BfMethodRefType*)checkType;
  2299. for (int dataIdx = 0; dataIdx < methodRefType->GetCaptureDataCount(); dataIdx++)
  2300. {
  2301. if (methodRefType->WantsDataPassedAsSplat(dataIdx))
  2302. SplatIterate<T>(dataLambda, methodRefType->GetCaptureType(dataIdx));
  2303. else
  2304. dataLambda(methodRefType->GetCaptureType(dataIdx));
  2305. }
  2306. }
  2307. else if (!checkType->IsValuelessType())
  2308. {
  2309. dataLambda(checkType);
  2310. }
  2311. }
  2312. static int GetSplatCount(BfType* type);
  2313. };
  2314. //void DbgCheckType(llvm::Type* checkType);
  2315. NS_BF_END
  2316. namespace std
  2317. {
  2318. template<>
  2319. struct hash<Beefy::BfTypeVector>
  2320. {
  2321. size_t operator()(const Beefy::BfTypeVector& val) const
  2322. {
  2323. int curHash = 0;
  2324. for (auto type : val)
  2325. curHash = ((curHash ^ type->mTypeId) << 5) - curHash;
  2326. return curHash;
  2327. }
  2328. };
  2329. template<>
  2330. struct hash<Beefy::BfFieldRef>
  2331. {
  2332. size_t operator()(const Beefy::BfFieldRef& fieldRef) const
  2333. {
  2334. return (size_t)fieldRef.mTypeInstance + fieldRef.mFieldIdx;
  2335. }
  2336. };
  2337. template<>
  2338. struct hash<Beefy::BfHotDevirtualizedMethod>
  2339. {
  2340. size_t operator()(const Beefy::BfHotDevirtualizedMethod& val) const
  2341. {
  2342. return (size_t)val.mMethod;
  2343. }
  2344. };
  2345. template<>
  2346. struct hash<Beefy::BfHotFunctionReference>
  2347. {
  2348. size_t operator()(const Beefy::BfHotFunctionReference& val) const
  2349. {
  2350. return (size_t)val.mMethod;
  2351. }
  2352. };
  2353. }