BfModule.h 81 KB

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