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 ) where
9 #include "HsVersions.h"
11 import HsSyn ( HsDecl(..), IfaceSig(..) )
13 import TcMonoType ( tcHsType, tcHsTypeKind, tcTyVarScope )
14 import TcEnv ( tcExtendTyVarEnv, tcExtendGlobalValEnv, tcSetGlobalValEnv,
15 tcLookupTyConByKey, tcLookupGlobalValueMaybe,
16 tcExplicitLookupGlobal, badCon, badPrimOp,
19 import TcType ( TcKind, kindToTcKind )
21 import RnHsSyn ( RenamedHsDecl )
23 import HsDecls ( HsIdInfo(..), HsStrictnessInfo(..) )
24 import CallConv ( cCallConv )
25 import Const ( Con(..), Literal(..) )
27 import CoreUtils ( coreExprType )
29 import WwLib ( mkWrapper )
30 import PrimOp ( PrimOp(..) )
32 import Id ( Id, mkImportedId, mkUserId,
33 isPrimitiveId_maybe, isDataConId_maybe
36 import DataCon ( dataConSig, dataConArgTys )
37 import SpecEnv ( addToSpecEnv )
38 import Type ( mkSynTy, mkTyVarTys, splitAlgTyConApp )
39 import Var ( mkTyVar, tyVarKind )
41 import Name ( Name, NamedThing(..) )
42 import Unique ( rationalTyConKey )
43 import TysWiredIn ( integerTy, stringTy )
44 import ErrUtils ( pprBagOfErrors )
45 import Maybes ( maybeToBool, MaybeErr(..) )
47 import Util ( zipWithEqual )
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 :: GlobalValueEnv -- Envt to use when checking unfoldings
59 -> [RenamedHsDecl] -- Ignore non-sig-decls in these decls
63 tcInterfaceSigs unf_env (SigD (IfaceSig name ty id_infos src_loc) : rest)
64 = tcAddSrcLoc src_loc (
65 tcAddErrCtxt (ifaceSigCtxt name) (
66 tcHsType ty `thenTc` \ sigma_ty ->
67 tcIdInfo unf_env name sigma_ty noIdInfo id_infos `thenTc` \ id_info ->
68 returnTc (mkImportedId name sigma_ty id_info)
69 )) `thenTc` \ sig_id ->
70 tcInterfaceSigs unf_env rest `thenTc` \ sig_ids ->
71 returnTc (sig_id : sig_ids)
73 tcInterfaceSigs unf_env (other_decl : rest) = tcInterfaceSigs unf_env rest
75 tcInterfaceSigs unf_env [] = returnTc []
79 tcIdInfo unf_env name ty info info_ins
80 = foldlTc tcPrag noIdInfo info_ins
82 tcPrag info (HsArity arity) = returnTc (arity `setArityInfo` info)
83 tcPrag info (HsUpdate upd) = returnTc (upd `setUpdateInfo` info)
84 tcPrag info (HsNoCafRefs) = returnTc (NoCafRefs `setCafInfo` info)
86 tcPrag info (HsUnfold inline_prag maybe_expr)
88 Just expr -> tcPragExpr unf_env name expr
89 Nothing -> returnNF_Tc Nothing
90 ) `thenNF_Tc` \ maybe_expr' ->
92 -- maybe_expr doesn't get looked at if the unfolding
93 -- is never inspected; so the typecheck doesn't even happen
94 unfold_info = case maybe_expr' of
95 Nothing -> NoUnfolding
96 Just expr' -> mkUnfolding expr'
97 info1 = unfold_info `setUnfoldingInfo` info
99 info2 = inline_prag `setInlinePragInfo` info1
103 tcPrag info (HsStrictness strict)
104 = tcStrictness unf_env ty info strict
106 tcPrag info (HsSpecialise tyvars tys rhs)
107 = tcTyVarScope tyvars $ \ tyvars' ->
108 mapAndUnzipTc tcHsTypeKind tys `thenTc` \ (kinds, tys') ->
109 -- Assume that the kinds match the kinds of the
110 -- type variables of the function; this is, after all, an
111 -- interface file generated by the compiler!
113 tcPragExpr unf_env name rhs `thenNF_Tc` \ maybe_rhs' ->
115 -- If spec_env isn't looked at, none of this
116 -- actually takes place
117 spec_env = specInfo info
118 spec_env' = case maybe_rhs' of
120 Just rhs' -> case addToSpecEnv True {- overlap ok -} spec_env tyvars' tys' rhs' of
121 Succeeded spec_env' -> spec_env'
122 Failed err -> pprTrace "tcIdInfo: bad specialisation"
123 (ppr name <+> ppr err) $
126 returnTc (spec_env' `setSpecInfo` info)
130 tcStrictness unf_env ty info (HsStrictnessInfo demands maybe_worker)
131 = tcWorker unf_env maybe_worker `thenNF_Tc` \ maybe_worker_id ->
132 uniqSMToTcM (mkWrapper ty demands) `thenNF_Tc` \ wrap_fn ->
134 -- Watch out! We can't pull on maybe_worker_id too eagerly!
135 info' = case maybe_worker_id of
136 Just worker_id -> setUnfoldingInfo (mkUnfolding (wrap_fn worker_id)) $
137 setInlinePragInfo IWantToBeINLINEd info
141 has_worker = maybeToBool maybe_worker_id
143 returnTc (StrictnessInfo demands has_worker `setStrictnessInfo` info')
145 -- Boring to write these out, but the result type differs from the arg type...
146 tcStrictness unf_env ty info HsBottom
147 = returnTc (BottomGuaranteed `setStrictnessInfo` info)
151 tcWorker unf_env Nothing = returnNF_Tc Nothing
153 tcWorker unf_env (Just (worker_name,_))
154 = returnNF_Tc (trace_maybe maybe_worker_id)
156 maybe_worker_id = tcExplicitLookupGlobal unf_env worker_name
158 -- The trace is so we can see what's getting dropped
159 trace_maybe Nothing = pprTrace "tcWorker failed:" (ppr worker_name) Nothing
160 trace_maybe (Just x) = Just x
163 For unfoldings we try to do the job lazily, so that we never type check
164 an unfolding that isn't going to be looked at.
167 tcPragExpr :: GlobalValueEnv -> Name -> UfExpr Name -> NF_TcM s (Maybe CoreExpr)
168 tcPragExpr unf_env name core_expr
170 recoverNF_Tc no_unfolding (
171 tcSetGlobalValEnv unf_env $
172 tcCoreExpr core_expr `thenTc` \ core_expr' ->
173 returnTc (Just core_expr')
176 -- The trace tells what wasn't available, for the benefit of
177 -- compiler hackers who want to improve it!
178 no_unfolding = getErrsTc `thenNF_Tc` \ (warns,errs) ->
179 returnNF_Tc (pprTrace "tcUnfolding failed with:"
180 (hang (ppr name) 4 (pprBagOfErrors errs))
185 Variables in unfoldings
186 ~~~~~~~~~~~~~~~~~~~~~~~
187 ****** Inside here we use only the Global environment, even for locally bound variables.
188 ****** Why? Because we know all the types and want to bind them to real Ids.
191 tcVar :: Name -> TcM s Id
193 = tcLookupGlobalValueMaybe name `thenNF_Tc` \ maybe_id ->
195 Just id -> returnTc id;
196 Nothing -> failWithTc (noDecl name)
199 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
205 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
207 tcCoreExpr (UfType ty)
208 = tcHsTypeKind ty `thenTc` \ (_, ty') ->
209 -- It might not be of kind type
212 tcCoreExpr (UfVar name)
213 = tcVar name `thenTc` \ id ->
216 tcCoreExpr (UfCon con args)
217 = tcUfCon con `thenTc` \ con' ->
218 mapTc tcCoreExpr args `thenTc` \ args' ->
219 returnTc (Con con' args')
221 tcCoreExpr (UfTuple name args)
222 = tcUfDataCon name `thenTc` \ con ->
223 mapTc tcCoreExpr args `thenTc` \ args' ->
225 -- Put the missing type arguments back in
226 con_args = map (Type . coreExprType) args' ++ args'
228 returnTc (Con con con_args)
230 tcCoreExpr (UfLam bndr body)
231 = tcCoreLamBndr bndr $ \ bndr' ->
232 tcCoreExpr body `thenTc` \ body' ->
233 returnTc (Lam bndr' body')
235 tcCoreExpr (UfApp fun arg)
236 = tcCoreExpr fun `thenTc` \ fun' ->
237 tcCoreExpr arg `thenTc` \ arg' ->
238 returnTc (App fun' arg')
240 tcCoreExpr (UfCase scrut case_bndr alts)
241 = tcCoreExpr scrut `thenTc` \ scrut' ->
243 scrut_ty = coreExprType scrut'
244 case_bndr' = mkUserId case_bndr scrut_ty
246 tcExtendGlobalValEnv [case_bndr'] $
247 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
248 returnTc (Case scrut' case_bndr' alts')
250 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
251 = tcCoreExpr rhs `thenTc` \ rhs' ->
252 tcCoreValBndr bndr $ \ bndr' ->
253 tcCoreExpr body `thenTc` \ body' ->
254 returnTc (Let (NonRec bndr' rhs') body')
256 tcCoreExpr (UfLet (UfRec pairs) body)
257 = tcCoreValBndrs bndrs $ \ bndrs' ->
258 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
259 tcCoreExpr body `thenTc` \ body' ->
260 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
262 (bndrs, rhss) = unzip pairs
264 tcCoreExpr (UfNote note expr)
265 = tcCoreExpr expr `thenTc` \ expr' ->
267 UfCoerce to_ty -> tcHsTypeKind to_ty `thenTc` \ (_,to_ty') ->
268 returnTc (Note (Coerce to_ty' (coreExprType expr')) expr')
269 UfInlineCall -> returnTc (Note InlineCall expr')
270 UfSCC cc -> returnTc (Note (SCC cc) expr')
272 tcCoreNote (UfSCC cc) = returnTc (SCC cc)
273 tcCoreNote UfInlineCall = returnTc InlineCall
276 -- rationalTy isn't built in so, we have to construct it
277 -- (the "ty" part of the incoming literal is simply bottom)
278 tcUfCon (UfLitCon (NoRepRational lit _))
279 = tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
281 rational_ty = mkSynTy rational_tycon []
283 returnTc (Literal (NoRepRational lit rational_ty))
285 -- Similarly for integers and strings, except that they are wired in
286 tcUfCon (UfLitCon (NoRepInteger lit _))
287 = returnTc (Literal (NoRepInteger lit integerTy))
288 tcUfCon (UfLitCon (NoRepStr lit _))
289 = returnTc (Literal (NoRepStr lit stringTy))
291 tcUfCon (UfLitCon other_lit)
292 = returnTc (Literal other_lit)
294 -- The dreaded lit-lits are also similar, except here the type
295 -- is read in explicitly rather than being implicit
296 tcUfCon (UfLitLitCon lit ty)
297 = tcHsType ty `thenTc` \ ty' ->
298 returnTc (Literal (MachLitLit lit ty'))
300 tcUfCon (UfDataCon name) = tcUfDataCon name
302 tcUfCon (UfPrimOp name)
303 = tcVar name `thenTc` \ op_id ->
304 case isPrimitiveId_maybe op_id of
305 Just op -> returnTc (PrimOp op)
306 Nothing -> failWithTc (badPrimOp name)
308 tcUfCon (UfCCallOp str casm gc)
309 = returnTc (PrimOp (CCallOp (Left str) casm gc cCallConv))
312 = tcVar name `thenTc` \ con_id ->
313 case isDataConId_maybe con_id of
314 Just con -> returnTc (DataCon con)
315 Nothing -> failWithTc (badCon name)
319 tcCoreLamBndr (UfValBinder name ty) thing_inside
320 = tcHsType ty `thenTc` \ ty' ->
322 id = mkUserId name ty'
324 tcExtendGlobalValEnv [id] $
327 tcCoreLamBndr (UfTyBinder name kind) thing_inside
329 tyvar = mkTyVar name kind
331 tcExtendTyVarEnv [name] [(kindToTcKind kind, tyvar)] $
334 tcCoreValBndr (UfValBinder name ty) thing_inside
335 = tcHsType ty `thenTc` \ ty' ->
337 id = mkUserId name ty'
339 tcExtendGlobalValEnv [id] $
342 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
343 = mapTc tcHsType tys `thenTc` \ tys' ->
345 ids = zipWithEqual "tcCoreValBndr" mkUserId names tys'
347 tcExtendGlobalValEnv ids $
350 names = [name | UfValBinder name _ <- bndrs]
351 tys = [ty | UfValBinder _ ty <- bndrs]
355 tcCoreAlt scrut_ty (UfDefault, names, rhs)
356 = ASSERT( null names )
357 tcCoreExpr rhs `thenTc` \ rhs' ->
358 returnTc (DEFAULT, [], rhs')
360 tcCoreAlt scrut_ty (UfLitCon lit, names, rhs)
361 = ASSERT( null names )
362 tcCoreExpr rhs `thenTc` \ rhs' ->
363 returnTc (Literal lit, [], rhs')
365 tcCoreAlt scrut_ty (UfLitLitCon str ty, names, rhs)
366 = ASSERT( null names )
367 tcCoreExpr rhs `thenTc` \ rhs' ->
368 tcHsType ty `thenTc` \ ty' ->
369 returnTc (Literal (MachLitLit str ty'), [], rhs')
371 -- A case alternative is made quite a bit more complicated
372 -- by the fact that we omit type annotations because we can
373 -- work them out. True enough, but its not that easy!
374 tcCoreAlt scrut_ty (UfDataCon con_name, names, rhs)
375 = tcVar con_name `thenTc` \ con_id ->
377 con = case isDataConId_maybe con_id of
379 Nothing -> pprPanic "tcCoreAlt" (ppr con_id)
381 (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
383 (tycon, inst_tys, cons) = splitAlgTyConApp scrut_ty
384 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
385 ex_tys' = mkTyVarTys ex_tyvars'
386 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
387 id_names = drop (length ex_tyvars) names
390 | length id_names /= length arg_tys
391 = pprPanic "tcCoreAlts" (ppr (con_name, names, rhs) $$
392 (ppr main_tyvars <+> ppr ex_tyvars) $$
396 = zipWithEqual "tcCoreAlts" mkUserId id_names arg_tys
398 ASSERT( con `elem` cons && length inst_tys == length main_tyvars )
399 tcExtendTyVarEnv (map getName ex_tyvars')
400 [ (kindToTcKind (tyVarKind tv), tv)
401 | tv <- ex_tyvars'] $
402 tcExtendGlobalValEnv arg_ids $
403 tcCoreExpr rhs `thenTc` \ rhs' ->
404 returnTc (DataCon con, ex_tyvars' ++ arg_ids, rhs')
408 ifaceSigCtxt sig_name
409 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]