756117a013076e5ad7f80110ff0dff2379167ba2
[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(tool_name: String)
29 do
30 _tool_name = tool_name
31 super
32 register_loader(new SrcModuleLoader)
33 end
34
35 # The name of the tool
36 # Used in help messages for instance
37 readable attr _tool_name: String
38
39 meth exec_cmd_line
40 do
41 process_options
42
43 if opt_help.value then
44 print "usage: {tool_name} [options] file..."
45 option_context.usage
46 exit(0)
47 end
48
49 if option_context.rest.length == 0 then
50 print "usage: {tool_name} [options] file..."
51 option_context.usage
52 exit(1)
53 end
54
55 var rest = option_context.rest
56 var to_do = new Array[MMModule]
57 for i in [0..rest.length[ do
58 var mod = get_module_from_filename(rest[i])
59 to_do.add(mod)
60 end
61 if opt_log.value then
62 dump_context_info
63 end
64
65 if not to_do.is_empty and not opt_only_metamodel.value and not opt_only_parse.value then
66 perform_work(to_do)
67 end
68 end
69
70 meth perform_work(mods: Array[MMModule]) is abstract
71
72 meth dump_context_info
73 do
74 for mod in module_hierarchy do
75 mod.dump_module_info
76 end
77 var tab = new Array[MMModule]
78 tab.add_all(module_hierarchy)
79 var name = module_hierarchy.select_smallests(tab).join("-")
80
81 var f = new OFStream.open("{name}.full_class_hierarchy.new.dot")
82 f.write(class_hierarchy.to_dot)
83 f.close
84
85 f = new OFStream.open("{name}.module_hierarchy.new.dot")
86 f.write(module_hierarchy.to_dot)
87 f.close
88 end
89 end
90
91 redef class MMModule
92 meth dump_module_info
93 do
94 var p = filename.file_path
95 var fname = "{p}/{name}"
96 var f = new OFStream.open("{fname}.class_hierarchy.new.dot")
97 f.write(class_specialization_hierarchy.to_dot)
98 f.close
99
100 f = new OFStream.open("{fname}.properties.log")
101 for cla in local_classes do
102 cla.dump_properties(f)
103 f.write("\n")
104 end
105 f.close
106 end
107 end
108
109 redef class MMLocalClass
110 meth dump_properties(file: OStream)
111 do
112 file.write("class {self}\n")
113 for p in global_properties do
114 var lp = self[p]
115 file.write("\t{lp}{lp.signature_for(get_type)}\n")
116 end
117 file.write("end # {self}\n")
118 end
119 end