/compiler/basicTypes/Module.lhs

http://github.com/ghc/ghc · Haskell · 477 lines · 308 code · 98 blank · 71 comment · 19 complexity · 353ca2c056a0a9016ff71ceecb9f7ee8 MD5 · raw file

  1. %
  2. % (c) The University of Glasgow, 2004-2006
  3. %
  4. Module
  5. ~~~~~~~~~~
  6. Simply the name of a module, represented as a FastString.
  7. These are Uniquable, hence we can build Maps with Modules as
  8. the keys.
  9. \begin{code}
  10. module Module
  11. (
  12. -- * The ModuleName type
  13. ModuleName,
  14. pprModuleName,
  15. moduleNameFS,
  16. moduleNameString,
  17. moduleNameSlashes, moduleNameColons,
  18. mkModuleName,
  19. mkModuleNameFS,
  20. stableModuleNameCmp,
  21. -- * The PackageId type
  22. PackageId,
  23. fsToPackageId,
  24. packageIdFS,
  25. stringToPackageId,
  26. packageIdString,
  27. stablePackageIdCmp,
  28. -- * Wired-in PackageIds
  29. -- $wired_in_packages
  30. primPackageId,
  31. integerPackageId,
  32. basePackageId,
  33. rtsPackageId,
  34. thPackageId,
  35. dphSeqPackageId,
  36. dphParPackageId,
  37. mainPackageId,
  38. thisGhcPackageId,
  39. -- * The Module type
  40. Module,
  41. modulePackageId, moduleName,
  42. pprModule,
  43. mkModule,
  44. stableModuleCmp,
  45. HasModule(..),
  46. ContainsModule(..),
  47. -- * The ModuleLocation type
  48. ModLocation(..),
  49. addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
  50. -- * Module mappings
  51. ModuleEnv,
  52. elemModuleEnv, extendModuleEnv, extendModuleEnvList,
  53. extendModuleEnvList_C, plusModuleEnv_C,
  54. delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
  55. lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
  56. moduleEnvKeys, moduleEnvElts, moduleEnvToList,
  57. unitModuleEnv, isEmptyModuleEnv,
  58. foldModuleEnv, extendModuleEnvWith, filterModuleEnv,
  59. -- * ModuleName mappings
  60. ModuleNameEnv,
  61. -- * Sets of Modules
  62. ModuleSet,
  63. emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
  64. ) where
  65. #include "Typeable.h"
  66. import Config
  67. import Outputable
  68. import Unique
  69. import UniqFM
  70. import FastString
  71. import Binary
  72. import Util
  73. import Data.Data
  74. import Data.Map (Map)
  75. import qualified Data.Map as Map
  76. import qualified FiniteMap as Map
  77. import System.FilePath
  78. \end{code}
  79. %************************************************************************
  80. %* *
  81. \subsection{Module locations}
  82. %* *
  83. %************************************************************************
  84. \begin{code}
  85. -- | Where a module lives on the file system: the actual locations
  86. -- of the .hs, .hi and .o files, if we have them
  87. data ModLocation
  88. = ModLocation {
  89. ml_hs_file :: Maybe FilePath,
  90. -- The source file, if we have one. Package modules
  91. -- probably don't have source files.
  92. ml_hi_file :: FilePath,
  93. -- Where the .hi file is, whether or not it exists
  94. -- yet. Always of form foo.hi, even if there is an
  95. -- hi-boot file (we add the -boot suffix later)
  96. ml_obj_file :: FilePath
  97. -- Where the .o file is, whether or not it exists yet.
  98. -- (might not exist either because the module hasn't
  99. -- been compiled yet, or because it is part of a
  100. -- package with a .a file)
  101. } deriving Show
  102. instance Outputable ModLocation where
  103. ppr = text . show
  104. \end{code}
  105. For a module in another package, the hs_file and obj_file
  106. components of ModLocation are undefined.
  107. The locations specified by a ModLocation may or may not
  108. correspond to actual files yet: for example, even if the object
  109. file doesn't exist, the ModLocation still contains the path to
  110. where the object file will reside if/when it is created.
  111. \begin{code}
  112. addBootSuffix :: FilePath -> FilePath
  113. -- ^ Add the @-boot@ suffix to .hs, .hi and .o files
  114. addBootSuffix path = path ++ "-boot"
  115. addBootSuffix_maybe :: Bool -> FilePath -> FilePath
  116. -- ^ Add the @-boot@ suffix if the @Bool@ argument is @True@
  117. addBootSuffix_maybe is_boot path
  118. | is_boot = addBootSuffix path
  119. | otherwise = path
  120. addBootSuffixLocn :: ModLocation -> ModLocation
  121. -- ^ Add the @-boot@ suffix to all file paths associated with the module
  122. addBootSuffixLocn locn
  123. = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
  124. , ml_hi_file = addBootSuffix (ml_hi_file locn)
  125. , ml_obj_file = addBootSuffix (ml_obj_file locn) }
  126. \end{code}
  127. %************************************************************************
  128. %* *
  129. \subsection{The name of a module}
  130. %* *
  131. %************************************************************************
  132. \begin{code}
  133. -- | A ModuleName is essentially a simple string, e.g. @Data.List@.
  134. newtype ModuleName = ModuleName FastString
  135. deriving Typeable
  136. instance Uniquable ModuleName where
  137. getUnique (ModuleName nm) = getUnique nm
  138. instance Eq ModuleName where
  139. nm1 == nm2 = getUnique nm1 == getUnique nm2
  140. -- Warning: gives an ordering relation based on the uniques of the
  141. -- FastStrings which are the (encoded) module names. This is _not_
  142. -- a lexicographical ordering.
  143. instance Ord ModuleName where
  144. nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
  145. instance Outputable ModuleName where
  146. ppr = pprModuleName
  147. instance Binary ModuleName where
  148. put_ bh (ModuleName fs) = put_ bh fs
  149. get bh = do fs <- get bh; return (ModuleName fs)
  150. instance Data ModuleName where
  151. -- don't traverse?
  152. toConstr _ = abstractConstr "ModuleName"
  153. gunfold _ _ = error "gunfold"
  154. dataTypeOf _ = mkNoRepType "ModuleName"
  155. stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
  156. -- ^ Compares module names lexically, rather than by their 'Unique's
  157. stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
  158. pprModuleName :: ModuleName -> SDoc
  159. pprModuleName (ModuleName nm) =
  160. getPprStyle $ \ sty ->
  161. if codeStyle sty
  162. then ztext (zEncodeFS nm)
  163. else ftext nm
  164. moduleNameFS :: ModuleName -> FastString
  165. moduleNameFS (ModuleName mod) = mod
  166. moduleNameString :: ModuleName -> String
  167. moduleNameString (ModuleName mod) = unpackFS mod
  168. mkModuleName :: String -> ModuleName
  169. mkModuleName s = ModuleName (mkFastString s)
  170. mkModuleNameFS :: FastString -> ModuleName
  171. mkModuleNameFS s = ModuleName s
  172. -- |Returns the string version of the module name, with dots replaced by slashes.
  173. --
  174. moduleNameSlashes :: ModuleName -> String
  175. moduleNameSlashes = dots_to_slashes . moduleNameString
  176. where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
  177. -- |Returns the string version of the module name, with dots replaced by underscores.
  178. --
  179. moduleNameColons :: ModuleName -> String
  180. moduleNameColons = dots_to_colons . moduleNameString
  181. where dots_to_colons = map (\c -> if c == '.' then ':' else c)
  182. \end{code}
  183. %************************************************************************
  184. %* *
  185. \subsection{A fully qualified module}
  186. %* *
  187. %************************************************************************
  188. \begin{code}
  189. -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
  190. data Module = Module {
  191. modulePackageId :: !PackageId, -- pkg-1.0
  192. moduleName :: !ModuleName -- A.B.C
  193. }
  194. deriving (Eq, Ord, Typeable)
  195. instance Uniquable Module where
  196. getUnique (Module p n) = getUnique (packageIdFS p `appendFS` moduleNameFS n)
  197. instance Outputable Module where
  198. ppr = pprModule
  199. instance Binary Module where
  200. put_ bh (Module p n) = put_ bh p >> put_ bh n
  201. get bh = do p <- get bh; n <- get bh; return (Module p n)
  202. instance Data Module where
  203. -- don't traverse?
  204. toConstr _ = abstractConstr "Module"
  205. gunfold _ _ = error "gunfold"
  206. dataTypeOf _ = mkNoRepType "Module"
  207. -- | This gives a stable ordering, as opposed to the Ord instance which
  208. -- gives an ordering based on the 'Unique's of the components, which may
  209. -- not be stable from run to run of the compiler.
  210. stableModuleCmp :: Module -> Module -> Ordering
  211. stableModuleCmp (Module p1 n1) (Module p2 n2)
  212. = (p1 `stablePackageIdCmp` p2) `thenCmp`
  213. (n1 `stableModuleNameCmp` n2)
  214. mkModule :: PackageId -> ModuleName -> Module
  215. mkModule = Module
  216. pprModule :: Module -> SDoc
  217. pprModule mod@(Module p n) =
  218. pprPackagePrefix p mod <> pprModuleName n
  219. pprPackagePrefix :: PackageId -> Module -> SDoc
  220. pprPackagePrefix p mod = getPprStyle doc
  221. where
  222. doc sty
  223. | codeStyle sty =
  224. if p == mainPackageId
  225. then empty -- never qualify the main package in code
  226. else ztext (zEncodeFS (packageIdFS p)) <> char '_'
  227. | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
  228. -- the PrintUnqualified tells us which modules have to
  229. -- be qualified with package names
  230. | otherwise = empty
  231. class ContainsModule t where
  232. extractModule :: t -> Module
  233. class HasModule m where
  234. getModule :: m Module
  235. \end{code}
  236. %************************************************************************
  237. %* *
  238. \subsection{PackageId}
  239. %* *
  240. %************************************************************************
  241. \begin{code}
  242. -- | Essentially just a string identifying a package, including the version: e.g. parsec-1.0
  243. newtype PackageId = PId FastString deriving( Eq, Typeable )
  244. -- here to avoid module loops with PackageConfig
  245. instance Uniquable PackageId where
  246. getUnique pid = getUnique (packageIdFS pid)
  247. -- Note: *not* a stable lexicographic ordering, a faster unique-based
  248. -- ordering.
  249. instance Ord PackageId where
  250. nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
  251. instance Data PackageId where
  252. -- don't traverse?
  253. toConstr _ = abstractConstr "PackageId"
  254. gunfold _ _ = error "gunfold"
  255. dataTypeOf _ = mkNoRepType "PackageId"
  256. stablePackageIdCmp :: PackageId -> PackageId -> Ordering
  257. -- ^ Compares package ids lexically, rather than by their 'Unique's
  258. stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
  259. instance Outputable PackageId where
  260. ppr pid = text (packageIdString pid)
  261. instance Binary PackageId where
  262. put_ bh pid = put_ bh (packageIdFS pid)
  263. get bh = do { fs <- get bh; return (fsToPackageId fs) }
  264. fsToPackageId :: FastString -> PackageId
  265. fsToPackageId = PId
  266. packageIdFS :: PackageId -> FastString
  267. packageIdFS (PId fs) = fs
  268. stringToPackageId :: String -> PackageId
  269. stringToPackageId = fsToPackageId . mkFastString
  270. packageIdString :: PackageId -> String
  271. packageIdString = unpackFS . packageIdFS
  272. -- -----------------------------------------------------------------------------
  273. -- $wired_in_packages
  274. -- Certain packages are known to the compiler, in that we know about certain
  275. -- entities that reside in these packages, and the compiler needs to
  276. -- declare static Modules and Names that refer to these packages. Hence
  277. -- the wired-in packages can't include version numbers, since we don't want
  278. -- to bake the version numbers of these packages into GHC.
  279. --
  280. -- So here's the plan. Wired-in packages are still versioned as
  281. -- normal in the packages database, and you can still have multiple
  282. -- versions of them installed. However, for each invocation of GHC,
  283. -- only a single instance of each wired-in package will be recognised
  284. -- (the desired one is selected via @-package@\/@-hide-package@), and GHC
  285. -- will use the unversioned 'PackageId' below when referring to it,
  286. -- including in .hi files and object file symbols. Unselected
  287. -- versions of wired-in packages will be ignored, as will any other
  288. -- package that depends directly or indirectly on it (much as if you
  289. -- had used @-ignore-package@).
  290. -- Make sure you change 'Packages.findWiredInPackages' if you add an entry here
  291. integerPackageId, primPackageId,
  292. basePackageId, rtsPackageId,
  293. thPackageId, dphSeqPackageId, dphParPackageId,
  294. mainPackageId, thisGhcPackageId :: PackageId
  295. primPackageId = fsToPackageId (fsLit "ghc-prim")
  296. integerPackageId = fsToPackageId (fsLit cIntegerLibrary)
  297. basePackageId = fsToPackageId (fsLit "base")
  298. rtsPackageId = fsToPackageId (fsLit "rts")
  299. thPackageId = fsToPackageId (fsLit "template-haskell")
  300. dphSeqPackageId = fsToPackageId (fsLit "dph-seq")
  301. dphParPackageId = fsToPackageId (fsLit "dph-par")
  302. thisGhcPackageId = fsToPackageId (fsLit ("ghc-" ++ cProjectVersion))
  303. -- | This is the package Id for the current program. It is the default
  304. -- package Id if you don't specify a package name. We don't add this prefix
  305. -- to symbol names, since there can be only one main package per program.
  306. mainPackageId = fsToPackageId (fsLit "main")
  307. \end{code}
  308. %************************************************************************
  309. %* *
  310. \subsection{@ModuleEnv@s}
  311. %* *
  312. %************************************************************************
  313. \begin{code}
  314. -- | A map keyed off of 'Module's
  315. newtype ModuleEnv elt = ModuleEnv (Map Module elt)
  316. filterModuleEnv :: (Module -> a -> Bool) -> ModuleEnv a -> ModuleEnv a
  317. filterModuleEnv f (ModuleEnv e) = ModuleEnv (Map.filterWithKey f e)
  318. elemModuleEnv :: Module -> ModuleEnv a -> Bool
  319. elemModuleEnv m (ModuleEnv e) = Map.member m e
  320. extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
  321. extendModuleEnv (ModuleEnv e) m x = ModuleEnv (Map.insert m x e)
  322. extendModuleEnvWith :: (a -> a -> a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
  323. extendModuleEnvWith f (ModuleEnv e) m x = ModuleEnv (Map.insertWith f m x e)
  324. extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
  325. extendModuleEnvList (ModuleEnv e) xs = ModuleEnv (Map.insertList xs e)
  326. extendModuleEnvList_C :: (a -> a -> a) -> ModuleEnv a -> [(Module, a)]
  327. -> ModuleEnv a
  328. extendModuleEnvList_C f (ModuleEnv e) xs = ModuleEnv (Map.insertListWith f xs e)
  329. plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
  330. plusModuleEnv_C f (ModuleEnv e1) (ModuleEnv e2) = ModuleEnv (Map.unionWith f e1 e2)
  331. delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
  332. delModuleEnvList (ModuleEnv e) ms = ModuleEnv (Map.deleteList ms e)
  333. delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
  334. delModuleEnv (ModuleEnv e) m = ModuleEnv (Map.delete m e)
  335. plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
  336. plusModuleEnv (ModuleEnv e1) (ModuleEnv e2) = ModuleEnv (Map.union e1 e2)
  337. lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
  338. lookupModuleEnv (ModuleEnv e) m = Map.lookup m e
  339. lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
  340. lookupWithDefaultModuleEnv (ModuleEnv e) x m = Map.findWithDefault x m e
  341. mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
  342. mapModuleEnv f (ModuleEnv e) = ModuleEnv (Map.mapWithKey (\_ v -> f v) e)
  343. mkModuleEnv :: [(Module, a)] -> ModuleEnv a
  344. mkModuleEnv xs = ModuleEnv (Map.fromList xs)
  345. emptyModuleEnv :: ModuleEnv a
  346. emptyModuleEnv = ModuleEnv Map.empty
  347. moduleEnvKeys :: ModuleEnv a -> [Module]
  348. moduleEnvKeys (ModuleEnv e) = Map.keys e
  349. moduleEnvElts :: ModuleEnv a -> [a]
  350. moduleEnvElts (ModuleEnv e) = Map.elems e
  351. moduleEnvToList :: ModuleEnv a -> [(Module, a)]
  352. moduleEnvToList (ModuleEnv e) = Map.toList e
  353. unitModuleEnv :: Module -> a -> ModuleEnv a
  354. unitModuleEnv m x = ModuleEnv (Map.singleton m x)
  355. isEmptyModuleEnv :: ModuleEnv a -> Bool
  356. isEmptyModuleEnv (ModuleEnv e) = Map.null e
  357. foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
  358. foldModuleEnv f x (ModuleEnv e) = Map.foldRightWithKey (\_ v -> f v) x e
  359. \end{code}
  360. \begin{code}
  361. -- | A set of 'Module's
  362. type ModuleSet = Map Module ()
  363. mkModuleSet :: [Module] -> ModuleSet
  364. extendModuleSet :: ModuleSet -> Module -> ModuleSet
  365. emptyModuleSet :: ModuleSet
  366. moduleSetElts :: ModuleSet -> [Module]
  367. elemModuleSet :: Module -> ModuleSet -> Bool
  368. emptyModuleSet = Map.empty
  369. mkModuleSet ms = Map.fromList [(m,()) | m <- ms ]
  370. extendModuleSet s m = Map.insert m () s
  371. moduleSetElts = Map.keys
  372. elemModuleSet = Map.member
  373. \end{code}
  374. A ModuleName has a Unique, so we can build mappings of these using
  375. UniqFM.
  376. \begin{code}
  377. -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
  378. type ModuleNameEnv elt = UniqFM elt
  379. \end{code}