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