diff options
Diffstat (limited to 'Parser')
-rwxr-xr-x | Parser/asdl_c.py | 86 |
1 files changed, 80 insertions, 6 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index fa71c23..d056c36 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -367,7 +367,12 @@ def has_sequence(types, doing_specialization): class StaticVisitor(PickleVisitor): - '''Very simple, always emit this static code''' + CODE = '''Very simple, always emit this static code. Overide CODE''' + + def visit(self, object): + self.emit(self.CODE, 0, reflow=False) + +class FreeUtilVisitor(StaticVisitor): CODE = '''static void free_seq_exprs(asdl_seq *seq) @@ -390,10 +395,6 @@ free_seq_stmts(asdl_seq *seq) } ''' - def visit(self, object): - self.emit(self.CODE, 0, reflow=False) - - class FreeVisitor(PickleVisitor): def func_begin(self, name, has_seq): @@ -483,6 +484,77 @@ class FreeVisitor(PickleVisitor): self.emit("free_%s((%s)%s);" % (field.type, ctype, value), depth) +class MarshalUtilVisitor(StaticVisitor): + + CODE = ''' +#define CHECKSIZE(BUF, OFF, MIN) { \\ + int need = *(OFF) + MIN; \\ + if (need >= PyString_GET_SIZE(*(BUF))) { \\ + int newsize = PyString_GET_SIZE(*(BUF)) * 2; \\ + if (newsize < need) \\ + newsize = need; \\ + if (_PyString_Resize((BUF), newsize) < 0) \\ + return 0; \\ + } \\ +} + +static int +marshal_write_int(PyObject **buf, int *offset, int x) +{ + char *s; + + CHECKSIZE(buf, offset, 4) + s = PyString_AS_STRING(*buf) + (*offset); + s[0] = (x & 0xff); + s[1] = (x >> 8) & 0xff; + s[2] = (x >> 16) & 0xff; + s[3] = (x >> 24) & 0xff; + *offset += 4; + return 1; +} + +static int +marshal_write_bool(PyObject **buf, int *offset, bool b) +{ + if (b) + marshal_write_int(buf, offset, 1); + else + marshal_write_int(buf, offset, 0); + return 1; +} + +static int +marshal_write_identifier(PyObject **buf, int *offset, identifier id) +{ + int l = PyString_GET_SIZE(id); + marshal_write_int(buf, offset, l); + CHECKSIZE(buf, offset, l); + memcpy(PyString_AS_STRING(*buf) + *offset, + PyString_AS_STRING(id), l); + *offset += l; + return 1; +} + +static int +marshal_write_string(PyObject **buf, int *offset, string s) +{ + int len = PyString_GET_SIZE(s); + marshal_write_int(buf, offset, len); + CHECKSIZE(buf, offset, len); + memcpy(PyString_AS_STRING(*buf) + *offset, + PyString_AS_STRING(s), len); + *offset += len; + return 1; +} + +static int +marshal_write_object(PyObject **buf, int *offset, object s) +{ + /* XXX */ + return 0; +} +''' + class MarshalFunctionVisitor(PickleVisitor): def func_begin(self, name, has_seq): @@ -563,6 +635,7 @@ class ChainOfVisitors: def visit(self, object): for v in self.visitors: v.visit(object) + v.emit("", 0) def main(srcfile): auto_gen_msg = '/* File automatically generated by %s */\n' % sys.argv[0] @@ -595,8 +668,9 @@ def main(srcfile): print >> f v = ChainOfVisitors(MarshalPrototypeVisitor(f), FunctionVisitor(f), - StaticVisitor(f), + FreeUtilVisitor(f), FreeVisitor(f), + MarshalUtilVisitor(f), MarshalFunctionVisitor(f), ) v.visit(mod) |