git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: fix issue with __superclass name mangling
commit0dc704b0c2b45b9aada5a3bffb5a5a67886b142f authorAndy Arvanitis <andyarvanitis@gmail.com> dateTue 20 Dec 2016, 15:52 (10 years ago) parent97de133
4 files changed+20−12
src/Language/PureScript/CodeGen/Cpp.hs+2−2 src/Language/PureScript/CodeGen/Cpp/Common.hs+16−4 src/Language/PureScript/CodeGen/Cpp/File.hs+2−3 src/Language/PureScript/Pretty/Cpp.hs−3
src/Language/PureScript/CodeGen/Cpp.hs+2−2
@@ -28,7 +28,6 @@ module Language.PureScript.CodeGen.Cpp
2828 ) where
2929
3030import Prelude.Compat
31−
3231import Data.Char (isLetter)
3332import Data.Function (on)
3433import Data.List
@@ -138,7 +137,8 @@ moduleToCpp otherOpts env (Module _ mn imps _ foreigns decls) = do
138137 -------------------------------------------------------------------------------------------------
139138 declToCpp _ (_, ident) e@(Abs (_, _, _, Just IsTypeClassConstructor) _ _) =
140139 let className = identToCpp ident
141− (supers, members) = span (C.__superclass_ `Text.isPrefixOf`) (identToCpp <$> (fst $ unAbs e []))
140+ (supers, members) =
141+ span (safeName C.__superclass_ `Text.isPrefixOf`) (identToCpp <$> (fst $ unAbs e []))
142142 in return $ CppStruct className [CppEnum Nothing Nothing (sort supers <> members)]
143143 declToCpp vqs (_, ident) (Abs (_, com, ty, _) arg body) = do
144144 fn <-
src/Language/PureScript/CodeGen/Cpp/Common.hs+16−4
@@ -18,11 +18,12 @@
1818
1919module Language.PureScript.CodeGen.Cpp.Common where
2020
21−import Prelude.Compat hiding (all, concatMap, last, init, null)
21+import Prelude.Compat hiding (all, any, concatMap, last, init, map, null)
2222
2323import Data.Char
24−import Data.Text hiding (map)
24+import Data.Text hiding (foldl1)
2525import Data.Monoid ((<>))
26+import qualified Language.PureScript.Constants as C
2627import Language.PureScript.Crash
2728import Language.PureScript.Names
2829
@@ -45,6 +46,10 @@ safeName name
4546safeName name
4647 | Just ('$', s) <- uncons name
4748 , all isDigit s = name
49+safeName name
50+ | C.__superclass_ `isPrefixOf` name
51+ , any (=='.') name
52+ = "$" <> (escaped $ dotsTo '_' name)
4853safeName name
4954 | "_" `isPrefixOf` name = "$" <> escaped name
5055safeName name = escaped name
@@ -52,6 +57,9 @@ safeName name = escaped name
5257escaped :: Text -> Text
5358escaped = escapeDoubleUnderscores . concatMap identCharToText
5459
60+dotsTo :: Char -> Text -> Text
61+dotsTo chr' = map (\c -> if c == '.' then chr' else c)
62+
5563-- |
5664-- C++ actually reserves all identifiers containing double
5765-- underscores.
@@ -235,8 +243,12 @@ normalizedName = unescapeDoubleUnderscores . normalizedName'
235243
236244moduleNameToCpp :: ModuleName -> Text
237245moduleNameToCpp (ModuleName pns) =
238− let name = intercalate "_" (runProperName `map` pns)
239− in if properNameIsCppReserved name then name <> "$" else name
246+ if properNameIsCppReserved name then name <> "$" else name
247+ where
248+ pnames = runProperName <$> pns
249+ name
250+ | foldl1 (||) (any (=='_') <$> pnames) = error "Module names can now contain underscores"
251+ | otherwise = intercalate "_" pnames
240252
241253-- |
242254-- Checks whether a proper name is reserved in C++11.
src/Language/PureScript/CodeGen/Cpp/File.hs+2−3
@@ -24,11 +24,10 @@ import Data.Monoid ((<>))
2424import Data.Text (Text)
2525
2626import Language.PureScript.CodeGen.Cpp.AST
27+import Language.PureScript.CodeGen.Cpp.Common
2728import Language.PureScript.CodeGen.Cpp.Types
2829import Language.PureScript.Names
2930
30−import qualified Language.PureScript.Pretty.Cpp as P
31−
3231---------------------------------------------------------------------------------------------------
3332toHeader :: [Cpp] -> [Cpp]
3433---------------------------------------------------------------------------------------------------
@@ -171,7 +170,7 @@ fileEnd :: ModuleName -> Text -> [Cpp]
171170fileEnd mn suffix = [CppRaw ("#endif // " <> fileModName mn suffix)]
172171
173172fileModName :: ModuleName -> Text -> Text
174−fileModName mn suffix = P.dotsTo '_' (runModuleName mn <> "_" <> suffix)
173+fileModName mn suffix = dotsTo '_' (runModuleName mn <> "_" <> suffix)
175174
176175isMain :: ModuleName -> Bool
177176isMain (ModuleName [ProperName "Main"]) = True
src/Language/PureScript/Pretty/Cpp.hs−3
@@ -515,9 +515,6 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
515515 , [ binary Or " || " ]
516516 ]
517517
518−dotsTo :: Char -> Text -> Text
519−dotsTo chr' = T.map (\c -> if c == '.' then chr' else c)
520−
521518argstr :: (Text, Maybe CppType) -> Text
522519argstr (name, Nothing) = argStr name (Auto [])
523520argstr (name, Just typ)