Class: Steep::AST::Types::Factory
- Inherits:
-
Object
- Object
- Steep::AST::Types::Factory
- Defined in:
- lib/steep/ast/types/factory.rb
Defined Under Namespace
Classes: InterfaceCalculationError
Instance Attribute Summary collapse
-
#definition_builder ⇒ Object
readonly
Returns the value of attribute definition_builder.
Instance Method Summary collapse
- #absolute_type(type, namespace:) ⇒ Object
- #absolute_type_name(type_name, namespace:) ⇒ Object
- #class_name?(type_name) ⇒ Boolean
- #env ⇒ Object
- #expand_alias(type) ⇒ Object
- #function_1(params, return_type) ⇒ Object
-
#initialize(builder:) ⇒ Factory
constructor
A new instance of Factory.
- #interface(type, private:, self_type: type) ⇒ Object
- #method_type(method_type, self_type:) ⇒ Object
- #module_name?(type_name) ⇒ Boolean
- #namespace(namespace) ⇒ Object
- #namespace_1(namespace) ⇒ Object
- #params(type) ⇒ Object
- #type(type) ⇒ Object
- #type_1(type) ⇒ Object
- #type_name(name) ⇒ Object
- #type_name_1(name) ⇒ Object
- #unfold(type_name) ⇒ Object
Constructor Details
#initialize(builder:) ⇒ Factory
Returns a new instance of Factory.
7 8 9 |
# File 'lib/steep/ast/types/factory.rb', line 7 def initialize(builder:) @definition_builder = builder end |
Instance Attribute Details
#definition_builder ⇒ Object (readonly)
Returns the value of attribute definition_builder.
5 6 7 |
# File 'lib/steep/ast/types/factory.rb', line 5 def definition_builder @definition_builder end |
Instance Method Details
#absolute_type(type, namespace:) ⇒ Object
523 524 525 526 |
# File 'lib/steep/ast/types/factory.rb', line 523 def absolute_type(type, namespace:) type(env.absolute_type(type_1(type), namespace: namespace_1(namespace)) {|type| type.name.absolute! }) end |
#absolute_type_name(type_name, namespace:) ⇒ Object
528 529 530 531 |
# File 'lib/steep/ast/types/factory.rb', line 528 def absolute_type_name(type_name, namespace:) type(env.absolute_type_name(type_name_1(type_name), namespace: namespace_1(namespace)) {|name| name.absolute! }) end |
#class_name?(type_name) ⇒ Boolean
514 515 516 517 |
# File 'lib/steep/ast/types/factory.rb', line 514 def class_name?(type_name) name = type_name_1(type_name) env.class?(name) && env.find_class(name).is_a?(Ruby::Signature::AST::Declarations::Class) end |
#env ⇒ Object
519 520 521 |
# File 'lib/steep/ast/types/factory.rb', line 519 def env @env ||= definition_builder.env end |
#expand_alias(type) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/steep/ast/types/factory.rb', line 250 def (type) unfolded = case type when AST::Types::Name::Alias unfolded = unfold(type.name) else type end if block_given? yield unfolded else unfolded end end |
#function_1(params, return_type) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/steep/ast/types/factory.rb', line 165 def function_1(params, return_type) Ruby::Signature::Types::Function.new( required_positionals: params.required.map {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, optional_positionals: params.optional.map {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, rest_positionals: params.rest&.yield_self {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, trailing_positionals: [], required_keywords: params.required_keywords.transform_values {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, optional_keywords: params.optional_keywords.transform_values {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, rest_keywords: params.rest_keywords&.yield_self {|type| Ruby::Signature::Types::Function::Param.new(name: nil, type: type_1(type)) }, return_type: type_1(return_type) ) end |
#interface(type, private:, self_type: type) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/steep/ast/types/factory.rb', line 265 def interface(type, private:, self_type: type) type = (type) case type when Self if self_type != type interface self_type, private: private, self_type: Self.new else raise "Unexpected `self` type interface" end when Name::Instance Interface::Interface.new(type: self_type, private: private).tap do |interface| definition = definition_builder.build_instance(type_name_1(type.name)) instance_type = Name::Instance.new(name: type.name, args: type.args.map { Any.new(location: nil) }, location: nil) module_type = type.to_module() subst = Interface::Substitution.build( definition.type_params, type.args, instance_type: instance_type, module_type: module_type, self_type: self_type ) definition.methods.each do |name, method| next if method.private? && !private interface.methods[name] = Interface::Interface::Combination.overload( method.method_types.map do |type| method_type(type, self_type: self_type) {|ty| ty.subst(subst) } end, incompatible: method.attributes.include?(:incompatible) ) end end when Name::Interface Interface::Interface.new(type: self_type, private: private).tap do |interface| type_name = type_name_1(type.name) decl = definition_builder.env.find_class(type_name) or raise "Unknown class: #{type_name}" definition = definition_builder.build_interface(type_name, decl) subst = Interface::Substitution.build( definition.type_params, type.args, self_type: self_type ) definition.methods.each do |name, method| interface.methods[name] = Interface::Interface::Combination.overload( method.method_types.map do |type| method_type(type, self_type: self_type) {|type| type.subst(subst) } end, incompatible: method.attributes.include?(:incompatible) ) end end when Name::Class, Name::Module Interface::Interface.new(type: self_type, private: private).tap do |interface| definition = definition_builder.build_singleton(type_name_1(type.name)) instance_type = Name::Instance.new(name: type.name, args: definition.declaration.type_params.each.map {Any.new(location: nil)}, location: nil) subst = Interface::Substitution.build( [], instance_type: instance_type, module_type: type, self_type: self_type ) definition.methods.each do |name, method| next if !private && method.private? interface.methods[name] = Interface::Interface::Combination.overload( method.method_types.map do |type| method_type(type, self_type: self_type) {|type| type.subst(subst) } end, incompatible: method.attributes.include?(:incompatible) ) end end when Literal interface type.back_type, private: private, self_type: self_type when Nil interface Builtin::NilClass.instance_type, private: private, self_type: self_type when Boolean interface(AST::Types::Union.build(types: [Builtin::TrueClass.instance_type, Builtin::FalseClass.instance_type]), private: private, self_type: self_type) when Union yield_self do interfaces = type.types.map {|ty| interface(ty, private: private, self_type: self_type) } interfaces.inject do |interface1, interface2| Interface::Interface.new(type: self_type, private: private).tap do |interface| common_methods = Set.new(interface1.methods.keys) & Set.new(interface2.methods.keys) common_methods.each do |name| interface.methods[name] = Interface::Interface::Combination.union([interface1.methods[name], interface2.methods[name]]) end end end end when Intersection yield_self do interfaces = type.types.map {|ty| interface(ty, private: private, self_type: self_type) } interfaces.inject do |interface1, interface2| Interface::Interface.new(type: self_type, private: private).tap do |interface| all_methods = Set.new(interface1.methods.keys) + Set.new(interface2.methods.keys) all_methods.each do |name| methods = [interface1.methods[name], interface2.methods[name]].compact interface.methods[name] = Interface::Interface::Combination.intersection(methods) end end end end when Tuple yield_self do element_type = Union.build(types: type.types, location: nil) array_type = Builtin::Array.instance_type(element_type) interface(array_type, private: private, self_type: self_type).tap do |array_interface| array_interface.methods[:[]] = array_interface.methods[:[]].yield_self do |aref| Interface::Interface::Combination.overload( type.types.map.with_index {|elem_type, index| Interface::MethodType.new( type_params: [], params: Interface::Params.new(required: [AST::Types::Literal.new(value: index)], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil), block: nil, return_type: elem_type, location: nil ) } + aref.types, incompatible: false ) end array_interface.methods[:[]=] = array_interface.methods[:[]=].yield_self do |update| Interface::Interface::Combination.overload( type.types.map.with_index {|elem_type, index| Interface::MethodType.new( type_params: [], params: Interface::Params.new(required: [AST::Types::Literal.new(value: index), elem_type], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil), block: nil, return_type: elem_type, location: nil ) } + update.types, incompatible: false ) end end end when Record yield_self do key_type = type.elements.keys.map {|value| Literal.new(value: value, location: nil) }.yield_self do |types| Union.build(types: types, location: nil) end value_type = Union.build(types: type.elements.values, location: nil) hash_type = Builtin::Hash.instance_type(key_type, value_type) interface(hash_type, private: private, self_type: self_type).tap do |hash_interface| hash_interface.methods[:[]] = hash_interface.methods[:[]].yield_self do |ref| Interface::Interface::Combination.overload( type.elements.map {|key_value, value_type| key_type = Literal.new(value: key_value, location: nil) Interface::MethodType.new( type_params: [], params: Interface::Params.new(required: [key_type], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil), block: nil, return_type: value_type, location: nil ) } + ref.types, incompatible: false ) end hash_interface.methods[:[]=] = hash_interface.methods[:[]=].yield_self do |update| Interface::Interface::Combination.overload( type.elements.map {|key_value, value_type| key_type = Literal.new(value: key_value, location: nil) Interface::MethodType.new( type_params: [], params: Interface::Params.new(required: [key_type, value_type], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil), block: nil, return_type: value_type, location: nil ) } + update.types, incompatible: false ) end end end when Proc interface(Builtin::Proc.instance_type, private: private, self_type: self_type).tap do |interface| method_type = Interface::MethodType.new( type_params: [], params: type.params, return_type: type.return_type, block: nil, location: nil ) interface.methods[:[]] = Interface::Interface::Combination.overload([method_type], incompatible: false) interface.methods[:call] = Interface::Interface::Combination.overload([method_type], incompatible: false) end else raise "Unexpected type for interface: #{type}" end end |
#method_type(method_type, self_type:) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/steep/ast/types/factory.rb', line 189 def method_type(method_type, self_type:) case method_type when Ruby::Signature::MethodType fvs = self_type.free_variables() type_params = [] alpha_vars = [] alpha_types = [] method_type.type_params.map do |name| if fvs.include?(name) type = Types::Var.fresh(name) alpha_vars << name alpha_types << type type_params << type.name else type_params << name end end subst = Interface::Substitution.build(alpha_vars, alpha_types) type = Interface::MethodType.new( type_params: type_params, return_type: type(method_type.type.return_type).subst(subst), params: params(method_type.type).subst(subst), location: nil, block: method_type.block&.yield_self do |block| Interface::Block.new( optional: !block.required, type: Proc.new(params: params(block.type).subst(subst), return_type: type(block.type.return_type).subst(subst), location: nil) ) end ) if block_given? yield type else type end when :any :any end end |
#module_name?(type_name) ⇒ Boolean
509 510 511 512 |
# File 'lib/steep/ast/types/factory.rb', line 509 def module_name?(type_name) name = type_name_1(type_name) env.class?(name) && env.find_class(name).is_a?(Ruby::Signature::AST::Declarations::Module) end |
#namespace(namespace) ⇒ Object
157 158 159 |
# File 'lib/steep/ast/types/factory.rb', line 157 def namespace(namespace) Namespace.parse(namespace.to_s) end |
#namespace_1(namespace) ⇒ Object
161 162 163 |
# File 'lib/steep/ast/types/factory.rb', line 161 def namespace_1(namespace) Ruby::Signature::Namespace.parse(namespace.to_s) end |
#params(type) ⇒ Object
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/steep/ast/types/factory.rb', line 178 def params(type) Interface::Params.new( required: type.required_positionals.map {|param| type(param.type) }, optional: type.optional_positionals.map {|param| type(param.type) }, rest: type.rest_positionals&.yield_self {|param| type(param.type) }, required_keywords: type.required_keywords.transform_values {|param| type(param.type) }, optional_keywords: type.optional_keywords.transform_values {|param| type(param.type) }, rest_keywords: type.rest_keywords&.yield_self {|param| type(param.type) } ) end |
#type(type) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/steep/ast/types/factory.rb', line 11 def type(type) case type when Ruby::Signature::Types::Bases::Any Any.new(location: nil) when Ruby::Signature::Types::Bases::Class Class.new(location: nil) when Ruby::Signature::Types::Bases::Instance Instance.new(location: nil) when Ruby::Signature::Types::Bases::Self Self.new(location: nil) when Ruby::Signature::Types::Bases::Top Top.new(location: nil) when Ruby::Signature::Types::Bases::Bottom Bot.new(location: nil) when Ruby::Signature::Types::Bases::Bool Boolean.new(location: nil) when Ruby::Signature::Types::Bases::Void Void.new(location: nil) when Ruby::Signature::Types::Bases::Nil Nil.new(location: nil) when Ruby::Signature::Types::Variable Var.new(name: type.name, location: nil) when Ruby::Signature::Types::ClassSingleton type_name = type_name(type.name) Name::Class.new(name: type_name, location: nil, constructor: nil) when Ruby::Signature::Types::ClassInstance type_name = type_name(type.name) args = type.args.map {|arg| type(arg) } Name::Instance.new(name: type_name, args: args, location: nil) when Ruby::Signature::Types::Interface type_name = type_name(type.name) args = type.args.map {|arg| type(arg) } Name::Interface.new(name: type_name, args: args, location: nil) when Ruby::Signature::Types::Alias type_name = type_name(type.name) Name::Alias.new(name: type_name, args: [], location: nil) when Ruby::Signature::Types::Union Union.build(types: type.types.map {|ty| type(ty) }, location: nil) when Ruby::Signature::Types::Intersection Intersection.build(types: type.types.map {|ty| type(ty) }, location: nil) when Ruby::Signature::Types::Optional Union.build(types: [type(type.type), Nil.new(location: nil)], location: nil) when Ruby::Signature::Types::Literal Literal.new(value: type.literal, location: nil) when Ruby::Signature::Types::Tuple Tuple.new(types: type.types.map {|ty| type(ty) }, location: nil) when Ruby::Signature::Types::Record elements = type.fields.each.with_object({}) do |(key, value), hash| hash[key] = type(value) end Record.new(elements: elements, location: nil) when Ruby::Signature::Types::Proc params = params(type.type) return_type = type(type.type.return_type) Proc.new(params: params, return_type: return_type, location: nil) else raise "Unexpected type given: #{type}" end end |
#type_1(type) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/steep/ast/types/factory.rb', line 71 def type_1(type) case type when Any Ruby::Signature::Types::Bases::Any.new(location: nil) when Class Ruby::Signature::Types::Bases::Class.new(location: nil) when Instance Ruby::Signature::Types::Bases::Instance.new(location: nil) when Self Ruby::Signature::Types::Bases::Self.new(location: nil) when Top Ruby::Signature::Types::Bases::Top.new(location: nil) when Bot Ruby::Signature::Types::Bases::Bottom.new(location: nil) when Boolean Ruby::Signature::Types::Bases::Bool.new(location: nil) when Void Ruby::Signature::Types::Bases::Void.new(location: nil) when Nil Ruby::Signature::Types::Bases::Nil.new(location: nil) when Var Ruby::Signature::Types::Variable.new(name: type.name, location: nil) when Name::Class, Name::Module Ruby::Signature::Types::ClassSingleton.new(name: type_name_1(type.name), location: nil) when Name::Instance Ruby::Signature::Types::ClassInstance.new( name: type_name_1(type.name), args: type.args.map {|arg| type_1(arg) }, location: nil ) when Name::Interface Ruby::Signature::Types::Interface.new( name: type_name_1(type.name), args: type.args.map {|arg| type_1(arg) }, location: nil ) when Name::Alias type.args.empty? or raise "alias type with args is not supported" Ruby::Signature::Types::Alias.new(name: type_name_1(type.name), location: nil) when Union Ruby::Signature::Types::Union.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Intersection Ruby::Signature::Types::Intersection.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Literal Ruby::Signature::Types::Literal.new(literal: type.value, location: nil) when Tuple Ruby::Signature::Types::Tuple.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Record fields = type.elements.each.with_object({}) do |(key, value), hash| hash[key] = type_1(value) end Ruby::Signature::Types::Record.new(fields: fields, location: nil) when Proc Ruby::Signature::Types::Proc.new( type: function_1(type.params, type.return_type), location: nil ) else raise "Unexpected type given: #{type} (#{type.class})" end end |
#type_name(name) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/steep/ast/types/factory.rb', line 142 def type_name(name) case when name.class? Names::Module.new(name: name.name, namespace: namespace(name.namespace), location: nil) when name.interface? Names::Interface.new(name: name.name, namespace: namespace(name.namespace), location: nil) when name.alias? Names::Alias.new(name: name.name, namespace: namespace(name.namespace), location: nil) end end |
#type_name_1(name) ⇒ Object
153 154 155 |
# File 'lib/steep/ast/types/factory.rb', line 153 def type_name_1(name) Ruby::Signature::TypeName.new(name: name.name, namespace: namespace_1(name.namespace)) end |
#unfold(type_name) ⇒ Object
243 244 245 246 247 248 |
# File 'lib/steep/ast/types/factory.rb', line 243 def unfold(type_name) type_name_1(type_name).yield_self do |type_name| decl = definition_builder.env.find_alias(type_name) or raise "Unknown type name: #{type_name}" type(definition_builder.env.absolute_type(decl.type, namespace: type_name.namespace)) end end |