BfModule.h 65 KB

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