Skip to content

Fix #2667 #2670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Lifetime;
class GenericParam;
class LifetimeParam;
class ConstGenericParam;
class TraitItem;
class AssociatedItem;
struct Crate;
class PathExpr;
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3395,14 +3395,14 @@ Module::process_file_path ()
bool no_candidates_found = !file_mod_found && !dir_mod_found;

if (multiple_candidates_found)
rust_error_at (locus,
rust_error_at (get_locus (),
"two candidates found for module %s: %s.rs and %s%smod.rs",
module_name.as_string ().c_str (),
module_name.as_string ().c_str (),
module_name.as_string ().c_str (), file_separator);

if (no_candidates_found)
rust_error_at (locus, "no candidate found for module %s",
rust_error_at (get_locus (), "no candidate found for module %s",
module_name.as_string ().c_str ());

if (no_candidates_found || multiple_candidates_found)
Expand Down
168 changes: 81 additions & 87 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,66 @@ namespace AST {
class ASTVisitor;
using AttrVec = std::vector<Attribute>;

class Located
{
public:
virtual location_t get_locus () const = 0;
};

class LocatedImpl : virtual public Located
{
private:
location_t locus;

protected:
LocatedImpl (location_t locus) : locus (locus) {}

public:
location_t get_locus () const override final { return locus; }
};

class NodeIdStore
{
NodeId node_id;

public:
NodeIdStore () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {}

NodeId get_node_id () const { return node_id; }

friend class Expr;
};

class Located
{
public:
virtual location_t get_locus () const = 0;
};

class LocatedImpl : virtual public Located
{
private:
location_t locus;

protected:
LocatedImpl (location_t locus) : locus (locus) {}

public:
location_t get_locus () const override final { return locus; }
};

class NodeIdStore
{
NodeId node_id;

public:
NodeIdStore () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {}

NodeId get_node_id () const { return node_id; }

friend class Expr;
};

class Visitable
{
public:
Expand Down Expand Up @@ -346,18 +406,16 @@ class PathSegment
};

// A segment of a simple path without generic or type arguments
class SimplePathSegment : public PathSegment
class SimplePathSegment : public PathSegment, virtual public NodeIdStore
{
std::string segment_name;
location_t locus;
NodeId node_id;

// only allow identifiers, "super", "self", "crate", or "$crate"
public:
// TODO: put checks in constructor to enforce this rule?
SimplePathSegment (std::string segment_name, location_t locus)
: segment_name (std::move (segment_name)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
: segment_name (std::move (segment_name)), locus (locus)
{}

/* Returns whether simple path segment is in an invalid state (currently, if
Expand All @@ -373,7 +431,6 @@ class SimplePathSegment : public PathSegment
std::string as_string () const override;

location_t get_locus () const { return locus; }
NodeId get_node_id () const { return node_id; }
const std::string &get_segment_name () const { return segment_name; }
bool is_super_path_seg () const
{
Expand All @@ -394,28 +451,25 @@ class SimplePathSegment : public PathSegment
};

// A simple path without generic or type arguments
class SimplePath
class SimplePath : public NodeIdStore
{
bool opening_scope_resolution;
std::vector<SimplePathSegment> segments;
location_t locus;
NodeId node_id;

public:
// Constructor
SimplePath (std::vector<SimplePathSegment> path_segments,
bool has_opening_scope_resolution = false,
location_t locus = UNDEF_LOCATION)
: opening_scope_resolution (has_opening_scope_resolution),
segments (std::move (path_segments)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
segments (std::move (path_segments)), locus (locus)
{}

SimplePath (Identifier ident)
: opening_scope_resolution (false),
segments ({SimplePathSegment (ident.as_string (), ident.get_locus ())}),
locus (ident.get_locus ()),
node_id (Analysis::Mappings::get ().get_next_node_id ())
locus (ident.get_locus ())
{}

// Creates an empty SimplePath.
Expand All @@ -435,7 +489,6 @@ class SimplePath
}

location_t get_locus () const { return locus; }
NodeId get_node_id () const { return node_id; }

// does this need visitor if not polymorphic? probably not

Expand Down Expand Up @@ -1077,7 +1130,9 @@ class MetaListNameValueStr;
/* Base statement abstract class. Note that most "statements" are not allowed
* in top-level module scope - only a subclass of statements called "items"
* are. */
class Stmt : public Visitable
class Stmt : public Visitable,
virtual public Located,
virtual public NodeIdStore
{
public:
enum class Kind
Expand All @@ -1099,11 +1154,8 @@ class Stmt : public Visitable

virtual std::string as_string () const = 0;

virtual location_t get_locus () const = 0;

virtual void mark_for_strip () = 0;
virtual bool is_marked_for_strip () const = 0;
NodeId get_node_id () const { return node_id; }

virtual Kind get_stmt_kind () = 0;

Expand All @@ -1114,12 +1166,8 @@ class Stmt : public Visitable
virtual void add_semicolon () {}

protected:
Stmt () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}

// Clone function implementation as pure virtual method
virtual Stmt *clone_stmt_impl () const = 0;

NodeId node_id;
};

// Rust "item" AST node (declaration of top-level/module-level allowed stuff)
Expand Down Expand Up @@ -1236,7 +1284,9 @@ class VisItem : public Item
class ExprWithoutBlock;

// Base expression AST node - abstract
class Expr : public Visitable
class Expr : public Visitable,
virtual public Located,
virtual public NodeIdStore
{
public:
enum class Kind
Expand Down Expand Up @@ -1302,8 +1352,6 @@ class Expr : public Visitable

virtual ~Expr () {}

virtual location_t get_locus () const = 0;

virtual bool is_literal () const { return false; }

// HACK: strictly not needed, but faster than full downcast clone
Expand All @@ -1312,8 +1360,6 @@ class Expr : public Visitable
virtual void mark_for_strip () = 0;
virtual bool is_marked_for_strip () const = 0;

virtual NodeId get_node_id () const { return node_id; }

virtual void set_node_id (NodeId id) { node_id = id; }

virtual std::vector<Attribute> &get_outer_attrs () = 0;
Expand All @@ -1323,13 +1369,8 @@ class Expr : public Visitable
virtual void set_outer_attrs (std::vector<Attribute>) = 0;

protected:
// Constructor
Expr () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}

// Clone function implementation as pure virtual method
virtual Expr *clone_expr_impl () const = 0;

NodeId node_id;
};

// AST node for an expression without an accompanying block - abstract
Expand Down Expand Up @@ -1417,7 +1458,9 @@ class IdentifierExpr : public ExprWithoutBlock
};

// Pattern base AST node
class Pattern : public Visitable
class Pattern : public Visitable,
virtual public Located,
virtual public NodeIdStore
{
public:
enum class Kind
Expand Down Expand Up @@ -1456,9 +1499,6 @@ class Pattern : public Visitable
virtual void mark_for_strip () {}
virtual bool is_marked_for_strip () const { return false; }

virtual location_t get_locus () const = 0;
virtual NodeId get_node_id () const = 0;

protected:
// Clone pattern implementation as pure virtual method
virtual Pattern *clone_pattern_impl () const = 0;
Expand All @@ -1468,7 +1508,9 @@ class Pattern : public Visitable
class TraitBound;

// Base class for types as represented in AST - abstract
class Type : public Visitable
class Type : public Visitable,
virtual public Located,
virtual public NodeIdStore
{
public:
// Unique pointer custom clone function
Expand All @@ -1492,17 +1534,9 @@ class Type : public Visitable
virtual void mark_for_strip () {}
virtual bool is_marked_for_strip () const { return false; }

virtual location_t get_locus () const = 0;

NodeId get_node_id () const { return node_id; }

protected:
Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}

// Clone function implementation as pure virtual method
virtual Type *clone_type_impl () const = 0;

NodeId node_id;
};

// A type without parentheses? - abstract
Expand Down Expand Up @@ -1715,7 +1749,8 @@ class LifetimeParam : public GenericParam
}
};

