git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Revert "C++: revamp arrays"

This reverts commit fd735e7f0993ebcf41137275acbecb55bff61fde.

commite35596a531e23f2013c767bed6dcc9b984bcbd5d authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 14 Feb 2016, 17:37 (11 years ago) parentfd735e7
7 files changed+17−60
pcc/include/purescript.cc+8−15 pcc/include/purescript.hh+4−23 src/Language/PureScript/CodeGen/Cpp.hs+4−4 src/Language/PureScript/CodeGen/Cpp/AST.hs−7 src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs−1 src/Language/PureScript/CodeGen/Cpp/Types.hs+1−4 src/Language/PureScript/Pretty/Cpp.hs−6
pcc/include/purescript.cc+8−15
@@ -27,7 +27,6 @@ namespace PureScript {
2727 case Type::String: new (&s) shared<std::string>(src.s); break; \
2828 case Type::Map: new (&m) shared<map>(src.m); break; \
2929 case Type::Vector: new (&v) shared<vector>(src.v); break; \
30− case Type::Array: new (&a) shared<array>(src.a); break; \
3130 case Type::Function: f = src.f; break; \
3231 case Type::Closure: new (&l) shared<closure>(src.l); break; \
3332 case Type::EffFunction: new (&e) shared<eff_fn>(src.e); break; \
@@ -47,7 +46,6 @@ namespace PureScript {
4746 case Type::String: new (&s) shared<std::string>(std::move(src.s)); break; \
4847 case Type::Map: new (&m) shared<map>(std::move(src.m)); break; \
4948 case Type::Vector: new (&v) shared<vector>(std::move(src.v)); break; \
50− case Type::Array: new (&a) shared<array>(std::move(src.a)); break; \
5149 case Type::Function: f = src.f; break; \
5250 case Type::Closure: new (&l) shared<closure>(std::move(src.l)); break; \
5351 case Type::EffFunction: new (&e) shared<eff_fn>(std::move(src.e)); break; \
@@ -94,7 +92,6 @@ any::~any() {
9492 case Type::String: s.~shared<std::string>(); break;
9593 case Type::Map: m.~shared<map>(); break;
9694 case Type::Vector: v.~shared<vector>(); break;
97− case Type::Array: a.~shared<array>(); break;
9895 case Type::Function: ; break;
9996 case Type::Closure: l.~shared<closure>(); break;
10097 case Type::EffFunction: e.~shared<eff_fn>(); break;
@@ -152,10 +149,10 @@ auto any::cast() const -> typename std::enable_if<std::is_same<T, map>::value, c
152149template auto any::cast<any::map>() const -> const map&;
153150
154151template <typename T>
155−auto any::cast() const -> typename std::enable_if<std::is_same<T, array>::value, const T&>::type {
156− RETURN_VALUE(Type::Array, a, *)
152+auto any::cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type {
153+ RETURN_VALUE(Type::Vector, v, *)
157154}
158−template auto any::cast<any::array>() const -> const array&;
155+template auto any::cast<any::vector>() const -> const vector&;
159156
160157auto any::operator()(const any& arg) const -> any {
161158 const any& variant = unthunkVariant(*this);
@@ -201,10 +198,6 @@ any::operator const vector&() const {
201198 return cast<vector>();
202199}
203200
204−any::operator const array&() const {
205− return cast<array>();
206−}
207−
208201auto any::extractPointer() const -> void* {
209202 RETURN_VALUE(Type::Pointer, p.get(),)
210203}
@@ -235,16 +228,16 @@ auto any::operator[](const char rhs[]) const -> const any& {
235228 throw runtime_error("map key not found");
236229}
237230
238−auto any::operator[](const size_t rhs) const -> const any& {
231+auto any::operator[](const vector::size_type rhs) const -> const any& {
239232 const any& variant = unthunkVariant(*this);
240− assert(variant.type == Type::Array);
241− return (*variant.a)[rhs];
233+ assert(variant.type == Type::Vector);
234+ return (*variant.v)[rhs];
242235}
243236
244237auto any::operator[](const any& rhs) const -> const any& {
245238 const any& variant = unthunkVariant(*this);
246− assert(variant.type == Type::Array);
247− return (*variant.a)[rhs.cast<long>()];
239+ assert(variant.type == Type::Vector);
240+ return (*variant.v)[rhs.cast<long>()];
248241}
249242
250243auto any::contains(const char key[]) const -> bool {
pcc/include/purescript.hh+4−23
@@ -29,7 +29,6 @@
2929#include <functional>
3030#include <string>
3131#include <vector>
32−#include <deque>
3332#include <utility>
3433#include <stdexcept>
3534
@@ -57,7 +56,7 @@ public:
5756};
5857
5958const bool undefined = false;
60−const size_t kDataCtor = 0;
59+const std::vector<int>::size_type kDataCtor = 0;
6160
6261// A variant data class designed to provide some features of dynamic typing.
6362//
@@ -74,7 +73,6 @@ class any {
7473 String,
7574 Map,
7675 Vector,
77− Array,
7876 Function,
7977 Closure,
8078 EffFunction,
@@ -82,22 +80,17 @@ class any {
8280 Pointer
8381 };
8482
85− private:
8683 mutable Type type = Type::Unknown;
87− using vector = std::vector<any>;
8884
89− public:
9085 struct as_thunk {
9186 };
9287 static constexpr as_thunk unthunk = as_thunk{};
9388
9489 using map = std::vector<std::pair<const char * const, const any>>;
95− using data = vector;
96− using array = std::deque<any>;
90+ using vector = std::vector<any>;
9791 using fn = auto (*)(const any&) -> any;
9892 using thunk = auto (*)(const as_thunk) -> const any&;
9993
100− private:
10194 class closure {
10295 public:
10396 virtual auto operator()(const any&) const -> any = 0;
@@ -128,7 +121,6 @@ class any {
128121 }
129122 };
130123
131− public:
132124 template <typename T>
133125 using shared = std::shared_ptr<T>;
134126
@@ -147,7 +139,6 @@ class any {
147139 mutable shared<std::string> s;
148140 mutable shared<map> m;
149141 mutable shared<vector> v;
150− mutable shared<array> a;
151142 mutable fn f;
152143 mutable shared<closure> l;
153144 mutable shared<eff_fn> e;
@@ -182,9 +173,6 @@ class any {
182173 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
183174 any(vector&& val) noexcept : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
184175
185− any(const array& val) : type(Type::Array), a(make_shared<array>(val)) {}
186− any(array&& val) noexcept : type(Type::Array), a(make_shared<array>(std::move(val))) {}
187−
188176 template <typename T>
189177 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
190178 : type(Type::Function), f(val) {}
@@ -240,14 +228,8 @@ class any {
240228 template <typename T>
241229 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
242230
243− // These cannot be thunked
244− template <typename T>
245− inline auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type {
246− return *v;
247− }
248−
249231 template <typename T>
250− auto cast() const -> typename std::enable_if<std::is_same<T, array>::value, const T&>::type;
232+ auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
251233
252234 template <typename T>
253235 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
@@ -267,10 +249,9 @@ class any {
267249 operator string() const;
268250 operator const map&() const;
269251 operator const vector&() const;
270− operator const array&() const;
271252
272253 auto operator[](const char[]) const -> const any&;
273− auto operator[](const size_t) const -> const any&;
254+ auto operator[](const vector::size_type) const -> const any&;
274255 auto operator[](const any&) const -> const any&;
275256
276257 auto contains(const char[]) const -> bool;
src/Language/PureScript/CodeGen/Cpp.hs+4−4
@@ -135,7 +135,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
135135 CppFunction (identToCpp ident)
136136 [] (Just $ CppAny [])
137137 [CppInline]
138− (CppBlock [CppReturn (CppDataLiteral [CppStringLiteral (identToCpp ident)])])
138+ (CppBlock [CppReturn (CppArrayLiteral [CppStringLiteral (identToCpp ident)])])
139139
140140 declToCpp _ ident (Constructor _ _ (ProperName _) fields) = return . CppNamespace [] $
141141 [ CppFunction ('$' : identToCpp ident) (farg <$> fields')
@@ -159,7 +159,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
159159 (Just $ CppAny [])
160160 (CppBlock $ fieldLambdas fs')]
161161 | otherwise = fullyConstructed
162− fullyConstructed = [CppReturn (CppDataLiteral (CppStringLiteral name : (CppVar <$> fields')))]
162+ fullyConstructed = [CppReturn (CppArrayLiteral (CppStringLiteral name : (CppVar <$> fields')))]
163163
164164 declToCpp _ ident val = do
165165 val' <- valueToCpp val
@@ -421,7 +421,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
421421 return $ case ctorType of
422422 ProductType -> cpps
423423 SumType ->
424− [ CppIfElse (CppBinary Equal (CppIndexer (CppVar ctorKey) (CppCast dataType $ CppVar varName))
424+ [ CppIfElse (CppBinary Equal (CppIndexer (CppVar ctorKey) (CppVar varName))
425425 (CppStringLiteral ctor))
426426 (CppBlock cpps)
427427 Nothing ]
@@ -434,7 +434,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
434434 cpps <- binderToCpp argVar done'' binder
435435 return (CppVariableIntroduction (argVar, Nothing)
436436 []
437− (Just (CppIndexer (fieldToIndex field) (CppCast dataType $ CppVar varName)))
437+ (Just (CppIndexer (fieldToIndex field) (CppVar varName)))
438438 : cpps)
439439 fieldToIndex :: Ident -> Cpp
440440 fieldToIndex = CppNumericLiteral . Left . (+1) . read . dropWhile isLetter . runIdent
src/Language/PureScript/CodeGen/Cpp/AST.hs−7
@@ -170,10 +170,6 @@ data Cpp
170170 --
171171 | CppArrayLiteral [Cpp]
172172 -- |
173− -- A data literal
174− --
175− | CppDataLiteral [Cpp]
176− -- |
177173 -- An array indexer expression
178174 --
179175 | CppIndexer Cpp Cpp
@@ -286,7 +282,6 @@ everywhereOnCpp f = go
286282 go (CppUnary op j) = f (CppUnary op (go j))
287283 go (CppBinary op j1 j2) = f (CppBinary op (go j1) (go j2))
288284 go (CppArrayLiteral cpp) = f (CppArrayLiteral (map go cpp))
289− go (CppDataLiteral cpp) = f (CppDataLiteral (map go cpp))
290285 go (CppIndexer j1 j2) = f (CppIndexer (go j1) (go j2))
291286 go (CppObjectLiteral cpp) = f (CppObjectLiteral (map (fmap go) cpp))
292287 go (CppAccessor prop j) = f (CppAccessor (go prop) (go j))
@@ -316,7 +311,6 @@ everywhereOnCppTopDownM f = f >=> go
316311 go (CppUnary op j) = CppUnary op <$> f' j
317312 go (CppBinary op j1 j2) = CppBinary op <$> f' j1 <*> f' j2
318313 go (CppArrayLiteral cpp) = CppArrayLiteral <$> traverse f' cpp
319− go (CppDataLiteral cpp) = CppDataLiteral <$> traverse f' cpp
320314 go (CppIndexer j1 j2) = CppIndexer <$> f' j1 <*> f' j2
321315 go (CppObjectLiteral cpp) = CppObjectLiteral <$> traverse (sndM f') cpp
322316 go (CppAccessor prop j) = CppAccessor prop <$> f' j
@@ -342,7 +336,6 @@ everythingOnCpp (<>) f = go
342336 go j@(CppUnary _ j1) = f j <> go j1
343337 go j@(CppBinary _ j1 j2) = f j <> go j1 <> go j2
344338 go j@(CppArrayLiteral cpp) = foldl (<>) (f j) (map go cpp)
345− go j@(CppDataLiteral cpp) = foldl (<>) (f j) (map go cpp)
346339 go j@(CppIndexer j1 j2) = f j <> go j1 <> go j2
347340 go j@(CppObjectLiteral cpp) = foldl (<>) (f j) (map (go . snd) cpp)
348341 go j@(CppAccessor j1 j2) = f j <> go j1 <> go j2
src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs−1
@@ -48,7 +48,6 @@ shouldInline (CppStringLiteral _) = True
4848shouldInline (CppBooleanLiteral _) = True
4949shouldInline (CppAccessor _ val) = shouldInline val
5050shouldInline (CppIndexer index val) = shouldInline index && shouldInline val
51−shouldInline (CppCast _ val) = shouldInline val
5251shouldInline _ = False
5352
5453etaConvert :: Cpp -> Cpp
src/Language/PureScript/CodeGen/Cpp/Types.hs+1−4
@@ -108,11 +108,8 @@ charType = CppPrimitive "char"
108108mapType :: CppType
109109mapType = CppPrimitive "any::map"
110110
111−dataType :: CppType
112−dataType = CppPrimitive "any::data"
113−
114111arrayType :: CppType
115−arrayType = CppPrimitive "any::array"
112+arrayType = CppPrimitive "any::vector"
116113
117114thunkMarkerType :: CppType
118115thunkMarkerType = CppPrimitive "any::as_thunk"
src/Language/PureScript/Pretty/Cpp.hs−6
@@ -71,12 +71,6 @@ literals = mkPattern' match
7171 , fmap (intercalate ", ") $ forM xs prettyPrintCpp'
7272 , return " }"
7373 ]
74− match (CppDataLiteral xs) = fmap concat $ sequence
75− [ return $ runType dataType
76− , return "{ "
77− , fmap (intercalate ", ") $ forM xs prettyPrintCpp'
78− , return " }"
79− ]
8074 match (CppObjectLiteral ps) = fmap concat $ sequence
8175 [ return $ runType mapType ++ "{\n"
8276 , withIndent $ do