summaryrefslogtreecommitdiffstats
path: root/src/libs/xpcom18a4/xpcom/typelib/xpidl/xpidl_doc.c
blob: d560e40725fd970b82521e77a3c739f3e573c3e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is 
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "xpidl.h"

/*
 * Generates documentation from javadoc-style comments in XPIDL files.
 */

static gboolean
doc_prolog(TreeState *state)
{
    fprintf(state->file, "<html>\n");
    fprintf(state->file, "<head>\n");

    fprintf(state->file,
            "<!-- this file is generated from %s.idl -->\n",
            state->basename);
    fprintf(state->file, "<title>documentation for %s.idl interfaces</title>\n",
            state->basename);
    fprintf(state->file, "</head>\n\n");
    fprintf(state->file, "<body>\n");

    return TRUE;
}

static gboolean
doc_epilog(TreeState *state)
{
    fprintf(state->file, "</body>\n");
    fprintf(state->file, "</html>\n");

    return TRUE;
}


static gboolean
doc_list(TreeState *state)
{
    IDL_tree iter;
    for (iter = state->tree; iter; iter = IDL_LIST(iter).next) {
        state->tree = IDL_LIST(iter).data;
        if (!xpidl_process_node(state))
            return FALSE;
    }
    return TRUE;
}

static gboolean
print_list(FILE *outfile, IDL_tree list)
{
    if (list == NULL)
        return TRUE;

    fprintf(outfile, "<ul>\n");
    while (list != NULL) {
        fprintf(outfile, "    <li>%s\n",
                IDL_IDENT(IDL_LIST(list).data).str);
        list = IDL_LIST(list).next;
    }
    fprintf(outfile, "</ul>\n");
    return TRUE;
}

static gboolean
doc_interface(TreeState *state)
{
    IDL_tree iface = state->tree;
    IDL_tree iter;
    IDL_tree orig;
    char *classname = IDL_IDENT(IDL_INTERFACE(iface).ident).str;
    GSList *doc_comments = IDL_IDENT(IDL_INTERFACE(iface).ident).comments;

    fprintf(state->file, "interface %s<br>\n", classname);

    /* Much more could happen at this step. */
    /*
     * If parsing doc comments, you might need to take some care with line
     * endings, as the xpidl frontend will return comments containing of /r,
     * /n, /r/n depending on the platform.  It's best to leave out platform
     * #defines and just treat them all as equivalent.
     */
    if (doc_comments != NULL) {
        fprintf(state->file, "doc comments:<br>\n");
        fprintf(state->file, "<pre>\n");
        printlist(state->file, doc_comments);
        fprintf(state->file, "</pre>\n");
        fprintf(state->file, "<br>\n");
    }
    
    /* inherits from */
    /*
     * Note that we accept multiple inheritance here (for e.g. gnome idl)
     * even though the header backend (specific to mozilla idl) rejects it.
     */
    if ((iter = IDL_INTERFACE(iface).inheritance_spec)) {
        fprintf(state->file, "%s inherits from:<br>\n", classname);
        print_list(state->file, iter);
        fprintf(state->file, "<br>\n");
    }

    /*
     * Call xpidl_process_node to recur through list of declarations in
     * interface body; another option would be to explicitly iterate through
     * the list.  xpidl_process_node currently requires twiddling the state to
     * get the right node; I'll fix that soon to just take the node.  Makes it
     * easier to follow what's going on, I think...
     */
    orig = state->tree;
    state->tree = IDL_INTERFACE(iface).body;
    if (state->tree && !xpidl_process_node(state))
        return FALSE;
    state->tree = orig;

    return TRUE;
}

/*
 * Copied from xpidl_header.c.  You'll probably want to change it; if you can
 * use it verbatim or abstract it, we could move it to xpidl_util.c and share
 * it from there.
 */
