PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/ghc-7.0.4/compiler/iface/TcIface.lhs

http://picorec.googlecode.com/
Haskell | 1302 lines | 995 code | 167 blank | 140 comment | 26 complexity | 9b55edc332ddcf3b26f202069665be1e MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file