[m-rev.] for review: automatic_include declaration

Peter Ross peter.ross at miscrit.be
Tue Nov 20 01:48:50 AEDT 2001


On Sat, Nov 17, 2001 at 02:17:26AM +1100, Simon Taylor wrote:
> On 16-Nov-2001, Peter Ross <peter.ross at miscrit.be> wrote:
> > Estimated hours taken: 4
> > Branches: main
> > 
> > Add the `:- automatic_include' declaration.
> >
> > This declaration allows one to automatically import a sub-module when
> > the parent module is imported.
> 
> I think `:- automatic_import' would be a better name.
> 
> > compiler/modules.m:
> >     When reading in short and long interfaces recursively read in any
> >     modules specified by an `:- automatic_include' declaration.
> >     Check that any `:- automatic_include' declaration in the interface
> >     refers to an existing visible sub-module.
> 
> You haven't fixed `mmc --generate-dependencies' to handle the extra
> dependencies on `.int' files.
> 

How does this look?

I will change the name to auto_import, but I just wanted to do the
interdiff first so as to avoid a lot of spurious changes.

I also updated the test case so that it checks for two levels of
auto_import-ing.

diff -u compiler/modules.m compiler/modules.m
--- compiler/modules.m
+++ compiler/modules.m
@@ -271,6 +271,10 @@
 				% The list of its public children,
 				% i.e. child modules that it includes
 				% in the interface section.
+		auto_imports	:: list(module_name),
+				% The list of modules that are
+				% specifed by an auto_import in the
+				% interface.
 		fact_table_deps :: list(string),  
 				% The list of filenames for fact tables
 				% in this module.
@@ -1385,6 +1389,7 @@
 	{ get_fact_table_dependencies(Items0, FactDeps) },
 	{ get_interface(Items0, InterfaceItems) },
 	{ get_children(InterfaceItems, PublicChildren) },
+	{ get_automatic_includes(InterfaceItems, AutoImports) },
 	{ MaybeTimestamp = yes(Timestamp) ->
 		MaybeTimestamps = yes(map__det_insert(map__init, ModuleName,
 			module_timestamp(".m", Timestamp, may_be_unqualified)))
@@ -1393,7 +1398,8 @@
 
 	},
 	{ init_module_imports(SourceFileName, ModuleName, Items0,
-		PublicChildren, FactDeps, MaybeTimestamps, Module0) },
+		PublicChildren, AutoImports, FactDeps,
+		MaybeTimestamps, Module0) },
 
 		% If this module has any seperately-compiled sub-modules,
 		% then we need to make everything in this module
@@ -1483,7 +1489,7 @@
 		% Construct the initial module import structure,
 		% and append a `:- imported' decl to the items.
 		%
-	{ init_module_imports(SourceFileName, ModuleName, Items0, [], [],
+	{ init_module_imports(SourceFileName, ModuleName, Items0, [], [], [],
 		no, Module0) },
 	{ append_pseudo_decl(Module0, imported(interface), Module1) },
 
@@ -1554,15 +1560,15 @@
 	).
 
 :- pred init_module_imports(file_name, module_name, item_list,
-			list(module_name), list(string),
+			list(module_name), list(module_name), list(string),
 			maybe(module_timestamps), module_imports).
-:- mode init_module_imports(in, in, in, in, in, in, out) is det.
+:- mode init_module_imports(in, in, in, in, in, in, in, out) is det.
 
 init_module_imports(SourceFileName, ModuleName, Items, PublicChildren,
-			FactDeps, MaybeTimestamps, Module) :-
+			AutoImports, FactDeps, MaybeTimestamps, Module) :-
 	Module = module_imports(SourceFileName, ModuleName, [], [], [], [],
-		PublicChildren, FactDeps, unknown, [], Items, no_module_errors,
-		MaybeTimestamps).
+		PublicChildren, AutoImports, FactDeps, unknown, [], Items,
+		no_module_errors, MaybeTimestamps).
 
 module_imports_get_source_file_name(Module, Module ^ source_file_name).
 module_imports_get_module_name(Module, Module ^ module_name).
@@ -1790,8 +1796,8 @@
 
 write_dependency_file(Module, AllDepsSet, MaybeTransOptDeps) -->
 	{ Module = module_imports(SourceFileName, ModuleName, ParentDeps,
-			IntDeps, ImplDeps, IndirectDeps, _InclDeps, FactDeps0,
-			ContainsForeignCode, ForeignImports0,
+			IntDeps, ImplDeps, IndirectDeps, _InclDeps, _AutoImps,
+			FactDeps0, ContainsForeignCode, ForeignImports0,
 			Items, _Error, _Timestamps) },
 	globals__io_lookup_bool_option(verbose, Verbose),
 	{ module_name_to_make_var_name(ModuleName, MakeVarName) },
