Skip to content

Commit

Permalink
parser: Prepare for unnamed struct/class ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Oct 31, 2024
1 parent b7e9153 commit 50dca93
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 97 deletions.
2 changes: 1 addition & 1 deletion dora-frontend/src/program_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl<'x> visit::Visitor for TopLevelDeclaration<'x> {
id: FieldId(idx),
name,
parsed_ty: ParsedType::new_ast(field.data_type.clone()),
mutable: field.mutable,
mutable: true,
visibility: modifiers.visibility(),
});
}
Expand Down
10 changes: 3 additions & 7 deletions dora-frontend/src/sema/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct ClassDefinition {
pub is_internal: bool,
pub internal_resolved: bool,
pub visibility: Visibility,
pub requires_named_arguments: bool,
pub field_name_style: ast::FieldNameStyle,

pub fields: Vec<Field>,

Expand Down Expand Up @@ -67,7 +67,7 @@ impl ClassDefinition {
is_internal: modifiers.is_internal,
internal_resolved: false,
visibility: modifiers.visibility(),
requires_named_arguments: ast.uses_braces,
field_name_style: ast.field_name_style,

fields,

Expand Down Expand Up @@ -102,7 +102,7 @@ impl ClassDefinition {
is_internal: false,
internal_resolved: false,
visibility,
requires_named_arguments: false,
field_name_style: ast::FieldNameStyle::Positional,

fields,

Expand Down Expand Up @@ -228,10 +228,6 @@ impl ElementAccess for ClassDefinition {
}

impl ElementWithFields for ClassDefinition {
fn requires_named_arguments(&self) -> bool {
self.requires_named_arguments
}

fn field_name(&self, idx: usize) -> Name {
self.fields[idx].name
}
Expand Down
1 change: 0 additions & 1 deletion dora-frontend/src/sema/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub trait ElementAccess {
}

pub trait ElementWithFields {
fn requires_named_arguments(&self) -> bool;
fn fields_len(&self) -> usize;
fn fields<'a>(&'a self) -> Box<dyn Iterator<Item = ElementField> + 'a>;
fn field_name(&self, idx: usize) -> Name;
Expand Down
8 changes: 2 additions & 6 deletions dora-frontend/src/sema/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct StructDefinition {
pub fields: Vec<StructDefinitionField>,
pub field_names: HashMap<Name, StructDefinitionFieldId>,
pub extensions: RefCell<Vec<ExtensionDefinitionId>>,
pub requires_named_arguments: bool,
pub field_name_style: ast::FieldNameStyle,
}

impl StructDefinition {
Expand Down Expand Up @@ -76,7 +76,7 @@ impl StructDefinition {
fields,
field_names,
extensions: RefCell::new(Vec::new()),
requires_named_arguments: node.uses_braces,
field_name_style: node.field_style,
}
}

Expand Down Expand Up @@ -180,10 +180,6 @@ impl ElementAccess for StructDefinition {
}

impl ElementWithFields for StructDefinition {
fn requires_named_arguments(&self) -> bool {
self.requires_named_arguments
}

fn fields_len(&self) -> usize {
self.fields.len()
}
Expand Down
4 changes: 2 additions & 2 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ fn check_expr_call_struct(
return ty_error();
}

if struct_.requires_named_arguments() {
if struct_.field_name_style.is_named() {
check_expr_call_ctor_with_named_fields(ck, struct_, type_params.clone(), &arguments);
} else {
check_expr_call_ctor_with_unnamed_fields(ck, struct_, type_params.clone(), &arguments);
Expand Down Expand Up @@ -699,7 +699,7 @@ fn check_expr_call_class(
ck.sa.report(ck.file_id, e.span, msg);
}

if cls.requires_named_arguments {
if cls.field_name_style.is_named() {
check_expr_call_ctor_with_named_fields(ck, cls, type_params.clone(), &arguments);
} else {
check_expr_call_ctor_with_unnamed_fields(ck, cls, type_params.clone(), &arguments);
Expand Down
33 changes: 18 additions & 15 deletions dora-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,26 @@ pub struct Struct {
pub green: GreenNode,
pub modifiers: Option<ModifierList>,
pub name: Option<Ident>,
pub fields: Vec<StructField>,
pub fields: Vec<Arc<Field>>,
pub type_params: Option<TypeParams>,
pub where_bounds: Option<WhereBounds>,
pub uses_braces: bool,
pub field_style: FieldNameStyle,
}

#[derive(Clone, Debug)]
pub struct StructField {
pub id: NodeId,
pub span: Span,
pub green: GreenNode,
pub modifiers: Option<ModifierList>,
pub name: Option<Ident>,
pub data_type: Type,
#[derive(Copy, Clone, Debug)]
pub enum FieldNameStyle {
Named,
Positional,
Old,
}

impl FieldNameStyle {
pub fn is_named(&self) -> bool {
match self {
FieldNameStyle::Named => true,
_ => false,
}
}
}

pub type WhereBounds = Arc<WhereBoundsData>;
Expand Down Expand Up @@ -607,10 +613,10 @@ pub struct Class {
pub modifiers: Option<ModifierList>,
pub name: Option<Ident>,

pub fields: Vec<Field>,
pub fields: Vec<Arc<Field>>,
pub type_params: Option<TypeParams>,
pub where_bounds: Option<WhereBounds>,
pub uses_braces: bool,
pub field_name_style: FieldNameStyle,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -644,9 +650,6 @@ pub struct Field {
pub modifiers: Option<ModifierList>,
pub name: Option<Ident>,
pub data_type: Type,
pub primary_ctor: bool,
pub expr: Option<Expr>,
pub mutable: bool,
}

#[derive(Clone, Debug)]
Expand Down
10 changes: 2 additions & 8 deletions dora-parser/src/ast/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ impl AstDumper {

self.indent(|d| {
for field in &struc.fields {
d.dump_struct_field(field);
d.dump_field(field);
}
});
}

fn dump_struct_field(&mut self, field: &StructField) {
fn dump_field(&mut self, field: &Arc<Field>) {
dump!(self, "field @ {} {}", field.span, field.id);
self.dump_maybe_ident(&field.name);
self.indent(|d| d.dump_type(&field.data_type));
Expand Down Expand Up @@ -205,12 +205,6 @@ impl AstDumper {
});
}

fn dump_field(&mut self, field: &Field) {
dump!(self, "field @ {} {}", field.span, field.id);
self.dump_maybe_ident(&field.name);
self.indent(|d| d.dump_type(&field.data_type));
}

fn dump_fct(&mut self, fct: &Function) {
dump!(self, "fct @ {} {}", fct.span, fct.id);
self.dump_maybe_ident(&fct.name);
Expand Down
12 changes: 2 additions & 10 deletions dora-parser/src/ast/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ pub trait Visitor: Sized {
walk_use(self, i);
}

fn visit_struct_field(&mut self, f: &StructField) {
walk_struct_field(self, f);
}

fn visit_ctor(&mut self, m: &Arc<Function>) {
walk_fct(self, &m);
}
Expand All @@ -57,7 +53,7 @@ pub trait Visitor: Sized {
walk_fct(self, &m);
}

fn visit_field(&mut self, p: &Field) {
fn visit_field(&mut self, p: &Arc<Field>) {
walk_field(self, p);
}

Expand Down Expand Up @@ -167,14 +163,10 @@ pub fn walk_type_alias<V: Visitor>(_v: &mut V, _node: &Arc<Alias>) {

pub fn walk_struct<V: Visitor>(v: &mut V, s: &Struct) {
for f in &s.fields {
v.visit_struct_field(f);
v.visit_field(f);
}
}

pub fn walk_struct_field<V: Visitor>(v: &mut V, f: &StructField) {
v.visit_type(&f.data_type);
}

pub fn walk_field<V: Visitor>(v: &mut V, f: &Field) {
v.visit_type(&f.data_type);
}
Expand Down
Loading

0 comments on commit 50dca93

Please sign in to comment.