[m-rev.] diff: document MLDS handling of empty structs & arrays

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Feb 28 00:56:43 AEDT 2002


Estimated hours taken: 1
Branches: main

compiler/mlds.m:
compiler/mlds_to_c.m:
	Improve the documentation about the handling of empty structs
	and empty arrays.

Workspace: /home/ceres/fjh/mercury
Index: compiler/mlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds.m,v
retrieving revision 1.83
diff -u -d -r1.83 mlds.m
--- compiler/mlds.m	11 Feb 2002 11:27:05 -0000	1.83
+++ compiler/mlds.m	27 Feb 2002 13:41:31 -0000
@@ -546,6 +546,10 @@
 :- type mlds__class_name == string.
 :- type mlds__class == mlds__fully_qualified_name(mlds__class_name).
 
+	% Note that standard C doesn't support empty structs,
+	% so when targetting C, it is the MLDS code generator's
+	% responsibility to ensure that each generated MLDS class
+	% has at least one base class or non-static data member.
 :- type mlds__class_defn
 	---> mlds__class_defn(
 		kind	::	mlds__class_kind,
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.120
diff -u -d -r1.120 mlds_to_c.m
--- compiler/mlds_to_c.m	18 Feb 2002 07:00:56 -0000	1.120
+++ compiler/mlds_to_c.m	27 Feb 2002 13:55:45 -0000
@@ -1075,6 +1075,16 @@
 	% Output the class declaration and the class members.
 	% We treat enumerations specially.
 	%
+	% Note that standard ANSI/ISO C does not allow empty structs.
+	% We could handle empty structs here, by adding a dummy member,
+	% but that would waste a lot of space, and would also
+	% cause incompatibilities between the data layout for
+	% --high-level-data and --no-high-level-data.  So instead,
+	% we make it is the responsibility of the MLDS code generator
+	% to not generate any.  (E.g. ml_type_gen.m checks whether
+	% `target_uses_empty_base_classes' before generating empty
+	% structs.)  Hence we don't need to check for empty structs here.
+	%
 	mlds_output_class_decl(Indent, Name, ClassDefn),
 	io__write_string(" {\n"),
 	( { Kind = mlds__enum } ->
@@ -1226,8 +1236,6 @@
 mlds_needs_initialization(init_struct([_|_])) = yes.
 mlds_needs_initialization(init_array(_)) = yes.
 
-	% XXX ANSI/ISO C does not allow empty arrays or empty structs;
-	% what we do for them here is probably not quite right.
 :- pred mlds_output_initializer_body(mlds__initializer, io__state, io__state).
 :- mode mlds_output_initializer_body(in, di, uo) is det.
 
@@ -1235,16 +1243,24 @@
 mlds_output_initializer_body(init_obj(Rval)) -->
 	mlds_output_rval(Rval).
 mlds_output_initializer_body(init_struct(FieldInits)) -->
+	% Note that standard ANSI/ISO C does not allow empty structs.
+	% But it is the responsibility of the MLDS code generator
+	% to not generate any.  So we don't need to handle empty
+	% initializers specially here.
 	io__write_string("{\n\t\t"),
 	io__write_list(FieldInits, ",\n\t\t", mlds_output_initializer_body),
 	io__write_string("}").
 mlds_output_initializer_body(init_array(ElementInits)) -->
 	io__write_string("{\n\t\t"),
-	(
-		{ ElementInits = [] }
-	->
-			% The MS VC++ compiler only generates a symbol, if
-			% the array has a known size.
+	% Standard ANSI/ISO C does not allow empty arrays. But the MLDS does.
+	% To keep the C compiler happy, we therefore convert zero-element
+	% MLDS arrays into one-element C arrays.  (The extra element is
+	% a minor waste of space, but it will otherwise be ignored.)
+	% So if the initializer list here is empty, we need to output
+	% a single initializer.  We can initialize the extra element
+	% with any value; we use "0", since that is a valid initializer
+	% for any type.
+	( { ElementInits = [] } ->
 		io__write_string("0")
 	;
 		io__write_list(ElementInits,
@@ -1830,9 +1846,10 @@
 mlds_output_array_type_suffix(no_size) -->
 	io__write_string("[]").
 mlds_output_array_type_suffix(array_size(Size0)) -->
-	%
-	% ANSI/ISO C forbids arrays of size 0.
-	%
+	% Standard ANSI/ISO C does not allow arrays of size 0.
+	% But the MLDS does.  To keep the C compiler happy,
+	% we therefore convert zero-element MLDS arrays into
+	% one-element C arrays.
 	{ int__max(Size0, 1, Size) },
 	io__format("[%d]", [i(Size)]).
 
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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