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