[m-rev.] diff: bug fixes for il grade

Tyson Dowd trd at cs.mu.OZ.AU
Mon Apr 23 21:29:16 AEST 2001


On 22-Apr-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 19-Apr-2001, Tyson Dowd <trd at cs.mu.OZ.AU> wrote:
> > Branches: main
> > 
> > Add support for nested classes.
> > Fix the implementation of inheritance.
> > 
> > compiler/ilasm.m:
> > 	Add code to allow nested classes in the IL assembler.
> > 	
> > compiler/mlds_to_il.m:
> > 	Add code to generate nested classes.
> > 	Fix bugs in the code to handle inheritance -- most of the
> > 	inheritance was hand-coded for the case of continutation
> > 	environments.
> 
> Thanks for that, Tyse.
> 
> > +++ compiler/mlds_to_il.m	2001/04/19 13:50:22
> > @@ -471,33 +471,21 @@
> >  		( 
> >  			Entity = mlds__class(ClassDefn) 
> >  		->
> > +			ClassDefn = mlds__class_defn(_ClassType, _Imports, 
> > +				Inherits, _Implements, Defns),
> > +			( Inherits = [],
> > +				Extends = extends_nothing
> > +			; Inherits = [InheritType | _],
> > +				Extends = extends(mlds_type_to_ilds_class_name(
> > +					InheritType))
> > +			),
> 
> It would be better for this code to complain rather than
> just ignoring the second base class, if there is more than one:
> 
> 			( Inherits = [],
> 				Extends = extends_nothing
> 			; Inherits = [InheritType],
> 				Extends = extends(mlds_type_to_ilds_class_name(
> 					InheritType))
> 			; Inherits = [_, _ | _],
> 				error("multiple inheritance not supported")
> 			),
> 
> > +			list__map(defn_to_class_decl, Defns, ILDefns),
> > +			make_constructor(FullClassName, ClassDefn, 
> > +				ConstructorILDefn),
> > +			Decls = [comment_term(MLDSDefnTerm),
> > +				class([public], TypeName,
> > +				Extends, implements([]),
> > +				[ConstructorILDefn | ILDefns])]
> 
> I think the "[public]" there is not right.
> It's pretty harmless, so no need to fix it now, but an XXX comment
> would be a good idea.
> 
> >  		;
> >  			Decls = [comment_term(MLDSDefnTerm),
> >  				comment("This type unimplemented.")]
> > @@ -663,12 +651,25 @@
> >  	mlds__function(_PredProcId, _Params, _MaybeStatements)), ILClassDecl) :-
> >  		ILClassDecl = comment("unimplemented: functions in classes").
> >  
> > -	% XXX this might not need to be implemented (nested classes)
> > -	% since it will probably be flattened earlier.
> > -defn_to_class_decl(mlds__defn(_Name, _Context, _DeclFlags,
> > -		mlds__class(_)), _ILClassDecl) :-
> > -	error("nested data definition not expected here").
> > -
> > +defn_to_class_decl(mlds__defn(EntityName, _Context, _DeclFlags,
> > +		mlds__class(ClassDefn)), ILClassDecl) :-
> > +	( EntityName = type(TypeName, _Arity) ->
> > +		ClassDefn = mlds__class_defn(_ClassType, _Imports, 
> > +			Inherits, _Implements, Defns),
> > +		FullClassName = structured_name("", [TypeName]),
> > +		list__map(defn_to_class_decl, Defns, ILDefns),
> > +		make_constructor(FullClassName, ClassDefn, ConstructorILDefn),
> > +		( Inherits = [],
> > +			Extends = extends_nothing
> > +		; Inherits = [InheritType | _],
> > +			Extends = extends(mlds_type_to_ilds_class_name(
> > +				InheritType))
> > +		),
> > +		ILClassDecl = nested_class([public], TypeName, Extends,
> > +			implements([]), [ConstructorILDefn | ILDefns])
> > +	;
> > +		error("expected type entity name for a nested class")
> > +	).
> 
> Same comments apply here too.
> There seems to be some duplicated code here, it would be worth extracting
> that out into a separate procedure.

I separated out the inhertitance calculation, which means the rest is
pretty trivial stuff.  There are some small diferences in how it is done
that make separating the remaining stuff a bit difficult for the
expected gain.

