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