Skip to content

Commit 9b8ffd2

Browse files
committed
Add flags for FnType FNTYPE_IS_METHOD_FLAG FNTYPE_IS_EXTERN_FLAG
1 parent 42de5f9 commit 9b8ffd2

File tree

4 files changed

+125
-16
lines changed

4 files changed

+125
-16
lines changed

gcc/rust/typecheck/rust-hir-type-check-implitem.h

+100-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,101 @@
2828
namespace Rust {
2929
namespace Resolver {
3030

31+
class TypeCheckTopLevelExternItem : public TypeCheckBase
32+
{
33+
using Rust::Resolver::TypeCheckBase::visit;
34+
35+
public:
36+
static void Resolve (HIR::ExternalItem *item)
37+
{
38+
TypeCheckTopLevelExternItem resolver;
39+
item->accept_vis (resolver);
40+
}
41+
42+
void visit (HIR::ExternalStaticItem &item) override
43+
{
44+
TyTy::BaseType *actual_type
45+
= TypeCheckType::Resolve (item.get_item_type ().get ());
46+
47+
context->insert_type (item.get_mappings (), actual_type);
48+
}
49+
50+
void visit (HIR::ExternalFunctionItem &function) override
51+
{
52+
std::vector<TyTy::SubstitutionParamMapping> substitutions;
53+
if (function.has_generics ())
54+
{
55+
for (auto &generic_param : function.get_generic_params ())
56+
{
57+
switch (generic_param.get ()->get_kind ())
58+
{
59+
case HIR::GenericParam::GenericKind::LIFETIME:
60+
// Skipping Lifetime completely until better handling.
61+
break;
62+
63+
case HIR::GenericParam::GenericKind::TYPE: {
64+
auto param_type
65+
= TypeResolveGenericParam::Resolve (generic_param.get ());
66+
context->insert_type (generic_param->get_mappings (),
67+
param_type);
68+
69+
substitutions.push_back (TyTy::SubstitutionParamMapping (
70+
static_cast<HIR::TypeParam &> (*generic_param),
71+
param_type));
72+
}
73+
break;
74+
}
75+
}
76+
}
77+
78+
TyTy::BaseType *ret_type = nullptr;
79+
if (!function.has_return_type ())
80+
ret_type = new TyTy::TupleType (function.get_mappings ().get_hirid ());
81+
else
82+
{
83+
auto resolved
84+
= TypeCheckType::Resolve (function.get_return_type ().get ());
85+
if (resolved == nullptr)
86+
{
87+
rust_error_at (function.get_locus (),
88+
"failed to resolve return type");
89+
return;
90+
}
91+
92+
ret_type = resolved->clone ();
93+
ret_type->set_ref (
94+
function.get_return_type ()->get_mappings ().get_hirid ());
95+
}
96+
97+
std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
98+
for (auto &param : function.get_function_params ())
99+
{
100+
// get the name as well required for later on
101+
auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ());
102+
103+
HIR::IdentifierPattern *param_pattern = new HIR::IdentifierPattern (
104+
param.get_param_name (), Location (), false, false,
105+
std::unique_ptr<HIR::Pattern> (nullptr));
106+
107+
params.push_back (
108+
std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern,
109+
param_tyty));
110+
111+
context->insert_type (param.get_mappings (), param_tyty);
112+
}
113+
114+
auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (),
115+
function.get_mappings ().get_defid (),
116+
function.get_item_name (),
117+
FNTYPE_IS_EXTERN_FLAG, std::move (params),
118+
ret_type, std::move (substitutions));
119+
context->insert_type (function.get_mappings (), fnType);
120+
}
121+
122+
private:
123+
TypeCheckTopLevelExternItem () : TypeCheckBase () {}
124+
};
125+
31126
class TypeCheckTopLevelImplItem : public TypeCheckBase
32127
{
33128
using Rust::Resolver::TypeCheckBase::visit;
@@ -134,11 +229,11 @@ class TypeCheckTopLevelImplItem : public TypeCheckBase
134229
context->insert_type (param.get_mappings (), param_tyty);
135230
}
136231

137-
auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (),
138-
function.get_mappings ().get_defid (),
139-
function.get_function_name (),
140-
function.is_method (), std::move (params),
141-
ret_type, std::move (substitutions));
232+
auto fnType = new TyTy::FnType (
233+
function.get_mappings ().get_hirid (),
234+
function.get_mappings ().get_defid (), function.get_function_name (),
235+
function.is_method () ? FNTYPE_IS_METHOD_FLAG : FNTYPE_DEFAULT_FLAGS,
236+
std::move (params), ret_type, std::move (substitutions));
142237
context->insert_type (function.get_mappings (), fnType);
143238
}
144239

