Module: FastRuby::VariabeTranslator

Defined in:
lib/fastruby/translator/modules/variable.rb

Instance Method Summary collapse

Instance Method Details

#to_c_cdecl(tree, result_var_ = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fastruby/translator/modules/variable.rb', line 95

def to_c_cdecl(tree, result_var_ = nil)
  
  result_var = result_var_ || "value"
  
  if tree[1].instance_of? Symbol
    code = "
      {
        // set constant #{tree[1].to_s}
        #{to_c tree[2], result_var};
        rb_const_set(rb_cObject, #{intern_num tree[1]}, #{result_var});
      }
      "
  elsif tree[1].instance_of? FastRuby::FastRubySexp

    if tree[1].node_type == :colon2
      code = "
        {
          // set constant #{tree[1].to_s}
          #{to_c tree[2], result_var};
          VALUE klass = Qnil;
          #{to_c tree[1][1], "klass"};
          rb_const_set(klass, #{intern_num tree[1][2]}, #{result_var});
        }
        "
    elsif tree[1].node_type == :colon3
      code = "
        {
        // set constant #{tree[1].to_s}
        #{to_c tree[2], result_var};
        rb_const_set(rb_cObject, #{intern_num tree[1][1]}, #{result_var});
        }
        "
    end
    
    if result_var_
      code
    else
       inline_block "VALUE #{result_var} = Qnil;\n" + code + "
        return #{result_var};
       "
    end
  end
end

#to_c_colon2(tree, result_var = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fastruby/translator/modules/variable.rb', line 143

def to_c_colon2(tree, result_var = nil)
  code = "
    {
    VALUE klass = Qnil;
    
    #{to_c tree[1],"klass"};
        #{
        if result_var
          "#{result_var} = Qnil;"
        end
      }

  if (rb_is_const_id(#{intern_num tree[2]})) {
    switch (TYPE(klass)) {
      case T_CLASS:
      case T_MODULE:
        #{
        if result_var
          "#{result_var} = rb_const_get_from(klass, #{intern_num tree[2]});"
        else
          "return rb_const_get_from(klass, #{intern_num tree[2]});"
        end
        }
        break;
      default:
        #{_raise("rb_eTypeError","not a class/module")};
        break;
    }
  }
  else {
        #{
        if result_var
          "#{result_var} = rb_funcall(klass, #{intern_num tree[2]}, 0, 0);"
        else
          "return rb_funcall(klass, #{intern_num tree[2]}, 0, 0);"
        end
        }
  }
    
        #{
        unless result_var
          "return Qnil;"
        end
        }
    }
  "
  
  if result_var
    code
  else
    inline_block code
  end
end

#to_c_colon3(tree) ⇒ Object



139
140
141
# File 'lib/fastruby/translator/modules/variable.rb', line 139

def to_c_colon3(tree)
  "rb_const_get_from(rb_cObject, #{intern_num tree[1]})"
end

#to_c_const(tree) ⇒ Object



91
92
93
# File 'lib/fastruby/translator/modules/variable.rb', line 91

def to_c_const(tree)
  "rb_const_get(CLASS_OF(plocals->self), #{intern_num(tree[1])})"
end

#to_c_cvar(tree) ⇒ Object



26
27
28
# File 'lib/fastruby/translator/modules/variable.rb', line 26

def to_c_cvar(tree)
  "rb_cvar_get(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]})"
end

#to_c_cvasgn(tree, result_var = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastruby/translator/modules/variable.rb', line 30

def to_c_cvasgn(tree, result_var = nil)
  if result_var
    "
      {
        VALUE recv = CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self;

        #{to_c tree[2], result_var};
        
        #{if RUBY_VERSION =~ /^1\.9/
          "rb_cvar_set(recv,#{intern_num tree[1]},#{result_var});"
          elsif RUBY_VERSION =~ /^1\.8/
          "rb_cvar_set(recv,#{intern_num tree[1]},#{result_var},Qfalse);"
          else
            raise RuntimeError, "unsupported ruby version #{RUBY_VERSION}"
          end 
        }
      }
   "
  else
  "__rb_cvar_set(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]},#{to_c tree[2]},Qfalse)"
  end
end

#to_c_defined(tree) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
# File 'lib/fastruby/translator/modules/variable.rb', line 253

