unoidl2

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/unoidl2.git/
Log | Files | Refs

commit b2bb9f0e0b62d1527964f53ba9beb1272fc1a222
parent 1f3e79ebc387bb2406e4b76fe459d74e3c4afd54
Author: Tomas Hlavaty <tom@logand.com>
Date:   Sun, 18 Mar 2012 23:16:54 +0100

2java understands interned symbols

Diffstat:
Munoidl2.c | 7+++++++
Munoidl2.h | 1+
Munoidl2java.c | 279+++++++++++++++++++++++++++++++++----------------------------------------------
3 files changed, 123 insertions(+), 164 deletions(-)

diff --git a/unoidl2.c b/unoidl2.c @@ -256,6 +256,13 @@ Any mapc(Fn1 fn, Any x) { return z; } +Any mapcx(void *env, Any (*fn)(void *env, Any e), Any x) { + Any z = NIL; + for(Any y = x; NIL != y; y = cdr(y)) + z = fn(env, car(y)); + return z; +} + Any find(Any elt, Any lst, Fn2 cmp, Fn1 key) { for(; NIL != lst; lst = cdr(lst)) { Any x = car(lst); diff --git a/unoidl2.h b/unoidl2.h @@ -62,6 +62,7 @@ void print_sym2_custom(Any x, char *sep); Any print(Any x); Any some(Fn1 test, Any lst); Any mapc(Fn1 fn, Any x); +Any mapcx(void *env, Any (*fn)(void *env, Any e), Any x); Any find(Any elt, Any lst, Fn2 cmp, Fn1 key); Any reverse(Any x, Any a); Any id(Any x); diff --git a/unoidl2java.c b/unoidl2java.c @@ -23,19 +23,18 @@ extern int yyparse(); +extern const Any NIL; +extern const Any T; extern const Any ast; +extern const Any root2; -static Any module; static Any typedefs; static Any enums; static Any structs; static Any rel_package; -static Any uno_star_sun_com; -static Any Exception; -static Any RuntimeException; -static Any XInterface; static Any com_sun_star_uno_XInterface; -static Any Context; +static Any com_sun_star_uno_Exception; +static Any com_sun_star_uno_RuntimeException; static int template = 0; static int use_XInterface = 0; @@ -54,11 +53,6 @@ static void pp_list(Any x, char *sep) { } } -static void pr_module(Any x, char *sep, int dot) { - print_sym2_custom(x, sep); - if(dot) pr(sep); -} - static Any last(Any x) { if(consp(x)) { for(; NIL != x; x = cdr(x)) @@ -67,29 +61,32 @@ static Any last(Any x) { } else return x; } -static void pr_package(Any class) { - pr("///--- "); pr_module(module, "/", 0); pr(" "); pp(last(class)); pl(".java"); // TODO only toplevel +static void pr_package(Any x) { + Any name = cadr(x); + Any p = sym2p(name); + pr("///--- "); print_sym2_custom(p, "/"); pr(" "); pp(sym2k(name)); pl(".java"); // TODO only toplevel pl("// Generated by unoidl2java, do not edit!"); - pr("package "); pr_module(module, ".", 0); pl(";"); - for(Any x = cdr(module); NIL != x; x = cdr(x)) { - pr("import "); pr_module(x, ".", 0); pl(".*;"); - } + pr("// "); print(car(x)); pr(" "); print(name); pl(""); + pr("package "); pp(p); pl(";"); + /* for(p = sym2p(p); NIL != sym2k(p); p = sym2p(p)) { */ + /* pr("import "); pp(p); pl(".*;"); */ + /* } */ } static void pr_enum(Any x) { // (enum Name T ONE TWO (THREE 3)) - enums = cons(cons(reverse(module, list1(cadr(x))), cdddr(x)), enums); Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any values = cdddr(x); Any v0 = car(values); Any v0k = consp(v0) ? car(v0) : v0; - pr_package(name); - pr("public final class "); pp(name); pl(" extends com.sun.star.uno.Enum { // enum"); - pr(" private "); pp(name); pl("(int value) {"); + pr_package(x); + Any k = sym2k(name); + pr("public final class "); pp(k); pl(" extends com.sun.star.uno.Enum {"); + pr(" private "); pp(k); pl("(int value) {"); pl(" super(value);"); pl(" }"); - pr(" public static "); pp(name); pl(" getDefault() {"); + pr(" public static "); pp(k); pl(" getDefault() {"); pr(" return "); pp(v0k); pl(";"); pl(" }"); int n = 0; @@ -98,23 +95,24 @@ static void pr_enum(Any x) { // (enum Name T ONE TWO (THREE 3)) Any value = car(y); int explicit1 = consp(value); explicit |= explicit1; - char *k = token(explicit1 ? car(value) : value); + Any k2 = sym1(explicit1 ? car(value) : value); Any v = explicit1 ? cadr(value) : mkinum(n++); - pr(" public static final "); pp(name); pr(" "); pr(k); pr(" = new "); - pp(name); pr("("); pp(v); pl(");"); - pr(" public static final int "); pr(k); pr("_value = "); + pr(" public static final "); pp(k); pr(" "); pp(k2); pr(" = new "); + pp(k); pr("("); pp(v); pl(");"); + pr(" public static final int "); pp(k); pr("_value = "); if(!explicit || explicit1) pp(v); // syntax error if not + //else {pr("@@@"); print(v);} pl(";"); } - pr(" public static "); pp(name); pl(" fromInt(int value) {"); + pr(" public static "); pp(k); pl(" fromInt(int value) {"); pl(" switch(value) {"); pl(" default: return null;"); n = 0; for(Any y = values; NIL != y; y = cdr(y)) { Any value = car(y); - char *k = token(consp(value) ? car(value) : value); + Any k = sym1(consp(value) ? car(value) : value); Any v = consp(value) ? cadr(value) : mkinum(n++); - pr(" case "); pp(v); pr(": return "); pr(k); pl(";"); + pr(" case "); pp(v); pr(": return "); pp(k); pl(";"); } pl(" }"); //pr(" return "); pp(v0k); pl(";"); @@ -142,27 +140,6 @@ static void pr_args2(Any x) { // for in, out, inout } } -static Any matching_enum_p(Any e, Any x) { - // (relative com sun star uno XInterface)(com sun star uno TypeClass) - int z = equal_(cdr(e), x); - if(!z) - z = equal_(reverse(module, cdr(e)), x); - return z ? e : NIL; -} - -static Any matching_struct_p(Any e, Any x) { - // (relative com sun star uno XInterface)(com sun star uno TypeClass) - int z = equal_(cdr(e), x); - if(!z) - z = equal_(reverse(module, cdr(e)), x); - return z ? e : NIL; -} - -static Any find_enum(Any x) {return find(x, enums, matching_enum_p, car);} -static Any find_struct(Any x) {return find(x, structs, matching_struct_p, car);} - -static Any resolve_typedef(Any x) {return find(x, typedefs, equal, car);} - static Any pr_default_slot(Any slot) { Any n = car(slot); Any t = cadr(slot); @@ -180,11 +157,11 @@ static Any pr_default_slot(Any slot) { break; case RELATIVE: case ABSOLUTE: - if(NIL != (e = find_enum(t))) { + if(NIL != (e = find(t, enums, equal, id))) { Any d = cadr(e); pr(" this."); pp(n); pl(" = "); pp(t); pr("."); pp(consp(d) ? car(d) : d); pr(";"); - } else if(NIL != (e = find_struct(t))) { + } else if(NIL != (e = find(t, structs, equal, id))) { pr(" this."); pp(n); pr(" = new "); pp(t); pl("();"); } else if(NIL == cddr(t)) { Any y = resolve_typedef(cons(cadr(t), module)); @@ -208,7 +185,7 @@ static void pr_default_slots(Any slots) { static int pr_slots_rec1(Any super) { if(NIL == super) return 0; - Any s = find_struct(super); + Any s = find(super, structs, equal, id); if(pr_slots_rec1(cadr(s))) pr(", "); pr_args(cddr(s)); return 1; @@ -216,7 +193,7 @@ static int pr_slots_rec1(Any super) { static int pr_slots_rec2(Any super) { if(NIL == super) return 0; - Any s = find_struct(super); + Any s = find(super, structs, equal, id); int z = pr_slots_rec2(cadr(s)); for(Any y = cddr(s); NIL != y; y = cdr(y)) { if(0 < z++) pr(", "); @@ -302,7 +279,7 @@ static Any kindeq(Kind k, Any a) {return k == kind(a) ? T : NIL;} static Any readonlyp(Any a) {return kindeq(READONLY, a);} -static Any pr_TypeInfo1(Any x) { +static Any pr_TypeInfo1(void *env, Any x) { int f = 0; Any t = NIL; switch(kind(car(x))) { @@ -324,7 +301,7 @@ static Any pr_TypeInfo1(Any x) { if(NIL != caddr(x)) f |= _ONEWAY; pr_TypeInfo2("Method", method, t, f, env, -1); int i = -1; - mapc(&i, pr_TypeInfo1, caddddr(x)); + mapcx(&i, pr_TypeInfo1, caddddr(x)); break; case IN: // (in aType (type)) t = caddr(x); @@ -347,7 +324,7 @@ static Any pr_TypeInfo1(Any x) { return NIL; } -static Any pr_TypeInfo1_attribute(Any x) { +static Any pr_TypeInfo1_attribute(void *env, Any x) { if(ATTRIBUTE == kind(car(x))) { pr_TypeInfo1(env, x); if(NIL == some(readonlyp, cadddr(x))) @@ -356,7 +333,7 @@ static Any pr_TypeInfo1_attribute(Any x) { return NIL; } -static Any pr_TypeInfo1_nonattribute(Any x) { +static Any pr_TypeInfo1_nonattribute(void *env, Any x) { if(ATTRIBUTE != kind(car(x))) pr_TypeInfo1(env, x); return NIL; @@ -369,8 +346,8 @@ static void pr_TypeInfo(Any body) { pr(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); int i = -1; k = -1; - mapc(&i, pr_TypeInfo1_attribute, body); - mapc(&i, pr_TypeInfo1_nonattribute, body); + mapcx(&i, pr_TypeInfo1_attribute, body); + mapcx(&i, pr_TypeInfo1_nonattribute, body); pl(""); pl(" };"); } @@ -386,17 +363,17 @@ static void pr_extends(Any x) { } static void pr_struct(Any x) { - structs = cons(cons(reverse(module, list1(cadr(x))), cdddr(x)), structs); Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any super = cadddr(x); Any slots = cddddr(x); - pr_package(name); - pr("public class "); pp(name); + pr_package(x); + Any k = sym2k(name); + pr("public class "); pp(k); pr_extends(super); - pl(" { // struct"); - pr_slots(slots, name, super); + pl(" {"); + pr_slots(slots, k, super); pr_TypeInfo(slots); pl("}"); } @@ -405,30 +382,30 @@ static void pr_struct(Any x) { static void pr_exception(Any x) { Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any super = cadddr(x); Any body = cddddr(x); - pr_package(name); - pr("public class "); pp(name); - int m = equal_(module, uno_star_sun_com); - int e = equal_(name, Exception) && m; - int r = equal_(name, RuntimeException) && m; + pr_package(x); + Any k = sym2k(name); + pr("public class "); pp(k); + int e = k == com_sun_star_uno_Exception; + int r = k == com_sun_star_uno_RuntimeException; if(e) pr(" extends java.lang.Exception"); else if(r) pr(" extends java.lang.RuntimeException"); else pr_extends(super); - pl(" { // exception"); + pl(" {"); if(e || r) { pl(" public java.lang.Object Context;"); - pr(" public "); pp(name); pl("() {"); + pr(" public "); pp(k); pl("() {"); pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message) {"); - pl(" super(Message);"); + pr(" public "); pp(k); pl("(java.lang.String m) {"); + pl(" super(m);"); pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message, java.lang.Object Context) {"); - pl(" super(Message);"); - pl(" this.Context = Context;"); + pr(" public "); pp(k); pl("(java.lang.String m, java.lang.Object c) {"); + pl(" super(m);"); + pl(" this.Context = c;"); pl(" }"); pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); pl(" new com.sun.star.lib.uno.typeinfo.MemberTypeInfo(\"Context\", 0, 128)"); @@ -438,29 +415,34 @@ static void pr_exception(Any x) { Any slot = car(y); pr(" public "); pp(cadr(slot)); pr(" "); pp(car(slot)); pl(";"); } - pr(" public "); pp(name); pl("() {"); + pr(" public "); pp(k); pl("() {"); pr_default_slots(body); pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String m) {"); + pr(" public "); pp(k); pl("(java.lang.String m) {"); pl(" super(m);"); pr_default_slots(body); pl(" }"); - pr(" public "); pp(name); pr("(java.lang.String m, java.lang.Object o) {"); - pl(" super(m, o);"); - pr_default_slots(body); - pl(" }"); + /* pr(" public "); pp(k); pl("(java.lang.String m, java.lang.Object c) {"); */ + /* pl(" super(m, c);"); */ + /* pr_default_slots(body); */ + /* pl(" }"); */ if(NIL != body) { - pr(" public "); pp(name); + pr(" public "); pp(k); pr("(java.lang.String Message, java.lang.Object Context,"); - pr_args(body); + pr_args(body); // TODO inherited args! pl(") {"); - pl(" super(Message, Context);"); + pl(" super(Message, Context);"); // TODO inherited args! for(Any y = body; NIL != y; y = cdr(y)) { Any slot = car(y); Any n = car(slot); pr(" this."); pp(n); pr(" = "); pp(n); pl(";"); } pl(" }"); + } else { + pr(" public "); pp(k); pl("(java.lang.String m, java.lang.Object c) {"); + pl(" super(m, c);"); + pr_default_slots(body); + pl(" }"); } pr_TypeInfo(body); } @@ -469,25 +451,23 @@ static void pr_exception(Any x) { } static Any pr_definterface_attribute(Any x) { - if(ATTRIBUTE == kind(car(x))) - pp(x); + if(ATTRIBUTE == kind(car(x))) pp(x); return NIL; } static Any pr_definterface_nonattribute(Any x) { - if(ATTRIBUTE != kind(car(x))) - pp(x); + if(ATTRIBUTE != kind(car(x))) pp(x); return NIL; } static void pr_definterface(Any x) { Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any super = cadddr(x); Any body = cddddr(x); - pr_package(name); - pr("public interface "); pp(name); + pr_package(x); + pr("public interface "); pp(sym2k(name)); pr_extends(super); use_XInterface = 1; int i = NIL == super ? 0 : 1; @@ -498,12 +478,11 @@ static void pr_definterface(Any x) { pp(cadr(slot)); } } - if(NIL == super && i <= 0 - && !(equal_(name, XInterface) && equal_(module, uno_star_sun_com))) + if(NIL == super && i <= 0 && name != com_sun_star_uno_XInterface) pr(" extends com.sun.star.uno.XInterface"); use_XInterface = 0; pl(" {"); - if(!(equal_(name, XInterface) && equal_(module, uno_star_sun_com))) { + if(name != com_sun_star_uno_XInterface) { mapc(pr_definterface_attribute, body); mapc(pr_definterface_nonattribute, body); pr_TypeInfo(body); @@ -562,10 +541,11 @@ static void pr_arg(Any x) { static void pr_service(Any x) { Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any super = cadddr(x); // interface service Any body = caddddr(x); // accumulated service - pr_package(name); + if(!(NIL != super && NIL == body)) return; // TODO why not? + pr_package(x); pl("import com.sun.star.uno.XComponentContext;"); pl("import com.sun.star.uno.DeploymentException;"); pl("import com.sun.star.uno.TypeClass;"); @@ -573,7 +553,8 @@ static void pr_service(Any x) { pl("import com.sun.star.uno.UnoRuntime;"); pl("import com.sun.star.lang.XMultiComponentFactory;"); pr("public final class "); pp(last(name)); - pl(" { // service"); + //if(NIL != super) {pr(" implements "); pp(super);} + pl(" {"); if(NIL != super && NIL == body) { // implicit constructor Any s = super; if(RELATIVE == kind(car(s)) && NIL == cddr(s)) @@ -583,9 +564,9 @@ static void pr_service(Any x) { pr(" "); pp(super); pl(" x = null;"); pl(" try {"); pl(" XMultiComponentFactory f = $getFactory(c);"); - pr(" x = ("); pp(s); pr(") $castInstance(f.createInstanceWithContext(\""); pr_module(module, ".", 1); pp(name); pl("\", c), c);"); + pr(" x = ("); pp(s); pr(") $castInstance(f.createInstanceWithContext(\""); pp(name); pl("\", c), c);"); pl(" } catch(Exception e) {"); - pr(" throw new DeploymentException(\"component context fails to supply "); pr_module(module, ".", 1); pp(name); pr(" of type "); pp(s); pl(": \".concat(e.toString()), c);"); + pr(" throw new DeploymentException(\"component context fails to supply "); pp(name); pr(" of type "); pp(s); pl(": \".concat(e.toString()), c);"); pl(" }"); pl(" return x;"); pl(" }"); @@ -598,7 +579,7 @@ static void pr_service(Any x) { pl(" private static Object $castInstance(Object o, XComponentContext c) {"); pr(" java.lang.Object o2 = UnoRuntime.queryInterface(new Type(\""); pp(s); pl("\", TypeClass.INTERFACE), o);"); pl(" if(o2 == null)"); - pr(" throw new DeploymentException(\"component context fails to supply service "); pr_module(module, ".", 1); pp(name); pr(" of type "); pp(s); pl("\", c);"); + pr(" throw new DeploymentException(\"component context fails to supply service "); pp(name); pr(" of type "); pp(s); pl("\", c);"); pl(" return o2;"); pl(" }"); } @@ -628,10 +609,10 @@ static void pr_singleton(Any x) { static void pr_constants(Any x) { Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any body = cdddr(x); - pr_package(name); - pr("public interface "); pp(name); pl(" { // constants"); + pr_package(x); + pr("public interface "); pp(sym2k(name)); pl(" {"); pp_list(body, ""); pl("}"); } @@ -654,44 +635,12 @@ static void pr_const(Any x) { pl(";"); } -static void pr_typedef(Any x) { - typedefs = cons(cons(cons(cadr(x), module), cadddr(x)), typedefs); -} - -static void pr_relative(Any x) { - if(!use_XInterface && equal_(com_sun_star_uno_XInterface, cdr(x))) - pr("java.lang.Object"); - else { - if(NIL == cddr(x)) { // local typedef - Any y = resolve_typedef(cons(cadr(x), module)); - if(NIL != y) { - pp(cdr(y)); - return; - } - } - Any y = resolve_typedef(reverse(cdr(x), NIL)); - if(NIL != y) { // non-local typedef - Any a = car(y); // (ServiceContextList iop corba star sun com) - Any d = cdr(y); // (sequence (relative ServiceContext)) - rel_package = cons(cdr(a), rel_package); - pp(d); - rel_package = cdr(rel_package); - return; - } - Any p = car(rel_package); - if(NIL == cddr(x)) // x => (relative ServiceContext) p => (iop corba star sun com) - pp_list(reverse(cons(cadr(x), p), NIL), "."); - else - pp_list(cdr(x), "."); - } -} - static void pr_absolute(Any x) { - if(!use_XInterface && equal_(com_sun_star_uno_XInterface, cdr(x))) + if(!use_XInterface && x == com_sun_star_uno_XInterface) pr("java.lang.Object"); else { - Any y = resolve_typedef(reverse(cdr(x), NIL)); - if(NIL != y) { // non-local typedef + Any y = find(x, typedefs, equal, id); + if(NIL != y) { Any a = car(y); // (ServiceContextList iop corba star sun com) Any d = cdr(y); // (sequence (relative ServiceContext)) rel_package = cons(cdr(a), rel_package); @@ -699,7 +648,7 @@ static void pr_absolute(Any x) { rel_package = cdr(rel_package); return; } - pp_list(cdr(x), "."); + pp(cadr(x)); } } @@ -717,12 +666,12 @@ static void pr_property(Any x) { static void pr_deftemplate(Any x) { Any name = cadr(x); Any published = caddr(x); - if(NIL != published) { + if(1 || NIL != published) { Any args = cadddr(x); Any slots = cddddr(x); - pr_package(name); + pr_package(x); pr("public class "); pp(name); - pr("<"); pp_list(args, ", "); pr(">"); pl(" { // deftemplate"); + pr("<"); pp_list(args, ", "); pr(">"); pl(" {"); pr_slots(slots, name, NIL); pr_TypeInfo(slots); pl("}"); @@ -766,11 +715,7 @@ static void pp(Any x) { if(consp(x)) { Any a = car(x); switch(kind(a)) { - case MODULE: - module = cons(cadr(x), module); - pp_list(cddr(x), ""); - module = cdr(module); - break; + case MODULE: break; case ENUM: pr_enum(x); break; case STRUCT: pr_struct(x); break; case DEFINTERFACE: pr_definterface(x); break; @@ -779,7 +724,7 @@ static void pp(Any x) { //case IN: //case OUT: //case INOUT: pr_arg(x); break; - case RELATIVE: pr_relative(x); break; + case RELATIVE: case ABSOLUTE: pr_absolute(x); break; case CONSTANTS: pr_constants(x); break; case CONST: pr_const(x); break; @@ -789,7 +734,7 @@ static void pp(Any x) { case SERVICE: pr_service(x); break; case PROPERTY: pr_property(x); break; case SINGLETON: pr_singleton(x); break; - case TYPEDEF: pr_typedef(x); break; + case TYPEDEF: break; case VOID: print(car(x)); break; case BOOLEAN: case BYTE: @@ -830,6 +775,8 @@ static void pp(Any x) { printf("NIL"); } +static void walk2java(Any x, Any c, Any k, Any p, Any v) {pp(v);} + static Any subst2java(Any v) { v = subst2_rel_abs(v); switch(kind(car(v))) { @@ -843,21 +790,25 @@ static Any subst2java(Any v) { int main() { init(); yyparse(); - module = NIL; + typedefs = NIL; enums = NIL; structs = NIL; rel_package = NIL; - uno_star_sun_com = list4(mk(ID, "uno"), mk(ID, "star"), mk(ID, "sun"), mk(ID, "com")); - Exception = mk(ID, "Exception"); - RuntimeException = mk(ID, "RuntimeException"); - XInterface = mk(ID, "XInterface"); - com_sun_star_uno_XInterface = reverse(uno_star_sun_com, list1(XInterface)); - Context = mk(ID, "Context"); - pp_list(ast, NULL); - //print(typedefs); - //print(enums); + Any p = intern2abs(list4(mk(ID, "com"), mk(ID, "sun"), mk(ID, "star"), mk(ID, "uno"))); + com_sun_star_uno_XInterface = intern2rel(sym1(mk(ID, "XInterface")), p); + com_sun_star_uno_Exception = intern2rel(sym1(mk(ID, "Exception")), p); + com_sun_star_uno_RuntimeException = intern2rel(sym1(mk(ID, "RuntimeException")), p); + //pp_list(ast, NULL); + build2_fn = subst2java; mapc(build2, ast); + + //print(typedefs); + //print(enums); + //print(structs); + + walk2_fn = walk2java; + walk2(root2); return 0; }