blob: a1dbbd898b9640040a87f9c619fa6b46c42d587f [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;
Lev Walkine4d6ab82004-09-22 16:05:13 +0000122 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000123 if(terminal && terminal == top_parent) {
124 _format = TNF_RSAFE;
Lev Walkin5a8219a2004-09-08 00:28:57 +0000125 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 }
127 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000128#if 0
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 case ASN_CONSTR_SEQUENCE_OF:
130 case ASN_CONSTR_SET_OF:
131 if(expr->Identifier) {
132 typename = expr->Identifier;
133 } else {
134 asn1p_expr_t *child;
135 child = TQ_FIRST(&(expr->members));
136 typename = asn1c_type_name(arg, child, _format);
137 if(typename)
138 return typename;
139 _format = TNF_SAFE;
140 typename = child->Identifier;
141 }
142 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000143#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 case ASN_BASIC_INTEGER:
145 case ASN_BASIC_ENUMERATED:
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000146 case ASN_BASIC_REAL:
147 if((arg->flags & A1C_USE_NATIVE_TYPES)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 switch(_format) {
149 case TNF_CTYPE:
150 case TNF_RSAFE:
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000151 if(expr->expr_type == ASN_BASIC_REAL)
152 return "double";
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 else
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000154 return "int";
155 default: break;
156 }
157 switch(expr->expr_type) {
158 case ASN_BASIC_INTEGER:
159 return "NativeInteger";
160 case ASN_BASIC_ENUMERATED:
161 return "NativeEnumerated";
162 case ASN_BASIC_REAL:
163 return "NativeReal";
164 default: break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
166 }
167 /* Fall through */
168 default:
Lev Walkin0e22b982004-08-25 02:03:26 +0000169 if(expr->expr_type
170 & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 if(_format == TNF_RSAFE)
172 _format = TNF_CTYPE;
173 typename = ASN_EXPR_TYPE2STR(expr->expr_type);
174 } else {
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000175 _format = TNF_RSAFE;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 typename = expr->Identifier;
177 }
178 }
179
180 switch(_format) {
181 case TNF_UNMODIFIED:
182 case TNF_INCLUDE:
183 return asn1c_make_identifier(1, typename, 0);
184 case TNF_SAFE:
185 return asn1c_make_identifier(0, typename, 0);
186 case TNF_CTYPE:
187 return asn1c_make_identifier(0, typename, "t", 0);
188 case TNF_RSAFE:
189 return asn1c_make_identifier(0, "struct", " ", typename, 0);
190 }
191
Lev Walkin8dd0eed2004-09-17 06:32:12 +0000192 assert(!"unreachable");
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 return typename;
194}
195