Support for multiple signature files in scope.
[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 convFindExactResult, -- move to HscTypes?
13 findImportedModule,
14 findExactModule,
15 findHomeModule,
16 findExposedPackageModule,
17 mkHomeModLocation,
18 mkHomeModLocation2,
19 mkHiOnlyModLocation,
20 addHomeModuleToFinder,
21 uncacheModule,
22 mkStubPaths,
23
24 findObjectLinkableMaybe,
25 findObjectLinkable,
26
27 cannotFindModule,
28 cannotFindInterface,
29
30 ) where
31
32 #include "HsVersions.h"
33
34 import Module
35 import HscTypes
36 import Packages
37 import FastString
38 import Util
39 import PrelNames ( gHC_PRIM )
40 import DynFlags
41 import Outputable
42 import Maybes ( expectJust )
43
44 import Data.IORef ( IORef, readIORef, atomicModifyIORef' )
45 import System.Directory
46 import System.FilePath
47 import Control.Monad
48 import Data.Time
49 import Data.List ( foldl', partition )
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 -> FindExactResult -> 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 FindExactResult)
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 = convFindExactResult `fmap` 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 FindExactResult
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
156 -> ModuleName
157 -> IO FindExactResult
158 -> IO FindExactResult
159 homeSearchCache hsc_env mod_name do_this = do
160 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
161 modLocationCache hsc_env mod do_this
162
163 -- | Converts a 'FindExactResult' into a 'FindResult' in the obvious way.
164 convFindExactResult :: FindExactResult -> FindResult
165 convFindExactResult (FoundExact loc m) = FoundModule (FoundHs loc m)
166 convFindExactResult (NoPackageExact pk) = NoPackage pk
167 convFindExactResult NotFoundExact { fer_paths = paths, fer_pkg = pkg } =
168 NotFound {
169 fr_paths = paths, fr_pkg = pkg,
170 fr_pkgs_hidden = [], fr_mods_hidden = [], fr_suggestions = []
171 }
172
173 foundExact :: FindExactResult -> Bool
174 foundExact FoundExact{} = True
175 foundExact _ = False
176
177 findExposedPackageModule :: HscEnv -> ModuleName -> Maybe FastString
178 -> IO FindResult
179 findExposedPackageModule hsc_env mod_name mb_pkg
180 = case lookupModuleWithSuggestions (hsc_dflags hsc_env) mod_name mb_pkg of
181 LookupFound (m, _) -> do
182 fmap convFindExactResult (findPackageModule hsc_env m)
183 LookupFoundSigs ms backing -> do
184 locs <- mapM (findPackageModule hsc_env . fst) ms
185 let (ok, missing) = partition foundExact locs
186 case missing of
187 -- At the moment, we return the errors one at a time. It might be
188 -- better if we collected them up and reported them all, but
189 -- FindResult doesn't have enough information to support this.
190 -- In any case, this REALLY shouldn't happen (it means there are
191 -- broken packages in the database.)
192 (m:_) -> return (convFindExactResult m)
193 _ -> return (FoundSigs [FoundHs l m | FoundExact l m <- ok] backing)
194 LookupMultiple rs ->
195 return (FoundMultiple rs)
196 LookupHidden pkg_hiddens mod_hiddens ->
197 return (NotFound{ fr_paths = [], fr_pkg = Nothing
198 , fr_pkgs_hidden = map (modulePackageKey.fst) pkg_hiddens
199 , fr_mods_hidden = map (modulePackageKey.fst) mod_hiddens
200 , fr_suggestions = [] })
201 LookupNotFound suggest ->
202 return (NotFound{ fr_paths = [], fr_pkg = Nothing
203 , fr_pkgs_hidden = []
204 , fr_mods_hidden = []
205 , fr_suggestions = suggest })
206
207 modLocationCache :: HscEnv -> Module -> IO FindExactResult -> IO FindExactResult
208 modLocationCache hsc_env mod do_this = do
209 m <- lookupFinderCache (hsc_FC hsc_env) mod
210 case m of
211 Just result -> return result
212 Nothing -> do
213 result <- do_this
214 addToFinderCache (hsc_FC hsc_env) mod result
215 return result
216
217 addHomeModuleToFinder :: HscEnv -> ModuleName -> ModLocation -> IO Module
218 addHomeModuleToFinder hsc_env mod_name loc = do
219 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
220 addToFinderCache (hsc_FC hsc_env) mod (FoundExact loc mod)
221 return mod
222
223 uncacheModule :: HscEnv -> ModuleName -> IO ()
224 uncacheModule hsc_env mod = do
225 let this_pkg = thisPackage (hsc_dflags hsc_env)
226 removeFromFinderCache (hsc_FC hsc_env) (mkModule this_pkg mod)
227
228 -- -----------------------------------------------------------------------------
229 -- The internal workers
230
231 -- | Implements the search for a module name in the home package only. Calling
232 -- this function directly is usually *not* what you want; currently, it's used
233 -- as a building block for the following operations:
234 --
235 -- 1. When you do a normal package lookup, we first check if the module
236 -- is available in the home module, before looking it up in the package
237 -- database.
238 --
239 -- 2. When you have a package qualified import with package name "this",
240 -- we shortcut to the home module.
241 --
242 -- 3. When we look up an exact 'Module', if the package key associated with
243 -- the module is the current home module do a look up in the home module.
244 --
245 -- 4. Some special-case code in GHCi (ToDo: Figure out why that needs to
246 -- call this.)
247 findHomeModule :: HscEnv -> ModuleName -> IO FindExactResult
248 findHomeModule hsc_env mod_name =
249 homeSearchCache hsc_env mod_name $
250 let
251 dflags = hsc_dflags hsc_env
252 home_path = importPaths dflags
253 hisuf = hiSuf dflags
254 mod = mkModule (thisPackage dflags) mod_name
255
256 source_exts =
257 [ ("hs", mkHomeModLocationSearched dflags mod_name "hs")
258 , ("lhs", mkHomeModLocationSearched dflags mod_name "lhs")
259 , ("hsig", mkHomeModLocationSearched dflags mod_name "hsig")
260 , ("lhsig", mkHomeModLocationSearched dflags mod_name "lhsig")
261 ]
262
263 hi_exts = [ (hisuf, mkHiOnlyModLocation dflags hisuf)
264 , (addBootSuffix hisuf, mkHiOnlyModLocation dflags hisuf)
265 ]
266
267 -- In compilation manager modes, we look for source files in the home
268 -- package because we can compile these automatically. In one-shot
269 -- compilation mode we look for .hi and .hi-boot files only.
270 exts | isOneShot (ghcMode dflags) = hi_exts
271 | otherwise = source_exts
272 in
273
274 -- special case for GHC.Prim; we won't find it in the filesystem.
275 -- This is important only when compiling the base package (where GHC.Prim
276 -- is a home module).
277 if mod == gHC_PRIM
278 then return (FoundExact (error "GHC.Prim ModLocation") mod)
279 else searchPathExts home_path mod exts
280
281
282 -- | Search for a module in external packages only.
283 findPackageModule :: HscEnv -> Module -> IO FindExactResult
284 findPackageModule hsc_env mod = do
285 let
286 dflags = hsc_dflags hsc_env
287 pkg_id = modulePackageKey mod
288 --
289 case lookupPackage dflags pkg_id of
290 Nothing -> return (NoPackageExact pkg_id)
291 Just pkg_conf -> findPackageModule_ hsc_env mod pkg_conf
292
293 -- | Look up the interface file associated with module @mod@. This function
294 -- requires a few invariants to be upheld: (1) the 'Module' in question must
295 -- be the module identifier of the *original* implementation of a module,
296 -- not a reexport (this invariant is upheld by @Packages.hs@) and (2)
297 -- the 'PackageConfig' must be consistent with the package key in the 'Module'.
298 -- The redundancy is to avoid an extra lookup in the package state
299 -- for the appropriate config.
300 findPackageModule_ :: HscEnv -> Module -> PackageConfig -> IO FindExactResult
301 findPackageModule_ hsc_env mod pkg_conf =
302 ASSERT( modulePackageKey mod == packageConfigId pkg_conf )
303 modLocationCache hsc_env mod $
304
305 -- special case for GHC.Prim; we won't find it in the filesystem.
306 if mod == gHC_PRIM
307 then return (FoundExact (error "GHC.Prim ModLocation") mod)
308 else
309
310 let
311 dflags = hsc_dflags hsc_env
312 tag = buildTag dflags
313
314 -- hi-suffix for packages depends on the build tag.
315 package_hisuf | null tag = "hi"
316 | otherwise = tag ++ "_hi"
317
318 mk_hi_loc = mkHiOnlyModLocation dflags package_hisuf
319
320 import_dirs = importDirs pkg_conf
321 -- we never look for a .hi-boot file in an external package;
322 -- .hi-boot files only make sense for the home package.
323 in
324 case import_dirs of
325 [one] | MkDepend <- ghcMode dflags -> do
326 -- there's only one place that this .hi file can be, so
327 -- don't bother looking for it.
328 let basename = moduleNameSlashes (moduleName mod)
329 loc <- mk_hi_loc one basename
330 return (FoundExact loc mod)
331 _otherwise ->
332 searchPathExts import_dirs mod [(package_hisuf, mk_hi_loc)]
333
334 -- -----------------------------------------------------------------------------
335 -- General path searching
336
337 searchPathExts
338 :: [FilePath] -- paths to search
339 -> Module -- module name
340 -> [ (
341 FileExt, -- suffix
342 FilePath -> BaseName -> IO ModLocation -- action
343 )
344 ]
345 -> IO FindExactResult
346
347 searchPathExts paths mod exts
348 = do result <- search to_search
349 {-
350 hPutStrLn stderr (showSDoc $
351 vcat [text "Search" <+> ppr mod <+> sep (map (text. fst) exts)
352 , nest 2 (vcat (map text paths))
353 , case result of
354 Succeeded (loc, p) -> text "Found" <+> ppr loc
355 Failed fs -> text "not found"])
356 -}
357 return result
358
359 where
360 basename = moduleNameSlashes (moduleName mod)
361
362 to_search :: [(FilePath, IO ModLocation)]
363 to_search = [ (file, fn path basename)
364 | path <- paths,
365 (ext,fn) <- exts,
366 let base | path == "." = basename
367 | otherwise = path </> basename
368 file = base <.> ext
369 ]
370
371 search [] = return (NotFoundExact {fer_paths = map fst to_search
372 ,fer_pkg = Just (modulePackageKey mod)})
373
374 search ((file, mk_result) : rest) = do
375 b <- doesFileExist file
376 if b
377 then do { loc <- mk_result; return (FoundExact loc mod) }
378 else search rest
379
380 mkHomeModLocationSearched :: DynFlags -> ModuleName -> FileExt
381 -> FilePath -> BaseName -> IO ModLocation
382 mkHomeModLocationSearched dflags mod suff path basename = do
383 mkHomeModLocation2 dflags mod (path </> basename) suff
384
385 -- -----------------------------------------------------------------------------
386 -- Constructing a home module location
387
388 -- This is where we construct the ModLocation for a module in the home
389 -- package, for which we have a source file. It is called from three
390 -- places:
391 --
392 -- (a) Here in the finder, when we are searching for a module to import,
393 -- using the search path (-i option).
394 --
395 -- (b) The compilation manager, when constructing the ModLocation for
396 -- a "root" module (a source file named explicitly on the command line
397 -- or in a :load command in GHCi).
398 --
399 -- (c) The driver in one-shot mode, when we need to construct a
400 -- ModLocation for a source file named on the command-line.
401 --
402 -- Parameters are:
403 --
404 -- mod
405 -- The name of the module
406 --
407 -- path
408 -- (a): The search path component where the source file was found.
409 -- (b) and (c): "."
410 --
411 -- src_basename
412 -- (a): (moduleNameSlashes mod)
413 -- (b) and (c): The filename of the source file, minus its extension
414 --
415 -- ext
416 -- The filename extension of the source file (usually "hs" or "lhs").
417
418 mkHomeModLocation :: DynFlags -> ModuleName -> FilePath -> IO ModLocation
419 mkHomeModLocation dflags mod src_filename = do
420 let (basename,extension) = splitExtension src_filename
421 mkHomeModLocation2 dflags mod basename extension
422
423 mkHomeModLocation2 :: DynFlags
424 -> ModuleName
425 -> FilePath -- Of source module, without suffix
426 -> String -- Suffix
427 -> IO ModLocation
428 mkHomeModLocation2 dflags mod src_basename ext = do
429 let mod_basename = moduleNameSlashes mod
430
431 obj_fn = mkObjPath dflags src_basename mod_basename
432 hi_fn = mkHiPath dflags src_basename mod_basename
433
434 return (ModLocation{ ml_hs_file = Just (src_basename <.> ext),
435 ml_hi_file = hi_fn,
436 ml_obj_file = obj_fn })
437
438 mkHiOnlyModLocation :: DynFlags -> Suffix -> FilePath -> String
439 -> IO ModLocation
440 mkHiOnlyModLocation dflags hisuf path basename
441 = do let full_basename = path </> basename
442 obj_fn = mkObjPath dflags full_basename basename
443 return ModLocation{ ml_hs_file = Nothing,
444 ml_hi_file = full_basename <.> hisuf,
445 -- Remove the .hi-boot suffix from
446 -- hi_file, if it had one. We always
447 -- want the name of the real .hi file
448 -- in the ml_hi_file field.
449 ml_obj_file = obj_fn
450 }
451
452 -- | Constructs the filename of a .o file for a given source file.
453 -- Does /not/ check whether the .o file exists
454 mkObjPath
455 :: DynFlags
456 -> FilePath -- the filename of the source file, minus the extension
457 -> String -- the module name with dots replaced by slashes
458 -> FilePath
459 mkObjPath dflags basename mod_basename = obj_basename <.> osuf
460 where
461 odir = objectDir dflags
462 osuf = objectSuf dflags
463
464 obj_basename | Just dir <- odir = dir </> mod_basename
465 | otherwise = basename
466
467
468 -- | Constructs the filename of a .hi file for a given source file.
469 -- Does /not/ check whether the .hi file exists
470 mkHiPath
471 :: DynFlags
472 -> FilePath -- the filename of the source file, minus the extension
473 -> String -- the module name with dots replaced by slashes
474 -> FilePath
475 mkHiPath dflags basename mod_basename = hi_basename <.> hisuf
476 where
477 hidir = hiDir dflags
478 hisuf = hiSuf dflags
479
480 hi_basename | Just dir <- hidir = dir </> mod_basename
481 | otherwise = basename
482
483
484
485 -- -----------------------------------------------------------------------------
486 -- Filenames of the stub files
487
488 -- We don't have to store these in ModLocations, because they can be derived
489 -- from other available information, and they're only rarely needed.
490
491 mkStubPaths
492 :: DynFlags
493 -> ModuleName
494 -> ModLocation
495 -> FilePath
496
497 mkStubPaths dflags mod location
498 = let
499 stubdir = stubDir dflags
500
501 mod_basename = moduleNameSlashes mod
502 src_basename = dropExtension $ expectJust "mkStubPaths"
503 (ml_hs_file location)
504
505 stub_basename0
506 | Just dir <- stubdir = dir </> mod_basename
507 | otherwise = src_basename
508
509 stub_basename = stub_basename0 ++ "_stub"
510 in
511 stub_basename <.> "h"
512
513 -- -----------------------------------------------------------------------------
514 -- findLinkable isn't related to the other stuff in here,
515 -- but there's no other obvious place for it
516
517 findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
518 findObjectLinkableMaybe mod locn
519 = do let obj_fn = ml_obj_file locn
520 maybe_obj_time <- modificationTimeIfExists obj_fn
521 case maybe_obj_time of
522 Nothing -> return Nothing
523 Just obj_time -> liftM Just (findObjectLinkable mod obj_fn obj_time)
524
525 -- Make an object linkable when we know the object file exists, and we know
526 -- its modification time.
527 findObjectLinkable :: Module -> FilePath -> UTCTime -> IO Linkable
528 findObjectLinkable mod obj_fn obj_time = return (LM obj_time mod [DotO obj_fn])
529 -- We used to look for _stub.o files here, but that was a bug (#706)
530 -- Now GHC merges the stub.o into the main .o (#3687)
531
532 -- -----------------------------------------------------------------------------
533 -- Error messages
534
535 cannotFindModule :: DynFlags -> ModuleName -> FindResult -> SDoc
536 cannotFindModule = cantFindErr (sLit "Could not find module")
537 (sLit "Ambiguous module name")
538
539 cannotFindInterface :: DynFlags -> ModuleName -> FindResult -> SDoc
540 cannotFindInterface = cantFindErr (sLit "Failed to load interface for")
541 (sLit "Ambiguous interface for")
542
543 cantFindErr :: LitString -> LitString -> DynFlags -> ModuleName -> FindResult
544 -> SDoc
545 cantFindErr _ multiple_found _ mod_name (FoundMultiple mods)
546 | Just pkgs <- unambiguousPackages
547 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
548 sep [ptext (sLit "it was found in multiple packages:"),
549 hsep (map ppr pkgs) ]
550 )
551 | otherwise
552 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
553 vcat (map pprMod mods)
554 )
555 where
556 unambiguousPackages = foldl' unambiguousPackage (Just []) mods
557 unambiguousPackage (Just xs) (m, ModOrigin (Just _) _ _ _)
558 = Just (modulePackageKey m : xs)
559 unambiguousPackage _ _ = Nothing
560
561 pprMod (m, o) = ptext (sLit "it is bound as") <+> ppr m <+>
562 ptext (sLit "by") <+> pprOrigin m o
563 pprOrigin _ ModHidden = panic "cantFindErr: bound by mod hidden"
564 pprOrigin m (ModOrigin e res _ f) = sep $ punctuate comma (
565 if e == Just True
566 then [ptext (sLit "package") <+> ppr (modulePackageKey m)]
567 else [] ++
568 map ((ptext (sLit "a reexport in package") <+>)
569 .ppr.packageConfigId) res ++
570 if f then [ptext (sLit "a package flag")] else []
571 )
572
573 cantFindErr cannot_find _ dflags mod_name find_result
574 = ptext cannot_find <+> quotes (ppr mod_name)
575 $$ more_info
576 where
577 more_info
578 = case find_result of
579 NoPackage pkg
580 -> ptext (sLit "no package matching") <+> quotes (ppr pkg) <+>
581 ptext (sLit "was found")
582
583 NotFound { fr_paths = files, fr_pkg = mb_pkg
584 , fr_mods_hidden = mod_hiddens, fr_pkgs_hidden = pkg_hiddens
585 , fr_suggestions = suggest }
586 | Just pkg <- mb_pkg, pkg /= thisPackage dflags
587 -> not_found_in_package pkg files
588
589 | not (null suggest)
590 -> pp_suggestions suggest $$ tried_these files
591
592 | null files && null mod_hiddens && null pkg_hiddens
593 -> ptext (sLit "It is not a module in the current program, or in any known package.")
594
595 | otherwise
596 -> vcat (map pkg_hidden pkg_hiddens) $$
597 vcat (map mod_hidden mod_hiddens) $$
598 tried_these files
599
600 _ -> pprPanic "cantFindErr"
601 (ptext cannot_find <+> quotes (ppr mod_name))
602
603 build_tag = buildTag dflags
604
605 not_found_in_package pkg files
606 | build_tag /= ""
607 = let
608 build = if build_tag == "p" then "profiling"
609 else "\"" ++ build_tag ++ "\""
610 in
611 ptext (sLit "Perhaps you haven't installed the ") <> text build <>
612 ptext (sLit " libraries for package ") <> quotes (ppr pkg) <> char '?' $$
613 tried_these files
614
615 | otherwise
616 = ptext (sLit "There are files missing in the ") <> quotes (ppr pkg) <>
617 ptext (sLit " package,") $$
618 ptext (sLit "try running 'ghc-pkg check'.") $$
619 tried_these files
620
621 tried_these files
622 | null files = Outputable.empty
623 | verbosity dflags < 3 =
624 ptext (sLit "Use -v to see a list of the files searched for.")
625 | otherwise =
626 hang (ptext (sLit "Locations searched:")) 2 $ vcat (map text files)
627
628 pkg_hidden :: PackageKey -> SDoc
629 pkg_hidden pkgid =
630 ptext (sLit "It is a member of the hidden package")
631 <+> quotes (ppr pkgid)
632 --FIXME: we don't really want to show the package key here we should
633 -- show the source package id or installed package id if it's ambiguous
634 <> dot $$ cabal_pkg_hidden_hint pkgid
635 cabal_pkg_hidden_hint pkgid
636 | gopt Opt_BuildingCabalPackage dflags
637 = let pkg = expectJust "pkg_hidden" (lookupPackage dflags pkgid)
638 in ptext (sLit "Perhaps you need to add") <+>
639 quotes (ppr (packageName pkg)) <+>
640 ptext (sLit "to the build-depends in your .cabal file.")
641 | otherwise = Outputable.empty
642
643 mod_hidden pkg =
644 ptext (sLit "it is a hidden module in the package") <+> quotes (ppr pkg)
645
646 pp_suggestions :: [ModuleSuggestion] -> SDoc
647 pp_suggestions sugs
648 | null sugs = Outputable.empty
649 | otherwise = hang (ptext (sLit "Perhaps you meant"))
650 2 (vcat (map pp_sugg sugs))
651
652 -- NB: Prefer the *original* location, and then reexports, and then
653 -- package flags when making suggestions. ToDo: if the original package
654 -- also has a reexport, prefer that one
655 pp_sugg (SuggestVisible m mod o) = ppr m <+> provenance o
656 where provenance ModHidden = Outputable.empty
657 provenance (ModOrigin{ fromOrigPackage = e,
658 fromExposedReexport = res,
659 fromPackageFlag = f })
660 | Just True <- e
661 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
662 | f && moduleName mod == m
663 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
664 | (pkg:_) <- res
665 = parens (ptext (sLit "from") <+> ppr (packageConfigId pkg)
666 <> comma <+> ptext (sLit "reexporting") <+> ppr mod)
667 | f
668 = parens (ptext (sLit "defined via package flags to be")
669 <+> ppr mod)
670 | otherwise = Outputable.empty
671 pp_sugg (SuggestHidden m mod o) = ppr m <+> provenance o
672 where provenance ModHidden = Outputable.empty
673 provenance (ModOrigin{ fromOrigPackage = e,
674 fromHiddenReexport = rhs })
675 | Just False <- e
676 = parens (ptext (sLit "needs flag -package-key")
677 <+> ppr (modulePackageKey mod))
678 | (pkg:_) <- rhs
679 = parens (ptext (sLit "needs flag -package-key")
680 <+> ppr (packageConfigId pkg))
681 | otherwise = Outputable.empty