* go-gcc.cc: #include "langhooks.h".
[official-gcc.git] / gcc / go / gofrontend / go.cc
blobd2331f3e0ae39b198e7a776b5913952b012de2ca
1 // go.cc -- Go frontend main file for gcc.
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.
7 #include "go-system.h"
9 #include "go-c.h"
11 #include "lex.h"
12 #include "parse.h"
13 #include "backend.h"
14 #include "gogo.h"
16 // The data structures we build to represent the file.
17 static Gogo* gogo;
19 // Create the main IR data structure.
21 GO_EXTERN_C
22 void
23 go_create_gogo(int int_type_size, int pointer_size, const char *pkgpath,
24 const char *prefix, const char *relative_import_path)
26 go_assert(::gogo == NULL);
27 Linemap* linemap = go_get_linemap();
28 ::gogo = new Gogo(go_get_backend(), linemap, int_type_size, pointer_size);
30 if (pkgpath != NULL)
31 ::gogo->set_pkgpath(pkgpath);
32 else if (prefix != NULL)
33 ::gogo->set_prefix(prefix);
35 if (relative_import_path != NULL)
36 ::gogo->set_relative_import_path(relative_import_path);
39 // Parse the input files.
41 GO_EXTERN_C
42 void
43 go_parse_input_files(const char** filenames, unsigned int filename_count,
44 bool only_check_syntax, bool)
46 go_assert(filename_count > 0);
48 for (unsigned int i = 0; i < filename_count; ++i)
50 if (i > 0)
51 ::gogo->clear_file_scope();
53 const char* filename = filenames[i];
54 FILE* file;
55 if (strcmp(filename, "-") == 0)
56 file = stdin;
57 else
59 file = fopen(filename, "r");
60 if (file == NULL)
61 fatal_error("cannot open %s: %m", filename);
64 Lex lexer(filename, file, ::gogo->linemap());
66 Parse parse(&lexer, ::gogo);
67 parse.program();
69 if (strcmp(filename, "-") != 0)
70 fclose(file);
73 ::gogo->linemap()->stop();
75 ::gogo->clear_file_scope();
77 // If the global predeclared names are referenced but not defined,
78 // define them now.
79 ::gogo->define_global_names();
81 // Finalize method lists and build stub methods for named types.
82 ::gogo->finalize_methods();
84 // Check that functions have a terminating statement.
85 ::gogo->check_return_statements();
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();
91 // Create function descriptors as needed.
92 ::gogo->create_function_descriptors();
94 // Write out queued up functions for hash and comparison of types.
95 ::gogo->write_specific_type_functions();
97 // Now that we have seen all the names, verify that types are
98 // correct.
99 ::gogo->verify_types();
101 // Work out types of unspecified constants and variables.
102 ::gogo->determine_types();
104 // Check types and issue errors as appropriate.
105 ::gogo->check_types();
107 if (only_check_syntax)
108 return;
110 // Export global identifiers as appropriate.
111 ::gogo->do_exports();
113 // Turn short-cut operators (&&, ||) into explicit if statements.
114 ::gogo->remove_shortcuts();
116 // Use temporary variables to force order of evaluation.
117 ::gogo->order_evaluations();
119 // Convert named types to backend representation.
120 ::gogo->convert_named_types();
122 // Flatten the parse tree.
123 ::gogo->flatten();
125 // Build thunks for functions which call recover.
126 ::gogo->build_recover_thunks();
128 // Convert complicated go and defer statements into simpler ones.
129 ::gogo->simplify_thunk_statements();
131 // Dump ast, use filename[0] as the base name
132 ::gogo->dump_ast(filenames[0]);
135 // Write out globals.
137 GO_EXTERN_C
138 void
139 go_write_globals()
141 return ::gogo->write_globals();
144 // Return the global IR structure. This is used by some of the
145 // langhooks to pass to other code.
147 Gogo*
148 go_get_gogo()
150 return ::gogo;