metamodel: rename 'universal' to 'enum'
[nit.git] / src / analysis / reachable_as_init.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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 # Module containing all bases for the reachable from init method analysis
18 package reachable_as_init
19
20 import icode
21 import program
22
23 redef class Program
24 # This attribute is the ReachableAsInitAnalysis results
25 readable writable var _rai: nullable ReachableAsInitAnalysis = null
26
27 # This method will create a file and output all inits reachable as init in it
28 fun dump_reachable_as_init_methods(directory_name: String) do
29 var f = new OFStream.open("{directory_name}/{main_module.name}.reachable_methods_as_init.log")
30 with_each_live_local_classes !action(c) do
31 for g in c.global_properties do
32 var p = c[g]
33 if not p.global.is_init_for(c) then continue
34 assert p isa MMMethod
35 if rai.is_method_reachable_as_init(p, c) then
36 f.write("{p.full_name}\n")
37 end
38 end
39 end
40 f.close
41 end
42 end
43
44 # Subclasses of this class would represent an analysis that produces
45 # at least a way of knowing if an initializer is called at least once
46 # as an initializer (and not as part of another initializer)
47 # in a specific class
48 class ReachableAsInitAnalysis
49 fun is_method_reachable_as_init(method: MMMethod, c: MMLocalClass): Bool is abstract
50 end
51
52 # Default behavior is to say that all initializers are called as init
53 class DefaultReachableAsInitAnalysis
54 super ReachableAsInitAnalysis
55 redef fun is_method_reachable_as_init(method: MMMethod, c: MMLocalClass): Bool do
56 if method.global.is_init and method.global.is_init_for(c) then return true
57 return false
58 end
59
60 init do end
61 end