summaryrefslogtreecommitdiff
path: root/tools/upbc.c
blob: 5401d69e84dd00674a7deb33c6f1953eab1fde29 (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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * upbc is the upb compiler.  This is some deep code that I wish could be
 * easier to understand, but by its nature it is doing some very "meta"
 * kinds of things.
 *
 * Copyright (c) 2009 Joshua Haberman.  See LICENSE for details.
 */

#include <ctype.h>
#include <inttypes.h>
#include <stdarg.h>
#include "descriptor.h"
#include "upb_context.h"
#include "upb_msg.h"
#include "upb_text.h"
#include "upb_array.h"
#include "upb_mm.h"

/* These are in-place string transformations that do not change the length of
 * the string (and thus never need to re-allocate). */

/* Convert to C identifier: foo.bar.Baz -> foo_bar_Baz. */
static void to_cident(struct upb_string *str)
{
  for(uint32_t i = 0; i < str->byte_len; i++)
    if(str->ptr[i] == '.' || str->ptr[i] == '/')
      str->ptr[i] = '_';
}

/* Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. */
static void to_preproc(struct upb_string *str)
{
  to_cident(str);
  for(uint32_t i = 0; i < str->byte_len; i++)
    str->ptr[i] = toupper(str->ptr[i]);
}

static int my_memrchr(char *data, char c, size_t len)
{
  int off = len-1;
  while(off > 0 && data[off] != c) --off;
  return off;
}

void *strtable_to_array(struct upb_strtable *t, int *size)
{
  *size = t->t.count;
  void **array = malloc(*size * sizeof(void*));
  struct upb_symtab_entry *e;
  int i = 0;
  for(e = upb_strtable_begin(t); e && i < *size; e = upb_strtable_next(t, &e->e))
    array[i++] = e;
  assert(i == *size && e == NULL);
  return array;
}

/* The _const.h file defines the constants (enums) defined in the .proto
 * file. */
static void write_const_h(struct upb_symtab_entry *entries[], int num_entries,
                          char *outfile_name, FILE *stream)
{
  /* Header file prologue. */
  struct upb_string *include_guard_name = upb_strdupc(outfile_name);
  to_preproc(include_guard_name);
  /* A bit cheesy, but will do the job. */
  include_guard_name->ptr[include_guard_name->byte_len-1] = 'C';
  fputs("/* This file was generated by upbc (the upb compiler).  "
        "Do not edit. */\n\n", stream),
  fprintf(stream, "#ifndef " UPB_STRFMT "\n", UPB_STRARG(include_guard_name));
  fprintf(stream, "#define " UPB_STRFMT "\n\n", UPB_STRARG(include_guard_name));
  fputs("#ifdef __cplusplus\n", stream);
  fputs("extern \"C\" {\n", stream);
  fputs("#endif\n\n", stream);

  /* Enums. */
  fprintf(stream, "/* Enums. */\n\n");
  for(int i = 0; i < num_entries; i++) {  /* Foreach enum */
    if(entries[i]->type != UPB_SYM_ENUM) continue;
    struct upb_symtab_entry *entry = entries[i];
    struct upb_enumdef *e = entry->ref._enum;
    google_protobuf_EnumDescriptorProto *ed = e->descriptor;
    /* We use entry->e.key (the fully qualified name) instead of ed->name. */
    struct upb_string *enum_name = upb_strdup(&entry->e.key);
    to_cident(enum_name);

    struct upb_string *enum_val_prefix = upb_strdup(&entry->e.key);
    enum_val_prefix->byte_len = my_memrchr(enum_val_prefix->ptr,
                                           UPB_SYMBOL_SEPARATOR,
                                           enum_val_prefix->byte_len);
    enum_val_prefix->byte_len++;
    to_preproc(enum_val_prefix);

    fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
    if(ed->set_flags.has.value) {
      for(uint32_t j = 0; j < ed->value->len; j++) {  /* Foreach enum value. */
        google_protobuf_EnumValueDescriptorProto *v = ed->value->elements[j];
        struct upb_string *value_name = upb_strdup(v->name);
        to_preproc(value_name);
        /* "  GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
        fprintf(stream, "  " UPB_STRFMT UPB_STRFMT " = %" PRIu32,
                UPB_STRARG(enum_val_prefix), UPB_STRARG(value_name), v->number);
        if(j != ed->value->len-1) fputc(',', stream);
        fputc('\n', stream);
        upb_string_unref(value_name);
      }
    }
    fprintf(stream, "} " UPB_STRFMT ";\n\n", UPB_STRARG(enum_name));
    upb_string_unref(enum_name);
    upb_string_unref(enum_val_prefix);
  }

  /* Epilogue. */
  fputs("#ifdef __cplusplus\n", stream);
  fputs("}  /* extern \"C\" */\n", stream);
  fputs("#endif\n\n", stream);
  fprintf(stream, "#endif  /* " UPB_STRFMT " */\n", UPB_STRARG(include_guard_name));
  upb_string_unref(include_guard_name);
}

/* The .h file defines structs for the types defined in the .proto file.  It
 * also defines constants for the enum values.
 *
 * Assumes that d has been validated. */
static void write_h(struct upb_symtab_entry *entries[], int num_entries,
                    char *outfile_name, char *descriptor_cident, FILE *stream)
{
  /* Header file prologue. */
  struct upb_string *include_guard_name = upb_strdupc(outfile_name);
  to_preproc(include_guard_name);
  fputs("/* This file was generated by upbc (the upb compiler).  "
        "Do not edit. */\n\n", stream),
  fprintf(stream, "#ifndef " UPB_STRFMT "\n", UPB_STRARG(include_guard_name));
  fprintf(stream, "#define " UPB_STRFMT "\n\n", UPB_STRARG(include_guard_name));
  fputs("#include <upb_struct.h>\n\n", stream);
  fputs("#ifdef __cplusplus\n", stream);
  fputs("extern \"C\" {\n", stream);
  fputs("#endif\n\n", stream);

  if(descriptor_cident) {
    fputs("struct google_protobuf_FileDescriptorSet;\n", stream);
    fprintf(stream, "extern struct google_protobuf_FileDescriptorSet *%s;\n\n",
            descriptor_cident);
  }

  /* Forward declarations. */
  fputs("/* Forward declarations of all message types.\n", stream);
  fputs(" * So they can refer to each other in ", stream);
  fputs("possibly-recursive ways. */\n\n", stream);

  for(int i = 0; i < num_entries; i++) {  /* Foreach message */
    if(entries[i]->type != UPB_SYM_MESSAGE) continue;
    struct upb_symtab_entry *entry = entries[i];
    /* We use entry->e.key (the fully qualified name). */
    struct upb_string *msg_name = upb_strdup(&entry->e.key);
    to_cident(msg_name);
    fprintf(stream, "struct " UPB_STRFMT ";\n", UPB_STRARG(msg_name));
    fprintf(stream, "typedef struct " UPB_STRFMT "\n    " UPB_STRFMT ";\n\n",
            UPB_STRARG(msg_name), UPB_STRARG(msg_name));
    upb_string_unref(msg_name);
  }

  /* Message Declarations. */
  fputs("/* The message definitions themselves. */\n\n", stream);
  for(int i = 0; i < num_entries; i++) {  /* Foreach message */
    if(entries[i]->type != UPB_SYM_MESSAGE) continue;
    struct upb_symtab_entry *entry = entries[i];
    struct upb_msgdef *m = entry->ref.msg;
    /* We use entry->e.key (the fully qualified name). */
    struct upb_string *msg_name = upb_strdup(&entry->e.key);
    to_cident(msg_name);
    fprintf(stream, "struct " UPB_STRFMT " {\n", UPB_STRARG(msg_name));
    fputs("  struct upb_mmhead mmhead;\n", stream);
    fputs("  struct upb_msgdef *def;\n", stream);
    fputs("  union {\n", stream);
    fprintf(stream, "    uint8_t bytes[%" PRIu32 "];\n", m->set_flags_bytes);
    fputs("    struct {\n", stream);
    for(uint32_t j = 0; j < m->num_fields; j++) {
      static char* labels[] = {"", "optional", "required", "repeated"};
      struct google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
      fprintf(stream, "      bool " UPB_STRFMT ":1;  /* = %" PRIu32 ", %s. */\n",
              UPB_STRARG(fd->name), fd->number, labels[fd->label]);
    }
    fputs("    } has;\n", stream);
    fputs("  } set_flags;\n", stream);
    for(uint32_t j = 0; j < m->num_fields; j++) {
      struct upb_fielddef *f = &m->fields[j];
      struct google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
      if(f->type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP ||
         f->type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE) {
        /* Submessages get special treatment, since we have to use the message
         * name directly. */
        struct upb_string type_name_ref = *fd->type_name;
        if(type_name_ref.ptr[0] == UPB_SYMBOL_SEPARATOR) {
          /* Omit leading '.'. */
          type_name_ref.ptr++;
          type_name_ref.byte_len--;
        }
        struct upb_string *type_name = upb_strdup(&type_name_ref);
        to_cident(type_name);
        if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) {
          fprintf(stream, "  UPB_MSG_ARRAY(" UPB_STRFMT ")* " UPB_STRFMT ";\n",
                  UPB_STRARG(type_name), UPB_STRARG(fd->name));
        } else {
          fprintf(stream, "  " UPB_STRFMT "* " UPB_STRFMT ";\n",
                  UPB_STRARG(type_name), UPB_STRARG(fd->name));
        }
        upb_string_unref(type_name);
      } else if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) {
        static char* c_types[] = {
          "", "struct upb_double_array*", "struct upb_float_array*",
          "struct upb_int64_array*", "struct upb_uint64_array*",
          "struct upb_int32_array*", "struct upb_uint64_array*",
          "struct upb_uint32_array*", "struct upb_bool_array*",
          "struct upb_string_array*", "", "",
          "struct upb_string_array*", "struct upb_uint32_array*",
          "struct upb_uint32_array*", "struct upb_int32_array*",
          "struct upb_int64_array*", "struct upb_int32_array*",
          "struct upb_int64_array*"
        };
        fprintf(stream, "  %s " UPB_STRFMT ";\n",
                c_types[fd->type], UPB_STRARG(fd->name));
      } else {
        static char* c_types[] = {
          "", "double", "float", "int64_t", "uint64_t", "int32_t", "uint64_t",
          "uint32_t", "bool", "struct upb_string*", "", "",
          "struct upb_string*", "uint32_t", "int32_t", "int32_t", "int64_t",
          "int32_t", "int64_t"
        };
        fprintf(stream, "  %s " UPB_STRFMT ";\n",
                c_types[fd->type], UPB_STRARG(fd->name));
      }
    }
    fputs("};\n", stream);
    fprintf(stream, "UPB_DEFINE_MSG_ARRAY(" UPB_STRFMT ")\n\n",
            UPB_STRARG(msg_name));
    upb_string_unref(msg_name);
  }

  /* Epilogue. */
  fputs("#ifdef __cplusplus\n", stream);
  fputs("}  /* extern \"C\" */\n", stream);
  fputs("#endif\n\n", stream);
  fprintf(stream, "#endif  /* " UPB_STRFMT " */\n", UPB_STRARG(include_guard_name));
  upb_string_unref(include_guard_name);
}

/* Format of table entries that we use when analyzing data structures for
 * write_messages_c. */
struct strtable_entry {
  struct upb_strtable_entry e;
  int offset;
  int num;
};

struct typetable_entry {
  struct upb_strtable_entry e;
  struct upb_fielddef *field;
  struct upb_string *cident;  /* Type name converted with to_cident(). */
  /* A list of all values of this type, in an established order. */
  union upb_value *values;
  int values_size, values_len;
  struct array {
    int offset;
    int len;
    struct upb_array *ptr;  /* So we can find it later. */
  } *arrays;
  int arrays_size, arrays_len;
};

struct msgtable_entry {
  struct upb_inttable_entry e;
  void *msg;
  int num;  /* Unique offset into the list of all msgs of this type. */
};

int compare_entries(const void *_e1, const void *_e2)
{
  struct strtable_entry *const*e1 = _e1, *const*e2 = _e2;
  return upb_strcmp(&(*e1)->e.key, &(*e2)->e.key);
}

/* Mutually recursive functions to recurse over a set of possibly nested
 * messages and extract all the strings.
 *
 * TODO: make these use a generic msg visitor. */

static void add_strings_from_msg(void *data, struct upb_msgdef *m,
                                 struct upb_strtable *t);

static void add_strings_from_value(union upb_value_ptr p,
                                   struct upb_fielddef *f,
                                   struct upb_strtable *t)
{
  if(upb_isstringtype(f->type)) {
    struct strtable_entry e = {.e = {.key = **p.str}};
    if(upb_strtable_lookup(t, &e.e.key) == NULL)
      upb_strtable_insert(t, &e.e);
  } else if(upb_issubmsg(f)) {
    add_strings_from_msg(*p.msg, f->ref.msg, t);
  }
}

static void add_strings_from_msg(void *data, struct upb_msgdef *m,
                                 struct upb_strtable *t)
{
  for(uint32_t i = 0; i < m->num_fields; i++) {
    struct upb_fielddef *f = &m->fields[i];
    if(!upb_msg_isset(data, f)) continue;
    union upb_value_ptr p = upb_msg_getptr(data, f);
    if(upb_isarray(f)) {
      struct upb_array *arr = *p.arr;
      for(uint32_t j = 0; j < arr->len; j++)
        add_strings_from_value(upb_array_getelementptr(arr, j), f, t);
    } else {
      add_strings_from_value(p, f, t);
    }
  }
}

/* Mutually recursive functions to recurse over a set of possibly nested
 * messages and extract all the messages (keyed by type).
 *
 * TODO: make these use a generic msg visitor. */

struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t,
                                                struct upb_fielddef *f)
{
  struct upb_string *type_name = upb_issubmsg(f) ? upb_strdup(&f->ref.msg->fqname) :
                                                   upb_strdupc(upb_type_info[f->type].ctype);
  struct typetable_entry *type_e = upb_strtable_lookup(t, type_name);
  if(type_e == NULL) {
    struct typetable_entry new_type_e = {
      .e = {.key = *type_name}, .field = f, .cident = upb_strdup(type_name),
      .values = NULL, .values_size = 0, .values_len = 0,
      .arrays = NULL, .arrays_size = 0, .arrays_len = 0
    };
    to_cident(new_type_e.cident);
    assert(upb_strtable_lookup(t, type_name) == NULL);
    assert(upb_strtable_lookup(t, &new_type_e.e.key) == NULL);
    upb_strtable_insert(t, &new_type_e.e);
    type_e = upb_strtable_lookup(t, type_name);
    assert(type_e);
  } else {
    upb_string_unref(type_name);
  }
  return type_e;
}

static void add_value(union upb_value_ptr p, struct upb_fielddef *f,
                      struct upb_strtable *t)
{
  struct typetable_entry *type_e = get_or_insert_typeentry(t, f);
  if(type_e->values_len == type_e->values_size) {
    type_e->values_size = UPB_MAX(type_e->values_size * 2, 4);
    type_e->values = realloc(type_e->values, sizeof(*type_e->values) * type_e->values_size);
  }
  type_e->values[type_e->values_len++] = upb_value_read(p, f->type);
}

static void add_submsgs(void *data, struct upb_msgdef *m, struct upb_strtable *t)
{
  for(uint32_t i = 0; i < m->num_fields; i++) {
    struct upb_fielddef *f = &m->fields[i];
    if(!upb_msg_isset(data, f)) continue;
    union upb_value_ptr p = upb_msg_getptr(data, f);
    if(upb_isarray(f)) {
      if(upb_isstring(f)) continue;  /* Handled by a different code-path. */
      struct upb_array *arr = *p.arr;

      /* Add to our list of arrays for this type. */
      struct typetable_entry *arr_type_e =
          get_or_insert_typeentry(t, f);
      if(arr_type_e->arrays_len == arr_type_e->arrays_size) {
        arr_type_e->arrays_size = UPB_MAX(arr_type_e->arrays_size * 2, 4);
        arr_type_e->arrays = realloc(arr_type_e->arrays,
                                     sizeof(*arr_type_e->arrays)*arr_type_e->arrays_size);
      }
      arr_type_e->arrays[arr_type_e->arrays_len].offset = arr_type_e->values_len;
      arr_type_e->arrays[arr_type_e->arrays_len].len = arr->len;
      arr_type_e->arrays[arr_type_e->arrays_len].ptr = *p.arr;
      arr_type_e->arrays_len++;

      /* Add the individual values in the array. */
      for(uint32_t j = 0; j < arr->len; j++)
        add_value(upb_array_getelementptr(arr, j), f, t);

      /* Add submsgs.  We must do this separately so that the msgs in this
       * array are contiguous (and don't have submsgs of the same type
       * interleaved). */
      for(uint32_t j = 0; j < arr->len; j++)
        add_submsgs(*upb_array_getelementptr(arr, j).msg, f->ref.msg, t);
    } else {
      if(!upb_issubmsg(f)) continue;
      add_value(p, f, t);
      add_submsgs(*p.msg, f->ref.msg, t);
    }
  }
}

/* write_messages_c emits a .c file that contains the data of a protobuf,
 * serialized as C structures. */
static void write_message_c(void *data, struct upb_msgdef *m,
                            char *cident, char *hfile_name,
                            int argc, char *argv[], char *infile_name,
                            FILE *stream)
{
  fputs(
    "/*\n"
    " * This file is a data dump of a protocol buffer into a C structure.\n"
    " * It was created by the upb compiler (upbc) with the following\n"
    " * command-line:\n"
    " *\n", stream);
  fputs(" *   ", stream);
  for(int i = 0; i < argc; i++) {
    fputs(argv[i], stream);
    if(i < argc-1) fputs(" ", stream);
  }
  fputs("\n *\n", stream);
  fprintf(stream, " * This file is a dump of '%s'.\n", infile_name);
  fputs(
    " * It contains exactly the same data, but in a C structure form\n"
    " * instead of a serialized protobuf.  This file contains no code,\n"
    " * only data.\n"
    " *\n"
    " * This file was auto-generated.  Do not edit. */\n\n", stream);

  fprintf(stream, "#include \"%s\"\n\n", hfile_name);

  /* Gather all strings into a giant string.  Use a hash to prevent adding the
   * same string more than once. */
  struct upb_strtable strings;
  upb_strtable_init(&strings, 16, sizeof(struct strtable_entry));
  add_strings_from_msg(data, m, &strings);

  int size;
  struct strtable_entry **str_entries = strtable_to_array(&strings, &size);
  /* Sort for nice size and reproduceability. */
  qsort(str_entries, size, sizeof(void*), compare_entries);

  /* Emit strings. */
  fputs("static char strdata[] =\n  \"", stream);
  int col = 2;
  int offset = 0;
  for(int i = 0; i < size; i++) {
    struct upb_string *s = &str_entries[i]->e.key;
    str_entries[i]->offset = offset;
    str_entries[i]->num = i;
    for(uint32_t j = 0; j < s->byte_len; j++) {
      if(++col == 80) {
        fputs("\"\n  \"", stream);
        col = 3;
      }
      fputc(s->ptr[j], stream);
    }
    offset += s->byte_len;
  }
  fputs("\";\n\n", stream);

  fputs("static struct upb_string strings[] = {\n", stream);
  for(int i = 0; i < size; i++) {
    struct strtable_entry *e = str_entries[i];
    fprintf(stream, "  {.ptr = &strdata[%d], .byte_len=%d},\n", e->offset, e->e.key.byte_len);
  }
  fputs("};\n\n", stream);
  free(str_entries);

  /* Gather a list of types for which we are emitting data, and give each msg
   * a unique number within its type. */
  struct upb_strtable types;
  upb_strtable_init(&types, 16, sizeof(struct typetable_entry));
  union upb_value val = {.msg = data};
  /* A fake field to get the recursion going. */
  struct upb_fielddef fake_field = {
      .type = GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE,
      .ref = {.msg = m}
  };
  add_value(upb_value_addrof(&val), &fake_field, &types);
  add_submsgs(data, m, &types);

  /* Emit foward declarations for all msgs of all types, and define arrays. */
  fprintf(stream, "/* Forward declarations of messages, and array decls. */\n");
  struct typetable_entry *e = upb_strtable_begin(&types);
  for(; e; e = upb_strtable_next(&types, &e->e)) {
    fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d];\n\n",
            UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->values_len);
    if(e->arrays_len > 0) {
      fprintf(stream, "static " UPB_STRFMT " *" UPB_STRFMT "_array_elems[] = {\n",
              UPB_STRARG(e->cident), UPB_STRARG(e->cident));
      for(int i = 0; i < e->arrays_len; i++) {
        struct array *arr = &e->arrays[i];
        for(int j = 0; j < arr->len; j++)
          fprintf(stream, "    &" UPB_STRFMT "_values[%d],\n", UPB_STRARG(e->cident), arr->offset + j);
      }
      fprintf(stream, "};\n");

      int cum_offset = 0;
      fprintf(stream, "static UPB_MSG_ARRAY(" UPB_STRFMT ") " UPB_STRFMT "_arrays[%d] = {\n",
              UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->arrays_len);
      for(int i = 0; i < e->arrays_len; i++) {
        struct array *arr = &e->arrays[i];
        fprintf(stream, "  {.elements = &" UPB_STRFMT "_array_elems[%d], .len=%d},\n",
                UPB_STRARG(e->cident), cum_offset, arr->len);
        cum_offset += arr->len;
      }
      fprintf(stream, "};\n");
    }
  }

  /* Emit definitions. */
  for(e = upb_strtable_begin(&types); e; e = upb_strtable_next(&types, &e->e)) {
    fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d] = {\n\n",
            UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->values_len);
    for(int i = 0; i < e->values_len; i++) {
      union upb_value val = e->values[i];
      if(upb_issubmsg(e->field)) {
        struct upb_msgdef *m = e->field->ref.msg;
        void *msgdata = val.msg;
        /* Print set flags. */
        fputs("  {.set_flags = {.has = {\n", stream);
        for(unsigned int j = 0; j < m->num_fields; j++) {
          struct upb_fielddef *f = &m->fields[j];
          google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
          fprintf(stream, "    ." UPB_STRFMT " = ", UPB_STRARG(fd->name));
          if(upb_msg_isset(msgdata, f))
            fprintf(stream, "true");
          else
            fprintf(stream, "false");
          fputs(",\n", stream);
        }
        fputs("  }},\n", stream);
        /* Print msg data. */
        for(unsigned int j = 0; j < m->num_fields; j++) {
          struct upb_fielddef *f = &m->fields[j];
          google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
          union upb_value val = upb_value_read(upb_msg_getptr(msgdata, f), f->type);
          fprintf(stream, "    ." UPB_STRFMT " = ", UPB_STRARG(fd->name));
          if(!upb_msg_isset(msgdata, f)) {
            fputs("0,   /* Not set. */", stream);
          } else if(upb_isstring(f)) {
            if(upb_isarray(f)) {
              fputs("Ack, string arrays are not supported yet!\n", stderr);
              exit(1);
            } else {
              struct strtable_entry *str_e = upb_strtable_lookup(&strings, val.str);
              assert(str_e);
              fprintf(stream, "&strings[%d],   /* \"" UPB_STRFMT "\" */",
                      str_e->num, UPB_STRARG(val.str));
            }
          } else if(upb_isarray(f)) {
            /* Find this submessage in the list of msgs for that type. */
            struct typetable_entry  *type_e = get_or_insert_typeentry(&types, f);
            assert(type_e);
            int arr_num = -1;
            for(int k = 0; k < type_e->arrays_len; k++) {
              if(type_e->arrays[k].ptr == val.arr) {
                arr_num = k;
                break;
              }
            }
            assert(arr_num != -1);
            fprintf(stream, "&" UPB_STRFMT "_arrays[%d],", UPB_STRARG(type_e->cident), arr_num);
          } else if(upb_issubmsg(f)) {
            /* Find this submessage in the list of msgs for that type. */
            struct typetable_entry  *type_e = get_or_insert_typeentry(&types, f);
            assert(type_e);
            int msg_num = -1;
            for(int k = 0; k < type_e->values_len; k++) {
              if(type_e->values[k].msg == val.msg) {
                msg_num = k;
                break;
              }
            }
            assert(msg_num != -1);
            fprintf(stream, "&" UPB_STRFMT "_values[%d],", UPB_STRARG(type_e->cident), msg_num);
          } else {
            upb_text_printval(f->type, val, stream);
            fputs(",", stream);
          }
          fputs("\n", stream);
        }
        fputs("  },\n", stream);
      } else if(upb_isstring(e->field)) {

      } else {
        /* Non string, non-message data. */
        upb_text_printval(e->field->type, val, stream);
      }
    }
    fputs("};\n", stream);
  }

  struct typetable_entry *toplevel_type =
      get_or_insert_typeentry(&types, &fake_field);
  assert(toplevel_type);
  fputs("/* The externally-visible definition. */\n", stream);
  /* It is always at offset zero, because we add it first. */
  fprintf(stream, UPB_STRFMT " *%s = &" UPB_STRFMT "_values[0];\n",
          UPB_STRARG(toplevel_type->cident), cident,
          UPB_STRARG(toplevel_type->cident));

  /* Free tables. */
  for(e = upb_strtable_begin(&types); e; e = upb_strtable_next(&types, &e->e)) {
    upb_string_unref(e->cident);
    free(e->values);
    free(e->arrays);
  }
  upb_strtable_free(&types);
  upb_strtable_free(&strings);
}

