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