mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 12:14:43 +00:00
refactor: Context::register_methods
This commit is contained in:
parent
932db3da0b
commit
c17d3d147a
2 changed files with 48 additions and 135 deletions
|
@ -757,50 +757,19 @@ impl Context {
|
||||||
};
|
};
|
||||||
let name = VarName::from_str(t.local_name());
|
let name = VarName::from_str(t.local_name());
|
||||||
let meta_t = v_enum(set! { val.clone() });
|
let meta_t = v_enum(set! { val.clone() });
|
||||||
self.locals.insert(
|
let vi = VarInfo::new(
|
||||||
name.clone(),
|
meta_t,
|
||||||
VarInfo::new(
|
muty,
|
||||||
meta_t,
|
vis,
|
||||||
muty,
|
Builtin,
|
||||||
vis,
|
None,
|
||||||
Builtin,
|
None,
|
||||||
None,
|
py_name.map(Str::ever),
|
||||||
None,
|
AbsLocation::unknown(),
|
||||||
py_name.map(Str::ever),
|
|
||||||
AbsLocation::unknown(),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
self.locals.insert(name.clone(), vi);
|
||||||
self.consts.insert(name.clone(), val);
|
self.consts.insert(name.clone(), val);
|
||||||
for impl_trait in ctx.super_traits.iter() {
|
self.register_methods(&t, &ctx);
|
||||||
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.mono_types.insert(name, (t, ctx));
|
self.mono_types.insert(name, (t, ctx));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -847,40 +816,44 @@ impl Context {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.consts.insert(name.clone(), val);
|
self.consts.insert(name.clone(), val);
|
||||||
for impl_trait in ctx.super_traits.iter() {
|
self.register_methods(&t, &ctx);
|
||||||
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.poly_types.insert(name, (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(
|
fn register_builtin_patch(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
|
|
|
@ -36,9 +36,7 @@ use crate::ty::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::build_hir::HIRBuilder;
|
use crate::build_hir::HIRBuilder;
|
||||||
use crate::context::{
|
use crate::context::{ClassDefType, Context, ContextKind, DefaultInfo, RegistrationMode};
|
||||||
ClassDefType, Context, ContextKind, DefaultInfo, MethodPair, RegistrationMode, TraitImpl,
|
|
||||||
};
|
|
||||||
use crate::error::readable_name;
|
use crate::error::readable_name;
|
||||||
use crate::error::{
|
use crate::error::{
|
||||||
CompileError, CompileErrors, CompileResult, TyCheckError, TyCheckErrors, TyCheckResult,
|
CompileError, CompileErrors, CompileResult, TyCheckError, TyCheckErrors, TyCheckResult,
|
||||||
|
@ -1652,36 +1650,7 @@ impl Context {
|
||||||
self.index().register(&vi);
|
self.index().register(&vi);
|
||||||
self.decls.insert(name.clone(), vi);
|
self.decls.insert(name.clone(), vi);
|
||||||
self.consts.insert(name.clone(), val);
|
self.consts.insert(name.clone(), val);
|
||||||
for impl_trait in ctx.super_traits.iter() {
|
self.register_methods(&t, &ctx);
|
||||||
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.mono_types.insert(name.clone(), (t, ctx));
|
self.mono_types.insert(name.clone(), (t, ctx));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1732,36 +1701,7 @@ impl Context {
|
||||||
);
|
);
|
||||||
self.consts
|
self.consts
|
||||||
.insert(name.clone(), ValueObj::Type(TypeObj::Generated(gen)));
|
.insert(name.clone(), ValueObj::Type(TypeObj::Generated(gen)));
|
||||||
for impl_trait in ctx.super_traits.iter() {
|
self.register_methods(&t, &ctx);
|
||||||
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.patches.insert(name.clone(), ctx);
|
self.patches.insert(name.clone(), ctx);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue