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.
16 // The data structures we build to represent the file.
19 // Create the main IR data structure.
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
);
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
);
38 // FIXME: This should be in the gcc dependent code.
39 ::gogo
->define_builtin_function_trees();
42 // Parse the input files.
46 go_parse_input_files(const char** filenames
, unsigned int filename_count
,
47 bool only_check_syntax
, bool)
49 go_assert(filename_count
> 0);
51 for (unsigned int i
= 0; i
< filename_count
; ++i
)
54 ::gogo
->clear_file_scope();
56 const char* filename
= filenames
[i
];
58 if (strcmp(filename
, "-") == 0)
62 file
= fopen(filename
, "r");
64 fatal_error("cannot open %s: %m", filename
);
67 Lex
lexer(filename
, file
, ::gogo
->linemap());
69 Parse
parse(&lexer
, ::gogo
);
72 if (strcmp(filename
, "-") != 0)
76 ::gogo
->linemap()->stop();
78 ::gogo
->clear_file_scope();
80 // If the global predeclared names are referenced but not defined,
82 ::gogo
->define_global_names();
84 // Finalize method lists and build stub methods for named types.
85 ::gogo
->finalize_methods();
87 // Check that functions have a terminating statement.
88 ::gogo
->check_return_statements();
90 // Now that we have seen all the names, lower the parse tree into a
91 // form which is easier to use.
92 ::gogo
->lower_parse_tree();
94 // Create function descriptors as needed.
95 ::gogo
->create_function_descriptors();
97 // Write out queued up functions for hash and comparison of types.
98 ::gogo
->write_specific_type_functions();
100 // Now that we have seen all the names, verify that types are
102 ::gogo
->verify_types();
104 // Work out types of unspecified constants and variables.
105 ::gogo
->determine_types();
107 // Check types and issue errors as appropriate.
108 ::gogo
->check_types();
110 if (only_check_syntax
)
113 // Export global identifiers as appropriate.
114 ::gogo
->do_exports();
116 // Turn short-cut operators (&&, ||) into explicit if statements.
117 ::gogo
->remove_shortcuts();
119 // Use temporary variables to force order of evaluation.
120 ::gogo
->order_evaluations();
122 // Build thunks for functions which call recover.
123 ::gogo
->build_recover_thunks();
125 // Convert complicated go and defer statements into simpler ones.
126 ::gogo
->simplify_thunk_statements();
128 // Dump ast, use filename[0] as the base name
129 ::gogo
->dump_ast(filenames
[0]);
132 // Write out globals.
138 return ::gogo
->write_globals();
141 // Return the global IR structure. This is used by some of the
142 // langhooks to pass to other code.