1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use interoptopus::lang::c::{CType, CompositeType, ConstantValue, EnumType, Field, FnPointerType, Function, OpaqueType, Parameter, PrimitiveType, PrimitiveValue};
use interoptopus::patterns::TypePattern;
use interoptopus::util::safe_name;
#[derive(Copy, Clone)]
pub struct Converter {}
pub trait CSharpTypeConverter {
fn primitive_to_typename(&self, x: &PrimitiveType) -> String {
match x {
PrimitiveType::Void => "void".to_string(),
PrimitiveType::Bool => "bool".to_string(),
PrimitiveType::U8 => "byte".to_string(),
PrimitiveType::U16 => "ushort".to_string(),
PrimitiveType::U32 => "uint".to_string(),
PrimitiveType::U64 => "ulong".to_string(),
PrimitiveType::I8 => "sbyte".to_string(),
PrimitiveType::I16 => "short".to_string(),
PrimitiveType::I32 => "int".to_string(),
PrimitiveType::I64 => "long".to_string(),
PrimitiveType::F32 => "float".to_string(),
PrimitiveType::F64 => "double".to_string(),
}
}
fn enum_to_typename(&self, x: &EnumType) -> String {
x.rust_name().to_string()
}
fn opaque_to_typename(&self, _: &OpaqueType) -> String {
"IntPtr".to_string()
}
fn composite_to_typename(&self, x: &CompositeType) -> String {
x.rust_name().to_string()
}
fn fnpointer_to_typename(&self, x: &FnPointerType) -> String {
vec!["InteropDelegate".to_string(), safe_name(&x.internal_name())].join("_")
}
fn to_typespecifier_in_field(&self, x: &CType, _field: &Field, _composite: &CompositeType) -> String {
match &x {
CType::Primitive(x) => self.primitive_to_typename(x),
CType::Enum(x) => self.enum_to_typename(x),
CType::Opaque(x) => self.opaque_to_typename(x),
CType::Composite(x) => self.composite_to_typename(x),
CType::ReadPointer(_) => "IntPtr".to_string(),
CType::ReadWritePointer(_) => "IntPtr".to_string(),
CType::FnPointer(x) => self.fnpointer_to_typename(x),
CType::Pattern(x) => match x {
TypePattern::AsciiPointer => "string".to_string(),
TypePattern::SuccessEnum(e) => self.enum_to_typename(e.the_enum()),
TypePattern::Slice(e) => self.composite_to_typename(e),
TypePattern::Option(e) => self.composite_to_typename(e),
},
}
}
fn to_typespecifier_in_param(&self, x: &CType) -> String {
match &x {
CType::Primitive(x) => self.primitive_to_typename(x),
CType::Enum(x) => self.enum_to_typename(x),
CType::Opaque(x) => self.opaque_to_typename(x),
CType::Composite(x) => self.composite_to_typename(x),
CType::ReadPointer(z) => match **z {
CType::Opaque(_) => "IntPtr".to_string(),
CType::Primitive(PrimitiveType::Void) => "IntPtr".to_string(),
CType::ReadPointer(_) => "ref IntPtr".to_string(),
CType::ReadWritePointer(_) => "ref IntPtr".to_string(),
_ => format!("ref {}", self.to_typespecifier_in_param(z)),
},
CType::ReadWritePointer(z) => match **z {
CType::Opaque(_) => "IntPtr".to_string(),
CType::Primitive(PrimitiveType::Void) => "IntPtr".to_string(),
CType::ReadPointer(_) => "out IntPtr".to_string(),
CType::ReadWritePointer(_) => "out IntPtr".to_string(),
_ => format!("out {}", self.to_typespecifier_in_param(z)),
},
CType::FnPointer(x) => self.fnpointer_to_typename(x),
CType::Pattern(x) => match x {
TypePattern::AsciiPointer => "string".to_string(),
TypePattern::SuccessEnum(e) => self.enum_to_typename(e.the_enum()),
TypePattern::Slice(x) => self.composite_to_typename(x),
TypePattern::Option(x) => self.composite_to_typename(x),
},
}
}
fn to_typespecifier_in_rval(&self, x: &CType) -> String {
match &x {
CType::Primitive(x) => self.primitive_to_typename(x),
CType::Enum(x) => self.enum_to_typename(x),
CType::Opaque(x) => self.opaque_to_typename(x),
CType::Composite(x) => self.composite_to_typename(x),
CType::ReadPointer(_) => "IntPtr".to_string(),
CType::ReadWritePointer(_) => "IntPtr".to_string(),
CType::FnPointer(x) => self.fnpointer_to_typename(x),
CType::Pattern(x) => match x {
TypePattern::AsciiPointer => "string".to_string(),
TypePattern::SuccessEnum(e) => self.enum_to_typename(e.the_enum()),
TypePattern::Slice(x) => self.composite_to_typename(x),
TypePattern::Option(x) => self.composite_to_typename(x),
},
}
}
fn constant_value_to_value(&self, value: &ConstantValue) -> String {
match value {
ConstantValue::Primitive(x) => match x {
PrimitiveValue::Bool(x) => format!("{}", x),
PrimitiveValue::U8(x) => format!("{}", x),
PrimitiveValue::U16(x) => format!("{}", x),
PrimitiveValue::U32(x) => format!("{}", x),
PrimitiveValue::U64(x) => format!("{}", x),
PrimitiveValue::I8(x) => format!("{}", x),
PrimitiveValue::I16(x) => format!("{}", x),
PrimitiveValue::I32(x) => format!("{}", x),
PrimitiveValue::I64(x) => format!("{}", x),
PrimitiveValue::F32(x) => format!("{}", x),
PrimitiveValue::F64(x) => format!("{}", x),
},
}
}
fn function_parameter_to_csharp_typename(&self, x: &Parameter, _function: &Function) -> String {
self.to_typespecifier_in_param(x.the_type())
}
fn function_rval_to_csharp_typename(&self, function: &Function) -> String {
self.to_typespecifier_in_rval(function.signature().rval())
}
fn function_name_to_csharp_name(&self, function: &Function) -> String {
function.name().to_string()
}
}
impl CSharpTypeConverter for Converter {}