2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcIfaceSig]{Type checking of type signatures in interface files}
7 #include "HsVersions.h"
9 module TcIfaceSig ( tcInterfaceSigs ) where
14 import TcMonoType ( tcHsType )
15 import TcEnv ( tcLookupGlobalValue, tcExtendTyVarEnv, tcExtendGlobalValEnv,
16 tcLookupTyConByKey, tcLookupGlobalValueMaybe, tcLookupLocalValue
18 import TcKind ( TcKind, kindToTcKind )
20 import HsSyn ( IfaceSig(..), HsDecl(..), TyDecl, ClassDecl, InstDecl, DefaultDecl, HsBinds,
22 import RnHsSyn ( RenamedHsDecl(..) )
24 import HsDecls ( HsIdInfo(..) )
25 import Literal ( Literal(..) )
28 import MagicUFs ( MagicUnfoldingFun )
29 import SpecEnv ( SpecEnv )
30 import PrimOp ( PrimOp(..) )
32 import Id ( GenId, mkImported, mkUserId, isPrimitiveId_maybe )
33 import Type ( mkSynTy )
34 import TyVar ( mkTyVar )
36 import Unique ( rationalTyConKey )
37 import TysWiredIn ( integerTy )
38 import PragmaInfo ( PragmaInfo(..) )
39 import ErrUtils ( pprBagOfErrors )
40 import Maybes ( maybeToBool )
42 import PprStyle ( PprStyle(..) )
43 import Util ( zipWithEqual, panic, pprTrace, pprPanic )
48 Ultimately, type signatures in interfaces will have pragmatic
49 information attached, so it is a good idea to have separate code to
52 As always, we do not have to worry about user-pragmas in interface
56 tcInterfaceSigs :: [RenamedHsDecl] -> TcM s [Id]
57 -- Ignore non-sig-decls in these decls
59 tcInterfaceSigs (SigD (IfaceSig name ty id_infos src_loc) : rest)
60 = tcAddSrcLoc src_loc $
61 tcHsType ty `thenTc` \ sigma_ty ->
62 tcIdInfo name noIdInfo id_infos `thenTc` \ id_info' ->
64 sig_id = mkImported name sigma_ty id_info'
66 tcInterfaceSigs rest `thenTc` \ sig_ids ->
67 returnTc (sig_id : sig_ids)
69 tcInterfaceSigs (other_decl : rest) = tcInterfaceSigs rest
71 tcInterfaceSigs [] = returnTc []
75 tcIdInfo name info [] = returnTc info
77 tcIdInfo name info (HsArity arity : rest)
78 = tcIdInfo name (info `addArityInfo` arity) rest
80 tcIdInfo name info (HsUpdate upd : rest)
81 = tcIdInfo name (info `addUpdateInfo` upd) rest
83 tcIdInfo name info (HsFBType fb : rest)
84 = tcIdInfo name (info `addFBTypeInfo` fb) rest
86 tcIdInfo name info (HsArgUsage au : rest)
87 = tcIdInfo name (info `addArgUsageInfo` au) rest
89 tcIdInfo name info (HsDeforest df : rest)
90 = tcIdInfo name (info `addDeforestInfo` df) rest
92 tcIdInfo name info (HsUnfold expr : rest)
93 = tcUnfolding name expr `thenNF_Tc` \ unfold_info ->
94 tcIdInfo name (info `addUnfoldInfo` unfold_info) rest
96 tcIdInfo name info (HsStrictness strict : rest)
97 = tcStrictness strict `thenTc` \ strict_info ->
98 tcIdInfo name (info `addStrictnessInfo` strict_info) rest
102 tcStrictness (StrictnessInfo demands (Just worker))
103 = tcWorker worker `thenNF_Tc` \ maybe_worker_id ->
104 returnTc (StrictnessInfo demands maybe_worker_id)
106 -- Boring to write these out, but the result type differe from the arg type...
107 tcStrictness (StrictnessInfo demands Nothing) = returnTc (StrictnessInfo demands Nothing)
108 tcStrictness NoStrictnessInfo = returnTc NoStrictnessInfo
109 tcStrictness BottomGuaranteed = returnTc BottomGuaranteed
114 = tcLookupGlobalValueMaybe worker `thenNF_Tc` \ maybe_worker_id ->
115 returnNF_Tc (trace_maybe maybe_worker_id)
117 -- The trace is so we can see what's getting dropped
118 trace_maybe Nothing = pprTrace "tcWorker failed:" (ppr PprDebug worker) Nothing
119 trace_maybe (Just x) = Just x
122 tcLookupGlobalValue worker
124 For unfoldings we try to do the job lazily, so that we never type check
125 an unfolding that isn't going to be looked at.
128 tcUnfolding name core_expr
130 recoverNF_Tc no_unfolding (
131 tcCoreExpr core_expr `thenTc` \ core_expr' ->
132 returnTc (mkUnfolding False core_expr')
135 -- The trace tells what wasn't available, for the benefit of
136 -- compiler hackers who want to improve it!
137 no_unfolding = getErrsTc `thenNF_Tc` \ (warns,errs) ->
138 returnNF_Tc (pprTrace "tcUnfolding failed with:"
139 (ppHang (ppr PprDebug name) 4 (pprBagOfErrors PprDebug errs))
144 Variables in unfoldings
145 ~~~~~~~~~~~~~~~~~~~~~~~
146 ****** Inside here we use only the Global environment, even for locally bound variables.
147 ****** Why? Because we know all the types and want to bind them to real Ids.
150 tcVar :: Name -> TcM s Id
152 = tcLookupGlobalValueMaybe name `thenNF_Tc` \ maybe_id ->
154 Just id -> returnTc id;
155 Nothing -> failTc (noDecl name)
158 noDecl name sty = ppCat [ppStr "Warning: no binding for", ppr sty name]
164 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
166 tcCoreExpr (UfVar name)
167 = tcVar name `thenTc` \ id ->
170 -- rationalTy isn't built in so we have to construct it
171 -- (the "ty" part of the incoming literal is simply bottom)
172 tcCoreExpr (UfLit (NoRepRational lit _))
173 = tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
175 rational_ty = mkSynTy rational_tycon []
177 returnTc (Lit (NoRepRational lit rational_ty))
179 -- Similarly for integers, except that it is wired in
180 tcCoreExpr (UfLit (NoRepInteger lit _))
181 = returnTc (Lit (NoRepInteger lit integerTy))
183 tcCoreExpr (UfLit other_lit)
184 = returnTc (Lit other_lit)
186 tcCoreExpr (UfCon con args)
187 = tcVar con `thenTc` \ con_id ->
188 mapTc tcCoreArg args `thenTc` \ args' ->
189 returnTc (Con con_id args')
191 tcCoreExpr (UfPrim prim args)
192 = tcCorePrim prim `thenTc` \ primop ->
193 mapTc tcCoreArg args `thenTc` \ args' ->
194 returnTc (Prim primop args')
196 tcCoreExpr (UfApp fun arg)
197 = tcCoreExpr fun `thenTc` \ fun' ->
198 tcCoreArg arg `thenTc` \ arg' ->
199 returnTc (App fun' arg')
201 tcCoreExpr (UfCase scrut alts)
202 = tcCoreExpr scrut `thenTc` \ scrut' ->
203 tcCoreAlts alts `thenTc` \ alts' ->
204 returnTc (Case scrut' alts')
206 tcCoreExpr (UfSCC cc expr)
207 = tcCoreExpr expr `thenTc` \ expr' ->
208 returnTc (SCC cc expr')
210 tcCoreExpr(UfCoerce coercion ty body)
211 = tcCoercion coercion `thenTc` \ coercion' ->
212 tcHsType ty `thenTc` \ ty' ->
213 tcCoreExpr body `thenTc` \ body' ->
214 returnTc (Coerce coercion' ty' body')
216 tcCoreExpr (UfLam bndr body)
217 = tcCoreLamBndr bndr $ \ bndr' ->
218 tcCoreExpr body `thenTc` \ body' ->
219 returnTc (Lam bndr' body')
221 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
222 = tcCoreExpr rhs `thenTc` \ rhs' ->
223 tcCoreValBndr bndr $ \ bndr' ->
224 tcCoreExpr body `thenTc` \ body' ->
225 returnTc (Let (NonRec bndr' rhs') body')
227 tcCoreExpr (UfLet (UfRec pairs) body)
228 = tcCoreValBndrs bndrs $ \ bndrs' ->
229 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
230 tcCoreExpr body `thenTc` \ body' ->
231 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
233 (bndrs, rhss) = unzip pairs
237 tcCoreLamBndr (UfValBinder name ty) thing_inside
238 = tcHsType ty `thenTc` \ ty' ->
240 id = mkUserId name ty' NoPragmaInfo
242 tcExtendGlobalValEnv [id] $
243 thing_inside (ValBinder id)
245 tcCoreLamBndr (UfTyBinder name kind) thing_inside
247 tyvar = mkTyVar name kind
249 tcExtendTyVarEnv [name] [(kindToTcKind kind, tyvar)] $
250 thing_inside (TyBinder tyvar)
252 tcCoreLamBndr (UfUsageBinder name) thing_inside
253 = error "tcCoreLamBndr: usage"
255 tcCoreValBndr (UfValBinder name ty) thing_inside
256 = tcHsType ty `thenTc` \ ty' ->
258 id = mkUserId name ty' NoPragmaInfo
260 tcExtendGlobalValEnv [id] $
263 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
264 = mapTc tcHsType tys `thenTc` \ tys' ->
266 ids = zipWithEqual "tcCoreValBndr" mk_id names tys'
267 mk_id name ty' = mkUserId name ty' NoPragmaInfo
269 tcExtendGlobalValEnv ids $
272 names = map (\ (UfValBinder name _) -> name) bndrs
273 tys = map (\ (UfValBinder _ ty) -> ty) bndrs
277 tcCoreArg (UfVarArg v) = tcVar v `thenTc` \ v' -> returnTc (VarArg v')
278 tcCoreArg (UfTyArg ty) = tcHsType ty `thenTc` \ ty' -> returnTc (TyArg ty')
279 tcCoreArg (UfLitArg lit) = returnTc (LitArg lit)
280 tcCoreArg (UfUsageArg u) = error "tcCoreArg: usage"
282 tcCoreAlts (UfAlgAlts alts deflt)
283 = mapTc tc_alt alts `thenTc` \ alts' ->
284 tcCoreDefault deflt `thenTc` \ deflt' ->
285 returnTc (AlgAlts alts' deflt')
287 tc_alt (con, bndrs, rhs) = tcVar con `thenTc` \ con' ->
288 tcCoreValBndrs bndrs $ \ bndrs' ->
289 tcCoreExpr rhs `thenTc` \ rhs' ->
290 returnTc (con', bndrs', rhs')
292 tcCoreAlts (UfPrimAlts alts deflt)
293 = mapTc tc_alt alts `thenTc` \ alts' ->
294 tcCoreDefault deflt `thenTc` \ deflt' ->
295 returnTc (PrimAlts alts' deflt')
297 tc_alt (lit, rhs) = tcCoreExpr rhs `thenTc` \ rhs' ->
300 tcCoreDefault UfNoDefault = returnTc NoDefault
301 tcCoreDefault (UfBindDefault bndr rhs) = tcCoreValBndr bndr $ \ bndr' ->
302 tcCoreExpr rhs `thenTc` \ rhs' ->
303 returnTc (BindDefault bndr' rhs')
305 tcCoercion (UfIn n) = tcVar n `thenTc` \ n' -> returnTc (CoerceIn n')
306 tcCoercion (UfOut n) = tcVar n `thenTc` \ n' -> returnTc (CoerceOut n')
308 tcCorePrim (UfOtherOp op)
309 = tcVar op `thenTc` \ op_id ->
310 case isPrimitiveId_maybe op_id of
311 Just prim_op -> returnTc prim_op
312 Nothing -> pprPanic "tcCorePrim" (ppr PprDebug op_id)
314 tcCorePrim (UfCCallOp str casm gc arg_tys res_ty)
315 = mapTc tcHsType arg_tys `thenTc` \ arg_tys' ->
316 tcHsType res_ty `thenTc` \ res_ty' ->
317 returnTc (CCallOp str casm gc arg_tys' res_ty')