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

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

Issue 510033002: Print function scopes with indents. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add newline before first parameter Created 6 years, 3 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
« no previous file with comments | « runtime/vm/ast_printer.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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/ast_printer.h" 5 #include "vm/ast_printer.h"
6 6
7 #include "vm/handles.h" 7 #include "vm/handles.h"
8 #include "vm/object.h" 8 #include "vm/object.h"
9 #include "vm/os.h" 9 #include "vm/os.h"
10 #include "vm/parser.h" 10 #include "vm/parser.h"
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 403
404 404
405 void AstPrinter::PrintNode(AstNode* node) { 405 void AstPrinter::PrintNode(AstNode* node) {
406 ASSERT(node != NULL); 406 ASSERT(node != NULL);
407 AstPrinter ast_printer; 407 AstPrinter ast_printer;
408 node->Visit(&ast_printer); 408 node->Visit(&ast_printer);
409 OS::Print("\n"); 409 OS::Print("\n");
410 } 410 }
411 411
412 412
413 static void IndentN(int count) {
414 for (int i = 0; i < count; i++) {
415 OS::Print(" ");
416 }
417 }
418
419
413 void AstPrinter::PrintLocalScopeVariable(const LocalScope* scope, 420 void AstPrinter::PrintLocalScopeVariable(const LocalScope* scope,
414 LocalVariable* var) { 421 LocalVariable* var,
422 int indent) {
415 ASSERT(scope != NULL); 423 ASSERT(scope != NULL);
416 ASSERT(var != NULL); 424 ASSERT(var != NULL);
425 IndentN(indent);
417 OS::Print("(%s%s '%s'", 426 OS::Print("(%s%s '%s'",
418 var->is_final() ? "final " : "", 427 var->is_final() ? "final " : "",
419 String::Handle(var->type().Name()).ToCString(), 428 String::Handle(var->type().Name()).ToCString(),
420 var->name().ToCString()); 429 var->name().ToCString());
421 if (var->owner() != scope) { 430 if (var->owner() != scope) {
422 OS::Print(" alias"); 431 OS::Print(" alias");
423 } 432 }
424 if (var->HasIndex()) { 433 if (var->HasIndex()) {
425 OS::Print(" @%d", var->index()); 434 OS::Print(" @%d", var->index());
426 if (var->is_captured()) { 435 if (var->is_captured()) {
427 OS::Print(" ctx %d", var->owner()->context_level()); 436 OS::Print(" ctx %d", var->owner()->context_level());
428 } 437 }
429 } else if (var->owner()->function_level() != 0) { 438 } else if (var->owner()->function_level() != 0) {
430 OS::Print(" lev %d", var->owner()->function_level()); 439 OS::Print(" lev %d", var->owner()->function_level());
431 } 440 }
432 OS::Print(" valid %" Pd "-%" Pd ")", 441 OS::Print(" valid %" Pd "-%" Pd ")\n",
433 var->token_pos(), 442 var->token_pos(),
434 scope->end_token_pos()); 443 scope->end_token_pos());
435 } 444 }
436 445
437 446
438 void AstPrinter::PrintLocalScope(const LocalScope* scope, 447 void AstPrinter::PrintLocalScope(const LocalScope* scope,
439 int start_index) { 448 int start_index,
449 int indent) {
440 ASSERT(scope != NULL); 450 ASSERT(scope != NULL);
441 for (int i = start_index; i < scope->num_variables(); i++) { 451 for (int i = start_index; i < scope->num_variables(); i++) {
442 LocalVariable* var = scope->VariableAt(i); 452 LocalVariable* var = scope->VariableAt(i);
443 PrintLocalScopeVariable(scope, var); 453 PrintLocalScopeVariable(scope, var, indent);
444 } 454 }
445 const LocalScope* child = scope->child(); 455 const LocalScope* child = scope->child();
446 while (child != NULL) { 456 while (child != NULL) {
457 IndentN(indent);
447 OS::Print("{scope %p ", child); 458 OS::Print("{scope %p ", child);
448 if (child->HasContextLevel()) { 459 if (child->HasContextLevel()) {
449 OS::Print("ctx %d numctxvar %d ", 460 OS::Print("ctx %d numctxvar %d ",
450 child->context_level(), 461 child->context_level(),
451 child->num_context_variables()); 462 child->num_context_variables());
452 } 463 }
453 OS::Print("llev %d ", child->loop_level()); 464 OS::Print("llev %d\n", child->loop_level());
454 PrintLocalScope(child, 0); 465 PrintLocalScope(child, 0, indent + kScopeIndent);
455 OS::Print("}"); 466 IndentN(indent);
467 OS::Print("}\n");
456 child = child->sibling(); 468 child = child->sibling();
457 } 469 }
458 } 470 }
459 471
460 472
461 void AstPrinter::PrintFunctionScope(const ParsedFunction& parsed_function) { 473 void AstPrinter::PrintFunctionScope(const ParsedFunction& parsed_function) {
462 HANDLESCOPE(Isolate::Current()); 474 HANDLESCOPE(Isolate::Current());
463 const Function& function = parsed_function.function(); 475 const Function& function = parsed_function.function();
464 const Array& default_parameter_values = 476 const Array& default_parameter_values =
465 parsed_function.default_parameter_values(); 477 parsed_function.default_parameter_values();
466 SequenceNode* node_sequence = parsed_function.node_sequence(); 478 SequenceNode* node_sequence = parsed_function.node_sequence();
467 ASSERT(node_sequence != NULL); 479 ASSERT(node_sequence != NULL);
468 const LocalScope* scope = node_sequence->scope(); 480 const LocalScope* scope = node_sequence->scope();
469 ASSERT(scope != NULL); 481 ASSERT(scope != NULL);
470 const char* function_name = function.ToFullyQualifiedCString(); 482 const char* function_name = function.ToFullyQualifiedCString();
471 OS::Print("Scope for function '%s' {scope %p ", function_name, scope); 483 OS::Print("Scope for function '%s'\n{scope %p ", function_name, scope);
472 if (scope->HasContextLevel()) { 484 if (scope->HasContextLevel()) {
473 OS::Print("ctx %d numctxvar %d ", 485 OS::Print("ctx %d numctxvar %d ",
474 scope->context_level(), 486 scope->context_level(),
475 scope->num_context_variables()); 487 scope->num_context_variables());
476 } 488 }
477 OS::Print("llev %d ", scope->loop_level()); 489 OS::Print("llev %d\n", scope->loop_level());
478 const int num_fixed_params = function.num_fixed_parameters(); 490 const int num_fixed_params = function.num_fixed_parameters();
479 const int num_params = num_fixed_params + function.NumOptionalParameters(); 491 const int num_params = num_fixed_params + function.NumOptionalParameters();
480 // Parameters must be listed first and must all appear in the top scope. 492 // Parameters must be listed first and must all appear in the top scope.
481 ASSERT(num_params <= scope->num_variables()); 493 ASSERT(num_params <= scope->num_variables());
482 int pos = 0; // Current position of variable in scope. 494 int pos = 0; // Current position of variable in scope.
495 int indent = kScopeIndent;
483 while (pos < num_params) { 496 while (pos < num_params) {
484 LocalVariable* param = scope->VariableAt(pos); 497 LocalVariable* param = scope->VariableAt(pos);
485 ASSERT(param->owner() == scope); // No aliases should precede parameters. 498 ASSERT(param->owner() == scope); // No aliases should precede parameters.
499 IndentN(indent);
486 OS::Print("(param %s%s '%s'", 500 OS::Print("(param %s%s '%s'",
487 param->is_final() ? "final " : "", 501 param->is_final() ? "final " : "",
488 String::Handle(param->type().Name()).ToCString(), 502 String::Handle(param->type().Name()).ToCString(),
489 param->name().ToCString()); 503 param->name().ToCString());
490 // Print the default value if the parameter is optional. 504 // Print the default value if the parameter is optional.
491 if (pos >= num_fixed_params && pos < num_params) { 505 if (pos >= num_fixed_params && pos < num_params) {
492 const Object& default_parameter_value = Object::Handle( 506 const Object& default_parameter_value = Object::Handle(
493 default_parameter_values.At(pos - num_fixed_params)); 507 default_parameter_values.At(pos - num_fixed_params));
494 OS::Print(" =%s", default_parameter_value.ToCString()); 508 OS::Print(" =%s", default_parameter_value.ToCString());
495 } 509 }
496 if (param->HasIndex()) { 510 if (param->HasIndex()) {
497 OS::Print(" @%d", param->index()); 511 OS::Print(" @%d", param->index());
498 if (param->is_captured()) { 512 if (param->is_captured()) {
499 OS::Print(" ctx %d", param->owner()->context_level()); 513 OS::Print(" ctx %d", param->owner()->context_level());
500 } 514 }
501 } 515 }
502 OS::Print(" valid %" Pd "-%" Pd ")", 516 OS::Print(" valid %" Pd "-%" Pd ")\n",
503 param->token_pos(), 517 param->token_pos(),
504 scope->end_token_pos()); 518 scope->end_token_pos());
505 pos++; 519 pos++;
506 } 520 }
507 // Visit remaining non-parameter variables and children scopes. 521 // Visit remaining non-parameter variables and children scopes.
508 PrintLocalScope(scope, pos); 522 PrintLocalScope(scope, pos, indent);
509 OS::Print("}\n"); 523 OS::Print("}\n");
510 } 524 }
511 525
512 526
513 void AstPrinter::PrintFunctionNodes(const ParsedFunction& parsed_function) { 527 void AstPrinter::PrintFunctionNodes(const ParsedFunction& parsed_function) {
514 HANDLESCOPE(Isolate::Current()); 528 HANDLESCOPE(Isolate::Current());
515 SequenceNode* node_sequence = parsed_function.node_sequence(); 529 SequenceNode* node_sequence = parsed_function.node_sequence();
516 ASSERT(node_sequence != NULL); 530 ASSERT(node_sequence != NULL);
517 AstPrinter ast_printer; 531 AstPrinter ast_printer;
518 const char* function_name = 532 const char* function_name =
519 parsed_function.function().ToFullyQualifiedCString(); 533 parsed_function.function().ToFullyQualifiedCString();
520 OS::Print("Ast for function '%s' {\n", function_name); 534 OS::Print("Ast for function '%s' {\n", function_name);
521 node_sequence->Visit(&ast_printer); 535 node_sequence->Visit(&ast_printer);
522 OS::Print("}\n"); 536 OS::Print("}\n");
523 } 537 }
524 538
525 } // namespace dart 539 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/ast_printer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698