diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 4eaa39d4aa58..c7940bdd1106 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -35,8 +35,21 @@ struct MacroExpander; class Identifier { public: - Identifier (std::string ident = "") - : ident (ident), node_id (Analysis::Mappings::get ()->get_next_node_id ()) + // Create dummy identifier + Identifier () + : ident (""), node_id (Analysis::Mappings::get ()->get_next_node_id ()), + loc (Location ()) + {} + // Create identifier with dummy location + Identifier (std::string ident, Location loc = Location ()) + : ident (ident), node_id (Analysis::Mappings::get ()->get_next_node_id ()), + loc (loc) + {} + // Create identifier from token + Identifier (const_TokenPtr token) + : ident (token->get_str ()), + node_id (Analysis::Mappings::get ()->get_next_node_id ()), + loc (token->get_locus ()) {} Identifier (const Identifier &) = default; @@ -45,6 +58,7 @@ class Identifier Identifier &operator= (Identifier &&) = default; NodeId get_node_id () const { return node_id; } + Location get_locus () const { return loc; } const std::string &as_string () const { return ident; } bool empty () const { return ident.empty (); } @@ -52,6 +66,7 @@ class Identifier private: std::string ident; NodeId node_id; + Location loc; }; std::ostream & @@ -1099,7 +1114,7 @@ class IdentifierExpr : public ExprWithoutBlock } // "Error state" if ident is empty, so base stripping on this. - void mark_for_strip () override { ident = {}; } + void mark_for_strip () override { ident = {""}; } bool is_marked_for_strip () const override { return ident.empty (); } const std::vector &get_outer_attrs () const { return outer_attrs; } diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index f854563b243d..3bb5d877b9a9 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -772,7 +772,7 @@ class StructPatternFieldIdent : public StructPatternField void accept_vis (ASTVisitor &vis) override; // based on idea of identifier no longer existing - void mark_for_strip () override { ident = {}; } + void mark_for_strip () override { ident = {""}; } bool is_marked_for_strip () const override { return ident.empty (); } const Identifier &get_identifier () const { return ident; } diff --git a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h index 97d2dde3b5de..b96a3a521396 100644 --- a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h +++ b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h @@ -60,7 +60,7 @@ class ScanDeadcode : public MarkLiveBase if (!implBlock->has_trait_ref ()) { rust_warning_at ( - function.get_locus (), 0, + function.get_function_name ().get_locus (), 0, "associated function is never used: %<%s%>", function.get_function_name ().as_string ().c_str ()); } @@ -68,7 +68,8 @@ class ScanDeadcode : public MarkLiveBase else { rust_warning_at ( - function.get_locus (), 0, "function is never used: %<%s%>", + function.get_function_name ().get_locus (), 0, + "function is never used: %<%s%>", function.get_function_name ().as_string ().c_str ()); } } diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 22d76ce53fa2..b65d5e3a53b9 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -687,7 +687,7 @@ class Module : public VisItem, public WithInnerAttrs // Copy constructor with vector clone Module (Module const &other) - : VisItem (other), WithInnerAttrs (other.inner_attrs) + : VisItem (other), WithInnerAttrs (other.inner_attrs), module_name ("") { items.reserve (other.items.size ()); for (const auto &e : other.items) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index ad52824c1712..dc96cd8f7e42 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1439,7 +1439,7 @@ Parser::parse_macro_rules_def (AST::AttrVec outer_attrs) { return nullptr; } - Identifier rule_name = ident_tok->get_str (); + Identifier rule_name{ident_tok}; // DEBUG rust_debug ("in macro rules def, about to parse parens."); @@ -1596,7 +1596,7 @@ Parser::parse_decl_macro_def (AST::Visibility vis, { return nullptr; } - Identifier rule_name = ident_tok->get_str (); + Identifier rule_name{ident_tok}; t = lexer.peek_token (); if (t->get_id () == LEFT_PAREN) @@ -2160,12 +2160,12 @@ Parser::parse_macro_match_fragment () Location fragment_locus = lexer.peek_token ()->get_locus (); skip_token (DOLLAR_SIGN); - Identifier ident{""}; + Identifier ident; auto identifier = lexer.peek_token (); if (identifier->get_id () == UNDERSCORE) - ident = {"_"}; + ident = {"_", identifier->get_locus ()}; else - ident = identifier->get_str (); + ident = {identifier}; if (ident.empty ()) { @@ -2411,7 +2411,7 @@ Parser::parse_module (AST::Visibility vis, { return nullptr; } - Identifier name{module_name->get_str ()}; + Identifier name{module_name}; const_TokenPtr t = lexer.peek_token (); @@ -2817,15 +2817,15 @@ Parser::parse_use_tree () return std::unique_ptr ( new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER, - std::move (path), locus, - t->get_str ())); + std::move (path), locus, t)); case UNDERSCORE: // skip lexer token lexer.skip_token (); return std::unique_ptr ( new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD, - std::move (path), locus, {"_"})); + std::move (path), locus, + {"_", t->get_locus ()})); default: add_error (Error ( t->get_locus (), @@ -2882,7 +2882,7 @@ Parser::parse_function (AST::Visibility vis, skip_after_next_block (); return nullptr; } - Identifier function_name = function_name_tok->get_str (); + Identifier function_name{function_name_tok}; // parse generic params - if exist std::vector> generic_params @@ -3500,8 +3500,7 @@ Parser::parse_type_param () // identifier return nullptr; } - // TODO: create identifier from identifier token - Identifier ident = identifier_tok->get_str (); + Identifier ident{identifier_tok}; lexer.skip_token (); // parse type param bounds (if they exist) @@ -4131,7 +4130,7 @@ Parser::parse_type_alias (AST::Visibility vis, skip_after_semicolon (); return nullptr; } - Identifier alias_name = alias_name_tok->get_str (); + Identifier alias_name{alias_name_tok}; // parse generic params, which may not exist std::vector> generic_params @@ -4191,7 +4190,7 @@ Parser::parse_struct (AST::Visibility vis, // skip after somewhere? return nullptr; } - Identifier struct_name = name_tok->get_str (); + Identifier struct_name{name_tok}; // parse generic params, which may or may not exist std::vector> generic_params @@ -4387,7 +4386,7 @@ Parser::parse_struct_field () // necessarily error return AST::StructField::create_error (); } - Identifier field_name = field_name_tok->get_str (); + Identifier field_name{field_name_tok}; lexer.skip_token (); if (!skip_token (COLON)) @@ -4513,7 +4512,7 @@ Parser::parse_enum (AST::Visibility vis, if (enum_name_tok == nullptr) return nullptr; - Identifier enum_name = enum_name_tok->get_str (); + Identifier enum_name = {enum_name_tok}; // parse generic params (of enum container, not enum variants) if they exist std::vector> generic_params @@ -4643,7 +4642,7 @@ Parser::parse_enum_item () return nullptr; } lexer.skip_token (); - Identifier item_name = item_name_tok->get_str (); + Identifier item_name{item_name_tok}; // branch based on next token const_TokenPtr t = lexer.peek_token (); @@ -4727,7 +4726,7 @@ Parser::parse_union (AST::Visibility vis, skip_after_next_block (); return nullptr; } - Identifier union_name = union_name_tok->get_str (); + Identifier union_name{union_name_tok}; // parse optional generic parameters std::vector> generic_params @@ -4845,7 +4844,7 @@ Parser::parse_static_item (AST::Visibility vis, if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; if (!skip_token (COLON)) { @@ -4906,7 +4905,7 @@ Parser::parse_trait (AST::Visibility vis, if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // parse generic parameters (if they exist) std::vector> generic_params @@ -5027,7 +5026,7 @@ Parser::parse_trait_item () if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // parse generic params std::vector> generic_params @@ -5162,7 +5161,7 @@ Parser::parse_trait_type (AST::AttrVec outer_attrs) if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; std::vector> bounds; @@ -5201,7 +5200,7 @@ Parser::parse_trait_const (AST::AttrVec outer_attrs) if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; if (!skip_token (COLON)) { @@ -5568,7 +5567,7 @@ Parser::parse_inherent_impl_function_or_method ( if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // parse generic params std::vector> generic_params @@ -5763,7 +5762,7 @@ Parser::parse_trait_impl_function_or_method ( { return nullptr; } - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // DEBUG: rust_debug ( @@ -5995,7 +5994,7 @@ Parser::parse_external_item () skip_after_semicolon (); return nullptr; } - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; if (!skip_token (COLON)) { @@ -6038,7 +6037,7 @@ Parser::parse_external_item () skip_after_semicolon (); return nullptr; } - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // parse (optional) generic params std::vector> generic_params @@ -6620,7 +6619,7 @@ Parser::parse_generic_args_binding () return AST::GenericArgsBinding::create_error (); } lexer.skip_token (); - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; if (!skip_token (EQUAL)) { @@ -7189,7 +7188,7 @@ Parser::parse_method () skip_after_next_block (); return AST::Method::create_error (); } - Identifier method_name = ident_tok->get_str (); + Identifier method_name{ident_tok}; // parse generic params - if exist std::vector> generic_params @@ -9659,14 +9658,14 @@ Parser::parse_maybe_named_param (AST::AttrVec outer_attrs) if (current->get_id () == IDENTIFIER && next->get_id () == COLON) { // named param - name = current->get_str (); + name = {current}; kind = AST::MaybeNamedParam::IDENTIFIER; lexer.skip_token (1); } else if (current->get_id () == UNDERSCORE && next->get_id () == COLON) { // wildcard param - name = {"_"}; + name = {"_", current->get_locus ()}; kind = AST::MaybeNamedParam::WILDCARD; lexer.skip_token (1); } @@ -11093,7 +11092,7 @@ Parser::parse_identifier_pattern () // skip somewhere? return nullptr; } - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; // DEBUG rust_debug ("parsed identifier in identifier pattern"); @@ -11518,7 +11517,7 @@ Parser::parse_struct_pattern_field_partial ( { case COLON: { // identifier-pattern - Identifier ident = t->get_str (); + Identifier ident{t}; lexer.skip_token (); skip_token (COLON); @@ -11543,7 +11542,7 @@ Parser::parse_struct_pattern_field_partial ( case COMMA: case RIGHT_CURLY: { // identifier only - Identifier ident = t->get_str (); + Identifier ident = {t}; lexer.skip_token (); return std::unique_ptr ( @@ -11581,7 +11580,7 @@ Parser::parse_struct_pattern_field_partial ( { return nullptr; } - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; return std::unique_ptr ( new AST::StructPatternFieldIdent (std::move (ident), has_ref, has_mut, @@ -11821,7 +11820,7 @@ Parser::parse_struct_expr_field () if (lexer.peek_token (1)->get_id () == COLON) { // struct expr field with identifier and expr - Identifier ident = t->get_str (); + Identifier ident = {t}; lexer.skip_token (1); // parse expression (required) @@ -11844,7 +11843,7 @@ Parser::parse_struct_expr_field () else { // struct expr field with identifier only - Identifier ident = t->get_str (); + Identifier ident{t}; lexer.skip_token (); return std::unique_ptr ( @@ -13975,7 +13974,7 @@ Parser::parse_field_access_expr ( if (ident_tok == nullptr) return nullptr; - Identifier ident = ident_tok->get_str (); + Identifier ident{ident_tok}; Location locus = struct_expr->get_locus (); diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index 99752bdf0fa7..9956621ece9b 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -134,6 +134,7 @@ ResolveTraitItems::visit (AST::TraitItemMethod &func) // self turns into (self: Self) as a function param AST::SelfParam &self_param = function.get_self_param (); + // FIXME: which location should be used for Rust::Identifier `self`? AST::IdentifierPattern self_pattern (self_param.get_node_id (), {"self"}, self_param.get_locus (), self_param.get_has_ref (), @@ -648,6 +649,7 @@ ResolveItem::visit (AST::Method &method) // self turns into (self: Self) as a function param AST::SelfParam &self_param = method.get_self_param (); + // FIXME: which location should be used for Rust::Identifier `self`? AST::IdentifierPattern self_pattern (self_param.get_node_id (), {"self"}, self_param.get_locus (), self_param.get_has_ref (), @@ -824,6 +826,7 @@ ResolveItem::visit (AST::Trait &trait) resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); // we need to inject an implicit self TypeParam here + // FIXME: which location should be used for Rust::Identifier `Self`? AST::TypeParam *implicit_self = new AST::TypeParam ({"Self"}, trait.get_locus ()); trait.insert_implict_self ( diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 2631f48de848..0bc5cb138290 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -225,6 +225,7 @@ TypeCheckImplItem::visit (HIR::Function &function) // compilation to know parameter names. The types are ignored but we // reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self_param (); + // FIXME: which location should be used for Rust::Identifier for `self`? HIR::IdentifierPattern *self_pattern = new HIR::IdentifierPattern ( mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), self_param.get_mut (), std::unique_ptr (nullptr));