-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathada_name.c
365 lines (314 loc) · 6.66 KB
/
ada_name.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* The routines here generate legal Ada identifiers from
* C identifiers. All the klugy unique name munging is
* done here as well
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include "c2ada.h"
extern char* ada_keyword(char*, int);
extern int auto_package;
#undef NULL
#define NULL 0
#define HTAB 512 /* unique name hash table size */
/*
* Unique name hash table node type
*/
typedef struct uniq_name_t
{
char* uname;
int uord;
hash_t uhash;
struct uniq_name_t* ulink;
} uniq_name_t;
static uniq_name_t* hash_table[HTAB];
/*
* allocator for unique name hash table nodes
*/
static uniq_name_t* new_uniq_name()
{
static uniq_name_t* free = NULL;
static int free_index;
if(free == NULL || free_index > 63)
{
free = (uniq_name_t*)allocate(sizeof(uniq_name_t) * 64);
free_index = 0;
}
return &free[free_index++];
}
bool is_ada_keyword(char* name)
/*
* Determine if argument is an Ada keyword.
*/
{
char buf[16];
char* p;
assert(name != NULL);
if(strlen(name) > 9)
{
return 0;
}
for(p = buf; *name; name++)
{
*p++ = lcase(*name);
}
*p = 0;
return (ada_keyword(buf, (int)(p - buf))) ? 1 : 0;
}
bool is_reserved_id(char* name)
/*
* Check if this is one of the configured reserved words.
*/
{
static char** reserved;
static int n_reserved = -1;
if(n_reserved == -1)
{
/* initialize table */
reserved = configured_reserved_ids(&n_reserved);
}
if(n_reserved == 0)
{
return 0;
}
else
{
int i;
for(i = 0; i < n_reserved; i++)
{
if(!lcasecmp(reserved[i], name))
{
return 1;
}
}
return 0;
}
}
void make_ada_identifier(char* name, char* buf)
/*
* Generate a legal Ada identifier from the argument 'name'.
* Specifically, ignore '$', ignore leading, trailing, or multiple
* underscores. Since hyphen may come from include directory names
* convert it to underscore.
*/
{
assert(name != NULL);
assert(buf != NULL);
while((*name == '_') || (*name == '$') || (*name == '.'))
{
name++;
}
if(*name == 0)
{
strcpy(buf, "YIKES");
}
while(*name)
{
switch(*name)
{
case '$':
name++;
break;
case '_':
if(name[1] != '_' && name[1] != 0)
{
*buf++ = *name;
}
name++;
break;
case '-':
*buf = '_';
buf++;
name++;
break;
default:
*buf++ = *name++;
break;
}
}
*buf = 0;
}
static uniq_name_t* find_uniq(char* name, int ord)
/*
* Determine if an identifier has already been used
* in the argument (ord) module.
*/
{
uniq_name_t* un;
hash_t hash;
int index;
hash = lcase_hash(name);
index = hash & (HTAB - 1);
if(!auto_package)
{
ord = 0;
}
for(un = hash_table[index]; un; un = un->ulink)
{
if(un->uord == ord && un->uhash == hash && !lcasecmp(un->uname, name))
{
return un;
}
}
return NULL;
}
char* uniq_name(char* name, int ord)
/*
* Find a name that's unique within the argument unit (ord).
* If the argument name collides, generate suffixed names
* until a unique one is generated.
*/
{
char buf[2048];
uniq_name_t* un;
int index, i;
strcpy(buf, name);
for(i = 0; find_uniq(buf, ord); i++)
{
sprintf(buf, "%s_c%d", name, i);
if(!find_uniq(buf, ord))
break;
}
un = new_uniq_name();
un->uname = new_string(buf);
un->uhash = lcase_hash(buf);
un->uord = (auto_package) ? ord : 0;
index = un->uhash & (HTAB - 1);
un->ulink = hash_table[index];
hash_table[index] = un;
return un->uname;
}
ident_case_t id_case(char* id)
{
return Lower; /* mg */
#if 0
/* code that was here equivalent to */
if (isupper(id[0]) {
return isupper(id[1]) ? Upper : Cap;
} else {
return Lower;
}
#endif
}
static void uppercase(char* id)
{
char c;
for(; *id; id++)
{
c = *id;
if(islower(c))
*id = toupper(c);
}
}
static void capitalize(char* id)
{
char* prev;
char c;
for(prev = id; *id; id++)
{
if(*id == '_')
{
c = *prev;
if(islower(c))
*prev = toupper(c);
prev = id + 1;
}
}
}
void id_format(char* id, ident_case_t icase)
{
switch(icase)
{
case Upper:
uppercase(id);
break;
case Cap:
capitalize(id);
break;
default:
break;
}
}
char* ada_name(char* name, int ord)
/*
* Transform an encoded string into a valid Ada identifier
* that is unique within unit <ord>.
* The "encoding" is that the first character of the argument
* string may be a code indicating a prefix to add to
* the generated name.
*/
{
char buf[2048];
char* p;
ident_case_t icase;
/* Ignore any leading underscores. */
for(; *name == '_'; name++)
;
/* Name was all underscores?? */
if(*name == 0)
{
name = "YIKES";
}
if(is_ada_keyword(name) || is_reserved_id(name))
{
/*
* NB: assume that none of the reserved ID's starts
* with "c_", so we don't have to test again.
*/
icase = id_case(name);
sprintf(buf, "c_%s", name);
}
else
{
switch(name[0])
{
case ENUM_PREFIX:
strcpy(buf, "enum_");
name++;
break;
case STRUCT_PREFIX:
strcpy(buf, "struct_");
name++;
break;
case UNION_PREFIX:
strcpy(buf, "union_");
name++;
break;
default:
buf[0] = 0;
break;
}
icase = id_case(name);
for(p = buf; *p; p++)
;
make_ada_identifier(name, p);
}
if(ord == -1)
{
p = new_string(buf);
}
else
{
p = uniq_name(buf, ord);
}
id_format(p, icase);
return p;
}
char* tail(char* id)
/*
* Return the rightmost component name of an Ada name; i.e.
* return the largest rightmost segment of id that
* doesn't contain a '.'
*/
{
char* p = id + strlen(id) - 1;
for(; p >= id; p--)
{
if(*p == '.')
return p + 1;
}
return id;
}