e026d6592ba0955fee6ec097d627a3467449a858
1 // go.cc -- Go frontend main file for gcc.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-diagnostics.h"
17 // The data structures we build to represent the file.
20 // Create the main IR data structure.
24 go_create_gogo(const struct go_create_gogo_args
* args
)
26 go_assert(::gogo
== NULL
);
27 ::gogo
= new Gogo(args
->backend
, args
->linemap
, args
->int_type_size
,
30 if (args
->pkgpath
!= NULL
)
31 ::gogo
->set_pkgpath(args
->pkgpath
);
32 else if (args
->prefix
!= NULL
)
33 ::gogo
->set_prefix(args
->prefix
);
35 if (args
->relative_import_path
!= NULL
)
36 ::gogo
->set_relative_import_path(args
->relative_import_path
);
37 ::gogo
->set_check_divide_by_zero(args
->check_divide_by_zero
);
38 ::gogo
->set_check_divide_overflow(args
->check_divide_overflow
);
39 if (args
->compiling_runtime
)
40 ::gogo
->set_compiling_runtime(args
->compiling_runtime
);
41 if (args
->c_header
!= NULL
)
42 ::gogo
->set_c_header(args
->c_header
);
43 ::gogo
->set_debug_escape_level(args
->debug_escape_level
);
44 if (args
->debug_escape_hash
!= NULL
)
45 ::gogo
->set_debug_escape_hash(args
->debug_escape_hash
);
46 ::gogo
->set_nil_check_size_threshold(args
->nil_check_size_threshold
);
47 if (args
->debug_optimization
)
48 ::gogo
->set_debug_optimization(args
->debug_optimization
);
49 if (args
->need_eqtype
)
50 ::gogo
->set_need_eqtype(args
->need_eqtype
);
53 // Parse the input files.
57 go_parse_input_files(const char** filenames
, unsigned int filename_count
,
58 bool only_check_syntax
, bool)
60 go_assert(filename_count
> 0);
62 Lex::Linknames all_linknames
;
63 for (unsigned int i
= 0; i
< filename_count
; ++i
)
66 ::gogo
->clear_file_scope();
68 const char* filename
= filenames
[i
];
70 if (strcmp(filename
, "-") == 0)
74 file
= fopen(filename
, "r");
76 go_fatal_error(Linemap::unknown_location(),
77 "cannot open %s: %m", filename
);
80 Lex
lexer(filename
, file
, ::gogo
->linemap());
82 Parse
parse(&lexer
, ::gogo
);
85 if (strcmp(filename
, "-") != 0)
88 Lex::Linknames
* linknames
= lexer
.get_and_clear_linknames();
89 if (linknames
!= NULL
)
91 if (!::gogo
->current_file_imported_unsafe())
93 for (Lex::Linknames::const_iterator p
= linknames
->begin();
94 p
!= linknames
->end();
96 go_error_at(p
->second
.loc
,
97 ("%<//go:linkname%> only allowed in Go files that "
98 "import \"unsafe\""));
100 all_linknames
.insert(linknames
->begin(), linknames
->end());
104 ::gogo
->clear_file_scope();
106 // If the global predeclared names are referenced but not defined,
108 ::gogo
->define_global_names();
110 // Apply any go:linkname directives.
111 for (Lex::Linknames::const_iterator p
= all_linknames
.begin();
112 p
!= all_linknames
.end();
114 ::gogo
->add_linkname(p
->first
, p
->second
.is_exported
, p
->second
.ext_name
,
117 // Finalize method lists and build stub methods for named types.
118 ::gogo
->finalize_methods();
120 // Check that functions have a terminating statement.
121 ::gogo
->check_return_statements();
123 // Now that we have seen all the names, lower the parse tree into a
124 // form which is easier to use.
125 ::gogo
->lower_parse_tree();
127 // At this point we have handled all inline functions, so we no
128 // longer need the linemap.
129 ::gogo
->linemap()->stop();
131 // Create function descriptors as needed.
132 ::gogo
->create_function_descriptors();
134 // Now that we have seen all the names, verify that types are
136 ::gogo
->verify_types();
138 // Work out types of unspecified constants and variables.
139 ::gogo
->determine_types();
141 // Check types and issue errors as appropriate.
142 ::gogo
->check_types();
144 if (only_check_syntax
)
147 // Do simple deadcode elimination.
148 ::gogo
->remove_deadcode();
150 // Make implicit type conversions explicit.
151 ::gogo
->add_conversions();
153 // Analyze the program flow for escape information.
154 ::gogo
->analyze_escape();
156 // Export global identifiers as appropriate.
157 ::gogo
->do_exports();
159 // Use temporary variables to force order of evaluation.
160 ::gogo
->order_evaluations();
162 // Turn short-cut operators (&&, ||) into explicit if statements.
163 ::gogo
->remove_shortcuts();
165 // Convert named types to backend representation.
166 ::gogo
->convert_named_types();
168 // Build thunks for functions which call recover.
169 ::gogo
->build_recover_thunks();
171 // Convert complicated go and defer statements into simpler ones.
172 ::gogo
->simplify_thunk_statements();
174 // Write out queued up functions for hash and comparison of types.
175 ::gogo
->write_specific_type_functions();
177 // Add write barriers.
178 ::gogo
->add_write_barriers();
180 // Flatten the parse tree.
183 // Reclaim memory of escape analysis Nodes.
184 ::gogo
->reclaim_escape_nodes();
186 // Dump ast, use filename[0] as the base name
187 ::gogo
->dump_ast(filenames
[0]);
190 // Write out globals.
196 return ::gogo
->write_globals();
199 // Return the global IR structure. This is used by some of the
200 // langhooks to pass to other code.