never executed always true always false
1
2 -- | The ModuleName type
3 module GHC.Unit.Module.Name
4 ( ModuleName
5 , pprModuleName
6 , moduleNameFS
7 , moduleNameString
8 , moduleNameSlashes, moduleNameColons
9 , mkModuleName
10 , mkModuleNameFS
11 , stableModuleNameCmp
12 , parseModuleName
13 )
14 where
15
16 import GHC.Prelude
17
18 import GHC.Utils.Outputable
19 import GHC.Types.Unique
20 import GHC.Data.FastString
21 import GHC.Utils.Binary
22 import GHC.Utils.Misc
23
24 import Control.DeepSeq
25 import Data.Data
26 import System.FilePath
27
28 import qualified Text.ParserCombinators.ReadP as Parse
29 import Text.ParserCombinators.ReadP (ReadP)
30 import Data.Char (isAlphaNum)
31
32 -- | A ModuleName is essentially a simple string, e.g. @Data.List@.
33 newtype ModuleName = ModuleName FastString deriving Show
34
35 instance Uniquable ModuleName where
36 getUnique (ModuleName nm) = getUnique nm
37
38 instance Eq ModuleName where
39 nm1 == nm2 = getUnique nm1 == getUnique nm2
40
41 instance Ord ModuleName where
42 nm1 `compare` nm2 = stableModuleNameCmp nm1 nm2
43
44 instance Outputable ModuleName where
45 ppr = pprModuleName
46
47 instance Binary ModuleName where
48 put_ bh (ModuleName fs) = put_ bh fs
49 get bh = do fs <- get bh; return (ModuleName fs)
50
51 instance Data ModuleName where
52 -- don't traverse?
53 toConstr _ = abstractConstr "ModuleName"
54 gunfold _ _ = error "gunfold"
55 dataTypeOf _ = mkNoRepType "ModuleName"
56
57 instance NFData ModuleName where
58 rnf x = x `seq` ()
59
60 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
61 -- ^ Compares module names lexically, rather than by their 'Unique's
62 stableModuleNameCmp n1 n2 = moduleNameFS n1 `lexicalCompareFS` moduleNameFS n2
63
64 pprModuleName :: ModuleName -> SDoc
65 pprModuleName (ModuleName nm) =
66 getPprStyle $ \ sty ->
67 if codeStyle sty
68 then ztext (zEncodeFS nm)
69 else ftext nm
70
71 moduleNameFS :: ModuleName -> FastString
72 moduleNameFS (ModuleName mod) = mod
73
74 moduleNameString :: ModuleName -> String
75 moduleNameString (ModuleName mod) = unpackFS mod
76
77 mkModuleName :: String -> ModuleName
78 mkModuleName s = ModuleName (mkFastString s)
79
80 mkModuleNameFS :: FastString -> ModuleName
81 mkModuleNameFS s = ModuleName s
82
83 -- |Returns the string version of the module name, with dots replaced by slashes.
84 --
85 moduleNameSlashes :: ModuleName -> String
86 moduleNameSlashes = dots_to_slashes . moduleNameString
87 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
88
89 -- |Returns the string version of the module name, with dots replaced by colons.
90 --
91 moduleNameColons :: ModuleName -> String
92 moduleNameColons = dots_to_colons . moduleNameString
93 where dots_to_colons = map (\c -> if c == '.' then ':' else c)
94
95 parseModuleName :: ReadP ModuleName
96 parseModuleName = fmap mkModuleName
97 $ Parse.munch1 (\c -> isAlphaNum c || c `elem` "_.")
98