blob: 4ab0f8ee7cf8b5fecb5324cdc102c04c91f2f828 [file] [log] [blame]
// -- Go frontend main file for gcc.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include "go-c.h"
#include "lex.h"
#include "parse.h"
#include "backend.h"
#include "gogo.h"
// The unique prefix to use for exported symbols. This is set during
// option processing.
static std::string unique_prefix;
// The data structures we build to represent the file.
static Gogo* gogo;
// Create the main IR data structure.
go_create_gogo(int int_type_size, int pointer_size)
go_assert(::gogo == NULL);
::gogo = new Gogo(go_get_backend(), int_type_size, pointer_size);
if (!unique_prefix.empty())
// FIXME: This should be in the gcc dependent code.
// Set the unique prefix we use for exported symbols.
go_set_prefix(const char* arg)
unique_prefix = arg;
for (size_t i = 0; i < unique_prefix.length(); ++i)
char c = unique_prefix[i];
if ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_')
unique_prefix[i] = '_';
// Parse the input files.
go_parse_input_files(const char** filenames, unsigned int filename_count,
bool only_check_syntax, bool require_return_statement)
go_assert(filename_count > 0);
for (unsigned int i = 0; i < filename_count; ++i)
if (i > 0)
const char* filename = filenames[i];
FILE* file;
if (strcmp(filename, "-") == 0)
file = stdin;
file = fopen(filename, "r");
if (file == NULL)
fatal_error("cannot open %s: %m", filename);
Lex lexer(filename, file);
Parse parse(&lexer, ::gogo);
if (strcmp(filename, "-") != 0)
// If the global predeclared names are referenced but not defined,
// define them now.
// Finalize method lists and build stub methods for named types.
// Now that we have seen all the names, lower the parse tree into a
// form which is easier to use.
// Now that we have seen all the names, verify that types are
// correct.
// Work out types of unspecified constants and variables.
// Check types and issue errors as appropriate.
if (only_check_syntax)
// Check that functions have return statements.
if (require_return_statement)
// Export global identifiers as appropriate.
// Turn short-cut operators (&&, ||) into explicit if statements.
// Use temporary variables to force order of evaluation.
// Build thunks for functions which call recover.
// Convert complicated go and defer statements into simpler ones.
// Dump ast, use filename[0] as the base name
// Write out globals.
return ::gogo->write_globals();
// Return the global IR structure. This is used by some of the
// langhooks to pass to other code.
return ::gogo;