blob: 3abe70171bb72aadbf12dd7e735958aeaff67025 [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 Walkin59004fa2004-08-20 13:37:01 +000024 OT_CTABLES, /* Constraint tables */
Lev Walkinf15320b2004-06-03 03:38:44 +000025 OT_CODE, /* Some code */
Lev Walkin725883b2006-10-09 12:07:58 +000026 OT_CTDEFS, /* Constraint definitions */
Lev Walkin59004fa2004-08-20 13:37:01 +000027 OT_STAT_DEFS, /* Static definitions */
Lev Walkinf15320b2004-06-03 03:38:44 +000028 OT_MAX
29 } target;
Lev Walkin59004fa2004-08-20 13:37:01 +000030
31 struct compiler_stream_destination_s {
32 TQ_HEAD(out_chunk_t) chunks;
33 int indent_level;
34 int indented;
35 } destination[OT_MAX];
Lev Walkinf15320b2004-06-03 03:38:44 +000036} compiler_streams_t;
37
38static char *_compiler_stream2str[] __attribute__ ((unused))
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +080039 = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "FWD-DEFS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" };
Lev Walkinf15320b2004-06-03 03:38:44 +000040
41int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
42
Lev Walkin3dcaafa2004-08-11 05:21:32 +000043
44/*****************************************************************
45 * Useful macros for invoking asn1c_compiled_output() and friends.
46 */
47
48/* Redirect output to a different stream. */
49#define REDIR(foo) do { arg->target->target = foo; } while(0)
Lev Walkin59004fa2004-08-20 13:37:01 +000050#define INDENT_LEVEL \
51 arg->target->destination[arg->target->target].indent_level
52#define INDENT(val) INDENT_LEVEL += (val)
53#define INDENTED(code) do { \
54 INDENT(+1); \
55 do { code; } while(0); \
56 INDENT(-1); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000057 } while(0)
58
Lev Walkin3dcaafa2004-08-11 05:21:32 +000059#define EMBED(ev) do { \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000060 arg->embed++; \
61 INDENTED(arg_t _tmp = *arg; \
62 _tmp.expr = ev; \
63 _tmp.default_cb(&_tmp); \
64 ); \
65 arg->embed--; \
Lev Walkin6fec44d2004-08-22 03:10:23 +000066 if(ev->expr_type != A1TC_EXTENSIBLE) \
67 OUT(";\n"); \
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +080068 assert(arg->target->target == OT_TYPE_DECLS || \
69 arg->target->target == OT_FWD_DEFS); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000070 } while(0)
71
72/* Output a piece of text into a default stream */
73#define OUT(fmt, args...) asn1c_compiled_output(arg, fmt, ##args)
Lev Walkin59004fa2004-08-20 13:37:01 +000074#define OUT_NOINDENT(fmt, args...) do { \
75 int _saved_indent = INDENT_LEVEL; \
76 INDENT_LEVEL = 0; \
77 OUT(fmt, ##args); \
78 INDENT_LEVEL = _saved_indent; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000079} while(0)
Lev Walkine0b56e02005-02-25 12:10:27 +000080#define OUT_DEBUG(fmt, args...) do { \
81 if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \
82 } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000083
84/* Generate #include line */
Lev Walkin34944f22010-10-07 08:25:37 +000085#define GEN_INCLUDE_STD(typename) do { \
86 if((arg->flags & A1C_INCLUDES_QUOTED)) { \
87 GEN_INCLUDE("\"" typename ".h\""); \
88 } else { \
89 GEN_INCLUDE("<" typename ".h>"); \
90 } } while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +000091#define GEN_INCLUDE(filename) do { \
92 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000093 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000094 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000095 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000096 REDIR(saved_target); \
97} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +000098#define GEN_POSTINCLUDE(filename) do { \
99 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +0000100 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +0000101 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +0000102 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +0000103 REDIR(saved_target); \
104} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000105
106/* Generate ASN.1 type declaration */
107#define GEN_DECLARE(expr) do { \
108 int saved_target = arg->target->target; \
Lev Walkinc8285712005-03-04 22:18:20 +0000109 REDIR(OT_FUNC_DECLS); \
Lev Walkin05363a72004-09-29 13:16:40 +0000110 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
Lev Walkina00d6b32006-03-21 03:40:38 +0000111 "asn_DEF_%s;\n", MKID(expr)); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000112 REDIR(saved_target); \
113} while(0)
114
Lev Walkin63b41262007-11-06 01:48:46 +0000115/*
116 * Format LONG_MIN according to C90 rules.
117 */
118#define OINT(iv) do { \
119 if(iv == (-2147483647L - 1)) \
120 OUT("(-2147483647L - 1)"); \
121 else \
122 OUT("%" PRIdASN, iv); \
123} while(0)
124
125#define OINTS(iv) do { \
126 if(iv == (-2147483647L - 1)) \
127 OUT("(-2147483647L - 1)"); \
128 else \
129 OUT("% " PRIdASN, iv); \
130} while(0)
131
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700132#endif /* ASN1_COMPILED_OUTPUT_H */