Skip to content

Commit aecb572

Browse files
committed
Include source in kernel.
- For now include source uncompressed. - When running from kernel, use token position 0 (i.e. dummy, but 'real' position) as start and end on functions and classes to enable Observatory to run with the dill file. - Debugging does not work, but one can browse the source in Observatory. R=kmillikin@google.com Review-Url: https://codereview.chromium.org/2587673004 .
1 parent d19c7ac commit aecb572

File tree

11 files changed

+172
-67
lines changed

11 files changed

+172
-67
lines changed

pkg/kernel/binary.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,20 @@ type StringReference {
5959
UInt index; // Index into the StringTable strings.
6060
}
6161

62-
type LineStarts {
62+
type Source {
63+
String source;
6364
// Line starts are delta-encoded (they are encoded as line lengths). The list
6465
// [0, 10, 25, 32, 42] is encoded as [0, 10, 15, 7, 10].
6566
List<Uint> lineStarts;
6667
}
6768

68-
type UriLineStarts {
69+
type UriSource {
6970
List<String> uris;
70-
LineStarts[uris.length] lineStarts;
71+
Source[uris.length] source;
7172
}
7273

7374
type UriReference {
74-
UInt index; // Index into the UriLineStarts uris.
75+
UInt index; // Index into the UriSource uris.
7576
}
7677

7778
type FileOffset {
@@ -93,7 +94,7 @@ type Something<T> extends Option<T> {
9394
type ProgramFile {
9495
MagicWord magic = 0x90ABCDEF;
9596
StringTable strings;
96-
UriLineStarts lineStartsMap;
97+
UriSource sourceMap;
9798
List<Library> libraries;
9899
LibraryProcedureReference mainMethod;
99100
}

pkg/kernel/lib/analyzer/loader.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class DartLoader implements ReferenceLevelLoader {
120120
var uri = applicationRoot.relativeUri(element.source.uri);
121121
return repository.getLibraryReference(uri)
122122
..name ??= getLibraryName(element)
123-
..fileUri = "file://${element.source.fullName}";
123+
..fileUri = '${element.source.uri}';
124124
}
125125

126126
void _buildTopLevelMember(
@@ -268,7 +268,7 @@ class DartLoader implements ReferenceLevelLoader {
268268
_classes[element] = classNode = new ast.Class(
269269
name: element.name,
270270
isAbstract: element.isAbstract,
271-
fileUri: "file://${element.source.fullName}");
271+
fileUri: '${element.source.uri}');
272272
classNode.level = ast.ClassLevel.Temporary;
273273
var library = getLibraryReference(element.library);
274274
library.addClass(classNode);
@@ -332,7 +332,8 @@ class DartLoader implements ReferenceLevelLoader {
332332
isAbstract: true,
333333
typeParameters: freshParameters.freshTypeParameters,
334334
supertype: freshParameters.substituteSuper(supertype),
335-
mixedInType: freshParameters.substituteSuper(mixinType));
335+
mixedInType: freshParameters.substituteSuper(mixinType),
336+
fileUri: classNode.fileUri);
336337
mixinClass.level = ast.ClassLevel.Type;
337338
supertype = new ast.Supertype(mixinClass,
338339
classNode.typeParameters.map(makeTypeParameterType).toList());
@@ -445,7 +446,7 @@ class DartLoader implements ReferenceLevelLoader {
445446
isStatic: true,
446447
isExternal: constructor.isExternal,
447448
isConst: constructor.isConst,
448-
fileUri: "file://${element.source.fullName}");
449+
fileUri: '${element.source.uri}');
449450
}
450451
return new ast.Constructor(scope.buildFunctionInterface(constructor),
451452
name: _nameOfMember(element),
@@ -460,8 +461,7 @@ class DartLoader implements ReferenceLevelLoader {
460461
isFinal: variable.isFinal,
461462
isConst: variable.isConst,
462463
type: scope.buildType(variable.type),
463-
fileUri: "file://${element.source.fullName}")
464-
..fileOffset = element.nameOffset;
464+
fileUri: '${element.source.uri}')..fileOffset = element.nameOffset;
465465

466466
case ElementKind.METHOD:
467467
case ElementKind.GETTER:
@@ -480,7 +480,7 @@ class DartLoader implements ReferenceLevelLoader {
480480
isAbstract: executable.isAbstract,
481481
isStatic: executable.isStatic,
482482
isExternal: executable.isExternal,
483-
fileUri: "file://${element.source.fullName}");
483+
fileUri: '${element.source.uri}');
484484

485485
default:
486486
throw 'Unexpected member kind: $element';
@@ -586,7 +586,7 @@ class DartLoader implements ReferenceLevelLoader {
586586
typeParameters: fresh.freshTypeParameters,
587587
supertype: new ast.Supertype(superclass, superArgs),
588588
mixedInType: new ast.Supertype(mixedInClass, mixinArgs),
589-
fileUri: mixedInClass.fileUri);
589+
fileUri: library.fileUri);
590590
result.level = ast.ClassLevel.Type;
591591
library.addClass(result);
592592
return result;
@@ -694,9 +694,15 @@ class DartLoader implements ReferenceLevelLoader {
694694
in libraryElement.units) {
695695
var source = compilationUnitElement.source;
696696
LineInfo lineInfo = context.computeLineInfo(source);
697-
program.uriToLineStarts["file://${source.fullName}"] =
698-
new List<int>.generate(lineInfo.lineCount, lineInfo.getOffsetOfLine,
699-
growable: false);
697+
String sourceCode;
698+
try {
699+
sourceCode = context.getContents(source).data;
700+
} catch (e) {
701+
// The source's contents could not be accessed.
702+
sourceCode = '';
703+
}
704+
program.uriToSource['${source.uri}'] =
705+
new ast.Source(lineInfo.lineStarts, sourceCode);
700706
}
701707
}
702708
return program;

