diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index 584d5e4..fa913d0 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use mist_parser::ast::*; -use crate::{Context, GenRust, GetRust, RustCodegen}; +use crate::{Context, GenRust, GenSpanTranslation, GetRust, RustCodegen}; pub struct ClassProcessedData { visibility: Visibility, @@ -117,7 +117,7 @@ impl ClassProcessedData { } for field in &self.fields { - cg.add_indentedln(&field.get_comment()); + field.gen_span(cg); cg.add_indentedln(&field.item.decl.get_rust()); } @@ -269,7 +269,7 @@ impl ClassProcessedData { } if let TypeExpr::Ref { mutable, .. } = params.remove(0) { - cg.add_indentedln(&method.get_comment()); + method.gen_span(cg); cg.add_indented(&format!("{}::__m_", target_rust_path)); cg.add(&method.item.name.get_rust()); cg.add(" as "); @@ -302,7 +302,7 @@ impl ClassProcessedData { for (override_tier, v) in &self.override_v_table { if let Some(path) = &override_tier.0 { - cg.add_indentedln(&v.get_comment()); + v.gen_span(cg); // This forces the compiler to statically verify that &Self can Deref into &Target cg.add_indentedln(&format!("let _: &{} = this;", path.get_rust())); } @@ -319,10 +319,8 @@ impl ClassProcessedData { ctx: &mut Context, cg: &mut RustCodegen, ) { - let constructor_comment = constructor.get_comment(); - cg.add_indentedln("#[allow(invalid_value)]"); - cg.add_indentedln(&constructor_comment); + constructor.gen_span(cg); cg.add_indented(&format!( "{}fn new{}(", @@ -357,10 +355,8 @@ impl ClassProcessedData { cg.add_indentedln("this._vptr = &Self::__V_TABLE;"); for field in &self.fields { - let comment = field.get_comment(); - if let Some(init) = &field.item.init { - cg.add_indentedln(&comment); + field.gen_span(cg); cg.add_indentedln(&format!("this.{} = ", field.item.decl.name.get_rust())); init.gen_rust(ctx, cg); } @@ -378,7 +374,7 @@ impl ClassProcessedData { cg.add_indentedln("this._vptr = &Self::__V_TABLE;"); - cg.add_indentedln(&constructor_comment); + constructor.gen_span(cg); cg.add_indentedln("this"); cg.indent -= 1; diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 1d926e1..7c806f4 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -142,6 +142,18 @@ impl GenRust for Attribute { } } +pub trait GenSpanTranslation { + fn gen_span(&self, _cg: &mut RustCodegen); +} + +impl GenSpanTranslation for Spanned { + fn gen_span(&self, cg: &mut RustCodegen) { + cg.mapping + .map + .insert((cg.position, MistMap(self.line, self.column))); + } +} + impl GenRust for T { fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) { cg.add(&self.get_rust()); @@ -150,10 +162,7 @@ impl GenRust for T { impl GenRust for Spanned { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { - cg.mapping - .map - .insert((cg.position, MistMap(self.line, self.column))); - + self.gen_span(cg); self.item.gen_rust(ctx, cg); } } diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index f1596ee..9a94947 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -1,6 +1,6 @@ use mist_parser::ast::*; -use crate::Context; +use crate::{Context, GenSpanTranslation}; use crate::{GenRust, GetRust, RustCodegen}; @@ -55,7 +55,7 @@ impl GenRust for Statement { cg.indent += 1; for match_item in match_items { - cg.add_indentedln(&match_item.get_comment()); + match_item.gen_span(cg); let MatchItem(pat, body) = &match_item.item; diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 04e6d4b..7de3984 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -72,9 +72,3 @@ impl From for Generics { Self(value.0.into_iter().map(Generic::from).collect()) } } - -impl Spanned { - pub fn get_comment(&self) -> String { - format!("/* {}:{} */", self.line, self.column) - } -}