OSDN Git Service

PR middle-end/20491
[pf3gnuchains/gcc-fork.git] / gcc / treelang / tree1.c
1 /* TREELANG Compiler almost main (tree1)
2    Called by GCC's toplev.c
3
4    Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003, 2004
5    Free Software Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, 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    ---------------------------------------------------------------------------
27
28    Written by Tim Josling 1999, 2000, 2001, based in part on other
29    parts of the GCC compiler.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "version.h"
38
39 #include "ggc.h"
40 #include "tree.h"
41 #include "cgraph.h"
42 #include "diagnostic.h"
43
44 #include "treelang.h"
45 #include "treetree.h"
46 #include "opts.h"
47 #include "options.h"
48
49 extern int yyparse (void);
50
51 /* Linked list of symbols - all must be unique in treelang.  */
52
53 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
54
55 /* Language for usage for messages.  */
56
57 const char *const language_string = "TREELANG - sample front end for GCC ";
58
59 /* Local prototypes.  */
60
61 void version (void);
62
63 /* Global variables.  */
64
65 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
66
67 /* 
68    Options. 
69 */
70
71 /* Trace the parser.  */
72 unsigned int option_parser_trace = 0;
73
74 /* Trace the lexical analysis.  */
75
76 unsigned int option_lexer_trace = 0;
77
78 /* Warning levels.  */
79
80 /* Local variables.  */
81
82 /* This is 1 if we have output the version string.  */
83
84 static int version_done = 0;
85
86 /* Variable nesting level.  */
87
88 static unsigned int work_nesting_level = 0;
89
90 /* Prepare to handle switches.  */
91 unsigned int
92 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
93                        const char **argv ATTRIBUTE_UNUSED)
94 {
95   return CL_Treelang;
96 }
97
98 /* Process a switch - called by opts.c.  */
99 int
100 treelang_handle_option (size_t scode, const char *arg ATTRIBUTE_UNUSED,
101                         int value)
102 {
103   enum opt_code code = (enum opt_code) scode;
104
105   switch (code)
106     {
107     case OPT_v:
108       if (!version_done)
109         {
110           fputs (language_string, stdout);
111           fputs (version_string, stdout);
112           fputs ("\n", stdout);
113           version_done = 1;
114         }
115       break;
116
117     case OPT_y:
118       option_lexer_trace = 1;
119       option_parser_trace = 1;
120       break;
121
122     case OPT_fparser_trace:
123       option_parser_trace = value;
124       break;
125
126     case OPT_flexer_trace:
127       option_lexer_trace = value;
128       break;
129
130     default:
131       gcc_unreachable ();
132     }
133
134   return 1;
135 }
136
137 /* Language dependent parser setup.  */
138
139 bool
140 treelang_init (void)
141 {
142 #ifndef USE_MAPPED_LOCATION
143   input_filename = main_input_filename;
144 #else
145   linemap_add (&line_table, LC_ENTER, false, main_input_filename, 1);
146 #endif
147
148   /* This error will not happen from GCC as it will always create a
149      fake input file.  */
150   if (!input_filename || input_filename[0] == ' ' || !input_filename[0]) 
151     {
152       if (!version_done)
153         {
154           fprintf (stderr, "No input file specified, try --help for help\n");
155           exit (1);
156         }
157
158       return false;
159     }
160
161   yyin = fopen (input_filename, "r");
162   if (!yyin)
163     {
164       fprintf (stderr, "Unable to open input file %s\n", input_filename);
165       exit (1);
166     }
167
168 #ifdef USE_MAPPED_LOCATION
169   linemap_add (&line_table, LC_RENAME, false, "<built-in>", 1);
170   linemap_line_start (&line_table, 0, 1);
171 #endif
172
173   /* Init decls, etc.  */
174   treelang_init_decl_processing ();
175
176   return true;
177 }
178
179 /* Language dependent wrapup.  */
180
181 void 
182 treelang_finish (void)
183 {
184   fclose (yyin);
185 }
186
187 /* Parse a file.  Debug flag doesn't seem to work. */
188
189 void
190 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
191 {
192 #ifdef USE_MAPPED_LOCATION
193   source_location s;
194   linemap_add (&line_table, LC_RENAME, false, main_input_filename, 1);
195   s = linemap_line_start (&line_table, 1, 80);
196   input_location = s;
197 #else
198   input_line = 1;
199 #endif
200
201   treelang_debug ();
202   yyparse ();
203   cgraph_finalize_compilation_unit ();
204 #ifdef USE_MAPPED_LOCATION
205   linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
206 #endif
207   cgraph_optimize ();
208 }
209
210 /* Allocate SIZE bytes and clear them.  Not to be used for strings
211    which must go in stringpool.  */
212
213 void *
214 my_malloc (size_t size)
215 {
216   void *mem;
217   mem = ggc_alloc (size);
218   if (!mem)
219     {
220       fprintf (stderr, "\nOut of memory\n");
221       abort ();
222     }
223   memset (mem, 0, size);
224   return mem;
225 }
226
227 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
228    return the symbol table entry from the symbol table if found there,
229    else 0.  */
230
231 struct prod_token_parm_item*
232 lookup_tree_name (struct prod_token_parm_item *prod)
233 {
234   struct prod_token_parm_item *this;
235   struct prod_token_parm_item *this_tok;
236   struct prod_token_parm_item *tok;
237
238   sanity_check (prod);
239   
240   tok = SYMBOL_TABLE_NAME (prod);
241   sanity_check (tok);
242   
243   for (this = symbol_table; this; this = this->tp.pro.next)
244     {
245       sanity_check (this);
246       this_tok = this->tp.pro.main_token;
247       sanity_check (this_tok);
248       if (tok->tp.tok.length != this_tok->tp.tok.length) 
249         continue;
250       if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars,
251                   this_tok->tp.tok.length))
252         continue;
253
254       if (option_parser_trace)
255         fprintf (stderr, "Found symbol %s (%i:%i) as %i \n",
256                  tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
257                  tok->tp.tok.charno, NUMERIC_TYPE (this));
258       return this;
259     }
260
261   if (option_parser_trace)
262     fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n",
263              tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
264              tok->tp.tok.charno, tok->type);
265   return NULL;
266 }
267
268 /* Insert name PROD into the symbol table.  Return 1 if duplicate, 0 if OK.  */
269
270 int
271 insert_tree_name (struct prod_token_parm_item *prod)
272 {
273   struct prod_token_parm_item *tok;
274   tok = SYMBOL_TABLE_NAME (prod);
275   sanity_check (prod);
276   if (lookup_tree_name (prod))
277     {
278       error ("%HDuplicate name %q.*s.", &tok->tp.tok.location,
279              tok->tp.tok.length, tok->tp.tok.chars);
280       return 1;
281     }
282   prod->tp.pro.next = symbol_table;
283   NESTING_LEVEL (prod) = work_nesting_level;
284   symbol_table = prod;
285   return 0;
286 }
287
288 /* Create a struct productions of type TYPE, main token MAIN_TOK.  */
289
290 struct prod_token_parm_item *
291 make_production (int type, struct prod_token_parm_item *main_tok)
292 {
293   struct prod_token_parm_item *prod;
294   prod = my_malloc (sizeof (struct prod_token_parm_item));
295   prod->category = production_category;
296   prod->type = type;
297   prod->tp.pro.main_token = main_tok;
298   return prod;
299
300
301 /* Abort if ITEM is not a valid structure, based on 'category'.  */
302
303 void
304 sanity_check (struct prod_token_parm_item *item)
305 {
306   switch (item->category)
307     {
308     case token_category:
309     case production_category:
310     case parameter_category:
311       break;
312       
313     default:
314       gcc_unreachable ();
315     }
316 }  
317
318 /* New garbage collection regime see gty.texi.  */
319 #include "gt-treelang-tree1.h"
320 /*#include "gt-treelang-treelang.h"*/
321 #include "gtype-treelang.h"