BfResolvedTypeUtils.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  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_Alloc = 0x40000,
  1244. BfAttributeTargets_All = 0x3FFFF
  1245. };
  1246. class BfAttributeData
  1247. {
  1248. public:
  1249. BfAttributeTargets mAttributeTargets;
  1250. bool mInherited;
  1251. bool mAllowMultiple;
  1252. public:
  1253. BfAttributeData()
  1254. {
  1255. mAttributeTargets = BfAttributeTargets_All;
  1256. mInherited = true;
  1257. mAllowMultiple = false;
  1258. }
  1259. };
  1260. class BfHotTypeData
  1261. {
  1262. public:
  1263. Array<BfHotTypeVersion*> mTypeVersions;
  1264. Array<BfHotTypeDataEntry> mVTableEntries; // Doesn't fill in until we rebuild or delete type
  1265. int mVTableOrigLength;
  1266. int mOrigInterfaceMethodsLength;
  1267. bool mPendingDataChange;
  1268. bool mHadDataChange; // True if we have EVER had a hot data change
  1269. public:
  1270. BfHotTypeData()
  1271. {
  1272. mVTableOrigLength = -1;
  1273. mOrigInterfaceMethodsLength = -1;
  1274. mPendingDataChange = false;
  1275. mHadDataChange = false;
  1276. }
  1277. ~BfHotTypeData();
  1278. BfHotTypeVersion* GetTypeVersion(int hotCommitedIdx);
  1279. BfHotTypeVersion* GetLatestVersion();
  1280. BfHotTypeVersion* GetLatestVersionHead(); // The oldest version that has the same data
  1281. void ClearVersionsAfter(int hotIdx);
  1282. };
  1283. struct BfTypeLookupEntry
  1284. {
  1285. BfAtomComposite mName;
  1286. int mNumGenericParams;
  1287. uint32 mAtomUpdateIdx;
  1288. BfTypeDef* mUseTypeDef;
  1289. bool operator==(const BfTypeLookupEntry& rhs) const
  1290. {
  1291. return (mName == rhs.mName) &&
  1292. (mNumGenericParams == rhs.mNumGenericParams) &&
  1293. (mUseTypeDef == rhs.mUseTypeDef);
  1294. }
  1295. };
  1296. NS_BF_END;
  1297. namespace std
  1298. {
  1299. template<>
  1300. struct hash<Beefy::BfTypeLookupEntry>
  1301. {
  1302. size_t operator()(const Beefy::BfTypeLookupEntry& entry) const
  1303. {
  1304. int curHash = 0;
  1305. for (int i = 0; i < entry.mName.mSize; i++)
  1306. curHash = ((curHash ^ (int)(intptr)entry.mName.mParts[i]) << 5) - curHash;
  1307. curHash = ((curHash ^ entry.mNumGenericParams) << 5) - curHash;
  1308. curHash ^= (intptr)entry.mUseTypeDef;
  1309. return curHash;
  1310. }
  1311. };
  1312. }
  1313. NS_BF_BEGIN;
  1314. struct BfTypeLookupResult
  1315. {
  1316. BfTypeDef* mTypeDef;
  1317. bool mForceLookup;
  1318. BfTypeLookupResult()
  1319. {
  1320. mTypeDef = NULL;
  1321. mForceLookup = false;
  1322. }
  1323. };
  1324. class BfDependedType : public BfType
  1325. {
  1326. public:
  1327. BfDependencyMap mDependencyMap; // This is a list of types that depend on THIS type
  1328. public:
  1329. virtual bool IsDependendType() override { return true; }
  1330. virtual BfDependedType* ToDependedType() override { return this; }
  1331. };
  1332. struct BfVirtualMethodEntry
  1333. {
  1334. BfNonGenericMethodRef mDeclaringMethod;
  1335. BfNonGenericMethodRef mImplementingMethod;
  1336. };
  1337. struct BfSpecializedMethodRefInfo
  1338. {
  1339. bool mHasReifiedRef;
  1340. BfSpecializedMethodRefInfo()
  1341. {
  1342. mHasReifiedRef = false;
  1343. }
  1344. };
  1345. class BfStaticSearch
  1346. {
  1347. public:
  1348. Array<BfTypeInstance*> mStaticTypes;
  1349. };
  1350. class BfTypeInfoEx
  1351. {
  1352. public:
  1353. BfType* mUnderlyingType;
  1354. int64 mMinValue;
  1355. int64 mMaxValue;
  1356. BfTypeInfoEx()
  1357. {
  1358. mUnderlyingType = NULL;
  1359. mMinValue = 0;
  1360. mMaxValue = 0;
  1361. }
  1362. };
  1363. // Instance of struct or class
  1364. class BfTypeInstance : public BfDependedType
  1365. {
  1366. public:
  1367. int mSignatureRevision;
  1368. int mLastNonGenericUsedRevision;
  1369. int mInheritanceId;
  1370. int mInheritanceCount;
  1371. BfModule* mModule;
  1372. BfTypeDef* mTypeDef;
  1373. BfTypeInstance* mBaseType;
  1374. BfCustomAttributes* mCustomAttributes;
  1375. BfAttributeData* mAttributeData;
  1376. BfTypeInfoEx* mTypeInfoEx;
  1377. Array<BfTypeInterfaceEntry> mInterfaces;
  1378. Array<BfTypeInterfaceMethodEntry> mInterfaceMethodTable;
  1379. Array<BfMethodInstanceGroup> mMethodInstanceGroups;
  1380. Array<BfVirtualMethodEntry> mVirtualMethodTable;
  1381. BfHotTypeData* mHotTypeData;
  1382. int mVirtualMethodTableSize; // With hot reloading, mVirtualMethodTableSize can be larger than mInterfaceMethodTable (live vtable versioning)
  1383. Array<BfFieldInstance> mFieldInstances;
  1384. Array<BfMethodInstance*> mInternalMethods;
  1385. Dictionary<BfTypeDef*, BfStaticSearch> mStaticSearchMap;
  1386. bool mHasStaticInitMethod;
  1387. bool mHasStaticDtorMethod;
  1388. bool mHasStaticMarkMethod;
  1389. bool mHasTLSFindMethod;
  1390. BfIRConstHolder* mConstHolder;
  1391. Dictionary<BfMethodRef, BfSpecializedMethodRefInfo> mSpecializedMethodReferences; // Includes both specialized methods and OnDemand methods
  1392. // 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
  1393. Dictionary<BfTypeLookupEntry, BfTypeLookupResult> mLookupResults;
  1394. int mTypeOptionsIdx;
  1395. int mMergedFieldDataCount;
  1396. int mInstAlign;
  1397. int mInstSize;
  1398. int16 mInheritDepth;
  1399. int16 mSlotNum;
  1400. bool mHasBeenInstantiated;
  1401. bool mIsReified;
  1402. bool mIsTypedPrimitive;
  1403. bool mIsCRepr;
  1404. bool mIsUnion;
  1405. bool mIsPacked;
  1406. bool mIsSplattable;
  1407. bool mTypeIncomplete;
  1408. bool mTypeFailed;
  1409. bool mTypeWarned;
  1410. bool mResolvingVarField;
  1411. bool mResolvingConstField;
  1412. bool mSkipTypeProtectionChecks;
  1413. bool mNeedsMethodProcessing;
  1414. bool mBaseTypeMayBeIncomplete;
  1415. bool mHasParameterizedBase; // Generic, array, etc
  1416. bool mIsFinishingType;
  1417. bool mHasPackingHoles;
  1418. bool mWantsGCMarking;
  1419. public:
  1420. BfTypeInstance()
  1421. {
  1422. mModule = NULL;
  1423. mSignatureRevision = -1;
  1424. mLastNonGenericUsedRevision = -1;
  1425. mInheritanceId = 0;
  1426. mInheritanceCount = 0;
  1427. mTypeDef = NULL;
  1428. mRevision = -1;
  1429. mIsReified = true;
  1430. mIsSplattable = false;
  1431. mIsPacked = false;
  1432. mBaseType = NULL;
  1433. mCustomAttributes = NULL;
  1434. mAttributeData = NULL;
  1435. mTypeInfoEx = NULL;
  1436. //mClassVData = NULL;
  1437. mVirtualMethodTableSize = 0;
  1438. mHotTypeData = NULL;
  1439. mHasStaticInitMethod = false;
  1440. mHasStaticMarkMethod = false;
  1441. mHasStaticDtorMethod = false;
  1442. mHasTLSFindMethod = false;
  1443. mSlotNum = -1;
  1444. mTypeOptionsIdx = -2; // -2 = not checked, -1 = none
  1445. mInstSize = -1;
  1446. mInstAlign = -1;
  1447. mInheritDepth = 0;
  1448. mIsTypedPrimitive = false;
  1449. mTypeIncomplete = true;
  1450. mIsCRepr = false;
  1451. mIsUnion = false;
  1452. mTypeFailed = false;
  1453. mTypeWarned = false;
  1454. mResolvingVarField = false;
  1455. mSkipTypeProtectionChecks = false;
  1456. mNeedsMethodProcessing = false;
  1457. mBaseTypeMayBeIncomplete = false;
  1458. mIsFinishingType = false;
  1459. mResolvingConstField = false;
  1460. mHasPackingHoles = false;
  1461. mHasBeenInstantiated = false;
  1462. mWantsGCMarking = false;
  1463. mHasParameterizedBase = false;
  1464. mMergedFieldDataCount = 0;
  1465. mConstHolder = NULL;
  1466. }
  1467. ~BfTypeInstance();
  1468. virtual BfModule* GetModule() override { return mModule; }
  1469. virtual BfTypeInstance* ToTypeInstance() override { return this; }
  1470. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1471. virtual BfPrimitiveType* ToPrimitiveType() override { return GetUnderlyingType()->ToPrimitiveType(); }
  1472. virtual bool HasWrappedRepresentation() override { return IsTypedPrimitive(); }
  1473. int GetEndingInstanceAlignment() { if (mInstSize % mInstAlign == 0) return mInstAlign; return mInstSize % mInstAlign; }
  1474. virtual bool HasTypeFailed() override { return mTypeFailed; }
  1475. virtual bool IsReified() override { return mIsReified; }
  1476. virtual bool NeedsExplicitAlignment() override { return !IsSizeAligned() || mIsPacked; }
  1477. virtual bool IsDataIncomplete() override { return ((mTypeIncomplete) || (mBaseTypeMayBeIncomplete)) && (!mNeedsMethodProcessing); }
  1478. virtual bool IsIncomplete() override { return (mTypeIncomplete) || (mBaseTypeMayBeIncomplete); }
  1479. virtual bool IsSplattable() override { BF_ASSERT((mInstSize >= 0) || (!IsComposite())); return mIsSplattable; }
  1480. virtual int GetSplatCount() override;
  1481. virtual bool IsTypeInstance() override { return true; }
  1482. virtual bool IsInterface() override { return mTypeDef->mTypeCode == BfTypeCode_Interface; }
  1483. virtual bool IsValueType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1484. virtual bool IsStruct() override { return ((mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum)) && (!mIsTypedPrimitive); }
  1485. virtual bool IsUnion() override { return mIsUnion; }
  1486. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1487. virtual bool IsFunction() override { return mTypeDef->mIsFunction; }
  1488. virtual bool IsString() override;
  1489. virtual bool IsIntPtrable() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); };
  1490. virtual bool IsEnum() override { return mTypeDef->mTypeCode == BfTypeCode_Enum; }
  1491. virtual bool IsPayloadEnum() override { return (mTypeDef->mTypeCode == BfTypeCode_Enum) && (!mIsTypedPrimitive); }
  1492. virtual bool IsTypedPrimitive() override { return mIsTypedPrimitive; }
  1493. virtual bool IsStructOrStructPtr() override { return mTypeDef->mTypeCode == BfTypeCode_Struct; }
  1494. virtual bool IsValueTypeOrValueTypePtr() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1495. virtual bool IsObject() override { return mTypeDef->mTypeCode == BfTypeCode_Object; }
  1496. virtual bool IsObjectOrStruct() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Struct); }
  1497. virtual bool IsObjectOrInterface() override { return (mTypeDef->mTypeCode == BfTypeCode_Object) || (mTypeDef->mTypeCode == BfTypeCode_Interface); }
  1498. virtual BfType* GetUnderlyingType() override;
  1499. //virtual bool IsValuelessType() override { return (mIsTypedPrimitive) && (mInstSize == 0); }
  1500. virtual bool CanBeValuelessType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
  1501. virtual bool IsValuelessType() override;
  1502. virtual bool HasPackingHoles() override { return mHasPackingHoles; }
  1503. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) override;
  1504. virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) override;
  1505. virtual bool WantsGCMarking() override;
  1506. virtual BfTypeCode GetLoweredType() override;
  1507. virtual BfTypeInstance* GetImplBaseType() { return mBaseType; }
  1508. virtual bool IsIRFuncUsed(BfIRFunction func);
  1509. void CalcHotVirtualData(/*Val128& vtHash, */Array<int>* ifaceMapping);
  1510. int GetOrigVTableSize();
  1511. int GetSelfVTableSize();
  1512. int GetOrigSelfVTableSize();
  1513. int GetBaseVTableSize();
  1514. int GetOrigBaseVTableSize();
  1515. int GetIFaceVMethodSize();
  1516. BfType* GetUnionInnerType(bool* wantSplat = NULL);
  1517. BfPrimitiveType* GetDiscriminatorType(int* outDataIdx = NULL);
  1518. bool HasEquivalentLayout(BfTypeInstance* compareTo);
  1519. BfIRConstHolder* GetOrCreateConstHolder();
  1520. BfIRValue CreateConst(BfConstant* fromConst, BfIRConstHolder* fromHolder);
  1521. int GetInstStride() { return BF_ALIGN(mInstSize, mInstAlign); }
  1522. bool HasOverrideMethods();
  1523. bool GetResultInfo(BfType*& valueType, int& okTagId);
  1524. virtual void ReportMemory(MemReporter* memReporter) override;
  1525. };
  1526. class BfBoxedType : public BfTypeInstance
  1527. {
  1528. public:
  1529. enum BoxedFlags
  1530. {
  1531. BoxedFlags_None = 0,
  1532. BoxedFlags_StructPtr = 1
  1533. };
  1534. public:
  1535. BfTypeInstance* mElementType;
  1536. BfBoxedType* mBoxedBaseType;
  1537. BoxedFlags mBoxedFlags;
  1538. public:
  1539. BfBoxedType()
  1540. {
  1541. mElementType = NULL;
  1542. mBoxedBaseType = NULL;
  1543. mBoxedFlags = BoxedFlags_None;
  1544. }
  1545. virtual bool IsBoxed() override { return true; }
  1546. virtual bool IsValueType() override { return false; }
  1547. virtual bool IsStruct() override { return false; }
  1548. virtual bool IsEnum() override { return false; }
  1549. virtual bool IsStructOrStructPtr() override { return false; }
  1550. virtual bool IsObject() override { return true; }
  1551. virtual bool IsObjectOrStruct() override { return true; }
  1552. virtual bool IsObjectOrInterface() override { return true; }
  1553. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1554. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1555. virtual BfGenericTypeInstance* ToGenericTypeInstance() override { return mElementType->ToGenericTypeInstance(); }
  1556. virtual bool IsSpecializedType() override { return !mElementType->IsUnspecializedType(); }
  1557. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1558. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1559. virtual BfTypeInstance* GetImplBaseType() override { return (mBoxedBaseType != NULL) ? mBoxedBaseType : mBaseType; }
  1560. bool IsBoxedStructPtr()
  1561. {
  1562. return (mBoxedFlags & BoxedFlags_StructPtr) != 0;
  1563. }
  1564. BfType* GetModifiedElementType();
  1565. };
  1566. class BfGenericExtensionEntry
  1567. {
  1568. public:
  1569. Array<BfGenericTypeParamInstance*> mGenericParams;
  1570. bool mConstraintsPassed;
  1571. public:
  1572. BfGenericExtensionEntry()
  1573. {
  1574. mConstraintsPassed = true;
  1575. }
  1576. ~BfGenericExtensionEntry();
  1577. };
  1578. class BfGenericExtensionInfo
  1579. {
  1580. public:
  1581. Dictionary<BfTypeDef*, BfGenericExtensionEntry> mExtensionMap;
  1582. };
  1583. // Note on nested generic types- mGenericParams is the accumulation of all generic params from outer to inner, so
  1584. // class ClassA<T> { class ClassB<T2> {} } will create a ClassA.ClassB<T, T2>
  1585. class BfGenericTypeInstance : public BfTypeInstance
  1586. {
  1587. public:
  1588. typedef Array<BfGenericTypeParamInstance*> GenericParamsVector;
  1589. Array<BfTypeReference*> mTypeGenericArgumentRefs;
  1590. BfTypeVector mTypeGenericArguments;
  1591. GenericParamsVector mGenericParams;
  1592. BfGenericExtensionInfo* mGenericExtensionInfo;
  1593. bool mIsUnspecialized;
  1594. bool mIsUnspecializedVariation;
  1595. bool mValidatedGenericConstraints;
  1596. bool mHadValidateErrors;
  1597. Array<BfProject*> mProjectsReferenced; // Generic methods that only refer to these projects don't need a specialized extension
  1598. public:
  1599. BfGenericTypeInstance()
  1600. {
  1601. mGenericExtensionInfo = NULL;
  1602. mHadValidateErrors = false;
  1603. mIsUnspecialized = false;
  1604. mIsUnspecializedVariation = false;
  1605. mValidatedGenericConstraints = false;
  1606. }
  1607. ~BfGenericTypeInstance();
  1608. GenericParamsVector* GetGenericParamsVector(BfTypeDef* declaringTypeDef);
  1609. void GenerateProjectsReferenced();
  1610. virtual BfGenericTypeInstance* ToGenericTypeInstance() override { return this; }
  1611. virtual bool IsGenericTypeInstance() override { return true; }
  1612. virtual bool IsSpecializedType() override { return !mIsUnspecialized; }
  1613. virtual bool IsSpecializedByAutoCompleteMethod() override;
  1614. virtual bool IsUnspecializedType() override { return mIsUnspecialized; }
  1615. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedVariation; }
  1616. virtual bool IsNullable() override;
  1617. virtual bool HasVarConstraints();
  1618. virtual BfType* GetUnderlyingType() override { return mIsTypedPrimitive ? BfTypeInstance::GetUnderlyingType() : mTypeGenericArguments[0]; }
  1619. virtual bool IsTypeMemberIncluded(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef, BfModule* module) override;
  1620. virtual void ReportMemory(MemReporter* memReporter) override;
  1621. };
  1622. class BfTypeAliasType : public BfTypeInstance
  1623. {
  1624. public:
  1625. BfType* mAliasToType;
  1626. public:
  1627. BfTypeAliasType()
  1628. {
  1629. mAliasToType = NULL;
  1630. }
  1631. virtual bool IsTypeAlias() override { return true; }
  1632. virtual BfType* GetUnderlyingType() override { return mAliasToType; }
  1633. };
  1634. class BfGenericTypeAliasType : public BfGenericTypeInstance
  1635. {
  1636. public:
  1637. BfType* mAliasToType;
  1638. public:
  1639. BfGenericTypeAliasType()
  1640. {
  1641. mAliasToType = NULL;
  1642. }
  1643. virtual bool IsTypeAlias() override { return true; }
  1644. virtual BfType* GetUnderlyingType() override { return mAliasToType; }
  1645. virtual bool WantsGCMarking() override { return mAliasToType->WantsGCMarking(); }
  1646. };
  1647. enum BfCaptureType
  1648. {
  1649. BfCaptureType_None,
  1650. BfCaptureType_Copy,
  1651. BfCaptureType_Reference,
  1652. };
  1653. class BfClosureType : public BfTypeInstance
  1654. {
  1655. public:
  1656. Val128 mClosureHash; // Includes base type and capture info
  1657. BfTypeInstance* mSrcDelegate;
  1658. bool mCreatedTypeDef;
  1659. String mNameAdd;
  1660. BfSource mSource;
  1661. Array<BfAstNode*> mDirectAllocNodes;
  1662. bool mIsUnique;
  1663. public:
  1664. BfClosureType(BfTypeInstance* srcDelegate, Val128 closureHash);
  1665. ~BfClosureType();
  1666. void Init(BfProject* bfProject);
  1667. BfFieldDef* AddField(BfType* type, const StringImpl& name);
  1668. BfMethodDef* AddDtor();
  1669. void Finish();
  1670. virtual bool IsClosure() override { return true; }
  1671. virtual bool IsOnDemand() override { return true; }
  1672. };
  1673. class BfDelegateType : public BfTypeInstance
  1674. {
  1675. public:
  1676. Array<BfAstNode*> mDirectAllocNodes;
  1677. // These depend on the params in Invoke
  1678. bool mIsUnspecializedType;
  1679. bool mIsUnspecializedTypeVariation;
  1680. public:
  1681. BfDelegateType()
  1682. {
  1683. mIsUnspecializedType = false;
  1684. mIsUnspecializedTypeVariation = false;
  1685. }
  1686. ~BfDelegateType();
  1687. virtual bool IsOnDemand() override { return true; }
  1688. virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
  1689. virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }
  1690. virtual bool IsFunction() override { return !mTypeDef->mIsDelegate; }
  1691. virtual bool IsFunctionFromTypeRef() override { return !mTypeDef->mIsDelegate; }
  1692. virtual bool IsUnspecializedType() override { return mIsUnspecializedType; }
  1693. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; }
  1694. //virtual bool IsReified() override { return mIsReified; }
  1695. };
  1696. /*class BfFunctionType : public BfTypeInstance
  1697. {
  1698. public:
  1699. BfType* mReturnType;
  1700. Array<BfType*> mParamTypes;
  1701. Array<String> mParamNames;
  1702. public:
  1703. virtual bool IsOnDemand() override { return true; }
  1704. virtual bool IsFunction() override { return true; }
  1705. virtual bool IsFunctionFromTypeRef() override { return true; }
  1706. };*/
  1707. class BfTupleType : public BfTypeInstance
  1708. {
  1709. public:
  1710. bool mCreatedTypeDef;
  1711. String mNameAdd;
  1712. BfSource* mSource;
  1713. bool mHasUnspecializedMembers;
  1714. public:
  1715. BfTupleType();
  1716. ~BfTupleType();
  1717. void Init(BfProject* bfProject, BfTypeInstance* valueTypeInstance);
  1718. BfFieldDef* AddField(const StringImpl& name);
  1719. void Finish();
  1720. virtual bool IsOnDemand() override { return true; }
  1721. virtual bool IsTuple() override { return true; }
  1722. virtual bool IsUnspecializedType() override { return mHasUnspecializedMembers; }
  1723. virtual bool IsUnspecializedTypeVariation() override { return mHasUnspecializedMembers; }
  1724. };
  1725. class BfConcreteInterfaceType : public BfType
  1726. {
  1727. public:
  1728. BfTypeInstance* mInterface;
  1729. virtual bool IsConcreteInterfaceType() override { return true; }
  1730. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1731. virtual BfType* GetUnderlyingType() override { return mInterface; }
  1732. };
  1733. class BfPointerType : public BfType
  1734. {
  1735. public:
  1736. BfType* mElementType;
  1737. public:
  1738. virtual bool IsWrappableType() override { return true; }
  1739. virtual bool IsReified() override { return mElementType->IsReified(); }
  1740. virtual bool IsPointer() override { return true; }
  1741. virtual bool IsIntPtrable() override { return true; }
  1742. virtual bool IsStructPtr() override { return mElementType->IsStruct(); }
  1743. virtual bool IsStructOrStructPtr() override { return mElementType->IsStruct(); }
  1744. virtual bool IsValueTypeOrValueTypePtr() override { return mElementType->IsValueType(); }
  1745. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1746. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1747. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1748. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1749. virtual bool IsVoidPtr() override { return mElementType->IsVoid(); }
  1750. };
  1751. // This is used for direct method references for generics so we can directly call methods rather than indirectly through delegates
  1752. class BfMethodRefType : public BfDependedType
  1753. {
  1754. public:
  1755. BfMethodInstance* mMethodRef;
  1756. String mMangledMethodName;
  1757. BfTypeInstance* mOwner;
  1758. int mOwnerRevision;
  1759. bool mIsAutoCompleteMethod;
  1760. Array<int> mParamToDataIdx;
  1761. Array<int> mDataToParamIdx;
  1762. bool mIsUnspecialized;
  1763. bool mIsUnspecializedVariation;
  1764. public:
  1765. BfMethodRefType()
  1766. {
  1767. mMethodRef = NULL;
  1768. mOwner = NULL;
  1769. mOwnerRevision = -1;
  1770. mIsAutoCompleteMethod = false;
  1771. mIsUnspecialized = false;
  1772. mIsUnspecializedVariation = false;
  1773. }
  1774. ~BfMethodRefType();
  1775. //virtual bool IsValuelessType() override { return mSize != 0; }
  1776. virtual bool IsValueType() override { return true; }
  1777. virtual bool IsComposite() override { return true; }
  1778. virtual bool IsMethodRef() override { return true; }
  1779. virtual bool IsSplattable() override { return true; }
  1780. virtual int GetSplatCount() override { return (int)mDataToParamIdx.mSize; }
  1781. virtual bool IsOnDemand() override { return true; }
  1782. virtual bool IsTemporary() override { return true; }
  1783. virtual bool IsUnspecializedType() override { return mIsUnspecialized; }
  1784. virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedVariation; }
  1785. int GetCaptureDataCount();
  1786. BfType* GetCaptureType(int captureDataIdx);
  1787. int GetDataIdxFromParamIdx(int paramIdx);
  1788. int GetParamIdxFromDataIdx(int dataIdx);
  1789. bool WantsDataPassedAsSplat(int dataIdx);
  1790. //virtual BfType* GetUnderlyingType() override { return mOwner; }
  1791. };
  1792. class BfRefType : public BfType
  1793. {
  1794. public:
  1795. enum RefKind
  1796. {
  1797. RefKind_Ref,
  1798. RefKind_Out,
  1799. RefKind_Mut
  1800. };
  1801. BfType* mElementType;
  1802. RefKind mRefKind;
  1803. // When an element gets rebuild, it may become valueless which makes us valueless
  1804. void CheckElement()
  1805. {
  1806. if ((mDefineState >= BfTypeDefineState_Defined) && (mElementType->mDefineState < BfTypeDefineState_Defined) && (mElementType->CanBeValuelessType()))
  1807. mDefineState = BfTypeDefineState_Declared;
  1808. }
  1809. virtual bool IsDataIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1810. virtual bool IsIncomplete() override { CheckElement(); return mDefineState < BfTypeDefineState_Defined; }
  1811. virtual bool IsReified() override { return mElementType->IsReified(); }
  1812. virtual bool IsRef() override { return true; }
  1813. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1814. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1815. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1816. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1817. virtual bool CanBeValuelessType() override { return mElementType->CanBeValuelessType(); }
  1818. virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1819. };
  1820. class BfArrayType : public BfGenericTypeInstance
  1821. {
  1822. public:
  1823. int mDimensions;
  1824. public:
  1825. virtual bool IsArray() override { return true; }
  1826. virtual bool IsValueType() override { return false; }
  1827. virtual bool IsStruct() override { return false; }
  1828. virtual bool IsStructOrStructPtr() override { return false; }
  1829. virtual bool IsObject() override { return true; }
  1830. virtual bool IsObjectOrStruct() override { return true; }
  1831. virtual bool IsObjectOrInterface() override { return true; }
  1832. int GetLengthBitCount();
  1833. };
  1834. class BfSizedArrayType : public BfDependedType
  1835. {
  1836. public:
  1837. BfType* mElementType;
  1838. intptr mElementCount;
  1839. bool mWantsGCMarking;
  1840. public:
  1841. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  1842. virtual bool IsSizedArray() override { return true; }
  1843. virtual bool IsWrappableType() override { return true; }
  1844. virtual bool IsValueType() override { return true; } // Is a type of struct
  1845. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  1846. virtual bool IsComposite() override { return true; }
  1847. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  1848. virtual bool IsStructOrStructPtr() override { return false; }
  1849. virtual bool IsReified() override { return mElementType->IsReified(); }
  1850. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1851. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1852. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1853. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1854. virtual bool CanBeValuelessType() override { return true; }
  1855. virtual bool WantsGCMarking() override { BF_ASSERT(mDefineState >= BfTypeDefineState_Defined); return mWantsGCMarking; }
  1856. // Leave the default "zero sized" definition
  1857. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1858. };
  1859. // This is used when a sized array is sized by a const generic argument
  1860. class BfUnknownSizedArrayType : public BfSizedArrayType
  1861. {
  1862. public:
  1863. BfType* mElementCountSource;
  1864. public:
  1865. virtual bool NeedsExplicitAlignment() override { return mElementType->NeedsExplicitAlignment(); }
  1866. virtual bool IsUnknownSizedArray() override { return true; }
  1867. virtual bool IsWrappableType() override { return true; }
  1868. virtual bool IsValueType() override { return true; } // Is a type of struct
  1869. virtual bool IsValueTypeOrValueTypePtr() override { return true; }
  1870. virtual bool IsComposite() override { return true; }
  1871. virtual bool IsStruct() override { return false; } // But IsStruct infers a definition, which it does not have
  1872. virtual bool IsStructOrStructPtr() override { return false; }
  1873. virtual bool IsReified() override { return mElementType->IsReified(); }
  1874. virtual bool IsDependentOnUnderlyingType() override { return true; }
  1875. virtual BfType* GetUnderlyingType() override { return mElementType; }
  1876. virtual bool IsUnspecializedType() override { return mElementType->IsUnspecializedType(); }
  1877. virtual bool IsUnspecializedTypeVariation() override { return mElementType->IsUnspecializedTypeVariation(); }
  1878. virtual bool CanBeValuelessType() override { return true; }
  1879. // Leave the default "zero sized" definition
  1880. //virtual bool IsValuelessType() override { return mElementType->IsValuelessType(); }
  1881. };
  1882. class BfConstExprValueType : public BfDependedType
  1883. {
  1884. public:
  1885. BfType* mType;
  1886. BfVariant mValue;
  1887. public:
  1888. virtual bool IsConstExprValue() override { return true; }
  1889. virtual BfType* GetUnderlyingType() override { return mType; }
  1890. };
  1891. /*class BfCustomAttributeArgument
  1892. {
  1893. public:
  1894. llvm::Constant* mConstParam;
  1895. };*/
  1896. class BfCustomAttributeSetProperty
  1897. {
  1898. public:
  1899. BfPropertyRef mPropertyRef;
  1900. BfTypedValue mParam;
  1901. };
  1902. class BfCustomAttributeSetField
  1903. {
  1904. public:
  1905. BfFieldRef mFieldRef;
  1906. BfTypedValue mParam;
  1907. };
  1908. class BfCustomAttribute
  1909. {
  1910. public:
  1911. BfAttributeDirective* mRef;
  1912. BfTypeInstance* mType;
  1913. BfMethodDef* mCtor;
  1914. Array<BfIRValue> mCtorArgs;
  1915. Array<BfCustomAttributeSetProperty> mSetProperties;
  1916. Array<BfCustomAttributeSetField> mSetField;
  1917. bool mAwaitingValidation;
  1918. };
  1919. class BfCustomAttributes
  1920. {
  1921. public:
  1922. Array<BfCustomAttribute> mAttributes;
  1923. bool Contains(BfTypeDef* typeDef);
  1924. BfCustomAttribute* Get(BfTypeDef* typeDef);
  1925. void ReportMemory(MemReporter* memReporter);
  1926. };
  1927. class BfResolvedTypeSetFuncs : public MultiHashSetFuncs
  1928. {
  1929. };
  1930. class BfResolvedTypeSet : public MultiHashSet<BfType*, BfResolvedTypeSetFuncs>
  1931. {
  1932. public:
  1933. enum BfHashFlags
  1934. {
  1935. BfHashFlag_None = 0,
  1936. BfHashFlag_AllowRef = 1,
  1937. BfHashFlag_AllowGenericParamConstValue = 2
  1938. };
  1939. class LookupContext
  1940. {
  1941. public:
  1942. BfModule* mModule;
  1943. BfTypeReference* mRootTypeRef;
  1944. BfTypeDef* mRootTypeDef;
  1945. BfType* mResolvedType;
  1946. bool mFailed;
  1947. public:
  1948. LookupContext()
  1949. {
  1950. mRootTypeRef = NULL;
  1951. mRootTypeDef = NULL;
  1952. mModule = NULL;
  1953. mResolvedType = NULL;
  1954. mFailed = false;
  1955. }
  1956. BfType* ResolveTypeRef(BfTypeReference* typeReference);
  1957. BfTypeDef* ResolveToTypeDef(BfTypeReference* typeReference);
  1958. };
  1959. public:
  1960. static BfVariant EvaluateToVariant(LookupContext* ctx, BfExpression* expr, BfType*& constGenericParam);
  1961. static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* lhsTypeGenericArguments, BfTypeReference* rhs, LookupContext* ctx, int& genericParamOffset);
  1962. static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* typeGenericArguments, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
  1963. static void HashGenericArguments(BfTypeReference* typeRef, LookupContext* ctx, int& hash);
  1964. static int Hash(BfType* type, LookupContext* ctx, bool allowRef = false);
  1965. static int Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
  1966. static bool Equals(BfType* lhs, BfType* rhs, LookupContext* ctx);
  1967. static bool Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx);
  1968. public:
  1969. BfResolvedTypeSet()
  1970. {
  1971. mHashSize = 9973;
  1972. mHashHeads = (Entry**)AllocateZero(sizeof(Entry*) * mHashSize, alignof(Entry*));
  1973. }
  1974. ~BfResolvedTypeSet();
  1975. template <typename T>
  1976. bool Insert(T* findType, LookupContext* ctx, BfResolvedTypeSet::Entry** entryPtr)
  1977. {
  1978. CheckRehash();
  1979. int tryCount = 0;
  1980. ctx->mFailed = false;
  1981. int hashVal = Hash(findType, ctx, BfHashFlag_AllowRef);
  1982. if (ctx->mFailed)
  1983. {
  1984. return false;
  1985. }
  1986. int bucket = (hashVal & 0x7FFFFFFF) % mHashSize;
  1987. auto checkEntry = mHashHeads[bucket];
  1988. while (checkEntry != NULL)
  1989. {
  1990. // checkEntry->mType can be NULL if we're in the process of filling it in (and this Insert is from an element type)
  1991. // OR if the type resolution failed after node insertion
  1992. if ((checkEntry->mValue != NULL) && (hashVal == checkEntry->mHash) && (Equals(checkEntry->mValue, findType, ctx)))
  1993. {
  1994. *entryPtr = checkEntry;
  1995. return false;
  1996. }
  1997. checkEntry = checkEntry->mNext;
  1998. tryCount++;
  1999. // If this fires off, this may indicate that our hashes are equivalent but Equals fails
  2000. if (tryCount >= 10)
  2001. {
  2002. NOP;
  2003. }
  2004. BF_ASSERT(tryCount < 10);
  2005. }
  2006. mCount++;
  2007. Entry* entry = (Entry*)BfResolvedTypeSetFuncs::Allocate(sizeof(Entry), alignof(Entry));
  2008. entry->mValue = NULL;
  2009. // if (mHashHeads[bucket] != NULL)
  2010. // mHashHeads[bucket]->mPrev = entry;
  2011. entry->mNext = mHashHeads[bucket];
  2012. entry->mHash = hashVal;
  2013. mHashHeads[bucket] = entry;
  2014. *entryPtr = entry;
  2015. return true;
  2016. }
  2017. // Iterator begin();
  2018. // Iterator end();
  2019. // Iterator erase(Iterator& itr);
  2020. void RemoveEntry(Entry* entry);
  2021. };
  2022. class BfTypeUtils
  2023. {
  2024. public:
  2025. static String HashEncode64(uint64 val); // Note: this only encodes 60 bits
  2026. static String TypeToString(BfTypeReference* typeRef);
  2027. static String TypeToString(BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  2028. static bool TypeToString(StringImpl& str, BfTypeDef* typeDef, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None);
  2029. static bool TypeEquals(BfType* typeA, BfType* typeB, BfType* selfType);
  2030. template <typename T>
  2031. static void GetProjectList(BfType* checkType, T* projectList, int immutableLength)
  2032. {
  2033. if (checkType->IsBoxed())
  2034. GetProjectList(((BfBoxedType*)checkType)->mElementType, projectList, immutableLength);
  2035. BfTypeInstance* typeInst = checkType->ToTypeInstance();
  2036. if (typeInst != NULL)
  2037. {
  2038. auto genericTypeInst = typeInst->ToGenericTypeInstance();
  2039. if (genericTypeInst != NULL)
  2040. {
  2041. for (auto genericArg : genericTypeInst->mTypeGenericArguments)
  2042. GetProjectList(genericArg, projectList, immutableLength);
  2043. }
  2044. BfProject* bfProject = typeInst->mTypeDef->mProject;
  2045. if (!projectList->Contains(bfProject))
  2046. {
  2047. bool handled = false;
  2048. for (int idx = 0; idx < (int)projectList->size(); idx++)
  2049. {
  2050. auto checkProject = (*projectList)[idx];
  2051. bool isBetter = bfProject->ContainsReference(checkProject);
  2052. bool isWorse = checkProject->ContainsReference(bfProject);
  2053. if (isBetter == isWorse)
  2054. continue;
  2055. if (isBetter)
  2056. {
  2057. if (idx >= immutableLength)
  2058. {
  2059. // This is even more specific, so replace with this one
  2060. (*projectList)[idx] = bfProject;
  2061. handled = true;
  2062. }
  2063. }
  2064. else
  2065. {
  2066. // This is less specific, ignore
  2067. handled = true;
  2068. }
  2069. break;
  2070. }
  2071. if (!handled)
  2072. {
  2073. projectList->Add(bfProject);
  2074. }
  2075. }
  2076. }
  2077. else if (checkType->IsPointer())
  2078. GetProjectList(((BfPointerType*)checkType)->mElementType, projectList, immutableLength);
  2079. else if (checkType->IsRef())
  2080. GetProjectList(((BfPointerType*)checkType)->mElementType, projectList, immutableLength);
  2081. else if (checkType->IsSizedArray())
  2082. GetProjectList(((BfSizedArrayType*)checkType)->mElementType, projectList, immutableLength);
  2083. else if (checkType->IsMethodRef())
  2084. GetProjectList(((BfMethodRefType*)checkType)->mOwner, projectList, immutableLength);
  2085. }
  2086. static BfPrimitiveType* GetPrimitiveType(BfModule* module, BfTypeCode typeCode);
  2087. static void PopulateType(BfModule* module, BfType* type);
  2088. template <typename T>
  2089. static void SplatIterate(const T& dataLambda, BfType* checkType)
  2090. {
  2091. auto checkTypeInstance = checkType->ToTypeInstance();
  2092. if ((checkTypeInstance != NULL) && (checkTypeInstance->IsValueType()) && (checkTypeInstance->IsDataIncomplete()))
  2093. PopulateType(checkTypeInstance->mModule, checkTypeInstance);
  2094. if (checkType->IsStruct())
  2095. {
  2096. if (checkTypeInstance->mBaseType != NULL)
  2097. SplatIterate<T>(dataLambda, checkTypeInstance->mBaseType);
  2098. if (checkTypeInstance->mIsUnion)
  2099. {
  2100. BfType* unionInnerType = checkTypeInstance->GetUnionInnerType();
  2101. SplatIterate<T>(dataLambda, unionInnerType);
  2102. }
  2103. else
  2104. {
  2105. for (int fieldIdx = 0; fieldIdx < (int)checkTypeInstance->mFieldInstances.size(); fieldIdx++)
  2106. {
  2107. auto fieldInstance = (BfFieldInstance*)&checkTypeInstance->mFieldInstances[fieldIdx];
  2108. if (fieldInstance->mDataIdx >= 0)
  2109. {
  2110. SplatIterate<T>(dataLambda, fieldInstance->GetResolvedType());
  2111. }
  2112. }
  2113. }
  2114. if (checkTypeInstance->IsEnum())
  2115. {
  2116. // Add discriminator
  2117. auto dscrType = checkTypeInstance->GetDiscriminatorType();
  2118. dataLambda(dscrType);
  2119. }
  2120. }
  2121. else if (checkType->IsMethodRef())
  2122. {
  2123. auto methodRefType = (BfMethodRefType*)checkType;
  2124. for (int dataIdx = 0; dataIdx < methodRefType->GetCaptureDataCount(); dataIdx++)
  2125. {
  2126. if (methodRefType->WantsDataPassedAsSplat(dataIdx))
  2127. SplatIterate<T>(dataLambda, methodRefType->GetCaptureType(dataIdx));
  2128. else
  2129. dataLambda(methodRefType->GetCaptureType(dataIdx));
  2130. }
  2131. }
  2132. else if (!checkType->IsValuelessType())
  2133. {
  2134. dataLambda(checkType);
  2135. }
  2136. }
  2137. static int GetSplatCount(BfType* type);
  2138. };
  2139. //void DbgCheckType(llvm::Type* checkType);
  2140. NS_BF_END
  2141. namespace std
  2142. {
  2143. template<>
  2144. struct hash<Beefy::BfTypeVector>
  2145. {
  2146. size_t operator()(const Beefy::BfTypeVector& val) const
  2147. {
  2148. int curHash = 0;
  2149. for (auto type : val)
  2150. curHash = ((curHash ^ type->mTypeId) << 5) - curHash;
  2151. return curHash;
  2152. }
  2153. };
  2154. template<>
  2155. struct hash<Beefy::BfFieldRef>
  2156. {
  2157. size_t operator()(const Beefy::BfFieldRef& fieldRef) const
  2158. {
  2159. return (size_t)fieldRef.mTypeInstance + fieldRef.mFieldIdx;
  2160. }
  2161. };
  2162. template<>
  2163. struct hash<Beefy::BfHotDevirtualizedMethod>
  2164. {
  2165. size_t operator()(const Beefy::BfHotDevirtualizedMethod& val) const
  2166. {
  2167. return (size_t)val.mMethod;
  2168. }
  2169. };
  2170. template<>
  2171. struct hash<Beefy::BfHotFunctionReference>
  2172. {
  2173. size_t operator()(const Beefy::BfHotFunctionReference& val) const
  2174. {
  2175. return (size_t)val.mMethod;
  2176. }
  2177. };
  2178. }