Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 1 | #ifndef ASN1_COMPILED_OUTPUT_H |
| 2 | #define ASN1_COMPILED_OUTPUT_H |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 { |
Lev Walkin | 61c7069 | 2004-09-08 00:28:47 +0000 | [diff] [blame] | 16 | OT_IGNORE, /* Ignore this output */ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 17 | OT_INCLUDES, /* #include files */ |
| 18 | OT_DEPS, /* Dependencies (other than #includes) */ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 19 | OT_FWD_DECLS, /* Forward declarations */ |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 20 | OT_FWD_DEFS, /* Forward definitions */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | OT_TYPE_DECLS, /* Type declarations */ |
| 22 | OT_FUNC_DECLS, /* Function declarations */ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 23 | OT_POST_INCLUDE,/* #include after type definition */ |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 24 | OT_IOC_TABLES, /* Information Object Class tables */ |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 25 | OT_CTABLES, /* Constraint tables */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | OT_CODE, /* Some code */ |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 27 | OT_CTDEFS, /* Constraint definitions */ |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 28 | OT_STAT_DEFS, /* Static definitions */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | OT_MAX |
| 30 | } target; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 31 | |
| 32 | struct compiler_stream_destination_s { |
| 33 | TQ_HEAD(out_chunk_t) chunks; |
| 34 | int indent_level; |
| 35 | int indented; |
| 36 | } destination[OT_MAX]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | } compiler_streams_t; |
| 38 | |
| 39 | static char *_compiler_stream2str[] __attribute__ ((unused)) |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 40 | = { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "FWD-DEFS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "IOC-TABLES", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" }; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | |
| 42 | int asn1c_compiled_output(arg_t *arg, const char *fmt, ...); |
| 43 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 44 | |
| 45 | /***************************************************************** |
| 46 | * Useful macros for invoking asn1c_compiled_output() and friends. |
| 47 | */ |
| 48 | |
| 49 | /* Redirect output to a different stream. */ |
| 50 | #define REDIR(foo) do { arg->target->target = foo; } while(0) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 51 | #define INDENT_LEVEL \ |
| 52 | arg->target->destination[arg->target->target].indent_level |
| 53 | #define INDENT(val) INDENT_LEVEL += (val) |
| 54 | #define INDENTED(code) do { \ |
| 55 | INDENT(+1); \ |
| 56 | do { code; } while(0); \ |
| 57 | INDENT(-1); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 58 | } while(0) |
| 59 | |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 60 | #define EMBED(ev) \ |
| 61 | do { \ |
| 62 | arg->embed++; \ |
| 63 | INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \ |
| 64 | _tmp.default_cb(&_tmp, NULL);); \ |
| 65 | arg->embed--; \ |
| 66 | if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \ |
| 67 | assert(arg->target->target == OT_TYPE_DECLS \ |
| 68 | || arg->target->target == OT_FWD_DEFS); \ |
| 69 | } while(0) |
| 70 | |
| 71 | #define EMBED_WITH_IOCT(ev, ioc) \ |
| 72 | do { \ |
| 73 | arg->embed++; \ |
| 74 | INDENTED(arg_t _tmp = *arg; _tmp.expr = ev; \ |
| 75 | _tmp.default_cb(&_tmp, ((ioc).ioct ? &ioc : 0));); \ |
| 76 | arg->embed--; \ |
| 77 | if(ev->expr_type != A1TC_EXTENSIBLE) OUT(";\n"); \ |
| 78 | assert(arg->target->target == OT_TYPE_DECLS \ |
| 79 | || arg->target->target == OT_FWD_DEFS); \ |
| 80 | } while(0) |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 81 | |
| 82 | /* Output a piece of text into a default stream */ |
| 83 | #define OUT(fmt, args...) asn1c_compiled_output(arg, fmt, ##args) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 84 | #define OUT_NOINDENT(fmt, args...) do { \ |
| 85 | int _saved_indent = INDENT_LEVEL; \ |
| 86 | INDENT_LEVEL = 0; \ |
| 87 | OUT(fmt, ##args); \ |
| 88 | INDENT_LEVEL = _saved_indent; \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 89 | } while(0) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 90 | #define OUT_DEBUG(fmt, args...) do { \ |
| 91 | if(arg->flags & A1C_DEBUG) OUT(fmt, ##args); \ |
| 92 | } while(0) |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 93 | |
| 94 | /* Generate #include line */ |
Lev Walkin | 34944f2 | 2010-10-07 08:25:37 +0000 | [diff] [blame] | 95 | #define GEN_INCLUDE_STD(typename) do { \ |
| 96 | if((arg->flags & A1C_INCLUDES_QUOTED)) { \ |
| 97 | GEN_INCLUDE("\"" typename ".h\""); \ |
| 98 | } else { \ |
| 99 | GEN_INCLUDE("<" typename ".h>"); \ |
| 100 | } } while(0) |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 101 | #define GEN_INCLUDE(filename) do { \ |
| 102 | int saved_target = arg->target->target; \ |
Lev Walkin | 0913f24 | 2006-03-06 00:30:30 +0000 | [diff] [blame] | 103 | if(!filename) break; \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 104 | REDIR(OT_INCLUDES); \ |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 105 | OUT_NOINDENT("#include %s\n", filename); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 106 | REDIR(saved_target); \ |
| 107 | } while(0) |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 108 | #define GEN_POSTINCLUDE(filename) do { \ |
| 109 | int saved_target = arg->target->target; \ |
Lev Walkin | 0913f24 | 2006-03-06 00:30:30 +0000 | [diff] [blame] | 110 | if(!filename) break; \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 111 | REDIR(OT_POST_INCLUDE); \ |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 112 | OUT_NOINDENT("#include %s\n", filename); \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 113 | REDIR(saved_target); \ |
| 114 | } while(0) |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 115 | |
| 116 | /* Generate ASN.1 type declaration */ |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 117 | #define GEN_DECLARE(type_name, expr) do { \ |
| 118 | int saved_target = arg->target->target; \ |
| 119 | REDIR(OT_FUNC_DECLS); \ |
| 120 | OUT_NOINDENT("extern asn_TYPE_descriptor_t " \ |
| 121 | "asn_DEF_%s;\n", MKID(expr)); \ |
| 122 | if (expr->_type_referenced) { \ |
| 123 | OUT_NOINDENT("extern asn_%s_specifics_t " \ |
| 124 | "asn_SPC_%s_specs_%d;\n", type_name, \ |
| 125 | MKID(expr), expr->_type_unique_index); \ |
| 126 | if(expr_elements_count(arg, expr)) \ |
| 127 | OUT_NOINDENT("extern asn_TYPE_member_t " \ |
| 128 | "asn_MBR_%s_%d[%d];\n", \ |
| 129 | MKID(expr), expr->_type_unique_index, \ |
| 130 | expr_elements_count(arg, expr)); \ |
| 131 | } \ |
| 132 | REDIR(saved_target); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 133 | } while(0) |
| 134 | |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Format LONG_MIN according to C90 rules. |
| 137 | */ |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 138 | #define OINT(iv) \ |
| 139 | do { \ |
| 140 | if(iv == (-2147483647L - 1)) \ |
| 141 | OUT("(-2147483647L - 1)"); \ |
| 142 | else \ |
| 143 | OUT("%s", asn1p_itoa(iv)); \ |
| 144 | } while(0) |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 145 | |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 146 | #define OINTS(iv) \ |
| 147 | do { \ |
| 148 | if(iv == (-2147483647L - 1)) \ |
| 149 | OUT("(-2147483647L - 1)"); \ |
| 150 | else \ |
| 151 | OUT("%s%s", (iv >= 0) ? " " : "", asn1p_itoa(iv)); \ |
| 152 | } while(0) |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 153 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 154 | #endif /* ASN1_COMPILED_OUTPUT_H */ |