Files
mercury/compiler/hlds_code_util.m
Zoltan Somogyi 885fd4a387 Remove almost all dependencies by the modules of parse_tree.m on the modules
Estimated hours taken: 12
Branches: main

Remove almost all dependencies by the modules of parse_tree.m on the modules
of hlds.m. The only such dependencies remaining now are on type_util.m.

compiler/hlds_data.m:
compiler/prog_data.m:
	Move the cons_id type from hlds_data to prog_data, since several parts
	of the parse tree data structure depend on it (particularly insts).
	Remove the need to import HLDS modules in prog_data.m by making the
	cons_ids that refer to procedure ids refer to them via a new type
	that contains shrouded pred_ids and proc_ids. Since pred_ids and
	proc_ids are abstract types in hlds_data, add predicates to hlds_data
	to shroud and unshroud them.

	Also move some other types, e.g. mode_id and class_id, from hlds_data
	to prog_data.

compiler/hlds_data.m:
compiler/prog_util.m:
	Move predicates for manipulating cons_ids from hlds_data to prog_util.

compiler/inst.m:
compiler/prog_data.m:
	Move the contents of inst.m to prog_data.m, since that is where it
	belongs, and since doing so eliminates a circular dependency.
	The separation doesn't serve any purpose any more, since we don't
	need to import hlds_data.m anymore to get access to the cons_id type.

compiler/mode_util.m:
compiler/prog_mode.m:
compiler/parse_tree.m:
	Move the predicates in mode_util that don't depend on the HLDS to a new
	module prog_mode, which is part of parse_tree.m.

compiler/notes/compiler_design.m:
	Mention prog_mode.m, and delete the mention of inst.m.

compiler/mercury_to_mercury.m:
compiler/hlds_out.m:
	Move the predicates that depend on HLDS out of mercury_to_mercury.m
	to hlds_out.m. Export from mercury_to_mercury.m the predicates needed
	by the moved predicates.

compiler/hlds_out.m:
compiler/prog_out.m:
	Move predicates for printing parts of the parse tree out of hlds_out.m
	to prog_out.m, since mercury_to_mercury.m needs to use them.

compiler/purity.m:
compiler/prog_out.m:
	Move predicates for printing purities from purity.m, which is part
	of check_hlds.m, to prog_out.m, since mercury_to_mercury.m needs to use
	them.

compiler/passes_aux.m:
compiler/prog_out.m:
	Move some utility predicates (e.g. for printing progress messages) from
	passes_aux.m to prog_out.m, since some predicates in submodules of
	parse_tree.m need to use them.

compiler/foreign.m:
compiler/prog_data.m:
	Move some types from foreign.m to prog_data.m to allow the elimination
	of some dependencies on foreign.m from submodules of parse_tree.m.

