2000-07-03 Donn Terry (donnte@microsoft.com)
[official-gcc.git] / gcc / cpphash.c
blobee0521b59770769b67d0d88fc600529eaccb31a8
1 /* Part of CPP library. (Macro handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "hashtab.h"
31 #include "obstack.h"
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 /* This is the second argument to eq_HASHNODE. */
37 struct hashdummy
39 const U_CHAR *name;
40 unsigned short length;
43 /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
44 #define HASHSIZE 500
46 static unsigned int hash_HASHNODE PARAMS ((const void *));
47 static int eq_HASHNODE PARAMS ((const void *, const void *));
48 static int dump_hash_helper PARAMS ((void **, void *));
50 static void dump_funlike_macro PARAMS ((cpp_reader *, cpp_hashnode *));
52 static const cpp_token *count_params PARAMS ((cpp_reader *,
53 const cpp_token *,
54 cpp_toklist *));
55 static int is__va_args__ PARAMS ((cpp_reader *, const cpp_token *));
56 static cpp_toklist *parse_define PARAMS((cpp_reader *));
57 static int check_macro_redefinition PARAMS((cpp_reader *, cpp_hashnode *hp,
58 const cpp_toklist *));
59 static int save_expansion PARAMS((cpp_reader *, cpp_toklist *,
60 const cpp_token *, const cpp_token *));
61 static unsigned int find_param PARAMS ((const cpp_token *,
62 const cpp_token *));
64 static const unsigned char var_args_str[] = "__VA_ARGS__";
66 /* Calculate hash of a string of length LEN. */
67 unsigned int
68 _cpp_calc_hash (str, len)
69 const U_CHAR *str;
70 size_t len;
72 size_t n = len;
73 unsigned int r = 0;
76 r = r * 67 + (*str++ - 113);
77 while (--n);
78 return r + len;
81 /* Calculate hash of a cpp_hashnode structure. */
82 static unsigned int
83 hash_HASHNODE (x)
84 const void *x;
86 const cpp_hashnode *h = (const cpp_hashnode *)x;
87 return h->hash;
90 /* Compare a cpp_hashnode structure (already in the table) with a
91 hashdummy structure (not yet in the table). This relies on the
92 rule that the existing entry is the first argument, the potential
93 entry the second. It also relies on the comparison function never
94 being called except as a direct consequence of a call to
95 htab_find(_slot)_with_hash. */
96 static int
97 eq_HASHNODE (x, y)
98 const void *x;
99 const void *y;
101 const cpp_hashnode *a = (const cpp_hashnode *)x;
102 const struct hashdummy *b = (const struct hashdummy *)y;
104 return (a->length == b->length
105 && !ustrncmp (a->name, b->name, a->length));
108 /* Find the hash node for name "name", of length LEN. */
110 cpp_hashnode *
111 cpp_lookup (pfile, name, len)
112 cpp_reader *pfile;
113 const U_CHAR *name;
114 int len;
116 struct hashdummy dummy;
117 cpp_hashnode *new, **slot;
118 unsigned int hash;
119 U_CHAR *p;
121 dummy.name = name;
122 dummy.length = len;
123 hash = _cpp_calc_hash (name, len);
125 slot = (cpp_hashnode **)
126 htab_find_slot_with_hash (pfile->hashtab, (void *)&dummy, hash, INSERT);
127 if (*slot)
128 return *slot;
130 /* Create a new hash node. */
131 p = obstack_alloc (pfile->hash_ob, sizeof (cpp_hashnode) + len);
132 new = (cpp_hashnode *)p;
133 p += offsetof (cpp_hashnode, name);
135 new->type = T_VOID;
136 new->length = len;
137 new->hash = hash;
138 new->fe_value = 0;
139 new->value.expansion = NULL;
141 memcpy (p, name, len);
142 p[len] = 0;
144 *slot = new;
145 return new;
148 /* Set up and tear down internal structures for macro expansion. */
149 void
150 _cpp_init_macros (pfile)
151 cpp_reader *pfile;
153 pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
154 eq_HASHNODE, (htab_del) _cpp_free_definition);
155 pfile->hash_ob = xnew (struct obstack);
156 obstack_init (pfile->hash_ob);
159 void
160 _cpp_cleanup_macros (pfile)
161 cpp_reader *pfile;
163 htab_delete (pfile->hashtab);
164 obstack_free (pfile->hash_ob, 0);
165 free (pfile->hash_ob);
168 /* Free the definition of macro H. */
170 void
171 _cpp_free_definition (h)
172 cpp_hashnode *h;
174 if (h->type == T_MACRO)
175 _cpp_free_toklist (h->value.expansion);
176 h->value.expansion = NULL;
179 /* Scans for a given token, returning the parameter number if found,
180 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
181 CPP_CLOSE_PAREN for TOKEN. */
182 static unsigned int
183 find_param (first, token)
184 const cpp_token *first, *token;
186 unsigned int param = 0;
188 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
189 if (first->type == CPP_NAME)
191 param++;
192 if (first->val.name.len == token->val.name.len
193 && !memcmp (first->val.name.text, token->val.name.text,
194 token->val.name.len))
195 return param;
198 return 0;
201 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
202 replacement list of a variable-arguments macro. TOKEN is assumed
203 to be of type CPP_NAME. */
204 static int
205 is__va_args__ (pfile, token)
206 cpp_reader *pfile;
207 const cpp_token *token;
209 if (!CPP_OPTION (pfile, pedantic)
210 || token->val.name.len != sizeof (var_args_str) - 1
211 || ustrncmp (token->val.name.text, var_args_str,
212 sizeof (var_args_str) - 1))
213 return 0;
215 cpp_pedwarn_with_line (pfile, token->line, token->col,
216 "\"%s\" is only valid in the replacement list of a function-like macro",
217 var_args_str);
218 return 1;
221 /* Counts the parameters to a function like macro, and saves their
222 spellings if necessary. Returns the token that we stopped scanning
223 at; if it's type isn't CPP_CLOSE_PAREN there was an error, which
224 has been reported. */
225 static const cpp_token *
226 count_params (pfile, first, list)
227 cpp_reader *pfile;
228 const cpp_token *first;
229 cpp_toklist *list;
231 unsigned int params_len = 0, prev_ident = 0;
232 const cpp_token *token, *temp;
234 list->paramc = 0;
235 for (token = first;; token++)
237 switch (token->type)
239 case CPP_EOF:
240 missing_paren:
241 cpp_error_with_line (pfile, token->line, token->col,
242 "missing ')' in macro parameter list");
243 goto out;
245 case CPP_COMMENT:
246 continue; /* Ignore -C comments. */
248 case CPP_NAME:
249 if (prev_ident)
251 cpp_error_with_line (pfile, token->line, token->col,
252 "macro parameters must be comma-separated");
253 goto out;
256 /* Constraint 6.10.3.5 */
257 if (is__va_args__ (pfile, token))
258 goto out;
260 params_len += token->val.name.len + 1;
261 prev_ident = 1;
262 list->paramc++;
264 /* Constraint 6.10.3.6 - duplicate parameter names. */
265 if (find_param (first, token))
267 cpp_error_with_line (pfile, token->line, token->col,
268 "duplicate macro parameter \"%.*s\"",
269 (int) token->val.name.len,
270 token->val.name.text);
271 goto out;
273 break;
275 default:
276 cpp_error_with_line (pfile, token->line, token->col,
277 "illegal token in macro parameter list");
278 goto out;
280 case CPP_CLOSE_PAREN:
281 if (prev_ident || list->paramc == 0)
282 goto scanned;
284 /* Fall through to pick up the error. */
285 case CPP_COMMA:
286 if (!prev_ident)
288 cpp_error_with_line (pfile, token->line, token->col,
289 "parameter name expected");
290 if (token->type == CPP_CLOSE_PAREN)
291 token--; /* Return the ',' not ')'. */
292 goto out;
294 prev_ident = 0;
295 break;
297 case CPP_ELLIPSIS:
298 /* Convert ISO-style var_args to named varargs by changing
299 the ellipsis into an identifier with name __VA_ARGS__.
300 This simplifies other handling. We can safely have its
301 text outside list->namebuf because there is no reason to
302 extend the size of the list's namebuf (and thus change
303 the pointer) in do_define. */
304 if (!prev_ident)
306 cpp_token *tok = (cpp_token *) token;
308 tok->type = CPP_NAME;
309 tok->val.name.len = sizeof (var_args_str) - 1;
310 tok->val.name.text = var_args_str; /* Safe. */
311 list->paramc++;
313 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
314 cpp_pedwarn (pfile,
315 "C89 does not permit anon varargs macros");
317 else
319 list->flags |= GNU_REST_ARGS;
320 if (CPP_PEDANTIC (pfile))
321 cpp_pedwarn (pfile,
322 "ISO C does not permit named varargs parameters");
325 list->flags |= VAR_ARGS;
326 token++;
327 if (token->type == CPP_CLOSE_PAREN)
328 goto scanned;
329 goto missing_paren;
333 scanned:
334 /* Store the null-terminated parameter spellings of a function, to
335 provide pedantic warnings to satisfy 6.10.3.2, or for use when
336 dumping macro definitions. */
337 if (list->paramc > 0 && pfile->save_parameter_spellings)
339 U_CHAR *buf;
341 _cpp_reserve_name_space (list, params_len);
342 list->params_len = list->name_used = params_len;
343 buf = list->namebuf;
344 for (temp = first; temp <= token; temp++)
345 if (temp->type == CPP_NAME)
347 memcpy (buf, temp->val.name.text, temp->val.name.len);
348 buf += temp->val.name.len;
349 *buf++ = '\0';
353 out:
354 return token;
357 /* Parses a #define directive. Returns null pointer on error. */
358 static cpp_toklist *
359 parse_define (pfile)
360 cpp_reader *pfile;
362 const cpp_token *token, *first_param;
363 cpp_toklist *list;
364 int prev_white = 0;
366 /* The first token after the macro's name. */
367 token = cpp_get_token (pfile);
369 /* Constraint 6.10.3.5 */
370 if (is__va_args__ (pfile, token - 1))
371 return 0;
373 while (token->type == CPP_COMMENT)
374 token++, prev_white = 1;
376 /* Allocate the expansion's list. It will go in the hash table. */
377 list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
378 _cpp_init_toklist (list, 0);
379 first_param = token + 1;
380 list->paramc = -1; /* Object-like macro. */
382 if (!prev_white && !(token->flags & PREV_WHITE))
384 if (token->type == CPP_OPEN_PAREN)
386 token = count_params (pfile, first_param, list);
387 if (token->type != CPP_CLOSE_PAREN)
388 goto error;
389 token++;
391 else if (token->type != CPP_EOF)
392 cpp_pedwarn (pfile,
393 "ISO C requires whitespace after the macro name");
396 if (save_expansion (pfile, list, token, first_param))
398 error:
399 _cpp_free_toklist (list);
400 list = 0;
403 return list;
406 static int
407 check_macro_redefinition (pfile, hp, list2)
408 cpp_reader *pfile;
409 cpp_hashnode *hp;
410 const cpp_toklist *list2;
412 const cpp_toklist *list1;
414 if (hp->type != T_MACRO)
415 return ! pfile->done_initializing;
417 /* Clear the whitespace and BOL flags of the first tokens. They get
418 altered during macro expansion, but is not significant here. */
419 list1 = hp->value.expansion;
420 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
421 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
423 if (!_cpp_equiv_toklists (list1, list2))
424 return 0;
426 if (CPP_OPTION (pfile, pedantic)
427 && list1->paramc > 0
428 && (list1->params_len != list2->params_len
429 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
430 return 0;
432 return 1;
435 /* Copy the tokens of the expansion. Change the type of macro
436 arguments from CPP_NAME to CPP_MACRO_ARG. Remove #'s that
437 represent stringification, flagging the CPP_MACRO_ARG it operates
438 on STRINGIFY. Remove ##'s, flagging the token on its immediate
439 left PASTE_LEFT. Returns non-zero on error. */
440 static int
441 save_expansion (pfile, list, first, first_param)
442 cpp_reader *pfile;
443 cpp_toklist *list;
444 const cpp_token *first;
445 const cpp_token *first_param;
447 const cpp_token *token;
448 cpp_token *dest;
449 unsigned int len, ntokens;
450 unsigned char *buf;
452 /* Count tokens in expansion. We drop paste tokens, and stringize
453 tokens, so don't count them. */
454 ntokens = len = 0;
455 for (token = first; token->type != CPP_EOF; token++)
457 const char *msg;
459 if (token->type == CPP_PASTE)
461 /* Token-paste ##, but is a normal token if traditional. */
462 if (! CPP_TRADITIONAL (pfile))
464 msg = "\"##\" cannot appear at either end of a macro expansion";
465 /* Constraint 6.10.3.3.1 */
466 if (token == first || token[1].type == CPP_EOF)
467 goto error;
468 continue;
471 else if (token->type == CPP_HASH)
473 /* Stringifying #, but is a normal character if traditional,
474 or in object-like macros. Constraint 6.10.3.2.1. */
475 if (list->paramc >= 0 && ! CPP_TRADITIONAL (pfile))
477 if (token[1].type == CPP_NAME
478 && find_param (first_param, token + 1))
479 continue;
480 if (! CPP_OPTION (pfile, lang_asm))
482 msg = "'#' is not followed by a macro parameter";
483 error:
484 cpp_error_with_line (pfile, token->line, token->col, msg);
485 return 1;
489 else if (token->type == CPP_NAME)
491 /* Constraint 6.10.3.5 */
492 if (!(list->flags & VAR_ARGS) && is__va_args__ (pfile, token))
493 return 1;
495 ntokens++;
496 if (token_spellings[token->type].type > SPELL_NONE)
497 len += token->val.name.len;
500 /* Allocate space to hold the tokens. Empty expansions are stored
501 as a single placemarker token. */
502 if (ntokens == 0)
503 ntokens++;
504 _cpp_expand_token_space (list, ntokens);
505 if (len > 0)
506 _cpp_expand_name_space (list, len);
508 dest = list->tokens;
509 buf = list->namebuf + list->name_used;
510 for (token = first; token->type != CPP_EOF; token++)
512 unsigned int param_no;
514 switch (token->type)
516 case CPP_NAME:
517 if (list->paramc == -1)
518 break;
520 /* Check if the name is a macro parameter. */
521 param_no = find_param (first_param, token);
522 if (param_no == 0)
523 break;
524 dest->val.aux = param_no - 1;
526 dest->type = CPP_MACRO_ARG;
527 if (token[-1].type == CPP_HASH && ! CPP_TRADITIONAL (pfile))
528 dest->flags = token[-1].flags | STRINGIFY_ARG;
529 else
530 dest->flags = token->flags; /* Particularly PREV_WHITE. */
531 dest++;
532 continue;
534 case CPP_PASTE:
535 if (! CPP_TRADITIONAL (pfile))
537 dest[-1].flags |= PASTE_LEFT;
538 continue;
540 break;
542 case CPP_HASH:
543 /* Stringifying #. Constraint 6.10.3.2.1 */
544 if (list->paramc >= 0 && ! CPP_TRADITIONAL (pfile)
545 && token[1].type == CPP_NAME
546 && find_param (first_param, token + 1))
547 continue;
548 break;
550 default:
551 break;
554 /* Copy the token. */
555 *dest = *token;
556 if (token_spellings[token->type].type > SPELL_NONE)
558 memcpy (buf, token->val.name.text, token->val.name.len);
559 dest->val.name.text = buf;
560 buf += dest->val.name.len;
562 dest++;
565 if (dest == list->tokens)
567 dest->type = CPP_PLACEMARKER;
568 dest->flags = 0;
571 list->tokens_used = ntokens;
572 list->line = pfile->token_list.line;
573 list->file = pfile->token_list.file;
574 list->name_used = len;
576 return 0;
580 _cpp_create_definition (pfile, hp)
581 cpp_reader *pfile;
582 cpp_hashnode *hp;
584 cpp_toklist *list;
586 list = parse_define (pfile);
587 if (!list)
588 return 0;
590 /* Check for a redefinition. Redefinition of a macro is allowed if
591 and only if the old and new definitions are the same.
592 (6.10.3 paragraph 2). */
594 if (hp->type != T_VOID)
596 if (!check_macro_redefinition (pfile, hp, list))
598 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
599 if (pfile->done_initializing && hp->type == T_MACRO)
600 cpp_pedwarn_with_file_and_line (pfile,
601 hp->value.expansion->file,
602 hp->value.expansion->line, 1,
603 "this is the location of the previous definition");
605 _cpp_free_definition (hp);
608 /* Enter definition in hash table. */
609 hp->type = T_MACRO;
610 hp->value.expansion = list;
612 return 1;
615 /* Dump the definition of macro MACRO on stdout. The format is suitable
616 to be read back in again. */
618 void
619 _cpp_dump_definition (pfile, hp)
620 cpp_reader *pfile;
621 cpp_hashnode *hp;
623 CPP_RESERVE (pfile, hp->length + sizeof "#define ");
624 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
625 CPP_PUTS_Q (pfile, hp->name, hp->length);
627 if (hp->type == T_MACRO)
629 if (hp->value.expansion->paramc >= 0)
630 dump_funlike_macro (pfile, hp);
631 else
633 const cpp_toklist *list = hp->value.expansion;
634 list->tokens[0].flags &= ~BOL;
635 list->tokens[0].flags |= PREV_WHITE;
636 _cpp_dump_list (pfile, list, list->tokens, 1);
639 else
640 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
642 if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
643 CPP_PUTC (pfile, '\n');
646 static void
647 dump_funlike_macro (pfile, node)
648 cpp_reader *pfile;
649 cpp_hashnode *node;
651 int i = 0;
652 const cpp_toklist * list = node->value.expansion;
653 const U_CHAR *param;
655 param = list->namebuf;
656 CPP_PUTC_Q (pfile, '(');
657 for (i = 0; i++ < list->paramc;)
659 unsigned int len;
661 len = ustrlen (param);
662 CPP_PUTS (pfile, param, len);
663 if (i < list->paramc)
664 CPP_PUTS(pfile, ", ", 2);
665 else if (list->flags & VAR_ARGS)
667 if (!ustrcmp (param, var_args_str))
668 pfile->limit -= sizeof (var_args_str) - 1;
669 CPP_PUTS (pfile, "...", 3);
671 param += len + 1;
673 CPP_PUTC (pfile, ')');
674 list->tokens[0].flags &= ~BOL;
675 list->tokens[0].flags |= PREV_WHITE;
676 _cpp_dump_list (pfile, list, list->tokens, 1);
679 /* Dump out the hash table. */
680 static int
681 dump_hash_helper (h, p)
682 void **h;
683 void *p;
685 cpp_hashnode *hp = (cpp_hashnode *)*h;
686 cpp_reader *pfile = (cpp_reader *)p;
688 if (hp->type == T_MACRO)
689 _cpp_dump_definition (pfile, hp);
690 return 1;
693 void
694 _cpp_dump_macro_hash (pfile)
695 cpp_reader *pfile;
697 htab_traverse (pfile->hashtab, dump_hash_helper, pfile);