summaryrefslogtreecommitdiff
path: root/src/expand
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2017-05-28 13:45:16 +0800
committerJohn Hodge <tpg@ucc.asn.au>2017-05-28 13:45:16 +0800
commit6747de8643ffd4a0a1705ac4c00c8cfb9a957154 (patch)
tree70b463241e2dcccd247afddf1fc1135c4f547127 /src/expand
parent99350d82f33acbbd3968dd0e45cfcfa415373b19 (diff)
downloadmrust-6747de8643ffd4a0a1705ac4c00c8cfb9a957154.tar.gz
AST - Refactor to make unit-like structs their own variant
Diffstat (limited to 'src/expand')
-rw-r--r--src/expand/derive.cpp65
-rw-r--r--src/expand/mod.cpp2
2 files changed, 45 insertions, 22 deletions
diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp
index c363b689..f09e9298 100644
--- a/src/expand/derive.cpp
+++ b/src/expand/derive.cpp
@@ -102,6 +102,8 @@ struct Deriver
{
::std::vector<TypeRef> ret;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -299,6 +301,13 @@ public:
// Generate code for Debug
AST::ExprNodeP node;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ node = NEWNODE(NamedValue, AST::Path("f"));
+ node = NEWNODE(CallMethod,
+ mv$(node), AST::PathNode("write_str",{}),
+ vec$( NEWNODE(String, name) )
+ );
+ ),
(Struct,
node = NEWNODE(NamedValue, AST::Path("f"));
node = NEWNODE(CallMethod,
@@ -480,6 +489,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -685,6 +696,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -896,6 +909,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -1049,6 +1064,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -1265,6 +1282,9 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
+ ),
(Struct,
::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals;
for( const auto& fld : e.ents )
@@ -1274,19 +1294,12 @@ public:
nodes.push_back( NEWNODE(StructLiteral, ty_path, nullptr, mv$(vals)) );
),
(Tuple,
- if( e.ents.size() == 0 )
- {
- nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
- }
- else
+ ::std::vector<AST::ExprNodeP> vals;
+ for( unsigned int idx = 0; idx < e.ents.size(); idx ++ )
{
- ::std::vector<AST::ExprNodeP> vals;
- for( unsigned int idx = 0; idx < e.ents.size(); idx ++ )
- {
- vals.push_back( this->clone_val_ref(core_name, this->field(FMT(idx))) );
- }
- nodes.push_back( NEWNODE(CallPath, AST::Path(ty_path), mv$(vals)) );
+ vals.push_back( this->clone_val_ref(core_name, this->field(FMT(idx))) );
}
+ nodes.push_back( NEWNODE(CallPath, AST::Path(ty_path), mv$(vals)) );
)
)
@@ -1428,6 +1441,9 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
+ ),
(Struct,
::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals;
for( const auto& fld : e.ents )
@@ -1437,19 +1453,12 @@ public:
nodes.push_back( NEWNODE(StructLiteral, ty_path, nullptr, mv$(vals)) );
),
(Tuple,
- if( e.ents.size() == 0 )
- {
- nodes.push_back( NEWNODE(NamedValue, AST::Path(ty_path)) );
- }
- else
+ ::std::vector<AST::ExprNodeP> vals;
+ for( unsigned int idx = 0; idx < e.ents.size(); idx ++ )
{
- ::std::vector<AST::ExprNodeP> vals;
- for( unsigned int idx = 0; idx < e.ents.size(); idx ++ )
- {
- vals.push_back( this->default_call(core_name) );
- }
- nodes.push_back( NEWNODE(CallPath, AST::Path(ty_path), mv$(vals)) );
+ vals.push_back( this->default_call(core_name) );
}
+ nodes.push_back( NEWNODE(CallPath, AST::Path(ty_path), mv$(vals)) );
)
)
@@ -1522,6 +1531,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
for( const auto& fld : e.ents )
{
@@ -1680,6 +1691,8 @@ public:
::std::vector<AST::ExprNodeP> nodes;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
unsigned int idx = 0;
for( const auto& fld : e.ents )
@@ -1707,6 +1720,9 @@ public:
::AST::ExprNodeP node;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ node = get_val_ok(core_name);
+ ),
(Struct,
node = NEWNODE(CallPath,
this->get_trait_path_Encoder() + "emit_struct",
@@ -1917,6 +1933,8 @@ public:
AST::ExprNodeP node_v;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ ),
(Struct,
::std::vector< ::std::pair< ::std::string, AST::ExprNodeP > > vals;
unsigned int idx = 0;
@@ -1949,6 +1967,9 @@ public:
::AST::ExprNodeP node;
TU_MATCH(AST::StructData, (str.m_data), (e),
+ (Unit,
+ node = NEWNODE(NamedValue, mv$(base_path));
+ ),
(Struct,
assert( !args[2] );
args[2] = NEWNODE(Integer, e.ents.size(), CORETYPE_UINT);
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index 788b2109..1ee2e19e 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -896,6 +896,8 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::
(Struct,
TU_MATCH(AST::StructData, (e.m_data), (sd),
+ (Unit,
+ ),
(Struct,
for(auto it = sd.ents.begin(); it != sd.ents.end(); ) {
auto& si = *it;