diff options
Diffstat (limited to 'src/Language/Fiddle/Compiler/Qualification.hs')
-rw-r--r-- | src/Language/Fiddle/Compiler/Qualification.hs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/Language/Fiddle/Compiler/Qualification.hs b/src/Language/Fiddle/Compiler/Qualification.hs new file mode 100644 index 0000000..146fd61 --- /dev/null +++ b/src/Language/Fiddle/Compiler/Qualification.hs @@ -0,0 +1,88 @@ +-- | Qualification compilation phase. +-- +-- The qualification phase is responsible for resolving all type references in +-- the AST to their fully-qualified counterparts. This process involves +-- replacing unqualified references with their fully-qualified names and +-- attaching the necessary metadata to each reference. This enriched information +-- is then available for use in later stages of the compilation pipeline. +-- +-- In this phase, symbol resolution statements (such as 'using' statements) are +-- removed, as they become unnecessary once references are fully qualified. +-- Additionally, package structures are flattened, and package declarations are +-- discarded since full qualification renders them redundant. +module Language.Fiddle.Compiler.Qualification (qualificationPhase) where + +import Control.Monad.Identity +import Data.Word +import Language.Fiddle.Ast +import Language.Fiddle.Compiler +import Language.Fiddle.Compiler.ConsistencyCheck +import Language.Fiddle.Internal.Scopes +import Language.Fiddle.Internal.UnitInterface +import Language.Fiddle.Types + +type CurrentStage = Expanded + +data GlobalState = GlobalState + { globalScope :: Scope String (Either SizeBits SizeBytes), + fileDependencies :: [FilePath], + unitInterface :: UnitInterface + } + +newtype LocalState = LocalState (ScopePath String) + +type I = Identity + +type Annot = Commented SourceSpan + +type SizeBits = Word32 + +type SizeBytes = Word32 + +instance CompilationStage Expanded where + type StageAfter Expanded = Qualified + type StageMonad Expanded = Compile GlobalState + type StageState Expanded = LocalState + type StageFunctor Expanded = Identity + type StageAnnotation Expanded = Commented SourceSpan + +qualificationPhase :: CompilationPhase Expanded Qualified +qualificationPhase = + pureCompilationPhase $ + fmap snd + . subCompile (GlobalState mempty mempty mempty) + . advanceStage (LocalState mempty) + +deriving instance AdvanceStage CurrentStage ObjTypeBody + +deriving instance AdvanceStage CurrentStage DeferredRegisterBody + +deriving instance AdvanceStage CurrentStage RegisterBody + +deriving instance AdvanceStage CurrentStage AnonymousBitsType + +deriving instance AdvanceStage CurrentStage ImportStatement + +deriving instance AdvanceStage CurrentStage BitType + +deriving instance AdvanceStage CurrentStage EnumBody + +deriving instance AdvanceStage CurrentStage EnumConstantDecl + +deriving instance AdvanceStage CurrentStage RegisterBitsDecl + +deriving instance AdvanceStage CurrentStage PackageBody + +deriving instance AdvanceStage CurrentStage ObjTypeDecl + +deriving instance AdvanceStage CurrentStage FiddleDecl + +deriving instance AdvanceStage CurrentStage FiddleUnit + +deriving instance AdvanceStage CurrentStage Expression + +deriving instance AdvanceStage CurrentStage RegisterBitsTypeRef + +deriving instance AdvanceStage CurrentStage ObjType + +deriving instance (AdvanceStage CurrentStage t) => AdvanceStage CurrentStage (Directed t) |