/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1990, 1991, 1992, 1995 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/* This file contains code for reading and writing module information: */

#ifndef ERROR_EXPORTS_H
#include "error_exports.h"
#endif

#ifndef FILE_EXPORTS_H
#include "file_exports.h"
#endif

#ifndef FLAGS_DEFS_H
#include "flags_defs.h"
#endif

#ifndef GEN_DEFS_H
#include "gen_defs.h"
#endif

#ifndef HEAP_EXPORTS_H
#include "heap_exports.h"
#endif

#ifndef LIBC_EXPORTS_H
#include "libc_exports.h"
#endif

#ifndef MODULE_DEFS_H
#include "module_defs.h"
#endif

#ifndef MSG_EXPORTS_H
#include "msg_exports.h"
#endif

#ifndef OBJECT_EXPORTS_H
#include "object_exports.h"
#endif

#ifndef ROUTINE_DEFS_H
#include "routine_defs.h"
#endif

#ifndef STR_EXPORTS_H
#include "str_exports.h"
#endif

#ifndef TABLE_EXPORTS_H
#include "table_exports.h"
#endif

#ifndef TYPE_DEFS_H
#include "type_defs.h"
#endif

#ifndef UNIX_ASSERT_H
#include "unix_assert.h"
#endif

#ifndef VECTOR_DEFS_H
#include "vector_defs.h"
#endif

/*
 * module_imports_read(module, gen)
 *	This routine will read in the interface file for each import statement
 *	in "module".
 */
void
module_imports_read(
	Module		module,
	Gen		gen)
{
	Heap		heap;
	Import		import;
	Str		import_dir;
	Vec(Str)	import_dirs;
	Type_defs	import_type_defs;
	Vec(Import)	imports;
	Stdio		in_file;
	Str		interface_file_name;
	int		position;
	Type_defs	type_defs;
	Type_tables	type_tables;

	heap = gen->heap;
	type_tables = gen->type_tables;
	imports = module->imports;
	import_dirs = gen->flags->import_dirs;
	import_type_defs = module->import_type_defs;
	VEC_LOOP(Import, imports, import) {
		in_file = (Stdio)0;
		VEC_LOOP(Str, import_dirs, import_dir) {
			interface_file_name = strprintf(heap, "%s/%s.sti",
							import_dir,
							import->name);
			in_file = fopen(interface_file_name, "r");
			if (in_file != (Stdio)0) {
				break;
			}
		}
		if (in_file == (Stdio)0) {
			msg_out(gen->msg, import->position,
				"Could not import %s\n", import->name);
		} else {
			position = import->position;
			routine_entrys_import(gen->routine_table,
					      in_file, position);
			object_table_read(gen->object_table, in_file);
			type_defs = type_defs_read(in_file, heap,
						   type_tables, position);
			vec_vec_append(Type_def, import_type_defs, type_defs);
			(void)fclose(in_file);
		}
	}
}

/*
 * module_write(module, gen, out_file_name)
 *	This routine will write "module" to "out_file_name" using "gen".
 */
void
module_write(
	Module		module,
	Gen		gen,
	Str		out_file_name)
{
	Heap		heap;
	Stdio		module_file;
	Str		module_file_name;
	Type_tables	type_tables;

	/* Open the module interface file: */
	heap = gen->heap;
	if (out_file_name == (Str)0) {
		module_file_name =
		    (Str)heap_alloc(heap, strlen(module->name) + 15);
		(void)sprintf(module_file_name,
			      "/tmp/%s.sti", module->name);
	} else {
		module_file_name = out_file_name;
	}
	module_file = fopen(module_file_name, "w");
	if (module_file == (Stdio)0) {
		error_fatal("Could not open %s for writing\n",
			    module_file_name);
	}

	/* Write out the information: */
	routine_entrys_export(gen->routine_table, module_file);
	type_tables = gen->type_tables;
	object_table_write(gen->object_table, module_file, type_tables);
	type_defs_write(module->type_defs, module_file, type_tables);

	/* Close the interface file: */
	(void)fclose(module_file);

}


