-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[swiftgen] Added models to represent Swift AST (#1140)
- Loading branch information
1 parent
00fd6b9
commit 0f90ac4
Showing
20 changed files
with
725 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright 2024, the Dart project authors. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
* Neither the name of Google LLC nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Swift2ObjC | ||
|
||
An experimental tool for generating bindings that allow interop between ObjC and | ||
Swift code. | ||
Swift code. |
31 changes: 31 additions & 0 deletions
31
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/compund_declaration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'confomingable.dart'; | ||
import 'declaration.dart'; | ||
import 'genericable.dart'; | ||
import '../shared/referred_type.dart'; | ||
import 'paramable.dart'; | ||
|
||
/// An interface for the declaration of all compound Swift entities. See `ClassDeclaration`, | ||
/// `StructDeclaration` and `ProtocolDeclaration` for concrete implementations. | ||
abstract interface class CompoundDeclaration | ||
implements Declaration, Genericable, Conformingable { | ||
abstract List<CompoundPropertyDeclaration> properties; | ||
abstract List<CompoundMethodDeclaration> methods; | ||
} | ||
|
||
/// An interface for a compound property. See `ClassPropertyDeclaration`, | ||
/// `StructPropertyDeclaration` and `ProtocolPropertyDeclaration` for concrete implementations. | ||
abstract interface class CompoundPropertyDeclaration implements Declaration { | ||
abstract bool hasSetter; | ||
abstract ReferredType type; | ||
} | ||
|
||
/// An interface for a compound method. See `ClassMethodDeclaration`, | ||
/// `StructMethodDeclaration` and `ProtocolMethodDeclaration` for concrete implementations. | ||
abstract interface class CompoundMethodDeclaration | ||
implements Declaration, Genericable, Paramable { | ||
abstract ReferredType returnType; | ||
} |
11 changes: 11 additions & 0 deletions
11
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/confomingable.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../../declarations/compounds/protocol_declaration.dart'; | ||
import '../shared/referred_type.dart'; | ||
|
||
/// An interface to describe a Swift entity's ability to confom to protocols. | ||
abstract interface class Conformingable { | ||
abstract List<DeclaredType<ProtocolDeclaration>> conformedProtocols; | ||
} |
9 changes: 9 additions & 0 deletions
9
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/declaration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// A common interface for all Swift entities declarations. | ||
abstract interface class Declaration { | ||
abstract String id; | ||
abstract String name; | ||
} |
19 changes: 19 additions & 0 deletions
19
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/enum_declaration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'confomingable.dart'; | ||
import 'declaration.dart'; | ||
import 'genericable.dart'; | ||
|
||
/// An interface for describing the declaration of a Swift enum. See `NormalEnumDeclaration`, | ||
/// `AssociatedValueEnumDeclaration` and `RawValueEnumDeclaration` for concrete implementations. | ||
abstract interface class EnumDeclaration | ||
implements Declaration, Genericable, Conformingable { | ||
abstract List<EnumCase> cases; | ||
} | ||
|
||
|
||
/// An interface describing an enum case. See `NormalEnumCase`, `AssociatedValueEnumCase` | ||
/// and `RawValueEnumCase` for concrete implementations. | ||
abstract interface class EnumCase implements Declaration {} |
10 changes: 10 additions & 0 deletions
10
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/genericable.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../shared/referred_type.dart'; | ||
|
||
/// An interface to describe a Swift entity's ability to have generic parameters. | ||
abstract interface class Genericable { | ||
abstract List<GenericType> genericParams; | ||
} |
8 changes: 8 additions & 0 deletions
8
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/objc_annotatable.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// An interface to describe a Swift entity's ability to be annotated with `@objc`. | ||
abstract interface class ObjCAnnotatable { | ||
abstract bool hasObjCAnnotation; | ||
} |
10 changes: 10 additions & 0 deletions
10
pkgs/swiftgen/swift2objc/lib/src/ast/_core/interfaces/paramable.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../shared/parameter.dart'; | ||
|
||
/// An interface to describe a Swift entity's ability to have parameters (e.g functions). | ||
abstract interface class Paramable { | ||
abstract List<Parameter> params; | ||
} |
18 changes: 18 additions & 0 deletions
18
pkgs/swiftgen/swift2objc/lib/src/ast/_core/shared/parameter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'referred_type.dart'; | ||
|
||
/// Describes parameters of function-like entities (e.g methods). | ||
class Parameter { | ||
String name; | ||
String? internalName; | ||
ReferredType type; | ||
|
||
Parameter({ | ||
required this.name, | ||
required this.internalName, | ||
required this.type, | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
pkgs/swiftgen/swift2objc/lib/src/ast/_core/shared/referred_type.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../interfaces/declaration.dart'; | ||
|
||
/// Describes a type regerence in declaration of Swift entities (e.g a method return type). | ||
/// See `DeclaredType` and `GenericType` for concrete implementation. | ||
abstract class ReferredType { | ||
String id; | ||
|
||
ReferredType({required this.id}); | ||
} | ||
|
||
class DeclaredType<T extends Declaration> extends ReferredType { | ||
T declaration; | ||
List<ReferredType> genericParams; | ||
|
||
DeclaredType({ | ||
required super.id, | ||
required this.declaration, | ||
required this.genericParams, | ||
}); | ||
} | ||
|
||
class GenericType extends ReferredType { | ||
String name; | ||
|
||
GenericType({ | ||
required this.name, | ||
required super.id, | ||
}); | ||
} |
104 changes: 104 additions & 0 deletions
104
pkgs/swiftgen/swift2objc/lib/src/ast/declarations/compounds/class_declaration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../../_core/interfaces/compund_declaration.dart'; | ||
import '../../_core/interfaces/objc_annotatable.dart'; | ||
import '../../_core/shared/parameter.dart'; | ||
import '../../_core/shared/referred_type.dart'; | ||
import 'protocol_declaration.dart'; | ||
|
||
/// Describes the declaration of a Swift class. | ||
class ClassDeclaration implements CompoundDeclaration, ObjCAnnotatable { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
covariant List<ClassPropertyDeclaration> properties; | ||
|
||
@override | ||
covariant List<ClassMethodDeclaration> methods; | ||
|
||
@override | ||
List<DeclaredType<ProtocolDeclaration>> conformedProtocols; | ||
|
||
@override | ||
List<GenericType> genericParams; | ||
|
||
@override | ||
bool hasObjCAnnotation; | ||
|
||
DeclaredType<ClassDeclaration>? superClass; | ||
|
||
ClassDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.properties, | ||
required this.methods, | ||
required this.conformedProtocols, | ||
required this.genericParams, | ||
required this.hasObjCAnnotation, | ||
this.superClass, | ||
}); | ||
} | ||
|
||
/// Describes the declaration of a property in a Swift class. | ||
class ClassPropertyDeclaration | ||
implements CompoundPropertyDeclaration, ObjCAnnotatable { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
bool hasSetter; | ||
|
||
@override | ||
ReferredType type; | ||
|
||
@override | ||
bool hasObjCAnnotation; | ||
|
||
ClassPropertyDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.type, | ||
required this.hasSetter, | ||
required this.hasObjCAnnotation, | ||
}); | ||
} | ||
|
||
/// Describes the declaration of a method in a Swift class. | ||
class ClassMethodDeclaration | ||
implements CompoundMethodDeclaration, ObjCAnnotatable { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
List<Parameter> params; | ||
|
||
@override | ||
List<GenericType> genericParams; | ||
|
||
@override | ||
ReferredType returnType; | ||
|
||
@override | ||
bool hasObjCAnnotation; | ||
|
||
ClassMethodDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.params, | ||
required this.genericParams, | ||
required this.returnType, | ||
required this.hasObjCAnnotation, | ||
}); | ||
} |
85 changes: 85 additions & 0 deletions
85
pkgs/swiftgen/swift2objc/lib/src/ast/declarations/compounds/protocol_declaration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../../_core/interfaces/compund_declaration.dart'; | ||
import '../../_core/shared/parameter.dart'; | ||
import '../../_core/shared/referred_type.dart'; | ||
|
||
/// Describes the declaration of a Swift protocol. | ||
class ProtocolDeclaration implements CompoundDeclaration { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
covariant List<ProtocolPropertyDeclaration> properties; | ||
|
||
@override | ||
covariant List<ProtocolMethodDeclaration> methods; | ||
|
||
@override | ||
List<DeclaredType<ProtocolDeclaration>> conformedProtocols; | ||
|
||
@override | ||
List<GenericType> genericParams; | ||
|
||
ProtocolDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.properties, | ||
required this.methods, | ||
required this.conformedProtocols, | ||
required this.genericParams, | ||
}); | ||
} | ||
|
||
/// Describes the declaration of a property in a Swift protocol. | ||
class ProtocolPropertyDeclaration implements CompoundPropertyDeclaration { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
bool hasSetter; | ||
|
||
@override | ||
ReferredType type; | ||
|
||
ProtocolPropertyDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.type, | ||
required this.hasSetter, | ||
}); | ||
} | ||
|
||
/// Describes the declaration of a method in a Swift protocol. | ||
class ProtocolMethodDeclaration implements CompoundMethodDeclaration { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
@override | ||
List<Parameter> params; | ||
|
||
@override | ||
List<GenericType> genericParams; | ||
|
||
@override | ||
ReferredType returnType; | ||
|
||
ProtocolMethodDeclaration({ | ||
required this.id, | ||
required this.name, | ||
required this.params, | ||
required this.genericParams, | ||
required this.returnType, | ||
}); | ||
} |
Oops, something went wrong.