gccrs: Refactor TyVar and TypeBoundPredicates

This extract these helpers into seperate files

Signed-off-by: Philip Herron <herron.philip@googlemail.com>

gcc/rust/ChangeLog:

	* Make-lang.in: update makefile
	* typecheck/rust-tyty.cc (TyVar::TyVar): move to new file
	(TyVar::get_tyty): likewise
	(TyVar::get_implicit_infer_var): likewise
	(TyVar::subst_covariant_var): likewise
	(TyVar::clone): likewise
	(TyVar::monomorphized_clone): likewise
	(TyWithLocation::TyWithLocation): likewise
	* typecheck/rust-tyty.h (class BaseType): cleanup
	(class TypeBoundPredicate): move to its own file
	(class TypeBoundPredicateItem): likewise
	(class TypeBoundsMappings): likewise
	(class TyVar): likewise
	(class TyWithLocation): likewise
	* typecheck/rust-tyty-bounds.h: New file.
	* typecheck/rust-tyty-util.cc: New file.
	* typecheck/rust-tyty-util.h: New file.
This commit is contained in:
Philip Herron
2023-01-12 18:00:52 +00:00
committed by Arthur Cohen
parent c1b1bbbc64
commit 688fccb522
6 changed files with 276 additions and 178 deletions

View File

@@ -115,6 +115,7 @@ GRS_OBJS = \
rust/rust-pub-restricted-visitor.o \
rust/rust-privacy-reporter.o \
rust/rust-tyty.o \
rust/rust-tyty-util.o \
rust/rust-tyty-call.o \
rust/rust-tyctx.o \
rust/rust-tyty-bounds.o \

View File

@@ -0,0 +1,88 @@
// Copyright (C) 2020-2022 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_TYTY_BOUNDS_H
#define RUST_TYTY_BOUNDS_H
#include "rust-location.h"
namespace Rust {
namespace Resolver {
class TraitReference;
class TraitItemReference;
class AssociatedImplTrait;
} // namespace Resolver
namespace TyTy {
class BaseType;
class TypeBoundPredicate;
class TypeBoundPredicateItem
{
public:
TypeBoundPredicateItem (const TypeBoundPredicate *parent,
const Resolver::TraitItemReference *trait_item_ref);
static TypeBoundPredicateItem error ();
bool is_error () const;
BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
const Resolver::TraitItemReference *get_raw_item () const;
bool needs_implementation () const;
const TypeBoundPredicate *get_parent () const;
Location get_locus () const;
private:
const TypeBoundPredicate *parent;
const Resolver::TraitItemReference *trait_item_ref;
};
class TypeBoundsMappings
{
protected:
TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
public:
std::vector<TypeBoundPredicate> &get_specified_bounds ();
const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
size_t num_specified_bounds () const;
std::string raw_bounds_as_string () const;
std::string bounds_as_string () const;
std::string raw_bounds_as_name () const;
protected:
void add_bound (TypeBoundPredicate predicate);
std::vector<TypeBoundPredicate> specified_bounds;
};
} // namespace TyTy
} // namespace Rust
#endif // RUST_TYTY_BOUNDS_H

View File

@@ -0,0 +1,116 @@
// Copyright (C) 2020-2022 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/>.
#include "rust-hir-type-check.h"
#include "rust-tyty.h"
namespace Rust {
namespace TyTy {
TyVar::TyVar (HirId ref) : ref (ref)
{
// ensure this reference is defined within the context
auto context = Resolver::TypeCheckContext::get ();
BaseType *lookup = nullptr;
bool ok = context->lookup_type (ref, &lookup);
rust_assert (ok);
}
BaseType *
TyVar::get_tyty () const
{
auto context = Resolver::TypeCheckContext::get ();
BaseType *lookup = nullptr;
bool ok = context->lookup_type (ref, &lookup);
rust_assert (ok);
return lookup;
}
TyVar
TyVar::get_implicit_infer_var (Location locus)
{
auto mappings = Analysis::Mappings::get ();
auto context = Resolver::TypeCheckContext::get ();
InferType *infer = new InferType (mappings->get_next_hir_id (),
InferType::InferTypeKind::GENERAL, locus);
context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
UNKNOWN_NODEID,
infer->get_ref (),
UNKNOWN_LOCAL_DEFID),
infer);
mappings->insert_location (infer->get_ref (), locus);
return TyVar (infer->get_ref ());
}
TyVar
TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
{
if (orig->get_kind () != TyTy::TypeKind::PARAM)
return TyVar (subst->get_ty_ref ());
else if (subst->get_kind () == TyTy::TypeKind::PARAM)
{
TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
{
return TyVar (subst->get_ty_ref ());
}
}
return TyVar (subst->get_ref ());
}
TyVar
TyVar::clone () const
{
TyTy::BaseType *c = get_tyty ()->clone ();
return TyVar (c->get_ref ());
}
TyVar
TyVar::monomorphized_clone () const
{
auto mappings = Analysis::Mappings::get ();
auto context = Resolver::TypeCheckContext::get ();
// this needs a new hirid
TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
c->set_ref (mappings->get_next_hir_id ());
// insert it
context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
UNKNOWN_NODEID, c->get_ref (),
UNKNOWN_LOCAL_DEFID),
c);
return TyVar (c->get_ref ());
}
TyWithLocation::TyWithLocation (BaseType *ty, Location locus)
: ty (ty), locus (locus)
{}
TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
{
auto mappings = Analysis::Mappings::get ();
locus = mappings->lookup_location (ty->get_ref ());
}
} // namespace TyTy
} // namespace Rust

View File

