Skip to content

Commit

Permalink
gccrs: Add location info to Rust::Identifier
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-ast.h: Modify constructors of `Rust::Identifier`
	* ast/rust-pattern.h: Likewise.
	* hir/tree/rust-hir-item.h: Likewise.
	* parse/rust-parse-impl.h (Parser::parse_macro_rules_def): Likewise.
	(Parser::parse_decl_macro_def): Likewise.
	(Parser::parse_macro_match_fragment): Likewise.
	(Parser::parse_module): Likewise.
	(Parser::parse_use_tree): Likewise.
	(Parser::parse_function): Likewise.
	(Parser::parse_type_param): Likewise.
	(Parser::parse_type_alias): Likewise.
	(Parser::parse_struct): Likewise.
	(Parser::parse_struct_field): Likewise.
	(Parser::parse_enum): Likewise.
	(Parser::parse_enum_item): Likewise.
	(Parser::parse_union): Likewise.
	(Parser::parse_static_item): Likewise.
	(Parser::parse_trait): Likewise.
	(Parser::parse_trait_item): Likewise.
	(Parser::parse_trait_type): Likewise.
	(Parser::parse_trait_const): Likewise.
	(Parser::parse_external_item): Likewise.
	(Parser::parse_generic_args_binding): Likewise.
	(Parser::parse_method): Likewise.
	(Parser::parse_maybe_named_param): Likewise.
	(Parser::parse_identifier_pattern): Likewise.
	(Parser::parse_struct_expr_field): Likewise.
	(ResolveItem::visit): Likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit):Add comments
	* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise.
	* checks/lints/rust-lint-scan-deadcode.h: Fix error location

Signed-off-by: Raiki Tamura <[email protected]>
  • Loading branch information
tamaroning authored and philberty committed Jul 6, 2023
1 parent 0f898a4 commit 5d8f78c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 45 deletions.
21 changes: 18 additions & 3 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,13 +58,15 @@ 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 (); }

private:
std::string ident;
NodeId node_id;
Location loc;
};

std::ostream &
Expand Down Expand Up @@ -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<Attribute> &get_outer_attrs () const { return outer_attrs; }
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
5 changes: 3 additions & 2 deletions gcc/rust/checks/lints/rust-lint-scan-deadcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ 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 ());
}
}
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 ());
}
}
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
75 changes: 37 additions & 38 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ Parser<ManagedTokenSource>::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.");
Expand Down Expand Up @@ -1596,7 +1596,7 @@ Parser<ManagedTokenSource>::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)
Expand Down Expand Up @@ -2160,12 +2160,12 @@ Parser<ManagedTokenSource>::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 ())
{
Expand Down Expand Up @@ -2411,7 +2411,7 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
{
return nullptr;
}
Identifier name{module_name->get_str ()};
Identifier name{module_name};

const_TokenPtr t = lexer.peek_token ();

Expand Down Expand Up @@ -2817,15 +2817,15 @@ Parser<ManagedTokenSource>::parse_use_tree ()

return std::unique_ptr<AST::UseTreeRebind> (
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<AST::UseTreeRebind> (
new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
std::move (path), locus, {"_"}));
std::move (path), locus,
{"_", t->get_locus ()}));
default:
add_error (Error (
t->get_locus (),
Expand Down Expand Up @@ -2882,7 +2882,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -3500,8 +3500,7 @@ Parser<ManagedTokenSource>::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)
Expand Down Expand Up @@ -4131,7 +4130,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -4191,7 +4190,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -4387,7 +4386,7 @@ Parser<ManagedTokenSource>::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))
Expand Down Expand Up @@ -4513,7 +4512,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -4643,7 +4642,7 @@ Parser<ManagedTokenSource>::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 ();
Expand Down Expand Up @@ -4727,7 +4726,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -4845,7 +4844,7 @@ Parser<ManagedTokenSource>::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))
{
Expand Down Expand Up @@ -4906,7 +4905,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -5027,7 +5026,7 @@ Parser<ManagedTokenSource>::parse_trait_item ()
if (ident_tok == nullptr)
return nullptr;

Identifier ident = ident_tok->get_str ();
Identifier ident{ident_tok};

// parse generic params
std::vector<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -5162,7 +5161,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::TypeParamBound>> bounds;

Expand Down Expand Up @@ -5201,7 +5200,7 @@ Parser<ManagedTokenSource>::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))
{
Expand Down Expand Up @@ -5568,7 +5567,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -5763,7 +5762,7 @@ Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
{
return nullptr;
}
Identifier ident = ident_tok->get_str ();
Identifier ident{ident_tok};

// DEBUG:
rust_debug (
Expand Down Expand Up @@ -5995,7 +5994,7 @@ Parser<ManagedTokenSource>::parse_external_item ()
skip_after_semicolon ();
return nullptr;
}
Identifier ident = ident_tok->get_str ();
Identifier ident{ident_tok};

if (!skip_token (COLON))
{
Expand Down Expand Up @@ -6038,7 +6037,7 @@ Parser<ManagedTokenSource>::parse_external_item ()
skip_after_semicolon ();
return nullptr;
}
Identifier ident = ident_tok->get_str ();
Identifier ident{ident_tok};

// parse (optional) generic params
std::vector<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -6620,7 +6619,7 @@ Parser<ManagedTokenSource>::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))
{
Expand Down Expand Up @@ -7189,7 +7188,7 @@ Parser<ManagedTokenSource>::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<std::unique_ptr<AST::GenericParam>> generic_params
Expand Down Expand Up @@ -9659,14 +9658,14 @@ Parser<ManagedTokenSource>::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);
}
Expand Down Expand Up @@ -11093,7 +11092,7 @@ Parser<ManagedTokenSource>::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");
Expand Down Expand Up @@ -11518,7 +11517,7 @@ Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
{
case COLON: {
// identifier-pattern
Identifier ident = t->get_str ();
Identifier ident{t};
lexer.skip_token ();

skip_token (COLON);
Expand All @@ -11543,7 +11542,7 @@ Parser<ManagedTokenSource>::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<AST::StructPatternFieldIdent> (
Expand Down Expand Up @@ -11581,7 +11580,7 @@ Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
{
return nullptr;
}
Identifier ident = ident_tok->get_str ();
Identifier ident{ident_tok};

return std::unique_ptr<AST::StructPatternFieldIdent> (
new AST::StructPatternFieldIdent (std::move (ident), has_ref, has_mut,
Expand Down Expand Up @@ -11821,7 +11820,7 @@ Parser<ManagedTokenSource>::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)
Expand All @@ -11844,7 +11843,7 @@ Parser<ManagedTokenSource>::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<AST::StructExprFieldIdentifier> (
Expand Down Expand Up @@ -13975,7 +13974,7 @@ Parser<ManagedTokenSource>::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 ();

Expand Down
Loading

0 comments on commit 5d8f78c

Please sign in to comment.