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.
16 // The unique prefix to use for exported symbols. This is set during
19 static std::string unique_prefix;
21 // The data structures we build to represent the file.
24 // Create the main IR data structure.
28 go_create_gogo(int int_type_size, int pointer_size)
30 go_assert(::gogo == NULL);
31 ::gogo = new Gogo(go_get_backend(), int_type_size, pointer_size);
32 if (!unique_prefix.empty())
33 ::gogo->set_unique_prefix(unique_prefix);
36 // Set the unique prefix we use for exported symbols.
40 go_set_prefix(const char* arg)
43 for (size_t i = 0; i < unique_prefix.length(); ++i)
45 char c = unique_prefix[i];
46 if ((c >= 'a' && c <= 'z')
47 || (c >= 'A' && c <= 'Z')
48 || (c >= '0' && c <= '9')
52 unique_prefix[i] = '_';
56 // Parse the input files.
60 go_parse_input_files(const char** filenames, unsigned int filename_count,
61 bool only_check_syntax, bool require_return_statement)
63 go_assert(filename_count > 0);
64 for (unsigned int i = 0; i < filename_count; ++i)
67 ::gogo->clear_file_scope();
69 const char* filename = filenames[i];
71 if (strcmp(filename, "-") == 0)
75 file = fopen(filename, "r");
77 fatal_error("cannot open %s: %m", filename);
80 Lex lexer(filename, file);
82 Parse parse(&lexer, ::gogo);
85 if (strcmp(filename, "-") != 0)
89 ::gogo->clear_file_scope();
91 // If the global predeclared names are referenced but not defined,
93 ::gogo->define_global_names();
95 // Finalize method lists and build stub methods for named types.
96 ::gogo->finalize_methods();
98 // Now that we have seen all the names, lower the parse tree into a
99 // form which is easier to use.
100 ::gogo->lower_parse_tree();
102 // Now that we have seen all the names, verify that types are
104 ::gogo->verify_types();
106 // Work out types of unspecified constants and variables.
107 ::gogo->determine_types();
109 // Check types and issue errors as appropriate.
110 ::gogo->check_types();
112 if (only_check_syntax)
115 // Check that functions have return statements.
116 if (require_return_statement)
117 ::gogo->check_return_statements();
119 // Export global identifiers as appropriate.
120 ::gogo->do_exports();
122 // Turn short-cut operators (&&, ||) into explicit if statements.
123 ::gogo->remove_shortcuts();
125 // Use temporary variables to force order of evaluation.
126 ::gogo->order_evaluations();
128 // Build thunks for functions which call recover.
129 ::gogo->build_recover_thunks();
131 // Convert complicated go and defer statements into simpler ones.
132 ::gogo->simplify_thunk_statements();
135 // Write out globals.
141 return ::gogo->write_globals();
144 // Return the global IR structure. This is used by some of the
145 // langhooks to pass to other code.