static gboolean
write_type(IDL_tree type_tree, FILE *outfile)
{
    if (!type_tree) {
        fputs("void", outfile);
        return TRUE;
    }

    switch (IDL_NODE_TYPE(type_tree)) {
      case IDLN_TYPE_INTEGER: {
        gboolean sign = IDL_TYPE_INTEGER(type_tree).f_signed;
        switch (IDL_TYPE_INTEGER(type_tree).f_type) {
          case IDL_INTEGER_TYPE_SHORT:
            fputs(sign ? "PRInt16" : "PRUint16", outfile);
            break;
          case IDL_INTEGER_TYPE_LONG:
            fputs(sign ? "PRInt32" : "PRUint32", outfile);
            break;
          case IDL_INTEGER_TYPE_LONGLONG:
            fputs(sign ? "PRInt64" : "PRUint64", outfile);
            break;
          default:
            g_error("Unknown integer type %d\n",
                    IDL_TYPE_INTEGER(type_tree).f_type);
            return FALSE;
        }
        break;
      }
      case IDLN_TYPE_CHAR:
        fputs("char", outfile);
        break;
      case IDLN_TYPE_WIDE_CHAR:
        fputs("PRUnichar", outfile); /* wchar_t? */
        break;
      case IDLN_TYPE_WIDE_STRING:
        fputs("PRUnichar *", outfile);
        break;
      case IDLN_TYPE_STRING:
        fputs("char *", outfile);
        break;
      case IDLN_TYPE_BOOLEAN:
        fputs("PRBool", outfile);
        break;
      case IDLN_TYPE_OCTET:
        fputs("PRUint8", outfile);
        break;
      case IDLN_TYPE_FLOAT:
        switch (IDL_TYPE_FLOAT(type_tree).f_type) {
          case IDL_FLOAT_TYPE_FLOAT:
            fputs("float", outfile);
            break;
          case IDL_FLOAT_TYPE_DOUBLE:
            fputs("double", outfile);
            break;
          /* XXX 'long double' just ignored, or what? */
          default:
            fprintf(outfile, "unknown_type_%d", IDL_NODE_TYPE(type_tree));
            break;
        }
        break;
      case IDLN_IDENT:
        if (UP_IS_NATIVE(type_tree)) {
            fputs(IDL_NATIVE(IDL_NODE_UP(type_tree)).user_type, outfile);
            if (IDL_tree_property_get(type_tree, "ptr")) {
                fputs(" *", outfile);
            } else if (IDL_tree_property_get(type_tree, "ref")) {
                fputs(" &", outfile);
            }
        } else {
            fputs(IDL_IDENT(type_tree).str, outfile);
        }
        if (UP_IS_AGGREGATE(type_tree))
            fputs(" *", outfile);
        break;
      default:
        fprintf(outfile, "unknown_type_%d", IDL_NODE_TYPE(type_tree));
        break;
    }
    return TRUE;
}

/* handle ATTR_DCL (attribute declaration) nodes */
static gboolean
doc_attribute_declaration(TreeState *state)
{
    IDL_tree attr = state->tree;

    if (!verify_attribute_declaration(attr))
        return FALSE;
    /*
     * Attribute idents can also take doc comments.  They're ignored here;
     * should they be?
     */

    if (IDL_ATTR_DCL(attr).f_readonly)
        fprintf(state->file, "readonly ");

    fprintf(state->file, "attribute ");

    if (!write_type(IDL_ATTR_DCL(attr).param_type_spec, state->file))
        return FALSE;

    fprintf(state->file, "\n");
    print_list(state->file, IDL_ATTR_DCL(attr).simple_declarations);
    fprintf(state->file, "<br>\n");

    return TRUE;
}

/* handle OP_DCL (method declaration) nodes */
static gboolean
doc_method_declaration(TreeState *state)
{
    /*
     * Doc comment for attributes also applies here.
     */

    /*
     * Look at 'write_method_signature' in xpidl_header.c for an example of how
     * to navigate parse trees for methods.  For here, I just print the method
     * name.
     */

    fprintf(state->file,
            "method %s<br>\n",
            IDL_IDENT(IDL_OP_DCL(state->tree).ident).str);

    return TRUE;
}

backend *
xpidl_doc_dispatch(void)
{
    static backend result;
    static nodeHandler table[IDLN_LAST];
    static gboolean initialized = FALSE;

    result.emit_prolog = doc_prolog;
    result.emit_epilog = doc_epilog;

    if (!initialized) {
        /* Initialize non-NULL elements */

        /* I just handle a few... many still to be filled in! */

        table[IDLN_LIST] = doc_list;
        table[IDLN_INTERFACE] = doc_interface;
        table[IDLN_ATTR_DCL] = doc_attribute_declaration;
        table[IDLN_OP_DCL] = doc_method_declaration;

        initialized = TRUE;
    }
  
    result.dispatch_table = table;
    return &result;
}