9500baf9cdd9b395ce190f10de9cf1e1e442e0fd
[ghc-hetmet.git] / ghc / compiler / typecheck / TcIfaceSig.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TcIfaceSig]{Type checking of type signatures in interface files}
5
6 \begin{code}
7 module TcIfaceSig ( tcInterfaceSigs ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( HsDecl(..), IfaceSig(..) )
12 import TcMonad
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
17                           tcExtendTyVarScope
18                         )
19 import TcEnv            ( ValueEnv, tcExtendTyVarEnv, 
20                           tcExtendGlobalValEnv, tcSetValueEnv,
21                           tcLookupTyConByKey, tcLookupValueMaybe,
22                           explicitLookupValue, badCon, badPrimOp
23                         )
24 import TcType           ( TcKind, kindToTcKind )
25
26 import RnHsSyn          ( RenamedHsDecl )
27 import HsCore
28 import HsDecls          ( HsIdInfo(..), HsStrictnessInfo(..) )
29 import CallConv         ( cCallConv )
30 import Const            ( Con(..), Literal(..) )
31 import CoreSyn
32 import CoreUtils        ( coreExprType )
33 import CoreUnfold
34 import CoreLint         ( lintUnfolding )
35 import WwLib            ( mkWrapper )
36 import PrimOp           ( PrimOp(..) )
37
38 import Id               ( Id, mkImportedId, mkUserId,
39                           isPrimitiveId_maybe, isDataConId_maybe
40                         )
41 import IdInfo
42 import DataCon          ( dataConSig, dataConArgTys )
43 import SpecEnv          ( addToSpecEnv )
44 import Type             ( mkSynTy, mkTyVarTys, splitAlgTyConApp )
45 import Var              ( IdOrTyVar, mkTyVar, tyVarKind )
46 import VarEnv
47 import Name             ( Name, NamedThing(..) )
48 import Unique           ( rationalTyConKey )
49 import TysWiredIn       ( integerTy, stringTy )
50 import ErrUtils         ( pprBagOfErrors )
51 import Maybes           ( maybeToBool, MaybeErr(..) )
52 import Outputable       
53 import Util             ( zipWithEqual )
54 \end{code}
55
56 Ultimately, type signatures in interfaces will have pragmatic
57 information attached, so it is a good idea to have separate code to
58 check them.
59
60 As always, we do not have to worry about user-pragmas in interface
61 signatures.
62
63 \begin{code}
64 tcInterfaceSigs :: ValueEnv             -- Envt to use when checking unfoldings
65                 -> [RenamedHsDecl]      -- Ignore non-sig-decls in these decls
66                 -> TcM s [Id]
67                 
68
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)
78
79 tcInterfaceSigs unf_env (other_decl : rest) = tcInterfaceSigs unf_env rest
80
81 tcInterfaceSigs unf_env [] = returnTc []
82 \end{code}
83
84 \begin{code}
85 tcIdInfo unf_env name ty info info_ins
86   = foldlTc tcPrag noIdInfo info_ins
87   where
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
92     tcPrag info (HsUnfold inline_prag maybe_expr)
93         = (case maybe_expr of
94                 Just expr -> tcPragExpr unf_env name [] expr
95                 Nothing   -> returnNF_Tc Nothing
96           )                                     `thenNF_Tc` \ maybe_expr' ->
97           let
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
104
105                 info2 = inline_prag `setInlinePragInfo` info1
106           in
107           returnTc info2
108
109     tcPrag info (HsStrictness strict)
110         = tcStrictness unf_env ty info strict
111
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!
118
119           tcPragExpr unf_env name tyvars' rhs   `thenNF_Tc` \ maybe_rhs' ->
120           let
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
125                           Nothing -> spec_env
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) $
130                                                                  spec_env
131           in
132           returnTc (spec_env' `setSpecInfo` info)
133 \end{code}
134
135 \begin{code}
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 ->
139     let
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
144
145                         Nothing        -> info
146
147         has_worker = maybeToBool maybe_worker_id
148     in
149     returnTc (StrictnessInfo demands bot_result has_worker  `setStrictnessInfo` info')
150 \end{code}
151
152 \begin{code}
153 tcWorker unf_env Nothing = returnNF_Tc Nothing
154
155 tcWorker unf_env (Just (worker_name,_))
156   = returnNF_Tc (trace_maybe maybe_worker_id)
157   where
158     maybe_worker_id = explicitLookupValue unf_env worker_name
159
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
163 \end{code}
164
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.
167
168 \begin{code}
169 tcPragExpr :: ValueEnv -> Name -> [IdOrTyVar] -> UfExpr Name -> NF_TcM s (Maybe CoreExpr)
170 tcPragExpr unf_env name in_scope_vars core_expr
171   = forkNF_Tc (
172         recoverNF_Tc no_unfolding (
173                 tcSetValueEnv unf_env $
174                 tcCoreExpr core_expr    `thenTc` \ core_expr' ->
175
176                 -- Check for type consistency in the unfolding
177                 tcGetSrcLoc             `thenNF_Tc` \ src_loc -> 
178                 returnTc (lintUnfolding src_loc in_scope_vars core_expr')
179     ))                  
180   where
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))
186                                          Nothing)
187 \end{code}
188
189
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.
194
195 \begin{code}
196 tcVar :: Name -> TcM s Id
197 tcVar name
198   = tcLookupValueMaybe name     `thenNF_Tc` \ maybe_id ->
199     case maybe_id of {
200         Just id -> returnTc id;
201         Nothing -> failWithTc (noDecl name)
202     }
203
204 noDecl name = hsep [ptext SLIT("Warning: no binding for"), ppr name]
205 \end{code}
206
207 UfCore expressions.
208
209 \begin{code}
210 tcCoreExpr :: UfExpr Name -> TcM s CoreExpr
211
212 tcCoreExpr (UfType ty)
213   = tcHsTypeKind ty     `thenTc` \ (_, ty') ->
214         -- It might not be of kind type
215     returnTc (Type ty')
216
217 tcCoreExpr (UfVar name)
218   = tcVar name  `thenTc` \ id ->
219     returnTc (Var id)
220
221 tcCoreExpr (UfCon con args) 
222   = tcUfCon con                 `thenTc` \ con' ->
223     mapTc tcCoreExpr args       `thenTc` \ args' ->
224     returnTc (Con con' args')
225
226 tcCoreExpr (UfTuple name args) 
227   = tcUfDataCon name            `thenTc` \ con ->
228     mapTc tcCoreExpr args       `thenTc` \ args' ->
229     let
230         -- Put the missing type arguments back in
231         con_args = map (Type . coreExprType) args' ++ args'
232     in
233     returnTc (Con con con_args)
234
235 tcCoreExpr (UfLam bndr body)
236   = tcCoreLamBndr bndr          $ \ bndr' ->
237     tcCoreExpr body             `thenTc` \ body' ->
238     returnTc (Lam bndr' body')
239
240 tcCoreExpr (UfApp fun arg)
241   = tcCoreExpr fun              `thenTc` \ fun' ->
242     tcCoreExpr arg              `thenTc` \ arg' ->
243     returnTc (App fun' arg')
244
245 tcCoreExpr (UfCase scrut case_bndr alts) 
246   = tcCoreExpr scrut                                    `thenTc` \ scrut' ->
247     let
248         scrut_ty = coreExprType scrut'
249         case_bndr' = mkUserId case_bndr scrut_ty
250     in
251     tcExtendGlobalValEnv [case_bndr']   $
252     mapTc (tcCoreAlt scrut_ty) alts     `thenTc` \ alts' ->
253     returnTc (Case scrut' case_bndr' alts')
254
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')
260
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')
266   where
267     (bndrs, rhss) = unzip pairs
268
269 tcCoreExpr (UfNote note expr) 
270   = tcCoreExpr expr             `thenTc` \ expr' ->
271     case note of
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')
276
277 tcCoreNote (UfSCC cc)   = returnTc (SCC cc)
278 tcCoreNote UfInlineCall = returnTc InlineCall 
279
280
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 ->
285     let
286         rational_ty  = mkSynTy rational_tycon []
287     in
288     returnTc (Literal (NoRepRational lit rational_ty)) 
289
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))
295
296 tcUfCon (UfLitCon other_lit)
297   = returnTc (Literal other_lit)
298
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'))
304
305 tcUfCon (UfDataCon name) = tcUfDataCon name
306
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)
312
313 tcUfCon (UfCCallOp str is_dyn casm gc)
314   = case is_dyn of
315        True  -> 
316           tcGetUnique `thenNF_Tc` \ u ->
317           returnTc (PrimOp (CCallOp (Right u) casm gc cCallConv))
318        False -> returnTc (PrimOp (CCallOp (Left str) casm gc cCallConv))
319
320 tcUfDataCon name
321   = tcVar name          `thenTc` \ con_id ->
322     case isDataConId_maybe con_id of
323         Just con -> returnTc (DataCon con)
324         Nothing  -> failWithTc (badCon name)
325 \end{code}
326
327 \begin{code}
328 tcCoreLamBndr (UfValBinder name ty) thing_inside
329   = tcHsType ty                 `thenTc` \ ty' ->
330     let
331         id = mkUserId name ty'
332     in
333     tcExtendGlobalValEnv [id] $
334     thing_inside id
335     
336 tcCoreLamBndr (UfTyBinder name kind) thing_inside
337   = let
338         tyvar = mkTyVar name kind
339     in
340     tcExtendTyVarEnv [tyvar] (thing_inside tyvar)
341     
342 tcCoreValBndr (UfValBinder name ty) thing_inside
343   = tcHsType ty                 `thenTc` \ ty' ->
344     let
345         id = mkUserId name ty'
346     in
347     tcExtendGlobalValEnv [id] $
348     thing_inside id
349     
350 tcCoreValBndrs bndrs thing_inside               -- Expect them all to be ValBinders
351   = mapTc tcHsType tys                  `thenTc` \ tys' ->
352     let
353         ids = zipWithEqual "tcCoreValBndr" mkUserId names tys'
354     in
355     tcExtendGlobalValEnv ids $
356     thing_inside ids
357   where
358     names = [name | UfValBinder name _  <- bndrs]
359     tys   = [ty   | UfValBinder _    ty <- bndrs]
360 \end{code}    
361
362 \begin{code}
363 tcCoreAlt scrut_ty (UfDefault, names, rhs)
364   = ASSERT( null names )
365     tcCoreExpr rhs              `thenTc` \ rhs' ->
366     returnTc (DEFAULT, [], rhs')
367   
368 tcCoreAlt scrut_ty (UfLitCon lit, names, rhs)
369   = ASSERT( null names )
370     tcCoreExpr rhs              `thenTc` \ rhs' ->
371     returnTc (Literal lit, [], rhs')
372
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')
378
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 ->
384     let
385         con                     = case isDataConId_maybe con_id of
386                                         Just con -> con
387                                         Nothing  -> pprPanic "tcCoreAlt" (ppr con_id)
388
389         (main_tyvars, _, ex_tyvars, _, _, _) = dataConSig con
390
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
396         arg_ids
397 #ifdef DEBUG
398                 | length id_names /= length arg_tys
399                 = pprPanic "tcCoreAlts" (ppr (con_name, names, rhs) $$
400                                          (ppr main_tyvars <+> ppr ex_tyvars) $$
401                                          ppr arg_tys)
402                 | otherwise
403 #endif
404                 = zipWithEqual "tcCoreAlts" mkUserId id_names arg_tys
405     in
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')
411 \end{code}
412
413 \begin{code}
414 ifaceSigCtxt sig_name
415   = hsep [ptext SLIT("In an interface-file signature for"), ppr sig_name]
416 \end{code}
417