2004-09-17 Jeffrey D. Oldham <oldham@codesourcery.com>
[official-gcc.git] / gcc / ada / trans.c
blob4defe553e628f50b28509662d7b27cecddcd7104
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2004, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 * *
25 ****************************************************************************/
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "real.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "rtl.h"
36 #include "expr.h"
37 #include "ggc.h"
38 #include "function.h"
39 #include "except.h"
40 #include "debug.h"
41 #include "output.h"
42 #include "tree-gimple.h"
43 #include "ada.h"
44 #include "types.h"
45 #include "atree.h"
46 #include "elists.h"
47 #include "namet.h"
48 #include "nlists.h"
49 #include "snames.h"
50 #include "stringt.h"
51 #include "uintp.h"
52 #include "urealp.h"
53 #include "fe.h"
54 #include "sinfo.h"
55 #include "einfo.h"
56 #include "ada-tree.h"
57 #include "gigi.h"
59 int max_gnat_nodes;
60 int number_names;
61 struct Node *Nodes_Ptr;
62 Node_Id *Next_Node_Ptr;
63 Node_Id *Prev_Node_Ptr;
64 struct Elist_Header *Elists_Ptr;
65 struct Elmt_Item *Elmts_Ptr;
66 struct String_Entry *Strings_Ptr;
67 Char_Code *String_Chars_Ptr;
68 struct List_Header *List_Headers_Ptr;
70 /* Current filename without path. */
71 const char *ref_filename;
73 /* If true, then gigi is being called on an analyzed but unexpanded
74 tree, and the only purpose of the call is to properly annotate
75 types with representation information. */
76 bool type_annotate_only;
78 /* A structure used to gather together information about a statement group.
79 We use this to gather related statements, for example the "then" part
80 of a IF. In the case where it represents a lexical scope, we may also
81 have a BLOCK node corresponding to it and/or cleanups. */
83 struct stmt_group GTY((chain_next ("%h.previous"))) {
84 struct stmt_group *previous; /* Previous code group. */
85 tree stmt_list; /* List of statements for this code group. */
86 tree block; /* BLOCK for this code group, if any. */
87 tree cleanups; /* Cleanups for this code group, if any. */
90 static GTY(()) struct stmt_group *current_stmt_group;
92 /* List of unused struct stmt_group nodes. */
93 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
95 /* A structure used to record information on elaboration procedures
96 we've made and need to process.
98 ??? gnat_node should be Node_Id, but gengtype gets confused. */
100 struct elab_info GTY((chain_next ("%h.next"))) {
101 struct elab_info *next; /* Pointer to next in chain. */
102 tree elab_proc; /* Elaboration procedure. */
103 int gnat_node; /* The N_Compilation_Unit. */
106 static GTY(()) struct elab_info *elab_info_list;
108 /* Free list of TREE_LIST nodes used for stacks. */
109 static GTY((deletable)) tree gnu_stack_free_list;
111 /* List of TREE_LIST nodes representing a stack of exception pointer
112 variables. TREE_VALUE is the VAR_DECL that stores the address of
113 the raised exception. Nonzero means we are in an exception
114 handler. Not used in the zero-cost case. */
115 static GTY(()) tree gnu_except_ptr_stack;
117 /* List of TREE_LIST nodes used to store the current elaboration procedure
118 decl. TREE_VALUE is the decl. */
119 static GTY(()) tree gnu_elab_proc_stack;
121 /* Variable that stores a list of labels to be used as a goto target instead of
122 a return in some functions. See processing for N_Subprogram_Body. */
123 static GTY(()) tree gnu_return_label_stack;
125 /* List of TREE_LIST nodes representing a stack of LOOP_STMT nodes.
126 TREE_VALUE of each entry is the label of the corresponding LOOP_STMT. */
127 static GTY(()) tree gnu_loop_label_stack;
129 /* List of TREE_LIST nodes representing labels for switch statements.
130 TREE_VALUE of each entry is the label at the end of the switch. */
131 static GTY(()) tree gnu_switch_label_stack;
133 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
134 static enum tree_code gnu_codes[Number_Node_Kinds];
136 /* Current node being treated, in case abort called. */
137 Node_Id error_gnat_node;
139 static void Compilation_Unit_to_gnu (Node_Id);
140 static void record_code_position (Node_Id);
141 static void insert_code_for (Node_Id);
142 static void start_stmt_group (void);
143 static void add_cleanup (tree);
144 static tree mark_visited (tree *, int *, void *);
145 static tree mark_unvisited (tree *, int *, void *);
146 static tree end_stmt_group (void);
147 static void add_stmt_list (List_Id);
148 static tree build_stmt_group (List_Id, bool);
149 static void push_stack (tree *, tree, tree);
150 static void pop_stack (tree *);
151 static enum gimplify_status gnat_gimplify_stmt (tree *);
152 static void elaborate_all_entities (Node_Id);
153 static void process_freeze_entity (Node_Id);
154 static void process_inlined_subprograms (Node_Id);
155 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
156 static tree emit_range_check (tree, Node_Id);
157 static tree emit_index_check (tree, tree, tree, tree);
158 static tree emit_check (tree, tree, int);
159 static tree convert_with_check (Entity_Id, tree, bool, bool, bool);
160 static bool addressable_p (tree);
161 static tree assoc_to_constructor (Node_Id, tree);
162 static tree extract_values (tree, tree);
163 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
164 static tree maybe_implicit_deref (tree);
165 static tree gnat_stabilize_reference_1 (tree, bool);
166 static void annotate_with_node (tree, Node_Id);
168 /* Constants for +0.5 and -0.5 for float-to-integer rounding. */
169 static REAL_VALUE_TYPE dconstp5;
170 static REAL_VALUE_TYPE dconstmp5;
172 /* This is the main program of the back-end. It sets up all the table
173 structures and then generates code. */
175 void
176 gigi (Node_Id gnat_root, int max_gnat_node, int number_name,
177 struct Node *nodes_ptr, Node_Id *next_node_ptr, Node_Id *prev_node_ptr,
178 struct Elist_Header *elists_ptr, struct Elmt_Item *elmts_ptr,
179 struct String_Entry *strings_ptr, Char_Code *string_chars_ptr,
180 struct List_Header *list_headers_ptr, Int number_units ATTRIBUTE_UNUSED,
181 char *file_info_ptr ATTRIBUTE_UNUSED, Entity_Id standard_integer,
182 Entity_Id standard_long_long_float, Entity_Id standard_exception_type,
183 Int gigi_operating_mode)
185 tree gnu_standard_long_long_float;
186 tree gnu_standard_exception_type;
187 struct elab_info *info;
189 max_gnat_nodes = max_gnat_node;
190 number_names = number_name;
191 Nodes_Ptr = nodes_ptr;
192 Next_Node_Ptr = next_node_ptr;
193 Prev_Node_Ptr = prev_node_ptr;
194 Elists_Ptr = elists_ptr;
195 Elmts_Ptr = elmts_ptr;
196 Strings_Ptr = strings_ptr;
197 String_Chars_Ptr = string_chars_ptr;
198 List_Headers_Ptr = list_headers_ptr;
200 type_annotate_only = (gigi_operating_mode == 1);
202 init_gnat_to_gnu ();
203 gnat_compute_largest_alignment ();
204 init_dummy_type ();
206 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
207 errors. */
208 if (type_annotate_only)
210 TYPE_SIZE (void_type_node) = bitsize_zero_node;
211 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
214 /* Save the type we made for integer as the type for Standard.Integer.
215 Then make the rest of the standard types. Note that some of these
216 may be subtypes. */
217 save_gnu_tree (Base_Type (standard_integer), TYPE_NAME (integer_type_node),
218 false);
220 gnu_except_ptr_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
222 gnu_standard_long_long_float
223 = gnat_to_gnu_entity (Base_Type (standard_long_long_float), NULL_TREE, 0);
224 gnu_standard_exception_type
225 = gnat_to_gnu_entity (Base_Type (standard_exception_type), NULL_TREE, 0);
227 init_gigi_decls (gnu_standard_long_long_float, gnu_standard_exception_type);
229 /* Process any Pragma Ident for the main unit. */
230 #ifdef ASM_OUTPUT_IDENT
231 if (Present (Ident_String (Main_Unit)))
232 ASM_OUTPUT_IDENT
233 (asm_out_file,
234 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
235 #endif
237 /* If we are using the GCC exception mechanism, let GCC know. */
238 if (Exception_Mechanism == GCC_ZCX)
239 gnat_init_gcc_eh ();
241 if (Nkind (gnat_root) != N_Compilation_Unit)
242 abort ();
244 Compilation_Unit_to_gnu (gnat_root);
246 /* Now see if we have any elaboration procedures to deal with. */
247 for (info = elab_info_list; info; info = info->next)
249 tree gnu_body = DECL_SAVED_TREE (info->elab_proc);
250 tree gnu_stmts;
252 /* Mark everything we have as not visited. */
253 walk_tree_without_duplicates (&gnu_body, mark_unvisited, NULL);
255 /* Set the current function to be the elaboration procedure and gimplify
256 what we have. */
257 current_function_decl = info->elab_proc;
258 gimplify_body (&gnu_body, info->elab_proc);
260 /* We should have a BIND_EXPR, but it may or may not have any statements
261 in it. If it doesn't have any, we have nothing to do. */
262 gnu_stmts = gnu_body;
263 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
264 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
266 /* If there are no statements, there is no elaboration code. */
267 if (!gnu_stmts || !STATEMENT_LIST_HEAD (gnu_stmts))
268 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
269 else
271 /* Otherwise, compile the function. Note that we'll be gimplifying
272 it twice, but that's fine for the nodes we use. */
273 begin_subprog_body (info->elab_proc);
274 end_subprog_body (gnu_body);
279 /* Perform initializations for this module. */
281 void
282 gnat_init_stmt_group ()
284 /* Initialize ourselves. */
285 init_code_table ();
286 start_stmt_group ();
288 /* Enable GNAT stack checking method if needed */
289 if (!Stack_Check_Probes_On_Target)
290 set_stack_check_libfunc (gen_rtx_SYMBOL_REF (Pmode, "_gnat_stack_check"));
292 if (Exception_Mechanism == Front_End_ZCX)
293 abort ();
295 REAL_ARITHMETIC (dconstp5, RDIV_EXPR, dconst1, dconst2);
296 REAL_ARITHMETIC (dconstmp5, RDIV_EXPR, dconstm1, dconst2);
299 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
300 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
301 where we should place the result type. */
303 static tree
304 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
306 tree gnu_result_type;
307 tree gnu_result;
308 Node_Id gnat_temp, gnat_temp_type;
310 /* If the Etype of this node does not equal the Etype of the Entity,
311 something is wrong with the entity map, probably in generic
312 instantiation. However, this does not apply to types. Since we sometime
313 have strange Ekind's, just do this test for objects. Also, if the Etype of
314 the Entity is private, the Etype of the N_Identifier is allowed to be the
315 full type and also we consider a packed array type to be the same as the
316 original type. Similarly, a class-wide type is equivalent to a subtype of
317 itself. Finally, if the types are Itypes, one may be a copy of the other,
318 which is also legal. */
319 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
320 ? gnat_node : Entity (gnat_node));
321 gnat_temp_type = Etype (gnat_temp);
323 if (Etype (gnat_node) != gnat_temp_type
324 && !(Is_Packed (gnat_temp_type)
325 && Etype (gnat_node) == Packed_Array_Type (gnat_temp_type))
326 && !(Is_Class_Wide_Type (Etype (gnat_node)))
327 && !(IN (Ekind (gnat_temp_type), Private_Kind)
328 && Present (Full_View (gnat_temp_type))
329 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
330 || (Is_Packed (Full_View (gnat_temp_type))
331 && (Etype (gnat_node)
332 == Packed_Array_Type (Full_View (gnat_temp_type))))))
333 && (!Is_Itype (Etype (gnat_node)) || !Is_Itype (gnat_temp_type))
334 && (Ekind (gnat_temp) == E_Variable
335 || Ekind (gnat_temp) == E_Component
336 || Ekind (gnat_temp) == E_Constant
337 || Ekind (gnat_temp) == E_Loop_Parameter
338 || IN (Ekind (gnat_temp), Formal_Kind)))
339 abort ();
341 /* If this is a reference to a deferred constant whose partial view is an
342 unconstrained private type, the proper type is on the full view of the
343 constant, not on the full view of the type, which may be unconstrained.
345 This may be a reference to a type, for example in the prefix of the
346 attribute Position, generated for dispatching code (see Make_DT in
347 exp_disp,adb). In that case we need the type itself, not is parent,
348 in particular if it is a derived type */
349 if (Is_Private_Type (gnat_temp_type)
350 && Has_Unknown_Discriminants (gnat_temp_type)
351 && Present (Full_View (gnat_temp))
352 && !Is_Type (gnat_temp))
354 gnat_temp = Full_View (gnat_temp);
355 gnat_temp_type = Etype (gnat_temp);
356 gnu_result_type = get_unpadded_type (gnat_temp_type);
358 else
360 /* Expand the type of this identitier first, in case it is an enumeral
361 literal, which only get made when the type is expanded. There is no
362 order-of-elaboration issue here. We want to use the Actual_Subtype if
363 it has already been elaborated, otherwise the Etype. Avoid using
364 Actual_Subtype for packed arrays to simplify things. */
365 if ((Ekind (gnat_temp) == E_Constant
366 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
367 && !(Is_Array_Type (Etype (gnat_temp))
368 && Present (Packed_Array_Type (Etype (gnat_temp))))
369 && Present (Actual_Subtype (gnat_temp))
370 && present_gnu_tree (Actual_Subtype (gnat_temp)))
371 gnat_temp_type = Actual_Subtype (gnat_temp);
372 else
373 gnat_temp_type = Etype (gnat_node);
375 gnu_result_type = get_unpadded_type (gnat_temp_type);
378 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
380 /* If we are in an exception handler, force this variable into memory to
381 ensure optimization does not remove stores that appear redundant but are
382 actually needed in case an exception occurs.
384 ??? Note that we need not do this if the variable is declared within the
385 handler, only if it is referenced in the handler and declared in an
386 enclosing block, but we have no way of testing that right now.
388 ??? Also, for now all we can do is make it volatile. But we only
389 do this for SJLJ. */
390 if (TREE_VALUE (gnu_except_ptr_stack)
391 && TREE_CODE (gnu_result) == VAR_DECL)
392 TREE_THIS_VOLATILE (gnu_result) = TREE_SIDE_EFFECTS (gnu_result) = 1;
394 /* Some objects (such as parameters passed by reference, globals of
395 variable size, and renamed objects) actually represent the address
396 of the object. In that case, we must do the dereference. Likewise,
397 deal with parameters to foreign convention subprograms. Call fold
398 here since GNU_RESULT may be a CONST_DECL. */
399 if (DECL_P (gnu_result)
400 && (DECL_BY_REF_P (gnu_result)
401 || (TREE_CODE (gnu_result) == PARM_DECL
402 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
404 bool ro = DECL_POINTS_TO_READONLY_P (gnu_result);
405 tree initial;
407 if (TREE_CODE (gnu_result) == PARM_DECL
408 && DECL_BY_COMPONENT_PTR_P (gnu_result))
409 gnu_result
410 = build_unary_op (INDIRECT_REF, NULL_TREE,
411 convert (build_pointer_type (gnu_result_type),
412 gnu_result));
414 /* If the object is constant, we try to do the dereference directly
415 through the DECL_INITIAL. This is actually required in order to get
416 correct aliasing information for renamed objects that are components
417 of non-aliased aggregates, because the type of the renamed object and
418 that of the aggregate don't alias.
420 Note that we expect the initial value to have been stabilized.
421 If it contains e.g. a variable reference, we certainly don't want
422 to re-evaluate the variable each time the renaming is used.
424 Stabilization is currently not performed at the global level but
425 create_var_decl avoids setting DECL_INITIAL if the value is not
426 constant then, and we get to the pointer dereference below.
428 ??? Couldn't the aliasing issue show up again in this case ?
429 There is no obvious reason why not. */
430 else if (TREE_READONLY (gnu_result)
431 && DECL_INITIAL (gnu_result)
432 /* Strip possible conversion to reference type. */
433 && ((initial = TREE_CODE (DECL_INITIAL (gnu_result))
434 == NOP_EXPR
435 ? TREE_OPERAND (DECL_INITIAL (gnu_result), 0)
436 : DECL_INITIAL (gnu_result), 1))
437 && TREE_CODE (initial) == ADDR_EXPR
438 && (TREE_CODE (TREE_OPERAND (initial, 0)) == ARRAY_REF
439 || (TREE_CODE (TREE_OPERAND (initial, 0))
440 == COMPONENT_REF)))
441 gnu_result = TREE_OPERAND (initial, 0);
442 else
443 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE,
444 fold (gnu_result));
446 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result) = ro;
449 /* The GNAT tree has the type of a function as the type of its result. Also
450 use the type of the result if the Etype is a subtype which is nominally
451 unconstrained. But remove any padding from the resulting type. */
452 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
453 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type))
455 gnu_result_type = TREE_TYPE (gnu_result);
456 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
457 && TYPE_IS_PADDING_P (gnu_result_type))
458 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
461 /* We always want to return the underlying INTEGER_CST for an enumeration
462 literal to avoid the need to call fold in lots of places. But don't do
463 this is the parent will be taking the address of this object. */
464 if (TREE_CODE (gnu_result) == CONST_DECL)
466 gnat_temp = Parent (gnat_node);
467 if (!DECL_CONST_CORRESPONDING_VAR (gnu_result)
468 || (Nkind (gnat_temp) != N_Reference
469 && !(Nkind (gnat_temp) == N_Attribute_Reference
470 && ((Get_Attribute_Id (Attribute_Name (gnat_temp))
471 == Attr_Address)
472 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
473 == Attr_Access)
474 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
475 == Attr_Unchecked_Access)
476 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
477 == Attr_Unrestricted_Access)))))
478 gnu_result = DECL_INITIAL (gnu_result);
481 *gnu_result_type_p = gnu_result_type;
482 return gnu_result;
485 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
486 any statements we generate. */
488 static tree
489 Pragma_to_gnu (Node_Id gnat_node)
491 Node_Id gnat_temp;
492 tree gnu_result = alloc_stmt_list ();
494 /* Check for (and ignore) unrecognized pragma and do nothing if we are just
495 annotating types. */
496 if (type_annotate_only || !Is_Pragma_Name (Chars (gnat_node)))
497 return gnu_result;
499 switch (Get_Pragma_Id (Chars (gnat_node)))
501 case Pragma_Inspection_Point:
502 /* Do nothing at top level: all such variables are already viewable. */
503 if (global_bindings_p ())
504 break;
506 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
507 Present (gnat_temp);
508 gnat_temp = Next (gnat_temp))
510 tree gnu_expr = gnat_to_gnu (Expression (gnat_temp));
512 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
513 gnu_expr = TREE_OPERAND (gnu_expr, 0);
515 gnu_expr = build1 (USE_STMT, void_type_node, gnu_expr);
516 annotate_with_node (gnu_expr, gnat_node);
517 append_to_statement_list (gnu_expr, &gnu_result);
519 break;
521 case Pragma_Optimize:
522 switch (Chars (Expression
523 (First (Pragma_Argument_Associations (gnat_node)))))
525 case Name_Time: case Name_Space:
526 if (optimize == 0)
527 post_error ("insufficient -O value?", gnat_node);
528 break;
530 case Name_Off:
531 if (optimize != 0)
532 post_error ("must specify -O0?", gnat_node);
533 break;
535 default:
536 abort ();
537 break;
539 break;
541 case Pragma_Reviewable:
542 if (write_symbols == NO_DEBUG)
543 post_error ("must specify -g?", gnat_node);
544 break;
547 return gnu_result;
549 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Attribute,
550 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
551 where we should place the result type. ATTRIBUTE is the attribute ID. */
553 static tree
554 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
556 tree gnu_result = error_mark_node;
557 tree gnu_result_type;
558 tree gnu_expr;
559 bool prefix_unused = false;
560 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
561 tree gnu_type = TREE_TYPE (gnu_prefix);
563 /* If the input is a NULL_EXPR, make a new one. */
564 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
566 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
567 return build1 (NULL_EXPR, *gnu_result_type_p,
568 TREE_OPERAND (gnu_prefix, 0));
571 switch (attribute)
573 case Attr_Pos:
574 case Attr_Val:
575 /* These are just conversions until since representation clauses for
576 enumerations are handled in the front end. */
578 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
580 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
581 gnu_result_type = get_unpadded_type (Etype (gnat_node));
582 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
583 checkp, checkp, true);
585 break;
587 case Attr_Pred:
588 case Attr_Succ:
589 /* These just add or subject the constant 1. Representation clauses for
590 enumerations are handled in the front-end. */
591 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
592 gnu_result_type = get_unpadded_type (Etype (gnat_node));
594 if (Do_Range_Check (First (Expressions (gnat_node))))
596 gnu_expr = protect_multiple_eval (gnu_expr);
597 gnu_expr
598 = emit_check
599 (build_binary_op (EQ_EXPR, integer_type_node,
600 gnu_expr,
601 attribute == Attr_Pred
602 ? TYPE_MIN_VALUE (gnu_result_type)
603 : TYPE_MAX_VALUE (gnu_result_type)),
604 gnu_expr, CE_Range_Check_Failed);
607 gnu_result
608 = build_binary_op (attribute == Attr_Pred
609 ? MINUS_EXPR : PLUS_EXPR,
610 gnu_result_type, gnu_expr,
611 convert (gnu_result_type, integer_one_node));
612 break;
614 case Attr_Address:
615 case Attr_Unrestricted_Access:
616 /* Conversions don't change something's address but can cause us to miss
617 the COMPONENT_REF case below, so strip them off. */
618 gnu_prefix = remove_conversions (gnu_prefix,
619 !Must_Be_Byte_Aligned (gnat_node));
621 /* If we are taking 'Address of an unconstrained object, this is the
622 pointer to the underlying array. */
623 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
625 /* ... fall through ... */
627 case Attr_Access:
628 case Attr_Unchecked_Access:
629 case Attr_Code_Address:
630 gnu_result_type = get_unpadded_type (Etype (gnat_node));
631 gnu_result
632 = build_unary_op (((attribute == Attr_Address
633 || attribute == Attr_Unrestricted_Access)
634 && !Must_Be_Byte_Aligned (gnat_node))
635 ? ATTR_ADDR_EXPR : ADDR_EXPR,
636 gnu_result_type, gnu_prefix);
638 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
639 don't try to build a trampoline. */
640 if (attribute == Attr_Code_Address)
642 for (gnu_expr = gnu_result;
643 TREE_CODE (gnu_expr) == NOP_EXPR
644 || TREE_CODE (gnu_expr) == CONVERT_EXPR;
645 gnu_expr = TREE_OPERAND (gnu_expr, 0))
646 TREE_CONSTANT (gnu_expr) = 1;
648 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
649 TREE_STATIC (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
651 break;
653 case Attr_Pool_Address:
655 tree gnu_obj_type;
656 tree gnu_ptr = gnu_prefix;
658 gnu_result_type = get_unpadded_type (Etype (gnat_node));
660 /* If this is an unconstrained array, we know the object must have been
661 allocated with the template in front of the object. So compute the
662 template address.*/
663 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
664 gnu_ptr
665 = convert (build_pointer_type
666 (TYPE_OBJECT_RECORD_TYPE
667 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
668 gnu_ptr);
670 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
671 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
672 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
674 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
675 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
676 tree gnu_byte_offset
677 = convert (gnu_char_ptr_type,
678 size_diffop (size_zero_node, gnu_pos));
680 gnu_ptr = convert (gnu_char_ptr_type, gnu_ptr);
681 gnu_ptr = build_binary_op (MINUS_EXPR, gnu_char_ptr_type,
682 gnu_ptr, gnu_byte_offset);
685 gnu_result = convert (gnu_result_type, gnu_ptr);
687 break;
689 case Attr_Size:
690 case Attr_Object_Size:
691 case Attr_Value_Size:
692 case Attr_Max_Size_In_Storage_Elements:
693 gnu_expr = gnu_prefix;
695 /* Remove NOPS from gnu_expr and conversions from gnu_prefix.
696 We only use GNU_EXPR to see if a COMPONENT_REF was involved. */
697 while (TREE_CODE (gnu_expr) == NOP_EXPR)
698 gnu_expr = TREE_OPERAND (gnu_expr, 0)
701 gnu_prefix = remove_conversions (gnu_prefix, true);
702 prefix_unused = true;
703 gnu_type = TREE_TYPE (gnu_prefix);
705 /* Replace an unconstrained array type with the type of the underlying
706 array. We can't do this with a call to maybe_unconstrained_array
707 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
708 use the record type that will be used to allocate the object and its
709 template. */
710 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
712 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
713 if (attribute != Attr_Max_Size_In_Storage_Elements)
714 gnu_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
717 /* If we're looking for the size of a field, return the field size.
718 Otherwise, if the prefix is an object, or if 'Object_Size or
719 'Max_Size_In_Storage_Elements has been specified, the result is the
720 GCC size of the type. Otherwise, the result is the RM_Size of the
721 type. */
722 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
723 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
724 else if (TREE_CODE (gnu_prefix) != TYPE_DECL
725 || attribute == Attr_Object_Size
726 || attribute == Attr_Max_Size_In_Storage_Elements)
728 /* If this is a padded type, the GCC size isn't relevant to the
729 programmer. Normally, what we want is the RM_Size, which was set
730 from the specified size, but if it was not set, we want the size
731 of the relevant field. Using the MAX of those two produces the
732 right result in all case. Don't use the size of the field if it's
733 a self-referential type, since that's never what's wanted. */
734 if (TREE_CODE (gnu_type) == RECORD_TYPE
735 && TYPE_IS_PADDING_P (gnu_type)
736 && TREE_CODE (gnu_expr) == COMPONENT_REF)
738 gnu_result = rm_size (gnu_type);
739 if (!(CONTAINS_PLACEHOLDER_P
740 (DECL_SIZE (TREE_OPERAND (gnu_expr, 1)))))
741 gnu_result
742 = size_binop (MAX_EXPR, gnu_result,
743 DECL_SIZE (TREE_OPERAND (gnu_expr, 1)));
745 else
746 gnu_result = TYPE_SIZE (gnu_type);
748 else
749 gnu_result = rm_size (gnu_type);
751 if (!gnu_result)
752 abort ();
754 /* Deal with a self-referential size by returning the maximum size for a
755 type and by qualifying the size with the object for 'Size of an
756 object. */
757 if (CONTAINS_PLACEHOLDER_P (gnu_result))
759 if (TREE_CODE (gnu_prefix) != TYPE_DECL)
760 gnu_result = substitute_placeholder_in_expr (gnu_result,
761 gnu_expr);
762 else
763 gnu_result = max_size (gnu_result, true);
766 /* If the type contains a template, subtract its size. */
767 if (TREE_CODE (gnu_type) == RECORD_TYPE
768 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
769 gnu_result = size_binop (MINUS_EXPR, gnu_result,
770 DECL_SIZE (TYPE_FIELDS (gnu_type)));
772 gnu_result_type = get_unpadded_type (Etype (gnat_node));
774 /* Always perform division using unsigned arithmetic as the size cannot
775 be negative, but may be an overflowed positive value. This provides
776 correct results for sizes up to 512 MB.
778 ??? Size should be calculated in storage elements directly. */
780 if (attribute == Attr_Max_Size_In_Storage_Elements)
781 gnu_result = convert (sizetype,
782 fold (build2 (CEIL_DIV_EXPR, bitsizetype,
783 gnu_result, bitsize_unit_node)));
784 break;
786 case Attr_Alignment:
787 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
788 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
789 == RECORD_TYPE)
790 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
791 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
793 gnu_type = TREE_TYPE (gnu_prefix);
794 gnu_result_type = get_unpadded_type (Etype (gnat_node));
795 prefix_unused = true;
797 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
798 gnu_result = size_int (DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)));
799 else
800 gnu_result = size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT);
801 break;
803 case Attr_First:
804 case Attr_Last:
805 case Attr_Range_Length:
806 prefix_unused = true;
808 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
810 gnu_result_type = get_unpadded_type (Etype (gnat_node));
812 if (attribute == Attr_First)
813 gnu_result = TYPE_MIN_VALUE (gnu_type);
814 else if (attribute == Attr_Last)
815 gnu_result = TYPE_MAX_VALUE (gnu_type);
816 else
817 gnu_result
818 = build_binary_op
819 (MAX_EXPR, get_base_type (gnu_result_type),
820 build_binary_op
821 (PLUS_EXPR, get_base_type (gnu_result_type),
822 build_binary_op (MINUS_EXPR,
823 get_base_type (gnu_result_type),
824 convert (gnu_result_type,
825 TYPE_MAX_VALUE (gnu_type)),
826 convert (gnu_result_type,
827 TYPE_MIN_VALUE (gnu_type))),
828 convert (gnu_result_type, integer_one_node)),
829 convert (gnu_result_type, integer_zero_node));
831 break;
834 /* ... fall through ... */
836 case Attr_Length:
838 int Dimension = (Present (Expressions (gnat_node))
839 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
840 : 1);
842 /* Make sure any implicit dereference gets done. */
843 gnu_prefix = maybe_implicit_deref (gnu_prefix);
844 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
845 gnu_type = TREE_TYPE (gnu_prefix);
846 prefix_unused = true;
847 gnu_result_type = get_unpadded_type (Etype (gnat_node));
849 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
851 int ndim;
852 tree gnu_type_temp;
854 for (ndim = 1, gnu_type_temp = gnu_type;
855 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
856 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
857 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
860 Dimension = ndim + 1 - Dimension;
863 for (; Dimension > 1; Dimension--)
864 gnu_type = TREE_TYPE (gnu_type);
866 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
867 abort ();
869 if (attribute == Attr_First)
870 gnu_result
871 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
872 else if (attribute == Attr_Last)
873 gnu_result
874 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
875 else
876 /* 'Length or 'Range_Length. */
878 tree gnu_compute_type
879 = gnat_signed_or_unsigned_type (0,
880 get_base_type (gnu_result_type));
882 gnu_result
883 = build_binary_op
884 (MAX_EXPR, gnu_compute_type,
885 build_binary_op
886 (PLUS_EXPR, gnu_compute_type,
887 build_binary_op
888 (MINUS_EXPR, gnu_compute_type,
889 convert (gnu_compute_type,
890 TYPE_MAX_VALUE
891 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)))),
892 convert (gnu_compute_type,
893 TYPE_MIN_VALUE
894 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))))),
895 convert (gnu_compute_type, integer_one_node)),
896 convert (gnu_compute_type, integer_zero_node));
899 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
900 handling. Note that these attributes could not have been used on
901 an unconstrained array type. */
902 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result,
903 gnu_prefix);
904 break;
907 case Attr_Bit_Position:
908 case Attr_Position:
909 case Attr_First_Bit:
910 case Attr_Last_Bit:
911 case Attr_Bit:
913 HOST_WIDE_INT bitsize;
914 HOST_WIDE_INT bitpos;
915 tree gnu_offset;
916 tree gnu_field_bitpos;
917 tree gnu_field_offset;
918 tree gnu_inner;
919 enum machine_mode mode;
920 int unsignedp, volatilep;
922 gnu_result_type = get_unpadded_type (Etype (gnat_node));
923 gnu_prefix = remove_conversions (gnu_prefix, true);
924 prefix_unused = true;
926 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
927 the result is 0. Don't allow 'Bit on a bare component, though. */
928 if (attribute == Attr_Bit
929 && TREE_CODE (gnu_prefix) != COMPONENT_REF
930 && TREE_CODE (gnu_prefix) != FIELD_DECL)
932 gnu_result = integer_zero_node;
933 break;
936 else if (TREE_CODE (gnu_prefix) != COMPONENT_REF
937 && !(attribute == Attr_Bit_Position
938 && TREE_CODE (gnu_prefix) == FIELD_DECL))
939 abort ();
941 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
942 &mode, &unsignedp, &volatilep);
944 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
946 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
947 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
949 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
950 TREE_CODE (gnu_inner) == COMPONENT_REF
951 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
952 gnu_inner = TREE_OPERAND (gnu_inner, 0))
954 gnu_field_bitpos
955 = size_binop (PLUS_EXPR, gnu_field_bitpos,
956 bit_position (TREE_OPERAND (gnu_inner, 1)));
957 gnu_field_offset
958 = size_binop (PLUS_EXPR, gnu_field_offset,
959 byte_position (TREE_OPERAND (gnu_inner, 1)));
962 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
964 gnu_field_bitpos = bit_position (gnu_prefix);
965 gnu_field_offset = byte_position (gnu_prefix);
967 else
969 gnu_field_bitpos = bitsize_zero_node;
970 gnu_field_offset = size_zero_node;
973 switch (attribute)
975 case Attr_Position:
976 gnu_result = gnu_field_offset;
977 break;
979 case Attr_First_Bit:
980 case Attr_Bit:
981 gnu_result = size_int (bitpos % BITS_PER_UNIT);
982 break;
984 case Attr_Last_Bit:
985 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
986 gnu_result = size_binop (PLUS_EXPR, gnu_result,
987 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
988 gnu_result = size_binop (MINUS_EXPR, gnu_result,
989 bitsize_one_node);
990 break;
992 case Attr_Bit_Position:
993 gnu_result = gnu_field_bitpos;
994 break;
997 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
998 we are handling. */
999 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
1000 break;
1003 case Attr_Min:
1004 case Attr_Max:
1006 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
1007 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
1009 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1010 gnu_result = build_binary_op (attribute == Attr_Min
1011 ? MIN_EXPR : MAX_EXPR,
1012 gnu_result_type, gnu_lhs, gnu_rhs);
1014 break;
1016 case Attr_Passed_By_Reference:
1017 gnu_result = size_int (default_pass_by_ref (gnu_type)
1018 || must_pass_by_ref (gnu_type));
1019 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1020 break;
1022 case Attr_Component_Size:
1023 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1024 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
1025 == RECORD_TYPE)
1026 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1027 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1029 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1030 gnu_type = TREE_TYPE (gnu_prefix);
1032 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1033 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
1035 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1036 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
1037 gnu_type = TREE_TYPE (gnu_type);
1039 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
1040 abort ();
1042 /* Note this size cannot be self-referential. */
1043 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
1044 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1045 prefix_unused = true;
1046 break;
1048 case Attr_Null_Parameter:
1049 /* This is just a zero cast to the pointer type for
1050 our prefix and dereferenced. */
1051 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1052 gnu_result
1053 = build_unary_op (INDIRECT_REF, NULL_TREE,
1054 convert (build_pointer_type (gnu_result_type),
1055 integer_zero_node));
1056 TREE_PRIVATE (gnu_result) = 1;
1057 break;
1059 case Attr_Mechanism_Code:
1061 int code;
1062 Entity_Id gnat_obj = Entity (Prefix (gnat_node));
1064 prefix_unused = true;
1065 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1066 if (Present (Expressions (gnat_node)))
1068 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
1070 for (gnat_obj = First_Formal (gnat_obj); i > 1;
1071 i--, gnat_obj = Next_Formal (gnat_obj))
1075 code = Mechanism (gnat_obj);
1076 if (code == Default)
1077 code = ((present_gnu_tree (gnat_obj)
1078 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
1079 || ((TREE_CODE (get_gnu_tree (gnat_obj))
1080 == PARM_DECL)
1081 && (DECL_BY_COMPONENT_PTR_P
1082 (get_gnu_tree (gnat_obj))))))
1083 ? By_Reference : By_Copy);
1084 gnu_result = convert (gnu_result_type, size_int (- code));
1086 break;
1088 default:
1089 /* Say we have an unimplemented attribute. Then set the value to be
1090 returned to be a zero and hope that's something we can convert to the
1091 type of this attribute. */
1092 post_error ("unimplemented attribute", gnat_node);
1093 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1094 gnu_result = integer_zero_node;
1095 break;
1098 /* If this is an attribute where the prefix was unused, force a use of it if
1099 it has a side-effect. But don't do it if the prefix is just an entity
1100 name. However, if an access check is needed, we must do it. See second
1101 example in AARM 11.6(5.e). */
1102 if (prefix_unused && TREE_SIDE_EFFECTS (gnu_prefix)
1103 && !Is_Entity_Name (Prefix (gnat_node)))
1104 gnu_result = fold (build2 (COMPOUND_EXPR, TREE_TYPE (gnu_result),
1105 gnu_prefix, gnu_result));
1107 *gnu_result_type_p = gnu_result_type;
1108 return gnu_result;
1111 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
1112 to a GCC tree, which is returned. */
1114 static tree
1115 Case_Statement_to_gnu (Node_Id gnat_node)
1117 tree gnu_result;
1118 tree gnu_expr;
1119 Node_Id gnat_when;
1121 gnu_expr = gnat_to_gnu (Expression (gnat_node));
1122 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
1124 /* The range of values in a case statement is determined by the rules in
1125 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
1126 of the expression. One exception arises in the case of a simple name that
1127 is parenthesized. This still has the Etype of the name, but since it is
1128 not a name, para 7 does not apply, and we need to go to the base type.
1129 This is the only case where parenthesization affects the dynamic
1130 semantics (i.e. the range of possible values at runtime that is covered
1131 by the others alternative.
1133 Another exception is if the subtype of the expression is non-static. In
1134 that case, we also have to use the base type. */
1135 if (Paren_Count (Expression (gnat_node)) != 0
1136 || !Is_OK_Static_Subtype (Underlying_Type
1137 (Etype (Expression (gnat_node)))))
1138 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
1140 /* We build a SWITCH_EXPR that contains the code with interspersed
1141 CASE_LABEL_EXPRs for each label. */
1143 push_stack (&gnu_switch_label_stack, NULL_TREE, create_artificial_label ());
1144 start_stmt_group ();
1145 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
1146 Present (gnat_when);
1147 gnat_when = Next_Non_Pragma (gnat_when))
1149 Node_Id gnat_choice;
1151 /* First compile all the different case choices for the current WHEN
1152 alternative. */
1153 for (gnat_choice = First (Discrete_Choices (gnat_when));
1154 Present (gnat_choice); gnat_choice = Next (gnat_choice))
1156 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
1158 switch (Nkind (gnat_choice))
1160 case N_Range:
1161 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
1162 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
1163 break;
1165 case N_Subtype_Indication:
1166 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
1167 (Constraint (gnat_choice))));
1168 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
1169 (Constraint (gnat_choice))));
1170 break;
1172 case N_Identifier:
1173 case N_Expanded_Name:
1174 /* This represents either a subtype range or a static value of
1175 some kind; Ekind says which. If a static value, fall through
1176 to the next case. */
1177 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
1179 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
1181 gnu_low = fold (TYPE_MIN_VALUE (gnu_type));
1182 gnu_high = fold (TYPE_MAX_VALUE (gnu_type));
1183 break;
1186 /* ... fall through ... */
1188 case N_Character_Literal:
1189 case N_Integer_Literal:
1190 gnu_low = gnat_to_gnu (gnat_choice);
1191 break;
1193 case N_Others_Choice:
1194 break;
1196 default:
1197 abort ();
1200 add_stmt_with_node (build3 (CASE_LABEL_EXPR, void_type_node,
1201 gnu_low, gnu_high,
1202 create_artificial_label ()),
1203 gnat_choice);
1206 /* Push a binding level here in case variables are declared since we want
1207 them to be local to this set of statements instead of the block
1208 containing the Case statement. */
1209 add_stmt (build_stmt_group (Statements (gnat_when), true));
1210 add_stmt (build1 (GOTO_EXPR, void_type_node,
1211 TREE_VALUE (gnu_switch_label_stack)));
1214 /* Now emit a definition of the label all the cases branched to. */
1215 add_stmt (build1 (LABEL_EXPR, void_type_node,
1216 TREE_VALUE (gnu_switch_label_stack)));
1217 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
1218 end_stmt_group (), NULL_TREE);
1219 pop_stack (&gnu_switch_label_stack);
1221 return gnu_result;
1224 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
1225 to a GCC tree, which is returned. */
1227 static tree
1228 Loop_Statement_to_gnu (Node_Id gnat_node)
1230 /* ??? It would be nice to use "build" here, but there's no build5. */
1231 tree gnu_loop_stmt = build_nt (LOOP_STMT, NULL_TREE, NULL_TREE,
1232 NULL_TREE, NULL_TREE, NULL_TREE);
1233 tree gnu_loop_var = NULL_TREE;
1234 Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
1235 tree gnu_cond_expr = NULL_TREE;
1236 tree gnu_result;
1238 TREE_TYPE (gnu_loop_stmt) = void_type_node;
1239 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
1240 LOOP_STMT_LABEL (gnu_loop_stmt) = create_artificial_label ();
1241 annotate_with_node (gnu_loop_stmt, gnat_node);
1243 /* Save the end label of this LOOP_STMT in a stack so that the corresponding
1244 N_Exit_Statement can find it. */
1245 push_stack (&gnu_loop_label_stack, NULL_TREE,
1246 LOOP_STMT_LABEL (gnu_loop_stmt));
1248 /* Set the condition that under which the loop should continue.
1249 For "LOOP .... END LOOP;" the condition is always true. */
1250 if (No (gnat_iter_scheme))
1252 /* The case "WHILE condition LOOP ..... END LOOP;" */
1253 else if (Present (Condition (gnat_iter_scheme)))
1254 LOOP_STMT_TOP_COND (gnu_loop_stmt)
1255 = gnat_to_gnu (Condition (gnat_iter_scheme));
1256 else
1258 /* We have an iteration scheme. */
1259 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
1260 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
1261 Entity_Id gnat_type = Etype (gnat_loop_var);
1262 tree gnu_type = get_unpadded_type (gnat_type);
1263 tree gnu_low = TYPE_MIN_VALUE (gnu_type);
1264 tree gnu_high = TYPE_MAX_VALUE (gnu_type);
1265 bool reversep = Reverse_Present (gnat_loop_spec);
1266 tree gnu_first = reversep ? gnu_high : gnu_low;
1267 tree gnu_last = reversep ? gnu_low : gnu_high;
1268 enum tree_code end_code = reversep ? GE_EXPR : LE_EXPR;
1269 tree gnu_base_type = get_base_type (gnu_type);
1270 tree gnu_limit = (reversep ? TYPE_MIN_VALUE (gnu_base_type)
1271 : TYPE_MAX_VALUE (gnu_base_type));
1273 /* We know the loop variable will not overflow if GNU_LAST is a constant
1274 and is not equal to GNU_LIMIT. If it might overflow, we have to move
1275 the limit test to the end of the loop. In that case, we have to test
1276 for an empty loop outside the loop. */
1277 if (TREE_CODE (gnu_last) != INTEGER_CST
1278 || TREE_CODE (gnu_limit) != INTEGER_CST
1279 || tree_int_cst_equal (gnu_last, gnu_limit))
1281 gnu_cond_expr
1282 = build3 (COND_EXPR, void_type_node,
1283 build_binary_op (LE_EXPR, integer_type_node,
1284 gnu_low, gnu_high),
1285 NULL_TREE, alloc_stmt_list ());
1286 annotate_with_node (gnu_cond_expr, gnat_loop_spec);
1289 /* Open a new nesting level that will surround the loop to declare the
1290 loop index variable. */
1291 start_stmt_group ();
1292 gnat_pushlevel ();
1294 /* Declare the loop index and set it to its initial value. */
1295 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
1296 if (DECL_BY_REF_P (gnu_loop_var))
1297 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
1299 /* The loop variable might be a padded type, so use `convert' to get a
1300 reference to the inner variable if so. */
1301 gnu_loop_var = convert (get_base_type (gnu_type), gnu_loop_var);
1303 /* Set either the top or bottom exit condition as appropriate depending
1304 on whether or not we know an overflow cannot occur. */
1305 if (gnu_cond_expr)
1306 LOOP_STMT_BOT_COND (gnu_loop_stmt)
1307 = build_binary_op (NE_EXPR, integer_type_node,
1308 gnu_loop_var, gnu_last);
1309 else
1310 LOOP_STMT_TOP_COND (gnu_loop_stmt)
1311 = build_binary_op (end_code, integer_type_node,
1312 gnu_loop_var, gnu_last);
1314 LOOP_STMT_UPDATE (gnu_loop_stmt)
1315 = build_binary_op (reversep ? PREDECREMENT_EXPR
1316 : PREINCREMENT_EXPR,
1317 TREE_TYPE (gnu_loop_var),
1318 gnu_loop_var,
1319 convert (TREE_TYPE (gnu_loop_var),
1320 integer_one_node));
1321 annotate_with_node (LOOP_STMT_UPDATE (gnu_loop_stmt),
1322 gnat_iter_scheme);
1325 /* If the loop was named, have the name point to this loop. In this case,
1326 the association is not a ..._DECL node, but the end label from this
1327 LOOP_STMT. */
1328 if (Present (Identifier (gnat_node)))
1329 save_gnu_tree (Entity (Identifier (gnat_node)),
1330 LOOP_STMT_LABEL (gnu_loop_stmt), true);
1332 /* Make the loop body into its own block, so any allocated storage will be
1333 released every iteration. This is needed for stack allocation. */
1334 LOOP_STMT_BODY (gnu_loop_stmt)
1335 = build_stmt_group (Statements (gnat_node), true);
1337 /* If we declared a variable, then we are in a statement group for that
1338 declaration. Add the LOOP_STMT to it and make that the "loop". */
1339 if (gnu_loop_var)
1341 add_stmt (gnu_loop_stmt);
1342 gnat_poplevel ();
1343 gnu_loop_stmt = end_stmt_group ();
1346 /* If we have an outer COND_EXPR, that's our result and this loop is its
1347 "true" statement. Otherwise, the result is the LOOP_STMT. */
1348 if (gnu_cond_expr)
1350 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
1351 gnu_result = gnu_cond_expr;
1352 recalculate_side_effects (gnu_cond_expr);
1354 else
1355 gnu_result = gnu_loop_stmt;
1357 pop_stack (&gnu_loop_label_stack);
1359 return gnu_result;
1362 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
1363 don't return anything. */
1365 static void
1366 Subprogram_Body_to_gnu (Node_Id gnat_node)
1368 /* Save debug output mode in case it is reset. */
1369 enum debug_info_type save_write_symbols = write_symbols;
1370 const struct gcc_debug_hooks *const save_debug_hooks = debug_hooks;
1371 /* Definining identifier of a parameter to the subprogram. */
1372 Entity_Id gnat_param;
1373 /* The defining identifier for the subprogram body. Note that if a
1374 specification has appeared before for this body, then the identifier
1375 occurring in that specification will also be a defining identifier and all
1376 the calls to this subprogram will point to that specification. */
1377 Entity_Id gnat_subprog_id
1378 = (Present (Corresponding_Spec (gnat_node))
1379 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
1380 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
1381 tree gnu_subprog_decl;
1382 /* The FUNCTION_TYPE node corresponding to the subprogram spec. */
1383 tree gnu_subprog_type;
1384 tree gnu_cico_list;
1385 tree gnu_result;
1387 /* If this is a generic object or if it has been eliminated,
1388 ignore it. */
1389 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
1390 || Ekind (gnat_subprog_id) == E_Generic_Function
1391 || Is_Eliminated (gnat_subprog_id))
1392 return;
1394 /* If debug information is suppressed for the subprogram, turn debug
1395 mode off for the duration of processing. */
1396 if (!Needs_Debug_Info (gnat_subprog_id))
1398 write_symbols = NO_DEBUG;
1399 debug_hooks = &do_nothing_debug_hooks;
1402 /* If this subprogram acts as its own spec, define it. Otherwise, just get
1403 the already-elaborated tree node. However, if this subprogram had its
1404 elaboration deferred, we will already have made a tree node for it. So
1405 treat it as not being defined in that case. Such a subprogram cannot
1406 have an address clause or a freeze node, so this test is safe, though it
1407 does disable some otherwise-useful error checking. */
1408 gnu_subprog_decl
1409 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
1410 Acts_As_Spec (gnat_node)
1411 && !present_gnu_tree (gnat_subprog_id));
1413 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
1415 /* Set the line number in the decl to correspond to that of the body so that
1416 the line number notes are written
1417 correctly. */
1418 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (gnu_subprog_decl));
1420 begin_subprog_body (gnu_subprog_decl);
1421 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
1423 /* If there are OUT parameters, we need to ensure that the return statement
1424 properly copies them out. We do this by making a new block and converting
1425 any inner return into a goto to a label at the end of the block. */
1426 push_stack (&gnu_return_label_stack, NULL_TREE,
1427 gnu_cico_list ? create_artificial_label () : NULL_TREE);
1429 /* Get a tree corresponding to the code for the subprogram. */
1430 start_stmt_group ();
1431 gnat_pushlevel ();
1433 /* See if there are any parameters for which we don't yet have GCC entities.
1434 These must be for OUT parameters for which we will be making VAR_DECL
1435 nodes here. Fill them in to TYPE_CI_CO_LIST, which must contain the empty
1436 entry as well. We can match up the entries because TYPE_CI_CO_LIST is in
1437 the order of the parameters. */
1438 for (gnat_param = First_Formal (gnat_subprog_id);
1439 Present (gnat_param);
1440 gnat_param = Next_Formal_With_Extras (gnat_param))
1441 if (!present_gnu_tree (gnat_param))
1443 /* Skip any entries that have been already filled in; they must
1444 correspond to IN OUT parameters. */
1445 for (; gnu_cico_list && TREE_VALUE (gnu_cico_list);
1446 gnu_cico_list = TREE_CHAIN (gnu_cico_list))
1449 /* Do any needed references for padded types. */
1450 TREE_VALUE (gnu_cico_list)
1451 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_list)),
1452 gnat_to_gnu_entity (gnat_param, NULL_TREE, 1));
1455 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
1457 /* Generate the code of the subprogram itself. A return statement will be
1458 present and any OUT parameters will be handled there. */
1459 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
1460 gnat_poplevel ();
1461 gnu_result = end_stmt_group ();
1463 /* If we made a special return label, we need to make a block that contains
1464 the definition of that label and the copying to the return value. That
1465 block first contains the function, then the label and copy statement. */
1466 if (TREE_VALUE (gnu_return_label_stack))
1468 tree gnu_retval;
1470 start_stmt_group ();
1471 gnat_pushlevel ();
1472 add_stmt (gnu_result);
1473 add_stmt (build1 (LABEL_EXPR, void_type_node,
1474 TREE_VALUE (gnu_return_label_stack)));
1476 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
1477 if (list_length (gnu_cico_list) == 1)
1478 gnu_retval = TREE_VALUE (gnu_cico_list);
1479 else
1480 gnu_retval = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
1481 gnu_cico_list);
1483 if (DECL_P (gnu_retval) && DECL_BY_REF_P (gnu_retval))
1484 gnu_retval = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_retval);
1486 add_stmt_with_node
1487 (build1 (RETURN_EXPR, void_type_node,
1488 build2 (MODIFY_EXPR, TREE_TYPE (gnu_retval),
1489 DECL_RESULT (current_function_decl), gnu_retval)),
1490 gnat_node);
1491 gnat_poplevel ();
1492 gnu_result = end_stmt_group ();
1495 pop_stack (&gnu_return_label_stack);
1497 /* Initialize the information node for the function and set the
1498 end location. */
1499 allocate_struct_function (current_function_decl);
1500 Sloc_to_locus
1501 ((Present (End_Label (Handled_Statement_Sequence (gnat_node)))
1502 ? Sloc (End_Label (Handled_Statement_Sequence (gnat_node)))
1503 : Sloc (gnat_node)),
1504 &cfun->function_end_locus);
1506 end_subprog_body (gnu_result);
1508 /* Disconnect the trees for parameters that we made variables for from the
1509 GNAT entities since these are unusable after we end the function. */
1510 for (gnat_param = First_Formal (gnat_subprog_id);
1511 Present (gnat_param);
1512 gnat_param = Next_Formal_With_Extras (gnat_param))
1513 if (TREE_CODE (get_gnu_tree (gnat_param)) == VAR_DECL)
1514 save_gnu_tree (gnat_param, NULL_TREE, false);
1516 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node)));
1517 write_symbols = save_write_symbols;
1518 debug_hooks = save_debug_hooks;
1521 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
1522 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
1523 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
1524 If GNU_TARGET is non-null, this must be a function call and the result
1525 of the call is to be placed into that object. */
1527 static tree
1528 call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target)
1530 tree gnu_result;
1531 /* The GCC node corresponding to the GNAT subprogram name. This can either
1532 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
1533 or an indirect reference expression (an INDIRECT_REF node) pointing to a
1534 subprogram. */
1535 tree gnu_subprog_node = gnat_to_gnu (Name (gnat_node));
1536 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
1537 tree gnu_subprog_type = TREE_TYPE (gnu_subprog_node);
1538 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE,
1539 gnu_subprog_node);
1540 Entity_Id gnat_formal;
1541 Node_Id gnat_actual;
1542 tree gnu_actual_list = NULL_TREE;
1543 tree gnu_name_list = NULL_TREE;
1544 tree gnu_before_list = NULL_TREE;
1545 tree gnu_after_list = NULL_TREE;
1546 tree gnu_subprog_call;
1548 switch (Nkind (Name (gnat_node)))
1550 case N_Identifier:
1551 case N_Operator_Symbol:
1552 case N_Expanded_Name:
1553 case N_Attribute_Reference:
1554 if (Is_Eliminated (Entity (Name (gnat_node))))
1555 Eliminate_Error_Msg (gnat_node, Entity (Name (gnat_node)));
1558 if (TREE_CODE (gnu_subprog_type) != FUNCTION_TYPE)
1559 abort ();
1561 /* If we are calling a stubbed function, make this into a raise of
1562 Program_Error. Elaborate all our args first. */
1563 if (TREE_CODE (gnu_subprog_node) == FUNCTION_DECL
1564 && DECL_STUBBED_P (gnu_subprog_node))
1566 for (gnat_actual = First_Actual (gnat_node);
1567 Present (gnat_actual);
1568 gnat_actual = Next_Actual (gnat_actual))
1569 add_stmt (gnat_to_gnu (gnat_actual));
1571 if (Nkind (gnat_node) == N_Function_Call && !gnu_target)
1573 *gnu_result_type_p = TREE_TYPE (gnu_subprog_type);
1574 return build1 (NULL_EXPR, *gnu_result_type_p,
1575 build_call_raise (PE_Stubbed_Subprogram_Called));
1577 else
1578 return build_call_raise (PE_Stubbed_Subprogram_Called);
1581 /* If we are calling by supplying a pointer to a target, set up that
1582 pointer as the first argument. Use GNU_TARGET if one was passed;
1583 otherwise, make a target by building a variable of the maximum size
1584 of the type. */
1585 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
1587 tree gnu_real_ret_type
1588 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
1590 if (!gnu_target)
1592 tree gnu_obj_type
1593 = maybe_pad_type (gnu_real_ret_type,
1594 max_size (TYPE_SIZE (gnu_real_ret_type), true),
1595 0, Etype (Name (gnat_node)), "PAD", false,
1596 false, false);
1598 gnu_target = create_tmp_var_raw (gnu_obj_type, "LR");
1599 gnat_pushdecl (gnu_target, gnat_node);
1602 gnu_actual_list
1603 = tree_cons (NULL_TREE,
1604 build_unary_op (ADDR_EXPR, NULL_TREE,
1605 unchecked_convert (gnu_real_ret_type,
1606 gnu_target,
1607 false)),
1608 NULL_TREE);
1612 /* The only way we can be making a call via an access type is if Name is an
1613 explicit dereference. In that case, get the list of formal args from the
1614 type the access type is pointing to. Otherwise, get the formals from
1615 entity being called. */
1616 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
1617 gnat_formal = First_Formal (Etype (Name (gnat_node)));
1618 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
1619 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
1620 gnat_formal = 0;
1621 else
1622 gnat_formal = First_Formal (Entity (Name (gnat_node)));
1624 /* Create the list of the actual parameters as GCC expects it, namely a chain
1625 of TREE_LIST nodes in which the TREE_VALUE field of each node is a
1626 parameter-expression and the TREE_PURPOSE field is null. Skip OUT
1627 parameters not passed by reference and don't need to be copied in. */
1628 for (gnat_actual = First_Actual (gnat_node);
1629 Present (gnat_actual);
1630 gnat_formal = Next_Formal_With_Extras (gnat_formal),
1631 gnat_actual = Next_Actual (gnat_actual))
1633 tree gnu_formal_type = gnat_to_gnu_type (Etype (gnat_formal));
1634 /* We treat a conversion between aggregate types as if it is an
1635 unchecked conversion. */
1636 bool unchecked_convert_p
1637 = (Nkind (gnat_actual) == N_Unchecked_Type_Conversion
1638 || (Nkind (gnat_actual) == N_Type_Conversion
1639 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))));
1640 Node_Id gnat_name = (unchecked_convert_p
1641 ? Expression (gnat_actual) : gnat_actual);
1642 tree gnu_name = gnat_to_gnu (gnat_name);
1643 tree gnu_name_type = gnat_to_gnu_type (Etype (gnat_name));
1644 tree gnu_formal
1645 = (present_gnu_tree (gnat_formal)
1646 ? get_gnu_tree (gnat_formal) : NULL_TREE);
1647 tree gnu_actual;
1649 /* If it's possible we may need to use this expression twice, make sure
1650 than any side-effects are handled via SAVE_EXPRs. Likewise if we need
1651 to force side-effects before the call.
1653 ??? This is more conservative than we need since we don't need to do
1654 this for pass-by-ref with no conversion. If we are passing a
1655 non-addressable Out or In Out parameter by reference, pass the address
1656 of a copy and set up to copy back out after the call. */
1657 if (Ekind (gnat_formal) != E_In_Parameter)
1659 gnu_name = gnat_stabilize_reference (gnu_name, true);
1660 if (!addressable_p (gnu_name)
1661 && gnu_formal
1662 && (DECL_BY_REF_P (gnu_formal)
1663 || (TREE_CODE (gnu_formal) == PARM_DECL
1664 && (DECL_BY_COMPONENT_PTR_P (gnu_formal)
1665 || (DECL_BY_DESCRIPTOR_P (gnu_formal))))))
1667 tree gnu_copy = gnu_name;
1668 tree gnu_temp;
1670 /* Remove any unpadding on the actual and make a copy. But if
1671 the actual is a left-justified modular type, first convert
1672 to it. */
1673 if (TREE_CODE (gnu_name) == COMPONENT_REF
1674 && ((TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))
1675 == RECORD_TYPE)
1676 && (TYPE_IS_PADDING_P
1677 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))))
1678 gnu_name = gnu_copy = TREE_OPERAND (gnu_name, 0);
1679 else if (TREE_CODE (gnu_name_type) == RECORD_TYPE
1680 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_name_type)))
1681 gnu_name = convert (gnu_name_type, gnu_name);
1683 gnu_actual = save_expr (gnu_name);
1685 /* Since we're going to take the address of the SAVE_EXPR, we
1686 don't want it to be marked as unchanging. So set
1687 TREE_ADDRESSABLE. */
1688 gnu_temp = skip_simple_arithmetic (gnu_actual);
1689 if (TREE_CODE (gnu_temp) == SAVE_EXPR)
1691 TREE_ADDRESSABLE (gnu_temp) = 1;
1692 TREE_READONLY (gnu_temp) = 0;
1695 /* Set up to move the copy back to the original. */
1696 gnu_temp = build2 (MODIFY_EXPR, TREE_TYPE (gnu_copy),
1697 gnu_copy, gnu_actual);
1698 annotate_with_node (gnu_temp, gnat_actual);
1699 append_to_statement_list (gnu_temp, &gnu_after_list);
1703 /* If this was a procedure call, we may not have removed any padding.
1704 So do it here for the part we will use as an input, if any. */
1705 gnu_actual = gnu_name;
1706 if (Ekind (gnat_formal) != E_Out_Parameter
1707 && TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
1708 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
1709 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
1710 gnu_actual);
1712 /* Unless this is an In parameter, we must remove any LJM building
1713 from GNU_NAME. */
1714 if (Ekind (gnat_formal) != E_In_Parameter
1715 && TREE_CODE (gnu_name) == CONSTRUCTOR
1716 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
1717 && TYPE_LEFT_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
1718 gnu_name = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))),
1719 gnu_name);
1721 if (Ekind (gnat_formal) != E_Out_Parameter
1722 && !unchecked_convert_p
1723 && Do_Range_Check (gnat_actual))
1724 gnu_actual = emit_range_check (gnu_actual, Etype (gnat_formal));
1726 /* Do any needed conversions. We need only check for unchecked
1727 conversion since normal conversions will be handled by just
1728 converting to the formal type. */
1729 if (unchecked_convert_p)
1731 gnu_actual
1732 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
1733 gnu_actual,
1734 (Nkind (gnat_actual)
1735 == N_Unchecked_Type_Conversion)
1736 && No_Truncation (gnat_actual));
1738 /* One we've done the unchecked conversion, we still must ensure that
1739 the object is in range of the formal's type. */
1740 if (Ekind (gnat_formal) != E_Out_Parameter
1741 && Do_Range_Check (gnat_actual))
1742 gnu_actual = emit_range_check (gnu_actual,
1743 Etype (gnat_formal));
1745 else if (TREE_CODE (gnu_actual) != SAVE_EXPR)
1746 /* We may have suppressed a conversion to the Etype of the actual since
1747 the parent is a procedure call. So add the conversion here. */
1748 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
1749 gnu_actual);
1751 if (TREE_CODE (gnu_actual) != SAVE_EXPR)
1752 gnu_actual = convert (gnu_formal_type, gnu_actual);
1754 /* If we have not saved a GCC object for the formal, it means it is an
1755 OUT parameter not passed by reference and that does not need to be
1756 copied in. Otherwise, look at the PARM_DECL to see if it is passed by
1757 reference. */
1758 if (gnu_formal
1759 && TREE_CODE (gnu_formal) == PARM_DECL && DECL_BY_REF_P (gnu_formal))
1761 if (Ekind (gnat_formal) != E_In_Parameter)
1763 gnu_actual = gnu_name;
1765 /* If we have a padded type, be sure we've removed padding. */
1766 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
1767 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))
1768 && TREE_CODE (gnu_actual) != SAVE_EXPR)
1769 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
1770 gnu_actual);
1773 /* Otherwise, if we have a non-addressable COMPONENT_REF of a
1774 variable-size type see if it's doing a unpadding operation. If
1775 so, remove that operation since we have no way of allocating the
1776 required temporary. */
1777 if (TREE_CODE (gnu_actual) == COMPONENT_REF
1778 && !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
1779 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_actual, 0)))
1780 == RECORD_TYPE)
1781 && TYPE_IS_PADDING_P (TREE_TYPE
1782 (TREE_OPERAND (gnu_actual, 0)))
1783 && !addressable_p (gnu_actual))
1784 gnu_actual = TREE_OPERAND (gnu_actual, 0);
1786 /* The symmetry of the paths to the type of an entity is broken here
1787 since arguments don't know that they will be passed by ref. */
1788 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
1789 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
1791 else if (gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL
1792 && DECL_BY_COMPONENT_PTR_P (gnu_formal))
1794 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
1795 gnu_actual = maybe_implicit_deref (gnu_actual);
1796 gnu_actual = maybe_unconstrained_array (gnu_actual);
1798 if (TREE_CODE (gnu_formal_type) == RECORD_TYPE
1799 && TYPE_IS_PADDING_P (gnu_formal_type))
1801 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
1802 gnu_actual = convert (gnu_formal_type, gnu_actual);
1805 /* Take the address of the object and convert to the proper pointer
1806 type. We'd like to actually compute the address of the beginning
1807 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
1808 possibility that the ARRAY_REF might return a constant and we'd be
1809 getting the wrong address. Neither approach is exactly correct,
1810 but this is the most likely to work in all cases. */
1811 gnu_actual = convert (gnu_formal_type,
1812 build_unary_op (ADDR_EXPR, NULL_TREE,
1813 gnu_actual));
1815 else if (gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL
1816 && DECL_BY_DESCRIPTOR_P (gnu_formal))
1818 /* If arg is 'Null_Parameter, pass zero descriptor. */
1819 if ((TREE_CODE (gnu_actual) == INDIRECT_REF
1820 || TREE_CODE (gnu_actual) == UNCONSTRAINED_ARRAY_REF)
1821 && TREE_PRIVATE (gnu_actual))
1822 gnu_actual = convert (DECL_ARG_TYPE (get_gnu_tree (gnat_formal)),
1823 integer_zero_node);
1824 else
1825 gnu_actual = build_unary_op (ADDR_EXPR, NULL_TREE,
1826 fill_vms_descriptor (gnu_actual,
1827 gnat_formal));
1829 else
1831 tree gnu_actual_size = TYPE_SIZE (TREE_TYPE (gnu_actual));
1833 if (Ekind (gnat_formal) != E_In_Parameter)
1834 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
1836 if (!gnu_formal || TREE_CODE (gnu_formal) != PARM_DECL)
1837 continue;
1839 /* If this is 'Null_Parameter, pass a zero even though we are
1840 dereferencing it. */
1841 else if (TREE_CODE (gnu_actual) == INDIRECT_REF
1842 && TREE_PRIVATE (gnu_actual)
1843 && host_integerp (gnu_actual_size, 1)
1844 && 0 >= compare_tree_int (gnu_actual_size,
1845 BITS_PER_WORD))
1846 gnu_actual
1847 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
1848 convert (gnat_type_for_size
1849 (tree_low_cst (gnu_actual_size, 1),
1851 integer_zero_node),
1852 false);
1853 else
1854 gnu_actual
1855 = convert (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (gnu_formal)),
1856 gnu_actual);
1859 gnu_actual_list = tree_cons (NULL_TREE, gnu_actual, gnu_actual_list);
1862 gnu_subprog_call = build3 (CALL_EXPR, TREE_TYPE (gnu_subprog_type),
1863 gnu_subprog_addr, nreverse (gnu_actual_list),
1864 NULL_TREE);
1866 /* If we return by passing a target, we emit the call and return the target
1867 as our result. */
1868 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
1870 add_stmt_with_node (gnu_subprog_call, gnat_node);
1871 *gnu_result_type_p
1872 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
1873 return unchecked_convert (*gnu_result_type_p, gnu_target, false);
1876 /* If it is a function call, the result is the call expression unless
1877 a target is specified, in which case we copy the result into the target
1878 and return the assignment statement. */
1879 else if (Nkind (gnat_node) == N_Function_Call)
1881 gnu_result = gnu_subprog_call;
1883 /* If the function returns an unconstrained array or by reference,
1884 we have to de-dereference the pointer. */
1885 if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type)
1886 || TYPE_RETURNS_BY_REF_P (gnu_subprog_type))
1887 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
1889 if (gnu_target)
1890 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
1891 gnu_target, gnu_result);
1892 else
1893 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
1895 return gnu_result;
1898 /* If this is the case where the GNAT tree contains a procedure call
1899 but the Ada procedure has copy in copy out parameters, the special
1900 parameter passing mechanism must be used. */
1901 else if (TYPE_CI_CO_LIST (gnu_subprog_type) != NULL_TREE)
1903 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy
1904 in copy out parameters. */
1905 tree scalar_return_list = TYPE_CI_CO_LIST (gnu_subprog_type);
1906 int length = list_length (scalar_return_list);
1908 if (length > 1)
1910 tree gnu_name;
1912 gnu_subprog_call = save_expr (gnu_subprog_call);
1913 gnu_name_list = nreverse (gnu_name_list);
1915 /* If any of the names had side-effects, ensure they are all
1916 evaluated before the call. */
1917 for (gnu_name = gnu_name_list; gnu_name;
1918 gnu_name = TREE_CHAIN (gnu_name))
1919 if (TREE_SIDE_EFFECTS (TREE_VALUE (gnu_name)))
1920 append_to_statement_list (TREE_VALUE (gnu_name),
1921 &gnu_before_list);
1924 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
1925 gnat_formal = First_Formal (Etype (Name (gnat_node)));
1926 else
1927 gnat_formal = First_Formal (Entity (Name (gnat_node)));
1929 for (gnat_actual = First_Actual (gnat_node);
1930 Present (gnat_actual);
1931 gnat_formal = Next_Formal_With_Extras (gnat_formal),
1932 gnat_actual = Next_Actual (gnat_actual))
1933 /* If we are dealing with a copy in copy out parameter, we must
1934 retrieve its value from the record returned in the call. */
1935 if (!(present_gnu_tree (gnat_formal)
1936 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
1937 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
1938 || (TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
1939 && ((DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))
1940 || (DECL_BY_DESCRIPTOR_P
1941 (get_gnu_tree (gnat_formal))))))))
1942 && Ekind (gnat_formal) != E_In_Parameter)
1944 /* Get the value to assign to this OUT or IN OUT parameter. It is
1945 either the result of the function if there is only a single such
1946 parameter or the appropriate field from the record returned. */
1947 tree gnu_result
1948 = length == 1 ? gnu_subprog_call
1949 : build_component_ref (gnu_subprog_call, NULL_TREE,
1950 TREE_PURPOSE (scalar_return_list),
1951 false);
1952 bool unchecked_conversion = (Nkind (gnat_actual)
1953 == N_Unchecked_Type_Conversion);
1954 /* If the actual is a conversion, get the inner expression, which
1955 will be the real destination, and convert the result to the
1956 type of the actual parameter. */
1957 tree gnu_actual
1958 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
1960 /* If the result is a padded type, remove the padding. */
1961 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
1962 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
1963 gnu_result = convert (TREE_TYPE (TYPE_FIELDS
1964 (TREE_TYPE (gnu_result))),
1965 gnu_result);
1967 /* If the result is a type conversion, do it. */
1968 if (Nkind (gnat_actual) == N_Type_Conversion)
1969 gnu_result
1970 = convert_with_check
1971 (Etype (Expression (gnat_actual)), gnu_result,
1972 Do_Overflow_Check (gnat_actual),
1973 Do_Range_Check (Expression (gnat_actual)),
1974 Float_Truncate (gnat_actual));
1976 else if (unchecked_conversion)
1977 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
1978 gnu_result,
1979 No_Truncation (gnat_actual));
1980 else
1982 if (Do_Range_Check (gnat_actual))
1983 gnu_result = emit_range_check (gnu_result,
1984 Etype (gnat_actual));
1986 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
1987 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
1988 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
1991 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
1992 gnu_actual, gnu_result);
1993 annotate_with_node (gnu_result, gnat_actual);
1994 append_to_statement_list (gnu_result, &gnu_before_list);
1995 scalar_return_list = TREE_CHAIN (scalar_return_list);
1996 gnu_name_list = TREE_CHAIN (gnu_name_list);
1999 else
2001 annotate_with_node (gnu_subprog_call, gnat_node);
2002 append_to_statement_list (gnu_subprog_call, &gnu_before_list);
2005 append_to_statement_list (gnu_after_list, &gnu_before_list);
2006 return gnu_before_list;
2009 /* Subroutine of gnat_to_gnu to translate gnat_node, an
2010 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
2012 static tree
2013 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
2015 tree gnu_jmpsave_decl = NULL_TREE;
2016 tree gnu_jmpbuf_decl = NULL_TREE;
2017 /* If just annotating, ignore all EH and cleanups. */
2018 bool gcc_zcx = (!type_annotate_only
2019 && Present (Exception_Handlers (gnat_node))
2020 && Exception_Mechanism == GCC_ZCX);
2021 bool setjmp_longjmp
2022 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
2023 && Exception_Mechanism == Setjmp_Longjmp);
2024 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
2025 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
2026 tree gnu_inner_block; /* The statement(s) for the block itself. */
2027 tree gnu_result;
2028 tree gnu_expr;
2029 Node_Id gnat_temp;
2031 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
2032 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
2033 add_cleanup, and when we leave the binding, end_stmt_group will create
2034 the TRY_FINALLY_EXPR.
2036 ??? The region level calls down there have been specifically put in place
2037 for a ZCX context and currently the order in which things are emitted
2038 (region/handlers) is different from the SJLJ case. Instead of putting
2039 other calls with different conditions at other places for the SJLJ case,
2040 it seems cleaner to reorder things for the SJLJ case and generalize the
2041 condition to make it not ZCX specific.
2043 If there are any exceptions or cleanup processing involved, we need an
2044 outer statement group (for Setjmp_Longjmp) and binding level. */
2045 if (binding_for_block)
2047 start_stmt_group ();
2048 gnat_pushlevel ();
2051 /* If we are to call a function when exiting this block add a cleanup
2052 to the binding level we made above. */
2053 if (at_end)
2054 add_cleanup (build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node))));
2056 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
2057 area for address of previous buffer. Do this first since we need to have
2058 the setjmp buf known for any decls in this block. */
2059 if (setjmp_longjmp)
2061 gnu_jmpsave_decl = create_var_decl (get_identifier ("JMPBUF_SAVE"),
2062 NULL_TREE, jmpbuf_ptr_type,
2063 build_call_0_expr (get_jmpbuf_decl),
2064 false, false, false, false, NULL,
2065 gnat_node);
2066 gnu_jmpbuf_decl = create_var_decl (get_identifier ("JMP_BUF"),
2067 NULL_TREE, jmpbuf_type,
2068 NULL_TREE, false, false, false, false,
2069 NULL, gnat_node);
2071 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
2073 /* When we exit this block, restore the saved value. */
2074 add_cleanup (build_call_1_expr (set_jmpbuf_decl, gnu_jmpsave_decl));
2077 /* Now build the tree for the declarations and statements inside this block.
2078 If this is SJLJ, set our jmp_buf as the current buffer. */
2079 start_stmt_group ();
2081 if (setjmp_longjmp)
2082 add_stmt (build_call_1_expr (set_jmpbuf_decl,
2083 build_unary_op (ADDR_EXPR, NULL_TREE,
2084 gnu_jmpbuf_decl)));
2087 if (Present (First_Real_Statement (gnat_node)))
2088 process_decls (Statements (gnat_node), Empty,
2089 First_Real_Statement (gnat_node), true, true);
2091 /* Generate code for each statement in the block. */
2092 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
2093 ? First_Real_Statement (gnat_node)
2094 : First (Statements (gnat_node)));
2095 Present (gnat_temp); gnat_temp = Next (gnat_temp))
2096 add_stmt (gnat_to_gnu (gnat_temp));
2097 gnu_inner_block = end_stmt_group ();
2099 /* Now generate code for the two exception models, if either is relevant for
2100 this block. */
2101 if (setjmp_longjmp)
2103 tree *gnu_else_ptr = 0;
2104 tree gnu_handler;
2106 /* Make a binding level for the exception handling declarations and code
2107 and set up gnu_except_ptr_stack for the handlers to use. */
2108 start_stmt_group ();
2109 gnat_pushlevel ();
2111 push_stack (&gnu_except_ptr_stack, NULL_TREE,
2112 create_var_decl (get_identifier ("EXCEPT_PTR"),
2113 NULL_TREE,
2114 build_pointer_type (except_type_node),
2115 build_call_0_expr (get_excptr_decl), false,
2116 false, false, false, NULL, gnat_node));
2118 /* Generate code for each handler. The N_Exception_Handler case does the
2119 real work and returns a COND_EXPR for each handler, which we chain
2120 together here. */
2121 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
2122 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
2124 gnu_expr = gnat_to_gnu (gnat_temp);
2126 /* If this is the first one, set it as the outer one. Otherwise,
2127 point the "else" part of the previous handler to us. Then point
2128 to our "else" part. */
2129 if (!gnu_else_ptr)
2130 add_stmt (gnu_expr);
2131 else
2132 *gnu_else_ptr = gnu_expr;
2134 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
2137 /* If none of the exception handlers did anything, re-raise but do not
2138 defer abortion. */
2139 gnu_expr = build_call_1_expr (raise_nodefer_decl,
2140 TREE_VALUE (gnu_except_ptr_stack));
2141 annotate_with_node (gnu_expr, gnat_node);
2143 if (gnu_else_ptr)
2144 *gnu_else_ptr = gnu_expr;
2145 else
2146 add_stmt (gnu_expr);
2148 /* End the binding level dedicated to the exception handlers and get the
2149 whole statement group. */
2150 pop_stack (&gnu_except_ptr_stack);
2151 gnat_poplevel ();
2152 gnu_handler = end_stmt_group ();
2154 /* If the setjmp returns 1, we restore our incoming longjmp value and
2155 then check the handlers. */
2156 start_stmt_group ();
2157 add_stmt_with_node (build_call_1_expr (set_jmpbuf_decl,
2158 gnu_jmpsave_decl),
2159 gnat_node);
2160 add_stmt (gnu_handler);
2161 gnu_handler = end_stmt_group ();
2163 /* This block is now "if (setjmp) ... <handlers> else <block>". */
2164 gnu_result = build3 (COND_EXPR, void_type_node,
2165 (build_call_1_expr
2166 (setjmp_decl,
2167 build_unary_op (ADDR_EXPR, NULL_TREE,
2168 gnu_jmpbuf_decl))),
2169 gnu_handler, gnu_inner_block);
2171 else if (gcc_zcx)
2173 tree gnu_handlers;
2175 /* First make a block containing the handlers. */
2176 start_stmt_group ();
2177 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
2178 Present (gnat_temp);
2179 gnat_temp = Next_Non_Pragma (gnat_temp))
2180 add_stmt (gnat_to_gnu (gnat_temp));
2181 gnu_handlers = end_stmt_group ();
2183 /* Now make the TRY_CATCH_EXPR for the block. */
2184 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
2185 gnu_inner_block, gnu_handlers);
2187 else
2188 gnu_result = gnu_inner_block;
2190 /* Now close our outer block, if we had to make one. */
2191 if (binding_for_block)
2193 add_stmt (gnu_result);
2194 gnat_poplevel ();
2195 gnu_result = end_stmt_group ();
2198 return gnu_result;
2201 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
2202 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
2203 exception handling. */
2205 static tree
2206 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
2208 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
2209 an "if" statement to select the proper exceptions. For "Others", exclude
2210 exceptions where Handled_By_Others is nonzero unless the All_Others flag
2211 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
2212 tree gnu_choice = integer_zero_node;
2213 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
2214 Node_Id gnat_temp;
2216 for (gnat_temp = First (Exception_Choices (gnat_node));
2217 gnat_temp; gnat_temp = Next (gnat_temp))
2219 tree this_choice;
2221 if (Nkind (gnat_temp) == N_Others_Choice)
2223 if (All_Others (gnat_temp))
2224 this_choice = integer_one_node;
2225 else
2226 this_choice
2227 = build_binary_op
2228 (EQ_EXPR, integer_type_node,
2229 convert
2230 (integer_type_node,
2231 build_component_ref
2232 (build_unary_op
2233 (INDIRECT_REF, NULL_TREE,
2234 TREE_VALUE (gnu_except_ptr_stack)),
2235 get_identifier ("not_handled_by_others"), NULL_TREE,
2236 false)),
2237 integer_zero_node);
2240 else if (Nkind (gnat_temp) == N_Identifier
2241 || Nkind (gnat_temp) == N_Expanded_Name)
2243 tree gnu_expr
2244 = gnat_to_gnu_entity (Entity (gnat_temp), NULL_TREE, 0);
2246 this_choice
2247 = build_binary_op
2248 (EQ_EXPR, integer_type_node, TREE_VALUE (gnu_except_ptr_stack),
2249 convert (TREE_TYPE (TREE_VALUE (gnu_except_ptr_stack)),
2250 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
2252 /* If this is the distinguished exception "Non_Ada_Error" (and we are
2253 in VMS mode), also allow a non-Ada exception (a VMS condition) t
2254 match. */
2255 if (Is_Non_Ada_Error (Entity (gnat_temp)))
2257 tree gnu_comp
2258 = build_component_ref
2259 (build_unary_op (INDIRECT_REF, NULL_TREE,
2260 TREE_VALUE (gnu_except_ptr_stack)),
2261 get_identifier ("lang"), NULL_TREE, false);
2263 this_choice
2264 = build_binary_op
2265 (TRUTH_ORIF_EXPR, integer_type_node,
2266 build_binary_op (EQ_EXPR, integer_type_node, gnu_comp,
2267 build_int_cst (TREE_TYPE (gnu_comp), 'V')),
2268 this_choice);
2271 else
2272 abort ();
2274 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
2275 gnu_choice, this_choice);
2278 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
2281 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
2282 to a GCC tree, which is returned. This is the variant for ZCX. */
2284 static tree
2285 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
2287 tree gnu_etypes_list = NULL_TREE;
2288 tree gnu_expr;
2289 tree gnu_etype;
2290 tree gnu_current_exc_ptr;
2291 tree gnu_incoming_exc_ptr;
2292 Node_Id gnat_temp;
2294 /* We build a TREE_LIST of nodes representing what exception types this
2295 handler can catch, with special cases for others and all others cases.
2297 Each exception type is actually identified by a pointer to the exception
2298 id, with special value zero for "others" and one for "all others". Beware
2299 that these special values are known and used by the personality routine to
2300 identify the corresponding specific kinds of handlers.
2302 ??? For initial time frame reasons, the others and all_others cases have
2303 been handled using specific type trees, but this somehow hides information
2304 from the back-end, which expects NULL to be passed for catch all and
2305 end_cleanup to be used for cleanups.
2307 Care should be taken to ensure that the control flow impact of such
2308 clauses is rendered in some way. lang_eh_type_covers is doing the trick
2309 currently. */
2310 for (gnat_temp = First (Exception_Choices (gnat_node));
2311 gnat_temp; gnat_temp = Next (gnat_temp))
2313 if (Nkind (gnat_temp) == N_Others_Choice)
2314 gnu_etype = (All_Others (gnat_temp) ? integer_one_node
2315 : integer_zero_node);
2316 else if (Nkind (gnat_temp) == N_Identifier
2317 || Nkind (gnat_temp) == N_Expanded_Name)
2319 Entity_Id gnat_ex_id = Entity (gnat_temp);
2321 /* Exception may be a renaming. Recover original exception which is
2322 the one elaborated and registered. */
2323 if (Present (Renamed_Object (gnat_ex_id)))
2324 gnat_ex_id = Renamed_Object (gnat_ex_id);
2326 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
2327 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
2329 /* The Non_Ada_Error case for VMS exceptions is handled
2330 by the personality routine. */
2332 else
2333 abort ();
2335 /* The GCC interface expects NULL to be passed for catch all handlers, so
2336 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
2337 is integer_zero_node. It would not work, however, because GCC's
2338 notion of "catch all" is stronger than our notion of "others". Until
2339 we correctly use the cleanup interface as well, doing that would
2340 prevent the "all others" handlers from beeing seen, because nothing
2341 can be caught beyond a catch all from GCC's point of view. */
2342 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
2345 start_stmt_group ();
2346 gnat_pushlevel ();
2348 /* Expand a call to the begin_handler hook at the beginning of the handler,
2349 and arrange for a call to the end_handler hook to occur on every possible
2350 exit path.
2352 The hooks expect a pointer to the low level occurrence. This is required
2353 for our stack management scheme because a raise inside the handler pushes
2354 a new occurrence on top of the stack, which means that this top does not
2355 necessarily match the occurrence this handler was dealing with.
2357 The EXC_PTR_EXPR object references the exception occurrence being
2358 propagated. Upon handler entry, this is the exception for which the
2359 handler is triggered. This might not be the case upon handler exit,
2360 however, as we might have a new occurrence propagated by the handler's
2361 body, and the end_handler hook called as a cleanup in this context.
2363 We use a local variable to retrieve the incoming value at handler entry
2364 time, and reuse it to feed the end_handler hook's argument at exit. */
2365 gnu_current_exc_ptr = build0 (EXC_PTR_EXPR, ptr_type_node);
2366 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
2367 ptr_type_node, gnu_current_exc_ptr,
2368 false, false, false, false, NULL,
2369 gnat_node);
2371 add_stmt_with_node (build_call_1_expr (begin_handler_decl,
2372 gnu_incoming_exc_ptr),
2373 gnat_node);
2374 add_cleanup (build_call_1_expr (end_handler_decl, gnu_incoming_exc_ptr));
2375 add_stmt_list (Statements (gnat_node));
2376 gnat_poplevel ();
2378 return build2 (CATCH_EXPR, void_type_node, gnu_etypes_list,
2379 end_stmt_group ());
2382 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
2384 static void
2385 Compilation_Unit_to_gnu (Node_Id gnat_node)
2387 /* Make the decl for the elaboration procedure. */
2388 bool body_p = (Defining_Entity (Unit (gnat_node)),
2389 Nkind (Unit (gnat_node)) == N_Package_Body
2390 || Nkind (Unit (gnat_node)) == N_Subprogram_Body);
2391 Entity_Id gnat_unit_entity = Defining_Entity (Unit (gnat_node));
2392 tree gnu_elab_proc_decl
2393 = create_subprog_decl
2394 (create_concat_name (gnat_unit_entity,
2395 body_p ? "elabb" : "elabs"),
2396 NULL_TREE, void_ftype, NULL_TREE, false, true, false, NULL,
2397 gnat_unit_entity);
2398 struct elab_info *info;
2400 push_stack (&gnu_elab_proc_stack, NULL_TREE, gnu_elab_proc_decl);
2402 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
2403 allocate_struct_function (gnu_elab_proc_decl);
2404 Sloc_to_locus (Sloc (gnat_unit_entity), &cfun->function_end_locus);
2405 cfun = 0;
2407 /* For a body, first process the spec if there is one. */
2408 if (Nkind (Unit (gnat_node)) == N_Package_Body
2409 || (Nkind (Unit (gnat_node)) == N_Subprogram_Body
2410 && !Acts_As_Spec (gnat_node)))
2411 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
2413 process_inlined_subprograms (gnat_node);
2415 if (type_annotate_only)
2417 elaborate_all_entities (gnat_node);
2419 if (Nkind (Unit (gnat_node)) == N_Subprogram_Declaration
2420 || Nkind (Unit (gnat_node)) == N_Generic_Package_Declaration
2421 || Nkind (Unit (gnat_node)) == N_Generic_Subprogram_Declaration)
2422 return;
2425 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
2426 true, true);
2427 add_stmt (gnat_to_gnu (Unit (gnat_node)));
2429 /* Process any pragmas and actions following the unit. */
2430 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
2431 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
2433 /* Save away what we've made so far and record this potential elaboration
2434 procedure. */
2435 info = (struct elab_info *) ggc_alloc (sizeof (struct elab_info));
2436 set_current_block_context (gnu_elab_proc_decl);
2437 gnat_poplevel ();
2438 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
2439 info->next = elab_info_list;
2440 info->elab_proc = gnu_elab_proc_decl;
2441 info->gnat_node = gnat_node;
2442 elab_info_list = info;
2444 /* Generate elaboration code for this unit, if necessary, and say whether
2445 we did or not. */
2446 pop_stack (&gnu_elab_proc_stack);
2449 /* This function is the driver of the GNAT to GCC tree transformation
2450 process. It is the entry point of the tree transformer. GNAT_NODE is the
2451 root of some GNAT tree. Return the root of the corresponding GCC tree.
2452 If this is an expression, return the GCC equivalent of the expression. If
2453 it is a statement, return the statement. In the case when called for a
2454 statement, it may also add statements to the current statement group, in
2455 which case anything it returns is to be interpreted as occuring after
2456 anything `it already added. */
2458 tree
2459 gnat_to_gnu (Node_Id gnat_node)
2461 bool went_into_elab_proc = false;
2462 tree gnu_result = error_mark_node; /* Default to no value. */
2463 tree gnu_result_type = void_type_node;
2464 tree gnu_expr;
2465 tree gnu_lhs, gnu_rhs;
2466 Node_Id gnat_temp;
2468 /* Save node number for error message and set location information. */
2469 error_gnat_node = gnat_node;
2470 Sloc_to_locus (Sloc (gnat_node), &input_location);
2472 if (type_annotate_only
2473 && IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call))
2474 return alloc_stmt_list ();
2476 /* If this node is a non-static subexpression and we are only
2477 annotating types, make this into a NULL_EXPR. */
2478 if (type_annotate_only
2479 && IN (Nkind (gnat_node), N_Subexpr)
2480 && Nkind (gnat_node) != N_Identifier
2481 && !Compile_Time_Known_Value (gnat_node))
2482 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
2483 build_call_raise (CE_Range_Check_Failed));
2485 /* If this is a Statement and we are at top level, it must be part of
2486 the elaboration procedure, so mark us as being in that procedure
2487 and push our context. */
2488 if (!current_function_decl
2489 && ((IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call)
2490 && Nkind (gnat_node) != N_Null_Statement)
2491 || Nkind (gnat_node) == N_Procedure_Call_Statement
2492 || Nkind (gnat_node) == N_Label
2493 || Nkind (gnat_node) == N_Implicit_Label_Declaration
2494 || Nkind (gnat_node) == N_Handled_Sequence_Of_Statements
2495 || ((Nkind (gnat_node) == N_Raise_Constraint_Error
2496 || Nkind (gnat_node) == N_Raise_Storage_Error
2497 || Nkind (gnat_node) == N_Raise_Program_Error)
2498 && (Ekind (Etype (gnat_node)) == E_Void))))
2500 current_function_decl = TREE_VALUE (gnu_elab_proc_stack);
2501 start_stmt_group ();
2502 gnat_pushlevel ();
2503 went_into_elab_proc = true;
2506 switch (Nkind (gnat_node))
2508 /********************************/
2509 /* Chapter 2: Lexical Elements: */
2510 /********************************/
2512 case N_Identifier:
2513 case N_Expanded_Name:
2514 case N_Operator_Symbol:
2515 case N_Defining_Identifier:
2516 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
2517 break;
2519 case N_Integer_Literal:
2521 tree gnu_type;
2523 /* Get the type of the result, looking inside any padding and
2524 left-justified modular types. Then get the value in that type. */
2525 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
2527 if (TREE_CODE (gnu_type) == RECORD_TYPE
2528 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2529 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2531 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
2533 /* If the result overflows (meaning it doesn't fit in its base type),
2534 abort. We would like to check that the value is within the range
2535 of the subtype, but that causes problems with subtypes whose usage
2536 will raise Constraint_Error and with biased representation, so
2537 we don't. */
2538 if (TREE_CONSTANT_OVERFLOW (gnu_result))
2539 abort ();
2541 break;
2543 case N_Character_Literal:
2544 /* If a Entity is present, it means that this was one of the
2545 literals in a user-defined character type. In that case,
2546 just return the value in the CONST_DECL. Otherwise, use the
2547 character code. In that case, the base type should be an
2548 INTEGER_TYPE, but we won't bother checking for that. */
2549 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2550 if (Present (Entity (gnat_node)))
2551 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
2552 else
2553 gnu_result
2554 = force_fit_type
2555 (build_int_cst (gnu_result_type, Char_Literal_Value (gnat_node)),
2556 false, false, false);
2557 break;
2559 case N_Real_Literal:
2560 /* If this is of a fixed-point type, the value we want is the
2561 value of the corresponding integer. */
2562 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
2564 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2565 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
2566 gnu_result_type);
2567 if (TREE_CONSTANT_OVERFLOW (gnu_result))
2568 abort ();
2571 /* We should never see a Vax_Float type literal, since the front end
2572 is supposed to transform these using appropriate conversions */
2573 else if (Vax_Float (Underlying_Type (Etype (gnat_node))))
2574 abort ();
2576 else
2578 Ureal ur_realval = Realval (gnat_node);
2580 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2582 /* If the real value is zero, so is the result. Otherwise,
2583 convert it to a machine number if it isn't already. That
2584 forces BASE to 0 or 2 and simplifies the rest of our logic. */
2585 if (UR_Is_Zero (ur_realval))
2586 gnu_result = convert (gnu_result_type, integer_zero_node);
2587 else
2589 if (!Is_Machine_Number (gnat_node))
2590 ur_realval
2591 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
2592 ur_realval, Round_Even, gnat_node);
2594 gnu_result
2595 = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
2597 /* If we have a base of zero, divide by the denominator.
2598 Otherwise, the base must be 2 and we scale the value, which
2599 we know can fit in the mantissa of the type (hence the use
2600 of that type above). */
2601 if (No (Rbase (ur_realval)))
2602 gnu_result
2603 = build_binary_op (RDIV_EXPR,
2604 get_base_type (gnu_result_type),
2605 gnu_result,
2606 UI_To_gnu (Denominator (ur_realval),
2607 gnu_result_type));
2608 else if (Rbase (ur_realval) != 2)
2609 abort ();
2611 else
2613 REAL_VALUE_TYPE tmp;
2615 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
2616 - UI_To_Int (Denominator (ur_realval)));
2617 gnu_result = build_real (gnu_result_type, tmp);
2621 /* Now see if we need to negate the result. Do it this way to
2622 properly handle -0. */
2623 if (UR_Is_Negative (Realval (gnat_node)))
2624 gnu_result
2625 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
2626 gnu_result);
2629 break;
2631 case N_String_Literal:
2632 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2633 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
2635 String_Id gnat_string = Strval (gnat_node);
2636 int length = String_Length (gnat_string);
2637 char *string = (char *) alloca (length + 1);
2638 int i;
2640 /* Build the string with the characters in the literal. Note
2641 that Ada strings are 1-origin. */
2642 for (i = 0; i < length; i++)
2643 string[i] = Get_String_Char (gnat_string, i + 1);
2645 /* Put a null at the end of the string in case it's in a context
2646 where GCC will want to treat it as a C string. */
2647 string[i] = 0;
2649 gnu_result = build_string (length, string);
2651 /* Strings in GCC don't normally have types, but we want
2652 this to not be converted to the array type. */
2653 TREE_TYPE (gnu_result) = gnu_result_type;
2655 else
2657 /* Build a list consisting of each character, then make
2658 the aggregate. */
2659 String_Id gnat_string = Strval (gnat_node);
2660 int length = String_Length (gnat_string);
2661 int i;
2662 tree gnu_list = NULL_TREE;
2663 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
2665 for (i = 0; i < length; i++)
2667 gnu_list
2668 = tree_cons (gnu_idx,
2669 build_int_cst (TREE_TYPE (gnu_result_type),
2670 Get_String_Char (gnat_string,
2671 i + 1)),
2672 gnu_list);
2674 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, integer_one_node,
2678 gnu_result
2679 = gnat_build_constructor (gnu_result_type, nreverse (gnu_list));
2681 break;
2683 case N_Pragma:
2684 gnu_result = Pragma_to_gnu (gnat_node);
2685 break;
2687 /**************************************/
2688 /* Chapter 3: Declarations and Types: */
2689 /**************************************/
2691 case N_Subtype_Declaration:
2692 case N_Full_Type_Declaration:
2693 case N_Incomplete_Type_Declaration:
2694 case N_Private_Type_Declaration:
2695 case N_Private_Extension_Declaration:
2696 case N_Task_Type_Declaration:
2697 process_type (Defining_Entity (gnat_node));
2698 gnu_result = alloc_stmt_list ();
2699 break;
2701 case N_Object_Declaration:
2702 case N_Exception_Declaration:
2703 gnat_temp = Defining_Entity (gnat_node);
2704 gnu_result = alloc_stmt_list ();
2706 /* If we are just annotating types and this object has an unconstrained
2707 or task type, don't elaborate it. */
2708 if (type_annotate_only
2709 && (((Is_Array_Type (Etype (gnat_temp))
2710 || Is_Record_Type (Etype (gnat_temp)))
2711 && !Is_Constrained (Etype (gnat_temp)))
2712 || Is_Concurrent_Type (Etype (gnat_temp))))
2713 break;
2715 if (Present (Expression (gnat_node))
2716 && !(Nkind (gnat_node) == N_Object_Declaration
2717 && No_Initialization (gnat_node))
2718 && (!type_annotate_only
2719 || Compile_Time_Known_Value (Expression (gnat_node))))
2721 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2722 if (Do_Range_Check (Expression (gnat_node)))
2723 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_temp));
2725 /* If this object has its elaboration delayed, we must force
2726 evaluation of GNU_EXPR right now and save it for when the object
2727 is frozen. */
2728 if (Present (Freeze_Node (gnat_temp)))
2730 if ((Is_Public (gnat_temp) || global_bindings_p ())
2731 && !TREE_CONSTANT (gnu_expr))
2732 gnu_expr
2733 = create_var_decl (create_concat_name (gnat_temp, "init"),
2734 NULL_TREE, TREE_TYPE (gnu_expr),
2735 gnu_expr, false, Is_Public (gnat_temp),
2736 false, false, NULL, gnat_temp);
2737 else
2738 gnu_expr = maybe_variable (gnu_expr);
2740 save_gnu_tree (gnat_node, gnu_expr, true);
2743 else
2744 gnu_expr = NULL_TREE;
2746 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
2747 gnu_expr = NULL_TREE;
2749 if (No (Freeze_Node (gnat_temp)))
2750 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
2751 break;
2753 case N_Object_Renaming_Declaration:
2754 gnat_temp = Defining_Entity (gnat_node);
2756 /* Don't do anything if this renaming is handled by the front end. or if
2757 we are just annotating types and this object has a composite or task
2758 type, don't elaborate it. We return the result in case it has any
2759 SAVE_EXPRs in it that need to be evaluated here. */
2760 if (!Is_Renaming_Of_Object (gnat_temp)
2761 && ! (type_annotate_only
2762 && (Is_Array_Type (Etype (gnat_temp))
2763 || Is_Record_Type (Etype (gnat_temp))
2764 || Is_Concurrent_Type (Etype (gnat_temp)))))
2765 gnu_result
2766 = gnat_to_gnu_entity (gnat_temp,
2767 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
2768 else
2769 gnu_result = alloc_stmt_list ();
2770 break;
2772 case N_Implicit_Label_Declaration:
2773 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
2774 gnu_result = alloc_stmt_list ();
2775 break;
2777 case N_Exception_Renaming_Declaration:
2778 case N_Number_Declaration:
2779 case N_Package_Renaming_Declaration:
2780 case N_Subprogram_Renaming_Declaration:
2781 /* These are fully handled in the front end. */
2782 gnu_result = alloc_stmt_list ();
2783 break;
2785 /*************************************/
2786 /* Chapter 4: Names and Expressions: */
2787 /*************************************/
2789 case N_Explicit_Dereference:
2790 gnu_result = gnat_to_gnu (Prefix (gnat_node));
2791 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2792 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
2793 break;
2795 case N_Indexed_Component:
2797 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
2798 tree gnu_type;
2799 int ndim;
2800 int i;
2801 Node_Id *gnat_expr_array;
2803 gnu_array_object = maybe_implicit_deref (gnu_array_object);
2804 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
2806 /* If we got a padded type, remove it too. */
2807 if (TREE_CODE (TREE_TYPE (gnu_array_object)) == RECORD_TYPE
2808 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
2809 gnu_array_object
2810 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
2811 gnu_array_object);
2813 gnu_result = gnu_array_object;
2815 /* First compute the number of dimensions of the array, then
2816 fill the expression array, the order depending on whether
2817 this is a Convention_Fortran array or not. */
2818 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
2819 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2820 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
2821 ndim++, gnu_type = TREE_TYPE (gnu_type))
2824 gnat_expr_array = (Node_Id *) alloca (ndim * sizeof (Node_Id));
2826 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
2827 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
2828 i >= 0;
2829 i--, gnat_temp = Next (gnat_temp))
2830 gnat_expr_array[i] = gnat_temp;
2831 else
2832 for (i = 0, gnat_temp = First (Expressions (gnat_node));
2833 i < ndim;
2834 i++, gnat_temp = Next (gnat_temp))
2835 gnat_expr_array[i] = gnat_temp;
2837 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
2838 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
2840 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
2841 abort ();
2843 gnat_temp = gnat_expr_array[i];
2844 gnu_expr = gnat_to_gnu (gnat_temp);
2846 if (Do_Range_Check (gnat_temp))
2847 gnu_expr
2848 = emit_index_check
2849 (gnu_array_object, gnu_expr,
2850 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
2851 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
2853 gnu_result = build_binary_op (ARRAY_REF, NULL_TREE,
2854 gnu_result, gnu_expr);
2858 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2859 break;
2861 case N_Slice:
2863 tree gnu_type;
2864 Node_Id gnat_range_node = Discrete_Range (gnat_node);
2866 gnu_result = gnat_to_gnu (Prefix (gnat_node));
2867 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2869 /* Do any implicit dereferences of the prefix and do any needed
2870 range check. */
2871 gnu_result = maybe_implicit_deref (gnu_result);
2872 gnu_result = maybe_unconstrained_array (gnu_result);
2873 gnu_type = TREE_TYPE (gnu_result);
2874 if (Do_Range_Check (gnat_range_node))
2876 /* Get the bounds of the slice. */
2877 tree gnu_index_type
2878 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
2879 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
2880 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
2881 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
2883 /* Check to see that the minimum slice value is in range */
2884 gnu_expr_l
2885 = emit_index_check
2886 (gnu_result, gnu_min_expr,
2887 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
2888 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
2890 /* Check to see that the maximum slice value is in range */
2891 gnu_expr_h
2892 = emit_index_check
2893 (gnu_result, gnu_max_expr,
2894 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
2895 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
2897 /* Derive a good type to convert everything too */
2898 gnu_expr_type = get_base_type (TREE_TYPE (gnu_expr_l));
2900 /* Build a compound expression that does the range checks */
2901 gnu_expr
2902 = build_binary_op (COMPOUND_EXPR, gnu_expr_type,
2903 convert (gnu_expr_type, gnu_expr_h),
2904 convert (gnu_expr_type, gnu_expr_l));
2906 /* Build a conditional expression that returns the range checks
2907 expression if the slice range is not null (max >= min) or
2908 returns the min if the slice range is null */
2909 gnu_expr
2910 = fold (build3 (COND_EXPR, gnu_expr_type,
2911 build_binary_op (GE_EXPR, gnu_expr_type,
2912 convert (gnu_expr_type,
2913 gnu_max_expr),
2914 convert (gnu_expr_type,
2915 gnu_min_expr)),
2916 gnu_expr, gnu_min_expr));
2918 else
2919 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
2921 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
2922 gnu_result, gnu_expr);
2924 break;
2926 case N_Selected_Component:
2928 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
2929 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
2930 Entity_Id gnat_pref_type = Etype (Prefix (gnat_node));
2931 tree gnu_field;
2933 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
2934 || IN (Ekind (gnat_pref_type), Access_Kind))
2936 if (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind))
2937 gnat_pref_type = Underlying_Type (gnat_pref_type);
2938 else if (IN (Ekind (gnat_pref_type), Access_Kind))
2939 gnat_pref_type = Designated_Type (gnat_pref_type);
2942 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2944 /* For discriminant references in tagged types always substitute the
2945 corresponding discriminant as the actual selected component. */
2947 if (Is_Tagged_Type (gnat_pref_type))
2948 while (Present (Corresponding_Discriminant (gnat_field)))
2949 gnat_field = Corresponding_Discriminant (gnat_field);
2951 /* For discriminant references of untagged types always substitute the
2952 corresponding stored discriminant. */
2954 else if (Present (Corresponding_Discriminant (gnat_field)))
2955 gnat_field = Original_Record_Component (gnat_field);
2957 /* Handle extracting the real or imaginary part of a complex.
2958 The real part is the first field and the imaginary the last. */
2960 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
2961 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
2962 ? REALPART_EXPR : IMAGPART_EXPR,
2963 NULL_TREE, gnu_prefix);
2964 else
2966 gnu_field = gnat_to_gnu_entity (gnat_field, NULL_TREE, 0);
2968 /* If there are discriminants, the prefix might be
2969 evaluated more than once, which is a problem if it has
2970 side-effects. */
2971 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
2972 ? Designated_Type (Etype
2973 (Prefix (gnat_node)))
2974 : Etype (Prefix (gnat_node))))
2975 gnu_prefix = gnat_stabilize_reference (gnu_prefix, 0);
2977 gnu_result
2978 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
2979 (Nkind (Parent (gnat_node))
2980 == N_Attribute_Reference));
2983 if (!gnu_result)
2984 abort ();
2986 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2988 break;
2990 case N_Attribute_Reference:
2992 /* The attribute designator (like an enumeration value). */
2993 int attribute = Get_Attribute_Id (Attribute_Name (gnat_node));
2995 /* The Elab_Spec and Elab_Body attributes are special in that
2996 Prefix is a unit, not an object with a GCC equivalent. Similarly
2997 for Elaborated, since that variable isn't otherwise known. */
2998 if (attribute == Attr_Elab_Body || attribute == Attr_Elab_Spec)
2999 return (create_subprog_decl
3000 (create_concat_name (Entity (Prefix (gnat_node)),
3001 attribute == Attr_Elab_Body
3002 ? "elabb" : "elabs"),
3003 NULL_TREE, void_ftype, NULL_TREE, false, true, true, NULL,
3004 gnat_node));
3006 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attribute);
3008 break;
3010 case N_Reference:
3011 /* Like 'Access as far as we are concerned. */
3012 gnu_result = gnat_to_gnu (Prefix (gnat_node));
3013 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
3014 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3015 break;
3017 case N_Aggregate:
3018 case N_Extension_Aggregate:
3020 tree gnu_aggr_type;
3022 /* ??? It is wrong to evaluate the type now, but there doesn't
3023 seem to be any other practical way of doing it. */
3025 gnu_aggr_type = gnu_result_type
3026 = get_unpadded_type (Etype (gnat_node));
3028 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
3029 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
3030 gnu_aggr_type
3031 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_result_type)));
3033 if (Null_Record_Present (gnat_node))
3034 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL_TREE);
3036 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE)
3037 gnu_result
3038 = assoc_to_constructor (First (Component_Associations (gnat_node)),
3039 gnu_aggr_type);
3040 else if (TREE_CODE (gnu_aggr_type) == UNION_TYPE)
3042 /* The first element is the discrimant, which we ignore. The
3043 next is the field we're building. Convert the expression
3044 to the type of the field and then to the union type. */
3045 Node_Id gnat_assoc
3046 = Next (First (Component_Associations (gnat_node)));
3047 Entity_Id gnat_field = Entity (First (Choices (gnat_assoc)));
3048 tree gnu_field_type
3049 = TREE_TYPE (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0));
3051 gnu_result = convert (gnu_field_type,
3052 gnat_to_gnu (Expression (gnat_assoc)));
3054 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
3055 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
3056 gnu_aggr_type,
3057 Component_Type (Etype (gnat_node)));
3058 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
3059 gnu_result
3060 = build_binary_op
3061 (COMPLEX_EXPR, gnu_aggr_type,
3062 gnat_to_gnu (Expression (First
3063 (Component_Associations (gnat_node)))),
3064 gnat_to_gnu (Expression
3065 (Next
3066 (First (Component_Associations (gnat_node))))));
3067 else
3068 abort ();
3070 gnu_result = convert (gnu_result_type, gnu_result);
3072 break;
3074 case N_Null:
3075 gnu_result = null_pointer_node;
3076 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3077 break;
3079 case N_Type_Conversion:
3080 case N_Qualified_Expression:
3081 /* Get the operand expression. */
3082 gnu_result = gnat_to_gnu (Expression (gnat_node));
3083 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3085 gnu_result
3086 = convert_with_check (Etype (gnat_node), gnu_result,
3087 Do_Overflow_Check (gnat_node),
3088 Do_Range_Check (Expression (gnat_node)),
3089 Nkind (gnat_node) == N_Type_Conversion
3090 && Float_Truncate (gnat_node));
3091 break;
3093 case N_Unchecked_Type_Conversion:
3094 gnu_result = gnat_to_gnu (Expression (gnat_node));
3095 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3097 /* If the result is a pointer type, see if we are improperly
3098 converting to a stricter alignment. */
3100 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
3101 && IN (Ekind (Etype (gnat_node)), Access_Kind))
3103 unsigned int align = known_alignment (gnu_result);
3104 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
3105 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
3107 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
3108 post_error_ne_tree_2
3109 ("?source alignment (^) < alignment of & (^)",
3110 gnat_node, Designated_Type (Etype (gnat_node)),
3111 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
3114 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
3115 No_Truncation (gnat_node));
3116 break;
3118 case N_In:
3119 case N_Not_In:
3121 tree gnu_object = gnat_to_gnu (Left_Opnd (gnat_node));
3122 Node_Id gnat_range = Right_Opnd (gnat_node);
3123 tree gnu_low;
3124 tree gnu_high;
3126 /* GNAT_RANGE is either an N_Range node or an identifier
3127 denoting a subtype. */
3128 if (Nkind (gnat_range) == N_Range)
3130 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
3131 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
3133 else if (Nkind (gnat_range) == N_Identifier
3134 || Nkind (gnat_range) == N_Expanded_Name)
3136 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
3138 gnu_low = TYPE_MIN_VALUE (gnu_range_type);
3139 gnu_high = TYPE_MAX_VALUE (gnu_range_type);
3141 else
3142 abort ();
3144 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3146 /* If LOW and HIGH are identical, perform an equality test.
3147 Otherwise, ensure that GNU_OBJECT is only evaluated once
3148 and perform a full range test. */
3149 if (operand_equal_p (gnu_low, gnu_high, 0))
3150 gnu_result = build_binary_op (EQ_EXPR, gnu_result_type,
3151 gnu_object, gnu_low);
3152 else
3154 gnu_object = protect_multiple_eval (gnu_object);
3155 gnu_result
3156 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type,
3157 build_binary_op (GE_EXPR, gnu_result_type,
3158 gnu_object, gnu_low),
3159 build_binary_op (LE_EXPR, gnu_result_type,
3160 gnu_object, gnu_high));
3163 if (Nkind (gnat_node) == N_Not_In)
3164 gnu_result = invert_truthvalue (gnu_result);
3166 break;
3168 case N_Op_Divide:
3169 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
3170 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
3171 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3172 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
3173 ? RDIV_EXPR
3174 : (Rounded_Result (gnat_node)
3175 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
3176 gnu_result_type, gnu_lhs, gnu_rhs);
3177 break;
3179 case N_Op_Or: case N_Op_And: case N_Op_Xor:
3180 /* These can either be operations on booleans or on modular types.
3181 Fall through for boolean types since that's the way GNU_CODES is
3182 set up. */
3183 if (IN (Ekind (Underlying_Type (Etype (gnat_node))),
3184 Modular_Integer_Kind))
3186 enum tree_code code
3187 = (Nkind (gnat_node) == N_Op_Or ? BIT_IOR_EXPR
3188 : Nkind (gnat_node) == N_Op_And ? BIT_AND_EXPR
3189 : BIT_XOR_EXPR);
3191 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
3192 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
3193 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3194 gnu_result = build_binary_op (code, gnu_result_type,
3195 gnu_lhs, gnu_rhs);
3196 break;
3199 /* ... fall through ... */
3201 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
3202 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
3203 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
3204 case N_Op_Mod: case N_Op_Rem:
3205 case N_Op_Rotate_Left:
3206 case N_Op_Rotate_Right:
3207 case N_Op_Shift_Left:
3208 case N_Op_Shift_Right:
3209 case N_Op_Shift_Right_Arithmetic:
3210 case N_And_Then: case N_Or_Else:
3212 enum tree_code code = gnu_codes[Nkind (gnat_node)];
3213 tree gnu_type;
3215 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
3216 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
3217 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
3219 /* If this is a comparison operator, convert any references to
3220 an unconstrained array value into a reference to the
3221 actual array. */
3222 if (TREE_CODE_CLASS (code) == tcc_comparison)
3224 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
3225 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
3228 /* If the result type is a private type, its full view may be a
3229 numeric subtype. The representation we need is that of its base
3230 type, given that it is the result of an arithmetic operation. */
3231 else if (Is_Private_Type (Etype (gnat_node)))
3232 gnu_type = gnu_result_type
3233 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node))));
3235 /* If this is a shift whose count is not guaranteed to be correct,
3236 we need to adjust the shift count. */
3237 if (IN (Nkind (gnat_node), N_Op_Shift)
3238 && !Shift_Count_OK (gnat_node))
3240 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
3241 tree gnu_max_shift
3242 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
3244 if (Nkind (gnat_node) == N_Op_Rotate_Left
3245 || Nkind (gnat_node) == N_Op_Rotate_Right)
3246 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
3247 gnu_rhs, gnu_max_shift);
3248 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic)
3249 gnu_rhs
3250 = build_binary_op
3251 (MIN_EXPR, gnu_count_type,
3252 build_binary_op (MINUS_EXPR,
3253 gnu_count_type,
3254 gnu_max_shift,
3255 convert (gnu_count_type,
3256 integer_one_node)),
3257 gnu_rhs);
3260 /* For right shifts, the type says what kind of shift to do,
3261 so we may need to choose a different type. */
3262 if (Nkind (gnat_node) == N_Op_Shift_Right
3263 && !TYPE_UNSIGNED (gnu_type))
3264 gnu_type = gnat_unsigned_type (gnu_type);
3265 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic
3266 && TYPE_UNSIGNED (gnu_type))
3267 gnu_type = gnat_signed_type (gnu_type);
3269 if (gnu_type != gnu_result_type)
3271 gnu_lhs = convert (gnu_type, gnu_lhs);
3272 gnu_rhs = convert (gnu_type, gnu_rhs);
3275 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
3277 /* If this is a logical shift with the shift count not verified,
3278 we must return zero if it is too large. We cannot compensate
3279 above in this case. */
3280 if ((Nkind (gnat_node) == N_Op_Shift_Left
3281 || Nkind (gnat_node) == N_Op_Shift_Right)
3282 && !Shift_Count_OK (gnat_node))
3283 gnu_result
3284 = build_cond_expr
3285 (gnu_type,
3286 build_binary_op (GE_EXPR, integer_type_node,
3287 gnu_rhs,
3288 convert (TREE_TYPE (gnu_rhs),
3289 TYPE_SIZE (gnu_type))),
3290 convert (gnu_type, integer_zero_node),
3291 gnu_result);
3293 break;
3295 case N_Conditional_Expression:
3297 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
3298 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
3299 tree gnu_false
3300 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
3302 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3303 gnu_result = build_cond_expr (gnu_result_type,
3304 gnat_truthvalue_conversion (gnu_cond),
3305 gnu_true, gnu_false);
3307 break;
3309 case N_Op_Plus:
3310 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
3311 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3312 break;
3314 case N_Op_Not:
3315 /* This case can apply to a boolean or a modular type.
3316 Fall through for a boolean operand since GNU_CODES is set
3317 up to handle this. */
3318 if (IN (Ekind (Etype (gnat_node)), Modular_Integer_Kind))
3320 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
3321 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3322 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
3323 gnu_expr);
3324 break;
3327 /* ... fall through ... */
3329 case N_Op_Minus: case N_Op_Abs:
3330 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
3332 if (Ekind (Etype (gnat_node)) != E_Private_Type)
3333 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3334 else
3335 gnu_result_type = get_unpadded_type (Base_Type
3336 (Full_View (Etype (gnat_node))));
3338 gnu_result = build_unary_op (gnu_codes[Nkind (gnat_node)],
3339 gnu_result_type, gnu_expr);
3340 break;
3342 case N_Allocator:
3344 tree gnu_init = 0;
3345 tree gnu_type;
3347 gnat_temp = Expression (gnat_node);
3349 /* The Expression operand can either be an N_Identifier or
3350 Expanded_Name, which must represent a type, or a
3351 N_Qualified_Expression, which contains both the object type and an
3352 initial value for the object. */
3353 if (Nkind (gnat_temp) == N_Identifier
3354 || Nkind (gnat_temp) == N_Expanded_Name)
3355 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
3356 else if (Nkind (gnat_temp) == N_Qualified_Expression)
3358 Entity_Id gnat_desig_type
3359 = Designated_Type (Underlying_Type (Etype (gnat_node)));
3361 gnu_init = gnat_to_gnu (Expression (gnat_temp));
3363 gnu_init = maybe_unconstrained_array (gnu_init);
3364 if (Do_Range_Check (Expression (gnat_temp)))
3365 gnu_init = emit_range_check (gnu_init, gnat_desig_type);
3367 if (Is_Elementary_Type (gnat_desig_type)
3368 || Is_Constrained (gnat_desig_type))
3370 gnu_type = gnat_to_gnu_type (gnat_desig_type);
3371 gnu_init = convert (gnu_type, gnu_init);
3373 else
3375 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
3376 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3377 gnu_type = TREE_TYPE (gnu_init);
3379 gnu_init = convert (gnu_type, gnu_init);
3382 else
3383 abort ();
3385 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3386 return build_allocator (gnu_type, gnu_init, gnu_result_type,
3387 Procedure_To_Call (gnat_node),
3388 Storage_Pool (gnat_node), gnat_node);
3390 break;
3392 /***************************/
3393 /* Chapter 5: Statements: */
3394 /***************************/
3396 case N_Label:
3397 gnu_result = build1 (LABEL_EXPR, void_type_node,
3398 gnat_to_gnu (Identifier (gnat_node)));
3399 break;
3401 case N_Null_Statement:
3402 gnu_result = alloc_stmt_list ();
3403 break;
3405 case N_Assignment_Statement:
3406 /* Get the LHS and RHS of the statement and convert any reference to an
3407 unconstrained array into a reference to the underlying array.
3408 If we are not to do range checking and the RHS is an N_Function_Call,
3409 pass the LHS to the call function. */
3410 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
3412 /* If the type has a size that overflows, convert this into raise of
3413 Storage_Error: execution shouldn't have gotten here anyway. */
3414 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_lhs))) == INTEGER_CST
3415 && TREE_OVERFLOW (TYPE_SIZE (TREE_TYPE (gnu_lhs))))
3416 gnu_result = build_call_raise (SE_Object_Too_Large);
3417 else if (Nkind (Expression (gnat_node)) == N_Function_Call
3418 && !Do_Range_Check (Expression (gnat_node)))
3419 gnu_result = call_to_gnu (Expression (gnat_node),
3420 &gnu_result_type, gnu_lhs);
3421 else
3423 gnu_rhs
3424 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node)));
3426 /* If range check is needed, emit code to generate it */
3427 if (Do_Range_Check (Expression (gnat_node)))
3428 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)));
3430 gnu_result
3431 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
3433 break;
3435 case N_If_Statement:
3437 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
3439 /* Make the outer COND_EXPR. Avoid non-determinism. */
3440 gnu_result = build3 (COND_EXPR, void_type_node,
3441 gnat_to_gnu (Condition (gnat_node)),
3442 NULL_TREE, NULL_TREE);
3443 COND_EXPR_THEN (gnu_result)
3444 = build_stmt_group (Then_Statements (gnat_node), false);
3445 TREE_SIDE_EFFECTS (gnu_result) = 1;
3446 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
3448 /* Now make a COND_EXPR for each of the "else if" parts. Put each
3449 into the previous "else" part and point to where to put any
3450 outer "else". Also avoid non-determinism. */
3451 if (Present (Elsif_Parts (gnat_node)))
3452 for (gnat_temp = First (Elsif_Parts (gnat_node));
3453 Present (gnat_temp); gnat_temp = Next (gnat_temp))
3455 gnu_expr = build3 (COND_EXPR, void_type_node,
3456 gnat_to_gnu (Condition (gnat_temp)),
3457 NULL_TREE, NULL_TREE);
3458 COND_EXPR_THEN (gnu_expr)
3459 = build_stmt_group (Then_Statements (gnat_temp), false);
3460 TREE_SIDE_EFFECTS (gnu_expr) = 1;
3461 annotate_with_node (gnu_expr, gnat_temp);
3462 *gnu_else_ptr = gnu_expr;
3463 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
3466 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
3468 break;
3470 case N_Case_Statement:
3471 gnu_result = Case_Statement_to_gnu (gnat_node);
3472 break;
3474 case N_Loop_Statement:
3475 gnu_result = Loop_Statement_to_gnu (gnat_node);
3476 break;
3478 case N_Block_Statement:
3479 start_stmt_group ();
3480 gnat_pushlevel ();
3481 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3482 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3483 gnat_poplevel ();
3484 gnu_result = end_stmt_group ();
3486 if (Present (Identifier (gnat_node)))
3487 mark_out_of_scope (Entity (Identifier (gnat_node)));
3488 break;
3490 case N_Exit_Statement:
3491 gnu_result
3492 = build2 (EXIT_STMT, void_type_node,
3493 (Present (Condition (gnat_node))
3494 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
3495 (Present (Name (gnat_node))
3496 ? get_gnu_tree (Entity (Name (gnat_node)))
3497 : TREE_VALUE (gnu_loop_label_stack)));
3498 break;
3500 case N_Return_Statement:
3502 /* The gnu function type of the subprogram currently processed. */
3503 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
3504 /* The return value from the subprogram. */
3505 tree gnu_ret_val = NULL_TREE;
3506 /* The place to put the return value. */
3507 tree gnu_lhs
3508 = (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type)
3509 ? build_unary_op (INDIRECT_REF, NULL_TREE,
3510 DECL_ARGUMENTS (current_function_decl))
3511 : DECL_RESULT (current_function_decl));
3513 /* If we are dealing with a "return;" from an Ada procedure with
3514 parameters passed by copy in copy out, we need to return a record
3515 containing the final values of these parameters. If the list
3516 contains only one entry, return just that entry.
3518 For a full description of the copy in copy out parameter mechanism,
3519 see the part of the gnat_to_gnu_entity routine dealing with the
3520 translation of subprograms.
3522 But if we have a return label defined, convert this into
3523 a branch to that label. */
3525 if (TREE_VALUE (gnu_return_label_stack))
3527 gnu_result = build1 (GOTO_EXPR, void_type_node,
3528 TREE_VALUE (gnu_return_label_stack));
3529 break;
3532 else if (TYPE_CI_CO_LIST (gnu_subprog_type))
3534 if (list_length (TYPE_CI_CO_LIST (gnu_subprog_type)) == 1)
3535 gnu_ret_val = TREE_VALUE (TYPE_CI_CO_LIST (gnu_subprog_type));
3536 else
3537 gnu_ret_val
3538 = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
3539 TYPE_CI_CO_LIST (gnu_subprog_type));
3542 /* If the Ada subprogram is a function, we just need to return the
3543 expression. If the subprogram returns an unconstrained
3544 array, we have to allocate a new version of the result and
3545 return it. If we return by reference, return a pointer. */
3547 else if (Present (Expression (gnat_node)))
3549 /* If the current function returns by target pointer and we
3550 are doing a call, pass that target to the call. */
3551 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type)
3552 && Nkind (Expression (gnat_node)) == N_Function_Call)
3553 gnu_result = call_to_gnu (Expression (gnat_node),
3554 &gnu_result_type, gnu_lhs);
3556 else
3558 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
3560 /* Do not remove the padding from GNU_RET_VAL if the inner
3561 type is self-referential since we want to allocate the fixed
3562 size in that case. */
3563 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
3564 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
3565 == RECORD_TYPE)
3566 && (TYPE_IS_PADDING_P
3567 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
3568 && (CONTAINS_PLACEHOLDER_P
3569 (TYPE_SIZE (TREE_TYPE (gnu_ret_val)))))
3570 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
3572 if (TYPE_RETURNS_BY_REF_P (gnu_subprog_type)
3573 || By_Ref (gnat_node))
3574 gnu_ret_val
3575 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
3577 else if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type))
3579 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
3581 /* We have two cases: either the function returns with
3582 depressed stack or not. If not, we allocate on the
3583 secondary stack. If so, we allocate in the stack frame.
3584 if no copy is needed, the front end will set By_Ref,
3585 which we handle in the case above. */
3586 if (TYPE_RETURNS_STACK_DEPRESSED (gnu_subprog_type))
3587 gnu_ret_val
3588 = build_allocator (TREE_TYPE (gnu_ret_val),
3589 gnu_ret_val,
3590 TREE_TYPE (gnu_subprog_type),
3591 0, -1, gnat_node);
3592 else
3593 gnu_ret_val
3594 = build_allocator (TREE_TYPE (gnu_ret_val),
3595 gnu_ret_val,
3596 TREE_TYPE (gnu_subprog_type),
3597 Procedure_To_Call (gnat_node),
3598 Storage_Pool (gnat_node),
3599 gnat_node);
3603 gnu_result = build2 (MODIFY_EXPR, TREE_TYPE (gnu_ret_val),
3604 gnu_lhs, gnu_ret_val);
3605 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
3607 add_stmt_with_node (gnu_result, gnat_node);
3608 gnu_ret_val = NULL_TREE;
3612 gnu_result = build1 (RETURN_EXPR, void_type_node,
3613 gnu_ret_val ? gnu_result : gnu_ret_val);
3615 break;
3617 case N_Goto_Statement:
3618 gnu_result = build1 (GOTO_EXPR, void_type_node,
3619 gnat_to_gnu (Name (gnat_node)));
3620 break;
3622 /****************************/
3623 /* Chapter 6: Subprograms: */
3624 /****************************/
3626 case N_Subprogram_Declaration:
3627 /* Unless there is a freeze node, declare the subprogram. We consider
3628 this a "definition" even though we're not generating code for
3629 the subprogram because we will be making the corresponding GCC
3630 node here. */
3632 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
3633 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
3634 NULL_TREE, 1);
3635 gnu_result = alloc_stmt_list ();
3636 break;
3638 case N_Abstract_Subprogram_Declaration:
3639 /* This subprogram doesn't exist for code generation purposes, but we
3640 have to elaborate the types of any parameters, unless they are
3641 imported types (nothing to generate in this case). */
3642 for (gnat_temp
3643 = First_Formal (Defining_Entity (Specification (gnat_node)));
3644 Present (gnat_temp);
3645 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3646 if (Is_Itype (Etype (gnat_temp))
3647 && !From_With_Type (Etype (gnat_temp)))
3648 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3650 gnu_result = alloc_stmt_list ();
3651 break;
3653 case N_Defining_Program_Unit_Name:
3654 /* For a child unit identifier go up a level to get the
3655 specificaton. We get this when we try to find the spec of
3656 a child unit package that is the compilation unit being compiled. */
3657 gnu_result = gnat_to_gnu (Parent (gnat_node));
3658 break;
3660 case N_Subprogram_Body:
3661 Subprogram_Body_to_gnu (gnat_node);
3662 gnu_result = alloc_stmt_list ();
3663 break;
3665 case N_Function_Call:
3666 case N_Procedure_Call_Statement:
3667 gnu_result = call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE);
3668 break;
3670 /*************************/
3671 /* Chapter 7: Packages: */
3672 /*************************/
3674 case N_Package_Declaration:
3675 gnu_result = gnat_to_gnu (Specification (gnat_node));
3676 break;
3678 case N_Package_Specification:
3680 start_stmt_group ();
3681 process_decls (Visible_Declarations (gnat_node),
3682 Private_Declarations (gnat_node), Empty, true, true);
3683 gnu_result = end_stmt_group ();
3684 break;
3686 case N_Package_Body:
3688 /* If this is the body of a generic package - do nothing */
3689 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
3691 gnu_result = alloc_stmt_list ();
3692 break;
3695 start_stmt_group ();
3696 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3698 if (Present (Handled_Statement_Sequence (gnat_node)))
3699 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3701 gnu_result = end_stmt_group ();
3702 break;
3704 /*********************************/
3705 /* Chapter 8: Visibility Rules: */
3706 /*********************************/
3708 case N_Use_Package_Clause:
3709 case N_Use_Type_Clause:
3710 /* Nothing to do here - but these may appear in list of declarations */
3711 gnu_result = alloc_stmt_list ();
3712 break;
3714 /***********************/
3715 /* Chapter 9: Tasks: */
3716 /***********************/
3718 case N_Protected_Type_Declaration:
3719 gnu_result = alloc_stmt_list ();
3720 break;
3722 case N_Single_Task_Declaration:
3723 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
3724 gnu_result = alloc_stmt_list ();
3725 break;
3727 /***********************************************************/
3728 /* Chapter 10: Program Structure and Compilation Issues: */
3729 /***********************************************************/
3731 case N_Compilation_Unit:
3733 /* This is not called for the main unit, which is handled in function
3734 gigi above. */
3735 start_stmt_group ();
3736 gnat_pushlevel ();
3738 Compilation_Unit_to_gnu (gnat_node);
3739 gnu_result = alloc_stmt_list ();
3740 break;
3742 case N_Subprogram_Body_Stub:
3743 case N_Package_Body_Stub:
3744 case N_Protected_Body_Stub:
3745 case N_Task_Body_Stub:
3746 /* Simply process whatever unit is being inserted. */
3747 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
3748 break;
3750 case N_Subunit:
3751 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
3752 break;
3754 /***************************/
3755 /* Chapter 11: Exceptions: */
3756 /***************************/
3758 case N_Handled_Sequence_Of_Statements:
3759 /* If there is an At_End procedure attached to this node, and the EH
3760 mechanism is SJLJ, we must have at least a corresponding At_End
3761 handler, unless the No_Exception_Handlers restriction is set. */
3762 if (!type_annotate_only
3763 && Exception_Mechanism == Setjmp_Longjmp
3764 && Present (At_End_Proc (gnat_node))
3765 && !Present (Exception_Handlers (gnat_node))
3766 && !No_Exception_Handlers_Set())
3767 abort ();
3769 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
3770 break;
3772 case N_Exception_Handler:
3773 if (Exception_Mechanism == Setjmp_Longjmp)
3774 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
3775 else if (Exception_Mechanism == GCC_ZCX)
3776 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
3777 else
3778 abort ();
3780 break;
3782 /*******************************/
3783 /* Chapter 12: Generic Units: */
3784 /*******************************/
3786 case N_Generic_Function_Renaming_Declaration:
3787 case N_Generic_Package_Renaming_Declaration:
3788 case N_Generic_Procedure_Renaming_Declaration:
3789 case N_Generic_Package_Declaration:
3790 case N_Generic_Subprogram_Declaration:
3791 case N_Package_Instantiation:
3792 case N_Procedure_Instantiation:
3793 case N_Function_Instantiation:
3794 /* These nodes can appear on a declaration list but there is nothing to
3795 to be done with them. */
3796 gnu_result = alloc_stmt_list ();
3797 break;
3799 /***************************************************/
3800 /* Chapter 13: Representation Clauses and */
3801 /* Implementation-Dependent Features: */
3802 /***************************************************/
3804 case N_Attribute_Definition_Clause:
3806 gnu_result = alloc_stmt_list ();
3808 /* The only one we need deal with is for 'Address. For the others, SEM
3809 puts the information elsewhere. We need only deal with 'Address
3810 if the object has a Freeze_Node (which it never will currently). */
3811 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address
3812 || No (Freeze_Node (Entity (Name (gnat_node)))))
3813 break;
3815 /* Get the value to use as the address and save it as the
3816 equivalent for GNAT_TEMP. When the object is frozen,
3817 gnat_to_gnu_entity will do the right thing. */
3818 save_gnu_tree (Entity (Name (gnat_node)),
3819 gnat_to_gnu (Expression (gnat_node)), true);
3820 break;
3822 case N_Enumeration_Representation_Clause:
3823 case N_Record_Representation_Clause:
3824 case N_At_Clause:
3825 /* We do nothing with these. SEM puts the information elsewhere. */
3826 gnu_result = alloc_stmt_list ();
3827 break;
3829 case N_Code_Statement:
3830 if (!type_annotate_only)
3832 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
3833 tree gnu_input_list = NULL_TREE, gnu_output_list = NULL_TREE;
3834 tree gnu_clobber_list = NULL_TREE;
3835 char *clobber;
3837 /* First process inputs, then outputs, then clobbers. */
3838 Setup_Asm_Inputs (gnat_node);
3839 while (Present (gnat_temp = Asm_Input_Value ()))
3841 tree gnu_value = gnat_to_gnu (gnat_temp);
3842 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
3843 (Asm_Input_Constraint ()));
3845 gnu_input_list
3846 = tree_cons (gnu_constr, gnu_value, gnu_input_list);
3847 Next_Asm_Input ();
3850 Setup_Asm_Outputs (gnat_node);
3851 while (Present (gnat_temp = Asm_Output_Variable ()))
3853 tree gnu_value = gnat_to_gnu (gnat_temp);
3854 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
3855 (Asm_Output_Constraint ()));
3857 gnu_output_list
3858 = tree_cons (gnu_constr, gnu_value, gnu_output_list);
3859 Next_Asm_Output ();
3862 Clobber_Setup (gnat_node);
3863 while ((clobber = Clobber_Get_Next ()))
3864 gnu_clobber_list
3865 = tree_cons (NULL_TREE,
3866 build_string (strlen (clobber) + 1, clobber),
3867 gnu_clobber_list);
3869 gnu_input_list = nreverse (gnu_input_list);
3870 gnu_output_list = nreverse (gnu_output_list);
3871 gnu_result = build4 (ASM_EXPR, void_type_node,
3872 gnu_template, gnu_output_list,
3873 gnu_input_list, gnu_clobber_list);
3874 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
3876 else
3877 gnu_result = alloc_stmt_list ();
3879 break;
3881 /***************************************************/
3882 /* Added Nodes */
3883 /***************************************************/
3885 case N_Freeze_Entity:
3886 start_stmt_group ();
3887 process_freeze_entity (gnat_node);
3888 process_decls (Actions (gnat_node), Empty, Empty, true, true);
3889 gnu_result = end_stmt_group ();
3890 break;
3892 case N_Itype_Reference:
3893 if (!present_gnu_tree (Itype (gnat_node)))
3894 process_type (Itype (gnat_node));
3896 gnu_result = alloc_stmt_list ();
3897 break;
3899 case N_Free_Statement:
3900 if (!type_annotate_only)
3902 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
3903 tree gnu_obj_type;
3904 tree gnu_obj_size;
3905 int align;
3907 /* If this is a thin pointer, we must dereference it to create
3908 a fat pointer, then go back below to a thin pointer. The
3909 reason for this is that we need a fat pointer someplace in
3910 order to properly compute the size. */
3911 if (TYPE_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
3912 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
3913 build_unary_op (INDIRECT_REF, NULL_TREE,
3914 gnu_ptr));
3916 /* If this is an unconstrained array, we know the object must
3917 have been allocated with the template in front of the object.
3918 So pass the template address, but get the total size. Do this
3919 by converting to a thin pointer. */
3920 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
3921 gnu_ptr
3922 = convert (build_pointer_type
3923 (TYPE_OBJECT_RECORD_TYPE
3924 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
3925 gnu_ptr);
3927 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
3928 gnu_obj_size = TYPE_SIZE_UNIT (gnu_obj_type);
3929 align = TYPE_ALIGN (gnu_obj_type);
3931 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
3932 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
3934 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
3935 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
3936 tree gnu_byte_offset
3937 = convert (gnu_char_ptr_type,
3938 size_diffop (size_zero_node, gnu_pos));
3940 gnu_ptr = convert (gnu_char_ptr_type, gnu_ptr);
3941 gnu_ptr = build_binary_op (MINUS_EXPR, gnu_char_ptr_type,
3942 gnu_ptr, gnu_byte_offset);
3945 gnu_result = build_call_alloc_dealloc (gnu_ptr, gnu_obj_size, align,
3946 Procedure_To_Call (gnat_node),
3947 Storage_Pool (gnat_node),
3948 gnat_node);
3950 break;
3952 case N_Raise_Constraint_Error:
3953 case N_Raise_Program_Error:
3954 case N_Raise_Storage_Error:
3955 if (type_annotate_only)
3957 gnu_result = alloc_stmt_list ();
3958 break;
3961 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3962 gnu_result = build_call_raise (UI_To_Int (Reason (gnat_node)));
3964 /* If the type is VOID, this is a statement, so we need to
3965 generate the code for the call. Handle a Condition, if there
3966 is one. */
3967 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
3969 annotate_with_node (gnu_result, gnat_node);
3971 if (Present (Condition (gnat_node)))
3972 gnu_result = build3 (COND_EXPR, void_type_node,
3973 gnat_to_gnu (Condition (gnat_node)),
3974 gnu_result, alloc_stmt_list ());
3976 else
3977 gnu_result = build1 (NULL_EXPR, gnu_result_type, gnu_result);
3978 break;
3980 case N_Validate_Unchecked_Conversion:
3981 /* If the result is a pointer type, see if we are either converting
3982 from a non-pointer or from a pointer to a type with a different
3983 alias set and warn if so. If the result defined in the same unit as
3984 this unchecked convertion, we can allow this because we can know to
3985 make that type have alias set 0. */
3987 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
3988 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
3990 if (POINTER_TYPE_P (gnu_target_type)
3991 && !In_Same_Source_Unit (Target_Type (gnat_node), gnat_node)
3992 && get_alias_set (TREE_TYPE (gnu_target_type)) != 0
3993 && !No_Strict_Aliasing (Underlying_Type (Target_Type (gnat_node)))
3994 && (!POINTER_TYPE_P (gnu_source_type)
3995 || (get_alias_set (TREE_TYPE (gnu_source_type))
3996 != get_alias_set (TREE_TYPE (gnu_target_type)))))
3998 post_error_ne
3999 ("?possible aliasing problem for type&",
4000 gnat_node, Target_Type (gnat_node));
4001 post_error
4002 ("\\?use -fno-strict-aliasing switch for references",
4003 gnat_node);
4004 post_error_ne
4005 ("\\?or use `pragma No_Strict_Aliasing (&);`",
4006 gnat_node, Target_Type (gnat_node));
4009 gnu_result = alloc_stmt_list ();
4010 break;
4012 case N_Raise_Statement:
4013 case N_Function_Specification:
4014 case N_Procedure_Specification:
4015 case N_Op_Concat:
4016 case N_Component_Association:
4017 case N_Task_Body:
4018 default:
4019 if (!type_annotate_only)
4020 abort ();
4022 gnu_result = alloc_stmt_list ();
4025 /* If we pushed our level as part of processing the elaboration routine,
4026 pop it back now. */
4027 if (went_into_elab_proc)
4029 add_stmt (gnu_result);
4030 gnat_poplevel ();
4031 gnu_result = end_stmt_group ();
4032 current_function_decl = NULL_TREE;
4035 /* Set the location information into the result. If we're supposed to
4036 return something of void_type, it means we have something we're
4037 elaborating for effect, so just return. */
4038 if (EXPR_P (gnu_result))
4039 annotate_with_node (gnu_result, gnat_node);
4041 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
4042 return gnu_result;
4044 /* If the result is a constant that overflows, raise constraint error. */
4045 else if (TREE_CODE (gnu_result) == INTEGER_CST
4046 && TREE_CONSTANT_OVERFLOW (gnu_result))
4048 post_error ("Constraint_Error will be raised at run-time?", gnat_node);
4050 gnu_result
4051 = build1 (NULL_EXPR, gnu_result_type,
4052 build_call_raise (CE_Overflow_Check_Failed));
4055 /* If our result has side-effects and is of an unconstrained type,
4056 make a SAVE_EXPR so that we can be sure it will only be referenced
4057 once. Note we must do this before any conversions. */
4058 if (TREE_SIDE_EFFECTS (gnu_result)
4059 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
4060 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
4061 gnu_result = gnat_stabilize_reference (gnu_result, 0);
4063 /* Now convert the result to the proper type. If the type is void or if
4064 we have no result, return error_mark_node to show we have no result.
4065 If the type of the result is correct or if we have a label (which doesn't
4066 have any well-defined type), return our result. Also don't do the
4067 conversion if the "desired" type involves a PLACEHOLDER_EXPR in its size
4068 since those are the cases where the front end may have the type wrong due
4069 to "instantiating" the unconstrained record with discriminant values
4070 or if this is a FIELD_DECL. If this is the Name of an assignment
4071 statement or a parameter of a procedure call, return what we have since
4072 the RHS has to be converted to our type there in that case, unless
4073 GNU_RESULT_TYPE has a simpler size. Similarly, if the two types are
4074 record types with the same name, the expression type has integral mode,
4075 and GNU_RESULT_TYPE BLKmode, don't convert. This will be the case when
4076 we are converting from a packable type to its actual type and we need
4077 those conversions to be NOPs in order for assignments into these types to
4078 work properly if the inner object is a bitfield and hence can't have
4079 its address taken. Finally, don't convert integral types that are the
4080 operand of an unchecked conversion since we need to ignore those
4081 conversions (for 'Valid). Otherwise, convert the result to the proper
4082 type. */
4084 if (Present (Parent (gnat_node))
4085 && ((Nkind (Parent (gnat_node)) == N_Assignment_Statement
4086 && Name (Parent (gnat_node)) == gnat_node)
4087 || (Nkind (Parent (gnat_node)) == N_Procedure_Call_Statement
4088 && Name (Parent (gnat_node)) != gnat_node)
4089 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
4090 && !AGGREGATE_TYPE_P (gnu_result_type)
4091 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
4092 || Nkind (Parent (gnat_node)) == N_Parameter_Association)
4093 && !(TYPE_SIZE (gnu_result_type)
4094 && TYPE_SIZE (TREE_TYPE (gnu_result))
4095 && (AGGREGATE_TYPE_P (gnu_result_type)
4096 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
4097 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
4098 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
4099 != INTEGER_CST))
4100 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4101 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
4102 && (CONTAINS_PLACEHOLDER_P
4103 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
4104 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
4105 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_result_type))))
4107 /* In this case remove padding only if the inner object is of
4108 self-referential size: in that case it must be an object of
4109 unconstrained type with a default discriminant. In other cases,
4110 we want to avoid copying too much data. */
4111 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
4112 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
4113 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE
4114 (TREE_TYPE (TYPE_FIELDS
4115 (TREE_TYPE (gnu_result))))))
4116 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4117 gnu_result);
4120 else if (TREE_CODE (gnu_result) == LABEL_DECL
4121 || TREE_CODE (gnu_result) == FIELD_DECL
4122 || TREE_CODE (gnu_result) == ERROR_MARK
4123 || (TYPE_SIZE (gnu_result_type)
4124 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4125 && TREE_CODE (gnu_result) != INDIRECT_REF
4126 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
4127 || ((TYPE_NAME (gnu_result_type)
4128 == TYPE_NAME (TREE_TYPE (gnu_result)))
4129 && TREE_CODE (gnu_result_type) == RECORD_TYPE
4130 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
4131 && TYPE_MODE (gnu_result_type) == BLKmode
4132 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (gnu_result)))
4133 == MODE_INT)))
4135 /* Remove any padding record, but do nothing more in this case. */
4136 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
4137 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4138 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4139 gnu_result);
4142 else if (gnu_result == error_mark_node
4143 || gnu_result_type == void_type_node)
4144 gnu_result = error_mark_node;
4145 else if (gnu_result_type != TREE_TYPE (gnu_result))
4146 gnu_result = convert (gnu_result_type, gnu_result);
4148 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RESULT. */
4149 while ((TREE_CODE (gnu_result) == NOP_EXPR
4150 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
4151 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
4152 gnu_result = TREE_OPERAND (gnu_result, 0);
4154 return gnu_result;
4157 /* Record the current code position in GNAT_NODE. */
4159 static void
4160 record_code_position (Node_Id gnat_node)
4162 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
4164 add_stmt_with_node (stmt_stmt, gnat_node);
4165 save_gnu_tree (gnat_node, stmt_stmt, true);
4168 /* Insert the code for GNAT_NODE at the position saved for that node. */
4170 static void
4171 insert_code_for (Node_Id gnat_node)
4173 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
4174 save_gnu_tree (gnat_node, NULL_TREE, true);
4177 /* Start a new statement group chained to the previous group. */
4179 static void
4180 start_stmt_group ()
4182 struct stmt_group *group = stmt_group_free_list;
4184 /* First see if we can get one from the free list. */
4185 if (group)
4186 stmt_group_free_list = group->previous;
4187 else
4188 group = (struct stmt_group *) ggc_alloc (sizeof (struct stmt_group));
4190 group->previous = current_stmt_group;
4191 group->stmt_list = group->block = group->cleanups = NULL_TREE;
4192 current_stmt_group = group;
4195 /* Add GNU_STMT to the current statement group. */
4197 void
4198 add_stmt (tree gnu_stmt)
4200 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
4202 /* If we're at top level, show everything in here is in use in case
4203 any of it is shared by a subprogram. */
4204 if (global_bindings_p ())
4205 walk_tree (&gnu_stmt, mark_visited, NULL, NULL);
4209 /* Similar, but set the location of GNU_STMT to that of GNAT_NODE. */
4211 void
4212 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
4214 if (Present (gnat_node))
4215 annotate_with_node (gnu_stmt, gnat_node);
4216 add_stmt (gnu_stmt);
4219 /* Add a declaration statement for GNU_DECL to the current statement group.
4220 Get SLOC from Entity_Id. */
4222 void
4223 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
4225 tree gnu_stmt;
4227 /* If this is a variable that Gigi is to ignore, we may have been given
4228 an ERROR_MARK. So test for it. We also might have been given a
4229 reference for a renaming. So only do something for a decl. Also
4230 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
4231 if (!DECL_P (gnu_decl)
4232 || (TREE_CODE (gnu_decl) == TYPE_DECL
4233 && TREE_CODE (TREE_TYPE (gnu_decl)) == UNCONSTRAINED_ARRAY_TYPE))
4234 return;
4236 /* If we are global, we don't want to actually output the DECL_EXPR for
4237 this decl since we already have evaluated the expressions in the
4238 sizes and positions as globals and doing it again would be wrong.
4239 But we do have to mark everything as used. */
4240 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
4241 if (!global_bindings_p ())
4242 add_stmt_with_node (gnu_stmt, gnat_entity);
4243 else
4245 walk_tree (&gnu_stmt, mark_visited, NULL, NULL);
4246 if (TREE_CODE (gnu_decl) == VAR_DECL
4247 || TREE_CODE (gnu_decl) == CONST_DECL)
4249 walk_tree (&DECL_SIZE (gnu_decl), mark_visited, NULL, NULL);
4250 walk_tree (&DECL_SIZE_UNIT (gnu_decl), mark_visited, NULL, NULL);
4251 walk_tree (&DECL_INITIAL (gnu_decl), mark_visited, NULL, NULL);
4255 /* If this is a DECL_EXPR for a variable with DECL_INITIAl set,
4256 there are two cases we need to handle here. */
4257 if (TREE_CODE (gnu_decl) == VAR_DECL && DECL_INITIAL (gnu_decl))
4259 tree gnu_init = DECL_INITIAL (gnu_decl);
4260 tree gnu_lhs = NULL_TREE;
4262 /* If this is a DECL_EXPR for a variable with DECL_INITIAL set
4263 and decl has a padded type, convert it to the unpadded type so the
4264 assignment is done properly. */
4265 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
4266 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_decl)))
4267 gnu_lhs
4268 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_decl))), gnu_decl);
4270 /* Otherwise, if this is going into memory and the initializer isn't
4271 valid for the assembler and loader. Gimplification could do this,
4272 but would be run too late if -fno-unit-at-a-time. */
4273 else if (TREE_STATIC (gnu_decl)
4274 && !initializer_constant_valid_p (gnu_init,
4275 TREE_TYPE (gnu_decl)))
4276 gnu_lhs = gnu_decl;
4278 if (gnu_lhs)
4280 tree gnu_assign_stmt
4281 = build_binary_op (MODIFY_EXPR, NULL_TREE,
4282 gnu_lhs, DECL_INITIAL (gnu_decl));
4284 DECL_INITIAL (gnu_decl) = 0;
4285 TREE_READONLY (gnu_decl) = 0;
4286 annotate_with_locus (gnu_assign_stmt,
4287 DECL_SOURCE_LOCATION (gnu_decl));
4288 add_stmt (gnu_assign_stmt);
4293 /* Utility function to mark nodes with TREE_VISITED. Called from walk_tree.
4294 We use this to indicate all variable sizes and positions in global types
4295 may not be shared by any subprogram. */
4297 static tree
4298 mark_visited (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4300 if (TREE_VISITED (*tp))
4301 *walk_subtrees = 0;
4303 /* Don't mark a dummy type as visited because we want to mark its sizes
4304 and fields once it's filled in. */
4305 else if (!TYPE_IS_DUMMY_P (*tp))
4306 TREE_VISITED (*tp) = 1;
4308 return NULL_TREE;
4311 /* Likewise, but to mark as unvisited. */
4313 static tree
4314 mark_unvisited (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4315 void *data ATTRIBUTE_UNUSED)
4317 TREE_VISITED (*tp) = 0;
4319 return NULL_TREE;
4322 /* Add GNU_CLEANUP, a cleanup action, to the current code group. */
4324 static void
4325 add_cleanup (tree gnu_cleanup)
4327 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
4330 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
4332 void
4333 set_block_for_group (tree gnu_block)
4335 if (current_stmt_group->block)
4336 abort ();
4338 current_stmt_group->block = gnu_block;
4341 /* Return code corresponding to the current code group. It is normally
4342 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
4343 BLOCK or cleanups were set. */
4345 static tree
4346 end_stmt_group ()
4348 struct stmt_group *group = current_stmt_group;
4349 tree gnu_retval = group->stmt_list;
4351 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
4352 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
4353 make a BIND_EXPR. Note that we nest in that because the cleanup may
4354 reference variables in the block. */
4355 if (gnu_retval == NULL_TREE)
4356 gnu_retval = alloc_stmt_list ();
4358 if (group->cleanups)
4359 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
4360 group->cleanups);
4362 if (current_stmt_group->block)
4363 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
4364 gnu_retval, group->block);
4366 /* Remove this group from the stack and add it to the free list. */
4367 current_stmt_group = group->previous;
4368 group->previous = stmt_group_free_list;
4369 stmt_group_free_list = group;
4371 return gnu_retval;
4374 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
4375 statements.*/
4377 static void
4378 add_stmt_list (List_Id gnat_list)
4380 Node_Id gnat_node;
4382 if (Present (gnat_list))
4383 for (gnat_node = First (gnat_list); Present (gnat_node);
4384 gnat_node = Next (gnat_node))
4385 add_stmt (gnat_to_gnu (gnat_node));
4388 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
4389 If BINDING_P is true, push and pop a binding level around the list. */
4391 static tree
4392 build_stmt_group (List_Id gnat_list, bool binding_p)
4394 start_stmt_group ();
4395 if (binding_p)
4396 gnat_pushlevel ();
4398 add_stmt_list (gnat_list);
4399 if (binding_p)
4400 gnat_poplevel ();
4402 return end_stmt_group ();
4405 /* Push and pop routines for stacks. We keep a free list around so we
4406 don't waste tree nodes. */
4408 static void
4409 push_stack (tree *gnu_stack_ptr, tree gnu_purpose, tree gnu_value)
4411 tree gnu_node = gnu_stack_free_list;
4413 if (gnu_node)
4415 gnu_stack_free_list = TREE_CHAIN (gnu_node);
4416 TREE_CHAIN (gnu_node) = *gnu_stack_ptr;
4417 TREE_PURPOSE (gnu_node) = gnu_purpose;
4418 TREE_VALUE (gnu_node) = gnu_value;
4420 else
4421 gnu_node = tree_cons (gnu_purpose, gnu_value, *gnu_stack_ptr);
4423 *gnu_stack_ptr = gnu_node;
4426 static void
4427 pop_stack (tree *gnu_stack_ptr)
4429 tree gnu_node = *gnu_stack_ptr;
4431 *gnu_stack_ptr = TREE_CHAIN (gnu_node);
4432 TREE_CHAIN (gnu_node) = gnu_stack_free_list;
4433 gnu_stack_free_list = gnu_node;
4436 /* GNU_STMT is a statement. We generate code for that statement. */
4438 void
4439 gnat_expand_stmt (tree gnu_stmt)
4441 #if 0
4442 tree gnu_elmt, gnu_elmt_2;
4443 #endif
4445 switch (TREE_CODE (gnu_stmt))
4447 #if 0
4448 case USE_STMT:
4449 /* First write a volatile ASM_INPUT to prevent anything from being
4450 moved. */
4451 gnu_elmt = gen_rtx_ASM_INPUT (VOIDmode, "");
4452 MEM_VOLATILE_P (gnu_elmt) = 1;
4453 emit_insn (gnu_elmt);
4455 gnu_elmt = expand_expr (TREE_OPERAND (gnu_stmt, 0), NULL_RTX, VOIDmode,
4456 modifier);
4457 emit_insn (gen_rtx_USE (VOIDmode, ));
4458 return target;
4459 #endif
4461 default:
4462 abort ();
4466 /* Generate GIMPLE in place for the expression at *EXPR_P. */
4469 gnat_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
4471 tree expr = *expr_p;
4473 if (IS_ADA_STMT (expr))
4474 return gnat_gimplify_stmt (expr_p);
4476 switch (TREE_CODE (expr))
4478 case NULL_EXPR:
4479 /* If this is for a scalar, just make a VAR_DECL for it. If for
4480 an aggregate, get a null pointer of the appropriate type and
4481 dereference it. */
4482 if (AGGREGATE_TYPE_P (TREE_TYPE (expr)))
4483 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (expr),
4484 convert (build_pointer_type (TREE_TYPE (expr)),
4485 integer_zero_node));
4486 else
4488 *expr_p = create_tmp_var (TREE_TYPE (expr), NULL);
4489 TREE_NO_WARNING (*expr_p) = 1;
4492 append_to_statement_list (TREE_OPERAND (expr, 0), pre_p);
4493 return GS_OK;
4495 case UNCONSTRAINED_ARRAY_REF:
4496 /* We should only do this if we are just elaborating for side-effects,
4497 but we can't know that yet. */
4498 *expr_p = TREE_OPERAND (*expr_p, 0);
4499 return GS_OK;
4501 case ADDR_EXPR:
4502 /* If we're taking the address of a constant CONSTRUCTOR, force it to
4503 be put into static memory. We know it's going to be readonly given
4504 the semantics we have and it's required to be static memory in
4505 the case when the reference is in an elaboration procedure. */
4506 if (TREE_CODE (TREE_OPERAND (expr, 0)) == CONSTRUCTOR
4507 && TREE_CONSTANT (TREE_OPERAND (expr, 0)))
4509 tree new_var
4510 = create_tmp_var (TREE_TYPE (TREE_OPERAND (expr, 0)), "C");
4512 TREE_READONLY (new_var) = 1;
4513 TREE_STATIC (new_var) = 1;
4514 TREE_ADDRESSABLE (new_var) = 1;
4516 gimplify_and_add (build2 (MODIFY_EXPR, TREE_TYPE (new_var),
4517 new_var, TREE_OPERAND (expr, 0)),
4518 pre_p);
4520 TREE_OPERAND (expr, 0) = new_var;
4521 return GS_ALL_DONE;
4523 return GS_UNHANDLED;
4525 case COMPONENT_REF:
4526 /* We have a kludge here. If the FIELD_DECL is from a fat pointer and is
4527 from an early dummy type, replace it with the proper FIELD_DECL. */
4528 if (TYPE_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (*expr_p, 0)))
4529 && DECL_ORIGINAL_FIELD (TREE_OPERAND (*expr_p, 1)))
4531 TREE_OPERAND (*expr_p, 1)
4532 = DECL_ORIGINAL_FIELD (TREE_OPERAND (*expr_p, 1));
4533 return GS_OK;
4536 /* ... fall through ... */
4538 default:
4539 return GS_UNHANDLED;
4543 /* Generate GIMPLE in place for the statement at *STMT_P. */
4545 static enum gimplify_status
4546 gnat_gimplify_stmt (tree *stmt_p)
4548 tree stmt = *stmt_p;
4550 switch (TREE_CODE (stmt))
4552 case STMT_STMT:
4553 *stmt_p = STMT_STMT_STMT (stmt);
4554 return GS_OK;
4556 case USE_STMT:
4557 *stmt_p = NULL_TREE;
4558 return GS_ALL_DONE;
4560 case LOOP_STMT:
4562 tree gnu_start_label = create_artificial_label ();
4563 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
4565 /* Set to emit the statements of the loop. */
4566 *stmt_p = NULL_TREE;
4568 /* We first emit the start label and then a conditional jump to
4569 the end label if there's a top condition, then the body of the
4570 loop, then a conditional branch to the end label, then the update,
4571 if any, and finally a jump to the start label and the definition
4572 of the end label. */
4573 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
4574 gnu_start_label),
4575 stmt_p);
4577 if (LOOP_STMT_TOP_COND (stmt))
4578 append_to_statement_list (build3 (COND_EXPR, void_type_node,
4579 LOOP_STMT_TOP_COND (stmt),
4580 alloc_stmt_list (),
4581 build1 (GOTO_EXPR,
4582 void_type_node,
4583 gnu_end_label)),
4584 stmt_p);
4586 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
4588 if (LOOP_STMT_BOT_COND (stmt))
4589 append_to_statement_list (build3 (COND_EXPR, void_type_node,
4590 LOOP_STMT_BOT_COND (stmt),
4591 alloc_stmt_list (),
4592 build1 (GOTO_EXPR,
4593 void_type_node,
4594 gnu_end_label)),
4595 stmt_p);
4597 if (LOOP_STMT_UPDATE (stmt))
4598 append_to_statement_list (LOOP_STMT_UPDATE (stmt), stmt_p);
4600 append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
4601 gnu_start_label),
4602 stmt_p);
4603 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
4604 gnu_end_label),
4605 stmt_p);
4606 return GS_OK;
4609 case EXIT_STMT:
4610 /* Build a statement to jump to the corresponding end label, then
4611 see if it needs to be conditional. */
4612 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
4613 if (EXIT_STMT_COND (stmt))
4614 *stmt_p = build3 (COND_EXPR, void_type_node,
4615 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
4616 return GS_OK;
4618 default:
4619 abort ();
4623 /* Force references to each of the entities in packages GNAT_NODE with's
4624 so that the debugging information for all of them are identical
4625 in all clients. Operate recursively on anything it with's, but check
4626 that we aren't elaborating something more than once. */
4628 /* The reason for this routine's existence is two-fold.
4629 First, with some debugging formats, notably MDEBUG on SGI
4630 IRIX, the linker will remove duplicate debugging information if two
4631 clients have identical debugguing information. With the normal scheme
4632 of elaboration, this does not usually occur, since entities in with'ed
4633 packages are elaborated on demand, and if clients have different usage
4634 patterns, the normal case, then the order and selection of entities
4635 will differ. In most cases however, it seems that linkers do not know
4636 how to eliminate duplicate debugging information, even if it is
4637 identical, so the use of this routine would increase the total amount
4638 of debugging information in the final executable.
4640 Second, this routine is called in type_annotate mode, to compute DDA
4641 information for types in withed units, for ASIS use */
4643 static void
4644 elaborate_all_entities (Node_Id gnat_node)
4646 Entity_Id gnat_with_clause, gnat_entity;
4648 /* Process each unit only once. As we trace the context of all relevant
4649 units transitively, including generic bodies, we may encounter the
4650 same generic unit repeatedly */
4652 if (!present_gnu_tree (gnat_node))
4653 save_gnu_tree (gnat_node, integer_zero_node, true);
4655 /* Save entities in all context units. A body may have an implicit_with
4656 on its own spec, if the context includes a child unit, so don't save
4657 the spec twice. */
4659 for (gnat_with_clause = First (Context_Items (gnat_node));
4660 Present (gnat_with_clause);
4661 gnat_with_clause = Next (gnat_with_clause))
4662 if (Nkind (gnat_with_clause) == N_With_Clause
4663 && !present_gnu_tree (Library_Unit (gnat_with_clause))
4664 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
4666 elaborate_all_entities (Library_Unit (gnat_with_clause));
4668 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
4670 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
4671 Present (gnat_entity);
4672 gnat_entity = Next_Entity (gnat_entity))
4673 if (Is_Public (gnat_entity)
4674 && Convention (gnat_entity) != Convention_Intrinsic
4675 && Ekind (gnat_entity) != E_Package
4676 && Ekind (gnat_entity) != E_Package_Body
4677 && Ekind (gnat_entity) != E_Operator
4678 && !(IN (Ekind (gnat_entity), Type_Kind)
4679 && !Is_Frozen (gnat_entity))
4680 && !((Ekind (gnat_entity) == E_Procedure
4681 || Ekind (gnat_entity) == E_Function)
4682 && Is_Intrinsic_Subprogram (gnat_entity))
4683 && !IN (Ekind (gnat_entity), Named_Kind)
4684 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
4685 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4687 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
4689 Node_Id gnat_body
4690 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
4692 /* Retrieve compilation unit node of generic body. */
4693 while (Present (gnat_body)
4694 && Nkind (gnat_body) != N_Compilation_Unit)
4695 gnat_body = Parent (gnat_body);
4697 /* If body is available, elaborate its context. */
4698 if (Present (gnat_body))
4699 elaborate_all_entities (gnat_body);
4703 if (Nkind (Unit (gnat_node)) == N_Package_Body && type_annotate_only)
4704 elaborate_all_entities (Library_Unit (gnat_node));
4707 /* Do the processing of N_Freeze_Entity, GNAT_NODE. */
4709 static void
4710 process_freeze_entity (Node_Id gnat_node)
4712 Entity_Id gnat_entity = Entity (gnat_node);
4713 tree gnu_old;
4714 tree gnu_new;
4715 tree gnu_init
4716 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
4717 && present_gnu_tree (Declaration_Node (gnat_entity)))
4718 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
4720 /* If this is a package, need to generate code for the package. */
4721 if (Ekind (gnat_entity) == E_Package)
4723 insert_code_for
4724 (Parent (Corresponding_Body
4725 (Parent (Declaration_Node (gnat_entity)))));
4726 return;
4729 /* Check for old definition after the above call. This Freeze_Node
4730 might be for one its Itypes. */
4731 gnu_old
4732 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
4734 /* If this entity has an Address representation clause, GNU_OLD is the
4735 address, so discard it here. */
4736 if (Present (Address_Clause (gnat_entity)))
4737 gnu_old = 0;
4739 /* Don't do anything for class-wide types they are always
4740 transformed into their root type. */
4741 if (Ekind (gnat_entity) == E_Class_Wide_Type
4742 || (Ekind (gnat_entity) == E_Class_Wide_Subtype
4743 && Present (Equivalent_Type (gnat_entity))))
4744 return;
4746 /* Don't do anything for subprograms that may have been elaborated before
4747 their freeze nodes. This can happen, for example because of an inner call
4748 in an instance body. */
4749 if (gnu_old
4750 && TREE_CODE (gnu_old) == FUNCTION_DECL
4751 && (Ekind (gnat_entity) == E_Function
4752 || Ekind (gnat_entity) == E_Procedure))
4753 return;
4755 /* If we have a non-dummy type old tree, we have nothing to do. Unless
4756 this is the public view of a private type whose full view was not
4757 delayed, this node was never delayed as it should have been.
4758 Also allow this to happen for concurrent types since we may have
4759 frozen both the Corresponding_Record_Type and this type. */
4760 if (gnu_old
4761 && !(TREE_CODE (gnu_old) == TYPE_DECL
4762 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
4764 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4765 && Present (Full_View (gnat_entity))
4766 && No (Freeze_Node (Full_View (gnat_entity))))
4767 return;
4768 else if (Is_Concurrent_Type (gnat_entity))
4769 return;
4770 else
4771 abort ();
4774 /* Reset the saved tree, if any, and elaborate the object or type for real.
4775 If there is a full declaration, elaborate it and copy the type to
4776 GNAT_ENTITY. Likewise if this is the record subtype corresponding to
4777 a class wide type or subtype. */
4778 if (gnu_old)
4780 save_gnu_tree (gnat_entity, NULL_TREE, false);
4781 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4782 && Present (Full_View (gnat_entity))
4783 && present_gnu_tree (Full_View (gnat_entity)))
4784 save_gnu_tree (Full_View (gnat_entity), NULL_TREE, false);
4785 if (Present (Class_Wide_Type (gnat_entity))
4786 && Class_Wide_Type (gnat_entity) != gnat_entity)
4787 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
4790 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4791 && Present (Full_View (gnat_entity)))
4793 gnu_new = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 1);
4795 /* The above call may have defined this entity (the simplest example
4796 of this is when we have a private enumeral type since the bounds
4797 will have the public view. */
4798 if (!present_gnu_tree (gnat_entity))
4799 save_gnu_tree (gnat_entity, gnu_new, false);
4800 if (Present (Class_Wide_Type (gnat_entity))
4801 && Class_Wide_Type (gnat_entity) != gnat_entity)
4802 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
4804 else
4805 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
4807 /* If we've made any pointers to the old version of this type, we
4808 have to update them. */
4809 if (gnu_old)
4810 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
4811 TREE_TYPE (gnu_new));
4814 /* Process the list of inlined subprograms of GNAT_NODE, which is an
4815 N_Compilation_Unit. */
4817 static void
4818 process_inlined_subprograms (Node_Id gnat_node)
4820 Entity_Id gnat_entity;
4821 Node_Id gnat_body;
4823 /* If we can inline, generate RTL for all the inlined subprograms.
4824 Define the entity first so we set DECL_EXTERNAL. */
4825 if (optimize > 0 && !flag_no_inline)
4826 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
4827 Present (gnat_entity);
4828 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
4830 gnat_body = Parent (Declaration_Node (gnat_entity));
4832 if (Nkind (gnat_body) != N_Subprogram_Body)
4834 /* ??? This really should always be Present. */
4835 if (No (Corresponding_Body (gnat_body)))
4836 continue;
4838 gnat_body
4839 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
4842 if (Present (gnat_body))
4844 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4845 add_stmt (gnat_to_gnu (gnat_body));
4850 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
4851 We make two passes, one to elaborate anything other than bodies (but
4852 we declare a function if there was no spec). The second pass
4853 elaborates the bodies.
4855 GNAT_END_LIST gives the element in the list past the end. Normally,
4856 this is Empty, but can be First_Real_Statement for a
4857 Handled_Sequence_Of_Statements.
4859 We make a complete pass through both lists if PASS1P is true, then make
4860 the second pass over both lists if PASS2P is true. The lists usually
4861 correspond to the public and private parts of a package. */
4863 static void
4864 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
4865 Node_Id gnat_end_list, bool pass1p, bool pass2p)
4867 List_Id gnat_decl_array[2];
4868 Node_Id gnat_decl;
4869 int i;
4871 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
4873 if (pass1p)
4874 for (i = 0; i <= 1; i++)
4875 if (Present (gnat_decl_array[i]))
4876 for (gnat_decl = First (gnat_decl_array[i]);
4877 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
4879 /* For package specs, we recurse inside the declarations,
4880 thus taking the two pass approach inside the boundary. */
4881 if (Nkind (gnat_decl) == N_Package_Declaration
4882 && (Nkind (Specification (gnat_decl)
4883 == N_Package_Specification)))
4884 process_decls (Visible_Declarations (Specification (gnat_decl)),
4885 Private_Declarations (Specification (gnat_decl)),
4886 Empty, true, false);
4888 /* Similarly for any declarations in the actions of a
4889 freeze node. */
4890 else if (Nkind (gnat_decl) == N_Freeze_Entity)
4892 process_freeze_entity (gnat_decl);
4893 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
4896 /* Package bodies with freeze nodes get their elaboration deferred
4897 until the freeze node, but the code must be placed in the right
4898 place, so record the code position now. */
4899 else if (Nkind (gnat_decl) == N_Package_Body
4900 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
4901 record_code_position (gnat_decl);
4903 else if (Nkind (gnat_decl) == N_Package_Body_Stub
4904 && Present (Library_Unit (gnat_decl))
4905 && Present (Freeze_Node
4906 (Corresponding_Spec
4907 (Proper_Body (Unit
4908 (Library_Unit (gnat_decl)))))))
4909 record_code_position
4910 (Proper_Body (Unit (Library_Unit (gnat_decl))));
4912 /* We defer most subprogram bodies to the second pass. */
4913 else if (Nkind (gnat_decl) == N_Subprogram_Body)
4915 if (Acts_As_Spec (gnat_decl))
4917 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
4919 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
4920 && Ekind (gnat_subprog_id) != E_Generic_Function)
4921 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
4924 /* For bodies and stubs that act as their own specs, the entity
4925 itself must be elaborated in the first pass, because it may
4926 be used in other declarations. */
4927 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
4929 Node_Id gnat_subprog_id =
4930 Defining_Entity (Specification (gnat_decl));
4932 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
4933 && Ekind (gnat_subprog_id) != E_Generic_Procedure
4934 && Ekind (gnat_subprog_id) != E_Generic_Function)
4935 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
4938 /* Concurrent stubs stand for the corresponding subprogram bodies,
4939 which are deferred like other bodies. */
4940 else if (Nkind (gnat_decl) == N_Task_Body_Stub
4941 || Nkind (gnat_decl) == N_Protected_Body_Stub)
4943 else
4944 add_stmt (gnat_to_gnu (gnat_decl));
4947 /* Here we elaborate everything we deferred above except for package bodies,
4948 which are elaborated at their freeze nodes. Note that we must also
4949 go inside things (package specs and freeze nodes) the first pass did. */
4950 if (pass2p)
4951 for (i = 0; i <= 1; i++)
4952 if (Present (gnat_decl_array[i]))
4953 for (gnat_decl = First (gnat_decl_array[i]);
4954 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
4956 if (Nkind (gnat_decl) == N_Subprogram_Body
4957 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
4958 || Nkind (gnat_decl) == N_Task_Body_Stub
4959 || Nkind (gnat_decl) == N_Protected_Body_Stub)
4960 add_stmt (gnat_to_gnu (gnat_decl));
4962 else if (Nkind (gnat_decl) == N_Package_Declaration
4963 && (Nkind (Specification (gnat_decl)
4964 == N_Package_Specification)))
4965 process_decls (Visible_Declarations (Specification (gnat_decl)),
4966 Private_Declarations (Specification (gnat_decl)),
4967 Empty, false, true);
4969 else if (Nkind (gnat_decl) == N_Freeze_Entity)
4970 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
4974 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
4975 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
4976 which we have to check. */
4978 static tree
4979 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type)
4981 tree gnu_range_type = get_unpadded_type (gnat_range_type);
4982 tree gnu_low = TYPE_MIN_VALUE (gnu_range_type);
4983 tree gnu_high = TYPE_MAX_VALUE (gnu_range_type);
4984 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
4986 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
4987 we can't do anything since we might be truncating the bounds. No
4988 check is needed in this case. */
4989 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
4990 && (TYPE_PRECISION (gnu_compare_type)
4991 < TYPE_PRECISION (get_base_type (gnu_range_type))))
4992 return gnu_expr;
4994 /* Checked expressions must be evaluated only once. */
4995 gnu_expr = protect_multiple_eval (gnu_expr);
4997 /* There's no good type to use here, so we might as well use
4998 integer_type_node. Note that the form of the check is
4999 (not (expr >= lo)) or (not (expr >= hi))
5000 the reason for this slightly convoluted form is that NaN's
5001 are not considered to be in range in the float case. */
5002 return emit_check
5003 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5004 invert_truthvalue
5005 (build_binary_op (GE_EXPR, integer_type_node,
5006 convert (gnu_compare_type, gnu_expr),
5007 convert (gnu_compare_type, gnu_low))),
5008 invert_truthvalue
5009 (build_binary_op (LE_EXPR, integer_type_node,
5010 convert (gnu_compare_type, gnu_expr),
5011 convert (gnu_compare_type,
5012 gnu_high)))),
5013 gnu_expr, CE_Range_Check_Failed);
5016 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object
5017 which we are about to index, GNU_EXPR is the index expression to be
5018 checked, GNU_LOW and GNU_HIGH are the lower and upper bounds
5019 against which GNU_EXPR has to be checked. Note that for index
5020 checking we cannot use the emit_range_check function (although very
5021 similar code needs to be generated in both cases) since for index
5022 checking the array type against which we are checking the indeces
5023 may be unconstrained and consequently we need to retrieve the
5024 actual index bounds from the array object itself
5025 (GNU_ARRAY_OBJECT). The place where we need to do that is in
5026 subprograms having unconstrained array formal parameters */
5028 static tree
5029 emit_index_check (tree gnu_array_object,
5030 tree gnu_expr,
5031 tree gnu_low,
5032 tree gnu_high)
5034 tree gnu_expr_check;
5036 /* Checked expressions must be evaluated only once. */
5037 gnu_expr = protect_multiple_eval (gnu_expr);
5039 /* Must do this computation in the base type in case the expression's
5040 type is an unsigned subtypes. */
5041 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
5043 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
5044 the object we are handling. */
5045 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
5046 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
5048 /* There's no good type to use here, so we might as well use
5049 integer_type_node. */
5050 return emit_check
5051 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5052 build_binary_op (LT_EXPR, integer_type_node,
5053 gnu_expr_check,
5054 convert (TREE_TYPE (gnu_expr_check),
5055 gnu_low)),
5056 build_binary_op (GT_EXPR, integer_type_node,
5057 gnu_expr_check,
5058 convert (TREE_TYPE (gnu_expr_check),
5059 gnu_high))),
5060 gnu_expr, CE_Index_Check_Failed);
5063 /* GNU_COND contains the condition corresponding to an access, discriminant or
5064 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
5065 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
5066 REASON is the code that says why the exception was raised. */
5068 static tree
5069 emit_check (tree gnu_cond, tree gnu_expr, int reason)
5071 tree gnu_call;
5072 tree gnu_result;
5074 gnu_call = build_call_raise (reason);
5076 /* Use an outer COMPOUND_EXPR to make sure that GNU_EXPR will get evaluated
5077 in front of the comparison in case it ends up being a SAVE_EXPR. Put the
5078 whole thing inside its own SAVE_EXPR so the inner SAVE_EXPR doesn't leak
5079 out. */
5080 gnu_result = fold (build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
5081 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr),
5082 gnu_call, gnu_expr),
5083 gnu_expr));
5085 /* If GNU_EXPR has side effects, make the outer COMPOUND_EXPR and
5086 protect it. Otherwise, show GNU_RESULT has no side effects: we
5087 don't need to evaluate it just for the check. */
5088 if (TREE_SIDE_EFFECTS (gnu_expr))
5089 gnu_result
5090 = build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_expr, gnu_result);
5091 else
5092 TREE_SIDE_EFFECTS (gnu_result) = 0;
5094 /* ??? Unfortunately, if we don't put a SAVE_EXPR around this whole thing,
5095 we will repeatedly do the test. It would be nice if GCC was able
5096 to optimize this and only do it once. */
5097 return save_expr (gnu_result);
5100 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing
5101 overflow checks if OVERFLOW_P is nonzero and range checks if
5102 RANGE_P is nonzero. GNAT_TYPE is known to be an integral type.
5103 If TRUNCATE_P is nonzero, do a float to integer conversion with
5104 truncation; otherwise round. */
5106 static tree
5107 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
5108 bool rangep, bool truncatep)
5110 tree gnu_type = get_unpadded_type (gnat_type);
5111 tree gnu_in_type = TREE_TYPE (gnu_expr);
5112 tree gnu_in_basetype = get_base_type (gnu_in_type);
5113 tree gnu_base_type = get_base_type (gnu_type);
5114 tree gnu_ada_base_type = get_ada_base_type (gnu_type);
5115 tree gnu_result = gnu_expr;
5117 /* If we are not doing any checks, the output is an integral type, and
5118 the input is not a floating type, just do the conversion. This
5119 shortcut is required to avoid problems with packed array types
5120 and simplifies code in all cases anyway. */
5121 if (!rangep && !overflowp && INTEGRAL_TYPE_P (gnu_base_type)
5122 && !FLOAT_TYPE_P (gnu_in_type))
5123 return convert (gnu_type, gnu_expr);
5125 /* First convert the expression to its base type. This
5126 will never generate code, but makes the tests below much simpler.
5127 But don't do this if converting from an integer type to an unconstrained
5128 array type since then we need to get the bounds from the original
5129 (unpacked) type. */
5130 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5131 gnu_result = convert (gnu_in_basetype, gnu_result);
5133 /* If overflow checks are requested, we need to be sure the result will
5134 fit in the output base type. But don't do this if the input
5135 is integer and the output floating-point. */
5136 if (overflowp
5137 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
5139 /* Ensure GNU_EXPR only gets evaluated once. */
5140 tree gnu_input = protect_multiple_eval (gnu_result);
5141 tree gnu_cond = integer_zero_node;
5142 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
5143 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
5144 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
5145 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
5147 /* Convert the lower bounds to signed types, so we're sure we're
5148 comparing them properly. Likewise, convert the upper bounds
5149 to unsigned types. */
5150 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
5151 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
5153 if (INTEGRAL_TYPE_P (gnu_in_basetype)
5154 && !TYPE_UNSIGNED (gnu_in_basetype))
5155 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
5157 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
5158 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
5160 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
5161 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
5163 /* Check each bound separately and only if the result bound
5164 is tighter than the bound on the input type. Note that all the
5165 types are base types, so the bounds must be constant. Also,
5166 the comparison is done in the base type of the input, which
5167 always has the proper signedness. First check for input
5168 integer (which means output integer), output float (which means
5169 both float), or mixed, in which case we always compare.
5170 Note that we have to do the comparison which would *fail* in the
5171 case of an error since if it's an FP comparison and one of the
5172 values is a NaN or Inf, the comparison will fail. */
5173 if (INTEGRAL_TYPE_P (gnu_in_basetype)
5174 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
5175 : (FLOAT_TYPE_P (gnu_base_type)
5176 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
5177 TREE_REAL_CST (gnu_out_lb))
5178 : 1))
5179 gnu_cond
5180 = invert_truthvalue
5181 (build_binary_op (GE_EXPR, integer_type_node,
5182 gnu_input, convert (gnu_in_basetype,
5183 gnu_out_lb)));
5185 if (INTEGRAL_TYPE_P (gnu_in_basetype)
5186 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
5187 : (FLOAT_TYPE_P (gnu_base_type)
5188 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
5189 TREE_REAL_CST (gnu_in_lb))
5190 : 1))
5191 gnu_cond
5192 = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node, gnu_cond,
5193 invert_truthvalue
5194 (build_binary_op (LE_EXPR, integer_type_node,
5195 gnu_input,
5196 convert (gnu_in_basetype,
5197 gnu_out_ub))));
5199 if (!integer_zerop (gnu_cond))
5200 gnu_result = emit_check (gnu_cond, gnu_input,
5201 CE_Overflow_Check_Failed);
5204 /* Now convert to the result base type. If this is a non-truncating
5205 float-to-integer conversion, round. */
5206 if (INTEGRAL_TYPE_P (gnu_ada_base_type) && FLOAT_TYPE_P (gnu_in_basetype)
5207 && !truncatep)
5209 tree gnu_point_5 = build_real (gnu_in_basetype, dconstp5);
5210 tree gnu_minus_point_5 = build_real (gnu_in_basetype, dconstmp5);
5211 tree gnu_zero = convert (gnu_in_basetype, integer_zero_node);
5212 tree gnu_saved_result = save_expr (gnu_result);
5213 tree gnu_comp = build2 (GE_EXPR, integer_type_node,
5214 gnu_saved_result, gnu_zero);
5215 tree gnu_adjust = build3 (COND_EXPR, gnu_in_basetype, gnu_comp,
5216 gnu_point_5, gnu_minus_point_5);
5218 gnu_result
5219 = build2 (PLUS_EXPR, gnu_in_basetype, gnu_saved_result, gnu_adjust);
5222 if (TREE_CODE (gnu_ada_base_type) == INTEGER_TYPE
5223 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_ada_base_type)
5224 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
5225 gnu_result = unchecked_convert (gnu_ada_base_type, gnu_result, false);
5226 else
5227 gnu_result = convert (gnu_ada_base_type, gnu_result);
5229 /* Finally, do the range check if requested. Note that if the
5230 result type is a modular type, the range check is actually
5231 an overflow check. */
5233 if (rangep
5234 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
5235 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
5236 gnu_result = emit_range_check (gnu_result, gnat_type);
5238 return convert (gnu_type, gnu_result);
5241 /* Return 1 if GNU_EXPR can be directly addressed. This is the case unless
5242 it is an expression involving computation or if it involves a bitfield
5243 reference. This returns the same as gnat_mark_addressable in most
5244 cases. */
5246 static bool
5247 addressable_p (tree gnu_expr)
5249 switch (TREE_CODE (gnu_expr))
5251 case VAR_DECL:
5252 case PARM_DECL:
5253 case FUNCTION_DECL:
5254 case RESULT_DECL:
5255 /* All DECLs are addressable: if they are in a register, we can force
5256 them to memory. */
5257 return true;
5259 case UNCONSTRAINED_ARRAY_REF:
5260 case INDIRECT_REF:
5261 case CONSTRUCTOR:
5262 case NULL_EXPR:
5263 case SAVE_EXPR:
5264 return true;
5266 case COMPONENT_REF:
5267 return (!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
5268 && (!DECL_NONADDRESSABLE_P (TREE_OPERAND (gnu_expr, 1))
5269 || !flag_strict_aliasing)
5270 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
5272 case ARRAY_REF: case ARRAY_RANGE_REF:
5273 case REALPART_EXPR: case IMAGPART_EXPR:
5274 case NOP_EXPR:
5275 return addressable_p (TREE_OPERAND (gnu_expr, 0));
5277 case CONVERT_EXPR:
5278 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
5279 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
5281 case VIEW_CONVERT_EXPR:
5283 /* This is addressable if we can avoid a copy. */
5284 tree type = TREE_TYPE (gnu_expr);
5285 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
5287 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
5288 && (TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
5289 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
5290 || ((TYPE_MODE (type) == BLKmode
5291 || TYPE_MODE (inner_type) == BLKmode)
5292 && (TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
5293 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
5294 || TYPE_ALIGN_OK (type)
5295 || TYPE_ALIGN_OK (inner_type))))
5296 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
5299 default:
5300 return false;
5304 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
5305 a separate Freeze node exists, delay the bulk of the processing. Otherwise
5306 make a GCC type for GNAT_ENTITY and set up the correspondance. */
5308 void
5309 process_type (Entity_Id gnat_entity)
5311 tree gnu_old
5312 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
5313 tree gnu_new;
5315 /* If we are to delay elaboration of this type, just do any
5316 elaborations needed for expressions within the declaration and
5317 make a dummy type entry for this node and its Full_View (if
5318 any) in case something points to it. Don't do this if it
5319 has already been done (the only way that can happen is if
5320 the private completion is also delayed). */
5321 if (Present (Freeze_Node (gnat_entity))
5322 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
5323 && Present (Full_View (gnat_entity))
5324 && Freeze_Node (Full_View (gnat_entity))
5325 && !present_gnu_tree (Full_View (gnat_entity))))
5327 elaborate_entity (gnat_entity);
5329 if (!gnu_old)
5331 tree gnu_decl = create_type_decl (get_entity_name (gnat_entity),
5332 make_dummy_type (gnat_entity),
5333 NULL, false, false, gnat_entity);
5335 save_gnu_tree (gnat_entity, gnu_decl, false);
5336 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
5337 && Present (Full_View (gnat_entity)))
5338 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
5341 return;
5344 /* If we saved away a dummy type for this node it means that this
5345 made the type that corresponds to the full type of an incomplete
5346 type. Clear that type for now and then update the type in the
5347 pointers. */
5348 if (gnu_old)
5350 if (TREE_CODE (gnu_old) != TYPE_DECL
5351 || !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)))
5353 /* If this was a withed access type, this is not an error
5354 and merely indicates we've already elaborated the type
5355 already. */
5356 if (Is_Type (gnat_entity) && From_With_Type (gnat_entity))
5357 return;
5359 abort ();
5362 save_gnu_tree (gnat_entity, NULL_TREE, false);
5365 /* Now fully elaborate the type. */
5366 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
5367 if (TREE_CODE (gnu_new) != TYPE_DECL)
5368 abort ();
5370 /* If we have an old type and we've made pointers to this type,
5371 update those pointers. */
5372 if (gnu_old)
5373 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
5374 TREE_TYPE (gnu_new));
5376 /* If this is a record type corresponding to a task or protected type
5377 that is a completion of an incomplete type, perform a similar update
5378 on the type. */
5379 /* ??? Including protected types here is a guess. */
5381 if (IN (Ekind (gnat_entity), Record_Kind)
5382 && Is_Concurrent_Record_Type (gnat_entity)
5383 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
5385 tree gnu_task_old
5386 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
5388 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
5389 NULL_TREE, false);
5390 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
5391 gnu_new, false);
5393 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
5394 TREE_TYPE (gnu_new));
5398 /* GNAT_ASSOC is the front of the Component_Associations of an N_Aggregate.
5399 GNU_TYPE is the GCC type of the corresponding record.
5401 Return a CONSTRUCTOR to build the record. */
5403 static tree
5404 assoc_to_constructor (Node_Id gnat_assoc, tree gnu_type)
5406 tree gnu_field, gnu_list, gnu_result;
5408 /* We test for GNU_FIELD being empty in the case where a variant
5409 was the last thing since we don't take things off GNAT_ASSOC in
5410 that case. We check GNAT_ASSOC in case we have a variant, but it
5411 has no fields. */
5413 for (gnu_list = NULL_TREE; Present (gnat_assoc);
5414 gnat_assoc = Next (gnat_assoc))
5416 Node_Id gnat_field = First (Choices (gnat_assoc));
5417 tree gnu_field = gnat_to_gnu_entity (Entity (gnat_field), NULL_TREE, 0);
5418 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
5420 /* The expander is supposed to put a single component selector name
5421 in every record component association */
5422 if (Next (gnat_field))
5423 abort ();
5425 /* Before assigning a value in an aggregate make sure range checks
5426 are done if required. Then convert to the type of the field. */
5427 if (Do_Range_Check (Expression (gnat_assoc)))
5428 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field));
5430 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
5432 /* Add the field and expression to the list. */
5433 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
5436 gnu_result = extract_values (gnu_list, gnu_type);
5438 /* Verify every enty in GNU_LIST was used. */
5439 for (gnu_field = gnu_list; gnu_field; gnu_field = TREE_CHAIN (gnu_field))
5440 if (!TREE_ADDRESSABLE (gnu_field))
5441 abort ();
5443 return gnu_result;
5446 /* Builds a possibly nested constructor for array aggregates. GNAT_EXPR
5447 is the first element of an array aggregate. It may itself be an
5448 aggregate (an array or record aggregate). GNU_ARRAY_TYPE is the gnu type
5449 corresponding to the array aggregate. GNAT_COMPONENT_TYPE is the type
5450 of the array component. It is needed for range checking. */
5452 static tree
5453 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
5454 Entity_Id gnat_component_type)
5456 tree gnu_expr_list = NULL_TREE;
5457 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
5458 tree gnu_expr;
5460 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
5462 /* If the expression is itself an array aggregate then first build the
5463 innermost constructor if it is part of our array (multi-dimensional
5464 case). */
5466 if (Nkind (gnat_expr) == N_Aggregate
5467 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
5468 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
5469 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
5470 TREE_TYPE (gnu_array_type),
5471 gnat_component_type);
5472 else
5474 gnu_expr = gnat_to_gnu (gnat_expr);
5476 /* before assigning the element to the array make sure it is
5477 in range */
5478 if (Do_Range_Check (gnat_expr))
5479 gnu_expr = emit_range_check (gnu_expr, gnat_component_type);
5482 gnu_expr_list
5483 = tree_cons (gnu_index, convert (TREE_TYPE (gnu_array_type), gnu_expr),
5484 gnu_expr_list);
5486 gnu_index = int_const_binop (PLUS_EXPR, gnu_index, integer_one_node, 0);
5489 return gnat_build_constructor (gnu_array_type, nreverse (gnu_expr_list));
5492 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
5493 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
5494 of the associations that are from RECORD_TYPE. If we see an internal
5495 record, make a recursive call to fill it in as well. */
5497 static tree
5498 extract_values (tree values, tree record_type)
5500 tree result = NULL_TREE;
5501 tree field, tem;
5503 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
5505 tree value = 0;
5507 /* _Parent is an internal field, but may have values in the aggregate,
5508 so check for values first. */
5509 if ((tem = purpose_member (field, values)))
5511 value = TREE_VALUE (tem);
5512 TREE_ADDRESSABLE (tem) = 1;
5515 else if (DECL_INTERNAL_P (field))
5517 value = extract_values (values, TREE_TYPE (field));
5518 if (TREE_CODE (value) == CONSTRUCTOR && !CONSTRUCTOR_ELTS (value))
5519 value = 0;
5521 else
5522 /* If we have a record subtype, the names will match, but not the
5523 actual FIELD_DECLs. */
5524 for (tem = values; tem; tem = TREE_CHAIN (tem))
5525 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
5527 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
5528 TREE_ADDRESSABLE (tem) = 1;
5531 if (!value)
5532 continue;
5534 result = tree_cons (field, value, result);
5537 return gnat_build_constructor (record_type, nreverse (result));
5540 /* EXP is to be treated as an array or record. Handle the cases when it is
5541 an access object and perform the required dereferences. */
5543 static tree
5544 maybe_implicit_deref (tree exp)
5546 /* If the type is a pointer, dereference it. */
5548 if (POINTER_TYPE_P (TREE_TYPE (exp)) || TYPE_FAT_POINTER_P (TREE_TYPE (exp)))
5549 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
5551 /* If we got a padded type, remove it too. */
5552 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
5553 && TYPE_IS_PADDING_P (TREE_TYPE (exp)))
5554 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
5556 return exp;
5559 /* Protect EXP from multiple evaluation. This may make a SAVE_EXPR. */
5561 tree
5562 protect_multiple_eval (tree exp)
5564 tree type = TREE_TYPE (exp);
5566 /* If this has no side effects, we don't need to do anything. */
5567 if (!TREE_SIDE_EFFECTS (exp))
5568 return exp;
5570 /* If it is a conversion, protect what's inside the conversion.
5571 Similarly, if we're indirectly referencing something, we only
5572 actually need to protect the address since the data itself can't
5573 change in these situations. */
5574 else if (TREE_CODE (exp) == NON_LVALUE_EXPR
5575 || TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
5576 || TREE_CODE (exp) == VIEW_CONVERT_EXPR
5577 || TREE_CODE (exp) == INDIRECT_REF
5578 || TREE_CODE (exp) == UNCONSTRAINED_ARRAY_REF)
5579 return build1 (TREE_CODE (exp), type,
5580 protect_multiple_eval (TREE_OPERAND (exp, 0)));
5582 /* If EXP is a fat pointer or something that can be placed into a register,
5583 just make a SAVE_EXPR. */
5584 if (TYPE_FAT_POINTER_P (type) || TYPE_MODE (type) != BLKmode)
5585 return save_expr (exp);
5587 /* Otherwise, dereference, protect the address, and re-reference. */
5588 else
5589 return
5590 build_unary_op (INDIRECT_REF, type,
5591 save_expr (build_unary_op (ADDR_EXPR,
5592 build_reference_type (type),
5593 exp)));
5596 /* This is equivalent to stabilize_reference in GCC's tree.c, but we know
5597 how to handle our new nodes and we take an extra argument that says
5598 whether to force evaluation of everything. */
5600 tree
5601 gnat_stabilize_reference (tree ref, bool force)
5603 tree type = TREE_TYPE (ref);
5604 enum tree_code code = TREE_CODE (ref);
5605 tree result;
5607 switch (code)
5609 case VAR_DECL:
5610 case PARM_DECL:
5611 case RESULT_DECL:
5612 /* No action is needed in this case. */
5613 return ref;
5615 case NOP_EXPR:
5616 case CONVERT_EXPR:
5617 case FLOAT_EXPR:
5618 case FIX_TRUNC_EXPR:
5619 case FIX_FLOOR_EXPR:
5620 case FIX_ROUND_EXPR:
5621 case FIX_CEIL_EXPR:
5622 case VIEW_CONVERT_EXPR:
5623 case ADDR_EXPR:
5624 result
5625 = build1 (code, type,
5626 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force));
5627 break;
5629 case INDIRECT_REF:
5630 case UNCONSTRAINED_ARRAY_REF:
5631 result = build1 (code, type,
5632 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5633 force));
5634 break;
5636 case COMPONENT_REF:
5637 result = build3 (COMPONENT_REF, type,
5638 gnat_stabilize_reference (TREE_OPERAND (ref, 0),
5639 force),
5640 TREE_OPERAND (ref, 1), NULL_TREE);
5641 break;
5643 case BIT_FIELD_REF:
5644 result = build3 (BIT_FIELD_REF, type,
5645 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force),
5646 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
5647 force),
5648 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 2),
5649 force));
5650 break;
5652 case ARRAY_REF:
5653 case ARRAY_RANGE_REF:
5654 result = build4 (code, type,
5655 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force),
5656 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
5657 force),
5658 NULL_TREE, NULL_TREE);
5659 break;
5661 case COMPOUND_EXPR:
5662 result = build2 (COMPOUND_EXPR, type,
5663 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5664 force),
5665 gnat_stabilize_reference (TREE_OPERAND (ref, 1),
5666 force));
5667 break;
5669 /* If arg isn't a kind of lvalue we recognize, make no change.
5670 Caller should recognize the error for an invalid lvalue. */
5671 default:
5672 return ref;
5674 case ERROR_MARK:
5675 return error_mark_node;
5678 TREE_READONLY (result) = TREE_READONLY (ref);
5680 /* TREE_THIS_VOLATILE and TREE_SIDE_EFFECTS attached to the initial
5681 expression may not be sustained across some paths, such as the way via
5682 build1 for INDIRECT_REF. We re-populate those flags here for the general
5683 case, which is consistent with the GCC version of this routine.
5685 Special care should be taken regarding TREE_SIDE_EFFECTS, because some
5686 paths introduce side effects where there was none initially (e.g. calls
5687 to save_expr), and we also want to keep track of that. */
5689 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
5690 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (ref);
5692 return result;
5695 /* Similar to stabilize_reference_1 in tree.c, but supports an extra
5696 arg to force a SAVE_EXPR for everything. */
5698 static tree
5699 gnat_stabilize_reference_1 (tree e, bool force)
5701 enum tree_code code = TREE_CODE (e);
5702 tree type = TREE_TYPE (e);
5703 tree result;
5705 /* We cannot ignore const expressions because it might be a reference
5706 to a const array but whose index contains side-effects. But we can
5707 ignore things that are actual constant or that already have been
5708 handled by this function. */
5710 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
5711 return e;
5713 switch (TREE_CODE_CLASS (code))
5715 case tcc_exceptional:
5716 case tcc_type:
5717 case tcc_declaration:
5718 case tcc_comparison:
5719 case tcc_statement:
5720 case tcc_expression:
5721 case tcc_reference:
5722 /* If this is a COMPONENT_REF of a fat pointer, save the entire
5723 fat pointer. This may be more efficient, but will also allow
5724 us to more easily find the match for the PLACEHOLDER_EXPR. */
5725 if (code == COMPONENT_REF
5726 && TYPE_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (e, 0))))
5727 result = build3 (COMPONENT_REF, type,
5728 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
5729 force),
5730 TREE_OPERAND (e, 1), TREE_OPERAND (e, 2));
5731 else if (TREE_SIDE_EFFECTS (e) || force)
5732 return save_expr (e);
5733 else
5734 return e;
5735 break;
5737 case tcc_constant:
5738 /* Constants need no processing. In fact, we should never reach
5739 here. */
5740 return e;
5742 case tcc_binary:
5743 /* Recursively stabilize each operand. */
5744 result = build2 (code, type,
5745 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), force),
5746 gnat_stabilize_reference_1 (TREE_OPERAND (e, 1),
5747 force));
5748 break;
5750 case tcc_unary:
5751 /* Recursively stabilize each operand. */
5752 result = build1 (code, type,
5753 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
5754 force));
5755 break;
5757 default:
5758 abort ();
5761 TREE_READONLY (result) = TREE_READONLY (e);
5763 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
5764 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (e);
5765 return result;
5768 extern char *__gnat_to_canonical_file_spec (char *);
5770 /* Convert Sloc into *LOCUS (a location_t). Return true if this Sloc
5771 corresponds to a source code location and false if it doesn't. In the
5772 latter case, we don't update *LOCUS. We also set the Gigi global variable
5773 REF_FILENAME to the reference file name as given by sinput (i.e no
5774 directory). */
5776 bool
5777 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
5779 /* If node not from source code, ignore. */
5780 if (Sloc < 0)
5781 return false;
5783 /* Use the identifier table to make a hashed, permanent copy of the filename,
5784 since the name table gets reallocated after Gigi returns but before all
5785 the debugging information is output. The __gnat_to_canonical_file_spec
5786 call translates filenames from pragmas Source_Reference that contain host
5787 style syntax not understood by gdb. */
5788 locus->file
5789 = IDENTIFIER_POINTER
5790 (get_identifier
5791 (__gnat_to_canonical_file_spec
5792 (Get_Name_String (Full_Debug_Name (Get_Source_File_Index (Sloc))))));
5794 locus->line = Get_Logical_Line_Number (Sloc);
5796 ref_filename
5797 = IDENTIFIER_POINTER
5798 (get_identifier
5799 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
5801 return true;
5804 /* Similar to annotate_with_locus, but start with the Sloc of GNAT_NODE and
5805 don't do anything if it doesn't correspond to a source location. */
5807 static void
5808 annotate_with_node (tree node, Node_Id gnat_node)
5810 location_t locus;
5812 if (!Sloc_to_locus (Sloc (gnat_node), &locus))
5813 return;
5815 annotate_with_locus (node, locus);
5818 /* Post an error message. MSG is the error message, properly annotated.
5819 NODE is the node at which to post the error and the node to use for the
5820 "&" substitution. */
5822 void
5823 post_error (const char *msg, Node_Id node)
5825 String_Template temp;
5826 Fat_Pointer fp;
5828 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5829 fp.Array = msg, fp.Bounds = &temp;
5830 if (Present (node))
5831 Error_Msg_N (fp, node);
5834 /* Similar, but NODE is the node at which to post the error and ENT
5835 is the node to use for the "&" substitution. */
5837 void
5838 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
5840 String_Template temp;
5841 Fat_Pointer fp;
5843 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5844 fp.Array = msg, fp.Bounds = &temp;
5845 if (Present (node))
5846 Error_Msg_NE (fp, node, ent);
5849 /* Similar, but NODE is the node at which to post the error, ENT is the node
5850 to use for the "&" substitution, and N is the number to use for the ^. */
5852 void
5853 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int n)
5855 String_Template temp;
5856 Fat_Pointer fp;
5858 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5859 fp.Array = msg, fp.Bounds = &temp;
5860 Error_Msg_Uint_1 = UI_From_Int (n);
5862 if (Present (node))
5863 Error_Msg_NE (fp, node, ent);
5866 /* Similar to post_error_ne_num, but T is a GCC tree representing the
5867 number to write. If the tree represents a constant that fits within
5868 a host integer, the text inside curly brackets in MSG will be output
5869 (presumably including a '^'). Otherwise that text will not be output
5870 and the text inside square brackets will be output instead. */
5872 void
5873 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
5875 char *newmsg = alloca (strlen (msg) + 1);
5876 String_Template temp = {1, 0};
5877 Fat_Pointer fp;
5878 char start_yes, end_yes, start_no, end_no;
5879 const char *p;
5880 char *q;
5882 fp.Array = newmsg, fp.Bounds = &temp;
5884 if (host_integerp (t, 1)
5885 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_INT
5887 compare_tree_int
5888 (t, (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_INT - 1)) - 1)) < 0
5889 #endif
5892 Error_Msg_Uint_1 = UI_From_Int (tree_low_cst (t, 1));
5893 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
5895 else
5896 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
5898 for (p = msg, q = newmsg; *p; p++)
5900 if (*p == start_yes)
5901 for (p++; *p != end_yes; p++)
5902 *q++ = *p;
5903 else if (*p == start_no)
5904 for (p++; *p != end_no; p++)
5906 else
5907 *q++ = *p;
5910 *q = 0;
5912 temp.High_Bound = strlen (newmsg);
5913 if (Present (node))
5914 Error_Msg_NE (fp, node, ent);
5917 /* Similar to post_error_ne_tree, except that NUM is a second
5918 integer to write in the message. */
5920 void
5921 post_error_ne_tree_2 (const char *msg,
5922 Node_Id node,
5923 Entity_Id ent,
5924 tree t,
5925 int num)
5927 Error_Msg_Uint_2 = UI_From_Int (num);
5928 post_error_ne_tree (msg, node, ent, t);
5931 /* Set the node for a second '&' in the error message. */
5933 void
5934 set_second_error_entity (Entity_Id e)
5936 Error_Msg_Node_2 = e;
5939 /* Initialize the table that maps GNAT codes to GCC codes for simple
5940 binary and unary operations. */
5942 void
5943 init_code_table (void)
5945 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
5946 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
5948 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
5949 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
5950 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
5951 gnu_codes[N_Op_Eq] = EQ_EXPR;
5952 gnu_codes[N_Op_Ne] = NE_EXPR;
5953 gnu_codes[N_Op_Lt] = LT_EXPR;
5954 gnu_codes[N_Op_Le] = LE_EXPR;
5955 gnu_codes[N_Op_Gt] = GT_EXPR;
5956 gnu_codes[N_Op_Ge] = GE_EXPR;
5957 gnu_codes[N_Op_Add] = PLUS_EXPR;
5958 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
5959 gnu_codes[N_Op_Multiply] = MULT_EXPR;
5960 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
5961 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
5962 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
5963 gnu_codes[N_Op_Abs] = ABS_EXPR;
5964 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
5965 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
5966 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
5967 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
5968 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
5969 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
5972 #include "gt-ada-trans.h"