Files
mercury/compiler/source_file_map.m
Zoltan Somogyi 672f77c4ec Add a new compiler option. --inform-ite-instead-of-switch.
Estimated hours taken: 20
Branches: main

Add a new compiler option. --inform-ite-instead-of-switch. If this is enabled,
the compiler will generate informational messages about if-then-elses that
it thinks should be converted to switches for the sake of program reliability.

Act on the output generated by this option.

compiler/simplify.m:
	Implement the new option.

	Fix an old bug that could cause us to generate warnings about code
	that was OK in one duplicated copy but not in another (where a switch
	arm's code is duplicated due to the case being selected for more than
	one cons_id).

compiler/options.m:
	Add the new option.

	Add a way to test for the bug fix in simplify.

doc/user_guide.texi:
	Document the new option.

NEWS:
	Mention the new option.

library/*.m:
mdbcomp/*.m:
browser/*.m:
compiler/*.m:
deep_profiler/*.m:
	Convert if-then-elses to switches at most of the sites suggested by the
	new option. At the remaining sites, switching to switches would have
	nontrivial downsides. This typically happens with the switched-on type
	has many functors, and we treat one or two specially (e.g. cons/2 in
	the cons_id type).

	Perform misc cleanups in the vicinity of the if-then-else to switch
	conversions.

	In a few cases, improve the error messages generated.

compiler/accumulator.m:
compiler/hlds_goal.m:
	(Rename and) move insts for particular kinds of goal from
	accumulator.m to hlds_goal.m, to allow them to be used in other
	modules. Using these insts allowed us to eliminate some if-then-elses
	entirely.

compiler/exprn_aux.m:
	Instead of fixing some if-then-elses, delete the predicates containing
	them, since they aren't used, and (as pointed out by the new option)
	would need considerable other fixing if they were ever needed again.

compiler/lp_rational.m:
	Add prefixes to the names of the function symbols on some types,
	since without those prefixes, it was hard to figure out what type
	the switch corresponding to an old if-then-else was switching on.

tests/invalid/reserve_tag.err_exp:
	Expect a new, improved error message.
2007-11-23 07:36:01 +00:00

251 lines
8.3 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2002-2007 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% File: source_file_map.m.
% Author: stayl.
%
% Maintain a mapping from module name to source file name.
%
%-----------------------------------------------------------------------------%
:- module parse_tree.source_file_map.
:- interface.
:- import_module mdbcomp.prim_data.
:- import_module parse_tree.prog_io.
:- import_module bool.
:- import_module io.
:- import_module list.
%-----------------------------------------------------------------------------%
% lookup_module_source_file(ModuleName, FileName, !IO)
%
:- pred lookup_module_source_file(module_name::in, file_name::out,
io::di, io::uo) is det.
% Return `yes' if there is a valid Mercury.modules file.
%
:- pred have_source_file_map(bool::out, io::di, io::uo) is det.
% Return the default fully-qualified source file name.
%
:- func default_source_file(module_name) = file_name.
% Given a list of file names, produce the Mercury.modules file.
%
:- pred write_source_file_map(list(string)::in, io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module libs.globals.
:- import_module parse_tree.modules.
:- import_module parse_tree.prog_out.
:- import_module char.
:- import_module dir.
:- import_module map.
:- import_module maybe.
:- import_module string.
%-----------------------------------------------------------------------------%
lookup_module_source_file(ModuleName, FileName, !IO) :-
get_source_file_map(SourceFileMap, !IO),
( map.search(SourceFileMap, ModuleName, FileName0) ->
FileName = FileName0
;
FileName = default_source_file(ModuleName)
).
default_source_file(ModuleName) = sym_name_to_string(ModuleName) ++ ".m".
have_source_file_map(HaveMap, !IO) :-
get_source_file_map(_, !IO),
globals.io_get_globals(Globals, !IO),
globals.get_source_file_map(Globals, MaybeSourceFileMap),
(
MaybeSourceFileMap = yes(Map),
\+ map.is_empty(Map)
->
HaveMap = yes
;
HaveMap = no
).
% Read the Mercury.modules file (if it exists) to find the mapping
% from module name to file name.
%
:- pred get_source_file_map(source_file_map::out, io::di, io::uo) is det.
get_source_file_map(SourceFileMap, !IO) :-
globals.io_get_globals(Globals0, !IO),
globals.get_source_file_map(Globals0, MaybeSourceFileMap0),
(
MaybeSourceFileMap0 = yes(SourceFileMap0),
SourceFileMap = SourceFileMap0
;
MaybeSourceFileMap0 = no,
io.open_input(modules_file_name, OpenRes, !IO),
(
OpenRes = ok(Stream),
io.set_input_stream(Stream, OldStream, !IO),
read_source_file_map([], map.init, SourceFileMap, !IO),
io.set_input_stream(OldStream, _, !IO),
io.close_input(Stream, !IO)
;
OpenRes = error(_),
% If the file doesn't exist, then the mapping is empty.
SourceFileMap = map.init
),
globals.io_get_globals(Globals1, !IO),
globals.set_source_file_map(yes(SourceFileMap), Globals1, Globals),
globals.io_set_globals(Globals, !IO)
).
:- pred read_source_file_map(list(char)::in,
source_file_map::in, source_file_map::out, io::di, io::uo) is det.
read_source_file_map(ModuleChars, !Map, !IO) :-
read_until_char('\t', [], ModuleCharsResult, !IO),
(
ModuleCharsResult = ok(RevModuleChars),
string.from_rev_char_list(RevModuleChars, ModuleStr),
ModuleName = string_to_sym_name(ModuleStr),
read_until_char('\n', [], FileNameCharsResult, !IO),
(
FileNameCharsResult = ok(FileNameChars),
string.from_rev_char_list(FileNameChars, FileName),
map.set(!.Map, ModuleName, FileName, !:Map),
read_source_file_map(ModuleChars, !Map, !IO)
;
FileNameCharsResult = eof,
io.set_exit_status(1, !IO),
io.write_string("mercury_compile: unexpected end " ++
"of file in Mercury.modules file.\n", !IO)
;
FileNameCharsResult = error(Error),
io.set_exit_status(1, !IO),
io.write_string("mercury_compile: error in " ++
"Mercury.modules file: ", !IO),
io.write_string(io.error_message(Error), !IO),
io.nl(!IO)
)
;
ModuleCharsResult = eof
;
ModuleCharsResult = error(Error),
io.set_exit_status(1, !IO),
io.write_string("mercury_compile: error in " ++
"Mercury.modules file: ", !IO),
io.write_string(io.error_message(Error), !IO),
io.nl(!IO)
).
:- pred read_until_char(char::in, list(char)::in, io.result(list(char))::out,
io::di, io::uo) is det.
read_until_char(EndChar, Chars0, Result, !IO) :-
io.read_char(CharRes, !IO),
(
CharRes = ok(Char),
( Char = EndChar ->
Result = ok(Chars0)
;
read_until_char(EndChar, [Char | Chars0], Result, !IO)
)
;
CharRes = eof,
(
Chars0 = [],
Result = eof
;
Chars0 = [_ | _],
Result = ok(Chars0)
)
;
CharRes = error(Error),
Result = error(Error)
).
write_source_file_map(FileNames, !IO) :-
ModulesFileName = modules_file_name,
io.open_output(ModulesFileName, OpenRes, !IO),
(
OpenRes = ok(Stream),
list.foldl2(write_source_file_map_2(Stream), FileNames,
map.init, _, !IO),
io.close_output(Stream, !IO)
;
OpenRes = error(Error),
io.set_exit_status(1, !IO),
io.write_string("mercury_compile: error opening `", !IO),
io.write_string(ModulesFileName, !IO),
io.write_string("' for output: ", !IO),
io.write_string(io.error_message(Error), !IO)
).
:- pred write_source_file_map_2(io.output_stream::in, file_name::in,
map(module_name, file_name)::in, map(module_name, file_name)::out,
io::di, io::uo) is det.
write_source_file_map_2(MapStream, FileName, SeenModules0, SeenModules, !IO) :-
find_module_name(FileName, MaybeModuleName, !IO),
(
MaybeModuleName = yes(ModuleName),
(
map.search(SeenModules0, ModuleName, PrevFileName),
PrevFileName \= FileName
->
io.write_string("mercury_compile: module `", !IO),
io.write_string(sym_name_to_string(ModuleName), !IO),
io.write_string("' defined in multiple files: ", !IO),
io.write_string(PrevFileName, !IO),
io.write_string(", ", !IO),
io.write_string(FileName, !IO),
io.write_string(".\n", !IO),
io.set_exit_status(1, !IO),
SeenModules = SeenModules0
;
map.set(SeenModules0, ModuleName, FileName, SeenModules)
),
( string.remove_suffix(FileName, ".m", PartialFileName0) ->
PartialFileName = PartialFileName0
;
PartialFileName = FileName
),
file_name_to_module_name(dir.basename_det(PartialFileName),
DefaultModuleName),
(
% Only include a module in the mapping if the name doesn't match
% the default.
dir.dirname(PartialFileName) = dir.this_directory : string,
ModuleName = DefaultModuleName
->
true
;
io.set_output_stream(MapStream, OldStream, !IO),
prog_out.write_sym_name(ModuleName, !IO),
io.write_string("\t", !IO),
io.write_string(FileName, !IO),
io.nl(!IO),
io.set_output_stream(OldStream, _, !IO)
)
;
MaybeModuleName = no,
SeenModules = SeenModules0
).
:- func modules_file_name = string.
modules_file_name = "Mercury.modules".