ir: simplify a bit the previous patch.
This commit is contained in:
+40
-66
@@ -11,7 +11,8 @@ use crate::bindgen::config::{Config, Language, Layout};
|
|||||||
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
|
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
|
||||||
use crate::bindgen::dependencies::Dependencies;
|
use crate::bindgen::dependencies::Dependencies;
|
||||||
use crate::bindgen::ir::{
|
use crate::bindgen::ir::{
|
||||||
AnnotationSet, Cfg, ConditionWrite, Documentation, Path, PrimitiveType, ToCondition, Type,
|
AnnotationSet, Cfg, ConditionWrite, Documentation, GenericPath, Path, PrimitiveType,
|
||||||
|
ToCondition, Type,
|
||||||
};
|
};
|
||||||
use crate::bindgen::library::Library;
|
use crate::bindgen::library::Library;
|
||||||
use crate::bindgen::monomorph::Monomorphs;
|
use crate::bindgen::monomorph::Monomorphs;
|
||||||
@@ -43,21 +44,22 @@ impl Function {
|
|||||||
attrs: &[syn::Attribute],
|
attrs: &[syn::Attribute],
|
||||||
mod_cfg: Option<&Cfg>,
|
mod_cfg: Option<&Cfg>,
|
||||||
) -> Result<Function, String> {
|
) -> Result<Function, String> {
|
||||||
let args = sig
|
let mut args = sig.inputs.iter().try_skip_map(|x| x.as_ident_and_type())?;
|
||||||
.inputs
|
|
||||||
.iter()
|
let mut ret = match sig.output {
|
||||||
.try_skip_map(|x| x.as_ident_and_type(self_type_path.as_ref()))?;
|
|
||||||
let ret = match sig.output {
|
|
||||||
syn::ReturnType::Default => Type::Primitive(PrimitiveType::Void),
|
syn::ReturnType::Default => Type::Primitive(PrimitiveType::Void),
|
||||||
syn::ReturnType::Type(_, ref ty) => {
|
syn::ReturnType::Type(_, ref ty) => {
|
||||||
if let Some(x) = Type::load(ty)? {
|
Type::load(ty)?.unwrap_or_else(|| Type::Primitive(PrimitiveType::Void))
|
||||||
x
|
|
||||||
} else {
|
|
||||||
Type::Primitive(PrimitiveType::Void)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(ref self_path) = self_type_path {
|
||||||
|
for (_, ref mut ty) in &mut args {
|
||||||
|
ty.replace_self_with(self_path);
|
||||||
|
}
|
||||||
|
ret.replace_self_with(self_path);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Function {
|
Ok(Function {
|
||||||
path,
|
path,
|
||||||
self_type_path,
|
self_type_path,
|
||||||
@@ -73,14 +75,15 @@ impl Function {
|
|||||||
pub fn swift_name(&self) -> String {
|
pub fn swift_name(&self) -> String {
|
||||||
// If the symbol name starts with the type name, separate the two components with '.'
|
// If the symbol name starts with the type name, separate the two components with '.'
|
||||||
// so that Swift recognises the association between the method and the type
|
// so that Swift recognises the association between the method and the type
|
||||||
let (ref type_prefix, ref type_name) = if let Some(type_name) = &self.self_type_path {
|
let (ref type_prefix, ref type_name) = match self.self_type_path {
|
||||||
let type_name = type_name.to_string();
|
Some(ref type_name) => {
|
||||||
if !self.path.name().starts_with(&type_name) {
|
let type_name = type_name.to_string();
|
||||||
return self.path.to_string();
|
if !self.path.name().starts_with(&type_name) {
|
||||||
|
return self.path.to_string();
|
||||||
|
}
|
||||||
|
(format!("{}.", type_name), type_name)
|
||||||
}
|
}
|
||||||
(format!("{}.", type_name), type_name)
|
None => ("".to_string(), "".to_string()),
|
||||||
} else {
|
|
||||||
("".to_string(), "".to_string())
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let item_name = self
|
let item_name = self
|
||||||
@@ -268,66 +271,37 @@ impl Source for Function {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait SynFnArgHelpers {
|
pub trait SynFnArgHelpers {
|
||||||
fn as_ident_and_type(
|
fn as_ident_and_type(&self) -> Result<Option<(String, Type)>, String>;
|
||||||
&self,
|
|
||||||
self_type_path: Option<&Path>,
|
|
||||||
) -> Result<Option<(String, Type)>, String>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_self_type(self_type_path: &Path, receiver: &syn::Receiver) -> Result<Option<Type>, String> {
|
fn gen_self_type(receiver: &syn::Receiver) -> Type {
|
||||||
fn _gen_self_type(
|
let self_ty = Type::Path(GenericPath::self_path());
|
||||||
self_type_path: &Path,
|
match receiver.reference {
|
||||||
receiver: &syn::Receiver,
|
Some(_) => match receiver.mutability {
|
||||||
) -> Result<syn::Type, syn::Error> {
|
Some(_) => Type::Ptr(Box::new(self_ty)),
|
||||||
Ok(match receiver.reference {
|
None => Type::ConstPtr(Box::new(self_ty)),
|
||||||
Some(_) => syn::Type::Reference(match receiver.mutability {
|
},
|
||||||
Some(_) => syn::parse_str(&format!("&mut {}", self_type_path))?,
|
None => self_ty,
|
||||||
None => syn::parse_str(&format!("&{}", self_type_path))?,
|
|
||||||
}),
|
|
||||||
None => syn::Type::Path(syn::parse_str(self_type_path.name())?),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Type::load(&Box::new(
|
|
||||||
_gen_self_type(self_type_path, receiver)
|
|
||||||
.map_err(|e| format!("Failed to generate self type: {}", e))?,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SynFnArgHelpers for syn::FnArg {
|
impl SynFnArgHelpers for syn::FnArg {
|
||||||
fn as_ident_and_type(
|
fn as_ident_and_type(&self) -> Result<Option<(String, Type)>, String> {
|
||||||
&self,
|
match *self {
|
||||||
self_type_path: Option<&Path>,
|
syn::FnArg::Typed(syn::PatType {
|
||||||
) -> Result<Option<(String, Type)>, String> {
|
|
||||||
match self {
|
|
||||||
&syn::FnArg::Typed(syn::PatType {
|
|
||||||
ref pat, ref ty, ..
|
ref pat, ref ty, ..
|
||||||
}) => match **pat {
|
}) => match **pat {
|
||||||
syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
|
syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
|
||||||
if let Some(mut x) = Type::load(ty)? {
|
let ty = match Type::load(ty)? {
|
||||||
if let Some(self_type_path) = self_type_path {
|
Some(x) => x,
|
||||||
x.replace_self_with(self_type_path)
|
None => return Ok(None),
|
||||||
}
|
};
|
||||||
Ok(Some((ident.to_string(), x)))
|
Ok(Some((ident.to_string(), ty)))
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => Err("Parameter has an unsupported type.".to_owned()),
|
_ => Err("Parameter has an unsupported type.".to_owned()),
|
||||||
},
|
},
|
||||||
&syn::FnArg::Receiver(ref receiver) => {
|
syn::FnArg::Receiver(ref receiver) => {
|
||||||
if let Some(self_type_path) = self_type_path {
|
Ok(Some(("self".to_string(), gen_self_type(receiver))))
|
||||||
if let Some(x) = gen_self_type(self_type_path, receiver)? {
|
|
||||||
Ok(Some(("self".to_string(), x)))
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(
|
|
||||||
"Parameter has an unsupported type (Self type found in free function)"
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ impl GenericPath {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn self_path() -> Self {
|
||||||
|
Self::new(Path::new("Self"), vec![])
|
||||||
|
}
|
||||||
|
|
||||||
pub fn replace_self_with(&mut self, self_ty: &Path) {
|
pub fn replace_self_with(&mut self, self_ty: &Path) {
|
||||||
if self.path.replace_self_with(self_ty) {
|
if self.path.replace_self_with(self_ty) {
|
||||||
self.export_name = self_ty.name().to_owned();
|
self.export_name = self_ty.name().to_owned();
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ void SelfTypeTestStruct_should_exist_unannotated(SelfTypeTestStruct self) CF_SWI
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ void SelfTypeTestStruct_should_exist_unannotated(SelfTypeTestStruct self) CF_SWI
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ void SelfTypeTestStruct_should_exist_unannotated(SelfTypeTestStruct self) CF_SWI
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ void SelfTypeTestStruct_should_exist_unannotated(SelfTypeTestStruct self) CF_SWI
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ void SelfTypeTestStruct_should_exist_unannotated(SelfTypeTestStruct self) CF_SWI
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(Box<SelfTypeTestStruct> self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(Box<SelfTypeTestStruct> self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
Box<SelfTypeTestStruct> SelfTypeTestStruct_should_not_exist_return_box() CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ void SelfTypeTestStruct_should_exist_unannotated(struct SelfTypeTestStruct self)
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(struct Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(struct Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
struct Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ void SelfTypeTestStruct_should_exist_unannotated(struct SelfTypeTestStruct self)
|
|||||||
|
|
||||||
void SelfTypeTestStruct_should_not_exist_box(struct Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
void SelfTypeTestStruct_should_not_exist_box(struct Box_SelfTypeTestStruct self) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_box(self:));
|
||||||
|
|
||||||
|
struct Box_SelfTypeTestStruct SelfTypeTestStruct_should_not_exist_return_box(void) CF_SWIFT_NAME(SelfTypeTestStruct.should_not_exist_return_box());
|
||||||
|
|
||||||
void free_function_should_exist_annotated_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
void free_function_should_exist_annotated_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_by_name(test_struct:));
|
||||||
|
|
||||||
void free_function_should_exist_annotated_mut_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
void free_function_should_exist_annotated_mut_by_name(struct SelfTypeTestStruct test_struct) CF_SWIFT_NAME(free_function_should_exist_annotated_mut_by_name(test_struct:));
|
||||||
|
|||||||
@@ -27,6 +27,12 @@ impl SelfTypeTestStruct {
|
|||||||
println!("should_not_exist_box");
|
println!("should_not_exist_box");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[export_name="SelfTypeTestStruct_should_not_exist_return_box"]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern fn should_not_exist_box() -> Box<Self> {
|
||||||
|
println!("should_not_exist_box");
|
||||||
|
}
|
||||||
|
|
||||||
#[export_name="SelfTypeTestStruct_should_exist_annotated_self"]
|
#[export_name="SelfTypeTestStruct_should_exist_annotated_self"]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern fn should_exist_annotated_self(self: Self) {
|
pub extern fn should_exist_annotated_self(self: Self) {
|
||||||
@@ -120,4 +126,4 @@ impl PointerToOpaque {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
header = "#define CF_SWIFT_NAME(_name) __attribute__((swift_name(#_name)))"
|
header = "#define CF_SWIFT_NAME(_name) __attribute__((swift_name(#_name)))"
|
||||||
|
|
||||||
[fn]
|
[fn]
|
||||||
swift_name_macro = "CF_SWIFT_NAME"
|
swift_name_macro = "CF_SWIFT_NAME"
|
||||||
|
|||||||
Reference in New Issue
Block a user