OSDN Git Service

1f2ce8adcde55178a3e31484fae3e600d11405a7
[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 data structures we build to represent the file.
17 static Gogo* gogo;
18
19 // Create the main IR data structure.
20
21 GO_EXTERN_C
22 void
23 go_create_gogo(int int_type_size, int pointer_size, const char *pkgpath,
24                const char *prefix)
25 {
26   go_assert(::gogo == NULL);
27   Linemap* linemap = go_get_linemap();
28   ::gogo = new Gogo(go_get_backend(), linemap, int_type_size, pointer_size);
29
30   if (pkgpath != NULL)
31     ::gogo->set_pkgpath(pkgpath);
32   else if (prefix != NULL)
33     ::gogo->set_prefix(prefix);
34
35   // FIXME: This should be in the gcc dependent code.
36   ::gogo->define_builtin_function_trees();
37 }
38
39 // Parse the input files.
40
41 GO_EXTERN_C
42 void
43 go_parse_input_files(const char** filenames, unsigned int filename_count,
44                      bool only_check_syntax, bool require_return_statement)
45 {
46   go_assert(filename_count > 0);
47
48   for (unsigned int i = 0; i < filename_count; ++i)
49     {
50       if (i > 0)
51         ::gogo->clear_file_scope();
52
53       const char* filename = filenames[i];
54       FILE* file;
55       if (strcmp(filename, "-") == 0)
56         file = stdin;
57       else
58         {
59           file = fopen(filename, "r");
60           if (file == NULL)
61             fatal_error("cannot open %s: %m", filename);
62         }
63
64       Lex lexer(filename, file, ::gogo->linemap());
65
66       Parse parse(&lexer, ::gogo);
67       parse.program();
68
69       if (strcmp(filename, "-") != 0)
70         fclose(file);
71     }
72
73   ::gogo->linemap()->stop();
74
75   ::gogo->clear_file_scope();
76
77   // If the global predeclared names are referenced but not defined,
78   // define them now.
79   ::gogo->define_global_names();
80
81   // Finalize method lists and build stub methods for named types.
82   ::gogo->finalize_methods();
83
84   // Now that we have seen all the names, lower the parse tree into a
85   // form which is easier to use.
86   ::gogo->lower_parse_tree();
87
88   // Write out queued up functions for hash and comparison of types.
89   ::gogo->write_specific_type_functions();
90
91   // Now that we have seen all the names, verify that types are
92   // correct.
93   ::gogo->verify_types();
94
95   // Work out types of unspecified constants and variables.
96   ::gogo->determine_types();
97
98   // Check types and issue errors as appropriate.
99   ::gogo->check_types();
100
101   if (only_check_syntax)
102     return;
103
104   // Check that functions have return statements.
105   if (require_return_statement)
106     ::gogo->check_return_statements();
107
108   // Export global identifiers as appropriate.
109   ::gogo->do_exports();
110
111   // Turn short-cut operators (&&, ||) into explicit if statements.
112   ::gogo->remove_shortcuts();
113
114   // Use temporary variables to force order of evaluation.
115   ::gogo->order_evaluations();
116
117   // Build thunks for functions which call recover.
118   ::gogo->build_recover_thunks();
119
120   // Convert complicated go and defer statements into simpler ones.
121   ::gogo->simplify_thunk_statements();
122   
123   // Dump ast, use filename[0] as the base name
124   ::gogo->dump_ast(filenames[0]);
125 }
126
127 // Write out globals.
128
129 GO_EXTERN_C
130 void
131 go_write_globals()
132 {
133   return ::gogo->write_globals();
134 }
135
136 // Return the global IR structure.  This is used by some of the
137 // langhooks to pass to other code.
138
139 Gogo*
140 go_get_gogo()
141 {
142   return ::gogo;
143 }