class AssociatedItem : public Visitable
// Abstract base class for items used within an impl block
class AssociatedItem : public Visitable, virtual public Located
{
protected:
// Clone function implementation as pure virtual method
Expand All @@ -1724,6 +1759,7 @@ class AssociatedItem : public Visitable
public:
virtual ~AssociatedItem () {}

// Unique pointer custom clone function
std::unique_ptr<AssociatedItem> clone_associated_item () const
{
return std::unique_ptr<AssociatedItem> (clone_associated_item_impl ());
Expand All @@ -1733,50 +1769,12 @@ class AssociatedItem : public Visitable

virtual void mark_for_strip () = 0;
virtual bool is_marked_for_strip () const = 0;

virtual location_t get_locus () const = 0;
};

// Item used in trait declarations - abstract base class
class TraitItem : public AssociatedItem
{
protected:
TraitItem (location_t locus)
: node_id (Analysis::Mappings::get ().get_next_node_id ()),
vis (Visibility::create_private ()), locus (locus)
{}

TraitItem (Visibility vis, location_t locus)
: node_id (Analysis::Mappings::get ().get_next_node_id ()), vis (vis),
locus (locus)
{}

// Clone function implementation as pure virtual method
virtual TraitItem *clone_associated_item_impl () const override = 0;

NodeId node_id;
Visibility vis;
location_t locus;

public:
// Unique pointer custom clone function
std::unique_ptr<TraitItem> clone_trait_item () const
{
return std::unique_ptr<TraitItem> (clone_associated_item_impl ());
}

NodeId get_node_id () const { return node_id; }
location_t get_locus () const override { return locus; }
};

// Abstract base class for an item used inside an extern block
class ExternalItem : public Visitable
class ExternalItem : public Visitable, virtual public NodeIdStore
{
public:
ExternalItem () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}

ExternalItem (NodeId node_id) : node_id (node_id) {}

virtual ~ExternalItem () {}

// Unique pointer custom clone function
Expand All @@ -1790,13 +1788,9 @@ class ExternalItem : public Visitable
virtual void mark_for_strip () = 0;
virtual bool is_marked_for_strip () const = 0;

virtual NodeId get_node_id () const { return node_id; }

protected:
// Clone function implementation as pure virtual method
virtual ExternalItem *clone_external_item_impl () const = 0;

NodeId node_id;
};

/* Data structure to store the data used in macro invocations and macro
Expand Down
Loading
Loading