(expand_inline_function): If called function calls alloca, save and
[official-gcc.git] / gcc / scan.c
blobb62899081916f57e02b35b2b532df494403b87c3
1 /* Utility functions for scan-decls and fix-header programs.
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include "scan.h"
19 #include "hconfig.h"
20 #include <ctype.h>
22 int lineno = 1;
23 int source_lineno = 1;
24 sstring source_filename;
26 void
27 make_sstring_space (str, count)
28 sstring *str;
29 int count;
31 int cur_pos = str->ptr - str->base;
32 int cur_size = str->limit - str->base;
33 int new_size = cur_pos + count + 100;
35 if (new_size <= cur_size)
36 return;
38 if (str->base == NULL)
39 str->base = xmalloc (new_size);
40 else
41 str->base = xrealloc (str->base, new_size);
42 str->ptr = str->base + cur_size;
43 str->limit = str->base + new_size;
46 void
47 sstring_append (dst, src)
48 sstring *dst;
49 sstring *src;
51 register char *d, *s;
52 register count = SSTRING_LENGTH(src);
53 MAKE_SSTRING_SPACE(dst, count + 1);
54 d = dst->ptr;
55 s = src->base;
56 while (--count >= 0) *d++ = *s++;
57 dst->ptr = d;
58 *d = 0;
61 memory_full ()
63 abort();
66 char *
67 xmalloc (size)
68 unsigned size;
70 register char *ptr = (char *) malloc (size);
71 if (ptr != 0) return (ptr);
72 memory_full ();
73 /*NOTREACHED*/
74 return 0;
78 char *
79 xrealloc (old, size)
80 char *old;
81 unsigned size;
83 register char *ptr = (char *) realloc (old, size);
84 if (ptr != 0) return (ptr);
85 memory_full ();
86 /*NOTREACHED*/
87 return 0;
90 int
91 scan_ident (fp, s, c)
92 register FILE *fp;
93 register sstring *s;
94 int c;
96 s->ptr = s->base;
97 if (isalpha(c) || c == '_')
99 for (;;)
101 SSTRING_PUT(s, c);
102 c = getc (fp);
103 if (c == EOF || !(isalnum(c) || c == '_'))
104 break;
107 MAKE_SSTRING_SPACE(s, 1);
108 *s->ptr = 0;
109 return c;
113 scan_string (fp, s, init)
114 register FILE *fp;
115 register sstring *s;
117 int c;
118 for (;;)
120 c = getc (fp);
121 if (c == EOF || c == '\n')
122 break;
123 if (c == init)
125 c = getc (fp);
126 break;
128 if (c == '\\')
130 c = getc (fp);
131 if (c == EOF)
132 break;
133 if (c == '\n')
134 continue;
136 SSTRING_PUT(s, c);
138 MAKE_SSTRING_SPACE(s, 1);
139 *s->ptr = 0;
140 return c;
143 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
146 skip_spaces (fp, c)
147 register FILE *fp;
148 int c;
150 for (;;)
152 if (c == ' ' || c == '\t')
153 c = getc (fp);
154 else if (c == '/')
156 c = getc (fp);
157 if (c != '*')
159 ungetc (c, fp);
160 return '/';
162 c = getc (fp);
163 for (;;)
165 if (c == EOF)
166 return EOF;
167 else if (c != '*')
169 if (c == '\n')
170 source_lineno++, lineno++;
171 c = getc (fp);
173 else if ((c = getc (fp)) == '/')
174 return getc (fp);
177 else
178 break;
180 return c;
184 read_upto (fp, str, delim)
185 FILE *fp;
186 sstring *str;
187 int delim;
189 int ch;
190 for (;;)
192 ch = getc (fp);
193 if (ch == EOF || ch == delim)
194 break;
195 SSTRING_PUT(str, ch);
197 MAKE_SSTRING_SPACE(str, 1);
198 *str->ptr = 0;
199 return ch;
203 get_token (fp, s)
204 register FILE *fp;
205 register sstring *s;
207 int c;
208 s->ptr = s->base;
209 retry:
210 c = ' ';
211 c = skip_spaces (fp, c);
212 if (c == '\n')
214 source_lineno++;
215 lineno++;
216 goto retry;
218 if (c == '#')
220 c = get_token (fp, s);
221 if (c == INT_TOKEN)
223 source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
224 get_token (fp, &source_filename);
226 for (;;)
228 c = getc (fp);
229 if (c == EOF)
230 return EOF;
231 if (c == '\n')
233 source_lineno++;
234 lineno++;
235 goto retry;
239 if (c == EOF)
240 return EOF;
241 if (isdigit (c))
245 SSTRING_PUT(s, c);
246 c = getc (fp);
247 } while (c != EOF && isdigit(c));
248 ungetc (c, fp);
249 c = INT_TOKEN;
250 goto done;
252 if (isalpha (c) || c == '_')
254 c = scan_ident (fp, s, c);
255 ungetc (c, fp);
256 return IDENTIFIER_TOKEN;
258 if (c == '\'' || c == '"')
260 c = scan_string (fp, s, c);
261 ungetc (c, fp);
262 return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
264 SSTRING_PUT(s, c);
265 done:
266 MAKE_SSTRING_SPACE(s, 1);
267 *s->ptr = 0;
268 return c;
271 unsigned long
272 hash (str)
273 char *str;
275 int h = 0;
276 /* Replace this with something faster/better! FIXME! */
277 while (*str) h = (h << 3) + *str++;
278 return h & 0x7FFFFFFF;