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

Side by Side Diff: runtime/vm/object.cc

Issue 1071703002: Remove some duplication in code that extracts the source for a function. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months 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 | Annotate | Revision Log
« runtime/vm/compiler_test.cc ('K') | « runtime/vm/object.h ('k') | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 6580 matching lines...) Expand 10 before | Expand all | Expand 10 after
6591 } else { 6591 } else {
6592 tmp = cls.UserVisibleName(); 6592 tmp = cls.UserVisibleName();
6593 } 6593 }
6594 } 6594 }
6595 tmp = String::Concat(tmp, Symbols::Dot()); 6595 tmp = String::Concat(tmp, Symbols::Dot());
6596 const String& suffix = String::Handle(UserVisibleName()); 6596 const String& suffix = String::Handle(UserVisibleName());
6597 return String::Concat(tmp, suffix); 6597 return String::Concat(tmp, suffix);
6598 } 6598 }
6599 6599
6600 6600
6601 RawString* Function::GetSource() { 6601 RawString* Function::GetSource() const {
6602 if (IsImplicitConstructor() || IsSignatureFunction()) {
6603 // We may need to handle more cases when the restrictions on mixins are
6604 // relaxed. In particular we might start associating some source with the
6605 // forwarding constructors when it becomes possible to specify a particular
6606 // constructor from the mixin to use.
6607 return String::null();
6608 }
6602 const Script& func_script = Script::Handle(script()); 6609 const Script& func_script = Script::Handle(script());
6603 // Without the + 1 the final "}" is not included. 6610 const TokenStream& stream = TokenStream::Handle(func_script.tokens());
6604 return func_script.GetSnippet(token_pos(), end_token_pos() + 1); 6611 if (!func_script.HasSource()) {
6612 // When source is not available, avoid printing the whole token stream and
6613 // doing expensive position calculations.
6614 return stream.GenerateSource(token_pos(), end_token_pos() + 1);
6615 }
6616
6617 const TokenStream::Iterator tkit(stream, end_token_pos());
6618 intptr_t from_line;
6619 intptr_t from_col;
6620 intptr_t to_line;
6621 intptr_t to_col;
6622 func_script.GetTokenLocation(token_pos(), &from_line, &from_col);
6623 func_script.GetTokenLocation(end_token_pos(), &to_line, &to_col);
6624 intptr_t last_tok_len = String::Handle(tkit.CurrentLiteral()).Length();
6625 // Handle special cases for end tokens of closures (where we exclude the last
6626 // token):
6627 // (1) "foo(() => null, bar);": End token is `,', but we don't print it.
6628 // (2) "foo(() => null);": End token is ')`, but we don't print it.
6629 // (3) "var foo = () => null;": End token is `;', but in this case the token
6630 // semicolon belongs to the assignment so we skip it.
6631 if ((tkit.CurrentTokenKind() == Token::kCOMMA) || // Case 1.
6632 (tkit.CurrentTokenKind() == Token::kRPAREN) || // Case 2.
6633 (tkit.CurrentTokenKind() == Token::kSEMICOLON &&
6634 String::Handle(name()).Equals("<anonymous closure>"))) { // Case 3.
6635 last_tok_len = 0;
6636 }
6637 const String& result = String::Handle(func_script.GetSnippet(
6638 from_line, from_col, to_line, to_col + last_tok_len));
6639 ASSERT(!result.IsNull());
6640 return result.raw();
6605 } 6641 }
6606 6642
6607 6643
6608 // Construct fingerprint from token stream. The token stream contains also 6644 // Construct fingerprint from token stream. The token stream contains also
6609 // arguments. 6645 // arguments.
6610 int32_t Function::SourceFingerprint() const { 6646 int32_t Function::SourceFingerprint() const {
6611 uint32_t result = IsImplicitClosureFunction() 6647 uint32_t result = IsImplicitClosureFunction()
6612 ? String::Handle(Function::Handle(parent_function()).Signature()).Hash() 6648 ? String::Handle(Function::Handle(parent_function()).Signature()).Hash()
6613 : String::Handle(Signature()).Hash(); 6649 : String::Handle(Signature()).Hash();
6614 TokenStream::Iterator tokens_iterator(TokenStream::Handle( 6650 TokenStream::Iterator tokens_iterator(TokenStream::Handle(
(...skipping 1779 matching lines...) Expand 10 before | Expand all | Expand 10 after
8394 if (line_start_idx >= 0) { 8430 if (line_start_idx >= 0) {
8395 return String::SubString(src, 8431 return String::SubString(src,
8396 line_start_idx, 8432 line_start_idx,
8397 last_char_idx - line_start_idx + 1); 8433 last_char_idx - line_start_idx + 1);
8398 } else { 8434 } else {
8399 return Symbols::Empty().raw(); 8435 return Symbols::Empty().raw();
8400 } 8436 }
8401 } 8437 }
8402 8438
8403 8439
8404 RawString* Script::GetSnippet(intptr_t from_token_pos,
8405 intptr_t to_token_pos) const {
8406 intptr_t from_line, from_column;
8407 intptr_t to_line, to_column;
8408 GetTokenLocation(from_token_pos, &from_line, &from_column);
8409 GetTokenLocation(to_token_pos, &to_line, &to_column);
8410 return GetSnippet(from_line, from_column, to_line, to_column);
8411 }
8412
8413
8414 RawString* Script::GetSnippet(intptr_t from_line, 8440 RawString* Script::GetSnippet(intptr_t from_line,
8415 intptr_t from_column, 8441 intptr_t from_column,
8416 intptr_t to_line, 8442 intptr_t to_line,
8417 intptr_t to_column) const { 8443 intptr_t to_column) const {
8418 const String& src = String::Handle(Source()); 8444 const String& src = String::Handle(Source());
8419 intptr_t length = src.Length(); 8445 intptr_t length = src.Length();
8420 intptr_t line = 1 + line_offset(); 8446 intptr_t line = 1 + line_offset();
8421 intptr_t column = 1; 8447 intptr_t column = 1;
8422 intptr_t scan_position = 0; 8448 intptr_t scan_position = 0;
8423 intptr_t snippet_start = -1; 8449 intptr_t snippet_start = -1;
(...skipping 12301 matching lines...) Expand 10 before | Expand all | Expand 10 after
20725 return tag_label.ToCString(); 20751 return tag_label.ToCString();
20726 } 20752 }
20727 20753
20728 20754
20729 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 20755 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
20730 Instance::PrintJSONImpl(stream, ref); 20756 Instance::PrintJSONImpl(stream, ref);
20731 } 20757 }
20732 20758
20733 20759
20734 } // namespace dart 20760 } // namespace dart
OLDNEW
« runtime/vm/compiler_test.cc ('K') | « runtime/vm/object.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698