blob: e21827e070b7684d4d7c5966cecd4bfb01a49df3 [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 */
Lev Walkinf15320b2004-06-03 03:38:44 +000020 OT_TYPE_DECLS, /* Type declarations */
21 OT_FUNC_DECLS, /* Function declarations */
Lev Walkinc8285712005-03-04 22:18:20 +000022 OT_POST_INCLUDE,/* #include after type definition */
Lev Walkin59004fa2004-08-20 13:37:01 +000023 OT_CTABLES, /* Constraint tables */
Lev Walkinf15320b2004-06-03 03:38:44 +000024 OT_CODE, /* Some code */
Lev Walkin725883b2006-10-09 12:07:58 +000025 OT_CTDEFS, /* Constraint definitions */
Lev Walkin59004fa2004-08-20 13:37:01 +000026 OT_STAT_DEFS, /* Static definitions */
Lev Walkinf15320b2004-06-03 03:38:44 +000027 OT_MAX
28 } target;
Lev Walkin59004fa2004-08-20 13:37:01 +000029
30 struct compiler_stream_destination_s {
31 TQ_HEAD(out_chunk_t) chunks;
32 int indent_level;
33 int indented;
34 } destination[OT_MAX];
Lev Walkinf15320b2004-06-03 03:38:44 +000035} compiler_streams_t;
36
37static char *_compiler_stream2str[] __attribute__ ((unused))
Lev Walkin725883b2006-10-09 12:07:58 +000038 = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" };
Lev Walkinf15320b2004-06-03 03:38:44 +000039
40int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
41
Lev Walkin3dcaafa2004-08-11 05:21:32 +000042
43/*****************************************************************
44 * Useful macros for invoking asn1c_compiled_output() and friends.
45 */
46
47/* Redirect output to a different stream. */
48#define REDIR(foo) do { arg->target->target = foo; } while(0)
Lev Walkin59004fa2004-08-20 13:37:01 +000049#define INDENT_LEVEL \
50 arg->target->destination[arg->target->target].indent_level
51#define INDENT(val) INDENT_LEVEL += (val)
52#define INDENTED(code) do { \
53 INDENT(+1); \
54 do { code; } while(0); \
55 INDENT(-1); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000056 } while(0)
57
Lev Walkin3dcaafa2004-08-11 05:21:32 +000058#define EMBED(ev) do { \
59 int saved_target = arg->target->target; \
60 REDIR(OT_TYPE_DECLS); \
61 arg->embed++; \
62 INDENTED(arg_t _tmp = *arg; \
63 _tmp.expr = ev; \
64 _tmp.default_cb(&_tmp); \
65 ); \
66 arg->embed--; \
Lev Walkin6fec44d2004-08-22 03:10:23 +000067 if(ev->expr_type != A1TC_EXTENSIBLE) \
68 OUT(";\n"); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000069 assert(arg->target->target == OT_TYPE_DECLS); \
70 REDIR(saved_target); \
71 } while(0)
72
73/* Output a piece of text into a default stream */
74#define OUT(fmt, args...) asn1c_compiled_output(arg, fmt, ##args)
Lev Walkin59004fa2004-08-20 13:37:01 +000075#define OUT_NOINDENT(fmt, args...) do { \
76 int _saved_indent = INDENT_LEVEL; \
77 INDENT_LEVEL = 0; \
78 OUT(fmt, ##args); \
79 INDENT_LEVEL = _saved_indent; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000080} while(0)
Lev Walkine0b56e02005-02-25 12:10:27 +000081#define OUT_DEBUG(fmt, args...) do { \
82 if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \
83 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000084
85/* Generate #include line */
Lev Walkin34944f22010-10-07 08:25:37 +000086#define GEN_INCLUDE_STD(typename) do { \
87 if((arg->flags & A1C_INCLUDES_QUOTED)) { \
88 GEN_INCLUDE("\"" typename ".h\""); \
89 } else { \
90 GEN_INCLUDE("<" typename ".h>"); \
91 } } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000092#define GEN_INCLUDE(filename) do { \
93 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000094 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000095 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000096 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000097 REDIR(saved_target); \
98} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +000099#define GEN_POSTINCLUDE(filename) do { \
100 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000101 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +0000102 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000103 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +0000104 REDIR(saved_target); \
105} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000106
107/* Generate ASN.1 type declaration */
108#define GEN_DECLARE(expr) do { \
109 int saved_target = arg->target->target; \
Lev Walkinc8285712005-03-04 22:18:20 +0000110 REDIR(OT_FUNC_DECLS); \
Lev Walkin05363a72004-09-29 13:16:40 +0000111 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
Lev Walkina00d6b32006-03-21 03:40:38 +0000112 "asn_DEF_%s;\n", MKID(expr)); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000113 REDIR(saved_target); \
114} while(0)
115
Lev Walkin63b41262007-11-06 01:48:46 +0000116/*
117 * Format LONG_MIN according to C90 rules.
118 */
119#define OINT(iv) do { \
120 if(iv == (-2147483647L - 1)) \
121 OUT("(-2147483647L - 1)"); \
122 else \
123 OUT("%" PRIdASN, iv); \
124} while(0)
125
126#define OINTS(iv) do { \
127 if(iv == (-2147483647L - 1)) \
128 OUT("(-2147483647L - 1)"); \
129 else \
130 OUT("% " PRIdASN, iv); \
131} while(0)
132
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700133#endif /* ASN1_COMPILED_OUTPUT_H */