Skip to content

Commit

Permalink
Refactor hir to avoid raw pointers and unneeded fwd
Browse files Browse the repository at this point in the history
Refactor the hir tree files to remove raw pointer usage and most forward
declarations. Move implementation out of headers and split headers into
smaller and more manageable units.

gcc/rust/ChangeLog:

	* Make-lang.in: Add new files.
	* hir/tree/rust-hir-item.h: Move Item definition and remove
	implementations to their corresponding cc file.
	* hir/tree/rust-hir-expr.h: Move implementation to the corresponding
	cc file.
	* hir/tree/rust-hir-path.h: Likewise.
	* hir/tree/rust-hir-pattern.h: Likewise.
	* hir/tree/rust-hir-stmt.h: Likewise.
	* hir/tree/rust-hir-type.h: Likewise.
	* hir/tree/rust-hir-visitor.h: Likewise.
	* hir/tree/rust-hir.h: Likewise.
	* hir/tree/rust-hir.cc (Crate::Crate): Add implementations from Crate
	and remove ConstGenericParam implementations to move them to their
	own file.
	* hir/tree/rust-hir-attrs.h: New file.
	* hir/tree/rust-hir-bound-abstract.h: New file.
	* hir/tree/rust-hir-bound.h: New file.
	* hir/tree/rust-hir-expr-abstract.h: New file.
	* hir/tree/rust-hir-expr.cc: New file.
	* hir/tree/rust-hir-generic-param.cc: New file.
	* hir/tree/rust-hir-generic-param.h: New file.
	* hir/tree/rust-hir-item.cc: New file.
	* hir/tree/rust-hir-literal.h: New file.
	* hir/tree/rust-hir-node.h: New file.
	* hir/tree/rust-hir-path.cc: New file.
	* hir/tree/rust-hir-pattern-abstract.h: New file.
	* hir/tree/rust-hir-simple-path.h: New file.
	* hir/tree/rust-hir-stmt.cc: New file.
	* hir/tree/rust-hir-trait-bound.h: New file.
	* hir/tree/rust-hir-type-abstract.cc: New file.
	* hir/tree/rust-hir-type-abstract.h: New file.
	* hir/tree/rust-hir-type-no-bounds.h: New file.
	* hir/tree/rust-hir-type.cc: New file.
	* hir/tree/rust-hir-visibility.h: New file.
	* hir/tree/rust-hir-visitable.h: New file.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Oct 21, 2024
1 parent 0370883 commit 3cb72c8
Show file tree
Hide file tree
Showing 31 changed files with 5,193 additions and 3,409 deletions.
7 changes: 7 additions & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ GRS_OBJS = \
rust/rust-polonius.o\
rust/rust-hir-dot-operator.o \
rust/rust-hir-path-probe.o \
rust/rust-hir-path.o \
rust/rust-hir-type.o \
rust/rust-hir-expr.o \
rust/rust-hir-type-abstract.o \
rust/rust-hir-item.o \
rust/rust-hir-stmt.o \
rust/rust-hir-generic-param.o \
rust/rust-type-util.o \
rust/rust-coercion.o \
rust/rust-casts.o \
Expand Down
56 changes: 56 additions & 0 deletions gcc/rust/hir/tree/rust-hir-attrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

// Copyright (C) 2020-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// 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/>.

#ifndef RUST_HIR_ATTRS_H
#define RUST_HIR_ATTRS_H

#include "rust-ast.h"

namespace Rust {
namespace HIR {

class WithOuterAttrs
{
protected:
AST::AttrVec outer_attrs;

public:
AST::AttrVec &get_outer_attrs () { return outer_attrs; }
const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }

WithOuterAttrs (AST::AttrVec outer_attrs)
: outer_attrs (std::move (outer_attrs)){};
};

class WithInnerAttrs
{
protected:
AST::AttrVec inner_attrs;

public:
AST::AttrVec get_inner_attrs () const { return inner_attrs; }

WithInnerAttrs (AST::AttrVec inner_attrs)
: inner_attrs (std::move (inner_attrs)){};
};

} // namespace HIR
} // namespace Rust

#endif
65 changes: 65 additions & 0 deletions gcc/rust/hir/tree/rust-hir-bound-abstract.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (C) 2020-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// 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/>.

#ifndef RUST_HIR_BOUND_ABSTRACT_H
#define RUST_HIR_BOUND_ABSTRACT_H

#include "rust-hir-visitable.h"
#include "rust-system.h"
#include "rust-hir-map.h"

namespace Rust {
namespace HIR {

/* Abstract base class representing a type param bound - Lifetime and TraitBound
* extends it */
class TypeParamBound : public FullVisitable
{
public:
using FullVisitable::accept_vis;
enum BoundType
{
LIFETIME,
TRAITBOUND
};

virtual ~TypeParamBound () {}

// Unique pointer custom clone function
std::unique_ptr<TypeParamBound> clone_type_param_bound () const
{
return std::unique_ptr<TypeParamBound> (clone_type_param_bound_impl ());
}

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

virtual Analysis::NodeMapping get_mappings () const = 0;

virtual location_t get_locus () const = 0;

virtual BoundType get_bound_type () const = 0;

protected:
// Clone function implementation as pure virtual method
virtual TypeParamBound *clone_type_param_bound_impl () const = 0;
};

} // namespace HIR
} // namespace Rust

#endif
94 changes: 94 additions & 0 deletions gcc/rust/hir/tree/rust-hir-bound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2020-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// 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/>.

#ifndef RUST_HIR_BOUND_H
#define RUST_HIR_BOUND_H

#include "rust-hir-bound-abstract.h"
#include "rust-common.h"
#include "rust-hir-path.h"

namespace Rust {
namespace HIR {

// Represents a lifetime (and is also a kind of type param bound)
class Lifetime : public TypeParamBound
{
private:
AST::Lifetime::LifetimeType lifetime_type;
std::string lifetime_name;
location_t locus;
Analysis::NodeMapping mappings;

public:
// Constructor
Lifetime (Analysis::NodeMapping mapping, AST::Lifetime::LifetimeType type,
std::string name, location_t locus)
: lifetime_type (type), lifetime_name (std::move (name)), locus (locus),
mappings (mapping)
{}

// Returns true if the lifetime is in an error state.
bool is_error () const
{
return lifetime_type == AST::Lifetime::LifetimeType::NAMED
&& lifetime_name.empty ();
}

static Lifetime error ()
{
return Lifetime (Analysis::NodeMapping::get_error (),
AST::Lifetime::LifetimeType::NAMED, "", UNDEF_LOCATION);
}

std::string as_string () const override;

void accept_vis (HIRFullVisitor &vis) override;

WARN_UNUSED_RESULT const std::string &get_name () const
{
return lifetime_name;
}

AST::Lifetime::LifetimeType get_lifetime_type () const
{
return lifetime_type;
}

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

Analysis::NodeMapping get_mappings () const override final
{
return mappings;
}

BoundType get_bound_type () const final override { return LIFETIME; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Lifetime *clone_type_param_bound_impl () const override
{
return new Lifetime (*this);
}
};

} // namespace HIR
} // namespace Rust

#endif
Loading

0 comments on commit 3cb72c8

Please sign in to comment.