[m-rev.] for review: fix some problems with module qualification

David Overton dmo at cs.mu.OZ.AU
Thu Feb 20 12:55:58 AEDT 2003


Estimated hours taken: 2
Branches: main

Some changes related to module qualification.

These changes fix problems for the HAL library that were introduced by
the change to not module-qualify symbols when writing out interface
files.

compiler/prog_io_util.m:
	When parsing an inst, if it has the module-qualifier `builtin'
	try to parse it as a builtin inst.

compiler/mercury_to_mercury.m:
	When writing out inst declarations, if the inst on the
	left-hand-side of the declaration could be interpreted as a
	builtin inst when unqualified, then make sure it is output
	module-qualified so that it can be read back in correctly
	without generating an "attempt to redefine builtin inst" error.

	In `maybe_unqualify_sym_name', do not unqualify names containing
	`__'.  This ensures that such names are output to the interface
	files module-qualified and can then be read back in correctly
	without interpreting the `__' as a module qualifier.

tests/hard_coded/builtin_inst_rename.m:
tests/hard_coded/builtin_inst_rename2.m:
tests/hard_coded/Mmakefile.m:
	Test case.

Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.225
diff -u -r1.225 mercury_to_mercury.m
--- compiler/mercury_to_mercury.m	12 Feb 2003 22:58:09 -0000	1.225
+++ compiler/mercury_to_mercury.m	20 Feb 2003 01:52:49 -0000
@@ -353,11 +353,12 @@
 :- implementation.
 
 :- import_module parse_tree__prog_out, parse_tree__prog_util, hlds__hlds_pred.
+:- import_module parse_tree__prog_io_util.
 :- import_module hlds__hlds_out, hlds__instmap.
 :- import_module recompilation__version.
 :- import_module check_hlds__purity, check_hlds__mode_util.
 :- import_module transform_hlds__term_util.
-:- import_module libs__options, transform_hlds__termination.
+:- import_module libs__globals, libs__options, transform_hlds__termination.
 :- import_module backend_libs__foreign.
 
 :- import_module assoc_list, char, int, string, set, lexer, ops, require.
@@ -435,7 +436,15 @@
 mercury_output_item(UnqualifiedItemNames,
 		inst_defn(VarSet, Name0, Args, InstDefn, _Cond),
 		Context) -->
-	{ maybe_unqualify_sym_name(UnqualifiedItemNames, Name0, Name) },
+	{ maybe_unqualify_sym_name(UnqualifiedItemNames, Name0, Name1) },
+	% If the unqualified name is a builtin inst, then output the qualified
+	% name.  This prevents the compiler giving a warning about redefining
+	% builtin insts when an interface file is read back in.
+	{ builtin_inst_name(Name1, Args) ->
+		Name = Name0
+	;
+		Name = Name1
+	},
 	maybe_output_line_number(Context),
 	mercury_output_inst_defn(VarSet, Name, Args, InstDefn, Context).
 
@@ -3784,8 +3793,18 @@
 :- mode maybe_unqualify_sym_name(in, in, out) is det.
 
 maybe_unqualify_sym_name(no, Name, Name).
