Commit 16c57fe2 by Roberto Lublinerman Committed by Ian Lance Taylor

gccgo: Added code to dump the AST tree.

gccgo:	Added code to dump the AST tree. The AST dump is
	activated with -fgo-dump-ast.
	Initial version, it only dumps (most) constructs that
	are expected after the lowering transformation.

	* Make-lang.in (GO_OBJS): Add go/ast-dump.o.
	(go/ast-dump.o): New target.
	(go/expressions.o): Depend on go/gofrontend/ast-dump.h.
	(go/statements.o): Likewise.

From-SVN: r177225
parent 44e7bfcb
2011-08-02 Roberto Lublinerman <rluble@gmail.com>
* Make-lang.in (GO_OBJS): Add go/ast-dump.o.
(go/ast-dump.o): New target.
(go/expressions.o): Depend on go/gofrontend/ast-dump.h.
(go/statements.o): Likewise.
2011-07-06 Richard Guenther <rguenther@suse.de>
* go-lang.c (go_langhook_init):
......
......@@ -45,6 +45,7 @@ gccgo$(exeext): $(GCCGO_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a $(LIBDEPS)
go-warn = $(STRICT_WARN)
GO_OBJS = \
go/ast-dump.o \
go/dataflow.o \
go/export.o \
go/expressions.o \
......@@ -247,6 +248,9 @@ go/go-gcc.o: go/go-gcc.cc $(GO_SYSTEM_H) $(TREE_H) tree-iterator.h \
go/%.o: go/gofrontend/%.cc
$(CXX) -c $(GOINCLUDES) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) $< $(OUTPUT_OPTION)
go/ast-dump.o: go/gofrontend/ast-dump.cc $(GO_SYSTME_H) $(GO_GOGO_H) \
$(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) $(GO_TYPES_H) \
go/gofrontend/ast-dump.h $(GO_C_H) go/gofrontend/go-dump.h
go/dataflow.o: go/gofrontend/dataflow.cc $(GO_SYSTEM_H) $(GO_GOGO_H) \
$(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) go/gofrontend/dataflow.h
go/export.o: go/gofrontend/export.cc $(GO_SYSTEM_H) \
......@@ -256,7 +260,8 @@ go/expressions.o: go/gofrontend/expressions.cc $(GO_SYSTEM_H) $(TOPLEV_H) \
intl.h $(TREE_H) $(GIMPLE_H) tree-iterator.h convert.h $(REAL_H) \
realmpfr.h $(GO_C_H) $(GO_GOGO_H) $(GO_TYPES_H) \
go/gofrontend/export.h $(GO_IMPORT_H) $(GO_STATEMENTS_H) $(GO_LEX_H) \
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_EXPRESSIONS_H)
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_EXPRESSIONS_H) \
go/gofrontend/ast-dump.h
go/go.o: go/gofrontend/go.cc $(GO_SYSTEM_H) $(GO_C_H) $(GO_LEX_H) \
$(GO_PARSE_H) go/gofrontend/backend.h $(GO_GOGO_H)
go/go-dump.o: go/gofrontend/go-dump.cc $(GO_SYSTEM_H) $(GO_C_H) \
......@@ -285,7 +290,8 @@ go/runtime.o: go/gofrontend/runtime.cc $(GO_SYSTEM_H) $(GO_GOGO_H) \
go/gofrontend/runtime.def
go/statements.o: go/gofrontend/statements.cc $(GO_SYSTEM_H) \
$(GO_C_H) $(GO_TYPES_H) $(GO_EXPRESSIONS_H) $(GO_GOGO_H) \
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_STATEMENTS_H)
$(GO_RUNTIME_H) go/gofrontend/backend.h $(GO_STATEMENTS_H) \
go/gofrontend/ast-dump.h
go/types.o: go/gofrontend/types.cc $(GO_SYSTEM_H) $(TOPLEV_H) intl.h $(TREE_H) \
$(GIMPLE_H) $(REAL_H) convert.h $(GO_C_H) $(GO_GOGO_H) \
go/gofrontend/operator.h $(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) \
......
// ast-dump.h -- 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.
#ifndef GO_AST_DUMP_H
#define GO_AST_DUMP_H
class Expression;
class Expression_list;
class Named_object;
class Statement;
class Gogo;
// This class implements fgo-dump-ast. the
// Abstract syntax tree dump of the Go program.
class Ast_dump_context
{
public:
Ast_dump_context();
// Initialize the dump context.
void
dump(Gogo*, const char* basename);
// Dump spaces to dumpfile as indentation.
void
print_indent();
// Increase current indentation for print_indent().
void
indent()
{ ++this->indent_;}
// Decrease current indentation for print_indent().
void
unindent()
{ --this->indent_;}
// Get dump output stream.
std::ostream&
ostream()
{ return *this->ostream_;}
// Dump a Block to dump file.
void
dump_block(Block*);
// Dump a type to dump file.
void
dump_type(const Type*);
// Dump an expression to dump file.
void
dump_expression(const Expression*);
// Dump an expression list to dump file.
void
dump_expression_list(const Expression_list*);
// Dump a typed identifier to dump file.
void
dump_typed_identifier(const Typed_identifier*);
// Dump a typed identifier list to dump file.
void
dump_typed_identifier_list(const Typed_identifier_list*);
// Dump temporary variable name to dump file.
void
dump_temp_variable_name(const Statement*);
// Dump unamed lable name to dump file.
void
dump_label_name(const Unnamed_label*);
// Dump operator symbol to dump file.
void
dump_operator(Operator);
private:
// Current indent level.
int indent_;
// Indentation offset.
static const int offset_;
// Stream on output dump file.
std::ostream* ostream_;
Gogo* gogo_;
};
#endif // GO_AST_DUMP_H
......@@ -42,6 +42,7 @@ class Export;
class Import;
class Temporary_statement;
class Label;
class Ast_dump_context;
// The base class for all expressions.
......@@ -635,6 +636,10 @@ class Expression
static tree
check_bounds(tree val, tree bound_type, tree sofar, source_location);
// Dump an expression to a dump constext.
void
dump_expression(Ast_dump_context*) const;
protected:
// May be implemented by child class: traverse the expressions.
virtual int
......@@ -731,6 +736,10 @@ class Expression
void
report_error(const char*);
// Child class implements dumping to a dump context.
virtual void
do_dump_expression(Ast_dump_context*) const = 0;
private:
// Convert to the desired statement classification, or return NULL.
// This is a controlled dynamic cast.
......@@ -934,6 +943,9 @@ class Var_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The variable we are referencing.
Named_object* variable_;
......@@ -978,6 +990,9 @@ class Temporary_reference_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The statement where the temporary variable is defined.
Temporary_statement* statement_;
......@@ -1031,6 +1046,9 @@ class String_expression : public Expression
void
do_export(Export*) const;
void
do_dump_expression(Ast_dump_context*) const;
private:
// The string value. This is immutable.
const std::string val_;
......@@ -1154,6 +1172,9 @@ class Binary_expression : public Expression
void
do_export(Export*) const;
void
do_dump_expression(Ast_dump_context*) const;
private:
// The binary operator to apply.
Operator op_;
......@@ -1290,6 +1311,9 @@ class Call_expression : public Expression
bool
determining_types();
void
do_dump_expression(Ast_dump_context*) const;
private:
bool
check_argument_type(int, const Type*, const Type*, source_location, bool);
......@@ -1384,6 +1408,9 @@ class Func_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The function itself.
Named_object* function_;
......@@ -1432,6 +1459,9 @@ class Unknown_expression : public Parser_expression
do_copy()
{ return new Unknown_expression(this->named_object_, this->location()); }
void
do_dump_expression(Ast_dump_context*) const;
private:
// The unknown name.
Named_object* named_object_;
......@@ -1456,6 +1486,12 @@ class Index_expression : public Parser_expression
set_is_lvalue()
{ this->is_lvalue_ = true; }
// Dump an index expression, i.e. an expression of the form
// expr[expr] or expr[expr:expr], to a dump context.
static void
dump_index_expression(Ast_dump_context*, const Expression* expr,
const Expression* start, const Expression* end);
protected:
int
do_traverse(Traverse*);
......@@ -1473,6 +1509,9 @@ class Index_expression : public Parser_expression
this->location());
}
void
do_dump_expression(Ast_dump_context*) const;
private:
// The expression being indexed.
Expression* left_;
......@@ -1572,6 +1611,9 @@ class Map_index_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The map we are looking into.
Expression* map_;
......@@ -1641,6 +1683,9 @@ class Bound_method_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The object used to find the method. This is passed to the method
// as the first argument.
......@@ -1712,6 +1757,9 @@ class Field_reference_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The expression we are looking into. This should have a type of
// struct.
......@@ -1777,6 +1825,9 @@ class Interface_field_reference_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The expression for the interface object. This should have a type
// of interface or pointer to interface.
......@@ -1830,6 +1881,9 @@ class Type_guard_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The expression to convert.
Expression* expr_;
......@@ -1889,6 +1943,9 @@ class Receive_expression : public Expression
tree
do_get_tree(Translate_context*);
void
do_dump_expression(Ast_dump_context*) const;
private:
// The channel from which we are receiving.
Expression* channel_;
......
......@@ -133,6 +133,9 @@ go_parse_input_files(const char** filenames, unsigned int filename_count,
// Convert complicated go and defer statements into simpler ones.
::gogo->simplify_thunk_statements();
// Dump ast, use filename[0] as the base name
::gogo->dump_ast(filenames[0]);
}
// Write out globals.
......
......@@ -422,6 +422,10 @@ class Gogo
void
simplify_thunk_statements();
// Dump AST if -fgo-dump-ast is set
void
dump_ast(const char* basename);
// Convert named types to the backend representation.
void
convert_named_types();
......@@ -512,7 +516,6 @@ class Gogo
receive_as_64bit_integer(tree type, tree channel, bool blocking,
bool for_select);
// Make a trampoline which calls FNADDR passing CLOSURE.
tree
make_trampoline(tree fnaddr, tree closure, source_location);
......
......@@ -43,6 +43,7 @@ class Typed_identifier_list;
class Bexpression;
class Bstatement;
class Bvariable;
class Ast_dump_context;
// This class is used to traverse assignments made by a statement
// which makes assignments.
......@@ -374,6 +375,10 @@ class Statement
Bstatement*
get_backend(Translate_context*);
// Dump AST representation of a statement to a dump context.
void
dump_statement(Ast_dump_context*) const;
protected:
// Implemented by child class: traverse the tree.
virtual int
......@@ -414,6 +419,10 @@ class Statement
virtual Bstatement*
do_get_backend(Translate_context*) = 0;
// Implemented by child class: dump ast representation.
virtual void
do_dump_statement(Ast_dump_context*) const = 0;
// Traverse an expression in a statement.
int
traverse_expression(Traverse*, Expression**);
......@@ -507,6 +516,9 @@ class Temporary_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The type of the temporary variable.
Type* type_;
......@@ -544,6 +556,9 @@ class Variable_declaration_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Named_object* var_;
};
......@@ -581,6 +596,9 @@ class Return_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// Return values. This may be NULL.
Expression_list* vals_;
......@@ -617,6 +635,9 @@ class Send_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The channel on which to send the value.
Expression* channel_;
......@@ -678,6 +699,10 @@ class Select_clauses
Bstatement*
get_backend(Translate_context*, Unnamed_label* break_label, source_location);
// Dump AST representation.
void
dump_clauses(Ast_dump_context*) const;
private:
// A single clause.
class Select_clause
......@@ -748,6 +773,10 @@ class Select_clauses
Bstatement*
get_statements_backend(Translate_context*);
// Dump AST representation.
void
dump_clause(Ast_dump_context*) const;
private:
// The channel.
Expression* channel_;
......@@ -825,6 +854,9 @@ class Select_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The select clauses.
Select_clauses* clauses_;
......@@ -844,7 +876,7 @@ class Thunk_statement : public Statement
// Return the call expression.
Expression*
call()
call() const
{ return this->call_; }
// Simplify a go or defer statement so that it only uses a single
......@@ -914,6 +946,9 @@ class Go_statement : public Thunk_statement
protected:
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
};
// A defer statement.
......@@ -928,6 +963,9 @@ class Defer_statement : public Thunk_statement
protected:
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
};
// A label statement.
......@@ -952,6 +990,9 @@ class Label_statement : public Statement
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The label.
Label* label_;
......@@ -1005,6 +1046,9 @@ class For_statement : public Statement
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The initialization statements. This may be NULL.
Block* init_;
......@@ -1063,6 +1107,9 @@ class For_range_statement : public Statement
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression*
make_range_ref(Named_object*, Temporary_statement*, source_location);
......@@ -1167,6 +1214,10 @@ class Case_clauses
std::vector<std::vector<Bexpression*> >* all_cases,
std::vector<Bstatement*>* all_statements) const;
// Dump the AST representation to a dump context.
void
dump_clauses(Ast_dump_context*) const;
private:
// For a constant switch we need to keep a record of constants we
// have already seen.
......@@ -1237,6 +1288,10 @@ class Case_clauses
get_backend(Translate_context*, Unnamed_label* break_label,
Case_constants*, std::vector<Bexpression*>* cases) const;
// Dump the AST representation to a dump context.
void
dump_clause(Ast_dump_context*) const;
private:
// The list of case expressions.
Expression_list* cases_;
......@@ -1292,6 +1347,9 @@ class Switch_statement : public Statement
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The value to switch on. This may be NULL.
Expression* val_;
......@@ -1342,6 +1400,10 @@ class Type_case_clauses
lower(Block*, Temporary_statement* descriptor_temp,
Unnamed_label* break_label) const;
// Dump the AST representation to a dump context.
void
dump_clauses(Ast_dump_context*) const;
private:
// One type case clause.
class Type_case_clause
......@@ -1382,6 +1444,10 @@ class Type_case_clauses
lower(Block*, Temporary_statement* descriptor_temp,
Unnamed_label* break_label, Unnamed_label** stmts_label) const;
// Dump the AST representation to a dump context.
void
dump_clause(Ast_dump_context*) const;
private:
// The type for this type clause.
Type* type_;
......@@ -1438,6 +1504,9 @@ class Type_switch_statement : public Statement
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The variable holding the value we are switching on.
Named_object* var_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment