]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/treelang/tree1.c
patch_tester.sh (usage): Watermark is not lexicographic.
[thirdparty/gcc.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 2007, 2008 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any 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; see the file COPYING3. If not, see
19 <http://www.gnu.org/licenses/>.
20
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24
25 ---------------------------------------------------------------------------
26
27 Written by Tim Josling 1999, 2000, 2001, based in part on other
28 parts of the GCC compiler. */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "version.h"
37
38 #include "ggc.h"
39 #include "tree.h"
40 #include "cgraph.h"
41 #include "diagnostic.h"
42
43 #include "treelang.h"
44 #include "treetree.h"
45 #include "opts.h"
46 #include "options.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 /* Prepare to handle switches. */
90 unsigned int
91 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
92 const char **argv ATTRIBUTE_UNUSED)
93 {
94 return CL_Treelang;
95 }
96
97 /* Process a switch - called by opts.c. */
98 int
99 treelang_handle_option (size_t scode, const char *arg ATTRIBUTE_UNUSED,
100 int value)
101 {
102 enum opt_code code = (enum opt_code) scode;
103
104 switch (code)
105 {
106 case OPT_v:
107 if (!version_done)
108 {
109 fputs (language_string, stdout);
110 fputs (pkgversion_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 linemap_add (line_table, LC_ENTER, false, main_input_filename, 1);
143
144 /* This error will not happen from GCC as it will always create a
145 fake input file. */
146 if (!input_filename || input_filename[0] == ' ' || !input_filename[0])
147 {
148 if (!version_done)
149 {
150 fprintf (stderr, "No input file specified, try --help for help\n");
151 exit (1);
152 }
153
154 return false;
155 }
156
157 yyin = fopen (input_filename, "r");
158 if (!yyin)
159 {
160 fprintf (stderr, "Unable to open input file %s\n", input_filename);
161 exit (1);
162 }
163
164 linemap_add (line_table, LC_RENAME, false, "<built-in>", 1);
165 linemap_line_start (line_table, 0, 1);
166
167 /* Init decls, etc. */
168 treelang_init_decl_processing ();
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 source_location s;
187 linemap_add (line_table, LC_RENAME, false, main_input_filename, 1);
188 s = linemap_line_start (line_table, 1, 80);
189 input_location = s;
190
191 treelang_debug ();
192 yyparse ();
193 cgraph_finalize_compilation_unit ();
194 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
195 cgraph_optimize ();
196 }
197
198 /* Allocate SIZE bytes and clear them. Not to be used for strings
199 which must go in stringpool. */
200
201 void *
202 my_malloc (size_t size)
203 {
204 void *mem;
205 mem = ggc_alloc (size);
206 if (!mem)
207 {
208 fprintf (stderr, "\nOut of memory\n");
209 abort ();
210 }
211 memset (mem, 0, size);
212 return mem;
213 }
214
215 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
216 return the symbol table entry from the symbol table if found there,
217 else 0. */
218
219 struct prod_token_parm_item*
220 lookup_tree_name (struct prod_token_parm_item *prod)
221 {
222 struct prod_token_parm_item *this;
223 struct prod_token_parm_item *this_tok;
224 struct prod_token_parm_item *tok;
225
226 sanity_check (prod);
227
228 tok = SYMBOL_TABLE_NAME (prod);
229 sanity_check (tok);
230
231 for (this = symbol_table; this; this = this->tp.pro.next)
232 {
233 sanity_check (this);
234 this_tok = this->tp.pro.main_token;
235 sanity_check (this_tok);
236 if (tok->tp.tok.length != this_tok->tp.tok.length)
237 continue;
238 if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars,
239 this_tok->tp.tok.length))
240 continue;
241
242 if (option_parser_trace)
243 fprintf (stderr, "Found symbol %s (%i:%i) as %i \n",
244 tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
245 tok->tp.tok.charno, NUMERIC_TYPE (this));
246 return this;
247 }
248
249 if (option_parser_trace)
250 fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n",
251 tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
252 tok->tp.tok.charno, tok->type);
253 return NULL;
254 }
255
256 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
257
258 int
259 insert_tree_name (struct prod_token_parm_item *prod)
260 {
261 struct prod_token_parm_item *tok;
262 tok = SYMBOL_TABLE_NAME (prod);
263 sanity_check (prod);
264 if (lookup_tree_name (prod))
265 {
266 error ("%HDuplicate name %q.*s.", &tok->tp.tok.location,
267 tok->tp.tok.length, tok->tp.tok.chars);
268 return 1;
269 }
270 prod->tp.pro.next = symbol_table;
271 NESTING_LEVEL (prod) = work_nesting_level;
272 symbol_table = prod;
273 return 0;
274 }
275
276 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
277
278 struct prod_token_parm_item *
279 make_production (int type, struct prod_token_parm_item *main_tok)
280 {
281 struct prod_token_parm_item *prod;
282 prod = my_malloc (sizeof (struct prod_token_parm_item));
283 prod->category = production_category;
284 prod->type = type;
285 prod->tp.pro.main_token = main_tok;
286 return prod;
287 }
288
289 /* Abort if ITEM is not a valid structure, based on 'category'. */
290
291 void
292 sanity_check (struct prod_token_parm_item *item)
293 {
294 switch (item->category)
295 {
296 case token_category:
297 case production_category:
298 case parameter_category:
299 break;
300
301 default:
302 gcc_unreachable ();
303 }
304 }
305
306 /* New garbage collection regime see gty.texi. */
307 #include "gt-treelang-tree1.h"
308 /*#include "gt-treelang-treelang.h"*/
309 #include "gtype-treelang.h"