1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
2002-02-08 01:27:48 -08:00
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-07-02 09:51:07 -07:00
|
|
|
(* The batch compiler *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Config
|
2000-03-06 21:02:33 -08:00
|
|
|
open Format
|
2012-05-30 07:52:37 -07:00
|
|
|
open Typedtree
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
(* Initialize the search path.
|
|
|
|
The current directory is always searched first,
|
|
|
|
then the directories specified with the -I option (in command-line order),
|
|
|
|
then the standard library directory. *)
|
|
|
|
|
|
|
|
let init_path () =
|
1997-11-18 09:14:54 -08:00
|
|
|
let dirs =
|
2003-07-17 01:38:28 -07:00
|
|
|
if !Clflags.use_threads
|
2000-11-07 06:41:12 -08:00
|
|
|
then "+threads" :: !Clflags.include_dirs
|
|
|
|
else !Clflags.include_dirs in
|
|
|
|
let exp_dirs =
|
2000-12-27 21:02:43 -08:00
|
|
|
List.map (expand_directory Config.standard_library) dirs in
|
2002-02-14 07:17:11 -08:00
|
|
|
load_path := "" :: List.rev_append exp_dirs (Clflags.std_include_dir ());
|
2005-07-31 05:03:40 -07:00
|
|
|
Env.reset_cache ()
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
(* Return the initial environment in which compilation proceeds. *)
|
|
|
|
|
|
|
|
let initial_env () =
|
2003-05-12 02:34:05 -07:00
|
|
|
Ident.reinit();
|
1995-07-02 09:51:07 -07:00
|
|
|
try
|
|
|
|
if !Clflags.nopervasives
|
|
|
|
then Env.initial
|
|
|
|
else Env.open_pers_signature "Pervasives" Env.initial
|
|
|
|
with Not_found ->
|
2008-10-06 06:53:54 -07:00
|
|
|
fatal_error "cannot open pervasives.cmi"
|
|
|
|
|
|
|
|
(* Note: this function is duplicated in compile.ml *)
|
2012-01-20 06:23:34 -08:00
|
|
|
let check_unit_name ppf filename name =
|
2008-10-06 06:53:54 -07:00
|
|
|
try
|
|
|
|
begin match name.[0] with
|
|
|
|
| 'A'..'Z' -> ()
|
|
|
|
| _ ->
|
2012-01-20 06:23:34 -08:00
|
|
|
Location.print_warning (Location.in_file filename) ppf
|
2008-10-06 06:53:54 -07:00
|
|
|
(Warnings.Bad_module_name name);
|
|
|
|
raise Exit;
|
|
|
|
end;
|
|
|
|
for i = 1 to String.length name - 1 do
|
|
|
|
match name.[i] with
|
|
|
|
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' | '\'' -> ()
|
|
|
|
| _ ->
|
2012-01-20 06:23:34 -08:00
|
|
|
Location.print_warning (Location.in_file filename) ppf
|
2008-10-06 06:53:54 -07:00
|
|
|
(Warnings.Bad_module_name name);
|
|
|
|
raise Exit;
|
|
|
|
done;
|
|
|
|
with Exit -> ()
|
|
|
|
;;
|
1995-07-02 09:51:07 -07:00
|
|
|
|
1996-04-25 09:06:43 -07:00
|
|
|
(* Compile a .mli file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let interface ppf sourcefile outputprefix =
|
2008-09-10 08:03:33 -07:00
|
|
|
Location.input_name := sourcefile;
|
2005-07-31 05:03:40 -07:00
|
|
|
init_path ();
|
2004-06-13 05:46:41 -07:00
|
|
|
let modulename =
|
|
|
|
String.capitalize(Filename.basename(chop_extension_if_any sourcefile)) in
|
2012-01-20 06:23:34 -08:00
|
|
|
check_unit_name ppf sourcefile modulename;
|
2005-07-31 05:03:40 -07:00
|
|
|
Env.set_unit_name modulename;
|
2002-02-08 02:14:31 -08:00
|
|
|
let inputfile = Pparse.preprocess sourcefile in
|
2012-05-30 07:52:37 -07:00
|
|
|
let initial_env = initial_env() in
|
2002-02-08 02:14:31 -08:00
|
|
|
try
|
|
|
|
let ast =
|
2012-01-20 06:23:34 -08:00
|
|
|
Pparse.file ppf inputfile Parse.interface ast_intf_magic_number in
|
|
|
|
if !Clflags.dump_parsetree then fprintf ppf "%a@." Printast.interface ast;
|
2012-10-17 09:09:38 -07:00
|
|
|
if !Clflags.dump_source then fprintf ppf "%a@." Pprintast.signature ast;
|
2012-05-30 07:52:37 -07:00
|
|
|
let tsg = Typemod.transl_signature initial_env ast in
|
2012-12-18 09:19:53 -08:00
|
|
|
if !Clflags.dump_typedtree then fprintf ppf "%a@." Printtyped.interface tsg;
|
2012-05-30 07:52:37 -07:00
|
|
|
let sg = tsg.sig_type in
|
2002-02-08 02:14:31 -08:00
|
|
|
if !Clflags.print_types then
|
2005-08-08 02:41:52 -07:00
|
|
|
fprintf std_formatter "%a@." Printtyp.signature
|
|
|
|
(Typemod.simplify_signature sg);
|
2012-05-30 07:52:37 -07:00
|
|
|
ignore (Includemod.signatures initial_env sg sg);
|
2012-05-25 06:27:48 -07:00
|
|
|
Typecore.force_delayed_checks ();
|
2002-02-08 02:14:31 -08:00
|
|
|
Warnings.check_fatal ();
|
2012-05-30 07:52:37 -07:00
|
|
|
if not !Clflags.print_types then begin
|
|
|
|
let sg = Env.save_signature sg modulename (outputprefix ^ ".cmi") in
|
|
|
|
Typemod.save_signature modulename tsg outputprefix sourcefile initial_env sg ;
|
|
|
|
end;
|
2008-01-21 00:42:14 -08:00
|
|
|
Pparse.remove_preprocessed inputfile;
|
2012-05-30 07:52:37 -07:00
|
|
|
Stypes.dump (Some (outputprefix ^ ".annot"))
|
2002-02-08 02:14:31 -08:00
|
|
|
with e ->
|
|
|
|
Pparse.remove_preprocessed_if_ast inputfile;
|
2012-05-30 07:52:37 -07:00
|
|
|
Stypes.dump (Some (outputprefix ^ ".annot"));
|
2002-02-08 02:14:31 -08:00
|
|
|
raise e
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
(* Compile a .ml file *)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let print_if ppf flag printer arg =
|
|
|
|
if !flag then fprintf ppf "%a@." printer arg;
|
1995-07-02 09:51:07 -07:00
|
|
|
arg
|
|
|
|
|
1998-04-27 08:16:48 -07:00
|
|
|
let (++) x f = f x
|
|
|
|
let (+++) (x, y) f = (x, f y)
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let implementation ppf sourcefile outputprefix =
|
2008-09-10 08:03:33 -07:00
|
|
|
Location.input_name := sourcefile;
|
2005-07-31 05:03:40 -07:00
|
|
|
init_path ();
|
2004-06-13 05:46:41 -07:00
|
|
|
let modulename =
|
|
|
|
String.capitalize(Filename.basename(chop_extension_if_any sourcefile)) in
|
2012-01-20 06:23:34 -08:00
|
|
|
check_unit_name ppf sourcefile modulename;
|
2005-07-31 05:03:40 -07:00
|
|
|
Env.set_unit_name modulename;
|
2002-02-08 02:14:31 -08:00
|
|
|
let inputfile = Pparse.preprocess sourcefile in
|
1998-04-27 08:16:48 -07:00
|
|
|
let env = initial_env() in
|
2005-08-01 08:51:09 -07:00
|
|
|
Compilenv.reset ?packname:!Clflags.for_package modulename;
|
2008-10-08 06:09:39 -07:00
|
|
|
let cmxfile = outputprefix ^ ".cmx" in
|
|
|
|
let objfile = outputprefix ^ ext_obj in
|
2002-02-08 02:14:31 -08:00
|
|
|
try
|
2003-06-23 06:22:09 -07:00
|
|
|
if !Clflags.print_types then ignore(
|
2012-01-20 06:23:34 -08:00
|
|
|
Pparse.file ppf inputfile Parse.implementation ast_impl_magic_number
|
|
|
|
++ print_if ppf Clflags.dump_parsetree Printast.implementation
|
2012-10-17 09:09:38 -07:00
|
|
|
++ print_if ppf Clflags.dump_source Pprintast.structure
|
2012-12-18 09:19:53 -08:00
|
|
|
++ Typemod.type_implementation sourcefile outputprefix modulename env
|
|
|
|
++ print_if ppf Clflags.dump_typedtree Printtyped.implementation_with_coercion)
|
2003-06-23 06:22:09 -07:00
|
|
|
else begin
|
2012-01-20 06:23:34 -08:00
|
|
|
Pparse.file ppf inputfile Parse.implementation ast_impl_magic_number
|
|
|
|
++ print_if ppf Clflags.dump_parsetree Printast.implementation
|
2012-10-17 09:09:38 -07:00
|
|
|
++ print_if ppf Clflags.dump_source Pprintast.structure
|
2004-06-13 05:46:41 -07:00
|
|
|
++ Typemod.type_implementation sourcefile outputprefix modulename env
|
2012-12-18 09:19:53 -08:00
|
|
|
++ print_if ppf Clflags.dump_typedtree Printtyped.implementation_with_coercion
|
2003-06-23 06:22:09 -07:00
|
|
|
++ Translmod.transl_store_implementation modulename
|
2012-01-20 06:23:34 -08:00
|
|
|
+++ print_if ppf Clflags.dump_rawlambda Printlambda.lambda
|
2003-06-23 06:22:09 -07:00
|
|
|
+++ Simplif.simplify_lambda
|
2012-01-20 06:23:34 -08:00
|
|
|
+++ print_if ppf Clflags.dump_lambda Printlambda.lambda
|
|
|
|
++ Asmgen.compile_implementation outputprefix ppf;
|
2008-10-08 06:09:39 -07:00
|
|
|
Compilenv.save_unit_info cmxfile;
|
2003-06-23 06:22:09 -07:00
|
|
|
end;
|
2002-02-08 02:14:31 -08:00
|
|
|
Warnings.check_fatal ();
|
2008-12-03 10:09:09 -08:00
|
|
|
Pparse.remove_preprocessed inputfile;
|
2012-05-30 07:52:37 -07:00
|
|
|
Stypes.dump (Some (outputprefix ^ ".annot"));
|
2002-02-08 02:14:31 -08:00
|
|
|
with x ->
|
2008-10-08 06:09:39 -07:00
|
|
|
remove_file objfile;
|
|
|
|
remove_file cmxfile;
|
2002-02-08 02:14:31 -08:00
|
|
|
Pparse.remove_preprocessed_if_ast inputfile;
|
2012-05-30 07:52:37 -07:00
|
|
|
Stypes.dump (Some (outputprefix ^ ".annot"));
|
2002-02-08 02:14:31 -08:00
|
|
|
raise x
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
let c_file name =
|
1998-11-06 07:39:43 -08:00
|
|
|
if Ccomp.compile_file name <> 0 then exit 2
|