1-- | Common code generation utility functions
2module CodeGen.Cpp.Common where
7import Data.Monoid ((<>))
9import qualified Data.Text as T
11import Language.PureScript.Crash
12import Language.PureScript.Names
14moduleNameToCpp :: ModuleName -> Text
15moduleNameToCpp (ModuleName pns) =
16 let name = T.intercalate "_" (runProperName `map` pns)
17 in if nameIsCppBuiltIn name then ("$" <> name <> "$") else name
19-- | Convert an 'Ident' into a valid C++ identifier:
21-- * Alphanumeric characters are kept unmodified.
23-- * Reserved C++ identifiers are prefixed with '$$'.
25-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
26identToCpp :: Ident -> Text
27identToCpp UnusedIdent = unusedName
28identToCpp (Ident "$__unused") = unusedName
29identToCpp (Ident name) = properToCpp name
30identToCpp (GenIdent _ _) = internalError "GenIdent in identToCpp"
33unusedName = "$unused$"
35properToCpp :: Text -> Text
37 | nameIsCppReserved name || nameIsCppBuiltIn 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 /= properToCpp 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 = '$' `T.cons` T.pack (show (ord c))
53-- | Checks whether an identifier name is reserved in C++.
54nameIsCppReserved :: Text -> Bool
55nameIsCppReserved name =
56 name `elem` cppAnyReserved
58-- | Checks whether a name matches a built-in value in C++.
59nameIsCppBuiltIn :: Text -> Bool
60nameIsCppBuiltIn name =
68cppAnyReserved :: [Text]
175prefixIsReserved :: Text -> Bool
176prefixIsReserved name =
177 any (`T.isPrefixOf` name)
214unbox t = "unbox<" <> t <> ">"
217arrayLengthFn = "array_length"
220tcoLoop = "$TCO_loop$"
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