Skip to content

Commit 8cc449a

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2wasm] Add utils for pragma reading and updating.
These will be used to manage pragmas for dynamic modules. Change-Id: I98615d3e553cf556115121f37ced02bb42cae47d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400903 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent 04d70b9 commit 8cc449a

File tree

2 files changed

+95
-66
lines changed

2 files changed

+95
-66
lines changed

pkg/dart2wasm/lib/record_class_generator.dart

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:kernel/ast.dart';
66
import 'package:kernel/core_types.dart';
77

88
import 'records.dart';
9+
import 'util.dart';
910

1011
/// Generates a class extending `Record` for each record shape in the
1112
/// [Component].
@@ -189,14 +190,6 @@ class _RecordClassGenerator {
189190
classes.putIfAbsent(shape, () => _generateClass(shape, id));
190191
}
191192

192-
/// Add a `@pragma('wasm:entry-point')` annotation to an annotatable.
193-
T _addWasmEntryPointPragma<T extends Annotatable>(T node) => node
194-
..addAnnotation(ConstantExpression(
195-
InstanceConstant(coreTypes.pragmaClass.reference, [], {
196-
coreTypes.pragmaName.fieldReference: StringConstant("wasm:entry-point"),
197-
coreTypes.pragmaOptions.fieldReference: NullConstant(),
198-
})));
199-
200193
Class _generateClass(RecordShape shape, int id) {
201194
final fields = _generateFields(shape);
202195

@@ -205,19 +198,22 @@ class _RecordClassGenerator {
205198
className = '${className}_${shape.names.join('_')}';
206199
}
207200

208-
final cls = _addWasmEntryPointPragma(Class(
209-
name: className,
210-
isAbstract: false,
211-
isAnonymousMixin: false,
212-
supertype: Supertype(coreTypes.recordClass, []),
213-
constructors: [_generateConstructor(shape, fields)],
214-
procedures: [
215-
_generateHashCode(fields, id),
216-
_generateToString(shape, fields),
217-
],
218-
fields: fields,
219-
fileUri: library.fileUri,
220-
));
201+
final cls = addWasmEntryPointPragma(
202+
Class(
203+
name: className,
204+
isAbstract: false,
205+
isAnonymousMixin: false,
206+
supertype: Supertype(coreTypes.recordClass, []),
207+
constructors: [_generateConstructor(shape, fields)],
208+
procedures: [
209+
_generateHashCode(fields, id),
210+
_generateToString(shape, fields),
211+
],
212+
fields: fields,
213+
fileUri: library.fileUri,
214+
),
215+
coreTypes);
216+
221217
library.addClass(cls);
222218
cls.addProcedure(_generateEquals(shape, fields, cls));
223219
cls.addProcedure(_generateCheckRecordType(shape, fields));
@@ -230,19 +226,23 @@ class _RecordClassGenerator {
230226
final List<Field> fields = [];
231227

232228
for (int i = 0; i < shape.positionals; i += 1) {
233-
fields.add(_addWasmEntryPointPragma(Field.immutable(
234-
Name('\$${i + 1}', library),
235-
isFinal: true,
236-
fileUri: library.fileUri,
237-
)));
229+
fields.add(addWasmEntryPointPragma(
230+
Field.immutable(
231+
Name('\$${i + 1}', library),
232+
isFinal: true,
233+
fileUri: library.fileUri,
234+
),
235+
coreTypes));
238236
}
239237

240238
for (String name in shape.names) {
241-
fields.add(_addWasmEntryPointPragma(Field.immutable(
242-
Name(name, library),
243-
isFinal: true,
244-
fileUri: library.fileUri,
245-
)));
239+
fields.add(addWasmEntryPointPragma(
240+
Field.immutable(
241+
Name(name, library),
242+
isFinal: true,
243+
fileUri: library.fileUri,
244+
),
245+
coreTypes));
246246
}
247247

248248
return fields;
@@ -263,11 +263,13 @@ class _RecordClassGenerator {
263263
final function =
264264
FunctionNode(null, positionalParameters: positionalParameters);
265265

266-
return _addWasmEntryPointPragma(Constructor(function,
267-
name: Name('_', library),
268-
isConst: true,
269-
initializers: initializers,
270-
fileUri: library.fileUri));
266+
return addWasmEntryPointPragma(
267+
Constructor(function,
268+
name: Name('_', library),
269+
isConst: true,
270+
initializers: initializers,
271+
fileUri: library.fileUri),
272+
coreTypes);
271273
}
272274

273275
/// Generate `int get hashCode` member.
@@ -295,12 +297,14 @@ class _RecordClassGenerator {
295297
}
296298
}
297299

298-
return _addWasmEntryPointPragma(Procedure(
299-
Name('hashCode', library),
300-
ProcedureKind.Getter,
301-
FunctionNode(ReturnStatement(returnValue), returnType: intType),
302-
fileUri: library.fileUri,
303-
));
300+
return addWasmEntryPointPragma(
301+
Procedure(
302+
Name('hashCode', library),
303+
ProcedureKind.Getter,
304+
FunctionNode(ReturnStatement(returnValue), returnType: intType),
305+
fileUri: library.fileUri,
306+
),
307+
coreTypes);
304308
}
305309

