Skip to content

Commit

Permalink
Handle case with nested typedef by using the last typedef with dartAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
mannprerak2 committed Dec 7, 2024
1 parent 3a355b1 commit 7991fee
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 19 deletions.
7 changes: 6 additions & 1 deletion pkgs/ffigen/example/shared_bindings/headers/a.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ void a_func1();

void a_func2(struct BaseStruct2 s, union BaseUnion2 u, BaseTypedef2 t);

void a_func3(TestInt i);
void a_func3(BaseNativeTypedef1 i);

void a_func4(BaseNativeTypedef2 i);

void a_func5(BaseNativeTypedef3 i);

4 changes: 3 additions & 1 deletion pkgs/ffigen/example/shared_bindings/headers/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ union BaseUnion2{

typedef struct BaseStruct1 BaseTypedef1;
typedef struct BaseStruct2 BaseTypedef2;
typedef int BaseNativeTypedef1;
typedef BaseNativeTypedef1 BaseNativeTypedef2;
typedef BaseNativeTypedef2 BaseNativeTypedef3;

enum BaseEnum{
BASE_ENUM_1,
Expand All @@ -30,4 +33,3 @@ enum BaseEnum{

void base_func1(BaseTypedef1 t1, BaseTypedef2 t2);

typedef int TestInt;
36 changes: 32 additions & 4 deletions pkgs/ffigen/example/shared_bindings/lib/generated/a_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,35 @@ class NativeLibraryA {
}

late final _a_func3Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(TestInt)>>('a_func3');
_lookup<ffi.NativeFunction<ffi.Void Function(BaseNativeTypedef1)>>(
'a_func3');
late final _a_func3 = _a_func3Ptr.asFunction<void Function(int)>();

void a_func4(
int i,
) {
return _a_func4(
i,
);
}

late final _a_func4Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(BaseNativeTypedef2)>>(
'a_func4');
late final _a_func4 = _a_func4Ptr.asFunction<void Function(int)>();

void a_func5(
int i,
) {
return _a_func5(
i,
);
}

late final _a_func5Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(BaseNativeTypedef3)>>(
'a_func5');
late final _a_func5 = _a_func5Ptr.asFunction<void Function(int)>();
}

final class BaseStruct1 extends ffi.Struct {
Expand All @@ -99,6 +126,10 @@ final class BaseUnion2 extends ffi.Union {

typedef BaseTypedef1 = BaseStruct1;
typedef BaseTypedef2 = BaseStruct2;
typedef BaseNativeTypedef1 = ffi.Int;
typedef DartBaseNativeTypedef1 = int;
typedef BaseNativeTypedef2 = BaseNativeTypedef1;
typedef BaseNativeTypedef3 = BaseNativeTypedef2;

enum BaseEnum {
BASE_ENUM_1(0),
Expand All @@ -114,9 +145,6 @@ enum BaseEnum {
};
}

typedef TestInt = ffi.Int;
typedef DartTestInt = int;

final class A_Struct1 extends ffi.Struct {
@ffi.Int()
external int a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,46 @@ class NativeLibraryASharedB {
void Function(imp1.BaseStruct2, imp1.BaseUnion2, imp1.BaseTypedef2)>();

void a_func3(
imp1.DartTestInt i,
imp1.DartBaseNativeTypedef1 i,
) {
return _a_func3(
i,
);
}

late final _a_func3Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(imp1.TestInt)>>('a_func3');
_lookup<ffi.NativeFunction<ffi.Void Function(imp1.BaseNativeTypedef1)>>(
'a_func3');
late final _a_func3 =
_a_func3Ptr.asFunction<void Function(imp1.DartTestInt)>();
_a_func3Ptr.asFunction<void Function(imp1.DartBaseNativeTypedef1)>();

void a_func4(
imp1.DartBaseNativeTypedef1 i,
) {
return _a_func4(
i,
);
}

late final _a_func4Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(imp1.BaseNativeTypedef2)>>(
'a_func4');
late final _a_func4 =
_a_func4Ptr.asFunction<void Function(imp1.DartBaseNativeTypedef1)>();

void a_func5(
imp1.DartBaseNativeTypedef1 i,
) {
return _a_func5(
i,
);
}

late final _a_func5Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function(imp1.BaseNativeTypedef3)>>(
'a_func5');
late final _a_func5 =
_a_func5Ptr.asFunction<void Function(imp1.DartBaseNativeTypedef1)>();
}

final class A_Struct1 extends ffi.Struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ final class BaseUnion2 extends ffi.Union {

typedef BaseTypedef1 = BaseStruct1;
typedef BaseTypedef2 = BaseStruct2;
typedef BaseNativeTypedef1 = ffi.Int;
typedef DartBaseNativeTypedef1 = int;
typedef BaseNativeTypedef2 = BaseNativeTypedef1;
typedef BaseNativeTypedef3 = BaseNativeTypedef2;

enum BaseEnum {
BASE_ENUM_1(0),
Expand All @@ -76,7 +80,4 @@ enum BaseEnum {
};
}

typedef TestInt = ffi.Int;
typedef DartTestInt = int;

const int BASE_MACRO_1 = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ files:
name: BaseUnion1
c:@U@BaseUnion2:
name: BaseUnion2
c:base.h@T@BaseNativeTypedef1:
name: BaseNativeTypedef1
dartName: DartBaseNativeTypedef1
c:base.h@T@BaseNativeTypedef2:
name: BaseNativeTypedef2
dartName: DartBaseNativeTypedef1
c:base.h@T@BaseNativeTypedef3:
name: BaseNativeTypedef3
dartName: DartBaseNativeTypedef1
c:base.h@T@BaseTypedef1:
name: BaseTypedef1
c:base.h@T@BaseTypedef2:
name: BaseTypedef2
c:base.h@T@TestInt:
name: TestInt
dartName: DartTestInt
13 changes: 9 additions & 4 deletions pkgs/ffigen/lib/src/code_generator/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,26 @@ class Writer {
strings.ffiNative: usesFfiNative,
},
strings.symbols: {
for (final b in bindings) b.usr: makeSymbolMapValue(b),
for (final b in bindings) b.usr: _makeSymbolMapValue(b),
},
},
},
};
}

Map<String, String> makeSymbolMapValue(Binding b) {
Map<String, String> _makeSymbolMapValue(Binding b) {
final dartName = b is Typealias ? getTypedefDartAliasName(b) : null;
return {
strings.name: b.name,
if (b is Typealias && b.dartAliasName != null)
strings.dartName: b.dartAliasName!,
if (dartName != null) strings.dartName: dartName,
};
}

String? getTypedefDartAliasName(Type b) {
if (b is! Typealias) return null;
return b.dartAliasName ?? getTypedefDartAliasName(b.type);
}

static String _objcImport(String entryPoint, String outDir) {
final frameworkHeader = parseObjCFrameworkHeader(entryPoint);

Expand Down

0 comments on commit 7991fee

Please sign in to comment.