2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Type checking of type signatures in interface files
10 tcImportDecl, checkWiredInTyCon, tcHiBootIface, typecheckIface,
11 tcIfaceDecl, tcIfaceInst, tcIfaceFamInst, tcIfaceRules,
12 tcIfaceVectInfo, tcIfaceAnnotations, tcIfaceGlobal, tcExtCoreBindings
15 #include "HsVersions.h"
41 import TysPrim ( anyTyConOfKind )
43 import BasicTypes ( nonRuleLoopBreaker )
48 import OccurAnal ( occurAnalyseExpr )
49 import Demand ( isBottomingSig )
71 An IfaceDecl is populated with RdrNames, and these are not renamed to
72 Names before typechecking, because there should be no scope errors etc.
74 -- For (b) consider: f = \$(...h....)
75 -- where h is imported, and calls f via an hi-boot file.
76 -- This is bad! But it is not seen as a staging error, because h
77 -- is indeed imported. We don't want the type-checker to black-hole
78 -- when simplifying and compiling the splice!
80 -- Simple solution: discard any unfolding that mentions a variable
81 -- bound in this module (and hence not yet processed).
82 -- The discarding happens when forkM finds a type error.
84 %************************************************************************
86 %* tcImportDecl is the key function for "faulting in" *
89 %************************************************************************
91 The main idea is this. We are chugging along type-checking source code, and
92 find a reference to GHC.Base.map. We call tcLookupGlobal, which doesn't find
93 it in the EPS type envt. So it
95 2 gets the decl for GHC.Base.map
96 3 typechecks it via tcIfaceDecl
97 4 and adds it to the type env in the EPS
99 Note that DURING STEP 4, we may find that map's type mentions a type
100 constructor that also
102 Notice that for imported things we read the current version from the EPS
103 mutable variable. This is important in situations like
105 where the code that e1 expands to might import some defns that
106 also turn out to be needed by the code that e2 expands to.
109 tcImportDecl :: Name -> TcM TyThing
110 -- Entry point for *source-code* uses of importDecl
112 | Just thing <- wiredInNameTyThing_maybe name
113 = do { when (needWiredInHomeIface thing)
114 (initIfaceTcRn (loadWiredInHomeIface name))
115 -- See Note [Loading instances for wired-in things]
118 = do { traceIf (text "tcImportDecl" <+> ppr name)
119 ; mb_thing <- initIfaceTcRn (importDecl name)
121 Succeeded thing -> return thing
122 Failed err -> failWithTc err }
124 importDecl :: Name -> IfM lcl (MaybeErr Message TyThing)
125 -- Get the TyThing for this Name from an interface file
126 -- It's not a wired-in thing -- the caller caught that
128 = ASSERT( not (isWiredInName name) )
131 -- Load the interface, which should populate the PTE
132 ; mb_iface <- ASSERT2( isExternalName name, ppr name )
133 loadInterface nd_doc (nameModule name) ImportBySystem
135 Failed err_msg -> return (Failed err_msg) ;
138 -- Now look it up again; this time we should find it
140 ; case lookupTypeEnv (eps_PTE eps) name of
141 Just thing -> return (Succeeded thing)
142 Nothing -> return (Failed not_found_msg)
145 nd_doc = ptext (sLit "Need decl for") <+> ppr name
146 not_found_msg = hang (ptext (sLit "Can't find interface-file declaration for") <+>
147 pprNameSpace (occNameSpace (nameOccName name)) <+> ppr name)
148 2 (vcat [ptext (sLit "Probable cause: bug in .hi-boot file, or inconsistent .hi file"),
149 ptext (sLit "Use -ddump-if-trace to get an idea of which file caused the error")])
152 %************************************************************************
154 Checks for wired-in things
156 %************************************************************************
158 Note [Loading instances for wired-in things]
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160 We need to make sure that we have at least *read* the interface files
161 for any module with an instance decl or RULE that we might want.
163 * If the instance decl is an orphan, we have a whole separate mechanism
166 * If the instance decl not an orphan, then the act of looking at the
167 TyCon or Class will force in the defining module for the
168 TyCon/Class, and hence the instance decl
170 * BUT, if the TyCon is a wired-in TyCon, we don't really need its interface;
171 but we must make sure we read its interface in case it has instances or
172 rules. That is what LoadIface.loadWiredInHomeInterface does. It's called
173 from TcIface.{tcImportDecl, checkWiredInTyCon, ifCheckWiredInThing}
175 * HOWEVER, only do this for TyCons. There are no wired-in Classes. There
176 are some wired-in Ids, but we don't want to load their interfaces. For
177 example, Control.Exception.Base.recSelError is wired in, but that module
178 is compiled late in the base library, and we don't want to force it to
179 load before it's been compiled!
181 All of this is done by the type checker. The renamer plays no role.
182 (It used to, but no longer.)
186 checkWiredInTyCon :: TyCon -> TcM ()
187 -- Ensure that the home module of the TyCon (and hence its instances)
188 -- are loaded. See Note [Loading instances for wired-in things]
189 -- It might not be a wired-in tycon (see the calls in TcUnify),
190 -- in which case this is a no-op.
192 | not (isWiredInName tc_name)
195 = do { mod <- getModule
196 ; ASSERT( isExternalName tc_name )
197 when (mod /= nameModule tc_name)
198 (initIfaceTcRn (loadWiredInHomeIface tc_name))
199 -- Don't look for (non-existent) Float.hi when
200 -- compiling Float.lhs, which mentions Float of course
201 -- A bit yukky to call initIfaceTcRn here
204 tc_name = tyConName tc
206 ifCheckWiredInThing :: TyThing -> IfL ()
207 -- Even though we are in an interface file, we want to make
208 -- sure the instances of a wired-in thing are loaded (imagine f :: Double -> Double)
209 -- Ditto want to ensure that RULES are loaded too
210 -- See Note [Loading instances for wired-in things]
211 ifCheckWiredInThing thing
212 = do { mod <- getIfModule
213 -- Check whether we are typechecking the interface for this
214 -- very module. E.g when compiling the base library in --make mode
215 -- we may typecheck GHC.Base.hi. At that point, GHC.Base is not in
216 -- the HPT, so without the test we'll demand-load it into the PIT!
217 -- C.f. the same test in checkWiredInTyCon above
218 ; let name = getName thing
219 ; ASSERT2( isExternalName name, ppr name )
220 when (needWiredInHomeIface thing && mod /= nameModule name)
221 (loadWiredInHomeIface name) }
223 needWiredInHomeIface :: TyThing -> Bool
224 -- Only for TyCons; see Note [Loading instances for wired-in things]
225 needWiredInHomeIface (ATyCon {}) = True
226 needWiredInHomeIface _ = False
229 %************************************************************************
231 Type-checking a complete interface
233 %************************************************************************
235 Suppose we discover we don't need to recompile. Then we must type
236 check the old interface file. This is a bit different to the
237 incremental type checking we do as we suck in interface files. Instead
238 we do things similarly as when we are typechecking source decls: we
239 bring into scope the type envt for the interface all at once, using a
240 knot. Remember, the decls aren't necessarily in dependency order --
241 and even if they were, the type decls might be mutually recursive.
244 typecheckIface :: ModIface -- Get the decls from here
245 -> TcRnIf gbl lcl ModDetails
247 = initIfaceTc iface $ \ tc_env_var -> do
248 -- The tc_env_var is freshly allocated, private to
249 -- type-checking this particular interface
250 { -- Get the right set of decls and rules. If we are compiling without -O
251 -- we discard pragmas before typechecking, so that we don't "see"
252 -- information that we shouldn't. From a versioning point of view
253 -- It's not actually *wrong* to do so, but in fact GHCi is unable
254 -- to handle unboxed tuples, so it must not see unfoldings.
255 ignore_prags <- doptM Opt_IgnoreInterfacePragmas
257 -- Typecheck the decls. This is done lazily, so that the knot-tying
258 -- within this single module work out right. In the If monad there is
259 -- no global envt for the current interface; instead, the knot is tied
260 -- through the if_rec_types field of IfGblEnv
261 ; names_w_things <- loadDecls ignore_prags (mi_decls iface)
262 ; let type_env = mkNameEnv names_w_things
263 ; writeMutVar tc_env_var type_env
265 -- Now do those rules, instances and annotations
266 ; insts <- mapM tcIfaceInst (mi_insts iface)
267 ; fam_insts <- mapM tcIfaceFamInst (mi_fam_insts iface)
268 ; rules <- tcIfaceRules ignore_prags (mi_rules iface)
269 ; anns <- tcIfaceAnnotations (mi_anns iface)
271 -- Vectorisation information
272 ; vect_info <- tcIfaceVectInfo (mi_module iface) type_env
276 ; exports <- ifaceExportNames (mi_exports iface)
279 ; traceIf (vcat [text "Finished typechecking interface for" <+> ppr (mi_module iface),
280 text "Type envt:" <+> ppr type_env])
281 ; return $ ModDetails { md_types = type_env
283 , md_fam_insts = fam_insts
286 , md_vect_info = vect_info
287 , md_exports = exports
293 %************************************************************************
295 Type and class declarations
297 %************************************************************************
300 tcHiBootIface :: HscSource -> Module -> TcRn ModDetails
301 -- Load the hi-boot iface for the module being compiled,
302 -- if it indeed exists in the transitive closure of imports
303 -- Return the ModDetails, empty if no hi-boot iface
304 tcHiBootIface hsc_src mod
305 | isHsBoot hsc_src -- Already compiling a hs-boot file
306 = return emptyModDetails
308 = do { traceIf (text "loadHiBootInterface" <+> ppr mod)
311 ; if not (isOneShot mode)
312 -- In --make and interactive mode, if this module has an hs-boot file
313 -- we'll have compiled it already, and it'll be in the HPT
315 -- We check wheher the interface is a *boot* interface.
316 -- It can happen (when using GHC from Visual Studio) that we
317 -- compile a module in TypecheckOnly mode, with a stable,
318 -- fully-populated HPT. In that case the boot interface isn't there
319 -- (it's been replaced by the mother module) so we can't check it.
320 -- And that's fine, because if M's ModInfo is in the HPT, then
321 -- it's been compiled once, and we don't need to check the boot iface
322 then do { hpt <- getHpt
323 ; case lookupUFM hpt (moduleName mod) of
324 Just info | mi_boot (hm_iface info)
325 -> return (hm_details info)
326 _ -> return emptyModDetails }
329 -- OK, so we're in one-shot mode.
330 -- In that case, we're read all the direct imports by now,
331 -- so eps_is_boot will record if any of our imports mention us by
332 -- way of hi-boot file
334 ; case lookupUFM (eps_is_boot eps) (moduleName mod) of {
335 Nothing -> return emptyModDetails ; -- The typical case
337 Just (_, False) -> failWithTc moduleLoop ;
338 -- Someone below us imported us!
339 -- This is a loop with no hi-boot in the way
341 Just (_mod, True) -> -- There's a hi-boot interface below us
343 do { read_result <- findAndReadIface
347 ; case read_result of
348 Failed err -> failWithTc (elaborate err)
349 Succeeded (iface, _path) -> typecheckIface iface
352 need = ptext (sLit "Need the hi-boot interface for") <+> ppr mod
353 <+> ptext (sLit "to compare against the Real Thing")
355 moduleLoop = ptext (sLit "Circular imports: module") <+> quotes (ppr mod)
356 <+> ptext (sLit "depends on itself")
358 elaborate err = hang (ptext (sLit "Could not find hi-boot interface for") <+>
359 quotes (ppr mod) <> colon) 4 err
363 %************************************************************************
365 Type and class declarations
367 %************************************************************************
369 When typechecking a data type decl, we *lazily* (via forkM) typecheck
370 the constructor argument types. This is in the hope that we may never
371 poke on those argument types, and hence may never need to load the
372 interface files for types mentioned in the arg types.
375 data Foo.S = MkS Baz.T
376 Mabye we can get away without even loading the interface for Baz!
378 This is not just a performance thing. Suppose we have
379 data Foo.S = MkS Baz.T
380 data Baz.T = MkT Foo.S
381 (in different interface files, of course).
382 Now, first we load and typecheck Foo.S, and add it to the type envt.
383 If we do explore MkS's argument, we'll load and typecheck Baz.T.
384 If we explore MkT's argument we'll find Foo.S already in the envt.
386 If we typechecked constructor args eagerly, when loading Foo.S we'd try to
387 typecheck the type Baz.T. So we'd fault in Baz.T... and then need Foo.S...
388 which isn't done yet.
390 All very cunning. However, there is a rather subtle gotcha which bit
391 me when developing this stuff. When we typecheck the decl for S, we
392 extend the type envt with S, MkS, and all its implicit Ids. Suppose
393 (a bug, but it happened) that the list of implicit Ids depended in
394 turn on the constructor arg types. Then the following sequence of
396 * we build a thunk <t> for the constructor arg tys
397 * we build a thunk for the extended type environment (depends on <t>)
398 * we write the extended type envt into the global EPS mutvar
400 Now we look something up in the type envt
402 * which reads the global type envt out of the global EPS mutvar
403 * but that depends in turn on <t>
405 It's subtle, because, it'd work fine if we typechecked the constructor args
406 eagerly -- they don't need the extended type envt. They just get the extended
407 type envt by accident, because they look at it later.
409 What this means is that the implicitTyThings MUST NOT DEPEND on any of
414 tcIfaceDecl :: Bool -- True <=> discard IdInfo on IfaceId bindings
418 tcIfaceDecl ignore_prags (IfaceId {ifName = occ_name, ifType = iface_type,
419 ifIdDetails = details, ifIdInfo = info})
420 = do { name <- lookupIfaceTop occ_name
421 ; ty <- tcIfaceType iface_type
422 ; details <- tcIdDetails ty details
423 ; info <- tcIdInfo ignore_prags name ty info
424 ; return (AnId (mkGlobalId details name ty info)) }
426 tcIfaceDecl _ (IfaceData {ifName = occ_name,
428 ifCtxt = ctxt, ifGadtSyntax = gadt_syn,
431 ifGeneric = want_generic,
432 ifFamInst = mb_family })
433 = bindIfaceTyVars_AT tv_bndrs $ \ tyvars -> do
434 { tc_name <- lookupIfaceTop occ_name
435 ; tycon <- fixM ( \ tycon -> do
436 { stupid_theta <- tcIfaceCtxt ctxt
437 ; mb_fam_inst <- tcFamInst mb_family
438 ; cons <- tcIfaceDataCons tc_name tycon tyvars rdr_cons
439 ; buildAlgTyCon tc_name tyvars stupid_theta
440 cons is_rec want_generic gadt_syn mb_fam_inst
442 ; traceIf (text "tcIfaceDecl4" <+> ppr tycon)
443 ; return (ATyCon tycon) }
445 tcIfaceDecl _ (IfaceSyn {ifName = occ_name, ifTyVars = tv_bndrs,
446 ifSynRhs = mb_rhs_ty,
447 ifSynKind = kind, ifFamInst = mb_family})
448 = bindIfaceTyVars_AT tv_bndrs $ \ tyvars -> do
449 { tc_name <- lookupIfaceTop occ_name
450 ; rhs_kind <- tcIfaceType kind -- Note [Synonym kind loop]
451 ; ~(rhs, fam) <- forkM (mk_doc tc_name) $
452 do { rhs <- tc_syn_rhs rhs_kind mb_rhs_ty
453 ; fam <- tcFamInst mb_family
454 ; return (rhs, fam) }
455 ; tycon <- buildSynTyCon tc_name tyvars rhs rhs_kind fam
456 ; return $ ATyCon tycon
459 mk_doc n = ptext (sLit "Type syonym") <+> ppr n
460 tc_syn_rhs kind Nothing = return (OpenSynTyCon kind Nothing)
461 tc_syn_rhs _ (Just ty) = do { rhs_ty <- tcIfaceType ty
462 ; return (SynonymTyCon rhs_ty) }
464 tcIfaceDecl ignore_prags
465 (IfaceClass {ifCtxt = rdr_ctxt, ifName = occ_name,
466 ifTyVars = tv_bndrs, ifFDs = rdr_fds,
467 ifATs = rdr_ats, ifSigs = rdr_sigs,
469 -- ToDo: in hs-boot files we should really treat abstract classes specially,
470 -- as we do abstract tycons
471 = bindIfaceTyVars tv_bndrs $ \ tyvars -> do
472 { cls_name <- lookupIfaceTop occ_name
473 ; ctxt <- tcIfaceCtxt rdr_ctxt
474 ; sigs <- mapM tc_sig rdr_sigs
475 ; fds <- mapM tc_fd rdr_fds
476 ; ats' <- mapM (tcIfaceDecl ignore_prags) rdr_ats
477 ; let ats = map (setAssocFamilyPermutation tyvars) ats'
478 ; cls <- buildClass ignore_prags cls_name tyvars ctxt fds ats sigs tc_isrec
479 ; return (AClass cls) }
481 tc_sig (IfaceClassOp occ dm rdr_ty)
482 = do { op_name <- lookupIfaceTop occ
483 ; op_ty <- forkM (mk_doc op_name rdr_ty) (tcIfaceType rdr_ty)
484 -- Must be done lazily for just the same reason as the
485 -- type of a data con; to avoid sucking in types that
486 -- it mentions unless it's necessray to do so
487 ; return (op_name, dm, op_ty) }
489 mk_doc op_name op_ty = ptext (sLit "Class op") <+> sep [ppr op_name, ppr op_ty]
491 tc_fd (tvs1, tvs2) = do { tvs1' <- mapM tcIfaceTyVar tvs1
492 ; tvs2' <- mapM tcIfaceTyVar tvs2
493 ; return (tvs1', tvs2') }
495 tcIfaceDecl _ (IfaceForeign {ifName = rdr_name, ifExtName = ext_name})
496 = do { name <- lookupIfaceTop rdr_name
497 ; return (ATyCon (mkForeignTyCon name ext_name
500 tcFamInst :: Maybe (IfaceTyCon, [IfaceType]) -> IfL (Maybe (TyCon, [Type]))
501 tcFamInst Nothing = return Nothing
502 tcFamInst (Just (fam, tys)) = do { famTyCon <- tcIfaceTyCon fam
503 ; insttys <- mapM tcIfaceType tys
504 ; return $ Just (famTyCon, insttys) }
506 tcIfaceDataCons :: Name -> TyCon -> [TyVar] -> IfaceConDecls -> IfL AlgTyConRhs
507 tcIfaceDataCons tycon_name tycon _ if_cons
509 IfAbstractTyCon -> return mkAbstractTyConRhs
510 IfOpenDataTyCon -> return mkOpenDataTyConRhs
511 IfDataTyCon cons -> do { data_cons <- mapM tc_con_decl cons
512 ; return (mkDataTyConRhs data_cons) }
513 IfNewTyCon con -> do { data_con <- tc_con_decl con
514 ; mkNewTyConRhs tycon_name tycon data_con }
516 tc_con_decl (IfCon { ifConInfix = is_infix,
517 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
518 ifConOcc = occ, ifConCtxt = ctxt, ifConEqSpec = spec,
519 ifConArgTys = args, ifConFields = field_lbls,
520 ifConStricts = stricts})
521 = bindIfaceTyVars univ_tvs $ \ univ_tyvars -> do
522 bindIfaceTyVars ex_tvs $ \ ex_tyvars -> do
523 { name <- lookupIfaceTop occ
524 ; eq_spec <- tcIfaceEqSpec spec
525 ; theta <- tcIfaceCtxt ctxt -- Laziness seems not worth the bother here
526 -- At one stage I thought that this context checking *had*
527 -- to be lazy, because of possible mutual recursion between the
528 -- type and the classe:
530 -- class Real a where { toRat :: a -> Ratio Integer }
531 -- data (Real a) => Ratio a = ...
532 -- But now I think that the laziness in checking class ops breaks
533 -- the loop, so no laziness needed
535 -- Read the argument types, but lazily to avoid faulting in
536 -- the component types unless they are really needed
537 ; arg_tys <- forkM (mk_doc name) (mapM tcIfaceType args)
538 ; lbl_names <- mapM lookupIfaceTop field_lbls
540 -- Remember, tycon is the representation tycon
541 ; let orig_res_ty = mkFamilyTyConApp tycon
542 (substTyVars (mkTopTvSubst eq_spec) univ_tyvars)
544 ; buildDataCon name is_infix {- Not infix -}
546 univ_tyvars ex_tyvars
548 arg_tys orig_res_ty tycon
550 mk_doc con_name = ptext (sLit "Constructor") <+> ppr con_name
552 tcIfaceEqSpec :: [(OccName, IfaceType)] -> IfL [(TyVar, Type)]
556 do_item (occ, if_ty) = do { tv <- tcIfaceTyVar (occNameFS occ)
557 ; ty <- tcIfaceType if_ty
561 Note [Synonym kind loop]
562 ~~~~~~~~~~~~~~~~~~~~~~~~
563 Notice that we eagerly grab the *kind* from the interface file, but
564 build a forkM thunk for the *rhs* (and family stuff). To see why,
565 consider this (Trac #2412)
567 M.hs: module M where { import X; data T = MkT S }
568 X.hs: module X where { import {-# SOURCE #-} M; type S = T }
569 M.hs-boot: module M where { data T }
571 When kind-checking M.hs we need S's kind. But we do not want to
572 find S's kind from (typeKind S-rhs), because we don't want to look at
573 S-rhs yet! Since S is imported from X.hi, S gets just one chance to
574 be defined, and we must not do that until we've finished with M.T.
576 Solution: record S's kind in the interface file; now we can safely
579 %************************************************************************
583 %************************************************************************
586 tcIfaceInst :: IfaceInst -> IfL Instance
587 tcIfaceInst (IfaceInst { ifDFun = dfun_occ, ifOFlag = oflag,
588 ifInstCls = cls, ifInstTys = mb_tcs })
589 = do { dfun <- forkM (ptext (sLit "Dict fun") <+> ppr dfun_occ) $
590 tcIfaceExtId dfun_occ
591 ; let mb_tcs' = map (fmap ifaceTyConName) mb_tcs
592 ; return (mkImportedInstance cls mb_tcs' dfun oflag) }
594 tcIfaceFamInst :: IfaceFamInst -> IfL FamInst
595 tcIfaceFamInst (IfaceFamInst { ifFamInstTyCon = tycon,
596 ifFamInstFam = fam, ifFamInstTys = mb_tcs })
597 -- { tycon' <- forkM (ptext (sLit "Inst tycon") <+> ppr tycon) $
598 -- the above line doesn't work, but this below does => CPP in Haskell = evil!
599 = do tycon' <- forkM (text ("Inst tycon") <+> ppr tycon) $
601 let mb_tcs' = map (fmap ifaceTyConName) mb_tcs
602 return (mkImportedFamInst fam mb_tcs' tycon')
606 %************************************************************************
610 %************************************************************************
612 We move a IfaceRule from eps_rules to eps_rule_base when all its LHS free vars
613 are in the type environment. However, remember that typechecking a Rule may
614 (as a side effect) augment the type envt, and so we may need to iterate the process.
617 tcIfaceRules :: Bool -- True <=> ignore rules
620 tcIfaceRules ignore_prags if_rules
621 | ignore_prags = return []
622 | otherwise = mapM tcIfaceRule if_rules
624 tcIfaceRule :: IfaceRule -> IfL CoreRule
625 tcIfaceRule (IfaceRule {ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
626 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
627 = do { ~(bndrs', args', rhs') <-
628 -- Typecheck the payload lazily, in the hope it'll never be looked at
629 forkM (ptext (sLit "Rule") <+> ftext name) $
630 bindIfaceBndrs bndrs $ \ bndrs' ->
631 do { args' <- mapM tcIfaceExpr args
632 ; rhs' <- tcIfaceExpr rhs
633 ; return (bndrs', args', rhs') }
634 ; let mb_tcs = map ifTopFreeName args
635 ; return (Rule { ru_name = name, ru_fn = fn, ru_act = act,
636 ru_bndrs = bndrs', ru_args = args',
637 ru_rhs = occurAnalyseExpr rhs',
639 ru_local = False }) } -- An imported RULE is never for a local Id
640 -- or, even if it is (module loop, perhaps)
641 -- we'll just leave it in the non-local set
643 -- This function *must* mirror exactly what Rules.topFreeName does
644 -- We could have stored the ru_rough field in the iface file
645 -- but that would be redundant, I think.
646 -- The only wrinkle is that we must not be deceived by
647 -- type syononyms at the top of a type arg. Since
648 -- we can't tell at this point, we are careful not
649 -- to write them out in coreRuleToIfaceRule
650 ifTopFreeName :: IfaceExpr -> Maybe Name
651 ifTopFreeName (IfaceType (IfaceTyConApp tc _ )) = Just (ifaceTyConName tc)
652 ifTopFreeName (IfaceApp f _) = ifTopFreeName f
653 ifTopFreeName (IfaceExt n) = Just n
654 ifTopFreeName _ = Nothing
658 %************************************************************************
662 %************************************************************************
665 tcIfaceAnnotations :: [IfaceAnnotation] -> IfL [Annotation]
666 tcIfaceAnnotations = mapM tcIfaceAnnotation
668 tcIfaceAnnotation :: IfaceAnnotation -> IfL Annotation
669 tcIfaceAnnotation (IfaceAnnotation target serialized) = do
670 target' <- tcIfaceAnnTarget target
671 return $ Annotation {
672 ann_target = target',
673 ann_value = serialized
676 tcIfaceAnnTarget :: IfaceAnnTarget -> IfL (AnnTarget Name)
677 tcIfaceAnnTarget (NamedTarget occ) = do
678 name <- lookupIfaceTop occ
679 return $ NamedTarget name
680 tcIfaceAnnTarget (ModuleTarget mod) = do
681 return $ ModuleTarget mod
686 %************************************************************************
688 Vectorisation information
690 %************************************************************************
693 tcIfaceVectInfo :: Module -> TypeEnv -> IfaceVectInfo -> IfL VectInfo
694 tcIfaceVectInfo mod typeEnv (IfaceVectInfo
695 { ifaceVectInfoVar = vars
696 , ifaceVectInfoTyCon = tycons
697 , ifaceVectInfoTyConReuse = tyconsReuse
699 = do { vVars <- mapM vectVarMapping vars
700 ; tyConRes1 <- mapM vectTyConMapping tycons
701 ; tyConRes2 <- mapM vectTyConReuseMapping tyconsReuse
702 ; let (vTyCons, vDataCons, vPAs, vIsos) = unzip4 (tyConRes1 ++ tyConRes2)
704 { vectInfoVar = mkVarEnv vVars
705 , vectInfoTyCon = mkNameEnv vTyCons
706 , vectInfoDataCon = mkNameEnv (concat vDataCons)
707 , vectInfoPADFun = mkNameEnv vPAs
708 , vectInfoIso = mkNameEnv vIsos
713 = do { vName <- lookupOrig mod (mkVectOcc (nameOccName name))
714 ; let { var = lookupVar name
715 ; vVar = lookupVar vName
717 ; return (var, (var, vVar))
719 vectTyConMapping name
720 = do { vName <- lookupOrig mod (mkVectTyConOcc (nameOccName name))
721 ; paName <- lookupOrig mod (mkPADFunOcc (nameOccName name))
722 ; isoName <- lookupOrig mod (mkVectIsoOcc (nameOccName name))
723 ; let { tycon = lookupTyCon name
724 ; vTycon = lookupTyCon vName
725 ; paTycon = lookupVar paName
726 ; isoTycon = lookupVar isoName
728 ; vDataCons <- mapM vectDataConMapping (tyConDataCons tycon)
729 ; return ((name, (tycon, vTycon)), -- (T, T_v)
730 vDataCons, -- list of (Ci, Ci_v)
731 (vName, (vTycon, paTycon)), -- (T_v, paT)
732 (name, (tycon, isoTycon))) -- (T, isoT)
734 vectTyConReuseMapping name
735 = do { paName <- lookupOrig mod (mkPADFunOcc (nameOccName name))
736 ; isoName <- lookupOrig mod (mkVectIsoOcc (nameOccName name))
737 ; let { tycon = lookupTyCon name
738 ; paTycon = lookupVar paName
739 ; isoTycon = lookupVar isoName
740 ; vDataCons = [ (dataConName dc, (dc, dc))
741 | dc <- tyConDataCons tycon]
743 ; return ((name, (tycon, tycon)), -- (T, T)
744 vDataCons, -- list of (Ci, Ci)
745 (name, (tycon, paTycon)), -- (T, paT)
746 (name, (tycon, isoTycon))) -- (T, isoT)
748 vectDataConMapping datacon
749 = do { let name = dataConName datacon
750 ; vName <- lookupOrig mod (mkVectDataConOcc (nameOccName name))
751 ; let vDataCon = lookupDataCon vName
752 ; return (name, (datacon, vDataCon))
755 lookupVar name = case lookupTypeEnv typeEnv name of
756 Just (AnId var) -> var
758 panic "TcIface.tcIfaceVectInfo: not an id"
760 panic "TcIface.tcIfaceVectInfo: unknown name"
761 lookupTyCon name = case lookupTypeEnv typeEnv name of
762 Just (ATyCon tc) -> tc
764 panic "TcIface.tcIfaceVectInfo: not a tycon"
766 panic "TcIface.tcIfaceVectInfo: unknown name"
767 lookupDataCon name = case lookupTypeEnv typeEnv name of
768 Just (ADataCon dc) -> dc
770 panic "TcIface.tcIfaceVectInfo: not a datacon"
772 panic "TcIface.tcIfaceVectInfo: unknown name"
775 %************************************************************************
779 %************************************************************************
782 tcIfaceType :: IfaceType -> IfL Type
783 tcIfaceType (IfaceTyVar n) = do { tv <- tcIfaceTyVar n; return (TyVarTy tv) }
784 tcIfaceType (IfaceAppTy t1 t2) = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (AppTy t1' t2') }
785 tcIfaceType (IfaceFunTy t1 t2) = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (FunTy t1' t2') }
786 tcIfaceType (IfaceTyConApp tc ts) = do { tc' <- tcIfaceTyCon tc; ts' <- tcIfaceTypes ts; return (mkTyConApp tc' ts') }
787 tcIfaceType (IfaceForAllTy tv t) = bindIfaceTyVar tv $ \ tv' -> do { t' <- tcIfaceType t; return (ForAllTy tv' t') }
788 tcIfaceType (IfacePredTy st) = do { st' <- tcIfacePredType st; return (PredTy st') }
790 tcIfaceTypes :: [IfaceType] -> IfL [Type]
791 tcIfaceTypes tys = mapM tcIfaceType tys
793 -----------------------------------------
794 tcIfacePredType :: IfacePredType -> IfL PredType
795 tcIfacePredType (IfaceClassP cls ts) = do { cls' <- tcIfaceClass cls; ts' <- tcIfaceTypes ts; return (ClassP cls' ts') }
796 tcIfacePredType (IfaceIParam ip t) = do { ip' <- newIPName ip; t' <- tcIfaceType t; return (IParam ip' t') }
797 tcIfacePredType (IfaceEqPred t1 t2) = do { t1' <- tcIfaceType t1; t2' <- tcIfaceType t2; return (EqPred t1' t2') }
799 -----------------------------------------
800 tcIfaceCtxt :: IfaceContext -> IfL ThetaType
801 tcIfaceCtxt sts = mapM tcIfacePredType sts
805 %************************************************************************
809 %************************************************************************
812 tcIfaceExpr :: IfaceExpr -> IfL CoreExpr
813 tcIfaceExpr (IfaceType ty)
814 = Type <$> tcIfaceType ty
816 tcIfaceExpr (IfaceLcl name)
817 = Var <$> tcIfaceLclId name
819 tcIfaceExpr (IfaceTick modName tickNo)
820 = Var <$> tcIfaceTick modName tickNo
822 tcIfaceExpr (IfaceExt gbl)
823 = Var <$> tcIfaceExtId gbl
825 tcIfaceExpr (IfaceLit lit)
828 tcIfaceExpr (IfaceFCall cc ty) = do
829 ty' <- tcIfaceType ty
831 return (Var (mkFCallId u cc ty'))
833 tcIfaceExpr (IfaceTuple boxity args) = do
834 args' <- mapM tcIfaceExpr args
835 -- Put the missing type arguments back in
836 let con_args = map (Type . exprType) args' ++ args'
837 return (mkApps (Var con_id) con_args)
840 con_id = dataConWorkId (tupleCon boxity arity)
843 tcIfaceExpr (IfaceLam bndr body)
844 = bindIfaceBndr bndr $ \bndr' ->
845 Lam bndr' <$> tcIfaceExpr body
847 tcIfaceExpr (IfaceApp fun arg)
848 = App <$> tcIfaceExpr fun <*> tcIfaceExpr arg
850 tcIfaceExpr (IfaceCase scrut case_bndr ty alts) = do
851 scrut' <- tcIfaceExpr scrut
852 case_bndr_name <- newIfaceName (mkVarOccFS case_bndr)
854 scrut_ty = exprType scrut'
855 case_bndr' = mkLocalId case_bndr_name scrut_ty
856 tc_app = splitTyConApp scrut_ty
857 -- NB: Won't always succeed (polymoprhic case)
858 -- but won't be demanded in those cases
859 -- NB: not tcSplitTyConApp; we are looking at Core here
860 -- look through non-rec newtypes to find the tycon that
861 -- corresponds to the datacon in this case alternative
863 extendIfaceIdEnv [case_bndr'] $ do
864 alts' <- mapM (tcIfaceAlt scrut' tc_app) alts
865 ty' <- tcIfaceType ty
866 return (Case scrut' case_bndr' ty' alts')
868 tcIfaceExpr (IfaceLet (IfaceNonRec bndr rhs) body) = do
869 rhs' <- tcIfaceExpr rhs
870 id <- tcIfaceLetBndr bndr
871 body' <- extendIfaceIdEnv [id] (tcIfaceExpr body)
872 return (Let (NonRec id rhs') body')
874 tcIfaceExpr (IfaceLet (IfaceRec pairs) body) = do
875 ids <- mapM tcIfaceLetBndr bndrs
876 extendIfaceIdEnv ids $ do
877 rhss' <- mapM tcIfaceExpr rhss
878 body' <- tcIfaceExpr body
879 return (Let (Rec (ids `zip` rhss')) body')
881 (bndrs, rhss) = unzip pairs
883 tcIfaceExpr (IfaceCast expr co) = do
884 expr' <- tcIfaceExpr expr
885 co' <- tcIfaceType co
886 return (Cast expr' co')
888 tcIfaceExpr (IfaceNote note expr) = do
889 expr' <- tcIfaceExpr expr
891 IfaceSCC cc -> return (Note (SCC cc) expr')
892 IfaceCoreNote n -> return (Note (CoreNote n) expr')
894 -------------------------
895 tcIfaceAlt :: CoreExpr -> (TyCon, [Type])
896 -> (IfaceConAlt, [FastString], IfaceExpr)
897 -> IfL (AltCon, [TyVar], CoreExpr)
898 tcIfaceAlt _ _ (IfaceDefault, names, rhs)
899 = ASSERT( null names ) do
900 rhs' <- tcIfaceExpr rhs
901 return (DEFAULT, [], rhs')
903 tcIfaceAlt _ _ (IfaceLitAlt lit, names, rhs)
904 = ASSERT( null names ) do
905 rhs' <- tcIfaceExpr rhs
906 return (LitAlt lit, [], rhs')
908 -- A case alternative is made quite a bit more complicated
909 -- by the fact that we omit type annotations because we can
910 -- work them out. True enough, but its not that easy!
911 tcIfaceAlt scrut (tycon, inst_tys) (IfaceDataAlt data_occ, arg_strs, rhs)
912 = do { con <- tcIfaceDataCon data_occ
913 ; when (debugIsOn && not (con `elem` tyConDataCons tycon))
914 (failIfM (ppr scrut $$ ppr con $$ ppr tycon $$ ppr (tyConDataCons tycon)))
915 ; tcIfaceDataAlt con inst_tys arg_strs rhs }
917 tcIfaceAlt _ (tycon, inst_tys) (IfaceTupleAlt _boxity, arg_occs, rhs)
918 = ASSERT( isTupleTyCon tycon )
919 do { let [data_con] = tyConDataCons tycon
920 ; tcIfaceDataAlt data_con inst_tys arg_occs rhs }
922 tcIfaceDataAlt :: DataCon -> [Type] -> [FastString] -> IfaceExpr
923 -> IfL (AltCon, [TyVar], CoreExpr)
924 tcIfaceDataAlt con inst_tys arg_strs rhs
925 = do { us <- newUniqueSupply
926 ; let uniqs = uniqsFromSupply us
927 ; let (ex_tvs, co_tvs, arg_ids)
928 = dataConRepFSInstPat arg_strs uniqs con inst_tys
929 all_tvs = ex_tvs ++ co_tvs
931 ; rhs' <- extendIfaceTyVarEnv all_tvs $
932 extendIfaceIdEnv arg_ids $
934 ; return (DataAlt con, all_tvs ++ arg_ids, rhs') }
939 tcExtCoreBindings :: [IfaceBinding] -> IfL [CoreBind] -- Used for external core
940 tcExtCoreBindings [] = return []
941 tcExtCoreBindings (b:bs) = do_one b (tcExtCoreBindings bs)
943 do_one :: IfaceBinding -> IfL [CoreBind] -> IfL [CoreBind]
944 do_one (IfaceNonRec bndr rhs) thing_inside
945 = do { rhs' <- tcIfaceExpr rhs
946 ; bndr' <- newExtCoreBndr bndr
947 ; extendIfaceIdEnv [bndr'] $ do
948 { core_binds <- thing_inside
949 ; return (NonRec bndr' rhs' : core_binds) }}
951 do_one (IfaceRec pairs) thing_inside
952 = do { bndrs' <- mapM newExtCoreBndr bndrs
953 ; extendIfaceIdEnv bndrs' $ do
954 { rhss' <- mapM tcIfaceExpr rhss
955 ; core_binds <- thing_inside
956 ; return (Rec (bndrs' `zip` rhss') : core_binds) }}
958 (bndrs,rhss) = unzip pairs
962 %************************************************************************
966 %************************************************************************
969 tcIdDetails :: Type -> IfaceIdDetails -> IfL IdDetails
970 tcIdDetails _ IfVanillaId = return VanillaId
971 tcIdDetails ty IfDFunId
972 = return (DFunId (isNewTyCon (classTyCon cls)))
974 (_, cls, _) = tcSplitDFunTy ty
976 tcIdDetails _ (IfRecSelId tc naughty)
977 = do { tc' <- tcIfaceTyCon tc
978 ; return (RecSelId { sel_tycon = tc', sel_naughty = naughty }) }
980 tcIdInfo :: Bool -> Name -> Type -> IfaceIdInfo -> IfL IdInfo
981 tcIdInfo ignore_prags name ty info
982 | ignore_prags = return vanillaIdInfo
983 | otherwise = case info of
984 NoInfo -> return vanillaIdInfo
985 HasInfo info -> foldlM tcPrag init_info info
987 -- Set the CgInfo to something sensible but uninformative before
988 -- we start; default assumption is that it has CAFs
989 init_info = vanillaIdInfo
991 tcPrag :: IdInfo -> IfaceInfoItem -> IfL IdInfo
992 tcPrag info HsNoCafRefs = return (info `setCafInfo` NoCafRefs)
993 tcPrag info (HsArity arity) = return (info `setArityInfo` arity)
994 tcPrag info (HsStrictness str) = return (info `setStrictnessInfo` Just str)
995 tcPrag info (HsInline prag) = return (info `setInlinePragInfo` prag)
997 -- The next two are lazy, so they don't transitively suck stuff in
998 tcPrag info (HsUnfold lb if_unf)
999 = do { unf <- tcUnfolding name ty info if_unf
1000 ; let info1 | lb = info `setOccInfo` nonRuleLoopBreaker
1002 ; return (info1 `setUnfoldingInfoLazily` unf) }
1006 tcUnfolding :: Name -> Type -> IdInfo -> IfaceUnfolding -> IfL Unfolding
1007 tcUnfolding name _ info (IfCoreUnfold if_expr)
1008 = do { mb_expr <- tcPragExpr name if_expr
1009 ; return (case mb_expr of
1010 Nothing -> NoUnfolding
1011 Just expr -> mkTopUnfolding is_bottoming expr) }
1013 -- Strictness should occur before unfolding!
1014 is_bottoming = case strictnessInfo info of
1015 Just sig -> isBottomingSig sig
1018 tcUnfolding name _ _ (IfCompulsory if_expr)
1019 = do { mb_expr <- tcPragExpr name if_expr
1020 ; return (case mb_expr of
1021 Nothing -> NoUnfolding
1022 Just expr -> mkCompulsoryUnfolding expr) }
1024 tcUnfolding name _ _ (IfInlineRule arity unsat_ok boring_ok if_expr)
1025 = do { mb_expr <- tcPragExpr name if_expr
1026 ; return (case mb_expr of
1027 Nothing -> NoUnfolding
1028 Just expr -> mkCoreUnfolding True InlineRule expr arity
1029 (UnfWhen unsat_ok boring_ok))
1032 tcUnfolding name ty info (IfWrapper arity wkr)
1033 = do { mb_wkr_id <- forkM_maybe doc (tcIfaceExtId wkr)
1034 ; us <- newUniqueSupply
1035 ; return (case mb_wkr_id of
1036 Nothing -> noUnfolding
1037 Just wkr_id -> make_inline_rule wkr_id us) }
1039 doc = text "Worker for" <+> ppr name
1041 make_inline_rule wkr_id us
1042 = mkWwInlineRule wkr_id
1043 (initUs_ us (mkWrapper ty strict_sig) wkr_id)
1046 -- Again we rely here on strictness info always appearing
1048 strict_sig = case strictnessInfo info of
1050 Nothing -> pprPanic "Worker info but no strictness for" (ppr wkr)
1052 tcUnfolding name dfun_ty _ (IfDFunUnfold ops)
1053 = do { mb_ops1 <- forkM_maybe doc $ mapM tcIfaceExpr ops
1054 ; return (case mb_ops1 of
1055 Nothing -> noUnfolding
1056 Just ops1 -> mkDFunUnfolding dfun_ty ops1) }
1058 doc = text "Class ops for dfun" <+> ppr name
1061 For unfoldings we try to do the job lazily, so that we never type check
1062 an unfolding that isn't going to be looked at.
1065 tcPragExpr :: Name -> IfaceExpr -> IfL (Maybe CoreExpr)
1066 tcPragExpr name expr
1067 = forkM_maybe doc $ do
1068 core_expr' <- tcIfaceExpr expr
1070 -- Check for type consistency in the unfolding
1071 ifOptM Opt_DoCoreLinting $ do
1072 in_scope <- get_in_scope_ids
1073 case lintUnfolding noSrcLoc in_scope core_expr' of
1074 Nothing -> return ()
1075 Just fail_msg -> pprPanic "Iface Lint failure" (hang doc 2 fail_msg)
1079 doc = text "Unfolding of" <+> ppr name
1080 get_in_scope_ids -- Urgh; but just for linting
1082 do { env <- getGblEnv
1083 ; case if_rec_types env of {
1084 Nothing -> return [] ;
1085 Just (_, get_env) -> do
1086 { type_env <- get_env
1087 ; return (typeEnvIds type_env) }}}
1092 %************************************************************************
1094 Getting from Names to TyThings
1096 %************************************************************************
1099 tcIfaceGlobal :: Name -> IfL TyThing
1101 | Just thing <- wiredInNameTyThing_maybe name
1102 -- Wired-in things include TyCons, DataCons, and Ids
1103 = do { ifCheckWiredInThing thing; return thing }
1105 = do { env <- getGblEnv
1106 ; case if_rec_types env of { -- Note [Tying the knot]
1107 Just (mod, get_type_env)
1108 | nameIsLocalOrFrom mod name
1109 -> do -- It's defined in the module being compiled
1110 { type_env <- setLclEnv () get_type_env -- yuk
1111 ; case lookupNameEnv type_env name of
1112 Just thing -> return thing
1113 Nothing -> pprPanic "tcIfaceGlobal (local): not found:"
1114 (ppr name $$ ppr type_env) }
1118 { hsc_env <- getTopEnv
1119 ; mb_thing <- liftIO (lookupTypeHscEnv hsc_env name)
1120 ; case mb_thing of {
1121 Just thing -> return thing ;
1124 { mb_thing <- importDecl name -- It's imported; go get it
1126 Failed err -> failIfM err
1127 Succeeded thing -> return thing
1130 -- Note [Tying the knot]
1131 -- ~~~~~~~~~~~~~~~~~~~~~
1132 -- The if_rec_types field is used in two situations:
1134 -- a) Compiling M.hs, which indiretly imports Foo.hi, which mentions M.T
1135 -- Then we look up M.T in M's type environment, which is splatted into if_rec_types
1136 -- after we've built M's type envt.
1138 -- b) In ghc --make, during the upsweep, we encounter M.hs, whose interface M.hi
1139 -- is up to date. So we call typecheckIface on M.hi. This splats M.T into
1140 -- if_rec_types so that the (lazily typechecked) decls see all the other decls
1142 -- In case (b) it's important to do the if_rec_types check *before* looking in the HPT
1143 -- Because if M.hs also has M.hs-boot, M.T will *already be* in the HPT, but in its
1144 -- emasculated form (e.g. lacking data constructors).
1146 tcIfaceTyCon :: IfaceTyCon -> IfL TyCon
1147 tcIfaceTyCon IfaceIntTc = tcWiredInTyCon intTyCon
1148 tcIfaceTyCon IfaceBoolTc = tcWiredInTyCon boolTyCon
1149 tcIfaceTyCon IfaceCharTc = tcWiredInTyCon charTyCon
1150 tcIfaceTyCon IfaceListTc = tcWiredInTyCon listTyCon
1151 tcIfaceTyCon IfacePArrTc = tcWiredInTyCon parrTyCon
1152 tcIfaceTyCon (IfaceTupTc bx ar) = tcWiredInTyCon (tupleTyCon bx ar)
1153 tcIfaceTyCon (IfaceAnyTc kind) = do { tc_kind <- tcIfaceType kind
1154 ; tcWiredInTyCon (anyTyConOfKind tc_kind) }
1155 tcIfaceTyCon (IfaceTc name) = do { thing <- tcIfaceGlobal name
1156 ; return (check_tc (tyThingTyCon thing)) }
1159 | debugIsOn = case toIfaceTyCon tc of
1161 _ -> pprTrace "check_tc" (ppr tc) tc
1163 -- we should be okay just returning Kind constructors without extra loading
1164 tcIfaceTyCon IfaceLiftedTypeKindTc = return liftedTypeKindTyCon
1165 tcIfaceTyCon IfaceOpenTypeKindTc = return openTypeKindTyCon
1166 tcIfaceTyCon IfaceUnliftedTypeKindTc = return unliftedTypeKindTyCon
1167 tcIfaceTyCon IfaceArgTypeKindTc = return argTypeKindTyCon
1168 tcIfaceTyCon IfaceUbxTupleKindTc = return ubxTupleKindTyCon
1170 -- Even though we are in an interface file, we want to make
1171 -- sure the instances and RULES of this tycon are loaded
1172 -- Imagine: f :: Double -> Double
1173 tcWiredInTyCon :: TyCon -> IfL TyCon
1174 tcWiredInTyCon tc = do { ifCheckWiredInThing (ATyCon tc)
1177 tcIfaceClass :: Name -> IfL Class
1178 tcIfaceClass name = do { thing <- tcIfaceGlobal name
1179 ; return (tyThingClass thing) }
1181 tcIfaceDataCon :: Name -> IfL DataCon
1182 tcIfaceDataCon name = do { thing <- tcIfaceGlobal name
1184 ADataCon dc -> return dc
1185 _ -> pprPanic "tcIfaceExtDC" (ppr name$$ ppr thing) }
1187 tcIfaceExtId :: Name -> IfL Id
1188 tcIfaceExtId name = do { thing <- tcIfaceGlobal name
1190 AnId id -> return id
1191 _ -> pprPanic "tcIfaceExtId" (ppr name$$ ppr thing) }
1194 %************************************************************************
1198 %************************************************************************
1201 bindIfaceBndr :: IfaceBndr -> (CoreBndr -> IfL a) -> IfL a
1202 bindIfaceBndr (IfaceIdBndr (fs, ty)) thing_inside
1203 = do { name <- newIfaceName (mkVarOccFS fs)
1204 ; ty' <- tcIfaceType ty
1205 ; let id = mkLocalId name ty'
1206 ; extendIfaceIdEnv [id] (thing_inside id) }
1207 bindIfaceBndr (IfaceTvBndr bndr) thing_inside
1208 = bindIfaceTyVar bndr thing_inside
1210 bindIfaceBndrs :: [IfaceBndr] -> ([CoreBndr] -> IfL a) -> IfL a
1211 bindIfaceBndrs [] thing_inside = thing_inside []
1212 bindIfaceBndrs (b:bs) thing_inside
1213 = bindIfaceBndr b $ \ b' ->
1214 bindIfaceBndrs bs $ \ bs' ->
1215 thing_inside (b':bs')
1218 -----------------------
1219 tcIfaceLetBndr :: IfaceLetBndr -> IfL Id
1220 tcIfaceLetBndr (IfLetBndr fs ty info)
1221 = do { name <- newIfaceName (mkVarOccFS fs)
1222 ; ty' <- tcIfaceType ty
1224 NoInfo -> return (mkLocalId name ty')
1225 HasInfo i -> return (mkLocalIdWithInfo name ty' (tc_info i)) }
1227 -- Similar to tcIdInfo, but much simpler
1228 tc_info [] = vanillaIdInfo
1229 tc_info (HsInline p : i) = tc_info i `setInlinePragInfo` p
1230 tc_info (HsArity a : i) = tc_info i `setArityInfo` a
1231 tc_info (HsStrictness s : i) = tc_info i `setStrictnessInfo` Just s
1232 tc_info (other : i) = pprTrace "tcIfaceLetBndr: discarding unexpected IdInfo"
1233 (ppr other) (tc_info i)
1235 -----------------------
1236 newExtCoreBndr :: IfaceLetBndr -> IfL Id
1237 newExtCoreBndr (IfLetBndr var ty _) -- Ignoring IdInfo for now
1238 = do { mod <- getIfModule
1239 ; name <- newGlobalBinder mod (mkVarOccFS var) noSrcSpan
1240 ; ty' <- tcIfaceType ty
1241 ; return (mkLocalId name ty') }
1243 -----------------------
1244 bindIfaceTyVar :: IfaceTvBndr -> (TyVar -> IfL a) -> IfL a
1245 bindIfaceTyVar (occ,kind) thing_inside
1246 = do { name <- newIfaceName (mkTyVarOccFS occ)
1247 ; tyvar <- mk_iface_tyvar name kind
1248 ; extendIfaceTyVarEnv [tyvar] (thing_inside tyvar) }
1250 bindIfaceTyVars :: [IfaceTvBndr] -> ([TyVar] -> IfL a) -> IfL a
1251 bindIfaceTyVars bndrs thing_inside
1252 = do { names <- newIfaceNames (map mkTyVarOccFS occs)
1253 ; tyvars <- zipWithM mk_iface_tyvar names kinds
1254 ; extendIfaceTyVarEnv tyvars (thing_inside tyvars) }
1256 (occs,kinds) = unzip bndrs
1258 mk_iface_tyvar :: Name -> IfaceKind -> IfL TyVar
1259 mk_iface_tyvar name ifKind
1260 = do { kind <- tcIfaceType ifKind
1261 ; if isCoercionKind kind then
1262 return (Var.mkCoVar name kind)
1264 return (Var.mkTyVar name kind) }
1266 bindIfaceTyVars_AT :: [IfaceTvBndr] -> ([TyVar] -> IfL a) -> IfL a
1267 -- Used for type variable in nested associated data/type declarations
1268 -- where some of the type variables are already in scope
1269 -- class C a where { data T a b }
1270 -- Here 'a' is in scope when we look at the 'data T'
1271 bindIfaceTyVars_AT [] thing_inside
1273 bindIfaceTyVars_AT (b@(tv_occ,_) : bs) thing_inside
1274 = bindIfaceTyVars_AT bs $ \ bs' ->
1275 do { mb_tv <- lookupIfaceTyVar tv_occ
1277 Just b' -> thing_inside (b':bs')
1278 Nothing -> bindIfaceTyVar b $ \ b' ->
1279 thing_inside (b':bs') }