OSDN Git Service

* config/i386/i386.h: Remove an unnecessary #undef.
[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 #include "opts.h"
48 #include "options.h"
49
50 extern int yyparse (void);
51
52 /* Linked list of symbols - all must be unique in treelang.  */
53
54 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
55
56 /* Language for usage for messages.  */
57
58 const char *const language_string = "TREELANG - sample front end for GCC ";
59
60 /* Local prototypes.  */
61
62 void version (void);
63
64 /* Global variables.  */
65
66 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
67
68 /* 
69    Options. 
70 */
71
72 /* Trace the parser.  */
73 unsigned int option_parser_trace = 0;
74
75 /* Trace the lexical analysis.  */
76
77 unsigned int option_lexer_trace = 0;
78
79 /* Warning levels.  */
80
81 /* Local variables.  */
82
83 /* This is 1 if we have output the version string.  */
84
85 static int version_done = 0;
86
87 /* Variable nesting level.  */
88
89 static unsigned int work_nesting_level = 0;
90
91 /* Prepare to handle switches.  */
92 unsigned int
93 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
94                        const char **argv ATTRIBUTE_UNUSED)
95 {
96   return CL_Treelang;
97 }
98
99 /* Process a switch - called by opts.c.  */
100 int
101 treelang_handle_option (size_t scode, const char *arg ATTRIBUTE_UNUSED,
102                         int value)
103 {
104   enum opt_code code = (enum opt_code) scode;
105
106   switch (code)
107     {
108     default:
109       abort();
110
111     case OPT_v:
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       break;
120
121     case OPT_y:
122       option_lexer_trace = 1;
123       option_parser_trace = 1;
124       break;
125
126     case OPT_fparser_trace:
127       option_parser_trace = value;
128       break;
129
130     case OPT_flexer_trace:
131       option_lexer_trace = value;
132       break;
133     }
134
135   return 1;
136 }
137
138 /* Language dependent parser setup.  */
139
140 bool
141 treelang_init (void)
142 {
143   input_filename = main_input_filename;
144   input_line = 0;
145
146   /* Init decls etc.  */
147
148   treelang_init_decl_processing ();
149
150   /* This error will not happen from GCC as it will always create a
151      fake input file.  */
152   if (!input_filename || input_filename[0] == ' ' || !input_filename[0]) 
153     {
154       if (!version_done)
155         {
156           fprintf (stderr, "No input file specified, try --help for help\n");
157           exit (1);
158         }
159
160       return false;
161     }
162
163   yyin = fopen (input_filename, "r");
164   if (!yyin)
165     {
166       fprintf (stderr, "Unable to open input file %s\n", input_filename);
167       exit (1);
168     }
169
170   return true;
171 }
172
173 /* Language dependent wrapup.  */
174
175 void 
176 treelang_finish (void)
177 {
178   fclose (yyin);
179 }
180
181 /* Parse a file.  Debug flag doesn't seem to work. */
182
183 void
184 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
185 {
186   treelang_debug ();
187   yyparse ();
188 }
189
190 /* Allocate SIZE bytes and clear them.  Not to be used for strings
191    which must go in stringpool.  */
192
193 void *
194 my_malloc (size_t size)
195 {
196   void *mem;
197   mem = ggc_alloc (size);
198   if (!mem)
199     {
200       fprintf (stderr, "\nOut of memory\n");
201       abort ();
202     }
203   memset (mem, 0, size);
204   return mem;
205 }
206
207 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
208    return the symbol table entry from the symbol table if found there,
209    else 0.  */
210
211 struct prod_token_parm_item*
212 lookup_tree_name (struct prod_token_parm_item *prod)
213 {
214   struct prod_token_parm_item *this;
215   struct prod_token_parm_item *this_tok;
216   struct prod_token_parm_item *tok;
217
218   sanity_check (prod);
219   
220   tok = SYMBOL_TABLE_NAME (prod);
221   sanity_check (tok);
222   
223   for (this = symbol_table; this; this = this->tp.pro.next)
224     {
225       sanity_check (this);
226       this_tok = this->tp.pro.main_token;
227       sanity_check (this_tok);
228       if (tok->tp.tok.length != this_tok->tp.tok.length) 
229         continue;
230       if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars, this_tok->tp.tok.length))
231         continue;
232       if (option_parser_trace)
233         fprintf (stderr, "Found symbol %s (%i:%i) as %i \n",
234                  tok->tp.tok.chars, 
235                  tok->tp.tok.location.line, tok->tp.tok.charno,
236                  NUMERIC_TYPE (this));
237       return this;
238     }
239   if (option_parser_trace)
240     fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n",
241              tok->tp.tok.chars, 
242              tok->tp.tok.location.line, tok->tp.tok.charno, tok->type);
243   return NULL;
244 }
245
246 /* Insert name PROD into the symbol table.  Return 1 if duplicate, 0 if OK.  */
247
248 int
249 insert_tree_name (struct prod_token_parm_item *prod)
250 {
251   struct prod_token_parm_item *tok;
252   tok = SYMBOL_TABLE_NAME (prod);
253   sanity_check (prod);
254   if (lookup_tree_name (prod))
255     {
256       fprintf (stderr, "%s:%i:%i duplicate name %s\n",
257                tok->tp.tok.location.file, tok->tp.tok.location.line, 
258                tok->tp.tok.charno, tok->tp.tok.chars);
259       errorcount++;
260       return 1;
261     }
262   prod->tp.pro.next = symbol_table;
263   NESTING_LEVEL (prod) = work_nesting_level;
264   symbol_table = prod;
265   return 0;
266 }
267
268 /* Create a struct productions of type TYPE, main token MAIN_TOK.  */
269
270 struct prod_token_parm_item *
271 make_production (int type, struct prod_token_parm_item *main_tok)
272 {
273   struct prod_token_parm_item *prod;
274   prod = my_malloc (sizeof (struct prod_token_parm_item));
275   prod->category = production_category;
276   prod->type = type;
277   prod->tp.pro.main_token = main_tok;
278   return prod;
279
280
281 /* Abort if ITEM is not a valid structure, based on 'category'.  */
282
283 void
284 sanity_check (struct prod_token_parm_item *item)
285 {
286   switch (item->category)
287     {
288     case   token_category:
289     case production_category:
290     case parameter_category:
291       break;
292       
293     default:
294       abort ();
295     }
296 }  
297
298 /* New garbage collection regime see gty.texi.  */
299 #include "gt-treelang-tree1.h"
300 /*#include "gt-treelang-treelang.h"*/
301 #include "gtype-treelang.h"