Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 22.1.0

* Allows generation of classes that aren't referenced in an API.

## 22.0.0

* [dart] Changes codec to send int64 instead of int32.
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ class Class extends Node {
Class({
required this.name,
required this.fields,
this.isReferenced = true,
this.isSwiftClass = false,
this.documentationComments = const <String>[],
});
Expand All @@ -662,6 +663,9 @@ class Class extends Node {
/// All the fields contained in the class.
List<NamedType> fields;

/// Whether the class is referenced in any API.
bool isReferenced;

/// Determines whether the defined class should be represented as a struct or
/// a class in Swift generation.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '22.0.0';
const String pigeonVersion = '22.1.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
17 changes: 9 additions & 8 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1393,23 +1393,24 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
getReferencedTypes(_apis, _classes);
final Set<String> referencedTypeNames =
referencedTypes.keys.map((TypeDeclaration e) => e.baseName).toSet();
final List<Class> referencedClasses = List<Class>.from(_classes);
referencedClasses
.removeWhere((Class x) => !referencedTypeNames.contains(x.name));
final List<Class> nonReferencedClasses = List<Class>.from(_classes);
nonReferencedClasses
.removeWhere((Class x) => referencedTypeNames.contains(x.name));
for (final Class x in nonReferencedClasses) {
x.isReferenced = false;
}

final List<Enum> referencedEnums = List<Enum>.from(_enums);
final Root completeRoot =
Root(apis: _apis, classes: referencedClasses, enums: referencedEnums);
Root(apis: _apis, classes: _classes, enums: referencedEnums);

final List<Error> validateErrors = _validateAst(completeRoot, source);
final List<Error> totalErrors = List<Error>.from(_errors);
totalErrors.addAll(validateErrors);

for (final MapEntry<TypeDeclaration, List<int>> element
in referencedTypes.entries) {
if (!referencedClasses
.map((Class e) => e.name)
.contains(element.key.baseName) &&
if (!_classes.map((Class e) => e.name).contains(element.key.baseName) &&
!referencedEnums
.map((Enum e) => e.name)
.contains(element.key.baseName) &&
Expand All @@ -1430,7 +1431,7 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
lineNumber: lineNumber));
}
}
for (final Class classDefinition in referencedClasses) {
for (final Class classDefinition in _classes) {
classDefinition.fields = _attachAssociatedDefinitions(
classDefinition.fields,
);
Expand Down
7 changes: 7 additions & 0 deletions packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ enum AnotherEnum {
justInCase,
}

// This exists to show that unused data classes still generate.
class UnusedClass {
UnusedClass({this.aField});

Object? aField;
}

class SimpleClass {
SimpleClass({
this.aString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,156 @@ public enum AnotherEnum {
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class UnusedClass {
private @Nullable Object aField;

public @Nullable Object getAField() {
return aField;
}

public void setAField(@Nullable Object setterArg) {
this.aField = setterArg;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UnusedClass that = (UnusedClass) o;
return Objects.equals(aField, that.aField);
}

@Override
public int hashCode() {
return Objects.hash(aField);
}

public static final class Builder {

private @Nullable Object aField;

@CanIgnoreReturnValue
public @NonNull Builder setAField(@Nullable Object setterArg) {
this.aField = setterArg;
return this;
}

public @NonNull UnusedClass build() {
UnusedClass pigeonReturn = new UnusedClass();
pigeonReturn.setAField(aField);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<>(1);
toListResult.add(aField);
return toListResult;
}

static @NonNull UnusedClass fromList(@NonNull ArrayList<Object> pigeonVar_list) {
UnusedClass pigeonResult = new UnusedClass();
Object aField = pigeonVar_list.get(0);
pigeonResult.setAField(aField);
return pigeonResult;
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class SimpleClass {
private @Nullable String aString;

public @Nullable String getAString() {
return aString;
}

public void setAString(@Nullable String setterArg) {
this.aString = setterArg;
}

private @NonNull Boolean aBool;

public @NonNull Boolean getABool() {
return aBool;
}

public void setABool(@NonNull Boolean setterArg) {
if (setterArg == null) {
throw new IllegalStateException("Nonnull field \"aBool\" is null.");
}
this.aBool = setterArg;
}

/** Constructor is non-public to enforce null safety; use Builder. */
SimpleClass() {}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SimpleClass that = (SimpleClass) o;
return Objects.equals(aString, that.aString) && aBool.equals(that.aBool);
}

@Override
public int hashCode() {
return Objects.hash(aString, aBool);
}

public static final class Builder {

private @Nullable String aString;

@CanIgnoreReturnValue
public @NonNull Builder setAString(@Nullable String setterArg) {
this.aString = setterArg;
return this;
}

private @Nullable Boolean aBool;

@CanIgnoreReturnValue
public @NonNull Builder setABool(@NonNull Boolean setterArg) {
this.aBool = setterArg;
return this;
}

public @NonNull SimpleClass build() {
SimpleClass pigeonReturn = new SimpleClass();
pigeonReturn.setAString(aString);
pigeonReturn.setABool(aBool);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<>(2);
toListResult.add(aString);
toListResult.add(aBool);
return toListResult;
}

static @NonNull SimpleClass fromList(@NonNull ArrayList<Object> pigeonVar_list) {
SimpleClass pigeonResult = new SimpleClass();
Object aString = pigeonVar_list.get(0);
pigeonResult.setAString((String) aString);
Object aBool = pigeonVar_list.get(1);
pigeonResult.setABool((Boolean) aBool);
return pigeonResult;
}
}

/**
* A class containing all supported types.
*
Expand Down Expand Up @@ -2043,14 +2193,18 @@ protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
return value == null ? null : AnotherEnum.values()[((Long) value).intValue()];
}
case (byte) 131:
return AllTypes.fromList((ArrayList<Object>) readValue(buffer));
return UnusedClass.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 132:
return AllNullableTypes.fromList((ArrayList<Object>) readValue(buffer));
return SimpleClass.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 133:
return AllNullableTypesWithoutRecursion.fromList((ArrayList<Object>) readValue(buffer));
return AllTypes.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 134:
return AllClassesWrapper.fromList((ArrayList<Object>) readValue(buffer));
return AllNullableTypes.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 135:
return AllNullableTypesWithoutRecursion.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 136:
return AllClassesWrapper.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 137:
return TestMessage.fromList((ArrayList<Object>) readValue(buffer));
default:
return super.readValueOfType(type, buffer);
Expand All @@ -2065,20 +2219,26 @@ protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
} else if (value instanceof AnotherEnum) {
stream.write(130);
writeValue(stream, value == null ? null : ((AnotherEnum) value).index);
} else if (value instanceof AllTypes) {
} else if (value instanceof UnusedClass) {
stream.write(131);
writeValue(stream, ((UnusedClass) value).toList());
} else if (value instanceof SimpleClass) {
stream.write(132);
writeValue(stream, ((SimpleClass) value).toList());
} else if (value instanceof AllTypes) {
stream.write(133);
writeValue(stream, ((AllTypes) value).toList());
} else if (value instanceof AllNullableTypes) {
stream.write(132);
stream.write(134);
writeValue(stream, ((AllNullableTypes) value).toList());
} else if (value instanceof AllNullableTypesWithoutRecursion) {
stream.write(133);
stream.write(135);
writeValue(stream, ((AllNullableTypesWithoutRecursion) value).toList());
} else if (value instanceof AllClassesWrapper) {
stream.write(134);
stream.write(136);
writeValue(stream, ((AllClassesWrapper) value).toList());
} else if (value instanceof TestMessage) {
stream.write(135);
stream.write(137);
writeValue(stream, ((TestMessage) value).toList());
} else {
super.writeValue(stream, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,27 @@ typedef NS_ENUM(NSUInteger, FLTAnotherEnum) {
- (instancetype)initWithValue:(FLTAnotherEnum)value;
@end

@class FLTUnusedClass;
@class FLTSimpleClass;
@class FLTAllTypes;
@class FLTAllNullableTypes;
@class FLTAllNullableTypesWithoutRecursion;
@class FLTAllClassesWrapper;
@class FLTTestMessage;

@interface FLTUnusedClass : NSObject
+ (instancetype)makeWithAField:(nullable id)aField;
@property(nonatomic, strong, nullable) id aField;
@end

@interface FLTSimpleClass : NSObject
/// `init` unavailable to enforce nonnull fields, see the `make` class method.
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)makeWithAString:(nullable NSString *)aString aBool:(BOOL)aBool;
@property(nonatomic, copy, nullable) NSString *aString;
@property(nonatomic, assign) BOOL aBool;
@end

/// A class containing all supported types.
@interface FLTAllTypes : NSObject
/// `init` unavailable to enforce nonnull fields, see the `make` class method.
Expand Down
Loading