summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-01-25 23:37:42 -0800
committerOri Bernstein <ori@eigenstate.org>2016-01-25 23:37:42 -0800
commit22a5bf9a39ed9c5fb2feb5153148416f24267158 (patch)
treed31f6797a3abaaba7003cb1df6ed218b38502023
parent1810885d58ee9a72b7c52092741b4cf2b91dc833 (diff)
downloadmcbind-22a5bf9a39ed9c5fb2feb5153148416f24267158.tar.gz
Code cleanups.
- Fix indentation - Remove global parameter
-rw-r--r--cgen.c188
-rw-r--r--cparse.c369
-rw-r--r--main.c23
-rw-r--r--mcbind.h22
-rw-r--r--myrgen.c168
-rw-r--r--types.c34
6 files changed, 409 insertions, 395 deletions
diff --git a/cgen.c b/cgen.c
index d689f11..0cfb481 100644
--- a/cgen.c
+++ b/cgen.c
@@ -15,85 +15,85 @@ void writecdecl(FILE *fd, Type *ty, char *name)
size_t i;
switch (ty->type) {
- case Tynone: die("invalid type\n"); break;
-
- case Tyvoid: fprintf(fd, "void"); break;
- case Tybool: fprintf(fd, "bool %s", name); break;
- case Tychar: fprintf(fd, "uint32_t %s", name); break;
-
- case Tyint8: fprintf(fd, "int8_t %s", name); break;
- case Tyint16: fprintf(fd, "int16_t %s", name); break;
- case Tyint: fprintf(fd, "int32_t %s", name); break;
- case Tyint32: fprintf(fd, "int32_t %s", name); break;
- case Tyint64: fprintf(fd, "int64_t %s", name); break;
- case Tylong: fprintf(fd, "int64_t %s", name); break;
-
- case Tybyte: fprintf(fd, "uint8_t %s", name); break;
- case Tyuint8: fprintf(fd, "uint8_t %s", name); break;
- case Tyuint16: fprintf(fd, "uint16_t %s", name); break;
- case Tyuint: fprintf(fd, "uint8_t %s", name); break;
- case Tyuint32: fprintf(fd, "uint32_t %s", name); break;
- case Tyuint64: fprintf(fd, "uint64_t %s", name); break;
- case Tyulong: fprintf(fd, "uint64_t %s", name); break;
-
- case Tyflt32: fprintf(fd, "float %s", name); break;
- case Tyflt64: fprintf(fd, "double %s", name); break;
-
- case Typtr:
- writecdecl(fd, ty->sub[0], "");
- fprintf(fd, "(*%s)", name);
- break;
-
- case Tyfunc:
- writecdecl(fd, ty->sub[0], name);
- fprintf(fd, " %s(", name);
- sep = "";
- for (i = 1; i < ty->nsub; i++) {
- fprintf(fd, "%s", sep);
- if (ty->subname[i]) {
- writecdecl(fd, ty->sub[i], ty->subname[i]);
- } else {
- snprintf(buf, sizeof buf, "a%zd", i);
- writecdecl(fd, ty->sub[i], buf);
- }
- sep = ", ";
+ case Tynone: die("invalid type\n"); break;
+
+ case Tyvoid: fprintf(fd, "void"); break;
+ case Tybool: fprintf(fd, "bool %s", name); break;
+ case Tychar: fprintf(fd, "uint32_t %s", name); break;
+
+ case Tyint8: fprintf(fd, "int8_t %s", name); break;
+ case Tyint16: fprintf(fd, "int16_t %s", name); break;
+ case Tyint: fprintf(fd, "int32_t %s", name); break;
+ case Tyint32: fprintf(fd, "int32_t %s", name); break;
+ case Tyint64: fprintf(fd, "int64_t %s", name); break;
+ case Tylong: fprintf(fd, "int64_t %s", name); break;
+
+ case Tybyte: fprintf(fd, "uint8_t %s", name); break;
+ case Tyuint8: fprintf(fd, "uint8_t %s", name); break;
+ case Tyuint16: fprintf(fd, "uint16_t %s", name); break;
+ case Tyuint: fprintf(fd, "uint8_t %s", name); break;
+ case Tyuint32: fprintf(fd, "uint32_t %s", name); break;
+ case Tyuint64: fprintf(fd, "uint64_t %s", name); break;
+ case Tyulong: fprintf(fd, "uint64_t %s", name); break;
+
+ case Tyflt32: fprintf(fd, "float %s", name); break;
+ case Tyflt64: fprintf(fd, "double %s", name); break;
+
+ case Typtr:
+ writecdecl(fd, ty->sub[0], "");
+ fprintf(fd, "(*%s)", name);
+ break;
+
+ case Tyfunc:
+ writecdecl(fd, ty->sub[0], name);
+ fprintf(fd, " %s(", name);
+ sep = "";
+ for (i = 1; i < ty->nsub; i++) {
+ fprintf(fd, "%s", sep);
+ if (ty->subname[i]) {
+ writecdecl(fd, ty->sub[i], ty->subname[i]);
+ } else {
+ snprintf(buf, sizeof buf, "a%zd", i);
+ writecdecl(fd, ty->sub[i], buf);
}
- fprintf(fd, ")");
- break;
-
- case Tyarray:
- writecdecl(fd, ty->sub[0], name);
- if (ty->arraysz)
- fprintf(fd, "[%zd]", ty->arraysz);
- else
- fprintf(fd, "[]");
- break;
- case Tystruct:
- fprintf(fd, "struct %s {\n", name);
- for (i = 0; i < ty->nsub; i++) {
- writecdecl(fd, ty->sub[i], ty->subname[i]);
- fprintf(fd, "\n");
- }
- fprintf(fd, "}");
- break;
- case Tyunion:
- fprintf(fd, "union %s {\n", name);
- for (i = 0; i < ty->nsub; i++) {
- writecdecl(fd, ty->sub[i], ty->subname[i]);
- fprintf(fd, ";\n");
- }
- fprintf(fd, "}");
- break;
- case Tyname:
- if (ty->sub)
- fprintf(fd, "%s %s", ty->defname, name);
- else
- fprintf(fd, "(struct {byte buf[%zu];}) %s", ty->size, name);
+ sep = ", ";
+ }
+ fprintf(fd, ")");
+ break;
+
+ case Tyarray:
+ writecdecl(fd, ty->sub[0], name);
+ if (ty->arraysz)
+ fprintf(fd, "[%zd]", ty->arraysz);
+ else
+ fprintf(fd, "[]");
+ break;
+ case Tystruct:
+ fprintf(fd, "struct %s {\n", name);
+ for (i = 0; i < ty->nsub; i++) {
+ writecdecl(fd, ty->sub[i], ty->subname[i]);
+ fprintf(fd, "\n");
+ }
+ fprintf(fd, "}");
+ break;
+ case Tyunion:
+ fprintf(fd, "union %s {\n", name);
+ for (i = 0; i < ty->nsub; i++) {
+ writecdecl(fd, ty->sub[i], ty->subname[i]);
+ fprintf(fd, ";\n");
+ }
+ fprintf(fd, "}");
break;
+ case Tyname:
+ if (ty->sub)
+ fprintf(fd, "%s %s", ty->defname, name);
+ else
+ fprintf(fd, "(struct {byte buf[%zu];}) %s", ty->size, name);
+ break;
}
}
-void writewrapper(FILE *fd, Dcl *dcl)
+void writewrapper(FILE *fd, Pkg *pkg, Dcl *dcl)
{
size_t i, argoff;
char buf[64];
@@ -105,7 +105,7 @@ void writewrapper(FILE *fd, Dcl *dcl)
fprintf(fd, "{\n");
fprintf(fd, "\t");
- if (needswrapper(ty->sub[0])) {
+ if (needswrapper(pkg, ty->sub[0])) {
argoff = 1;
fprintf(fd, "*a1 = ");
} else if (ty->sub[0]->type != Tyvoid) {
@@ -115,7 +115,7 @@ void writewrapper(FILE *fd, Dcl *dcl)
fprintf(fd, "%s(", dcl->name);
sep = "";
for (i = 1; i < ty->nsub; i++) {
- if (needswrapper(ty->sub[i]))
+ if (needswrapper(pkg, ty->sub[i]))
fprintf(fd, "*");
if (ty->subname[i]) {
fprintf(fd, "%s%s", sep, ty->subname[i]);
@@ -129,16 +129,16 @@ void writewrapper(FILE *fd, Dcl *dcl)
fprintf(fd, "}\n");
}
-void aliasdecl(FILE *fd, Dcl *dcl, char *pkg)
+void aliasdecl(FILE *fd, Pkg *pkg, Dcl *dcl, char *pkgname)
{
char buf[512];
- snprintf(buf, sizeof buf, "%s$%s", pkg, dcl->name);
+ snprintf(buf, sizeof buf, "%s$%s", pkgname, dcl->name);
writecdecl(fd, dcl->type, buf);
fprintf(fd, "__attribute__((weak, alias(%s)))", dcl->name);
}
-void funcdcl(FILE *fd, Dcl *dcl, char *pkg)
+void funcdcl(FILE *fd, Pkg *pkg, Dcl *dcl, char *pkgname)
{
char buf[512];
char *suffix;
@@ -146,42 +146,42 @@ void funcdcl(FILE *fd, Dcl *dcl, char *pkg)
suffix = "";
w = dcl->type;
- if (funcneedswrapper(dcl->type)) {
+ if (funcneedswrapper(pkg, dcl->type)) {
suffix = "_wrapped";
- w = wrappedtype(w);
+ w = wrappedtype(pkg, w);
}
- snprintf(buf, sizeof buf, "%s$%s%s", pkg, dcl->name, suffix);
+ snprintf(buf, sizeof buf, "%s$%s%s", pkgname, dcl->name, suffix);
writecdecl(fd, w, buf);
fprintf(fd, "\n");
- writewrapper(fd, dcl);
+ writewrapper(fd, pkg, dcl);
}
-void writecdcl(FILE *fd, Dcl *dcl, char *pkg)
+void writecdcl(FILE *fd, Pkg *pkg, Dcl *dcl, char *pkgname)
{
if (dcl->type->type == Tyfunc)
- funcdcl(fd, dcl, pkg);
+ funcdcl(fd, pkg, dcl, pkgname);
else
- aliasdecl(fd, dcl, pkg);
+ aliasdecl(fd, pkg, dcl, pkgname);
}
-void writeheaders(FILE *fd)
+void writeheaders(Pkg *pkg, FILE *fd)
{
size_t i;
fprintf(fd, "/*\n");
- fprintf(fd, "This file is generated by the Myrddin C binding generator.\n");
+ fprintf(fd, "This file is generated by the Myrddin C curpkg generator.\n");
fprintf(fd, "Unless you like having your work clobbered, I would suggest\n");
fprintf(fd, "that you edit elsewhere\n");
fprintf(fd, "*/\n\n");
fprintf(fd, "#include <stdint.h>\n");
fprintf(fd, "#include <stddef.h>\n");
- for (i = 0; i < binding.nhdr; i++) {
- fprintf(fd, "#include <%s>\n", binding.hdr[i]);
+ for (i = 0; i < pkg->nhdr; i++) {
+ fprintf(fd, "#include <%s>\n", pkg->hdr[i]);
}
fprintf(fd, "\n");
}
-void gluegen(char *hdr)
+void gluegen(Pkg *pkg, char *hdr)
{
char out[512];
size_t i;
@@ -189,10 +189,10 @@ void gluegen(char *hdr)
swapsuffix(out, sizeof out, hdr, ".h", "-glue.c");
fd = fopen(out, "w");
- writeheaders(fd);
+ writeheaders(pkg, fd);
- for (i = 0; i < binding.ndcl; i++) {
- writecdcl(fd, binding.dcl[i], binding.pkg);
+ for (i = 0; i < pkg->ndcl; i++) {
+ writecdcl(fd, pkg, pkg->dcl[i], pkg->pkg);
}
fclose(fd);
}
diff --git a/cparse.c b/cparse.c
index 2822c6d..abc9a41 100644
--- a/cparse.c
+++ b/cparse.c
@@ -11,8 +11,8 @@
#include "util.h"
#include "mcbind.h"
-Type *gettype(CXCursor c, CXType t);
-Type *gettypex(CXCursor c, CXType t, const char *tydef, bool defn);
+Type *gettype(Pkg *pkg, CXCursor c, CXType t);
+Type *gettypex(Pkg *pkg, CXCursor c, CXType t, const char *tydef, bool defn);
void p(CXCursor c)
{
@@ -76,38 +76,47 @@ char *striptype(const char *s)
return strdup(s);
}
-enum CXChildVisitResult
-addmembers(CXCursor c, CXCursor pc, CXClientData d)
+typedef struct PkgTypePair PkgTypePair;
+struct PkgTypePair {
+ Pkg *pkg;
+ Type *type;
+};
+
+enum CXChildVisitResult addmembers(CXCursor c, CXCursor pc, CXClientData d)
{
CXString str, dname;
CXType mtype;
Type *ty, *mty;
char buf[64];
char *name;
+ PkgTypePair *ptp;
+ Pkg *pkg;
- ty = d;
+ ptp = d;
+ ty = ptp->type;
+ pkg = ptp->pkg;
mtype = clang_getCursorType(c);
switch (clang_getCursorKind(c)) {
- case CXCursor_FieldDecl:
- dname = clang_getCursorSpelling(c);
- name = strdup(clang_getCString(dname));
- mty = gettype(c, mtype);
- break;
- case CXCursor_UnionDecl:
- snprintf(buf, sizeof buf, "__anon%zd", ty->nsub);
- name = strdup(buf);
- mty = gettypex(c, mtype, name, true);
- case CXCursor_StructDecl:
- snprintf(buf, sizeof buf, "__anon%zd", ty->nsub);
- name = strdup(buf);
- mty = gettypex(c, mtype, name, true);
- break;
- default:
- str = clang_getCursorSpelling(c);
- printf("%s is not a member\n", clang_getCString(str));
- clang_disposeString(str);
- return CXChildVisit_Continue;
+ case CXCursor_FieldDecl:
+ dname = clang_getCursorSpelling(c);
+ name = strdup(clang_getCString(dname));
+ mty = gettype(pkg, c, mtype);
+ break;
+ case CXCursor_UnionDecl:
+ snprintf(buf, sizeof buf, "__anon%zd", ty->nsub);
+ name = strdup(buf);
+ mty = gettypex(pkg, c, mtype, name, true);
+ case CXCursor_StructDecl:
+ snprintf(buf, sizeof buf, "__anon%zd", ty->nsub);
+ name = strdup(buf);
+ mty = gettypex(pkg, c, mtype, name, true);
+ break;
+ default:
+ str = clang_getCursorSpelling(c);
+ printf("%s is not a member\n", clang_getCString(str));
+ clang_disposeString(str);
+ return CXChildVisit_Continue;
}
lappend(&ty->sub, &ty->nsub, mty);
lappend(&ty->subname, &ty->nsubname, name);
@@ -115,10 +124,11 @@ addmembers(CXCursor c, CXCursor pc, CXClientData d)
}
-Type *gettyperecord(CXType t, const char *tydef)
+Type *gettyperecord(Pkg *pkg, CXType t, const char *tydef)
{
- CXCursor c;
+ PkgTypePair ptp;
CXString str;
+ CXCursor c;
Type *ty;
char *s;
@@ -128,23 +138,28 @@ Type *gettyperecord(CXType t, const char *tydef)
else
s = striptype(clang_getCString(str));
c = clang_getTypeDeclaration(t);
+ ptp.pkg = pkg;
switch (clang_getCursorKind(c)) {
- case CXCursor_UnionDecl:
- ty = mktyunion(s, NULL, 0);
- clang_visitChildren(c, addmembers, ty);
- break;
- case CXCursor_StructDecl:
- ty = mktystruct(s, NULL, 0);
- clang_visitChildren(c, addmembers, ty);
- break;
- default:
- die("skipped kind: %s\n", clang_getCString(clang_getCursorSpelling(c)));
- break;
+ case CXCursor_UnionDecl:
+ ty = mktyunion(s, NULL, 0);
+ ptp.type = ty;
+ htput(pkg->tydefs, ty->defname, ty);
+ clang_visitChildren(c, addmembers, &ptp);
+ break;
+ case CXCursor_StructDecl:
+ ty = mktystruct(s, NULL, 0);
+ ptp.type = ty;
+ htput(pkg->tydefs, ty->defname, ty);
+ clang_visitChildren(c, addmembers, &ptp);
+ break;
+ default:
+ die("skipped kind: %s\n", clang_getCString(clang_getCursorSpelling(c)));
+ break;
}
return ty;
}
-Type *gettypex(CXCursor c, CXType t, const char *tydef, bool defn)
+Type *gettypex(Pkg *pkg, CXCursor c, CXType t, const char *tydef, bool defn)
{
CXString str;
CXType subt;
@@ -153,110 +168,110 @@ Type *gettypex(CXCursor c, CXType t, const char *tydef, bool defn)
const char *s;
switch (t.kind) {
- case CXType_Invalid:
- case CXType_Void: ty = mktype(Tyvoid); break;
- case CXType_Bool: ty = mktype(Tybool); break;
- case CXType_Char_U: ty = mktype(Tybyte); break;
- case CXType_UChar: ty = mktype(Tybyte); break;
- case CXType_Char32: ty = mktype(Tychar); break;
- case CXType_UShort: ty = mktype(Tyuint16); break;
- case CXType_UInt: ty = mktype(Tyuint32); break;
- case CXType_ULong: ty = mktype(Tyuint64); break;
- case CXType_ULongLong: ty = mktype(Tyuint64); break;
-
- case CXType_Char_S: ty = mktype(Tyint8); break;
- case CXType_SChar: ty = mktype(Tyint8); break;
- case CXType_Short: ty = mktype(Tyint16); break;
- case CXType_Int: ty = mktype(Tyint32); break;
- case CXType_Long: ty = mktype(Tyint64); break;
- case CXType_LongLong: ty = mktype(Tyint64); break;
-
- case CXType_Float: ty = mktype(Tyflt32); break;
- case CXType_Double: ty = mktype(Tyflt64); break;
-
- case CXType_ConstantArray:
- subt = clang_getArrayElementType(t);
- sz = clang_getArraySize(t);
- ty = mktyarray(gettype(c, subt), sz);
- break;
- case CXType_IncompleteArray:
- subt = clang_getArrayElementType(t);
- ty = mktyptr(gettype(c, subt));
- break;
- case CXType_Pointer:
- subt = clang_getPointeeType(t);
- ty = mktyptr(gettype(c, subt));
- break;
- case CXType_Enum:
- printf("ENUM %s\n", clang_getCString(clang_getTypeSpelling(t)));
- ty = NULL;
- break;
- case CXType_Typedef:
- str = clang_getTypeSpelling(t);
- s = clang_getCString(str);
- if (!strcmp(s, "__builtin_va_list"))
- ty = mktyptr(mktype(Tyvoid));
- else
- ty = mktyname(strdup(s));
- clang_disposeString(str);
- break;
-
- case CXType_FunctionNoProto:
- case CXType_FunctionProto:
- ty = mktype(Tyfunc);
- lappend(&ty->sub, &ty->nsub, gettype(c, clang_getResultType(t)));
- lappend(&ty->subname, &ty->nsubname, NULL);
- narg = clang_getNumArgTypes(t);
- for (i = 0; i < narg; i++) {
- subt = clang_getArgType(t, i);
- lappend(&ty->sub, &ty->nsub, gettype(c, subt));
- lappend(&ty->subname, &ty->nsubname, NULL);
- }
- break;
- /* union and enum */
- case CXType_Record:
- if (!defn) {
- str = clang_getTypeSpelling(t);
- ty = mktyname(striptype(clang_getCString(str)));
- clang_disposeString(str);
- } else {
- ty = gettyperecord(t, tydef);
- }
- break;
- /* funky char types */
- case CXType_Char16: ty = mktype(Tyint16); break;
- case CXType_WChar: ty = mktype(Tyint32); break;
- /* Unsupported or non-C type */
- case CXType_Unexposed:
- str = clang_getTypeSpelling(t);
- s = clang_getCString(str);
- parsefail(c, "warning: unexposed type %s", s);
- if (!strcmp(s, "__builtin_va_list"))
- ty = mktyptr(mktype(Tyvoid));
- else
- ty = mktyname(striptype(clang_getCString(str)));
- clang_disposeString(str);
- break;
- default:
- str = clang_getTypeSpelling(t);
- parsefail(c, "unsupported type %s", clang_getCString(str));
- exit(0);
- break;
+ case CXType_Invalid:
+ case CXType_Void: ty = mktype(Tyvoid); break;
+ case CXType_Bool: ty = mktype(Tybool); break;
+ case CXType_Char_U: ty = mktype(Tybyte); break;
+ case CXType_UChar: ty = mktype(Tybyte); break;
+ case CXType_Char32: ty = mktype(Tychar); break;
+ case CXType_UShort: ty = mktype(Tyuint16); break;
+ case CXType_UInt: ty = mktype(Tyuint32); break;
+ case CXType_ULong: ty = mktype(Tyuint64); break;
+ case CXType_ULongLong: ty = mktype(Tyuint64); break;
+
+ case CXType_Char_S: ty = mktype(Tyint8); break;
+ case CXType_SChar: ty = mktype(Tyint8); break;
+ case CXType_Short: ty = mktype(Tyint16); break;
+ case CXType_Int: ty = mktype(Tyint32); break;
+ case CXType_Long: ty = mktype(Tyint64); break;
+ case CXType_LongLong: ty = mktype(Tyint64); break;
+
+ case CXType_Float: ty = mktype(Tyflt32); break;
+ case CXType_Double: ty = mktype(Tyflt64); break;
+
+ case CXType_ConstantArray:
+ subt = clang_getArrayElementType(t);
+ sz = clang_getArraySize(t);
+ ty = mktyarray(gettype(pkg, c, subt), sz);
+ break;
+ case CXType_IncompleteArray:
+ subt = clang_getArrayElementType(t);
+ ty = mktyptr(gettype(pkg, c, subt));
+ break;
+ case CXType_Pointer:
+ subt = clang_getPointeeType(t);
+ ty = mktyptr(gettype(pkg, c, subt));
+ break;
+ case CXType_Enum:
+ printf("ENUM %s\n", clang_getCString(clang_getTypeSpelling(t)));
+ ty = NULL;
+ break;
+ case CXType_Typedef:
+ str = clang_getTypeSpelling(t);
+ s = clang_getCString(str);
+ if (!strcmp(s, "__builtin_va_list"))
+ ty = mktyptr(mktype(Tyvoid));
+ else
+ ty = mktyname(strdup(s));
+ clang_disposeString(str);
+ break;
+
+ case CXType_FunctionNoProto:
+ case CXType_FunctionProto:
+ ty = mktype(Tyfunc);
+ lappend(&ty->sub, &ty->nsub, gettype(pkg, c, clang_getResultType(t)));
+ lappend(&ty->subname, &ty->nsubname, NULL);
+ narg = clang_getNumArgTypes(t);
+ for (i = 0; i < narg; i++) {
+ subt = clang_getArgType(t, i);
+ lappend(&ty->sub, &ty->nsub, gettype(pkg, c, subt));
+ lappend(&ty->subname, &ty->nsubname, NULL);
+ }
+ break;
+ /* union and enum */
+ case CXType_Record:
+ if (!defn) {
+ str = clang_getTypeSpelling(t);
+ ty = mktyname(striptype(clang_getCString(str)));
+ clang_disposeString(str);
+ } else {
+ ty = gettyperecord(pkg, t, tydef);
+ }
+ break;
+ /* funky char types */
+ case CXType_Char16: ty = mktype(Tyint16); break;
+ case CXType_WChar: ty = mktype(Tyint32); break;
+ /* Unsupported or non-C type */
+ case CXType_Unexposed:
+ str = clang_getTypeSpelling(t);
+ s = clang_getCString(str);
+ parsefail(c, "warning: unexposed type %s", s);
+ if (!strcmp(s, "__builtin_va_list"))
+ ty = mktyptr(mktype(Tyvoid));
+ else
+ ty = mktyname(striptype(clang_getCString(str)));
+ clang_disposeString(str);
+ break;
+ default:
+ str = clang_getTypeSpelling(t);
+ parsefail(c, "unsupported type %s", clang_getCString(str));
+ exit(0);
+ break;
}
ty->size = clang_Type_getSizeOf(t);
return ty;
}
-Type *gettype(CXCursor c, CXType t)
+Type *gettype(Pkg *pkg, CXCursor c, CXType t)
{
Type *ty;
- ty = gettypex(c, t, NULL, false);
+ ty = gettypex(pkg, c, t, NULL, false);
assert(ty != NULL);
return ty;
}
-void addfuncdcl(CXCursor c)
+void addfuncdcl(Pkg *pkg, CXCursor c)
{
CXString dname, dtype;
CXType type;
@@ -274,10 +289,10 @@ void addfuncdcl(CXCursor c)
goto dispose;
}
n = strdup(clang_getCString(dname));
- ty = gettype(c, type);
+ ty = gettype(pkg, c, type);
if (ty) {
dcl = mkdcl(n, ty, 1);
- lappend(&binding.dcl, &binding.ndcl, dcl);
+ lappend(&pkg->dcl, &pkg->ndcl, dcl);
}
dispose:
@@ -285,7 +300,7 @@ dispose:
clang_disposeString(dtype);
}
-void addvardcl(CXCursor c)
+void addvardcl(Pkg *pkg, CXCursor c)
{
CXString dname;
CXType type;
@@ -297,16 +312,16 @@ void addvardcl(CXCursor c)
dname = clang_getCursorSpelling(c);
n = strdup(clang_getCString(dname));
- ty = gettype(c, type);
+ ty = gettype(pkg, c, type);
if (ty) {
dcl = mkdcl(n, ty, 1);
- lappend(&binding.dcl, &binding.ndcl, dcl);
+ lappend(&pkg->dcl, &pkg->ndcl, dcl);
}
clang_disposeString(dname);
}
-void addtypedef(CXCursor c)
+void addtypedef(Pkg *pkg, CXCursor c)
{
CXString str;
CXCursor dcl;
@@ -318,79 +333,81 @@ void addtypedef(CXCursor c)
str = clang_getCursorSpelling(c);
sub = clang_getTypedefDeclUnderlyingType(c);
switch (sub.kind) {
- case CXType_Unexposed:
- dcl = clang_getTypeDeclaration(sub);
- sub = clang_getCursorType(dcl);
- defn = false;
- break;
- default:
- dcl = c;
- defn = true;
- break;
+ case CXType_Unexposed:
+ dcl = clang_getTypeDeclaration(sub);
+ sub = clang_getCursorType(dcl);
+ defn = false;
+ break;
+ default:
+ dcl = c;
+ defn = true;
+ break;
};
s = strdup(clang_getCString(str));
- ty = gettypex(dcl, sub, s, defn);
- if (!htget(binding.tydefs, s))
- htput(binding.tydefs, s, ty);
+ ty = gettypex(pkg, dcl, sub, s, defn);
+ if (!htget(pkg->tydefs, s))
+ htput(pkg->tydefs, s, ty);
clang_disposeString(str);
}
-void addstructdef(CXCursor c)
+void addstructdef(Pkg *pkg, CXCursor c)
{
Type *ty;
- ty = gettypex(c, clang_getCursorType(c), NULL, true);
- htput(binding.tydefs, ty->defname, ty);
+ ty = gettypex(pkg, c, clang_getCursorType(c), NULL, true);
+ htput(pkg->tydefs, ty->defname, ty);
}
-void adduniondef(CXCursor c)
+void adduniondef(Pkg *pkg, CXCursor c)
{
Type *ty;
- ty = gettypex(c, clang_getCursorType(c), NULL, true);
- htput(binding.tydefs, ty->defname, ty);
+ ty = gettypex(pkg, c, clang_getCursorType(c), NULL, true);
+ htput(pkg->tydefs, ty->defname, ty);
}
enum CXChildVisitResult scrapedcl(CXCursor c, CXCursor pc, CXClientData d)
{
+ Pkg *pkg;
+
+ pkg = d;
switch (clang_getCursorKind(c)) {
- case CXCursor_FunctionDecl:
- addfuncdcl(c);
- break;
- case CXCursor_VarDecl:
- addvardcl(c);
- break;
- case CXCursor_TypedefDecl:
- addtypedef(c);
- break;
- case CXCursor_StructDecl:
- addstructdef(c);
- break;
- case CXCursor_UnionDecl:
- adduniondef(c);
- break;
- default:
- printf("skipped kind: %d\n", clang_getCursorKind(c));
- break;
+ case CXCursor_FunctionDecl:
+ addfuncdcl(pkg, c);
+ break;
+ case CXCursor_VarDecl:
+ addvardcl(pkg, c);
+ break;
+ case CXCursor_TypedefDecl:
+ addtypedef(pkg, c);
+ break;
+ case CXCursor_StructDecl:
+ addstructdef(pkg, c);
+ break;
+ case CXCursor_UnionDecl:
+ adduniondef(pkg, c);
+ break;
+ default:
+ printf("skipped kind: %d\n", clang_getCursorKind(c));
+ break;
}
return CXChildVisit_Continue;
}
- void
-loadhdr(char *f)
+void loadhdr(Pkg *pkg, char *f)
{
char **args;
size_t nargs;
CXTranslationUnit tu;
- lappend(&binding.hdr, &binding.nhdr, f);
+ lappend(&pkg->hdr, &pkg->nhdr, f);
initargs(&args, &nargs, f);
CXIndex idx = clang_createIndex(0, 1);
tu = clang_parseTranslationUnit(
idx, 0,
(const char * const *) args, nargs,
0, 0, CXTranslationUnit_None);
- clang_visitChildren(clang_getTranslationUnitCursor(tu), scrapedcl, NULL);
+ clang_visitChildren(clang_getTranslationUnitCursor(tu), scrapedcl, pkg);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(idx);
}
diff --git a/main.c b/main.c
index 0ea47e0..90363c5 100644
--- a/main.c
+++ b/main.c
@@ -8,14 +8,12 @@
#include "mcbind.h"
#include "optctx.h"
-Binding binding = {0};
-
static void usage(char *bin)
{
printf("%s [-h] [-p pkg] [-P prefix] hdrs..\n", bin);
printf("\t-h\tprint this help\n");
- printf("\t-p pkg\tgenerate bindings in package 'pkg'\n");
+ printf("\t-p pkg\tgenerate curpkgs in package 'pkg'\n");
printf("\t-P pfx\tstrip 'pfx' from C function names\n");
printf("\t-H inc\tinclude header `inc` before the headers");
exit(0);
@@ -26,18 +24,19 @@ int main(int argc, char **argv)
Optctx ctx;
char *out;
size_t i;
+ Pkg pkg;
optinit(&ctx, "hp:P:", argv, argc);
- binding.tydefs = mkht(strhash, streq);
+ pkg.tydefs = mkht(strhash, streq);
while (!optdone(&ctx)) {
switch (optnext(&ctx)) {
- case 'h': usage(argv[0]); break;
- case 'p': binding.pkg = ctx.optarg; break;
- case 'P': binding.prefix = ctx.optarg; break;
+ case 'h': usage(argv[0]); break;
+ case 'p': pkg.pkg = ctx.optarg; break;
+ case 'P': pkg.prefix = ctx.optarg; break;
}
}
- if (!binding.pkg) {
+ if (!pkg.pkg) {
fprintf(stderr, "package name is mandatory\n");
exit(1);
}
@@ -46,12 +45,12 @@ int main(int argc, char **argv)
exit(1);
}
- out = binding.pkg;
+ out = pkg.pkg;
for (i = 0; i < ctx.nargs; i++) {
- loadhdr(ctx.args[i]);
+ loadhdr(&pkg, ctx.args[i]);
}
- myrgen(out);
- gluegen(out);
+ myrgen(&pkg, out);
+ gluegen(&pkg, out);
return 0;
}
diff --git a/mcbind.h b/mcbind.h
index d65d2be..c1adfa7 100644
--- a/mcbind.h
+++ b/mcbind.h
@@ -1,6 +1,6 @@
typedef struct Type Type;
typedef struct Dcl Dcl;
-typedef struct Binding Binding;
+typedef struct Pkg Pkg;
typedef struct Loc Loc;
extern char *opt_pkgname;
@@ -64,7 +64,7 @@ struct Dcl {
char isconst;
};
-struct Binding {
+struct Pkg {
char *pkg; /* the Myrddin package name */
char *prefix; /* the prefix to remove from C funcs */
char **hdr; /* the C headers the glue needs to include */
@@ -76,27 +76,25 @@ struct Binding {
char **files; /* the source files loaded */
};
-extern Binding binding;
-
-void loadhdr(char *f);
+void loadhdr(Pkg *pkg, char *f);
/* type */
Type *mktype(Ty ty);
Type *mktyarray(Type *sub, size_t sz);
-Type *mktydefn(char *name, Type *sub);
Type *mktyptr(Type *sub);
Type *mktyname(char *s);
Type *mktystruct(char *s, Dcl **dcl, size_t ndcl);
Type *mktyunion(char *s, Dcl **dcl, size_t ndcl);
Type *mktyopaque(char *name, size_t sz);
-Type *tybase(Type *t);
-Type *wrappedtype(Type *ty);
-bool funcneedswrapper(Type *ty);
-bool needswrapper(Type *ty);
+
+Type *tybase(Pkg *pkg, Type *t);
+Type *wrappedtype(Pkg *pkg, Type *ty);
+bool funcneedswrapper(Pkg *pkg, Type *ty);
+bool needswrapper(Pkg *pkg, Type *ty);
/* code gen */
-void myrgen(char *hdr);
-void gluegen(char *hdr);
+void myrgen(Pkg *pkg, char *hdr);
+void gluegen(Pkg *pkg, char *hdr);
/* dcl */
Dcl *mkdcl(char *cname, Type *type, int isconst);
diff --git a/myrgen.c b/myrgen.c
index 7289825..52110c4 100644
--- a/myrgen.c
+++ b/myrgen.c
@@ -33,83 +33,83 @@ void writemyrtype(FILE *fd, Type *ty)
size_t i;
switch (ty->type) {
- case Tynone: die("invalid type\n"); break;
-
- case Tyvoid: fprintf(fd, "void"); break;
- case Tybool: fprintf(fd, "bool"); break;
- case Tychar: fprintf(fd, "char"); break;
-
- case Tyint8: fprintf(fd, "int8"); break;
- case Tyint16: fprintf(fd, "int16"); break;
- case Tyint: fprintf(fd, "int"); break;
- case Tyint32: fprintf(fd, "int32"); break;
- case Tyint64: fprintf(fd, "int64"); break;
- case Tylong: fprintf(fd, "int64"); break;
-
- case Tybyte: fprintf(fd, "byte"); break;
- case Tyuint8: fprintf(fd, "uint8"); break;
- case Tyuint16: fprintf(fd, "uint16"); break;
- case Tyuint: fprintf(fd, "uint"); break;
- case Tyuint32: fprintf(fd, "uint32"); break;
- case Tyuint64: fprintf(fd, "uint64"); break;
- case Tyulong: fprintf(fd, "uint64"); break;
-
- case Tyflt32: fprintf(fd, "flt32"); break;
- case Tyflt64: fprintf(fd, "flt64"); break;
-
- case Typtr:
- writemyrtype(fd, ty->sub[0]);
- fprintf(fd, "#");
- break;
-
- case Tyfunc:
- fprintf(fd, "(");
- writefunctypebody(fd, ty);
- fprintf(fd, ")");
- break;
-
- case Tyarray:
- writemyrtype(fd, ty->sub[0]);
- if (ty->arraysz)
- fprintf(fd, "[%zd]", ty->arraysz);
- else
- fprintf(fd, "[]");
- break;
- case Tystruct:
- fprintf(fd, "struct\n");
- for (i = 0; i < ty->nsub; i++) {
- fprintf(fd, "\t%s : ", ty->subname[i]);
- writemyrtype(fd, ty->sub[i]);
- fprintf(fd, "\n");
- }
- fprintf(fd, ";;");
- break;
- case Tyunion:
- fprintf(fd, "struct\n");
- for (i = 0; i < ty->nsub; i++) {
- fprintf(fd, "\t%s : ", ty->subname[i]);
- writemyrtype(fd, ty->sub[i]);
- fprintf(fd, "[0]\n");
- }
- fprintf(fd, "__pad : byte[%zd]\n", ty->size);
- fprintf(fd, ";;");
- break;
- case Tyname:
- fprintf(fd, "%s", ty->defname);
- break;
+ case Tynone: die("invalid type\n"); break;
+
+ case Tyvoid: fprintf(fd, "void"); break;
+ case Tybool: fprintf(fd, "bool"); break;
+ case Tychar: fprintf(fd, "char"); break;
+
+ case Tyint8: fprintf(fd, "int8"); break;
+ case Tyint16: fprintf(fd, "int16"); break;
+ case Tyint: fprintf(fd, "int"); break;
+ case Tyint32: fprintf(fd, "int32"); break;
+ case Tyint64: fprintf(fd, "int64"); break;
+ case Tylong: fprintf(fd, "int64"); break;
+
+ case Tybyte: fprintf(fd, "byte"); break;
+ case Tyuint8: fprintf(fd, "uint8"); break;
+ case Tyuint16: fprintf(fd, "uint16"); break;
+ case Tyuint: fprintf(fd, "uint"); break;
+ case Tyuint32: fprintf(fd, "uint32"); break;
+ case Tyuint64: fprintf(fd, "uint64"); break;
+ case Tyulong: fprintf(fd, "uint64"); break;
+
+ case Tyflt32: fprintf(fd, "flt32"); break;
+ case Tyflt64: fprintf(fd, "flt64"); break;
+
+ case Typtr:
+ writemyrtype(fd, ty->sub[0]);
+ fprintf(fd, "#");
+ break;
+
+ case Tyfunc:
+ fprintf(fd, "(");
+ writefunctypebody(fd, ty);
+ fprintf(fd, ")");
+ break;
+
+ case Tyarray:
+ writemyrtype(fd, ty->sub[0]);
+ if (ty->arraysz)
+ fprintf(fd, "[%zd]", ty->arraysz);
+ else
+ fprintf(fd, "[]");
+ break;
+ case Tystruct:
+ fprintf(fd, "struct\n");
+ for (i = 0; i < ty->nsub; i++) {
+ fprintf(fd, "\t%s : ", ty->subname[i]);
+ writemyrtype(fd, ty->sub[i]);
+ fprintf(fd, "\n");
+ }
+ fprintf(fd, ";;");
+ break;
+ case Tyunion:
+ fprintf(fd, "struct\n");
+ for (i = 0; i < ty->nsub; i++) {
+ fprintf(fd, "\t%s : ", ty->subname[i]);
+ writemyrtype(fd, ty->sub[i]);
+ fprintf(fd, "[0]\n");
+ }
+ fprintf(fd, "__pad : byte[%zd]\n", ty->size);
+ fprintf(fd, ";;");
+ break;
+ case Tyname:
+ fprintf(fd, "%s", ty->defname);
+ break;
}
}
-void writewrappedargs(FILE *fd, Type *ty)
+void writewrappedargs(FILE *fd, Pkg *pkg, Type *ty)
{
char *sep, *ptr;
size_t i, argoff;
sep = "";
- if (needswrapper(ty->sub[0]))
+ if (needswrapper(pkg, ty->sub[0]))
argoff = 1;
for (i = 1; i < ty->nsub; i++) {
- if (needswrapper(ty->sub[i]))
+ if (needswrapper(pkg, ty->sub[i]))
ptr = "&";
else
ptr = "";
@@ -121,7 +121,7 @@ void writewrappedargs(FILE *fd, Type *ty)
}
}
-void writewrapcall(FILE *fd, Dcl *d)
+void writewrapcall(FILE *fd, Pkg *pkg, Dcl *d)
{
Type *ty;
@@ -130,38 +130,38 @@ void writewrapcall(FILE *fd, Dcl *d)
writefunctypebody(fd, ty);
fprintf(fd, "\n");
- if (needswrapper(ty->sub[0])) {
+ if (needswrapper(pkg, ty->sub[0])) {
fprintf(fd, "\tvar ret\n");
fprintf(fd, "\t%s_wrapper(&ret, ", d->name);
- writewrappedargs(fd, ty);
+ writewrappedargs(fd, pkg, ty);
fprintf(fd, ")\n");
fprintf(fd, "\t-> ret\n");
} else {
fprintf(fd, "\t-> %s_wrapper(", d->name);
- writewrappedargs(fd, ty);
+ writewrappedargs(fd, pkg, ty);
fprintf(fd, ")\n");
}
fprintf(fd, "}\n\n");
}
-void writewraptype(FILE *fd, Dcl *d)
+void writewraptype(FILE *fd, Pkg *pkg, Dcl *d)
{
Type *w;
- w = wrappedtype(d->type);
+ w = wrappedtype(pkg, d->type);
fprintf(fd, "const %s_wrapper : ", d->name);
writemyrtype(fd, w);
}
-void writemyrdcl(FILE *fd, Dcl *d)
+void writemyrdcl(FILE *fd, Pkg *pkg, Dcl *d)
{
if (d->isconst)
fprintf(fd, "const %s ", d->name);
else
fprintf(fd, "var %s ", d->name);
- if (funcneedswrapper(d->type)) {
- writewrapcall(fd, d);
- writewraptype(fd, d);
+ if (funcneedswrapper(pkg, d->type)) {
+ writewrapcall(fd, pkg, d);
+ writewraptype(fd, pkg, d);
} else {
fprintf(fd, " : ");
writemyrtype(fd, d->type);
@@ -169,7 +169,7 @@ void writemyrdcl(FILE *fd, Dcl *d)
}
}
-void myrgen(char *hdr)
+void myrgen(Pkg *pkg, char *hdr)
{
char out[512];
size_t i, nk;
@@ -179,20 +179,20 @@ void myrgen(char *hdr)
swapsuffix(out, sizeof out, hdr, ".h", ".myr");
fd = fopen(out, "w");
fprintf(fd, "/*\n");
- fprintf(fd, "This file is generated by the Myrddin C binding generator.\n");
+ fprintf(fd, "This file is generated by the Myrddin C curpkg generator.\n");
fprintf(fd, "Unless you like having your work clobbered, I would suggest\n");
fprintf(fd, "that you edit elsewhere\n");
fprintf(fd, "*/\n\n");
- fprintf(fd, "pkg %s =\n", binding.pkg ? binding.pkg : "");
- k = htkeys(binding.tydefs, &nk);
+ fprintf(fd, "pkg %s =\n", pkg->pkg ? pkg->pkg : "");
+ k = htkeys(pkg->tydefs, &nk);
for (i = 0; i < nk; i++) {
fprintf(fd, "type %s = ", (char*)k[i]);
- writemyrtype(fd, htget(binding.tydefs, k[i]));
+ writemyrtype(fd, htget(pkg->tydefs, k[i]));
fprintf(fd, "\n");
}
- for (i = 0; i < binding.ndcl; i++) {
- writemyrdcl(fd, binding.dcl[i]);
+ for (i = 0; i < pkg->ndcl; i++) {
+ writemyrdcl(fd, pkg, pkg->dcl[i]);
fprintf(fd, "\n");
}
fprintf(fd, ";;\n");
diff --git a/types.c b/types.c
index f5e135e..4b1c1bb 100644
--- a/types.c
+++ b/types.c
@@ -36,15 +36,17 @@ Type *mktyptr(Type *sub)
return t;
}
+/*
Type *mktydefn(char *name, Type *sub)
{
Type *t;
t = mktype(Tyname);
lappend(&t->sub, &t->nsub, sub);
t->defname = name;
- htput(binding.tydefs, t->defname, t);
+ htput(curpkg.tydefs, t->defname, t);
return t;
}
+*/
Type *mktyname(char *name)
{
@@ -59,7 +61,6 @@ Type *mktystruct(char *name, Dcl **dcls, size_t ndcls)
Type *t;
t = mktype(Tystruct);
t->defname = name;
- htput(binding.tydefs, t->defname, t);
return t;
}
@@ -68,7 +69,6 @@ Type *mktyunion(char *name, Dcl **dcls, size_t ndcls)
Type *t;
t = mktype(Tyunion);
t->defname = name;
- htput(binding.tydefs, t->defname, t);
return t;
}
@@ -81,14 +81,14 @@ Type *mktyopaque(char *name, size_t sz)
return t;
}
-Type *tybase(Type *orig)
+Type *tybase(Pkg *pkg, Type *orig)
{
Type *ty, *sub;
ty = orig;
while (ty->type == Tyname) {
if (!ty->sub || !ty->sub[0]) {
- sub = htget(binding.tydefs, ty->defname);
+ sub = htget(pkg->tydefs, ty->defname);
if (!ty->sub)
lappend(&ty->sub, &ty->nsub, sub);
else
@@ -104,44 +104,44 @@ Type *tybase(Type *orig)
return ty;
}
-bool needswrapper(Type *orig)
+bool needswrapper(Pkg *pkg, Type *orig)
{
Type *ty;
- ty = tybase(orig);
+ ty = tybase(pkg, orig);
switch (ty->type) {
- case Tystruct: return 1;
- case Tyunion: return 1;
- case Tyarray: return 1;
- default: return 0;
+ case Tystruct: return 1;
+ case Tyunion: return 1;
+ case Tyarray: return 1;
+ default: return 0;
}
}
-bool funcneedswrapper(Type *ty)
+bool funcneedswrapper(Pkg *pkg, Type *ty)
{
size_t i;
- ty = tybase(ty);
+ ty = tybase(pkg, ty);
if (ty->type != Tyfunc)
return 0;
for (i = 0; i < ty->nsub; i++)
- if (needswrapper(ty->sub[i]))
+ if (needswrapper(pkg, ty->sub[i]))
return 1;
return 0;
}
-Type *wrappedtype(Type *ty)
+Type *wrappedtype(Pkg *pkg, Type *ty)
{
Type *w;
size_t i;
w = mktype(Tyfunc);
- if (needswrapper(ty->sub[0])) {
+ if (needswrapper(pkg, ty->sub[0])) {
lappend(&w->sub, &w->nsub, mktype(Tyvoid));
lappend(&w->subname, &w->nsubname, NULL);
}
for (i = 0; i < ty->nsub; i++) {
- if (needswrapper(ty->sub[i]))
+ if (needswrapper(pkg, ty->sub[i]))
lappend(&w->sub, &w->nsub, mktyptr(ty->sub[i]));
else
lappend(&w->sub, &w->nsub, ty->sub[i]);