Class: BasicObject::TypeRegistry

Inherits:
Object
  • Object
show all
Extended by:
TypedRb::Runtime::Normalization, TypedRb::Runtime::Normalization::Validations
Defined in:
lib/typed/runtime/type_registry.rb

Class Method Summary collapse

Methods included from TypedRb::Runtime::Normalization::Validations

validate_function_signature, validate_method, validate_signature, validate_signatures

Methods included from TypedRb::Runtime::Normalization

build_class_methods_info, build_generic_singleton_object, build_generic_super_type, check_generic_super_type, check_super_type_annotations, collect_methods, compute_parameters_info, find_methods, find_methods_for_class, find_methods_for_top_level_object, normalize_generic_types!, normalize_method_signatures, normalize_methods!, normalize_signature!, object_key, parse_class, parse_object_key, valid_super_type?

Class Method Details

.clearObject



17
18
19
20
21
# File 'lib/typed/runtime/type_registry.rb', line 17

def clear
  generic_types_registry.clear
  registry.clear
  clear_parsing_registries
end

.clear_parsing_registriesObject



11
12
13
14
# File 'lib/typed/runtime/type_registry.rb', line 11

def clear_parsing_registries
  generic_types_parser_registry.clear
  parser_registry.clear
end

.existential_types_registry=(registry) ⇒ Object



141
142
143
# File 'lib/typed/runtime/type_registry.rb', line 141

def existential_types_registry=(registry)
  @existential_types_registry = registry
end

.find(kind, klass, message) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/typed/runtime/type_registry.rb', line 98

def find(kind, klass, message)
  class_data = registry[[kind, klass]]
  if class_data
    # TODO: What should we when the class is in the registry but the method is missing?
    # The class has been typed but only partially?
    # Dynamic invocation or error?
    # Maybe an additional @dynamic annotation can be added to distinguish the desired outcome.
    # Preferred outcome might be nil to catch errors in unification, safer assumption.
    # class_data[message.to_s] || nil # ::TypedRb::Types::TyDynamicFunction.new(klass, message)
    class_data[message.to_s] || [::TypedRb::Types::TyDynamicFunction.new(klass, message)]
  elsif kind == :instance_variable || kind == :class_variable
    nil
  else
    # if registered?(klass)
    #   nil
    # else
    result = Array.('TypedRb::Types::TyFunction').new
    result << ::TypedRb::Types::TyDynamicFunction.new(klass, message)
    result
    # end
  end
end

.find_existential_type(type) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/typed/runtime/type_registry.rb', line 49

def find_existential_type(type)
  existential_type = existential_types_registry[type]
  if existential_type.nil?
    generic_existential_type = generic_types_registry[type]
    existential_type = if generic_existential_type
                         TypedRb::Types::TyGenericExistentialType.new(type, generic_existential_type.type_vars)
                       else
                         TypedRb::Types::TyExistentialType.new(type)
                       end
    @existential_types_registry[type] = existential_type
  end
  existential_type
end

.find_generic_type(type) ⇒ Object



64
65
66
# File 'lib/typed/runtime/type_registry.rb', line 64

def find_generic_type(type)
  @generic_types_registry[type]
end

.generic_types_registry=(registry) ⇒ Object



137
138
139
# File 'lib/typed/runtime/type_registry.rb', line 137

def generic_types_registry=(registry)
  @generic_types_registry = registry
end

.normalize_types!Object



128
129
130
131
# File 'lib/typed/runtime/type_registry.rb', line 128

def normalize_types!
  normalize_generic_types!
  normalize_methods!
end

.register_generic_type_information(generic_type_information, generic_super_types_information) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typed/runtime/type_registry.rb', line 31

def register_generic_type_information(generic_type_information, generic_super_types_information)
  if generic_type_information.is_a?(String)
    # concrete type with a generic super type
    generic_type_information = {
        :type => generic_type_information,
        :parameters => [],
        :kind => :generic_type
    }
  end
  generic_type_information[:super_type] = generic_super_types_information
  if generic_types_parser_registry[generic_type_information[:type]]
    super_type = (generic_types_parser_registry[generic_type_information[:type]][:super_type] || [])
    generic_types_parser_registry[generic_type_information[:type]][:super_type]= super_type.concat(generic_type_information[:super_type])
  else
    generic_types_parser_registry[generic_type_information[:type]] = generic_type_information
  end
end

.register_type_information(kind, receiver, method, type_ast) ⇒ Object



24
25
26
27
28
# File 'lib/typed/runtime/type_registry.rb', line 24

def register_type_information(kind, receiver, method, type_ast)
  methods = methods_for(kind, receiver)[method] || []
  methods << type_ast
  methods_for(kind, receiver)[method] = methods
end

.registered?(klass) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/typed/runtime/type_registry.rb', line 122

def registered?(klass)
  registered_classes = registry.keys.map(&:last)
  registered_classes.include?(klass)
end

.registry=(registry) ⇒ Object



133
134
135
# File 'lib/typed/runtime/type_registry.rb', line 133

def registry=(registry)
  @registry = registry
end

.type_var?(klass, variable) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
# File 'lib/typed/runtime/type_registry.rb', line 84

def type_var?(klass, variable)
  singleton_object = generic_types_registry[klass]
  if singleton_object
    singleton_object.type_vars.any? do |type_var|
      type_var.variable == variable
    end
  else
    false
  end
end

.type_vars_for(klass) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/typed/runtime/type_registry.rb', line 69

def type_vars_for(klass)
  singleton_object = find_generic_type(klass)
  if singleton_object
    singleton_object.type_vars.map do |type_var|
      ::TypedRb::Types::Polymorphism::TypeVariable.new(type_var.variable,
                                                       :upper_bound => type_var.upper_bound,
                                                       :lower_bound => type_var.lower_bound,
                                                       :gen_name => false)
    end
  else
    Array.call(TypedRb::Types::Polymorphism::TypeVariable).new
  end
end