123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- #pragma once
- #include "BfModule.h"
- #include "BeefySysLib/util/Deque.h"
- NS_BF_BEGIN
- class BfWorkListEntry
- {
- public:
- BfType* mType;
- BfModule* mFromModule;
- int mRevision;
- int mSignatureRevision;
- int mFromModuleRevision;
- int mFromModuleRebuildIdx;
- int mReqId;
- static int sCurReqId;
- public:
- BfWorkListEntry()
- {
- mType = NULL;
- mFromModule = NULL;
- mRevision = -1;
- mSignatureRevision = -1;
- mFromModuleRevision = -1;
- mFromModuleRebuildIdx = -1;
- mReqId = ++sCurReqId;
- }
- };
- class BfTypeProcessRequest : public BfWorkListEntry
- {
- public:
- bool mRebuildType;
- BfTypeProcessRequest()
- {
- mRebuildType = false;
- }
- };
- class BfMethodSpecializationRequest : public BfWorkListEntry
- {
- public:
- int32 mMethodIdx;
- BfTypeVector mMethodGenericArguments;
- BfGetMethodInstanceFlags mFlags;
- BfTypeInstance* mForeignType;
- public:
- BfMethodSpecializationRequest()
- {
- mMethodIdx = -1;
- mFlags = BfGetMethodInstanceFlag_None;
- mForeignType = NULL;
- }
- void Init(BfTypeInstance* typeInstance, BfTypeInstance* foreignType, BfMethodDef* methodDef)
- {
- mType = typeInstance;
- mMethodIdx = methodDef->mIdx;
- mForeignType = foreignType;
- if (foreignType != NULL)
- BF_ASSERT(foreignType->mTypeDef->mMethods[mMethodIdx] == methodDef);
- else
- BF_ASSERT(typeInstance->mTypeDef->mMethods[mMethodIdx] == methodDef);
- }
- };
- class BfMethodProcessRequest : public BfWorkListEntry
- {
- public:
- BfMethodInstance* mMethodInstance;
- BfMethodProcessRequest()
- {
- mMethodInstance = NULL;
- }
- ~BfMethodProcessRequest()
- {
- Disable();
- }
- void Disable()
- {
- if (mMethodInstance != NULL)
- {
- BF_ASSERT(mMethodInstance->mMethodProcessRequest == this);
- mMethodInstance->mMethodProcessRequest = NULL;
- mMethodInstance = NULL;
- }
- }
- };
- class BfInlineMethodRequest : public BfMethodProcessRequest
- {
- public:
- BfIRFunction mFunc;
- ~BfInlineMethodRequest()
- {
- mMethodInstance = NULL;
- }
- };
- class BfTypeRefVerifyRequest : public BfWorkListEntry
- {
- public:
- BfTypeInstance* mCurTypeInstance;
- BfAstNode* mRefNode;
- };
- class BfMidCompileRequest : public BfWorkListEntry
- {
- public:
- String mReason;
- };
- struct BfStringPoolEntry
- {
- String mString;
- int mLastUsedRevision;
- int mFirstUsedRevision;
- };
- class BfGlobalContainerEntry
- {
- public:
- BfTypeDef* mTypeDef;
- BfTypeInstance* mTypeInst;
- };
- class BfTypeState
- {
- public:
- enum ResolveKind
- {
- ResolveKind_None,
- ResolveKind_BuildingGenericParams,
- ResolveKind_ResolvingVarType,
- ResolveKind_UnionInnerType,
- ResolveKind_LocalVariable,
- ResolveKind_Attributes,
- ResolveKind_FieldType,
- ResolveKind_ConstField
- };
- public:
- BfTypeState* mPrevState;
- BfType* mType;
- BfTypeDef* mGlobalContainerCurUserTypeDef;
- Array<BfGlobalContainerEntry> mGlobalContainers; // All global containers that are visible
- BfPopulateType mPopulateType;
- BfTypeReference* mCurBaseTypeRef;
- BfTypeInstance* mCurBaseType;
- BfTypeReference* mCurAttributeTypeRef;
- BfFieldDef* mCurFieldDef;
- BfMethodDef* mCurMethodDef;
- BfTypeDef* mCurTypeDef;
- BfTypeDef* mForceActiveTypeDef;
- BfProject* mActiveProject;
- ResolveKind mResolveKind;
- BfAstNode* mCurVarInitializer;
- int mArrayInitializerSize;
- BfTypeInstance* mInitializerBaseType;
- public:
- BfTypeState()
- {
- mPrevState = NULL;
- mType = NULL;
- mGlobalContainerCurUserTypeDef = NULL;
- mPopulateType = BfPopulateType_Identity;
- mCurBaseTypeRef = NULL;
- mCurBaseType = NULL;
- mCurFieldDef = NULL;
- mCurMethodDef = NULL;
- mCurAttributeTypeRef = NULL;
- mCurTypeDef = NULL;
- mForceActiveTypeDef = NULL;
- mActiveProject = NULL;
- mCurVarInitializer = NULL;
- mArrayInitializerSize = -1;
- mResolveKind = ResolveKind_None;
- mInitializerBaseType = NULL;
- }
- BfTypeState(BfType* type, BfTypeState* prevState = NULL)
- {
- mPrevState = prevState;
- mType = type;
- mGlobalContainerCurUserTypeDef = NULL;
- mPopulateType = BfPopulateType_Declaration;
- mCurBaseTypeRef = NULL;
- mCurBaseType = NULL;
- mCurFieldDef = NULL;
- mCurMethodDef = NULL;
- mCurAttributeTypeRef = NULL;
- mCurTypeDef = NULL;
- mForceActiveTypeDef = NULL;
- mActiveProject = NULL;
- mCurVarInitializer = NULL;
- mArrayInitializerSize = -1;
- mResolveKind = ResolveKind_None;
- mInitializerBaseType = NULL;
- }
- };
- class BfSavedTypeData
- {
- public:
- BfHotTypeData* mHotTypeData;
- int mTypeId;
- public:
- ~BfSavedTypeData()
- {
- delete mHotTypeData;
- }
- };
- struct SpecializedErrorData
- {
- // We need to store errors during type specialization and method specialization,
- // because if the type is deleted we need to clear the errors
- BfTypeInstance* mRefType;
- BfModule* mModule;
- BfMethodInstance* mMethodInstance;
- BfError* mError;
- SpecializedErrorData()
- {
- mRefType = NULL;
- mModule = NULL;
- mMethodInstance = NULL;
- mError = NULL;
- }
- };
- struct BfCaseInsensitiveStringHash
- {
- size_t operator()(const StringImpl& str) const
- {
- int curHash = 0;
- for (int i = 0; i < (int)str.length(); i++)
- curHash = ((curHash ^ (int)(intptr)toupper(str[i])) << 5) - curHash;
- return curHash;
- }
- };
- struct BfCaseInsensitiveStringEquals
- {
- bool operator()(const StringImpl& lhs, const StringImpl& rhs) const
- {
- if (lhs.length() != rhs.length())
- return false;
- return _stricmp(lhs.c_str(), rhs.c_str()) == 0;
- }
- };
- template <typename T>
- class WorkQueue : public Deque<T*>
- {
- public:
- BumpAllocator mWorkAlloc;
- int RemoveAt(int idx)
- {
- if (idx == 0)
- {
- T*& ref = (*this)[idx];
- if (ref != NULL)
- (*ref).~T();
- Deque<T*>::RemoveAt(0);
- if (this->mSize == 0)
- {
- mWorkAlloc.Clear();
- this->mOffset = 0;
- }
- return idx - 1;
- }
- else
- {
- T*& ref = (*this)[idx];
- if (ref != NULL)
- {
- (*ref).~T();
- ref = NULL;
- }
- return idx;
- }
- }
- void Clear()
- {
- Deque<T*>::Clear();
- mWorkAlloc.Clear();
- }
- T* Alloc()
- {
- T* item = mWorkAlloc.Alloc<T>();
- this->Add(item);
- return item;
- }
- };
- template <typename T>
- class PtrWorkQueue : public Deque<T>
- {
- public:
- int RemoveAt(int idx)
- {
- if (idx == 0)
- {
- Deque<T>::RemoveAt(0);
- return idx - 1;
- }
- else
- {
- (*this)[idx] = NULL;
- return idx;
- }
- }
- };
- class BfConstraintState
- {
- public:
- BfGenericParamInstance* mGenericParamInstance;
- BfType* mLeftType;
- BfType* mRightType;
- BfConstraintState* mPrevState;
- BfMethodInstance* mMethodInstance;
- BfTypeVector* mMethodGenericArgsOverride;
- public:
- BfConstraintState()
- {
- mGenericParamInstance = NULL;
- mLeftType = NULL;
- mRightType = NULL;
- mMethodInstance = NULL;
- mMethodGenericArgsOverride = NULL;
- mPrevState = NULL;
- }
- bool operator==(const BfConstraintState& other) const
- {
- return
- (mGenericParamInstance == other.mGenericParamInstance) &&
- (mLeftType == other.mLeftType) &&
- (mRightType == other.mRightType);
- }
- };
- enum BfFailKind
- {
- BfFailKind_Normal,
- BfFailKind_Deep
- };
- class BfContext
- {
- public:
- CritSect mCritSect;
- bool mDeleting;
- BfTypeState* mCurTypeState;
- BfSizedArray<BfNamespaceDeclaration*>* mCurNamespaceNodes;
- BfConstraintState* mCurConstraintState;
- bool mResolvingVarField;
- int mMappedObjectRevision;
- bool mAssertOnPopulateType;
- BfSystem* mSystem;
- BfCompiler* mCompiler;
- bool mAllowLockYield;
- bool mLockModules;
- BfModule* mScratchModule;
- BfModule* mUnreifiedModule;
- HashSet<String> mUsedModuleNames;
- HashSet<BfType*> mGhostDependencies; // We couldn't properly rebuild our dependencies
- Dictionary<BfProject*, BfModule*> mProjectModule;
- Array<BfModule*> mModules;
- Array<BfModule*> mDeletingModules;
- Dictionary<BfTypeInstance*, BfFailKind> mFailTypes; // All types handled after a failure need to be rebuild on subsequent compile
- HashSet<BfTypeInstance*> mReferencedIFaceSlots;
- BfMethodInstance* mValueTypeDeinitSentinel;
- Array<BfAstNode*> mTempNodes;
- BfResolvedTypeSet mResolvedTypes;
- Array<BfType*> mTypes; // Can contain NULLs for deleted types
- Array<BfFieldInstance*> mFieldResolveReentrys; // For detecting 'var' field circular refs
- Dictionary<String, BfSavedTypeData*> mSavedTypeDataMap;
- Array<BfSavedTypeData*> mSavedTypeData;
- BfTypeInstance* mBfTypeType;
- BfTypeInstance* mBfObjectType;
- bool mCanSkipObjectCtor;
- bool mCanSkipValueTypeCtor;
- BfPointerType* mBfClassVDataPtrType;
- PtrWorkQueue<BfModule*> mReifyModuleWorkList;
- WorkQueue<BfMethodProcessRequest> mMethodWorkList;
- WorkQueue<BfInlineMethodRequest> mInlineMethodWorkList;
- WorkQueue<BfTypeProcessRequest> mPopulateTypeWorkList;
- WorkQueue<BfMethodSpecializationRequest> mMethodSpecializationWorkList;
- WorkQueue<BfTypeRefVerifyRequest> mTypeRefVerifyWorkList;
- WorkQueue<BfMidCompileRequest> mMidCompileWorkList;
- PtrWorkQueue<BfModule*> mFinishedSlotAwaitModuleWorkList;
- PtrWorkQueue<BfModule*> mFinishedModuleWorkList;
- bool mHasReifiedQueuedRebuildTypes;
- Array<BfGenericParamType*> mGenericParamTypes[3];
- Array<BfType*> mTypeGraveyard;
- Array<BfTypeDef*> mTypeDefGraveyard;
- Array<BfLocalMethod*> mLocalMethodGraveyard;
- Dictionary<String, int> mStringObjectPool;
- Dictionary<int, BfStringPoolEntry> mStringObjectIdMap;
- int mCurStringObjectPoolId;
- HashSet<BfTypeInstance*> mQueuedSpecializedMethodRebuildTypes;
- BfAllocPool<BfPointerType> mPointerTypePool;
- BfAllocPool<BfArrayType> mArrayTypePool;
- BfAllocPool<BfSizedArrayType> mSizedArrayTypePool;
- BfAllocPool<BfUnknownSizedArrayType> mUnknownSizedArrayTypePool;
- BfAllocPool<BfBoxedType> mBoxedTypePool;
- BfAllocPool<BfTupleType> mTupleTypePool;
- BfAllocPool<BfTypeAliasType> mAliasTypePool;
- BfAllocPool<BfRefType> mRefTypePool;
- BfAllocPool<BfModifiedTypeType> mModifiedTypeTypePool;
- BfAllocPool<BfTypeInstance> mGenericTypeInstancePool;
- BfAllocPool<BfArrayType> mArrayTypeInstancePool;
- BfAllocPool<BfGenericParamType> mGenericParamTypePool;
- BfAllocPool<BfDirectTypeDefReference> mTypeDefTypeRefPool;
- BfAllocPool<BfConcreteInterfaceType> mConcreteInterfaceTypePool;
- BfAllocPool<BfConstExprValueType> mConstExprValueTypePool;
- BfAllocPool<BfDelegateType> mDelegateTypePool;
- BfPrimitiveType* mPrimitiveTypes[BfTypeCode_Length];
- BfPrimitiveType* mPrimitiveStructTypes[BfTypeCode_Length];
- public:
- void AssignModule(BfType* type);
- void HandleTypeWorkItem(BfType* type);
- void EnsureHotMangledVirtualMethodName(BfMethodInstance* methodInstance);
- void EnsureHotMangledVirtualMethodNames();
- void PopulateHotTypeDataVTable(BfTypeInstance* typeInstance);
- void DeleteType(BfType* type, bool deferDepRebuilds = false);
- void UpdateAfterDeletingTypes();
- void VerifyTypeLookups(BfTypeInstance* typeInst);
- void GenerateModuleName_TypeInst(BfTypeInstance* typeInst, StringImpl& name);
- void GenerateModuleName_Type(BfType* type, StringImpl& name);
- void GenerateModuleName(BfTypeInstance* typeInst, StringImpl& name);
- bool IsSentinelMethod(BfMethodInstance* methodInstance);
- void SaveDeletingType(BfType* type);
- BfType* FindType(const StringImpl& typeName);
- String TypeIdToString(int typeId);
- BfHotTypeData* GetHotTypeData(int typeId);
- void ReflectInit();
- public:
- BfContext(BfCompiler* compiler);
- ~BfContext();
- void ReportMemory(MemReporter* memReporter);
- void ProcessMethod(BfMethodInstance* methodInstance);
- int GetStringLiteralId(const StringImpl& str);
- void CheckLockYield();
- bool IsCancellingAndYield();
- void QueueFinishModule(BfModule * module);
- void CancelWorkItems();
- bool ProcessWorkList(bool onlyReifiedTypes, bool onlyReifiedMethods);
- void HandleChangedTypeDef(BfTypeDef* typeDef, bool isAutoCompleteTempType = false);
- void PreUpdateRevisedTypes();
- void UpdateRevisedTypes();
- void VerifyTypeLookups();
- void QueueMethodSpecializations(BfTypeInstance* typeInst, bool checkSpecializedMethodRebuildFlag);
- void MarkAsReferenced(BfDependedType* depType);
- void RemoveInvalidFailTypes();
- bool IsWorkItemValid(BfWorkListEntry* item);
- bool IsWorkItemValid(BfMethodInstance* methodInstance);
- bool IsWorkItemValid(BfMethodProcessRequest* item);
- bool IsWorkItemValid(BfInlineMethodRequest* item);
- bool IsWorkItemValid(BfMethodSpecializationRequest* item);
- void RemoveInvalidWorkItems();
- BfType* FindTypeById(int typeId);
- void AddTypeToWorkList(BfType* type);
- void ValidateDependencies();
- void RebuildType(BfType* type, bool deleteOnDemandTypes = true, bool rebuildModule = true, bool placeSpecializiedInPurgatory = true);
- void RebuildDependentTypes(BfDependedType* dType);
- void QueueMidCompileRebuildDependentTypes(BfDependedType* dType, const String& reason);
- void RebuildDependentTypes_MidCompile(BfDependedType* dType, const String& reason);
- bool IsRebuilding(BfType* type);
- bool CanRebuild(BfType* type);
- void TypeDataChanged(BfDependedType* dType, bool isNonStaticDataChange);
- void TypeMethodSignaturesChanged(BfTypeInstance* typeInst);
- void TypeInlineMethodInternalsChanged(BfTypeInstance* typeInst);
- void TypeConstEvalChanged(BfTypeInstance* typeInst);
- void TypeConstEvalFieldChanged(BfTypeInstance* typeInst);
- void CheckSpecializedErrorData();
- void TryUnreifyModules();
- void MarkUsedModules(BfProject* project, BfModule* module);
- void RemapObject();
- void Finish();
- void Cleanup();
- };
- NS_BF_END
|