summaryrefslogtreecommitdiff
path: root/src/interface-map.c
blob: 2ac1871f1bead77127987daf7a00dc98a381cecd (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* interface mapping
 *
 * This file is part of nsgenbind.
 * Published under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 */

#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "options.h"
#include "utils.h"
#include "nsgenbind-ast.h"
#include "webidl-ast.h"
#include "interface-map.h"

/** count the number of nodes of a given type on an interface */
static int
enumerate_interface_type(struct webidl_node *interface_node,
                         enum webidl_node_type node_type)
{
        int count = 0;
        struct webidl_node *members_node;

        members_node = webidl_node_find_type(
                webidl_node_getnode(interface_node),
                NULL,
                WEBIDL_NODE_TYPE_LIST);
        while (members_node != NULL) {
                count += webidl_node_enumerate_type(
                        webidl_node_getnode(members_node),
                        node_type);

                members_node = webidl_node_find_type(
                        webidl_node_getnode(interface_node),
                        members_node,
                        WEBIDL_NODE_TYPE_LIST);
        }

        return count;
}

/* find index of inherited node if it is one of those listed in the
 * binding also maintain refcounts
 */
static void
compute_inherit_refcount(struct interface_map_entry *entries, int entryc)
{
        int idx;
        int inf;

        for (idx = 0; idx < entryc; idx++ ) {
                entries[idx].inherit_idx = -1;
                for (inf = 0; inf < entryc; inf++ ) {
                        /* cannot inherit from self and name must match */
                        if ((inf != idx) &&
                            (entries[idx].inherit_name != NULL ) &&
                            (strcmp(entries[idx].inherit_name,
                                    entries[inf].name) == 0)) {
                                entries[idx].inherit_idx = inf;
                                entries[inf].refcount++;
                                break;
                        }
                }
        }
}

/** Topoligical sort based on the refcount
 *
 * do not need to consider loops as constructed graph is a acyclic
 *
 * alloc a second copy of the map
 * repeat until all entries copied:
 *   walk source mapping until first entry with zero refcount
 *   put the entry  at the end of the output map
 *   reduce refcount on inherit index if !=-1
 *   remove entry from source map
 */
static struct interface_map_entry *
interface_topoligical_sort(struct interface_map_entry *srcinf, int infc)
{
        struct interface_map_entry *dstinf;
        int idx;
        int inf;

        dstinf = calloc(infc, sizeof(struct interface_map_entry));
        if (dstinf == NULL) {
                return NULL;
        }

        for (idx = infc - 1; idx >= 0; idx--) {
                /* walk source map until first valid entry with zero refcount */
                inf = 0;
                while ((inf < infc) &&
                       ((srcinf[inf].name == NULL) ||
                        (srcinf[inf].refcount > 0))) {
                        inf++;
                }
                if (inf == infc) {
                        free(dstinf);
                        return NULL;
                }

                /* copy entry to the end of the output map */
                dstinf[idx].name = srcinf[inf].name;
                dstinf[idx].node = srcinf[inf].node;
                dstinf[idx].inherit_name = srcinf[inf].inherit_name;
                dstinf[idx].operationc = srcinf[inf].operationc;
                dstinf[idx].operationv = srcinf[inf].operationv;
                dstinf[idx].attributec = srcinf[inf].attributec;
                dstinf[idx].attributev = srcinf[inf].attributev;
                dstinf[idx].constantc = srcinf[inf].constantc;
                dstinf[idx].constantv = srcinf[inf].constantv;
                dstinf[idx].class = srcinf[inf].class;

                /* reduce refcount on inherit index if !=-1 */
                if (srcinf[inf].inherit_idx != -1) {
                        srcinf[srcinf[inf].inherit_idx].refcount--;
                }

                /* remove entry from source map */
                srcinf[inf].name = NULL;
        }

        return dstinf;
}

static int
operation_map_new(struct webidl_node *interface,
                  struct genbind_node *class,
                  int *operationc_out,
                  struct interface_map_operation_entry **operationv_out)
{
        struct webidl_node *list_node;
        struct webidl_node *op_node; /* attribute node */
        struct interface_map_operation_entry *cure; /* current entry */
        struct interface_map_operation_entry *operationv;
        int operationc;

        /* enumerate operationss */
        operationc = enumerate_interface_type(interface,
                                              WEBIDL_NODE_TYPE_OPERATION);
        *operationc_out = operationc;

        if (operationc < 1) {
                *operationv_out = NULL; /* no operations so empty map */
                return 0;
        }

        operationv = calloc(operationc,
                            sizeof(struct interface_map_operation_entry));
        if (operationv == NULL) {
                return -1;
        };
        cure = operationv;

        /* iterate each list node within the interface */
        list_node = webidl_node_find_type(
                webidl_node_getnode(interface),
                NULL,
                WEBIDL_NODE_TYPE_LIST);

        while (list_node != NULL) {
                /* iterate through operations on list */
                op_node = webidl_node_find_type(
                        webidl_node_getnode(list_node),
                        NULL,
                        WEBIDL_NODE_TYPE_OPERATION);

                while (op_node != NULL) {
                        cure->node = op_node;

                        cure->name = webidl_node_gettext(
                                webidl_node_find_type(
                                        webidl_node_getnode(op_node),
                                        NULL,
                                        WEBIDL_NODE_TYPE_IDENT));

                        cure->method = genbind_node_find_method_ident(
                                               class,
                                               NULL,
                                               GENBIND_METHOD_TYPE_METHOD,
                                               cure->name);

                        cure++;

                        /* move to next operation */
                        op_node = webidl_node_find_type(
                                webidl_node_getnode(list_node),
                                op_node,
                                WEBIDL_NODE_TYPE_OPERATION);
                }

                list_node = webidl_node_find_type(
                        webidl_node_getnode(interface),
                        list_node,
                        WEBIDL_NODE_TYPE_LIST);
        }

        *operationv_out = operationv; /* resulting operations map */

        return 0;
}

static int
attribute_map_new(struct webidl_node *interface,
                  struct genbind_node *class,
                  int *attributec_out,
                  struct interface_map_attribute_entry **attributev_out)
{
        struct webidl_node *list_node;
        struct webidl_node *at_node; /* attribute node */
        struct interface_map_attribute_entry *cure; /* current entry */
        struct interface_map_attribute_entry *attributev;
        int attributec;

        /* enumerate attributes */
        attributec = enumerate_interface_type(interface,
                                              WEBIDL_NODE_TYPE_ATTRIBUTE);
        *attributec_out = attributec;

        if (attributec < 1) {
                *attributev_out = NULL; /* no attributes so empty map */
                return 0;
        }

        attributev = calloc(attributec,
                            sizeof(struct interface_map_attribute_entry));
        if (attributev == NULL) {
                return -1;
        };
        cure = attributev;

        /* iterate each list node within the interface */
        list_node = webidl_node_find_type(
                webidl_node_getnode(interface),
                NULL,
                WEBIDL_NODE_TYPE_LIST);

        while (list_node != NULL) {
                /* iterate through attributes on list */
                at_node = webidl_node_find_type(
                        webidl_node_getnode(list_node),
                        NULL,
                        WEBIDL_NODE_TYPE_ATTRIBUTE);

                while (at_node != NULL) {
                        enum webidl_type_modifier *modifier;

                        cure->node = at_node;

                        cure->name = webidl_node_gettext(
                                webidl_node_find_type(
                                        webidl_node_getnode(at_node),
                                        NULL,
                                        WEBIDL_NODE_TYPE_IDENT));

                        cure->getter = genbind_node_find_method_ident(
                                               class,
                                               NULL,
                                               GENBIND_METHOD_TYPE_GETTER,
                                               cure->name);

                        /* check fo readonly attributes */
                        modifier = (enum webidl_type_modifier *)webidl_node_getint(
                                webidl_node_find_type(
                                        webidl_node_getnode(at_node),
                                        NULL,
                                        WEBIDL_NODE_TYPE_MODIFIER));
                        if ((modifier != NULL) &&
                            (*modifier == WEBIDL_TYPE_MODIFIER_READONLY)) {
                                cure->modifier = WEBIDL_TYPE_MODIFIER_READONLY;
                        } else {
                                cure->modifier = WEBIDL_TYPE_MODIFIER_NONE;
                                cure->setter = genbind_node_find_method_ident(
                                                     class,
                                                     NULL,
                                                     GENBIND_METHOD_TYPE_SETTER,
                                                     cure->name);
                        }

                        cure++;

                        /* move to next attribute */
                        at_node = webidl_node_find_type(
                                webidl_node_getnode(list_node),
                                at_node,
                                WEBIDL_NODE_TYPE_ATTRIBUTE);
                }

                list_node = webidl_node_find_type(
                        webidl_node_getnode(interface),
                        list_node,
                        WEBIDL_NODE_TYPE_LIST);
        }

        *attributev_out = attributev; /* resulting attributes map */

        return 0;
}

static int
constant_map_new(struct webidl_node *interface,
                 int *constantc_out,
                 struct interface_map_constant_entry **constantv_out)
{
        struct webidl_node *list_node;
        struct webidl_node *constant_node; /* constant node */
        struct interface_map_constant_entry *cure; /* current entry */
        struct interface_map_constant_entry *constantv;
        int constantc;

        /* enumerate constants */
        constantc = enumerate_interface_type(interface,
                                              WEBIDL_NODE_TYPE_CONST);

        if (constantc < 1) {
                *constantc_out = 0;
                *constantv_out = NULL; /* no constants so empty map */
                return 0;
        }

        *constantc_out = constantc;

        constantv = calloc(constantc,
                           sizeof(struct interface_map_constant_entry));
        if (constantv == NULL) {
                return -1;
        };
        cure = constantv;

        /* iterate each list node within the interface */
        list_node = webidl_node_find_type(
                webidl_node_getnode(interface),
                NULL,
                WEBIDL_NODE_TYPE_LIST);

        while (list_node != NULL) {
                /* iterate through constants on list */
                constant_node = webidl_node_find_type(
                        webidl_node_getnode(list_node),
                        NULL,
                        WEBIDL_NODE_TYPE_CONST);

                while (constant_node != NULL) {
                        cure->node = constant_node;

                        cure->name = webidl_node_gettext(
                                webidl_node_find_type(
                                        webidl_node_getnode(constant_node),
                                        NULL,
                                        WEBIDL_NODE_TYPE_IDENT));

                        cure++;

                        /* move to next constant */
                        constant_node = webidl_node_find_type(
                                webidl_node_getnode(list_node),
                                constant_node,
                                WEBIDL_NODE_TYPE_CONST);
                }

                list_node = webidl_node_find_type(
                        webidl_node_getnode(interface),
                        list_node,
                        WEBIDL_NODE_TYPE_LIST);
        }

        *constantv_out = constantv; /* resulting constants map */

        return 0;
}

int interface_map_new(struct genbind_node *genbind,
                        struct webidl_node *webidl,
                        struct interface_map **index_out)
{
        int interfacec;
        struct interface_map_entry *entries;
        struct interface_map_entry *sorted_entries;
        struct interface_map_entry *ecur;
        struct webidl_node *node;
        struct interface_map *index;

        interfacec = webidl_node_enumerate_type(webidl,
                                                WEBIDL_NODE_TYPE_INTERFACE);

        if (options->verbose) {
                printf("Indexing %d interfaces\n", interfacec);
        }

        entries = calloc(interfacec, sizeof(struct interface_map_entry));
        if (entries == NULL) {
                return -1;
        }

        /* for each interface populate an entry in the index */
        ecur = entries;
        node = webidl_node_find_type(webidl, NULL, WEBIDL_NODE_TYPE_INTERFACE);
        while (node != NULL) {

                /* fill index entry */
                ecur->node = node;

                /* name of interface */
                ecur->name = webidl_node_gettext(
                    webidl_node_find_type(
                        webidl_node_getnode(node),
                        NULL,
                        WEBIDL_NODE_TYPE_IDENT));

                /* name of the inherited interface (if any) */
                ecur->inherit_name = webidl_node_gettext(
                    webidl_node_find_type(
                        webidl_node_getnode(node),
                        NULL,
                        WEBIDL_NODE_TYPE_INTERFACE_INHERITANCE));

                /* matching class from binding  */
                ecur->class = genbind_node_find_type_ident(genbind,
                                     NULL, GENBIND_NODE_TYPE_CLASS, ecur->name);

                /* enumerate and map the interface operations */
                operation_map_new(node,
                                  ecur->class,
                                  &ecur->operationc,
                                  &ecur->operationv);

                /* enumerate and map the interface attributes */
                attribute_map_new(node,
                                  ecur->class,
                                  &ecur->attributec,
                                  &ecur->attributev);

                /* enumerate and map the interface constants */
                constant_map_new(node,
                                 &ecur->constantc,
                                 &ecur->constantv);

                /* move to next interface */
                node = webidl_node_find_type(webidl, node,
                                             WEBIDL_NODE_TYPE_INTERFACE);
                ecur++;
        }

        /* compute inheritance and refcounts on map */
        compute_inherit_refcount(entries, interfacec);

        /* sort interfaces to ensure correct ordering */
        sorted_entries = interface_topoligical_sort(entries, interfacec);
        free(entries);
        if (sorted_entries == NULL) {
                return -1;
        }

        /* compute inheritance and refcounts on sorted map */
        compute_inherit_refcount(sorted_entries, interfacec);

        index = malloc(sizeof(struct interface_map));
        index->entryc = interfacec;
        index->entries = sorted_entries;

        *index_out = index;

        return 0;
}

int interface_map_dump(struct interface_map *index)
{
        FILE *dumpf;
        int eidx;
        struct interface_map_entry *ecur;

        /* only dump AST to file if required */
        if (!options->debug) {
                return 0;
        }

        dumpf = genb_fopen("interface-map", "w");
        if (dumpf == NULL) {
                return 2;
        }

        ecur = index->entries;
        for (eidx = 0; eidx < index->entryc; eidx++) {
                fprintf(dumpf, "%d %s\n", eidx, ecur->name);
                if (ecur->inherit_name != NULL) {
                        fprintf(dumpf, "        inherit:%s\n", ecur->inherit_name);
                }
                if (ecur->class != NULL) {
                        fprintf(dumpf, "        class:%p\n", ecur->class);
                }
                if (ecur->operationc > 0) {
                        int opc = ecur->operationc;
                        struct interface_map_operation_entry *ope;

                        fprintf(dumpf, "        %d operations\n",
                                ecur->operationc);

                        ope = ecur->operationv;
                        while (ope != NULL) {
                                fprintf(dumpf, "                %s %p\n",
                                        ope->name, ope->method);
                                ope++;
                                opc--;
                                if (opc == 0) {
                                        break;
                                }
                        }

                }
                if (ecur->attributec > 0) {
                        int attrc = ecur->attributec;
                        struct interface_map_attribute_entry *attre;

                        fprintf(dumpf, "        %d attributes\n", attrc);

                        attre = ecur->attributev;
                        while (attre != NULL) {
                                fprintf(dumpf, "                %s %p",
                                        attre->name,
                                        attre->getter);
                                if (attre->modifier == WEBIDL_TYPE_MODIFIER_NONE) {
                                        fprintf(dumpf, " %p\n", attre->setter);
                                } else {
                                        fprintf(dumpf, "\n");
                                }
                                attre++;
                                attrc--;
                                if (attrc == 0) {
                                        break;
                                }
                        }
                }
                if (ecur->constantc > 0) {
                        int idx;

                        fprintf(dumpf, "        %d constants\n",
                                ecur->constantc);

                        for (idx = 0; idx < ecur->constantc; idx++) {
                                struct interface_map_constant_entry *cone;
                                cone = ecur->constantv + idx;
                                fprintf(dumpf, "                %s\n",
                                        cone->name);
                        }
                }
                ecur++;
        }

        fclose(dumpf);

        return 0;
}

int interface_map_dumpdot(struct interface_map *index)
{
        FILE *dumpf;
        int eidx;
        struct interface_map_entry *ecur;

        /* only dump AST to file if required */
        if (!options->debug) {
                return 0;
        }

        dumpf = genb_fopen("interface.dot", "w");
        if (dumpf == NULL) {
                return 2;
        }

        fprintf(dumpf, "digraph interfaces {\n");

        ecur = index->entries;
        for (eidx = 0; eidx < index->entryc; eidx++) {
            if (ecur->class != NULL) {
                /* interfaces bound to a class are shown in blue */
                fprintf(dumpf, "%04d [label=\"%s\" fontcolor=\"blue\"];\n",
                        eidx, ecur->name);
            } else {
                fprintf(dumpf, "%04d [label=\"%s\"];\n", eidx, ecur->name);
            }
            ecur++;
        }

        ecur = index->entries;
        for (eidx = 0; eidx < index->entryc; eidx++) {
            if (index->entries[eidx].inherit_idx != -1) {
                fprintf(dumpf, "%04d -> %04d;\n",
                        eidx, index->entries[eidx].inherit_idx);
            }
        }

        fprintf(dumpf, "}\n");

        fclose(dumpf);

        return 0;
}

struct interface_map_entry *
interface_map_inherit_entry(struct interface_map *map,
                           struct interface_map_entry *entry)
{
        struct interface_map_entry *res = NULL;

        if ((entry != NULL) &&
            (entry->inherit_idx != -1)) {
                res = &map->entries[entry->inherit_idx];
        }
        return res;
}