Skip to content

Commit 21b2bf7

Browse files
authored
[ffigen] Remove BindingsIndex._declaredTypes (#2846)
1 parent e46b07f commit 21b2bf7

File tree

8 files changed

+109
-114
lines changed

8 files changed

+109
-114
lines changed

pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ Compound? _parseCompoundDeclaration(
123123
constructor,
124124
) {
125125
// Parse the cursor definition instead, if this is a forward declaration.
126-
final declUsr = cursor.usr();
126+
final usr = cursor.usr();
127+
128+
final cachedCompound = context.bindingsIndex.getSeenCompound(usr);
129+
if (cachedCompound != null) return cachedCompound;
130+
127131
final String declName;
128132

129133
// Only set name using USR if the type is not Anonymous (A struct is anonymous
@@ -132,7 +136,7 @@ Compound? _parseCompoundDeclaration(
132136
if (clang.clang_Cursor_isAnonymous(cursor) == 0) {
133137
// This gives the significant name, i.e name of the struct if defined or
134138
// name of the first typedef declaration that refers to it.
135-
declName = declUsr.split('@').last;
139+
declName = usr.split('@').last;
136140
} else {
137141
// Empty names are treated as inline declarations.
138142
declName = '';
@@ -144,12 +148,13 @@ Compound? _parseCompoundDeclaration(
144148
return null;
145149
}
146150

147-
final decl = Declaration(usr: declUsr, originalName: declName);
151+
final decl = Declaration(usr: usr, originalName: declName);
152+
final Compound compound;
148153
if (declName.isEmpty) {
149154
cursor = context.cursorIndex.getDefinition(cursor);
150-
return constructor(
155+
compound = constructor(
151156
name: 'Unnamed$className',
152-
usr: declUsr,
157+
usr: usr,
153158
dartDoc: getCursorDocComment(
154159
context,
155160
cursor,
@@ -163,8 +168,8 @@ Compound? _parseCompoundDeclaration(
163168
context.logger.fine(
164169
'++++ Adding $className: Name: $declName, ${cursor.completeStringRepr()}',
165170
);
166-
return constructor(
167-
usr: declUsr,
171+
compound = constructor(
172+
usr: usr,
168173
originalName: declName,
169174
name: configDecl.rename(decl),
170175
dartDoc: getCursorDocComment(
@@ -176,6 +181,8 @@ Compound? _parseCompoundDeclaration(
176181
nativeType: cursor.type().spelling(),
177182
);
178183
}
184+
context.bindingsIndex.addCompoundToSeen(usr, compound);
185+
return compound;
179186
}
180187

181188
void fillCompoundMembersIfNeeded(

pkgs/ffigen/lib/src/header_parser/sub_parsers/enumdecl_parser.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ import 'unnamed_enumdecl_parser.dart';
2424
// Parse the cursor definition instead, if this is a forward declaration.
2525
cursor = context.cursorIndex.getDefinition(cursor);
2626

27-
final enumUsr = cursor.usr();
27+
final usr = cursor.usr();
28+
29+
final cachedEnum = context.bindingsIndex.getSeenEnum(usr);
30+
if (cachedEnum != null) return (cachedEnum, cachedEnum.nativeType);
31+
2832
final String enumName;
2933
// Only set name using USR if the type is not Anonymous (i.e not inside
3034
// any typedef and declared inplace inside another type).
3135
if (clang.clang_Cursor_isAnonymous(cursor) == 0) {
3236
// This gives the significant name, i.e name of the enum if defined or
3337
// name of the first typedef declaration that refers to it.
34-
enumName = enumUsr.split('@').last;
38+
enumName = usr.split('@').last;
3539
} else {
3640
enumName = '';
3741
}
@@ -51,7 +55,7 @@ import 'unnamed_enumdecl_parser.dart';
5155
return (null, nativeType);
5256
}
5357

54-
final decl = Declaration(usr: enumUsr, originalName: enumName);
58+
final decl = Declaration(usr: usr, originalName: enumName);
5559
if (enumName.isEmpty) {
5660
logger.fine('Saving anonymous enum.');
5761
final addedConstants = saveUnNamedEnum(context, cursor);
@@ -61,7 +65,7 @@ import 'unnamed_enumdecl_parser.dart';
6165
} else {
6266
logger.fine('++++ Adding Enum: ${cursor.completeStringRepr()}');
6367
enumClass = EnumClass(
64-
usr: enumUsr,
68+
usr: usr,
6569
dartDoc: getCursorDocComment(
6670
context,
6771
cursor,
@@ -111,12 +115,13 @@ import 'unnamed_enumdecl_parser.dart';
111115
});
112116
final suggestedStyle = isNSOptions ? EnumStyle.intConstants : null;
113117
enumClass.style = config.enums.style(decl, suggestedStyle);
118+
context.bindingsIndex.addEnumToSeen(usr, enumClass);
114119
}
115120

116121
if (hasNegativeEnumConstants) {
117122
// Change enum native type to signed type.
118123
logger.fine(
119-
'For enum $enumUsr - using signed type for $nativeType : '
124+
'For enum $usr - using signed type for $nativeType : '
120125
'${unsignedToSignedNativeIntType[nativeType]}',
121126
);
122127
nativeType = unsignedToSignedNativeIntType[nativeType] ?? nativeType;

pkgs/ffigen/lib/src/header_parser/sub_parsers/objcinterfacedecl_parser.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ Type? parseObjCInterfaceDeclaration(
1818
Context context,
1919
clang_types.CXCursor cursor,
2020
) {
21-
final itfUsr = cursor.usr();
22-
final itfName = cursor.spelling();
23-
final decl = Declaration(usr: itfUsr, originalName: itfName);
21+
final usr = cursor.usr();
22+
23+
final cachedItf = context.bindingsIndex.getSeenObjCInterface(usr);
24+
if (cachedItf != null) return cachedItf;
25+
26+
final name = cursor.spelling();
27+
final decl = Declaration(usr: usr, originalName: name);
2428
final apiAvailability = ApiAvailability.fromCursor(cursor, context);
2529

2630
final config = context.config;
@@ -31,23 +35,25 @@ Type? parseObjCInterfaceDeclaration(
3135

3236
context.logger.fine(
3337
'++++ Adding ObjC interface: '
34-
'Name: $itfName, ${cursor.completeStringRepr()}',
38+
'Name: $name, ${cursor.completeStringRepr()}',
3539
);
3640

37-
return ObjCInterface(
41+
final itf = ObjCInterface(
3842
context: context,
39-
usr: itfUsr,
40-
originalName: itfName,
43+
usr: usr,
44+
originalName: name,
4145
name: objcInterfaces.rename(decl),
42-
lookupName: applyModulePrefix(itfName, objcInterfaces.module(decl)),
46+
lookupName: applyModulePrefix(name, objcInterfaces.module(decl)),
4347
dartDoc: getCursorDocComment(
4448
context,
4549
cursor,
46-
fallbackComment: itfName,
50+
fallbackComment: name,
4751
availability: apiAvailability.dartDoc,
4852
),
4953
apiAvailability: apiAvailability,
5054
);
55+
context.bindingsIndex.addObjCInterfaceToSeen(usr, itf);
56+
return itf;
5157
}
5258

5359
void fillObjCInterfaceMethodsIfNeeded(

pkgs/ffigen/lib/src/header_parser/sub_parsers/typedefdecl_parser.dart

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ Typealias? parseTypedefDeclaration(
3636
final logger = context.logger;
3737
final config = context.config;
3838
final bindingsIndex = context.bindingsIndex;
39-
final typedefName = cursor.spelling();
40-
final typedefUsr = cursor.usr();
41-
final decl = Declaration(usr: typedefUsr, originalName: typedefName);
39+
final name = cursor.spelling();
40+
final usr = cursor.usr();
41+
42+
final cachedType = bindingsIndex.getSeenTypealias(usr);
43+
if (cachedType != null) return cachedType;
44+
45+
final decl = Declaration(usr: usr, originalName: name);
4246
final ct = clang.clang_getTypedefDeclUnderlyingType(cursor);
4347
final s = getCodeGenType(
4448
context,
@@ -47,46 +51,48 @@ Typealias? parseTypedefDeclaration(
4751
originalCursor: cursor,
4852
);
4953

50-
if (bindingsIndex.isSeenUnsupportedTypealias(typedefUsr)) {
54+
if (bindingsIndex.isSeenUnsupportedTypealias(usr)) {
5155
// Do not process unsupported typealiases again.
5256
} else if (s is UnimplementedType) {
5357
logger.fine(
54-
"Skipped Typedef '$typedefName': "
58+
"Skipped Typedef '$name': "
5559
'Unimplemented type referred.',
5660
);
57-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
58-
} else if (s is Compound && s.originalName == typedefName) {
61+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
62+
} else if (s is Compound && s.originalName == name) {
5963
// Ignore typedef if it refers to a compound with the same original name.
60-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
64+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
6165
logger.fine(
62-
"Skipped Typedef '$typedefName': "
66+
"Skipped Typedef '$name': "
6367
'Name matches with referred struct/union.',
6468
);
6569
} else if (s is EnumClass) {
6670
// Ignore typedefs to Enum.
67-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
68-
logger.fine("Skipped Typedef '$typedefName': typedef to enum.");
71+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
72+
logger.fine("Skipped Typedef '$name': typedef to enum.");
6973
} else if (s is HandleType) {
7074
// Ignore typedefs to Handle.
71-
logger.fine("Skipped Typedef '$typedefName': typedef to Dart Handle.");
72-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
75+
logger.fine("Skipped Typedef '$name': typedef to Dart Handle.");
76+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
7377
} else if (s is ConstantArray || s is IncompleteArray) {
7478
// Ignore typedefs to Constant Array.
75-
logger.fine("Skipped Typedef '$typedefName': typedef to array.");
76-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
79+
logger.fine("Skipped Typedef '$name': typedef to array.");
80+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
7781
} else if (s is BooleanType) {
7882
// Ignore typedefs to Boolean.
79-
logger.fine("Skipped Typedef '$typedefName': typedef to bool.");
80-
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
83+
logger.fine("Skipped Typedef '$name': typedef to bool.");
84+
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
8185
} else {
8286
// Create typealias.
83-
return Typealias(
84-
usr: typedefUsr,
85-
originalName: typedefName,
87+
final type = Typealias(
88+
usr: usr,
89+
originalName: name,
8690
name: config.typedefs.rename(decl),
8791
type: s,
8892
dartDoc: getCursorDocComment(context, cursor),
8993
);
94+
bindingsIndex.addTypealiasToSeen(usr, type);
95+
return type;
9096
}
9197
return null;
9298
}

0 commit comments

Comments
 (0)