Mercurial > projects > dstep
comparison scripts/dgen.rb @ 4:5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 05 Jul 2009 21:52:04 +0200 |
parents | d0162d8ca0f2 |
children | 9e67a1122e85 |
comparison
equal
deleted
inserted
replaced
3:d0162d8ca0f2 | 4:5a1a6afbfe3a |
---|---|
180 end | 180 end |
181 end | 181 end |
182 | 182 |
183 # This Struct represents a C/Objective-C header | 183 # This Struct represents a C/Objective-C header |
184 HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :c_constants, :d_constants, :d_constants_static_this, :defines, | 184 HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :c_constants, :d_constants, :d_constants_static_this, :defines, |
185 :enums, :functions, :c_functions, :function_wrappers, :imports, :path, :structs, :typedefs) do | 185 :enums, :functions, :c_functions, :function_pointers, :function_wrappers, :imports, :path, :structs, :typedefs) do |
186 def initialize | 186 def initialize |
187 self.name = "" | 187 self.name = "" |
188 self.cftypes = [] | 188 self.cftypes = [] |
189 self.constants = [] | 189 self.constants = [] |
190 self.defines = [] | 190 self.defines = [] |
191 self.enums = [] | 191 self.enums = [] |
192 self.framework = "" | 192 self.framework = "" |
193 self.functions = [] | 193 self.functions = [] |
194 self.c_functions = [] | 194 self.c_functions = [] |
195 self.function_pointers = [] | |
195 self.function_wrappers = [] | 196 self.function_wrappers = [] |
196 self.imports = [] | 197 self.imports = [] |
197 self.path = "" | 198 self.path = "" |
198 self.structs = [] | 199 self.structs = [] |
199 self.typedefs = [] | 200 self.typedefs = [] |
246 header.d_constants = d_constants unless file.constant.nil? | 247 header.d_constants = d_constants unless file.constant.nil? |
247 header.d_constants_static_this = d_constants_static_this unless file.constant.nil? | 248 header.d_constants_static_this = d_constants_static_this unless file.constant.nil? |
248 header.enums = enums(file.enum) unless file.enum.nil? | 249 header.enums = enums(file.enum) unless file.enum.nil? |
249 header.functions = functions(file.function) unless file.function.nil? | 250 header.functions = functions(file.function) unless file.function.nil? |
250 header.c_functions = c_functions unless file.function.nil? | 251 header.c_functions = c_functions unless file.function.nil? |
252 header.function_pointers = function_pointers(file.functionPointer) unless file.functionPointer.nil? | |
251 header.function_wrappers = function_wrappers unless file.function.nil? | 253 header.function_wrappers = function_wrappers unless file.function.nil? |
252 header.imports = imports(file.import, file.name, framework.name) unless file.import.nil? | 254 header.imports = imports(file.import, file.name, framework.name) unless file.import.nil? |
253 header.structs = structs(file.struct) unless file.struct.nil? | 255 header.structs = structs(file.struct) unless file.struct.nil? |
254 | 256 |
255 header.typedefs = typedefs(file.typedef) unless file.typedef.nil? | 257 header.typedefs = typedefs(file.typedef) unless file.typedef.nil? |
271 header.c_constants = c_constants unless file.constant.nil? | 273 header.c_constants = c_constants unless file.constant.nil? |
272 header.d_constants = d_constants unless file.constant.nil? | 274 header.d_constants = d_constants unless file.constant.nil? |
273 header.d_constants_static_this = d_constants_static_this unless file.constant.nil? | 275 header.d_constants_static_this = d_constants_static_this unless file.constant.nil? |
274 header.enums = enums(file.enum) unless file.enum.nil? | 276 header.enums = enums(file.enum) unless file.enum.nil? |
275 header.functions = functions(file.function) unless file.function.nil? | 277 header.functions = functions(file.function) unless file.function.nil? |
278 header.function_pointers = function_pointers(file.functionPointer) unless file.functionPointer.nil? | |
276 header.c_functions = c_functions unless file.function.nil? | 279 header.c_functions = c_functions unless file.function.nil? |
277 header.function_wrappers = function_wrappers unless file.function.nil? | 280 header.function_wrappers = function_wrappers unless file.function.nil? |
278 header.imports = imports(file.import, file.name) unless file.import.nil? | 281 header.imports = imports(file.import, file.name) unless file.import.nil? |
279 header.structs = structs(file.struct) unless file.struct.nil? | 282 header.structs = structs(file.struct) unless file.struct.nil? |
280 header.typedefs = typedefs(file.typedef) unless file.typedef.nil? | 283 header.typedefs = typedefs(file.typedef) unless file.typedef.nil? |
309 file << "\n" unless @d_constants.length > 0 || @function_wrappers.length > 0 | 312 file << "\n" unless @d_constants.length > 0 || @function_wrappers.length > 0 |
310 file << "import bindings = #{mod}_bindings".nl.nl(false) if @d_constants.length > 0 || @function_wrappers.length > 0 | 313 file << "import bindings = #{mod}_bindings".nl.nl(false) if @d_constants.length > 0 || @function_wrappers.length > 0 |
311 file << header.defines | 314 file << header.defines |
312 file << header.typedefs | 315 file << header.typedefs |
313 file << header.cftypes | 316 file << header.cftypes |
317 file << header.function_pointers | |
314 file << header.c_constants | 318 file << header.c_constants |
315 file << header.d_constants | 319 file << header.d_constants |
316 file << header.enums | 320 file << header.enums |
317 file << header.structs | 321 file << header.structs |
318 | 322 |
343 file << value.code.nl(false) | 347 file << value.code.nl(false) |
344 @templates.delete(template) | 348 @templates.delete(template) |
345 end | 349 end |
346 | 350 |
347 file << header.function_wrappers | 351 file << header.function_wrappers |
348 file << header.c_functions | 352 file << header.c_functions |
349 end | 353 end |
350 | 354 |
351 File.open(bindings_file_path, "w") do |file| | 355 File.open(bindings_file_path, "w") do |file| |
352 file << "module " | 356 file << "module " |
353 file << mod | 357 file << mod |
375 file << "\n" unless @d_constants.length > 0 || @function_wrappers.length > 0 | 379 file << "\n" unless @d_constants.length > 0 || @function_wrappers.length > 0 |
376 file << "import bindings = #{mod}_bindings".nl.nl(false) if @d_constants.length > 0 || @function_wrappers.length > 0 | 380 file << "import bindings = #{mod}_bindings".nl.nl(false) if @d_constants.length > 0 || @function_wrappers.length > 0 |
377 file << header.defines | 381 file << header.defines |
378 file << header.typedefs | 382 file << header.typedefs |
379 file << header.cftypes | 383 file << header.cftypes |
384 file << header.function_pointers | |
380 file << header.c_constants | 385 file << header.c_constants |
381 file << header.d_constants | 386 file << header.d_constants |
382 file << header.enums | 387 file << header.enums |
383 file << header.structs | 388 file << header.structs |
384 | 389 |
788 # Generates the D code for the function/method args | 793 # Generates the D code for the function/method args |
789 def args (args, variadic, method = false) | 794 def args (args, variadic, method = false) |
790 return "" if args.nil? | 795 return "" if args.nil? |
791 | 796 |
792 str = StringIO.new | 797 str = StringIO.new |
793 if variadic | 798 |
794 #p args | |
795 end | |
796 args.each do |arg| | 799 args.each do |arg| |
797 | 800 |
798 if method || arg.type != "@" | 801 if method || arg.type != "@" |
799 type = get_type(arg.type, arg.type64, arg.declaredType) | 802 type = get_type(arg.type, arg.type64, arg.declaredType) |
800 else | 803 else |
801 type = "id" | 804 type = "id" |
802 end | 805 end |
803 | 806 |
804 str << type | 807 str << type |
805 str << " " | 808 str << " " unless arg.name.nil? |
806 str << get_identifier(arg["name"]) | 809 str << get_identifier(arg["name"]) |
807 str << ", " | 810 str << ", " |
808 end | 811 end |
809 | 812 |
810 if variadic | 813 if variadic |
876 end | 879 end |
877 | 880 |
878 str << "}" | 881 str << "}" |
879 str.string | 882 str.string |
880 end | 883 end |
884 | |
885 def function_pointers (function_pointers) | |
886 return "" if function_pointers.length == 0 | |
887 | |
888 str = StringIO.new | |
889 | |
890 str << "extern (C)\n{".nl(false) | |
891 | |
892 function_pointers.each do |fp| | |
893 str << "alias ".indent | |
894 str << get_type(fp.returnValue[0].type, fp.returnValue[0].type64, fp.returnValue[0].declaredType) | |
895 str << " function (" | |
896 str << args(fp.arg, fp.variadic == "true") | |
897 str << ") " | |
898 str << get_identifier(fp.name).nl | |
899 end | |
900 | |
901 str << "}".nl(false).nl(false) | |
902 | |
903 str.string | |
904 end | |
881 | 905 |
882 def c_functions | 906 def c_functions |
883 return "" if @c_functions.length == 0 | 907 return "" if @c_functions.length == 0 |
884 | 908 |
885 str = StringIO.new | 909 str = StringIO.new |