const char usage[] =
  "upbc -- upb compiler.\n"
  "upb v0.1  http://blog.reverberate.org/upb/\n"
  "\n"
  "Usage: upbc [options] descriptor-file\n"
  "\n"
  "  -i C-IDENFITER     Output the descriptor as a C data structure with the\n"
  "                     given identifier (otherwise only a header will be\n"
  "                     generated\n"
  "\n"
  "  -o OUTFILE-BASE    Write to OUTFILE-BASE.h and OUTFILE-BASE.c instead\n"
  "                     of using the input file as a basename.\n"
;

void usage_err(char *err)
{
  fprintf(stderr, "upbc: %s\n\n", err);
  fputs(usage, stderr);
  exit(1);
}

void error(char *err, ...)
{
  va_list args;
  va_start(args, err);
  fprintf(stderr, "upbc: ");
  vfprintf(stderr, err, args);
  va_end(args);
  exit(1);
}

void sort_fields_in_descriptor(google_protobuf_DescriptorProto *d)
{
  if(d->set_flags.has.field) upb_msgdef_sortfds(d->field->elements, d->field->len);
  if(d->set_flags.has.nested_type)
    for(uint32_t i = 0; i < d->nested_type->len; i++)
      sort_fields_in_descriptor(d->nested_type->elements[i]);
}

