# HG changeset patch # User Jacob Carlborg # Date 1246823524 -7200 # Node ID 5a1a6afbfe3a6115abfdcd1488969f47cc7217f9 # Parent d0162d8ca0f21794b831968d45e537921af7b455 Added support for function pointers and variadic function pointers to dgen and dstepgen diff -r d0162d8ca0f2 -r 5a1a6afbfe3a scripts/dgen.rb --- a/scripts/dgen.rb Sun Jul 05 20:54:42 2009 +0200 +++ b/scripts/dgen.rb Sun Jul 05 21:52:04 2009 +0200 @@ -182,7 +182,7 @@ # This Struct represents a C/Objective-C header HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :c_constants, :d_constants, :d_constants_static_this, :defines, - :enums, :functions, :c_functions, :function_wrappers, :imports, :path, :structs, :typedefs) do + :enums, :functions, :c_functions, :function_pointers, :function_wrappers, :imports, :path, :structs, :typedefs) do def initialize self.name = "" self.cftypes = [] @@ -192,6 +192,7 @@ self.framework = "" self.functions = [] self.c_functions = [] + self.function_pointers = [] self.function_wrappers = [] self.imports = [] self.path = "" @@ -248,6 +249,7 @@ header.enums = enums(file.enum) unless file.enum.nil? header.functions = functions(file.function) unless file.function.nil? header.c_functions = c_functions unless file.function.nil? + header.function_pointers = function_pointers(file.functionPointer) unless file.functionPointer.nil? header.function_wrappers = function_wrappers unless file.function.nil? header.imports = imports(file.import, file.name, framework.name) unless file.import.nil? header.structs = structs(file.struct) unless file.struct.nil? @@ -273,6 +275,7 @@ header.d_constants_static_this = d_constants_static_this unless file.constant.nil? header.enums = enums(file.enum) unless file.enum.nil? header.functions = functions(file.function) unless file.function.nil? + header.function_pointers = function_pointers(file.functionPointer) unless file.functionPointer.nil? header.c_functions = c_functions unless file.function.nil? header.function_wrappers = function_wrappers unless file.function.nil? header.imports = imports(file.import, file.name) unless file.import.nil? @@ -311,6 +314,7 @@ file << header.defines file << header.typedefs file << header.cftypes + file << header.function_pointers file << header.c_constants file << header.d_constants file << header.enums @@ -345,7 +349,7 @@ end file << header.function_wrappers - file << header.c_functions + file << header.c_functions end File.open(bindings_file_path, "w") do |file| @@ -377,6 +381,7 @@ file << header.defines file << header.typedefs file << header.cftypes + file << header.function_pointers file << header.c_constants file << header.d_constants file << header.enums @@ -790,9 +795,7 @@ return "" if args.nil? str = StringIO.new - if variadic - #p args - end + args.each do |arg| if method || arg.type != "@" @@ -802,7 +805,7 @@ end str << type - str << " " + str << " " unless arg.name.nil? str << get_identifier(arg["name"]) str << ", " end @@ -879,6 +882,27 @@ str.string end + def function_pointers (function_pointers) + return "" if function_pointers.length == 0 + + str = StringIO.new + + str << "extern (C)\n{".nl(false) + + function_pointers.each do |fp| + str << "alias ".indent + str << get_type(fp.returnValue[0].type, fp.returnValue[0].type64, fp.returnValue[0].declaredType) + str << " function (" + str << args(fp.arg, fp.variadic == "true") + str << ") " + str << get_identifier(fp.name).nl + end + + str << "}".nl(false).nl(false) + + str.string + end + def c_functions return "" if @c_functions.length == 0 diff -r d0162d8ca0f2 -r 5a1a6afbfe3a scripts/dstepgen.rb --- a/scripts/dstepgen.rb Sun Jul 05 20:54:42 2009 +0200 +++ b/scripts/dstepgen.rb Sun Jul 05 21:52:04 2009 +0200 @@ -208,9 +208,9 @@ end def function_pointers (header) - re = /typedef\s+([\w\s]+)\s*\(\s*\*\s*(\w+)\s*\)\s*\(([^)]*)\)\s*;/ + re = /typedef\s+([\w\s\*]+)\s*\(\s*\*\s*(\w+)\s*\)\s*\(([^)]*)\)\s*;/ data = @cpp_result.scan(re) - re = /typedef\s+([\w\s]+)\s*\(([^)]+)\)\s*;/ + re = /typedef\s+([\w\s\*]+)\s*\(([^)]+)\)\s*;/ data |= @cpp_result.scan(re).map do |m| ary = m[0].split(/(\w+)$/) ary[1] << " *" @@ -219,8 +219,10 @@ end data.each do |m| + variadic = false name = m[1] args = m[2].split(",").map do |x| + variadic = x =~ /\.\.\./ ? true : false if x.include?(" ") ptr = x.sub!(/\[\]\s*$/, "") x = x.sub(/\w+\s*$/, "").strip @@ -230,8 +232,13 @@ end end + args.delete("...") do + args + end + type = "#{m[0]}(*)(#{args.join(', ')})" header.function_pointers[name] = get_function_pointer(type) + header.function_pointers[name].variadic = variadic end end @@ -1499,7 +1506,7 @@ end header.function_pointers.each do |fp, value| - xml.functionPointer :name => fp do + xml.functionPointer :name => fp, :variadic => value.variadic do value.args.each do |arg| xml.arg :declaredType => arg.type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const end