[project @ 2001-08-17 17:18:51 by apt]
[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             ( isUpper )
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         ; let toNothing = dopt_HscLang dflags == HscNothing
206
207         ; when (verbosity dflags >= 1) $
208                 hPutStrLn stderr ("Compiling " ++ 
209                         compMsg (not toInterp) mod location);
210
211             -------------------
212             -- PARSE
213             -------------------
214         ; maybe_parsed <- myParseModule dflags 
215                              (unJust "hscRecomp:hspp" (ml_hspp_file location))
216         ; case maybe_parsed of {
217              Nothing -> return (HscFail pcs_ch);
218              Just rdr_module -> do {
219         ; let this_mod = mkHomeModule (hsModuleName rdr_module)
220     
221             -------------------
222             -- RENAME
223             -------------------
224         ; (pcs_rn, print_unqualified, maybe_rn_result) 
225              <- _scc_ "Rename" 
226                  renameModule dflags hit hst pcs_ch this_mod rdr_module
227         ; case maybe_rn_result of {
228              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
229              Just (is_exported, new_iface, rn_hs_decls) -> do {
230     
231             -- In interactive mode, we don't want to discard any top-level entities at
232             -- all (eg. do not inline them away during simplification), and retain them
233             -- all in the TypeEnv so they are available from the command line.
234             --
235             -- isGlobalName separates the user-defined top-level names from those
236             -- introduced by the type checker.
237         ; let dont_discard | ghci_mode == Interactive = isGlobalName
238                            | otherwise = is_exported
239
240             -------------------
241             -- TYPECHECK
242             -------------------
243         ; maybe_tc_result 
244             <- _scc_ "TypeCheck" typecheckModule dflags pcs_rn hst new_iface 
245                                              print_unqualified rn_hs_decls 
246         ; case maybe_tc_result of {
247              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
248              Just (pcs_tc, tc_result) -> do {
249     
250             -------------------
251             -- DESUGAR
252             -------------------
253         ; (ds_details, foreign_stuff) 
254              <- _scc_ "DeSugar" 
255                 deSugar dflags pcs_tc hst this_mod print_unqualified tc_result
256
257         ; pcs_middle
258             <- if ghci_mode == OneShot 
259                   then do init_pcs <- initPersistentCompilerState
260                           init_prs <- initPersistentRenamerState
261                           let 
262                               rules   = pcs_rules pcs_tc        
263                               orig_tc = prsOrig (pcs_PRS pcs_tc)
264                               new_prs = init_prs{ prsOrig=orig_tc }
265
266                           orig_tc `seq` rules `seq` new_prs `seq`
267                             return init_pcs{ pcs_PRS = new_prs,
268                                              pcs_rules = rules }
269                   else return pcs_tc
270
271             -------------------
272             -- SIMPLIFY
273             -------------------
274         ; simpl_details
275              <- _scc_     "Core2Core"
276                 core2core dflags pcs_middle hst dont_discard ds_details
277
278             -------------------
279             -- TIDY
280             -------------------
281         ; cg_info_ref <- newIORef Nothing ;
282         ; let cg_info :: CgInfoEnv
283               cg_info = unsafePerformIO $ do {
284                            maybe_cg_env <- readIORef cg_info_ref ;
285                            case maybe_cg_env of
286                              Just env -> return env
287                              Nothing  -> do { printError "Urk! Looked at CgInfo too early!";
288                                               return emptyNameEnv } }
289                 -- cg_info_ref will be filled in just after restOfCodeGeneration
290                 -- Meanwhile, tidyCorePgm is careful not to look at cg_info!
291
292         ; (pcs_simpl, tidy_details) 
293              <- _scc_ "CoreTidy"
294                 tidyCorePgm dflags this_mod pcs_middle cg_info simpl_details
295       
296         ; pcs_final <- if ghci_mode == OneShot then initPersistentCompilerState
297                                                else return pcs_simpl
298
299         -- alive at this point:  
300         --      tidy_details
301         --      new_iface               
302
303         ; emitExternalCore dflags new_iface tidy_details 
304             -------------------
305             -- PREPARE FOR CODE GENERATION
306             -------------------
307               -- Do saturation and convert to A-normal form
308         ; prepd_details <- _scc_ "CorePrep" corePrepPgm dflags tidy_details
309
310             -------------------
311             -- CONVERT TO STG and COMPLETE CODE GENERATION
312             -------------------
313         ; let
314             ModDetails{md_binds=binds, md_types=env_tc} = prepd_details
315
316             local_tycons     = typeEnvTyCons  env_tc
317             local_classes    = typeEnvClasses env_tc
318
319             imported_module_names = map ideclName (hsModuleImports rdr_module)
320
321             mod_name_to_Module nm
322                  = do m <- findModule nm ; return (fst (fromJust m))
323
324             (h_code,c_code,fe_binders) = foreign_stuff
325
326         ; imported_modules <- mapM mod_name_to_Module imported_module_names
327
328         ; (stub_h_exists, stub_c_exists, maybe_bcos, final_iface )
329            <- if toInterp
330 #ifdef GHCI
331                 then do 
332                     -----------------  Generate byte code ------------------
333                     (bcos,itbl_env) <- byteCodeGen dflags binds 
334                                         local_tycons local_classes
335
336                     -- Fill in the code-gen info
337                     writeIORef cg_info_ref (Just emptyNameEnv)
338
339                     ------------------ BUILD THE NEW ModIface ------------
340                     final_iface <- _scc_ "MkFinalIface" 
341                           mkFinalIface ghci_mode dflags location 
342                                    maybe_checked_iface new_iface tidy_details
343
344                     return ( False, False, Just (bcos,itbl_env), final_iface )
345 #else
346                 then error "GHC not compiled with interpreter"
347 #endif
348
349                 else do
350                     -----------------  Convert to STG ------------------
351                     (stg_binds, cost_centre_info, stg_back_end_info) 
352                               <- _scc_ "CoreToStg"
353                                   myCoreToStg dflags this_mod binds
354                     
355                     -- Fill in the code-gen info for the earlier tidyCorePgm
356                     writeIORef cg_info_ref (Just stg_back_end_info)
357
358                     ------------------ BUILD THE NEW ModIface ------------
359                     final_iface <- _scc_ "MkFinalIface" 
360                           mkFinalIface ghci_mode dflags location 
361                                    maybe_checked_iface new_iface tidy_details
362
363                     if toNothing 
364                       then do
365                           return (False, False, Nothing, final_iface)
366                       else do
367                           ------------------  Code generation ------------------
368                           abstractC <- _scc_ "CodeGen"
369                                         codeGen dflags this_mod imported_modules
370                                                cost_centre_info fe_binders
371                                                local_tycons stg_binds
372                           
373                           ------------------  Code output -----------------------
374                           (stub_h_exists, stub_c_exists)
375                              <- codeOutput dflags this_mod local_tycons
376                                    binds stg_binds
377                                    c_code h_code abstractC
378                               
379                           return (stub_h_exists, stub_c_exists, Nothing, final_iface)
380
381         ; let final_details = tidy_details {md_binds = []} 
382
383
384           -- and the answer is ...
385         ; return (HscRecomp pcs_final
386                             final_details
387                             final_iface
388                             stub_h_exists stub_c_exists
389                             maybe_bcos)
390           }}}}}}}
391
392 myParseModule dflags src_filename
393  = do --------------------------  Parser  ----------------
394       showPass dflags "Parser"
395       _scc_  "Parser" do
396
397       buf <- hGetStringBuffer True{-expand tabs-} src_filename
398
399       let glaexts | dopt Opt_GlasgowExts dflags = 1#
400                   | otherwise                   = 0#
401
402       case parseModule buf PState{ bol = 0#, atbol = 1#,
403                                    context = [], glasgow_exts = glaexts,
404                                    loc = mkSrcLoc (_PK_ src_filename) 1 } of {
405
406         PFailed err -> do { hPutStrLn stderr (showSDoc err);
407                             freeStringBuffer buf;
408                             return Nothing };
409
410         POk _ rdr_module@(HsModule mod_name _ _ _ _ _ _) -> do {
411
412       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
413       
414       dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
415                            (ppSourceStats False rdr_module) ;
416       
417       return (Just rdr_module)
418         -- ToDo: free the string buffer later.
419       }}
420
421
422 myCoreToStg dflags this_mod tidy_binds
423  = do 
424       () <- coreBindsSize tidy_binds `seq` return ()
425       -- TEMP: the above call zaps some space usage allocated by the
426       -- simplifier, which for reasons I don't understand, persists
427       -- thoroughout code generation
428
429       stg_binds <- _scc_ "Core2Stg" coreToStg dflags tidy_binds
430
431       (stg_binds2, cost_centre_info)
432            <- _scc_ "Core2Stg" stg2stg dflags this_mod stg_binds
433
434       let env_rhs :: CgInfoEnv
435           env_rhs = mkNameEnv [ (idName bndr, CgInfo (stgRhsArity rhs) caf_info)
436                               | (bind,_) <- stg_binds2, 
437                                 let caf_info 
438                                      | stgBindHasCafRefs bind = MayHaveCafRefs
439                                      | otherwise = NoCafRefs,
440                                 (bndr,rhs) <- stgBindPairs bind ]
441
442       return (stg_binds2, cost_centre_info, env_rhs)
443    where
444       stgBindPairs (StgNonRec _ b r) = [(b,r)]
445       stgBindPairs (StgRec    _ prs) = prs
446
447
448 \end{code}
449
450
451 %************************************************************************
452 %*                                                                      *
453 \subsection{Compiling a do-statement}
454 %*                                                                      *
455 %************************************************************************
456
457 \begin{code}
458 #ifdef GHCI
459 hscStmt
460   :: DynFlags
461   -> HomeSymbolTable    
462   -> HomeIfaceTable
463   -> PersistentCompilerState    -- IN: persistent compiler state
464   -> InteractiveContext         -- Context for compiling
465   -> String                     -- The statement
466   -> Bool                       -- just treat it as an expression
467   -> IO ( PersistentCompilerState, 
468           Maybe ( [Id], 
469                   Type, 
470                   UnlinkedBCOExpr) )
471 \end{code}
472
473 When the UnlinkedBCOExpr is linked you get an HValue of type
474         IO [HValue]
475 When you run it you get a list of HValues that should be 
476 the same length as the list of names; add them to the ClosureEnv.
477
478 A naked expression returns a singleton Name [it].
479
480         What you type                   The IO [HValue] that hscStmt returns
481         -------------                   ------------------------------------
482         let pat = expr          ==>     let pat = expr in return [coerce HVal x, coerce HVal y, ...]
483                                         bindings: [x,y,...]
484
485         pat <- expr             ==>     expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
486                                         bindings: [x,y,...]
487
488         expr (of IO type)       ==>     expr >>= \ v -> return [v]
489           [NB: result not printed]      bindings: [it]
490           
491
492         expr (of non-IO type, 
493           result showable)      ==>     let v = expr in print v >> return [v]
494                                         bindings: [it]
495
496         expr (of non-IO type, 
497           result not showable)  ==>     error
498
499 \begin{code}
500 hscStmt dflags hst hit pcs0 icontext stmt just_expr
501    = let 
502         InteractiveContext { 
503              ic_rn_env   = rn_env, 
504              ic_type_env = type_env,
505              ic_module   = scope_mod } = icontext
506      in
507      do { maybe_stmt <- hscParseStmt dflags stmt
508         ; case maybe_stmt of
509              Nothing -> return (pcs0, Nothing)
510              Just parsed_stmt -> do {
511
512            let { notExprStmt (ExprStmt _ _ _) = False;
513                  notExprStmt _                = True 
514                };
515
516            if (just_expr && notExprStmt parsed_stmt)
517                 then do hPutStrLn stderr ("not an expression: `" ++ stmt ++ "'")
518                         return (pcs0, Nothing)
519                 else do {
520
521                 -- Rename it
522           (pcs1, print_unqual, maybe_renamed_stmt)
523                  <- renameStmt dflags hit hst pcs0 scope_mod 
524                                 iNTERACTIVE rn_env parsed_stmt
525
526         ; case maybe_renamed_stmt of
527                 Nothing -> return (pcs0, Nothing)
528                 Just (bound_names, rn_stmt) -> do {
529
530                 -- Typecheck it
531           maybe_tc_return <- 
532             if just_expr 
533                 then case rn_stmt of { (ExprStmt e _ _, decls) -> 
534                      typecheckExpr dflags pcs1 hst type_env
535                            print_unqual iNTERACTIVE (e,decls) }
536                 else typecheckStmt dflags pcs1 hst type_env
537                            print_unqual iNTERACTIVE bound_names rn_stmt
538
539         ; case maybe_tc_return of
540                 Nothing -> return (pcs0, Nothing)
541                 Just (pcs2, tc_expr, bound_ids, ty) ->  do {
542
543                 -- Desugar it
544           ds_expr <- deSugarExpr dflags pcs2 hst iNTERACTIVE print_unqual tc_expr
545         
546                 -- Simplify it
547         ; simpl_expr <- simplifyExpr dflags pcs2 hst ds_expr
548
549                 -- Tidy it (temporary, until coreSat does cloning)
550         ; tidy_expr <- tidyCoreExpr simpl_expr
551
552                 -- Prepare for codegen
553         ; prepd_expr <- corePrepExpr dflags tidy_expr
554
555                 -- Convert to BCOs
556         ; bcos <- coreExprToBCOs dflags prepd_expr
557
558         ; let
559                 -- Make all the bound ids "global" ids, now that
560                 -- they're notionally top-level bindings.  This is
561                 -- important: otherwise when we come to compile an expression
562                 -- using these ids later, the byte code generator will consider
563                 -- the occurrences to be free rather than global.
564              global_bound_ids = map globaliseId bound_ids;
565              globaliseId id   = setGlobalIdDetails id VanillaGlobal
566
567         ; return (pcs2, Just (global_bound_ids, ty, bcos))
568
569      }}}}}
570
571 hscParseStmt :: DynFlags -> String -> IO (Maybe RdrNameStmt)
572 hscParseStmt dflags str
573  = do --------------------------  Parser  ----------------
574       showPass dflags "Parser"
575       _scc_ "Parser" do
576
577       buf <- stringToStringBuffer str
578
579       let glaexts | dopt Opt_GlasgowExts dflags = 1#
580                   | otherwise                   = 0#
581
582       case parseStmt buf PState{ bol = 0#, atbol = 1#,
583                                  context = [], glasgow_exts = glaexts,
584                                  loc = mkSrcLoc SLIT("<interactive>") 1 } of {
585
586         PFailed err -> do { hPutStrLn stderr (showSDoc err);
587 --      Not yet implemented in <4.11    freeStringBuffer buf;
588                             return Nothing };
589
590         -- no stmt: the line consisted of just space or comments
591         POk _ Nothing -> return Nothing;
592
593         POk _ (Just rdr_stmt) -> do {
594
595       --ToDo: can't free the string buffer until we've finished this
596       -- compilation sweep and all the identifiers have gone away.
597       --freeStringBuffer buf;
598       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_stmt);
599       return (Just rdr_stmt)
600       }}
601 #endif
602 \end{code}
603
604 %************************************************************************
605 %*                                                                      *
606 \subsection{Getting information about an identifer}
607 %*                                                                      *
608 %************************************************************************
609
610 \begin{code}
611 #ifdef GHCI
612 hscThing -- like hscStmt, but deals with a single identifier
613   :: DynFlags
614   -> HomeSymbolTable
615   -> HomeIfaceTable
616   -> PersistentCompilerState    -- IN: persistent compiler state
617   -> InteractiveContext         -- Context for compiling
618   -> String                     -- The identifier
619   -> IO ( PersistentCompilerState,
620           [TyThing] )
621
622 hscThing dflags hst hit pcs0 icontext str
623    = do let 
624           InteractiveContext {
625              ic_rn_env   = rn_env,
626              ic_type_env = type_env,
627              ic_module   = scope_mod } = icontext
628
629           rdr_names
630              | '.' `elem` str 
631                 = [ mkQual ns (fmod,fvar) | ns <- namespaces var ]
632              | otherwise
633                 = [ mkUnqual ns fstr | ns <- namespaces str ]
634              where (mod,var) = split_longest_prefix str '.'
635                    fmod = mkFastString mod
636                    fvar = mkFastString var
637                    fstr = mkFastString str
638                    namespaces s 
639                         | isUpper c || c == ':' = [ tcClsName, dataName ]
640                         | otherwise             = [ varName ]
641                         where c = head s
642
643         (pcs, unqual, maybe_rn_result) <- 
644            renameRdrName dflags hit hst pcs0 scope_mod scope_mod 
645                 rn_env rdr_names
646
647         case maybe_rn_result of {
648              Nothing -> return (pcs, []);
649              Just (names, decls) -> do {
650
651         maybe_pcs <- typecheckExtraDecls dflags pcs hst unqual
652                         iNTERACTIVE decls;
653
654         case maybe_pcs of {
655              Nothing -> return (pcs, []);
656              Just pcs ->
657                 let maybe_ty_things = map (lookupType hst (pcs_PTE pcs)) names
658                 in
659                 return (pcs, catMaybes maybe_ty_things) }
660         }}
661 #endif
662 \end{code}
663
664 %************************************************************************
665 %*                                                                      *
666 \subsection{Initial persistent state}
667 %*                                                                      *
668 %************************************************************************
669
670 \begin{code}
671 initPersistentCompilerState :: IO PersistentCompilerState
672 initPersistentCompilerState 
673   = do prs <- initPersistentRenamerState
674        return (
675         PCS { pcs_PIT   = emptyIfaceTable,
676               pcs_PTE   = wiredInThingEnv,
677               pcs_insts = emptyInstEnv,
678               pcs_rules = emptyRuleBase,
679               pcs_PRS   = prs
680             }
681         )
682
683 initPersistentRenamerState :: IO PersistentRenamerState
684   = do us <- mkSplitUniqSupply 'r'
685        return (
686         PRS { prsOrig  = NameSupply { nsUniqs = us,
687                                       nsNames = initOrigNames,
688                                       nsIPs   = emptyFM },
689               prsDecls   = (emptyNameEnv, 0),
690               prsInsts   = (emptyBag, 0),
691               prsRules   = (emptyBag, 0),
692               prsImpMods = emptyFM
693             }
694         )
695
696 initOrigNames :: FiniteMap (ModuleName,OccName) Name
697 initOrigNames 
698    = grab knownKeyNames `plusFM` grab (map getName wiredInThings)
699      where
700         grab names = foldl add emptyFM names
701         add env name 
702            = addToFM env (moduleName (nameModule name), nameOccName name) name
703 \end{code}