OSDN Git Service

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