Add trait implementations to the type system.
This commit is contained in:
parent
96677aa499
commit
2076d74ece
4 changed files with 35 additions and 0 deletions
|
@ -52,6 +52,17 @@ impl Context {
|
||||||
self.types.new_trait(name, required)
|
self.types.new_trait(name, required)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_trait_implementation(&mut self, type_id: TypeId, trait_id: TypeId) -> TypeId {
|
||||||
|
let ty_name_id = self.types.get(type_id.clone()).unwrap().name();
|
||||||
|
let tr_name_id = self.types.get(trait_id.clone()).unwrap().name();
|
||||||
|
|
||||||
|
let ty_name = self.strings.get(ty_name_id).unwrap().clone();
|
||||||
|
let tr_name = self.strings.get(tr_name_id).unwrap().clone();
|
||||||
|
|
||||||
|
let name = self.strings.set(&format!("{}<{}>", tr_name, ty_name));
|
||||||
|
self.types.new_implementation(name, type_id, trait_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_type(&self, id: TypeId) -> Option<&Type> {
|
pub fn get_type(&self, id: TypeId) -> Option<&Type> {
|
||||||
self.types.get(id)
|
self.types.get(id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,4 +9,5 @@ pub enum Inner {
|
||||||
Buffer,
|
Buffer,
|
||||||
Compound(Vec<(StaticStringId, TypeId)>),
|
Compound(Vec<(StaticStringId, TypeId)>),
|
||||||
Trait(Vec<SignatureId>),
|
Trait(Vec<SignatureId>),
|
||||||
|
Implementation(TypeId, TypeId),
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,21 @@ impl Types {
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_implementation(
|
||||||
|
&mut self,
|
||||||
|
name: StaticStringId,
|
||||||
|
type_id: TypeId,
|
||||||
|
trait_id: TypeId,
|
||||||
|
) -> TypeId {
|
||||||
|
let id = self.next_type_id();
|
||||||
|
|
||||||
|
self.0.insert(
|
||||||
|
id.clone(),
|
||||||
|
Type::new_impl(id.clone(), name, type_id, trait_id),
|
||||||
|
);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: TypeId) -> Option<&Type> {
|
pub fn get(&self, id: TypeId) -> Option<&Type> {
|
||||||
self.0.get(&id)
|
self.0.get(&id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,14 @@ impl Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_impl(id: TypeId, name: StaticStringId, type_id: TypeId, trait_id: TypeId) -> Self {
|
||||||
|
Type {
|
||||||
|
name: name,
|
||||||
|
id: id,
|
||||||
|
inner: Inner::Implementation(type_id, trait_id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn id(&self) -> TypeId {
|
pub fn id(&self) -> TypeId {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue