blob: 3b1f055d7a1abcb361e6ae3bc506847f398bb2fd [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 Walkin59004fa2004-08-20 13:37:01 +000025 OT_STAT_DEFS, /* Static definitions */
Lev Walkinf15320b2004-06-03 03:38:44 +000026 OT_MAX
27 } target;
Lev Walkin59004fa2004-08-20 13:37:01 +000028
29 struct compiler_stream_destination_s {
30 TQ_HEAD(out_chunk_t) chunks;
31 int indent_level;
32 int indented;
33 } destination[OT_MAX];
Lev Walkinf15320b2004-06-03 03:38:44 +000034} compiler_streams_t;
35
36static char *_compiler_stream2str[] __attribute__ ((unused))
Lev Walkinc8285712005-03-04 22:18:20 +000037 = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "STAT-DEFS" };
Lev Walkinf15320b2004-06-03 03:38:44 +000038
39int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
40
Lev Walkin3dcaafa2004-08-11 05:21:32 +000041
42/*****************************************************************
43 * Useful macros for invoking asn1c_compiled_output() and friends.
44 */
45
46/* Redirect output to a different stream. */
47#define REDIR(foo) do { arg->target->target = foo; } while(0)
Lev Walkin59004fa2004-08-20 13:37:01 +000048#define INDENT_LEVEL \
49 arg->target->destination[arg->target->target].indent_level
50#define INDENT(val) INDENT_LEVEL += (val)
51#define INDENTED(code) do { \
52 INDENT(+1); \
53 do { code; } while(0); \
54 INDENT(-1); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000055 } while(0)
56
Lev Walkin3dcaafa2004-08-11 05:21:32 +000057#define EMBED(ev) do { \
58 int saved_target = arg->target->target; \
59 REDIR(OT_TYPE_DECLS); \
60 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"); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000068 assert(arg->target->target == OT_TYPE_DECLS); \
69 REDIR(saved_target); \
70 } 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 Walkin22b5ed42006-09-13 02:51:20 +000085#define GEN_INCLUDE_STD(typename) GEN_INCLUDE("<" typename ".h>")
Lev Walkin3dcaafa2004-08-11 05:21:32 +000086#define GEN_INCLUDE(filename) do { \
87 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000088 if(!filename) break; \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000089 REDIR(OT_INCLUDES); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000090 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000091 REDIR(saved_target); \
92} while(0)
Lev Walkinc8285712005-03-04 22:18:20 +000093#define GEN_POSTINCLUDE(filename) do { \
94 int saved_target = arg->target->target; \
Lev Walkin0913f242006-03-06 00:30:30 +000095 if(!filename) break; \
Lev Walkinc8285712005-03-04 22:18:20 +000096 REDIR(OT_POST_INCLUDE); \
Lev Walkin22b5ed42006-09-13 02:51:20 +000097 OUT_NOINDENT("#include %s\n", filename); \
Lev Walkinc8285712005-03-04 22:18:20 +000098 REDIR(saved_target); \
99} while(0)
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000100
101/* Generate ASN.1 type declaration */
102#define GEN_DECLARE(expr) do { \
103 int saved_target = arg->target->target; \
Lev Walkinc8285712005-03-04 22:18:20 +0000104 REDIR(OT_FUNC_DECLS); \
Lev Walkin05363a72004-09-29 13:16:40 +0000105 OUT_NOINDENT("extern asn_TYPE_descriptor_t " \
Lev Walkina00d6b32006-03-21 03:40:38 +0000106 "asn_DEF_%s;\n", MKID(expr)); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000107 REDIR(saved_target); \
108} while(0)
109
Lev Walkinf15320b2004-06-03 03:38:44 +0000110#endif /* _ASN1_COMPILED_OUTPUT_H_ */