mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 14:25:56 +00:00
code_info.nl: Tidy up the code for code_info__cons_id_to_tag. prog_io.nl, mode_util.nl, mercury_to_mercury.nl: Add new inst `free(Type)' and new inst_names `typed_ground(Type)' and `typed_inst(Type, Inst)' so that we can propagate type information through the mode system. Do some of the work necessary to propagate type info to modes. type_util.nl: Add an extra output argument to type_to_type_id so that it can return the type's arguments; also fix a determinism error in type_to_type_id. hlds.nl: Add a comment. list.nl: Fix the determinism annotation for same_length. map.nl: Add map__apply_to_list. mode_info.nl: Add mode_info__get_types_of_vars.
124 lines
3.9 KiB
Mathematica
124 lines
3.9 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.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- 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 non-variable type, return it's type-id and argument types.
|
|
|
|
:- pred type_to_type_id(type, type_id, list(type)).
|
|
:- mode type_to_type_id(in, out, out) is det.
|
|
|
|
% 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 = term__functor(_, _, _),
|
|
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.
|
|
|
|
type_to_type_id(term__variable(_), _, _) :-
|
|
error("cannot make type_id for a type variable").
|
|
type_to_type_id(term__functor(Name, Args, _), TypeId, Args) :-
|
|
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").
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|