Class: Cplus2Ruby::CppCodeGenerator

Inherits:
CodeGenerator show all
Defined in:
lib/cplus2ruby/cpp_code_generator.rb

Constant Summary collapse

DEFAULT_INCLUDES =
[:"stdlib.h", "ruby.h"]

Instance Method Summary collapse

Methods inherited from CodeGenerator

#all_methods_of, #all_properties_of, #args_convertable?, #arity, #initialize, #no_wrap?, #wrap?, #write_out

Constructor Details

This class inherits a constructor from Cplus2Ruby::CodeGenerator

Instance Method Details

#gen_class_declaration(klass) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 157

def gen_class_declaration(klass)
  if klass.superclass == Object
    sc = "RubyObject"
  else
    sc = klass.superclass.name
  end

  #
  # Do we have free or mark methods defined?
  #
  m = {}
  [:free, :mark].each do |kind|
    if not stmts_for_free_or_mark_method(klass, kind).empty?
      m[kind] = "virtual void __#{kind}__();"
    end
  end

  #
  # Write out property declarations and method signatures.
  #
  stmts = []

  all_properties_of(klass) {|name, options| 
    stmts << gen_property(name, options)
  }

  all_methods_of(klass) {|name, options|
    stmts << gen_method(nil, name, options, options[:inline], true)
  }
     
  if no_wrap?(klass)
    %[
      struct #{klass.name} 
      {
        #{stmts.join("; \n")};
      };
    ]
  else
    %[
      struct #{klass.name} : #{sc}
      {
        typedef #{sc} super;

        #{klass.name}();

        #{m[:free]}
        #{m[:mark]}
        
        #{stmts.join("; \n")};
      };
    ]
  end
end

#gen_class_impl(klass) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 211

def gen_class_impl(klass)
  # FIXME: helper_codes

  stmts = [] 

  if wrap?(klass)
    stmts << gen_constructor(klass)

    [:free, :mark].each {|kind| 
      stmts << gen_free_or_mark_method(klass, kind)
    }
  end

  all_methods_of(klass) do |name, options|
    next if options[:inline]
    stmts << gen_method(klass.name, name, options, true, false)
  end

  stmts.join("\n")
end

#gen_constructor(klass) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 62

def gen_constructor(klass)
  stmts = []
  all_properties_of(klass) do |name, options|
    init = @model.typing.lookup_entry(:init, options, options[:type])
    stmts << @model.typing.var_assgn("this->#{name}", init) unless init.nil?
  end
  #return "" if stmts.empty?
  %[
    #{klass.name}::#{klass.name}()
    {
      #{stmts.join(";\n")};
    }
  ]
end

#gen_free_or_mark_method(klass, kind) ⇒ Object

kind is either :free or :mark



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 50

def gen_free_or_mark_method(klass, kind)
  stmts = stmts_for_free_or_mark_method(klass, kind)
  return "" if stmts.empty?
  stmts.unshift("super::__#{kind}__()")
  %[
    void #{klass.name}::__#{kind}__()
    {
      #{stmts.join(";\n")};
    }
  ]
end

#gen_header_fileObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 232

def gen_header_file
  out = ""
  out << gen_includes(DEFAULT_INCLUDES + @model.includes)
  out << gen_rubyObject()
  out << gen_type_aliases(@model.typing.aliases)
  out << @model.code

  # forward class declarations
  @model.entities_ordered.each do |klass|
    out << "struct #{klass.name};\n"
  end

  # FIXME: helper_headers

  #
  # class declarations
  #
  @model.entities_ordered.each do |klass|
    out << gen_class_declaration(klass)
  end

  return out
end

#gen_impl_file(mod_name) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 256

