Unify hsig and hs-boot; add preliminary "hs-boot" merging.
[ghc.git] / compiler / main / Finder.hs
1 {-
2 (c) The University of Glasgow, 2000-2006
3
4 \section[Finder]{Module Finder}
5 -}
6
7 {-# LANGUAGE CPP #-}
8
9 module Finder (
10 flushFinderCaches,
11 FindResult(..),
12 findImportedModule,
13 findExactModule,
14 findHomeModule,
15 findExposedPackageModule,
16 mkHomeModLocation,
17 mkHomeModLocation2,
18 mkHiOnlyModLocation,
19 addHomeModuleToFinder,
20 uncacheModule,
21 mkStubPaths,
22
23 findObjectLinkableMaybe,
24 findObjectLinkable,
25
26 cannotFindModule,
27 cannotFindInterface,
28
29 ) where
30
31 #include "HsVersions.h"
32
33 import Module
34 import HscTypes
35 import Packages
36 import FastString
37 import Util
38 import PrelNames ( gHC_PRIM )
39 import DynFlags
40 import Outputable
41 import Maybes ( expectJust )
42
43 import Data.IORef ( IORef, readIORef, atomicModifyIORef' )
44 import System.Directory
45 import System.FilePath
46 import Control.Monad
47 import Data.Time
48 import Data.List ( foldl' )
49
50
51 type FileExt = String -- Filename extension
52 type BaseName = String -- Basename of file
53
54 -- -----------------------------------------------------------------------------
55 -- The Finder
56
57 -- The Finder provides a thin filesystem abstraction to the rest of
58 -- the compiler. For a given module, it can tell you where the
59 -- source, interface, and object files for that module live.
60
61 -- It does *not* know which particular package a module lives in. Use
62 -- Packages.lookupModuleInAllPackages for that.
63
64 -- -----------------------------------------------------------------------------
65 -- The finder's cache
66
67 -- remove all the home modules from the cache; package modules are
68 -- assumed to not move around during a session.
69 flushFinderCaches :: HscEnv -> IO ()
70 flushFinderCaches hsc_env =
71 atomicModifyIORef' fc_ref $ \fm -> (filterModuleEnv is_ext fm, ())
72 where
73 this_pkg = thisPackage (hsc_dflags hsc_env)
74 fc_ref = hsc_FC hsc_env
75 is_ext mod _ | modulePackageKey mod /= this_pkg = True
76 | otherwise = False
77
78 addToFinderCache :: IORef FinderCache -> Module -> FindResult -> IO ()
79 addToFinderCache ref key val =
80 atomicModifyIORef' ref $ \c -> (extendModuleEnv c key val, ())
81
82 removeFromFinderCache :: IORef FinderCache -> Module -> IO ()
83 removeFromFinderCache ref key =
84 atomicModifyIORef' ref $ \c -> (delModuleEnv c key, ())
85
86 lookupFinderCache :: IORef FinderCache -> Module -> IO (Maybe FindResult)
87 lookupFinderCache ref key = do
88 c <- readIORef ref
89 return $! lookupModuleEnv c key
90
91 -- -----------------------------------------------------------------------------
92 -- The two external entry points
93
94 -- | Locate a module that was imported by the user. We have the
95 -- module's name, and possibly a package name. Without a package
96 -- name, this function will use the search path and the known exposed
97 -- packages to find the module, if a package is specified then only
98 -- that package is searched for the module.
99
100 findImportedModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
101 findImportedModule hsc_env mod_name mb_pkg =
102 case mb_pkg of
103 Nothing -> unqual_import
104 Just pkg | pkg == fsLit "this" -> home_import -- "this" is special
105 | otherwise -> pkg_import
106 where
107 home_import = findHomeModule hsc_env mod_name
108
109 pkg_import = findExposedPackageModule hsc_env mod_name mb_pkg
110
111 unqual_import = home_import
112 `orIfNotFound`
113 findExposedPackageModule hsc_env mod_name Nothing
114
115 -- | Locate a specific 'Module'. The purpose of this function is to
116 -- create a 'ModLocation' for a given 'Module', that is to find out
117 -- where the files associated with this module live. It is used when
118 -- reading the interface for a module mentioned by another interface,
119 -- for example (a "system import").
120
121 findExactModule :: HscEnv -> Module -> IO FindResult
122 findExactModule hsc_env mod =
123 let dflags = hsc_dflags hsc_env
124 in if modulePackageKey mod == thisPackage dflags
125 then findHomeModule hsc_env (moduleName mod)
126 else findPackageModule hsc_env mod
127
128 -- -----------------------------------------------------------------------------
129 -- Helpers
130
131 orIfNotFound :: IO FindResult -> IO FindResult -> IO FindResult
132 orIfNotFound this or_this = do
133 res <- this
134 case res of
135 NotFound { fr_paths = paths1, fr_mods_hidden = mh1
136 , fr_pkgs_hidden = ph1, fr_suggestions = s1 }
137 -> do res2 <- or_this
138 case res2 of
139 NotFound { fr_paths = paths2, fr_pkg = mb_pkg2, fr_mods_hidden = mh2
140 , fr_pkgs_hidden = ph2, fr_suggestions = s2 }
141 -> return (NotFound { fr_paths = paths1 ++ paths2
142 , fr_pkg = mb_pkg2 -- snd arg is the package search
143 , fr_mods_hidden = mh1 ++ mh2
144 , fr_pkgs_hidden = ph1 ++ ph2
145 , fr_suggestions = s1 ++ s2 })
146 _other -> return res2
147 _other -> return res
148
149 -- | Helper function for 'findHomeModule': this function wraps an IO action
150 -- which would look up @mod_name@ in the file system (the home package),
151 -- and first consults the 'hsc_FC' cache to see if the lookup has already
152 -- been done. Otherwise, do the lookup (with the IO action) and save
153 -- the result in the finder cache and the module location cache (if it
154 -- was successful.)
155 homeSearchCache :: HscEnv -> ModuleName -> IO FindResult -> IO FindResult
156 homeSearchCache hsc_env mod_name do_this = do
157 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
158 modLocationCache hsc_env mod do_this
159
160 findExposedPackageModule :: HscEnv -> ModuleName -> Maybe FastString
161 -> IO FindResult
162 findExposedPackageModule hsc_env mod_name mb_pkg
163 = case lookupModuleWithSuggestions (hsc_dflags hsc_env) mod_name mb_pkg of
164 LookupFound m pkg_conf ->
165 findPackageModule_ hsc_env m pkg_conf
166 LookupMultiple rs ->
167 return (FoundMultiple rs)
168 LookupHidden pkg_hiddens mod_hiddens ->
169 return (NotFound{ fr_paths = [], fr_pkg = Nothing
170 , fr_pkgs_hidden = map (modulePackageKey.fst) pkg_hiddens
171 , fr_mods_hidden = map (modulePackageKey.fst) mod_hiddens
172 , fr_suggestions = [] })
173 LookupNotFound suggest ->
174 return (NotFound{ fr_paths = [], fr_pkg = Nothing
175 , fr_pkgs_hidden = []
176 , fr_mods_hidden = []
177 , fr_suggestions = suggest })
178
179 modLocationCache :: HscEnv -> Module -> IO FindResult -> IO FindResult
180 modLocationCache hsc_env mod do_this = do
181 m <- lookupFinderCache (hsc_FC hsc_env) mod
182 case m of
183 Just result -> return result
184 Nothing -> do
185 result <- do_this
186 addToFinderCache (hsc_FC hsc_env) mod result
187 return result
188
189 addHomeModuleToFinder :: HscEnv -> ModuleName -> ModLocation -> IO Module
190 addHomeModuleToFinder hsc_env mod_name loc = do
191 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
192 addToFinderCache (hsc_FC hsc_env) mod (Found loc mod)
193 return mod
194
195 uncacheModule :: HscEnv -> ModuleName -> IO ()
196 uncacheModule hsc_env mod = do
197 let this_pkg = thisPackage (hsc_dflags hsc_env)
198 removeFromFinderCache (hsc_FC hsc_env) (mkModule this_pkg mod)
199
200 -- -----------------------------------------------------------------------------
201 -- The internal workers
202
203 -- | Implements the search for a module name in the home package only. Calling
204 -- this function directly is usually *not* what you want; currently, it's used
205 -- as a building block for the following operations:
206 --
207 -- 1. When you do a normal package lookup, we first check if the module
208 -- is available in the home module, before looking it up in the package
209 -- database.
210 --
211 -- 2. When you have a package qualified import with package name "this",
212 -- we shortcut to the home module.
213 --
214 -- 3. When we look up an exact 'Module', if the package key associated with
215 -- the module is the current home module do a look up in the home module.
216 --
217 -- 4. Some special-case code in GHCi (ToDo: Figure out why that needs to
218 -- call this.)
219 findHomeModule :: HscEnv -> ModuleName -> IO FindResult
220 findHomeModule hsc_env mod_name =
221 homeSearchCache hsc_env mod_name $
222 let
223 dflags = hsc_dflags hsc_env
224 home_path = importPaths dflags
225 hisuf = hiSuf dflags
226 mod = mkModule (thisPackage dflags) mod_name
227
228 source_exts =
229 [ ("hs", mkHomeModLocationSearched dflags mod_name "hs")
230 , ("lhs", mkHomeModLocationSearched dflags mod_name "lhs")
231 -- TODO: This is a giant hack! If we find an hs-boot file,
232 -- pretend that there's an hs file here too, even if there isn't.
233 -- GhcMake will know what to do next.
234 , ("hs-boot", mkHomeModLocationSearched dflags mod_name "hs")
235 , ("lhs-boot", mkHomeModLocationSearched dflags mod_name "lhs")
236 ]
237
238 hi_exts = [ (hisuf, mkHiOnlyModLocation dflags hisuf)
239 , (addBootSuffix hisuf, mkHiOnlyModLocation dflags hisuf)
240 ]
241
242 -- In compilation manager modes, we look for source files in the home
243 -- package because we can compile these automatically. In one-shot
244 -- compilation mode we look for .hi and .hi-boot files only.
245 exts | isOneShot (ghcMode dflags) = hi_exts
246 | otherwise = source_exts
247 in
248
249 -- special case for GHC.Prim; we won't find it in the filesystem.
250 -- This is important only when compiling the base package (where GHC.Prim
251 -- is a home module).
252 if mod == gHC_PRIM
253 then return (Found (error "GHC.Prim ModLocation") mod)
254 else searchPathExts home_path mod exts
255
256 -- | Search for a module in external packages only.
257 findPackageModule :: HscEnv -> Module -> IO FindResult
258 findPackageModule hsc_env mod = do
259 let
260 dflags = hsc_dflags hsc_env
261 pkg_id = modulePackageKey mod
262 --
263 case lookupPackage dflags pkg_id of
264 Nothing -> return (NoPackage pkg_id)
265 Just pkg_conf -> findPackageModule_ hsc_env mod pkg_conf
266
267 -- | Look up the interface file associated with module @mod@. This function
268 -- requires a few invariants to be upheld: (1) the 'Module' in question must
269 -- be the module identifier of the *original* implementation of a module,
270 -- not a reexport (this invariant is upheld by @Packages.hs@) and (2)
271 -- the 'PackageConfig' must be consistent with the package key in the 'Module'.
272 -- The redundancy is to avoid an extra lookup in the package state
273 -- for the appropriate config.
274 findPackageModule_ :: HscEnv -> Module -> PackageConfig -> IO FindResult
275 findPackageModule_ hsc_env mod pkg_conf =
276 ASSERT( modulePackageKey mod == packageConfigId pkg_conf )
277 modLocationCache hsc_env mod $
278
279 -- special case for GHC.Prim; we won't find it in the filesystem.
280 if mod == gHC_PRIM
281 then return (Found (error "GHC.Prim ModLocation") mod)
282 else
283
284 let
285 dflags = hsc_dflags hsc_env
286 tag = buildTag dflags
287
288 -- hi-suffix for packages depends on the build tag.
289 package_hisuf | null tag = "hi"
290 | otherwise = tag ++ "_hi"
291
292 mk_hi_loc = mkHiOnlyModLocation dflags package_hisuf
293
294 import_dirs = importDirs pkg_conf
295 -- we never look for a .hi-boot file in an external package;
296 -- .hi-boot files only make sense for the home package.
297 in
298 case import_dirs of
299 [one] | MkDepend <- ghcMode dflags -> do
300 -- there's only one place that this .hi file can be, so
301 -- don't bother looking for it.
302 let basename = moduleNameSlashes (moduleName mod)
303 loc <- mk_hi_loc one basename
304 return (Found loc mod)
305 _otherwise ->
306 searchPathExts import_dirs mod [(package_hisuf, mk_hi_loc)]
307
308 -- -----------------------------------------------------------------------------
309 -- General path searching
310
311 searchPathExts
312 :: [FilePath] -- paths to search
313 -> Module -- module name
314 -> [ (
315 FileExt, -- suffix
316 FilePath -> BaseName -> IO ModLocation -- action
317 )
318 ]
319 -> IO FindResult
320
321 searchPathExts paths mod exts
322 = do result <- search to_search
323 {-
324 hPutStrLn stderr (showSDoc $
325 vcat [text "Search" <+> ppr mod <+> sep (map (text. fst) exts)
326 , nest 2 (vcat (map text paths))
327 , case result of
328 Succeeded (loc, p) -> text "Found" <+> ppr loc
329 Failed fs -> text "not found"])
330 -}
331 return result
332
333 where
334 basename = moduleNameSlashes (moduleName mod)
335
336 to_search :: [(FilePath, IO ModLocation)]
337 to_search = [ (file, fn path basename)
338 | path <- paths,
339 (ext,fn) <- exts,
340 let base | path == "." = basename
341 | otherwise = path </> basename
342 file = base <.> ext
343 ]
344
345 search [] = return (NotFound { fr_paths = map fst to_search
346 , fr_pkg = Just (modulePackageKey mod)
347 , fr_mods_hidden = [], fr_pkgs_hidden = []
348 , fr_suggestions = [] })
349
350 search ((file, mk_result) : rest) = do
351 b <- doesFileExist file
352 if b
353 then do { loc <- mk_result; return (Found loc mod) }
354 else search rest
355
356 mkHomeModLocationSearched :: DynFlags -> ModuleName -> FileExt
357 -> FilePath -> BaseName -> IO ModLocation
358 mkHomeModLocationSearched dflags mod suff path basename = do
359 mkHomeModLocation2 dflags mod (path </> basename) suff
360
361 -- -----------------------------------------------------------------------------
362 -- Constructing a home module location
363
364 -- This is where we construct the ModLocation for a module in the home
365 -- package, for which we have a source file. It is called from three
366 -- places:
367 --
368 -- (a) Here in the finder, when we are searching for a module to import,
369 -- using the search path (-i option).
370 --
371 -- (b) The compilation manager, when constructing the ModLocation for
372 -- a "root" module (a source file named explicitly on the command line
373 -- or in a :load command in GHCi).
374 --
375 -- (c) The driver in one-shot mode, when we need to construct a
376 -- ModLocation for a source file named on the command-line.
377 --
378 -- Parameters are:
379 --
380 -- mod
381 -- The name of the module
382 --
383 -- path
384 -- (a): The search path component where the source file was found.
385 -- (b) and (c): "."
386 --
387 -- src_basename
388 -- (a): (moduleNameSlashes mod)
389 -- (b) and (c): The filename of the source file, minus its extension
390 --
391 -- ext
392 -- The filename extension of the source file (usually "hs" or "lhs").
393
394 mkHomeModLocation :: DynFlags -> ModuleName -> FilePath -> IO ModLocation
395 mkHomeModLocation dflags mod src_filename = do
396 let (basename,extension) = splitExtension src_filename
397 mkHomeModLocation2 dflags mod basename extension
398
399 mkHomeModLocation2 :: DynFlags
400 -> ModuleName
401 -> FilePath -- Of source module, without suffix
402 -> String -- Suffix
403 -> IO ModLocation
404 mkHomeModLocation2 dflags mod src_basename ext = do
405 let mod_basename = moduleNameSlashes mod
406
407 obj_fn = mkObjPath dflags src_basename mod_basename
408 hi_fn = mkHiPath dflags src_basename mod_basename
409
410 return (ModLocation{ ml_hs_file = Just (src_basename <.> ext),
411 ml_hi_file = hi_fn,
412 ml_obj_file = obj_fn })
413
414 mkHiOnlyModLocation :: DynFlags -> Suffix -> FilePath -> String
415 -> IO ModLocation
416 mkHiOnlyModLocation dflags hisuf path basename
417 = do let full_basename = path </> basename
418 obj_fn = mkObjPath dflags full_basename basename
419 return ModLocation{ ml_hs_file = Nothing,
420 ml_hi_file = full_basename <.> hisuf,
421 -- Remove the .hi-boot suffix from
422 -- hi_file, if it had one. We always
423 -- want the name of the real .hi file
424 -- in the ml_hi_file field.
425 ml_obj_file = obj_fn
426 }
427
428 -- | Constructs the filename of a .o file for a given source file.
429 -- Does /not/ check whether the .o file exists
430 mkObjPath
431 :: DynFlags
432 -> FilePath -- the filename of the source file, minus the extension
433 -> String -- the module name with dots replaced by slashes
434 -> FilePath
435 mkObjPath dflags basename mod_basename = obj_basename <.> osuf
436 where
437 odir = objectDir dflags
438 osuf = objectSuf dflags
439
440 obj_basename | Just dir <- odir = dir </> mod_basename
441 | otherwise = basename
442
443
444 -- | Constructs the filename of a .hi file for a given source file.
445 -- Does /not/ check whether the .hi file exists
446 mkHiPath
447 :: DynFlags
448 -> FilePath -- the filename of the source file, minus the extension
449 -> String -- the module name with dots replaced by slashes
450 -> FilePath
451 mkHiPath dflags basename mod_basename = hi_basename <.> hisuf
452 where
453 hidir = hiDir dflags
454 hisuf = hiSuf dflags
455
456 hi_basename | Just dir <- hidir = dir </> mod_basename
457 | otherwise = basename
458
459
460
461 -- -----------------------------------------------------------------------------
462 -- Filenames of the stub files
463
464 -- We don't have to store these in ModLocations, because they can be derived
465 -- from other available information, and they're only rarely needed.
466
467 mkStubPaths
468 :: DynFlags
469 -> ModuleName
470 -> ModLocation
471 -> FilePath
472
473 mkStubPaths dflags mod location
474 = let
475 stubdir = stubDir dflags
476
477 mod_basename = moduleNameSlashes mod
478 src_basename = dropExtension $ expectJust "mkStubPaths"
479 (ml_hs_file location)
480
481 stub_basename0
482 | Just dir <- stubdir = dir </> mod_basename
483 | otherwise = src_basename
484
485 stub_basename = stub_basename0 ++ "_stub"
486 in
487 stub_basename <.> "h"
488
489 -- -----------------------------------------------------------------------------
490 -- findLinkable isn't related to the other stuff in here,
491 -- but there's no other obvious place for it
492
493 findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
494 findObjectLinkableMaybe mod locn
495 = do let obj_fn = ml_obj_file locn
496 maybe_obj_time <- modificationTimeIfExists obj_fn
497 case maybe_obj_time of
498 Nothing -> return Nothing
499 Just obj_time -> liftM Just (findObjectLinkable mod obj_fn obj_time)
500
501 -- Make an object linkable when we know the object file exists, and we know
502 -- its modification time.
503 findObjectLinkable :: Module -> FilePath -> UTCTime -> IO Linkable
504 findObjectLinkable mod obj_fn obj_time = return (LM obj_time mod [DotO obj_fn])
505 -- We used to look for _stub.o files here, but that was a bug (#706)
506 -- Now GHC merges the stub.o into the main .o (#3687)
507
508 -- -----------------------------------------------------------------------------
509 -- Error messages
510
511 cannotFindModule :: DynFlags -> ModuleName -> FindResult -> SDoc
512 cannotFindModule = cantFindErr (sLit "Could not find module")
513 (sLit "Ambiguous module name")
514
515 cannotFindInterface :: DynFlags -> ModuleName -> FindResult -> SDoc
516 cannotFindInterface = cantFindErr (sLit "Failed to load interface for")
517 (sLit "Ambiguous interface for")
518
519 cantFindErr :: LitString -> LitString -> DynFlags -> ModuleName -> FindResult
520 -> SDoc
521 cantFindErr _ multiple_found _ mod_name (FoundMultiple mods)
522 | Just pkgs <- unambiguousPackages
523 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
524 sep [ptext (sLit "it was found in multiple packages:"),
525 hsep (map ppr pkgs) ]
526 )
527 | otherwise
528 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
529 vcat (map pprMod mods)
530 )
531 where
532 unambiguousPackages = foldl' unambiguousPackage (Just []) mods
533 unambiguousPackage (Just xs) (m, ModOrigin (Just _) _ _ _)
534 = Just (modulePackageKey m : xs)
535 unambiguousPackage _ _ = Nothing
536
537 pprMod (m, o) = ptext (sLit "it is bound as") <+> ppr m <+>
538 ptext (sLit "by") <+> pprOrigin m o
539 pprOrigin _ ModHidden = panic "cantFindErr: bound by mod hidden"
540 pprOrigin m (ModOrigin e res _ f) = sep $ punctuate comma (
541 if e == Just True
542 then [ptext (sLit "package") <+> ppr (modulePackageKey m)]
543 else [] ++
544 map ((ptext (sLit "a reexport in package") <+>)
545 .ppr.packageConfigId) res ++
546 if f then [ptext (sLit "a package flag")] else []
547 )
548
549 cantFindErr cannot_find _ dflags mod_name find_result
550 = ptext cannot_find <+> quotes (ppr mod_name)
551 $$ more_info
552 where
553 more_info
554 = case find_result of
555 NoPackage pkg
556 -> ptext (sLit "no package key matching") <+> quotes (ppr pkg) <+>
557 ptext (sLit "was found") $$ looks_like_srcpkgid pkg
558
559 NotFound { fr_paths = files, fr_pkg = mb_pkg
560 , fr_mods_hidden = mod_hiddens, fr_pkgs_hidden = pkg_hiddens
561 , fr_suggestions = suggest }
562 | Just pkg <- mb_pkg, pkg /= thisPackage dflags
563 -> not_found_in_package pkg files
564
565 | not (null suggest)
566 -> pp_suggestions suggest $$ tried_these files
567
568 | null files && null mod_hiddens && null pkg_hiddens
569 -> ptext (sLit "It is not a module in the current program, or in any known package.")
570
571 | otherwise
572 -> vcat (map pkg_hidden pkg_hiddens) $$
573 vcat (map mod_hidden mod_hiddens) $$
574 tried_these files
575
576 _ -> panic "cantFindErr"
577
578 build_tag = buildTag dflags
579
580 not_found_in_package pkg files
581 | build_tag /= ""
582 = let
583 build = if build_tag == "p" then "profiling"
584 else "\"" ++ build_tag ++ "\""
585 in
586 ptext (sLit "Perhaps you haven't installed the ") <> text build <>
587 ptext (sLit " libraries for package ") <> quotes (ppr pkg) <> char '?' $$
588 tried_these files
589
590 | otherwise
591 = ptext (sLit "There are files missing in the ") <> quotes (ppr pkg) <>
592 ptext (sLit " package,") $$
593 ptext (sLit "try running 'ghc-pkg check'.") $$
594 tried_these files
595
596 tried_these files
597 | null files = Outputable.empty
598 | verbosity dflags < 3 =
599 ptext (sLit "Use -v to see a list of the files searched for.")
600 | otherwise =
601 hang (ptext (sLit "Locations searched:")) 2 $ vcat (map text files)
602
603 pkg_hidden :: PackageKey -> SDoc
604 pkg_hidden pkgid =
605 ptext (sLit "It is a member of the hidden package")
606 <+> quotes (ppr pkgid)
607 --FIXME: we don't really want to show the package key here we should
608 -- show the source package id or installed package id if it's ambiguous
609 <> dot $$ cabal_pkg_hidden_hint pkgid
610 cabal_pkg_hidden_hint pkgid
611 | gopt Opt_BuildingCabalPackage dflags
612 = let pkg = expectJust "pkg_hidden" (lookupPackage dflags pkgid)
613 in ptext (sLit "Perhaps you need to add") <+>
614 quotes (ppr (packageName pkg)) <+>
615 ptext (sLit "to the build-depends in your .cabal file.")
616 | otherwise = Outputable.empty
617
618 looks_like_srcpkgid :: PackageKey -> SDoc
619 looks_like_srcpkgid pk
620 -- Unsafely coerce a package key FastString into a source package ID
621 -- FastString and see if it means anything.
622 | (pkg:pkgs) <- searchPackageId dflags (SourcePackageId (packageKeyFS pk))
623 = parens (text "This package key looks like the source package ID;" $$
624 text "the real package key is" <+> quotes (ftext (packageKeyFS (packageKey pkg))) $$
625 (if null pkgs then Outputable.empty
626 else text "and" <+> int (length pkgs) <+> text "other candidates"))
627 -- Todo: also check if it looks like a package name!
628 | otherwise = Outputable.empty
629
630 mod_hidden pkg =
631 ptext (sLit "it is a hidden module in the package") <+> quotes (ppr pkg)
632
633 pp_suggestions :: [ModuleSuggestion] -> SDoc
634 pp_suggestions sugs
635 | null sugs = Outputable.empty
636 | otherwise = hang (ptext (sLit "Perhaps you meant"))
637 2 (vcat (map pp_sugg sugs))
638
639 -- NB: Prefer the *original* location, and then reexports, and then
640 -- package flags when making suggestions. ToDo: if the original package
641 -- also has a reexport, prefer that one
642 pp_sugg (SuggestVisible m mod o) = ppr m <+> provenance o
643 where provenance ModHidden = Outputable.empty
644 provenance (ModOrigin{ fromOrigPackage = e,
645 fromExposedReexport = res,
646 fromPackageFlag = f })
647 | Just True <- e
648 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
649 | f && moduleName mod == m
650 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
651 | (pkg:_) <- res
652 = parens (ptext (sLit "from") <+> ppr (packageConfigId pkg)
653 <> comma <+> ptext (sLit "reexporting") <+> ppr mod)
654 | f
655 = parens (ptext (sLit "defined via package flags to be")
656 <+> ppr mod)
657 | otherwise = Outputable.empty
658 pp_sugg (SuggestHidden m mod o) = ppr m <+> provenance o
659 where provenance ModHidden = Outputable.empty
660 provenance (ModOrigin{ fromOrigPackage = e,
661 fromHiddenReexport = rhs })
662 | Just False <- e
663 = parens (ptext (sLit "needs flag -package-key")
664 <+> ppr (modulePackageKey mod))
665 | (pkg:_) <- rhs
666 = parens (ptext (sLit "needs flag -package-key")
667 <+> ppr (packageConfigId pkg))
668 | otherwise = Outputable.empty