diff options
author | John Hodge <tpg@mutabah.net> | 2016-03-10 11:13:10 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-03-10 11:13:10 +0800 |
commit | b0e44483f1f598d6e3aa5ca8b325560a843f162f (patch) | |
tree | 3371073789c717ce14f3c556f236a9d8aa7f692f | |
parent | ce771e31b4cd157a87e7ec6531d625a895228ebf (diff) | |
download | mrust-b0e44483f1f598d6e3aa5ca8b325560a843f162f.tar.gz |
AST - Clean up MetaItems/MetaItem
-rw-r--r-- | src/ast/ast.cpp | 41 | ||||
-rw-r--r-- | src/ast/attrs.hpp | 57 | ||||
-rw-r--r-- | src/expand/mod.cpp | 4 | ||||
-rw-r--r-- | src/parse/common.hpp | 4 | ||||
-rw-r--r-- | src/parse/expr.cpp | 59 | ||||
-rw-r--r-- | src/parse/root.cpp | 19 | ||||
-rw-r--r-- | src/synexts/derive.cpp | 2 | ||||
-rw-r--r-- | src/types.cpp | 24 | ||||
-rw-r--r-- | src/types.hpp | 24 |
9 files changed, 120 insertions, 114 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 35cab7a2..0bdbf5c0 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -12,6 +12,21 @@ 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::clone() const
+{
+ return MetaItems( clone_mivec(m_items) );
+}
+
void MetaItems::push_back(MetaItem i)
{
m_items.push_back( ::std::move(i) );
@@ -30,14 +45,32 @@ SERIALISE_TYPE_A(MetaItems::, "AST_MetaItems", { s.item(m_items);
})
+
+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");
+}
+
+
SERIALISE_TYPE(MetaItem::, "AST_MetaItem", {
s << m_name;
- s << m_str_val;
- s << m_sub_items;
+ //s << m_str_val;
+ //s << m_sub_items;
},{
s.item(m_name);
- s.item(m_str_val);
- s.item(m_sub_items);
+ //s.item(m_str_val);
+ //s.item(m_sub_items);
})
bool ImplDef::matches(::std::vector<TypeRef>& out_types, const Path& trait, const TypeRef& type) const
diff --git a/src/ast/attrs.hpp b/src/ast/attrs.hpp index ebbad066..b7b7f91c 100644 --- a/src/ast/attrs.hpp +++ b/src/ast/attrs.hpp @@ -14,13 +14,18 @@ public: ::std::vector<MetaItem> m_items; MetaItems() {} + MetaItems(MetaItems&&) = default; + MetaItems& operator=(MetaItems&&) = default; + MetaItems(const MetaItems&) = delete; MetaItems(::std::vector<MetaItem> items): - m_items(items) + m_items( mv$(items) ) { } void push_back(MetaItem i); + MetaItems clone() const; + MetaItem* get(const char *name); bool has(const char *name) { return get(name) != 0; @@ -33,42 +38,66 @@ public: SERIALISABLE_PROTOTYPES(); }; + +TAGGED_UNION(MetaItemData, None, + (None, ()), + (String, ( + ::std::string val; + )), + (List, ( + ::std::vector<MetaItem> sub_items; + )) + ); + class MetaItem: public Serialisable { ::std::string m_name; - MetaItems m_sub_items; - ::std::string m_str_val; + MetaItemData m_data; public: MetaItem() {} MetaItem(::std::string name): - m_name(name) + m_name(name), + m_data( MetaItemData::make_None({}) ) { } MetaItem(::std::string name, ::std::string str_val): m_name(name), - m_str_val(str_val) + m_data( MetaItemData::make_String({mv$(str_val)}) ) { } MetaItem(::std::string name, ::std::vector<MetaItem> items): m_name(name), - m_sub_items(items) + m_data( MetaItemData::make_List({mv$(items)}) ) { } + MetaItem clone() const; + void mark_used() {} const ::std::string& name() const { return m_name; } - const ::std::string& string() const { return m_str_val; } - bool has_sub_items() const { return m_sub_items.m_items.size() > 0; } - const MetaItems& items() const { return m_sub_items; } - MetaItems& items() { return m_sub_items; } + + 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; - if(x.m_sub_items.m_items.size()) - os << "(" << x.m_sub_items.m_items << ")"; - else - os << "=\"" << x.m_str_val << "\""; + TU_MATCH(MetaItemData, (x.m_data), (e), + (None, + ), + (String, + os << "=\"" << e.val << "\""; + ), + (List, + os << "(" << e.sub_items << ")"; + ) + ) return os; } diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 8eb6dd87..78225a2f 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -47,8 +47,8 @@ void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::std::functi for( auto& a : attrs.m_items ) { if( a.name() == "cfg_attr" ) { - if( check_cfg(a.items().m_items.at(0)) ) { - Expand_Attr(a.items().m_items.at(1), stage, f); + if( check_cfg(a.items().at(0)) ) { + Expand_Attr(a.items().at(1), stage, f); } } else { diff --git a/src/parse/common.hpp b/src/parse/common.hpp index 837bd104..552f8c4c 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -55,8 +55,8 @@ extern void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std extern void Parse_ModRoot_Items(TokenStream& lex, AST::Module& mod, bool file_controls_dir, const ::std::string& path);
-extern AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self, bool can_be_prototype);
-extern AST::Function Parse_FunctionDefWithCode(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self);
+extern AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaItems& attrs, bool allow_self, bool can_be_prototype);
+extern AST::Function Parse_FunctionDefWithCode(TokenStream& lex, ::std::string abi, AST::MetaItems& attrs, bool allow_self);
extern AST::Expr Parse_Expr(TokenStream& lex, bool const_only);
extern AST::Expr Parse_ExprBlock(TokenStream& lex);
extern AST::ExprNodeP Parse_Expr0(TokenStream& lex);
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index b1cc25ce..b07567a7 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -44,7 +44,6 @@ AST::Expr Parse_ExprBlock(TokenStream& lex) ExprNodeP Parse_ExprBlockNode(TokenStream& lex);
ExprNodeP Parse_ExprBlockLine(TokenStream& lex, bool *expect_end);
-void Parse_ExternBlock(TokenStream& lex, AST::MetaItems attrs, ::std::vector< AST::Named<AST::Function> >& imports);
ExprNodeP Parse_ExprBlockNode(TokenStream& lex)
{
@@ -230,64 +229,6 @@ ExprNodeP Parse_ExprBlockLine(TokenStream& lex, bool *expect_end) }
}
}
-/// Extern block within a block
-void Parse_ExternBlock(TokenStream& lex, AST::MetaItems attrs, ::std::vector< AST::Named<AST::Function> >& imports)
-{
- Token tok;
-
- // - default ABI is "C"
- ::std::string abi = "C";
- if( GET_TOK(tok, lex) == TOK_STRING ) {
- abi = tok.str();
- }
- else {
- lex.putback(tok);
- }
-
- bool is_block = false;
- if( GET_TOK(tok, lex) == TOK_BRACE_OPEN ) {
- is_block = true;
- }
- else
- lex.putback(tok);
-
- do {
- AST::MetaItems inner_attrs;
- if( is_block )
- {
- while( GET_TOK(tok, lex) == TOK_ATTR_OPEN )
- {
- inner_attrs.push_back( Parse_MetaItem(lex) );
- GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
- }
- lex.putback(tok);
- }
- else
- {
- inner_attrs = attrs;
- }
- ::std::string name;
- switch( GET_TOK(tok, lex) )
- {
- case TOK_RWORD_FN:
- GET_CHECK_TOK(tok, lex, TOK_IDENT);
- name = tok.str();
- imports.push_back( AST::Named<AST::Function>(
- ::std::move(name),
- Parse_FunctionDef(lex, abi, AST::MetaItems(), false, true),
- false
- ) );
- GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
- break;
- default:
- throw ParseError::Unexpected(lex, tok);
- }
- } while( is_block && LOOK_AHEAD(lex) != TOK_BRACE_CLOSE );
- if( is_block )
- GET_CHECK_TOK(tok, lex, TOK_BRACE_CLOSE);
- else
- GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
-}
/// While loop (either as a statement, or as part of an expression)
ExprNodeP Parse_WhileStmt(TokenStream& lex, ::std::string lifetime)
{
diff --git a/src/parse/root.cpp b/src/parse/root.cpp index d5b8d561..a5f9da81 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -214,7 +214,7 @@ void Parse_WhereClause(TokenStream& lex, AST::GenericParams& params) }
/// Parse a function definition (after the 'fn <name>')
-AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self, bool can_be_prototype)
+AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaItems& attrs, bool allow_self, bool can_be_prototype)
{
TRACE_FUNCTION;
@@ -379,10 +379,10 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaIt return AST::Function(::std::move(params), ::std::move(ret_type), ::std::move(args));
}
-AST::Function Parse_FunctionDefWithCode(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self)
+AST::Function Parse_FunctionDefWithCode(TokenStream& lex, ::std::string abi, AST::MetaItems& attrs, bool allow_self)
{
Token tok;
- auto ret = Parse_FunctionDef(lex, abi, ::std::move(attrs), allow_self, false);
+ auto ret = Parse_FunctionDef(lex, abi, attrs, allow_self, false);
GET_CHECK_TOK(tok, lex, TOK_BRACE_OPEN);
lex.putback(tok);
ret.set_code( Parse_ExprBlock(lex) );
@@ -815,7 +815,7 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) items.push_back(Parse_MetaItem(lex));
} while(GET_TOK(tok, lex) == TOK_COMMA);
CHECK_TOK(tok, TOK_PAREN_CLOSE);
- return AST::MetaItem(name, items); }
+ return AST::MetaItem(name, mv$(items)); }
default:
lex.putback(tok);
return AST::MetaItem(name);
@@ -1009,7 +1009,8 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) GET_CHECK_TOK(tok, lex, TOK_IDENT);
::std::string name = tok.str();
// - Self allowed, can't be prototype-form
- impl.add_function(is_public, ::std::move(name), Parse_FunctionDefWithCode(lex, abi, ::std::move(item_attrs), true));
+ auto fcn = Parse_FunctionDefWithCode(lex, abi, item_attrs, true);
+ impl.add_function(is_public, ::std::move(name), mv$(fcn));
break; }
default:
@@ -1265,9 +1266,9 @@ void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std::strin {
// `use ...`
case TOK_RWORD_USE:
- Parse_Use(lex, [&mod,is_public,&file_path,meta_items](AST::Path p, std::string s) {
+ Parse_Use(lex, [&mod,is_public,&file_path,&meta_items](AST::Path p, std::string s) {
DEBUG(file_path << " - use " << p << " as '" << s << "'");
- mod.add_alias(is_public, mv$(p), s, meta_items);
+ mod.add_alias(is_public, mv$(p), s, meta_items.clone());
});
GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
break;
@@ -1410,7 +1411,7 @@ void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std::strin ::std::string name = tok.str();
// TODO: Mark as unsafe
auto i = Parse_TraitDef(lex, meta_items);
- mod.add_trait(is_public, name, mv$(i), meta_items);
+ mod.add_trait(is_public, name, mv$(i), mv$(meta_items));
break; }
// `unsafe impl`
case TOK_RWORD_IMPL:
@@ -1624,7 +1625,7 @@ void Parse_ModRoot(TokenStream& lex, AST::Module& mod, AST::MetaItems& mod_attrs AST::MetaItem item = Parse_MetaItem(lex);
GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
- mod_attrs.push_back( item );
+ mod_attrs.push_back( mv$(item) );
}
lex.putback(tok);
diff --git a/src/synexts/derive.cpp b/src/synexts/derive.cpp index ca0a2c3f..5849e280 100644 --- a/src/synexts/derive.cpp +++ b/src/synexts/derive.cpp @@ -126,7 +126,7 @@ static void derive_item(AST::Module& mod, const AST::MetaItem& attr, const AST:: for( const auto& param : params.ty_params() ) type.path().nodes().back().args().push_back( TypeRef(TypeRef::TagArg(), param.name()) ); - for( const auto& trait : attr.items().m_items ) + for( const auto& trait : attr.items() ) { DEBUG("- " << trait.name()); auto dp = find_impl(trait.name()); diff --git a/src/types.cpp b/src/types.cpp index 33bf38f8..b056bf94 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -95,6 +95,30 @@ Ordering Type_Function::ord(const Type_Function& x) const return (*m_rettype).ord( *x.m_rettype ); } +TypeRef::TypeRef(const TypeRef& other) +{ + switch( other.m_data.tag() ) + { + #define _COPY(VAR) case TypeData::TAG_##VAR: m_data = TypeData::make_##VAR(other.m_data.as_##VAR()); break; + #define _CLONE(VAR, code...) case TypeData::TAG_##VAR: { auto& old = other.m_data.as_##VAR(); m_data = TypeData::make_##VAR(code); } break; + _COPY(None) + _COPY(Any) + case TypeData::TAG_Macro: throw ::std::runtime_error("Copying an unexpanded type macro"); + _COPY(Unit) + _COPY(Primitive) + _COPY(Function) + _COPY(Tuple) + _CLONE(Borrow, { old.is_mut, box$(TypeRef(*old.inner)) }) + _CLONE(Pointer, { old.is_mut, box$(TypeRef(*old.inner)) }) + _CLONE(Array, { box$(TypeRef(*old.inner)), old.size }) + _COPY(Generic) + _COPY(Path) + _COPY(TraitObject) + #undef _COPY + #undef _CLONE + } +} + /// Replace this type reference with a dereferenced version bool TypeRef::deref(bool is_implicit) { diff --git a/src/types.hpp b/src/types.hpp index 5b19d036..f70de94a 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -112,29 +112,7 @@ public: m_data( mv$(other.m_data) )
{}
- TypeRef(const TypeRef& other)
- {
- switch( other.m_data.tag() )
- {
- #define _COPY(VAR) case TypeData::TAG_##VAR: m_data = TypeData::make_##VAR(other.m_data.as_##VAR()); break;
- #define _CLONE(VAR, code...) case TypeData::TAG_##VAR: { auto& old = other.m_data.as_##VAR(); m_data = TypeData::make_##VAR(code); } break;
- _COPY(None)
- _COPY(Any)
- _COPY(Macro)
- _COPY(Unit)
- _COPY(Primitive)
- _COPY(Function)
- _COPY(Tuple)
- _CLONE(Borrow, { old.is_mut, box$(TypeRef(*old.inner)) })
- _CLONE(Pointer, { old.is_mut, box$(TypeRef(*old.inner)) })
- _CLONE(Array, { box$(TypeRef(*old.inner)), old.size })
- _COPY(Generic)
- _COPY(Path)
- _COPY(TraitObject)
- #undef _COPY
- #undef _CLONE
- }
- }
+ TypeRef(const TypeRef& other);
TypeRef& operator=(const TypeRef& other) {
m_data = TypeRef(other).m_data;
return *this;
|