git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: remove some (obvious) unneeded lambda captures
commitadbb4769419f4530591485aafc61df7470dc991d authorAndy Arvanitis <andyarvanitis@gmail.com> dateFri 15 Jan 2016, 21:46 (11 years ago) parentc9cce9b
2 files changed+38−15
src/Language/PureScript/CodeGen/Cpp.hs+14−12 src/Language/PureScript/CodeGen/Cpp/File.hs+24−3
src/Language/PureScript/CodeGen/Cpp.hs+14−12
@@ -145,7 +145,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
145145 | otherwise = ([], [])
146146 farg = \x -> (x, Just $ CppAny [CppConst, CppRef])
147147 fieldLambdas flds
148− | (f' : fs') <- flds = [CppReturn $ CppLambda [CppCaptureAll] (farg <$> [f'])
148+ | (f' : fs') <- flds = [CppReturn . maybeRemCaps $
149+ CppLambda [CppCaptureAll] (farg <$> [f'])
149150 (Just $ CppAny [])
150151 (CppBlock $ fieldLambdas fs')]
151152 | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
@@ -155,17 +156,17 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
155156 return $ CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just val')
156157
157158 -------------------------------------------------------------------------------------------------
158− toLambda :: [CppCaptureType] -> Cpp -> Cpp
159+ fnToLambda :: Cpp -> Cpp
159160 -------------------------------------------------------------------------------------------------
160− toLambda cs (CppFunction name args _ qs body) =
161+ fnToLambda (CppFunction name args _ qs body) =
161162 CppVariableIntroduction (name, Just $ CppAny [CppConst])
162163 (filter (==CppStatic) qs)
163− (Just (CppLambda cs' args (Just $ CppAny []) body))
164+ (Just (CppLambda cs args (Just $ CppAny []) body))
164165 where
165− cs' | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
166− | otherwise = cs
167− toLambda cs (CppComment com cpp) = CppComment com (toLambda cs cpp)
168− toLambda _ _ = error "Not a function!"
166+ cs | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
167+ | otherwise = [CppCaptureAll]
168+ fnToLambda (CppComment com cpp) = CppComment com (fnToLambda cpp)
169+ fnToLambda _ = error "Not a function!"
169170
170171 -------------------------------------------------------------------------------------------------
171172 asReturnBlock :: Cpp -> Cpp
@@ -178,8 +179,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
178179 convertNestedLambdas = everywhereOnCpp go
179180 where
180181 go :: Cpp -> Cpp
181− go f@(CppFunction {}) = toLambda [CppCaptureAll] f
182− go (CppLambda [] args rtyp body) = CppLambda [CppCaptureAll] args rtyp body
182+ go f@(CppFunction {}) = maybeRemCaps $ fnToLambda f
183+ go (CppLambda _ args rtyp body) = maybeRemCaps $ CppLambda [CppCaptureAll] args rtyp body
183184 go cpp = cpp
184185
185186 -------------------------------------------------------------------------------------------------
@@ -211,11 +212,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
211212
212213 toelem :: Cpp -> (String, Cpp)
213214 toelem (CppFunction name args rtyp _ (CppBlock body)) =
214− (name, withdict $ CppLambda [CppCaptureAll] args rtyp (CppBlock $ (accessor localdict <$> fns) ++ body))
215+ (name, withdict . maybeRemCaps $ CppLambda [CppCaptureAll]
216+ args rtyp (CppBlock $ (accessor localdict <$> fns) ++ body))
215217 toelem _ = error "not a function"
216218
217219 withdict :: Cpp -> Cpp
218− withdict cpp' =
220+ withdict cpp' = maybeRemCaps $
219221 CppLambda [CppCaptureAll] [(localdict, Just $ CppAny [CppConst, CppRef])]
220222 (Just $ CppAny [])
221223 (CppBlock [CppReturn cpp'])
src/Language/PureScript/CodeGen/Cpp/File.hs+24−3
@@ -20,7 +20,6 @@ module Language.PureScript.CodeGen.Cpp.File where
2020import Data.Maybe
2121
2222import Language.PureScript.CodeGen.Cpp.AST
23−-- import Language.PureScript.CodeGen.Cpp.Templates
2423import Language.PureScript.CodeGen.Cpp.Types
2524import Language.PureScript.Names
2625
@@ -119,14 +118,36 @@ toBody = catMaybes . map go
119118 block = CppBlock [val, CppReturn (CppVar "_value_")]
120119 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
121120 addCaptures :: Cpp -> Cpp
122− addCaptures = everywhereOnCpp addCapture
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
126+ addCaptures cpps' = everywhereOnCpp addCapture cpps'
123127 where
124128 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
126130 addCapture cpp' = cpp'
127131 go (CppComment comms cpp') | Just commented <- go cpp' = Just (CppComment comms commented)
128132 go _ = Nothing
129133
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+
130151---------------------------------------------------------------------------------------------------
131152
132153fileBegin :: ModuleName -> String -> [Cpp]