diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ast/ast.cpp | 2 | ||||
-rw-r--r-- | src/ast/ast.hpp | 2 | ||||
-rw-r--r-- | src/expand/mod.cpp | 6 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 6 | ||||
-rw-r--r-- | src/macro_rules/mod.cpp | 4 | ||||
-rw-r--r-- | src/parse/root.cpp | 45 | ||||
-rw-r--r-- | src/resolve/absolute.cpp | 2 |
7 files changed, 38 insertions, 29 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 354c620d..efac6520 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -248,7 +248,7 @@ UseStmt UseStmt::clone() const return UseStmt(sp, path);
}
-void ExternBlock::add_fcn(Named<Item> named_item)
+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) );
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 3f43e32a..2554ed58 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -463,7 +463,7 @@ public: const ::std::string& abi() const { return m_abi; }
- void add_fcn(Named<Item> named_item);
+ void add_item(Named<Item> named_item);
// NOTE: Only Function and Static are valid.
::std::vector<Named<Item>>& items() { return m_items; }
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 57330540..9537e5d3 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -757,6 +757,12 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> mo ), (ExternBlock, // TODO: Run expand on inner items? + // HACK: Just convert inner items into outer items + auto items = mv$( e.items() ); + for(auto& i2 : items) + { + mod.items().push_back( mv$(i2) ); + } ), (Impl, Expand_Impl(is_early, crate, modstack, modpath, mod, e); diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 9667aa70..22477926 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1038,7 +1038,11 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H BUG(sp, "Stray macro invocation in " << path); ), (ExternBlock, - TODO(sp, "Expand ExternBlock"); + if( e.items().size() > 0 ) + { + TODO(sp, "Expand ExternBlock"); + } + // TODO: Insert a record of the `link` attribute ), (Impl, TODO(sp, "Expand Item::Impl"); diff --git a/src/macro_rules/mod.cpp b/src/macro_rules/mod.cpp index 095763a8..56f75bde 100644 --- a/src/macro_rules/mod.cpp +++ b/src/macro_rules/mod.cpp @@ -138,7 +138,9 @@ bool is_token_item(eTokenType tt) { case TOK_RWORD_ENUM:
case TOK_RWORD_TRAIT:
case TOK_RWORD_MOD:
- //case TOK_RWORD_IMPL:
+ case TOK_RWORD_USE:
+ case TOK_RWORD_EXTERN:
+ case TOK_RWORD_IMPL:
// TODO: more?
case TOK_INTERPOLATED_ITEM:
return true;
diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 97f0dc30..ff792788 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -1077,7 +1077,7 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) impl.items().back().data->attrs = mv$(item_attrs); // Empty for functions
}
-void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, ::AST::MetaItems block_attrs)
+AST::ExternBlock Parse_ExternBlock(TokenStream& lex, ::std::string abi, ::AST::MetaItems& block_attrs)
{
TRACE_FUNCTION;
Token tok;
@@ -1088,7 +1088,8 @@ void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, :: GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
}
PUTBACK(tok, lex);
- // TODO: Use `block_attrs`
+
+ AST::ExternBlock rv { mv$(abi) };
while( GET_TOK(tok, lex) != TOK_BRACE_CLOSE )
{
@@ -1101,6 +1102,8 @@ void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, :: }
SET_ATTRS(lex, meta_items);
+ auto ps = lex.start_span();
+
bool is_public = false;
if( tok.type() == TOK_RWORD_PUB ) {
is_public = true;
@@ -1114,8 +1117,12 @@ void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, :: // parse function as prototype
// - no self
auto i = ::AST::Item( Parse_FunctionDef(lex, abi, meta_items, false, true) );
- mod.add_item( is_public, mv$(name), mv$(i), mv$(meta_items) );
GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
+
+ i.attrs = mv$(meta_items);
+ i.span = lex.end_span(ps);
+
+ rv.add_item( AST::Named<AST::Item> { mv$(name), mv$(i), is_public } );
break; }
case TOK_RWORD_STATIC: {
bool is_mut = false;
@@ -1130,12 +1137,16 @@ void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, :: GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
auto i = ::AST::Item(::AST::Static( (is_mut ? ::AST::Static::MUT : ::AST::Static::STATIC), type, ::AST::Expr() ));
- mod.add_item(is_public, mv$(name), mv$(i), mv$(meta_items));
+ i.attrs = mv$(meta_items);
+ i.span = lex.end_span(ps);
+ rv.add_item( AST::Named<AST::Item> { mv$(name), mv$(i), is_public } );
break; }
default:
throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_RWORD_STATIC});
}
}
+
+ return rv;
}
void Parse_Use_Wildcard(Span sp, AST::Path base_path, ::std::function<void(AST::UseStmt, ::std::string)> fcn)
@@ -1355,7 +1366,9 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::UseStmt, ::std::strin break; }
// NOTE: Extern blocks aren't items, and get handled in the caller.
case TOK_BRACE_OPEN:
- TODO(lex.getPosition(), "Parse `extern \"abi\" {` as an item");
+ item_name = "";
+ item_data = ::AST::Item( Parse_ExternBlock(lex, mv$(abi), meta_items) );
+ break;
default:
throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_BRACE_OPEN});
}
@@ -1369,12 +1382,8 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::UseStmt, ::std::strin // NOTE: `extern { ...` is handled in caller
case TOK_BRACE_OPEN:
- if( GET_TOK(tok, lex) != TOK_BRACE_CLOSE )
- {
- // TODO: Parse extern blocks into individual items (that are split up in HIR)
- TODO(lex.getPosition(), "Parse `extern {` as an item");
- }
- // HACK: Return nothing
+ item_name = "";
+ item_data = ::AST::Item( Parse_ExternBlock(lex, "", meta_items) );
break;
// `extern crate "crate-name" as crate_name;`
@@ -1686,20 +1695,6 @@ void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_item });
GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
}
- //else if( LOOKAHEAD2(lex, TOK_RWORD_EXTERN, TOK_BRACE_OPEN) || LOOKAHEAD3(lex, TOK_RWORD_EXTERN, TOK_STRING, TOK_BRACE_OPEN) )
- else if( lex.lookahead(0) == TOK_RWORD_EXTERN && ( (lex.lookahead(1) == TOK_STRING && lex.lookahead(2) == TOK_BRACE_OPEN) || lex.lookahead(1) == TOK_BRACE_OPEN ) )
- {
- // `extern "<ABI>" { ...`
- GET_CHECK_TOK(tok, lex, TOK_RWORD_EXTERN);
- ::std::string abi = "C";
- if( GET_TOK(tok, lex) == TOK_STRING ) {
- abi = tok.str();
- GET_TOK(tok, lex);
- }
- CHECK_TOK(tok, TOK_BRACE_OPEN);
-
- Parse_ExternBlock(lex, mod, mv$(abi), mv$(meta_items));
- }
// `unsafe impl`
// TODO: Move these two into Parse_Mod_Item_S
//else if( LOOKAHEAD1(lex, TOK_RWORD_IMPL) || LOOKAHEAD2(lex, TOK_RWORD_UNSAFE, TOK_RWORD_IMPL) )
diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 118eca7f..c2e77678 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1901,6 +1901,8 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) ( BUG(i2.data.span, "Unexpected item in ExternBlock - " << i2.data.tag_str()); ), + (None, + ), (Function, Resolve_Absolute_Function(item_context, e2); ), |