From b7b634f517967da41befff67d579c5a1afa8d016 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 19 Aug 2016 11:15:45 +0800 Subject: AST - Macro invocations in item list --- src/ast/ast.cpp | 3 +++ src/ast/ast.hpp | 1 + src/expand/mod.cpp | 3 +++ src/hir/from_ast.cpp | 3 +++ src/parse/root.cpp | 12 ++++++++++++ src/resolve/absolute.cpp | 4 ++++ src/resolve/index.cpp | 2 ++ src/resolve/use.cpp | 3 +++ 8 files changed, 31 insertions(+) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 1da79f33..8052a51e 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -262,6 +262,9 @@ Module::ItemRef Module::find_item(const ::std::string& needle, bool allow_leaves (None, throw ::std::runtime_error("BUG: Hit a None item"); ), + (MacroInv, + throw ::std::runtime_error("BUG: Hit a macro invocation"); + ), (Module, return ItemRef(e); ), (Crate, return ItemRef(e.name); ), (Type, return ItemRef(e); ), diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 02e9c5f2..b14d7e46 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -661,6 +661,7 @@ private: TAGGED_UNION_EX(Item, (: public Serialisable), None, ( (None, struct {} ), + (MacroInv, MacroInvocation), (Module, Module), (Crate, struct { ::std::string name; diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 01342ea5..d4c117b1 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -616,6 +616,9 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList mo (None, // Skip, nothing ), + (MacroInv, + TODO(Span(), "Macro invocation"); + ), (Module, LList sub_modstack(&modstack, &e); Expand_Mod(is_early, crate, sub_modstack, path, e); diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 5c84b8f1..90ab5c2a 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -941,6 +941,9 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H TU_MATCH(::AST::Item, (item.data), (e), (None, ), + (MacroInv, + BUG(Span(), "Stray macro invocation in " << path); + ), (Module, _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Module(e, mv$(item_path)) ); ), diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 87d1e9c3..b932bf1a 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -582,6 +582,7 @@ AST::Trait Parse_TraitDef(TokenStream& lex, AST::Module& mod, const AST::MetaIte CHECK_TOK(tok, TOK_BRACE_OPEN); while( GET_TOK(tok, lex) != TOK_BRACE_CLOSE ) { + AST::MetaItems item_attrs; while( tok.type() == TOK_ATTR_OPEN ) { @@ -591,6 +592,17 @@ AST::Trait Parse_TraitDef(TokenStream& lex, AST::Module& mod, const AST::MetaIte } SET_ATTRS(lex, item_attrs); + auto ps = lex.start_span(); + if( tok.type() == TOK_MACRO ) { + auto inv = Parse_MacroInvocation( ps, AST::MetaItems(), mv$(tok.str()), lex ); + // - Silently consume ';' after the macro + if( GET_TOK(tok, lex) != TOK_SEMICOLON ) + PUTBACK(tok, lex); + + trait.items().push_back( AST::Named("", AST::Item(mv$(inv)), false) ); + continue ; + } + bool is_specialisable = false; if( tok.type() == TOK_IDENT && tok.str() == "default" ) { is_specialisable = true; diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 8938fbb1..0a0cf478 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1229,6 +1229,7 @@ void Resolve_Absolute_ImplItems(Context& item_context, ::AST::NamedList< ::AST: { TU_MATCH(AST::Item, (i.data), (e), (None, ), + (MacroInv, BUG(i.data.span, "Resolve_Absolute_ImplItems - MacroInv");), (Module, BUG(i.data.span, "Resolve_Absolute_ImplItems - Module");), (Crate , BUG(i.data.span, "Resolve_Absolute_ImplItems - Crate");), (Enum , BUG(i.data.span, "Resolve_Absolute_ImplItems - Enum");), @@ -1283,6 +1284,7 @@ void Resolve_Absolute_ImplItems(Context& item_context, ::std::vector< ::AST::Im { TU_MATCH(AST::Item, (*i.data), (e), (None, ), + (MacroInv, BUG(i.data->span, "Resolve_Absolute_ImplItems - MacroInv");), (Module, BUG(i.data->span, "Resolve_Absolute_ImplItems - Module");), (Crate , BUG(i.data->span, "Resolve_Absolute_ImplItems - Crate");), (Enum , BUG(i.data->span, "Resolve_Absolute_ImplItems - Enum");), @@ -1342,6 +1344,8 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) TU_MATCH(AST::Item, (i.data), (e), (None, ), + (MacroInv, + ), (Module, DEBUG("Module - " << i.name); Resolve_Absolute_Mod(item_context.m_crate, e); diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp index eeae3f5b..57f33cc6 100644 --- a/src/resolve/index.cpp +++ b/src/resolve/index.cpp @@ -79,6 +79,8 @@ void Resolve_Index_Module_Base(AST::Module& mod) TU_MATCH(AST::Item, (i.data), (e), (None, ), + (MacroInv, + ), // - Types/modules only (Module, p.bind( ::AST::PathBinding::make_Module({&e}) ); diff --git a/src/resolve/use.cpp b/src/resolve/use.cpp index 361cca76..e68580ee 100644 --- a/src/resolve/use.cpp +++ b/src/resolve/use.cpp @@ -219,6 +219,9 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path (None, // IMPOSSIBLE - Handled above ), + (MacroInv, + BUG(span, "HIt MacroInv in use resolution"); + ), (Crate, //return ::AST::PathBinding::make_Crate({&e}); TODO(span, "Handle importing from a crate"); -- cgit v1.2.3