gcc/rust/typecheck/rust-hir-type-check-toplevel.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ class TypeCheckTopLevel : public TypeCheckBase
230230

231231
auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (),
232232
function.get_mappings ().get_defid (),
233-
function.get_function_name (), false,
234-
std::move (params), ret_type,
235-
std::move (substitutions));
233+
function.get_function_name (),
234+
FNTYPE_DEFAULT_FLAGS, std::move (params),
235+
ret_type, std::move (substitutions));
236236
context->insert_type (function.get_mappings (), fnType);
237237
}
238238

@@ -274,6 +274,14 @@ class TypeCheckTopLevel : public TypeCheckBase
274274
substitutions);
275275
}
276276

277+
void visit (HIR::ExternBlock &extern_block) override
278+
{
279+
for (auto &item : extern_block.get_extern_items ())
280+
{
281+
TypeCheckTopLevelExternItem::Resolve (item.get ());
282+
}
283+
}
284+
277285
private:
278286
TypeCheckTopLevel () : TypeCheckBase () {}
279287
};

gcc/rust/typecheck/rust-tyty.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ FnType::clone ()
816816
std::pair<HIR::Pattern *, BaseType *> (p.first, p.second->clone ()));
817817

818818
return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (),
819-
is_method_flag, std::move (cloned_params),
819+
flags, std::move (cloned_params),
820820
get_return_type ()->clone (), clone_substs (),
821821
get_combined_refs ());
822822
}

gcc/rust/typecheck/rust-tyty.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -970,30 +970,34 @@ class ADTType : public BaseType, public SubstitutionRef
970970
class FnType : public BaseType, public SubstitutionRef
971971
{
972972
public:
973-
FnType (HirId ref, DefId id, std::string identifier, bool is_method,
973+
#define FNTYPE_DEFAULT_FLAGS 0x00
974+
#define FNTYPE_IS_METHOD_FLAG 0x01
975+
#define FNTYPE_IS_EXTERN_FLAG 0x02
976+
977+
FnType (HirId ref, DefId id, std::string identifier, uint8_t flags,
974978
std::vector<std::pair<HIR::Pattern *, BaseType *> > params,
975979
BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
976980
std::set<HirId> refs = std::set<HirId> ())
977981
: BaseType (ref, ref, TypeKind::FNDEF, refs),
978982
SubstitutionRef (std::move (subst_refs),
979983
SubstitutionArgumentMappings::error ()),
980-
params (std::move (params)), type (type), is_method_flag (is_method),
984+
params (std::move (params)), type (type), flags (flags),
981985
identifier (identifier), id (id)
982986
{
983987
LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
984988
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
985989
}
986990

987991
FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier,
988-
bool is_method,
992+
uint8_t flags,
989993
std::vector<std::pair<HIR::Pattern *, BaseType *> > params,
990994
BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
991995
std::set<HirId> refs = std::set<HirId> ())
992996
: BaseType (ref, ty_ref, TypeKind::FNDEF, refs),
993997
SubstitutionRef (std::move (subst_refs),
994998
SubstitutionArgumentMappings::error ()),
995-
params (params), type (type), is_method_flag (is_method),
996-
identifier (identifier), id (id)
999+
params (params), type (type), flags (flags), identifier (identifier),
1000+
id (id)
9971001
{
9981002
LocalDefId local_def_id = id & DEF_ID_LOCAL_DEF_MASK;
9991003
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
@@ -1022,9 +1026,11 @@ class FnType : public BaseType, public SubstitutionRef
10221026
if (num_params () == 0)
10231027
return false;
10241028

1025-
return is_method_flag;
1029+
return (flags & FNTYPE_IS_METHOD_FLAG) != 0;
10261030
}
10271031

1032+
bool is_extern () const { return (flags & FNTYPE_IS_EXTERN_FLAG) != 0; }
1033+
10281034
DefId get_id () const { return id; }
10291035

10301036
// get the Self type for the method
@@ -1077,7 +1083,7 @@ class FnType : public BaseType, public SubstitutionRef
10771083
private:
10781084
std::vector<std::pair<HIR::Pattern *, BaseType *> > params;
10791085
BaseType *type;
1080-
bool is_method_flag;
1086+
uint8_t flags;
10811087
std::string identifier;
10821088
DefId id;
10831089
};

0 commit comments

Comments
 (0)