Class: Steep::AST::Types::Factory
- Defined in:
- lib/steep/ast/types/factory.rb
Instance Attribute Summary collapse
-
#definition_builder ⇒ Object
readonly
Returns the value of attribute definition_builder.
-
#type_cache ⇒ Object
readonly
Returns the value of attribute type_cache.
Instance Method Summary collapse
- #absolute_type(type, context:) ⇒ Object
- #absolute_type_name(type_name, context:) ⇒ Object
- #class_name?(type_name) ⇒ Boolean
- #deep_expand_alias(type, recursive: Set.new) ⇒ Object
- #env ⇒ Object
- #expand_alias(type) ⇒ Object
- #flatten_union(type, acc = []) ⇒ Object
- #function_1(func) ⇒ Object
-
#initialize(builder:) ⇒ Factory
constructor
A new instance of Factory.
- #inspect ⇒ Object
- #instance_type(type_name, args: nil) ⇒ Object
- #method_type(method_type) ⇒ Object
- #method_type_1(method_type) ⇒ Object
- #module_name?(type_name) ⇒ Boolean
- #normalize_args(type_name, args) ⇒ Object
- #normalize_type(type) ⇒ Object
- #params(type) ⇒ Object
- #partition_union(type) ⇒ Object
- #try_instance_type(type) ⇒ Object
- #try_singleton_type(type) ⇒ Object
- #type(type) ⇒ Object
- #type_1(type) ⇒ Object
- #type_1_opt(type) ⇒ Object
- #type_name_resolver ⇒ Object
- #type_opt(type) ⇒ Object
- #type_param(type_param) ⇒ Object
- #type_param_1(type_param) ⇒ Object
- #unfold(type_name, args) ⇒ Object
- #unwrap_optional(type) ⇒ Object
Constructor Details
#initialize(builder:) ⇒ Factory
Returns a new instance of Factory.
15 16 17 18 19 20 21 |
# File 'lib/steep/ast/types/factory.rb', line 15 def initialize(builder:) @definition_builder = builder @type_cache = {} @method_type_cache = {} @method_type_cache.compare_by_identity 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 |
#type_cache ⇒ Object (readonly)
Returns the value of attribute type_cache.
7 8 9 |
# File 'lib/steep/ast/types/factory.rb', line 7 def type_cache @type_cache end |
Instance Method Details
#absolute_type(type, context:) ⇒ Object
466 467 468 469 470 471 |
# File 'lib/steep/ast/types/factory.rb', line 466 def absolute_type(type, context:) absolute_type = type_1(type).map_type_name do |name| absolute_type_name(name, context: context) || name.absolute! end type(absolute_type) end |
#absolute_type_name(type_name, context:) ⇒ Object
473 474 475 |
# File 'lib/steep/ast/types/factory.rb', line 473 def absolute_type_name(type_name, context:) type_name_resolver.resolve(type_name, context: context) end |
#class_name?(type_name) ⇒ Boolean
458 459 460 |
# File 'lib/steep/ast/types/factory.rb', line 458 def class_name?(type_name) env.class_entry(type_name) ? true : false end |
#deep_expand_alias(type, recursive: Set.new) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/steep/ast/types/factory.rb', line 358 def (type, recursive: Set.new) case type when AST::Types::Name::Alias unless recursive.member?(type.name) unfolded = (type) (unfolded, recursive: recursive.union([type.name])) end when AST::Types::Union types = type.types.map {|ty| (ty, recursive: recursive) or return } AST::Types::Union.build(types: types) when AST::Types::Intersection types = type.types.map {|ty| (ty, recursive: recursive) or return } AST::Types::Intersection.build(types: types) else type end end |
#env ⇒ Object
462 463 464 |
# File 'lib/steep/ast/types/factory.rb', line 462 def env definition_builder.env end |
#expand_alias(type) ⇒ Object
349 350 351 352 353 354 355 356 |
# File 'lib/steep/ast/types/factory.rb', line 349 def (type) case type when AST::Types::Name::Alias unfold(type.name, type.args) else type end end |
#flatten_union(type, acc = []) ⇒ Object
376 377 378 379 380 381 382 383 384 385 |
# File 'lib/steep/ast/types/factory.rb', line 376 def flatten_union(type, acc = []) case type when AST::Types::Union type.types.each {|ty| flatten_union(ty, acc) } else acc << type end acc end |
#function_1(func) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/steep/ast/types/factory.rb', line 239 def function_1(func) params = func.params return_type = func.return_type if params RBS::Types::Function.new( required_positionals: params.required.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, optional_positionals: params.optional.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, rest_positionals: params.rest&.yield_self {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, trailing_positionals: [], required_keywords: params.required_keywords.transform_values {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, optional_keywords: params.optional_keywords.transform_values {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, rest_keywords: params.rest_keywords&.yield_self {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) }, return_type: type_1(return_type) ) else RBS::Types::UntypedFunction.new(return_type: type_1(return_type)) end end |
#inspect ⇒ Object
9 10 11 12 13 |
# File 'lib/steep/ast/types/factory.rb', line 9 def inspect s = "#<%s:%#018x " % [self.class, object_id] s << "@definition_builder=#<%s:%#018x>" % [definition_builder.class, definition_builder.object_id] s + ">" end |
#instance_type(type_name, args: nil) ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/steep/ast/types/factory.rb', line 477 def instance_type(type_name, args: nil) raise unless type_name.class? definition = definition_builder.build_singleton(type_name) def_args = definition.type_params.map { Any.instance } if args raise if def_args.size != args.size else args = def_args end AST::Types::Name::Instance.new(name: type_name, args: args) end |
#method_type(method_type) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/steep/ast/types/factory.rb', line 302 def method_type(method_type) @method_type_cache[method_type] ||= Interface::MethodType.new( type_params: method_type.type_params.map {|param| type_param(param) }, type: Interface::Function.new( params: params(method_type.type), return_type: type(method_type.type.return_type), location: method_type.location ), block: method_type.block&.yield_self do |block| Interface::Block.new( optional: !block.required, type: Interface::Function.new( params: params(block.type), return_type: type(block.type.return_type), location: nil ), self_type: type_opt(block.self_type) ) end ) end |
#method_type_1(method_type) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/steep/ast/types/factory.rb', line 325 def method_type_1(method_type) RBS::MethodType.new( type_params: method_type.type_params.map {|param| type_param_1(param) }, type: function_1(method_type.type), block: method_type.block&.yield_self do |block| RBS::Types::Block.new( type: function_1(block.type), required: !block.optional, self_type: type_1_opt(block.self_type) ) end, location: nil ) end |
#module_name?(type_name) ⇒ Boolean
454 455 456 |
# File 'lib/steep/ast/types/factory.rb', line 454 def module_name?(type_name) env.module_entry(type_name) ? true : false end |
#normalize_args(type_name, args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/steep/ast/types/factory.rb', line 39 def normalize_args(type_name, args) case when type_name.class? if entry = env.module_class_entry(type_name, normalized: true) type_params = entry.type_params end when type_name.interface? if entry = env.interface_decls.fetch(type_name, nil) type_params = entry.decl.type_params end when type_name.alias? if entry = env.type_alias_decls.fetch(type_name, nil) type_params = entry.decl.type_params end end if type_params && !type_params.empty? RBS::AST::TypeParam.normalize_args(type_params, args) else args end end |
#normalize_type(type) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/steep/ast/types/factory.rb', line 512 def normalize_type(type) case type when AST::Types::Name::Instance AST::Types::Name::Instance.new( name: env.normalize_module_name(type.name), args: type.args.map {|ty| normalize_type(ty) } ) when AST::Types::Name::Singleton AST::Types::Name::Singleton.new( name: env.normalize_module_name(type.name) ) when AST::Types::Any, AST::Types::Boolean, AST::Types::Bot, AST::Types::Nil, AST::Types::Top, AST::Types::Void, AST::Types::Literal, AST::Types::Class, AST::Types::Instance, AST::Types::Self, AST::Types::Var, AST::Types::Logic::Base type when AST::Types::Intersection AST::Types::Intersection.build( types: type.types.map {|type| normalize_type(type) } ) when AST::Types::Union AST::Types::Union.build( types: type.types.map {|type| normalize_type(type) } ) when AST::Types::Record type.map_type {|type| normalize_type(type) } when AST::Types::Tuple AST::Types::Tuple.new( types: type.types.map {|type| normalize_type(type) } ) when AST::Types::Proc type.map_type {|type| normalize_type(type) } when AST::Types::Name::Alias AST::Types::Name::Alias.new( name: type.name, args: type.args.map {|ty| normalize_type(ty) } ) when AST::Types::Name::Interface AST::Types::Name::Interface.new( name: type.name, args: type.args.map {|ty| normalize_type(ty) } ) end end |
#params(type) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/steep/ast/types/factory.rb', line 259 def params(type) case type when RBS::Types::Function Interface::Function::Params.build( 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) } ) when RBS::Types::UntypedFunction nil end end |
#partition_union(type) ⇒ Object
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 |
# File 'lib/steep/ast/types/factory.rb', line 387 def partition_union(type) case type when AST::Types::Name::Alias unfold = (type) if unfold == type [type, type] else partition_union(unfold) end when AST::Types::Union truthy_types = [] #: Array[AST::Types::t] falsy_types = [] #: Array[AST::Types::t] type.types.each do |type| truthy, falsy = partition_union(type) truthy_types << truthy if truthy falsy_types << falsy if falsy end [ truthy_types.empty? ? nil : AST::Types::Union.build(types: truthy_types), falsy_types.empty? ? nil : AST::Types::Union.build(types: falsy_types) ] when AST::Types::Any, AST::Types::Boolean, AST::Types::Top, AST::Types::Logic::Base [type, type] when AST::Types::Bot, AST::Types::Void [nil, nil] when AST::Types::Nil [nil, type] when AST::Types::Literal if type.value == false [nil, type] else [type, nil] end else [type, nil] end end |
#try_instance_type(type) ⇒ Object
492 493 494 495 496 497 498 499 500 501 |
# File 'lib/steep/ast/types/factory.rb', line 492 def try_instance_type(type) case type when AST::Types::Name::Instance instance_type(type.name) when AST::Types::Name::Singleton instance_type(type.name) else nil end end |
#try_singleton_type(type) ⇒ Object
503 504 505 506 507 508 509 510 |
# File 'lib/steep/ast/types/factory.rb', line 503 def try_singleton_type(type) case type when AST::Types::Name::Instance, AST::Types::Name::Singleton AST::Types::Name::Singleton.new(name:type.name) else nil end end |
#type(type) ⇒ Object
62 63 64 65 66 67 68 69 70 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 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/steep/ast/types/factory.rb', line 62 def type(type) if ty = type_cache[type] return ty end type_cache[type] = case type when RBS::Types::Bases::Any Any.instance when RBS::Types::Bases::Class Class.instance when RBS::Types::Bases::Instance Instance.instance when RBS::Types::Bases::Self Self.instance when RBS::Types::Bases::Top Top.instance when RBS::Types::Bases::Bottom Bot.instance when RBS::Types::Bases::Bool Boolean.instance when RBS::Types::Bases::Void Void.instance when RBS::Types::Bases::Nil Nil.instance when RBS::Types::Variable Var.new(name: type.name) when RBS::Types::ClassSingleton type_name = type.name Name::Singleton.new(name: type_name) when RBS::Types::ClassInstance type_name = type.name args = normalize_args(type_name, type.args).map {|arg| type(arg) } Name::Instance.new(name: type_name, args: args) when RBS::Types::Interface type_name = type.name args = normalize_args(type_name, type.args).map {|arg| type(arg) } Name::Interface.new(name: type_name, args: args) when RBS::Types::Alias type_name = type.name args = normalize_args(type_name, type.args).map {|arg| type(arg) } Name::Alias.new(name: type_name, args: args) when RBS::Types::Union Union.build(types: type.types.map {|ty| type(ty) }) when RBS::Types::Intersection Intersection.build(types: type.types.map {|ty| type(ty) }) when RBS::Types::Optional Union.build(types: [type(type.type), Nil.instance()]) when RBS::Types::Literal Literal.new(value: type.literal) when RBS::Types::Tuple Tuple.new(types: type.types.map {|ty| type(ty) }) when RBS::Types::Record elements = {} #: Hash[Record::key, AST::Types::t] required_keys = Set[] #: Set[Record::key] type.all_fields.each do |key, (value, required)| required_keys << key if required elements[key] = type(value) end Record.new(elements: elements, required_keys: required_keys) when RBS::Types::Proc func = Interface::Function.new( params: params(type.type), return_type: type(type.type.return_type), location: nil ) block = if type.block Interface::Block.new( type: Interface::Function.new( params: params(type.block.type), return_type: type(type.block.type.return_type), location: nil ), optional: !type.block.required, self_type: type_opt(type.block.self_type) ) end Proc.new( type: func, block: block, self_type: type_opt(type.self_type) ) else raise "Unexpected type given: #{type}" end end |
#type_1(type) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 233 234 235 236 237 |
# File 'lib/steep/ast/types/factory.rb', line 152 def type_1(type) case type when Any RBS::Types::Bases::Any.new(location: nil) when Class RBS::Types::Bases::Class.new(location: nil) when Instance RBS::Types::Bases::Instance.new(location: nil) when Self RBS::Types::Bases::Self.new(location: nil) when Top RBS::Types::Bases::Top.new(location: nil) when Bot RBS::Types::Bases::Bottom.new(location: nil) when Boolean RBS::Types::Bases::Bool.new(location: nil) when Void RBS::Types::Bases::Void.new(location: nil) when Nil RBS::Types::Bases::Nil.new(location: nil) when Var RBS::Types::Variable.new(name: type.name, location: nil) when Name::Singleton RBS::Types::ClassSingleton.new(name: type.name, location: nil) when Name::Instance RBS::Types::ClassInstance.new( name: type.name, args: type.args.map {|arg| type_1(arg) }, location: nil ) when Name::Interface RBS::Types::Interface.new( name: type.name, args: type.args.map {|arg| type_1(arg) }, location: nil ) when Name::Alias RBS::Types::Alias.new( name: type.name, args: type.args.map {|arg| type_1(arg) }, location: nil ) when Union RBS::Types::Union.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Intersection RBS::Types::Intersection.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Literal RBS::Types::Literal.new(literal: type.value, location: nil) when Tuple RBS::Types::Tuple.new( types: type.types.map {|ty| type_1(ty) }, location: nil ) when Record all_fields = {} #: Hash[Symbol, [RBS::Types::t, bool]] type.elements.each do |key, value| raise unless key.is_a?(Symbol) all_fields[key] = [type_1(value), type.required?(key)] end RBS::Types::Record.new(all_fields: all_fields, location: nil) when Proc block = if type.block RBS::Types::Block.new( type: function_1(type.block.type), required: !type.block.optional?, self_type: type_1_opt(type.block.self_type) ) end RBS::Types::Proc.new( type: function_1(type.type), self_type: type_1_opt(type.self_type), block: block, location: nil ) when Logic::Base RBS::Types::Bases::Bool.new(location: nil) else raise "Unexpected type given: #{type} (#{type.class})" end end |
#type_1_opt(type) ⇒ Object
33 34 35 36 37 |
# File 'lib/steep/ast/types/factory.rb', line 33 def type_1_opt(type) if type type_1(type) end end |
#type_name_resolver ⇒ Object
23 24 25 |
# File 'lib/steep/ast/types/factory.rb', line 23 def type_name_resolver @type_name_resolver ||= RBS::Resolver::TypeNameResolver.build(definition_builder.env) end |
#type_opt(type) ⇒ Object
27 28 29 30 31 |
# File 'lib/steep/ast/types/factory.rb', line 27 def type_opt(type) if type type(type) end end |
#type_param(type_param) ⇒ Object
275 276 277 278 279 280 281 282 283 |
# File 'lib/steep/ast/types/factory.rb', line 275 def type_param(type_param) Interface::TypeParam.new( name: type_param.name, upper_bound: type_opt(type_param.upper_bound_type), variance: type_param.variance, unchecked: type_param.unchecked?, default_type: type_opt(type_param.default_type) ) end |
#type_param_1(type_param) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/steep/ast/types/factory.rb', line 285 def type_param_1(type_param) RBS::AST::TypeParam.new( name: type_param.name, variance: type_param.variance, upper_bound: type_param.upper_bound&.yield_self {|u| case u_ = type_1(u) when RBS::Types::ClassInstance, RBS::Types::ClassSingleton, RBS::Types::Interface u_ else raise "`#{u_}` cannot be type parameter upper bound" end }, lower_bound: nil, location: type_param.location ).unchecked!(type_param.unchecked) end |
#unfold(type_name, args) ⇒ Object
340 341 342 343 344 345 346 347 |
# File 'lib/steep/ast/types/factory.rb', line 340 def unfold(type_name, args) type( definition_builder.( type_name, args.empty? ? [] : args.map {|t| type_1(t) } ) ) end |
#unwrap_optional(type) ⇒ Object
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 |
# File 'lib/steep/ast/types/factory.rb', line 428 def unwrap_optional(type) case type when AST::Types::Union unwrap = type.types.filter_map do |type| unless type.is_a?(AST::Types::Nil) type end end unless unwrap.empty? AST::Types::Union.build(types: unwrap) end when AST::Types::Nil nil when AST::Types::Name::Alias type_ = (type) if type_ == type type_ else unwrap_optional(type_) end else type end end |