Permalink
Newer
Older
100644 67 lines (60 sloc) 1.98 KB
1
class TypesGenerator
2
3
def self.generate
4
# Type maps that maps different C types to the C type representations we use
5
type_map = {
6
"char" => :char,
7
"signed char" => :char,
8
"__signed char" => :char,
9
"unsigned char" => :uchar,
10
"short" => :short,
11
"signed short" => :short,
12
"signed short int" => :short,
13
"unsigned short" => :ushort,
14
"unsigned short int" => :ushort,
15
"int" => :int,
16
"signed int" => :int,
17
"unsigned int" => :uint,
18
"long" => :long,
19
"long int" => :long,
20
"signed long" => :long,
21
"signed long int" => :long,
22
"unsigned long" => :ulong,
23
"unsigned long int" => :ulong,
24
"long unsigned int" => :ulong,
25
"long long" => :long_long,
26
"long long int" => :long_long,
27
"signed long long" => :long_long,
28
"signed long long int" => :long_long,
29
"unsigned long long" => :ulong_long,
30
"unsigned long long int" => :ulong_long,
31
"char *" => :string,
32
"void *" => :pointer
33
}
34
35
typedefs = `echo "#include <sys/types.h>" | cpp | grep typedef `
36
code = ""
37
typedefs.each do |type|
38
# Ignore unions or structs
39
next if type =~ /union|struct/
40
# We don't handle typdefs like typedef unsigned int u_int16_t __attribute__ ((__mode__ (__HI__)));
41
# that occur on linux
42
next if type =~ /\(.*\)/
43
44
# strip off the starting typedef and ending ;
45
type.gsub!(/^(.*typedef\s*)/, "")
46
type.gsub!(/\s*;\s*$/,"")
47
48
parts = type.split(/\s+/)
49
final_type = parts.pop
50
def_type = parts.join(" ")
51
52
if type = type_map[def_type]
53
code << "rbx.platform.typedef.#{final_type} = #{type}\n"
54
type_map[final_type] = type_map[def_type]
55
else
56
# Fallback to an ordinary pointer if we don't know the type
57
if def_type =~ /\*/
58
code << "rbx.platform.typedef.#{final_type} = pointer\n"
59
type_map[final_type] = :pointer
60
end
61
end
62
end
63
code
64
end
65
end
66