blob: e026d6592ba0955fee6ec097d627a3467449a858 [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 "go-diagnostics.h"
#include "lex.h"
#include "parse.h"
#include "backend.h"
#include "gogo.h"
// The data structures we build to represent the file.
static Gogo* gogo;
// Create the main IR data structure.
go_create_gogo(const struct go_create_gogo_args* args)
go_assert(::gogo == NULL);
::gogo = new Gogo(args->backend, args->linemap, args->int_type_size,
if (args->pkgpath != NULL)
else if (args->prefix != NULL)
if (args->relative_import_path != NULL)
if (args->compiling_runtime)
if (args->c_header != NULL)
if (args->debug_escape_hash != NULL)
if (args->debug_optimization)
if (args->need_eqtype)
// Parse the input files.
go_parse_input_files(const char** filenames, unsigned int filename_count,
bool only_check_syntax, bool)
go_assert(filename_count > 0);
Lex::Linknames all_linknames;
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)
"cannot open %s: %m", filename);
Lex lexer(filename, file, ::gogo->linemap());
Parse parse(&lexer, ::gogo);
if (strcmp(filename, "-") != 0)
Lex::Linknames* linknames = lexer.get_and_clear_linknames();
if (linknames != NULL)
if (!::gogo->current_file_imported_unsafe())
for (Lex::Linknames::const_iterator p = linknames->begin();
p != linknames->end();
("%<//go:linkname%> only allowed in Go files that "
"import \"unsafe\""));
all_linknames.insert(linknames->begin(), linknames->end());
// If the global predeclared names are referenced but not defined,
// define them now.
// Apply any go:linkname directives.
for (Lex::Linknames::const_iterator p = all_linknames.begin();
p != all_linknames.end();
::gogo->add_linkname(p->first, p->second.is_exported, p->second.ext_name,
// Finalize method lists and build stub methods for named types.
// Check that functions have a terminating statement.
// Now that we have seen all the names, lower the parse tree into a
// form which is easier to use.
// At this point we have handled all inline functions, so we no
// longer need the linemap.
// Create function descriptors as needed.
// 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)
// Do simple deadcode elimination.
// Make implicit type conversions explicit.
// Analyze the program flow for escape information.
// Export global identifiers as appropriate.
// Use temporary variables to force order of evaluation.
// Turn short-cut operators (&&, ||) into explicit if statements.
// Convert named types to backend representation.
// Build thunks for functions which call recover.
// Convert complicated go and defer statements into simpler ones.
// Write out queued up functions for hash and comparison of types.
// Add write barriers.
// Flatten the parse tree.
// Reclaim memory of escape analysis Nodes.
// 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;