Skip to content

Commit

Permalink
All - Use NamedFunction, builds
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Nov 17, 2024
1 parent 78a2c77 commit f3e8d50
Show file tree
Hide file tree
Showing 25 changed files with 948 additions and 226 deletions.
1 change: 1 addition & 0 deletions Notes/UpgradeQuirks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,4 @@
- Method lookup with `Arc` reciever
- `async` (functions and blocks)
- References to `Self` in `impl Foo`
- Requires zero-sized functions?
4 changes: 4 additions & 0 deletions src/hir/deserialise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ namespace {
_(StaticString, m_in.read_string() )
_(Const, { box$(deserialise_path()) } )
_(Generic, deserialise_genericref())
_(Function, { box$(deserialise_path()) } )
_(ItemAddr, box$(deserialise_path()) )
#undef _
default:
Expand Down Expand Up @@ -1114,6 +1115,9 @@ namespace {
static_cast< ::HIR::BorrowType>( m_in.read_tag() ),
deserialise_type()
})
_(NamedFunction, {
deserialise_path()
})
_(Function, {
deserialise_genericparams(),
m_in.read_bool(),
Expand Down
3 changes: 3 additions & 0 deletions src/hir/serialise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,9 @@
(Generic,
serialise(e);
),
(Function,
serialise_path(*e.p);
),
(ItemAddr,
serialise_path(*e);
)
Expand Down
108 changes: 106 additions & 2 deletions src/hir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,101 @@ bool ::HIR::TypeData_ErasedType_AliasInner::is_public_to(const HIR::SimplePath&
return true;
}

::HIR::TypeData_FunctionPointer HIR::TypeData::Data_NamedFunction::decay(const Span& sp) const
{
const ::HIR::TypeRef* ty_self = nullptr;
const ::HIR::PathParams* pp_impl = nullptr;
const ::HIR::PathParams* pp_method = nullptr;

TU_MATCH_HDRA( (this->def), { )
TU_ARMA(Function, fp) {
ASSERT_BUG(sp, fp, "Non-initialised NamedFunction definition: " << this->path);
TU_MATCH_HDRA( (this->path.m_data), {)
TU_ARMA(Generic, pe) {
pp_method = &pe.m_params;
}
TU_ARMA(UfcsKnown, pe) {
ty_self = &pe.type;
pp_impl = &pe.trait.m_params;
pp_method = &pe.params;
}
TU_ARMA(UfcsInherent, pe) {
ty_self = &pe.type;
pp_impl = &pe.impl_params;
pp_method = &pe.params;
}
TU_ARMA(UfcsUnknown, pe) {
BUG(sp, "UfcsUnknown seen");
}
}
MonomorphStatePtr ms { ty_self, pp_impl, pp_method };
const auto& f = *fp;
::HIR::TypeData_FunctionPointer ft {
HIR::GenericParams(), // TODO: Get HRLs
f.m_unsafe,
f.m_variadic,
f.m_abi,
ms.monomorph_type(sp, f.m_return),
{}
};
HIR::PathParams method_pp_trimmed;
if( !f.m_params.m_lifetimes.empty() )
{
ft.hrls.m_lifetimes = f.m_params.m_lifetimes;
method_pp_trimmed = ms.pp_method->clone();
method_pp_trimmed.m_lifetimes = std::move(ft.hrls.make_nop_params(3, /*lifetimes_only*/true).m_lifetimes);
ms.pp_method = &method_pp_trimmed;
}
for( const auto& arg : f.m_args )
{
ft.m_arg_types.push_back( ms.monomorph_type(sp, arg.second) );
}
return ft;
}
TU_ARMA(EnumConstructor, ec) {
const auto& e = this->path.m_data.as_Generic();
MonomorphStatePtr ms { nullptr, &e.m_params, nullptr };
auto enum_path = e.m_path; enum_path.m_components.pop_back();
const auto& enm = *ec.e;
ASSERT_BUG(sp, enm.m_data.is_Data(), "Enum " << enum_path << " isn't a data-holding enum");
const auto& var_ty = enm.m_data.as_Data()[ec.v].type;
const auto& str = *var_ty.data().as_Path().binding.as_Struct();
const auto& var_data = str.m_data.as_Tuple();

::HIR::TypeData_FunctionPointer ft {
HIR::GenericParams(), // TODO: Get HRLs
false, false,
ABI_RUST,
::HIR::TypeRef::new_path( ::HIR::GenericPath(mv$(enum_path), e.m_params.clone()), ::HIR::TypePathBinding::make_Enum(&enm) ),
{}
};
for( const auto& arg : var_data )
{
ft.m_arg_types.push_back( ms.monomorph_type(sp, arg.ent) );
}
return ft;
}
TU_ARMA(StructConstructor, p) {
const auto& e = this->path.m_data.as_Generic();
MonomorphStatePtr ms { nullptr, &e.m_params, nullptr };
::HIR::TypeData_FunctionPointer ft {
HIR::GenericParams(), // TODO: Get HRLs
false, false,
ABI_RUST,
::HIR::TypeRef::new_path( this->path.clone(), ::HIR::TypePathBinding::make_Struct(p) ),
{}
};
for( const auto& arg : p->m_data.as_Tuple() )
{
ft.m_arg_types.push_back( ms.monomorph_type(sp, arg.ent) );
}
return ft;
}
}
BUG(sp, "Unreachable code?");
}


void ::HIR::TypeRef::fmt(::std::ostream& os) const
{
if(!m_ptr) {
Expand Down Expand Up @@ -262,7 +357,7 @@ void ::HIR::TypeRef::fmt(::std::ostream& os) const
os << e.inner;
}
TU_ARMA(NamedFunction, e) {
os << "fn{" << e.path <<"}";
os << "fn{" << (e.def.is_Function() && !e.def.as_Function() ? "!" : "") << e.path <<"}";
}
TU_ARMA(Function, e) {
if( !e.hrls.m_lifetimes.empty() ) {
Expand Down Expand Up @@ -674,6 +769,7 @@ ::HIR::Compare HIR::MatchGenerics::cmp_path(const Span& sp, const ::HIR::Path& p
}
}
}
DEBUG("rv = " << rv);
return rv;
}

Expand Down Expand Up @@ -1067,6 +1163,14 @@ const ::HIR::GenericParams* HIR::TypePathBinding::get_generics() const
}
return rv;
}
HIR::TypeData_NamedFunction_Ty HIR::TypeData_NamedFunction_Ty::clone() const {
TU_MATCH_HDRA( (*this), { )
TU_ARMA(Function, e) return e;
TU_ARMA(EnumConstructor, e) return e;
TU_ARMA(StructConstructor, e) return e;
}
throw "";
}

