[project @ 2000-11-17 13:33:17 by sewardj]
[ghc-hetmet.git] / ghc / compiler / compMan / CompManager.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[CompManager]{The Compilation Manager}
5
6 \begin{code}
7 module CompManager ( cmInit, cmLoadModule, 
8                      cmGetExpr, cmRunExpr,
9                      CmState, emptyCmState,  -- abstract
10                      cmLookupSymbol --tmp
11                    )
12 where
13
14 #include "HsVersions.h"
15
16 import List             ( nub )
17 import Maybe            ( catMaybes, fromMaybe )
18 import Maybes           ( maybeToBool )
19 import Outputable
20 import UniqFM           ( emptyUFM, lookupUFM, addToUFM, delListFromUFM,
21                           UniqFM, listToUFM )
22 import Unique           ( Uniquable )
23 import Digraph          ( SCC(..), stronglyConnComp )
24
25 import CmLink
26 import CmTypes
27 import HscTypes
28 import Interpreter      ( HValue )
29 import Module           ( ModuleName, moduleName,
30                           isModuleInThisPackage, moduleEnvElts,
31                           moduleNameUserString )
32 import CmStaticInfo     ( PackageConfigInfo, GhciMode(..) )
33 import DriverPipeline
34 import GetImports
35 import HscTypes         ( HomeSymbolTable, HomeIfaceTable, 
36                           PersistentCompilerState, ModDetails(..) )
37 import Name             ( lookupNameEnv )
38 import RdrName
39 import Module
40 import PrelNames        ( mainName )
41 import HscMain          ( initPersistentCompilerState )
42 import Finder           ( findModule, emptyHomeDirCache )
43 import DriverUtil       ( BarfKind(..), splitFilename3 )
44 import Util
45 import Panic            ( panic )
46
47 import Exception        ( throwDyn )
48 import IO
49 import Time             ( ClockTime )
50 import Directory        ( getModificationTime, doesFileExist )
51
52 \end{code}
53
54
55
56 \begin{code}
57 cmInit :: PackageConfigInfo -> GhciMode -> IO CmState
58 cmInit raw_package_info gmode
59    = emptyCmState raw_package_info gmode
60
61 cmGetExpr :: CmState
62           -> ModuleName
63           -> String
64           -> IO (CmState, Either [SDoc] HValue)
65 cmGetExpr cmstate modhdl expr
66    = return (panic "cmGetExpr:unimp")
67
68 cmRunExpr :: HValue -> IO ()
69 cmRunExpr hval
70    = return (panic "cmRunExpr:unimp")
71
72
73 -- Persistent state just for CM, excluding link & compile subsystems
74 data PersistentCMState
75    = PersistentCMState {
76         hst   :: HomeSymbolTable,    -- home symbol table
77         hit   :: HomeIfaceTable,     -- home interface table
78         ui    :: UnlinkedImage,      -- the unlinked images
79         mg    :: ModuleGraph,        -- the module graph
80         pci   :: PackageConfigInfo,  -- NEVER CHANGES
81         gmode :: GhciMode            -- NEVER CHANGES
82      }
83
84 emptyPCMS :: PackageConfigInfo -> GhciMode -> PersistentCMState
85 emptyPCMS pci gmode
86   = PersistentCMState { hst = emptyHST, hit = emptyHIT,
87                         ui  = emptyUI,  mg  = emptyMG, 
88                         pci = pci, gmode = gmode }
89
90 emptyHIT :: HomeIfaceTable
91 emptyHIT = emptyUFM
92 emptyHST :: HomeSymbolTable
93 emptyHST = emptyUFM
94
95
96
97 -- Persistent state for the entire system
98 data CmState
99    = CmState {
100         pcms   :: PersistentCMState,       -- CM's persistent state
101         pcs    :: PersistentCompilerState, -- compile's persistent state
102         pls    :: PersistentLinkerState    -- link's persistent state
103      }
104
105 emptyCmState :: PackageConfigInfo -> GhciMode -> IO CmState
106 emptyCmState pci gmode
107     = do let pcms = emptyPCMS pci gmode
108          pcs     <- initPersistentCompilerState
109          pls     <- emptyPLS
110          return (CmState { pcms   = pcms,
111                            pcs    = pcs,
112                            pls    = pls })
113
114 -- CM internal types
115 type UnlinkedImage = [Linkable] -- the unlinked images (should be a set, really)
116 emptyUI :: UnlinkedImage
117 emptyUI = []
118
119 type ModuleGraph = [ModSummary]  -- the module graph, topologically sorted
120 emptyMG :: ModuleGraph
121 emptyMG = []
122
123 \end{code}
124
125 The real business of the compilation manager: given a system state and
126 a module name, try and bring the module up to date, probably changing
127 the system state at the same time.
128
129 \begin{code}
130 cmLoadModule :: CmState 
131              -> ModuleName
132              -> IO (CmState, Maybe ModuleName)
133
134 cmLoadModule cmstate1 rootname
135    = do -- version 1's are the original, before downsweep
136         let pcms1     = pcms   cmstate1
137         let pls1      = pls    cmstate1
138         let pcs1      = pcs    cmstate1
139         let mg1       = mg     pcms1
140         let hst1      = hst    pcms1
141         let hit1      = hit    pcms1
142         let ui1       = ui     pcms1
143    
144         let pcii      = pci   pcms1 -- this never changes
145         let ghci_mode = gmode pcms1 -- ToDo: fix!
146
147         -- During upsweep, look at new summaries to see if source has
148         -- changed.  Here's a function to pass down; it takes a new
149         -- summary.
150         let source_changed :: ModSummary -> Bool
151             source_changed = summary_indicates_source_changed mg1
152
153         -- Do the downsweep to reestablish the module graph
154         -- then generate version 2's by removing from HIT,HST,UI any
155         -- modules in the old MG which are not in the new one.
156
157         -- Throw away the old home dir cache
158         emptyHomeDirCache
159
160         hPutStr stderr "cmLoadModule: downsweep begins\n"
161         mg2unsorted <- downsweep [rootname]
162
163         let modnames1   = map name_of_summary mg1
164         let modnames2   = map name_of_summary mg2unsorted
165         let mods_to_zap = filter (`notElem` modnames2) modnames1
166
167         let (hst2, hit2, ui2)
168                = removeFromTopLevelEnvs mods_to_zap (hst1, hit1, ui1)
169         -- should be cycle free; ignores 'import source's
170         let mg2 = topological_sort False mg2unsorted
171         -- ... whereas this takes them into account.  Used for
172         -- backing out partially complete cycles following a failed
173         -- upsweep, and for removing from hst/hit all the modules
174         -- not in strict downwards closure, during calls to compile.
175         let mg2_with_srcimps = topological_sort True mg2unsorted
176       
177         let reachable_from :: ModuleName -> [ModuleName]
178             reachable_from = downwards_closure_of_module mg2unsorted
179
180         hPutStrLn stderr "after tsort:\n"
181         hPutStrLn stderr (showSDoc (vcat (map ppr mg2)))
182
183         -- Because we don't take into account source imports when doing
184         -- the topological sort, there shouldn't be any cycles in mg2.
185         -- If there is, we complain and give up -- the user needs to
186         -- break the cycle using a boot file.
187
188         -- Now do the upsweep, calling compile for each module in
189         -- turn.  Final result is version 3 of everything.
190
191         let threaded2 = CmThreaded pcs1 hst2 hit2
192
193         (upsweep_complete_success, threaded3, modsDone, newLis)
194            <- upsweep_mods ghci_mode ui2 reachable_from source_changed threaded2 mg2
195
196         let ui3 = add_to_ui ui2 newLis
197         let (CmThreaded pcs3 hst3 hit3) = threaded3
198
199         -- At this point, modsDone and newLis should have the same
200         -- length, so there is one new (or old) linkable for each 
201         -- mod which was processed (passed to compile).
202
203         -- Try and do linking in some form, depending on whether the
204         -- upsweep was completely or only partially successful.
205
206         if upsweep_complete_success
207
208          then 
209            -- Easy; just relink it all.
210            do hPutStrLn stderr "UPSWEEP COMPLETELY SUCCESSFUL"
211               linkresult 
212                  <- link ghci_mode (any exports_main (moduleEnvElts hst3)) 
213                          newLis pls1
214               case linkresult of
215                  LinkErrs _ _
216                     -> panic "cmLoadModule: link failed (1)"
217                  LinkOK pls3 
218                     -> do let pcms3 = PersistentCMState { hst=hst3, hit=hit3, 
219                                                           ui=ui3, mg=modsDone, 
220                                                           pci=pcii, gmode=ghci_mode }
221                           let cmstate3 
222                                  = CmState { pcms=pcms3, pcs=pcs3, pls=pls3 }
223                           return (cmstate3, Just rootname)
224
225          else 
226            -- Tricky.  We need to back out the effects of compiling any
227            -- half-done cycles, both so as to clean up the top level envs
228            -- and to avoid telling the interactive linker to link them.
229            do hPutStrLn stderr "UPSWEEP PARTIALLY SUCCESSFUL"
230
231               let modsDone_names
232                      = map name_of_summary modsDone
233               let mods_to_zap_names 
234                      = findPartiallyCompletedCycles modsDone_names mg2_with_srcimps
235               let (hst4, hit4, ui4) 
236                      = removeFromTopLevelEnvs mods_to_zap_names (hst3,hit3,ui3)
237               let mods_to_keep
238                      = filter ((`notElem` mods_to_zap_names).name_of_summary) modsDone
239               let mods_to_keep_names 
240                      = map name_of_summary mods_to_keep
241               -- we could get the relevant linkables by filtering newLis, but
242               -- it seems easier to drag them out of the updated, cleaned-up UI
243               let linkables_to_link 
244                      = map (findModuleLinkable ui4) mods_to_keep_names
245
246               linkresult <- link ghci_mode False linkables_to_link pls1
247               case linkresult of
248                  LinkErrs _ _
249                     -> panic "cmLoadModule: link failed (2)"
250                  LinkOK pls4
251                     -> do let pcms4 = PersistentCMState { hst=hst4, hit=hit4, 
252                                                           ui=ui4, mg=mods_to_keep,
253                                                           pci=pcii, gmode=ghci_mode }
254                           let cmstate4 
255                                  = CmState { pcms=pcms4, pcs=pcs3, pls=pls4 }
256                           return (cmstate4, 
257                                   -- choose rather arbitrarily who to return
258                                   if null mods_to_keep then Nothing 
259                                      else Just (last mods_to_keep_names))
260
261
262 -- Given a bunch of old summaries and a new summary, try and
263 -- find the corresponding old summary, and, if found, compare
264 -- its source timestamp with that of the new summary.  If in
265 -- doubt say True.
266 summary_indicates_source_changed :: [ModSummary] -> ModSummary -> Bool
267 summary_indicates_source_changed old_summaries new_summary
268    = case [old | old <- old_summaries, 
269                  name_of_summary old == name_of_summary new_summary] of
270
271         (_:_:_) -> panic "summary_indicates_newer_source"
272                    
273         []      -> -- can't find a corresponding old summary, so
274                    -- compare source and iface dates in the new summary.
275                    trace (showSDoc (text "SISC: no old summary, new =" 
276                                     <+> pprSummaryTimes new_summary)) (
277                    case (ms_hs_date new_summary, ms_hi_date new_summary) of
278                       (Just hs_t, Just hi_t) -> hs_t > hi_t
279                       other                  -> True
280                    )
281
282         [old]   -> -- found old summary; compare source timestamps
283                    trace (showSDoc (text "SISC: old =" 
284                                     <+> pprSummaryTimes old
285                                     <+> pprSummaryTimes new_summary)) (
286                    case (ms_hs_date old, ms_hs_date new_summary) of
287                       (Just old_t, Just new_t) -> new_t > old_t
288                       other                    -> True
289                    )
290
291 -- Return (names of) all those in modsDone who are part of a cycle
292 -- as defined by theGraph.
293 findPartiallyCompletedCycles :: [ModuleName] -> [SCC ModSummary] -> [ModuleName]
294 findPartiallyCompletedCycles modsDone theGraph
295    = chew theGraph
296      where
297         chew [] = []
298         chew ((AcyclicSCC v):rest) = chew rest    -- acyclic?  not interesting.
299         chew ((CyclicSCC vs):rest)
300            = let names_in_this_cycle = nub (map name_of_summary vs)
301                  mods_in_this_cycle  
302                     = nub ([done | done <- modsDone, 
303                                    done `elem` names_in_this_cycle])
304                  chewed_rest = chew rest
305              in 
306              if   not (null mods_in_this_cycle) 
307                   && length mods_in_this_cycle < length names_in_this_cycle
308              then mods_in_this_cycle ++ chewed_rest
309              else chewed_rest
310
311
312 -- Does this ModDetails export Main.main?
313 exports_main :: ModDetails -> Bool
314 exports_main md
315    = maybeToBool (lookupNameEnv (md_types md) mainName)
316
317
318 -- Add the given (LM-form) Linkables to the UI, overwriting previous
319 -- versions if they exist.
320 add_to_ui :: UnlinkedImage -> [Linkable] -> UnlinkedImage
321 add_to_ui ui lis
322    = foldr add1 ui lis
323      where
324         add1 :: Linkable -> UnlinkedImage -> UnlinkedImage
325         add1 li ui
326            = li : filter (\li2 -> not (for_same_module li li2)) ui
327
328         for_same_module :: Linkable -> Linkable -> Bool
329         for_same_module li1 li2 
330            = not (is_package_linkable li1)
331              && not (is_package_linkable li2)
332              && modname_of_linkable li1 == modname_of_linkable li2
333                                   
334
335 data CmThreaded  -- stuff threaded through individual module compilations
336    = CmThreaded PersistentCompilerState HomeSymbolTable HomeIfaceTable
337
338
339 -- Compile multiple modules, stopping as soon as an error appears.
340 -- There better had not be any cyclic groups here -- we check for them.
341 upsweep_mods :: GhciMode
342              -> UnlinkedImage         -- old linkables
343              -> (ModuleName -> [ModuleName])  -- to construct downward closures
344              -> (ModSummary -> Bool)  -- has source changed?
345              -> CmThreaded            -- PCS & HST & HIT
346              -> [SCC ModSummary]      -- mods to do (the worklist)
347                                       -- ...... RETURNING ......
348              -> IO (Bool{-complete success?-},
349                     CmThreaded,
350                     [ModSummary],     -- mods which succeeded
351                     [Linkable])       -- new linkables
352
353 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded []
354    = return (True, threaded, [], [])
355
356 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded ((CyclicSCC ms):_)
357    = do hPutStrLn stderr ("ghc: module imports form a cycle for modules:\n\t" ++
358                           unwords (map (moduleNameUserString.name_of_summary) ms))
359         return (False, threaded, [], [])
360
361 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded ((AcyclicSCC mod):mods)
362    = do (threaded1, maybe_linkable) 
363            <- upsweep_mod ghci_mode oldUI threaded mod 
364                           (reachable_from (name_of_summary mod)) 
365                           (source_changed mod)
366         case maybe_linkable of
367            Just linkable 
368               -> -- No errors; do the rest
369                  do (restOK, threaded2, modOKs, linkables) 
370                        <- upsweep_mods ghci_mode oldUI reachable_from source_changed threaded1 mods
371                     return (restOK, threaded2, mod:modOKs, linkable:linkables)
372            Nothing -- we got a compilation error; give up now
373               -> return (False, threaded1, [], [])
374
375
376 -- Compile a single module.  Always produce a Linkable for it if 
377 -- successful.  If no compilation happened, return the old Linkable.
378 upsweep_mod :: GhciMode 
379             -> UnlinkedImage
380             -> CmThreaded
381             -> ModSummary
382             -> [ModuleName]
383             -> Bool
384             -> IO (CmThreaded, Maybe Linkable)
385
386 upsweep_mod ghci_mode oldUI threaded1 summary1 
387             reachable_from_here source_might_have_changed
388    = do let mod_name = name_of_summary summary1
389         let (CmThreaded pcs1 hst1 hit1) = threaded1
390         let old_iface = lookupUFM hit1 (name_of_summary summary1)
391
392         -- We *have* to compile it if we're in batch mode and we can't see
393         -- a previous linkable for it on disk.
394         compilation_mandatory 
395            <- if ghci_mode /= Batch then return False 
396               else case ml_obj_file (ms_location summary1) of
397                       Nothing     -> do --putStrLn "cmcm: object?!"
398                                         return True
399                       Just obj_fn -> do --putStrLn ("cmcm: old obj " ++ obj_fn)
400                                         b <- doesFileExist obj_fn
401                                         return (not b)
402
403         let compilation_might_be_needed 
404                = source_might_have_changed || compilation_mandatory
405             source_unchanged
406                = not compilation_might_be_needed
407             (hst1_strictDC, hit1_strictDC)
408                = retainInTopLevelEnvs reachable_from_here (hst1,hit1)
409
410         compresult <- compile ghci_mode summary1 source_unchanged
411                          old_iface hst1_strictDC hit1_strictDC pcs1
412
413         --putStrLn ( "UPSWEEP_MOD: smhc = " ++ show source_might_have_changed 
414         --           ++ ",  cman = " ++ show compilation_mandatory)
415
416         case compresult of
417
418            -- Compilation "succeeded", but didn't return a new iface or
419            -- linkable, meaning that compilation wasn't needed, and the
420            -- new details were manufactured from the old iface.
421            CompOK details Nothing pcs2
422               -> let hst2         = addToUFM hst1 mod_name details
423                      hit2         = hit1
424                      threaded2    = CmThreaded pcs2 hst2 hit2
425                      old_linkable 
426                         | ghci_mode == Interactive 
427                         = findModuleLinkable oldUI mod_name
428                         | otherwise
429                         = LM mod_name
430                              [DotO (unJust (ml_obj_file (ms_location summary1)) 
431                                     "upsweep_mod")]
432                  in  return (threaded2, Just old_linkable)
433
434            -- Compilation really did happen, and succeeded.  A new
435            -- details, iface and linkable are returned.
436            CompOK details (Just (new_iface, new_linkable)) pcs2
437               -> let hst2      = addToUFM hst1 mod_name details
438                      hit2      = addToUFM hit1 mod_name new_iface
439                      threaded2 = CmThreaded pcs2 hst2 hit2
440                  in  return (threaded2, Just new_linkable)
441
442            -- Compilation failed.  compile may still have updated
443            -- the PCS, tho.
444            CompErrs pcs2
445               -> let threaded2 = CmThreaded pcs2 hst1 hit1
446                  in  return (threaded2, Nothing)
447
448
449 -- Remove unwanted modules from the top level envs (HST, HIT, UI).
450 removeFromTopLevelEnvs :: [ModuleName]
451                        -> (HomeSymbolTable, HomeIfaceTable, UnlinkedImage)
452                        -> (HomeSymbolTable, HomeIfaceTable, UnlinkedImage)
453 removeFromTopLevelEnvs zap_these (hst, hit, ui)
454    = (delListFromUFM hst zap_these,
455       delListFromUFM hit zap_these,
456       filterModuleLinkables (`notElem` zap_these) ui
457      )
458
459 retainInTopLevelEnvs :: [ModuleName]
460                         -> (HomeSymbolTable, HomeIfaceTable)
461                         -> (HomeSymbolTable, HomeIfaceTable)
462 retainInTopLevelEnvs keep_these (hst, hit)
463    = (retainInUFM hst keep_these,
464       retainInUFM hit keep_these
465      )
466      where
467         retainInUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
468         retainInUFM ufm keys_to_keep
469            = listToUFM (concatMap (maybeLookupUFM ufm) keys_to_keep)
470         maybeLookupUFM ufm u 
471            = case lookupUFM ufm u of Nothing -> []; Just val -> [(u, val)] 
472
473 -- Needed to clean up HIT and HST so that we don't get duplicates in inst env
474 downwards_closure_of_module :: [ModSummary] -> ModuleName -> [ModuleName]
475 downwards_closure_of_module summaries root
476    = let toEdge :: ModSummary -> (ModuleName,[ModuleName])
477          toEdge summ
478              = (name_of_summary summ, ms_srcimps summ ++ ms_imps summ)
479          res = simple_transitive_closure (map toEdge summaries) [root]             
480      in
481          trace (showSDoc (text "DC of mod" <+> ppr root
482                           <+> text "=" <+> ppr res)) (
483          res
484          )
485
486 -- Calculate transitive closures from a set of roots given an adjacency list
487 simple_transitive_closure :: Eq a => [(a,[a])] -> [a] -> [a]
488 simple_transitive_closure graph set 
489    = let set2      = nub (concatMap dsts set ++ set)
490          dsts node = fromMaybe [] (lookup node graph)
491      in
492          if   length set == length set2
493          then set
494          else simple_transitive_closure graph set2
495
496
497 -- Calculate SCCs of the module graph, with or without taking into
498 -- account source imports.
499 topological_sort :: Bool -> [ModSummary] -> [SCC ModSummary]
500 topological_sort include_source_imports summaries
501    = let 
502          toEdge :: ModSummary -> (ModSummary,ModuleName,[ModuleName])
503          toEdge summ
504              = (summ, name_of_summary summ, 
505                       (if include_source_imports 
506                        then ms_srcimps summ else []) ++ ms_imps summ)
507         
508          mash_edge :: (ModSummary,ModuleName,[ModuleName]) -> (ModSummary,Int,[Int])
509          mash_edge (summ, m, m_imports)
510             = case lookup m key_map of
511                  Nothing -> panic "reverse_topological_sort"
512                  Just mk -> (summ, mk, 
513                                 -- ignore imports not from the home package
514                                 catMaybes (map (flip lookup key_map) m_imports))
515
516          edges     = map toEdge summaries
517          key_map   = zip [nm | (s,nm,imps) <- edges] [1 ..] :: [(ModuleName,Int)]
518          scc_input = map mash_edge edges
519          sccs      = stronglyConnComp scc_input
520      in
521          sccs
522
523
524 -- Chase downwards from the specified root set, returning summaries
525 -- for all home modules encountered.  Only follow source-import
526 -- links.
527 downsweep :: [ModuleName] -> IO [ModSummary]
528 downsweep rootNm
529    = do rootSummaries <- mapM getSummary rootNm
530         loop (filter (isModuleInThisPackage.ms_mod) rootSummaries)
531      where
532         getSummary :: ModuleName -> IO ModSummary
533         getSummary nm
534            | trace ("getSummary: "++ showSDoc (ppr nm)) True
535            = do found <- findModule nm
536                 case found of
537                    -- Be sure not to use the mod and location passed in to 
538                    -- summarise for any other purpose -- summarise may change
539                    -- the module names in them if name of module /= name of file,
540                    -- and put the changed versions in the returned summary.
541                    -- These will then conflict with the passed-in versions.
542                    Just (mod, location) -> summarise mod location
543                    Nothing -> throwDyn (OtherError 
544                                    ("no signs of life for module `" 
545                                      ++ showSDoc (ppr nm) ++ "'"))
546                                  
547         -- loop invariant: homeSummaries doesn't contain package modules
548         loop :: [ModSummary] -> IO [ModSummary]
549         loop homeSummaries
550            = do let allImps :: [ModuleName]
551                     allImps = (nub . concatMap ms_imps) homeSummaries
552                 let allHome   -- all modules currently in homeSummaries
553                        = map (moduleName.ms_mod) homeSummaries
554                 let neededImps
555                        = filter (`notElem` allHome) allImps
556                 neededSummaries
557                        <- mapM getSummary neededImps
558                 let newHomeSummaries
559                        = filter (isModuleInThisPackage.ms_mod) neededSummaries
560                 if null newHomeSummaries
561                  then return homeSummaries
562                  else loop (newHomeSummaries ++ homeSummaries)
563
564
565 -- Summarise a module, and pick up source and interface timestamps.
566 summarise :: Module -> ModuleLocation -> IO ModSummary
567 summarise mod location
568    | isModuleInThisPackage mod
569    = do let hs_fn = unJust (ml_hs_file location) "summarise"
570         hspp_fn <- preprocess hs_fn
571         modsrc <- readFile hspp_fn
572         let (srcimps,imps,mod_name) = getImports modsrc
573
574         maybe_src_timestamp
575            <- case ml_hs_file location of 
576                  Nothing     -> return Nothing
577                  Just src_fn -> maybe_getModificationTime src_fn
578         maybe_iface_timestamp
579            <- case ml_hi_file location of 
580                  Nothing     -> return Nothing
581                  Just if_fn  -> maybe_getModificationTime if_fn
582
583         -- If the module name is Main, allow it to be in a file
584         -- different from Main.hs, and mash the mod and loc 
585         -- to match.  Otherwise just moan.
586         (mashed_mod, mashed_loc)
587            <- case () of
588               () |  mod_name == moduleName mod
589                  -> return (mod, location)
590                  |  mod_name /= moduleName mod && mod_name == mkModuleName "Main"
591                  -> return (mash mod location "Main")
592                  |  otherwise
593                  -> do hPutStrLn stderr (showSDoc (
594                           text "ghc: warning: file name - module name mismatch:" <+> 
595                           ppr (moduleName mod) <+> text "vs" <+> ppr mod_name))
596                        return (mash mod location (moduleNameUserString (moduleName mod)))
597                where
598                  mash old_mod old_loc new_nm
599                     = (mkHomeModule (mkModuleName new_nm), 
600                        old_loc{ml_hi_file = maybe_swizzle_basename new_nm 
601                                                 (ml_hi_file old_loc)})
602
603                  maybe_swizzle_basename new Nothing = Nothing
604                  maybe_swizzle_basename new (Just old) 
605                     = case splitFilename3 old of 
606                          (dir, name, ext) -> Just (dir ++ new ++ ext)
607
608         return (ModSummary mashed_mod 
609                            mashed_loc{ml_hspp_file=Just hspp_fn} 
610                            srcimps imps
611                            maybe_src_timestamp maybe_iface_timestamp)
612
613    | otherwise
614    = return (ModSummary mod location [] [] Nothing Nothing)
615
616    where
617       maybe_getModificationTime :: FilePath -> IO (Maybe ClockTime)
618       maybe_getModificationTime fn
619          = (do time <- getModificationTime fn
620                return (Just time)) 
621            `catch`
622            (\err -> return Nothing)
623
624 cmLookupSymbol :: RdrName -> CmState -> Maybe HValue
625 cmLookupSymbol nm CmState{ pls = pls } = lookupClosure nm pls
626 \end{code}