Class: FFIDB::Exporters::C

Inherits:
FFIDB::Exporter show all
Defined in:
lib/ffidb/exporters/c.rb

Overview

Code generator for the C programming language.

Direct Known Subclasses

Cpp, Go

Constant Summary collapse

SYMBOL_INDENT =
0
EXTERN_QUALIFIER =
'extern'

Instance Attribute Summary

Attributes inherited from FFIDB::Exporter

#options

Instance Method Summary collapse

Methods inherited from FFIDB::Exporter

#begin, #begin_library, #close, #debug?, #dlopen_paths_for, #emit, #export_enum, #export_function, #export_header, #export_struct, #export_symbol, #export_typedef, #export_union, #finish_library, for, #header?, #initialize, #verbose?

Constructor Details

This class inherits a constructor from FFIDB::Exporter

Instance Method Details

#_export_function(function, **kwargs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ffidb/exporters/c.rb', line 16

def _export_function(function, **kwargs)
  parameters = function.parameters.each_value.map do |p|
    p_type = case
      when p.type.function_pointer?
        p.type.to_s.sub('(*)', "(*#{p.name})")
      when self.options[:parameter_names] == false
        p.type.to_s.gsub(' *', '*')
      else "#{p.type.to_s.gsub(' *', '*')} #{p.name}"
    end
    p_type.gsub('const char *const[]', 'const char* const*') # FIXME
  end
  print ' '*self.symbol_indent if self.symbol_indent.nonzero?
  print self.extern_qualifier, ' ' if self.extern_qualifier
  if function.type.function_pointer?
    print function.type.to_s.sub('(*)', "(*#{function.name}(#{parameters.join(', ')}))")
  else
    print function.type, ' ', function.name, '('
    parameters.each_with_index do |p, i|
      print ', ' if i.nonzero?
      print p
    end
    print ')'
  end
  puts (self.options[:semicolon] == false ? '' : ';')
end

#finishObject



12
13
14
# File 'lib/ffidb/exporters/c.rb', line 12

def finish
  puts self.render_template('c.erb')
end