@@ -0,0 +1,69 @@
// Copyright (C) 2020-2022 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_TYTY_UTIL_H
#define RUST_TYTY_UTIL_H
#include "rust-hir-map.h"
namespace Rust {
namespace TyTy {
class BaseType;
// this is a placeholder for types that can change like inference variables
class TyVar
{
public:
explicit TyVar (HirId ref);
HirId get_ref () const { return ref; }
BaseType *get_tyty () const;
TyVar clone () const;
TyVar monomorphized_clone () const;
static TyVar get_implicit_infer_var (Location locus);
static TyVar subst_covariant_var (TyTy::BaseType *orig,
TyTy::BaseType *subst);
private:
HirId ref;
};
class TyWithLocation
{
public:
explicit TyWithLocation (BaseType *ty, Location locus);
explicit TyWithLocation (BaseType *ty);
BaseType *get_ty () const { return ty; }
Location get_locus () const { return locus; }
private:
BaseType *ty;
Location locus;
};
} // namespace TyTy
} // namespace Rust
#endif // RUST_TYTY_UTIL_H

View File

@@ -330,96 +330,6 @@ BaseType::debug () const
debug_str ().c_str ());
}
TyVar::TyVar (HirId ref) : ref (ref)
{
// ensure this reference is defined within the context
auto context = Resolver::TypeCheckContext::get ();
BaseType *lookup = nullptr;
bool ok = context->lookup_type (ref, &lookup);
rust_assert (ok);
}
BaseType *
TyVar::get_tyty () const
{
auto context = Resolver::TypeCheckContext::get ();
BaseType *lookup = nullptr;
bool ok = context->lookup_type (ref, &lookup);
rust_assert (ok);
return lookup;
}
TyVar
TyVar::get_implicit_infer_var (Location locus)
{
auto mappings = Analysis::Mappings::get ();
auto context = Resolver::TypeCheckContext::get ();
InferType *infer = new InferType (mappings->get_next_hir_id (),
InferType::InferTypeKind::GENERAL, locus);
context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
UNKNOWN_NODEID,
infer->get_ref (),
UNKNOWN_LOCAL_DEFID),
infer);
mappings->insert_location (infer->get_ref (), locus);
return TyVar (infer->get_ref ());
}
TyVar
TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
{
if (orig->get_kind () != TyTy::TypeKind::PARAM)
return TyVar (subst->get_ty_ref ());
else if (subst->get_kind () == TyTy::TypeKind::PARAM)
{
TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
{
return TyVar (subst->get_ty_ref ());
}
}
return TyVar (subst->get_ref ());
}
TyVar
TyVar::clone () const
{
TyTy::BaseType *c = get_tyty ()->clone ();
return TyVar (c->get_ref ());
}
TyVar
TyVar::monomorphized_clone () const
{
auto mappings = Analysis::Mappings::get ();
auto context = Resolver::TypeCheckContext::get ();
// this needs a new hirid
TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
c->set_ref (mappings->get_next_hir_id ());
// insert it
context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
UNKNOWN_NODEID, c->get_ref (),
UNKNOWN_LOCAL_DEFID),
c);
return TyVar (c->get_ref ());
}
TyWithLocation::TyWithLocation (BaseType *ty, Location locus)
: ty (ty), locus (locus)
{}
TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
{
auto mappings = Analysis::Mappings::get ();
locus = mappings->lookup_location (ty->get_ref ());
}
void
InferType::accept_vis (TyVisitor &vis)
{

View File

@@ -25,6 +25,8 @@
#include "rust-abi.h"
#include "rust-common.h"
#include "rust-identifier.h"
#include "rust-tyty-bounds.h"
#include "rust-tyty-util.h"
namespace Rust {
@@ -76,57 +78,6 @@ public:
static std::string to_string (TypeKind kind);
};
class BaseType;
class TypeBoundPredicate;
class TypeBoundPredicateItem
{
public:
TypeBoundPredicateItem (const TypeBoundPredicate *parent,
const Resolver::TraitItemReference *trait_item_ref);
static TypeBoundPredicateItem error ();
bool is_error () const;
BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
const Resolver::TraitItemReference *get_raw_item () const;
bool needs_implementation () const;
const TypeBoundPredicate *get_parent () const;
Location get_locus () const;
private:
const TypeBoundPredicate *parent;
const Resolver::TraitItemReference *trait_item_ref;
};
class TypeBoundsMappings
{
protected:
TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
public:
std::vector<TypeBoundPredicate> &get_specified_bounds ();
const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
size_t num_specified_bounds () const;
std::string raw_bounds_as_string () const;
std::string bounds_as_string () const;
std::string raw_bounds_as_name () const;
protected:
void add_bound (TypeBoundPredicate predicate);
std::vector<TypeBoundPredicate> specified_bounds;
};
extern void
set_cmp_autoderef_mode ();
extern void
@@ -268,43 +219,6 @@ protected:
Analysis::Mappings *mappings;
};
// this is a placeholder for types that can change like inference variables
class TyVar
{
public:
explicit TyVar (HirId ref);
HirId get_ref () const { return ref; }
BaseType *get_tyty () const;
TyVar clone () const;
TyVar monomorphized_clone () const;
static TyVar get_implicit_infer_var (Location locus);
static TyVar subst_covariant_var (TyTy::BaseType *orig,
TyTy::BaseType *subst);
private:
HirId ref;
};
class TyWithLocation
{
public:
explicit TyWithLocation (BaseType *ty, Location locus);
explicit TyWithLocation (BaseType *ty);
BaseType *get_ty () const { return ty; }
Location get_locus () const { return locus; }
private:
BaseType *ty;
Location locus;
};
class InferType : public BaseType
{
public: