unoidl2

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

commit 30216021755c72716db33c0342f293e5a7f91228
parent b54fa2ab0f67a6b74f6d8d2e73d2acb7a5f4a3a3
Author: Tomas Hlavaty <tom@logand.com>
Date:   Wed, 11 Jan 2012 00:39:41 +0100

2java improvements

Diffstat:
Munoidl2java.c | 401+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 240 insertions(+), 161 deletions(-)

diff --git a/unoidl2java.c b/unoidl2java.c @@ -33,8 +33,12 @@ 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 int template = 0; +static int use_XInterface = 0; +static int seq0 = 0; static inline void pr(char *x) {printf("%s", x);} static inline void pl(char *x) {printf("%s\n", x);} @@ -80,44 +84,46 @@ static void pr_package(Any class) { static void pr_enum(Any x) { Any name = cadr(x); Any published = caddr(x); - 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) {"); - pl(" super(value);"); - pl(" }"); - pr(" public static "); pp(name); pl(" getDefault() {"); - pr(" return "); pp(v0k); pl(";"); - pl(" }"); - int n = 0; - int explicit = 0; - for(Any y = values; !null(y); y = cdr(y)) { - Any value = car(y); - int explicit1 = consp(value); - explicit |= explicit1; - char *k = token(explicit1 ? car(value) : value); - Any v = explicit1 ? cadr(value) : mkinum(n++); - pr(" public static final int "); pr(k); pr("_value = "); - if(!explicit || explicit1) pp(v); // syntax error if not - pl(";"); - pr(" public static final "); pp(name); pr(" "); pr(k); pr(" = new "); - pp(name); pr("("); pp(v); pl(");"); - } - pr(" public static "); pp(name); pl(" fromInt(int value) {"); - pl(" switch(value) {"); - n = 0; - for(Any y = values; !null(y); y = cdr(y)) { - Any value = car(y); - char *k = token(consp(value) ? car(value) : value); - Any v = consp(value) ? cadr(value) : mkinum(n++); - pr(" case "); pp(v); pr(": return "); pr(k); pl(";"); - } - pl(" }"); - pr(" return "); pp(v0k); pl(";"); - pl(" }"); - pl("}"); + if(!null(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) {"); + pl(" super(value);"); + pl(" }"); + pr(" public static "); pp(name); pl(" getDefault() {"); + pr(" return "); pp(v0k); pl(";"); + pl(" }"); + int n = 0; + int explicit = 0; + for(Any y = values; !null(y); y = cdr(y)) { + Any value = car(y); + int explicit1 = consp(value); + explicit |= explicit1; + char *k = token(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 = "); + if(!explicit || explicit1) pp(v); // syntax error if not + pl(";"); + } + pr(" public static "); pp(name); pl(" fromInt(int value) {"); + pl(" switch(value) {"); + pr(" default: return null;"); + n = 0; + for(Any y = values; !null(y); y = cdr(y)) { + Any value = car(y); + char *k = token(consp(value) ? car(value) : value); + Any v = consp(value) ? cadr(value) : mkinum(n++); + pr(" case "); pp(v); pr(": return "); pr(k); pl(";"); + } + pl(" }"); + //pr(" return "); pp(v0k); pl(";"); + pl(" }"); + pl("}");} } static void pr_args(Any x) { @@ -136,24 +142,31 @@ static void pr_args2(Any x) { // for in, out, inout } } -static void pr_slots(Any slots, Any name) { +static void pr_default_slots(Any slots) { + // TODO init enum to default for(Any y = slots; !null(y); y = cdr(y)) { Any slot = car(y); Any n = car(slot); Any t = cadr(slot); - pr(" public "); pp(t); pr(" "); pp(n); pl(";"); + switch(kind(consp(t) ? car(t) : t)) { + case STRING: pr(" this."); pp(n); pl(" = \"\";"); break; + case ANY: pr(" this."); pp(n); pl(" = com.sun.star.uno.Any.VOID;"); break; + case TYPE: pr(" this."); pp(n); pl(" = com.sun.star.uno.Type.VOID;"); break; + case SEQUENCE: pr(" this."); pp(n); pr(" = new "); seq0 = 1; pp(t); seq0 = 0; pl(";"); break; + //default: pr("@@@"); print(t); pp(t); + } } - pr(" public "); pp(name); pl("() {"); - // TODO init enum to default +} + +static void pr_slots(Any slots, Any name) { for(Any y = slots; !null(y); y = cdr(y)) { Any slot = car(y); Any n = car(slot); Any t = cadr(slot); - switch(kind(t)) { - case STRING: pr(" this."); pp(n); pr(" = \"\";"); break; - case ANY: pr(" this."); pp(n); pr(" = Any.VOID;"); break; - } + pr(" public "); pp(t); pr(" "); pp(n); pl(";"); } + pr(" public "); pp(name); pl("() {"); + pr_default_slots(slots); pl(" }"); pr(" public "); pp(name); pr("("); pr_args(slots); pl(") {"); for(Any y = slots; !null(y); y = cdr(y)) { @@ -166,101 +179,146 @@ static void pr_slots(Any slots, Any name) { pl(" }"); } -static void pr_struct(Any x) { - Any name = cadr(x); - Any published = caddr(x); - Any super = cadddr(x); - Any slots = cddddr(x); - pr_package(name); - pr("public class "); pp(name); - if(!null(super)) {pr(" extends "); pp(super);} - pl(" { // struct"); - pr_slots(slots, name); - pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); - int i = 0; - for(Any y = slots; !null(y); y = cdr(y)) { - Any slot = car(y); - pr(" new com.sun.star.lib.uno.typeinfo.MemberTypeInfo(\""); pp(car(slot)); pr("\", "); pi(i++); pr(", 4)"); pl(null(y) ? "" : ","); +static void pr_TypeInfo(char *kind, Any name, int n, int type, Any rest) { + pr(" new com.sun.star.lib.uno.typeinfo."); pr(kind); pr("TypeInfo(\""); + pp(name); pr("\", "); pi(n); pr(", "); pi(type); pr(")"); + pl(null(rest) ? "" : ","); +} + +static void pr_extends(Any x) { + if(!null(x)) { + pr(" extends "); + use_XInterface = 1; + pp(x); + use_XInterface = 0; } - pl(" };"); - pl("}"); } -static void pr_exception(Any x) { +static void pr_struct(Any x) { Any name = cadr(x); Any published = caddr(x); - Any super = cadddr(x); - Any body = cddddr(x); - pr_package(name); - pr("public class "); pp(name); - int e = equal(name, Exception) && equal(module, uno_star_sun_com); - int r = equal(name, RuntimeException) && equal(module, uno_star_sun_com); - if(e) - pr(" extends java.lang.Exception"); - else if(r) - pr(" extends java.lang.RuntimeException"); - else if(!null(super)) { - pr(" extends "); pp(super); - } - pl(" { // exception"); - if(e || r) { - pl(" public java.lang.Object Context;"); - pr(" public "); pp(name); pl("() {"); - pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message) {"); - pl(" super(Message);"); - pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message, java.lang.Object Context) {"); - pl(" super(Message);"); - pl(" this.Context = Context;"); - 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)"); - pl(" };"); - } else { - for(Any y = body; !null(y); y = cdr(y)) { - Any slot = car(y); - pr(" "); pp(cadr(slot)); pr(" "); pp(car(slot)); pl(";"); - } - pr(" public "); pp(name); pl("() {"); - pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message) {"); - pl(" super(Message);"); - pl(" }"); - pr(" public "); pp(name); pl("(java.lang.String Message, java.lang.Object Object) {"); - pl(" super(Message, Object);"); - pl(" }"); + if(!null(published)) { + Any super = cadddr(x); + Any slots = cddddr(x); + pr_package(name); + pr("public class "); pp(name); + pr_extends(super); + pl(" { // struct"); + pr_slots(slots, name); pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); int i = 0; - for(Any y = body; !null(y); y = cdr(y)) { + for(Any y = slots; !null(y); y = cdr(y)) { Any slot = car(y); - pr(" new com.sun.star.lib.uno.typeinfo.MemberTypeInfo(\""); pp(cadr(slot)); pr("\", "); pi(i++); pr(", 128)"); pl(null(y) ? "" : ","); + pr_TypeInfo("Member", car(slot), i++, 0, cdr(y)); } pl(" };"); + pl("}"); + } +} + +static void pr_exception(Any x) { + Any name = cadr(x); + Any published = caddr(x); + if(!null(published)) { + Any super = cadddr(x); + Any body = cddddr(x); + pr_package(name); + pr("public class "); pp(name); + int e = equal(name, Exception) && equal(module, uno_star_sun_com); + int r = equal(name, RuntimeException) && equal(module, uno_star_sun_com); + if(e) + pr(" extends java.lang.Exception"); + else if(r) + pr(" extends java.lang.RuntimeException"); + else pr_extends(super); + pl(" { // exception"); + if(e || r) { + pl(" public java.lang.Object Context;"); + pr(" public "); pp(name); pl("() {"); + pl(" }"); + pr(" public "); pp(name); pl("(java.lang.String Message) {"); + pl(" super(Message);"); + pl(" }"); + pr(" public "); pp(name); pl("(java.lang.String Message, java.lang.Object Context) {"); + pl(" super(Message);"); + pl(" this.Context = Context;"); + pl(" }"); + pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); + pr_TypeInfo("Member", Context, 0, 128, NIL); + pl(" };"); + } else { + for(Any y = body; !null(y); y = cdr(y)) { + Any slot = car(y); + pr(" public "); pp(cadr(slot)); pr(" "); pp(car(slot)); pl(";"); + } + pr(" public "); pp(name); pl("() {"); + pr_default_slots(body); + pl(" }"); + pr(" public "); pp(name); pl("(java.lang.String Message) {"); + pl(" super(Message);"); + // TODO init default slots + pl(" }"); + pr(" public "); pp(name); pr("(java.lang.String Message, java.lang.Object Object"); /* TODO additional slot args see CannotConvertException */ pl(") {"); + pl(" super(Message, Object);"); + pl(" }"); + if(!null(body)) { + pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); + int i = 0; + for(Any y = body; !null(y); y = cdr(y)) { + Any slot = car(y); + pr_TypeInfo("Method", cadr(slot), i++, 0, cdr(y)); + } + pl(" };"); + } + } + pl("}"); } - pl("}"); } static void pr_definterface(Any x) { Any name = cadr(x); Any published = caddr(x); - Any super = cadddr(x); - Any body = cddddr(x); - pr_package(name); - pr("public interface "); pp(name); - if(!null(super)) {pr(" extends "); pp(super);} - pl(" {"); - if(!(equal(name, XInterface) && equal(module, uno_star_sun_com))) { - pp_list(body, ""); - pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); - int i = 0; + if(!null(published)) { + Any super = cadddr(x); + Any body = cddddr(x); + pr_package(name); + pr("public interface "); pp(name); + pr_extends(super); + use_XInterface = 1; + int i = null(super) ? 0 : 1; for(Any y = body; !null(y); y = cdr(y)) { Any slot = car(y); - pr(" new com.sun.star.lib.uno.typeinfo.MemberTypeInfo(\""); pp(cadr(slot)); pr("\", "); pi(i++); pr(", 0)"); pl(null(y) ? "" : ","); + if(INTERFACE == kind(car(slot))) { + pr(0 < i++ ? ", " : " extends "); + pp(cadr(slot)); + } } - pl(" };"); + if(null(super) && i <= 0 + && !(equal(name, XInterface) && equal(module, uno_star_sun_com))) + pr(" extends com.sun.star.uno.XInterface"); + use_XInterface = 0; + pl(" {"); + if(!(equal(name, XInterface) && equal(module, uno_star_sun_com))) { + pp_list(body, ""); + int info = 0; + for(Any y = body; !null(y); y = cdr(y)) { + Any slot = car(y); + if(INTERFACE != kind(car(slot))) + info = 1; + } + if(info) { + pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); + int i = 0; + for(Any y = body; !null(y); y = cdr(y)) { + Any slot = car(y); + if(INTERFACE != kind(car(slot))) + pr_TypeInfo("Method", cadr(slot), i++, 0, cdr(y)); // INTERFACE 128 + } + pl(" };"); + } + } + pl("}"); } - pl("}"); } static void pr_method(Any x) { @@ -319,11 +377,13 @@ static void pr_singleton(Any x) { static void pr_constants(Any x) { Any name = cadr(x); Any published = caddr(x); - Any body = cdddr(x); - pr_package(name); - pr("public interface "); pp(name); pl(" { // constants"); - pp_list(body, ""); - pl("}"); + if(!null(published)) { + Any body = cdddr(x); + pr_package(name); + pr("public interface "); pp(name); pl(" { // constants"); + pp_list(body, ""); + pl("}"); + } } static void pr_const(Any x) { @@ -360,40 +420,48 @@ static Any reverse(Any x, Any a) { } static void pr_relative(Any x) { - if(null(cddr(x))) { // local typedef - Any y = find(cons(cadr(x), module), typedefs, equal); - if(!null(y)) { - pp(cdr(y)); + if(!use_XInterface && equal(com_sun_star_uno_XInterface, cdr(x))) + pr("java.lang.Object"); + else { + if(null(cddr(x))) { // local typedef + Any y = find(cons(cadr(x), module), typedefs, equal); + if(!null(y)) { + pp(cdr(y)); + return; + } + } + Any y = find(reverse(cdr(x), NIL), typedefs, equal); + if(!null(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(null(cddr(x))) // x => (relative ServiceContext) p => (iop corba star sun com) + pp_list(reverse(cons(cadr(x), p), NIL), "."); + else + pp_list(cdr(x), "."); } - Any y = find(reverse(cdr(x), NIL), typedefs, equal); - if(!null(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(null(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) { - Any y = find(reverse(cdr(x), NIL), typedefs, equal); - if(!null(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; + if(!use_XInterface && equal(com_sun_star_uno_XInterface, cdr(x))) + pr("java.lang.Object"); + else { + Any y = find(reverse(cdr(x), NIL), typedefs, equal); + if(!null(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; + } + pp_list(cdr(x), "."); } - pp_list(cdr(x), "."); } static void pr_property(Any x) { @@ -408,13 +476,22 @@ static void pr_property(Any x) { static void pr_deftemplate(Any x) { Any name = cadr(x); Any published = caddr(x); - Any args = cadddr(x); - Any slots = cddddr(x); - pr_package(name); - pr("public class "); pp(name); - pr("<"); pp_list(args, ", "); pr(">"); pl(" { // deftemplate"); - pr_slots(slots, name); - pl("}"); + if(!null(published)) { + Any args = cadddr(x); + Any slots = cddddr(x); + pr_package(name); + pr("public class "); pp(name); + pr("<"); pp_list(args, ", "); pr(">"); pl(" { // deftemplate"); + pr_slots(slots, name); + pl(" public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = {"); + int i = 0; + for(Any y = slots; !null(y); y = cdr(y)) { + Any slot = car(y); + pr_TypeInfo("Member", car(slot), i++, 0, cdr(y)); + } + pl(" };"); + pl("}"); + } } static void pr_primitive(Any x) { @@ -471,7 +548,7 @@ static void pp(Any x) { case ABSOLUTE: pr_absolute(x); break; case CONSTANTS: pr_constants(x); break; case CONST: pr_const(x); break; - case SEQUENCE: pp(cadr(x)); pr("[]"); break; + case SEQUENCE: pp(cadr(x)); pr(seq0 ? "[0]" : "[]"); break; //case ATTRIBUTE: pr_attribute(x); break; case EXCEPTION: pr_exception(x); break; //case SERVICE: pr_service(x); break; @@ -527,6 +604,8 @@ int main() { 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); return 0;