[m-rev.] for review: nested classes

Peter Ross peter.ross at miscrit.be
Wed Jul 11 01:58:17 AEST 2001


Hi,

For Tyson to review.


===================================================================


Estimated hours taken: 4
Branches: main

Implement nested classes.

compiler/ilasm.m:
    Update the classattr type to the latest fields from the SDK
    documentation.
    Add the predicate output_classattr which will write the string
    representation of the attribute.

compiler/mlds_to_il.m:
    Factor out the code between classes and nested classes.
    Generate the ILDS for nested classes.

Index: compiler/ilasm.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ilasm.m,v
retrieving revision 1.12
diff -u -r1.12 ilasm.m
--- compiler/ilasm.m	8 Jul 2001 19:52:45 -0000	1.12
+++ compiler/ilasm.m	10 Jul 2001 15:54:07 -0000
@@ -184,12 +184,18 @@
 
 	% attributes that a class can have.
 	% see SDK documentation for what they all mean.
-:- type classattr
-	--->	abstract    ;  ansi       ;  auto          ;  autochar
-	;	contextful  ;  enum       ;  explicit      ;  import
-	;	interface   ;  lazyinit   ;  marshalbyref  ;  public
-	;	sealed      ;  sequential ;  unicode       ;  value
-	;	wrapper.
+:-  type classattr
+	--->	abstract		; ansi
+	;	auto			; autochar
+	;	beforefieldinit		; explicit
+	;	interface		; nestedassembly
+	;	nestedfamandassem	; nestedfamily
+	;	nestedfamorassem	; nestedprivate
+	;	nestedpublic		; private
+	;	public 			; rtspecialname
+	;	sealed			; sequential
+	;	serializable		; specialname
+	;	unicode.
 
 	% attributes that a method can have.	
 	% see SDK documentation for what they all mean.
@@ -296,7 +302,7 @@
 ilasm__output_decl(class(Attrs, Id, Extends, Implements, Contents),
 		Info0, Info) --> 
 	io__write_string(".class "),
