Skip to content

Commit

Permalink
feat:add position to ReadsReference variant
Browse files Browse the repository at this point in the history
  • Loading branch information
wzwywx committed Nov 2, 2023
1 parent de1fa94 commit 511e5e2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
9 changes: 7 additions & 2 deletions checker/src/behavior/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,15 @@ pub enum ThisValue {
}

impl ThisValue {
pub(crate) fn get(&self, environment: &mut Environment, types: &mut TypeStore) -> TypeId {
pub(crate) fn get(
&self,
environment: &mut Environment,
types: &mut TypeStore,
position: SpanWithSource,
) -> TypeId {
match self {
ThisValue::Passed(value) => *value,
ThisValue::UseParent => environment.get_value_of_this(types),
ThisValue::UseParent => environment.get_value_of_this(types, position),
}
}

Expand Down
6 changes: 6 additions & 0 deletions checker/src/context/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,13 @@ impl<'a> Environment<'a> {

// TODO temp position
let mut value = None;

for event in self.facts.events.iter() {
// TODO explain why don't need to detect sets
if let Event::ReadsReference {
reference: other_reference,
reflects_dependency: Some(dep),
position,
} = event
{
if reference == *other_reference {
Expand Down Expand Up @@ -643,6 +645,7 @@ impl<'a> Environment<'a> {
self.facts.events.push(Event::ReadsReference {
reference: RootReference::Variable(og_var.get_id()),
reflects_dependency: Some(ty),
position,
});

ty
Expand Down Expand Up @@ -999,6 +1002,7 @@ pub(crate) fn get_this_type_from_constraint(
facts: &mut Facts,
this_ty: TypeId,
types: &mut TypeStore,
position: SpanWithSource,
) -> TypeId {
// TODO `this_ty` can be error here..?
if this_ty == TypeId::ERROR_TYPE {
Expand Down Expand Up @@ -1034,6 +1038,7 @@ pub(crate) fn get_this_type_from_constraint(
if let Event::ReadsReference {
reference: other_reference,
reflects_dependency: Some(dep),
position,
} = event
{
if reference == RootReference::This {
Expand All @@ -1049,6 +1054,7 @@ pub(crate) fn get_this_type_from_constraint(
facts.events.push(Event::ReadsReference {
reference: RootReference::This,
reflects_dependency: Some(ty),
position,
});

ty
Expand Down
8 changes: 6 additions & 2 deletions checker/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,12 +1400,16 @@ impl<T: ContextType> Context<T> {
self.parents_iter().map(|env| get_on_ctx!(&env.facts))
}

pub(crate) fn get_value_of_this(&mut self, types: &mut TypeStore) -> TypeId {
pub(crate) fn get_value_of_this(
&mut self,
types: &mut TypeStore,
position: SpanWithSource,
) -> TypeId {
match self.can_use_this {
CanUseThis::NotYetSuperToBeCalled { .. } => todo!("Cannot use super before call"),
CanUseThis::ConstructorCalled { this_ty } => this_ty,
CanUseThis::Yeah { this_ty } => {
get_this_type_from_constraint(&mut self.facts, this_ty, types)
get_this_type_from_constraint(&mut self.facts, this_ty, types, position)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions checker/src/events/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn apply_event(
types: &mut TypeStore,
) -> EarlyReturn {
match event {
Event::ReadsReference { reference, reflects_dependency } => {
Event::ReadsReference { reference, reflects_dependency, position } => {
if let Some(id) = reflects_dependency {
let value = match reference {
RootReference::Variable(id) => {
Expand All @@ -40,7 +40,7 @@ pub(crate) fn apply_event(
}
}
}
RootReference::This => this_value.get(environment, types),
RootReference::This => this_value.get(environment, types, position),
};
type_arguments.set_id_from_reference(id, value, types);
}
Expand Down
6 changes: 5 additions & 1 deletion checker/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ pub enum Event {
/// Reads variable
///
/// Can be used for DCE reasons, or finding variables in context
ReadsReference { reference: RootReference, reflects_dependency: Option<TypeId> },
ReadsReference {
reference: RootReference,
reflects_dependency: Option<TypeId>,
position: SpanWithSource,
},
/// Also used for DCE
SetsVariable(VariableId, TypeId),
/// Mostly trivial, sometimes can call a function :(
Expand Down
12 changes: 10 additions & 2 deletions checker/src/synthesis/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,10 @@ pub(super) fn synthesise_expression<T: crate::ReadFromFS>(
}
}
Expression::ThisReference(..) => {
Instance::RValue(environment.get_value_of_this(&mut checking_data.types))
let position = SynthesisableExpression::get_position(expression)
.clone()
.with_source(environment.get_source());
Instance::RValue(environment.get_value_of_this(&mut checking_data.types, position))
}
Expression::SuperExpression(reference, position) => match reference {
SuperReference::Call { arguments } => {
Expand Down Expand Up @@ -856,7 +859,12 @@ pub(super) fn synthesise_class_fields<T: crate::ReadFromFS>(
environment: &mut Environment,
checking_data: &mut CheckingData<T, super::EznoParser>,
) {
let this = environment.get_value_of_this(&mut checking_data.types);
// TODO: Needs clarification. This only passes the position of the first field. Should we pass all fields positions?
let fields_position = SynthesisableExpression::get_position(&fields[0].2)
.clone()
.with_source(environment.get_source());

let this = environment.get_value_of_this(&mut checking_data.types, fields_position);
for (under, publicity, mut expression) in fields {
let new = synthesise_expression(&expression, environment, checking_data);
environment.facts.events.push(Event::Setter {
Expand Down

0 comments on commit 511e5e2

Please sign in to comment.