Skip to content

Commit e9e0dae

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
Add importUri and fileUri to Source.
This will enable the VM to map URIs to package-URIs to solve problems such as #35859 Change-Id: I15520325a5b81a99a7e3f56c2e35fd775d9da946 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96905 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Peter von der Ahé <[email protected]>
1 parent 4daa9b9 commit e9e0dae

17 files changed

+92
-47
lines changed

pkg/compiler/lib/src/io/location_provider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LocationCollector extends CodeOutputListener implements LocationProvider {
4040
@override
4141
Location getLocation(int offset) {
4242
RangeError.checkValueInInterval(offset, 0, length, 'offset');
43-
return new Source(lineStarts, null).getLocation(null, offset);
43+
return new Source(lineStarts, null, null, null).getLocation(null, offset);
4444
}
4545

4646
@override

pkg/compiler/lib/src/io/source_file.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ abstract class SourceFile<T> implements Input<T>, LocationProvider {
2626
kernel.Source cachedKernelSource;
2727

2828
kernel.Source get kernelSource {
29-
return cachedKernelSource ??=
30-
new kernel.Source(lineStarts, slowUtf8ZeroTerminatedBytes())
31-
..cachedText = slowText();
29+
// TODO(johnniwinther): Instead of creating a new Source object,
30+
// we should use the one provided by the front-end.
31+
return cachedKernelSource ??= new kernel.Source(
32+
lineStarts,
33+
slowUtf8ZeroTerminatedBytes(),
34+
uri /* TODO(jensj): What is the import URI? */,
35+
uri)
36+
..cachedText = slowText();
3237
}
3338

3439
/// The name of the file.

pkg/dev_compiler/lib/src/kernel/analyzer_to_kernel.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class AnalyzerToKernel {
150150
var uriToSource = <Uri, Source>{};
151151

152152
void addCompilationUnit(a.CompilationUnitElement unit) {
153-
uriToSource[unit.source.uri] = Source(unit.lineInfo.lineStarts, []);
153+
uriToSource[unit.source.uri] = Source(
154+
unit.lineInfo.lineStarts,
155+
[],
156+
unit.uri != null ? Uri.base.resolve(unit.uri) : unit.source.uri,
157+
unit.source.uri);
154158
}
155159

156160
for (var uri in bundle.unlinkedUnitUris) {

pkg/front_end/lib/src/compute_platform_binaries_location.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ Uri translateSdk(Uri uri) {
6969
Map<Uri, Source> uriToSource = CompilerContext.current.uriToSource;
7070
Source source = uriToSource[uri];
7171
if (source.source.isEmpty) {
72-
uriToSource[uri] = new Source(source.lineStarts,
73-
new File.fromUri(candidate).readAsBytesSync());
72+
uriToSource[uri] = new Source(
73+
source.lineStarts,
74+
new File.fromUri(candidate).readAsBytesSync(),
75+
source.importUri,
76+
source.fileUri);
7477
}
7578
}
7679
return candidate;

pkg/front_end/lib/src/fasta/dill/dill_target.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DillTarget extends TargetImplementation {
3939

4040
@override
4141
void addSourceInformation(
42-
Uri uri, List<int> lineStarts, List<int> sourceCode) {
42+
Uri importUri, Uri fileUri, List<int> lineStarts, List<int> sourceCode) {
4343
unsupported("addSourceInformation", -1, null);
4444
}
4545

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ class KernelTarget extends TargetImplementation {
155155
new SourceLoader(fileSystem, includeComments, this);
156156

157157
void addSourceInformation(
158-
Uri uri, List<int> lineStarts, List<int> sourceCode) {
159-
uriToSource[uri] = new Source(lineStarts, sourceCode);
158+
Uri importUri, Uri fileUri, List<int> lineStarts, List<int> sourceCode) {
159+
uriToSource[fileUri] =
160+
new Source(lineStarts, sourceCode, importUri, fileUri);
160161
}
161162

162163
/// Return list of same size as input with possibly translated uris.
@@ -328,8 +329,10 @@ class KernelTarget extends TargetImplementation {
328329

329330
Map<Uri, Source> uriToSource = new Map<Uri, Source>();
330331
void copySource(Uri uri, Source source) {
331-
uriToSource[uri] =
332-
excludeSource ? new Source(source.lineStarts, const <int>[]) : source;
332+
uriToSource[uri] = excludeSource
333+
? new Source(source.lineStarts, const <int>[], source.importUri,
334+
source.fileUri)
335+
: source;
333336
}
334337

335338
this.uriToSource.forEach(copySource);

pkg/front_end/lib/src/fasta/kernel/utils.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ Future<Null> writeComponentToFile(Component component, Uri uri,
6060

6161
/// Serialize the libraries in [component] that match [filter].
6262
List<int> serializeComponent(Component component,
63-
{bool filter(Library library), bool excludeUriToSource: false}) {
63+
{bool filter(Library library), bool includeSources: true}) {
6464
ByteSink byteSink = new ByteSink();
65-
BinaryPrinter printer = filter == null && !excludeUriToSource
66-
? new BinaryPrinter(byteSink)
65+
BinaryPrinter printer = filter == null
66+
? new BinaryPrinter(byteSink, includeSources: includeSources)
6767
: new LimitedBinaryPrinter(
68-
byteSink, filter ?? (_) => true, excludeUriToSource);
68+
byteSink, filter ?? (_) => true, !includeSources);
6969
printer.writeComponentFile(component);
7070
return byteSink.builder.takeBytes();
7171
}

pkg/front_end/lib/src/fasta/source/source_loader.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ class SourceLoader extends Loader<Library> {
203203
Token token = result.tokens;
204204
if (!suppressLexicalErrors) {
205205
List<int> source = getSource(bytes);
206-
target.addSourceInformation(library.fileUri, result.lineStarts, source);
206+
target.addSourceInformation(
207+
library.isPatch ? library.fileUri : library.uri,
208+
library.fileUri,
209+
result.lineStarts,
210+
source);
207211
}
208212
while (token is ErrorToken) {
209213
if (!suppressLexicalErrors) {

pkg/front_end/lib/src/fasta/target_implementation.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ abstract class TargetImplementation extends Target {
131131
}
132132

133133
void addSourceInformation(
134-
Uri uri, List<int> lineStarts, List<int> sourceCode);
134+
Uri importUri, Uri fileUri, List<int> lineStarts, List<int> sourceCode);
135135

136136
void readPatchFiles(covariant LibraryBuilder library) {}
137137

pkg/front_end/lib/src/kernel_generator_impl.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Future<CompilerResult> generateKernelInternal(
117117
libraryFilter: kernelTarget.isSourceLibrary);
118118
}
119119

120-
// Copy the component to exclude the uriToSource map from the summary.
120+
// Create the requested component ("truncating" or not).
121121
//
122122
// Note: we don't pass the library argument to the constructor to
123123
// preserve the the libraries parent pointer (it should continue to point
@@ -128,6 +128,7 @@ Future<CompilerResult> generateKernelInternal(
128128
? kernelTarget.loader.libraries
129129
: summaryComponent.libraries);
130130
trimmedSummaryComponent.metadata.addAll(summaryComponent.metadata);
131+
trimmedSummaryComponent.uriToSource.addAll(summaryComponent.uriToSource);
131132

132133
// As documented, we only run outline transformations when we are building
133134
// summaries without building a full component (at this time, that's
@@ -136,7 +137,9 @@ Future<CompilerResult> generateKernelInternal(
136137
options.target.performOutlineTransformations(trimmedSummaryComponent);
137138
options.ticker.logMs("Transformed outline");
138139
}
139-
summary = serializeComponent(trimmedSummaryComponent);
140+
// Don't include source (but do add it above to include importUris).
141+
summary =
142+
serializeComponent(trimmedSummaryComponent, includeSources: false);
140143
options.ticker.logMs("Generated outline");
141144
}
142145

pkg/front_end/test/fasta/type_promotion_look_ahead_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class TypePromotionLookAheadStep extends Step<ScannedFile, TypePromotionResult,
9191
return context.context
9292
.runInContext<Result<TypePromotionResult>>((CompilerContext c) async {
9393
Uri uri = file.file.uri;
94-
c.uriToSource[uri] = new Source(file.result.lineStarts, file.file.bytes);
94+
c.uriToSource[uri] =
95+
new Source(file.result.lineStarts, file.file.bytes, uri, uri);
9596
StringBuffer buffer = new StringBuffer();
9697
Parser parser = new Parser(new TestListener(uri, buffer));
9798
try {

pkg/kernel/binary.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ type SourceInfo {
9797
// Line starts are delta-encoded (they are encoded as line lengths). The list
9898
// [0, 10, 25, 32, 42] is encoded as [0, 10, 15, 7, 10].
9999
List<UInt> lineStarts;
100+
101+
List<Byte> importUriUtf8Bytes;
100102
}
101103

102104
type UriSource {
@@ -137,7 +139,7 @@ type CanonicalName {
137139

138140
type ComponentFile {
139141
UInt32 magic = 0x90ABCDEF;
140-
UInt32 formatVersion = 21;
142+
UInt32 formatVersion = 22;
141143
List<String> problemsAsJson; // Described in problems.md.
142144
Library[] libraries;
143145
UriSource sourceMap;

pkg/kernel/lib/ast.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5951,9 +5951,13 @@ class Source {
59515951

59525952
final List<int> source;
59535953

5954+
final Uri importUri;
5955+
5956+
final Uri fileUri;
5957+
59545958
String cachedText;
59555959

5956-
Source(this.lineStarts, this.source);
5960+
Source(this.lineStarts, this.source, this.importUri, this.fileUri);
59575961

59585962
/// Return the text corresponding to [line] which is a 1-based line
59595963
/// number. The returned line contains no line separators.

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,11 @@ class BinaryBuilder {
696696
lineStarts[j] = lineStart;
697697
previousLineStart = lineStart;
698698
}
699-
uriToSource[uri] = new Source(lineStarts, sourceCode);
699+
List<int> importUriBytes = readByteList();
700+
Uri importUri = importUriBytes.isEmpty
701+
? null
702+
: Uri.parse(const Utf8Decoder().convert(importUriBytes));
703+
uriToSource[uri] = new Source(lineStarts, sourceCode, importUri, uri);
700704
}
701705

702706
// Read index.

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -740,31 +740,20 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
740740
Uint8List buffer = new Uint8List(1 << 16);
741741
for (Uri uri in _sourceUriIndexer.index.keys) {
742742
index[i] = getBufferOffset();
743-
Source source = ((includeSources &&
744-
_sourcesFromRealImplementation.length > i &&
745-
_sourcesFromRealImplementation[i] == true)
746-
? uriToSource[uri]
747-
: null) ??
748-
new Source(<int>[], const <int>[]);
743+
Source source = uriToSource[uri];
744+
if (source == null ||
745+
!(includeSources &&
746+
_sourcesFromRealImplementation.length > i &&
747+
_sourcesFromRealImplementation[i] == true)) {
748+
source = new Source(
749+
<int>[], const <int>[], source?.importUri, source?.fileUri);
750+
}
749751

750752
String uriAsString = uri == null ? "" : "$uri";
751-
if (uriAsString.length * 3 < buffer.length) {
752-
int length = NotQuiteString.writeUtf8(buffer, 0, uriAsString);
753-
if (length < 0) {
754-
// Utf8 encoding failed.
755-
writeByteList(utf8.encoder.convert(uriAsString));
756-
} else {
757-
writeUInt30(length);
758-
for (int j = 0; j < length; j++) {
759-
writeByte(buffer[j]);
760-
}
761-
}
762-
} else {
763-
// Uncommon case with very long url.
764-
writeByteList(utf8.encoder.convert(uriAsString));
765-
}
753+
outputStringViaBuffer(uriAsString, buffer);
766754

767755
writeByteList(source.source);
756+
768757
List<int> lineStarts = source.lineStarts;
769758
writeUInt30(lineStarts.length);
770759
int previousLineStart = 0;
@@ -773,6 +762,11 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
773762
writeUInt30(lineStart - previousLineStart);
774763
previousLineStart = lineStart;
775764
}
765+
766+
String importUriAsString =
767+
source.importUri == null ? "" : "${source.importUri}";
768+
outputStringViaBuffer(importUriAsString, buffer);
769+
776770
i++;
777771
}
778772

@@ -782,6 +776,24 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
782776
}
783777
}
784778

779+
void outputStringViaBuffer(String uriAsString, Uint8List buffer) {
780+
if (uriAsString.length * 3 < buffer.length) {
781+
int length = NotQuiteString.writeUtf8(buffer, 0, uriAsString);
782+
if (length < 0) {
783+
// Utf8 encoding failed.
784+
writeByteList(utf8.encoder.convert(uriAsString));
785+
} else {
786+
writeUInt30(length);
787+
for (int j = 0; j < length; j++) {
788+
writeByte(buffer[j]);
789+
}
790+
}
791+
} else {
792+
// Uncommon case with very long url.
793+
writeByteList(utf8.encoder.convert(uriAsString));
794+
}
795+
}
796+
785797
void writeLibraryDependencyReference(LibraryDependency node) {
786798
int index = _libraryDependencyIndex[node];
787799
if (index == null) {

pkg/kernel/lib/binary/tag.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Tag {
145145
/// Internal version of kernel binary format.
146146
/// Bump it when making incompatible changes in kernel binaries.
147147
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
148-
static const int BinaryFormatVersion = 21;
148+
static const int BinaryFormatVersion = 22;
149149
}
150150

151151
abstract class ConstantTag {

runtime/vm/kernel_binary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static const uint32_t kMagicProgramFile = 0x90ABCDEFu;
2020

2121
// Both version numbers are inclusive.
2222
static const uint32_t kMinSupportedKernelFormatVersion = 18;
23-
static const uint32_t kMaxSupportedKernelFormatVersion = 21;
23+
static const uint32_t kMaxSupportedKernelFormatVersion = 22;
2424

2525
// Keep in sync with package:kernel/lib/binary/tag.dart
2626
#define KERNEL_TAG_LIST(V) \

0 commit comments

Comments
 (0)