Class: Orbacle::GlobalTree

Inherits:
Object
  • Object
show all
Defined in:
lib/orbacle/global_tree.rb

Defined Under Namespace

Classes: ArgumentsTree, Constant, Klass, Lambda, Method, Mod

Instance Method Summary collapse

Constructor Details

#initialize(id_generator) ⇒ GlobalTree

Returns a new instance of GlobalTree.



91
92
93
94
95
96
97
98
99
100
# File 'lib/orbacle/global_tree.rb', line 91

def initialize(id_generator)
  @id_generator = id_generator
  @constants = ConstantsTree.new
  @classes_by_id = {}
  @modules_by_id = {}
  @methods_by_class_id = Hash.new {|h,k| h[k] = Hash.new {|h2, k2| h2[k2] = [] } }
  @methods_by_id = {}
  @lambdas_by_id = {}
  @type_mapping = Hash.new(BottomType.new)
end

Instance Method Details

#add_class(parent_ref) ⇒ Object

Definitions



179
180
181
182
183
# File 'lib/orbacle/global_tree.rb', line 179

def add_class(parent_ref)
  klass = Klass.new(id_generator.call, parent_ref)
  @classes_by_id[klass.id] = klass
  klass
end

#add_constant(constant) ⇒ Object

Constants



216
217
218
219
# File 'lib/orbacle/global_tree.rb', line 216

def add_constant(constant)
  @constants.add_element(constant.scope, constant.name, constant)
  constant
end

#add_lambda(args) ⇒ Object

Lambdas



231
232
233
234
235
# File 'lib/orbacle/global_tree.rb', line 231

def add_lambda(args)
  lamba = Lambda.new(id_generator.call, args)
  @lambdas_by_id[lamba.id] = lamba
  lamba
end

#add_method(id, place_of_definition_id, name, location, visibility, args) ⇒ Object

Methods



104
105
106
107
108
109
# File 'lib/orbacle/global_tree.rb', line 104

def add_method(id, place_of_definition_id, name, location, visibility, args)
  metod = Method.new(id, place_of_definition_id, name, location, visibility, args)
  @methods_by_class_id[metod.place_of_definition_id][metod.name] << metod
  @methods_by_id[metod.id] = metod
  metod
end

#add_moduleObject



185
186
187
188
189
# File 'lib/orbacle/global_tree.rb', line 185

def add_module
  mod = Mod.new(id_generator.call)
  @modules_by_id[mod.id] = mod
  mod
end

#change_method_visibility(klass_id, name, new_visibility) ⇒ Object



161
162
163
164
165
# File 'lib/orbacle/global_tree.rb', line 161

def change_method_visibility(klass_id, name, new_visibility)
  @methods_by_class_id[klass_id][name].each do |m|
    m.visibility = new_visibility
  end
end

#find_class_by_name(full_name) ⇒ Object



261
262
263
264
265
# File 'lib/orbacle/global_tree.rb', line 261

def find_class_by_name(full_name)
  const = find_constant_by_name(full_name)
  return nil if const.nil?
  get_class(const.definition_id)
end

#find_class_method_from_class_name(class_name, method_name) ⇒ Object



142
143
144
# File 'lib/orbacle/global_tree.rb', line 142

def find_class_method_from_class_name(class_name, method_name)
  get_class_methods_from_class_name(class_name, method_name).first
end

#find_constant_by_name(full_name) ⇒ Object



273
274
275
# File 'lib/orbacle/global_tree.rb', line 273

def find_constant_by_name(full_name)
  @constants.find_by_const_name(ConstName.from_string(full_name))
end

#find_constant_for_definition(definition_id) ⇒ Object



277
278
279
280
281
# File 'lib/orbacle/global_tree.rb', line 277

def find_constant_for_definition(definition_id)
  @constants.find do |constant|
    constant.definition_id.equal?(definition_id)
  end
end

#find_deep_class_method_from_class_name(class_name, method_name) ⇒ Object



301
302
303
# File 'lib/orbacle/global_tree.rb', line 301

def find_deep_class_method_from_class_name(class_name, method_name)
  get_deep_class_methods_from_class_name(class_name, method_name).first
end

#find_deep_instance_method_from_class_name(class_name, method_name) ⇒ Object



283
284
285
# File 'lib/orbacle/global_tree.rb', line 283

def find_deep_instance_method_from_class_name(class_name, method_name)
  get_deep_instance_methods_from_class_name(class_name, method_name).first
end

#find_instance_method_from_class_id(class_id, method_name) ⇒ Object



115
116
117
# File 'lib/orbacle/global_tree.rb', line 115

def find_instance_method_from_class_id(class_id, method_name)
  get_instance_methods_from_class_id(class_id, method_name).first
end

#find_instance_method_from_class_name(class_name, method_name) ⇒ Object



111
112
113
# File 'lib/orbacle/global_tree.rb', line 111

def find_instance_method_from_class_name(class_name, method_name)
  get_instance_methods_from_class_name(class_name, method_name).first
end

#find_method_including_position(file_path, position) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/orbacle/global_tree.rb', line 167

def find_method_including_position(file_path, position)
  @methods_by_id
    .values
    .select {|m| m.location &&
             m.location.uri.eql?(file_path) &&
             m.location.position_range.include_position?(position) }
    .sort_by {|m| m.location.span }
    .first
end

#find_module_by_name(full_name) ⇒ Object



267
268
269
270
271
# File 'lib/orbacle/global_tree.rb', line 267

def find_module_by_name(full_name)
  const = find_constant_by_name(full_name)
  return nil if const.nil?
  get_module(const.definition_id)
end

