compiler: introduce size threshold for nil checks
[official-gcc.git] / gcc / go / gofrontend / go.cc
bloba2b8cecb3fe9eab2b54c99ea74249c0d5098dbe3
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);
44 ::gogo->set_nil_check_size_threshold(args->nil_check_size_threshold);
47 // Parse the input files.
49 GO_EXTERN_C
50 void
51 go_parse_input_files(const char** filenames, unsigned int filename_count,
52 bool only_check_syntax, bool)
54 go_assert(filename_count > 0);
56 Lex::Linknames all_linknames;
57 for (unsigned int i = 0; i < filename_count; ++i)
59 if (i > 0)
60 ::gogo->clear_file_scope();
62 const char* filename = filenames[i];
63 FILE* file;
64 if (strcmp(filename, "-") == 0)
65 file = stdin;
66 else
68 file = fopen(filename, "r");
69 if (file == NULL)
70 go_fatal_error(Linemap::unknown_location(),
71 "cannot open %s: %m", filename);
74 Lex lexer(filename, file, ::gogo->linemap());
76 Parse parse(&lexer, ::gogo);
77 parse.program();
79 if (strcmp(filename, "-") != 0)
80 fclose(file);
82 Lex::Linknames* linknames = lexer.get_and_clear_linknames();
83 if (linknames != NULL)
85 if (!::gogo->current_file_imported_unsafe())
87 for (Lex::Linknames::const_iterator p = linknames->begin();
88 p != linknames->end();
89 ++p)
90 go_error_at(p->second.loc,
91 ("//go:linkname only allowed in Go files that "
92 "import \"unsafe\""));
94 all_linknames.insert(linknames->begin(), linknames->end());
98 ::gogo->linemap()->stop();
100 ::gogo->clear_file_scope();
102 // If the global predeclared names are referenced but not defined,
103 // define them now.
104 ::gogo->define_global_names();
106 // Apply any go:linkname directives.
107 for (Lex::Linknames::const_iterator p = all_linknames.begin();
108 p != all_linknames.end();
109 ++p)
110 ::gogo->add_linkname(p->first, p->second.is_exported, p->second.ext_name,
111 p->second.loc);
113 // Finalize method lists and build stub methods for named types.
114 ::gogo->finalize_methods();
116 // Check that functions have a terminating statement.
117 ::gogo->check_return_statements();
119 // Now that we have seen all the names, lower the parse tree into a
120 // form which is easier to use.
121 ::gogo->lower_parse_tree();
123 // Create function descriptors as needed.
124 ::gogo->create_function_descriptors();
126 // Now that we have seen all the names, verify that types are
127 // correct.
128 ::gogo->verify_types();
130 // Work out types of unspecified constants and variables.
131 ::gogo->determine_types();
133 // Check types and issue errors as appropriate.
134 ::gogo->check_types();
136 if (only_check_syntax)
137 return;
139 ::gogo->analyze_escape();
141 // Export global identifiers as appropriate.
142 ::gogo->do_exports();
144 // Turn short-cut operators (&&, ||) into explicit if statements.
145 ::gogo->remove_shortcuts();
147 // Use temporary variables to force order of evaluation.
148 ::gogo->order_evaluations();
150 // Convert named types to backend representation.
151 ::gogo->convert_named_types();
153 // Build thunks for functions which call recover.
154 ::gogo->build_recover_thunks();
156 // Convert complicated go and defer statements into simpler ones.
157 ::gogo->simplify_thunk_statements();
159 // Write out queued up functions for hash and comparison of types.
160 ::gogo->write_specific_type_functions();
162 // Add write barriers.
163 ::gogo->add_write_barriers();
165 // Flatten the parse tree.
166 ::gogo->flatten();
168 // Dump ast, use filename[0] as the base name
169 ::gogo->dump_ast(filenames[0]);
172 // Write out globals.
174 GO_EXTERN_C
175 void
176 go_write_globals()
178 return ::gogo->write_globals();
181 // Return the global IR structure. This is used by some of the
182 // langhooks to pass to other code.
184 Gogo*
185 go_get_gogo()
187 return ::gogo;