OSDN Git Service

Clarify copyright license agreement.
[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 "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 float_type_size, int pointer_size)
28 {
29   gcc_assert(::gogo == NULL);
30   ::gogo = new Gogo(int_type_size, float_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   // Build required interface method tables.
122   ::gogo->build_interface_method_tables();
123
124   // Turn short-cut operators (&&, ||) into explicit if statements.
125   ::gogo->remove_shortcuts();
126
127   // Use temporary variables to force order of evaluation.
128   ::gogo->order_evaluations();
129
130   // Build thunks for functions which call recover.
131   ::gogo->build_recover_thunks();
132
133   // Convert complicated go and defer statements into simpler ones.
134   ::gogo->simplify_thunk_statements();
135 }
136
137 // Write out globals.
138
139 GO_EXTERN_C
140 void
141 go_write_globals()
142 {
143   return ::gogo->write_globals();
144 }
145
146 // Return the global IR structure.  This is used by some of the
147 // langhooks to pass to other code.
148
149 Gogo*
150 go_get_gogo()
151 {
152   return ::gogo;
153 }