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
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! */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 /* This is the second argument to eq_HASHNODE. */
41 unsigned short length;
44 /* Stores basic information about a macro, before it is allocated. */
47 unsigned int paramlen;
52 /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
55 static unsigned int hash_HASHNODE PARAMS ((const void *));
56 static int eq_HASHNODE PARAMS ((const void *, const void *));
57 static int dump_hash_helper PARAMS ((void **, void *));
59 static void dump_funlike_macro PARAMS ((cpp_reader *, cpp_hashnode *));
61 static const cpp_token *count_params PARAMS ((cpp_reader *,
63 struct macro_info *));
64 static int is__va_args__ PARAMS ((cpp_reader *, const cpp_token *));
65 static const cpp_toklist * parse_define PARAMS((cpp_reader *));
66 static int check_macro_redefinition PARAMS((cpp_reader *, cpp_hashnode *hp,
67 const cpp_toklist *));
68 static const cpp_toklist * save_expansion PARAMS((cpp_reader *,
71 struct macro_info *));
72 static unsigned int find_param PARAMS ((const cpp_token *,
74 static cpp_toklist * alloc_macro PARAMS ((cpp_reader *,
76 unsigned int, unsigned int));
77 static void free_macro PARAMS((const cpp_toklist *));
79 /* Calculate hash of a string of length LEN. */
81 _cpp_calc_hash (str, len)
89 r = r * 67 + (*str++ - 113);
94 /* Calculate hash of a cpp_hashnode structure. */
99 const cpp_hashnode *h = (const cpp_hashnode *)x;
103 /* Compare a cpp_hashnode structure (already in the table) with a
104 hashdummy structure (not yet in the table). This relies on the
105 rule that the existing entry is the first argument, the potential
106 entry the second. It also relies on the comparison function never
107 being called except as a direct consequence of a call to
108 the htab_find routines. */
114 const cpp_hashnode *a = (const cpp_hashnode *)x;
115 const struct hashdummy *b = (const struct hashdummy *)y;
117 return (a->hash == b->hash
118 && a->length == b->length
119 && !memcmp (a->name, b->name, a->length));
122 /* Find the hash node for name "name", of length LEN. */
125 cpp_lookup (pfile, name, len)
130 struct hashdummy dummy;
131 cpp_hashnode *new, **slot;
137 dummy.hash = hash = _cpp_calc_hash (name, len);
139 slot = (cpp_hashnode **)
140 htab_find_slot_with_hash (pfile->hashtab, (void *)&dummy, hash, INSERT);
144 /* Create a new hash node. */
145 p = obstack_alloc (pfile->hash_ob, sizeof (cpp_hashnode) + len);
146 new = (cpp_hashnode *)p;
147 p += offsetof (cpp_hashnode, name);
153 new->value.expansion = NULL;
155 memcpy (p, name, len);
162 /* Set up and tear down internal structures for macro expansion. */
164 _cpp_init_macros (pfile)
167 pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
168 eq_HASHNODE, (htab_del) _cpp_free_definition);
169 pfile->hash_ob = xnew (struct obstack);
170 obstack_init (pfile->hash_ob);
174 _cpp_cleanup_macros (pfile)
177 htab_delete (pfile->hashtab);
178 obstack_free (pfile->hash_ob, 0);
179 free (pfile->hash_ob);
182 /* Free the definition of macro H. */
185 _cpp_free_definition (h)
188 if (h->type == T_MACRO)
189 free_macro (h->value.expansion);
190 h->value.expansion = NULL;
193 /* Scans for a given token, returning the parameter number if found,
194 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
195 CPP_CLOSE_PAREN for TOKEN. */
197 find_param (first, token)
198 const cpp_token *first, *token;
200 unsigned int param = 0;
202 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
203 if (first->type == CPP_NAME)
206 if (first->val.node == token->val.node)
213 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
214 replacement list of a variable-arguments macro. TOKEN is assumed
215 to be of type CPP_NAME. */
217 is__va_args__ (pfile, token)
219 const cpp_token *token;
221 if (!CPP_PEDANTIC (pfile)
222 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
225 cpp_pedwarn_with_line (pfile, token->line, token->col,
226 "\"%s\" is only valid in the replacement list of a function-like macro",
227 token->val.node->name);
231 /* Counts the parameters to a function-like macro, the length of their
232 null-terminated names, and whether the macro is a variable-argument
233 one. FIRST is the token immediately after the open parenthesis,
234 INFO stores the data, and should have paramlen and flags zero.
236 Returns the token that we stopped scanning at; if it's type isn't
237 CPP_CLOSE_PAREN there was an error, which has been reported. */
238 static const cpp_token *
239 count_params (pfile, first, info)
241 const cpp_token *first;
242 struct macro_info *info;
244 unsigned int prev_ident = 0;
245 const cpp_token *token;
248 for (token = first;; token++)
254 cpp_error_with_line (pfile, token->line, token->col,
255 "missing ')' in macro parameter list");
259 continue; /* Ignore -C comments. */
264 cpp_error_with_line (pfile, token->line, token->col,
265 "macro parameters must be comma-separated");
269 /* Constraint 6.10.3.5 */
270 if (is__va_args__ (pfile, token))
273 /* Constraint 6.10.3.6 - duplicate parameter names. */
274 if (find_param (first, token))
276 cpp_error_with_line (pfile, token->line, token->col,
277 "duplicate macro parameter \"%s\"",
278 token->val.node->name);
284 if (pfile->save_parameter_spellings)
285 info->paramlen += token->val.node->length + 1;
289 cpp_error_with_line (pfile, token->line, token->col,
290 "illegal token in macro parameter list");
293 case CPP_CLOSE_PAREN:
294 if (prev_ident || info->paramc == 0)
297 /* Fall through to pick up the error. */
301 cpp_error_with_line (pfile, token->line, token->col,
302 "parameter name expected");
303 if (token->type == CPP_CLOSE_PAREN)
304 token--; /* Return the ',' not ')'. */
311 /* Convert ISO-style var_args to named varargs by changing
312 the ellipsis into an identifier with name __VA_ARGS__.
313 This simplifies other handling. */
316 cpp_token *tok = (cpp_token *) token;
318 tok->type = CPP_NAME;
319 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
322 if (pfile->save_parameter_spellings)
323 info->paramlen += tok->val.node->length + 1;
325 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
327 "C89 does not permit anon varargs macros");
331 info->flags |= GNU_REST_ARGS;
332 if (CPP_PEDANTIC (pfile))
334 "ISO C does not permit named varargs parameters");
337 info->flags |= VAR_ARGS;
339 if (token->type == CPP_CLOSE_PAREN)
349 /* Parses a #define directive. Returns null pointer on error. */
350 static const cpp_toklist *
354 const cpp_token *token, *first_param;
355 struct macro_info info;
358 /* The first token after the macro's name. */
359 token = _cpp_get_token (pfile);
361 /* Constraint 6.10.3.5 */
362 if (is__va_args__ (pfile, token - 1))
365 while (token->type == CPP_COMMENT)
366 token++, prev_white = 1;
367 first_param = token + 1;
369 /* Assume object-like macro. */
374 if (!prev_white && !(token->flags & PREV_WHITE))
376 if (token->type == CPP_OPEN_PAREN)
378 token = count_params (pfile, first_param, &info);
379 if (token->type != CPP_CLOSE_PAREN)
383 else if (token->type != CPP_EOF)
385 "ISO C requires whitespace after the macro name");
388 return save_expansion (pfile, token, first_param, &info);
392 check_macro_redefinition (pfile, hp, list2)
395 const cpp_toklist *list2;
397 const cpp_toklist *list1;
399 if (hp->type != T_MACRO)
400 return ! pfile->done_initializing;
402 /* Clear the whitespace and BOL flags of the first tokens. They get
403 altered during macro expansion, but is not significant here. */
404 list1 = hp->value.expansion;
405 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
406 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
408 if (!_cpp_equiv_toklists (list1, list2))
411 if (CPP_OPTION (pfile, pedantic)
413 && (list1->params_len != list2->params_len
414 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
420 /* This is a dummy structure whose only purpose is getting alignment
425 cpp_token first_token;
429 /* Allocate space to hold the token list, its tokens, their text, and
430 the parameter names if needed. Empty expansions are stored as a
431 single placemarker token.
433 These are all allocated in a block together for performance
434 reasons. Therefore, this token list cannot be expanded like a
435 normal token list. Try to do so, and you lose. */
437 alloc_macro (pfile, info, ntokens, len)
439 struct macro_info *info;
440 unsigned int ntokens, len;
443 struct toklist_dummy *dummy;
446 size = sizeof (struct toklist_dummy);
447 size += (ntokens - 1) * sizeof(cpp_token);
448 size += len + info->paramlen;
450 dummy = (struct toklist_dummy *) xmalloc (size);
451 list = (cpp_toklist *) dummy;
453 /* Initialize the monster. */
454 list->tokens = &dummy->first_token;
455 list->tokens_used = list->tokens_cap = ntokens;
457 list->namebuf = (unsigned char *) &list->tokens[ntokens];
458 list->name_used = list->name_cap = len + info->paramlen;
461 list->line = pfile->token_list.line;
462 list->file = pfile->token_list.file;
463 list->params_len = info->paramlen;
464 list->paramc = info->paramc;
465 list->flags = info->flags;
470 /* Free a macro allocated by allocate_macro. */
473 const cpp_toklist *list;
478 /* Copy the tokens of the expansion, beginning with FIRST until
479 CPP_EOF. For a function-like macro, FIRST_PARAM points to the
480 first parameter. INFO contains information about the macro.
482 Change the type of macro arguments in the expansion from CPP_NAME
483 to CPP_MACRO_ARG. Remove #'s that represent stringification,
484 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
485 flagging the token on its immediate left PASTE_LEFT. Returns the
486 token list for the macro expansion, or 0 on error. */
487 static const cpp_toklist *
488 save_expansion (pfile, first, first_param, info)
490 const cpp_token *first;
491 const cpp_token *first_param;
492 struct macro_info *info;
494 const cpp_token *token;
497 unsigned int len, ntokens;
500 /* Count tokens in expansion. We drop paste tokens, and stringize
501 tokens, so don't count them. */
503 for (token = first; token->type != CPP_EOF; token++)
505 if (token->type == CPP_PASTE)
507 /* Token-paste ##, can appear in both object-like and
508 function-like macros, but not at the ends. Constraint
510 if (token == first || token[1].type == CPP_EOF)
512 cpp_error_with_line (pfile, token->line, token->col,
513 "'##' cannot appear at either end of a macro expansion");
518 else if (token->type == CPP_HASH)
520 /* Stringifying #, but a normal character in object-like
521 macros. Must come before a parameter name. Constraint
523 if (info->paramc >= 0)
525 if (token[1].type == CPP_NAME
526 && find_param (first_param, token + 1))
528 if (! CPP_OPTION (pfile, lang_asm))
530 cpp_error_with_line (pfile, token->line, token->col,
531 "'#' is not followed by a macro parameter");
536 else if (token->type == CPP_NAME)
538 /* Constraint 6.10.3.5 */
539 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
541 /* It might be worth doing a check here that we aren't a
542 macro argument, since we don't store the text of macro
543 arguments. This would reduce "len" and save space. */
546 if (TOKEN_SPELL (token) == SPELL_STRING)
547 len += token->val.str.len;
552 list = alloc_macro (pfile, info, ntokens, len);
555 /* Store the null-terminated parameter spellings of a macro, to
556 provide pedantic warnings to satisfy 6.10.3.2, or for use when
557 dumping macro definitions. They must go first. */
558 if (list->params_len)
559 for (token = first_param; token < first; token++)
560 if (token->type == CPP_NAME)
563 memcpy (buf, token->val.node->name, token->val.node->length + 1);
564 buf += token->val.node->length + 1;
568 for (token = first; token->type != CPP_EOF; token++)
570 unsigned int param_no;
575 if (list->paramc == -1)
578 /* Check if the name is a macro parameter. */
579 param_no = find_param (first_param, token);
582 dest->val.aux = param_no - 1;
584 dest->type = CPP_MACRO_ARG;
585 if (token[-1].type == CPP_HASH)
586 dest->flags = token[-1].flags | STRINGIFY_ARG;
588 dest->flags = token->flags; /* Particularly PREV_WHITE. */
593 dest[-1].flags |= PASTE_LEFT;
597 /* Stringifying #. Constraint 6.10.3.2.1 */
598 if (list->paramc >= 0 && token[1].type == CPP_NAME
599 && find_param (first_param, token + 1))
607 /* Copy the token. */
609 if (TOKEN_SPELL (token) == SPELL_STRING)
611 memcpy (buf, token->val.str.text, token->val.str.len);
612 dest->val.str.text = buf;
613 buf += dest->val.str.len;
618 if (dest == list->tokens)
620 dest->type = CPP_PLACEMARKER;
628 _cpp_create_definition (pfile, hp)
632 const cpp_toklist *list;
634 list = parse_define (pfile);
638 /* Check for a redefinition. Redefinition of a macro is allowed if
639 and only if the old and new definitions are the same.
640 (6.10.3 paragraph 2). */
642 if (hp->type != T_VOID)
644 if (!check_macro_redefinition (pfile, hp, list))
646 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
647 if (pfile->done_initializing && hp->type == T_MACRO)
648 cpp_pedwarn_with_file_and_line (pfile,
649 hp->value.expansion->file,
650 hp->value.expansion->line, 1,
651 "this is the location of the previous definition");
653 _cpp_free_definition (hp);
656 /* Enter definition in hash table. */
658 hp->value.expansion = list;
663 /* Dump the definition of macro MACRO on stdout. The format is suitable
664 to be read back in again. */
667 _cpp_dump_definition (pfile, hp)
671 CPP_RESERVE (pfile, hp->length + sizeof "#define ");
672 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
673 CPP_PUTS_Q (pfile, hp->name, hp->length);
675 if (hp->type == T_MACRO)
677 if (hp->value.expansion->paramc >= 0)
678 dump_funlike_macro (pfile, hp);
681 const cpp_toklist *list = hp->value.expansion;
682 list->tokens[0].flags &= ~BOL;
683 list->tokens[0].flags |= PREV_WHITE;
684 _cpp_dump_list (pfile, list, list->tokens, 1);
688 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
690 if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
691 CPP_PUTC (pfile, '\n');
695 dump_funlike_macro (pfile, node)
700 const cpp_toklist * list = node->value.expansion;
703 param = list->namebuf;
704 CPP_PUTC_Q (pfile, '(');
705 for (i = 0; i++ < list->paramc;)
709 len = ustrlen (param);
710 CPP_PUTS (pfile, param, len);
711 if (i < list->paramc)
712 CPP_PUTS(pfile, ", ", 2);
713 else if (list->flags & VAR_ARGS)
715 if (!ustrcmp (param, U"__VA_ARGS__"))
716 pfile->limit -= sizeof (U"__VA_ARGS__") - 1;
717 CPP_PUTS_Q (pfile, "...", 3);
721 CPP_PUTC (pfile, ')');
722 list->tokens[0].flags &= ~BOL;
723 list->tokens[0].flags |= PREV_WHITE;
724 _cpp_dump_list (pfile, list, list->tokens, 1);
727 /* Dump out the hash table. */
729 dump_hash_helper (h, p)
733 cpp_hashnode *hp = (cpp_hashnode *)*h;
734 cpp_reader *pfile = (cpp_reader *)p;
736 if (hp->type == T_MACRO)
737 _cpp_dump_definition (pfile, hp);
742 _cpp_dump_macro_hash (pfile)
745 htab_traverse (pfile->hashtab, dump_hash_helper, pfile);