Class: RBS::DefinitionBuilder::AncestorBuilder
- Inherits:
-
Object
- Object
- RBS::DefinitionBuilder::AncestorBuilder
- Defined in:
- lib/rbs/definition_builder/ancestor_builder.rb
Defined Under Namespace
Classes: OneAncestors
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#instance_ancestors_cache ⇒ Object
readonly
Returns the value of attribute instance_ancestors_cache.
-
#interface_ancestors_cache ⇒ Object
readonly
Returns the value of attribute interface_ancestors_cache.
-
#one_instance_ancestors_cache ⇒ Object
readonly
Returns the value of attribute one_instance_ancestors_cache.
-
#one_interface_ancestors_cache ⇒ Object
readonly
Returns the value of attribute one_interface_ancestors_cache.
-
#one_singleton_ancestors_cache ⇒ Object
readonly
Returns the value of attribute one_singleton_ancestors_cache.
-
#singleton_ancestors_cache ⇒ Object
readonly
Returns the value of attribute singleton_ancestors_cache.
Instance Method Summary collapse
- #fill_ancestor_source(ancestor, name:, source:, &block) ⇒ Object
-
#initialize(env:) ⇒ AncestorBuilder
constructor
A new instance of AncestorBuilder.
- #instance_ancestors(type_name, building_ancestors: []) ⇒ Object
- #interface_ancestors(type_name, building_ancestors: []) ⇒ Object
- #mixin_ancestors(entry, type_name, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) ⇒ Object
- #mixin_ancestors0(decl, type_name, align_params:, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) ⇒ Object
- #one_instance_ancestors(type_name) ⇒ Object
- #one_interface_ancestors(type_name) ⇒ Object
- #one_singleton_ancestors(type_name) ⇒ Object
- #singleton_ancestors(type_name, building_ancestors: []) ⇒ Object
- #validate_super_class!(type_name, entry) ⇒ Object
Constructor Details
#initialize(env:) ⇒ AncestorBuilder
Returns a new instance of AncestorBuilder.
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 162 def initialize(env:) @env = env @one_instance_ancestors_cache = {} @instance_ancestors_cache = {} @one_singleton_ancestors_cache = {} @singleton_ancestors_cache = {} @one_interface_ancestors_cache = {} @interface_ancestors_cache = {} end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
151 152 153 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 151 def env @env end |
#instance_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute instance_ancestors_cache.
154 155 156 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 154 def instance_ancestors_cache @instance_ancestors_cache end |
#interface_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute interface_ancestors_cache.
160 161 162 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 160 def interface_ancestors_cache @interface_ancestors_cache end |
#one_instance_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute one_instance_ancestors_cache.
153 154 155 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 153 def one_instance_ancestors_cache @one_instance_ancestors_cache end |
#one_interface_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute one_interface_ancestors_cache.
159 160 161 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 159 def one_interface_ancestors_cache @one_interface_ancestors_cache end |
#one_singleton_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute one_singleton_ancestors_cache.
156 157 158 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 156 def one_singleton_ancestors_cache @one_singleton_ancestors_cache end |
#singleton_ancestors_cache ⇒ Object (readonly)
Returns the value of attribute singleton_ancestors_cache.
157 158 159 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 157 def singleton_ancestors_cache @singleton_ancestors_cache end |
Instance Method Details
#fill_ancestor_source(ancestor, name:, source:, &block) ⇒ Object
606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 606 def fill_ancestor_source(ancestor, name:, source:, &block) case ancestor when Definition::Ancestor::Instance if ancestor.name == name && !ancestor.source Definition::Ancestor::Instance.new(name: ancestor.name, args: ancestor.args, source: source) else ancestor end else ancestor end end |
#instance_ancestors(type_name, building_ancestors: []) ⇒ Object
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 508 509 510 511 512 513 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 434 def instance_ancestors(type_name, building_ancestors: []) as = instance_ancestors_cache[type_name] and return as entry = env.class_decls[type_name] or raise "Unknown name for instance_ancestors: #{type_name}" params = entry.type_params.each.map(&:name) args = entry.type_params.map do |type_param| Types::Variable.new(name: type_param.name, location: type_param.location) end self_ancestor = Definition::Ancestor::Instance.new(name: type_name, args: args, source: nil) RecursiveAncestorError.check!(self_ancestor, ancestors: building_ancestors, location: entry.primary.decl.location) building_ancestors.push self_ancestor one_ancestors = one_instance_ancestors(type_name) # @type var ancestors: Array[::RBS::Definition::Ancestor::t] ancestors = [] case entry when Environment::ClassEntry if super_class = one_ancestors.super_class # @type var super_class: Definition::Ancestor::Instance super_name = super_class.name super_args = super_class.args super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors) .apply(super_args, env: env, location: entry.primary.decl.super_class&.location) super_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: super_name, source: :super) } ancestors.unshift(*super_ancestors) end end if self_types = one_ancestors.self_types self_types.each do |mod| if mod.name.class? # Ensure there is no loop in ancestors chain instance_ancestors(mod.name, building_ancestors: building_ancestors) end end end if included_modules = one_ancestors.included_modules included_modules.each do |mod| name = mod.name arg_types = mod.args mod.source.is_a?(AST::Members::Include) or raise mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors) .apply(arg_types, env: env, location: mod.source.location) mod_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: name, source: mod.source) } ancestors.unshift(*mod_ancestors) end end ancestors.unshift(self_ancestor) if prepended_modules = one_ancestors.prepended_modules prepended_modules.each do |mod| name = mod.name arg_types = mod.args mod.source.is_a?(AST::Members::Prepend) or raise mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors) .apply(arg_types, env: env, location: mod.source.location) mod_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: name, source: mod.source) } ancestors.unshift(*mod_ancestors) end end building_ancestors.pop instance_ancestors_cache[type_name] = Definition::InstanceAncestors.new( type_name: type_name, params: params, ancestors: ancestors ) end |
#interface_ancestors(type_name, building_ancestors: []) ⇒ Object
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 570 def interface_ancestors(type_name, building_ancestors: []) as = interface_ancestors_cache[type_name] and return as entry = env.interface_decls[type_name] or raise "Unknown name for interface_ancestors: #{type_name}" params = entry.decl.type_params.each.map(&:name) args = Types::Variable.build(params) self_ancestor = Definition::Ancestor::Instance.new(name: type_name, args: args, source: nil) RecursiveAncestorError.check!(self_ancestor, ancestors: building_ancestors, location: entry.decl.location) building_ancestors.push self_ancestor one_ancestors = one_interface_ancestors(type_name) ancestors = [] included_interfaces = one_ancestors.included_interfaces or raise included_interfaces.each do |a| a.source.is_a?(AST::Members::Include) or raise included_ancestors = interface_ancestors(a.name, building_ancestors: building_ancestors) .apply(a.args, env: env, location: a.source.location) included_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: a.name, source: a.source) } ancestors.unshift(*included_ancestors) end ancestors.unshift(self_ancestor) building_ancestors.pop interface_ancestors_cache[type_name] = Definition::InstanceAncestors.new( type_name: type_name, params: params, ancestors: ancestors ) end |
#mixin_ancestors(entry, type_name, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 414 def mixin_ancestors(entry, type_name, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) entry.decls.each do |d| decl = d.decl align_params = Substitution.build( decl.type_params.each.map(&:name), entry.type_params.map {|param| Types::Variable.new(name: param.name, location: param.location) } ) mixin_ancestors0(decl, type_name, align_params: align_params, included_modules: included_modules, included_interfaces: included_interfaces, extended_modules: extended_modules, prepended_modules: prepended_modules, extended_interfaces: extended_interfaces) end end |
#mixin_ancestors0(decl, type_name, align_params:, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) ⇒ Object
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 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 348 def mixin_ancestors0(decl, type_name, align_params:, included_modules:, included_interfaces:, extended_modules:, prepended_modules:, extended_interfaces:) decl.each_mixin do |member| case member when AST::Members::Include module_name = member.name module_args = member.args.map {|type| align_params ? type.sub(align_params) : type } case when member.name.class? && included_modules MixinClassError.check!(type_name: type_name, env: env, member: member) NoMixinFoundError.check!(member.name, env: env, member: member) module_decl = env.normalized_module_entry(module_name) or raise module_args = AST::TypeParam.normalize_args(module_decl.type_params, module_args) module_name = env.normalize_module_name(module_name) included_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args, source: member) when member.name.interface? && included_interfaces NoMixinFoundError.check!(member.name, env: env, member: member) interface_decl = env.interface_decls.fetch(module_name) module_args = AST::TypeParam.normalize_args(interface_decl.decl.type_params, module_args) included_interfaces << Definition::Ancestor::Instance.new(name: module_name, args: module_args, source: member) end when AST::Members::Prepend if prepended_modules MixinClassError.check!(type_name: type_name, env: env, member: member) NoMixinFoundError.check!(member.name, env: env, member: member) module_decl = env.normalized_module_entry(member.name) or raise module_name = module_decl.name module_args = member.args.map {|type| align_params ? type.sub(align_params) : type } module_args = AST::TypeParam.normalize_args(module_decl.type_params, module_args) prepended_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args, source: member) end when AST::Members::Extend module_name = member.name module_args = member.args case when member.name.class? && extended_modules MixinClassError.check!(type_name: type_name, env: env, member: member) NoMixinFoundError.check!(member.name, env: env, member: member) module_decl = env.normalized_module_entry(module_name) or raise module_args = AST::TypeParam.normalize_args(module_decl.type_params, module_args) module_name = env.normalize_module_name(module_name) extended_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args, source: member) when member.name.interface? && extended_interfaces NoMixinFoundError.check!(member.name, env: env, member: member) interface_decl = env.interface_decls.fetch(module_name) module_args = AST::TypeParam.normalize_args(interface_decl.decl.type_params, module_args) extended_interfaces << Definition::Ancestor::Instance.new(name: module_name, args: module_args, source: member) end end end end |
#one_instance_ancestors(type_name) ⇒ Object
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 192 def one_instance_ancestors(type_name) type_name = env.normalize_module_name(type_name) as = one_instance_ancestors_cache[type_name] and return as entry = env.class_decls[type_name] or raise "Unknown name for one_instance_ancestors: #{type_name}" params = entry.type_params.each.map(&:name) case entry when Environment::ClassEntry validate_super_class!(type_name, entry) primary = entry.primary super_class = primary.decl.super_class if type_name != BuiltinNames::BasicObject.name if super_class super_name = super_class.name super_args = super_class.args else super_name = BuiltinNames::Object.name super_args = [] end super_name = env.normalize_module_name(super_name) NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location) if super_class InheritModuleError.check!(super_class, env: env) end super_entry = env.normalized_class_entry(super_name) or raise super_args = AST::TypeParam.normalize_args(super_entry.type_params, super_args) ancestors = OneAncestors.class_instance( type_name: type_name, params: params, super_class: Definition::Ancestor::Instance.new(name: super_name, args: super_args, source: :super) ) else ancestors = OneAncestors.class_instance( type_name: type_name, params: params, super_class: nil ) end when Environment::ModuleEntry ancestors = OneAncestors.module_instance(type_name: type_name, params: params) self_types = ancestors.self_types or raise if entry.self_types.empty? self_types.push Definition::Ancestor::Instance.new(name: BuiltinNames::Object.name, args: [], source: nil) else entry.self_types.each do |module_self| NoSelfTypeFoundError.check!(module_self, env: env) module_name = module_self.name if module_name.class? module_entry = env.normalized_module_class_entry(module_name) or raise module_name = module_entry.name self_args = AST::TypeParam.normalize_args(module_entry.type_params, module_self.args) end if module_name.interface? interface_entry = env.interface_decls.fetch(module_name) self_args = AST::TypeParam.normalize_args(interface_entry.decl.type_params, module_self.args) end self_args or raise self_types.push Definition::Ancestor::Instance.new(name: module_name, args: self_args, source: module_self) end end end mixin_ancestors(entry, type_name, included_modules: ancestors.included_modules, included_interfaces: ancestors.included_interfaces, prepended_modules: ancestors.prepended_modules, extended_modules: nil, extended_interfaces: nil) one_instance_ancestors_cache[type_name] = ancestors end |
#one_interface_ancestors(type_name) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 329 def one_interface_ancestors(type_name) one_interface_ancestors_cache[type_name] ||= begin entry = env.interface_decls[type_name] or raise "Unknown name for one_interface_ancestors: #{type_name}" params = entry.decl.type_params.each.map(&:name) OneAncestors.interface(type_name: type_name, params: params).tap do |ancestors| mixin_ancestors0(entry.decl, type_name, align_params: nil, included_modules: nil, included_interfaces: ancestors.included_interfaces, prepended_modules: nil, extended_modules: nil, extended_interfaces: nil) end end end |
#one_singleton_ancestors(type_name) ⇒ Object
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 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 275 def one_singleton_ancestors(type_name) type_name = env.normalize_module_name(type_name) as = one_singleton_ancestors_cache[type_name] and return as entry = env.class_decls[type_name] or raise "Unknown name for one_singleton_ancestors: #{type_name}" case entry when Environment::ClassEntry validate_super_class!(type_name, entry) primary = entry.primary super_class = primary.decl.super_class if type_name != BuiltinNames::BasicObject.name if super_class super_name = super_class.name else super_name = BuiltinNames::Object.name end super_name = env.normalize_module_name(super_name) NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location) if super_class InheritModuleError.check!(super_class, env: env) end ancestors = OneAncestors.singleton( type_name: type_name, super_class: Definition::Ancestor::Singleton.new(name: super_name) ) else ancestors = OneAncestors.singleton( type_name: type_name, super_class: Definition::Ancestor::Instance.new(name: BuiltinNames::Class.name, args: [], source: :super) ) end when Environment::ModuleEntry ancestors = OneAncestors.singleton( type_name: type_name, super_class: Definition::Ancestor::Instance.new(name: BuiltinNames::Module.name, args: [], source: :super) ) end mixin_ancestors(entry, type_name, included_modules: nil, included_interfaces: nil, prepended_modules: nil, extended_modules: ancestors.extended_modules, extended_interfaces: ancestors.extended_interfaces) one_singleton_ancestors_cache[type_name] = ancestors end |
#singleton_ancestors(type_name, building_ancestors: []) ⇒ Object
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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 515 def singleton_ancestors(type_name, building_ancestors: []) as = singleton_ancestors_cache[type_name] and return as entry = env.class_decls[type_name] or raise "Unknown name for singleton_ancestors: #{type_name}" self_ancestor = Definition::Ancestor::Singleton.new(name: type_name) RecursiveAncestorError.check!(self_ancestor, ancestors: building_ancestors, location: entry.primary.decl.location) building_ancestors.push self_ancestor one_ancestors = one_singleton_ancestors(type_name) ancestors = [] case super_class = one_ancestors.super_class when Definition::Ancestor::Instance super_name = super_class.name super_args = super_class.args super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors) .apply(super_args, env: env, location: nil) super_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: super_name, source: :super) } ancestors.unshift(*super_ancestors) when Definition::Ancestor::Singleton super_name = super_class.name super_ancestors = singleton_ancestors(super_name, building_ancestors: []) ancestors.unshift(*super_ancestors.ancestors) end extended_modules = one_ancestors.extended_modules or raise extended_modules.each do |mod| name = mod.name args = mod.args mod.source.is_a?(AST::Members::Extend) or raise mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors) .apply(args, env: env, location: mod.source.location) mod_ancestors.map! {|ancestor| fill_ancestor_source(ancestor, name: name, source: mod.source) } ancestors.unshift(*mod_ancestors) end ancestors.unshift(self_ancestor) building_ancestors.pop singleton_ancestors_cache[type_name] = Definition::SingletonAncestors.new( type_name: type_name, ancestors: ancestors ) end |
#validate_super_class!(type_name, entry) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rbs/definition_builder/ancestor_builder.rb', line 175 def validate_super_class!(type_name, entry) with_super_classes = entry.decls.select {|d| d.decl.super_class } return if with_super_classes.size <= 1 super_types = with_super_classes.map do |d| super_class = d.decl.super_class or raise Types::ClassInstance.new(name: super_class.name, args: super_class.args, location: nil) end super_types.uniq! return if super_types.size == 1 raise SuperclassMismatchError.new(name: type_name, entry: entry) end |