2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / treelang / tree1.c
blobcec2baf717da43ec8058d60024bb259789863662
1 /* TREELANG Compiler almost main (tree1)
2 Called by GCC's toplev.c
4 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003, 2004
5 Free Software Foundation, Inc.
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.
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.
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.
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!
26 ---------------------------------------------------------------------------
28 Written by Tim Josling 1999, 2000, 2001, based in part on other
29 parts of the GCC compiler. */
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"
39 #include "ggc.h"
40 #include "tree.h"
41 #include "cgraph.h"
42 #include "diagnostic.h"
44 #include "treelang.h"
45 #include "treetree.h"
46 #include "opts.h"
47 #include "options.h"
49 extern int yyparse (void);
51 /* Linked list of symbols - all must be unique in treelang. */
53 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
55 /* Language for usage for messages. */
57 const char *const language_string = "TREELANG - sample front end for GCC ";
59 /* Local prototypes. */
61 void version (void);
63 /* Global variables. */
65 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
67 /*
68 Options.
71 /* Trace the parser. */
72 unsigned int option_parser_trace = 0;
74 /* Trace the lexical analysis. */
76 unsigned int option_lexer_trace = 0;
78 /* Warning levels. */
80 /* Local variables. */
82 /* This is 1 if we have output the version string. */
84 static int version_done = 0;
86 /* Variable nesting level. */
88 static unsigned int work_nesting_level = 0;
90 /* Prepare to handle switches. */
91 unsigned int
92 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
93 const char **argv ATTRIBUTE_UNUSED)
95 return CL_Treelang;
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)
103 enum opt_code code = (enum opt_code) scode;
105 switch (code)
107 case OPT_v:
108 if (!version_done)
110 fputs (language_string, stdout);
111 fputs (version_string, stdout);
112 fputs ("\n", stdout);
113 version_done = 1;
115 break;
117 case OPT_y:
118 option_lexer_trace = 1;
119 option_parser_trace = 1;
120 break;
122 case OPT_fparser_trace:
123 option_parser_trace = value;
124 break;
126 case OPT_flexer_trace:
127 option_lexer_trace = value;
128 break;
130 default:
131 gcc_unreachable ();
134 return 1;
137 /* Language dependent parser setup. */
139 bool
140 treelang_init (void)
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
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])
152 if (!version_done)
154 fprintf (stderr, "No input file specified, try --help for help\n");
155 exit (1);
158 return false;
161 yyin = fopen (input_filename, "r");
162 if (!yyin)
164 fprintf (stderr, "Unable to open input file %s\n", input_filename);
165 exit (1);
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
173 /* Init decls, etc. */
174 treelang_init_decl_processing ();
176 return true;
179 /* Language dependent wrapup. */
181 void
182 treelang_finish (void)
184 fclose (yyin);
187 /* Parse a file. Debug flag doesn't seem to work. */
189 void
190 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
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
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 ();
210 /* Allocate SIZE bytes and clear them. Not to be used for strings
211 which must go in stringpool. */
213 void *
214 my_malloc (size_t size)
216 void *mem;
217 mem = ggc_alloc (size);
218 if (!mem)
220 fprintf (stderr, "\nOut of memory\n");
221 abort ();
223 memset (mem, 0, size);
224 return mem;
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. */
231 struct prod_token_parm_item*
232 lookup_tree_name (struct prod_token_parm_item *prod)
234 struct prod_token_parm_item *this;
235 struct prod_token_parm_item *this_tok;
236 struct prod_token_parm_item *tok;
238 sanity_check (prod);
240 tok = SYMBOL_TABLE_NAME (prod);
241 sanity_check (tok);
243 for (this = symbol_table; this; this = this->tp.pro.next)
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;
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;
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;
268 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
271 insert_tree_name (struct prod_token_parm_item *prod)
273 struct prod_token_parm_item *tok;
274 tok = SYMBOL_TABLE_NAME (prod);
275 sanity_check (prod);
276 if (lookup_tree_name (prod))
278 error ("%HDuplicate name %q.*s.", &tok->tp.tok.location,
279 tok->tp.tok.length, tok->tp.tok.chars);
280 return 1;
282 prod->tp.pro.next = symbol_table;
283 NESTING_LEVEL (prod) = work_nesting_level;
284 symbol_table = prod;
285 return 0;
288 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
290 struct prod_token_parm_item *
291 make_production (int type, struct prod_token_parm_item *main_tok)
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;
301 /* Abort if ITEM is not a valid structure, based on 'category'. */
303 void
304 sanity_check (struct prod_token_parm_item *item)
306 switch (item->category)
308 case token_category:
309 case production_category:
310 case parameter_category:
311 break;
313 default:
314 gcc_unreachable ();
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"