306310
/// Generate `String toString()` member.
@@ -357,12 +361,14 @@ class _RecordClassGenerator {
357361
),
358362
));
359363

360-
return _addWasmEntryPointPragma(Procedure(
361-
Name('toString', library),
362-
ProcedureKind.Method,
363-
FunctionNode(ReturnStatement(stringExpression)),
364-
fileUri: library.fileUri,
365-
));
364+
return addWasmEntryPointPragma(
365+
Procedure(
366+
Name('toString', library),
367+
ProcedureKind.Method,
368+
FunctionNode(ReturnStatement(stringExpression)),
369+
fileUri: library.fileUri,
370+
),
371+
coreTypes);
366372
}
367373

368374
/// Generate `bool operator ==` member.
@@ -410,12 +416,14 @@ class _RecordClassGenerator {
410416
returnType: boolType,
411417
);
412418

413-
return _addWasmEntryPointPragma(Procedure(
414-
Name('==', library),
415-
ProcedureKind.Operator,
416-
function,
417-
fileUri: library.fileUri,
418-
));
419+
return addWasmEntryPointPragma(
420+
Procedure(
421+
Name('==', library),
422+
ProcedureKind.Operator,
423+
function,
424+
fileUri: library.fileUri,
425+
),
426+
coreTypes);
419427
}
420428

421429
/// Generate `_checkRecordType` member.
@@ -485,12 +493,14 @@ class _RecordClassGenerator {
485493
returnType: boolType,
486494
);
487495

488-
return _addWasmEntryPointPragma(Procedure(
489-
Name('_checkRecordType', library),
490-
ProcedureKind.Method,
491-
function,
492-
fileUri: library.fileUri,
493-
));
496+
return addWasmEntryPointPragma(
497+
Procedure(
498+
Name('_checkRecordType', library),
499+
ProcedureKind.Method,
500+
function,
501+
fileUri: library.fileUri,
502+
),
503+
coreTypes);
494504
}
495505

496506
/// Generate `_Type get _recordRuntimeType` member.
@@ -547,12 +557,14 @@ class _RecordClassGenerator {
547557
InterfaceType(recordRuntimeTypeClass, Nullability.nonNullable),
548558
);
549559

550-
return _addWasmEntryPointPragma(Procedure(
551-
Name(name, library),
552-
ProcedureKind.Getter,
553-
function,
554-
fileUri: library.fileUri,
555-
));
560+
return addWasmEntryPointPragma(
561+
Procedure(
562+
Name(name, library),
563+
ProcedureKind.Getter,
564+
function,
565+
fileUri: library.fileUri,
566+
),
567+
coreTypes);
556568
}
557569

558570
Constant _fieldNamesConstant(RecordShape shape) {

pkg/dart2wasm/lib/util.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import 'package:kernel/ast.dart';
66
import 'package:kernel/core_types.dart';
77

8+
bool hasPragma(CoreTypes coreTypes, Annotatable node, String name) {
9+
return getPragma(coreTypes, node, name, defaultValue: '') != null;
10+
}
11+
812
T? getPragma<T>(CoreTypes coreTypes, Annotatable node, String name,
913
{T? defaultValue}) {
1014
for (Expression annotation in node.annotations) {
@@ -35,3 +39,16 @@ T? getPragma<T>(CoreTypes coreTypes, Annotatable node, String name,
3539
}
3640
return null;
3741
}
42+
43+
/// Add a `@pragma('wasm:entry-point')` annotation to an annotatable.
44+
T addWasmEntryPointPragma<T extends Annotatable>(T node, CoreTypes coreTypes) =>
45+
addPragma(node, 'wasm:entry-point', coreTypes);
46+
47+
T addPragma<T extends Annotatable>(
48+
T node, String pragmaName, CoreTypes coreTypes, {Constant? value}) =>
49+
node
50+
..addAnnotation(ConstantExpression(
51+
InstanceConstant(coreTypes.pragmaClass.reference, [], {
52+
coreTypes.pragmaName.fieldReference: StringConstant(pragmaName),
53+
coreTypes.pragmaOptions.fieldReference: value ?? NullConstant(),
54+
})));

0 commit comments

Comments
 (0)