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,
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,
21 tcLookupTyConByKey, tcLookupValueMaybe,
22 explicitLookupValue, badCon, badPrimOp
24 import TcType ( TcKind, kindToTcKind )
26 import RnHsSyn ( RenamedHsDecl )
28 import HsDecls ( HsIdInfo(..), HsStrictnessInfo(..) )
29 import CallConv ( cCallConv )
30 import Const ( Con(..), Literal(..) )
32 import CoreUtils ( coreExprType )
34 import CoreLint ( lintUnfolding )
35 import WwLib ( mkWrapper )
36 import PrimOp ( PrimOp(..) )
38 import Id ( Id, mkImportedId, mkUserId,
39 isPrimitiveId_maybe, isDataConId_maybe
42 import DataCon ( dataConSig, dataConArgTys )
43 import SpecEnv ( addToSpecEnv )
44 import Type ( mkSynTy, mkTyVarTys, splitAlgTyConApp )
45 import Var ( IdOrTyVar, mkTyVar, tyVarKind )
47 import Name ( Name, NamedThing(..) )
48 import Unique ( rationalTyConKey )
49 import TysWiredIn ( integerTy, stringTy )
50 import ErrUtils ( pprBagOfErrors )
51 import Maybes ( maybeToBool, MaybeErr(..) )
53 import Util ( zipWithEqual )
56 Ultimately, type signatures in interfaces will have pragmatic
57 information attached, so it is a good idea to have separate code to
60 As always, we do not have to worry about user-pragmas in interface
64 tcInterfaceSigs :: ValueEnv -- Envt to use when checking unfoldings
65 -> [RenamedHsDecl] -- Ignore non-sig-decls in these decls
69 tcInterfaceSigs unf_env (SigD (IfaceSig name ty id_infos src_loc) : rest)
70 = tcAddSrcLoc src_loc (
71 tcAddErrCtxt (ifaceSigCtxt name) (
72 tcHsType ty `thenTc` \ sigma_ty ->
73 tcIdInfo unf_env name sigma_ty noIdInfo id_infos `thenTc` \ id_info ->
74 returnTc (mkImportedId name sigma_ty id_info)
75 )) `thenTc` \ sig_id ->
76 tcInterfaceSigs unf_env rest `thenTc` \ sig_ids ->
77 returnTc (sig_id : sig_ids)
79 tcInterfaceSigs unf_env (other_decl : rest) = tcInterfaceSigs unf_env rest
81 tcInterfaceSigs unf_env [] = returnTc []
85 tcIdInfo unf_env name ty info info_ins
86 = foldlTc tcPrag noIdInfo info_ins
88 tcPrag info (HsArity arity) = returnTc (arity `setArityInfo` info)
89 tcPrag info (HsUpdate upd) = returnTc (upd `setUpdateInfo` info)
90 tcPrag info (HsNoCafRefs) = returnTc (NoCafRefs `setCafInfo` info)
92 tcPrag info (HsUnfold inline_prag maybe_expr)
94 Just expr -> tcPragExpr unf_env name [] expr
95 Nothing -> returnNF_Tc Nothing
96 ) `thenNF_Tc` \ maybe_expr' ->
98 -- maybe_expr doesn't get looked at if the unfolding
99 -- is never inspected; so the typecheck doesn't even happen
100 unfold_info = case maybe_expr' of
101 Nothing -> NoUnfolding
102 Just expr' -> mkUnfolding expr'
103 info1 = unfold_info `setUnfoldingInfo` info
105 info2 = inline_prag `setInlinePragInfo` info1
109 tcPrag info (HsStrictness strict)
110 = tcStrictness unf_env ty info strict
112 tcPrag info (HsSpecialise tyvars tys rhs)
113 = tcExtendTyVarScope tyvars $ \ tyvars' ->
114 mapAndUnzipTc tcHsTypeKind tys `thenTc` \ (kinds, tys') ->
115 -- Assume that the kinds match the kinds of the
116 -- type variables of the function; this is, after all, an
117 -- interface file generated by the compiler!
119 tcPragExpr unf_env name tyvars' rhs `thenNF_Tc` \ maybe_rhs' ->
121 -- If spec_env isn't looked at, none of this
122 -- actually takes place
123 spec_env = specInfo info
124 spec_env' = case maybe_rhs' of
126 Just rhs' -> case addToSpecEnv True {- overlap ok -} spec_env tyvars' tys' rhs' of
127 Succeeded spec_env' -> spec_env'
128 Failed err -> pprTrace "tcIdInfo: bad specialisation"
129 (ppr name <+> ppr err) $
132 returnTc (spec_env' `setSpecInfo` info)
136 tcStrictness unf_env ty info (HsStrictnessInfo (demands, bot_result) maybe_worker)
137 = tcWorker unf_env maybe_worker `thenNF_Tc` \ maybe_worker_id ->
138 uniqSMToTcM (mkWrapper ty demands) `thenNF_Tc` \ wrap_fn ->
140 -- Watch out! We can't pull on maybe_worker_id too eagerly!
141 info' = case maybe_worker_id of
142 Just worker_id -> setUnfoldingInfo (mkUnfolding (wrap_fn worker_id)) $
143 setInlinePragInfo IWantToBeINLINEd info
147 has_worker = maybeToBool maybe_worker_id
149 returnTc (StrictnessInfo demands bot_result has_worker `setStrictnessInfo` info')
153 tcWorker unf_env Nothing = returnNF_Tc Nothing
155 tcWorker unf_env (Just (worker_name,_))
156 = returnNF_Tc (trace_maybe maybe_worker_id)
158 maybe_worker_id = explicitLookupValue unf_env worker_name
160 -- The trace is so we can see what's getting dropped
161 trace_maybe Nothing = pprTrace "tcWorker failed:" (ppr worker_name) Nothing
162 trace_maybe (Just x) = Just x
165 For unfoldings we try to do the job lazily, so that we never type check
166 an unfolding that isn't going to be looked at.
169 tcPragExpr :: ValueEnv -> Name -> [IdOrTyVar] -> UfExpr Name -> NF_TcM s (Maybe CoreExpr)
170 tcPragExpr unf_env name in_scope_vars core_expr
172 recoverNF_Tc no_unfolding (
173 tcSetValueEnv unf_env $
174 tcCoreExpr core_expr `thenTc` \ core_expr' ->
176 -- Check for type consistency in the unfolding
177 tcGetSrcLoc `thenNF_Tc` \ src_loc ->
178 returnTc (lintUnfolding src_loc in_scope_vars core_expr')
181 -- The trace tells what wasn't available, for the benefit of
182 -- compiler hackers who want to improve it!
183 no_unfolding = getErrsTc `thenNF_Tc` \ (warns,errs) ->
184 returnNF_Tc (pprTrace "tcUnfolding failed with:"
185 (hang (ppr name) 4 (pprBagOfErrors errs))
190 Variables in unfoldings
191 ~~~~~~~~~~~~~~~~~~~~~~~
192 ****** Inside here we use only the Global environment, even for locally bound variables.
193 ****** Why? Because we know all the types and want to bind them to real Ids.
196 tcVar :: Name -> TcM s Id
198 = tcLookupValueMaybe name `thenNF_Tc` \ maybe_id ->
200 Just id -> returnTc id;
201 Nothing -> failWithTc (noDecl name)
204 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
210 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
212 tcCoreExpr (UfType ty)
213 = tcHsTypeKind ty `thenTc` \ (_, ty') ->
214 -- It might not be of kind type
217 tcCoreExpr (UfVar name)
218 = tcVar name `thenTc` \ id ->
221 tcCoreExpr (UfCon con args)
222 = tcUfCon con `thenTc` \ con' ->
223 mapTc tcCoreExpr args `thenTc` \ args' ->
224 returnTc (Con con' args')
226 tcCoreExpr (UfTuple name args)
227 = tcUfDataCon name `thenTc` \ con ->
228 mapTc tcCoreExpr args `thenTc` \ args' ->
230 -- Put the missing type arguments back in
231 con_args = map (Type . coreExprType) args' ++ args'
233 returnTc (Con con con_args)
235 tcCoreExpr (UfLam bndr body)
236 = tcCoreLamBndr bndr $ \ bndr' ->
237 tcCoreExpr body `thenTc` \ body' ->
238 returnTc (Lam bndr' body')
240 tcCoreExpr (UfApp fun arg)
241 = tcCoreExpr fun `thenTc` \ fun' ->
242 tcCoreExpr arg `thenTc` \ arg' ->
243 returnTc (App fun' arg')
245 tcCoreExpr (UfCase scrut case_bndr alts)
246 = tcCoreExpr scrut `thenTc` \ scrut' ->
248 scrut_ty = coreExprType scrut'
249 case_bndr' = mkUserId case_bndr scrut_ty
251 tcExtendGlobalValEnv [case_bndr'] $
252 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
253 returnTc (Case scrut' case_bndr' alts')
255 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
256 = tcCoreExpr rhs `thenTc` \ rhs' ->
257 tcCoreValBndr bndr $ \ bndr' ->
258 tcCoreExpr body `thenTc` \ body' ->
259 returnTc (Let (NonRec bndr' rhs') body')
261 tcCoreExpr (UfLet (UfRec pairs) body)
262 = tcCoreValBndrs bndrs $ \ bndrs' ->
263 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
264 tcCoreExpr body `thenTc` \ body' ->
265 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
267 (bndrs, rhss) = unzip pairs
269 tcCoreExpr (UfNote note expr)
270 = tcCoreExpr expr `thenTc` \ expr' ->
272 UfCoerce to_ty -> tcHsType to_ty `thenTc` \ to_ty' ->
273 returnTc (Note (Coerce to_ty' (coreExprType expr')) expr')
274 UfInlineCall -> returnTc (Note InlineCall expr')
275 UfSCC cc -> returnTc (Note (SCC cc) expr')
277 tcCoreNote (UfSCC cc) = returnTc (SCC cc)
278 tcCoreNote UfInlineCall = returnTc InlineCall
281 -- rationalTy isn't built in so, we have to construct it
282 -- (the "ty" part of the incoming literal is simply bottom)
283 tcUfCon (UfLitCon (NoRepRational lit _))
284 = tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
286 rational_ty = mkSynTy rational_tycon []
288 returnTc (Literal (NoRepRational lit rational_ty))
290 -- Similarly for integers and strings, except that they are wired in
291 tcUfCon (UfLitCon (NoRepInteger lit _))
292 = returnTc (Literal (NoRepInteger lit integerTy))
293 tcUfCon (UfLitCon (NoRepStr lit _))
294 = returnTc (Literal (NoRepStr lit stringTy))
296 tcUfCon (UfLitCon other_lit)
297 = returnTc (Literal other_lit)
299 -- The dreaded lit-lits are also similar, except here the type
300 -- is read in explicitly rather than being implicit
301 tcUfCon (UfLitLitCon lit ty)
302 = tcHsType ty `thenTc` \ ty' ->
303 returnTc (Literal (MachLitLit lit ty'))
305 tcUfCon (UfDataCon name) = tcUfDataCon name
307 tcUfCon (UfPrimOp name)
308 = tcVar name `thenTc` \ op_id ->
309 case isPrimitiveId_maybe op_id of
310 Just op -> returnTc (PrimOp op)
311 Nothing -> failWithTc (badPrimOp name)
313 tcUfCon (UfCCallOp str is_dyn casm gc)
316 tcGetUnique `thenNF_Tc` \ u ->
317 returnTc (PrimOp (CCallOp (Right u) casm gc cCallConv))
318 False -> returnTc (PrimOp (CCallOp (Left str) casm gc cCallConv))
321 = tcVar name `thenTc` \ con_id ->
322 case isDataConId_maybe con_id of
323 Just con -> returnTc (DataCon con)
324 Nothing -> failWithTc (badCon name)
328 tcCoreLamBndr (UfValBinder name ty) thing_inside
329 = tcHsType ty `thenTc` \ ty' ->
331 id = mkUserId name ty'
333 tcExtendGlobalValEnv [id] $
336 tcCoreLamBndr (UfTyBinder name kind) thing_inside
338 tyvar = mkTyVar name kind
340 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
342 tcCoreValBndr (UfValBinder name ty) thing_inside
343 = tcHsType ty `thenTc` \ ty' ->
345 id = mkUserId name ty'
347 tcExtendGlobalValEnv [id] $
350 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
351 = mapTc tcHsType tys `thenTc` \ tys' ->
353 ids = zipWithEqual "tcCoreValBndr" mkUserId names tys'
355 tcExtendGlobalValEnv ids $
358 names = [name | UfValBinder name _ <- bndrs]
359 tys = [ty | UfValBinder _ ty <- bndrs]
363 tcCoreAlt scrut_ty (UfDefault, names, rhs)
364 = ASSERT( null names )
365 tcCoreExpr rhs `thenTc` \ rhs' ->
366 returnTc (DEFAULT, [], rhs')
368 tcCoreAlt scrut_ty (UfLitCon lit, names, rhs)
369 = ASSERT( null names )
370 tcCoreExpr rhs `thenTc` \ rhs' ->
371 returnTc (Literal lit, [], rhs')
373 tcCoreAlt scrut_ty (UfLitLitCon str ty, names, rhs)
374 = ASSERT( null names )
375 tcCoreExpr rhs `thenTc` \ rhs' ->
376 tcHsType ty `thenTc` \ ty' ->
377 returnTc (Literal (MachLitLit str ty'), [], rhs')
379 -- A case alternative is made quite a bit more complicated
380 -- by the fact that we omit type annotations because we can
381 -- work them out. True enough, but its not that easy!
382 tcCoreAlt scrut_ty (UfDataCon con_name, names, rhs)
383 = tcVar con_name `thenTc` \ con_id ->
385 con = case isDataConId_maybe con_id of
387 Nothing -> pprPanic "tcCoreAlt" (ppr con_id)
389 (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
391 (tycon, inst_tys, cons) = splitAlgTyConApp scrut_ty
392 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
393 ex_tys' = mkTyVarTys ex_tyvars'
394 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
395 id_names = drop (length ex_tyvars) names
398 | length id_names /= length arg_tys
399 = pprPanic "tcCoreAlts" (ppr (con_name, names, rhs) $$
400 (ppr main_tyvars <+> ppr ex_tyvars) $$
404 = zipWithEqual "tcCoreAlts" mkUserId id_names arg_tys
406 ASSERT( con `elem` cons && length inst_tys == length main_tyvars )
407 tcExtendTyVarEnv ex_tyvars' $
408 tcExtendGlobalValEnv arg_ids $
409 tcCoreExpr rhs `thenTc` \ rhs' ->
410 returnTc (DataCon con, ex_tyvars' ++ arg_ids, rhs')
414 ifaceSigCtxt sig_name
415 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]