lexi.c revision 1.128 1 /* $NetBSD: lexi.c,v 1.128 2021/10/31 22:38:12 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.128 2021/10/31 22:38:12 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_ident},
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 };
256
257 return name[sym];
258 }
259
260 static void
261 debug_print_buf(const char *name, const struct buffer *buf)
262 {
263 if (buf->s < buf->e) {
264 debug_printf("%s ", name);
265 debug_vis_range("\"", buf->s, buf->e, "\"\n");
266 }
267 }
268
269 #define debug_ps_bool(name) \
270 if (ps.name != prev_ps.name) \
271 debug_println("[%c] ps." #name, ps.name ? 'x' : ' ')
272 #define debug_ps_int(name) \
273 if (ps.name != prev_ps.name) \
274 debug_println("%3d ps." #name, ps.name)
275
276 static void
277 debug_lexi(lexer_symbol lsym)
278 {
279 /*
280 * Watch out for 'rolled back parser state' in the debug output; the
281 * differences around these are unreliable.
282 */
283 static struct parser_state prev_ps;
284
285 debug_println("");
286 debug_printf("line %d: %s%s", line_no, lsym_name(lsym),
287 ps.curr_is_type ? " type" : "");
288 debug_vis_range(" \"", token.s, token.e, "\"\n");
289
290 debug_print_buf("label", &lab);
291 debug_print_buf("code", &code);
292 debug_print_buf("comment", &com);
293
294 debug_println(" ps.prev_token = %s", lsym_name(ps.prev_token));
295 debug_ps_bool(prev_is_type);
296 debug_ps_bool(curr_newline);
297 debug_ps_bool(curr_col_1);
298 debug_ps_bool(next_unary);
299 // procname
300 debug_ps_bool(want_blank);
301 debug_ps_int(paren_level);
302 debug_ps_int(p_l_follow);
303 // paren_indents
304 debug_ps_int(cast_mask);
305 debug_ps_int(not_cast_mask);
306
307 debug_ps_int(comment_delta);
308 debug_ps_int(n_comment_delta);
309 debug_ps_int(com_ind);
310
311 debug_ps_bool(block_init);
312 debug_ps_int(block_init_level);
313 debug_ps_bool(init_or_struct);
314
315 debug_ps_int(ind_level);
316 debug_ps_int(ind_level_follow);
317
318 debug_ps_int(decl_nest);
319 debug_ps_bool(decl_on_line);
320 debug_ps_bool(in_decl);
321 debug_ps_int(just_saw_decl);
322 debug_ps_bool(in_parameter_declaration);
323 debug_ps_bool(decl_indent_done);
324
325 debug_ps_bool(in_stmt);
326 debug_ps_bool(ind_stmt);
327 debug_ps_bool(is_case_label);
328
329 debug_ps_bool(search_stmt);
330
331 prev_ps = ps;
332 }
333 #endif
334
335 /* ARGSUSED */
336 static lexer_symbol
337 lexi_end(lexer_symbol lsym)
338 {
339 #ifdef debug
340 debug_lexi(lsym);
341 #endif
342 return lsym;
343 }
344
345 static void
346 lex_number(void)
347 {
348 for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
349 unsigned char ch = (unsigned char)*inp.s;
350 if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
351 break;
352
353 unsigned char row = lex_number_row[ch];
354 if (lex_number_state[row][s - 'A'] == ' ') {
355 /*-
356 * lex_number_state[0][s - 'A'] now indicates the type:
357 * f = floating, i = integer, u = unknown
358 */
359 break;
360 }
361
362 s = lex_number_state[row][s - 'A'];
363 token_add_char(inbuf_next());
364 }
365 }
366
367 static void
368 lex_word(void)
369 {
370 while (isalnum((unsigned char)*inp.s) ||
371 *inp.s == '\\' ||
372 *inp.s == '_' || *inp.s == '$') {
373
374 if (*inp.s == '\\') {
375 if (inp.s[1] == '\n') {
376 inp.s += 2;
377 if (inp.s >= inp.e)
378 inbuf_read_line();
379 } else
380 break;
381 }
382
383 token_add_char(inbuf_next());
384 }
385 }
386
387 static void
388 lex_char_or_string(void)
389 {
390 for (char delim = *token.s;;) {
391 if (*inp.s == '\n') {
392 diag(1, "Unterminated literal");
393 return;
394 }
395
396 token_add_char(inbuf_next());
397 if (token.e[-1] == delim)
398 return;
399
400 if (token.e[-1] == '\\') {
401 if (*inp.s == '\n')
402 ++line_no;
403 token_add_char(inbuf_next());
404 }
405 }
406 }
407
408 /* Guess whether the current token is a declared type. */
409 static bool
410 probably_typename(void)
411 {
412 if (ps.p_l_follow > 0)
413 return false;
414 if (ps.block_init || ps.in_stmt)
415 return false;
416 if (inp.s[0] == '*' && inp.s[1] != '=')
417 goto maybe;
418 if (isalpha((unsigned char)*inp.s))
419 goto maybe;
420 return false;
421 maybe:
422 return ps.prev_token == lsym_semicolon ||
423 ps.prev_token == lsym_lbrace ||
424 ps.prev_token == lsym_rbrace;
425 }
426
427 static int
428 bsearch_typenames(const char *key)
429 {
430 const char **arr = typenames.items;
431 int lo = 0;
432 int hi = (int)typenames.len - 1;
433
434 while (lo <= hi) {
435 int mid = (int)((unsigned)(lo + hi) >> 1);
436 int cmp = strcmp(arr[mid], key);
437 if (cmp < 0)
438 lo = mid + 1;
439 else if (cmp > 0)
440 hi = mid - 1;
441 else
442 return mid;
443 }
444 return -(lo + 1);
445 }
446
447 static bool
448 is_typename(void)
449 {
450 if (opt.auto_typedefs &&
451 token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
452 return true;
453
454 return bsearch_typenames(token.s) >= 0;
455 }
456
457 static int
458 cmp_keyword_by_name(const void *key, const void *elem)
459 {
460 return strcmp(key, ((const struct keyword *)elem)->name);
461 }
462
463 /* Read an alphanumeric token into 'token', or return end_of_file. */
464 static lexer_symbol
465 lexi_alnum(void)
466 {
467 if (isdigit((unsigned char)*inp.s) ||
468 (inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))) {
469 lex_number();
470 } else if (isalnum((unsigned char)*inp.s) ||
471 *inp.s == '_' || *inp.s == '$') {
472 lex_word();
473 } else
474 return lsym_eof; /* just as a placeholder */
475
476 *token.e = '\0';
477
478 if (token.s[0] == 'L' && token.s[1] == '\0' &&
479 (*inp.s == '"' || *inp.s == '\''))
480 return lsym_string_prefix;
481
482 while (ch_isblank(inbuf_peek()))
483 inbuf_skip();
484
485 if (ps.prev_token == lsym_tag && ps.p_l_follow == 0) {
486 ps.next_unary = true;
487 return lsym_type_at_paren_level_0;
488 }
489
490 /* Operator after identifier is binary unless last token was 'struct'. */
491 ps.next_unary = ps.prev_token == lsym_tag;
492
493 const struct keyword *kw = bsearch(token.s, keywords,
494 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
495 if (kw == NULL) {
496 if (is_typename()) {
497 ps.curr_is_type = true;
498 ps.next_unary = true;
499 goto found_typename;
500 }
501
502 } else { /* we have a keyword */
503 ps.curr_is_type = kw->lsym == lsym_type;
504 ps.next_unary = true;
505 if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
506 return kw->lsym;
507
508 found_typename:
509 if (ps.p_l_follow > 0) {
510 /* inside parentheses: cast, param list, offsetof or sizeof */
511 ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask;
512 }
513 if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
514 if (kw != NULL && kw->lsym == lsym_tag)
515 return lsym_tag;
516 if (ps.p_l_follow == 0)
517 return lsym_type_at_paren_level_0;
518 }
519 }
520
521 if (*inp.s == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
522 !ps.in_parameter_declaration && !ps.block_init) {
523
524 for (const char *p = inp.s; p < inp.e;)
525 if (*p++ == ')' && (*p == ';' || *p == ','))
526 goto no_function_definition;
527
528 strncpy(ps.procname, token.s, sizeof ps.procname - 1);
529 if (ps.in_decl)
530 ps.in_parameter_declaration = true;
531 return lsym_funcname;
532 no_function_definition:;
533
534 } else if (probably_typename()) {
535 ps.curr_is_type = true;
536 ps.next_unary = true;
537 return lsym_type_at_paren_level_0;
538 }
539
540 return lsym_ident; /* the ident is not in the list */
541 }
542
543 /* Reads the next token, placing it in the global variable "token". */
544 lexer_symbol
545 lexi(void)
546 {
547 token.e = token.s;
548 ps.curr_col_1 = ps.curr_newline;
549 ps.curr_newline = false;
550 ps.prev_is_type = ps.curr_is_type;
551 ps.curr_is_type = false;
552
553 while (ch_isblank(*inp.s)) {
554 ps.curr_col_1 = false;
555 inbuf_skip();
556 }
557
558 lexer_symbol alnum_lsym = lexi_alnum();
559 if (alnum_lsym != lsym_eof)
560 return lexi_end(alnum_lsym);
561
562 /* Scan a non-alphanumeric token */
563
564 check_size_token(3); /* for things like "<<=" */
565 *token.e++ = inbuf_next();
566 *token.e = '\0';
567
568 lexer_symbol lsym;
569 bool unary_delim = false; /* whether the current token forces a
570 * following operator to be unary */
571
572 switch (*token.s) {
573 case '\n':
574 unary_delim = ps.next_unary;
575 ps.curr_newline = true;
576 /* if data has been exhausted, the newline is a dummy. */
577 lsym = had_eof ? lsym_eof : lsym_newline;
578 break;
579
580 case '\'':
581 case '"':
582 lex_char_or_string();
583 lsym = lsym_ident;
584 break;
585
586 case '(':
587 case '[':
588 unary_delim = true;
589 lsym = lsym_lparen_or_lbracket;
590 break;
591
592 case ')':
593 case ']':
594 lsym = lsym_rparen_or_rbracket;
595 break;
596
597 case '#':
598 unary_delim = ps.next_unary;
599 lsym = lsym_preprocessing;
600 break;
601
602 case '?':
603 unary_delim = true;
604 lsym = lsym_question;
605 break;
606
607 case ':':
608 lsym = lsym_colon;
609 unary_delim = true;
610 break;
611
612 case ';':
613 unary_delim = true;
614 lsym = lsym_semicolon;
615 break;
616
617 case '{':
618 unary_delim = true;
619 lsym = lsym_lbrace;
620 break;
621
622 case '}':
623 unary_delim = true;
624 lsym = lsym_rbrace;
625 break;
626
627 case '\f':
628 unary_delim = ps.next_unary;
629 ps.curr_newline = true;
630 lsym = lsym_form_feed;
631 break;
632
633 case ',':
634 unary_delim = true;
635 lsym = lsym_comma;
636 break;
637
638 case '.':
639 unary_delim = false;
640 lsym = lsym_period;
641 break;
642
643 case '-':
644 case '+':
645 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
646 unary_delim = true;
647
648 if (*inp.s == token.s[0]) { /* ++, -- */
649 *token.e++ = *inp.s++;
650 if (ps.prev_token == lsym_ident ||
651 ps.prev_token == lsym_rparen_or_rbracket) {
652 lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
653 unary_delim = false;
654 }
655
656 } else if (*inp.s == '=') { /* += */
657 *token.e++ = *inp.s++;
658
659 } else if (*inp.s == '>') { /* -> */
660 *token.e++ = *inp.s++;
661 unary_delim = false;
662 lsym = lsym_unary_op;
663 ps.want_blank = false;
664 }
665 break;
666
667 case '=':
668 if (ps.init_or_struct)
669 ps.block_init = true;
670 if (*inp.s == '=') { /* == */
671 *token.e++ = *inp.s++;
672 *token.e = '\0';
673 }
674 lsym = lsym_binary_op;
675 unary_delim = true;
676 break;
677
678 case '>':
679 case '<':
680 case '!': /* ops like <, <<, <=, !=, etc */
681 if (*inp.s == '>' || *inp.s == '<' || *inp.s == '=')
682 *token.e++ = inbuf_next();
683 if (*inp.s == '=')
684 *token.e++ = *inp.s++;
685 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
686 unary_delim = true;
687 break;
688
689 case '*':
690 unary_delim = true;
691 if (!ps.next_unary) {
692 if (*inp.s == '=')
693 *token.e++ = *inp.s++;
694 lsym = lsym_binary_op;
695 break;
696 }
697
698 while (*inp.s == '*' || isspace((unsigned char)*inp.s)) {
699 if (*inp.s == '*')
700 token_add_char('*');
701 inbuf_skip();
702 }
703
704 if (ps.in_decl) {
705 char *tp = inp.s;
706
707 while (isalpha((unsigned char)*tp) ||
708 isspace((unsigned char)*tp)) {
709 if (++tp >= inp.e)
710 inbuf_read_line();
711 }
712 if (*tp == '(')
713 ps.procname[0] = ' ';
714 }
715
716 lsym = lsym_unary_op;
717 break;
718
719 default:
720 if (token.s[0] == '/' && (*inp.s == '*' || *inp.s == '/')) {
721 /* it is start of comment */
722 *token.e++ = inbuf_next();
723
724 lsym = lsym_comment;
725 unary_delim = ps.next_unary;
726 break;
727 }
728
729 while (token.e[-1] == *inp.s || *inp.s == '=') {
730 /* handle '||', '&&', etc., and also things as in 'int *****i' */
731 token_add_char(inbuf_next());
732 }
733
734 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
735 unary_delim = true;
736 }
737
738 if (inp.s >= inp.e) /* check for input buffer empty */
739 inbuf_read_line();
740
741 ps.next_unary = unary_delim;
742
743 check_size_token(1);
744 *token.e = '\0';
745
746 return lexi_end(lsym);
747 }
748
749 void
750 register_typename(const char *name)
751 {
752 if (typenames.len >= typenames.cap) {
753 typenames.cap = 16 + 2 * typenames.cap;
754 typenames.items = xrealloc(typenames.items,
755 sizeof(typenames.items[0]) * typenames.cap);
756 }
757
758 int pos = bsearch_typenames(name);
759 if (pos >= 0)
760 return; /* already in the list */
761
762 pos = -(pos + 1);
763 memmove(typenames.items + pos + 1, typenames.items + pos,
764 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
765 typenames.items[pos] = xstrdup(name);
766 }
767