compiler/*.m:
	Conform to the changes above, mostly by updating lists of imported
	modules and module qualifications. In some cases, also do some local
	cleanups such as converting predicate declarations to predmode syntax
	and fixing white space.
2004-06-14 04:17:03 +00:00

149 lines
5.4 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 2002-2004 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: hlds_code_util.m.
%
% various utilities routines for use during hlds generation.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module hlds__hlds_code_util.
:- interface.
:- import_module hlds__hlds_data.
:- import_module hlds__hlds_module.
:- import_module parse_tree__prog_data.
:- import_module list.
% Are equivalence types fully expanded on this backend?
:- pred are_equivalence_types_expanded(module_info::in) is semidet.
% Find out how a function symbol (constructor) is represented
% in the given type.
:- func cons_id_to_tag(cons_id, type, module_info) = cons_tag.
% Given a list of types, mangle the names so into a string which
% identifies them. The types must all have their top level functor
% bound, with any arguments free variables.
:- pred make_instance_string(list(type)::in, string::out) is det.
:- implementation.
:- import_module check_hlds__type_util.
:- import_module hlds__hlds_pred.
:- import_module libs__globals.
:- import_module libs__options.
:- import_module parse_tree__prog_io.
:- import_module parse_tree__prog_out.
:- import_module bool, char, string, require, map, std_util, term.
%-----------------------------------------------------------------------------%
are_equivalence_types_expanded(ModuleInfo) :-
module_info_globals(ModuleInfo, Globals),
globals__lookup_bool_option(Globals, highlevel_data, HighLevelData),
HighLevelData = yes,
globals__get_target(Globals, Target),
( Target = il ; Target = java).
%-----------------------------------------------------------------------------%
cons_id_to_tag(int_const(I), _, _) = int_constant(I).
cons_id_to_tag(float_const(F), _, _) = float_constant(F).
cons_id_to_tag(string_const(S), _, _) = string_constant(S).
cons_id_to_tag(pred_const(ShroudedPredProcId, EvalMethod), _, _) =
pred_closure_tag(PredId, ProcId, EvalMethod) :-
proc(PredId, ProcId) = unshroud_pred_proc_id(ShroudedPredProcId).
cons_id_to_tag(type_ctor_info_const(M,T,A), _, _) =
type_ctor_info_constant(M,T,A).
cons_id_to_tag(base_typeclass_info_const(M,C,_,N), _, _) =
base_typeclass_info_constant(M,C,N).
cons_id_to_tag(type_info_cell_constructor(_), _, _) = unshared_tag(0).
cons_id_to_tag(typeclass_info_cell_constructor, _, _) = unshared_tag(0).
cons_id_to_tag(tabling_pointer_const(ShroudedPredProcId), _, _) =
tabling_pointer_constant(PredId, ProcId) :-
proc(PredId, ProcId) = unshroud_pred_proc_id(ShroudedPredProcId).
cons_id_to_tag(deep_profiling_proc_layout(ShroudedPredProcId), _, _) =
deep_profiling_proc_layout_tag(PredId, ProcId) :-
proc(PredId, ProcId) = unshroud_pred_proc_id(ShroudedPredProcId).
cons_id_to_tag(table_io_decl(ShroudedPredProcId), _, _) =
table_io_decl_tag(PredId, ProcId) :-
proc(PredId, ProcId) = unshroud_pred_proc_id(ShroudedPredProcId).
cons_id_to_tag(cons(Name, Arity), Type, ModuleInfo) = Tag :-
(
% handle the `character' type specially
Type = term__functor(term__atom("character"), [], _),
Name = unqualified(ConsName),
string__char_to_string(Char, ConsName)
->
char__to_int(Char, CharCode),
Tag = int_constant(CharCode)
;
% Tuples do not need a tag. Note that unary tuples are not
% treated as no_tag types. There's no reason why they
% couldn't be, it's just not worth the effort.
type_is_tuple(Type, _)
->
Tag = single_functor
;
% Use the type to determine the type_ctor
( type_to_ctor_and_args(Type, TypeCtor0, _) ->
TypeCtor = TypeCtor0
;
% the type-checker should ensure that this never happens
error("cons_id_to_tag: invalid type")
),
% Given the type_ctor, lookup up the constructor tag
% table for that type
module_info_types(ModuleInfo, TypeTable),
map__lookup(TypeTable, TypeCtor, TypeDefn),
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
(
ConsTable0 = TypeBody ^ du_type_cons_tag_values
->
ConsTable = ConsTable0
;
% this should never happen
error("cons_id_to_tag: type is not d.u. type?")
),
% Finally look up the cons_id in the table
map__lookup(ConsTable, cons(Name, Arity), Tag)
).
%-----------------------------------------------------------------------------%
% Note that for historical reasons, builtin types
% are treated as being unqualified (`int') rather than
% being qualified (`builtin:int') at this point.
make_instance_string(InstanceTypes, InstanceString) :-
list__map(type_to_string, InstanceTypes, InstanceStrings),
string__append_list(InstanceStrings, InstanceString).
:- pred type_to_string((type)::in, string::out) is det.
type_to_string(Type, String) :-
( sym_name_and_args(Type, TypeName, TypeArgs) ->
prog_out__sym_name_to_string(TypeName, "__", TypeNameString),
list__length(TypeArgs, TypeArity),
string__int_to_string(TypeArity, TypeArityString),
string__append_list(
[TypeNameString, "__arity", TypeArityString, "__"],
String)
;
error("type_to_string: invalid type")
).
%----------------------------------------------------------------------------%