@@ -36,7 +36,9 @@ func NewGenerator(formatter fidlgen.Formatter) *Generator {
36
36
"ConstValue" : ConstValue ,
37
37
"EnumAttributes" : EnumAttributes ,
38
38
"StructAttributes" : StructAttributes ,
39
- "DescribeType" : DescribeType ,
39
+ "DescribeType" : func (desc zither.TypeDescriptor ) string {
40
+ return DescribeType (desc , CaseStyleRust )
41
+ },
40
42
})
41
43
return & Generator {* gen }
42
44
}
@@ -182,24 +184,56 @@ func StructAttributes() []string {
182
184
return []string {"#[repr(C)]" }
183
185
}
184
186
185
- func DescribeType (desc zither.TypeDescriptor ) string {
187
+ // CaseStyle represents a style of casing Rust type names.
188
+ type CaseStyle int
189
+
190
+ const (
191
+ // CaseStyleRust represents official rust style.
192
+ CaseStyleRust CaseStyle = iota
193
+
194
+ // CaseStyleSyscall gives a C-style spelling for certain types, for
195
+ // suggestive use in the Rust FFI syscall wrapper definitions.
196
+ CaseStyleSyscall
197
+ )
198
+
199
+ func DescribeType (desc zither.TypeDescriptor , style CaseStyle ) string {
200
+ var casify func (string ) string
201
+ switch style {
202
+ case CaseStyleRust :
203
+ casify = fidlgen .ToUpperCamelCase
204
+ case CaseStyleSyscall :
205
+ casify = func (name string ) string {
206
+ typ := fidlgen .ToSnakeCase (name )
207
+ switch desc .Kind {
208
+ case zither .TypeKindAlias , zither .TypeKindStruct , zither .TypeKindEnum ,
209
+ zither .TypeKindBits , zither .TypeKindHandle :
210
+ return "zx_" + typ + "_t"
211
+ default :
212
+ return typ
213
+ }
214
+ }
215
+ }
216
+
186
217
switch desc .Kind {
187
218
case zither .TypeKindBool , zither .TypeKindInteger , zither .TypeKindSize :
188
219
return ScalarTypeName (fidlgen .PrimitiveSubtype (desc .Type ))
189
220
case zither .TypeKindEnum , zither .TypeKindBits , zither .TypeKindStruct :
190
221
layout , _ := fidlgen .MustReadName (desc .Type ).SplitMember ()
191
- return fidlgen . ToUpperCamelCase (layout .DeclarationName ())
222
+ return casify (layout .DeclarationName ())
192
223
case zither .TypeKindAlias , zither .TypeKindHandle :
193
224
// TODO(fxbug.dev/105758): This assumes that the alias/handle was defined
194
225
// within the same package. That's true now, but this would need to be
195
226
// re-evaluated if/when zither supports library dependencies and the IR
196
227
// preserves imported alias names.
197
- return fidlgen . ToUpperCamelCase (fidlgen .MustReadName (desc .Type ).DeclarationName ())
228
+ return casify (fidlgen .MustReadName (desc .Type ).DeclarationName ())
198
229
case zither .TypeKindArray :
199
- return fmt .Sprintf ("[%s; %d]" , DescribeType (* desc .ElementType ), * desc .ElementCount )
230
+ return fmt .Sprintf ("[%s; %d]" , DescribeType (* desc .ElementType , style ), * desc .ElementCount )
200
231
case zither .TypeKindPointer , zither .TypeKindVoidPointer :
201
- // TODO(fxbug.dev/110294): Handle mutability.
202
- return "*const " + DescribeType (* desc .ElementType )
232
+ mutability := "const"
233
+ if desc .ElementType .Mutable {
234
+ mutability = "mut"
235
+ }
236
+ return fmt .Sprintf ("*%s %s" , mutability , DescribeType (* desc .ElementType , style ))
203
237
default :
204
238
panic (fmt .Sprintf ("unsupported type kind: %v" , desc .Kind ))
205
239
}
0 commit comments