rename ScopeHint to TypeHint
This commit is contained in:
parent
0d631bfa89
commit
7d77e1df32
@ -1,8 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
any::TypeId,
|
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
hint::black_box,
|
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -12,14 +10,14 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ScopeHint<'scope>(TypeIdRef, &'scope ScopeHints<'scope>);
|
pub struct TypeHint<'scope>(TypeIdRef, &'scope ScopeHints<'scope>);
|
||||||
|
|
||||||
impl<'scope> ScopeHint<'scope> {
|
impl<'scope> TypeHint<'scope> {
|
||||||
pub unsafe fn resolve_type(&self) -> TypeKind {
|
pub unsafe fn resolve_type(&self) -> TypeKind {
|
||||||
unsafe { *self.1.types.hints.borrow().get_unchecked(*self.0.borrow()) }
|
unsafe { *self.1.types.hints.borrow().get_unchecked(*self.0.borrow()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn narrow(&mut self, other: &ScopeHint) -> Result<ScopeHint<'scope>, ErrorKind> {
|
pub fn narrow(&mut self, other: &TypeHint) -> Result<TypeHint<'scope>, ErrorKind> {
|
||||||
self.1.combine_vars(self, other)
|
self.1.combine_vars(self, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +26,7 @@ impl<'scope> ScopeHint<'scope> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'scope> std::fmt::Debug for ScopeHint<'scope> {
|
impl<'scope> std::fmt::Debug for TypeHint<'scope> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_tuple("Hint")
|
f.debug_tuple("Hint")
|
||||||
.field(&self.0)
|
.field(&self.0)
|
||||||
@ -117,7 +115,7 @@ impl<'outer> ScopeHints<'outer> {
|
|||||||
name: String,
|
name: String,
|
||||||
mutable: bool,
|
mutable: bool,
|
||||||
initial_ty: TypeKind,
|
initial_ty: TypeKind,
|
||||||
) -> Result<ScopeHint<'outer>, ErrorKind> {
|
) -> Result<TypeHint<'outer>, ErrorKind> {
|
||||||
if self.variables.borrow().contains_key(&name) {
|
if self.variables.borrow().contains_key(&name) {
|
||||||
return Err(ErrorKind::VariableAlreadyDefined(name));
|
return Err(ErrorKind::VariableAlreadyDefined(name));
|
||||||
}
|
}
|
||||||
@ -125,10 +123,10 @@ impl<'outer> ScopeHints<'outer> {
|
|||||||
self.variables
|
self.variables
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.insert(name, (mutable, idx.clone()));
|
.insert(name, (mutable, idx.clone()));
|
||||||
Ok(ScopeHint(idx, self))
|
Ok(TypeHint(idx, self))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_type(&'outer self, ty: &TypeKind) -> Option<ScopeHint<'outer>> {
|
pub fn from_type(&'outer self, ty: &TypeKind) -> Option<TypeHint<'outer>> {
|
||||||
let idx = match ty {
|
let idx = match ty {
|
||||||
TypeKind::Vague(super::VagueType::Hinted(idx)) => {
|
TypeKind::Vague(super::VagueType::Hinted(idx)) => {
|
||||||
let inner_idx = unsafe { *self.types.recurse_type_ref(*idx).borrow() };
|
let inner_idx = unsafe { *self.types.recurse_type_ref(*idx).borrow() };
|
||||||
@ -143,27 +141,27 @@ impl<'outer> ScopeHints<'outer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Some(ScopeHint(idx, self))
|
Some(TypeHint(idx, self))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn narrow_to_type(
|
fn narrow_to_type(
|
||||||
&'outer self,
|
&'outer self,
|
||||||
hint: &ScopeHint,
|
hint: &TypeHint,
|
||||||
ty: &TypeKind,
|
ty: &TypeKind,
|
||||||
) -> Result<ScopeHint<'outer>, ErrorKind> {
|
) -> Result<TypeHint<'outer>, ErrorKind> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut hints = self.types.hints.borrow_mut();
|
let mut hints = self.types.hints.borrow_mut();
|
||||||
let existing = hints.get_unchecked_mut(*hint.0.borrow());
|
let existing = hints.get_unchecked_mut(*hint.0.borrow());
|
||||||
*existing = existing.collapse_into(&ty)?;
|
*existing = existing.collapse_into(&ty)?;
|
||||||
Ok(ScopeHint(hint.0.clone(), self))
|
Ok(TypeHint(hint.0.clone(), self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn combine_vars(
|
fn combine_vars(
|
||||||
&'outer self,
|
&'outer self,
|
||||||
hint1: &ScopeHint,
|
hint1: &TypeHint,
|
||||||
hint2: &ScopeHint,
|
hint2: &TypeHint,
|
||||||
) -> Result<ScopeHint<'outer>, ErrorKind> {
|
) -> Result<TypeHint<'outer>, ErrorKind> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ty = self
|
let ty = self
|
||||||
.types
|
.types
|
||||||
@ -177,7 +175,7 @@ impl<'outer> ScopeHints<'outer> {
|
|||||||
*idx.borrow_mut() = *hint1.0.borrow();
|
*idx.borrow_mut() = *hint1.0.borrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(ScopeHint(hint1.0.clone(), self))
|
Ok(TypeHint(hint1.0.clone(), self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,20 +187,20 @@ impl<'outer> ScopeHints<'outer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_hint(&'outer self, name: &String) -> Option<(bool, ScopeHint<'outer>)> {
|
pub fn find_hint(&'outer self, name: &String) -> Option<(bool, TypeHint<'outer>)> {
|
||||||
self.variables
|
self.variables
|
||||||
.borrow()
|
.borrow()
|
||||||
.get(name)
|
.get(name)
|
||||||
.map(|(mutable, idx)| (*mutable, ScopeHint(idx.clone(), self)))
|
.map(|(mutable, idx)| (*mutable, TypeHint(idx.clone(), self)))
|
||||||
.or(self.outer.map(|o| o.find_hint(name)).flatten())
|
.or(self.outer.map(|o| o.find_hint(name)).flatten())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn binop(
|
pub fn binop(
|
||||||
&'outer self,
|
&'outer self,
|
||||||
op: &BinaryOperator,
|
op: &BinaryOperator,
|
||||||
lhs: &mut ScopeHint<'outer>,
|
lhs: &mut TypeHint<'outer>,
|
||||||
rhs: &mut ScopeHint<'outer>,
|
rhs: &mut TypeHint<'outer>,
|
||||||
) -> Result<ScopeHint<'outer>, ErrorKind> {
|
) -> Result<TypeHint<'outer>, ErrorKind> {
|
||||||
let ty = lhs.narrow(rhs)?;
|
let ty = lhs.narrow(rhs)?;
|
||||||
Ok(match op {
|
Ok(match op {
|
||||||
BinaryOperator::Add => ty,
|
BinaryOperator::Add => ty,
|
||||||
|
@ -8,7 +8,7 @@ use VagueType::*;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
pass::{Pass, PassState, ScopeFunction, ScopeVariable},
|
pass::{Pass, PassState, ScopeFunction, ScopeVariable},
|
||||||
scopehints::{ScopeHint, ScopeHints, TypeHints},
|
scopehints::{ScopeHints, TypeHint, TypeHints},
|
||||||
types::{pick_return, ReturnType},
|
types::{pick_return, ReturnType},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -105,7 +105,7 @@ impl Block {
|
|||||||
&mut self,
|
&mut self,
|
||||||
state: &mut PassState<ErrorKind>,
|
state: &mut PassState<ErrorKind>,
|
||||||
outer_hints: &'s ScopeHints,
|
outer_hints: &'s ScopeHints,
|
||||||
) -> Result<(ReturnKind, ScopeHint<'s>), ErrorKind> {
|
) -> Result<(ReturnKind, TypeHint<'s>), ErrorKind> {
|
||||||
let mut state = state.inner();
|
let mut state = state.inner();
|
||||||
let inner_hints = outer_hints.inner();
|
let inner_hints = outer_hints.inner();
|
||||||
|
|
||||||
@ -313,7 +313,7 @@ impl Expression {
|
|||||||
&mut self,
|
&mut self,
|
||||||
state: &mut PassState<ErrorKind>,
|
state: &mut PassState<ErrorKind>,
|
||||||
hints: &'s ScopeHints<'s>,
|
hints: &'s ScopeHints<'s>,
|
||||||
) -> Result<ScopeHint<'s>, ErrorKind> {
|
) -> Result<TypeHint<'s>, ErrorKind> {
|
||||||
match &mut self.0 {
|
match &mut self.0 {
|
||||||
ExprKind::Variable(var) => {
|
ExprKind::Variable(var) => {
|
||||||
let hint = hints
|
let hint = hints
|
||||||
|
Loading…
Reference in New Issue
Block a user