diff options
author | ubsan <npmazzuca@gmail.com> | 2016-12-24 19:07:08 -0700 |
---|---|---|
committer | ubsan <npmazzuca@gmail.com> | 2016-12-24 19:07:08 -0700 |
commit | 0c14c734fa32014fd24297ccdbed927016185ffd (patch) | |
tree | ed4bbff4be39c44c57164641f4ed32b5ad4675f0 /src/ast | |
parent | d12a8a886caf2e0edf33c1af831b1df990d2c892 (diff) | |
download | mrust-0c14c734fa32014fd24297ccdbed927016185ffd.tar.gz |
No more tears!
No more DOS line endings or trailing whitespace either
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/ast.cpp | 864 | ||||
-rw-r--r-- | src/ast/ast.hpp | 1284 | ||||
-rw-r--r-- | src/ast/attrs.hpp | 22 | ||||
-rw-r--r-- | src/ast/crate.cpp | 12 | ||||
-rw-r--r-- | src/ast/crate.hpp | 16 | ||||
-rw-r--r-- | src/ast/dump.cpp | 82 | ||||
-rw-r--r-- | src/ast/expr.cpp | 8 | ||||
-rw-r--r-- | src/ast/expr.hpp | 86 | ||||
-rw-r--r-- | src/ast/expr_ptr.hpp | 2 | ||||
-rw-r--r-- | src/ast/generics.hpp | 22 | ||||
-rw-r--r-- | src/ast/item.hpp | 4 | ||||
-rw-r--r-- | src/ast/macro.hpp | 8 | ||||
-rw-r--r-- | src/ast/path.cpp | 16 | ||||
-rw-r--r-- | src/ast/path.hpp | 52 | ||||
-rw-r--r-- | src/ast/pattern.cpp | 10 | ||||
-rw-r--r-- | src/ast/pattern.hpp | 28 | ||||
-rw-r--r-- | src/ast/types.cpp | 8 | ||||
-rw-r--r-- | src/ast/types.hpp | 552 |
18 files changed, 1538 insertions, 1538 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 15b8c7d7..39250a41 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -1,432 +1,432 @@ -/*
- */
-#include "ast.hpp"
-#include "crate.hpp"
-#include "types.hpp"
-#include "expr.hpp"
-#include "../common.hpp"
-#include <iostream>
-#include "../parse/parseerror.hpp"
-#include <algorithm>
-#include <serialiser_texttree.hpp>
-
-namespace AST {
-
-
-namespace {
- ::std::vector<MetaItem> clone_mivec(const ::std::vector<MetaItem>& v) {
- ::std::vector<MetaItem> ri;
- ri.reserve(v.size());
- for(const auto& i : v)
- ri.push_back( i.clone() );
- return ri;
- }
-}
-
-MetaItems::~MetaItems()
-{
-}
-MetaItems MetaItems::clone() const
-{
- return MetaItems( m_span, clone_mivec(m_items) );
-}
-
-void MetaItems::push_back(MetaItem i)
-{
- m_items.push_back( ::std::move(i) );
-}
-const MetaItem* MetaItems::get(const char *name) const
-{
- for( auto& i : m_items ) {
- if(i.name() == name) {
- //i.mark_used();
- return &i;
- }
- }
- return 0;
-}
-
-MetaItem::~MetaItem()
-{
-}
-MetaItem MetaItem::clone() const
-{
- TU_MATCH(MetaItemData, (m_data), (e),
- (None,
- return MetaItem(m_name);
- ),
- (String,
- return MetaItem(m_name, e.val);
- ),
- (List,
- return MetaItem(m_name, clone_mivec(e.sub_items));
- )
- )
- throw ::std::runtime_error("MetaItem::clone - Fell off end");
-}
-
-StructItem StructItem::clone() const
-{
- return StructItem(m_attrs.clone(), m_is_public, m_name, m_type.clone());
-}
-TupleItem TupleItem::clone() const
-{
- return TupleItem(m_attrs.clone(), m_is_public, m_type.clone());
-}
-
-
-TypeAlias TypeAlias::clone() const
-{
- return TypeAlias( m_params.clone(), m_type.clone() );
-}
-Static Static::clone() const
-{
- return Static( m_class, m_type.clone(), m_value.is_valid() ? AST::Expr( m_value.node().clone() ) : AST::Expr() );
-}
-
-Function::Function(Span sp, GenericParams params, ::std::string abi, bool is_unsafe, bool is_const, bool is_variadic, TypeRef ret_type, Arglist args):
- m_span(sp),
- m_params( move(params) ),
- m_rettype( move(ret_type) ),
- m_args( move(args) ),
- m_abi( mv$(abi) ),
- m_is_const(is_const),
- m_is_unsafe(is_unsafe),
- m_is_variadic(is_variadic)
-{
-}
-Function Function::clone() const
-{
- decltype(m_args) new_args;
- for(const auto& arg : m_args)
- new_args.push_back( ::std::make_pair( arg.first.clone(), arg.second.clone() ) );
-
- auto rv = Function( m_span, m_params.clone(), m_abi, m_is_unsafe, m_is_const, m_is_variadic, m_rettype.clone(), mv$(new_args) );
- if( m_code.is_valid() )
- {
- rv.m_code = AST::Expr( m_code.node().clone() );
- }
- return rv;
-}
-
-void Trait::add_type(::std::string name, TypeRef type) {
- m_items.push_back( Named<Item>(mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))}), true) );
-}
-void Trait::add_function(::std::string name, Function fcn) {
- DEBUG("trait fn " << name);
- m_items.push_back( Named<Item>(mv$(name), Item::make_Function({mv$(fcn)}), true) );
-}
-void Trait::add_static(::std::string name, Static v) {
- m_items.push_back( Named<Item>(mv$(name), Item::make_Static({mv$(v)}), true) );
-}
-void Trait::set_is_marker() {
- m_is_marker = true;
-}
-bool Trait::is_marker() const {
- return m_is_marker;
-}
-bool Trait::has_named_item(const ::std::string& name, bool& out_is_fcn) const
-{
- for( const auto& i : m_items )
- {
- if( i.name == name ) {
- out_is_fcn = i.data.is_Function();
- return true;
- }
- }
- return false;
-}
-
-Trait Trait::clone() const
-{
- auto rv = Trait(m_params.clone(), m_supertraits);
- for(const auto& item : m_items)
- {
- rv.m_items.push_back( Named<Item> { item.name, item.data.clone(), item.is_pub } );
- }
- return rv;
-}
-
-Enum Enum::clone() const
-{
- decltype(m_variants) new_variants;
- for(const auto& var : m_variants)
- {
- TU_MATCHA( (var.m_data), (e),
- (Value,
- new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, e.m_value.clone()) );
- ),
- (Tuple,
- decltype(e.m_sub_types) new_st;
- for(const auto& f : e.m_sub_types)
- new_st.push_back( f.clone() );
- new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, mv$(new_st)) );
- ),
- (Struct,
- decltype(e.m_fields) new_fields;
- for(const auto& f : e.m_fields)
- new_fields.push_back( f.clone() );
- new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, mv$(new_fields)) );
- )
- )
- }
- return Enum(m_params.clone(), mv$(new_variants));
-}
-Struct Struct::clone() const
-{
- TU_MATCHA( (m_data), (e),
- (Tuple,
- decltype(e.ents) new_fields;
- for(const auto& f : e.ents)
- new_fields.push_back( f.clone() );
- return Struct(m_params.clone(), mv$(new_fields));
- ),
- (Struct,
- decltype(e.ents) new_fields;
- for(const auto& f : e.ents)
- new_fields.push_back( f.clone() );
- return Struct(m_params.clone(), mv$(new_fields));
- )
- )
- throw "";
-}
-
-Union Union::clone() const
-{
- decltype(m_variants) new_vars;
- for(const auto& f : m_variants)
- new_vars.push_back( f.clone() );
- return Union(m_params.clone(), mv$(new_vars));
-}
-
-::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl)
-{
- return os << "impl<" << impl.m_params << "> " << impl.m_trait.ent << " for " << impl.m_type << "";
-}
-
-void Impl::add_function(bool is_public, bool is_specialisable, ::std::string name, Function fcn)
-{
- DEBUG("impl fn " << name);
- m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Function(mv$(fcn)) ) } );
-}
-void Impl::add_type(bool is_public, bool is_specialisable, ::std::string name, TypeRef type)
-{
- m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Type(TypeAlias(GenericParams(), mv$(type))) ) } );
-}
-void Impl::add_static(bool is_public, bool is_specialisable, ::std::string name, Static v)
-{
- m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Static(mv$(v)) ) } );
-}
-void Impl::add_macro_invocation(MacroInvocation item) {
- m_items.push_back( ImplItem { false, false, "", box$( Item::make_MacroInv(mv$(item)) ) } );
-}
-
-bool Impl::has_named_item(const ::std::string& name) const
-{
- for( const auto& it : this->items() )
- {
- if( it.name == name ) {
- return true;
- }
- }
- return false;
-}
-
-::std::ostream& operator<<(::std::ostream& os, const Impl& impl)
-{
- return os << impl.m_def;
-}
-
-::rust::option<char> ImplRef::find_named_item(const ::std::string& name) const
-{
- if( this->impl.has_named_item(name) ) {
- return ::rust::Some(' ');
- }
- else {
- return ::rust::None<char>();
- }
-}
-
-::std::ostream& operator<<(::std::ostream& os, const UseStmt& x)
-{
- os << "Use(" << x.path << ")";
- return os;
-}
-
-
-
-MacroInvocation MacroInvocation::clone() const
-{
- return MacroInvocation(m_span, m_macro_name, m_ident, m_input.clone());
-}
-
-
-UseStmt UseStmt::clone() const
-{
- return UseStmt(sp, path);
-}
-
-void ExternBlock::add_item(Named<Item> named_item)
-{
- ASSERT_BUG(named_item.data.span, named_item.data.is_Function() || named_item.data.is_Static(), "Incorrect item type for ExternBlock");
- m_items.push_back( mv$(named_item) );
-}
-ExternBlock ExternBlock::clone() const
-{
- TODO(Span(), "Clone an extern block");
-}
-
-::std::shared_ptr<AST::Module> Module::add_anon() {
- auto rv = ::std::shared_ptr<AST::Module>( new Module(m_my_path + FMT("#" << m_anon_modules.size())) );
- DEBUG("New anon " << rv->m_my_path);
- rv->m_file_info = m_file_info;
-
- m_anon_modules.push_back( rv );
-
- return rv;
-}
-
-void Module::add_item( Named<Item> named_item ) {
- m_items.push_back( mv$(named_item) );
- const auto& i = m_items.back();
- if( i.name == "" ) {
- }
- else {
- DEBUG(m_my_path << "::" << i.name << " = " << i.data.tag_str() << ", attrs = " << i.data.attrs);
- }
-}
-void Module::add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs) {
- it.attrs = mv$(attrs);
- add_item( Named<Item>( mv$(name), mv$(it), is_pub ) );
-}
-void Module::add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs) {
- this->add_item( is_public, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) );
-}
-void Module::add_alias(bool is_public, UseStmt us, ::std::string name, MetaItems attrs) {
- this->add_item( is_public, mv$(name), Item(mv$(us)), mv$(attrs) );
-}
-void Module::add_macro_invocation(MacroInvocation item) {
- this->add_item( false, "", Item( mv$(item) ), ::AST::MetaItems {} );
-}
-void Module::add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro) {
- m_macros.push_back( Named<MacroRulesPtr>( mv$(name), mv$(macro), is_exported ) );
-}
-void Module::add_macro_import(::std::string name, const MacroRules& mr) {
- m_macro_import_res.push_back( NamedNS<const MacroRules*>( mv$(name), &mr, false ) );
-}
-
-Item Item::clone() const
-{
- TU_MATCHA( (*this), (e),
- (None,
- return AST::Item(e);
- ),
- (MacroInv,
- TODO(this->span, "Clone on Item::MacroInv");
- ),
- (Use,
- return AST::Item(e.clone());
- ),
- (ExternBlock,
- TODO(this->span, "Clone on Item::" << this->tag_str());
- ),
- (Impl,
- TODO(this->span, "Clone on Item::Impl");
- ),
- (NegImpl,
- TODO(this->span, "Clone on Item::NegImpl");
- ),
- (Module,
- TODO(this->span, "Clone on Item::Module");
- ),
- (Crate,
- return AST::Item(e);
- ),
- (Type,
- return AST::Item(e.clone());
- ),
- (Struct,
- return AST::Item(e.clone());
- ),
- (Enum,
- return AST::Item(e.clone());
- ),
- (Union,
- return AST::Item(e.clone());
- ),
- (Trait,
- return AST::Item(e.clone());
- ),
-
- (Function,
- return AST::Item(e.clone());
- ),
- (Static,
- return AST::Item(e.clone());
- )
- )
- throw "";
-}
-
-
-
-::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp)
-{
- //os << "TypeParam(";
- os << tp.m_name;
- os << " = ";
- os << tp.m_default;
- //os << ")";
- return os;
-}
-
-::std::ostream& operator<<(::std::ostream& os, const GenericBound& x)
-{
- TU_MATCH(GenericBound, (x), (ent),
- (Lifetime,
- os << "'" << ent.test << ": '" << ent.bound;
- ),
- (TypeLifetime,
- os << ent.type << ": '" << ent.bound;
- ),
- (IsTrait,
- if( ! ent.hrls.empty() )
- {
- os << "for<";
- for(const auto& l : ent.hrls)
- os << "'" << l;
- os << ">";
- }
- os << ent.type << ": " << ent.trait;
- ),
- (MaybeTrait,
- os << ent.type << ": ?" << ent.trait;
- ),
- (NotTrait,
- os << ent.type << ": !" << ent.trait;
- ),
- (Equality,
- os << ent.type << " = " << ent.replacement;
- )
- )
- return os;
-}
-
-
-int GenericParams::find_name(const char* name) const
-{
- for( unsigned int i = 0; i < m_type_params.size(); i ++ )
- {
- if( m_type_params[i].name() == name )
- return i;
- }
- DEBUG("Type param '" << name << "' not in list");
- return -1;
-}
-
-::std::ostream& operator<<(::std::ostream& os, const GenericParams& tps)
-{
- return os << "<" << tps.m_lifetime_params << "," << tps.m_type_params << "> where {" << tps.m_bounds << "}";
-}
-
-} // namespace AST
+/* + */ +#include "ast.hpp" +#include "crate.hpp" +#include "types.hpp" +#include "expr.hpp" +#include "../common.hpp" +#include <iostream> +#include "../parse/parseerror.hpp" +#include <algorithm> +#include <serialiser_texttree.hpp> + +namespace AST { + + +namespace { + ::std::vector<MetaItem> clone_mivec(const ::std::vector<MetaItem>& v) { + ::std::vector<MetaItem> ri; + ri.reserve(v.size()); + for(const auto& i : v) + ri.push_back( i.clone() ); + return ri; + } +} + +MetaItems::~MetaItems() +{ +} +MetaItems MetaItems::clone() const +{ + return MetaItems( m_span, clone_mivec(m_items) ); +} + +void MetaItems::push_back(MetaItem i) +{ + m_items.push_back( ::std::move(i) ); +} +const MetaItem* MetaItems::get(const char *name) const +{ + for( auto& i : m_items ) { + if(i.name() == name) { + //i.mark_used(); + return &i; + } + } + return 0; +} + +MetaItem::~MetaItem() +{ +} +MetaItem MetaItem::clone() const +{ + TU_MATCH(MetaItemData, (m_data), (e), + (None, + return MetaItem(m_name); + ), + (String, + return MetaItem(m_name, e.val); + ), + (List, + return MetaItem(m_name, clone_mivec(e.sub_items)); + ) + ) + throw ::std::runtime_error("MetaItem::clone - Fell off end"); +} + +StructItem StructItem::clone() const +{ + return StructItem(m_attrs.clone(), m_is_public, m_name, m_type.clone()); +} +TupleItem TupleItem::clone() const +{ + return TupleItem(m_attrs.clone(), m_is_public, m_type.clone()); +} + + +TypeAlias TypeAlias::clone() const +{ + return TypeAlias( m_params.clone(), m_type.clone() ); +} +Static Static::clone() const +{ + return Static( m_class, m_type.clone(), m_value.is_valid() ? AST::Expr( m_value.node().clone() ) : AST::Expr() ); +} + +Function::Function(Span sp, GenericParams params, ::std::string abi, bool is_unsafe, bool is_const, bool is_variadic, TypeRef ret_type, Arglist args): + m_span(sp), + m_params( move(params) ), + m_rettype( move(ret_type) ), + m_args( move(args) ), + m_abi( mv$(abi) ), + m_is_const(is_const), + m_is_unsafe(is_unsafe), + m_is_variadic(is_variadic) +{ +} +Function Function::clone() const +{ + decltype(m_args) new_args; + for(const auto& arg : m_args) + new_args.push_back( ::std::make_pair( arg.first.clone(), arg.second.clone() ) ); + + auto rv = Function( m_span, m_params.clone(), m_abi, m_is_unsafe, m_is_const, m_is_variadic, m_rettype.clone(), mv$(new_args) ); + if( m_code.is_valid() ) + { + rv.m_code = AST::Expr( m_code.node().clone() ); + } + return rv; +} + +void Trait::add_type(::std::string name, TypeRef type) { + m_items.push_back( Named<Item>(mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))}), true) ); +} +void Trait::add_function(::std::string name, Function fcn) { + DEBUG("trait fn " << name); + m_items.push_back( Named<Item>(mv$(name), Item::make_Function({mv$(fcn)}), true) ); +} +void Trait::add_static(::std::string name, Static v) { + m_items.push_back( Named<Item>(mv$(name), Item::make_Static({mv$(v)}), true) ); +} +void Trait::set_is_marker() { + m_is_marker = true; +} +bool Trait::is_marker() const { + return m_is_marker; +} +bool Trait::has_named_item(const ::std::string& name, bool& out_is_fcn) const +{ + for( const auto& i : m_items ) + { + if( i.name == name ) { + out_is_fcn = i.data.is_Function(); + return true; + } + } + return false; +} + +Trait Trait::clone() const +{ + auto rv = Trait(m_params.clone(), m_supertraits); + for(const auto& item : m_items) + { + rv.m_items.push_back( Named<Item> { item.name, item.data.clone(), item.is_pub } ); + } + return rv; +} + +Enum Enum::clone() const +{ + decltype(m_variants) new_variants; + for(const auto& var : m_variants) + { + TU_MATCHA( (var.m_data), (e), + (Value, + new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, e.m_value.clone()) ); + ), + (Tuple, + decltype(e.m_sub_types) new_st; + for(const auto& f : e.m_sub_types) + new_st.push_back( f.clone() ); + new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, mv$(new_st)) ); + ), + (Struct, + decltype(e.m_fields) new_fields; + for(const auto& f : e.m_fields) + new_fields.push_back( f.clone() ); + new_variants.push_back( EnumVariant(var.m_attrs.clone(), var.m_name, mv$(new_fields)) ); + ) + ) + } + return Enum(m_params.clone(), mv$(new_variants)); +} +Struct Struct::clone() const +{ + TU_MATCHA( (m_data), (e), + (Tuple, + decltype(e.ents) new_fields; + for(const auto& f : e.ents) + new_fields.push_back( f.clone() ); + return Struct(m_params.clone(), mv$(new_fields)); + ), + (Struct, + decltype(e.ents) new_fields; + for(const auto& f : e.ents) + new_fields.push_back( f.clone() ); + return Struct(m_params.clone(), mv$(new_fields)); + ) + ) + throw ""; +} + +Union Union::clone() const +{ + decltype(m_variants) new_vars; + for(const auto& f : m_variants) + new_vars.push_back( f.clone() ); + return Union(m_params.clone(), mv$(new_vars)); +} + +::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl) +{ + return os << "impl<" << impl.m_params << "> " << impl.m_trait.ent << " for " << impl.m_type << ""; +} + +void Impl::add_function(bool is_public, bool is_specialisable, ::std::string name, Function fcn) +{ + DEBUG("impl fn " << name); + m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Function(mv$(fcn)) ) } ); +} +void Impl::add_type(bool is_public, bool is_specialisable, ::std::string name, TypeRef type) +{ + m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Type(TypeAlias(GenericParams(), mv$(type))) ) } ); +} +void Impl::add_static(bool is_public, bool is_specialisable, ::std::string name, Static v) +{ + m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Static(mv$(v)) ) } ); +} +void Impl::add_macro_invocation(MacroInvocation item) { + m_items.push_back( ImplItem { false, false, "", box$( Item::make_MacroInv(mv$(item)) ) } ); +} + +bool Impl::has_named_item(const ::std::string& name) const +{ + for( const auto& it : this->items() ) + { + if( it.name == name ) { + return true; + } + } + return false; +} + +::std::ostream& operator<<(::std::ostream& os, const Impl& impl) +{ + return os << impl.m_def; +} + +::rust::option<char> ImplRef::find_named_item(const ::std::string& name) const +{ + if( this->impl.has_named_item(name) ) { + return ::rust::Some(' '); + } + else { + return ::rust::None<char>(); + } +} + +::std::ostream& operator<<(::std::ostream& os, const UseStmt& x) +{ + os << "Use(" << x.path << ")"; + return os; +} + + + +MacroInvocation MacroInvocation::clone() const +{ + return MacroInvocation(m_span, m_macro_name, m_ident, m_input.clone()); +} + + +UseStmt UseStmt::clone() const +{ + return UseStmt(sp, path); +} + +void ExternBlock::add_item(Named<Item> named_item) +{ + ASSERT_BUG(named_item.data.span, named_item.data.is_Function() || named_item.data.is_Static(), "Incorrect item type for ExternBlock"); + m_items.push_back( mv$(named_item) ); +} +ExternBlock ExternBlock::clone() const +{ + TODO(Span(), "Clone an extern block"); +} + +::std::shared_ptr<AST::Module> Module::add_anon() { + auto rv = ::std::shared_ptr<AST::Module>( new Module(m_my_path + FMT("#" << m_anon_modules.size())) ); + DEBUG("New anon " << rv->m_my_path); + rv->m_file_info = m_file_info; + + m_anon_modules.push_back( rv ); + + return rv; +} + +void Module::add_item( Named<Item> named_item ) { + m_items.push_back( mv$(named_item) ); + const auto& i = m_items.back(); + if( i.name == "" ) { + } + else { + DEBUG(m_my_path << "::" << i.name << " = " << i.data.tag_str() << ", attrs = " << i.data.attrs); + } +} +void Module::add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs) { + it.attrs = mv$(attrs); + add_item( Named<Item>( mv$(name), mv$(it), is_pub ) ); +} +void Module::add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs) { + this->add_item( is_public, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); +} +void Module::add_alias(bool is_public, UseStmt us, ::std::string name, MetaItems attrs) { + this->add_item( is_public, mv$(name), Item(mv$(us)), mv$(attrs) ); +} +void Module::add_macro_invocation(MacroInvocation item) { + this->add_item( false, "", Item( mv$(item) ), ::AST::MetaItems {} ); +} +void Module::add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro) { + m_macros.push_back( Named<MacroRulesPtr>( mv$(name), mv$(macro), is_exported ) ); +} +void Module::add_macro_import(::std::string name, const MacroRules& mr) { + m_macro_import_res.push_back( NamedNS<const MacroRules*>( mv$(name), &mr, false ) ); +} + +Item Item::clone() const +{ + TU_MATCHA( (*this), (e), + (None, + return AST::Item(e); + ), + (MacroInv, + TODO(this->span, "Clone on Item::MacroInv"); + ), + (Use, + return AST::Item(e.clone()); + ), + (ExternBlock, + TODO(this->span, "Clone on Item::" << this->tag_str()); + ), + (Impl, + TODO(this->span, "Clone on Item::Impl"); + ), + (NegImpl, + TODO(this->span, "Clone on Item::NegImpl"); + ), + (Module, + TODO(this->span, "Clone on Item::Module"); + ), + (Crate, + return AST::Item(e); + ), + (Type, + return AST::Item(e.clone()); + ), + (Struct, + return AST::Item(e.clone()); + ), + (Enum, + return AST::Item(e.clone()); + ), + (Union, + return AST::Item(e.clone()); + ), + (Trait, + return AST::Item(e.clone()); + ), + + (Function, + return AST::Item(e.clone()); + ), + (Static, + return AST::Item(e.clone()); + ) + ) + throw ""; +} + + + +::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp) +{ + //os << "TypeParam("; + os << tp.m_name; + os << " = "; + os << tp.m_default; + //os << ")"; + return os; +} + +::std::ostream& operator<<(::std::ostream& os, const GenericBound& x) +{ + TU_MATCH(GenericBound, (x), (ent), + (Lifetime, + os << "'" << ent.test << ": '" << ent.bound; + ), + (TypeLifetime, + os << ent.type << ": '" << ent.bound; + ), + (IsTrait, + if( ! ent.hrls.empty() ) + { + os << "for<"; + for(const auto& l : ent.hrls) + os << "'" << l; + os << ">"; + } + os << ent.type << ": " << ent.trait; + ), + (MaybeTrait, + os << ent.type << ": ?" << ent.trait; + ), + (NotTrait, + os << ent.type << ": !" << ent.trait; + ), + (Equality, + os << ent.type << " = " << ent.replacement; + ) + ) + return os; +} + + +int GenericParams::find_name(const char* name) const +{ + for( unsigned int i = 0; i < m_type_params.size(); i ++ ) + { + if( m_type_params[i].name() == name ) + return i; + } + DEBUG("Type param '" << name << "' not in list"); + return -1; +} + +::std::ostream& operator<<(::std::ostream& os, const GenericParams& tps) +{ + return os << "<" << tps.m_lifetime_params << "," << tps.m_type_params << "> where {" << tps.m_bounds << "}"; +} + +} // namespace AST diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 4b48aa01..b63cb2e6 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -1,642 +1,642 @@ -/*
- * MRustC - Rust Compiler
- * - By John Hodge (Mutabah/thePowersGang)
- *
- * ast/ast.hpp
- * - Core AST header
- */
-#ifndef AST_HPP_INCLUDED
-#define AST_HPP_INCLUDED
-
-#include <string>
-#include <vector>
-#include <stdexcept>
-#include "../coretypes.hpp"
-#include <memory>
-#include <map>
-#include <unordered_map>
-#include <algorithm>
-
-#include "../parse/tokentree.hpp"
-#include "types.hpp"
-#include <serialise.hpp>
-
-#include <ast/pattern.hpp>
-#include <ast/attrs.hpp>
-#include <ast/expr_ptr.hpp>
-#include <ast/item.hpp>
-#include <ast/macro.hpp> // MacroInvocation
-
-#include "generics.hpp"
-
-#include <macro_rules/macro_rules_ptr.hpp>
-
-namespace AST {
-
-class Crate;
-
-class Module;
-class Item;
-
-using ::std::unique_ptr;
-using ::std::move;
-
-enum eItemType
-{
- ITEM_TRAIT,
- ITEM_STRUCT,
- ITEM_FN,
-};
-
-struct StructItem
-{
- ::AST::MetaItems m_attrs;
- bool m_is_public;
- ::std::string m_name;
- TypeRef m_type;
-
- //StructItem() {}
-
- StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty):
- m_attrs( mv$(attrs) ),
- m_is_public(is_pub),
- m_name( mv$(name) ),
- m_type( mv$(ty) )
- {
- }
-
- friend ::std::ostream& operator<<(::std::ostream& os, const StructItem& x) {
- return os << (x.m_is_public ? "pub " : "") << x.m_name << ": " << x.m_type;
- }
-
- StructItem clone() const;
-};
-
-struct TupleItem
-{
- ::AST::MetaItems m_attrs;
- bool m_is_public;
- TypeRef m_type;
-
- //TupleItem() {}
-
- TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty):
- m_attrs( mv$(attrs) ),
- m_is_public(is_pub),
- m_type( mv$(ty) )
- {
- }
-
- friend ::std::ostream& operator<<(::std::ostream& os, const TupleItem& x) {
- return os << (x.m_is_public ? "pub " : "") << x.m_type;
- }
-
- TupleItem clone() const;
-};
-
-class TypeAlias
-{
- GenericParams m_params;
- TypeRef m_type;
-public:
- //TypeAlias() {}
- TypeAlias(GenericParams params, TypeRef type):
- m_params( move(params) ),
- m_type( move(type) )
- {}
-
- const GenericParams& params() const { return m_params; }
- const TypeRef& type() const { return m_type; }
-
- GenericParams& params() { return m_params; }
- TypeRef& type() { return m_type; }
-
- TypeAlias clone() const;
-};
-
-class Static
-{
-public:
- enum Class
- {
- CONST,
- STATIC,
- MUT,
- };
-private:
- Class m_class;
- TypeRef m_type;
- Expr m_value;
-public:
- //Static():
- // m_class(CONST)
- //{}
- Static(Class s_class, TypeRef type, Expr value):
- m_class(s_class),
- m_type( move(type) ),
- m_value( move(value) )
- {}
-
- const Class& s_class() const { return m_class; }
- const TypeRef& type() const { return m_type; }
- const Expr& value() const { return m_value; }
-
- TypeRef& type() { return m_type; }
- Expr& value() { return m_value; }
-
- Static clone() const;
-};
-
-class Function
-{
-public:
- typedef ::std::vector< ::std::pair<AST::Pattern,TypeRef> > Arglist;
-
-private:
- Span m_span;
- //::std::string m_lifetime;
- GenericParams m_params;
- Expr m_code;
- TypeRef m_rettype;
- Arglist m_args;
-
- // TODO: ABI, const, and unsafe
- ::std::string m_abi;
- bool m_is_const;
- bool m_is_unsafe;
- bool m_is_variadic; // extern only
-public:
- Function(const Function&) = delete;
- Function& operator=(const Function&) = delete;
- Function(Function&&) = default;
- Function& operator=(Function&&) = default;
-
- //Function() {}
- Function(Span sp, GenericParams params, ::std::string abi, bool is_unsafe, bool is_const, bool is_variadic, TypeRef ret_type, Arglist args);
-
- void set_code(Expr code) { m_code = ::std::move(code); }
-
- const ::std::string& abi() const { return m_abi; };
- bool is_const() const { return m_is_const; }
- bool is_unsafe() const { return m_is_unsafe; }
- bool is_variadic() const { return m_is_variadic; }
-
- const GenericParams& params() const { return m_params; }
- GenericParams& params() { return m_params; }
- const Expr& code() const { return m_code; }
- Expr& code() { return m_code; }
- const TypeRef& rettype() const { return m_rettype; }
- TypeRef& rettype() { return m_rettype; }
- const Arglist& args() const { return m_args; }
- Arglist& args() { return m_args; }
-
- Function clone() const;
-};
-
-class Trait
-{
- GenericParams m_params;
- ::std::vector< Spanned<AST::Path> > m_supertraits;
-
- bool m_is_marker;
- NamedList<Item> m_items;
-public:
- Trait():
- m_is_marker(false)
- {}
- Trait(GenericParams params, ::std::vector< Spanned<Path> > supertraits):
- m_params( mv$(params) ),
- m_supertraits( mv$(supertraits) ),
- m_is_marker(false)
- {
- }
-
- const GenericParams& params() const { return m_params; }
- GenericParams& params() { return m_params; }
- const ::std::vector<Spanned<Path> >& supertraits() const { return m_supertraits; }
- ::std::vector<Spanned<Path> >& supertraits() { return m_supertraits; }
-
- const NamedList<Item>& items() const { return m_items; }
- NamedList<Item>& items() { return m_items; }
-
- void add_type(::std::string name, TypeRef type);
- void add_function(::std::string name, Function fcn);
- void add_static(::std::string name, Static v);
-
- void set_is_marker();
- bool is_marker() const;
-
- bool has_named_item(const ::std::string& name, bool& out_is_fcn) const;
-
- Trait clone() const;
-};
-
-TAGGED_UNION_EX(EnumVariantData, (), Value,
- (
- (Value, struct {
- ::AST::Expr m_value;
- }),
- (Tuple, struct {
- ::std::vector<TypeRef> m_sub_types;
- }),
- (Struct, struct {
- ::std::vector<StructItem> m_fields;
- })
- ),
- (), (),
- (
- public:
- )
- );
-
-struct EnumVariant
-{
- MetaItems m_attrs;
- ::std::string m_name;
- EnumVariantData m_data;
-
- EnumVariant()
- {
- }
-
- EnumVariant(MetaItems attrs, ::std::string name, Expr&& value):
- m_attrs( mv$(attrs) ),
- m_name( mv$(name) ),
- m_data( EnumVariantData::make_Value({mv$(value)}) )
- {
- }
-
- EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types):
- m_attrs( mv$(attrs) ),
- m_name( ::std::move(name) ),
- m_data( EnumVariantData::make_Tuple( {mv$(sub_types)} ) )
- {
- }
-
- EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<StructItem> fields):
- m_attrs( mv$(attrs) ),
- m_name( ::std::move(name) ),
- m_data( EnumVariantData::make_Struct( {mv$(fields)} ) )
- {
- }
-
- friend ::std::ostream& operator<<(::std::ostream& os, const EnumVariant& x)
- {
- os << "EnumVariant(" << x.m_name;
- TU_MATCH(EnumVariantData, (x.m_data), (e),
- (Value,
- os << " = " << e.m_value;
- ),
- (Tuple,
- os << "(" << e.m_sub_types << ")";
- ),
- (Struct,
- os << " { " << e.m_fields << " }";
- )
- )
- return os << ")";
- }
-};
-
-class Enum
-{
- GenericParams m_params;
- ::std::vector<EnumVariant> m_variants;
-public:
- Enum() {}
- Enum( GenericParams params, ::std::vector<EnumVariant> variants ):
- m_params( move(params) ),
- m_variants( move(variants) )
- {}
-
- const GenericParams& params() const { return m_params; }
- GenericParams& params() { return m_params; }
- const ::std::vector<EnumVariant>& variants() const { return m_variants; }
- ::std::vector<EnumVariant>& variants() { return m_variants; }
-
- Enum clone() const;
-};
-
-TAGGED_UNION_EX(StructData, (), Struct,
- (
- (Tuple, struct {
- ::std::vector<TupleItem> ents;
- }),
- (Struct, struct {
- ::std::vector<StructItem> ents;
- })
- ),
- (),(),
- (
- public:
- )
- );
-
-class Struct
-{
- GenericParams m_params;
-public:
- StructData m_data;
-
- Struct() {}
- Struct( GenericParams params, ::std::vector<StructItem> fields ):
- m_params( move(params) ),
- m_data( StructData::make_Struct({mv$(fields)}) )
- {}
- Struct( GenericParams params, ::std::vector<TupleItem> fields ):
- m_params( move(params) ),
- m_data( StructData::make_Tuple({mv$(fields)}) )
- {}
-
- const GenericParams& params() const { return m_params; }
- GenericParams& params() { return m_params; }
-
- Struct clone() const;
-};
-
-class Union
-{
-public:
- GenericParams m_params;
- ::std::vector<StructItem> m_variants;
-
- Union( GenericParams params, ::std::vector<StructItem> fields ):
- m_params( move(params) ),
- m_variants( mv$(fields) )
- {}
-
- Union clone() const;
-};
-
-class ImplDef
-{
- Span m_span;
- MetaItems m_attrs;
- GenericParams m_params;
- Spanned<Path> m_trait;
- TypeRef m_type;
-public:
- //ImplDef() {}
- ImplDef(ImplDef&&) /*noexcept*/ = default;
- ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned<Path> trait_type, TypeRef impl_type):
- m_span( mv$(sp) ),
- m_attrs( mv$(attrs) ),
- m_params( mv$(params) ),
- m_trait( mv$(trait_type) ),
- m_type( mv$(impl_type) )
- {}
- ImplDef& operator=(ImplDef&&) = default;
-
- // Accessors
- const Span& span() const { return m_span; }
- const MetaItems& attrs() const { return m_attrs; }
- MetaItems& attrs() { return m_attrs; }
-
- const GenericParams& params() const { return m_params; }
- GenericParams& params() { return m_params; }
- const Spanned<Path>& trait() const { return m_trait; }
- Spanned<Path>& trait() { return m_trait; }
- const TypeRef& type() const { return m_type; }
- TypeRef& type() { return m_type; }
-
-
- friend ::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl);
-};
-
-class Impl
-{
-public:
- struct ImplItem {
- bool is_pub; // Ignored for trait impls
- bool is_specialisable;
- ::std::string name;
-
- ::std::unique_ptr<Item> data;
- };
-
-private:
- ImplDef m_def;
- Span m_span;
-
- ::std::vector< ImplItem > m_items;
- //NamedList<TypeRef> m_types;
- //NamedList<Function> m_functions;
- //NamedList<Static> m_statics;
-
-public:
- //Impl() {}
- Impl(Impl&&) /*noexcept*/ = default;
- Impl(ImplDef def):
- m_def( mv$(def) )
- {}
- Impl& operator=(Impl&&) = default;
-
- void add_function(bool is_public, bool is_specialisable, ::std::string name, Function fcn);
- void add_type(bool is_public, bool is_specialisable, ::std::string name, TypeRef type);
- void add_static(bool is_public, bool is_specialisable, ::std::string name, Static v);
- void add_macro_invocation( MacroInvocation inv );
-
- const ImplDef& def() const { return m_def; }
- ImplDef& def() { return m_def; }
- const ::std::vector<ImplItem>& items() const { return m_items; }
- ::std::vector<ImplItem>& items() { return m_items; }
-
- bool has_named_item(const ::std::string& name) const;
-
- friend ::std::ostream& operator<<(::std::ostream& os, const Impl& impl);
-
-private:
-};
-
-struct UseStmt
-{
- Span sp;
- ::AST::Path path;
- ::AST::PathBinding alt_binding;
-
- UseStmt()
- {}
- UseStmt(Span sp, Path p):
- sp(sp),
- path(p)
- {
- }
-
- UseStmt clone() const;
-
- friend ::std::ostream& operator<<(::std::ostream& os, const UseStmt& x);
-};
-
-class ExternBlock
-{
- ::std::string m_abi;
- ::std::vector< Named<Item>> m_items;
-public:
- ExternBlock(::std::string abi):
- m_abi( mv$(abi) )
- {}
-
- const ::std::string& abi() const { return m_abi; }
-
- void add_item(Named<Item> named_item);
-
- // NOTE: Only Function and Static are valid.
- ::std::vector<Named<Item>>& items() { return m_items; }
- const ::std::vector<Named<Item>>& items() const { return m_items; }
-
- ExternBlock clone() const;
-};
-
-/// Representation of a parsed (and being converted) function
-class Module
-{
- ::AST::Path m_my_path;
-
- // Module-level items
- /// General items
- ::std::vector<Named<Item>> m_items;
-
- // --- Runtime caches and state ---
- ::std::vector< ::std::shared_ptr<Module> > m_anon_modules;
-
- ::std::vector< NamedNS<const MacroRules*> > m_macro_import_res; // Vec of imported macros (not serialised)
- ::std::vector< Named<MacroRulesPtr> > m_macros;
-
-public:
- struct FileInfo
- {
- bool controls_dir = false;
- ::std::string path = "!";
- };
-
- FileInfo m_file_info;
-
- bool m_insert_prelude = true; // Set to false by `#[no_prelude]` handler
- char m_index_populated = 0; // 0 = no, 1 = partial, 2 = complete
- struct IndexEnt {
- bool is_pub; // Used as part of glob import checking
- bool is_import; // Set if this item has a path that isn't `mod->path() + name`
- ::AST::Path path;
- };
-
- // TODO: Document difference between namespace and Type
- ::std::unordered_map< ::std::string, IndexEnt > m_namespace_items;
- ::std::unordered_map< ::std::string, IndexEnt > m_type_items;
- ::std::unordered_map< ::std::string, IndexEnt > m_value_items;
-
-public:
- Module() {}
- Module(::AST::Path path):
- m_my_path( mv$(path) )
- {
- }
-
- bool is_anon() const {
- return m_my_path.nodes().size() > 0 && m_my_path.nodes().back().name()[0] == '#';
- }
-
- /// Create an anon module (for use inside expressions)
- ::std::shared_ptr<AST::Module> add_anon();
-
- void add_item(Named<Item> item);
- void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs);
- void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs);
- void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs);
- void add_macro_invocation(MacroInvocation item);
-
- void add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro);
- void add_macro_import(::std::string name, const MacroRules& mr);
-
-
-
- const ::AST::Path& path() const { return m_my_path; }
-
- ::std::vector<Named<Item>>& items() { return m_items; }
- const ::std::vector<Named<Item>>& items() const { return m_items; }
-
- ::std::vector< ::std::shared_ptr<Module> >& anon_mods() { return m_anon_modules; }
- const ::std::vector< ::std::shared_ptr<Module> >& anon_mods() const { return m_anon_modules; }
-
-
- NamedList<MacroRulesPtr>& macros() { return m_macros; }
- const NamedList<MacroRulesPtr>& macros() const { return m_macros; }
- const ::std::vector<NamedNS<const MacroRules*> > macro_imports_res() const { return m_macro_import_res; }
-
-private:
- void resolve_macro_import(const Crate& crate, const ::std::string& modname, const ::std::string& macro_name);
-};
-
-TAGGED_UNION_EX(Item, (), None,
- (
- (None, struct {} ),
- (MacroInv, MacroInvocation),
- (Use, UseStmt),
-
- // Nameless items
- (ExternBlock, ExternBlock),
- (Impl, Impl),
- (NegImpl, ImplDef),
-
- (Module, Module),
- (Crate, struct {
- ::std::string name;
- }),
-
- (Type, TypeAlias),
- (Struct, Struct),
- (Enum, Enum),
- (Union, Union),
- (Trait, Trait),
-
- (Function, Function),
- (Static, Static)
- ),
-
- (, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);),
- (
- public:
- MetaItems attrs;
- Span span;
-
- Item clone() const;
- )
- );
-
-
-struct ImplRef
-{
- const Impl& impl;
- ::std::vector<TypeRef> params;
-
- ImplRef(const Impl& impl, ::std::vector<TypeRef> params):
- impl(impl),
- params( mv$(params) )
- {}
-
- ::rust::option<char> find_named_item(const ::std::string& name) const;
-};
-
-} // namespace AST
-
-class GenericResolveClosure
-{
- const ::AST::GenericParams& m_params;
- const ::std::vector<TypeRef>& m_args;
-public:
- GenericResolveClosure(const AST::GenericParams& params, const ::std::vector<TypeRef>& args):
- m_params(params),
- m_args(args)
- {}
- const TypeRef& operator()(const char *argname) {
- for(unsigned int i = 0; i < m_params.ty_params().size(); i ++)
- {
- if( m_params.ty_params()[i].name() == argname ) {
- return m_args.at(i);
- }
- }
- throw ::std::runtime_error("BUGCHECK - Unknown arg in field type");
- }
-};
-
-
-#endif // AST_HPP_INCLUDED
+/* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * ast/ast.hpp + * - Core AST header + */ +#ifndef AST_HPP_INCLUDED +#define AST_HPP_INCLUDED + +#include <string> +#include <vector> +#include <stdexcept> +#include "../coretypes.hpp" +#include <memory> +#include <map> +#include <unordered_map> +#include <algorithm> + +#include "../parse/tokentree.hpp" +#include "types.hpp" +#include <serialise.hpp> + +#include <ast/pattern.hpp> +#include <ast/attrs.hpp> +#include <ast/expr_ptr.hpp> +#include <ast/item.hpp> +#include <ast/macro.hpp> // MacroInvocation + +#include "generics.hpp" + +#include <macro_rules/macro_rules_ptr.hpp> + +namespace AST { + +class Crate; + +class Module; +class Item; + +using ::std::unique_ptr; +using ::std::move; + +enum eItemType +{ + ITEM_TRAIT, + ITEM_STRUCT, + ITEM_FN, +}; + +struct StructItem +{ + ::AST::MetaItems m_attrs; + bool m_is_public; + ::std::string m_name; + TypeRef m_type; + + //StructItem() {} + + StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty): + m_attrs( mv$(attrs) ), + m_is_public(is_pub), + m_name( mv$(name) ), + m_type( mv$(ty) ) + { + } + + friend ::std::ostream& operator<<(::std::ostream& os, const StructItem& x) { + return os << (x.m_is_public ? "pub " : "") << x.m_name << ": " << x.m_type; + } + + StructItem clone() const; +}; + +struct TupleItem +{ + ::AST::MetaItems m_attrs; + bool m_is_public; + TypeRef m_type; + + //TupleItem() {} + + TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty): + m_attrs( mv$(attrs) ), + m_is_public(is_pub), + m_type( mv$(ty) ) + { + } + + friend ::std::ostream& operator<<(::std::ostream& os, const TupleItem& x) { + return os << (x.m_is_public ? "pub " : "") << x.m_type; + } + + TupleItem clone() const; +}; + +class TypeAlias +{ + GenericParams m_params; + TypeRef m_type; +public: + //TypeAlias() {} + TypeAlias(GenericParams params, TypeRef type): + m_params( move(params) ), + m_type( move(type) ) + {} + + const GenericParams& params() const { return m_params; } + const TypeRef& type() const { return m_type; } + + GenericParams& params() { return m_params; } + TypeRef& type() { return m_type; } + + TypeAlias clone() const; +}; + +class Static +{ +public: + enum Class + { + CONST, + STATIC, + MUT, + }; +private: + Class m_class; + TypeRef m_type; + Expr m_value; +public: + //Static(): + // m_class(CONST) + //{} + Static(Class s_class, TypeRef type, Expr value): + m_class(s_class), + m_type( move(type) ), + m_value( move(value) ) + {} + + const Class& s_class() const { return m_class; } + const TypeRef& type() const { return m_type; } + const Expr& value() const { return m_value; } + + TypeRef& type() { return m_type; } + Expr& value() { return m_value; } + + Static clone() const; +}; + +class Function +{ +public: + typedef ::std::vector< ::std::pair<AST::Pattern,TypeRef> > Arglist; + +private: + Span m_span; + //::std::string m_lifetime; + GenericParams m_params; + Expr m_code; + TypeRef m_rettype; + Arglist m_args; + + // TODO: ABI, const, and unsafe + ::std::string m_abi; + bool m_is_const; + bool m_is_unsafe; + bool m_is_variadic; // extern only +public: + Function(const Function&) = delete; + Function& operator=(const Function&) = delete; + Function(Function&&) = default; + Function& operator=(Function&&) = default; + + //Function() {} + Function(Span sp, GenericParams params, ::std::string abi, bool is_unsafe, bool is_const, bool is_variadic, TypeRef ret_type, Arglist args); + + void set_code(Expr code) { m_code = ::std::move(code); } + + const ::std::string& abi() const { return m_abi; }; + bool is_const() const { return m_is_const; } + bool is_unsafe() const { return m_is_unsafe; } + bool is_variadic() const { return m_is_variadic; } + + const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } + const Expr& code() const { return m_code; } + Expr& code() { return m_code; } + const TypeRef& rettype() const { return m_rettype; } + TypeRef& rettype() { return m_rettype; } + const Arglist& args() const { return m_args; } + Arglist& args() { return m_args; } + + Function clone() const; +}; + +class Trait +{ + GenericParams m_params; + ::std::vector< Spanned<AST::Path> > m_supertraits; + + bool m_is_marker; + NamedList<Item> m_items; +public: + Trait(): + m_is_marker(false) + {} + Trait(GenericParams params, ::std::vector< Spanned<Path> > supertraits): + m_params( mv$(params) ), + m_supertraits( mv$(supertraits) ), + m_is_marker(false) + { + } + + const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } + const ::std::vector<Spanned<Path> >& supertraits() const { return m_supertraits; } + ::std::vector<Spanned<Path> >& supertraits() { return m_supertraits; } + + const NamedList<Item>& items() const { return m_items; } + NamedList<Item>& items() { return m_items; } + + void add_type(::std::string name, TypeRef type); + void add_function(::std::string name, Function fcn); + void add_static(::std::string name, Static v); + + void set_is_marker(); + bool is_marker() const; + + bool has_named_item(const ::std::string& name, bool& out_is_fcn) const; + + Trait clone() const; +}; + +TAGGED_UNION_EX(EnumVariantData, (), Value, + ( + (Value, struct { + ::AST::Expr m_value; + }), + (Tuple, struct { + ::std::vector<TypeRef> m_sub_types; + }), + (Struct, struct { + ::std::vector<StructItem> m_fields; + }) + ), + (), (), + ( + public: + ) + ); + +struct EnumVariant +{ + MetaItems m_attrs; + ::std::string m_name; + EnumVariantData m_data; + + EnumVariant() + { + } + + EnumVariant(MetaItems attrs, ::std::string name, Expr&& value): + m_attrs( mv$(attrs) ), + m_name( mv$(name) ), + m_data( EnumVariantData::make_Value({mv$(value)}) ) + { + } + + EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types): + m_attrs( mv$(attrs) ), + m_name( ::std::move(name) ), + m_data( EnumVariantData::make_Tuple( {mv$(sub_types)} ) ) + { + } + + EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<StructItem> fields): + m_attrs( mv$(attrs) ), + m_name( ::std::move(name) ), + m_data( EnumVariantData::make_Struct( {mv$(fields)} ) ) + { + } + + friend ::std::ostream& operator<<(::std::ostream& os, const EnumVariant& x) + { + os << "EnumVariant(" << x.m_name; + TU_MATCH(EnumVariantData, (x.m_data), (e), + (Value, + os << " = " << e.m_value; + ), + (Tuple, + os << "(" << e.m_sub_types << ")"; + ), + (Struct, + os << " { " << e.m_fields << " }"; + ) + ) + return os << ")"; + } +}; + +class Enum +{ + GenericParams m_params; + ::std::vector<EnumVariant> m_variants; +public: + Enum() {} + Enum( GenericParams params, ::std::vector<EnumVariant> variants ): + m_params( move(params) ), + m_variants( move(variants) ) + {} + + const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } + const ::std::vector<EnumVariant>& variants() const { return m_variants; } + ::std::vector<EnumVariant>& variants() { return m_variants; } + + Enum clone() const; +}; + +TAGGED_UNION_EX(StructData, (), Struct, + ( + (Tuple, struct { + ::std::vector<TupleItem> ents; + }), + (Struct, struct { + ::std::vector<StructItem> ents; + }) + ), + (),(), + ( + public: + ) + ); + +class Struct +{ + GenericParams m_params; +public: + StructData m_data; + + Struct() {} + Struct( GenericParams params, ::std::vector<StructItem> fields ): + m_params( move(params) ), + m_data( StructData::make_Struct({mv$(fields)}) ) + {} + Struct( GenericParams params, ::std::vector<TupleItem> fields ): + m_params( move(params) ), + m_data( StructData::make_Tuple({mv$(fields)}) ) + {} + + const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } + + Struct clone() const; +}; + +class Union +{ +public: + GenericParams m_params; + ::std::vector<StructItem> m_variants; + + Union( GenericParams params, ::std::vector<StructItem> fields ): + m_params( move(params) ), + m_variants( mv$(fields) ) + {} + + Union clone() const; +}; + +class ImplDef +{ + Span m_span; + MetaItems m_attrs; + GenericParams m_params; + Spanned<Path> m_trait; + TypeRef m_type; +public: + //ImplDef() {} + ImplDef(ImplDef&&) /*noexcept*/ = default; + ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned<Path> trait_type, TypeRef impl_type): + m_span( mv$(sp) ), + m_attrs( mv$(attrs) ), + m_params( mv$(params) ), + m_trait( mv$(trait_type) ), + m_type( mv$(impl_type) ) + {} + ImplDef& operator=(ImplDef&&) = default; + + // Accessors + const Span& span() const { return m_span; } + const MetaItems& attrs() const { return m_attrs; } + MetaItems& attrs() { return m_attrs; } + + const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } + const Spanned<Path>& trait() const { return m_trait; } + Spanned<Path>& trait() { return m_trait; } + const TypeRef& type() const { return m_type; } + TypeRef& type() { return m_type; } + + + friend ::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl); +}; + +class Impl +{ +public: + struct ImplItem { + bool is_pub; // Ignored for trait impls + bool is_specialisable; + ::std::string name; + + ::std::unique_ptr<Item> data; + }; + +private: + ImplDef m_def; + Span m_span; + + ::std::vector< ImplItem > m_items; + //NamedList<TypeRef> m_types; + //NamedList<Function> m_functions; + //NamedList<Static> m_statics; + +public: + //Impl() {} + Impl(Impl&&) /*noexcept*/ = default; + Impl(ImplDef def): + m_def( mv$(def) ) + {} + Impl& operator=(Impl&&) = default; + + void add_function(bool is_public, bool is_specialisable, ::std::string name, Function fcn); + void add_type(bool is_public, bool is_specialisable, ::std::string name, TypeRef type); + void add_static(bool is_public, bool is_specialisable, ::std::string name, Static v); + void add_macro_invocation( MacroInvocation inv ); + + const ImplDef& def() const { return m_def; } + ImplDef& def() { return m_def; } + const ::std::vector<ImplItem>& items() const { return m_items; } + ::std::vector<ImplItem>& items() { return m_items; } + + bool has_named_item(const ::std::string& name) const; + + friend ::std::ostream& operator<<(::std::ostream& os, const Impl& impl); + +private: +}; + +struct UseStmt +{ + Span sp; + ::AST::Path path; + ::AST::PathBinding alt_binding; + + UseStmt() + {} + UseStmt(Span sp, Path p): + sp(sp), + path(p) + { + } + + UseStmt clone() const; + + friend ::std::ostream& operator<<(::std::ostream& os, const UseStmt& x); +}; + +class ExternBlock +{ + ::std::string m_abi; + ::std::vector< Named<Item>> m_items; +public: + ExternBlock(::std::string abi): + m_abi( mv$(abi) ) + {} + + const ::std::string& abi() const { return m_abi; } + + void add_item(Named<Item> named_item); + + // NOTE: Only Function and Static are valid. + ::std::vector<Named<Item>>& items() { return m_items; } + const ::std::vector<Named<Item>>& items() const { return m_items; } + + ExternBlock clone() const; +}; + +/// Representation of a parsed (and being converted) function +class Module +{ + ::AST::Path m_my_path; + + // Module-level items + /// General items + ::std::vector<Named<Item>> m_items; + + // --- Runtime caches and state --- + ::std::vector< ::std::shared_ptr<Module> > m_anon_modules; + + ::std::vector< NamedNS<const MacroRules*> > m_macro_import_res; // Vec of imported macros (not serialised) + ::std::vector< Named<MacroRulesPtr> > m_macros; + +public: + struct FileInfo + { + bool controls_dir = false; + ::std::string path = "!"; + }; + + FileInfo m_file_info; + + bool m_insert_prelude = true; // Set to false by `#[no_prelude]` handler + char m_index_populated = 0; // 0 = no, 1 = partial, 2 = complete + struct IndexEnt { + bool is_pub; // Used as part of glob import checking + bool is_import; // Set if this item has a path that isn't `mod->path() + name` + ::AST::Path path; + }; + + // TODO: Document difference between namespace and Type + ::std::unordered_map< ::std::string, IndexEnt > m_namespace_items; + ::std::unordered_map< ::std::string, IndexEnt > m_type_items; + ::std::unordered_map< ::std::string, IndexEnt > m_value_items; + +public: + Module() {} + Module(::AST::Path path): + m_my_path( mv$(path) ) + { + } + + bool is_anon() const { + return m_my_path.nodes().size() > 0 && m_my_path.nodes().back().name()[0] == '#'; + } + + /// Create an anon module (for use inside expressions) + ::std::shared_ptr<AST::Module> add_anon(); + + void add_item(Named<Item> item); + void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs); + void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs); + void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs); + void add_macro_invocation(MacroInvocation item); + + void add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro); + void add_macro_import(::std::string name, const MacroRules& mr); + + + + const ::AST::Path& path() const { return m_my_path; } + + ::std::vector<Named<Item>>& items() { return m_items; } + const ::std::vector<Named<Item>>& items() const { return m_items; } + + ::std::vector< ::std::shared_ptr<Module> >& anon_mods() { return m_anon_modules; } + const ::std::vector< ::std::shared_ptr<Module> >& anon_mods() const { return m_anon_modules; } + + + NamedList<MacroRulesPtr>& macros() { return m_macros; } + const NamedList<MacroRulesPtr>& macros() const { return m_macros; } + const ::std::vector<NamedNS<const MacroRules*> > macro_imports_res() const { return m_macro_import_res; } + +private: + void resolve_macro_import(const Crate& crate, const ::std::string& modname, const ::std::string& macro_name); +}; + +TAGGED_UNION_EX(Item, (), None, + ( + (None, struct {} ), + (MacroInv, MacroInvocation), + (Use, UseStmt), + + // Nameless items + (ExternBlock, ExternBlock), + (Impl, Impl), + (NegImpl, ImplDef), + + (Module, Module), + (Crate, struct { + ::std::string name; + }), + + (Type, TypeAlias), + (Struct, Struct), + (Enum, Enum), + (Union, Union), + (Trait, Trait), + + (Function, Function), + (Static, Static) + ), + + (, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);), + ( + public: + MetaItems attrs; + Span span; + + Item clone() const; + ) + ); + + +struct ImplRef +{ + const Impl& impl; + ::std::vector<TypeRef> params; + + ImplRef(const Impl& impl, ::std::vector<TypeRef> params): + impl(impl), + params( mv$(params) ) + {} + + ::rust::option<char> find_named_item(const ::std::string& name) const; +}; + +} // namespace AST + +class GenericResolveClosure +{ + const ::AST::GenericParams& m_params; + const ::std::vector<TypeRef>& m_args; +public: + GenericResolveClosure(const AST::GenericParams& params, const ::std::vector<TypeRef>& args): + m_params(params), + m_args(args) + {} + const TypeRef& operator()(const char *argname) { + for(unsigned int i = 0; i < m_params.ty_params().size(); i ++) + { + if( m_params.ty_params()[i].name() == argname ) { + return m_args.at(i); + } + } + throw ::std::runtime_error("BUGCHECK - Unknown arg in field type"); + } +}; + + +#endif // AST_HPP_INCLUDED diff --git a/src/ast/attrs.hpp b/src/ast/attrs.hpp index 372d888a..28bfec96 100644 --- a/src/ast/attrs.hpp +++ b/src/ast/attrs.hpp @@ -12,7 +12,7 @@ class MetaItems public: Span m_span; ::std::vector<MetaItem> m_items; - + virtual ~MetaItems(); MetaItems() {} MetaItems(MetaItems&&) = default; @@ -23,17 +23,17 @@ public: m_items( mv$(items) ) { } - + void push_back(MetaItem i); - + MetaItems clone() const; - + MetaItem* get(const char *name) { return const_cast<MetaItem*>( const_cast<const MetaItems*>(this)->get(name)); } const MetaItem* get(const char *name) const; bool has(const char *name) const { return get(name) != 0; } - + friend ::std::ostream& operator<<(::std::ostream& os, const MetaItems& x) { return os << "[" << x.m_items << "]"; } @@ -74,21 +74,21 @@ public: m_data( MetaItemData::make_List({mv$(items)}) ) { } - + MetaItem clone() const; - + void mark_used() {} const ::std::string& name() const { return m_name; } - + bool has_noarg() const { return m_data.is_None(); } - + bool has_string() const { return m_data.is_String(); } const ::std::string& string() const { return m_data.as_String().val; } - + bool has_sub_items() const { return m_data.is_List(); } const ::std::vector<MetaItem>& items() const { return m_data.as_List().sub_items; } ::std::vector<MetaItem>& items() { return m_data.as_List().sub_items; } - + friend ::std::ostream& operator<<(::std::ostream& os, const MetaItem& x) { os << x.m_name; TU_MATCH(MetaItemData, (x.m_data), (e), diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp index b18ca662..58a08980 100644 --- a/src/ast/crate.cpp +++ b/src/ast/crate.cpp @@ -61,13 +61,13 @@ void Crate::load_externs() } }; iterate_module(m_root_module, cb); - + // Check for no_std or no_core, and load libstd/libcore // - Duplicates some of the logic in "Expand", but also helps keep crate loading separate to most of expand // NOTE: Not all crates are loaded here, any crates loaded by macro invocations will be done during expand. bool no_std = false; bool no_core = false; - + for( const auto& a : this->m_attrs.m_items ) { if( a.name() == "no_std" ) @@ -105,7 +105,7 @@ void Crate::load_extern_crate(Span sp, const ::std::string& name) ::std::string path; for(const auto& p : paths){ path = p + "lib" + name + ".hir"; - + if( ::std::ifstream(path).good() ) { break ; } @@ -113,10 +113,10 @@ void Crate::load_extern_crate(Span sp, const ::std::string& name) if( !::std::ifstream(path).good() ) { ERROR(sp, E0000, "Unable to locate crate '" << name << "'"); } - + auto res = m_extern_crates.insert(::std::make_pair( name, ExternCrate { name, path } )); auto crate_ext_list = mv$( res.first->second.m_hir->m_ext_crates ); - + // Load referenced crates for( const auto& ext : crate_ext_list ) { @@ -132,7 +132,7 @@ ExternCrate::ExternCrate(const ::std::string& name, const ::std::string& path): { TRACE_FUNCTION_F("name=" << name << ", path='" << path << "'"); m_hir = HIR_Deserialise(path, name); - + m_hir->post_load_update(name); } diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp index 31076c02..c51dacf5 100644 --- a/src/ast/crate.hpp +++ b/src/ast/crate.hpp @@ -14,14 +14,14 @@ class Crate { public: ::AST::MetaItems m_attrs; - + ::std::map< ::std::string, ::AST::Path> m_lang_items; public: Module m_root_module; ::std::map< ::std::string, ExternCrate> m_extern_crates; // Mapping filled by searching for (?visible) macros with is_pub=true ::std::map< ::std::string, const MacroRules*> m_exported_macros; - + enum class Type { Unknown, RustLib, @@ -37,15 +37,15 @@ public: ::std::string m_crate_name; AST::Path m_prelude_path; - + Crate(); const Module& root_module() const { return m_root_module; } Module& root_module() { return m_root_module; } - + /// Load referenced crates void load_externs(); - + void load_extern_crate(Span sp, const ::std::string& name); }; @@ -55,14 +55,14 @@ class ExternCrate public: ::std::string m_name; ::HIR::CratePtr m_hir; - + ExternCrate(const ::std::string& name, const ::std::string& path); - + ExternCrate(ExternCrate&&) = default; ExternCrate& operator=(ExternCrate&&) = default; ExternCrate(const ExternCrate&) = delete; ExternCrate& operator=(const ExternCrate& ) = delete; - + void with_all_macros(::std::function<void(const ::std::string& , const MacroRules&)> cb) const; const MacroRules* find_macro_rules(const ::std::string& name) const; }; diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index 4773a845..07343de4 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -1,7 +1,7 @@ /* * MRustC - Mutabah's Rust Compiler * - By John Hodge (Mutabah/thePowersGang) - * + * * ast/dump.cpp * - Dumps the AST of a crate as rust code (annotated) */ @@ -30,7 +30,7 @@ public: m_indent_level(0), m_expr_root(false) {} - + void handle_module(const AST::Module& mod); void handle_struct(const AST::Struct& s); void handle_enum(const AST::Enum& s); @@ -170,7 +170,7 @@ public: virtual void visit(AST::ExprNode_Loop& n) override { bool expr_root = m_expr_root; m_expr_root = false; - + switch(n.m_type) { case AST::ExprNode_Loop::LOOP: @@ -193,7 +193,7 @@ public: AST::NodeVisitor::visit(n.m_cond); break; } - + if( expr_root ) { m_os << "\n"; @@ -211,7 +211,7 @@ public: m_expr_root = false; m_os << "match "; AST::NodeVisitor::visit(n.m_val); - + if(expr_root) { m_os << "\n"; @@ -222,7 +222,7 @@ public: m_os << " {\n"; inc_indent(); } - + for( auto& arm : n.m_arms ) { m_os << indent(); @@ -245,7 +245,7 @@ public: dec_indent(); m_os << ",\n"; } - + if(expr_root) { m_os << indent() << "}"; @@ -261,7 +261,7 @@ public: m_expr_root = false; m_os << "if "; AST::NodeVisitor::visit(n.m_cond); - + visit_if_common(expr_root, n.m_true, n.m_false); } virtual void visit(AST::ExprNode_IfLet& n) override { @@ -271,7 +271,7 @@ public: print_pattern(n.m_pattern, true); m_os << " = "; AST::NodeVisitor::visit(n.m_value); - + visit_if_common(expr_root, n.m_true, n.m_false); } void visit_if_common(bool expr_root, const ::std::unique_ptr<AST::ExprNode>& tv, const ::std::unique_ptr<AST::ExprNode>& fv) @@ -392,7 +392,7 @@ public: m_expr_root = false; m_os << "b\"" << n.m_value << "\""; } - + virtual void visit(AST::ExprNode_StructLiteral& n) override { m_expr_root = false; m_os << n.m_path << " {\n"; @@ -533,7 +533,7 @@ public: case AST::ExprNode_UniOp::REFMUT: m_os << "&mut "; break; case AST::ExprNode_UniOp::QMARK: break; } - + if( IS(*n.m_value, AST::ExprNode_BinOp) ) m_os << "("; AST::NodeVisitor::visit(n.m_value); @@ -553,14 +553,14 @@ private: AST::NodeVisitor::visit(node); m_os << ")"; } - + void print_attrs(const AST::MetaItems& attrs); void print_params(const AST::GenericParams& params); void print_bounds(const AST::GenericParams& params); void print_pattern_tuple(const AST::Pattern::TuplePat& v, bool is_refutable); void print_pattern(const AST::Pattern& p, bool is_refutable); void print_type(const TypeRef& t); - + void inc_indent(); RepeatLitStr indent(); void dec_indent(); @@ -584,7 +584,7 @@ void RustPrinter::print_attrs(const AST::MetaItems& attrs) void RustPrinter::handle_module(const AST::Module& mod) { bool need_nl = true; - + for( const auto& i : mod.items() ) { if( !i.data.is_Use() ) continue ; @@ -608,12 +608,12 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << ";\n"; } need_nl = true; - + for( const auto& item : mod.items() ) { if( !item.data.is_Module() ) continue ; const auto& e = item.data.as_Module(); - + m_os << "\n"; m_os << indent() << (item.is_pub ? "pub " : "") << "mod " << item.name << "\n"; m_os << indent() << "{\n"; @@ -623,12 +623,12 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << indent() << "}\n"; m_os << "\n"; } - + for( const auto& item : mod.items() ) { if( !item.data.is_Type() ) continue ; const auto& e = item.data.as_Type(); - + if(need_nl) { m_os << "\n"; need_nl = false; @@ -641,45 +641,45 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << ";\n"; } need_nl = true; - + for( const auto& item : mod.items() ) { if( !item.data.is_Struct() ) continue ; const auto& e = item.data.as_Struct(); - + m_os << "\n"; print_attrs(item.data.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "struct " << item.name; handle_struct(e); } - + for( const auto& item : mod.items() ) { if( !item.data.is_Enum() ) continue ; const auto& e = item.data.as_Enum(); - + m_os << "\n"; print_attrs(item.data.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "enum " << item.name; handle_enum(e); } - + for( const auto& item : mod.items() ) { if( !item.data.is_Trait() ) continue ; const auto& e = item.data.as_Trait(); - + m_os << "\n"; print_attrs(item.data.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "trait " << item.name; handle_trait(e); } - + for( const auto& item : mod.items() ) { if( !item.data.is_Static() ) continue ; const auto& e = item.data.as_Static(); - + if(need_nl) { m_os << "\n"; need_nl = false; @@ -696,12 +696,12 @@ void RustPrinter::handle_module(const AST::Module& mod) e.value().visit_nodes(*this); m_os << ";\n"; } - + for( const auto& item : mod.items() ) { if( !item.data.is_Function() ) continue ; const auto& e = item.data.as_Function(); - + m_os << "\n"; print_attrs(item.data.attrs); handle_function(item.is_pub, item.name, e); @@ -711,7 +711,7 @@ void RustPrinter::handle_module(const AST::Module& mod) { if( !item.data.is_Impl() ) continue ; const auto& i = item.data.as_Impl(); - + m_os << "\n"; m_os << indent() << "impl"; print_params(i.def().params()); @@ -720,7 +720,7 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << " " << i.def().trait().ent << " for"; } m_os << " " << i.def().type() << "\n"; - + print_bounds(i.def().params()); m_os << indent() << "{\n"; inc_indent(); @@ -796,13 +796,13 @@ void RustPrinter::print_bounds(const AST::GenericParams& params) m_os << indent() << "where\n"; inc_indent(); bool is_first = true; - + for( const auto& b : params.bounds() ) { if( !is_first ) m_os << ",\n"; is_first = false; - + m_os << indent(); TU_MATCH(AST::GenericBound, (b), (ent), (Lifetime, @@ -829,7 +829,7 @@ void RustPrinter::print_bounds(const AST::GenericParams& params) ) } m_os << "\n"; - + dec_indent(); } } @@ -934,7 +934,7 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable) m_os << v.leading; m_os << ", "; } - + if(v.extra_bind.is_valid()) { const auto& b = v.extra_bind; @@ -955,7 +955,7 @@ void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable) } m_os << ".."; needs_comma = true; - + if(v.trailing.size()) { if( needs_comma ) { m_os << ", "; @@ -975,7 +975,7 @@ void RustPrinter::print_type(const TypeRef& t) void RustPrinter::handle_struct(const AST::Struct& s) { print_params(s.params()); - + TU_MATCH(AST::StructData, (s.m_data), (e), (Tuple, if( e.ents.size() == 0 ) @@ -997,7 +997,7 @@ void RustPrinter::handle_struct(const AST::Struct& s) (Struct, m_os << "\n"; print_bounds(s.params()); - + m_os << indent() << "{\n"; inc_indent(); for( const auto& i : e.ents ) @@ -1060,7 +1060,7 @@ void RustPrinter::handle_trait(const AST::Trait& s) m_os << indent() << "{\n"; inc_indent(); - + for( const auto& i : s.items() ) { TU_MATCH_DEF(AST::Item, (i.data), (e), @@ -1074,7 +1074,7 @@ void RustPrinter::handle_trait(const AST::Trait& s) ) ) } - + dec_indent(); m_os << indent() << "}\n"; m_os << "\n"; @@ -1107,12 +1107,12 @@ void RustPrinter::handle_function(bool is_pub, const ::std::string& name, const { m_os << " -> " << f.rettype().print_pretty(); } - + if( f.code().is_valid() ) { m_os << "\n"; print_bounds(f.params()); - + m_os << indent(); f.code().visit_nodes(*this); m_os << "\n"; diff --git a/src/ast/expr.cpp b/src/ast/expr.cpp index d3fa1989..cc6ca98f 100644 --- a/src/ast/expr.cpp +++ b/src/ast/expr.cpp @@ -5,7 +5,7 @@ namespace AST { - + Expr::Expr(unique_ptr<ExprNode> node): m_node(node.release()) { @@ -188,7 +188,7 @@ NODE(ExprNode_Match, { os << " " << pat; if( arm.m_cond ) os << " if " << *arm.m_cond; - + os << " => " << *arm.m_code << ","; } os << "}"; @@ -258,11 +258,11 @@ NODE(ExprNode_StructLiteral, { os << "/* todo: sl */"; },{ ExprNode_StructLiteral::t_values vals; - + for(const auto& v : m_values) { vals.push_back( ::std::make_pair(v.first, v.second->clone()) ); } - + return NEWNODE(ExprNode_StructLiteral, AST::Path(m_path), OPT_CLONE(m_base_value), mv$(vals) ); }) diff --git a/src/ast/expr.hpp b/src/ast/expr.hpp index e955a4a4..4c80bcb5 100644 --- a/src/ast/expr.hpp +++ b/src/ast/expr.hpp @@ -24,11 +24,11 @@ class ExprNode Position m_pos; public: virtual ~ExprNode() = 0; - + virtual void visit(NodeVisitor& nv) = 0; virtual void print(::std::ostream& os) const = 0; virtual ::std::unique_ptr<ExprNode> clone() const = 0; - + void set_pos(Position p) { m_pos = ::std::move(p); } const Position& get_pos() const { return m_pos; } Span span() const { return m_pos; } @@ -37,7 +37,7 @@ public: m_attrs = mv$(mi); } MetaItems& attrs() { return m_attrs; } - + static ::std::unique_ptr<ExprNode> from_deserialiser(Deserialiser& d); }; typedef ::std::unique_ptr<ExprNode> ExprNodeP; @@ -68,7 +68,7 @@ struct ExprNode_Block: m_nodes( move(nodes) ) { } - + NODE_METHODS(); }; @@ -78,13 +78,13 @@ struct ExprNode_Macro: ::std::string m_name; ::std::string m_ident; ::TokenTree m_tokens; - + ExprNode_Macro(::std::string name, ::std::string ident, ::TokenTree&& tokens): m_name(name), m_ident(ident), m_tokens( move(tokens) ) {} - + NODE_METHODS(); }; @@ -106,7 +106,7 @@ struct ExprNode_Flow: m_value( move(value) ) { } - + NODE_METHODS(); }; struct ExprNode_LetBinding: @@ -122,7 +122,7 @@ struct ExprNode_LetBinding: m_value( move(value) ) { } - + NODE_METHODS(); }; struct ExprNode_Assign: @@ -145,7 +145,7 @@ struct ExprNode_Assign: m_value( move(value) ) { } - + NODE_METHODS(); }; struct ExprNode_CallPath: @@ -159,7 +159,7 @@ struct ExprNode_CallPath: m_args( move(args) ) { } - + NODE_METHODS(); }; struct ExprNode_CallMethod: @@ -175,7 +175,7 @@ struct ExprNode_CallMethod: m_args( move(args) ) { } - + NODE_METHODS(); }; // Call an object (Fn/FnMut/FnOnce) @@ -234,7 +234,7 @@ struct ExprNode_Match_Arm MetaItems m_attrs; ::std::vector<Pattern> m_patterns; unique_ptr<ExprNode> m_cond; - + unique_ptr<ExprNode> m_code; @@ -246,7 +246,7 @@ struct ExprNode_Match_Arm m_code( mv$(code) ) {} }; - + struct ExprNode_Match: public ExprNode { @@ -305,7 +305,7 @@ struct ExprNode_Integer: m_value(value) { } - + NODE_METHODS(); }; // Literal float @@ -320,7 +320,7 @@ struct ExprNode_Float: m_value(value) { } - + NODE_METHODS(); }; // Literal boolean @@ -333,7 +333,7 @@ struct ExprNode_Bool: m_value(value) { } - + NODE_METHODS(); }; // Literal string @@ -341,11 +341,11 @@ struct ExprNode_String: public ExprNode { ::std::string m_value; - + ExprNode_String(::std::string value): m_value( ::std::move(value) ) {} - + NODE_METHODS(); }; // Literal byte string @@ -353,11 +353,11 @@ struct ExprNode_ByteString: public ExprNode { ::std::string m_value; - + ExprNode_ByteString(::std::string value): m_value( ::std::move(value) ) {} - + NODE_METHODS(); }; @@ -366,19 +366,19 @@ struct ExprNode_Closure: public ExprNode { typedef ::std::vector< ::std::pair<AST::Pattern, TypeRef> > args_t; - + args_t m_args; TypeRef m_return; unique_ptr<ExprNode> m_code; bool m_is_move; - + ExprNode_Closure(args_t args, TypeRef rv, unique_ptr<ExprNode> code, bool is_move): m_args( ::std::move(args) ), m_return( ::std::move(rv) ), m_code( ::std::move(code) ), m_is_move( is_move ) {} - + NODE_METHODS(); }; // Literal structure @@ -395,7 +395,7 @@ struct ExprNode_StructLiteral: m_base_value( move(base_value) ), m_values( move(values) ) {} - + NODE_METHODS(); }; // Array @@ -404,7 +404,7 @@ struct ExprNode_Array: { unique_ptr<ExprNode> m_size; // if non-NULL, it's a sized array ::std::vector< unique_ptr<ExprNode> > m_values; - + ExprNode_Array(::std::vector< unique_ptr<ExprNode> > vals): m_values( ::std::move(vals) ) {} @@ -413,7 +413,7 @@ struct ExprNode_Array: { m_values.push_back( ::std::move(val) ); } - + NODE_METHODS(); }; // Tuple @@ -421,11 +421,11 @@ struct ExprNode_Tuple: public ExprNode { ::std::vector< unique_ptr<ExprNode> > m_values; - + ExprNode_Tuple(::std::vector< unique_ptr<ExprNode> > vals): m_values( ::std::move(vals) ) {} - + NODE_METHODS(); }; // Variable / Constant @@ -459,12 +459,12 @@ struct ExprNode_Index: { ::std::unique_ptr<ExprNode> m_obj; ::std::unique_ptr<ExprNode> m_idx; - + ExprNode_Index(::std::unique_ptr<ExprNode> obj, ::std::unique_ptr<ExprNode> idx): m_obj( ::std::move(obj) ), m_idx( ::std::move(idx) ) {} - + NODE_METHODS(); }; @@ -473,12 +473,12 @@ struct ExprNode_Deref: public ExprNode { ::std::unique_ptr<ExprNode> m_value; - + ExprNode_Deref(::std::unique_ptr<ExprNode> value): m_value( ::std::move(value) ) { } - + NODE_METHODS(); }; @@ -523,7 +523,7 @@ struct ExprNode_BinOp: CMPLTE, CMPGT, CMPGTE, - + RANGE, RANGE_INC, BOOLAND, @@ -535,13 +535,13 @@ struct ExprNode_BinOp: SHL, SHR, - + MULTIPLY, DIVIDE, MODULO, ADD, SUB, - + PLACE_IN, // `in PLACE { expr }` or `PLACE <- expr` }; @@ -555,7 +555,7 @@ struct ExprNode_BinOp: m_right( ::std::move(right) ) { } - + NODE_METHODS(); }; @@ -570,7 +570,7 @@ struct ExprNode_UniOp: NEGATE, // '-<expr>' QMARK, // '<expr>?' }; - + enum Type m_type; ::std::unique_ptr<ExprNode> m_value; @@ -579,7 +579,7 @@ struct ExprNode_UniOp: m_value( ::std::move(value) ) { } - + NODE_METHODS(); }; @@ -593,7 +593,7 @@ public: cnode->visit(*this); } virtual bool is_const() const { return false; } - + #define NT(nt) \ virtual void visit(nt& node) = 0/*; \ virtual void visit(const nt& node) = 0*/ @@ -609,7 +609,7 @@ public: NT(ExprNode_Match); NT(ExprNode_If); NT(ExprNode_IfLet); - + NT(ExprNode_Integer); NT(ExprNode_Float); NT(ExprNode_Bool); @@ -620,7 +620,7 @@ public: NT(ExprNode_Array); NT(ExprNode_Tuple); NT(ExprNode_NamedValue); - + NT(ExprNode_Field); NT(ExprNode_Index); NT(ExprNode_Deref); @@ -653,7 +653,7 @@ public: NT(ExprNode_Match); NT(ExprNode_If); NT(ExprNode_IfLet); - + NT(ExprNode_Integer); NT(ExprNode_Float); NT(ExprNode_Bool); @@ -664,7 +664,7 @@ public: NT(ExprNode_Array); NT(ExprNode_Tuple); NT(ExprNode_NamedValue); - + NT(ExprNode_Field); NT(ExprNode_Index); NT(ExprNode_Deref); diff --git a/src/ast/expr_ptr.hpp b/src/ast/expr_ptr.hpp index eb60c49f..1d68af15 100644 --- a/src/ast/expr_ptr.hpp +++ b/src/ast/expr_ptr.hpp @@ -24,7 +24,7 @@ public: ::std::shared_ptr<ExprNode> take_node() { assert(m_node.get()); return ::std::move(m_node); } void visit_nodes(NodeVisitor& v); void visit_nodes(NodeVisitor& v) const; - + Expr clone() const; friend ::std::ostream& operator<<(::std::ostream& os, const Expr& pat); diff --git a/src/ast/generics.hpp b/src/ast/generics.hpp index 7f8858a8..2de5aeaa 100644 --- a/src/ast/generics.hpp +++ b/src/ast/generics.hpp @@ -26,12 +26,12 @@ public: assert(m_default.is_wildcard()); m_default = ::std::move(type); } - + const ::std::string& name() const { return m_name; } - + const TypeRef& get_default() const { return m_default; } TypeRef& get_default() { return m_default; } - + friend ::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp); }; @@ -69,13 +69,13 @@ TAGGED_UNION_EX( GenericBound, (), Lifetime, TypeRef replacement; }) ), - + (, span(x.span) ), ( span = x.span; ), ( public: - + Span span; - + GenericBound clone() const { TU_MATCH(GenericBound, ( (*this) ), (ent), (Lifetime, return make_Lifetime({ent.test, ent.bound}); ), @@ -102,7 +102,7 @@ public: GenericParams(GenericParams&& x) = default; GenericParams& operator=(GenericParams&& x) = default; GenericParams(const GenericParams& x) = delete; - + GenericParams clone() const { GenericParams rv; rv.m_type_params = ::std::vector<TypeParam>( m_type_params ); // Copy-constructable @@ -112,23 +112,23 @@ public: rv.m_bounds.push_back( e.clone() ); return rv; } - + const ::std::vector<TypeParam>& ty_params() const { return m_type_params; } ::std::vector<TypeParam>& ty_params() { return m_type_params; } const ::std::vector< ::std::string>& lft_params() const { return m_lifetime_params; } const ::std::vector<GenericBound>& bounds() const { return m_bounds; } ::std::vector<GenericBound>& bounds() { return m_bounds; } - + void add_ty_param(TypeParam param) { m_type_params.push_back( ::std::move(param) ); } void add_lft_param(::std::string name) { m_lifetime_params.push_back( ::std::move(name) ); } void add_bound(GenericBound bound) { m_bounds.push_back( ::std::move(bound) ); } - + int find_name(const char* name) const; bool check_params(Crate& crate, const ::std::vector<TypeRef>& types) const; bool check_params(Crate& crate, ::std::vector<TypeRef>& types, bool allow_infer) const; - + friend ::std::ostream& operator<<(::std::ostream& os, const GenericParams& tp); }; diff --git a/src/ast/item.hpp b/src/ast/item.hpp index 2d3d1a1b..cc88f3e2 100644 --- a/src/ast/item.hpp +++ b/src/ast/item.hpp @@ -12,7 +12,7 @@ struct NamedNS ::std::string name; T data; bool is_pub; - + NamedNS(): is_pub(false) {} @@ -25,7 +25,7 @@ struct NamedNS is_pub( is_pub ) { } - + //friend ::std::ostream& operator<<(::std::ostream& os, const Named& i) { // return os << (i.is_pub ? "pub " : " ") << i.name << ": " << i.data; //} diff --git a/src/ast/macro.hpp b/src/ast/macro.hpp index 9ae58fb9..84baa5a3 100644 --- a/src/ast/macro.hpp +++ b/src/ast/macro.hpp @@ -11,7 +11,7 @@ namespace AST { class MacroInvocation { Span m_span; - + ::std::string m_macro_name; ::std::string m_ident; TokenTree m_input; @@ -20,11 +20,11 @@ public: MacroInvocation& operator=(MacroInvocation&&) = default; MacroInvocation(const MacroInvocation&) = delete; MacroInvocation& operator=(const MacroInvocation&) = delete; - + MacroInvocation() { } - + MacroInvocation(Span span, ::std::string macro, ::std::string ident, TokenTree input): m_span( mv$(span) ), m_macro_name( mv$(macro) ), @@ -32,7 +32,7 @@ public: m_input( mv$(input) ) { } - + MacroInvocation clone() const; void clear() { diff --git a/src/ast/path.cpp b/src/ast/path.cpp index ac023c8f..f02f48fb 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -27,7 +27,7 @@ namespace AST { (TypeAlias, os << "TypeAlias";), (StructMethod, os << "StructMethod"; ), (TraitMethod, os << "TraitMethod"; ), - + (TypeParameter, os << "TyParam(" << i.level << " # " << i.idx << ")"; ), (Variable, os << "Var(" << i.slot << ")"; ) ) @@ -49,7 +49,7 @@ PathBinding PathBinding::clone() const (EnumVar , return PathBinding::make_EnumVar(e); ), (StructMethod, return PathBinding::make_StructMethod(e); ), (TraitMethod, return PathBinding::make_TraitMethod(e); ), - + (TypeParameter, return PathBinding::make_TypeParameter(e); ), (Variable, return PathBinding::make_Variable(e); ) ) @@ -84,7 +84,7 @@ PathParams::PathParams(const PathParams& x): m_types.reserve( x.m_types.size() ); for(const auto& t : x.m_types) m_types.push_back(t.clone()); - + m_assoc.reserve( x.m_assoc.size() ); for(const auto& t : x.m_assoc) m_assoc.push_back( ::std::make_pair(t.first, t.second.clone()) ); @@ -180,7 +180,7 @@ AST::Path::Path(const Path& x): m_class = Class::make_UFCS({ box$(ent.type->clone()), nullptr, ent.nodes }); ) ) - + memcpy(&m_binding, &x.m_binding, sizeof(PathBinding)); } @@ -199,7 +199,7 @@ void Path::bind_enum_var(const Enum& ent, const ::std::string& name, const ::std } if( idx == ent.variants().size() ) throw ParseError::Generic("Enum variant not found"); - + DEBUG("Bound to enum variant '" << name << "' (#" << idx << ")"); m_binding = PathBinding::make_EnumVar({ &ent, idx }); } @@ -216,10 +216,10 @@ Path& Path::operator+=(const Path& other) Ordering Path::ord(const Path& x) const { Ordering rv; - + rv = ::ord( (unsigned)m_class.tag(), (unsigned)x.m_class.tag() ); if( rv != OrdEqual ) return rv; - + TU_MATCH(Path::Class, (m_class, x.m_class), (ent, x_ent), (Invalid, return OrdEqual; @@ -249,7 +249,7 @@ Ordering Path::ord(const Path& x) const return ::ord(ent.nodes, x_ent.nodes); ) ) - + return OrdEqual; } diff --git a/src/ast/path.hpp b/src/ast/path.hpp index b45767a2..7a67b93e 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -112,7 +112,7 @@ struct PathParams ::std::vector< ::std::string > m_lifetimes; ::std::vector< TypeRef > m_types; ::std::vector< ::std::pair< ::std::string, TypeRef> > m_assoc; - + PathParams(PathParams&& x) = default; PathParams(const PathParams& x); PathParams() {} @@ -121,16 +121,16 @@ struct PathParams m_types(mv$(tys)), m_assoc(mv$(a)) {} - + PathParams& operator=(PathParams&& x) = default; PathParams& operator=(const PathParams& x) = delete; - + bool is_empty() const { return m_lifetimes.empty() && m_types.empty() && m_assoc.empty(); } - + Ordering ord(const PathParams& x) const; - + friend ::std::ostream& operator<<(::std::ostream& os, const PathParams& x); }; @@ -142,13 +142,13 @@ public: PathNode() {} PathNode(::std::string name, PathParams args = {}); const ::std::string& name() const { return m_name; } - + const ::AST::PathParams& args() const { return m_params; } ::AST::PathParams& args() { return m_params; } - + Ordering ord(const PathNode& x) const; void print_pretty(::std::ostream& os, bool is_type_context) const; - + bool operator==(const PathNode& x) const { return ord(x) == OrdEqual; } friend ::std::ostream& operator<<(::std::ostream& os, const PathNode& pn); }; @@ -201,20 +201,20 @@ public: //DEBUG("Path, " << x); return *this; } - + Path(const Path& x); Path& operator=(const AST::Path&) = delete; - + // ABSOLUTE Path(::std::string crate, ::std::vector<PathNode> nodes): m_class( Class::make_Absolute({ mv$(crate), mv$(nodes)}) ) {} - + // UFCS struct TagUfcs {}; Path(TagUfcs, TypeRef type, ::std::vector<PathNode> nodes={}); Path(TagUfcs, TypeRef type, Path trait, ::std::vector<PathNode> nodes={}); - + // VARIABLE struct TagLocal {}; Path(TagLocal, ::std::string name): @@ -223,7 +223,7 @@ public: Path(::std::string name): m_class( Class::make_Local({ mv$(name) }) ) {} - + // RELATIVE struct TagRelative {}; Path(TagRelative, Ident::Hygiene hygiene, ::std::vector<PathNode> nodes): @@ -239,7 +239,7 @@ public: Path(TagSuper, unsigned int count, ::std::vector<PathNode> nodes): m_class( Class::make_Super({ count, mv$(nodes) }) ) {} - + //void set_crate(::std::string crate) { // if( m_crate == "" ) { // m_crate = crate; @@ -247,11 +247,11 @@ public: // } //} - + Class::Tag class_tag() const { return m_class.tag(); } - + Path operator+(PathNode pn) const { Path tmp = Path(*this); tmp.nodes().push_back( mv$(pn) ); @@ -274,7 +274,7 @@ public: nodes().push_back( mv$(node) ); m_binding = PathBinding(); } - + bool is_trivial() const { TU_MATCH_DEF(Class, (m_class), (e), ( @@ -288,11 +288,11 @@ public: ) ) } - + bool is_valid() const { return !m_class.is_Invalid(); } bool is_absolute() const { return m_class.is_Absolute(); } bool is_relative() const { return m_class.is_Relative() || m_class.is_Super() || m_class.is_Self(); } - + size_t size() const { TU_MATCH(Class, (m_class), (ent), (Invalid, assert(!m_class.is_Invalid()); throw ::std::runtime_error("Path::nodes() on Invalid"); ), @@ -308,11 +308,11 @@ public: //const ::std::string& crate() const { return m_crate; } bool is_concrete() const; - + bool is_bound() const { return !m_binding.is_Unbound(); } const PathBinding& binding() const { return m_binding; } void bind_variable(unsigned int slot); - + ::std::vector<PathNode>& nodes() { TU_MATCH(Class, (m_class), (ent), (Invalid, assert(!m_class.is_Invalid()); throw ::std::runtime_error("Path::nodes() on Invalid"); ), @@ -328,20 +328,20 @@ public: const ::std::vector<PathNode>& nodes() const { return ((Path*)this)->nodes(); } - + PathNode& operator[](int idx) { if(idx>=0) return nodes()[idx]; else return nodes()[size()+idx]; } const PathNode& operator[](int idx) const { return (*(Path*)this)[idx]; } - + Ordering ord(const Path& x) const; bool operator==(const Path& x) const { return ord(x) == OrdEqual; } bool operator!=(const Path& x) const { return ord(x) != OrdEqual; } bool operator<(const Path& x) const { return ord(x) != OrdLess; } - + void print_pretty(::std::ostream& os, bool is_type_context) const; friend ::std::ostream& operator<<(::std::ostream& os, const Path& path); private: static void resolve_args_nl(::std::vector<PathNode>& nodes, ::std::function<TypeRef(const char*)> fcn); - + void check_param_counts(const GenericParams& params, bool expect_params, PathNode& node); public: void bind_enum_var(const Enum& ent, const ::std::string& name, const ::std::vector<TypeRef>& args={}); @@ -349,7 +349,7 @@ public: (void)args; m_binding = PathBinding::make_Function({&ent}); } - + void bind(::AST::PathBinding pb) { m_binding = mv$(pb); } diff --git a/src/ast/pattern.cpp b/src/ast/pattern.cpp index 3128fd91..bfcc6eeb 100644 --- a/src/ast/pattern.cpp +++ b/src/ast/pattern.cpp @@ -123,7 +123,7 @@ namespace AST { os << ent.leading; needs_comma = true; } - + if( needs_comma ) { os << ", "; } @@ -131,7 +131,7 @@ namespace AST { os << ent.extra_bind.m_name; os << ".."; needs_comma = true; - + if(ent.trailing.size()) { if( needs_comma ) { os << ", "; @@ -167,7 +167,7 @@ AST::Pattern AST::Pattern::clone() const AST::Pattern rv; rv.m_span = m_span; rv.m_binding = PatternBinding(m_binding); - + struct H { static ::std::unique_ptr<Pattern> clone_sp(const ::std::unique_ptr<Pattern>& p) { return ::std::make_unique<Pattern>( p->clone() ); @@ -198,7 +198,7 @@ AST::Pattern AST::Pattern::clone() const throw ""; } }; - + TU_MATCH(Pattern::Data, (m_data), (e), (Any, rv.m_data = Data::make_Any(e); @@ -237,7 +237,7 @@ AST::Pattern AST::Pattern::clone() const rv.m_data = Data::make_SplitSlice({ H::clone_list(e.leading), e.extra_bind, H::clone_list(e.trailing) }); ) ) - + return rv; } diff --git a/src/ast/pattern.hpp b/src/ast/pattern.hpp index a2df7b59..af6a5adf 100644 --- a/src/ast/pattern.hpp +++ b/src/ast/pattern.hpp @@ -46,11 +46,11 @@ public: m_mutable(ismut), m_slot( ~0u ) {} - + PatternBinding(PatternBinding&& x) = default; PatternBinding(const PatternBinding& x) = default; PatternBinding& operator=(PatternBinding&& x) = default; - + bool is_valid() const { return m_name.name != ""; } }; @@ -78,7 +78,7 @@ public: bool has_wildcard; ::std::vector<Pattern> end; }; - + TAGGED_UNION(Data, Any, (MaybeBind, struct { Ident name; } ), (Macro, struct { unique_ptr<::AST::MacroInvocation> inv; } ), @@ -96,15 +96,15 @@ private: Span m_span; PatternBinding m_binding; Data m_data; - + public: virtual ~Pattern(); - + Pattern() {} Pattern(Pattern&&) = default; Pattern& operator=(Pattern&&) = default; - + Pattern(Data dat): m_binding(), m_data( mv$(dat) ) @@ -135,8 +135,8 @@ public: Pattern(TagValue, Value val, Value end = Value()): m_data( Data::make_Value({ ::std::move(val), ::std::move(end) }) ) {} - - + + struct TagReference {}; Pattern(TagReference, bool is_mutable, Pattern sub_pattern): m_data( Data::make_Ref( /*Data::Data_Ref */ { @@ -163,20 +163,20 @@ public: struct TagStruct {}; Pattern(TagStruct, Path path, ::std::vector< ::std::pair< ::std::string,Pattern> > sub_patterns, bool is_exhaustive): - m_data( Data::make_Struct( { ::std::move(path), ::std::move(sub_patterns), is_exhaustive } ) ) + m_data( Data::make_Struct( { ::std::move(path), ::std::move(sub_patterns), is_exhaustive } ) ) {} - + // Mutators void set_bind(Ident name, PatternBinding::Type type, bool is_mut) { m_binding = PatternBinding(mv$(name), type, is_mut); } - - + + const Span& span() const { return m_span; } void set_span(Span sp) { m_span = mv$(sp); } - + Pattern clone() const; - + // Accessors PatternBinding& binding() { return m_binding; } const PatternBinding& binding() const { return m_binding; } diff --git a/src/ast/types.cpp b/src/ast/types.cpp index c8f373e5..21e47b4b 100644 --- a/src/ast/types.cpp +++ b/src/ast/types.cpp @@ -1,7 +1,7 @@ /* * MRustC - Mutabah's Rust Compiler * - By John Hodge (Mutabah/thePowersGang) - * + * * types.cpp * - Backing code for the TypeRef class * @@ -87,7 +87,7 @@ Type_Function::Type_Function(const Type_Function& other): Ordering Type_Function::ord(const Type_Function& x) const { Ordering rv; - + rv = ::ord(m_abi, x.m_abi); if(rv != OrdEqual) return rv; rv = ::ord(m_arg_types, x.m_arg_types); @@ -139,10 +139,10 @@ TypeRef TypeRef::clone() const Ordering TypeRef::ord(const TypeRef& x) const { Ordering rv; - + rv = ::ord( (unsigned)m_data.tag(), (unsigned)x.m_data.tag() ); if(rv != OrdEqual) return rv; - + TU_MATCH(TypeData, (m_data, x.m_data), (ent, x_ent), (None, return OrdEqual;), (Macro, throw CompileError::BugCheck("TypeRef::ord - unexpanded macro");), diff --git a/src/ast/types.hpp b/src/ast/types.hpp index 04c1517f..e2114bae 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -1,276 +1,276 @@ -#ifndef TYPES_HPP_INCLUDED
-#define TYPES_HPP_INCLUDED
-
-#include <memory>
-
-#include "common.hpp"
-#include "coretypes.hpp"
-#include "ast/path.hpp"
-#include "ast/macro.hpp"
-#include <serialise.hpp>
-#include <tagged_union.hpp>
-
-namespace AST {
-class ExprNode;
-class Expr;
-}
-
-class PrettyPrintType
-{
- const TypeRef& m_type;
-public:
- PrettyPrintType(const TypeRef& ty):
- m_type(ty)
- {}
-
- void print(::std::ostream& os) const;
-
- friend ::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& v);
-};
-
-struct TypeArgRef
-{
- ::std::string name;
- unsigned int level;
- const AST::GenericParams* params;
-};
-
-struct Type_Function
-{
- bool is_unsafe;
- ::std::string m_abi;
- ::std::unique_ptr<TypeRef> m_rettype;
- ::std::vector<TypeRef> m_arg_types;
- bool is_variadic;
-
- Type_Function() {}
- Type_Function(bool is_unsafe, ::std::string abi, ::std::unique_ptr<TypeRef> ret, ::std::vector<TypeRef> args, bool is_variadic):
- is_unsafe(is_unsafe),
- m_abi(mv$(abi)),
- m_rettype(mv$(ret)),
- m_arg_types(mv$(args)),
- is_variadic(is_variadic)
- {}
- Type_Function(Type_Function&& other) = default;
- Type_Function(const Type_Function& other);
-
- Ordering ord(const Type_Function& x) const;
-};
-
-TAGGED_UNION(TypeData, None,
- (None, struct { }),
- (Any, struct { }),
- (Bang, struct { }),
- (Unit, struct { }),
- (Macro, struct {
- ::AST::MacroInvocation inv;
- }),
- (Primitive, struct {
- enum eCoreType core_type;
- }),
- (Function, struct {
- Type_Function info;
- }),
- (Tuple, struct {
- ::std::vector<TypeRef> inner_types;
- }),
- (Borrow, struct {
- bool is_mut;
- ::std::unique_ptr<TypeRef> inner;
- }),
- (Pointer, struct {
- bool is_mut;
- ::std::unique_ptr<TypeRef> inner;
- }),
- (Array, struct {
- ::std::unique_ptr<TypeRef> inner;
- ::std::shared_ptr<AST::ExprNode> size;
- }),
- (Generic, struct {
- ::std::string name;
- unsigned int index;
- }),
- (Path, struct {
- AST::Path path;
- }),
- (TraitObject, struct {
- ::std::vector<::std::string> hrls;
- ::std::vector<AST::Path> traits;
- }),
- (ErasedType, struct {
- ::std::vector<::std::string> hrls;
- ::std::vector<AST::Path> traits;
- })
- );
-
-/// A type
-class TypeRef
-{
- Span m_span;
-public:
- TypeData m_data;
-
- ~TypeRef();
-
- TypeRef(TypeRef&& other) = default;
- TypeRef& operator=(TypeRef&& other) = default;
-
- #if 1
- TypeRef(const TypeRef& other) = delete;
- TypeRef& operator=(const TypeRef& other) = delete;
- #else
- TypeRef(const TypeRef& other): m_span(other.m_span) {
- *this = other.clone();
- }
- TypeRef& operator=(const TypeRef& other) {
- m_data = mv$(other.clone().m_data);
- return *this;
- }
- #endif
-
- TypeRef(Span sp):
- m_span( mv$(sp) ),
- m_data( TypeData::make_Any({}) )
- {}
- TypeRef(Span sp, TypeData data):
- m_span( mv$(sp) ),
- m_data( mv$(data) )
- {}
-
- struct TagInvalid {};
- TypeRef(TagInvalid, Span sp):
- m_span(mv$(sp)),
- m_data(TypeData::make_None({}))
- {}
-
- struct TagMacro {};
- TypeRef(TagMacro, ::AST::MacroInvocation inv):
- m_span(inv.span()),
- m_data(TypeData::make_Macro({mv$(inv)}))
- {}
-
- struct TagUnit {}; // unit maps to a zero-length tuple, just easier to type
- TypeRef(TagUnit, Span sp):
- m_span(mv$(sp)),
- m_data(TypeData::make_Unit({}))
- {}
-
- struct TagPrimitive {};
- TypeRef(TagPrimitive, Span sp, enum eCoreType type):
- m_span(mv$(sp)),
- m_data(TypeData::make_Primitive({type}))
- {}
- TypeRef(Span sp, enum eCoreType type):
- m_span(mv$(sp)),
- m_data(TypeData::make_Primitive({type}))
- {}
-
- struct TagTuple {};
- TypeRef(TagTuple , Span sp, ::std::vector<TypeRef> inner_types):
- m_span(mv$(sp)),
- m_data(TypeData::make_Tuple({::std::move(inner_types)}))
- {}
- struct TagFunction {};
- TypeRef(TagFunction, Span sp, bool is_unsafe, ::std::string abi, ::std::vector<TypeRef> args, bool is_variadic, TypeRef ret):
- m_span(mv$(sp)),
- m_data(TypeData::make_Function({ Type_Function( is_unsafe, abi, box$(ret), mv$(args), is_variadic ) }))
- {}
-
- struct TagReference {};
- TypeRef(TagReference , Span sp, bool is_mut, TypeRef inner_type):
- m_span(mv$(sp)),
- m_data(TypeData::make_Borrow({ is_mut, ::make_unique_ptr(mv$(inner_type)) }))
- {}
- struct TagPointer {};
- TypeRef(TagPointer , Span sp, bool is_mut, TypeRef inner_type):
- m_span(mv$(sp)),
- m_data(TypeData::make_Pointer({ is_mut, ::make_unique_ptr(mv$(inner_type)) }))
- {}
- struct TagSizedArray {};
- TypeRef(TagSizedArray , Span sp, TypeRef inner_type, ::std::shared_ptr<AST::ExprNode> size):
- m_span(mv$(sp)),
- m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), mv$(size) }))
- {}
- struct TagUnsizedArray {};
- TypeRef(TagUnsizedArray , Span sp, TypeRef inner_type):
- m_span(mv$(sp)),
- m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), ::std::shared_ptr<AST::ExprNode>() }))
- {}
-
- struct TagArg {};
- TypeRef(TagArg, Span sp, ::std::string name, unsigned int binding = ~0u):
- m_span( mv$(sp) ),
- m_data(TypeData::make_Generic({ name, binding }))
- {}
- TypeRef(Span sp, ::std::string name, unsigned int binding = ~0u):
- TypeRef(TagArg(), mv$(sp), mv$(name), binding)
- {}
-
- struct TagPath {};
- TypeRef(TagPath, Span sp, AST::Path path):
- m_span(mv$(sp)),
- m_data(TypeData::make_Path({ ::std::move(path) }))
- {}
- TypeRef(Span sp, AST::Path path):
- TypeRef(TagPath(), mv$(sp), mv$(path))
- {}
-
- TypeRef( Span sp, ::std::vector<::std::string> hrls, ::std::vector<AST::Path> traits ):
- m_span(mv$(sp)),
- m_data(TypeData::make_TraitObject({ mv$(hrls), ::std::move(traits) }))
- {}
-
-
- const Span& span() const { return m_span; }
-
- bool is_valid() const { return ! m_data.is_None(); }
-
- bool is_unbounded() const { return m_data.is_Any(); }
- bool is_wildcard() const { return m_data.is_Any(); }
-
- bool is_unit() const { return m_data.is_Unit(); }
- bool is_primitive() const { return m_data.is_Primitive(); }
-
- bool is_path() const { return m_data.is_Path(); }
- const AST::Path& path() const { return m_data.as_Path().path; }
- AST::Path& path() { return m_data.as_Path().path; }
-
- bool is_type_param() const { return m_data.is_Generic(); }
- const ::std::string& type_param() const { return m_data.as_Generic().name; }
-
- bool is_reference() const { return m_data.is_Borrow(); }
- bool is_pointer() const { return m_data.is_Pointer(); }
- bool is_tuple() const { return m_data.is_Tuple(); }
-
- TypeRef clone() const;
-
- const TypeRef& inner_type() const {
- 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() {
- 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; )
- )
- }
-
- Ordering ord(const TypeRef& x) const;
- bool operator==(const TypeRef& x) const { return ord(x) == OrdEqual; }
- bool operator!=(const TypeRef& x) const { return ord(x) != OrdEqual; }
- bool operator<(const TypeRef& x) const { return ord(x) == OrdLess; };
-
- PrettyPrintType print_pretty() const { return PrettyPrintType(*this); }
-
- friend class PrettyPrintType;
-
- friend ::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr);
-};
-
-#endif // TYPES_HPP_INCLUDED
+#ifndef TYPES_HPP_INCLUDED +#define TYPES_HPP_INCLUDED + +#include <memory> + +#include "common.hpp" +#include "coretypes.hpp" +#include "ast/path.hpp" +#include "ast/macro.hpp" +#include <serialise.hpp> +#include <tagged_union.hpp> + +namespace AST { +class ExprNode; +class Expr; +} + +class PrettyPrintType +{ + const TypeRef& m_type; +public: + PrettyPrintType(const TypeRef& ty): + m_type(ty) + {} + + void print(::std::ostream& os) const; + + friend ::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& v); +}; + +struct TypeArgRef +{ + ::std::string name; + unsigned int level; + const AST::GenericParams* params; +}; + +struct Type_Function +{ + bool is_unsafe; + ::std::string m_abi; + ::std::unique_ptr<TypeRef> m_rettype; + ::std::vector<TypeRef> m_arg_types; + bool is_variadic; + + Type_Function() {} + Type_Function(bool is_unsafe, ::std::string abi, ::std::unique_ptr<TypeRef> ret, ::std::vector<TypeRef> args, bool is_variadic): + is_unsafe(is_unsafe), + m_abi(mv$(abi)), + m_rettype(mv$(ret)), + m_arg_types(mv$(args)), + is_variadic(is_variadic) + {} + Type_Function(Type_Function&& other) = default; + Type_Function(const Type_Function& other); + + Ordering ord(const Type_Function& x) const; +}; + +TAGGED_UNION(TypeData, None, + (None, struct { }), + (Any, struct { }), + (Bang, struct { }), + (Unit, struct { }), + (Macro, struct { + ::AST::MacroInvocation inv; + }), + (Primitive, struct { + enum eCoreType core_type; + }), + (Function, struct { + Type_Function info; + }), + (Tuple, struct { + ::std::vector<TypeRef> inner_types; + }), + (Borrow, struct { + bool is_mut; + ::std::unique_ptr<TypeRef> inner; + }), + (Pointer, struct { + bool is_mut; + ::std::unique_ptr<TypeRef> inner; + }), + (Array, struct { + ::std::unique_ptr<TypeRef> inner; + ::std::shared_ptr<AST::ExprNode> size; + }), + (Generic, struct { + ::std::string name; + unsigned int index; + }), + (Path, struct { + AST::Path path; + }), + (TraitObject, struct { + ::std::vector<::std::string> hrls; + ::std::vector<AST::Path> traits; + }), + (ErasedType, struct { + ::std::vector<::std::string> hrls; + ::std::vector<AST::Path> traits; + }) + ); + +/// A type +class TypeRef +{ + Span m_span; +public: + TypeData m_data; + + ~TypeRef(); + + TypeRef(TypeRef&& other) = default; + TypeRef& operator=(TypeRef&& other) = default; + + #if 1 + TypeRef(const TypeRef& other) = delete; + TypeRef& operator=(const TypeRef& other) = delete; + #else + TypeRef(const TypeRef& other): m_span(other.m_span) { + *this = other.clone(); + } + TypeRef& operator=(const TypeRef& other) { + m_data = mv$(other.clone().m_data); + return *this; + } + #endif + + TypeRef(Span sp): + m_span( mv$(sp) ), + m_data( TypeData::make_Any({}) ) + {} + TypeRef(Span sp, TypeData data): + m_span( mv$(sp) ), + m_data( mv$(data) ) + {} + + struct TagInvalid {}; + TypeRef(TagInvalid, Span sp): + m_span(mv$(sp)), + m_data(TypeData::make_None({})) + {} + + struct TagMacro {}; + TypeRef(TagMacro, ::AST::MacroInvocation inv): + m_span(inv.span()), + m_data(TypeData::make_Macro({mv$(inv)})) + {} + + struct TagUnit {}; // unit maps to a zero-length tuple, just easier to type + TypeRef(TagUnit, Span sp): + m_span(mv$(sp)), + m_data(TypeData::make_Unit({})) + {} + + struct TagPrimitive {}; + TypeRef(TagPrimitive, Span sp, enum eCoreType type): + m_span(mv$(sp)), + m_data(TypeData::make_Primitive({type})) + {} + TypeRef(Span sp, enum eCoreType type): + m_span(mv$(sp)), + m_data(TypeData::make_Primitive({type})) + {} + + struct TagTuple {}; + TypeRef(TagTuple , Span sp, ::std::vector<TypeRef> inner_types): + m_span(mv$(sp)), + m_data(TypeData::make_Tuple({::std::move(inner_types)})) + {} + struct TagFunction {}; + TypeRef(TagFunction, Span sp, bool is_unsafe, ::std::string abi, ::std::vector<TypeRef> args, bool is_variadic, TypeRef ret): + m_span(mv$(sp)), + m_data(TypeData::make_Function({ Type_Function( is_unsafe, abi, box$(ret), mv$(args), is_variadic ) })) + {} + + struct TagReference {}; + TypeRef(TagReference , Span sp, bool is_mut, TypeRef inner_type): + m_span(mv$(sp)), + m_data(TypeData::make_Borrow({ is_mut, ::make_unique_ptr(mv$(inner_type)) })) + {} + struct TagPointer {}; + TypeRef(TagPointer , Span sp, bool is_mut, TypeRef inner_type): + m_span(mv$(sp)), + m_data(TypeData::make_Pointer({ is_mut, ::make_unique_ptr(mv$(inner_type)) })) + {} + struct TagSizedArray {}; + TypeRef(TagSizedArray , Span sp, TypeRef inner_type, ::std::shared_ptr<AST::ExprNode> size): + m_span(mv$(sp)), + m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), mv$(size) })) + {} + struct TagUnsizedArray {}; + TypeRef(TagUnsizedArray , Span sp, TypeRef inner_type): + m_span(mv$(sp)), + m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), ::std::shared_ptr<AST::ExprNode>() })) + {} + + struct TagArg {}; + TypeRef(TagArg, Span sp, ::std::string name, unsigned int binding = ~0u): + m_span( mv$(sp) ), + m_data(TypeData::make_Generic({ name, binding })) + {} + TypeRef(Span sp, ::std::string name, unsigned int binding = ~0u): + TypeRef(TagArg(), mv$(sp), mv$(name), binding) + {} + + struct TagPath {}; + TypeRef(TagPath, Span sp, AST::Path path): + m_span(mv$(sp)), + m_data(TypeData::make_Path({ ::std::move(path) })) + {} + TypeRef(Span sp, AST::Path path): + TypeRef(TagPath(), mv$(sp), mv$(path)) + {} + + TypeRef( Span sp, ::std::vector<::std::string> hrls, ::std::vector<AST::Path> traits ): + m_span(mv$(sp)), + m_data(TypeData::make_TraitObject({ mv$(hrls), ::std::move(traits) })) + {} + + + const Span& span() const { return m_span; } + + bool is_valid() const { return ! m_data.is_None(); } + + bool is_unbounded() const { return m_data.is_Any(); } + bool is_wildcard() const { return m_data.is_Any(); } + + bool is_unit() const { return m_data.is_Unit(); } + bool is_primitive() const { return m_data.is_Primitive(); } + + bool is_path() const { return m_data.is_Path(); } + const AST::Path& path() const { return m_data.as_Path().path; } + AST::Path& path() { return m_data.as_Path().path; } + + bool is_type_param() const { return m_data.is_Generic(); } + const ::std::string& type_param() const { return m_data.as_Generic().name; } + + bool is_reference() const { return m_data.is_Borrow(); } + bool is_pointer() const { return m_data.is_Pointer(); } + bool is_tuple() const { return m_data.is_Tuple(); } + + TypeRef clone() const; + + const TypeRef& inner_type() const { + 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() { + 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; ) + ) + } + + Ordering ord(const TypeRef& x) const; + bool operator==(const TypeRef& x) const { return ord(x) == OrdEqual; } + bool operator!=(const TypeRef& x) const { return ord(x) != OrdEqual; } + bool operator<(const TypeRef& x) const { return ord(x) == OrdLess; }; + + PrettyPrintType print_pretty() const { return PrettyPrintType(*this); } + + friend class PrettyPrintType; + + friend ::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr); +}; + +#endif // TYPES_HPP_INCLUDED |