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