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