1-- | Common code generation utility functions
2module CodeGen.IL.Common where
6import Control.Monad.Supply.Class (MonadSupply, freshName)
8import Data.Monoid ((<>))
10import qualified Data.Text as T
12import Language.PureScript.Crash
13import Language.PureScript.Names
15moduleNameToIL :: ModuleName -> Text
16moduleNameToIL (ModuleName mn) =
17 let name = T.replace "." "_" mn
18 in if nameIsILBuiltIn name then (name <> "_") else name
20-- | Convert an 'Ident' into a valid C++ identifier:
22-- * Alphanumeric characters are kept unmodified.
24-- * Reserved C++ identifiers are wrapped with '_'.
26identToIL :: Ident -> Text
27identToIL UnusedIdent = unusedName
28identToIL (Ident "$__unused") = unusedName
29identToIL (Ident name) = properToIL name
30identToIL (GenIdent _ _) = internalError "GenIdent in identToIL"
35properToIL :: Text -> Text
37 | nameIsILReserved name || nameIsILBuiltIn name || prefixIsReserved name = name <> "_"
38 | otherwise = T.concatMap identCharToText name
40-- | Test if a string is a valid AST identifier without escaping.
41identNeedsEscaping :: Text -> Bool
42identNeedsEscaping s = s /= properToIL s || T.null s
44-- | Attempts to find a human-readable name for a symbol, if none has been specified returns the
46identCharToText :: Char -> Text
47identCharToText c | isAlphaNum c = T.singleton c
48identCharToText '_' = "_"
49identCharToText '\'' = "Prime_"
50identCharToText '$' = "$"
51identCharToText c = "_code_point_" <> T.pack (show (ord c))
53-- | Checks whether an identifier name is reserved in C++.
54nameIsILReserved :: Text -> Bool
55nameIsILReserved name =
56 name `elem` ilAnyReserved
58-- | Checks whether a name matches a built-in value in C++.
59nameIsILBuiltIn :: Text -> Bool
68ilAnyReserved :: [Text]
175prefixIsReserved :: Text -> Bool
176prefixIsReserved name =
177 any (`T.isPrefixOf` name)
201uncurriedFnType :: Int -> Text
202uncurriedFnType i = "Fn" <> (T.pack . show $ i)
220unbox t = "unbox<" <> t <> ">"
223arrayLengthFn = "array_length"
225unretainedSuffix :: Text
226unretainedSuffix = "_weak_"
228freshName' :: MonadSupply m => m Text
231 return $ T.replace "$" "_var" name <> "_"
233moduleRenamerMarker :: Text
234moduleRenamerMarker = "__"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list