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