pkg/kernel/lib/ast.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,17 +3504,17 @@ class Supertype extends Node {
35043504
class Program extends TreeNode {
35053505
final List<Library> libraries;
35063506

3507-
/// Map from a source file uri to a line-starts table.
3507+
/// Map from a source file uri to a line-starts table and source code.
35083508
/// Given a source file uri and a offset in that file one can translate
35093509
/// it to a line:column position in that file.
3510-
final Map<String, List<int>> uriToLineStarts;
3510+
final Map<String, Source> uriToSource;
35113511

35123512
/// Reference to the main method in one of the libraries.
35133513
Procedure mainMethod;
35143514

3515-
Program([List<Library> libraries, Map<String, List<int>> uriToLineStarts])
3515+
Program([List<Library> libraries, Map<String, Source> uriToSource])
35163516
: libraries = libraries ?? <Library>[],
3517-
uriToLineStarts = uriToLineStarts ?? {} {
3517+
uriToSource = uriToSource ?? <String, Source>{} {
35183518
setParents(libraries, this);
35193519
}
35203520

@@ -3533,7 +3533,7 @@ class Program extends TreeNode {
35333533

35343534
/// Translates an offset to line and column numbers in the given file.
35353535
Location getLocation(String file, int offset) {
3536-
List<int> lines = uriToLineStarts[file];
3536+
List<int> lines = uriToSource[file].lineStarts;
35373537
int low = 0, high = lines.length - 1;
35383538
while (low < high) {
35393539
int mid = high - ((high - low) >> 1); // Get middle, rounding up.
@@ -3651,3 +3651,10 @@ class _ChildReplacer extends Transformer {
36513651
}
36523652
}
36533653
}
3654+
3655+
class Source {
3656+
final List<int> lineStarts;
3657+
final String source;
3658+
3659+
Source(this.lineStarts, this.source);
3660+
}

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class BinaryBuilder {
177177
'Magic number was: ${magic.toRadixString(16)}');
178178
}
179179
readStringTable();
180-
Map<String, List<int>> uriToLineStarts = readUriToLineStarts();
180+
Map<String, Source> uriToSource = readUriToSource();
181181
importTable.length = readUInt();
182182
for (int i = 0; i < importTable.length; ++i) {
183183
importTable[i] = new Library(null);
@@ -187,15 +187,17 @@ class BinaryBuilder {
187187
readLibrary();
188188
}
189189
var mainMethod = readMemberReference(allowNull: true);
190-
return new Program(importTable, uriToLineStarts)..mainMethod = mainMethod;
190+
return new Program(importTable, uriToSource)
191+
..mainMethod = mainMethod;
191192
}
192193

193-
Map<String, List<int>> readUriToLineStarts() {
194+
Map<String, Source> readUriToSource() {
194195
readSourceUriTable();
195196
int length = _sourceUriTable.length;
196-
Map<String, List<int>> uriToLineStarts = {};
197+
Map<String, Source> uriToLineStarts = <String, Source>{};
197198
for (int i = 0; i < length; ++i) {
198199
String uri = _sourceUriTable[i];
200+
String sourceCode = readStringEntry();
199201
int lineCount = readUInt();
200202
List<int> lineStarts = new List<int>(lineCount);
201203
int previousLineStart = 0;
@@ -204,7 +206,7 @@ class BinaryBuilder {
204206
lineStarts[j] = lineStart;
205207
previousLineStart = lineStart;
206208
}
207-
uriToLineStarts[uri] = lineStarts;
209+
uriToLineStarts[uri] = new Source(lineStarts, sourceCode);
208210
}
209211
return uriToLineStarts;
210212
}

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,23 @@ class BinaryPrinter extends Visitor {
137137
_importTable = new ProgramImportTable(program);
138138
_stringIndexer.build(program);
139139
writeStringTable(_stringIndexer);
140-
writeUriToLineStarts(program);
140+
writeUriToSource(program);
141141
writeList(program.libraries, writeNode);
142142
writeMemberReference(program.mainMethod, allowNull: true);
143143
_flush();
144144
}
145145

146-
void writeUriToLineStarts(Program program) {
147-
program.uriToLineStarts.keys.forEach((uri) {
146+
void writeUriToSource(Program program) {
147+
program.uriToSource.keys.forEach((uri) {
148148
_sourceUriIndexer.put(uri);
149149
});
150150
writeStringTable(_sourceUriIndexer);
151151
for (int i = 0; i < _sourceUriIndexer.entries.length; i++) {
152152
String uri = _sourceUriIndexer.entries[i].value;
153-
List<int> lineStarts = program.uriToLineStarts[uri] ?? [];
153+
Source source = program.uriToSource[uri] ?? new Source([], '');
154+
String sourceCode = source.source;
155+
writeStringTableEntry(sourceCode);
156+
List<int> lineStarts = source.lineStarts;
154157
writeUInt30(lineStarts.length);
155158
int previousLineStart = 0;
156159
lineStarts.forEach((lineStart) {

runtime/vm/kernel.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,29 +311,37 @@ class StringTable {
311311
};
312312

313313

314-
class LineStartingTable {
314+
class SourceTable {
315315
public:
316316
void ReadFrom(Reader* reader);
317317
void WriteTo(Writer* writer);
318-
~LineStartingTable() {
318+
~SourceTable() {
319319
for (intptr_t i = 0; i < size_; ++i) {
320-
delete[] values_[i];
320+
delete source_code_[i];
321+
delete[] line_starts_[i];
321322
}
322-
delete[] values_;
323+
delete[] source_code_;
324+
delete[] line_starts_;
325+
delete[] line_count_;
323326
}
324327

325328
intptr_t size() { return size_; }
326-
intptr_t* valuesFor(int i) { return values_[i]; }
329+
String* SourceFor(intptr_t i) { return source_code_[i]; }
330+
intptr_t* LineStartsFor(intptr_t i) { return line_starts_[i]; }
331+
intptr_t LineCountFor(intptr_t i) { return line_count_[i]; }
327332

328333
private:
329-
LineStartingTable() : values_(NULL), size_(0) {}
334+
SourceTable()
335+
: source_code_(NULL), line_starts_(NULL), line_count_(NULL), size_(0) {}
330336

331337
friend class Program;
332338

333-
intptr_t** values_;
339+
String** source_code_;
340+
intptr_t** line_starts_;
341+
intptr_t* line_count_;
334342
intptr_t size_;
335343

336-
DISALLOW_COPY_AND_ASSIGN(LineStartingTable);
344+
DISALLOW_COPY_AND_ASSIGN(SourceTable);
337345
};
338346

339347
// Forward declare all classes.
@@ -2798,7 +2806,7 @@ class Program : public TreeNode {
27982806

27992807
StringTable& string_table() { return string_table_; }
28002808
StringTable& source_uri_table() { return source_uri_table_; }
2801-
LineStartingTable& line_starting_table() { return line_starting_table_; }
2809+
SourceTable& source_table() { return source_table_; }
28022810
List<Library>& libraries() { return libraries_; }
28032811
Procedure* main_method() { return main_method_; }
28042812

@@ -2809,7 +2817,7 @@ class Program : public TreeNode {
28092817
Ref<Procedure> main_method_;
28102818
StringTable string_table_;
28112819
StringTable source_uri_table_;
2812-
LineStartingTable line_starting_table_;
2820+
SourceTable source_table_;
28132821

28142822
DISALLOW_COPY_AND_ASSIGN(Program);
28152823
};

runtime/vm/kernel_binary.cc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -828,33 +828,37 @@ void StringTable::WriteTo(Writer* writer) {
828828
}
829829

830830

831-
void LineStartingTable::ReadFrom(Reader* reader) {
831+
void SourceTable::ReadFrom(Reader* reader) {
832832
size_ = reader->helper()->program()->source_uri_table().strings().length();
833-
values_ = new intptr_t*[size_];
833+
source_code_ = new String*[size_];
834+
line_starts_ = new intptr_t*[size_];
835+
line_count_ = new intptr_t[size_];
834836
for (intptr_t i = 0; i < size_; ++i) {
837+
source_code_[i] = StringImpl::ReadFrom(reader);
835838
intptr_t line_count = reader->ReadUInt();
836-
intptr_t* line_starts = new intptr_t[line_count + 1];
837-
line_starts[0] = line_count;
839+
intptr_t* line_starts = new intptr_t[line_count];
840+
line_count_[i] = line_count;
838841
intptr_t previous_line_start = 0;
839842
for (intptr_t j = 0; j < line_count; ++j) {
840843
intptr_t line_start = reader->ReadUInt() + previous_line_start;
841-
line_starts[j + 1] = line_start;
844+
line_starts[j] = line_start;
842845
previous_line_start = line_start;
843846
}
844-
values_[i] = line_starts;
847+
line_starts_[i] = line_starts;
845848
}
846849
}
847850

848851

849-
void LineStartingTable::WriteTo(Writer* writer) {
852+
void SourceTable::WriteTo(Writer* writer) {
850853
for (intptr_t i = 0; i < size_; ++i) {
851-
intptr_t* line_starts = values_[i];
852-
intptr_t line_count = line_starts[0];
854+
StringImpl::WriteTo(writer, source_code_[i]);
855+
intptr_t* line_starts = line_starts_[i];
856+
intptr_t line_count = line_count_[i];
853857
writer->WriteUInt(line_count);
854858

855859
intptr_t previous_line_start = 0;
856860
for (intptr_t j = 0; j < line_count; ++j) {
857-
intptr_t line_start = line_starts[j + 1];
861+
intptr_t line_start = line_starts[j];
858862
writer->WriteUInt(line_start - previous_line_start);
859863
previous_line_start = line_start;
860864
}
@@ -2828,7 +2832,7 @@ Program* Program::ReadFrom(Reader* reader) {
28282832

28292833
program->string_table_.ReadFrom(reader);
28302834
program->source_uri_table_.ReadFrom(reader);
2831-
program->line_starting_table_.ReadFrom(reader);
2835+
program->source_table_.ReadFrom(reader);
28322836

28332837
int libraries = reader->ReadUInt();
28342838
program->libraries().EnsureInitialized(libraries);
@@ -2853,7 +2857,7 @@ void Program::WriteTo(Writer* writer) {
28532857
// strings in nodes are present in [string_table_].
28542858
string_table_.WriteTo(writer);
28552859
source_uri_table_.WriteTo(writer);
2856-
line_starting_table_.WriteTo(writer);
2860+
source_table_.WriteTo(writer);
28572861

28582862
libraries_.WriteTo(writer);
28592863
Reference::WriteMemberTo(writer, main_method_);

0 commit comments

Comments
 (0)