Debian Codegraph @

src/output.cc

raw
1 /* Output routines.
2 Copyright (C) 1989-2025 Free Software Foundation, Inc.
3 Written by Douglas C. Schmidt <[email protected]>
4 and Bruno Haible <[email protected]>.
5
6 This file is part of GNU GPERF.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22
23 /* Specification. */
24 #include "output.h"
25
26 #include <stdio.h>
27 #include <string.h> /* declares strncpy(), strchr() */
28 #include <ctype.h> /* declares isprint() */
29 #include <assert.h> /* defines assert() */
30 #include <limits.h> /* defines SCHAR_MAX etc. */
31 #include "options.h"
32 #include "version.h"
33
34 /* ============================== Portability ============================== */
35
36 /* Dynamically allocated array with dynamic extent:
37
38 Example:
39 DYNAMIC_ARRAY (my_array, int, n);
40 ...
41 FREE_DYNAMIC_ARRAY (my_array);
42
43 Attention: depending on your implementation my_array is either the array
44 itself or a pointer to the array! Always use my_array only as expression!
45 */
46 #if HAVE_DYNAMIC_ARRAY
47 #define DYNAMIC_ARRAY(var,eltype,size) eltype var[size]
48 #define FREE_DYNAMIC_ARRAY(var)
49 #else
50 #define DYNAMIC_ARRAY(var,eltype,size) eltype *var = new eltype[size]
51 #define FREE_DYNAMIC_ARRAY(var) delete[] var
52 #endif
53
54 /* ========================================================================= */
55
56 /* The "register " storage-class specifier. */
57 static const char *register_scs;
58
59 /* The "const " qualifier. */
60 static const char *const_always;
61
62 /* The "const " qualifier, for read-only arrays. */
63 static const char *const_readonly_array;
64
65 /* The "const " qualifier, for the array type. */
66 static const char *const_for_struct;
67
68 /* Returns the smallest unsigned C type capable of holding integers
69 up to N. */
70
71 static const char *
72 smallest_integral_type (int n)
73 {
74 if (n <= UCHAR_MAX) return "unsigned char";
75 if (n <= USHRT_MAX) return "unsigned short";
76 return "unsigned int";
77 }
78
79 /* Returns the smallest signed C type capable of holding integers
80 from MIN to MAX. */
81
82 static const char *
83 smallest_integral_type (int min, int max)
84 {
85 if (option[ANSIC] | option[CPLUSPLUS])
86 if (min >= SCHAR_MIN && max <= SCHAR_MAX) return "signed char";
87 if (min >= SHRT_MIN && max <= SHRT_MAX) return "short";
88 return "int";
89 }
90
91 /* ------------------------------------------------------------------------- */
92
93 /* Constructor.
94 Note about the keyword list starting at head:
95 - The list is ordered by increasing _hash_value. This has been achieved
96 by Search::sort().
97 - Duplicates, i.e. keywords with the same _selchars set, are chained
98 through the _duplicate_link pointer. Only one representative per
99 duplicate equivalence class remains on the linear keyword list.
100 - Accidental duplicates, i.e. keywords for which the _asso_values[] search
101 couldn't achieve different hash values, cannot occur on the linear
102 keyword list. Search::optimize would catch this mistake.
103 */
104 Output::Output (KeywordExt_List *head, const char *struct_decl,
105 unsigned int struct_decl_lineno, const char *return_type,
106 const char *struct_tag, const char *verbatim_declarations,
107 const char *verbatim_declarations_end,
108 unsigned int verbatim_declarations_lineno,
109 const char *verbatim_code, const char *verbatim_code_end,
110 unsigned int verbatim_code_lineno, bool charset_dependent,
111 int total_keys, int max_key_len, int min_key_len,
112 bool hash_includes_len, const Positions& positions,
113 const unsigned int *alpha_inc, int total_duplicates,
114 unsigned int alpha_size, const int *asso_values)
115 : _head (head), _struct_decl (struct_decl),
116 _struct_decl_lineno (struct_decl_lineno), _return_type (return_type),
117 _struct_tag (struct_tag),
118 _verbatim_declarations (verbatim_declarations),
119 _verbatim_declarations_end (verbatim_declarations_end),
120 _verbatim_declarations_lineno (verbatim_declarations_lineno),
121 _verbatim_code (verbatim_code),
122 _verbatim_code_end (verbatim_code_end),
123 _verbatim_code_lineno (verbatim_code_lineno),
124 _charset_dependent (charset_dependent),
125 _total_keys (total_keys),
126 _max_key_len (max_key_len), _min_key_len (min_key_len),
127 _hash_includes_len (hash_includes_len),
128 _key_positions (positions), _alpha_inc (alpha_inc),
129 _total_duplicates (total_duplicates), _alpha_size (alpha_size),
130 _asso_values (asso_values)
131 {
132 }
133
134 /* ------------------------------------------------------------------------- */
135
136 /* Computes the minimum and maximum hash values, and stores them
137 in _min_hash_value and _max_hash_value. */
138
139 void
140 Output::compute_min_max ()
141 {
142 /* Since the list is already sorted by hash value all we need to do is
143 to look at the first and the last element of the list. */
144
145 _min_hash_value = _head->first()->_hash_value;
146
147 KeywordExt_List *temp;
148 for (temp = _head; temp->rest(); temp = temp->rest())
149 ;
150 _max_hash_value = temp->first()->_hash_value;
151 }
152
153 /* ------------------------------------------------------------------------- */
154
155 /* Returns the number of different hash values. */
156
157 int
158 Output::num_hash_values () const
159 {
160 /* Since the list is already sorted by hash value and doesn't contain
161 duplicates, we can simply count the number of keywords on the list. */
162 int count = 0;
163 for (KeywordExt_List *temp = _head; temp; temp = temp->rest())
164 count++;
165 return count;
166 }
167
168 /* -------------------- Output_Constants and subclasses -------------------- */
169
170 /* This class outputs an enumeration defining some constants. */
171
172 struct Output_Constants
173 {
174 virtual void output_start () = 0;
175 virtual void output_item (const char *name, int value) = 0;
176 virtual void output_end () = 0;
177 Output_Constants () {}
178 virtual ~Output_Constants () {}
179 };
180
181 /* This class outputs an enumeration in #define syntax. */
182
183 struct Output_Defines : public Output_Constants
184 {
185 virtual void output_start ();
186 virtual void output_item (const char *name, int value);
187 virtual void output_end ();
188 Output_Defines () {}
189 virtual ~Output_Defines () {}
190 };
191
192 void Output_Defines::output_start ()
193 {
194 printf ("\n");
195 }
196
197 void Output_Defines::output_item (const char *name, int value)
198 {
199 printf ("#define %s %d\n", name, value);
200 }
201
202 void Output_Defines::output_end ()
203 {
204 }
205
206 /* This class outputs an enumeration using 'enum'. */
207
208 struct Output_Enum : public Output_Constants
209 {
210 virtual void output_start ();
211 virtual void output_item (const char *name, int value);
212 virtual void output_end ();
213 Output_Enum (const char *indent)
214 : _indentation (indent) {}
215 virtual ~Output_Enum () {}
216 private:
217 const char *_indentation;
218 bool _pending_comma;
219 };
220
221 void Output_Enum::output_start ()
222 {
223 printf ("%senum\n"
224 "%s {\n",
225 _indentation, _indentation);
226 _pending_comma = false;
227 }
228
229 void Output_Enum::output_item (const char *name, int value)
230 {
231 if (_pending_comma)
232 printf (",\n");
233 printf ("%s %s = %d", _indentation, name, value);
234 _pending_comma = true;
235 }
236
237 void Output_Enum::output_end ()
238 {
239 if (_pending_comma)
240 printf ("\n");
241 printf ("%s };\n\n", _indentation);
242 }
243
244 /* Outputs a constant in the given style. */
245
246 static void
247 output_constant (struct Output_Constants& style, const char *name, int value)
248 {
249 const char *prefix = option.get_constants_prefix ();
250 DYNAMIC_ARRAY (combined_name, char, strlen (prefix) + strlen (name) + 1);
251 strcpy (combined_name, prefix);
252 strcpy (combined_name + strlen (prefix), name);
253 style.output_item (combined_name, value);
254 FREE_DYNAMIC_ARRAY (combined_name);
255 }
256
257 /* Outputs the maximum and minimum hash values etc. */
258
259 void
260 Output::output_constants (struct Output_Constants& style) const
261 {
262 style.output_start ();
263 output_constant (style, "TOTAL_KEYWORDS", _total_keys);
264 output_constant (style, "MIN_WORD_LENGTH", _min_key_len);
265 output_constant (style, "MAX_WORD_LENGTH", _max_key_len);
266 output_constant (style, "MIN_HASH_VALUE", _min_hash_value);
267 output_constant (style, "MAX_HASH_VALUE", _max_hash_value);
268 style.output_end ();
269 }
270
271 /* ------------------------------------------------------------------------- */
272
273 /* We use a downcase table because when called repeatedly, the code
274 gperf_downcase[c]
275 is faster than
276 if (c >= 'A' && c <= 'Z')
277 c += 'a' - 'A';
278 */
279 #define USE_DOWNCASE_TABLE 1
280
281 #if USE_DOWNCASE_TABLE
282
283 /* Output gperf's ASCII-downcase table. */
284
285 static void
286 output_upperlower_table ()
287 {
288 unsigned int c;
289
290 printf ("#ifndef GPERF_DOWNCASE\n"
291 "#define GPERF_DOWNCASE 1\n"
292 "static %sunsigned char gperf_downcase[256] =\n"
293 " {",
294 const_readonly_array);
295 for (c = 0; c < 256; c++)
296 {
297 if ((c % 15) == 0)
298 printf ("\n ");
299 printf (" %3d", c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c);
300 if (c < 255)
301 printf (",");
302 }
303 printf ("\n"
304 " };\n"
305 "#endif\n\n");
306 }
307
308 #endif
309
310 /* Output gperf's ASCII-case insensitive strcmp replacement. */
311
312 static void
313 output_upperlower_strcmp ()
314 {
315 printf ("#ifndef GPERF_CASE_STRCMP\n"
316 "#define GPERF_CASE_STRCMP 1\n"
317 "static int\n"
318 "gperf_case_strcmp ");
319 printf (option[KRC] ?
320 "(s1, s2)\n"
321 " %schar *s1;\n"
322 " %schar *s2;\n" :
323 option[C] ?
324 "(s1, s2)\n"
325 " %sconst char *s1;\n"
326 " %sconst char *s2;\n" :
327 option[ANSIC] | option[CPLUSPLUS] ?
328 "(%sconst char *s1, %sconst char *s2)\n" :
329 "",
330 register_scs, register_scs);
331 #if USE_DOWNCASE_TABLE
332 printf ("{\n"
333 " for (;;)\n"
334 " {\n"
335 " unsigned char c1 = gperf_downcase[(unsigned char)*s1++];\n"
336 " unsigned char c2 = gperf_downcase[(unsigned char)*s2++];\n"
337 " if (c1 != 0 && c1 == c2)\n"
338 " continue;\n"
339 " return (int)c1 - (int)c2;\n"
340 " }\n"
341 "}\n");
342 #else
343 printf ("{\n"
344 " for (;;)\n"
345 " {\n"
346 " unsigned char c1 = *s1++;\n"
347 " unsigned char c2 = *s2++;\n"
348 " if (c1 >= 'A' && c1 <= 'Z')\n"
349 " c1 += 'a' - 'A';\n"
350 " if (c2 >= 'A' && c2 <= 'Z')\n"
351 " c2 += 'a' - 'A';\n"
352 " if (c1 != 0 && c1 == c2)\n"
353 " continue;\n"
354 " return (int)c1 - (int)c2;\n"
355 " }\n"
356 "}\n");
357 #endif
358 printf ("#endif\n\n");
359 }
360
361 /* Output gperf's ASCII-case insensitive strncmp replacement. */
362
363 static void
364 output_upperlower_strncmp ()
365 {
366 printf ("#ifndef GPERF_CASE_STRNCMP\n"
367 "#define GPERF_CASE_STRNCMP 1\n"
368 "static int\n"
369 "gperf_case_strncmp ");
370 printf (option[KRC] ?
371 "(s1, s2, n)\n"
372 " %schar *s1;\n"
373 " %schar *s2;\n"
374 " %ssize_t n;\n" :
375 option[C] ?
376 "(s1, s2, n)\n"
377 " %sconst char *s1;\n"
378 " %sconst char *s2;\n"
379 " %ssize_t n;\n" :
380 option[ANSIC] | option[CPLUSPLUS] ?
381 "(%sconst char *s1, %sconst char *s2, %ssize_t n)\n" :
382 "",
383 register_scs, register_scs, register_scs);
384 #if USE_DOWNCASE_TABLE
385 printf ("{\n"
386 " for (; n > 0;)\n"
387 " {\n"
388 " unsigned char c1 = gperf_downcase[(unsigned char)*s1++];\n"
389 " unsigned char c2 = gperf_downcase[(unsigned char)*s2++];\n"
390 " if (c1 != 0 && c1 == c2)\n"
391 " {\n"
392 " n--;\n"
393 " continue;\n"
394 " }\n"
395 " return (int)c1 - (int)c2;\n"
396 " }\n"
397 " return 0;\n"
398 "}\n");
399 #else
400 printf ("{\n"
401 " for (; n > 0;)\n"
402 " {\n"
403 " unsigned char c1 = *s1++;\n"
404 " unsigned char c2 = *s2++;\n"
405 " if (c1 >= 'A' && c1 <= 'Z')\n"
406 " c1 += 'a' - 'A';\n"
407 " if (c2 >= 'A' && c2 <= 'Z')\n"
408 " c2 += 'a' - 'A';\n"
409 " if (c1 != 0 && c1 == c2)\n"
410 " {\n"
411 " n--;\n"
412 " continue;\n"
413 " }\n"
414 " return (int)c1 - (int)c2;\n"
415 " }\n"
416 " return 0;\n"
417 "}\n");
418 #endif
419 printf ("#endif\n\n");
420 }
421
422 /* Output gperf's ASCII-case insensitive memcmp replacement. */
423
424 static void
425 output_upperlower_memcmp ()
426 {
427 printf ("#ifndef GPERF_CASE_MEMCMP\n"
428 "#define GPERF_CASE_MEMCMP 1\n"
429 "static int\n"
430 "gperf_case_memcmp ");
431 printf (option[KRC] ?
432 "(s1, s2, n)\n"
433 " %schar *s1;\n"
434 " %schar *s2;\n"
435 " %ssize_t n;\n" :
436 option[C] ?
437 "(s1, s2, n)\n"
438 " %sconst char *s1;\n"
439 " %sconst char *s2;\n"
440 " %ssize_t n;\n" :
441 option[ANSIC] | option[CPLUSPLUS] ?
442 "(%sconst char *s1, %sconst char *s2, %ssize_t n)\n" :
443 "",
444 register_scs, register_scs, register_scs);
445 #if USE_DOWNCASE_TABLE
446 printf ("{\n"
447 " for (; n > 0;)\n"
448 " {\n"
449 " unsigned char c1 = gperf_downcase[(unsigned char)*s1++];\n"
450 " unsigned char c2 = gperf_downcase[(unsigned char)*s2++];\n"
451 " if (c1 == c2)\n"
452 " {\n"
453 " n--;\n"
454 " continue;\n"
455 " }\n"
456 " return (int)c1 - (int)c2;\n"
457 " }\n"
458 " return 0;\n"
459 "}\n");
460 #else
461 printf ("{\n"
462 " for (; n > 0;)\n"
463 " {\n"
464 " unsigned char c1 = *s1++;\n"
465 " unsigned char c2 = *s2++;\n"
466 " if (c1 >= 'A' && c1 <= 'Z')\n"
467 " c1 += 'a' - 'A';\n"
468 " if (c2 >= 'A' && c2 <= 'Z')\n"
469 " c2 += 'a' - 'A';\n"
470 " if (c1 == c2)\n"
471 " {\n"
472 " n--;\n"
473 " continue;\n"
474 " }\n"
475 " return (int)c1 - (int)c2;\n"
476 " }\n"
477 " return 0;\n"
478 "}\n");
479 #endif
480 printf ("#endif\n\n");
481 }
482
483 /* ------------------------------------------------------------------------- */
484
485 /* Outputs a keyword, as a string: enclosed in double quotes, escaping
486 backslashes, double quote and unprintable characters. */
487
488 static void
489 output_string (const char *key, int len)
490 {
491 putchar ('"');
492 for (; len > 0; len--)
493 {
494 unsigned char c = static_cast<unsigned char>(*key++);
495 if (isprint (c))
496 {
497 if (c == '"' || c == '\\')
498 putchar ('\\');
499 putchar (c);
500 }
501 else
502 {
503 /* Use octal escapes, not hexadecimal escapes, because some old
504 C compilers didn't understand hexadecimal escapes, and because
505 hexadecimal escapes are not limited to 2 digits, thus needing
506 special care if the following character happens to be a digit. */
507 putchar ('\\');
508 putchar ('0' + ((c >> 6) & 7));
509 putchar ('0' + ((c >> 3) & 7));
510 putchar ('0' + (c & 7));
511 }
512 }
513 putchar ('"');
514 }
515
516 /* ------------------------------------------------------------------------- */
517
518 /* Outputs a #line directive, referring to the given line number. */
519
520 static void
521 output_line_directive (unsigned int lineno)
522 {
523 const char *file_name = option.get_input_file_name ();
524 if (file_name != NULL)
525 {
526 printf ("#line %u ", lineno);
527 output_string (file_name, strlen (file_name));
528 printf ("\n");
529 }
530 }
531
532 /* ------------------------------------------------------------------------- */
533
534 /* Outputs a type and a const specifier (i.e. "const " or "").
535 The output is terminated with a space. */
536
537 static void
538 output_const_type (const char *const_string, const char *type_string)
539 {
540 if (type_string[strlen(type_string)-1] == '*')
541 /* For pointer types, put the 'const' after the type. */
542 printf ("%s %s", type_string, const_string);
543 else
544 /* For scalar or struct types, put the 'const' before the type. */
545 printf ("%s%s ", const_string, type_string);
546 }
547
548 /* ----------------------- Output_Expr and subclasses ----------------------- */
549
550 /* This class outputs a general expression. */
551
552 struct Output_Expr
553 {
554 virtual void output_expr () const = 0;
555 Output_Expr () {}
556 virtual ~Output_Expr () {}
557 };
558
559 /* This class outputs an expression formed by a single string. */
560
561 struct Output_Expr1 : public Output_Expr
562 {
563 virtual void output_expr () const;
564 Output_Expr1 (const char *piece1) : _p1 (piece1) {}
565 virtual ~Output_Expr1 () {}
566 private:
567 const char *_p1;
568 };
569
570 void Output_Expr1::output_expr () const
571 {
572 printf ("%s", _p1);
573 }
574
575 #if 0 /* unused */
576
577 /* This class outputs an expression formed by the concatenation of two
578 strings. */
579
580 struct Output_Expr2 : public Output_Expr
581 {
582 virtual void output_expr () const;
583 Output_Expr2 (const char *piece1, const char *piece2)
584 : _p1 (piece1), _p2 (piece2) {}
585 virtual ~Output_Expr2 () {}
586 private:
587 const char *_p1;
588 const char *_p2;
589 };
590
591 void Output_Expr2::output_expr () const
592 {
593 printf ("%s%s", _p1, _p2);
594 }
595
596 #endif
597
598 /* --------------------- Output_Compare and subclasses --------------------- */
599
600 /* This class outputs a comparison expression. */
601
602 struct Output_Compare
603 {
604 /* Outputs the comparison expression.
605 expr1 outputs a simple expression of type 'const char *' referring to
606 the string being looked up. expr2 outputs a simple expression of type
607 'const char *' referring to the constant string stored in the gperf
608 generated hash table. */
609 virtual void output_comparison (const Output_Expr& expr1,
610 const Output_Expr& expr2) const = 0;
611 /* Outputs the comparison expression for the first byte.
612 Returns true if the this comparison is complete. */
613 bool output_firstchar_comparison (const Output_Expr& expr1,
614 const Output_Expr& expr2) const;
615 Output_Compare () {}
616 virtual ~Output_Compare () {}
617 };
618
619 bool Output_Compare::output_firstchar_comparison (const Output_Expr& expr1,
620 const Output_Expr& expr2) const
621 {
622 /* First, we emit a comparison of the first byte of the two strings.
623 This catches most cases where the string being looked up is not in the
624 hash table but happens to have the same hash code as an element of the
625 hash table. */
626 if (option[UPPERLOWER])
627 {
628 /* Incomplete comparison, just for speedup. */
629 printf ("(((unsigned char)*");
630 expr1.output_expr ();
631 printf (" ^ (unsigned char)*");
632 expr2.output_expr ();
633 printf (") & ~32) == 0");
634 return false;
635 }
636 else
637 {
638 /* Complete comparison. */
639 printf ("*");
640 expr1.output_expr ();
641 printf (" == *");
642 expr2.output_expr ();
643 return true;
644 }
645 }
646
647 /* This class outputs a comparison using strcmp. */
648
649 struct Output_Compare_Strcmp : public Output_Compare
650 {
651 virtual void output_comparison (const Output_Expr& expr1,
652 const Output_Expr& expr2) const;
653 Output_Compare_Strcmp () {}
654 virtual ~Output_Compare_Strcmp () {}
655 };
656
657 void Output_Compare_Strcmp::output_comparison (const Output_Expr& expr1,
658 const Output_Expr& expr2) const
659 {
660 bool firstchar_done = output_firstchar_comparison (expr1, expr2);
661 printf (" && !");
662 if (option[UPPERLOWER])
663 printf ("gperf_case_");
664 printf ("strcmp (");
665 if (firstchar_done)
666 {
667 expr1.output_expr ();
668 printf (" + 1, ");
669 expr2.output_expr ();
670 printf (" + 1");
671 }
672 else
673 {
674 expr1.output_expr ();
675 printf (", ");
676 expr2.output_expr ();
677 }
678 printf (")");
679 }
680
681 /* This class outputs a comparison using strncmp.
682 Note that the length of expr1 will be available through the local variable
683 'len'. */
684
685 struct Output_Compare_Strncmp : public Output_Compare
686 {
687 virtual void output_comparison (const Output_Expr& expr1,
688 const Output_Expr& expr2) const;
689 Output_Compare_Strncmp () {}
690 virtual ~Output_Compare_Strncmp () {}
691 };
692
693 void Output_Compare_Strncmp::output_comparison (const Output_Expr& expr1,
694 const Output_Expr& expr2) const
695 {
696 bool firstchar_done = output_firstchar_comparison (expr1, expr2);
697 printf (" && !");
698 if (option[UPPERLOWER])
699 printf ("gperf_case_");
700 printf ("strncmp (");
701 if (firstchar_done)
702 {
703 expr1.output_expr ();
704 printf (" + 1, ");
705 expr2.output_expr ();
706 printf (" + 1, len - 1");
707 }
708 else
709 {
710 expr1.output_expr ();
711 printf (", ");
712 expr2.output_expr ();
713 printf (", len");
714 }
715 printf (") && ");
716 expr2.output_expr ();
717 printf ("[len] == '\\0'");
718 }
719
720 /* This class outputs a comparison using memcmp.
721 Note that the length of expr1 (available through the local variable 'len')
722 must be verified to be equal to the length of expr2 prior to this
723 comparison. */
724
725 struct Output_Compare_Memcmp : public Output_Compare
726 {
727 virtual void output_comparison (const Output_Expr& expr1,
728 const Output_Expr& expr2) const;
729 Output_Compare_Memcmp () {}
730 virtual ~Output_Compare_Memcmp () {}
731 };
732
733 void Output_Compare_Memcmp::output_comparison (const Output_Expr& expr1,
734 const Output_Expr& expr2) const
735 {
736 bool firstchar_done = output_firstchar_comparison (expr1, expr2);
737 printf (" && !");
738 if (option[UPPERLOWER])
739 printf ("gperf_case_");
740 printf ("memcmp (");
741 if (firstchar_done)
742 {
743 expr1.output_expr ();
744 printf (" + 1, ");
745 expr2.output_expr ();
746 printf (" + 1, len - 1");
747 }
748 else
749 {
750 expr1.output_expr ();
751 printf (", ");
752 expr2.output_expr ();
753 printf (", len");
754 }
755 printf (")");
756 }
757
758 /* ------------------------------------------------------------------------- */
759
760 /* Generates a C expression for an asso_values[] index. */
761
762 void
763 Output::output_asso_values_index (int pos) const
764 {
765 if (pos == Positions::LASTCHAR)
766 printf ("str[len - 1]");
767 else
768 {
769 printf ("str[%d]", pos);
770 if (_alpha_inc[pos])
771 printf ("+%u", _alpha_inc[pos]);
772 }
773 }
774
775 /* Generates a C expression for an asso_values[] reference. */
776
777 void
778 Output::output_asso_values_ref (int pos) const
779 {
780 printf ("asso_values[");
781 /* Always cast to unsigned char. This is necessary when the alpha_inc
782 is nonzero, and also avoids a gcc warning "subscript has type 'char'". */
783 if (option[CPLUSPLUS])
784 {
785 /* In C++, a C style cast may lead to a 'warning: use of old-style cast'.
786 Therefore prefer the C++ style cast syntax. */
787 printf ("static_cast<unsigned char>(");
788 output_asso_values_index (pos);
789 printf (")");
790 }
791 else
792 {
793 printf ("(unsigned char)");
794 output_asso_values_index (pos);
795 }
796 printf ("]");
797 }
798
799 /* Generates C code for the hash function that returns the
800 proper encoding for each keyword.
801 The hash function has the signature
802 unsigned int <hash> (const char *str, size_t len). */
803
804 void
805 Output::output_hash_function () const
806 {
807 /* Output the function's head. */
808 if (option[CPLUSPLUS])
809 printf ("inline ");
810 else if (option[KRC] | option[C] | option[ANSIC])
811 printf ("#ifdef __GNUC__\n"
812 "__inline\n"
813 "#else\n"
814 "#ifdef __cplusplus\n"
815 "inline\n"
816 "#endif\n"
817 "#endif\n");
818
819 /* Does the function use the 'str' argument? */
820 bool uses_str = (_key_positions.get_size() > 0);
821 /* Does the function use the 'len' argument? */
822 bool uses_len =
823 (_hash_includes_len
824 || (_key_positions.get_size() > 0
825 && (_key_positions[0] >= _min_key_len
826 || _key_positions[_key_positions.get_size() - 1] == Positions::LASTCHAR)));
827 if (!uses_str || !uses_len)
828 /* Pacify lint. */
829 printf ("/*ARGSUSED*/\n");
830
831 if (option[KRC] | option[C] | option[ANSIC])
832 printf ("static ");
833 printf ("unsigned int\n");
834 if (option[CPLUSPLUS])
835 printf ("%s::", option.get_class_name ());
836 printf ("%s ", option.get_hash_name ());
837 /* We better not use [[__maybe_unused__]] or __attribute__ ((__unused__))
838 because support for these syntaxes in the compilers is constantly
839 changing. */
840 printf (option[KRC] ?
841 "(str, len)\n"
842 " %schar *str;\n"
843 " %ssize_t len;\n" :
844 option[C] ?
845 "(str, len)\n"
846 " %sconst char *str;\n"
847 " %ssize_t len;\n" :
848 option[ANSIC] | option[CPLUSPLUS] ?
849 "(%sconst char *str, %ssize_t len)\n" :
850 "",
851 register_scs, register_scs);
852
853 /* Note that when the hash function is called, it has already been verified
854 that min_key_len <= len <= max_key_len. */
855
856 /* Output the function's body. */
857 printf ("{\n");
858
859 /* First the asso_values array. */
860 if (_key_positions.get_size() > 0)
861 {
862 /* The values in the asso_values array are all unsigned integers
863 <= MAX_HASH_VALUE + 1. */
864 printf (" static %s%s asso_values[] =\n"
865 " {",
866 const_readonly_array,
867 smallest_integral_type (_max_hash_value + 1));
868
869 const int columns = 10;
870
871 /* Calculate maximum number of digits required for MAX_HASH_VALUE + 1. */
872 int field_width = 2;
873 for (int trunc = _max_hash_value + 1; (trunc /= 10) > 0;)
874 field_width++;
875
876 for (unsigned int count = 0; count < _alpha_size; count++)
877 {
878 if (count > 0)
879 printf (",");
880 if ((count % columns) == 0)
881 printf ("\n ");
882 printf ("%*d", field_width, _asso_values[count]);
883 }
884
885 printf ("\n"
886 " };\n");
887 }
888
889 if (!uses_str)
890 /* The function does not use the 'str' argument.
891 Silence "gcc -Wunused-parameter". */
892 printf (" (void) str;\n");
893 if (!uses_len)
894 /* The function does not use the 'len' argument.
895 Silence "gcc -Wunused-parameter". */
896 printf (" (void) len;\n");
897
898 if (_key_positions.get_size() == 0)
899 {
900 /* Trivial case: No key positions at all. */
901 printf (" return %s;\n",
902 _hash_includes_len ? "len" : "0");
903 }
904 else
905 {
906 /* Iterate through the key positions. Remember that Positions::sort()
907 has sorted them in decreasing order, with Positions::LASTCHAR coming
908 last. */
909 PositionIterator iter = _key_positions.iterator(_max_key_len);
910 int key_pos;
911
912 /* Get the highest key position. */
913 key_pos = iter.next ();
914
915 if (key_pos == Positions::LASTCHAR || key_pos < _min_key_len)
916 {
917 /* We can perform additional optimizations here:
918 Write it out as a single expression. Note that the values
919 are added as 'int's even though the asso_values array may
920 contain 'unsigned char's or 'unsigned short's. */
921
922 printf (" return %s",
923 _hash_includes_len ? "len + " : "");
924
925 if (_key_positions.get_size() == 2
926 && _key_positions[0] == 0
927 && _key_positions[1] == Positions::LASTCHAR)
928 /* Optimize special case of "-k 1,$". */
929 {
930 output_asso_values_ref (Positions::LASTCHAR);
931 printf (" + ");
932 output_asso_values_ref (0);
933 }
934 else
935 {
936 for (; key_pos != Positions::LASTCHAR; )
937 {
938 output_asso_values_ref (key_pos);
939 if ((key_pos = iter.next ()) != PositionIterator::EOS)
940 printf (" + ");
941 else
942 break;
943 }
944
945 if (key_pos == Positions::LASTCHAR)
946 output_asso_values_ref (Positions::LASTCHAR);
947 }
948
949 printf (";\n");
950 }
951 else
952 {
953 /* We've got to use the correct, but brute force, technique. */
954 /* Pseudo-statement or comment that avoids a compiler warning or
955 lint warning. */
956 const char * const fallthrough_marker =
957 "#if (defined __cplusplus && (__cplusplus >= 201703L || (__cplusplus >= 201103L && defined __clang__ && __clang_major__ + (__clang_minor__ >= 9) > 3))) || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 202000L && ((defined __GNUC__ && __GNUC__ >= 10) || (defined __clang__ && __clang_major__ >= 9)))\n"
958 " [[fallthrough]];\n"
959 "#elif (defined __GNUC__ && __GNUC__ >= 7) || (defined __clang__ && __clang_major__ >= 10)\n"
960 " __attribute__ ((__fallthrough__));\n"
961 "#endif\n"
962 " /*FALLTHROUGH*/\n";
963 /* It doesn't really matter whether hval is an 'int' or
964 'unsigned int', but 'unsigned int' gives fewer warnings. */
965 printf (" %sunsigned int hval = %s;\n\n"
966 " switch (%s)\n"
967 " {\n"
968 " default:\n",
969 register_scs, _hash_includes_len ? "len" : "0",
970 _hash_includes_len ? "hval" : "len");
971
972 while (key_pos != Positions::LASTCHAR && key_pos >= _max_key_len)
973 if ((key_pos = iter.next ()) == PositionIterator::EOS)
974 break;
975
976 if (key_pos != PositionIterator::EOS && key_pos != Positions::LASTCHAR)
977 {
978 int i = key_pos;
979 do
980 {
981 if (i > key_pos)
982 printf ("%s", fallthrough_marker);
983 for ( ; i > key_pos; i--)
984 printf (" case %d:\n", i);
985
986 printf (" hval += ");
987 output_asso_values_ref (key_pos);
988 printf (";\n");
989
990 key_pos = iter.next ();
991 }
992 while (key_pos != PositionIterator::EOS && key_pos != Positions::LASTCHAR);
993
994 if (i >= _min_key_len)
995 printf ("%s", fallthrough_marker);
996 for ( ; i >= _min_key_len; i--)
997 printf (" case %d:\n", i);
998 }
999
1000 printf (" break;\n"
1001 " }\n"
1002 " return hval");
1003 if (key_pos == Positions::LASTCHAR)
1004 {
1005 printf (" + ");
1006 output_asso_values_ref (Positions::LASTCHAR);
1007 }
1008 printf (";\n");
1009 }
1010 }
1011 printf ("}\n\n");
1012 }
1013
1014 /* ------------------------------------------------------------------------- */
1015
1016 /* Prints out a table of keyword lengths, for use with the
1017 comparison code in generated function 'in_word_set'.
1018 Only called if option[LENTABLE]. */
1019
1020 void
1021 Output::output_keylength_table () const
1022 {
1023 const int columns = 14;
1024 const char * const indent = option[GLOBAL] ? "" : " ";
1025
1026 printf ("%sstatic %s%s %s[] =\n"
1027 "%s {",
1028 indent, const_readonly_array,
1029 smallest_integral_type (_max_key_len),
1030 option.get_lengthtable_name (),
1031 indent);
1032
1033 /* Generate an array of lengths, similar to output_keyword_table. */
1034 int index;
1035 int column;
1036 KeywordExt_List *temp;
1037
1038 column = 0;
1039 for (temp = _head, index = 0; temp; temp = temp->rest())
1040 {
1041 KeywordExt *keyword = temp->first();
1042
1043 /* If generating a switch statement, and there is no user defined type,
1044 we generate non-duplicates directly in the code. Only duplicates go
1045 into the table. */
1046 if (option[SWITCH] && !option[TYPE] && !keyword->_duplicate_link)
1047 continue;
1048
1049 if (index < keyword->_hash_value && !option[SWITCH] && !option[DUP])
1050 {
1051 /* Some blank entries. */
1052 for ( ; index < keyword->_hash_value; index++)
1053 {
1054 if (index > 0)
1055 printf (",");
1056 if ((column++ % columns) == 0)
1057 printf ("\n%s ", indent);
1058 printf ("%3d", 0);
1059 }
1060 }
1061
1062 if (index > 0)
1063 printf (",");
1064 if ((column++ % columns) == 0)
1065 printf("\n%s ", indent);
1066 printf ("%3d", keyword->_allchars_length);
1067 index++;
1068
1069 /* Deal with duplicates specially. */
1070 if (keyword->_duplicate_link) // implies option[DUP]
1071 for (KeywordExt *links = keyword->_duplicate_link; links; links = links->_duplicate_link)
1072 {
1073 printf (",");
1074 if ((column++ % columns) == 0)
1075 printf("\n%s ", indent);
1076 printf ("%3d", links->_allchars_length);
1077 index++;
1078 }
1079 }
1080
1081 printf ("\n%s };\n", indent);
1082 if (option[GLOBAL])
1083 printf ("\n");
1084 }
1085
1086 /* ------------------------------------------------------------------------- */
1087
1088 /* Prints out the string pool, containing the strings of the keyword table.
1089 Only called if option[SHAREDLIB]. */
1090
1091 void
1092 Output::output_string_pool () const
1093 {
1094 const char * const indent = option[TYPE] || option[GLOBAL] ? "" : " ";
1095 int index;
1096 KeywordExt_List *temp;
1097
1098 printf ("%sstruct %s_t\n"
1099 "%s {\n",
1100 indent, option.get_stringpool_name (), indent);
1101 for (temp = _head, index = 0; temp; temp = temp->rest())
1102 {
1103 KeywordExt *keyword = temp->first();
1104
1105 /* If generating a switch statement, and there is no user defined type,
1106 we generate non-duplicates directly in the code. Only duplicates go
1107 into the table. */
1108 if (option[SWITCH] && !option[TYPE] && !keyword->_duplicate_link)
1109 continue;
1110
1111 if (!option[SWITCH] && !option[DUP])
1112 index = keyword->_hash_value;
1113
1114 printf ("%s char %s_str%d[sizeof(",
1115 indent, option.get_stringpool_name (), index);
1116 output_string (keyword->_allchars, keyword->_allchars_length);
1117 printf (")];\n");
1118
1119 /* Deal with duplicates specially. */
1120 if (keyword->_duplicate_link) // implies option[DUP]
1121 for (KeywordExt *links = keyword->_duplicate_link; links; links = links->_duplicate_link)
1122 if (!(links->_allchars_length == keyword->_allchars_length
1123 && memcmp (links->_allchars, keyword->_allchars,
1124 keyword->_allchars_length) == 0))
1125 {
1126 index++;
1127 printf ("%s char %s_str%d[sizeof(",
1128 indent, option.get_stringpool_name (), index);
1129 output_string (links->_allchars, links->_allchars_length);
1130 printf (")];\n");
1131 }
1132
1133 index++;
1134 }
1135 printf ("%s };\n",
1136 indent);
1137
1138 printf ("%sstatic %sstruct %s_t %s_contents =\n"
1139 "%s {\n",
1140 indent, const_readonly_array, option.get_stringpool_name (),
1141 option.get_stringpool_name (), indent);
1142 for (temp = _head, index = 0; temp; temp = temp->rest())
1143 {
1144 KeywordExt *keyword = temp->first();
1145
1146 /* If generating a switch statement, and there is no user defined type,
1147 we generate non-duplicates directly in the code. Only duplicates go
1148 into the table. */
1149 if (option[SWITCH] && !option[TYPE] && !keyword->_duplicate_link)
1150 continue;
1151
1152 if (index > 0)
1153 printf (",\n");
1154
1155 if (!option[SWITCH] && !option[DUP])
1156 index = keyword->_hash_value;
1157
1158 printf ("%s ",
1159 indent);
1160 output_string (keyword->_allchars, keyword->_allchars_length);
1161
1162 /* Deal with duplicates specially. */
1163 if (keyword->_duplicate_link) // implies option[DUP]
1164 for (KeywordExt *links = keyword->_duplicate_link; links; links = links->_duplicate_link)
1165 if (!(links->_allchars_length == keyword->_allchars_length
1166 && memcmp (links->_allchars, keyword->_allchars,
1167 keyword->_allchars_length) == 0))
1168 {
1169 index++;
1170 printf (",\n");
1171 printf ("%s ",
1172 indent);
1173 output_string (links->_allchars, links->_allchars_length);
1174 }
1175
1176 index++;
1177 }
1178 if (index > 0)
1179 printf ("\n");
1180 printf ("%s };\n",
1181 indent);
1182 printf ("%s#define %s ((%schar *) &%s_contents)\n",
1183 indent, option.get_stringpool_name (), const_always,
1184 option.get_stringpool_name ());
1185 if (option[GLOBAL])
1186 printf ("\n");
1187 }
1188
1189 /* ------------------------------------------------------------------------- */
1190
1191 static void
1192 output_keyword_entry (KeywordExt *temp, int stringpool_index, const char *indent, bool is_duplicate)
1193 {
1194 if (option[TYPE])
1195 output_line_directive (temp->_lineno);
1196 printf ("%s ", indent);
1197 if (option[TYPE])
1198 printf ("{");
1199 if (option[SHAREDLIB])
1200 /* How to determine a certain offset in stringpool at compile time?
1201 - The standard way would be to use the 'offsetof' macro. But it is only
1202 defined in <stddef.h>, and <stddef.h> is not among the prerequisite
1203 header files that the user must #include.
1204 - The next best way would be to take the address and cast to 'intptr_t'
1205 or 'uintptr_t'. But these types are only defined in <stdint.h>, and
1206 <stdint.h> is not among the prerequisite header files that the user
1207 must #include.
1208 - The next best approximation of 'uintptr_t' is 'size_t'. It is defined
1209 in the prerequisite header <string.h>.
1210 - The types 'long' and 'unsigned long' do work as well, but on 64-bit
1211 native Windows platforms, they don't have the same size as pointers
1212 and therefore generate warnings. */
1213 printf ("(int)(size_t)&((struct %s_t *)0)->%s_str%d",
1214 option.get_stringpool_name (), option.get_stringpool_name (),
1215 stringpool_index);
1216 else
1217 output_string (temp->_allchars, temp->_allchars_length);
1218 if (option[TYPE])
1219 {
1220 if (strlen (temp->_rest) > 0)
1221 printf (",%s", temp->_rest);
1222 printf ("}");
1223 }
1224 if (option[DEBUG])
1225 {
1226 printf (" /* ");
1227 if (is_duplicate)
1228 printf ("hash value duplicate, ");
1229 else
1230 printf ("hash value = %d, ", temp->_hash_value);
1231 printf ("index = %d */", temp->_final_index);
1232 }
1233 }
1234
1235 static void
1236 output_keyword_blank_entries (int count, const char *indent)
1237 {
1238 int columns;
1239 if (option[TYPE])
1240 {
1241 columns = 58 / (4 + (option[SHAREDLIB] ? 2 : option[NULLSTRINGS] ? 8 : 2)
1242 + strlen (option.get_initializer_suffix()));
1243 if (columns == 0)
1244 columns = 1;
1245 }
1246 else
1247 {
1248 columns = (option[SHAREDLIB] ? 9 : option[NULLSTRINGS] ? 4 : 9);
1249 }
1250 int column = 0;
1251 for (int i = 0; i < count; i++)
1252 {
1253 if ((column % columns) == 0)
1254 {
1255 if (i > 0)
1256 printf (",\n");
1257 printf ("%s ", indent);
1258 }
1259 else
1260 {
1261 if (i > 0)
1262 printf (", ");
1263 }
1264 if (option[TYPE])
1265 printf ("{");
1266 if (option[SHAREDLIB])
1267 printf ("-1");
1268 else
1269 {
1270 if (option[NULLSTRINGS])
1271 printf ("(char*)0");
1272 else
1273 printf ("\"\"");
1274 }
1275 if (option[TYPE])
1276 printf ("%s}", option.get_initializer_suffix());
1277 column++;
1278 }
1279 }
1280
1281 /* Prints out the array containing the keywords for the hash function. */
1282
1283 void
1284 Output::output_keyword_table () const
1285 {
1286 const char *indent = option[GLOBAL] ? "" : " ";
1287 int index;
1288 KeywordExt_List *temp;
1289
1290 /* Avoid compiler warnings "warning: missing initializer for field ..."
1291 for each of the blank entries. */
1292 bool silence_missing_initializer_warning =
1293 option[TYPE] && !option.has_initializer_suffix();
1294 const char *preprocessor_condition =
1295 "(defined __GNUC__ && __GNUC__ + (__GNUC_MINOR__ >= 6) > 4) || (defined __clang__ && __clang_major__ >= 3)";
1296 if (silence_missing_initializer_warning)
1297 {
1298 printf ("#if %s\n", preprocessor_condition);
1299 printf ("#pragma GCC diagnostic push\n");
1300 printf ("#pragma GCC diagnostic ignored \"-Wmissing-field-initializers\"\n");
1301 printf ("#endif\n");
1302 }
1303
1304 printf ("%sstatic ",
1305 indent);
1306 output_const_type (const_readonly_array, _wordlist_eltype);
1307 printf ("%s[] =\n"
1308 "%s {\n",
1309 option.get_wordlist_name (),
1310 indent);
1311
1312 /* Generate an array of reserved words at appropriate locations. */
1313
1314 for (temp = _head, index = 0; temp; temp = temp->rest())
1315 {
1316 KeywordExt *keyword = temp->first();
1317
1318 /* If generating a switch statement, and there is no user defined type,
1319 we generate non-duplicates directly in the code. Only duplicates go
1320 into the table. */
1321 if (option[SWITCH] && !option[TYPE] && !keyword->_duplicate_link)
1322 continue;
1323
1324 if (index > 0)
1325 printf (",\n");
1326
1327 if (index < keyword->_hash_value && !option[SWITCH] && !option[DUP])
1328 {
1329 /* Some blank entries. */
1330 output_keyword_blank_entries (keyword->_hash_value - index, indent);
1331 printf (",\n");
1332 index = keyword->_hash_value;
1333 }
1334
1335 keyword->_final_index = index;
1336
1337 output_keyword_entry (keyword, index, indent, false);
1338
1339 /* Deal with duplicates specially. */
1340 if (keyword->_duplicate_link) // implies option[DUP]
1341 for (KeywordExt *links = keyword->_duplicate_link; links; links = links->_duplicate_link)
1342 {
1343 links->_final_index = ++index;
1344 printf (",\n");
1345 int stringpool_index =
1346 (links->_allchars_length == keyword->_allchars_length
1347 && memcmp (links->_allchars, keyword->_allchars,
1348 keyword->_allchars_length) == 0
1349 ? keyword->_final_index
1350 : links->_final_index);
1351 output_keyword_entry (links, stringpool_index, indent, true);
1352 }
1353
1354 index++;
1355 }
1356 if (index > 0)
1357 printf ("\n");
1358
1359 printf ("%s };\n", indent);
1360
1361 if (silence_missing_initializer_warning)
1362 {
1363 printf ("#if %s\n", preprocessor_condition);
1364 printf ("#pragma GCC diagnostic pop\n");
1365 printf ("#endif\n");
1366 }
1367 printf ("\n");
1368 }
1369
1370 /* ------------------------------------------------------------------------- */
1371
1372 /* Generates the large, sparse table that maps hash values into
1373 the smaller, contiguous range of the keyword table. */
1374
1375 void
1376 Output::output_lookup_array () const
1377 {
1378 if (option[DUP])
1379 {
1380 const int DEFAULT_VALUE = -1;
1381
1382 /* Because of the way output_keyword_table works, every duplicate set is
1383 stored contiguously in the wordlist array. */
1384 struct duplicate_entry
1385 {
1386 int hash_value; /* Hash value for this particular duplicate set. */
1387 int index; /* Index into the main keyword storage array. */
1388 int count; /* Number of consecutive duplicates at this index. */
1389 };
1390
1391 duplicate_entry *duplicates = new duplicate_entry[_total_duplicates];
1392 int *lookup_array = new int[_max_hash_value + 1 + 2*_total_duplicates];
1393 int lookup_array_size = _max_hash_value + 1;
1394 duplicate_entry *dup_ptr = &duplicates[0];
1395 int *lookup_ptr = &lookup_array[_max_hash_value + 1 + 2*_total_duplicates];
1396
1397 while (lookup_ptr > lookup_array)
1398 *--lookup_ptr = DEFAULT_VALUE;
1399
1400 /* Now dup_ptr = &duplicates[0] and lookup_ptr = &lookup_array[0]. */
1401
1402 for (KeywordExt_List *temp = _head; temp; temp = temp->rest())
1403 {
1404 int hash_value = temp->first()->_hash_value;
1405 lookup_array[hash_value] = temp->first()->_final_index;
1406 if (option[DEBUG])
1407 fprintf (stderr, "keyword = %.*s, index = %d\n",
1408 temp->first()->_allchars_length, temp->first()->_allchars, temp->first()->_final_index);
1409 if (temp->first()->_duplicate_link)
1410 {
1411 /* Start a duplicate entry. */
1412 dup_ptr->hash_value = hash_value;
1413 dup_ptr->index = temp->first()->_final_index;
1414 dup_ptr->count = 1;
1415
1416 for (KeywordExt *ptr = temp->first()->_duplicate_link; ptr; ptr = ptr->_duplicate_link)
1417 {
1418 dup_ptr->count++;
1419 if (option[DEBUG])
1420 fprintf (stderr,
1421 "static linked keyword = %.*s, index = %d\n",
1422 ptr->_allchars_length, ptr->_allchars, ptr->_final_index);
1423 }
1424 assert (dup_ptr->count >= 2);
1425 dup_ptr++;
1426 }
1427 }
1428
1429 while (dup_ptr > duplicates)
1430 {
1431 dup_ptr--;
1432
1433 if (option[DEBUG])
1434 fprintf (stderr,
1435 "dup_ptr[%lu]: hash_value = %d, index = %d, count = %d\n",
1436 static_cast<unsigned long>(dup_ptr - duplicates),
1437 dup_ptr->hash_value, dup_ptr->index, dup_ptr->count);
1438
1439 int i;
1440 /* Start searching for available space towards the right part
1441 of the lookup array. */
1442 for (i = dup_ptr->hash_value; i < lookup_array_size-1; i++)
1443 if (lookup_array[i] == DEFAULT_VALUE
1444 && lookup_array[i + 1] == DEFAULT_VALUE)
1445 goto found_i;
1446 /* If we didn't find it to the right look to the left instead... */
1447 for (i = dup_ptr->hash_value-1; i >= 0; i--)
1448 if (lookup_array[i] == DEFAULT_VALUE
1449 && lookup_array[i + 1] == DEFAULT_VALUE)
1450 goto found_i;
1451 /* Append to the end of lookup_array. */
1452 i = lookup_array_size;
1453 lookup_array_size += 2;
1454 found_i:
1455 /* Put in an indirection from dup_ptr->_hash_value to i.
1456 At i and i+1 store dup_ptr->_final_index and dup_ptr->count. */
1457 assert (lookup_array[dup_ptr->hash_value] == dup_ptr->index);
1458 lookup_array[dup_ptr->hash_value] = - 1 - _total_keys - i;
1459 lookup_array[i] = - _total_keys + dup_ptr->index;
1460 lookup_array[i + 1] = - dup_ptr->count;
1461 /* All these three values are <= -2, distinct from DEFAULT_VALUE. */
1462 }
1463
1464 /* The values of the lookup array are now known. */
1465
1466 int min = INT_MAX;
1467 int max = INT_MIN;
1468 lookup_ptr = lookup_array + lookup_array_size;
1469 while (lookup_ptr > lookup_array)
1470 {
1471 int val = *--lookup_ptr;
1472 if (min > val)
1473 min = val;
1474 if (max < val)
1475 max = val;
1476 }
1477
1478 const char *indent = option[GLOBAL] ? "" : " ";
1479 printf ("%sstatic %s%s lookup[] =\n"
1480 "%s {",
1481 indent, const_readonly_array, smallest_integral_type (min, max),
1482 indent);
1483
1484 int field_width;
1485 /* Calculate maximum number of digits required for MIN..MAX. */
1486 {
1487 field_width = 2;
1488 for (int trunc = max; (trunc /= 10) > 0;)
1489 field_width++;
1490 }
1491 if (min < 0)
1492 {
1493 int neg_field_width = 2;
1494 for (int trunc = -min; (trunc /= 10) > 0;)
1495 neg_field_width++;
1496 neg_field_width++; /* account for the minus sign */
1497 if (field_width < neg_field_width)
1498 field_width = neg_field_width;
1499 }
1500
1501 const int columns = 42 / field_width;
1502 int column;
1503
1504 column = 0;
1505 for (int i = 0; i < lookup_array_size; i++)
1506 {
1507 if (i > 0)
1508 printf (",");
1509 if ((column++ % columns) == 0)
1510 printf("\n%s ", indent);
1511 printf ("%*d", field_width, lookup_array[i]);
1512 }
1513 printf ("\n%s };\n\n", indent);
1514
1515 delete[] duplicates;
1516 delete[] lookup_array;
1517 }
1518 }
1519
1520 /* ------------------------------------------------------------------------- */
1521
1522 /* Generate all pools needed for the lookup function. */
1523
1524 void
1525 Output::output_lookup_pools () const
1526 {
1527 if (option[SWITCH])
1528 {
1529 if (option[TYPE] || (option[DUP] && _total_duplicates > 0))
1530 output_string_pool ();
1531 }
1532 else
1533 {
1534 output_string_pool ();
1535 }
1536 }
1537
1538 /* Generate all the tables needed for the lookup function. */
1539
1540 void
1541 Output::output_lookup_tables () const
1542 {
1543 if (option[SWITCH])
1544 {
1545 /* Use the switch in place of lookup table. */
1546 if (option[LENTABLE] && (option[DUP] && _total_duplicates > 0))
1547 output_keylength_table ();
1548 if (option[TYPE] || (option[DUP] && _total_duplicates > 0))
1549 output_keyword_table ();
1550 }
1551 else
1552 {
1553 /* Use the lookup table, in place of switch. */
1554 if (option[LENTABLE])
1555 output_keylength_table ();
1556 output_keyword_table ();
1557 output_lookup_array ();
1558 }
1559 }
1560
1561 /* ------------------------------------------------------------------------- */
1562
1563 /* Output a single switch case (including duplicates). Advance list. */
1564
1565 static KeywordExt_List *
1566 output_switch_case (KeywordExt_List *list, int indent, int *jumps_away)
1567 {
1568 if (option[DEBUG])
1569 printf ("%*s/* hash value = %4d, keyword = \"%.*s\" */\n",
1570 indent, "", list->first()->_hash_value, list->first()->_allchars_length, list->first()->_allchars);
1571
1572 if (option[DUP] && list->first()->_duplicate_link)
1573 {
1574 if (option[LENTABLE])
1575 printf ("%*slengthptr = &%s[%d];\n",
1576 indent, "", option.get_lengthtable_name (), list->first()->_final_index);
1577 printf ("%*swordptr = &%s[%d];\n",
1578 indent, "", option.get_wordlist_name (), list->first()->_final_index);
1579
1580 int count = 0;
1581 for (KeywordExt *links = list->first(); links; links = links->_duplicate_link)
1582 count++;
1583
1584 printf ("%*swordendptr = wordptr + %d;\n"
1585 "%*sgoto multicompare;\n",
1586 indent, "", count,
1587 indent, "");
1588 *jumps_away = 1;
1589 }
1590 else
1591 {
1592 if (option[LENTABLE])
1593 {
1594 printf ("%*sif (len == %d)\n"
1595 "%*s {\n",
1596 indent, "", list->first()->_allchars_length,
1597 indent, "");
1598 indent += 4;
1599 }
1600 printf ("%*sresword = ",
1601 indent, "");
1602 if (option[TYPE])
1603 printf ("&%s[%d]", option.get_wordlist_name (), list->first()->_final_index);
1604 else
1605 output_string (list->first()->_allchars, list->first()->_allchars_length);
1606 printf (";\n");
1607 printf ("%*sgoto compare;\n",
1608 indent, "");
1609 if (option[LENTABLE])
1610 {
1611 indent -= 4;
1612 printf ("%*s }\n",
1613 indent, "");
1614 }
1615 else
1616 *jumps_away = 1;
1617 }
1618
1619 return list->rest();
1620 }
1621
1622 /* Output a total of size cases, grouped into num_switches switch statements,
1623 where 0 < num_switches <= size. */
1624
1625 static void
1626 output_switches (KeywordExt_List *list, int num_switches, int size, int min_hash_value, int max_hash_value, int indent)
1627 {
1628 if (option[DEBUG])
1629 printf ("%*s/* know %d <= key <= %d, contains %d cases */\n",
1630 indent, "", min_hash_value, max_hash_value, size);
1631
1632 if (num_switches > 1)
1633 {
1634 int part1 = num_switches / 2;
1635 int part2 = num_switches - part1;
1636 int size1 = static_cast<int>(static_cast<double>(size) / static_cast<double>(num_switches) * static_cast<double>(part1) + 0.5);
1637 int size2 = size - size1;
1638
1639 KeywordExt_List *temp = list;
1640 for (int count = size1; count > 0; count--)
1641 temp = temp->rest();
1642
1643 printf ("%*sif (key < %d)\n"
1644 "%*s {\n",
1645 indent, "", temp->first()->_hash_value,
1646 indent, "");
1647
1648 output_switches (list, part1, size1, min_hash_value, temp->first()->_hash_value-1, indent+4);
1649
1650 printf ("%*s }\n"
1651 "%*selse\n"
1652 "%*s {\n",
1653 indent, "", indent, "", indent, "");
1654
1655 output_switches (temp, part2, size2, temp->first()->_hash_value, max_hash_value, indent+4);
1656
1657 printf ("%*s }\n",
1658 indent, "");
1659 }
1660 else
1661 {
1662 /* Output a single switch. */
1663 int lowest_case_value = list->first()->_hash_value;
1664 if (size == 1)
1665 {
1666 int jumps_away = 0;
1667 assert (min_hash_value <= lowest_case_value);
1668 assert (lowest_case_value <= max_hash_value);
1669 if (min_hash_value == max_hash_value)
1670 output_switch_case (list, indent, &jumps_away);
1671 else
1672 {
1673 printf ("%*sif (key == %d)\n"
1674 "%*s {\n",
1675 indent, "", lowest_case_value,
1676 indent, "");
1677 output_switch_case (list, indent+4, &jumps_away);
1678 printf ("%*s }\n",
1679 indent, "");
1680 }
1681 }
1682 else
1683 {
1684 if (lowest_case_value == 0)
1685 printf ("%*sswitch (key)\n", indent, "");
1686 else
1687 printf ("%*sswitch (key - %d)\n", indent, "", lowest_case_value);
1688 printf ("%*s {\n",
1689 indent, "");
1690 for (; size > 0; size--)
1691 {
1692 int jumps_away = 0;
1693 printf ("%*s case %d:\n",
1694 indent, "", list->first()->_hash_value - lowest_case_value);
1695 list = output_switch_case (list, indent+6, &jumps_away);
1696 if (!jumps_away)
1697 printf ("%*s break;\n",
1698 indent, "");
1699 }
1700 printf ("%*s }\n",
1701 indent, "");
1702 }
1703 }
1704 }
1705
1706 /* Generates C code to perform the keyword lookup. */
1707
1708 void
1709 Output::output_lookup_function_body (const Output_Compare& comparison) const
1710 {
1711 /* An expression equivalent to NULL, of type _return_type.
1712 We don't use plain "0", because that would trigger a gcc 15 warning
1713 if the warning option -Wzero-as-null-pointer-constant is in use.
1714 In C++, using "nullptr" if __cplusplus >= 201103L would be possible,
1715 but is not worth the trouble. */
1716 char *null_expression = new char[17 + strlen (_return_type) + 1];
1717 if (option[CPLUSPLUS])
1718 sprintf (null_expression, "static_cast<%s> (0)", _return_type);
1719 else
1720 sprintf (null_expression, "(%s) 0", _return_type);
1721
1722 printf (" if (len <= %sMAX_WORD_LENGTH && len >= %sMIN_WORD_LENGTH)\n"
1723 " {\n"
1724 " %sunsigned int key = %s (str, len);\n\n",
1725 option.get_constants_prefix (), option.get_constants_prefix (),
1726 register_scs, option.get_hash_name ());
1727
1728 if (option[SWITCH])
1729 {
1730 int switch_size = num_hash_values ();
1731 int num_switches = option.get_total_switches ();
1732 if (num_switches > switch_size)
1733 num_switches = switch_size;
1734
1735 printf (" if (key <= %sMAX_HASH_VALUE",
1736 option.get_constants_prefix ());
1737 if (_min_hash_value > 0)
1738 printf (" && key >= %sMIN_HASH_VALUE",
1739 option.get_constants_prefix ());
1740 printf (")\n"
1741 " {\n");
1742 if (option[DUP] && _total_duplicates > 0)
1743 {
1744 if (option[LENTABLE])
1745 printf (" %s%s%s *lengthptr;\n",
1746 register_scs, const_always,
1747 smallest_integral_type (_max_key_len));
1748 printf (" %s",
1749 register_scs);
1750 output_const_type (const_readonly_array, _wordlist_eltype);
1751 printf ("*wordptr;\n");
1752 printf (" %s",
1753 register_scs);
1754 output_const_type (const_readonly_array, _wordlist_eltype);
1755 printf ("*wordendptr;\n");
1756 }
1757 if (option[TYPE])
1758 {
1759 printf (" %s",
1760 register_scs);
1761 output_const_type (const_readonly_array, _struct_tag);
1762 printf ("*resword;\n\n");
1763 }
1764 else
1765 printf (" %s%sresword;\n\n",
1766 register_scs, _struct_tag);
1767
1768 output_switches (_head, num_switches, switch_size, _min_hash_value, _max_hash_value, 10);
1769
1770 printf (" return %s;\n",
1771 null_expression);
1772 if (option[DUP] && _total_duplicates > 0)
1773 {
1774 int indent = 8;
1775 printf ("%*smulticompare:\n"
1776 "%*s while (wordptr < wordendptr)\n"
1777 "%*s {\n",
1778 indent, "", indent, "", indent, "");
1779 if (option[LENTABLE])
1780 {
1781 printf ("%*s if (len == *lengthptr)\n"
1782 "%*s {\n",
1783 indent, "", indent, "");
1784 indent += 4;
1785 }
1786 printf ("%*s %s%schar *s = ",
1787 indent, "", register_scs, const_always);
1788 if (option[TYPE])
1789 printf ("wordptr->%s", option.get_slot_name ());
1790 else
1791 printf ("*wordptr");
1792 if (option[SHAREDLIB])
1793 printf (" + %s",
1794 option.get_stringpool_name ());
1795 printf (";\n\n"
1796 "%*s if (",
1797 indent, "");
1798 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("s"));
1799 printf (")\n"
1800 "%*s return %s;\n",
1801 indent, "",
1802 option[TYPE] ? "wordptr" : "s");
1803 if (option[LENTABLE])
1804 {
1805 indent -= 4;
1806 printf ("%*s }\n",
1807 indent, "");
1808 }
1809 if (option[LENTABLE])
1810 printf ("%*s lengthptr++;\n",
1811 indent, "");
1812 printf ("%*s wordptr++;\n"
1813 "%*s }\n"
1814 "%*s return %s;\n",
1815 indent, "", indent, "", indent, "", null_expression);
1816 }
1817 printf (" compare:\n");
1818 if (option[TYPE])
1819 {
1820 printf (" {\n"
1821 " %s%schar *s = resword->%s",
1822 register_scs, const_always, option.get_slot_name ());
1823 if (option[SHAREDLIB])
1824 printf (" + %s",
1825 option.get_stringpool_name ());
1826 printf (";\n\n"
1827 " if (");
1828 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("s"));
1829 printf (")\n"
1830 " return resword;\n"
1831 " }\n");
1832 }
1833 else
1834 {
1835 printf (" if (");
1836 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("resword"));
1837 printf (")\n"
1838 " return resword;\n");
1839 }
1840 printf (" }\n");
1841 }
1842 else
1843 {
1844 printf (" if (key <= %sMAX_HASH_VALUE)\n",
1845 option.get_constants_prefix ());
1846
1847 if (option[DUP])
1848 {
1849 int indent = 8;
1850 printf ("%*s{\n"
1851 "%*s %sint index = lookup[key];\n\n"
1852 "%*s if (index >= 0)\n",
1853 indent, "", indent, "", register_scs, indent, "");
1854 if (option[LENTABLE])
1855 {
1856 printf ("%*s {\n"
1857 "%*s if (len == %s[index])\n",
1858 indent, "", indent, "", option.get_lengthtable_name ());
1859 indent += 4;
1860 }
1861 printf ("%*s {\n"
1862 "%*s %s%schar *s = %s[index]",
1863 indent, "",
1864 indent, "", register_scs, const_always,
1865 option.get_wordlist_name ());
1866 if (option[TYPE])
1867 printf (".%s", option.get_slot_name ());
1868 if (option[SHAREDLIB])
1869 printf (" + %s",
1870 option.get_stringpool_name ());
1871 printf (";\n\n"
1872 "%*s if (",
1873 indent, "");
1874 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("s"));
1875 printf (")\n"
1876 "%*s return ",
1877 indent, "");
1878 if (option[TYPE])
1879 printf ("&%s[index]", option.get_wordlist_name ());
1880 else
1881 printf ("s");
1882 printf (";\n"
1883 "%*s }\n",
1884 indent, "");
1885 if (option[LENTABLE])
1886 {
1887 indent -= 4;
1888 printf ("%*s }\n", indent, "");
1889 }
1890 if (_total_duplicates > 0)
1891 {
1892 printf ("%*s else if (index < -%sTOTAL_KEYWORDS)\n"
1893 "%*s {\n"
1894 "%*s %sint offset = - 1 - %sTOTAL_KEYWORDS - index;\n",
1895 indent, "", option.get_constants_prefix (), indent, "",
1896 indent, "", register_scs, option.get_constants_prefix ());
1897 if (option[LENTABLE])
1898 printf ("%*s %s%s%s *lengthptr = &%s[%sTOTAL_KEYWORDS + lookup[offset]];\n",
1899 indent, "", register_scs, const_always, smallest_integral_type (_max_key_len),
1900 option.get_lengthtable_name (), option.get_constants_prefix ());
1901 printf ("%*s %s",
1902 indent, "", register_scs);
1903 output_const_type (const_readonly_array, _wordlist_eltype);
1904 printf ("*wordptr = &%s[%sTOTAL_KEYWORDS + lookup[offset]];\n",
1905 option.get_wordlist_name (), option.get_constants_prefix ());
1906 printf ("%*s %s",
1907 indent, "", register_scs);
1908 output_const_type (const_readonly_array, _wordlist_eltype);
1909 printf ("*wordendptr = wordptr + -lookup[offset + 1];\n\n");
1910 printf ("%*s while (wordptr < wordendptr)\n"
1911 "%*s {\n",
1912 indent, "", indent, "");
1913 if (option[LENTABLE])
1914 {
1915 printf ("%*s if (len == *lengthptr)\n"
1916 "%*s {\n",
1917 indent, "", indent, "");
1918 indent += 4;
1919 }
1920 printf ("%*s %s%schar *s = ",
1921 indent, "", register_scs, const_always);
1922 if (option[TYPE])
1923 printf ("wordptr->%s", option.get_slot_name ());
1924 else
1925 printf ("*wordptr");
1926 if (option[SHAREDLIB])
1927 printf (" + %s",
1928 option.get_stringpool_name ());
1929 printf (";\n\n"
1930 "%*s if (",
1931 indent, "");
1932 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("s"));
1933 printf (")\n"
1934 "%*s return %s;\n",
1935 indent, "",
1936 option[TYPE] ? "wordptr" : "s");
1937 if (option[LENTABLE])
1938 {
1939 indent -= 4;
1940 printf ("%*s }\n",
1941 indent, "");
1942 }
1943 if (option[LENTABLE])
1944 printf ("%*s lengthptr++;\n",
1945 indent, "");
1946 printf ("%*s wordptr++;\n"
1947 "%*s }\n"
1948 "%*s }\n",
1949 indent, "", indent, "", indent, "");
1950 }
1951 printf ("%*s}\n",
1952 indent, "");
1953 }
1954 else
1955 {
1956 int indent = 8;
1957 if (option[LENTABLE])
1958 {
1959 printf ("%*sif (len == %s[key])\n",
1960 indent, "", option.get_lengthtable_name ());
1961 indent += 2;
1962 }
1963
1964 if (option[SHAREDLIB])
1965 {
1966 if (!option[LENTABLE])
1967 {
1968 printf ("%*s{\n"
1969 "%*s %sint o = %s[key]",
1970 indent, "",
1971 indent, "", register_scs,
1972 option.get_wordlist_name ());
1973 if (option[TYPE])
1974 printf (".%s", option.get_slot_name ());
1975 printf (";\n"
1976 "%*s if (o >= 0)\n"
1977 "%*s {\n",
1978 indent, "",
1979 indent, "");
1980 indent += 4;
1981 printf ("%*s %s%schar *s = o",
1982 indent, "", register_scs, const_always);
1983 }
1984 else
1985 {
1986 /* No need for the (o >= 0) test, because the
1987 (len == lengthtable[key]) test already guarantees that
1988 key points to nonempty table entry. */
1989 printf ("%*s{\n"
1990 "%*s %s%schar *s = %s[key]",
1991 indent, "",
1992 indent, "", register_scs, const_always,
1993 option.get_wordlist_name ());
1994 if (option[TYPE])
1995 printf (".%s", option.get_slot_name ());
1996 }
1997 printf (" + %s",
1998 option.get_stringpool_name ());
1999 }
2000 else
2001 {
2002 printf ("%*s{\n"
2003 "%*s %s%schar *s = %s[key]",
2004 indent, "",
2005 indent, "", register_scs, const_always,
2006 option.get_wordlist_name ());
2007 if (option[TYPE])
2008 printf (".%s", option.get_slot_name ());
2009 }
2010
2011 printf (";\n\n"
2012 "%*s if (",
2013 indent, "");
2014 if (!option[SHAREDLIB] && option[NULLSTRINGS])
2015 printf ("s && ");
2016 comparison.output_comparison (Output_Expr1 ("str"), Output_Expr1 ("s"));
2017 printf (")\n"
2018 "%*s return ",
2019 indent, "");
2020 if (option[TYPE])
2021 printf ("&%s[key]", option.get_wordlist_name ());
2022 else
2023 printf ("s");
2024 printf (";\n");
2025 if (option[SHAREDLIB] && !option[LENTABLE])
2026 {
2027 indent -= 4;
2028 printf ("%*s }\n",
2029 indent, "");
2030 }
2031 printf ("%*s}\n",
2032 indent, "");
2033 }
2034 }
2035 printf (" }\n"
2036 " return %s;\n",
2037 null_expression);
2038
2039 delete[] null_expression;
2040 }
2041
2042 /* Generates C code for the lookup function. */
2043
2044 void
2045 Output::output_lookup_function () const
2046 {
2047 /* Output the function's head. */
2048 /* We don't declare the lookup function 'static' because we cannot make
2049 assumptions about the compilation units of the user.
2050 Since we don't make it 'static', it makes no sense to declare it 'inline',
2051 because non-static inline functions must not reference static functions or
2052 variables, see ISO C 99 section 6.7.4.(3). */
2053
2054 printf ("%s%s\n",
2055 const_for_struct, _return_type);
2056 if (option[CPLUSPLUS])
2057 printf ("%s::", option.get_class_name ());
2058 printf ("%s ", option.get_function_name ());
2059 printf (option[KRC] ?
2060 "(str, len)\n"
2061 " %schar *str;\n"
2062 " %ssize_t len;\n" :
2063 option[C] ?
2064 "(str, len)\n"
2065 " %sconst char *str;\n"
2066 " %ssize_t len;\n" :
2067 option[ANSIC] | option[CPLUSPLUS] ?
2068 "(%sconst char *str, %ssize_t len)\n" :
2069 "",
2070 register_scs, register_scs);
2071
2072 /* Output the function's body. */
2073 printf ("{\n");
2074
2075 if (option[ENUM] && !option[GLOBAL])
2076 {
2077 Output_Enum style (" ");
2078 output_constants (style);
2079 }
2080
2081 if (option[SHAREDLIB] && !(option[GLOBAL] || option[TYPE]))
2082 output_lookup_pools ();
2083 if (!option[GLOBAL])
2084 output_lookup_tables ();
2085
2086 if (option[LENTABLE])
2087 output_lookup_function_body (Output_Compare_Memcmp ());
2088 else
2089 {
2090 if (option[COMP])
2091 output_lookup_function_body (Output_Compare_Strncmp ());
2092 else
2093 output_lookup_function_body (Output_Compare_Strcmp ());
2094 }
2095
2096 printf ("}\n");
2097 }
2098
2099 /* ------------------------------------------------------------------------- */
2100
2101 /* Generates the hash function and the key word recognizer function
2102 based upon the user's Options. */
2103
2104 void
2105 Output::output ()
2106 {
2107 compute_min_max ();
2108
2109 if (option[CPLUSPLUS])
2110 /* The 'register' keyword is removed from C++17.
2111 See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4340 */
2112 register_scs = "";
2113 else
2114 register_scs = "register ";
2115
2116 if (option[C] | option[ANSIC] | option[CPLUSPLUS])
2117 {
2118 const_always = "const ";
2119 const_readonly_array = (option[CONST] ? "const " : "");
2120 const_for_struct = ((option[CONST] && option[TYPE]) ? "const " : "");
2121 }
2122 else
2123 {
2124 const_always = "";
2125 const_readonly_array = "";
2126 const_for_struct = "";
2127 }
2128
2129 if (!option[TYPE])
2130 {
2131 _return_type = (const_always[0] ? "const char *" : "char *");
2132 _struct_tag = (const_always[0] ? "const char *" : "char *");
2133 }
2134
2135 _wordlist_eltype = (option[SHAREDLIB] && !option[TYPE] ? "int" : _struct_tag);
2136
2137 printf ("/* ");
2138 if (option[KRC])
2139 printf ("KR-C");
2140 else if (option[C])
2141 printf ("C");
2142 else if (option[ANSIC])
2143 printf ("ANSI-C");
2144 else if (option[CPLUSPLUS])
2145 printf ("C++");
2146 printf (" code produced by gperf version %s */\n", version_string);
2147 option.print_options ();
2148 printf ("\n");
2149 if (!option[POSITIONS])
2150 {
2151 printf ("/* Computed positions: -k'");
2152 _key_positions.print();
2153 printf ("' */\n");
2154 }
2155 printf ("\n");
2156
2157 if (_charset_dependent
2158 && (_key_positions.get_size() > 0 || option[UPPERLOWER]))
2159 {
2160 /* The generated tables assume that the execution character set is
2161 based on ISO-646, not EBCDIC. */
2162 printf ("#if !((' ' == 32) && ('!' == 33) && ('\"' == 34) && ('#' == 35) \\\n"
2163 " && ('%%' == 37) && ('&' == 38) && ('\\'' == 39) && ('(' == 40) \\\n"
2164 " && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \\\n"
2165 " && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \\\n"
2166 " && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \\\n"
2167 " && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \\\n"
2168 " && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \\\n"
2169 " && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \\\n"
2170 " && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \\\n"
2171 " && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \\\n"
2172 " && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \\\n"
2173 " && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \\\n"
2174 " && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \\\n"
2175 " && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \\\n"
2176 " && ('Z' == 90) && ('[' == 91) && ('\\\\' == 92) && (']' == 93) \\\n"
2177 " && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \\\n"
2178 " && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \\\n"
2179 " && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \\\n"
2180 " && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \\\n"
2181 " && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \\\n"
2182 " && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \\\n"
2183 " && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \\\n"
2184 " && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))\n"
2185 "/* The character set is not based on ISO-646. */\n");
2186 printf ("%s \"gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>.\"\n", option[KRC] || option[C] ? "error" : "#error");
2187 printf ("#endif\n\n");
2188 }
2189
2190 if (_verbatim_declarations < _verbatim_declarations_end)
2191 {
2192 output_line_directive (_verbatim_declarations_lineno);
2193 fwrite (_verbatim_declarations, 1,
2194 _verbatim_declarations_end - _verbatim_declarations, stdout);
2195 }
2196
2197 if (option[TYPE] && !option[NOTYPE]) /* Output type declaration now, reference it later on.... */
2198 {
2199 output_line_directive (_struct_decl_lineno);
2200 printf ("%s\n", _struct_decl);
2201 }
2202
2203 if (option[INCLUDE])
2204 printf ("#include <string.h>\n"); /* Declare strlen(), strcmp(), strncmp(). */
2205
2206 if (!option[ENUM])
2207 {
2208 Output_Defines style;
2209 output_constants (style);
2210 }
2211 else if (option[GLOBAL])
2212 {
2213 Output_Enum style ("");
2214 output_constants (style);
2215 }
2216
2217 printf ("/* maximum key range = %d, duplicates = %d */\n\n",
2218 _max_hash_value - _min_hash_value + 1, _total_duplicates);
2219
2220 if (option[UPPERLOWER])
2221 {
2222 #if USE_DOWNCASE_TABLE
2223 output_upperlower_table ();
2224 #endif
2225
2226 if (option[LENTABLE])
2227 output_upperlower_memcmp ();
2228 else
2229 {
2230 if (option[COMP])
2231 output_upperlower_strncmp ();
2232 else
2233 output_upperlower_strcmp ();
2234 }
2235 }
2236
2237 if (option[CPLUSPLUS])
2238 printf ("class %s\n"
2239 "{\n"
2240 "private:\n"
2241 " static inline unsigned int %s (const char *str, size_t len);\n"
2242 "public:\n"
2243 " static %s%s%s (const char *str, size_t len);\n"
2244 "};\n"
2245 "\n",
2246 option.get_class_name (), option.get_hash_name (),
2247 const_for_struct, _return_type, option.get_function_name ());
2248
2249 output_hash_function ();
2250
2251 if (option[SHAREDLIB] && (option[GLOBAL] || option[TYPE]))
2252 output_lookup_pools ();
2253 if (option[GLOBAL])
2254 output_lookup_tables ();
2255
2256 output_lookup_function ();
2257
2258 if (_verbatim_code < _verbatim_code_end)
2259 {
2260 output_line_directive (_verbatim_code_lineno);
2261 fwrite (_verbatim_code, 1, _verbatim_code_end - _verbatim_code, stdout);
2262 }
2263
2264 fflush (stdout);
2265 }