Class: Steep::AST::Types::Proc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::ChildrenLevel

#level_of_children

Constructor Details

#initialize(type:, block:, self_type:) ⇒ Proc

Returns a new instance of Proc.



9
10
11
12
13
# File 'lib/steep/ast/types/proc.rb', line 9

def initialize(type:, block:, self_type:)
  @type = type
  @block = block
  @self_type = self_type
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



6
7
8
# File 'lib/steep/ast/types/proc.rb', line 6

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



15
16
17
# File 'lib/steep/ast/types/proc.rb', line 15

def ==(other)
  other.is_a?(self.class) && other.type == type && other.block == block && other.self_type == self_type
end

#back_typeObject



92
93
94
95
# File 'lib/steep/ast/types/proc.rb', line 92

def back_type
  Name::Instance.new(name: Builtin::Proc.module_name,
                     args: [])
end

#block_required?Boolean

Returns:



97
98
99
100
101
102
103
# File 'lib/steep/ast/types/proc.rb', line 97

def block_required?
  if block
    !block.optional?
  else
    false
  end
end

#each_child(&block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/steep/ast/types/proc.rb', line 105

def each_child(&block)
  if block
    type.each_child(&block)
    self.block&.type&.each_child(&block)
    self_type.each_child(&block) if self_type
  else
    enum_for :each_child
  end
end

#free_variablesObject



46
47
48
49
50
51
52
# File 'lib/steep/ast/types/proc.rb', line 46

def free_variables()
  @fvs ||= Set[].tap do |fvs|
    fvs.merge(type.free_variables)
    fvs.merge(block.free_variables) if block
    fvs.merge(self_type.free_variables) if self_type
  end
end

#hashObject



19
20
21
# File 'lib/steep/ast/types/proc.rb', line 19

def hash
  self.class.hash ^ type.hash ^ block.hash ^ self_type.hash
end

#levelObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/steep/ast/types/proc.rb', line 56

def level
  children = type.params&.each_type.to_a + [type.return_type]
  if block
    if block.type.params
      children.push(*block.type.params.each_type.to_a)
    end
    children.push(block.type.return_type)
  end
  if self_type
    children.push(self_type)
  end
  [0] + level_of_children(children)
end

#map_type(&block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/steep/ast/types/proc.rb', line 70

def map_type(&block)
  self.class.new(
    type: type.map_type(&block),
    block: self.block&.map_type(&block),
    self_type: self_type ? yield(self_type) : nil
  )
end

#one_arg?Boolean

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/steep/ast/types/proc.rb', line 78

def one_arg?
  params = type.params
  if params
    params.required.size == 1 &&
      params.optional.empty? &&
      !params.rest &&
      params.required_keywords.empty? &&
      params.optional_keywords.empty? &&
      !params.rest_keywords
  else
    true
  end
end

#subst(s) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/steep/ast/types/proc.rb', line 25

def subst(s)
  self.class.new(
    type: type.subst(s),
    block: block&.subst(s),
    self_type: self_type&.subst(s)
  )
end

#to_sObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/steep/ast/types/proc.rb', line 33

def to_s
  s =
    if self_type
      "[self: #{self_type}] "
    end

  if block
    "^#{type.params} #{s}#{block} -> #{type.return_type}"
  else
    "^#{type.params} #{s}-> #{type.return_type}"
  end
end