BfSystem.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/util/CritSect.h"
  4. #include "BeefySysLib/util/Hash.h"
  5. #include "BeefySysLib/util/HashSet.h"
  6. #include "BeefySysLib/util/Deque.h"
  7. #include "BeefySysLib/util/BumpAllocator.h"
  8. #include "BeefySysLib/util/MultiHashSet.h"
  9. #include "../Beef/BfCommon.h"
  10. #include "BfAst.h"
  11. #include "BfUtil.h"
  12. #include <unordered_map>
  13. #include <unordered_set>
  14. #include <set>
  15. #include "MemReporter.h"
  16. namespace llvm
  17. {
  18. class Type;
  19. class Function;
  20. }
  21. #define BF_NEW_INT_TYPES
  22. #ifdef BF_NEW_INT_TYPES
  23. #define BF_INT32_NAME "int32"
  24. #else
  25. #define BF_INT32_NAME "int"
  26. #endif
  27. #ifdef BF_PLATFORM_WINDOWS
  28. #define BF_OBJ_EXT ".obj"
  29. #else
  30. #define BF_OBJ_EXT ".o"
  31. #endif
  32. NS_BF_BEGIN
  33. class BfSystem;
  34. class BfCompiler;
  35. class BfTypeReference;
  36. class BfProject;
  37. class BfTypeDef;
  38. struct BfTypeDefMapFuncs;
  39. typedef HashSet<BfProject*> BfProjectSet;
  40. class BfAtom
  41. {
  42. public:
  43. enum Kind
  44. {
  45. Kind_Normal,
  46. Kind_Anon
  47. };
  48. public:
  49. StringView mString;
  50. Kind mKind;
  51. int mRefCount;
  52. int mPendingDerefCount;
  53. int mHash;
  54. uint32 mAtomUpdateIdx;
  55. bool mIsSystemType;
  56. Dictionary<BfAtom*, int> mPrevNamesMap;
  57. public:
  58. ~BfAtom();
  59. const StringView& ToString()
  60. {
  61. return mString;
  62. }
  63. void ToString(StringImpl& str)
  64. {
  65. str += mString;
  66. }
  67. void Ref();
  68. };
  69. class BfAtomComposite
  70. {
  71. public:
  72. BfAtom** mParts;
  73. int16 mSize;
  74. int16 mAllocSize;
  75. bool mOwns;
  76. public:
  77. BfAtomComposite();
  78. BfAtomComposite(BfAtomComposite&& rhs);
  79. BfAtomComposite(const BfAtomComposite& rhs);
  80. BfAtomComposite(BfAtom* atom);
  81. BfAtomComposite(const BfAtomComposite& left, const BfAtomComposite& right);
  82. BfAtomComposite(const BfAtomComposite& left, BfAtom* right);
  83. ~BfAtomComposite();
  84. void Set(const BfAtomComposite& left, const BfAtomComposite& right);
  85. void Set(BfAtom** atomsA, int countA, BfAtom** atomsB, int countB);
  86. BfAtomComposite& operator=(const BfAtomComposite& rhs);
  87. bool operator==(const BfAtomComposite& other) const;
  88. bool operator!=(const BfAtomComposite& other) const;
  89. bool IsValid() const;
  90. bool IsEmpty() const;
  91. int GetPartsCount() const;
  92. String ToString() const;
  93. void ToString(StringImpl& str) const;
  94. bool StartsWith(const BfAtomComposite& other) const;
  95. bool EndsWith(const BfAtomComposite& other) const;
  96. BfAtomComposite GetSub(int start, int len) const;
  97. void Reference(const BfAtomComposite& other);
  98. uint32 GetAtomUpdateIdx();
  99. };
  100. template <const int TBufSize>
  101. class BfAtomCompositeT : public BfAtomComposite
  102. {
  103. public:
  104. BfAtom* mInternalBuffer[TBufSize];
  105. public:
  106. BfAtomCompositeT()
  107. {
  108. mAllocSize = (int16)TBufSize;
  109. mParts = mInternalBuffer;
  110. }
  111. BfAtomCompositeT(const BfAtomComposite& rhs)
  112. {
  113. mAllocSize = (int16)TBufSize;
  114. mParts = mInternalBuffer;
  115. *this = rhs;
  116. }
  117. BfAtomCompositeT& operator=(const BfAtomComposite& rhs)
  118. {
  119. Set(rhs.mParts, rhs.mSize, NULL, 0);
  120. return *this;
  121. }
  122. };
  123. class BfSizedAtomComposite : public BfAtomComposite
  124. {
  125. public:
  126. BfAtom* mInitialAlloc[8];
  127. BfSizedAtomComposite();
  128. ~BfSizedAtomComposite();
  129. };
  130. struct BfAtomCompositeHash
  131. {
  132. size_t operator()(const BfAtomComposite& composite) const
  133. {
  134. int curHash = 0;
  135. for (int i = 0; i < (int)composite.mSize; i++)
  136. curHash = ((curHash ^ (int)(intptr)composite.mParts[i]->mHash) << 5) - curHash;
  137. return curHash;
  138. }
  139. };
  140. struct BfAtomCompositeEquals
  141. {
  142. bool operator()(const BfAtomComposite& lhs, const BfAtomComposite& rhs) const
  143. {
  144. if (lhs.mSize != rhs.mSize)
  145. return false;
  146. for (int i = 0; i < lhs.mSize; i++)
  147. if (lhs.mParts[i] != rhs.mParts[i])
  148. return false;
  149. return true;
  150. }
  151. };
  152. enum BfFailHandleKind
  153. {
  154. BfFailHandleKind_Normal,
  155. BfFailHandleKind_Soft,
  156. BfFailHandleKind_Ignore
  157. };
  158. enum BfWhileSpecializingFlags : int8
  159. {
  160. BfWhileSpecializingFlag_None = 0,
  161. BfWhileSpecializingFlag_Type = 1,
  162. BfWhileSpecializingFlag_Method = 2
  163. };
  164. enum BfCompilerOptionFlags
  165. {
  166. BfCompilerOptionFlag_EmitDebugInfo = 1,
  167. BfCompilerOptionFlag_EmitLineInfo = 2,
  168. BfCompilerOptionFlag_WriteIR = 4,
  169. BfCompilerOptionFlag_GenerateOBJ = 8,
  170. BfCompilerOptionFlag_GenerateBitcode = 0x10,
  171. BfCompilerOptionFlag_ClearLocalVars = 0x20,
  172. BfCompilerOptionFlag_RuntimeChecks = 0x40,
  173. BfCompilerOptionFlag_EmitDynamicCastCheck = 0x80,
  174. BfCompilerOptionFlag_EnableObjectDebugFlags = 0x100,
  175. BfCompilerOptionFlag_EmitObjectAccessCheck = 0x200,
  176. BfCompilerOptionFlag_EnableCustodian = 0x400,
  177. BfCompilerOptionFlag_EnableRealtimeLeakCheck = 0x800,
  178. BfCompilerOptionFlag_EnableSideStack = 0x1000,
  179. BfCompilerOptionFlag_EnableHotSwapping = 0x2000,
  180. BfCompilerOptionFlag_IncrementalBuild = 0x4000,
  181. BfCompilerOptionFlag_DebugAlloc = 0x8000,
  182. BfCompilerOptionFlag_OmitDebugHelpers = 0x10000,
  183. BfCompilerOptionFlag_NoFramePointerElim = 0x20000,
  184. BfCompilerOptionFlag_ArithmeticChecks = 0x40000,
  185. };
  186. enum BfTypeFlags
  187. {
  188. BfTypeFlags_UnspecializedGeneric = 0x0001,
  189. BfTypeFlags_SpecializedGeneric = 0x0002,
  190. BfTypeFlags_Array = 0x0004,
  191. BfTypeFlags_Object = 0x0008,
  192. BfTypeFlags_Boxed = 0x0010,
  193. BfTypeFlags_Pointer = 0x0020,
  194. BfTypeFlags_Struct = 0x0040,
  195. BfTypeFlags_Interface = 0x0080,
  196. BfTypeFlags_Primitive = 0x0100,
  197. BfTypeFlags_TypedPrimitive = 0x0200,
  198. BfTypeFlags_Tuple = 0x0400,
  199. BfTypeFlags_Nullable = 0x0800,
  200. BfTypeFlags_SizedArray = 0x1000,
  201. BfTypeFlags_Splattable = 0x2000,
  202. BfTypeFlags_Union = 0x4000,
  203. BfTypeFlags_ConstExpr = 0x8000,
  204. //
  205. BfTypeFlags_WantsMarking = 0x10000,
  206. BfTypeFlags_Delegate = 0x20000,
  207. BfTypeFlags_Function = 0x40000,
  208. BfTypeFlags_HasDestructor = 0x80000,
  209. BfTypeFlags_GenericParam = 0x100000,
  210. BfTypeFlags_Static = 0x200000,
  211. BfTypeFlags_Abstract = 0x400000,
  212. BfTypeFlags_HasAppendWantMark = 0x800000,
  213. };
  214. enum BfMethodFlags
  215. {
  216. BfMethodFlags_Protected = 3,
  217. BfMethodFlags_Public = 6,
  218. BfMethodFlags_Static = 0x10,
  219. BfMethodFlags_Virtual = 0x40,
  220. BfMethodFlags_ReadOnly = 0x100,
  221. BfMethodFlags_Mixin = 0x200,
  222. BfMethodFlags_StdCall = 0x1000,
  223. BfMethodFlags_FastCall = 0x2000,
  224. BfMethodFlags_ThisCall = 0x3000,
  225. BfMethodFlags_Mutating = 0x4000,
  226. BfMethodFlags_Constructor = 0x8000,
  227. BfMethodFlags_AppendBit0 = 0x10000,
  228. BfMethodFlags_AppendBit1 = 0x20000,
  229. BfMethodFlags_CheckedBit0 = 0x40000,
  230. BfMethodFlags_CheckedBit1 = 0x80000,
  231. };
  232. enum BfComptimeMethodFlags
  233. {
  234. BfComptimeMethodFlags_None = 0,
  235. BfComptimeMethodFlags_NoReflect = 1
  236. };
  237. enum BfObjectFlags : uint8
  238. {
  239. BfObjectFlag_None = 0,
  240. BfObjectFlag_MarkIdMask = 0x03,
  241. BfObjectFlag_Allocated = 0x04,
  242. BfObjectFlag_StackAlloc = 0x08,
  243. BfObjectFlag_AppendAlloc = 0x10,
  244. BfObjectFlag_AllocInfo = 0x20,
  245. BfObjectFlag_AllocInfo_Short = 0x40,
  246. BfObjectFlag_Deleted = 0x80,
  247. BfObjectFlag_StackDeleted = 0x80 // We remove StackAlloc so it doesn't get scanned
  248. };
  249. enum BfCustomAttributeFlags : uint8
  250. {
  251. BfCustomAttributeFlags_None,
  252. BfCustomAttributeFlags_DisallowAllowMultiple = 1,
  253. BfCustomAttributeFlags_NotInherited = 2,
  254. BfCustomAttributeFlags_ReflectAttribute = 4,
  255. BfCustomAttributeFlags_AlwaysIncludeTarget = 8
  256. };
  257. enum BfAlwaysIncludeFlags : uint8
  258. {
  259. BfAlwaysIncludeFlag_None = 0,
  260. BfAlwaysIncludeFlag_Type = 1,
  261. BfAlwaysIncludeFlag_IncludeAllMethods = 2,
  262. BfAlwaysIncludeFlag_AssumeInstantiated = 4,
  263. BfAlwaysIncludeFlag_All = BfAlwaysIncludeFlag_Type | BfAlwaysIncludeFlag_IncludeAllMethods | BfAlwaysIncludeFlag_AssumeInstantiated
  264. };
  265. enum BfCEOnCompileKind : uint8
  266. {
  267. BfCEOnCompileKind_None,
  268. BfCEOnCompileKind_TypeInit,
  269. BfCEOnCompileKind_TypeDone
  270. };
  271. enum BfPlatformType
  272. {
  273. BfPlatformType_Unknown,
  274. BfPlatformType_Windows,
  275. BfPlatformType_Linux,
  276. BfPlatformType_macOS,
  277. BfPlatformType_iOS,
  278. BfPlatformType_Android,
  279. BfPlatformType_Wasm
  280. };
  281. enum BfMachineType
  282. {
  283. BfMachineType_Unknown,
  284. BfMachineType_x86,
  285. BfMachineType_x64,
  286. BfMachineType_ARM,
  287. BfMachineType_AArch64,
  288. BfMachineType_Wasm32,
  289. BfMachineType_Wasm64,
  290. };
  291. enum BfToolsetType
  292. {
  293. BfToolsetType_GNU,
  294. BfToolsetType_Microsoft,
  295. BfToolsetType_LLVM
  296. };
  297. enum BfSIMDSetting
  298. {
  299. BfSIMDSetting_None,
  300. BfSIMDSetting_MMX,
  301. BfSIMDSetting_SSE,
  302. BfSIMDSetting_SSE2,
  303. BfSIMDSetting_SSE3,
  304. BfSIMDSetting_SSE4,
  305. BfSIMDSetting_SSE41,
  306. BfSIMDSetting_AVX,
  307. BfSIMDSetting_AVX2,
  308. };
  309. enum BfAsmKind
  310. {
  311. BfAsmKind_None,
  312. BfAsmKind_ATT,
  313. BfAsmKind_Intel,
  314. };
  315. enum BfOptLevel
  316. {
  317. BfOptLevel_NotSet = -1,
  318. BfOptLevel_O0 = 0,
  319. BfOptLevel_O1,
  320. BfOptLevel_O2,
  321. BfOptLevel_O3,
  322. BfOptLevel_Og,
  323. BfOptLevel_OgPlus
  324. };
  325. enum BfLTOType
  326. {
  327. BfLTOType_None = 0,
  328. BfLTOType_Thin = 1,
  329. BfLTOType_Fat = 2
  330. };
  331. enum BfCFLAAType
  332. {
  333. BfCFLAAType_None,
  334. BfCFLAAType_Steensgaard,
  335. BfCFLAAType_Andersen,
  336. BfCFLAAType_Both
  337. };
  338. enum BfRelocType
  339. {
  340. BfRelocType_NotSet,
  341. BfRelocType_Static,
  342. BfRelocType_PIC,
  343. BfRelocType_DynamicNoPIC,
  344. BfRelocType_ROPI,
  345. BfRelocType_RWPI,
  346. BfRelocType_ROPI_RWPI
  347. };
  348. enum BfPICLevel
  349. {
  350. BfPICLevel_NotSet,
  351. BfPICLevel_Not,
  352. BfPICLevel_Small,
  353. BfPICLevel_Big
  354. };
  355. struct BfCodeGenOptions
  356. {
  357. bool mIsHotCompile;
  358. bool mWriteObj;
  359. bool mWriteBitcode;
  360. BfAsmKind mAsmKind;
  361. bool mWriteToLib;
  362. bool mWriteLLVMIR;
  363. int16 mVirtualMethodOfs;
  364. int16 mDynSlotOfs;
  365. BfRelocType mRelocType;
  366. BfPICLevel mPICLevel;
  367. BfSIMDSetting mSIMDSetting;
  368. BfOptLevel mOptLevel;
  369. BfLTOType mLTOType;
  370. int mSizeLevel;
  371. BfCFLAAType mUseCFLAA;
  372. bool mUseNewSROA;
  373. bool mDisableTailCalls;
  374. bool mDisableUnitAtATime;
  375. bool mDisableUnrollLoops;
  376. bool mBBVectorize;
  377. bool mSLPVectorize;
  378. bool mLoopVectorize;
  379. bool mRerollLoops;
  380. bool mLoadCombine;
  381. bool mDisableGVNLoadPRE;
  382. bool mVerifyInput;
  383. bool mVerifyOutput;
  384. bool mStripDebug;
  385. bool mMergeFunctions;
  386. bool mEnableMLSM;
  387. bool mRunSLPAfterLoopVectorization;
  388. bool mUseGVNAfterVectorization;
  389. bool mEnableLoopInterchange;
  390. bool mEnableLoopLoadElim;
  391. bool mExtraVectorizerPasses;
  392. bool mEnableEarlyCSEMemSSA;
  393. bool mEnableGVNHoist;
  394. bool mEnableGVNSink;
  395. bool mDisableLibCallsShrinkWrap;
  396. bool mExpensiveCombines;
  397. bool mEnableSimpleLoopUnswitch;
  398. bool mDivergentTarget;
  399. bool mNewGVN;
  400. bool mRunPartialInlining;
  401. bool mUseLoopVersioningLICM;
  402. bool mEnableUnrollAndJam;
  403. bool mEnableHotColdSplit;
  404. Val128 mHash;
  405. BfCodeGenOptions()
  406. {
  407. mIsHotCompile = false;
  408. mWriteObj = true;
  409. mWriteBitcode = false;
  410. mAsmKind = BfAsmKind_None;
  411. mWriteToLib = false;
  412. mWriteLLVMIR = false;
  413. mVirtualMethodOfs = 0;
  414. mDynSlotOfs = 0;
  415. mRelocType = BfRelocType_NotSet;
  416. mPICLevel = BfPICLevel_NotSet;
  417. mSIMDSetting = BfSIMDSetting_None;
  418. mOptLevel = BfOptLevel_O0;
  419. mLTOType = BfLTOType_None;
  420. mSizeLevel = 0;
  421. mUseCFLAA = BfCFLAAType_None;
  422. mUseNewSROA = false;
  423. mDisableTailCalls = false;
  424. mDisableUnitAtATime = false;
  425. mDisableUnrollLoops = false;
  426. mBBVectorize = false;
  427. mSLPVectorize = false;
  428. mLoopVectorize = false;
  429. mRerollLoops = false;
  430. mLoadCombine = false;
  431. mDisableGVNLoadPRE = false;
  432. mVerifyInput = false;
  433. mVerifyOutput = false;
  434. mStripDebug = false;
  435. mMergeFunctions = false;
  436. mEnableMLSM = false;
  437. mRunSLPAfterLoopVectorization = false;
  438. mUseGVNAfterVectorization = false;
  439. mEnableLoopInterchange = false;
  440. mEnableLoopLoadElim = true;
  441. mExtraVectorizerPasses = false;
  442. mEnableEarlyCSEMemSSA = true;
  443. mEnableGVNHoist = false;
  444. mEnableGVNSink = false;
  445. mDisableLibCallsShrinkWrap = false;
  446. mExpensiveCombines = false;
  447. mEnableSimpleLoopUnswitch = false;
  448. mDivergentTarget = false;
  449. mNewGVN = false;
  450. mRunPartialInlining = false;
  451. mUseLoopVersioningLICM = false;
  452. mEnableUnrollAndJam = false;
  453. mEnableHotColdSplit = false;
  454. }
  455. void GenerateHash()
  456. {
  457. HashContext hashCtx;
  458. hashCtx.Mixin(mWriteObj);
  459. hashCtx.Mixin(mWriteBitcode);
  460. hashCtx.Mixin(mAsmKind);
  461. hashCtx.Mixin(mWriteToLib);
  462. hashCtx.Mixin(mWriteLLVMIR);
  463. hashCtx.Mixin(mVirtualMethodOfs);
  464. hashCtx.Mixin(mDynSlotOfs);
  465. hashCtx.Mixin(mRelocType);
  466. hashCtx.Mixin(mPICLevel);
  467. hashCtx.Mixin(mSIMDSetting);
  468. hashCtx.Mixin(mOptLevel);
  469. hashCtx.Mixin(mLTOType);
  470. hashCtx.Mixin(mSizeLevel);
  471. hashCtx.Mixin(mUseCFLAA);
  472. hashCtx.Mixin(mUseNewSROA);
  473. hashCtx.Mixin(mDisableTailCalls);
  474. hashCtx.Mixin(mDisableUnitAtATime);
  475. hashCtx.Mixin(mDisableUnrollLoops);
  476. hashCtx.Mixin(mBBVectorize);
  477. hashCtx.Mixin(mSLPVectorize);
  478. hashCtx.Mixin(mLoopVectorize);
  479. hashCtx.Mixin(mRerollLoops);
  480. hashCtx.Mixin(mLoadCombine);
  481. hashCtx.Mixin(mDisableGVNLoadPRE);
  482. hashCtx.Mixin(mVerifyInput);
  483. hashCtx.Mixin(mVerifyOutput);
  484. hashCtx.Mixin(mStripDebug);
  485. hashCtx.Mixin(mMergeFunctions);
  486. hashCtx.Mixin(mEnableMLSM);
  487. hashCtx.Mixin(mRunSLPAfterLoopVectorization);
  488. hashCtx.Mixin(mUseGVNAfterVectorization);
  489. hashCtx.Mixin(mEnableLoopInterchange);
  490. hashCtx.Mixin(mEnableLoopLoadElim);
  491. hashCtx.Mixin(mExtraVectorizerPasses);
  492. mHash = hashCtx.Finish128();
  493. }
  494. };
  495. enum BfParamKind : uint8
  496. {
  497. BfParamKind_Normal,
  498. BfParamKind_ExplicitThis,
  499. BfParamKind_Params,
  500. BfParamKind_DelegateParam,
  501. BfParamKind_ImplicitCapture,
  502. BfParamKind_AppendIdx,
  503. BfParamKind_VarArgs
  504. };
  505. enum BfShow : uint8
  506. {
  507. BfShow_Show,
  508. BfShow_HideIndirect,
  509. BfShow_Hide
  510. };
  511. class BfParameterDef
  512. {
  513. public:
  514. String mName;
  515. BfTypeReference* mTypeRef;
  516. BfParameterDeclaration* mParamDeclaration;
  517. int mMethodGenericParamIdx;
  518. BfParamKind mParamKind;
  519. uint8 mNamePrefixCount; // Number of @'s
  520. public:
  521. BfParameterDef()
  522. {
  523. mTypeRef = NULL;
  524. mMethodGenericParamIdx = -1;
  525. mParamKind = BfParamKind_Normal;
  526. mParamDeclaration = NULL;
  527. mNamePrefixCount = 0;
  528. }
  529. void SetName(BfAstNode* nameNode);
  530. };
  531. class BfMemberDef
  532. {
  533. public:
  534. #ifdef _DEBUG
  535. StringT<48> mName;
  536. #else
  537. String mName;
  538. #endif
  539. BfTypeDef* mDeclaringType;
  540. BfProtection mProtection;
  541. uint8 mNamePrefixCount; // Number of @'s
  542. bool mIsStatic;
  543. BfShow mShow;
  544. bool mIsReadOnly;
  545. bool mHasMultiDefs;
  546. public:
  547. BfMemberDef()
  548. {
  549. mDeclaringType = NULL;
  550. mProtection = BfProtection_Public;
  551. mNamePrefixCount = 0;
  552. mIsStatic = false;
  553. mShow = BfShow_Show;
  554. mIsReadOnly = false;
  555. mHasMultiDefs = false;
  556. }
  557. virtual ~BfMemberDef()
  558. {
  559. }
  560. void SetName(BfAstNode* nameNode);
  561. };
  562. class BfFieldDef : public BfMemberDef
  563. {
  564. public:
  565. int mIdx;
  566. bool mIsConst; // Note: Consts are also all considered Static
  567. BfTypeReference* mTypeRef;
  568. BfProtection mUsingProtection;
  569. bool mIsInline;
  570. bool mIsVolatile;
  571. bool mIsExtern;
  572. bool mIsAppend;
  573. bool mIsProperty;
  574. BfAstNode* mFieldDeclaration;
  575. // It may seem that fields and properties don't need a 'mNextWithSameName', but with extensions it's possible
  576. // to have two libraries which each add a field to a type with the same name
  577. BfFieldDef* mNextWithSameName;
  578. public:
  579. BfFieldDef()
  580. {
  581. mIdx = 0;
  582. mIsConst = false;
  583. mTypeRef = NULL;
  584. mUsingProtection = BfProtection_Hidden;
  585. mIsInline = false;
  586. mIsExtern = false;
  587. mIsAppend = false;
  588. mIsVolatile = false;
  589. mIsProperty = false;
  590. mFieldDeclaration = NULL;
  591. mNextWithSameName = NULL;
  592. }
  593. bool IsUnnamedTupleField()
  594. {
  595. return (mName[0] >= '0') && (mName[0] <= '9');
  596. }
  597. bool IsEnumCaseEntry();
  598. bool IsNonConstStatic()
  599. {
  600. return mIsStatic && !mIsConst;
  601. }
  602. BfAstNode* GetRefNode()
  603. {
  604. if (mFieldDeclaration == NULL)
  605. return NULL;
  606. if (auto fieldDeclaration = BfNodeDynCast<BfFieldDeclaration>(mFieldDeclaration))
  607. {
  608. if (fieldDeclaration->mNameNode != NULL)
  609. return fieldDeclaration->mNameNode;
  610. if (fieldDeclaration->mTypeRef != NULL)
  611. return fieldDeclaration->mTypeRef;
  612. }
  613. if (auto paramDeclaration = BfNodeDynCast<BfParameterDeclaration>(mFieldDeclaration))
  614. {
  615. if (paramDeclaration->mNameNode != NULL)
  616. return paramDeclaration->mNameNode;
  617. }
  618. return mFieldDeclaration;
  619. }
  620. BfFieldDeclaration* GetFieldDeclaration()
  621. {
  622. return BfNodeDynCast<BfFieldDeclaration>(mFieldDeclaration);
  623. }
  624. BfParameterDeclaration* GetParamDeclaration()
  625. {
  626. return BfNodeDynCast<BfParameterDeclaration>(mFieldDeclaration);
  627. }
  628. BfExpression* GetInitializer()
  629. {
  630. if (auto fieldDecl = GetFieldDeclaration())
  631. return fieldDecl->mInitializer;
  632. if (auto paramDecl = GetParamDeclaration())
  633. return paramDecl->mInitializer;
  634. return NULL;
  635. }
  636. BfAstNode* GetNameNode()
  637. {
  638. if (auto fieldDecl = GetFieldDeclaration())
  639. return fieldDecl->mNameNode;
  640. if (auto paramDecl = GetParamDeclaration())
  641. return paramDecl->mNameNode;
  642. return NULL;
  643. }
  644. };
  645. class BfPropertyDef : public BfFieldDef
  646. {
  647. public:
  648. Array<BfMethodDef*> mMethods;
  649. BfPropertyDef* mNextWithSameName;
  650. public:
  651. BfPropertyDef()
  652. {
  653. mNextWithSameName = NULL;
  654. }
  655. bool IsVirtual();
  656. bool HasExplicitInterface();
  657. bool IsExpressionBodied();
  658. BfAstNode* GetRefNode();
  659. };
  660. enum BfGenericParamFlags : uint16
  661. {
  662. BfGenericParamFlag_None = 0,
  663. BfGenericParamFlag_Class = 1,
  664. BfGenericParamFlag_Struct = 2,
  665. BfGenericParamFlag_StructPtr = 4,
  666. BfGenericParamFlag_Enum = 8,
  667. BfGenericParamFlag_Interface = 0x10,
  668. BfGenericParamFlag_Concrete = 0x20,
  669. BfGenericParamFlag_New = 0x40,
  670. BfGenericParamFlag_Delete = 0x80,
  671. BfGenericParamFlag_Var = 0x100,
  672. BfGenericParamFlag_Const = 0x200,
  673. BfGenericParamFlag_Equals = 0x400,
  674. BfGenericParamFlag_Equals_Op = 0x800,
  675. BfGenericParamFlag_Equals_Type = 0x1000,
  676. BfGenericParamFlag_Equals_IFace = 0x2000,
  677. BfGenericParamFlag_ComptypeExpr = 0x4000
  678. };
  679. class BfConstraintDef
  680. {
  681. public:
  682. BfGenericParamFlags mGenericParamFlags;
  683. Array<BfAstNode*> mConstraints;
  684. BfConstraintDef()
  685. {
  686. mGenericParamFlags = BfGenericParamFlag_None;
  687. }
  688. bool operator==(const BfConstraintDef& other) const
  689. {
  690. if (mGenericParamFlags != other.mGenericParamFlags)
  691. return false;
  692. if (mConstraints.mSize != other.mConstraints.mSize)
  693. return false;
  694. for (int i = 0; i < mConstraints.mSize; i++)
  695. {
  696. if (!mConstraints[i]->Equals(other.mConstraints[i]->ToStringView()))
  697. return false;
  698. }
  699. return true;
  700. }
  701. bool operator!=(const BfConstraintDef& other) const
  702. {
  703. return !(*this == other);
  704. }
  705. };
  706. class BfGenericParamDef : public BfConstraintDef
  707. {
  708. public:
  709. String mName;
  710. Array<BfIdentifierNode*> mNameNodes; // 0 is always the def name
  711. bool operator==(const BfGenericParamDef& other) const
  712. {
  713. if (mName != other.mName)
  714. return false;
  715. return *(BfConstraintDef*)this == *(BfConstraintDef*)&other;
  716. }
  717. bool operator!=(const BfGenericParamDef& other) const
  718. {
  719. return !(*this == other);
  720. }
  721. };
  722. class BfExternalConstraintDef : public BfConstraintDef
  723. {
  724. public:
  725. BfTypeReference* mTypeRef;
  726. };
  727. // CTOR is split into two for Objects - Ctor clears and sets up VData, Ctor_Body executes ctor body code
  728. enum BfMethodType : uint8
  729. {
  730. BfMethodType_Ignore,
  731. BfMethodType_Normal,
  732. BfMethodType_PropertyGetter,
  733. BfMethodType_PropertySetter,
  734. BfMethodType_CtorCalcAppend,
  735. BfMethodType_Ctor,
  736. BfMethodType_CtorNoBody,
  737. BfMethodType_CtorClear,
  738. BfMethodType_Init,
  739. BfMethodType_Dtor,
  740. BfMethodType_Operator,
  741. BfMethodType_Mixin,
  742. BfMethodType_Extension
  743. };
  744. enum BfCallingConvention : uint8
  745. {
  746. BfCallingConvention_Unspecified,
  747. BfCallingConvention_Cdecl,
  748. BfCallingConvention_Stdcall,
  749. BfCallingConvention_Fastcall,
  750. };
  751. #define BF_METHODNAME_MARKMEMBERS "GCMarkMembers"
  752. #define BF_METHODNAME_MARKMEMBERS_STATIC "GCMarkStaticMembers"
  753. #define BF_METHODNAME_FIND_TLS_MEMBERS "GCFindTLSMembers"
  754. #define BF_METHODNAME_DYNAMICCAST "DynamicCastToTypeId"
  755. #define BF_METHODNAME_DYNAMICCAST_INTERFACE "DynamicCastToInterface"
  756. #define BF_METHODNAME_DYNAMICCAST_SIGNATURE "DynamicCastToSignature"
  757. #define BF_METHODNAME_CALCAPPEND "this$calcAppend"
  758. #define BF_METHODNAME_ENUM_HASFLAG "HasFlag"
  759. #define BF_METHODNAME_ENUM_GETUNDERLYING "get__Underlying"
  760. #define BF_METHODNAME_ENUM_GETUNDERLYINGREF "get__UnderlyingRef"
  761. #define BF_METHODNAME_EQUALS "Equals"
  762. #define BF_METHODNAME_INVOKE "Invoke"
  763. #define BF_METHODNAME_TO_STRING "ToString"
  764. #define BF_METHODNAME_DEFAULT_EQUALS "__Equals"
  765. #define BF_METHODNAME_DEFAULT_STRICT_EQUALS "__StrictEquals"
  766. enum BfOptimize : int8
  767. {
  768. BfOptimize_Default,
  769. BfOptimize_Unoptimized,
  770. BfOptimize_Optimized
  771. };
  772. enum BfImportKind : int8
  773. {
  774. BfImportKind_None,
  775. BfImportKind_Import_Unknown,
  776. BfImportKind_Import_Dynamic,
  777. BfImportKind_Import_Static,
  778. BfImportKind_Export
  779. };
  780. enum BfCommutableKind : int8
  781. {
  782. BfCommutableKind_None,
  783. BfCommutableKind_Operator,
  784. BfCommutableKind_Forward,
  785. BfCommutableKind_Reverse,
  786. };
  787. enum BfComptimeFlags : int8
  788. {
  789. BfComptimeFlag_None,
  790. BfComptimeFlag_Comptime = 1,
  791. BfComptimeFlag_OnlyFromComptime = 2,
  792. BfComptimeFlag_ConstEval = 4
  793. };
  794. enum BfAllowAppendKind : int8
  795. {
  796. BfAllowAppendKind_No,
  797. BfAllowAppendKind_Yes,
  798. BfAllowAppendKind_ZeroGap,
  799. BfAllowAppendKind_Infer
  800. };
  801. class BfMethodDef : public BfMemberDef
  802. {
  803. public:
  804. BfAstNode* mMethodDeclaration;
  805. BfAstNode* mBody;
  806. BfTypeReference* mExplicitInterface;
  807. BfTypeReference* mReturnTypeRef;
  808. Array<BfParameterDef*> mParams;
  809. Array<BfGenericParamDef*> mGenericParams;
  810. Array<BfExternalConstraintDef> mExternalConstraints;
  811. Dictionary<StringView, int>* mParamNameMap;
  812. BfMethodDef* mNextWithSameName;
  813. Val128 mFullHash;
  814. int mIdx;
  815. int mPropertyIdx;
  816. BfMethodType mMethodType;
  817. bool mIsLocalMethod;
  818. bool mIsVirtual;
  819. bool mIsOverride;
  820. bool mIsAbstract;
  821. bool mIsConcrete;
  822. bool mIsPartial;
  823. bool mIsNew;
  824. bool mCodeChanged;
  825. bool mWantsBody;
  826. bool mCLink;
  827. BfAllowAppendKind mAppendKind;
  828. bool mAlwaysInline;
  829. bool mIsNoReturn;
  830. bool mIsMutating;
  831. bool mIsNoSplat;
  832. bool mIsNoReflect;
  833. bool mIsSkipCall;
  834. bool mHasComptime;
  835. bool mIsOperator;
  836. bool mIsExtern;
  837. bool mIsNoDiscard;
  838. bool mHasExplicitThis;
  839. bool mAddedAfterEmit;
  840. BfCommutableKind mCommutableKind;
  841. BfCheckedKind mCheckedKind;
  842. BfImportKind mImportKind;
  843. BfCallingConvention mCallingConvention;
  844. public:
  845. BfMethodDef()
  846. {
  847. mIdx = -1;
  848. mPropertyIdx = -1;
  849. mIsLocalMethod = false;
  850. mIsVirtual = false;
  851. mIsOverride = false;
  852. mIsAbstract = false;
  853. mIsConcrete = false;
  854. mIsStatic = false;
  855. mIsNew = false;
  856. mIsPartial = false;
  857. mCLink = false;
  858. mIsNoReturn = false;
  859. mIsMutating = false;
  860. mIsNoSplat = false;
  861. mIsNoReflect = false;
  862. mIsSkipCall = false;
  863. mHasComptime = false;
  864. mIsOperator = false;
  865. mIsExtern = false;
  866. mIsNoDiscard = false;
  867. mHasExplicitThis = false;
  868. mAddedAfterEmit = false;
  869. mBody = NULL;
  870. mExplicitInterface = NULL;
  871. mReturnTypeRef = NULL;
  872. mMethodDeclaration = NULL;
  873. mCodeChanged = false;
  874. mWantsBody = true;
  875. mCommutableKind = BfCommutableKind_None;
  876. mCheckedKind = BfCheckedKind_NotSet;
  877. mImportKind = BfImportKind_None;
  878. mMethodType = BfMethodType_Normal;
  879. mCallingConvention = BfCallingConvention_Unspecified;
  880. mAppendKind = BfAllowAppendKind_No;
  881. mAlwaysInline = false;
  882. mParamNameMap = NULL;
  883. mNextWithSameName = NULL;
  884. }
  885. virtual ~BfMethodDef();
  886. static BfImportKind GetImportKindFromPath(const StringImpl& filePath);
  887. bool HasNoThisSplat() { return mIsMutating || mIsNoSplat; }
  888. bool HasAppend() { return mAppendKind != BfAllowAppendKind_No; }
  889. void Reset();
  890. void FreeMembers();
  891. BfMethodDeclaration* GetMethodDeclaration();
  892. BfPropertyMethodDeclaration* GetPropertyMethodDeclaration();
  893. BfPropertyDeclaration* GetPropertyDeclaration();
  894. BfAstNode* GetRefNode();
  895. BfTokenNode* GetMutNode();
  896. bool HasBody();
  897. bool IsEmptyPartial();
  898. bool IsDefaultCtor();
  899. bool IsCtorOrInit();
  900. String ToString();
  901. String GetReflectName();
  902. int GetExplicitParamCount();
  903. void BuildParamNameMap();
  904. bool CanReflect();
  905. };
  906. class BfOperatorDef : public BfMethodDef
  907. {
  908. public:
  909. BfOperatorDeclaration* mOperatorDeclaration;
  910. public:
  911. BfOperatorDef()
  912. {
  913. mOperatorDeclaration = NULL;
  914. }
  915. bool IsExplicit()
  916. {
  917. if (mOperatorDeclaration->mExplicitToken != NULL)
  918. return mOperatorDeclaration->mExplicitToken->mToken == BfToken_Explicit;
  919. if (mOperatorDeclaration->mOpTypeToken != NULL)
  920. return mOperatorDeclaration->mOpTypeToken->mToken == BfToken_Explicit;
  921. return false;
  922. }
  923. };
  924. struct BfTypeDefLookupContext
  925. {
  926. public:
  927. int mBestPri;
  928. BfTypeDef* mBestTypeDef;
  929. BfTypeDef* mAmbiguousTypeDef;
  930. Array<BfProject*>* mCheckProjects;
  931. public:
  932. BfTypeDefLookupContext()
  933. {
  934. mBestPri = (int)0x80000000;
  935. mBestTypeDef = NULL;
  936. mAmbiguousTypeDef = NULL;
  937. mCheckProjects = NULL;
  938. }
  939. bool HasValidMatch()
  940. {
  941. return (mBestPri >= 0) && (mBestTypeDef != NULL);
  942. }
  943. };
  944. struct BfMemberSetEntry
  945. {
  946. BfMemberDef* mMemberDef;
  947. BfMemberSetEntry(BfMemberDef* memberDef)
  948. {
  949. mMemberDef = memberDef;
  950. }
  951. bool operator==(const BfMemberSetEntry& other) const
  952. {
  953. return mMemberDef->mName == other.mMemberDef->mName;
  954. }
  955. bool operator==(const StringImpl& other) const
  956. {
  957. return mMemberDef->mName == other;
  958. }
  959. };
  960. class BfTypeDefMemberSet : public HashSet<BfMemberSetEntry>
  961. {
  962. public:
  963. int mSourceSize;
  964. public:
  965. BfTypeDefMemberSet()
  966. {
  967. mSourceSize = 0;
  968. }
  969. void Clear()
  970. {
  971. HashSet<BfMemberSetEntry>::Clear();
  972. mSourceSize = 0;
  973. }
  974. };
  975. // For partial classes, the first entry in the map will contain the combined data
  976. class BfTypeDef
  977. {
  978. public:
  979. enum DefState
  980. {
  981. DefState_New,
  982. DefState_Defined,
  983. DefState_CompositeWithPartials, // Temporary condition
  984. DefState_AwaitingNewVersion,
  985. DefState_Signature_Changed,
  986. DefState_InlinedInternals_Changed, // Code within methods, including inlined methods, changed
  987. DefState_Internals_Changed, // Only code within a non-inlined methods changed
  988. DefState_Refresh,
  989. DefState_Deleted,
  990. DefState_Emitted,
  991. DefState_EmittedDirty
  992. };
  993. public:
  994. BfTypeDef* mNextRevision;
  995. BfSystem* mSystem;
  996. BfProject* mProject;
  997. BfTypeDeclaration* mTypeDeclaration;
  998. BfSource* mSource;
  999. DefState mDefState;
  1000. Val128 mSignatureHash; // Data, methods, etc
  1001. Val128 mFullHash;
  1002. Val128 mInlineHash;
  1003. BfTypeDef* mEmitParent;
  1004. BfTypeDef* mOuterType;
  1005. BfAtomComposite mNamespace;
  1006. BfAtom* mName;
  1007. BfAtom* mNameEx; // Contains extensions like `1 for param counts
  1008. BfAtomComposite mFullName;
  1009. BfAtomComposite mFullNameEx;
  1010. BfProtection mProtection;
  1011. Array<BfAtomComposite> mNamespaceSearch;
  1012. Array<BfTypeReference*> mStaticSearch;
  1013. Array<BfTypeReference*> mInternalAccessSet;
  1014. Array<BfFieldDef*> mFields;
  1015. Array<BfPropertyDef*> mProperties;
  1016. Array<BfMethodDef*> mMethods;
  1017. BfTypeDefMemberSet mMethodSet;
  1018. BfTypeDefMemberSet mFieldSet;
  1019. BfTypeDefMemberSet mPropertySet;
  1020. Array<BfOperatorDef*> mOperators;
  1021. Array<BfGenericParamDef*> mGenericParamDefs;
  1022. Array<BfExternalConstraintDef> mExternalConstraints;
  1023. Array<BfTypeReference*> mBaseTypes;
  1024. Array<BfTypeDef*> mNestedTypes;
  1025. Array<BfDirectStrTypeReference*> mDirectAllocNodes;
  1026. Array<BfTypeDef*> mPartials; // Only valid for mIsCombinedPartial
  1027. int mHash;
  1028. int mPartialIdx;
  1029. int mNestDepth;
  1030. int mDupDetectedRevision; // Error state
  1031. BfTypeCode mTypeCode;
  1032. BfShow mShow;
  1033. bool mIsAlwaysInclude;
  1034. bool mIsNoDiscard;
  1035. bool mIsPartial;
  1036. bool mIsExplicitPartial;
  1037. bool mPartialUsed;
  1038. bool mIsCombinedPartial;
  1039. bool mIsDelegate;
  1040. bool mIsFunction;
  1041. bool mIsClosure;
  1042. bool mIsAbstract;
  1043. bool mIsStatic;
  1044. bool mHasCEOnCompile;
  1045. bool mHasAppendCtor;
  1046. bool mHasCtorNoBody;
  1047. bool mHasExtensionMethods;
  1048. bool mHasOverrideMethods;
  1049. bool mHasUsingFields;
  1050. bool mIsOpaque;
  1051. bool mIsNextRevision;
  1052. bool mInDeleteQueue;
  1053. bool mForceUseNextRevision;
  1054. public:
  1055. BfTypeDef()
  1056. {
  1057. Init();
  1058. }
  1059. ~BfTypeDef();
  1060. void Init()
  1061. {
  1062. mName = NULL;
  1063. mNameEx = NULL;
  1064. mSystem = NULL;
  1065. mProject = NULL;
  1066. mTypeCode = BfTypeCode_None;
  1067. mShow = BfShow_Show;
  1068. mIsAlwaysInclude = false;
  1069. mIsNoDiscard = false;
  1070. mIsExplicitPartial = false;
  1071. mIsPartial = false;
  1072. mIsCombinedPartial = false;
  1073. mTypeDeclaration = NULL;
  1074. mSource = NULL;
  1075. mDefState = DefState_New;
  1076. mHash = 0;
  1077. mPartialIdx = -1;
  1078. mIsAbstract = false;
  1079. mIsDelegate = false;
  1080. mIsFunction = false;
  1081. mIsClosure = false;
  1082. mIsStatic = false;
  1083. mHasCEOnCompile = false;
  1084. mHasAppendCtor = false;
  1085. mHasCtorNoBody = false;
  1086. mHasExtensionMethods = false;
  1087. mHasOverrideMethods = false;
  1088. mHasUsingFields = false;
  1089. mIsOpaque = false;
  1090. mPartialUsed = false;
  1091. mIsNextRevision = false;
  1092. mInDeleteQueue = false;
  1093. mForceUseNextRevision = false;
  1094. mDupDetectedRevision = -1;
  1095. mNestDepth = 0;
  1096. mEmitParent = NULL;
  1097. mOuterType = NULL;
  1098. mTypeDeclaration = NULL;
  1099. mNextRevision = NULL;
  1100. mProtection = BfProtection_Public;
  1101. }
  1102. BfSource* GetLastSource();
  1103. bool IsGlobalsContainer();
  1104. void Reset();
  1105. void FreeMembers();
  1106. void PopulateMemberSets();
  1107. void ClearMemberSets();
  1108. void RemoveGenericParamDef(BfGenericParamDef* genericParamDef);
  1109. int GetSelfGenericParamCount();
  1110. String ToString();
  1111. BfMethodDef* GetMethodByName(const StringImpl& name, int paramCount = -1);
  1112. BfFieldDef* GetFieldByName(const StringImpl& name);
  1113. bool HasAutoProperty(BfPropertyDeclaration* propertyDeclaration);
  1114. bool ContainsPartial(BfTypeDef* partialTypeDef);
  1115. bool HasParsingFailed();
  1116. String GetAutoPropertyName(BfPropertyDeclaration* propertyDeclaration);
  1117. BfAstNode* GetRefNode();
  1118. bool IsEmitted() { return mEmitParent != NULL; }
  1119. BfTypeDef* GetDefinition(bool getEmitRoot = false)
  1120. {
  1121. if (mEmitParent != NULL)
  1122. {
  1123. if ((getEmitRoot) && (mEmitParent->mIsCombinedPartial))
  1124. return mEmitParent->mPartials[0];
  1125. return mEmitParent;
  1126. }
  1127. return this;
  1128. }
  1129. BfTypeDef* GetLatest()
  1130. {
  1131. if (mEmitParent != NULL)
  1132. return mEmitParent->GetLatest();
  1133. if (mNextRevision != NULL)
  1134. return mNextRevision;
  1135. return this;
  1136. }
  1137. void ReportMemory(MemReporter* memReporter);
  1138. bool NameEquals(BfTypeDef* otherTypeDef);
  1139. bool IsExtension()
  1140. {
  1141. return mTypeCode == BfTypeCode_Extension;
  1142. }
  1143. bool HasSource(BfSource* source);
  1144. bool HasCustomAttributes();
  1145. };
  1146. struct BfTypeDefMapFuncs : public AllocatorCLib
  1147. {
  1148. int GetHash(BfTypeDef* typeDef)
  1149. {
  1150. return GetHash(typeDef->mFullName);
  1151. }
  1152. int GetHash(const BfAtomComposite& name)
  1153. {
  1154. int hash = 0;
  1155. for (int i = 0; i < name.mSize; i++)
  1156. {
  1157. auto atom = name.mParts[i];
  1158. hash = ((hash ^ atom->mHash) << 5) - hash;
  1159. }
  1160. return (hash & 0x7FFFFFFF);
  1161. }
  1162. bool Matches(const BfAtomComposite& name, BfTypeDef* typeDef)
  1163. {
  1164. return name == typeDef->mFullName;
  1165. }
  1166. bool Matches(BfTypeDef* keyTypeDef, BfTypeDef* typeDef)
  1167. {
  1168. return keyTypeDef == typeDef;
  1169. }
  1170. };
  1171. class BfTypeDefMap : public MultiHashSet<BfTypeDef*, BfTypeDefMapFuncs>
  1172. {
  1173. public:
  1174. struct SkipEntry
  1175. {
  1176. public:
  1177. int mIndex;
  1178. int mRevision;
  1179. public:
  1180. SkipEntry()
  1181. {
  1182. mIndex = -1;
  1183. mRevision = -1;
  1184. }
  1185. SkipEntry(int index, int revision)
  1186. {
  1187. mIndex = index;
  1188. mRevision = revision;
  1189. }
  1190. };
  1191. Array<SkipEntry> mPartialSkipCache;
  1192. int mRevision;
  1193. public:
  1194. BfTypeDefMap()
  1195. {
  1196. mRevision = 1;
  1197. }
  1198. void Add(BfTypeDef* value)
  1199. {
  1200. MultiHashSet::Add(value);
  1201. mRevision++;
  1202. }
  1203. void AddAfter(BfTypeDef* value, Entry* afterEntry)
  1204. {
  1205. MultiHashSet::AddAfter(value, afterEntry);
  1206. mRevision++;
  1207. }
  1208. template <typename TKey>
  1209. bool Remove(const TKey& key)
  1210. {
  1211. bool result = MultiHashSet::Remove(key);
  1212. mRevision++;
  1213. return result;
  1214. }
  1215. Iterator Erase(const Iterator& itr)
  1216. {
  1217. auto result = MultiHashSet::Erase(itr);
  1218. mRevision++;
  1219. return result;
  1220. }
  1221. void Clear()
  1222. {
  1223. MultiHashSet::Clear();
  1224. mRevision++;
  1225. }
  1226. void SetPartialSkipCache(int partialIdx, int mapToIdx)
  1227. {
  1228. while (partialIdx >= mPartialSkipCache.mSize)
  1229. mPartialSkipCache.Add(SkipEntry());
  1230. mPartialSkipCache[partialIdx] = SkipEntry(mapToIdx, mRevision);
  1231. }
  1232. };
  1233. enum BfTargetType
  1234. {
  1235. BfTargetType_BeefConsoleApplication,
  1236. BfTargetType_BeefWindowsApplication,
  1237. BfTargetType_BeefLib,
  1238. BfTargetType_CustomBuild,
  1239. BfTargetType_BeefTest,
  1240. BfTargetType_C_ConsoleApplication,
  1241. BfTargetType_C_WindowsApplication,
  1242. BfTargetType_BeefApplication_StaticLib,
  1243. BfTargetType_BeefApplication_DynamicLib,
  1244. BfTargetType_BeefLib_StaticLib,
  1245. BfTargetType_BeefLib_DynamicLib,
  1246. };
  1247. enum BfProjectFlags
  1248. {
  1249. BfProjectFlags_None = 0,
  1250. BfProjectFlags_MergeFunctions = 1,
  1251. BfProjectFlags_CombineLoads = 2,
  1252. BfProjectFlags_VectorizeLoops = 4,
  1253. BfProjectFlags_VectorizeSLP = 8,
  1254. BfProjectFlags_SingleModule = 0x10,
  1255. BfProjectFlags_AsmOutput = 0x20,
  1256. BfProjectFlags_AsmOutput_ATT = 0x40,
  1257. BfProjectFlags_AlwaysIncludeAll = 0x80,
  1258. };
  1259. class BfProject
  1260. {
  1261. public:
  1262. enum DeleteStage
  1263. {
  1264. DeleteStage_None,
  1265. DeleteStage_Queued,
  1266. DeleteStage_AwaitingRefs,
  1267. };
  1268. enum DependencyKind
  1269. {
  1270. DependencyKind_None,
  1271. DependencyKind_Dependency,
  1272. DependencyKind_Identity,
  1273. DependencyKind_Dependent_Exclusive,
  1274. DependencyKind_Dependent_Shared
  1275. };
  1276. public:
  1277. BfSystem* mSystem;
  1278. String mName;
  1279. String mSafeName;
  1280. String mDirectory;
  1281. Array<BfProject*> mDependencies;
  1282. BfTargetType mTargetType;
  1283. BfCodeGenOptions mCodeGenOptions;
  1284. bool mDisabled;
  1285. bool mSingleModule;
  1286. bool mAlwaysIncludeAll;
  1287. DeleteStage mDeleteStage;
  1288. int mIdx;
  1289. String mStartupObject;
  1290. Array<String> mPreprocessorMacros;
  1291. Dictionary<BfAtomComposite, int> mNamespaces;
  1292. HashSet<BfModule*> mUsedModules;
  1293. HashSet<BfType*> mReferencedTypeData;
  1294. HashSet<BfProject*> mDependencySet;
  1295. Dictionary<BfProject*, DependencyKind> mDependencyKindDict;
  1296. Val128 mBuildConfigHash;
  1297. Val128 mVDataConfigHash;
  1298. bool mBuildConfigChanged;
  1299. public:
  1300. BfProject();
  1301. ~BfProject();
  1302. void ClearCache();
  1303. bool ContainsReference(BfProject* refProject);
  1304. bool ReferencesOrReferencedBy(BfProject* refProject);
  1305. bool IsTestProject();
  1306. bool HasDependency(BfProject* project);
  1307. DependencyKind GetDependencyKind(BfProject* project);
  1308. };
  1309. //CDH TODO move these out to separate header if list gets big/unwieldy
  1310. enum BfWarning
  1311. {
  1312. BfWarning_CS0108_MemberHidesInherited = 108,
  1313. BfWarning_CS0114_MethodHidesInherited = 114,
  1314. BfWarning_CS0162_UnreachableCode = 162,
  1315. BfWarning_CS0168_VariableDeclaredButNeverUsed = 168,
  1316. BfWarning_CS0472_ValueTypeNullCompare = 472,
  1317. BfWarning_CS1030_PragmaWarning = 1030,
  1318. BfWarning_BF4201_Only7Hex = 4201,
  1319. BfWarning_BF4202_TooManyHexForInt = 4202,
  1320. BfWarning_BF4203_UnnecessaryDynamicCast = 4203,
  1321. BfWarning_BF4204_AddressOfReadOnly = 4204,
  1322. BfWarning_BF4205_StringInterpolationParam = 4205,
  1323. BfWarning_BF4206_OperatorCommutableUsage = 4206,
  1324. BfWarning_C4554_PossiblePrecedenceError = 4554
  1325. };
  1326. class BfErrorLocation
  1327. {
  1328. public:
  1329. String mFile;
  1330. int mLine;
  1331. int mColumn;
  1332. };
  1333. class BfErrorBase
  1334. {
  1335. public:
  1336. bool mIsWarning;
  1337. bool mIsDeferred;
  1338. BfSourceData* mSource;
  1339. int mSrcStart;
  1340. int mSrcEnd;
  1341. BfErrorLocation* mLocation;
  1342. public:
  1343. BfErrorBase()
  1344. {
  1345. mIsWarning = false;
  1346. mIsDeferred = false;
  1347. mSource = NULL;
  1348. mSrcStart = -1;
  1349. mSrcEnd = -1;
  1350. mLocation = NULL;
  1351. }
  1352. ~BfErrorBase();
  1353. void SetSource(BfPassInstance* passInstance, BfSourceData* source);
  1354. };
  1355. class BfMoreInfo : public BfErrorBase
  1356. {
  1357. public:
  1358. String mInfo;
  1359. };
  1360. class BfError : public BfErrorBase
  1361. {
  1362. public:
  1363. bool mIsAfter;
  1364. bool mIsPersistent;
  1365. BfWhileSpecializingFlags mIsWhileSpecializing;
  1366. bool mIgnore;
  1367. BfProject* mProject;
  1368. String mError;
  1369. int mWarningNumber;
  1370. Array<BfMoreInfo*> mMoreInfo;
  1371. public:
  1372. BfError()
  1373. {
  1374. mIsAfter = false;
  1375. mIsPersistent = false;
  1376. mIsWhileSpecializing = BfWhileSpecializingFlag_None;
  1377. mIgnore = false;
  1378. mProject = NULL;
  1379. mWarningNumber = 0;
  1380. }
  1381. ~BfError()
  1382. {
  1383. for (auto moreInfo : mMoreInfo)
  1384. delete moreInfo;
  1385. }
  1386. int GetSrcStart()
  1387. {
  1388. return mSrcStart;
  1389. }
  1390. int GetSrcLength()
  1391. {
  1392. return mSrcEnd - mSrcStart;
  1393. }
  1394. int GetSrcEnd()
  1395. {
  1396. return mSrcEnd;
  1397. }
  1398. };
  1399. class BfSourceClassifier;
  1400. class BfAutoComplete;
  1401. enum BfFailFlags
  1402. {
  1403. BfFailFlag_None = 0,
  1404. BfFailFlag_ShowSpaceChars = 1
  1405. };
  1406. struct BfErrorEntry
  1407. {
  1408. public:
  1409. BfErrorBase* mError;
  1410. BfErrorEntry(BfErrorBase* error)
  1411. {
  1412. mError = error;
  1413. }
  1414. size_t GetHashCode() const;
  1415. bool operator==(const BfErrorEntry& other) const;
  1416. };
  1417. class BfPassInstance
  1418. {
  1419. public:
  1420. struct StateInfo
  1421. {
  1422. int mOutStreamSize;
  1423. int mErrorsSize;
  1424. int mWarningCount;
  1425. int mDeferredErrorCount;
  1426. int mFailedIdx;
  1427. int mWarnIdx;
  1428. };
  1429. public:
  1430. const int sMaxDisplayErrors = 100;
  1431. const int sMaxErrors = 1000;
  1432. BfSystem* mSystem;
  1433. BfCompiler* mCompiler;
  1434. bool mTrimMessagesToCursor;
  1435. int mFailedIdx;
  1436. int mWarnIdx;
  1437. Dictionary<BfSourceData*, String> mSourceFileNameMap;
  1438. HashSet<BfErrorEntry> mErrorSet;
  1439. Array<BfError*> mErrors;
  1440. int mIgnoreCount;
  1441. int mWarningCount;
  1442. int mDeferredErrorCount;
  1443. Deque<String> mOutStream;
  1444. bool mLastWasDisplayed;
  1445. bool mLastWasAdded;
  1446. uint8 mClassifierPassId;
  1447. HashSet<BfSourceData*> mFilterErrorsTo;
  1448. bool mHadSignatureChanges;
  1449. public:
  1450. BfPassInstance(BfSystem* bfSystem)
  1451. {
  1452. mTrimMessagesToCursor = false;
  1453. mFailedIdx = 0;
  1454. mWarnIdx = 0;
  1455. mSystem = bfSystem;
  1456. mCompiler = NULL;
  1457. mLastWasDisplayed = false;
  1458. mLastWasAdded = false;
  1459. mClassifierPassId = 0;
  1460. mWarningCount = 0;
  1461. mDeferredErrorCount = 0;
  1462. mIgnoreCount = 0;
  1463. mHadSignatureChanges = false;
  1464. }
  1465. ~BfPassInstance();
  1466. StateInfo GetState();
  1467. void RestoreState(StateInfo stateInfo);
  1468. void ClearErrors();
  1469. bool HasFailed();
  1470. bool HasMessages();
  1471. void OutputLine(const StringImpl& str);
  1472. bool PopOutString(String* outString);
  1473. bool WantsRangeRecorded(BfSourceData* bfSource, int srcIdx, int srcLen, bool isWarning, bool isDeferred = false);
  1474. bool WantsRangeDisplayed(BfSourceData* bfSource, int srcIdx, int srcLen, bool isWarning, bool isDeferred = false);
  1475. void TrimSourceRange(BfSourceData* source, int startIdx, int& srcLen); // Trim to a single line, in cases when we reference a large multi-line node
  1476. bool HasLastFailedAt(BfAstNode* astNode);
  1477. void MessageAt(const StringImpl& msgPrefix, const StringImpl& error, BfSourceData* bfSource, int srcIdx, int srcLen = 1, BfFailFlags flags = BfFailFlag_None);
  1478. void FixSrcStartAndEnd(BfSourceData* source, int& startIdx, int& endIdx);
  1479. BfError* WarnAt(int warningNumber, const StringImpl& warning, BfSourceData* bfSource, int srcIdx, int srcLen = 1, bool isDeferred = false);
  1480. BfError* Warn(int warningNumber, const StringImpl& warning);
  1481. BfError* Warn(int warningNumber, const StringImpl& warning, BfAstNode* refNode, bool isDeferred = false);
  1482. BfError* DeferWarn(int warningNumber, const StringImpl& warning, BfAstNode* refNode);
  1483. BfError* WarnAfter(int warningNumber, const StringImpl& warning, BfAstNode* refNode);
  1484. BfError* WarnAfterAt(int warningNumber, const StringImpl& error, BfSourceData* bfSource, int srcIdx);
  1485. BfMoreInfo* MoreInfoAt(const StringImpl& info, BfSourceData* bfSource, int srcIdx, int srcLen, BfFailFlags flags = BfFailFlag_None);
  1486. BfMoreInfo* MoreInfo(const StringImpl& info, bool forceQueue = false);
  1487. BfMoreInfo* MoreInfo(const StringImpl& info, BfAstNode* refNode);
  1488. BfMoreInfo* MoreInfoAfter(const StringImpl& info, BfAstNode* refNode);
  1489. BfError* FailAt(const StringImpl& error, BfSourceData* bfSource, int srcIdx, int srcLen = 1, BfFailFlags flags = BfFailFlag_None);
  1490. BfError* FailAfterAt(const StringImpl& error, BfSourceData* bfSource, int srcIdx);
  1491. BfError* Fail(const StringImpl& error);
  1492. BfError* Fail(const StringImpl& error, BfAstNode* refNode);
  1493. BfError* FailAfter(const StringImpl& error, BfAstNode* refNode);
  1494. BfError* DeferFail(const StringImpl& error, BfAstNode* refNode);
  1495. void SilentFail();
  1496. void TryFlushDeferredError();
  1497. void WriteErrorSummary();
  1498. };
  1499. enum BfOptionFlags
  1500. {
  1501. BfOptionFlags_None = 0,
  1502. BfOptionFlags_RuntimeChecks = 1,
  1503. BfOptionFlags_InitLocalVariables = 2,
  1504. BfOptionFlags_EmitDynamicCastCheck = 4,
  1505. BfOptionFlags_EmitObjectAccessCheck = 8,
  1506. BfOptionFlags_ArithmeticCheck = 0x10,
  1507. BfOptionFlags_ReflectAlwaysIncludeType = 0x20,
  1508. BfOptionFlags_ReflectAlwaysIncludeAll = 0x40,
  1509. BfOptionFlags_ReflectAssumeInstantiated = 0x80,
  1510. BfOptionFlags_ReflectBoxing = 0x100,
  1511. BfOptionFlags_ReflectStaticFields = 0x200,
  1512. BfOptionFlags_ReflectNonStaticFields = 0x400,
  1513. BfOptionFlags_ReflectStaticMethods = 0x800,
  1514. BfOptionFlags_ReflectNonStaticMethods = 0x1000,
  1515. BfOptionFlags_ReflectConstructors = 0x2000,
  1516. BfOptionFlags_ReflectAlwaysIncludeFiltered = 0x4000,
  1517. BfOptionFlags_Reflect_MethodMask = BfOptionFlags_ReflectStaticMethods | BfOptionFlags_ReflectNonStaticMethods | BfOptionFlags_ReflectConstructors,
  1518. BfOptionFlags_Mask = 0x3FFF
  1519. };
  1520. enum BfFieldFlags
  1521. {
  1522. BfFieldFlags_Protected = 3,
  1523. BfFieldFlags_Public = 6,
  1524. BfFieldFlags_Static = 0x10,
  1525. BfFieldFlags_Const = 0x40,
  1526. BfFieldFlags_SpecialName = 0x80,
  1527. BfFieldFlags_EnumPayload = 0x100,
  1528. BfFieldFlags_EnumDiscriminator = 0x200,
  1529. BfFieldFlags_EnumCase = 0x400,
  1530. BfFieldFlags_ReadOnly = 0x800,
  1531. BfFieldFlags_Appended = 0x1000
  1532. };
  1533. enum BfReflectKind
  1534. {
  1535. BfReflectKind_None = 0,
  1536. BfReflectKind_Type = 1,
  1537. BfReflectKind_NonStaticFields = 2,
  1538. BfReflectKind_StaticFields = 4,
  1539. BfReflectKind_DefaultConstructor = 8,
  1540. BfReflectKind_Constructors = 0x10,
  1541. BfReflectKind_StaticMethods = 0x20,
  1542. BfReflectKind_Methods = 0x40,
  1543. BfReflectKind_DynamicBoxing = 0x80,
  1544. BfReflectKind_User = 0x100,
  1545. BfReflectKind_All = 0x1FF,
  1546. BfReflectKind_ApplyToInnerTypes = 0x200
  1547. };
  1548. class BfTypeOptions
  1549. {
  1550. public:
  1551. struct MethodFilter
  1552. {
  1553. String mFilter;
  1554. BfOptionFlags mOrFlags;
  1555. BfOptionFlags mAndFlags;
  1556. };
  1557. public:
  1558. Array<String> mTypeFilters;
  1559. Array<String> mAttributeFilters;
  1560. Array<int> mMatchedIndices;
  1561. int mSIMDSetting;
  1562. int mOptimizationLevel;
  1563. int mEmitDebugInfo;
  1564. BfOptionFlags mAndFlags;
  1565. BfOptionFlags mOrFlags;
  1566. Array<MethodFilter> mReflectMethodFilters;
  1567. Array<MethodFilter> mReflectMethodAttributeFilters;
  1568. int mAllocStackTraceDepth;
  1569. public:
  1570. static int Apply(int val, int applyVal)
  1571. {
  1572. if (applyVal != -1)
  1573. return applyVal;
  1574. return val;
  1575. }
  1576. bool Apply(bool val, BfOptionFlags flags)
  1577. {
  1578. if (val)
  1579. return (mAndFlags & flags) != 0;
  1580. else
  1581. return (mOrFlags & flags) != 0;
  1582. }
  1583. bool HasReflectMethodFilters()
  1584. {
  1585. return !mReflectMethodFilters.IsEmpty() || !mReflectMethodAttributeFilters.IsEmpty();
  1586. }
  1587. };
  1588. enum BfFindTypeDefFlags
  1589. {
  1590. BfFindTypeDefFlag_None,
  1591. BfFindTypeDefFlag_AllowGlobal
  1592. };
  1593. class BfSystem
  1594. {
  1595. public:
  1596. int mPtrSize;
  1597. bool mIsResolveOnly;
  1598. CritSect mDataLock; // short-lived, hold only while active modifying data
  1599. // The following are protected by mDataLock:
  1600. HashSet<String> mUsedSafeProjectNames;
  1601. Array<BfProject*> mProjects;
  1602. Array<BfProject*> mProjectDeleteQueue;
  1603. Array<BfParser*> mParsers;
  1604. Array<BfParser*> mParserDeleteQueue;
  1605. Array<BfTypeDef*> mTypeDefDeleteQueue;
  1606. Array<BfTypeOptions> mTypeOptions;
  1607. Array<BfTypeOptions> mMergedTypeOptions;
  1608. int mUpdateCnt;
  1609. bool mWorkspaceConfigChanged;
  1610. Val128 mWorkspaceConfigHash;
  1611. Array<BfCompiler*> mCompilers;
  1612. BfAtom* mGlobalsAtom;
  1613. BfAtom* mHiddenAtom;
  1614. BfAtom* mEmptyAtom;
  1615. BfAtom* mBfAtom;
  1616. CritSect mSystemLock; // long-lived, hold while compiling
  1617. int mYieldDisallowCount; // We can only yield lock when we are at 0
  1618. volatile int mCurSystemLockPri;
  1619. BfpThreadId mCurSystemLockThreadId;
  1620. volatile int mPendingSystemLockPri;
  1621. uint32 mYieldTickCount;
  1622. int mHighestYieldTime;
  1623. // The following are protected by mSystemLock - can only be accessed by the compiling thread
  1624. Dictionary<String, BfTypeDef*> mSystemTypeDefs;
  1625. BfTypeDefMap mTypeDefs;
  1626. bool mNeedsTypesHandledByCompiler;
  1627. BumpAllocator mAlloc;
  1628. int mAtomCreateIdx;
  1629. Dictionary<StringView, BfAtom*> mAtomMap;
  1630. Array<BfAtom*> mAtomGraveyard;
  1631. uint32 mAtomUpdateIdx;
  1632. int32 mTypeMapVersion; // Increment when we add any new types or namespaces
  1633. int32 mAnonymousAtomCount;
  1634. int32 mCurUniqueId;
  1635. OwnedVector<BfMethodDef> mMethodGraveyard;
  1636. OwnedVector<BfFieldDef> mFieldGraveyard;
  1637. OwnedVector<BfDirectStrTypeReference> mDirectTypeRefs;
  1638. OwnedVector<BfRefTypeRef> mRefTypeRefs;
  1639. public:
  1640. BfTypeDef* mTypeVoid;
  1641. BfTypeDef* mTypeNullPtr;
  1642. BfTypeDef* mTypeSelf;
  1643. BfTypeDef* mTypeDot;
  1644. BfTypeDef* mTypeVar;
  1645. BfTypeDef* mTypeLet;
  1646. BfTypeDef* mTypeBool;
  1647. BfTypeDef* mTypeIntPtr;
  1648. BfTypeDef* mTypeUIntPtr;
  1649. BfTypeDef* mTypeIntUnknown;
  1650. BfTypeDef* mTypeUIntUnknown;
  1651. BfTypeDef* mTypeInt8;
  1652. BfTypeDef* mTypeUInt8;
  1653. BfTypeDef* mTypeInt16;
  1654. BfTypeDef* mTypeUInt16;
  1655. BfTypeDef* mTypeInt32;
  1656. BfTypeDef* mTypeUInt32;
  1657. BfTypeDef* mTypeInt64;
  1658. BfTypeDef* mTypeUInt64;
  1659. BfTypeDef* mTypeChar8;
  1660. BfTypeDef* mTypeChar16;
  1661. BfTypeDef* mTypeChar32;
  1662. BfTypeDef* mTypeSingle;
  1663. BfTypeDef* mTypeDouble;
  1664. BfDirectStrTypeReference* mDirectVoidTypeRef;
  1665. BfDirectStrTypeReference* mDirectBoolTypeRef;
  1666. BfDirectStrTypeReference* mDirectSelfTypeRef;
  1667. BfDirectStrTypeReference* mDirectSelfBaseTypeRef;
  1668. BfRefTypeRef* mDirectRefSelfBaseTypeRef;
  1669. BfDirectStrTypeReference* mDirectObjectTypeRef;
  1670. BfDirectStrTypeReference* mDirectStringTypeRef;
  1671. BfDirectStrTypeReference* mDirectIntTypeRef;
  1672. BfRefTypeRef* mDirectRefIntTypeRef;
  1673. BfDirectStrTypeReference* mDirectInt32TypeRef;
  1674. public:
  1675. BfSystem();
  1676. ~BfSystem();
  1677. BfAtom* GetAtom(const StringImpl& string, BfAtom::Kind kind = BfAtom::Kind_Normal);
  1678. BfAtom* FindAtom(const StringImpl& string); // Doesn't create a ref
  1679. BfAtom* FindAtom(const StringView& string); // Doesn't create a ref
  1680. void ReleaseAtom(BfAtom* atom);
  1681. void ProcessAtomGraveyard();
  1682. void RefAtomComposite(const BfAtomComposite& atomComposite);
  1683. void ReleaseAtomComposite(const BfAtomComposite& atomComposite);
  1684. void SanityCheckAtomComposite(const BfAtomComposite& atomComposite);
  1685. void TrackName(BfTypeDef* typeDef);
  1686. void UntrackName(BfTypeDef* typeDef);
  1687. bool ParseAtomComposite(const StringView& name, BfAtomComposite& composite, bool addRefs = false);
  1688. void CreateBasicTypes();
  1689. bool DoesLiteralFit(BfTypeCode typeCode, int64 value);
  1690. bool DoesLiteralFit(BfTypeCode typeCode, uint64 value);
  1691. bool DoesLiteralFit(BfTypeCode typeCode, const BfVariant& variant);
  1692. BfParser* CreateParser(BfProject* bfProject);
  1693. BfCompiler* CreateCompiler(bool isResolveOnly);
  1694. BfProject* GetProject(const StringImpl& projName);
  1695. uint64 GetTypeDeclListHash();
  1696. BfTypeReference* GetTypeRefElement(BfTypeReference* typeRef);
  1697. BfTypeDef* FilterDeletedTypeDef(BfTypeDef* typeDef);
  1698. bool CheckTypeDefReference(BfTypeDef* typeDef, BfProject* project);
  1699. BfTypeDef* FindTypeDef(const BfAtomComposite& findName, int numGenericArgs = 0, BfProject* project = NULL, const Array<BfAtomComposite>& namespaceSearch = Array<BfAtomComposite>(), BfTypeDef** ambiguousTypeDef = NULL, BfFindTypeDefFlags flags = BfFindTypeDefFlag_None);
  1700. bool FindTypeDef(const BfAtomComposite& findName, int numGenericArgs, BfProject* project, const BfAtomComposite& checkNamespace, bool allowPrivate, BfTypeDefLookupContext* ctx);
  1701. BfTypeDef* FindTypeDef(const StringImpl& typeName, int numGenericArgs = 0, BfProject* project = NULL, const Array<BfAtomComposite>& namespaceSearch = Array<BfAtomComposite>(), BfTypeDef** ambiguousTypeDef = NULL, BfFindTypeDefFlags flags = BfFindTypeDefFlag_None);
  1702. BfTypeDef* FindTypeDef(const StringImpl& typeName, BfProject* project);
  1703. BfTypeDef* FindTypeDefEx(const StringImpl& typeName);
  1704. void ClearTypeDefCache();
  1705. void FindFixitNamespaces(const StringImpl& typeName, int numGenericArgs, BfProject* project, std::set<String>& fixitNamespaces);
  1706. void RemoveTypeDef(BfTypeDef* typeDef);
  1707. //BfTypeDefMap::Iterator RemoveTypeDef(BfTypeDefMap::Iterator typeDefItr);
  1708. void AddNamespaceUsage(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1709. void RemoveNamespaceUsage(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1710. bool ContainsNamespace(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1711. void InjectNewRevision(BfTypeDef* typeDef);
  1712. void AddToCompositePartial(BfPassInstance* passInstance, BfTypeDef* compositeTypeDef, BfTypeDef* partialTypeDef);
  1713. void FinishCompositePartial(BfTypeDef* compositeTypeDef);
  1714. void CopyTypeDef(BfTypeDef* typeDef, BfTypeDef* nextTypeDef);
  1715. void UpdateEmittedTypeDef(BfTypeDef* typeDef);
  1716. BfTypeDef* GetCombinedPartial(BfTypeDef* typeDef);
  1717. BfTypeDef* GetOuterTypeNonPartial(BfTypeDef* typeDef);
  1718. int GetGenericParamIdx(const Array<BfGenericParamDef*>& genericParams, const StringImpl& name);
  1719. int GetGenericParamIdx(const Array<BfGenericParamDef*>& genericParams, BfTypeReference* typeRef);
  1720. void StartYieldSection();
  1721. void CheckLockYield(); // Yields to a higher priority request
  1722. void SummarizeYieldSection();
  1723. void NotifyWillRequestLock(int priority);
  1724. void Lock(int priority);
  1725. void Unlock();
  1726. void AssertWeHaveLock();
  1727. void RemoveDeletedParsers();
  1728. void RemoveOldParsers();
  1729. void RemoveOldData();
  1730. void VerifyTypeDef(BfTypeDef* typeDef);
  1731. BfPassInstance* CreatePassInstance();
  1732. BfTypeOptions* GetTypeOptions(int optionsIdx);
  1733. bool HasTestProjects();
  1734. bool IsCompatibleCallingConvention(BfCallingConvention callConvA, BfCallingConvention callConvB);
  1735. };
  1736. class AutoDisallowYield
  1737. {
  1738. public:
  1739. BfSystem* mSystem;
  1740. bool mHeld;
  1741. public:
  1742. AutoDisallowYield(BfSystem* system)
  1743. {
  1744. mSystem = system;
  1745. mSystem->mYieldDisallowCount++;
  1746. mHeld = true;
  1747. }
  1748. ~AutoDisallowYield()
  1749. {
  1750. if (mHeld)
  1751. mSystem->mYieldDisallowCount--;
  1752. }
  1753. void Release()
  1754. {
  1755. BF_ASSERT(mHeld);
  1756. if (mHeld)
  1757. {
  1758. mHeld = false;
  1759. mSystem->mYieldDisallowCount--;
  1760. }
  1761. }
  1762. void Acquire()
  1763. {
  1764. BF_ASSERT(!mHeld);
  1765. if (!mHeld)
  1766. {
  1767. mHeld = true;
  1768. mSystem->mYieldDisallowCount++;
  1769. }
  1770. }
  1771. };
  1772. #ifdef _DEBUG
  1773. #ifdef BF_PLATFORM_WINDOWS
  1774. #define BF_WANTS_LOG_HI
  1775. #define BF_WANTS_LOG
  1776. #define BF_WANTS_LOG_SYS
  1777. //#define BF_WANTS_LOG2
  1778. //#define BF_WANTS_LOG_CLANG
  1779. #define BF_WANTS_LOG_DBG
  1780. #define BF_WANTS_LOG_DBGEXPR
  1781. //#define BF_WANTS_LOG_CV
  1782. #endif
  1783. #else
  1784. #define BF_WANTS_LOG_HI
  1785. //#define BF_WANTS_LOG
  1786. //#define BF_WANTS_LOG_SYS
  1787. //#define BF_WANTS_LOG2
  1788. //#define BF_WANTS_LOG_CLANG
  1789. //#define BF_WANTS_LOG_DBGEXPR
  1790. //#define BF_WANTS_LOG_CV
  1791. //#define BF_WANTS_LOG_DBG
  1792. #endif
  1793. #ifdef BF_WANTS_LOG
  1794. #define BfLog(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1795. #else
  1796. //#define BfLog(fmt) {} // Nothing
  1797. #define BfLog(fmt, ...) {} // Nothing
  1798. #endif
  1799. #ifdef BF_WANTS_LOG
  1800. #define BfLogX(logIdx, fmt, ...) DoBfLog(logIdx, fmt, ##__VA_ARGS__)
  1801. #else
  1802. #define BfLogX(logIdx, fmt, ...) {} // Nothing
  1803. #endif
  1804. #ifdef BF_WANTS_LOG_SYS
  1805. #define BfLogSys(sys, fmt, ...) DoBfLog((sys)->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1806. #define BfLogSysM(fmt, ...) DoBfLog(mSystem->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1807. #else
  1808. #define BfLogSys(...) {} // Nothing
  1809. #define BfLogSysM(...) {} // Nothing
  1810. #endif
  1811. #ifdef BF_WANTS_LOG_HI
  1812. #define BfLogSysHI(sys, fmt, ...) DoBfLog((sys)->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1813. #define BfLogSysMHI(fmt, ...) DoBfLog(mSystem->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1814. #define BfLogDbgHI(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1815. #else
  1816. #define BfLogSysHI(...) {} // Nothing
  1817. #define BfLogSysMHI(...) {} // Nothing
  1818. #endif
  1819. #ifdef BF_WANTS_LOG_CLANG
  1820. //#define BfLogClang(fmt) DoBfLog(fmt)
  1821. #define BfLogClang(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1822. #else
  1823. #define BfLogClang(fmt, ...) {} // Nothing
  1824. #endif
  1825. #ifdef BF_WANTS_LOG_DBG
  1826. #define BfLogDbg(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1827. #else
  1828. #define BfLogDbg(fmt, ...) {} // Nothing
  1829. #endif
  1830. #ifdef BF_WANTS_LOG_DBGEXPR
  1831. #define BfLogDbgExpr(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1832. #else
  1833. #define BfLogDbgExpr(fmt, ...) {} // Nothing
  1834. #endif
  1835. #ifdef BF_WANTS_LOG2
  1836. #define BfLog2(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1837. #else
  1838. #define BfLog2(fmt, ...) {} // Nothing
  1839. #endif
  1840. #ifdef BF_WANTS_LOG_CV
  1841. #define BfLogCv(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1842. #else
  1843. #define BfLogCv(fmt, ...) {} // Nothing
  1844. #endif
  1845. void DoBfLog(int fileIdx, const char* fmt ...);
  1846. NS_BF_END
  1847. namespace std
  1848. {
  1849. template <>
  1850. struct hash<Beefy::BfAtomComposite>
  1851. {
  1852. size_t operator()(const Beefy::BfAtomComposite& composite) const
  1853. {
  1854. int curHash = 0;
  1855. for (int i = 0; i < (int)composite.mSize; i++)
  1856. curHash = ((curHash ^ (int)(intptr)composite.mParts[i]->mHash) << 5) - curHash;
  1857. return curHash;
  1858. }
  1859. };
  1860. template <>
  1861. struct hash<Beefy::BfMemberSetEntry>
  1862. {
  1863. size_t operator()(const Beefy::BfMemberSetEntry& entry) const
  1864. {
  1865. return std::hash<Beefy::String>()(entry.mMemberDef->mName);
  1866. }
  1867. };
  1868. }
  1869. namespace std
  1870. {
  1871. template<>
  1872. struct hash<Beefy::BfErrorEntry>
  1873. {
  1874. size_t operator()(const Beefy::BfErrorEntry& val) const
  1875. {
  1876. return val.GetHashCode();
  1877. }
  1878. };
  1879. }