static gboolean java_prolog(TreeState *state) { int len, i; state->priv = calloc(1, sizeof(struct java_priv_data)); if (!state->priv) return FALSE; TYPEDEFS(state) = 0; TYPEDEFS(state) = g_hash_table_new(g_str_hash, g_str_equal); if (!TYPEDEFS(state)) { /* XXX report error */ free(state->priv); return FALSE; } KEYWORDS(state) = 0; KEYWORDS(state) = g_hash_table_new(g_str_hash, g_str_equal); if (!KEYWORDS(state)) { g_hash_table_destroy(TYPEDEFS(state)); free(state->priv); return FALSE; } len = sizeof(javaKeywords)/sizeof(*javaKeywords); for (i = 0; i < len; i++) { g_hash_table_insert(KEYWORDS(state), javaKeywords[i], javaKeywords[i]); } return TRUE; }
static gboolean java_epilog(TreeState *state) { /* points to other elements of the tree, so just destroy the table */ g_hash_table_destroy(TYPEDEFS(state)); g_hash_table_destroy(KEYWORDS(state)); free(state->priv); state->priv = NULL; return TRUE; }
static gboolean type_declaration(TreeState *state) { /* * Unlike C, Java has no type declaration directive. * Instead, we record the mapping, and look up the actual type * when needed. */ IDL_tree type = IDL_TYPE_DCL(state->tree).type_spec; IDL_tree dcls = IDL_TYPE_DCL(state->tree).dcls; /* XXX: check for illegal types */ g_hash_table_insert(TYPEDEFS(state), IDL_IDENT(IDL_LIST(dcls).data).str, type); return TRUE; }
static gboolean xpcom_to_java_type(TreeState *state, IDL_tree type) { IDL_tree real_type; IDL_tree up; if (!type) { fputs("Object", state->file); return TRUE; } /* Could be a typedef; try to map it to the real type */ real_type = find_underlying_type(type); type = real_type ? real_type : type; switch(IDL_NODE_TYPE(type)) { case IDLN_TYPE_INTEGER: { switch(IDL_TYPE_INTEGER(type).f_type) { case IDL_INTEGER_TYPE_SHORT: fputs("short", state->file); break; case IDL_INTEGER_TYPE_LONG: fputs("int", state->file); break; case IDL_INTEGER_TYPE_LONGLONG: fputs("long", state->file); break; default: g_error(" Unknown integer type: %d\n", IDL_TYPE_INTEGER(type).f_type); return FALSE; } break; } case IDLN_TYPE_CHAR: case IDLN_TYPE_WIDE_CHAR: fputs("char", state->file); break; case IDLN_TYPE_WIDE_STRING: case IDLN_TYPE_STRING: fputs("String", state->file); break; case IDLN_TYPE_BOOLEAN: fputs("boolean", state->file); break; case IDLN_TYPE_OCTET: fputs("byte", state->file); break; case IDLN_TYPE_FLOAT: switch(IDL_TYPE_FLOAT(type).f_type) { case IDL_FLOAT_TYPE_FLOAT: fputs("float", state->file); break; case IDL_FLOAT_TYPE_DOUBLE: fputs("double", state->file); break; default: g_error(" Unknown floating point typ: %d\n", IDL_NODE_TYPE(type)); break; } break; case IDLN_IDENT: if (!(up = IDL_NODE_UP(type))) { IDL_tree_error(state->tree, "ERROR: orphan ident %s in param list\n", IDL_IDENT(state->tree).str); return FALSE; } switch (IDL_NODE_TYPE(up)) { case IDLN_FORWARD_DCL: case IDLN_INTERFACE: { char *className; const char *iid_is; handle_iid_is: /* might get here via the goto, so re-check type */ if (IDL_NODE_TYPE(up) == IDLN_INTERFACE) className = IDL_IDENT(IDL_INTERFACE(up).ident).str; else if (IDL_NODE_TYPE(up) == IDLN_FORWARD_DCL) className = IDL_IDENT(IDL_FORWARD_DCL(up).ident).str; else className = IDL_IDENT(IDL_NATIVE(up).ident).str; iid_is = NULL; if (IDL_NODE_TYPE(state->tree) == IDLN_PARAM_DCL) { iid_is = IDL_tree_property_get(IDL_PARAM_DCL(state->tree).simple_declarator, "iid_is"); } if (iid_is) { fputs("nsISupports", state->file); } else { /* XXX How do we want to handle this? If it's an IDLN_INTERFACE, * then we can just output the name of the class, since the IDL * files exist for those classes. However, if it's an * IDLN_FORWARD_DCL, some of those interfaces are not defined in * IDL files, so we get an error when trying to compile the java * files. So, for now, we just output them as the base iface * (nsISupports). */ if (IDL_NODE_TYPE(up) == IDLN_FORWARD_DCL) fputs("nsISupports", state->file); else fprintf(state->file, "%s", className); } break; } case IDLN_NATIVE: { char *ident; /* jband - adding goto for iid_is when type is native */ if (IDL_NODE_TYPE(state->tree) == IDLN_PARAM_DCL && IDL_tree_property_get(IDL_PARAM_DCL(state->tree).simple_declarator, "iid_is")) { goto handle_iid_is; /* fputs("nsISupports", state->file); */ break; } ident = IDL_IDENT(type).str; if (IDL_tree_property_get(type, "nsid")) { fputs("String", state->file); } else if (IDL_tree_property_get(type, "domstring")) { fputs("String", state->file); } else if (IDL_tree_property_get(type, "astring")) { fputs("String", state->file); } else if (IDL_tree_property_get(type, "utf8string")) { fputs("String", state->file); } else if (IDL_tree_property_get(type, "cstring")) { fputs("String", state->file); } else { const char* user_type = IDL_NATIVE(IDL_NODE_UP(type)).user_type; IDL_tree real_type = g_hash_table_lookup(TYPEDEFS(state), user_type); if (real_type) { return xpcom_to_java_type(state, real_type); } else { if (strcmp(user_type, "PRInt8") == 0 || strcmp(user_type, "PRUint8") == 0) { fputs("byte", state->file); } else if (strcmp(user_type, "PRInt16") == 0 || strcmp(user_type, "PRUint16") == 0) { fputs("short", state->file); } else if (strcmp(user_type, "PRInt32") == 0 || strcmp(user_type, "PRUint32") == 0 || strcmp(user_type, "int") == 0) { fputs("int", state->file); } else if (strcmp(user_type, "PRInt64") == 0 || strcmp(user_type, "PRUint64") == 0) { fputs("long", state->file); } else if (strcmp(user_type, "PRBool") == 0) { fputs("boolean", state->file); } else if (strncmp(user_type, "char", 4) == 0 || strncmp(user_type, "const char", 10) == 0 || strncmp(user_type, "unsigned char", 13) == 0) { if (IDL_tree_property_get(type, "ptr")) { fputs("byte[]", state->file); } else { fputs("char", state->file); } } else if (strcmp(user_type, "nsIID") == 0) { fputs("String", state->file); } else if (strcmp(user_type, "nsString") == 0 || strcmp(user_type, "nsAString") == 0 || strcmp(user_type, "nsACString") == 0) { fputs("String", state->file); } else { fputs("int", state->file); } } } break; } default: if (IDL_NODE_TYPE(IDL_NODE_UP(up)) == IDLN_TYPE_DCL) { /* restart with the underlying type */ IDL_tree new_type; new_type = IDL_TYPE_DCL(IDL_NODE_UP(up)).type_spec; if (new_type) { gboolean rc = xpcom_to_java_type(state, new_type); return rc; } else { /* do what we would do in recursion if !type */ fputs("Object", state->file); return TRUE; } } IDL_tree_error(state->tree, "can't handle %s ident in param list\n", "that type of" ); return FALSE; } break; default: IDL_tree_error(state->tree, "can't handle %s in param list\n", #ifdef DEBUG_shaver /* XXX is this safe to use on Win now? */ IDL_NODE_TYPE_NAME(IDL_NODE_UP(type)) #else "that type" #endif ); return FALSE; } return TRUE; }
static gboolean xpcom_to_java_type (TreeState *state) { IDL_tree real_type; if (!state->tree) { fputs("Object", FILENAME(state)); return TRUE; } /* Could be a typedef; try to map it to the real type */ real_type = find_underlying_type(state->tree); state->tree = real_type ? real_type : state->tree; switch(IDL_NODE_TYPE(state->tree)) { case IDLN_TYPE_INTEGER: { switch(IDL_TYPE_INTEGER(state->tree).f_type) { case IDL_INTEGER_TYPE_SHORT: fputs("short", FILENAME(state)); break; case IDL_INTEGER_TYPE_LONG: fputs("int", FILENAME(state)); break; case IDL_INTEGER_TYPE_LONGLONG: fputs("long", FILENAME(state)); break; default: g_error(" Unknown integer type: %d\n", IDL_TYPE_INTEGER(state->tree).f_type); return FALSE; } break; } case IDLN_TYPE_CHAR: case IDLN_TYPE_WIDE_CHAR: fputs("char", FILENAME(state)); break; case IDLN_TYPE_WIDE_STRING: case IDLN_TYPE_STRING: fputs("String", FILENAME(state)); break; case IDLN_TYPE_BOOLEAN: fputs("boolean", FILENAME(state)); break; case IDLN_TYPE_OCTET: fputs("byte", FILENAME(state)); break; case IDLN_TYPE_FLOAT: switch(IDL_TYPE_FLOAT(state->tree).f_type) { case IDL_FLOAT_TYPE_FLOAT: fputs("float", FILENAME(state)); break; case IDL_FLOAT_TYPE_DOUBLE: fputs("double", FILENAME(state)); break; default: g_error(" Unknown floating point typ: %d\n", IDL_NODE_TYPE(state->tree)); break; } break; case IDLN_IDENT: if (IDL_NODE_UP(state->tree) && IDL_NODE_TYPE(IDL_NODE_UP(state->tree)) == IDLN_NATIVE) { const char *user_type = IDL_NATIVE(IDL_NODE_UP(state->tree)).user_type; const char *ident_str = IDL_IDENT(IDL_NATIVE(IDL_NODE_UP(state->tree)).ident).str; if (strcmp(user_type, "void") == 0) { /*it should not happend for scriptable methods*/ fputs("Object", FILENAME(state)); } /* XXX: s.b test for "id" attribute */ /* XXX: special class for nsIDs */ else if (strcmp(user_type, "nsID") == 0) { fputs("ID", FILENAME(state)); } else if (strcmp(user_type, "nsIID") == 0) { fputs("IID", FILENAME(state)); } else if (strcmp(user_type, "nsCID") == 0) { fputs("CID", FILENAME(state)); } else { /* XXX: special class for opaque types */ /*it should not happend for scriptable methods*/ fputs("OpaqueValue", FILENAME(state)); } } else { const char *ident_str = IDL_IDENT(state->tree).str; /* XXX: big kludge; s.b. way to match to typedefs */ if (strcmp(ident_str, "PRInt8") == 0 || strcmp(ident_str, "PRUint8") == 0) { fputs("byte", FILENAME(state)); } else if (strcmp(ident_str, "PRInt16") == 0 || strcmp(ident_str, "PRUint16") == 0) { fputs("short", FILENAME(state)); } else if (strcmp(ident_str, "PRInt32") == 0 || strcmp(ident_str, "PRUint32") == 0) { fputs("int", FILENAME(state)); } else if (strcmp(ident_str, "PRInt64") == 0 || strcmp(ident_str, "PRUint64") == 0) { fputs("long", FILENAME(state)); } else if (strcmp(ident_str, "PRBool") == 0) { fputs("boolean", FILENAME(state)); } else if (strcmp(ident_str, "nsrefcnt") == 0) { fputs("int", FILENAME(state)); } else { IDL_tree real_type = g_hash_table_lookup(TYPEDEFS(state), ident_str); if (real_type) { IDL_tree orig_tree = state->tree; state->tree = real_type; xpcom_to_java_type(state); state->tree = orig_tree; } else { fputs(subscriptIdentifier(state, (char*) ident_str), FILENAME(state)); } } } break; case IDLN_TYPE_ENUM: case IDLN_TYPE_OBJECT: default: g_error(" Unknown type: %d\n", IDL_TYPE_FLOAT(state->tree).f_type); break; } return TRUE; }