BfModule.h 76 KB

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