1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
// ast-dump.cc -- AST debug dump. -*- C++ -*-
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include <iostream>
#include <fstream>
#include "gogo.h"
#include "expressions.h"
#include "statements.h"
#include "types.h"
#include "ast-dump.h"
#include "go-c.h"
#include "go-dump.h"
#include "go-diagnostics.h"
// The -fgo-dump-ast flag to activate AST dumps.
Go_dump ast_dump_flag("ast");
// This class is used to traverse the tree to look for blocks and
// function headers.
class Ast_dump_traverse_blocks_and_functions : public Traverse
{
public:
Ast_dump_traverse_blocks_and_functions(Ast_dump_context* ast_dump_context)
: Traverse(traverse_blocks | traverse_functions),
ast_dump_context_(ast_dump_context)
{ }
protected:
int
block(Block*);
int
function(Named_object*);
private:
Ast_dump_context* ast_dump_context_;
};
// This class is used to traverse the tree to look for statements.
class Ast_dump_traverse_statements : public Traverse
{
public:
Ast_dump_traverse_statements(Ast_dump_context* ast_dump_context)
: Traverse(traverse_statements),
ast_dump_context_(ast_dump_context)
{ }
protected:
int
statement(Block*, size_t* pindex, Statement*);
private:
Ast_dump_context* ast_dump_context_;
};
// For each block we enclose it in brackets.
int Ast_dump_traverse_blocks_and_functions::block(Block * block)
{
if (block == NULL)
{
this->ast_dump_context_->ostream() << std::endl;
return TRAVERSE_EXIT;
}
this->ast_dump_context_->print_indent();
this->ast_dump_context_->ostream() << "{" << std::endl;
this->ast_dump_context_->indent();
// Dump statememts.
Ast_dump_traverse_statements adts(this->ast_dump_context_);
block->traverse(&adts);
this->ast_dump_context_->unindent();
this->ast_dump_context_->print_indent();
this->ast_dump_context_->ostream() << "}" << std::endl;
return TRAVERSE_SKIP_COMPONENTS;
}
// Dump each traversed statement.
int
Ast_dump_traverse_statements::statement(Block* block, size_t* pindex,
Statement* statement)
{
statement->dump_statement(this->ast_dump_context_);
if (statement->is_block_statement())
{
Ast_dump_traverse_blocks_and_functions adtbf(this->ast_dump_context_);
statement->traverse(block, pindex, &adtbf);
}
return TRAVERSE_SKIP_COMPONENTS;
}
// Dump the function header.
int
Ast_dump_traverse_blocks_and_functions::function(Named_object* no)
{
this->ast_dump_context_->ostream() << no->name();
go_assert(no->is_function());
Function* func = no->func_value();
this->ast_dump_context_->ostream() << "(";
this->ast_dump_context_->dump_typed_identifier_list(
func->type()->parameters());
this->ast_dump_context_->ostream() << ")";
Function::Results* res = func->result_variables();
if (res != NULL && !res->empty())
{
this->ast_dump_context_->ostream() << " (";
for (Function::Results::const_iterator it = res->begin();
it != res->end();
it++)
{
if (it != res->begin())
this->ast_dump_context_->ostream() << ",";
Named_object* no = (*it);
this->ast_dump_context_->ostream() << no->name() << " ";
go_assert(no->is_result_variable());
Result_variable* resvar = no->result_var_value();
this->ast_dump_context_->dump_type(resvar->type());
}
this->ast_dump_context_->ostream() << ")";
}
this->ast_dump_context_->ostream() << " : ";
this->ast_dump_context_->dump_type(func->type());
this->ast_dump_context_->ostream() << std::endl;
return TRAVERSE_CONTINUE;
}
// Class Ast_dump_context.
Ast_dump_context::Ast_dump_context(std::ostream* out /* = NULL */,
bool dump_subblocks /* = true */)
: indent_(0), dump_subblocks_(dump_subblocks), ostream_(out), gogo_(NULL)
{
}
// Dump files will be named %basename%.dump.ast
const char* kAstDumpFileExtension = ".dump.ast";
// Dump the internal representation.
void
Ast_dump_context::dump(Gogo* gogo, const char* basename)
{
std::ofstream out;
std::string dumpname(basename);
dumpname += ".dump.ast";
out.open(dumpname.c_str());
if (out.fail())
{
go_error_at(Linemap::unknown_location(),
"cannot open %s:%m, -fgo-dump-ast ignored", dumpname.c_str());
return;
}
this->gogo_ = gogo;
this->ostream_ = &out;
Ast_dump_traverse_blocks_and_functions adtbf(this);
gogo->traverse(&adtbf);
out.close();
}
// Dump a textual representation of a type to the
// the dump file.
void
Ast_dump_context::dump_type(const Type* t)
{
if (t == NULL)
this->ostream() << "(nil type)";
else
// FIXME: write a type pretty printer instead of
// using mangled names.
if (this->gogo_ != NULL)
this->ostream() << "(" << t->mangled_name(this->gogo_) << ")";
}
// Dump a textual representation of a block to the
// the dump file.
void
Ast_dump_context::dump_block(Block* b)
{
Ast_dump_traverse_blocks_and_functions adtbf(this);
b->traverse(&adtbf);
}
// Dump a textual representation of an expression to the
// the dump file.
void
Ast_dump_context::dump_expression(const Expression* e)
{
e->dump_expression(this);
}
// Dump a textual representation of an expression list to the
// the dump file.
void
Ast_dump_context::dump_expression_list(const Expression_list* el,
bool as_pairs /* = false */)
{
if (el == NULL)
return;
for (std::vector<Expression*>::const_iterator it = el->begin();
it != el->end();
it++)
{
if ( it != el->begin())
this->ostream() << ",";
if (*it != NULL)
(*it)->dump_expression(this);
else
this->ostream() << "NULL";
if (as_pairs)
{
this->ostream() << ":";
++it;
(*it)->dump_expression(this);
}
}
}
// Dump a textual representation of a typed identifier to the
// the dump file.
void
Ast_dump_context::dump_typed_identifier(const Typed_identifier* ti)
{
this->ostream() << ti->name() << " ";
this->dump_type(ti->type());
}
// Dump a textual representation of a typed identifier list to the
// the dump file.
void
Ast_dump_context::dump_typed_identifier_list(
const Typed_identifier_list* ti_list)
{
if (ti_list == NULL)
return;
for (Typed_identifier_list::const_iterator it = ti_list->begin();
it != ti_list->end();
it++)
{
if (it != ti_list->begin())
this->ostream() << ",";
this->dump_typed_identifier(&(*it));
}
}
// Dump a textual representation of a temporary variable to the
// the dump file.
void
Ast_dump_context::dump_temp_variable_name(const Statement* s)
{
go_assert(s->classification() == Statement::STATEMENT_TEMPORARY);
// Use the statement address as part of the name for the temporary variable.
this->ostream() << "tmp." << (uintptr_t) s;
}
// Dump a textual representation of a label to the
// the dump file.
void
Ast_dump_context::dump_label_name(const Unnamed_label* l)
{
// Use the unnamed label address as part of the name for the temporary
// variable.
this->ostream() << "label." << (uintptr_t) l;
}
// Produce a textual representation of an operator symbol.
static const char*
op_string(Operator op)
{
// FIXME: This should be in line with symbols that are parsed,
// exported and/or imported.
switch (op)
{
case OPERATOR_PLUS:
return "+";
case OPERATOR_MINUS:
return "-";
case OPERATOR_NOT:
return "!";
case OPERATOR_XOR:
return "^";
case OPERATOR_OR:
return "|";
case OPERATOR_AND:
return "&";
case OPERATOR_MULT:
return "*";
case OPERATOR_OROR:
return "||";
case OPERATOR_ANDAND:
return "&&";
case OPERATOR_EQEQ:
return "==";
case OPERATOR_NOTEQ:
return "!=";
case OPERATOR_LT:
return "<";
case OPERATOR_LE:
return "<=";
case OPERATOR_GT:
return ">";
case OPERATOR_GE:
return ">=";
case OPERATOR_DIV:
return "/";
case OPERATOR_MOD:
return "%";
case OPERATOR_LSHIFT:
return "<<";
case OPERATOR_RSHIFT:
return "//";
case OPERATOR_BITCLEAR:
return "&^";
case OPERATOR_CHANOP:
return "<-";
case OPERATOR_PLUSEQ:
return "+=";
case OPERATOR_MINUSEQ:
return "-=";
case OPERATOR_OREQ:
return "|=";
case OPERATOR_XOREQ:
return "^=";
case OPERATOR_MULTEQ:
return "*=";
case OPERATOR_DIVEQ:
return "/=";
case OPERATOR_MODEQ:
return "%=";
case OPERATOR_LSHIFTEQ:
return "<<=";
case OPERATOR_RSHIFTEQ:
return ">>=";
case OPERATOR_ANDEQ:
return "&=";
case OPERATOR_BITCLEAREQ:
return "&^=";
case OPERATOR_PLUSPLUS:
return "++";
case OPERATOR_MINUSMINUS:
return "--";
case OPERATOR_COLON:
return ":";
case OPERATOR_COLONEQ:
return ":=";
case OPERATOR_SEMICOLON:
return ";";
case OPERATOR_DOT:
return ".";
case OPERATOR_ELLIPSIS:
return "...";
case OPERATOR_COMMA:
return ",";
case OPERATOR_LPAREN:
return "(";
case OPERATOR_RPAREN:
return ")";
case OPERATOR_LCURLY:
return "{";
case OPERATOR_RCURLY:
return "}";
case OPERATOR_LSQUARE:
return "[";
case OPERATOR_RSQUARE:
return "]";
default:
go_unreachable();
}
return NULL;
}
// Dump a textual representation of an operator to the
// the dump file.
void
Ast_dump_context::dump_operator(Operator op)
{
this->ostream() << op_string(op);
}
// Size of a single indent.
const int Ast_dump_context::offset_ = 2;
// Print indenting spaces to dump file.
void
Ast_dump_context::print_indent()
{
for (int i = 0; i < this->indent_ * this->offset_; i++)
this->ostream() << " ";
}
// Dump a textual representation of the ast to the
// the dump file.
void Gogo::dump_ast(const char* basename)
{
if (::ast_dump_flag.is_enabled())
{
Ast_dump_context adc;
adc.dump(this, basename);
}
}
// Implementation of String_dump interface.
void
Ast_dump_context::write_c_string(const char* s)
{
this->ostream() << s;
}
void
Ast_dump_context::write_string(const std::string& s)
{
this->ostream() << s;
}
// Dump statment to stream.
void
Ast_dump_context::dump_to_stream(const Statement* stm, std::ostream* out)
{
Ast_dump_context adc(out, false);
stm->dump_statement(&adc);
}
// Dump expression to stream.
void
Ast_dump_context::dump_to_stream(const Expression* expr, std::ostream* out)
{
Ast_dump_context adc(out, false);
expr->dump_expression(&adc);
}
|