Ejemplo n.º 1
0
func (me *glFunc) makeCgo() {
	var (
		i  int
		fp *glFuncParam
	)
	me.clang.entry = sfmt("%s (APIENTRYP ptrgl%s)(", me.retType.c, me.name)
	for i, fp = range me.params {
		me.clang.entry += sfmt("%s %s", ustr.Ifs(ustr.IsOneOf(fp.kind, "array", "reference"), fp.typeRef.c+"*", fp.typeRef.c), fp.name)
		if i < len(me.params)-1 {
			me.clang.entry += ", "
		}
	}
	me.clang.entry += ");"

	if *flagSupports {
		me.clang.supporter = sfmt("int cancall%s() { return ((ptrgl%s == NULL) ? 0 : 1); }", me.name, me.name)
	}

	me.clang.wrapper = sfmt("%s call%s(", me.retType.c, me.name)
	for i, fp = range me.params {
		me.clang.wrapper += sfmt("%s %s", ustr.Ifs(ustr.IsOneOf(fp.kind, "array", "reference"), fp.typeRef.c+"*", fp.typeRef.c), fp.saneName)
		if i < len(me.params)-1 {
			me.clang.wrapper += ", "
		}
	}
	me.clang.wrapper += sfmt(") { %s (*ptrgl%s)(", ustr.Ifs(me.retType.c == "void", "", "return"), me.name)
	for i, fp = range me.params {
		me.clang.wrapper += fp.saneName
		if i < len(me.params)-1 {
			me.clang.wrapper += ", "
		}
	}
	me.clang.wrapper += "); }"
}
Ejemplo n.º 2
0
func (me *Attribute) makePkg(bag *PkgBag) {
	var safeName, typeName, tmp, key, defVal, impName string
	var defName = "Default"
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.AttributeFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		tmp = ustr.PrefixWithSep(impName, ".", idPrefix+"HasAttr_"+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
		if bag.attRefImps[me], bag.attsKeys[me] = impName, key; len(bag.attsCache[key]) == 0 {
			bag.attsCache[key] = tmp
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && (len(me.SimpleTypes) > 0) {
			typeName = me.SimpleTypes[0].Name.String()
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			typeName = bag.resolveQnameRef(typeName, "T", &impName)
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if len(bag.attsCache[key]) == 0 {
			tmp = idPrefix + "HasAttr_" + key
			bag.attsKeys[me] = key
			bag.attsCache[key] = tmp
			var td = bag.addType(me, tmp, "", me.Annotation)
			td.addField(me, safeName, typeName, ustr.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String()+",attr", me.Annotation)
			if isPt := bag.isParseType(typeName); len(defVal) > 0 {
				doc := sfmt("Returns the %v value for %v -- "+ustr.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
				if isPt {
					if PkgGen.ForceParseForDefaults {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("var x = new(%v); x.Set(%#v); return *x", typeName, defVal), doc)
					} else {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%v)", typeName, defVal), doc)
					}
				} else {
					td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%#v)", typeName, defVal), doc)
				}
			}
		} else {
			bag.attsKeys[me] = key
		}
	}
	me.elemBase.afterMakePkg(bag)
}
Ejemplo n.º 3
0
func (me *glPack) makeTryFunc(src *glPackSrc, fun *glFunc, glPrefix, errPrefix string) {
	src.addLn("//\tCalls %s%s() and yields the err returned by %sError(), if any.", glPrefix, fun.name, errPrefix)
	src.add("func (_ GlTry) %s(", fun.name)
	args, ret := "", ""
	for i, fp := range fun.params {
		fpgtPtr := strings.HasSuffix(fp.typeRef.g, "Ptr")
		tmp := strings.Replace(ustr.Ifs(ustr.IsOneOf(fp.kind, "array", "reference") && !fpgtPtr, "*"+fp.typeRef.g, fp.typeRef.g), "***", "**", -1)
		if pos := strings.LastIndex(tmp, "*"); pos < 0 {
			tmp = glPrefix + tmp
		} else {
			tmp = tmp[:pos+1] + glPrefix + tmp[pos+1:]
		}
		src.add("%s %s", fp.saneName, tmp)
		args += fp.saneName
		if i < len(fun.params)-1 {
			src.add(", ")
			args += ", "
		}
	}
	src.add(") (err error")
	if len(fun.retType.g) > 0 {
		ret = "tryRetVal__ = "
		src.add(", tryRetVal__ %v", glPrefix+fun.retType.g)
	}
	src.add(") {")
	src.addLn(`
	%s%s(%s)
	err = %sLastError("%s()")
	return`, ret, glPrefix+fun.name, args, errPrefix, glPrefix+fun.name)
	src.addLn("}\n")
}
Ejemplo n.º 4
0
func (me *glPack) makeEnums() {
	var src glPackSrc
	if *flagTry {
		src.addLn("import \"fmt\"")
	}
	for name, enum := range allEnums {
		if (!enum.legacy) && (len(enum.exts) == 0 || len(enum.ver) > 0 || uslice.StrHasAny(cfg.genExts, enum.exts...)) {
			me.enums[name] = enum
		}
	}
	src.addLn("const (")
	for _, enum := range me.enums {
		if len(enum.val) == 0 && me.enums[enum.ref] == nil {
			me.enums[enum.ref] = allEnums[enum.ref]
		}
	}
	for _, enum := range me.enums {
		src.addLn("\t%s = %s", enum.name, ustr.Ifs(len(enum.val) == 0, enum.ref, enum.val))
	}
	src.addLn(")\n")
	toUi := func(str string) (ui uint64) {
		ui, _ = strconv.ParseUint(str, 0, 64)
		return
	}
	enumsDone := map[uint64]bool{toUi("0xFFFFFFFFFFFFFFFF"): true}
	isEarlyEnum := func(name string) bool {
		return (strings.HasSuffix(name, "_SHADER") && strings.Count(name, "_") <= 2) || ustr.IsOneOf(name, "INVALID_ENUM", "INVALID_VALUE", "INVALID_OPERATION", "OUT_OF_MEMORY", "INVALID_FRAMEBUFFER_OPERATION", "STACK_OVERFLOW", "STACK_UNDERFLOW")
	}
	if *flagTry {
		src.addLn(`
//	Returns the name of the specified Enum. Not recommended in a real-time loop.
func (_ GlUtil) EnumName(enum Enum) (name string) {
	switch enum {`)
		for _, enum := range me.enums {
			if isEarlyEnum(enum.name) {
				enumsDone[toUi(enum.finalVal())] = true
				src.addLn("\tcase %s:\n\t\tname = \"GL_%s\"", enum.name, enum.name)
			}
		}
		for _, enum := range me.enums {
			if ui := toUi(enum.finalVal()); !enumsDone[ui] {
				enumsDone[ui] = true
				src.addLn("\tcase %s:\n\t\tname = \"GL_%s\"", enum.name, enum.name)
			}
		}
		src.addLn(`	default:
		name = fmt.Sprintf("GL_ENUM_%%v", enum)
	}
	return
}
`)
	}
	me.sources["enums"] = src
}
Ejemplo n.º 5
0
func (me *Union) makePkg(bag *PkgBag) {
	var memberTypes []string
	var rtn, rtnSafeName, safeName string
	me.elemBase.beforeMakePkg(bag)
	me.hasElemsSimpleType.makePkg(bag)
	memberTypes = ustr.Split(me.MemberTypes, " ")
	for _, st := range me.SimpleTypes {
		memberTypes = append(memberTypes, st.Name.String())
	}
	for _, mt := range memberTypes {
		rtn = bag.resolveQnameRef(mt, "T", nil)
		safeName, rtnSafeName = bag.safeName(ustr.PrependIf(bag.Stacks.CurSimpleType().Name.String(), "T")), bag.safeName(rtn)
		bag.ctd.addMethod(me, safeName, "To"+rtnSafeName, rtn, sfmt(ustr.Ifs(bag.isParseType(rtn), "var x = new(%v); x.Set(me.String()); return *x", "return %v(me)"), rtn), sfmt("%v is an XSD union-type of several types. This is a simple type conversion to %v, but keep in mind the actual value may or may not be a valid %v value.", safeName, rtnSafeName, rtnSafeName), me.Annotation)
	}
	me.elemBase.afterMakePkg(bag)
}
Ejemplo n.º 6
0
Archivo: makepkg.go Proyecto: tmc/xsd
func (me *declMethod) render(bag *PkgBag, dt *declType) {
	for _, ann := range me.Annotations {
		if ann != nil {
			//			ann.makePkg(bag)
			log.Println("Ann makePkg")
		}
	}
	bag.appendFmt(false, "//\t%s", me.Doc)
	rt := bag.rewriteTypeSpec(me.ReturnType)
	bag.appendFmt(true, "func (me %s) %s %s { %s }", bag.rewriteTypeSpec(me.ReceiverType), ustr.Ifs(strings.Contains(me.Name, "("), me.Name, me.Name+" ()"), rt, strings.Replace(me.Body, me.ReturnType, rt, -1))
}
Ejemplo n.º 7
0
func (me *Element) makePkg(bag *PkgBag) {
	var (
		safeName, typeName, valueType, tmp, key, defVal, impName string
		subEl                                                    *Element
	)
	asterisk, defName, doc := "", "Default", ""
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.ElementFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	me.hasElemComplexType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			tmp = ustr.PrefixWithSep(impName, ".", idPrefix+pref+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
			if bag.elemRefImps[me], bag.elemKeys[me] = impName, key; len(cache[key]) == 0 {
				cache[key] = tmp
			}
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && ((me.ComplexType != nil) || (len(me.SimpleTypes) > 0)) {
			if me.ComplexType != nil {
				asterisk, typeName = "*", me.ComplexType.Name.String()
			} else {
				typeName = me.SimpleTypes[0].Name.String()
			}
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			if typeName = bag.resolveQnameRef(typeName, "T", &impName); bag.Schema.RootSchema().globalComplexType(bag, typeName) != nil {
				asterisk = "*"
			}
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = bag.Stacks.FullName() + "_" + safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if valueType = bag.simpleContentValueTypes[typeName]; len(valueType) == 0 {
			valueType = typeName
		}
		isPt := bag.isParseType(valueType)
		if _, isChoice := me.Parent().(*Choice); isChoice && isPt {
			asterisk = "*"
		}
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			if tmp = idPrefix + pref + key; !bag.elemsWritten[tmp] {
				bag.elemsWritten[tmp], bag.elemKeys[me] = true, key
				cache[key] = tmp
				var td = bag.addType(me, tmp, "", me.Annotation)
				td.addField(me, ustr.Ifs(pref == "HasElems_", pluralize(safeName), safeName), ustr.Ifs(pref == "HasElems_", "[]"+asterisk+typeName, asterisk+typeName), ustr.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String(), me.Annotation)
				if me.parent == bag.Schema {
					for _, subEl = range bag.Schema.RootSchema().globalSubstitutionElems(me) {
						td.addEmbed(subEl, idPrefix+pref+bag.safeName(subEl.Name.String()), subEl.Annotation)
					}
				}
				if len(defVal) > 0 {
					doc = sfmt("Returns the %v value for %v -- "+ustr.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
					if isPt {
						if PkgGen.ForceParseForDefaults {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("var x = new(%v); x.Set(%#v); return *x", valueType, defVal), doc)
						} else {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%v)", valueType, defVal), doc)
						}
					} else {
						td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%#v)", valueType, defVal), doc)
					}
				}
			}
		}
	}
	me.elemBase.afterMakePkg(bag)
}
Ejemplo n.º 8
0
func (me *glPack) makeFuncs() {
	var (
		i       int
		fpgtPtr bool
		src     glPackSrc
		fun     *glFunc
		fp      *glFuncParam
	)
	isBeast := func() bool {
		b := (!strings.HasPrefix(fun.cat, "VERSION_")) && (!strings.HasSuffix(fun.name, fun.cat[:strings.Index(fun.cat, "_")])) && !strings.HasSuffix(fun.name, "EXT")
		return b
	}
	for _, fun = range allFuncs {
		if strings.Contains(fun.cat, " ") {
			println(fun.cat)
		}
		if strings.HasPrefix(fun.cat, "VERSION_") || uslice.StrHas(cfg.genExts, fun.cat) || isBeast() {
			me.funcs[fun.name] = fun
			if uslice.StrHas(cfg.genExts, fun.cat) {
				println("EXT:" + fun.name)
			}
		}
	}

	for _, fun = range me.funcs {
		src.addLn("//\tGLAPI Wiki: http://www.opengl.org/wiki/GLAPI/gl%s", fun.name)
		src.addLn("//\tSDK doc: http://www.opengl.org/sdk/docs/man/xhtml/gl%s.xml", fun.name)
		src.add("func %s(", fun.name)
		for i, fp = range fun.params {
			fpgtPtr = strings.HasSuffix(fp.typeRef.g, "Ptr")
			src.add("%s %s", fp.saneName, strings.Replace(ustr.Ifs(ustr.IsOneOf(fp.kind, "array", "reference") && !fpgtPtr, "*"+fp.typeRef.g, fp.typeRef.g), "***", "**", -1))
			if i < len(fun.params)-1 {
				src.add(", ")
			}
		}
		src.add(") %s {\n\t", fun.retType.g)
		if len(fun.retType.g) > 0 {
			src.add("return (%s)(", fun.retType.g)
		}
		src.add("C.call%s(", fun.name)
		for i, fp = range fun.params {
			fpgtPtr = fp.typeRef.g == "Ptr"
			if fp.typeRef.c == "GLchar* const" {
				src.add("(**C.GLchar)(unsafe.Pointer(%s))", fp.saneName)
			} else if fp.typeRef.c == "GLDEBUGPROC" {
				src.add("(*[0]byte)(%s)", fp.saneName)
			} else {
				src.add("(%s%s)(%s)", ustr.Ifs(ustr.IsOneOf(fp.kind, "array", "reference") && !fpgtPtr, "*", ""), ustr.Ifs(strings.HasSuffix(fp.typeRef.g, "Ptr"), "unsafe.Pointer", "C."+fp.typeRef.c), fp.saneName)
			}
			if i < len(fun.params)-1 {
				src.add(", ")
			}
		}
		src.add(")")
		if len(fun.retType.g) > 0 {
			src.add(")")
		}
		src.addLn("\n}\n")
		fun.makeCgo()
	}
	me.sources["funcs"] = src
}