First NIT release and new clean mercurial repository
[nit.git] / src / abstracttool.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18
19 # Base module for nit tools that load, manipulate or transform NIT models
20 package abstracttool
21
22 import mmloader
23 import syntax
24
25 class AbstractCompiler
26 special ToolContext
27
28 init
29 do
30 super
31 register_loader(new SrcModuleLoader)
32 end
33
34 meth exec_cmd_line
35 do
36 process_options
37
38 if opt_help.value then
39 option_context.usage
40 exit(0)
41 end
42
43 if option_context.rest.length == 0 then
44 option_context.usage
45 exit(1)
46 end
47
48 var rest = option_context.rest
49 var to_do = new Array[MMModule]
50 for i in [0..rest.length[ do
51 var mod = get_module_from_filename(rest[i])
52 to_do.add(mod)
53 end
54 if opt_log.value then
55 dump_context_info
56 end
57
58 if not to_do.is_empty and not opt_only_metamodel.value then
59 perform_work(to_do)
60 end
61 end
62
63 meth perform_work(mods: Array[MMModule]) is abstract
64
65 meth dump_context_info
66 do
67 for mod in module_hierarchy do
68 mod.dump_module_info
69 end
70 var tab = new Array[MMModule]
71 tab.add_all(module_hierarchy)
72 var name = module_hierarchy.select_smallests(tab).join("-")
73
74 var f = new OFStream.open("{name}.full_class_hierarchy.new.dot")
75 f.write(class_hierarchy.to_dot)
76 f.close
77
78 f = new OFStream.open("{name}.module_hierarchy.new.dot")
79 f.write(module_hierarchy.to_dot)
80 f.close
81 end
82 end
83
84 redef class MMModule
85 meth dump_module_info
86 do
87 var p = filename.file_path
88 var fname = "{p}/{name}"
89 var f = new OFStream.open("{fname}.class_hierarchy.new.dot")
90 f.write(class_specialization_hierarchy.to_dot)
91 f.close
92
93 f = new OFStream.open("{fname}.properties.log")
94 for cla in local_classes do
95 cla.dump_properties(f)
96 f.write("\n")
97 end
98 f.close
99 end
100 end
101
102 redef class MMLocalClass
103 meth dump_properties(file: OStream)
104 do
105 file.write("class {self}\n")
106 for p in global_properties do
107 var lp = self[p]
108 file.write("\t{lp}{lp.signature}\n")
109 end
110 file.write("end # {self}\n")
111 end
112 end