OSDN Git Service

* cpphash.c (struct macro_info): Add new members.
[pf3gnuchains/gcc-fork.git] / gcc / cpphash.c
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
7
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.
12
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.
17
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.
21
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!  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "hashtab.h"
31 #include "obstack.h"
32
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
35
36 /* This is the second argument to eq_HASHNODE.  */
37 struct hashdummy
38 {
39   const U_CHAR *name;
40   unsigned int hash;
41   unsigned short length;
42 };
43
44 /* Stores basic information about a macro, before it is allocated.  */
45 struct macro_info
46 {
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.  */
53   unsigned char flags;
54 };
55
56 /* Initial hash table size.  (It can grow if necessary - see hashtab.c.)  */
57 #define HASHSIZE 4096
58
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 *));
65
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 *,
72                                         const cpp_token *));
73 static cpp_toklist * alloc_macro PARAMS ((cpp_reader *, struct macro_info *));
74
75 /* Calculate hash of a string of length LEN.  */
76 unsigned int
77 _cpp_calc_hash (str, len)
78      const U_CHAR *str;
79      size_t len;
80 {
81   size_t n = len;
82   unsigned int r = 0;
83
84   do
85     r = r * 67 + (*str++ - 113);
86   while (--n);
87   return r + len;
88 }
89
90 /* Calculate hash of a cpp_hashnode structure.  */
91 static unsigned int
92 hash_HASHNODE (x)
93      const void *x;
94 {
95   const cpp_hashnode *h = (const cpp_hashnode *)x;
96   return h->hash;
97 }
98
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.  */
105 static int
106 eq_HASHNODE (x, y)
107      const void *x;
108      const void *y;
109 {
110   const cpp_hashnode *a = (const cpp_hashnode *)x;
111   const struct hashdummy *b = (const struct hashdummy *)y;
112
113   return (a->hash == b->hash
114           && a->length == b->length
115           && !memcmp (a->name, b->name, a->length));
116 }
117
118 /* Find the hash node for name "name", of length LEN.  */
119
120 cpp_hashnode *
121 cpp_lookup (pfile, name, len)
122      cpp_reader *pfile;
123      const U_CHAR *name;
124      int len;
125 {
126   struct hashdummy dummy;
127   cpp_hashnode *new, **slot;
128   unsigned int hash;
129   U_CHAR *p;
130
131   dummy.name = name;
132   dummy.length = len;
133   dummy.hash = hash = _cpp_calc_hash (name, len);
134
135   slot = (cpp_hashnode **)
136     htab_find_slot_with_hash (pfile->hashtab, (void *)&dummy, hash, INSERT);
137   if (*slot)
138     return *slot;
139
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);
144
145   new->type = T_VOID;
146   new->length = len;
147   new->hash = hash;
148   new->fe_value = 0;
149   new->value.expansion = NULL;
150
151   memcpy (p, name, len);
152   p[len] = 0;
153
154   *slot = new;
155   return new;
156 }
157
158 /* Set up and tear down internal structures for macro expansion.  */
159 void
160 _cpp_init_macros (pfile)
161      cpp_reader *pfile;
162 {
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);
167 }
168
169 void
170 _cpp_cleanup_macros (pfile)
171      cpp_reader *pfile;
172 {
173   htab_delete (pfile->hashtab);
174   obstack_free (pfile->hash_ob, 0);
175   free (pfile->hash_ob);
176 }
177
178 /* Free the definition of macro H.  */
179
180 void
181 _cpp_free_definition (h)
182      cpp_hashnode *h;
183 {
184   if (h->type == T_MACRO)
185     free ((PTR) h->value.expansion);
186   h->value.expansion = NULL;
187 }
188
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.  */
192 static unsigned int
193 find_param (first, token)
194      const cpp_token *first, *token;
195 {
196   unsigned int param = 0;
197
198   for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
199     if (first->type == CPP_NAME)
200       {
201         param++;
202         if (first->val.node == token->val.node)
203           return param;
204       }
205
206   return 0;
207 }
208
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.  */
212 static int
213 is__va_args__ (pfile, token)
214      cpp_reader *pfile;
215      const cpp_token *token;
216 {
217   if (!CPP_PEDANTIC (pfile)
218       || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
219     return 0;
220
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);
224   return 1;
225 }
226
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.
231
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.  */
235 static void
236 count_params (pfile, info)
237      cpp_reader *pfile;
238      struct macro_info *info;
239 {
240   unsigned int prev_ident = 0;
241   const cpp_token *token;
242
243   info->paramc = 0;
244   info->paramlen = 0;
245   info->flags = 0;
246   info->first = info->first_param; /* Not a ')' indicating success.  */
247
248   for (token = info->first_param;; token++)
249     {
250       switch (token->type)
251         {
252         default:
253           cpp_error_with_line (pfile, token->line, token->col,
254                                "illegal token in macro parameter list");
255           return;
256
257         case CPP_EOF:
258         missing_paren:
259           cpp_error_with_line (pfile, token->line, token->col,
260                                "missing ')' in macro parameter list");
261           return;
262
263         case CPP_COMMENT:
264           continue;             /* Ignore -C comments.  */
265
266         case CPP_NAME:
267           if (prev_ident)
268             {
269               cpp_error_with_line (pfile, token->line, token->col,
270                            "macro parameters must be comma-separated");
271               return;
272             }
273
274           /* Constraint 6.10.3.5  */
275           if (is__va_args__ (pfile, token))
276             return;
277
278           /* Constraint 6.10.3.6 - duplicate parameter names.  */
279           if (find_param (info->first, token))
280             {
281               cpp_error_with_line (pfile, token->line, token->col,
282                                    "duplicate macro parameter \"%s\"",
283                                    token->val.node->name);
284               return;
285             }
286
287           prev_ident = 1;
288           info->paramc++;
289           info->paramlen += token->val.node->length + 1;
290           continue;
291
292         case CPP_CLOSE_PAREN:
293           if (prev_ident || info->paramc == 0)
294             break;
295
296           /* Fall through to pick up the error.  */
297         case CPP_COMMA:
298           if (!prev_ident)
299             {
300               cpp_error_with_line (pfile, token->line, token->col,
301                                    "parameter name expected");
302               return;
303             }
304           prev_ident = 0;
305           continue;
306
307         case CPP_ELLIPSIS:
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. */
311           if (!prev_ident)
312             {
313               cpp_token *tok = (cpp_token *) token;
314
315               tok->type = CPP_NAME;
316               tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
317
318               info->paramc++;
319               info->paramlen += tok->val.node->length + 1;
320
321               if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
322                 cpp_pedwarn (pfile,
323                              "C89 does not permit anon varargs macros");
324             }
325           else
326             {
327               info->flags |= GNU_REST_ARGS;
328               if (CPP_PEDANTIC (pfile))
329                 cpp_pedwarn (pfile,
330                              "ISO C does not permit named varargs parameters");
331             }
332
333           info->flags |= VAR_ARGS;
334           token++;
335           if (token->type == CPP_CLOSE_PAREN)
336             break;
337           goto missing_paren;
338         }
339
340       /* Success.  */
341       info->first = token + 1;
342       if (!pfile->save_parameter_spellings)
343         info->paramlen = 0;
344       return;
345     }
346 }
347
348 /* Parses a #define directive.  On success, returns zero, and INFO is
349    filled in appropriately.  */
350 static int
351 parse_define (pfile, info)
352      cpp_reader *pfile;
353      struct macro_info *info;
354 {
355   const cpp_token *token;
356   int prev_white = 0;
357
358   /* The first token after the macro's name.  */
359   token = _cpp_get_token (pfile);
360
361   /* Constraint 6.10.3.5  */
362   if (is__va_args__ (pfile, token - 1))
363     return 1;
364
365   while (token->type == CPP_COMMENT)
366     token++, prev_white = 1;
367   prev_white |= token->flags & PREV_WHITE;
368
369   if (token->type == CPP_OPEN_PAREN && !prev_white)
370     {
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)
375         return 1;
376     }
377   else
378     {
379       /* An object-like macro.  */
380       info->paramc = -1;
381       info->paramlen = 0;
382       info->flags = 0;
383       info->first = token;
384       if (!prev_white && token->type != CPP_EOF)
385         cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
386     }
387
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++)
392     {
393       if (token->type == CPP_PASTE)
394         {
395           /* Token-paste ##, can appear in both object-like and
396              function-like macros, but not at the ends.  Constraint
397              6.10.3.3.1 */
398           if (token == info->first || token[1].type == CPP_EOF)
399             {
400               cpp_error_with_line (pfile, token->line, token->col,
401                 "'##' cannot appear at either end of a macro expansion");
402               return 1;
403             }
404           continue;
405         }
406       else if (token->type == CPP_HASH)
407         {
408           /* Stringifying #, but a normal character in object-like
409              macros.  Must come before a parameter name.  Constraint
410              6.10.3.2.1.  */
411           if (info->paramc >= 0)
412             {
413               if (token[1].type == CPP_NAME
414                   && find_param (info->first_param, token + 1))
415                 continue;
416               if (! CPP_OPTION (pfile, lang_asm))
417                 {
418                   cpp_error_with_line (pfile, token->line, token->col,
419                                "'#' is not followed by a macro parameter");
420                   return 1;
421                 }
422             }
423         }
424       else if (token->type == CPP_NAME)
425         {
426           /* Constraint 6.10.3.5  */
427           if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
428             return 1;
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.  */
432         }
433       info->ntokens++;
434       if (TOKEN_SPELL (token) == SPELL_STRING)
435         info->len += token->val.str.len;
436     }
437   
438   return 0;
439 }
440
441 /* Returns non-zero if a macro redefinition is trivial.  */
442 static int
443 check_macro_redefinition (pfile, hp, list2)
444      cpp_reader *pfile;
445      cpp_hashnode *hp;
446      const cpp_toklist *list2;
447 {
448   const cpp_toklist *list1;
449
450   if (hp->type != T_MACRO)
451     return ! pfile->done_initializing;
452
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);
458
459   if (!_cpp_equiv_toklists (list1, list2))
460     return 0;
461
462   if (CPP_OPTION (pfile, pedantic)
463       && list1->paramc > 0
464       && (list1->params_len != list2->params_len
465           || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
466     return 0;
467
468   return 1;
469 }
470
471 /* This is a dummy structure whose only purpose is getting alignment
472    correct.  */
473 struct toklist_dummy
474 {
475   cpp_toklist list;
476   cpp_token first_token;
477 };
478
479
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.
483
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.  */
487 static cpp_toklist *
488 alloc_macro (pfile, info)
489      cpp_reader *pfile;
490      struct macro_info *info;
491 {
492   unsigned int size;
493   struct toklist_dummy *dummy;
494   cpp_toklist *list;
495
496   /* Empty macros become a single placemarker token.  */
497   if (info->ntokens == 0)
498     info->ntokens = 1;
499
500   size = sizeof (struct toklist_dummy);
501   size += (info->ntokens - 1) * sizeof(cpp_token);
502   size += info->len + info->paramlen;
503
504   dummy = (struct toklist_dummy *) xmalloc (size);
505   list = (cpp_toklist *) dummy;
506   
507   /* Initialize the monster.  */
508   list->tokens = &dummy->first_token;
509   list->tokens_used = list->tokens_cap = info->ntokens;
510
511   list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
512   list->name_used = list->name_cap = info->len + info->paramlen;
513
514   list->directive = 0;
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;
520
521   return list;
522 }
523
524 /* Copy the tokens of the expansion, beginning with info->first until
525    CPP_EOF.  INFO contains information about the macro.
526
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)
534      cpp_reader *pfile;
535      struct macro_info *info;
536 {
537   const cpp_token *token;
538   cpp_toklist *list;
539   cpp_token *dest;
540   unsigned char *buf;
541       
542   list = alloc_macro (pfile, info);
543   buf = list->namebuf;
544
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)
551         {
552           /* Copy null too.  */
553           memcpy (buf, token->val.node->name, token->val.node->length + 1);
554           buf += token->val.node->length + 1;
555         }
556
557   dest = list->tokens;
558   for (token = info->first; token->type != CPP_EOF; token++)
559     {
560       unsigned int param_no;
561
562       switch (token->type)
563         {
564         case CPP_NAME:
565           if (list->paramc == -1)
566             break;
567
568           /* Check if the name is a macro parameter.  */
569           param_no = find_param (info->first_param, token);
570           if (param_no == 0)
571             break;
572           dest->val.aux = param_no - 1;
573
574           dest->type = CPP_MACRO_ARG;
575           if (token[-1].type == CPP_HASH)
576             dest->flags = token[-1].flags | STRINGIFY_ARG;
577           else
578             dest->flags = token->flags;  /* Particularly PREV_WHITE.  */
579           dest++;
580           continue;
581
582         case CPP_PASTE:
583           dest[-1].flags |= PASTE_LEFT;
584           continue;
585
586         case CPP_HASH:
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))
590             continue;
591           break;
592
593         default:
594           break;
595         }
596
597       /* Copy the token.  */
598       *dest = *token;
599       if (TOKEN_SPELL (token) == SPELL_STRING)
600         {
601           memcpy (buf, token->val.str.text, token->val.str.len);
602           dest->val.str.text = buf;
603           buf += dest->val.str.len;
604         }
605       dest++;
606     }
607
608   /* Empty macros become a single placemarker token.  */
609   if (dest == list->tokens)
610     {
611       dest->type = CPP_PLACEMARKER;
612       dest->flags = 0;
613     }
614
615   return list;
616 }
617
618 /* Parse a macro and save its expansion.  Returns non-zero on success.  */
619 int
620 _cpp_create_definition (pfile, hp)
621      cpp_reader *pfile;
622      cpp_hashnode *hp;
623 {
624   struct macro_info info;
625   const cpp_toklist *list;
626
627   if (parse_define (pfile, &info))
628     return 0;
629   list = save_expansion (pfile, &info);
630
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). */
634
635   if (hp->type != T_VOID)
636     {
637       if (!check_macro_redefinition (pfile, hp, list))
638         {
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");
645         }
646       _cpp_free_definition (hp);
647     }
648
649   /* Enter definition in hash table.  */
650   hp->type = T_MACRO;
651   hp->value.expansion = list;
652
653   return 1;
654 }
655
656 /* Dump the definition of macro MACRO on stdout.  The format is suitable
657    to be read back in again. */
658
659 void
660 _cpp_dump_definition (pfile, hp)
661      cpp_reader *pfile;
662      cpp_hashnode *hp;
663 {
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);
667
668   if (hp->type == T_MACRO)
669     {
670       if (hp->value.expansion->paramc >= 0)
671         dump_funlike_macro (pfile, hp);
672       else
673         {
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);
678         }
679     }
680   else
681     cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
682
683   if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
684     CPP_PUTC (pfile, '\n');
685 }
686
687 static void
688 dump_funlike_macro (pfile, node)
689      cpp_reader *pfile;
690      cpp_hashnode *node;
691 {
692   int i = 0;
693   const cpp_toklist * list = node->value.expansion;
694   const U_CHAR *param;
695
696   param = list->namebuf;
697   CPP_PUTC_Q (pfile, '(');
698   for (i = 0; i++ < list->paramc;)
699     {
700       unsigned int len;
701
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)
707         {
708           if (!ustrcmp (param, U"__VA_ARGS__"))
709             pfile->limit -= sizeof (U"__VA_ARGS__") - 1;
710           CPP_PUTS_Q (pfile, "...", 3);
711         }
712       param += len + 1;
713     }
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);
718 }
719
720 /* Dump out the hash table.  */
721 static int
722 dump_hash_helper (h, p)
723      void **h;
724      void *p;
725 {
726   cpp_hashnode *hp = (cpp_hashnode *)*h;
727   cpp_reader *pfile = (cpp_reader *)p;
728
729   if (hp->type == T_MACRO)
730     _cpp_dump_definition (pfile, hp);
731   return 1;
732 }
733
734 void
735 _cpp_dump_macro_hash (pfile)
736      cpp_reader *pfile;
737 {
738   htab_traverse (pfile->hashtab, dump_hash_helper, pfile);
739 }