Пример #1
0
func ProcessFunctionInfo(fi *gi.FunctionInfo, container *gi.BaseInfo) {
	var fullnm string
	flags := fi.Flags()
	name := fi.Name()

	// --- header
	fb := NewFunctionBuilder(fi)
	printf("func ")
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		// add receiver if it's a method
		printf("(this0 %s) ",
			GoTypeForInterface(container, TypePointer|TypeReceiver))
		fullnm = container.Name() + "."
	}
	switch {
	case flags&gi.FUNCTION_IS_CONSTRUCTOR != 0:
		// special names for constructors
		name = fmt.Sprintf("New%s%s", container.Name(), CtorSuffix(name))
	case flags&gi.FUNCTION_IS_METHOD == 0 && container != nil:
		name = fmt.Sprintf("%s%s", container.Name(), LowerCaseToCamelCase(name))
	default:
		name = fmt.Sprintf("%s", LowerCaseToCamelCase(name))
	}
	fullnm += name
	name = Rename(fullnm, name)
	printf("%s(", name)
	for i, arg := range fb.Args {
		printf("%s0 %s", arg.ArgInfo.Name(), GoType(arg.TypeInfo, TypeNone))
		if i != len(fb.Args)-1 {
			printf(", ")
		}
	}
	printf(")")
	switch len(fb.Rets) {
	case 0:
		// do nothing if there are not return values
	case 1:
		if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 {
			// override return types for constructors, We can't
			// return generic widget here as C does. Go's type
			// system is stronger.
			printf(" %s",
				GoTypeForInterface(container, TypePointer|TypeReturn))
			break
		}
		if fb.Rets[0].Index == -2 {
			printf(" error")
		} else {
			printf(" %s", GoType(fb.Rets[0].TypeInfo, TypeReturn))
		}
	default:
		printf(" (")
		for i, ret := range fb.Rets {
			if ret.Index == -2 {
				// special error type in Go to represent GError
				printf("error")
				continue
			}
			printf(GoType(ret.TypeInfo, TypeReturn))
			if i != len(fb.Rets)-1 {
				printf(", ")
			}
		}
		printf(")")
	}
	printf(" {\n")

	// --- body stage 1 (Go to C conversions)

	// var declarations
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		printf("\tvar this1 %s\n", CgoTypeForInterface(container, TypePointer))
	}
	for _, arg := range fb.Args {
		printf("\tvar %s1 %s\n", arg.ArgInfo.Name(),
			CgoType(arg.TypeInfo, TypeNone))
		if al := arg.TypeInfo.ArrayLength(); al != -1 {
			arg := fb.OrigArgs[al]
			printf("\tvar %s1 %s\n", arg.Name(),
				CgoType(arg.Type(), TypeNone))
		}
	}

	for _, ret := range fb.Rets {
		if ret.Index == -1 {
			continue
		}

		if ret.Index == -2 {
			printf("\tvar err1 *C.GError\n")
			continue
		}

		if ret.ArgInfo.Direction() == gi.DIRECTION_INOUT {
			continue
		}

		printf("\tvar %s1 %s\n", ret.ArgInfo.Name(),
			CgoType(ret.TypeInfo, TypeNone))
		if al := ret.TypeInfo.ArrayLength(); al != -1 {
			arg := fb.OrigArgs[al]
			printf("\tvar %s1 %s\n", arg.Name(),
				CgoType(arg.Type(), TypeNone))
		}
	}

	// conversions
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		conv := GoToCgoForInterface(container, "this0", "this1", ConvPointer)
		printf("%s", PrintLinesWithIndent(conv))
	}
	for _, arg := range fb.Args {
		nm := arg.ArgInfo.Name()
		conv := GoToCgo(arg.TypeInfo, nm+"0", nm+"1", ConvNone)
		printf("%s", PrintLinesWithIndent(conv))

		// register callback in the global map
		if arg.TypeInfo.Tag() == gi.TYPE_TAG_INTERFACE {
			bi := arg.TypeInfo.Interface()
			if bi.Type() == gi.INFO_TYPE_CALLBACK {
				if arg.ArgInfo.Scope() != gi.SCOPE_TYPE_CALL {
					printf("\t%sHolder.Grab(%s1)\n", Config.Sys.GNS, nm)
				}
			}
		}

		// array length
		if len := arg.TypeInfo.ArrayLength(); len != -1 {
			lenarg := fb.OrigArgs[len]
			conv = GoToCgo(lenarg.Type(),
				"len("+nm+"0)", lenarg.Name()+"1", ConvNone)
			printf("\t%s\n", conv)
		}
	}

	// --- body stage 2 (the function call)
	printf("\t")
	if fb.HasReturnValue() {
		printf("ret1 := ")
	}

	userdata, destroy, scope := fb.HasClosureArgument()
	printf("C.")
	if scope != gi.SCOPE_TYPE_INVALID {
		printf("_")
	}
	printf("%s(", fi.Symbol())
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		printf("this1")
		if len(fb.OrigArgs) > 0 {
			printf(", ")
		}
	}
	for i, ri, n := 0, 0, len(fb.OrigArgs); i < n; i++ {
		if i == userdata || i == destroy {
			continue
		}

		oarg := fb.OrigArgs[i]

		var arg string
		dir := oarg.Direction()
		if dir == gi.DIRECTION_INOUT || dir == gi.DIRECTION_OUT {
			arg = fmt.Sprintf("&%s1", oarg.Name())
		} else {
			arg = fmt.Sprintf("%s1", oarg.Name())
		}

		if ri != 0 {
			printf(", ")
		}
		printf("%s", arg)
		ri++
	}
	if flags&gi.FUNCTION_THROWS != 0 {
		printf(", &err1")
	}
	printf(")\n")

	// --- body stage 3 (C to Go conversions)

	// var declarations
	for _, ret := range fb.Rets {
		switch ret.Index {
		case -1:
			if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 {
				printf("\tvar ret2 %s\n",
					GoTypeForInterface(container, TypePointer|TypeReturn))
			} else {
				printf("\tvar ret2 %s\n", GoType(ret.TypeInfo, TypeReturn))
			}
		case -2:
			printf("\tvar err2 error\n")
		default:
			printf("\tvar %s2 %s\n", ret.ArgInfo.Name(),
				GoType(ret.TypeInfo, TypeReturn))
		}
	}

	// conversions
	for _, ret := range fb.Rets {
		if ret.Index == -2 {
			printf("\tif err1 != nil {\n")
			printf("\t\terr2 = errors.New(C.GoString(((*_GError)(unsafe.Pointer(err1))).message))\n")
			printf("\t\tC.g_error_free(err1)\n")
			printf("\t}\n")
			continue
		}

		var nm string
		if ret.Index == -1 {
			nm = "ret"
		} else {
			nm = ret.ArgInfo.Name()
		}

		// array length
		if len := ret.TypeInfo.ArrayLength(); len != -1 {
			lenarg := fb.OrigArgs[len]
			printf("\t%s2 = make(%s, %s)\n",
				nm, GoType(ret.TypeInfo, TypeReturn),
				lenarg.Name()+"1")
		}

		var ownership gi.Transfer
		if ret.Index == -1 {
			ownership = fi.CallerOwns()
		} else {
			ownership = ret.ArgInfo.OwnershipTransfer()
			if ret.ArgInfo.Direction() == gi.DIRECTION_INOUT {
				// not sure if it's true in all cases, but so far
				ownership = gi.TRANSFER_NOTHING
			}
		}
		var conv string
		if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 && ret.Index == -1 {
			conv = CgoToGoForInterface(container, "ret1", "ret2",
				ConvPointer|OwnershipToConvFlags(ownership))
		} else {
			conv = CgoToGo(ret.TypeInfo, nm+"1", nm+"2",
				OwnershipToConvFlags(ownership))
		}
		printf("%s", PrintLinesWithIndent(conv))
	}

	// --- body stage 4 (return)
	if len(fb.Rets) == 0 {
		printf("}\n")
		return
	}

	printf("\treturn ")
	for i, ret := range fb.Rets {
		var nm string
		switch ret.Index {
		case -1:
			nm = "ret2"
		case -2:
			nm = "err2"
		default:
			nm = ret.ArgInfo.Name() + "2"
		}
		if i != 0 {
			printf(", ")
		}
		printf(nm)
	}

	printf("\n}\n")
}
Пример #2
0
func (this *binding_generator) process_function_info(fi *gi.FunctionInfo) {
	p := printer_to(&this.go_bindings)

	var fullnm string
	flags := fi.Flags()
	name := fi.Name()

	container := fi.Container()

	// --- header
	fb := new_function_builder(fi)
	p("func ")
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		// add receiver if it's a method
		p("(this0 %s) ", go_type_for_interface(container, type_pointer|type_receiver))
		fullnm = container.Name() + "."
	}
	switch {
	case flags&gi.FUNCTION_IS_CONSTRUCTOR != 0:
		// special names for constructors
		name = fmt.Sprintf("New%s%s", container.Name(), ctor_suffix(name))
	case flags&gi.FUNCTION_IS_METHOD == 0 && container != nil:
		name = fmt.Sprintf("%s%s", container.Name(), lower_case_to_camel_case(name))
	default:
		name = fmt.Sprintf("%s", lower_case_to_camel_case(name))
	}
	fullnm += name
	name = config.rename(fullnm, name)
	p("%s(", name)
	for i, arg := range fb.args {
		if i != 0 {
			p(", ")
		}
		p("%s0 %s", arg.arg_info.Name(), go_type(arg.type_info, type_none))
	}
	p(")")
	switch len(fb.rets) {
	case 0:
		// do nothing if there are not return values
	case 1:
		if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 {
			// override return types for constructors, We can't
			// return generic widget here as C does. Go's type
			// system is stronger.
			p(" %s", go_type_for_interface(container, type_pointer|type_return))
			break
		}
		if fb.rets[0].index == -2 {
			p(" error")
		} else {
			p(" %s", go_type(fb.rets[0].type_info, type_return))
		}
	default:
		p(" (")
		for i, ret := range fb.rets {
			if ret.index == -2 {
				// special error type in Go to represent GError
				p("error")
				continue
			}
			p(go_type(ret.type_info, type_return))
			if i != len(fb.rets)-1 {
				p(", ")
			}
		}
		p(")")
	}
	p(" {\n")

	// --- body stage 1 (Go to C conversions)

	// var declarations
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		p("\tvar this1 %s\n", cgo_type_for_interface(container, type_pointer))
	}
	for _, arg := range fb.args {
		p("\tvar %s1 %s\n", arg.arg_info.Name(), cgo_type(arg.type_info, type_none))
		if al := arg.type_info.ArrayLength(); al != -1 {
			arg := fb.orig_args[al]
			p("\tvar %s1 %s\n", arg.Name(), cgo_type(arg.Type(), type_none))
		}
	}

	for _, ret := range fb.rets {
		if ret.index == -1 {
			continue
		}

		if ret.index == -2 {
			p("\tvar err1 *C.GError\n")
			continue
		}

		if ret.arg_info.Direction() == gi.DIRECTION_INOUT {
			continue
		}

		p("\tvar %s1 %s\n", ret.arg_info.Name(), cgo_type(ret.type_info, type_none))
		if al := ret.type_info.ArrayLength(); al != -1 {
			arg := fb.orig_args[al]
			p("\tvar %s1 %s\n", arg.Name(), cgo_type(arg.Type(), type_none))
		}
	}

	// conversions
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		conv := go_to_cgo_for_interface(container, "this0", "this1", conv_pointer)
		p("%s", print_lines_with_indent(conv))
	}
	for _, arg := range fb.args {
		nm := arg.arg_info.Name()
		conv := go_to_cgo(arg.type_info, nm+"0", nm+"1", conv_none)
		p("%s", print_lines_with_indent(conv))

		// register callback in the global map
		if arg.type_info.Tag() == gi.TYPE_TAG_INTERFACE {
			bi := arg.type_info.Interface()
			if bi.Type() == gi.INFO_TYPE_CALLBACK {
				if arg.arg_info.Scope() != gi.SCOPE_TYPE_CALL {
					p("\t%sHolder.Grab(%s1)\n", config.gns, nm)
				}
			}
		}

		// array length
		if len := arg.type_info.ArrayLength(); len != -1 {
			lenarg := fb.orig_args[len]
			conv = go_to_cgo(lenarg.Type(), "len("+nm+"0)", lenarg.Name()+"1", conv_none)
			p("\t%s\n", conv)
		}
	}

	// --- body stage 2 (the function call)
	p("\t")
	if fb.has_return_value() {
		p("ret1 := ")
	}

	userdata, destroy, scope := fb.has_closure_argument()
	p("C.")
	if scope != gi.SCOPE_TYPE_INVALID {
		p("_")
	}
	p("%s(", fi.Symbol())
	if flags&gi.FUNCTION_IS_METHOD != 0 {
		p("this1")
		if len(fb.orig_args) > 0 {
			p(", ")
		}
	}
	for i, ri, n := 0, 0, len(fb.orig_args); i < n; i++ {
		if i == userdata || i == destroy {
			continue
		}

		oarg := fb.orig_args[i]

		var arg string
		dir := oarg.Direction()
		if dir == gi.DIRECTION_INOUT || dir == gi.DIRECTION_OUT {
			arg = fmt.Sprintf("&%s1", oarg.Name())
		} else {
			arg = fmt.Sprintf("%s1", oarg.Name())
		}

		if ri != 0 {
			p(", ")
		}
		p("%s", arg)
		ri++
	}
	if flags&gi.FUNCTION_THROWS != 0 {
		p(", &err1")
	}
	p(")\n")

	// --- body stage 3 (C to Go conversions)

	// var declarations
	for _, ret := range fb.rets {
		switch ret.index {
		case -1:
			if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 {
				p("\tvar ret2 %s\n", go_type_for_interface(container, type_pointer|type_return))
			} else {
				p("\tvar ret2 %s\n", go_type(ret.type_info, type_return))
			}
		case -2:
			p("\tvar err2 error\n")
		default:
			p("\tvar %s2 %s\n", ret.arg_info.Name(), go_type(ret.type_info, type_return))
		}
	}

	// conversions
	for _, ret := range fb.rets {
		if ret.index == -2 {
			p("\tif err1 != nil {\n")
			p("\t\terr2 = errors.New(C.GoString(((*_GError)(unsafe.Pointer(err1))).message))\n")
			p("\t\tC.g_error_free(err1)\n")
			p("\t}\n")
			continue
		}

		var nm string
		if ret.index == -1 {
			nm = "ret"
		} else {
			nm = ret.arg_info.Name()
		}

		// array length
		if len := ret.type_info.ArrayLength(); len != -1 {
			lenarg := fb.orig_args[len]
			p("\t%s2 = make(%s, %s)\n",
				nm, go_type(ret.type_info, type_return),
				lenarg.Name()+"1")
		}

		var ownership gi.Transfer
		if ret.index == -1 {
			ownership = fi.CallerOwns()
		} else {
			ownership = ret.arg_info.OwnershipTransfer()
			if ret.arg_info.Direction() == gi.DIRECTION_INOUT {
				// not sure if it's true in all cases, but so far
				ownership = gi.TRANSFER_NOTHING
			}
		}
		var conv string
		if flags&gi.FUNCTION_IS_CONSTRUCTOR != 0 && ret.index == -1 {
			conv = cgo_to_go_for_interface(container, "ret1", "ret2",
				conv_pointer|ownership_to_conv_flags(ownership))
		} else {
			conv = cgo_to_go(ret.type_info, nm+"1", nm+"2",
				ownership_to_conv_flags(ownership))
		}
		p("%s", print_lines_with_indent(conv))
	}

	// --- body stage 4 (return)
	if len(fb.rets) == 0 {
		p("}\n")
		return
	}

	p("\treturn ")
	for i, ret := range fb.rets {
		var nm string
		switch ret.index {
		case -1:
			nm = "ret2"
		case -2:
			nm = "err2"
		default:
			nm = ret.arg_info.Name() + "2"
		}
		if i != 0 {
			p(", ")
		}
		p(nm)
	}

	p("\n}\n")
}