Class: TypeProf::Core::Builtin

Inherits:
Object
  • Object
show all
Defined in:
lib/typeprof/core/builtin.rb

Instance Method Summary collapse

Constructor Details

#initialize(genv) ⇒ Builtin

Returns a new instance of Builtin.



3
4
5
# File 'lib/typeprof/core/builtin.rb', line 3

def initialize(genv)
  @genv = genv
end

Instance Method Details

#array_aref(changes, node, ty, a_args, ret) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/typeprof/core/builtin.rb', line 36

def array_aref(changes, node, ty, a_args, ret)
  if a_args.positionals.size == 1
    case ty
    when Type::Array
      idx = node.positional_args[0]
      if idx.is_a?(AST::IntegerNode)
        idx = idx.lit
      else
        idx = nil
      end
      vtx = ty.get_elem(@genv, idx)
      changes.add_edge(@genv, vtx, ret)
      true
    else
      false
    end
  else
    false
  end
end

#array_aset(changes, node, ty, a_args, ret) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/typeprof/core/builtin.rb', line 57

def array_aset(changes, node, ty, a_args, ret)
  if a_args.positionals.size == 2
    case ty
    when Type::Array
      val = a_args.positionals[1]
      idx = node.positional_args[0]
      if idx.is_a?(AST::IntegerNode) && ty.get_elem(@genv, idx.lit)
        changes.add_edge(@genv, val, ty.get_elem(@genv, idx.lit))
      else
        changes.add_edge(@genv, val, ty.get_elem(@genv))
      end
      true
    else
      false
    end
  else
    false
  end
end

#array_push(changes, node, ty, a_args, ret) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/typeprof/core/builtin.rb', line 77

def array_push(changes, node, ty, a_args, ret)
  if a_args.positionals.size == 1
    if ty.is_a?(Type::Array)
      val = a_args.positionals[0]
      changes.add_edge(@genv, val, ty.get_elem(@genv))
    end
    recv = Source.new(ty)
    changes.add_edge(@genv, recv, ret)
  else
    false
  end
end

#class_new(changes, node, ty, a_args, ret) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/typeprof/core/builtin.rb', line 7

def class_new(changes, node, ty, a_args, ret)
  if ty.is_a?(Type::Singleton)
    ty = ty.get_instance_type(@genv)
    recv = Source.new(ty)
    changes.add_method_call_box(@genv, recv, :initialize, a_args, false)
    changes.add_edge(@genv, Source.new(ty), ret)
  end
  true
end

#deployObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/typeprof/core/builtin.rb', line 130

def deploy
  {
    class_new: [[:Class], false, :new],
    object_class: [[:Object], false, :class],
    proc_call: [[:Proc], false, :call],
    array_aref: [[:Array], false, :[]],
    array_aset: [[:Array], false, :[]=],
    array_push: [[:Array], false, :<<],
    hash_aref: [[:Hash], false, :[]],
    hash_aset: [[:Hash], false, :[]=],
  }.each do |key, (cpath, singleton, mid)|
    me = @genv.resolve_method(cpath, singleton, mid)
    me.builtin = method(key)
  end
end

#hash_aref(changes, node, ty, a_args, ret) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/typeprof/core/builtin.rb', line 90

def hash_aref(changes, node, ty, a_args, ret)
  if a_args.positionals.size == 1
    case ty
    when Type::Hash
      idx = node.positional_args[0]
      idx = idx.is_a?(AST::SymbolNode) ? idx.lit : nil
      changes.add_edge(@genv, ty.get_value(idx), ret)
      true
    else
      false
    end
  else
    false
  end
end

#hash_aset(changes, node, ty, a_args, ret) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/typeprof/core/builtin.rb', line 106

def hash_aset(changes, node, ty, a_args, ret)
  if a_args.positionals.size == 2
    case ty
    when Type::Hash
      val = a_args.positionals[1]
      idx = node.positional_args[0]
      if idx.is_a?(AST::SymbolNode) && ty.get_value(idx.lit)
        # TODO: how to handle new key?
        changes.add_edge(@genv, val, ty.get_value(idx.lit))
      else
        # TODO: literal_pairs will not be updated
        changes.add_edge(@genv, a_args.positionals[0], ty.get_key)
        changes.add_edge(@genv, val, ty.get_value)
      end
      changes.add_edge(@genv, val, ret)
      true
    else
      false
    end
  else
    false
  end
end

#object_class(changes, node, ty, a_args, ret) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/typeprof/core/builtin.rb', line 17

def object_class(changes, node, ty, a_args, ret)
  ty = ty.base_type(@genv)
  mod = ty.is_a?(Type::Instance) ? ty.mod : @genv.mod_class
  ty = Type::Singleton.new(@genv, mod)
  vtx = Source.new(ty)
  changes.add_edge(@genv, vtx, ret)
  true
end

#proc_call(changes, node, ty, a_args, ret) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/typeprof/core/builtin.rb', line 26

def proc_call(changes, node, ty, a_args, ret)
  case ty
  when Type::Proc
    ty.block.accept_args(@genv, changes, a_args.positionals, ret, false)
    true
  else
    false
  end
end