Beispiel #1
0
Datei: job.go Projekt: cznic/c
func (j *Job) union(n cc.Node, t cc.Type) {
	j.body("\n_ [0]struct{")
	m, _ := t.Members()
	for _, v := range m {
		j.body("\n%s", xc.Dict.S(v.Name))
		j.typ(n, v.Type)
		if t := v.Type; t.Kind() != cc.Union && t.Kind() != cc.Struct {
			j.body(" // %s", v.Type)
		}
	}
	j.body("\n}")
	j.body("\nUnion [%v]byte", t.SizeOf())
}
Beispiel #2
0
Datei: job.go Projekt: cznic/c
func (j *Job) initializerList(st cc.Type, n *cc.InitializerList) {
	j.typ(n, st)
	j.body("{\n")
	defer j.body("\n}")

	if st.Kind() == cc.Union {
		return
	}

	if st.Kind() == cc.Array {
		t := st.Element()
		for ; n != nil; n = n.InitializerList {
			if n.DesignationOpt != nil {
				todo(n)
			}

			j.initializer(t, n.Initializer)
			j.body(",")
			j.body("\n")
		}
		return
	}

	var m []cc.Member
	if st != nil {
		m, _ = st.Members()
	}
	i := 0
	for ; n != nil; n = n.InitializerList {
		if n.DesignationOpt != nil {
			todo(n)
		}

		var t cc.Type
		if i < len(m) {
			t = m[i].Type
		}
		j.initializer(t, n.Initializer)
		j.body(",")
		j.body("\n")
		i++
	}
}
Beispiel #3
0
Datei: job.go Projekt: cznic/c
func (j *Job) addTag2(t cc.Type) {
	for t.Kind() == cc.Ptr || t.Kind() == cc.Array {
		t = t.Element()
	}
	tag := t.Tag()
	var n cc.Node
	if tag != 0 {
		n = j.In.Declarations.Tags[tag].Node
	}
	d := t.Declarator()
	if (n != nil && j.ImportPath(n) != 0) || j.tags[tag] != nil {
		return
	}

	if tag != 0 {
		j.tags[tag] = d
	}
	m, _ := t.Members()
	for _, v := range m {
		j.addTag2(v.Type)
	}
}
Beispiel #4
0
Datei: job.go Projekt: cznic/c
func (j *Job) typ(n cc.Node, t cc.Type) {
	if t.Declarator().RawSpecifier().TypedefName() == idVaList {
		j.body("[]interface{}")
		return
	}

	stars := 0
	for t.Kind() == cc.Ptr {
		stars++
		t = t.Element()
	}
	stars0 := stars
	if (t.Kind() == cc.Void || t.Kind() == cc.Function) && stars > 0 {
		stars--
	}
	j.body("%s", strings.Repeat("*", stars))
	switch k := t.Kind(); k {
	case cc.Char, cc.Double, cc.Int, cc.Long, cc.LongDouble, cc.LongLong, cc.SChar,
		cc.Short, cc.UChar, cc.ULong, cc.ULongLong, cc.UInt, cc.UShort:
		j.body(" %s", j.Model.Items[k].More)
	case cc.Void:
		if stars0 == 0 {
			todo(n)
		}

		j.body(" unsafe.Pointer")
		j.imports[idUnsafe] = struct{}{}
	case cc.Struct, cc.Union:
		// Use tag if available.
		if tag := t.Tag(); tag != 0 {
			nm := mangleTag(tag, true)
			j.qualifier(j.In.Declarations.Tags[tag].Node)
			j.body(" %s", xc.Dict.S(nm))
			break
		}

		// Use typedef name if available.
		if tdn := t.Declarator().RawSpecifier().TypedefName(); tdn != 0 {
			nm := mangleIdent(tdn, true)
			j.qualifier(j.In.Declarations.Identifiers[tdn].Node)
			j.body(" %s", xc.Dict.S(nm))
			break
		}

		j.body(" struct {")
		switch m, incomplete := t.Members(); {
		case incomplete:
			j.body("// Incomplete type\n_ byte")
		case t.Kind() == cc.Union:
			j.union(n, t)
			var f func(cc.Type)
			f = func(t cc.Type) {
				j.addTag(t)
				m, _ := t.Members()
				for _, v := range m {
					f(v.Type)
				}
			}
		default:
			for _, v := range m {
				if v.Bits != 0 {
					j.bits(n, v)
					continue
				}

				j.addTag(v.Type)
				id := v.Name
				nm := mangleIdent(id, true)
				j.body("\n%s", xc.Dict.S(nm))
				j.typ(v.Declarator, v.Type)
				if t := v.Type; t.Kind() != cc.Union && t.Kind() != cc.Struct {
					j.body("// %s", v.Type)
				}
			}
		}
		j.body("\n}")
	case cc.Function:
		j.body(" func")
		j.signature(n, t)
	case cc.Array:
		j.arrayType(n, t)
	default:
		todo(n, t, t.Kind())
	}
}