summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ast/ast.cpp4
-rw-r--r--src/ast/ast.hpp47
-rw-r--r--src/ast/path.cpp2
-rw-r--r--src/ast/path.hpp18
-rw-r--r--src/ast/pattern.cpp151
-rw-r--r--src/convert/ast_iterate.cpp76
-rw-r--r--src/convert/render.cpp50
-rw-r--r--src/convert/resolve.cpp16
-rw-r--r--src/dump_as_rust.cpp35
-rw-r--r--src/include/tagged_union.hpp30
-rw-r--r--src/main.cpp4
-rw-r--r--src/types.cpp327
-rw-r--r--src/types.hpp30
13 files changed, 385 insertions, 405 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp
index 10a7d68a..f89cca98 100644
--- a/src/ast/ast.cpp
+++ b/src/ast/ast.cpp
@@ -730,7 +730,7 @@ Module::ItemRef Module::find_item(const ::std::string& needle, bool allow_leaves
// - If it's a module, recurse
(Module,
auto rv = info.module_->find_item(needle);
- if( rv.tag() != Module::ItemRef::None ) {
+ if( !rv.is_None() ) {
// Don't return RV, return the import (so caller can rewrite path if need be)
return ItemRef(imp);
//return rv;
@@ -921,7 +921,7 @@ SERIALISE_TYPE(TypeParam::, "AST_TypeParam", {
}
-#define SERIALISE_TU_ARM(CLASS, NAME, TAG, ...) case CLASS::TAG: { *this = CLASS::make_null_##TAG(); auto& NAME = this->as_##TAG(); (void)&NAME; __VA_ARGS__ } break;
+#define SERIALISE_TU_ARM(CLASS, NAME, TAG, ...) case CLASS::TAG_##TAG: { *this = CLASS::make_null_##TAG(); auto& NAME = this->as_##TAG(); (void)&NAME; __VA_ARGS__ } break;
#define SERIALISE_TU_ARMS(CLASS, NAME, ...) TU_GMA(__VA_ARGS__)(SERIALISE_TU_ARM, (CLASS, NAME), __VA_ARGS__)
#define SERIALISE_TU(PATH, TAG, NAME, ...) \
void operator%(::Serialiser& s, PATH::Tag c) { s << PATH::tag_to_str(c); } \
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp
index f057d9a3..74f4b36f 100644
--- a/src/ast/ast.hpp
+++ b/src/ast/ast.hpp
@@ -25,6 +25,8 @@
#include "expr.hpp"
+
+#if 1
namespace AST {
using ::std::unique_ptr;
@@ -724,30 +726,30 @@ public:
public:
enum Type
{
- None,
- Module,
- Crate,
- TypeAlias,
- Function,
- Trait,
- Struct,
- Enum,
- Static,
- Use,
+ TAG_None,
+ TAG_Module,
+ TAG_Crate,
+ TAG_TypeAlias,
+ TAG_Function,
+ TAG_Trait,
+ TAG_Struct,
+ TAG_Enum,
+ TAG_Static,
+ TAG_Use,
};
private:
Type m_type;
const void* m_ref;
public:
- ItemRef(): m_type(None) {}
+ ItemRef(): m_type(TAG_None) {}
Type tag() const { return m_type; }
- bool is_None() const { return m_type == None; }
+ bool is_None() const { return m_type == TAG_None; }
const Type& as_None() const { return m_type; } // HACK: Returns &Type in place of &void
#define _(ty,ident) \
- ItemRef(const ty& ref): m_type(ident), m_ref(&ref) {} \
- bool is_##ident() const { return m_type == ident; } \
- const ty& as_##ident() const { assert(m_type == ident); return *(const ty*)m_ref; }
+ ItemRef(const ty& ref): m_type(TAG_##ident), m_ref(&ref) {} \
+ bool is_##ident() const { return m_type == TAG_##ident; } \
+ const ty& as_##ident() const { assert(m_type == TAG_##ident); return *(const ty*)m_ref; }
_(AST::Module, Module)
_(::std::string, Crate)
_(AST::TypeAlias, TypeAlias)
@@ -890,23 +892,28 @@ public:
SERIALISABLE_PROTOTYPES();
};
+}
+#endif
+
+namespace AST
+{
class CStruct
{
- ::std::vector<StructItem> m_fields;
+// ::std::vector<StructItem> m_fields;
public:
const char* name() const { return "TODO"; }
const char* mangled_name() const { return "TODO"; }
- const ::std::vector<StructItem>& fields() const { return m_fields; }
+// const ::std::vector<StructItem>& fields() const { return m_fields; }
};
class Flat
{
::std::vector<CStruct> m_structs;
- ::std::vector< ::std::pair< ::std::string,Function> > m_functions;
+// ::std::vector< ::std::pair< ::std::string,Function> > m_functions;
public:
- const ::std::vector< ::std::pair< ::std::string, Function> >& functions() const { return m_functions; }
+// const ::std::vector< ::std::pair< ::std::string, Function> >& functions() const { return m_functions; }
const ::std::vector<CStruct>& structs() const { return m_structs; }
};
@@ -914,7 +921,7 @@ public:
class GenericResolveClosure
{
- const AST::TypeParams& m_params;
+ const ::AST::TypeParams& m_params;
const ::std::vector<TypeRef>& m_args;
public:
GenericResolveClosure(const AST::TypeParams& params, const ::std::vector<TypeRef>& args):
diff --git a/src/ast/path.cpp b/src/ast/path.cpp
index 797bb5b1..516cebbc 100644
--- a/src/ast/path.cpp
+++ b/src/ast/path.cpp
@@ -537,7 +537,7 @@ void operator%(::Deserialiser& s, Path::Class::Tag& c) {
s.item(n);
c = Path::Class::tag_from_str(n);
}
-#define _D(VAR, ...) case Class::VAR: { m_class = Class::make_null_##VAR(); auto& ent = m_class.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+#define _D(VAR, ...) case Class::TAG_##VAR: { m_class = Class::make_null_##VAR(); auto& ent = m_class.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(Path::, "AST_Path", {
s % m_class.tag();
TU_MATCH(Path::Class, (m_class), (ent),
diff --git a/src/ast/path.hpp b/src/ast/path.hpp
index a2178a8a..4f019b08 100644
--- a/src/ast/path.hpp
+++ b/src/ast/path.hpp
@@ -252,15 +252,17 @@ public:
void match_args(const Path& other, ::std::function<void(const char*,const TypeRef&)> fcn) const;
bool is_trivial() const {
- switch(m_class.tag())
- {
- case Class::Local: return true;
- case Class::Relative: {
- auto& e = m_class.as_Relative();
+ TU_MATCH_DEF(Class, (m_class), (e),
+ (
+ return false;
+ ),
+ (Local,
+ return true;
+ ),
+ (Relative,
return e.nodes.size() == 1 && e.nodes[0].args().size() == 0;
- }
- default: return false;
- }
+ )
+ )
}
bool is_valid() const { return !m_class.is_Invalid(); }
diff --git a/src/ast/pattern.cpp b/src/ast/pattern.cpp
index cc5153cb..1b189fa9 100644
--- a/src/ast/pattern.cpp
+++ b/src/ast/pattern.cpp
@@ -14,32 +14,31 @@ namespace AST {
::std::ostream& operator<<(::std::ostream& os, const Pattern& pat)
{
os << "Pattern(" << pat.m_binding << " @ ";
- switch(pat.m_data.tag())
- {
- case Pattern::Data::Any:
+ TU_MATCH(Pattern::Data, (pat.m_data), (ent),
+ (Any,
os << "_";
- break;
- case Pattern::Data::MaybeBind:
+ ),
+ (MaybeBind,
os << "?";
- break;
- case Pattern::Data::Ref:
- os << "&" << (pat.m_data.as_Ref().mut ? "mut " : "") << *pat.m_data.as_Ref().sub;
- break;
- case Pattern::Data::Value:
- os << *pat.m_data.as_Value().start;
- if( pat.m_data.as_Value().end.get() )
- os << " ... " << *pat.m_data.as_Value().end;
- break;
- case Pattern::Data::Tuple:
- os << "(" << pat.m_data.as_Tuple().sub_patterns << ")";
- break;
- case Pattern::Data::StructTuple:
- os << pat.m_data.as_StructTuple().path << " (" << pat.m_data.as_StructTuple().sub_patterns << ")";
- break;
- case Pattern::Data::Struct:
- os << pat.m_data.as_Struct().path << " {" << pat.m_data.as_Struct().sub_patterns << "}";
- break;
- }
+ ),
+ (Ref,
+ os << "&" << (ent.mut ? "mut " : "") << *ent.sub;
+ ),
+ (Value,
+ os << *ent.start;
+ if( ent.end.get() )
+ os << " ... " << *ent.end;
+ ),
+ (Tuple,
+ os << "(" << ent.sub_patterns << ")";
+ ),
+ (StructTuple,
+ os << ent.path << " (" << ent.sub_patterns << ")";
+ ),
+ (Struct,
+ os << ent.path << " {" << ent.sub_patterns << "}";
+ )
+ )
os << ")";
return os;
}
@@ -51,71 +50,63 @@ void operator%(::Deserialiser& s, Pattern::Data::Tag& c) {
s.item(n);
c = Pattern::Data::tag_from_str(n);
}
+#define _D(VAR, ...) case Pattern::Data::TAG_##VAR: { m_data = Pattern::Data::make_null_##VAR(); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(Pattern::, "Pattern", {
s.item(m_binding);
s % m_data.tag();
- switch(m_data.tag())
- {
- case Pattern::Data::Any:
- break;
- case Pattern::Data::MaybeBind:
- break;
- case Pattern::Data::Ref:
- s << m_data.as_Ref().mut;
- s << m_data.as_Ref().sub;
- break;
- case Pattern::Data::Value:
- s << m_data.as_Value().start;
- s << m_data.as_Value().end;
- break;
- case Pattern::Data::Tuple:
- s << m_data.as_Tuple().sub_patterns;
- break;
- case Pattern::Data::StructTuple:
- s << m_data.as_StructTuple().path;
- s << m_data.as_StructTuple().sub_patterns;
- break;
- case Pattern::Data::Struct:
- s << m_data.as_Struct().path;
- s << m_data.as_Struct().sub_patterns;
- break;
- }
+ TU_MATCH(Pattern::Data, (m_data), (e),
+ (Any,
+ ),
+ (MaybeBind,
+ ),
+ (Ref,
+ s << e.mut;
+ s << e.sub;
+ ),
+ (Value,
+ s << e.start;
+ s << e.end;
+ ),
+ (Tuple,
+ s << e.sub_patterns;
+ ),
+ (StructTuple,
+ s << e.path;
+ s << e.sub_patterns;
+ ),
+ (Struct,
+ s << e.path;
+ s << e.sub_patterns;
+ )
+ )
},{
s.item(m_binding);
Pattern::Data::Tag tag;
s % tag;
switch(tag)
{
- case Pattern::Data::Any:
- m_data = Pattern::Data::make_null_Any();
- break;
- case Pattern::Data::MaybeBind:
- m_data = Pattern::Data::make_null_MaybeBind();
- break;
- case Pattern::Data::Ref:
- m_data = Pattern::Data::make_null_Ref();
- s.item( m_data.as_Ref().mut );
- s.item( m_data.as_Ref().sub );
- break;
- case Pattern::Data::Value:
- m_data = Pattern::Data::make_null_Value();
- s.item( m_data.as_Value().start );
- s.item( m_data.as_Value().end );
- break;
- case Pattern::Data::Tuple:
- m_data = Pattern::Data::make_null_Tuple();
- s.item( m_data.as_Tuple().sub_patterns );
- break;
- case Pattern::Data::StructTuple:
- m_data = Pattern::Data::make_null_StructTuple();
- s.item( m_data.as_StructTuple().path );
- s.item( m_data.as_StructTuple().sub_patterns );
- break;
- case Pattern::Data::Struct:
- m_data = Pattern::Data::make_null_Struct();
- s.item( m_data.as_Struct().path );
- s.item( m_data.as_Struct().sub_patterns );
- break;
+ _D(Any, )
+ _D(MaybeBind,
+ )
+ _D(Ref,
+ s.item( ent.mut );
+ s.item( ent.sub );
+ )
+ _D(Value,
+ s.item( ent.start );
+ s.item( ent.end );
+ )
+ _D(Tuple,
+ s.item( ent.sub_patterns );
+ )
+ _D(StructTuple,
+ s.item( ent.path );
+ s.item( ent.sub_patterns );
+ )
+ _D(Struct,
+ s.item( ent.path );
+ s.item( ent.sub_patterns );
+ )
}
});
diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp
index 1536fa61..50936c93 100644
--- a/src/convert/ast_iterate.cpp
+++ b/src/convert/ast_iterate.cpp
@@ -9,41 +9,39 @@ void CASTIterator::handle_path(AST::Path& path, CASTIterator::PathMode pm)
void CASTIterator::handle_type(TypeRef& type)
{
TRACE_FUNCTION_F("type = " << type);
- #define _(VAR, ...) case TypeData::VAR: { auto &ent = type.m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
- switch(type.m_data.tag())
- {
- _(None)
- _(Any)
- _(Unit)
- _(Primitive)
- _(Path,
+
+ TU_MATCH(TypeData, (type.m_data), (ent),
+ (None),
+ (Any),
+ (Unit),
+ (Primitive),
+ (Path,
handle_path(ent.path, MODE_TYPE);
- )
- _(Tuple,
+ ),
+ (Tuple,
for(auto& subtype : ent.inner_types)
handle_type(subtype);
- )
- _(Borrow,
+ ),
+ (Borrow,
handle_type(*ent.inner);
- )
- _(Pointer,
+ ),
+ (Pointer,
handle_type(*ent.inner);
- )
- _(Array,
+ ),
+ (Array,
handle_type(*ent.inner);
- )
- _(Function,
+ ),
+ (Function,
handle_type(*ent.info.m_rettype);
for(auto& arg : ent.info.m_arg_types)
handle_type(arg);
- )
- _(Generic)
- _(TraitObject,
+ ),
+ (Generic),
+ (TraitObject,
for(auto& trait : ent.traits)
handle_path(trait, MODE_TYPE);
)
- }
- #undef _
+ )
}
void CASTIterator::handle_expr(AST::ExprNode& node)
{
@@ -121,12 +119,11 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
{
//DEBUG("pat = " << pat);
// Resolve names
- switch(pat.data().tag())
- {
- case AST::Pattern::Data::Any:
+ TU_MATCH(AST::Pattern::Data, (pat.data()), (v),
+ (Any,
// Wildcard, nothing to do
- break;
- case AST::Pattern::Data::Ref: {
+ ),
+ (Ref, {
auto& v = pat.data().as_Ref();
if( type_hint.is_wildcard() )
handle_pattern(*v.sub, (const TypeRef&)TypeRef());
@@ -134,16 +131,17 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
throw ::std::runtime_error( FMT("Ref pattern on non-ref value: " << type_hint) );
else
handle_pattern(*v.sub, type_hint.inner_type());
- break; }
- case AST::Pattern::Data::MaybeBind:
+ }),
+ (MaybeBind,
throw ::std::runtime_error("Calling CASTIterator::handle_pattern on MAYBE_BIND, not valid");
- case AST::Pattern::Data::Value: {
+ ),
+ (Value, {
auto& v = pat.data().as_Value();
handle_expr( *v.start );
if( v.end.get() )
handle_expr( *v.end );
- break; }
- case AST::Pattern::Data::Tuple: {
+ }),
+ (Tuple, {
auto& v = pat.data().as_Tuple();
// Tuple is handled by subpattern code
if( type_hint.is_wildcard() )
@@ -167,8 +165,8 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
handle_pattern(v.sub_patterns[i], inner_types[i]);
}
}
- break; }
- case AST::Pattern::Data::Struct: {
+ }),
+ (Struct, {
auto& v = pat.data().as_Struct();
handle_path( v.path, CASTIterator::MODE_TYPE );
if( type_hint.is_wildcard() )
@@ -184,8 +182,8 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
{
throw ::std::runtime_error("TODO: Struct typecheck/iterate");
}
- break; }
- case AST::Pattern::Data::StructTuple: {
+ }),
+ (StructTuple, {
auto& v = pat.data().as_StructTuple();
// Resolve the path!
handle_path( v.path, CASTIterator::MODE_TYPE );
@@ -227,8 +225,8 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
)
)
}
- break; }
- }
+ })
+ )
// Extract bindings and add to namespace
if( pat.binding().size() > 0 )
{
diff --git a/src/convert/render.cpp b/src/convert/render.cpp
index 5c388d07..77ba6dce 100644
--- a/src/convert/render.cpp
+++ b/src/convert/render.cpp
@@ -21,12 +21,12 @@ void Render_Type(::std::ostream& os, const TypeRef& type, const char *name)
void Render_CStruct(::std::ostream& os, const AST::CStruct& str)
{
os << "struct " << str.name() << "{\n";
- for(auto& f : str.fields())
- {
- os << "\t";
- Render_Type(os, f.data, f.name.c_str());
- os << ";\n";
- }
+ //for(auto& f : str.fields())
+ //{
+ // os << "\t";
+ // Render_Type(os, f.data, f.name.c_str());
+ // os << ";\n";
+ //}
os << "}\n";
}
@@ -36,24 +36,24 @@ void Render_Crate(::std::ostream& os, const AST::Flat& crate)
for(const auto& s : crate.structs())
os << "struct " << s.mangled_name() << ";\n";
- for(const auto& item : crate.functions())
- {
- const auto& name = item.first;
- const auto& fcn = item.second;
- Render_Type(os, fcn.rettype(), nullptr);
- os << " " << name << "(";
- //bool is_first = true;
- //for(const auto& f : fcn.args())
- //{
- // if( !is_first )
- // os << ", ";
- // is_first = false;
- // // TODO: handle pattern
- // //Render_Type(os, f.second, f.first.c_str());
- //}
- os << ")\n{\n";
- // Dump expression AST
- os << "}\n";
- }
+ //for(const auto& item : crate.functions())
+ //{
+ // const auto& name = item.first;
+ // const auto& fcn = item.second;
+ // Render_Type(os, fcn.rettype(), nullptr);
+ // os << " " << name << "(";
+ // //bool is_first = true;
+ // //for(const auto& f : fcn.args())
+ // //{
+ // // if( !is_first )
+ // // os << ", ";
+ // // is_first = false;
+ // // // TODO: handle pattern
+ // // //Render_Type(os, f.second, f.first.c_str());
+ // //}
+ // os << ")\n{\n";
+ // // Dump expression AST
+ // os << "}\n";
+ //}
}
diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp
index d1463c8c..b04147d6 100644
--- a/src/convert/resolve.cpp
+++ b/src/convert/resolve.cpp
@@ -618,13 +618,13 @@ void CPathResolver::handle_path_int(const Span& span, AST::Path& path, CASTItera
// - This means converting all partial forms (i.e. not UFCS, Variable, or Absolute)
switch( path.class_tag() )
{
- case AST::Path::Class::Invalid:
+ case AST::Path::Class::TAG_Invalid:
// TODO: Throw an error
assert( !path.m_class.is_Invalid() );
return;
// --- Already absolute forms
// > Absolute: Resolve
- case AST::Path::Class::Absolute:
+ case AST::Path::Class::TAG_Absolute:
DEBUG("Absolute - binding");
INDENT();
handle_path_abs(span, path, mode);
@@ -640,12 +640,12 @@ void CPathResolver::handle_path_int(const Span& span, AST::Path& path, CASTItera
UNINDENT();
break;
// > UFCS: Resolve the type and trait
- case AST::Path::Class::UFCS:
+ case AST::Path::Class::TAG_UFCS:
handle_path_ufcs(span, path, mode);
break;
// > Variable: (wait, how is this known already?)
// - 'self', 'Self'
- case AST::Path::Class::Local:
+ case AST::Path::Class::TAG_Local:
DEBUG("Check local");
if( !path.binding().is_Unbound() )
{
@@ -684,12 +684,12 @@ void CPathResolver::handle_path_int(const Span& span, AST::Path& path, CASTItera
if(0)
// Unannotated relative
- case AST::Path::Class::Relative:
+ case AST::Path::Class::TAG_Relative:
handle_path_rel(span, path, mode);
if(0)
// Module relative
- case AST::Path::Class::Self:
+ case AST::Path::Class::TAG_Self:
{
if( this->find_self_mod_item(span, path, path[0].name()) ) {
// Fall
@@ -703,7 +703,7 @@ void CPathResolver::handle_path_int(const Span& span, AST::Path& path, CASTItera
if(0)
// Parent module relative
// TODO: "super::" can be chained
- case AST::Path::Class::Super:
+ case AST::Path::Class::TAG_Super:
{
if( this->find_super_mod_item(span, path, path[0].name()) ) {
// Fall
@@ -1473,7 +1473,7 @@ void CPathResolver::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint)
{
TRACE_FUNCTION_F("pat = " << pat);
// Resolve "Maybe Bind" entries
- if( pat.data().tag() == AST::Pattern::Data::MaybeBind )
+ if( pat.data().is_MaybeBind() )
{
::std::string name = pat.binding();
// Locate a _constant_ within the current namespace which matches this name
diff --git a/src/dump_as_rust.cpp b/src/dump_as_rust.cpp
index cbd0eaac..f0355a6f 100644
--- a/src/dump_as_rust.cpp
+++ b/src/dump_as_rust.cpp
@@ -720,34 +720,33 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable)
if( p.binding() != "" ) {
m_os << p.binding();
// If binding is irrefutable, and would be binding against a wildcard, just emit the name
- if( !is_refutable && p.data().tag() == AST::Pattern::Data::Any )
+ if( !is_refutable && p.data().is_Any() )
{
return ;
}
m_os << " @ ";
}
- switch(p.data().tag())
- {
- case AST::Pattern::Data::Any:
+ TU_MATCH(AST::Pattern::Data, (p.data()), (v),
+ (Any,
m_os << "_";
- break;
- case AST::Pattern::Data::MaybeBind:
+ ),
+ (MaybeBind,
m_os << "_ /*?*/";
- break;
- case AST::Pattern::Data::Ref: {
+ ),
+ (Ref, {
const auto& v = p.data().as_Ref();
m_os << "& ";
print_pattern(*v.sub, is_refutable);
- break; }
- case AST::Pattern::Data::Value: {
+ }),
+ (Value, {
auto& v = p.data().as_Value();
v.start->visit(*this);
if( v.end.get() ) {
m_os << " ... ";
v.end->visit(*this);
}
- break; }
- case AST::Pattern::Data::StructTuple: {
+ }),
+ (StructTuple, {
const auto& v = p.data().as_StructTuple();
m_os << v.path << "(";
for(const auto& sp : v.sub_patterns) {
@@ -755,8 +754,8 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable)
m_os << ",";
}
m_os << ")";
- break; }
- case AST::Pattern::Data::Struct: {
+ }),
+ (Struct, {
const auto& v = p.data().as_Struct();
m_os << v.path << "(";
for(const auto& sp : v.sub_patterns) {
@@ -765,8 +764,8 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable)
m_os << ",";
}
m_os << ")";
- break; }
- case AST::Pattern::Data::Tuple: {
+ }),
+ (Tuple, {
const auto& v = p.data().as_Tuple();
m_os << "(";
for(const auto& sp : v.sub_patterns) {
@@ -774,8 +773,8 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable)
m_os << ",";
}
m_os << ")";
- break; }
- }
+ })
+ )
}
void RustPrinter::print_type(const TypeRef& t)
diff --git a/src/include/tagged_union.hpp b/src/include/tagged_union.hpp
index 208e15ea..91b7e646 100644
--- a/src/include/tagged_union.hpp
+++ b/src/include/tagged_union.hpp
@@ -26,7 +26,7 @@
#define _DISP8(n, v, ...) n v _DISP7(n, __VA_ARGS__)
#define _DISP9(n, a1,a2,a3,a4, b1,b2,b3,b4, c1) _DISP4(n, a1,a2,a3,a4) _DISP3(n, b1,b2,b3) _DISP2(n, b4,c1)
#define _DISP10(n, a1,a2,a3,a4, b1,b2,b3,b4, c1,c2) _DISP4(n, a1,a2,a3,a4) _DISP4(n, b1,b2,b3,b4) _DISP2(n, c1,c2)
-#define _DISP11(n, a1,a2,a3,a4, b1,b2,b3,b4, c1,c2,c3) _DISP4(n, a1,a2,a3,a4) _DISP4(n, b1,b2,b3,b4) _DISP2(n, c1,c2,c3)
+#define _DISP11(n, a1,a2,a3,a4, b1,b2,b3,b4, c1,c2,c3) _DISP4(n, a1,a2,a3,a4) _DISP4(n, b1,b2,b3,b4) _DISP3(n, c1,c2,c3)
#define _DISP12(n, a1,a2,a3,a4, b1,b2,b3,b4, c1,c2,c3,c4) _DISP4(n, a1,a2,a3,a4) _DISP4(n, b1,b2,b3,b4) _DISP4(n, c1,c2,c3,c4)
#define _DISP13(n, a1,a2,a3,a4,a5, b1,b2,b3,b4, c1,c2,c3,c4) _DISP5(n, a1,a2,a3,a4,a5) _DISP4(n, b1,b2,b3,b4) _DISP4(n, c1,c2,c3,c4)
#define _DISP14(n, a1,a2,a3,a4,a5, b1,b2,b3,b4,b5, c1,c2,c3,c4) _DISP5(n, a1,a2,a3,a4,a5) _DISP5(n, b1,b2,b3,b4,b5) _DISP4(n, c1,c2,c3,c4)
@@ -88,28 +88,28 @@
#define TU_MATCH_BIND1(TAG, VAR, NAME) /*MATCH_BIND*/ auto& NAME = VAR.as_##TAG(); (void)&NAME;
#define TU_MATCH_BIND2_(TAG, v1,v2, n1,n2) TU_MATCH_BIND1(TAG, v1, n1) TU_MATCH_BIND1(TAG, v2, n2)
#define TU_MATCH_BIND2(...) TU_MATCH_BIND2_(__VA_ARGS__)
-#define TU_MATCH_ARM(CLASS, VAR, NAME, TAG, ...) case CLASS::TAG: {/*
+#define TU_MATCH_ARM(CLASS, VAR, NAME, TAG, ...) case CLASS::TAG_##TAG: {/*
*/ TU_GM(TU_MATCH_BIND, TU_EXP VAR)(TAG, TU_EXP VAR , TU_EXP NAME)/*
*/ __VA_ARGS__/*
*/} break;
#define TU_MATCH_ARMS(CLASS, VAR, NAME, ...) TU_GMA(__VA_ARGS__)(TU_MATCH_ARM, (CLASS, VAR, NAME), __VA_ARGS__)
-#define TU_IFLET(CLASS, VAR, TAG, NAME, ...) if(VAR.tag() == CLASS::TAG) { auto& NAME = VAR.as_##TAG(); (void)&NAME; __VA_ARGS__ }
+#define TU_IFLET(CLASS, VAR, TAG, NAME, ...) if(VAR.tag() == CLASS::TAG_##TAG) { auto& NAME = VAR.as_##TAG(); (void)&NAME; __VA_ARGS__ }
#define TU_DATANAME(name) Data_##name
// Internals of TU_CONS
#define TU_CONS_I(__name, __tag, __type) \
- __name(__type v): m_tag(__tag) { new (m_data) __type( ::std::move(v) ); } \
- static self_t make_null_##__tag() { self_t ret; ret.m_tag = __tag; new (ret.m_data) __type; return ::std::move(ret); } \
+ __name(__type v): m_tag(TAG_##__tag) { new (m_data) __type( ::std::move(v) ); } \
+ static self_t make_null_##__tag() { self_t ret; ret.m_tag = TAG_##__tag; new (ret.m_data) __type; return ::std::move(ret); } \
static self_t make_##__tag(__type v) \
{\
return __name( ::std::move(v) );\
}\
- bool is_##__tag() const { return m_tag == __tag; } \
- const __type& as_##__tag() const { assert(m_tag == __tag); return reinterpret_cast<const __type&>(m_data); } \
- __type& as_##__tag() { assert(m_tag == __tag); return reinterpret_cast<__type&>(m_data); } \
- __type unwrap_##__tag() { assert(m_tag == __tag); return ::std::move(reinterpret_cast<__type&>(m_data)); } \
+ bool is_##__tag() const { return m_tag == TAG_##__tag; } \
+ const __type& as_##__tag() const { assert(m_tag == TAG_##__tag); return reinterpret_cast<const __type&>(m_data); } \
+ __type& as_##__tag() { assert(m_tag == TAG_##__tag); return reinterpret_cast<__type&>(m_data); } \
+ __type unwrap_##__tag() { assert(m_tag == TAG_##__tag); return ::std::move(reinterpret_cast<__type&>(m_data)); } \
// Define a tagged union constructor
#define TU_CONS(__name, name, _) TU_CONS_I(__name, name, TU_DATANAME(name))
@@ -118,21 +118,21 @@
#define TU_TYPEDEF(name, content) struct TU_DATANAME(name) { TU_EXP content; };/*
*/
-#define TU_TAG(name, _) name,
+#define TU_TAG(name, _) TAG_##name,
// Destructor internals
-#define TU_DEST_CASE(tag, _) case tag: as_##tag().~TU_DATANAME(tag)(); break;/*
+#define TU_DEST_CASE(tag, _) case TAG_##tag: as_##tag().~TU_DATANAME(tag)(); break;/*
*/
// move constructor internals
-#define TU_MOVE_CASE(tag, _) case tag: new(m_data) TU_DATANAME(tag)(x.unwrap_##tag()); break;/*
+#define TU_MOVE_CASE(tag, _) case TAG_##tag: new(m_data) TU_DATANAME(tag)(x.unwrap_##tag()); break;/*
*/
// "tag_to_str" internals
-#define TU_TOSTR_CASE(tag,_) case tag: return #tag;/*
+#define TU_TOSTR_CASE(tag,_) case TAG_##tag: return #tag;/*
*/
// "tag_from_str" internals
-#define TU_FROMSTR_CASE(tag,_) else if(str == #tag) return tag;/*
+#define TU_FROMSTR_CASE(tag,_) else if(str == #tag) return TAG_##tag;/*
*/
#define MAXS(...) TU_GM(MAXS,__VA_ARGS__)(__VA_ARGS__)
@@ -170,7 +170,7 @@ class _name TU_EXP _inherit { \
Tag m_tag; \
char m_data[MAXS _variants];/*
*/ public:\
- _name(): m_tag(_def) { new((void*)m_data) TU_DATANAME(_def); }\
+ _name(): m_tag(TAG_##_def) { new((void*)m_data) TU_DATANAME(_def); }\
_name(const _name&) = delete; \
_name(_name&& x) noexcept: m_tag(x.m_tag) { switch(m_tag) { TU_MOVE_CASES _variants } } \
_name& operator =(_name&& x) { this->~_name(); m_tag = x.m_tag; switch(m_tag) { TU_MOVE_CASES _variants }; return *this; } \
diff --git a/src/main.cpp b/src/main.cpp
index e883b7ac..b8b661cc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -135,8 +135,8 @@ int main(int argc, char *argv[])
return 0;
}
// Flatten modules into "mangled" set
- g_cur_phase = "Flatten";
- AST::Flat flat_crate = Convert_Flatten(crate);
+ //g_cur_phase = "Flatten";
+ //AST::Flat flat_crate = Convert_Flatten(crate);
// Convert structures to C structures / tagged enums
//Convert_Render(flat_crate, stdout);
diff --git a/src/types.cpp b/src/types.cpp
index 68347f55..2eaf7cd2 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -62,16 +62,16 @@ Ordering Type_Function::ord(const Type_Function& x) const
/// Replace this type reference with a dereferenced version
bool TypeRef::deref(bool is_implicit)
{
- #define _(VAR, ...) case TypeData::VAR: { auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+ #define _(VAR, ...) case TypeData::TAG_##VAR: { auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
switch(m_data.tag())
{
- case TypeData::None: throw ::std::runtime_error("Dereferencing ! - bugcheck");
- case TypeData::Any: throw ::std::runtime_error("Dereferencing _");
- case TypeData::Unit: throw ::std::runtime_error("Dereferencing ()");
- case TypeData::Primitive: throw ::std::runtime_error("Dereferencing a primtive type");
- case TypeData::Function: throw ::std::runtime_error("Dereferencing a function");
+ case TypeData::TAG_None: throw ::std::runtime_error("Dereferencing ! - bugcheck");
+ case TypeData::TAG_Any: throw ::std::runtime_error("Dereferencing _");
+ case TypeData::TAG_Unit: throw ::std::runtime_error("Dereferencing ()");
+ case TypeData::TAG_Primitive: throw ::std::runtime_error("Dereferencing a primtive type");
+ case TypeData::TAG_Function: throw ::std::runtime_error("Dereferencing a function");
- case TypeData::Generic:
+ case TypeData::TAG_Generic:
// TODO: Check for Deref<Output=?> bound
throw ::std::runtime_error("TODO: Check for a Deref bound on generic");
_(Borrow,
@@ -85,11 +85,11 @@ bool TypeRef::deref(bool is_implicit)
*this = *ent.inner;
return true;
)
- case TypeData::Tuple:
- case TypeData::Array:
- case TypeData::Path:
+ case TypeData::TAG_Tuple:
+ case TypeData::TAG_Array:
+ case TypeData::TAG_Path:
throw ::std::runtime_error("TODO: Search for an impl of Deref");
- case TypeData::TraitObject:
+ case TypeData::TAG_TraitObject:
throw ::std::runtime_error("TODO: TypeRef::deref on MULTIDST");
}
#undef _
@@ -128,22 +128,20 @@ void TypeRef::merge_with(const TypeRef& other)
}
- #define _(VAR, ...) case TypeData::VAR: { auto &ent = m_data.as_##VAR(); (void)&ent; const auto &other_ent = other.m_data.as_##VAR(); (void)&other_ent; __VA_ARGS__ } break;
- switch(m_data.tag())
- {
- _(None,
+ TU_MATCH(TypeData, (m_data, other.m_data), (ent, other_ent),
+ (None,
throw ::std::runtime_error("TypeRef::merge_with - Reached concrete/wildcard");
- )
- _(Any,
+ ),
+ (Any,
throw ::std::runtime_error("TypeRef::merge_with - Reached concrete/wildcard");
- )
- _(Unit,
+ ),
+ (Unit,
throw ::std::runtime_error("TypeRef::merge_with - Reached concrete/wildcard");
- )
- _(Primitive,
+ ),
+ (Primitive,
throw ::std::runtime_error("TypeRef::merge_with - Reached concrete/wildcard");
- )
- _(Function,
+ ),
+ (Function,
ent.info.m_rettype->merge_with( *other_ent.info.m_rettype );
if( ent.info.m_arg_types.size() != other_ent.info.m_arg_types.size() )
@@ -152,8 +150,8 @@ void TypeRef::merge_with(const TypeRef& other)
{
ent.info.m_arg_types[i].merge_with( other_ent.info.m_arg_types[i] );
}
- )
- _(Tuple,
+ ),
+ (Tuple,
// Other is known not to be wildcard, and is also a tuple, so it must be the same size
if( ent.inner_types.size() != other_ent.inner_types.size() )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible [tuple sz]");
@@ -161,31 +159,30 @@ void TypeRef::merge_with(const TypeRef& other)
{
ent.inner_types[i].merge_with( other_ent.inner_types[i] );
}
- )
- _(Borrow,
+ ),
+ (Borrow,
if( ent.is_mut != other_ent.is_mut )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible [inner mut]");
ent.inner->merge_with( *other_ent.inner );
- )
- _(Pointer,
+ ),
+ (Pointer,
if( ent.is_mut != other_ent.is_mut )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible [inner mut]");
ent.inner->merge_with( *other_ent.inner );
- )
- _(Array,
+ ),
+ (Array,
throw ::std::runtime_error("TODO: TypeRef::merge_with on ARRAY");
- )
- _(Generic,
+ ),
+ (Generic,
throw ::std::runtime_error("TODO: TypeRef::merge_with on GENERIC");
- )
- _(Path,
+ ),
+ (Path,
throw ::std::runtime_error("TODO: TypeRef::merge_with on PATH");
- )
- _(TraitObject,
+ ),
+ (TraitObject,
throw ::std::runtime_error("TODO: TypeRef::merge_with on MULTIDST");
)
- }
- #undef _
+ )
}
/// Resolve all Generic/Argument types to the value returned by the passed closure
@@ -195,7 +192,7 @@ void TypeRef::merge_with(const TypeRef& other)
void TypeRef::resolve_args(::std::function<TypeRef(const char*)> fcn)
{
DEBUG("" << *this);
- #define _(VAR, ...) case TypeData::VAR: { auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+ #define _(VAR, ...) case TypeData::TAG_##VAR: { auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
switch(m_data.tag())
{
_(None,
@@ -245,14 +242,14 @@ void TypeRef::resolve_args(::std::function<TypeRef(const char*)> fcn)
void TypeRef::match_args(const TypeRef& other, ::std::function<void(const char*,const TypeRef&)> fcn) const
{
// If this type is a generic, then call the closure with the other type
- if( m_data.tag() == TypeData::Generic ) {
+ if( m_data.tag() == TypeData::TAG_Generic ) {
fcn( m_data.as_Generic().name.c_str(), other );
return ;
}
// If the other type is a wildcard, early return
// - TODO - Might want to restrict the other type to be of the same form as this type
- if( other.m_data.tag() == TypeData::Any )
+ if( other.m_data.tag() == TypeData::TAG_Any )
return;
DEBUG("this = " << *this << ", other = " << other);
@@ -260,23 +257,21 @@ void TypeRef::match_args(const TypeRef& other, ::std::function<void(const char*,
// Any other case, it's a "pattern" match
if( m_data.tag() != other.m_data.tag() )
throw ::std::runtime_error("Type mismatch (class)");
- #define _(VAR, ...) case TypeData::VAR: { const auto &ent = m_data.as_##VAR(); const auto &other_ent = other.m_data.as_##VAR(); (void)&ent; (void)&other_ent; __VA_ARGS__ } break;
- switch(m_data.tag())
- {
- _(None,
+ TU_MATCH(TypeData, (m_data, other.m_data), (ent, other_ent),
+ (None,
throw ::std::runtime_error("TypeRef::match_args on !");
- )
- _(Any,
+ ),
+ (Any,
// Wait, isn't this an error?
throw ::std::runtime_error("Encountered '_' in match_args");
- )
- _(Unit)
- _(Primitive,
+ ),
+ (Unit),
+ (Primitive,
// TODO: Should check if the type matches
if( ent.core_type != other_ent.core_type )
throw ::std::runtime_error("Type mismatch (core)");
- )
- _(Function,
+ ),
+ (Function,
if( ent.info.m_abi != other_ent.info.m_abi )
throw ::std::runtime_error("Type mismatch (function abi)");
ent.info.m_rettype->match_args( *other_ent.info.m_rettype, fcn );
@@ -285,86 +280,83 @@ void TypeRef::match_args(const TypeRef& other, ::std::function<void(const char*,
throw ::std::runtime_error("Type mismatch (function size)");
for(unsigned int i = 0; i < ent.info.m_arg_types.size(); i ++ )
ent.info.m_arg_types[i].match_args( other_ent.info.m_arg_types[i], fcn );
- )
- _(Tuple,
+ ),
+ (Tuple,
if( ent.inner_types.size() != other_ent.inner_types.size() )
throw ::std::runtime_error("Type mismatch (tuple size)");
for(unsigned int i = 0; i < ent.inner_types.size(); i ++ )
ent.inner_types[i].match_args( other_ent.inner_types[i], fcn );
- )
- _(Borrow,
+ ),
+ (Borrow,
if( ent.is_mut != other_ent.is_mut )
throw ::std::runtime_error("Type mismatch (inner mutable)");
ent.inner->match_args( *other_ent.inner, fcn );
- )
- _(Pointer,
+ ),
+ (Pointer,
if( ent.is_mut != other_ent.is_mut )
throw ::std::runtime_error("Type mismatch (inner mutable)");
ent.inner->match_args( *other_ent.inner, fcn );
- )
- _(Array,
+ ),
+ (Array,
ent.inner->match_args( *other_ent.inner, fcn );
if(ent.size.get() || other_ent.size.get())
{
throw ::std::runtime_error("TODO: Sized array match_args");
}
- )
- _(Generic,
+ ),
+ (Generic,
throw ::std::runtime_error("Encountered GENERIC in match_args");
- )
- _(Path,
+ ),
+ (Path,
ent.path.match_args(other_ent.path, fcn);
- )
- _(TraitObject,
+ ),
+ (TraitObject,
throw ::std::runtime_error("TODO: TypeRef::match_args on MULTIDST");
)
- }
- #undef _
+ )
}
bool TypeRef::impls_wildcard(const AST::Crate& crate, const AST::Path& trait) const
{
- #define _(VAR, ...) case TypeData::VAR: { const auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
- switch(m_data.tag())
- {
- _(None,
+ TU_MATCH(TypeData, (m_data), (ent),
+ (None,
throw CompileError::BugCheck("TypeRef::impls_wildcard on !");
- )
- _(Any,
+ ),
+ (Any,
throw CompileError::BugCheck("TypeRef::impls_wildcard on _");
- )
+ ),
// Generics are an error?
- _(Generic,
+ (Generic,
// TODO: Include an annotation to the TypeParams structure relevant to this type
// - Allows searching the params for the impl, without having to pass the params down
throw CompileError::Todo("TypeRef::impls_wildcard - param");
- )
+ ),
// Primitives always impl
- _(Unit, return true; )
- _(Primitive, return true; )
+ (Unit, return true; ),
+ (Primitive, return true; ),
// Functions are pointers (currently), so they implement the trait
- _(Function, return true; )
+ (Function, return true; ),
// Pointers/arrays inherit directly
- _(Borrow,
+ (Borrow,
return crate.find_impl(trait, *ent.inner, nullptr, nullptr);
- )
- _(Pointer,
+ ),
+ (Pointer,
return crate.find_impl(trait, *ent.inner, nullptr, nullptr);
- )
- _(Array,
+ ),
+ (Array,
return crate.find_impl(trait, *ent.inner, nullptr, nullptr);
- )
+ ),
// Tuples just destructure
- _(Tuple,
+ (Tuple,
for( const auto& fld : ent.inner_types )
{
if( !crate.find_impl(trait, fld, nullptr, nullptr) )
return false;
}
return true;
- )
+ ),
// Path types destructure
- _(Path,
+ (Path,
// - structs need all fields to impl this trait (cache result)
// - same for enums, tuples, arrays, pointers...
// - traits check the Self bounds
@@ -405,14 +397,13 @@ bool TypeRef::impls_wildcard(const AST::Crate& crate, const AST::Path& trait) co
return true;
)
)
- )
+ ),
// MultiDST is special - It only impls if this trait is in the list
// (or if a listed trait requires/impls the trait)
- _(TraitObject,
+ (TraitObject,
throw CompileError::Todo("TypeRef::impls_wildcard - MULTIDST");
)
- }
- #undef _
+ )
throw CompileError::BugCheck("TypeRef::impls_wildcard - Fell off end");
}
@@ -420,51 +411,50 @@ bool TypeRef::impls_wildcard(const AST::Crate& crate, const AST::Path& trait) co
bool TypeRef::is_concrete() const
{
#define _(VAR, ...) case TypeData::VAR: { const auto &ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
- switch(m_data.tag())
- {
- _(None,
+ TU_MATCH(TypeData, (m_data), (ent),
+ (None,
throw ::std::runtime_error("TypeRef::is_concrete on !");
- )
- _(Any, return false;)
- _(Unit, return true; )
- _(Primitive, return true; )
- _(Function,
+ ),
+ (Any, return false;),
+ (Unit, return true; ),
+ (Primitive, return true; ),
+ (Function,
if( not ent.info.m_rettype->is_concrete() )
return false;
for(const auto& t : ent.info.m_arg_types )
if( not t.is_concrete() )
return false;
return true;
- )
- _(Tuple,
+ ),
+ (Tuple,
for(const auto& t : ent.inner_types)
if( not t.is_concrete() )
return false;
return true;
- )
- _(Borrow,
+ ),
+ (Borrow,
return ent.inner->is_concrete();
- )
- _(Pointer,
+ ),
+ (Pointer,
return ent.inner->is_concrete();
- )
- _(Array,
+ ),
+ (Array,
return ent.inner->is_concrete();
- )
- _(Generic,
+ ),
+ (Generic,
// Well, I guess a generic param is "concrete"
return true;
- )
- _(Path,
+ ),
+ (Path,
return ent.path.is_concrete();
- )
- _(TraitObject,
+ ),
+ (TraitObject,
for(const auto& p : ent.traits )
if( not p.is_concrete() )
return false;
return true;
)
- }
+ )
#undef _
throw ::std::runtime_error( FMT("BUGCHECK - Invalid type class on " << *this) );
}
@@ -472,45 +462,44 @@ bool TypeRef::is_concrete() const
int TypeRef::equal_no_generic(const TypeRef& x) const
{
//DEBUG(*this << ", " << x);
- if( m_data.tag() == TypeData::Generic ) //|| x.m_class == TypeRef::GENERIC )
+ if( m_data.tag() == TypeData::TAG_Generic ) //|| x.m_class == TypeRef::GENERIC )
return 1;
if( m_data.tag() != x.m_data.tag() ) return -1;
- #define _(VAR, ...) case TypeData::VAR: { const auto &ent = m_data.as_##VAR(); const auto& x_ent = x.m_data.as_##VAR(); (void)&ent; (void)&x_ent; __VA_ARGS__ } break;
- switch(m_data.tag())
- {
- _(None, return 0;)
- _(Unit, return 0;)
- _(Any, return 0;)
- _(Primitive,
+
+ TU_MATCH(TypeData, (m_data, x.m_data), (ent, x_ent),
+ (None, return 0;),
+ (Unit, return 0;),
+ (Any, return 0;),
+ (Primitive,
if( ent.core_type != x_ent.core_type ) return -1;
return 0;
- )
- _(Function,
+ ),
+ (Function,
if( ent.info.m_abi != x_ent.info.m_abi ) return -1;
throw CompileError::Todo("TypeRef::equal_no_generic - FUNCTION");
- )
- _(Generic,
+ ),
+ (Generic,
throw CompileError::BugCheck("equal_no_generic - Generic should have been handled above");
- )
- _(Path,
+ ),
+ (Path,
return ent.path.equal_no_generic( x_ent.path );
- )
- _(Borrow,
+ ),
+ (Borrow,
if( ent.is_mut != x_ent.is_mut )
return -1;
return ent.inner->equal_no_generic( *x_ent.inner );
- )
- _(Pointer,
+ ),
+ (Pointer,
if( ent.is_mut != x_ent.is_mut )
return -1;
return ent.inner->equal_no_generic( *x_ent.inner );
- )
- _(Array,
+ ),
+ (Array,
if( ent.size.get() || x_ent.size.get() )
throw CompileError::Todo("TypeRef::equal_no_generic - sized array");
return ent.inner->equal_no_generic( *x_ent.inner );
- )
- _(Tuple,
+ ),
+ (Tuple,
bool fuzzy = false;
if( ent.inner_types.size() != x_ent.inner_types.size() )
return -1;
@@ -521,12 +510,11 @@ int TypeRef::equal_no_generic(const TypeRef& x) const
if(rv > 0) fuzzy = true;
}
return (fuzzy ? 1 : 0);
- )
- _(TraitObject,
+ ),
+ (TraitObject,
throw CompileError::Todo("TypeRef::equal_no_generic - MULTIDST");
)
- }
- #undef _
+ )
throw CompileError::BugCheck("equal_no_generic - Ran off end");
}
Ordering TypeRef::ord(const TypeRef& x) const
@@ -536,32 +524,30 @@ Ordering TypeRef::ord(const TypeRef& x) const
rv = ::ord( (unsigned)m_data.tag(), (unsigned)x.m_data.tag() );
if(rv != OrdEqual) return rv;
- #define _(VAR, ...) case TypeData::VAR: { const auto &ent = m_data.as_##VAR(); const auto& x_ent = x.m_data.as_##VAR(); (void)&ent; (void)&x_ent; __VA_ARGS__ } break;
- switch(x.m_data.tag())
- {
- _(None, return OrdEqual;)
- _(Any, return OrdEqual;)
- _(Unit, return OrdEqual;)
- _(Primitive,
+ TU_MATCH(TypeData, (m_data, x.m_data), (ent, x_ent),
+ (None, return OrdEqual;),
+ (Any, return OrdEqual;),
+ (Unit, return OrdEqual;),
+ (Primitive,
return ::ord( (unsigned)ent.core_type, (unsigned)x_ent.core_type );
- )
- _(Function,
+ ),
+ (Function,
return ent.info.ord( x_ent.info );
- )
- _(Tuple,
+ ),
+ (Tuple,
return ::ord(ent.inner_types, x_ent.inner_types);
- )
- _(Borrow,
+ ),
+ (Borrow,
rv = ::ord(ent.is_mut, x_ent.is_mut);
if(rv != OrdEqual) return rv;
return (*ent.inner).ord(*x_ent.inner);
- )
- _(Pointer,
+ ),
+ (Pointer,
rv = ::ord(ent.is_mut, x_ent.is_mut);
if(rv != OrdEqual) return rv;
return (*ent.inner).ord(*x_ent.inner);
- )
- _(Array,
+ ),
+ (Array,
rv = (*ent.inner).ord( *x_ent.inner );
if(rv != OrdEqual) return rv;
if(ent.size.get())
@@ -569,8 +555,8 @@ Ordering TypeRef::ord(const TypeRef& x) const
throw ::std::runtime_error("TODO: Sized array comparisons");
}
return OrdEqual;
- )
- _(Generic,
+ ),
+ (Generic,
if( ent.params != x_ent.params )
{
DEBUG(*this << " == " << x);
@@ -582,15 +568,14 @@ Ordering TypeRef::ord(const TypeRef& x) const
else {
}
return ::ord(ent.name, x_ent.name);
- )
- _(Path,
+ ),
+ (Path,
return ent.path.ord( x_ent.path );
- )
- _(TraitObject,
+ ),
+ (TraitObject,
return ::ord(ent.traits, x_ent.traits);
)
- }
- #undef _
+ )
throw ::std::runtime_error(FMT("BUGCHECK - Unhandled TypeRef class '" << m_data.tag() << "'"));
}
@@ -600,7 +585,7 @@ Ordering TypeRef::ord(const TypeRef& x) const
::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr) {
//os << "TypeRef(";
- #define _(VAR, ...) case TypeData::VAR: { const auto &ent = tr.m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+ #define _(VAR, ...) case TypeData::TAG_##VAR: { const auto &ent = tr.m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
switch(tr.m_data.tag())
{
_(None,
@@ -705,8 +690,8 @@ void operator%(::Deserialiser& s, TypeData::Tag& c) {
return box$(n);
}
-#define _S(VAR, ...) case TypeData::VAR: { const auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
-#define _D(VAR, ...) case TypeData::VAR: { m_data = TypeData::make_null_##VAR(); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+#define _S(VAR, ...) case TypeData::TAG_##VAR: { const auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
+#define _D(VAR, ...) case TypeData::TAG_##VAR: { m_data = TypeData::make_null_##VAR(); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(TypeRef::, "TypeRef", {
s % m_data.tag();
switch(m_data.tag())
diff --git a/src/types.hpp b/src/types.hpp
index 9146d971..ef3e4d1d 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -110,8 +110,8 @@ public:
{
switch( other.m_data.tag() )
{
- #define _COPY(VAR) case TypeData::VAR: m_data = TypeData::make_##VAR(other.m_data.as_##VAR()); break;
- #define _CLONE(VAR, code...) case TypeData::VAR: { auto& old = other.m_data.as_##VAR(); m_data = TypeData::make_##VAR(code); } break;
+ #define _COPY(VAR) case TypeData::TAG_##VAR: m_data = TypeData::make_##VAR(other.m_data.as_##VAR()); break;
+ #define _CLONE(VAR, code...) case TypeData::TAG_##VAR: { auto& old = other.m_data.as_##VAR(); m_data = TypeData::make_##VAR(code); } break;
_COPY(None)
_COPY(Any)
_COPY(Unit)
@@ -247,22 +247,20 @@ public:
//}
const TypeRef& inner_type() const {
- switch(m_data.tag())
- {
- case TypeData::Borrow: return *m_data.as_Borrow().inner;
- case TypeData::Pointer: return *m_data.as_Pointer().inner;
- case TypeData::Array: return *m_data.as_Array().inner;
- default: throw ::std::runtime_error("Called inner_type on non-wrapper");
- }
+ TU_MATCH_DEF(TypeData, (m_data), (e),
+ ( throw ::std::runtime_error("Called inner_type on non-wrapper"); ),
+ (Borrow, return *e.inner; ),
+ (Pointer, return *e.inner; ),
+ (Array, return *e.inner; )
+ )
}
TypeRef& inner_type() {
- switch(m_data.tag())
- {
- case TypeData::Borrow: return *m_data.as_Borrow().inner;
- case TypeData::Pointer: return *m_data.as_Pointer().inner;
- case TypeData::Array: return *m_data.as_Array().inner;
- default: throw ::std::runtime_error("Called inner_type on non-wrapper");
- }
+ TU_MATCH_DEF(TypeData, (m_data), (e),
+ ( throw ::std::runtime_error("Called inner_type on non-wrapper"); ),
+ (Borrow, return *e.inner; ),
+ (Pointer, return *e.inner; ),
+ (Array, return *e.inner; )
+ )
}
//::std::vector<TypeRef>& sub_types() { return m_inner_types; }
//const ::std::vector<TypeRef>& sub_types() const { return m_inner_types; }