comparison scripts/dstepgen.rb @ 3:d0162d8ca0f2

Added support for function pointers in dstepgen
author Jacob Carlborg <doob@me.com>
date Sun, 05 Jul 2009 20:54:42 +0200
parents 9fd439a28ce3
children 5a1a6afbfe3a
comparison
equal deleted inserted replaced
2:9fd439a28ce3 3:d0162d8ca0f2
71 71
72 def method_missing (method, *args) 72 def method_missing (method, *args)
73 self.class.instance_eval do 73 self.class.instance_eval do
74 define_method(method) do |*args| 74 define_method(method) do |*args|
75 if args.length > 0 75 if args.length > 0
76 self[method[0 ... -1]] = args[0] 76 #self[method[0 ... -1]] = args[0]
77 self[eval(":#{method}"[0 ... -1])] = args[0] 77 self[eval(":#{method}"[0 ... -1])] = args[0]
78 else 78 else
79 result = self[method] 79 result = self[method]
80 return result unless result.nil? 80 return result unless result.nil?
81 self[eval(":#{method}")] 81 self[eval(":#{method}")]
99 self.path = "" 99 self.path = ""
100 end 100 end
101 end 101 end
102 102
103 # This Struct represents a C/Objective-C header 103 # This Struct represents a C/Objective-C header
104 HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :defines, :enums, :externs, :functions, 104 HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :defines, :enums, :externs, :functions, :function_pointers,
105 :imports, :inline_functions, :opaques, :path, :structs, :typedefs) do 105 :imports, :inline_functions, :opaques, :path, :structs, :typedefs) do
106 def initialize 106 def initialize
107 self.name = "" 107 self.name = ""
108 self.cftypes = [] 108 self.cftypes = []
109 self.constants = [] 109 self.constants = []
110 self.defines = [] 110 self.defines = []
111 self.enums = [] 111 self.enums = []
112 self.externs = [] 112 self.externs = []
113 self.framework = "" 113 self.framework = ""
114 self.functions = [] 114 self.functions = []
115 self.function_pointers = {}
115 self.imports = [] 116 self.imports = []
116 self.inline_functions = [] 117 self.inline_functions = []
117 self.opaques = [] 118 self.opaques = []
118 self.path = "" 119 self.path = ""
119 self.structs = [] 120 self.structs = []
144 @headers = [] 145 @headers = []
145 @classes = {} 146 @classes = {}
146 @protocols = {} 147 @protocols = {}
147 @categories = {} 148 @categories = {}
148 #@informal_protocols = {} 149 #@informal_protocols = {}
149 @function_pointer_types = {} 150 #@function_pointers = {}
150 @do_64bit = false 151 @do_64bit = false
151 end 152 end
152 153
153 def get_classes 154 def get_classes
154 @classes 155 @classes
204 205
205 header.enums << enum 206 header.enums << enum
206 end 207 end
207 end 208 end
208 209
209 def function_pointer_types (header) 210 def function_pointers (header)
210 re = /typedef\s+([\w\s]+)\s*\(\s*\*\s*(\w+)\s*\)\s*\(([^)]*)\)\s*;/ 211 re = /typedef\s+([\w\s]+)\s*\(\s*\*\s*(\w+)\s*\)\s*\(([^)]*)\)\s*;/
211 data = @cpp_result.scan(re) 212 data = @cpp_result.scan(re)
212 re = /typedef\s+([\w\s]+)\s*\(([^)]+)\)\s*;/ 213 re = /typedef\s+([\w\s]+)\s*\(([^)]+)\)\s*;/
213 data |= @cpp_result.scan(re).map do |m| 214 data |= @cpp_result.scan(re).map do |m|
214 ary = m[0].split(/(\w+)$/) 215 ary = m[0].split(/(\w+)$/)
228 x.strip 229 x.strip
229 end 230 end
230 end 231 end
231 232
232 type = "#{m[0]}(*)(#{args.join(', ')})" 233 type = "#{m[0]}(*)(#{args.join(', ')})"
233 @function_pointer_types[name] = type 234 header.function_pointers[name] = get_function_pointer(type)
234 end 235 end
236 end
237
238 def get_function_pointer (fp)
239 type = {}
240 re = /(.+)\(\*\)\((.+)\)/
241
242 if fp =~ /(.+)\(\*\)\((.+)\)/
243 m1 = $1
244 m2 = $2
245
246 type.const = m1 =~ /const/ ? true : false
247 m1.gsub!(/const/, "")
248 type.return_type = m1.strip
249 type.args = []
250
251 args = m2.split(",")
252
253 args.each do |arg|
254 const = arg =~ /const/ ? true : false
255 arg.gsub!(/const/, "")
256 arg.strip!
257 arg.gsub!(/\s*\*/, "*")
258 type.args << {:type => arg, :const => const}
259 end
260 end
261
262 type
235 end 263 end
236 264
237 def typedefs (header) 265 def typedefs (header)
238 re = /^\s*typedef\s+(.+)\s+([\w\*]+)\s*;$/ 266 re = /^\s*typedef\s+(.+)\s+([\w\*]+)\s*;$/
239 data = @cpp_result 267 data = @cpp_result
572 defines(header) 600 defines(header)
573 classes(header) 601 classes(header)
574 protocols(header) 602 protocols(header)
575 #informal_protocols(header) 603 #informal_protocols(header)
576 categories(header) 604 categories(header)
577 function_pointer_types(header) 605 function_pointers(header)
578 end 606 end
579 end 607 end
580 608
581 @headers.each do |header| 609 @headers.each do |header|
582 prepare(header.path) 610 prepare(header.path)
593 defines(header) 621 defines(header)
594 classes(header) 622 classes(header)
595 protocols(header) 623 protocols(header)
596 #informal_protocols(header) 624 #informal_protocols(header)
597 categories(header) 625 categories(header)
598 function_pointer_types(header) 626 function_pointers(header)
599 end 627 end
600 end 628 end
601 629
602 def get_var_info (str, multi = false) 630 def get_var_info (str, multi = false)
603 str.strip! 631 str.strip!
733 raise "Empty type (was '#{type}')" if t.empty? 761 raise "Empty type (was '#{type}')" if t.empty?
734 762
735 @stripped_return_type = t 763 @stripped_return_type = t
736 end 764 end
737 765
738 def function_pointer? (function_pointer_types) 766 # def function_pointer? (function_pointers)
739 type = @function_pointer_types[@stripped_return_type] || @stripped_return_type 767 # type = @function_pointers[@stripped_return_type] || @stripped_return_type
740 @function_pointer_type ||= FunctionPointerInfo.new_from_type(type) 768 # @function_pointer ||= FunctionPointerInfo.new_from_type(type)
741 end 769 # end
742 770
743 def <=>(x) 771 def <=>(x)
744 self.name <=> x.name 772 self.name <=> x.name
745 end 773 end
746 774
1136 function.args.each do |arg| 1164 function.args.each do |arg|
1137 types << arg.stripped_return_type 1165 types << arg.stripped_return_type
1138 end 1166 end
1139 end 1167 end
1140 1168
1169 header.function_pointers.each do |fp, value|
1170 types << value.return_type
1171
1172 value.args.each do |arg|
1173 types << arg.type
1174 end
1175 end
1176
1141 types 1177 types
1142 end 1178 end
1143 1179
1144 def collect_classes_types (enable_64bit) 1180 def collect_classes_types (enable_64bit)
1145 types = [] 1181 types = []
1239 function.resolved_type = resolved_types[i] unless enable_64bit 1275 function.resolved_type = resolved_types[i] unless enable_64bit
1240 function.resolved_type64 = resolved_types[i] if enable_64bit 1276 function.resolved_type64 = resolved_types[i] if enable_64bit
1241 i += 1 1277 i += 1
1242 1278
1243 function.args.each do |arg| 1279 function.args.each do |arg|
1280 arg.resolved_type = resolved_types[i] unless enable_64bit
1281 arg.resolved_type64 = resolved_types[i] if enable_64bit
1282 i += 1
1283 end
1284 end
1285
1286 header.function_pointers.each do |fp, value|
1287 value.resolved_type = resolved_types[i] unless enable_64bit
1288 value.resolved_type64 = resolved_types[i] if enable_64bit
1289 i += 1
1290
1291 value.args.each do |arg|
1244 arg.resolved_type = resolved_types[i] unless enable_64bit 1292 arg.resolved_type = resolved_types[i] unless enable_64bit
1245 arg.resolved_type64 = resolved_types[i] if enable_64bit 1293 arg.resolved_type64 = resolved_types[i] if enable_64bit
1246 i += 1 1294 i += 1
1247 end 1295 end
1248 end 1296 end
1447 end 1495 end
1448 1496
1449 xml.returnValue :declaredType => function.return_type, :type => function.resolved_type, :type64 => function.resolved_type64, :const => function.const, :typeModifier => function.type_modifier 1497 xml.returnValue :declaredType => function.return_type, :type => function.resolved_type, :type64 => function.resolved_type64, :const => function.const, :typeModifier => function.type_modifier
1450 end 1498 end
1451 end 1499 end
1500
1501 header.function_pointers.each do |fp, value|
1502 xml.functionPointer :name => fp do
1503 value.args.each do |arg|
1504 xml.arg :declaredType => arg.type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const
1505 end
1506
1507 xml.returnValue :declaredType => value.return_type, :type => value.resolved_type, :type64 => value.resolved_type64, :const => value.const
1508 end
1509 end
1452 end 1510 end
1453 end 1511 end
1454 1512
1455 def generate_classes (xml) 1513 def generate_classes (xml)
1456 @classes.each do |clazz, value| 1514 @classes.each do |clazz, value|