From c971a6fa8375598ecf9c99ee6b086e8cf957f568 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 29 Jul 2018 12:47:44 +0100 Subject: All - Initial work on supporting 1.29 as a target version --- src/expand/assert.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ src/expand/file_line.cpp | 9 +++++ src/expand/lang_item.cpp | 15 +++++++- src/expand/mod.cpp | 19 ++++++++++- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/expand/assert.cpp (limited to 'src/expand') diff --git a/src/expand/assert.cpp b/src/expand/assert.cpp new file mode 100644 index 00000000..1c057935 --- /dev/null +++ b/src/expand/assert.cpp @@ -0,0 +1,89 @@ +/* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * expand/assert.cpp + * - assert! built-in macro (1.29) + */ +#include +#include // for Expand_BareExpr +#include +#include "../parse/ttstream.hpp" +#include "../parse/common.hpp" +#include "../parse/parseerror.hpp" + +class CExpander_assert: + public ExpandProcMacro +{ + ::std::unique_ptr expand(const Span& sp, const ::AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override + { + Token tok; + + auto lex = TTStream(sp, tt); + lex.parse_state().module = &mod; + if( ident != "" ) + ERROR(sp, E0000, "format_args! doesn't take an ident"); + + // assertion condition + auto n = Parse_Expr0(lex); + ASSERT_BUG(sp, n, "No expression returned"); + + ::std::vector toks; + + toks.push_back( Token(TOK_RWORD_IF) ); + + GET_TOK(tok, lex); + if( tok == TOK_COMMA ) + { + toks.push_back( Token(InterpolatedFragment(InterpolatedFragment::EXPR, n.release())) ); + toks.push_back( Token(TOK_BRACE_OPEN) ); + // User-provided message + toks.push_back( Token(TOK_IDENT, "panic") ); + toks.push_back( Token(TOK_EXCLAM) ); + toks.push_back( Token(TOK_PAREN_OPEN) ); + while(lex.lookahead(0) != TOK_EOF ) + { + toks.push_back( Token(InterpolatedFragment(InterpolatedFragment::EXPR, Parse_Expr0(lex).release())) ); + if( lex.lookahead(0) != TOK_COMMA ) + break; + GET_CHECK_TOK(tok, lex, TOK_COMMA); + toks.push_back( Token(TOK_COMMA) ); + } + GET_CHECK_TOK(tok, lex, TOK_EOF); + toks.push_back( Token(TOK_PAREN_CLOSE) ); + } + else if( tok == TOK_EOF ) + { + ::std::stringstream ss; + ss << "assertion failed: "; + n->print(ss); + + toks.push_back( Token(InterpolatedFragment(InterpolatedFragment::EXPR, n.release())) ); + + toks.push_back( Token(TOK_BRACE_OPEN) ); + // Auto-generated message + toks.push_back( Token(TOK_IDENT, "panic") ); + toks.push_back( Token(TOK_EXCLAM) ); + toks.push_back( Token(TOK_PAREN_OPEN) ); + toks.push_back( Token(TOK_STRING, ss.str()) ); + toks.push_back( Token(TOK_PAREN_CLOSE) ); + } + else + { + throw ParseError::Unexpected(lex, tok, {TOK_COMMA, TOK_EOF}); + } + + toks.push_back( Token(TOK_BRACE_CLOSE) ); + + return box$( TTStreamO(sp, TokenTree(Ident::Hygiene::new_scope(), mv$(toks))) ); + } +}; + +void Expand_init_assert() +{ + if( TARGETVER_1_29 ) + { + Register_Synext_Macro("assert", ::std::unique_ptr(new CExpander_assert)); + } +} + diff --git a/src/expand/file_line.cpp b/src/expand/file_line.cpp index 7a827ccf..2bf85ffd 100644 --- a/src/expand/file_line.cpp +++ b/src/expand/file_line.cpp @@ -46,6 +46,14 @@ class CExpanderColumn: return box$( TTStreamO(sp, TokenTree(Token((uint64_t)get_top_span(sp).start_ofs, CORETYPE_U32))) ); } }; +class CExpanderUnstableColumn: + public ExpandProcMacro +{ + ::std::unique_ptr expand(const Span& sp, const AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override + { + return box$( TTStreamO(sp, TokenTree(Token((uint64_t)get_top_span(sp).start_ofs, CORETYPE_U32))) ); + } +}; class CExpanderModulePath: public ExpandProcMacro @@ -65,5 +73,6 @@ class CExpanderModulePath: STATIC_MACRO("file", CExpanderFile); STATIC_MACRO("line", CExpanderLine); STATIC_MACRO("column", CExpanderColumn); +STATIC_MACRO("__rust_unstable_column", CExpanderUnstableColumn); STATIC_MACRO("module_path", CExpanderModulePath); diff --git a/src/expand/lang_item.cpp b/src/expand/lang_item.cpp index 789ad88e..62527ac9 100644 --- a/src/expand/lang_item.cpp +++ b/src/expand/lang_item.cpp @@ -30,6 +30,7 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "copy" ) { DEBUG("Bind 'copy' to " << path); } + else if( TARGETVER_1_29 && name == "clone" ) {} // - Trait // ops traits else if( name == "drop" ) { DEBUG("Bind '"< Expand_Macro( +::std::unique_ptr Expand_Macro_Inner( const ::AST::Crate& crate, LList modstack, ::AST::Module& mod, Span mi_span, const ::std::string& name, const ::std::string& input_ident, TokenTree& input_tt ) @@ -144,6 +150,17 @@ void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& c // Error - Unknown macro name ERROR(mi_span, E0000, "Unknown macro '" << name << "'"); } +::std::unique_ptr Expand_Macro( + const ::AST::Crate& crate, LList modstack, ::AST::Module& mod, + Span mi_span, const ::std::string& name, const ::std::string& input_ident, TokenTree& input_tt + ) +{ + auto rv = Expand_Macro_Inner(crate, modstack, mod, mi_span, name, input_ident, input_tt); + assert(rv); + rv->parse_state().module = &mod; + rv->parse_state().crate = &crate; + return rv; +} ::std::unique_ptr Expand_Macro(const ::AST::Crate& crate, LList modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi) { return Expand_Macro(crate, modstack, mod, mi.span(), mi.name(), mi.input_ident(), mi.input_tt()); -- cgit v1.2.3