OSDN Git Service

* gcc.dg/compat/scalar-by-value-3_main.c: New file.
[pf3gnuchains/gcc-fork.git] / gcc / treelang / tree1.c
1   /* 
2
3     TREELANG Compiler almost main (tree1)
4     Called by GCC's toplev.c
5
6     Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
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,
21     Boston, MA 02111-1307, USA.
22
23     In other words, you are welcome to use, share and improve this program.
24     You are forbidden to forbid anyone else to use, share and improve
25     what you give them.   Help stamp out software-hoarding!  
26
27     ---------------------------------------------------------------------------
28
29     Written by Tim Josling 1999, 2000, 2001, based in part on other
30     parts of the GCC compiler.
31
32 */
33
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "flags.h"
39 #include "toplev.h"
40
41 #include "ggc.h"
42 #include "tree.h"
43 #include "diagnostic.h"
44
45 #include "treelang.h"
46 #include "treetree.h"
47
48 extern int yyparse (void);
49
50 /* Linked list of symbols - all must be unique in treelang.  */
51
52 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
53
54 /* Language for usage for messages.  */
55
56 const char *const language_string = "TREELANG - sample front end for GCC ";
57
58 /* Local prototypes.  */
59
60 void version (void);
61
62 /* Global variables.  */
63
64 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
65
66 /* 
67    Options. 
68 */
69
70 /* Trace the parser.  */
71 unsigned int option_parser_trace = 0;
72
73 /* Trace the lexical analysis.  */
74
75 unsigned int option_lexer_trace = 0;
76
77 /* Warning levels.  */
78
79 /* Local variables.  */
80
81 /* This is 1 if we have output the version string.  */
82
83 static int version_done = 0;
84
85 /* Variable nesting level.  */
86
87 static unsigned int work_nesting_level = 0;
88
89 /* Process one switch - called by toplev.c.  */
90
91 int
92 treelang_decode_option (num_options_left, first_option_left)
93      int num_options_left ATTRIBUTE_UNUSED; 
94      char** first_option_left;
95 {
96   
97   /*
98     Process options - bear in mind I may get options that are really
99     meant for someone else (eg the main compiler) so I have to be very
100     permissive. 
101     
102   */
103   
104   if (first_option_left[0][0] != '-')
105     return 0; 
106   
107   switch (first_option_left[0][1]) 
108     {
109     case '-':
110       if (!strcmp (first_option_left[0],"--help"))
111         {
112           if (!version_done)
113             {
114               fputs (language_string, stdout);
115               fputs (version_string, stdout);
116               fputs ("\n", stdout);
117               version_done = 1;
118             }
119           fprintf (stdout, "Usage: tree1 [switches] -o output input\n");
120           return 1;
121         }
122       break;
123
124     case 'v':
125       if (!strcmp (first_option_left[0],"-v"))
126         {
127           if (!version_done)
128             {
129               fputs (language_string, stdout);
130               fputs (version_string, stdout);
131               fputs ("\n", stdout);
132               version_done = 1;
133             }
134           return 1;
135         }
136       break;
137
138     case 'y':
139       if (!strcmp (first_option_left[0],"-y"))
140         {
141           option_lexer_trace = 1;
142           option_parser_trace = 1;
143           return 1;
144         }
145       break;
146
147     case 'f':
148       if (!strcmp (first_option_left[0],"-fparser-trace"))
149         {
150           option_parser_trace = 1;
151           return 1;
152         }
153       if (!strcmp (first_option_left[0],"-flexer-trace"))
154         {
155           option_lexer_trace = 1;
156           return 1;
157         }
158       break;
159
160     case 'w':
161       if (!strcmp (first_option_left[0],"-w"))
162         {
163           /* Tolerate this option but ignore it - we always put out
164              all warnings.  */
165           return 1;
166         }
167       break;
168
169     case 'W':
170       if (!strcmp (first_option_left[0],"-Wall"))
171         {
172           return 1;
173         }
174       break;
175
176     default:
177       break;
178     }
179
180   return 0;
181 }
182
183 /* Language dependent parser setup.  */
184
185 bool
186 treelang_init ()
187 {
188   input_filename = main_input_filename;
189   input_line = 0;
190
191   /* Init decls etc.  */
192
193   treelang_init_decl_processing ();
194
195   /* This error will not happen from GCC as it will always create a
196      fake input file.  */
197   if (!input_filename || input_filename[0] == ' ' || !input_filename[0]) 
198     {
199       if (!version_done)
200         {
201           fprintf (stderr, "No input file specified, try --help for help\n");
202           exit (1);
203         }
204
205       return false;
206     }
207
208   yyin = fopen (input_filename, "r");
209   if (!yyin)
210     {
211       fprintf (stderr, "Unable to open input file %s\n", input_filename);
212       exit (1);
213     }
214
215   return true;
216 }
217
218 /* Language dependent wrapup.  */
219
220 void 
221 treelang_finish (void)
222 {
223   fclose (yyin);
224 }
225
226 /* Parse a file.  Debug flag doesn't seem to work. */
227
228 void
229 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
230 {
231   treelang_debug ();
232   yyparse ();
233 }
234
235 /* Allocate SIZE bytes and clear them.  Not to be used for strings
236    which must go in stringpool.  */
237
238 void *
239 my_malloc (size_t size)
240 {
241   void *mem;
242   mem = ggc_alloc (size);
243   if (!mem)
244     {
245       fprintf (stderr, "\nOut of memory\n");
246       abort ();
247     }
248   memset (mem, 0, size);
249   return mem;
250 }
251
252 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
253    return the symbol table entry from the symbol table if found there,
254    else 0.  */
255
256 struct prod_token_parm_item*
257 lookup_tree_name (struct prod_token_parm_item *prod)
258 {
259   struct prod_token_parm_item *this;
260   struct prod_token_parm_item *this_tok;
261   struct prod_token_parm_item *tok;
262
263   sanity_check (prod);
264   
265   tok = SYMBOL_TABLE_NAME (prod);
266   sanity_check (tok);
267   
268   for (this = symbol_table; this; this = this->tp.pro.next)
269     {
270       sanity_check (this);
271       this_tok = this->tp.pro.main_token;
272       sanity_check (this_tok);
273       if (tok->tp.tok.length != this_tok->tp.tok.length) 
274         continue;
275       if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars, this_tok->tp.tok.length))
276         continue;
277       if (option_parser_trace)
278         fprintf (stderr, "Found symbol %s (%i:%i) as %i \n",
279                  tok->tp.tok.chars, 
280                  tok->tp.tok.location.line, tok->tp.tok.charno,
281                  NUMERIC_TYPE (this));
282       return this;
283     }
284   if (option_parser_trace)
285     fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n",
286              tok->tp.tok.chars, 
287              tok->tp.tok.location.line, tok->tp.tok.charno, tok->type);
288   return NULL;
289 }
290
291 /* Insert name PROD into the symbol table.  Return 1 if duplicate, 0 if OK.  */
292
293 int
294 insert_tree_name (struct prod_token_parm_item *prod)
295 {
296   struct prod_token_parm_item *tok;
297   tok = SYMBOL_TABLE_NAME (prod);
298   sanity_check (prod);
299   if (lookup_tree_name (prod))
300     {
301       fprintf (stderr, "%s:%i:%i duplicate name %s\n",
302                tok->tp.tok.location.file, tok->tp.tok.location.line, 
303                tok->tp.tok.charno, tok->tp.tok.chars);
304       errorcount++;
305       return 1;
306     }
307   prod->tp.pro.next = symbol_table;
308   NESTING_LEVEL (prod) = work_nesting_level;
309   symbol_table = prod;
310   return 0;
311 }
312
313 /* Create a struct productions of type TYPE, main token MAIN_TOK.  */
314
315 struct prod_token_parm_item *
316 make_production (int type, struct prod_token_parm_item *main_tok)
317 {
318   struct prod_token_parm_item *prod;
319   prod = my_malloc (sizeof (struct prod_token_parm_item));
320   prod->category = production_category;
321   prod->type = type;
322   prod->tp.pro.main_token = main_tok;
323   return prod;
324
325
326 /* Abort if ITEM is not a valid structure, based on 'category'.  */
327
328 void
329 sanity_check (struct prod_token_parm_item *item)
330 {
331   switch (item->category)
332     {
333     case   token_category:
334     case production_category:
335     case parameter_category:
336       break;
337       
338     default:
339       abort ();
340     }
341 }  
342
343 /* New garbage collection regime see gty.texi.  */
344 #include "gt-treelang-tree1.h"
345 /*#include "gt-treelang-treelang.h"*/
346 #include "gtype-treelang.h"