BfSystem.h 36 KB

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