Skip to content

Commit

Permalink
[vm/ffi] Refactor CFE transform to compound
Browse files Browse the repository at this point in the history
In preparation of having `Union` besides `Struct`, renames all mentions
of `Struct` to `Compound`.

Also, changes the type checking to have `allowXYZ` named arguments
always default to false, and remove redundant arguments.

Bug: #38491

tools/test.py ffi ffi_2
TEST=tests/ffi(_2)/(.*)by_value_(*.)_test.dart

Change-Id: Ie5f7cf4189dc315f896e3b3933ff0e0580ac540f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194424
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Apr 13, 2021
1 parent 0889552 commit c57ca14
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 159 deletions.
23 changes: 12 additions & 11 deletions pkg/vm/lib/transformations/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class FfiTransformer extends Transformer {
final Field arraySizeDimension5Field;
final Field arraySizeDimensionsField;
final Class pointerClass;
final Class compoundClass;
final Class structClass;
final Class ffiStructLayoutClass;
final Field ffiStructLayoutTypesField;
Expand Down Expand Up @@ -330,6 +331,7 @@ class FfiTransformer extends Transformer {
arraySizeDimensionsField =
index.getMember('dart:ffi', '_ArraySize', 'dimensions'),
pointerClass = index.getClass('dart:ffi', 'Pointer'),
compoundClass = index.getClass('dart:ffi', '_Compound'),
structClass = index.getClass('dart:ffi', 'Struct'),
ffiStructLayoutClass = index.getClass('dart:ffi', '_FfiStructLayout'),
ffiStructLayoutTypesField =
Expand Down Expand Up @@ -459,8 +461,7 @@ class FfiTransformer extends Transformer {
/// [NativeFunction]<T1 Function(T2, T3) -> S1 Function(S2, S3)
/// where DartRepresentationOf(Tn) -> Sn
DartType convertNativeTypeToDartType(DartType nativeType,
{bool allowStructs = false,
bool allowStructItself = false,
{bool allowCompounds = false,
bool allowHandle = false,
bool allowInlineArray = false}) {
if (nativeType is! InterfaceType) {
Expand All @@ -476,11 +477,11 @@ class FfiTransformer extends Transformer {
}
return nativeType;
}
if (hierarchy.isSubclassOf(nativeClass, structClass)) {
if (structClass == nativeClass) {
return allowStructItself ? nativeType : null;
if (hierarchy.isSubclassOf(nativeClass, compoundClass)) {
if (nativeClass == structClass) {
return null;
}
return allowStructs ? nativeType : null;
return allowCompounds ? nativeType : null;
}
if (nativeType_ == null) {
return null;
Expand Down Expand Up @@ -514,11 +515,11 @@ class FfiTransformer extends Transformer {
if (fun.typeParameters.length != 0) return null;

final DartType returnType = convertNativeTypeToDartType(fun.returnType,
allowStructs: allowStructs, allowHandle: true);
allowCompounds: true, allowHandle: true);
if (returnType == null) return null;
final List<DartType> argumentTypes = fun.positionalParameters
.map((t) => convertNativeTypeToDartType(t,
allowStructs: allowStructs, allowHandle: true))
allowCompounds: true, allowHandle: true))
.toList();
if (argumentTypes.contains(null)) return null;
return FunctionType(argumentTypes, returnType, Nullability.legacy);
Expand Down Expand Up @@ -740,7 +741,7 @@ class FfiTransformer extends Transformer {
return dimensions;
}

bool isStructSubtype(DartType type) {
bool isCompoundSubtype(DartType type) {
if (type is InvalidType) {
return false;
}
Expand All @@ -762,9 +763,9 @@ class FfiTransformer extends Transformer {
class FfiTransformerData {
final Map<Field, Procedure> replacedGetters;
final Map<Field, Procedure> replacedSetters;
final Set<Class> emptyStructs;
final Set<Class> emptyCompounds;
FfiTransformerData(
this.replacedGetters, this.replacedSetters, this.emptyStructs);
this.replacedGetters, this.replacedSetters, this.emptyCompounds);
}

/// Checks if any library depends on dart:ffi.
Expand Down
Loading

0 comments on commit c57ca14

Please sign in to comment.