Skip to content

Commit

Permalink
Directly insert dependent definitions and diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 25, 2023
1 parent 200607f commit f2e518f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
26 changes: 9 additions & 17 deletions valuescript_compiler/src/function_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::asm::{
Builtin, Definition, DefinitionContent, FnLine, Function, Instruction, Label, Pointer, Register,
Value,
};
use crate::diagnostic::{Diagnostic, DiagnosticContainer, DiagnosticLevel, DiagnosticReporter};
use crate::diagnostic::{Diagnostic, DiagnosticContainer, DiagnosticReporter};
use crate::expression_compiler::CompiledExpression;
use crate::expression_compiler::ExpressionCompiler;
use crate::ident::Ident;
Expand Down Expand Up @@ -63,7 +63,6 @@ pub struct CatchSetting {
pub struct FunctionCompiler<'a> {
pub mc: &'a mut ModuleCompiler,
pub current: Function,
pub definitions: Vec<Definition>,
pub owner_id: OwnerId,
pub reg_allocator: RegAllocator,
pub label_allocator: NameAllocator,
Expand All @@ -73,13 +72,11 @@ pub struct FunctionCompiler<'a> {
pub end_label: Option<Label>,
pub is_returning_register: Option<Register>,
pub finally_labels: Vec<Label>,

pub diagnostics: Vec<Diagnostic>,
}

impl<'a> DiagnosticContainer for FunctionCompiler<'a> {
fn diagnostics_mut(&mut self) -> &mut Vec<Diagnostic> {
&mut self.diagnostics
&mut self.mc.diagnostics
}
}

