Rewrite conversion of named types to backend representation.
[gcc.git] / gcc / go / gofrontend / go.cc
1 // go.cc -- Go frontend main file for gcc.
2
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.
6
7 #include "go-system.h"
8
9 #include "go-c.h"
10
11 #include "lex.h"
12 #include "parse.h"
13 #include "gogo.h"
14
15 // The unique prefix to use for exported symbols. This is set during
16 // option processing.
17
18 static std::string unique_prefix;
19
20 // The data structures we build to represent the file.
21 static Gogo* gogo;
22
23 // Create the main IR data structure.
24
25 GO_EXTERN_C
26 void
27 go_create_gogo(int int_type_size, int pointer_size)
28 {
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);
33 }
34
35 // Set the unique prefix we use for exported symbols.
36
37 GO_EXTERN_C
38 void
39 go_set_prefix(const char* arg)
40 {
41 unique_prefix = arg;
42 for (size_t i = 0; i < unique_prefix.length(); ++i)
43 {
44 char c = unique_prefix[i];
45 if ((c >= 'a' && c <= 'z')
46 || (c >= 'A' && c <= 'Z')
47 || (c >= '0' && c <= '9')
48 || c == '_')
49 ;
50 else
51 unique_prefix[i] = '_';
52 }
53 }
54
55 // Parse the input files.
56
57 GO_EXTERN_C
58 void
59 go_parse_input_files(const char** filenames, unsigned int filename_count,
60 bool only_check_syntax, bool require_return_statement)
61 {
62 gcc_assert(filename_count > 0);
63 for (unsigned int i = 0; i < filename_count; ++i)
64 {
65 if (i > 0)
66 ::gogo->clear_file_scope();
67
68 const char* filename = filenames[i];
69 FILE* file;
70 if (strcmp(filename, "-") == 0)
71 file = stdin;
72 else
73 {
74 file = fopen(filename, "r");
75 if (file == NULL)
76 fatal_error("cannot open %s: %m", filename);
77 }
78
79 Lex lexer(filename, file);
80
81 Parse parse(&lexer, ::gogo);
82 parse.program();
83
84 if (strcmp(filename, "-") != 0)
85 fclose(file);
86 }
87
88 ::gogo->clear_file_scope();
89
90 // If the global predeclared names are referenced but not defined,
91 // define them now.
92 ::gogo->define_global_names();
93
94 // Finalize method lists and build stub methods for named types.
95 ::gogo->finalize_methods();
96
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();
100
101 // Now that we have seen all the names, verify that types are
102 // correct.
103 ::gogo->verify_types();
104
105 // Work out types of unspecified constants and variables.
106 ::gogo->determine_types();
107
108 // Check types and issue errors as appropriate.
109 ::gogo->check_types();
110
111 if (only_check_syntax)
112 return;
113
114 // Check that functions have return statements.
115 if (require_return_statement)
116 ::gogo->check_return_statements();
117
118 // Export global identifiers as appropriate.
119 ::gogo->do_exports();
120
121 // Turn short-cut operators (&&, ||) into explicit if statements.
122 ::gogo->remove_shortcuts();
123
124 // Use temporary variables to force order of evaluation.
125 ::gogo->order_evaluations();
126
127 // Build thunks for functions which call recover.
128 ::gogo->build_recover_thunks();
129
130 // Convert complicated go and defer statements into simpler ones.
131 ::gogo->simplify_thunk_statements();
132 }
133
134 // Write out globals.
135
136 GO_EXTERN_C
137 void
138 go_write_globals()
139 {
140 return ::gogo->write_globals();
141 }
142
143 // Return the global IR structure. This is used by some of the
144 // langhooks to pass to other code.
145
146 Gogo*
147 go_get_gogo()
148 {
149 return ::gogo;
150 }