BfSystem.h 36 KB

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