I will commit this change on the main branch.

Here are the incremental changes:

diff -u compiler/mlds_to_il.m compiler/mlds_to_il.m
--- compiler/mlds_to_il.m
+++ compiler/mlds_to_il.m
@@ -473,15 +473,11 @@
 		->
 			ClassDefn = mlds__class_defn(_ClassType, _Imports, 
 				Inherits, _Implements, Defns),
-			( Inherits = [],
-				Extends = extends_nothing
-			; Inherits = [InheritType | _],
-				Extends = extends(mlds_type_to_ilds_class_name(
-					InheritType))
-			),
+			Extends = mlds_inherits_to_ilds_inherits(Inherits),
 			list__map(defn_to_class_decl, Defns, ILDefns),
 			make_constructor(FullClassName, ClassDefn, 
 				ConstructorILDefn),
+				% XXX we assume public here
 			Decls = [comment_term(MLDSDefnTerm),
 				class([public], TypeName,
 				Extends, implements([]),
@@ -499,6 +495,7 @@
 		Decls = []
 	).
 
+
 %-----------------------------------------------------------------------------
 
 	%
@@ -659,12 +656,7 @@
 		FullClassName = structured_name("", [TypeName]),
 		list__map(defn_to_class_decl, Defns, ILDefns),
 		make_constructor(FullClassName, ClassDefn, ConstructorILDefn),
-		( Inherits = [],
-			Extends = extends_nothing
-		; Inherits = [InheritType | _],
-			Extends = extends(mlds_type_to_ilds_class_name(
-				InheritType))
-		),
+		Extends = mlds_inherits_to_ilds_inherits(Inherits),
 		ILClassDecl = nested_class([public], TypeName, Extends,
 			implements([]), [ConstructorILDefn | ILDefns])
 	;
@@ -1698,6 +1690,17 @@
 %
 % Conversion of MLDS types to IL types.
 
+:- func mlds_inherits_to_ilds_inherits(list(mlds__type)) = ilasm__extends.
+mlds_inherits_to_ilds_inherits(Inherits) = Extends :-
+	( Inherits = [],
+		Extends = extends_nothing
+	; Inherits = [InheritType],
+		Extends = extends(mlds_type_to_ilds_class_name(
+			InheritType))
+	; Inherits = [_, _ | _],
+		error("multiple inheritance not supported")
+	).
+
 :- pred mlds_signature_to_ilds_type_params(mlds__func_signature, list(ilds__type)).
 :- mode mlds_signature_to_ilds_type_params(in, out) is det.
 mlds_signature_to_ilds_type_params(func_signature(Args, _Returns), Params) :-
@@ -1835,15 +1838,18 @@
 		[MldsClassName]).
 
 :- func mlds_type_to_ilds_class_name(mlds__type) = ilds__class_name.
-mlds_type_to_ilds_class_name(MldsType) = ClassName :-
-	ILType = mlds_type_to_ilds_type(MldsType),
+mlds_type_to_ilds_class_name(MldsType) = 
+	get_ilds_type_class_name(mlds_type_to_ilds_type(MldsType)).
+
+:- func get_ilds_type_class_name(ilds__type) = ilds__class_name.
+get_ilds_type_class_name(ILType) = ClassName :-
 	( 
 		ILType = ilds__type(_, class(ClassName0))
 	->
 		ClassName = ClassName0
 	;
 		unexpected(this_file,
-			"mlds_type_to_ilds_class_name: type not a class")
+			"get_ilds_type_class_name: type not a class")
 	).	
 
 
@@ -2471,10 +2477,11 @@
 :- mode make_constructor(in, in, out) is det.
 make_constructor(ClassName, mlds__class_defn(_,  _Imports, Inherits, 
 		_Implements, Defns), ILDecl) :-
-	( Inherits = [],
+	Extends = mlds_inherits_to_ilds_inherits(Inherits),
+	( Extends = extends_nothing,
 		CtorMemberName = il_generic_class_name
-	; Inherits = [InheritType | _],
-		CtorMemberName = mlds_type_to_ilds_class_name(InheritType)
+	; Extends = extends(CtorMemberName0),
+		CtorMemberName = CtorMemberName0
 	),
 	list__map(call_field_constructor(ClassName), Defns, 
 		FieldConstrInstrsLists),

-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
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