2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcModule]{Typechecking a whole module}
12 #include "HsVersions.h"
14 import CmdLineOpts ( DynFlag(..), DynFlags, opt_PprStyle_Debug )
15 import HsSyn ( HsBinds(..), MonoBinds(..), HsDecl(..) )
16 import HsTypes ( toHsType )
17 import RnHsSyn ( RenamedHsDecl )
18 import TcHsSyn ( TypecheckedMonoBinds,
19 TypecheckedForeignDecl, TypecheckedRuleDecl,
20 zonkTopBinds, zonkForeignExports, zonkRules
24 import Inst ( plusLIE )
25 import TcBinds ( tcTopBinds )
26 import TcClassDcl ( tcClassDecls2, mkImplicitClassBinds )
27 import TcDefaults ( tcDefaults )
28 import TcEnv ( TcEnv, InstInfo(iDFunId), tcExtendGlobalValEnv,
29 tcEnvTyCons, tcEnvClasses, isLocalThing,
30 tcSetEnv, tcSetInstEnv, initTcEnv, getTcGEnv
32 import TcRules ( tcRules )
33 import TcForeign ( tcForeignImports, tcForeignExports )
34 import TcIfaceSig ( tcInterfaceSigs )
35 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
36 import TcSimplify ( tcSimplifyTop )
37 import TcTyClsDecls ( tcTyAndClassDecls )
38 import TcTyDecls ( mkImplicitDataBinds )
40 import CoreUnfold ( unfoldingTemplate )
41 import Type ( funResultTy, splitForAllTys )
42 import Bag ( isEmptyBag )
43 import ErrUtils ( printErrorsAndWarnings, dumpIfSet_dyn )
44 import Id ( idType, idName, idUnfolding )
45 import Module ( Module )
46 import Name ( Name, nameOccName, isLocallyDefined, isGlobalName,
47 toRdrName, nameEnvElts, lookupNameEnv,
49 import TyCon ( tyConGenInfo, isClassTyCon )
50 import OccName ( isSysOcc )
51 import Maybes ( thenMaybe )
53 import BasicTypes ( EP(..), Fixity )
54 import Bag ( isEmptyBag )
56 import HscTypes ( PersistentCompilerState(..), HomeSymbolTable, HomeIfaceTable,
57 PackageTypeEnv, DFunId, ModIface(..),
58 TypeEnv, extendTypeEnvList, lookupTable,
59 TyThing(..), mkTypeEnv )
60 import List ( partition )
63 Outside-world interface:
66 -- Convenient type synonyms first:
69 tc_pcs :: PersistentCompilerState, -- Augmented with imported information,
70 -- (but not stuff from this module)
72 -- All these fields have info *just for this module*
73 tc_env :: TypeEnv, -- The top level TypeEnv
74 tc_insts :: [DFunId], -- Instances
75 tc_binds :: TypecheckedMonoBinds, -- Bindings
76 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
77 tc_rules :: [TypecheckedRuleDecl] -- Transformation rules
84 -> PersistentCompilerState
85 -> HomeSymbolTable -> HomeIfaceTable
87 -> IO (Maybe TcResults)
89 typecheckModule dflags this_mod pcs hst hit decls
90 = do env <- initTcEnv hst (pcs_PTE pcs)
92 (maybe_result, (warns,errs)) <- initTc dflags env tc_module
94 let { maybe_tc_result :: Maybe TcResults ;
95 maybe_tc_result = case maybe_result of
97 Just (_,r) -> Just r }
99 printErrorsAndWarnings (errs,warns)
100 printTcDump dflags maybe_tc_result
102 if isEmptyBag errs then
103 return maybe_tc_result
107 tc_module :: TcM (TcEnv, TcResults)
108 tc_module = fixTc (\ ~(unf_env ,_) -> tcModule pcs hst get_fixity this_mod decls unf_env)
112 get_fixity :: Name -> Maybe Fixity
113 get_fixity nm = lookupTable hit pit nm `thenMaybe` \ iface ->
114 lookupNameEnv (mi_fixities iface) nm
117 The internal monster:
119 tcModule :: PersistentCompilerState
121 -> (Name -> Maybe Fixity)
124 -> TcEnv -- The knot-tied environment
125 -> TcM (TcEnv, TcResults)
127 -- (unf_env :: TcEnv) is used for type-checking interface pragmas
128 -- which is done lazily [ie failure just drops the pragma
129 -- without having any global-failure effect].
131 -- unf_env is also used to get the pragama info
132 -- for imported dfuns and default methods
134 tcModule pcs hst get_fixity this_mod decls unf_env
135 = -- Type-check the type and class decls
136 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
139 classes = tcEnvClasses env
140 tycons = tcEnvTyCons env -- INCLUDES tycons derived from classes
141 local_tycons = [ tc | tc <- tycons,
143 not (isClassTyCon tc)
145 -- For local_tycons, filter out the ones derived from classes
146 -- Otherwise the latter show up in interface files
149 -- Typecheck the instance decls, includes deriving
150 tcInstDecls1 (pcs_insts pcs) (pcs_PRS pcs)
151 hst unf_env get_fixity this_mod
152 local_tycons decls `thenTc` \ (new_pcs_insts, inst_env, local_inst_info, deriv_binds) ->
153 tcSetInstEnv inst_env $
155 -- Default declarations
156 tcDefaults decls `thenTc` \ defaulting_tys ->
157 tcSetDefaultTys defaulting_tys $
159 -- Interface type signatures
160 -- We tie a knot so that the Ids read out of interfaces are in scope
161 -- when we read their pragmas.
162 -- What we rely on is that pragmas are typechecked lazily; if
163 -- any type errors are found (ie there's an inconsistency)
164 -- we silently discard the pragma
165 -- We must do this before mkImplicitDataBinds (which comes next), since
166 -- the latter looks up unpackCStringId, for example, which is usually
168 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
169 tcExtendGlobalValEnv sig_ids $
171 -- Create any necessary record selector Ids and their bindings
172 -- "Necessary" includes data and newtype declarations
173 -- We don't create bindings for dictionary constructors;
174 -- they are always fully applied, and the bindings are just there
175 -- to support partial applications
176 mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
177 mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
179 -- Extend the global value environment with
181 -- (b) record selectors
182 -- (c) class op selectors
183 -- (d) default-method ids... where? I can't see where these are
184 -- put into the envt, and I'm worried that the zonking phase
185 -- will find they aren't there and complain.
186 tcExtendGlobalValEnv data_ids $
187 tcExtendGlobalValEnv cls_ids $
189 -- Foreign import declarations next
190 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
191 tcExtendGlobalValEnv fo_ids $
193 -- Value declarations next.
194 -- We also typecheck any extra binds that came out of the "deriving" process
195 tcTopBinds (get_binds decls `ThenBinds` deriv_binds) `thenTc` \ ((val_binds, env), lie_valdecls) ->
198 -- Foreign export declarations next
199 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
201 -- Second pass over class and instance declarations,
202 -- to compile the bindings themselves.
203 tcInstDecls2 local_inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
204 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
205 tcRules (pcs_rules pcs) this_mod decls `thenNF_Tc` \ (new_pcs_rules, lie_rules, local_rules) ->
207 -- Deal with constant or ambiguous InstIds. How could
208 -- there be ambiguous ones? They can only arise if a
209 -- top-level decl falls under the monomorphism
210 -- restriction, and no subsequent decl instantiates its
211 -- type. (Usually, ambiguous type variables are resolved
212 -- during the generalisation step.)
214 lie_alldecls = lie_valdecls `plusLIE`
215 lie_instdecls `plusLIE`
216 lie_clasdecls `plusLIE`
217 lie_fodecls `plusLIE`
220 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
222 -- Backsubstitution. This must be done last.
223 -- Even tcSimplifyTop may do some unification.
225 all_binds = imp_data_binds `AndMonoBinds`
226 imp_cls_binds `AndMonoBinds`
227 val_binds `AndMonoBinds`
228 inst_binds `AndMonoBinds`
229 cls_dm_binds `AndMonoBinds`
230 const_inst_binds `AndMonoBinds`
233 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', final_env) ->
235 -- zonkTopBinds puts all the top-level Ids into the tcGEnv
236 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
237 zonkRules local_rules `thenNF_Tc` \ local_rules' ->
240 let (local_things, imported_things) = partition (isLocalThing this_mod)
241 (nameEnvElts (getTcGEnv final_env))
243 local_type_env :: TypeEnv
244 local_type_env = mkTypeEnv local_things
246 new_pte :: PackageTypeEnv
247 new_pte = extendTypeEnvList (pcs_PTE pcs) imported_things
249 final_pcs :: PersistentCompilerState
250 final_pcs = pcs { pcs_PTE = new_pte,
251 pcs_insts = new_pcs_insts,
252 pcs_rules = new_pcs_rules
256 TcResults { tc_pcs = final_pcs,
257 tc_env = local_type_env,
258 tc_binds = all_binds',
259 tc_insts = map iDFunId local_inst_info,
260 tc_fords = foi_decls ++ foe_decls',
261 tc_rules = local_rules'
264 get_binds decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
269 %************************************************************************
271 \subsection{Dumping output}
273 %************************************************************************
276 printTcDump dflags Nothing = return ()
277 printTcDump dflags (Just results)
278 = do dumpIfSet_dyn dflags Opt_D_dump_types
279 "Type signatures" (dump_sigs results)
280 dumpIfSet_dyn dflags Opt_D_dump_tc
281 "Typechecked" (dump_tc results)
284 = vcat [ppr (tc_binds results),
285 pp_rules (tc_rules results),
286 ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
289 dump_sigs results -- Print type signatures
290 = -- Convert to HsType so that we get source-language style printing
291 -- And sort by RdrName
292 vcat $ map ppr_sig $ sortLt lt_sig $
293 [(toRdrName id, toHsType (idType id))
294 | AnId id <- nameEnvElts (tc_env results),
298 lt_sig (n1,_) (n2,_) = n1 < n2
299 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
301 want_sig id | opt_PprStyle_Debug = True
302 | otherwise = isLocallyDefined n &&
304 not (isSysOcc (nameOccName n))
308 ppr_gen_tycons tcs = vcat [ptext SLIT("{-# Generic type constructor details"),
309 vcat (map ppr_gen_tycon (filter isLocallyDefined tcs)),
313 -- x&y are now Id's, not CoreExpr's
315 | Just ep <- tyConGenInfo tycon
316 = (ppr tycon <> colon) $$ nest 4 (ppr_ep ep)
318 | otherwise = ppr tycon <> colon <+> ptext SLIT("Not derivable")
321 = vcat [ ptext SLIT("Rep type:") <+> ppr (funResultTy from_tau),
322 ptext SLIT("From:") <+> ppr (unfoldingTemplate (idUnfolding from)),
323 ptext SLIT("To:") <+> ppr (unfoldingTemplate (idUnfolding to))
326 (_,from_tau) = splitForAllTys (idType from)
329 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
330 nest 4 (vcat (map ppr rs)),