::HIR::TypeRef HIR::TypeRef::clone() const
{
Expand Down Expand Up @@ -1149,7 +1253,7 @@ ::HIR::TypeRef HIR::TypeRef::clone_shallow() const
return ::HIR::TypeRef( TypeData::make_Pointer({e.type, e.inner.clone()}) );
}
TU_ARMA(NamedFunction, e) {
return ::HIR::TypeRef( TypeData::make_NamedFunction({ e.path.clone(), e.def }) );
return ::HIR::TypeRef( TypeData::make_NamedFunction({ e.path.clone(), e.def.clone() }) );
}
TU_ARMA(Function, e) {
TypeData_FunctionPointer ft {
Expand Down
12 changes: 11 additions & 1 deletion src/hir/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ struct TypeData_FunctionPointer
TypeRef m_rettype;
::std::vector<TypeRef> m_arg_types;
};
TAGGED_UNION_EX(TypeData_NamedFunction_Ty, (), Function, (
(Function, const ::HIR::Function* ),
(EnumConstructor, struct { const ::HIR::Enum* e; size_t v; }),
(StructConstructor, const ::HIR::Struct*)
), (), (), (
TypeData_NamedFunction_Ty clone() const;
)
);

TAGGED_UNION(TypeData, Diverge,
(Infer, struct {
Expand Down Expand Up @@ -233,7 +241,9 @@ TAGGED_UNION(TypeData, Diverge,
}),
(NamedFunction, struct {
::HIR::Path path;
const ::HIR::Function* def; // Function definition, used to decay to a pointer, populated by bind
TypeData_NamedFunction_Ty def;

TypeData_FunctionPointer decay(const Span& sp) const;
}),
(Function, TypeData_FunctionPointer), // TODO: Pointer wrap, this is quite large
(Closure, struct {
Expand Down
Loading

0 comments on commit f3e8d50

Please sign in to comment.