blob: 2bdbd7c858932fe8a70ea424137ce7ddd98f147e [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
115 if(_format == TNF_RSAFE || _format == TNF_CTYPE) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 /*
117 * The recursion-safe format is requested.
118 * The problem here is that only constructed types
119 * might be referenced with "struct".
120 * Change RSAFE to CTYPE if the terminal type
121 * is primitive.
122 */
123 asn1p_expr_t *terminal;
124 terminal = asn1f_find_terminal_type_ex(
Lev Walkin14d70992004-08-22 03:23:46 +0000125 arg->asn, arg->mod, arg->expr);
Lev Walkin5a8219a2004-09-08 00:28:57 +0000126 if(terminal) {
127 if(terminal->expr_type
128 & (ASN_BASIC_MASK | ASN_STRING_MASK))
129 _format = TNF_CTYPE;
130 if(terminal == top_parent)
131 _format = TNF_RSAFE;
132 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 }
134 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000135#if 0
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 case ASN_CONSTR_SEQUENCE_OF:
137 case ASN_CONSTR_SET_OF:
138 if(expr->Identifier) {
139 typename = expr->Identifier;
140 } else {
141 asn1p_expr_t *child;
142 child = TQ_FIRST(&(expr->members));
143 typename = asn1c_type_name(arg, child, _format);
144 if(typename)
145 return typename;
146 _format = TNF_SAFE;
147 typename = child->Identifier;
148 }
149 break;
Lev Walkin0e22b982004-08-25 02:03:26 +0000150#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 case ASN_BASIC_INTEGER:
152 case ASN_BASIC_ENUMERATED:
153 if((arg->flags & A1C_USE_NATIVE_INTEGERS)) {
154 switch(_format) {
155 case TNF_CTYPE:
156 case TNF_RSAFE:
157 return "int";
158 default:
159 if(expr->expr_type == ASN_BASIC_INTEGER)
160 return "NativeInteger";
161 else
162 return "NativeEnumerated";
163 }
164 }
165 /* Fall through */
166 default:
Lev Walkin0e22b982004-08-25 02:03:26 +0000167 if(expr->expr_type
168 & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 if(_format == TNF_RSAFE)
170 _format = TNF_CTYPE;
171 typename = ASN_EXPR_TYPE2STR(expr->expr_type);
172 } else {
173 _format = TNF_SAFE;
174 typename = expr->Identifier;
175 }
176 }
177
178 switch(_format) {
179 case TNF_UNMODIFIED:
180 case TNF_INCLUDE:
181 return asn1c_make_identifier(1, typename, 0);
182 case TNF_SAFE:
183 return asn1c_make_identifier(0, typename, 0);
184 case TNF_CTYPE:
185 return asn1c_make_identifier(0, typename, "t", 0);
186 case TNF_RSAFE:
187 return asn1c_make_identifier(0, "struct", " ", typename, 0);
188 }
189
190 assert("!unreachable");
191 return typename;
192}
193