BfModule.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. #pragma once
  2. #pragma warning(push)
  3. #pragma warning(disable:4141)
  4. #pragma warning(disable:4146)
  5. #pragma warning(disable:4291)
  6. #pragma warning(disable:4244)
  7. #pragma warning(disable:4267)
  8. #pragma warning(disable:4624)
  9. #pragma warning(disable:4800)
  10. #pragma warning(disable:4996)
  11. #include "BeefySysLib/Common.h"
  12. #include "BeefySysLib/util/CritSect.h"
  13. #include "BeefySysLib/util/PerfTimer.h"
  14. #include "BeefySysLib/util/Hash.h"
  15. #include "BeefySysLib/util/HashSet.h"
  16. #include "BeefySysLib/util/SizedArray.h"
  17. #include "BfSourceClassifier.h"
  18. #include "BfAst.h"
  19. #include "BfSystem.h"
  20. #include "BfIRBuilder.h"
  21. #include "BfResolvedTypeUtils.h"
  22. #include "BfUtil.h"
  23. #include <unordered_set>
  24. #include <functional>
  25. #pragma warning(pop)
  26. NS_BF_BEGIN
  27. class BfType;
  28. class BfResolvedType;
  29. class BfExprEvaluator;
  30. enum BfPopulateType
  31. {
  32. BfPopulateType_TypeDef,
  33. BfPopulateType_Identity,
  34. BfPopulateType_IdentityNoRemapAlias,
  35. BfPopulateType_Declaration,
  36. BfPopulateType_BaseType,
  37. BfPopulateType_Interfaces,
  38. BfPopulateType_Data,
  39. BfPopulateType_DataAndMethods,
  40. BfPopulateType_Full = BfPopulateType_DataAndMethods ,
  41. BfPopulateType_Full_Force
  42. };
  43. enum BfEvalExprFlags
  44. {
  45. BfEvalExprFlags_None = 0,
  46. BfEvalExprFlags_ExplicitCast = 1,
  47. BfEvalExprFlags_NoCast = 2,
  48. BfEvalExprFlags_NoValueAddr = 4,
  49. BfEvalExprFlags_PropogateNullConditional = 8,
  50. BfEvalExprFlags_IgnoreNullConditional = 0x10,
  51. BfEvalExprFlags_AllowSplat = 0x20,
  52. BfEvalExprFlags_AllowEnumId = 0x40,
  53. BfEvalExprFlags_AllowIntUnknown = 0x80,
  54. BfEvalExprFlags_CreateConditionalScope = 0x100,
  55. BfEvalExprFlags_PendingPropSet = 0x200,
  56. BfEvalExprFlags_AllowParamsExpr = 0x400,
  57. BfEvalExprFlags_AllowRefExpr = 0x800,
  58. BfEvalExprFlags_AllowOutExpr = 0x1000,
  59. BfEvalExprFlags_FieldInitializer = 0x2000,
  60. BfEvalExprFlags_VariableDeclaration = 0x4000
  61. };
  62. enum BfCastFlags
  63. {
  64. BfCastFlags_None = 0,
  65. BfCastFlags_Explicit = 1,
  66. BfCastFlags_Unchecked = 2,
  67. BfCastFlags_Internal = 4,
  68. BfCastFlags_SilentFail = 8,
  69. BfCastFlags_NoBoxDtor = 0x10,
  70. BfCastFlags_NoConversionOperator = 0x20,
  71. BfCastFlags_FromCompiler = 0x40, // Not user specified
  72. BfCastFlags_Force = 0x80,
  73. BfCastFlags_PreferAddr = 0x100,
  74. BfCastFlags_WarnOnBox = 0x200
  75. };
  76. enum BfCastResultFlags
  77. {
  78. BfCastResultFlags_None = 0,
  79. BfCastResultFlags_IsAddr = 1,
  80. BfCastResultFlags_IsTemp = 2
  81. };
  82. enum BfAllocFlags
  83. {
  84. BfAllocFlags_None = 0,
  85. BfAllocFlags_RawArray = 1,
  86. BfAllocFlags_ZeroMemory = 2,
  87. BfAllocFlags_NoDtorCall = 4,
  88. BfAllocFlags_NoDefaultToMalloc = 8
  89. };
  90. enum BfProtectionCheckFlags
  91. {
  92. BfProtectionCheckFlag_None = 0,
  93. BfProtectionCheckFlag_CheckedProtected = 1,
  94. BfProtectionCheckFlag_CheckedPrivate = 2,
  95. BfProtectionCheckFlag_AllowProtected = 4,
  96. BfProtectionCheckFlag_AllowPrivate = 8,
  97. };
  98. enum BfEmbeddedStatementFlags
  99. {
  100. BfEmbeddedStatementFlags_None = 0,
  101. BfEmbeddedStatementFlags_IsConditional = 1,
  102. BfEmbeddedStatementFlags_IsDeferredBlock = 2
  103. };
  104. class BfLocalVariable
  105. {
  106. public:
  107. int64 mUnassignedFieldFlags;
  108. BfType* mResolvedType;
  109. BfIdentifierNode* mNameNode;
  110. String mName;
  111. BfIRValue mAddr;
  112. BfIRValue mConstValue;
  113. BfIRValue mValue;
  114. BfIRMDNode mDbgVarInst;
  115. BfIRValue mDbgDeclareInst;
  116. BfIRBlock mDeclBlock;
  117. int mLocalVarIdx; // Index in mLocals
  118. int mLocalVarId; // Unique Id for identification (does not get reused, unlike mLocalVarIdx)
  119. int mCompositeCount;
  120. int mWrittenToId;
  121. int mReadFromId;
  122. int mParamIdx;
  123. bool mIsThis;
  124. bool mHasLocalStructBacking;
  125. bool mIsStruct;
  126. bool mIsImplicitParam;
  127. bool mParamFailed;
  128. bool mIsAssigned;
  129. bool mIsReadOnly;
  130. bool mIsSplat;
  131. bool mIsLowered;
  132. bool mAllowAddr;
  133. bool mIsShadow;
  134. bool mUsedImplicitly; // Passed implicitly to a local method, capture by ref if we can
  135. BfLocalVariable* mShadowedLocal;
  136. public:
  137. BfLocalVariable()
  138. {
  139. mUnassignedFieldFlags = 0;
  140. mResolvedType = NULL;
  141. mNameNode = NULL;
  142. mLocalVarIdx = -1;
  143. mLocalVarId = -1;
  144. mCompositeCount = -1;
  145. mParamIdx = -2;
  146. mIsThis = false;
  147. mHasLocalStructBacking = false;
  148. mIsStruct = false;
  149. mIsImplicitParam = false;
  150. mParamFailed = false;
  151. mIsAssigned = false;
  152. mWrittenToId = -1;
  153. mReadFromId = -1;
  154. mIsReadOnly = false;
  155. mIsSplat = false;
  156. mIsLowered = false;
  157. mAllowAddr = false;
  158. mIsShadow = false;
  159. mUsedImplicitly = false;
  160. mShadowedLocal = NULL;
  161. }
  162. bool IsParam()
  163. {
  164. return mParamIdx != -2;
  165. }
  166. void Init();
  167. };
  168. class BfMethodState;
  169. class BfMixinState;
  170. class BfClosureState;
  171. class BfLocalMethod
  172. {
  173. public:
  174. BfSystem* mSystem;
  175. BfModule* mModule;
  176. BfSource* mSource;
  177. BfMethodDeclaration* mMethodDeclaration;
  178. String mExpectedFullName;
  179. String mMethodName;
  180. BfMethodDef* mMethodDef;
  181. BfLocalMethod* mOuterLocalMethod;
  182. BfMethodInstanceGroup* mMethodInstanceGroup;
  183. BfMethodInstance* mLambdaInvokeMethodInstance;
  184. BfLambdaBindExpression* mLambdaBindExpr;
  185. BfMethodState* mDeclMethodState;
  186. BfIRMDNode mDeclDIScope;
  187. BfMixinState* mDeclMixinState;
  188. OwnedVector<BfDirectTypeReference> mDirectTypeRefs;
  189. bool mDeclOnly;
  190. BfLocalMethod* mNextWithSameName;
  191. public:
  192. BfLocalMethod()
  193. {
  194. mModule = NULL;
  195. mSystem = NULL;
  196. mSource = NULL;
  197. mMethodDeclaration = NULL;
  198. mMethodDef = NULL;
  199. mOuterLocalMethod = NULL;
  200. mMethodInstanceGroup = NULL;
  201. mLambdaInvokeMethodInstance = NULL;
  202. mLambdaBindExpr = NULL;
  203. mDeclMethodState = NULL;
  204. mDeclMixinState = NULL;
  205. mDeclOnly = false;
  206. mNextWithSameName = NULL;
  207. }
  208. ~BfLocalMethod();
  209. };
  210. class BfDeferredCapture
  211. {
  212. public:
  213. String mName;
  214. BfTypedValue mValue;
  215. };
  216. class BfDeferredCallEntry
  217. {
  218. public:
  219. BfDeferredCallEntry* mNext;
  220. BfAstNode* mSrcNode;
  221. BfTypedValue mTarget;
  222. BfModuleMethodInstance mModuleMethodInstance;
  223. BfIRValue mDeferredAlloca;
  224. SizedArray<BfIRValue, 1> mScopeArgs;
  225. Array<BfDeferredCapture> mCaptures;
  226. BfBlock* mDeferredBlock;
  227. int mHandlerCount;
  228. bool mBypassVirtual;
  229. bool mDoNullCheck;
  230. bool mCastThis;
  231. bool mArgsNeedLoad;
  232. bool mIgnored;
  233. SLIList<BfDeferredCallEntry*> mDynList;
  234. BfIRValue mDynCallTail;
  235. public:
  236. BfDeferredCallEntry()
  237. {
  238. mBypassVirtual = false;
  239. mDoNullCheck = false;
  240. mNext = NULL;
  241. mSrcNode = NULL;
  242. mDeferredBlock = NULL;
  243. mHandlerCount = 0;
  244. mArgsNeedLoad = false;
  245. mCastThis = false;
  246. mIgnored = false;
  247. }
  248. ~BfDeferredCallEntry()
  249. {
  250. mDynList.DeleteAll();
  251. }
  252. bool IsDynList()
  253. {
  254. return (bool)mDynCallTail;
  255. }
  256. };
  257. struct BfDeferredHandler
  258. {
  259. BfIRBlock mHandlerBlock;
  260. BfIRBlock mDoneBlock;
  261. };
  262. // "Looped" means this scope will execute zero to many times, "Conditional" means zero or one.
  263. // Looped and Conditional are mutually exclusive. "Dyn" means Looped OR Conditional.
  264. class BfScopeData
  265. {
  266. public:
  267. BfScopeData* mPrevScope;
  268. BfIRMDNode mDIScope;
  269. BfIRMDNode mDIInlinedAt;
  270. String mLabel;
  271. BfAstNode* mLabelNode;
  272. int mLocalVarStart;
  273. int mScopeDepth;
  274. int mMixinDepth;
  275. bool mIsScopeHead; // For first scope data or for inlined start
  276. bool mIsLoop;
  277. bool mIsConditional; // Rarely set - usually we rely on OuterIsConditional or InnerIsConditional
  278. bool mOuterIsConditional;
  279. bool mInnerIsConditional;
  280. bool mHadOuterDynStack;
  281. bool mAllowTargeting;
  282. bool mHadScopeValueRetain;
  283. bool mIsDeferredBlock;
  284. BfBlock* mAstBlock;
  285. BfAstNode* mCloseNode;
  286. BfExprEvaluator* mExprEvaluator;
  287. SLIList<BfDeferredCallEntry*> mDeferredCallEntries;
  288. BfIRValue mBlock;
  289. BfIRValue mValueScopeStart;
  290. BfIRValue mSavedStack;
  291. Array<BfIRValue> mSavedStackUses;
  292. Array<BfDeferredHandler> mDeferredHandlers; // These get cleared when us our a parent gets new entries added into mDeferredCallEntries
  293. Array<BfIRBlock> mAtEndBlocks; // Move these to the end after we close scope
  294. Array<BfIRValue> mDeferredLifetimeEnds;
  295. BfIRMDNode mAltDIFile;
  296. BfIRMDNode mAltDIScope;
  297. public:
  298. BfScopeData()
  299. {
  300. mPrevScope = NULL;
  301. mLocalVarStart = 0;
  302. mLabelNode = NULL;
  303. mAstBlock = NULL;
  304. mCloseNode = NULL;
  305. mExprEvaluator = NULL;
  306. mIsScopeHead = false;
  307. mIsLoop = false;
  308. mIsConditional = false;
  309. mOuterIsConditional = false;
  310. mInnerIsConditional = false;
  311. mHadOuterDynStack = false;
  312. mHadScopeValueRetain = false;
  313. mIsDeferredBlock = false;
  314. mAllowTargeting = true;
  315. mMixinDepth = 0;
  316. mScopeDepth = 0;
  317. }
  318. ~BfScopeData()
  319. {
  320. mDeferredCallEntries.DeleteAll();
  321. }
  322. BfScopeData* GetHead()
  323. {
  324. auto checkScope = this;
  325. while (!checkScope->mIsScopeHead)
  326. checkScope = checkScope->mPrevScope;
  327. return checkScope;
  328. }
  329. BfScopeData* GetTargetable()
  330. {
  331. if (!mAllowTargeting)
  332. return mPrevScope->GetTargetable();
  333. return this;
  334. }
  335. bool IsLooped(BfScopeData* scopeData)
  336. {
  337. auto checkScope = this;
  338. while (checkScope != NULL)
  339. {
  340. if (checkScope->mIsLoop)
  341. return true;
  342. if (checkScope == scopeData)
  343. break;
  344. checkScope = checkScope->mPrevScope;
  345. }
  346. return false;
  347. }
  348. bool IsDyn(BfScopeData* scopeData)
  349. {
  350. auto checkScope = this;
  351. // Scoping to a loop is dynamic - it doesn't have to cross the loop boundary.
  352. // Scoping to a loop _body_ is not necessarily dynamic, however.
  353. while (checkScope != NULL)
  354. {
  355. if (checkScope->mIsConditional)
  356. return true;
  357. if ((checkScope->mIsLoop) || (checkScope->mInnerIsConditional))
  358. return true;
  359. if (checkScope == scopeData)
  360. break;
  361. if (checkScope->mOuterIsConditional)
  362. return true;
  363. checkScope = checkScope->mPrevScope;
  364. }
  365. return false;
  366. }
  367. bool CrossesMixin(BfScopeData* scopeData)
  368. {
  369. // Check for a transition for having an inlinedAt to not having one
  370. if (!mDIInlinedAt)
  371. return false;
  372. auto checkScope = this;
  373. while (checkScope != scopeData)
  374. {
  375. checkScope = checkScope->mPrevScope;
  376. if (!checkScope->mDIInlinedAt)
  377. return true;
  378. }
  379. return false;
  380. }
  381. void ClearHandlers(BfScopeData* scopeData)
  382. {
  383. auto checkScope = this;
  384. while (true)
  385. {
  386. checkScope->mDeferredHandlers.Clear();
  387. if (checkScope == scopeData)
  388. break;
  389. checkScope = checkScope->mPrevScope;
  390. }
  391. }
  392. int GetDepth()
  393. {
  394. int depth = 0;
  395. auto checkScopeData = this;
  396. while (true)
  397. {
  398. checkScopeData = checkScopeData->mPrevScope;
  399. if (checkScopeData == NULL)
  400. break;
  401. depth++;
  402. }
  403. return depth;
  404. }
  405. };
  406. class BfAllocTarget
  407. {
  408. public:
  409. BfScopeData* mScopeData;
  410. BfAstNode* mRefNode;
  411. BfTypedValue mCustomAllocator;
  412. BfScopedInvocationTarget* mScopedInvocationTarget;
  413. public:
  414. BfAllocTarget()
  415. {
  416. mScopeData = NULL;
  417. mRefNode = NULL;
  418. mCustomAllocator = NULL;
  419. mScopedInvocationTarget = NULL;
  420. }
  421. BfAllocTarget(BfScopeData* scopeData)
  422. {
  423. mScopeData = scopeData;
  424. mRefNode = NULL;
  425. mCustomAllocator = NULL;
  426. mScopedInvocationTarget = NULL;
  427. }
  428. BfAllocTarget(const BfTypedValue& customAllocator, BfAstNode* refNode)
  429. {
  430. mScopeData = NULL;
  431. mCustomAllocator = customAllocator;
  432. mRefNode = NULL;
  433. mScopedInvocationTarget = NULL;
  434. }
  435. };
  436. class BfBreakData
  437. {
  438. public:
  439. BfBreakData* mPrevBreakData;
  440. BfScopeData* mScope;
  441. BfIRBlock mIRContinueBlock;
  442. BfIRBlock mIRBreakBlock;
  443. BfIRBlock mIRFallthroughBlock;
  444. BfIRValue mInnerValueScopeStart;
  445. bool mHadBreak;
  446. public:
  447. BfBreakData()
  448. {
  449. mPrevBreakData = NULL;
  450. mScope = NULL;
  451. mHadBreak = false;
  452. }
  453. };
  454. struct BfAssignedLocal
  455. {
  456. BfLocalVariable* mLocalVar;
  457. int mLocalVarField;
  458. bool operator==(const BfAssignedLocal& second) const
  459. {
  460. return (mLocalVar == second.mLocalVar) && (mLocalVarField == second.mLocalVarField);
  461. }
  462. };
  463. // We use this structure in the case where we have multiple execution paths, then we merge the assigned variables together
  464. // So when we have "if (check) { a = 1; } else {a = 2; }" we can know that a IS definitely assigned afterwards
  465. class BfDeferredLocalAssignData
  466. {
  467. public:
  468. BfScopeData* mScopeData;
  469. int mVarIdBarrier;
  470. SizedArray<BfAssignedLocal, 4> mAssignedLocals;
  471. bool mIsChained;
  472. BfDeferredLocalAssignData* mChainedAssignData;
  473. bool mHadFallthrough;
  474. bool mHadReturn;
  475. bool mIsUnconditional;
  476. bool mIsIfCondition;
  477. bool mIfMayBeSkipped;
  478. public:
  479. BfDeferredLocalAssignData(BfScopeData* scopeData = NULL)
  480. {
  481. mScopeData = scopeData;
  482. mVarIdBarrier = -1;
  483. mHadFallthrough = false;
  484. mHadReturn = false;
  485. mChainedAssignData = NULL;
  486. mIsChained = false;
  487. mIsUnconditional = false;
  488. mIsIfCondition = false;
  489. mIfMayBeSkipped = false;
  490. }
  491. void ExtendFrom(BfDeferredLocalAssignData* outerLocalAssignData, bool doChain = false);
  492. void BreakExtendChain();
  493. void SetIntersection(const BfDeferredLocalAssignData& otherLocalAssignData);
  494. void Validate() const;
  495. void SetUnion(const BfDeferredLocalAssignData& otherLocalAssignData);
  496. };
  497. class BfMixinRecord
  498. {
  499. public:
  500. BfAstNode * mSource;
  501. };
  502. class BfDeferredLocalMethod
  503. {
  504. public:
  505. BfLocalMethod* mLocalMethod;
  506. BfMethodInstance* mMethodInstance;
  507. Array<BfLocalMethod*> mLocalMethods; // Local methods that were in scope at the time
  508. Array<BfLocalVariable> mConstLocals;
  509. Array<BfMixinRecord> mMixinStateRecords;
  510. };
  511. class BfClosureState
  512. {
  513. public:
  514. bool mCapturing;
  515. bool mCaptureVisitingBody;
  516. int mCaptureStartAccessId;
  517. // When we need to look into another local method to determine captures, but we don't want to process local variable declarations or cause infinite recursion
  518. bool mBlindCapturing;
  519. bool mDeclaringMethodIsMutating;
  520. BfLocalMethod* mLocalMethod;
  521. BfClosureInstanceInfo* mClosureInstanceInfo;
  522. BfMethodDef* mClosureMethodDef;
  523. BfType* mReturnType;
  524. BfTypeInstance* mClosureType;
  525. BfDeferredLocalMethod* mActiveDeferredLocalMethod;
  526. Array<BfLocalVariable> mConstLocals; // Locals not inserted into the captured 'this'
  527. HashSet<BfFieldInstance*> mReferencedOuterClosureMembers;
  528. HashSet<BfMethodInstance*> mLocalMethodRefSet;
  529. Array<BfMethodInstance*> mLocalMethodRefs;
  530. Array<BfMethodInstance*> mDeferredProcessLocalMethods;
  531. public:
  532. BfClosureState()
  533. {
  534. mClosureMethodDef = NULL;
  535. mLocalMethod = NULL;
  536. mClosureInstanceInfo = NULL;
  537. mCapturing = false;
  538. mCaptureVisitingBody = false;
  539. mCaptureStartAccessId = -1;
  540. mBlindCapturing = false;
  541. mDeclaringMethodIsMutating = false;
  542. mActiveDeferredLocalMethod = NULL;
  543. mReturnType = NULL;
  544. mClosureType = NULL;
  545. }
  546. };
  547. class BfIteratorClassState
  548. {
  549. public:
  550. BfTypeInstance* mIteratorClass;
  551. bool mCapturing;
  552. public:
  553. BfIteratorClassState()
  554. {
  555. mCapturing = false;
  556. }
  557. };
  558. class BfPendingNullConditional
  559. {
  560. public:
  561. BfIRBlock mPrevBB;
  562. BfIRBlock mCheckBB;
  563. BfIRBlock mDoneBB;
  564. };
  565. class BfAttributeState
  566. {
  567. public:
  568. BfAttributeTargets mTarget;
  569. BfCustomAttributes* mCustomAttributes;
  570. bool mUsed;
  571. BfAttributeState()
  572. {
  573. mTarget = BfAttributeTargets_None;
  574. mCustomAttributes = NULL;
  575. mUsed = false;
  576. }
  577. ~BfAttributeState()
  578. {
  579. if (mCustomAttributes != NULL)
  580. delete mCustomAttributes;
  581. }
  582. };
  583. class BfMixinState
  584. {
  585. public:
  586. BfMixinState* mPrevMixinState;
  587. BfAstNode* mSource;
  588. BfScopeData* mCallerScope;
  589. BfScopeData* mTargetScope; // Equals caller scope unless user explicitly calls specifies scope override
  590. BfMethodInstance* mMixinMethodInstance;
  591. BfAstNode* mResultExpr;
  592. int mLocalsStartIdx;
  593. bool mUsedInvocationScope;
  594. bool mHasDeferredUsage;
  595. BfTypedValue mTarget;
  596. int mLastTargetAccessId;
  597. public:
  598. BfMixinState()
  599. {
  600. mLastTargetAccessId = -1;
  601. }
  602. BfMixinState* GetRoot()
  603. {
  604. auto curMixin = this;
  605. while (curMixin->mPrevMixinState != NULL)
  606. curMixin = curMixin->mPrevMixinState;
  607. return curMixin;
  608. }
  609. };
  610. class BfDeferredCallEmitState
  611. {
  612. public:
  613. BfAstNode* mCloseNode;
  614. public:
  615. BfDeferredCallEmitState()
  616. {
  617. mCloseNode = NULL;
  618. }
  619. };
  620. class BfTypeLookupError
  621. {
  622. public:
  623. enum BfErrorKind
  624. {
  625. BfErrorKind_None,
  626. BfErrorKind_Ambiguous,
  627. BfErrorKind_Inaccessible
  628. };
  629. public:
  630. BfErrorKind mErrorKind;
  631. BfAstNode* mRefNode;
  632. BfTypeDef* mAmbiguousTypeDef;
  633. public:
  634. BfTypeLookupError()
  635. {
  636. mErrorKind = BfErrorKind_None;
  637. mRefNode = NULL;
  638. mAmbiguousTypeDef = NULL;
  639. }
  640. };
  641. /*struct BfSplatDecompHash
  642. {
  643. size_t operator()(const std::pair<BfIRValue, int>& val) const
  644. {
  645. return (val.first.mId << 4) + val.second;
  646. }
  647. };
  648. struct BfSplatDecompEquals
  649. {
  650. bool operator()(const std::pair<BfIRValue, int>& lhs, const std::pair<BfIRValue, int>& rhs) const
  651. {
  652. return (lhs.first.mFlags == rhs.first.mFlags) && (lhs.first.mId == rhs.first.mId) && (lhs.second == rhs.second);
  653. }
  654. };*/
  655. struct BfMethodRefHash
  656. {
  657. size_t operator()(const BfMethodRef& val) const
  658. {
  659. if (val.mTypeInstance == NULL)
  660. return 0;
  661. return val.mTypeInstance->mTypeId ^ (val.mMethodNum << 10);
  662. }
  663. };
  664. class BfConstResolveState
  665. {
  666. public:
  667. BfMethodInstance* mMethodInstance;
  668. BfConstResolveState* mPrevConstResolveState;
  669. BfConstResolveState()
  670. {
  671. mMethodInstance = NULL;
  672. mPrevConstResolveState = NULL;
  673. }
  674. };
  675. struct BfLocalVarEntry
  676. {
  677. BfLocalVariable* mLocalVar;
  678. BfLocalVarEntry(BfLocalVariable* localVar)
  679. {
  680. mLocalVar = localVar;
  681. }
  682. bool operator==(const BfLocalVarEntry& other) const
  683. {
  684. return mLocalVar->mName == other.mLocalVar->mName;
  685. }
  686. bool operator==(const StringImpl& name) const
  687. {
  688. return mLocalVar->mName == name;
  689. }
  690. };
  691. class BfLambdaCaptureInfo
  692. {
  693. public:
  694. String mName;
  695. };
  696. class BfLambdaInstance
  697. {
  698. public:
  699. BfTypeInstance* mDelegateTypeInstance;
  700. BfTypeInstance* mUseTypeInstance;
  701. BfClosureType* mClosureTypeInstance;
  702. BfMixinState* mDeclMixinState;
  703. BfTypeInstance* mOuterClosure;
  704. BfIRValue mClosureFunc;
  705. BfIRValue mDtorFunc;
  706. bool mCopyOuterCaptures;
  707. bool mIsStatic;
  708. Array<BfLambdaCaptureInfo> mCaptures;
  709. BfMethodInstance* mMethodInstance;
  710. BfMethodInstance* mDtorMethodInstance;
  711. Array<BfLocalVariable> mConstLocals;
  712. OwnedVector<BfParameterDeclaration> mParamDecls;
  713. public:
  714. BfLambdaInstance()
  715. {
  716. mDelegateTypeInstance = NULL;
  717. mUseTypeInstance = NULL;
  718. mClosureTypeInstance = NULL;
  719. mDeclMixinState = NULL;
  720. mOuterClosure = NULL;
  721. mCopyOuterCaptures = false;
  722. mIsStatic = false;
  723. mMethodInstance = NULL;
  724. mDtorMethodInstance = NULL;
  725. }
  726. ~BfLambdaInstance()
  727. {
  728. delete mMethodInstance->mMethodDef;
  729. delete mMethodInstance;
  730. if (mDtorMethodInstance != NULL)
  731. {
  732. delete mDtorMethodInstance->mMethodDef;
  733. delete mDtorMethodInstance;
  734. }
  735. }
  736. };
  737. class BfParentNodeEntry
  738. {
  739. public:
  740. BfAstNode* mNode;
  741. BfParentNodeEntry* mPrev;
  742. };
  743. class BfMethodState
  744. {
  745. public:
  746. enum TempKind
  747. {
  748. TempKind_None,
  749. TempKind_Static,
  750. TempKind_NonStatic
  751. };
  752. public:
  753. BfMethodState* mPrevMethodState; // Only non-null for things like local methods
  754. BfConstResolveState* mConstResolveState;
  755. BfMethodInstance* mMethodInstance;
  756. BfHotDataReferenceBuilder* mHotDataReferenceBuilder;
  757. BfIRFunction mIRFunction;
  758. BfIRBlock mIRHeadBlock;
  759. BfIRBlock mIRInitBlock;
  760. BfIRBlock mIREntryBlock;
  761. Array<BfLocalVariable*> mLocals;
  762. HashSet<BfLocalVarEntry> mLocalVarSet;
  763. Array<BfLocalMethod*> mLocalMethods;
  764. Dictionary<String, BfLocalMethod*> mLocalMethodMap;
  765. Dictionary<String, BfLocalMethod*> mLocalMethodCache; // So any lambda 'capturing' and 'processing' stages use the same local method
  766. Array<BfDeferredLocalMethod*> mDeferredLocalMethods;
  767. OwnedVector<BfMixinState> mMixinStates;
  768. Dictionary<BfAstNode*, BfLambdaInstance*> mLambdaCache;
  769. Array<BfLambdaInstance*> mDeferredLambdaInstances;
  770. Array<BfIRValue> mSplatDecompAddrs;
  771. BfDeferredLocalAssignData* mDeferredLocalAssignData;
  772. int mDeferredLoopListCount;
  773. int mDeferredLoopListEntryCount;
  774. HashSet<int> mSkipObjectAccessChecks; // Indexed by BfIRValue value id
  775. Dictionary<int64, BfType*>* mGenericTypeBindings;
  776. BfIRMDNode mDIFile;
  777. bool mInHeadScope; // Is in starting scope of code on entry, controls mStackAllocUncondCount
  778. BfTypedValue mRetVal;
  779. BfIRValue mRetValAddr;
  780. int mCurAppendAlign;
  781. BfIRValue mDynStackRevIdx; // Increments when we restore the stack, which can invalidate dynSize for dynamic looped allocs
  782. BfIRBlock mIRExitBlock;
  783. BfBreakData* mBreakData;
  784. int mBlockNestLevel; // 0 = top level
  785. bool mIsEmbedded; // Is an embedded statement (ie: if () stmt) not wrapped in a block
  786. bool mIgnoreObjectAccessCheck;
  787. BfMixinState* mMixinState;
  788. BfClosureState* mClosureState;
  789. BfDeferredCallEmitState* mDeferredCallEmitState;
  790. BfIteratorClassState* mIteratorClassState;
  791. BfPendingNullConditional* mPendingNullConditional;
  792. BfTypeOptions* mMethodTypeOptions; // for [Options] attribute
  793. BfIRMDNode mDIRetVal;
  794. BfScopeData mHeadScope;
  795. BfScopeData* mCurScope;
  796. BfScopeData* mTailScope; // Usually equals mCurScope
  797. TempKind mTempKind; // Used for var inference, etc
  798. bool mInDeferredBlock;
  799. bool mHadReturn;
  800. bool mHadContinue;
  801. bool mMayNeedThisAccessCheck;
  802. bool mLeftBlockUncond; // Definitely left block. mHadReturn also sets mLeftBlock
  803. bool mLeftBlockCond; // May have left block.
  804. bool mInPostReturn; // Unreachable code
  805. bool mCrossingMixin; // ie: emitting dtors in response to a return in a mixin
  806. bool mNoBind;
  807. bool mInConditionalBlock; // IE: RHS of ((A) && (B)), indicates an allocation in 'B' won't be dominated by a dtor, for example
  808. bool mAllowUinitReads;
  809. bool mCancelledDeferredCall;
  810. bool mNoObjectAccessChecks;
  811. int mCurLocalVarId;
  812. int mCurAccessId; // For checking to see if a block reads from or writes to a local
  813. public:
  814. BfMethodState()
  815. {
  816. mMethodInstance = NULL;
  817. mPrevMethodState = NULL;
  818. mConstResolveState = NULL;
  819. mHotDataReferenceBuilder = NULL;
  820. mHeadScope.mIsScopeHead = true;
  821. mCurScope = &mHeadScope;
  822. mTailScope = &mHeadScope;
  823. mHadReturn = false;
  824. mLeftBlockUncond = false;
  825. mLeftBlockCond = false;
  826. mHadContinue = false;
  827. mMayNeedThisAccessCheck = false;
  828. mTempKind = TempKind_None;
  829. mInHeadScope = true;
  830. mBreakData = NULL;
  831. mBlockNestLevel = 0;
  832. mInPostReturn = false;
  833. mCrossingMixin = false;
  834. mNoBind = false;
  835. mIsEmbedded = false;
  836. mIgnoreObjectAccessCheck = false;
  837. mInConditionalBlock = false;
  838. mAllowUinitReads = false;
  839. mCancelledDeferredCall = false;
  840. mNoObjectAccessChecks = false;
  841. mInDeferredBlock = false;
  842. mDeferredLocalAssignData = NULL;
  843. mCurLocalVarId = 0;
  844. mCurAccessId = 1;
  845. mCurAppendAlign = 0;
  846. mDeferredLoopListCount = 0;
  847. mDeferredLoopListEntryCount = 0;
  848. mClosureState = NULL;
  849. mDeferredCallEmitState = NULL;
  850. mIteratorClassState = NULL;
  851. mGenericTypeBindings = NULL;
  852. mMixinState = NULL;
  853. mPendingNullConditional = NULL;
  854. mMethodTypeOptions = NULL;
  855. }
  856. ~BfMethodState();
  857. void AddScope(BfScopeData* newScopeData)
  858. {
  859. BF_ASSERT(newScopeData != mCurScope);
  860. mInHeadScope = false;
  861. newScopeData->mDIScope = mCurScope->mDIScope;
  862. newScopeData->mDIInlinedAt = mCurScope->mDIInlinedAt;
  863. newScopeData->mLocalVarStart = mCurScope->mLocalVarStart;
  864. newScopeData->mExprEvaluator = mCurScope->mExprEvaluator;
  865. newScopeData->mAltDIFile = mCurScope->mAltDIFile;
  866. newScopeData->mPrevScope = mCurScope;
  867. newScopeData->mMixinDepth = mCurScope->mMixinDepth;
  868. newScopeData->mScopeDepth = mCurScope->mScopeDepth + 1;
  869. mCurScope = newScopeData;
  870. mTailScope = mCurScope;
  871. }
  872. void SetHadReturn(bool hadReturn)
  873. {
  874. mHadReturn = hadReturn;
  875. if (mDeferredLocalAssignData != NULL)
  876. mDeferredLocalAssignData->mHadReturn = hadReturn;
  877. }
  878. BfMethodState* GetRootMethodState()
  879. {
  880. auto checkMethodState = this;
  881. while (checkMethodState->mPrevMethodState != NULL)
  882. checkMethodState = checkMethodState->mPrevMethodState;
  883. return checkMethodState;
  884. }
  885. BfMethodState* GetNonCaptureState()
  886. {
  887. auto checkMethodState = this;
  888. while ((checkMethodState->mPrevMethodState != NULL) && (checkMethodState->mClosureState != NULL) &&
  889. (checkMethodState->mClosureState->mCapturing) && (checkMethodState->mClosureState->mLocalMethod != NULL))
  890. checkMethodState = checkMethodState->mPrevMethodState;
  891. return checkMethodState;
  892. }
  893. BfMethodState* GetMethodStateForLocal(BfLocalVariable* localVar);
  894. bool InMainMixinScope()
  895. {
  896. if (mMixinState == NULL)
  897. return false;
  898. return mMixinState->mCallerScope == mCurScope->mPrevScope;
  899. }
  900. BfMixinState* GetRootMixinState()
  901. {
  902. BfMixinState* mixinState = mMixinState;
  903. while ((mixinState != NULL) && (mixinState->mPrevMixinState != NULL))
  904. {
  905. mixinState = mixinState->mPrevMixinState;
  906. }
  907. return mixinState;
  908. }
  909. BfAstNode* GetRootMixinSource()
  910. {
  911. BfMixinState* mixinState = NULL;
  912. auto checkMethodState = this;
  913. while (checkMethodState != NULL)
  914. {
  915. if (checkMethodState->mMixinState != NULL)
  916. mixinState = checkMethodState->mMixinState;
  917. if (checkMethodState->mClosureState != NULL)
  918. {
  919. auto activeLocalMethod = checkMethodState->mClosureState->mActiveDeferredLocalMethod;
  920. if (activeLocalMethod != NULL)
  921. {
  922. if (!activeLocalMethod->mMixinStateRecords.IsEmpty())
  923. return activeLocalMethod->mMixinStateRecords.back().mSource;
  924. }
  925. }
  926. checkMethodState = checkMethodState->mPrevMethodState;
  927. }
  928. if (mixinState != NULL)
  929. return mixinState->GetRoot()->mSource;
  930. return NULL;
  931. }
  932. bool HasMixin()
  933. {
  934. auto checkMethodState = this;
  935. while (checkMethodState != NULL)
  936. {
  937. if (checkMethodState->mMixinState != NULL)
  938. return true;
  939. checkMethodState = checkMethodState->mPrevMethodState;
  940. }
  941. return false;
  942. }
  943. bool HasNonStaticMixin()
  944. {
  945. auto checkMethodState = this;
  946. while (checkMethodState != NULL)
  947. {
  948. if ((checkMethodState->mMixinState != NULL) && (!checkMethodState->mMixinState->mMixinMethodInstance->mMethodDef->mIsStatic))
  949. return true;
  950. checkMethodState = checkMethodState->mPrevMethodState;
  951. }
  952. return false;
  953. }
  954. void LocalDefined(BfLocalVariable* localVar, int fieldIdx = -1);
  955. void ApplyDeferredLocalAssignData(const BfDeferredLocalAssignData& deferredLocalAssignData);
  956. void Reset();
  957. int GetLocalStartIdx()
  958. {
  959. if (mMixinState != NULL)
  960. return mMixinState->mLocalsStartIdx;
  961. return 0;
  962. }
  963. };
  964. class BfDeferredMethodCallData;
  965. enum BfValueFlags
  966. {
  967. BfValueFlags_None = 0,
  968. BfValueFlags_Boxed = 1,
  969. };
  970. enum BfBuiltInFuncType
  971. {
  972. BfBuiltInFuncType_PrintF,
  973. BfBuiltInFuncType_Malloc,
  974. BfBuiltInFuncType_Free,
  975. BfBuiltInFuncType_LoadSharedLibraries,
  976. BfBuiltInFuncType_Count
  977. };
  978. // These are the options that can be applied to individual methods that cause AltModules
  979. // to be build, since they are exclusive to an LLVMModule; LLVM optimization-related
  980. // options always apply to entire LLVM modules
  981. struct BfModuleOptions
  982. {
  983. public:
  984. BfSIMDSetting mSIMDSetting;
  985. int mEmitDebugInfo;
  986. BfOptLevel mOptLevel;
  987. bool operator==(const BfModuleOptions& other)
  988. {
  989. return (mSIMDSetting == other.mSIMDSetting) &&
  990. (mEmitDebugInfo == other.mEmitDebugInfo) &&
  991. (mOptLevel == other.mOptLevel);
  992. }
  993. bool operator!=(const BfModuleOptions& other)
  994. {
  995. return !(*this == other);
  996. }
  997. };
  998. struct BfGenericParamSource
  999. {
  1000. public:
  1001. BfTypeInstance* mTypeInstance;
  1002. BfMethodInstance* mMethodInstance;
  1003. bool mCheckAccessibility;
  1004. public:
  1005. BfGenericParamSource()
  1006. {
  1007. mTypeInstance = NULL;
  1008. mMethodInstance = NULL;
  1009. mCheckAccessibility = true;
  1010. }
  1011. BfGenericParamSource(BfTypeInstance* typeInstance)
  1012. {
  1013. mTypeInstance = typeInstance;
  1014. mMethodInstance = NULL;
  1015. mCheckAccessibility = true;
  1016. }
  1017. BfGenericParamSource(BfMethodInstance* methodInstance)
  1018. {
  1019. mTypeInstance = NULL;
  1020. mMethodInstance = methodInstance;
  1021. mCheckAccessibility = true;
  1022. }
  1023. BfTypeInstance* GetTypeInstance() const
  1024. {
  1025. if (mTypeInstance != NULL)
  1026. return mTypeInstance;
  1027. if (mMethodInstance != NULL)
  1028. return mMethodInstance->GetOwner();
  1029. return NULL;
  1030. }
  1031. };
  1032. class BfAmbiguityContext
  1033. {
  1034. public:
  1035. class Entry
  1036. {
  1037. public:
  1038. BfTypeInterfaceEntry* mInterfaceEntry;
  1039. int mMethodIdx;
  1040. Array<BfMethodInstance*> mCandidates;
  1041. };
  1042. public:
  1043. BfModule* mModule;
  1044. BfTypeInstance* mTypeInstance;
  1045. bool mIsProjectSpecific;
  1046. bool mIsReslotting;
  1047. Dictionary<int, Entry> mEntries;
  1048. public:
  1049. BfAmbiguityContext()
  1050. {
  1051. mModule = NULL;
  1052. mTypeInstance = NULL;
  1053. mIsProjectSpecific = false;
  1054. mIsReslotting = false;
  1055. }
  1056. void Add(int id, BfTypeInterfaceEntry* ifaceEntry, int methodIdx, BfMethodInstance* candidateA, BfMethodInstance* candidateB);
  1057. void Remove(int id);
  1058. void Finish();
  1059. };
  1060. enum BfDefaultValueKind
  1061. {
  1062. BfDefaultValueKind_Const,
  1063. BfDefaultValueKind_Value,
  1064. BfDefaultValueKind_Addr,
  1065. BfDefaultValueKind_Undef
  1066. };
  1067. class BfModuleFileName
  1068. {
  1069. public:
  1070. Array<BfProject*> mProjects;
  1071. String mFileName;
  1072. bool mModuleWritten;
  1073. bool mWroteToLib;
  1074. bool operator==(const BfModuleFileName& second) const
  1075. {
  1076. return (mProjects == second.mProjects) && (mFileName == second.mFileName);
  1077. }
  1078. };
  1079. class BfGlobalLookup
  1080. {
  1081. public:
  1082. enum Kind
  1083. {
  1084. Kind_All,
  1085. Kind_Field,
  1086. Kind_Method
  1087. };
  1088. public:
  1089. Kind mKind;
  1090. String mName;
  1091. };
  1092. enum BfResolveTypeRefFlags
  1093. {
  1094. BfResolveTypeRefFlag_None = 0,
  1095. BfResolveTypeRefFlag_NoResolveGenericParam = 1,
  1096. BfResolveTypeRefFlag_AllowRef = 2,
  1097. BfResolveTypeRefFlag_AllowRefGeneric = 4,
  1098. BfResolveTypeRefFlag_IgnoreLookupError = 8,
  1099. BfResolveTypeRefFlag_AllowGenericTypeParamConstValue = 0x10,
  1100. BfResolveTypeRefFlag_AllowGenericMethodParamConstValue = 0x20,
  1101. BfResolveTypeRefFlag_AllowGenericParamConstValue = 0x10 | 0x20,
  1102. BfResolveTypeRefFlag_AutoComplete = 0x40,
  1103. BfResolveTypeRefFlag_FromIndirectSource = 0x80, // Such as a type alias or a generic parameter
  1104. };
  1105. enum BfSrcPosFlags
  1106. {
  1107. BfSrcPosFlag_None = 0,
  1108. BfSrcPosFlag_Expression = 1,
  1109. BfSrcPosFlag_NoSetDebugLoc = 2,
  1110. BfSrcPosFlag_Force = 4
  1111. };
  1112. enum BfDeferredBlockFlags
  1113. {
  1114. BfDeferredBlockFlag_None = 0,
  1115. BfDeferredBlockFlag_BypassVirtual = 1,
  1116. BfDeferredBlockFlag_DoNullChecks = 2,
  1117. BfDeferredBlockFlag_SkipObjectAccessCheck = 4,
  1118. BfDeferredBlockFlag_MoveNewBlocksToEnd = 8,
  1119. };
  1120. class BfVDataExtEntry
  1121. {
  1122. public:
  1123. BfTypeInstance* mDeclTypeInst;
  1124. BfTypeInstance* mImplTypeInst;
  1125. bool operator==(const BfVDataExtEntry& rhs)
  1126. {
  1127. return ((mDeclTypeInst == rhs.mDeclTypeInst) && (mImplTypeInst == rhs.mImplTypeInst));
  1128. }
  1129. };
  1130. class BfModule : public BfStructuralVisitor
  1131. {
  1132. public:
  1133. enum RebuildKind
  1134. {
  1135. RebuildKind_None,
  1136. RebuildKind_SkipOnDemandTypes,
  1137. RebuildKind_All
  1138. };
  1139. public:
  1140. Val128 mDataHash;
  1141. String mModuleName;
  1142. Array<BfModuleFileName> mOutFileNames;
  1143. // SpecializedModules contain method specializations with types that come from other projects
  1144. Dictionary<Array<BfProject*>, BfModule*> mSpecializedMethodModules;
  1145. BfModule* mParentModule;
  1146. BfModule* mNextAltModule; // Linked
  1147. BfModuleOptions* mModuleOptions; // Only in altModules
  1148. BfSystem* mSystem;
  1149. BfCompiler* mCompiler;
  1150. BfContext* mContext;
  1151. BfProject* mProject;
  1152. BfIRType mStringLiteralType;
  1153. BfTypeInstance* mCurTypeInstance;
  1154. Dictionary<BfParserData*, BfFileInstance*> mFileInstanceMap;
  1155. Dictionary<String, BfFileInstance*> mNamedFileInstanceMap;
  1156. Array<BfTypeInstance*> mOwnedTypeInstances;
  1157. Dictionary<int, BfIRValue> mStringObjectPool;
  1158. Dictionary<int, BfIRValue> mStringCharPtrPool;
  1159. Array<int> mStringPoolRefs;
  1160. Array<BfIRBuilder*> mPrevIRBuilders; // Before extensions
  1161. BfIRBuilder* mBfIRBuilder;
  1162. BfMethodState* mCurMethodState;
  1163. BfAttributeState* mAttributeState;
  1164. BfFilePosition mCurFilePosition;
  1165. BfMethodInstance* mCurMethodInstance;
  1166. BfParentNodeEntry* mParentNodeEntry;
  1167. BfIRFunction mBuiltInFuncs[BfBuiltInFuncType_Count];
  1168. Array<BfDllImportEntry> mDllImportEntries;
  1169. Array<int> mImportFileNames;
  1170. Dictionary<BfMethodRef, BfIRValue> mFuncReferences;
  1171. Dictionary<BfFieldRef, BfIRValue> mStaticFieldRefs;
  1172. Dictionary<BfTypeInstance*, BfIRValue> mInterfaceSlotRefs;
  1173. Dictionary<BfTypeInstance*, BfIRValue> mClassVDataRefs;
  1174. Dictionary<BfVDataExtEntry, BfIRValue> mClassVDataExtRefs;
  1175. Dictionary<BfType*, BfIRValue> mTypeDataRefs;
  1176. Dictionary<BfType*, BfIRValue> mDbgRawAllocDataRefs;
  1177. Dictionary<BfMethodInstance*, BfDeferredMethodCallData*> mDeferredMethodCallData;
  1178. HashSet<int64> mDeferredMethodIds;
  1179. HashSet<BfModule*> mModuleRefs;
  1180. BfIRMDNode mDICompileUnit;
  1181. int mRevision;
  1182. int mRebuildIdx;
  1183. int mLastUsedRevision;
  1184. int mExtensionCount;
  1185. int mIncompleteMethodCount;
  1186. int mOnDemandMethodCount;
  1187. int mLastModuleWrittenRevision;
  1188. int mCurLocalMethodId;
  1189. int16 mUsedSlotCount; // -1 = not used, 0 = awaiting
  1190. bool mAddedToCount;
  1191. bool mHasForceLinkMarker;
  1192. bool mIsReified;
  1193. bool mReifyQueued;
  1194. bool mWantsIRIgnoreWrites;
  1195. bool mHasGenericMethods;
  1196. bool mIsSpecialModule; // vdata, unspecialized, external
  1197. bool mIsScratchModule;
  1198. bool mIsSpecializedMethodModuleRoot;
  1199. bool mIsModuleMutable; // Set to false after writing module to disk, can be set back to true after doing extension module
  1200. bool mWroteToLib;
  1201. bool mHadBuildError;
  1202. bool mHadBuildWarning;
  1203. bool mHadVarUsage;
  1204. bool mIgnoreErrors;
  1205. bool mIgnoreWarnings;
  1206. bool mSetIllegalSrcPosition;
  1207. bool mHadIgnoredError;
  1208. bool mReportErrors; // Still puts system in error state when set to false
  1209. bool mIsInsideAutoComplete;
  1210. bool mIsHotModule;
  1211. bool mIsDeleting;
  1212. bool mSkipInnerLookup;
  1213. bool mAwaitingInitFinish;
  1214. bool mAwaitingFinish;
  1215. bool mHasFullDebugInfo;
  1216. bool mNoResolveGenericParams;
  1217. bool mHadHotObjectWrites;
  1218. public:
  1219. void NotImpl(BfAstNode* astNode);
  1220. void AddMethodReference(const BfMethodRef& methodRef, BfGetMethodInstanceFlags flags = BfGetMethodInstanceFlag_None);
  1221. bool CheckProtection(BfProtection protection, bool allowProtected, bool allowPrivate);
  1222. void GetAccessAllowed(BfTypeInstance* checkType, bool& allowProtected, bool& allowPrivate);
  1223. bool CheckProtection(BfProtectionCheckFlags& flags, BfTypeInstance* memberOwner, BfProtection memberProtection, BfTypeInstance* lookupStartType);
  1224. void SetElementType(BfAstNode* astNode, BfSourceElementType elementType);
  1225. void SetHadVarUsage();
  1226. BfError* Fail(const StringImpl& error, BfAstNode* refNode = NULL, bool isPersistent = false);
  1227. BfError* FailAfter(const StringImpl& error, BfAstNode* refNode);
  1228. BfError* Warn(int warningNum, const StringImpl& warning, BfAstNode* refNode = NULL, bool isPersistent = false);
  1229. void CheckRangeError(BfType* type, BfAstNode* refNode);
  1230. bool CheckCircularDataError();
  1231. BfFileInstance* GetFileFromNode(BfAstNode* astNode);
  1232. //void UpdateSrcPos(BfAstNode* astNode, bool setDebugLoc = true, int debugLocOffset = 0, bool force = false);
  1233. void UpdateSrcPos(BfAstNode* astNode, BfSrcPosFlags flags = BfSrcPosFlag_None, int debugLocOffset = 0);
  1234. void UseDefaultSrcPos(BfSrcPosFlags flags = BfSrcPosFlag_None, int debugLocOffset = 0);
  1235. void UpdateExprSrcPos(BfAstNode* astNode, BfSrcPosFlags flags = BfSrcPosFlag_None);
  1236. void SetIllegalSrcPos(BfSrcPosFlags flags = BfSrcPosFlag_None);
  1237. void SetIllegalExprSrcPos(BfSrcPosFlags flags = BfSrcPosFlag_None);
  1238. void GetConstClassValueParam(BfIRValue classVData, SizedArrayImpl<BfIRValue>& typeValueParams);
  1239. BfIRValue GetConstValue(int64 val);
  1240. BfIRValue GetConstValue(int64 val, BfType* type);
  1241. BfIRValue GetConstValue8(int val);
  1242. BfIRValue GetConstValue32(int32 val);
  1243. BfIRValue GetConstValue64(int64 val);
  1244. BfIRValue GetDefaultValue(BfType* type);
  1245. BfTypedValue GetFakeTypedValue(BfType* type);
  1246. BfTypedValue GetDefaultTypedValue(BfType* type, bool allowRef = false, BfDefaultValueKind defaultValueKind = BfDefaultValueKind_Const);
  1247. BfIRValue CreateStringObjectValue(const StringImpl& str, int stringId, bool define);
  1248. BfIRValue CreateStringCharPtr(const StringImpl& str, int stringId, bool define);
  1249. int GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL);
  1250. String* GetStringPoolString(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL);
  1251. BfIRValue GetStringCharPtr(int stringId);
  1252. BfIRValue GetStringCharPtr(BfIRValue strValue);
  1253. BfIRValue GetStringCharPtr(const StringImpl& str);
  1254. BfIRValue GetStringObjectValue(int idx);
  1255. BfIRValue GetStringObjectValue(const StringImpl& str, bool define = false);
  1256. BfIRValue CreateGlobalConstValue(const StringImpl& name, BfIRValue constant, BfIRType type, bool external);
  1257. void VariantToString(StringImpl& str, const BfVariant& variant);
  1258. StringT<128> TypeToString(BfType* resolvedType);
  1259. StringT<128> TypeToString(BfType* resolvedType, BfTypeNameFlags typeNameFlags, Array<String>* genericMethodParamNameOverrides = NULL);
  1260. void DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameFlags typeNameFlags = BfTypeNameFlags_None, Array<String>* genericMethodParamNameOverrides = NULL);
  1261. String MethodToString(BfMethodInstance* methodInst, BfMethodNameFlags methodNameFlags = BfMethodNameFlag_ResolveGenericParamNames, BfTypeVector* methodGenericArgs = NULL);
  1262. void CurrentAddToConstHolder(BfIRValue& irVal);
  1263. void ClearConstData();
  1264. BfTypedValue GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType);
  1265. BfIRValue ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType);
  1266. void ValidateCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeTargets attrTarget);
  1267. void GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeDirective* attributesDirective, BfAttributeTargets attrType);
  1268. BfCustomAttributes* GetCustomAttributes(BfAttributeDirective* attributesDirective, BfAttributeTargets attrType);
  1269. void ProcessTypeInstCustomAttributes(bool& isPacked, bool& isUnion, bool& isCRepr, bool& isOrdered);
  1270. void ProcessCustomAttributeData();
  1271. bool TryGetConstString(BfIRConstHolder* constHolder, BfIRValue irValue, StringImpl& str);
  1272. BfVariant TypedValueToVariant(BfAstNode* refNode, const BfTypedValue& value, bool allowUndef = false);
  1273. BfTypedValue FlushNullConditional(BfTypedValue result, bool ignoreNullable = false);
  1274. void NewScopeState(bool createLexicalBlock = true, bool flushValueScope = true); // returns prev scope data
  1275. BfIRValue CreateAlloca(BfType* type, bool addLifetime = true, const char* name = NULL, BfIRValue arraySize = BfIRValue());
  1276. BfIRValue CreateAllocaInst(BfTypeInstance* typeInst, bool addLifetime = true, const char* name = NULL);
  1277. void AddStackAlloc(BfTypedValue val, BfAstNode* refNode, BfScopeData* scope, bool condAlloca = false, bool mayEscape = false);
  1278. void RestoreScoreState_LocalVariables();
  1279. void RestoreScopeState();
  1280. void MarkDynStack(BfScopeData* scope);
  1281. void SaveStackState(BfScopeData* scope);
  1282. BfIRValue ValueScopeStart();
  1283. void ValueScopeEnd(BfIRValue valueScopeStart);
  1284. void AddBasicBlock(BfIRBlock bb, bool activate = true);
  1285. void VisitEmbeddedStatement(BfAstNode* stmt, BfExprEvaluator* exprEvaluator = NULL, BfEmbeddedStatementFlags flags = BfEmbeddedStatementFlags_None);
  1286. void VisitCodeBlock(BfBlock* block);
  1287. void VisitCodeBlock(BfBlock* block, BfIRBlock continueBlock, BfIRBlock breakBlock, BfIRBlock fallthroughBlock, bool defaultBreak, bool* hadReturn = NULL, BfLabelNode* labelNode = NULL, bool closeScope = false);
  1288. void DoForLess(BfForEachStatement* forEachStmt);
  1289. // Util
  1290. void CreateReturn(BfIRValue val);
  1291. void EmitReturn(BfIRValue val);
  1292. void EmitDefaultReturn();
  1293. void EmitDeferredCall(BfModuleMethodInstance moduleMethodInstance, SizedArrayImpl<BfIRValue>& llvmArgs, BfDeferredBlockFlags flags = BfDeferredBlockFlag_None);
  1294. bool AddDeferredCallEntry(BfDeferredCallEntry* deferredCallEntry, BfScopeData* scope);
  1295. void AddDeferredBlock(BfBlock* block, BfScopeData* scope, Array<BfDeferredCapture>* captures = NULL);
  1296. BfDeferredCallEntry* AddDeferredCall(const BfModuleMethodInstance& moduleMethodInstance, SizedArrayImpl<BfIRValue>& llvmArgs, BfScopeData* scope, BfAstNode* srcNode = NULL, bool bypassVirtual = false, bool doNullCheck = false);
  1297. void EmitDeferredCall(BfDeferredCallEntry& deferredCallEntry);
  1298. void EmitDeferredCallProcessor(SLIList<BfDeferredCallEntry*>& callEntries, BfIRValue callTail);
  1299. bool CanImplicitlyCast(BfTypedValue typedVal, BfType* toType, BfCastFlags castFlags = BfCastFlags_None);
  1300. bool AreSplatsCompatible(BfType* fromType, BfType* toType, bool* outNeedsMemberCasting);
  1301. BfTypedValue BoxValue(BfAstNode* srcNode, BfTypedValue typedVal, BfType* toType /*Can be System.Object or interface*/, const BfAllocTarget& allocTarget, bool callDtor = true);
  1302. BfIRValue CastToFunction(BfAstNode* srcNode, BfMethodInstance* methodInstance, BfType* toType, BfCastFlags castFlags = BfCastFlags_None);
  1303. BfIRValue CastToValue(BfAstNode* srcNode, BfTypedValue val, BfType* toType, BfCastFlags castFlags = BfCastFlags_None, BfCastResultFlags* resultFlags = NULL);
  1304. BfTypedValue Cast(BfAstNode* srcNode, const BfTypedValue& val, BfType* toType, BfCastFlags castFlags = BfCastFlags_None);
  1305. BfPrimitiveType* GetIntCoercibleType(BfType* type);
  1306. BfTypedValue GetIntCoercible(const BfTypedValue& typedValue);
  1307. bool WantsDebugInfo();
  1308. BfTypeOptions* GetTypeOptions();
  1309. void CleanupFileInstances();
  1310. void AssertErrorState();
  1311. void AssertParseErrorState();
  1312. void InitTypeInst(BfTypedValue typedValue, BfScopeData* scope, bool zeroMemory, BfIRValue dataSize);
  1313. BfIRValue AllocBytes(BfAstNode* refNode, const BfAllocTarget& allocTarget, BfType* type, BfIRValue sizeValue, BfIRValue alignValue, BfAllocFlags allocFlags/*bool zeroMemory, bool defaultToMalloc*/);
  1314. BfIRValue GetMarkFuncPtr(BfType* type);
  1315. BfIRValue GetDbgRawAllocData(BfType* type);
  1316. BfIRValue AllocFromType(BfType* type, const BfAllocTarget& allocTarget, BfIRValue appendSizeValue = BfIRValue(), BfIRValue arraySize = BfIRValue(), int arrayDim = 0, /*bool isRawArrayAlloc = false, bool zeroMemory = true*/BfAllocFlags allocFlags = BfAllocFlags_ZeroMemory, int alignOverride = -1);
  1317. void ValidateAllocation(BfType* type, BfAstNode* refNode);
  1318. bool IsOptimized();
  1319. void EmitAlign(BfIRValue& appendCurIdx, int align);
  1320. void EmitAppendAlign(int align, int sizeMultiple = 0);
  1321. BfIRValue AppendAllocFromType(BfType* type, BfIRValue appendSizeValue = BfIRValue(), int appendAllocAlign = 0, BfIRValue arraySize = BfIRValue(), int arrayDim = 0, bool isRawArrayAlloc = false, bool zeroMemory = true);
  1322. bool IsTargetingBeefBackend();
  1323. bool WantsLifetimes();
  1324. bool HasCompiledOutput();
  1325. void SkipObjectAccessCheck(BfTypedValue typedVal);
  1326. void EmitObjectAccessCheck(BfTypedValue typedVal);
  1327. void EmitEnsureInstructionAt();
  1328. void EmitDynamicCastCheck(const BfTypedValue& targetValue, BfType* targetType, BfIRBlock trueBlock, BfIRBlock falseBlock, bool nullSucceeds = false);
  1329. void EmitDynamicCastCheck(BfTypedValue typedVal, BfType* type, bool allowNull);
  1330. void CheckStaticAccess(BfTypeInstance* typeInstance);
  1331. BfFieldInstance* GetFieldByName(BfTypeInstance* typeInstance, const StringImpl& fieldName);
  1332. BfTypedValue RemoveRef(BfTypedValue typedValue);
  1333. BfTypedValue LoadOrAggregateValue(BfTypedValue typedValue);
  1334. BfTypedValue LoadValue(BfTypedValue typedValue, BfAstNode* refNode = NULL, bool isVolatile = false);
  1335. void AggregateSplatIntoAddr(BfTypedValue typedValue, BfIRValue addrVal);
  1336. BfTypedValue AggregateSplat(BfTypedValue typedValue, BfIRValue* valueArrPtr = NULL);
  1337. BfTypedValue MakeAddressable(BfTypedValue typedValue);
  1338. BfTypedValue RemoveReadOnly(BfTypedValue typedValue);
  1339. BfIRValue ExtractSplatValue(BfTypedValue typedValue, int componentIdx, BfType* wantType = NULL, bool* isAddr = NULL);
  1340. BfTypedValue ExtractValue(BfTypedValue typedValue, BfFieldInstance* fieldInst, int fieldIdx);
  1341. BfIRValue ExtractValue(BfTypedValue typedValue, int dataIdx);
  1342. BfIRValue CreateIndexedValue(BfType* elementType, BfIRValue value, BfIRValue indexValue, bool isElementIndex = false);
  1343. BfIRValue CreateIndexedValue(BfType* elementType, BfIRValue value, int indexValue, bool isElementIndex = false);
  1344. bool CheckModifyValue(BfTypedValue& typedValue, BfAstNode* refNode, const char* modifyType = NULL);
  1345. BfIRValue GetInterfaceSlotNum(BfTypeInstance* ifaceType);
  1346. void HadSlotCountDependency();
  1347. BfTypedValue ReferenceStaticField(BfFieldInstance* fieldInstance);
  1348. BfTypedValue GetThis();
  1349. BfLocalVariable* GetThisVariable();
  1350. bool IsInGeneric();
  1351. bool IsInSpecializedGeneric();
  1352. bool IsInSpecializedSection(); // Either a specialized generic or an injected mixin
  1353. bool IsInUnspecializedGeneric();
  1354. // BfStmtEvaluator.cpp
  1355. virtual void Visit(BfAstNode* astNode) override;
  1356. virtual void Visit(BfIdentifierNode* identifierNode) override;
  1357. virtual void Visit(BfTypeReference* typeRef) override;
  1358. virtual void Visit(BfEmptyStatement* astNode) override;
  1359. virtual void Visit(BfExpression* expressionStmt) override;
  1360. virtual void Visit(BfExpressionStatement* expressionStmt) override;
  1361. virtual void Visit(BfVariableDeclaration* varDecl) override;
  1362. virtual void Visit(BfLocalMethodDeclaration* methodDecl) override;
  1363. virtual void Visit(BfThrowStatement* throwStmt) override;
  1364. virtual void Visit(BfDeleteStatement* deleteStmt) override;
  1365. virtual void Visit(BfSwitchStatement* switchStmt) override;
  1366. virtual void Visit(BfTryStatement* tryStmt) override;
  1367. virtual void Visit(BfCatchStatement* catchStmt) override;
  1368. virtual void Visit(BfFinallyStatement* finallyStmt) override;
  1369. virtual void Visit(BfCheckedStatement* checkedStmt) override;
  1370. virtual void Visit(BfUncheckedStatement* uncheckedStmt) override;
  1371. void DoIfStatement(BfIfStatement* ifStmt, bool includeTrueStmt, bool includeFalseStmt);
  1372. virtual void Visit(BfIfStatement* ifStmt) override;
  1373. virtual void Visit(BfReturnStatement* returnStmt) override;
  1374. virtual void Visit(BfYieldStatement* yieldStmt) override;
  1375. virtual void Visit(BfBreakStatement* breakStmt) override;
  1376. virtual void Visit(BfContinueStatement* continueStmt) override;
  1377. virtual void Visit(BfFallthroughStatement* fallthroughStmt) override;
  1378. virtual void Visit(BfUsingStatement* usingStmt) override;
  1379. virtual void Visit(BfDoStatement* doStmt) override;
  1380. virtual void Visit(BfRepeatStatement* doStmt) override;
  1381. virtual void Visit(BfWhileStatement* whileStmt) override;
  1382. virtual void Visit(BfForStatement* forStmt) override;
  1383. virtual void Visit(BfForEachStatement* forEachStmt) override;
  1384. virtual void Visit(BfDeferStatement* deferStmt) override;
  1385. virtual void Visit(BfBlock* block) override;
  1386. virtual void Visit(BfLabeledBlock* labeledBlock) override;
  1387. virtual void Visit(BfRootNode* rootNode) override;
  1388. virtual void Visit(BfInlineAsmStatement* asmStmt) override;
  1389. // Type helpers
  1390. BfGenericExtensionEntry* BuildGenericExtensionInfo(BfGenericTypeInstance* genericTypeInst, BfTypeDef* partialTypeDef);
  1391. bool BuildGenericParams(BfType* resolvedTypeRef);
  1392. bool ValidateGenericConstraints(BfTypeReference* typeRef, BfGenericTypeInstance* genericTypeInstance, bool ignoreErrors);
  1393. bool AreConstraintsSubset(BfGenericParamInstance* checkInner, BfGenericParamInstance* checkOuter);
  1394. bool ShouldAllowMultipleDefinitions(BfTypeInstance* typeInst, BfTypeDef* firstDeclaringTypeDef, BfTypeDef* secondDeclaringTypeDef);
  1395. bool InitType(BfType* resolvedTypeRef, BfPopulateType populateType);
  1396. bool CheckAccessMemberProtection(BfProtection protection, BfType* memberType);
  1397. bool CheckDefineMemberProtection(BfProtection protection, BfType* memberType);
  1398. void CheckMemberNames(BfTypeInstance* typeInst);
  1399. void AddDependency(BfType* usedType, BfType* usingType, BfDependencyMap::DependencyDependencyFlag flags);
  1400. void AddCallDependency(BfMethodInstance* methodInstance, bool devirtualized = false);
  1401. void AddFieldDependency(BfTypeInstance* typeInstance, BfFieldInstance* fieldInstance, BfType* fieldType);
  1402. void TypeFailed(BfTypeInstance* typeInstance);
  1403. bool IsAttribute(BfTypeInstance* typeInst);
  1404. void PopulateGlobalContainersList(const BfGlobalLookup& globalLookup);
  1405. void AddFailType(BfTypeInstance* typeInstance);
  1406. void MarkDerivedDirty(BfTypeInstance* typeInst);
  1407. void CheckAddFailType();
  1408. bool PopulateType(BfType* resolvedTypeRef, BfPopulateType populateType = BfPopulateType_Data);
  1409. int GenerateTypeOptions(BfCustomAttributes* customAttributes, BfTypeInstance* typeInstance, bool checkTypeName);
  1410. void SetTypeOptions(BfTypeInstance* typeInstance);
  1411. BfModuleOptions GetModuleOptions();
  1412. BfCheckedKind GetDefaultCheckedKind();
  1413. bool DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateType = BfPopulateType_Data);
  1414. static BfModule* GetModuleFor(BfType* type);
  1415. void DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance);
  1416. void RebuildMethods(BfTypeInstance* typeInstance);
  1417. void CreateStaticField(BfFieldInstance* fieldInstance, bool isThreadLocal = false);
  1418. void ResolveConstField(BfTypeInstance* typeInst, BfFieldInstance* fieldInstance, BfFieldDef* field, bool forceResolve = false);
  1419. BfTypedValue GetFieldInitializerValue(BfFieldInstance* fieldInstance, BfExpression* initializer = NULL, BfFieldDef* fieldDef = NULL, BfType* fieldType = NULL);
  1420. void MarkFieldInitialized(BfFieldInstance* fieldInstance);
  1421. bool IsThreadLocal(BfFieldInstance* fieldInstance);
  1422. BfType* ResolveVarFieldType(BfTypeInstance* typeInst, BfFieldInstance* fieldInstance, BfFieldDef* field);
  1423. void FindSubTypes(BfTypeInstance* classType, SizedArrayImpl<int>* outVals, SizedArrayImpl<BfTypeInstance*>* exChecks, bool isInterfacePass);
  1424. BfType* CheckUnspecializedGenericType(BfGenericTypeInstance* genericTypeInst, BfPopulateType populateType);
  1425. BfTypeInstance* GetUnspecializedTypeInstance(BfTypeInstance* typeInst);
  1426. BfArrayType* CreateArrayType(BfType* resolvedType, int dimensions);
  1427. BfSizedArrayType* CreateSizedArrayType(BfType* resolvedType, int size);
  1428. BfUnknownSizedArrayType* CreateUnknownSizedArrayType(BfType* resolvedType, BfType* sizeParam);
  1429. BfPointerType* CreatePointerType(BfType* resolvedType);
  1430. BfPointerType* CreatePointerType(BfTypeReference* typeRef);
  1431. BfConstExprValueType* CreateConstExprValueType(const BfTypedValue& typedValue);
  1432. BfBoxedType* CreateBoxedType(BfType* resolvedTypeRef);
  1433. BfTupleType* CreateTupleType(const BfTypeVector& fieldTypes, const Array<String>& fieldNames);
  1434. BfTupleType* SantizeTupleType(BfTupleType* tupleType);
  1435. BfRefType* CreateRefType(BfType* resolvedTypeRef, BfRefType::RefKind refKind = BfRefType::RefKind_Ref);
  1436. BfRetTypeType* CreateRetTypeType(BfType* resolvedTypeRef);
  1437. BfConcreteInterfaceType* CreateConcreteInterfaceType(BfTypeInstance* interfaceType);
  1438. BfTypeInstance* GetWrappedStructType(BfType* type, bool allowSpecialized = true);
  1439. BfTypeInstance* GetPrimitiveStructType(BfTypeCode typeCode);
  1440. BfPrimitiveType* GetPrimitiveType(BfTypeCode typeCode);
  1441. BfMethodRefType* CreateMethodRefType(BfMethodInstance* methodInstance, bool mustAlreadyExist = false);
  1442. BfType* FixIntUnknown(BfType* type);
  1443. void FixIntUnknown(BfTypedValue& typedVal);
  1444. void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs);
  1445. BfTypeDef* ResolveGenericInstanceDef(BfGenericInstanceTypeRef* genericTypeRef);
  1446. BfType* ResolveType(BfType* lookupType, BfPopulateType populateType = BfPopulateType_Data);
  1447. void ResolveGenericParamConstraints(BfGenericParamInstance* genericParamInstance, const Array<BfGenericParamDef*>& genericParamDefs, int genericParamIdx);
  1448. String GenericParamSourceToString(const BfGenericParamSource& genericParamSource);
  1449. bool CheckGenericConstraints(const BfGenericParamSource& genericParamSource, BfType* checkArgType, BfAstNode* checkArgTypeRef, BfGenericParamInstance* genericParamInst, BfTypeVector* methodGenericArgs = NULL, BfError** errorOut = NULL);
  1450. BfIRValue AllocLocalVariable(BfType* type, const StringImpl& name, bool doLifetimeEnd = true);
  1451. void DoAddLocalVariable(BfLocalVariable* localVar);
  1452. BfLocalVariable* AddLocalVariableDef(BfLocalVariable* localVarDef, bool addDebugInfo = false, bool doAliasValue = false, BfIRValue declareBefore = BfIRValue(), BfIRInitType initType = BfIRInitType_NotSet);
  1453. bool TryLocalVariableInit(BfLocalVariable* localVar);
  1454. void LocalVariableDone(BfLocalVariable* localVar, bool isMethodExit);
  1455. void CreateDIRetVal();
  1456. void CheckTupleVariableDeclaration(BfTupleExpression* tupleExpr, BfType* initType);
  1457. void HandleTupleVariableDeclaration(BfVariableDeclaration* varDecl, BfTupleExpression* tupleExpr, BfTypedValue initTupleValue, bool isReadOnly, bool isConst, bool forceAddr, BfIRBlock* declBlock = NULL);
  1458. void HandleTupleVariableDeclaration(BfVariableDeclaration* varDecl);
  1459. void HandleCaseEnumMatch_Tuple(BfTypedValue tupleVal, const BfSizedArray<BfExpression*>& arguments, BfAstNode* tooFewRef, BfIRValue phiVal, BfIRBlock& matchedBlock, BfIRBlock falseBlock, bool& hadConditional, bool clearOutOnMismatch);
  1460. BfTypedValue TryCaseEnumMatch(BfTypedValue enumVal, BfTypedValue tagVal, BfExpression* expr, BfIRBlock* eqBlock, BfIRBlock* notEqBlock, BfIRBlock* matchBlock, int& uncondTagId, bool& hadConditional, bool clearOutOnMismatch);
  1461. BfTypedValue HandleCaseBind(BfTypedValue enumVal, const BfTypedValue& tagVal, BfEnumCaseBindExpression* bindExpr, BfIRBlock* eqBlock = NULL, BfIRBlock* notEqBlock = NULL, BfIRBlock* matchBlock = NULL, int* outEnumIdx = NULL);
  1462. void TryInitVar(BfAstNode* checkNode, BfLocalVariable* varDecl, BfTypedValue initValue, BfTypedValue& checkResult);
  1463. BfLocalVariable* HandleVariableDeclaration(BfVariableDeclaration* varDecl, BfExprEvaluator* exprEvaluator = NULL);
  1464. BfLocalVariable* HandleVariableDeclaration(BfVariableDeclaration* varDecl, BfTypedValue val, bool updateSrcLoc = true, bool forceAddr = false);
  1465. void CheckVariableDef(BfLocalVariable* variableDef);
  1466. BfScopeData* FindScope(BfAstNode* scopeName, BfMixinState* curMixinState, bool allowAcrossDeferredBlock);
  1467. BfScopeData* FindScope(BfAstNode* scopeName, bool allowAcrossDeferredBlock);
  1468. BfBreakData* FindBreakData(BfAstNode* scopeName);
  1469. void EmitLifetimeEnds(BfScopeData* scopeData);
  1470. void ClearLifetimeEnds();
  1471. bool HasDeferredScopeCalls(BfScopeData* scope);
  1472. void EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scope, BfIRBlock doneBlock = BfIRBlock());
  1473. void MarkScopeLeft(BfScopeData* scopeData);
  1474. BfGenericParamType* GetGenericParamType(BfGenericParamKind paramKind, int paramIdx);
  1475. BfType* ResolveGenericType(BfType* unspecializedType, const BfTypeVector& methodGenericArguments, bool allowFail = false);
  1476. bool IsUnboundGeneric(BfType* type);
  1477. BfGenericParamInstance* GetGenericParamInstance(BfGenericParamType* type);
  1478. BfTypeInstance* GetBaseType(BfTypeInstance* typeInst);
  1479. void HandleTypeGenericParamRef(BfAstNode* refNode, BfTypeDef* typeDef, int typeGenericParamIdx);
  1480. void HandleMethodGenericParamRef(BfAstNode* refNode, BfTypeDef* typeDef, BfMethodDef* methodDef, int typeGenericParamIdx);
  1481. BfType* ResolveTypeResult(BfTypeReference* typeRef, BfType* resolvedTypeRef, BfPopulateType populateType, BfResolveTypeRefFlags resolveFlags);
  1482. void ShowAmbiguousTypeError(BfAstNode* refNode, BfTypeDef* typeDef, BfTypeDef* otherTypeDef);
  1483. void ShowGenericArgCountError(BfTypeReference* typeRef, int wantedGenericParams);
  1484. BfTypeDef* GetActiveTypeDef(BfTypeInstance* typeInstanceOverride = NULL, bool useMixinDecl = false); // useMixinDecl is useful for type lookup, but we don't want the decl project to limit what methods the user can call
  1485. BfTypeDef* FindTypeDefRaw(const BfAtomComposite& findName, int numGenericArgs, BfTypeInstance* typeInstance, BfTypeDef* useTypeDef, BfTypeLookupError* error);
  1486. BfTypeDef* FindTypeDef(const BfAtomComposite& findName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL);
  1487. BfTypeDef* FindTypeDef(const StringImpl& typeName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL);
  1488. BfTypeDef* FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL, int numGenericParams = 0);
  1489. BfTypedValue TryLookupGenericConstVaue(BfIdentifierNode* identifierNode, BfType* expectingType);
  1490. void CheckTypeRefFixit(BfAstNode* typeRef, const char* appendName = NULL);
  1491. void CheckIdentifierFixit(BfAstNode* node);
  1492. void TypeRefNotFound(BfTypeReference* typeRef, const char* appendName = NULL);
  1493. bool ValidateTypeWildcard(BfTypeReference* typeRef, bool isAttributeRef);
  1494. BfType* ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0);
  1495. BfType* ResolveTypeRefAllowUnboundGenerics(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, bool resolveGenericParam = true);
  1496. BfType* ResolveTypeRef(BfAstNode* astNode, const BfSizedArray<BfTypeReference*>* genericArgs, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0);
  1497. //BfType* ResolveTypeRef(BfIdentifierNode* identifier, const BfSizedArray<BfTypeReference*>& genericArgs, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0);
  1498. BfType* ResolveTypeDef(BfTypeDef* typeDef, BfPopulateType populateType = BfPopulateType_Data);
  1499. BfType* ResolveTypeDef(BfTypeDef* typeDef, const BfTypeVector& genericArgs, BfPopulateType populateType = BfPopulateType_Data);
  1500. BfType* ResolveInnerType(BfType* outerType, BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, bool ignoreErrors = false);
  1501. BfType* ResolveInnerType(BfType* outerType, BfIdentifierNode* identifier, BfPopulateType populateType = BfPopulateType_Data, bool ignoreErrors = false);
  1502. BfTypeDef* GetCombinedPartialTypeDef(BfTypeDef* type);
  1503. BfTypeInstance* GetOuterType(BfType* type);
  1504. bool IsInnerType(BfType* checkInnerType, BfType* checkOuterType);
  1505. bool IsInnerType(BfTypeDef* checkInnerType, BfTypeDef* checkOuterType);
  1506. bool TypeHasParent(BfTypeDef* checkChildTypeDef, BfTypeDef* checkParentTypeDef);
  1507. BfTypeDef* FindCommonOuterType(BfTypeDef* type, BfTypeDef* type2);
  1508. bool TypeIsSubTypeOf(BfTypeInstance* srcType, BfTypeInstance* wantType, bool checkAccessibility = true);
  1509. int GetTypeDistance(BfType* fromType, BfType* toType);
  1510. bool IsTypeMoreSpecific(BfType* leftType, BfType* rightType);
  1511. bool GetBasePropertyDef(BfPropertyDef*& propDef, BfTypeInstance*& typeInst);
  1512. // Method helpers
  1513. void CheckInterfaceMethod(BfMethodInstance* methodInstance);
  1514. void CreateDelegateInvokeMethod();
  1515. BfType* GetDelegateReturnType(BfType* delegateType);
  1516. BfMethodInstance* GetDelegateInvokeMethod(BfTypeInstance* typeInstance);
  1517. String GetLocalMethodName(const StringImpl& baseName, BfAstNode* anchorNode, BfMethodState* declMethodState, BfMixinState* declMixinState);
  1518. BfMethodDef* GetLocalMethodDef(BfLocalMethod* localMethod);
  1519. BfModuleMethodInstance GetLocalMethodInstance(BfLocalMethod* localMethod, const BfTypeVector& methodGenericArguments, BfMethodInstance* methodInstance = NULL, bool force = false);
  1520. int GetLocalInferrableGenericArgCount(BfMethodDef* methodDef);
  1521. void GetMethodCustomAttributes(BfMethodInstance* methodInstance);
  1522. void SetupIRFunction(BfMethodInstance* methodInstance, StringImpl& mangledName, bool isTemporaryFunc, bool* outIsIntrinsic);
  1523. void CheckHotMethod(BfMethodInstance* methodInstance, const StringImpl& mangledName);
  1524. void DoMethodDeclaration(BfMethodDeclaration* methodDeclaration, bool isTemporaryFunc, bool addToWorkList = true);
  1525. void AddMethodToWorkList(BfMethodInstance* methodInstance);
  1526. bool IsInterestedInMethod(BfTypeInstance* typeInstance, BfMethodDef* methodDef);
  1527. void CalcAppendAlign(BfMethodInstance* methodInst);
  1528. BfTypedValue TryConstCalcAppend(BfMethodInstance* methodInst, SizedArrayImpl<BfIRValue>& args);
  1529. BfTypedValue CallBaseCtorCalc(bool constOnly);
  1530. void EmitCtorCalcAppend();
  1531. void CreateStaticCtor();
  1532. BfIRValue CreateDllImportGlobalVar(BfMethodInstance* methodInstance, bool define = false);
  1533. void CreateDllImportMethod();
  1534. BfIRCallingConv GetCallingConvention(BfTypeInstance* typeInst, BfMethodDef* methodDef);
  1535. BfIRCallingConv GetCallingConvention(BfMethodInstance* methodInstance);
  1536. void SetupIRMethod(BfMethodInstance* methodInstance, BfIRFunction func, bool isInlined);
  1537. void EmitCtorBody(bool& skipBody);
  1538. void EmitDtorBody();
  1539. void EmitEnumToStringBody();
  1540. void EmitTupleToStringBody();
  1541. void EmitGCMarkValue(BfTypedValue& thisValue, BfType* checkType, int memberDepth, int curOffset, HashSet<int>& objectOffsets, BfModuleMethodInstance markFromGCThreadMethodInstance);
  1542. void EmitGCMarkValue(BfTypedValue markVal, BfModuleMethodInstance markFromGCThreadMethodInstance);
  1543. void EmitGCMarkMembers();
  1544. void EmitGCFindTLSMembers();
  1545. void EmitIteratorBlock(bool& skipBody);
  1546. void EmitEquals(BfTypedValue leftValue, BfTypedValue rightValue, BfIRBlock exitBB);
  1547. void CreateFakeCallerMethod(const String& funcName);
  1548. void CallChainedMethods(BfMethodInstance* methodInstance, bool reverse = false);
  1549. void AddHotDataReferences(BfHotDataReferenceBuilder* builder);
  1550. void ProcessMethod_SetupParams(BfMethodInstance* methodInstance, BfType* thisType, bool wantsDIData, SizedArrayImpl<BfIRMDNode>* diParams);
  1551. void ProcessMethod_ProcessDeferredLocals(int startIdx = 0);
  1552. void ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup = false);
  1553. void CreateDynamicCastMethod();
  1554. void CreateValueTypeEqualsMethod();
  1555. BfIRFunction GetIntrinsic(BfMethodInstance* methodInstance, bool reportFailure = false);
  1556. BfIRFunction GetBuiltInFunc(BfBuiltInFuncType funcType);
  1557. BfIRValue CreateFunctionFrom(BfMethodInstance* methodInstance, bool tryExisting, bool isInlined);
  1558. void EvaluateWithNewScope(BfExprEvaluator& exprEvaluator, BfExpression* expr, BfEvalExprFlags flags);
  1559. BfTypedValue CreateValueFromExpression(BfExprEvaluator& exprEvaluator, BfExpression* expr, BfType* wantTypeRef = NULL, BfEvalExprFlags flags = BfEvalExprFlags_None, BfType** outOrigType = NULL);
  1560. BfTypedValue CreateValueFromExpression(BfExpression* expr, BfType* wantTypeRef = NULL, BfEvalExprFlags flags = BfEvalExprFlags_None, BfType** outOrigType = NULL);
  1561. BfTypedValue GetOrCreateVarAddr(BfExpression* expr);
  1562. BfMethodInstance* GetRawMethodInstanceAtIdx(BfTypeInstance* typeInstance, int methodIdx, const char* assertName = NULL);
  1563. BfMethodInstance* GetRawMethodInstance(BfTypeInstance* typeInstance, BfMethodDef* methodDef);
  1564. BfMethodInstance* GetRawMethodByName(BfTypeInstance* typeInstance, const StringImpl& methodName, int paramCount = -1, bool checkBase = false, bool allowMixin = false);
  1565. BfMethodInstance* GetUnspecializedMethodInstance(BfMethodInstance* methodInstance); // Unspecialized owner type and unspecialized method type
  1566. int GetGenericParamAndReturnCount(BfMethodInstance* methodInstance);
  1567. BfModule* GetSpecializedMethodModule(const Array<BfProject*>& projectList);
  1568. BfModuleMethodInstance GetMethodInstanceAtIdx(BfTypeInstance* typeInstance, int methodIdx, const char* assertName = NULL);
  1569. BfModuleMethodInstance GetMethodByName(BfTypeInstance* typeInstance, const StringImpl& methodName, int paramCount = -1, bool checkBase = false);
  1570. BfModuleMethodInstance GetMethodByName(BfTypeInstance* typeInstance, const StringImpl& methodName, const Array<BfType*>& paramTypes, bool checkBase = false);
  1571. BfModuleMethodInstance GetInternalMethod(const StringImpl& methodName, int paramCount = -1);
  1572. bool IsMethodImplementedAndReified(BfTypeInstance* typeInstance, const StringImpl& methodName, int paramCount = -1, bool checkBase = false);
  1573. bool HasMixin(BfTypeInstance* typeInstance, const StringImpl& methodName, int paramCount, bool checkBase = false);
  1574. bool CompareMethodSignatures(BfMethodInstance* methodA, BfMethodInstance* methodB); // Doesn't compare return types
  1575. bool IsCompatibleInterfaceMethod(BfMethodInstance* methodA, BfMethodInstance* methodB);
  1576. void UniqueSlotVirtualMethod(BfMethodInstance* methodInstance);
  1577. void CompareDeclTypes(BfTypeDef* newDeclType, BfTypeDef* prevDeclType, bool& isBetter, bool& isWorse);
  1578. bool SlotVirtualMethod(BfMethodInstance* methodInstance, BfAmbiguityContext* ambiguityContext = NULL);
  1579. bool SlotInterfaceMethod(BfMethodInstance* methodInstance);
  1580. BfModuleMethodInstance ReferenceExternalMethodInstance(BfMethodInstance* methodInstance, BfGetMethodInstanceFlags flags = BfGetMethodInstanceFlag_None);
  1581. BfModule* GetOrCreateMethodModule(BfMethodInstance* methodInstance);
  1582. BfModuleMethodInstance GetMethodInstance(BfTypeInstance* typeInst, BfMethodDef* methodDef, const BfTypeVector& methodGenericArguments, BfGetMethodInstanceFlags flags = BfGetMethodInstanceFlag_None, BfTypeInstance* foreignType = NULL);
  1583. BfModuleMethodInstance GetMethodInstance(BfMethodInstance* methodInstance, BfGetMethodInstanceFlags flags = BfGetMethodInstanceFlag_None);
  1584. BfMethodInstance* GetOuterMethodInstance(BfMethodInstance* methodInstance); // Only useful for local methods
  1585. void SetupMethodIdHash(BfMethodInstance* methodInstance);
  1586. // Type Data
  1587. BfIRValue CreateClassVDataGlobal(BfTypeInstance* typeInstance, int* outNumElements = NULL, String* outMangledName = NULL);
  1588. BfIRValue GetClassVDataPtr(BfTypeInstance* typeInstance);
  1589. BfIRValue CreateClassVDataExtGlobal(BfTypeInstance* declTypeInst, BfTypeInstance* implTypeInst, int startVirtIdx);
  1590. BfIRValue CreateTypeDataRef(BfType* type);
  1591. BfIRValue CreateTypeData(BfType* type, Dictionary<int, int>& usedStringIdMap, bool forceReflectFields, bool needsTypeData, bool needsTypeNames, bool needsVData);
  1592. BfIRValue FixClassVData(BfIRValue value);
  1593. public:
  1594. BfModule(BfContext* context, const StringImpl& moduleName);
  1595. virtual ~BfModule();
  1596. void Init(bool isFullRebuild = true);
  1597. bool WantsFinishModule();
  1598. void FinishInit();
  1599. void ReifyModule();
  1600. void UnreifyModule();
  1601. void Cleanup();
  1602. void StartNewRevision(RebuildKind rebuildKind = RebuildKind_All, bool force = false);
  1603. void PrepareForIRWriting(BfTypeInstance* typeInst);
  1604. void EnsureIRBuilder(bool dbgVerifyCodeGen = false);
  1605. void DbgFinish();
  1606. BfIRValue CreateForceLinkMarker(BfModule* module, String* outName);
  1607. void ClearModuleData();
  1608. void DisownMethods();
  1609. void ClearModule();
  1610. void StartExtension(); // For new method specializations
  1611. bool Finish();
  1612. void RemoveModuleData();
  1613. void ReportMemory(MemReporter* memReporter);
  1614. };
  1615. class BfAutoParentNodeEntry
  1616. {
  1617. public:
  1618. BfModule* mModule;
  1619. BfParentNodeEntry mParentNodeEntry;
  1620. BfAutoParentNodeEntry(BfModule* module, BfAstNode* node)
  1621. {
  1622. mModule = module;
  1623. mParentNodeEntry.mNode = node;
  1624. mParentNodeEntry.mPrev = module->mParentNodeEntry;
  1625. module->mParentNodeEntry = &mParentNodeEntry;
  1626. }
  1627. ~BfAutoParentNodeEntry()
  1628. {
  1629. mModule->mParentNodeEntry = mParentNodeEntry.mPrev;
  1630. }
  1631. };
  1632. class BfVDataModule : public BfModule
  1633. {
  1634. public:
  1635. HashSet<int> mDefinedStrings;
  1636. public:
  1637. BfVDataModule(BfContext* context) : BfModule(context, StringImpl::MakeRef("vdata"))
  1638. {
  1639. }
  1640. };
  1641. NS_BF_END
  1642. namespace std
  1643. {
  1644. template<>
  1645. struct hash<Beefy::BfMethodRef>
  1646. {
  1647. size_t operator()(const Beefy::BfMethodRef& val) const
  1648. {
  1649. if (val.mTypeInstance == NULL)
  1650. return 0;
  1651. return val.mTypeInstance->mTypeId ^ (val.mMethodNum << 10);
  1652. }
  1653. };
  1654. template<>
  1655. struct hash<Beefy::BfVDataExtEntry>
  1656. {
  1657. size_t operator()(const Beefy::BfVDataExtEntry& val) const
  1658. {
  1659. return ((size_t)(val.mDeclTypeInst) * 17) ^ (size_t)(val.mDeclTypeInst);
  1660. }
  1661. };
  1662. template<>
  1663. struct hash<Beefy::BfLocalVarEntry>
  1664. {
  1665. size_t operator()(const Beefy::BfLocalVarEntry& val) const
  1666. {
  1667. return std::hash<Beefy::String>()(val.mLocalVar->mName);
  1668. }
  1669. };
  1670. }