Lines Matching refs:emit
87 self.emit("_Py_IDENTIFIER(%s);" % name, 0)
90 def emit(self, s, depth, reflow=True): member in EmitVisitor
124 self.emit(s, depth)
125 self.emit("", depth)
130 self.emit(s, depth)
131 self.emit("", depth)
136 self.emit(s, depth)
137 self.emit("", depth)
155 def emit(s, depth=depth): function
156 self.emit(s % sys._getframe(1).f_locals, depth)
162 emit("enum _%(name)s_kind {" + ", ".join(enum) + "};")
164 emit("struct _%(name)s {")
165 emit("enum _%(name)s_kind kind;", depth + 1)
166 emit("union {", depth + 1)
169 emit("} v;", depth + 1)
174 emit("%s %s;" % (type, field.name), depth + 1);
175 emit("};")
176 emit("")
180 self.emit("struct {", depth)
183 self.emit("} %s;" % cons.name, depth)
184 self.emit("", depth)
193 self.emit("asdl_int_seq *%(name)s;" % locals(), depth)
195 self.emit("asdl_seq *%(name)s;" % locals(), depth)
197 self.emit("%(ctype)s %(name)s;" % locals(), depth)
200 self.emit("struct _%(name)s {" % locals(), depth)
207 self.emit("%s %s;" % (type, field.name), depth + 1);
208 self.emit("};", depth)
209 self.emit("", depth)
273 self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
275 self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
288 def emit(s, depth=0, reflow=True): function
289 self.emit(s, depth, reflow)
296 self.emit("%s" % ctype, 0)
297 emit("%s(%s)" % (name, argstr))
298 emit("{")
299 emit("%s p;" % ctype, 1)
302 emit("if (!%s) {" % argname, 1)
303 emit("PyErr_SetString(PyExc_ValueError,", 2)
305 emit(' "%s");' % msg,
307 emit('return NULL;', 2)
308 emit('}', 1)
310 emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
311 emit("if (!p)", 1)
312 emit("return NULL;", 2)
317 emit("return p;", 1)
318 emit("}")
319 emit("")
322 def emit(s, depth=0, reflow=True): function
323 self.emit(s, depth, reflow)
324 emit("p->kind = %s_kind;" % name, 1)
326 emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
328 emit("p->%s = %s;" % (argname, argname), 1)
331 def emit(s, depth=0, reflow=True): function
332 self.emit(s, depth, reflow)
334 emit("p->%s = %s;" % (argname, argname), 1)
336 emit("p->%s = %s;" % (argname, argname), 1)
364 self.emit(code % (name, get_c_type(name)), 0)
372 self.emit("int", 0)
373 self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
374 self.emit("{", 0)
375 self.emit("int isinstance;", 1)
376 self.emit("", 0)
379 self.emit("", 0)
383 self.emit(format % error, 1, reflow=False)
385 self.emit("failed:", 1)
386 self.emit("Py_XDECREF(tmp);", 1)
387 self.emit("return 1;", 1)
388 self.emit("}", 0)
389 self.emit("", 0)
396 self.emit(line % (t.name,), 1)
397 self.emit("if (isinstance == -1) {", 1)
398 self.emit("return 1;", 2)
399 self.emit("}", 1)
400 self.emit("if (isinstance) {", 1)
401 self.emit("*out = %s;" % t.name, 2)
402 self.emit("return 0;", 2)
403 self.emit("}", 1)
411 self.emit("PyObject *tmp = NULL;", 1)
414 self.emit("", 0)
416 self.emit("if (obj == Py_None) {", 1)
417 self.emit("*out = NULL;", 2)
418 self.emit("return 0;", 2)
419 self.emit("}", 1)
424 self.emit(line % (t.name,), 1)
425 self.emit("if (isinstance == -1) {", 1)
426 self.emit("return 1;", 2)
427 self.emit("}", 1)
428 self.emit("if (isinstance) {", 1)
431 self.emit("", 0)
435 self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
436 self.emit("if (*out == NULL) goto failed;", 2)
437 self.emit("return 0;", 2)
438 self.emit("}", 1)
443 self.emit("%s %s;" % (ctype, a.name), 1)
453 self.emit("int", 0)
454 self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
455 self.emit("{", 0)
456 self.emit("PyObject* tmp = NULL;", 1)
461 self.emit("", 0)
468 self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1)
469 self.emit("return 0;", 1)
470 self.emit("failed:", 0)
471 self.emit("Py_XDECREF(tmp);", 1)
472 self.emit("return 1;", 1)
473 self.emit("}", 0)
474 self.emit("", 0)
480 self.emit("asdl_int_seq* %s;" % field.name, depth)
482 self.emit("asdl_seq* %s;" % field.name, depth)
485 self.emit("%s %s;" % (ctype, field.name), depth)
500 self.emit("if (_PyObject_LookupAttrId(obj, &PyId_%s, &tmp) < 0) {" % field.name, depth)
501 self.emit("return 1;", depth+1)
502 self.emit("}", depth)
504 self.emit("if (tmp == NULL) {", depth)
507 self.emit(format % message, depth+1, reflow=False)
508 self.emit("return 1;", depth+1)
510 self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
511 self.emit("Py_CLEAR(tmp);", depth+1)
513 self.emit("%s = 0;" % field.name, depth+1)
515 self.emit("%s = NULL;" % field.name, depth+1)
518 self.emit("}", depth)
519 self.emit("else {", depth)
521 self.emit("int res;", depth+1)
523 self.emit("Py_ssize_t len;", depth+1)
524 self.emit("Py_ssize_t i;", depth+1)
525 self.emit("if (!PyList_Check(tmp)) {", depth+1)
526 self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
530 self.emit("goto failed;", depth+2)
531 self.emit("}", depth+1)
532 self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
534 self.emit("%s = _Py_asdl_int_seq_new(len, arena);" % field.name, depth+1)
536 self.emit("%s = _Py_asdl_seq_new(len, arena);" % field.name, depth+1)
537 self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
538 self.emit("for (i = 0; i < len; i++) {", depth+1)
539 self.emit("%s val;" % ctype, depth+2)
540 self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
542 self.emit("if (res != 0) goto failed;", depth+2)
543 self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
544 self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
548 self.emit("goto failed;", depth+3)
549 self.emit("}", depth+2)
550 self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2)
551 self.emit("}", depth+1)
553 self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
555 self.emit("if (res != 0) goto failed;", depth+1)
557 self.emit("Py_CLEAR(tmp);", depth+1)
558 self.emit("}", depth)
565 self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
574 self.emit("static PyTypeObject *%s_type;" % name, 0)
575 self.emit("static PyObject* ast2obj_%s(void*);" % name, 0)
579 self.emit("static char *%s_attributes[] = {" % name, 0)
581 self.emit('"%s",' % a.name, 1)
582 self.emit("};", 0)
586 self.emit("static char *%s_fields[]={" % name,0)
588 self.emit('"%s",' % f.name, 1)
589 self.emit("};", 0)
592 self.emit("static PyTypeObject *%s_type;" % name, 0)
596 self.emit("static char *%s_attributes[] = {" % name, 0)
598 self.emit('"%s",' % a.name, 1)
599 self.emit("};", 0)
607 self.emit("static PyObject *%s;" % tnames, 0)
608 self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0)
613 self.emit("static PyTypeObject *%s_type;" % cons.name, 0)
617 self.emit("static char *%s_fields[]={" % cons.name, 0)
619 self.emit('"%s",' % t.name, 1)
620 self.emit("};",0)
625 self.emit("""
958 self.emit("static int init_types(void)",0)
959 self.emit("{", 0)
960 self.emit("static int initialized;", 1)
961 self.emit("if (initialized) return 1;", 1)
962 self.emit("if (add_ast_fields() < 0) return 0;", 1)
965 self.emit("initialized = 1;", 1)
966 self.emit("return 1;", 1);
967 self.emit("}", 0)
974 self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
976 self.emit("if (!%s_type) return 0;" % name, 1)
978 self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
981 self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
984 self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
986 self.emit("if (!%s_type) return 0;" % name, 1)
988 self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
991 self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
1001 self.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
1003 self.emit("if (!%s_type) return 0;" % cons.name, 1)
1005 self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
1007 self.emit("if (!%s_singleton) return 0;" % cons.name, 1)
1013 self.emit("static struct PyModuleDef _astmodule = {", 0)
1014 self.emit(' PyModuleDef_HEAD_INIT, "_ast"', 0)
1015 self.emit("};", 0)
1016 self.emit("PyMODINIT_FUNC", 0)
1017 self.emit("PyInit__ast(void)", 0)
1018 self.emit("{", 0)
1019 self.emit("PyObject *m, *d;", 1)
1020 self.emit("if (!init_types()) return NULL;", 1)
1021 self.emit('m = PyModule_Create(&_astmodule);', 1)
1022 self.emit("if (!m) return NULL;", 1)
1023 self.emit("d = PyModule_GetDict(m);", 1)
1024 self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1)
1025 self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1)
1026 self.emit("return NULL;", 2)
1029 self.emit("return m;", 1)
1030 self.emit("}", 0)
1044 …self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name, name)…
1070 self.emit(self.CODE, 0, reflow=False)
1077 self.emit("PyObject*", 0)
1078 self.emit("ast2obj_%s(void* _o)" % (name), 0)
1079 self.emit("{", 0)
1080 self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
1081 self.emit("PyObject *result = NULL, *value = NULL;", 1)
1082 self.emit('if (!o) {', 1)
1083 self.emit("Py_RETURN_NONE;", 2)
1084 self.emit("}", 1)
1085 self.emit('', 0)
1088 self.emit("return result;", 1)
1089 self.emit("failed:", 0)
1090 self.emit("Py_XDECREF(value);", 1)
1091 self.emit("Py_XDECREF(result);", 1)
1092 self.emit("return NULL;", 1)
1093 self.emit("}", 0)
1094 self.emit("", 0)
1101 self.emit("switch (o->kind) {", 1)
1105 self.emit("}", 1)
1107 self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
1108 self.emit("if (!value) goto failed;", 1)
1109 self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
1110 self.emit('goto failed;', 2)
1111 self.emit('Py_DECREF(value);', 1)
1115 self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0)
1116 self.emit("{", 0)
1117 self.emit("switch(o) {", 1)
1119 self.emit("case %s:" % t.name, 2)
1120 self.emit("Py_INCREF(%s_singleton);" % t.name, 3)
1121 self.emit("return %s_singleton;" % t.name, 3)
1122 self.emit("default:", 2)
1123 self.emit('/* should never happen, but just in case ... */', 3)
1125 self.emit(code, 3, reflow=False)
1126 self.emit("return NULL;", 3)
1127 self.emit("}", 1)
1128 self.emit("}", 0)
1132 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1);
1133 self.emit("if (!result) return NULL;", 1)
1137 self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
1138 self.emit("if (!value) goto failed;", 1)
1139 self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
1140 self.emit('goto failed;', 2)
1141 self.emit('Py_DECREF(value);', 1)
1145 self.emit("case %s_kind:" % cons.name, 1)
1146 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2);
1147 self.emit("if (!result) goto failed;", 2)
1150 self.emit("break;", 2)
1153 def emit(s, d): function
1154 self.emit(s, depth + d)
1160 emit("if (!value) goto failed;", 0)
1161 emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) == -1)' % field.name, 0)
1162 emit("goto failed;", 1)
1163 emit("Py_DECREF(value);", 0)
1165 def emitSeq(self, field, value, depth, emit): argument
1166 emit("seq = %s;" % value, 0)
1167 emit("n = asdl_seq_LEN(seq);", 0)
1168 emit("value = PyList_New(n);", 0)
1169 emit("if (!value) goto failed;", 0)
1170 emit("for (i = 0; i < n; i++) {", 0)
1172 emit("if (!value1) goto failed;", 1)
1173 emit("PyList_SET_ITEM(value, i, value1);", 1)
1174 emit("value1 = NULL;", 1)
1175 emit("}", 0)
1183 self.emit("{", depth)
1184 self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1)
1185 self.emit("value = PyList_New(n);", depth+1)
1186 self.emit("if (!value) goto failed;", depth+1)
1187 self.emit("for(i = 0; i < n; i++)", depth+1)
1189 … self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value,
1191 self.emit("}", depth)
1193 self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth)
1196 self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False)
1255 v.emit("", 0)