Skip to content

Commit

Permalink
hir: Add ExternalTypeItem node
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-item.h (class ExternalTypeItem): New class.
	* hir/tree/rust-hir.cc (ExternalTypeItem::as_string): Likewise.
	* backend/rust-compile-extern.h: Add base for handling HIR::ExternalTypeItem
	node.
	* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
	* checks/errors/borrowck/rust-function-collector.h: Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-const-checker.h: Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.h: Likewise.
	* hir/rust-ast-lower-extern.h: Likewise.
	* hir/rust-hir-dump.cc (Dump::visit): Likewise.
	* hir/rust-hir-dump.h: Likewise.
	* hir/tree/rust-hir-full-decls.h (class ExternalTypeItem): Likewise.
	* hir/tree/rust-hir-visitor.h: Likewise.
	(ExternalTypeItem::accept_vis): Likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): Likewise.
	* typecheck/rust-hir-type-check-implitem.h: Likewise.
  • Loading branch information
CohenArthur committed Feb 29, 2024
1 parent 7b3fa7a commit 8e3c34a
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 3 deletions.
7 changes: 7 additions & 0 deletions gcc/rust/backend/rust-compile-extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "rust-compile-base.h"
#include "rust-compile-intrinsic.h"
#include "rust-compile-type.h"
#include "rust-diagnostics.h"
#include "rust-hir-full-decls.h"

namespace Rust {
namespace Compile {
Expand Down Expand Up @@ -152,6 +154,11 @@ class CompileExternItem : public HIRCompileBase,
reference = address_expression (fndecl, ref_locus);
}

void visit (HIR::ExternalTypeItem &type) override
{
rust_sorry_at (type.get_locus (), "extern types are not supported yet");
}

private:
CompileExternItem (Context *ctx, TyTy::BaseType *concrete,
location_t ref_locus)
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class StructBuilder : public AbstractBuilder, public HIR::HIRFullVisitor
void visit (HIR::ImplBlock &impl) override { rust_unreachable (); }
void visit (HIR::ExternalStaticItem &item) override { rust_unreachable (); }
void visit (HIR::ExternalFunctionItem &item) override { rust_unreachable (); }
void visit (HIR::ExternalTypeItem &item) override { rust_unreachable (); }
void visit (HIR::ExternBlock &block) override { rust_unreachable (); }
void visit (HIR::LiteralPattern &pattern) override { rust_unreachable (); }
void visit (HIR::IdentifierPattern &pattern) override { rust_unreachable (); }
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/borrowck/rust-function-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class FunctionCollector : public HIR::HIRFullVisitor
void visit (HIR::ImplBlock &impl) override {}
void visit (HIR::ExternalStaticItem &item) override {}
void visit (HIR::ExternalFunctionItem &item) override {}
void visit (HIR::ExternalTypeItem &item) override {}
void visit (HIR::ExternBlock &block) override {}
void visit (HIR::LiteralPattern &pattern) override {}
void visit (HIR::IdentifierPattern &pattern) override {}
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/rust-const-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ void
ConstChecker::visit (ExternalFunctionItem &)
{}

void
ConstChecker::visit (ExternalTypeItem &)
{}

void
ConstChecker::visit (ExternBlock &block)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-const-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class ConstChecker : public HIRFullVisitor
virtual void visit (ImplBlock &impl) override;
virtual void visit (ExternalStaticItem &item) override;
virtual void visit (ExternalFunctionItem &item) override;
virtual void visit (ExternalTypeItem &item) override;
virtual void visit (ExternBlock &block) override;
virtual void visit (LiteralPattern &pattern) override;
virtual void visit (IdentifierPattern &pattern) override;
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/rust-unsafe-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ void
UnsafeChecker::visit (ExternalFunctionItem &)
{}

void
UnsafeChecker::visit (ExternalTypeItem &)
{}

void
UnsafeChecker::visit (ExternBlock &block)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-unsafe-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class UnsafeChecker : public HIRFullVisitor
virtual void visit (ImplBlock &impl) override;
virtual void visit (ExternalStaticItem &item) override;
virtual void visit (ExternalFunctionItem &item) override;
virtual void visit (ExternalTypeItem &item) override;
virtual void visit (ExternBlock &block) override;
virtual void visit (LiteralPattern &pattern) override;
virtual void visit (IdentifierPattern &pattern) override;
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/hir/rust-ast-lower-extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class ASTLoweringExternItem : public ASTLoweringBase
function.get_outer_attrs (), function.get_locus ());
}

void visit (AST::ExternalTypeItem &type) override
{
rust_sorry_at (type.get_locus (), "extern types are not implemented yet");
}

private:
ASTLoweringExternItem () : translated (nullptr) {}

Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,16 @@ Dump::visit (ExternalFunctionItem &e)
end ("ExternalFunctionItem");
}

void
Dump::visit (ExternalTypeItem &e)
{
begin ("ExternalTypeItem");

do_externalitem (e);

end ("ExternalTypeItem");
}

void
Dump::visit (ExternBlock &e)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-hir-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class Dump : public HIRFullVisitor

