BfResolvedTypeUtils.h 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  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 BfGenericParamInstance
  827. {
  828. public:
  829. int mGenericParamFlags;
  830. Array<BfTypeInstance*> mInterfaceConstraints;
  831. BfType* mTypeConstraint;
  832. int mRefCount;
  833. BfGenericParamInstance()
  834. {
  835. mGenericParamFlags = 0;
  836. mTypeConstraint = NULL;
  837. mRefCount = 1;
  838. }
  839. void Release()
  840. {
  841. if (--mRefCount == 0)
  842. delete this;
  843. }
  844. virtual ~BfGenericParamInstance()
  845. {
  846. }
  847. virtual BfGenericParamDef* GetGenericParamDef() = 0;
  848. };
  849. class BfGenericTypeParamInstance : public BfGenericParamInstance
  850. {
  851. public:
  852. BfTypeDef* mTypeDef;
  853. int mGenericIdx;
  854. public:
  855. BfGenericTypeParamInstance(BfTypeDef* typeDef, int genericIdx)
  856. {
  857. mTypeDef = typeDef;
  858. mGenericIdx = genericIdx;
  859. mGenericParamFlags = GetGenericParamDef()->mGenericParamFlags;
  860. mTypeConstraint = NULL;
  861. }
  862. BfGenericTypeParamInstance* AddRef()
  863. {
  864. mRefCount++;
  865. return this;
  866. }
  867. virtual BfGenericParamDef* GetGenericParamDef() override
  868. {
  869. return mTypeDef->mGenericParamDefs[mGenericIdx];
  870. }
  871. };
  872. class BfGenericMethodParamInstance : public BfGenericParamInstance
  873. {
  874. public:
  875. BfMethodDef* mMethodDef;
  876. int mGenericIdx;
  877. public:
  878. BfGenericMethodParamInstance(BfMethodDef* methodDef, int genericIdx)
  879. {
  880. mMethodDef = methodDef;
  881. mGenericIdx = genericIdx;
  882. mGenericParamFlags = GetGenericParamDef()->mGenericParamFlags;
  883. mTypeConstraint = NULL;
  884. }
  885. BfGenericMethodParamInstance* AddRef()
  886. {
  887. mRefCount++;
  888. return this;
  889. }
  890. virtual BfGenericParamDef* GetGenericParamDef() override
  891. {
  892. return mMethodDef->mGenericParams[mGenericIdx];
  893. }
  894. };
  895. #define BF_VALCOMP(val) if (val != 0) return val
  896. struct BfTypeVectorHash
  897. {
  898. size_t operator()(const BfTypeVector& val) const;
  899. };
  900. struct BfTypeVectorEquals
  901. {
  902. bool operator()(const BfTypeVector& lhs, const BfTypeVector& rhs) const;
  903. };
  904. // Specialized for Class<T> but not Method<T>
  905. class BfMethodInstanceGroup
  906. {
  907. public:
  908. BfTypeInstance* mOwner;
  909. BfMethodInstance* mDefault;
  910. typedef Dictionary<BfTypeVector, BfMethodInstance*> MapType;
  911. MapType* mMethodSpecializationMap;
  912. int mMethodIdx;
  913. int mRefCount; // External references from BfMethodRefType
  914. BfMethodOnDemandKind mOnDemandKind;
  915. public:
  916. BfMethodInstanceGroup()
  917. {
  918. mOwner = NULL;
  919. mDefault = NULL;
  920. mMethodSpecializationMap = NULL;
  921. mMethodIdx = -1;
  922. mOnDemandKind = BfMethodOnDemandKind_NotSet;
  923. mRefCount = 0;
  924. }
  925. ~BfMethodInstanceGroup();
  926. BfMethodInstanceGroup(BfMethodInstanceGroup&& prev) noexcept
  927. {
  928. mOwner = prev.mOwner;
  929. mDefault = prev.mDefault;
  930. mMethodSpecializationMap = prev.mMethodSpecializationMap;
  931. mMethodIdx = prev.mMethodIdx;
  932. mRefCount = prev.mRefCount;
  933. mOnDemandKind = prev.mOnDemandKind;
  934. if (mDefault != NULL)
  935. mDefault->mMethodInstanceGroup = this;
  936. if (mMethodSpecializationMap != NULL)
  937. {
  938. for (auto& pair : *mMethodSpecializationMap)
  939. pair.mValue->mMethodInstanceGroup = this;
  940. }
  941. prev.mRefCount = 0;
  942. prev.mDefault = NULL;
  943. prev.mMethodSpecializationMap = NULL;
  944. }
  945. bool IsImplemented()
  946. {
  947. return (mOnDemandKind == BfMethodOnDemandKind_AlwaysInclude) ||
  948. (mOnDemandKind == BfMethodOnDemandKind_Referenced) ||
  949. (mOnDemandKind == BfMethodOnDemandKind_InWorkList);
  950. }
  951. };
  952. class BfFieldInstance
  953. {
  954. public:
  955. BfTypeInstance* mOwner;
  956. BfType* mResolvedType;
  957. BfCustomAttributes* mCustomAttributes;
  958. int mConstIdx;
  959. int mFieldIdx;
  960. int mDataIdx; // mFieldIdx includes statics & consts, mDataIdx does not
  961. int mMergedDataIdx; // Like mDataIdx but contains base class's data
  962. int mDataOffset;
  963. int mDataSize;
  964. bool mFieldIncluded;
  965. bool mIsEnumPayloadCase;
  966. bool mIsThreadLocal;
  967. bool mIsInferredType;
  968. int mLastRevisionReferenced;
  969. public:
  970. BfFieldDef* GetFieldDef();
  971. BfFieldInstance(const BfFieldInstance& copyFrom)
  972. {
  973. BF_ASSERT(copyFrom.mCustomAttributes == NULL);
  974. mOwner = copyFrom.mOwner;
  975. mResolvedType = copyFrom.mResolvedType;
  976. mCustomAttributes = copyFrom.mCustomAttributes;
  977. mConstIdx = copyFrom.mConstIdx;
  978. mFieldIdx = copyFrom.mFieldIdx;
  979. mDataIdx = copyFrom.mDataIdx;
  980. mMergedDataIdx = copyFrom.mMergedDataIdx;
  981. mDataOffset = copyFrom.mDataOffset;
  982. mDataSize = copyFrom.mDataSize;
  983. mFieldIncluded = copyFrom.mFieldIncluded;
  984. mIsEnumPayloadCase = copyFrom.mIsEnumPayloadCase;
  985. mIsThreadLocal = copyFrom.mIsThreadLocal;
  986. mIsInferredType = copyFrom.mIsInferredType;
  987. mLastRevisionReferenced = copyFrom.mLastRevisionReferenced;
  988. }
  989. BfFieldInstance()
  990. {
  991. mFieldIdx = -1;
  992. mOwner = NULL;
  993. mResolvedType = NULL;
  994. mIsEnumPayloadCase = false;
  995. mCustomAttributes = NULL;
  996. mConstIdx = -1;
  997. mDataIdx = -1;
  998. mMergedDataIdx = -1;
  999. mDataOffset = -1;
  1000. mDataSize = 0;
  1001. mFieldIncluded = true;
  1002. mIsThreadLocal = false;
  1003. mIsInferredType = false;
  1004. mLastRevisionReferenced = -1;
  1005. }
  1006. ~BfFieldInstance();
  1007. BfType* GetResolvedType();
  1008. void SetResolvedType(BfType* type);
  1009. };
  1010. enum BfMethodRefKind
  1011. {
  1012. BfMethodRefKind_VExtMarker = -1,
  1013. BfMethodRefKind_AmbiguousRef = -2
  1014. };
  1015. class BfNonGenericMethodRef
  1016. {
  1017. public:
  1018. BfTypeInstance* mTypeInstance;
  1019. union
  1020. {
  1021. int mMethodNum;
  1022. BfMethodRefKind mKind;
  1023. };
  1024. int mSignatureHash;
  1025. BfNonGenericMethodRef()
  1026. {
  1027. mTypeInstance = NULL;
  1028. mMethodNum = 0;
  1029. mSignatureHash = 0;
  1030. }
  1031. BfNonGenericMethodRef(BfMethodInstance* methodInstance);
  1032. operator BfMethodInstance*() const;
  1033. bool IsNull() { return mTypeInstance == NULL; };
  1034. BfMethodInstance* operator->() const;
  1035. BfNonGenericMethodRef& operator=(BfMethodInstance* methodInstance);
  1036. bool operator==(const BfNonGenericMethodRef& methodRef) const;
  1037. bool operator==(BfMethodInstance* methodInstance) const;
  1038. struct Hash
  1039. {
  1040. size_t operator()(const BfNonGenericMethodRef& val) const;
  1041. };
  1042. struct Equals
  1043. {
  1044. bool operator()(const BfNonGenericMethodRef& lhs, const BfNonGenericMethodRef& rhs) const
  1045. {
  1046. return lhs == rhs;
  1047. }
  1048. };
  1049. };
  1050. enum BfMethodRefFlags : uint8
  1051. {
  1052. BfMethodRefFlag_None = 0,
  1053. BfMethodRefFlag_AlwaysInclude = 1
  1054. };
  1055. class BfMethodRef
  1056. {
  1057. public:
  1058. BfTypeInstance* mTypeInstance;
  1059. union
  1060. {
  1061. int mMethodNum;
  1062. BfMethodRefKind mKind;
  1063. };
  1064. Array<BfType*> mMethodGenericArguments;
  1065. int mSignatureHash;
  1066. BfMethodRefFlags mMethodRefFlags;
  1067. BfMethodRef()
  1068. {
  1069. mTypeInstance = NULL;
  1070. mMethodNum = 0;
  1071. mSignatureHash = 0;
  1072. mMethodRefFlags = BfMethodRefFlag_None;
  1073. }
  1074. BfMethodRef(BfMethodInstance* methodInstance);
  1075. operator BfMethodInstance*() const;
  1076. bool IsNull() { return mTypeInstance == NULL; };
  1077. BfMethodInstance* operator->() const;
  1078. BfMethodRef& operator=(BfMethodInstance* methodInstance);
  1079. bool operator==(const BfMethodRef& methodRef) const;
  1080. bool operator==(BfMethodInstance* methodInstance) const;
  1081. struct Hash
  1082. {
  1083. size_t operator()(const BfMethodRef& val) const;
  1084. };
  1085. struct Equals
  1086. {
  1087. bool operator()(const BfMethodRef& lhs, const BfMethodRef& rhs) const
  1088. {
  1089. return lhs == rhs;
  1090. }
  1091. };
  1092. };
  1093. class BfFieldRef
  1094. {
  1095. public:
  1096. BfTypeInstance* mTypeInstance;
  1097. int mFieldIdx;
  1098. public:
  1099. BfFieldRef()
  1100. {
  1101. mTypeInstance = NULL;
  1102. mFieldIdx = 0;
  1103. }
  1104. BfFieldRef(BfTypeInstance* typeInst, BfFieldDef* fieldDef);
  1105. BfFieldRef(BfFieldInstance* fieldInstance);
  1106. bool operator==(const BfFieldRef& other) const
  1107. {
  1108. return (mTypeInstance == other.mTypeInstance) && (mFieldIdx == other.mFieldIdx);
  1109. }
  1110. operator BfFieldInstance*() const;
  1111. operator BfFieldDef*() const;
  1112. operator BfPropertyDef*() const;
  1113. };
  1114. class BfPropertyRef
  1115. {
  1116. public:
  1117. BfTypeInstance* mTypeInstance;
  1118. int mPropIdx;
  1119. public:
  1120. BfPropertyRef()
  1121. {
  1122. mTypeInstance = NULL;
  1123. mPropIdx = 0;
  1124. }
  1125. BfPropertyRef(BfTypeInstance* typeInst, BfPropertyDef* propDef);
  1126. operator BfPropertyDef*() const;
  1127. };
  1128. class BfTypeInterfaceEntry
  1129. {
  1130. public:
  1131. BfTypeDef* mDeclaringType;
  1132. BfTypeInstance* mInterfaceType;
  1133. int mStartInterfaceTableIdx;
  1134. int mStartVirtualIdx; // Relative to start of virtual interface methods
  1135. bool mIsRedeclared;
  1136. };
  1137. class BfTypeInterfaceMethodEntry
  1138. {
  1139. public:
  1140. BfNonGenericMethodRef mMethodRef;
  1141. //int mVirtualIdx;
  1142. public:
  1143. BfTypeInterfaceMethodEntry()
  1144. {
  1145. //mVirtualIdx = -1;
  1146. }
  1147. };
  1148. enum BfAttributeTargets : int32
  1149. {
  1150. BfAttributeTargets_SkipValidate = -1,
  1151. BfAttributeTargets_None = 0,
  1152. BfAttributeTargets_Assembly = 0x0001,
  1153. BfAttributeTargets_Module = 0x0002,
  1154. BfAttributeTargets_Class = 0x0004,
  1155. BfAttributeTargets_Struct = 0x0008,
  1156. BfAttributeTargets_Enum = 0x0010,
  1157. BfAttributeTargets_Constructor = 0x0020,
  1158. BfAttributeTargets_Method = 0x0040,
  1159. BfAttributeTargets_Property = 0x0080,
  1160. BfAttributeTargets_Field = 0x0100,
  1161. BfAttributeTargets_StaticField = 0x0200,
  1162. BfAttributeTargets_Interface = 0x0400,
  1163. BfAttributeTargets_Parameter = 0x0800,
  1164. BfAttributeTargets_Delegate = 0x1000,
  1165. BfAttributeTargets_Function = 0x2000,
  1166. BfAttributeTargets_ReturnValue = 0x4000,
  1167. BfAttributeTargets_GenericParameter = 0x8000,
  1168. BfAttributeTargets_Invocation = 0x10000,
  1169. BfAttributeTargets_MemberAccess = 0x20000,
  1170. BfAttributeTargets_All = 0x3FFFF
  1171. };
  1172. class BfAttributeData
  1173. {
  1174. public:
  1175. BfAttributeTargets mAttributeTargets;
  1176. bool mInherited;
  1177. bool mAllowMultiple;
  1178. public:
  1179. BfAttributeData()
  1180. {
  1181. mAttributeTargets = BfAttributeTargets_All;
  1182. mInherited = true;
  1183. mAllowMultiple = false;
  1184. }
  1185. };
  1186. class BfHotTypeData
  1187. {
  1188. public:
  1189. Array<BfHotTypeVersion*> mTypeVersions;
  1190. Array<BfHotTypeDataEntry> mVTableEntries; // Doesn't fill in until we rebuild or delete type
  1191. int mVTableOrigLength;
  1192. int mOrigInterfaceMethodsLength;
  1193. bool mPendingDataChange;
  1194. bool mHadDataChange; // True if we have EVER had a hot data change
  1195. public:
  1196. BfHotTypeData()
  1197. {
  1198. mVTableOrigLength = -1;
  1199. mOrigInterfaceMethodsLength = -1;
  1200. mPendingDataChange = false;
  1201. mHadDataChange = false;
  1202. }
  1203. ~BfHotTypeData();
  1204. BfHotTypeVersion* GetTypeVersion(int hotCommitedIdx);
  1205. BfHotTypeVersion* GetLatestVersion();
  1206. BfHotTypeVersion* GetLatestVersionHead(); // The oldest version that has the same data
  1207. void ClearVersionsAfter(int hotIdx);
  1208. };
  1209. struct BfTypeLookupEntry
  1210. {
  1211. BfAtomComposite mName;
  1212. int mNumGenericParams;
  1213. uint32 mAtomUpdateIdx;
  1214. BfTypeDef* mUseTypeDef;
  1215. bool operator==(const BfTypeLookupEntry& rhs) const
  1216. {
  1217. return (mName == rhs.mName) &&
  1218. (mNumGenericParams == rhs.mNumGenericParams) &&
  1219. (mUseTypeDef == rhs.mUseTypeDef);
  1220. }
  1221. };
  1222. NS_BF_END;
  1223. namespace std
  1224. {
  1225. template<>
  1226. struct hash<Beefy::BfTypeLookupEntry>
  1227. {
  1228. size_t operator()(const Beefy::BfTypeLookupEntry& entry) const
  1229. {
  1230. int curHash = 0;
  1231. for (int i = 0; i < entry.mName.mSize; i++)
  1232. curHash = ((curHash ^ (int)(intptr)entry.mName.mParts[i]) << 5) - curHash;
  1233. curHash = ((curHash ^ entry.mNumGenericParams) << 5) - curHash;
  1234. curHash ^= (intptr)entry.mUseTypeDef;
  1235. return curHash;
  1236. }
  1237. };
  1238. }
  1239. NS_BF_BEGIN;
  1240. struct BfTypeLookupResult
  1241. {
  1242. BfTypeDef* mTypeDef;
  1243. bool mForceLookup;
  1244. BfTypeLookupResult()
  1245. {
  1246. mTypeDef = NULL;
  1247. mForceLookup = false;
  1248. }
  1249. };
  1250. class BfDependedType : public BfType
  1251. {
  1252. public:
  1253. BfDependencyMap mDependencyMap; // This is a list of types that depend on THIS type
  1254. public:
  1255. virtual bool IsDependendType() override { return true; }
  1256. virtual BfDependedType* ToDependedType() override { return this; }
  1257. };
  1258. struct BfVirtualMethodEntry
  1259. {
  1260. BfNonGenericMethodRef mDeclaringMethod;
  1261. BfNonGenericMethodRef mImplementingMethod;
  1262. };
  1263. struct BfSpecializedMethodRefInfo
  1264. {
  1265. bool mHasReifiedRef;
  1266. BfSpecializedMethodRefInfo()
  1267. {
  1268. mHasReifiedRef = false;
  1269. }
  1270. };
  1271. class BfStaticSearch
  1272. {
  1273. public:
  1274. Array<BfTypeInstance*> mStaticTypes;
  1275. };
  1276. class BfTypeInfoEx
  1277. {
  1278. public:
  1279. BfType* mUnderlyingType;
  1280. int64 mMinValue;
  1281. int64 mMaxValue;
  1282. BfTypeInfoEx()
  1283. {
  1284. mUnderlyingType = NULL;
  1285. mMinValue = 0;
  1286. mMaxValue = 0;
  1287. }
  1288. };
  1289. // Instance of struct or class
  1290. class BfTypeInstance : public BfDependedType
  1291. {
  1292. public:
  1293. int mSignatureRevision;
  1294. int mLastNonGenericUsedRevision;
  1295. int mInheritanceId;
  1296. int mInheritanceCount;
  1297. BfModule* mModule;
  1298. BfTypeDef* mTypeDef;
  1299. BfTypeInstance* mBaseType;
  1300. BfCustomAttributes* mCustomAttributes;
  1301. BfAttributeData* mAttributeData;
  1302. BfTypeInfoEx* mTypeInfoEx;
  1303. Array<BfTypeInterfaceEntry> mInterfaces;
  1304. Array<BfTypeInterfaceMethodEntry> mInterfaceMethodTable;
  1305. Array<BfMethodInstanceGroup> mMethodInstanceGroups;
  1306. Array<BfVirtualMethodEntry> mVirtualMethodTable;
  1307. BfHotTypeData* mHotTypeData;
  1308. int mVirtualMethodTableSize; // With hot reloading, mVirtualMethodTableSize can be larger than mInterfaceMethodTable (live vtable versioning)
  1309. Array<BfFieldInstance> mFieldInstances;
  1310. Array<BfMethodInstance*> mInternalMethods;
  1311. Dictionary<BfTypeDef*, BfStaticSearch> mStaticSearchMap;
  1312. bool mHasStaticInitMethod;
  1313. bool mHasStaticDtorMethod;
  1314. bool mHasStaticMarkMethod;
  1315. bool mHasTLSFindMethod;
  1316. BfIRConstHolder* mConstHolder;
  1317. Dictionary<BfMethodRef, BfSpecializedMethodRefInfo> mSpecializedMethodReferences; // Includes both specialized methods and OnDemand methods
  1318. // 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
  1319. Dictionary<BfTypeLookupEntry, BfTypeLookupResult> mLookupResults;
  1320. int mTypeOptionsIdx;
  1321. int mMergedFieldDataCount;
  1322. int mInstAlign;
  1323. int mInstSize;
  1324. int16 mInheritDepth;
  1325. int16 mSlotNum;
  1326. bool mHasBeenInstantiated;
  1327. bool mIsReified;
  1328. bool mIsTypedPrimitive;
  1329. bool mIsCRepr;
  1330. bool mIsUnion;
  1331. bool mIsPacked;
  1332. bool mIsSplattable;
  1333. bool mTypeIncomplete;
  1334. bool mTypeFailed;
  1335. bool mTypeWarned;
  1336. bool mResolvingVarField;
  1337. bool mResolvingConstField;
  1338. bool mSkipTypeProtectionChecks;
  1339. bool mNeedsMethodProcessing;
  1340. bool mBaseTypeMayBeIncomplete;
  1341. bool mHasParameterizedBase; // Generic, array, etc
  1342. bool mIsFinishingType;
  1343. bool mHasPackingHoles;
  1344. bool mWantsGCMarking;
  1345. public:
  1346. BfTypeInstance()
  1347. {
  1348. mModule = NULL;
  1349. mSignatureRevision = -1;
  1350. mLastNonGenericUsedRevision = -1;
  1351. mInheritanceId = 0;
  1352. mInheritanceCount = 0;
  1353. mTypeDef = NULL;
  1354. mRevision = -1;
  1355. mIsReified = true;
  1356. mIsSplattable = false;
  1357. mIsPacked = false;
  1358. mBaseType = NULL;
  1359. mCustomAttributes = NULL;
  1360. mAttributeData = NULL;
  1361. mTypeInfoEx = NULL;
  1362. //mClassVData = NULL;
  1363. mVirtualMethodTableSize = 0;
  1364. mHotTypeData = NULL;
  1365. mHasStaticInitMethod = false;
  1366. mHasStaticMarkMethod = false;
  1367. mHasStaticDtorMethod = false;
  1368. mHasTLSFindMethod = false;
  1369. mSlotNum = -1;
  1370. mTypeOptionsIdx = -2; // -2 = not checked, -1 = none
  1371. mInstSize = -1;
  1372. mInstAlign = -1;
  1373. mInheritDepth = 0;
  1374. mIsTypedPrimitive = false;
  1375. mTypeIncomplete = true;
  1376. mIsCRepr = false;
  1377. mIsUnion = false;
  1378. mTypeFailed = false;
  1379. mTypeWarned = false;
  1380. mResolvingVarField = false;
  1381. mSkipTypeProtectionChecks = false;
  1382. mNeedsMethodProcessing = false;
  1383. mBaseTypeMayBeIncomplete = false;
  1384. mIsFinishingType = false;
  1385. mResolvingConstField = false;
  1386. mHasPackingHoles = false;
  1387. mHasBeenInstantiated = false;
  1388. mWantsGCMarking = false;
  1389. mHasParameterizedBase = false;
  1390. mMergedFieldDataCount = 0;
  1391. mConstHolder = NULL;
  1392. }
  1393. ~BfTypeInstance();
  1394. virtual BfModule* GetModule() override { return mModule; }
  1395. virtual BfTypeInstance* ToTypeInstance() override { return this; }
  1396. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1397. virtual BfPrimitiveType* ToPrimitiveType() override { return GetUnderlyingType()->ToPrimitiveType(); }
  1398. virtual bool HasWrappedRepresentation() override { return IsTypedPrimitive(); }
  1399. int GetEndingInstanceAlignment() { if (mInstSize % mInstAlign == 0) return mInstAlign; return mInstSize % mInstAlign; }
  1400. virtual bool HasTypeFailed() override { return mTypeFailed; }
  1401. virtual bool IsReified() override { return mIsReified; }
  1402. virtual bool NeedsExplicitAlignment() override { return !IsSizeAligned() || mIsPacked; }
  1403. virtual bool IsDataIncomplete() override { return ((mTypeIncomplete) || (mBaseTypeMayBeIncomplete)) && (!mNeedsMethodProcessing); }
  1404. virtual bool IsIncomplete() override { return (mTypeIncomplete) || (mBaseTypeMayBeIncomplete); }
  1405. virtual bool IsSplattable() override { BF_ASSERT((mInstSize >= 0) || (!IsComposite())); return mIsSplattable; }
  1406. virtual int GetSplatCount() override;
  1407. virtual bool IsTypeInstance() override { return true; }
  1408. virtual bool IsInterface() override { return mTypeDef->mTypeCode == BfTypeCode_Interface; }
  1409. virtual bool IsValueType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1410. virtual bool IsStruct() override { return ((mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum)) && (!mIsTypedPrimitive); }
  1411. virtual bool IsUnion() override { return mIsUnion; }
  1412. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1413. virtual bool IsFunction() override { return mTypeDef->mIsFunction; }
  1414. virtual bool IsString() override;
  1415. virtual bool IsIntPtrable() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); };
  1416. virtual bool IsEnum() override { return mTypeDef->mTypeCode == BfTypeCode_Enum; }
  1417. virtual bool IsPayloadEnum() override { return (mTypeDef->mTypeCode == BfTypeCode_Enum) && (!mIsTypedPrimitive); }
  1418. virtual bool IsTypedPrimitive() override { return mIsTypedPrimitive; }
  1419. virtual bool IsStructOrStructPtr() override { return mTypeDef->mTypeCode == BfTypeCode_Struct; }
  1420. virtual bool IsValueTypeOrValueTypePtr() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1421. virtual bool IsObject() override { return mTypeDef->mTypeCode == BfTypeCode_Object; }
  1422. virtual bool IsObjectOrStruct() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Struct); }
  1423. virtual bool IsObjectOrInterface() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); }
  1424. virtual BfType* GetUnderlyingType() override;
  1425. //virtual bool IsValuelessType() override { return (mIsTypedPrimitive) && (mInstSize == 0); }
  1426. virtual bool CanBeValuelessType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1427. virtual bool IsValuelessType() override;
  1428. virtual bool HasPackingHoles() override { return mHasPackingHoles; }
  1429. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) override;
  1430. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) override;
  1431. virtual bool WantsGCMarking() override;
  1432. virtual BfTypeCode GetLoweredType() override;
  1433. virtual BfTypeInstance* GetImplBaseType() { return mBaseType; }
  1434. virtual bool IsIRFuncUsed(BfIRFunction func);
  1435. void CalcHotVirtualData(/*Val128& vtHash, */Array<int>* ifaceMapping);
  1436. int GetOrigVTableSize();
  1437. int GetSelfVTableSize();
  1438. int GetOrigSelfVTableSize();
  1439. int GetBaseVTableSize();
  1440. int GetOrigBaseVTableSize();
  1441. int GetIFaceVMethodSize();
  1442. BfType* GetUnionInnerType(bool* wantSplat = NULL);
  1443. BfPrimitiveType* GetDiscriminatorType(int* outDataIdx = NULL);
  1444. bool HasEquivalentLayout(BfTypeInstance* compareTo);
  1445. BfIRConstHolder* GetOrCreateConstHolder();
  1446. BfIRValue CreateConst(BfConstant* fromConst, BfIRConstHolder* fromHolder);
  1447. int GetInstStride() { return BF_ALIGN(mInstSize, mInstAlign); }
  1448. bool HasOverrideMethods();
  1449. bool GetResultInfo(BfType*& valueType, int& okTagId);
  1450. virtual void ReportMemory(MemReporter* memReporter) override;
  1451. };
  1452. class BfBoxedType : public BfTypeInstance
  1453. {
  1454. public:
  1455. enum BoxedFlags
  1456. {
  1457. BoxedFlags_None = 0,
  1458. BoxedFlags_StructPtr = 1
  1459. };
  1460. public:
  1461. BfTypeInstance* mElementType;
  1462. BfBoxedType* mBoxedBaseType;
  1463. BoxedFlags mBoxedFlags;
  1464. public:
  1465. BfBoxedType()
  1466. {
  1467. mElementType = NULL;
  1468. mBoxedBaseType = NULL;
  1469. mBoxedFlags = BoxedFlags_None;
  1470. }
  1471. virtual bool IsBoxed() override { return true; }
  1472. virtual bool IsValueType() override { return false; }
  1473. virtual bool IsStruct() override { return false; }
  1474. virtual bool IsEnum() override { return false; }
  1475. virtual bool IsStructOrStructPtr() override { return false; }
  1476. virtual bool IsObject() override { return true; }
  1477. virtual bool IsObjectOrStruct() override { return true; }
  1478. virtual bool IsObjectOrInterface() override { return true; }
  1479. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1480. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1481. virtual BfGenericTypeInstance* ToGenericTypeInstance() override { return mElementType->ToGenericTypeInstance(); }
  1482. virtual bool IsSpecializedType() override { return !mElementType->IsUnspecializedType(); }
  1483. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1484. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1485. virtual BfTypeInstance* GetImplBaseType() override { return (mBoxedBaseType != NULL) ? mBoxedBaseType : mBaseType; }
  1486. bool IsBoxedStructPtr()
  1487. {
  1488. return (mBoxedFlags & BoxedFlags_StructPtr) != 0;
  1489. }
  1490. BfType* GetModifiedElementType();
  1491. };
  1492. class BfGenericExtensionEntry
  1493. {
  1494. public:
  1495. Array<BfGenericTypeParamInstance*> mGenericParams;
  1496. bool mConstraintsPassed;
  1497. public:
  1498. BfGenericExtensionEntry()
  1499. {
  1500. mConstraintsPassed = true;
  1501. }
  1502. ~BfGenericExtensionEntry();
  1503. };
  1504. class BfGenericExtensionInfo
  1505. {
  1506. public:
  1507. Dictionary<BfTypeDef*, BfGenericExtensionEntry> mExtensionMap;
  1508. };
  1509. // Note on nested generic types- mGenericParams is the accumulation of all generic params from outer to inner, so
  1510. // class ClassA<T> { class ClassB<T2> {} } will create a ClassA.ClassB<T, T2>
  1511. class BfGenericTypeInstance : public BfTypeInstance
  1512. {
  1513. public:
  1514. typedef Array<BfGenericTypeParamInstance*> GenericParamsVector;
  1515. Array<BfTypeReference*> mTypeGenericArgumentRefs;
  1516. BfTypeVector mTypeGenericArguments;
  1517. GenericParamsVector mGenericParams;
  1518. BfGenericExtensionInfo* mGenericExtensionInfo;
  1519. bool mIsUnspecialized;
  1520. bool mIsUnspecializedVariation;
  1521. bool mValidatedGenericConstraints;
  1522. bool mHadValidateErrors;
  1523. Array<BfProject*> mProjectsReferenced; // Generic methods that only refer to these projects don't need a specialized extension
  1524. public:
  1525. BfGenericTypeInstance()
  1526. {
  1527. mGenericExtensionInfo = NULL;
  1528. mHadValidateErrors = false;
  1529. mIsUnspecialized = false;
  1530. mIsUnspecializedVariation = false;
  1531. mValidatedGenericConstraints = false;
  1532. }
  1533. ~BfGenericTypeInstance();
  1534. GenericParamsVector* GetGenericParamsVector(BfTypeDef* declaringTypeDef);
  1535. void GenerateProjectsReferenced();
  1536. virtual BfGenericTypeInstance* ToGenericTypeInstance() override { return this; }
  1537. virtual bool IsGenericTypeInstance() override { return true; }
  1538. virtual bool IsSpecializedType() override { return !mIsUnspecialized; }
  1539. virtual bool IsSpecializedByAutoCompleteMethod() override;
  1540. virtual bool IsUnspecializedType() override { return mIsUnspecialized; }
  1541. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedVariation; }
  1542. virtual bool IsNullable() override;
  1543. virtual bool HasVarConstraints();
  1544. virtual BfType* GetUnderlyingType() override { return mIsTypedPrimitive ? BfTypeInstance::GetUnderlyingType() : mTypeGenericArguments[0]; }
  1545. virtual bool IsTypeMemberIncluded(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef, BfModule* module) override;
  1546. virtual void ReportMemory(MemReporter* memReporter) override;
  1547. };
  1548. class BfTypeAliasType : public BfTypeInstance
  1549. {
  1550. public:
  1551. BfType* mAliasToType;
  1552. public:
  1553. BfTypeAliasType()
  1554. {
  1555. mAliasToType = NULL;
  1556. }
  1557. virtual bool IsTypeAlias() override { return true; }
  1558. virtual BfType* GetUnderlyingType() override { return mAliasToType; }
  1559. };
  1560. class BfGenericTypeAliasType : public BfGenericTypeInstance
  1561. {
  1562. public:
  1563. BfType* mAliasToType;
  1564. public:
  1565. BfGenericTypeAliasType()
  1566. {
  1567. mAliasToType = NULL;
  1568. }
  1569. virtual bool IsTypeAlias() override { return true; }
  1570. virtual BfType* GetUnderlyingType() override { return mAliasToType; }
  1571. virtual bool WantsGCMarking() override { return mAliasToType->WantsGCMarking(); }
  1572. };
  1573. enum BfCaptureType
  1574. {
  1575. BfCaptureType_Value,
  1576. BfCaptureType_Reference,
  1577. BfCaptureType_Copy
  1578. };
  1579. class BfClosureType : public BfTypeInstance
  1580. {
  1581. public:
  1582. Val128 mClosureHash; // Includes base type and capture info
  1583. BfTypeInstance* mSrcDelegate;
  1584. bool mCreatedTypeDef;
  1585. String mNameAdd;
  1586. BfSource mSource;
  1587. Array<BfAstNode*> mDirectAllocNodes;
  1588. bool mIsUnique;
  1589. public:
  1590. BfClosureType(BfTypeInstance* srcDelegate, Val128 closureHash);
  1591. ~BfClosureType();
  1592. void Init(BfProject* bfProject);
  1593. BfFieldDef* AddField(BfType* type, const StringImpl& name);
  1594. BfMethodDef* AddDtor();
  1595. void Finish();
  1596. virtual bool IsClosure() override { return true; }
  1597. virtual bool IsOnDemand() override { return true; }
  1598. };
  1599. class BfDelegateType : public BfTypeInstance
  1600. {
  1601. public:
  1602. Array<BfAstNode*> mDirectAllocNodes;
  1603. // These depend on the params in Invoke
  1604. bool mIsUnspecializedType;
  1605. bool mIsUnspecializedTypeVariation;
  1606. public:
  1607. BfDelegateType()
  1608. {
  1609. mIsUnspecializedType = false;
  1610. mIsUnspecializedTypeVariation = false;
  1611. }
  1612. ~BfDelegateType();
  1613. virtual bool IsOnDemand() override { return true; }
  1614. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1615. virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }
  1616. virtual bool IsFunction() override { return !mTypeDef->mIsDelegate; }
  1617. virtual bool IsFunctionFromTypeRef() override { return !mTypeDef->mIsDelegate; }
  1618. virtual bool IsUnspecializedType() override { return mIsUnspecializedType; }
  1619. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; }
  1620. //virtual bool IsReified() override { return mIsReified; }
  1621. };
  1622. /*class BfFunctionType : public BfTypeInstance
  1623. {
  1624. public:
  1625. BfType* mReturnType;
  1626. Array<BfType*> mParamTypes;
  1627. Array<String> mParamNames;
  1628. public:
  1629. virtual bool IsOnDemand() override { return true; }
  1630. virtual bool IsFunction() override { return true; }
  1631. virtual bool IsFunctionFromTypeRef() override { return true; }
  1632. };*/
  1633. class BfTupleType : public BfTypeInstance
  1634. {
  1635. public:
  1636. bool mCreatedTypeDef;
  1637. String mNameAdd;
  1638. BfSource* mSource;
  1639. bool mHasUnspecializedMembers;
  1640. public:
  1641. BfTupleType();
  1642. ~BfTupleType();
  1643. void Init(BfProject* bfProject, BfTypeInstance* valueTypeInstance);
  1644. BfFieldDef* AddField(const StringImpl& name);
  1645. void Finish();
  1646. virtual bool IsOnDemand() override { return true; }
  1647. virtual bool IsTuple() override { return true; }
  1648. virtual bool IsUnspecializedType() override { return mHasUnspecializedMembers; }
  1649. virtual bool IsUnspecializedTypeVariation() override { return mHasUnspecializedMembers; }
  1650. };
  1651. class BfConcreteInterfaceType : public BfType
  1652. {
  1653. public:
  1654. BfTypeInstance* mInterface;
  1655. virtual bool IsConcreteInterfaceType() override { return true; }
  1656. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1657. virtual BfType* GetUnderlyingType() override { return mInterface; }
  1658. };
  1659. class BfPointerType : public BfType
  1660. {
  1661. public:
  1662. BfType* mElementType;
  1663. public:
  1664. virtual bool IsWrappableType() override { return true; }
  1665. virtual bool IsReified() override { return mElementType->IsReified(); }
  1666. virtual bool IsPointer() override { return true; }
  1667. virtual bool IsIntPtrable() override { return true; }
  1668. virtual bool IsStructPtr() override { return mElementType->IsStruct(); }
  1669. virtual bool IsStructOrStructPtr() override { return mElementType->IsStruct(); }
  1670. virtual bool IsValueTypeOrValueTypePtr() override { return mElementType->IsValueType(); }
  1671. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1672. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1673. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1674. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1675. virtual bool IsVoidPtr() override { return mElementType->IsVoid(); }
  1676. };
  1677. // This is used for direct method references for generics so we can directly call methods rather than indirectly through delegates
  1678. class BfMethodRefType : public BfDependedType
  1679. {
  1680. public:
  1681. BfMethodInstance* mMethodRef;
  1682. String mMangledMethodName;
  1683. BfTypeInstance* mOwner;
  1684. int mOwnerRevision;
  1685. bool mIsAutoCompleteMethod;
  1686. Array<int> mParamToDataIdx;
  1687. Array<int> mDataToParamIdx;
  1688. bool mIsUnspecialized;
  1689. bool mIsUnspecializedVariation;
  1690. public:
  1691. BfMethodRefType()
  1692. {
  1693. mMethodRef = NULL;
  1694. mOwner = NULL;
  1695. mOwnerRevision = -1;
  1696. mIsAutoCompleteMethod = false;
  1697. mIsUnspecialized = false;
  1698. mIsUnspecializedVariation = false;
  1699. }
  1700. ~BfMethodRefType();
  1701. //virtual bool IsValuelessType() override { return mSize != 0; }
  1702. virtual bool IsValueType() override { return true; }
  1703. virtual bool IsComposite() override { return true; }
  1704. virtual bool IsMethodRef() override { return true; }
  1705. virtual bool IsSplattable() override { return true; }
  1706. virtual int GetSplatCount() override { return (int)mDataToParamIdx.mSize; }
  1707. virtual bool IsOnDemand() override { return true; }
  1708. virtual bool IsTemporary() override { return true; }
  1709. virtual bool IsUnspecializedType() override { return mIsUnspecialized; }
  1710. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedVariation; }
  1711. int GetCaptureDataCount();
  1712. BfType* GetCaptureType(int captureDataIdx);
  1713. int GetDataIdxFromParamIdx(int paramIdx);
  1714. int GetParamIdxFromDataIdx(int dataIdx);
  1715. bool WantsDataPassedAsSplat(int dataIdx);
  1716. //virtual BfType* GetUnderlyingType() override { return mOwner; }
  1717. };
  1718. class BfRefType : public BfType
  1719. {
  1720. public:
  1721. enum RefKind
  1722. {
  1723. RefKind_Ref,
  1724. RefKind_Out,
  1725. RefKind_Mut
  1726. };
  1727. BfType* mElementType;
  1728. RefKind mRefKind;
  1729. // When an element gets rebuild, it may become valueless which makes us valueless
  1730. void CheckElement()
  1731. {
  1732. if ((mDefineState >= BfTypeDefineState_Defined) && (mElementType->mDefineState < BfTypeDefineState_Defined) && (mElementType->CanBeValuelessType()))
  1733. mDefineState = BfTypeDefineState_Declared;
  1734. }
  1735. virtual bool IsDataIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1736. virtual bool IsIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1737. virtual bool IsReified() override { return mElementType->IsReified(); }
  1738. virtual bool IsRef() override { return true; }
  1739. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1740. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1741. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1742. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1743. virtual bool CanBeValuelessType() override { return mElementType->CanBeValuelessType(); }
  1744. virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1745. };
  1746. class BfArrayType : public BfGenericTypeInstance
  1747. {
  1748. public:
  1749. int mDimensions;
  1750. public:
  1751. virtual bool IsArray() override { return true; }
  1752. virtual bool IsValueType() override { return false; }
  1753. virtual bool IsStruct() override { return false; }
  1754. virtual bool IsStructOrStructPtr() override { return false; }
  1755. virtual bool IsObject() override { return true; }
  1756. virtual bool IsObjectOrStruct() override { return true; }
  1757. virtual bool IsObjectOrInterface() override { return true; }
  1758. int GetLengthBitCount();
  1759. };
  1760. class BfSizedArrayType : public BfDependedType
  1761. {
  1762. public:
  1763. BfType* mElementType;
  1764. intptr mElementCount;
  1765. bool mWantsGCMarking;
  1766. public:
  1767. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  1768. virtual bool IsSizedArray() override { return true; }
  1769. virtual bool IsWrappableType() override { return true; }
  1770. virtual bool IsValueType() override { return true; } // Is a type of struct
  1771. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  1772. virtual bool IsComposite() override { return true; }
  1773. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  1774. virtual bool IsStructOrStructPtr() override { return false; }
  1775. virtual bool IsReified() override { return mElementType->IsReified(); }
  1776. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1777. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1778. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1779. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1780. virtual bool CanBeValuelessType() override { return true; }
  1781. virtual bool WantsGCMarking() override { BF_ASSERT(mDefineState >= BfTypeDefineState_Defined); return mWantsGCMarking; }
  1782. // Leave the default "zero sized" definition
  1783. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1784. };
  1785. // This is used when a sized array is sized by a const generic argument
  1786. class BfUnknownSizedArrayType : public BfSizedArrayType
  1787. {
  1788. public:
  1789. BfType* mElementCountSource;
  1790. public:
  1791. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  1792. virtual bool IsUnknownSizedArray() override { return true; }
  1793. virtual bool IsWrappableType() override { return true; }
  1794. virtual bool IsValueType() override { return true; } // Is a type of struct
  1795. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  1796. virtual bool IsComposite() override { return true; }
  1797. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  1798. virtual bool IsStructOrStructPtr() override { return false; }
  1799. virtual bool IsReified() override { return mElementType->IsReified(); }
  1800. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1801. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1802. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1803. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1804. virtual bool CanBeValuelessType() override { return true; }
  1805. // Leave the default "zero sized" definition
  1806. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1807. };
  1808. class BfConstExprValueType : public BfDependedType
  1809. {
  1810. public:
  1811. BfType* mType;
  1812. BfVariant mValue;
  1813. public:
  1814. virtual bool IsConstExprValue() override { return true; }
  1815. virtual BfType* GetUnderlyingType() override { return mType; }
  1816. };
  1817. /*class BfCustomAttributeArgument
  1818. {
  1819. public:
  1820. llvm::Constant* mConstParam;
  1821. };*/
  1822. class BfCustomAttributeSetProperty
  1823. {
  1824. public:
  1825. BfPropertyRef mPropertyRef;
  1826. BfTypedValue mParam;
  1827. };
  1828. class BfCustomAttributeSetField
  1829. {
  1830. public:
  1831. BfFieldRef mFieldRef;
  1832. BfTypedValue mParam;
  1833. };
  1834. class BfCustomAttribute
  1835. {
  1836. public:
  1837. BfAttributeDirective* mRef;
  1838. BfTypeInstance* mType;
  1839. BfMethodDef* mCtor;
  1840. Array<BfIRValue> mCtorArgs;
  1841. Array<BfCustomAttributeSetProperty> mSetProperties;
  1842. Array<BfCustomAttributeSetField> mSetField;
  1843. bool mAwaitingValidation;
  1844. };
  1845. class BfCustomAttributes
  1846. {
  1847. public:
  1848. Array<BfCustomAttribute> mAttributes;
  1849. bool Contains(BfTypeDef* typeDef);
  1850. BfCustomAttribute* Get(BfTypeDef* typeDef);
  1851. void ReportMemory(MemReporter* memReporter);
  1852. };
  1853. class BfResolvedTypeSetFuncs : public MultiHashSetFuncs
  1854. {
  1855. };
  1856. class BfResolvedTypeSet : public MultiHashSet<BfType*, BfResolvedTypeSetFuncs>
  1857. {
  1858. public:
  1859. enum BfHashFlags
  1860. {
  1861. BfHashFlag_None = 0,
  1862. BfHashFlag_AllowRef = 1,
  1863. BfHashFlag_AllowGenericParamConstValue = 2
  1864. };
  1865. class LookupContext
  1866. {
  1867. public:
  1868. BfModule* mModule;
  1869. BfTypeReference* mRootTypeRef;
  1870. BfTypeDef* mRootTypeDef;
  1871. BfType* mResolvedType;
  1872. bool mFailed;
  1873. public:
  1874. LookupContext()
  1875. {
  1876. mRootTypeRef = NULL;
  1877. mRootTypeDef = NULL;
  1878. mModule = NULL;
  1879. mResolvedType = NULL;
  1880. mFailed = false;
  1881. }
  1882. BfType* ResolveTypeRef(BfTypeReference* typeReference);
  1883. BfTypeDef* ResolveToTypeDef(BfTypeReference* typeReference);
  1884. };
  1885. public:
  1886. static BfVariant EvaluateToVariant(LookupContext* ctx, BfExpression* expr, BfType*& constGenericParam);
  1887. static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* lhsTypeGenericArguments, BfTypeReference* rhs, LookupContext* ctx, int& genericParamOffset);
  1888. static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* typeGenericArguments, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
  1889. static void HashGenericArguments(BfTypeReference* typeRef, LookupContext* ctx, int& hash);
  1890. static int Hash(BfType* type, LookupContext* ctx, bool allowRef = false);
  1891. static int Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
  1892. static bool Equals(BfType* lhs, BfType* rhs, LookupContext* ctx);
  1893. static bool Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx);
  1894. public:
  1895. BfResolvedTypeSet()
  1896. {
  1897. mHashSize = 9973;
  1898. mHashHeads = (Entry**)AllocateZero(sizeof(Entry*) * mHashSize, alignof(Entry*));
  1899. }
  1900. ~BfResolvedTypeSet();
  1901. template <typename T>
  1902. bool Insert(T* findType, LookupContext* ctx, BfResolvedTypeSet::Entry** entryPtr)
  1903. {
  1904. CheckRehash();
  1905. int tryCount = 0;
  1906. ctx->mFailed = false;
  1907. int hashVal = Hash(findType, ctx, BfHashFlag_AllowRef);
  1908. if (ctx->mFailed)
  1909. {
  1910. return false;
  1911. }
  1912. int bucket = (hashVal & 0x7FFFFFFF) % mHashSize;
  1913. auto checkEntry = mHashHeads[bucket];
  1914. while (checkEntry != NULL)
  1915. {
  1916. // checkEntry->mType can be NULL if we're in the process of filling it in (and this Insert is from an element type)
  1917. // OR if the type resolution failed after node insertion
  1918. if ((checkEntry->mValue != NULL) && (hashVal == checkEntry->mHash) && (Equals(checkEntry->mValue, findType, ctx)))
  1919. {
  1920. *entryPtr = checkEntry;
  1921. return false;
  1922. }
  1923. checkEntry = checkEntry->mNext;
  1924. tryCount++;
  1925. // If this fires off, this may indicate that our hashes are equivalent but Equals fails
  1926. if (tryCount >= 10)
  1927. {
  1928. NOP;
  1929. }
  1930. BF_ASSERT(tryCount < 10);
  1931. }
  1932. mCount++;
  1933. Entry* entry = (Entry*)BfResolvedTypeSetFuncs::Allocate(sizeof(Entry), alignof(Entry));
  1934. entry->mValue = NULL;
  1935. // if (mHashHeads[bucket] != NULL)
  1936. // mHashHeads[bucket]->mPrev = entry;
  1937. entry->mNext = mHashHeads[bucket];
  1938. entry->mHash = hashVal;
  1939. mHashHeads[bucket] = entry;
  1940. *entryPtr = entry;
  1941. return true;
  1942. }
  1943. // Iterator begin();
  1944. // Iterator end();
  1945. // Iterator erase(Iterator& itr);
  1946. void RemoveEntry(Entry* entry);
  1947. };
  1948. class BfTypeUtils
  1949. {
  1950. public:
  1951. static String HashEncode64(uint64 val); // Note: this only encodes 60 bits
  1952. static String TypeToString(BfTypeReference* typeRef);
  1953. static String TypeToString(BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  1954. static bool TypeToString(StringImpl& str, BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  1955. static void GetProjectList(BfType* checkType, Array<BfProject*>* projectVector, int immutableLength);
  1956. static BfPrimitiveType* GetPrimitiveType(BfModule* module, BfTypeCode typeCode);
  1957. static void PopulateType(BfModule* module, BfType* type);
  1958. template <typename T>
  1959. static void SplatIterate(const T& dataLambda, BfType* checkType)
  1960. {
  1961. auto checkTypeInstance = checkType->ToTypeInstance();
  1962. if ((checkTypeInstance != NULL) && (checkTypeInstance->IsValueType()) && (checkTypeInstance->IsDataIncomplete()))
  1963. PopulateType(checkTypeInstance->mModule, checkTypeInstance);
  1964. if (checkType->IsStruct())
  1965. {
  1966. if (checkTypeInstance->mBaseType != NULL)
  1967. SplatIterate<T>(dataLambda, checkTypeInstance->mBaseType);
  1968. if (checkTypeInstance->mIsUnion)
  1969. {
  1970. BfType* unionInnerType = checkTypeInstance->GetUnionInnerType();
  1971. SplatIterate<T>(dataLambda, unionInnerType);
  1972. }
  1973. else
  1974. {
  1975. for (int fieldIdx = 0; fieldIdx < (int)checkTypeInstance->mFieldInstances.size(); fieldIdx++)
  1976. {
  1977. auto fieldInstance = (BfFieldInstance*)&checkTypeInstance->mFieldInstances[fieldIdx];
  1978. if (fieldInstance->mDataIdx >= 0)
  1979. {
  1980. SplatIterate<T>(dataLambda, fieldInstance->GetResolvedType());
  1981. }
  1982. }
  1983. }
  1984. if (checkTypeInstance->IsEnum())
  1985. {
  1986. // Add discriminator
  1987. auto dscrType = checkTypeInstance->GetDiscriminatorType();
  1988. dataLambda(dscrType);
  1989. }
  1990. }
  1991. else if (checkType->IsMethodRef())
  1992. {
  1993. auto methodRefType = (BfMethodRefType*)checkType;
  1994. for (int dataIdx = 0; dataIdx < methodRefType->GetCaptureDataCount(); dataIdx++)
  1995. {
  1996. if (methodRefType->WantsDataPassedAsSplat(dataIdx))
  1997. SplatIterate<T>(dataLambda, methodRefType->GetCaptureType(dataIdx));
  1998. else
  1999. dataLambda(methodRefType->GetCaptureType(dataIdx));
  2000. }
  2001. }
  2002. else if (!checkType->IsValuelessType())
  2003. {
  2004. dataLambda(checkType);
  2005. }
  2006. }
  2007. static int GetSplatCount(BfType* type);
  2008. };
  2009. //void DbgCheckType(llvm::Type* checkType);
  2010. NS_BF_END
  2011. namespace std
  2012. {
  2013. template<>
  2014. struct hash<Beefy::BfTypeVector>
  2015. {
  2016. size_t operator()(const Beefy::BfTypeVector& val) const
  2017. {
  2018. int curHash = 0;
  2019. for (auto type : val)
  2020. curHash = ((curHash ^ type->mTypeId) << 5) - curHash;
  2021. return curHash;
  2022. }
  2023. };
  2024. template<>
  2025. struct hash<Beefy::BfFieldRef>
  2026. {
  2027. size_t operator()(const Beefy::BfFieldRef& fieldRef) const
  2028. {
  2029. return (size_t)fieldRef.mTypeInstance + fieldRef.mFieldIdx;
  2030. }
  2031. };
  2032. template<>
  2033. struct hash<Beefy::BfHotDevirtualizedMethod>
  2034. {
  2035. size_t operator()(const Beefy::BfHotDevirtualizedMethod& val) const
  2036. {
  2037. return (size_t)val.mMethod;
  2038. }
  2039. };
  2040. template<>
  2041. struct hash<Beefy::BfHotFunctionReference>
  2042. {
  2043. size_t operator()(const Beefy::BfHotFunctionReference& val) const
  2044. {
  2045. return (size_t)val.mMethod;
  2046. }
  2047. };
  2048. }