Remove unnecessary free-variables from renamer
[ghc-hetmet.git] / compiler / iface / TcIface.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 Type checking of type signatures in interface files
7
8 \begin{code}
9 module TcIface ( 
10         tcImportDecl, checkWiredInTyCon, tcHiBootIface, typecheckIface, 
11         tcIfaceDecl, tcIfaceInst, tcIfaceFamInst, tcIfaceRules,
12         tcIfaceVectInfo, tcIfaceGlobal, tcExtCoreBindings
13  ) where
14
15 #include "HsVersions.h"
16
17 import IfaceSyn
18 import LoadIface
19 import IfaceEnv
20 import BuildTyCl
21 import TcRnMonad
22 import Type
23 import TypeRep
24 import HscTypes
25 import InstEnv
26 import FamInstEnv
27 import CoreSyn
28 import CoreUtils
29 import CoreUnfold
30 import CoreLint
31 import WorkWrap
32 import Id
33 import MkId
34 import IdInfo
35 import Class
36 import TyCon
37 import DataCon
38 import TysWiredIn
39 import Var              ( TyVar )
40 import qualified Var
41 import VarEnv
42 import Name
43 import NameEnv
44 import OccName
45 import Module
46 import UniqFM
47 import UniqSupply
48 import Outputable       
49 import ErrUtils
50 import Maybes
51 import SrcLoc
52 import DynFlags
53 import Control.Monad
54
55 import Data.List
56 import Data.Maybe
57 \end{code}
58
59 This module takes
60
61         IfaceDecl -> TyThing
62         IfaceType -> Type
63         etc
64
65 An IfaceDecl is populated with RdrNames, and these are not renamed to
66 Names before typechecking, because there should be no scope errors etc.
67
68         -- For (b) consider: f = $(...h....)
69         -- where h is imported, and calls f via an hi-boot file.  
70         -- This is bad!  But it is not seen as a staging error, because h
71         -- is indeed imported.  We don't want the type-checker to black-hole 
72         -- when simplifying and compiling the splice!
73         --
74         -- Simple solution: discard any unfolding that mentions a variable
75         -- bound in this module (and hence not yet processed).
76         -- The discarding happens when forkM finds a type error.
77
78 %************************************************************************
79 %*                                                                      *
80 %*      tcImportDecl is the key function for "faulting in"              *
81 %*      imported things
82 %*                                                                      *
83 %************************************************************************
84
85 The main idea is this.  We are chugging along type-checking source code, and
86 find a reference to GHC.Base.map.  We call tcLookupGlobal, which doesn't find
87 it in the EPS type envt.  So it 
88         1 loads GHC.Base.hi
89         2 gets the decl for GHC.Base.map
90         3 typechecks it via tcIfaceDecl
91         4 and adds it to the type env in the EPS
92
93 Note that DURING STEP 4, we may find that map's type mentions a type 
94 constructor that also 
95
96 Notice that for imported things we read the current version from the EPS
97 mutable variable.  This is important in situations like
98         ...$(e1)...$(e2)...
99 where the code that e1 expands to might import some defns that 
100 also turn out to be needed by the code that e2 expands to.
101
102 \begin{code}
103 tcImportDecl :: Name -> TcM TyThing
104 -- Entry point for *source-code* uses of importDecl
105 tcImportDecl name 
106   | Just thing <- wiredInNameTyThing_maybe name
107   = do  { initIfaceTcRn (loadWiredInHomeIface name) 
108                 -- See Note [Loading instances] in LoadIface
109         ; return thing }
110   | otherwise
111   = do  { traceIf (text "tcImportDecl" <+> ppr name)
112         ; mb_thing <- initIfaceTcRn (importDecl name)
113         ; case mb_thing of
114             Succeeded thing -> return thing
115             Failed err      -> failWithTc err }
116
117 checkWiredInTyCon :: TyCon -> TcM ()
118 -- Ensure that the home module of the TyCon (and hence its instances)
119 -- are loaded. See See Note [Loading instances] in LoadIface
120 -- It might not be a wired-in tycon (see the calls in TcUnify),
121 -- in which case this is a no-op.
122 checkWiredInTyCon tc    
123   | not (isWiredInName tc_name) 
124   = return ()
125   | otherwise
126   = do  { mod <- getModule
127         ; unless (mod == nameModule tc_name)
128                  (initIfaceTcRn (loadWiredInHomeIface tc_name))
129                 -- Don't look for (non-existent) Float.hi when
130                 -- compiling Float.lhs, which mentions Float of course
131                 -- A bit yukky to call initIfaceTcRn here
132         }
133   where
134     tc_name = tyConName tc
135
136 importDecl :: Name -> IfM lcl (MaybeErr Message TyThing)
137 -- Get the TyThing for this Name from an interface file
138 -- It's not a wired-in thing -- the caller caught that
139 importDecl name
140   = ASSERT( not (isWiredInName name) )
141     do  { traceIf nd_doc
142
143         -- Load the interface, which should populate the PTE
144         ; mb_iface <- loadInterface nd_doc (nameModule name) ImportBySystem
145         ; case mb_iface of {
146                 Failed err_msg  -> return (Failed err_msg) ;
147                 Succeeded iface -> do
148
149         -- Now look it up again; this time we should find it
150         { eps <- getEps 
151         ; case lookupTypeEnv (eps_PTE eps) name of
152             Just thing -> return (Succeeded thing)
153             Nothing    -> return (Failed not_found_msg)
154     }}}
155   where
156     nd_doc = ptext SLIT("Need decl for") <+> ppr name
157     not_found_msg = hang (ptext SLIT("Can't find interface-file declaration for") <+>
158                                 pprNameSpace (occNameSpace (nameOccName name)) <+> ppr name)
159                        2 (vcat [ptext SLIT("Probable cause: bug in .hi-boot file, or inconsistent .hi file"),
160                                 ptext SLIT("Use -ddump-if-trace to get an idea of which file caused the error")])
161 \end{code}
162
163 %************************************************************************
164 %*                                                                      *
165                 Type-checking a complete interface
166 %*                                                                      *
167 %************************************************************************
168
169 Suppose we discover we don't need to recompile.  Then we must type
170 check the old interface file.  This is a bit different to the
171 incremental type checking we do as we suck in interface files.  Instead
172 we do things similarly as when we are typechecking source decls: we
173 bring into scope the type envt for the interface all at once, using a
174 knot.  Remember, the decls aren't necessarily in dependency order --
175 and even if they were, the type decls might be mutually recursive.
176
177 \begin{code}
178 typecheckIface :: ModIface      -- Get the decls from here
179                -> TcRnIf gbl lcl ModDetails
180 typecheckIface iface
181   = initIfaceTc iface $ \ tc_env_var -> do
182         -- The tc_env_var is freshly allocated, private to 
183         -- type-checking this particular interface
184         {       -- Get the right set of decls and rules.  If we are compiling without -O
185                 -- we discard pragmas before typechecking, so that we don't "see"
186                 -- information that we shouldn't.  From a versioning point of view
187                 -- It's not actually *wrong* to do so, but in fact GHCi is unable 
188                 -- to handle unboxed tuples, so it must not see unfoldings.
189           ignore_prags <- doptM Opt_IgnoreInterfacePragmas
190
191                 -- Typecheck the decls.  This is done lazily, so that the knot-tying
192                 -- within this single module work out right.  In the If monad there is
193                 -- no global envt for the current interface; instead, the knot is tied
194                 -- through the if_rec_types field of IfGblEnv
195         ; names_w_things <- loadDecls ignore_prags (mi_decls iface)
196         ; let type_env = mkNameEnv names_w_things
197         ; writeMutVar tc_env_var type_env
198
199                 -- Now do those rules and instances
200         ; insts     <- mapM tcIfaceInst    (mi_insts     iface)
201         ; fam_insts <- mapM tcIfaceFamInst (mi_fam_insts iface)
202         ; rules     <- tcIfaceRules ignore_prags (mi_rules iface)
203
204                 -- Vectorisation information
205         ; vect_info <- tcIfaceVectInfo (mi_module iface) type_env 
206                                        (mi_vect_info iface)
207
208                 -- Exports
209         ; exports <- ifaceExportNames (mi_exports iface)
210
211                 -- Finished
212         ; traceIf (vcat [text "Finished typechecking interface for" <+> ppr (mi_module iface),
213                          text "Type envt:" <+> ppr type_env])
214         ; return $ ModDetails { md_types     = type_env
215                               , md_insts     = insts
216                               , md_fam_insts = fam_insts
217                               , md_rules     = rules
218                               , md_vect_info = vect_info
219                               , md_exports   = exports
220                               , md_modBreaks = emptyModBreaks
221                               }
222     }
223 \end{code}
224
225
226 %************************************************************************
227 %*                                                                      *
228                 Type and class declarations
229 %*                                                                      *
230 %************************************************************************
231
232 \begin{code}
233 tcHiBootIface :: HscSource -> Module -> TcRn ModDetails
234 -- Load the hi-boot iface for the module being compiled,
235 -- if it indeed exists in the transitive closure of imports
236 -- Return the ModDetails, empty if no hi-boot iface
237 tcHiBootIface hsc_src mod
238   | isHsBoot hsc_src            -- Already compiling a hs-boot file
239   = return emptyModDetails
240   | otherwise
241   = do  { traceIf (text "loadHiBootInterface" <+> ppr mod)
242
243         ; mode <- getGhcMode
244         ; if not (isOneShot mode)
245                 -- In --make and interactive mode, if this module has an hs-boot file
246                 -- we'll have compiled it already, and it'll be in the HPT
247                 -- 
248                 -- We check wheher the interface is a *boot* interface.
249                 -- It can happen (when using GHC from Visual Studio) that we
250                 -- compile a module in TypecheckOnly mode, with a stable, 
251                 -- fully-populated HPT.  In that case the boot interface isn't there
252                 -- (it's been replaced by the mother module) so we can't check it.
253                 -- And that's fine, because if M's ModInfo is in the HPT, then 
254                 -- it's been compiled once, and we don't need to check the boot iface
255           then do { hpt <- getHpt
256                   ; case lookupUFM hpt (moduleName mod) of
257                       Just info | mi_boot (hm_iface info) 
258                                 -> return (hm_details info)
259                       other -> return emptyModDetails }
260           else do
261
262         -- OK, so we're in one-shot mode.  
263         -- In that case, we're read all the direct imports by now, 
264         -- so eps_is_boot will record if any of our imports mention us by 
265         -- way of hi-boot file
266         { eps <- getEps
267         ; case lookupUFM (eps_is_boot eps) (moduleName mod) of {
268             Nothing -> return emptyModDetails ; -- The typical case
269
270             Just (_, False) -> failWithTc moduleLoop ;
271                 -- Someone below us imported us!
272                 -- This is a loop with no hi-boot in the way
273                 
274             Just (_mod, True) ->        -- There's a hi-boot interface below us
275                 
276     do  { read_result <- findAndReadIface 
277                                 need mod
278                                 True    -- Hi-boot file
279
280         ; case read_result of
281                 Failed err               -> failWithTc (elaborate err)
282                 Succeeded (iface, _path) -> typecheckIface iface
283     }}}}
284   where
285     need = ptext SLIT("Need the hi-boot interface for") <+> ppr mod
286                  <+> ptext SLIT("to compare against the Real Thing")
287
288     moduleLoop = ptext SLIT("Circular imports: module") <+> quotes (ppr mod) 
289                      <+> ptext SLIT("depends on itself")
290
291     elaborate err = hang (ptext SLIT("Could not find hi-boot interface for") <+> 
292                           quotes (ppr mod) <> colon) 4 err
293 \end{code}
294
295
296 %************************************************************************
297 %*                                                                      *
298                 Type and class declarations
299 %*                                                                      *
300 %************************************************************************
301
302 When typechecking a data type decl, we *lazily* (via forkM) typecheck
303 the constructor argument types.  This is in the hope that we may never
304 poke on those argument types, and hence may never need to load the
305 interface files for types mentioned in the arg types.
306
307 E.g.    
308         data Foo.S = MkS Baz.T
309 Mabye we can get away without even loading the interface for Baz!
310
311 This is not just a performance thing.  Suppose we have
312         data Foo.S = MkS Baz.T
313         data Baz.T = MkT Foo.S
314 (in different interface files, of course).
315 Now, first we load and typecheck Foo.S, and add it to the type envt.  
316 If we do explore MkS's argument, we'll load and typecheck Baz.T.
317 If we explore MkT's argument we'll find Foo.S already in the envt.  
318
319 If we typechecked constructor args eagerly, when loading Foo.S we'd try to
320 typecheck the type Baz.T.  So we'd fault in Baz.T... and then need Foo.S...
321 which isn't done yet.
322
323 All very cunning. However, there is a rather subtle gotcha which bit
324 me when developing this stuff.  When we typecheck the decl for S, we
325 extend the type envt with S, MkS, and all its implicit Ids.  Suppose
326 (a bug, but it happened) that the list of implicit Ids depended in
327 turn on the constructor arg types.  Then the following sequence of
328 events takes place:
329         * we build a thunk <t> for the constructor arg tys
330         * we build a thunk for the extended type environment (depends on <t>)
331         * we write the extended type envt into the global EPS mutvar
332         
333 Now we look something up in the type envt
334         * that pulls on <t>
335         * which reads the global type envt out of the global EPS mutvar
336         * but that depends in turn on <t>
337
338 It's subtle, because, it'd work fine if we typechecked the constructor args 
339 eagerly -- they don't need the extended type envt.  They just get the extended
340 type envt by accident, because they look at it later.
341
342 What this means is that the implicitTyThings MUST NOT DEPEND on any of
343 the forkM stuff.
344
345
346 \begin{code}
347 tcIfaceDecl :: Bool     -- True <=> discard IdInfo on IfaceId bindings
348             -> IfaceDecl
349             -> IfL TyThing
350
351 tcIfaceDecl ignore_prags (IfaceId {ifName = occ_name, ifType = iface_type, ifIdInfo = info})
352   = do  { name <- lookupIfaceTop occ_name
353         ; ty <- tcIfaceType iface_type
354         ; info <- tcIdInfo ignore_prags name ty info
355         ; return (AnId (mkVanillaGlobal name ty info)) }
356
357 tcIfaceDecl ignore_prags 
358             (IfaceData {ifName = occ_name, 
359                         ifTyVars = tv_bndrs, 
360                         ifCtxt = ctxt, ifGadtSyntax = gadt_syn,
361                         ifCons = rdr_cons, 
362                         ifRec = is_rec, 
363                         ifGeneric = want_generic,
364                         ifFamInst = mb_family })
365   = do  { tc_name <- lookupIfaceTop occ_name
366         ; bindIfaceTyVars tv_bndrs $ \ tyvars -> do
367
368         { tycon <- fixM ( \ tycon -> do
369             { stupid_theta <- tcIfaceCtxt ctxt
370             ; famInst <- 
371                 case mb_family of
372                   Nothing         -> return Nothing
373                   Just (fam, tys) -> 
374                     do { famTyCon <- tcIfaceTyCon fam
375                        ; insttys <- mapM tcIfaceType tys
376                        ; return $ Just (famTyCon, insttys)
377                        }
378             ; cons <- tcIfaceDataCons tc_name tycon tyvars rdr_cons
379             ; buildAlgTyCon tc_name tyvars stupid_theta
380                             cons is_rec want_generic gadt_syn famInst
381             })
382         ; traceIf (text "tcIfaceDecl4" <+> ppr tycon)
383         ; return (ATyCon tycon)
384     }}
385
386 tcIfaceDecl ignore_prags 
387             (IfaceSyn {ifName = occ_name, ifTyVars = tv_bndrs, 
388                        ifOpenSyn = isOpen, ifSynRhs = rdr_rhs_ty,
389                        ifFamInst = mb_family})
390    = bindIfaceTyVars tv_bndrs $ \ tyvars -> do
391      { tc_name <- lookupIfaceTop occ_name
392      ; rhs_tyki <- tcIfaceType rdr_rhs_ty
393      ; let rhs = if isOpen then OpenSynTyCon rhs_tyki Nothing
394                            else SynonymTyCon rhs_tyki
395      ; famInst <- case mb_family of
396                     Nothing         -> return Nothing
397                     Just (fam, tys) -> 
398                       do { famTyCon <- tcIfaceTyCon fam
399                          ; insttys <- mapM tcIfaceType tys
400                          ; return $ Just (famTyCon, insttys)
401                          }
402      ; tycon <- buildSynTyCon tc_name tyvars rhs famInst
403      ; return $ ATyCon tycon
404      }
405
406 tcIfaceDecl ignore_prags
407             (IfaceClass {ifCtxt = rdr_ctxt, ifName = occ_name, 
408                          ifTyVars = tv_bndrs, ifFDs = rdr_fds, 
409                          ifATs = rdr_ats, ifSigs = rdr_sigs, 
410                          ifRec = tc_isrec })
411 -- ToDo: in hs-boot files we should really treat abstract classes specially,
412 --       as we do abstract tycons
413   = bindIfaceTyVars tv_bndrs $ \ tyvars -> do
414     { cls_name <- lookupIfaceTop occ_name
415     ; ctxt <- tcIfaceCtxt rdr_ctxt
416     ; sigs <- mappM tc_sig rdr_sigs
417     ; fds  <- mappM tc_fd rdr_fds
418     ; ats'  <- mappM (tcIfaceDecl ignore_prags) rdr_ats
419     ; let ats = zipWith setTyThingPoss ats' (map ifTyVars rdr_ats)
420     ; cls  <- buildClass cls_name tyvars ctxt fds ats sigs tc_isrec
421     ; return (AClass cls) }
422   where
423    tc_sig (IfaceClassOp occ dm rdr_ty)
424      = do { op_name <- lookupIfaceTop occ
425           ; op_ty   <- forkM (mk_doc op_name rdr_ty) (tcIfaceType rdr_ty)
426                 -- Must be done lazily for just the same reason as the 
427                 -- type of a data con; to avoid sucking in types that
428                 -- it mentions unless it's necessray to do so
429           ; return (op_name, dm, op_ty) }
430
431    mk_doc op_name op_ty = ptext SLIT("Class op") <+> sep [ppr op_name, ppr op_ty]
432
433    tc_fd (tvs1, tvs2) = do { tvs1' <- mappM tcIfaceTyVar tvs1
434                            ; tvs2' <- mappM tcIfaceTyVar tvs2
435                            ; return (tvs1', tvs2') }
436
437    -- For each AT argument compute the position of the corresponding class
438    -- parameter in the class head.  This will later serve as a permutation
439    -- vector when checking the validity of instance declarations.
440    setTyThingPoss (ATyCon tycon) atTyVars = 
441      let classTyVars = map fst tv_bndrs
442          poss        =   catMaybes 
443                        . map ((`elemIndex` classTyVars) . fst) 
444                        $ atTyVars
445                     -- There will be no Nothing, as we already passed renaming
446      in 
447      ATyCon (setTyConArgPoss tycon poss)
448    setTyThingPoss _               _ = panic "TcIface.setTyThingPoss"
449
450 tcIfaceDecl ignore_prags (IfaceForeign {ifName = rdr_name, ifExtName = ext_name})
451   = do  { name <- lookupIfaceTop rdr_name
452         ; return (ATyCon (mkForeignTyCon name ext_name 
453                                          liftedTypeKind 0)) }
454
455 tcIfaceDataCons tycon_name tycon tc_tyvars if_cons
456   = case if_cons of
457         IfAbstractTyCon  -> return mkAbstractTyConRhs
458         IfOpenDataTyCon  -> return mkOpenDataTyConRhs
459         IfDataTyCon cons -> do  { data_cons <- mappM tc_con_decl cons
460                                 ; return (mkDataTyConRhs data_cons) }
461         IfNewTyCon con   -> do  { data_con <- tc_con_decl con
462                                 ; mkNewTyConRhs tycon_name tycon data_con }
463   where
464     tc_con_decl (IfCon { ifConInfix = is_infix, 
465                          ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
466                          ifConOcc = occ, ifConCtxt = ctxt, ifConEqSpec = spec,
467                          ifConArgTys = args, ifConFields = field_lbls,
468                          ifConStricts = stricts})
469      = bindIfaceTyVars univ_tvs $ \ univ_tyvars -> do
470        bindIfaceTyVars ex_tvs    $ \ ex_tyvars -> do
471         { name  <- lookupIfaceTop occ
472         ; eq_spec <- tcIfaceEqSpec spec
473         ; theta <- tcIfaceCtxt ctxt     -- Laziness seems not worth the bother here
474                 -- At one stage I thought that this context checking *had*
475                 -- to be lazy, because of possible mutual recursion between the
476                 -- type and the classe: 
477                 -- E.g. 
478                 --      class Real a where { toRat :: a -> Ratio Integer }
479                 --      data (Real a) => Ratio a = ...
480                 -- But now I think that the laziness in checking class ops breaks 
481                 -- the loop, so no laziness needed
482
483         -- Read the argument types, but lazily to avoid faulting in
484         -- the component types unless they are really needed
485         ; arg_tys <- forkM (mk_doc name) (mappM tcIfaceType args)
486         ; lbl_names <- mappM lookupIfaceTop field_lbls
487
488         ; buildDataCon name is_infix {- Not infix -}
489                        stricts lbl_names
490                        univ_tyvars ex_tyvars 
491                        eq_spec theta 
492                        arg_tys tycon
493         }
494     mk_doc con_name = ptext SLIT("Constructor") <+> ppr con_name
495
496 tcIfaceEqSpec spec
497   = mapM do_item spec
498   where
499     do_item (occ, if_ty) = do { tv <- tcIfaceTyVar (occNameFS occ)
500                               ; ty <- tcIfaceType if_ty
501                               ; return (tv,ty) }
502 \end{code}
503
504
505 %************************************************************************
506 %*                                                                      *
507                 Instances
508 %*                                                                      *
509 %************************************************************************
510
511 \begin{code}
512 tcIfaceInst :: IfaceInst -> IfL Instance
513 tcIfaceInst (IfaceInst { ifDFun = dfun_occ, ifOFlag = oflag,
514                          ifInstCls = cls, ifInstTys = mb_tcs,
515                          ifInstOrph = orph })
516   = do  { dfun    <- forkM (ptext SLIT("Dict fun") <+> ppr dfun_occ) $
517                      tcIfaceExtId dfun_occ
518         ; let mb_tcs' = map (fmap ifaceTyConName) mb_tcs
519         ; return (mkImportedInstance cls mb_tcs' dfun oflag) }
520
521 tcIfaceFamInst :: IfaceFamInst -> IfL FamInst
522 tcIfaceFamInst (IfaceFamInst { ifFamInstTyCon = tycon, 
523                                ifFamInstFam = fam, ifFamInstTys = mb_tcs })
524 --  = do        { tycon'  <- forkM (ptext SLIT("Inst tycon") <+> ppr tycon) $
525 -- ^^^this line doesn't work, but vvv this does => CPP in Haskell = evil!
526   = do  { tycon'  <- forkM (text ("Inst tycon") <+> ppr tycon) $
527                      tcIfaceTyCon tycon
528         ; let mb_tcs' = map (fmap ifaceTyConName) mb_tcs
529         ; return (mkImportedFamInst fam mb_tcs' tycon') }
530 \end{code}
531
532
533 %************************************************************************
534 %*                                                                      *
535                 Rules
536 %*                                                                      *
537 %************************************************************************
538
539 We move a IfaceRule from eps_rules to eps_rule_base when all its LHS free vars
540 are in the type environment.  However, remember that typechecking a Rule may 
541 (as a side effect) augment the type envt, and so we may need to iterate the process.
542
543 \begin{code}
544 tcIfaceRules :: Bool            -- True <=> ignore rules
545              -> [IfaceRule]
546              -> IfL [CoreRule]
547 tcIfaceRules ignore_prags if_rules
548   | ignore_prags = return []
549   | otherwise    = mapM tcIfaceRule if_rules
550
551 tcIfaceRule :: IfaceRule -> IfL CoreRule
552 tcIfaceRule (IfaceRule {ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
553                         ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs,
554                         ifRuleOrph = orph })
555   = do  { ~(bndrs', args', rhs') <- 
556                 -- Typecheck the payload lazily, in the hope it'll never be looked at
557                 forkM (ptext SLIT("Rule") <+> ftext name) $
558                 bindIfaceBndrs bndrs                      $ \ bndrs' ->
559                 do { args' <- mappM tcIfaceExpr args
560                    ; rhs'  <- tcIfaceExpr rhs
561                    ; return (bndrs', args', rhs') }
562         ; let mb_tcs = map ifTopFreeName args
563         ; lcl <- getLclEnv
564         ; returnM (Rule { ru_name = name, ru_fn = fn, ru_act = act, 
565                           ru_bndrs = bndrs', ru_args = args', 
566                           ru_rhs = rhs', 
567                           ru_rough = mb_tcs,
568                           ru_local = False }) } -- An imported RULE is never for a local Id
569                                                 -- or, even if it is (module loop, perhaps)
570                                                 -- we'll just leave it in the non-local set
571   where
572         -- This function *must* mirror exactly what Rules.topFreeName does
573         -- We could have stored the ru_rough field in the iface file
574         -- but that would be redundant, I think.
575         -- The only wrinkle is that we must not be deceived by
576         -- type syononyms at the top of a type arg.  Since
577         -- we can't tell at this point, we are careful not
578         -- to write them out in coreRuleToIfaceRule
579     ifTopFreeName :: IfaceExpr -> Maybe Name
580     ifTopFreeName (IfaceType (IfaceTyConApp tc _ )) = Just (ifaceTyConName tc)
581     ifTopFreeName (IfaceApp f a)                    = ifTopFreeName f
582     ifTopFreeName (IfaceExt n)                      = Just n
583     ifTopFreeName other                             = Nothing
584 \end{code}
585
586
587 %************************************************************************
588 %*                                                                      *
589                 Vectorisation information
590 %*                                                                      *
591 %************************************************************************
592
593 \begin{code}
594 tcIfaceVectInfo :: Module -> TypeEnv  -> IfaceVectInfo -> IfL VectInfo
595 tcIfaceVectInfo mod typeEnv (IfaceVectInfo 
596                              { ifaceVectInfoCCVar        = vars
597                              , ifaceVectInfoCCTyCon      = tycons
598                              , ifaceVectInfoCCTyConReuse = tyconsReuse
599                              })
600   = do { ccVars    <- mapM ccVarMapping vars
601        ; tyConRes1 <- mapM ccTyConMapping      tycons
602        ; tyConRes2 <- mapM ccTyConReuseMapping tycons
603        ; let (ccTyCons, ccDataCons, ccIsos) = unzip3 (tyConRes1 ++ tyConRes2)
604        ; return $ VectInfo 
605                   { vectInfoCCVar     = mkVarEnv  ccVars
606                   , vectInfoCCTyCon   = mkNameEnv ccTyCons
607                   , vectInfoCCDataCon = mkNameEnv (concat ccDataCons)
608                   , vectInfoCCIso     = mkNameEnv ccIsos
609                   }
610        }
611   where
612     ccVarMapping name 
613       = do { ccName <- lookupOrig mod (mkCloOcc (nameOccName name))
614            ; let { var   = lookupVar name
615                  ; ccVar = lookupVar ccName
616                  }
617            ; return (var, (var, ccVar))
618            }
619     ccTyConMapping name 
620       = do { ccName  <- lookupOrig mod (mkCloTyConOcc (nameOccName name))
621            ; isoName <- lookupOrig mod (mkCloIsoOcc   (nameOccName name))
622            ; let { tycon    = lookupTyCon name
623                  ; ccTycon  = lookupTyCon ccName
624                  ; isoTycon = lookupVar isoName
625                  }
626            ; ccDataCons <- mapM ccDataConMapping (tyConDataCons tycon)
627            ; return ((name, (tycon, ccTycon)),   -- (T, T_CC)
628                      ccDataCons,                 -- list of (Ci, Ci_CC)
629                      (name, (tycon, isoTycon)))  -- (T, isoT)
630            }
631     ccTyConReuseMapping name 
632       = do { isoName <- lookupOrig mod (mkCloIsoOcc   (nameOccName name))
633            ; let { tycon      = lookupTyCon name
634                  ; isoTycon   = lookupVar isoName
635                  ; ccDataCons = [ (dataConName dc, (dc, dc)) 
636                                 | dc <- tyConDataCons tycon]
637                  }
638            ; return ((name, (tycon, tycon)),     -- (T, T)
639                      ccDataCons,                 -- list of (Ci, Ci)
640                      (name, (tycon, isoTycon)))  -- (T, isoT)
641            }
642     ccDataConMapping datacon
643       = do { let name = dataConName datacon
644            ; ccName <- lookupOrig mod (mkCloDataConOcc (nameOccName name))
645            ; let ccDataCon = lookupDataCon ccName
646            ; return (name, (datacon, ccDataCon))
647            }
648     --
649     lookupVar name = case lookupTypeEnv typeEnv name of
650                        Just (AnId var) -> var
651                        Just _         -> 
652                          panic "TcIface.tcIfaceVectInfo: not an id"
653                        Nothing        ->
654                          panic "TcIface.tcIfaceVectInfo: unknown name"
655     lookupTyCon name = case lookupTypeEnv typeEnv name of
656                          Just (ATyCon tc) -> tc
657                          Just _         -> 
658                            panic "TcIface.tcIfaceVectInfo: not a tycon"
659                          Nothing        ->
660                            panic "TcIface.tcIfaceVectInfo: unknown name"
661     lookupDataCon name = case lookupTypeEnv typeEnv name of
662                            Just (ADataCon dc) -> dc
663                            Just _         -> 
664                              panic "TcIface.tcIfaceVectInfo: not a datacon"
665                            Nothing        ->
666                              panic "TcIface.tcIfaceVectInfo: unknown name"
667 \end{code}
668
669 %************************************************************************
670 %*                                                                      *
671                         Types
672 %*                                                                      *
673 %************************************************************************
674
675 \begin{code}
676 tcIfaceType :: IfaceType -> IfL Type
677 tcIfaceType (IfaceTyVar n)        = do { tv <- tcIfaceTyVar n; return (TyVarTy tv) }
678 tcIfaceType (IfaceAppTy t1 t2)    = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (AppTy t1' t2') }
679 tcIfaceType (IfaceFunTy t1 t2)    = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (FunTy t1' t2') }
680 tcIfaceType (IfaceTyConApp tc ts) = do { tc' <- tcIfaceTyCon tc; ts' <- tcIfaceTypes ts; return (mkTyConApp tc' ts') }
681 tcIfaceType (IfaceForAllTy tv t)  = bindIfaceTyVar tv $ \ tv' -> do { t' <- tcIfaceType t; return (ForAllTy tv' t') }
682 tcIfaceType (IfacePredTy st)      = do { st' <- tcIfacePredType st; return (PredTy st') }
683
684 tcIfaceTypes tys = mapM tcIfaceType tys
685
686 -----------------------------------------
687 tcIfacePredType :: IfacePredType -> IfL PredType
688 tcIfacePredType (IfaceClassP cls ts) = do { cls' <- tcIfaceClass cls; ts' <- tcIfaceTypes ts; return (ClassP cls' ts') }
689 tcIfacePredType (IfaceIParam ip t)   = do { ip' <- newIPName ip; t' <- tcIfaceType t; return (IParam ip' t') }
690 tcIfacePredType (IfaceEqPred t1 t2)  = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (EqPred t1' t2') }
691
692 -----------------------------------------
693 tcIfaceCtxt :: IfaceContext -> IfL ThetaType
694 tcIfaceCtxt sts = mappM tcIfacePredType sts
695 \end{code}
696
697
698 %************************************************************************
699 %*                                                                      *
700                         Core
701 %*                                                                      *
702 %************************************************************************
703
704 \begin{code}
705 tcIfaceExpr :: IfaceExpr -> IfL CoreExpr
706 tcIfaceExpr (IfaceType ty)
707   = tcIfaceType ty              `thenM` \ ty' ->
708     returnM (Type ty')
709
710 tcIfaceExpr (IfaceLcl name)
711   = tcIfaceLclId name   `thenM` \ id ->
712     returnM (Var id)
713
714 tcIfaceExpr (IfaceExt gbl)
715   = tcIfaceExtId gbl    `thenM` \ id ->
716     returnM (Var id)
717
718 tcIfaceExpr (IfaceLit lit)
719   = returnM (Lit lit)
720
721 tcIfaceExpr (IfaceFCall cc ty)
722   = tcIfaceType ty      `thenM` \ ty' ->
723     newUnique           `thenM` \ u ->
724     returnM (Var (mkFCallId u cc ty'))
725
726 tcIfaceExpr (IfaceTuple boxity args) 
727   = mappM tcIfaceExpr args      `thenM` \ args' ->
728     let
729         -- Put the missing type arguments back in
730         con_args = map (Type . exprType) args' ++ args'
731     in
732     returnM (mkApps (Var con_id) con_args)
733   where
734     arity = length args
735     con_id = dataConWorkId (tupleCon boxity arity)
736     
737
738 tcIfaceExpr (IfaceLam bndr body)
739   = bindIfaceBndr bndr          $ \ bndr' ->
740     tcIfaceExpr body            `thenM` \ body' ->
741     returnM (Lam bndr' body')
742
743 tcIfaceExpr (IfaceApp fun arg)
744   = tcIfaceExpr fun             `thenM` \ fun' ->
745     tcIfaceExpr arg             `thenM` \ arg' ->
746     returnM (App fun' arg')
747
748 tcIfaceExpr (IfaceCase scrut case_bndr ty alts) 
749   = tcIfaceExpr scrut           `thenM` \ scrut' ->
750     newIfaceName (mkVarOccFS case_bndr) `thenM` \ case_bndr_name ->
751     let
752         scrut_ty   = exprType scrut'
753         case_bndr' = mkLocalId case_bndr_name scrut_ty
754         tc_app     = splitTyConApp scrut_ty
755                 -- NB: Won't always succeed (polymoprhic case)
756                 --     but won't be demanded in those cases
757                 -- NB: not tcSplitTyConApp; we are looking at Core here
758                 --     look through non-rec newtypes to find the tycon that
759                 --     corresponds to the datacon in this case alternative
760     in
761     extendIfaceIdEnv [case_bndr']       $
762     mappM (tcIfaceAlt tc_app) alts      `thenM` \ alts' ->
763     tcIfaceType ty              `thenM` \ ty' ->
764     returnM (Case scrut' case_bndr' ty' alts')
765
766 tcIfaceExpr (IfaceLet (IfaceNonRec bndr rhs) body)
767   = do  { rhs' <- tcIfaceExpr rhs
768         ; id   <- tcIfaceLetBndr bndr
769         ; body' <- extendIfaceIdEnv [id] (tcIfaceExpr body)
770         ; return (Let (NonRec id rhs') body') }
771
772 tcIfaceExpr (IfaceLet (IfaceRec pairs) body)
773   = do  { ids <- mapM tcIfaceLetBndr bndrs
774         ; extendIfaceIdEnv ids $ do
775         { rhss' <- mapM tcIfaceExpr rhss
776         ; body' <- tcIfaceExpr body
777         ; return (Let (Rec (ids `zip` rhss')) body') } }
778   where
779     (bndrs, rhss) = unzip pairs
780
781 tcIfaceExpr (IfaceCast expr co) = do
782   expr' <- tcIfaceExpr expr
783   co' <- tcIfaceType co
784   returnM (Cast expr' co')
785
786 tcIfaceExpr (IfaceNote note expr) 
787   = tcIfaceExpr expr            `thenM` \ expr' ->
788     case note of
789         IfaceInlineMe     -> returnM (Note InlineMe   expr')
790         IfaceSCC cc       -> returnM (Note (SCC cc)   expr')
791         IfaceCoreNote n   -> returnM (Note (CoreNote n) expr')
792
793 -------------------------
794 tcIfaceAlt _ (IfaceDefault, names, rhs)
795   = ASSERT( null names )
796     tcIfaceExpr rhs             `thenM` \ rhs' ->
797     returnM (DEFAULT, [], rhs')
798   
799 tcIfaceAlt _ (IfaceLitAlt lit, names, rhs)
800   = ASSERT( null names )
801     tcIfaceExpr rhs             `thenM` \ rhs' ->
802     returnM (LitAlt lit, [], rhs')
803
804 -- A case alternative is made quite a bit more complicated
805 -- by the fact that we omit type annotations because we can
806 -- work them out.  True enough, but its not that easy!
807 tcIfaceAlt (tycon, inst_tys) (IfaceDataAlt data_occ, arg_strs, rhs)
808   = do  { con <- tcIfaceDataCon data_occ
809         ; ASSERT2( con `elem` tyConDataCons tycon,
810                    ppr con $$ ppr tycon $$ ppr (tyConDataCons tycon) )
811           tcIfaceDataAlt con inst_tys arg_strs rhs }
812                   
813 tcIfaceAlt (tycon, inst_tys) (IfaceTupleAlt boxity, arg_occs, rhs)
814   = ASSERT( isTupleTyCon tycon )
815     do  { let [data_con] = tyConDataCons tycon
816         ; tcIfaceDataAlt data_con inst_tys arg_occs rhs }
817
818 tcIfaceDataAlt con inst_tys arg_strs rhs
819   = do  { us <- newUniqueSupply
820         ; let uniqs = uniqsFromSupply us
821         ; let (ex_tvs, co_tvs, arg_ids)
822                       = dataConRepFSInstPat arg_strs uniqs con inst_tys
823               all_tvs = ex_tvs ++ co_tvs
824
825         ; rhs' <- extendIfaceTyVarEnv all_tvs   $
826                   extendIfaceIdEnv arg_ids      $
827                   tcIfaceExpr rhs
828         ; return (DataAlt con, all_tvs ++ arg_ids, rhs') }
829 \end{code}
830
831
832 \begin{code}
833 tcExtCoreBindings :: [IfaceBinding] -> IfL [CoreBind]   -- Used for external core
834 tcExtCoreBindings []     = return []
835 tcExtCoreBindings (b:bs) = do_one b (tcExtCoreBindings bs)
836
837 do_one :: IfaceBinding -> IfL [CoreBind] -> IfL [CoreBind]
838 do_one (IfaceNonRec bndr rhs) thing_inside
839   = do  { rhs' <- tcIfaceExpr rhs
840         ; bndr' <- newExtCoreBndr bndr
841         ; extendIfaceIdEnv [bndr'] $ do 
842         { core_binds <- thing_inside
843         ; return (NonRec bndr' rhs' : core_binds) }}
844
845 do_one (IfaceRec pairs) thing_inside
846   = do  { bndrs' <- mappM newExtCoreBndr bndrs
847         ; extendIfaceIdEnv bndrs' $ do
848         { rhss' <- mappM tcIfaceExpr rhss
849         ; core_binds <- thing_inside
850         ; return (Rec (bndrs' `zip` rhss') : core_binds) }}
851   where
852     (bndrs,rhss) = unzip pairs
853 \end{code}
854
855
856 %************************************************************************
857 %*                                                                      *
858                 IdInfo
859 %*                                                                      *
860 %************************************************************************
861
862 \begin{code}
863 tcIdInfo :: Bool -> Name -> Type -> IfaceIdInfo -> IfL IdInfo
864 tcIdInfo ignore_prags name ty info 
865   | ignore_prags = return vanillaIdInfo
866   | otherwise    = case info of
867                         NoInfo       -> return vanillaIdInfo
868                         HasInfo info -> foldlM tcPrag init_info info
869   where
870     -- Set the CgInfo to something sensible but uninformative before
871     -- we start; default assumption is that it has CAFs
872     init_info = vanillaIdInfo
873
874     tcPrag info HsNoCafRefs         = returnM (info `setCafInfo`   NoCafRefs)
875     tcPrag info (HsArity arity)     = returnM (info `setArityInfo` arity)
876     tcPrag info (HsStrictness str)  = returnM (info `setAllStrictnessInfo` Just str)
877
878         -- The next two are lazy, so they don't transitively suck stuff in
879     tcPrag info (HsWorker nm arity) = tcWorkerInfo ty info nm arity
880     tcPrag info (HsInline inline_prag) = returnM (info `setInlinePragInfo` inline_prag)
881     tcPrag info (HsUnfold expr)
882         = tcPragExpr name expr  `thenM` \ maybe_expr' ->
883           let
884                 -- maybe_expr' doesn't get looked at if the unfolding
885                 -- is never inspected; so the typecheck doesn't even happen
886                 unfold_info = case maybe_expr' of
887                                 Nothing    -> noUnfolding
888                                 Just expr' -> mkTopUnfolding expr' 
889           in
890           returnM (info `setUnfoldingInfoLazily` unfold_info)
891 \end{code}
892
893 \begin{code}
894 tcWorkerInfo ty info wkr arity
895   = do  { mb_wkr_id <- forkM_maybe doc (tcIfaceExtId wkr)
896
897         -- We return without testing maybe_wkr_id, but as soon as info is
898         -- looked at we will test it.  That's ok, because its outside the
899         -- knot; and there seems no big reason to further defer the
900         -- tcIfaceId lookup.  (Contrast with tcPragExpr, where postponing walking
901         -- over the unfolding until it's actually used does seem worth while.)
902         ; us <- newUniqueSupply
903
904         ; returnM (case mb_wkr_id of
905                      Nothing     -> info
906                      Just wkr_id -> add_wkr_info us wkr_id info) }
907   where
908     doc = text "Worker for" <+> ppr wkr
909     add_wkr_info us wkr_id info
910         = info `setUnfoldingInfoLazily`  mk_unfolding us wkr_id
911                `setWorkerInfo`           HasWorker wkr_id arity
912
913     mk_unfolding us wkr_id = mkTopUnfolding (initUs_ us (mkWrapper ty strict_sig) wkr_id)
914
915         -- We are relying here on strictness info always appearing 
916         -- before worker info,  fingers crossed ....
917     strict_sig = case newStrictnessInfo info of
918                    Just sig -> sig
919                    Nothing  -> pprPanic "Worker info but no strictness for" (ppr wkr)
920 \end{code}
921
922 For unfoldings we try to do the job lazily, so that we never type check
923 an unfolding that isn't going to be looked at.
924
925 \begin{code}
926 tcPragExpr :: Name -> IfaceExpr -> IfL (Maybe CoreExpr)
927 tcPragExpr name expr
928   = forkM_maybe doc $
929     tcIfaceExpr expr            `thenM` \ core_expr' ->
930
931                 -- Check for type consistency in the unfolding
932     ifOptM Opt_DoCoreLinting (
933         get_in_scope_ids                        `thenM` \ in_scope -> 
934         case lintUnfolding noSrcLoc in_scope core_expr' of
935           Nothing       -> returnM ()
936           Just fail_msg -> pprPanic "Iface Lint failure" (hang doc 2 fail_msg)
937     )                           `thenM_`
938
939    returnM core_expr'   
940   where
941     doc = text "Unfolding of" <+> ppr name
942     get_in_scope_ids    -- Urgh; but just for linting
943         = setLclEnv () $ 
944           do    { env <- getGblEnv 
945                 ; case if_rec_types env of {
946                           Nothing -> return [] ;
947                           Just (_, get_env) -> do
948                 { type_env <- get_env
949                 ; return (typeEnvIds type_env) }}}
950 \end{code}
951
952
953
954 %************************************************************************
955 %*                                                                      *
956                 Getting from Names to TyThings
957 %*                                                                      *
958 %************************************************************************
959
960 \begin{code}
961 tcIfaceGlobal :: Name -> IfL TyThing
962 tcIfaceGlobal name
963   | Just thing <- wiredInNameTyThing_maybe name
964         -- Wired-in things include TyCons, DataCons, and Ids
965   = do { ifCheckWiredInThing name; return thing }
966   | otherwise
967   = do  { (eps,hpt) <- getEpsAndHpt
968         ; dflags <- getDOpts
969         ; case lookupType dflags hpt (eps_PTE eps) name of {
970             Just thing -> return thing ;
971             Nothing    -> do
972
973         { env <- getGblEnv
974         ; case if_rec_types env of {
975             Just (mod, get_type_env) 
976                 | nameIsLocalOrFrom mod name
977                 -> do           -- It's defined in the module being compiled
978                 { type_env <- setLclEnv () get_type_env         -- yuk
979                 ; case lookupNameEnv type_env name of
980                         Just thing -> return thing
981                         Nothing    -> pprPanic "tcIfaceGlobal (local): not found:"  
982                                                 (ppr name $$ ppr type_env) }
983
984           ; other -> do
985
986         { mb_thing <- importDecl name   -- It's imported; go get it
987         ; case mb_thing of
988             Failed err      -> failIfM err
989             Succeeded thing -> return thing
990     }}}}}
991
992 ifCheckWiredInThing :: Name -> IfL ()
993 -- Even though we are in an interface file, we want to make
994 -- sure the instances of a wired-in thing are loaded (imagine f :: Double -> Double)
995 -- Ditto want to ensure that RULES are loaded too
996 -- See Note [Loading instances] in LoadIface
997 ifCheckWiredInThing name 
998   = do  { mod <- getIfModule
999                 -- Check whether we are typechecking the interface for this
1000                 -- very module.  E.g when compiling the base library in --make mode
1001                 -- we may typecheck GHC.Base.hi. At that point, GHC.Base is not in
1002                 -- the HPT, so without the test we'll demand-load it into the PIT!
1003                 -- C.f. the same test in checkWiredInTyCon above
1004         ; unless (mod == nameModule name)
1005                  (loadWiredInHomeIface name) }
1006
1007 tcIfaceTyCon :: IfaceTyCon -> IfL TyCon
1008 tcIfaceTyCon IfaceIntTc         = tcWiredInTyCon intTyCon
1009 tcIfaceTyCon IfaceBoolTc        = tcWiredInTyCon boolTyCon
1010 tcIfaceTyCon IfaceCharTc        = tcWiredInTyCon charTyCon
1011 tcIfaceTyCon IfaceListTc        = tcWiredInTyCon listTyCon
1012 tcIfaceTyCon IfacePArrTc        = tcWiredInTyCon parrTyCon
1013 tcIfaceTyCon (IfaceTupTc bx ar) = tcWiredInTyCon (tupleTyCon bx ar)
1014 tcIfaceTyCon (IfaceTc name)     = do { thing <- tcIfaceGlobal name 
1015                                      ; return (check_tc (tyThingTyCon thing)) }
1016   where
1017 #ifdef DEBUG
1018     check_tc tc = case toIfaceTyCon tc of
1019                    IfaceTc _ -> tc
1020                    other     -> pprTrace "check_tc" (ppr tc) tc
1021 #else
1022     check_tc tc = tc
1023 #endif
1024 -- we should be okay just returning Kind constructors without extra loading
1025 tcIfaceTyCon IfaceLiftedTypeKindTc   = return liftedTypeKindTyCon
1026 tcIfaceTyCon IfaceOpenTypeKindTc     = return openTypeKindTyCon
1027 tcIfaceTyCon IfaceUnliftedTypeKindTc = return unliftedTypeKindTyCon
1028 tcIfaceTyCon IfaceArgTypeKindTc      = return argTypeKindTyCon
1029 tcIfaceTyCon IfaceUbxTupleKindTc     = return ubxTupleKindTyCon
1030
1031 -- Even though we are in an interface file, we want to make
1032 -- sure the instances and RULES of this tycon are loaded 
1033 -- Imagine: f :: Double -> Double
1034 tcWiredInTyCon :: TyCon -> IfL TyCon
1035 tcWiredInTyCon tc = do { ifCheckWiredInThing (tyConName tc)
1036                        ; return tc }
1037
1038 tcIfaceClass :: Name -> IfL Class
1039 tcIfaceClass name = do { thing <- tcIfaceGlobal name
1040                        ; return (tyThingClass thing) }
1041
1042 tcIfaceDataCon :: Name -> IfL DataCon
1043 tcIfaceDataCon name = do { thing <- tcIfaceGlobal name
1044                          ; case thing of
1045                                 ADataCon dc -> return dc
1046                                 other   -> pprPanic "tcIfaceExtDC" (ppr name$$ ppr thing) }
1047
1048 tcIfaceExtId :: Name -> IfL Id
1049 tcIfaceExtId name = do { thing <- tcIfaceGlobal name
1050                        ; case thing of
1051                           AnId id -> return id
1052                           other   -> pprPanic "tcIfaceExtId" (ppr name$$ ppr thing) }
1053 \end{code}
1054
1055 %************************************************************************
1056 %*                                                                      *
1057                 Bindings
1058 %*                                                                      *
1059 %************************************************************************
1060
1061 \begin{code}
1062 bindIfaceBndr :: IfaceBndr -> (CoreBndr -> IfL a) -> IfL a
1063 bindIfaceBndr (IfaceIdBndr (fs, ty)) thing_inside
1064   = do  { name <- newIfaceName (mkVarOccFS fs)
1065         ; ty' <- tcIfaceType ty
1066         ; let id = mkLocalId name ty'
1067         ; extendIfaceIdEnv [id] (thing_inside id) }
1068 bindIfaceBndr (IfaceTvBndr bndr) thing_inside
1069   = bindIfaceTyVar bndr thing_inside
1070     
1071 bindIfaceBndrs :: [IfaceBndr] -> ([CoreBndr] -> IfL a) -> IfL a
1072 bindIfaceBndrs []     thing_inside = thing_inside []
1073 bindIfaceBndrs (b:bs) thing_inside
1074   = bindIfaceBndr b     $ \ b' ->
1075     bindIfaceBndrs bs   $ \ bs' ->
1076     thing_inside (b':bs')
1077
1078 -----------------------
1079 tcIfaceLetBndr (IfLetBndr fs ty info)
1080   = do  { name <- newIfaceName (mkVarOccFS fs)
1081         ; ty' <- tcIfaceType ty
1082         ; case info of
1083                 NoInfo    -> return (mkLocalId name ty')
1084                 HasInfo i -> return (mkLocalIdWithInfo name ty' (tc_info i)) } 
1085   where
1086         -- Similar to tcIdInfo, but much simpler
1087     tc_info [] = vanillaIdInfo
1088     tc_info (HsInline p     : i) = tc_info i `setInlinePragInfo` p 
1089     tc_info (HsArity a      : i) = tc_info i `setArityInfo` a 
1090     tc_info (HsStrictness s : i) = tc_info i `setAllStrictnessInfo` Just s 
1091     tc_info (other          : i) = pprTrace "tcIfaceLetBndr: discarding unexpected IdInfo" 
1092                                             (ppr other) (tc_info i)
1093
1094 -----------------------
1095 newExtCoreBndr :: IfaceLetBndr -> IfL Id
1096 newExtCoreBndr (IfLetBndr var ty _)     -- Ignoring IdInfo for now
1097   = do  { mod <- getIfModule
1098         ; name <- newGlobalBinder mod (mkVarOccFS var) noSrcSpan
1099         ; ty' <- tcIfaceType ty
1100         ; return (mkLocalId name ty') }
1101
1102 -----------------------
1103 bindIfaceTyVar :: IfaceTvBndr -> (TyVar -> IfL a) -> IfL a
1104 bindIfaceTyVar (occ,kind) thing_inside
1105   = do  { name <- newIfaceName (mkTyVarOcc occ)
1106         ; tyvar <- mk_iface_tyvar name kind
1107         ; extendIfaceTyVarEnv [tyvar] (thing_inside tyvar) }
1108
1109 bindIfaceTyVars :: [IfaceTvBndr] -> ([TyVar] -> IfL a) -> IfL a
1110 bindIfaceTyVars bndrs thing_inside
1111   = do  { names <- newIfaceNames (map mkTyVarOcc occs)
1112         ; tyvars <- TcRnMonad.zipWithM mk_iface_tyvar names kinds
1113         ; extendIfaceTyVarEnv tyvars (thing_inside tyvars) }
1114   where
1115     (occs,kinds) = unzip bndrs
1116
1117 mk_iface_tyvar :: Name -> IfaceKind -> IfL TyVar
1118 mk_iface_tyvar name ifKind
1119    = do { kind <- tcIfaceType ifKind
1120         ; if isCoercionKind kind then 
1121                 return (Var.mkCoVar name kind)
1122           else
1123                 return (Var.mkTyVar name kind) }
1124 \end{code}
1125