Expand All @@ -93,7 +90,6 @@ impl<'a> FunctionCompiler<'a> {
FunctionCompiler {
mc,
current: Function::default(),
definitions: vec![],
owner_id,
reg_allocator,
label_allocator: NameAllocator::default(),
Expand All @@ -103,7 +99,6 @@ impl<'a> FunctionCompiler<'a> {
end_label: None,
is_returning_register: None,
finally_labels: vec![],
diagnostics: vec![],
}
}

Expand Down Expand Up @@ -132,11 +127,10 @@ impl<'a> FunctionCompiler<'a> {
let name = self.mc.scope_analysis.lookup(ident);

if name.is_none() {
self.diagnostics.push(Diagnostic {
level: DiagnosticLevel::InternalError,
message: format!("Could not find name for ident {:?}", ident),
span: ident.span,
});
self.mc.diagnostics.push(Diagnostic::internal_error(
ident.span,
&format!("Could not find name for ident {:?}", ident),
));
}

name
Expand Down Expand Up @@ -210,7 +204,7 @@ impl<'a> FunctionCompiler<'a> {
definition_pointer: Pointer,
fn_name: Option<String>,
functionish: Functionish,
) -> (Vec<Definition>, Vec<Diagnostic>) {
) {
let mut self_ = FunctionCompiler::new(mc, functionish.owner_id());

self_
Expand All @@ -223,8 +217,6 @@ impl<'a> FunctionCompiler<'a> {
.expect("Failed to queue function");

self_.process_queue();

(self_.definitions, self_.diagnostics)
}

pub fn process_queue(&mut self) {
Expand Down Expand Up @@ -333,7 +325,7 @@ impl<'a> FunctionCompiler<'a> {
self.is_returning_register = None;
}

self.definitions.push(Definition {
self.mc.module.definitions.push(Definition {
pointer: definition_pointer,
content: DefinitionContent::Function(take(&mut self.current)),
});
Expand Down Expand Up @@ -1204,7 +1196,7 @@ impl<'a> FunctionCompiler<'a> {

let enum_value = self.mc.compile_enum_value(ts_enum);

self.definitions.push(Definition {
self.mc.module.definitions.push(Definition {
pointer,
content: DefinitionContent::Value(enum_value),
});
Expand Down
31 changes: 9 additions & 22 deletions valuescript_compiler/src/module_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,11 @@ impl ModuleCompiler {
));
}

let mut fn_defns = self.compile_fn(
self.compile_fn(
pointer,
Some(fn_name),
Functionish::Fn(Some(fn_.ident.clone()), fn_.function.clone()),
);

self.module.definitions.append(&mut fn_defns);
}

fn compile_enum_decl(&mut self, export: bool, ts_enum: &swc_ecma_ast::TsEnumDecl) {
Expand Down Expand Up @@ -396,13 +394,11 @@ impl ModuleCompiler {

self.module.export_default = Value::Pointer(defn.clone());

let mut fn_defns = self.compile_fn(
self.compile_fn(
defn,
fn_name,
Functionish::Fn(fn_.ident.clone(), fn_.function.clone()),
);

self.module.definitions.append(&mut fn_defns);
}
DefaultDecl::TsInterfaceDecl(_) => {
// Nothing to do
Expand Down Expand Up @@ -701,13 +697,8 @@ impl ModuleCompiler {
defn_pointer: Pointer,
fn_name: Option<String>,
functionish: Functionish,
) -> Vec<Definition> {
let (defn, mut diagnostics) =
FunctionCompiler::compile(self, defn_pointer, fn_name, functionish);

self.diagnostics.append(&mut diagnostics);

defn
) {
FunctionCompiler::compile(self, defn_pointer, fn_name, functionish);
}

pub fn compile_class(
Expand All @@ -719,7 +710,6 @@ impl ModuleCompiler {
let mut constructor: Value = Value::Void;
let mut prototype: Object = Object::default();
let mut static_: Object = Object::default();
let mut dependent_definitions: Vec<Definition>;

let defn_name = match ident {
Some(ident) => match self
Expand Down Expand Up @@ -793,7 +783,6 @@ impl ModuleCompiler {

// Include any other definitions that were created by the member initializers
mi_fnc.process_queue();
dependent_definitions = std::mem::take(&mut mi_fnc.definitions);

let mut has_constructor = false;

Expand All @@ -803,15 +792,15 @@ impl ModuleCompiler {

let ctor_defn_name = self.allocate_defn(&format!("{}_constructor", defn_name.name));

dependent_definitions.append(&mut self.compile_fn(
self.compile_fn(
ctor_defn_name.clone(),
None,
Functionish::Constructor(
member_initializers_assembly.clone(),
class.span,
ctor.clone(),
),
));
);

constructor = Value::Pointer(ctor_defn_name);
}
Expand All @@ -821,7 +810,7 @@ impl ModuleCompiler {
let ctor_defn_name = self.allocate_defn(&format!("{}_constructor", defn_name.name));

constructor = Value::Pointer(ctor_defn_name.clone());
dependent_definitions.push(Definition {
self.module.definitions.push(Definition {
pointer: ctor_defn_name,
content: DefinitionContent::Function(Function {
is_generator: false,
Expand Down Expand Up @@ -849,11 +838,11 @@ impl ModuleCompiler {
let method_defn_name =
self.allocate_defn(&ident_from_str(&format!("{}_{}", defn_name.name, name)));

dependent_definitions.append(&mut self.compile_fn(
self.compile_fn(
method_defn_name.clone(),
None,
Functionish::Fn(None, method.function.clone()),
));
);

let dst = match method.is_static {
false => &mut prototype,
Expand Down Expand Up @@ -892,8 +881,6 @@ impl ModuleCompiler {
}))),
});

self.module.definitions.append(&mut dependent_definitions);

defn_name
}

Expand Down
12 changes: 4 additions & 8 deletions valuescript_compiler/src/static_expression_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ impl<'a> StaticExpressionCompiler<'a> {
None => self.mc.allocate_defn_numbered("_anon"),
};

let mut nested_defns = self.mc.compile_fn(
self.mc.compile_fn(
p.clone(),
fn_name.clone(),
Functionish::Fn(fn_ident, method.function.clone()),
);

self.mc.module.definitions.append(&mut nested_defns);

(key, Value::Pointer(p))
}
},
Expand Down Expand Up @@ -161,22 +159,20 @@ impl<'a> StaticExpressionCompiler<'a> {
None => self.mc.allocate_defn_numbered("_anon"),
};

let mut fn_defns = self.mc.compile_fn(
self.mc.compile_fn(
p.clone(),
fn_name,
Functionish::Fn(fn_.ident.clone(), fn_.function.clone()),
);

self.mc.module.definitions.append(&mut fn_defns);

Value::Pointer(p)
}
swc_ecma_ast::Expr::Arrow(arrow) => {
let p = self.mc.allocate_defn_numbered("_anon");
let mut fn_defns = self

self
.mc
.compile_fn(p.clone(), None, Functionish::Arrow(arrow.clone()));
self.mc.module.definitions.append(&mut fn_defns);

Value::Pointer(p)
}
Expand Down

0 comments on commit f2e518f

Please sign in to comment.