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.
15 // The unique prefix to use for exported symbols. This is set during
18 static std::string unique_prefix
;
20 // The data structures we build to represent the file.
23 // Create the main IR data structure.
27 go_create_gogo(int int_type_size
, int pointer_size
)
29 gcc_assert(::gogo
== NULL
);
30 ::gogo
= new Gogo(int_type_size
, pointer_size
);
31 if (!unique_prefix
.empty())
32 ::gogo
->set_unique_prefix(unique_prefix
);
35 // Set the unique prefix we use for exported symbols.
39 go_set_prefix(const char* arg
)
42 for (size_t i
= 0; i
< unique_prefix
.length(); ++i
)
44 char c
= unique_prefix
[i
];
45 if ((c
>= 'a' && c
<= 'z')
46 || (c
>= 'A' && c
<= 'Z')
47 || (c
>= '0' && c
<= '9')
51 unique_prefix
[i
] = '_';
55 // Parse the input files.
59 go_parse_input_files(const char** filenames
, unsigned int filename_count
,
60 bool only_check_syntax
, bool require_return_statement
)
62 gcc_assert(filename_count
> 0);
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 fatal_error("cannot open %s: %m", filename
);
79 Lex
lexer(filename
, file
);
81 Parse
parse(&lexer
, ::gogo
);
84 if (strcmp(filename
, "-") != 0)
88 ::gogo
->clear_file_scope();
90 // If the global predeclared names are referenced but not defined,
92 ::gogo
->define_global_names();
94 // Finalize method lists and build stub methods for named types.
95 ::gogo
->finalize_methods();
97 // Now that we have seen all the names, lower the parse tree into a
98 // form which is easier to use.
99 ::gogo
->lower_parse_tree();
101 // Now that we have seen all the names, verify that types are
103 ::gogo
->verify_types();
105 // Work out types of unspecified constants and variables.
106 ::gogo
->determine_types();
108 // Check types and issue errors as appropriate.
109 ::gogo
->check_types();
111 if (only_check_syntax
)
114 // Check that functions have return statements.
115 if (require_return_statement
)
116 ::gogo
->check_return_statements();
118 // Export global identifiers as appropriate.
119 ::gogo
->do_exports();
121 // Turn short-cut operators (&&, ||) into explicit if statements.
122 ::gogo
->remove_shortcuts();
124 // Use temporary variables to force order of evaluation.
125 ::gogo
->order_evaluations();
127 // Build thunks for functions which call recover.
128 ::gogo
->build_recover_thunks();
130 // Convert complicated go and defer statements into simpler ones.
131 ::gogo
->simplify_thunk_statements();
134 // Write out globals.
140 return ::gogo
->write_globals();
143 // Return the global IR structure. This is used by some of the
144 // langhooks to pass to other code.