lexi.c revision 1.155 1 /* $NetBSD: lexi.c,v 1.155 2021/11/25 16:09:02 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: lexi.c,v 1.155 2021/11/25 16:09:02 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
49 #endif
50
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "indent.h"
55
56 /*
57 * While inside lexi_alnum, this constant just marks a type, independently of
58 * the parentheses level.
59 */
60 #define lsym_type lsym_type_outside_parentheses
61
62 /* must be sorted alphabetically, is used in binary search */
63 static const struct keyword {
64 const char *name;
65 lexer_symbol lsym;
66 } keywords[] = {
67 {"_Bool", lsym_type},
68 {"_Complex", lsym_type},
69 {"_Imaginary", lsym_type},
70 {"auto", lsym_storage_class},
71 {"bool", lsym_type},
72 {"break", lsym_word},
73 {"case", lsym_case_label},
74 {"char", lsym_type},
75 {"complex", lsym_type},
76 {"const", lsym_type},
77 {"continue", lsym_word},
78 {"default", lsym_case_label},
79 {"do", lsym_do},
80 {"double", lsym_type},
81 {"else", lsym_else},
82 {"enum", lsym_tag},
83 {"extern", lsym_storage_class},
84 {"float", lsym_type},
85 {"for", lsym_for},
86 {"goto", lsym_word},
87 {"if", lsym_if},
88 {"imaginary", lsym_type},
89 {"inline", lsym_word},
90 {"int", lsym_type},
91 {"long", lsym_type},
92 {"offsetof", lsym_offsetof},
93 {"register", lsym_storage_class},
94 {"restrict", lsym_word},
95 {"return", lsym_return},
96 {"short", lsym_type},
97 {"signed", lsym_type},
98 {"sizeof", lsym_sizeof},
99 {"static", lsym_storage_class},
100 {"struct", lsym_tag},
101 {"switch", lsym_switch},
102 {"typedef", lsym_typedef},
103 {"union", lsym_tag},
104 {"unsigned", lsym_type},
105 {"void", lsym_type},
106 {"volatile", lsym_type},
107 {"while", lsym_while}
108 };
109
110 static struct {
111 const char **items;
112 unsigned int len;
113 unsigned int cap;
114 } typenames;
115
116 /*
117 * The transition table below was rewritten by hand from lx's output, given
118 * the following definitions. lx is Katherine Flavel's lexer generator.
119 *
120 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
121 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
122 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
123 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
124 *
125 * D+ E FS? -> $float;
126 * D* "." D+ E? FS? -> $float;
127 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
128 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
129 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
130 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
131 */
132 /* INDENT OFF */
133 static const unsigned char lex_number_state[][26] = {
134 /* examples:
135 00
136 s 0xx
137 t 00xaa
138 a 11 101100xxa..
139 r 11ee0001101lbuuxx.a.pp
140 t.01.e+008bLuxll0Ll.aa.p+0
141 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
142 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu", /* (other) */
143 [1] = "CEIDEHHHIJQ U Q VUVVZZZ", /* 0 */
144 [2] = "DEIDEHHHIJQ U Q VUVVZZZ", /* 1 */
145 [3] = "DEIDEHHHIJ U VUVVZZZ", /* 2 3 4 5 6 7 */
146 [4] = "DEJDEHHHJJ U VUVVZZZ", /* 8 9 */
147 [5] = " U VUVV ", /* A a C c D d */
148 [6] = " K U VUVV ", /* B b */
149 [7] = " FFF FF U VUVV ", /* E e */
150 [8] = " f f U VUVV f", /* F f */
151 [9] = " LLf fL PR Li L f", /* L */
152 [10] = " OOf fO S P O i O f", /* l */
153 [11] = " FFX ", /* P p */
154 [12] = " MM M i iiM M ", /* U u */
155 [13] = " N ", /* X x */
156 [14] = " G Y ", /* + - */
157 [15] = "B EE EE T W ", /* . */
158 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
159 };
160 /* INDENT ON */
161
162 static const unsigned char lex_number_row[] = {
163 ['0'] = 1,
164 ['1'] = 2,
165 ['2'] = 3, ['3'] = 3, ['4'] = 3, ['5'] = 3, ['6'] = 3, ['7'] = 3,
166 ['8'] = 4, ['9'] = 4,
167 ['A'] = 5, ['a'] = 5, ['C'] = 5, ['c'] = 5, ['D'] = 5, ['d'] = 5,
168 ['B'] = 6, ['b'] = 6,
169 ['E'] = 7, ['e'] = 7,
170 ['F'] = 8, ['f'] = 8,
171 ['L'] = 9,
172 ['l'] = 10,
173 ['P'] = 11, ['p'] = 11,
174 ['U'] = 12, ['u'] = 12,
175 ['X'] = 13, ['x'] = 13,
176 ['+'] = 14, ['-'] = 14,
177 ['.'] = 15,
178 };
179
180 static void
181 check_size_token(size_t desired_size)
182 {
183 if (token.e + desired_size >= token.l)
184 buf_expand(&token, desired_size);
185 }
186
187 static void
188 token_add_char(char ch)
189 {
190 check_size_token(1);
191 *token.e++ = ch;
192 }
193
194 #ifdef debug
195 static const char *
196 lsym_name(lexer_symbol sym)
197 {
198 static const char *const name[] = {
199 "eof",
200 "preprocessing",
201 "newline",
202 "form_feed",
203 "comment",
204 "lparen_or_lbracket",
205 "rparen_or_rbracket",
206 "lbrace",
207 "rbrace",
208 "period",
209 "unary_op",
210 "binary_op",
211 "postfix_op",
212 "question",
213 "colon",
214 "comma",
215 "semicolon",
216 "typedef",
217 "storage_class",
218 "type_outside_parentheses",
219 "type_in_parentheses",
220 "tag",
221 "case_label",
222 "string_prefix",
223 "sizeof",
224 "offsetof",
225 "word",
226 "funcname",
227 "do",
228 "else",
229 "for",
230 "if",
231 "switch",
232 "while",
233 "return",
234 };
235
236 return name[sym];
237 }
238
239 static void
240 debug_print_buf(const char *name, const struct buffer *buf)
241 {
242 if (buf->s < buf->e) {
243 debug_printf("%s ", name);
244 debug_vis_range("\"", buf->s, buf->e, "\"\n");
245 }
246 }
247
248 #define debug_ps_bool(name) \
249 if (ps.name != prev_ps.name) \
250 debug_println("[%c] ps." #name, ps.name ? 'x' : ' ')
251 #define debug_ps_int(name) \
252 if (ps.name != prev_ps.name) \
253 debug_println("%3d ps." #name, ps.name)
254
255 static void
256 debug_lexi(lexer_symbol lsym)
257 {
258 /*
259 * Watch out for 'rolled back parser state' in the debug output; the
260 * differences around these are unreliable.
261 */
262 static struct parser_state prev_ps;
263
264 debug_println("");
265 debug_printf("line %d: %s", line_no, lsym_name(lsym));
266 debug_vis_range(" \"", token.s, token.e, "\"\n");
267
268 debug_print_buf("label", &lab);
269 debug_print_buf("code", &code);
270 debug_print_buf("comment", &com);
271
272 debug_println(" ps.prev_token = %s", lsym_name(ps.prev_token));
273 debug_ps_bool(next_col_1);
274 debug_ps_bool(curr_col_1);
275 debug_ps_bool(next_unary);
276 debug_ps_bool(is_function_definition);
277 debug_ps_bool(want_blank);
278 debug_ps_int(paren_level);
279 debug_ps_int(p_l_follow);
280 if (ps.paren_level != prev_ps.paren_level) {
281 debug_printf(" ps.paren_indents:");
282 for (int i = 0; i < ps.paren_level; i++)
283 debug_printf(" %d", ps.paren_indents[i]);
284 debug_println("");
285 }
286 debug_ps_int(cast_mask);
287 debug_ps_int(not_cast_mask);
288
289 debug_ps_int(comment_delta);
290 debug_ps_int(n_comment_delta);
291 debug_ps_int(com_ind);
292
293 debug_ps_bool(block_init);
294 debug_ps_int(block_init_level);
295 debug_ps_bool(init_or_struct);
296
297 debug_ps_int(ind_level);
298 debug_ps_int(ind_level_follow);
299
300 debug_ps_int(decl_level);
301 debug_ps_bool(decl_on_line);
302 debug_ps_bool(in_decl);
303 debug_ps_int(just_saw_decl);
304 debug_ps_bool(in_parameter_declaration);
305 debug_ps_bool(decl_indent_done);
306
307 debug_ps_bool(in_stmt_or_decl);
308 debug_ps_bool(in_stmt_cont);
309 debug_ps_bool(is_case_label);
310
311 debug_ps_bool(search_stmt);
312
313 prev_ps = ps;
314 }
315 #endif
316
317 static lexer_symbol
318 lexi_end(lexer_symbol lsym)
319 {
320 #ifdef debug
321 debug_lexi(lsym);
322 #endif
323 return lsym;
324 }
325
326 static void
327 lex_number(void)
328 {
329 for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
330 unsigned char ch = (unsigned char)inp_peek();
331 if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
332 break;
333
334 unsigned char row = lex_number_row[ch];
335 if (lex_number_state[row][s - 'A'] == ' ') {
336 /*-
337 * lex_number_state[0][s - 'A'] now indicates the type:
338 * f = floating, i = integer, u = unknown
339 */
340 return;
341 }
342
343 s = lex_number_state[row][s - 'A'];
344 token_add_char(inp_next());
345 }
346 }
347
348 static bool
349 is_identifier_start(char ch)
350 {
351 return ch_isalpha(ch) || ch == '_' || ch == '$';
352 }
353
354 static bool
355 is_identifier_part(char ch)
356 {
357 return ch_isalnum(ch) || ch == '_' || ch == '$';
358 }
359
360 static void
361 lex_word(void)
362 {
363 for (;;) {
364 if (is_identifier_part(inp_peek()))
365 token_add_char(inp_next());
366 else if (inp_peek() == '\\' && inp_lookahead(1) == '\n') {
367 inp_skip();
368 inp_skip();
369 } else
370 return;
371 }
372 }
373
374 static void
375 lex_char_or_string(void)
376 {
377 for (char delim = token.e[-1];;) {
378 if (inp_peek() == '\n') {
379 diag(1, "Unterminated literal");
380 return;
381 }
382
383 token_add_char(inp_next());
384 if (token.e[-1] == delim)
385 return;
386
387 if (token.e[-1] == '\\') {
388 if (inp_peek() == '\n')
389 ++line_no;
390 token_add_char(inp_next());
391 }
392 }
393 }
394
395 /* Guess whether the current token is a declared type. */
396 static bool
397 probably_typename(void)
398 {
399 if (ps.prev_token == lsym_storage_class)
400 return true;
401 if (ps.block_init)
402 return false;
403 if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */
404 return false;
405 if (inp_peek() == '*' && inp_lookahead(1) != '=')
406 goto maybe;
407 /* XXX: is_identifier_start */
408 if (ch_isalpha(inp_peek()))
409 goto maybe;
410 return false;
411 maybe:
412 return ps.prev_token == lsym_semicolon ||
413 ps.prev_token == lsym_lbrace ||
414 ps.prev_token == lsym_rbrace;
415 }
416
417 static int
418 bsearch_typenames(const char *key)
419 {
420 const char **arr = typenames.items;
421 int lo = 0;
422 int hi = (int)typenames.len - 1;
423
424 while (lo <= hi) {
425 int mid = (int)((unsigned)(lo + hi) >> 1);
426 int cmp = strcmp(arr[mid], key);
427 if (cmp < 0)
428 lo = mid + 1;
429 else if (cmp > 0)
430 hi = mid - 1;
431 else
432 return mid;
433 }
434 return -(lo + 1);
435 }
436
437 static bool
438 is_typename(void)
439 {
440 if (opt.auto_typedefs &&
441 token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
442 return true;
443
444 return bsearch_typenames(token.s) >= 0;
445 }
446
447 static int
448 cmp_keyword_by_name(const void *key, const void *elem)
449 {
450 return strcmp(key, ((const struct keyword *)elem)->name);
451 }
452
453 static bool
454 probably_looking_at_definition(void)
455 {
456 for (const char *p = inp_p(), *e = inp_line_end(); p < e;)
457 if (*p++ == ')' && (*p == ';' || *p == ','))
458 return false;
459 return true;
460 }
461
462 /* Read an alphanumeric token into 'token', or return lsym_eof. */
463 static lexer_symbol
464 lexi_alnum(void)
465 {
466 if (ch_isdigit(inp_peek()) ||
467 (inp_peek() == '.' && ch_isdigit(inp_lookahead(1)))) {
468 lex_number();
469 } else if (is_identifier_part(inp_peek())) {
470 lex_word();
471 } else
472 return lsym_eof; /* just as a placeholder */
473
474 *token.e = '\0';
475
476 if (token.s[0] == 'L' && token.s[1] == '\0' &&
477 (inp_peek() == '"' || inp_peek() == '\''))
478 return lsym_string_prefix;
479
480 while (ch_isblank(inp_peek()))
481 inp_skip();
482
483 ps.next_unary = ps.prev_token == lsym_tag; /* for 'struct s *' */
484
485 if (ps.prev_token == lsym_tag && ps.p_l_follow == 0)
486 return lsym_type_outside_parentheses;
487
488 const struct keyword *kw = bsearch(token.s, keywords,
489 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
490 bool is_type = false;
491 if (kw == NULL) {
492 if (is_typename()) {
493 is_type = true;
494 ps.next_unary = true;
495 goto found_typename;
496 }
497
498 } else { /* we have a keyword */
499 is_type = kw->lsym == lsym_type;
500 ps.next_unary = true;
501 if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
502 return kw->lsym;
503
504 found_typename:
505 if (ps.p_l_follow > 0) {
506 /* inside parentheses: cast, param list, offsetof or sizeof */
507 ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask;
508 }
509 if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
510 if (kw != NULL && kw->lsym == lsym_tag)
511 return lsym_tag;
512 if (ps.p_l_follow == 0)
513 return lsym_type_outside_parentheses;
514 }
515 }
516
517 if (inp_peek() == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
518 !ps.in_parameter_declaration && !ps.block_init) {
519
520 if (probably_looking_at_definition()) {
521 ps.is_function_definition = true;
522 if (ps.in_decl)
523 ps.in_parameter_declaration = true;
524 return lsym_funcname;
525 }
526
527 } else if (ps.p_l_follow == 0 && probably_typename()) {
528 ps.next_unary = true;
529 return lsym_type_outside_parentheses;
530 }
531
532 return is_type ? lsym_type_in_parentheses : lsym_word;
533 }
534
535 /* Reads the next token, placing it in the global variable "token". */
536 lexer_symbol
537 lexi(void)
538 {
539 token.e = token.s;
540 ps.curr_col_1 = ps.next_col_1;
541 ps.next_col_1 = false;
542
543 while (ch_isblank(inp_peek())) {
544 ps.curr_col_1 = false;
545 inp_skip();
546 }
547
548 lexer_symbol alnum_lsym = lexi_alnum();
549 if (alnum_lsym != lsym_eof)
550 return lexi_end(alnum_lsym);
551
552 /* Scan a non-alphanumeric token */
553
554 check_size_token(3); /* for things like "<<=" */
555 *token.e++ = inp_next();
556 *token.e = '\0';
557
558 lexer_symbol lsym;
559 bool unary_delim = false; /* whether the current token forces a
560 * following operator to be unary */
561
562 switch (token.e[-1]) {
563 case '\n':
564 unary_delim = ps.next_unary;
565 ps.next_col_1 = true;
566 /* if data has been exhausted, the newline is a dummy. */
567 lsym = had_eof ? lsym_eof : lsym_newline;
568 break;
569
570 case '\'':
571 case '"':
572 lex_char_or_string();
573 lsym = lsym_word;
574 break;
575
576 case '(':
577 case '[':
578 unary_delim = true;
579 lsym = lsym_lparen_or_lbracket;
580 break;
581
582 case ')':
583 case ']':
584 lsym = lsym_rparen_or_rbracket;
585 break;
586
587 case '#':
588 unary_delim = ps.next_unary;
589 lsym = lsym_preprocessing;
590 break;
591
592 case '?':
593 unary_delim = true;
594 lsym = lsym_question;
595 break;
596
597 case ':':
598 lsym = lsym_colon;
599 unary_delim = true;
600 break;
601
602 case ';':
603 unary_delim = true;
604 lsym = lsym_semicolon;
605 break;
606
607 case '{':
608 unary_delim = true;
609 lsym = lsym_lbrace;
610 break;
611
612 case '}':
613 unary_delim = true;
614 lsym = lsym_rbrace;
615 break;
616
617 case '\f':
618 unary_delim = ps.next_unary;
619 ps.next_col_1 = true;
620 lsym = lsym_form_feed;
621 break;
622
623 case ',':
624 unary_delim = true;
625 lsym = lsym_comma;
626 break;
627
628 case '.':
629 unary_delim = false;
630 lsym = lsym_period;
631 break;
632
633 case '-':
634 case '+':
635 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
636 unary_delim = true;
637
638 if (inp_peek() == token.e[-1]) { /* ++, -- */
639 *token.e++ = inp_next();
640 if (ps.prev_token == lsym_word ||
641 ps.prev_token == lsym_rparen_or_rbracket) {
642 lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
643 unary_delim = false;
644 }
645
646 } else if (inp_peek() == '=') { /* += */
647 *token.e++ = inp_next();
648
649 } else if (inp_peek() == '>') { /* -> */
650 *token.e++ = inp_next();
651 unary_delim = false;
652 lsym = lsym_unary_op;
653 ps.want_blank = false;
654 }
655 break;
656
657 case '=':
658 if (ps.init_or_struct)
659 ps.block_init = true;
660 if (inp_peek() == '=') { /* == */
661 *token.e++ = inp_next();
662 *token.e = '\0';
663 }
664 lsym = lsym_binary_op;
665 unary_delim = true;
666 break;
667
668 case '>':
669 case '<':
670 case '!': /* ops like <, <<, <=, !=, etc */
671 if (inp_peek() == '>' || inp_peek() == '<' || inp_peek() == '=')
672 *token.e++ = inp_next();
673 if (inp_peek() == '=')
674 *token.e++ = inp_next();
675 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
676 unary_delim = true;
677 break;
678
679 case '*':
680 unary_delim = true;
681 if (!ps.next_unary) {
682 if (inp_peek() == '=')
683 *token.e++ = inp_next();
684 lsym = lsym_binary_op;
685 break;
686 }
687
688 while (inp_peek() == '*' || ch_isspace(inp_peek())) {
689 if (inp_peek() == '*')
690 token_add_char('*');
691 inp_skip();
692 }
693
694 if (ps.in_decl) {
695 const char *tp = inp_p(), *e = inp_line_end();
696
697 while (tp < e) {
698 if (ch_isspace(*tp))
699 tp++;
700 else if (is_identifier_start(*tp)) {
701 tp++;
702 while (tp < e && is_identifier_part(*tp))
703 tp++;
704 } else
705 break;
706 }
707
708 if (tp < e && *tp == '(')
709 ps.is_function_definition = true;
710 }
711
712 lsym = lsym_unary_op;
713 break;
714
715 default:
716 if (token.e[-1] == '/' && (inp_peek() == '*' || inp_peek() == '/')) {
717 *token.e++ = inp_next();
718 lsym = lsym_comment;
719 unary_delim = ps.next_unary;
720 break;
721 }
722
723 /* handle '||', '&&', etc., and also things as in 'int *****i' */
724 while (token.e[-1] == inp_peek() || inp_peek() == '=')
725 token_add_char(inp_next());
726
727 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
728 unary_delim = true;
729 }
730
731 ps.next_unary = unary_delim;
732
733 check_size_token(1);
734 *token.e = '\0';
735
736 return lexi_end(lsym);
737 }
738
739 void
740 register_typename(const char *name)
741 {
742 if (typenames.len >= typenames.cap) {
743 typenames.cap = 16 + 2 * typenames.cap;
744 typenames.items = xrealloc(typenames.items,
745 sizeof(typenames.items[0]) * typenames.cap);
746 }
747
748 int pos = bsearch_typenames(name);
749 if (pos >= 0)
750 return; /* already in the list */
751
752 pos = -(pos + 1);
753 memmove(typenames.items + pos + 1, typenames.items + pos,
754 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
755 typenames.items[pos] = xstrdup(name);
756 }
757