Files
mercury/compiler/type_util.m
Fergus Henderson a02ee50c30 Fix the definition of MERCC.
Makefile.mercury:
	Fix the definition of MERCC.

hlds.nl:
	For each discriminated union type, store the tags for the
	constructors for that type, and store whether
	or not the type is an enumeration type.

make_hlds.nl, make_tags.nl:
	Added a new file `make_tags.nl' which is caled from make_hlds.nl
	to computes the above info.

type_util.nl:
	In type_is_enumeration, use the flag stored in the HLDS rather
	than recomputing it on demand.

undef_types.nl:
	Minor change to match the changes in hlds.nl.

mercury_compile.nl:
	Ensure that foo.err depends on foo.nl.
	Dump the hlds before liveness analysis as well as after
	compilation (this is probably just a temporary hack).
1994-06-24 05:25:28 +00:00

121 lines
3.7 KiB
Mathematica

%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% File: type_util.nl.
% Main author: fjh.
% This file provides some utility predicates which operate on types.
% It is used by various stages of the compilation after type-checking,
% include the mode checker and the code generator.
% XXX TODO: implement type_is_enumeration.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module type_util.
:- interface.
:- import_module prog_io, hlds.
%-----------------------------------------------------------------------------%
% Succeed iff type is an "atomic" type - one which can be
% unified using a simple_unify (register comparison) rather
% than a complicated_unify.
:- pred type_is_atomic(type, module_info).
:- mode type_is_atomic(in, in) is semidet.
%-----------------------------------------------------------------------------%
% Given a type, determine what sort of type it is.
:- pred classify_type(type, module_info, builtin_type).
:- mode classify_type(in, in, out) is det.
:- type builtin_type ---> inttype
; chartype
; strtype
; enumtype
; usertype(type).
%-----------------------------------------------------------------------------%
% Given a constant and an arity, return a type_id.
:- pred make_type_id(const, int, type_id).
:- mode make_type_id(in, in, out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module list, term, require, map, std_util.
type_is_atomic(Type, ModuleInfo) :-
classify_type(Type, ModuleInfo, BuiltinType),
% XXX we can't use \= until the compiler handles
% scopes for \= properly.
\+ BuiltinType = usertype(_).
%-----------------------------------------------------------------------------%
% Given a type, determine what sort of type it is.
classify_type(VarType, ModuleInfo, Type) :-
(
VarType = term__functor(term__atom("character"), [], _)
->
Type = chartype
;
VarType = term__functor(term__atom("int"), [], _)
->
Type = inttype
;
VarType = term__functor(term__atom("string"), [], _)
->
Type = strtype
;
type_is_enumeration(VarType, ModuleInfo)
->
Type = enumtype
;
Type = usertype(VarType)
).
:- pred type_is_enumeration(type, module_info).
:- mode type_is_enumeration(in, in) is semidet.
type_is_enumeration(Type, ModuleInfo) :-
type_to_type_id(Type, TypeId),
module_info_types(ModuleInfo, TypeDefnTable),
map__lookup(TypeDefnTable, TypeId, TypeDefn),
TypeDefn = hlds__type_defn(_, _, TypeBody, _, _),
TypeBody = du_type(_, _, IsEnum),
IsEnum = yes.
:- pred type_to_type_id(type, type_id).
:- mode type_to_type_id(in, out) is det.
type_to_type_id(term__functor(Name, Args, _), TypeId) :-
list__length(Args, Arity),
make_type_id(Name, Arity, TypeId).
%-----------------------------------------------------------------------------%
% Given a constant and an arity, return a type_id.
% XXX this should take a name and an arity;
% use of integers/floats/strings as type names should
% be rejected by the parser in prog_io.nl, not here.
make_type_id(term__atom(Name), Arity, unqualified(Name) - Arity).
make_type_id(term__integer(_), _, unqualified("<error>") - 0) :-
error("atom expected").
make_type_id(term__float(_), _, unqualified("<error>") - 0) :-
error("atom expected").
make_type_id(term__string(_), _, unqualified("<error>") - 0) :-
error("atom expected").
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%