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 RecTcEnv, 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, idUnfolding )
45 import Module ( Module )
46 import Name ( Name, isLocallyDefined, toRdrName )
47 import Name ( nameEnvElts, lookupNameEnv )
48 import TyCon ( tyConGenInfo )
49 import Maybes ( thenMaybe )
51 import BasicTypes ( EP(..), Fixity )
52 import Bag ( isEmptyBag )
54 import HscTypes ( PersistentCompilerState(..), HomeSymbolTable, HomeIfaceTable,
55 PackageTypeEnv, DFunId, ModIface(..),
56 TypeEnv, extendTypeEnvList, lookupIface,
57 TyThing(..), mkTypeEnv )
58 import List ( partition )
61 Outside-world interface:
64 -- Convenient type synonyms first:
67 tc_pcs :: PersistentCompilerState, -- Augmented with imported information,
68 -- (but not stuff from this module)
70 -- All these fields have info *just for this module*
71 tc_env :: TypeEnv, -- The top level TypeEnv
72 tc_insts :: [DFunId], -- Instances
73 tc_binds :: TypecheckedMonoBinds, -- Bindings
74 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
75 tc_rules :: [TypecheckedRuleDecl] -- Transformation rules
82 -> PersistentCompilerState
83 -> HomeSymbolTable -> HomeIfaceTable
85 -> IO (Maybe TcResults)
87 typecheckModule dflags this_mod pcs hst hit decls
88 = do env <- initTcEnv hst (pcs_PTE pcs)
90 (maybe_result, (warns,errs)) <- initTc dflags env tc_module
92 let { maybe_tc_result :: Maybe TcResults ;
93 maybe_tc_result = case maybe_result of
95 Just (_,r) -> Just r }
97 printErrorsAndWarnings (errs,warns)
98 printTcDump dflags maybe_tc_result
100 if isEmptyBag errs then
101 return maybe_tc_result
105 tc_module :: TcM (RecTcEnv, TcResults)
106 tc_module = fixTc (\ ~(unf_env ,_) -> tcModule pcs hst get_fixity this_mod decls unf_env)
110 get_fixity :: Name -> Maybe Fixity
111 get_fixity nm = lookupIface hit pit this_mod nm `thenMaybe` \ iface ->
112 lookupNameEnv (mi_fixities iface) nm
115 The internal monster:
117 tcModule :: PersistentCompilerState
119 -> (Name -> Maybe Fixity)
122 -> RecTcEnv -- The knot-tied environment
123 -> TcM (TcEnv, TcResults)
125 -- (unf_env :: RecTcEnv) is used for type-checking interface pragmas
126 -- which is done lazily [ie failure just drops the pragma
127 -- without having any global-failure effect].
129 -- unf_env is also used to get the pragama info
130 -- for imported dfuns and default methods
132 tcModule pcs hst get_fixity this_mod decls unf_env
133 = -- Type-check the type and class decls
134 traceTc (text "Tc1") `thenTc_`
135 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
138 classes = tcEnvClasses env
139 tycons = tcEnvTyCons env -- INCLUDES tycons derived from classes
142 -- Typecheck the instance decls, includes deriving
143 traceTc (text "Tc2") `thenTc_`
144 tcInstDecls1 (pcs_insts pcs) (pcs_PRS pcs)
145 hst unf_env get_fixity this_mod
146 tycons decls `thenTc` \ (new_pcs_insts, inst_env, local_inst_info, deriv_binds) ->
147 tcSetInstEnv inst_env $
149 -- Default declarations
150 traceTc (text "Tc3") `thenTc_`
151 tcDefaults decls `thenTc` \ defaulting_tys ->
152 tcSetDefaultTys defaulting_tys $
154 -- Interface type signatures
155 -- We tie a knot so that the Ids read out of interfaces are in scope
156 -- when we read their pragmas.
157 -- What we rely on is that pragmas are typechecked lazily; if
158 -- any type errors are found (ie there's an inconsistency)
159 -- we silently discard the pragma
160 -- We must do this before mkImplicitDataBinds (which comes next), since
161 -- the latter looks up unpackCStringId, for example, which is usually
163 traceTc (text "Tc3") `thenTc_`
164 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
165 traceTc (text "Tc5") `thenTc_` (
166 tcExtendGlobalValEnv sig_ids $
167 tcGetEnv `thenTc` \ unf_env ->
169 -- Create any necessary record selector Ids and their bindings
170 -- "Necessary" includes data and newtype declarations
171 -- We don't create bindings for dictionary constructors;
172 -- they are always fully applied, and the bindings are just there
173 -- to support partial applications
174 mkImplicitDataBinds this_mod tycons `thenTc` \ (data_ids, imp_data_binds) ->
175 mkImplicitClassBinds this_mod classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
177 -- Extend the global value environment with
179 -- (b) record selectors
180 -- (c) class op selectors
181 -- (d) default-method ids... where? I can't see where these are
182 -- put into the envt, and I'm worried that the zonking phase
183 -- will find they aren't there and complain.
184 tcExtendGlobalValEnv data_ids $
185 tcExtendGlobalValEnv cls_ids $
187 -- Foreign import declarations next
188 traceTc (text "Tc6") `thenTc_`
189 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
190 tcExtendGlobalValEnv fo_ids $
192 -- Value declarations next.
193 -- We also typecheck any extra binds that came out of the "deriving" process
194 traceTc (text "Tc7") `thenTc_`
195 tcTopBinds (get_binds decls `ThenBinds` deriv_binds) `thenTc` \ ((val_binds, env), lie_valdecls) ->
198 -- Foreign export declarations next
199 traceTc (text "Tc8") `thenTc_`
200 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
202 -- Second pass over class and instance declarations,
203 -- to compile the bindings themselves.
204 tcInstDecls2 local_inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
205 tcClassDecls2 this_mod decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
206 tcRules (pcs_rules pcs) this_mod decls `thenNF_Tc` \ (new_pcs_rules, lie_rules, local_rules) ->
208 -- Deal with constant or ambiguous InstIds. How could
209 -- there be ambiguous ones? They can only arise if a
210 -- top-level decl falls under the monomorphism
211 -- restriction, and no subsequent decl instantiates its
212 -- type. (Usually, ambiguous type variables are resolved
213 -- during the generalisation step.)
215 lie_alldecls = lie_valdecls `plusLIE`
216 lie_instdecls `plusLIE`
217 lie_clasdecls `plusLIE`
218 lie_fodecls `plusLIE`
221 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
223 -- Backsubstitution. This must be done last.
224 -- Even tcSimplifyTop may do some unification.
226 all_binds = imp_data_binds `AndMonoBinds`
227 imp_cls_binds `AndMonoBinds`
228 val_binds `AndMonoBinds`
229 inst_binds `AndMonoBinds`
230 cls_dm_binds `AndMonoBinds`
231 const_inst_binds `AndMonoBinds`
234 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', final_env) ->
236 -- zonkTopBinds puts all the top-level Ids into the tcGEnv
237 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
238 zonkRules local_rules `thenNF_Tc` \ local_rules' ->
241 let (local_things, imported_things) = partition (isLocalThing this_mod)
242 (nameEnvElts (getTcGEnv final_env))
244 local_type_env :: TypeEnv
245 local_type_env = mkTypeEnv local_things
247 new_pte :: PackageTypeEnv
248 new_pte = extendTypeEnvList (pcs_PTE pcs) imported_things
250 final_pcs :: PersistentCompilerState
251 final_pcs = pcs { pcs_PTE = new_pte,
252 pcs_insts = new_pcs_insts,
253 pcs_rules = new_pcs_rules
257 TcResults { tc_pcs = final_pcs,
258 tc_env = local_type_env,
259 tc_binds = all_binds',
260 tc_insts = map iDFunId local_inst_info,
261 tc_fords = foi_decls ++ foe_decls',
262 tc_rules = local_rules'
266 get_binds decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
271 %************************************************************************
273 \subsection{Dumping output}
275 %************************************************************************
278 printTcDump dflags Nothing = return ()
279 printTcDump dflags (Just results)
280 = do dumpIfSet_dyn dflags Opt_D_dump_types
281 "Type signatures" (dump_sigs results)
282 dumpIfSet_dyn dflags Opt_D_dump_tc
283 "Typechecked" (dump_tc results)
286 = vcat [ppr (tc_binds results),
287 pp_rules (tc_rules results),
288 ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
291 dump_sigs results -- Print type signatures
292 = -- Convert to HsType so that we get source-language style printing
293 -- And sort by RdrName
294 vcat $ map ppr_sig $ sortLt lt_sig $
295 [(toRdrName id, toHsType (idType id))
296 | AnId id <- nameEnvElts (tc_env results),
300 lt_sig (n1,_) (n2,_) = n1 < n2
301 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
303 want_sig id | opt_PprStyle_Debug = True
304 | otherwise = isLocallyDefined id
306 ppr_gen_tycons tcs = vcat [ptext SLIT("{-# Generic type constructor details"),
307 vcat (map ppr_gen_tycon (filter isLocallyDefined tcs)),
311 -- x&y are now Id's, not CoreExpr's
313 | Just ep <- tyConGenInfo tycon
314 = (ppr tycon <> colon) $$ nest 4 (ppr_ep ep)
316 | otherwise = ppr tycon <> colon <+> ptext SLIT("Not derivable")
319 = vcat [ ptext SLIT("Rep type:") <+> ppr (funResultTy from_tau),
320 ptext SLIT("From:") <+> ppr (unfoldingTemplate (idUnfolding from)),
321 ptext SLIT("To:") <+> ppr (unfoldingTemplate (idUnfolding to))
324 (_,from_tau) = splitForAllTys (idType from)
327 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
328 nest 4 (vcat (map ppr rs)),