git.iko.soy/rowtype-yoga/purescript-native
on d67d12e help sign in
425 lines 14 kB history raw
1///////////////////////////////////////////////////////////////////////////////
2//
3// Module : purescript.hh
4// Copyright : (c) Andy Arvanitis 2015
5// License : MIT
6//
7// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8// Stability : experimental
9// Portability :
10//
11// Basic types and functions to support purescript-to-C++1x rendering
12//
13///////////////////////////////////////////////////////////////////////////////
14//
15#ifndef PureScript_HH
16#define PureScript_HH
18// Standard includes
20#if defined(DEBUG)
21 #include <cassert>
22#else
23 #define NDEBUG
24 #undef assert
25 #define assert(x)
26#endif
28#include <functional>
29#include <string>
30#include <vector>
31#include <deque>
32#include <utility>
33#include <stdexcept>
34#include <iso646.h> // mostly for MS Visual Studio compiler
35#include "purescript_memory.hh"
37namespace PureScript {
39using cstring = const char *;
40using nullptr_t = std::nullptr_t;
42// Workaround for missing C++11 version in gcc
43class runtime_error : public std::runtime_error {
44public:
45 runtime_error(const char message[]) : std::runtime_error(std::string(message)) {}
46};
48const bool undefined = false;
49const size_t constructor = 0;
51// A variant data class designed to provide some features of dynamic typing.
52//
53class any {
55 public:
56 enum class Type {
57 Thunk = 0x10,
58 Integer,
59 Double,
60 Character,
61 Boolean,
62 StringLiteral,
63 Function,
64 EffFunction,
65 RawPointer,
66 String,
67 Map,
68 Data,
69 Array,
70 Closure,
71 EffClosure,
72 Pointer
73 };
75 private:
76 mutable Type type;
78 public:
79 struct as_thunk {
80 };
81 static constexpr as_thunk unthunk = as_thunk{};
83 using map_pair = std::pair<const char * const, const any>;
84 using map = std::vector<map_pair WITH_ALLOCATOR(map_pair)>;
85 using data = std::vector<any WITH_ALLOCATOR(any)>;
86 using array = std::deque<any WITH_ALLOCATOR(any)>;
87 using fn = auto (*)(const any&) -> any;
88 using eff_fn = auto (*)() -> any;
89 using thunk = auto (*)(const as_thunk) -> const any&;
91 private:
92 class closure {
93 public:
94 virtual auto operator()(const any&) const -> any = 0;
95 virtual ~closure() {}
96 };
98 template <typename T>
99 class _closure : public closure {
100 const T lambda;
101 public:
102 _closure(const T& l) noexcept : lambda(l) {}
103 auto operator()(const any& arg) const -> any override {
104 return lambda(arg);
105 }
106 };
108 class eff_closure {
109 public:
110 virtual auto operator()() const -> any = 0;
111 virtual ~eff_closure() {}
112 };
114 template <typename T>
115 class _eff_closure : public eff_closure {
116 const T lambda;
117 public:
118 _eff_closure(const T& l) noexcept : lambda(l) {}
119 auto operator()() const -> any override {
120 return lambda();
121 }
122 };
124 private:
125 union {
126 mutable thunk t;
127 mutable int i;
128 mutable double d;
129 mutable char c;
130 mutable bool b;
131 mutable cstring r;
132 mutable fn f;
133 mutable eff_fn e;
134 mutable void * u;
135 mutable managed<std::string> s;
136 mutable managed<map> m;
137 mutable managed<data> v;
138 mutable managed<array> a;
139 mutable managed<closure> l;
140 mutable managed<eff_closure> k;
141 mutable managed<void> p;
142 };
144 public:
146 any(const int val) noexcept : type(Type::Integer), i(val) {}
147 any(const long int_value) noexcept : type(Type::Integer), i(int_value) {
148 assert(int_value >= INT_MIN && int_value <= INT_MAX);
149 }
151 any(const double val) noexcept : type(Type::Double), d(val) {}
152 any(const char val) noexcept : type(Type::Character), c(val) {}
154 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
155 any(const T val) noexcept : type(Type::Boolean), b(val) {}
157 template <size_t N>
158 any(const char (&val)[N]) noexcept : type(Type::StringLiteral), r(val) {}
159 any(char * val) : type(Type::String), s(make_managed<std::string>(val)) {}
161 any(const std::string& val) : type(Type::String), s(make_managed<std::string>(val)) {}
162 any(std::string&& val) noexcept : type(Type::String), s(make_managed<std::string>(std::move(val))) {}
164 any(const managed<std::string>& val) noexcept : type(Type::String), s(val) {}
165 any(managed<std::string>&& val) noexcept : type(Type::String), s(std::move(val)) {}
167 any(const map& val) : type(Type::Map), m(make_managed<map>(val)) {}
168 any(map&& val) noexcept : type(Type::Map), m(make_managed<map>(std::move(val))) {}
170 any(const data& val) : type(Type::Data), v(make_managed<data>(val)) {}
171 any(data&& val) noexcept : type(Type::Data), v(make_managed<data>(std::move(val))) {}
173 any(const array& val) : type(Type::Array), a(make_managed<array>(val)) {}
174 any(array&& val) noexcept : type(Type::Array), a(make_managed<array>(std::move(val))) {}
176 template <typename T>
177 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0) noexcept
178 : type(Type::Function), f(val) {}
180 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
181 !std::is_convertible<T,fn>::value>::type>
182 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
183 : type(Type::Closure), l(make_managed<_closure<T>>(val)) {}
185 template <typename T>
186 any(const T& val, typename std::enable_if<std::is_convertible<T,eff_fn>::value>::type* = 0) noexcept
187 : type(Type::EffFunction), e(val) {}
189 template <typename T,
190 typename = typename std::enable_if<!std::is_same<any,T>::value &&
191 !std::is_convertible<T,eff_fn>::value>::type>
192 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
193 : type(Type::EffClosure), k(make_managed<_eff_closure<T>>(val)) {}
195 template <typename T>
196 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0) noexcept
197 : type(Type::Thunk), t(val) {}
199 template <typename T,
200 typename = typename std::enable_if<std::is_class<typename std::remove_pointer<T>::type>::value>::type>
201 any(const T& val, typename std::enable_if<IS_POINTER_TYPE(T)::value>::type* = 0) noexcept
202 : type(Type::Pointer), p(val) {}
204 template <typename T>
205 any(T&& val, typename std::enable_if<std::is_assignable<managed<void>,T>::value>::type* = 0) noexcept
206 : type(Type::Pointer), p(std::move(val)) {}
208 // Explicit void* to raw pointer value
209 template <typename T>
210 any(const T& val, typename std::enable_if<std::is_same<T,void*>::value>::type* = 0) noexcept
211 : type(Type::RawPointer), u(val) {}
213 any(std::nullptr_t) noexcept : type(Type::Pointer), p(nullptr) {}
216#if !defined(USE_GC)
217 private:
218 template <typename T,
219 typename U,
220 typename = typename std::enable_if<!std::is_reference<T>::value>::type>
221 static constexpr auto move_if_rvalue(U&& u) noexcept -> U&& {
222 return static_cast<U&&>(u);
223 }
225 template <typename T,
226 typename U,
227 typename = typename std::enable_if<std::is_reference<T>::value>::type>
228 static constexpr auto move_if_rvalue(U& u) noexcept -> U& {
229 return static_cast<U&>(u);
230 }
232 template <typename T>
233 auto assign(T&& other) const noexcept -> void {
234 switch (other.type) {
235 case Type::Thunk: t = other.t; break;
236 case Type::Integer: i = other.i; break;
237 case Type::Double: d = other.d; break;
238 case Type::Character: c = other.c; break;
239 case Type::Boolean: b = other.b; break;
240 case Type::StringLiteral: r = other.r; break;
241 case Type::Function: f = other.f; break;
242 case Type::EffFunction: e = other.e; break;
243 case Type::RawPointer: u = other.u; break;
245 case Type::String: new (&s) managed<std::string>(move_if_rvalue<T>(other.s)); break;
246 case Type::Map: new (&m) managed<map>(move_if_rvalue<T>(other.m)); break;
247 case Type::Data: new (&v) managed<data>(move_if_rvalue<T>(other.v)); break;
248 case Type::Array: new (&a) managed<array>(move_if_rvalue<T>(other.a)); break;
249 case Type::Closure: new (&l) managed<closure>(move_if_rvalue<T>(other.l)); break;
250 case Type::EffClosure: new (&k) managed<eff_closure>(move_if_rvalue<T>(other.k)); break;
251 case Type::Pointer: new (&p) managed<void>(move_if_rvalue<T>(other.p)); break;
253 default: assert(false && "Bad 'any' type"); break;
254 }
255 }
257 auto destruct() -> void {
258 switch (type) {
259 case Type::String: s.~managed<std::string>(); break;
260 case Type::Map: m.~managed<map>(); break;
261 case Type::Data: v.~managed<data>(); break;
262 case Type::Array: a.~managed<array>(); break;
263 case Type::Closure: l.~managed<closure>(); break;
264 case Type::EffClosure: k.~managed<eff_closure>(); break;
265 case Type::Pointer: p.~managed<void>(); break;
267 default: break;
268 }
269 }
271 public:
272 any(const any& other) noexcept : type(other.type) {
273 assign(other);
274 }
276 any(any&& other) noexcept : type(other.type) {
277 assign(std::move(other));
278 }
280 auto operator=(const any& rhs) noexcept -> any& {
281 destruct();
282 type = rhs.type;
283 assign(rhs);
284 return *this;
285 }
287 auto operator=(any&& rhs) noexcept -> any& {
288 destruct();
289 type = rhs.type;
290 assign(std::move(rhs));
291 return *this;
292 }
294 ~any() {
295 destruct();
296 }
297#endif // !defined(USE_GC)
299 public:
300 any() = delete;
302 auto operator()(const any&) const -> any;
303 auto operator()(const as_thunk) const -> const any&;
304 auto operator()() const -> any;
306 operator int() const;
307 operator double() const;
308 operator bool() const;
309 operator char() const;
310 operator cstring() const;
311 operator const map&() const;
312 operator const data&() const;
313 operator const array&() const;
315 auto operator[](const char[]) const -> const any&;
316 auto operator[](const size_t) const -> const any&;
317 auto operator[](const any&) const -> const any&;
319 auto contains(const char[]) const -> bool;
321 auto extractPointer() const -> void*;
323 auto rawPointer() const -> void* {
324 const any& variant = unthunkVariant(*this);
325 assert(type == Type::RawPointer);
326 return variant.u;
327 }
329 static auto unthunkVariant(const any&) -> const any&;
331 #define DEFINE_OPERATOR(op, ty, rty) \
332 inline friend auto operator op (const any& lhs, ty rhs) -> rty { \
333 return (ty)lhs op rhs; \
334 } \
335 inline friend auto operator op (ty lhs, const any& rhs) -> rty { \
336 return lhs op (ty)rhs; \
337 } \
339 #define DECLARE_COMPARISON_OPERATOR(op) \
340 friend auto operator op (const any&, const any&) -> bool; \
341 DEFINE_OPERATOR(op, int, bool) \
342 DEFINE_OPERATOR(op, double, bool) \
343 DEFINE_OPERATOR(op, char, bool) \
344 DEFINE_OPERATOR(op, bool, bool) \
345 friend auto operator op (const any&, const char * const) -> bool; \
346 friend auto operator op (const char * const, const any&) -> bool;
348 DECLARE_COMPARISON_OPERATOR(==)
349 DECLARE_COMPARISON_OPERATOR(!=)
350 DECLARE_COMPARISON_OPERATOR(<)
351 DECLARE_COMPARISON_OPERATOR(<=)
352 DECLARE_COMPARISON_OPERATOR(>)
353 DECLARE_COMPARISON_OPERATOR(>=)
355 friend auto operator+(const any&, const any&) -> any;
357 DEFINE_OPERATOR(+, int, int)
358 DEFINE_OPERATOR(+, double, double)
359 DEFINE_OPERATOR(+, char, char)
360 friend auto operator+(const any& lhs, const char * const rhs) -> std::string;
361 friend auto operator+(const char * const lhs, const any& rhs) -> std::string;
363 friend auto operator-(const any&, const any&) -> any;
365 DEFINE_OPERATOR(-, int, int)
366 DEFINE_OPERATOR(-, double, double)
367 DEFINE_OPERATOR(-, char, char)
369 friend auto operator*(const any&, const any&) -> any;
371 DEFINE_OPERATOR(*, int, int)
372 DEFINE_OPERATOR(*, double, double)
373 DEFINE_OPERATOR(*, char, char)
375 friend auto operator/(const any&, const any&) -> any;
377 DEFINE_OPERATOR(/, int, int)
378 DEFINE_OPERATOR(/, double, double)
379 DEFINE_OPERATOR(/, char, char)
381 friend auto operator%(const any&, const any&) -> any;
383 DEFINE_OPERATOR(%, int, int)
384 DEFINE_OPERATOR(%, char, char)
386 friend auto operator-(const any&) -> any; // unary negate
387};
389template <typename T>
390inline auto cast(const any& a) ->
391 typename std::enable_if<std::is_arithmetic<T>::value ||
392 std::is_same<T,cstring>::value, T>::type {
393 return a;
396template <typename T>
397inline auto cast(const any& a) ->
398 typename std::enable_if<std::is_same<T, any::map>::value ||
399 std::is_same<T, any::data>::value ||
400 std::is_same<T, any::array>::value, const T&>::type {
401 return a;
404template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
405inline auto cast(const any& a) ->
406 typename std::enable_if<!std::is_same<T, any::map>::value &&
407 !std::is_same<T, any::data>::value &&
408 !std::is_same<T, any::array>::value, T&>::type {
409 return *static_cast<T*>(a.extractPointer());
412template <typename T>
413inline auto cast(const any& a) ->
414 typename std::enable_if<std::is_same<void*, T>::value, T>::type {
415 return a.rawPointer();
418} // namespace PureScript
420#undef WITH_ALLOCATOR
421#undef IS_POINTER_TYPE
422#undef DEFINE_OPERATOR
423#undef DECLARE_COMPARISON_OPERATOR
425#endif // PureScript_HH