BfContext.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #pragma once
  2. #include "BfModule.h"
  3. #include "BeefySysLib/util/Deque.h"
  4. NS_BF_BEGIN
  5. class BfWorkListEntry
  6. {
  7. public:
  8. BfType* mType;
  9. BfModule* mFromModule;
  10. int mRevision;
  11. int mSignatureRevision;
  12. int mFromModuleRevision;
  13. int mFromModuleRebuildIdx;
  14. int mReqId;
  15. static int sCurReqId;
  16. public:
  17. BfWorkListEntry()
  18. {
  19. mType = NULL;
  20. mFromModule = NULL;
  21. mRevision = -1;
  22. mSignatureRevision = -1;
  23. mFromModuleRevision = -1;
  24. mFromModuleRebuildIdx = -1;
  25. mReqId = ++sCurReqId;
  26. }
  27. };
  28. class BfTypeProcessRequest : public BfWorkListEntry
  29. {
  30. public:
  31. bool mRebuildType;
  32. BfTypeProcessRequest()
  33. {
  34. mRebuildType = false;
  35. }
  36. };
  37. class BfMethodSpecializationRequest : public BfWorkListEntry
  38. {
  39. public:
  40. int32 mMethodIdx;
  41. BfTypeVector mMethodGenericArguments;
  42. BfGetMethodInstanceFlags mFlags;
  43. BfTypeInstance* mForeignType;
  44. public:
  45. BfMethodSpecializationRequest()
  46. {
  47. mMethodIdx = -1;
  48. mFlags = BfGetMethodInstanceFlag_None;
  49. mForeignType = NULL;
  50. }
  51. void Init(BfTypeInstance* typeInstance, BfTypeInstance* foreignType, BfMethodDef* methodDef)
  52. {
  53. mType = typeInstance;
  54. mMethodIdx = methodDef->mIdx;
  55. mForeignType = foreignType;
  56. if (foreignType != NULL)
  57. BF_ASSERT(foreignType->mTypeDef->mMethods[mMethodIdx] == methodDef);
  58. else
  59. BF_ASSERT(typeInstance->mTypeDef->mMethods[mMethodIdx] == methodDef);
  60. }
  61. };
  62. class BfMethodProcessRequest : public BfWorkListEntry
  63. {
  64. public:
  65. BfMethodInstance* mMethodInstance;
  66. BfMethodProcessRequest()
  67. {
  68. mMethodInstance = NULL;
  69. }
  70. ~BfMethodProcessRequest()
  71. {
  72. Disable();
  73. }
  74. void Disable()
  75. {
  76. if (mMethodInstance != NULL)
  77. {
  78. BF_ASSERT(mMethodInstance->mMethodProcessRequest == this);
  79. mMethodInstance->mMethodProcessRequest = NULL;
  80. mMethodInstance = NULL;
  81. }
  82. }
  83. };
  84. class BfInlineMethodRequest : public BfMethodProcessRequest
  85. {
  86. public:
  87. BfIRFunction mFunc;
  88. ~BfInlineMethodRequest()
  89. {
  90. mMethodInstance = NULL;
  91. }
  92. };
  93. class BfTypeRefVerifyRequest : public BfWorkListEntry
  94. {
  95. public:
  96. BfTypeInstance* mCurTypeInstance;
  97. BfAstNode* mRefNode;
  98. };
  99. struct BfStringPoolEntry
  100. {
  101. String mString;
  102. int mLastUsedRevision;
  103. int mFirstUsedRevision;
  104. };
  105. class BfGlobalContainerEntry
  106. {
  107. public:
  108. BfTypeDef* mTypeDef;
  109. BfTypeInstance* mTypeInst;
  110. };
  111. class BfTypeState
  112. {
  113. public:
  114. enum ResolveKind
  115. {
  116. ResolveKind_None,
  117. ResolveKind_BuildingGenericParams,
  118. ResolveKind_ResolvingVarType,
  119. ResolveKind_UnionInnerType,
  120. ResolveKind_LocalVariable,
  121. ResolveKind_Attributes,
  122. ResolveKind_FieldType,
  123. ResolveKind_ConstField
  124. };
  125. public:
  126. BfTypeState* mPrevState;
  127. BfType* mType;
  128. BfTypeDef* mGlobalContainerCurUserTypeDef;
  129. Array<BfGlobalContainerEntry> mGlobalContainers; // All global containers that are visible
  130. BfPopulateType mPopulateType;
  131. BfTypeReference* mCurBaseTypeRef;
  132. BfTypeInstance* mCurBaseType;
  133. BfTypeReference* mCurAttributeTypeRef;
  134. BfFieldDef* mCurFieldDef;
  135. BfTypeDef* mCurTypeDef;
  136. BfTypeDef* mForceActiveTypeDef;
  137. ResolveKind mResolveKind;
  138. BfAstNode* mCurVarInitializer;
  139. int mArrayInitializerSize;
  140. public:
  141. BfTypeState()
  142. {
  143. mPrevState = NULL;
  144. mType = NULL;
  145. mGlobalContainerCurUserTypeDef = NULL;
  146. mPopulateType = BfPopulateType_Identity;
  147. mCurBaseTypeRef = NULL;
  148. mCurBaseType = NULL;
  149. mCurFieldDef = NULL;
  150. mCurAttributeTypeRef = NULL;
  151. mCurTypeDef = NULL;
  152. mForceActiveTypeDef = NULL;
  153. mCurVarInitializer = NULL;
  154. mArrayInitializerSize = -1;
  155. mResolveKind = ResolveKind_None;
  156. }
  157. BfTypeState(BfType* type, BfTypeState* prevState = NULL)
  158. {
  159. mPrevState = prevState;
  160. mType = type;
  161. mGlobalContainerCurUserTypeDef = NULL;
  162. mPopulateType = BfPopulateType_Declaration;
  163. mCurBaseTypeRef = NULL;
  164. mCurBaseType = NULL;
  165. mCurFieldDef = NULL;
  166. mCurAttributeTypeRef = NULL;
  167. mCurTypeDef = NULL;
  168. mForceActiveTypeDef = NULL;
  169. mCurVarInitializer = NULL;
  170. mArrayInitializerSize = -1;
  171. mResolveKind = ResolveKind_None;
  172. }
  173. };
  174. class BfSavedTypeData
  175. {
  176. public:
  177. BfHotTypeData* mHotTypeData;
  178. int mTypeId;
  179. public:
  180. ~BfSavedTypeData()
  181. {
  182. delete mHotTypeData;
  183. }
  184. };
  185. struct SpecializedErrorData
  186. {
  187. // We need to store errors during type specialization and method specialization,
  188. // because if the type is deleted we need to clear the errors
  189. BfTypeInstance* mRefType;
  190. BfModule* mModule;
  191. BfMethodInstance* mMethodInstance;
  192. BfError* mError;
  193. SpecializedErrorData()
  194. {
  195. mRefType = NULL;
  196. mModule = NULL;
  197. mMethodInstance = NULL;
  198. mError = NULL;
  199. }
  200. };
  201. struct BfCaseInsensitiveStringHash
  202. {
  203. size_t operator()(const StringImpl& str) const
  204. {
  205. int curHash = 0;
  206. for (int i = 0; i < (int)str.length(); i++)
  207. curHash = ((curHash ^ (int)(intptr)toupper(str[i])) << 5) - curHash;
  208. return curHash;
  209. }
  210. };
  211. struct BfCaseInsensitiveStringEquals
  212. {
  213. bool operator()(const StringImpl& lhs, const StringImpl& rhs) const
  214. {
  215. if (lhs.length() != rhs.length())
  216. return false;
  217. return _stricmp(lhs.c_str(), rhs.c_str()) == 0;
  218. }
  219. };
  220. template <typename T>
  221. class WorkQueue : public Deque<T*>
  222. {
  223. public:
  224. BumpAllocator mWorkAlloc;
  225. int RemoveAt(int idx)
  226. {
  227. if (idx == 0)
  228. {
  229. T*& ref = (*this)[idx];
  230. if (ref != NULL)
  231. (*ref).~T();
  232. Deque<T*>::RemoveAt(0);
  233. if (this->mSize == 0)
  234. {
  235. mWorkAlloc.Clear();
  236. this->mOffset = 0;
  237. }
  238. return idx - 1;
  239. }
  240. else
  241. {
  242. T*& ref = (*this)[idx];
  243. if (ref != NULL)
  244. {
  245. (*ref).~T();
  246. ref = NULL;
  247. }
  248. return idx;
  249. }
  250. }
  251. void Clear()
  252. {
  253. Deque<T*>::Clear();
  254. mWorkAlloc.Clear();
  255. }
  256. T* Alloc()
  257. {
  258. T* item = mWorkAlloc.Alloc<T>();
  259. this->Add(item);
  260. return item;
  261. }
  262. };
  263. template <typename T>
  264. class PtrWorkQueue : public Deque<T>
  265. {
  266. public:
  267. int RemoveAt(int idx)
  268. {
  269. if (idx == 0)
  270. {
  271. Deque<T>::RemoveAt(0);
  272. return idx - 1;
  273. }
  274. else
  275. {
  276. (*this)[idx] = NULL;
  277. return idx;
  278. }
  279. }
  280. };
  281. class BfConstraintState
  282. {
  283. public:
  284. BfGenericParamInstance* mGenericParamInstance;
  285. BfType* mLeftType;
  286. BfType* mRightType;
  287. BfConstraintState* mPrevState;
  288. BfMethodInstance* mMethodInstance;
  289. BfTypeVector* mMethodGenericArgsOverride;
  290. public:
  291. BfConstraintState()
  292. {
  293. mGenericParamInstance = NULL;
  294. mLeftType = NULL;
  295. mRightType = NULL;
  296. mMethodInstance = NULL;
  297. mMethodGenericArgsOverride = NULL;
  298. mPrevState = NULL;
  299. }
  300. bool operator==(const BfConstraintState& other) const
  301. {
  302. return
  303. (mGenericParamInstance == other.mGenericParamInstance) &&
  304. (mLeftType == other.mLeftType) &&
  305. (mRightType == other.mRightType);
  306. }
  307. };
  308. class BfContext
  309. {
  310. public:
  311. CritSect mCritSect;
  312. bool mDeleting;
  313. BfTypeState* mCurTypeState;
  314. BfSizedArray<BfNamespaceDeclaration*>* mCurNamespaceNodes;
  315. BfConstraintState* mCurConstraintState;
  316. bool mResolvingVarField;
  317. int mMappedObjectRevision;
  318. bool mAssertOnPopulateType;
  319. BfSystem* mSystem;
  320. BfCompiler* mCompiler;
  321. bool mAllowLockYield;
  322. bool mLockModules;
  323. BfModule* mScratchModule;
  324. BfModule* mUnreifiedModule;
  325. HashSet<String> mUsedModuleNames;
  326. Dictionary<BfProject*, BfModule*> mProjectModule;
  327. Array<BfModule*> mModules;
  328. Array<BfModule*> mDeletingModules;
  329. HashSet<BfTypeInstance*> mFailTypes; // All types handled after a failure need to be rebuild on subsequent compile
  330. HashSet<BfTypeInstance*> mReferencedIFaceSlots;
  331. BfMethodInstance* mValueTypeDeinitSentinel;
  332. Array<BfAstNode*> mTempNodes;
  333. BfResolvedTypeSet mResolvedTypes;
  334. Array<BfType*> mTypes; // Can contain NULLs for deleted types
  335. Array<BfFieldInstance*> mFieldResolveReentrys; // For detecting 'var' field circular refs
  336. Dictionary<String, BfSavedTypeData*> mSavedTypeDataMap;
  337. Array<BfSavedTypeData*> mSavedTypeData;
  338. BfTypeInstance* mBfTypeType;
  339. BfTypeInstance* mBfObjectType;
  340. bool mCanSkipObjectCtor;
  341. bool mCanSkipValueTypeCtor;
  342. BfPointerType* mBfClassVDataPtrType;
  343. PtrWorkQueue<BfModule*> mReifyModuleWorkList;
  344. WorkQueue<BfMethodProcessRequest> mMethodWorkList;
  345. WorkQueue<BfInlineMethodRequest> mInlineMethodWorkList;
  346. WorkQueue<BfTypeProcessRequest> mPopulateTypeWorkList;
  347. WorkQueue<BfMethodSpecializationRequest> mMethodSpecializationWorkList;
  348. WorkQueue<BfTypeRefVerifyRequest> mTypeRefVerifyWorkList;
  349. PtrWorkQueue<BfModule*> mFinishedSlotAwaitModuleWorkList;
  350. PtrWorkQueue<BfModule*> mFinishedModuleWorkList;
  351. bool mHasReifiedQueuedRebuildTypes;
  352. Array<BfGenericParamType*> mGenericParamTypes[3];
  353. Array<BfType*> mTypeGraveyard;
  354. Array<BfTypeDef*> mTypeDefGraveyard;
  355. Array<BfLocalMethod*> mLocalMethodGraveyard;
  356. Dictionary<String, int> mStringObjectPool;
  357. Dictionary<int, BfStringPoolEntry> mStringObjectIdMap;
  358. int mCurStringObjectPoolId;
  359. HashSet<BfTypeInstance*> mQueuedSpecializedMethodRebuildTypes;
  360. BfAllocPool<BfPointerType> mPointerTypePool;
  361. BfAllocPool<BfArrayType> mArrayTypePool;
  362. BfAllocPool<BfSizedArrayType> mSizedArrayTypePool;
  363. BfAllocPool<BfUnknownSizedArrayType> mUnknownSizedArrayTypePool;
  364. BfAllocPool<BfBoxedType> mBoxedTypePool;
  365. BfAllocPool<BfTupleType> mTupleTypePool;
  366. BfAllocPool<BfTypeAliasType> mAliasTypePool;
  367. BfAllocPool<BfRefType> mRefTypePool;
  368. BfAllocPool<BfModifiedTypeType> mModifiedTypeTypePool;
  369. BfAllocPool<BfTypeInstance> mGenericTypeInstancePool;
  370. BfAllocPool<BfArrayType> mArrayTypeInstancePool;
  371. BfAllocPool<BfGenericParamType> mGenericParamTypePool;
  372. BfAllocPool<BfDirectTypeDefReference> mTypeDefTypeRefPool;
  373. BfAllocPool<BfConcreteInterfaceType> mConcreteInterfaceTypePool;
  374. BfAllocPool<BfConstExprValueType> mConstExprValueTypePool;
  375. BfAllocPool<BfDelegateType> mDelegateTypePool;
  376. BfPrimitiveType* mPrimitiveTypes[BfTypeCode_Length];
  377. BfPrimitiveType* mPrimitiveStructTypes[BfTypeCode_Length];
  378. public:
  379. void AssignModule(BfType* type);
  380. void HandleTypeWorkItem(BfType* type);
  381. void EnsureHotMangledVirtualMethodName(BfMethodInstance* methodInstance);
  382. void EnsureHotMangledVirtualMethodNames();
  383. void PopulateHotTypeDataVTable(BfTypeInstance* typeInstance);
  384. void DeleteType(BfType* type, bool deferDepRebuilds = false);
  385. void UpdateAfterDeletingTypes();
  386. void VerifyTypeLookups(BfTypeInstance* typeInst);
  387. void GenerateModuleName_TypeInst(BfTypeInstance* typeInst, String& name);
  388. void GenerateModuleName_Type(BfType* type, String& name);
  389. String GenerateModuleName(BfTypeInstance* typeInst);
  390. bool IsSentinelMethod(BfMethodInstance* methodInstance);
  391. void SaveDeletingType(BfType* type);
  392. BfType* FindType(const StringImpl& typeName);
  393. String TypeIdToString(int typeId);
  394. BfHotTypeData* GetHotTypeData(int typeId);
  395. void ReflectInit();
  396. public:
  397. BfContext(BfCompiler* compiler);
  398. ~BfContext();
  399. void ReportMemory(MemReporter* memReporter);
  400. void ProcessMethod(BfMethodInstance* methodInstance);
  401. int GetStringLiteralId(const StringImpl& str);
  402. void CheckLockYield();
  403. bool IsCancellingAndYield();
  404. void QueueFinishModule(BfModule * module);
  405. void CancelWorkItems();
  406. bool ProcessWorkList(bool onlyReifiedTypes, bool onlyReifiedMethods);
  407. void HandleChangedTypeDef(BfTypeDef* typeDef, bool isAutoCompleteTempType = false);
  408. void PreUpdateRevisedTypes();
  409. void UpdateRevisedTypes();
  410. void VerifyTypeLookups();
  411. void QueueMethodSpecializations(BfTypeInstance* typeInst, bool checkSpecializedMethodRebuildFlag);
  412. void MarkAsReferenced(BfDependedType* depType);
  413. void RemoveInvalidFailTypes();
  414. void RemoveInvalidWorkItems();
  415. BfType* FindTypeById(int typeId);
  416. void AddTypeToWorkList(BfType* type);
  417. void ValidateDependencies();
  418. void RebuildType(BfType* type, bool deleteOnDemandTypes = true, bool rebuildModule = true, bool placeSpecializiedInPurgatory = true);
  419. void RebuildDependentTypes(BfDependedType* dType);
  420. void RebuildDependentTypes_MidCompile(BfDependedType* dType, const String& reason);
  421. bool CanRebuild(BfType* type);
  422. void TypeDataChanged(BfDependedType* dType, bool isNonStaticDataChange);
  423. void TypeMethodSignaturesChanged(BfTypeInstance* typeInst);
  424. void TypeInlineMethodInternalsChanged(BfTypeInstance* typeInst);
  425. void TypeConstEvalChanged(BfTypeInstance* typeInst);
  426. void TypeConstEvalFieldChanged(BfTypeInstance* typeInst);
  427. void CheckSpecializedErrorData();
  428. void TryUnreifyModules();
  429. void MarkUsedModules(BfProject* project, BfModule* module);
  430. void RemapObject();
  431. void Finish();
  432. void Cleanup();
  433. };
  434. NS_BF_END