BfSystem.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 BfTypeReference;
  35. class BfCompiler;
  36. class BfProject;
  37. struct BfTypeDefMapFuncs;
  38. typedef MultiHashSet<BfTypeDef*, BfTypeDefMapFuncs> BfTypeDefMap;
  39. class BfAtom
  40. {
  41. public:
  42. StringView mString;
  43. int mRefCount;
  44. int mHash;
  45. uint32 mAtomUpdateIdx;
  46. bool mIsSystemType;
  47. Dictionary<BfAtom*, int> mPrevNamesMap;
  48. public:
  49. ~BfAtom();
  50. const StringView& ToString()
  51. {
  52. return mString;
  53. }
  54. void ToString(StringImpl& str)
  55. {
  56. str += mString;
  57. }
  58. void Ref();
  59. };
  60. class BfAtomComposite
  61. {
  62. public:
  63. BfAtom** mParts;
  64. int16 mSize;
  65. int16 mAllocSize;
  66. bool mOwns;
  67. public:
  68. BfAtomComposite();
  69. BfAtomComposite(BfAtomComposite&& rhs);
  70. BfAtomComposite(const BfAtomComposite& rhs);
  71. BfAtomComposite(BfAtom* atom);
  72. BfAtomComposite(const BfAtomComposite& left, const BfAtomComposite& right);
  73. BfAtomComposite(const BfAtomComposite& left, BfAtom* right);
  74. ~BfAtomComposite();
  75. void Set(const BfAtomComposite& left, const BfAtomComposite& right);
  76. void Set(BfAtom** atomsA, int countA, BfAtom** atomsB, int countB);
  77. BfAtomComposite& operator=(const BfAtomComposite& rhs);
  78. bool operator==(const BfAtomComposite& other) const;
  79. bool operator!=(const BfAtomComposite& other) const;
  80. bool IsValid() const;
  81. bool IsEmpty() const;
  82. int GetPartsCount() const;
  83. String ToString() const;
  84. void ToString(StringImpl& str) const;
  85. bool StartsWith(const BfAtomComposite& other) const;
  86. bool EndsWith(const BfAtomComposite& other) const;
  87. BfAtomComposite GetSub(int start, int len) const;
  88. void Reference(const BfAtomComposite& other);
  89. uint32 GetAtomUpdateIdx();
  90. };
  91. class BfSizedAtomComposite : public BfAtomComposite
  92. {
  93. public:
  94. BfAtom* mInitialAlloc[8];
  95. BfSizedAtomComposite();
  96. ~BfSizedAtomComposite();
  97. };
  98. struct BfAtomCompositeHash
  99. {
  100. size_t operator()(const BfAtomComposite& composite) const
  101. {
  102. int curHash = 0;
  103. for (int i = 0; i < (int)composite.mSize; i++)
  104. curHash = ((curHash ^ (int)(intptr)composite.mParts[i]->mHash) << 5) - curHash;
  105. return curHash;
  106. }
  107. };
  108. struct BfAtomCompositeEquals
  109. {
  110. bool operator()(const BfAtomComposite& lhs, const BfAtomComposite& rhs) const
  111. {
  112. if (lhs.mSize != rhs.mSize)
  113. return false;
  114. for (int i = 0; i < lhs.mSize; i++)
  115. if (lhs.mParts[i] != rhs.mParts[i])
  116. return false;
  117. return true;
  118. }
  119. };
  120. enum BfCompilerOptionFlags
  121. {
  122. BfCompilerOptionFlag_EmitDebugInfo = 1,
  123. BfCompilerOptionFlag_EmitLineInfo = 2,
  124. BfCompilerOptionFlag_WriteIR = 4,
  125. BfCompilerOptionFlag_GenerateOBJ = 8,
  126. BfCompilerOptionFlag_NoFramePointerElim = 0x10,
  127. BfCompilerOptionFlag_ClearLocalVars = 0x20,
  128. BfCompilerOptionFlag_RuntimeChecks = 0x40,
  129. BfCompilerOptionFlag_EmitDynamicCastCheck = 0x80,
  130. BfCompilerOptionFlag_EnableObjectDebugFlags = 0x100,
  131. BfCompilerOptionFlag_EmitObjectAccessCheck = 0x200,
  132. BfCompilerOptionFlag_EnableCustodian = 0x400,
  133. BfCompilerOptionFlag_EnableRealtimeLeakCheck = 0x800,
  134. BfCompilerOptionFlag_EnableSideStack = 0x1000,
  135. BfCompilerOptionFlag_EnableHotSwapping = 0x2000,
  136. BfCompilerOptionFlag_IncrementalBuild = 0x4000,
  137. BfCompilerOptionFlag_DebugAlloc = 0x8000,
  138. BfCompilerOptionFlag_OmitDebugHelpers = 0x10000
  139. };
  140. enum BfTypeFlags
  141. {
  142. BfTypeFlags_UnspecializedGeneric = 0x0001,
  143. BfTypeFlags_SpecializedGeneric = 0x0002,
  144. BfTypeFlags_Array = 0x0004,
  145. BfTypeFlags_Object = 0x0008,
  146. BfTypeFlags_Boxed = 0x0010,
  147. BfTypeFlags_Pointer = 0x0020,
  148. BfTypeFlags_Struct = 0x0040,
  149. BfTypeFlags_Primitive = 0x0080,
  150. BfTypeFlags_TypedPrimitive = 0x0100,
  151. BfTypeFlags_Tuple = 0x0200,
  152. BfTypeFlags_Nullable = 0x0400,
  153. BfTypeFlags_SizedArray = 0x0800,
  154. BfTypeFlags_Splattable = 0x1000,
  155. BfTypeFlags_Union = 0x2000,
  156. //
  157. BfTypeFlags_WantsMarking = 0x8000,
  158. BfTypeFlags_Delegate = 0x10000,
  159. BfTypeFlags_HasDestructor = 0x20000,
  160. };
  161. enum BfObjectFlags : uint8
  162. {
  163. BfObjectFlag_None = 0,
  164. BfObjectFlag_MarkIdMask = 0x03,
  165. BfObjectFlag_Allocated = 0x04,
  166. BfObjectFlag_StackAlloc = 0x08,
  167. BfObjectFlag_AppendAlloc = 0x10,
  168. BfObjectFlag_AllocInfo = 0x20,
  169. BfObjectFlag_AllocInfo_Short = 0x40,
  170. BfObjectFlag_Deleted = 0x80,
  171. BfObjectFlag_StackDeleted = 0x80 // We remove StackAlloc so it doesn't get scanned
  172. };
  173. enum BfCustomAttributeFlags
  174. {
  175. BfCustomAttributeFlags_None,
  176. BfCustomAttributeFlags_DisallowAllowMultiple = 1,
  177. BfCustomAttributeFlags_NotInherited = 2,
  178. BfCustomAttributeFlags_ReflectAttribute = 4,
  179. BfCustomAttributeFlags_AlwaysIncludeTarget = 8
  180. };
  181. enum BfMachineType
  182. {
  183. BfMachineType_Unknown,
  184. BfMachineType_x86,
  185. BfMachineType_x64,
  186. BfMachineType_AArch64
  187. };
  188. enum BfToolsetType
  189. {
  190. BfToolsetType_GNU,
  191. BfToolsetType_Microsoft,
  192. BfToolsetType_LLVM
  193. };
  194. enum BfSIMDSetting
  195. {
  196. BfSIMDSetting_None,
  197. BfSIMDSetting_MMX,
  198. BfSIMDSetting_SSE,
  199. BfSIMDSetting_SSE2,
  200. BfSIMDSetting_SSE3,
  201. BfSIMDSetting_SSE4,
  202. BfSIMDSetting_SSE41,
  203. BfSIMDSetting_AVX,
  204. BfSIMDSetting_AVX2,
  205. };
  206. enum BfAsmKind
  207. {
  208. BfAsmKind_None,
  209. BfAsmKind_ATT,
  210. BfAsmKind_Intel,
  211. };
  212. enum BfOptLevel
  213. {
  214. BfOptLevel_NotSet = -1,
  215. BfOptLevel_O0 = 0,
  216. BfOptLevel_O1,
  217. BfOptLevel_O2,
  218. BfOptLevel_O3,
  219. BfOptLevel_Og,
  220. BfOptLevel_OgPlus
  221. };
  222. enum BfLTOType
  223. {
  224. BfLTOType_None = 0,
  225. BfLTOType_Thin = 1
  226. };
  227. enum BfCFLAAType
  228. {
  229. BfCFLAAType_None,
  230. BfCFLAAType_Steensgaard,
  231. BfCFLAAType_Andersen,
  232. BfCFLAAType_Both
  233. };
  234. struct BfCodeGenOptions
  235. {
  236. bool mIsHotCompile;
  237. bool mWriteObj;
  238. BfAsmKind mAsmKind;
  239. bool mWriteToLib;
  240. bool mWriteLLVMIR;
  241. int16 mVirtualMethodOfs;
  242. int16 mDynSlotOfs;
  243. BfSIMDSetting mSIMDSetting;
  244. BfOptLevel mOptLevel;
  245. BfLTOType mLTOType;
  246. int mSizeLevel;
  247. BfCFLAAType mUseCFLAA;
  248. bool mUseNewSROA;
  249. bool mDisableTailCalls;
  250. bool mDisableUnitAtATime;
  251. bool mDisableUnrollLoops;
  252. bool mBBVectorize;
  253. bool mSLPVectorize;
  254. bool mLoopVectorize;
  255. bool mRerollLoops;
  256. bool mLoadCombine;
  257. bool mDisableGVNLoadPRE;
  258. bool mVerifyInput;
  259. bool mVerifyOutput;
  260. bool mStripDebug;
  261. bool mMergeFunctions;
  262. bool mEnableMLSM;
  263. bool mRunSLPAfterLoopVectorization;
  264. bool mUseGVNAfterVectorization;
  265. bool mEnableLoopInterchange;
  266. bool mEnableLoopLoadElim;
  267. bool mExtraVectorizerPasses;
  268. bool mEnableEarlyCSEMemSSA;
  269. bool mEnableGVNHoist;
  270. bool mEnableGVNSink;
  271. bool mDisableLibCallsShrinkWrap;
  272. bool mExpensiveCombines;
  273. bool mEnableSimpleLoopUnswitch;
  274. bool mDivergentTarget;
  275. bool mNewGVN;
  276. bool mRunPartialInlining;
  277. bool mUseLoopVersioningLICM;
  278. bool mEnableUnrollAndJam;
  279. bool mEnableHotColdSplit;
  280. Val128 mHash;
  281. BfCodeGenOptions()
  282. {
  283. mIsHotCompile = false;
  284. mWriteObj = true;
  285. mAsmKind = BfAsmKind_None;
  286. mWriteToLib = false;
  287. mWriteLLVMIR = false;
  288. mVirtualMethodOfs = 0;
  289. mDynSlotOfs = 0;
  290. mSIMDSetting = BfSIMDSetting_None;
  291. mOptLevel = BfOptLevel_O0;
  292. mLTOType = BfLTOType_None;
  293. mSizeLevel = 0;
  294. mUseCFLAA = BfCFLAAType_None;
  295. mUseNewSROA = false;
  296. mDisableTailCalls = false;
  297. mDisableUnitAtATime = false;
  298. mDisableUnrollLoops = false;
  299. mBBVectorize = false;
  300. mSLPVectorize = false;
  301. mLoopVectorize = false;
  302. mRerollLoops = false;
  303. mLoadCombine = false;
  304. mDisableGVNLoadPRE = false;
  305. mVerifyInput = false;
  306. mVerifyOutput = false;
  307. mStripDebug = false;
  308. mMergeFunctions = false;
  309. mEnableMLSM = false;
  310. mRunSLPAfterLoopVectorization = false;
  311. mUseGVNAfterVectorization = false;
  312. mEnableLoopInterchange = false;
  313. mEnableLoopLoadElim = true;
  314. mExtraVectorizerPasses = false;
  315. mEnableEarlyCSEMemSSA = true;
  316. mEnableGVNHoist = false;
  317. mEnableGVNSink = false;
  318. mDisableLibCallsShrinkWrap = false;
  319. mExpensiveCombines = false;
  320. mEnableSimpleLoopUnswitch = false;
  321. mDivergentTarget = false;
  322. mNewGVN = false;
  323. mRunPartialInlining = false;
  324. mUseLoopVersioningLICM = false;
  325. mEnableUnrollAndJam = false;
  326. mEnableHotColdSplit = false;
  327. }
  328. void GenerateHash()
  329. {
  330. HashContext hashCtx;
  331. hashCtx.Mixin(mWriteObj);
  332. hashCtx.Mixin(mWriteToLib);
  333. hashCtx.Mixin(mWriteLLVMIR);
  334. hashCtx.Mixin(mVirtualMethodOfs);
  335. hashCtx.Mixin(mDynSlotOfs);
  336. hashCtx.Mixin(mSIMDSetting);
  337. hashCtx.Mixin(mOptLevel);
  338. hashCtx.Mixin(mLTOType);
  339. hashCtx.Mixin(mSizeLevel);
  340. hashCtx.Mixin(mUseCFLAA);
  341. hashCtx.Mixin(mUseNewSROA);
  342. hashCtx.Mixin(mDisableTailCalls);
  343. hashCtx.Mixin(mDisableUnitAtATime);
  344. hashCtx.Mixin(mDisableUnrollLoops);
  345. hashCtx.Mixin(mBBVectorize);
  346. hashCtx.Mixin(mSLPVectorize);
  347. hashCtx.Mixin(mLoopVectorize);
  348. hashCtx.Mixin(mRerollLoops);
  349. hashCtx.Mixin(mLoadCombine);
  350. hashCtx.Mixin(mDisableGVNLoadPRE);
  351. hashCtx.Mixin(mVerifyInput);
  352. hashCtx.Mixin(mVerifyOutput);
  353. hashCtx.Mixin(mStripDebug);
  354. hashCtx.Mixin(mMergeFunctions);
  355. hashCtx.Mixin(mEnableMLSM);
  356. hashCtx.Mixin(mRunSLPAfterLoopVectorization);
  357. hashCtx.Mixin(mUseGVNAfterVectorization);
  358. hashCtx.Mixin(mEnableLoopInterchange);
  359. hashCtx.Mixin(mEnableLoopLoadElim);
  360. hashCtx.Mixin(mExtraVectorizerPasses);
  361. mHash = hashCtx.Finish128();
  362. }
  363. };
  364. enum BfParamKind : uint8
  365. {
  366. BfParamKind_Normal,
  367. BfParamKind_Params,
  368. BfParamKind_DelegateParam,
  369. BfParamKind_ImplicitCapture,
  370. BfParamKind_AppendIdx,
  371. };
  372. class BfParameterDef
  373. {
  374. public:
  375. String mName;
  376. BfTypeReference* mTypeRef;
  377. BfParameterDeclaration* mParamDeclaration;
  378. int mMethodGenericParamIdx;
  379. BfParamKind mParamKind;
  380. public:
  381. BfParameterDef()
  382. {
  383. mTypeRef = NULL;
  384. mMethodGenericParamIdx = -1;
  385. mParamKind = BfParamKind_Normal;
  386. mParamDeclaration = NULL;
  387. }
  388. };
  389. class BfMemberDef
  390. {
  391. public:
  392. String mName;
  393. BfTypeDef* mDeclaringType;
  394. BfProtection mProtection;
  395. bool mIsStatic;
  396. bool mIsNoShow;
  397. bool mIsReadOnly;
  398. bool mHasMultiDefs;
  399. public:
  400. BfMemberDef()
  401. {
  402. mDeclaringType = NULL;
  403. mProtection = BfProtection_Public;
  404. mIsStatic = false;
  405. mIsNoShow = false;
  406. mIsReadOnly = false;
  407. mHasMultiDefs = false;
  408. }
  409. virtual ~BfMemberDef()
  410. {
  411. }
  412. };
  413. class BfFieldDef : public BfMemberDef
  414. {
  415. public:
  416. int mIdx;
  417. bool mIsConst; // Note: Consts are also all considered Static
  418. bool mIsInline;
  419. bool mIsVolatile;
  420. bool mIsExtern;
  421. BfTypeReference* mTypeRef;
  422. BfExpression* mInitializer;
  423. BfFieldDeclaration* mFieldDeclaration;
  424. // It may seem that fields and properties don't need a 'mNextWithSameName', but with extensions it's possible
  425. // to have two libraries which each add a field to a type with the same name
  426. BfFieldDef* mNextWithSameName;
  427. public:
  428. BfFieldDef()
  429. {
  430. mIdx = 0;
  431. mIsConst = false;
  432. mIsInline = false;
  433. mIsExtern = false;
  434. mIsVolatile = false;
  435. mTypeRef = NULL;
  436. mInitializer = NULL;
  437. mFieldDeclaration = NULL;
  438. mNextWithSameName = NULL;
  439. }
  440. bool IsUnnamedTupleField()
  441. {
  442. return (mName[0] >= '0') && (mName[0] <= '9');
  443. }
  444. bool IsEnumCaseEntry()
  445. {
  446. return (mFieldDeclaration != NULL) && (BfNodeIsA<BfEnumEntryDeclaration>(mFieldDeclaration));
  447. }
  448. bool IsNonConstStatic()
  449. {
  450. return mIsStatic && !mIsConst;
  451. }
  452. BfAstNode* GetRefNode()
  453. {
  454. if (mFieldDeclaration == NULL)
  455. return NULL;
  456. if (mFieldDeclaration->mNameNode != NULL)
  457. return mFieldDeclaration->mNameNode;
  458. return mFieldDeclaration;
  459. }
  460. };
  461. class BfPropertyDef : public BfFieldDef
  462. {
  463. public:
  464. Array<BfMethodDef*> mMethods;
  465. BfPropertyDef* mNextWithSameName;
  466. public:
  467. BfPropertyDef()
  468. {
  469. mNextWithSameName = NULL;
  470. }
  471. bool HasExplicitInterface();
  472. BfAstNode* GetRefNode();
  473. };
  474. enum BfGenericParamFlags : uint8
  475. {
  476. BfGenericParamFlag_None = 0,
  477. BfGenericParamFlag_Class = 1,
  478. BfGenericParamFlag_Struct = 2,
  479. BfGenericParamFlag_StructPtr = 4,
  480. BfGenericParamFlag_New = 8,
  481. BfGenericParamFlag_Delete = 16,
  482. BfGenericParamFlag_Var = 32,
  483. BfGenericParamFlag_Const = 64
  484. };
  485. class BfGenericParamDef
  486. {
  487. public:
  488. //BfTypeDef* mOwner;
  489. String mName;
  490. Array<BfIdentifierNode*> mNameNodes; // 0 is always the def name
  491. BfGenericParamFlags mGenericParamFlags;
  492. Array<BfTypeReference*> mInterfaceConstraints;
  493. };
  494. // CTOR is split into two for Objects - Ctor clears and sets up VData, Ctor_Body executes ctor body code
  495. enum BfMethodType : uint8
  496. {
  497. BfMethodType_Ignore,
  498. BfMethodType_Normal,
  499. //BfMethodType_Lambda,
  500. BfMethodType_PropertyGetter,
  501. BfMethodType_PropertySetter,
  502. BfMethodType_CtorCalcAppend,
  503. BfMethodType_Ctor,
  504. BfMethodType_CtorNoBody,
  505. BfMethodType_CtorClear,
  506. BfMethodType_Dtor,
  507. BfMethodType_Operator,
  508. BfMethodType_Mixin
  509. };
  510. enum BfCallingConvention : uint8
  511. {
  512. BfCallingConvention_Unspecified,
  513. BfCallingConvention_Cdecl,
  514. BfCallingConvention_Stdcall,
  515. BfCallingConvention_Fastcall,
  516. BfCallingConvention_CVarArgs,
  517. };
  518. #define BF_METHODNAME_MARKMEMBERS "GCMarkMembers"
  519. #define BF_METHODNAME_MARKMEMBERS_STATIC "GCMarkStaticMembers"
  520. #define BF_METHODNAME_FIND_TLS_MEMBERS "GCFindTLSMembers"
  521. #define BF_METHODNAME_DYNAMICCAST "DynamicCastToTypeId"
  522. #define BF_METHODNAME_DYNAMICCAST_INTERFACE "DynamicCastToInterface"
  523. #define BF_METHODNAME_CALCAPPEND "this$calcAppend"
  524. #define BF_METHODNAME_ENUM_HASFLAG "HasFlag"
  525. #define BF_METHODNAME_ENUM_GETUNDERLYING "get__Underlying"
  526. #define BF_METHODNAME_ENUM_GETUNDERLYINGREF "get__UnderlyingRef"
  527. #define BF_METHODNAME_EQUALS "Equals"
  528. #define BF_METHODNAME_INVOKE "Invoke"
  529. #define BF_METHODNAME_TO_STRING "ToString"
  530. #define BF_METHODNAME_DEFAULT_EQUALS "__Equals"
  531. enum BfOptimize : int8
  532. {
  533. BfOptimize_Default,
  534. BfOptimize_Unoptimized,
  535. BfOptimize_Optimized
  536. };
  537. enum BfImportKind : int8
  538. {
  539. BfImportKind_None,
  540. BfImportKind_Static,
  541. BfImportKind_Dynamic,
  542. BfImportKind_Export
  543. };
  544. enum BfCheckedKind : int8
  545. {
  546. BfCheckedKind_NotSet,
  547. BfCheckedKind_Checked,
  548. BfCheckedKind_Unchecked
  549. };
  550. class BfMethodDef : public BfMemberDef
  551. {
  552. public:
  553. BfAstNode* mMethodDeclaration;
  554. BfAstNode* mBody;
  555. BfTypeReference* mExplicitInterface;
  556. BfTypeReference* mReturnTypeRef;
  557. Array<BfParameterDef*> mParams;
  558. Array<BfGenericParamDef*> mGenericParams;
  559. BfMethodDef* mNextWithSameName;
  560. Val128 mFullHash;
  561. int mIdx;
  562. int mPropertyIdx;
  563. BfMethodType mMethodType;
  564. bool mIsLocalMethod;
  565. bool mIsVirtual;
  566. bool mIsOverride;
  567. bool mIsAbstract;
  568. bool mIsConcrete;
  569. bool mIsPartial;
  570. bool mIsNew;
  571. bool mCodeChanged;
  572. bool mWantsBody;
  573. bool mCLink;
  574. bool mHasAppend;
  575. bool mAlwaysInline;
  576. bool mNoReturn;
  577. bool mIsMutating;
  578. bool mNoSplat;
  579. bool mNoReflect;
  580. bool mIsSkipCall;
  581. bool mIsOperator;
  582. bool mIsExtern;
  583. bool mIsNoDiscard;
  584. BfCheckedKind mCheckedKind;
  585. BfImportKind mImportKind;
  586. BfCallingConvention mCallingConvention;
  587. public:
  588. BfMethodDef()
  589. {
  590. mIdx = -1;
  591. mPropertyIdx = -1;
  592. mIsLocalMethod = false;
  593. mIsVirtual = false;
  594. mIsOverride = false;
  595. mIsAbstract = false;
  596. mIsConcrete = false;
  597. mIsStatic = false;
  598. mIsNew = false;
  599. mIsPartial = false;
  600. mCLink = false;
  601. mNoReturn = false;
  602. mIsMutating = false;
  603. mNoSplat = false;
  604. mNoReflect = false;
  605. mIsSkipCall = false;
  606. mIsOperator = false;
  607. mIsExtern = false;
  608. mIsNoDiscard = false;
  609. mBody = NULL;
  610. mExplicitInterface = NULL;
  611. mReturnTypeRef = NULL;
  612. mMethodDeclaration = NULL;
  613. mCodeChanged = false;
  614. mWantsBody = true;
  615. mCheckedKind = BfCheckedKind_NotSet;
  616. mImportKind = BfImportKind_None;
  617. mMethodType = BfMethodType_Normal;
  618. mCallingConvention = BfCallingConvention_Unspecified;
  619. mHasAppend = false;
  620. mAlwaysInline = false;
  621. mNextWithSameName = NULL;
  622. }
  623. virtual ~BfMethodDef();
  624. bool HasNoThisSplat() { return mIsMutating || mNoSplat; }
  625. void Reset();
  626. void FreeMembers();
  627. BfMethodDeclaration* GetMethodDeclaration();
  628. BfPropertyMethodDeclaration* GetPropertyMethodDeclaration();
  629. BfPropertyDeclaration* GetPropertyDeclaration();
  630. BfAstNode* GetRefNode();
  631. BfTokenNode* GetMutNode();
  632. bool HasBody();
  633. bool IsEmptyPartial();
  634. bool IsDefaultCtor();
  635. String ToString();
  636. };
  637. class BfOperatorDef : public BfMethodDef
  638. {
  639. public:
  640. BfOperatorDeclaration* mOperatorDeclaration;
  641. public:
  642. BfOperatorDef()
  643. {
  644. mOperatorDeclaration = NULL;
  645. }
  646. };
  647. struct BfTypeDefLookupContext
  648. {
  649. public:
  650. int mBestPri;
  651. BfTypeDef* mBestTypeDef;
  652. BfTypeDef* mAmbiguousTypeDef;
  653. public:
  654. BfTypeDefLookupContext()
  655. {
  656. mBestPri = (int)0x80000000;
  657. mBestTypeDef = NULL;
  658. mAmbiguousTypeDef = NULL;
  659. }
  660. bool HasValidMatch()
  661. {
  662. return (mBestPri >= 0) && (mBestTypeDef != NULL);
  663. }
  664. };
  665. struct BfMemberSetEntry
  666. {
  667. BfMemberDef* mMemberDef;
  668. BfMemberSetEntry(BfMemberDef* memberDef)
  669. {
  670. mMemberDef = memberDef;
  671. }
  672. bool operator==(const BfMemberSetEntry& other) const
  673. {
  674. return mMemberDef->mName == other.mMemberDef->mName;
  675. }
  676. bool operator==(const String& other) const
  677. {
  678. return mMemberDef->mName == other;
  679. }
  680. };
  681. // For partial classes, the first entry in the map will contain the combined data
  682. class BfTypeDef
  683. {
  684. public:
  685. enum DefState
  686. {
  687. DefState_New,
  688. DefState_Defined,
  689. DefState_CompositeWithPartials, // Temporary condition
  690. DefState_AwaitingNewVersion,
  691. DefState_Signature_Changed,
  692. DefState_InlinedInternals_Changed, // Code within methods, including inlined methods, changed
  693. DefState_Internals_Changed, // Only code within a non-inlined methods changed
  694. DefState_Deleted
  695. };
  696. public:
  697. BfTypeDef* mNextRevision;
  698. BfSystem* mSystem;
  699. BfProject* mProject;
  700. BfTypeDeclaration* mTypeDeclaration;
  701. BfSource* mSource;
  702. DefState mDefState;
  703. Val128 mSignatureHash; // Data, methods, etc
  704. Val128 mFullHash;
  705. Val128 mInlineHash;
  706. BfTypeDef* mOuterType;
  707. BfAtomComposite mNamespace;
  708. BfAtom* mName;
  709. BfAtom* mNameEx; // Contains extensions like `1 for param counts
  710. BfAtomComposite mFullName;
  711. BfAtomComposite mFullNameEx;
  712. BfProtection mProtection;
  713. Array<BfAtomComposite> mNamespaceSearch;
  714. Array<BfTypeReference*> mStaticSearch;
  715. Array<BfFieldDef*> mFields;
  716. Array<BfPropertyDef*> mProperties;
  717. Array<BfMethodDef*> mMethods;
  718. HashSet<BfMemberSetEntry> mMethodSet;
  719. HashSet<BfMemberSetEntry> mFieldSet;
  720. HashSet<BfMemberSetEntry> mPropertySet;
  721. Array<BfOperatorDef*> mOperators;
  722. BfMethodDef* mDtorDef;
  723. Array<BfGenericParamDef*> mGenericParamDefs;
  724. Array<BfTypeReference*> mBaseTypes;
  725. Array<BfTypeDef*> mNestedTypes;
  726. Array<BfDirectStrTypeReference*> mDirectAllocNodes;
  727. Array<BfTypeDef*> mPartials; // Only valid for mIsCombinedPartial
  728. int mHash;
  729. int mPartialIdx;
  730. int mNestDepth;
  731. int mDupDetectedRevision; // Error state
  732. BfTypeCode mTypeCode;
  733. bool mIsAlwaysInclude;
  734. bool mIsNoDiscard;
  735. bool mIsPartial;
  736. bool mIsExplicitPartial;
  737. bool mPartialUsed;
  738. bool mIsCombinedPartial;
  739. bool mIsDelegate;
  740. bool mIsFunction;
  741. bool mIsClosure;
  742. bool mIsAbstract;
  743. bool mIsConcrete;
  744. bool mIsStatic;
  745. bool mHasAppendCtor;
  746. bool mHasOverrideMethods;
  747. bool mIsOpaque;
  748. bool mIsNextRevision;
  749. public:
  750. BfTypeDef()
  751. {
  752. Init();
  753. }
  754. ~BfTypeDef();
  755. void Init()
  756. {
  757. mName = NULL;
  758. mNameEx = NULL;
  759. mSystem = NULL;
  760. mProject = NULL;
  761. mTypeCode = BfTypeCode_None;
  762. mIsAlwaysInclude = false;
  763. mIsNoDiscard = false;
  764. mIsExplicitPartial = false;
  765. mIsPartial = false;
  766. mIsCombinedPartial = false;
  767. mTypeDeclaration = NULL;
  768. mSource = NULL;
  769. mDefState = DefState_New;
  770. mHash = 0;
  771. mPartialIdx = -1;
  772. mIsAbstract = false;
  773. mIsConcrete = false;
  774. mIsDelegate = false;
  775. mIsFunction = false;
  776. mIsClosure = false;
  777. mIsStatic = false;
  778. mHasAppendCtor = false;
  779. mHasOverrideMethods = false;
  780. mIsOpaque = false;
  781. mPartialUsed = false;
  782. mIsNextRevision = false;
  783. mDupDetectedRevision = -1;
  784. mNestDepth = 0;
  785. mOuterType = NULL;
  786. mTypeDeclaration = NULL;
  787. mDtorDef = NULL;
  788. mNextRevision = NULL;
  789. mProtection = BfProtection_Public;
  790. }
  791. BfSource* GetLastSource();
  792. bool IsGlobalsContainer();
  793. void Reset();
  794. void FreeMembers();
  795. void PopulateMemberSets();
  796. void RemoveGenericParamDef(BfGenericParamDef* genericParamDef);
  797. int GetSelfGenericParamCount();
  798. String ToString();
  799. BfMethodDef* GetMethodByName(const StringImpl& name, int paramCount = -1);
  800. bool HasAutoProperty(BfPropertyDeclaration* propertyDeclaration);
  801. String GetAutoPropertyName(BfPropertyDeclaration* propertyDeclaration);
  802. BfAstNode* GetRefNode();
  803. BfTypeDef* GetLatest()
  804. {
  805. if (mNextRevision != NULL)
  806. return mNextRevision;
  807. return this;
  808. }
  809. void ReportMemory(MemReporter* memReporter);
  810. bool NameEquals(BfTypeDef* otherTypeDef);
  811. bool IsExtension()
  812. {
  813. return mTypeCode == BfTypeCode_Extension;
  814. }
  815. bool HasSource(BfSource* source);
  816. };
  817. struct BfTypeDefMapFuncs : public MultiHashSetFuncs
  818. {
  819. int GetHash(BfTypeDef* typeDef)
  820. {
  821. return GetHash(typeDef->mFullName);
  822. }
  823. int GetHash(const BfAtomComposite& name)
  824. {
  825. int hash = 0;
  826. for (int i = 0; i < name.mSize; i++)
  827. {
  828. auto atom = name.mParts[i];
  829. hash = ((hash ^ atom->mHash) << 5) - hash;
  830. }
  831. return (hash & 0x7FFFFFFF);
  832. }
  833. bool Matches(const BfAtomComposite& name, BfTypeDef* typeDef)
  834. {
  835. return name == typeDef->mFullName;
  836. }
  837. bool Matches(BfTypeDef* keyTypeDef, BfTypeDef* typeDef)
  838. {
  839. return keyTypeDef == typeDef;
  840. }
  841. };
  842. enum BfTargetType
  843. {
  844. BfTargetType_BeefConsoleApplication,
  845. BfTargetType_BeefWindowsApplication,
  846. BfTargetType_BeefLib,
  847. BfTargetType_BeefDynLib,
  848. BfTargetType_CustomBuild,
  849. BfTargetType_C_ConsoleApplication,
  850. BfTargetType_C_WindowsApplication,
  851. BfTargetType_BeefTest
  852. };
  853. enum BfProjectFlags
  854. {
  855. BfProjectFlags_None = 0,
  856. BfProjectFlags_MergeFunctions = 1,
  857. BfProjectFlags_CombineLoads = 2,
  858. BfProjectFlags_VectorizeLoops = 4,
  859. BfProjectFlags_VectorizeSLP = 8,
  860. BfProjectFlags_SingleModule = 0x10,
  861. BfProjectFlags_AsmOutput = 0x20,
  862. BfProjectFlags_AsmOutput_ATT = 0x40,
  863. BfProjectFlags_AlwaysIncludeAll = 0x80,
  864. };
  865. class BfProject
  866. {
  867. public:
  868. BfSystem* mSystem;
  869. String mName;
  870. Array<BfProject*> mDependencies;
  871. BfTargetType mTargetType;
  872. BfCodeGenOptions mCodeGenOptions;
  873. bool mDisabled;
  874. bool mSingleModule;
  875. bool mAlwaysIncludeAll;
  876. int mIdx;
  877. String mStartupObject;
  878. Array<String> mPreprocessorMacros;
  879. Dictionary<BfAtomComposite, int> mNamespaces;
  880. HashSet<BfModule*> mUsedModules;
  881. HashSet<BfType*> mReferencedTypeData;
  882. Val128 mBuildConfigHash;
  883. Val128 mVDataConfigHash;
  884. bool mBuildConfigChanged;
  885. public:
  886. BfProject();
  887. ~BfProject();
  888. bool ContainsReference(BfProject* refProject);
  889. bool ReferencesOrReferencedBy(BfProject* refProject);
  890. bool IsTestProject();
  891. };
  892. //CDH TODO move these out to separate header if list gets big/unwieldy
  893. enum BfWarning
  894. {
  895. BfWarning_CS0108_MemberHidesInherited = 108,
  896. BfWarning_CS0114_MethodHidesInherited = 114,
  897. BfWarning_CS0162_UnreachableCode = 162,
  898. BfWarning_CS0168_VariableDeclaredButNeverUsed = 168,
  899. BfWarning_CS0472_ValueTypeNullCompare = 472,
  900. BfWarning_CS1030_PragmaWarning = 1030,
  901. BfWarning_BF4201_Only7Hex = 4201,
  902. BfWarning_BF4202_TooManyHexForInt = 4202,
  903. BfWarning_BF4203_UnnecessaryDynamicCast = 4203
  904. };
  905. class BfErrorBase
  906. {
  907. public:
  908. bool mIsWarning;
  909. bool mIsDeferred;
  910. BfSourceData* mSource;
  911. int mSrcStart;
  912. int mSrcEnd;
  913. public:
  914. BfErrorBase()
  915. {
  916. mIsWarning = false;
  917. mIsDeferred = false;
  918. mSource = NULL;
  919. mSrcStart = -1;
  920. mSrcEnd = -1;
  921. }
  922. ~BfErrorBase();
  923. void SetSource(BfPassInstance* passInstance, BfSourceData* source);
  924. };
  925. class BfMoreInfo : public BfErrorBase
  926. {
  927. public:
  928. String mInfo;
  929. };
  930. class BfError : public BfErrorBase
  931. {
  932. public:
  933. bool mIsAfter;
  934. bool mIsPersistent;
  935. bool mIsWhileSpecializing;
  936. bool mIgnore;
  937. String mError;
  938. int mWarningNumber;
  939. Array<BfMoreInfo*> mMoreInfo;
  940. public:
  941. BfError()
  942. {
  943. mIsAfter = false;
  944. mIsPersistent = false;
  945. mIsWhileSpecializing = false;
  946. mIgnore = false;
  947. mWarningNumber = 0;
  948. }
  949. ~BfError()
  950. {
  951. for (auto moreInfo : mMoreInfo)
  952. delete moreInfo;
  953. }
  954. int GetSrcStart()
  955. {
  956. return mSrcStart;
  957. }
  958. int GetSrcLength()
  959. {
  960. return mSrcEnd - mSrcStart;
  961. }
  962. int GetSrcEnd()
  963. {
  964. return mSrcEnd;
  965. }
  966. };
  967. class BfSourceClassifier;
  968. class BfAutoComplete;
  969. enum BfFailFlags
  970. {
  971. BfFailFlag_None = 0,
  972. BfFailFlag_ShowSpaceChars = 1
  973. };
  974. struct BfErrorEntry
  975. {
  976. public:
  977. BfErrorBase* mError;
  978. BfErrorEntry(BfErrorBase* error)
  979. {
  980. mError = error;
  981. }
  982. size_t GetHashCode() const;
  983. bool operator==(const BfErrorEntry& other) const;
  984. };
  985. class BfPassInstance
  986. {
  987. public:
  988. const int sMaxDisplayErrors = 100;
  989. const int sMaxErrors = 1000;
  990. BfSystem* mSystem;
  991. bool mTrimMessagesToCursor;
  992. int mFailedIdx;
  993. Dictionary<BfSourceData*, String> mSourceFileNameMap;
  994. HashSet<BfErrorEntry> mErrorSet;
  995. Array<BfError*> mErrors;
  996. int mIgnoreCount;
  997. int mWarningCount;
  998. int mDeferredErrorCount;
  999. Deque<String> mOutStream;
  1000. bool mLastWasDisplayed;
  1001. bool mLastWasAdded;
  1002. uint8 mClassifierPassId;
  1003. BfParser* mFilterErrorsTo;
  1004. bool mHadSignatureChanges;
  1005. public:
  1006. BfPassInstance(BfSystem* bfSystem)
  1007. {
  1008. mTrimMessagesToCursor = false;
  1009. mFailedIdx = 0;
  1010. mSystem = bfSystem;
  1011. mLastWasDisplayed = false;
  1012. mLastWasAdded = false;
  1013. mClassifierPassId = 0;
  1014. mWarningCount = 0;
  1015. mDeferredErrorCount = 0;
  1016. mIgnoreCount = 0;
  1017. mFilterErrorsTo = NULL;
  1018. mHadSignatureChanges = false;
  1019. }
  1020. ~BfPassInstance();
  1021. void ClearErrors();
  1022. bool HasFailed();
  1023. bool HasMessages();
  1024. void OutputLine(const StringImpl& str);
  1025. bool PopOutString(String* outString);
  1026. bool WantsRangeRecorded(BfSourceData* bfSource, int srcIdx, int srcLen, bool isWarning, bool isDeferred = false);
  1027. bool WantsRangeDisplayed(BfSourceData* bfSource, int srcIdx, int srcLen, bool isWarning, bool isDeferred = false);
  1028. void TrimSourceRange(BfSourceData* source, int startIdx, int& srcLen); // Trim to a single line, in cases when we reference a large multi-line node
  1029. bool HasLastFailedAt(BfAstNode* astNode);
  1030. void MessageAt(const StringImpl& msgPrefix, const StringImpl& error, BfSourceData* bfSource, int srcIdx, int srcLen = 1, BfFailFlags flags = BfFailFlag_None);
  1031. void FixSrcStartAndEnd(BfSourceData* source, int& startIdx, int& endIdx);
  1032. BfError* WarnAt(int warningNumber, const StringImpl& warning, BfSourceData* bfSource, int srcIdx, int srcLen = 1);
  1033. BfError* Warn(int warningNumber, const StringImpl& warning);
  1034. BfError* Warn(int warningNumber, const StringImpl& warning, BfAstNode* refNode);
  1035. BfError* WarnAfter(int warningNumber, const StringImpl& warning, BfAstNode* refNode);
  1036. BfError* MoreInfoAt(const StringImpl& info, BfSourceData* bfSource, int srcIdx, int srcLen, BfFailFlags flags = BfFailFlag_None);
  1037. BfError* MoreInfo(const StringImpl& info);
  1038. BfError* MoreInfo(const StringImpl& info, BfAstNode* refNode);
  1039. BfError* MoreInfoAfter(const StringImpl& info, BfAstNode* refNode);
  1040. BfError* FailAt(const StringImpl& error, BfSourceData* bfSource, int srcIdx, int srcLen = 1, BfFailFlags flags = BfFailFlag_None);
  1041. BfError* FailAfterAt(const StringImpl& error, BfSourceData* bfSource, int srcIdx);
  1042. BfError* Fail(const StringImpl& error);
  1043. BfError* Fail(const StringImpl& error, BfAstNode* refNode);
  1044. BfError* FailAfter(const StringImpl& error, BfAstNode* refNode);
  1045. BfError* DeferFail(const StringImpl& error, BfAstNode* refNode);
  1046. void SilentFail();
  1047. void TryFlushDeferredError();
  1048. void WriteErrorSummary();
  1049. };
  1050. enum BfOptionalBool
  1051. {
  1052. BfOptionalBool_NotSet = -1,
  1053. BfOptionalBool_False = 0,
  1054. BfOptionalBool_True = 1
  1055. };
  1056. class BfTypeOptions
  1057. {
  1058. public:
  1059. Array<String> mTypeFilters;
  1060. Array<String> mAttributeFilters;
  1061. Array<int> mMatchedIndices;
  1062. int mSIMDSetting;
  1063. int mOptimizationLevel;
  1064. int mEmitDebugInfo;
  1065. BfOptionalBool mRuntimeChecks;
  1066. BfOptionalBool mInitLocalVariables;
  1067. BfOptionalBool mEmitDynamicCastCheck;
  1068. BfOptionalBool mEmitObjectAccessCheck;
  1069. int mAllocStackTraceDepth;
  1070. public:
  1071. static int Apply(int val, int applyVal)
  1072. {
  1073. if (applyVal != -1)
  1074. return applyVal;
  1075. return val;
  1076. }
  1077. static bool Apply(bool val, BfOptionalBool applyVal)
  1078. {
  1079. if (applyVal != BfOptionalBool_NotSet)
  1080. return applyVal == BfOptionalBool_True;
  1081. return val;
  1082. }
  1083. };
  1084. class BfSystem
  1085. {
  1086. public:
  1087. int mPtrSize;
  1088. bool mIsResolveOnly;
  1089. CritSect mDataLock; // short-lived, hold only while active modifying data
  1090. // The following are protected by mDataLock:
  1091. Array<BfProject*> mProjects;
  1092. Array<BfProject*> mProjectDeleteQueue;
  1093. Array<BfParser*> mParsers;
  1094. Array<BfParser*> mParserDeleteQueue;
  1095. Array<BfTypeDef*> mTypeDefDeleteQueue;
  1096. Array<BfTypeOptions> mTypeOptions;
  1097. Array<BfTypeOptions> mMergedTypeOptions;
  1098. int mUpdateCnt;
  1099. bool mWorkspaceConfigChanged;
  1100. Val128 mWorkspaceConfigHash;
  1101. Array<BfCompiler*> mCompilers;
  1102. BfAtom* mGlobalsAtom;
  1103. BfAtom* mEmptyAtom;
  1104. BfAtom* mBfAtom;
  1105. CritSect mSystemLock; // long-lived, hold while compiling
  1106. int mYieldDisallowCount; // We can only yield lock when we are at 0
  1107. volatile int mCurSystemLockPri;
  1108. BfpThreadId mCurSystemLockThreadId;
  1109. volatile int mPendingSystemLockPri;
  1110. uint32 mYieldTickCount;
  1111. int mHighestYieldTime;
  1112. // The following are protected by mSystemLock - can only be accessed by the compiling thread
  1113. Dictionary<String, BfTypeDef*> mSystemTypeDefs;
  1114. BfTypeDefMap mTypeDefs;
  1115. bool mNeedsTypesHandledByCompiler;
  1116. BumpAllocator mAlloc;
  1117. int mAtomCreateIdx;
  1118. Dictionary<StringView, BfAtom*> mAtomMap;
  1119. Array<BfAtom*> mAtomGraveyard;
  1120. uint32 mAtomUpdateIdx;
  1121. int32 mTypeMapVersion; // Increment when we add any new types or namespaces
  1122. OwnedVector<BfMethodDef> mMethodGraveyard;
  1123. OwnedVector<BfFieldDef> mFieldGraveyard;
  1124. OwnedVector<BfDirectStrTypeReference> mDirectTypeRefs;
  1125. OwnedVector<BfRefTypeRef> mRefTypeRefs;
  1126. public:
  1127. BfTypeDef* mTypeVoid;
  1128. BfTypeDef* mTypeNullPtr;
  1129. BfTypeDef* mTypeSelf;
  1130. BfTypeDef* mTypeDot;
  1131. BfTypeDef* mTypeVar;
  1132. BfTypeDef* mTypeLet;
  1133. BfTypeDef* mTypeBool;
  1134. BfTypeDef* mTypeIntPtr;
  1135. BfTypeDef* mTypeUIntPtr;
  1136. BfTypeDef* mTypeIntUnknown;
  1137. BfTypeDef* mTypeUIntUnknown;
  1138. BfTypeDef* mTypeInt8;
  1139. BfTypeDef* mTypeUInt8;
  1140. BfTypeDef* mTypeInt16;
  1141. BfTypeDef* mTypeUInt16;
  1142. BfTypeDef* mTypeInt32;
  1143. BfTypeDef* mTypeUInt32;
  1144. BfTypeDef* mTypeInt64;
  1145. BfTypeDef* mTypeUInt64;
  1146. BfTypeDef* mTypeChar8;
  1147. BfTypeDef* mTypeChar16;
  1148. BfTypeDef* mTypeChar32;
  1149. BfTypeDef* mTypeSingle;
  1150. BfTypeDef* mTypeDouble;
  1151. BfDirectStrTypeReference* mDirectVoidTypeRef;
  1152. BfDirectStrTypeReference* mDirectBoolTypeRef;
  1153. BfDirectStrTypeReference* mDirectSelfTypeRef;
  1154. BfDirectStrTypeReference* mDirectSelfBaseTypeRef;
  1155. BfRefTypeRef* mDirectRefSelfBaseTypeRef;
  1156. BfDirectStrTypeReference* mDirectObjectTypeRef;
  1157. BfDirectStrTypeReference* mDirectStringTypeRef;
  1158. BfDirectStrTypeReference* mDirectIntTypeRef;
  1159. BfRefTypeRef* mDirectRefIntTypeRef;
  1160. BfDirectStrTypeReference* mDirectInt32TypeRef;
  1161. public:
  1162. BfSystem();
  1163. ~BfSystem();
  1164. BfAtom* GetAtom(const StringImpl& string);
  1165. BfAtom* FindAtom(const StringImpl& string); // Doesn't create a ref
  1166. BfAtom* FindAtom(const StringView& string); // Doesn't create a ref
  1167. void ReleaseAtom(BfAtom* atom);
  1168. void ProcessAtomGraveyard();
  1169. void RefAtomComposite(const BfAtomComposite& atomComposite);
  1170. void ReleaseAtomComposite(const BfAtomComposite& atomComposite);
  1171. void SanityCheckAtomComposite(const BfAtomComposite& atomComposite);
  1172. void TrackName(BfTypeDef* typeDef);
  1173. void UntrackName(BfTypeDef* typeDef);
  1174. bool ParseAtomComposite(const StringView& name, BfAtomComposite& composite, bool addRefs = false);
  1175. void CreateBasicTypes();
  1176. bool DoesLiteralFit(BfTypeCode typeCode, int64 value);
  1177. BfParser* CreateParser(BfProject* bfProject);
  1178. BfCompiler* CreateCompiler(bool isResolveOnly);
  1179. BfProject* GetProject(const StringImpl& projName);
  1180. BfTypeReference* GetTypeRefElement(BfTypeReference* typeRef);
  1181. BfTypeDef* FilterDeletedTypeDef(BfTypeDef* typeDef);
  1182. bool CheckTypeDefReference(BfTypeDef* typeDef, BfProject* project);
  1183. BfTypeDef* FindTypeDef(const BfAtomComposite& findName, int numGenericArgs = 0, BfProject* project = NULL, const Array<BfAtomComposite>& namespaceSearch = Array<BfAtomComposite>(), BfTypeDef** ambiguousTypeDef = NULL);
  1184. bool FindTypeDef(const BfAtomComposite& findName, int numGenericArgs, BfProject* project, const BfAtomComposite& checkNamespace, bool allowPrivate, BfTypeDefLookupContext* ctx);
  1185. BfTypeDef* FindTypeDef(const StringImpl& typeName, int numGenericArgs = 0, BfProject* project = NULL, const Array<BfAtomComposite>& namespaceSearch = Array<BfAtomComposite>(), BfTypeDef** ambiguousTypeDef = NULL);
  1186. BfTypeDef* FindTypeDef(const StringImpl& typeName, BfProject* project);
  1187. BfTypeDef* FindTypeDefEx(const StringImpl& typeName);
  1188. void FindFixitNamespaces(const StringImpl& typeName, int numGenericArgs, BfProject* project, std::set<String>& fixitNamespaces);
  1189. void RemoveTypeDef(BfTypeDef* typeDef);
  1190. //BfTypeDefMap::Iterator RemoveTypeDef(BfTypeDefMap::Iterator typeDefItr);
  1191. void AddNamespaceUsage(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1192. void RemoveNamespaceUsage(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1193. bool ContainsNamespace(const BfAtomComposite& namespaceStr, BfProject* bfProject);
  1194. void InjectNewRevision(BfTypeDef* typeDef);
  1195. void AddToCompositePartial(BfPassInstance* passInstance, BfTypeDef* compositeTypeDef, BfTypeDef* partialTypeDef);
  1196. void FinishCompositePartial(BfTypeDef* compositeTypeDef);
  1197. BfTypeDef* GetCombinedPartial(BfTypeDef* typeDef);
  1198. BfTypeDef* GetOuterTypeNonPartial(BfTypeDef* typeDef);
  1199. int GetGenericParamIdx(const Array<BfGenericParamDef*>& genericParams, const StringImpl& name);
  1200. int GetGenericParamIdx(const Array<BfGenericParamDef*>& genericParams, BfTypeReference* typeRef);
  1201. void StartYieldSection();
  1202. void CheckLockYield(); // Yields to a higher priority request
  1203. void SummarizeYieldSection();
  1204. void NotifyWillRequestLock(int priority);
  1205. void Lock(int priority);
  1206. void Unlock();
  1207. void AssertWeHaveLock();
  1208. void RemoveDeletedParsers();
  1209. void RemoveOldParsers();
  1210. void RemoveOldData();
  1211. void VerifyTypeDef(BfTypeDef* typeDef);
  1212. BfPassInstance* CreatePassInstance();
  1213. BfTypeOptions* GetTypeOptions(int optionsIdx);
  1214. bool HasTestProjects();
  1215. bool IsCompatibleCallingConvention(BfCallingConvention callConvA, BfCallingConvention callConvB);
  1216. };
  1217. class AutoDisallowYield
  1218. {
  1219. public:
  1220. BfSystem* mSystem;
  1221. bool mHeld;
  1222. public:
  1223. AutoDisallowYield(BfSystem* system)
  1224. {
  1225. mSystem = system;
  1226. mSystem->mYieldDisallowCount++;
  1227. mHeld = true;
  1228. }
  1229. ~AutoDisallowYield()
  1230. {
  1231. if (mHeld)
  1232. mSystem->mYieldDisallowCount--;
  1233. }
  1234. void Release()
  1235. {
  1236. BF_ASSERT(mHeld);
  1237. if (mHeld)
  1238. {
  1239. mHeld = false;
  1240. mSystem->mYieldDisallowCount--;
  1241. }
  1242. }
  1243. void Acquire()
  1244. {
  1245. BF_ASSERT(!mHeld);
  1246. if (!mHeld)
  1247. {
  1248. mHeld = true;
  1249. mSystem->mYieldDisallowCount++;
  1250. }
  1251. }
  1252. };
  1253. #ifdef _DEBUG
  1254. #ifdef BF_PLATFORM_WINDOWS
  1255. #define BF_WANTS_LOG
  1256. #define BF_WANTS_LOG_SYS
  1257. //#define BF_WANTS_LOG2
  1258. //#define BF_WANTS_LOG_CLANG
  1259. #define BF_WANTS_LOG_DBG
  1260. #define BF_WANTS_LOG_DBGEXPR
  1261. //#define BF_WANTS_LOG_CV
  1262. #endif
  1263. #else
  1264. //#define BF_WANTS_LOG
  1265. //#define BF_WANTS_LOG2
  1266. //#define BF_WANTS_LOG_CLANG
  1267. //#define BF_WANTS_LOG_DBGEXPR
  1268. //#define BF_WANTS_LOG_CV
  1269. //#define BF_WANTS_LOG_DBG
  1270. #endif
  1271. #ifdef BF_WANTS_LOG
  1272. #define BfLog(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1273. #else
  1274. //#define BfLog(fmt) {} // Nothing
  1275. #define BfLog(fmt, ...) {} // Nothing
  1276. #endif
  1277. #ifdef BF_WANTS_LOG_SYS
  1278. #define BfLogSys(sys, fmt, ...) DoBfLog((sys)->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1279. #define BfLogSysM(fmt, ...) DoBfLog(mSystem->mIsResolveOnly ? 1 : 2, fmt, ##__VA_ARGS__)
  1280. #else
  1281. #define BfLogSys(...) {} // Nothing
  1282. #define BfLogSysM(...) {} // Nothing
  1283. #endif
  1284. #ifdef BF_WANTS_LOG_CLANG
  1285. //#define BfLogClang(fmt) DoBfLog(fmt)
  1286. #define BfLogClang(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1287. #else
  1288. #define BfLogClang(fmt, ...) {} // Nothing
  1289. #endif
  1290. #ifdef BF_WANTS_LOG_DBG
  1291. #define BfLogDbg(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1292. #else
  1293. #define BfLogDbg(fmt, ...) {} // Nothing
  1294. #endif
  1295. #ifdef BF_WANTS_LOG_DBGEXPR
  1296. #define BfLogDbgExpr(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1297. #else
  1298. #define BfLogDbgExpr(fmt, ...) {} // Nothing
  1299. #endif
  1300. #ifdef BF_WANTS_LOG2
  1301. #define BfLog2(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1302. #else
  1303. #define BfLog2(fmt, ...) {} // Nothing
  1304. #endif
  1305. #ifdef BF_WANTS_LOG_CV
  1306. #define BfLogCv(fmt, ...) DoBfLog(0, fmt, ##__VA_ARGS__)
  1307. #else
  1308. #define BfLogCv(fmt, ...) {} // Nothing
  1309. #endif
  1310. void DoBfLog(int fileIdx, const char* fmt ...);
  1311. NS_BF_END
  1312. namespace std
  1313. {
  1314. template <>
  1315. struct hash<Beefy::BfAtomComposite>
  1316. {
  1317. size_t operator()(const Beefy::BfAtomComposite& composite) const
  1318. {
  1319. int curHash = 0;
  1320. for (int i = 0; i < (int)composite.mSize; i++)
  1321. curHash = ((curHash ^ (int)(intptr)composite.mParts[i]->mHash) << 5) - curHash;
  1322. return curHash;
  1323. }
  1324. };
  1325. template <>
  1326. struct hash<Beefy::BfMemberSetEntry>
  1327. {
  1328. size_t operator()(const Beefy::BfMemberSetEntry& entry) const
  1329. {
  1330. return std::hash<Beefy::String>()(entry.mMemberDef->mName);
  1331. }
  1332. };
  1333. }
  1334. namespace std
  1335. {
  1336. template<>
  1337. struct hash<Beefy::BfErrorEntry>
  1338. {
  1339. size_t operator()(const Beefy::BfErrorEntry& val) const
  1340. {
  1341. return val.GetHashCode();
  1342. }
  1343. };
  1344. }