Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: lib/ast.dart

Issue 2448873004: Add convenience getter to extract the location of an AST node. (Closed)
Patch Set: Remove unintended changes Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// ----------------------------------------------------------------------- 5 /// -----------------------------------------------------------------------
6 /// ERROR HANDLING 6 /// ERROR HANDLING
7 /// ----------------------------------------------------------------------- 7 /// -----------------------------------------------------------------------
8 /// 8 ///
9 /// As a rule of thumb, errors that can be detected statically are handled by 9 /// As a rule of thumb, errors that can be detected statically are handled by
10 /// the frontend, typically by translating the erroneous code into a 'throw' or 10 /// the frontend, typically by translating the erroneous code into a 'throw' or
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 } 127 }
128 128
129 /// Removes this node from the [List] it is currently stored in, or assigns 129 /// Removes this node from the [List] it is currently stored in, or assigns
130 /// `null` to the field on the parent currently pointing to the node. 130 /// `null` to the field on the parent currently pointing to the node.
131 /// 131 ///
132 /// Has no effect if the node is orphaned or if the parent pointer is stale. 132 /// Has no effect if the node is orphaned or if the parent pointer is stale.
133 void remove() { 133 void remove() {
134 parent?.replaceChild(this, null); 134 parent?.replaceChild(this, null);
135 parent = null; 135 parent = null;
136 } 136 }
137
138 Program get enclosingProgram => parent?.enclosingProgram;
139
140 /// Returns the best known source location of the given AST node, or `null` if
141 /// the node is orphaned.
142 ///
143 /// This getter is intended for diagnostics and debugging, and should be
144 /// avoided in production code.
145 Location get location {
146 if (fileOffset == noOffset) return parent?.location;
147 return _getLocationInEnclosingFile(fileOffset);
148 }
149
150 Location _getLocationInEnclosingFile(int offset) {
151 return parent?._getLocationInEnclosingFile(offset);
152 }
137 } 153 }
138 154
139 // ------------------------------------------------------------------------ 155 // ------------------------------------------------------------------------
140 // LIBRARIES and CLASSES 156 // LIBRARIES and CLASSES
141 // ------------------------------------------------------------------------ 157 // ------------------------------------------------------------------------
142 158
143 class Library extends TreeNode implements Comparable<Library> { 159 class Library extends TreeNode implements Comparable<Library> {
144 /// An absolute import path to this library. 160 /// An absolute import path to this library.
145 /// 161 ///
146 /// The [Uri] should have the `dart`, `package`, or `file` scheme. 162 /// The [Uri] should have the `dart`, `package`, or `file` scheme.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 } 233 }
218 234
219 static int _libraryIdCounter = 0; 235 static int _libraryIdCounter = 0;
220 int _libraryId = ++_libraryIdCounter; 236 int _libraryId = ++_libraryIdCounter;
221 237
222 int compareTo(Library other) => _libraryId - other._libraryId; 238 int compareTo(Library other) => _libraryId - other._libraryId;
223 239
224 /// Returns a possibly synthesized name for this library, consistent with 240 /// Returns a possibly synthesized name for this library, consistent with
225 /// the names across all [toString] calls. 241 /// the names across all [toString] calls.
226 String toString() => debugLibraryName(this); 242 String toString() => debugLibraryName(this);
243
244 Location _getLocationInEnclosingFile(int offset) {
245 return enclosingProgram.getLocation(fileUri, offset);
246 }
227 } 247 }
228 248
229 /// The degree to which the contents of a class have been loaded into memory. 249 /// The degree to which the contents of a class have been loaded into memory.
230 /// 250 ///
231 /// Each level imply the requirements of the previous ones. 251 /// Each level imply the requirements of the previous ones.
232 enum ClassLevel { 252 enum ClassLevel {
233 /// Temporary loading level for internal use by IR producers. Consumers of 253 /// Temporary loading level for internal use by IR producers. Consumers of
234 /// kernel code should not expect to see classes at this level. 254 /// kernel code should not expect to see classes at this level.
235 Temporary, 255 Temporary,
236 256
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 supertype = v.visitSupertype(supertype); 471 supertype = v.visitSupertype(supertype);
452 } 472 }
453 if (mixedInType != null) { 473 if (mixedInType != null) {
454 mixedInType = v.visitSupertype(mixedInType); 474 mixedInType = v.visitSupertype(mixedInType);
455 } 475 }
456 transformSupertypeList(implementedTypes, v); 476 transformSupertypeList(implementedTypes, v);
457 transformList(constructors, v, this); 477 transformList(constructors, v, this);
458 transformList(procedures, v, this); 478 transformList(procedures, v, this);
459 transformList(fields, v, this); 479 transformList(fields, v, this);
460 } 480 }
481
482 Location _getLocationInEnclosingFile(int offset) {
483 return enclosingProgram.getLocation(fileUri, offset);
484 }
461 } 485 }
462 486
463 // ------------------------------------------------------------------------ 487 // ------------------------------------------------------------------------
464 // MEMBERS 488 // MEMBERS
465 // ------------------------------------------------------------------------ 489 // ------------------------------------------------------------------------
466 490
467 /// A indirect reference to a member, which can be updated to point at another 491 /// A indirect reference to a member, which can be updated to point at another
468 /// member at a later time. 492 /// member at a later time.
469 class _MemberAccessor { 493 class _MemberAccessor {
470 Member target; 494 Member target;
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 /// 743 ///
720 /// [DirectPropertySet] and [FieldInitializer]s are not affected, and will 744 /// [DirectPropertySet] and [FieldInitializer]s are not affected, and will
721 /// continue to access the field directly. [PropertySet] nodes created after 745 /// continue to access the field directly. [PropertySet] nodes created after
722 /// the call will not be affected until the method is called again. 746 /// the call will not be affected until the method is called again.
723 /// 747 ///
724 /// Existing [ClassHierarchy] instances are not affected by this call. 748 /// Existing [ClassHierarchy] instances are not affected by this call.
725 void replaceSetterInterfaceWith(Procedure setter) { 749 void replaceSetterInterfaceWith(Procedure setter) {
726 _setterInterface.target = setter; 750 _setterInterface.target = setter;
727 _setterInterface = new _MemberAccessor(this); 751 _setterInterface = new _MemberAccessor(this);
728 } 752 }
753
754 Location _getLocationInEnclosingFile(int offset) {
755 return enclosingProgram.getLocation(fileUri, offset);
756 }
729 } 757 }
730 758
731 /// A generative constructor, possibly redirecting. 759 /// A generative constructor, possibly redirecting.
732 /// 760 ///
733 /// Note that factory constructors are treated as [Procedure]s. 761 /// Note that factory constructors are treated as [Procedure]s.
734 /// 762 ///
735 /// Constructors do not take type parameters. Type arguments from a constructor 763 /// Constructors do not take type parameters. Type arguments from a constructor
736 /// invocation should be matched with the type parameters declared in the class. 764 /// invocation should be matched with the type parameters declared in the class.
737 /// 765 ///
738 /// For unnamed constructors, the name is an empty string (in a [Name]). 766 /// For unnamed constructors, the name is an empty string (in a [Name]).
(...skipping 2752 matching lines...) Expand 10 before | Expand all | Expand 10 after
3491 accept(TreeVisitor v) => v.visitProgram(this); 3519 accept(TreeVisitor v) => v.visitProgram(this);
3492 3520
3493 visitChildren(Visitor v) { 3521 visitChildren(Visitor v) {
3494 visitList(libraries, v); 3522 visitList(libraries, v);
3495 mainMethod?.acceptReference(v); 3523 mainMethod?.acceptReference(v);
3496 } 3524 }
3497 3525
3498 transformChildren(Transformer v) { 3526 transformChildren(Transformer v) {
3499 transformList(libraries, v, this); 3527 transformList(libraries, v, this);
3500 } 3528 }
3529
3530 Program get enclosingProgram => this;
3531
3532 /// Translates an offset to line and column numbers in the given file.
3533 Location getLocation(String file, int offset) {
3534 List<int> lines = uriToLineStarts[file];
3535 int low = 0, high = lines.length - 1;
3536 while (low < high) {
3537 int mid = high - ((high - low) >> 1); // Get middle, rounding up.
3538 int pivot = lines[mid];
3539 if (pivot <= offset) {
3540 low = mid;
3541 } else {
3542 high = mid - 1;
3543 }
3544 }
3545 int lineIndex = low;
3546 int lineStart = lines[lineIndex];
3547 int lineNumber = 1 + lineIndex;
3548 int columnNumber = offset - lineStart;
3549 return new Location(file, lineNumber, columnNumber);
3550 }
3551 }
3552
3553 /// A tuple with file, line, and column number, for displaying human-readable
ahe 2016/10/25 14:43:35 (and Emacs-readable) Nice touch :-)
3554 /// locations.
3555 class Location {
3556 final String file;
3557 final int line; // 1-based.
3558 final int column; // 1-based.
3559
3560 Location(this.file, this.line, this.column);
3561
3562 String toString() => '$file:$line:$column';
3501 } 3563 }
3502 3564
3503 // ------------------------------------------------------------------------ 3565 // ------------------------------------------------------------------------
3504 // INTERNAL FUNCTIONS 3566 // INTERNAL FUNCTIONS
3505 // ------------------------------------------------------------------------ 3567 // ------------------------------------------------------------------------
3506 3568
3507 void setParents(List<TreeNode> nodes, TreeNode parent) { 3569 void setParents(List<TreeNode> nodes, TreeNode parent) {
3508 for (int i = 0; i < nodes.length; ++i) { 3570 for (int i = 0; i < nodes.length; ++i) {
3509 nodes[i].parent = parent; 3571 nodes[i].parent = parent;
3510 } 3572 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
3580 3642
3581 @override 3643 @override
3582 defaultTreeNode(TreeNode node) { 3644 defaultTreeNode(TreeNode node) {
3583 if (node == child) { 3645 if (node == child) {
3584 return replacement; 3646 return replacement;
3585 } else { 3647 } else {
3586 return node; 3648 return node;
3587 } 3649 }
3588 } 3650 }
3589 } 3651 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698