blob: 86edb52bd6ab81b5fc7cfe9fe46bd01c44ab06de [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_misc.h"
3
Lev Walkinf15320b2004-06-03 03:38:44 +00004#include <asn1fix_export.h>
5
Lev Walkinf15320b2004-06-03 03:38:44 +00006/*
7 * Construct identifier from multiple parts.
8 * Convert unsafe characters to underscores.
9 */
10char *
11asn1c_make_identifier(int unsafe_only_spaces, char *arg1, ...) {
12 static char *storage;
13 static int storage_size;
14 int nodelimiter = 0;
15 va_list ap;
16 char *str;
17 int size;
18 char *p;
19
20 if(arg1 == NULL)
21 return NULL;
22
23 /*
24 * Estimate the necessary storage size
25 */
26 size = strlen(arg1);
27 va_start(ap, arg1);
28 while((str = va_arg(ap, char *)))
29 size += 1 + strlen(str);
30 va_end(ap);
31
32 /*
33 * Make sure we have this amount of storage.
34 */
35 if(storage_size <= size) {
36 if(storage) free(storage);
37 storage = malloc(size + 1);
38 if(storage) {
39 storage_size = size;
40 } else {
41 storage_size = 0;
42 return NULL;
43 }
44 }
45
46 /*
47 * Fill-in the storage.
48 */
49 va_start(ap, arg1);
50 str = arg1;
51 p = storage;
52 for(str = arg1; str; str = va_arg(ap, char *)) {
53 int subst_made = 0;
54
55 if(str[0] == ' ' && str[1] == '\0') {
56 *p++ = ' ';
57 nodelimiter = 1; /* No delimiter */
58 continue;
59 }
60
61 if(str != arg1 && !nodelimiter)
62 *p++ = '_'; /* Delimiter between tokens */
63 nodelimiter = 0;
64
65 for(; *str; str++) {
66 if(isalnum(*str)) {
67 *p++ = *str;
68 subst_made = 0;
69 } else if(!subst_made++) {
70 if(unsafe_only_spaces && !isspace(*str)) {
71 *p ++ = *str;
72 } else {
73 *p++ = '_';
74 }
75 }
76 }
77 }
78 va_end(ap);
79 *p = '\0';
80
81 assert((p - storage) <= storage_size);
82
83 return storage;
84}
85
Lev Walkinf15320b2004-06-03 03:38:44 +000086char *
87asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
Lev Walkin5a8219a2004-09-08 00:28:57 +000088 asn1p_expr_t *top_parent;
Lev Walkinf15320b2004-06-03 03:38:44 +000089 char *typename;
90
Lev Walkin5a8219a2004-09-08 00:28:57 +000091 /* Rewind to the topmost parent expression */
92 if((top_parent = expr->parent_expr))
93 while(top_parent->parent_expr)
94 top_parent = top_parent->parent_expr;
95
Lev Walkinf15320b2004-06-03 03:38:44 +000096 switch(expr->expr_type) {
97 case A1TC_REFERENCE:
98 typename = expr->reference->components[
99 expr->reference->comp_count-1].name;
100 if(typename[0] == '&') {
101 arg_t tmp = *arg;
102
103 /*
104 * This is a reference to a type defined in a class.
105 * Resolve it and use instead.
106 */
107 tmp.expr = asn1f_class_access_ex(arg->asn, arg->mod,
Lev Walkin065a74b2004-08-22 03:23:15 +0000108 arg->expr, expr->reference);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 if(tmp.expr) return NULL;
110
Lev Walkin065a74b2004-08-22 03:23:15 +0000111 tmp.mod = tmp.expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 return asn1c_type_name(&tmp, tmp.expr, _format);
Lev Walkin5a8219a2004-09-08 00:28:57 +0000113 }
114
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000115 if(_format == TNF_CTYPE) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 /*
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000117 * If the component references the type itself,
118 * switch to a recursion safe type representation
119 * ("struct foo" instead of "foo_t").
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 */
121 asn1p_expr_t *terminal;
122 terminal = asn1f_find_terminal_type_ex(
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000123 arg->asn, arg->mod, expr);
124 if(terminal && terminal == top_parent) {
125 _format = TNF_RSAFE;
Lev Walkin5a8219a2004-09-08 00:28:57 +0000126 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 }
128 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000129#if 0
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 case ASN_CONSTR_SEQUENCE_OF:
131 case ASN_CONSTR_SET_OF:
132 if(expr->Identifier) {
133 typename = expr->Identifier;
134 } else {
135 asn1p_expr_t *child;
136 child = TQ_FIRST(&(expr->members));
137 typename = asn1c_type_name(arg, child, _format);
138 if(typename)
139 return typename;
140 _format = TNF_SAFE;
141 typename = child->Identifier;
142 }
143 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000144#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 case ASN_BASIC_INTEGER:
146 case ASN_BASIC_ENUMERATED:
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000147 case ASN_BASIC_REAL:
148 if((arg->flags & A1C_USE_NATIVE_TYPES)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 switch(_format) {
150 case TNF_CTYPE:
151 case TNF_RSAFE:
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000152 if(expr->expr_type == ASN_BASIC_REAL)
153 return "double";
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 else
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000155 return "int";
156 default: break;
157 }
158 switch(expr->expr_type) {
159 case ASN_BASIC_INTEGER:
160 return "NativeInteger";
161 case ASN_BASIC_ENUMERATED:
162 return "NativeEnumerated";
163 case ASN_BASIC_REAL:
164 return "NativeReal";
165 default: break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
167 }
168 /* Fall through */
169 default:
Lev Walkin0e22b982004-08-25 02:03:26 +0000170 if(expr->expr_type
171 & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 if(_format == TNF_RSAFE)
173 _format = TNF_CTYPE;
174 typename = ASN_EXPR_TYPE2STR(expr->expr_type);
175 } else {
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000176 _format = TNF_RSAFE;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 typename = expr->Identifier;
178 }
179 }
180
181 switch(_format) {
182 case TNF_UNMODIFIED:
183 case TNF_INCLUDE:
184 return asn1c_make_identifier(1, typename, 0);
185 case TNF_SAFE:
186 return asn1c_make_identifier(0, typename, 0);
187 case TNF_CTYPE:
188 return asn1c_make_identifier(0, typename, "t", 0);
189 case TNF_RSAFE:
190 return asn1c_make_identifier(0, "struct", " ", typename, 0);
191 }
192
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000193 assert(!"unreachable");
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 return typename;
195}
196