[project @ 2001-08-15 14:40:24 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / HscMain.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-2000
3 %
4
5 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
6
7 \begin{code}
8 module HscMain ( HscResult(..), hscMain, 
9 #ifdef GHCI
10                  hscStmt, hscThing,
11 #endif
12                  initPersistentCompilerState ) where
13
14 #include "HsVersions.h"
15
16 #ifdef GHCI
17 import ByteCodeGen      ( byteCodeGen )
18 import CoreTidy         ( tidyCoreExpr )
19 import CorePrep         ( corePrepExpr )
20 import Rename           ( renameStmt, renameRdrName )
21 import RdrName          ( mkUnqual, mkQual )
22 import RdrHsSyn         ( RdrNameStmt )
23 import OccName          ( varName, dataName, tcClsName )
24 import Type             ( Type )
25 import Id               ( Id, idName, setGlobalIdDetails )
26 import IdInfo           ( GlobalIdDetails(VanillaGlobal) )
27 import HscTypes         ( InteractiveContext(..) )
28 import PrelNames        ( iNTERACTIVE )
29 import StringBuffer     ( stringToStringBuffer )
30 import FastString       ( mkFastString )
31 import Char             ( isLower )
32 import DriverUtil       ( split_longest_prefix )
33 #endif
34
35 import HsSyn
36
37 import Id               ( idName )
38 import IdInfo           ( CafInfo(..), CgInfoEnv, CgInfo(..) )
39 import StringBuffer     ( hGetStringBuffer, freeStringBuffer )
40 import Parser
41 import Lex              ( PState(..), ParseResult(..) )
42 import SrcLoc           ( mkSrcLoc )
43 import Finder           ( findModule )
44 import Rename           ( checkOldIface, renameModule, closeIfaceDecls )
45 import Rules            ( emptyRuleBase )
46 import PrelInfo         ( wiredInThingEnv, wiredInThings )
47 import PrelNames        ( knownKeyNames )
48 import MkIface          ( mkFinalIface )
49 import TcModule
50 import InstEnv          ( emptyInstEnv )
51 import Desugar
52 import SimplCore
53 import CoreUtils        ( coreBindsSize )
54 import CoreTidy         ( tidyCorePgm )
55 import CorePrep         ( corePrepPgm )
56 import StgSyn
57 import CoreToStg        ( coreToStg )
58 import SimplStg         ( stg2stg )
59 import CodeGen          ( codeGen )
60 import CodeOutput       ( codeOutput )
61
62 import Module           ( ModuleName, moduleName, mkHomeModule, 
63                           moduleUserString )
64 import CmdLineOpts
65 import ErrUtils         ( dumpIfSet_dyn, showPass, printError )
66 import Util             ( unJust )
67 import UniqSupply       ( mkSplitUniqSupply )
68
69 import Bag              ( emptyBag )
70 import Outputable
71 import Interpreter
72 import HscStats         ( ppSourceStats )
73 import HscTypes
74 import FiniteMap        ( FiniteMap, plusFM, emptyFM, addToFM )
75 import OccName          ( OccName )
76 import Name             ( Name, nameModule, nameOccName, getName, isGlobalName )
77 import NameEnv          ( emptyNameEnv, mkNameEnv )
78 import Module           ( Module )
79
80 import IOExts           ( newIORef, readIORef, writeIORef, unsafePerformIO )
81
82 import Monad            ( when )
83 import Maybe            ( isJust, fromJust, catMaybes )
84 import IO
85
86 import MkExternalCore   ( emitExternalCore )
87 \end{code}
88
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection{The main compiler pipeline}
93 %*                                                                      *
94 %************************************************************************
95
96 \begin{code}
97 data HscResult
98    -- compilation failed
99    = HscFail     PersistentCompilerState -- updated PCS
100    -- concluded that it wasn't necessary
101    | HscNoRecomp PersistentCompilerState -- updated PCS
102                  ModDetails              -- new details (HomeSymbolTable additions)
103                  ModIface                -- new iface (if any compilation was done)
104    -- did recompilation
105    | HscRecomp   PersistentCompilerState -- updated PCS
106                  ModDetails              -- new details (HomeSymbolTable additions)
107                  ModIface                -- new iface (if any compilation was done)
108                  Bool                   -- stub_h exists
109                  Bool                   -- stub_c exists
110 #ifdef GHCI
111                  (Maybe ([UnlinkedBCO],ItblEnv)) -- interpreted code, if any
112 #else
113                  (Maybe ())                      -- no interpreted code whatsoever
114 #endif
115
116         -- no errors or warnings; the individual passes
117         -- (parse/rename/typecheck) print messages themselves
118
119 hscMain
120   :: GhciMode
121   -> DynFlags
122   -> Module
123   -> ModuleLocation             -- location info
124   -> Bool                       -- True <=> source unchanged
125   -> Bool                       -- True <=> have an object file (for msgs only)
126   -> Maybe ModIface             -- old interface, if available
127   -> HomeSymbolTable            -- for home module ModDetails
128   -> HomeIfaceTable
129   -> PersistentCompilerState    -- IN: persistent compiler state
130   -> IO HscResult
131
132 hscMain ghci_mode dflags mod location source_unchanged have_object 
133         maybe_old_iface hst hit pcs
134  = do {
135       showPass dflags ("Checking old interface for hs = " 
136                         ++ show (ml_hs_file location)
137                         ++ ", hspp = " ++ show (ml_hspp_file location));
138
139       (pcs_ch, errs_found, (recomp_reqd, maybe_checked_iface))
140          <- _scc_ "checkOldIface"
141             checkOldIface ghci_mode dflags hit hst pcs (ml_hi_file location)
142                 source_unchanged maybe_old_iface;
143
144       if errs_found then
145          return (HscFail pcs_ch)
146       else do {
147
148       let no_old_iface = not (isJust maybe_checked_iface)
149           what_next | recomp_reqd || no_old_iface = hscRecomp 
150                     | otherwise                   = hscNoRecomp
151       ;
152       what_next ghci_mode dflags have_object mod location 
153                 maybe_checked_iface hst hit pcs_ch
154       }}
155
156
157 -- we definitely expect to have the old interface available
158 hscNoRecomp ghci_mode dflags have_object 
159             mod location (Just old_iface) hst hit pcs_ch
160  | ghci_mode == OneShot
161  = do {
162       hPutStrLn stderr "compilation IS NOT required";
163       let { bomb = panic "hscNoRecomp:OneShot" };
164       return (HscNoRecomp pcs_ch bomb bomb)
165       }
166  | otherwise
167  = do {
168       when (verbosity dflags >= 1) $
169                 hPutStrLn stderr ("Skipping  " ++ 
170                         compMsg have_object mod location);
171
172       -- CLOSURE
173       (pcs_cl, closure_errs, cl_hs_decls) 
174          <- closeIfaceDecls dflags hit hst pcs_ch old_iface ;
175       if closure_errs then 
176          return (HscFail pcs_cl) 
177       else do {
178
179       -- TYPECHECK
180       maybe_tc_result 
181         <- typecheckIface dflags pcs_cl hst old_iface cl_hs_decls;
182
183       case maybe_tc_result of {
184          Nothing -> return (HscFail pcs_cl);
185          Just (pcs_tc, new_details) ->
186
187       return (HscNoRecomp pcs_tc new_details old_iface)
188       }}}
189
190 compMsg use_object mod location =
191     mod_str ++ take (max 0 (16 - length mod_str)) (repeat ' ')
192     ++" ( " ++ unJust "hscRecomp" (ml_hs_file location) ++ ", "
193     ++ (if use_object
194           then unJust "hscRecomp" (ml_obj_file location)
195           else "interpreted")
196     ++ " )"
197  where mod_str = moduleUserString mod
198
199
200 hscRecomp ghci_mode dflags have_object 
201           mod location maybe_checked_iface hst hit pcs_ch
202  = do   {
203           -- what target are we shooting for?
204         ; let toInterp = dopt_HscLang dflags == HscInterpreted
205
206         ; when (verbosity dflags >= 1) $
207                 hPutStrLn stderr ("Compiling " ++ 
208                         compMsg (not toInterp) mod location);
209
210             -------------------
211             -- PARSE
212             -------------------
213         ; maybe_parsed <- myParseModule dflags 
214                              (unJust "hscRecomp:hspp" (ml_hspp_file location))
215         ; case maybe_parsed of {
216              Nothing -> return (HscFail pcs_ch);
217              Just rdr_module -> do {
218         ; let this_mod = mkHomeModule (hsModuleName rdr_module)
219     
220             -------------------
221             -- RENAME
222             -------------------
223         ; (pcs_rn, print_unqualified, maybe_rn_result) 
224              <- _scc_ "Rename" 
225                  renameModule dflags hit hst pcs_ch this_mod rdr_module
226         ; case maybe_rn_result of {
227              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
228              Just (is_exported, new_iface, rn_hs_decls) -> do {
229     
230             -- In interactive mode, we don't want to discard any top-level entities at
231             -- all (eg. do not inline them away during simplification), and retain them
232             -- all in the TypeEnv so they are available from the command line.
233             --
234             -- isGlobalName separates the user-defined top-level names from those
235             -- introduced by the type checker.
236         ; let dont_discard | ghci_mode == Interactive = isGlobalName
237                            | otherwise = is_exported
238
239             -------------------
240             -- TYPECHECK
241             -------------------
242         ; maybe_tc_result 
243             <- _scc_ "TypeCheck" typecheckModule dflags pcs_rn hst new_iface 
244                                              print_unqualified rn_hs_decls 
245         ; case maybe_tc_result of {
246              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
247              Just (pcs_tc, tc_result) -> do {
248     
249             -------------------
250             -- DESUGAR
251             -------------------
252         ; (ds_details, foreign_stuff) 
253              <- _scc_ "DeSugar" 
254                 deSugar dflags pcs_tc hst this_mod print_unqualified tc_result
255
256         ; pcs_middle
257             <- if ghci_mode == OneShot 
258                   then do init_pcs <- initPersistentCompilerState
259                           init_prs <- initPersistentRenamerState
260                           let 
261                               rules   = pcs_rules pcs_tc        
262                               orig_tc = prsOrig (pcs_PRS pcs_tc)
263                               new_prs = init_prs{ prsOrig=orig_tc }
264
265                           orig_tc `seq` rules `seq` new_prs `seq`
266                             return init_pcs{ pcs_PRS = new_prs,
267                                              pcs_rules = rules }
268                   else return pcs_tc
269
270             -------------------
271             -- SIMPLIFY
272             -------------------
273         ; simpl_details
274              <- _scc_     "Core2Core"
275                 core2core dflags pcs_middle hst dont_discard ds_details
276
277             -------------------
278             -- TIDY
279             -------------------
280         ; cg_info_ref <- newIORef Nothing ;
281         ; let cg_info :: CgInfoEnv
282               cg_info = unsafePerformIO $ do {
283                            maybe_cg_env <- readIORef cg_info_ref ;
284                            case maybe_cg_env of
285                              Just env -> return env
286                              Nothing  -> do { printError "Urk! Looked at CgInfo too early!";
287                                               return emptyNameEnv } }
288                 -- cg_info_ref will be filled in just after restOfCodeGeneration
289                 -- Meanwhile, tidyCorePgm is careful not to look at cg_info!
290
291         ; (pcs_simpl, tidy_details) 
292              <- _scc_ "CoreTidy"
293                 tidyCorePgm dflags this_mod pcs_middle cg_info simpl_details
294       
295         ; pcs_final <- if ghci_mode == OneShot then initPersistentCompilerState
296                                                else return pcs_simpl
297
298         -- alive at this point:  
299         --      tidy_details
300         --      new_iface               
301
302         ; emitExternalCore dflags new_iface tidy_details 
303             -------------------
304             -- PREPARE FOR CODE GENERATION
305             -------------------
306               -- Do saturation and convert to A-normal form
307         ; prepd_details <- _scc_ "CorePrep" corePrepPgm dflags tidy_details
308
309             -------------------
310             -- CONVERT TO STG and COMPLETE CODE GENERATION
311             -------------------
312         ; let
313             ModDetails{md_binds=binds, md_types=env_tc} = prepd_details
314
315             local_tycons     = typeEnvTyCons  env_tc
316             local_classes    = typeEnvClasses env_tc
317
318             imported_module_names = map ideclName (hsModuleImports rdr_module)
319
320             mod_name_to_Module nm
321                  = do m <- findModule nm ; return (fst (fromJust m))
322
323             (h_code,c_code,fe_binders) = foreign_stuff
324
325         ; imported_modules <- mapM mod_name_to_Module imported_module_names
326
327         ; (stub_h_exists, stub_c_exists, maybe_bcos, final_iface )
328            <- if toInterp
329 #ifdef GHCI
330                 then do 
331                     -----------------  Generate byte code ------------------
332                     (bcos,itbl_env) <- byteCodeGen dflags binds 
333                                         local_tycons local_classes
334
335                     -- Fill in the code-gen info
336                     writeIORef cg_info_ref (Just emptyNameEnv)
337
338                     ------------------ BUILD THE NEW ModIface ------------
339                     final_iface <- _scc_ "MkFinalIface" 
340                           mkFinalIface ghci_mode dflags location 
341                                    maybe_checked_iface new_iface tidy_details
342
343                     return ( False, False, Just (bcos,itbl_env), final_iface )
344 #else
345                 then error "GHC not compiled with interpreter"
346 #endif
347
348                 else do
349                     -----------------  Convert to STG ------------------
350                     (stg_binds, cost_centre_info, stg_back_end_info) 
351                               <- _scc_ "CoreToStg"
352                                   myCoreToStg dflags this_mod binds
353                     
354                     -- Fill in the code-gen info for the earlier tidyCorePgm
355                     writeIORef cg_info_ref (Just stg_back_end_info)
356
357                     ------------------ BUILD THE NEW ModIface ------------
358                     final_iface <- _scc_ "MkFinalIface" 
359                           mkFinalIface ghci_mode dflags location 
360                                    maybe_checked_iface new_iface tidy_details
361
362                     ------------------  Code generation ------------------
363                     abstractC <- _scc_ "CodeGen"
364                                   codeGen dflags this_mod imported_modules
365                                          cost_centre_info fe_binders
366                                          local_tycons stg_binds
367                     
368                     ------------------  Code output -----------------------
369                     (stub_h_exists, stub_c_exists)
370                        <- codeOutput dflags this_mod local_tycons
371                              binds stg_binds
372                              c_code h_code abstractC
373                         
374                     return (stub_h_exists, stub_c_exists, Nothing, final_iface)
375
376         ; let final_details = tidy_details {md_binds = []} 
377
378
379           -- and the answer is ...
380         ; return (HscRecomp pcs_final
381                             final_details
382                             final_iface
383                             stub_h_exists stub_c_exists
384                             maybe_bcos)
385           }}}}}}}
386
387 myParseModule dflags src_filename
388  = do --------------------------  Parser  ----------------
389       showPass dflags "Parser"
390       _scc_  "Parser" do
391
392       buf <- hGetStringBuffer True{-expand tabs-} src_filename
393
394       let glaexts | dopt Opt_GlasgowExts dflags = 1#
395                   | otherwise                   = 0#
396
397       case parseModule buf PState{ bol = 0#, atbol = 1#,
398                                    context = [], glasgow_exts = glaexts,
399                                    loc = mkSrcLoc (_PK_ src_filename) 1 } of {
400
401         PFailed err -> do { hPutStrLn stderr (showSDoc err);
402                             freeStringBuffer buf;
403                             return Nothing };
404
405         POk _ rdr_module@(HsModule mod_name _ _ _ _ _ _) -> do {
406
407       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
408       
409       dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
410                            (ppSourceStats False rdr_module) ;
411       
412       return (Just rdr_module)
413         -- ToDo: free the string buffer later.
414       }}
415
416
417 myCoreToStg dflags this_mod tidy_binds
418  = do 
419       () <- coreBindsSize tidy_binds `seq` return ()
420       -- TEMP: the above call zaps some space usage allocated by the
421       -- simplifier, which for reasons I don't understand, persists
422       -- thoroughout code generation
423
424       stg_binds <- _scc_ "Core2Stg" coreToStg dflags tidy_binds
425
426       (stg_binds2, cost_centre_info)
427            <- _scc_ "Core2Stg" stg2stg dflags this_mod stg_binds
428
429       let env_rhs :: CgInfoEnv
430           env_rhs = mkNameEnv [ (idName bndr, CgInfo (stgRhsArity rhs) caf_info)
431                               | (bind,_) <- stg_binds2, 
432                                 let caf_info 
433                                      | stgBindHasCafRefs bind = MayHaveCafRefs
434                                      | otherwise = NoCafRefs,
435                                 (bndr,rhs) <- stgBindPairs bind ]
436
437       return (stg_binds2, cost_centre_info, env_rhs)
438    where
439       stgBindPairs (StgNonRec _ b r) = [(b,r)]
440       stgBindPairs (StgRec    _ prs) = prs
441
442
443 \end{code}
444
445
446 %************************************************************************
447 %*                                                                      *
448 \subsection{Compiling a do-statement}
449 %*                                                                      *
450 %************************************************************************
451
452 \begin{code}
453 #ifdef GHCI
454 hscStmt
455   :: DynFlags
456   -> HomeSymbolTable    
457   -> HomeIfaceTable
458   -> PersistentCompilerState    -- IN: persistent compiler state
459   -> InteractiveContext         -- Context for compiling
460   -> String                     -- The statement
461   -> Bool                       -- just treat it as an expression
462   -> IO ( PersistentCompilerState, 
463           Maybe ( [Id], 
464                   Type, 
465                   UnlinkedBCOExpr) )
466 \end{code}
467
468 When the UnlinkedBCOExpr is linked you get an HValue of type
469         IO [HValue]
470 When you run it you get a list of HValues that should be 
471 the same length as the list of names; add them to the ClosureEnv.
472
473 A naked expression returns a singleton Name [it].
474
475         What you type                   The IO [HValue] that hscStmt returns
476         -------------                   ------------------------------------
477         let pat = expr          ==>     let pat = expr in return [coerce HVal x, coerce HVal y, ...]
478                                         bindings: [x,y,...]
479
480         pat <- expr             ==>     expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
481                                         bindings: [x,y,...]
482
483         expr (of IO type)       ==>     expr >>= \ v -> return [v]
484           [NB: result not printed]      bindings: [it]
485           
486
487         expr (of non-IO type, 
488           result showable)      ==>     let v = expr in print v >> return [v]
489                                         bindings: [it]
490
491         expr (of non-IO type, 
492           result not showable)  ==>     error
493
494 \begin{code}
495 hscStmt dflags hst hit pcs0 icontext stmt just_expr
496    = let 
497         InteractiveContext { 
498              ic_rn_env   = rn_env, 
499              ic_type_env = type_env,
500              ic_module   = scope_mod } = icontext
501      in
502      do { maybe_stmt <- hscParseStmt dflags stmt
503         ; case maybe_stmt of
504              Nothing -> return (pcs0, Nothing)
505              Just parsed_stmt -> do {
506
507            let { notExprStmt (ExprStmt _ _ _) = False;
508                  notExprStmt _                = True 
509                };
510
511            if (just_expr && notExprStmt parsed_stmt)
512                 then do hPutStrLn stderr ("not an expression: `" ++ stmt ++ "'")
513                         return (pcs0, Nothing)
514                 else do {
515
516                 -- Rename it
517           (pcs1, print_unqual, maybe_renamed_stmt)
518                  <- renameStmt dflags hit hst pcs0 scope_mod 
519                                 iNTERACTIVE rn_env parsed_stmt
520
521         ; case maybe_renamed_stmt of
522                 Nothing -> return (pcs0, Nothing)
523                 Just (bound_names, rn_stmt) -> do {
524
525                 -- Typecheck it
526           maybe_tc_return <- 
527             if just_expr 
528                 then case rn_stmt of { (ExprStmt e _ _, decls) -> 
529                      typecheckExpr dflags pcs1 hst type_env
530                            print_unqual iNTERACTIVE (e,decls) }
531                 else typecheckStmt dflags pcs1 hst type_env
532                            print_unqual iNTERACTIVE bound_names rn_stmt
533
534         ; case maybe_tc_return of
535                 Nothing -> return (pcs0, Nothing)
536                 Just (pcs2, tc_expr, bound_ids, ty) ->  do {
537
538                 -- Desugar it
539           ds_expr <- deSugarExpr dflags pcs2 hst iNTERACTIVE print_unqual tc_expr
540         
541                 -- Simplify it
542         ; simpl_expr <- simplifyExpr dflags pcs2 hst ds_expr
543
544                 -- Tidy it (temporary, until coreSat does cloning)
545         ; tidy_expr <- tidyCoreExpr simpl_expr
546
547                 -- Prepare for codegen
548         ; prepd_expr <- corePrepExpr dflags tidy_expr
549
550                 -- Convert to BCOs
551         ; bcos <- coreExprToBCOs dflags prepd_expr
552
553         ; let
554                 -- Make all the bound ids "global" ids, now that
555                 -- they're notionally top-level bindings.  This is
556                 -- important: otherwise when we come to compile an expression
557                 -- using these ids later, the byte code generator will consider
558                 -- the occurrences to be free rather than global.
559              global_bound_ids = map globaliseId bound_ids;
560              globaliseId id   = setGlobalIdDetails id VanillaGlobal
561
562         ; return (pcs2, Just (global_bound_ids, ty, bcos))
563
564      }}}}}
565
566 hscParseStmt :: DynFlags -> String -> IO (Maybe RdrNameStmt)
567 hscParseStmt dflags str
568  = do --------------------------  Parser  ----------------
569       showPass dflags "Parser"
570       _scc_ "Parser" do
571
572       buf <- stringToStringBuffer str
573
574       let glaexts | dopt Opt_GlasgowExts dflags = 1#
575                   | otherwise                   = 0#
576
577       case parseStmt buf PState{ bol = 0#, atbol = 1#,
578                                  context = [], glasgow_exts = glaexts,
579                                  loc = mkSrcLoc SLIT("<no file>") 0 } of {
580
581         PFailed err -> do { hPutStrLn stderr (showSDoc err);
582 --      Not yet implemented in <4.11    freeStringBuffer buf;
583                             return Nothing };
584
585         -- no stmt: the line consisted of just space or comments
586         POk _ Nothing -> return Nothing;
587
588         POk _ (Just rdr_stmt) -> do {
589
590       --ToDo: can't free the string buffer until we've finished this
591       -- compilation sweep and all the identifiers have gone away.
592       --freeStringBuffer buf;
593       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_stmt);
594       return (Just rdr_stmt)
595       }}
596 #endif
597 \end{code}
598
599 %************************************************************************
600 %*                                                                      *
601 \subsection{Getting information about an identifer}
602 %*                                                                      *
603 %************************************************************************
604
605 \begin{code}
606 #ifdef GHCI
607 hscThing -- like hscStmt, but deals with a single identifier
608   :: DynFlags
609   -> HomeSymbolTable
610   -> HomeIfaceTable
611   -> PersistentCompilerState    -- IN: persistent compiler state
612   -> InteractiveContext         -- Context for compiling
613   -> String                     -- The identifier
614   -> IO ( PersistentCompilerState,
615           [TyThing] )
616
617 hscThing dflags hst hit pcs0 icontext str
618    = do let 
619           InteractiveContext {
620              ic_rn_env   = rn_env,
621              ic_type_env = type_env,
622              ic_module   = scope_mod } = icontext
623
624           rdr_names
625              | '.' `elem` str 
626                 = [ mkQual ns (fmod,fvar) | ns <- namespaces var ]
627              | otherwise
628                 = [ mkUnqual ns fstr | ns <- namespaces str ]
629              where (mod,var) = split_longest_prefix str '.'
630                    fmod = mkFastString mod
631                    fvar = mkFastString var
632                    fstr = mkFastString str
633                    namespaces s | isLower (head s) = [ varName ]
634                                 | otherwise        = [ tcClsName, dataName ]
635
636         (pcs, unqual, maybe_rn_result) <- 
637            renameRdrName dflags hit hst pcs0 scope_mod scope_mod 
638                 rn_env rdr_names
639
640         case maybe_rn_result of {
641              Nothing -> return (pcs, []);
642              Just (names, decls) -> do {
643
644         maybe_pcs <- typecheckExtraDecls dflags pcs hst unqual
645                         iNTERACTIVE decls;
646
647         case maybe_pcs of {
648              Nothing -> return (pcs, []);
649              Just pcs ->
650                 let maybe_ty_things = map (lookupType hst (pcs_PTE pcs)) names
651                 in
652                 return (pcs, catMaybes maybe_ty_things) }
653         }}
654 #endif
655 \end{code}
656
657 %************************************************************************
658 %*                                                                      *
659 \subsection{Initial persistent state}
660 %*                                                                      *
661 %************************************************************************
662
663 \begin{code}
664 initPersistentCompilerState :: IO PersistentCompilerState
665 initPersistentCompilerState 
666   = do prs <- initPersistentRenamerState
667        return (
668         PCS { pcs_PIT   = emptyIfaceTable,
669               pcs_PTE   = wiredInThingEnv,
670               pcs_insts = emptyInstEnv,
671               pcs_rules = emptyRuleBase,
672               pcs_PRS   = prs
673             }
674         )
675
676 initPersistentRenamerState :: IO PersistentRenamerState
677   = do us <- mkSplitUniqSupply 'r'
678        return (
679         PRS { prsOrig  = NameSupply { nsUniqs = us,
680                                       nsNames = initOrigNames,
681                                       nsIPs   = emptyFM },
682               prsDecls   = (emptyNameEnv, 0),
683               prsInsts   = (emptyBag, 0),
684               prsRules   = (emptyBag, 0),
685               prsImpMods = emptyFM
686             }
687         )
688
689 initOrigNames :: FiniteMap (ModuleName,OccName) Name
690 initOrigNames 
691    = grab knownKeyNames `plusFM` grab (map getName wiredInThings)
692      where
693         grab names = foldl add emptyFM names
694         add env name 
695            = addToFM env (moduleName (nameModule name), nameOccName name) name
696 \end{code}