OSDN Git Service

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