refactor: Context::register_methods

This commit is contained in:
Shunsuke Shibayama 2023-05-31 11:03:09 +09:00
parent 932db3da0b
commit c17d3d147a
2 changed files with 48 additions and 135 deletions

View file

@ -757,50 +757,19 @@ impl Context {
};
let name = VarName::from_str(t.local_name());
let meta_t = v_enum(set! { val.clone() });
self.locals.insert(
name.clone(),
VarInfo::new(
meta_t,
muty,
vis,
Builtin,
None,
None,
py_name.map(Str::ever),
AbsLocation::unknown(),
),
let vi = VarInfo::new(
meta_t,
muty,
vis,
Builtin,
None,
None,
py_name.map(Str::ever),
AbsLocation::unknown(),
);
self.locals.insert(name.clone(), vi);
self.consts.insert(name.clone(), val);
for impl_trait in ctx.super_traits.iter() {
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
} else {
self.trait_impls().register(
impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone())],
);
}
}
for (trait_method, vi) in ctx.decls.iter() {
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_traits.insert(
trait_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
for (class_method, vi) in ctx.locals.iter() {
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_classes.insert(
class_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
self.register_methods(&t, &ctx);
self.mono_types.insert(name, (t, ctx));
}
}
@ -847,40 +816,44 @@ impl Context {
);
}
self.consts.insert(name.clone(), val);
for impl_trait in ctx.super_traits.iter() {
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
} else {
self.trait_impls().register(
impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone())],
);
}
}
for (trait_method, vi) in ctx.decls.iter() {
if let Some(traits) = self.method_to_traits.get_mut(trait_method.inspect()) {
traits.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_traits.insert(
trait_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
for (class_method, vi) in ctx.locals.iter() {
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_classes.insert(
class_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
self.register_methods(&t, &ctx);
self.poly_types.insert(name, (t, ctx));
}
}
pub(crate) fn register_methods(&mut self, t: &Type, ctx: &Self) {
for impl_trait in ctx.super_traits.iter() {
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
} else {
self.trait_impls().register(
impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone())],
);
}
}
for (trait_method, vi) in ctx.decls.iter() {
if let Some(traits) = self.method_to_traits.get_mut(trait_method.inspect()) {
traits.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_traits.insert(
trait_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
for (class_method, vi) in ctx.locals.iter() {
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_classes.insert(
class_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
}
fn register_builtin_patch(
&mut self,
name: &'static str,

View file

@ -36,9 +36,7 @@ use crate::ty::{
};
use crate::build_hir::HIRBuilder;
use crate::context::{
ClassDefType, Context, ContextKind, DefaultInfo, MethodPair, RegistrationMode, TraitImpl,
};
use crate::context::{ClassDefType, Context, ContextKind, DefaultInfo, RegistrationMode};
use crate::error::readable_name;
use crate::error::{
CompileError, CompileErrors, CompileResult, TyCheckError, TyCheckErrors, TyCheckResult,
@ -1652,36 +1650,7 @@ impl Context {
self.index().register(&vi);
self.decls.insert(name.clone(), vi);
self.consts.insert(name.clone(), val);
for impl_trait in ctx.super_traits.iter() {
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
} else {
self.trait_impls().register(
impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone())],
);
}
}
for (trait_method, vi) in ctx.decls.iter() {
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_traits.insert(
trait_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
for (class_method, vi) in ctx.locals.iter() {
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_classes.insert(
class_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
self.register_methods(&t, &ctx);
self.mono_types.insert(name.clone(), (t, ctx));
Ok(())
}
@ -1732,36 +1701,7 @@ impl Context {
);
self.consts
.insert(name.clone(), ValueObj::Type(TypeObj::Generated(gen)));
for impl_trait in ctx.super_traits.iter() {
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
} else {
self.trait_impls().register(
impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone())],
);
}
}
for (trait_method, vi) in ctx.decls.iter() {
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_traits.insert(
trait_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
for (class_method, vi) in ctx.locals.iter() {
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
types.push(MethodPair::new(t.clone(), vi.clone()));
} else {
self.method_to_classes.insert(
class_method.inspect().clone(),
vec![MethodPair::new(t.clone(), vi.clone())],
);
}
}
self.register_methods(&t, &ctx);
self.patches.insert(name.clone(), ctx);
Ok(())
}