mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Generate C local variables.
This commit is contained in:
1 parent
491985b34f
commit
ca78c343cb
4 files changed
+57
-3
No files matched your search
@@ -289,7 +289,7 @@ func writeFuncImpl(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Func) error
|
|||||||
}
|
}
|
||||||
if n.Public() {
|
if n.Public() {
|
||||||
fmt.Fprintf(b, "if (self->magic != PUFFS_MAGIC) {"+
|
fmt.Fprintf(b, "if (self->magic != PUFFS_MAGIC) {"+
|
||||||
"status = puffs_%s_error_constructor_not_called; goto cleanup0; }", pkgName)
|
"status = puffs_%s_error_constructor_not_called; goto cleanup0; }\n", pkgName)
|
||||||
cleanup0 = true
|
cleanup0 = true
|
||||||
}
|
}
|
||||||
} else if r := n.Receiver(); r != 0 {
|
} else if r := n.Receiver(); r != 0 {
|
||||||
@@ -299,6 +299,12 @@ func writeFuncImpl(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Func) error
|
|||||||
// TODO: check the args.
|
// TODO: check the args.
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Generate the local variables.
|
||||||
|
if err := writeVars(b, pkgName, m, n.Node(), 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
// TODO: generate the function body.
|
// TODO: generate the function body.
|
||||||
|
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
@@ -353,6 +359,38 @@ func writeField(b *bytes.Buffer, m *t.IDMap, n *a.Field) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uint32) error {
|
||||||
|
if depth > a.MaxBodyDepth {
|
||||||
|
return fmt.Errorf("cgen: body recursion depth too large")
|
||||||
|
}
|
||||||
|
depth++
|
||||||
|
|
||||||
|
if n.Kind() == a.KVar {
|
||||||
|
x := n.Var().XType()
|
||||||
|
if k := x.Name().Key(); k < t.Key(len(cTypeNames)) {
|
||||||
|
if s := cTypeNames[k]; s != "" {
|
||||||
|
fmt.Fprintf(b, "%s %s;\n", s, n.Var().Name().String(m))
|
||||||
|
// TODO: remove this hack for "unused variable" warning. In the
|
||||||
|
// long term, we want unused variables to fail noisily.
|
||||||
|
name := n.Var().Name().String(m)
|
||||||
|
fmt.Fprintf(b, "%s = 0; if (%s) {}\n", name, name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: fix this.
|
||||||
|
return fmt.Errorf("cgen: cannot convert Puffs type %q to C", x.String(m))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, l := range n.Raw().SubLists() {
|
||||||
|
for _, o := range l {
|
||||||
|
if err := writeVars(b, pkgName, m, o, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func format(rawSource *bytes.Buffer) ([]byte, error) {
|
func format(rawSource *bytes.Buffer) ([]byte, error) {
|
||||||
stdout := &bytes.Buffer{}
|
stdout := &bytes.Buffer{}
|
||||||
cmd := exec.Command("clang-format", "-style=Chromium")
|
cmd := exec.Command("clang-format", "-style=Chromium")
|
||||||
|
|||||||
+13
@@ -141,6 +141,19 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
|||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t bits;
|
||||||
|
bits = 0;
|
||||||
|
if (bits) {
|
||||||
|
}
|
||||||
|
uint32_t n_bits;
|
||||||
|
n_bits = 0;
|
||||||
|
if (n_bits) {
|
||||||
|
}
|
||||||
|
uint32_t width;
|
||||||
|
width = 0;
|
||||||
|
if (width) {
|
||||||
|
}
|
||||||
|
|
||||||
cleanup0:
|
cleanup0:
|
||||||
self->status = status;
|
self->status = status;
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
@@ -443,6 +443,9 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inne
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MaxBodyDepth is an advisory limit for a function body's recursion depth.
|
||||||
|
const MaxBodyDepth = 255
|
||||||
|
|
||||||
// Func is "func ID0.ID1(LHS)(RHS) { List1 }":
|
// Func is "func ID0.ID1(LHS)(RHS) { List1 }":
|
||||||
// - FlagsImpure is "ID1" vs "ID1!"
|
// - FlagsImpure is "ID1" vs "ID1!"
|
||||||
// - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure
|
// - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure
|
||||||
|
|||||||
+2
-2
@@ -27,8 +27,8 @@ func (q *checker) tcheckVars(n *a.Node) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, l := range n.Raw().SubLists() {
|
for _, l := range n.Raw().SubLists() {
|
||||||
for _, m := range l {
|
for _, o := range l {
|
||||||
if err := q.tcheckVars(m); err != nil {
|
if err := q.tcheckVars(o); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user