P0409R2 - allow lambda capture [=, this]
[official-gcc.git] / gcc / go / gofrontend / go.cc
blobe3f17bccbefc8d99983eba6084579de2488f8904
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.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "go-diagnostics.h"
12 #include "lex.h"
13 #include "parse.h"
14 #include "backend.h"
15 #include "gogo.h"
17 // The data structures we build to represent the file.
18 static Gogo* gogo;
20 // Create the main IR data structure.
22 GO_EXTERN_C
23 void
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,
28 args->pointer_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);
46 // Parse the input files.
48 GO_EXTERN_C
49 void
50 go_parse_input_files(const char** filenames, unsigned int filename_count,
51 bool only_check_syntax, bool)
53 go_assert(filename_count > 0);
55 Lex::Linknames all_linknames;
56 for (unsigned int i = 0; i < filename_count; ++i)
58 if (i > 0)
59 ::gogo->clear_file_scope();
61 const char* filename = filenames[i];
62 FILE* file;
63 if (strcmp(filename, "-") == 0)
64 file = stdin;
65 else
67 file = fopen(filename, "r");
68 if (file == NULL)
69 go_fatal_error(Linemap::unknown_location(),
70 "cannot open %s: %m", filename);
73 Lex lexer(filename, file, ::gogo->linemap());
75 Parse parse(&lexer, ::gogo);
76 parse.program();
78 if (strcmp(filename, "-") != 0)
79 fclose(file);
81 Lex::Linknames* linknames = lexer.get_and_clear_linknames();
82 if (linknames != NULL)
84 if (!::gogo->current_file_imported_unsafe())
86 for (Lex::Linknames::const_iterator p = linknames->begin();
87 p != linknames->end();
88 ++p)
89 go_error_at(p->second.loc,
90 ("//go:linkname only allowed in Go files that "
91 "import \"unsafe\""));
93 all_linknames.insert(linknames->begin(), linknames->end());
97 ::gogo->linemap()->stop();
99 ::gogo->clear_file_scope();
101 // If the global predeclared names are referenced but not defined,
102 // define them now.
103 ::gogo->define_global_names();
105 // Apply any go:linkname directives.
106 for (Lex::Linknames::const_iterator p = all_linknames.begin();
107 p != all_linknames.end();
108 ++p)
109 ::gogo->add_linkname(p->first, p->second.is_exported, p->second.ext_name,
110 p->second.loc);
112 // Finalize method lists and build stub methods for named types.
113 ::gogo->finalize_methods();
115 // Check that functions have a terminating statement.
116 ::gogo->check_return_statements();
118 // Now that we have seen all the names, lower the parse tree into a
119 // form which is easier to use.
120 ::gogo->lower_parse_tree();
122 // Create function descriptors as needed.
123 ::gogo->create_function_descriptors();
125 // Now that we have seen all the names, verify that types are
126 // correct.
127 ::gogo->verify_types();
129 // Work out types of unspecified constants and variables.
130 ::gogo->determine_types();
132 // Check types and issue errors as appropriate.
133 ::gogo->check_types();
135 if (only_check_syntax)
136 return;
138 ::gogo->analyze_escape();
140 // Export global identifiers as appropriate.
141 ::gogo->do_exports();
143 // Turn short-cut operators (&&, ||) into explicit if statements.
144 ::gogo->remove_shortcuts();
146 // Use temporary variables to force order of evaluation.
147 ::gogo->order_evaluations();
149 // Convert named types to backend representation.
150 ::gogo->convert_named_types();
152 // Build thunks for functions which call recover.
153 ::gogo->build_recover_thunks();
155 // Convert complicated go and defer statements into simpler ones.
156 ::gogo->simplify_thunk_statements();
158 // Write out queued up functions for hash and comparison of types.
159 ::gogo->write_specific_type_functions();
161 // Add write barriers.
162 ::gogo->add_write_barriers();
164 // Flatten the parse tree.
165 ::gogo->flatten();
167 // Dump ast, use filename[0] as the base name
168 ::gogo->dump_ast(filenames[0]);
171 // Write out globals.
173 GO_EXTERN_C
174 void
175 go_write_globals()
177 return ::gogo->write_globals();
180 // Return the global IR structure. This is used by some of the
181 // langhooks to pass to other code.
183 Gogo*
184 go_get_gogo()
186 return ::gogo;