2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcIfaceSig]{Type checking of type signatures in interface files}
7 module TcIfaceSig ( tcInterfaceSigs,
12 #include "HsVersions.h"
14 import HsSyn ( CoreDecl(..), TyClDecl(..), HsTupCon(..) )
15 import TcHsSyn ( TypecheckedCoreBind )
18 import TcMonoType ( tcIfaceType, kcHsSigType )
19 import TcEnv ( tcExtendTyVarEnv, tcExtendGlobalValEnv, tcLookupGlobalId,
22 import RnHsSyn ( RenamedCoreDecl, RenamedTyClDecl )
24 import Literal ( Literal(..) )
26 import CoreUtils ( exprType )
28 import CoreLint ( lintUnfolding )
29 import WorkWrap ( mkWrapper )
31 import Id ( Id, mkVanillaGlobal, mkLocalId, isDataConWorkId_maybe )
32 import MkId ( mkFCallId )
34 import TyCon ( tyConDataCons, tyConTyVars )
35 import DataCon ( DataCon, dataConWorkId, dataConExistentialTyVars, dataConArgTys )
36 import Type ( mkTyVarTys, splitTyConApp )
37 import TysWiredIn ( tupleCon )
38 import Var ( mkTyVar, tyVarKind )
40 import UniqSupply ( initUs_ )
42 import Util ( zipWithEqual, dropList, equalLength )
43 import HscTypes ( TyThing(..), typeEnvIds )
44 import CmdLineOpts ( DynFlag(..) )
47 Ultimately, type signatures in interfaces will have pragmatic
48 information attached, so it is a good idea to have separate code to
51 As always, we do not have to worry about user-pragmas in interface
55 tcInterfaceSigs :: [RenamedTyClDecl] -- Ignore non-sig-decls in these decls
58 tcInterfaceSigs decls = fixM (tc_interface_sigs decls)
59 -- We tie a knot so that the Ids read out of interfaces are in scope
60 -- when we read their pragmas.
61 -- What we rely on is that pragmas are typechecked lazily; if
62 -- any type errors are found (ie there's an inconsistency)
63 -- we silently discard the pragma
65 -- NOTE ALSO: the knot is in two parts:
66 -- * Ids defined in this module are added to the typechecker envt
67 -- which is knot-tied by the fixM.
68 -- * Imported Ids are side-effected into the PCS by the
69 -- tcExtendGlobalValueEnv, so they will be seen there provided
70 -- we don't look them up too early.
71 -- In both cases, we must defer lookups until after the knot is tied
73 -- We used to have a much bigger loop (in TcRnDriver), so that the
74 -- interface pragmas could mention variables bound in this module
75 -- (by mutual recn), but
76 -- (a) the knot is tiresomely big, and
77 -- (b) it black-holes when we have Template Haskell
79 -- For (b) consider: f = $(...h....)
80 -- where h is imported, and calls f via an hi-boot file.
81 -- This is bad! But it is not seen as a staging error, because h
82 -- is indeed imported. We don't want the type-checker to black-hole
83 -- when simplifying and compiling the splice!
85 -- Simple solution: discard any unfolding that mentions a variable
86 -- bound in this module (and hence not yet processed).
87 -- The discarding happens when forkM finds a type error.
89 tc_interface_sigs decls unf_env
90 = sequenceM [do_one d | d@(IfaceSig {}) <- decls] `thenM` \ sig_ids ->
91 tcExtendGlobalValEnv sig_ids getGblEnv
92 -- Return the extended environment
94 in_scope_vars = typeEnvIds (tcg_type_env unf_env)
95 -- When we have hi-boot files, an unfolding might refer to
96 -- something defined in this module, so we must build a
97 -- suitable in-scope set. This thunk will only be poked
98 -- if -dcore-lint is on.
100 do_one IfaceSig {tcdName = name, tcdType = ty,
101 tcdIdInfo = id_infos, tcdLoc = src_loc}
102 = addSrcLoc src_loc $
103 addErrCtxt (ifaceSigCtxt name) $
104 tcIfaceType ty `thenM` \ sigma_ty ->
105 tcIdInfo unf_env in_scope_vars name
106 sigma_ty id_infos `thenM` \ id_info ->
107 returnM (mkVanillaGlobal name sigma_ty id_info)
111 tcIdInfo unf_env in_scope_vars name ty info_ins
112 = setGblEnv unf_env $
113 -- Use the knot-tied environment for the IdInfo
114 -- In particular: typechecking unfoldings and worker names
115 foldlM tcPrag init_info info_ins
117 -- Set the CgInfo to something sensible but uninformative before
118 -- we start; default assumption is that it has CAFs
119 init_info = hasCafIdInfo
121 tcPrag info HsNoCafRefs = returnM (info `setCafInfo` NoCafRefs)
122 tcPrag info (HsArity arity) = returnM (info `setArityInfo` arity)
123 tcPrag info (HsStrictness str) = returnM (info `setAllStrictnessInfo` Just str)
124 tcPrag info (HsWorker nm arity) = tcWorkerInfo ty info nm arity
126 tcPrag info (HsUnfold inline_prag expr)
127 = tcPragExpr name in_scope_vars expr `thenM` \ maybe_expr' ->
129 -- maybe_expr' doesn't get looked at if the unfolding
130 -- is never inspected; so the typecheck doesn't even happen
131 unfold_info = case maybe_expr' of
132 Nothing -> noUnfolding
133 Just expr' -> mkTopUnfolding expr'
135 returnM (info `setUnfoldingInfoLazily` unfold_info
136 `setInlinePragInfo` inline_prag)
140 tcWorkerInfo ty info wkr_name arity
141 = forkM doc (tcVar wkr_name) `thenM` \ maybe_wkr_id ->
142 -- Watch out! We can't pull on unf_env too eagerly!
145 -- We return without testing maybe_wkr_id, but as soon as info is
146 -- looked at we will test it. That's ok, because its outside the
147 -- knot; and there seems no big reason to further defer the
148 -- tcVar lookup. (Contrast with tcPragExpr, where postponing walking
149 -- over the unfolding until it's actually used does seem worth while.)
150 newUniqueSupply `thenM` \ us ->
151 returnM (case maybe_wkr_id of
153 Just wkr_id -> info `setUnfoldingInfoLazily` mk_unfolding us wkr_id
154 `setWorkerInfo` HasWorker wkr_id arity)
157 doc = text "worker for" <+> ppr wkr_name
159 mk_unfolding us wkr_id = mkTopUnfolding (initUs_ us (mkWrapper ty strict_sig) wkr_id)
161 -- We are relying here on strictness info always appearing
162 -- before worker info, fingers crossed ....
163 strict_sig = case newStrictnessInfo info of
165 Nothing -> pprPanic "Worker info but no strictness for" (ppr wkr_name)
168 For unfoldings we try to do the job lazily, so that we never type check
169 an unfolding that isn't going to be looked at.
172 tcPragExpr :: Name -> [Id] -> UfExpr Name -> TcM (Maybe CoreExpr)
173 tcPragExpr name in_scope_vars expr
175 tcCoreExpr expr `thenM` \ core_expr' ->
177 -- Check for type consistency in the unfolding
178 ifOptM Opt_DoCoreLinting (
179 getSrcLocM `thenM` \ src_loc ->
180 case lintUnfolding src_loc in_scope_vars core_expr' of
181 Nothing -> returnM ()
182 Just fail_msg -> failWithTc ((doc <+> text "Failed Lint") $$ fail_msg)
187 doc = text "unfolding of" <+> ppr name
191 Variables in unfoldings
192 ~~~~~~~~~~~~~~~~~~~~~~~
195 tcVar :: Name -> TcM Id
196 -- Inside here we use only the Global environment, even for locally bound variables.
197 -- Why? Because we know all the types and want to bind them to real Ids.
198 tcVar name = tcLookupGlobalId name
204 tcCoreExpr :: UfExpr Name -> TcM CoreExpr
206 tcCoreExpr (UfType ty)
207 = tcIfaceType ty `thenM` \ ty' ->
208 -- It might not be of kind type
211 tcCoreExpr (UfVar name)
212 = tcVar name `thenM` \ id ->
215 tcCoreExpr (UfLit lit)
218 -- The dreaded lit-lits are also similar, except here the type
219 -- is read in explicitly rather than being implicit
220 tcCoreExpr (UfLitLit lit ty)
221 = tcIfaceType ty `thenM` \ ty' ->
222 returnM (Lit (MachLitLit lit ty'))
224 tcCoreExpr (UfFCall cc ty)
225 = tcIfaceType ty `thenM` \ ty' ->
226 newUnique `thenM` \ u ->
227 returnM (Var (mkFCallId u cc ty'))
229 tcCoreExpr (UfTuple (HsTupCon boxity arity) args)
230 = mappM tcCoreExpr args `thenM` \ args' ->
232 -- Put the missing type arguments back in
233 con_args = map (Type . exprType) args' ++ args'
235 returnM (mkApps (Var con_id) con_args)
237 con_id = dataConWorkId (tupleCon boxity arity)
240 tcCoreExpr (UfLam bndr body)
241 = tcCoreLamBndr bndr $ \ bndr' ->
242 tcCoreExpr body `thenM` \ body' ->
243 returnM (Lam bndr' body')
245 tcCoreExpr (UfApp fun arg)
246 = tcCoreExpr fun `thenM` \ fun' ->
247 tcCoreExpr arg `thenM` \ arg' ->
248 returnM (App fun' arg')
250 tcCoreExpr (UfCase scrut case_bndr alts)
251 = tcCoreExpr scrut `thenM` \ scrut' ->
253 scrut_ty = exprType scrut'
254 case_bndr' = mkLocalId case_bndr scrut_ty
256 tcExtendGlobalValEnv [case_bndr'] $
257 mappM (tcCoreAlt scrut_ty) alts `thenM` \ alts' ->
258 returnM (Case scrut' case_bndr' alts')
260 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
261 = tcCoreExpr rhs `thenM` \ rhs' ->
262 tcCoreValBndr bndr $ \ bndr' ->
263 tcCoreExpr body `thenM` \ body' ->
264 returnM (Let (NonRec bndr' rhs') body')
266 tcCoreExpr (UfLet (UfRec pairs) body)
267 = tcCoreValBndrs bndrs $ \ bndrs' ->
268 mappM tcCoreExpr rhss `thenM` \ rhss' ->
269 tcCoreExpr body `thenM` \ body' ->
270 returnM (Let (Rec (bndrs' `zip` rhss')) body')
272 (bndrs, rhss) = unzip pairs
274 tcCoreExpr (UfNote note expr)
275 = tcCoreExpr expr `thenM` \ expr' ->
277 UfCoerce to_ty -> tcIfaceType to_ty `thenM` \ to_ty' ->
278 returnM (Note (Coerce to_ty'
279 (exprType expr')) expr')
280 UfInlineCall -> returnM (Note InlineCall expr')
281 UfInlineMe -> returnM (Note InlineMe expr')
282 UfSCC cc -> returnM (Note (SCC cc) expr')
286 tcCoreLamBndr (UfValBinder name ty) thing_inside
287 = tcIfaceType ty `thenM` \ ty' ->
289 id = mkLocalId name ty'
291 tcExtendGlobalValEnv [id] $
294 tcCoreLamBndr (UfTyBinder name kind) thing_inside
296 tyvar = mkTyVar name kind
298 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
300 tcCoreLamBndrs [] thing_inside = thing_inside []
301 tcCoreLamBndrs (b:bs) thing_inside
302 = tcCoreLamBndr b $ \ b' ->
303 tcCoreLamBndrs bs $ \ bs' ->
304 thing_inside (b':bs')
306 tcCoreValBndr (UfValBinder name ty) thing_inside
307 = tcIfaceType ty `thenM` \ ty' ->
309 id = mkLocalId name ty'
311 tcExtendGlobalValEnv [id] $
314 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
315 = mappM tcIfaceType tys `thenM` \ tys' ->
317 ids = zipWithEqual "tcCoreValBndr" mkLocalId names tys'
319 tcExtendGlobalValEnv ids $
322 names = [name | UfValBinder name _ <- bndrs]
323 tys = [ty | UfValBinder _ ty <- bndrs]
327 tcCoreAlt scrut_ty (UfDefault, names, rhs)
328 = ASSERT( null names )
329 tcCoreExpr rhs `thenM` \ rhs' ->
330 returnM (DEFAULT, [], rhs')
332 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
333 = ASSERT( null names )
334 tcCoreExpr rhs `thenM` \ rhs' ->
335 returnM (LitAlt lit, [], rhs')
337 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
338 = ASSERT( null names )
339 tcCoreExpr rhs `thenM` \ rhs' ->
340 tcIfaceType ty `thenM` \ ty' ->
341 returnM (LitAlt (MachLitLit str ty'), [], rhs')
343 -- A case alternative is made quite a bit more complicated
344 -- by the fact that we omit type annotations because we can
345 -- work them out. True enough, but its not that easy!
346 tcCoreAlt scrut_ty alt@(con, names, rhs)
347 = tcConAlt con `thenM` \ con ->
349 ex_tyvars = dataConExistentialTyVars con
350 (tycon, inst_tys) = splitTyConApp scrut_ty -- NB: not tcSplitTyConApp
351 -- We are looking at Core here
352 main_tyvars = tyConTyVars tycon
353 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
354 ex_tys' = mkTyVarTys ex_tyvars'
355 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
356 id_names = dropList ex_tyvars names
359 | not (equalLength id_names arg_tys)
360 = pprPanic "tcCoreAlts" (ppr (con, names, rhs) $$
361 (ppr main_tyvars <+> ppr ex_tyvars) $$
365 = zipWithEqual "tcCoreAlts" mkLocalId id_names arg_tys
367 ASSERT( con `elem` tyConDataCons tycon && equalLength inst_tys main_tyvars )
368 tcExtendTyVarEnv ex_tyvars' $
369 tcExtendGlobalValEnv arg_ids $
370 tcCoreExpr rhs `thenM` \ rhs' ->
371 returnM (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
374 tcConAlt :: UfConAlt Name -> TcM DataCon
375 tcConAlt (UfTupleAlt (HsTupCon boxity arity))
376 = returnM (tupleCon boxity arity)
378 tcConAlt (UfDataAlt con_name) -- When reading interface files
379 -- the con_name will be the real name of
381 = tcLookupDataCon con_name
384 %************************************************************************
386 \subsection{Core decls}
388 %************************************************************************
392 tcCoreBinds :: [RenamedCoreDecl] -> TcM [TypecheckedCoreBind]
393 -- We don't assume the bindings are in dependency order
394 -- So first build the environment, then check the RHSs
395 tcCoreBinds ls = mappM tcCoreBinder ls `thenM` \ bndrs ->
396 tcExtendGlobalValEnv bndrs $
397 mappM (tcCoreBind bndrs) ls
399 tcCoreBinder (CoreDecl nm ty _ _)
400 = kcHsSigType ty `thenM_`
401 tcIfaceType ty `thenM` \ ty' ->
402 returnM (mkLocalId nm ty')
404 tcCoreBind bndrs (CoreDecl nm _ rhs loc)
405 = tcVar nm `thenM` \ id ->
406 tcCoreExpr rhs `thenM` \ rhs' ->
408 mb_err = lintUnfolding loc bndrs rhs'
411 Just err -> addErr err
412 Nothing -> returnM ()) `thenM_`
419 ifaceSigCtxt sig_name
420 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]