summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2017-07-16 22:29:44 +0800
committerJohn Hodge <tpg@ucc.asn.au>2017-07-16 22:29:44 +0800
commit9fe26bb83dcee67be874e57c439ea2e1ee984306 (patch)
tree6d720fd25f20f6bebc89df96c2a6106e79b32a54
parent3ae9e699d94196621f9a9ee6fc1962b2862e7438 (diff)
downloadmrust-9fe26bb83dcee67be874e57c439ea2e1ee984306.tar.gz
AST - Attributes on struct literal entries
-rw-r--r--src/ast/dump.cpp5
-rw-r--r--src/ast/expr.cpp6
-rw-r--r--src/ast/expr.hpp7
-rw-r--r--src/expand/derive.cpp24
-rw-r--r--src/expand/mod.cpp16
-rw-r--r--src/expand/test_harness.cpp14
-rw-r--r--src/hir/from_ast_expr.cpp6
-rw-r--r--src/parse/expr.cpp20
8 files changed, 56 insertions, 42 deletions
diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp
index 0d77054c..363123fa 100644
--- a/src/ast/dump.cpp
+++ b/src/ast/dump.cpp
@@ -404,8 +404,9 @@ public:
inc_indent();
for( const auto& i : n.m_values )
{
- m_os << indent() << i.first << ": ";
- AST::NodeVisitor::visit(i.second);
+ // TODO: Attributes
+ m_os << indent() << i.name << ": ";
+ AST::NodeVisitor::visit(i.value);
m_os << ",\n";
}
if( n.m_base_value.get() )
diff --git a/src/ast/expr.cpp b/src/ast/expr.cpp
index c1baf9b4..535c2e24 100644
--- a/src/ast/expr.cpp
+++ b/src/ast/expr.cpp
@@ -286,7 +286,7 @@ NODE(ExprNode_StructLiteral, {
os << m_path << " { ";
for(const auto& v : m_values)
{
- os << v.first << ": " << *v.second << ", ";
+ os << v.name << ": " << *v.value << ", ";
}
if(m_base_value)
{
@@ -297,7 +297,7 @@ NODE(ExprNode_StructLiteral, {
ExprNode_StructLiteral::t_values vals;
for(const auto& v : m_values) {
- vals.push_back( ::std::make_pair(v.first, v.second->clone()) );
+ vals.push_back({ v.attrs.clone(), v.name, v.value->clone() });
}
return NEWNODE(ExprNode_StructLiteral, AST::Path(m_path), OPT_CLONE(m_base_value), mv$(vals) );
@@ -550,7 +550,7 @@ NV(ExprNode_StructLiteral,
{
visit(node.m_base_value);
for( auto& val : node.m_values )
- visit(val.second);
+ visit(val.value);
})
NV(ExprNode_Array,
{
diff --git a/src/ast/expr.hpp b/src/ast/expr.hpp
index a6b58e03..2c9a1c35 100644
--- a/src/ast/expr.hpp
+++ b/src/ast/expr.hpp
@@ -417,7 +417,12 @@ struct ExprNode_Closure:
struct ExprNode_StructLiteral:
public ExprNode
{
- typedef ::std::vector< ::std::pair< ::std::string, unique_ptr<ExprNode> > > t_values;
+ struct Ent {
+ MetaItems attrs;
+ ::std::string name;
+ unique_ptr<ExprNode> value;
+ };
+ typedef ::std::vector<Ent> t_values;
Path m_path;
unique_ptr<ExprNode> m_base_value;
t_values m_values;
diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp
index f09e9298..30adadc1 100644
--- a/src/expand/derive.cpp
+++ b/src/expand/derive.cpp
@@ -1286,10 +1286,10 @@ public:
nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
),
(Struct,
- ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals;
+ ::AST::ExprNode_StructLiteral::t_values vals;
for( const auto& fld : e.ents )
{
- vals.push_back( ::std::make_pair(fld.m_name, this->clone_val_ref(core_name, this->field(fld.m_name)) ) );
+ vals.push_back({ {}, fld.m_name, this->clone_val_ref(core_name, this->field(fld.m_name)) });
}
nodes.push_back( NEWNODE(StructLiteral, ty_path, nullptr, mv$(vals)) );
),
@@ -1338,13 +1338,13 @@ public:
),
(Struct,
::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a;
- ::std::vector< ::std::pair<std::string, AST::ExprNodeP> > vals;
+ ::AST::ExprNode_StructLiteral::t_values vals;
for( const auto& fld : e.m_fields )
{
auto name_a = FMT("a" << fld.m_name);
pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) );
- vals.push_back( ::std::make_pair( fld.m_name, this->clone_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) ) );
+ vals.push_back({ {}, fld.m_name, this->clone_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) });
}
pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true);
@@ -1445,10 +1445,10 @@ public:
nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
),
(Struct,
- ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals;
+ ::AST::ExprNode_StructLiteral::t_values vals;
for( const auto& fld : e.ents )
{
- vals.push_back( ::std::make_pair(fld.m_name, this->default_call(core_name)) );
+ vals.push_back({ {}, fld.m_name, this->default_call(core_name) });
}
nodes.push_back( NEWNODE(StructLiteral, ty_path, nullptr, mv$(vals)) );
),
@@ -1936,14 +1936,14 @@ public:
(Unit,
),
(Struct,
- ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP > > vals;
+ ::AST::ExprNode_StructLiteral::t_values vals;
unsigned int idx = 0;
for( const auto& fld : e.ents )
{
- vals.push_back(::std::make_pair(fld.m_name, NEWNODE(UniOp, ::AST::ExprNode_UniOp::QMARK, NEWNODE(CallPath,
+ vals.push_back({ {}, fld.m_name, NEWNODE(UniOp, ::AST::ExprNode_UniOp::QMARK, NEWNODE(CallPath,
this->get_trait_path_Decoder() + "read_struct_field",
vec$( NEWNODE(NamedValue, AST::Path("d")), NEWNODE(String, fld.m_name), NEWNODE(Integer, idx, CORETYPE_UINT), this->dec_closure( sp, this->dec_val() ) )
- )) ));
+ )) });
idx ++;
}
node_v = NEWNODE(StructLiteral, base_path, nullptr, mv$(vals));
@@ -2021,21 +2021,21 @@ public:
code = NEWNODE(CallPath, base_path + v.m_name, mv$(args));
),
(Struct,
- ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP > > vals;
+ ::AST::ExprNode_StructLiteral::t_values vals;
unsigned int idx = 0;
for( const auto& fld : e.m_fields )
{
auto name_a = FMT("a" << fld.m_name);
- vals.push_back(::std::make_pair(fld.m_name, NEWNODE(UniOp, ::AST::ExprNode_UniOp::QMARK, NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_enum_struct_variant_field",
+ vals.push_back({ {}, fld.m_name, NEWNODE(UniOp, ::AST::ExprNode_UniOp::QMARK, NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_enum_struct_variant_field",
vec$(
NEWNODE(NamedValue, AST::Path("d")),
NEWNODE(String, fld.m_name),
NEWNODE(Integer, idx, CORETYPE_UINT),
this->dec_closure(sp, this->dec_val())
)
- ) )));
+ ) )});
idx ++;
}
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index adc53bf4..9137cc02 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -657,7 +657,7 @@ struct CExpandExpr:
for(auto& val : node.m_values)
{
// TODO: Attributes on struct literal items (#[cfg] only?)
- this->visit_nodelete(node, val.second);
+ this->visit_nodelete(node, val.value);
}
}
void visit(::AST::ExprNode_Array& node) override {
@@ -705,18 +705,18 @@ struct CExpandExpr:
::AST::ExprNode_StructLiteral::t_values values;
if( node.m_left && node.m_right )
{
- values.push_back( ::std::make_pair( ::std::string("start"), mv$(node.m_left ) ) );
- values.push_back( ::std::make_pair( ::std::string("end") , mv$(node.m_right) ) );
+ values.push_back({ {}, "start", mv$(node.m_left ) });
+ values.push_back({ {}, "end" , mv$(node.m_right) });
replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_Range), nullptr, mv$(values)) );
}
else if( node.m_left )
{
- values.push_back( ::std::make_pair( ::std::string("start"), mv$(node.m_left ) ) );
+ values.push_back({ {}, "start", mv$(node.m_left ) });
replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_RangeFrom), nullptr, mv$(values)) );
}
else if( node.m_right )
{
- values.push_back( ::std::make_pair( ::std::string("end") , mv$(node.m_right) ) );
+ values.push_back({ {}, "end" , mv$(node.m_right) });
replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_RangeTo), nullptr, mv$(values)) );
}
else
@@ -734,14 +734,14 @@ struct CExpandExpr:
if( node.m_left )
{
::AST::ExprNode_StructLiteral::t_values values;
- values.push_back( ::std::make_pair( ::std::string("start"), mv$(node.m_left) ) );
- values.push_back( ::std::make_pair( ::std::string("end") , mv$(node.m_right) ) );
+ values.push_back({ {}, "start", mv$(node.m_left) });
+ values.push_back({ {}, "end" , mv$(node.m_right) });
replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_RangeInclusive_NonEmpty), nullptr, mv$(values)) );
}
else
{
::AST::ExprNode_StructLiteral::t_values values;
- values.push_back( ::std::make_pair( ::std::string("end") , mv$(node.m_right) ) );
+ values.push_back({ {}, "end", mv$(node.m_right) });
replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_RangeToInclusive), nullptr, mv$(values)) );
}
replacement->set_span( node.span() );
diff --git a/src/expand/test_harness.cpp b/src/expand/test_harness.cpp
index 12d32121..204ca75e 100644
--- a/src/expand/test_harness.cpp
+++ b/src/expand/test_harness.cpp
@@ -55,12 +55,12 @@ void Expand_TestHarness(::AST::Crate& crate)
::AST::ExprNode_StructLiteral::t_values desc_vals;
// `name: "foo",`
- desc_vals.push_back( ::std::make_pair("name", NEWNODE(_CallPath,
+ desc_vals.push_back({ {}, "name", NEWNODE(_CallPath,
::AST::Path("test", { ::AST::PathNode("StaticTestName") }),
::make_vec1( NEWNODE(_String, test.name) )
- ) ));
+ ) });
// `ignore: false,`
- desc_vals.push_back( ::std::make_pair("ignore", NEWNODE(_Bool, test.ignore)) );
+ desc_vals.push_back({ {}, "ignore", NEWNODE(_Bool, test.ignore) });
// `should_panic: ShouldPanic::No,`
{
::AST::ExprNodeP should_panic_val;
@@ -79,18 +79,18 @@ void Expand_TestHarness(::AST::Crate& crate)
);
break;
}
- desc_vals.push_back( ::std::make_pair("should_panic", mv$(should_panic_val)) );
+ desc_vals.push_back({ {}, "should_panic", mv$(should_panic_val) });
}
auto desc_expr = NEWNODE(_StructLiteral, ::AST::Path("test", { ::AST::PathNode("TestDesc")}), nullptr, mv$(desc_vals));
::AST::ExprNode_StructLiteral::t_values descandfn_vals;
- descandfn_vals.push_back( ::std::make_pair(::std::string("desc"), mv$(desc_expr)) );
+ descandfn_vals.push_back({ {}, ::std::string("desc"), mv$(desc_expr) });
auto test_type_var_name = test.is_benchmark ? "StaticBenchFn" : "StaticTestFn";
- descandfn_vals.push_back( ::std::make_pair(::std::string("testfn"), NEWNODE(_CallPath,
+ descandfn_vals.push_back({ {}, ::std::string("testfn"), NEWNODE(_CallPath,
::AST::Path("test", { ::AST::PathNode(test_type_var_name) }),
::make_vec1( NEWNODE(_NamedValue, AST::Path(test.path)) )
- ) ) );
+ ) });
test_nodes.push_back( NEWNODE(_StructLiteral, ::AST::Path("test", { ::AST::PathNode("TestDescAndFn")}), nullptr, mv$(descandfn_vals) ) );
}
diff --git a/src/hir/from_ast_expr.cpp b/src/hir/from_ast_expr.cpp
index 4a82867f..9bbb0418 100644
--- a/src/hir/from_ast_expr.cpp
+++ b/src/hir/from_ast_expr.cpp
@@ -518,15 +518,15 @@ struct LowerHIR_ExprNode_Visitor:
m_rv.reset( new ::HIR::ExprNode_UnionLiteral( v.span(),
LowerHIR_GenericPath(v.span(), v.m_path),
- v.m_values[0].first,
- LowerHIR_ExprNode_Inner(*v.m_values[0].second)
+ v.m_values[0].name,
+ LowerHIR_ExprNode_Inner(*v.m_values[0].value)
) );
}
else
{
::HIR::ExprNode_StructLiteral::t_values values;
for(const auto& val : v.m_values)
- values.push_back( ::std::make_pair(val.first, LowerHIR_ExprNode_Inner(*val.second)) );
+ values.push_back( ::std::make_pair(val.name, LowerHIR_ExprNode_Inner(*val.value)) );
m_rv.reset( new ::HIR::ExprNode_StructLiteral( v.span(),
LowerHIR_GenericPath(v.span(), v.m_path),
! v.m_path.binding().is_EnumVar(),
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp
index 547e38e0..788a68f5 100644
--- a/src/parse/expr.cpp
+++ b/src/parse/expr.cpp
@@ -996,22 +996,30 @@ ExprNodeP Parse_ExprVal_StructLiteral(TokenStream& lex, AST::Path path)
// Braced structure literal
// - A series of 0 or more pairs of <ident>: <expr>,
// - '..' <expr>
- ::std::vector< ::std::pair< ::std::string, ::std::unique_ptr<AST::ExprNode>> > items;
- while( GET_TOK(tok, lex) == TOK_IDENT )
+ ::AST::ExprNode_StructLiteral::t_values items;
+ while( GET_TOK(tok, lex) == TOK_IDENT || tok.type() == TOK_ATTR_OPEN )
{
+ ::AST::MetaItems attrs;
+ while( tok.type() == TOK_ATTR_OPEN )
+ {
+ attrs.push_back( Parse_MetaItem(lex) );
+ GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
+ GET_TOK(tok, lex);
+ }
+ CHECK_TOK(tok, TOK_IDENT);
auto name = mv$(tok.str());
+ ExprNodeP val;
if( lex.lookahead(0) != TOK_COLON )
{
- ExprNodeP val = NEWNODE( AST::ExprNode_NamedValue, ::AST::Path(name) );
- items.push_back( ::std::make_pair(::std::move(name), ::std::move(val)) );
+ val = NEWNODE( AST::ExprNode_NamedValue, ::AST::Path(name) );
}
else
{
GET_CHECK_TOK(tok, lex, TOK_COLON);
- ExprNodeP val = Parse_Stmt(lex);
- items.push_back( ::std::make_pair(::std::move(name), ::std::move(val)) );
+ val = Parse_Stmt(lex);
}
+ items.push_back(::AST::ExprNode_StructLiteral::Ent { mv$(attrs), mv$(name), mv$(val) });
if( GET_TOK(tok,lex) == TOK_BRACE_CLOSE )
break;