virtual void visit (ExternalStaticItem &) override;
virtual void visit (ExternalFunctionItem &) override;
virtual void visit (ExternalTypeItem &) override;
virtual void visit (ExternBlock &) override;

virtual void visit (LiteralPattern &) override;
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/tree/rust-hir-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class ExternalItem;
class ExternalStaticItem;
struct NamedFunctionParam;
class ExternalFunctionItem;
class ExternalTypeItem;
class ExternBlock;

// rust-pattern.h
Expand Down
39 changes: 36 additions & 3 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,7 @@ class ExternalItem : public Node
{
Static,
Function,
Type,
};

virtual ~ExternalItem () {}
Expand Down Expand Up @@ -3084,11 +3085,13 @@ class ExternalFunctionItem : public ExternalItem

// Copy constructor with clone
ExternalFunctionItem (ExternalFunctionItem const &other)
: ExternalItem (other), return_type (other.return_type->clone_type ()),
where_clause (other.where_clause),
: ExternalItem (other), where_clause (other.where_clause),
function_params (other.function_params),
has_variadics (other.has_variadics)
{
if (other.return_type)
return_type = other.return_type->clone_type ();

generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
Expand All @@ -3098,11 +3101,14 @@ class ExternalFunctionItem : public ExternalItem
ExternalFunctionItem &operator= (ExternalFunctionItem const &other)
{
ExternalItem::operator= (other);
return_type = other.return_type->clone_type ();

where_clause = other.where_clause;
function_params = other.function_params;
has_variadics = other.has_variadics;

if (other.return_type)
return_type = other.return_type->clone_type ();

generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
Expand Down Expand Up @@ -3144,6 +3150,33 @@ class ExternalFunctionItem : public ExternalItem
}
};

class ExternalTypeItem : public ExternalItem
{
ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name,
Visibility vis, AST::AttrVec outer_attrs, location_t locus)
: ExternalItem (std::move (mappings), std::move (item_name),
std::move (vis), std::move (outer_attrs), locus)
{}

ExternalTypeItem (ExternalTypeItem const &other) : ExternalItem (other) {}

ExternalTypeItem (ExternalTypeItem &&other) = default;
ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;

std::string as_string () const override;

void accept_vis (HIRFullVisitor &vis) override;
void accept_vis (HIRExternalItemVisitor &vis) override;

ExternKind get_extern_kind () override { return ExternKind::Type; }

protected:
ExternalTypeItem *clone_external_item_impl () const override
{
return new ExternalTypeItem (*this);
}
};

// An extern block HIR node
class ExternBlock : public VisItem, public WithInnerAttrs
{
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/hir/tree/rust-hir-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class HIRFullVisitor
virtual void visit (ImplBlock &impl) = 0;
virtual void visit (ExternalStaticItem &item) = 0;
virtual void visit (ExternalFunctionItem &item) = 0;
virtual void visit (ExternalTypeItem &item) = 0;
virtual void visit (ExternBlock &block) = 0;
virtual void visit (LiteralPattern &pattern) = 0;
virtual void visit (IdentifierPattern &pattern) = 0;
Expand Down Expand Up @@ -255,6 +256,7 @@ class HIRFullVisitorBase : public HIRFullVisitor

virtual void visit (ExternalStaticItem &) override {}
virtual void visit (ExternalFunctionItem &) override {}
virtual void visit (ExternalTypeItem &) override {}
virtual void visit (ExternBlock &) override {}

virtual void visit (LiteralPattern &) override {}
Expand Down Expand Up @@ -306,6 +308,7 @@ class HIRExternalItemVisitor
public:
virtual void visit (ExternalStaticItem &item) = 0;
virtual void visit (ExternalFunctionItem &item) = 0;
virtual void visit (ExternalTypeItem &item) = 0;
};

class HIRTraitItemVisitor
Expand Down
25 changes: 25 additions & 0 deletions gcc/rust/hir/tree/rust-hir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,19 @@ ExternalFunctionItem::as_string () const
return str;
}

std::string
ExternalTypeItem::as_string () const
{
std::string str = ExternalItem::as_string ();

str += "type ";

// add name
str += get_item_name ().as_string ();

return str;
}

std::string
NamedFunctionParam::as_string () const
{
Expand Down Expand Up @@ -4272,6 +4285,12 @@ ExternalFunctionItem::accept_vis (HIRFullVisitor &vis)
vis.visit (*this);
}

void
ExternalTypeItem::accept_vis (HIRFullVisitor &vis)
{
vis.visit (*this);
}

void
ExternBlock::accept_vis (HIRFullVisitor &vis)
{
Expand Down Expand Up @@ -4542,6 +4561,12 @@ ExternalFunctionItem::accept_vis (HIRExternalItemVisitor &vis)
vis.visit (*this);
}

void
ExternalTypeItem::accept_vis (HIRExternalItemVisitor &vis)
{
vis.visit (*this);
}

void
ExternalStaticItem::accept_vis (HIRExternalItemVisitor &vis)
{
Expand Down
Loading

0 comments on commit 8e3c34a

Please sign in to comment.