vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #ifndef _ASN1_COMPILED_OUTPUT_H_ |
| 2 | #define _ASN1_COMPILED_OUTPUT_H_ |
| 3 | |
| 4 | /* |
| 5 | * An elementary chunk of target language text. |
| 6 | */ |
| 7 | typedef struct out_chunk { |
| 8 | char *buf; |
| 9 | int len; |
| 10 | |
| 11 | TQ_ENTRY(struct out_chunk) next; |
| 12 | } out_chunk_t; |
| 13 | |
| 14 | typedef struct compiler_streams { |
| 15 | enum { |
vlm | 9cb7877 | 2004-09-08 00:28:47 +0000 | [diff] [blame] | 16 | OT_IGNORE, /* Ignore this output */ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 17 | OT_INCLUDES, /* #include files */ |
| 18 | OT_DEPS, /* Dependencies (other than #includes) */ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 19 | OT_FWD_DECLS, /* Forward declarations */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | OT_TYPE_DECLS, /* Type declarations */ |
| 21 | OT_FUNC_DECLS, /* Function declarations */ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 22 | OT_POST_INCLUDE,/* #include after type definition */ |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 23 | OT_CTABLES, /* Constraint tables */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | OT_CODE, /* Some code */ |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 25 | OT_STAT_DEFS, /* Static definitions */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | OT_MAX |
| 27 | } target; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 28 | |
| 29 | struct compiler_stream_destination_s { |
| 30 | TQ_HEAD(out_chunk_t) chunks; |
| 31 | int indent_level; |
| 32 | int indented; |
| 33 | } destination[OT_MAX]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | } compiler_streams_t; |
| 35 | |
| 36 | static char *_compiler_stream2str[] __attribute__ ((unused)) |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 37 | = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "CTABLES", "CODE", "STAT-DEFS" }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | |
| 39 | int asn1c_compiled_output(arg_t *arg, const char *fmt, ...); |
| 40 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 41 | |
| 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) |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 48 | #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); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 55 | } while(0) |
| 56 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 57 | #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--; \ |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 66 | if(ev->expr_type != A1TC_EXTENSIBLE) \ |
| 67 | OUT(";\n"); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 68 | 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) |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 74 | #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; \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 79 | } while(0) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 80 | #define OUT_DEBUG(fmt, args...) do { \ |
| 81 | if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \ |
| 82 | } while(0) |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 83 | |
| 84 | /* Generate #include line */ |
vlm | ea22677 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 85 | #define GEN_INCLUDE_STD(typename) GEN_INCLUDE("<" typename ".h>") |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 86 | #define GEN_INCLUDE(filename) do { \ |
| 87 | int saved_target = arg->target->target; \ |
vlm | fe03aba | 2006-03-06 00:30:30 +0000 | [diff] [blame] | 88 | if(!filename) break; \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 89 | REDIR(OT_INCLUDES); \ |
vlm | ea22677 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 90 | OUT_NOINDENT("#include %s\n", filename); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 91 | REDIR(saved_target); \ |
| 92 | } while(0) |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 93 | #define GEN_POSTINCLUDE(filename) do { \ |
| 94 | int saved_target = arg->target->target; \ |
vlm | fe03aba | 2006-03-06 00:30:30 +0000 | [diff] [blame] | 95 | if(!filename) break; \ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 96 | REDIR(OT_POST_INCLUDE); \ |
vlm | ea22677 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 97 | OUT_NOINDENT("#include %s\n", filename); \ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 98 | REDIR(saved_target); \ |
| 99 | } while(0) |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 100 | |
| 101 | /* Generate ASN.1 type declaration */ |
| 102 | #define GEN_DECLARE(expr) do { \ |
| 103 | int saved_target = arg->target->target; \ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 104 | REDIR(OT_FUNC_DECLS); \ |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 105 | OUT_NOINDENT("extern asn_TYPE_descriptor_t " \ |
vlm | 0c6d381 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 106 | "asn_DEF_%s;\n", MKID(expr)); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 107 | REDIR(saved_target); \ |
| 108 | } while(0) |
| 109 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | #endif /* _ASN1_COMPILED_OUTPUT_H_ */ |