[project @ 2000-10-12 13:44:59 by simonpj]
[ghc-hetmet.git] / ghc / compiler / main / HscTypes.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[HscTypes]{Types for the per-module compiler}
5
6 \begin{code}
7 module HscTypes  ( )
8 where
9
10 #include "HsVersions.h"
11
12 \end{code}
13
14 %************************************************************************
15 %*                                                                      *
16 \subsection{Symbol tables and Module details}
17 %*                                                                      *
18 %************************************************************************
19
20 A @ModDetails@ summarises everything we know about a compiled module
21
22 \begin{code}
23 data ModDetails
24    = ModDetails {
25         moduleExports :: Avails,                -- What it exports
26         moduleEnv     :: GlobalRdrEnv,          -- Its top level environment
27
28         fixityEnv     :: NameEnv Fixity,
29         deprecEnv     :: NameEnv DeprecTxt,
30         typeEnv       :: NameEnv TyThing,       -- TyThing is in TcEnv.lhs
31
32         instEnv       :: InstEnv,
33         ruleEnv       :: IdEnv [CoreRule]       -- Domain includes Ids from other modules
34      }
35 \end{code}
36
37 Symbol tables map modules to ModDetails:
38
39 \begin{code}
40 type SymbolTable        = ModuleEnv ModDetails
41 type HomeSymbolTable    = SymbolTable   -- Domain = modules in the home package
42 type PackageSymbolTable = SymbolTable   -- Domain = modules in the some other package
43 type GlobalSymbolTable  = SymbolTable   -- Domain = all modules
44 \end{code}
45
46
47 Simple lookups in the symbol table
48
49 \begin{code}
50 lookupFixityEnv :: SymbolTable -> Name -> Fixity
51         -- Returns defaultFixity if there isn't an explicit fixity
52 lookupFixityEnv tbl name
53   = case lookupModuleEnv tbl (nameModule name) of
54         Nothing      -> defaultFixity
55         Just details -> case lookupNameEnv (fixityEnv details) name of
56                                 Just fixity -> fixity
57                                 Nothing     -> defaultFixity
58
59 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
60 lookupTypeEnv tbl name
61   = case lookupModuleEnv tbl (nameModule name) of
62         Just details -> lookupNameEnv (typeEnv details) name
63         Nothing      -> Nothing
64 \end{code}
65
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection{Auxiliary types}
70 %*                                                                      *
71 %************************************************************************
72
73 These types are defined here because they are mentioned in ModDetails,
74 but they are mostly elaborated elsewhere
75
76 \begin{code}
77 data TyThing = AnId   Id
78              | ATyCon TyCon
79              | AClass Class
80
81 type DeprecationEnv = NameEnv DeprecTxt         -- Give reason for deprecation
82
83 type GlobalRdrEnv = RdrNameEnv [Name]   -- The list is because there may be name clashes
84                                         -- These only get reported on lookup,
85                                         -- not on construction
86
87 type InstEnv    = UniqFM ClsInstEnv             -- Maps Class to instances for that class
88 type ClsInstEnv = [(TyVarSet, [Type], Id)]      -- The instances for a particular class
89 \end{code}
90
91
92 \begin{code}
93 type Avails       = [AvailInfo]
94 type AvailInfo    = GenAvailInfo Name
95 type RdrAvailInfo = GenAvailInfo OccName
96
97 data GenAvailInfo name  = Avail name     -- An ordinary identifier
98                         | AvailTC name   -- The name of the type or class
99                                   [name] -- The available pieces of type/class.
100                                          -- NB: If the type or class is itself
101                                          -- to be in scope, it must be in this list.
102                                          -- Thus, typically: AvailTC Eq [Eq, ==, /=]
103                         deriving( Eq )
104                         -- Equality used when deciding if the interface has changed
105
106 type AvailEnv     = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contains it
107 \end{code}
108
109
110 %************************************************************************
111 %*                                                                      *
112 \subsection{ModIface}
113 %*                                                                      *
114 %************************************************************************
115
116 \begin{code}
117 -- ModIFace is nearly the same as RnMonad.ParsedIface.
118 -- Right now it's identical :)
119 data ModIFace 
120    = ModIFace {
121         mi_mod       :: Module,                   -- Complete with package info
122         mi_vers      :: Version,                  -- Module version number
123         mi_orphan    :: WhetherHasOrphans,        -- Whether this module has orphans
124         mi_usages    :: [ImportVersion OccName],  -- Usages
125         mi_exports   :: [ExportItem],             -- Exports
126         mi_insts     :: [RdrNameInstDecl],        -- Local instance declarations
127         mi_decls     :: [(Version, RdrNameHsDecl)],    -- Local definitions
128         mi_fixity    :: (Version, [RdrNameFixitySig]), -- Local fixity declarations, 
129                                                        -- with their version
130         mi_rules     :: (Version, [RdrNameRuleDecl]),  -- Rules, with their version
131         mi_deprecs   :: [RdrNameDeprecation]           -- Deprecations
132      }
133 \end{code}
134
135
136 %************************************************************************
137 %*                                                                      *
138 \subsection{The persistent compiler state}
139 %*                                                                      *
140 %************************************************************************
141
142 \begin{code}
143 data PersistentCompilerState 
144    = PCS {
145         pcsPST :: PackageSymbolTable,           -- Domain = non-home-package modules
146         pcsPRS :: PersistentRenamerState
147      }
148 \end{code}
149
150 The @PersistentRenamerState@ persists across successive calls to the
151 compiler.
152
153 It contains:
154   * a name supply, which deals with allocating unique names to
155     (Module,OccName) original names, 
156  
157   * a "holding pen" for declarations that have been read out of
158     interface files but not yet sucked in, renamed, and typechecked
159
160 \begin{code}
161 data PersistentRenamerState
162   = PRS { prsNS    :: NameSupply,
163           prsDecls :: DeclsMap,
164           prsInsts :: IfaceInsts,
165           prsRules :: IfaceRules,
166     }
167
168 data NameSupply
169  = NS { nsUniqs  :: UniqSupply,
170         nsNames  :: FiniteMap (Module,OccName) Name     -- Ensures that one original name gets one unique
171         nsIParam :: FiniteMap OccName Name              -- Ensures that one implicit parameter name gets one unique
172    }
173
174 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
175                 -- A DeclsMap contains a binding for each Name in the declaration
176                 -- including the constructors of a type decl etc.
177                 -- The Bool is True just for the 'main' Name.
178
179 type IfaceInsts = Bag GatedDecl
180 type IfaceRules = Bag GatedDecl
181
182 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
183 \end{code}
184
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection{The result of compiling one module}
189 %*                                                                      *
190 %************************************************************************
191
192 \begin{code}
193 data CompResult
194    = CompOK   ModDetails  -- new details (HST additions)
195               (Maybe (ModIFace, Linkable))
196                        -- summary and code; Nothing => compilation not reqd
197                        -- (old summary and code are still valid)
198               PersistentCompilerState   -- updated PCS
199               (Bag WarnMsg)             -- warnings
200
201    | CompErrs PersistentCompilerState   -- updated PCS
202               (Bag ErrMsg)              -- errors
203               (Bag WarnMsg)             -- warnings
204
205
206 -- The driver sits between 'compile' and 'hscMain', translating calls
207 -- to the former into calls to the latter, and results from the latter
208 -- into results from the former.  It does things like preprocessing
209 -- the .hs file if necessary, and compiling up the .stub_c files to
210 -- generate Linkables.
211
212 data HscResult
213    = HscOK   ModDetails                 -- new details (HomeSymbolTable additions)
214              Maybe ModIFace             -- new iface (if any compilation was done)
215              Maybe String               -- generated stub_h
216              Maybe String               -- generated stub_c
217              PersistentCompilerState    -- updated PCS
218              [SDoc]                     -- warnings
219
220    | HscErrs PersistentCompilerState    -- updated PCS
221              [SDoc]                     -- errors
222              [SDoc]                     -- warnings
223
224         
225 -- These two are only here to avoid recursion between CmCompile and
226 -- CompManager.  They really ought to be in the latter.
227 type ModuleEnv a = UniqFM a   -- Domain is Module
228
229 type HomeModMap         = FiniteMap ModuleName Module -- domain: home mods only
230 type HomeInterfaceTable = ModuleEnv ModIFace
231 \end{code}
232
233