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)
91 tcPrag info (HsCprInfo cpr_info) = returnTc (cpr_info `setCprInfo` info)
93 tcPrag info (HsUnfold inline_prag maybe_expr)
95 Just expr -> tcPragExpr unf_env name [] expr
96 Nothing -> returnNF_Tc Nothing
97 ) `thenNF_Tc` \ maybe_expr' ->
99 -- maybe_expr doesn't get looked at if the unfolding
100 -- is never inspected; so the typecheck doesn't even happen
101 unfold_info = case maybe_expr' of
102 Nothing -> NoUnfolding
103 Just expr' -> mkUnfolding expr'
104 info1 = unfold_info `setUnfoldingInfo` info
106 info2 = inline_prag `setInlinePragInfo` info1
110 tcPrag info (HsStrictness strict)
111 = tcStrictness unf_env ty info strict
113 tcPrag info (HsSpecialise tyvars tys rhs)
114 = tcExtendTyVarScope tyvars $ \ tyvars' ->
115 mapAndUnzipTc tcHsTypeKind tys `thenTc` \ (kinds, tys') ->
116 -- Assume that the kinds match the kinds of the
117 -- type variables of the function; this is, after all, an
118 -- interface file generated by the compiler!
120 tcPragExpr unf_env name tyvars' rhs `thenNF_Tc` \ maybe_rhs' ->
122 -- If spec_env isn't looked at, none of this
123 -- actually takes place
124 spec_env = specInfo info
125 spec_env' = case maybe_rhs' of
127 Just rhs' -> case addToSpecEnv True {- overlap ok -} spec_env tyvars' tys' rhs' of
128 Succeeded spec_env' -> spec_env'
129 Failed err -> pprTrace "tcIdInfo: bad specialisation"
130 (ppr name <+> ppr err) $
133 returnTc (spec_env' `setSpecInfo` info)
137 tcStrictness unf_env ty info (HsStrictnessInfo (demands, bot_result) maybe_worker)
138 = tcWorker unf_env maybe_worker `thenNF_Tc` \ maybe_worker_id ->
139 -- We are relying here on cpr info always appearing before strictness info
140 -- fingers crossed ....
141 uniqSMToTcM (mkWrapper ty demands (cprInfo info))
142 `thenNF_Tc` \ wrap_fn ->
144 -- Watch out! We can't pull on maybe_worker_id too eagerly!
145 info' = case maybe_worker_id of
146 Just worker_id -> setUnfoldingInfo (mkUnfolding (wrap_fn worker_id)) $
147 setWorkerInfo (Just worker_id) $
148 setInlinePragInfo IWantToBeINLINEd info
152 has_worker = maybeToBool maybe_worker_id
154 returnTc (StrictnessInfo demands bot_result `setStrictnessInfo` info')
158 tcWorker unf_env Nothing = returnNF_Tc Nothing
160 tcWorker unf_env (Just (worker_name,_))
161 = returnNF_Tc (trace_maybe maybe_worker_id)
163 maybe_worker_id = explicitLookupValue unf_env worker_name
165 -- The trace is so we can see what's getting dropped
166 trace_maybe Nothing = pprTrace "tcWorker failed:" (ppr worker_name) Nothing
167 trace_maybe (Just x) = Just x
170 For unfoldings we try to do the job lazily, so that we never type check
171 an unfolding that isn't going to be looked at.
174 tcPragExpr :: ValueEnv -> Name -> [IdOrTyVar] -> UfExpr Name -> NF_TcM s (Maybe CoreExpr)
175 tcPragExpr unf_env name in_scope_vars core_expr
177 recoverNF_Tc no_unfolding (
178 tcSetValueEnv unf_env $
179 tcCoreExpr core_expr `thenTc` \ core_expr' ->
181 -- Check for type consistency in the unfolding
182 tcGetSrcLoc `thenNF_Tc` \ src_loc ->
183 returnTc (lintUnfolding src_loc in_scope_vars core_expr')
186 -- The trace tells what wasn't available, for the benefit of
187 -- compiler hackers who want to improve it!
188 no_unfolding = getErrsTc `thenNF_Tc` \ (warns,errs) ->
189 returnNF_Tc (pprTrace "tcUnfolding failed with:"
190 (hang (ppr name) 4 (pprBagOfErrors errs))
195 Variables in unfoldings
196 ~~~~~~~~~~~~~~~~~~~~~~~
197 ****** Inside here we use only the Global environment, even for locally bound variables.
198 ****** Why? Because we know all the types and want to bind them to real Ids.
201 tcVar :: Name -> TcM s Id
203 = tcLookupValueMaybe name `thenNF_Tc` \ maybe_id ->
205 Just id -> returnTc id;
206 Nothing -> failWithTc (noDecl name)
209 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
215 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
217 tcCoreExpr (UfType ty)
218 = tcHsTypeKind ty `thenTc` \ (_, ty') ->
219 -- It might not be of kind type
222 tcCoreExpr (UfVar name)
223 = tcVar name `thenTc` \ id ->
226 tcCoreExpr (UfCon con args)
227 = tcUfCon con `thenTc` \ con' ->
228 mapTc tcCoreExpr args `thenTc` \ args' ->
229 returnTc (Con con' args')
231 tcCoreExpr (UfTuple name args)
232 = tcUfDataCon name `thenTc` \ con ->
233 mapTc tcCoreExpr args `thenTc` \ args' ->
235 -- Put the missing type arguments back in
236 con_args = map (Type . coreExprType) args' ++ args'
238 returnTc (Con con con_args)
240 tcCoreExpr (UfLam bndr body)
241 = tcCoreLamBndr bndr $ \ bndr' ->
242 tcCoreExpr body `thenTc` \ body' ->
243 returnTc (Lam bndr' body')
245 tcCoreExpr (UfApp fun arg)
246 = tcCoreExpr fun `thenTc` \ fun' ->
247 tcCoreExpr arg `thenTc` \ arg' ->
248 returnTc (App fun' arg')
250 tcCoreExpr (UfCase scrut case_bndr alts)
251 = tcCoreExpr scrut `thenTc` \ scrut' ->
253 scrut_ty = coreExprType scrut'
254 case_bndr' = mkUserId case_bndr scrut_ty
256 tcExtendGlobalValEnv [case_bndr'] $
257 mapTc (tcCoreAlt scrut_ty) alts `thenTc` \ alts' ->
258 returnTc (Case scrut' case_bndr' alts')
260 tcCoreExpr (UfLet (UfNonRec bndr rhs) body)
261 = tcCoreExpr rhs `thenTc` \ rhs' ->
262 tcCoreValBndr bndr $ \ bndr' ->
263 tcCoreExpr body `thenTc` \ body' ->
264 returnTc (Let (NonRec bndr' rhs') body')
266 tcCoreExpr (UfLet (UfRec pairs) body)
267 = tcCoreValBndrs bndrs $ \ bndrs' ->
268 mapTc tcCoreExpr rhss `thenTc` \ rhss' ->
269 tcCoreExpr body `thenTc` \ body' ->
270 returnTc (Let (Rec (bndrs' `zip` rhss')) body')
272 (bndrs, rhss) = unzip pairs
274 tcCoreExpr (UfNote note expr)
275 = tcCoreExpr expr `thenTc` \ expr' ->
277 UfCoerce to_ty -> tcHsType to_ty `thenTc` \ to_ty' ->
278 returnTc (Note (Coerce to_ty' (coreExprType expr')) expr')
279 UfInlineCall -> returnTc (Note InlineCall expr')
280 UfSCC cc -> returnTc (Note (SCC cc) expr')
282 tcCoreNote (UfSCC cc) = returnTc (SCC cc)
283 tcCoreNote UfInlineCall = returnTc InlineCall
286 -- rationalTy isn't built in so, we have to construct it
287 -- (the "ty" part of the incoming literal is simply bottom)
288 tcUfCon (UfLitCon (NoRepRational lit _))
289 = tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
291 rational_ty = mkSynTy rational_tycon []
293 returnTc (Literal (NoRepRational lit rational_ty))
295 -- Similarly for integers and strings, except that they are wired in
296 tcUfCon (UfLitCon (NoRepInteger lit _))
297 = returnTc (Literal (NoRepInteger lit integerTy))
298 tcUfCon (UfLitCon (NoRepStr lit _))
299 = returnTc (Literal (NoRepStr lit stringTy))
301 tcUfCon (UfLitCon other_lit)
302 = returnTc (Literal other_lit)
304 -- The dreaded lit-lits are also similar, except here the type
305 -- is read in explicitly rather than being implicit
306 tcUfCon (UfLitLitCon lit ty)
307 = tcHsType ty `thenTc` \ ty' ->
308 returnTc (Literal (MachLitLit lit ty'))
310 tcUfCon (UfDataCon name) = tcUfDataCon name
312 tcUfCon (UfPrimOp name)
313 = tcVar name `thenTc` \ op_id ->
314 case isPrimitiveId_maybe op_id of
315 Just op -> returnTc (PrimOp op)
316 Nothing -> failWithTc (badPrimOp name)
318 tcUfCon (UfCCallOp str is_dyn casm gc)
321 tcGetUnique `thenNF_Tc` \ u ->
322 returnTc (PrimOp (CCallOp (Right u) casm gc cCallConv))
323 False -> returnTc (PrimOp (CCallOp (Left str) casm gc cCallConv))
326 = tcVar name `thenTc` \ con_id ->
327 case isDataConId_maybe con_id of
328 Just con -> returnTc (DataCon con)
329 Nothing -> failWithTc (badCon name)
333 tcCoreLamBndr (UfValBinder name ty) thing_inside
334 = tcHsType ty `thenTc` \ ty' ->
336 id = mkUserId name ty'
338 tcExtendGlobalValEnv [id] $
341 tcCoreLamBndr (UfTyBinder name kind) thing_inside
343 tyvar = mkTyVar name kind
345 tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
347 tcCoreValBndr (UfValBinder name ty) thing_inside
348 = tcHsType ty `thenTc` \ ty' ->
350 id = mkUserId name ty'
352 tcExtendGlobalValEnv [id] $
355 tcCoreValBndrs bndrs thing_inside -- Expect them all to be ValBinders
356 = mapTc tcHsType tys `thenTc` \ tys' ->
358 ids = zipWithEqual "tcCoreValBndr" mkUserId names tys'
360 tcExtendGlobalValEnv ids $
363 names = [name | UfValBinder name _ <- bndrs]
364 tys = [ty | UfValBinder _ ty <- bndrs]
368 tcCoreAlt scrut_ty (UfDefault, names, rhs)
369 = ASSERT( null names )
370 tcCoreExpr rhs `thenTc` \ rhs' ->
371 returnTc (DEFAULT, [], rhs')
373 tcCoreAlt scrut_ty (UfLitCon lit, names, rhs)
374 = ASSERT( null names )
375 tcCoreExpr rhs `thenTc` \ rhs' ->
376 returnTc (Literal lit, [], rhs')
378 tcCoreAlt scrut_ty (UfLitLitCon str ty, names, rhs)
379 = ASSERT( null names )
380 tcCoreExpr rhs `thenTc` \ rhs' ->
381 tcHsType ty `thenTc` \ ty' ->
382 returnTc (Literal (MachLitLit str ty'), [], rhs')
384 -- A case alternative is made quite a bit more complicated
385 -- by the fact that we omit type annotations because we can
386 -- work them out. True enough, but its not that easy!
387 tcCoreAlt scrut_ty (UfDataCon con_name, names, rhs)
388 = tcVar con_name `thenTc` \ con_id ->
390 con = case isDataConId_maybe con_id of
392 Nothing -> pprPanic "tcCoreAlt" (ppr con_id)
394 (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
396 (tycon, inst_tys, cons) = splitAlgTyConApp scrut_ty
397 ex_tyvars' = [mkTyVar name (tyVarKind tv) | (name,tv) <- names `zip` ex_tyvars]
398 ex_tys' = mkTyVarTys ex_tyvars'
399 arg_tys = dataConArgTys con (inst_tys ++ ex_tys')
400 id_names = drop (length ex_tyvars) names
403 | length id_names /= length arg_tys
404 = pprPanic "tcCoreAlts" (ppr (con_name, names, rhs) $$
405 (ppr main_tyvars <+> ppr ex_tyvars) $$
409 = zipWithEqual "tcCoreAlts" mkUserId id_names arg_tys
411 ASSERT( con `elem` cons && length inst_tys == length main_tyvars )
412 tcExtendTyVarEnv ex_tyvars' $
413 tcExtendGlobalValEnv arg_ids $
414 tcCoreExpr rhs `thenTc` \ rhs' ->
415 returnTc (DataCon con, ex_tyvars' ++ arg_ids, rhs')
419 ifaceSigCtxt sig_name
420 = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]