int main(int argc, char *argv[])
{
  /* Parse arguments. */
  char *outfile_base = NULL, *input_file = NULL, *cident = NULL;
  for(int i = 1; i < argc; i++) {
    if(strcmp(argv[i], "-o") == 0) {
      if(++i == argc)
        usage_err("-o must be followed by a FILE-BASE.");
      else if(outfile_base)
        usage_err("-o was specified multiple times.");
      outfile_base = argv[i];
    } else if(strcmp(argv[i], "-i") == 0) {
      if(++i == argc)
        usage_err("-i must be followed by a C-IDENTIFIER.");
      else if(cident)
        usage_err("-i was specified multiple times.");
      cident = argv[i];
    } else {
      if(input_file)
        usage_err("You can only specify one input file.");
      input_file = argv[i];
    }
  }
  if(!input_file) usage_err("You must specify an input file.");
  if(!outfile_base) outfile_base = input_file;

  /* Read input file. */
  struct upb_string *descriptor = upb_strreadfile(input_file);
  if(!descriptor)
    error("Couldn't read input file.");

  /* Parse input file. */
  struct upb_context *c = upb_context_new();
  struct upb_msg *fds_msg = upb_msg_new(c->fds_msg);
  struct upb_status status = UPB_STATUS_INIT;
  upb_msg_parsestr(fds_msg, descriptor->ptr, descriptor->byte_len, &status);
  if(!upb_ok(&status))
    error("Failed to parse input file descriptor: %s", status.msg);
  //upb_msg_print(fds_msg, false, stderr);
  google_protobuf_FileDescriptorSet *fds = (void*)fds_msg;
  upb_context_addfds(c, fds, &status);
  if(!upb_ok(&status))
    error("Failed to resolve symbols in descriptor: %s", status.msg);

  /* We need to sort the fields of all the descriptors.  They will already be
   * sorted in the upb_msgs that we base our header file output on, so we must
   * sort here to match. */
  for(uint32_t i = 0; i < fds->file->len; i++) {
    google_protobuf_FileDescriptorProto *fd = fds->file->elements[i];
    if(!fd->set_flags.has.message_type) continue;
    for(uint32_t j = 0; j < fd->message_type->len; j++)
      sort_fields_in_descriptor(fd->message_type->elements[j]);
  }

  /* Emit output files. */
  const int maxsize = 256;
  char h_filename[maxsize], h_const_filename[maxsize], c_filename[maxsize];
  if(snprintf(h_filename, maxsize, "%s.h", outfile_base) >= maxsize ||
     snprintf(c_filename, maxsize, "%s.c", outfile_base) >= maxsize ||
     snprintf(h_const_filename, maxsize, "%s_const.h", outfile_base) >= maxsize)
    error("File base too long.\n");

  FILE *h_file = fopen(h_filename, "w");
  if(!h_file) error("Failed to open .h output file");
  FILE *h_const_file = fopen(h_const_filename, "w");
  if(!h_const_file) error("Failed to open _const.h output file");

  int symcount;
  struct upb_symtab_entry **entries = strtable_to_array(&c->symtab, &symcount);
  write_h(entries, symcount, h_filename, cident, h_file);
  write_const_h(entries, symcount, h_filename, h_const_file);
  free(entries);
  if(cident) {
    FILE *c_file = fopen(c_filename, "w");
    if(!c_file) error("Failed to open .h output file");
    write_message_c(fds, c->fds_msg, cident, h_filename, argc, argv, input_file, c_file);
    fclose(c_file);
  }
  upb_msg_unref(fds_msg);
  upb_context_unref(c);
  upb_string_unref(descriptor);
  fclose(h_file);
  fclose(h_const_file);

  return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback