blob: 04d7ff2f27a121018168021ef588b2eb3dd7156f [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#ifndef _ASN1_COMPILED_OUTPUT_H_
2#define _ASN1_COMPILED_OUTPUT_H_
3
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 Walkin22b5ed42006-09-13 02:51:20 +000086#define GEN_INCLUDE_STD(typename) GEN_INCLUDE("<" typename ".h>")
Lev Walkin3dcaafa2004-08-11 05:21:32 +000087#define GEN_INCLUDE(filename) do { \
88 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000089 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000090 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000091 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000092 REDIR(saved_target); \
93} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +000094#define GEN_POSTINCLUDE(filename) do { \
95 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000096 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +000097 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000098 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +000099 REDIR(saved_target); \
100} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000101
102/* Generate ASN.1 type declaration */
103#define GEN_DECLARE(expr) do { \
104 int saved_target = arg->target->target; \
Lev Walkinc8285712005-03-04 22:18:20 +0000105 REDIR(OT_FUNC_DECLS); \
Lev Walkin05363a72004-09-29 13:16:40 +0000106 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
Lev Walkina00d6b32006-03-21 03:40:38 +0000107 "asn_DEF_%s;\n", MKID(expr)); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000108 REDIR(saved_target); \
109} while(0)
110
Lev Walkin63b41262007-11-06 01:48:46 +0000111/*
112 * Format LONG_MIN according to C90 rules.
113 */
114#define OINT(iv) do { \
115 if(iv == (-2147483647L - 1)) \
116 OUT("(-2147483647L - 1)"); \
117 else \
118 OUT("%" PRIdASN, iv); \
119} while(0)
120
121#define OINTS(iv) do { \
122 if(iv == (-2147483647L - 1)) \
123 OUT("(-2147483647L - 1)"); \
124 else \
125 OUT("% " PRIdASN, iv); \
126} while(0)
127
Lev Walkinf15320b2004-06-03 03:38:44 +0000128#endif /* _ASN1_COMPILED_OUTPUT_H_ */