diff options
author | John Hodge <tpg@ucc.asn.au> | 2018-06-03 14:57:05 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2018-06-03 14:57:05 +0800 |
commit | bf8f8b4b4a9fe273451be59f68acafbe61968b83 (patch) | |
tree | 82993550cb3c88de0edbd55d79e4ea8e8cefffac /src/include | |
parent | 39b3cf53798683e496804f8322da2254b10850f4 (diff) | |
parent | a7fb27789a2b34543851d207120e2c0001ee9c27 (diff) | |
download | mrust-bf8f8b4b4a9fe273451be59f68acafbe61968b83.tar.gz |
Merge branch 'master' of https://github.com/thepowersgang/mrustc
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/debug.hpp | 7 | ||||
-rw-r--r-- | src/include/serialise.hpp | 222 | ||||
-rw-r--r-- | src/include/serialiser_texttree.hpp | 67 | ||||
-rw-r--r-- | src/include/span.hpp | 2 | ||||
-rw-r--r-- | src/include/string_view.hpp | 89 | ||||
-rw-r--r-- | src/include/synext_decorator.hpp | 22 |
6 files changed, 108 insertions, 301 deletions
diff --git a/src/include/debug.hpp b/src/include/debug.hpp index 2f593cfb..3f059301 100644 --- a/src/include/debug.hpp +++ b/src/include/debug.hpp @@ -1,4 +1,11 @@ /* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * include/debug.hpp + * - Common compiler debugging macros/helpers + * + * see also src/include/span.hpp */ #pragma once #include <sstream> diff --git a/src/include/serialise.hpp b/src/include/serialise.hpp deleted file mode 100644 index 0d6d781a..00000000 --- a/src/include/serialise.hpp +++ /dev/null @@ -1,222 +0,0 @@ -/* - */ -#ifndef _SERIALSE_HPP_INCLUDED_ -#define _SERIALSE_HPP_INCLUDED_ - -#include <vector> -#include <string> -#include <map> -#include <memory> -#include <stdexcept> - -class Serialiser; -class Deserialiser; - -#define SERIALISABLE_PROTOTYPES()\ - const char* serialise_tag() const override; \ - void serialise(::Serialiser& s) const override; \ - void deserialise(::Deserialiser& s) override -#define SERIALISE_TYPE(method_prefix, tag_str, body, des_body) \ - const char* method_prefix serialise_tag() const { return tag_str; } \ - void method_prefix serialise(::Serialiser& s) const { body } \ - void method_prefix deserialise(::Deserialiser& s) { des_body } -#define SERIALISE_TYPE_A(method_prefix, tag_str, body) SERIALISE_TYPE(method_prefix, tag_str, body, body) -#define SERIALISE_TYPE_S(class_, body) SERIALISE_TYPE(class_::, #class_, body, body) -#define SERIALISE_TU(class_, tag_str, val_name, ...) SERIALISE_TYPE(class_::, tag_str,\ - {\ - s << class_::tag_to_str(this->tag());\ - TU_MATCH(class_, (*this), (val_name), __VA_ARGS__)\ - },/* -*/ {\ - ::std::string STU_tag_str;\ - s.item(STU_tag_str);\ - auto tag = class_::tag_from_str(STU_tag_str);\ - switch(tag) { \ - case class_::TAGDEAD: break;\ - SERIALISE_TU_MATCH_ARMS(class_, val_name, __VA_ARGS__)\ - }\ - }) -#define SERIALISE_TU_MATCH_ARM(CLASS, VAL_NAME, TAG, ...) case CLASS::TAG_##TAG: {/* -*/ *this = CLASS::make_##TAG({});/* -*/ auto& VAL_NAME = this->as_##TAG(); /* -*/ __VA_ARGS__/* -*/} break; -#define SERIALISE_TU_MATCH_ARMS(CLASS, NAME, ...) TU_EXP1( TU_GMA(__VA_ARGS__)(SERIALISE_TU_MATCH_ARM, (CLASS, NAME), __VA_ARGS__) ) - -class DeserialiseFailure: - public ::std::runtime_error -{ - //const char *m_fcn; - //const char *m_message; -public: - DeserialiseFailure(const char *fcn, const char *message): - ::std::runtime_error("Deserialise failure")//, - //m_fcn(fcn), - //m_message(message) - {} -}; - -class Serialisable -{ -public: - virtual const char* serialise_tag() const = 0; - virtual void serialise(Serialiser& s) const = 0; - virtual void deserialise(Deserialiser& s) = 0; -}; - -class Serialiser -{ -protected: - virtual void start_object(const char *tag) = 0; - virtual void end_object(const char *tag) = 0; - virtual void start_array(unsigned int size) = 0; - virtual void end_array() = 0; -public: - template<typename T> - inline void item(T& v) { *this << v; } - - virtual Serialiser& operator<<(bool val) = 0; - virtual Serialiser& operator<<(uint64_t val) = 0; - virtual Serialiser& operator<<(int64_t val) = 0; - virtual Serialiser& operator<<(double val) = 0; - Serialiser& operator<<(unsigned int val) { return *this << (uint64_t)val; }; - virtual Serialiser& operator<<(const char* s) = 0; - Serialiser& operator<<(const ::std::string& s) { - return *this << s.c_str(); - } - Serialiser& operator<<(const Serialisable& subobj); - - template<typename T> - Serialiser& operator<<(const ::std::vector<T>& v) - { - start_array(v.size()); - for(const auto& ent : v) - *this << ent; - end_array(); - return *this; - } - template<typename T> - Serialiser& operator<<(const ::std::shared_ptr<T>& v) - { - *this << v.get(); - if(v.get()) - *this << *v; - return *this; - } - template<typename T> - Serialiser& operator<<(const ::std::unique_ptr<T>& v) - { - *this << v.get(); - if(v.get()) - *this << *v; - return *this; - } - template<typename T1, typename T2> - Serialiser& operator<<(const ::std::pair<T1,T2>& v) - { - start_array(2); - *this << v.first; - *this << v.second; - end_array(); - return *this; - } - template<typename T1, typename T2> - Serialiser& operator<<(const ::std::map<T1,T2>& v) - { - start_array(v.size()); - for(const auto& ent : v) - *this << ent; - end_array(); - return *this; - } -}; - -class Deserialiser -{ -protected: - virtual size_t start_array() = 0; - virtual void end_array() = 0; - - virtual ::std::string read_tag() = 0; -public: - virtual void item(bool& b) = 0; - virtual void item(uint64_t& v) = 0; - void item(unsigned int& v) { uint64_t v1; this->item(v1); v = static_cast<unsigned int>(v1); } - virtual void item(int64_t& val) = 0; - virtual void item(double& v) = 0; - virtual void item(::std::string& s) = 0; - - virtual void start_object(const char *tag) = 0; - virtual void end_object(const char *tag) = 0; - ::std::string start_object(); - - void item(Serialisable& v); - Deserialiser& operator>>(Serialisable& v) { - this->item(v); - return *this; - } - - template<typename T> - void item(::std::vector<T>& v) { - size_t size = start_array(); - v.reserve(size); - for(size_t i = 0; i < size; i ++) { - T item; - this->item(item); - v.emplace_back( ::std::move(item) ); - } - end_array(); - } - template<typename T> - void item(::std::shared_ptr<T>& v) - { - bool present; - - item(present); - - if(present) { - v.reset(new T); - item(*v); - } - else { - v.reset(); - } - } - template<typename T> - void item(::std::unique_ptr<T>& v) - { - bool present; - - item(present); - - if(present) { - v.reset( T::from_deserialiser(*this).release() ); - } - else { - v.reset(); - } - } - template<typename T1, typename T2> - void item(::std::pair<T1,T2>& v) - { - if(2 != start_array()) - throw ::std::runtime_error("Invalid array size for pair"); - item(v.first); - item(v.second); - end_array(); - } - template<typename T1, typename T2> - void item(::std::map<T1,T2>& v) - { - size_t count = start_array(); - while(count--) { - ::std::pair<T1,T2> e; - item(e); - v.insert( ::std::move(e) ); - } - end_array(); - } -}; - -#endif - diff --git a/src/include/serialiser_texttree.hpp b/src/include/serialiser_texttree.hpp deleted file mode 100644 index 7c86d326..00000000 --- a/src/include/serialiser_texttree.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - */ - -#ifndef _SERIALISER_TEXTTREE_HPP_INCLUDED_ -#define _SERIALISER_TEXTTREE_HPP_INCLUDED_ - -#include <ostream> -#include <istream> -#include "serialise.hpp" - -class Serialiser_TextTree: - public Serialiser -{ - ::std::ostream& m_os; - int m_indent_level; - bool m_array_was_empty; -public: - Serialiser_TextTree(::std::ostream& os); - - virtual Serialiser& operator<<(bool val) override; - virtual Serialiser& operator<<(uint64_t val) override; - virtual Serialiser& operator<<(int64_t val) override; - virtual Serialiser& operator<<(double val) override; - virtual Serialiser& operator<<(const char* s) override; - -protected: - virtual void start_object(const char *tag) override; - virtual void end_object(const char* tag) override; - virtual void start_array(unsigned int size) override; - virtual void end_array() override; -private: - void indent(); - void unindent(); - void print_indent(); -}; - - -class Deserialiser_TextTree: - public Deserialiser -{ - ::std::istream& m_is; - - static bool is_ws(char c); - char getc(); - char peekc(); - void eat_ws(); -public: - Deserialiser_TextTree(::std::istream& is); - -protected: - virtual size_t start_array() override; - virtual void end_array() override; - virtual ::std::string read_tag() override; - -public: - virtual void item(bool& b) override; - virtual void item(uint64_t& v) override; - virtual void item(int64_t& v) override; - virtual void item(double& v) override; - virtual void item(::std::string& s) override; - - virtual void start_object(const char *tag) override; - virtual void end_object(const char *tag) override; -}; - -#endif - diff --git a/src/include/span.hpp b/src/include/span.hpp index 59c960fc..68d6bfdf 100644 --- a/src/include/span.hpp +++ b/src/include/span.hpp @@ -74,4 +74,4 @@ Spanned<T> make_spanned(Span sp, T val) { #define BUG(span, msg) do { ::Span(span).bug([&](::std::ostream& os) { os << __FILE__ << ":" << __LINE__ << ": " << msg; }); throw ::std::runtime_error("Bug fell through"); } while(0) #define TODO(span, msg) do { const char* __TODO_func = __func__; ::Span(span).bug([&](::std::ostream& os) { os << __FILE__ << ":" << __LINE__ << ": TODO: " << __TODO_func << " - " << msg; }); throw ::std::runtime_error("Bug (todo) fell through"); } while(0) -#define ASSERT_BUG(span, cnd, msg) do { if( !(cnd) ) { ::Span(span).bug([&](::std::ostream& os) { os << "ASSERT FAIL: " #cnd << ": " << msg; }); throw ::std::runtime_error("Bug fell through"); } } while(0) +#define ASSERT_BUG(span, cnd, msg) do { if( !(cnd) ) { ::Span(span).bug([&](::std::ostream& os) { os << "ASSERT FAIL: " << __FILE__ << ":" << __LINE__ << ":" #cnd << ": " << msg; }); throw ::std::runtime_error("Bug fell through"); } } while(0) diff --git a/src/include/string_view.hpp b/src/include/string_view.hpp new file mode 100644 index 00000000..7d049e9e --- /dev/null +++ b/src/include/string_view.hpp @@ -0,0 +1,89 @@ +/* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * include/string_view.hpp + * - Clone of the `string_view` class (introduced in C++17) + */ +#pragma once +#include <string> +#include <cstddef> // size_t +#include <iostream> // ostream + +namespace std { + +class string_view +{ + const char* m_start; + const char* m_end; + +public: + string_view(): + m_start(nullptr), m_end(nullptr) + { + } + string_view(const char* s, const char* e): + m_start(s), m_end(e) + { + if(!(s <= e)) + throw ::std::invalid_argument("start must be before end for string_view"); + } + string_view(const char* s): + m_start(s), m_end(s) + { + while(*m_end) + m_end ++; + } + string_view(const string& s): + m_start(s.data()), m_end(m_start + s.size()) + { + } + + size_t size() const { + return m_end - m_start; + } + + bool operator==(const string_view& x) const { return cmp(x) == 0; } + bool operator!=(const string_view& x) const { return cmp(x) != 0; } + bool operator< (const string_view& x) const { return cmp(x) < 0; } + bool operator> (const string_view& x) const { return cmp(x) > 0; } + bool operator<=(const string_view& x) const { return cmp(x) <= 0; } + bool operator>=(const string_view& x) const { return cmp(x) >= 0; } + bool operator==(const char* x) const { return cmp(string_view(x)) == 0; } + bool operator!=(const char* x) const { return cmp(string_view(x)) != 0; } + bool operator< (const char* x) const { return cmp(string_view(x)) < 0; } + bool operator> (const char* x) const { return cmp(string_view(x)) > 0; } + bool operator<=(const char* x) const { return cmp(string_view(x)) <= 0; } + bool operator>=(const char* x) const { return cmp(string_view(x)) >= 0; } + bool operator==(const string& x) const { return cmp(string_view(x)) == 0; } + bool operator!=(const string& x) const { return cmp(string_view(x)) != 0; } + bool operator< (const string& x) const { return cmp(string_view(x)) < 0; } + bool operator> (const string& x) const { return cmp(string_view(x)) > 0; } + bool operator<=(const string& x) const { return cmp(string_view(x)) <= 0; } + bool operator>=(const string& x) const { return cmp(string_view(x)) >= 0; } + + friend ::std::ostream& operator<<(::std::ostream& os, const string_view& x) { + for(const char* s = x.m_start; s != x.m_end; s++) + os << *s; + return os; + } + +private: + int cmp(const string_view& x) const { + const char *a, *b; + for( a = m_start, b = x.m_start; a != m_end && b != x.m_end; a++, b++) + { + if( *a != *b ) { + return *a < *b ? -1 : 1; + } + } + if( a == m_end && b == m_end ) + return 0; + if( a == m_end ) + return -1; + else + return 1; + } +}; + +} diff --git a/src/include/synext_decorator.hpp b/src/include/synext_decorator.hpp index af19491d..049a28b0 100644 --- a/src/include/synext_decorator.hpp +++ b/src/include/synext_decorator.hpp @@ -13,7 +13,7 @@ class TypeRef; namespace AST { class Crate; - class MetaItem; + class Attribute; class Path; struct StructItem; @@ -38,26 +38,26 @@ enum class AttrStage class ExpandDecorator { - void unexpected(const Span& sp, const AST::MetaItem& mi, const char* loc_str) const; + void unexpected(const Span& sp, const AST::Attribute& mi, const char* loc_str) const; public: virtual AttrStage stage() const = 0; - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const { unexpected(sp, mi, "crate"); } - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const { unexpected(sp, mi, "item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const { unexpected(sp, mi, "crate"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const { unexpected(sp, mi, "item"); } // NOTE: To delete, set the type to `_` - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const { unexpected(sp, mi, "impl"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const { unexpected(sp, mi, "impl"); } // NOTE: To delete, clear the name - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::StructItem& si) const { unexpected(sp, mi, "struct item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::StructItem& si) const { unexpected(sp, mi, "struct item"); } // NOTE: To delete, make the type invalid - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::TupleItem& si) const { unexpected(sp, mi, "tuple item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::TupleItem& si) const { unexpected(sp, mi, "tuple item"); } // NOTE: To delete, clear the name - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::EnumVariant& ev) const { unexpected(sp, mi, "enum variant"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::EnumVariant& ev) const { unexpected(sp, mi, "enum variant"); } - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::std::unique_ptr<AST::ExprNode>& expr) const { unexpected(sp, mi, "expression"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::std::unique_ptr<AST::ExprNode>& expr) const { unexpected(sp, mi, "expression"); } // NOTE: To delete, clear the patterns vector - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& expr) const { unexpected(sp, mi, "match arm"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& expr) const { unexpected(sp, mi, "match arm"); } // NOTE: To delete, clear the value - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& expr) const { unexpected(sp, mi, "struct literal ent"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& expr) const { unexpected(sp, mi, "struct literal ent"); } }; struct DecoratorDef; |