[project @ 2000-10-11 15:04:47 by simonpj]
[ghc-hetmet.git] / ghc / compiler / ghci / CmCompile.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[CmCompile]{Compiler for GHCI}
5
6 \begin{code}
7 module CmCompile ( cmCompile,
8                    ModDetails,         -- abstract
9                    ModIFace,           -- abstract
10                    PersistentCompilerState, emptyPCS,    -- abstract
11                    HomeSymbolTable,    -- not abstract (CM needs to see it)
12                    HomeInterfaceTable, -- ditto
13                    CompResult(..)
14                  )
15 where
16
17 #include "HsVersions.h"
18
19 import CmLink           ( Linkable(..) )
20 import Outputable       ( SDoc )
21 import CmFind           ( Finder )
22 import CmSummarise      ( ModSummary, name_of_summary )
23 import FiniteMap        ( FiniteMap, emptyFM )
24
25 import Module           ( Module )
26 import RnMonad          ( Avails, GlobalRdrEnv, DeclsMap, 
27                           WhetherHasOrphans, ImportVersion,
28                           IfaceInsts, IfaceRules, ExportItem )
29 import TcEnv            ( TyThing, InstEnv )
30 import Name             ( Name, OccName )
31 import BasicTypes       ( Fixity, Version )
32 import Id               ( Id )
33 import CoreSyn          ( CoreRule )
34 import RdrHsSyn         ( RdrNameDeprecation, RdrNameRuleDecl, RdrNameFixitySig,
35                           RdrNameHsDecl, RdrNameInstDecl )
36
37
38 \end{code}
39
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{The main compiler interface}
44 %*                                                                      *
45 %************************************************************************
46
47
48 \begin{code}
49 cmCompile :: Finder                  -- to find modules
50           -> ModSummary              -- summary, including source
51           -> Maybe ModIFace          -- old interface, if available
52           -> HomeModMap              -- ModuleName -> Module
53           -> HomeSymbolTable         -- for home module ModDetails          
54           -> PersistentCompilerState -- IN: persistent compiler state
55           -> IO CompResult
56
57 cmCompile finder summary old_iface hst pcs
58    = do putStrLn ("cmCompile: compiling " ++ name_of_summary summary)
59         return (CompOK (error "cmCompile:modDetails")
60                        (Just (error "cmCompile:modIFace", 
61                               --error "cmCompile:Linkable"
62                               --LM (name_of_summary summary) [] 
63                               LM (name_of_summary summary) []
64                               ))
65                        pcs
66                        []
67                )
68
69 data CompResult
70    = CompOK   ModDetails  -- new details (HST additions)
71               (Maybe (ModIFace, Linkable))
72                        -- summary and code; Nothing => compilation not reqd
73                        -- (old summary and code are still valid)
74               PersistentCompilerState -- updated PCS
75               [SDoc]                  -- warnings
76
77    | CompErrs PersistentCompilerState -- updated PCS
78               [SDoc]                  -- errors
79               [SDoc]                  -- warnings
80
81
82 -- These two are only here to avoid recursion between CmCompile and
83 -- CompManager.  They really ought to be in the latter.
84 type ModuleEnv a = UniqFM a   -- Domain is Module
85
86 type HomeModMap         = FiniteMap ModuleName Module -- domain: home mods only
87 type HomeSymbolTable    = ModuleEnv ModDetails        -- ditto
88 type HomeInterfaceTable = ModuleEnv ModIFace
89 \end{code}
90
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection{Module details}
95 %*                                                                      *
96 %************************************************************************
97
98 A @ModDetails@ summarises everything we know about a compiled module
99
100 \begin{code}
101 data ModDetails
102    = ModDetails {
103         moduleExports :: Avails,                -- What it exports
104         moduleEnv     :: GlobalRdrEnv,          -- Its top level environment
105
106         fixityEnv     :: NameEnv Fixity,
107         deprecEnv     :: NameEnv DeprecTxt,
108         typeEnv       :: NameEnv TyThing,       -- TyThing is in TcEnv.lhs
109
110         instEnv       :: InstEnv,
111         ruleEnv       :: IdEnv [CoreRule]       -- Domain includes Ids from other modules
112      }
113 \end{code}
114
115 Auxiliary definitions
116
117 \begin{code}
118 type DeprecationEnv = NameEnv DeprecTxt         -- Give reason for deprecation
119
120 type GlobalRdrEnv = RdrNameEnv [Name]   -- The list is because there may be name clashes
121                                         -- These only get reported on lookup,
122                                         -- not on construction
123
124 data GenAvailInfo name  = Avail name     -- An ordinary identifier
125                         | AvailTC name   -- The name of the type or class
126                                   [name] -- The available pieces of type/class.
127                                          -- NB: If the type or class is itself
128                                          -- to be in scope, it must be in this list.
129                                          -- Thus, typically: AvailTC Eq [Eq, ==, /=]
130                         deriving( Eq )
131                         -- Equality used when deciding if the interface has changed
132
133 type AvailEnv     = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contains it
134 type AvailInfo    = GenAvailInfo Name
135 type RdrAvailInfo = GenAvailInfo OccName
136 type Avails       = [AvailInfo]
137 \end{code}
138
139
140 %************************************************************************
141 %*                                                                      *
142 \subsection{The persistent compiler state}
143 %*                                                                      *
144 %************************************************************************
145
146 \begin{code}
147 data PersistentCompilerState 
148    = PCS {
149         pcsPST    :: PackageSymbolTable,        -- Domain = non-home-package modules
150         pcsHP     :: HoldingPen,                -- Pre-slurped interface bits and pieces
151         pcsNS     :: NameSupply                 -- Allocate uniques for names
152      }
153
154 type PackageSymbolTable = ModuleEnv ModDetails
155
156 data NameSupply
157  = NS { nsUniqs  :: UniqSupply,
158         nsNames  :: FiniteMap (Module,OccName) Name     -- Ensures that one original name gets one unique
159         nsIParam :: FiniteMap OccName Name              -- Ensures that one implicit parameter name gets one unique
160    }
161 \end{code}
162
163
164 %************************************************************************
165 %*                                                                      *
166 \subsection{ModIface}
167 %*                                                                      *
168 %************************************************************************
169
170 \begin{code}
171 -- ModIFace is nearly the same as RnMonad.ParsedIface.
172 -- Right now it's identical :)
173 data ModIFace 
174    = ModIFace {
175         mi_mod       :: Module,                   -- Complete with package info
176         mi_vers      :: Version,                  -- Module version number
177         mi_orphan    :: WhetherHasOrphans,        -- Whether this module has orphans
178         mi_usages    :: [ImportVersion OccName],  -- Usages
179         mi_exports   :: [ExportItem],             -- Exports
180         mi_insts     :: [RdrNameInstDecl],        -- Local instance declarations
181         mi_decls     :: [(Version, RdrNameHsDecl)],    -- Local definitions
182         mi_fixity    :: (Version, [RdrNameFixitySig]), -- Local fixity declarations, 
183                                                        -- with their version
184         mi_rules     :: (Version, [RdrNameRuleDecl]),  -- Rules, with their version
185         mi_deprecs   :: [RdrNameDeprecation]           -- Deprecations
186      }
187
188 \end{code}