Skip to content

Commit 6c5bb1f

Browse files
committed
ir: Add support for arrays.
1 parent 3b1305a commit 6c5bb1f

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/bindgen/ir/constant.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub enum Literal {
114114
ty: Type,
115115
value: Box<Literal>,
116116
},
117+
Array {
118+
items: Vec<Literal>,
119+
},
117120
}
118121

119122
impl Literal {
@@ -182,13 +185,19 @@ impl Literal {
182185
}
183186
}
184187
}
188+
Literal::Array { ref mut items } => {
189+
for item in items {
190+
item.replace_self_with(self_ty);
191+
}
192+
}
185193
Literal::Expr(..) => {}
186194
}
187195
}
188196

189197
fn is_valid(&self, bindings: &Bindings) -> bool {
190198
match *self {
191199
Literal::Expr(..) => true,
200+
Literal::Array { ref items } => items.iter().all(|i| i.is_valid(bindings)),
192201
Literal::Path {
193202
ref associated_to,
194203
ref name,
@@ -228,6 +237,14 @@ impl Literal {
228237
ref right,
229238
..
230239
} => left.visit(visitor) && right.visit(visitor),
240+
Literal::Array { ref items } => {
241+
for item in items {
242+
if !item.visit(visitor) {
243+
return false;
244+
}
245+
}
246+
true
247+
}
231248
Literal::FieldAccess { ref base, .. } => base.visit(visitor),
232249
Literal::Struct { ref fields, .. } => {
233250
for (_name, field) in fields.iter() {
@@ -305,6 +322,11 @@ impl Literal {
305322
left.rename_for_config(config);
306323
right.rename_for_config(config);
307324
}
325+
Literal::Array { ref mut items } => {
326+
for item in items {
327+
item.rename_for_config(config);
328+
}
329+
}
308330
Literal::Expr(_) => {}
309331
Literal::Cast {
310332
ref mut ty,
@@ -506,6 +528,14 @@ impl Literal {
506528
}
507529
}
508530

531+
syn::Expr::Array(syn::ExprArray { ref elems, .. }) => {
532+
let mut items = vec![];
533+
for elem in elems {
534+
items.push(Self::load(elem)?);
535+
}
536+
Ok(Literal::Array { items })
537+
}
538+
509539
_ => Err(format!("Unsupported expression. {:?}", *expr)),
510540
}
511541
}
@@ -665,8 +695,14 @@ impl Constant {
665695
} else {
666696
out.write("static const ");
667697
}
668-
language_backend.write_type(out, &self.ty);
669-
write!(out, " {};", self.export_name());
698+
crate::bindgen::cdecl::write_field(
699+
language_backend,
700+
out,
701+
&self.ty,
702+
self.export_name(),
703+
config,
704+
);
705+
write!(out, ";");
670706
condition.write_after(config, out);
671707
}
672708

@@ -759,9 +795,8 @@ impl Constant {
759795
} else {
760796
out.write("const ");
761797
}
762-
763-
language_backend.write_type(out, &self.ty);
764-
write!(out, " {name} = ");
798+
crate::bindgen::cdecl::write_field(language_backend, out, &self.ty, &name, config);
799+
write!(out, " = ");
765800
language_backend.write_literal(out, value);
766801
write!(out, ";");
767802
}
@@ -771,10 +806,10 @@ impl Constant {
771806
}
772807
Language::Cython => {
773808
out.write("const ");
774-
language_backend.write_type(out, &self.ty);
775809
// For extern Cython declarations the initializer is ignored,
776810
// but still useful as documentation, so we write it as a comment.
777-
write!(out, " {name} # = ");
811+
crate::bindgen::cdecl::write_field(language_backend, out, &self.ty, &name, config);
812+
write!(out, " # = ");
778813
language_backend.write_literal(out, value);
779814
}
780815
}

src/bindgen/language_backend/clike.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,14 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
840840
fn write_literal<W: Write>(&mut self, out: &mut SourceWriter<W>, l: &Literal) {
841841
match l {
842842
Literal::Expr(v) => write!(out, "{v}"),
843+
Literal::Array { items } => {
844+
write!(out, "{{ ");
845+
for item in items {
846+
self.write_literal(out, item);
847+
write!(out, ", ");
848+
}
849+
write!(out, "}}");
850+
}
843851
Literal::Path {
844852
ref associated_to,
845853
ref name,

src/bindgen/language_backend/cython.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
384384
out.write(">");
385385
self.write_literal(out, value);
386386
}
387+
Literal::Array { ref items } => {
388+
write!(out, "[ ");
389+
for item in items {
390+
self.write_literal(out, item);
391+
write!(out, ", ");
392+
}
393+
write!(out, "]");
394+
}
387395
Literal::Struct {
388396
export_name,
389397
fields,

0 commit comments

Comments
 (0)