Class: Steep::AST::Types::Name

Inherits:
Object
  • Object
show all
Includes:
Helper::ChildrenLevel
Defined in:
lib/steep/ast/types/name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::ChildrenLevel

#level_of_children

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

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/steep/ast/types/name.rb', line 7

def args
  @args
end

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/steep/ast/types/name.rb', line 5

def location
  @location
end

#nameObject (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_variablesObject



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

#hashObject



21
22
23
# File 'lib/steep/ast/types/name.rb', line 21

def hash
  self.class.hash ^ name.hash ^ args.hash
end

#instance_typeObject



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

#levelObject



118
119
120
# File 'lib/steep/ast/types/name.rb', line 118

def level
  [0] + level_of_children(args)
end

#module_typeObject



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_sObject



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