void CFCC_write_man_pages(CFCC *self) { CFCHierarchy *hierarchy = self->hierarchy; CFCClass **ordered = CFCHierarchy_ordered_classes(hierarchy); size_t num_classes = 0; for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (!CFCClass_included(klass)) { ++num_classes; } } char **man_pages = (char**)CALLOCATE(num_classes, sizeof(char*)); // Generate man pages, but don't write. That way, if there's an error // while generating the pages, we leak memory but don't clutter up the file // system. for (size_t i = 0, j = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } char *man_page = CFCCMan_create_man_page(klass); man_pages[j++] = man_page; } const char *dest = CFCHierarchy_get_dest(hierarchy); char *man3_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "man" CHY_DIR_SEP "man3", dest); if (!CFCUtil_is_dir(man3_path)) { CFCUtil_make_path(man3_path); if (!CFCUtil_is_dir(man3_path)) { CFCUtil_die("Can't make path %s", man3_path); } } // Write out any man pages that have changed. for (size_t i = 0, j = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } char *raw_man_page = man_pages[j++]; if (!raw_man_page) { continue; } char *man_page = CFCUtil_sprintf("%s%s%s", self->man_header, raw_man_page, self->man_footer); const char *full_struct_sym = CFCClass_full_struct_sym(klass); char *filename = CFCUtil_sprintf("%s" CHY_DIR_SEP "%s.3", man3_path, full_struct_sym); CFCUtil_write_if_changed(filename, man_page, strlen(man_page)); FREEMEM(filename); FREEMEM(man_page); FREEMEM(raw_man_page); } FREEMEM(man3_path); FREEMEM(man_pages); FREEMEM(ordered); }
static char* S_gen_type_linkups(CFCPython *self, CFCParcel *parcel, CFCClass **ordered) { char *handles = CFCUtil_strdup(""); char *py_types = CFCUtil_strdup(""); int num_items = 0; for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass) || CFCClass_inert(klass)) { continue; } const char *class_var = CFCClass_full_class_var(klass); const char *struct_sym = CFCClass_get_struct_sym(klass); char *handles_temp = CFCUtil_sprintf("%s handles[%d] = &%s;\n", handles, num_items, class_var); char *py_types_temp = CFCUtil_sprintf("%s py_types[%d] = &%s_pytype_struct;\n", py_types, num_items, struct_sym); FREEMEM(handles); FREEMEM(py_types); handles = handles_temp; py_types = py_types_temp; num_items++; } char pattern[] = "static void\n" "S_link_py_types(void) {\n" " const int num_items = %d;\n" " size_t handles_size = num_items * sizeof(cfish_Class**);\n" " size_t py_types_size = num_items * sizeof(PyTypeObject*);\n" " cfish_Class ***handles = (cfish_Class***)CFISH_MALLOCATE(handles_size);\n" " PyTypeObject **py_types = (PyTypeObject**)CFISH_MALLOCATE(py_types_size);\n" "%s\n" "%s\n" " CFBind_assoc_py_types(handles, py_types, num_items);\n" " CFISH_FREEMEM(handles);\n" " CFISH_FREEMEM(py_types);\n" "}\n" ; char *content = CFCUtil_sprintf(pattern, num_items, handles, py_types); FREEMEM(handles); FREEMEM(py_types); return content; }
char* CFCC_link_text(CFCUri *uri_obj) { char *link_text = NULL; CFCUriType type = CFCUri_get_type(uri_obj); switch (type) { case CFC_URI_ERROR: { const char *error = CFCUri_get_error(uri_obj); link_text = CFCUtil_sprintf("[%s]", error); break; } case CFC_URI_NULL: link_text = CFCUtil_strdup("NULL"); break; case CFC_URI_CLASS: { CFCClass *klass = CFCUri_get_class(uri_obj); const char *src = CFCClass_included(klass) ? CFCClass_get_name(klass) : CFCClass_get_struct_sym(klass); link_text = CFCUtil_strdup(src); break; } case CFC_URI_FUNCTION: case CFC_URI_METHOD: { const char *name = CFCUri_get_callable_name(uri_obj); link_text = CFCUtil_sprintf("%s()", name); break; } case CFC_URI_DOCUMENT: { CFCDocument *doc = CFCUri_get_document(uri_obj); const char *name = CFCDocument_get_name(doc); link_text = CFCUtil_strdup(name); break; } default: CFCUtil_die("Unsupported node type: %d", type); break; } return link_text; }
void CFCPerlTypeMap_write_xs_typemap(CFCHierarchy *hierarchy) { CFCClass **classes = CFCHierarchy_ordered_classes(hierarchy); char *start = CFCUtil_strdup(""); char *input = CFCUtil_strdup(""); char *output = CFCUtil_strdup(""); for (int i = 0; classes[i] != NULL; i++) { CFCClass *klass = classes[i]; if (CFCClass_included(klass)) { continue; } const char *full_struct_sym = CFCClass_full_struct_sym(klass); const char *vtable_var = CFCClass_full_vtable_var(klass); start = CFCUtil_cat(start, full_struct_sym, "*\t", vtable_var, "_\n", NULL); input = CFCUtil_cat(input, vtable_var, "_\n" " $var = (", full_struct_sym, "*)XSBind_sv_to_cfish_obj($arg, ", vtable_var, ", NULL);\n\n", NULL); output = CFCUtil_cat(output, vtable_var, "_\n" " $arg = (SV*)Cfish_Obj_To_Host((cfish_Obj*)$var);\n" " CFISH_DECREF($var);\n" "\n", NULL); } char *content = CFCUtil_strdup(""); content = CFCUtil_cat(content, typemap_start, start, "\n\n", typemap_input, input, "\n\n", typemap_output, output, "\n\n", NULL); CFCUtil_write_if_changed("typemap", content, strlen(content)); FREEMEM(content); FREEMEM(output); FREEMEM(input); FREEMEM(start); FREEMEM(classes); }
static char* S_gen_class_bindings(CFCPython *self, CFCParcel *parcel, const char *pymod_name, CFCClass **ordered) { char *bindings = CFCUtil_strdup(""); for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } const char *class_name = CFCClass_get_name(klass); CFCPyClass *class_binding = CFCPyClass_singleton(class_name); if (!class_binding) { // No binding spec'd out, so create one using defaults. class_binding = CFCPyClass_new(klass); CFCPyClass_add_to_registry(class_binding); } char *code = CFCPyClass_gen_binding_code(class_binding); bindings = CFCUtil_cat(bindings, code, NULL); FREEMEM(code); } return bindings; }
static void S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) { const char *parcel_name = CFCParcel_get_name(parcel); char *pymod_name = CFCUtil_strdup(parcel_name); // TODO: Stop lowercasing when parcels are restricted to lowercase. for (int i = 0; pymod_name[i] != '\0'; i++) { pymod_name[i] = tolower(pymod_name[i]); } const char *last_dot = strrchr(pymod_name, '.'); const char *last_component = last_dot != NULL ? last_dot + 1 : pymod_name; char *helper_mod_name = CFCUtil_sprintf("%s._%s", pymod_name, last_component); for (int i = 0; helper_mod_name[i] != '\0'; i++) { helper_mod_name[i] = tolower(helper_mod_name[i]); } CFCClass **ordered = CFCHierarchy_ordered_classes(self->hierarchy); CFCParcel **parcels = CFCParcel_all_parcels(); char *callbacks = S_gen_callbacks(self, parcel, ordered); char *type_linkups = S_gen_type_linkups(self, parcel, ordered); char *pound_includes = CFCUtil_strdup(""); char *class_bindings = S_gen_class_bindings(self, parcel, pymod_name, ordered); char *parcel_boots = CFCUtil_strdup(""); char *pytype_ready_calls = CFCUtil_strdup(""); char *module_adds = CFCUtil_strdup(""); // Add parcel bootstrapping calls. for (size_t i = 0; parcels[i]; ++i) { if (!CFCParcel_included(parcels[i])) { const char *prefix = CFCParcel_get_prefix(parcels[i]); parcel_boots = CFCUtil_cat(parcel_boots, " ", prefix, "bootstrap_parcel();\n", NULL); } } for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } const char *struct_sym = CFCClass_get_struct_sym(klass); const char *include_h = CFCClass_include_h(klass); pound_includes = CFCUtil_cat(pound_includes, "#include \"", include_h, "\"\n", NULL); // The PyType_Ready invocations for instantiable classes are handled // via bootstrapping of Clownfish Class objects. Since inert classes // do not at present have Class objects, we need to handle their // PyType_Ready calls independently. if (CFCClass_inert(klass)) { pytype_ready_calls = CFCUtil_cat(pytype_ready_calls, " if (PyType_Ready(&", struct_sym, "_pytype_struct) < 0) { return NULL; }\n", NULL); } module_adds = CFCUtil_cat(module_adds, " PyModule_AddObject(module, \"", struct_sym, "\", (PyObject*)&", struct_sym, "_pytype_struct);\n", NULL); } const char pattern[] = "%s\n" "\n" "#include \"Python.h\"\n" "#include \"cfish_parcel.h\"\n" "#include \"CFBind.h\"\n" "%s\n" "\n" "%s\n" // callbacks "\n" "static PyModuleDef module_def = {\n" " PyModuleDef_HEAD_INIT,\n" " \"%s\",\n" // module name " NULL,\n" // docstring " -1,\n" " NULL, NULL, NULL, NULL, NULL\n" "};\n" "\n" "%s" // class bindings "\n" "%s" // S_link_py_types function "\n" "PyMODINIT_FUNC\n" "PyInit__%s(void) {\n" " cfish_Class_bootstrap_hook1 = CFBind_class_bootstrap_hook1;\n" "\n" "%s\n" // PyType_Ready calls "\n" " S_link_py_types();\n" "\n" "%s\n" // parcel boots "\n" " PyObject *module = PyModule_Create(&module_def);\n" "%s\n" // Add types to module "\n" " return module;\n" "}\n" "\n" "%s\n" "\n"; char *content = CFCUtil_sprintf(pattern, self->header, pound_includes, callbacks, helper_mod_name, class_bindings, type_linkups, last_component, pytype_ready_calls, parcel_boots, module_adds, self->footer); char *filepath = CFCUtil_sprintf("%s" CHY_DIR_SEP "_%s.c", dest, last_component); CFCUtil_write_if_changed(filepath, content, strlen(content)); FREEMEM(filepath); FREEMEM(content); FREEMEM(module_adds); FREEMEM(pytype_ready_calls); FREEMEM(parcel_boots); FREEMEM(class_bindings); FREEMEM(helper_mod_name); FREEMEM(pymod_name); FREEMEM(pound_includes); FREEMEM(type_linkups); FREEMEM(callbacks); FREEMEM(ordered); }
static char* S_gen_callbacks(CFCPython *self, CFCParcel *parcel, CFCClass **ordered) { char *callbacks = CFCUtil_strdup(""); // Generate implementation files containing callback definitions. for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass) || CFCClass_inert(klass) //|| CFCClass_get_parcel(klass) != parcel ) { continue; } CFCMethod **fresh_methods = CFCClass_fresh_methods(klass); for (int meth_num = 0; fresh_methods[meth_num] != NULL; meth_num++) { CFCMethod *method = fresh_methods[meth_num]; // Define callback. if (CFCMethod_novel(method) && !CFCMethod_final(method)) { char *cb_def = CFCPyMethod_callback_def(method, klass); callbacks = CFCUtil_cat(callbacks, cb_def, "\n", NULL); FREEMEM(cb_def); } } } static const char helpers[] = "static PyObject*\n" "S_pack_tuple(int num_args, ...) {\n" " PyObject *tuple = PyTuple_New(num_args);\n" " va_list args;\n" " va_start(args, num_args);\n" " for (int i = 0; i < num_args; i++) {\n" " PyObject *arg = va_arg(args, PyObject*);\n" " PyTuple_SET_ITEM(tuple, i, arg);\n" " }\n" " va_end(args);\n" " return tuple;\n" "}\n" "#define CFBIND_TRY(routine) \\\n" " do { \\\n" " jmp_buf env; \\\n" " jmp_buf *prev_env = CFBind_swap_env(&env); \\\n" " if (!setjmp(env)) { \\\n" " routine; \\\n" " } \\\n" " CFBind_swap_env(prev_env); \\\n" " } while (0)\n" "\n" "static PyObject*\n" "S_call_pymeth(PyObject *self, const char *meth_name, PyObject *args,\n" " const char *file, int line, const char *func) {\n" " PyObject *callable = PyObject_GetAttrString(self, meth_name);\n" " if (!PyCallable_Check(callable)) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func, \"Attr '%s' not callable\",\n" " meth_name);\n" " cfish_Err_throw_mess(CFISH_ERR, mess);\n" " }\n" " PyObject *result = PyObject_CallObject(callable, args);\n" " Py_DECREF(args);\n" " if (result == NULL) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func,\n" " \"Callback to '%s' failed\", meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " return result;\n" "}\n" "\n" "#define CALL_PYMETH_VOID(self, meth_name, args) \\\n" " S_call_pymeth_void(self, meth_name, args, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "\n" "static void\n" "S_call_pymeth_void(PyObject *self, const char *meth_name, PyObject *args,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " if (py_result == NULL) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func, \"Call to %s failed\",\n" " meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " Py_DECREF(py_result);\n" "}\n" "\n" "#define CALL_PYMETH_BOOL(self, meth_name, args) \\\n" " S_call_pymeth_bool(self, meth_name, args, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "\n" "static bool\n" "S_call_pymeth_bool(PyObject *self, const char *meth_name, PyObject *args,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " int truthiness = py_result != NULL\n" " ? PyObject_IsTrue(py_result)\n" " : -1;\n" " if (truthiness == -1) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func, \"Call to %s failed\",\n" " meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " Py_DECREF(py_result);\n" " return !!truthiness;\n" "}\n" "\n" "#define CALL_PYMETH_OBJ(self, meth_name, args, ret_class, nullable) \\\n" " S_call_pymeth_obj(self, meth_name, args, ret_class, nullable, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "\n" "static cfish_Obj*\n" "S_call_pymeth_obj(PyObject *self, const char *meth_name,\n" " PyObject *args, cfish_Class *ret_class, bool nullable,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " cfish_Obj *result = CFBind_py_to_cfish(py_result, ret_class);\n" " Py_DECREF(py_result);\n" " if (!nullable && result == NULL) {\n" " CFISH_THROW(CFISH_ERR, \"%s cannot return NULL\", meth_name);\n" " }\n" " else if (!cfish_Obj_is_a(result, ret_class)) {\n" " cfish_Class *result_class = cfish_Obj_get_class(result);\n" " CFISH_DECREF(result);\n" " CFISH_THROW(CFISH_ERR, \"%s returned %o instead of %o\", meth_name,\n" " CFISH_Class_Get_Name(result_class),\n" " CFISH_Class_Get_Name(ret_class));\n" " }\n" " return result;\n" "}\n" "\n" "#define CALL_PYMETH_DOUBLE(self, meth_name, args) \\\n" " S_call_pymeth_f64(self, meth_name, args, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "#define CALL_PYMETH_FLOAT(self, meth_name, args) \\\n" " ((float)S_call_pymeth_f64(self, meth_name, args, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "\n" "static double\n" "S_call_pymeth_f64(PyObject *self, const char *meth_name, PyObject *args,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " PyErr_Clear();\n" " double result = PyFloat_AsDouble(py_result);\n" " if (PyErr_Occurred()) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func,\n" " \"Converting result of '%s' to double failed\",\n" " meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " Py_DECREF(py_result);\n" " return result;\n" "}\n" "\n" "#define CALL_PYMETH_INT64_T(self, meth_name, args) \\\n" " S_call_pymeth_i64(self, meth_name, args, INT64_MAX, INT64_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "#define CALL_PYMETH_INT32_T(self, meth_name, args) \\\n" " ((int32_t)S_call_pymeth_i64(self, meth_name, args, INT32_MAX, INT32_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_INT16_T(self, meth_name, args) \\\n" " ((int16_t)S_call_pymeth_i64(self, meth_name, args, INT16_MAX, INT16_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_INT8_T(self, meth_name, args) \\\n" " ((int8_t)S_call_pymeth_i64(self, meth_name, args, INT8_MAX, INT8_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_CHAR(self, meth_name, args) \\\n" " ((char)S_call_pymeth_i64(self, meth_name, args, CHAR_MAX, CHAR_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_SHORT(self, meth_name, args) \\\n" " ((short)S_call_pymeth_i64(self, meth_name, args, SHRT_MAX, SHRT_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_INT(self, meth_name, args) \\\n" " ((int16_t)S_call_pymeth_i64(self, meth_name, args, INT_MAX, INT_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_LONG(self, meth_name, args) \\\n" " ((int16_t)S_call_pymeth_i64(self, meth_name, args, LONG_MAX, LONG_MIN, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "\n" "static int64_t\n" "S_call_pymeth_i64(PyObject *self, const char *meth_name, PyObject *args,\n" " int64_t max, int64_t min,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " PyErr_Clear();\n" " int64_t result = PyLong_AsLongLong(py_result);\n" " if (PyErr_Occurred() || result > max || result < min) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func,\n" " \"Converting result of '%s' to int64_t failed\",\n" " meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " Py_DECREF(py_result);\n" " return result;\n" "}\n" "\n" "#define CALL_PYMETH_UINT64_T(self, meth_name, args) \\\n" " S_call_pymeth_u64(self, meth_name, args, UINT64_MAX, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "#define CALL_PYMETH_UINT32_T(self, meth_name, args) \\\n" " ((uint32_t)S_call_pymeth_u64(self, meth_name, args, UINT32_MAX, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_UINT16_T(self, meth_name, args) \\\n" " ((uint32_t)S_call_pymeth_u64(self, meth_name, args, UINT16_MAX, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_UINT8_T(self, meth_name, args) \\\n" " ((uint32_t)S_call_pymeth_u64(self, meth_name, args, UINT8_MAX, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO))\n" "#define CALL_PYMETH_SIZE_T(self, meth_name, args) \\\n" " S_call_pymeth_u64(self, meth_name, args, SIZE_MAX, \\\n" " __FILE__, __LINE__, CFISH_ERR_FUNC_MACRO)\n" "\n" "static uint64_t\n" "S_call_pymeth_u64(PyObject *self, const char *meth_name, PyObject *args,\n" " uint64_t max,\n" " const char *file, int line, const char *func) {\n" " PyObject *py_result\n" " = S_call_pymeth(self, meth_name, args, file, line, func);\n" " PyErr_Clear();\n" " uint64_t result = PyLong_AsUnsignedLongLong(py_result);\n" " if (PyErr_Occurred()) {\n" " cfish_String *mess\n" " = cfish_Err_make_mess(file, line, func,\n" " \"Converting result of '%s' to uint64_t failed\",\n" " meth_name);\n" " CFBind_reraise_pyerr(CFISH_ERR, mess);\n" " }\n" " Py_DECREF(py_result);\n" " return result;\n" "}\n" ; static const char pattern[] = "%s\n" "\n" "%s" ; char *content = CFCUtil_sprintf(pattern, helpers, callbacks); FREEMEM(callbacks); return content; }
static void S_run_include_tests(CFCTest *test) { { CFCHierarchy *hierarchy = CFCHierarchy_new(T_CFDEST); CFCHierarchy_add_source_dir(hierarchy, T_CFEXT); CFCHierarchy_add_include_dir(hierarchy, T_CFBASE); const char **include_dirs = CFCHierarchy_get_include_dirs(hierarchy); STR_EQ(test, include_dirs[0], T_CFBASE, "include_dirs[0]"); OK(test, include_dirs[1] == NULL, "include_dirs[1]"); CFCHierarchy_build(hierarchy); CFCClass **classes = CFCHierarchy_ordered_classes(hierarchy); CFCClass *rottweiler = NULL;; int num_classes; int num_source_classes = 0; for (num_classes = 0; classes[num_classes]; ++num_classes) { CFCClass *klass = classes[num_classes]; int expect_included = 1; const char *class_name = CFCClass_get_name(klass); if (strcmp(class_name, "Animal::Rottweiler") == 0) { rottweiler = klass; expect_included = 0; ++num_source_classes; } INT_EQ(test, CFCClass_included(klass), expect_included, "included"); } INT_EQ(test, num_classes, 5, "class count"); INT_EQ(test, num_source_classes, 1, "source class count"); STR_EQ(test, CFCClass_get_name(CFCClass_get_parent(rottweiler)), "Animal::Dog", "parent of included class"); FREEMEM(classes); CFCBase_decref((CFCBase*)hierarchy); CFCClass_clear_registry(); CFCParcel_reap_singletons(); } { CFCHierarchy *hierarchy = CFCHierarchy_new(T_CFDEST); CFCHierarchy_add_source_dir(hierarchy, T_CFBASE); CFCHierarchy_add_source_dir(hierarchy, T_CFEXT); CFCHierarchy_build(hierarchy); CFCClass **classes = CFCHierarchy_ordered_classes(hierarchy); CFCClass *rottweiler = NULL;; int num_classes; for (num_classes = 0; classes[num_classes]; ++num_classes) { CFCClass *klass = classes[num_classes]; const char *class_name = CFCClass_get_name(klass); if (strcmp(class_name, "Animal::Rottweiler") == 0) { rottweiler = klass; } OK(test, !CFCClass_included(klass), "not included"); } INT_EQ(test, num_classes, 5, "class count"); OK(test, rottweiler != NULL, "found rottweiler"); STR_EQ(test, CFCClass_get_name(CFCClass_get_parent(rottweiler)), "Animal::Dog", "parent of class from second source"); FREEMEM(classes); CFCBase_decref((CFCBase*)hierarchy); CFCClass_clear_registry(); CFCParcel_reap_singletons(); } { CFCHierarchy *hierarchy = CFCHierarchy_new(T_CFDEST); CFCHierarchy_add_include_dir(hierarchy, T_CFBASE); CFCHierarchy_add_include_dir(hierarchy, T_CFEXT); CFCHierarchy_add_prereq(hierarchy, "AnimalExtension"); CFCHierarchy_build(hierarchy); CFCParcel *animal = CFCParcel_fetch("Animal"); OK(test, animal != NULL, "parcel Animal registered"); OK(test, CFCParcel_required(animal), "parcel Animal required"); CFCParcel *animal_ext = CFCParcel_fetch("AnimalExtension"); OK(test, animal_ext != NULL, "parcel AnimalExtension registered"); OK(test, CFCParcel_required(animal_ext), "parcel AnimalExtension required"); CFCClass **classes = CFCHierarchy_ordered_classes(hierarchy); int num_classes = 0; while (classes[num_classes]) { ++num_classes; } INT_EQ(test, num_classes, 5, "class count"); FREEMEM(classes); CFCBase_decref((CFCBase*)hierarchy); CFCClass_clear_registry(); CFCParcel_reap_singletons(); } rmdir(T_CFDEST_INCLUDE); rmdir(T_CFDEST_SOURCE); rmdir(T_CFDEST); }
static char* S_convert_link(cmark_node *link, CFCClass *doc_class, int header_level) { cmark_node *child = cmark_node_first_child(link); const char *uri = cmark_node_get_url(link); char *text = S_nodes_to_pod(child, doc_class, header_level); char *retval; if (!CFCUri_is_clownfish_uri(uri)) { retval = S_pod_link(text, uri); FREEMEM(text); return retval; } char *new_uri = NULL; char *new_text = NULL; CFCUri *uri_obj = CFCUri_new(uri, doc_class); CFCUriType type = CFCUri_get_type(uri_obj); switch (type) { case CFC_URI_ERROR: { const char *error = CFCUri_get_error(uri_obj); new_text = CFCUtil_sprintf("[%s]", error); break; } case CFC_URI_NULL: // Change all instances of NULL to 'undef' new_text = CFCUtil_strdup("undef"); break; case CFC_URI_CLASS: { CFCClass *klass = CFCUri_get_class(uri_obj); if (klass != doc_class) { const char *class_name = CFCClass_get_name(klass); new_uri = CFCUtil_strdup(class_name); } if (text[0] == '\0') { const char *src = CFCClass_included(klass) ? CFCClass_get_name(klass) : CFCClass_get_struct_sym(klass); new_text = CFCUtil_strdup(src); } break; } case CFC_URI_FUNCTION: case CFC_URI_METHOD: { CFCClass *klass = CFCUri_get_class(uri_obj); const char *name = CFCUri_get_callable_name(uri_obj); // Convert "Err_get_error" to "Clownfish->error". if (strcmp(CFCClass_full_struct_sym(klass), "cfish_Err") == 0 && strcmp(name, "get_error") == 0 ) { new_text = CFCUtil_strdup("Clownfish->error"); break; } char *perl_name = CFCUtil_strdup(name); for (size_t i = 0; perl_name[i] != '\0'; ++i) { perl_name[i] = CFCUtil_tolower(perl_name[i]); } // The Perl POD only contains sections for novel methods. Link // to the class where the method is declared first. if (type == CFC_URI_METHOD) { CFCClass *parent = CFCClass_get_parent(klass); while (parent && CFCClass_method(parent, name)) { klass = parent; parent = CFCClass_get_parent(klass); } } if (klass == doc_class) { new_uri = CFCUtil_sprintf("/%s", perl_name); } else { const char *class_name = CFCClass_get_name(klass); new_uri = CFCUtil_sprintf("%s/%s", class_name, perl_name); } if (text[0] == '\0') { new_text = CFCUtil_sprintf("%s()", perl_name); } FREEMEM(perl_name); break; } case CFC_URI_DOCUMENT: { CFCDocument *doc = CFCUri_get_document(uri_obj); const char *path_part = CFCDocument_get_path_part(doc); new_uri = CFCUtil_global_replace(path_part, CHY_DIR_SEP, "::"); if (text[0] == '\0') { const char *name = CFCDocument_get_name(doc); new_text = CFCUtil_strdup(name); } break; } } if (new_text) { FREEMEM(text); text = new_text; } if (new_uri) { retval = S_pod_link(text, new_uri); FREEMEM(new_uri); FREEMEM(text); } else { retval = text; } CFCBase_decref((CFCBase*)uri_obj); return retval; }
void CFCPerl_write_bindings(CFCPerl *self) { CFCClass **ordered = CFCHierarchy_ordered_classes(self->hierarchy); CFCPerlClass **registry = CFCPerlClass_registry(); char *hand_rolled_xs = CFCUtil_strdup(""); char *generated_xs = CFCUtil_strdup(""); char *xs_init = CFCUtil_strdup(""); // Pound-includes for generated headers. for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; // TODO: Don't include headers for parcels the source parcels don't // depend on. const char *include_h = CFCClass_include_h(klass); generated_xs = CFCUtil_cat(generated_xs, "#include \"", include_h, "\"\n", NULL); } generated_xs = CFCUtil_cat(generated_xs, "\n", NULL); for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } // Constructors. CFCPerlConstructor **constructors = CFCPerlClass_constructor_bindings(klass); for (size_t j = 0; constructors[j] != NULL; j++) { CFCPerlSub *xsub = (CFCPerlSub*)constructors[j]; // Add the XSUB function definition. char *xsub_def = CFCPerlConstructor_xsub_def(constructors[j]); generated_xs = CFCUtil_cat(generated_xs, xsub_def, "\n", NULL); FREEMEM(xsub_def); // Add XSUB initialization at boot. xs_init = S_add_xs_init(xs_init, xsub); } FREEMEM(constructors); // Methods. CFCPerlMethod **methods = CFCPerlClass_method_bindings(klass); for (size_t j = 0; methods[j] != NULL; j++) { CFCPerlSub *xsub = (CFCPerlSub*)methods[j]; // Add the XSUB function definition. char *xsub_def = CFCPerlMethod_xsub_def(methods[j]); generated_xs = CFCUtil_cat(generated_xs, xsub_def, "\n", NULL); FREEMEM(xsub_def); // Add XSUB initialization at boot. xs_init = S_add_xs_init(xs_init, xsub); } FREEMEM(methods); } // Hand-rolled XS. for (size_t i = 0; registry[i] != NULL; i++) { const char *xs = CFCPerlClass_get_xs_code(registry[i]); hand_rolled_xs = CFCUtil_cat(hand_rolled_xs, xs, "\n", NULL); } // Write out if there have been any changes. char *xs_file_contents = S_xs_file_contents(self, generated_xs, xs_init, hand_rolled_xs); CFCUtil_write_if_changed(self->xs_path, xs_file_contents, strlen(xs_file_contents)); FREEMEM(xs_file_contents); FREEMEM(hand_rolled_xs); FREEMEM(xs_init); FREEMEM(generated_xs); FREEMEM(ordered); }
static void S_write_boot_c(CFCPerl *self) { CFCClass **ordered = CFCHierarchy_ordered_classes(self->hierarchy); CFCParcel **parcels = CFCParcel_all_parcels(); char *pound_includes = CFCUtil_strdup(""); char *bootstrap_code = CFCUtil_strdup(""); char *alias_adds = CFCUtil_strdup(""); char *isa_pushes = CFCUtil_strdup(""); for (size_t i = 0; parcels[i]; ++i) { if (!CFCParcel_included(parcels[i])) { const char *prefix = CFCParcel_get_prefix(parcels[i]); bootstrap_code = CFCUtil_cat(bootstrap_code, " ", prefix, "bootstrap_parcel();\n", NULL); } } for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; if (CFCClass_included(klass)) { continue; } const char *class_name = CFCClass_get_class_name(klass); const char *include_h = CFCClass_include_h(klass); pound_includes = CFCUtil_cat(pound_includes, "#include \"", include_h, "\"\n", NULL); if (CFCClass_inert(klass)) { continue; } // Add aliases for selected KinoSearch classes which allow old indexes // to be read. CFCPerlClass *class_binding = CFCPerlClass_singleton(class_name); if (class_binding) { const char *vtable_var = CFCClass_full_vtable_var(klass); const char **aliases = CFCPerlClass_get_class_aliases(class_binding); for (size_t j = 0; aliases[j] != NULL; j++) { const char *alias = aliases[j]; size_t alias_len = strlen(alias); const char pattern[] = " cfish_VTable_add_alias_to_registry(" "%s, \"%s\", %u);\n"; char *alias_add = CFCUtil_sprintf(pattern, vtable_var, alias, (unsigned)alias_len); alias_adds = CFCUtil_cat(alias_adds, alias_add, NULL); FREEMEM(alias_add); } char *metadata_code = CFCPerlClass_method_metadata_code(class_binding); alias_adds = CFCUtil_cat(alias_adds, metadata_code, NULL); FREEMEM(metadata_code); } CFCClass *parent = CFCClass_get_parent(klass); if (parent) { const char *parent_class_name = CFCClass_get_class_name(parent); isa_pushes = CFCUtil_cat(isa_pushes, " isa = get_av(\"", class_name, "::ISA\", 1);\n", NULL); isa_pushes = CFCUtil_cat(isa_pushes, " av_push(isa, newSVpv(\"", parent_class_name, "\", 0));\n", NULL); } } const char pattern[] = "%s\n" "\n" "#include \"cfish_parcel.h\"\n" "#include \"EXTERN.h\"\n" "#include \"perl.h\"\n" "#include \"XSUB.h\"\n" "#include \"boot.h\"\n" "#include \"Clownfish/String.h\"\n" "#include \"Clownfish/VTable.h\"\n" "%s\n" "\n" "void\n" "%s() {\n" "%s" "\n" "%s" "\n" " AV *isa;\n" "%s" "}\n" "\n" "%s\n" "\n"; char *content = CFCUtil_sprintf(pattern, self->header, pound_includes, self->boot_func, bootstrap_code, alias_adds, isa_pushes, self->footer); const char *src_dest = CFCHierarchy_get_source_dest(self->hierarchy); char *boot_c_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "boot.c", src_dest); CFCUtil_write_file(boot_c_path, content, strlen(content)); FREEMEM(boot_c_path); FREEMEM(content); FREEMEM(isa_pushes); FREEMEM(alias_adds); FREEMEM(bootstrap_code); FREEMEM(pound_includes); FREEMEM(parcels); FREEMEM(ordered); }
static char* S_virtual_method_def(CFCMethod *method, CFCClass *klass) { CFCParamList *param_list = CFCMethod_get_param_list(method); const char *invoker_struct = CFCClass_full_struct_sym(klass); const char *common_struct = CFCType_get_specifier(CFCMethod_self_type(method)); const char *visibility = CFCClass_included(klass) ? "CHY_IMPORT" : "CHY_EXPORT"; size_t meth_sym_size = CFCMethod_full_method_sym(method, klass, NULL, 0); char *full_meth_sym = (char*)MALLOCATE(meth_sym_size); CFCMethod_full_method_sym(method, klass, full_meth_sym, meth_sym_size); size_t offset_sym_size = CFCMethod_full_offset_sym(method, klass, NULL, 0); char *full_offset_sym = (char*)MALLOCATE(offset_sym_size); CFCMethod_full_offset_sym(method, klass, full_offset_sym, offset_sym_size); size_t full_typedef_size = CFCMethod_full_typedef(method, klass, NULL, 0); char *full_typedef = (char*)MALLOCATE(full_typedef_size); CFCMethod_full_typedef(method, klass, full_typedef, full_typedef_size); // Prepare parameter lists, minus invoker. The invoker gets forced to // "self" later. const char *arg_names_minus_invoker = CFCParamList_name_list(param_list); const char *params_minus_invoker = CFCParamList_to_c(param_list); while (*arg_names_minus_invoker && *arg_names_minus_invoker != ',') { arg_names_minus_invoker++; } while (*params_minus_invoker && *params_minus_invoker != ',') { params_minus_invoker++; } // Prepare a return statement... or not. CFCType *return_type = CFCMethod_get_return_type(method); const char *ret_type_str = CFCType_to_c(return_type); const char *maybe_return = CFCType_is_void(return_type) ? "" : "return "; const char pattern[] = "extern %s size_t %s;\n" "static CHY_INLINE %s\n" "%s(const %s *self%s) {\n" " char *const method_address = *(char**)self + %s;\n" " const %s method = *((%s*)method_address);\n" " %smethod((%s*)self%s);\n" "}\n"; size_t size = sizeof(pattern) + strlen(visibility) + strlen(full_offset_sym) + strlen(ret_type_str) + strlen(full_meth_sym) + strlen(invoker_struct) + strlen(params_minus_invoker) + strlen(full_offset_sym) + strlen(full_typedef) + strlen(full_typedef) + strlen(maybe_return) + strlen(common_struct) + strlen(arg_names_minus_invoker) + 40; char *method_def = (char*)MALLOCATE(size); sprintf(method_def, pattern, visibility, full_offset_sym, ret_type_str, full_meth_sym, invoker_struct, params_minus_invoker, full_offset_sym, full_typedef, full_typedef, maybe_return, common_struct, arg_names_minus_invoker); FREEMEM(full_offset_sym); FREEMEM(full_meth_sym); FREEMEM(full_typedef); return method_def; }
CFCClass* CFCClass_do_create(CFCClass *self, struct CFCParcel *parcel, const char *exposure, const char *name, const char *nickname, CFCDocuComment *docucomment, CFCFileSpec *file_spec, const char *parent_class_name, int is_final, int is_inert, int is_abstract) { CFCUTIL_NULL_CHECK(parcel); CFCUTIL_NULL_CHECK(name); exposure = exposure ? exposure : "parcel"; // Validate. if (!S_validate_exposure(exposure)) { CFCBase_decref((CFCBase*)self); CFCUtil_die("Invalid exposure: '%s'", exposure); } if (!CFCClass_validate_class_name(name)) { CFCBase_decref((CFCBase*)self); CFCUtil_die("Invalid name: '%s'", name); } const char *last_colon = strrchr(name, ':'); const char *struct_sym = last_colon ? last_colon + 1 : name; // Derive nickname if necessary, then validate. const char *real_nickname = NULL; if (nickname) { real_nickname = nickname; } else { real_nickname = struct_sym; } if (!S_validate_nickname(real_nickname)) { CFCBase_decref((CFCBase*)self); CFCUtil_die("Invalid nickname: '%s'", real_nickname); } // Default parent class name is "Clownfish::Obj". if (!is_inert && !parent_class_name && strcmp(name, "Clownfish::Obj") != 0 ) { parent_class_name = "Clownfish::Obj"; } // Assign. self->parcel = (CFCParcel*)CFCBase_incref((CFCBase*)parcel); self->exposure = CFCUtil_strdup(exposure); self->name = CFCUtil_strdup(name); self->nickname = CFCUtil_strdup(real_nickname); self->tree_grown = false; self->parent = NULL; self->children = (CFCClass**)CALLOCATE(1, sizeof(CFCClass*)); self->num_kids = 0; self->functions = (CFCFunction**)CALLOCATE(1, sizeof(CFCFunction*)); self->num_functions = 0; self->fresh_methods = (CFCMethod**)CALLOCATE(1, sizeof(CFCMethod*)); self->num_fresh_meths = 0; self->methods = NULL; self->num_methods = 0; self->fresh_vars = (CFCVariable**)CALLOCATE(1, sizeof(CFCVariable*)); self->num_fresh_vars = 0; self->member_vars = NULL; self->num_member_vars = 0; self->inert_vars = (CFCVariable**)CALLOCATE(1, sizeof(CFCVariable*)); self->num_inert_vars = 0; self->parent_class_name = CFCUtil_strdup(parent_class_name); self->docucomment = (CFCDocuComment*)CFCBase_incref((CFCBase*)docucomment); self->file_spec = (CFCFileSpec*)CFCBase_incref((CFCBase*)file_spec); // Cache several derived symbols. const char *prefix = CFCClass_get_prefix(self); self->struct_sym = CFCUtil_strdup(struct_sym); self->full_struct_sym = CFCUtil_sprintf("%s%s", prefix, struct_sym); self->ivars_struct = CFCUtil_sprintf("%sIVARS", struct_sym); self->full_ivars_struct = CFCUtil_sprintf("%s%s", prefix, self->ivars_struct); self->ivars_func = CFCUtil_sprintf("%s_IVARS", self->nickname); self->full_ivars_func = CFCUtil_sprintf("%s%s", prefix, self->ivars_func); self->full_ivars_offset = CFCUtil_sprintf("%s_OFFSET", self->full_ivars_func); const char *PREFIX = CFCClass_get_PREFIX(self); size_t struct_sym_len = strlen(struct_sym); char *short_class_var = (char*)MALLOCATE(struct_sym_len + 1); size_t i; for (i = 0; i < struct_sym_len; i++) { short_class_var[i] = toupper(struct_sym[i]); } short_class_var[struct_sym_len] = '\0'; self->short_class_var = short_class_var; self->full_class_var = CFCUtil_sprintf("%s%s", PREFIX, short_class_var); self->privacy_symbol = CFCUtil_sprintf("C_%s", self->full_class_var); // Build the relative path to the autogenerated C header file. if (file_spec) { const char *path_part = CFCFileSpec_get_path_part(self->file_spec); self->include_h = CFCUtil_sprintf("%s.h", path_part); } else { self->include_h = CFCUtil_strdup("class.h"); } self->is_final = !!is_final; self->is_inert = !!is_inert; self->is_abstract = !!is_abstract; // Check for include flag mismatch. if (!CFCClass_included(self) && CFCParcel_included(parcel)) { CFCUtil_die("Class %s from source dir found in parcel %s from" " include dir", name, CFCParcel_get_name(parcel)); } // Skip class if it's from an include dir and the parcel was already // processed in another source or include dir. const char *class_source_dir = CFCClass_get_source_dir(self); const char *parcel_source_dir = CFCParcel_get_source_dir(parcel); if (!CFCClass_included(self) || !class_source_dir || !parcel_source_dir || strcmp(class_source_dir, parcel_source_dir) == 0 ) { char *error; CFCUTIL_TRY { // Store in registry. S_register(self); } CFCUTIL_CATCH(error); if (error) { CFCBase_decref((CFCBase*)self); CFCUtil_rethrow(error); } CFCParcel_add_struct_sym(parcel, self->struct_sym); }