-- | The ModuleName type
module GHC.Unit.Module.Name
    ( ModuleName
    , pprModuleName
    , moduleNameFS
    , moduleNameString
    , moduleNameSlashes, moduleNameColons
    , mkModuleName
    , mkModuleNameFS
    , stableModuleNameCmp
    , parseModuleName
    )
where

import GHC.Prelude

import GHC.Utils.Outputable
import GHC.Types.Unique
import GHC.Data.FastString
import GHC.Utils.Binary
import GHC.Utils.Misc

import Control.DeepSeq
import Data.Data
import System.FilePath

import qualified Text.ParserCombinators.ReadP as Parse
import Text.ParserCombinators.ReadP (ReadP)
import Data.Char (isAlphaNum)

-- | A ModuleName is essentially a simple string, e.g. @Data.List@.
newtype ModuleName = ModuleName FastString deriving Int -> ModuleName -> ShowS
[ModuleName] -> ShowS
ModuleName -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ModuleName] -> ShowS
$cshowList :: [ModuleName] -> ShowS
show :: ModuleName -> String
$cshow :: ModuleName -> String
showsPrec :: Int -> ModuleName -> ShowS
$cshowsPrec :: Int -> ModuleName -> ShowS
Show

instance Uniquable ModuleName where
  getUnique :: ModuleName -> Unique
getUnique (ModuleName FastString
nm) = forall a. Uniquable a => a -> Unique
getUnique FastString
nm

instance Eq ModuleName where
  ModuleName
nm1 == :: ModuleName -> ModuleName -> Bool
== ModuleName
nm2 = forall a. Uniquable a => a -> Unique
getUnique ModuleName
nm1 forall a. Eq a => a -> a -> Bool
== forall a. Uniquable a => a -> Unique
getUnique ModuleName
nm2

instance Ord ModuleName where
  ModuleName
nm1 compare :: ModuleName -> ModuleName -> Ordering
`compare` ModuleName
nm2 = ModuleName -> ModuleName -> Ordering
stableModuleNameCmp ModuleName
nm1 ModuleName
nm2

instance Outputable ModuleName where
  ppr :: ModuleName -> SDoc
ppr = ModuleName -> SDoc
pprModuleName

instance Binary ModuleName where
  put_ :: BinHandle -> ModuleName -> IO ()
put_ BinHandle
bh (ModuleName FastString
fs) = forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh FastString
fs
  get :: BinHandle -> IO ModuleName
get BinHandle
bh = do FastString
fs <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; forall (m :: * -> *) a. Monad m => a -> m a
return (FastString -> ModuleName
ModuleName FastString
fs)

instance Data ModuleName where
  -- don't traverse?
  toConstr :: ModuleName -> Constr
toConstr ModuleName
_   = String -> Constr
abstractConstr String
"ModuleName"
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ModuleName
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = forall a. HasCallStack => String -> a
error String
"gunfold"
  dataTypeOf :: ModuleName -> DataType
dataTypeOf ModuleName
_ = String -> DataType
mkNoRepType String
"ModuleName"

instance NFData ModuleName where
  rnf :: ModuleName -> ()
rnf ModuleName
x = ModuleName
x seq :: forall a b. a -> b -> b
`seq` ()

stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
-- ^ Compares module names lexically, rather than by their 'Unique's
stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
stableModuleNameCmp ModuleName
n1 ModuleName
n2 = ModuleName -> FastString
moduleNameFS ModuleName
n1 FastString -> FastString -> Ordering
`lexicalCompareFS` ModuleName -> FastString
moduleNameFS ModuleName
n2

pprModuleName :: ModuleName -> SDoc
pprModuleName :: ModuleName -> SDoc
pprModuleName (ModuleName FastString
nm) =
    (PprStyle -> SDoc) -> SDoc
getPprStyle forall a b. (a -> b) -> a -> b
$ \ PprStyle
sty ->
    if PprStyle -> Bool
codeStyle PprStyle
sty
        then FastZString -> SDoc
ztext (FastString -> FastZString
zEncodeFS FastString
nm)
        else FastString -> SDoc
ftext FastString
nm

moduleNameFS :: ModuleName -> FastString
moduleNameFS :: ModuleName -> FastString
moduleNameFS (ModuleName FastString
mod) = FastString
mod

moduleNameString :: ModuleName -> String
moduleNameString :: ModuleName -> String
moduleNameString (ModuleName FastString
mod) = FastString -> String
unpackFS FastString
mod

mkModuleName :: String -> ModuleName
mkModuleName :: String -> ModuleName
mkModuleName String
s = FastString -> ModuleName
ModuleName (String -> FastString
mkFastString String
s)

mkModuleNameFS :: FastString -> ModuleName
mkModuleNameFS :: FastString -> ModuleName
mkModuleNameFS FastString
s = FastString -> ModuleName
ModuleName FastString
s

-- |Returns the string version of the module name, with dots replaced by slashes.
--
moduleNameSlashes :: ModuleName -> String
moduleNameSlashes :: ModuleName -> String
moduleNameSlashes = ShowS
dots_to_slashes forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleName -> String
moduleNameString
  where dots_to_slashes :: ShowS
dots_to_slashes = forall a b. (a -> b) -> [a] -> [b]
map (\Char
c -> if Char
c forall a. Eq a => a -> a -> Bool
== Char
'.' then Char
pathSeparator else Char
c)

-- |Returns the string version of the module name, with dots replaced by colons.
--
moduleNameColons :: ModuleName -> String
moduleNameColons :: ModuleName -> String
moduleNameColons = ShowS
dots_to_colons forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleName -> String
moduleNameString
  where dots_to_colons :: ShowS
dots_to_colons = forall a b. (a -> b) -> [a] -> [b]
map (\Char
c -> if Char
c forall a. Eq a => a -> a -> Bool
== Char
'.' then Char
':' else Char
c)

parseModuleName :: ReadP ModuleName
parseModuleName :: ReadP ModuleName
parseModuleName = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ModuleName
mkModuleName
                forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ReadP String
Parse.munch1 (\Char
c -> Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"_.")