Skip to content

Commit 7b10dc6

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Use fileUri to determine when to create a FileUriExpression
This adds a fileUri property to MetadataBuilder and passes the fileUri of the annotated node Change-Id: I8772ce012dc0c6f33be1f2a67e78ab6c34222405 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/424941 Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 42e3a7a commit 7b10dc6

File tree

63 files changed

+502
-446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+502
-446
lines changed

pkg/front_end/lib/src/builder/library_builder.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ abstract class SourceCompilationUnit
228228
{required bool allowPartInParts});
229229

230230
void buildOutlineExpressions(
231-
Annotatable annotatable, BodyBuilderContext bodyBuilderContext,
232-
{required bool createFileUriExpression});
231+
{required Annotatable annotatable,
232+
required Uri annotatableFileUri,
233+
required BodyBuilderContext bodyBuilderContext});
233234

234235
/// Reports that [feature] is not enabled, using [charOffset] and
235236
/// [length] for the location of the message.

pkg/front_end/lib/src/builder/metadata_builder.dart

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class MetadataBuilder {
3232
/// Expression for an already parsed annotation.
3333
Expression? _expression;
3434

35-
MetadataBuilder(Token this._atToken)
35+
final Uri fileUri;
36+
37+
MetadataBuilder(Token this._atToken, this.fileUri)
3638
: atOffset = _atToken.charOffset,
3739
hasPatch = _atToken.next?.lexeme == 'patch';
3840

@@ -51,13 +53,12 @@ class MetadataBuilder {
5153
_unresolvedSharedExpressionForTesting;
5254

5355
static void buildAnnotations(
54-
Annotatable parent,
55-
List<MetadataBuilder>? metadata,
56-
BodyBuilderContext bodyBuilderContext,
57-
SourceLibraryBuilder library,
58-
Uri fileUri,
59-
LookupScope scope,
60-
{bool createFileUriExpression = false}) {
56+
{required Annotatable annotatable,
57+
required Uri annotatableFileUri,
58+
required List<MetadataBuilder>? metadata,
59+
required BodyBuilderContext bodyBuilderContext,
60+
required SourceLibraryBuilder libraryBuilder,
61+
required LookupScope scope}) {
6162
if (metadata == null) return;
6263

6364
// [BodyBuilder] used to build annotations from [Token]s.
@@ -74,33 +75,42 @@ class MetadataBuilder {
7475

7576
for (int i = 0; i < metadata.length; ++i) {
7677
MetadataBuilder annotationBuilder = metadata[i];
78+
bool createFileUriExpression =
79+
annotatableFileUri != annotationBuilder.fileUri;
7780
Token? beginToken = annotationBuilder._atToken;
7881
if (beginToken != null) {
7982
if (computeSharedExpressionForTesting) {
8083
// Coverage-ignore-block(suite): Not run.
8184
annotationBuilder._sharedExpression = _parseSharedExpression(
82-
library.loader, beginToken, library.importUri, fileUri, scope);
85+
libraryBuilder.loader,
86+
beginToken,
87+
libraryBuilder.importUri,
88+
annotationBuilder.fileUri,
89+
scope);
8390
if (delaySharedExpressionLookupForTesting) {
8491
annotationBuilder._unresolvedSharedExpressionForTesting =
85-
_parseSharedExpression(library.loader, beginToken,
86-
library.importUri, fileUri, scope,
92+
_parseSharedExpression(libraryBuilder.loader, beginToken,
93+
libraryBuilder.importUri, annotationBuilder.fileUri, scope,
8794
delayLookupForTesting: true);
8895
}
8996
}
9097

91-
bodyBuilder ??= library.loader.createBodyBuilderForOutlineExpression(
92-
library, bodyBuilderContext, scope, fileUri);
98+
bodyBuilder ??= libraryBuilder.loader
99+
.createBodyBuilderForOutlineExpression(libraryBuilder,
100+
bodyBuilderContext, scope, annotationBuilder.fileUri);
93101
Expression annotation = bodyBuilder.parseAnnotation(beginToken);
94102
annotationBuilder._atToken = null;
95103
if (createFileUriExpression) {
96-
annotation = new FileUriExpression(annotation, fileUri)
97-
..fileOffset = annotationBuilder.atOffset;
104+
annotation =
105+
new FileUriExpression(annotation, annotationBuilder.fileUri)
106+
..fileOffset = annotationBuilder.atOffset;
98107
}
99108
// Record the index of [annotation] in `parent.annotations`.
100-
parsedAnnotationBuilders[annotationBuilder] = parent.annotations.length;
109+
parsedAnnotationBuilders[annotationBuilder] =
110+
annotatable.annotations.length;
101111
// It is important for the inference and backlog computations that the
102112
// annotation is already a child of [parent].
103-
parent.addAnnotation(annotation);
113+
annotatable.addAnnotation(annotation);
104114
} else {
105115
// The annotation is needed for multiple declarations so we need to
106116
// clone the expression to use it more than once. For instance
@@ -124,22 +134,23 @@ class MetadataBuilder {
124134
cloner.cloneInContext(annotationBuilder._expression!);
125135
// Coverage-ignore(suite): Not run.
126136
if (createFileUriExpression && annotation is! FileUriExpression) {
127-
annotation = new FileUriExpression(annotation, fileUri)
128-
..fileOffset = annotationBuilder.atOffset;
137+
annotation =
138+
new FileUriExpression(annotation, annotationBuilder.fileUri)
139+
..fileOffset = annotationBuilder.atOffset;
129140
}
130-
parent.addAnnotation(annotation);
141+
annotatable.addAnnotation(annotation);
131142
}
132143
}
133144
if (bodyBuilder != null) {
134145
// TODO(johnniwinther): Avoid potentially inferring annotations multiple
135146
// times.
136-
bodyBuilder.inferAnnotations(parent, parent.annotations);
147+
bodyBuilder.inferAnnotations(annotatable, annotatable.annotations);
137148
bodyBuilder.performBacklogComputations();
138149
for (MapEntry<MetadataBuilder, int> entry
139150
in parsedAnnotationBuilders.entries) {
140151
MetadataBuilder annotationBuilder = entry.key;
141152
int index = entry.value;
142-
annotationBuilder._expression = parent.annotations[index];
153+
annotationBuilder._expression = annotatable.annotations[index];
143154
}
144155
}
145156
}

pkg/front_end/lib/src/fragment/class/declaration.dart

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ abstract class ClassDeclaration {
2020

2121
void buildOutlineExpressions(
2222
{required Annotatable annotatable,
23+
required Uri annotatableFileUri,
2324
required SourceLibraryBuilder libraryBuilder,
2425
required ClassHierarchy classHierarchy,
2526
required BodyBuilderContext bodyBuilderContext,
@@ -69,18 +70,18 @@ class RegularClassDeclaration implements ClassDeclaration {
6970
@override
7071
void buildOutlineExpressions(
7172
{required Annotatable annotatable,
73+
required Uri annotatableFileUri,
7274
required SourceLibraryBuilder libraryBuilder,
7375
required ClassHierarchy classHierarchy,
7476
required BodyBuilderContext bodyBuilderContext,
7577
required bool createFileUriExpression}) {
7678
MetadataBuilder.buildAnnotations(
77-
annotatable,
78-
_fragment.metadata,
79-
bodyBuilderContext,
80-
libraryBuilder,
81-
_fragment.fileUri,
82-
_fragment.enclosingScope,
83-
createFileUriExpression: createFileUriExpression);
79+
annotatable: annotatable,
80+
annotatableFileUri: annotatableFileUri,
81+
metadata: _fragment.metadata,
82+
bodyBuilderContext: bodyBuilderContext,
83+
libraryBuilder: libraryBuilder,
84+
scope: _fragment.enclosingScope);
8485
}
8586

8687
@override
@@ -133,18 +134,18 @@ class EnumDeclaration implements ClassDeclaration {
133134
@override
134135
void buildOutlineExpressions(
135136
{required Annotatable annotatable,
137+
required Uri annotatableFileUri,
136138
required SourceLibraryBuilder libraryBuilder,
137139
required ClassHierarchy classHierarchy,
138140
required BodyBuilderContext bodyBuilderContext,
139141
required bool createFileUriExpression}) {
140142
MetadataBuilder.buildAnnotations(
141-
annotatable,
142-
_fragment.metadata,
143-
bodyBuilderContext,
144-
libraryBuilder,
145-
_fragment.fileUri,
146-
_fragment.enclosingScope,
147-
createFileUriExpression: createFileUriExpression);
143+
annotatable: annotatable,
144+
annotatableFileUri: annotatableFileUri,
145+
metadata: _fragment.metadata,
146+
bodyBuilderContext: bodyBuilderContext,
147+
libraryBuilder: libraryBuilder,
148+
scope: _fragment.enclosingScope);
148149
}
149150

150151
@override
@@ -198,18 +199,18 @@ class NamedMixinApplication implements ClassDeclaration {
198199
@override
199200
void buildOutlineExpressions(
200201
{required Annotatable annotatable,
202+
required Uri annotatableFileUri,
201203
required SourceLibraryBuilder libraryBuilder,
202204
required ClassHierarchy classHierarchy,
203205
required BodyBuilderContext bodyBuilderContext,
204206
required bool createFileUriExpression}) {
205207
MetadataBuilder.buildAnnotations(
206-
annotatable,
207-
_fragment.metadata,
208-
bodyBuilderContext,
209-
libraryBuilder,
210-
_fragment.fileUri,
211-
_fragment.enclosingScope,
212-
createFileUriExpression: createFileUriExpression);
208+
annotatable: annotatable,
209+
annotatableFileUri: annotatableFileUri,
210+
metadata: _fragment.metadata,
211+
bodyBuilderContext: bodyBuilderContext,
212+
libraryBuilder: libraryBuilder,
213+
scope: _fragment.enclosingScope);
213214
}
214215

215216
@override
@@ -266,6 +267,7 @@ class AnonymousMixinApplication implements ClassDeclaration {
266267
@override
267268
void buildOutlineExpressions(
268269
{required Annotatable annotatable,
270+
required Uri annotatableFileUri,
269271
required SourceLibraryBuilder libraryBuilder,
270272
required ClassHierarchy classHierarchy,
271273
required BodyBuilderContext bodyBuilderContext,
@@ -319,18 +321,18 @@ class MixinDeclaration implements ClassDeclaration {
319321
@override
320322
void buildOutlineExpressions(
321323
{required Annotatable annotatable,
324+
required Uri annotatableFileUri,
322325
required SourceLibraryBuilder libraryBuilder,
323326
required ClassHierarchy classHierarchy,
324327
required BodyBuilderContext bodyBuilderContext,
325328
required bool createFileUriExpression}) {
326329
MetadataBuilder.buildAnnotations(
327-
annotatable,
328-
_fragment.metadata,
329-
bodyBuilderContext,
330-
libraryBuilder,
331-
_fragment.fileUri,
332-
_fragment.enclosingScope,
333-
createFileUriExpression: createFileUriExpression);
330+
annotatable: annotatable,
331+
annotatableFileUri: annotatableFileUri,
332+
metadata: _fragment.metadata,
333+
bodyBuilderContext: bodyBuilderContext,
334+
libraryBuilder: libraryBuilder,
335+
scope: _fragment.enclosingScope);
334336
}
335337

336338
@override

0 commit comments

Comments
 (0)