Skip to content

Commit

Permalink
[swiftgen] Added models to represent Swift AST (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad3id committed Jun 4, 2024
1 parent 00fd6b9 commit 0f90ac4
Show file tree
Hide file tree
Showing 20 changed files with 725 additions and 1 deletion.
27 changes: 27 additions & 0 deletions pkgs/swiftgen/swift2objc/LICENSE
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.
2 changes: 1 addition & 1 deletion pkgs/swiftgen/swift2objc/README.md
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.
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;
}
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;
}
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;
}
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 {}
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;
}
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;
}
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 pkgs/swiftgen/swift2objc/lib/src/ast/_core/shared/parameter.dart
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,
});
}
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,
});
}
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,
});
}
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,
});
}
Loading

0 comments on commit 0f90ac4

Please sign in to comment.