-- Copyright 2019 Google LLC
--
-- Use of this source code is governed by a BSD-style
-- license that can be found in the LICENSE file or at
-- https://developers.google.com/open-source/licenses/bsd

-- | This module defines custom types for defining names of various
-- syntax terms.
--
-- These types are all instances of 'Data.String.IsString'.  For ease of use,
-- we recommend enabling the @OverloadedStrings@ extension.
{-# LANGUAGE CPP #-}
module GHC.SourceGen.Name
    ( -- * RdrNameStr
      RdrNameStr(..)
    , RawNameSpace(..)
    , rdrNameStrToString
    , qual
    , unqual
      -- * OccNameStr
    , OccNameStr
    , occNameStrToString
    , occNameStrNamespace
    , occNameToStr
    , nameToStr
      -- ModuleNameStr
    , ModuleNameStr(..)
    , moduleNameStrToString
    ) where

#if MIN_VERSION_ghc(9,0,0)
import GHC.Data.FastString (unpackFS)
import GHC.Unit.Module (moduleNameString)
import GHC.Types.Name.Occurrence (OccName, occNameFS, occNameSpace, isVarNameSpace)
import GHC.Types.Name (Name, nameOccName)
#else
import FastString (unpackFS)
import Module (moduleNameString)
import OccName (OccName, occNameFS, occNameSpace, isVarNameSpace)
import Name (Name, nameOccName)
#endif
import GHC.SourceGen.Name.Internal

unqual :: OccNameStr -> RdrNameStr
unqual :: OccNameStr -> RdrNameStr
unqual = OccNameStr -> RdrNameStr
UnqualStr

qual :: ModuleNameStr -> OccNameStr -> RdrNameStr
qual :: ModuleNameStr -> OccNameStr -> RdrNameStr
qual = ModuleNameStr -> OccNameStr -> RdrNameStr
QualStr

moduleNameStrToString :: ModuleNameStr -> String
moduleNameStrToString :: ModuleNameStr -> String
moduleNameStrToString = ModuleName -> String
moduleNameString (ModuleName -> String)
-> (ModuleNameStr -> ModuleName) -> ModuleNameStr -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleNameStr -> ModuleName
unModuleNameStr

occNameStrToString :: OccNameStr -> String
occNameStrToString :: OccNameStr -> String
occNameStrToString (OccNameStr RawNameSpace
_ FastString
s) = FastString -> String
unpackFS FastString
s

occNameStrNamespace :: OccNameStr -> RawNameSpace
occNameStrNamespace :: OccNameStr -> RawNameSpace
occNameStrNamespace (OccNameStr RawNameSpace
n FastString
_) = RawNameSpace
n

rdrNameStrToString :: RdrNameStr -> String
rdrNameStrToString :: RdrNameStr -> String
rdrNameStrToString (UnqualStr OccNameStr
o) = OccNameStr -> String
occNameStrToString OccNameStr
o
rdrNameStrToString (QualStr ModuleNameStr
m OccNameStr
o) =
    ModuleNameStr -> String
moduleNameStrToString ModuleNameStr
m String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char
'.' Char -> String -> String
forall a. a -> [a] -> [a]
: OccNameStr -> String
occNameStrToString OccNameStr
o

-- | Converts a GHC 'OccName' to an 'OccNameStr'.  Ignores whether the input
-- came from the namespace of types or of values.
occNameToStr :: OccName -> OccNameStr
occNameToStr :: OccName -> OccNameStr
occNameToStr OccName
o = RawNameSpace -> FastString -> OccNameStr
OccNameStr RawNameSpace
n (OccName -> FastString
occNameFS OccName
o)
  where
    n :: RawNameSpace
n = if NameSpace -> Bool
isVarNameSpace (NameSpace -> Bool) -> NameSpace -> Bool
forall a b. (a -> b) -> a -> b
$ OccName -> NameSpace
occNameSpace OccName
o
            then RawNameSpace
Value
            else RawNameSpace
Constructor

-- | Converts from a GHC 'Name' to an 'OccNameStr'.  Ignores whether
-- the input came from the namespace of types or of values, as well
-- as any other information about where the name came from.
nameToStr :: Name -> OccNameStr
nameToStr :: Name -> OccNameStr
nameToStr = OccName -> OccNameStr
occNameToStr  (OccName -> OccNameStr) -> (Name -> OccName) -> Name -> OccNameStr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> OccName
nameOccName