blob: 47762a8d8e8a4bb21d1f4b4b00baf5473b3f7f9f [file] [log] [blame]
Lev Walkinc6cac8e2016-03-14 02:57:07 -07001#ifndef ASN1_COMPILED_OUTPUT_H
2#define ASN1_COMPILED_OUTPUT_H
Lev Walkinf15320b2004-06-03 03:38:44 +00003
4/*
5 * An elementary chunk of target language text.
6 */
7typedef struct out_chunk {
8 char *buf;
9 int len;
10
11 TQ_ENTRY(struct out_chunk) next;
12} out_chunk_t;
13
14typedef struct compiler_streams {
15 enum {
Lev Walkin61c70692004-09-08 00:28:47 +000016 OT_IGNORE, /* Ignore this output */
Lev Walkin3dcaafa2004-08-11 05:21:32 +000017 OT_INCLUDES, /* #include files */
18 OT_DEPS, /* Dependencies (other than #includes) */
Lev Walkinc8285712005-03-04 22:18:20 +000019 OT_FWD_DECLS, /* Forward declarations */
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +080020 OT_FWD_DEFS, /* Forward definitions */
Lev Walkinf15320b2004-06-03 03:38:44 +000021 OT_TYPE_DECLS, /* Type declarations */
22 OT_FUNC_DECLS, /* Function declarations */
Lev Walkinc8285712005-03-04 22:18:20 +000023 OT_POST_INCLUDE,/* #include after type definition */
Lev Walkin9de6cd82017-08-10 05:47:46 -070024 OT_IOC_TABLES, /* Information Object Class tables */
Lev Walkin59004fa2004-08-20 13:37:01 +000025 OT_CTABLES, /* Constraint tables */
Lev Walkinf15320b2004-06-03 03:38:44 +000026 OT_CODE, /* Some code */
Lev Walkin725883b2006-10-09 12:07:58 +000027 OT_CTDEFS, /* Constraint definitions */
Lev Walkin59004fa2004-08-20 13:37:01 +000028 OT_STAT_DEFS, /* Static definitions */
Lev Walkinf15320b2004-06-03 03:38:44 +000029 OT_MAX
30 } target;
Lev Walkin59004fa2004-08-20 13:37:01 +000031
32 struct compiler_stream_destination_s {
33 TQ_HEAD(out_chunk_t) chunks;
34 int indent_level;
35 int indented;
36 } destination[OT_MAX];
Lev Walkinf15320b2004-06-03 03:38:44 +000037} compiler_streams_t;
38
39static char *_compiler_stream2str[] __attribute__ ((unused))
Lev Walkin9de6cd82017-08-10 05:47:46 -070040 = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "FWD-DEFS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "IOC-TABLES", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" };
Lev Walkinf15320b2004-06-03 03:38:44 +000041
42int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
43
Lev Walkin3dcaafa2004-08-11 05:21:32 +000044
45/*****************************************************************
46 * Useful macros for invoking asn1c_compiled_output() and friends.
47 */
48
49/* Redirect output to a different stream. */
50#define REDIR(foo) do { arg->target->target = foo; } while(0)
Lev Walkin59004fa2004-08-20 13:37:01 +000051#define INDENT_LEVEL \
52 arg->target->destination[arg->target->target].indent_level
53#define INDENT(val) INDENT_LEVEL += (val)
54#define INDENTED(code) do { \
55 INDENT(+1); \
56 do { code; } while(0); \
57 INDENT(-1); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000058 } while(0)
59
Lev Walkin9de6cd82017-08-10 05:47:46 -070060#define EMBED(ev) \
61 do { \
62 arg->embed++; \
63 INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \
64 _tmp.default_cb(&_tmp, NULL);); \
65 arg->embed--; \
66 if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \
67 assert(arg->target->target == OT_TYPE_DECLS \
68 || arg->target->target == OT_FWD_DEFS); \
69 } while(0)
70
71#define EMBED_WITH_IOCT(ev, ioc) \
72 do { \
73 arg->embed++; \
74 INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \
75 _tmp.default_cb(&_tmp, ((ioc).ioct ? &ioc : 0));); \
76 arg->embed--; \
77 if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \
78 assert(arg->target->target == OT_TYPE_DECLS \
79 || arg->target->target == OT_FWD_DEFS); \
80 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000081
82/* Output a piece of text into a default stream */
83#define OUT(fmt, args...) asn1c_compiled_output(arg, fmt, ##args)
Lev Walkin59004fa2004-08-20 13:37:01 +000084#define OUT_NOINDENT(fmt, args...) do { \
85 int _saved_indent = INDENT_LEVEL; \
86 INDENT_LEVEL = 0; \
87 OUT(fmt, ##args); \
88 INDENT_LEVEL = _saved_indent; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000089} while(0)
Lev Walkine0b56e02005-02-25 12:10:27 +000090#define OUT_DEBUG(fmt, args...) do { \
91 if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \
92 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000093
94/* Generate #include line */
Lev Walkin34944f22010-10-07 08:25:37 +000095#define GEN_INCLUDE_STD(typename) do { \
96 if((arg->flags & A1C_INCLUDES_QUOTED)) { \
97 GEN_INCLUDE("\"" typename ".h\""); \
98 } else { \
99 GEN_INCLUDE("<" typename ".h>"); \
100 } } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000101#define GEN_INCLUDE(filename) do { \
102 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000103 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000104 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000105 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000106 REDIR(saved_target); \
107} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +0000108#define GEN_POSTINCLUDE(filename) do { \
109 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000110 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +0000111 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000112 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +0000113 REDIR(saved_target); \
114} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000115
116/* Generate ASN.1 type declaration */
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800117#define GEN_DECLARE(type_name, expr) do { \
118 int saved_target = arg->target->target; \
119 REDIR(OT_FUNC_DECLS); \
120 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
121 "asn_DEF_%s;\n", MKID(expr)); \
122 if (expr->_type_referenced) { \
123 OUT_NOINDENT("extern asn_%s_specifics_t " \
124 "asn_SPC_%s_specs_%d;\n", type_name, \
125 MKID(expr), expr->_type_unique_index); \
126 if(expr_elements_count(arg, expr)) \
127 OUT_NOINDENT("extern asn_TYPE_member_t " \
128 "asn_MBR_%s_%d[%d];\n", \
129 MKID(expr), expr->_type_unique_index, \
130 expr_elements_count(arg, expr)); \
131 } \
132 REDIR(saved_target); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000133} while(0)
134
Lev Walkin63b41262007-11-06 01:48:46 +0000135/*
136 * Format LONG_MIN according to C90 rules.
137 */
Lev Walkinda997b12017-08-04 01:38:41 -0700138#define OINT(iv) \
139 do { \
140 if(iv == (-2147483647L - 1)) \
141 OUT("(-2147483647L - 1)"); \
142 else \
143 OUT("%s", asn1p_itoa(iv)); \
144 } while(0)
Lev Walkin63b41262007-11-06 01:48:46 +0000145
Lev Walkinda997b12017-08-04 01:38:41 -0700146#define OINTS(iv) \
147 do { \
148 if(iv == (-2147483647L - 1)) \
149 OUT("(-2147483647L - 1)"); \
150 else \
151 OUT("%s%s", (iv >= 0) ? " " : "", asn1p_itoa(iv)); \
152 } while(0)
Lev Walkin63b41262007-11-06 01:48:46 +0000153
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700154#endif /* ASN1_COMPILED_OUTPUT_H */