home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / c-lex.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  49KB  |  1,928 lines

  1. /* Lexical analyzer for C and Objective C.
  2.    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <setjmp.h>
  24.  
  25. #include "config.h"
  26. #include "rtl.h"
  27. #include "tree.h"
  28. #include "input.h"
  29. #include "c-lex.h"
  30. #include "c-tree.h"
  31. #include "flags.h"
  32. #include "c-parse.h"
  33.  
  34. #ifdef MULTIBYTE_CHARS
  35. #include <stdlib.h>
  36. #include <locale.h>
  37. #endif
  38.  
  39. #ifndef errno
  40. extern int errno;
  41. #endif
  42.  
  43. /* The elements of `ridpointers' are identifier nodes
  44.    for the reserved type names and storage classes.
  45.    It is indexed by a RID_... value.  */
  46. tree ridpointers[(int) RID_MAX];
  47.  
  48. /* Cause the `yydebug' variable to be defined.  */
  49. #define YYDEBUG 1
  50.  
  51. /* the declaration found for the last IDENTIFIER token read in.
  52.    yylex must look this up to detect typedefs, which get token type TYPENAME,
  53.    so it is left around in case the identifier is not a typedef but is
  54.    used in a context which makes it a reference to a variable.  */
  55. tree lastiddecl;
  56.  
  57. /* Nonzero enables objc features.  */
  58.  
  59. int doing_objc_thang;
  60.  
  61. extern tree lookup_interface ();
  62.  
  63. extern int yydebug;
  64.  
  65. /* File used for outputting assembler code.  */
  66. extern FILE *asm_out_file;
  67.  
  68. #ifndef WCHAR_TYPE_SIZE
  69. #ifdef INT_TYPE_SIZE
  70. #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
  71. #else
  72. #define WCHAR_TYPE_SIZE    BITS_PER_WORD
  73. #endif
  74. #endif
  75.  
  76. /* Number of bytes in a wide character.  */
  77. #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
  78.  
  79. static int maxtoken;        /* Current nominal length of token buffer.  */
  80. char *token_buffer;    /* Pointer to token buffer.
  81.                Actual allocated length is maxtoken + 2.
  82.                This is not static because objc-parse.y uses it.  */
  83.  
  84. /* Nonzero if end-of-file has been seen on input.  */
  85. static int end_of_file;
  86.  
  87. /* Buffered-back input character; faster than using ungetc.  */
  88. static int nextchar = -1;
  89.  
  90. int check_newline ();
  91.  
  92. /* Nonzero tells yylex to ignore \ in string constants.  */
  93. static int ignore_escape_flag = 0;
  94.  
  95. /* C code produced by gperf version 2.5 (GNU C++ version) */
  96. /* Command-line: gperf -p -j1 -i 1 -g -o -t -N is_reserved_word -k1,3,$ c-parse.gperf  */ 
  97. struct resword { char *name; short token; enum rid rid; };
  98.  
  99. #define TOTAL_KEYWORDS 53
  100. #define MIN_WORD_LENGTH 2
  101. #define MAX_WORD_LENGTH 13
  102. #define MIN_HASH_VALUE 7
  103. #define MAX_HASH_VALUE 102
  104. /* maximum key range = 96, duplicates = 0 */
  105.  
  106. #ifdef __GNUC__
  107. __inline
  108. #endif
  109. static unsigned int
  110. hash (str, len)
  111.      register char *str;
  112.      register int unsigned len;
  113. {
  114.   static unsigned char asso_values[] =
  115.     {
  116.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  117.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  118.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  119.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  120.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  121.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  122.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  123.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  124.      103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  125.      103, 103, 103, 103, 103,   1, 103,   2,   1,  24,
  126.        1,   5,  19,  39,  16,  13, 103,   1,  25,   1,
  127.       34,  34,  24, 103,  13,  12,   1,  45,  24,   7,
  128.      103, 103,   2, 103, 103, 103, 103, 103,
  129.     };
  130.   register int hval = len;
  131.  
  132.   switch (hval)
  133.     {
  134.       default:
  135.       case 3:
  136.         hval += asso_values[str[2]];
  137.       case 2:
  138.       case 1:
  139.         hval += asso_values[str[0]];
  140.     }
  141.   return hval + asso_values[str[len - 1]];
  142. }
  143.  
  144. #ifdef __GNUC__
  145. __inline
  146. #endif
  147. struct resword *
  148. is_reserved_word (str, len)
  149.      register char *str;
  150.      register unsigned int len;
  151. {
  152.   static struct resword wordlist[] =
  153.     {
  154.       {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
  155.       {"asm",  ASM_KEYWORD, NORID},
  156.       {"",}, 
  157.       {"__asm",  ASM_KEYWORD, NORID},
  158.       {"",}, 
  159.       {"__asm__",  ASM_KEYWORD, NORID},
  160.       {"break",  BREAK, NORID},
  161.       {"__typeof__",  TYPEOF, NORID},
  162.       {"",}, 
  163.       {"__alignof__",  ALIGNOF, NORID},
  164.       {"",}, 
  165.       {"__attribute__",  ATTRIBUTE, NORID},
  166.       {"int",  TYPESPEC, RID_INT},
  167.       {"__attribute",  ATTRIBUTE, NORID},
  168.       {"__extension__",  EXTENSION, NORID},
  169.       {"",}, 
  170.       {"__signed",  TYPESPEC, RID_SIGNED},
  171.       {"",}, 
  172.       {"__signed__",  TYPESPEC, RID_SIGNED},
  173.       {"__inline__",  SCSPEC, RID_INLINE},
  174.       {"else",  ELSE, NORID},
  175.       {"__inline",  SCSPEC, RID_INLINE},
  176.       {"default",  DEFAULT, NORID},
  177.       {"__typeof",  TYPEOF, NORID},
  178.       {"while",  WHILE, NORID},
  179.       {"__alignof",  ALIGNOF, NORID},
  180.       {"struct",  STRUCT, NORID},
  181.       {"__const",  TYPE_QUAL, RID_CONST},
  182.       {"if",  IF, NORID},
  183.       {"__const__",  TYPE_QUAL, RID_CONST},
  184.       {"__label__",  LABEL, NORID},
  185.       {"do",  DO, NORID},
  186.       {"__volatile__",  TYPE_QUAL, RID_VOLATILE},
  187.       {"sizeof",  SIZEOF, NORID},
  188.       {"__volatile",  TYPE_QUAL, RID_VOLATILE},
  189.       {"auto",  SCSPEC, RID_AUTO},
  190.       {"void",  TYPESPEC, RID_VOID},
  191.       {"char",  TYPESPEC, RID_CHAR},
  192.       {"static",  SCSPEC, RID_STATIC},
  193.       {"case",  CASE, NORID},
  194.       {"extern",  SCSPEC, RID_EXTERN},
  195.       {"switch",  SWITCH, NORID},
  196.       {"for",  FOR, NORID},
  197.       {"inline",  SCSPEC, RID_INLINE},
  198.       {"typeof",  TYPEOF, NORID},
  199.       {"typedef",  SCSPEC, RID_TYPEDEF},
  200.       {"short",  TYPESPEC, RID_SHORT},
  201.       {"",}, 
  202.       {"return",  RETURN, NORID},
  203.       {"enum",  ENUM, NORID},
  204.       {"",}, 
  205.       {"double",  TYPESPEC, RID_DOUBLE},
  206.       {"signed",  TYPESPEC, RID_SIGNED},
  207.       {"float",  TYPESPEC, RID_FLOAT},
  208.       {"",}, {"",}, 
  209.       {"volatile",  TYPE_QUAL, RID_VOLATILE},
  210.       {"",}, 
  211.       {"const",  TYPE_QUAL, RID_CONST},
  212.       {"",}, 
  213.       {"unsigned",  TYPESPEC, RID_UNSIGNED},
  214.       {"",}, {"",}, {"",}, {"",}, 
  215.       {"continue",  CONTINUE, NORID},
  216.       {"",}, 
  217.       {"register",  SCSPEC, RID_REGISTER},
  218.       {"",}, {"",}, {"",}, {"",}, 
  219.       {"goto",  GOTO, NORID},
  220.       {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
  221.       {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
  222.       
  223.       {"union",  UNION, NORID},
  224.       {"",}, {"",}, {"",}, {"",}, 
  225.       {"long",  TYPESPEC, RID_LONG},
  226.     };
  227.  
  228.   if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
  229.     {
  230.       register int key = hash (str, len);
  231.  
  232.       if (key <= MAX_HASH_VALUE && key >= 0)
  233.         {
  234.           register char *s = wordlist[key].name;
  235.  
  236.           if (*s == *str && !strcmp (str + 1, s + 1))
  237.             return &wordlist[key];
  238.         }
  239.     }
  240.   return 0;
  241. }
  242.  
  243. /* Return something to represent absolute declarators containing a *.
  244.    TARGET is the absolute declarator that the * contains.
  245.    TYPE_QUALS is a list of modifiers such as const or volatile
  246.    to apply to the pointer type, represented as identifiers.
  247.  
  248.    We return an INDIRECT_REF whose "contents" are TARGET
  249.    and whose type is the modifier list.  */
  250.  
  251. tree
  252. make_pointer_declarator (type_quals, target)
  253.      tree type_quals, target;
  254. {
  255.   return build1 (INDIRECT_REF, type_quals, target);
  256. }
  257.  
  258. void
  259. init_lex ()
  260. {
  261.   /* Make identifier nodes long enough for the language-specific slots.  */
  262.   set_identifier_size (sizeof (struct lang_identifier));
  263.  
  264.   /* Start it at 0, because check_newline is called at the very beginning
  265.      and will increment it to 1.  */
  266.   lineno = 0;
  267.  
  268. #ifdef MULTIBYTE_CHARS
  269.   /* Change to the native locale for multibyte conversions.  */
  270.   setlocale (LC_CTYPE, "");
  271. #endif
  272.  
  273.   maxtoken = 40;
  274.   token_buffer = (char *) xmalloc (maxtoken + 2);
  275.  
  276.   ridpointers[(int) RID_INT] = get_identifier ("int");
  277.   ridpointers[(int) RID_CHAR] = get_identifier ("c