2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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,
15 tcLookupTyConByKey, tcLookupGlobalValueMaybe,
16 tcExplicitLookupGlobal
18 import TcKind ( TcKind, kindToTcKind )
20 import RnHsSyn ( RenamedHsDecl(..) )
22 import HsDecls ( HsIdInfo(..), HsStrictnessInfo(..) )
23 import Literal ( Literal(..) )
25 import CoreUtils ( coreExprType )
27 import MagicUFs ( MagicUnfoldingFun )
28 import WwLib ( mkWrapper )
29 import PrimOp ( PrimOp(..) )
31 import MkId ( mkImportedId, mkUserId )
32 import Id ( Id, addInlinePragma, isPrimitiveId_maybe, dataConArgTys )
34 import SpecEnv ( addToSpecEnv )
35 import Type ( mkSynTy, splitAlgTyConApp )
36 import TyVar ( mkSysTyVar )
38 import Unique ( rationalTyConKey, uniqueOf )
39 import TysWiredIn ( integerTy )
40 import ErrUtils ( pprBagOfErrors )
41 import Maybes ( maybeToBool, MaybeErr(..) )
43 import Util ( zipWithEqual )
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 :: TcEnv s -- Envt to use when checking unfoldings
56 -> [RenamedHsDecl] -- Ignore non-sig-decls in these decls
60 tcInterfaceSigs unf_env (SigD (IfaceSig name ty id_infos src_loc) : rest)
61 = tcAddSrcLoc src_loc (
62 tcAddErrCtxt (ifaceSigCtxt name) (
63 tcHsType ty `thenTc` \ sigma_ty ->
64 tcIdInfo unf_env name sigma_ty noIdInfo id_infos `thenTc` \ id_info ->
65 returnTc (mkImportedId name sigma_ty id_info)
66 )) `thenTc` \ sig_id ->
67 tcInterfaceSigs unf_env rest `thenTc` \ sig_ids ->
68 returnTc (sig_id : sig_ids)
70 tcInterfaceSigs unf_env (other_decl : rest) = tcInterfaceSigs unf_env rest
72 tcInterfaceSigs unf_env [] = returnTc []
76 tcIdInfo unf_env name ty info info_ins
77 = foldlTc tcPrag noIdInfo info_ins
79 tcPrag info (HsArity arity) = returnTc (arity `setArityInfo` info)
80 tcPrag info (HsUpdate upd) = returnTc (upd `setUpdateInfo` info)
81 tcPrag info (HsFBType fb) = returnTc (fb `setFBTypeInfo` info)
82 tcPrag info (HsArgUsage au) = returnTc (au `setArgUsageInfo` info)
84 tcPrag info (HsUnfold inline expr)
85 = tcPragExpr unf_env name expr `thenNF_Tc` \ maybe_expr' ->
87 -- maybe_expr doesn't get looked at if the unfolding
88 -- is never inspected; so the typecheck doesn't even happen
89 unfold_info = case maybe_expr' of
90 Nothing -> NoUnfolding
91 Just expr' -> mkUnfolding expr'
92 info1 = unfold_info `setUnfoldingInfo` info
94 info2 | inline = IWantToBeINLINEd `setInlinePragInfo` info1
99 tcPrag info (HsStrictness strict)
100 = tcStrictness unf_env ty info strict
102 tcPrag info (HsSpecialise tyvars tys rhs)
103 = tcTyVarScope tyvars $ \ tyvars' ->
104 mapTc tcHsType tys `thenTc` \ tys' ->
105 tcPragExpr unf_env name rhs `thenNF_Tc` \ maybe_rhs' ->
107 -- If spec_env isn't looked at, none of this
108 -- actually takes place
109 spec_env = specInfo info
110 spec_env' = case maybe_rhs' of
112 Just rhs' -> case addToSpecEnv True {- overlap ok -} spec_env tyvars' tys' rhs' of
113 Succeeded spec_env' -> spec_env'
114 Failed err -> pprTrace "tcIdInfo: bad specialisation"
115 (ppr name <+> ppr err) $
118 returnTc (spec_env' `setSpecInfo` info)
122 tcStrictness unf_env ty info (HsStrictnessInfo demands maybe_worker)
123 = tcWorker unf_env maybe_worker `thenNF_Tc` \ maybe_worker_id ->
124 uniqSMToTcM (mkWrapper ty demands) `thenNF_Tc` \ wrap_fn ->
126 -- Watch out! We can't pull on maybe_worker_id too eagerly!
127 info' = case maybe_worker_id of
128 Just worker_id -> setUnfoldingInfo (mkUnfolding (wrap_fn worker_id)) $
129 setInlinePragInfo IWantToBeINLINEd info
133 has_worker = maybeToBool maybe_worker_id
135 returnTc (StrictnessInfo demands has_worker `setStrictnessInfo` info')
137 -- Boring to write these out, but the result type differs from the arg type...
138 tcStrictness unf_env ty info HsBottom
139 = returnTc (BottomGuaranteed `setStrictnessInfo` info)
143 tcWorker unf_env Nothing = returnNF_Tc Nothing
145 tcWorker unf_env (Just (worker_name,_))
146 = returnNF_Tc (trace_maybe maybe_worker_id)
148 maybe_worker_id = tcExplicitLookupGlobal unf_env worker_name
150 -- The trace is so we can see what's getting dropped
151 trace_maybe Nothing = pprTrace "tcWorker failed:" (ppr worker_name) Nothing
152 trace_maybe (Just x) = Just x
155 For unfoldings we try to do the job lazily, so that we never type check
156 an unfolding that isn't going to be looked at.
159 tcPragExpr unf_env name core_expr
161 recoverNF_Tc no_unfolding (
163 tcCoreExpr core_expr `thenTc` \ core_expr' ->
164 returnTc (Just core_expr')
167 -- The trace tells what wasn't available, for the benefit of
168 -- compiler hackers who want to improve it!
169 no_unfolding = getErrsTc `thenNF_Tc` \ (warns,errs) ->
170 returnNF_Tc (pprTrace "tcUnfolding failed with:"
171 (hang (ppr name) 4 (pprBagOfErrors errs))
176 Variables in unfoldings
177 ~~~~~~~~~~~~~~~~~~~~~~~
178 ****** Inside here we use only the Global environment, even for locally bound variables.
179 ****** Why? Because we know all the types and want to bind them to real Ids.
182 tcVar :: Name -> TcM s Id
184 = tcLookupGlobalValueMaybe name `thenNF_Tc` \ maybe_id ->
186 Just id -> returnTc id;
187 Nothing -> failWithTc (noDecl name)
190 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
196 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
198 tcCoreExpr (UfVar name)
199 = tcVar name `thenTc` \ id ->
202 -- rationalTy isn't built in so we have to construct it
203 -- (the "ty" part of the incoming literal is simply bottom)
204 tcCoreExpr (UfLit (NoRepRational lit _))
205 = tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
207 rational_ty = mkSynTy rational_tycon []
209 returnTc (Lit (NoRepRational lit rational_ty))
211 -- Similarly for integers, except that it is wired in
212 tcCoreExpr (UfLit (NoRepInteger lit _))
213 = returnTc (Lit (NoRepInteger lit integerTy))
215 tcCoreExpr (UfLit other_lit)
216 = returnTc (Lit other_lit)
218 tcCoreExpr (UfCon con args)
219 = tcVar con `thenTc` \ con_id ->
220 mapTc tcCoreArg args `thenTc` \ args' ->
221 returnTc (Con con_id args')
223 tcCoreExpr (UfPrim prim args)
224 = tcCorePrim prim `thenTc` \ primop ->
225 mapTc tcCoreArg args `thenTc` \ args' ->
226 returnTc (Prim primop args')
228 tcCoreExpr (UfLam bndr body)
229 = tcCoreLamBndr bndr $ \ bndr' ->
230 tcCoreExpr body `thenTc` \ body' ->
231 returnTc (Lam bndr' body')
233 tcCoreExpr (UfApp fun arg)
234 = tcCoreExpr fun `thenTc` \ fun' ->
235 tcCoreArg arg `thenTc` \ arg' ->
236 returnTc (App fun' arg')
238 tcCoreExpr (UfCase scrut alts)
239 = tcCoreExpr scrut `thenTc` \ scrut' ->
240 tcCoreAlts (coreExprType scrut') alts `thenTc` \ alts' ->
241 returnTc (Case scrut' alts')
243 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
244 = tcCoreExpr rhs `thenTc` \ rhs' ->
245 tcCoreValBndr bndr $ \ bndr' ->
246 tcCoreExpr body `thenTc` \ body' ->
247 returnTc (Let (NonRec bndr' rhs') body')
249 tcCoreExpr (UfLet (UfRec pairs) body)
250 = tcCoreValBndrs bndrs $ \ bndrs' ->
251 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
252 tcCoreExpr body `thenTc` \ body' ->
253 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
255 (bndrs, rhss) = unzip pairs
257 tcCoreExpr (UfNote note expr)
258 = tcCoreExpr expr `thenTc` \ expr' ->
260 UfCoerce to_ty -> tcHsTypeKind to_ty `thenTc` \ (_,to_ty') ->
261 returnTc (Note (Coerce to_ty' (coreExprType expr')) expr')
262 UfInlineCall -> returnTc (Note InlineCall expr')
263 UfSCC cc -> returnTc (Note (SCC cc) expr')
265 tcCoreNote (UfSCC cc) = returnTc (SCC cc)
266 tcCoreNote UfInlineCall = returnTc InlineCall
270 tcCoreLamBndr (UfValBinder name ty) thing_inside
271 = tcHsType ty `thenTc` \ ty' ->
273 id = mkUserId name ty'
275 tcExtendGlobalValEnv [id] $
276 thing_inside (ValBinder id)
278 tcCoreLamBndr (UfTyBinder name kind) thing_inside
280 tyvar = mkSysTyVar (uniqueOf name) kind
282 tcExtendTyVarEnv [name] [(kindToTcKind kind, tyvar)] $
283 thing_inside (TyBinder tyvar)
285 tcCoreValBndr (UfValBinder name ty) thing_inside
286 = tcHsType ty `thenTc` \ ty' ->
288 id = mkUserId name ty'
290 tcExtendGlobalValEnv [id] $
293 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
294 = mapTc tcHsType tys `thenTc` \ tys' ->
296 ids = zipWithEqual "tcCoreValBndr" mkUserId names tys'
298 tcExtendGlobalValEnv ids $
301 names = map (\ (UfValBinder name _) -> name) bndrs
302 tys = map (\ (UfValBinder _ ty) -> ty) bndrs
306 tcCoreArg (UfVarArg v) = tcVar v `thenTc` \ v' -> returnTc (VarArg v')
307 tcCoreArg (UfTyArg ty) = tcHsTypeKind ty `thenTc` \ (_,ty') -> returnTc (TyArg ty')
308 tcCoreArg (UfLitArg lit) = returnTc (LitArg lit)
310 tcCoreAlts scrut_ty (UfAlgAlts alts deflt)
311 = mapTc tc_alt alts `thenTc` \ alts' ->
312 tcCoreDefault scrut_ty deflt `thenTc` \ deflt' ->
313 returnTc (AlgAlts alts' deflt')
315 tc_alt (con, names, rhs)
316 = tcVar con `thenTc` \ con' ->
318 arg_tys = dataConArgTys con' inst_tys
319 (tycon, inst_tys, cons) = splitAlgTyConApp scrut_ty
320 arg_ids = zipWithEqual "tcCoreAlts" mkUserId names arg_tys
322 tcExtendGlobalValEnv arg_ids $
323 tcCoreExpr rhs `thenTc` \ rhs' ->
324 returnTc (con', arg_ids, rhs')
326 tcCoreAlts scrut_ty (UfPrimAlts alts deflt)
327 = mapTc tc_alt alts `thenTc` \ alts' ->
328 tcCoreDefault scrut_ty deflt `thenTc` \ deflt' ->
329 returnTc (PrimAlts alts' deflt')
331 tc_alt (lit, rhs) = tcCoreExpr rhs `thenTc` \ rhs' ->
334 tcCoreDefault scrut_ty UfNoDefault = returnTc NoDefault
335 tcCoreDefault scrut_ty (UfBindDefault name rhs)
337 deflt_id = mkUserId name scrut_ty
339 tcExtendGlobalValEnv [deflt_id] $
340 tcCoreExpr rhs `thenTc` \ rhs' ->
341 returnTc (BindDefault deflt_id rhs')
344 tcCorePrim (UfOtherOp op)
345 = tcVar op `thenTc` \ op_id ->
346 case isPrimitiveId_maybe op_id of
347 Just prim_op -> returnTc prim_op
348 Nothing -> pprPanic "tcCorePrim" (ppr op_id)
350 tcCorePrim (UfCCallOp str casm gc arg_tys res_ty)
351 = mapTc tcHsType arg_tys `thenTc` \ arg_tys' ->
352 tcHsType res_ty `thenTc` \ res_ty' ->
353 returnTc (CCallOp str casm gc arg_tys' res_ty')
357 ifaceSigCtxt sig_name
358 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]