blob: 41077dfb37c00a655dd3d5fb6958711e0638f3ff [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
Lev Walkin840fb8e2017-11-19 23:39:59 -080042int asn1c_compiled_output(arg_t *arg, const char *file, int lineno,
43 const char *func, const char *fmt, ...)
44 __attribute__((format(printf, 5, 6)));
Lev Walkinf15320b2004-06-03 03:38:44 +000045
Lev Walkin3dcaafa2004-08-11 05:21:32 +000046
47/*****************************************************************
48 * Useful macros for invoking asn1c_compiled_output() and friends.
49 */
50
51/* Redirect output to a different stream. */
52#define REDIR(foo) do { arg->target->target = foo; } while(0)
Lev Walkin59004fa2004-08-20 13:37:01 +000053#define INDENT_LEVEL \
54 arg->target->destination[arg->target->target].indent_level
55#define INDENT(val) INDENT_LEVEL += (val)
56#define INDENTED(code) do { \
57 INDENT(+1); \
58 do { code; } while(0); \
59 INDENT(-1); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000060 } while(0)
61
Lev Walkin9de6cd82017-08-10 05:47:46 -070062#define EMBED(ev) \
63 do { \
64 arg->embed++; \
65 INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \
66 _tmp.default_cb(&_tmp, NULL);); \
67 arg->embed--; \
68 if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \
69 assert(arg->target->target == OT_TYPE_DECLS \
70 || arg->target->target == OT_FWD_DEFS); \
71 } while(0)
72
73#define EMBED_WITH_IOCT(ev, ioc) \
74 do { \
75 arg->embed++; \
76 INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \
77 _tmp.default_cb(&_tmp, ((ioc).ioct ? &ioc : 0));); \
78 arg->embed--; \
79 if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \
80 assert(arg->target->target == OT_TYPE_DECLS \
81 || arg->target->target == OT_FWD_DEFS); \
82 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000083
84/* Output a piece of text into a default stream */
Lev Walkin840fb8e2017-11-19 23:39:59 -080085#define OUT(fmt, args...) \
86 asn1c_compiled_output(arg, __FILE__, __LINE__, __func__, fmt, ##args)
Lev Walkin59004fa2004-08-20 13:37:01 +000087#define OUT_NOINDENT(fmt, args...) do { \
88 int _saved_indent = INDENT_LEVEL; \
89 INDENT_LEVEL = 0; \
90 OUT(fmt, ##args); \
91 INDENT_LEVEL = _saved_indent; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000092} while(0)
Lev Walkine0b56e02005-02-25 12:10:27 +000093#define OUT_DEBUG(fmt, args...) do { \
94 if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \
95 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000096
97/* Generate #include line */
Lev Walkin34944f22010-10-07 08:25:37 +000098#define GEN_INCLUDE_STD(typename) do { \
99 if((arg->flags & A1C_INCLUDES_QUOTED)) { \
100 GEN_INCLUDE("\"" typename ".h\""); \
101 } else { \
102 GEN_INCLUDE("<" typename ".h>"); \
103 } } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000104#define GEN_INCLUDE(filename) do { \
105 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000106 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000107 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000108 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000109 REDIR(saved_target); \
110} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +0000111#define GEN_POSTINCLUDE(filename) do { \
112 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000113 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +0000114 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000115 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +0000116 REDIR(saved_target); \
117} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000118
119/* Generate ASN.1 type declaration */
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800120#define GEN_DECLARE(type_name, expr) do { \
121 int saved_target = arg->target->target; \
122 REDIR(OT_FUNC_DECLS); \
123 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
124 "asn_DEF_%s;\n", MKID(expr)); \
125 if (expr->_type_referenced) { \
126 OUT_NOINDENT("extern asn_%s_specifics_t " \
127 "asn_SPC_%s_specs_%d;\n", type_name, \
128 MKID(expr), expr->_type_unique_index); \
129 if(expr_elements_count(arg, expr)) \
130 OUT_NOINDENT("extern asn_TYPE_member_t " \
131 "asn_MBR_%s_%d[%d];\n", \
132 MKID(expr), expr->_type_unique_index, \
133 expr_elements_count(arg, expr)); \
134 } \
135 REDIR(saved_target); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000136} while(0)
137
Lev Walkin63b41262007-11-06 01:48:46 +0000138/*
139 * Format LONG_MIN according to C90 rules.
140 */
Lev Walkinda997b12017-08-04 01:38:41 -0700141#define OINT(iv) \
142 do { \
143 if(iv == (-2147483647L - 1)) \
144 OUT("(-2147483647L - 1)"); \
145 else \
146 OUT("%s", asn1p_itoa(iv)); \
147 } while(0)
Lev Walkin63b41262007-11-06 01:48:46 +0000148
Lev Walkinda997b12017-08-04 01:38:41 -0700149#define OINTS(iv) \
150 do { \
151 if(iv == (-2147483647L - 1)) \
152 OUT("(-2147483647L - 1)"); \
153 else \
154 OUT("%s%s", (iv >= 0) ? " " : "", asn1p_itoa(iv)); \
155 } while(0)
Lev Walkin63b41262007-11-06 01:48:46 +0000156
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700157#endif /* ASN1_COMPILED_OUTPUT_H */