forked from eclipse-cyclonedds/cyclonedds-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.c
1157 lines (1035 loc) · 33.7 KB
/
generator.c
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright(c) 2020 to 2022 ZettaScale Technology and others
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
// v. 1.0 which is available at
// http://www.eclipse.org/org/documents/edl-v10.php.
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#include <assert.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include "idl/file.h"
#include "idl/version.h"
#include "idl/processor.h"
#include "idl/string.h"
#include "idl/stream.h"
#include "idl/visit.h"
#include "generator.h"
static size_t istok(const char *str, size_t len, const char **toks)
{
for (size_t num = 0; toks && toks[num]; num++) {
if (strncmp(toks[num], str, len) == 0)
return num+1;
}
return 0;
}
static int makefmtp(
char **fmtp, const char *str, const char **toks, const char **flags)
{
char buf[64], *fmt;
size_t len = 0;
size_t num, src, tok, dest;
#define FMT "%%%zu$%s"
src = tok = 0;
do {
if (tok) {
if (str[src] == '}' && (num = istok(str+tok, src-tok, toks))) {
const char *flag;
assert(toks && flags);
if (!flags)
return -1;
flag = flags[num-1];
len += (size_t)snprintf(buf, sizeof(buf), FMT, num, flag);
tok = 0;
} else if (str[src] == '}' || str[src] == '\0') {
/* unknown token, rewind */
len += 1;
src = tok - 1;
tok = 0;
}
} else if (str[src] == '{') {
tok = src + 1;
} else if (str[src]) {
len += 1u + (str[src] == '%'); /* escape % */
}
} while (str[src++]);
if (!(fmt = malloc(len + 1)))
return -1;
src = tok = dest = 0;
do {
if (tok) {
if (str[src] == '}' && (num = istok(str+tok, src-tok, toks))) {
const char *flag;
assert(toks && flags);
flag = flags[num-1];
dest += (size_t)snprintf(&fmt[dest], (len-dest)+1, FMT, num, flag);
tok = 0;
} else if (str[src] == '}' || str[src] == '\0') {
fmt[dest++] = '{';
src = tok - 1;
tok = 0;
}
} else if (str[src] == '{') {
tok = src + 1;
} else if (str[src] == '%') {
fmt[dest++] = '%';
fmt[dest++] = '%';
} else if (str[src]) {
fmt[dest++] = str[src];
}
} while (str[src++]);
assert(dest == len);
fmt[len] = '\0';
#undef FMT
*fmtp = fmt;
return (int)len;
}
/* If a keyword matches the specified identifier, prepend _cxx_ */
static const char *cpp11_keywords[] = {
#define _cxx_(str) "_cxx_" str
_cxx_("alignas"), _cxx_("alignof"), _cxx_("and"), _cxx_("and_eq"),
_cxx_("asm"), _cxx_("auto"), _cxx_("bitand"), _cxx_("bitor"), _cxx_("bool"),
_cxx_("break"), _cxx_("case"), _cxx_("catch"), _cxx_("char"),
_cxx_("char16_t"), _cxx_("char32_t"), _cxx_("class"), _cxx_("compl"),
_cxx_("concept"), _cxx_("const"), _cxx_("constexpr"), _cxx_("const_cast"),
_cxx_("continue"), _cxx_("decltype"), _cxx_("default"), _cxx_("delete"),
_cxx_("do"), _cxx_("double"), _cxx_("dynamic_cast"), _cxx_("else"),
_cxx_("enum"), _cxx_("explicit"), _cxx_("export"), _cxx_("extern"),
_cxx_("false"), _cxx_("float"), _cxx_("for"), _cxx_("friend"), _cxx_("goto"),
_cxx_("if"), _cxx_("inline"), _cxx_("int"), _cxx_("long"), _cxx_("mutable"),
_cxx_("namespace"), _cxx_("new"), _cxx_("noexcept"), _cxx_("not"),
_cxx_("not_eq"), _cxx_("nullptr"), _cxx_("operator"), _cxx_("or"),
_cxx_("or_eq"), _cxx_("private"), _cxx_("protected"), _cxx_("public"),
_cxx_("register"), _cxx_("reinterpret_cast"), _cxx_("requires"),
_cxx_("return"), _cxx_("short"), _cxx_("signed"), _cxx_("sizeof"),
_cxx_("static"), _cxx_("static_assert"), _cxx_("static_cast"),
_cxx_("struct"), _cxx_("switch"), _cxx_("template"), _cxx_("this"),
_cxx_("thread_local"), _cxx_("throw"), _cxx_("true"), _cxx_("try"),
_cxx_("typedef"), _cxx_("typeid"), _cxx_("typename"), _cxx_("union"),
_cxx_("unsigned"), _cxx_("using"), _cxx_("virtual"), _cxx_("void"),
_cxx_("volatile"), _cxx_("wchar_t"), _cxx_("while"), _cxx_("xor"),
_cxx_("xor_eq"), _cxx_("int16_t"), _cxx_("int32_t"), _cxx_("int64_t"),
_cxx_("uint8_t"), _cxx_("uint16_t"), _cxx_("uint32_t"), _cxx_("uint64_t")
#undef _cxx_
};
const char *get_cpp11_name(const void *node)
{
const char *name = idl_identifier(node);
static const size_t n = sizeof(cpp11_keywords)/sizeof(cpp11_keywords[0]);
/* search through the C++ keyword list */
for (size_t i=0; i < n; i++) {
if (strcmp(cpp11_keywords[i] + 5, name) == 0)
return cpp11_keywords[i];
}
return name;
}
static int get_cpp11_base_type(
char *str, size_t size, const void *node, void *user_data)
{
const char *type;
(void)user_data;
switch (idl_type(node)) {
case IDL_CHAR: type = "char"; break;
case IDL_WCHAR: type = "wchar"; break;
case IDL_BOOL: type = "bool"; break;
case IDL_INT8: type = "int8_t"; break;
case IDL_UINT8:
case IDL_OCTET: type = "uint8_t"; break;
case IDL_SHORT:
case IDL_INT16: type = "int16_t"; break;
case IDL_USHORT:
case IDL_UINT16: type = "uint16_t"; break;
case IDL_LONG:
case IDL_INT32: type = "int32_t"; break;
case IDL_ULONG:
case IDL_UINT32: type = "uint32_t"; break;
case IDL_LLONG:
case IDL_INT64: type = "int64_t"; break;
case IDL_ULLONG:
case IDL_UINT64: type = "uint64_t"; break;
case IDL_FLOAT: type = "float"; break;
case IDL_DOUBLE: type = "double"; break;
case IDL_LDOUBLE: type = "long double"; break;
default:
abort();
}
return idl_snprintf(str, size, "%s", type);
}
static int get_cpp11_templ_type(
char *str, size_t size, const void *node, void *user_data)
{
struct generator *gen = user_data;
const char *fmt;
assert(str && size);
switch (idl_type(node)) {
case IDL_SEQUENCE: {
int cnt;
char buf[128], *type = buf;
const idl_sequence_t *sequence = node;
if (sequence->maximum)
fmt = gen->bounded_sequence_format;
else
fmt = gen->sequence_format;
if ((cnt = get_cpp11_type(type, sizeof(buf), sequence->type_spec, gen)) < 0)
return -1;
if ((size_t)cnt < sizeof(buf))
return idl_snprintf(str, size, fmt, type, sequence->maximum);
if (!(type = malloc((size_t)cnt+1)))
return -1;
if ((cnt = get_cpp11_type(type, (size_t)cnt+1, sequence->type_spec, gen)) >= 0)
cnt = idl_snprintf(str, size, fmt, type, sequence->maximum);
free(type);
return cnt;
}
case IDL_STRING: {
const idl_string_t *string = node;
if (string->maximum)
fmt = gen->bounded_string_format;
else
fmt = gen->string_format;
return idl_snprintf(str, size, fmt, string->maximum);
}
default:
break;
}
abort();
}
static int get_cpp11_array_type(
char *str, size_t size, const void *node, void *user_data)
{
struct generator *gen = user_data;
const idl_type_spec_t *type_spec = idl_type_spec(node);
const idl_const_expr_t *const_expr;
int cnt;
char buf[1], *type = NULL;
assert(idl_is_declarator(node));
if ((cnt = get_cpp11_type(buf, sizeof(buf), type_spec, gen)) < 0)
return -1;
if (!(type = malloc((size_t)cnt + 1)))
return -1;
cnt = get_cpp11_type(type, (size_t)cnt + 1, type_spec, gen);
if (cnt < 0)
goto err_type;
const_expr = ((const idl_declarator_t *)node)->const_expr;
/* iterate backwards through the list so that the last entries in the list
are the innermost arrays */
for (const idl_const_expr_t *ce = const_expr; ce; ce = idl_next(ce))
const_expr = ce;
assert(const_expr);
for (const idl_const_expr_t *ce = const_expr; ce; ce = idl_previous(ce)) {
char *ptr = NULL;
uint32_t dim = ((const idl_literal_t *)ce)->value.uint32;
cnt = idl_asprintf(&ptr, gen->array_format, type, dim);
if (cnt < 0)
goto err_type;
free(type);
type = ptr;
}
cnt = idl_snprintf(str, size, "%s", type);
err_type:
free(type);
return cnt;
}
static void copy(
char *dest, size_t off, size_t size, const char *src, size_t len)
{
size_t cnt;
if (off >= size)
return;
cnt = size - off;
cnt = cnt > len ? len : cnt;
memmove(dest+off, src, cnt);
}
static int get_cpp11_fully_scoped_name_seps(
char *str, size_t size, const void *node, const char *sep)
{
const char *name;
size_t cnt, off, len = 0;
static const idl_mask_t mask =
IDL_MODULE | IDL_STRUCT | IDL_UNION | IDL_ENUM |
IDL_ENUMERATOR | IDL_DECLARATOR | IDL_BITMASK;
assert(str && size);
for (const idl_node_t *n = node; n; n = n->parent) {
if (!(idl_mask(n) & mask))
continue;
name = get_cpp11_name(n);
len += strlen(name) + strlen(sep);
}
off = len;
for (const idl_node_t *n = node; n; n = n->parent) {
if (!(idl_mask(n) & mask))
continue;
name = get_cpp11_name(n);
assert(name);
cnt = strlen(name);
assert(cnt <= off);
off -= cnt;
copy(str, off, size, name, cnt);
cnt = strlen(sep);
assert(cnt <= off);
off -= cnt;
copy(str, off, size, sep, cnt);
}
assert(off == 0);
str[ (len < size ? len : size - 1) ] = '\0';
return (int)len;
}
int get_cpp11_fully_scoped_name(
char *str, size_t size, const void *node, void *user_data)
{
(void)user_data;
return get_cpp11_fully_scoped_name_seps(str, size, node, "::");
}
int get_cpp11_name_typedef(
char *str, size_t size, const void *node, void *user_data)
{
(void)user_data;
return get_cpp11_fully_scoped_name_seps(str, size, node, "_");
}
int get_cpp11_type(
char *str, size_t size, const void *node, void *user_data)
{
if (idl_is_case(node)) {
const idl_case_t *_case = node;
if (idl_is_array(_case->declarator))
return get_cpp11_type(str, size, _case->declarator, user_data);
else
return get_cpp11_type(str, size, _case->type_spec, user_data);
} else if (idl_is_base_type(node))
return get_cpp11_base_type(str, size, node, user_data);
else if (idl_is_templ_type(node))
return get_cpp11_templ_type(str, size, node, user_data);
else if (idl_is_array(node))
return get_cpp11_array_type(str, size, node, user_data);
else
return get_cpp11_fully_scoped_name(str, size, node, user_data);
}
int get_cpp11_default_value(
char *str, size_t size, const void *node, void *user_data)
{
struct generator *gen = user_data;
const idl_enumerator_t *enumerator;
const idl_enum_t *e;
const idl_type_spec_t *unaliased = idl_strip(node, IDL_STRIP_ALIASES | IDL_STRIP_FORWARD);
const char *value = NULL;
if (idl_is_array(unaliased))
return idl_snprintf(str, size, "{ }");
switch (idl_type(unaliased)) {
case IDL_ENUM:
e = ((const idl_enum_t *)unaliased);
enumerator = e->default_enumerator ? e->default_enumerator : e->enumerators;
return get_cpp11_fully_scoped_name(str, size, enumerator, gen);
case IDL_BOOL:
value = "false";
break;
case IDL_CHAR:
case IDL_WCHAR:
case IDL_OCTET:
case IDL_BITMASK:
value = "0";
break;
case IDL_FLOAT:
value = "0.0f";
break;
case IDL_DOUBLE:
case IDL_LDOUBLE:
value = "0.0";
break;
case IDL_INT8:
case IDL_UINT8:
case IDL_INT16:
case IDL_UINT16:
case IDL_SHORT:
case IDL_USHORT:
case IDL_INT32:
case IDL_UINT32:
case IDL_LONG:
case IDL_ULONG:
case IDL_INT64:
case IDL_UINT64:
case IDL_LLONG:
case IDL_ULLONG:
value = "0";
break;
default:
abort();
}
return idl_snprintf(str, size, "%s", value);
}
int get_cpp11_base_type_const_value(
char *str, size_t size, const void *node, void *user_data)
{
const idl_literal_t *literal = node;
(void)user_data;
switch (idl_type(literal)) {
case IDL_BOOL:
return idl_snprintf(str, size, "%s", literal->value.bln ? "true" : "false");
case IDL_INT8:
return idl_snprintf(str, size, "%" PRId8, literal->value.int8);
case IDL_UINT8:
case IDL_OCTET:
return idl_snprintf(str, size, "%" PRIu8, literal->value.uint8);
case IDL_INT16:
case IDL_SHORT:
return idl_snprintf(str, size, "%" PRId16, literal->value.int16);
case IDL_UINT16:
case IDL_USHORT:
return idl_snprintf(str, size, "%" PRIu16, literal->value.uint16);
case IDL_INT32:
case IDL_LONG:
return idl_snprintf(str, size, "%" PRId32, literal->value.int32);
case IDL_UINT32:
case IDL_ULONG:
return idl_snprintf(str, size, "%" PRIu32, literal->value.uint32);
case IDL_INT64:
case IDL_LLONG:
return idl_snprintf(str, size, "%" PRId64, literal->value.int64);
case IDL_UINT64:
case IDL_ULLONG:
case IDL_BITMASK:
return idl_snprintf(str, size, "%" PRIu64, literal->value.uint64);
case IDL_FLOAT:
return idl_snprintf(str, size, "%.6f", literal->value.flt);
case IDL_DOUBLE:
return idl_snprintf(str, size, "%f", literal->value.dbl);
case IDL_LDOUBLE:
return idl_snprintf(str, size, "%Lf", literal->value.ldbl);
case IDL_CHAR:
if (idl_isprint(literal->value.chr))
return idl_snprintf(str, size, "\'%c\'", literal->value.chr);
return idl_snprintf(str, size, "0x%X", literal->value.chr);
case IDL_STRING:
return idl_snprintf(str, size, "\"%s\"", literal->value.str);
default:
break;
}
abort();
}
static int get_cpp11_templ_type_const_value(
char *str, size_t size, const void *node, void *user_data)
{
const idl_literal_t *literal = node;
(void)user_data;
if (!idl_is_string(literal))
return -1;
assert(literal->value.str);
return idl_snprintf(str, size, "\"%s\"", literal->value.str);
}
int get_cpp11_value(
char *str, size_t size, const void *node, void *user_data)
{
if (idl_type(node) & (IDL_BASE_TYPE | IDL_BITMASK))
return get_cpp11_base_type_const_value(str, size, node, user_data);
if (idl_type(node) & IDL_TEMPL_TYPE)
return get_cpp11_templ_type_const_value(str, size, node, user_data);
if (idl_is_enumerator(node))
return get_cpp11_fully_scoped_name(str, size, node, user_data);
abort();
}
bool is_optional(
const void *node)
{
if (idl_is_member(node)) {
return ((const idl_member_t *)node)->optional.value;
} else if (idl_is_declarator(node)
|| idl_is_type_spec(node)) {
return is_optional(idl_parent(node));
}
return false;
}
bool is_external(
const void *node)
{
if (idl_is_member(node) || idl_is_case(node)) {
return idl_is_external(node);
} else if (idl_is_declarator(node)
|| idl_is_type_spec(node)) {
return is_external(idl_parent(node));
}
return false;
}
bool must_understand(
const void *node)
{
if (idl_is_member(node)) {
return ((const idl_member_t *)node)->must_understand.value;
} else if (idl_is_declarator(node)
|| idl_is_type_spec(node)) {
return must_understand(idl_parent(node));
}
return false;
}
bool is_nested(const void *node)
{
if (idl_is_struct(node)) {
return ((const idl_struct_t *)node)->nested.value;
} else {
assert(idl_is_union(node));
return ((const idl_union_t *)node)->nested.value;
}
}
static bool sc_union(const idl_union_t *_union)
{
if (!is_selfcontained(_union->switch_type_spec->type_spec))
return false;
const idl_case_t *_case = NULL;
IDL_FOREACH(_case, _union->cases) {
if (!is_selfcontained(_case->type_spec))
return false;
}
return true;
}
static bool sc_struct(const idl_struct_t *str)
{
const idl_member_t *mem = NULL;
IDL_FOREACH(mem, str->members) {
if (!is_selfcontained(mem->type_spec))
return false;
}
if (str->inherit_spec)
return is_selfcontained(str->inherit_spec->base);
return true;
}
bool is_selfcontained(const void *node)
{
if (idl_is_sequence(node)
|| idl_is_string(node)
|| idl_is_optional(node)) {
return false;
} else if (idl_is_typedef(node)) {
return is_selfcontained(((const idl_typedef_t*)node)->type_spec);
} else if (idl_is_struct(node)) {
return sc_struct((const idl_struct_t*)node);
} else if (idl_is_union(node)) {
return sc_union((const idl_union_t*)node);
} else if (idl_is_declarator(node)) {
const idl_node_t *parent = ((const idl_node_t*)node)->parent;
assert (idl_is_typedef(parent));
return is_selfcontained(parent);
} else {
return !is_external(node);
}
}
idl_extensibility_t
get_extensibility(const void *node)
{
if (idl_is_enum(node)) {
const idl_enum_t *ptr = node;
return ptr->extensibility.value;
} else if (idl_is_union(node)) {
const idl_union_t *ptr = node;
return ptr->extensibility.value;
} else if (idl_is_struct(node)) {
const idl_struct_t *ptr = node;
return ptr->extensibility.value;
}
return IDL_FINAL;
}
idl_type_t unalias_bitmask(const idl_node_t *node)
{
assert(idl_is_bitmask(node));
uint16_t width = ((const idl_bitmask_t*)node)->bit_bound.value;
if (width > 32)
return IDL_UINT64;
else if (width > 16)
return IDL_UINT32;
else if (width > 8)
return IDL_UINT16;
else
return IDL_UINT8;
}
static char *
figure_guard(const char *file, const idl_md5_byte_t digest[16])
{
char *inc = NULL;
if (idl_asprintf(&inc, "DDSCXX_%s_%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
file, digest[0], digest[1], digest[2], digest[3], digest[4], digest[5],
digest[6], digest[7], digest[8], digest[9], digest[10], digest[11],
digest[12], digest[13], digest[14], digest[15]) == -1 )
return NULL;
/* replace any non-alphanumeric characters */
for (char *ptr = inc; *ptr; ptr++) {
if (idl_islower((unsigned char)*ptr))
*ptr = (char)idl_toupper((unsigned char)*ptr);
else if (!idl_isalnum((unsigned char)*ptr))
*ptr = '_';
}
return inc;
}
static idl_retcode_t
print_header(FILE *fh, const char *in, const char *out)
{
static const char fmt[] =
"/****************************************************************\n"
"\n"
" Generated by Eclipse Cyclone DDS IDL to CXX Translator\n"
" File name: %s\n"
" Source: %s\n"
" Cyclone DDS: v%s\n"
"\n"
"*****************************************************************/\n";
if (idl_fprintf(fh, fmt, in, out, IDL_VERSION) < 0)
return IDL_RETCODE_NO_MEMORY;
return IDL_RETCODE_OK;
}
static idl_retcode_t
print_impl_header(FILE *fh, const char *in, const char *out, const char *hdr)
{
const char *fmt = "#include \"%s\"\n\n";
if (print_header(fh, in, out)
|| idl_fprintf(fh, fmt, hdr) < 0)
return IDL_RETCODE_NO_MEMORY;
return IDL_RETCODE_OK;
}
static idl_retcode_t print_guard_if(FILE* fh, const char *guard)
{
static const char *fmt =
"#ifndef %1$s\n"
"#define %1$s\n\n";
if (idl_fprintf(fh, fmt, guard) < 0)
return IDL_RETCODE_NO_MEMORY;
return IDL_RETCODE_OK;
}
static idl_retcode_t print_guard_endif(FILE *fh, const char *guard)
{
static const char fmt[] =
"#endif // %s\n";
if (idl_fprintf(fh, fmt, guard) < 0)
return IDL_RETCODE_NO_MEMORY;
return IDL_RETCODE_OK;
}
static idl_retcode_t
register_union(
const idl_pstate_t *pstate,
bool revisit,
const idl_path_t *path,
const void *node,
void *user_data)
{
struct generator *gen = user_data;
const idl_location_t *loc;
const char *src = NULL;
(void)revisit;
(void)path;
loc = idl_location(node);
assert(loc);
if (pstate->sources)
src = pstate->sources->path->name;
/* do not include headers if required by types in includes */
if (src && strcmp(loc->first.source->path->name, src) == 0)
gen->uses_union = true;
return IDL_RETCODE_OK;
}
static idl_retcode_t
register_optional_or_external(
const idl_pstate_t *pstate,
bool revisit,
const idl_path_t *path,
const void *node,
void *user_data)
{
(void)pstate;
(void)revisit;
(void)path;
struct generator *gen = user_data;
if (is_optional(node))
gen->uses_optional = true;
if (idl_is_external(node))
gen->uses_external = true;
return IDL_RETCODE_OK;
}
static idl_retcode_t
register_types(
const idl_pstate_t *pstate,
bool revisit,
const idl_path_t *path,
const void *node,
void *user_data)
{
struct generator *gen = user_data;
const idl_type_spec_t *type_spec;
const idl_location_t *loc;
const char *src = NULL;
(void)pstate;
(void)revisit;
(void)path;
if (idl_is_typedef(node))
{
const idl_typedef_t *td = node;
if (idl_is_array(td->declarators))
gen->uses_array = true;
}
if (idl_is_array(node))
gen->uses_array = true;
type_spec = idl_strip(idl_type_spec(node), IDL_STRIP_ALIASES | IDL_STRIP_FORWARD);
loc = idl_location(type_spec);
assert(type_spec && loc);
if (pstate->sources)
src = pstate->sources->path->name;
/* do not include headers if required by types in includes */
if (src && strcmp(loc->first.source->path->name, src) != 0)
return IDL_VISIT_DONT_RECURSE;
if (idl_is_array(type_spec))
gen->uses_array = true;
type_spec = idl_strip(idl_type_spec(node), IDL_STRIP_ALIASES | IDL_STRIP_ALIASES_ARRAY | IDL_STRIP_FORWARD);
loc = idl_location(type_spec);
assert(type_spec && loc);
/* do not include headers if required by types in includes */
if (src && strcmp(loc->first.source->path->name, src) != 0)
return IDL_VISIT_DONT_RECURSE;
if (idl_is_sequence(type_spec)) {
if (idl_is_bounded(type_spec))
gen->uses_bounded_sequence = true;
else
gen->uses_sequence = true;
return IDL_VISIT_TYPE_SPEC;
} else if (idl_is_string(type_spec)) {
if (idl_is_bounded(type_spec))
gen->uses_bounded_string = true;
else
gen->uses_string = true;
} else if (idl_is_union(type_spec)) {
gen->uses_union = true;
} else if (idl_is_integer_type(type_spec)) {
gen->uses_integers = true;
} else if (idl_type(type_spec) == IDL_OCTET) {
gen->uses_integers = true;
}
return IDL_RETCODE_OK;
}
static idl_retcode_t
register_bitmask(
const idl_pstate_t *pstate,
bool revisit,
const idl_path_t *path,
const void *node,
void *user_data)
{
(void)pstate;
(void)revisit;
(void)path;
(void)node;
struct generator *gen = user_data;
assert(idl_is_bitmask(node));
assert(gen);
gen->uses_integers = true;
return IDL_RETCODE_OK;
}
static idl_retcode_t
generate_includes(const idl_pstate_t *pstate, struct generator *generator)
{
idl_retcode_t ret;
idl_visitor_t visitor;
const char *sources[] = { NULL, NULL };
const idl_source_t *include, *source = pstate->sources;
/* determine which "system" headers to include */
memset(&visitor, 0, sizeof(visitor));
visitor.visit = IDL_DECLARATOR | IDL_SEQUENCE | IDL_UNION | IDL_MEMBER | IDL_CONST | IDL_TYPEDEF | IDL_CASE | IDL_BITMASK;
visitor.accept[IDL_ACCEPT_DECLARATOR] = ®ister_types;
visitor.accept[IDL_ACCEPT_TYPEDEF] = ®ister_types;
visitor.accept[IDL_ACCEPT_MEMBER] = ®ister_optional_or_external;
visitor.accept[IDL_ACCEPT_CASE] = ®ister_optional_or_external;
visitor.accept[IDL_ACCEPT_SEQUENCE] = ®ister_types;
visitor.accept[IDL_ACCEPT_CONST] = ®ister_types;
visitor.accept[IDL_ACCEPT_UNION] = ®ister_union;
visitor.accept[IDL_ACCEPT_BITMASK] = ®ister_bitmask;
assert(pstate->sources);
sources[0] = pstate->sources->path->name;
visitor.sources = sources;
if ((ret = idl_visit(pstate, pstate->root, &visitor, generator)))
return ret;
for (include = source->includes; include; include = include->next) {
int cnt;
const char *file = include->file->name;
const char *ext = strrchr(file, '.');
if (ext && idl_strcasecmp(ext, ".idl") == 0) {
const char *fmt = "#include \"%.*s.hpp\"\n";
int len = (int)(ext - file);
cnt = idl_fprintf(generator->header.handle, fmt, len, file);
} else {
const char *fmt = "#include \"%s\"\n";
cnt = idl_fprintf(generator->header.handle, fmt, file);
}
if (cnt < 0 || fputs("\n", generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;
}
{ int len = 0;
const char *incs[12];
incs[len++] = "<utility>";
incs[len++] = "<ostream>";
if (generator->uses_integers)
incs[len++] = "<cstdint>";
if (generator->uses_array)
incs[len++] = generator->array_include;
if (generator->uses_sequence)
incs[len++] = generator->sequence_include;
if (generator->uses_bounded_sequence)
incs[len++] = generator->bounded_sequence_include;
if (generator->uses_string)
incs[len++] = generator->string_include;
if (generator->uses_bounded_string)
incs[len++] = generator->bounded_string_include;
if (generator->uses_union) {
incs[len++] = generator->union_include;
incs[len++] = "<dds/core/Exception.hpp>\n";
}
if (generator->uses_optional)
incs[len++] = generator->optional_include;
if (generator->uses_external)
incs[len++] = generator->external_include;
for (int i=0, j; i < len; i++) {
for (j=0; j < i && strcmp(incs[i], incs[j]) != 0; j++) ;
if (j < i)
continue;
const char *fmt = "#include %s\n";
if (idl_fprintf(generator->header.handle, fmt, incs[i]) < 0)
return IDL_RETCODE_NO_MEMORY;
}
}
if (fputs("\n", generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;
if (generator->uses_array || generator->uses_sequence || generator->uses_bounded_sequence || generator->uses_optional)
{
// ostream cpp
// streaming operators for the used std types
const char *fmt;
fmt = "#include <org/eclipse/cyclonedds/util/ostream_operators.hpp>\n\n";
if (fputs(fmt, generator->impl.handle) < 0)
return IDL_RETCODE_NO_MEMORY;
}
return IDL_RETCODE_OK;
}
#if _WIN32
__declspec(dllexport)
#endif
idl_retcode_t generate_nosetup(const idl_pstate_t *pstate, struct generator *gen)
{
idl_retcode_t ret;
char *guard;
if (!(guard = figure_guard(gen->header.path, pstate->digest)))
return IDL_RETCODE_NO_MEMORY;
if ((ret = print_header(gen->header.handle, gen->path, gen->header.path)))
goto err_print;
if ((ret = print_impl_header(gen->impl.handle, gen->path, gen->impl.path, gen->header.path)))
goto err_print;
if ((ret = print_guard_if(gen->header.handle, guard)))
goto err_print;
if ((ret = generate_includes(pstate, gen)))
goto err_print;
if ((ret = generate_types(pstate, gen)))
goto err_print;
if ((ret = generate_traits(pstate, gen)))
goto err_print;
if ((ret = generate_streamers(pstate, gen)))
goto err_print;
if ((ret = print_guard_endif(gen->header.handle, guard)))
goto err_print;
free(guard);
return IDL_RETCODE_OK;
err_print:
free(guard);
return ret;
}
const char *seq_tmpl = "std::vector<{TYPE}>";
const char *seq_inc = "<vector>";
const char *arr_tmpl = "std::array<{TYPE}, {DIMENSION}>";
const char *arr_inc = "<array>";
const char *bnd_seq_tmpl = "std::vector<{TYPE}>";
const char *bnd_seq_inc = "<vector>";
const char *str_tmpl = "std::string";
const char *str_inc = "<string>";
const char *bnd_str_tmpl = "std::string";
const char *bnd_str_inc = "<string>";
#if IDLCXX_USE_BOOST
const char *opt_tmpl = "boost::optional";
const char *opt_inc = "\"boost/optional.hpp\"";
const char *uni_tmpl = "boost::variant";
const char *uni_get_tmpl = "boost::get";
const char *uni_inc = "\"boost/variant.hpp\"";
#else
const char *opt_tmpl = "std::optional";
const char *opt_inc = "<optional>";
const char *uni_tmpl = "std::variant";
const char *uni_get_tmpl = "std::get";
const char *uni_inc = "<variant>";
#endif
const char *ext_tmpl = "dds::core::external";
const char *ext_inc = "<dds/core/External.hpp>";
static const char *arr_toks[] = { "TYPE", "DIMENSION", NULL };
static const char *arr_flags[] = { "s", PRIu32, NULL };
static const char *seq_toks[] = { "TYPE", NULL };
static const char *seq_flags[] = { "s", NULL };
static const char *bnd_seq_toks[] = { "TYPE", "BOUND", NULL };
static const char *bnd_seq_flags[] = { "s", PRIu32, NULL };
static const char *bnd_str_toks[] = { "BOUND", NULL };
static const char *bnd_str_flags[] = { PRIu32, NULL };
#if _WIN32
__declspec(dllexport)
#endif
idl_retcode_t generate(const idl_pstate_t *pstate, const idlc_generator_config_t *config)
{
idl_retcode_t ret = IDL_RETCODE_NO_MEMORY;
struct generator gen;
assert(pstate->paths);
assert(pstate->paths->name);
assert(config);
memset(&gen, 0, sizeof(gen));
gen.path = pstate->sources->path->name;
gen.config = config;
/* generate output filenames and open output files */
if (idl_generate_out_file(gen.path, config->output_dir, config->base_dir, "hpp", &gen.header.path, false) < 0 ||