Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift2objc] Support Protocols #1832

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -4,13 +4,14 @@

import '../../_core/interfaces/compound_declaration.dart';
import '../../_core/interfaces/nestable_declaration.dart';
import '../../_core/interfaces/objc_annotatable.dart';
import '../../_core/shared/referred_type.dart';
import 'members/initializer_declaration.dart';
import 'members/method_declaration.dart';
import 'members/property_declaration.dart';

/// Describes the declaration of a Swift protocol.
class ProtocolDeclaration implements CompoundDeclaration {
class ProtocolDeclaration implements CompoundDeclaration, ObjCAnnotatable {
@override
String id;

Expand All @@ -23,9 +24,20 @@ class ProtocolDeclaration implements CompoundDeclaration {
@override
covariant List<MethodDeclaration> methods;

/// Only present if indicated with `@objc`
@override
List<PropertyDeclaration> optionalProperties;

/// Only present if indicated with `@objc`
@override
List<MethodDeclaration> optionalMethods;

@override
List<DeclaredType<ProtocolDeclaration>> conformedProtocols;

@override
bool hasObjCAnnotation;

@override
List<GenericType> typeParams;

Expand All @@ -46,7 +58,10 @@ class ProtocolDeclaration implements CompoundDeclaration {
required this.initializers,
required this.conformedProtocols,
required this.typeParams,
this.hasObjCAnnotation = false,
this.nestingParent,
this.nestedDeclarations = const [],
this.optionalMethods = const [],
this.optionalProperties = const [],
});
}
4 changes: 4 additions & 0 deletions pkgs/swift2objc/lib/src/parser/_core/parsed_symbolgraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class ParsedRelation {
}

enum ParsedRelationKind {
requirementOf,
defaultImplementationOf,
optionalRequirementOf,
conformsTo,
memberOf;

static final _supportedRelationKindsMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// BSD-style license that can be found in the LICENSE file.

import '../../../ast/_core/interfaces/compound_declaration.dart';
import '../../../ast/_core/interfaces/declaration.dart';
import '../../../ast/_core/interfaces/nestable_declaration.dart';
import '../../../ast/declarations/compounds/class_declaration.dart';
import '../../../ast/declarations/compounds/members/initializer_declaration.dart';
import '../../../ast/declarations/compounds/members/method_declaration.dart';
import '../../../ast/declarations/compounds/members/property_declaration.dart';
import '../../../ast/declarations/compounds/protocol_declaration.dart';
import '../../../ast/declarations/compounds/struct_declaration.dart';
import '../../_core/parsed_symbolgraph.dart';
import '../../_core/utils.dart';
Expand Down Expand Up @@ -107,3 +109,128 @@ StructDeclaration parseStructDeclaration(
symbolgraph,
);
}

// This won't work as there's more for a protocol declaration
// Placing this here as placeholder declaration
// TODO: Implement extensions before adding support for default implementations
// 5. protocol func with reimpl in extension -> requirementOf
// and defaultImplementationOf, swift.method
// 6. protocol var with reimpl in extension -> requirementOf
// and defaultImplementationOf, swift.property,
// TODO: Replace generics to associatedType and implement
ProtocolDeclaration parseProtocolDeclaration(
ParsedSymbol protocolSymbol,
ParsedSymbolgraph symbolgraph
) {
final compoundId = parseSymbolId(protocolSymbol.json);
final compoundRelations = symbolgraph.relations[compoundId] ?? [];

// construct protocol
final protocol = ProtocolDeclaration(
id: compoundId, name: parseSymbolName(protocolSymbol.json),
properties: [],
methods: [],
initializers: [],
conformedProtocols: [],
typeParams: []
);

// get optional member declarations if any
final optionalMemberDeclarations = compoundRelations.where((relation) {
final isOptionalRequirementRelation =
relation.kind == ParsedRelationKind.optionalRequirementOf;
final isMemberOfProtocol = relation.targetId == compoundId;
return isMemberOfProtocol && isOptionalRequirementRelation;
}).map((relation) {
final memberSymbol = symbolgraph.symbols[relation.sourceId];
if (memberSymbol == null) {
return null;
}
return tryParseDeclaration(memberSymbol, symbolgraph);
})
.nonNulls
.dedupeBy((decl) => decl.id)
.toList()
;

// get normal member declarations
final memberDeclarations = compoundRelations.where((relation) {
final isOptionalRequirementRelation =
relation.kind == ParsedRelationKind.requirementOf
&& relation.kind != ParsedRelationKind.optionalRequirementOf;
final isMemberOfProtocol = relation.targetId == compoundId;
return isMemberOfProtocol && isOptionalRequirementRelation;
}).map((relation) {
final memberSymbol = symbolgraph.symbols[relation.sourceId];
if (memberSymbol == null) {
return null;
}
return tryParseDeclaration(memberSymbol, symbolgraph);
})
.nonNulls
.dedupeBy((decl) => decl.id)
.toList()
;

// get conformed protocols
final conformedProtocolDeclarations = compoundRelations.where((relation) {
final isOptionalRequirementRelation =
relation.kind == ParsedRelationKind.conformsTo;
final isMemberOfProtocol = relation.targetId == compoundId;
return isMemberOfProtocol && isOptionalRequirementRelation;
}).map((relation) {
final memberSymbol = symbolgraph.symbols[relation.sourceId];
if (memberSymbol == null) {
return null;
}
var conformedDecl = tryParseDeclaration(memberSymbol, symbolgraph) as ProtocolDeclaration;
return conformedDecl.asDeclaredType;
})
.nonNulls
.dedupeBy((decl) => decl.id)
.toList()
;

// If the protocol has optional members, it must be annotated with `@objc`
if (optionalMemberDeclarations.isNotEmpty) {
protocol.hasObjCAnnotation = true;
}

protocol.methods.addAll(
memberDeclarations
.whereType<MethodDeclaration>()
.dedupeBy((m) => m.fullName),
);

protocol.properties.addAll(
memberDeclarations.whereType<PropertyDeclaration>(),
);

protocol.optionalMethods.addAll(
optionalMemberDeclarations
.whereType<MethodDeclaration>()
.dedupeBy((m) => m.fullName),
);

protocol.optionalProperties.addAll(
optionalMemberDeclarations.whereType<PropertyDeclaration>(),
);

protocol.conformedProtocols.addAll(
conformedProtocolDeclarations
);

protocol.initializers.addAll(
memberDeclarations
.whereType<InitializerDeclaration>()
.dedupeBy((m) => m.fullName),
);

protocol.nestedDeclarations.addAll(
memberDeclarations.whereType<NestableDeclaration>(),
);

protocol.nestedDeclarations.fillNestingParents(protocol);

return protocol;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Declaration parseDeclaration(
'swift.init' => parseInitializerDeclaration(symbolJson, symbolgraph),
'swift.func' => parseGlobalFunctionDeclaration(symbolJson, symbolgraph),
'swift.var' => parseGlobalVariableDeclaration(symbolJson, symbolgraph),
'swift.protocol' => parseProtocolDeclaration(parsedSymbol, symbolgraph),
_ => throw Exception(
'Symbol of type $symbolType is not implemented yet.',
),
Expand Down
Loading