Class: Steep::AST::Types::Name
- Inherits:
-
Object
- Object
- Steep::AST::Types::Name
- Includes:
- Helper::ChildrenLevel
- Defined in:
- lib/steep/ast/types/name.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .new_class(location: nil, name:, constructor:, args: []) ⇒ Object
- .new_instance(location: nil, name:, args: []) ⇒ Object
- .new_interface(location: nil, name:, args: []) ⇒ Object
- .new_module(location: nil, name:, args: []) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #class_type(constructor:) ⇒ Object
- #free_variables ⇒ Object
- #hash ⇒ Object
-
#initialize(name:, args:, location: nil) ⇒ Name
constructor
A new instance of Name.
- #instance_type ⇒ Object
- #level ⇒ Object
- #module_type ⇒ Object
- #subst(s) ⇒ Object
- #to_s ⇒ Object
Methods included from Helper::ChildrenLevel
Constructor Details
#initialize(name:, args:, location: nil) ⇒ Name
Returns a new instance of Name.
9 10 11 12 13 |
# File 'lib/steep/ast/types/name.rb', line 9 def initialize(name:, args:, location: nil) @location = location @name = name @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
7 8 9 |
# File 'lib/steep/ast/types/name.rb', line 7 def args @args end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
5 6 7 |
# File 'lib/steep/ast/types/name.rb', line 5 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/steep/ast/types/name.rb', line 6 def name @name end |
Class Method Details
.new_class(location: nil, name:, constructor:, args: []) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/steep/ast/types/name.rb', line 42 def self.new_class(location: nil, name:, constructor:, args: []) name = ModuleName.parse(name) unless name.is_a?(ModuleName) new(location: location, name: TypeName::Class.new(name: name, constructor: constructor), args: args) end |
.new_instance(location: nil, name:, args: []) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/steep/ast/types/name.rb', line 49 def self.new_instance(location: nil, name:, args: []) name = ModuleName.parse(name) unless name.is_a?(ModuleName) new(location: location, name: TypeName::Instance.new(name: name), args: args) end |
.new_interface(location: nil, name:, args: []) ⇒ Object
56 57 58 |
# File 'lib/steep/ast/types/name.rb', line 56 def self.new_interface(location: nil, name:, args: []) new(location: location, name: TypeName::Interface.new(name: name), args: args) end |
.new_module(location: nil, name:, args: []) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/steep/ast/types/name.rb', line 35 def self.new_module(location: nil, name:, args: []) name = ModuleName.parse(name) unless name.is_a?(ModuleName) new(location: location, name: TypeName::Module.new(name: name), args: args) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
15 16 17 18 19 |
# File 'lib/steep/ast/types/name.rb', line 15 def ==(other) other.is_a?(Name) && other.name == name && other.args == args end |
#class_type(constructor:) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/steep/ast/types/name.rb', line 79 def class_type(constructor:) case name when TypeName::Instance self.class.new_class(location: location, name: name.name, constructor: constructor, args: []) when TypeName::Class self when TypeName::Module, TypeName::Interface raise "Cannot make class type: #{inspect}" else raise "Unknown name: #{name.inspect}" end end |
#free_variables ⇒ Object
110 111 112 113 114 |
# File 'lib/steep/ast/types/name.rb', line 110 def free_variables self.args.each.with_object(Set.new) do |type, vars| vars.merge(type.free_variables) end end |
#hash ⇒ Object
21 22 23 |
# File 'lib/steep/ast/types/name.rb', line 21 def hash self.class.hash ^ name.hash ^ args.hash end |
#instance_type ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/steep/ast/types/name.rb', line 66 def instance_type case name when TypeName::Interface, TypeName::Instance self when TypeName::Module, TypeName::Class self.class.new_instance(location: location, name: name.name, args: args) else raise "Unknown name: #{name.inspect}" end end |
#level ⇒ Object
118 119 120 |
# File 'lib/steep/ast/types/name.rb', line 118 def level [0] + level_of_children(args) end |
#module_type ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/steep/ast/types/name.rb', line 95 def module_type case name when TypeName::Instance, self.class.new_module(location: location, name: name.name, args: args) when TypeName::Module self when TypeName::Class, TypeName::Interface raise "Cannot make module type: #{inspect}" else raise "Unknown name: #{name.inspect}" end end |
#subst(s) ⇒ Object
60 61 62 63 64 |
# File 'lib/steep/ast/types/name.rb', line 60 def subst(s) self.class.new(location: location, name: name, args: args.map {|a| a.subst(s) }) end |
#to_s ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/steep/ast/types/name.rb', line 27 def to_s if args.empty? "#{name}" else "#{name}<#{args.join(", ")}>" end end |