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 )
19 import TcMonoType ( tcIfaceType, kcHsSigType )
20 import TcEnv ( RecTcGblEnv, tcExtendTyVarEnv,
22 tcLookupGlobal_maybe, tcLookupRecId_maybe
25 import RnHsSyn ( RenamedCoreDecl, RenamedTyClDecl )
27 import Literal ( Literal(..) )
29 import CoreUtils ( exprType )
31 import CoreLint ( lintUnfolding )
32 import WorkWrap ( mkWrapper )
34 import Id ( Id, mkVanillaGlobal, mkLocalId, isDataConWrapId_maybe )
35 import MkId ( mkFCallId )
37 import TyCon ( tyConDataCons, tyConTyVars )
38 import DataCon ( DataCon, dataConWorkId, dataConExistentialTyVars, dataConArgTys )
39 import Type ( mkTyVarTys, splitTyConApp )
40 import TysWiredIn ( tupleCon )
41 import Var ( mkTyVar, tyVarKind )
43 import UniqSupply ( initUs_ )
45 import Util ( zipWithEqual, dropList, equalLength )
46 import HscTypes ( TyThing(..), typeEnvIds )
47 import CmdLineOpts ( DynFlag(..) )
50 Ultimately, type signatures in interfaces will have pragmatic
51 information attached, so it is a good idea to have separate code to
54 As always, we do not have to worry about user-pragmas in interface
58 tcInterfaceSigs :: RecTcGblEnv -- Envt to use when checking unfoldings
59 -> [RenamedTyClDecl] -- Ignore non-sig-decls in these decls
63 tcInterfaceSigs unf_env decls
64 = sequenceM [ do_one name ty id_infos src_loc
65 | IfaceSig {tcdName = name, tcdType = ty,
66 tcdIdInfo = id_infos, tcdLoc =src_loc} <- decls]
68 in_scope_vars = typeEnvIds (tcg_type_env unf_env)
69 -- When we have hi-boot files, an unfolding might refer to
70 -- something defined in this module, so we must build a
71 -- suitable in-scope set. This thunk will only be poked
72 -- if -dcore-lint is on.
74 do_one name ty id_infos src_loc
76 addErrCtxt (ifaceSigCtxt name) $
77 tcIfaceType ty `thenM` \ sigma_ty ->
78 tcIdInfo unf_env in_scope_vars name
79 sigma_ty id_infos `thenM` \ id_info ->
80 returnM (mkVanillaGlobal name sigma_ty id_info)
84 tcIdInfo unf_env in_scope_vars name ty info_ins
85 = foldlM tcPrag init_info info_ins
87 -- Set the CgInfo to something sensible but uninformative before
88 -- we start; default assumption is that it has CAFs
89 init_info = hasCafIdInfo
91 tcPrag info (HsNoCafRefs) = returnM (info `setCafInfo` NoCafRefs)
93 tcPrag info (HsArity arity) =
94 returnM (info `setArityInfo` arity)
96 tcPrag info (HsUnfold inline_prag expr)
97 = tcPragExpr unf_env name in_scope_vars expr `thenM` \ maybe_expr' ->
99 -- maybe_expr doesn't get looked at if the unfolding
100 -- is never inspected; so the typecheck doesn't even happen
101 unfold_info = case maybe_expr' of
102 Nothing -> noUnfolding
103 Just expr' -> mkTopUnfolding expr'
104 info1 = info `setUnfoldingInfo` unfold_info
105 info2 = info1 `setInlinePragInfo` inline_prag
109 tcPrag info (HsStrictness strict_info)
110 = returnM (info `setAllStrictnessInfo` Just strict_info)
112 tcPrag info (HsWorker nm arity)
113 = tcWorkerInfo unf_env ty info nm arity
117 tcWorkerInfo unf_env ty info worker_name arity
118 = newUniqueSupply `thenM` \ us ->
120 wrap_fn = initUs_ us (mkWrapper ty strict_sig)
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
148 tcCoreExpr expr `thenM` \ core_expr' ->
150 -- Check for type consistency in the unfolding
151 ifOptM Opt_DoCoreLinting (
152 getSrcLocM `thenM` \ src_loc ->
153 case lintUnfolding src_loc in_scope_vars core_expr' of
154 Nothing -> returnM ()
155 Just fail_msg -> failWithTc ((doc <+> text "Failed Lint") $$ fail_msg)
160 doc = text "unfolding of" <+> ppr name
164 Variables in unfoldings
165 ~~~~~~~~~~~~~~~~~~~~~~~
166 ****** Inside here we use only the Global environment, even for locally bound variables.
167 ****** Why? Because we know all the types and want to bind them to real Ids.
170 tcVar :: Name -> TcM Id
172 = tcLookupGlobal_maybe name `thenM` \ maybe_id ->
174 Just (AnId id) -> returnM id ;
175 Nothing -> failWithTc (noDecl name)
178 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
184 tcCoreExpr :: UfExpr Name -> TcM CoreExpr
186 tcCoreExpr (UfType ty)
187 = tcIfaceType ty `thenM` \ ty' ->
188 -- It might not be of kind type
191 tcCoreExpr (UfVar name)
192 = tcVar name `thenM` \ id ->
195 tcCoreExpr (UfLit lit)
198 -- The dreaded lit-lits are also similar, except here the type
199 -- is read in explicitly rather than being implicit
200 tcCoreExpr (UfLitLit lit ty)
201 = tcIfaceType ty `thenM` \ ty' ->
202 returnM (Lit (MachLitLit lit ty'))
204 tcCoreExpr (UfFCall cc ty)
205 = tcIfaceType ty `thenM` \ ty' ->
206 newUnique `thenM` \ u ->
207 returnM (Var (mkFCallId u cc ty'))
209 tcCoreExpr (UfTuple (HsTupCon boxity arity) args)
210 = mappM tcCoreExpr args `thenM` \ args' ->
212 -- Put the missing type arguments back in
213 con_args = map (Type . exprType) args' ++ args'
215 returnM (mkApps (Var con_id) con_args)
217 con_id = dataConWorkId (tupleCon boxity arity)
220 tcCoreExpr (UfLam bndr body)
221 = tcCoreLamBndr bndr $ \ bndr' ->
222 tcCoreExpr body `thenM` \ body' ->
223 returnM (Lam bndr' body')
225 tcCoreExpr (UfApp fun arg)
226 = tcCoreExpr fun `thenM` \ fun' ->
227 tcCoreExpr arg `thenM` \ arg' ->
228 returnM (App fun' arg')
230 tcCoreExpr (UfCase scrut case_bndr alts)
231 = tcCoreExpr scrut `thenM` \ scrut' ->
233 scrut_ty = exprType scrut'
234 case_bndr' = mkLocalId case_bndr scrut_ty
236 tcExtendGlobalValEnv [case_bndr'] $
237 mappM (tcCoreAlt scrut_ty) alts `thenM` \ alts' ->
238 returnM (Case scrut' case_bndr' alts')
240 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
241 = tcCoreExpr rhs `thenM` \ rhs' ->
242 tcCoreValBndr bndr $ \ bndr' ->
243 tcCoreExpr body `thenM` \ body' ->
244 returnM (Let (NonRec bndr' rhs') body')
246 tcCoreExpr (UfLet (UfRec pairs) body)
247 = tcCoreValBndrs bndrs $ \ bndrs' ->
248 mappM tcCoreExpr rhss `thenM` \ rhss' ->
249 tcCoreExpr body `thenM` \ body' ->
250 returnM (Let (Rec (bndrs' `zip` rhss')) body')
252 (bndrs, rhss) = unzip pairs
254 tcCoreExpr (UfNote note expr)
255 = tcCoreExpr expr `thenM` \ expr' ->
257 UfCoerce to_ty -> tcIfaceType to_ty `thenM` \ to_ty' ->
258 returnM (Note (Coerce to_ty'
259 (exprType expr')) expr')
260 UfInlineCall -> returnM (Note InlineCall expr')
261 UfInlineMe -> returnM (Note InlineMe expr')
262 UfSCC cc -> returnM (Note (SCC cc) expr')
266 tcCoreLamBndr (UfValBinder name ty) thing_inside
267 = tcIfaceType ty `thenM` \ ty' ->
269 id = mkLocalId name ty'
271 tcExtendGlobalValEnv [id] $
274 tcCoreLamBndr (UfTyBinder name kind) thing_inside
276 tyvar = mkTyVar name kind
278 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
280 tcCoreLamBndrs [] thing_inside = thing_inside []
281 tcCoreLamBndrs (b:bs) thing_inside
282 = tcCoreLamBndr b $ \ b' ->
283 tcCoreLamBndrs bs $ \ bs' ->
284 thing_inside (b':bs')
286 tcCoreValBndr (UfValBinder name ty) thing_inside
287 = tcIfaceType ty `thenM` \ ty' ->
289 id = mkLocalId name ty'
291 tcExtendGlobalValEnv [id] $
294 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
295 = mappM tcIfaceType tys `thenM` \ tys' ->
297 ids = zipWithEqual "tcCoreValBndr" mkLocalId names tys'
299 tcExtendGlobalValEnv ids $
302 names = [name | UfValBinder name _ <- bndrs]
303 tys = [ty | UfValBinder _ ty <- bndrs]
307 tcCoreAlt scrut_ty (UfDefault, names, rhs)
308 = ASSERT( null names )
309 tcCoreExpr rhs `thenM` \ rhs' ->
310 returnM (DEFAULT, [], rhs')
312 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
313 = ASSERT( null names )
314 tcCoreExpr rhs `thenM` \ rhs' ->
315 returnM (LitAlt lit, [], rhs')
317 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
318 = ASSERT( null names )
319 tcCoreExpr rhs `thenM` \ rhs' ->
320 tcIfaceType ty `thenM` \ ty' ->
321 returnM (LitAlt (MachLitLit str ty'), [], rhs')
323 -- A case alternative is made quite a bit more complicated
324 -- by the fact that we omit type annotations because we can
325 -- work them out. True enough, but its not that easy!
326 tcCoreAlt scrut_ty alt@(con, names, rhs)
327 = tcConAlt con `thenM` \ con ->
329 ex_tyvars = dataConExistentialTyVars con
330 (tycon, inst_tys) = splitTyConApp scrut_ty -- NB: not tcSplitTyConApp
331 -- We are looking at Core here
332 main_tyvars = tyConTyVars tycon
333 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
334 ex_tys' = mkTyVarTys ex_tyvars'
335 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
336 id_names = dropList ex_tyvars names
339 | not (equalLength id_names arg_tys)
340 = pprPanic "tcCoreAlts" (ppr (con, names, rhs) $$
341 (ppr main_tyvars <+> ppr ex_tyvars) $$
345 = zipWithEqual "tcCoreAlts" mkLocalId id_names arg_tys
347 ASSERT( con `elem` tyConDataCons tycon && equalLength inst_tys main_tyvars )
348 tcExtendTyVarEnv ex_tyvars' $
349 tcExtendGlobalValEnv arg_ids $
350 tcCoreExpr rhs `thenM` \ rhs' ->
351 returnM (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
354 tcConAlt :: UfConAlt Name -> TcM DataCon
355 tcConAlt (UfTupleAlt (HsTupCon boxity arity))
356 = returnM (tupleCon boxity arity)
358 tcConAlt (UfDataAlt con_name)
359 = tcVar con_name `thenM` \ con_id ->
360 returnM (case isDataConWrapId_maybe con_id of
362 Nothing -> pprPanic "tcCoreAlt" (ppr con_id))
365 %************************************************************************
367 \subsection{Core decls}
369 %************************************************************************
373 tcCoreBinds :: [RenamedCoreDecl] -> TcM [TypecheckedCoreBind]
374 -- We don't assume the bindings are in dependency order
375 -- So first build the environment, then check the RHSs
376 tcCoreBinds ls = mappM tcCoreBinder ls `thenM` \ bndrs ->
377 tcExtendGlobalValEnv bndrs $
378 mappM (tcCoreBind bndrs) ls
380 tcCoreBinder (CoreDecl nm ty _ _)
381 = kcHsSigType ty `thenM_`
382 tcIfaceType ty `thenM` \ ty' ->
383 returnM (mkLocalId nm ty')
385 tcCoreBind bndrs (CoreDecl nm _ rhs loc)
386 = tcVar nm `thenM` \ id ->
387 tcCoreExpr rhs `thenM` \ rhs' ->
389 mb_err = lintUnfolding loc bndrs rhs'
392 Just err -> addErr err
393 Nothing -> returnM ()) `thenM_`
400 ifaceSigCtxt sig_name
401 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]