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 const cpp_token *first_param; /* First parameter token. */
48 const cpp_token *first; /* First expansion token. */
49 unsigned int paramlen; /* Length of parameter names. */
50 unsigned int len; /* Length of token strings. */
51 unsigned int ntokens; /* Number of tokens in expansion. */
52 short paramc; /* Number of parameters. */
56 /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
59 static unsigned int hash_HASHNODE PARAMS ((const void *));
60 static int eq_HASHNODE PARAMS ((const void *, const void *));
61 static int dump_hash_helper PARAMS ((void **, void *));
62 static void dump_funlike_macro PARAMS ((cpp_reader *, cpp_hashnode *));
63 static void count_params PARAMS ((cpp_reader *, struct macro_info *));
64 static int is__va_args__ PARAMS ((cpp_reader *, const cpp_token *));
66 static int parse_define PARAMS((cpp_reader *, struct macro_info *));
67 static int check_macro_redefinition PARAMS((cpp_reader *, cpp_hashnode *hp,
68 const cpp_toklist *));
69 static const cpp_toklist * save_expansion PARAMS((cpp_reader *,
70 struct macro_info *));
71 static unsigned int find_param PARAMS ((const cpp_token *,
73 static cpp_toklist * alloc_macro PARAMS ((cpp_reader *, struct macro_info *));
75 /* Calculate hash of a string of length LEN. */
77 _cpp_calc_hash (str, len)
85 r = r * 67 + (*str++ - 113);
90 /* Calculate hash of a cpp_hashnode structure. */
95 const cpp_hashnode *h = (const cpp_hashnode *)x;
99 /* Compare a cpp_hashnode structure (already in the table) with a
100 hashdummy structure (not yet in the table). This relies on the
101 rule that the existing entry is the first argument, the potential
102 entry the second. It also relies on the comparison function never
103 being called except as a direct consequence of a call to
104 the htab_find routines. */
110 const cpp_hashnode *a = (const cpp_hashnode *)x;
111 const struct hashdummy *b = (const struct hashdummy *)y;
113 return (a->hash == b->hash
114 && a->length == b->length
115 && !memcmp (a->name, b->name, a->length));
118 /* Find the hash node for name "name", of length LEN. */
121 cpp_lookup (pfile, name, len)
126 struct hashdummy dummy;
127 cpp_hashnode *new, **slot;
133 dummy.hash = hash = _cpp_calc_hash (name, len);
135 slot = (cpp_hashnode **)
136 htab_find_slot_with_hash (pfile->hashtab, (void *)&dummy, hash, INSERT);
140 /* Create a new hash node. */
141 p = obstack_alloc (pfile->hash_ob, sizeof (cpp_hashnode) + len);
142 new = (cpp_hashnode *)p;
143 p += offsetof (cpp_hashnode, name);
149 new->value.expansion = NULL;
151 memcpy (p, name, len);
158 /* Set up and tear down internal structures for macro expansion. */
160 _cpp_init_macros (pfile)
163 pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
164 eq_HASHNODE, (htab_del) _cpp_free_definition);
165 pfile->hash_ob = xnew (struct obstack);
166 obstack_init (pfile->hash_ob);
170 _cpp_cleanup_macros (pfile)
173 htab_delete (pfile->hashtab);
174 obstack_free (pfile->hash_ob, 0);
175 free (pfile->hash_ob);
178 /* Free the definition of macro H. */
181 _cpp_free_definition (h)
184 if (h->type == T_MACRO)
185 free ((PTR) h->value.expansion);
186 h->value.expansion = NULL;
189 /* Scans for a given token, returning the parameter number if found,
190 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
191 CPP_CLOSE_PAREN for TOKEN. */
193 find_param (first, token)
194 const cpp_token *first, *token;
196 unsigned int param = 0;
198 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
199 if (first->type == CPP_NAME)
202 if (first->val.node == token->val.node)
209 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
210 replacement list of a variable-arguments macro. TOKEN is assumed
211 to be of type CPP_NAME. */
213 is__va_args__ (pfile, token)
215 const cpp_token *token;
217 if (!CPP_PEDANTIC (pfile)
218 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
221 cpp_pedwarn_with_line (pfile, token->line, token->col,
222 "\"%s\" is only valid in the replacement list of a function-like macro",
223 token->val.node->name);
227 /* Counts the parameters to a function-like macro, the length of their
228 null-terminated names, and whether the macro is a variable-argument
229 one. FIRST is the token immediately after the open parenthesis,
230 INFO stores the data.
232 On success, info->first is updated to the token after the closing
233 parenthesis, i.e. the first token of the expansion. Otherwise
234 there was an error, which has been reported. */
236 count_params (pfile, info)
238 struct macro_info *info;
240 unsigned int prev_ident = 0;
241 const cpp_token *token;
246 info->first = info->first_param; /* Not a ')' indicating success. */
248 for (token = info->first_param;; token++)
253 cpp_error_with_line (pfile, token->line, token->col,
254 "illegal token in macro parameter list");
259 cpp_error_with_line (pfile, token->line, token->col,
260 "missing ')' in macro parameter list");
264 continue; /* Ignore -C comments. */
269 cpp_error_with_line (pfile, token->line, token->col,
270 "macro parameters must be comma-separated");
274 /* Constraint 6.10.3.5 */
275 if (is__va_args__ (pfile, token))
278 /* Constraint 6.10.3.6 - duplicate parameter names. */
279 if (find_param (info->first, token))
281 cpp_error_with_line (pfile, token->line, token->col,
282 "duplicate macro parameter \"%s\"",
283 token->val.node->name);
289 info->paramlen += token->val.node->length + 1;
292 case CPP_CLOSE_PAREN:
293 if (prev_ident || info->paramc == 0)
296 /* Fall through to pick up the error. */
300 cpp_error_with_line (pfile, token->line, token->col,
301 "parameter name expected");
308 /* Convert ISO-style var_args to named varargs by changing
309 the ellipsis into an identifier with name __VA_ARGS__.
310 This simplifies other handling. */
313 cpp_token *tok = (cpp_token *) token;
315 tok->type = CPP_NAME;
316 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
319 info->paramlen += tok->val.node->length + 1;
321 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
323 "C89 does not permit anon varargs macros");
327 info->flags |= GNU_REST_ARGS;
328 if (CPP_PEDANTIC (pfile))
330 "ISO C does not permit named varargs parameters");
333 info->flags |= VAR_ARGS;
335 if (token->type == CPP_CLOSE_PAREN)
341 info->first = token + 1;
342 if (!pfile->save_parameter_spellings)
348 /* Parses a #define directive. On success, returns zero, and INFO is
349 filled in appropriately. */
351 parse_define (pfile, info)
353 struct macro_info *info;
355 const cpp_token *token;
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 prev_white |= token->flags & PREV_WHITE;
369 if (token->type == CPP_OPEN_PAREN && !prev_white)
371 /* A function-like macro. */
372 info->first_param = token + 1;
373 count_params (pfile, info);
374 if (info->first[-1].type != CPP_CLOSE_PAREN)
379 /* An object-like macro. */
384 if (!prev_white && token->type != CPP_EOF)
385 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
388 /* Count tokens in expansion. We drop paste tokens, and stringize
389 tokens, so don't count them. */
390 info->ntokens = info->len = 0;
391 for (token = info->first; token->type != CPP_EOF; token++)
393 if (token->type == CPP_PASTE)
395 /* Token-paste ##, can appear in both object-like and
396 function-like macros, but not at the ends. Constraint
398 if (token == info->first || token[1].type == CPP_EOF)
400 cpp_error_with_line (pfile, token->line, token->col,
401 "'##' cannot appear at either end of a macro expansion");
406 else if (token->type == CPP_HASH)
408 /* Stringifying #, but a normal character in object-like
409 macros. Must come before a parameter name. Constraint
411 if (info->paramc >= 0)
413 if (token[1].type == CPP_NAME
414 && find_param (info->first_param, token + 1))
416 if (! CPP_OPTION (pfile, lang_asm))
418 cpp_error_with_line (pfile, token->line, token->col,
419 "'#' is not followed by a macro parameter");
424 else if (token->type == CPP_NAME)
426 /* Constraint 6.10.3.5 */
427 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
429 /* It might be worth doing a check here that we aren't a
430 macro argument, since we don't store the text of macro
431 arguments. This would reduce "len" and save space. */
434 if (TOKEN_SPELL (token) == SPELL_STRING)
435 info->len += token->val.str.len;
441 /* Returns non-zero if a macro redefinition is trivial. */
443 check_macro_redefinition (pfile, hp, list2)
446 const cpp_toklist *list2;
448 const cpp_toklist *list1;
450 if (hp->type != T_MACRO)
451 return ! pfile->done_initializing;
453 /* Clear the whitespace and BOL flags of the first tokens. They get
454 altered during macro expansion, but is not significant here. */
455 list1 = hp->value.expansion;
456 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
457 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
459 if (!_cpp_equiv_toklists (list1, list2))
462 if (CPP_OPTION (pfile, pedantic)
464 && (list1->params_len != list2->params_len
465 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
471 /* This is a dummy structure whose only purpose is getting alignment
476 cpp_token first_token;
480 /* Allocate space to hold the token list, its tokens, their text, and
481 the parameter names if needed. Empty expansions are stored as a
482 single placemarker token.
484 These are all allocated in a block together for performance
485 reasons. Therefore, this token list cannot be expanded like a
486 normal token list. Try to do so, and you lose. */
488 alloc_macro (pfile, info)
490 struct macro_info *info;
493 struct toklist_dummy *dummy;
496 /* Empty macros become a single placemarker token. */
497 if (info->ntokens == 0)
500 size = sizeof (struct toklist_dummy);
501 size += (info->ntokens - 1) * sizeof(cpp_token);
502 size += info->len + info->paramlen;
504 dummy = (struct toklist_dummy *) xmalloc (size);
505 list = (cpp_toklist *) dummy;
507 /* Initialize the monster. */
508 list->tokens = &dummy->first_token;
509 list->tokens_used = list->tokens_cap = info->ntokens;
511 list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
512 list->name_used = list->name_cap = info->len + info->paramlen;
515 list->line = pfile->token_list.line;
516 list->file = pfile->token_list.file;
517 list->params_len = info->paramlen;
518 list->paramc = info->paramc;
519 list->flags = info->flags;
524 /* Copy the tokens of the expansion, beginning with info->first until
525 CPP_EOF. INFO contains information about the macro.
527 Change the type of macro arguments in the expansion from CPP_NAME
528 to CPP_MACRO_ARG. Remove #'s that represent stringification,
529 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
530 flagging the token on its immediate left PASTE_LEFT. Returns the
531 token list for the macro expansion. */
532 static const cpp_toklist *
533 save_expansion (pfile, info)
535 struct macro_info *info;
537 const cpp_token *token;
542 list = alloc_macro (pfile, info);
545 /* Store the null-terminated parameter spellings of a macro, to
546 provide pedantic warnings to satisfy 6.10.3.2, or for use when
547 dumping macro definitions. They must go first. */
548 if (list->params_len)
549 for (token = info->first_param; token < info->first; token++)
550 if (token->type == CPP_NAME)
553 memcpy (buf, token->val.node->name, token->val.node->length + 1);
554 buf += token->val.node->length + 1;
558 for (token = info->first; token->type != CPP_EOF; token++)
560 unsigned int param_no;
565 if (list->paramc == -1)
568 /* Check if the name is a macro parameter. */
569 param_no = find_param (info->first_param, token);
572 dest->val.aux = param_no - 1;
574 dest->type = CPP_MACRO_ARG;
575 if (token[-1].type == CPP_HASH)
576 dest->flags = token[-1].flags | STRINGIFY_ARG;
578 dest->flags = token->flags; /* Particularly PREV_WHITE. */
583 dest[-1].flags |= PASTE_LEFT;
587 /* Stringifying #. Constraint 6.10.3.2.1 */
588 if (list->paramc >= 0 && token[1].type == CPP_NAME
589 && find_param (info->first_param, token + 1))
597 /* Copy the token. */
599 if (TOKEN_SPELL (token) == SPELL_STRING)
601 memcpy (buf, token->val.str.text, token->val.str.len);
602 dest->val.str.text = buf;
603 buf += dest->val.str.len;
608 /* Empty macros become a single placemarker token. */
609 if (dest == list->tokens)
611 dest->type = CPP_PLACEMARKER;
618 /* Parse a macro and save its expansion. Returns non-zero on success. */
620 _cpp_create_definition (pfile, hp)
624 struct macro_info info;
625 const cpp_toklist *list;
627 if (parse_define (pfile, &info))
629 list = save_expansion (pfile, &info);
631 /* Check for a redefinition. Redefinition of a macro is allowed if
632 and only if the old and new definitions are the same.
633 (6.10.3 paragraph 2). */
635 if (hp->type != T_VOID)
637 if (!check_macro_redefinition (pfile, hp, list))
639 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
640 if (pfile->done_initializing && hp->type == T_MACRO)
641 cpp_pedwarn_with_file_and_line (pfile,
642 hp->value.expansion->file,
643 hp->value.expansion->line, 1,
644 "this is the location of the previous definition");
646 _cpp_free_definition (hp);
649 /* Enter definition in hash table. */
651 hp->value.expansion = list;
656 /* Dump the definition of macro MACRO on stdout. The format is suitable
657 to be read back in again. */
660 _cpp_dump_definition (pfile, hp)
664 CPP_RESERVE (pfile, hp->length + sizeof "#define ");
665 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
666 CPP_PUTS_Q (pfile, hp->name, hp->length);
668 if (hp->type == T_MACRO)
670 if (hp->value.expansion->paramc >= 0)
671 dump_funlike_macro (pfile, hp);
674 const cpp_toklist *list = hp->value.expansion;
675 list->tokens[0].flags &= ~BOL;
676 list->tokens[0].flags |= PREV_WHITE;
677 _cpp_dump_list (pfile, list, list->tokens, 1);
681 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
683 if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
684 CPP_PUTC (pfile, '\n');
688 dump_funlike_macro (pfile, node)
693 const cpp_toklist * list = node->value.expansion;
696 param = list->namebuf;
697 CPP_PUTC_Q (pfile, '(');
698 for (i = 0; i++ < list->paramc;)
702 len = ustrlen (param);
703 CPP_PUTS (pfile, param, len);
704 if (i < list->paramc)
705 CPP_PUTS(pfile, ", ", 2);
706 else if (list->flags & VAR_ARGS)
708 if (!ustrcmp (param, U"__VA_ARGS__"))
709 pfile->limit -= sizeof (U"__VA_ARGS__") - 1;
710 CPP_PUTS_Q (pfile, "...", 3);
714 CPP_PUTC (pfile, ')');
715 list->tokens[0].flags &= ~BOL;
716 list->tokens[0].flags |= PREV_WHITE;
717 _cpp_dump_list (pfile, list, list->tokens, 1);
720 /* Dump out the hash table. */
722 dump_hash_helper (h, p)
726 cpp_hashnode *hp = (cpp_hashnode *)*h;
727 cpp_reader *pfile = (cpp_reader *)p;
729 if (hp->type == T_MACRO)
730 _cpp_dump_definition (pfile, hp);
735 _cpp_dump_macro_hash (pfile)
738 htab_traverse (pfile->hashtab, dump_hash_helper, pfile);