-	io__write_list(Attrs, " ", io__write),
+	io__write_list(Attrs, " ", output_classattr),
 	( { Attrs \= [] } ->
 		io__write_string(" ")
 	;
@@ -1316,6 +1322,30 @@
 	io__write_string("("),
 	ilasm__write_list(ArgTypes, ", ", output_type, Info1, Info),
 	io__write_string(")").
+
+:- pred output_classattr(classattr::in, io__state::di, io__state::uo) is det.
+
+output_classattr(abstract) --> io__write_string("abstract").
+output_classattr(ansi) --> io__write_string("ansi").
+output_classattr(auto) --> io__write_string("auto").
+output_classattr(autochar) --> io__write_string("autochar").
+output_classattr(beforefieldinit) --> io__write_string("beforefieldinit").
+output_classattr(explicit) --> io__write_string("explicit").
+output_classattr(interface) --> io__write_string("interface").
+output_classattr(nestedassembly) --> io__write_string("nested assembly").
+output_classattr(nestedfamandassem) --> io__write_string("nested famandassem").
+output_classattr(nestedfamily) --> io__write_string("nested family").
+output_classattr(nestedfamorassem) --> io__write_string("nested famorassem").
+output_classattr(nestedprivate) --> io__write_string("nested private").
+output_classattr(nestedpublic) --> io__write_string("nested public").
+output_classattr(private) --> io__write_string("private").
+output_classattr(public) --> io__write_string("public").
+output_classattr(rtspecialname) --> io__write_string("rtspecialname").
+output_classattr(sealed) --> io__write_string("sealed").
+output_classattr(sequential) --> io__write_string("sequential").
+output_classattr(serializable) --> io__write_string("serializable").
+output_classattr(specialname) --> io__write_string("specialname").
+output_classattr(unicode) --> io__write_string("unicode").
 
 :- pred ilasm__output_assembly_decl(assembly_decl::in, 
 	io__state::di, io__state::uo) is det.
Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.40
diff -u -r1.40 mlds_to_il.m
--- compiler/mlds_to_il.m	10 Jul 2001 14:49:15 -0000	1.40
+++ compiler/mlds_to_il.m	10 Jul 2001 15:54:11 -0000
@@ -449,32 +449,9 @@
 mlds_defn_to_ilasm_decl(defn(Name, _Context, Flags, class(ClassDefn)),
 		Decl, Info0, Info) :-
 	il_info_new_class(Info0, Info1),
-	EntityName = entity_name_to_ilds_id(Name),
-	ClassDefn = class_defn(_Kind, _Imports, Inherits, Implements,
-			Ctors, Members),
-	( Inherits = [],
-		Extends = extends_nothing,
-		Parent = il_generic_class_name
-	; Inherits = [Parent0 | Rest],
-		( Rest = [] ->
-			Parent = mlds_type_to_ilds_class_name(
-					Info1 ^ il_data_rep, Parent0),
-			Extends = extends(Parent)
-		;
-			error(this_file ++ 
-				": multiple inheritance not supported.")
-		)
-	),
-
-	Interfaces = implements(
-			list__map(interface_id_to_class_name, Implements)),
 
-	ClassName = class_name(Info1 ^ module_name, EntityName),
-	list__map_foldl(generate_method(ClassName, no), Members,
-			MethodsAndFields, Info1, Info2),
-	list__map_foldl(generate_method(ClassName, yes(Parent)), Ctors,
-			IlCtors, Info2, Info3),
-	MethodsAndFieldsAndCtors = IlCtors ++ MethodsAndFields,
+	generate_class_body(Name, ClassDefn, ClassName, EntityName, Extends,
+			Interfaces, MethodsAndFieldsAndCtors, Info1, Info3),
 
 		% Only the mercury_code class needs to have the
 		% initialization instructions executed by the class
@@ -504,6 +481,46 @@
 	Decl = class(decl_flags_to_classattrs(Flags), EntityName, Extends,
 			Interfaces, MethodDecls).
 
+:- pred generate_class_body(mlds__entity_name::in, mlds__class_defn::in,
+		ilds__class_name::out, ilds__id::out, extends::out,
+		implements::out, list(classdecl)::out,
+		il_info::in, il_info::out) is det.
+
+generate_class_body(Name, ClassDefn, ClassName, EntityName, Extends, Interfaces,
+		ClassDecls, Info0, Info) :-
+	EntityName = entity_name_to_ilds_id(Name),
+	ClassDefn = class_defn(_Kind, _Imports, Inherits, Implements,
+			Ctors, Members),
+	Parent - Extends = generate_parent_and_extends(Info0 ^ il_data_rep,
+			Inherits),
+	Interfaces = implements(
+			list__map(interface_id_to_class_name, Implements)),
+
+	ClassName = class_name(Info0 ^ module_name, EntityName),
+	list__map_foldl(generate_method(ClassName, no), Members,
+			MethodsAndFields, Info0, Info1),
+	list__map_foldl(generate_method(ClassName, yes(Parent)), Ctors,
+			IlCtors, Info1, Info),
+	ClassDecls = IlCtors ++ MethodsAndFields.
+
+
+:- func generate_parent_and_extends(il_data_rep, list(mlds__class_id))
+	= pair(ilds__class_name, extends).
+
+generate_parent_and_extends(DataRep, Inherits) = Parent - Extends :-
+	( Inherits = [],
+		Parent = il_generic_class_name,
+		Extends = extends_nothing
+	; Inherits = [Parent0 | Rest],
+		( Rest = [] ->
+			Parent = mlds_type_to_ilds_class_name(DataRep, Parent0),
+			Extends = extends(Parent)
+		;
+			error(this_file ++ 
+				": multiple inheritance not supported.")
+		)
+	).
+
 class_name(Module, Name) = structured_name(Assembly, ClassName ++ [Name]) :-
 	ClassName = sym_name_to_list(mlds_module_name_to_sym_name(Module)),
 		% Any name beginning with mercury is in the standard
@@ -527,19 +544,42 @@
 :- func decl_flags_to_classattrs(mlds__decl_flags) = list(ilasm__classattr).
 
 decl_flags_to_classattrs(Flags)
-	= list__condense([Access, Finality, Abstractness]) :-
+	= list__condense([Access, decl_flags_to_classattrs_2(Flags)]) :-
 	AccessFlag = access(Flags),
 	( AccessFlag = public,
 		Access = [public]
 	; AccessFlag = protected,
 		Access = []
 	; AccessFlag = private,
-		Access = []
+		Access = [private]
 	; AccessFlag = default,
 		error("decl_flags_to_classattrs: default access flag")
 	; AccessFlag = local,
 		error("decl_flags_to_classattrs: local access flag")
-	),
+	).
+
+:- func decl_flags_to_nestedclassattrs(mlds__decl_flags) =
+		list(ilasm__classattr).
+
+decl_flags_to_nestedclassattrs(Flags)
+	= list__condense([Access, decl_flags_to_classattrs_2(Flags)]) :-
+	AccessFlag = access(Flags),
+	( AccessFlag = public,
+		Access = [nestedpublic]
+	; AccessFlag = protected,
+		Access = [nestedfamily]
+	; AccessFlag = private,
+		Access = [nestedprivate]
+	; AccessFlag = default,
+		Access = [nestedassembly]
+	; AccessFlag = local,
+		error("decl_flags_to_classattrs: local access flag")
+	).
+
+:- func decl_flags_to_classattrs_2(mlds__decl_flags) = list(ilasm__classattr).
+
+decl_flags_to_classattrs_2(Flags)
+	= list__condense([Finality, Abstractness]) :-
 	FinalityFlag = finality(Flags),
 	( FinalityFlag = overridable,
 		Finality = []
@@ -861,9 +901,12 @@
 	{ ClassDecl = ilasm__method(methodhead(Attrs, MemberName,
 			ILSignature, []), MethodContents)}.
 
-generate_method(_, _, defn(_Name, _Context, _Flags, Entity), _ClassDecl) -->
-	{ Entity = class(_ClassDefn) },
-	{ sorry(this_file, "nested classes") }.
+generate_method(_, _, defn(Name, _Context, Flags, Entity), ClassDecl) -->
+	{ Entity = class(ClassDefn) },
+	generate_class_body(Name, ClassDefn, _ClassName, EntityName,
+			Extends, Interfaces, ClassDecls),
+	{ ClassDecl = nested_class(decl_flags_to_nestedclassattrs(Flags),
+			EntityName, Extends, Interfaces, ClassDecls) }.
 
 %-----------------------------------------------------------------------------%
 

--------------------------------------------------------------------------
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