Skip to content

Commit

Permalink
New type GenericParam represents a generic parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendorff authored and emilio committed May 9, 2022
1 parent 13c0a4a commit c732069
Show file tree
Hide file tree
Showing 28 changed files with 462 additions and 103 deletions.
21 changes: 2 additions & 19 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Enum {
}

let path = Path::new(item.ident.unraw().to_string());
let generic_params = GenericParams::new(&item.generics);
let generic_params = GenericParams::load(&item.generics)?;

let mut variants = Vec::new();
let mut has_data = false;
Expand Down Expand Up @@ -616,24 +616,7 @@ impl Item for Enum {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
self.generic_params.len() > 0,
"{} is not generic",
self.path.name()
);
assert!(
self.generic_params.len() == generic_values.len(),
"{} has {} params but is being instantiated with {} values",
self.path.name(),
self.generic_params.len(),
generic_values.len(),
);

let mappings = self
.generic_params
.iter()
.zip(generic_values.iter())
.collect::<Vec<_>>();
let mappings = self.generic_params.call(self.path.name(), generic_values);

for variant in &self.variants {
if let VariantBody::Body { ref body, .. } = variant.body {
Expand Down
117 changes: 96 additions & 21 deletions src/bindgen/ir/generic_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,95 @@ use std::ops::Deref;

use syn::ext::IdentExt;

use crate::bindgen::cdecl;
use crate::bindgen::config::{Config, Language};
use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver};
use crate::bindgen::ir::{ArrayLength, Path, Type};
use crate::bindgen::utilities::IterHelpers;
use crate::bindgen::writer::{Source, SourceWriter};

#[derive(Debug, Clone)]
pub enum GenericParamType {
Type,
Const(Type),
}

#[derive(Debug, Clone)]
pub struct GenericParam {
name: Path,
ty: GenericParamType,
}

impl GenericParam {
pub fn new_type_param(name: &str) -> Self {
GenericParam {
name: Path::new(name),
ty: GenericParamType::Type,
}
}

pub fn load(param: &syn::GenericParam) -> Result<Option<Self>, String> {
match *param {
syn::GenericParam::Type(syn::TypeParam { ref ident, .. }) => Ok(Some(GenericParam {
name: Path::new(ident.unraw().to_string()),
ty: GenericParamType::Type,
})),

syn::GenericParam::Lifetime(_) => Ok(None),

syn::GenericParam::Const(syn::ConstParam {
ref ident, ref ty, ..
}) => match Type::load(ty)? {
None => {
// A type that evaporates, like PhantomData.
Err(format!("unsupported const generic type: {:?}", ty))
}
Some(ty) => Ok(Some(GenericParam {
name: Path::new(ident.unraw().to_string()),
ty: GenericParamType::Const(ty),
})),
},
}
}

pub fn name(&self) -> &Path {
&self.name
}
}

#[derive(Default, Debug, Clone)]
pub struct GenericParams(pub Vec<Path>);
pub struct GenericParams(pub Vec<GenericParam>);

impl GenericParams {
pub fn new(generics: &syn::Generics) -> Self {
GenericParams(
generics
.params
.iter()
.filter_map(|x| match *x {
syn::GenericParam::Type(syn::TypeParam { ref ident, .. })
| syn::GenericParam::Const(syn::ConstParam { ref ident, .. }) => {
Some(Path::new(ident.unraw().to_string()))
}
_ => None,
})
.collect(),
)
pub fn load(generics: &syn::Generics) -> Result<Self, String> {
let mut params = vec![];
for param in &generics.params {
if let Some(p) = GenericParam::load(param)? {
params.push(p);
}
}

Ok(GenericParams(params))
}

/// Associate each parameter with an argument.
pub fn call<'out>(
&'out self,
item_name: &str,
arguments: &'out [GenericArgument],
) -> Vec<(&'out Path, &'out GenericArgument)> {
assert!(self.len() > 0, "{} is not generic", item_name);
assert!(
self.len() == arguments.len(),
"{} has {} params but is being instantiated with {} values",
item_name,
self.len(),
arguments.len(),
);
self.iter()
.map(|param| param.name())
.zip(arguments.iter())
.collect()
}

fn write_internal<F: Write>(
Expand All @@ -41,9 +106,19 @@ impl GenericParams {
if i != 0 {
out.write(", ");
}
write!(out, "typename {}", item);
if with_default {
write!(out, " = void");
match item.ty {
GenericParamType::Type => {
write!(out, "typename {}", item.name);
if with_default {
write!(out, " = void");
}
}
GenericParamType::Const(ref ty) => {
cdecl::write_field(out, ty, item.name.name(), config);
if with_default {
write!(out, " = 0");
}
}
}
}
out.write(">");
Expand All @@ -57,9 +132,9 @@ impl GenericParams {
}

impl Deref for GenericParams {
type Target = [Path];
type Target = [GenericParam];

fn deref(&self) -> &[Path] {
fn deref(&self) -> &[GenericParam] {
&self.0
}
}
Expand Down Expand Up @@ -168,7 +243,7 @@ impl GenericPath {
for generic in &mut self.generics {
generic.rename_for_config(config, generic_params);
}
if !generic_params.contains(&self.path) {
if !generic_params.iter().any(|param| param.name == self.path) {
config.export.rename(&mut self.export_name);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl OpaqueItem {
) -> Result<OpaqueItem, String> {
Ok(Self::new(
path,
GenericParams::new(generics),
GenericParams::load(generics)?,
Cfg::append(mod_cfg, Cfg::load(attrs)),
AnnotationSet::load(attrs).unwrap_or_else(|_| AnnotationSet::new()),
Documentation::load(attrs),
Expand Down
22 changes: 2 additions & 20 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Struct {

Ok(Struct::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
fields,
has_tag_field,
is_enum_variant_body,
Expand Down Expand Up @@ -370,25 +370,7 @@ impl Item for Struct {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
self.generic_params.len() > 0,
"{} is not generic",
self.path
);
assert!(
self.generic_params.len() == generic_values.len(),
"{} has {} params but is being instantiated with {} values",
self.path,
self.generic_params.len(),
generic_values.len(),
);

let mappings = self
.generic_params
.iter()
.zip(generic_values.iter())
.collect::<Vec<_>>();

let mappings = self.generic_params.call(self.path.name(), generic_values);
let monomorph = self.specialize(generic_values, &mappings, library.get_config());
out.insert_struct(library, self, monomorph, generic_values.to_owned());
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ impl Type {
}
}
let path = generic.path();
if !generic_params.contains(path) {
if !generic_params.iter().any(|param| param.name() == path) {
if let Some(items) = library.get_items(path) {
if !out.items.contains(path) {
out.items.insert(path.clone());
Expand Down
21 changes: 2 additions & 19 deletions src/bindgen/ir/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Typedef {
let path = Path::new(item.ident.unraw().to_string());
Ok(Typedef::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
x,
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
AnnotationSet::load(&item.attrs)?,
Expand Down Expand Up @@ -159,24 +159,7 @@ impl Item for Typedef {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
self.generic_params.len() > 0,
"{} is not generic",
self.path
);
assert!(
self.generic_params.len() == generic_values.len(),
"{} has {} params but is being instantiated with {} values",
self.path,
self.generic_params.len(),
generic_values.len(),
);

let mappings = self
.generic_params
.iter()
.zip(generic_values.iter())
.collect::<Vec<_>>();
let mappings = self.generic_params.call(self.path.name(), generic_values);

let mangled_path = mangle::mangle_path(
&self.path,
Expand Down
21 changes: 2 additions & 19 deletions src/bindgen/ir/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Union {

Ok(Union::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
fields,
repr.align,
tuple_union,
Expand Down Expand Up @@ -227,24 +227,7 @@ impl Item for Union {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
self.generic_params.len() > 0,
"{} is not generic",
self.path
);
assert!(
self.generic_params.len() == generic_values.len(),
"{} has {} params but is being instantiated with {} values",
self.path,
self.generic_params.len(),
generic_values.len(),
);

let mappings = self
.generic_params
.iter()
.zip(generic_values.iter())
.collect::<Vec<_>>();
let mappings = self.generic_params.call(self.path.name(), generic_values);

let mangled_path = mangle::mangle_path(
&self.path,
Expand Down
9 changes: 6 additions & 3 deletions src/bindgen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::bindgen::cargo::{Cargo, PackageRef};
use crate::bindgen::config::{Config, ParseConfig};
use crate::bindgen::error::Error;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParam, GenericParams,
ItemMap, OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
};
use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemFnHelpers};

Expand Down Expand Up @@ -425,7 +425,10 @@ impl Parse {
pub fn add_std_types(&mut self) {
let mut add_opaque = |path: &str, generic_params: Vec<&str>| {
let path = Path::new(path);
let generic_params: Vec<_> = generic_params.into_iter().map(Path::new).collect();
let generic_params: Vec<_> = generic_params
.into_iter()
.map(GenericParam::new_type_param)
.collect();
self.opaque_items.try_insert(OpaqueItem::new(
path,
GenericParams(generic_params),
Expand Down
17 changes: 17 additions & 0 deletions tests/expectations/const_generics.both.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define TITLE_SIZE 80

typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];

typedef int8_t CArrayString_40[40];

typedef struct Book {
CArrayString_TITLE_SIZE title;
CArrayString_40 author;
} Book;

void root(struct Book *a);
25 changes: 25 additions & 0 deletions tests/expectations/const_generics.both.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define TITLE_SIZE 80

typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];

typedef int8_t CArrayString_40[40];

typedef struct Book {
CArrayString_TITLE_SIZE title;
CArrayString_40 author;
} Book;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void root(struct Book *a);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
17 changes: 17 additions & 0 deletions tests/expectations/const_generics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define TITLE_SIZE 80

typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];

typedef int8_t CArrayString_40[40];

typedef struct {
CArrayString_TITLE_SIZE title;
CArrayString_40 author;
} Book;

void root(Book *a);
Loading

0 comments on commit c732069

Please sign in to comment.