OSDN Git Service

* patch_tester.sh (usage): Watermark is not lexicographic.
[pf3gnuchains/gcc-fork.git] / gcc / treelang / treetree.c
1 /* TREELANG Compiler interface to GCC's middle end (treetree.c)
2    Called by the parser.
3
4    If you want a working example of how to write a front end to GCC,
5    you are in the right place.
6
7    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
8    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
9    Free Software Foundation, Inc.
10
11    This code is based on toy.c written by Richard Kenner.
12
13    It was later modified by Jonathan Bartlett whose changes have all
14    been removed (by Tim Josling).
15
16    Various bits and pieces were cloned from the GCC main tree, as
17    GCC evolved, for COBOLForGCC, by Tim Josling.
18
19    It was adapted to TREELANG by Tim Josling 2001.
20
21    Updated to function-at-a-time by James A. Morrison, 2004.
22
23    -----------------------------------------------------------------------
24
25    This program is free software; you can redistribute it and/or modify it
26    under the terms of the GNU General Public License as published by the
27    Free Software Foundation; either version 3, or (at your option) any
28    later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33    GNU General Public License for more details.
34
35    You should have received a copy of the GNU General Public License
36    along with this program; see the file COPYING3.  If not see
37    <http://www.gnu.org/licenses/>.
38
39    In other words, you are welcome to use, share and improve this program.
40    You are forbidden to forbid anyone else to use, share and improve
41    what you give them.   Help stamp out software-hoarding!
42
43    -----------------------------------------------------------------------  */
44
45 /* Assumption: garbage collection is never called implicitly.  It will
46    not be called 'at any time' when short of memory.  It will only be
47    called explicitly at the end of each function.  This removes the
48    need for a *lot* of bother to ensure everything is in the mark trees
49    at all times.  */
50
51 /* Note, it is OK to use GCC extensions such as long long in a compiler front
52    end.  This is because the GCC front ends are built using GCC.   */
53
54 /* GCC headers.  */
55
56 #include "config.h"
57 #include "system.h"
58 #include "coretypes.h"
59 #include "tm.h"
60 #include "tree.h"
61 #include "tree-dump.h"
62 #include "tree-iterator.h"
63 #include "tree-gimple.h"
64 #include "function.h"
65 #include "flags.h"
66 #include "output.h"
67 #include "ggc.h"
68 #include "toplev.h"
69 #include "varray.h"
70 #include "langhooks-def.h"
71 #include "langhooks.h"
72 #include "target.h"
73
74 #include "cgraph.h"
75
76 #include "treelang.h"
77 #include "treetree.h"
78 #include "opts.h"
79
80 extern int option_main;
81 extern char **file_names;
82
83 /* Types expected by gcc's garbage collector.
84    These types exist to allow language front-ends to
85    add extra information in gcc's parse tree data structure.
86    But the treelang front end doesn't use them -- it has
87    its own parse tree data structure.
88    We define them here only to satisfy gcc's garbage collector.  */
89
90 /* Language-specific identifier information.  */
91
92 struct lang_identifier GTY(())
93 {
94   struct tree_identifier common;
95 };
96
97 /* Language-specific tree node information.  */
98
99 union lang_tree_node 
100   GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
101 {
102   union tree_node GTY ((tag ("0"), 
103                         desc ("tree_node_structure (&%h)"))) 
104     generic;
105   struct lang_identifier GTY ((tag ("1"))) identifier;
106 };
107
108 /* Language-specific type information.  */
109
110 struct lang_type GTY(())
111 {
112   char junk; /* dummy field to ensure struct is not empty */
113 };
114
115 /* Language-specific declaration information.  */
116
117 struct lang_decl GTY(())
118 {
119   char junk; /* dummy field to ensure struct is not empty */
120 };
121
122 struct language_function GTY(())
123 {
124   char junk; /* dummy field to ensure struct is not empty */
125 };
126
127 static bool tree_mark_addressable (tree exp);
128 static tree tree_lang_type_for_size (unsigned precision, int unsignedp);
129 static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp);
130
131 /* Functions to keep track of the current scope.  */
132 static void pushlevel (int ignore);
133 static tree poplevel (int keep, int reverse, int functionbody);
134 static tree pushdecl (tree decl);
135 static tree* getstmtlist (void);
136
137 /* Langhooks.  */
138 extern const struct attribute_spec treelang_attribute_table[];
139 static tree getdecls (void);
140 static int global_bindings_p (void);
141 static void insert_block (tree);
142
143 static void tree_push_type_decl (tree id, tree type_node);
144
145 /* The front end language hooks (addresses of code for this front
146    end).  These are not really very language-dependent, i.e.
147    treelang, C, Mercury, etc. can all use almost the same definitions.  */
148
149 #undef LANG_HOOKS_MARK_ADDRESSABLE
150 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
151 #undef LANG_HOOKS_TYPE_FOR_MODE
152 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
153 #undef LANG_HOOKS_TYPE_FOR_SIZE
154 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
155 #undef LANG_HOOKS_PARSE_FILE
156 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
157 #undef LANG_HOOKS_ATTRIBUTE_TABLE
158 #define LANG_HOOKS_ATTRIBUTE_TABLE treelang_attribute_table
159
160 /* #undef LANG_HOOKS_TYPES_COMPATIBLE_P
161 #define LANG_HOOKS_TYPES_COMPATIBLE_P hook_bool_tree_tree_true
162 */
163 /* Hook routines and data unique to treelang.  */
164
165 #undef LANG_HOOKS_INIT
166 #define LANG_HOOKS_INIT treelang_init
167 #undef LANG_HOOKS_NAME
168 #define LANG_HOOKS_NAME "GNU treelang"
169 #undef LANG_HOOKS_FINISH
170 #define LANG_HOOKS_FINISH               treelang_finish
171 #undef LANG_HOOKS_INIT_OPTIONS
172 #define LANG_HOOKS_INIT_OPTIONS  treelang_init_options
173 #undef LANG_HOOKS_HANDLE_OPTION
174 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
175 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
176
177 /* Tree code type/name/code tables.  */
178
179 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
180
181 const enum tree_code_class tree_code_type[] = {
182 #include "tree.def"
183   tcc_exceptional
184 };
185 #undef DEFTREECODE
186
187 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
188
189 const unsigned char tree_code_length[] = {
190 #include "tree.def"
191   0
192 };
193 #undef DEFTREECODE
194
195 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
196
197 const char *const tree_code_name[] = {
198 #include "tree.def"
199   "@@dummy"
200 };
201 #undef DEFTREECODE
202
203 /* Number of bits in int and char - accessed by front end.  */
204
205 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
206
207 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
208
209 /* Return the tree stuff for this type TYPE_NUM.  */
210
211 tree
212 tree_code_get_type (int type_num)
213 {
214   switch (type_num)
215     {
216     case SIGNED_CHAR:
217       return signed_char_type_node;
218
219     case UNSIGNED_CHAR:
220       return unsigned_char_type_node;
221
222     case SIGNED_INT:
223       return integer_type_node;
224
225     case UNSIGNED_INT:
226       return unsigned_type_node;
227
228     case VOID_TYPE:
229       return void_type_node;
230
231     default:
232       gcc_unreachable ();
233     }
234 }
235
236 /* Output the code for the start of an if statement.  The test
237    expression is EXP (true if not zero), and the stmt occurred at line
238    LINENO in file FILENAME.  */
239
240 void
241 tree_code_if_start (tree exp, location_t loc)
242 {
243   tree cond_exp, cond;
244   cond_exp = fold_build2 (NE_EXPR, boolean_type_node, exp,
245                           build_int_cst (TREE_TYPE (exp), 0));
246   SET_EXPR_LOCATION (cond_exp, loc);
247   cond = build3 (COND_EXPR, void_type_node, cond_exp, NULL_TREE,
248                  NULL_TREE);
249   SET_EXPR_LOCATION (cond, loc);
250   append_to_statement_list_force (cond, getstmtlist ());
251   pushlevel (0);
252 }
253
254 /* Output the code for the else of an if statement.  The else occurred
255    at line LINENO in file FILENAME.  */
256
257 void
258 tree_code_if_else (location_t loc ATTRIBUTE_UNUSED)
259 {
260   tree stmts = *getstmtlist ();
261   tree block = poplevel (1, 0, 0);
262   if (BLOCK_VARS (block))
263     {
264       tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
265                               stmts, block);
266       stmts = alloc_stmt_list ();
267       append_to_statement_list (bindexpr, &stmts);
268     }
269
270   TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 1) = stmts;
271   pushlevel (0);
272 }
273
274 /* Output the code for the end_if an if statement.  The end_if (final brace)
275    occurred at line LINENO in file FILENAME.  */
276
277 void
278 tree_code_if_end (location_t loc ATTRIBUTE_UNUSED)
279 {
280   tree stmts = *getstmtlist ();
281   tree block = poplevel (1, 0, 0);
282   if (BLOCK_VARS (block))
283     {
284        tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
285                                stmts, block);
286        stmts = alloc_stmt_list ();
287        append_to_statement_list (bindexpr, &stmts);
288     }
289
290   TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 2) = stmts;
291 }
292
293 /* Create a function.  The prototype name is NAME, storage class is
294    STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
295    is PARMS, returns decl for this function.  */
296
297 tree
298 tree_code_create_function_prototype (const unsigned char *chars,
299                                      unsigned int storage_class,
300                                      unsigned int ret_type,
301                                      struct prod_token_parm_item* parms,
302                                      location_t loc)
303 {
304
305   tree id;
306   struct prod_token_parm_item* parm;
307   tree type_list = NULL_TREE;
308   tree type_node;
309   tree fn_type;
310   tree fn_decl;
311   tree parm_list = NULL_TREE;
312
313   /* Build the type.  */
314   id = get_identifier ((const char*)chars);
315   for (parm = parms; parm; parm = parm->tp.par.next)
316     {
317       gcc_assert (parm->category == parameter_category);
318       type_node = tree_code_get_type (parm->type);
319       type_list = tree_cons (NULL_TREE, type_node, type_list);
320     }
321   /* Last parm if void indicates fixed length list (as opposed to
322      printf style va_* list).  */
323   type_list = tree_cons (NULL_TREE, void_type_node, type_list);
324
325   /* The back end needs them in reverse order.  */
326   type_list = nreverse (type_list);
327
328   type_node = tree_code_get_type (ret_type);
329   fn_type = build_function_type (type_node, type_list);
330
331   id = get_identifier ((const char*)chars);
332   fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
333
334   /* Nested functions not supported here.  */
335   DECL_CONTEXT (fn_decl) = NULL_TREE;
336   DECL_SOURCE_LOCATION (fn_decl) = loc;
337
338   TREE_PUBLIC (fn_decl) = 0;
339   DECL_EXTERNAL (fn_decl) = 0;
340   TREE_STATIC (fn_decl) = 0;
341   switch (storage_class)
342     {
343     case STATIC_STORAGE:
344       break;
345
346     case EXTERNAL_DEFINITION_STORAGE:
347       TREE_PUBLIC (fn_decl) = 1;
348       break;
349
350     case EXTERNAL_REFERENCE_STORAGE:
351       DECL_EXTERNAL (fn_decl) = 1;
352       break;
353
354     case AUTOMATIC_STORAGE:
355     default:
356       gcc_unreachable ();
357     }
358
359   /* Make the argument variable decls.  */
360   for (parm = parms; parm; parm = parm->tp.par.next)
361     {
362       tree parm_decl = build_decl (PARM_DECL, get_identifier
363                                    ((const char*) (parm->tp.par.variable_name)),
364                                    tree_code_get_type (parm->type));
365
366       /* Some languages have different nominal and real types.  */
367       DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
368       gcc_assert (DECL_ARG_TYPE (parm_decl));
369       gcc_assert (fn_decl);
370       DECL_CONTEXT (parm_decl) = fn_decl;
371       DECL_SOURCE_LOCATION (parm_decl) = loc;
372       parm_list = chainon (parm_decl, parm_list);
373     }
374
375   /* Back into reverse order as the back end likes them.  */
376   parm_list = nreverse (parm_list);
377
378   DECL_ARGUMENTS (fn_decl) = parm_list;
379
380   /* Save the decls for use when the args are referred to.  */
381   for (parm = parms; parm_list;
382        parm_list = TREE_CHAIN (parm_list),
383         parm = parm->tp.par.next)
384     {
385       gcc_assert (parm); /* Too few.  */
386       *parm->tp.par.where_to_put_var_tree = parm_list;
387     }
388   gcc_assert (!parm); /* Too many.  */
389
390   /* Process declaration of function defined elsewhere.  */
391   rest_of_decl_compilation (fn_decl, 1, 0);
392
393   return fn_decl;
394 }
395
396
397 /* Output code for start of function; the decl of the function is in
398    PREV_SAVED (as created by tree_code_create_function_prototype),
399    the function is at line number LINENO in file FILENAME.  The
400    parameter details are in the lists PARMS. Returns nothing.  */
401
402 void
403 tree_code_create_function_initial (tree prev_saved,
404                                    location_t loc)
405 {
406   tree fn_decl;
407   tree resultdecl;
408
409   fn_decl = prev_saved;
410   gcc_assert (fn_decl);
411
412   /* Output message if not -quiet.  */
413   announce_function (fn_decl);
414
415   /* This has something to do with forcing output also.  */
416   pushdecl (fn_decl);
417
418   /* Set current function for error msgs etc.  */
419   current_function_decl = fn_decl;
420   DECL_INITIAL (fn_decl) = error_mark_node;
421
422   DECL_SOURCE_LOCATION (fn_decl) = loc;
423
424   /* Create a DECL for the functions result.  */
425   resultdecl =
426     build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
427   DECL_CONTEXT (resultdecl) = fn_decl;
428   DECL_ARTIFICIAL (resultdecl) = 1;
429   DECL_IGNORED_P (resultdecl) = 1;
430   DECL_SOURCE_LOCATION (resultdecl) = loc;
431   DECL_RESULT (fn_decl) = resultdecl;
432
433   /* Create a new level at the start of the function.  */
434
435   pushlevel (0);
436
437   TREE_STATIC (fn_decl) = 1;
438 }
439
440 /* Wrapup a function contained in file FILENAME, ending at line LINENO.  */
441 void
442 tree_code_create_function_wrapup (location_t loc)
443 {
444   tree block;
445   tree fn_decl;
446   tree stmts = *getstmtlist ();
447
448   fn_decl = current_function_decl;
449
450   /* Pop the level.  */
451
452   block = poplevel (1, 0, 1);
453
454   /* And attach it to the function.  */
455
456   DECL_SAVED_TREE (fn_decl) = build3 (BIND_EXPR, void_type_node,
457                                       BLOCK_VARS (block),
458                                       stmts, block);
459
460   allocate_struct_function (fn_decl, false);
461   cfun->function_end_locus = loc;
462
463   /* Dump the original tree to a file.  */
464   dump_function (TDI_original, fn_decl);
465
466   /* Convert current function to GIMPLE for the middle end.  */
467   gimplify_function_tree (fn_decl);
468   dump_function (TDI_generic, fn_decl);
469
470   /* We are not inside of any scope now.  */
471   current_function_decl = NULL_TREE;
472   set_cfun (NULL);
473
474   /* Pass the current function off to the middle end.  */
475   (void)cgraph_node (fn_decl);
476   cgraph_finalize_function (fn_decl, false);
477 }
478
479 /* Create a variable.
480
481    The storage class is STORAGE_CLASS (eg LOCAL).
482    The name is CHARS/LENGTH.
483    The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
484    The init tree is INIT.  */
485
486 tree
487 tree_code_create_variable (unsigned int storage_class,
488                            const unsigned char *chars,
489                            unsigned int length,
490                            unsigned int expression_type,
491                            tree init,
492                            location_t loc)
493 {
494   tree var_type;
495   tree var_id;
496   tree var_decl;
497
498   /* 1. Build the type.  */
499   var_type = tree_code_get_type (expression_type);
500
501   /* 2. Build the name.  */
502   gcc_assert (chars[length] == 0); /* Should be null terminated.  */
503
504   var_id = get_identifier ((const char*)chars);
505
506   /* 3. Build the decl and set up init.  */
507   var_decl = build_decl (VAR_DECL, var_id, var_type);
508
509   /* 3a. Initialization.  */
510   if (init)
511     DECL_INITIAL (var_decl) = fold_convert (var_type, init);
512   else
513     DECL_INITIAL (var_decl) = NULL_TREE;
514
515   gcc_assert (TYPE_SIZE (var_type) != 0); /* Did not calculate size.  */
516
517   DECL_CONTEXT (var_decl) = current_function_decl;
518
519   DECL_SOURCE_LOCATION (var_decl) = loc;
520
521   DECL_EXTERNAL (var_decl) = 0;
522   TREE_PUBLIC (var_decl) = 0;
523   TREE_STATIC (var_decl) = 0;
524   /* Set the storage mode and whether only visible in the same file.  */
525   switch (storage_class)
526     {
527     case STATIC_STORAGE:
528       TREE_STATIC (var_decl) = 1;
529       break;
530
531     case AUTOMATIC_STORAGE:
532       break;
533
534     case EXTERNAL_DEFINITION_STORAGE:
535       TREE_PUBLIC (var_decl) = 1;
536       break;
537
538     case EXTERNAL_REFERENCE_STORAGE:
539       DECL_EXTERNAL (var_decl) = 1;
540       break;
541
542     default:
543       gcc_unreachable ();
544     }
545
546   TYPE_NAME (TREE_TYPE (var_decl)) = TYPE_NAME (var_type);
547   return pushdecl (copy_node (var_decl));
548 }
549
550
551 /* Generate code for return statement.  Type is in TYPE, expression
552    is in EXP if present.  */
553
554 void
555 tree_code_generate_return (tree type, tree exp)
556 {
557   tree setret;
558 #ifdef ENABLE_CHECKING
559   tree param;
560
561   for (param = DECL_ARGUMENTS (current_function_decl);
562        param;
563        param = TREE_CHAIN (param))
564     gcc_assert (DECL_CONTEXT (param) == current_function_decl);
565 #endif
566
567   if (exp && TREE_TYPE (TREE_TYPE (current_function_decl)) != void_type_node)
568     {
569       setret = fold_build2 (MODIFY_EXPR, type, 
570                             DECL_RESULT (current_function_decl),
571                             fold_convert (type, exp));
572       TREE_SIDE_EFFECTS (setret) = 1;
573       TREE_USED (setret) = 1;
574       setret = build1 (RETURN_EXPR, void_type_node, setret);
575       /* Use EXPR_LOCUS so we don't lose any information about the file we
576          are compiling.  */
577       SET_EXPR_LOCUS (setret, EXPR_LOCUS (exp));
578     }
579    else
580      setret = build1 (RETURN_EXPR, void_type_node, NULL_TREE);
581
582    append_to_statement_list_force (setret, getstmtlist ());
583 }
584
585
586 /* Output the code for this expression statement CODE.  */
587
588 void
589 tree_code_output_expression_statement (tree code, location_t loc)
590 {
591   /* Output the line number information.  */
592   SET_EXPR_LOCATION (code, loc);
593   TREE_USED (code) = 1;
594   TREE_SIDE_EFFECTS (code) = 1;
595   /* put CODE into the code list.  */
596   append_to_statement_list_force (code, getstmtlist ());
597 }
598
599 /* Return a tree for a constant integer value in the token TOK.  No
600    size checking is done.  */
601
602 tree
603 tree_code_get_integer_value (const unsigned char *chars, unsigned int length)
604 {
605   long long int val = 0;
606   unsigned int ix;
607   unsigned int start = 0;
608   int negative = 1;
609   switch (chars[0])
610     {
611     case (unsigned char)'-':
612       negative = -1;
613       start = 1;
614       break;
615
616     case (unsigned char)'+':
617       start = 1;
618       break;
619
620     default:
621       break;
622     }
623   for (ix = start; ix < length; ix++)
624     val = val * 10 + chars[ix] - (unsigned char)'0';
625   val = val*negative;
626   return build_int_cst_wide (start == 1 ?
627                                 integer_type_node : unsigned_type_node,
628                              val & 0xffffffff, (val >> 32) & 0xffffffff);
629 }
630
631 /* Return the tree for an expression, type EXP_TYPE (see treetree.h)
632    with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe).  */
633 tree
634 tree_code_get_expression (unsigned int exp_type,
635                           tree type, tree op1, tree op2,
636                           tree op3 ATTRIBUTE_UNUSED,
637                           location_t loc)
638 {
639   tree ret1;
640   int operator;
641
642   switch (exp_type)
643     {
644     case EXP_ASSIGN:
645       gcc_assert (op1 && op2);
646       operator = MODIFY_EXPR;
647       ret1 = fold_build2 (operator, void_type_node, op1,
648                           fold_convert (TREE_TYPE (op1), op2));
649
650       break;
651
652     case EXP_PLUS:
653       operator = PLUS_EXPR;
654       goto binary_expression;
655
656     case EXP_MINUS:
657       operator = MINUS_EXPR;
658       goto binary_expression;
659
660     case EXP_EQUALS:
661       operator = EQ_EXPR;
662       goto binary_expression;
663
664     /* Expand a binary expression.  Ensure the operands are the right type.  */
665     binary_expression:
666       gcc_assert (op1 && op2);
667       ret1  =  fold_build2 (operator, type,
668                             fold_convert (type, op1),
669                             fold_convert (type, op2));
670       break;
671
672       /* Reference to a variable.  This is dead easy, just return the
673          decl for the variable.  If the TYPE is different than the
674          variable type, convert it.  However, to keep accurate location
675          information we wrap it in a NOP_EXPR is is easily stripped.  */
676     case EXP_REFERENCE:
677       gcc_assert (op1);
678       TREE_USED (op1) = 1;
679       if (type == TREE_TYPE (op1))
680         ret1 = build1 (NOP_EXPR, type, op1);
681       else
682         ret1 = fold_convert (type, op1);
683       break;
684
685     case EXP_FUNCTION_INVOCATION:
686       gcc_assert (op1);
687       gcc_assert(TREE_TYPE (TREE_TYPE (op1)) == type);
688       TREE_USED (op1) = 1;
689       ret1 = build_function_call_expr(op1, op2);
690       break;
691
692     default:
693       gcc_unreachable ();
694     }
695
696   /* Declarations already have a location and constants can be shared so they
697      shouldn't a location set on them.  */
698   if (! DECL_P (ret1) && ! TREE_CONSTANT (ret1))
699     SET_EXPR_LOCATION (ret1, loc);
700   return ret1;
701 }
702
703 /* Init parameter list and return empty list.  */
704
705 tree
706 tree_code_init_parameters (void)
707 {
708   return NULL_TREE;
709 }
710
711 /* Add a parameter EXP whose expression type is EXP_PROTO to list
712    LIST, returning the new list.  */
713
714 tree
715 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
716 {
717   tree new_exp;
718   new_exp = tree_cons (NULL_TREE,
719                        fold_convert (TREE_TYPE (proto_exp),
720                                      exp), NULL_TREE);
721   if (!list)
722     return new_exp;
723   return chainon (new_exp, list);
724 }
725
726 /* Get a stringpool entry for a string S of length L.  This is needed
727    because the GTY routines don't mark strings, forcing you to put
728    them into stringpool, which is never freed.  */
729
730 const char*
731 get_string (const char *s, size_t l)
732 {
733   tree t;
734   t = get_identifier_with_length (s, l);
735   return IDENTIFIER_POINTER(t);
736 }
737   
738 /* Save typing debug_tree all the time. Dump a tree T pretty and
739    concise.  */
740
741 void dt (tree t);
742
743 void
744 dt (tree t)
745 {
746   debug_tree (t);
747 }
748
749 /* Routines Expected by gcc:  */
750
751 /* These are used to build types for various sizes.  The code below
752    is a simplified version of that of GNAT.  */
753
754 #ifndef MAX_BITS_PER_WORD
755 #define MAX_BITS_PER_WORD  BITS_PER_WORD
756 #endif
757
758 /* This variable keeps a table for types for each precision so that we only 
759    allocate each of them once. Signed and unsigned types are kept separate.  */
760 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
761
762 /* Mark EXP saying that we need to be able to take the
763    address of it; it should not be allocated in a register.
764    Value is 1 if successful.  
765    
766    This implementation was copied from c-decl.c. */
767
768 static bool
769 tree_mark_addressable (tree exp)
770 {
771   register tree x = exp;
772   while (1)
773     switch (TREE_CODE (x))
774       {
775       case COMPONENT_REF:
776       case ADDR_EXPR:
777       case ARRAY_REF:
778       case REALPART_EXPR:
779       case IMAGPART_EXPR:
780         x = TREE_OPERAND (x, 0);
781         break;
782   
783       case CONSTRUCTOR:
784         TREE_ADDRESSABLE (x) = 1;
785         return 1;
786
787       case VAR_DECL:
788       case CONST_DECL:
789       case PARM_DECL:
790       case RESULT_DECL:
791         if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
792             && DECL_NONLOCAL (x))
793           {
794             if (TREE_PUBLIC (x))
795               {
796                 error ("Global register variable %qD used in nested function.",
797                        x);
798                 return 0;
799               }
800             pedwarn ("Register variable %qD used in nested function.", x);
801           }
802         else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
803           {
804             if (TREE_PUBLIC (x))
805               {
806                 error ("Address of global register variable %qD requested.",
807                        x);
808                 return 0;
809               }
810
811             pedwarn ("Address of register variable %qD requested.", x);
812           }
813
814         /* drops in */
815       case FUNCTION_DECL:
816         TREE_ADDRESSABLE (x) = 1;
817
818       default:
819         return 1;
820     }
821 }
822   
823 /* Return an integer type with the number of bits of precision given by  
824    PRECISION.  UNSIGNEDP is nonzero if the type is unsigned; otherwise
825    it is a signed type.  */
826   
827 static tree
828 tree_lang_type_for_size (unsigned precision, int unsignedp)
829 {
830   tree t;
831
832   if (precision <= MAX_BITS_PER_WORD
833       && signed_and_unsigned_types[precision][unsignedp] != 0)
834     return signed_and_unsigned_types[precision][unsignedp];
835
836   if (unsignedp)
837     t = signed_and_unsigned_types[precision][1]
838       = make_unsigned_type (precision);
839   else
840     t = signed_and_unsigned_types[precision][0]
841       = make_signed_type (precision);
842   
843   return t;
844 }
845
846 /* Return a data type that has machine mode MODE.  UNSIGNEDP selects
847    an unsigned type; otherwise a signed type is returned.  */
848
849 static tree
850 tree_lang_type_for_mode (enum machine_mode mode, int unsignedp)
851 {
852   if (SCALAR_INT_MODE_P (mode))
853     return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
854   else
855     return NULL_TREE;
856 }
857
858 \f
859 /* These functions and variables deal with binding contours.  We only
860    need these functions for the list of PARM_DECLs, but we leave the
861    functions more general; these are a simplified version of the
862    functions from GNAT.  */
863
864 /* For each binding contour we allocate a binding_level structure which records
865    the entities defined or declared in that contour. Contours include:
866
867         the global one
868         one for each subprogram definition
869         one for each compound statement (declare block)
870
871    Binding contours are used to create GCC tree BLOCK nodes.  */
872
873 struct binding_level
874 {
875   /* A chain of ..._DECL nodes for all variables, constants, functions,
876      parameters and type declarations.  These ..._DECL nodes are chained
877      through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
878      in the reverse of the order supplied to be compatible with the
879      back-end.  */
880   tree names;
881   /* For each level (except the global one), a chain of BLOCK nodes for all
882      the levels that were entered and exited one level down from this one.  */
883   tree blocks;
884
885   tree stmts;
886   /* The binding level containing this one (the enclosing binding level). */
887   struct binding_level *level_chain;
888 };
889
890 /* The binding level currently in effect.  */
891 static struct binding_level *current_binding_level = NULL;
892
893 /* The outermost binding level. This binding level is created when the
894    compiler is started and it will exist through the entire compilation.  */
895 static struct binding_level *global_binding_level;
896
897 /* Binding level structures are initialized by copying this one.  */
898 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL };
899 \f
900 /* Return non-zero if we are currently in the global binding level.  */
901
902 static int
903 global_bindings_p (void)
904 {
905   return current_binding_level == global_binding_level ? -1 : 0;
906 }
907
908
909 /* Return the list of declarations in the current level. Note that this list
910    is in reverse order (it has to be so for back-end compatibility).  */
911
912 static tree
913 getdecls (void)
914 {
915   return current_binding_level->names;
916 }
917
918 /* Return a STATMENT_LIST for the current block.  */
919
920 static tree*
921 getstmtlist (void)
922 {
923   return &current_binding_level->stmts;
924 }
925
926 /* Enter a new binding level. The input parameter is ignored, but has to be
927    specified for back-end compatibility.  */
928
929 static void
930 pushlevel (int ignore ATTRIBUTE_UNUSED)
931 {
932   struct binding_level *newlevel = XNEW (struct binding_level);
933
934   *newlevel = clear_binding_level;
935
936   /* Add this level to the front of the chain (stack) of levels that are
937      active.  */
938   newlevel->level_chain = current_binding_level;
939   current_binding_level = newlevel;
940   current_binding_level->stmts = alloc_stmt_list ();
941 }
942
943 /* Exit a binding level.
944    Pop the level off, and restore the state of the identifier-decl mappings
945    that were in effect when this level was entered.
946
947    If KEEP is nonzero, this level had explicit declarations, so
948    and create a "block" (a BLOCK node) for the level
949    to record its declarations and subblocks for symbol table output.
950
951    If FUNCTIONBODY is nonzero, this level is the body of a function,
952    so create a block as if KEEP were set and also clear out all
953    label names.
954
955    If REVERSE is nonzero, reverse the order of decls before putting
956    them into the BLOCK.  */
957
958 static tree
959 poplevel (int keep, int reverse, int functionbody)
960 {
961   /* Points to a BLOCK tree node. This is the BLOCK node constructed for the
962      binding level that we are about to exit and which is returned by this
963      routine.  */
964   tree block_node = NULL_TREE;
965   tree decl_chain;
966   tree subblock_chain = current_binding_level->blocks;
967   tree subblock_node;
968
969   /* Reverse the list of *_DECL nodes if desired.  Note that the ..._DECL
970      nodes chained through the `names' field of current_binding_level are in
971      reverse order except for PARM_DECL node, which are explicitly stored in
972      the right order.  */
973   decl_chain = (reverse) ? nreverse (current_binding_level->names)
974                          : current_binding_level->names;
975
976   /* If there were any declarations in the current binding level, or if this
977      binding level is a function body, or if there are any nested blocks then
978      create a BLOCK node to record them for the life of this function.  */
979   if (keep || functionbody)
980     block_node = build_block (keep ? decl_chain : 0, subblock_chain, 0, 0);
981
982   /* Record the BLOCK node just built as the subblock its enclosing scope.  */
983   for (subblock_node = subblock_chain; subblock_node;
984        subblock_node = TREE_CHAIN (subblock_node))
985     BLOCK_SUPERCONTEXT (subblock_node) = block_node;
986
987   /* Clear out the meanings of the local variables of this level.  */
988
989   for (subblock_node = decl_chain; subblock_node;
990        subblock_node = TREE_CHAIN (subblock_node))
991     if (DECL_NAME (subblock_node) != 0)
992       /* If the identifier was used or addressed via a local extern decl,  
993          don't forget that fact.   */
994       if (DECL_EXTERNAL (subblock_node))
995         {
996           if (TREE_USED (subblock_node))
997             TREE_USED (DECL_NAME (subblock_node)) = 1;
998         }
999
1000   /* Pop the current level.  */
1001   current_binding_level = current_binding_level->level_chain;
1002
1003   if (functionbody)
1004     {
1005       /* This is the top level block of a function.  */
1006       DECL_INITIAL (current_function_decl) = block_node;
1007     }
1008   else if (block_node)
1009     {
1010       current_binding_level->blocks
1011         = chainon (current_binding_level->blocks, block_node);
1012     }
1013
1014   /* If we did not make a block for the level just exited, any blocks made for
1015      inner levels (since they cannot be recorded as subblocks in that level)
1016      must be carried forward so they will later become subblocks of something
1017      else.  */
1018   else if (subblock_chain)
1019     current_binding_level->blocks
1020       = chainon (current_binding_level->blocks, subblock_chain);
1021   if (block_node)
1022     TREE_USED (block_node) = 1;
1023
1024   return block_node;
1025 }
1026 \f
1027 /* Insert BLOCK at the end of the list of subblocks of the
1028    current binding level.  This is used when a BIND_EXPR is expanded,
1029    to handle the BLOCK node inside the BIND_EXPR.  */
1030
1031 static void
1032 insert_block (tree block)
1033 {
1034   TREE_USED (block) = 1;
1035   current_binding_level->blocks
1036     = chainon (current_binding_level->blocks, block);
1037 }
1038
1039
1040 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1041    Returns the ..._DECL node. */
1042
1043 tree
1044 pushdecl (tree decl)
1045 {
1046   /* External objects aren't nested, other objects may be.  */
1047     
1048   if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1049     DECL_CONTEXT (decl) = 0;
1050   else
1051     DECL_CONTEXT (decl) = current_function_decl;
1052
1053   /* Put the declaration on the list.  The list of declarations is in reverse
1054      order. The list will be reversed later if necessary.  This needs to be
1055      this way for compatibility with the back-end.  */
1056
1057   TREE_CHAIN (decl) = current_binding_level->names;
1058   current_binding_level->names = decl;
1059
1060   /* For the declaration of a type, set its name if it is not already set. */
1061
1062   if (TREE_CODE (decl) == TYPE_DECL
1063       && TYPE_NAME (TREE_TYPE (decl)) == 0)
1064     TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1065
1066   /* Put automatic variables into the intermediate representation.  */
1067   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
1068       && !TREE_STATIC (decl) && !TREE_PUBLIC (decl))
1069     tree_code_output_expression_statement (build1 (DECL_EXPR, void_type_node,
1070                                                    decl),
1071                                            DECL_SOURCE_LOCATION (decl));
1072   return decl;
1073 }
1074 \f
1075
1076 static void
1077 tree_push_type_decl(tree id, tree type_node)
1078 {
1079   tree decl = build_decl (TYPE_DECL, id, type_node);
1080   TYPE_NAME (type_node) = id;
1081   pushdecl (decl);
1082 }
1083
1084 #define NULL_BINDING_LEVEL (struct binding_level *) NULL                        
1085
1086 /* Create the predefined scalar types of C,
1087    and some nodes representing standard constants (0, 1, (void *) 0).
1088    Initialize the global binding level.
1089    Make definitions for built-in primitive functions.  */
1090
1091 void
1092 treelang_init_decl_processing (void)
1093 {
1094   current_function_decl = NULL;
1095   current_binding_level = NULL_BINDING_LEVEL;
1096   pushlevel (0);        /* make the binding_level structure for global names */
1097   global_binding_level = current_binding_level;
1098
1099   build_common_tree_nodes (flag_signed_char, false);
1100
1101   /* set standard type names */
1102
1103   /* Define `int' and `char' last so that they are not overwritten.  */
1104   tree_push_type_decl (NULL_TREE, intQI_type_node);
1105   tree_push_type_decl (NULL_TREE, intHI_type_node);
1106   tree_push_type_decl (NULL_TREE, intSI_type_node);
1107   tree_push_type_decl (NULL_TREE, intDI_type_node);
1108 #if HOST_BITS_PER_WIDE_INT >= 64
1109   tree_push_type_decl (NULL_TREE, intTI_type_node);
1110 #endif
1111   tree_push_type_decl (NULL_TREE, unsigned_intQI_type_node);
1112   tree_push_type_decl (NULL_TREE, unsigned_intHI_type_node);
1113   tree_push_type_decl (NULL_TREE, unsigned_intSI_type_node);
1114   tree_push_type_decl (NULL_TREE, unsigned_intDI_type_node);
1115 #if HOST_BITS_PER_WIDE_INT >= 64
1116   tree_push_type_decl (NULL_TREE, unsigned_intTI_type_node);
1117 #endif
1118
1119   tree_push_type_decl (get_identifier ("int"), integer_type_node);
1120   tree_push_type_decl (get_identifier ("char"), char_type_node);
1121   tree_push_type_decl (get_identifier ("long int"),
1122                               long_integer_type_node);
1123   tree_push_type_decl (get_identifier ("unsigned int"),
1124                               unsigned_type_node);
1125   tree_push_type_decl (get_identifier ("long unsigned int"),
1126                               long_unsigned_type_node);
1127   tree_push_type_decl (get_identifier ("long long int"),
1128                               long_long_integer_type_node);
1129   tree_push_type_decl (get_identifier ("long long unsigned int"),
1130                               long_long_unsigned_type_node);
1131   tree_push_type_decl (get_identifier ("short int"),
1132                               short_integer_type_node);
1133   tree_push_type_decl (get_identifier ("short unsigned int"),
1134                               short_unsigned_type_node);
1135   tree_push_type_decl (get_identifier ("signed char"),
1136                               signed_char_type_node);
1137   tree_push_type_decl (get_identifier ("unsigned char"),
1138                               unsigned_char_type_node);
1139   size_type_node = make_unsigned_type (POINTER_SIZE);
1140   tree_push_type_decl (get_identifier ("size_t"), size_type_node);
1141   set_sizetype (size_type_node);
1142
1143   build_common_tree_nodes_2 (/* short_double= */ 0);
1144
1145   tree_push_type_decl (get_identifier ("float"), float_type_node);
1146   tree_push_type_decl (get_identifier ("double"), double_type_node);
1147   tree_push_type_decl (get_identifier ("long double"), long_double_type_node);
1148   tree_push_type_decl (get_identifier ("void"), void_type_node);
1149
1150   build_common_builtin_nodes ();
1151   (*targetm.init_builtins) ();
1152
1153   pedantic_lvalues = pedantic;
1154 }
1155
1156 static tree
1157 handle_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1158                   int ARG_UNUSED (flags), bool *no_add_attrs)
1159 {
1160   if (TREE_CODE (*node) == FUNCTION_DECL)
1161     {
1162       if (strcmp (IDENTIFIER_POINTER (name), "const") == 0)
1163         TREE_READONLY (*node) = 1;
1164       if (strcmp (IDENTIFIER_POINTER (name), "nothrow") == 0)
1165         TREE_NOTHROW (*node) = 1;
1166     }
1167   else
1168     {
1169       warning (OPT_Wattributes, "%qD attribute ignored", name);
1170       *no_add_attrs = true;
1171     }
1172
1173   return NULL_TREE;
1174 }
1175
1176 const struct attribute_spec treelang_attribute_table[] =
1177 {
1178   { "const", 0, 0, true, false, false, handle_attribute },
1179   { "nothrow", 0, 0, true, false, false, handle_attribute },
1180   { NULL, 0, 0, false, false, false, NULL },
1181 };
1182
1183 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1184 #include "gt-treelang-treetree.h"