Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ Compound? _parseCompoundDeclaration(
constructor,
) {
// Parse the cursor definition instead, if this is a forward declaration.
final declUsr = cursor.usr();
final usr = cursor.usr();

final cachedCompound = context.bindingsIndex.getSeenCompound(usr);
if (cachedCompound != null) return cachedCompound;

final String declName;

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

final decl = Declaration(usr: declUsr, originalName: declName);
final decl = Declaration(usr: usr, originalName: declName);
final Compound compound;
if (declName.isEmpty) {
cursor = context.cursorIndex.getDefinition(cursor);
return constructor(
compound = constructor(
name: 'Unnamed$className',
usr: declUsr,
usr: usr,
dartDoc: getCursorDocComment(
context,
cursor,
Expand All @@ -163,8 +168,8 @@ Compound? _parseCompoundDeclaration(
context.logger.fine(
'++++ Adding $className: Name: $declName, ${cursor.completeStringRepr()}',
);
return constructor(
usr: declUsr,
compound = constructor(
usr: usr,
originalName: declName,
name: configDecl.rename(decl),
dartDoc: getCursorDocComment(
Expand All @@ -176,6 +181,8 @@ Compound? _parseCompoundDeclaration(
nativeType: cursor.type().spelling(),
);
}
context.bindingsIndex.addCompoundToSeen(usr, compound);
return compound;
}

void fillCompoundMembersIfNeeded(
Expand Down
15 changes: 10 additions & 5 deletions pkgs/ffigen/lib/src/header_parser/sub_parsers/enumdecl_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ import 'unnamed_enumdecl_parser.dart';
// Parse the cursor definition instead, if this is a forward declaration.
cursor = context.cursorIndex.getDefinition(cursor);

final enumUsr = cursor.usr();
final usr = cursor.usr();

final cachedEnum = context.bindingsIndex.getSeenEnum(usr);
if (cachedEnum != null) return (cachedEnum, cachedEnum.nativeType);

final String enumName;
// Only set name using USR if the type is not Anonymous (i.e not inside
// any typedef and declared inplace inside another type).
if (clang.clang_Cursor_isAnonymous(cursor) == 0) {
// This gives the significant name, i.e name of the enum if defined or
// name of the first typedef declaration that refers to it.
enumName = enumUsr.split('@').last;
enumName = usr.split('@').last;
} else {
enumName = '';
}
Expand All @@ -51,7 +55,7 @@ import 'unnamed_enumdecl_parser.dart';
return (null, nativeType);
}

final decl = Declaration(usr: enumUsr, originalName: enumName);
final decl = Declaration(usr: usr, originalName: enumName);
if (enumName.isEmpty) {
logger.fine('Saving anonymous enum.');
final addedConstants = saveUnNamedEnum(context, cursor);
Expand All @@ -61,7 +65,7 @@ import 'unnamed_enumdecl_parser.dart';
} else {
logger.fine('++++ Adding Enum: ${cursor.completeStringRepr()}');
enumClass = EnumClass(
usr: enumUsr,
usr: usr,
dartDoc: getCursorDocComment(
context,
cursor,
Expand Down Expand Up @@ -111,12 +115,13 @@ import 'unnamed_enumdecl_parser.dart';
});
final suggestedStyle = isNSOptions ? EnumStyle.intConstants : null;
enumClass.style = config.enums.style(decl, suggestedStyle);
context.bindingsIndex.addEnumToSeen(usr, enumClass);
}

if (hasNegativeEnumConstants) {
// Change enum native type to signed type.
logger.fine(
'For enum $enumUsr - using signed type for $nativeType : '
'For enum $usr - using signed type for $nativeType : '
'${unsignedToSignedNativeIntType[nativeType]}',
);
nativeType = unsignedToSignedNativeIntType[nativeType] ?? nativeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ Type? parseObjCInterfaceDeclaration(
Context context,
clang_types.CXCursor cursor,
) {
final itfUsr = cursor.usr();
final itfName = cursor.spelling();
final decl = Declaration(usr: itfUsr, originalName: itfName);
final usr = cursor.usr();

final cachedItf = context.bindingsIndex.getSeenObjCInterface(usr);
if (cachedItf != null) return cachedItf;

final name = cursor.spelling();
final decl = Declaration(usr: usr, originalName: name);
final apiAvailability = ApiAvailability.fromCursor(cursor, context);

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

context.logger.fine(
'++++ Adding ObjC interface: '
'Name: $itfName, ${cursor.completeStringRepr()}',
'Name: $name, ${cursor.completeStringRepr()}',
);

return ObjCInterface(
final itf = ObjCInterface(
context: context,
usr: itfUsr,
originalName: itfName,
usr: usr,
originalName: name,
name: objcInterfaces.rename(decl),
lookupName: applyModulePrefix(itfName, objcInterfaces.module(decl)),
lookupName: applyModulePrefix(name, objcInterfaces.module(decl)),
dartDoc: getCursorDocComment(
context,
cursor,
fallbackComment: itfName,
fallbackComment: name,
availability: apiAvailability.dartDoc,
),
apiAvailability: apiAvailability,
);
context.bindingsIndex.addObjCInterfaceToSeen(usr, itf);
return itf;
}

void fillObjCInterfaceMethodsIfNeeded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ Typealias? parseTypedefDeclaration(
final logger = context.logger;
final config = context.config;
final bindingsIndex = context.bindingsIndex;
final typedefName = cursor.spelling();
final typedefUsr = cursor.usr();
final decl = Declaration(usr: typedefUsr, originalName: typedefName);
final name = cursor.spelling();
final usr = cursor.usr();

final cachedType = bindingsIndex.getSeenTypealias(usr);
if (cachedType != null) return cachedType;

final decl = Declaration(usr: usr, originalName: name);
final ct = clang.clang_getTypedefDeclUnderlyingType(cursor);
final s = getCodeGenType(
context,
Expand All @@ -47,46 +51,48 @@ Typealias? parseTypedefDeclaration(
originalCursor: cursor,
);

if (bindingsIndex.isSeenUnsupportedTypealias(typedefUsr)) {
if (bindingsIndex.isSeenUnsupportedTypealias(usr)) {
// Do not process unsupported typealiases again.
} else if (s is UnimplementedType) {
logger.fine(
"Skipped Typedef '$typedefName': "
"Skipped Typedef '$name': "
'Unimplemented type referred.',
);
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
} else if (s is Compound && s.originalName == typedefName) {
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
} else if (s is Compound && s.originalName == name) {
// Ignore typedef if it refers to a compound with the same original name.
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
logger.fine(
"Skipped Typedef '$typedefName': "
"Skipped Typedef '$name': "
'Name matches with referred struct/union.',
);
} else if (s is EnumClass) {
// Ignore typedefs to Enum.
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
logger.fine("Skipped Typedef '$typedefName': typedef to enum.");
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
logger.fine("Skipped Typedef '$name': typedef to enum.");
} else if (s is HandleType) {
// Ignore typedefs to Handle.
logger.fine("Skipped Typedef '$typedefName': typedef to Dart Handle.");
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
logger.fine("Skipped Typedef '$name': typedef to Dart Handle.");
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
} else if (s is ConstantArray || s is IncompleteArray) {
// Ignore typedefs to Constant Array.
logger.fine("Skipped Typedef '$typedefName': typedef to array.");
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
logger.fine("Skipped Typedef '$name': typedef to array.");
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
} else if (s is BooleanType) {
// Ignore typedefs to Boolean.
logger.fine("Skipped Typedef '$typedefName': typedef to bool.");
bindingsIndex.addUnsupportedTypealiasToSeen(typedefUsr);
logger.fine("Skipped Typedef '$name': typedef to bool.");
bindingsIndex.addUnsupportedTypealiasToSeen(usr);
} else {
// Create typealias.
return Typealias(
usr: typedefUsr,
originalName: typedefName,
final type = Typealias(
usr: usr,
originalName: name,
name: config.typedefs.rename(decl),
type: s,
dartDoc: getCursorDocComment(context, cursor),
);
bindingsIndex.addTypealiasToSeen(usr, type);
return type;
}
return null;
}
Loading
Loading