diff options
author | John Hodge <tpg@mutabah.net> | 2018-07-29 12:47:44 +0100 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2018-07-29 12:47:44 +0100 |
commit | c971a6fa8375598ecf9c99ee6b086e8cf957f568 (patch) | |
tree | f7f431cbac52e6b25d311bedd503b6584c51bd37 /src/expand | |
parent | 5d4bf9e96d795623f1b32b2f1f2e57c4f74419fe (diff) | |
download | mrust-c971a6fa8375598ecf9c99ee6b086e8cf957f568.tar.gz |
All - Initial work on supporting 1.29 as a target version
Diffstat (limited to 'src/expand')
-rw-r--r-- | src/expand/assert.cpp | 89 | ||||
-rw-r--r-- | src/expand/file_line.cpp | 9 | ||||
-rw-r--r-- | src/expand/lang_item.cpp | 15 | ||||
-rw-r--r-- | src/expand/mod.cpp | 19 |
4 files changed, 130 insertions, 2 deletions
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 <synext_macro.hpp> +#include <synext.hpp> // for Expand_BareExpr +#include <parse/interpolated_fragment.hpp> +#include "../parse/ttstream.hpp" +#include "../parse/common.hpp" +#include "../parse/parseerror.hpp" + +class CExpander_assert: + public ExpandProcMacro +{ + ::std::unique_ptr<TokenStream> 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<TokenTree> 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<ExpandProcMacro>(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<TokenStream> 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 '"<<name<<"' to " << path); } else if( name == "add" ) { DEBUG("Bind '"<<name<<"' to " << path); } @@ -67,7 +68,8 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "fn_once" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "eq" ) { DEBUG("Bind '"<<name<<"' to " << path); } - else if( name == "ord" ) { DEBUG("Bind '"<<name<<"' to " << path); } + else if( TARGETVER_1_19 && name == "ord" ) { DEBUG("Bind '"<<name<<"' to " << path); } + else if( TARGETVER_1_29 && name == "partial_ord" ) { DEBUG("Bind '"<<name<<"' to " << path); } // New name for v1.29 else if( name == "unsize" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "coerce_unsized" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "freeze" ) { DEBUG("Bind '"<<name<<"' to " << path); } @@ -84,6 +86,12 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "range_from" ) { } else if( name == "range_to" ) { } else if( name == "unsafe_cell" ) { } + else if( TARGETVER_1_29 && name == "alloc_layout") { } + else if( TARGETVER_1_29 && name == "panic_info" ) {} // Struct + + // Generators + else if( TARGETVER_1_29 && name == "generator" ) {} // - Trait + else if( TARGETVER_1_29 && name == "generator_state" ) {} // - State enum // Statics else if( name == "msvc_try_filter" ) { } @@ -96,6 +104,7 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, } else if( name == "str_eq" ) { } else if( name == "drop_in_place" ) { } + else if( name == "align_offset" ) { } // - builtin `box` support else if( name == "exchange_malloc" ) { } else if( name == "exchange_free" ) { } @@ -148,6 +157,9 @@ public: (Struct, handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_STRUCT); ), + (Enum, + handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_ENUM); + ), (Trait, handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_TRAIT); ) @@ -176,6 +188,7 @@ public: // collections else if( name == "str" ) {} else if( name == "slice" ) {} + else if( TARGETVER_1_29 && name == "slice_u8" ) {} // std - interestingly else if( name == "f32" ) {} else if( name == "f64" ) {} diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 5d6d3234..872105a6 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -41,6 +41,12 @@ void Register_Synext_Macro_Static(MacroDef* def) { g_macros_list = def; } +void Expand_Init() +{ + // TODO: Initialise all macros here. + void Expand_init_assert(); Expand_init_assert(); +} + void ExpandDecorator::unexpected(const Span& sp, const AST::Attribute& mi, const char* loc_str) const { @@ -86,7 +92,7 @@ void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& c Expand_Attrs(attrs, stage, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate, mod, impl); }); } -::std::unique_ptr<TokenStream> Expand_Macro( +::std::unique_ptr<TokenStream> Expand_Macro_Inner( const ::AST::Crate& crate, LList<const AST::Module*> 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<TokenStream> Expand_Macro( + const ::AST::Crate& crate, LList<const AST::Module*> 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<TokenStream> Expand_Macro(const ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi) { return Expand_Macro(crate, modstack, mod, mi.span(), mi.name(), mi.input_ident(), mi.input_tt()); |