BfResolvedTypeUtils.h 71 KB

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