#find_super_method(method_id) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/orbacle/global_tree.rb', line 152

def find_super_method(method_id)
  analyzed_method = @methods_by_id.fetch(method_id)
  klass_of_this_method = get_class(analyzed_method.place_of_definition_id)
  return nil if klass_of_this_method.nil? || klass_of_this_method.parent_ref.nil?
  parent_klass = solve_reference(klass_of_this_method.parent_ref)
  return nil if parent_klass.nil?
  find_instance_method_from_class_name(parent_klass.full_name, analyzed_method.name)
end

#get_all_instance_methods_from_class_name(class_name) ⇒ Object



136
137
138
139
140
# File 'lib/orbacle/global_tree.rb', line 136

def get_all_instance_methods_from_class_name(class_name)
  klass = find_class_by_name(class_name)
  return [] if klass.nil?
  @methods_by_class_id[klass.id].values.flatten
end

#get_class(class_id) ⇒ Object



191
192
193
# File 'lib/orbacle/global_tree.rb', line 191

def get_class(class_id)
  @classes_by_id[class_id]
end

#get_class_methods_from_class_name(class_name, method_name) ⇒ Object



129
130
131
132
133
134
# File 'lib/orbacle/global_tree.rb', line 129

def get_class_methods_from_class_name(class_name, method_name)
  klass = find_class_by_name(class_name)
  return [] if klass.nil?
  eigenclass = get_eigenclass_of_definition(klass.id)
  get_instance_methods_from_class_id(eigenclass.id, method_name)
end

#get_deep_class_methods_from_class_name(class_name, method_name) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/orbacle/global_tree.rb', line 305

def get_deep_class_methods_from_class_name(class_name, method_name)
  found_methods = get_class_methods_from_class_name(class_name, method_name)
  if found_methods.empty?
    parent_name = get_parent_of(class_name)
    if parent_name
      get_deep_class_methods_from_class_name(parent_name, method_name)
    else
      []
    end
  else
    found_methods
  end
end

#get_deep_instance_methods_from_class_name(class_name, method_name) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/orbacle/global_tree.rb', line 287

def get_deep_instance_methods_from_class_name(class_name, method_name)
  found_methods = get_instance_methods_from_class_name(class_name, method_name)
  if found_methods.empty?
    parent_name = get_parent_of(class_name)
    if parent_name
      get_deep_instance_methods_from_class_name(parent_name, method_name)
    else
      []
    end
  else
    found_methods
  end
end

#get_definition(definition_id) ⇒ Object



199
200
201
# File 'lib/orbacle/global_tree.rb', line 199

def get_definition(definition_id)
  get_class(definition_id) || get_module(definition_id)
end

#get_eigenclass_of_definition(definition_id) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/orbacle/global_tree.rb', line 203

def get_eigenclass_of_definition(definition_id)
  definition = get_definition(definition_id)
  if definition.eigenclass_id
    get_class(definition.eigenclass_id)
  else
    eigenclass = add_class(nil)
    definition.eigenclass_id = eigenclass.id
    eigenclass
  end
end

#get_instance_methods_from_class_id(class_id, method_name) ⇒ Object



119
120
121
# File 'lib/orbacle/global_tree.rb', line 119

def get_instance_methods_from_class_id(class_id, method_name)
  @methods_by_class_id[class_id][method_name]
end

#get_instance_methods_from_class_name(class_name, method_name) ⇒ Object



123
124
125
126
127
# File 'lib/orbacle/global_tree.rb', line 123

def get_instance_methods_from_class_name(class_name, method_name)
  klass = find_class_by_name(class_name)
  return [] if klass.nil?
  get_instance_methods_from_class_id(klass.id, method_name)
end

#get_lambda(lambda_id) ⇒ Object



237
238
239
# File 'lib/orbacle/global_tree.rb', line 237

def get_lambda(lambda_id)
  @lambdas_by_id[lambda_id]
end

#get_methods(method_name) ⇒ Object



146
147
148
149
150
# File 'lib/orbacle/global_tree.rb', line 146

def get_methods(method_name)
  @methods_by_id.values.select do |m|
    m.name.eql?(method_name)
  end
end

#get_module(module_id) ⇒ Object



195
196
197
# File 'lib/orbacle/global_tree.rb', line 195

def get_module(module_id)
  @modules_by_id[module_id]
end

#get_parent_of(class_name) ⇒ Object

Other



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/orbacle/global_tree.rb', line 243

def get_parent_of(class_name)
  return nil if class_name.eql?("Object")

  const = find_constant_by_name(class_name)
  return "Object" if const.nil?

  klass = get_class(const.definition_id)
  return "Object" if klass.nil?

  return "Object" if klass.parent_ref.nil?
  parent_const = solve_reference(klass.parent_ref)
  if parent_const
    parent_const.full_name
  else
    klass.parent_ref.relative_name
  end
end

#set_type_of(node, new_type) ⇒ Object



325
326
327
# File 'lib/orbacle/global_tree.rb', line 325

def set_type_of(node, new_type)
  @type_mapping[node] = new_type
end

#solve_reference(const_ref) ⇒ Object



221
222
223
# File 'lib/orbacle/global_tree.rb', line 221

def solve_reference(const_ref)
  @constants.find_by_const_ref(const_ref)
end

#solve_reference2(const_ref) ⇒ Object



225
226
227
# File 'lib/orbacle/global_tree.rb', line 225

def solve_reference2(const_ref)
  @constants.select_by_const_ref(const_ref)
end

#type_of(node) ⇒ Object

Types



321
322
323
# File 'lib/orbacle/global_tree.rb', line 321

def type_of(node)
  @type_mapping[node]
end