def gen_impl_file(mod_name)
  out = ""
  out << %{#include "#{mod_name}.h"\n\n}

  #
  # class declarations
  #
  @model.entities_ordered.each do |klass|
    out << gen_class_impl(klass)
  end

  return out
end

#gen_include(inc) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 20

def gen_include(inc)
  "#include " + 
  case inc
  when Symbol
    %{<#{inc}>}
  when String
    %{"#{inc}"}
  else
    raise ArgumentError, "invalid header"
  end
end

#gen_includes(includes) ⇒ Object



32
33
34
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 32

def gen_includes(includes)
  includes.map {|inc| gen_include(inc) }.join("\n")
end

#gen_method(klassname, name, options, include_body, is_declaration) ⇒ Object



151
152
153
154
155
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 151

def gen_method(klassname, name, options, include_body, is_declaration)
  str = gen_method_sig(klassname, name, options, is_declaration)
  str << gen_method_body(klassname, name, options) if include_body
  str
end

#gen_method_body(klassname, name, options) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 107

def gen_method_body(klassname, name, options)
  if options[:stub]
    gen_stub_method(klassname, name, options)
  else
    "{\n" + (options[:body] || @model.settings[:default_body_when_nil]) + "}\n"
  end
end

#gen_method_sig(klassname, name, options, is_declaration) ⇒ Object

If klassname is nil, then it doesn’t include the

Klassname

prefix.

Doesn’t include the semicolon at the end.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 87

def gen_method_sig(klassname, name, options, is_declaration)
  args = options[:arguments].dup
  returns = args.delete(:returns) || "void"

  out = ""
  if is_declaration
    out << "static " if options[:static] 
    out << "inline " if options[:inline]
    out << "virtual " if options[:virtual]
  end
  out << @model.typing.var_decl(returns, "")
  out << " "

  s = args.map {|aname, atype| @model.typing.var_decl(atype, aname) }.join(", ")

  out << "#{klassname}::" if klassname
  out << "#{name}(#{s})"
  return out
end

#gen_property(name, options) ⇒ Object



77
78
79
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 77

def gen_property(name, options)
  @model.typing.var_decl(options[:type], name)
end

#gen_rubyObjectObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 6

def gen_rubyObject
  %[
    struct RubyObject {
      VALUE __obj__;
      RubyObject() { __obj__ = Qnil; }
      virtual ~RubyObject() {};
      static void __free(void *ptr) { ((RubyObject*)ptr)->__free__(); }
      static void __mark(void *ptr) { ((RubyObject*)ptr)->__mark__(); }
      virtual void __free__() { delete this; }
      virtual void __mark__() { }
    };
  ]
end

#gen_stub_method(klassname, name, options) ⇒ Object

Generates a C++ method that forwards the call to the Ruby method of the same name.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 119

def gen_stub_method(klassname, name, options)
  raise "Stub method with body is invalid!" if options[:body]

  args = options[:arguments].dup
  unless args_convertable?(args)
    raise "ERROR: Cannot convert stub method #{klassname}::#{name}"
  end

  returns = args.delete(:returns) || "void"

  out = ""
  out << "{\n"
  out << "VALUE __res__ = " if returns != 'void'

  # TODO: move rb_intern out
  call_args = ["@__obj__", %{rb_intern("#{name}")}, args.size] + 
    args.map {|n, k| @model.typing.convert(k, n, :c2ruby) }

  out << %{rb_funcall(#{call_args.join(', ')});}

  # check return type
  if returns != 'void' 
    out << @model.typing.convert(returns, '__res__', :ruby2c_checktype)
    retval = @model.typing.convert(returns, '__res__', :ruby2c) 
    out << "return #{retval};\n"
  end

  out << "}\n"

  return out
end

#gen_type_alias(from, to) ⇒ Object



36
37
38
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 36

def gen_type_alias(from, to)
  "typedef #{to} #{from};"
end

#gen_type_aliases(type_aliases) ⇒ Object

Type aliases is a hash in the form from => to.



43
44
45
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 43

def gen_type_aliases(type_aliases)
  type_aliases.map {|from, to| gen_type_alias(from, to) }.join("\n")
end

#write_files(mod_name) ⇒ Object



270
271
272
273
# File 'lib/cplus2ruby/cpp_code_generator.rb', line 270

def write_files(mod_name)
  write_out(mod_name + ".h", gen_header_file())
  write_out(mod_name + ".cc", gen_impl_file(mod_name))
end