git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Discontinue use of '$' in identifiers
commit137cc636bd003485da4037b3cf7bd4f8dc4b1190 authorAndy Arvanitis <andyarvanitis@gmail.com> dateFri 15 Mar 2019, 19:38 (8 years ago) parent843c3d9
4 files changed+25−21
runtime/purescript.h+3−3 src/CodeGen/Cpp.hs+10−5 src/CodeGen/Cpp/Common.hs+8−9 src/CodeGen/Cpp/Optimizer/TCO.hs+4−4
runtime/purescript.h+3−3
@@ -219,14 +219,14 @@ namespace purescript {
219219
220220#define DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR() \
221221 inline auto foreign() -> dict_t& {\
222− static dict_t $dict$;\
223− return $dict$;\
222+ static dict_t _;\
223+ return _;\
224224 }
225225
226226#define FOREIGN_BEGIN(NS) namespace NS {\
227227 using namespace purescript;\
228228 DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR()\
229− static const auto $foreign_exports_init$ = ([]() -> char {\
229+ static const auto data = ([]() -> char {\
230230 dict_t& exports = foreign();
231231#define FOREIGN_END return 0; }()); }
232232
src/CodeGen/Cpp.hs+10−5
@@ -207,7 +207,7 @@ moduleToCpp (Module _ coms mn _ imps _ foreigns decls) _ =
207207 valueToCpp (ObjectUpdate _ o ps) = do
208208 obj <- valueToCpp o
209209 updates <- mapM (sndM valueToCpp) ps
210− obj' <- freshName
210+ obj' <- freshName'
211211 let objVar = AST.Var Nothing obj'
212212 copy = AST.VariableIntroduction Nothing obj' (Just $ AST.App Nothing (AST.Var Nothing (unbox dictType)) [obj])
213213 assign (k, v) = AST.Assignment Nothing (accessorString k objVar) v
@@ -311,7 +311,7 @@ moduleToCpp (Module _ coms mn _ imps _ foreigns decls) _ =
311311 -- and guards.
312312 bindersToCpp :: SourceSpan -> [CaseAlternative Ann] -> [AST] -> m AST
313313 bindersToCpp ss binders vals = do
314− valNames <- replicateM (length vals) freshName
314+ valNames <- replicateM (length vals) freshName'
315315 let assignments = zipWith (AST.VariableIntroduction Nothing) valNames (map Just vals)
316316 cpps <- forM binders $ \(CaseAlternative bs result) -> do
317317 ret <- guardsToCpp result
@@ -362,7 +362,7 @@ moduleToCpp (Module _ coms mn _ imps _ foreigns decls) _ =
362362 go :: [(Ident, Binder Ann)] -> [AST] -> m [AST]
363363 go [] done' = return done'
364364 go ((field, binder) : remain) done' = do
365− argVar <- freshName
365+ argVar <- freshName'
366366 done'' <- go remain done'
367367 cpp <- binderToCpp argVar done'' binder
368368 return (AST.VariableIntroduction Nothing argVar (Just $ accessorString (mkString $ identToCpp field) $ AST.Var Nothing varName) : cpp)
@@ -390,7 +390,7 @@ moduleToCpp (Module _ coms mn _ imps _ foreigns decls) _ =
390390 go :: [AST] -> [(PSString, Binder Ann)] -> m [AST]
391391 go done' [] = return done'
392392 go done' ((prop, binder):bs') = do
393− propVar <- freshName
393+ propVar <- freshName'
394394 done'' <- go done' bs'
395395 cpp <- binderToCpp propVar done'' binder
396396 return (AST.VariableIntroduction Nothing propVar (Just (accessorString prop (AST.Var Nothing varName))) : cpp)
@@ -404,7 +404,7 @@ moduleToCpp (Module _ coms mn _ imps _ foreigns decls) _ =
404404 go :: [AST] -> Integer -> [Binder Ann] -> m [AST]
405405 go done' _ [] = return done'
406406 go done' index (binder:bs') = do
407− elVar <- freshName
407+ elVar <- freshName'
408408 done'' <- go done' (index + 1) bs'
409409 cpp <- binderToCpp elVar done'' binder
410410 return (AST.VariableIntroduction Nothing elVar (Just (AST.Indexer Nothing (AST.NumericLiteral Nothing (Left index)) (AST.Var Nothing varName))) : cpp)
@@ -414,3 +414,8 @@ emptyAnn = (SourceSpan "" (SourcePos 0 0) (SourcePos 0 0), [], Nothing, Nothing)
414414
415415arrayLength :: AST -> AST
416416arrayLength a = AST.App Nothing (AST.Var Nothing arrayLengthFn) [a]
417+
418+freshName' :: MonadSupply m => m Text
419+freshName' = do
420+ name <- freshName
421+ return $ T.replace "$" "_Local_" name
src/CodeGen/Cpp/Common.hs+8−9
@@ -14,15 +14,14 @@ import Language.PureScript.Names
1414moduleNameToCpp :: ModuleName -> Text
1515moduleNameToCpp (ModuleName pns) =
1616 let name = T.intercalate "_" (runProperName `map` pns)
17− in if nameIsCppBuiltIn name then "$$" <> name else name
17+ in if nameIsCppBuiltIn name then ("_" <> name <> "_") else name
1818
1919-- | Convert an 'Ident' into a valid C++ identifier:
2020--
2121-- * Alphanumeric characters are kept unmodified.
2222--
23−-- * Reserved C++ identifiers are prefixed with '$$'.
23+-- * Reserved C++ identifiers are wrapped with '_'.
2424--
25−-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
2625identToCpp :: Ident -> Text
2726identToCpp UnusedIdent = unusedName
2827identToCpp (Ident "$__unused") = unusedName
@@ -30,11 +29,11 @@ identToCpp (Ident name) = properToCpp name
3029identToCpp (GenIdent _ _) = internalError "GenIdent in identToCpp"
3130
3231unusedName :: Text
33−unusedName = "$unused$"
32+unusedName = "_Unused_"
3433
3534properToCpp :: Text -> Text
3635properToCpp name
37− | nameIsCppReserved name || nameIsCppBuiltIn name || prefixIsReserved name = "$$" <> name
36+ | nameIsCppReserved name || nameIsCppBuiltIn name || prefixIsReserved name = "_" <> name <> "_"
3837 | otherwise = T.concatMap identCharToText name
3938
4039-- | Test if a string is a valid AST identifier without escaping.
@@ -46,9 +45,9 @@ identNeedsEscaping s = s /= properToCpp s || T.null s
4645identCharToText :: Char -> Text
4746identCharToText c | isAlphaNum c = T.singleton c
4847identCharToText '_' = "_"
49−identCharToText '\'' = "$prime"
48+identCharToText '\'' = "_Prime_"
5049identCharToText '$' = "$"
51−identCharToText c = '$' `T.cons` T.pack (show (ord c))
50+identCharToText c = "_Code_Point_" <> T.pack (show (ord c))
5251
5352-- | Checks whether an identifier name is reserved in C++.
5453nameIsCppReserved :: Text -> Bool
@@ -217,7 +216,7 @@ arrayLengthFn :: Text
217216arrayLengthFn = "array_length"
218217
219218unretainedSuffix :: Text
220−unretainedSuffix = "$weak"
219+unretainedSuffix = "_Weak_"
221220
222221tcoLoop :: Text
223−tcoLoop = "$TCO_loop$"
222+tcoLoop = "_TCO_Loop_"
src/CodeGen/Cpp/Optimizer/TCO.hs+4−4
@@ -14,16 +14,16 @@ import qualified Language.PureScript.Constants as C
1414tco :: AST -> AST -> AST
1515tco mn = everywhere convert where
1616 tcoVar :: Text -> Text
17− tcoVar arg = "$TCO_" <> arg <> "$"
17+ tcoVar arg = "_TCO_" <> arg <> "_"
1818
1919 copyVar :: Text -> Text
20− copyVar arg = "$TCO_copy_" <> arg <> "$"
20+ copyVar arg = "_TCO_Copy_" <> arg <> "_"
2121
2222 tcoDone :: Text
23− tcoDone = "$TCO_done$"
23+ tcoDone = "_TCO_Done_"
2424
2525 tcoResult :: Text
26− tcoResult = "$TCO_result$"
26+ tcoResult = "_TCO_Result_"
2727
2828 convert :: AST -> AST
2929 convert fn@(Function ss (Just name) args _)