blob: e38ce8d908352cbce507a154d667881e4bf8e1dc [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00002#include "asn1c_lang.h"
3#include "asn1c_out.h"
4#include "asn1c_save.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00005
6static void default_logger_cb(int, const char *fmt, ...);
7static int asn1c_compile_expr(arg_t *arg);
8static int asn1c_attach_streams(asn1p_expr_t *expr);
9
10int
Lev Walkin866cff12005-03-05 00:50:53 +000011asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
12 int argc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +000013 arg_t arg_s;
14 arg_t *arg = &arg_s;
Lev Walkinb85a8132005-08-18 13:38:19 +000015 asn1p_module_t *mod;
Lev Walkinf15320b2004-06-03 03:38:44 +000016 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 */
Lev Walkinb85a8132005-08-18 13:38:19 +000033 TQ_FOR(mod, &(asn->modules), mod_next) {
34 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000035 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 */
Lev Walkin866cff12005-03-05 00:50:53 +000061 if(asn1c_save_compiled_output(arg, datadir, argc, argv))
Lev Walkinf15320b2004-06-03 03:38:44 +000062 return -1;
63
64 return 0;
65}
66
67static int
68asn1c_compile_expr(arg_t *arg) {
69 asn1p_expr_t *expr = arg->expr;
70 int (*type_cb)(arg_t *);
71 int ret;
72
Lev Walkind9bd7752004-06-05 08:17:50 +000073 assert((int)expr->meta_type >= AMT_INVALID);
Lev Walkinf15320b2004-06-03 03:38:44 +000074 assert(expr->meta_type < AMT_EXPR_META_MAX);
Lev Walkind9bd7752004-06-05 08:17:50 +000075 assert((int)expr->expr_type >= A1TC_INVALID);
Lev Walkinf15320b2004-06-03 03:38:44 +000076 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
Lev Walkinf15320b2004-06-03 03:38:44 +000081 DEBUG("Compiling %s at line %d",
82 expr->Identifier,
83 expr->_lineno);
84
85 ret = type_cb(arg);
Lev Walkin6fec44d2004-08-22 03:10:23 +000086
87 if(arg->target->destination[OT_TYPE_DECLS].indent_level == 0)
88 OUT(";\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000089 } 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:
Lev Walkin9c2285a2006-03-09 08:49:26 +000099 case AMT_OBJECTCLASS:
100 case AMT_OBJECTFIELD:
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 case AMT_VALUE:
102 case AMT_VALUESET:
103 ret = 0;
104 break;
105 default:
106 break;
107 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 }
109
110 if(ret == -1) {
Lev Walkin6fec44d2004-08-22 03:10:23 +0000111 FATAL("Cannot compile \"%s\" (%x:%x) at line %d",
112 arg->expr->Identifier,
113 arg->expr->expr_type,
114 arg->expr->meta_type,
115 arg->expr->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 OUT("#error Cannot compile \"%s\" (%x/%x) at line %d\n",
117 arg->expr->Identifier,
118 arg->expr->meta_type,
119 arg->expr->expr_type,
120 arg->expr->_lineno
121 );
122 }
123
124 return ret;
125}
126
127static int
128asn1c_attach_streams(asn1p_expr_t *expr) {
129 compiler_streams_t *cs;
130 int i;
131
132 if(expr->data)
133 return 0; /* Already attached? */
134
135 expr->data = calloc(1, sizeof(compiler_streams_t));
136 if(expr->data == NULL)
137 return -1;
138
139 cs = expr->data;
140 for(i = 0; i < OT_MAX; i++) {
Lev Walkin59004fa2004-08-20 13:37:01 +0000141 TQ_INIT(&(cs->destination[i].chunks));
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 }
143
144 return 0;
145}
146
147static void
148default_logger_cb(int _severity, const char *fmt, ...) {
149 va_list ap;
150 char *pfx = "";
151
152 switch(_severity) {
153 case -1: pfx = "DEBUG: "; break;
154 case 0: pfx = "WARNING: "; break;
155 case 1: pfx = "FATAL: "; break;
156 }
157
158 fprintf(stderr, "%s", pfx);
159 va_start(ap, fmt);
160 vfprintf(stderr, fmt, ap);
161 va_end(ap);
162 fprintf(stderr, "\n");
163}
164