[m-rev.] for review: redefine io.state and store.store types

Peter Wang novalazy at gmail.com
Tue Aug 17 15:55:59 AEST 2010


Branches: main, 10.04

Redefine `io.state' and `store.store(S)' as foreign types.  Previously they
were defined as d.u. types, but since they are treated as dummy types by the
compiler, attempts to deconstruct values of those types (e.g. for printing
in ssdebug) could crash the program.

library/io.m:
library/store.m:
        As above.

diff --git a/library/io.m b/library/io.m
index 402b1f3..cf3a5d9 100644
--- a/library/io.m
+++ b/library/io.m
@@ -1694,10 +1694,14 @@
 
     % Values of type `io.state' are never really used:
     % instead we store data in global variables.
-    % The reason this is not defined simply as `io.state == c_pointer'
-    % is so that `type_name' produces more informative results
-    % for cases such as `type_name(main)'.
-:- type io.state ---> state(c_pointer).
+    % The reason this is defined as a foreign type is to prevent attempts
+    % to deconstruct values of the type.
+:- type io.state.
+:- pragma foreign_type("C", io.state, "MR_Word", [can_pass_as_mercury_type]).
+:- pragma foreign_type("IL", io.state, "int32", [can_pass_as_mercury_type]).
+:- pragma foreign_type("Java", io.state, "java.lang.Object",
+    [can_pass_as_mercury_type]).
+:- pragma foreign_type("Erlang", io.state, "", [can_pass_as_mercury_type]).
 
 :- pragma foreign_decl("C", "
     extern MR_Word      ML_io_stream_db;
diff --git a/library/store.m b/library/store.m
index f6d10c8..45e3794 100644
--- a/library/store.m
+++ b/library/store.m
@@ -243,9 +243,14 @@
 % The store type itself is just a dummy type,
 % with no real representation.
 
-    % XXX We use `mkstore' here rather than `store' to work around a bug
-    % with the Java back-end: it generates invalid Java code if we use `store'.
-:- type store(S) ---> mkstore(c_pointer)
+:- type store(S).
+:- pragma foreign_type("C", store(S), "MR_Word", [can_pass_as_mercury_type])
+    where equality is store_equal, comparison is store_compare.
+:- pragma foreign_type("IL", store(S), "int32", [can_pass_as_mercury_type])
+    where equality is store_equal, comparison is store_compare.
+:- pragma foreign_type("Java", store(S), "int", [can_pass_as_mercury_type])
+    where equality is store_equal, comparison is store_compare.
+:- pragma foreign_type("Erlang", store(S), "", [can_pass_as_mercury_type])
     where equality is store_equal, comparison is store_compare.
 
 :- pred store_equal(store(S)::in, store(S)::in) is semidet.

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list