Skip to content

Commit a3038b8

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm,dyn_modules] Support pragmas on local functions in bytecode
Pragmas on local functions are used internally by FFI. TEST=ci Change-Id: I3ffb9984d8fcd943b22a98746faa915b2ce7c9fa Cq-Include-Trybots: luci.dart.try:vm-aot-dyn-linux-debug-x64-try,vm-aot-dyn-linux-product-x64-try,vm-dyn-linux-debug-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446121 Reviewed-by: Tess Strickland <sstrickl@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
1 parent 6157e8d commit a3038b8

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

pkg/dart2bytecode/docs/bytecode.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ type ClosureDeclaration {
647647
UInt flags = (hasOptionalPositionalParams, hasOptionalNamedParams,
648648
hasTypeParams, hasSourcePositions,
649649
isAsync, isAsyncStar, isSyncStar, isDebuggable,
650-
hasParameterFlags)
650+
hasParameterFlags, hasAnnotations, hasPragma)
651651
652652
// Member or Closure.
653653
PackedObject parent;
@@ -672,6 +672,11 @@ type ClosureDeclaration {
672672
List<UInt> parameterFlags;
673673
674674
PackedObject returnType;
675+
676+
if hasAnnotations
677+
// Offset of closure annotations in ‘annotations’ section of
678+
// component, followed by parameter annotations.
679+
UInt annotationsOffset;
675680
}
676681
677682
type ClosureCode {

pkg/dart2bytecode/lib/bytecode_generator.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,23 +403,22 @@ class BytecodeGenerator extends RecursiveVisitor {
403403
// section. Return the annotations for the function only. The bytecode reader
404404
// will implicitly find the parameter annotations by reading N packed objects
405405
// after reading the function's annotations, one for each parameter.
406-
Annotations getFunctionAnnotations(Member member) {
407-
final functionNodes = member.annotations;
406+
Annotations getFunctionAnnotations(
407+
List<Expression> annotations, FunctionNode function) {
408408
final parameterNodeLists = <List<Expression>>[];
409-
for (VariableDeclaration variable
410-
in member.function!.positionalParameters) {
409+
for (VariableDeclaration variable in function.positionalParameters) {
411410
parameterNodeLists.add(variable.annotations);
412411
}
413-
for (VariableDeclaration variable in member.function!.namedParameters) {
412+
for (VariableDeclaration variable in function.namedParameters) {
414413
parameterNodeLists.add(variable.annotations);
415414
}
416415

417-
if (functionNodes.isEmpty &&
416+
if (annotations.isEmpty &&
418417
parameterNodeLists.every((nodes) => nodes.isEmpty)) {
419418
return const Annotations(null, false);
420419
}
421420

422-
List<Constant> functionConstants = functionNodes.map(_getConstant).toList();
421+
List<Constant> functionConstants = annotations.map(_getConstant).toList();
423422
bool hasPragma = functionConstants.any(_isPragma);
424423
if (!options.emitAnnotations && !hasPragma) {
425424
return const Annotations(null, false);
@@ -677,7 +676,8 @@ class BytecodeGenerator extends RecursiveVisitor {
677676
}
678677
endPosition = member.fileEndOffset;
679678
}
680-
final Annotations annotations = getFunctionAnnotations(member);
679+
final Annotations annotations =
680+
getFunctionAnnotations(member.annotations, function);
681681
if (annotations.object != null) {
682682
flags |= FunctionDeclaration.hasAnnotationsFlag;
683683
if (annotations.hasPragma) {
@@ -2548,6 +2548,18 @@ class BytecodeGenerator extends RecursiveVisitor {
25482548
flags |= ClosureDeclaration.hasParameterFlagsFlag;
25492549
}
25502550

2551+
final Annotations annotations = getFunctionAnnotations(
2552+
node is ast.FunctionDeclaration
2553+
? node.variable.annotations
2554+
: const <Expression>[],
2555+
function);
2556+
if (annotations.object != null) {
2557+
flags |= ClosureDeclaration.hasAnnotationsFlag;
2558+
if (annotations.hasPragma) {
2559+
flags |= ClosureDeclaration.hasPragmaFlag;
2560+
}
2561+
}
2562+
25512563
return new ClosureDeclaration(
25522564
flags,
25532565
objectTable.getHandle(parent)!,
@@ -2559,7 +2571,8 @@ class BytecodeGenerator extends RecursiveVisitor {
25592571
function.namedParameters.length,
25602572
parameters,
25612573
parameterFlags,
2562-
objectTable.getHandle(function.returnType)!);
2574+
objectTable.getHandle(function.returnType)!,
2575+
annotations.object);
25632576
}
25642577

25652578
void _genAllocateClosureInstance(

pkg/dart2bytecode/lib/declarations.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ class ClosureDeclaration {
947947
static const isSyncStarFlag = 1 << 6;
948948
static const isDebuggableFlag = 1 << 7;
949949
static const hasParameterFlagsFlag = 1 << 8;
950+
static const hasAnnotationsFlag = 1 << 9;
951+
static const hasPragmaFlag = 1 << 10;
950952

951953
int flags;
952954
final ObjectHandle parent;
@@ -960,6 +962,7 @@ class ClosureDeclaration {
960962
// Only contains the required flag for named parameters when present.
961963
final List<int>? parameterFlags;
962964
final ObjectHandle returnType;
965+
final AnnotationsDeclaration? annotations;
963966
ClosureCode? code;
964967

965968
ClosureDeclaration(
@@ -973,7 +976,8 @@ class ClosureDeclaration {
973976
this.numNamedParams,
974977
this.parameters,
975978
this.parameterFlags,
976-
this.returnType);
979+
this.returnType,
980+
this.annotations);
977981

978982
void write(BufferedWriter writer) {
979983
writer.writePackedUInt30(flags);
@@ -1006,6 +1010,9 @@ class ClosureDeclaration {
10061010
}
10071011
}
10081012
writer.writePackedObject(returnType);
1013+
if ((flags & hasAnnotationsFlag) != 0) {
1014+
writer.writeLinkOffset(annotations!);
1015+
}
10091016
}
10101017

10111018
factory ClosureDeclaration.read(BufferedReader reader) {
@@ -1043,6 +1050,9 @@ class ClosureDeclaration {
10431050
numParameterFlags, (_) => reader.readPackedUInt30());
10441051
}
10451052
final ObjectHandle returnType = reader.readPackedObject();
1053+
final annotations = ((flags & hasAnnotationsFlag) != 0)
1054+
? reader.readLinkOffset<AnnotationsDeclaration>()
1055+
: null;
10461056
return new ClosureDeclaration(
10471057
flags,
10481058
parent,
@@ -1054,7 +1064,8 @@ class ClosureDeclaration {
10541064
numNamedParams,
10551065
parameters,
10561066
parameterFlags,
1057-
returnType);
1067+
returnType,
1068+
annotations);
10581069
}
10591070

10601071
void _writeParamsToBuffer(
@@ -1088,6 +1099,9 @@ class ClosureDeclaration {
10881099
if (position != TreeNode.noOffset) {
10891100
sb.write(' pos = $position, end-pos = $endPosition');
10901101
}
1102+
if ((flags & hasAnnotationsFlag) != 0) {
1103+
sb.write(' annotations $annotations\n');
1104+
}
10911105
if ((flags & hasTypeParamsFlag) != 0) {
10921106
sb.write(' type-params $typeParameters');
10931107
}

runtime/vm/bytecode_reader.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
322322
const int kIsSyncStarFlag = 1 << 6;
323323
const int kIsDebuggableFlag = 1 << 7;
324324
const int kHasParameterFlagsFlag = 1 << 8;
325+
const int kHasAnnotationsFlag = 1 << 9;
326+
const int kHasPragmaFlag = 1 << 10;
325327

326328
const intptr_t flags = reader_.ReadUInt();
329+
const bool has_pragma = (flags & kHasPragmaFlag) != 0;
327330

328331
Object& parent = Object::Handle(Z, ReadObject());
329332
if (!parent.IsFunction()) {
@@ -359,6 +362,7 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
359362
closure.set_is_inlinable(false);
360363
}
361364
closure.set_is_debuggable((flags & kIsDebuggableFlag) != 0);
365+
closure.set_has_pragma(has_pragma);
362366

363367
closures_->SetAt(closureIndex, closure);
364368

@@ -371,6 +375,11 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
371375
(flags & kHasParameterFlagsFlag) != 0);
372376

373377
closure.SetSignature(signature);
378+
379+
if ((flags & kHasAnnotationsFlag) != 0) {
380+
ReadAnnotations(Class::Handle(Z, Function::Cast(parent).Owner()), closure,
381+
has_pragma);
382+
}
374383
}
375384

376385
FunctionTypePtr BytecodeReaderHelper::ReadFunctionSignature(

0 commit comments

Comments
 (0)