def to_c_defined(tree)
  nt = tree[1].node_type

  if nt == :self
  'rb_str_new2("self")'
  elsif nt == :true
  'rb_str_new2("true")'
  elsif nt == :false
  'rb_str_new2("false")'
  elsif nt == :nil
  'rb_str_new2("nil")'
  elsif nt == :lvar
  'rb_str_new2("local-variable")'
  elsif nt == :gvar
  "rb_gvar_defined((struct global_entry*)#{global_entry(tree[1][1])}) ? #{literal_value "global-variable"} : Qnil"
  elsif nt == :const
  "rb_const_defined(rb_cObject, #{intern_num tree[1][1]}) ? #{literal_value "constant"} : Qnil"
  elsif nt == :call
    if RUBY_VERSION =~ /^1\.8/
    "rb_method_node(CLASS_OF(#{to_c tree[1][1]}), #{intern_num tree[1][2]}) ? #{literal_value "method"} : Qnil"
    else
    "rb_method_entry(CLASS_OF(#{to_c tree[1][1]}), #{intern_num tree[1][2]}) ? #{literal_value "method"} : Qnil"
    end
  elsif nt == :yield
    "rb_block_given_p() ? #{literal_value "yield"} : Qnil"
  elsif nt == :ivar
  "rb_ivar_defined(plocals->self,#{intern_num tree[1][1]}) ? #{literal_value "instance-variable"} : Qnil"
  elsif nt == :attrset or
        nt == :op_asgn1 or
        nt == :op_asgn2 or
        nt == :op_asgn_or or
        nt == :op_asgn_and or
        nt == :op_asgn_masgn or
        nt == :masgn or
        nt == :lasgn or
        nt == :dasgn or
        nt == :dasgn_curr or
        nt == :gasgn or
        nt == :iasgn or
        nt == :cdecl or
        nt == :cvdecl or
        nt == :cvasgn
    literal_value "assignment"
  else
    literal_value "expression"
  end
end

#to_c_gasgn(tree, result_var = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fastruby/translator/modules/variable.rb', line 61

def to_c_gasgn(tree, result_var = nil)
  if result_var
      "
      {
      #{to_c tree[2], result_var}; 
      rb_gvar_set((void*)#{global_entry(tree[1])},#{result_var});
      }
      "
  else
  "_rb_gvar_set((void*)#{global_entry(tree[1])}, #{to_c tree[2]})"
  end
end

#to_c_gvar(tree) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/fastruby/translator/modules/variable.rb', line 53

def to_c_gvar(tree)
  if (tree[1] == :$!)
    "pframe->thread_data->exception"
  else
    "rb_gvar_get((struct global_entry*)#{global_entry(tree[1])})"
  end
end

#to_c_iasgn(tree, result_var = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fastruby/translator/modules/variable.rb', line 78

def to_c_iasgn(tree, result_var = nil)
  if result_var
      "
      {
      #{to_c tree[2], result_var}; 
      rb_ivar_set(#{locals_accessor}self,#{intern_num tree[1]},#{result_var});
      }
      "
  else
    "_rb_ivar_set(#{locals_accessor}self,#{intern_num tree[1]},#{to_c tree[2]})"
  end
end

#to_c_ivar(tree) ⇒ Object



74
75
76
# File 'lib/fastruby/translator/modules/variable.rb', line 74

def to_c_ivar(tree)
  "rb_ivar_get(#{locals_accessor}self,#{intern_num tree[1]})"
end

#to_c_lasgn(tree, result_var = nil) ⇒ Object



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
# File 'lib/fastruby/translator/modules/variable.rb', line 197

def to_c_lasgn(tree, result_var = nil)
  code = "
      {
        #{to_c tree[2], result_var};
        #{locals_accessor}#{tree[1]} = #{result_var};
       }
       "

  if result_var
    if options[:validate_lvar_types]
      klass = @infer_lvar_map[tree[1]]
      if klass
        "
        {
          #{to_c tree[2], result_var};
          if (CLASS_OF(#{result_var})!=#{literal_value klass}) {
            #{_raise(literal_value(FastRuby::TypeMismatchAssignmentException), "Illegal assignment at runtime (type mismatch)")};
          }
          #{locals_accessor}#{tree[1]} = #{result_var};
        }
       "
      else
        code
      end
    else
      code
    end
  else
    if options[:validate_lvar_types]
      klass = @infer_lvar_map[tree[1]]
      if klass
        verify_type_function = proc { |name| "
          static VALUE #{name}(VALUE arg, void* pframe ) {
            if (CLASS_OF(arg)!=#{literal_value klass}) {
              #{_raise(literal_value(FastRuby::TypeMismatchAssignmentException), "Illegal assignment at runtime (type mismatch)")};
            }
            return arg;
          }
        "
        }
        
        "_lvar_assing(&#{locals_accessor}#{tree[1]}, #{anonymous_function(&verify_type_function)}(#{to_c tree[2]},pframe))"
      else
        "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
      end
    else
      "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
    end
  end
end

#to_c_lvar(tree) ⇒ Object



248
249
250
# File 'lib/fastruby/translator/modules/variable.rb', line 248

def to_c_lvar(tree)
  locals_accessor + tree[1].to_s
end