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,
13 #include "HsVersions.h"
15 import HsSyn ( CoreDecl(..), TyClDecl(..), HsTupCon(..) )
16 import TcHsSyn ( TypecheckedCoreBind )
18 import TcMonoType ( tcIfaceType )
19 import TcEnv ( RecTcGblEnv, tcExtendTyVarEnv,
21 tcLookupGlobal_maybe, tcLookupRecId_maybe
24 import RnHsSyn ( RenamedCoreDecl, RenamedTyClDecl )
26 import Literal ( Literal(..) )
28 import CoreUtils ( exprType )
30 import CoreLint ( lintUnfolding )
31 import WorkWrap ( mkWrapper )
33 import Id ( Id, mkVanillaGlobal, mkLocalId, isDataConWrapId_maybe )
34 import MkId ( mkFCallId )
36 import TyCon ( tyConDataCons, tyConTyVars )
37 import DataCon ( DataCon, dataConWorkId, dataConExistentialTyVars, dataConArgTys )
38 import Type ( mkTyVarTys, splitTyConApp )
39 import TysWiredIn ( tupleCon )
40 import Var ( mkTyVar, tyVarKind )
42 import UniqSupply ( initUs_ )
44 import Util ( zipWithEqual, dropList, equalLength )
45 import HscTypes ( TyThing(..) )
48 Ultimately, type signatures in interfaces will have pragmatic
49 information attached, so it is a good idea to have separate code to
52 As always, we do not have to worry about user-pragmas in interface
56 tcInterfaceSigs :: RecTcGblEnv -- Envt to use when checking unfoldings
57 -> [RenamedTyClDecl] -- Ignore non-sig-decls in these decls
61 tcInterfaceSigs unf_env decls
62 = sequenceM [ do_one name ty id_infos src_loc
63 | IfaceSig {tcdName = name, tcdType = ty,
64 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
78 addErrCtxt (ifaceSigCtxt name) $
79 tcIfaceType ty `thenM` \ sigma_ty ->
80 tcIdInfo unf_env in_scope_vars name
81 sigma_ty id_infos `thenM` \ id_info ->
82 returnM (mkVanillaGlobal name sigma_ty id_info)
86 tcIdInfo unf_env in_scope_vars name ty info_ins
87 = foldlM tcPrag init_info info_ins
89 -- Set the CgInfo to something sensible but uninformative before
90 -- we start; default assumption is that it has CAFs
91 init_info = hasCafIdInfo
93 tcPrag info (HsNoCafRefs) = returnM (info `setCafInfo` NoCafRefs)
95 tcPrag info (HsArity arity) =
96 returnM (info `setArityInfo` arity)
98 tcPrag info (HsUnfold inline_prag expr)
99 = tcPragExpr unf_env name in_scope_vars expr `thenM` \ 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 = returnM (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 = newUniqueSupply `thenM` \ us ->
122 wrap_fn = initUs_ us (mkWrapper ty strict_sig)
124 -- Watch out! We can't pull on unf_env too eagerly!
125 info' = case tcLookupRecId_maybe unf_env worker_name of
127 info `setUnfoldingInfo` mkTopUnfolding (wrap_fn worker_id)
128 `setWorkerInfo` HasWorker worker_id arity
130 Nothing -> pprTrace "tcWorkerInfo failed:"
131 (ppr worker_name) info
135 -- We are relying here on strictness info always appearing
136 -- before worker info, fingers crossed ....
137 strict_sig = case newStrictnessInfo info of
139 Nothing -> pprPanic "Worker info but no strictness for" (ppr worker_name)
142 For unfoldings we try to do the job lazily, so that we never type check
143 an unfolding that isn't going to be looked at.
146 tcPragExpr unf_env name in_scope_vars expr
150 tcCoreExpr expr `thenM` \ core_expr' ->
152 -- Check for type consistency in the unfolding
153 getSrcLocM `thenM` \ src_loc ->
154 getDOpts `thenM` \ dflags ->
155 case lintUnfolding dflags src_loc in_scope_vars core_expr' of
156 (Nothing,_) -> returnM core_expr' -- ignore warnings
157 (Just fail_msg,_) -> failWithTc ((doc <+> text "failed Lint") $$ fail_msg)
159 doc = text "unfolding of" <+> ppr name
163 Variables in unfoldings
164 ~~~~~~~~~~~~~~~~~~~~~~~
165 ****** Inside here we use only the Global environment, even for locally bound variables.
166 ****** Why? Because we know all the types and want to bind them to real Ids.
169 tcVar :: Name -> TcM Id
171 = tcLookupGlobal_maybe name `thenM` \ maybe_id ->
173 Just (AnId id) -> returnM id ;
174 Nothing -> failWithTc (noDecl name)
177 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
183 tcCoreExpr :: UfExpr Name -> TcM CoreExpr
185 tcCoreExpr (UfType ty)
186 = tcIfaceType ty `thenM` \ ty' ->
187 -- It might not be of kind type
190 tcCoreExpr (UfVar name)
191 = tcVar name `thenM` \ id ->
194 tcCoreExpr (UfLit lit)
197 -- The dreaded lit-lits are also similar, except here the type
198 -- is read in explicitly rather than being implicit
199 tcCoreExpr (UfLitLit lit ty)
200 = tcIfaceType ty `thenM` \ ty' ->
201 returnM (Lit (MachLitLit lit ty'))
203 tcCoreExpr (UfFCall cc ty)
204 = tcIfaceType ty `thenM` \ ty' ->
205 newUnique `thenM` \ u ->
206 returnM (Var (mkFCallId u cc ty'))
208 tcCoreExpr (UfTuple (HsTupCon boxity arity) args)
209 = mappM tcCoreExpr args `thenM` \ args' ->
211 -- Put the missing type arguments back in
212 con_args = map (Type . exprType) args' ++ args'
214 returnM (mkApps (Var con_id) con_args)
216 con_id = dataConWorkId (tupleCon boxity arity)
219 tcCoreExpr (UfLam bndr body)
220 = tcCoreLamBndr bndr $ \ bndr' ->
221 tcCoreExpr body `thenM` \ body' ->
222 returnM (Lam bndr' body')
224 tcCoreExpr (UfApp fun arg)
225 = tcCoreExpr fun `thenM` \ fun' ->
226 tcCoreExpr arg `thenM` \ arg' ->
227 returnM (App fun' arg')
229 tcCoreExpr (UfCase scrut case_bndr alts)
230 = tcCoreExpr scrut `thenM` \ scrut' ->
232 scrut_ty = exprType scrut'
233 case_bndr' = mkLocalId case_bndr scrut_ty
235 tcExtendGlobalValEnv [case_bndr'] $
236 mappM (tcCoreAlt scrut_ty) alts `thenM` \ alts' ->
237 returnM (Case scrut' case_bndr' alts')
239 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
240 = tcCoreExpr rhs `thenM` \ rhs' ->
241 tcCoreValBndr bndr $ \ bndr' ->
242 tcCoreExpr body `thenM` \ body' ->
243 returnM (Let (NonRec bndr' rhs') body')
245 tcCoreExpr (UfLet (UfRec pairs) body)
246 = tcCoreValBndrs bndrs $ \ bndrs' ->
247 mappM tcCoreExpr rhss `thenM` \ rhss' ->
248 tcCoreExpr body `thenM` \ body' ->
249 returnM (Let (Rec (bndrs' `zip` rhss')) body')
251 (bndrs, rhss) = unzip pairs
253 tcCoreExpr (UfNote note expr)
254 = tcCoreExpr expr `thenM` \ expr' ->
256 UfCoerce to_ty -> tcIfaceType to_ty `thenM` \ to_ty' ->
257 returnM (Note (Coerce to_ty'
258 (exprType expr')) expr')
259 UfInlineCall -> returnM (Note InlineCall expr')
260 UfInlineMe -> returnM (Note InlineMe expr')
261 UfSCC cc -> returnM (Note (SCC cc) expr')
265 tcCoreLamBndr (UfValBinder name ty) thing_inside
266 = tcIfaceType ty `thenM` \ ty' ->
268 id = mkLocalId name ty'
270 tcExtendGlobalValEnv [id] $
273 tcCoreLamBndr (UfTyBinder name kind) thing_inside
275 tyvar = mkTyVar name kind
277 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
279 tcCoreLamBndrs [] thing_inside = thing_inside []
280 tcCoreLamBndrs (b:bs) thing_inside
281 = tcCoreLamBndr b $ \ b' ->
282 tcCoreLamBndrs bs $ \ bs' ->
283 thing_inside (b':bs')
285 tcCoreValBndr (UfValBinder name ty) thing_inside
286 = tcIfaceType ty `thenM` \ ty' ->
288 id = mkLocalId name ty'
290 tcExtendGlobalValEnv [id] $
293 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
294 = mappM tcIfaceType tys `thenM` \ tys' ->
296 ids = zipWithEqual "tcCoreValBndr" mkLocalId names tys'
298 tcExtendGlobalValEnv ids $
301 names = [name | UfValBinder name _ <- bndrs]
302 tys = [ty | UfValBinder _ ty <- bndrs]
306 tcCoreAlt scrut_ty (UfDefault, names, rhs)
307 = ASSERT( null names )
308 tcCoreExpr rhs `thenM` \ rhs' ->
309 returnM (DEFAULT, [], rhs')
311 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
312 = ASSERT( null names )
313 tcCoreExpr rhs `thenM` \ rhs' ->
314 returnM (LitAlt lit, [], rhs')
316 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
317 = ASSERT( null names )
318 tcCoreExpr rhs `thenM` \ rhs' ->
319 tcIfaceType ty `thenM` \ ty' ->
320 returnM (LitAlt (MachLitLit str ty'), [], rhs')
322 -- A case alternative is made quite a bit more complicated
323 -- by the fact that we omit type annotations because we can
324 -- work them out. True enough, but its not that easy!
325 tcCoreAlt scrut_ty alt@(con, names, rhs)
326 = tcConAlt con `thenM` \ con ->
328 ex_tyvars = dataConExistentialTyVars con
329 (tycon, inst_tys) = splitTyConApp scrut_ty -- NB: not tcSplitTyConApp
330 -- We are looking at Core here
331 main_tyvars = tyConTyVars tycon
332 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
333 ex_tys' = mkTyVarTys ex_tyvars'
334 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
335 id_names = dropList ex_tyvars names
338 | not (equalLength id_names arg_tys)
339 = pprPanic "tcCoreAlts" (ppr (con, names, rhs) $$
340 (ppr main_tyvars <+> ppr ex_tyvars) $$
344 = zipWithEqual "tcCoreAlts" mkLocalId id_names arg_tys
346 ASSERT( con `elem` tyConDataCons tycon && equalLength inst_tys main_tyvars )
347 tcExtendTyVarEnv ex_tyvars' $
348 tcExtendGlobalValEnv arg_ids $
349 tcCoreExpr rhs `thenM` \ rhs' ->
350 returnM (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
353 tcConAlt :: UfConAlt Name -> TcM DataCon
354 tcConAlt (UfTupleAlt (HsTupCon boxity arity))
355 = returnM (tupleCon boxity arity)
357 tcConAlt (UfDataAlt con_name)
358 = tcVar con_name `thenM` \ con_id ->
359 returnM (case isDataConWrapId_maybe con_id of
361 Nothing -> pprPanic "tcCoreAlt" (ppr con_id))
364 %************************************************************************
366 \subsection{Core decls}
368 %************************************************************************
372 tcCoreBinds :: [RenamedCoreDecl] -> TcM [TypecheckedCoreBind]
373 -- We don't assume the bindings are in dependency order
374 -- So first build the environment, then check the RHSs
375 tcCoreBinds ls = mappM tcCoreBinder ls `thenM` \ bndrs ->
376 tcExtendGlobalValEnv bndrs $
379 tcCoreBinder (CoreDecl nm ty _ _)
380 = tcIfaceType ty `thenM` \ ty' ->
381 returnM (mkLocalId nm ty')
383 tcCoreBind (CoreDecl nm _ rhs _)
384 = tcVar nm `thenM` \ id ->
385 tcCoreExpr rhs `thenM` \ rhs' ->
391 ifaceSigCtxt sig_name
392 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]