PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/basicTypes/Module.lhs

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