Skip to content

Commit

Permalink
ast: Introduce class hierarchy for lang item paths
Browse files Browse the repository at this point in the history
Create a base Path class which is derived into two children classes for
regular paths and lang item paths. This allows it to hold either the
segments of a fully formed path, or the node ID of a lang-item path.
This is required in order to create these special paths
which do not have segments, and do not necessarily have a canonical
form - they only depend on where the item was defined.

gcc/rust/ChangeLog:

	* ast/rust-ast-full-decls.h (class PathPattern): Rename PathPattern to...
	(class Path): ...Path
	* ast/rust-ast-collector.cc (TokenCollector::visit): Add required methods
	for LangItemPath and RegularPath.
	* ast/rust-ast-collector.h: Likewise.
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
	* ast/rust-ast-visitor.h: Likewise.
	* ast/rust-path.cc (PathPattern::as_string): Likewise.
	(RegularPath::as_string): Likewise.
	(LangItemPath::as_string): Likewise.
	(PathPattern::convert_to_simple_path): Likewise.
	(RegularPath::convert_to_simple_path): Likewise.
	(RegularPath::accept_vis): Likewise.
	(LangItemPath::accept_vis): Likewise.
	(PathInExpression::as_string): Likewise.
	(QualifiedPathInExpression::as_string): Likewise.
	* ast/rust-path.h (class PathPattern): Likewise.
	(class Path): Likewise.
	(class RegularPath): Likewise.
	(class LangItemPath): Likewise.
	(class PathInExpression): Likewise.
	(class QualifiedPathInExpression): Likewise.
	* ast/rust-pattern.h (class PathPattern): Likewise.
	(class Path): Likewise.
	* expand/rust-derive.h: Likewise.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
	* hir/rust-ast-lower-base.h: Likewise.
	* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
	* resolve/rust-ast-resolve-base.h: Likewise.
  • Loading branch information
CohenArthur committed Aug 19, 2024
1 parent 530b612 commit d44051d
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 42 deletions.
14 changes: 14 additions & 0 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "rust-expr.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
#include "rust-system.h"
#include "rust-token.h"

namespace Rust {
Expand Down Expand Up @@ -560,6 +561,19 @@ TokenCollector::visit (PathInExpression &path)
visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
}

void
TokenCollector::visit (RegularPath &path)
{
// FIXME: We probably want to have a proper implementation here, and call this
// function from things like the PathInExpression visitor
}

void
TokenCollector::visit (LangItemPath &path)
{
// TODO: Implement proper token collection for lang item paths
}

void
TokenCollector::visit (TypePathSegment &segment)
{
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class TokenCollector : public ASTVisitor
void visit (PathExprSegment &segment);
void visit (PathIdentSegment &segment);
void visit (PathInExpression &path);
void visit (RegularPath &path);
void visit (LangItemPath &path);
void visit (TypePathSegment &segment);
void visit (TypePathSegmentGeneric &segment);
void visit (TypePathSegmentFunction &segment);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PathIdentSegment;
struct GenericArgsBinding;
struct GenericArgs;
class PathExprSegment;
class PathPattern;
class Path;
class PathInExpression;
class TypePathSegment;
class TypePathSegmentGeneric;
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ DefaultASTVisitor::visit (AST::ConstGenericParam &const_param)
visit (const_param.get_default_value ());
}

void
DefaultASTVisitor::visit (AST::RegularPath &path)
{
for (auto &segment : path.get_segments ())
visit (segment);
}

void
DefaultASTVisitor::visit (AST::LangItemPath &path)
{}

void
DefaultASTVisitor::visit (AST::PathInExpression &path)
{
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rust-ast-full-decls.h"
#include "rust-ast.h"
#include "rust-item.h"
#include "rust-path.h"
#include "rust-system.h"

namespace Rust {
Expand Down Expand Up @@ -58,6 +59,8 @@ class ASTVisitor
// virtual void visit(TraitImplItem& trait_impl_item) = 0;

// rust-path.h
virtual void visit (RegularPath &path) = 0;
virtual void visit (LangItemPath &path) = 0;
virtual void visit (PathInExpression &path) = 0;
virtual void visit (TypePathSegment &segment) = 0;
virtual void visit (TypePathSegmentGeneric &segment) = 0;
Expand Down Expand Up @@ -249,6 +252,8 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::Lifetime &lifetime) override;
virtual void visit (AST::LifetimeParam &lifetime_param) override;
virtual void visit (AST::ConstGenericParam &const_param) override;
virtual void visit (AST::RegularPath &path) override;
virtual void visit (AST::LangItemPath &path) override;
virtual void visit (AST::PathInExpression &path) override;
virtual void visit (AST::TypePathSegment &segment) override;
virtual void visit (AST::TypePathSegmentGeneric &segment) override;
Expand Down
28 changes: 24 additions & 4 deletions gcc/rust/ast/rust-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */

#include "rust-path.h"
#include "rust-system.h"
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ PathExprSegment::as_string () const
}

std::string
PathPattern::as_string () const
RegularPath::as_string () const
{
std::string str;

Expand All @@ -148,8 +149,15 @@ PathPattern::as_string () const
return str;
}

std::string
LangItemPath::as_string () const
{
// FIXME: Handle #[lang] paths
rust_unreachable ();
}

SimplePath
PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const
RegularPath::convert_to_simple_path (bool with_opening_scope_resolution) const
{
if (!has_segments ())
return SimplePath::create_empty ();
Expand Down Expand Up @@ -183,6 +191,18 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const
locus);
}

void
RegularPath::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
LangItemPath::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
PathInExpression::accept_vis (ASTVisitor &vis)
{
Expand All @@ -197,7 +217,7 @@ PathInExpression::as_string () const
if (has_opening_scope_resolution)
str = "::";

return str + PathPattern::as_string ();
return str + path->as_string ();
}

std::string
Expand Down Expand Up @@ -297,7 +317,7 @@ TypePathFunction::as_string () const
std::string
QualifiedPathInExpression::as_string () const
{
return path_type.as_string () + "::" + PathPattern::as_string ();
return path_type.as_string () + "::" + path->as_string ();
}

std::string
Expand Down
Loading

0 comments on commit d44051d

Please sign in to comment.