PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/ghc-7.0.4/compiler/basicTypes/Module.lhs

http://picorec.googlecode.com/
Haskell | 463 lines | 299 code | 96 blank | 68 comment | 12 complexity | b21c4997c69b425c2003e0fb16fc4b1c MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  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,
  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. -- * The Module type
  39. Module,
  40. modulePackageId, moduleName,
  41. pprModule,
  42. mkModule,
  43. stableModuleCmp,
  44. -- * The ModuleLocation type
  45. ModLocation(..),
  46. addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
  47. -- * Module mappings
  48. ModuleEnv,
  49. elemModuleEnv, extendModuleEnv, extendModuleEnvList,
  50. extendModuleEnvList_C, plusModuleEnv_C,
  51. delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
  52. lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
  53. moduleEnvKeys, moduleEnvElts, moduleEnvToList,
  54. unitModuleEnv, isEmptyModuleEnv,
  55. foldModuleEnv, extendModuleEnvWith, filterModuleEnv,
  56. -- * ModuleName mappings
  57. ModuleNameEnv,
  58. -- * Sets of Modules
  59. ModuleSet,
  60. emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
  61. ) where
  62. #include "Typeable.h"
  63. import Config
  64. import Outputable
  65. import qualified Pretty
  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. instance Uniquable ModuleName where
  134. getUnique (ModuleName nm) = getUnique nm
  135. instance Eq ModuleName where
  136. nm1 == nm2 = getUnique nm1 == getUnique nm2
  137. -- Warning: gives an ordering relation based on the uniques of the
  138. -- FastStrings which are the (encoded) module names. This is _not_
  139. -- a lexicographical ordering.
  140. instance Ord ModuleName where
  141. nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
  142. instance Outputable ModuleName where
  143. ppr = pprModuleName
  144. instance Binary ModuleName where
  145. put_ bh (ModuleName fs) = put_ bh fs
  146. get bh = do fs <- get bh; return (ModuleName fs)
  147. INSTANCE_TYPEABLE0(ModuleName,moduleNameTc,"ModuleName")
  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 ftext (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. moduleNameSlashes :: ModuleName -> String
  172. moduleNameSlashes = dots_to_slashes . moduleNameString
  173. where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
  174. \end{code}
  175. %************************************************************************
  176. %* *
  177. \subsection{A fully qualified module}
  178. %* *
  179. %************************************************************************
  180. \begin{code}
  181. -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
  182. data Module = Module {
  183. modulePackageId :: !PackageId, -- pkg-1.0
  184. moduleName :: !ModuleName -- A.B.C
  185. }
  186. deriving (Eq, Ord)
  187. instance Uniquable Module where
  188. getUnique (Module p n) = getUnique (packageIdFS p `appendFS` moduleNameFS n)
  189. instance Outputable Module where
  190. ppr = pprModule
  191. instance Binary Module where
  192. put_ bh (Module p n) = put_ bh p >> put_ bh n
  193. get bh = do p <- get bh; n <- get bh; return (Module p n)
  194. INSTANCE_TYPEABLE0(Module,moduleTc,"Module")
  195. instance Data Module where
  196. -- don't traverse?
  197. toConstr _ = abstractConstr "Module"
  198. gunfold _ _ = error "gunfold"
  199. dataTypeOf _ = mkNoRepType "Module"
  200. -- | This gives a stable ordering, as opposed to the Ord instance which
  201. -- gives an ordering based on the 'Unique's of the components, which may
  202. -- not be stable from run to run of the compiler.
  203. stableModuleCmp :: Module -> Module -> Ordering
  204. stableModuleCmp (Module p1 n1) (Module p2 n2)
  205. = (p1 `stablePackageIdCmp` p2) `thenCmp`
  206. (n1 `stableModuleNameCmp` n2)
  207. mkModule :: PackageId -> ModuleName -> Module
  208. mkModule = Module
  209. pprModule :: Module -> SDoc
  210. pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
  211. pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
  212. pprPackagePrefix p mod = getPprStyle doc
  213. where
  214. doc sty
  215. | codeStyle sty =
  216. if p == mainPackageId
  217. then empty -- never qualify the main package in code
  218. else ftext (zEncodeFS (packageIdFS p)) <> char '_'
  219. | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
  220. -- the PrintUnqualified tells us which modules have to
  221. -- be qualified with package names
  222. | otherwise = empty
  223. \end{code}
  224. %************************************************************************
  225. %* *
  226. \subsection{PackageId}
  227. %* *
  228. %************************************************************************
  229. \begin{code}
  230. -- | Essentially just a string identifying a package, including the version: e.g. parsec-1.0
  231. newtype PackageId = PId FastString deriving( Eq )
  232. -- here to avoid module loops with PackageConfig
  233. instance Uniquable PackageId where
  234. getUnique pid = getUnique (packageIdFS pid)
  235. -- Note: *not* a stable lexicographic ordering, a faster unique-based
  236. -- ordering.
  237. instance Ord PackageId where
  238. nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
  239. INSTANCE_TYPEABLE0(PackageId,packageIdTc,"PackageId")
  240. instance Data PackageId where
  241. -- don't traverse?
  242. toConstr _ = abstractConstr "PackageId"
  243. gunfold _ _ = error "gunfold"
  244. dataTypeOf _ = mkNoRepType "PackageId"
  245. stablePackageIdCmp :: PackageId -> PackageId -> Ordering
  246. -- ^ Compares package ids lexically, rather than by their 'Unique's
  247. stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
  248. instance Outputable PackageId where
  249. ppr pid = text (packageIdString pid)
  250. instance Binary PackageId where
  251. put_ bh pid = put_ bh (packageIdFS pid)
  252. get bh = do { fs <- get bh; return (fsToPackageId fs) }
  253. fsToPackageId :: FastString -> PackageId
  254. fsToPackageId = PId
  255. packageIdFS :: PackageId -> FastString
  256. packageIdFS (PId fs) = fs
  257. stringToPackageId :: String -> PackageId
  258. stringToPackageId = fsToPackageId . mkFastString
  259. packageIdString :: PackageId -> String
  260. packageIdString = unpackFS . packageIdFS
  261. -- -----------------------------------------------------------------------------
  262. -- $wired_in_packages
  263. -- Certain packages are known to the compiler, in that we know about certain
  264. -- entities that reside in these packages, and the compiler needs to
  265. -- declare static Modules and Names that refer to these packages. Hence
  266. -- the wired-in packages can't include version numbers, since we don't want
  267. -- to bake the version numbers of these packages into GHC.
  268. --
  269. -- So here's the plan. Wired-in packages are still versioned as
  270. -- normal in the packages database, and you can still have multiple
  271. -- versions of them installed. However, for each invocation of GHC,
  272. -- only a single instance of each wired-in package will be recognised
  273. -- (the desired one is selected via @-package@\/@-hide-package@), and GHC
  274. -- will use the unversioned 'PackageId' below when referring to it,
  275. -- including in .hi files and object file symbols. Unselected
  276. -- versions of wired-in packages will be ignored, as will any other
  277. -- package that depends directly or indirectly on it (much as if you
  278. -- had used @-ignore-package@).
  279. -- Make sure you change 'Packages.findWiredInPackages' if you add an entry here
  280. integerPackageId, primPackageId,
  281. basePackageId, rtsPackageId,
  282. thPackageId, dphSeqPackageId, dphParPackageId,
  283. mainPackageId :: PackageId
  284. primPackageId = fsToPackageId (fsLit "ghc-prim")
  285. integerPackageId = fsToPackageId (fsLit cIntegerLibrary)
  286. basePackageId = fsToPackageId (fsLit "base")
  287. rtsPackageId = fsToPackageId (fsLit "rts")
  288. thPackageId = fsToPackageId (fsLit "template-haskell")
  289. dphSeqPackageId = fsToPackageId (fsLit "dph-seq")
  290. dphParPackageId = fsToPackageId (fsLit "dph-par")
  291. -- | This is the package Id for the current program. It is the default
  292. -- package Id if you don't specify a package name. We don't add this prefix
  293. -- to symbol names, since there can be only one main package per program.
  294. mainPackageId = fsToPackageId (fsLit "main")
  295. \end{code}
  296. %************************************************************************
  297. %* *
  298. \subsection{@ModuleEnv@s}
  299. %* *
  300. %************************************************************************
  301. \begin{code}
  302. -- | A map keyed off of 'Module's
  303. newtype ModuleEnv elt = ModuleEnv (Map Module elt)
  304. filterModuleEnv :: (Module -> a -> Bool) -> ModuleEnv a -> ModuleEnv a
  305. filterModuleEnv f (ModuleEnv e) = ModuleEnv (Map.filterWithKey f e)
  306. elemModuleEnv :: Module -> ModuleEnv a -> Bool
  307. elemModuleEnv m (ModuleEnv e) = Map.member m e
  308. extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
  309. extendModuleEnv (ModuleEnv e) m x = ModuleEnv (Map.insert m x e)
  310. extendModuleEnvWith :: (a -> a -> a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
  311. extendModuleEnvWith f (ModuleEnv e) m x = ModuleEnv (Map.insertWith f m x e)
  312. extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
  313. extendModuleEnvList (ModuleEnv e) xs = ModuleEnv (Map.insertList xs e)
  314. extendModuleEnvList_C :: (a -> a -> a) -> ModuleEnv a -> [(Module, a)]
  315. -> ModuleEnv a
  316. extendModuleEnvList_C f (ModuleEnv e) xs = ModuleEnv (Map.insertListWith f xs e)
  317. plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
  318. plusModuleEnv_C f (ModuleEnv e1) (ModuleEnv e2) = ModuleEnv (Map.unionWith f e1 e2)
  319. delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
  320. delModuleEnvList (ModuleEnv e) ms = ModuleEnv (Map.deleteList ms e)
  321. delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
  322. delModuleEnv (ModuleEnv e) m = ModuleEnv (Map.delete m e)
  323. plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
  324. plusModuleEnv (ModuleEnv e1) (ModuleEnv e2) = ModuleEnv (Map.union e1 e2)
  325. lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
  326. lookupModuleEnv (ModuleEnv e) m = Map.lookup m e
  327. lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
  328. lookupWithDefaultModuleEnv (ModuleEnv e) x m = Map.findWithDefault x m e
  329. mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
  330. mapModuleEnv f (ModuleEnv e) = ModuleEnv (Map.mapWithKey (\_ v -> f v) e)
  331. mkModuleEnv :: [(Module, a)] -> ModuleEnv a
  332. mkModuleEnv xs = ModuleEnv (Map.fromList xs)
  333. emptyModuleEnv :: ModuleEnv a
  334. emptyModuleEnv = ModuleEnv Map.empty
  335. moduleEnvKeys :: ModuleEnv a -> [Module]
  336. moduleEnvKeys (ModuleEnv e) = Map.keys e
  337. moduleEnvElts :: ModuleEnv a -> [a]
  338. moduleEnvElts (ModuleEnv e) = Map.elems e
  339. moduleEnvToList :: ModuleEnv a -> [(Module, a)]
  340. moduleEnvToList (ModuleEnv e) = Map.toList e
  341. unitModuleEnv :: Module -> a -> ModuleEnv a
  342. unitModuleEnv m x = ModuleEnv (Map.singleton m x)
  343. isEmptyModuleEnv :: ModuleEnv a -> Bool
  344. isEmptyModuleEnv (ModuleEnv e) = Map.null e
  345. foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
  346. foldModuleEnv f x (ModuleEnv e) = Map.foldRightWithKey (\_ v -> f v) x e
  347. \end{code}
  348. \begin{code}
  349. -- | A set of 'Module's
  350. type ModuleSet = Map Module ()
  351. mkModuleSet :: [Module] -> ModuleSet
  352. extendModuleSet :: ModuleSet -> Module -> ModuleSet
  353. emptyModuleSet :: ModuleSet
  354. moduleSetElts :: ModuleSet -> [Module]
  355. elemModuleSet :: Module -> ModuleSet -> Bool
  356. emptyModuleSet = Map.empty
  357. mkModuleSet ms = Map.fromList [(m,()) | m <- ms ]
  358. extendModuleSet s m = Map.insert m () s
  359. moduleSetElts = Map.keys
  360. elemModuleSet = Map.member
  361. \end{code}
  362. A ModuleName has a Unique, so we can build mappings of these using
  363. UniqFM.
  364. \begin{code}
  365. -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
  366. type ModuleNameEnv elt = UniqFM elt
  367. \end{code}