Skip to content

Commit

Permalink
Remove QuSelf type
Browse files Browse the repository at this point in the history
  • Loading branch information
GsLogimaker committed Dec 20, 2023
1 parent f8a5388 commit 9f54ab6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 79 deletions.
55 changes: 4 additions & 51 deletions qu/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,8 @@ pub struct Definitions {
)
.collect();
self.function_groups.push(impl_group);
for (key, fn_id) in trait_group_pairs {
// Copy over keys and values, converting cases of Self
// to the class type
let mut impl_key = key;
impl_key.set_self_type(class_id);
for (_, fn_id) in trait_group_pairs {
// Copy over keys and values
self.add_to_function_group(
ItemId::FunctionGroup(impl_group_id),
fn_id,
Expand Down Expand Up @@ -1192,7 +1189,7 @@ pub struct Definitions {
pub fn define_function_in_item(
&mut self,
item_id: ItemId,
mut fn_definition:FunctionMetadata,
fn_definition:FunctionMetadata,
default_class:Option<ClassId>,
auto_add_to_class:bool,
) -> Result<FunctionId, QuMsg> {
Expand All @@ -1204,28 +1201,6 @@ pub struct Definitions {
.first()
.map(|x| *x)
});

if let Some(x) = class_id { if x.is_self_type() { panic!(
"Can't define function, {}, because self type is ambiguous",
fn_definition.identity.display_pretty(self)
) } }

// Replace instances of the self type
match (class_id, auto_add_to_class) {
(Some(class_id), true) /*if !self.get_class(class_id)?.is_trait*/ => {
// Replace instances of the self type with the specific class
fn_definition.identity.set_self_type(class_id);
},
(_, false) | (None, _) => {
// Function is static, throw an error if any Self types exist
if fn_definition.identity.has_self_type() {
panic!(
"Couldn't define static function, {}, because it contains a Self type, which can only be used in non-static functions.",
fn_definition.identity.display_pretty(self)
)
}
}
}

// Register function
let new_function_id = self.add_function(fn_definition);
Expand Down Expand Up @@ -1254,11 +1229,8 @@ pub struct Definitions {
class_id:ClassId,
trait_id:ClassId,
parent_item:ItemId,
mut external_function:FunctionMetadata,
external_function:FunctionMetadata,
) -> Result<(), QuMsg> {
// Convert cases of Self to the implementing class
external_function.identity.set_self_type(class_id);

let trait_group_id = self.get_class(trait_id)?
.common
.get_function_group_id(
Expand Down Expand Up @@ -1467,25 +1439,6 @@ pub struct FunctionIdentity {
ret_type
)
}

fn has_self_type(&mut self) -> bool{
self.return_type.is_self_type()
|| {
for arg in self.parameters.iter() {
if arg.is_self_type() {
return true;
}
}
false
}
}

fn set_self_type(&mut self, to_type:ClassId) {
self.return_type.set_self(to_type);
for arg in self.parameters.iter_mut() {
arg.set_self(to_type);
}
}
} impl Hash for FunctionIdentity {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
Expand Down
13 changes: 1 addition & 12 deletions qu/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::alloc::Layout;
use std::fmt::Debug;

use crate::QuMsg;
use crate::QuSelf;
use crate::Register;
use crate::QuVm;
use crate::Uuid;
Expand Down Expand Up @@ -183,16 +182,6 @@ impl ClassId {
.common.implementations
.contains_key(&other)
}

pub(crate) fn is_self_type(self) -> bool{
self == QuSelf::id()
}

pub(crate) fn set_self(&mut self, to_type:ClassId) {
if self.is_self_type() {
*self = to_type;
}
}
} impl From<usize> for ClassId {
fn from(index: usize) -> Self {
Self(index)
Expand Down Expand Up @@ -369,7 +358,7 @@ pub trait RegistererLayer {
if args.len() == 0 {
panic!("Can't add function to class without binding a 'self' argument. TODO: better msg")
}
if !args[0].is(class, self.get_definitions()) && !args[0].is_self_type() {
if !args[0].is(class, self.get_definitions()) {
panic!(
"First argument's type, {}, does not match class's type, {}.",
self.get_definitions().get_class(args[0])?.common.name,
Expand Down
21 changes: 5 additions & 16 deletions qu/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
let float = m.add_class::<Float>()?;
let int = m.add_class::<Int>()?;
let module = m.add_class::<Module>()?;
let self_type = m.add_class::<QuSelf>()?;



Expand All @@ -98,7 +97,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
]
let ident = m.add_trait::<Type>()?;
m.add_function_to_class(ident, name,
[self_type, self_type], self_type,
[ident, ident], ident,
&|api| { (EMPTY_FN)(api) }
)?;
);
Expand All @@ -111,7 +110,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
]
let ident = m.add_trait::<Type>()?;
m.add_function_to_class(ident, name,
[self_type, self_type], bool,
[ident, ident], bool,
&|api| { (EMPTY_FN)(api) }
)?;
);
Expand Down Expand Up @@ -252,9 +251,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
m.implement_function(
equal,
class,
"equal",
[self_type, self_type],
bool,
"equal", [class, class], bool,
&|api| {
let left = *api.get::<Class>(0)?;
let right = *api.get::<Class>(1)?;
Expand Down Expand Up @@ -293,7 +290,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
m.implement_function(
trait_id,
class_id,
fn_name, [self_type, self_type], class_id,
fn_name, [class_id, class_id], class_id,
&|api| {
api.set::<Ret>(
*api.get::<OpType>(0)? op *api.get::<OpType>(1)?
Expand All @@ -319,9 +316,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> {
m.implement_function(
trait_id,
class_id,
fn_name,
[self_type, self_type],
bool,
fn_name, [class_id, class_id], bool,
&|api| {
let value = *api.get::<OpType>(0)?
op *api.get::<OpType>(1)?;
Expand Down Expand Up @@ -385,12 +380,6 @@ duplicate!(
}
);

/// The Self type Qu
pub struct QuSelf {}
impl Register for QuSelf {
fn name() -> &'static str {"Self"}
}


/// A reference to a Qu class.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
Expand Down

0 comments on commit 9f54ab6

Please sign in to comment.