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 #include "go-diagnostics.h"
11 
12 #include "lex.h"
13 #include "parse.h"
14 #include "backend.h"
15 #include "gogo.h"
16 
17 // The data structures we build to represent the file.
18 static Gogo* gogo;
19 
20 // Create the main IR data structure.
21 
22 GO_EXTERN_C
23 void
go_create_gogo(const struct go_create_gogo_args * args)24 go_create_gogo(const struct go_create_gogo_args* args)
25 {
26   go_assert(::gogo == NULL);
27   ::gogo = new Gogo(args->backend, args->linemap, args->int_type_size,
28 		    args->pointer_size);
29 
30   if (args->pkgpath != NULL)
31     ::gogo->set_pkgpath(args->pkgpath);
32   else if (args->prefix != NULL)
33     ::gogo->set_prefix(args->prefix);
34 
35   if (args->relative_import_path != NULL)
36     ::gogo->set_relative_import_path(args->relative_import_path);
37   ::gogo->set_check_divide_by_zero(args->check_divide_by_zero);
38   ::gogo->set_check_divide_overflow(args->check_divide_overflow);
39   if (args->compiling_runtime)
40     ::gogo->set_compiling_runtime(args->compiling_runtime);
41   if (args->c_header != NULL)
42     ::gogo->set_c_header(args->c_header);
43   ::gogo->set_debug_escape_level(args->debug_escape_level);
44   if (args->debug_escape_hash != NULL)
45     ::gogo->set_debug_escape_hash(args->debug_escape_hash);
46   ::gogo->set_nil_check_size_threshold(args->nil_check_size_threshold);
47 }
48 
49 // Parse the input files.
50 
51 GO_EXTERN_C
52 void
go_parse_input_files(const char ** filenames,unsigned int filename_count,bool only_check_syntax,bool)53 go_parse_input_files(const char** filenames, unsigned int filename_count,
54 		     bool only_check_syntax, bool)
55 {
56   go_assert(filename_count > 0);
57 
58   Lex::Linknames all_linknames;
59   for (unsigned int i = 0; i < filename_count; ++i)
60     {
61       if (i > 0)
62 	::gogo->clear_file_scope();
63 
64       const char* filename = filenames[i];
65       FILE* file;
66       if (strcmp(filename, "-") == 0)
67 	file = stdin;
68       else
69 	{
70 	  file = fopen(filename, "r");
71 	  if (file == NULL)
72 	    go_fatal_error(Linemap::unknown_location(),
73 			   "cannot open %s: %m", filename);
74 	}
75 
76       Lex lexer(filename, file, ::gogo->linemap());
77 
78       Parse parse(&lexer, ::gogo);
79       parse.program();
80 
81       if (strcmp(filename, "-") != 0)
82 	fclose(file);
83 
84       Lex::Linknames* linknames = lexer.get_and_clear_linknames();
85       if (linknames != NULL)
86 	{
87 	  if (!::gogo->current_file_imported_unsafe())
88 	    {
89 	      for (Lex::Linknames::const_iterator p = linknames->begin();
90 		   p != linknames->end();
91 		   ++p)
92 		go_error_at(p->second.loc,
93 			    ("//go:linkname only allowed in Go files that "
94 			     "import \"unsafe\""));
95 	    }
96 	  all_linknames.insert(linknames->begin(), linknames->end());
97 	}
98     }
99 
100   ::gogo->linemap()->stop();
101 
102   ::gogo->clear_file_scope();
103 
104   // If the global predeclared names are referenced but not defined,
105   // define them now.
106   ::gogo->define_global_names();
107 
108   // Apply any go:linkname directives.
109   for (Lex::Linknames::const_iterator p = all_linknames.begin();
110        p != all_linknames.end();
111        ++p)
112     ::gogo->add_linkname(p->first, p->second.is_exported, p->second.ext_name,
113 			 p->second.loc);
114 
115   // Finalize method lists and build stub methods for named types.
116   ::gogo->finalize_methods();
117 
118   // Check that functions have a terminating statement.
119   ::gogo->check_return_statements();
120 
121   // Now that we have seen all the names, lower the parse tree into a
122   // form which is easier to use.
123   ::gogo->lower_parse_tree();
124 
125   // Create function descriptors as needed.
126   ::gogo->create_function_descriptors();
127 
128   // Now that we have seen all the names, verify that types are
129   // correct.
130   ::gogo->verify_types();
131 
132   // Work out types of unspecified constants and variables.
133   ::gogo->determine_types();
134 
135   // Check types and issue errors as appropriate.
136   ::gogo->check_types();
137 
138   if (only_check_syntax)
139     return;
140 
141   ::gogo->analyze_escape();
142 
143   // Export global identifiers as appropriate.
144   ::gogo->do_exports();
145 
146   // Turn short-cut operators (&&, ||) into explicit if statements.
147   ::gogo->remove_shortcuts();
148 
149   // Use temporary variables to force order of evaluation.
150   ::gogo->order_evaluations();
151 
152   // Convert named types to backend representation.
153   ::gogo->convert_named_types();
154 
155   // Build thunks for functions which call recover.
156   ::gogo->build_recover_thunks();
157 
158   // Convert complicated go and defer statements into simpler ones.
159   ::gogo->simplify_thunk_statements();
160 
161   // Write out queued up functions for hash and comparison of types.
162   ::gogo->write_specific_type_functions();
163 
164   // Add write barriers.
165   ::gogo->add_write_barriers();
166 
167   // Flatten the parse tree.
168   ::gogo->flatten();
169 
170   // Reclaim memory of escape analysis Nodes.
171   ::gogo->reclaim_escape_nodes();
172 
173   // Dump ast, use filename[0] as the base name
174   ::gogo->dump_ast(filenames[0]);
175 }
176 
177 // Write out globals.
178 
179 GO_EXTERN_C
180 void
go_write_globals()181 go_write_globals()
182 {
183   return ::gogo->write_globals();
184 }
185 
186 // Return the global IR structure.  This is used by some of the
187 // langhooks to pass to other code.
188 
189 Gogo*
go_get_gogo()190 go_get_gogo()
191 {
192   return ::gogo;
193 }
194