From 143219798492864c8eff7fecbdb9257fa10bf4ec Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 6 Jul 2022 16:57:26 +0100 Subject: Allow linemap to be optional nullptr --- gcc/rust/lex/rust-lex.cc | 20 ++++++++++++++------ gcc/rust/lex/rust-lex.h | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index 13921e7..5447c72 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -347,7 +347,7 @@ Lexer::build_token () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); break; } else @@ -368,7 +368,7 @@ Lexer::build_token () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); continue; case '\r': // cr // Ignore, we expect a newline (lf) soon. @@ -540,7 +540,7 @@ Lexer::build_token () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); str.shrink_to_fit (); if (is_inner) @@ -617,7 +617,7 @@ Lexer::build_token () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); continue; } @@ -686,7 +686,7 @@ Lexer::build_token () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); str += '\n'; continue; } @@ -1400,7 +1400,7 @@ Lexer::parse_partial_string_continue () current_line++; current_column = 1; // tell line_table that new line starts - line_map->start_line (current_line, max_column_hint); + start_line (current_line, max_column_hint); // reset "length" additional_length_offset = 1; @@ -2688,4 +2688,12 @@ Lexer::split_current_token (TokenId new_left, TokenId new_right) token_queue.replace_current_value (std::move (new_left_tok)); token_queue.insert (1, std::move (new_right_tok)); } + +void +Lexer::start_line (int current_line, int current_column) +{ + if (line_map) + line_map->start_line (current_line, current_column); +} + } // namespace Rust diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h index 429b9e1..b501a69 100644 --- a/gcc/rust/lex/rust-lex.h +++ b/gcc/rust/lex/rust-lex.h @@ -205,6 +205,8 @@ public: std::string get_filename () { return std::string (input.get_filename ()); } private: + void start_line (int current_line, int current_column); + // File for use as input. RAIIFile input; // TODO is this actually required? could just have file storage in InputSource -- cgit v1.1 From 9507bdec30fb7f88a02765f83f29cce52a5cf8ad Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 4 Jul 2022 16:56:24 +0100 Subject: Support extern-blocks in ast-dumps This allows us to support really basic expressions and extern blocks. These are used for the hello world version of importing metadata in crates. --- gcc/rust/ast/rust-ast-dump.cc | 87 +++++++++++++++++++++++++++++++++++++++---- gcc/rust/ast/rust-ast-dump.h | 1 + 2 files changed, 81 insertions(+), 7 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 6bf2bee..a06dd72 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -52,6 +52,12 @@ Dump::go (AST::Crate &crate) } void +Dump::go (AST::Item &item) +{ + item.accept_vis (*this); +} + +void Dump::format_function_param (FunctionParam ¶m) { param.get_pattern ()->accept_vis (*this); @@ -73,7 +79,9 @@ Dump::visit (AttrInputMetaItemContainer &input) void Dump::visit (IdentifierExpr &ident_expr) -{} +{ + stream << ident_expr.get_ident (); +} void Dump::visit (Lifetime &lifetime) @@ -121,7 +129,9 @@ Dump::visit (QualifiedPathInType &path) // rust-expr.h void Dump::visit (LiteralExpr &expr) -{} +{ + stream << expr.as_string (); +} void Dump::visit (AttrInputLiteral &attr_input) @@ -153,7 +163,24 @@ Dump::visit (NegationExpr &expr) void Dump::visit (ArithmeticOrLogicalExpr &expr) -{} +{ + expr.get_left_expr ()->accept_vis (*this); + stream << " "; + + switch (expr.get_expr_type ()) + { + case ArithmeticOrLogicalOperator::ADD: + stream << "+"; + break; + + default: + gcc_unreachable (); + break; + } + + stream << " "; + expr.get_right_expr ()->accept_vis (*this); +} void Dump::visit (ComparisonExpr &expr) @@ -257,7 +284,10 @@ Dump::visit (BlockExpr &expr) } if (expr.has_tail_expr ()) - expr.get_tail_expr ()->accept_vis (*this); + { + stream << indentation; + expr.get_tail_expr ()->accept_vis (*this); + } indentation.decrement (); stream << "\n" << indentation << "}\n"; @@ -649,12 +679,55 @@ Dump::visit (ExternalStaticItem &item) {} void -Dump::visit (ExternalFunctionItem &item) -{} +Dump::visit (ExternalFunctionItem &function) +{ + stream << "fn " << function.get_identifier () << '('; + + for (size_t i = 0; i < function.get_function_params ().size (); i++) + { + auto ¶m = function.get_function_params ().at (i); + bool has_next = (i + 1) < function.get_function_params ().size (); + + stream << param.get_name () << ": "; + param.get_type ()->accept_vis (*this); + + if (has_next) + stream << ", "; + } + + stream << ')'; + if (function.has_return_type ()) + { + stream << "-> "; + function.get_return_type ()->accept_vis (*this); + } +} void Dump::visit (ExternBlock &block) -{} +{ + stream << "extern "; + + if (block.has_abi ()) + { + stream << "\""; + stream << block.get_abi (); + stream << "\" "; + } + + stream << "{\n"; + indentation.increment (); + + for (auto &item : block.get_extern_items ()) + { + stream << indentation; + item->accept_vis (*this); + stream << ";\n"; + } + + indentation.decrement (); + stream << "\n" << indentation << "}\n"; +} // rust-macro.h void diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 51c6f84..d74d887 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -50,6 +50,7 @@ public: * Run the visitor on an entire crate and its items */ void go (AST::Crate &crate); + void go (AST::Item &item); private: std::ostream &stream; -- cgit v1.1 From d99b68560c0de90f1e3b23bdf08f9c01e190534a Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 6 Jul 2022 21:24:02 +0100 Subject: Fix bad ABI from string method causing all rust abi to become C abi --- gcc/rust/util/rust-abi.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/util/rust-abi.cc b/gcc/rust/util/rust-abi.cc index c27e382..4739887 100644 --- a/gcc/rust/util/rust-abi.cc +++ b/gcc/rust/util/rust-abi.cc @@ -22,7 +22,7 @@ Rust::ABI get_abi_from_string (const std::string &abi) { if (abi.compare ("rust") == 0) - return Rust::ABI::C; + return Rust::ABI::RUST; else if (abi.compare ("rust-intrinsic") == 0) return Rust::ABI::INTRINSIC; else if (abi.compare ("C") == 0) -- cgit v1.1