Add ClassDefType

This commit is contained in:
Shunsuke Shibayama 2022-09-12 13:09:54 +09:00
parent cff0cae28a
commit c6b8d6a6df
4 changed files with 31 additions and 8 deletions

View file

@ -19,7 +19,7 @@ use erg_parser::ast::VarName;
use crate::context::initialize::const_func::{class_func, inherit_func, inheritable_func}; use crate::context::initialize::const_func::{class_func, inherit_func, inheritable_func};
use crate::context::instantiate::{ConstTemplate, TyVarContext}; use crate::context::instantiate::{ConstTemplate, TyVarContext};
use crate::context::{Context, ContextKind, DefaultInfo, ParamSpec, TraitInstance}; use crate::context::{ClassDefType, Context, ContextKind, DefaultInfo, ParamSpec, TraitInstance};
use crate::varinfo::{Mutability, VarInfo, VarKind}; use crate::varinfo::{Mutability, VarInfo, VarKind};
use DefaultInfo::*; use DefaultInfo::*;
use Mutability::*; use Mutability::*;
@ -114,7 +114,7 @@ impl Context {
let t = Self::instantiate_t(t, &mut tv_ctx); let t = Self::instantiate_t(t, &mut tv_ctx);
// FIXME: panic // FIXME: panic
if let Some((_, root_ctx)) = self.poly_types.get_mut(&t.name()) { if let Some((_, root_ctx)) = self.poly_types.get_mut(&t.name()) {
root_ctx.methods_list.push((t, ctx)); root_ctx.methods_list.push((ClassDefType::Simple(t), ctx));
} else { } else {
let name = VarName::from_str(t.name()); let name = VarName::from_str(t.name());
self.locals.insert( self.locals.insert(
@ -463,6 +463,8 @@ impl Context {
float.register_builtin_const("SubO", ValueObj::builtin_t(Float)); float.register_builtin_const("SubO", ValueObj::builtin_t(Float));
float.register_builtin_const("MulO", ValueObj::builtin_t(Float)); float.register_builtin_const("MulO", ValueObj::builtin_t(Float));
float.register_builtin_const("DivO", ValueObj::builtin_t(Float)); float.register_builtin_const("DivO", ValueObj::builtin_t(Float));
// TODO: support multi platform
float.register_builtin_const("EPSILON", ValueObj::Float(2.220446049250313e-16));
float.register_builtin_const("MutType!", ValueObj::builtin_t(mono("Float!"))); float.register_builtin_const("MutType!", ValueObj::builtin_t(mono("Float!")));
float.register_builtin_impl("Real", Float, Const, Public); float.register_builtin_impl("Real", Float, Const, Public);
float.register_builtin_impl("Imag", Float, Const, Public); float.register_builtin_impl("Imag", Float, Const, Public);

View file

@ -65,6 +65,23 @@ impl TraitInstance {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ClassDefType {
Simple(Type),
ImplTrait { class: Type, impl_trait: Type },
}
impl std::fmt::Display for ClassDefType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ClassDefType::Simple(ty) => write!(f, "{ty}"),
ClassDefType::ImplTrait { class, impl_trait } => {
write!(f, "{class}|<: {impl_trait}|")
}
}
}
}
/// ``` /// ```
/// # use erg_common::ty::{Type, TyParam}; /// # use erg_common::ty::{Type, TyParam};
/// # use erg_compiler::context::TyParamIdx; /// # use erg_compiler::context::TyParamIdx;
@ -254,7 +271,7 @@ pub struct Context {
pub(crate) super_traits: Vec<Type>, // if self is not a trait, means implemented traits pub(crate) super_traits: Vec<Type>, // if self is not a trait, means implemented traits
// method definitions, if the context is a type // method definitions, if the context is a type
// specializations are included and needs to be separated out // specializations are included and needs to be separated out
pub(crate) methods_list: Vec<(Type, Context)>, pub(crate) methods_list: Vec<(ClassDefType, Context)>,
/// K: method name, V: impl patch /// K: method name, V: impl patch
/// Provided methods can switch implementations on a scope-by-scope basis /// Provided methods can switch implementations on a scope-by-scope basis
/// K: メソッド名, V: それを実装するパッチたち /// K: メソッド名, V: それを実装するパッチたち

View file

@ -14,7 +14,7 @@ use erg_type::value::{GenTypeObj, TypeKind, TypeObj, ValueObj};
use erg_type::{HasType, ParamTy, SubrType, TyBound, Type}; use erg_type::{HasType, ParamTy, SubrType, TyBound, Type};
use Type::*; use Type::*;
use crate::context::{Context, DefaultInfo, RegistrationMode, TraitInstance}; use crate::context::{ClassDefType, Context, DefaultInfo, RegistrationMode, TraitInstance};
use crate::error::readable_name; use crate::error::readable_name;
use crate::error::{TyCheckError, TyCheckResult}; use crate::error::{TyCheckError, TyCheckResult};
use crate::hir; use crate::hir;
@ -590,7 +590,8 @@ impl Context {
methods.register_fixed_auto_impl("__new__", new_t.clone(), Immutable, Private); methods.register_fixed_auto_impl("__new__", new_t.clone(), Immutable, Private);
// 必要なら、ユーザーが独自に上書きする // 必要なら、ユーザーが独自に上書きする
methods.register_auto_impl("new", new_t, Immutable, Public); methods.register_auto_impl("new", new_t, Immutable, Public);
ctx.methods_list.push((gen.t.clone(), methods)); ctx.methods_list
.push((ClassDefType::Simple(gen.t.clone()), methods));
self.register_gen_mono_type(ident, gen, ctx, Const); self.register_gen_mono_type(ident, gen, ctx, Const);
} else { } else {
todo!() todo!()
@ -625,7 +626,8 @@ impl Context {
); );
// 必要なら、ユーザーが独自に上書きする // 必要なら、ユーザーが独自に上書きする
methods.register_auto_impl("new", new_t, Immutable, Public); methods.register_auto_impl("new", new_t, Immutable, Public);
ctx.methods_list.push((gen.t.clone(), methods)); ctx.methods_list
.push((ClassDefType::Simple(gen.t.clone()), methods));
self.register_gen_mono_type(ident, gen, ctx, Const); self.register_gen_mono_type(ident, gen, ctx, Const);
} else { } else {
todo!("super class not found") todo!("super class not found")

View file

@ -20,7 +20,7 @@ use erg_type::typaram::TyParam;
use erg_type::value::{TypeObj, ValueObj}; use erg_type::value::{TypeObj, ValueObj};
use erg_type::{HasType, ParamTy, Type}; use erg_type::{HasType, ParamTy, Type};
use crate::context::{Context, ContextKind, RegistrationMode}; use crate::context::{ClassDefType, Context, ContextKind, RegistrationMode};
use crate::error::{ use crate::error::{
CompileError, CompileErrors, LowerError, LowerErrors, LowerResult, LowerWarnings, CompileError, CompileErrors, LowerError, LowerErrors, LowerResult, LowerWarnings,
}; };
@ -760,7 +760,9 @@ impl ASTLowerer {
} }
} }
} }
class_root.methods_list.push((class, methods)); class_root
.methods_list
.push((ClassDefType::Simple(class), methods));
} else { } else {
todo!() todo!()
} }