1///////////////////////////////////////////////////////////////////////////////
3// Module : purescript.hh
4// Copyright : (c) Andy Arvanitis 2015
7// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8// Stability : experimental
11// Basic types and functions to support purescript-to-C++1x rendering
13///////////////////////////////////////////////////////////////////////////////
18#if !defined(__cplusplus)
19#error This is a C++ file!
41#include <iso646.h> // mostly for MS Visual Studio compiler
42#include "purescript_memory.hh"
46using cstring = const char *;
49using std::runtime_error;
52 struct SymbolGeneratorAnchor {};
55 struct SymbolGenerator {
56 constexpr static SymbolGeneratorAnchor anchor = SymbolGeneratorAnchor{};
59 constexpr SymbolGeneratorAnchor SymbolGenerator<T>::anchor;
61 using Symbol = const SymbolGeneratorAnchor *;
64#define define_symbol(S) namespace PureScript { \
71#define symbol(S) (&Private::SymbolGenerator<::PureScript::Private::Symbols::S_ ## S>::anchor)
73constexpr bool undefined = false;
75// TODO: Not a real limit, just used for simpler accessors
76static constexpr size_t unknown_size = 64;
78// A variant data class designed to provide some features of dynamic typing.
107 static constexpr as_thunk unthunk = as_thunk{};
109 using map_pair = std::pair<const Private::Symbol, const any>;
112 using map = std::array<const map_pair, N>;
115 using data = std::array<const any, N>;
117 using array = std::deque<any WITH_ALLOCATOR(any)>;
118 using fn = auto (*)(const any&) -> any;
119 using eff_fn = auto (*)() -> any;
120 using thunk = auto (*)(const as_thunk) -> const any&;
125 virtual auto operator()(const any&) const -> any = 0;
129 template <typename T>
130 class Closure_ : public Closure {
133 Closure_(const T& l) noexcept : lambda(l) {}
134 auto operator()(const any& arg) const -> any override {
141 virtual auto operator()() const -> any = 0;
142 virtual ~EffClosure();
145 template <typename T>
146 class EffClosure_ : public EffClosure {
149 EffClosure_(const T& l) noexcept : lambda(l) {}
150 auto operator()() const -> any override {
165 mutable managed<string> s;
166 mutable managed<array> a;
167 mutable managed<Closure> l;
168 mutable managed<EffClosure> k;
169 mutable managed<void> p;
174 any(const int val) noexcept : tag(Tag::Integer), i(val) {}
175 any(const long val) noexcept : tag(Tag::Integer), i(static_cast<decltype(i)>(val)) {
176 assert(val >= std::numeric_limits<decltype(i)>::min() &&
177 val <= std::numeric_limits<decltype(i)>::max());
179 any(const double val) noexcept : tag(Tag::Double), d(val) {}
180 any(const char32_t val) noexcept : tag(Tag::Character), c(val) {}
181 any(const char val) noexcept : tag(Tag::Character), c(val) {}
183 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
184 any(const T val) noexcept : tag(Tag::Boolean), b(val) {}
186 any(const char * val) : tag(Tag::String), s(make_managed<string>(val)) {}
187 any(const string& val) : tag(Tag::String), s(make_managed<string>(val)) {}
188 any(string&& val) noexcept : tag(Tag::String), s(make_managed<string>(std::move(val))) {}
190 any(const managed<string>& val) noexcept : tag(Tag::String), s(val) {}
191 any(managed<string>&& val) noexcept : tag(Tag::String), s(std::move(val)) {}
194 any(map<N>&& val) noexcept : tag(Tag::Map), p(make_managed<map<N>>(std::move(val))) {}
197 any(data<N>&& val) noexcept : tag(Tag::Data), p(make_managed<data<N>>(std::move(val))) {}
199 any(const array& val) : tag(Tag::Array), a(make_managed<array>(val)) {}
200 any(array&& val) noexcept : tag(Tag::Array), a(make_managed<array>(std::move(val))) {}
202 template <typename T>
203 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0) noexcept
204 : tag(Tag::Function), f(val) {}
206 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
207 !std::is_convertible<T,fn>::value>::type>
208 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
209 : tag(Tag::Closure), l(make_managed<Closure_<T>>(val)) {}
211 template <typename T>
212 any(const T& val, typename std::enable_if<std::is_convertible<T,eff_fn>::value>::type* = 0) noexcept
213 : tag(Tag::EffFunction), e(val) {}
215 template <typename T,
216 typename = typename std::enable_if<!std::is_same<any,T>::value &&
217 !std::is_convertible<T,eff_fn>::value>::type>
218 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
219 : tag(Tag::EffClosure), k(make_managed<EffClosure_<T>>(val)) {}
221 template <typename T>
222 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0) noexcept
223 : tag(Tag::Thunk), t(val) {}
225 template <typename T,
226 typename = typename std::enable_if<std::is_class<typename std::remove_pointer<T>::type>::value>::type>
227 any(const T& val, typename std::enable_if<IS_POINTER_TYPE(T)::value>::type* = 0) noexcept
228 : tag(Tag::Pointer), p(val) {}
230 template <typename T>
231 any(T&& val, typename std::enable_if<std::is_assignable<managed<void>,T>::value>::type* = 0) noexcept
232 : tag(Tag::Pointer), p(std::move(val)) {}
234 // Explicit void* to raw pointer value
235 template <typename T>
236 any(const T& val, typename std::enable_if<std::is_same<T,void*>::value>::type* = 0) noexcept
237 : tag(Tag::RawPointer), v(val) {}
239 any(nullptr_t) noexcept : tag(Tag::RawPointer), v(nullptr) {}
244 template <typename T,
246 typename = typename std::enable_if<!std::is_reference<T>::value>::type>
247 static constexpr auto move_if_rvalue(U&& u) noexcept -> U&& {
248 return static_cast<U&&>(u);
251 template <typename T,
253 typename = typename std::enable_if<std::is_reference<T>::value>::type>
254 static constexpr auto move_if_rvalue(U& u) noexcept -> U& {
255 return static_cast<U&>(u);
258 template <typename T>
259 auto assign(T&& other) const noexcept -> void {
261 case Tag::Thunk: t = other.t; break;
262 case Tag::Integer: i = other.i; break;
263 case Tag::Double: d = other.d; break;
264 case Tag::Character: c = other.c; break;
265 case Tag::Boolean: b = other.b; break;
266 case Tag::Function: f = other.f; break;
267 case Tag::EffFunction: e = other.e; break;
268 case Tag::RawPointer: v = other.v; break;
270 case Tag::String: new (&s) managed<string>(move_if_rvalue<T>(other.s)); break;
271 case Tag::Array: new (&a) managed<array>(move_if_rvalue<T>(other.a)); break;
272 case Tag::Closure: new (&l) managed<Closure>(move_if_rvalue<T>(other.l)); break;
273 case Tag::EffClosure: new (&k) managed<EffClosure>(move_if_rvalue<T>(other.k)); break;
276 case Tag::Pointer: new (&p) managed<void>(move_if_rvalue<T>(other.p)); break;
278 default: assert(false && "Bad 'any' tag"); break;
282 auto destruct() -> void {
284 case Tag::String: s.~managed<string>(); break;
285 case Tag::Array: a.~managed<array>(); break;
286 case Tag::Closure: l.~managed<Closure>(); break;
287 case Tag::EffClosure: k.~managed<EffClosure>(); break;
290 case Tag::Pointer: p.~managed<void>(); break;
297 any(const any& other) noexcept : tag(other.tag) {
301 any(any&& other) noexcept : tag(other.tag) {
302 assign(std::move(other));
305 auto operator=(const any& rhs) noexcept -> any& {
312 auto operator=(any&& rhs) noexcept -> any& {
315 assign(std::move(rhs));
322#endif // !defined(USE_GC)
327 auto operator()(const any&) const -> any;
328 auto operator()(const as_thunk) const -> const any&;
329 auto operator()() const -> any;
331 operator int() const;
332 operator double() const;
333 operator bool() const;
334 operator char32_t() const;
335 operator char() const;
336 operator size_t() const;
337 operator const string&() const;
338 operator const char *() const;
339 operator const array&() const;
341 auto operator[](const size_t) const -> const any&;
343 auto size() const -> size_t;
344 auto empty() const -> bool;
345 auto contains(const Private::Symbol) const -> bool;
347 auto extractPointer(IF_DEBUG(const Tag)) const -> void*;
349 auto rawPointer() const -> void* {
350 const any& variant = unthunkVariant(*this);
351 assert(tag == Tag::RawPointer);
355 static auto unthunkVariant(const any&) -> const any&;
357 #define DEFINE_OPERATOR(op) \
358 template <typename T, \
359 typename = typename std::enable_if<!std::is_same<T,any>::value && \
360 !std::is_same<T,const char *>::value>::type> \
361 inline friend auto operator op (const any& lhs, T rhs) -> T { \
362 return static_cast<typename std::add_lvalue_reference<typename std::add_const<T>::type >::type>(lhs) op rhs; \
364 template <typename T, \
365 typename = typename std::enable_if<!std::is_same<T,any>::value && \
366 !std::is_same<T,const char *>::value>::type> \
367 inline friend auto operator op (T lhs, const any& rhs) -> T { \
368 return lhs op static_cast<typename std::add_lvalue_reference<typename std::add_const<T>::type >::type>(rhs); \
370 friend auto operator op (const any&, const any&) -> any; \
372 #define DEFINE_COMPARISON_OPERATOR(op) \
373 template <typename T, \
374 typename = typename std::enable_if<!std::is_same<T,any>::value && \
375 !std::is_same<T,const char *>::value>::type> \
376 inline friend auto operator op (const any& lhs, T rhs) -> bool { \
377 return static_cast<typename std::add_lvalue_reference<typename std::add_const<T>::type >::type>(lhs) op rhs; \
379 template <typename T, \
380 typename = typename std::enable_if<!std::is_same<T,any>::value && \
381 !std::is_same<T,const char *>::value>::type> \
382 inline friend auto operator op (T lhs, const any& rhs) -> bool { \
383 return lhs op static_cast<typename std::add_lvalue_reference<typename std::add_const<T>::type >::type>(rhs); \
385 inline friend auto operator op (const any& lhs, const char * rhs) -> bool { \
386 return static_cast<const string&>(lhs) op rhs; \
388 inline friend auto operator op (const char * lhs, const any& rhs) -> bool { \
389 return lhs op static_cast<const string&>(rhs); \
391 friend auto operator op (const any&, const any&) -> bool; \
393 DEFINE_COMPARISON_OPERATOR(==)
394 DEFINE_COMPARISON_OPERATOR(!=)
395 DEFINE_COMPARISON_OPERATOR(<)
396 DEFINE_COMPARISON_OPERATOR(<=)
397 DEFINE_COMPARISON_OPERATOR(>)
398 DEFINE_COMPARISON_OPERATOR(>=)
406 inline friend auto operator+(const any& lhs, const char * rhs) -> string {
407 return static_cast<const string&>(lhs) + rhs;
409 inline friend auto operator+(const char * lhs, const any& rhs) -> string {
410 return lhs + static_cast<const string&>(rhs);
413 friend auto operator-(const any&) -> any; // unary negate
418 template <typename T, typename U=void>
422 struct TagHelper<any::map<N>> {
423 static constexpr any::Tag tag = any::Tag::Map;
427 struct TagHelper<any::data<N>> {
428 static constexpr any::Tag tag = any::Tag::Data;
431 template <typename T>
432 struct TagHelper<T> {
433 static constexpr any::Tag tag = any::Tag::Pointer;
439constexpr auto cast(const T& a) -> const T& {
444inline auto cast(const any& a) ->
445 typename std::enable_if<std::is_arithmetic<T>::value ||
446 std::is_same<T,const char *>::value, T>::type {
451inline auto cast(const any& a) ->
452 typename std::enable_if<std::is_same<T, string>::value ||
453 std::is_same<T, any::array>::value, const T&>::type {
458 typename = typename std::enable_if<std::is_class<T>::value &&
459 !std::is_same<T,string>::value &&
460 !std::is_same<T,any::array>::value>::type>
461inline auto cast(const any& a) -> T& {
462 return *static_cast<T*>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
466 typename = typename std::enable_if<std::is_pointer<T>::value &&
467 !std::is_same<T,void*>::value &&
468 !std::is_same<T,const char *>::value>::type>
469inline auto cast(const any& a) -> T {
470 return static_cast<T>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
474inline auto cast(const any& a) ->
475 typename std::enable_if<std::is_same<void*, T>::value, T>::type {
476 return a.rawPointer();
480 template <size_t N, typename T>
481 inline auto get(const T& a) ->
482 typename std::enable_if<!std::is_same<any, T>::value, const any&>::type {
487 inline auto get(const any& a) -> const any& {
488 return cast<any::map<N+1>>(a)[N].second;
492 inline auto get(const Private::Symbol key, const any::map<N>& a) -> const any& {
493 static_assert(N > 0, "map size must be greater than zero");
494 typename std::remove_reference<decltype(a)>::type::size_type i = 0;
496 if (a[i].first == key) {
499 } while (a[++i].first != nullptr);
500 assert(false && "map key not found");
501 static const any invalid_key(nullptr);
505 inline auto get(const Private::Symbol key, const any& a) -> const any& {
506 return get(key, cast<any::map<unknown_size>>(a));
509 #define P(N) any::map_pair&& arg ## N
510 #define T(N) any::map<N+1>
511 #define E(N) std::forward<decltype(arg ## N)>(arg ## N)
512 #define M(...) {{ __VA_ARGS__, { nullptr, nullptr } }}
514 inline auto make(P(1)) -> T(1) {
517 inline auto make(P(1), P(2)) -> T(2) {
518 return T(2) M(E(1), E(2));
520 inline auto make(P(1), P(2), P(3)) -> T(3) {
521 return T(3) M(E(1), E(2), E(3));
523 inline auto make(P(1), P(2), P(3), P(4)) -> T(4) {
524 return T(4) M(E(1), E(2), E(3), E(4));
526 inline auto make(P(1), P(2), P(3), P(4), P(5)) -> T(5) {
527 return T(5) M(E(1), E(2), E(3), E(4), E(5));
529 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6)) -> T(6) {
530 return T(6) M(E(1), E(2), E(3), E(4), E(5), E(6));
532 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6), P(7)) -> T(7) {
533 return T(7) M(E(1), E(2), E(3), E(4), E(5), E(6), E(7));
535 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6), P(7), P(8)) -> T(8) {
536 return T(8) M(E(1), E(2), E(3), E(4), E(5), E(6), E(7), E(8));
545namespace record = map;
548 template <size_t N, typename T>
549 inline auto get(const T& a) ->
550 typename std::enable_if<!std::is_same<any, T>::value, const any&>::type {
555 inline auto get(const any& a) -> const any& {
556 return cast<any::data<N+1>>(a)[N];
559 template <typename T>
560 inline auto ctor(const T& a) -> int {
565} // namespace PureScript
568#undef IS_POINTER_TYPE
569#undef DEFINE_OPERATOR
570#undef DEFINE_COMPARISON_OPERATOR
572#endif // PureScript_HH
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list