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.
108 static constexpr as_thunk unthunk = as_thunk{};
110 using map_pair = std::pair<const Private::Symbol, const any>;
113 using map = std::array<const map_pair, N>;
116 using data = std::array<const any, N>;
118 using array = std::deque<any WITH_ALLOCATOR(any)>;
119 using fn = auto (*)(const any&) -> any;
120 using eff_fn = auto (*)() -> any;
121 using thunk = auto (*)(const as_thunk) -> const any&;
126 virtual auto operator()(const any&) const -> any = 0;
130 template <typename T>
131 class Closure_ : public Closure {
134 Closure_(const T& l) noexcept : lambda(l) {}
135 auto operator()(const any& arg) const -> any override {
142 virtual auto operator()() const -> any = 0;
143 virtual ~EffClosure();
146 template <typename T>
147 class EffClosure_ : public EffClosure {
150 EffClosure_(const T& l) noexcept : lambda(l) {}
151 auto operator()() const -> any override {
167 mutable managed<string> s;
168 mutable managed<array> a;
169 mutable managed<Closure> l;
170 mutable managed<EffClosure> k;
171 mutable managed<void> p;
176 any(const int val) noexcept : tag(Tag::Integer), i(val) {}
177 any(const long val) noexcept : tag(Tag::Integer), i(static_cast<decltype(i)>(val)) {
178 assert(val >= std::numeric_limits<decltype(i)>::min() &&
179 val <= std::numeric_limits<decltype(i)>::max());
182 any(const double val) noexcept : tag(Tag::Double), d(val) {}
183 any(const char val) noexcept : tag(Tag::Character), c(val) {}
184 any(const char32_t val) noexcept : tag(Tag::Integer), i(val) {}
186 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
187 any(const T val) noexcept : tag(Tag::Boolean), b(val) {}
189 any(const char * val) : tag(Tag::String), s(make_managed<string>(val)) {}
190 any(const string& val) : tag(Tag::String), s(make_managed<string>(val)) {}
191 any(string&& val) noexcept : tag(Tag::String), s(make_managed<string>(std::move(val))) {}
193 any(const managed<string>& val) noexcept : tag(Tag::String), s(val) {}
194 any(managed<string>&& val) noexcept : tag(Tag::String), s(std::move(val)) {}
197 any(map<N>&& val) noexcept : tag(Tag::Map), p(make_managed<map<N>>(std::move(val))) {}
200 any(data<N>&& val) noexcept : tag(Tag::Data), p(make_managed<data<N>>(std::move(val))) {}
202 any(const array& val) : tag(Tag::Array), a(make_managed<array>(val)) {}
203 any(array&& val) noexcept : tag(Tag::Array), a(make_managed<array>(std::move(val))) {}
205 template <typename T>
206 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0) noexcept
207 : tag(Tag::Function), f(val) {}
209 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
210 !std::is_convertible<T,fn>::value>::type>
211 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
212 : tag(Tag::Closure), l(make_managed<Closure_<T>>(val)) {}
214 template <typename T>
215 any(const T& val, typename std::enable_if<std::is_convertible<T,eff_fn>::value>::type* = 0) noexcept
216 : tag(Tag::EffFunction), e(val) {}
218 template <typename T,
219 typename = typename std::enable_if<!std::is_same<any,T>::value &&
220 !std::is_convertible<T,eff_fn>::value>::type>
221 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
222 : tag(Tag::EffClosure), k(make_managed<EffClosure_<T>>(val)) {}
224 template <typename T>
225 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0) noexcept
226 : tag(Tag::Thunk), t(val) {}
228 template <typename T,
229 typename = typename std::enable_if<std::is_class<typename std::remove_pointer<T>::type>::value>::type>
230 any(const T& val, typename std::enable_if<IS_POINTER_TYPE(T)::value>::type* = 0) noexcept
231 : tag(Tag::Pointer), p(val) {}
233 template <typename T>
234 any(T&& val, typename std::enable_if<std::is_assignable<managed<void>,T>::value>::type* = 0) noexcept
235 : tag(Tag::Pointer), p(std::move(val)) {}
237 // Explicit void* to raw pointer value
238 template <typename T>
239 any(const T& val, typename std::enable_if<std::is_same<T,void*>::value>::type* = 0) noexcept
240 : tag(Tag::RawPointer), v(val) {}
242 any(nullptr_t) noexcept : tag(Tag::RawPointer), v(nullptr) {}
247 template <typename T,
249 typename = typename std::enable_if<!std::is_reference<T>::value>::type>
250 static constexpr auto move_if_rvalue(U&& u) noexcept -> U&& {
251 return static_cast<U&&>(u);
254 template <typename T,
256 typename = typename std::enable_if<std::is_reference<T>::value>::type>
257 static constexpr auto move_if_rvalue(U& u) noexcept -> U& {
258 return static_cast<U&>(u);
261 template <typename T>
262 auto assign(T&& other) const noexcept -> void {
264 case Tag::Thunk: t = other.t; break;
265 case Tag::Integer: i = other.i; break;
266 case Tag::Double: d = other.d; break;
267 case Tag::Character: c = other.c; break;
268 case Tag::Boolean: b = other.b; break;
269 case Tag::StringLiteral: r = other.r; break;
270 case Tag::Function: f = other.f; break;
271 case Tag::EffFunction: e = other.e; break;
272 case Tag::RawPointer: v = other.v; break;
274 case Tag::String: new (&s) managed<string>(move_if_rvalue<T>(other.s)); break;
275 case Tag::Array: new (&a) managed<array>(move_if_rvalue<T>(other.a)); break;
276 case Tag::Closure: new (&l) managed<Closure>(move_if_rvalue<T>(other.l)); break;
277 case Tag::EffClosure: new (&k) managed<EffClosure>(move_if_rvalue<T>(other.k)); break;
280 case Tag::Pointer: new (&p) managed<void>(move_if_rvalue<T>(other.p)); break;
282 default: assert(false && "Bad 'any' tag"); break;
286 auto destruct() -> void {
288 case Tag::String: s.~managed<string>(); break;
289 case Tag::Array: a.~managed<array>(); break;
290 case Tag::Closure: l.~managed<Closure>(); break;
291 case Tag::EffClosure: k.~managed<EffClosure>(); break;
294 case Tag::Pointer: p.~managed<void>(); break;
301 any(const any& other) noexcept : tag(other.tag) {
305 any(any&& other) noexcept : tag(other.tag) {
306 assign(std::move(other));
309 auto operator=(const any& rhs) noexcept -> any& {
316 auto operator=(any&& rhs) noexcept -> any& {
319 assign(std::move(rhs));
326#endif // !defined(USE_GC)
331 auto operator()(const any&) const -> any;
332 auto operator()(const as_thunk) const -> const any&;
333 auto operator()() const -> any;
335 operator int() const;
336 operator double() const;
337 operator bool() const;
338 operator char() const;
339 operator size_t() const;
340 operator const char *() const;
341 operator const array&() const;
343 auto operator[](const size_t) const -> const any&;
345 auto size() const -> size_t;
346 auto empty() const -> bool;
347 auto contains(const Private::Symbol) const -> bool;
349 auto extractPointer(IF_DEBUG(const Tag)) const -> void*;
351 auto rawPointer() const -> void* {
352 const any& variant = unthunkVariant(*this);
353 assert(tag == Tag::RawPointer);
357 static auto unthunkVariant(const any&) -> const any&;
359 #define DEFINE_OPERATOR(op) \
360 template <typename T> \
361 inline friend auto operator op (const any& lhs, T rhs) -> T { \
362 return static_cast<T>(lhs) op rhs; \
364 template <typename T> \
365 inline friend auto operator op (T lhs, const any& rhs) -> T { \
366 return lhs op static_cast<T>(rhs); \
368 friend auto operator op (const any&, const any&) -> any;
370 #define DEFINE_COMPARISON_OPERATOR(op) \
371 template <typename T> \
372 inline friend auto operator op (const any& lhs, T rhs) -> bool { \
373 return static_cast<T>(lhs) op rhs; \
375 template <typename T> \
376 inline friend auto operator op (T lhs, const any& rhs) -> bool { \
377 return lhs op static_cast<T>(rhs); \
379 friend auto operator op (const any&, const any&) -> bool; \
380 friend auto operator op (const any&, const char * const) -> bool; \
381 friend auto operator op (const char * const, const any&) -> bool;
383 DEFINE_COMPARISON_OPERATOR(==)
384 DEFINE_COMPARISON_OPERATOR(!=)
385 DEFINE_COMPARISON_OPERATOR(<)
386 DEFINE_COMPARISON_OPERATOR(<=)
387 DEFINE_COMPARISON_OPERATOR(>)
388 DEFINE_COMPARISON_OPERATOR(>=)
396 friend auto operator+(const any& lhs, const char * const rhs) -> string;
397 friend auto operator+(const char * const lhs, const any& rhs) -> string;
399 friend auto operator-(const any&) -> any; // unary negate
404 template <typename T, typename U=void>
408 struct TagHelper<any::map<N>> {
409 static constexpr any::Tag tag = any::Tag::Map;
413 struct TagHelper<any::data<N>> {
414 static constexpr any::Tag tag = any::Tag::Data;
417 template <typename T>
418 struct TagHelper<T> {
419 static constexpr any::Tag tag = any::Tag::Pointer;
425constexpr auto cast(const T& a) -> const T& {
430inline auto cast(const any& a) ->
431 typename std::enable_if<std::is_arithmetic<T>::value ||
432 std::is_same<T,const char *>::value, T>::type {
437inline auto cast(const any& a) ->
438 typename std::enable_if<std::is_same<T, any::array>::value, const T&>::type {
442template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
443inline auto cast(const any& a) ->
444 typename std::enable_if<!std::is_same<T, any::array>::value, T&>::type {
445 return *static_cast<T*>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
449 typename = typename std::enable_if<std::is_pointer<T>::value &&
450 !std::is_same<T,void*>::value &&
451 !std::is_same<T,const char *>::value>::type>
452inline auto cast(const any& a) -> T {
453 return static_cast<T>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
457inline auto cast(const any& a) ->
458 typename std::enable_if<std::is_same<void*, T>::value, T>::type {
459 return a.rawPointer();
463 template <size_t N, typename T>
464 inline auto get(const T& a) ->
465 typename std::enable_if<!std::is_same<any, T>::value, const any&>::type {
470 inline auto get(const any& a) -> const any& {
471 return cast<any::map<N+1>>(a)[N].second;
475 inline auto get(const Private::Symbol key, const any::map<N>& a) -> const any& {
476 static_assert(N > 0, "map size must be greater than zero");
477 typename std::remove_reference<decltype(a)>::type::size_type i = 0;
479 if (a[i].first == key) {
482 } while (a[++i].first != nullptr);
483 assert(false && "map key not found");
484 static const any invalid_key(nullptr);
488 inline auto get(const Private::Symbol key, const any& a) -> const any& {
489 return get(key, cast<any::map<unknown_size>>(a));
492 #define P(N) any::map_pair&& arg ## N
493 #define T(N) any::map<N+1>
494 #define E(N) std::forward<decltype(arg ## N)>(arg ## N)
495 #define M(...) {{ __VA_ARGS__, { nullptr, nullptr } }}
497 inline auto make(P(1)) -> T(1) {
500 inline auto make(P(1), P(2)) -> T(2) {
501 return T(2) M(E(1), E(2));
503 inline auto make(P(1), P(2), P(3)) -> T(3) {
504 return T(3) M(E(1), E(2), E(3));
506 inline auto make(P(1), P(2), P(3), P(4)) -> T(4) {
507 return T(4) M(E(1), E(2), E(3), E(4));
509 inline auto make(P(1), P(2), P(3), P(4), P(5)) -> T(5) {
510 return T(5) M(E(1), E(2), E(3), E(4), E(5));
512 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6)) -> T(6) {
513 return T(6) M(E(1), E(2), E(3), E(4), E(5), E(6));
515 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6), P(7)) -> T(7) {
516 return T(7) M(E(1), E(2), E(3), E(4), E(5), E(6), E(7));
518 inline auto make(P(1), P(2), P(3), P(4), P(5), P(6), P(7), P(8)) -> T(8) {
519 return T(8) M(E(1), E(2), E(3), E(4), E(5), E(6), E(7), E(8));
528namespace record = map;
531 template <size_t N, typename T>
532 inline auto get(const T& a) ->
533 typename std::enable_if<!std::is_same<any, T>::value, const any&>::type {
538 inline auto get(const any& a) -> const any& {
539 return cast<any::data<N+1>>(a)[N];
542 template <typename T>
543 inline auto ctor(const T& a) -> int {
548} // namespace PureScript
551#undef IS_POINTER_TYPE
552#undef DEFINE_OPERATOR
553#undef DEFINE_COMPARISON_OPERATOR
555#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