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,
14 #include "HsVersions.h"
16 import HsSyn ( TyClDecl(..), HsTupCon(..) )
18 import TcMonoType ( tcIfaceType )
19 import TcEnv ( RecTcEnv, tcExtendTyVarEnv,
20 tcExtendGlobalValEnv, tcSetEnv, tcEnvIds,
21 tcLookupGlobal_maybe, tcLookupRecId_maybe
24 import RnHsSyn ( RenamedTyClDecl )
26 import Literal ( Literal(..) )
28 import CoreUtils ( exprType )
30 import CoreLint ( lintUnfolding )
31 import WorkWrap ( mkWrapper )
33 import Id ( Id, mkVanillaGlobal, mkLocalId, idName, isDataConWrapId_maybe )
34 import Module ( Module )
35 import MkId ( mkFCallId )
37 import TyCon ( tyConDataCons )
38 import DataCon ( DataCon, dataConId, dataConSig, dataConArgTys )
39 import Type ( Type, mkTyVarTys, splitTyConApp )
40 import TysWiredIn ( tupleCon )
41 import Var ( mkTyVar, tyVarKind )
42 import Name ( Name, nameIsLocalOrFrom )
43 import ErrUtils ( pprBagOfErrors )
45 import Util ( zipWithEqual, dropList, equalLength )
46 import HscTypes ( TyThing(..) )
49 Ultimately, type signatures in interfaces will have pragmatic
50 information attached, so it is a good idea to have separate code to
53 As always, we do not have to worry about user-pragmas in interface
57 tcInterfaceSigs :: RecTcEnv -- Envt to use when checking unfoldings
58 -> Module -- This module
59 -> [RenamedTyClDecl] -- Ignore non-sig-decls in these decls
63 tcInterfaceSigs unf_env mod decls
64 = listTc [ do_one name ty id_infos src_loc
65 | IfaceSig {tcdName = name, tcdType = ty, tcdIdInfo = id_infos, tcdLoc =src_loc} <- decls]
67 in_scope_vars = filter (nameIsLocalOrFrom mod . idName) (tcEnvIds unf_env)
68 -- Oops: using isLocalId instead can give a black hole
69 -- because it looks at the idinfo
71 -- When we have hi-boot files, an unfolding might refer to
72 -- something defined in this module, so we must build a
73 -- suitable in-scope set. This thunk will only be poked
74 -- if -dcore-lint is on.
76 do_one name ty id_infos src_loc
77 = tcAddSrcLoc src_loc $
78 tcAddErrCtxt (ifaceSigCtxt name) $
79 tcIfaceType ty `thenTc` \ sigma_ty ->
80 tcIdInfo unf_env in_scope_vars name
81 sigma_ty id_infos `thenTc` \ id_info ->
82 returnTc (mkVanillaGlobal name sigma_ty id_info)
86 tcIdInfo unf_env in_scope_vars name ty info_ins
87 = foldlTc tcPrag init_info info_ins
89 -- set the CgInfo to something sensible but uninformative before
90 -- we start, because the default CgInfo is a panic.
91 init_info = vanillaIdInfo `setCgInfo` vanillaCgInfo
93 tcPrag info (HsNoCafRefs) = returnTc (info `setCafInfo` NoCafRefs)
95 tcPrag info (HsArity arity) =
96 returnTc (info `setArityInfo` arity)
98 tcPrag info (HsUnfold inline_prag expr)
99 = tcPragExpr unf_env name in_scope_vars expr `thenNF_Tc` \ maybe_expr' ->
101 -- maybe_expr doesn't get looked at if the unfolding
102 -- is never inspected; so the typecheck doesn't even happen
103 unfold_info = case maybe_expr' of
104 Nothing -> noUnfolding
105 Just expr' -> mkTopUnfolding expr'
106 info1 = info `setUnfoldingInfo` unfold_info
107 info2 = info1 `setInlinePragInfo` inline_prag
111 tcPrag info (HsStrictness strict_info)
112 = returnTc (info `setAllStrictnessInfo` Just strict_info)
114 tcPrag info (HsWorker nm arity)
115 = tcWorkerInfo unf_env ty info nm arity
119 tcWorkerInfo unf_env ty info worker_name arity
120 = uniqSMToTcM (mkWrapper ty strict_sig) `thenNF_Tc` \ wrap_fn ->
122 -- Watch out! We can't pull on unf_env too eagerly!
123 info' = case tcLookupRecId_maybe unf_env worker_name of
125 info `setUnfoldingInfo` mkTopUnfolding (wrap_fn worker_id)
126 `setWorkerInfo` HasWorker worker_id arity
128 Nothing -> pprTrace "tcWorkerInfo failed:"
129 (ppr worker_name) info
133 -- We are relying here on strictness info always appearing
134 -- before worker info, fingers crossed ....
135 strict_sig = case newStrictnessInfo info of
137 Nothing -> pprPanic "Worker info but no strictness for" (ppr worker_name)
140 For unfoldings we try to do the job lazily, so that we never type check
141 an unfolding that isn't going to be looked at.
144 tcPragExpr unf_env name in_scope_vars expr
145 = tcDelay unf_env doc Nothing $
146 tcCoreExpr expr `thenTc` \ core_expr' ->
148 -- Check for type consistency in the unfolding
149 tcGetSrcLoc `thenNF_Tc` \ src_loc ->
150 getDOptsTc `thenNF_Tc` \ dflags ->
151 case lintUnfolding dflags src_loc in_scope_vars core_expr' of
152 (Nothing,_) -> returnTc (Just core_expr') -- ignore warnings
153 (Just fail_msg,_) -> failWithTc ((doc <+> text "failed Lint") $$ fail_msg)
155 doc = text "unfolding of" <+> ppr name
157 tcDelay :: RecTcEnv -> SDoc -> a -> TcM a -> NF_TcM a
158 tcDelay unf_env doc bad_ans thing_inside
160 recoverNF_Tc bad_value (
161 tcSetEnv unf_env thing_inside
164 -- The trace tells what wasn't available, for the benefit of
165 -- compiler hackers who want to improve it!
166 bad_value = getErrsTc `thenNF_Tc` \ (warns,errs) ->
167 returnNF_Tc (pprTrace "Failed:"
168 (hang doc 4 (pprBagOfErrors errs))
173 Variables in unfoldings
174 ~~~~~~~~~~~~~~~~~~~~~~~
175 ****** Inside here we use only the Global environment, even for locally bound variables.
176 ****** Why? Because we know all the types and want to bind them to real Ids.
179 tcVar :: Name -> TcM Id
181 = tcLookupGlobal_maybe name `thenNF_Tc` \ maybe_id ->
183 Just (AnId id) -> returnTc id ;
184 Nothing -> failWithTc (noDecl name)
187 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
193 tcCoreExpr :: UfExpr Name -> TcM CoreExpr
195 tcCoreExpr (UfType ty)
196 = tcIfaceType ty `thenTc` \ ty' ->
197 -- It might not be of kind type
200 tcCoreExpr (UfVar name)
201 = tcVar name `thenTc` \ id ->
204 tcCoreExpr (UfLit lit)
207 -- The dreaded lit-lits are also similar, except here the type
208 -- is read in explicitly rather than being implicit
209 tcCoreExpr (UfLitLit lit ty)
210 = tcIfaceType ty `thenTc` \ ty' ->
211 returnTc (Lit (MachLitLit lit ty'))
213 tcCoreExpr (UfFCall cc ty)
214 = tcIfaceType ty `thenTc` \ ty' ->
215 tcGetUnique `thenNF_Tc` \ u ->
216 returnTc (Var (mkFCallId u cc ty'))
218 tcCoreExpr (UfTuple (HsTupCon _ boxity arity) args)
219 = mapTc tcCoreExpr args `thenTc` \ args' ->
221 -- Put the missing type arguments back in
222 con_args = map (Type . exprType) args' ++ args'
224 returnTc (mkApps (Var con_id) con_args)
226 con_id = dataConWorkId (tupleCon boxity arity)
229 tcCoreExpr (UfLam bndr body)
230 = tcCoreLamBndr bndr $ \ bndr' ->
231 tcCoreExpr body `thenTc` \ body' ->
232 returnTc (Lam bndr' body')
234 tcCoreExpr (UfApp fun arg)
235 = tcCoreExpr fun `thenTc` \ fun' ->
236 tcCoreExpr arg `thenTc` \ arg' ->
237 returnTc (App fun' arg')
239 tcCoreExpr (UfCase scrut case_bndr alts)
240 = tcCoreExpr scrut `thenTc` \ scrut' ->
242 scrut_ty = exprType scrut'
243 case_bndr' = mkLocalId case_bndr scrut_ty
245 tcExtendGlobalValEnv [case_bndr'] $
246 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
247 returnTc (Case scrut' case_bndr' alts')
249 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
250 = tcCoreExpr rhs `thenTc` \ rhs' ->
251 tcCoreValBndr bndr $ \ bndr' ->
252 tcCoreExpr body `thenTc` \ body' ->
253 returnTc (Let (NonRec bndr' rhs') body')
255 tcCoreExpr (UfLet (UfRec pairs) body)
256 = tcCoreValBndrs bndrs $ \ bndrs' ->
257 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
258 tcCoreExpr body `thenTc` \ body' ->
259 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
261 (bndrs, rhss) = unzip pairs
263 tcCoreExpr (UfNote note expr)
264 = tcCoreExpr expr `thenTc` \ expr' ->
266 UfCoerce to_ty -> tcIfaceType to_ty `thenTc` \ to_ty' ->
267 returnTc (Note (Coerce to_ty'
268 (exprType expr')) expr')
269 UfInlineCall -> returnTc (Note InlineCall expr')
270 UfInlineMe -> returnTc (Note InlineMe expr')
271 UfSCC cc -> returnTc (Note (SCC cc) expr')
275 tcCoreLamBndr (UfValBinder name ty) thing_inside
276 = tcIfaceType ty `thenTc` \ ty' ->
278 id = mkLocalId name ty'
280 tcExtendGlobalValEnv [id] $
283 tcCoreLamBndr (UfTyBinder name kind) thing_inside
285 tyvar = mkTyVar name kind
287 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
289 tcCoreLamBndrs [] thing_inside = thing_inside []
290 tcCoreLamBndrs (b:bs) thing_inside
291 = tcCoreLamBndr b $ \ b' ->
292 tcCoreLamBndrs bs $ \ bs' ->
293 thing_inside (b':bs')
295 tcCoreValBndr (UfValBinder name ty) thing_inside
296 = tcIfaceType ty `thenTc` \ ty' ->
298 id = mkLocalId name ty'
300 tcExtendGlobalValEnv [id] $
303 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
304 = mapTc tcIfaceType tys `thenTc` \ tys' ->
306 ids = zipWithEqual "tcCoreValBndr" mkLocalId names tys'
308 tcExtendGlobalValEnv ids $
311 names = [name | UfValBinder name _ <- bndrs]
312 tys = [ty | UfValBinder _ ty <- bndrs]
316 tcCoreAlt scrut_ty (UfDefault, names, rhs)
317 = ASSERT( null names )
318 tcCoreExpr rhs `thenTc` \ rhs' ->
319 returnTc (DEFAULT, [], rhs')
321 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
322 = ASSERT( null names )
323 tcCoreExpr rhs `thenTc` \ rhs' ->
324 returnTc (LitAlt lit, [], rhs')
326 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
327 = ASSERT( null names )
328 tcCoreExpr rhs `thenTc` \ rhs' ->
329 tcIfaceType ty `thenTc` \ ty' ->
330 returnTc (LitAlt (MachLitLit str ty'), [], rhs')
332 -- A case alternative is made quite a bit more complicated
333 -- by the fact that we omit type annotations because we can
334 -- work them out. True enough, but its not that easy!
335 tcCoreAlt scrut_ty alt@(con, names, rhs)
336 = tcConAlt con `thenTc` \ con ->
338 ex_tyvars = dataConExistentialTyVars con
339 (tycon, inst_tys) = splitTyConApp scrut_ty -- NB: not tcSplitTyConApp
340 -- We are looking at Core here
341 main_tyvars = tyConTyVars tycon
342 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
343 ex_tys' = mkTyVarTys ex_tyvars'
344 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
345 id_names = dropList ex_tyvars names
348 | not (equalLength id_names arg_tys)
349 = pprPanic "tcCoreAlts" (ppr (con, names, rhs) $$
350 (ppr main_tyvars <+> ppr ex_tyvars) $$
354 = zipWithEqual "tcCoreAlts" mkLocalId id_names arg_tys
356 ASSERT( con `elem` tyConDataCons tycon && equalLength inst_tys main_tyvars )
357 tcExtendTyVarEnv ex_tyvars' $
358 tcExtendGlobalValEnv arg_ids $
359 tcCoreExpr rhs `thenTc` \ rhs' ->
360 returnTc (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
363 tcConAlt :: UfConAlt Name -> TcM DataCon
364 tcConAlt (UfTupleAlt (HsTupCon _ boxity arity))
365 = returnTc (tupleCon boxity arity)
367 tcConAlt (UfDataAlt con_name)
368 = tcVar con_name `thenTc` \ con_id ->
369 returnTc (case isDataConWrapId_maybe con_id of
371 Nothing -> pprPanic "tcCoreAlt" (ppr con_id))
374 %************************************************************************
376 \subsection{Core decls}
378 %************************************************************************
382 tcCoreBinds :: [RenamedTyClDecl]
383 -> TcM [(Id, Type, CoreExpr)]
384 tcCoreBinds ls = mapTc tcOne ls
386 tcOne (CoreDecl { tcdName = nm, tcdType = ty, tcdRhs = rhs }) =
387 tcVar nm `thenTc` \ i ->
388 tcIfaceType ty `thenTc` \ ty' ->
389 tcCoreExpr rhs `thenTc` \ rhs' ->
390 returnTc (i,ty',rhs')
397 ifaceSigCtxt sig_name
398 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]