git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge remote-tracking branch 'pure11/pure11' into native
commitd3bc249882ec0b15bd8537a4df4dcbac13d3ee80 authorAndy Arvanitis <andyarvanitis@gmail.com> dateThu 2 Mar 2017, 15:20 (10 years ago) parent6a9cb4399b2705
3 files changed+23−72
pcc/runtime/purescript.cc+3−2 pcc/runtime/purescript.hh+19−69 src/Language/PureScript/Pretty/Cpp.hs+1−1
pcc/runtime/purescript.cc+3−2
@@ -119,7 +119,8 @@ auto any::empty() const -> bool {
119119
120120auto any::contains(const Private::Symbol key) const -> bool {
121121 // TODO: assumes at least one element -- safe assumption?
122− const auto& m = cast<dict<unknown_size>>(*this);
122+ const auto& m =
123+ *static_cast<const any::dict<unknown_size>*>(this->extractPointer(IF_DEBUG(any::Tag::Dictionary)));
123124 std::remove_reference<decltype(m)>::type::size_type i = 0;
124125 do {
125126 if (m[i].first == key) {
@@ -151,7 +152,7 @@ auto any::at(const char * key) const -> const any& {
151152 case any::Tag::Boolean: return lhs.b op static_cast<decltype(any::b)>(rhs); \
152153 case any::Tag::String: return *static_cast<const string*>(POINTER_FROM_MEMBER(lhs.p)) \
153154 op static_cast<const string&>(rhs); \
154− case any::Tag::RawPointer: return lhs.v op cast<decltype(any::v)>(rhs); \
155+ case any::Tag::RawPointer: return lhs.v op static_cast<decltype(any::v)>(rhs); \
155156 case any::Tag::Pointer: return lhs.p op any::unthunkVariant(rhs).p; \
156157 default: assert(false && "Unsupported tag for operator " #op); \
157158 } \
pcc/runtime/purescript.hh+19−69
@@ -44,7 +44,7 @@
4444
4545namespace PureScript {
4646
47−using cstring = const char *;
47+using cstring = const char *; // TODO: deprecated
4848using std::string;
4949using std::nullptr_t;
5050using std::runtime_error;
@@ -193,6 +193,9 @@ class any {
193193 assert(val >= std::numeric_limits<decltype(i)>::min() &&
194194 val <= std::numeric_limits<decltype(i)>::max());
195195 }
196+ any(const size_t val) noexcept : tag(Tag::Integer), i(static_cast<decltype(i)>(val)) {
197+ assert(val <= std::numeric_limits<decltype(i)>::max());
198+ }
196199 any(const double val) noexcept : tag(Tag::Double), d(val) {}
197200 any(const char32_t val) noexcept : tag(Tag::Character), c(val) {}
198201 any(const char val) noexcept : tag(Tag::Character), c(val) {}
@@ -333,14 +336,14 @@ class any {
333336 operator const record&() const;
334337
335338 operator void *() const {
336− return extractPointer(IF_DEBUG(Tag::RawPointer));
339+ return rawPointer();
337340 }
338341
339342 template <typename T,
340343 typename = typename std::enable_if<std::is_pointer<T>::value &&
341344 std::is_class<typename std::remove_pointer<T>::type>::value>::type>
342345 operator T() const {
343− return static_cast<T>(extractPointer(IF_DEBUG(Tag::RawPointer)));
346+ return static_cast<T>(rawPointer());
344347 }
345348
346349 auto operator[](const size_t) const -> const any&;
@@ -419,77 +422,24 @@ class any {
419422
420423}; // class any
421424
422−namespace Private {
423− template <typename T, typename U=void>
424− struct TagHelper {};
425−
426− template <>
427− struct TagHelper<string> {
428− static constexpr any::Tag tag = any::Tag::String;
429− };
430−
431− template <>
432− struct TagHelper<const char *> {
433− static constexpr any::Tag tag = any::Tag::String;
434− };
435−
436− template <size_t N>
437− struct TagHelper<any::dict<N>> {
438− static constexpr any::Tag tag = any::Tag::Dictionary;
439− };
440−
441− template <size_t N>
442− struct TagHelper<any::data<N>> {
443− static constexpr any::Tag tag = any::Tag::Data;
444− };
445−
446− template <>
447− struct TagHelper<any::array> {
448− static constexpr any::Tag tag = any::Tag::Array;
449− };
450−
451− template <>
452− struct TagHelper<any::record> {
453− static constexpr any::Tag tag = any::Tag::Record;
454− };
455−
456− template <typename T>
457− struct TagHelper<T> {
458− static constexpr any::Tag tag = any::Tag::Pointer;
459− };
460−}
461−
462−// Pass-through
463−template <typename T>
464−constexpr auto cast(const T& a) -> const T& {
465− return a;
466−}
467−
468−template <typename T>
469−inline auto cast(const any& a) ->
470− typename std::enable_if<std::is_arithmetic<T>::value ||
471− std::is_same<T,const char *>::value, T>::type {
472− return a;
473−}
474−
475425template <typename T,
476426 typename = typename std::enable_if<std::is_class<T>::value>::type>
477−inline auto cast(const any& a) -> T& {
478− return *static_cast<T*>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
427+inline auto cast_managed(const any& a) -> T& {
428+ return *static_cast<T*>(a.extractPointer(IF_DEBUG(any::Tag::Pointer)));
479429}
480430
431+// TODO: Deprecated - use static_cast
481432template <typename T,
482− typename = typename std::enable_if<std::is_pointer<T>::value &&
483− !std::is_same<T,void*>::value &&
484− !std::is_same<T,const char *>::value>::type>
433+ typename = typename std::enable_if<!std::is_class<T>::value>::type>
485434inline auto cast(const any& a) -> T {
486− return static_cast<T>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
435+ return static_cast<T>(a);
487436}
488437
489−template <typename T>
490−inline auto cast(const any& a) ->
491− typename std::enable_if<std::is_same<void*, T>::value, T>::type {
492− return a.rawPointer();
438+// TODO: Deprecated - use cast_managed
439+template <typename T,
440+ typename = typename std::enable_if<std::is_class<T>::value>::type>
441+inline auto cast(const any& a) -> T& {
442+ return cast_managed<T>(a);
493443}
494444
495445namespace dict {
@@ -501,7 +451,7 @@ namespace dict {
501451
502452 template <size_t N>
503453 inline auto get(const any& a) -> const any& {
504− return cast<any::dict<N+1>>(a)[N].second;
454+ return (*static_cast<const any::dict<N+1>*>(a.extractPointer(IF_DEBUG(any::Tag::Dictionary))))[N].second;
505455 }
506456
507457 template <size_t N>
@@ -519,7 +469,7 @@ namespace dict {
519469 }
520470
521471 inline auto get(const Private::Symbol key, const any& a) -> const any& {
522− return get(key, cast<any::dict<unknown_size>>(a));
472+ return get(key, *static_cast<const any::dict<unknown_size>*>(a.extractPointer(IF_DEBUG(any::Tag::Dictionary))));
523473 }
524474
525475 #define P(N) any::dict_pair&& arg ## N
@@ -567,7 +517,7 @@ namespace data {
567517
568518 template <size_t N>
569519 inline auto get(const any& a) -> const any& {
570− return cast<any::data<N+1>>(a)[N];
520+ return (*static_cast<const any::data<N+1>*>(a.extractPointer(IF_DEBUG(any::Tag::Data))))[N];
571521 }
572522
573523 template <typename T>
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -268,7 +268,7 @@ literals = mkPattern' match
268268 CppNumericLiteral {} -> vstr
269269 CppStringLiteral {} -> vstr
270270 CppBooleanLiteral {} -> vstr
271− _ -> "cast" <> angles (runType typ) <> parensT val'
271+ _ -> "static_cast" <> angles (runType typ) <> parensT val'
272272 where
273273 vstr = prettyPrintCpp1 val
274274 val' | "(" `T.isInfixOf` vstr || "[" `T.isInfixOf` vstr = parensT vstr