8cc449ac8c
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 <kustermann@google.com> Commit-Queue: Nate Biggs <natebiggs@google.com>
55 lines
2.1 KiB
Dart
55 lines
2.1 KiB
Dart
// 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 'package:kernel/ast.dart';
|
|
import 'package:kernel/core_types.dart';
|
|
|
|
bool hasPragma(CoreTypes coreTypes, Annotatable node, String name) {
|
|
return getPragma(coreTypes, node, name, defaultValue: '') != null;
|
|
}
|
|
|
|
T? getPragma<T>(CoreTypes coreTypes, Annotatable node, String name,
|
|
{T? defaultValue}) {
|
|
for (Expression annotation in node.annotations) {
|
|
if (annotation is ConstantExpression) {
|
|
Constant constant = annotation.constant;
|
|
if (constant is InstanceConstant) {
|
|
if (constant.classNode == coreTypes.pragmaClass) {
|
|
Constant? nameConstant =
|
|
constant.fieldValues[coreTypes.pragmaName.fieldReference];
|
|
if (nameConstant is StringConstant && nameConstant.value == name) {
|
|
Constant? value =
|
|
constant.fieldValues[coreTypes.pragmaOptions.fieldReference];
|
|
if (value == null || value is NullConstant) {
|
|
return defaultValue;
|
|
}
|
|
if (value is PrimitiveConstant<T>) {
|
|
return value.value;
|
|
}
|
|
if (value is! T) {
|
|
throw ArgumentError("$name pragma argument has unexpected type "
|
|
"${value.runtimeType} (expected $T)");
|
|
}
|
|
return value as T;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Add a `@pragma('wasm:entry-point')` annotation to an annotatable.
|
|
T addWasmEntryPointPragma<T extends Annotatable>(T node, CoreTypes coreTypes) =>
|
|
addPragma(node, 'wasm:entry-point', coreTypes);
|
|
|
|
T addPragma<T extends Annotatable>(
|
|
T node, String pragmaName, CoreTypes coreTypes, {Constant? value}) =>
|
|
node
|
|
..addAnnotation(ConstantExpression(
|
|
InstanceConstant(coreTypes.pragmaClass.reference, [], {
|
|
coreTypes.pragmaName.fieldReference: StringConstant(pragmaName),
|
|
coreTypes.pragmaOptions.fieldReference: value ?? NullConstant(),
|
|
})));
|