version: v0.6.1-git
[nit.git] / src / metrics / metrics_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Helpers for various statistics tools.
18 module metrics_base
19
20 import modelbuilder
21 import csv
22 import counter
23
24 redef class ToolContext
25
26 # --all
27 var opt_all = new OptionBool("Compute all metrics", "--all")
28
29 # --inheritance
30 var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
31 # --genericity
32 var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
33 # --self
34 var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
35 # --nullables
36 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
37 # --static-types
38 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
39 # --tables
40 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
41 # --rta
42 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
43 # --generate-csv
44 var opt_generate_csv = new OptionBool("Generate CVS format metrics", "--generate-csv")
45 # --generate_hyperdoc
46 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
47 # --poset
48 var opt_poset = new OptionBool("Complete metrics on posets", "--poset")
49
50 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
51 var output_dir: String = "."
52
53 redef init
54 do
55 super
56 self.option_context.add_option(opt_all)
57 self.option_context.add_option(opt_inheritance)
58 self.option_context.add_option(opt_refinement)
59 self.option_context.add_option(opt_self)
60 self.option_context.add_option(opt_nullables)
61 self.option_context.add_option(opt_static_types)
62 self.option_context.add_option(opt_tables)
63 self.option_context.add_option(opt_rta)
64 self.option_context.add_option(opt_generate_csv)
65 self.option_context.add_option(opt_generate_hyperdoc)
66 self.option_context.add_option(opt_poset)
67 self.option_context.add_option(opt_dir)
68 end
69
70 redef fun process_options
71 do
72 super
73 var val = self.opt_dir.value
74 if val != null then
75 val = val.simplify_path
76 val.mkdir
77 self.output_dir = val
78 end
79 end
80 end
81
82 redef class Model
83
84 # List of modules in std lib
85 # FIXME this is quite ugly, find a dynamic way...
86 fun std_modules: Set[String] do
87 if self.std_modules_cache == null then
88 self.std_modules_cache = new HashSet[String]
89 self.std_modules_cache.add("collection")
90 self.std_modules_cache.add("abstract_collection")
91 self.std_modules_cache.add("array")
92 self.std_modules_cache.add("hash_collection")
93 self.std_modules_cache.add("list")
94 self.std_modules_cache.add("range")
95 self.std_modules_cache.add("sorter")
96 self.std_modules_cache.add("environ")
97 self.std_modules_cache.add("exec")
98 self.std_modules_cache.add("file")
99 self.std_modules_cache.add("gc")
100 self.std_modules_cache.add("hash")
101 self.std_modules_cache.add("kernel")
102 self.std_modules_cache.add("math")
103 self.std_modules_cache.add("standard")
104 self.std_modules_cache.add("stream")
105 self.std_modules_cache.add("string")
106 self.std_modules_cache.add("string_search")
107 self.std_modules_cache.add("time")
108 end
109 return self.std_modules_cache.as(not null)
110 end
111 private var std_modules_cache: nullable Set[String]
112 end
113
114 redef class MClass
115 fun is_class: Bool do
116 return self.kind == concrete_kind or self.kind == abstract_kind
117 end
118
119 fun is_interface: Bool do
120 return self.kind == interface_kind
121 end
122
123 fun is_enum: Bool do
124 return self.kind == enum_kind
125 end
126
127 fun is_abstract: Bool do
128 return self.kind == abstract_kind
129 end
130
131 fun is_user_defined: Bool do
132 return self.intro_mmodule.is_user_defined
133 end
134 end
135
136 redef class MModule
137 fun is_user_defined: Bool do
138 return not self.model.std_modules.has(self.name)
139 end
140 end