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 ( HsDecl(..), IfaceSig(..), HsTupCon(..) )
13 import TcMonoType ( tcHsType, tcHsTypeKind,
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
19 import TcEnv ( ValueEnv, tcExtendTyVarEnv,
20 tcExtendGlobalValEnv, tcSetValueEnv,
22 explicitLookupValue, badCon, badPrimOp, valueEnvIds
24 import TcType ( TcKind, kindToTcKind )
26 import RnHsSyn ( RenamedHsDecl )
28 import Literal ( Literal(..) )
30 import CoreUtils ( exprType )
32 import CoreLint ( lintUnfolding )
33 import WorkWrap ( mkWrapper )
34 import PrimOp ( PrimOp(..) )
36 import Id ( Id, mkId, mkVanillaId,
39 import MkId ( mkCCallOpId )
41 import DataCon ( dataConSig, dataConArgTys )
42 import Type ( mkSynTy, mkTyVarTys, splitAlgTyConApp, splitAlgTyConApp_maybe, splitFunTys, unUsgTy )
43 import Var ( mkTyVar, tyVarKind )
45 import Name ( Name, NamedThing(..), isLocallyDefined )
46 import TysWiredIn ( integerTy, stringTy )
47 import Demand ( wwLazy )
48 import ErrUtils ( pprBagOfErrors )
49 import Maybes ( maybeToBool, MaybeErr(..) )
51 import Util ( zipWithEqual )
54 Ultimately, type signatures in interfaces will have pragmatic
55 information attached, so it is a good idea to have separate code to
58 As always, we do not have to worry about user-pragmas in interface
62 tcInterfaceSigs :: ValueEnv -- Envt to use when checking unfoldings
63 -> [RenamedHsDecl] -- Ignore non-sig-decls in these decls
67 tcInterfaceSigs unf_env decls
68 = listTc [ do_one name ty id_infos src_loc
69 | SigD (IfaceSig name ty id_infos src_loc) <- decls]
71 in_scope_vars = filter isLocallyDefined (valueEnvIds unf_env)
73 do_one name ty id_infos src_loc
74 = tcAddSrcLoc src_loc $
75 tcAddErrCtxt (ifaceSigCtxt name) $
76 tcHsType ty `thenTc` \ sigma_ty ->
77 tcIdInfo unf_env in_scope_vars name
78 sigma_ty vanillaIdInfo id_infos `thenTc` \ id_info ->
79 returnTc (mkId name sigma_ty id_info)
83 tcIdInfo unf_env in_scope_vars name ty info info_ins
84 = foldlTc tcPrag vanillaIdInfo info_ins
86 tcPrag info (HsArity arity) = returnTc (info `setArityInfo` arity)
87 tcPrag info (HsUpdate upd) = returnTc (info `setUpdateInfo` upd)
88 tcPrag info (HsNoCafRefs) = returnTc (info `setCafInfo` NoCafRefs)
89 tcPrag info HsCprInfo = returnTc (info `setCprInfo` ReturnsCPR)
91 tcPrag info (HsUnfold inline_prag expr)
92 = tcPragExpr unf_env name in_scope_vars expr `thenNF_Tc` \ maybe_expr' ->
94 -- maybe_expr doesn't get looked at if the unfolding
95 -- is never inspected; so the typecheck doesn't even happen
96 unfold_info = case maybe_expr' of
97 Nothing -> noUnfolding
98 Just expr' -> mkTopUnfolding expr'
99 info1 = info `setUnfoldingInfo` unfold_info
100 info2 = info1 `setInlinePragInfo` inline_prag
104 tcPrag info (HsStrictness strict_info)
105 = returnTc (info `setStrictnessInfo` strict_info)
107 tcPrag info (HsWorker nm)
108 = tcWorkerInfo unf_env ty info nm
112 tcWorkerInfo unf_env ty info worker_name
113 | not (hasArity arity_info)
114 = pprPanic "Worker with no arity info" (ppr worker_name)
117 = uniqSMToTcM (mkWrapper ty arity demands res_bot cpr_info) `thenNF_Tc` \ wrap_fn ->
119 -- Watch out! We can't pull on unf_env too eagerly!
120 info' = case explicitLookupValue unf_env worker_name of
121 Just worker_id -> info `setUnfoldingInfo` mkTopUnfolding (wrap_fn worker_id)
122 `setWorkerInfo` HasWorker worker_id arity
124 Nothing -> pprTrace "tcWorkerInfo failed:" (ppr worker_name) info
128 -- We are relying here on arity, cpr and strictness info always appearing
129 -- before worker info, fingers crossed ....
130 arity_info = arityInfo info
131 arity = arityLowerBound arity_info
132 cpr_info = cprInfo info
133 (demands, res_bot) = case strictnessInfo info of
134 StrictnessInfo d r -> (d,r)
135 _ -> (take arity (repeat wwLazy),False) -- Noncommittal
138 For unfoldings we try to do the job lazily, so that we never type check
139 an unfolding that isn't going to be looked at.
142 tcPragExpr unf_env name in_scope_vars expr
143 = tcDelay unf_env doc $
144 tcCoreExpr expr `thenTc` \ core_expr' ->
146 -- Check for type consistency in the unfolding
147 tcGetSrcLoc `thenNF_Tc` \ src_loc ->
148 case lintUnfolding src_loc in_scope_vars core_expr' of
149 (Nothing,_) -> returnTc core_expr' -- ignore warnings
150 (Just fail_msg,_) -> failWithTc ((doc <+> text "failed Lint") $$ fail_msg)
152 doc = text "unfolding of" <+> ppr name
154 tcDelay :: ValueEnv -> SDoc -> TcM s a -> NF_TcM s (Maybe a)
155 tcDelay unf_env doc thing_inside
157 recoverNF_Tc bad_value (
158 tcSetValueEnv unf_env thing_inside `thenTc` \ r ->
162 -- The trace tells what wasn't available, for the benefit of
163 -- compiler hackers who want to improve it!
164 bad_value = getErrsTc `thenNF_Tc` \ (warns,errs) ->
165 returnNF_Tc (pprTrace "Failed:"
166 (hang doc 4 (pprBagOfErrors errs))
171 Variables in unfoldings
172 ~~~~~~~~~~~~~~~~~~~~~~~
173 ****** Inside here we use only the Global environment, even for locally bound variables.
174 ****** Why? Because we know all the types and want to bind them to real Ids.
177 tcVar :: Name -> TcM s Id
179 = tcLookupValueMaybe name `thenNF_Tc` \ maybe_id ->
181 Just id -> returnTc id;
182 Nothing -> failWithTc (noDecl name)
185 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
191 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
193 tcCoreExpr (UfType ty)
194 = tcHsTypeKind ty `thenTc` \ (_, ty') ->
195 -- It might not be of kind type
198 tcCoreExpr (UfVar name)
199 = tcVar name `thenTc` \ id ->
202 tcCoreExpr (UfLit lit)
205 -- The dreaded lit-lits are also similar, except here the type
206 -- is read in explicitly rather than being implicit
207 tcCoreExpr (UfLitLit lit ty)
208 = tcHsType ty `thenTc` \ ty' ->
209 returnTc (Lit (MachLitLit lit ty'))
211 tcCoreExpr (UfCCall cc ty)
212 = tcHsType ty `thenTc` \ ty' ->
213 tcGetUnique `thenNF_Tc` \ u ->
214 returnTc (Var (mkCCallOpId u cc ty'))
216 tcCoreExpr (UfTuple (HsTupCon name _) args)
217 = tcVar name `thenTc` \ con_id ->
218 mapTc tcCoreExpr args `thenTc` \ args' ->
220 -- Put the missing type arguments back in
221 con_args = map (Type . unUsgTy . exprType) args' ++ args'
223 returnTc (mkApps (Var con_id) con_args)
225 tcCoreExpr (UfLam bndr body)
226 = tcCoreLamBndr bndr $ \ bndr' ->
227 tcCoreExpr body `thenTc` \ body' ->
228 returnTc (Lam bndr' body')
230 tcCoreExpr (UfApp fun arg)
231 = tcCoreExpr fun `thenTc` \ fun' ->
232 tcCoreExpr arg `thenTc` \ arg' ->
233 returnTc (App fun' arg')
235 tcCoreExpr (UfCase scrut case_bndr alts)
236 = tcCoreExpr scrut `thenTc` \ scrut' ->
238 scrut_ty = exprType scrut'
239 case_bndr' = mkVanillaId case_bndr scrut_ty
241 tcExtendGlobalValEnv [case_bndr'] $
242 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
243 returnTc (Case scrut' case_bndr' alts')
245 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
246 = tcCoreExpr rhs `thenTc` \ rhs' ->
247 tcCoreValBndr bndr $ \ bndr' ->
248 tcCoreExpr body `thenTc` \ body' ->
249 returnTc (Let (NonRec bndr' rhs') body')
251 tcCoreExpr (UfLet (UfRec pairs) body)
252 = tcCoreValBndrs bndrs $ \ bndrs' ->
253 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
254 tcCoreExpr body `thenTc` \ body' ->
255 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
257 (bndrs, rhss) = unzip pairs
259 tcCoreExpr (UfNote note expr)
260 = tcCoreExpr expr `thenTc` \ expr' ->
262 UfCoerce to_ty -> tcHsType to_ty `thenTc` \ to_ty' ->
263 returnTc (Note (Coerce (unUsgTy to_ty')
264 (unUsgTy (exprType expr'))) expr')
265 UfInlineCall -> returnTc (Note InlineCall expr')
266 UfInlineMe -> returnTc (Note InlineMe expr')
267 UfSCC cc -> returnTc (Note (SCC cc) expr')
269 tcCoreNote (UfSCC cc) = returnTc (SCC cc)
270 tcCoreNote UfInlineCall = returnTc InlineCall
274 tcCoreLamBndr (UfValBinder name ty) thing_inside
275 = tcHsType ty `thenTc` \ ty' ->
277 id = mkVanillaId name ty'
279 tcExtendGlobalValEnv [id] $
282 tcCoreLamBndr (UfTyBinder name kind) thing_inside
284 tyvar = mkTyVar name kind
286 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
288 tcCoreLamBndrs [] thing_inside = thing_inside []
289 tcCoreLamBndrs (b:bs) thing_inside
290 = tcCoreLamBndr b $ \ b' ->
291 tcCoreLamBndrs bs $ \ bs' ->
292 thing_inside (b':bs')
294 tcCoreValBndr (UfValBinder name ty) thing_inside
295 = tcHsType ty `thenTc` \ ty' ->
297 id = mkVanillaId name ty'
299 tcExtendGlobalValEnv [id] $
302 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
303 = mapTc tcHsType tys `thenTc` \ tys' ->
305 ids = zipWithEqual "tcCoreValBndr" mkVanillaId names tys'
307 tcExtendGlobalValEnv ids $
310 names = [name | UfValBinder name _ <- bndrs]
311 tys = [ty | UfValBinder _ ty <- bndrs]
315 tcCoreAlt scrut_ty (UfDefault, names, rhs)
316 = ASSERT( null names )
317 tcCoreExpr rhs `thenTc` \ rhs' ->
318 returnTc (DEFAULT, [], rhs')
320 tcCoreAlt scrut_ty (UfLitAlt lit, names, rhs)
321 = ASSERT( null names )
322 tcCoreExpr rhs `thenTc` \ rhs' ->
323 returnTc (LitAlt lit, [], rhs')
325 tcCoreAlt scrut_ty (UfLitLitAlt str ty, names, rhs)
326 = ASSERT( null names )
327 tcCoreExpr rhs `thenTc` \ rhs' ->
328 tcHsType ty `thenTc` \ ty' ->
329 returnTc (LitAlt (MachLitLit str ty'), [], rhs')
331 -- A case alternative is made quite a bit more complicated
332 -- by the fact that we omit type annotations because we can
333 -- work them out. True enough, but its not that easy!
334 tcCoreAlt scrut_ty alt@(UfDataAlt con_name, names, rhs)
335 = tcVar con_name `thenTc` \ con_id ->
337 con = case isDataConWrapId_maybe con_id of
339 Nothing -> pprPanic "tcCoreAlt" (ppr con_id)
341 (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
343 (_, inst_tys, cons) = case splitAlgTyConApp_maybe scrut_ty of
345 Nothing -> pprPanic "tcCoreAlt" (ppr alt)
346 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
347 ex_tys' = mkTyVarTys ex_tyvars'
348 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
349 id_names = drop (length ex_tyvars) names
352 | length id_names /= length arg_tys
353 = pprPanic "tcCoreAlts" (ppr (con_name, names, rhs) $$
354 (ppr main_tyvars <+> ppr ex_tyvars) $$
358 = zipWithEqual "tcCoreAlts" mkVanillaId id_names arg_tys
360 ASSERT( con `elem` cons && length inst_tys == length main_tyvars )
361 tcExtendTyVarEnv ex_tyvars' $
362 tcExtendGlobalValEnv arg_ids $
363 tcCoreExpr rhs `thenTc` \ rhs' ->
364 returnTc (DataAlt con, ex_tyvars' ++ arg_ids, rhs')
368 ifaceSigCtxt sig_name
369 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]