git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: remove some (obvious) unneeded lambda captures
adbb476Andy Arvanitis
src/Language/PureScript/CodeGen/Cpp.hs+14−12
@@ -145,7 +145,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
145 | otherwise = ([], [])
145 | otherwise = ([], [])
146 farg = \x -> (x, Just $ CppAny [CppConst, CppRef])
146 farg = \x -> (x, Just $ CppAny [CppConst, CppRef])
147 fieldLambdas flds
147 fieldLambdas flds
148+ | (f' : fs') <- flds = [CppReturn . maybeRemCaps $
148− | (f' : fs') <- flds = [CppReturn $ CppLambda [CppCaptureAll] (farg <$> [f'])
149+ CppLambda [CppCaptureAll] (farg <$> [f'])
149 (Just $ CppAny [])
150 (Just $ CppAny [])
150 (CppBlock $ fieldLambdas fs')]
151 (CppBlock $ fieldLambdas fs')]
151 | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
152 | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
@@ -155,17 +156,17 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
155 return $ CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just val')
156 return $ CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just val')
156
157
157 -------------------------------------------------------------------------------------------------
158 -------------------------------------------------------------------------------------------------
158− toLambda :: [CppCaptureType] -> Cpp -> Cpp
159+ fnToLambda :: Cpp -> Cpp
159 -------------------------------------------------------------------------------------------------
160 -------------------------------------------------------------------------------------------------
160− toLambda cs (CppFunction name args _ qs body) =
161+ fnToLambda (CppFunction name args _ qs body) =
161 CppVariableIntroduction (name, Just $ CppAny [CppConst])
162 CppVariableIntroduction (name, Just $ CppAny [CppConst])
162 (filter (==CppStatic) qs)
163 (filter (==CppStatic) qs)
163− (Just (CppLambda cs' args (Just $ CppAny []) body))
164+ (Just (CppLambda cs args (Just $ CppAny []) body))
164 where
165 where
165− cs' | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
166+ cs | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
166− | otherwise = cs
167+ | otherwise = [CppCaptureAll]
167− toLambda cs (CppComment com cpp) = CppComment com (toLambda cs cpp)
168+ fnToLambda (CppComment com cpp) = CppComment com (fnToLambda cpp)
168− toLambda _ _ = error "Not a function!"
169+ fnToLambda _ = error "Not a function!"
169
170
170 -------------------------------------------------------------------------------------------------
171 -------------------------------------------------------------------------------------------------
171 asReturnBlock :: Cpp -> Cpp
172 asReturnBlock :: Cpp -> Cpp
@@ -178,8 +179,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
178 convertNestedLambdas = everywhereOnCpp go
179 convertNestedLambdas = everywhereOnCpp go
179 where
180 where
180 go :: Cpp -> Cpp
181 go :: Cpp -> Cpp
181− go f@(CppFunction {}) = toLambda [CppCaptureAll] f
182+ go f@(CppFunction {}) = maybeRemCaps $ fnToLambda f
182− go (CppLambda [] args rtyp body) = CppLambda [CppCaptureAll] args rtyp body
183+ go (CppLambda _ args rtyp body) = maybeRemCaps $ CppLambda [CppCaptureAll] args rtyp body
183 go cpp = cpp
184 go cpp = cpp
184
185
185 -------------------------------------------------------------------------------------------------
186 -------------------------------------------------------------------------------------------------
@@ -211,11 +212,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
211
212
212 toelem :: Cpp -> (String, Cpp)
213 toelem :: Cpp -> (String, Cpp)
213 toelem (CppFunction name args rtyp _ (CppBlock body)) =
214 toelem (CppFunction name args rtyp _ (CppBlock body)) =
215+ (name, withdict . maybeRemCaps $ CppLambda [CppCaptureAll]
214− (name, withdict $ CppLambda [CppCaptureAll] args rtyp (CppBlock $ (accessor localdict <$> fns) ++ body))
216+ args rtyp (CppBlock $ (accessor localdict <$> fns) ++ body))
215 toelem _ = error "not a function"
217 toelem _ = error "not a function"
216
218
217 withdict :: Cpp -> Cpp
219 withdict :: Cpp -> Cpp
218− withdict cpp' =
220+ withdict cpp' = maybeRemCaps $
219 CppLambda [CppCaptureAll] [(localdict, Just $ CppAny [CppConst, CppRef])]
221 CppLambda [CppCaptureAll] [(localdict, Just $ CppAny [CppConst, CppRef])]
220 (Just $ CppAny [])
222 (Just $ CppAny [])
221 (CppBlock [CppReturn cpp'])
223 (CppBlock [CppReturn cpp'])
src/Language/PureScript/CodeGen/Cpp/File.hs+24−3
@@ -20,7 +20,6 @@ module Language.PureScript.CodeGen.Cpp.File where
20import Data.Maybe
20import Data.Maybe
21
21
22import Language.PureScript.CodeGen.Cpp.AST
22import Language.PureScript.CodeGen.Cpp.AST
23−-- import Language.PureScript.CodeGen.Cpp.Templates
24import Language.PureScript.CodeGen.Cpp.Types
23import Language.PureScript.CodeGen.Cpp.Types
25import Language.PureScript.Names
24import Language.PureScript.Names
26
25
@@ -119,14 +118,36 @@ toBody = catMaybes . map go
119 block = CppBlock [val, CppReturn (CppVar "_value_")]
118 block = CppBlock [val, CppReturn (CppVar "_value_")]
120 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
119 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
121 addCaptures :: Cpp -> Cpp
120 addCaptures :: Cpp -> Cpp
121+ addCaptures (CppObjectLiteral objs) = CppObjectLiteral (objlam <$> objs)
122+ where
123+ objlam :: (String, Cpp) -> (String, Cpp)
124+ objlam (name, (CppLambda _ args rty body)) = (name , CppLambda [] args rty $ addCaptures body)
125+ objlam obj = obj
122− addCaptures = everywhereOnCpp addCapture
126+ addCaptures cpps' = everywhereOnCpp addCapture cpps'
123 where
127 where
124 addCapture :: Cpp -> Cpp
128 addCapture :: Cpp -> Cpp
125− addCapture (CppLambda [] args rty body) = CppLambda [CppCaptureAll] args rty body
129+ addCapture (CppLambda _ args rty body) = maybeRemCaps $ CppLambda [CppCaptureAll] args rty body
126 addCapture cpp' = cpp'
130 addCapture cpp' = cpp'
127 go (CppComment comms cpp') | Just commented <- go cpp' = Just (CppComment comms commented)
131 go (CppComment comms cpp') | Just commented <- go cpp' = Just (CppComment comms commented)
128 go _ = Nothing
132 go _ = Nothing
129
133
134+-------------------------------------------------------------------------------------------------
135+maybeRemCaps :: Cpp -> Cpp
136+-------------------------------------------------------------------------------------------------
137+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn CppNumericLiteral{}]))
138+ = CppLambda [] args rtyp body
139+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn CppStringLiteral{}]))
140+ = CppLambda [] args rtyp body
141+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn CppCharLiteral{}]))
142+ = CppLambda [] args rtyp body
143+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn CppBooleanLiteral{}]))
144+ = CppLambda [] args rtyp body
145+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn CppAccessor{}]))
146+ = CppLambda [] args rtyp body
147+maybeRemCaps (CppLambda [CppCaptureAll] args rtyp body@(CppBlock [CppReturn (CppVar "unit")])) -- TODO: not really safe
148+ = CppLambda [] args rtyp body
149+maybeRemCaps cpp = cpp
150+
130---------------------------------------------------------------------------------------------------
151---------------------------------------------------------------------------------------------------
131
152
132fileBegin :: ModuleName -> String -> [Cpp]
153fileBegin :: ModuleName -> String -> [Cpp]