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 if (args
->check_divide_by_zero
)
38 ::gogo
->set_check_divide_by_zero(args
->check_divide_by_zero
);
39 if (args
->check_divide_overflow
)
40 ::gogo
->set_check_divide_overflow(args
->check_divide_overflow
);
41 if (args
->compiling_runtime
)
42 ::gogo
->set_compiling_runtime(args
->compiling_runtime
);
43 if (args
->c_header
!= NULL
)
44 ::gogo
->set_c_header(args
->c_header
);
45 ::gogo
->set_debug_escape_level(args
->debug_escape_level
);
48 // Parse the input files.
52 go_parse_input_files(const char** filenames
, unsigned int filename_count
,
53 bool only_check_syntax
, bool)
55 go_assert(filename_count
> 0);
57 Lex::Linknames all_linknames
;
58 for (unsigned int i
= 0; i
< filename_count
; ++i
)
61 ::gogo
->clear_file_scope();
63 const char* filename
= filenames
[i
];
65 if (strcmp(filename
, "-") == 0)
69 file
= fopen(filename
, "r");
71 go_fatal_error(Linemap::unknown_location(),
72 "cannot open %s: %m", filename
);
75 Lex
lexer(filename
, file
, ::gogo
->linemap());
77 Parse
parse(&lexer
, ::gogo
);
80 if (strcmp(filename
, "-") != 0)
83 Lex::Linknames
* linknames
= lexer
.get_and_clear_linknames();
84 if (linknames
!= NULL
)
86 if (!::gogo
->current_file_imported_unsafe())
88 for (Lex::Linknames::const_iterator p
= linknames
->begin();
89 p
!= linknames
->end();
91 go_error_at(p
->second
.loc
,
92 ("//go:linkname only allowed in Go files that "
93 "import \"unsafe\""));
95 all_linknames
.insert(linknames
->begin(), linknames
->end());
99 ::gogo
->linemap()->stop();
101 ::gogo
->clear_file_scope();
103 // If the global predeclared names are referenced but not defined,
105 ::gogo
->define_global_names();
107 // Apply any go:linkname directives.
108 for (Lex::Linknames::const_iterator p
= all_linknames
.begin();
109 p
!= all_linknames
.end();
111 ::gogo
->add_linkname(p
->first
, p
->second
.is_exported
, p
->second
.ext_name
,
114 // Finalize method lists and build stub methods for named types.
115 ::gogo
->finalize_methods();
117 // Check that functions have a terminating statement.
118 ::gogo
->check_return_statements();
120 // Now that we have seen all the names, lower the parse tree into a
121 // form which is easier to use.
122 ::gogo
->lower_parse_tree();
124 // Create function descriptors as needed.
125 ::gogo
->create_function_descriptors();
127 // Now that we have seen all the names, verify that types are
129 ::gogo
->verify_types();
131 // Work out types of unspecified constants and variables.
132 ::gogo
->determine_types();
134 // Check types and issue errors as appropriate.
135 ::gogo
->check_types();
137 if (only_check_syntax
)
140 ::gogo
->analyze_escape();
142 // Export global identifiers as appropriate.
143 ::gogo
->do_exports();
145 // Turn short-cut operators (&&, ||) into explicit if statements.
146 ::gogo
->remove_shortcuts();
148 // Use temporary variables to force order of evaluation.
149 ::gogo
->order_evaluations();
151 // Convert named types to backend representation.
152 ::gogo
->convert_named_types();
154 // Build thunks for functions which call recover.
155 ::gogo
->build_recover_thunks();
157 // Convert complicated go and defer statements into simpler ones.
158 ::gogo
->simplify_thunk_statements();
160 // Write out queued up functions for hash and comparison of types.
161 ::gogo
->write_specific_type_functions();
163 // Flatten the parse tree.
166 // Dump ast, use filename[0] as the base name
167 ::gogo
->dump_ast(filenames
[0]);
170 // Write out globals.
176 return ::gogo
->write_globals();
179 // Return the global IR structure. This is used by some of the
180 // langhooks to pass to other code.