@@ -2043,35 +2049,19 @@
 				"endif"
 		]),
 
-		% The .date and .date0 files depend on the .int0 files
-		% for the parent modules, and the .int3 files for the
-		% directly and indirectly imported modules.
-		%
-		% For nested sub-modules, the `.date' files for the
-		% parent modules also depend on the same things as the
-		% `.date' files for this module, since all the `.date'
-		% files will get produced by a single mmc command.
-		% XXX The same is true for the `.date0' files, but
-		% including those dependencies here might result in
-		% cyclic dependencies(?).
-
 		module_name_to_file_name(ModuleName, ".date", no,
 						DateFileName),
 		module_name_to_file_name(ModuleName, ".date0", no,
 						Date0FileName),
 		io__write_strings(DepStream, [
 				"\n\n", DateFileName, " ",
-				Date0FileName
-		]),
-		write_dependencies_list(ParentDeps, ".date", DepStream),
-		io__write_strings(DepStream, [
-				" : ",
+				Date0FileName, " : ",
 				SourceFileName
 		]),
 		write_dependencies_list(ParentDeps, ".int0", DepStream),
 		write_dependencies_list(LongDeps, ".int3", DepStream),
 		write_dependencies_list(ShortDeps, ".int3", DepStream),
-
+			
 		module_name_to_file_name(ModuleName, ".dir", no, DirFileName),
 		module_name_to_split_c_file_name(ModuleName, 0, ".$O",
 			SplitCObj0FileName),
@@ -2915,7 +2905,8 @@
 		ParentDeps = ModuleImports ^ parent_deps,
 		relation__add_element(IntRel0, ModuleName, IntModuleKey,
 			IntRel1),
-		add_int_deps(IntModuleKey, ModuleImports, IntRel1, IntRel2),
+		add_int_deps(DepsMap, IntModuleKey, ModuleImports,
+				IntRel1, IntRel2),
 		list__foldl(add_parent_impl_deps(DepsMap, IntModuleKey),
 				ParentDeps, IntRel2, IntRel3),
 
@@ -2930,8 +2921,9 @@
 		% only by its parents.
 		%
 		relation__add_element(ImplRel0, ModuleName, ImplModuleKey,
-			ImplRel1),
-		add_impl_deps(ImplModuleKey, ModuleImports, ImplRel1, ImplRel2),
+				ImplRel1),
+		add_impl_deps(DepsMap, ImplModuleKey, ModuleImports,
+				ImplRel1, ImplRel2),
 		list__foldl(add_parent_impl_deps(DepsMap, ImplModuleKey),
 				ParentDeps, ImplRel2, ImplRel3)
 	;
@@ -2943,28 +2935,32 @@
 
 % add interface dependencies to the interface deps relation
 %
-:- pred add_int_deps(relation_key, module_imports, deps_rel, deps_rel).
-:- mode add_int_deps(in, in, in, out) is det.
+:- pred add_int_deps(deps_map::in, relation_key::in, module_imports::in,
+		deps_rel::in, deps_rel::out) is det.
 
-add_int_deps(ModuleKey, ModuleImports, Rel0, Rel) :-
+add_int_deps(DepsMap, ModuleKey, ModuleImports, Rel0, Rel) :-
 	AddDep = add_dep(ModuleKey),
 	list__foldl(AddDep, ModuleImports ^ parent_deps, Rel0, Rel1),
 	list__foldl(AddDep, ModuleImports ^ int_deps, Rel1, Rel2),
-	list__foldl(AddDep, ModuleImports ^ public_children, Rel2, Rel).
+	list__foldl(AddDep, ModuleImports ^ public_children, Rel2, Rel3),
+	list__foldl(add_auto_import_deps(DepsMap, ModuleKey),
+			ModuleImports ^ int_deps, Rel3, Rel).
 
 % add direct implementation dependencies for a module to the
 % impl. deps relation
 %
-:- pred add_impl_deps(relation_key, module_imports, deps_rel, deps_rel).
-:- mode add_impl_deps(in, in, in, out) is det.
+:- pred add_impl_deps(deps_map::in, relation_key::in, module_imports::in,
+		deps_rel::in, deps_rel::out) is det.
 
-add_impl_deps(ModuleKey, ModuleImports, Rel0, Rel) :-
+add_impl_deps(DepsMap, ModuleKey, ModuleImports, Rel0, Rel) :-
 	% the implementation dependencies are a superset of the
 	% interface dependencies, so first we add the interface deps
-	add_int_deps(ModuleKey, ModuleImports, Rel0, Rel1),
+	add_int_deps(DepsMap, ModuleKey, ModuleImports, Rel0, Rel1),
 	% then we add the impl deps
 	module_imports_get_impl_deps(ModuleImports, ImplDeps),
-	list__foldl(add_dep(ModuleKey), ImplDeps, Rel1, Rel).
+	list__foldl(add_auto_import_deps(DepsMap, ModuleKey),
+			ImplDeps, Rel1, Rel2),
+	list__foldl(add_dep(ModuleKey), ImplDeps, Rel2, Rel).
 
 % add parent implementation dependencies for the given Parent module
 % to the impl. deps relation values for the given ModuleKey.
@@ -2975,7 +2971,20 @@
 
 add_parent_impl_deps(DepsMap, ModuleKey, Parent, Rel0, Rel) :-
 	map__lookup(DepsMap, Parent, deps(_, ParentModuleImports)),
-	add_impl_deps(ModuleKey, ParentModuleImports, Rel0, Rel).
+	add_impl_deps(DepsMap, ModuleKey, ParentModuleImports, Rel0, Rel).
+
+% add dependencies between the module specified by the module key and
+% and every module automatically imported by an auto_import declaration
+% starting from module_name.
+:- pred add_auto_import_deps(deps_map::in, relation_key::in, module_name::in,
+		deps_rel::in, deps_rel::out) is det.
+
+add_auto_import_deps(DepsMap, ModuleKey, SubModule, Rel0, Rel) :-
+	map__lookup(DepsMap, SubModule, deps(_, SubModuleImports)),
+	AutoImps = SubModuleImports ^ auto_imports,
+	list__foldl(add_dep(ModuleKey), AutoImps, Rel0, Rel1),
+	list__foldl(add_auto_import_deps(DepsMap, ModuleKey),
+			AutoImps, Rel1, Rel).
 
 % add a single dependency to a relation
 %
@@ -4391,6 +4400,7 @@
 		InterfaceImportDeps, InterfaceUseDeps),
 	list__append(InterfaceImportDeps, InterfaceUseDeps, 
 		InterfaceDeps),
+	get_automatic_includes(InterfaceItems, AutoImps),
 
 	% we don't fill in the indirect dependencies yet
 	IndirectDeps = [],
@@ -4412,7 +4422,7 @@
 
 	ModuleImports = module_imports(FileName, ModuleName, ParentDeps,
 		InterfaceDeps, ImplementationDeps, IndirectDeps, IncludeDeps,
-		FactTableDeps, ContainsForeignCode, ForeignImports,
+		AutoImps, FactTableDeps, ContainsForeignCode, ForeignImports,
 		[], Error, no).
 
 %-----------------------------------------------------------------------------%
@@ -4703,7 +4713,6 @@
 				ModItems0)
 		),
 		{ get_dependencies(Items, IndirectImports1, IndirectUses1) },
-		{ get_automatic_includes(Items, Includes) },
 		{ list__append(IndirectImports0, IndirectImports1,
 			IndirectImports2) },
 		{ list__append(IndirectImports2, IndirectUses1,
@@ -4720,6 +4729,7 @@
 				ModItems0)
 		),
 		{ get_dependencies(Items, IndirectImports1, IndirectUses1) },
+		{ get_automatic_includes(Items, Includes) },
 		{ list__append(IndirectImports0, IndirectImports1,
 			IndirectImports2) },
 		{ list__append(IndirectImports2, IndirectUses1,
diff -u tests/hard_coded/sub-modules/auto_parent.m tests/invalid/automatic_include.m
--- tests/hard_coded/sub-modules/auto_parent.m
+++ tests/invalid/automatic_include.m
@@ -1,32 +1,15 @@
-% Test the automatic_include statement.
-% Used by automatic_include.m
-
-:- module auto_parent.
+:- module (automatic_include).
 
 :- interface.
 
-:- import_module io.
-
-:- include_module separate.
-
-:- automatic_include auto_parent__nested, auto_parent__separate.
+:- type t.
 
-:- pred hello(io__state::di, io__state::uo) is det.
-
-  :- module auto_parent__nested.
-  :- interface.
-  :- pred hello(io__state::di, io__state::uo) is det.
-  :- end_module auto_parent__nested.
+:- automatic_include automatic_include__child.	% not visible
+:- automatic_include list.			% doesn't exist
 
 :- implementation.
 
-hello -->
-	io__write_string("auto_parent: hello\n").
-
-:- module auto_parent__nested.
-:- implementation.
-
-hello -->
-	io__write_string("auto_parent__nested: hello\n").
-
-:- end_module auto_parent__nested.
+:- module automatic_include__child.
+:- interface.
+:- type t.
+:- end_module automatic_include__child.
diff -u tests/hard_coded/sub-modules/auto_parent.separate.m tests/invalid/automatic_include.m
--- tests/hard_coded/sub-modules/auto_parent.separate.m
+++ tests/invalid/automatic_include.m
@@ -1,14 +1,15 @@
-% Used by automatic_include.m
-
-:- module auto_parent__separate.
+:- module (automatic_include).
 
 :- interface.
 
-% The parent module autos io.
+:- type t.
 
-:- pred hello(io__state::di, io__state::uo) is det.
+:- automatic_include automatic_include__child.	% not visible
+:- automatic_include list.			% doesn't exist
 
 :- implementation.
 
-hello -->
-	io__write_string("auto_parent__separate: hello\n").
+:- module automatic_include__child.
+:- interface.
+:- type t.
+:- end_module automatic_include__child.
diff -u tests/hard_coded/sub-modules/automatic_include.exp tests/invalid/automatic_include.m
--- tests/hard_coded/sub-modules/automatic_include.exp
+++ tests/invalid/automatic_include.m
@@ -1,2 +1,15 @@
-auto_parent__nested: hello
-auto_parent__separate: hello
+:- module (automatic_include).
+
+:- interface.
+
+:- type t.
+
+:- automatic_include automatic_include__child.	% not visible
+:- automatic_include list.			% doesn't exist
+
+:- implementation.
+
+:- module automatic_include__child.
+:- interface.
+:- type t.
+:- end_module automatic_include__child.
diff -u tests/hard_coded/sub-modules/automatic_include.m tests/invalid/automatic_include.m
--- tests/hard_coded/sub-modules/automatic_include.m
+++ tests/invalid/automatic_include.m
@@ -1,18 +1,15 @@
-% Test the when importing the module auto_parent that we also get the
-% sub-modules nested and separate automatically imported.
-
 :- module (automatic_include).
 
 :- interface.
 
-:- import_module io.
+:- type t.
 
-:- pred main(io__state::di, io__state::uo) is det.
+:- automatic_include automatic_include__child.	% not visible
+:- automatic_include list.			% doesn't exist
 
 :- implementation.
 
-:- use_module auto_parent.
-
-main -->
-	auto_parent__nested__hello,
-	auto_parent__separate__hello.
+:- module automatic_include__child.
+:- interface.
+:- type t.
+:- end_module automatic_include__child.
diff -u tests/invalid/automatic_include.err_exp tests/invalid/automatic_include.m
--- tests/invalid/automatic_include.err_exp
+++ tests/invalid/automatic_include.m
@@ -1,6 +1,15 @@
-automatic_include.m:007: error: The automatic_include declaration refers to the
-automatic_include.m:007:   sub-module `automatic_include:child' which doesn't
-automatic_include.m:007:   exist or is not visible externally.
-automatic_include.m:008: error: The automatic_include declaration refers to the
-automatic_include.m:008:   sub-module `automatic_include:list' which doesn't
-automatic_include.m:008:   exist or is not visible externally.
+:- module (automatic_include).
+
+:- interface.
+
+:- type t.
+
+:- automatic_include automatic_include__child.	% not visible
+:- automatic_include list.			% doesn't exist
+
+:- implementation.
+
+:- module automatic_include__child.
+:- interface.
+:- type t.
+:- end_module automatic_include__child.
only in patch2:
--- compiler/mercury_compile.m	9 Nov 2001 18:20:48 -0000	1.224
+++ compiler/mercury_compile.m	19 Nov 2001 14:35:57 -0000
@@ -1299,7 +1299,8 @@
 			% imported (or used), and for all ancestor modules.
 			{ Imports0 = module_imports(_File, _Module, Ancestors,
 				InterfaceImports, ImplementationImports,
-				_IndirectImports, _PublicChildren, _FactDeps,
+				_IndirectImports, _PublicChildren, 
+				_AutoImports, _FactDeps,
 				_ForeignCode, _ForeignImports, _Items,
 				_Error, _Timestamps) },
 			{ list__condense([Ancestors, InterfaceImports,
only in patch2:
--- compiler/intermod.m	6 Nov 2001 15:20:44 -0000	1.109
+++ compiler/intermod.m	19 Nov 2001 14:35:52 -0000
@@ -2046,7 +2046,8 @@
 		% Read in the .opt files for imported and ancestor modules.
 		%
 	{ Module0 = module_imports(_, ModuleName, Ancestors0, InterfaceDeps0,
-				ImplementationDeps0, _, _, _, _, _, _, _, _) },
+				ImplementationDeps0,
+				_, _, _, _, _, _, _, _, _) },
 	{ list__condense([Ancestors0, InterfaceDeps0, ImplementationDeps0],
 		OptFiles) },
 	read_optimization_interfaces(OptFiles, [], OptItems, no, OptError),
--------------------------------------------------------------------------
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