vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2 | #include "asn1c_lang.h" |
| 3 | #include "asn1c_out.h" |
| 4 | #include "asn1c_save.h" |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 5 | |
| 6 | static void default_logger_cb(int, const char *fmt, ...); |
| 7 | static int asn1c_compile_expr(arg_t *arg); |
| 8 | static int asn1c_attach_streams(asn1p_expr_t *expr); |
| 9 | |
| 10 | int |
vlm | 2c874fe | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 11 | asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags, |
| 12 | int argc, char **argv) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | arg_t arg_s; |
| 14 | arg_t *arg = &arg_s; |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 15 | asn1p_module_t *mod; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | int ret; |
| 17 | |
| 18 | /* |
| 19 | * Initialize target language. |
| 20 | */ |
| 21 | ret = asn1c_with_language(ASN1C_LANGUAGE_C); |
| 22 | assert(ret == 0); |
| 23 | |
| 24 | memset(arg, 0, sizeof(*arg)); |
| 25 | arg->default_cb = asn1c_compile_expr; |
| 26 | arg->logger_cb = default_logger_cb; |
| 27 | arg->flags = flags; |
| 28 | arg->asn = asn; |
| 29 | |
| 30 | /* |
| 31 | * Compile each individual top level structure. |
| 32 | */ |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 33 | TQ_FOR(mod, &(asn->modules), mod_next) { |
| 34 | TQ_FOR(arg->expr, &(mod->members), next) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | compiler_streams_t *cs = NULL; |
| 36 | |
| 37 | if(asn1c_attach_streams(arg->expr)) |
| 38 | return -1; |
| 39 | |
| 40 | cs = arg->expr->data; |
| 41 | cs->target = OT_TYPE_DECLS; |
| 42 | arg->target = cs; |
| 43 | |
| 44 | ret = asn1c_compile_expr(arg); |
| 45 | if(ret) { |
| 46 | FATAL("Cannot compile %s (%x:%x) at line %d", |
| 47 | arg->expr->Identifier, |
| 48 | arg->expr->expr_type, |
| 49 | arg->expr->meta_type, |
| 50 | arg->expr->_lineno); |
| 51 | return ret; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | DEBUG("Saving compiled data"); |
| 57 | |
| 58 | /* |
| 59 | * Save or print out the compiled result. |
| 60 | */ |
vlm | 2c874fe | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 61 | if(asn1c_save_compiled_output(arg, datadir, argc, argv)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | return -1; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | asn1c_compile_expr(arg_t *arg) { |
| 69 | asn1p_expr_t *expr = arg->expr; |
| 70 | int (*type_cb)(arg_t *); |
| 71 | int ret; |
| 72 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 73 | assert((int)expr->meta_type >= AMT_INVALID); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | assert(expr->meta_type < AMT_EXPR_META_MAX); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 75 | assert((int)expr->expr_type >= A1TC_INVALID); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | assert(expr->expr_type < ASN_EXPR_TYPE_MAX); |
| 77 | |
| 78 | type_cb = asn1_lang_map[expr->meta_type][expr->expr_type].type_cb; |
| 79 | if(type_cb) { |
| 80 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | DEBUG("Compiling %s at line %d", |
| 82 | expr->Identifier, |
| 83 | expr->_lineno); |
| 84 | |
| 85 | ret = type_cb(arg); |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 86 | |
| 87 | if(arg->target->destination[OT_TYPE_DECLS].indent_level == 0) |
| 88 | OUT(";\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | } else { |
| 90 | ret = -1; |
| 91 | /* |
| 92 | * Even if the target language compiler does not know |
| 93 | * how to compile the given expression, we know that |
| 94 | * certain expressions need not to be compiled at all. |
| 95 | */ |
| 96 | switch(expr->meta_type) { |
| 97 | case AMT_PARAMTYPE: |
| 98 | case AMT_OBJECT: |
| 99 | case AMT_OBJECTSET: |
| 100 | case AMT_VALUE: |
| 101 | case AMT_VALUESET: |
| 102 | ret = 0; |
| 103 | break; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | switch(expr->expr_type) { |
| 109 | case A1TC_TYPEID: |
| 110 | ret = 0; /* TYPE-IDENTIFIER is a CLASS */ |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if(ret == -1) { |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 117 | FATAL("Cannot compile \"%s\" (%x:%x) at line %d", |
| 118 | arg->expr->Identifier, |
| 119 | arg->expr->expr_type, |
| 120 | arg->expr->meta_type, |
| 121 | arg->expr->_lineno); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | OUT("#error Cannot compile \"%s\" (%x/%x) at line %d\n", |
| 123 | arg->expr->Identifier, |
| 124 | arg->expr->meta_type, |
| 125 | arg->expr->expr_type, |
| 126 | arg->expr->_lineno |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static int |
| 134 | asn1c_attach_streams(asn1p_expr_t *expr) { |
| 135 | compiler_streams_t *cs; |
| 136 | int i; |
| 137 | |
| 138 | if(expr->data) |
| 139 | return 0; /* Already attached? */ |
| 140 | |
| 141 | expr->data = calloc(1, sizeof(compiler_streams_t)); |
| 142 | if(expr->data == NULL) |
| 143 | return -1; |
| 144 | |
| 145 | cs = expr->data; |
| 146 | for(i = 0; i < OT_MAX; i++) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 147 | TQ_INIT(&(cs->destination[i].chunks)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | default_logger_cb(int _severity, const char *fmt, ...) { |
| 155 | va_list ap; |
| 156 | char *pfx = ""; |
| 157 | |
| 158 | switch(_severity) { |
| 159 | case -1: pfx = "DEBUG: "; break; |
| 160 | case 0: pfx = "WARNING: "; break; |
| 161 | case 1: pfx = "FATAL: "; break; |
| 162 | } |
| 163 | |
| 164 | fprintf(stderr, "%s", pfx); |
| 165 | va_start(ap, fmt); |
| 166 | vfprintf(stderr, fmt, ap); |
| 167 | va_end(ap); |
| 168 | fprintf(stderr, "\n"); |
| 169 | } |
| 170 | |