-maybe_unqualify_sym_name(yes, Name0, unqualified(Name)) :-
-	unqualify_name(Name0, Name).
+maybe_unqualify_sym_name(yes, Name0, Name) :-
+	unqualify_name(Name0, Name1),
+	( string__sub_string_search(Name1, "__", _) ->
+		% Do not unqualify a name that contains "__".  This prevents the
+		% "__" being treated a module qualifier when the name is read
+		% back in.
+		% XXX this whole `__' module qualifier thing is a bit of a hack
+		% isn't it.
+		Name = Name0
+	;
+		Name = unqualified(Name1)
+	).
 
 %-----------------------------------------------------------------------------%
 
@@ -3964,5 +3983,17 @@
 		output_string(Sep, Str1, Str2),
 		output_list(Items, Sep, Pred, Str2, Str)
 	).
+
+%-----------------------------------------------------------------------------%
+
+% Succeed if the sym_name describes a builtin inst.
+
+:- pred builtin_inst_name(sym_name::in, list(inst_var)::in) is semidet.
+
+builtin_inst_name(unqualified(Name), Args0) :-
+	Args1 = list__map(func(V) = term__variable(term__coerce_var(V)), Args0),
+	Term = term__functor(term__atom(Name), Args1, term__context_init),
+	convert_inst(no_allow_constrained_inst_var, Term, Inst),
+	Inst \= defined_inst(user_inst(_, _)).
 
 %-----------------------------------------------------------------------------%
Index: compiler/prog_io_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/prog_io_util.m,v
retrieving revision 1.25
diff -u -r1.25 prog_io_util.m
--- compiler/prog_io_util.m	9 Jul 2002 01:29:58 -0000	1.25
+++ compiler/prog_io_util.m	20 Feb 2003 01:52:49 -0000
@@ -167,8 +167,9 @@
 
 :- implementation.
 
-:- import_module parse_tree__prog_io, parse_tree__prog_io_goal, libs__options.
-:- import_module libs__globals.
+:- import_module parse_tree__prog_io, parse_tree__prog_io_goal.
+:- import_module parse_tree__prog_util.
+:- import_module libs__options, libs__globals.
 
 % XXX we should not need to import hlds*.m here.
 % But currently we need to import hlds_data.m for the `cons_id' type
@@ -358,7 +359,7 @@
 convert_inst(_, term__variable(V0), inst_var(V)) :-
 	term__coerce_var(V0, V).
 convert_inst(AllowConstrainedInstVar, Term, Result) :-
-	Term = term__functor(Name, Args0, _Context),
+	Term = term__functor(Name, Args0, Context),
 	% `free' insts
 	( Name = term__atom("free"), Args0 = [] ->
 		Result = free
@@ -455,8 +456,30 @@
 	;
 		parse_qualified_term(Term, Term, "inst",
 			ok(QualifiedName, Args1)),
-		convert_inst_list(AllowConstrainedInstVar, Args1, Args),
-		Result = defined_inst(user_inst(QualifiedName, Args))
+		(
+			mercury_public_builtin_module(BuiltinModule),
+			sym_name_get_module_name(QualifiedName, unqualified(""),
+				BuiltinModule),
+			% If the term is qualified with the `builtin' module
+			% then it may be one of the builtin insts.
+			% We call convert_inst recursively to check for this.
+			unqualify_name(QualifiedName, UnqualifiedName),
+			UnqualifiedTerm =
+				term__functor(term__atom(UnqualifiedName),
+					Args1, Context),
+			convert_inst(AllowConstrainedInstVar, UnqualifiedTerm,
+				Result0),
+
+			% However, if the inst is a user_inst defined inside
+			% the `builtin' module then we need to make sure it is
+			% properly module-qualified.
+			Result0 \= defined_inst(user_inst(_, _))
+		->
+			Result = Result0
+		;
+			convert_inst_list(AllowConstrainedInstVar, Args1, Args),
+			Result = defined_inst(user_inst(QualifiedName, Args))
+		)
 	).
 
 standard_det("det", det).
Index: tests/hard_coded/builtin_inst_rename.m
===================================================================
RCS file: tests/hard_coded/builtin_inst_rename.m
diff -N tests/hard_coded/builtin_inst_rename.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/builtin_inst_rename.m	20 Feb 2003 01:52:49 -0000
@@ -0,0 +1,22 @@
+:- module builtin_inst_rename.
+
+:- interface.
+
+:- import_module builtin_inst_rename2.
+:- import_module io.
+
+:- pred p(builtin_inst_rename2.my__int, builtin_inst_rename2.my__int).
+:- mode p(builtin_inst_rename2__ground >> builtin_inst_rename2__ground,
+	  builtin_inst_rename2__free   >> builtin_inst_rename2__ground)
+	is det.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+p(X, X).
+
+main -->
+	{ p(42, X) },
+	io__write_int(X),
+	io__nl.
Index: tests/hard_coded/builtin_inst_rename2.m
===================================================================
RCS file: tests/hard_coded/builtin_inst_rename2.m
diff -N tests/hard_coded/builtin_inst_rename2.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/builtin_inst_rename2.m	20 Feb 2003 01:52:49 -0000
@@ -0,0 +1,8 @@
+:- module builtin_inst_rename2.
+
+:- interface.
+
+:- inst builtin_inst_rename2__free == builtin__free.
+:- inst builtin_inst_rename2__ground == builtin__ground.
+
+:- type builtin_inst_rename2.my__int == int.
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.190
diff -u -r1.190 Mmakefile
--- tests/hard_coded/Mmakefile	18 Feb 2003 06:27:32 -0000	1.190
+++ tests/hard_coded/Mmakefile	20 Feb 2003 01:52:49 -0000
@@ -8,6 +8,7 @@
 	address_of_builtins \
 	agg \
 	bidirectional \
+	builtin_inst_rename \
 	boyer \
 	c_write_string \
 	cc_and_non_cc_test \
-- 
David Overton                  Uni of Melbourne     +61 3 8344 1354
dmo at cs.mu.oz.au                Monash Uni (Clayton) +61 3 9905 5779
http://www.cs.mu.oz.au/~dmo    Mobile Phone         +61 4 0337 4393
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list