Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2 | #include "asn1c_lang.h" |
| 3 | #include "asn1c_out.h" |
| 4 | #include "asn1c_save.h" |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 5 | #include "asn1c_ioc.h" |
Lev Walkin | 5efafc5 | 2017-09-05 03:43:00 -0700 | [diff] [blame] | 6 | #include "asn1c_naming.h" |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | |
| 8 | static void default_logger_cb(int, const char *fmt, ...); |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 9 | static int asn1c_compile_expr(arg_t *arg, const asn1c_ioc_table_and_objset_t *); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | static int asn1c_attach_streams(asn1p_expr_t *expr); |
Bi-Ruei, Chiu | 9b78083 | 2016-11-07 22:19:10 +0800 | [diff] [blame] | 11 | static int asn1c_detach_streams(asn1p_expr_t *expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | |
| 13 | int |
Jon Ringle | 6431b1c | 2017-11-15 01:22:02 -0500 | [diff] [blame] | 14 | asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags flags, |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 15 | int argc, int optc, char **argv) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | arg_t arg_s; |
| 17 | arg_t *arg = &arg_s; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 18 | asn1p_module_t *mod; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | int ret; |
| 20 | |
Lev Walkin | 5efafc5 | 2017-09-05 03:43:00 -0700 | [diff] [blame] | 21 | c_name_clash_finder_init(); |
| 22 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Initialize target language. |
| 25 | */ |
| 26 | ret = asn1c_with_language(ASN1C_LANGUAGE_C); |
| 27 | assert(ret == 0); |
| 28 | |
| 29 | memset(arg, 0, sizeof(*arg)); |
| 30 | arg->default_cb = asn1c_compile_expr; |
| 31 | arg->logger_cb = default_logger_cb; |
| 32 | arg->flags = flags; |
| 33 | arg->asn = asn; |
| 34 | |
| 35 | /* |
| 36 | * Compile each individual top level structure. |
| 37 | */ |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 38 | TQ_FOR(mod, &(asn->modules), mod_next) { |
| 39 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 40 | arg->ns = asn1_namespace_new_from_module(mod, 0); |
| 41 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | compiler_streams_t *cs = NULL; |
| 43 | |
| 44 | if(asn1c_attach_streams(arg->expr)) |
| 45 | return -1; |
| 46 | |
| 47 | cs = arg->expr->data; |
| 48 | cs->target = OT_TYPE_DECLS; |
| 49 | arg->target = cs; |
| 50 | |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 51 | ret = asn1c_compile_expr(arg, NULL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 52 | if(ret) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 53 | FATAL("Cannot compile \"%s\" (%x:%x) at line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | arg->expr->Identifier, |
| 55 | arg->expr->expr_type, |
| 56 | arg->expr->meta_type, |
| 57 | arg->expr->_lineno); |
| 58 | return ret; |
| 59 | } |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 60 | |
| 61 | asn1_namespace_free(arg->ns); |
| 62 | arg->ns = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Lev Walkin | 5efafc5 | 2017-09-05 03:43:00 -0700 | [diff] [blame] | 66 | if(c_name_clash(arg)) { |
| 67 | if(arg->flags & A1C_COMPOUND_NAMES) { |
| 68 | FATAL("Name clashes encountered even with -fcompound-names flag"); |
| 69 | /* Proceed further for better debugging. */ |
| 70 | } else { |
| 71 | FATAL("Use \"-fcompound-names\" flag to asn1c to resolve name clashes"); |
| 72 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 73 | /* Proceed further for better debugging. */ |
| 74 | } else { |
| 75 | return -1; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | DEBUG("Saving compiled data"); |
| 81 | |
Lev Walkin | 47fc74c | 2017-09-26 18:02:27 -0700 | [diff] [blame] | 82 | c_name_clash_finder_destroy(); |
| 83 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Save or print out the compiled result. |
| 86 | */ |
Jon Ringle | 6431b1c | 2017-11-15 01:22:02 -0500 | [diff] [blame] | 87 | if(asn1c_save_compiled_output(arg, datadir, destdir, argc, optc, argv)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | return -1; |
| 89 | |
Bi-Ruei, Chiu | 9b78083 | 2016-11-07 22:19:10 +0800 | [diff] [blame] | 90 | TQ_FOR(mod, &(asn->modules), mod_next) { |
| 91 | TQ_FOR(arg->expr, &(mod->members), next) { |
| 92 | asn1c_detach_streams(arg->expr); |
| 93 | } |
| 94 | } |
| 95 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 100 | asn1c_compile_expr(arg_t *arg, const asn1c_ioc_table_and_objset_t *opt_ioc) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | asn1p_expr_t *expr = arg->expr; |
| 102 | int (*type_cb)(arg_t *); |
| 103 | int ret; |
| 104 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 105 | assert((int)expr->meta_type >= AMT_INVALID); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | assert(expr->meta_type < AMT_EXPR_META_MAX); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 107 | assert((int)expr->expr_type >= A1TC_INVALID); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 108 | assert(expr->expr_type < ASN_EXPR_TYPE_MAX); |
| 109 | |
| 110 | type_cb = asn1_lang_map[expr->meta_type][expr->expr_type].type_cb; |
| 111 | if(type_cb) { |
| 112 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | DEBUG("Compiling %s at line %d", |
| 114 | expr->Identifier, |
| 115 | expr->_lineno); |
| 116 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 117 | if(expr->lhs_params && expr->spec_index == -1) { |
| 118 | int i; |
| 119 | ret = 0; |
| 120 | DEBUG("Parameterized type %s at line %d: %s (%d)", |
| 121 | expr->Identifier, expr->_lineno, |
| 122 | expr->specializations.pspecs_count |
Bi-Ruei, Chiu | 89cde74 | 2017-10-06 21:27:39 +0800 | [diff] [blame] | 123 | ? "compiling" : "unused, skipping", |
| 124 | expr->specializations.pspecs_count); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 125 | for(i = 0; i<expr->specializations.pspecs_count; i++) { |
| 126 | arg->expr = expr->specializations |
| 127 | .pspec[i].my_clone; |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 128 | ret = asn1c_compile_expr(arg, opt_ioc); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 129 | if(ret) break; |
| 130 | } |
| 131 | arg->expr = expr; /* Restore */ |
| 132 | } else { |
| 133 | ret = type_cb(arg); |
| 134 | if(arg->target->destination[OT_TYPE_DECLS] |
| 135 | .indent_level == 0) |
| 136 | OUT(";\n"); |
| 137 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | } else { |
| 139 | ret = -1; |
| 140 | /* |
| 141 | * Even if the target language compiler does not know |
| 142 | * how to compile the given expression, we know that |
| 143 | * certain expressions need not to be compiled at all. |
| 144 | */ |
| 145 | switch(expr->meta_type) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 146 | case AMT_OBJECT: |
Lev Walkin | 9c2285a | 2006-03-09 08:49:26 +0000 | [diff] [blame] | 147 | case AMT_OBJECTCLASS: |
| 148 | case AMT_OBJECTFIELD: |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | case AMT_VALUE: |
| 150 | case AMT_VALUESET: |
| 151 | ret = 0; |
| 152 | break; |
| 153 | default: |
| 154 | break; |
| 155 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if(ret == -1) { |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 159 | FATAL("Cannot compile \"%s\" (%x:%x) at line %d", |
| 160 | arg->expr->Identifier, |
| 161 | arg->expr->expr_type, |
| 162 | arg->expr->meta_type, |
| 163 | arg->expr->_lineno); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 164 | OUT("#error Cannot compile \"%s\" (%x/%x) at line %d\n", |
| 165 | arg->expr->Identifier, |
| 166 | arg->expr->meta_type, |
| 167 | arg->expr->expr_type, |
| 168 | arg->expr->_lineno |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | asn1c_attach_streams(asn1p_expr_t *expr) { |
| 177 | compiler_streams_t *cs; |
| 178 | int i; |
| 179 | |
| 180 | if(expr->data) |
| 181 | return 0; /* Already attached? */ |
| 182 | |
| 183 | expr->data = calloc(1, sizeof(compiler_streams_t)); |
| 184 | if(expr->data == NULL) |
| 185 | return -1; |
| 186 | |
| 187 | cs = expr->data; |
| 188 | for(i = 0; i < OT_MAX; i++) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 189 | TQ_INIT(&(cs->destination[i].chunks)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Bi-Ruei, Chiu | 9b78083 | 2016-11-07 22:19:10 +0800 | [diff] [blame] | 195 | int |
| 196 | asn1c_detach_streams(asn1p_expr_t *expr) { |
| 197 | compiler_streams_t *cs; |
| 198 | out_chunk_t *m; |
| 199 | int i; |
| 200 | |
| 201 | if(!expr->data) |
| 202 | return 0; /* Already detached? */ |
| 203 | |
| 204 | cs = expr->data; |
| 205 | for(i = 0; i < OT_MAX; i++) { |
| 206 | while((m = TQ_REMOVE(&(cs->destination[i].chunks), next))) { |
| 207 | free(m->buf); |
| 208 | free(m); |
| 209 | } |
| 210 | } |
| 211 | free(expr->data); |
| 212 | expr->data = (void *)NULL; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | static void |
| 218 | default_logger_cb(int _severity, const char *fmt, ...) { |
| 219 | va_list ap; |
| 220 | char *pfx = ""; |
| 221 | |
| 222 | switch(_severity) { |
| 223 | case -1: pfx = "DEBUG: "; break; |
| 224 | case 0: pfx = "WARNING: "; break; |
| 225 | case 1: pfx = "FATAL: "; break; |
| 226 | } |
| 227 | |
| 228 | fprintf(stderr, "%s", pfx); |
| 229 | va_start(ap, fmt); |
| 230 | vfprintf(stderr, fmt, ap); |
| 231 | va_end(ap); |
| 232 | fprintf(stderr, "\n"); |
| 233 | } |
| 234 | |
Lev Walkin | 4215c51 | 2017-11-19 19:01:26 -0800 | [diff] [blame] | 235 | static void |
| 236 | asn1c_debug_expr_naming(arg_t *arg) { |
| 237 | asn1p_expr_t *expr = arg->expr; |
| 238 | |
| 239 | printf("%s: ", expr->Identifier); |
| 240 | printf("%s\n", c_names_format(c_name(arg))); |
| 241 | |
| 242 | printf("\n"); |
| 243 | |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | asn1c_debug_type_naming(asn1p_t *asn, enum asn1c_flags flags, |
| 248 | char **asn_type_names) { |
| 249 | arg_t arg_s; |
| 250 | arg_t *arg = &arg_s; |
| 251 | asn1p_module_t *mod; |
| 252 | |
| 253 | memset(arg, 0, sizeof(*arg)); |
| 254 | arg->logger_cb = default_logger_cb; |
| 255 | arg->flags = flags; |
| 256 | arg->asn = asn; |
| 257 | |
| 258 | c_name_clash_finder_init(); |
| 259 | |
| 260 | /* |
| 261 | * Compile each individual top level structure. |
| 262 | */ |
| 263 | TQ_FOR(mod, &(asn->modules), mod_next) { |
| 264 | int namespace_shown = 0; |
| 265 | TQ_FOR(arg->expr, &(mod->members), next) { |
| 266 | arg->ns = asn1_namespace_new_from_module(mod, 0); |
| 267 | |
| 268 | for(char **t = asn_type_names; *t; t++) { |
| 269 | if(strcmp(*t, arg->expr->Identifier) == 0) { |
| 270 | if(!namespace_shown) { |
| 271 | namespace_shown = 1; |
| 272 | printf("Namespace %s\n", |
| 273 | asn1_namespace_string(arg->ns)); |
| 274 | } |
| 275 | asn1c_debug_expr_naming(arg); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | asn1_namespace_free(arg->ns); |
| 280 | arg->ns = 0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | c_name_clash_finder_destroy(); |
| 285 | } |
| 286 | |