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, tcVar, tcCoreExpr, tcCoreLamBndrs ) where
9 #include "HsVersions.h"
11 import HsSyn ( TyClDecl(..), HsTupCon(..) )
13 import TcMonoType ( tcHsType )
14 -- NB: all the tyars in interface files are kinded,
15 -- so tcHsType will do the Right Thing without
16 -- having to mess about with zonking
18 import TcEnv ( TcEnv, RecTcEnv, tcExtendTyVarEnv,
19 tcExtendGlobalValEnv, tcSetEnv,
20 tcLookupGlobal_maybe, tcLookupRecId_maybe
23 import RnHsSyn ( RenamedTyClDecl )
25 import Literal ( Literal(..) )
27 import CoreUtils ( exprType )
29 import CoreLint ( lintUnfolding )
30 import WorkWrap ( mkWrapper )
32 import Id ( Id, mkId, mkVanillaId, isDataConWrapId_maybe )
33 import MkId ( mkCCallOpId )
35 import DataCon ( DataCon, dataConId, dataConSig, dataConArgTys )
36 import Type ( mkTyVarTys, splitAlgTyConApp_maybe )
37 import TysWiredIn ( tupleCon )
38 import Var ( mkTyVar, tyVarKind )
40 import Demand ( wwLazy )
41 import ErrUtils ( pprBagOfErrors )
43 import Util ( zipWithEqual )
44 import HscTypes ( TyThing(..) )
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 :: RecTcEnv -- Envt to use when checking unfoldings
56 -> [RenamedTyClDecl] -- Ignore non-sig-decls in these decls
60 tcInterfaceSigs unf_env decls
61 = listTc [ do_one name ty id_infos src_loc
62 | IfaceSig {tcdName = name, tcdType = ty, tcdIdInfo = id_infos, tcdLoc =src_loc} <- decls]
64 in_scope_vars = [] -- I think this will be OK
66 do_one name ty id_infos src_loc
67 = tcAddSrcLoc src_loc $
68 tcAddErrCtxt (ifaceSigCtxt name) $
69 tcHsType ty `thenTc` \ sigma_ty ->
70 tcIdInfo unf_env in_scope_vars name
71 sigma_ty id_infos `thenTc` \ id_info ->
72 returnTc (mkId name sigma_ty id_info)
76 tcIdInfo unf_env in_scope_vars name ty info_ins
77 = foldlTc tcPrag constantIdInfo info_ins
79 tcPrag info (HsArity arity) = returnTc (info `setArityInfo` arity)
80 tcPrag info (HsNoCafRefs) = returnTc (info `setCafInfo` NoCafRefs)
81 tcPrag info HsCprInfo = returnTc (info `setCprInfo` ReturnsCPR)
83 tcPrag info (HsUnfold inline_prag expr)
84 = tcPragExpr unf_env name in_scope_vars expr `thenNF_Tc` \ maybe_expr' ->
86 -- maybe_expr doesn't get looked at if the unfolding
87 -- is never inspected; so the typecheck doesn't even happen
88 unfold_info = case maybe_expr' of
89 Nothing -> noUnfolding
90 Just expr' -> mkTopUnfolding expr'
91 info1 = info `setUnfoldingInfo` unfold_info
92 info2 = info1 `setInlinePragInfo` inline_prag
96 tcPrag info (HsStrictness strict_info)
97 = returnTc (info `setStrictnessInfo` strict_info)
99 tcPrag info (HsWorker nm)
100 = tcWorkerInfo unf_env ty info nm
104 tcWorkerInfo unf_env ty info worker_name
105 | not (hasArity arity_info)
106 = pprPanic "Worker with no arity info" (ppr worker_name)
109 = uniqSMToTcM (mkWrapper ty arity demands res_bot cpr_info) `thenNF_Tc` \ wrap_fn ->
111 -- Watch out! We can't pull on unf_env too eagerly!
112 info' = case tcLookupRecId_maybe unf_env worker_name of
113 Just worker_id -> info `setUnfoldingInfo` mkTopUnfolding (wrap_fn worker_id)
114 `setWorkerInfo` HasWorker worker_id arity
116 Nothing -> pprTrace "tcWorkerInfo failed:" (ppr worker_name) info
120 -- We are relying here on arity, cpr and strictness info always appearing
121 -- before worker info, fingers crossed ....
122 arity_info = arityInfo info
123 arity = arityLowerBound arity_info
124 cpr_info = cprInfo info
125 (demands, res_bot) = case strictnessInfo info of
126 StrictnessInfo d r -> (d,r)
127 _ -> (take arity (repeat wwLazy),False) -- Noncommittal
130 For unfoldings we try to do the job lazily, so that we never type check
131 an unfolding that isn't going to be looked at.
134 tcPragExpr unf_env name in_scope_vars expr
135 = tcDelay unf_env doc $
136 tcCoreExpr expr `thenTc` \ core_expr' ->
138 -- Check for type consistency in the unfolding
139 tcGetSrcLoc `thenNF_Tc` \ src_loc ->
140 getDOptsTc `thenTc` \ dflags ->
141 case lintUnfolding dflags src_loc in_scope_vars core_expr' of
142 (Nothing,_) -> returnTc core_expr' -- ignore warnings
143 (Just fail_msg,_) -> failWithTc ((doc <+> text "failed Lint") $$ fail_msg)
145 doc = text "unfolding of" <+> ppr name
147 tcDelay :: RecTcEnv -> SDoc -> TcM a -> NF_TcM (Maybe a)
148 tcDelay unf_env doc thing_inside
150 recoverNF_Tc bad_value (
151 tcSetEnv unf_env thing_inside `thenTc` \ r ->
155 -- The trace tells what wasn't available, for the benefit of
156 -- compiler hackers who want to improve it!
157 bad_value = getErrsTc `thenNF_Tc` \ (warns,errs) ->
158 returnNF_Tc (pprTrace "Failed:"
159 (hang doc 4 (pprBagOfErrors errs))
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 `thenNF_Tc` \ maybe_id ->
174 Just (AnId id) -> returnTc 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 = tcHsType ty `thenTc` \ ty' ->
188 -- It might not be of kind type
191 tcCoreExpr (UfVar name)
192 = tcVar name `thenTc` \ 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 = tcHsType ty `thenTc` \ ty' ->
202 returnTc (Lit (MachLitLit lit ty'))
204 tcCoreExpr (UfCCall cc ty)
205 = tcHsType ty `thenTc` \ ty' ->
206 tcGetUnique `thenNF_Tc` \ u ->
207 returnTc (Var (mkCCallOpId u cc ty'))
209 tcCoreExpr (UfTuple (HsTupCon _ boxity arity) args)
210 = mapTc tcCoreExpr args `thenTc` \ args' ->
212 -- Put the missing type arguments back in
213 con_args = map (Type . exprType) args' ++ args'
215 returnTc (mkApps (Var con_id) con_args)
217 con_id = dataConId (tupleCon boxity arity)
220 tcCoreExpr (UfLam bndr body)
221 = tcCoreLamBndr bndr $ \ bndr' ->
222 tcCoreExpr body `thenTc` \ body' ->
223 returnTc (Lam bndr' body')
225 tcCoreExpr (UfApp fun arg)
226 = tcCoreExpr fun `thenTc` \ fun' ->
227 tcCoreExpr arg `thenTc` \ arg' ->
228 returnTc (App fun' arg')
230 tcCoreExpr (UfCase scrut case_bndr alts)
231 = tcCoreExpr scrut `thenTc` \ scrut' ->
233 scrut_ty = exprType scrut'
234 case_bndr' = mkVanillaId case_bndr scrut_ty
236 tcExtendGlobalValEnv [case_bndr'] $
237 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
238 returnTc (Case scrut' case_bndr' alts')
240 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
241 = tcCoreExpr rhs `thenTc` \ rhs' ->
242 tcCoreValBndr bndr $ \ bndr' ->
243 tcCoreExpr body `thenTc` \ body' ->
244 returnTc (Let (NonRec bndr' rhs') body')
246 tcCoreExpr (UfLet (UfRec pairs) body)
247 = tcCoreValBndrs bndrs $ \ bndrs' ->
248 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
249 tcCoreExpr body `thenTc` \ body' ->
250 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
252 (bndrs, rhss) = unzip pairs
254 tcCoreExpr (UfNote note expr)
255 = tcCoreExpr expr `thenTc` \ expr' ->
257 UfCoerce to_ty -> tcHsType to_ty `thenTc` \ to_ty' ->
258 returnTc (Note (Coerce to_ty'
259 (exprType expr')) expr')
260 UfInlineCall -> returnTc (Note InlineCall expr')
261 UfInlineMe -> returnTc (Note InlineMe expr')
262 UfSCC cc -> returnTc (Note (SCC cc) expr')
266 tcCoreLamBndr (UfValBinder name ty) thing_inside
267 = tcHsType ty `thenTc` \ ty' ->
269 id = mkVanillaId 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 = tcHsType ty `thenTc` \ ty' ->
289 id = mkVanillaId name ty'
291 tcExtendGlobalValEnv [id] $
294 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
295 = mapTc tcHsType tys `thenTc` \ tys' ->
297 ids = zipWithEqual "tcCoreValBndr" mkVanillaId 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 `thenTc` \ rhs' ->
310 returnTc (DEFAULT, [], rhs')
312 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
313 = ASSERT( null names )
314 tcCoreExpr rhs `thenTc` \ rhs' ->
315 returnTc (LitAlt lit, [], rhs')
317 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
318 = ASSERT( null names )
319 tcCoreExpr rhs `thenTc` \ rhs' ->
320 tcHsType ty `thenTc` \ ty' ->
321 returnTc (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 `thenTc` \ con ->
329 (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
331 (_, inst_tys, cons) = case splitAlgTyConApp_maybe scrut_ty of
333 Nothing -> pprPanic "tcCoreAlt" (ppr alt)
334 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
335 ex_tys' = mkTyVarTys ex_tyvars'
336 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
337 id_names = drop (length ex_tyvars) names
340 | length id_names /= length arg_tys
341 = pprPanic "tcCoreAlts" (ppr (con, names, rhs) $$
342 (ppr main_tyvars <+> ppr ex_tyvars) $$
346 = zipWithEqual "tcCoreAlts" mkVanillaId id_names arg_tys
348 ASSERT( con `elem` cons && length inst_tys == length main_tyvars )
349 tcExtendTyVarEnv ex_tyvars' $
350 tcExtendGlobalValEnv arg_ids $
351 tcCoreExpr rhs `thenTc` \ rhs' ->
352 returnTc (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
355 tcConAlt :: UfConAlt Name -> TcM DataCon
356 tcConAlt (UfTupleAlt (HsTupCon _ boxity arity))
357 = returnTc (tupleCon boxity arity)
359 tcConAlt (UfDataAlt con_name)
360 = tcVar con_name `thenTc` \ con_id ->
361 returnTc (case isDataConWrapId_maybe con_id of
363 Nothing -> pprPanic "tcCoreAlt" (ppr con_id))
367 ifaceSigCtxt sig_name
368 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]