]>
Commit | Line | Data |
---|---|---|
a1ab4c31 AC |
1 | /**************************************************************************** |
2 | * * | |
3 | * GNAT COMPILER COMPONENTS * | |
4 | * * | |
5 | * T R A N S * | |
6 | * * | |
7 | * C Implementation File * | |
8 | * * | |
b2263428 | 9 | * Copyright (C) 1992-2023, Free Software Foundation, Inc. * |
a1ab4c31 AC |
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- * | |
748086b7 | 13 | * ware Foundation; either version 3, or (at your option) any later ver- * |
a1ab4c31 AC |
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 * | |
748086b7 JJ |
18 | * Public License distributed with GNAT; see file COPYING3. If not see * |
19 | * <http://www.gnu.org/licenses/>. * | |
a1ab4c31 AC |
20 | * * |
21 | * GNAT was originally developed by the GNAT team at New York University. * | |
22 | * Extensive contributions were provided by Ada Core Technologies Inc. * | |
23 | * * | |
24 | ****************************************************************************/ | |
25 | ||
26 | #include "config.h" | |
27 | #include "system.h" | |
28 | #include "coretypes.h" | |
2adfab87 AM |
29 | #include "target.h" |
30 | #include "function.h" | |
31 | #include "bitmap.h" | |
a1ab4c31 | 32 | #include "tree.h" |
2adfab87 | 33 | #include "gimple-expr.h" |
d8a2d370 | 34 | #include "stringpool.h" |
2adfab87 | 35 | #include "cgraph.h" |
64235766 | 36 | #include "predict.h" |
2adfab87 AM |
37 | #include "diagnostic.h" |
38 | #include "alias.h" | |
39 | #include "fold-const.h" | |
d8a2d370 DN |
40 | #include "stor-layout.h" |
41 | #include "stmt.h" | |
42 | #include "varasm.h" | |
a1ab4c31 | 43 | #include "output.h" |
fa6fd7b7 | 44 | #include "debug.h" |
d477d1fe | 45 | #include "libfuncs.h" /* For set_stack_check_libfunc. */ |
a1ab4c31 | 46 | #include "tree-iterator.h" |
2fb9a547 | 47 | #include "gimplify.h" |
f2423384 | 48 | #include "opts.h" |
48a24fcf | 49 | #include "common/common-target.h" |
7beb2b9d | 50 | #include "gomp-constants.h" |
314e6352 ML |
51 | #include "stringpool.h" |
52 | #include "attribs.h" | |
89576d86 | 53 | #include "tree-nested.h" |
8713b7e4 | 54 | |
a1ab4c31 | 55 | #include "ada.h" |
8713b7e4 | 56 | #include "adadecode.h" |
a1ab4c31 AC |
57 | #include "types.h" |
58 | #include "atree.h" | |
a1ab4c31 AC |
59 | #include "namet.h" |
60 | #include "nlists.h" | |
61 | #include "snames.h" | |
62 | #include "stringt.h" | |
63 | #include "uintp.h" | |
64 | #include "urealp.h" | |
65 | #include "fe.h" | |
66 | #include "sinfo.h" | |
67 | #include "einfo.h" | |
831f44c6 | 68 | #include "gadaint.h" |
a1ab4c31 AC |
69 | #include "ada-tree.h" |
70 | #include "gigi.h" | |
a1ab4c31 AC |
71 | |
72 | /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca, | |
73 | for fear of running out of stack space. If we need more, we use xmalloc | |
74 | instead. */ | |
75 | #define ALLOCA_THRESHOLD 1000 | |
76 | ||
831f44c6 | 77 | /* Pointers to front-end tables accessed through macros. */ |
99e30ba8 | 78 | Node_Header *Node_Offsets_Ptr; |
a6d3b499 | 79 | any_slot *Slots_Ptr; |
a1ab4c31 AC |
80 | Node_Id *Next_Node_Ptr; |
81 | Node_Id *Prev_Node_Ptr; | |
82 | struct Elist_Header *Elists_Ptr; | |
83 | struct Elmt_Item *Elmts_Ptr; | |
84 | struct String_Entry *Strings_Ptr; | |
85 | Char_Code *String_Chars_Ptr; | |
86 | struct List_Header *List_Headers_Ptr; | |
87 | ||
831f44c6 EB |
88 | /* Highest number in the front-end node table. */ |
89 | int max_gnat_nodes; | |
90 | ||
1e17ef87 | 91 | /* True when gigi is being called on an analyzed but unexpanded |
a1ab4c31 | 92 | tree, and the only purpose of the call is to properly annotate |
1e17ef87 | 93 | types with representation information. */ |
a1ab4c31 AC |
94 | bool type_annotate_only; |
95 | ||
f04b8d69 | 96 | /* List of N_Validate_Unchecked_Conversion nodes in the unit. */ |
9771b263 | 97 | static vec<Node_Id> gnat_validate_uc_list; |
f04b8d69 | 98 | |
ec4a0d83 EB |
99 | /* List of expressions of pragma Compile_Time_{Error|Warning} in the unit. */ |
100 | static vec<Node_Id> gnat_compile_time_expr_list; | |
101 | ||
a1ab4c31 AC |
102 | /* When not optimizing, we cache the 'First, 'Last and 'Length attributes |
103 | of unconstrained array IN parameters to avoid emitting a great deal of | |
104 | redundant instructions to recompute them each time. */ | |
6bf68a93 | 105 | struct GTY (()) parm_attr_d { |
a1ab4c31 AC |
106 | int id; /* GTY doesn't like Entity_Id. */ |
107 | int dim; | |
108 | tree first; | |
109 | tree last; | |
110 | tree length; | |
111 | }; | |
112 | ||
6bf68a93 | 113 | typedef struct parm_attr_d *parm_attr; |
a1ab4c31 | 114 | |
852dd866 | 115 | /* Structure used to record information for a function. */ |
d1b38208 | 116 | struct GTY(()) language_function { |
9771b263 | 117 | vec<parm_attr, va_gc> *parm_attr_cache; |
71196d4e | 118 | bitmap named_ret_val; |
9771b263 | 119 | vec<tree, va_gc> *other_ret_val; |
088b91c7 | 120 | int gnat_ret; |
a1ab4c31 AC |
121 | }; |
122 | ||
123 | #define f_parm_attr_cache \ | |
124 | DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache | |
125 | ||
71196d4e EB |
126 | #define f_named_ret_val \ |
127 | DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val | |
128 | ||
129 | #define f_other_ret_val \ | |
130 | DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val | |
131 | ||
088b91c7 EB |
132 | #define f_gnat_ret \ |
133 | DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret | |
134 | ||
a1ab4c31 AC |
135 | /* A structure used to gather together information about a statement group. |
136 | We use this to gather related statements, for example the "then" part | |
137 | of a IF. In the case where it represents a lexical scope, we may also | |
138 | have a BLOCK node corresponding to it and/or cleanups. */ | |
139 | ||
d1b38208 | 140 | struct GTY((chain_next ("%h.previous"))) stmt_group { |
a1ab4c31 | 141 | struct stmt_group *previous; /* Previous code group. */ |
1e17ef87 EB |
142 | tree stmt_list; /* List of statements for this code group. */ |
143 | tree block; /* BLOCK for this code group, if any. */ | |
a1ab4c31 AC |
144 | tree cleanups; /* Cleanups for this code group, if any. */ |
145 | }; | |
146 | ||
147 | static GTY(()) struct stmt_group *current_stmt_group; | |
148 | ||
149 | /* List of unused struct stmt_group nodes. */ | |
150 | static GTY((deletable)) struct stmt_group *stmt_group_free_list; | |
151 | ||
152 | /* A structure used to record information on elaboration procedures | |
153 | we've made and need to process. | |
154 | ||
155 | ??? gnat_node should be Node_Id, but gengtype gets confused. */ | |
156 | ||
d1b38208 | 157 | struct GTY((chain_next ("%h.next"))) elab_info { |
1e17ef87 | 158 | struct elab_info *next; /* Pointer to next in chain. */ |
a1ab4c31 AC |
159 | tree elab_proc; /* Elaboration procedure. */ |
160 | int gnat_node; /* The N_Compilation_Unit. */ | |
161 | }; | |
162 | ||
163 | static GTY(()) struct elab_info *elab_info_list; | |
164 | ||
39f579c7 NF |
165 | /* Stack of exception pointer variables. Each entry is the VAR_DECL |
166 | that stores the address of the raised exception. Nonzero means we | |
167 | are in an exception handler. Not used in the zero-cost case. */ | |
9771b263 | 168 | static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack; |
a1ab4c31 | 169 | |
624e1688 AC |
170 | /* In ZCX case, current exception pointer. Used to re-raise it. */ |
171 | static GTY(()) tree gnu_incoming_exc_ptr; | |
172 | ||
39f579c7 | 173 | /* Stack for storing the current elaboration procedure decl. */ |
9771b263 | 174 | static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack; |
a1ab4c31 | 175 | |
39f579c7 NF |
176 | /* Stack of labels to be used as a goto target instead of a return in |
177 | some functions. See processing for N_Subprogram_Body. */ | |
9771b263 | 178 | static GTY(()) vec<tree, va_gc> *gnu_return_label_stack; |
a1ab4c31 | 179 | |
35a382b8 EB |
180 | /* Stack of variable for the return value of a function with copy-in/copy-out |
181 | parameters. See processing for N_Subprogram_Body. */ | |
9771b263 | 182 | static GTY(()) vec<tree, va_gc> *gnu_return_var_stack; |
35a382b8 | 183 | |
15bf7d19 EB |
184 | /* Structure used to record information for a range check. */ |
185 | struct GTY(()) range_check_info_d { | |
186 | tree low_bound; | |
187 | tree high_bound; | |
933a7325 EB |
188 | tree disp; |
189 | bool neg_p; | |
15bf7d19 EB |
190 | tree type; |
191 | tree invariant_cond; | |
64235766 | 192 | tree inserted_cond; |
15bf7d19 EB |
193 | }; |
194 | ||
195 | typedef struct range_check_info_d *range_check_info; | |
196 | ||
15bf7d19 EB |
197 | /* Structure used to record information for a loop. */ |
198 | struct GTY(()) loop_info_d { | |
852dd866 | 199 | tree fndecl; |
633a3f2a | 200 | tree stmt; |
15bf7d19 | 201 | tree loop_var; |
87ab2b04 EB |
202 | tree low_bound; |
203 | tree high_bound; | |
7beb2b9d OH |
204 | tree omp_loop_clauses; |
205 | tree omp_construct_clauses; | |
206 | enum tree_code omp_code; | |
9771b263 | 207 | vec<range_check_info, va_gc> *checks; |
852dd866 | 208 | vec<tree, va_gc> *invariants; |
15bf7d19 EB |
209 | }; |
210 | ||
211 | typedef struct loop_info_d *loop_info; | |
212 | ||
15bf7d19 | 213 | /* Stack of loop_info structures associated with LOOP_STMT nodes. */ |
9771b263 | 214 | static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack; |
a1ab4c31 | 215 | |
39f579c7 | 216 | /* The stacks for N_{Push,Pop}_*_Label. */ |
8f8f531f PMR |
217 | static vec<Entity_Id> gnu_constraint_error_label_stack; |
218 | static vec<Entity_Id> gnu_storage_error_label_stack; | |
219 | static vec<Entity_Id> gnu_program_error_label_stack; | |
a1ab4c31 AC |
220 | |
221 | /* Map GNAT tree codes to GCC tree codes for simple expressions. */ | |
222 | static enum tree_code gnu_codes[Number_Node_Kinds]; | |
223 | ||
a1ab4c31 | 224 | static void init_code_table (void); |
7543c8b6 | 225 | static tree get_elaboration_procedure (void); |
a1ab4c31 | 226 | static void Compilation_Unit_to_gnu (Node_Id); |
102a1631 | 227 | static bool empty_stmt_list_p (tree); |
a1ab4c31 AC |
228 | static void record_code_position (Node_Id); |
229 | static void insert_code_for (Node_Id); | |
230 | static void add_cleanup (tree, Node_Id); | |
a1ab4c31 | 231 | static void add_stmt_list (List_Id); |
a1ab4c31 | 232 | static tree build_stmt_group (List_Id, bool); |
a712b009 | 233 | static inline bool stmt_group_may_fallthru (void); |
a1ab4c31 AC |
234 | static enum gimplify_status gnat_gimplify_stmt (tree *); |
235 | static void elaborate_all_entities (Node_Id); | |
236 | static void process_freeze_entity (Node_Id); | |
6d16658d | 237 | static void process_decls (List_Id, List_Id, bool, bool); |
10069d53 EB |
238 | static tree emit_check (tree, tree, int, Node_Id); |
239 | static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id); | |
240 | static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id); | |
815b5368 | 241 | static tree convert_with_check (Entity_Id, tree, bool, bool, Node_Id); |
a1ab4c31 AC |
242 | static bool addressable_p (tree, tree); |
243 | static tree assoc_to_constructor (Entity_Id, Node_Id, tree); | |
815b5368 | 244 | static tree pos_to_constructor (Node_Id, tree); |
f04b8d69 | 245 | static void validate_unchecked_conversion (Node_Id); |
ba464315 | 246 | static void set_expr_location_from_node (tree, Node_Id, bool = false); |
17c168fe | 247 | static void set_gnu_expr_location_from_node (tree, Node_Id); |
ba464315 | 248 | static bool set_end_locus_from_node (tree, Node_Id); |
86da71db | 249 | static int lvalue_required_p (Node_Id, tree, bool, bool); |
c1fd8753 | 250 | static tree build_raise_check (int, enum exception_info_kind); |
6162cec0 | 251 | static tree create_init_temporary (const char *, tree, tree *, Node_Id); |
44662f68 | 252 | static bool maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk); |
a1ab4c31 | 253 | |
fa6fd7b7 AO |
254 | /* This makes gigi's file_info_ptr visible in this translation unit, |
255 | so that Sloc_to_locus can look it up when deciding whether to map | |
256 | decls to instances. */ | |
257 | ||
258 | static struct File_Info_Type *file_map; | |
259 | ||
39fa0de5 EB |
260 | /* Return the string of the identifier allocated for the file name Id. */ |
261 | ||
262 | static const char* | |
263 | File_Name_to_gnu (Name_Id Id) | |
264 | { | |
265 | /* __gnat_to_canonical_file_spec translates file names from pragmas | |
266 | Source_Reference that contain host style syntax not understood by GDB. */ | |
267 | const char *name = __gnat_to_canonical_file_spec (Get_Name_String (Id)); | |
268 | ||
269 | /* Use the identifier table to make a permanent copy of the file name as | |
270 | the name table gets reallocated after Gigi returns but before all the | |
271 | debugging information is output. */ | |
272 | return IDENTIFIER_POINTER (get_identifier (name)); | |
273 | } | |
274 | ||
a1ab4c31 AC |
275 | /* This is the main program of the back-end. It sets up all the table |
276 | structures and then generates code. */ | |
277 | ||
278 | void | |
4bcf6815 AC |
279 | gigi (Node_Id gnat_root, |
280 | int max_gnat_node, | |
281 | int number_name ATTRIBUTE_UNUSED, | |
99e30ba8 | 282 | Node_Header *node_offsets_ptr, |
a6d3b499 | 283 | any_slot *slots_ptr, |
4bcf6815 AC |
284 | Node_Id *next_node_ptr, |
285 | Node_Id *prev_node_ptr, | |
286 | struct Elist_Header *elists_ptr, | |
287 | struct Elmt_Item *elmts_ptr, | |
288 | struct String_Entry *strings_ptr, | |
289 | Char_Code *string_chars_ptr, | |
290 | struct List_Header *list_headers_ptr, | |
291 | Nat number_file, | |
6936c61a | 292 | struct File_Info_Type *file_info_ptr, |
1a9c7e9c | 293 | Entity_Id standard_address, |
4bcf6815 | 294 | Entity_Id standard_boolean, |
4bcf6815 | 295 | Entity_Id standard_character, |
4bcf6815 | 296 | Entity_Id standard_exception_type, |
1a9c7e9c EB |
297 | Entity_Id standard_integer, |
298 | Entity_Id standard_long_long_float, | |
4bcf6815 | 299 | Int gigi_operating_mode) |
a1ab4c31 | 300 | { |
f04b8d69 | 301 | Node_Id gnat_iter; |
01ddebf2 | 302 | Entity_Id gnat_literal; |
1eb58520 | 303 | tree t, ftype, int64_type; |
a1ab4c31 AC |
304 | struct elab_info *info; |
305 | int i; | |
306 | ||
307 | max_gnat_nodes = max_gnat_node; | |
831f44c6 | 308 | |
76f9c7f4 BD |
309 | Node_Offsets_Ptr = node_offsets_ptr; |
310 | Slots_Ptr = slots_ptr; | |
a1ab4c31 AC |
311 | Next_Node_Ptr = next_node_ptr; |
312 | Prev_Node_Ptr = prev_node_ptr; | |
313 | Elists_Ptr = elists_ptr; | |
314 | Elmts_Ptr = elmts_ptr; | |
315 | Strings_Ptr = strings_ptr; | |
316 | String_Chars_Ptr = string_chars_ptr; | |
317 | List_Headers_Ptr = list_headers_ptr; | |
318 | ||
319 | type_annotate_only = (gigi_operating_mode == 1); | |
320 | ||
fa6fd7b7 AO |
321 | if (Generate_SCO_Instance_Table != 0) |
322 | { | |
323 | file_map = file_info_ptr; | |
324 | maybe_create_decl_to_instance_map (number_file); | |
325 | } | |
326 | ||
831f44c6 | 327 | for (i = 0; i < number_file; i++) |
a1ab4c31 | 328 | { |
a1ab4c31 | 329 | /* We rely on the order isomorphism between files and line maps. */ |
39fa0de5 EB |
330 | if ((int) LINEMAPS_ORDINARY_USED (line_table) != i) |
331 | { | |
332 | gcc_assert (i > 0); | |
333 | error ("%s contains too many lines", | |
334 | File_Name_to_gnu (file_info_ptr[i - 1].File_Name)); | |
335 | } | |
a1ab4c31 AC |
336 | |
337 | /* We create the line map for a source file at once, with a fixed number | |
338 | of columns chosen to avoid jumping over the next power of 2. */ | |
39fa0de5 EB |
339 | linemap_add (line_table, LC_ENTER, 0, |
340 | File_Name_to_gnu (file_info_ptr[i].File_Name), 1); | |
a1ab4c31 AC |
341 | linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252); |
342 | linemap_position_for_column (line_table, 252 - 1); | |
343 | linemap_add (line_table, LC_LEAVE, 0, NULL, 0); | |
344 | } | |
345 | ||
52e04e56 EB |
346 | gcc_assert (Nkind (gnat_root) == N_Compilation_Unit); |
347 | ||
348 | /* Declare the name of the compilation unit as the first global | |
349 | name in order to make the middle-end fully deterministic. */ | |
350 | t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL); | |
351 | first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t)); | |
352 | ||
a1ab4c31 AC |
353 | /* Initialize ourselves. */ |
354 | init_code_table (); | |
4116e7d0 | 355 | init_gnat_decl (); |
842d4ee2 | 356 | init_gnat_utils (); |
a1ab4c31 AC |
357 | |
358 | /* If we are just annotating types, give VOID_TYPE zero sizes to avoid | |
359 | errors. */ | |
360 | if (type_annotate_only) | |
361 | { | |
362 | TYPE_SIZE (void_type_node) = bitsize_zero_node; | |
363 | TYPE_SIZE_UNIT (void_type_node) = size_zero_node; | |
364 | } | |
365 | ||
a1ab4c31 AC |
366 | /* Enable GNAT stack checking method if needed */ |
367 | if (!Stack_Check_Probes_On_Target) | |
1f3f64b9 EB |
368 | { |
369 | set_stack_check_libfunc ("__gnat_stack_check"); | |
370 | if (flag_stack_check != NO_STACK_CHECK) | |
371 | Check_Restriction_No_Dependence_On_System (Name_Stack_Checking, | |
372 | gnat_root); | |
373 | } | |
a1ab4c31 | 374 | |
caa9d12a EB |
375 | /* Retrieve alignment settings. */ |
376 | double_float_alignment = get_target_double_float_alignment (); | |
377 | double_scalar_alignment = get_target_double_scalar_alignment (); | |
378 | ||
1a9c7e9c EB |
379 | /* Record the builtin types. */ |
380 | record_builtin_type ("address", pointer_sized_int_node, false); | |
1aeb40dd | 381 | record_builtin_type ("integer", integer_type_node, false); |
825da0d2 | 382 | record_builtin_type ("character", char_type_node, false); |
1aeb40dd EB |
383 | record_builtin_type ("boolean", boolean_type_node, false); |
384 | record_builtin_type ("void", void_type_node, false); | |
10069d53 | 385 | |
1a9c7e9c EB |
386 | /* Save the type we made for address as the type for Standard.Address. */ |
387 | save_gnu_tree (Base_Type (standard_address), | |
388 | TYPE_NAME (pointer_sized_int_node), | |
389 | false); | |
390 | ||
391 | /* Likewise for integer as the type for Standard.Integer. */ | |
6936c61a EB |
392 | save_gnu_tree (Base_Type (standard_integer), |
393 | TYPE_NAME (integer_type_node), | |
10069d53 | 394 | false); |
a1ab4c31 | 395 | |
6936c61a | 396 | /* Likewise for character as the type for Standard.Character. */ |
825da0d2 | 397 | finish_character_type (char_type_node); |
6936c61a | 398 | save_gnu_tree (Base_Type (standard_character), |
825da0d2 | 399 | TYPE_NAME (char_type_node), |
6936c61a EB |
400 | false); |
401 | ||
402 | /* Likewise for boolean as the type for Standard.Boolean. */ | |
403 | save_gnu_tree (Base_Type (standard_boolean), | |
404 | TYPE_NAME (boolean_type_node), | |
01ddebf2 EB |
405 | false); |
406 | gnat_literal = First_Literal (Base_Type (standard_boolean)); | |
407 | t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node); | |
408 | gcc_assert (t == boolean_false_node); | |
409 | t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE, | |
2056c5ed | 410 | boolean_type_node, t, true, false, false, false, false, |
c1a569ef | 411 | true, false, NULL, gnat_literal); |
01ddebf2 EB |
412 | save_gnu_tree (gnat_literal, t, false); |
413 | gnat_literal = Next_Literal (gnat_literal); | |
414 | t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node); | |
415 | gcc_assert (t == boolean_true_node); | |
416 | t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE, | |
2056c5ed | 417 | boolean_type_node, t, true, false, false, false, false, |
c1a569ef | 418 | true, false, NULL, gnat_literal); |
01ddebf2 EB |
419 | save_gnu_tree (gnat_literal, t, false); |
420 | ||
9182f718 | 421 | /* Declare the building blocks of function nodes. */ |
c1fd8753 | 422 | void_ftype = build_function_type_list (void_type_node, NULL_TREE); |
10069d53 EB |
423 | ptr_void_ftype = build_pointer_type (void_ftype); |
424 | ||
c01fe451 | 425 | /* Now declare run-time functions. */ |
10069d53 EB |
426 | malloc_decl |
427 | = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE, | |
ab3dd4ab EB |
428 | build_function_type_list (ptr_type_node, sizetype, |
429 | NULL_TREE), | |
13a6dfe3 | 430 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 431 | false, NULL, Empty); |
10069d53 EB |
432 | DECL_IS_MALLOC (malloc_decl) = 1; |
433 | ||
10069d53 EB |
434 | free_decl |
435 | = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE, | |
ab3dd4ab EB |
436 | build_function_type_list (void_type_node, |
437 | ptr_type_node, NULL_TREE), | |
13a6dfe3 | 438 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 439 | false, NULL, Empty); |
9182f718 | 440 | |
9182f718 EB |
441 | realloc_decl |
442 | = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE, | |
ab3dd4ab EB |
443 | build_function_type_list (ptr_type_node, |
444 | ptr_type_node, sizetype, | |
445 | NULL_TREE), | |
13a6dfe3 | 446 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 447 | false, NULL, Empty); |
10069d53 EB |
448 | |
449 | /* This is used for 64-bit multiplication with overflow checking. */ | |
1eb58520 | 450 | int64_type = gnat_type_for_size (64, 0); |
10069d53 EB |
451 | mulv64_decl |
452 | = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE, | |
453 | build_function_type_list (int64_type, int64_type, | |
454 | int64_type, NULL_TREE), | |
13a6dfe3 | 455 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 456 | false, NULL, Empty); |
10069d53 | 457 | |
f2d9f95e EB |
458 | if (Enable_128bit_Types) |
459 | { | |
460 | tree int128_type = gnat_type_for_size (128, 0); | |
461 | mulv128_decl | |
462 | = create_subprog_decl (get_identifier ("__gnat_mulv128"), NULL_TREE, | |
463 | build_function_type_list (int128_type, | |
464 | int128_type, | |
465 | int128_type, | |
466 | NULL_TREE), | |
467 | NULL_TREE, is_default, true, true, true, false, | |
468 | false, NULL, Empty); | |
469 | } | |
470 | ||
76af763d EB |
471 | /* Name of the _Parent field in tagged record types. */ |
472 | parent_name_id = get_identifier (Get_Name_String (Name_uParent)); | |
473 | ||
17ba0ad5 EB |
474 | /* Name of the Not_Handled_By_Others field in exception record types. */ |
475 | not_handled_by_others_name_id = get_identifier ("not_handled_by_others"); | |
871fda0a | 476 | |
10069d53 | 477 | /* Make the types and functions used for exception processing. */ |
64235766 EB |
478 | except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type)); |
479 | ||
64235766 EB |
480 | set_exception_parameter_decl |
481 | = create_subprog_decl | |
482 | (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE, | |
483 | build_function_type_list (void_type_node, ptr_type_node, ptr_type_node, | |
484 | NULL_TREE), | |
13a6dfe3 | 485 | NULL_TREE, is_default, true, true, true, false, false, NULL, Empty); |
64235766 | 486 | |
10069d53 | 487 | /* Hooks to call when entering/leaving an exception handler. */ |
5d733372 AO |
488 | ftype = build_function_type_list (ptr_type_node, |
489 | ptr_type_node, NULL_TREE); | |
10069d53 | 490 | begin_handler_decl |
5d733372 AO |
491 | = create_subprog_decl (get_identifier ("__gnat_begin_handler_v1"), |
492 | NULL_TREE, ftype, NULL_TREE, | |
13a6dfe3 | 493 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 494 | Empty); |
5d733372 AO |
495 | /* __gnat_begin_handler_v1 is not a dummy procedure, but we arrange |
496 | for it not to throw. */ | |
4ec7c4ec | 497 | TREE_NOTHROW (begin_handler_decl) = 1; |
10069d53 | 498 | |
5d733372 AO |
499 | ftype = build_function_type_list (ptr_type_node, |
500 | ptr_type_node, ptr_type_node, | |
501 | ptr_type_node, NULL_TREE); | |
10069d53 | 502 | end_handler_decl |
5d733372 | 503 | = create_subprog_decl (get_identifier ("__gnat_end_handler_v1"), NULL_TREE, |
c1a569ef | 504 | ftype, NULL_TREE, |
13a6dfe3 | 505 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 506 | Empty); |
10069d53 | 507 | |
5d733372 | 508 | ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); |
48a24fcf TG |
509 | unhandled_except_decl |
510 | = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"), | |
c1a569ef | 511 | NULL_TREE, ftype, NULL_TREE, |
13a6dfe3 | 512 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 513 | Empty); |
1e55d29a EB |
514 | |
515 | /* Indicate that it never returns. */ | |
516 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); | |
517 | reraise_zcx_decl | |
518 | = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE, | |
519 | ftype, NULL_TREE, | |
13a6dfe3 | 520 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 521 | Empty); |
48a24fcf | 522 | |
64235766 EB |
523 | /* Dummy objects to materialize "others" and "all others" in the exception |
524 | tables. These are exported by a-exexpr-gcc.adb, so see this unit for | |
525 | the types to use. */ | |
526 | others_decl | |
527 | = create_var_decl (get_identifier ("OTHERS"), | |
528 | get_identifier ("__gnat_others_value"), | |
825da0d2 | 529 | char_type_node, NULL_TREE, |
2056c5ed | 530 | true, false, true, false, false, true, false, |
64235766 EB |
531 | NULL, Empty); |
532 | ||
533 | all_others_decl | |
534 | = create_var_decl (get_identifier ("ALL_OTHERS"), | |
535 | get_identifier ("__gnat_all_others_value"), | |
825da0d2 | 536 | char_type_node, NULL_TREE, |
2056c5ed | 537 | true, false, true, false, false, true, false, |
64235766 EB |
538 | NULL, Empty); |
539 | ||
540 | unhandled_others_decl | |
541 | = create_var_decl (get_identifier ("UNHANDLED_OTHERS"), | |
542 | get_identifier ("__gnat_unhandled_others_value"), | |
825da0d2 | 543 | char_type_node, NULL_TREE, |
2056c5ed | 544 | true, false, true, false, false, true, false, |
64235766 | 545 | NULL, Empty); |
624e1688 | 546 | |
10069d53 | 547 | /* If in no exception handlers mode, all raise statements are redirected to |
d6d9f534 | 548 | __gnat_last_chance_handler. */ |
10069d53 EB |
549 | if (No_Exception_Handlers_Set ()) |
550 | { | |
1e55d29a EB |
551 | /* Indicate that it never returns. */ |
552 | ftype = build_function_type_list (void_type_node, | |
553 | build_pointer_type (char_type_node), | |
554 | integer_type_node, NULL_TREE); | |
555 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); | |
10069d53 EB |
556 | tree decl |
557 | = create_subprog_decl | |
1e55d29a | 558 | (get_identifier ("__gnat_last_chance_handler"), NULL_TREE, ftype, |
13a6dfe3 | 559 | NULL_TREE, is_default, true, true, true, false, false, NULL, |
ff9baa5f | 560 | Empty); |
cfc839a4 EB |
561 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++) |
562 | gnat_raise_decls[i] = decl; | |
10069d53 EB |
563 | } |
564 | else | |
10069d53 | 565 | { |
437f8c1e AC |
566 | /* Otherwise, make one decl for each exception reason. */ |
567 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++) | |
c1fd8753 | 568 | gnat_raise_decls[i] = build_raise_check (i, exception_simple); |
437f8c1e AC |
569 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++) |
570 | gnat_raise_decls_ext[i] | |
c1fd8753 | 571 | = build_raise_check (i, |
437f8c1e | 572 | i == CE_Index_Check_Failed |
ea034236 AC |
573 | || i == CE_Range_Check_Failed |
574 | || i == CE_Invalid_Data | |
575 | ? exception_range : exception_column); | |
10069d53 EB |
576 | } |
577 | ||
10069d53 EB |
578 | /* Build the special descriptor type and its null node if needed. */ |
579 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
580 | { | |
581 | tree null_node = fold_convert (ptr_void_ftype, null_pointer_node); | |
0e228dd9 | 582 | tree field_list = NULL_TREE; |
10069d53 | 583 | int j; |
9771b263 | 584 | vec<constructor_elt, va_gc> *null_vec = NULL; |
0e228dd9 | 585 | constructor_elt *elt; |
10069d53 EB |
586 | |
587 | fdesc_type_node = make_node (RECORD_TYPE); | |
cb3874dc | 588 | vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS, true); |
9771b263 | 589 | elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1); |
10069d53 EB |
590 | |
591 | for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++) | |
592 | { | |
da01bfee EB |
593 | tree field |
594 | = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node, | |
595 | NULL_TREE, NULL_TREE, 0, 1); | |
7d76717d | 596 | DECL_CHAIN (field) = field_list; |
10069d53 | 597 | field_list = field; |
0e228dd9 NF |
598 | elt->index = field; |
599 | elt->value = null_node; | |
600 | elt--; | |
10069d53 EB |
601 | } |
602 | ||
032d1b71 | 603 | finish_record_type (fdesc_type_node, nreverse (field_list), 0, false); |
1aeb40dd | 604 | record_builtin_type ("descriptor", fdesc_type_node, true); |
0e228dd9 | 605 | null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec); |
10069d53 EB |
606 | } |
607 | ||
1eb58520 AC |
608 | longest_float_type_node |
609 | = get_unpadded_type (Base_Type (standard_long_long_float)); | |
f7ebc6a8 | 610 | |
10069d53 EB |
611 | main_identifier_node = get_identifier ("main"); |
612 | ||
ce19ac12 | 613 | gnat_init_gcc_eh (); |
c8a23c29 EB |
614 | |
615 | /* Initialize the GCC support for FP operations. */ | |
616 | gnat_init_gcc_fp (); | |
617 | ||
618 | /* Install the builtins we might need, either internally or as user-available | |
619 | facilities for Intrinsic imports. Note that this must be done after the | |
620 | GCC exception mechanism is initialized. */ | |
10069d53 | 621 | gnat_install_builtins (); |
a1ab4c31 | 622 | |
9771b263 | 623 | vec_safe_push (gnu_except_ptr_stack, NULL_TREE); |
8f8f531f PMR |
624 | |
625 | gnu_constraint_error_label_stack.safe_push (Empty); | |
626 | gnu_storage_error_label_stack.safe_push (Empty); | |
627 | gnu_program_error_label_stack.safe_push (Empty); | |
a1ab4c31 | 628 | |
a1ab4c31 | 629 | /* Process any Pragma Ident for the main unit. */ |
a1ab4c31 | 630 | if (Present (Ident_String (Main_Unit))) |
a8781821 SB |
631 | targetm.asm_out.output_ident |
632 | (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit)))); | |
a1ab4c31 | 633 | |
c3831524 AC |
634 | /* Force -fno-strict-aliasing if the configuration pragma was seen. */ |
635 | if (No_Strict_Aliasing_CP) | |
636 | flag_strict_aliasing = 0; | |
637 | ||
c0c91386 JJ |
638 | /* Save the current optimization options again after the above possible |
639 | global_options changes. */ | |
ba948b37 JJ |
640 | optimization_default_node |
641 | = build_optimization_node (&global_options, &global_options_set); | |
c0c91386 JJ |
642 | optimization_current_node = optimization_default_node; |
643 | ||
6a7a3f31 | 644 | /* Now translate the compilation unit proper. */ |
a1ab4c31 AC |
645 | Compilation_Unit_to_gnu (gnat_root); |
646 | ||
f04b8d69 EB |
647 | /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at |
648 | the very end to avoid having to second-guess the front-end when we run | |
649 | into dummy nodes during the regular processing. */ | |
9771b263 | 650 | for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++) |
f04b8d69 | 651 | validate_unchecked_conversion (gnat_iter); |
9771b263 | 652 | gnat_validate_uc_list.release (); |
f04b8d69 | 653 | |
6a7a3f31 | 654 | /* Finally see if we have any elaboration procedures to deal with. */ |
a1ab4c31 AC |
655 | for (info = elab_info_list; info; info = info->next) |
656 | { | |
102a1631 | 657 | tree gnu_body = DECL_SAVED_TREE (info->elab_proc); |
a1ab4c31 | 658 | |
2fa03086 EB |
659 | /* We should have a BIND_EXPR but it may not have any statements in it. |
660 | If it doesn't have any, we have nothing to do except for setting the | |
661 | flag on the GNAT node. Otherwise, process the function as others. */ | |
102a1631 | 662 | tree gnu_stmts = gnu_body; |
a406865a RG |
663 | if (TREE_CODE (gnu_stmts) == BIND_EXPR) |
664 | gnu_stmts = BIND_EXPR_BODY (gnu_stmts); | |
102a1631 | 665 | if (!gnu_stmts || empty_stmt_list_p (gnu_stmts)) |
2fa03086 | 666 | Set_Has_No_Elaboration_Code (info->gnat_node, 1); |
a406865a RG |
667 | else |
668 | { | |
a406865a RG |
669 | begin_subprog_body (info->elab_proc); |
670 | end_subprog_body (gnu_body); | |
71196d4e | 671 | rest_of_subprog_body_compilation (info->elab_proc); |
a406865a | 672 | } |
a1ab4c31 AC |
673 | } |
674 | ||
f04b8d69 | 675 | /* Destroy ourselves. */ |
fa6fd7b7 | 676 | file_map = NULL; |
4116e7d0 | 677 | destroy_gnat_decl (); |
842d4ee2 | 678 | destroy_gnat_utils (); |
f04b8d69 | 679 | |
a1ab4c31 | 680 | /* We cannot track the location of errors past this point. */ |
738b83cd | 681 | Current_Error_Node = Empty; |
a1ab4c31 | 682 | } |
ce2d0ce2 | 683 | |
437f8c1e | 684 | /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given |
c1fd8753 | 685 | CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */ |
437f8c1e AC |
686 | |
687 | static tree | |
c1fd8753 | 688 | build_raise_check (int check, enum exception_info_kind kind) |
437f8c1e | 689 | { |
c1fd8753 | 690 | tree result, ftype; |
0c644c99 TG |
691 | const char pfx[] = "__gnat_rcheck_"; |
692 | ||
693 | strcpy (Name_Buffer, pfx); | |
694 | Name_Len = sizeof (pfx) - 1; | |
a6d3b499 | 695 | Get_RT_Exception_Name ((enum RT_Exception_Code) check); |
437f8c1e | 696 | |
c1fd8753 | 697 | if (kind == exception_simple) |
437f8c1e | 698 | { |
0c644c99 | 699 | Name_Buffer[Name_Len] = 0; |
c1fd8753 NF |
700 | ftype |
701 | = build_function_type_list (void_type_node, | |
825da0d2 | 702 | build_pointer_type (char_type_node), |
c1fd8753 | 703 | integer_type_node, NULL_TREE); |
437f8c1e AC |
704 | } |
705 | else | |
706 | { | |
c1fd8753 | 707 | tree t = (kind == exception_column ? NULL_TREE : integer_type_node); |
0c644c99 TG |
708 | |
709 | strcpy (Name_Buffer + Name_Len, "_ext"); | |
710 | Name_Buffer[Name_Len + 4] = 0; | |
c1fd8753 NF |
711 | ftype |
712 | = build_function_type_list (void_type_node, | |
825da0d2 | 713 | build_pointer_type (char_type_node), |
c1fd8753 NF |
714 | integer_type_node, integer_type_node, |
715 | t, t, NULL_TREE); | |
437f8c1e | 716 | } |
cfc839a4 | 717 | |
2056c5ed | 718 | /* Indicate that it never returns. */ |
1e55d29a | 719 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); |
c1fd8753 | 720 | result |
1e55d29a | 721 | = create_subprog_decl (get_identifier (Name_Buffer), NULL_TREE, ftype, |
13a6dfe3 | 722 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 723 | false, NULL, Empty); |
c1fd8753 | 724 | |
437f8c1e AC |
725 | return result; |
726 | } | |
ce2d0ce2 | 727 | |
3cd64bab EB |
728 | /* Return a positive value if an lvalue is required for GNAT_NODE, which is |
729 | an N_Attribute_Reference. */ | |
730 | ||
731 | static int | |
732 | lvalue_required_for_attribute_p (Node_Id gnat_node) | |
733 | { | |
734 | switch (Get_Attribute_Id (Attribute_Name (gnat_node))) | |
735 | { | |
3cd64bab EB |
736 | case Attr_Pred: |
737 | case Attr_Succ: | |
738 | case Attr_First: | |
739 | case Attr_Last: | |
740 | case Attr_Range_Length: | |
741 | case Attr_Length: | |
742 | case Attr_Object_Size: | |
79069232 | 743 | case Attr_Size: |
3cd64bab EB |
744 | case Attr_Value_Size: |
745 | case Attr_Component_Size: | |
24228312 | 746 | case Attr_Descriptor_Size: |
3cd64bab EB |
747 | case Attr_Max_Size_In_Storage_Elements: |
748 | case Attr_Min: | |
749 | case Attr_Max: | |
750 | case Attr_Null_Parameter: | |
751 | case Attr_Passed_By_Reference: | |
752 | case Attr_Mechanism_Code: | |
24228312 AC |
753 | case Attr_Machine: |
754 | case Attr_Model: | |
3cd64bab EB |
755 | return 0; |
756 | ||
757 | case Attr_Address: | |
758 | case Attr_Access: | |
759 | case Attr_Unchecked_Access: | |
760 | case Attr_Unrestricted_Access: | |
761 | case Attr_Code_Address: | |
762 | case Attr_Pool_Address: | |
3cd64bab EB |
763 | case Attr_Alignment: |
764 | case Attr_Bit_Position: | |
765 | case Attr_Position: | |
766 | case Attr_First_Bit: | |
767 | case Attr_Last_Bit: | |
768 | case Attr_Bit: | |
7e4680c1 EB |
769 | case Attr_Asm_Input: |
770 | case Attr_Asm_Output: | |
3cd64bab EB |
771 | default: |
772 | return 1; | |
773 | } | |
774 | } | |
775 | ||
22d12fc2 EB |
776 | /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE |
777 | is the type that will be used for GNAT_NODE in the translated GNU tree. | |
778 | CONSTANT indicates whether the underlying object represented by GNAT_NODE | |
cb3d597d | 779 | is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates |
86da71db EB |
780 | whether its value is the address of another constant. If it isn't, then |
781 | ADDRESS_OF_CONSTANT is ignored. | |
22d12fc2 EB |
782 | |
783 | The function climbs up the GNAT tree starting from the node and returns 1 | |
784 | upon encountering a node that effectively requires an lvalue downstream. | |
785 | It returns int instead of bool to facilitate usage in non-purely binary | |
786 | logic contexts. */ | |
a1ab4c31 AC |
787 | |
788 | static int | |
03b6f8a2 | 789 | lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant, |
86da71db | 790 | bool address_of_constant) |
a1ab4c31 AC |
791 | { |
792 | Node_Id gnat_parent = Parent (gnat_node), gnat_temp; | |
793 | ||
794 | switch (Nkind (gnat_parent)) | |
795 | { | |
796 | case N_Reference: | |
797 | return 1; | |
798 | ||
799 | case N_Attribute_Reference: | |
3cd64bab | 800 | return lvalue_required_for_attribute_p (gnat_parent); |
a1ab4c31 AC |
801 | |
802 | case N_Parameter_Association: | |
803 | case N_Function_Call: | |
804 | case N_Procedure_Call_Statement: | |
1fc24649 EB |
805 | /* If the parameter is by reference, an lvalue is required. */ |
806 | return (!constant | |
807 | || must_pass_by_ref (gnu_type) | |
808 | || default_pass_by_ref (gnu_type)); | |
a1ab4c31 | 809 | |
0c6fbbfc EB |
810 | case N_Pragma_Argument_Association: |
811 | return lvalue_required_p (gnat_parent, gnu_type, constant, | |
812 | address_of_constant); | |
813 | ||
814 | case N_Pragma: | |
815 | if (Is_Pragma_Name (Chars (Pragma_Identifier (gnat_parent)))) | |
816 | { | |
817 | const unsigned char id | |
818 | = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_parent))); | |
819 | return id == Pragma_Inspection_Point; | |
820 | } | |
821 | else | |
822 | return 0; | |
823 | ||
a1ab4c31 AC |
824 | case N_Indexed_Component: |
825 | /* Only the array expression can require an lvalue. */ | |
826 | if (Prefix (gnat_parent) != gnat_node) | |
827 | return 0; | |
828 | ||
ed98eed8 EB |
829 | /* ??? Consider that referencing an indexed component with a variable |
830 | index forces the whole aggregate to memory. Note that testing only | |
831 | for literals is conservative, any static expression in the RM sense | |
832 | could probably be accepted with some additional work. */ | |
a1ab4c31 AC |
833 | for (gnat_temp = First (Expressions (gnat_parent)); |
834 | Present (gnat_temp); | |
835 | gnat_temp = Next (gnat_temp)) | |
ed98eed8 EB |
836 | if (Nkind (gnat_temp) != N_Character_Literal |
837 | && Nkind (gnat_temp) != N_Integer_Literal | |
838 | && !(Is_Entity_Name (gnat_temp) | |
839 | && Ekind (Entity (gnat_temp)) == E_Enumeration_Literal)) | |
a1ab4c31 AC |
840 | return 1; |
841 | ||
9c453de7 | 842 | /* ... fall through ... */ |
a1ab4c31 | 843 | |
527ed00b | 844 | case N_Selected_Component: |
a1ab4c31 | 845 | case N_Slice: |
527ed00b | 846 | /* Only the prefix expression can require an lvalue. */ |
a1ab4c31 AC |
847 | if (Prefix (gnat_parent) != gnat_node) |
848 | return 0; | |
849 | ||
7f6dd102 EB |
850 | return lvalue_required_p (gnat_parent, |
851 | get_unpadded_type (Etype (gnat_parent)), | |
852 | constant, address_of_constant); | |
a1ab4c31 | 853 | |
a1ab4c31 | 854 | case N_Object_Renaming_Declaration: |
e297e2ea EB |
855 | /* We need to preserve addresses through a renaming. */ |
856 | return 1; | |
a1ab4c31 | 857 | |
bbaba73f EB |
858 | case N_Object_Declaration: |
859 | /* We cannot use a constructor if this is an atomic object because | |
860 | the actual assignment might end up being done component-wise. */ | |
1fc24649 EB |
861 | return (!constant |
862 | ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node))) | |
b120ca61 | 863 | && Is_Full_Access (Defining_Entity (gnat_parent))) |
cb3d597d EB |
864 | /* We don't use a constructor if this is a class-wide object |
865 | because the effective type of the object is the equivalent | |
866 | type of the class-wide subtype and it smashes most of the | |
867 | data into an array of bytes to which we cannot convert. */ | |
868 | || Ekind ((Etype (Defining_Entity (gnat_parent)))) | |
869 | == E_Class_Wide_Subtype); | |
bbaba73f EB |
870 | |
871 | case N_Assignment_Statement: | |
872 | /* We cannot use a constructor if the LHS is an atomic object because | |
873 | the actual assignment might end up being done component-wise. */ | |
1fc24649 EB |
874 | return (!constant |
875 | || Name (gnat_parent) == gnat_node | |
03b6f8a2 | 876 | || (Is_Composite_Type (Underlying_Type (Etype (gnat_node))) |
f797c2b7 | 877 | && Is_Entity_Name (Name (gnat_parent)) |
b120ca61 | 878 | && Is_Full_Access (Entity (Name (gnat_parent))))); |
bbaba73f | 879 | |
054d6b83 EB |
880 | case N_Unchecked_Type_Conversion: |
881 | if (!constant) | |
882 | return 1; | |
76af763d | 883 | |
9c453de7 | 884 | /* ... fall through ... */ |
76af763d | 885 | |
054d6b83 EB |
886 | case N_Type_Conversion: |
887 | case N_Qualified_Expression: | |
888 | /* We must look through all conversions because we may need to bypass | |
889 | an intermediate conversion that is meant to be purely formal. */ | |
890 | return lvalue_required_p (gnat_parent, | |
891 | get_unpadded_type (Etype (gnat_parent)), | |
86da71db | 892 | constant, address_of_constant); |
cb3d597d EB |
893 | |
894 | case N_Explicit_Dereference: | |
895 | /* We look through dereferences for address of constant because we need | |
896 | to handle the special cases listed above. */ | |
897 | if (constant && address_of_constant) | |
898 | return lvalue_required_p (gnat_parent, | |
899 | get_unpadded_type (Etype (gnat_parent)), | |
86da71db | 900 | true, false); |
cb3d597d | 901 | |
9c453de7 | 902 | /* ... fall through ... */ |
22d12fc2 | 903 | |
a1ab4c31 AC |
904 | default: |
905 | return 0; | |
906 | } | |
907 | ||
908 | gcc_unreachable (); | |
909 | } | |
910 | ||
527ed00b EB |
911 | /* Return true if an lvalue should be used for GNAT_NODE. GNU_TYPE is the type |
912 | that will be used for GNAT_NODE in the translated GNU tree and is assumed to | |
913 | be an aggregate type. | |
914 | ||
915 | The function climbs up the GNAT tree starting from the node and returns true | |
916 | upon encountering a node that makes it doable to decide. lvalue_required_p | |
917 | should have been previously invoked on the arguments and returned false. */ | |
918 | ||
919 | static bool | |
920 | lvalue_for_aggregate_p (Node_Id gnat_node, tree gnu_type) | |
921 | { | |
922 | Node_Id gnat_parent = Parent (gnat_node); | |
923 | ||
924 | switch (Nkind (gnat_parent)) | |
925 | { | |
926 | case N_Parameter_Association: | |
927 | case N_Function_Call: | |
928 | case N_Procedure_Call_Statement: | |
929 | /* Even if the parameter is by copy, prefer an lvalue. */ | |
930 | return true; | |
931 | ||
6a1e04b2 EB |
932 | case N_Simple_Return_Statement: |
933 | /* Likewise for a return value. */ | |
934 | return true; | |
935 | ||
527ed00b EB |
936 | case N_Indexed_Component: |
937 | case N_Selected_Component: | |
938 | /* If an elementary component is used, take it from the constant. */ | |
939 | if (!Is_Composite_Type (Underlying_Type (Etype (gnat_parent)))) | |
940 | return false; | |
941 | ||
942 | /* ... fall through ... */ | |
943 | ||
944 | case N_Slice: | |
945 | return lvalue_for_aggregate_p (gnat_parent, | |
946 | get_unpadded_type (Etype (gnat_parent))); | |
947 | ||
948 | case N_Object_Declaration: | |
fe47e816 EB |
949 | /* For an aggregate object declaration, return false consistently. */ |
950 | return false; | |
527ed00b EB |
951 | |
952 | case N_Assignment_Statement: | |
953 | /* For an aggregate assignment, decide based on the size. */ | |
954 | { | |
955 | const HOST_WIDE_INT size = int_size_in_bytes (gnu_type); | |
956 | return size < 0 || size >= param_large_stack_frame / 4; | |
957 | } | |
958 | ||
959 | case N_Unchecked_Type_Conversion: | |
960 | case N_Type_Conversion: | |
961 | case N_Qualified_Expression: | |
962 | return lvalue_for_aggregate_p (gnat_parent, | |
963 | get_unpadded_type (Etype (gnat_parent))); | |
964 | ||
965 | case N_Allocator: | |
966 | /* We should only reach here through the N_Qualified_Expression case. | |
967 | Force an lvalue for aggregate types since a block-copy to the newly | |
968 | allocated area of memory is made. */ | |
969 | return true; | |
970 | ||
971 | default: | |
972 | return false; | |
973 | } | |
974 | ||
975 | gcc_unreachable (); | |
976 | } | |
977 | ||
978 | ||
e297e2ea EB |
979 | /* Return true if T is a constant DECL node that can be safely replaced |
980 | by its initializer. */ | |
981 | ||
982 | static bool | |
983 | constant_decl_with_initializer_p (tree t) | |
984 | { | |
985 | if (!TREE_CONSTANT (t) || !DECL_P (t) || !DECL_INITIAL (t)) | |
986 | return false; | |
987 | ||
988 | /* Return false for aggregate types that contain a placeholder since | |
989 | their initializers cannot be manipulated easily. */ | |
990 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
991 | && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t)) | |
992 | && type_contains_placeholder_p (TREE_TYPE (t))) | |
993 | return false; | |
994 | ||
995 | return true; | |
996 | } | |
997 | ||
998 | /* Return an expression equivalent to EXP but where constant DECL nodes | |
999 | have been replaced by their initializer. */ | |
1000 | ||
1001 | static tree | |
1002 | fold_constant_decl_in_expr (tree exp) | |
1003 | { | |
1004 | enum tree_code code = TREE_CODE (exp); | |
1005 | tree op0; | |
1006 | ||
1007 | switch (code) | |
1008 | { | |
1009 | case CONST_DECL: | |
1010 | case VAR_DECL: | |
1011 | if (!constant_decl_with_initializer_p (exp)) | |
1012 | return exp; | |
1013 | ||
1014 | return DECL_INITIAL (exp); | |
1015 | ||
e297e2ea EB |
1016 | case COMPONENT_REF: |
1017 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); | |
1018 | if (op0 == TREE_OPERAND (exp, 0)) | |
1019 | return exp; | |
1020 | ||
552cc590 EB |
1021 | return fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, |
1022 | TREE_OPERAND (exp, 1), NULL_TREE); | |
1023 | ||
1024 | case BIT_FIELD_REF: | |
1025 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); | |
1026 | if (op0 == TREE_OPERAND (exp, 0)) | |
1027 | return exp; | |
1028 | ||
1029 | return fold_build3 (BIT_FIELD_REF, TREE_TYPE (exp), op0, | |
1030 | TREE_OPERAND (exp, 1), TREE_OPERAND (exp, 2)); | |
e297e2ea EB |
1031 | |
1032 | case ARRAY_REF: | |
1033 | case ARRAY_RANGE_REF: | |
a239279e EB |
1034 | /* If the index is not itself constant, then nothing can be folded. */ |
1035 | if (!TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1036 | return exp; | |
e297e2ea EB |
1037 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); |
1038 | if (op0 == TREE_OPERAND (exp, 0)) | |
1039 | return exp; | |
1040 | ||
1041 | return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1), | |
544d14e1 | 1042 | TREE_OPERAND (exp, 2), TREE_OPERAND (exp, 3))); |
e297e2ea | 1043 | |
e297e2ea EB |
1044 | case REALPART_EXPR: |
1045 | case IMAGPART_EXPR: | |
ea292448 | 1046 | case VIEW_CONVERT_EXPR: |
e297e2ea EB |
1047 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); |
1048 | if (op0 == TREE_OPERAND (exp, 0)) | |
1049 | return exp; | |
1050 | ||
1051 | return fold_build1 (code, TREE_TYPE (exp), op0); | |
1052 | ||
1053 | default: | |
1054 | return exp; | |
1055 | } | |
1056 | ||
1057 | gcc_unreachable (); | |
1058 | } | |
1059 | ||
3016ec8a EB |
1060 | /* Return true if TYPE and DEF_TYPE are compatible GNAT types for Gigi. */ |
1061 | ||
1062 | static bool | |
1063 | Gigi_Types_Compatible (Entity_Id type, Entity_Id def_type) | |
1064 | { | |
1065 | /* The trivial case. */ | |
1066 | if (type == def_type) | |
1067 | return true; | |
1068 | ||
1069 | /* A class-wide type is equivalent to a subtype of itself. */ | |
1070 | if (Is_Class_Wide_Type (type)) | |
1071 | return true; | |
1072 | ||
1073 | /* A packed array type is compatible with its implementation type. */ | |
1074 | if (Is_Packed (def_type) && type == Packed_Array_Impl_Type (def_type)) | |
1075 | return true; | |
1076 | ||
1077 | /* If both types are Itypes, one may be a copy of the other. */ | |
1078 | if (Is_Itype (def_type) && Is_Itype (type)) | |
1079 | return true; | |
1080 | ||
1081 | /* If the type is incomplete and comes from a limited context, then also | |
1082 | consider its non-limited view. */ | |
1083 | if (Is_Incomplete_Type (def_type) | |
1084 | && From_Limited_With (def_type) | |
1085 | && Present (Non_Limited_View (def_type))) | |
1086 | return Gigi_Types_Compatible (type, Non_Limited_View (def_type)); | |
1087 | ||
1088 | /* If the type is incomplete/private, then also consider its full view. */ | |
1089 | if (Is_Incomplete_Or_Private_Type (def_type) | |
1090 | && Present (Full_View (def_type))) | |
1091 | return Gigi_Types_Compatible (type, Full_View (def_type)); | |
1092 | ||
1093 | return false; | |
1094 | } | |
1095 | ||
ef12e74c EB |
1096 | /* Return the full view of a private constant E, or of a renaming thereof, if |
1097 | its type has discriminants, and Empty otherwise. */ | |
1098 | ||
1099 | static Entity_Id | |
1100 | Full_View_Of_Private_Constant (Entity_Id E) | |
1101 | { | |
1102 | while (Present (Renamed_Object (E)) && Is_Entity_Name (Renamed_Object (E))) | |
1103 | E = Entity (Renamed_Object (E)); | |
1104 | ||
1105 | if (Ekind (E) != E_Constant || No (Full_View (E))) | |
1106 | return Empty; | |
1107 | ||
1108 | const Entity_Id T = Etype (E); | |
1109 | ||
1110 | if (Is_Private_Type (T) | |
1111 | && (Has_Unknown_Discriminants (T) | |
1112 | || (Present (Full_View (T)) && Has_Discriminants (Full_View (T))))) | |
1113 | return Full_View (E); | |
1114 | ||
1115 | return Empty; | |
1116 | } | |
1117 | ||
e08f1aad EB |
1118 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Identifier, to a GCC |
1119 | tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we should | |
1120 | place the result type. */ | |
a1ab4c31 AC |
1121 | |
1122 | static tree | |
1123 | Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p) | |
1124 | { | |
ef12e74c EB |
1125 | Entity_Id gnat_entity = (Nkind (gnat_node) == N_Defining_Identifier |
1126 | || Nkind (gnat_node) == N_Defining_Operator_Symbol) | |
1127 | ? gnat_node : Entity (gnat_node); | |
1128 | Entity_Id gnat_result_type; | |
1129 | tree gnu_result, gnu_result_type; | |
a1ab4c31 AC |
1130 | /* If GNAT_NODE is a constant, whether we should use the initialization |
1131 | value instead of the constant entity, typically for scalars with an | |
1132 | address clause when the parent doesn't require an lvalue. */ | |
ef12e74c | 1133 | bool use_constant_initializer; |
3016ec8a EB |
1134 | /* Whether we should require an lvalue for GNAT_NODE. Needed in |
1135 | specific circumstances only, so evaluated lazily. < 0 means | |
1136 | unknown, > 0 means known true, 0 means known false. */ | |
ef12e74c | 1137 | int require_lvalue; |
a1ab4c31 | 1138 | |
1e55d29a EB |
1139 | /* If the Etype of this node is not the same as that of the Entity, then |
1140 | something went wrong, probably in generic instantiation. However, this | |
1141 | does not apply to types. Since we sometime have strange Ekind's, just | |
3016ec8a EB |
1142 | do this test for objects, except for discriminants because their type |
1143 | may have been changed to a subtype by Exp_Ch3.Adjust_Discriminants. */ | |
1144 | gcc_assert (!Is_Object (gnat_entity) | |
1145 | || Ekind (gnat_entity) == E_Discriminant | |
ef12e74c EB |
1146 | || Etype (gnat_node) == Etype (gnat_entity) |
1147 | || Gigi_Types_Compatible (Etype (gnat_node), | |
1148 | Etype (gnat_entity))); | |
a1ab4c31 | 1149 | |
ef12e74c | 1150 | /* If this is a reference to a deferred constant whose partial view is of |
a1ab4c31 | 1151 | unconstrained private type, the proper type is on the full view of the |
ef12e74c EB |
1152 | constant, not on the full view of the type which may be unconstrained. */ |
1153 | const Entity_Id gnat_full_view = Full_View_Of_Private_Constant (gnat_entity); | |
1154 | if (Present (gnat_full_view)) | |
a1ab4c31 | 1155 | { |
ef12e74c | 1156 | gnat_entity = gnat_full_view; |
3016ec8a | 1157 | gnat_result_type = Etype (gnat_entity); |
a1ab4c31 AC |
1158 | } |
1159 | else | |
1160 | { | |
eb0f4e48 EB |
1161 | /* We use the Actual_Subtype only if it has already been elaborated, |
1162 | as we may be invoked precisely during its elaboration, otherwise | |
79069232 EB |
1163 | the Etype. Avoid using it for packed arrays to simplify things, |
1164 | except in a return statement because we need the actual size and | |
1165 | the front-end does not make it explicit in this case. */ | |
3016ec8a | 1166 | if ((Ekind (gnat_entity) == E_Constant |
eb0f4e48 EB |
1167 | || Ekind (gnat_entity) == E_Variable |
1168 | || Is_Formal (gnat_entity)) | |
3016ec8a | 1169 | && !(Is_Array_Type (Etype (gnat_entity)) |
79069232 EB |
1170 | && Present (Packed_Array_Impl_Type (Etype (gnat_entity))) |
1171 | && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement) | |
3016ec8a EB |
1172 | && Present (Actual_Subtype (gnat_entity)) |
1173 | && present_gnu_tree (Actual_Subtype (gnat_entity))) | |
1174 | gnat_result_type = Actual_Subtype (gnat_entity); | |
a1ab4c31 | 1175 | else |
3016ec8a | 1176 | gnat_result_type = Etype (gnat_node); |
a1ab4c31 AC |
1177 | } |
1178 | ||
59a6274d EB |
1179 | /* Expand the type of this identifier first if it is needed, in case it is an |
1180 | enumeral literal, which only get made when the type is expanded. There is | |
1181 | no order-of-elaboration issue here. */ | |
1182 | if (Is_Subprogram (gnat_entity)) | |
1183 | gnu_result_type = NULL_TREE; | |
1184 | else | |
1185 | gnu_result_type = get_unpadded_type (gnat_result_type); | |
a1ab4c31 | 1186 | |
e4270465 | 1187 | /* If this is a non-imported elementary constant with an address clause, |
a1ab4c31 AC |
1188 | retrieve the value instead of a pointer to be dereferenced unless |
1189 | an lvalue is required. This is generally more efficient and actually | |
1190 | required if this is a static expression because it might be used | |
1191 | in a context where a dereference is inappropriate, such as a case | |
1192 | statement alternative or a record discriminant. There is no possible | |
308e6f3a | 1193 | volatile-ness short-circuit here since Volatile constants must be |
1e17ef87 | 1194 | imported per C.6. */ |
3016ec8a EB |
1195 | if (Ekind (gnat_entity) == E_Constant |
1196 | && Is_Elementary_Type (gnat_result_type) | |
1197 | && !Is_Imported (gnat_entity) | |
1198 | && Present (Address_Clause (gnat_entity))) | |
a1ab4c31 | 1199 | { |
86da71db EB |
1200 | require_lvalue |
1201 | = lvalue_required_p (gnat_node, gnu_result_type, true, false); | |
a1ab4c31 AC |
1202 | use_constant_initializer = !require_lvalue; |
1203 | } | |
ef12e74c EB |
1204 | else |
1205 | { | |
1206 | require_lvalue = -1; | |
1207 | use_constant_initializer = false; | |
1208 | } | |
a1ab4c31 | 1209 | |
ef12e74c | 1210 | /* Fetch the initialization value of a constant if requested. */ |
a1ab4c31 AC |
1211 | if (use_constant_initializer) |
1212 | { | |
1213 | /* If this is a deferred constant, the initializer is attached to | |
1214 | the full view. */ | |
3016ec8a EB |
1215 | if (Present (Full_View (gnat_entity))) |
1216 | gnat_entity = Full_View (gnat_entity); | |
a1ab4c31 | 1217 | |
3016ec8a | 1218 | gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_entity))); |
a1ab4c31 AC |
1219 | } |
1220 | else | |
3016ec8a | 1221 | gnu_result = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
a1ab4c31 | 1222 | |
a1ab4c31 AC |
1223 | /* Some objects (such as parameters passed by reference, globals of |
1224 | variable size, and renamed objects) actually represent the address | |
1225 | of the object. In that case, we must do the dereference. Likewise, | |
1226 | deal with parameters to foreign convention subprograms. */ | |
1227 | if (DECL_P (gnu_result) | |
1228 | && (DECL_BY_REF_P (gnu_result) | |
1229 | || (TREE_CODE (gnu_result) == PARM_DECL | |
1230 | && DECL_BY_COMPONENT_PTR_P (gnu_result)))) | |
1231 | { | |
ced57283 | 1232 | const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result); |
a1ab4c31 | 1233 | |
ad1d36ba | 1234 | /* If it's a PARM_DECL to foreign convention subprogram, convert it. */ |
a1ab4c31 AC |
1235 | if (TREE_CODE (gnu_result) == PARM_DECL |
1236 | && DECL_BY_COMPONENT_PTR_P (gnu_result)) | |
ad1d36ba EB |
1237 | gnu_result |
1238 | = convert (build_pointer_type (gnu_result_type), gnu_result); | |
1239 | ||
1240 | /* If it's a CONST_DECL, return the underlying constant like below. */ | |
e4270465 EB |
1241 | else if (TREE_CODE (gnu_result) == CONST_DECL |
1242 | && !(DECL_CONST_ADDRESS_P (gnu_result) | |
1243 | && lvalue_required_p (gnat_node, gnu_result_type, true, | |
86da71db | 1244 | true))) |
ad1d36ba | 1245 | gnu_result = DECL_INITIAL (gnu_result); |
a1ab4c31 | 1246 | |
5bdd063b EB |
1247 | /* Do the final dereference. */ |
1248 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result); | |
a1ab4c31 | 1249 | |
04682fe7 | 1250 | if ((INDIRECT_REF_P (gnu_result) |
5bdd063b EB |
1251 | || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF) |
1252 | && No (Address_Clause (gnat_entity))) | |
1253 | TREE_THIS_NOTRAP (gnu_result) = 1; | |
ad1d36ba | 1254 | |
5bdd063b EB |
1255 | if (read_only) |
1256 | TREE_READONLY (gnu_result) = 1; | |
a1ab4c31 AC |
1257 | } |
1258 | ||
58c8f770 EB |
1259 | /* If we have a constant declaration and its initializer, try to return the |
1260 | latter to avoid the need to call fold in lots of places and the need for | |
e297e2ea EB |
1261 | elaboration code if this identifier is used as an initializer itself. */ |
1262 | if (constant_decl_with_initializer_p (gnu_result)) | |
a1ab4c31 | 1263 | { |
c34f3839 EB |
1264 | bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL |
1265 | && !DECL_CONST_CORRESPONDING_VAR (gnu_result)); | |
cb3d597d EB |
1266 | bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL |
1267 | && DECL_CONST_ADDRESS_P (gnu_result)); | |
1268 | ||
1269 | /* If there is a (corresponding) variable or this is the address of a | |
1270 | constant, we only want to return the initializer if an lvalue isn't | |
1271 | required. Evaluate this now if we have not already done so. */ | |
1272 | if ((!constant_only || address_of_constant) && require_lvalue < 0) | |
1273 | require_lvalue | |
1274 | = lvalue_required_p (gnat_node, gnu_result_type, true, | |
527ed00b EB |
1275 | address_of_constant) |
1276 | || (AGGREGATE_TYPE_P (gnu_result_type) | |
1277 | && lvalue_for_aggregate_p (gnat_node, gnu_result_type)); | |
cb3d597d | 1278 | |
6ba4f08f | 1279 | /* Finally retrieve the initializer if this is deemed valid. */ |
cb3d597d | 1280 | if ((constant_only && !address_of_constant) || !require_lvalue) |
6ba4f08f | 1281 | gnu_result = DECL_INITIAL (gnu_result); |
a1ab4c31 AC |
1282 | } |
1283 | ||
e297e2ea EB |
1284 | /* But for a constant renaming we couldn't do that incrementally for its |
1285 | definition because of the need to return an lvalue so, if the present | |
1286 | context doesn't itself require an lvalue, we try again here. */ | |
3016ec8a EB |
1287 | else if (Ekind (gnat_entity) == E_Constant |
1288 | && Is_Elementary_Type (gnat_result_type) | |
1289 | && Present (Renamed_Object (gnat_entity))) | |
e297e2ea EB |
1290 | { |
1291 | if (require_lvalue < 0) | |
1292 | require_lvalue | |
86da71db | 1293 | = lvalue_required_p (gnat_node, gnu_result_type, true, false); |
e297e2ea EB |
1294 | if (!require_lvalue) |
1295 | gnu_result = fold_constant_decl_in_expr (gnu_result); | |
1296 | } | |
1297 | ||
1c4ae4e5 EB |
1298 | /* The GNAT tree has the type of a function set to its result type, so we |
1299 | adjust here. Also use the type of the result if the Etype is a subtype | |
1300 | that is nominally unconstrained. Likewise if this is a deferred constant | |
1301 | of a discriminated type whose full view can be elaborated statically, to | |
1302 | avoid problematic conversions to the nominal subtype. But remove any | |
1303 | padding from the resulting type. */ | |
69720717 | 1304 | if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_result)) |
3016ec8a EB |
1305 | || Is_Constr_Subt_For_UN_Aliased (gnat_result_type) |
1306 | || (Ekind (gnat_entity) == E_Constant | |
1307 | && Present (Full_View (gnat_entity)) | |
1308 | && Has_Discriminants (gnat_result_type) | |
1c4ae4e5 EB |
1309 | && TREE_CODE (gnu_result) == CONSTRUCTOR)) |
1310 | { | |
1311 | gnu_result_type = TREE_TYPE (gnu_result); | |
1312 | if (TYPE_IS_PADDING_P (gnu_result_type)) | |
1313 | gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type)); | |
1314 | } | |
1315 | ||
a1ab4c31 | 1316 | *gnu_result_type_p = gnu_result_type; |
58c8f770 | 1317 | |
a1ab4c31 AC |
1318 | return gnu_result; |
1319 | } | |
e76a8cac | 1320 | |
e08f1aad EB |
1321 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Pragma, to a GCC |
1322 | tree, which is returned. */ | |
a1ab4c31 AC |
1323 | |
1324 | static tree | |
1325 | Pragma_to_gnu (Node_Id gnat_node) | |
1326 | { | |
47625858 | 1327 | tree gnu_result = alloc_stmt_list (); |
f2423384 | 1328 | Node_Id gnat_temp; |
a1ab4c31 | 1329 | |
ec4a0d83 EB |
1330 | /* Check for (and ignore) unrecognized pragmas. */ |
1331 | if (!Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node)))) | |
a1ab4c31 AC |
1332 | return gnu_result; |
1333 | ||
ec4a0d83 EB |
1334 | const unsigned char id |
1335 | = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node))); | |
1336 | ||
1337 | /* Save the expression of pragma Compile_Time_{Error|Warning} for later. */ | |
1338 | if (id == Pragma_Compile_Time_Error || id == Pragma_Compile_Time_Warning) | |
1339 | { | |
1340 | gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1341 | gnat_compile_time_expr_list.safe_push (Expression (gnat_temp)); | |
1342 | return gnu_result; | |
1343 | } | |
1344 | ||
1345 | /* Stop there if we are just annotating types. */ | |
1346 | if (type_annotate_only) | |
1347 | return gnu_result; | |
1348 | ||
1349 | switch (id) | |
a1ab4c31 AC |
1350 | { |
1351 | case Pragma_Inspection_Point: | |
1352 | /* Do nothing at top level: all such variables are already viewable. */ | |
1353 | if (global_bindings_p ()) | |
1354 | break; | |
1355 | ||
1356 | for (gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1357 | Present (gnat_temp); | |
1358 | gnat_temp = Next (gnat_temp)) | |
1359 | { | |
1360 | Node_Id gnat_expr = Expression (gnat_temp); | |
1361 | tree gnu_expr = gnat_to_gnu (gnat_expr); | |
a1ab4c31 AC |
1362 | tree asm_constraint = NULL_TREE; |
1363 | #ifdef ASM_COMMENT_START | |
1364 | char *comment; | |
1365 | #endif | |
9e4cacfa | 1366 | gnu_expr = maybe_unconstrained_array (gnu_expr); |
0c6fbbfc EB |
1367 | if (TREE_CODE (gnu_expr) == CONST_DECL |
1368 | && DECL_CONST_CORRESPONDING_VAR (gnu_expr)) | |
1369 | gnu_expr = DECL_CONST_CORRESPONDING_VAR (gnu_expr); | |
9e4cacfa | 1370 | gnat_mark_addressable (gnu_expr); |
a1ab4c31 AC |
1371 | |
1372 | #ifdef ASM_COMMENT_START | |
1373 | comment = concat (ASM_COMMENT_START, | |
1374 | " inspection point: ", | |
1375 | Get_Name_String (Chars (gnat_expr)), | |
9e4cacfa | 1376 | " is at %0", |
a1ab4c31 AC |
1377 | NULL); |
1378 | asm_constraint = build_string (strlen (comment), comment); | |
1379 | free (comment); | |
1380 | #endif | |
1c384bf1 | 1381 | gnu_expr = build5 (ASM_EXPR, void_type_node, |
a1ab4c31 AC |
1382 | asm_constraint, |
1383 | NULL_TREE, | |
1384 | tree_cons | |
1385 | (build_tree_list (NULL_TREE, | |
9e4cacfa EB |
1386 | build_string (1, "m")), |
1387 | gnu_expr, NULL_TREE), | |
1c384bf1 | 1388 | NULL_TREE, NULL_TREE); |
a1ab4c31 AC |
1389 | ASM_VOLATILE_P (gnu_expr) = 1; |
1390 | set_expr_location_from_node (gnu_expr, gnat_node); | |
1391 | append_to_statement_list (gnu_expr, &gnu_result); | |
1392 | } | |
1393 | break; | |
1394 | ||
633a3f2a EB |
1395 | case Pragma_Loop_Optimize: |
1396 | for (gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1397 | Present (gnat_temp); | |
1398 | gnat_temp = Next (gnat_temp)) | |
1399 | { | |
3418f5e9 | 1400 | tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt; |
633a3f2a EB |
1401 | |
1402 | switch (Chars (Expression (gnat_temp))) | |
1403 | { | |
3418f5e9 EB |
1404 | case Name_Ivdep: |
1405 | LOOP_STMT_IVDEP (gnu_loop_stmt) = 1; | |
1406 | break; | |
1407 | ||
633a3f2a EB |
1408 | case Name_No_Unroll: |
1409 | LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1; | |
1410 | break; | |
1411 | ||
1412 | case Name_Unroll: | |
1413 | LOOP_STMT_UNROLL (gnu_loop_stmt) = 1; | |
1414 | break; | |
1415 | ||
1416 | case Name_No_Vector: | |
1417 | LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1; | |
1418 | break; | |
1419 | ||
1420 | case Name_Vector: | |
1421 | LOOP_STMT_VECTOR (gnu_loop_stmt) = 1; | |
1422 | break; | |
1423 | ||
1424 | default: | |
1425 | gcc_unreachable (); | |
1426 | } | |
1427 | } | |
1428 | break; | |
1429 | ||
a1ab4c31 AC |
1430 | case Pragma_Optimize: |
1431 | switch (Chars (Expression | |
1432 | (First (Pragma_Argument_Associations (gnat_node))))) | |
1433 | { | |
a1ab4c31 | 1434 | case Name_Off: |
e84319a3 | 1435 | if (optimize) |
4a29b8d6 | 1436 | post_error ("must specify -O0??", gnat_node); |
a1ab4c31 AC |
1437 | break; |
1438 | ||
257e81a6 EB |
1439 | case Name_Space: |
1440 | if (!optimize_size) | |
4a29b8d6 | 1441 | post_error ("must specify -Os??", gnat_node); |
257e81a6 EB |
1442 | break; |
1443 | ||
1444 | case Name_Time: | |
1445 | if (!optimize) | |
4a29b8d6 | 1446 | post_error ("insufficient -O value??", gnat_node); |
257e81a6 EB |
1447 | break; |
1448 | ||
a1ab4c31 AC |
1449 | default: |
1450 | gcc_unreachable (); | |
1451 | } | |
1452 | break; | |
1453 | ||
1454 | case Pragma_Reviewable: | |
1455 | if (write_symbols == NO_DEBUG) | |
4a29b8d6 | 1456 | post_error ("must specify -g??", gnat_node); |
a1ab4c31 | 1457 | break; |
f2423384 | 1458 | |
2749b01b | 1459 | case Pragma_Warning_As_Error: |
f2423384 EB |
1460 | case Pragma_Warnings: |
1461 | { | |
1462 | Node_Id gnat_expr; | |
1463 | /* Preserve the location of the pragma. */ | |
1464 | const location_t location = input_location; | |
1465 | struct cl_option_handlers handlers; | |
1466 | unsigned int option_index; | |
1467 | diagnostic_t kind; | |
1468 | bool imply; | |
1469 | ||
1470 | gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1471 | ||
2749b01b | 1472 | /* This is the String form: pragma Warning{s|_As_Error}(String). */ |
f2423384 EB |
1473 | if (Nkind (Expression (gnat_temp)) == N_String_Literal) |
1474 | { | |
ec4a0d83 | 1475 | switch (id) |
2749b01b EB |
1476 | { |
1477 | case Pragma_Warning_As_Error: | |
1478 | kind = DK_ERROR; | |
1479 | imply = false; | |
1480 | break; | |
1481 | ||
1482 | case Pragma_Warnings: | |
1483 | kind = DK_WARNING; | |
1484 | imply = true; | |
1485 | break; | |
1486 | ||
1487 | default: | |
1488 | gcc_unreachable (); | |
1489 | } | |
1490 | ||
f2423384 | 1491 | gnat_expr = Expression (gnat_temp); |
f2423384 EB |
1492 | } |
1493 | ||
1494 | /* This is the On/Off form: pragma Warnings (On | Off [,String]). */ | |
1495 | else if (Nkind (Expression (gnat_temp)) == N_Identifier) | |
1496 | { | |
1497 | switch (Chars (Expression (gnat_temp))) | |
1498 | { | |
1499 | case Name_Off: | |
1500 | kind = DK_IGNORED; | |
1501 | break; | |
1502 | ||
1503 | case Name_On: | |
1504 | kind = DK_WARNING; | |
1505 | break; | |
1506 | ||
1507 | default: | |
1508 | gcc_unreachable (); | |
1509 | } | |
1510 | ||
f10ff6cc | 1511 | /* Deal with optional pattern (but ignore Reason => "..."). */ |
113c69ff EB |
1512 | if (Present (Next (gnat_temp)) |
1513 | && Chars (Next (gnat_temp)) != Name_Reason) | |
f2423384 EB |
1514 | { |
1515 | /* pragma Warnings (On | Off, Name) is handled differently. */ | |
1516 | if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal) | |
1517 | break; | |
1518 | ||
1519 | gnat_expr = Expression (Next (gnat_temp)); | |
1520 | } | |
1521 | else | |
e3371908 EB |
1522 | { |
1523 | gnat_expr = Empty; | |
1524 | ||
1525 | /* For pragma Warnings (Off), we save the current state... */ | |
1526 | if (kind == DK_IGNORED) | |
1527 | diagnostic_push_diagnostics (global_dc, location); | |
1528 | ||
1529 | /* ...so that, for pragma Warnings (On), we do not enable all | |
1530 | the warnings but just restore the previous state. */ | |
1531 | else | |
1532 | { | |
1533 | diagnostic_pop_diagnostics (global_dc, location); | |
1534 | break; | |
1535 | } | |
1536 | } | |
f2423384 EB |
1537 | |
1538 | imply = false; | |
1539 | } | |
1540 | ||
1541 | else | |
1542 | gcc_unreachable (); | |
1543 | ||
1544 | /* This is the same implementation as in the C family of compilers. */ | |
0cec1c2d | 1545 | const unsigned int lang_mask = CL_Ada | CL_COMMON; |
63bbf46d | 1546 | const char *arg = NULL; |
f2423384 EB |
1547 | if (Present (gnat_expr)) |
1548 | { | |
1549 | tree gnu_expr = gnat_to_gnu (gnat_expr); | |
0cec1c2d | 1550 | const char *option_string = TREE_STRING_POINTER (gnu_expr); |
f2423384 | 1551 | const int len = TREE_STRING_LENGTH (gnu_expr); |
0cec1c2d | 1552 | if (len < 3 || option_string[0] != '-' || option_string[1] != 'W') |
f2423384 | 1553 | break; |
0cec1c2d EB |
1554 | option_index = find_opt (option_string + 1, lang_mask); |
1555 | if (option_index == OPT_SPECIAL_unknown) | |
1556 | { | |
4a29b8d6 | 1557 | post_error ("unknown -W switch??", gnat_node); |
f2423384 | 1558 | break; |
0cec1c2d EB |
1559 | } |
1560 | else if (!(cl_options[option_index].flags & CL_WARNING)) | |
1561 | { | |
4a29b8d6 | 1562 | post_error ("-W switch does not control warning??", gnat_node); |
0cec1c2d EB |
1563 | break; |
1564 | } | |
1565 | else if (!(cl_options[option_index].flags & lang_mask)) | |
f2423384 | 1566 | { |
4a29b8d6 | 1567 | post_error ("-W switch not valid for Ada??", gnat_node); |
f2423384 EB |
1568 | break; |
1569 | } | |
63bbf46d JJ |
1570 | if (cl_options[option_index].flags & CL_JOINED) |
1571 | arg = option_string + 1 + cl_options[option_index].opt_len; | |
f2423384 EB |
1572 | } |
1573 | else | |
1574 | option_index = 0; | |
1575 | ||
0694da45 | 1576 | set_default_handlers (&handlers, NULL); |
63bbf46d | 1577 | control_warning_option (option_index, (int) kind, arg, imply, location, |
0cec1c2d | 1578 | lang_mask, &handlers, &global_options, |
f2423384 EB |
1579 | &global_options_set, global_dc); |
1580 | } | |
1581 | break; | |
633a3f2a EB |
1582 | |
1583 | default: | |
1584 | break; | |
a1ab4c31 AC |
1585 | } |
1586 | ||
1587 | return gnu_result; | |
1588 | } | |
ce2d0ce2 | 1589 | |
ae5a77fa | 1590 | /* Check the inline status of nested function FNDECL wrt its parent function. |
87411e95 EB |
1591 | |
1592 | If a non-inline nested function is referenced from an inline external | |
1593 | function, we cannot honor both requests at the same time without cloning | |
1594 | the nested function in the current unit since it is private to its unit. | |
1595 | We could inline it as well but it's probably better to err on the side | |
1596 | of too little inlining. | |
1597 | ||
ae5a77fa | 1598 | This must be done only on nested functions present in the source code |
87411e95 | 1599 | and not on nested functions generated by the compiler, e.g. finalizers, |
ae5a77fa EB |
1600 | because they may be not marked inline and we don't want them to block |
1601 | the inlining of the parent function. */ | |
87411e95 EB |
1602 | |
1603 | static void | |
1604 | check_inlining_for_nested_subprog (tree fndecl) | |
1605 | { | |
ae5a77fa EB |
1606 | if (DECL_IGNORED_P (current_function_decl) || DECL_IGNORED_P (fndecl)) |
1607 | return; | |
1608 | ||
1609 | if (DECL_DECLARED_INLINE_P (fndecl)) | |
1610 | return; | |
1611 | ||
1612 | tree parent_decl = decl_function_context (fndecl); | |
1613 | if (DECL_EXTERNAL (parent_decl) && DECL_DECLARED_INLINE_P (parent_decl)) | |
87411e95 EB |
1614 | { |
1615 | const location_t loc1 = DECL_SOURCE_LOCATION (fndecl); | |
ae5a77fa | 1616 | const location_t loc2 = DECL_SOURCE_LOCATION (parent_decl); |
87411e95 | 1617 | |
ae5a77fa | 1618 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (parent_decl))) |
87411e95 | 1619 | { |
a9c697b8 MS |
1620 | error_at (loc1, "subprogram %q+F not marked %<Inline_Always%>", |
1621 | fndecl); | |
87411e95 EB |
1622 | error_at (loc2, "parent subprogram cannot be inlined"); |
1623 | } | |
1624 | else | |
1625 | { | |
a9c697b8 | 1626 | warning_at (loc1, OPT_Winline, "subprogram %q+F not marked %<Inline%>", |
87411e95 EB |
1627 | fndecl); |
1628 | warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined"); | |
1629 | } | |
1630 | ||
ae5a77fa EB |
1631 | DECL_DECLARED_INLINE_P (parent_decl) = 0; |
1632 | DECL_UNINLINABLE (parent_decl) = 1; | |
87411e95 EB |
1633 | } |
1634 | } | |
ce2d0ce2 | 1635 | |
1eb58520 AC |
1636 | /* Return an expression for the length of TYPE, an integral type, computed in |
1637 | RESULT_TYPE, another integral type. | |
1638 | ||
1639 | We used to compute the length as MAX (hb - lb + 1, 0) which could overflow | |
1640 | when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0 | |
1641 | which would only overflow in much rarer cases, for extremely large arrays | |
1642 | we expect never to encounter in practice. Besides, the former computation | |
1643 | required the use of potentially constraining signed arithmetics while the | |
1644 | latter does not. Note that the comparison must be done in the original | |
1645 | base index type in order to avoid any overflow during the conversion. */ | |
1646 | ||
1647 | static tree | |
1648 | get_type_length (tree type, tree result_type) | |
1649 | { | |
1650 | tree comp_type = get_base_type (result_type); | |
825da0d2 | 1651 | tree base_type = maybe_character_type (get_base_type (type)); |
1eb58520 AC |
1652 | tree lb = convert (base_type, TYPE_MIN_VALUE (type)); |
1653 | tree hb = convert (base_type, TYPE_MAX_VALUE (type)); | |
1654 | tree length | |
1655 | = build_binary_op (PLUS_EXPR, comp_type, | |
1656 | build_binary_op (MINUS_EXPR, comp_type, | |
1657 | convert (comp_type, hb), | |
1658 | convert (comp_type, lb)), | |
9a1bdc31 | 1659 | build_int_cst (comp_type, 1)); |
1eb58520 AC |
1660 | length |
1661 | = build_cond_expr (result_type, | |
1662 | build_binary_op (GE_EXPR, boolean_type_node, hb, lb), | |
1663 | convert (result_type, length), | |
9a1bdc31 | 1664 | build_int_cst (result_type, 0)); |
1eb58520 AC |
1665 | return length; |
1666 | } | |
1667 | ||
e08f1aad EB |
1668 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node, to a |
1669 | GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we | |
1670 | should place the result type. ATTRIBUTE is the attribute ID. */ | |
a1ab4c31 AC |
1671 | |
1672 | static tree | |
1673 | Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute) | |
1674 | { | |
088d3b0f | 1675 | const Node_Id gnat_prefix = Prefix (gnat_node); |
1e55d29a EB |
1676 | tree gnu_prefix = gnat_to_gnu (gnat_prefix); |
1677 | tree gnu_type = TREE_TYPE (gnu_prefix); | |
1678 | tree gnu_expr, gnu_result_type, gnu_result = error_mark_node; | |
caa9d12a | 1679 | bool prefix_unused = false; |
544d14e1 | 1680 | Entity_Id gnat_smo; |
a1ab4c31 AC |
1681 | |
1682 | /* If the input is a NULL_EXPR, make a new one. */ | |
1683 | if (TREE_CODE (gnu_prefix) == NULL_EXPR) | |
1684 | { | |
feec4372 EB |
1685 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
1686 | *gnu_result_type_p = gnu_result_type; | |
1687 | return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0)); | |
a1ab4c31 AC |
1688 | } |
1689 | ||
544d14e1 EB |
1690 | /* If the input is a LOAD_EXPR of an unconstrained array type, the second |
1691 | operand contains the storage model object. */ | |
1692 | if (TREE_CODE (gnu_prefix) == LOAD_EXPR | |
1693 | && TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
1694 | gnat_smo = tree_to_shwi (TREE_OPERAND (gnu_prefix, 1)); | |
1695 | else | |
1696 | gnat_smo = Empty; | |
1697 | ||
a1ab4c31 AC |
1698 | switch (attribute) |
1699 | { | |
a1ab4c31 AC |
1700 | case Attr_Pred: |
1701 | case Attr_Succ: | |
feec4372 EB |
1702 | /* These just add or subtract the constant 1 since representation |
1703 | clauses for enumeration types are handled in the front-end. */ | |
a1ab4c31 AC |
1704 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); |
1705 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
825da0d2 EB |
1706 | gnu_type = maybe_character_type (gnu_result_type); |
1707 | if (TREE_TYPE (gnu_expr) != gnu_type) | |
1708 | gnu_expr = convert (gnu_type, gnu_expr); | |
a1ab4c31 | 1709 | gnu_result |
feec4372 | 1710 | = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR, |
825da0d2 | 1711 | gnu_type, gnu_expr, build_int_cst (gnu_type, 1)); |
a1ab4c31 AC |
1712 | break; |
1713 | ||
1714 | case Attr_Address: | |
1715 | case Attr_Unrestricted_Access: | |
3fd9ae96 EB |
1716 | /* Conversions don't change the address of references but can cause |
1717 | build_unary_op to miss the references below, so strip them off. | |
1718 | On the contrary, if the address-of operation causes a temporary | |
1719 | to be created, then it must be created with the proper type. */ | |
1720 | gnu_expr = remove_conversions (gnu_prefix, | |
1721 | !Must_Be_Byte_Aligned (gnat_node)); | |
1722 | if (REFERENCE_CLASS_P (gnu_expr)) | |
1723 | gnu_prefix = gnu_expr; | |
a1ab4c31 AC |
1724 | |
1725 | /* If we are taking 'Address of an unconstrained object, this is the | |
1726 | pointer to the underlying array. */ | |
1727 | if (attribute == Attr_Address) | |
1728 | gnu_prefix = maybe_unconstrained_array (gnu_prefix); | |
1729 | ||
1730 | /* If we are building a static dispatch table, we have to honor | |
1731 | TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible | |
1732 | with the C++ ABI. We do it in the non-static case as well, | |
1733 | see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */ | |
1734 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
1735 | && Is_Dispatch_Table_Entity (Etype (gnat_node))) | |
1736 | { | |
0e228dd9 | 1737 | tree gnu_field, t; |
a1ab4c31 AC |
1738 | /* Descriptors can only be built here for top-level functions. */ |
1739 | bool build_descriptor = (global_bindings_p () != 0); | |
1740 | int i; | |
9771b263 | 1741 | vec<constructor_elt, va_gc> *gnu_vec = NULL; |
0e228dd9 | 1742 | constructor_elt *elt; |
a1ab4c31 AC |
1743 | |
1744 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
1745 | ||
1746 | /* If we're not going to build the descriptor, we have to retrieve | |
1747 | the one which will be built by the linker (or by the compiler | |
1748 | later if a static chain is requested). */ | |
1749 | if (!build_descriptor) | |
1750 | { | |
1751 | gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix); | |
1752 | gnu_result = fold_convert (build_pointer_type (gnu_result_type), | |
1753 | gnu_result); | |
1754 | gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result); | |
1755 | } | |
1756 | ||
cb3874dc | 1757 | vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS, true); |
9771b263 | 1758 | elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1); |
a1ab4c31 AC |
1759 | for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0; |
1760 | i < TARGET_VTABLE_USES_DESCRIPTORS; | |
7d76717d | 1761 | gnu_field = DECL_CHAIN (gnu_field), i++) |
a1ab4c31 AC |
1762 | { |
1763 | if (build_descriptor) | |
1764 | { | |
1765 | t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix, | |
1766 | build_int_cst (NULL_TREE, i)); | |
1767 | TREE_CONSTANT (t) = 1; | |
1768 | } | |
1769 | else | |
1770 | t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result, | |
1771 | gnu_field, NULL_TREE); | |
1772 | ||
0e228dd9 NF |
1773 | elt->index = gnu_field; |
1774 | elt->value = t; | |
1775 | elt--; | |
a1ab4c31 AC |
1776 | } |
1777 | ||
0e228dd9 | 1778 | gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec); |
a1ab4c31 AC |
1779 | break; |
1780 | } | |
1781 | ||
9c453de7 | 1782 | /* ... fall through ... */ |
a1ab4c31 AC |
1783 | |
1784 | case Attr_Access: | |
1785 | case Attr_Unchecked_Access: | |
1786 | case Attr_Code_Address: | |
a5720c08 EB |
1787 | /* Taking the address of a type does not make sense. */ |
1788 | gcc_assert (TREE_CODE (gnu_prefix) != TYPE_DECL); | |
1789 | ||
a1ab4c31 AC |
1790 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
1791 | gnu_result | |
1792 | = build_unary_op (((attribute == Attr_Address | |
1793 | || attribute == Attr_Unrestricted_Access) | |
1794 | && !Must_Be_Byte_Aligned (gnat_node)) | |
1795 | ? ATTR_ADDR_EXPR : ADDR_EXPR, | |
1796 | gnu_result_type, gnu_prefix); | |
1797 | ||
1798 | /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we | |
1e039275 | 1799 | don't try to build a trampoline. */ |
a1ab4c31 AC |
1800 | if (attribute == Attr_Code_Address) |
1801 | { | |
722356ce | 1802 | gnu_expr = remove_conversions (gnu_result, false); |
a1ab4c31 AC |
1803 | |
1804 | if (TREE_CODE (gnu_expr) == ADDR_EXPR) | |
1805 | TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1; | |
4c640e26 EB |
1806 | |
1807 | /* On targets for which function symbols denote a descriptor, the | |
1808 | code address is stored within the first slot of the descriptor | |
1809 | so we do an additional dereference: | |
1810 | result = *((result_type *) result) | |
1811 | where we expect result to be of some pointer type already. */ | |
1812 | if (targetm.calls.custom_function_descriptors == 0) | |
1813 | gnu_result | |
1814 | = build_unary_op (INDIRECT_REF, NULL_TREE, | |
1815 | convert (build_pointer_type (gnu_result_type), | |
1816 | gnu_result)); | |
a1ab4c31 AC |
1817 | } |
1818 | ||
28dd0055 EB |
1819 | /* For 'Access, issue an error message if the prefix is a C++ method |
1820 | since it can use a special calling convention on some platforms, | |
1821 | which cannot be propagated to the access type. */ | |
1822 | else if (attribute == Attr_Access | |
69720717 | 1823 | && TREE_CODE (TREE_TYPE (gnu_prefix)) == METHOD_TYPE) |
28dd0055 EB |
1824 | post_error ("access to C++ constructor or member function not allowed", |
1825 | gnat_node); | |
1826 | ||
a1ab4c31 AC |
1827 | /* For other address attributes applied to a nested function, |
1828 | find an inner ADDR_EXPR and annotate it so that we can issue | |
1829 | a useful warning with -Wtrampolines. */ | |
ef5a9557 EB |
1830 | else if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_prefix)) |
1831 | && (gnu_expr = remove_conversions (gnu_result, false)) | |
1832 | && TREE_CODE (gnu_expr) == ADDR_EXPR | |
1833 | && decl_function_context (TREE_OPERAND (gnu_expr, 0))) | |
a1ab4c31 | 1834 | { |
ef5a9557 | 1835 | set_expr_location_from_node (gnu_expr, gnat_node); |
a1ab4c31 | 1836 | |
ef5a9557 EB |
1837 | /* Also check the inlining status. */ |
1838 | check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0)); | |
1839 | ||
1840 | /* Moreover, for 'Access or 'Unrestricted_Access with non- | |
1841 | foreign-compatible representation, mark the ADDR_EXPR so | |
1842 | that we can build a descriptor instead of a trampoline. */ | |
1843 | if ((attribute == Attr_Access | |
1844 | || attribute == Attr_Unrestricted_Access) | |
1845 | && targetm.calls.custom_function_descriptors > 0 | |
1846 | && Can_Use_Internal_Rep (Underlying_Type (Etype (gnat_node)))) | |
1847 | FUNC_ADDR_BY_DESCRIPTOR (gnu_expr) = 1; | |
1848 | ||
1849 | /* Otherwise, we need to check that we are not violating the | |
1850 | No_Implicit_Dynamic_Code restriction. */ | |
1851 | else if (targetm.calls.custom_function_descriptors != 0) | |
1852 | Check_Implicit_Dynamic_Code_Allowed (gnat_node); | |
a1ab4c31 AC |
1853 | } |
1854 | break; | |
1855 | ||
1856 | case Attr_Pool_Address: | |
1857 | { | |
a1ab4c31 | 1858 | tree gnu_ptr = gnu_prefix; |
0d7de0e1 | 1859 | tree gnu_obj_type; |
a1ab4c31 AC |
1860 | |
1861 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
1862 | ||
0d7de0e1 EB |
1863 | /* If this is fat pointer, the object must have been allocated with the |
1864 | template in front of the array. So compute the template address; do | |
1865 | it by converting to a thin pointer. */ | |
315cff15 | 1866 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
1867 | gnu_ptr |
1868 | = convert (build_pointer_type | |
1869 | (TYPE_OBJECT_RECORD_TYPE | |
1870 | (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))), | |
1871 | gnu_ptr); | |
1872 | ||
1873 | gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr)); | |
0d7de0e1 EB |
1874 | |
1875 | /* If this is a thin pointer, the object must have been allocated with | |
1876 | the template in front of the array. So compute the template address | |
1877 | and return it. */ | |
1878 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) | |
1879 | gnu_ptr | |
1880 | = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr), | |
1881 | gnu_ptr, | |
2b45154d EB |
1882 | fold_build1 (NEGATE_EXPR, sizetype, |
1883 | byte_position | |
1884 | (DECL_CHAIN | |
1885 | TYPE_FIELDS ((gnu_obj_type))))); | |
a1ab4c31 AC |
1886 | |
1887 | gnu_result = convert (gnu_result_type, gnu_ptr); | |
1888 | } | |
1889 | break; | |
1890 | ||
1891 | case Attr_Size: | |
1892 | case Attr_Object_Size: | |
1893 | case Attr_Value_Size: | |
1894 | case Attr_Max_Size_In_Storage_Elements: | |
79069232 EB |
1895 | /* Strip NOPs, conversions between original and packable versions, and |
1896 | unpadding from GNU_PREFIX. Note that we cannot simply strip every | |
1897 | VIEW_CONVERT_EXPR because some of them may give the actual size, e.g. | |
1898 | for nominally unconstrained packed array. We use GNU_EXPR to see | |
1899 | if a COMPONENT_REF was involved. */ | |
1900 | while (CONVERT_EXPR_P (gnu_prefix) | |
1901 | || TREE_CODE (gnu_prefix) == NON_LVALUE_EXPR | |
1902 | || (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR | |
1903 | && TREE_CODE (TREE_TYPE (gnu_prefix)) == RECORD_TYPE | |
1904 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))) | |
20faffe7 | 1905 | == RECORD_TYPE |
79069232 EB |
1906 | && TYPE_NAME (TREE_TYPE (gnu_prefix)) |
1907 | == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))) | |
1908 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); | |
1909 | gnu_expr = gnu_prefix; | |
1910 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF | |
1911 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))) | |
1912 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); | |
a1ab4c31 AC |
1913 | prefix_unused = true; |
1914 | gnu_type = TREE_TYPE (gnu_prefix); | |
1915 | ||
1916 | /* Replace an unconstrained array type with the type of the underlying | |
65e0a92b EB |
1917 | array, except for 'Max_Size_In_Storage_Elements because we need to |
1918 | return the (maximum) size requested for an allocator. */ | |
a1ab4c31 AC |
1919 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) |
1920 | { | |
1921 | gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type); | |
1922 | if (attribute != Attr_Max_Size_In_Storage_Elements) | |
7d76717d | 1923 | gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))); |
a1ab4c31 AC |
1924 | } |
1925 | ||
da81cc57 EB |
1926 | /* The type must be frozen at this point. */ |
1927 | gcc_assert (COMPLETE_TYPE_P (gnu_type)); | |
1928 | ||
842d4ee2 | 1929 | /* If we're looking for the size of a field, return the field size. */ |
a1ab4c31 AC |
1930 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) |
1931 | gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1)); | |
842d4ee2 EB |
1932 | |
1933 | /* Otherwise, if the prefix is an object, or if we are looking for | |
1934 | 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the | |
1935 | GCC size of the type. We make an exception for padded objects, | |
1936 | as we do not take into account alignment promotions for the size. | |
1937 | This is in keeping with the object case of gnat_to_gnu_entity. */ | |
1938 | else if ((TREE_CODE (gnu_prefix) != TYPE_DECL | |
1939 | && !(TYPE_IS_PADDING_P (gnu_type) | |
4d39941e EB |
1940 | && TREE_CODE (gnu_expr) == COMPONENT_REF |
1941 | && pad_type_has_rm_size (gnu_type))) | |
a1ab4c31 AC |
1942 | || attribute == Attr_Object_Size |
1943 | || attribute == Attr_Max_Size_In_Storage_Elements) | |
1944 | { | |
842d4ee2 EB |
1945 | /* If this is a dereference and we have a special dynamic constrained |
1946 | subtype on the prefix, use it to compute the size; otherwise, use | |
1947 | the designated subtype. */ | |
d5518dab EB |
1948 | if (Nkind (gnat_prefix) == N_Explicit_Dereference |
1949 | && Present (Actual_Designated_Subtype (gnat_prefix))) | |
a1ab4c31 | 1950 | { |
d5518dab EB |
1951 | tree gnu_actual_obj_type |
1952 | = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_prefix)); | |
1e17ef87 | 1953 | tree gnu_ptr_type |
088d3b0f | 1954 | = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix))); |
1e17ef87 | 1955 | |
d5518dab EB |
1956 | if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)) |
1957 | gnu_type | |
1958 | = build_unc_object_type_from_ptr (gnu_ptr_type, | |
1959 | gnu_actual_obj_type, | |
1960 | get_identifier ("SIZE"), | |
1961 | false); | |
a1ab4c31 | 1962 | } |
842d4ee2 EB |
1963 | |
1964 | gnu_result = TYPE_SIZE (gnu_type); | |
a1ab4c31 | 1965 | } |
842d4ee2 EB |
1966 | |
1967 | /* Otherwise, the result is the RM size of the type. */ | |
a1ab4c31 AC |
1968 | else |
1969 | gnu_result = rm_size (gnu_type); | |
1970 | ||
683ccd05 EB |
1971 | /* Deal with a self-referential size by qualifying the size with the |
1972 | object or returning the maximum size for a type. */ | |
1973 | if (TREE_CODE (gnu_prefix) != TYPE_DECL) | |
544d14e1 EB |
1974 | { |
1975 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); | |
1976 | if (Present (gnat_smo)) | |
1977 | gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo); | |
1978 | } | |
683ccd05 EB |
1979 | else if (CONTAINS_PLACEHOLDER_P (gnu_result)) |
1980 | gnu_result = max_size (gnu_result, true); | |
a1ab4c31 | 1981 | |
65e0a92b EB |
1982 | /* If the type contains a template, subtract the padded size of the |
1983 | template, except for 'Max_Size_In_Storage_Elements because we need | |
1984 | to return the (maximum) size requested for an allocator. */ | |
a1ab4c31 | 1985 | if (TREE_CODE (gnu_type) == RECORD_TYPE |
65e0a92b EB |
1986 | && TYPE_CONTAINS_TEMPLATE_P (gnu_type) |
1987 | && attribute != Attr_Max_Size_In_Storage_Elements) | |
1988 | gnu_result | |
1989 | = size_binop (MINUS_EXPR, gnu_result, | |
1990 | bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)))); | |
a1ab4c31 | 1991 | |
58c8f770 | 1992 | /* For 'Max_Size_In_Storage_Elements, adjust the unit. */ |
a1ab4c31 | 1993 | if (attribute == Attr_Max_Size_In_Storage_Elements) |
58c8f770 EB |
1994 | gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node); |
1995 | ||
1996 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
a1ab4c31 AC |
1997 | break; |
1998 | ||
1999 | case Attr_Alignment: | |
caa9d12a EB |
2000 | { |
2001 | unsigned int align; | |
a1ab4c31 | 2002 | |
caa9d12a | 2003 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF |
315cff15 | 2004 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))) |
caa9d12a | 2005 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); |
a1ab4c31 | 2006 | |
caa9d12a EB |
2007 | gnu_type = TREE_TYPE (gnu_prefix); |
2008 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2009 | prefix_unused = true; | |
2010 | ||
2011 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) | |
2012 | align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT; | |
2013 | else | |
2014 | { | |
caa9d12a EB |
2015 | Entity_Id gnat_type = Etype (gnat_prefix); |
2016 | unsigned int double_align; | |
2017 | bool is_capped_double, align_clause; | |
2018 | ||
2019 | /* If the default alignment of "double" or larger scalar types is | |
2020 | specifically capped and there is an alignment clause neither | |
2021 | on the type nor on the prefix itself, return the cap. */ | |
2022 | if ((double_align = double_float_alignment) > 0) | |
2023 | is_capped_double | |
2024 | = is_double_float_or_array (gnat_type, &align_clause); | |
2025 | else if ((double_align = double_scalar_alignment) > 0) | |
2026 | is_capped_double | |
2027 | = is_double_scalar_or_array (gnat_type, &align_clause); | |
2028 | else | |
2029 | is_capped_double = align_clause = false; | |
2030 | ||
2031 | if (is_capped_double | |
2032 | && Nkind (gnat_prefix) == N_Identifier | |
2033 | && Present (Alignment_Clause (Entity (gnat_prefix)))) | |
2034 | align_clause = true; | |
2035 | ||
2036 | if (is_capped_double && !align_clause) | |
2037 | align = double_align; | |
2038 | else | |
2039 | align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT; | |
2040 | } | |
2041 | ||
2042 | gnu_result = size_int (align); | |
2043 | } | |
a1ab4c31 AC |
2044 | break; |
2045 | ||
2046 | case Attr_First: | |
2047 | case Attr_Last: | |
2048 | case Attr_Range_Length: | |
2049 | prefix_unused = true; | |
2050 | ||
c95f808d | 2051 | if (INTEGRAL_TYPE_P (gnu_type) || SCALAR_FLOAT_TYPE_P (gnu_type)) |
a1ab4c31 AC |
2052 | { |
2053 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2054 | ||
2055 | if (attribute == Attr_First) | |
2056 | gnu_result = TYPE_MIN_VALUE (gnu_type); | |
2057 | else if (attribute == Attr_Last) | |
2058 | gnu_result = TYPE_MAX_VALUE (gnu_type); | |
2059 | else | |
1eb58520 | 2060 | gnu_result = get_type_length (gnu_type, gnu_result_type); |
a1ab4c31 AC |
2061 | break; |
2062 | } | |
2063 | ||
9c453de7 | 2064 | /* ... fall through ... */ |
a1ab4c31 AC |
2065 | |
2066 | case Attr_Length: | |
2067 | { | |
2068 | int Dimension = (Present (Expressions (gnat_node)) | |
2069 | ? UI_To_Int (Intval (First (Expressions (gnat_node)))) | |
2070 | : 1), i; | |
6bf68a93 | 2071 | struct parm_attr_d *pa = NULL; |
a1ab4c31 | 2072 | Entity_Id gnat_param = Empty; |
088d3b0f | 2073 | bool unconstrained_ptr_deref = false; |
a1ab4c31 | 2074 | |
bb24f343 | 2075 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 | 2076 | gnu_prefix = maybe_unconstrained_array (gnu_prefix); |
ad1d36ba | 2077 | |
088d3b0f EB |
2078 | /* We treat unconstrained array In parameters specially. We also note |
2079 | whether we are dereferencing a pointer to unconstrained array. */ | |
2080 | if (!Is_Constrained (Etype (gnat_prefix))) | |
2081 | switch (Nkind (gnat_prefix)) | |
2082 | { | |
2083 | case N_Identifier: | |
2084 | /* This is the direct case. */ | |
2085 | if (Ekind (Entity (gnat_prefix)) == E_In_Parameter) | |
2086 | gnat_param = Entity (gnat_prefix); | |
2087 | break; | |
2088 | ||
2089 | case N_Explicit_Dereference: | |
2090 | /* This is the indirect case. Note that we need to be sure that | |
2091 | the access value cannot be null as we'll hoist the load. */ | |
2092 | if (Nkind (Prefix (gnat_prefix)) == N_Identifier | |
2093 | && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter) | |
2094 | { | |
2095 | if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix)))) | |
2096 | gnat_param = Entity (Prefix (gnat_prefix)); | |
2097 | } | |
2098 | else | |
2099 | unconstrained_ptr_deref = true; | |
2100 | break; | |
2101 | ||
2102 | default: | |
2103 | break; | |
ad1d36ba EB |
2104 | } |
2105 | ||
9b17f12b EB |
2106 | /* If the prefix is the view conversion of a constrained array to an |
2107 | unconstrained form, we retrieve the constrained array because we | |
2108 | might not be able to substitute the PLACEHOLDER_EXPR coming from | |
2109 | the conversion. This can occur with the 'Old attribute applied | |
2110 | to a parameter with an unconstrained type, which gets rewritten | |
2111 | into a constrained local variable very late in the game. */ | |
2112 | if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR | |
2113 | && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix))) | |
2114 | && !CONTAINS_PLACEHOLDER_P | |
2115 | (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))) | |
2116 | gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)); | |
2117 | else | |
2118 | gnu_type = TREE_TYPE (gnu_prefix); | |
2119 | ||
a1ab4c31 AC |
2120 | prefix_unused = true; |
2121 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2122 | ||
2123 | if (TYPE_CONVENTION_FORTRAN_P (gnu_type)) | |
2124 | { | |
2125 | int ndim; | |
2126 | tree gnu_type_temp; | |
2127 | ||
2128 | for (ndim = 1, gnu_type_temp = gnu_type; | |
2129 | TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE | |
2130 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp)); | |
2131 | ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp)) | |
2132 | ; | |
2133 | ||
2134 | Dimension = ndim + 1 - Dimension; | |
2135 | } | |
2136 | ||
2137 | for (i = 1; i < Dimension; i++) | |
2138 | gnu_type = TREE_TYPE (gnu_type); | |
2139 | ||
2140 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
2141 | ||
2142 | /* When not optimizing, look up the slot associated with the parameter | |
9a30c7c4 AC |
2143 | and the dimension in the cache and create a new one on failure. |
2144 | Don't do this when the actual subtype needs debug info (this happens | |
2145 | with -gnatD): in elaborate_expression_1, we create variables that | |
2146 | hold the bounds, so caching attributes isn't very interesting and | |
2147 | causes dependency issues between these variables and cached | |
2148 | expressions. */ | |
2149 | if (!optimize | |
2150 | && Present (gnat_param) | |
2151 | && !(Present (Actual_Subtype (gnat_param)) | |
2152 | && Needs_Debug_Info (Actual_Subtype (gnat_param)))) | |
a1ab4c31 | 2153 | { |
9771b263 | 2154 | FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa) |
a1ab4c31 AC |
2155 | if (pa->id == gnat_param && pa->dim == Dimension) |
2156 | break; | |
2157 | ||
2158 | if (!pa) | |
2159 | { | |
766090c2 | 2160 | pa = ggc_cleared_alloc<parm_attr_d> (); |
a1ab4c31 AC |
2161 | pa->id = gnat_param; |
2162 | pa->dim = Dimension; | |
9771b263 | 2163 | vec_safe_push (f_parm_attr_cache, pa); |
a1ab4c31 AC |
2164 | } |
2165 | } | |
2166 | ||
2167 | /* Return the cached expression or build a new one. */ | |
2168 | if (attribute == Attr_First) | |
2169 | { | |
2170 | if (pa && pa->first) | |
2171 | { | |
2172 | gnu_result = pa->first; | |
2173 | break; | |
2174 | } | |
2175 | ||
2176 | gnu_result | |
2177 | = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))); | |
2178 | } | |
2179 | ||
2180 | else if (attribute == Attr_Last) | |
2181 | { | |
2182 | if (pa && pa->last) | |
2183 | { | |
2184 | gnu_result = pa->last; | |
2185 | break; | |
2186 | } | |
2187 | ||
2188 | gnu_result | |
2189 | = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))); | |
2190 | } | |
2191 | ||
2192 | else /* attribute == Attr_Range_Length || attribute == Attr_Length */ | |
2193 | { | |
2194 | if (pa && pa->length) | |
2195 | { | |
2196 | gnu_result = pa->length; | |
2197 | break; | |
2198 | } | |
1eb58520 AC |
2199 | |
2200 | gnu_result | |
2201 | = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)), | |
2202 | gnu_result_type); | |
a1ab4c31 AC |
2203 | } |
2204 | ||
2205 | /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are | |
2206 | handling. Note that these attributes could not have been used on | |
2207 | an unconstrained array type. */ | |
4e6602a8 | 2208 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); |
544d14e1 EB |
2209 | if (Present (gnat_smo)) |
2210 | gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo); | |
a1ab4c31 AC |
2211 | |
2212 | /* Cache the expression we have just computed. Since we want to do it | |
c01fe451 | 2213 | at run time, we force the use of a SAVE_EXPR and let the gimplifier |
586fea26 EB |
2214 | create the temporary in the outermost binding level. We will make |
2215 | sure in Subprogram_Body_to_gnu that it is evaluated on all possible | |
2216 | paths by forcing its evaluation on entry of the function. */ | |
a1ab4c31 AC |
2217 | if (pa) |
2218 | { | |
2219 | gnu_result | |
2220 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result); | |
088d3b0f EB |
2221 | switch (attribute) |
2222 | { | |
2223 | case Attr_First: | |
2224 | pa->first = gnu_result; | |
2225 | break; | |
2226 | ||
2227 | case Attr_Last: | |
2228 | pa->last = gnu_result; | |
2229 | break; | |
2230 | ||
2231 | case Attr_Length: | |
2232 | case Attr_Range_Length: | |
2233 | pa->length = gnu_result; | |
2234 | break; | |
2235 | ||
2236 | default: | |
2237 | gcc_unreachable (); | |
2238 | } | |
a1ab4c31 | 2239 | } |
321e10dd | 2240 | |
088d3b0f EB |
2241 | /* Otherwise, evaluate it each time it is referenced. */ |
2242 | else | |
2243 | switch (attribute) | |
2244 | { | |
2245 | case Attr_First: | |
2246 | case Attr_Last: | |
2247 | /* If we are dereferencing a pointer to unconstrained array, we | |
2248 | need to capture the value because the pointed-to bounds may | |
2249 | subsequently be released. */ | |
2250 | if (unconstrained_ptr_deref) | |
2251 | gnu_result | |
2252 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result); | |
2253 | break; | |
2254 | ||
2255 | case Attr_Length: | |
2256 | case Attr_Range_Length: | |
2257 | /* Set the source location onto the predicate of the condition | |
2258 | but not if the expression is cached to avoid messing up the | |
2259 | debug info. */ | |
2260 | if (TREE_CODE (gnu_result) == COND_EXPR | |
2261 | && EXPR_P (TREE_OPERAND (gnu_result, 0))) | |
2262 | set_expr_location_from_node (TREE_OPERAND (gnu_result, 0), | |
2263 | gnat_node); | |
2264 | break; | |
2265 | ||
2266 | default: | |
2267 | gcc_unreachable (); | |
2268 | } | |
321e10dd | 2269 | |
a1ab4c31 AC |
2270 | break; |
2271 | } | |
2272 | ||
2273 | case Attr_Bit_Position: | |
2274 | case Attr_Position: | |
2275 | case Attr_First_Bit: | |
2276 | case Attr_Last_Bit: | |
2277 | case Attr_Bit: | |
2278 | { | |
f37fac2b RS |
2279 | poly_int64 bitsize; |
2280 | poly_int64 bitpos; | |
a1ab4c31 AC |
2281 | tree gnu_offset; |
2282 | tree gnu_field_bitpos; | |
2283 | tree gnu_field_offset; | |
2284 | tree gnu_inner; | |
ef4bddc2 | 2285 | machine_mode mode; |
ee45a32d | 2286 | int unsignedp, reversep, volatilep; |
a1ab4c31 AC |
2287 | |
2288 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2289 | gnu_prefix = remove_conversions (gnu_prefix, true); | |
2290 | prefix_unused = true; | |
2291 | ||
2292 | /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF, | |
1e17ef87 | 2293 | the result is 0. Don't allow 'Bit on a bare component, though. */ |
a1ab4c31 AC |
2294 | if (attribute == Attr_Bit |
2295 | && TREE_CODE (gnu_prefix) != COMPONENT_REF | |
2296 | && TREE_CODE (gnu_prefix) != FIELD_DECL) | |
2297 | { | |
2298 | gnu_result = integer_zero_node; | |
2299 | break; | |
2300 | } | |
2301 | ||
2302 | else | |
2303 | gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF | |
2304 | || (attribute == Attr_Bit_Position | |
2305 | && TREE_CODE (gnu_prefix) == FIELD_DECL)); | |
2306 | ||
2307 | get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset, | |
25b75a48 | 2308 | &mode, &unsignedp, &reversep, &volatilep); |
a1ab4c31 AC |
2309 | |
2310 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) | |
2311 | { | |
2312 | gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1)); | |
2313 | gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1)); | |
2314 | ||
2315 | for (gnu_inner = TREE_OPERAND (gnu_prefix, 0); | |
2316 | TREE_CODE (gnu_inner) == COMPONENT_REF | |
2317 | && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1)); | |
2318 | gnu_inner = TREE_OPERAND (gnu_inner, 0)) | |
2319 | { | |
2320 | gnu_field_bitpos | |
2321 | = size_binop (PLUS_EXPR, gnu_field_bitpos, | |
2322 | bit_position (TREE_OPERAND (gnu_inner, 1))); | |
2323 | gnu_field_offset | |
2324 | = size_binop (PLUS_EXPR, gnu_field_offset, | |
2325 | byte_position (TREE_OPERAND (gnu_inner, 1))); | |
2326 | } | |
2327 | } | |
2328 | else if (TREE_CODE (gnu_prefix) == FIELD_DECL) | |
2329 | { | |
2330 | gnu_field_bitpos = bit_position (gnu_prefix); | |
2331 | gnu_field_offset = byte_position (gnu_prefix); | |
2332 | } | |
2333 | else | |
2334 | { | |
2335 | gnu_field_bitpos = bitsize_zero_node; | |
2336 | gnu_field_offset = size_zero_node; | |
2337 | } | |
2338 | ||
2339 | switch (attribute) | |
2340 | { | |
2341 | case Attr_Position: | |
2342 | gnu_result = gnu_field_offset; | |
2343 | break; | |
2344 | ||
2345 | case Attr_First_Bit: | |
2346 | case Attr_Bit: | |
f37fac2b | 2347 | gnu_result = size_int (num_trailing_bits (bitpos)); |
a1ab4c31 AC |
2348 | break; |
2349 | ||
2350 | case Attr_Last_Bit: | |
f37fac2b | 2351 | gnu_result = bitsize_int (num_trailing_bits (bitpos)); |
a1ab4c31 AC |
2352 | gnu_result = size_binop (PLUS_EXPR, gnu_result, |
2353 | TYPE_SIZE (TREE_TYPE (gnu_prefix))); | |
7cc15171 EB |
2354 | /* ??? Avoid a large unsigned result that will overflow when |
2355 | converted to the signed universal_integer. */ | |
2356 | if (integer_zerop (gnu_result)) | |
2357 | gnu_result = integer_minus_one_node; | |
2358 | else | |
2359 | gnu_result | |
2360 | = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node); | |
a1ab4c31 AC |
2361 | break; |
2362 | ||
2363 | case Attr_Bit_Position: | |
2364 | gnu_result = gnu_field_bitpos; | |
2365 | break; | |
7cc15171 | 2366 | } |
a1ab4c31 | 2367 | |
feec4372 EB |
2368 | /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are |
2369 | handling. */ | |
a1ab4c31 | 2370 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); |
544d14e1 EB |
2371 | if (Present (gnat_smo)) |
2372 | gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo); | |
a1ab4c31 AC |
2373 | break; |
2374 | } | |
2375 | ||
2376 | case Attr_Min: | |
2377 | case Attr_Max: | |
2378 | { | |
2379 | tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node))); | |
2380 | tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node)))); | |
2381 | ||
2382 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
881cdd61 EB |
2383 | |
2384 | /* The result of {MIN,MAX}_EXPR is unspecified if either operand is | |
2385 | a NaN so we implement the semantics of C99 f{min,max} to make it | |
2386 | predictable in this case: if either operand is a NaN, the other | |
2387 | is returned; if both operands are NaN's, a NaN is returned. */ | |
89d5c50b EB |
2388 | if (SCALAR_FLOAT_TYPE_P (gnu_result_type) |
2389 | && !Machine_Overflows_On_Target) | |
881cdd61 EB |
2390 | { |
2391 | const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs); | |
2392 | const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs); | |
2393 | tree t = builtin_decl_explicit (BUILT_IN_ISNAN); | |
2394 | tree lhs_is_nan, rhs_is_nan; | |
2395 | ||
2396 | /* If the operands have side-effects, they need to be evaluated | |
2397 | only once in spite of the multiple references in the result. */ | |
2398 | if (lhs_side_effects_p) | |
2399 | gnu_lhs = gnat_protect_expr (gnu_lhs); | |
2400 | if (rhs_side_effects_p) | |
2401 | gnu_rhs = gnat_protect_expr (gnu_rhs); | |
2402 | ||
2403 | lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node, | |
2404 | build_call_expr (t, 1, gnu_lhs), | |
2405 | integer_zero_node); | |
2406 | ||
2407 | rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node, | |
2408 | build_call_expr (t, 1, gnu_rhs), | |
2409 | integer_zero_node); | |
2410 | ||
2411 | gnu_result = build_binary_op (attribute == Attr_Min | |
2412 | ? MIN_EXPR : MAX_EXPR, | |
2413 | gnu_result_type, gnu_lhs, gnu_rhs); | |
2414 | gnu_result = fold_build3 (COND_EXPR, gnu_result_type, | |
2415 | rhs_is_nan, gnu_lhs, gnu_result); | |
2416 | gnu_result = fold_build3 (COND_EXPR, gnu_result_type, | |
2417 | lhs_is_nan, gnu_rhs, gnu_result); | |
2418 | ||
2419 | /* If the operands have side-effects, they need to be evaluated | |
2420 | before doing the tests above since the place they otherwise | |
2421 | would end up being evaluated at run time could be wrong. */ | |
2422 | if (lhs_side_effects_p) | |
2423 | gnu_result | |
2424 | = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result); | |
2425 | ||
2426 | if (rhs_side_effects_p) | |
2427 | gnu_result | |
2428 | = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result); | |
2429 | } | |
2430 | else | |
2431 | gnu_result = build_binary_op (attribute == Attr_Min | |
2432 | ? MIN_EXPR : MAX_EXPR, | |
2433 | gnu_result_type, gnu_lhs, gnu_rhs); | |
a1ab4c31 AC |
2434 | } |
2435 | break; | |
2436 | ||
2437 | case Attr_Passed_By_Reference: | |
2438 | gnu_result = size_int (default_pass_by_ref (gnu_type) | |
2439 | || must_pass_by_ref (gnu_type)); | |
2440 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2441 | break; | |
2442 | ||
2443 | case Attr_Component_Size: | |
bb24f343 | 2444 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 AC |
2445 | gnu_type = TREE_TYPE (gnu_prefix); |
2446 | ||
2447 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
2448 | gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type)))); | |
2449 | ||
2450 | while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE | |
2451 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) | |
2452 | gnu_type = TREE_TYPE (gnu_type); | |
2453 | ||
2454 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
2455 | ||
2456 | /* Note this size cannot be self-referential. */ | |
2457 | gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type)); | |
2458 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2459 | prefix_unused = true; | |
2460 | break; | |
2461 | ||
203ddcea AC |
2462 | case Attr_Descriptor_Size: |
2463 | gnu_type = TREE_TYPE (gnu_prefix); | |
2464 | gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE); | |
2465 | ||
65e0a92b | 2466 | /* Return the padded size of the template in the object record type. */ |
203ddcea | 2467 | gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type); |
2b45154d | 2468 | gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type))); |
203ddcea AC |
2469 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
2470 | prefix_unused = true; | |
2471 | break; | |
2472 | ||
a1ab4c31 | 2473 | case Attr_Null_Parameter: |
feec4372 EB |
2474 | /* This is just a zero cast to the pointer type for our prefix and |
2475 | dereferenced. */ | |
a1ab4c31 AC |
2476 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
2477 | gnu_result | |
2478 | = build_unary_op (INDIRECT_REF, NULL_TREE, | |
2479 | convert (build_pointer_type (gnu_result_type), | |
2480 | integer_zero_node)); | |
a1ab4c31 AC |
2481 | break; |
2482 | ||
2483 | case Attr_Mechanism_Code: | |
2484 | { | |
088d3b0f | 2485 | Entity_Id gnat_obj = Entity (gnat_prefix); |
a1ab4c31 | 2486 | int code; |
a1ab4c31 AC |
2487 | |
2488 | prefix_unused = true; | |
2489 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2490 | if (Present (Expressions (gnat_node))) | |
2491 | { | |
2492 | int i = UI_To_Int (Intval (First (Expressions (gnat_node)))); | |
2493 | ||
2494 | for (gnat_obj = First_Formal (gnat_obj); i > 1; | |
2495 | i--, gnat_obj = Next_Formal (gnat_obj)) | |
2496 | ; | |
2497 | } | |
2498 | ||
2499 | code = Mechanism (gnat_obj); | |
2500 | if (code == Default) | |
2501 | code = ((present_gnu_tree (gnat_obj) | |
2502 | && (DECL_BY_REF_P (get_gnu_tree (gnat_obj)) | |
2503 | || ((TREE_CODE (get_gnu_tree (gnat_obj)) | |
2504 | == PARM_DECL) | |
2505 | && (DECL_BY_COMPONENT_PTR_P | |
2506 | (get_gnu_tree (gnat_obj)))))) | |
2507 | ? By_Reference : By_Copy); | |
2508 | gnu_result = convert (gnu_result_type, size_int (- code)); | |
2509 | } | |
2510 | break; | |
2511 | ||
24228312 AC |
2512 | case Attr_Model: |
2513 | /* We treat Model as identical to Machine. This is true for at least | |
2514 | IEEE and some other nice floating-point systems. */ | |
2515 | ||
9c453de7 | 2516 | /* ... fall through ... */ |
24228312 AC |
2517 | |
2518 | case Attr_Machine: | |
2519 | /* The trick is to force the compiler to store the result in memory so | |
2520 | that we do not have extra precision used. But do this only when this | |
1eb58520 AC |
2521 | is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of |
2522 | the type is lower than that of the longest floating-point type. */ | |
24228312 AC |
2523 | prefix_unused = true; |
2524 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); | |
2525 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2526 | gnu_result = convert (gnu_result_type, gnu_expr); | |
2527 | ||
c68cdfac EB |
2528 | if (TREE_CODE (gnu_result) != REAL_CST |
2529 | && fp_arith_may_widen | |
1eb58520 AC |
2530 | && TYPE_PRECISION (gnu_result_type) |
2531 | < TYPE_PRECISION (longest_float_type_node)) | |
24228312 AC |
2532 | { |
2533 | tree rec_type = make_node (RECORD_TYPE); | |
2534 | tree field | |
2535 | = create_field_decl (get_identifier ("OBJ"), gnu_result_type, | |
2536 | rec_type, NULL_TREE, NULL_TREE, 0, 0); | |
2537 | tree rec_val, asm_expr; | |
2538 | ||
2539 | finish_record_type (rec_type, field, 0, false); | |
2540 | ||
2541 | rec_val = build_constructor_single (rec_type, field, gnu_result); | |
c68cdfac | 2542 | rec_val = build1 (SAVE_EXPR, rec_type, rec_val); |
24228312 AC |
2543 | |
2544 | asm_expr | |
2545 | = build5 (ASM_EXPR, void_type_node, | |
2546 | build_string (0, ""), | |
2547 | tree_cons (build_tree_list (NULL_TREE, | |
2548 | build_string (2, "=m")), | |
2549 | rec_val, NULL_TREE), | |
2550 | tree_cons (build_tree_list (NULL_TREE, | |
2551 | build_string (1, "m")), | |
2552 | rec_val, NULL_TREE), | |
2553 | NULL_TREE, NULL_TREE); | |
2554 | ASM_VOLATILE_P (asm_expr) = 1; | |
2555 | ||
2556 | gnu_result | |
2557 | = build_compound_expr (gnu_result_type, asm_expr, | |
64235766 EB |
2558 | build_component_ref (rec_val, field, |
2559 | false)); | |
24228312 AC |
2560 | } |
2561 | break; | |
2562 | ||
06d75031 EB |
2563 | case Attr_Deref: |
2564 | prefix_unused = true; | |
2565 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); | |
2566 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2567 | /* This can be a random address so build an alias-all pointer type. */ | |
2568 | gnu_expr | |
2569 | = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode, | |
2570 | true), | |
2571 | gnu_expr); | |
2572 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr); | |
2573 | break; | |
2574 | ||
a1ab4c31 | 2575 | default: |
010dffa9 EB |
2576 | /* This abort means that we have an unimplemented attribute. */ |
2577 | gcc_unreachable (); | |
a1ab4c31 AC |
2578 | } |
2579 | ||
2580 | /* If this is an attribute where the prefix was unused, force a use of it if | |
2581 | it has a side-effect. But don't do it if the prefix is just an entity | |
2582 | name. However, if an access check is needed, we must do it. See second | |
1e17ef87 | 2583 | example in AARM 11.6(5.e). */ |
088d3b0f EB |
2584 | if (prefix_unused |
2585 | && TREE_SIDE_EFFECTS (gnu_prefix) | |
2586 | && !Is_Entity_Name (gnat_prefix)) | |
2587 | gnu_result | |
24228312 | 2588 | = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result); |
a1ab4c31 AC |
2589 | |
2590 | *gnu_result_type_p = gnu_result_type; | |
2591 | return gnu_result; | |
2592 | } | |
ce2d0ce2 | 2593 | |
e08f1aad EB |
2594 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Case_Statement, to a |
2595 | GCC tree, which is returned. */ | |
a1ab4c31 AC |
2596 | |
2597 | static tree | |
2598 | Case_Statement_to_gnu (Node_Id gnat_node) | |
2599 | { | |
a31d78c6 | 2600 | tree gnu_result, gnu_expr, gnu_type, gnu_label; |
a1ab4c31 | 2601 | Node_Id gnat_when; |
2d3c7e4f | 2602 | location_t end_locus; |
83e279c4 | 2603 | bool may_fallthru = false; |
a1ab4c31 AC |
2604 | |
2605 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); | |
2606 | gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr); | |
a31d78c6 EB |
2607 | gnu_expr = maybe_character_value (gnu_expr); |
2608 | gnu_type = TREE_TYPE (gnu_expr); | |
a1ab4c31 | 2609 | |
a1ab4c31 AC |
2610 | /* We build a SWITCH_EXPR that contains the code with interspersed |
2611 | CASE_LABEL_EXPRs for each label. */ | |
08b8b90c | 2612 | if (!Sloc_to_locus (End_Location (gnat_node), &end_locus)) |
2d3c7e4f EB |
2613 | end_locus = input_location; |
2614 | gnu_label = create_artificial_label (end_locus); | |
a1ab4c31 | 2615 | start_stmt_group (); |
b4f73deb | 2616 | |
a1ab4c31 AC |
2617 | for (gnat_when = First_Non_Pragma (Alternatives (gnat_node)); |
2618 | Present (gnat_when); | |
2619 | gnat_when = Next_Non_Pragma (gnat_when)) | |
2620 | { | |
9c69c3af | 2621 | bool choices_added_p = false; |
a1ab4c31 | 2622 | Node_Id gnat_choice; |
a1ab4c31 AC |
2623 | |
2624 | /* First compile all the different case choices for the current WHEN | |
2625 | alternative. */ | |
2626 | for (gnat_choice = First (Discrete_Choices (gnat_when)); | |
ac43e11e AC |
2627 | Present (gnat_choice); |
2628 | gnat_choice = Next (gnat_choice)) | |
a1ab4c31 AC |
2629 | { |
2630 | tree gnu_low = NULL_TREE, gnu_high = NULL_TREE; | |
ac43e11e | 2631 | tree label = create_artificial_label (input_location); |
a1ab4c31 AC |
2632 | |
2633 | switch (Nkind (gnat_choice)) | |
2634 | { | |
2635 | case N_Range: | |
2636 | gnu_low = gnat_to_gnu (Low_Bound (gnat_choice)); | |
2637 | gnu_high = gnat_to_gnu (High_Bound (gnat_choice)); | |
2638 | break; | |
2639 | ||
2640 | case N_Subtype_Indication: | |
2641 | gnu_low = gnat_to_gnu (Low_Bound (Range_Expression | |
2642 | (Constraint (gnat_choice)))); | |
2643 | gnu_high = gnat_to_gnu (High_Bound (Range_Expression | |
2644 | (Constraint (gnat_choice)))); | |
2645 | break; | |
2646 | ||
2647 | case N_Identifier: | |
2648 | case N_Expanded_Name: | |
2649 | /* This represents either a subtype range or a static value of | |
2650 | some kind; Ekind says which. */ | |
7ed9919d | 2651 | if (Is_Type (Entity (gnat_choice))) |
a1ab4c31 AC |
2652 | { |
2653 | tree gnu_type = get_unpadded_type (Entity (gnat_choice)); | |
2654 | ||
ac43e11e AC |
2655 | gnu_low = TYPE_MIN_VALUE (gnu_type); |
2656 | gnu_high = TYPE_MAX_VALUE (gnu_type); | |
a1ab4c31 AC |
2657 | break; |
2658 | } | |
2659 | ||
9c453de7 | 2660 | /* ... fall through ... */ |
a1ab4c31 AC |
2661 | |
2662 | case N_Character_Literal: | |
2663 | case N_Integer_Literal: | |
2664 | gnu_low = gnat_to_gnu (gnat_choice); | |
2665 | break; | |
2666 | ||
2667 | case N_Others_Choice: | |
2668 | break; | |
2669 | ||
2670 | default: | |
2671 | gcc_unreachable (); | |
2672 | } | |
2673 | ||
ac43e11e AC |
2674 | /* Everything should be folded into constants at this point. */ |
2675 | gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST); | |
2676 | gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST); | |
2677 | ||
a31d78c6 EB |
2678 | if (gnu_low && TREE_TYPE (gnu_low) != gnu_type) |
2679 | gnu_low = convert (gnu_type, gnu_low); | |
2680 | if (gnu_high && TREE_TYPE (gnu_high) != gnu_type) | |
2681 | gnu_high = convert (gnu_type, gnu_high); | |
2682 | ||
ac43e11e AC |
2683 | add_stmt_with_node (build_case_label (gnu_low, gnu_high, label), |
2684 | gnat_choice); | |
2685 | choices_added_p = true; | |
a1ab4c31 AC |
2686 | } |
2687 | ||
a1d7a124 EB |
2688 | /* This construct doesn't define a scope so we shouldn't push a binding |
2689 | level around the statement list. Except that we have always done so | |
2690 | historically and this makes it possible to reduce stack usage. As a | |
2691 | compromise, we keep doing it for case statements, for which this has | |
2692 | never been problematic, but not for case expressions in Ada 2012. */ | |
9c69c3af | 2693 | if (choices_added_p) |
a1ab4c31 | 2694 | { |
a1d7a124 EB |
2695 | const bool is_case_expression |
2696 | = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions); | |
2697 | tree group | |
2698 | = build_stmt_group (Statements (gnat_when), !is_case_expression); | |
83e279c4 EB |
2699 | bool group_may_fallthru = block_may_fallthru (group); |
2700 | add_stmt (group); | |
2701 | if (group_may_fallthru) | |
2702 | { | |
2d3c7e4f EB |
2703 | tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label); |
2704 | SET_EXPR_LOCATION (stmt, end_locus); | |
2705 | add_stmt (stmt); | |
83e279c4 EB |
2706 | may_fallthru = true; |
2707 | } | |
a1ab4c31 AC |
2708 | } |
2709 | } | |
2710 | ||
41a961e9 | 2711 | /* Now emit a definition of the label the cases branch to, if any. */ |
83e279c4 EB |
2712 | if (may_fallthru) |
2713 | add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label)); | |
9e851845 | 2714 | gnu_result = build2 (SWITCH_EXPR, gnu_type, gnu_expr, end_stmt_group ()); |
a1ab4c31 AC |
2715 | |
2716 | return gnu_result; | |
2717 | } | |
ce2d0ce2 | 2718 | |
87ab2b04 EB |
2719 | /* Return true if we are in the body of a loop. */ |
2720 | ||
2721 | static inline bool | |
2722 | inside_loop_p (void) | |
2723 | { | |
2724 | return !vec_safe_is_empty (gnu_loop_stack); | |
2725 | } | |
2726 | ||
933a7325 EB |
2727 | /* Find out whether EXPR is a simple additive expression based on the iteration |
2728 | variable of some enclosing loop in the current function. If so, return the | |
2729 | loop and set *DISP to the displacement and *NEG_P to true if this is for a | |
2730 | subtraction; otherwise, return NULL. */ | |
15bf7d19 | 2731 | |
87ab2b04 | 2732 | static struct loop_info_d * |
22903f22 | 2733 | find_loop_for (tree expr, tree *disp, bool *neg_p) |
15bf7d19 | 2734 | { |
933a7325 EB |
2735 | tree var, add, cst; |
2736 | bool minus_p; | |
15bf7d19 EB |
2737 | struct loop_info_d *iter = NULL; |
2738 | unsigned int i; | |
2739 | ||
933a7325 EB |
2740 | if (is_simple_additive_expression (expr, &add, &cst, &minus_p)) |
2741 | { | |
2742 | var = add; | |
2743 | if (disp) | |
2744 | *disp = cst; | |
2745 | if (neg_p) | |
2746 | *neg_p = minus_p; | |
2747 | } | |
2748 | else | |
2749 | { | |
2750 | var = expr; | |
2751 | if (disp) | |
2752 | *disp = NULL_TREE; | |
2753 | if (neg_p) | |
2754 | *neg_p = false; | |
2755 | } | |
2756 | ||
722356ce | 2757 | var = remove_conversions (var, false); |
15bf7d19 EB |
2758 | |
2759 | if (TREE_CODE (var) != VAR_DECL) | |
2760 | return NULL; | |
2761 | ||
852dd866 EB |
2762 | gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0); |
2763 | ||
2764 | FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter) | |
2765 | if (iter->loop_var == var && iter->fndecl == current_function_decl) | |
2766 | break; | |
2767 | ||
2768 | return iter; | |
2769 | } | |
15bf7d19 | 2770 | |
852dd866 EB |
2771 | /* Return the innermost enclosing loop in the current function. */ |
2772 | ||
2773 | static struct loop_info_d * | |
2774 | find_loop (void) | |
2775 | { | |
2776 | struct loop_info_d *iter = NULL; | |
2777 | unsigned int i; | |
2778 | ||
2779 | gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0); | |
90b4c164 | 2780 | |
547bbe49 | 2781 | FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter) |
852dd866 | 2782 | if (iter->fndecl == current_function_decl) |
15bf7d19 EB |
2783 | break; |
2784 | ||
87ab2b04 | 2785 | return iter; |
15bf7d19 EB |
2786 | } |
2787 | ||
d88bbbb9 EB |
2788 | /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is |
2789 | false, or the maximum value if MAX is true, of TYPE. */ | |
2790 | ||
2791 | static bool | |
2792 | can_equal_min_or_max_val_p (tree val, tree type, bool max) | |
2793 | { | |
2794 | tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type)); | |
2795 | ||
2796 | if (TREE_CODE (min_or_max_val) != INTEGER_CST) | |
2797 | return true; | |
2798 | ||
2799 | if (TREE_CODE (val) == NOP_EXPR) | |
2800 | val = (max | |
2801 | ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))) | |
2802 | : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))); | |
2803 | ||
2804 | if (TREE_CODE (val) != INTEGER_CST) | |
2805 | return true; | |
2806 | ||
2efdbf0f EB |
2807 | if (max) |
2808 | return tree_int_cst_lt (val, min_or_max_val) == 0; | |
2809 | else | |
2810 | return tree_int_cst_lt (min_or_max_val, val) == 0; | |
d88bbbb9 EB |
2811 | } |
2812 | ||
2813 | /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE. | |
2814 | If REVERSE is true, minimum value is taken as maximum value. */ | |
2815 | ||
2816 | static inline bool | |
2817 | can_equal_min_val_p (tree val, tree type, bool reverse) | |
2818 | { | |
2819 | return can_equal_min_or_max_val_p (val, type, reverse); | |
2820 | } | |
2821 | ||
2822 | /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE. | |
2823 | If REVERSE is true, maximum value is taken as minimum value. */ | |
2824 | ||
2825 | static inline bool | |
2826 | can_equal_max_val_p (tree val, tree type, bool reverse) | |
2827 | { | |
2828 | return can_equal_min_or_max_val_p (val, type, !reverse); | |
2829 | } | |
2830 | ||
64235766 EB |
2831 | /* Replace EXPR1 and EXPR2 by invariant expressions if possible. Return |
2832 | true if both expressions have been replaced and false otherwise. */ | |
2833 | ||
2834 | static bool | |
2835 | make_invariant (tree *expr1, tree *expr2) | |
2836 | { | |
2837 | tree inv_expr1 = gnat_invariant_expr (*expr1); | |
2838 | tree inv_expr2 = gnat_invariant_expr (*expr2); | |
2839 | ||
2840 | if (inv_expr1) | |
2841 | *expr1 = inv_expr1; | |
2842 | ||
2843 | if (inv_expr2) | |
2844 | *expr2 = inv_expr2; | |
2845 | ||
2846 | return inv_expr1 && inv_expr2; | |
2847 | } | |
2848 | ||
1ddde8dc EB |
2849 | /* Helper function for walk_tree, used by independent_iterations_p below. */ |
2850 | ||
2851 | static tree | |
2852 | scan_rhs_r (tree *tp, int *walk_subtrees, void *data) | |
2853 | { | |
2854 | bitmap *params = (bitmap *)data; | |
2855 | tree t = *tp; | |
2856 | ||
2857 | /* No need to walk into types or decls. */ | |
2858 | if (IS_TYPE_OR_DECL_P (t)) | |
2859 | *walk_subtrees = 0; | |
2860 | ||
2861 | if (TREE_CODE (t) == PARM_DECL && bitmap_bit_p (*params, DECL_UID (t))) | |
2862 | return t; | |
2863 | ||
2864 | return NULL_TREE; | |
2865 | } | |
2866 | ||
2867 | /* Return true if STMT_LIST generates independent iterations in a loop. */ | |
2868 | ||
2869 | static bool | |
2870 | independent_iterations_p (tree stmt_list) | |
2871 | { | |
2872 | tree_stmt_iterator tsi; | |
2873 | bitmap params = BITMAP_GGC_ALLOC(); | |
b9778c45 | 2874 | auto_vec<tree, 16> rhs; |
1ddde8dc EB |
2875 | tree iter; |
2876 | int i; | |
2877 | ||
2878 | if (TREE_CODE (stmt_list) == BIND_EXPR) | |
2879 | stmt_list = BIND_EXPR_BODY (stmt_list); | |
2880 | ||
2881 | /* Scan the list and return false on anything that is not either a check | |
2882 | or an assignment to a parameter with restricted aliasing. */ | |
2883 | for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi)) | |
2884 | { | |
2885 | tree stmt = tsi_stmt (tsi); | |
2886 | ||
2887 | switch (TREE_CODE (stmt)) | |
2888 | { | |
2889 | case COND_EXPR: | |
2890 | { | |
2891 | if (COND_EXPR_ELSE (stmt)) | |
2892 | return false; | |
2893 | if (TREE_CODE (COND_EXPR_THEN (stmt)) != CALL_EXPR) | |
2894 | return false; | |
2895 | tree func = get_callee_fndecl (COND_EXPR_THEN (stmt)); | |
2896 | if (!(func && TREE_THIS_VOLATILE (func))) | |
2897 | return false; | |
2898 | break; | |
2899 | } | |
2900 | ||
2901 | case MODIFY_EXPR: | |
2902 | { | |
2903 | tree lhs = TREE_OPERAND (stmt, 0); | |
2904 | while (handled_component_p (lhs)) | |
2905 | lhs = TREE_OPERAND (lhs, 0); | |
2906 | if (TREE_CODE (lhs) != INDIRECT_REF) | |
2907 | return false; | |
2908 | lhs = TREE_OPERAND (lhs, 0); | |
2909 | if (!(TREE_CODE (lhs) == PARM_DECL | |
2910 | && DECL_RESTRICTED_ALIASING_P (lhs))) | |
2911 | return false; | |
2912 | bitmap_set_bit (params, DECL_UID (lhs)); | |
2913 | rhs.safe_push (TREE_OPERAND (stmt, 1)); | |
2914 | break; | |
2915 | } | |
2916 | ||
2917 | default: | |
2918 | return false; | |
2919 | } | |
2920 | } | |
2921 | ||
2922 | /* At this point we know that the list contains only statements that will | |
2923 | modify parameters with restricted aliasing. Check that the statements | |
2924 | don't at the time read from these parameters. */ | |
2925 | FOR_EACH_VEC_ELT (rhs, i, iter) | |
2926 | if (walk_tree_without_duplicates (&iter, scan_rhs_r, ¶ms)) | |
2927 | return false; | |
2928 | ||
2929 | return true; | |
2930 | } | |
2931 | ||
e08f1aad EB |
2932 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Loop_Statement, to a |
2933 | GCC tree, which is returned. */ | |
7beb2b9d OH |
2934 | |
2935 | static tree | |
852dd866 | 2936 | Loop_Statement_to_gnu (Node_Id gnat_node) |
7beb2b9d | 2937 | { |
7beb2b9d | 2938 | const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node); |
852dd866 EB |
2939 | struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> (); |
2940 | tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE, | |
2941 | NULL_TREE, NULL_TREE, NULL_TREE); | |
2942 | tree gnu_loop_label = create_artificial_label (input_location); | |
2943 | tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE; | |
2944 | tree gnu_result; | |
2945 | ||
2946 | /* Push the loop_info structure associated with the LOOP_STMT. */ | |
2947 | gnu_loop_info->fndecl = current_function_decl; | |
2948 | gnu_loop_info->stmt = gnu_loop_stmt; | |
2949 | vec_safe_push (gnu_loop_stack, gnu_loop_info); | |
2950 | ||
2951 | /* Set location information for statement and end label. */ | |
2952 | set_expr_location_from_node (gnu_loop_stmt, gnat_node); | |
2953 | Sloc_to_locus (Sloc (End_Label (gnat_node)), | |
2954 | &DECL_SOURCE_LOCATION (gnu_loop_label)); | |
2955 | LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label; | |
7beb2b9d | 2956 | |
93101d23 EB |
2957 | /* Set the condition under which the loop must keep going. If we have an |
2958 | explicit condition, use it to set the location information throughout | |
2959 | the translation of the loop statement to avoid having multiple SLOCs. | |
a1ab4c31 | 2960 | |
7fda1596 | 2961 | For the case "LOOP .... END LOOP;" the condition is always true. */ |
a1ab4c31 AC |
2962 | if (No (gnat_iter_scheme)) |
2963 | ; | |
7fda1596 EB |
2964 | |
2965 | /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */ | |
a1ab4c31 | 2966 | else if (Present (Condition (gnat_iter_scheme))) |
93101d23 EB |
2967 | { |
2968 | LOOP_STMT_COND (gnu_loop_stmt) | |
2969 | = gnat_to_gnu (Condition (gnat_iter_scheme)); | |
2970 | ||
2971 | set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme); | |
2972 | } | |
7fda1596 | 2973 | |
58c8f770 EB |
2974 | /* Otherwise we have an iteration scheme and the condition is given by the |
2975 | bounds of the subtype of the iteration variable. */ | |
a1ab4c31 AC |
2976 | else |
2977 | { | |
a1ab4c31 AC |
2978 | Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme); |
2979 | Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec); | |
2980 | Entity_Id gnat_type = Etype (gnat_loop_var); | |
2981 | tree gnu_type = get_unpadded_type (gnat_type); | |
825da0d2 | 2982 | tree gnu_base_type = maybe_character_type (get_base_type (gnu_type)); |
9a1bdc31 | 2983 | tree gnu_one_node = build_int_cst (gnu_base_type, 1); |
6162cec0 | 2984 | tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt; |
d88bbbb9 | 2985 | enum tree_code update_code, test_code, shift_code; |
6162cec0 | 2986 | bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false; |
82d3b03a | 2987 | |
1eb58520 AC |
2988 | gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type)); |
2989 | gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type)); | |
15bf7d19 | 2990 | |
58c8f770 | 2991 | /* We must disable modulo reduction for the iteration variable, if any, |
82d3b03a | 2992 | in order for the loop comparison to be effective. */ |
d88bbbb9 | 2993 | if (reverse) |
82d3b03a EB |
2994 | { |
2995 | gnu_first = gnu_high; | |
2996 | gnu_last = gnu_low; | |
2997 | update_code = MINUS_NOMOD_EXPR; | |
58c8f770 | 2998 | test_code = GE_EXPR; |
d88bbbb9 | 2999 | shift_code = PLUS_NOMOD_EXPR; |
82d3b03a EB |
3000 | } |
3001 | else | |
3002 | { | |
3003 | gnu_first = gnu_low; | |
3004 | gnu_last = gnu_high; | |
3005 | update_code = PLUS_NOMOD_EXPR; | |
58c8f770 | 3006 | test_code = LE_EXPR; |
d88bbbb9 EB |
3007 | shift_code = MINUS_NOMOD_EXPR; |
3008 | } | |
3009 | ||
3010 | /* We use two different strategies to translate the loop, depending on | |
3011 | whether optimization is enabled. | |
3012 | ||
6162cec0 EB |
3013 | If it is, we generate the canonical loop form expected by the loop |
3014 | optimizer and the loop vectorizer, which is the do-while form: | |
d88bbbb9 EB |
3015 | |
3016 | ENTRY_COND | |
3017 | loop: | |
3018 | TOP_UPDATE | |
3019 | BODY | |
3020 | BOTTOM_COND | |
3021 | GOTO loop | |
3022 | ||
6162cec0 EB |
3023 | This avoids an implicit dependency on loop header copying and makes |
3024 | it possible to turn BOTTOM_COND into an inequality test. | |
3025 | ||
3026 | If optimization is disabled, loop header copying doesn't come into | |
3027 | play and we try to generate the loop form with the fewer conditional | |
3028 | branches. First, the default form, which is: | |
d88bbbb9 EB |
3029 | |
3030 | loop: | |
3031 | TOP_COND | |
3032 | BODY | |
3033 | BOTTOM_UPDATE | |
3034 | GOTO loop | |
3035 | ||
6162cec0 EB |
3036 | It should catch most loops with constant ending point. Then, if we |
3037 | cannot, we try to generate the shifted form: | |
d88bbbb9 | 3038 | |
d88bbbb9 | 3039 | loop: |
6162cec0 EB |
3040 | TOP_COND |
3041 | TOP_UPDATE | |
d88bbbb9 | 3042 | BODY |
d88bbbb9 EB |
3043 | GOTO loop |
3044 | ||
6162cec0 EB |
3045 | which should catch loops with constant starting point. Otherwise, if |
3046 | we cannot, we generate the fallback form: | |
d88bbbb9 | 3047 | |
6162cec0 | 3048 | ENTRY_COND |
d88bbbb9 | 3049 | loop: |
d88bbbb9 | 3050 | BODY |
6162cec0 EB |
3051 | BOTTOM_COND |
3052 | BOTTOM_UPDATE | |
d88bbbb9 EB |
3053 | GOTO loop |
3054 | ||
6162cec0 | 3055 | which works in all cases. */ |
d88bbbb9 | 3056 | |
09248547 | 3057 | if (optimize && !optimize_debug) |
d88bbbb9 | 3058 | { |
6162cec0 EB |
3059 | /* We can use the do-while form directly if GNU_FIRST-1 doesn't |
3060 | overflow. */ | |
d88bbbb9 | 3061 | if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)) |
d88bbbb9 EB |
3062 | ; |
3063 | ||
6162cec0 | 3064 | /* Otherwise, use the do-while form with the help of a special |
15bf7d19 | 3065 | induction variable in the unsigned version of the base type |
48d3d75d | 3066 | or the unsigned version of the size type, whichever is the |
15bf7d19 | 3067 | largest, in order to have wrap-around arithmetics for it. */ |
d88bbbb9 | 3068 | else |
6162cec0 | 3069 | { |
48d3d75d EB |
3070 | if (TYPE_PRECISION (gnu_base_type) |
3071 | > TYPE_PRECISION (size_type_node)) | |
00a22e5e EB |
3072 | gnu_base_type |
3073 | = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1); | |
15bf7d19 | 3074 | else |
48d3d75d | 3075 | gnu_base_type = size_type_node; |
15bf7d19 EB |
3076 | |
3077 | gnu_first = convert (gnu_base_type, gnu_first); | |
3078 | gnu_last = convert (gnu_base_type, gnu_last); | |
9a1bdc31 | 3079 | gnu_one_node = build_int_cst (gnu_base_type, 1); |
6162cec0 EB |
3080 | use_iv = true; |
3081 | } | |
3082 | ||
3083 | gnu_first | |
3084 | = build_binary_op (shift_code, gnu_base_type, gnu_first, | |
3085 | gnu_one_node); | |
3086 | LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1; | |
3087 | LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1; | |
d88bbbb9 EB |
3088 | } |
3089 | else | |
3090 | { | |
3091 | /* We can use the default form if GNU_LAST+1 doesn't overflow. */ | |
3092 | if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse)) | |
3093 | ; | |
3094 | ||
3095 | /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor | |
3096 | GNU_LAST-1 does. */ | |
3097 | else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse) | |
3098 | && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse)) | |
3099 | { | |
6162cec0 EB |
3100 | gnu_first |
3101 | = build_binary_op (shift_code, gnu_base_type, gnu_first, | |
3102 | gnu_one_node); | |
3103 | gnu_last | |
3104 | = build_binary_op (shift_code, gnu_base_type, gnu_last, | |
3105 | gnu_one_node); | |
d88bbbb9 EB |
3106 | LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1; |
3107 | } | |
3108 | ||
3109 | /* Otherwise, use the fallback form. */ | |
3110 | else | |
6162cec0 | 3111 | LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1; |
82d3b03a | 3112 | } |
a1ab4c31 | 3113 | |
d88bbbb9 | 3114 | /* If we use the BOTTOM_COND, we can turn the test into an inequality |
ec1b8711 | 3115 | test but we have to add ENTRY_COND to protect the empty loop. */ |
d88bbbb9 | 3116 | if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt)) |
a1ab4c31 | 3117 | { |
d88bbbb9 | 3118 | test_code = NE_EXPR; |
ec1b8711 EB |
3119 | gnu_cond_expr |
3120 | = build3 (COND_EXPR, void_type_node, | |
3121 | build_binary_op (LE_EXPR, boolean_type_node, | |
3122 | gnu_low, gnu_high), | |
3123 | NULL_TREE, alloc_stmt_list ()); | |
3124 | set_expr_location_from_node (gnu_cond_expr, gnat_iter_scheme); | |
a1ab4c31 AC |
3125 | } |
3126 | ||
3127 | /* Open a new nesting level that will surround the loop to declare the | |
58c8f770 | 3128 | iteration variable. */ |
a1ab4c31 AC |
3129 | start_stmt_group (); |
3130 | gnat_pushlevel (); | |
3131 | ||
6162cec0 EB |
3132 | /* If we use the special induction variable, create it and set it to |
3133 | its initial value. Morever, the regular iteration variable cannot | |
3134 | itself be initialized, lest the initial value wrapped around. */ | |
3135 | if (use_iv) | |
3136 | { | |
3137 | gnu_loop_iv | |
3138 | = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var); | |
3139 | add_stmt (gnu_stmt); | |
3140 | gnu_first = NULL_TREE; | |
3141 | } | |
3142 | else | |
3143 | gnu_loop_iv = NULL_TREE; | |
3144 | ||
58c8f770 | 3145 | /* Declare the iteration variable and set it to its initial value. */ |
afc737f0 | 3146 | gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, true); |
a1ab4c31 AC |
3147 | if (DECL_BY_REF_P (gnu_loop_var)) |
3148 | gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var); | |
15bf7d19 EB |
3149 | else if (use_iv) |
3150 | { | |
3151 | gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var)); | |
3152 | SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv); | |
3153 | } | |
3154 | gnu_loop_info->loop_var = gnu_loop_var; | |
87ab2b04 EB |
3155 | gnu_loop_info->low_bound = gnu_low; |
3156 | gnu_loop_info->high_bound = gnu_high; | |
a1ab4c31 | 3157 | |
58c8f770 EB |
3158 | /* Do all the arithmetics in the base type. */ |
3159 | gnu_loop_var = convert (gnu_base_type, gnu_loop_var); | |
a1ab4c31 | 3160 | |
d88bbbb9 | 3161 | /* Set either the top or bottom exit condition. */ |
6162cec0 EB |
3162 | if (use_iv) |
3163 | LOOP_STMT_COND (gnu_loop_stmt) | |
3164 | = build_binary_op (test_code, boolean_type_node, gnu_loop_iv, | |
3165 | gnu_last); | |
3166 | else | |
3167 | LOOP_STMT_COND (gnu_loop_stmt) | |
3168 | = build_binary_op (test_code, boolean_type_node, gnu_loop_var, | |
3169 | gnu_last); | |
a1ab4c31 | 3170 | |
d88bbbb9 EB |
3171 | /* Set either the top or bottom update statement and give it the source |
3172 | location of the iteration for better coverage info. */ | |
6162cec0 EB |
3173 | if (use_iv) |
3174 | { | |
3175 | gnu_stmt | |
3176 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv, | |
3177 | build_binary_op (update_code, gnu_base_type, | |
3178 | gnu_loop_iv, gnu_one_node)); | |
3179 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3180 | append_to_statement_list (gnu_stmt, | |
3181 | &LOOP_STMT_UPDATE (gnu_loop_stmt)); | |
3182 | gnu_stmt | |
3183 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var, | |
3184 | gnu_loop_iv); | |
3185 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3186 | append_to_statement_list (gnu_stmt, | |
3187 | &LOOP_STMT_UPDATE (gnu_loop_stmt)); | |
3188 | } | |
3189 | else | |
3190 | { | |
3191 | gnu_stmt | |
3192 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var, | |
3193 | build_binary_op (update_code, gnu_base_type, | |
3194 | gnu_loop_var, gnu_one_node)); | |
3195 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3196 | LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt; | |
3197 | } | |
93101d23 EB |
3198 | |
3199 | set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme); | |
a1ab4c31 AC |
3200 | } |
3201 | ||
3202 | /* If the loop was named, have the name point to this loop. In this case, | |
58c8f770 | 3203 | the association is not a DECL node, but the end label of the loop. */ |
a1ab4c31 | 3204 | if (Present (Identifier (gnat_node))) |
58c8f770 | 3205 | save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true); |
a1ab4c31 AC |
3206 | |
3207 | /* Make the loop body into its own block, so any allocated storage will be | |
3208 | released every iteration. This is needed for stack allocation. */ | |
3209 | LOOP_STMT_BODY (gnu_loop_stmt) | |
3210 | = build_stmt_group (Statements (gnat_node), true); | |
58c8f770 | 3211 | TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1; |
a1ab4c31 | 3212 | |
6162cec0 EB |
3213 | /* If we have an iteration scheme, then we are in a statement group. Add |
3214 | the LOOP_STMT to it, finish it and make it the "loop". */ | |
3215 | if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme))) | |
a1ab4c31 | 3216 | { |
64235766 EB |
3217 | /* First, if we have computed invariant conditions for range (or index) |
3218 | checks applied to the iteration variable, find out whether they can | |
3219 | be evaluated to false at compile time; otherwise, if there are not | |
3220 | too many of them, combine them with the original checks. If loop | |
3221 | unswitching is enabled, do not require the loop bounds to be also | |
3222 | invariant, as their evaluation will still be ahead of the loop. */ | |
3223 | if (vec_safe_length (gnu_loop_info->checks) > 0 | |
3bdf0b64 | 3224 | && (make_invariant (&gnu_low, &gnu_high) || optimize >= 3)) |
64235766 EB |
3225 | { |
3226 | struct range_check_info_d *rci; | |
3227 | unsigned int i, n_remaining_checks = 0; | |
3228 | ||
3229 | FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci) | |
3230 | { | |
933a7325 EB |
3231 | tree low_ok, high_ok; |
3232 | ||
3233 | if (rci->low_bound) | |
3234 | { | |
3235 | tree gnu_adjusted_low = convert (rci->type, gnu_low); | |
3236 | if (rci->disp) | |
3237 | gnu_adjusted_low | |
3238 | = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR, | |
3239 | rci->type, gnu_adjusted_low, rci->disp); | |
3240 | low_ok | |
3241 | = build_binary_op (GE_EXPR, boolean_type_node, | |
3242 | gnu_adjusted_low, rci->low_bound); | |
3243 | } | |
3244 | else | |
3245 | low_ok = boolean_true_node; | |
3246 | ||
3247 | if (rci->high_bound) | |
3248 | { | |
3249 | tree gnu_adjusted_high = convert (rci->type, gnu_high); | |
3250 | if (rci->disp) | |
3251 | gnu_adjusted_high | |
3252 | = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR, | |
3253 | rci->type, gnu_adjusted_high, rci->disp); | |
3254 | high_ok | |
3255 | = build_binary_op (LE_EXPR, boolean_type_node, | |
3256 | gnu_adjusted_high, rci->high_bound); | |
3257 | } | |
3258 | else | |
3259 | high_ok = boolean_true_node; | |
64235766 EB |
3260 | |
3261 | tree range_ok | |
3262 | = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node, | |
3263 | low_ok, high_ok); | |
3264 | ||
3265 | rci->invariant_cond | |
3266 | = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok); | |
3267 | ||
3268 | if (rci->invariant_cond == boolean_false_node) | |
3269 | TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond; | |
3270 | else | |
3271 | n_remaining_checks++; | |
3272 | } | |
3273 | ||
3274 | /* Note that loop unswitching can only be applied a small number of | |
3275 | times to a given loop (PARAM_MAX_UNSWITCH_LEVEL default to 3). */ | |
278f422c | 3276 | if (IN_RANGE (n_remaining_checks, 1, 3) |
3bdf0b64 | 3277 | && optimize >= 2 |
278f422c | 3278 | && !optimize_size) |
64235766 EB |
3279 | FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci) |
3280 | if (rci->invariant_cond != boolean_false_node) | |
3281 | { | |
3282 | TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond; | |
3283 | ||
3bdf0b64 | 3284 | if (optimize >= 3) |
64235766 EB |
3285 | add_stmt_with_node_force (rci->inserted_cond, gnat_node); |
3286 | } | |
3287 | } | |
15bf7d19 | 3288 | |
852dd866 EB |
3289 | /* Second, if we have recorded invariants to be hoisted, emit them. */ |
3290 | if (vec_safe_length (gnu_loop_info->invariants) > 0) | |
3291 | { | |
3292 | tree *iter; | |
3293 | unsigned int i; | |
3294 | FOR_EACH_VEC_ELT (*gnu_loop_info->invariants, i, iter) | |
3295 | add_stmt_with_node_force (*iter, gnat_node); | |
3296 | } | |
3297 | ||
3298 | /* Third, if loop vectorization is enabled and the iterations of the | |
1ddde8dc | 3299 | loop can easily be proved as independent, mark the loop. */ |
3bdf0b64 | 3300 | if (optimize >= 3 |
1ddde8dc EB |
3301 | && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt))) |
3302 | LOOP_STMT_IVDEP (gnu_loop_stmt) = 1; | |
3303 | ||
a1ab4c31 AC |
3304 | add_stmt (gnu_loop_stmt); |
3305 | gnat_poplevel (); | |
3306 | gnu_loop_stmt = end_stmt_group (); | |
3307 | } | |
3308 | ||
3309 | /* If we have an outer COND_EXPR, that's our result and this loop is its | |
7fda1596 | 3310 | "true" statement. Otherwise, the result is the LOOP_STMT. */ |
a1ab4c31 AC |
3311 | if (gnu_cond_expr) |
3312 | { | |
3313 | COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt; | |
66c14933 | 3314 | TREE_SIDE_EFFECTS (gnu_cond_expr) = 1; |
a1ab4c31 | 3315 | gnu_result = gnu_cond_expr; |
a1ab4c31 AC |
3316 | } |
3317 | else | |
3318 | gnu_result = gnu_loop_stmt; | |
3319 | ||
9771b263 | 3320 | gnu_loop_stack->pop (); |
a1ab4c31 AC |
3321 | |
3322 | return gnu_result; | |
3323 | } | |
ce2d0ce2 | 3324 | |
17a98a3d | 3325 | /* This page implements a form of Named Return Value optimization modeled |
71196d4e EB |
3326 | on the C++ optimization of the same name. The main difference is that |
3327 | we disregard any semantical considerations when applying it here, the | |
3328 | counterpart being that we don't try to apply it to semantically loaded | |
a0b8b1b7 | 3329 | return types, i.e. types with the TYPE_BY_REFERENCE_P flag set. |
71196d4e EB |
3330 | |
3331 | We consider a function body of the following GENERIC form: | |
3332 | ||
3333 | return_type R1; | |
3334 | [...] | |
3335 | RETURN_EXPR [<retval> = ...] | |
3336 | [...] | |
3337 | RETURN_EXPR [<retval> = R1] | |
3338 | [...] | |
3339 | return_type Ri; | |
3340 | [...] | |
3341 | RETURN_EXPR [<retval> = ...] | |
3342 | [...] | |
3343 | RETURN_EXPR [<retval> = Ri] | |
3344 | [...] | |
3345 | ||
13b6b773 EB |
3346 | where the Ri are not addressable and we try to fulfill a simple criterion |
3347 | that would make it possible to replace one or several Ri variables by the | |
3348 | single RESULT_DECL of the function. | |
71196d4e EB |
3349 | |
3350 | The first observation is that RETURN_EXPRs that don't directly reference | |
3351 | any of the Ri variables on the RHS of their assignment are transparent wrt | |
3352 | the optimization. This is because the Ri variables aren't addressable so | |
3353 | any transformation applied to them doesn't affect the RHS; moreover, the | |
3354 | assignment writes the full <retval> object so existing values are entirely | |
3355 | discarded. | |
3356 | ||
3357 | This property can be extended to some forms of RETURN_EXPRs that reference | |
3358 | the Ri variables, for example CONSTRUCTORs, but isn't true in the general | |
3359 | case, in particular when function calls are involved. | |
3360 | ||
3361 | Therefore the algorithm is as follows: | |
3362 | ||
3363 | 1. Collect the list of candidates for a Named Return Value (Ri variables | |
3364 | on the RHS of assignments of RETURN_EXPRs) as well as the list of the | |
3365 | other expressions on the RHS of such assignments. | |
3366 | ||
3367 | 2. Prune the members of the first list (candidates) that are referenced | |
3368 | by a member of the second list (expressions). | |
3369 | ||
3370 | 3. Extract a set of candidates with non-overlapping live ranges from the | |
3371 | first list. These are the Named Return Values. | |
3372 | ||
3373 | 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the | |
088b91c7 EB |
3374 | Named Return Values in the function with the RESULT_DECL. |
3375 | ||
3376 | If the function returns an unconstrained type, things are a bit different | |
3377 | because the anonymous return object is allocated on the secondary stack | |
3378 | and RESULT_DECL is only a pointer to it. Each return object can be of a | |
3379 | different size and is allocated separately so we need not care about the | |
13b6b773 EB |
3380 | addressability and the aforementioned overlapping issues. Therefore, we |
3381 | don't collect the other expressions and skip step #2 in the algorithm. */ | |
71196d4e EB |
3382 | |
3383 | struct nrv_data | |
3384 | { | |
3385 | bitmap nrv; | |
3386 | tree result; | |
088b91c7 | 3387 | Node_Id gnat_ret; |
6e2830c3 | 3388 | hash_set<tree> *visited; |
71196d4e EB |
3389 | }; |
3390 | ||
3391 | /* Return true if T is a Named Return Value. */ | |
3392 | ||
3393 | static inline bool | |
3394 | is_nrv_p (bitmap nrv, tree t) | |
3395 | { | |
04682fe7 | 3396 | return VAR_P (t) && bitmap_bit_p (nrv, DECL_UID (t)); |
71196d4e EB |
3397 | } |
3398 | ||
3399 | /* Helper function for walk_tree, used by finalize_nrv below. */ | |
3400 | ||
3401 | static tree | |
3402 | prune_nrv_r (tree *tp, int *walk_subtrees, void *data) | |
3403 | { | |
3404 | struct nrv_data *dp = (struct nrv_data *)data; | |
3405 | tree t = *tp; | |
3406 | ||
3407 | /* No need to walk into types or decls. */ | |
3408 | if (IS_TYPE_OR_DECL_P (t)) | |
3409 | *walk_subtrees = 0; | |
3410 | ||
3411 | if (is_nrv_p (dp->nrv, t)) | |
3412 | bitmap_clear_bit (dp->nrv, DECL_UID (t)); | |
3413 | ||
3414 | return NULL_TREE; | |
3415 | } | |
3416 | ||
3417 | /* Prune Named Return Values in BLOCK and return true if there is still a | |
3418 | Named Return Value in BLOCK or one of its sub-blocks. */ | |
3419 | ||
3420 | static bool | |
3421 | prune_nrv_in_block (bitmap nrv, tree block) | |
3422 | { | |
3423 | bool has_nrv = false; | |
3424 | tree t; | |
3425 | ||
3426 | /* First recurse on the sub-blocks. */ | |
3427 | for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t)) | |
3428 | has_nrv |= prune_nrv_in_block (nrv, t); | |
3429 | ||
3430 | /* Then make sure to keep at most one NRV per block. */ | |
3431 | for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t)) | |
3432 | if (is_nrv_p (nrv, t)) | |
3433 | { | |
3434 | if (has_nrv) | |
3435 | bitmap_clear_bit (nrv, DECL_UID (t)); | |
3436 | else | |
3437 | has_nrv = true; | |
3438 | } | |
3439 | ||
3440 | return has_nrv; | |
3441 | } | |
3442 | ||
3443 | /* Helper function for walk_tree, used by finalize_nrv below. */ | |
3444 | ||
3445 | static tree | |
3446 | finalize_nrv_r (tree *tp, int *walk_subtrees, void *data) | |
3447 | { | |
3448 | struct nrv_data *dp = (struct nrv_data *)data; | |
3449 | tree t = *tp; | |
3450 | ||
3451 | /* No need to walk into types. */ | |
3452 | if (TYPE_P (t)) | |
3453 | *walk_subtrees = 0; | |
3454 | ||
3455 | /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a | |
3456 | nop, but differs from using NULL_TREE in that it indicates that we care | |
3457 | about the value of the RESULT_DECL. */ | |
3458 | else if (TREE_CODE (t) == RETURN_EXPR | |
d864aeeb | 3459 | && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR) |
71196d4e | 3460 | { |
5d2a63dc | 3461 | tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1); |
71196d4e EB |
3462 | |
3463 | /* Strip useless conversions around the return value. */ | |
3464 | if (gnat_useless_type_conversion (ret_val)) | |
3465 | ret_val = TREE_OPERAND (ret_val, 0); | |
3466 | ||
3467 | if (is_nrv_p (dp->nrv, ret_val)) | |
5d2a63dc | 3468 | TREE_OPERAND (t, 0) = dp->result; |
71196d4e EB |
3469 | } |
3470 | ||
3471 | /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL, | |
3472 | if needed. */ | |
3473 | else if (TREE_CODE (t) == DECL_EXPR | |
3474 | && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t))) | |
3475 | { | |
3476 | tree var = DECL_EXPR_DECL (t), init; | |
3477 | ||
3478 | if (DECL_INITIAL (var)) | |
3479 | { | |
3480 | init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result, | |
3481 | DECL_INITIAL (var)); | |
3482 | SET_EXPR_LOCATION (init, EXPR_LOCATION (t)); | |
3483 | DECL_INITIAL (var) = NULL_TREE; | |
3484 | } | |
3485 | else | |
3486 | init = build_empty_stmt (EXPR_LOCATION (t)); | |
3487 | *tp = init; | |
3488 | ||
3489 | /* Identify the NRV to the RESULT_DECL for debugging purposes. */ | |
3490 | SET_DECL_VALUE_EXPR (var, dp->result); | |
3491 | DECL_HAS_VALUE_EXPR_P (var) = 1; | |
3492 | /* ??? Kludge to avoid an assertion failure during inlining. */ | |
3493 | DECL_SIZE (var) = bitsize_unit_node; | |
3494 | DECL_SIZE_UNIT (var) = size_one_node; | |
3495 | } | |
3496 | ||
3497 | /* And replace all uses of NRVs with the RESULT_DECL. */ | |
3498 | else if (is_nrv_p (dp->nrv, t)) | |
3499 | *tp = convert (TREE_TYPE (t), dp->result); | |
3500 | ||
3501 | /* Avoid walking into the same tree more than once. Unfortunately, we | |
088b91c7 EB |
3502 | can't just use walk_tree_without_duplicates because it would only |
3503 | call us for the first occurrence of NRVs in the function body. */ | |
6e2830c3 | 3504 | if (dp->visited->add (*tp)) |
088b91c7 EB |
3505 | *walk_subtrees = 0; |
3506 | ||
3507 | return NULL_TREE; | |
3508 | } | |
3509 | ||
3510 | /* Likewise, but used when the function returns an unconstrained type. */ | |
3511 | ||
3512 | static tree | |
3513 | finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data) | |
3514 | { | |
3515 | struct nrv_data *dp = (struct nrv_data *)data; | |
3516 | tree t = *tp; | |
3517 | ||
3518 | /* No need to walk into types. */ | |
3519 | if (TYPE_P (t)) | |
3520 | *walk_subtrees = 0; | |
3521 | ||
3522 | /* We need to see the DECL_EXPR of NRVs before any other references so we | |
3523 | walk the body of BIND_EXPR before walking its variables. */ | |
3524 | else if (TREE_CODE (t) == BIND_EXPR) | |
3525 | walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL); | |
3526 | ||
3527 | /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final | |
3528 | return value built by the allocator instead of the whole construct. */ | |
3529 | else if (TREE_CODE (t) == RETURN_EXPR | |
d864aeeb | 3530 | && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR) |
088b91c7 EB |
3531 | { |
3532 | tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1); | |
3533 | ||
3534 | /* This is the construct returned by the allocator. */ | |
3535 | if (TREE_CODE (ret_val) == COMPOUND_EXPR | |
3536 | && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR) | |
3537 | { | |
2117b9bb EB |
3538 | tree rhs = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1); |
3539 | ||
088b91c7 | 3540 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val))) |
2117b9bb | 3541 | ret_val = CONSTRUCTOR_ELT (rhs, 1)->value; |
088b91c7 | 3542 | else |
2117b9bb | 3543 | ret_val = rhs; |
088b91c7 EB |
3544 | } |
3545 | ||
3546 | /* Strip useless conversions around the return value. */ | |
3547 | if (gnat_useless_type_conversion (ret_val) | |
3548 | || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR) | |
3549 | ret_val = TREE_OPERAND (ret_val, 0); | |
3550 | ||
3551 | /* Strip unpadding around the return value. */ | |
3552 | if (TREE_CODE (ret_val) == COMPONENT_REF | |
3553 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0)))) | |
3554 | ret_val = TREE_OPERAND (ret_val, 0); | |
3555 | ||
3556 | /* Assign the new return value to the RESULT_DECL. */ | |
3557 | if (is_nrv_p (dp->nrv, ret_val)) | |
3558 | TREE_OPERAND (TREE_OPERAND (t, 0), 1) | |
3559 | = TREE_OPERAND (DECL_INITIAL (ret_val), 0); | |
3560 | } | |
3561 | ||
3562 | /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result | |
3563 | into a new variable. */ | |
3564 | else if (TREE_CODE (t) == DECL_EXPR | |
3565 | && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t))) | |
3566 | { | |
3567 | tree saved_current_function_decl = current_function_decl; | |
3568 | tree var = DECL_EXPR_DECL (t); | |
3569 | tree alloc, p_array, new_var, new_ret; | |
9771b263 DN |
3570 | vec<constructor_elt, va_gc> *v; |
3571 | vec_alloc (v, 2); | |
088b91c7 EB |
3572 | |
3573 | /* Create an artificial context to build the allocation. */ | |
3574 | current_function_decl = decl_function_context (var); | |
3575 | start_stmt_group (); | |
3576 | gnat_pushlevel (); | |
3577 | ||
3578 | /* This will return a COMPOUND_EXPR with the allocation in the first | |
3579 | arm and the final return value in the second arm. */ | |
3580 | alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var), | |
3581 | TREE_TYPE (dp->result), | |
3582 | Procedure_To_Call (dp->gnat_ret), | |
3583 | Storage_Pool (dp->gnat_ret), | |
3584 | Empty, false); | |
3585 | ||
3586 | /* The new variable is built as a reference to the allocated space. */ | |
3587 | new_var | |
3588 | = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var), | |
3589 | build_reference_type (TREE_TYPE (var))); | |
3590 | DECL_BY_REFERENCE (new_var) = 1; | |
3591 | ||
3592 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc))) | |
3593 | { | |
2117b9bb EB |
3594 | tree cst = TREE_OPERAND (alloc, 1); |
3595 | ||
088b91c7 EB |
3596 | /* The new initial value is a COMPOUND_EXPR with the allocation in |
3597 | the first arm and the value of P_ARRAY in the second arm. */ | |
3598 | DECL_INITIAL (new_var) | |
3599 | = build2 (COMPOUND_EXPR, TREE_TYPE (new_var), | |
3600 | TREE_OPERAND (alloc, 0), | |
2117b9bb | 3601 | CONSTRUCTOR_ELT (cst, 0)->value); |
088b91c7 EB |
3602 | |
3603 | /* Build a modified CONSTRUCTOR that references NEW_VAR. */ | |
3604 | p_array = TYPE_FIELDS (TREE_TYPE (alloc)); | |
3605 | CONSTRUCTOR_APPEND_ELT (v, p_array, | |
3606 | fold_convert (TREE_TYPE (p_array), new_var)); | |
3607 | CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array), | |
2117b9bb | 3608 | CONSTRUCTOR_ELT (cst, 1)->value); |
088b91c7 EB |
3609 | new_ret = build_constructor (TREE_TYPE (alloc), v); |
3610 | } | |
3611 | else | |
3612 | { | |
3613 | /* The new initial value is just the allocation. */ | |
3614 | DECL_INITIAL (new_var) = alloc; | |
3615 | new_ret = fold_convert (TREE_TYPE (alloc), new_var); | |
3616 | } | |
3617 | ||
3618 | gnat_pushdecl (new_var, Empty); | |
3619 | ||
3620 | /* Destroy the artificial context and insert the new statements. */ | |
3621 | gnat_zaplevel (); | |
3622 | *tp = end_stmt_group (); | |
3623 | current_function_decl = saved_current_function_decl; | |
3624 | ||
3625 | /* Chain NEW_VAR immediately after VAR and ignore the latter. */ | |
3626 | DECL_CHAIN (new_var) = DECL_CHAIN (var); | |
3627 | DECL_CHAIN (var) = new_var; | |
3628 | DECL_IGNORED_P (var) = 1; | |
3629 | ||
3630 | /* Save the new return value and the dereference of NEW_VAR. */ | |
3631 | DECL_INITIAL (var) | |
3632 | = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret, | |
3633 | build1 (INDIRECT_REF, TREE_TYPE (var), new_var)); | |
3634 | /* ??? Kludge to avoid messing up during inlining. */ | |
3635 | DECL_CONTEXT (var) = NULL_TREE; | |
3636 | } | |
3637 | ||
3638 | /* And replace all uses of NRVs with the dereference of NEW_VAR. */ | |
3639 | else if (is_nrv_p (dp->nrv, t)) | |
3640 | *tp = TREE_OPERAND (DECL_INITIAL (t), 1); | |
3641 | ||
3642 | /* Avoid walking into the same tree more than once. Unfortunately, we | |
3643 | can't just use walk_tree_without_duplicates because it would only | |
3644 | call us for the first occurrence of NRVs in the function body. */ | |
6e2830c3 | 3645 | if (dp->visited->add (*tp)) |
71196d4e EB |
3646 | *walk_subtrees = 0; |
3647 | ||
3648 | return NULL_TREE; | |
3649 | } | |
3650 | ||
81edaf2f EB |
3651 | /* Apply FUNC to all the sub-trees of nested functions in NODE. FUNC is called |
3652 | with the DATA and the address of each sub-tree. If FUNC returns a non-NULL | |
3653 | value, the traversal is stopped. */ | |
3654 | ||
3655 | static void | |
3656 | walk_nesting_tree (struct cgraph_node *node, walk_tree_fn func, void *data) | |
3657 | { | |
89576d86 JH |
3658 | for (node = first_nested_function (node); |
3659 | node; node = next_nested_function (node)) | |
81edaf2f EB |
3660 | { |
3661 | walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), func, data); | |
3662 | walk_nesting_tree (node, func, data); | |
3663 | } | |
3664 | } | |
3665 | ||
71196d4e EB |
3666 | /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap |
3667 | contains the candidates for Named Return Value and OTHER is a list of | |
088b91c7 | 3668 | the other return values. GNAT_RET is a representative return node. */ |
71196d4e EB |
3669 | |
3670 | static void | |
9771b263 | 3671 | finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret) |
71196d4e | 3672 | { |
71196d4e | 3673 | struct nrv_data data; |
088b91c7 | 3674 | walk_tree_fn func; |
71196d4e EB |
3675 | unsigned int i; |
3676 | tree iter; | |
3677 | ||
3678 | /* We shouldn't be applying the optimization to return types that we aren't | |
3679 | allowed to manipulate freely. */ | |
a0b8b1b7 | 3680 | gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl)))); |
71196d4e EB |
3681 | |
3682 | /* Prune the candidates that are referenced by other return values. */ | |
3683 | data.nrv = nrv; | |
3684 | data.result = NULL_TREE; | |
547bbe49 | 3685 | data.gnat_ret = Empty; |
71196d4e | 3686 | data.visited = NULL; |
547bbe49 | 3687 | FOR_EACH_VEC_SAFE_ELT (other, i, iter) |
71196d4e EB |
3688 | walk_tree_without_duplicates (&iter, prune_nrv_r, &data); |
3689 | if (bitmap_empty_p (nrv)) | |
3690 | return; | |
3691 | ||
3692 | /* Prune also the candidates that are referenced by nested functions. */ | |
81edaf2f | 3693 | walk_nesting_tree (cgraph_node::get_create (fndecl), prune_nrv_r, &data); |
71196d4e EB |
3694 | if (bitmap_empty_p (nrv)) |
3695 | return; | |
3696 | ||
3697 | /* Extract a set of NRVs with non-overlapping live ranges. */ | |
3698 | if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl))) | |
3699 | return; | |
3700 | ||
3701 | /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */ | |
3702 | data.nrv = nrv; | |
3703 | data.result = DECL_RESULT (fndecl); | |
088b91c7 | 3704 | data.gnat_ret = gnat_ret; |
6e2830c3 | 3705 | data.visited = new hash_set<tree>; |
89e037d0 | 3706 | if (TYPE_RETURN_BY_DIRECT_REF_P (TREE_TYPE (fndecl))) |
088b91c7 EB |
3707 | func = finalize_nrv_unc_r; |
3708 | else | |
3709 | func = finalize_nrv_r; | |
3710 | walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL); | |
6e2830c3 | 3711 | delete data.visited; |
71196d4e EB |
3712 | } |
3713 | ||
3714 | /* Return true if RET_VAL can be used as a Named Return Value for the | |
3715 | anonymous return object RET_OBJ. */ | |
3716 | ||
3717 | static bool | |
3718 | return_value_ok_for_nrv_p (tree ret_obj, tree ret_val) | |
3719 | { | |
3720 | if (TREE_CODE (ret_val) != VAR_DECL) | |
3721 | return false; | |
3722 | ||
3723 | if (TREE_THIS_VOLATILE (ret_val)) | |
3724 | return false; | |
3725 | ||
3726 | if (DECL_CONTEXT (ret_val) != current_function_decl) | |
3727 | return false; | |
3728 | ||
3729 | if (TREE_STATIC (ret_val)) | |
3730 | return false; | |
3731 | ||
13b6b773 EB |
3732 | /* For the constrained case, test for addressability. */ |
3733 | if (ret_obj && TREE_ADDRESSABLE (ret_val)) | |
71196d4e EB |
3734 | return false; |
3735 | ||
0b9cdb9a | 3736 | /* For the constrained case, test for overalignment. */ |
088b91c7 | 3737 | if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj)) |
71196d4e EB |
3738 | return false; |
3739 | ||
0b9cdb9a EB |
3740 | /* For the unconstrained case, test for bogus initialization. */ |
3741 | if (!ret_obj | |
3742 | && DECL_INITIAL (ret_val) | |
3743 | && TREE_CODE (DECL_INITIAL (ret_val)) == NULL_EXPR) | |
3744 | return false; | |
3745 | ||
71196d4e EB |
3746 | return true; |
3747 | } | |
3748 | ||
3749 | /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around | |
3750 | the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR | |
3751 | around RESULT_OBJ, which may be null in this case. */ | |
f3d34576 EB |
3752 | |
3753 | static tree | |
3754 | build_return_expr (tree ret_obj, tree ret_val) | |
3755 | { | |
3756 | tree result_expr; | |
3757 | ||
3758 | if (ret_val) | |
3759 | { | |
3760 | /* The gimplifier explicitly enforces the following invariant: | |
3761 | ||
3762 | RETURN_EXPR | |
3763 | | | |
d864aeeb | 3764 | INIT_EXPR |
f3d34576 EB |
3765 | / \ |
3766 | / \ | |
3767 | RET_OBJ ... | |
3768 | ||
3769 | As a consequence, type consistency dictates that we use the type | |
3770 | of the RET_OBJ as the operation type. */ | |
3771 | tree operation_type = TREE_TYPE (ret_obj); | |
3772 | ||
d864aeeb EB |
3773 | /* Convert the right operand to the operation type. Note that this is |
3774 | the transformation applied in the INIT_EXPR case of build_binary_op, | |
f3d34576 EB |
3775 | with the assumption that the type cannot involve a placeholder. */ |
3776 | if (operation_type != TREE_TYPE (ret_val)) | |
3777 | ret_val = convert (operation_type, ret_val); | |
3778 | ||
d864aeeb EB |
3779 | /* We always can use an INIT_EXPR for the return object. */ |
3780 | result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val); | |
71196d4e EB |
3781 | |
3782 | /* If the function returns an aggregate type, find out whether this is | |
3783 | a candidate for Named Return Value. If so, record it. Otherwise, | |
3784 | if this is an expression of some kind, record it elsewhere. */ | |
3785 | if (optimize | |
09248547 | 3786 | && !optimize_debug |
71196d4e EB |
3787 | && AGGREGATE_TYPE_P (operation_type) |
3788 | && !TYPE_IS_FAT_POINTER_P (operation_type) | |
f563ce55 | 3789 | && TYPE_MODE (operation_type) == BLKmode |
71196d4e EB |
3790 | && aggregate_value_p (operation_type, current_function_decl)) |
3791 | { | |
71196d4e EB |
3792 | /* Strip useless conversions around the return value. */ |
3793 | if (gnat_useless_type_conversion (ret_val)) | |
3794 | ret_val = TREE_OPERAND (ret_val, 0); | |
3795 | ||
3796 | /* Now apply the test to the return value. */ | |
3797 | if (return_value_ok_for_nrv_p (ret_obj, ret_val)) | |
3798 | { | |
3799 | if (!f_named_ret_val) | |
3800 | f_named_ret_val = BITMAP_GGC_ALLOC (); | |
3801 | bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val)); | |
3802 | } | |
3803 | ||
3804 | /* Note that we need not care about CONSTRUCTORs here, as they are | |
3805 | totally transparent given the read-compose-write semantics of | |
3806 | assignments from CONSTRUCTORs. */ | |
3807 | else if (EXPR_P (ret_val)) | |
9771b263 | 3808 | vec_safe_push (f_other_ret_val, ret_val); |
71196d4e | 3809 | } |
f3d34576 EB |
3810 | } |
3811 | else | |
3812 | result_expr = ret_obj; | |
3813 | ||
3814 | return build1 (RETURN_EXPR, void_type_node, result_expr); | |
3815 | } | |
ce2d0ce2 | 3816 | |
6d16658d BD |
3817 | /* Subroutine of gnat_to_gnu to translate the At_End_Proc of GNAT_NODE, an |
3818 | N_Block_Statement or N_Handled_Sequence_Of_Statements or N_*_Body node. | |
3819 | ||
3820 | To invoked the GCC mechanism, we call add_cleanup and when we leave the | |
3821 | group, end_stmt_group will create the TRY_FINALLY_EXPR construct. */ | |
3822 | ||
3823 | static void | |
3824 | At_End_Proc_to_gnu (Node_Id gnat_node) | |
3825 | { | |
3826 | tree proc_decl = gnat_to_gnu (At_End_Proc (gnat_node)); | |
0b66f882 | 3827 | Node_Id gnat_end_label; |
6d16658d BD |
3828 | |
3829 | /* When not optimizing, disable inlining of finalizers as this can | |
3830 | create a more complex CFG in the parent function. */ | |
3831 | if (!optimize || optimize_debug) | |
3832 | DECL_DECLARED_INLINE_P (proc_decl) = 0; | |
3833 | ||
0b66f882 EB |
3834 | /* Retrieve the end label attached to the node, if any. */ |
3835 | if (Nkind (gnat_node) == N_Handled_Sequence_Of_Statements) | |
3836 | gnat_end_label = End_Label (gnat_node); | |
3837 | else if (Present (Handled_Statement_Sequence (gnat_node))) | |
3838 | gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node)); | |
3839 | else | |
3840 | gnat_end_label = Empty; | |
3841 | ||
6d16658d BD |
3842 | /* If there is no end label attached, we use the location of the At_End |
3843 | procedure because Expand_Cleanup_Actions might reset the location of | |
0b66f882 | 3844 | the enclosing construct to that of an inner statement. */ |
6d16658d | 3845 | add_cleanup (build_call_n_expr (proc_decl, 0), |
0b66f882 EB |
3846 | Present (gnat_end_label) |
3847 | ? gnat_end_label : At_End_Proc (gnat_node)); | |
6d16658d BD |
3848 | } |
3849 | ||
e08f1aad | 3850 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Subprogram_Body. */ |
a1ab4c31 AC |
3851 | |
3852 | static void | |
3853 | Subprogram_Body_to_gnu (Node_Id gnat_node) | |
3854 | { | |
a1ab4c31 AC |
3855 | /* The defining identifier for the subprogram body. Note that if a |
3856 | specification has appeared before for this body, then the identifier | |
e08f1aad EB |
3857 | occurring in that specification will also be a defining identifier |
3858 | and calls to this subprogram will point to that specification. */ | |
4e279bd9 | 3859 | Entity_Id gnat_subprog |
a1ab4c31 AC |
3860 | = (Present (Corresponding_Spec (gnat_node)) |
3861 | ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node)); | |
e08f1aad | 3862 | /* The FUNCTION_DECL node corresponding to the defining identifier. */ |
4e279bd9 | 3863 | tree gnu_subprog; |
d47d0a8d EB |
3864 | /* Its RESULT_DECL node. */ |
3865 | tree gnu_result_decl; | |
35a382b8 | 3866 | /* Its FUNCTION_TYPE node. */ |
a1ab4c31 | 3867 | tree gnu_subprog_type; |
35a382b8 | 3868 | /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */ |
a1ab4c31 | 3869 | tree gnu_cico_list; |
35a382b8 | 3870 | /* The entry in the CI_CO_LIST that represents a function return, if any. */ |
e08f1aad EB |
3871 | tree gnu_return_var_elmt; |
3872 | /* Its source location. */ | |
40e63ada | 3873 | location_t locus; |
a1ab4c31 | 3874 | |
4e279bd9 EB |
3875 | /* If this is a generic subprogram or it has been eliminated, ignore it. */ |
3876 | if (Is_Generic_Subprogram (gnat_subprog) || Is_Eliminated (gnat_subprog)) | |
a1ab4c31 AC |
3877 | return; |
3878 | ||
3879 | /* If this subprogram acts as its own spec, define it. Otherwise, just get | |
3880 | the already-elaborated tree node. However, if this subprogram had its | |
3881 | elaboration deferred, we will already have made a tree node for it. So | |
3882 | treat it as not being defined in that case. Such a subprogram cannot | |
3883 | have an address clause or a freeze node, so this test is safe, though it | |
3884 | does disable some otherwise-useful error checking. */ | |
4e279bd9 EB |
3885 | gnu_subprog |
3886 | = gnat_to_gnu_entity (gnat_subprog, NULL_TREE, | |
a1ab4c31 | 3887 | Acts_As_Spec (gnat_node) |
4e279bd9 EB |
3888 | && !present_gnu_tree (gnat_subprog)); |
3889 | DECL_FUNCTION_IS_DEF (gnu_subprog) = true; | |
3890 | gnu_result_decl = DECL_RESULT (gnu_subprog); | |
3891 | gnu_subprog_type = TREE_TYPE (gnu_subprog); | |
35a382b8 | 3892 | gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type); |
2374257a EB |
3893 | if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node) |
3894 | gnu_return_var_elmt = gnu_cico_list; | |
e08f1aad EB |
3895 | else |
3896 | gnu_return_var_elmt = NULL_TREE; | |
a1ab4c31 | 3897 | |
d47d0a8d | 3898 | /* If the function returns by invisible reference, make it explicit in the |
e08f1aad | 3899 | function body. See gnat_to_gnu_subprog_type for more details. */ |
2374257a | 3900 | if (TREE_ADDRESSABLE (gnu_subprog_type)) |
d47d0a8d EB |
3901 | { |
3902 | TREE_TYPE (gnu_result_decl) | |
3903 | = build_reference_type (TREE_TYPE (gnu_result_decl)); | |
3904 | relayout_decl (gnu_result_decl); | |
3905 | } | |
3906 | ||
40e63ada | 3907 | /* Set the line number in the decl to correspond to that of the body. */ |
4e279bd9 | 3908 | if (DECL_IGNORED_P (gnu_subprog)) |
a45918f8 | 3909 | locus = UNKNOWN_LOCATION; |
4e279bd9 | 3910 | else if (!Sloc_to_locus (Sloc (gnat_node), &locus, false, gnu_subprog)) |
0c8af82a | 3911 | locus = input_location; |
4e279bd9 | 3912 | DECL_SOURCE_LOCATION (gnu_subprog) = locus; |
a1ab4c31 | 3913 | |
44662f68 | 3914 | /* Try to create a bona-fide thunk and hand it over to the middle-end. */ |
4e279bd9 | 3915 | if (Is_Thunk (gnat_subprog) |
89e037d0 | 3916 | && !Is_Secondary_Stack_Thunk (gnat_subprog) |
4e279bd9 | 3917 | && maybe_make_gnu_thunk (gnat_subprog, gnu_subprog)) |
44662f68 EB |
3918 | return; |
3919 | ||
a1ab4c31 | 3920 | /* Initialize the information structure for the function. */ |
4e279bd9 | 3921 | allocate_struct_function (gnu_subprog, false); |
e08f1aad EB |
3922 | language_function *gnu_subprog_lang = ggc_cleared_alloc<language_function> (); |
3923 | DECL_STRUCT_FUNCTION (gnu_subprog)->language = gnu_subprog_lang; | |
4e279bd9 | 3924 | DECL_STRUCT_FUNCTION (gnu_subprog)->function_start_locus = locus; |
58c8f770 | 3925 | set_cfun (NULL); |
a1ab4c31 | 3926 | |
4e279bd9 | 3927 | begin_subprog_body (gnu_subprog); |
a1ab4c31 | 3928 | |
2374257a EB |
3929 | /* If there are copy-in/copy-out parameters, we need to ensure that they are |
3930 | properly copied out by the return statement. We do this by making a new | |
3931 | block and converting any return into a goto to a label at the end of the | |
3932 | block. */ | |
a963da4d EB |
3933 | if (gnu_cico_list) |
3934 | { | |
e08f1aad | 3935 | tree gnu_return_var; |
35a382b8 | 3936 | |
b16b6cc9 | 3937 | vec_safe_push (gnu_return_label_stack, |
a963da4d EB |
3938 | create_artificial_label (input_location)); |
3939 | ||
3940 | start_stmt_group (); | |
3941 | gnat_pushlevel (); | |
3942 | ||
2374257a EB |
3943 | /* If this is a function with copy-in/copy-out parameters and which does |
3944 | not return by invisible reference, we also need a variable for the | |
3945 | return value to be placed. */ | |
3946 | if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type)) | |
35a382b8 EB |
3947 | { |
3948 | tree gnu_return_type | |
3949 | = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt)); | |
3950 | ||
35a382b8 EB |
3951 | gnu_return_var |
3952 | = create_var_decl (get_identifier ("RETVAL"), NULL_TREE, | |
2056c5ed EB |
3953 | gnu_return_type, NULL_TREE, |
3954 | false, false, false, false, false, | |
4e279bd9 | 3955 | true, false, NULL, gnat_subprog); |
35a382b8 EB |
3956 | TREE_VALUE (gnu_return_var_elmt) = gnu_return_var; |
3957 | } | |
e08f1aad EB |
3958 | else |
3959 | gnu_return_var = NULL_TREE; | |
35a382b8 | 3960 | |
9771b263 | 3961 | vec_safe_push (gnu_return_var_stack, gnu_return_var); |
35a382b8 | 3962 | |
a963da4d EB |
3963 | /* See whether there are parameters for which we don't have a GCC tree |
3964 | yet. These must be Out parameters. Make a VAR_DECL for them and | |
3965 | put it into TYPE_CI_CO_LIST, which must contain an empty entry too. | |
3966 | We can match up the entries because TYPE_CI_CO_LIST is in the order | |
3967 | of the parameters. */ | |
e08f1aad | 3968 | for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog); |
a963da4d EB |
3969 | Present (gnat_param); |
3970 | gnat_param = Next_Formal_With_Extras (gnat_param)) | |
3971 | if (!present_gnu_tree (gnat_param)) | |
3972 | { | |
3973 | tree gnu_cico_entry = gnu_cico_list; | |
54625ca1 | 3974 | tree gnu_decl; |
a963da4d EB |
3975 | |
3976 | /* Skip any entries that have been already filled in; they must | |
3977 | correspond to In Out parameters. */ | |
3978 | while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry)) | |
3979 | gnu_cico_entry = TREE_CHAIN (gnu_cico_entry); | |
3980 | ||
54625ca1 | 3981 | /* Do any needed dereferences for by-ref objects. */ |
afc737f0 | 3982 | gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, true); |
54625ca1 EB |
3983 | gcc_assert (DECL_P (gnu_decl)); |
3984 | if (DECL_BY_REF_P (gnu_decl)) | |
3985 | gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl); | |
3986 | ||
a963da4d EB |
3987 | /* Do any needed references for padded types. */ |
3988 | TREE_VALUE (gnu_cico_entry) | |
54625ca1 | 3989 | = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl); |
a963da4d EB |
3990 | } |
3991 | } | |
3992 | else | |
9771b263 | 3993 | vec_safe_push (gnu_return_label_stack, NULL_TREE); |
a1ab4c31 AC |
3994 | |
3995 | /* Get a tree corresponding to the code for the subprogram. */ | |
3996 | start_stmt_group (); | |
3997 | gnat_pushlevel (); | |
3998 | ||
e08f1aad | 3999 | /* First translate the declarations of the subprogram. */ |
6d16658d | 4000 | process_decls (Declarations (gnat_node), Empty, true, true); |
a1ab4c31 | 4001 | |
e08f1aad EB |
4002 | /* Then generate the code of the subprogram itself. A return statement will |
4003 | be present and any Out parameters will be handled there. */ | |
a1ab4c31 | 4004 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); |
e08f1aad | 4005 | |
6d16658d BD |
4006 | /* Process the At_End_Proc, if any. */ |
4007 | if (Present (At_End_Proc (gnat_node))) | |
4008 | At_End_Proc_to_gnu (gnat_node); | |
4009 | ||
a1ab4c31 | 4010 | gnat_poplevel (); |
e08f1aad | 4011 | tree gnu_result = end_stmt_group (); |
a1ab4c31 | 4012 | |
cc997e0c EB |
4013 | /* Attempt setting the end_locus of our GCC body tree, typically a BIND_EXPR, |
4014 | then the end_locus of our GCC subprogram declaration tree. */ | |
4015 | set_end_locus_from_node (gnu_result, gnat_node); | |
4e279bd9 | 4016 | set_end_locus_from_node (gnu_subprog, gnat_node); |
cc997e0c | 4017 | |
0394741f EB |
4018 | /* If we populated the parameter attributes cache, we need to make sure that |
4019 | the cached expressions are evaluated on all the possible paths leading to | |
4020 | their uses. So we force their evaluation on entry of the function. */ | |
e08f1aad | 4021 | vec<parm_attr, va_gc> *cache = gnu_subprog_lang->parm_attr_cache; |
0394741f EB |
4022 | if (cache) |
4023 | { | |
4024 | struct parm_attr_d *pa; | |
4025 | int i; | |
4026 | ||
4027 | start_stmt_group (); | |
4028 | ||
9771b263 | 4029 | FOR_EACH_VEC_ELT (*cache, i, pa) |
0394741f EB |
4030 | { |
4031 | if (pa->first) | |
4032 | add_stmt_with_node_force (pa->first, gnat_node); | |
4033 | if (pa->last) | |
4034 | add_stmt_with_node_force (pa->last, gnat_node); | |
4035 | if (pa->length) | |
4036 | add_stmt_with_node_force (pa->length, gnat_node); | |
4037 | } | |
4038 | ||
4039 | add_stmt (gnu_result); | |
4040 | gnu_result = end_stmt_group (); | |
f3d34576 | 4041 | |
e08f1aad | 4042 | gnu_subprog_lang->parm_attr_cache = NULL; |
0394741f EB |
4043 | } |
4044 | ||
a963da4d EB |
4045 | /* If we are dealing with a return from an Ada procedure with parameters |
4046 | passed by copy-in/copy-out, we need to return a record containing the | |
4047 | final values of these parameters. If the list contains only one entry, | |
4048 | return just that entry though. | |
4049 | ||
4050 | For a full description of the copy-in/copy-out parameter mechanism, see | |
4051 | the part of the gnat_to_gnu_entity routine dealing with the translation | |
4052 | of subprograms. | |
4053 | ||
4054 | We need to make a block that contains the definition of that label and | |
4055 | the copying of the return value. It first contains the function, then | |
4056 | the label and copy statement. */ | |
4057 | if (gnu_cico_list) | |
4058 | { | |
2374257a EB |
4059 | const Node_Id gnat_end_label |
4060 | = End_Label (Handled_Statement_Sequence (gnat_node)); | |
a963da4d | 4061 | |
df2abf54 EB |
4062 | gnu_return_var_stack->pop (); |
4063 | ||
a963da4d EB |
4064 | add_stmt (gnu_result); |
4065 | add_stmt (build1 (LABEL_EXPR, void_type_node, | |
9771b263 | 4066 | gnu_return_label_stack->last ())); |
a963da4d | 4067 | |
2374257a EB |
4068 | /* If this is a function which returns by invisible reference, the |
4069 | return value has already been dealt with at the return statements, | |
4070 | so we only need to indirectly copy out the parameters. */ | |
4071 | if (TREE_ADDRESSABLE (gnu_subprog_type)) | |
4072 | { | |
4073 | tree gnu_ret_deref | |
4074 | = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl); | |
4075 | tree t; | |
4076 | ||
4077 | gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node); | |
4078 | ||
4079 | for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t)) | |
4080 | { | |
4081 | tree gnu_field_deref | |
64235766 | 4082 | = build_component_ref (gnu_ret_deref, TREE_PURPOSE (t), true); |
2374257a EB |
4083 | gnu_result = build2 (MODIFY_EXPR, void_type_node, |
4084 | gnu_field_deref, TREE_VALUE (t)); | |
4085 | add_stmt_with_node (gnu_result, gnat_end_label); | |
4086 | } | |
4087 | } | |
4088 | ||
4089 | /* Otherwise, if this is a procedure or a function which does not return | |
4090 | by invisible reference, we can do a direct block-copy out. */ | |
a963da4d | 4091 | else |
2374257a EB |
4092 | { |
4093 | tree gnu_retval; | |
4094 | ||
4095 | if (list_length (gnu_cico_list) == 1) | |
4096 | gnu_retval = TREE_VALUE (gnu_cico_list); | |
4097 | else | |
4098 | gnu_retval | |
4099 | = build_constructor_from_list (TREE_TYPE (gnu_subprog_type), | |
4100 | gnu_cico_list); | |
4101 | ||
4102 | gnu_result = build_return_expr (gnu_result_decl, gnu_retval); | |
4103 | add_stmt_with_node (gnu_result, gnat_end_label); | |
4104 | } | |
a963da4d | 4105 | |
a963da4d EB |
4106 | gnat_poplevel (); |
4107 | gnu_result = end_stmt_group (); | |
4108 | } | |
4109 | ||
9771b263 | 4110 | gnu_return_label_stack->pop (); |
a963da4d | 4111 | |
48a24fcf TG |
4112 | /* On SEH targets, install an exception handler around the main entry |
4113 | point to catch unhandled exceptions. */ | |
4e279bd9 | 4114 | if (DECL_NAME (gnu_subprog) == main_identifier_node |
48a24fcf TG |
4115 | && targetm_common.except_unwind_info (&global_options) == UI_SEH) |
4116 | { | |
4117 | tree t; | |
4118 | tree etype; | |
4119 | ||
4120 | t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), | |
4121 | 1, integer_zero_node); | |
4122 | t = build_call_n_expr (unhandled_except_decl, 1, t); | |
4123 | ||
4124 | etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl); | |
4125 | etype = tree_cons (NULL_TREE, etype, NULL_TREE); | |
4126 | ||
4127 | t = build2 (CATCH_EXPR, void_type_node, etype, t); | |
4128 | gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result), | |
4129 | gnu_result, t); | |
4130 | } | |
4131 | ||
f3d34576 EB |
4132 | end_subprog_body (gnu_result); |
4133 | ||
f4cd2542 EB |
4134 | /* Finally annotate the parameters and disconnect the trees for parameters |
4135 | that we have turned into variables since they are now unusable. */ | |
e08f1aad | 4136 | for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog); |
a1ab4c31 AC |
4137 | Present (gnat_param); |
4138 | gnat_param = Next_Formal_With_Extras (gnat_param)) | |
f4cd2542 EB |
4139 | { |
4140 | tree gnu_param = get_gnu_tree (gnat_param); | |
04682fe7 | 4141 | bool is_var_decl = VAR_P (gnu_param); |
0c700259 | 4142 | |
f4cd2542 | 4143 | annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE, |
491f54a7 | 4144 | DECL_BY_REF_P (gnu_param)); |
0c700259 EB |
4145 | |
4146 | if (is_var_decl) | |
f4cd2542 EB |
4147 | save_gnu_tree (gnat_param, NULL_TREE, false); |
4148 | } | |
a1ab4c31 | 4149 | |
0d24bf76 | 4150 | /* Disconnect the variable created for the return value. */ |
35a382b8 EB |
4151 | if (gnu_return_var_elmt) |
4152 | TREE_VALUE (gnu_return_var_elmt) = void_type_node; | |
4153 | ||
71196d4e EB |
4154 | /* If the function returns an aggregate type and we have candidates for |
4155 | a Named Return Value, finalize the optimization. */ | |
e08f1aad | 4156 | if (optimize && !optimize_debug && gnu_subprog_lang->named_ret_val) |
71196d4e | 4157 | { |
4e279bd9 | 4158 | finalize_nrv (gnu_subprog, |
e08f1aad EB |
4159 | gnu_subprog_lang->named_ret_val, |
4160 | gnu_subprog_lang->other_ret_val, | |
4161 | gnu_subprog_lang->gnat_ret); | |
4162 | gnu_subprog_lang->named_ret_val = NULL; | |
4163 | gnu_subprog_lang->other_ret_val = NULL; | |
71196d4e EB |
4164 | } |
4165 | ||
87411e95 EB |
4166 | /* If this is an inlined external function that has been marked uninlinable, |
4167 | drop the body and stop there. Otherwise compile the body. */ | |
4e279bd9 EB |
4168 | if (DECL_EXTERNAL (gnu_subprog) && DECL_UNINLINABLE (gnu_subprog)) |
4169 | DECL_SAVED_TREE (gnu_subprog) = NULL_TREE; | |
87411e95 | 4170 | else |
4e279bd9 | 4171 | rest_of_subprog_body_compilation (gnu_subprog); |
a1ab4c31 | 4172 | } |
ce2d0ce2 | 4173 | |
17a98a3d EB |
4174 | /* The type of an atomic access. */ |
4175 | ||
4176 | typedef enum { NOT_ATOMIC, SIMPLE_ATOMIC, OUTER_ATOMIC } atomic_acces_t; | |
4177 | ||
4178 | /* Return true if GNAT_NODE references an Atomic entity. This is modeled on | |
4179 | the Is_Atomic_Object predicate of the front-end, but additionally handles | |
4180 | explicit dereferences. */ | |
033ba5bf EB |
4181 | |
4182 | static bool | |
f797c2b7 | 4183 | node_is_atomic (Node_Id gnat_node) |
033ba5bf | 4184 | { |
f797c2b7 | 4185 | Entity_Id gnat_entity; |
033ba5bf | 4186 | |
f797c2b7 | 4187 | switch (Nkind (gnat_node)) |
033ba5bf | 4188 | { |
f797c2b7 EB |
4189 | case N_Identifier: |
4190 | case N_Expanded_Name: | |
4191 | gnat_entity = Entity (gnat_node); | |
1141fbd7 | 4192 | if (!Is_Object (gnat_entity)) |
f797c2b7 | 4193 | break; |
1141fbd7 EB |
4194 | return Is_Atomic (gnat_entity) |
4195 | || (Is_Atomic (Etype (gnat_entity)) | |
4196 | && !simple_constant_p (gnat_entity)); | |
f797c2b7 EB |
4197 | |
4198 | case N_Selected_Component: | |
17a98a3d EB |
4199 | return Is_Atomic (Etype (gnat_node)) |
4200 | || Is_Atomic (Entity (Selector_Name (gnat_node))); | |
f797c2b7 EB |
4201 | |
4202 | case N_Indexed_Component: | |
17a98a3d EB |
4203 | return Is_Atomic (Etype (gnat_node)) |
4204 | || Has_Atomic_Components (Etype (Prefix (gnat_node))) | |
4205 | || (Is_Entity_Name (Prefix (gnat_node)) | |
4206 | && Has_Atomic_Components (Entity (Prefix (gnat_node)))); | |
f797c2b7 EB |
4207 | |
4208 | case N_Explicit_Dereference: | |
4209 | return Is_Atomic (Etype (gnat_node)); | |
4210 | ||
4211 | default: | |
4212 | break; | |
033ba5bf EB |
4213 | } |
4214 | ||
f797c2b7 EB |
4215 | return false; |
4216 | } | |
4217 | ||
17a98a3d | 4218 | /* Return true if GNAT_NODE references a Volatile_Full_Access entity. This is |
2cee58d8 EB |
4219 | modeled on the Is_Volatile_Full_Access_Object predicate of the front-end, |
4220 | but additionally handles explicit dereferences. */ | |
f797c2b7 EB |
4221 | |
4222 | static bool | |
17a98a3d | 4223 | node_is_volatile_full_access (Node_Id gnat_node) |
f797c2b7 EB |
4224 | { |
4225 | Entity_Id gnat_entity; | |
4226 | ||
4227 | switch (Nkind (gnat_node)) | |
033ba5bf | 4228 | { |
033ba5bf | 4229 | case N_Identifier: |
f797c2b7 EB |
4230 | case N_Expanded_Name: |
4231 | gnat_entity = Entity (gnat_node); | |
f1bc6414 EB |
4232 | if (!Is_Object (gnat_entity)) |
4233 | break; | |
f797c2b7 | 4234 | return Is_Volatile_Full_Access (gnat_entity) |
1141fbd7 EB |
4235 | || (Is_Volatile_Full_Access (Etype (gnat_entity)) |
4236 | && !simple_constant_p (gnat_entity)); | |
f797c2b7 | 4237 | |
033ba5bf | 4238 | case N_Selected_Component: |
17a98a3d EB |
4239 | return Is_Volatile_Full_Access (Etype (gnat_node)) |
4240 | || Is_Volatile_Full_Access (Entity (Selector_Name (gnat_node))); | |
f797c2b7 EB |
4241 | |
4242 | case N_Indexed_Component: | |
4243 | case N_Explicit_Dereference: | |
4244 | return Is_Volatile_Full_Access (Etype (gnat_node)); | |
033ba5bf EB |
4245 | |
4246 | default: | |
f797c2b7 | 4247 | break; |
033ba5bf EB |
4248 | } |
4249 | ||
f797c2b7 EB |
4250 | return false; |
4251 | } | |
4252 | ||
17a98a3d | 4253 | /* Return true if GNAT_NODE references a component of a larger object. */ |
f797c2b7 | 4254 | |
17a98a3d EB |
4255 | static inline bool |
4256 | node_is_component (Node_Id gnat_node) | |
f797c2b7 | 4257 | { |
17a98a3d EB |
4258 | const Node_Kind k = Nkind (gnat_node); |
4259 | return | |
4260 | (k == N_Indexed_Component || k == N_Selected_Component || k == N_Slice); | |
f797c2b7 EB |
4261 | } |
4262 | ||
17a98a3d EB |
4263 | /* Compute whether GNAT_NODE requires atomic access and set TYPE to the type |
4264 | of access and SYNC according to the associated synchronization setting. | |
241125b2 | 4265 | |
17a98a3d | 4266 | We implement 3 different semantics of atomicity in this function: |
241125b2 | 4267 | |
17a98a3d | 4268 | 1. the Ada 95/2005/2012 semantics of the Atomic aspect/pragma, |
81e68a19 | 4269 | 2. the Ada 2022 semantics of the Atomic aspect/pragma, |
17a98a3d | 4270 | 3. the semantics of the Volatile_Full_Access GNAT aspect/pragma. |
f797c2b7 | 4271 | |
17a98a3d | 4272 | They are mutually exclusive and the FE should have rejected conflicts. */ |
f797c2b7 | 4273 | |
17a98a3d EB |
4274 | static void |
4275 | get_atomic_access (Node_Id gnat_node, atomic_acces_t *type, bool *sync) | |
f797c2b7 | 4276 | { |
17a98a3d | 4277 | Node_Id gnat_parent, gnat_temp; |
f797c2b7 | 4278 | unsigned char attr_id; |
f797c2b7 | 4279 | |
17a98a3d EB |
4280 | /* First, scan the parent to filter out irrelevant cases. */ |
4281 | gnat_parent = Parent (gnat_node); | |
f797c2b7 | 4282 | switch (Nkind (gnat_parent)) |
033ba5bf EB |
4283 | { |
4284 | case N_Attribute_Reference: | |
4285 | attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent)); | |
4286 | /* Do not mess up machine code insertions. */ | |
4287 | if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output) | |
17a98a3d | 4288 | goto not_atomic; |
f797c2b7 EB |
4289 | |
4290 | /* Nothing to do if we are the prefix of an attribute, since we do not | |
4291 | want an atomic access for things like 'Size. */ | |
4292 | ||
9c453de7 | 4293 | /* ... fall through ... */ |
f797c2b7 EB |
4294 | |
4295 | case N_Reference: | |
4296 | /* The N_Reference node is like an attribute. */ | |
4297 | if (Prefix (gnat_parent) == gnat_node) | |
17a98a3d | 4298 | goto not_atomic; |
033ba5bf EB |
4299 | break; |
4300 | ||
4301 | case N_Object_Renaming_Declaration: | |
f797c2b7 EB |
4302 | /* Nothing to do for the identifier in an object renaming declaration, |
4303 | the renaming itself does not need atomic access. */ | |
17a98a3d | 4304 | goto not_atomic; |
033ba5bf EB |
4305 | |
4306 | default: | |
4307 | break; | |
4308 | } | |
4309 | ||
17a98a3d EB |
4310 | /* Now strip any type conversion from GNAT_NODE. */ |
4311 | if (Nkind (gnat_node) == N_Type_Conversion | |
4312 | || Nkind (gnat_node) == N_Unchecked_Type_Conversion) | |
4313 | gnat_node = Expression (gnat_node); | |
f797c2b7 | 4314 | |
17a98a3d | 4315 | /* Up to Ada 2012, for Atomic itself, only reads and updates of the object as |
81e68a19 | 4316 | a whole require atomic access (RM C.6(15)). But, starting with Ada 2022, |
17a98a3d EB |
4317 | reads of or writes to a nonatomic subcomponent of the object also require |
4318 | atomic access (RM C.6(19)). */ | |
4319 | if (node_is_atomic (gnat_node)) | |
4320 | { | |
4321 | bool as_a_whole = true; | |
f797c2b7 | 4322 | |
17a98a3d EB |
4323 | /* If we are the prefix of the parent, then the access is partial. */ |
4324 | for (gnat_temp = gnat_node, gnat_parent = Parent (gnat_temp); | |
4325 | node_is_component (gnat_parent) && Prefix (gnat_parent) == gnat_temp; | |
4326 | gnat_temp = gnat_parent, gnat_parent = Parent (gnat_temp)) | |
81e68a19 | 4327 | if (Ada_Version < Ada_2022 || node_is_atomic (gnat_parent)) |
17a98a3d EB |
4328 | goto not_atomic; |
4329 | else | |
4330 | as_a_whole = false; | |
f797c2b7 | 4331 | |
17a98a3d EB |
4332 | /* We consider that partial accesses are not sequential actions and, |
4333 | therefore, do not require synchronization. */ | |
4334 | *type = SIMPLE_ATOMIC; | |
4335 | *sync = as_a_whole ? Atomic_Sync_Required (gnat_node) : false; | |
4336 | return; | |
4337 | } | |
f797c2b7 | 4338 | |
17a98a3d EB |
4339 | /* Look for an outer atomic access of a nonatomic subcomponent. Note that, |
4340 | for VFA, we do this before looking at the node itself because we need to | |
4341 | access the outermost VFA object atomically, unlike for Atomic where it is | |
4342 | the innermost atomic object (RM C.6(19)). */ | |
4343 | for (gnat_temp = gnat_node; | |
4344 | node_is_component (gnat_temp); | |
4345 | gnat_temp = Prefix (gnat_temp)) | |
81e68a19 | 4346 | if ((Ada_Version >= Ada_2022 && node_is_atomic (Prefix (gnat_temp))) |
17a98a3d EB |
4347 | || node_is_volatile_full_access (Prefix (gnat_temp))) |
4348 | { | |
4349 | *type = OUTER_ATOMIC; | |
4350 | *sync = false; | |
4351 | return; | |
4352 | } | |
4353 | ||
4354 | /* Unlike Atomic, accessing a VFA object always requires atomic access. */ | |
4355 | if (node_is_volatile_full_access (gnat_node)) | |
4356 | { | |
4357 | *type = SIMPLE_ATOMIC; | |
4358 | *sync = false; | |
4359 | return; | |
4360 | } | |
4361 | ||
4362 | not_atomic: | |
4363 | *type = NOT_ATOMIC; | |
4364 | *sync = false; | |
033ba5bf | 4365 | } |
ce2d0ce2 EB |
4366 | |
4367 | /* Return true if GNAT_NODE requires simple atomic access and, if so, set SYNC | |
17a98a3d EB |
4368 | according to the associated synchronization setting. */ |
4369 | ||
4370 | static inline bool | |
4371 | simple_atomic_access_required_p (Node_Id gnat_node, bool *sync) | |
4372 | { | |
4373 | atomic_acces_t type; | |
4374 | get_atomic_access (gnat_node, &type, sync); | |
4375 | return type == SIMPLE_ATOMIC; | |
4376 | } | |
4377 | ||
544d14e1 EB |
4378 | /* Return the storage model specified by GNAT_NODE, or else Empty. */ |
4379 | ||
4380 | static Entity_Id | |
4381 | get_storage_model (Node_Id gnat_node) | |
4382 | { | |
4383 | if (Nkind (gnat_node) == N_Explicit_Dereference | |
4384 | && Has_Designated_Storage_Model_Aspect (Etype (Prefix (gnat_node)))) | |
4385 | return Storage_Model_Object (Etype (Prefix (gnat_node))); | |
4386 | else | |
4387 | return Empty; | |
4388 | } | |
4389 | ||
4390 | /* Compute whether GNAT_NODE requires storage model access and set GNAT_SMO to | |
4391 | the storage model object to be used for it if it does, or else Empty. */ | |
4392 | ||
4393 | static void | |
4394 | get_storage_model_access (Node_Id gnat_node, Entity_Id *gnat_smo) | |
4395 | { | |
4396 | const Node_Id gnat_parent = Parent (gnat_node); | |
b3c206bf | 4397 | *gnat_smo = Empty; |
544d14e1 | 4398 | |
b3c206bf | 4399 | switch (Nkind (gnat_parent)) |
544d14e1 | 4400 | { |
b3c206bf MP |
4401 | case N_Attribute_Reference: |
4402 | /* If the parent is an attribute reference that requires an lvalue and | |
4403 | gnat_node is the Prefix (i.e. not a parameter), we do not need to | |
4404 | actually access any storage. */ | |
4405 | if (lvalue_required_for_attribute_p (gnat_parent) | |
4406 | && Prefix (gnat_parent) == gnat_node) | |
4407 | return; | |
4408 | break; | |
4409 | ||
4410 | case N_Object_Renaming_Declaration: | |
4411 | /* Nothing to do for the identifier in an object renaming declaration, | |
4412 | the renaming itself does not need storage model access. */ | |
544d14e1 | 4413 | return; |
b3c206bf MP |
4414 | |
4415 | default: | |
4416 | break; | |
544d14e1 EB |
4417 | } |
4418 | ||
b3c206bf MP |
4419 | /* If we are the prefix of the parent, then the access is above us. */ |
4420 | if (node_is_component (gnat_parent) && Prefix (gnat_parent) == gnat_node) | |
4421 | return; | |
4422 | ||
798c08ca EB |
4423 | /* Now strip any type conversion from GNAT_NODE. */ |
4424 | if (Nkind (gnat_node) == N_Type_Conversion | |
4425 | || Nkind (gnat_node) == N_Unchecked_Type_Conversion) | |
4426 | gnat_node = Expression (gnat_node); | |
4427 | ||
544d14e1 EB |
4428 | while (node_is_component (gnat_node)) |
4429 | gnat_node = Prefix (gnat_node); | |
4430 | ||
4431 | *gnat_smo = get_storage_model (gnat_node); | |
4432 | } | |
4433 | ||
4434 | /* Return true if GNAT_NODE requires storage model access and, if so, set | |
4435 | GNAT_SMO to the storage model object to be used for it. */ | |
4436 | ||
4437 | static bool | |
4438 | storage_model_access_required_p (Node_Id gnat_node, Entity_Id *gnat_smo) | |
4439 | { | |
4440 | get_storage_model_access (gnat_node, gnat_smo); | |
4441 | return Present (*gnat_smo); | |
4442 | } | |
4443 | ||
ddb5a105 EB |
4444 | /* Create a temporary variable with PREFIX and TYPE, and return it. */ |
4445 | ||
4446 | static tree | |
4447 | create_temporary (const char *prefix, tree type) | |
4448 | { | |
2056c5ed EB |
4449 | tree gnu_temp |
4450 | = create_var_decl (create_tmp_var_name (prefix), NULL_TREE, | |
4451 | type, NULL_TREE, | |
4452 | false, false, false, false, false, | |
4453 | true, false, NULL, Empty); | |
ddb5a105 EB |
4454 | return gnu_temp; |
4455 | } | |
35a382b8 EB |
4456 | |
4457 | /* Create a temporary variable with PREFIX and initialize it with GNU_INIT. | |
4458 | Put the initialization statement into GNU_INIT_STMT and annotate it with | |
4459 | the SLOC of GNAT_NODE. Return the temporary variable. */ | |
4460 | ||
4461 | static tree | |
4462 | create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt, | |
4463 | Node_Id gnat_node) | |
4464 | { | |
ddb5a105 | 4465 | tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init)); |
35a382b8 EB |
4466 | |
4467 | *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init); | |
4468 | set_expr_location_from_node (*gnu_init_stmt, gnat_node); | |
4469 | ||
4470 | return gnu_temp; | |
4471 | } | |
4472 | ||
b81e2d5e EB |
4473 | /* Return true if TYPE is an array of scalar type. */ |
4474 | ||
4475 | static bool | |
4476 | is_array_of_scalar_type (tree type) | |
4477 | { | |
4478 | if (TREE_CODE (type) != ARRAY_TYPE) | |
4479 | return false; | |
4480 | ||
4481 | type = TREE_TYPE (type); | |
4482 | ||
4483 | return !AGGREGATE_TYPE_P (type) && !POINTER_TYPE_P (type); | |
4484 | } | |
4485 | ||
4486 | /* Helper function for walk_tree, used by return_slot_opt_for_pure_call_p. */ | |
4487 | ||
4488 | static tree | |
4489 | find_decls_r (tree *tp, int *walk_subtrees, void *data) | |
4490 | { | |
4491 | bitmap decls = (bitmap) data; | |
4492 | ||
4493 | if (TYPE_P (*tp)) | |
4494 | *walk_subtrees = 0; | |
4495 | ||
4496 | else if (DECL_P (*tp)) | |
4497 | bitmap_set_bit (decls, DECL_UID (*tp)); | |
4498 | ||
4499 | return NULL_TREE; | |
4500 | } | |
4501 | ||
4502 | /* Return whether the assignment TARGET = CALL can be subject to the return | |
4503 | slot optimization, under the assumption that the called function be pure | |
4504 | in the Ada sense and return an array of scalar type. */ | |
4505 | ||
4506 | static bool | |
4507 | return_slot_opt_for_pure_call_p (tree target, tree call) | |
4508 | { | |
4509 | /* Check that the target is a DECL. */ | |
4510 | if (!DECL_P (target)) | |
4511 | return false; | |
4512 | ||
4513 | const bitmap decls = BITMAP_GGC_ALLOC (); | |
4514 | call_expr_arg_iterator iter; | |
4515 | tree arg; | |
4516 | ||
4517 | /* Check that all the arguments have either a scalar type (we assume that | |
4518 | this means by-copy passing mechanism) or array of scalar type. */ | |
4519 | FOR_EACH_CALL_EXPR_ARG (arg, iter, call) | |
4520 | { | |
4521 | tree arg_type = TREE_TYPE (arg); | |
4522 | if (TREE_CODE (arg_type) == REFERENCE_TYPE) | |
4523 | arg_type = TREE_TYPE (arg_type); | |
4524 | ||
4525 | if (is_array_of_scalar_type (arg_type)) | |
4526 | walk_tree_without_duplicates (&arg, find_decls_r, decls); | |
4527 | ||
4528 | else if (AGGREGATE_TYPE_P (arg_type) || POINTER_TYPE_P (arg_type)) | |
4529 | return false; | |
4530 | } | |
4531 | ||
4532 | /* Check that the target is not referenced by the non-scalar arguments. */ | |
4533 | return !bitmap_bit_p (decls, DECL_UID (target)); | |
4534 | } | |
4535 | ||
a42dd9fe EB |
4536 | /* Elaborate types referenced in the profile (FIRST_FORMAL, RESULT_TYPE). */ |
4537 | ||
4538 | static void | |
4539 | elaborate_profile (Entity_Id first_formal, Entity_Id result_type) | |
4540 | { | |
4541 | Entity_Id formal; | |
4542 | ||
4543 | for (formal = first_formal; | |
4544 | Present (formal); | |
4545 | formal = Next_Formal_With_Extras (formal)) | |
4546 | (void) gnat_to_gnu_type (Etype (formal)); | |
4547 | ||
4548 | if (Present (result_type) && Ekind (result_type) != E_Void) | |
4549 | (void) gnat_to_gnu_type (result_type); | |
4550 | } | |
4551 | ||
e08f1aad | 4552 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Function_Call |
a1ab4c31 AC |
4553 | or an N_Procedure_Call_Statement, to a GCC tree, which is returned. |
4554 | GNU_RESULT_TYPE_P is a pointer to where we should place the result type. | |
0b3467c4 | 4555 | If GNU_TARGET is non-null, this must be a function call on the RHS of a |
033ba5bf | 4556 | N_Assignment_Statement and the result is to be placed into that object. |
17a98a3d EB |
4557 | ATOMIC_ACCESS is the type of atomic access to be used for the assignment |
4558 | to GNU_TARGET. If, in addition, ATOMIC_SYNC is true, then the assignment | |
2d4883a1 EB |
4559 | to GNU_TARGET requires atomic synchronization. GNAT_SMO is the storage |
4560 | model object to be used for the assignment to GNU_TARGET or Empty if there | |
4561 | is none. */ | |
a1ab4c31 AC |
4562 | |
4563 | static tree | |
80096613 | 4564 | Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target, |
2d4883a1 | 4565 | atomic_acces_t atomic_access, bool atomic_sync, Entity_Id gnat_smo) |
a1ab4c31 | 4566 | { |
ddb5a105 EB |
4567 | const bool function_call = (Nkind (gnat_node) == N_Function_Call); |
4568 | const bool returning_value = (function_call && !gnu_target); | |
a1ab4c31 AC |
4569 | /* The GCC node corresponding to the GNAT subprogram name. This can either |
4570 | be a FUNCTION_DECL node if we are dealing with a standard subprogram call, | |
4571 | or an indirect reference expression (an INDIRECT_REF node) pointing to a | |
4572 | subprogram. */ | |
298339ef EB |
4573 | const Node_Id gnat_subprog = Name (gnat_node); |
4574 | tree gnu_subprog = gnat_to_gnu (gnat_subprog); | |
a1ab4c31 | 4575 | /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */ |
ced57283 | 4576 | tree gnu_subprog_type = TREE_TYPE (gnu_subprog); |
ddb5a105 | 4577 | /* The return type of the FUNCTION_TYPE. */ |
a42dd9fe | 4578 | tree gnu_result_type;; |
b9778c45 EB |
4579 | const bool frontend_builtin |
4580 | = (TREE_CODE (gnu_subprog) == FUNCTION_DECL | |
4581 | && DECL_BUILT_IN_CLASS (gnu_subprog) == BUILT_IN_FRONTEND); | |
4582 | auto_vec<tree, 16> gnu_actual_vec; | |
a1ab4c31 | 4583 | tree gnu_name_list = NULL_TREE; |
ddb5a105 | 4584 | tree gnu_stmt_list = NULL_TREE; |
a1ab4c31 | 4585 | tree gnu_after_list = NULL_TREE; |
ddb5a105 | 4586 | tree gnu_retval = NULL_TREE; |
35a382b8 | 4587 | tree gnu_call, gnu_result; |
f53aff92 EB |
4588 | bool went_into_elab_proc; |
4589 | bool pushed_binding_level; | |
c95f808d EB |
4590 | bool variadic; |
4591 | bool by_descriptor; | |
ddb5a105 | 4592 | Entity_Id gnat_formal; |
a42dd9fe | 4593 | Entity_Id gnat_result_type; |
ddb5a105 | 4594 | Node_Id gnat_actual; |
17a98a3d EB |
4595 | atomic_acces_t aa_type; |
4596 | bool aa_sync; | |
a1ab4c31 | 4597 | |
f15ad1e3 | 4598 | /* The only way we can make a call via an access type is if GNAT_NAME is an |
a1ab4c31 | 4599 | explicit dereference. In that case, get the list of formal args from the |
ced57283 | 4600 | type the access type is pointing to. Otherwise, get the formals from the |
a1ab4c31 | 4601 | entity being called. */ |
298339ef | 4602 | if (Nkind (gnat_subprog) == N_Explicit_Dereference) |
4c640e26 | 4603 | { |
c95f808d | 4604 | const Entity_Id gnat_prefix_type |
298339ef | 4605 | = Underlying_Type (Etype (Prefix (gnat_subprog))); |
c95f808d | 4606 | |
298339ef | 4607 | gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog)); |
a42dd9fe | 4608 | gnat_result_type = Etype (Etype (gnat_subprog)); |
c95f808d | 4609 | variadic = IN (Convention (gnat_prefix_type), Convention_C_Variadic); |
4c640e26 EB |
4610 | |
4611 | /* If the access type doesn't require foreign-compatible representation, | |
4612 | be prepared for descriptors. */ | |
c95f808d EB |
4613 | by_descriptor |
4614 | = targetm.calls.custom_function_descriptors > 0 | |
4615 | && Can_Use_Internal_Rep (gnat_prefix_type); | |
4c640e26 | 4616 | } |
f15ad1e3 | 4617 | |
298339ef | 4618 | else if (Nkind (gnat_subprog) == N_Attribute_Reference) |
c95f808d EB |
4619 | { |
4620 | /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */ | |
4621 | gnat_formal = Empty; | |
a42dd9fe | 4622 | gnat_result_type = Empty; |
c95f808d EB |
4623 | variadic = false; |
4624 | by_descriptor = false; | |
4625 | } | |
f15ad1e3 | 4626 | |
a1ab4c31 | 4627 | else |
c95f808d | 4628 | { |
298339ef | 4629 | gcc_checking_assert (Is_Entity_Name (gnat_subprog)); |
f15ad1e3 | 4630 | |
298339ef | 4631 | gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog)); |
a42dd9fe | 4632 | gnat_result_type = Etype (Entity_Id (gnat_subprog)); |
298339ef | 4633 | variadic = IN (Convention (Entity (gnat_subprog)), Convention_C_Variadic); |
c95f808d | 4634 | by_descriptor = false; |
f15ad1e3 EB |
4635 | |
4636 | /* If we are calling a stubbed function, then raise Program_Error, but | |
4637 | elaborate all our args first. */ | |
298339ef | 4638 | if (Convention (Entity (gnat_subprog)) == Convention_Stubbed) |
f15ad1e3 EB |
4639 | { |
4640 | tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called, | |
4641 | gnat_node, N_Raise_Program_Error); | |
4642 | ||
4643 | for (gnat_actual = First_Actual (gnat_node); | |
4644 | Present (gnat_actual); | |
4645 | gnat_actual = Next_Actual (gnat_actual)) | |
4646 | add_stmt (gnat_to_gnu (gnat_actual)); | |
4647 | ||
4648 | if (returning_value) | |
4649 | { | |
a42dd9fe | 4650 | gnu_result_type = TREE_TYPE (gnu_subprog_type); |
f15ad1e3 EB |
4651 | *gnu_result_type_p = gnu_result_type; |
4652 | return build1 (NULL_EXPR, gnu_result_type, call_expr); | |
4653 | } | |
4654 | ||
4655 | return call_expr; | |
4656 | } | |
4657 | } | |
4658 | ||
a42dd9fe EB |
4659 | /* We must elaborate the entire profile now because, if it references types |
4660 | that were initially incomplete,, their elaboration changes the contents | |
4661 | of GNU_SUBPROG_TYPE and, in particular, may change the result type. */ | |
4662 | elaborate_profile (gnat_formal, gnat_result_type); | |
4663 | ||
f15ad1e3 | 4664 | gcc_assert (FUNC_OR_METHOD_TYPE_P (gnu_subprog_type)); |
a42dd9fe | 4665 | gnu_result_type = TREE_TYPE (gnu_subprog_type); |
f15ad1e3 EB |
4666 | |
4667 | if (TREE_CODE (gnu_subprog) == FUNCTION_DECL) | |
4668 | { | |
4669 | /* For a call to a nested function, check the inlining status. */ | |
4670 | if (decl_function_context (gnu_subprog)) | |
4671 | check_inlining_for_nested_subprog (gnu_subprog); | |
4672 | ||
4673 | /* For a recursive call, avoid explosion due to recursive inlining. */ | |
4674 | if (gnu_subprog == current_function_decl) | |
4675 | DECL_DISREGARD_INLINE_LIMITS (gnu_subprog) = 0; | |
c95f808d | 4676 | } |
a1ab4c31 | 4677 | |
ddb5a105 EB |
4678 | /* The lifetime of the temporaries created for the call ends right after the |
4679 | return value is copied, so we can give them the scope of the elaboration | |
4680 | routine at top level. */ | |
35a382b8 | 4681 | if (!current_function_decl) |
0b3467c4 | 4682 | { |
2231f17f | 4683 | current_function_decl = get_elaboration_procedure (); |
0b3467c4 EB |
4684 | went_into_elab_proc = true; |
4685 | } | |
f53aff92 EB |
4686 | else |
4687 | went_into_elab_proc = false; | |
0b3467c4 | 4688 | |
4a582c9f EB |
4689 | /* First, create the temporary for the return value when: |
4690 | ||
4691 | 1. There is no target and the function has copy-in/copy-out parameters, | |
4692 | because we need to preserve the return value before copying back the | |
4693 | parameters. | |
4694 | ||
b81e2d5e EB |
4695 | 2. There is no target and the call is made for neither the declaration |
4696 | of an object (regular or renaming), nor a return statement, nor an | |
4697 | allocator, nor an aggregate, and the return type has variable size | |
4698 | because in this case the gimplifier cannot create the temporary, or | |
4699 | more generally is an aggregate type, because the gimplifier would | |
4700 | create the temporary in the outermost scope instead of locally here. | |
4701 | But there is an exception for an allocator of unconstrained record | |
4702 | type with default discriminant because we allocate the actual size | |
4703 | in this case, unlike in the other cases, so we need a temporary to | |
4704 | fetch the discriminant and we create it here. | |
4a582c9f EB |
4705 | |
4706 | 3. There is a target and it is a slice or an array with fixed size, | |
4707 | and the return type has variable size, because the gimplifier | |
4708 | doesn't handle these cases. | |
4709 | ||
02221bed EB |
4710 | 4. There is a target which is a bit-field and the function returns an |
4711 | unconstrained record type with default discriminant, because the | |
4712 | return may copy more data than the bit-field can contain. | |
4713 | ||
544d14e1 EB |
4714 | 5. There is a target which needs to be accessed with a storage model. |
4715 | ||
4716 | 6. There is no target and we have misaligned In Out or Out parameters | |
5d2a63dc EB |
4717 | passed by reference, because we need to preserve the return value |
4718 | before copying back the parameters. However, in this case, we'll | |
4719 | defer creating the temporary, see below. | |
4720 | ||
4a582c9f EB |
4721 | This must be done before we push a binding level around the call, since |
4722 | we will pop it before copying the return value. */ | |
ddb5a105 | 4723 | if (function_call |
4a582c9f EB |
4724 | && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type)) |
4725 | || (!gnu_target | |
4726 | && Nkind (Parent (gnat_node)) != N_Object_Declaration | |
fc7a823e | 4727 | && Nkind (Parent (gnat_node)) != N_Object_Renaming_Declaration |
5d2a63dc | 4728 | && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement |
1b6f8e97 EB |
4729 | && (!(Nkind (Parent (gnat_node)) == N_Qualified_Expression |
4730 | && Nkind (Parent (Parent (gnat_node))) == N_Allocator) | |
4731 | || type_is_padding_self_referential (gnu_result_type)) | |
b81e2d5e | 4732 | && Nkind (Parent (gnat_node)) != N_Aggregate |
5d2a63dc EB |
4733 | && AGGREGATE_TYPE_P (gnu_result_type) |
4734 | && !TYPE_IS_FAT_POINTER_P (gnu_result_type)) | |
4a582c9f EB |
4735 | || (gnu_target |
4736 | && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF | |
4737 | || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE | |
4738 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target))) | |
4739 | == INTEGER_CST)) | |
02221bed EB |
4740 | && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST) |
4741 | || (gnu_target | |
4742 | && TREE_CODE (gnu_target) == COMPONENT_REF | |
4743 | && DECL_BIT_FIELD (TREE_OPERAND (gnu_target, 1)) | |
1e66a917 EB |
4744 | && DECL_SIZE (TREE_OPERAND (gnu_target, 1)) |
4745 | != TYPE_SIZE (TREE_TYPE (gnu_target)) | |
544d14e1 EB |
4746 | && type_is_padding_self_referential (gnu_result_type)) |
4747 | || (gnu_target | |
2d4883a1 EB |
4748 | && Present (gnat_smo) |
4749 | && Present (Storage_Model_Copy_To (gnat_smo))))) | |
93e708f9 EB |
4750 | { |
4751 | gnu_retval = create_temporary ("R", gnu_result_type); | |
4752 | DECL_RETURN_VALUE_P (gnu_retval) = 1; | |
4753 | } | |
ddb5a105 | 4754 | |
5d2a63dc EB |
4755 | /* If we don't need a value or have already created it, push a binding level |
4756 | around the call. This will narrow the lifetime of the temporaries we may | |
4757 | need to make when translating the parameters as much as possible. */ | |
4758 | if (!returning_value || gnu_retval) | |
4759 | { | |
4760 | start_stmt_group (); | |
4761 | gnat_pushlevel (); | |
4762 | pushed_binding_level = true; | |
4763 | } | |
f53aff92 EB |
4764 | else |
4765 | pushed_binding_level = false; | |
5d2a63dc | 4766 | |
ced57283 EB |
4767 | /* Create the list of the actual parameters as GCC expects it, namely a |
4768 | chain of TREE_LIST nodes in which the TREE_VALUE field of each node | |
4769 | is an expression and the TREE_PURPOSE field is null. But skip Out | |
4770 | parameters not passed by reference and that need not be copied in. */ | |
a1ab4c31 AC |
4771 | for (gnat_actual = First_Actual (gnat_node); |
4772 | Present (gnat_actual); | |
4773 | gnat_formal = Next_Formal_With_Extras (gnat_formal), | |
4774 | gnat_actual = Next_Actual (gnat_actual)) | |
4775 | { | |
19f51f28 | 4776 | Entity_Id gnat_formal_type = Etype (gnat_formal); |
7cdb6871 | 4777 | tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type); |
ced57283 EB |
4778 | tree gnu_formal = present_gnu_tree (gnat_formal) |
4779 | ? get_gnu_tree (gnat_formal) : NULL_TREE; | |
6e03839f | 4780 | const bool in_param = (Ekind (gnat_formal) == E_In_Parameter); |
c946adde EB |
4781 | const bool is_true_formal_parm |
4782 | = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL; | |
033ba5bf | 4783 | const bool is_by_ref_formal_parm |
1eb58520 AC |
4784 | = is_true_formal_parm |
4785 | && (DECL_BY_REF_P (gnu_formal) | |
4786 | || DECL_BY_COMPONENT_PTR_P (gnu_formal)); | |
6e03839f | 4787 | /* In the In Out or Out case, we must suppress conversions that yield |
c34f3839 EB |
4788 | an lvalue but can nevertheless cause the creation of a temporary, |
4789 | because we need the real object in this case, either to pass its | |
4790 | address if it's passed by reference or as target of the back copy | |
ddb5a105 | 4791 | done after the call if it uses the copy-in/copy-out mechanism. |
c34f3839 | 4792 | We do it in the In case too, except for an unchecked conversion |
19f51f28 EB |
4793 | to an elementary type or a constrained composite type because it |
4794 | alone can cause the actual to be misaligned and the addressability | |
4795 | test is applied to the real object. */ | |
c946adde | 4796 | const bool suppress_type_conversion |
a1ab4c31 | 4797 | = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion |
6e03839f | 4798 | && (!in_param |
1ab1c4ee | 4799 | || !is_by_ref_formal_parm |
19f51f28 EB |
4800 | || (Is_Composite_Type (Underlying_Type (gnat_formal_type)) |
4801 | && !Is_Constrained (Underlying_Type (gnat_formal_type))))) | |
a1ab4c31 | 4802 | || (Nkind (gnat_actual) == N_Type_Conversion |
19f51f28 | 4803 | && Is_Composite_Type (Underlying_Type (gnat_formal_type)))); |
ced57283 EB |
4804 | Node_Id gnat_name = suppress_type_conversion |
4805 | ? Expression (gnat_actual) : gnat_actual; | |
a1ab4c31 | 4806 | tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type; |
a1ab4c31 AC |
4807 | |
4808 | /* If it's possible we may need to use this expression twice, make sure | |
ced57283 | 4809 | that any side-effects are handled via SAVE_EXPRs; likewise if we need |
7194767c | 4810 | to force side-effects before the call. */ |
6e03839f | 4811 | if (!in_param && !is_by_ref_formal_parm) |
fc7a823e EB |
4812 | { |
4813 | tree init = NULL_TREE; | |
4814 | gnu_name = gnat_stabilize_reference (gnu_name, true, &init); | |
4815 | if (init) | |
4816 | gnu_name | |
4817 | = build_compound_expr (TREE_TYPE (gnu_name), init, gnu_name); | |
4818 | } | |
a1ab4c31 | 4819 | |
2d4883a1 EB |
4820 | /* If we are passing a non-addressable parameter by reference, pass the |
4821 | address of a copy. In the In Out or Out case, set up to copy back | |
4822 | out after the call. */ | |
033ba5bf | 4823 | if (is_by_ref_formal_parm |
a1ab4c31 | 4824 | && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name))) |
2d4883a1 | 4825 | && !addressable_p (gnu_name, gnu_name_type)) |
a1ab4c31 | 4826 | { |
0b3467c4 EB |
4827 | tree gnu_orig = gnu_name, gnu_temp, gnu_stmt; |
4828 | ||
4829 | /* Do not issue warnings for CONSTRUCTORs since this is not a copy | |
4830 | but sort of an instantiation for them. */ | |
1edbeb15 | 4831 | if (TREE_CODE (remove_conversions (gnu_name, true)) == CONSTRUCTOR) |
0b3467c4 EB |
4832 | ; |
4833 | ||
1edbeb15 EB |
4834 | /* If the formal is passed by reference, a copy is not allowed. */ |
4835 | else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type) | |
4836 | || Is_Aliased (gnat_formal)) | |
0b3467c4 EB |
4837 | post_error ("misaligned actual cannot be passed by reference", |
4838 | gnat_actual); | |
4839 | ||
1edbeb15 EB |
4840 | /* If the mechanism was forced to by-ref, a copy is not allowed but |
4841 | we issue only a warning because this case is not strict Ada. */ | |
4842 | else if (DECL_FORCED_BY_REF_P (gnu_formal)) | |
4843 | post_error ("misaligned actual cannot be passed by reference??", | |
4844 | gnat_actual); | |
a1ab4c31 | 4845 | |
56fe7b05 EB |
4846 | /* If the actual type of the object is already the nominal type, |
4847 | we have nothing to do, except if the size is self-referential | |
4848 | in which case we'll remove the unpadding below. */ | |
4849 | if (TREE_TYPE (gnu_name) == gnu_name_type | |
4850 | && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type))) | |
4851 | ; | |
4852 | ||
0b3467c4 | 4853 | /* Otherwise remove the unpadding from all the objects. */ |
56fe7b05 | 4854 | else if (TREE_CODE (gnu_name) == COMPONENT_REF |
315cff15 EB |
4855 | && TYPE_IS_PADDING_P |
4856 | (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))) | |
0b3467c4 | 4857 | gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0); |
a1ab4c31 | 4858 | |
169afcb9 EB |
4859 | /* Otherwise convert to the nominal type of the object if needed. |
4860 | There are several cases in which we need to make the temporary | |
4861 | using this type instead of the actual type of the object when | |
4862 | they are distinct, because the expectations of the callee would | |
4863 | otherwise not be met: | |
a1ab4c31 | 4864 | - if it's a justified modular type, |
169afcb9 EB |
4865 | - if the actual type is a smaller form of it, |
4866 | - if it's a smaller form of the actual type. */ | |
4867 | else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE | |
4868 | && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type) | |
4869 | || smaller_form_type_p (TREE_TYPE (gnu_name), | |
4870 | gnu_name_type))) | |
4871 | || (INTEGRAL_TYPE_P (gnu_name_type) | |
4872 | && smaller_form_type_p (gnu_name_type, | |
4873 | TREE_TYPE (gnu_name)))) | |
a1ab4c31 AC |
4874 | gnu_name = convert (gnu_name_type, gnu_name); |
4875 | ||
ddb5a105 EB |
4876 | /* If this is an In Out or Out parameter and we're returning a value, |
4877 | we need to create a temporary for the return value because we must | |
4878 | preserve it before copying back at the very end. */ | |
4879 | if (!in_param && returning_value && !gnu_retval) | |
93e708f9 EB |
4880 | { |
4881 | gnu_retval = create_temporary ("R", gnu_result_type); | |
4882 | DECL_RETURN_VALUE_P (gnu_retval) = 1; | |
4883 | } | |
ddb5a105 | 4884 | |
5d2a63dc EB |
4885 | /* If we haven't pushed a binding level, push it now. This will |
4886 | narrow the lifetime of the temporary we are about to make as | |
4887 | much as possible. */ | |
4888 | if (!pushed_binding_level && (!returning_value || gnu_retval)) | |
35a382b8 EB |
4889 | { |
4890 | start_stmt_group (); | |
4891 | gnat_pushlevel (); | |
4892 | pushed_binding_level = true; | |
4893 | } | |
4894 | ||
ddb5a105 | 4895 | /* Create an explicit temporary holding the copy. */ |
544d14e1 | 4896 | |
8da3776f EB |
4897 | /* Do not initialize it for the _Init parameter of an initialization |
4898 | procedure since no data is meant to be passed in. */ | |
4899 | if (Ekind (gnat_formal) == E_Out_Parameter | |
298339ef EB |
4900 | && Is_Entity_Name (gnat_subprog) |
4901 | && Is_Init_Proc (Entity (gnat_subprog))) | |
2d4883a1 | 4902 | gnu_name = gnu_temp = create_temporary ("A", TREE_TYPE (gnu_name)); |
8da3776f EB |
4903 | |
4904 | /* Initialize it on the fly like for an implicit temporary in the | |
4905 | other cases, as we don't necessarily have a statement list. */ | |
4906 | else | |
4907 | { | |
2d4883a1 EB |
4908 | gnu_temp = create_init_temporary ("A", gnu_name, &gnu_stmt, |
4909 | gnat_actual); | |
4910 | gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt, | |
8da3776f EB |
4911 | gnu_temp); |
4912 | } | |
cb3d597d | 4913 | |
ced57283 | 4914 | /* Set up to move the copy back to the original if needed. */ |
35a382b8 | 4915 | if (!in_param) |
a1ab4c31 | 4916 | { |
bb29e9ff EB |
4917 | /* If the original is a COND_EXPR whose first arm isn't meant to |
4918 | be further used, just deal with the second arm. This is very | |
4919 | likely the conditional expression built for a check. */ | |
4920 | if (TREE_CODE (gnu_orig) == COND_EXPR | |
4921 | && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR | |
4922 | && integer_zerop | |
4923 | (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1))) | |
4924 | gnu_orig = TREE_OPERAND (gnu_orig, 2); | |
4925 | ||
2d4883a1 EB |
4926 | gnu_stmt |
4927 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp); | |
0b3467c4 | 4928 | set_expr_location_from_node (gnu_stmt, gnat_node); |
bb29e9ff | 4929 | |
0b3467c4 | 4930 | append_to_statement_list (gnu_stmt, &gnu_after_list); |
a1ab4c31 AC |
4931 | } |
4932 | } | |
4933 | ||
4934 | /* Start from the real object and build the actual. */ | |
8234d02a | 4935 | tree gnu_actual = gnu_name; |
a1ab4c31 | 4936 | |
f797c2b7 | 4937 | /* If atomic access is required for an In or In Out actual parameter, |
2d4883a1 | 4938 | build the atomic load. */ |
033ba5bf EB |
4939 | if (is_true_formal_parm |
4940 | && !is_by_ref_formal_parm | |
2d4883a1 EB |
4941 | && Ekind (gnat_formal) != E_Out_Parameter |
4942 | && simple_atomic_access_required_p (gnat_actual, &aa_sync)) | |
4943 | gnu_actual = build_atomic_load (gnu_actual, aa_sync); | |
033ba5bf | 4944 | |
a1ab4c31 AC |
4945 | /* If this was a procedure call, we may not have removed any padding. |
4946 | So do it here for the part we will use as an input, if any. */ | |
4947 | if (Ekind (gnat_formal) != E_Out_Parameter | |
a1ab4c31 | 4948 | && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))) |
c34f3839 EB |
4949 | gnu_actual |
4950 | = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual); | |
4951 | ||
4952 | /* Put back the conversion we suppressed above in the computation of the | |
4953 | real object. And even if we didn't suppress any conversion there, we | |
4954 | may have suppressed a conversion to the Etype of the actual earlier, | |
8234d02a EB |
4955 | since the parent is a procedure call, so put it back here. Note that |
4956 | we might have a dummy type here if the actual is the dereference of a | |
fccc47dd EB |
4957 | pointer to it, but that's OK when the formal is passed by reference. |
4958 | We also do not put back a conversion between an actual and a formal | |
4959 | that are unconstrained array types to avoid creating local bounds. */ | |
7a0877c0 | 4960 | tree gnu_actual_type = get_unpadded_type (Etype (gnat_actual)); |
8234d02a EB |
4961 | if (TYPE_IS_DUMMY_P (gnu_actual_type)) |
4962 | gcc_assert (is_true_formal_parm && DECL_BY_REF_P (gnu_formal)); | |
4963 | else if (suppress_type_conversion | |
4964 | && Nkind (gnat_actual) == N_Unchecked_Type_Conversion) | |
4965 | gnu_actual = unchecked_convert (gnu_actual_type, gnu_actual, | |
4966 | No_Truncation (gnat_actual)); | |
fccc47dd EB |
4967 | else if ((TREE_CODE (TREE_TYPE (gnu_actual)) == UNCONSTRAINED_ARRAY_TYPE |
4968 | || (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE | |
4969 | && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual)))) | |
4970 | && TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) | |
4971 | ; | |
a1ab4c31 | 4972 | else |
8234d02a | 4973 | gnu_actual = convert (gnu_actual_type, gnu_actual); |
c34f3839 | 4974 | |
815b5368 | 4975 | gigi_checking_assert (!Do_Range_Check (gnat_actual)); |
a1ab4c31 | 4976 | |
96540b24 | 4977 | /* First see if the parameter is passed by reference. */ |
c946adde | 4978 | if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal)) |
a1ab4c31 | 4979 | { |
6e03839f | 4980 | if (!in_param) |
a1ab4c31 AC |
4981 | { |
4982 | /* In Out or Out parameters passed by reference don't use the | |
ddb5a105 | 4983 | copy-in/copy-out mechanism so the address of the real object |
a1ab4c31 AC |
4984 | must be passed to the function. */ |
4985 | gnu_actual = gnu_name; | |
4986 | ||
4987 | /* If we have a padded type, be sure we've removed padding. */ | |
0b3467c4 | 4988 | if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))) |
a1ab4c31 AC |
4989 | gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)), |
4990 | gnu_actual); | |
4991 | ||
4992 | /* If we have the constructed subtype of an aliased object | |
4993 | with an unconstrained nominal subtype, the type of the | |
4994 | actual includes the template, although it is formally | |
4995 | constrained. So we need to convert it back to the real | |
4996 | constructed subtype to retrieve the constrained part | |
4997 | and takes its address. */ | |
4998 | if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE | |
4999 | && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual)) | |
a1ab4c31 | 5000 | && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual)) |
43a4dd82 | 5001 | && Is_Array_Type (Underlying_Type (Etype (gnat_actual)))) |
6e03839f | 5002 | gnu_actual = convert (gnu_actual_type, gnu_actual); |
a1ab4c31 AC |
5003 | } |
5004 | ||
0b3467c4 EB |
5005 | /* There is no need to convert the actual to the formal's type before |
5006 | taking its address. The only exception is for unconstrained array | |
5007 | types because of the way we build fat pointers. */ | |
7bf9a5ac EB |
5008 | if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) |
5009 | { | |
6e03839f EB |
5010 | /* Put back the conversion we suppressed above for In Out or Out |
5011 | parameters, since it may set the bounds of the actual. */ | |
5012 | if (!in_param && suppress_type_conversion) | |
5013 | gnu_actual = convert (gnu_actual_type, gnu_actual); | |
7bf9a5ac EB |
5014 | gnu_actual = convert (gnu_formal_type, gnu_actual); |
5015 | } | |
0b3467c4 | 5016 | |
6e03839f EB |
5017 | /* Take the address of the object and convert to the proper pointer |
5018 | type. */ | |
7bf9a5ac | 5019 | gnu_formal_type = TREE_TYPE (gnu_formal); |
a1ab4c31 AC |
5020 | gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); |
5021 | } | |
96540b24 EB |
5022 | |
5023 | /* Then see if the parameter is an array passed to a foreign convention | |
5024 | subprogram. */ | |
c946adde | 5025 | else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal)) |
a1ab4c31 | 5026 | { |
bb24f343 | 5027 | gnu_actual = maybe_padded_object (gnu_actual); |
a1ab4c31 AC |
5028 | gnu_actual = maybe_unconstrained_array (gnu_actual); |
5029 | ||
a1ab4c31 AC |
5030 | /* Take the address of the object and convert to the proper pointer |
5031 | type. We'd like to actually compute the address of the beginning | |
5032 | of the array using an ADDR_EXPR of an ARRAY_REF, but there's a | |
5033 | possibility that the ARRAY_REF might return a constant and we'd be | |
5034 | getting the wrong address. Neither approach is exactly correct, | |
5035 | but this is the most likely to work in all cases. */ | |
6e03839f | 5036 | gnu_formal_type = TREE_TYPE (gnu_formal); |
0b3467c4 | 5037 | gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); |
a1ab4c31 | 5038 | } |
96540b24 | 5039 | |
c95f808d EB |
5040 | /* Then see if the parameter is passed by copy. */ |
5041 | else if (is_true_formal_parm) | |
a1ab4c31 | 5042 | { |
6e03839f | 5043 | if (!in_param) |
a1ab4c31 AC |
5044 | gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list); |
5045 | ||
0b3467c4 EB |
5046 | gnu_actual = convert (gnu_formal_type, gnu_actual); |
5047 | ||
b9778c45 EB |
5048 | /* If this is a front-end built-in function, there is no need to |
5049 | convert to the type used to pass the argument. */ | |
ad00a297 | 5050 | if (!frontend_builtin) |
a1ab4c31 AC |
5051 | gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual); |
5052 | } | |
5053 | ||
c95f808d EB |
5054 | /* Then see if this is an unnamed parameter in a variadic C function. */ |
5055 | else if (variadic) | |
5056 | { | |
5057 | /* This is based on the processing done in gnat_to_gnu_param, but | |
5058 | we expect the mechanism to be set in (almost) all cases. */ | |
5059 | const Mechanism_Type mech = Mechanism (gnat_formal); | |
5060 | ||
5061 | /* Strip off possible padding type. */ | |
5062 | if (TYPE_IS_PADDING_P (gnu_formal_type)) | |
5063 | gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type)); | |
5064 | ||
5065 | /* Arrays are passed as pointers to element type. First check for | |
5066 | unconstrained array and get the underlying array. */ | |
5067 | if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) | |
5068 | gnu_formal_type | |
5069 | = TREE_TYPE | |
5070 | (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_formal_type)))); | |
5071 | ||
5072 | /* Arrays are passed as pointers to element type. */ | |
5073 | if (mech != By_Copy && TREE_CODE (gnu_formal_type) == ARRAY_TYPE) | |
5074 | { | |
bb24f343 | 5075 | gnu_actual = maybe_padded_object (gnu_actual); |
c95f808d EB |
5076 | gnu_actual = maybe_unconstrained_array (gnu_actual); |
5077 | ||
5078 | /* Strip off any multi-dimensional entries, then strip | |
5079 | off the last array to get the component type. */ | |
5080 | while (TREE_CODE (TREE_TYPE (gnu_formal_type)) == ARRAY_TYPE | |
5081 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_formal_type))) | |
5082 | gnu_formal_type = TREE_TYPE (gnu_formal_type); | |
5083 | ||
5084 | gnu_formal_type = TREE_TYPE (gnu_formal_type); | |
5085 | gnu_formal_type = build_pointer_type (gnu_formal_type); | |
5086 | gnu_actual | |
5087 | = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); | |
5088 | } | |
5089 | ||
5090 | /* Fat pointers are passed as thin pointers. */ | |
5091 | else if (TYPE_IS_FAT_POINTER_P (gnu_formal_type)) | |
5092 | gnu_formal_type | |
5093 | = make_type_from_size (gnu_formal_type, | |
5094 | size_int (POINTER_SIZE), 0); | |
5095 | ||
5096 | /* If we were requested or muss pass by reference, do so. | |
5097 | If we were requested to pass by copy, do so. | |
5098 | Otherwise, pass In Out or Out parameters or aggregates by | |
5099 | reference. */ | |
5100 | else if (mech == By_Reference | |
5101 | || must_pass_by_ref (gnu_formal_type) | |
5102 | || (mech != By_Copy | |
5103 | && (!in_param || AGGREGATE_TYPE_P (gnu_formal_type)))) | |
5104 | { | |
5105 | gnu_formal_type = build_reference_type (gnu_formal_type); | |
5106 | gnu_actual | |
5107 | = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); | |
5108 | } | |
5109 | ||
5110 | /* Otherwise pass by copy after applying default C promotions. */ | |
5111 | else | |
5112 | { | |
5113 | if (INTEGRAL_TYPE_P (gnu_formal_type) | |
5114 | && TYPE_PRECISION (gnu_formal_type) | |
5115 | < TYPE_PRECISION (integer_type_node)) | |
5116 | gnu_formal_type = integer_type_node; | |
5117 | ||
5118 | else if (SCALAR_FLOAT_TYPE_P (gnu_formal_type) | |
5119 | && TYPE_PRECISION (gnu_formal_type) | |
5120 | < TYPE_PRECISION (double_type_node)) | |
5121 | gnu_formal_type = double_type_node; | |
5122 | } | |
5123 | ||
5124 | gnu_actual = convert (gnu_formal_type, gnu_actual); | |
5125 | } | |
5126 | ||
5127 | /* If we didn't create a PARM_DECL for the formal, this means that | |
5128 | it is an Out parameter not passed by reference and that need not | |
5129 | be copied in. In this case, the value of the actual need not be | |
5130 | read. However, we still need to make sure that its side-effects | |
5131 | are evaluated before the call, so we evaluate its address. */ | |
5132 | else | |
5133 | { | |
5134 | if (!in_param) | |
5135 | gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list); | |
5136 | ||
5137 | if (TREE_SIDE_EFFECTS (gnu_name)) | |
5138 | { | |
5139 | tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name); | |
5140 | append_to_statement_list (addr, &gnu_stmt_list); | |
5141 | } | |
5142 | ||
5143 | continue; | |
5144 | } | |
5145 | ||
b9778c45 EB |
5146 | gnu_actual_vec.safe_push (gnu_actual); |
5147 | } | |
5148 | ||
5149 | if (frontend_builtin) | |
5150 | { | |
5151 | tree pred_cst = build_int_cst (integer_type_node, PRED_BUILTIN_EXPECT); | |
5152 | enum internal_fn icode = IFN_BUILTIN_EXPECT; | |
5153 | ||
4d732405 | 5154 | switch (DECL_FE_FUNCTION_CODE (gnu_subprog)) |
b9778c45 EB |
5155 | { |
5156 | case BUILT_IN_EXPECT: | |
5157 | break; | |
5158 | case BUILT_IN_LIKELY: | |
5159 | gnu_actual_vec.safe_push (boolean_true_node); | |
5160 | break; | |
5161 | case BUILT_IN_UNLIKELY: | |
5162 | gnu_actual_vec.safe_push (boolean_false_node); | |
5163 | break; | |
5164 | default: | |
5165 | gcc_unreachable (); | |
5166 | } | |
5167 | ||
5168 | gnu_actual_vec.safe_push (pred_cst); | |
5169 | ||
5170 | gnu_call | |
5171 | = build_call_expr_internal_loc_array (UNKNOWN_LOCATION, | |
5172 | icode, | |
5173 | gnu_result_type, | |
5174 | gnu_actual_vec.length (), | |
5175 | gnu_actual_vec.begin ()); | |
5176 | } | |
5177 | else | |
5178 | { | |
5179 | gnu_call | |
5180 | = build_call_array_loc (UNKNOWN_LOCATION, | |
5181 | gnu_result_type, | |
5182 | build_unary_op (ADDR_EXPR, NULL_TREE, | |
5183 | gnu_subprog), | |
5184 | gnu_actual_vec.length (), | |
5185 | gnu_actual_vec.begin ()); | |
5186 | CALL_EXPR_BY_DESCRIPTOR (gnu_call) = by_descriptor; | |
a1ab4c31 AC |
5187 | } |
5188 | ||
ced57283 | 5189 | set_expr_location_from_node (gnu_call, gnat_node); |
a1ab4c31 | 5190 | |
ddb5a105 EB |
5191 | /* If we have created a temporary for the return value, initialize it. */ |
5192 | if (gnu_retval) | |
5193 | { | |
5194 | tree gnu_stmt | |
5195 | = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call); | |
5196 | set_expr_location_from_node (gnu_stmt, gnat_node); | |
5197 | append_to_statement_list (gnu_stmt, &gnu_stmt_list); | |
5198 | gnu_call = gnu_retval; | |
5199 | } | |
5200 | ||
35a382b8 EB |
5201 | /* If this is a subprogram with copy-in/copy-out parameters, we need to |
5202 | unpack the valued returned from the function into the In Out or Out | |
5203 | parameters. We deal with the function return (if this is an Ada | |
5204 | function) below. */ | |
d47d0a8d | 5205 | if (TYPE_CI_CO_LIST (gnu_subprog_type)) |
a1ab4c31 | 5206 | { |
0b3467c4 EB |
5207 | /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/ |
5208 | copy-out parameters. */ | |
a09d56d8 EB |
5209 | tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type); |
5210 | const int length = list_length (gnu_cico_list); | |
a1ab4c31 | 5211 | |
35a382b8 EB |
5212 | /* The call sequence must contain one and only one call, even though the |
5213 | function is pure. Save the result into a temporary if needed. */ | |
a1ab4c31 AC |
5214 | if (length > 1) |
5215 | { | |
ddb5a105 EB |
5216 | if (!gnu_retval) |
5217 | { | |
5218 | tree gnu_stmt; | |
ddb5a105 EB |
5219 | gnu_call |
5220 | = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node); | |
5221 | append_to_statement_list (gnu_stmt, &gnu_stmt_list); | |
5222 | } | |
0b3467c4 | 5223 | |
a1ab4c31 | 5224 | gnu_name_list = nreverse (gnu_name_list); |
a1ab4c31 AC |
5225 | } |
5226 | ||
35a382b8 EB |
5227 | /* The first entry is for the actual return value if this is a |
5228 | function, so skip it. */ | |
b83053bf | 5229 | if (function_call) |
35a382b8 EB |
5230 | gnu_cico_list = TREE_CHAIN (gnu_cico_list); |
5231 | ||
298339ef EB |
5232 | if (Nkind (gnat_subprog) == N_Explicit_Dereference) |
5233 | gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog)); | |
a1ab4c31 | 5234 | else |
298339ef | 5235 | gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog)); |
a1ab4c31 AC |
5236 | |
5237 | for (gnat_actual = First_Actual (gnat_node); | |
5238 | Present (gnat_actual); | |
5239 | gnat_formal = Next_Formal_With_Extras (gnat_formal), | |
5240 | gnat_actual = Next_Actual (gnat_actual)) | |
35a382b8 | 5241 | /* If we are dealing with a copy-in/copy-out parameter, we must |
a1ab4c31 AC |
5242 | retrieve its value from the record returned in the call. */ |
5243 | if (!(present_gnu_tree (gnat_formal) | |
5244 | && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL | |
5245 | && (DECL_BY_REF_P (get_gnu_tree (gnat_formal)) | |
1eb58520 | 5246 | || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal)))) |
a1ab4c31 AC |
5247 | && Ekind (gnat_formal) != E_In_Parameter) |
5248 | { | |
6e03839f | 5249 | /* Get the value to assign to this In Out or Out parameter. It is |
a1ab4c31 AC |
5250 | either the result of the function if there is only a single such |
5251 | parameter or the appropriate field from the record returned. */ | |
5252 | tree gnu_result | |
ced57283 EB |
5253 | = length == 1 |
5254 | ? gnu_call | |
64235766 EB |
5255 | : build_component_ref (gnu_call, TREE_PURPOSE (gnu_cico_list), |
5256 | false); | |
a1ab4c31 AC |
5257 | |
5258 | /* If the actual is a conversion, get the inner expression, which | |
5259 | will be the real destination, and convert the result to the | |
5260 | type of the actual parameter. */ | |
5261 | tree gnu_actual | |
5262 | = maybe_unconstrained_array (TREE_VALUE (gnu_name_list)); | |
5263 | ||
ad00a297 EB |
5264 | /* If the result is padded, remove the padding. */ |
5265 | gnu_result = maybe_padded_object (gnu_result); | |
a1ab4c31 AC |
5266 | |
5267 | /* If the actual is a type conversion, the real target object is | |
5268 | denoted by the inner Expression and we need to convert the | |
5269 | result to the associated type. | |
5270 | We also need to convert our gnu assignment target to this type | |
5271 | if the corresponding GNU_NAME was constructed from the GNAT | |
5272 | conversion node and not from the inner Expression. */ | |
5273 | if (Nkind (gnat_actual) == N_Type_Conversion) | |
5274 | { | |
815b5368 EB |
5275 | const Node_Id gnat_expr = Expression (gnat_actual); |
5276 | ||
5277 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); | |
5278 | ||
a1ab4c31 | 5279 | gnu_result |
815b5368 EB |
5280 | = convert_with_check (Etype (gnat_expr), gnu_result, |
5281 | Do_Overflow_Check (gnat_actual), | |
5282 | Float_Truncate (gnat_actual), | |
5283 | gnat_actual); | |
a1ab4c31 AC |
5284 | |
5285 | if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))) | |
5286 | gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual); | |
5287 | } | |
5288 | ||
5289 | /* Unchecked conversions as actuals for Out parameters are not | |
5290 | allowed in user code because they are not variables, but do | |
5291 | occur in front-end expansions. The associated GNU_NAME is | |
5292 | always obtained from the inner expression in such cases. */ | |
5293 | else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion) | |
5294 | gnu_result = unchecked_convert (TREE_TYPE (gnu_actual), | |
5295 | gnu_result, | |
5296 | No_Truncation (gnat_actual)); | |
5297 | else | |
5298 | { | |
815b5368 | 5299 | gigi_checking_assert (!Do_Range_Check (gnat_actual)); |
a1ab4c31 AC |
5300 | |
5301 | if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual))) | |
5302 | && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result))))) | |
5303 | gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result); | |
5304 | } | |
5305 | ||
17a98a3d EB |
5306 | get_atomic_access (gnat_actual, &aa_type, &aa_sync); |
5307 | ||
f797c2b7 EB |
5308 | /* If an outer atomic access is required for an actual parameter, |
5309 | build the load-modify-store sequence. */ | |
17a98a3d | 5310 | if (aa_type == OUTER_ATOMIC) |
f797c2b7 EB |
5311 | gnu_result |
5312 | = build_load_modify_store (gnu_actual, gnu_result, gnat_node); | |
5313 | ||
17a98a3d | 5314 | /* Or else, if a simple atomic access is required, build the atomic |
f797c2b7 | 5315 | store. */ |
17a98a3d EB |
5316 | else if (aa_type == SIMPLE_ATOMIC) |
5317 | gnu_result | |
5318 | = build_atomic_store (gnu_actual, gnu_result, aa_sync); | |
f797c2b7 EB |
5319 | |
5320 | /* Otherwise build a regular assignment. */ | |
033ba5bf EB |
5321 | else |
5322 | gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE, | |
5323 | gnu_actual, gnu_result); | |
f797c2b7 EB |
5324 | |
5325 | if (EXPR_P (gnu_result)) | |
5326 | set_expr_location_from_node (gnu_result, gnat_node); | |
ddb5a105 | 5327 | append_to_statement_list (gnu_result, &gnu_stmt_list); |
a09d56d8 | 5328 | gnu_cico_list = TREE_CHAIN (gnu_cico_list); |
a1ab4c31 AC |
5329 | gnu_name_list = TREE_CHAIN (gnu_name_list); |
5330 | } | |
ced57283 | 5331 | } |
35a382b8 EB |
5332 | |
5333 | /* If this is a function call, the result is the call expression unless a | |
5334 | target is specified, in which case we copy the result into the target | |
5335 | and return the assignment statement. */ | |
ddb5a105 | 5336 | if (function_call) |
35a382b8 | 5337 | { |
35a382b8 EB |
5338 | /* If this is a function with copy-in/copy-out parameters, extract the |
5339 | return value from it and update the return type. */ | |
5340 | if (TYPE_CI_CO_LIST (gnu_subprog_type)) | |
5341 | { | |
b83053bf | 5342 | tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type); |
64235766 EB |
5343 | gnu_call |
5344 | = build_component_ref (gnu_call, TREE_PURPOSE (gnu_elmt), false); | |
35a382b8 EB |
5345 | gnu_result_type = TREE_TYPE (gnu_call); |
5346 | } | |
5347 | ||
89e037d0 EB |
5348 | /* If the function returns by direct reference, we have to dereference |
5349 | the pointer. */ | |
5350 | if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)) | |
35a382b8 EB |
5351 | gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call); |
5352 | ||
5353 | if (gnu_target) | |
5354 | { | |
5355 | Node_Id gnat_parent = Parent (gnat_node); | |
5356 | enum tree_code op_code; | |
5357 | ||
815b5368 | 5358 | gigi_checking_assert (!Do_Range_Check (gnat_node)); |
35a382b8 | 5359 | |
ddb5a105 EB |
5360 | /* ??? If the return type has variable size, then force the return |
5361 | slot optimization as we would not be able to create a temporary. | |
ddb5a105 | 5362 | That's what has been done historically. */ |
fc7a823e | 5363 | if (return_type_with_variable_size_p (gnu_result_type)) |
35a382b8 | 5364 | op_code = INIT_EXPR; |
b81e2d5e EB |
5365 | |
5366 | /* If this is a call to a pure function returning an array of scalar | |
5367 | type, try to apply the return slot optimization. */ | |
5368 | else if ((TYPE_READONLY (gnu_subprog_type) | |
5369 | || TYPE_RESTRICT (gnu_subprog_type)) | |
5370 | && is_array_of_scalar_type (gnu_result_type) | |
5371 | && TYPE_MODE (gnu_result_type) == BLKmode | |
5372 | && aggregate_value_p (gnu_result_type, gnu_subprog_type) | |
5373 | && return_slot_opt_for_pure_call_p (gnu_target, gnu_call)) | |
5374 | op_code = INIT_EXPR; | |
5375 | ||
9aa9ba95 EB |
5376 | /* If this is the initialization of a return object in a function |
5377 | returning by invisible reference, we can always use the return | |
5378 | slot optimization. */ | |
5379 | else if (TREE_CODE (gnu_target) == INDIRECT_REF | |
5380 | && TREE_CODE (TREE_OPERAND (gnu_target, 0)) == RESULT_DECL | |
5381 | && current_function_decl | |
5382 | && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl))) | |
5383 | op_code = INIT_EXPR; | |
5384 | ||
35a382b8 EB |
5385 | else |
5386 | op_code = MODIFY_EXPR; | |
5387 | ||
f797c2b7 | 5388 | /* Use the required method to move the result to the target. */ |
17a98a3d | 5389 | if (atomic_access == OUTER_ATOMIC) |
f797c2b7 EB |
5390 | gnu_call |
5391 | = build_load_modify_store (gnu_target, gnu_call, gnat_node); | |
17a98a3d | 5392 | else if (atomic_access == SIMPLE_ATOMIC) |
f797c2b7 | 5393 | gnu_call = build_atomic_store (gnu_target, gnu_call, atomic_sync); |
2d4883a1 EB |
5394 | else if (Present (gnat_smo) |
5395 | && Present (Storage_Model_Copy_To (gnat_smo))) | |
544d14e1 | 5396 | gnu_call |
2d4883a1 | 5397 | = build_storage_model_store (gnat_smo, gnu_target, gnu_call); |
033ba5bf EB |
5398 | else |
5399 | gnu_call | |
5400 | = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call); | |
f797c2b7 EB |
5401 | |
5402 | if (EXPR_P (gnu_call)) | |
5403 | set_expr_location_from_node (gnu_call, gnat_parent); | |
ddb5a105 | 5404 | append_to_statement_list (gnu_call, &gnu_stmt_list); |
35a382b8 EB |
5405 | } |
5406 | else | |
5407 | *gnu_result_type_p = get_unpadded_type (Etype (gnat_node)); | |
5408 | } | |
5409 | ||
5410 | /* Otherwise, if this is a procedure call statement without copy-in/copy-out | |
5411 | parameters, the result is just the call statement. */ | |
5412 | else if (!TYPE_CI_CO_LIST (gnu_subprog_type)) | |
ddb5a105 EB |
5413 | append_to_statement_list (gnu_call, &gnu_stmt_list); |
5414 | ||
5415 | /* Finally, add the copy back statements, if any. */ | |
5416 | append_to_statement_list (gnu_after_list, &gnu_stmt_list); | |
a1ab4c31 | 5417 | |
35a382b8 EB |
5418 | if (went_into_elab_proc) |
5419 | current_function_decl = NULL_TREE; | |
ced57283 | 5420 | |
ddb5a105 EB |
5421 | /* If we have pushed a binding level, pop it and finish up the enclosing |
5422 | statement group. */ | |
35a382b8 EB |
5423 | if (pushed_binding_level) |
5424 | { | |
ddb5a105 | 5425 | add_stmt (gnu_stmt_list); |
35a382b8 EB |
5426 | gnat_poplevel (); |
5427 | gnu_result = end_stmt_group (); | |
5428 | } | |
ddb5a105 EB |
5429 | |
5430 | /* Otherwise, retrieve the statement list, if any. */ | |
5431 | else if (gnu_stmt_list) | |
5432 | gnu_result = gnu_stmt_list; | |
5433 | ||
5434 | /* Otherwise, just return the call expression. */ | |
35a382b8 EB |
5435 | else |
5436 | return gnu_call; | |
5437 | ||
71196d4e EB |
5438 | /* If we nevertheless need a value, make a COMPOUND_EXPR to return it. |
5439 | But first simplify if we have only one statement in the list. */ | |
35a382b8 | 5440 | if (returning_value) |
71196d4e EB |
5441 | { |
5442 | tree first = expr_first (gnu_result), last = expr_last (gnu_result); | |
5443 | if (first == last) | |
5444 | gnu_result = first; | |
5445 | gnu_result | |
5446 | = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call); | |
5447 | } | |
35a382b8 EB |
5448 | |
5449 | return gnu_result; | |
a1ab4c31 | 5450 | } |
ce2d0ce2 | 5451 | |
e08f1aad | 5452 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an |
a1ab4c31 AC |
5453 | N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */ |
5454 | ||
5455 | static tree | |
5456 | Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node) | |
5457 | { | |
a1ab4c31 | 5458 | /* If just annotating, ignore all EH and cleanups. */ |
6d16658d | 5459 | const bool eh |
ce19ac12 | 5460 | = !type_annotate_only && Present (Exception_Handlers (gnat_node)); |
736e16ef | 5461 | const bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node)); |
a1ab4c31 | 5462 | tree gnu_result; |
a1ab4c31 AC |
5463 | Node_Id gnat_temp; |
5464 | ||
6d16658d BD |
5465 | /* The exception handling mechanism can handle both ZCX and SJLJ schemes, and |
5466 | is exposed through the TRY_CATCH_EXPR construct that we build manually. | |
a1ab4c31 AC |
5467 | |
5468 | ??? The region level calls down there have been specifically put in place | |
5469 | for a ZCX context and currently the order in which things are emitted | |
736e16ef | 5470 | (region/handlers) is different from the SJLJ case. Instead of putting |
a1ab4c31 AC |
5471 | other calls with different conditions at other places for the SJLJ case, |
5472 | it seems cleaner to reorder things for the SJLJ case and generalize the | |
6d16658d | 5473 | condition to make it not ZCX specific. */ |
a1ab4c31 | 5474 | |
6d16658d | 5475 | /* First build the tree for the statements inside the sequence. */ |
a1ab4c31 AC |
5476 | start_stmt_group (); |
5477 | ||
6d16658d BD |
5478 | for (gnat_temp = First (Statements (gnat_node)); |
5479 | Present (gnat_temp); | |
5480 | gnat_temp = Next (gnat_temp)) | |
a1ab4c31 | 5481 | add_stmt (gnat_to_gnu (gnat_temp)); |
736e16ef | 5482 | |
6d16658d | 5483 | gnu_result = end_stmt_group (); |
a1ab4c31 | 5484 | |
6d16658d BD |
5485 | /* Then process the exception handlers, if any. */ |
5486 | if (eh) | |
a1ab4c31 AC |
5487 | { |
5488 | tree gnu_handlers; | |
678400b3 | 5489 | location_t locus; |
a1ab4c31 | 5490 | |
6d16658d | 5491 | /* First make a group containing the handlers. */ |
a1ab4c31 AC |
5492 | start_stmt_group (); |
5493 | for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node)); | |
5494 | Present (gnat_temp); | |
5495 | gnat_temp = Next_Non_Pragma (gnat_temp)) | |
5496 | add_stmt (gnat_to_gnu (gnat_temp)); | |
5497 | gnu_handlers = end_stmt_group (); | |
5498 | ||
6d16658d BD |
5499 | /* Now make the TRY_CATCH_EXPR for the group. */ |
5500 | gnu_result | |
5501 | = build2 (TRY_CATCH_EXPR, void_type_node, gnu_result, gnu_handlers); | |
5502 | ||
362db0b2 | 5503 | /* Set a location. We need to find a unique location for the dispatching |
678400b3 AC |
5504 | code, otherwise we can get coverage or debugging issues. Try with |
5505 | the location of the end label. */ | |
5506 | if (Present (End_Label (gnat_node)) | |
5507 | && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus)) | |
5508 | SET_EXPR_LOCATION (gnu_result, locus); | |
5509 | else | |
362db0b2 TQ |
5510 | /* Clear column information so that the exception handler of an |
5511 | implicit transient block does not incorrectly inherit the slocs | |
5512 | of a decision, which would otherwise confuse control flow based | |
5513 | coverage analysis tools. */ | |
ba464315 | 5514 | set_expr_location_from_node (gnu_result, gnat_node, true); |
a1ab4c31 | 5515 | } |
a1ab4c31 | 5516 | |
6d16658d BD |
5517 | /* Process the At_End_Proc, if any. */ |
5518 | if (at_end) | |
a1ab4c31 | 5519 | { |
6d16658d | 5520 | start_stmt_group (); |
a1ab4c31 | 5521 | add_stmt (gnu_result); |
6d16658d | 5522 | At_End_Proc_to_gnu (gnat_node); |
a1ab4c31 AC |
5523 | gnu_result = end_stmt_group (); |
5524 | } | |
5525 | ||
5526 | return gnu_result; | |
5527 | } | |
ce2d0ce2 | 5528 | |
4ec7c4ec EB |
5529 | /* Return true if no statement in GNAT_LIST can alter the control flow. */ |
5530 | ||
5531 | static bool | |
5532 | stmt_list_cannot_alter_control_flow_p (List_Id gnat_list) | |
5533 | { | |
5534 | if (No (gnat_list)) | |
5535 | return true; | |
5536 | ||
5537 | /* This is very conservative, we reject everything except for simple | |
5538 | assignments between identifiers or literals. */ | |
5539 | for (Node_Id gnat_node = First (gnat_list); | |
5540 | Present (gnat_node); | |
5541 | gnat_node = Next (gnat_node)) | |
5542 | { | |
5543 | if (Nkind (gnat_node) != N_Assignment_Statement) | |
5544 | return false; | |
5545 | ||
5546 | if (Nkind (Name (gnat_node)) != N_Identifier) | |
5547 | return false; | |
5548 | ||
5549 | Node_Kind nkind = Nkind (Expression (gnat_node)); | |
5550 | if (nkind != N_Identifier | |
5551 | && nkind != N_Integer_Literal | |
5552 | && nkind != N_Real_Literal) | |
5553 | return false; | |
5554 | } | |
5555 | ||
5556 | return true; | |
5557 | } | |
5558 | ||
e08f1aad EB |
5559 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Exception_Handler, |
5560 | to a GCC tree, which is returned. */ | |
a1ab4c31 AC |
5561 | |
5562 | static tree | |
e08f1aad | 5563 | Exception_Handler_to_gnu (Node_Id gnat_node) |
a1ab4c31 AC |
5564 | { |
5565 | tree gnu_etypes_list = NULL_TREE; | |
a1ab4c31 AC |
5566 | |
5567 | /* We build a TREE_LIST of nodes representing what exception types this | |
5568 | handler can catch, with special cases for others and all others cases. | |
5569 | ||
5570 | Each exception type is actually identified by a pointer to the exception | |
1a710808 | 5571 | id, or to a dummy object for "others" and "all others". */ |
4ec7c4ec EB |
5572 | for (Node_Id gnat_temp = First (Exception_Choices (gnat_node)); |
5573 | gnat_temp; | |
5574 | gnat_temp = Next (gnat_temp)) | |
a1ab4c31 | 5575 | { |
fd6e497e EB |
5576 | tree gnu_expr, gnu_etype; |
5577 | ||
a1ab4c31 AC |
5578 | if (Nkind (gnat_temp) == N_Others_Choice) |
5579 | { | |
fd6e497e EB |
5580 | gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl; |
5581 | gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); | |
a1ab4c31 AC |
5582 | } |
5583 | else if (Nkind (gnat_temp) == N_Identifier | |
5584 | || Nkind (gnat_temp) == N_Expanded_Name) | |
5585 | { | |
5586 | Entity_Id gnat_ex_id = Entity (gnat_temp); | |
5587 | ||
fd6e497e | 5588 | /* Exception may be a renaming. Recover original exception which is |
a1ab4c31 AC |
5589 | the one elaborated and registered. */ |
5590 | if (Present (Renamed_Object (gnat_ex_id))) | |
5591 | gnat_ex_id = Renamed_Object (gnat_ex_id); | |
5592 | ||
afc737f0 | 5593 | gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false); |
a1ab4c31 | 5594 | gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); |
a1ab4c31 AC |
5595 | } |
5596 | else | |
5597 | gcc_unreachable (); | |
5598 | ||
5599 | /* The GCC interface expects NULL to be passed for catch all handlers, so | |
5600 | it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype | |
5601 | is integer_zero_node. It would not work, however, because GCC's | |
5602 | notion of "catch all" is stronger than our notion of "others". Until | |
5603 | we correctly use the cleanup interface as well, doing that would | |
5604 | prevent the "all others" handlers from being seen, because nothing | |
5605 | can be caught beyond a catch all from GCC's point of view. */ | |
5606 | gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list); | |
5607 | } | |
5608 | ||
5609 | start_stmt_group (); | |
a1ab4c31 | 5610 | |
5d733372 AO |
5611 | /* Expand a call to the begin_handler hook at the beginning of the |
5612 | handler, and arrange for a call to the end_handler hook to occur | |
5613 | on every possible exit path. GDB sets a breakpoint in the | |
5614 | begin_handler for catchpoints. | |
a1ab4c31 | 5615 | |
5d733372 AO |
5616 | A v1 begin handler saves the cleanup from the exception object, |
5617 | and marks the exception as in use, so that it will not be | |
5618 | released by other handlers. A v1 end handler restores the | |
5619 | cleanup and releases the exception object, unless it is still | |
5620 | claimed, or the exception is being propagated (reraised). | |
a1ab4c31 | 5621 | |
1d65f45c | 5622 | __builtin_eh_pointer references the exception occurrence being |
5d733372 AO |
5623 | handled or propagated. Within the handler region, it is the |
5624 | former, but within the else branch of the EH_ELSE_EXPR, i.e. the | |
5625 | exceptional cleanup path, it is the latter, so we must save the | |
5626 | occurrence being handled early on, so that, should an exception | |
5627 | be (re)raised, we can release the current exception, or figure | |
5628 | out we're not to release it because we're propagating a reraise | |
5629 | thereof. | |
5630 | ||
5631 | We use local variables to retrieve the incoming value at handler | |
5632 | entry time (EXPTR), the saved cleanup (EXCLN) and the token | |
5633 | (EXVTK), and reuse them to feed the end_handler hook's argument | |
5634 | at exit. */ | |
5635 | ||
5636 | /* CODE: void *EXPTR = __builtin_eh_pointer (0); */ | |
4ec7c4ec | 5637 | tree gnu_current_exc_ptr |
e79983f4 | 5638 | = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), |
1d65f45c | 5639 | 1, integer_zero_node); |
5d733372 | 5640 | tree exc_ptr |
c1a569ef EB |
5641 | = create_var_decl (get_identifier ("EXPTR"), NULL_TREE, |
5642 | ptr_type_node, gnu_current_exc_ptr, | |
5d733372 | 5643 | true, false, false, false, false, true, true, |
c1a569ef | 5644 | NULL, gnat_node); |
a1ab4c31 | 5645 | |
5d733372 AO |
5646 | tree prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr; |
5647 | gnu_incoming_exc_ptr = exc_ptr; | |
5648 | ||
5649 | /* begin_handler_decl must not throw, so we can use it as an | |
5650 | initializer for a variable used in cleanups. | |
5651 | ||
5652 | CODE: void *EXCLN = __gnat_begin_handler_v1 (EXPTR); */ | |
5653 | tree exc_cleanup | |
5654 | = create_var_decl (get_identifier ("EXCLN"), NULL_TREE, | |
5655 | ptr_type_node, | |
5656 | build_call_n_expr (begin_handler_decl, 1, | |
5657 | exc_ptr), | |
5658 | true, false, false, false, false, | |
5659 | true, true, NULL, gnat_node); | |
811189d9 AC |
5660 | |
5661 | /* Declare and initialize the choice parameter, if present. */ | |
5662 | if (Present (Choice_Parameter (gnat_node))) | |
5663 | { | |
fd6e497e | 5664 | tree gnu_param |
afc737f0 | 5665 | = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, true); |
811189d9 | 5666 | |
5d733372 | 5667 | /* CODE: __gnat_set_exception_parameter (&choice_param, EXPTR); */ |
811189d9 AC |
5668 | add_stmt (build_call_n_expr |
5669 | (set_exception_parameter_decl, 2, | |
5670 | build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param), | |
5671 | gnu_incoming_exc_ptr)); | |
5672 | } | |
5673 | ||
5d733372 | 5674 | /* CODE: <handler proper> */ |
4ec7c4ec EB |
5675 | add_stmt_list (Statements (gnat_node)); |
5676 | ||
5d733372 AO |
5677 | tree call = build_call_n_expr (end_handler_decl, 3, |
5678 | exc_ptr, | |
5679 | exc_cleanup, | |
5680 | null_pointer_node); | |
5681 | /* If the handler can only end by falling off the end, don't bother | |
5682 | with cleanups. */ | |
4ec7c4ec | 5683 | if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node))) |
5d733372 AO |
5684 | /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, NULL); */ |
5685 | add_stmt_with_node (call, gnat_node); | |
5686 | /* Otherwise, all of the above is after | |
5687 | CODE: try { | |
5688 | ||
5689 | The call above will appear after | |
5690 | CODE: } finally { | |
5691 | ||
5692 | And the code below will appear after | |
5693 | CODE: } else { | |
5694 | ||
5695 | The else block to a finally block is taken instead of the finally | |
5696 | block when an exception propagates out of the try block. */ | |
4ec7c4ec | 5697 | else |
5d733372 AO |
5698 | { |
5699 | start_stmt_group (); | |
6d16658d | 5700 | |
5d733372 AO |
5701 | /* CODE: void *EXPRP = __builtin_eh_handler (0); */ |
5702 | tree prop_ptr | |
5703 | = create_var_decl (get_identifier ("EXPRP"), NULL_TREE, | |
5704 | ptr_type_node, | |
5705 | build_call_expr (builtin_decl_explicit | |
5706 | (BUILT_IN_EH_POINTER), | |
5707 | 1, integer_zero_node), | |
5708 | true, false, false, false, false, | |
5709 | true, true, NULL, gnat_node); | |
5710 | ||
5711 | /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, EXPRP); */ | |
5712 | tree ecall = build_call_n_expr (end_handler_decl, 3, | |
5713 | exc_ptr, | |
5714 | exc_cleanup, | |
5715 | prop_ptr); | |
5716 | ||
5717 | add_stmt_with_node (ecall, gnat_node); | |
5718 | ||
5719 | /* CODE: } */ | |
5d733372 AO |
5720 | tree eblk = end_stmt_group (); |
5721 | tree ehls = build2 (EH_ELSE_EXPR, void_type_node, call, eblk); | |
5722 | add_cleanup (ehls, gnat_node); | |
5723 | } | |
4ec7c4ec | 5724 | |
624e1688 AC |
5725 | gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr; |
5726 | ||
fd6e497e EB |
5727 | return |
5728 | build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ()); | |
a1ab4c31 | 5729 | } |
ce2d0ce2 | 5730 | |
e08f1aad | 5731 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Compilation_Unit. */ |
a1ab4c31 AC |
5732 | |
5733 | static void | |
5734 | Compilation_Unit_to_gnu (Node_Id gnat_node) | |
5735 | { | |
58c8f770 EB |
5736 | const Node_Id gnat_unit = Unit (gnat_node); |
5737 | const bool body_p = (Nkind (gnat_unit) == N_Package_Body | |
5738 | || Nkind (gnat_unit) == N_Subprogram_Body); | |
5739 | const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit); | |
384e3fb1 | 5740 | Entity_Id gnat_entity; |
ec4a0d83 | 5741 | Node_Id gnat_pragma, gnat_iter; |
ff9baa5f PMR |
5742 | /* Make the decl for the elaboration procedure. Emit debug info for it, so |
5743 | that users can break into their elaboration code in debuggers. Kludge: | |
19d9ce46 OH |
5744 | don't consider it as a definition so that we have a line map for its |
5745 | body, but no subprogram description in debug info. In addition, don't | |
5746 | qualify it as artificial, even though it is not a user subprogram per se, | |
5747 | in particular for specs. Unlike, say, clones created internally by the | |
5748 | compiler, this subprogram materializes specific user code and flagging it | |
5749 | artificial would take elab code away from gcov's analysis. */ | |
a1ab4c31 AC |
5750 | tree gnu_elab_proc_decl |
5751 | = create_subprog_decl | |
58c8f770 | 5752 | (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"), |
2056c5ed | 5753 | NULL_TREE, void_ftype, NULL_TREE, |
19d9ce46 | 5754 | is_default, true, false, false, true, false, NULL, gnat_unit); |
a1ab4c31 AC |
5755 | struct elab_info *info; |
5756 | ||
9771b263 | 5757 | vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl); |
a1ab4c31 | 5758 | DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1; |
58c8f770 EB |
5759 | |
5760 | /* Initialize the information structure for the function. */ | |
a1ab4c31 | 5761 | allocate_struct_function (gnu_elab_proc_decl, false); |
a1ab4c31 | 5762 | set_cfun (NULL); |
58c8f770 EB |
5763 | |
5764 | current_function_decl = NULL_TREE; | |
5765 | ||
a09d56d8 EB |
5766 | start_stmt_group (); |
5767 | gnat_pushlevel (); | |
a1ab4c31 | 5768 | |
1e17ef87 | 5769 | /* For a body, first process the spec if there is one. */ |
6ddf9843 EB |
5770 | if (Nkind (gnat_unit) == N_Package_Body |
5771 | || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node))) | |
5772 | add_stmt (gnat_to_gnu (Library_Unit (gnat_node))); | |
a1ab4c31 | 5773 | |
5daed84a EB |
5774 | if (type_annotate_only && gnat_node == Cunit (Main_Unit)) |
5775 | { | |
5776 | elaborate_all_entities (gnat_node); | |
5777 | ||
6ddf9843 EB |
5778 | if (Nkind (gnat_unit) == N_Subprogram_Declaration |
5779 | || Nkind (gnat_unit) == N_Generic_Package_Declaration | |
5780 | || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration) | |
5daed84a EB |
5781 | return; |
5782 | } | |
5783 | ||
257e81a6 EB |
5784 | /* Then process any pragmas and declarations preceding the unit. */ |
5785 | for (gnat_pragma = First (Context_Items (gnat_node)); | |
5786 | Present (gnat_pragma); | |
5787 | gnat_pragma = Next (gnat_pragma)) | |
5788 | if (Nkind (gnat_pragma) == N_Pragma) | |
5789 | add_stmt (gnat_to_gnu (gnat_pragma)); | |
6d16658d | 5790 | process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, |
5daed84a | 5791 | true, true); |
257e81a6 EB |
5792 | |
5793 | /* Process the unit itself. */ | |
6ddf9843 | 5794 | add_stmt (gnat_to_gnu (gnat_unit)); |
5daed84a | 5795 | |
384e3fb1 JM |
5796 | /* Generate code for all the inlined subprograms. */ |
5797 | for (gnat_entity = First_Inlined_Subprogram (gnat_node); | |
5798 | Present (gnat_entity); | |
5799 | gnat_entity = Next_Inlined_Subprogram (gnat_entity)) | |
58c8f770 | 5800 | { |
384e3fb1 | 5801 | Node_Id gnat_body; |
58c8f770 | 5802 | |
384e3fb1 JM |
5803 | /* Without optimization, process only the required subprograms. */ |
5804 | if (!optimize && !Has_Pragma_Inline_Always (gnat_entity)) | |
5805 | continue; | |
58c8f770 | 5806 | |
0c09a01e EB |
5807 | /* The set of inlined subprograms is computed from data recorded early |
5808 | during expansion and it can be a strict superset of the final set | |
5809 | computed after semantic analysis, for example if a call to such a | |
5810 | subprogram occurs in a pragma Assert and assertions are disabled. | |
5811 | In that case, semantic analysis resets Is_Public to false but the | |
5812 | entry for the subprogram in the inlining tables is stalled. */ | |
5813 | if (!Is_Public (gnat_entity)) | |
5814 | continue; | |
5815 | ||
384e3fb1 JM |
5816 | gnat_body = Parent (Declaration_Node (gnat_entity)); |
5817 | if (Nkind (gnat_body) != N_Subprogram_Body) | |
5818 | { | |
5819 | /* ??? This happens when only the spec of a package is provided. */ | |
5820 | if (No (Corresponding_Body (gnat_body))) | |
5821 | continue; | |
58c8f770 | 5822 | |
384e3fb1 JM |
5823 | gnat_body |
5824 | = Parent (Declaration_Node (Corresponding_Body (gnat_body))); | |
58c8f770 | 5825 | } |
384e3fb1 JM |
5826 | |
5827 | /* Define the entity first so we set DECL_EXTERNAL. */ | |
afc737f0 | 5828 | gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
384e3fb1 | 5829 | add_stmt (gnat_to_gnu (gnat_body)); |
58c8f770 | 5830 | } |
a1ab4c31 | 5831 | |
a1ab4c31 AC |
5832 | /* Process any pragmas and actions following the unit. */ |
5833 | add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node))); | |
5834 | add_stmt_list (Actions (Aux_Decls_Node (gnat_node))); | |
7b56a91b | 5835 | finalize_from_limited_with (); |
a1ab4c31 | 5836 | |
ec4a0d83 EB |
5837 | /* Then process the expressions of pragma Compile_Time_{Error|Warning} to |
5838 | annotate types referenced therein if they have not been annotated. */ | |
5839 | for (int i = 0; gnat_compile_time_expr_list.iterate (i, &gnat_iter); i++) | |
5840 | (void) gnat_to_gnu_external (gnat_iter); | |
5841 | gnat_compile_time_expr_list.release (); | |
5842 | ||
ea292448 | 5843 | /* Save away what we've made so far and finish it up. */ |
a1ab4c31 AC |
5844 | set_current_block_context (gnu_elab_proc_decl); |
5845 | gnat_poplevel (); | |
5846 | DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group (); | |
2a02d090 | 5847 | set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit); |
ea292448 | 5848 | gnu_elab_proc_stack->pop (); |
58c8f770 | 5849 | |
ea292448 EB |
5850 | /* Record this potential elaboration procedure for later processing. */ |
5851 | info = ggc_alloc<elab_info> (); | |
a1ab4c31 AC |
5852 | info->next = elab_info_list; |
5853 | info->elab_proc = gnu_elab_proc_decl; | |
5854 | info->gnat_node = gnat_node; | |
5855 | elab_info_list = info; | |
5856 | ||
9a30c7c4 AC |
5857 | /* Force the processing for all nodes that remain in the queue. */ |
5858 | process_deferred_decl_context (true); | |
a1ab4c31 | 5859 | } |
ce2d0ce2 | 5860 | |
64235766 EB |
5861 | /* Mark COND, a boolean expression, as predicating a call to a noreturn |
5862 | function, i.e. predict that it is very likely false, and return it. | |
5863 | ||
5864 | The compiler will automatically predict the last edge leading to a call | |
5865 | to a noreturn function as very unlikely taken. This function makes it | |
b9778c45 | 5866 | possible to extend the prediction to predecessors in case the condition |
64235766 EB |
5867 | is made up of several short-circuit operators. */ |
5868 | ||
5869 | static tree | |
5870 | build_noreturn_cond (tree cond) | |
5871 | { | |
b9778c45 EB |
5872 | tree pred_cst = build_int_cst (integer_type_node, PRED_NORETURN); |
5873 | return | |
5874 | build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_BUILTIN_EXPECT, | |
5875 | boolean_type_node, 3, cond, | |
5876 | boolean_false_node, pred_cst); | |
64235766 EB |
5877 | } |
5878 | ||
3eaec285 EB |
5879 | /* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a |
5880 | range of values, into GNU_LOW and GNU_HIGH bounds. */ | |
5881 | ||
5882 | static void | |
5883 | Range_to_gnu (Node_Id gnat_range, tree *gnu_low, tree *gnu_high) | |
5884 | { | |
5885 | /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */ | |
5886 | switch (Nkind (gnat_range)) | |
5887 | { | |
5888 | case N_Range: | |
5889 | *gnu_low = gnat_to_gnu (Low_Bound (gnat_range)); | |
5890 | *gnu_high = gnat_to_gnu (High_Bound (gnat_range)); | |
5891 | break; | |
5892 | ||
5893 | case N_Expanded_Name: | |
5894 | case N_Identifier: | |
5895 | { | |
5896 | tree gnu_range_type = get_unpadded_type (Entity (gnat_range)); | |
5897 | tree gnu_range_base_type = get_base_type (gnu_range_type); | |
5898 | ||
5899 | *gnu_low | |
5900 | = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type)); | |
5901 | *gnu_high | |
5902 | = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type)); | |
5903 | } | |
5904 | break; | |
5905 | ||
5906 | default: | |
5907 | gcc_unreachable (); | |
5908 | } | |
5909 | } | |
5910 | ||
9a1bdc31 | 5911 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error, |
e08f1aad EB |
5912 | to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to |
5913 | where we should place the result type. */ | |
80096613 EB |
5914 | |
5915 | static tree | |
5916 | Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p) | |
5917 | { | |
5918 | const Node_Kind kind = Nkind (gnat_node); | |
80096613 | 5919 | const Node_Id gnat_cond = Condition (gnat_node); |
c900c700 | 5920 | const int reason = UI_To_Int (Reason (gnat_node)); |
80096613 EB |
5921 | const bool with_extra_info |
5922 | = Exception_Extra_Info | |
5923 | && !No_Exception_Handlers_Set () | |
8f8f531f | 5924 | && No (get_exception_label (kind)); |
80096613 | 5925 | tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE; |
c900c700 | 5926 | Node_Id gnat_rcond; |
80096613 | 5927 | |
9a1bdc31 EB |
5928 | /* The following processing is not required for correctness. Its purpose is |
5929 | to give more precise error messages and to record some information. */ | |
80096613 EB |
5930 | switch (reason) |
5931 | { | |
5932 | case CE_Access_Check_Failed: | |
5933 | if (with_extra_info) | |
9a1bdc31 | 5934 | gnu_result = build_call_raise_column (reason, gnat_node, kind); |
80096613 EB |
5935 | break; |
5936 | ||
5937 | case CE_Index_Check_Failed: | |
5938 | case CE_Range_Check_Failed: | |
5939 | case CE_Invalid_Data: | |
c900c700 EB |
5940 | if (No (gnat_cond) || Nkind (gnat_cond) != N_Op_Not) |
5941 | break; | |
5942 | gnat_rcond = Right_Opnd (gnat_cond); | |
5943 | if (Nkind (gnat_rcond) == N_In | |
5944 | || Nkind (gnat_rcond) == N_Op_Ge | |
5945 | || Nkind (gnat_rcond) == N_Op_Le) | |
80096613 | 5946 | { |
c900c700 EB |
5947 | const Node_Id gnat_index = Left_Opnd (gnat_rcond); |
5948 | const Node_Id gnat_type = Etype (gnat_index); | |
5949 | tree gnu_index = gnat_to_gnu (gnat_index); | |
5950 | tree gnu_type = get_unpadded_type (gnat_type); | |
5951 | tree gnu_low_bound, gnu_high_bound, disp; | |
87ab2b04 | 5952 | struct loop_info_d *loop; |
c900c700 | 5953 | bool neg_p; |
80096613 | 5954 | |
c900c700 | 5955 | switch (Nkind (gnat_rcond)) |
80096613 EB |
5956 | { |
5957 | case N_In: | |
c900c700 | 5958 | Range_to_gnu (Right_Opnd (gnat_rcond), |
3eaec285 | 5959 | &gnu_low_bound, &gnu_high_bound); |
80096613 EB |
5960 | break; |
5961 | ||
5962 | case N_Op_Ge: | |
c900c700 EB |
5963 | gnu_low_bound = gnat_to_gnu (Right_Opnd (gnat_rcond)); |
5964 | gnu_high_bound = TYPE_MAX_VALUE (gnu_type); | |
80096613 EB |
5965 | break; |
5966 | ||
5967 | case N_Op_Le: | |
c900c700 EB |
5968 | gnu_low_bound = TYPE_MIN_VALUE (gnu_type); |
5969 | gnu_high_bound = gnat_to_gnu (Right_Opnd (gnat_rcond)); | |
80096613 EB |
5970 | break; |
5971 | ||
5972 | default: | |
c900c700 | 5973 | gcc_unreachable (); |
80096613 EB |
5974 | } |
5975 | ||
c900c700 | 5976 | gnu_type = maybe_character_type (gnu_type); |
825da0d2 EB |
5977 | if (TREE_TYPE (gnu_index) != gnu_type) |
5978 | { | |
c900c700 EB |
5979 | gnu_low_bound = convert (gnu_type, gnu_low_bound); |
5980 | gnu_high_bound = convert (gnu_type, gnu_high_bound); | |
825da0d2 EB |
5981 | gnu_index = convert (gnu_type, gnu_index); |
5982 | } | |
5983 | ||
80096613 | 5984 | if (with_extra_info |
80096613 EB |
5985 | && Known_Esize (gnat_type) |
5986 | && UI_To_Int (Esize (gnat_type)) <= 32) | |
5987 | gnu_result | |
9a1bdc31 | 5988 | = build_call_raise_range (reason, gnat_node, kind, gnu_index, |
80096613 EB |
5989 | gnu_low_bound, gnu_high_bound); |
5990 | ||
64235766 EB |
5991 | /* If optimization is enabled and we are inside a loop, we try to |
5992 | compute invariant conditions for checks applied to the iteration | |
5993 | variable, i.e. conditions that are independent of the variable | |
5994 | and necessary in order for the checks to fail in the course of | |
5995 | some iteration. If we succeed, we consider an alternative: | |
5996 | ||
5997 | 1. If loop unswitching is enabled, we prepend these conditions | |
5998 | to the original conditions of the checks. This will make it | |
5999 | possible for the loop unswitching pass to replace the loop | |
6000 | with two loops, one of which has the checks eliminated and | |
6001 | the other has the original checks reinstated, and a prologue | |
6002 | implementing a run-time selection. The former loop will be | |
6003 | for example suitable for vectorization. | |
6004 | ||
6005 | 2. Otherwise, we instead append the conditions to the original | |
6006 | conditions of the checks. At worse, if the conditions cannot | |
6007 | be evaluated at compile time, they will be evaluated as true | |
6008 | at run time only when the checks have already failed, thus | |
6009 | contributing negatively only to the size of the executable. | |
6010 | But the hope is that these invariant conditions be evaluated | |
6011 | at compile time to false, thus taking away the entire checks | |
6012 | with them. */ | |
87ab2b04 | 6013 | if (optimize |
87ab2b04 | 6014 | && inside_loop_p () |
80096613 EB |
6015 | && (!gnu_low_bound |
6016 | || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound))) | |
6017 | && (!gnu_high_bound | |
6018 | || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound))) | |
933a7325 | 6019 | && (loop = find_loop_for (gnu_index, &disp, &neg_p))) |
80096613 | 6020 | { |
87ab2b04 | 6021 | struct range_check_info_d *rci = ggc_alloc<range_check_info_d> (); |
80096613 EB |
6022 | rci->low_bound = gnu_low_bound; |
6023 | rci->high_bound = gnu_high_bound; | |
933a7325 EB |
6024 | rci->disp = disp; |
6025 | rci->neg_p = neg_p; | |
825da0d2 | 6026 | rci->type = gnu_type; |
64235766 EB |
6027 | rci->inserted_cond |
6028 | = build1 (SAVE_EXPR, boolean_type_node, boolean_true_node); | |
87ab2b04 | 6029 | vec_safe_push (loop->checks, rci); |
64235766 | 6030 | gnu_cond = build_noreturn_cond (gnat_to_gnu (gnat_cond)); |
3bdf0b64 | 6031 | if (optimize >= 3) |
64235766 EB |
6032 | gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR, |
6033 | boolean_type_node, | |
6034 | rci->inserted_cond, | |
6035 | gnu_cond); | |
6036 | else | |
6037 | gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR, | |
6038 | boolean_type_node, | |
6039 | gnu_cond, | |
6040 | rci->inserted_cond); | |
80096613 EB |
6041 | } |
6042 | } | |
6043 | break; | |
6044 | ||
6045 | default: | |
6046 | break; | |
6047 | } | |
6048 | ||
c900c700 EB |
6049 | /* The following processing does the real work, but we must nevertheless make |
6050 | sure not to override the result of the previous processing. */ | |
80096613 EB |
6051 | if (!gnu_result) |
6052 | gnu_result = build_call_raise (reason, gnat_node, kind); | |
6053 | set_expr_location_from_node (gnu_result, gnat_node); | |
6054 | ||
9a1bdc31 EB |
6055 | *gnu_result_type_p = get_unpadded_type (Etype (gnat_node)); |
6056 | ||
80096613 EB |
6057 | /* If the type is VOID, this is a statement, so we need to generate the code |
6058 | for the call. Handle a condition, if there is one. */ | |
6059 | if (VOID_TYPE_P (*gnu_result_type_p)) | |
6060 | { | |
6061 | if (Present (gnat_cond)) | |
6062 | { | |
6063 | if (!gnu_cond) | |
6064 | gnu_cond = gnat_to_gnu (gnat_cond); | |
f53aff92 EB |
6065 | if (integer_zerop (gnu_cond)) |
6066 | return alloc_stmt_list (); | |
80096613 EB |
6067 | gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result, |
6068 | alloc_stmt_list ()); | |
6069 | } | |
6070 | } | |
6071 | else | |
6fdacaf4 PT |
6072 | { |
6073 | /* The condition field must not be present when the node is used as an | |
6074 | expression form. */ | |
6075 | gigi_checking_assert (No (gnat_cond)); | |
6076 | gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result); | |
6077 | } | |
80096613 EB |
6078 | |
6079 | return gnu_result; | |
6080 | } | |
ce2d0ce2 | 6081 | |
27ab5bd8 EB |
6082 | /* Return true if GNAT_NODE is on the LHS of an assignment or an actual |
6083 | parameter of a call. */ | |
6084 | ||
6085 | static bool | |
6086 | lhs_or_actual_p (Node_Id gnat_node) | |
6087 | { | |
17a98a3d EB |
6088 | const Node_Id gnat_parent = Parent (gnat_node); |
6089 | const Node_Kind kind = Nkind (gnat_parent); | |
27ab5bd8 EB |
6090 | |
6091 | if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node) | |
6092 | return true; | |
6093 | ||
6094 | if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call) | |
6095 | && Name (gnat_parent) != gnat_node) | |
6096 | return true; | |
6097 | ||
6098 | if (kind == N_Parameter_Association) | |
6099 | return true; | |
6100 | ||
6101 | return false; | |
6102 | } | |
6103 | ||
033ba5bf EB |
6104 | /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS |
6105 | of an assignment or an actual parameter of a call. */ | |
6106 | ||
6107 | static bool | |
6108 | present_in_lhs_or_actual_p (Node_Id gnat_node) | |
6109 | { | |
033ba5bf EB |
6110 | if (lhs_or_actual_p (gnat_node)) |
6111 | return true; | |
6112 | ||
17a98a3d | 6113 | const Node_Kind kind = Nkind (Parent (gnat_node)); |
033ba5bf EB |
6114 | |
6115 | if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion) | |
6116 | && lhs_or_actual_p (Parent (gnat_node))) | |
6117 | return true; | |
6118 | ||
6119 | return false; | |
6120 | } | |
6121 | ||
4f8a6678 EB |
6122 | /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far |
6123 | as gigi is concerned. This is used to avoid conversions on the LHS. */ | |
c2efda0d EB |
6124 | |
6125 | static bool | |
4f8a6678 | 6126 | unchecked_conversion_nop (Node_Id gnat_node) |
c2efda0d EB |
6127 | { |
6128 | Entity_Id from_type, to_type; | |
6129 | ||
4f8a6678 EB |
6130 | /* The conversion must be on the LHS of an assignment or an actual parameter |
6131 | of a call. Otherwise, even if the conversion was essentially a no-op, it | |
6132 | could de facto ensure type consistency and this should be preserved. */ | |
27ab5bd8 | 6133 | if (!lhs_or_actual_p (gnat_node)) |
c2efda0d EB |
6134 | return false; |
6135 | ||
6136 | from_type = Etype (Expression (gnat_node)); | |
6137 | ||
6138 | /* We're interested in artificial conversions generated by the front-end | |
6139 | to make private types explicit, e.g. in Expand_Assign_Array. */ | |
6140 | if (!Is_Private_Type (from_type)) | |
6141 | return false; | |
6142 | ||
6143 | from_type = Underlying_Type (from_type); | |
6144 | to_type = Etype (gnat_node); | |
6145 | ||
6146 | /* The direct conversion to the underlying type is a no-op. */ | |
6147 | if (to_type == from_type) | |
6148 | return true; | |
6149 | ||
1a4cb227 | 6150 | /* For an array subtype, the conversion to the PAIT is a no-op. */ |
c2efda0d | 6151 | if (Ekind (from_type) == E_Array_Subtype |
1a4cb227 | 6152 | && to_type == Packed_Array_Impl_Type (from_type)) |
c2efda0d EB |
6153 | return true; |
6154 | ||
68a3eb69 EB |
6155 | /* For a record subtype, the conversion to the type is a no-op. */ |
6156 | if (Ekind (from_type) == E_Record_Subtype | |
6157 | && to_type == Etype (from_type)) | |
6158 | return true; | |
6159 | ||
c2efda0d EB |
6160 | return false; |
6161 | } | |
6162 | ||
4c335163 EB |
6163 | /* Return true if GNAT_NODE represents a statement. */ |
6164 | ||
6165 | static bool | |
6166 | statement_node_p (Node_Id gnat_node) | |
6167 | { | |
6168 | const Node_Kind kind = Nkind (gnat_node); | |
6169 | ||
6170 | if (kind == N_Label) | |
6171 | return true; | |
6172 | ||
6173 | if (IN (kind, N_Statement_Other_Than_Procedure_Call)) | |
6174 | return true; | |
6175 | ||
6176 | if (kind == N_Procedure_Call_Statement) | |
6177 | return true; | |
6178 | ||
6179 | if (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void) | |
6180 | return true; | |
6181 | ||
6182 | return false; | |
6183 | } | |
6184 | ||
3f13dd77 EB |
6185 | /* This function is the driver of the GNAT to GCC tree transformation process. |
6186 | It is the entry point of the tree transformer. GNAT_NODE is the root of | |
6187 | some GNAT tree. Return the root of the corresponding GCC tree. If this | |
6188 | is an expression, return the GCC equivalent of the expression. If this | |
6189 | is a statement, return the statement or add it to the current statement | |
6190 | group, in which case anything returned is to be interpreted as occurring | |
6191 | after anything added. */ | |
a1ab4c31 AC |
6192 | |
6193 | tree | |
6194 | gnat_to_gnu (Node_Id gnat_node) | |
6195 | { | |
3f13dd77 | 6196 | const Node_Kind kind = Nkind (gnat_node); |
1e17ef87 | 6197 | tree gnu_result = error_mark_node; /* Default to no value. */ |
a1ab4c31 | 6198 | tree gnu_result_type = void_type_node; |
3f13dd77 | 6199 | tree gnu_expr, gnu_lhs, gnu_rhs; |
a1ab4c31 | 6200 | Node_Id gnat_temp; |
17a98a3d | 6201 | atomic_acces_t aa_type; |
f53aff92 | 6202 | bool went_into_elab_proc; |
17a98a3d | 6203 | bool aa_sync; |
544d14e1 | 6204 | Entity_Id gnat_smo; |
a1ab4c31 AC |
6205 | |
6206 | /* Save node number for error message and set location information. */ | |
b64c4968 EB |
6207 | if (Sloc (gnat_node) > No_Location) |
6208 | Current_Error_Node = gnat_node; | |
a1ab4c31 AC |
6209 | Sloc_to_locus (Sloc (gnat_node), &input_location); |
6210 | ||
4c335163 EB |
6211 | /* If we are only annotating types and this node is a statement, return |
6212 | an empty statement list. */ | |
6213 | if (type_annotate_only && statement_node_p (gnat_node)) | |
a1ab4c31 AC |
6214 | return alloc_stmt_list (); |
6215 | ||
4c335163 EB |
6216 | /* If we are only annotating types and this node is a subexpression, return |
6217 | a NULL_EXPR, but filter out nodes appearing in the expressions attached | |
6218 | to packed array implementation types. */ | |
a1ab4c31 | 6219 | if (type_annotate_only |
3f13dd77 | 6220 | && IN (kind, N_Subexpr) |
4c335163 EB |
6221 | && !(((IN (kind, N_Op) && kind != N_Op_Expon) |
6222 | || kind == N_Type_Conversion) | |
6223 | && Is_Integer_Type (Etype (gnat_node))) | |
6224 | && !(kind == N_Attribute_Reference | |
4b9e1bc7 EB |
6225 | && (Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Length |
6226 | || Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Size) | |
6227 | && Is_Constrained (Etype (Prefix (gnat_node))) | |
4c335163 | 6228 | && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node)))) |
93200f92 | 6229 | && kind != N_Expanded_Name |
3f13dd77 | 6230 | && kind != N_Identifier |
a1ab4c31 AC |
6231 | && !Compile_Time_Known_Value (gnat_node)) |
6232 | return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)), | |
6233 | build_call_raise (CE_Range_Check_Failed, gnat_node, | |
6234 | N_Raise_Constraint_Error)); | |
6235 | ||
f53aff92 EB |
6236 | /* If this is a statement and we are at top level, it must be part of the |
6237 | elaboration procedure, so mark us as being in that procedure. */ | |
6238 | if ((statement_node_p (gnat_node) | |
6239 | || kind == N_Handled_Sequence_Of_Statements | |
6240 | || kind == N_Implicit_Label_Declaration) | |
6241 | && !current_function_decl) | |
a1ab4c31 | 6242 | { |
f53aff92 EB |
6243 | current_function_decl = get_elaboration_procedure (); |
6244 | went_into_elab_proc = true; | |
a1ab4c31 | 6245 | } |
f53aff92 EB |
6246 | else |
6247 | went_into_elab_proc = false; | |
a1ab4c31 | 6248 | |
3f13dd77 | 6249 | switch (kind) |
a1ab4c31 AC |
6250 | { |
6251 | /********************************/ | |
1e17ef87 | 6252 | /* Chapter 2: Lexical Elements */ |
a1ab4c31 AC |
6253 | /********************************/ |
6254 | ||
6255 | case N_Identifier: | |
6256 | case N_Expanded_Name: | |
6257 | case N_Operator_Symbol: | |
6258 | case N_Defining_Identifier: | |
1d4b96e0 | 6259 | case N_Defining_Operator_Symbol: |
a1ab4c31 | 6260 | gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type); |
033ba5bf | 6261 | |
f797c2b7 | 6262 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6263 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6264 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6265 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
a1ab4c31 AC |
6266 | break; |
6267 | ||
6268 | case N_Integer_Literal: | |
6269 | { | |
6270 | tree gnu_type; | |
6271 | ||
6272 | /* Get the type of the result, looking inside any padding and | |
6273 | justified modular types. Then get the value in that type. */ | |
6274 | gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6275 | ||
6276 | if (TREE_CODE (gnu_type) == RECORD_TYPE | |
6277 | && TYPE_JUSTIFIED_MODULAR_P (gnu_type)) | |
6278 | gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type)); | |
6279 | ||
6280 | gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type); | |
6281 | ||
6282 | /* If the result overflows (meaning it doesn't fit in its base type), | |
104099b8 EB |
6283 | abort, unless this is for a named number because that's not fatal. |
6284 | We would like to check that the value is within the range of the | |
6285 | subtype, but that causes problems with subtypes whose usage will | |
6286 | raise Constraint_Error and also with biased representation. */ | |
6287 | if (TREE_OVERFLOW (gnu_result)) | |
6288 | { | |
6289 | if (Nkind (Parent (gnat_node)) == N_Number_Declaration) | |
6290 | gnu_result = error_mark_node; | |
6291 | else | |
6292 | gcc_unreachable (); | |
6293 | } | |
a1ab4c31 AC |
6294 | } |
6295 | break; | |
6296 | ||
6297 | case N_Character_Literal: | |
6298 | /* If a Entity is present, it means that this was one of the | |
6299 | literals in a user-defined character type. In that case, | |
6300 | just return the value in the CONST_DECL. Otherwise, use the | |
6301 | character code. In that case, the base type should be an | |
6302 | INTEGER_TYPE, but we won't bother checking for that. */ | |
6303 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6304 | if (Present (Entity (gnat_node))) | |
6305 | gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node))); | |
6306 | else | |
6307 | gnu_result | |
9a1bdc31 EB |
6308 | = build_int_cst (gnu_result_type, |
6309 | UI_To_CC (Char_Literal_Value (gnat_node))); | |
a1ab4c31 AC |
6310 | break; |
6311 | ||
6312 | case N_Real_Literal: | |
015bee83 AC |
6313 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6314 | ||
08477031 EB |
6315 | /* If this is of a fixed-point type, the value we want is the value of |
6316 | the corresponding integer. */ | |
7ed9919d | 6317 | if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node)))) |
a1ab4c31 | 6318 | { |
a1ab4c31 AC |
6319 | gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node), |
6320 | gnu_result_type); | |
6321 | gcc_assert (!TREE_OVERFLOW (gnu_result)); | |
6322 | } | |
6323 | ||
a1ab4c31 | 6324 | else |
1e17ef87 | 6325 | { |
a1ab4c31 AC |
6326 | Ureal ur_realval = Realval (gnat_node); |
6327 | ||
08477031 EB |
6328 | /* First convert the value to a machine number if it isn't already. |
6329 | That will force the base to 2 for non-zero values and simplify | |
6330 | the rest of the logic. */ | |
015bee83 AC |
6331 | if (!Is_Machine_Number (gnat_node)) |
6332 | ur_realval | |
6333 | = Machine (Base_Type (Underlying_Type (Etype (gnat_node))), | |
6334 | ur_realval, Round_Even, gnat_node); | |
a1ab4c31 | 6335 | |
a1ab4c31 | 6336 | if (UR_Is_Zero (ur_realval)) |
9a1bdc31 | 6337 | gnu_result = build_real (gnu_result_type, dconst0); |
a1ab4c31 AC |
6338 | else |
6339 | { | |
015bee83 | 6340 | REAL_VALUE_TYPE tmp; |
a1ab4c31 | 6341 | |
08477031 | 6342 | gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type); |
a1ab4c31 | 6343 | |
015bee83 | 6344 | /* The base must be 2 as Machine guarantees this, so we scale |
08477031 EB |
6345 | the value, which we know can fit in the mantissa of the type |
6346 | (hence the use of that type above). */ | |
015bee83 AC |
6347 | gcc_assert (Rbase (ur_realval) == 2); |
6348 | real_ldexp (&tmp, &TREE_REAL_CST (gnu_result), | |
6349 | - UI_To_Int (Denominator (ur_realval))); | |
6350 | gnu_result = build_real (gnu_result_type, tmp); | |
a1ab4c31 AC |
6351 | } |
6352 | ||
6353 | /* Now see if we need to negate the result. Do it this way to | |
6354 | properly handle -0. */ | |
6355 | if (UR_Is_Negative (Realval (gnat_node))) | |
6356 | gnu_result | |
6357 | = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type), | |
6358 | gnu_result); | |
6359 | } | |
6360 | ||
6361 | break; | |
6362 | ||
6363 | case N_String_Literal: | |
6364 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6365 | if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR) | |
6366 | { | |
6367 | String_Id gnat_string = Strval (gnat_node); | |
6368 | int length = String_Length (gnat_string); | |
6369 | int i; | |
6370 | char *string; | |
6371 | if (length >= ALLOCA_THRESHOLD) | |
ad00a297 | 6372 | string = XNEWVEC (char, length); |
1e17ef87 | 6373 | else |
ad00a297 | 6374 | string = (char *) alloca (length); |
a1ab4c31 AC |
6375 | |
6376 | /* Build the string with the characters in the literal. Note | |
6377 | that Ada strings are 1-origin. */ | |
6378 | for (i = 0; i < length; i++) | |
6379 | string[i] = Get_String_Char (gnat_string, i + 1); | |
6380 | ||
a1ab4c31 AC |
6381 | gnu_result = build_string (length, string); |
6382 | ||
6383 | /* Strings in GCC don't normally have types, but we want | |
6384 | this to not be converted to the array type. */ | |
6385 | TREE_TYPE (gnu_result) = gnu_result_type; | |
6386 | ||
1e17ef87 EB |
6387 | if (length >= ALLOCA_THRESHOLD) |
6388 | free (string); | |
a1ab4c31 AC |
6389 | } |
6390 | else | |
6391 | { | |
6392 | /* Build a list consisting of each character, then make | |
6393 | the aggregate. */ | |
6394 | String_Id gnat_string = Strval (gnat_node); | |
6395 | int length = String_Length (gnat_string); | |
6396 | int i; | |
a1ab4c31 | 6397 | tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type)); |
8b9aec86 | 6398 | tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node); |
9771b263 DN |
6399 | vec<constructor_elt, va_gc> *gnu_vec; |
6400 | vec_alloc (gnu_vec, length); | |
a1ab4c31 AC |
6401 | |
6402 | for (i = 0; i < length; i++) | |
6403 | { | |
0e228dd9 NF |
6404 | tree t = build_int_cst (TREE_TYPE (gnu_result_type), |
6405 | Get_String_Char (gnat_string, i + 1)); | |
a1ab4c31 | 6406 | |
0e228dd9 | 6407 | CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t); |
8b9aec86 | 6408 | gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node); |
a1ab4c31 AC |
6409 | } |
6410 | ||
0e228dd9 | 6411 | gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec); |
a1ab4c31 AC |
6412 | } |
6413 | break; | |
6414 | ||
6415 | case N_Pragma: | |
6416 | gnu_result = Pragma_to_gnu (gnat_node); | |
6417 | break; | |
6418 | ||
6419 | /**************************************/ | |
1e17ef87 | 6420 | /* Chapter 3: Declarations and Types */ |
a1ab4c31 AC |
6421 | /**************************************/ |
6422 | ||
6423 | case N_Subtype_Declaration: | |
6424 | case N_Full_Type_Declaration: | |
6425 | case N_Incomplete_Type_Declaration: | |
6426 | case N_Private_Type_Declaration: | |
6427 | case N_Private_Extension_Declaration: | |
6428 | case N_Task_Type_Declaration: | |
6429 | process_type (Defining_Entity (gnat_node)); | |
6430 | gnu_result = alloc_stmt_list (); | |
6431 | break; | |
6432 | ||
6433 | case N_Object_Declaration: | |
104099b8 | 6434 | case N_Number_Declaration: |
a1ab4c31 AC |
6435 | case N_Exception_Declaration: |
6436 | gnat_temp = Defining_Entity (gnat_node); | |
6437 | gnu_result = alloc_stmt_list (); | |
6438 | ||
6439 | /* If we are just annotating types and this object has an unconstrained | |
6440 | or task type, don't elaborate it. */ | |
6441 | if (type_annotate_only | |
6442 | && (((Is_Array_Type (Etype (gnat_temp)) | |
6443 | || Is_Record_Type (Etype (gnat_temp))) | |
6444 | && !Is_Constrained (Etype (gnat_temp))) | |
3d25b3ba | 6445 | || Is_Concurrent_Type (Etype (gnat_temp)))) |
a1ab4c31 AC |
6446 | break; |
6447 | ||
9aa9ba95 EB |
6448 | /* If this is a constant related to a return initialized by a reference |
6449 | to a function call in a function returning by invisible reference: | |
6450 | ||
6451 | type Ann is access all Result_Type; | |
6452 | Rnn : constant Ann := Func'reference; | |
6453 | [...] | |
6454 | return Rnn.all; | |
6455 | ||
6456 | then elide the temporary by forwarding the return object to Func: | |
6457 | ||
2473f28d | 6458 | result_type *Rnn = (result_type *) <retval>; |
9aa9ba95 EB |
6459 | *<retval> = Func (); [return slot optimization] |
6460 | [...] | |
2473f28d | 6461 | return Rnn; |
9aa9ba95 EB |
6462 | |
6463 | That's necessary if the result type needs finalization because the | |
6464 | temporary would never be adjusted as Expand_Simple_Function_Return | |
6465 | also elides the temporary in this case. */ | |
6466 | if (Ekind (gnat_temp) == E_Constant | |
6467 | && Is_Related_To_Func_Return (gnat_temp) | |
6468 | && Nkind (Expression (gnat_node)) == N_Reference | |
6469 | && Nkind (Prefix (Expression (gnat_node))) == N_Function_Call | |
6470 | && current_function_decl | |
6471 | && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl))) | |
6472 | { | |
2473f28d EB |
6473 | gnat_to_gnu_entity (gnat_temp, |
6474 | DECL_RESULT (current_function_decl), | |
6475 | true); | |
6476 | gnu_result | |
6477 | = build_unary_op (INDIRECT_REF, NULL_TREE, | |
6478 | DECL_RESULT (current_function_decl)); | |
9aa9ba95 EB |
6479 | gnu_result |
6480 | = Call_to_gnu (Prefix (Expression (gnat_node)), | |
6481 | &gnu_result_type, gnu_result, | |
544d14e1 | 6482 | NOT_ATOMIC, false, Empty); |
9aa9ba95 EB |
6483 | break; |
6484 | } | |
6485 | ||
a1ab4c31 | 6486 | if (Present (Expression (gnat_node)) |
3f13dd77 | 6487 | && !(kind == N_Object_Declaration && No_Initialization (gnat_node)) |
a1ab4c31 AC |
6488 | && (!type_annotate_only |
6489 | || Compile_Time_Known_Value (Expression (gnat_node)))) | |
6490 | { | |
815b5368 EB |
6491 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_node))); |
6492 | ||
a1ab4c31 | 6493 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); |
a1ab4c31 | 6494 | |
b4407150 | 6495 | /* First deal with erroneous expressions. */ |
104099b8 EB |
6496 | if (TREE_CODE (gnu_expr) == ERROR_MARK) |
6497 | { | |
6498 | /* If this is a named number for which we cannot manipulate | |
6499 | the value, just skip the declaration altogether. */ | |
6500 | if (kind == N_Number_Declaration) | |
6501 | break; | |
6502 | else if (type_annotate_only) | |
6503 | gnu_expr = NULL_TREE; | |
6504 | } | |
b4407150 EB |
6505 | |
6506 | /* Then a special case: we do not want the SLOC of the expression | |
6507 | of the tag to pop up every time it is referenced somewhere. */ | |
6508 | else if (EXPR_P (gnu_expr) && Is_Tag (gnat_temp)) | |
6509 | SET_EXPR_LOCATION (gnu_expr, UNKNOWN_LOCATION); | |
a1ab4c31 AC |
6510 | } |
6511 | else | |
6512 | gnu_expr = NULL_TREE; | |
6513 | ||
8df2e902 EB |
6514 | /* If this is a deferred constant with an address clause, we ignore the |
6515 | full view since the clause is on the partial view and we cannot have | |
6516 | 2 different GCC trees for the object. The only bits of the full view | |
6517 | we will use is the initializer, but it will be directly fetched. */ | |
7c775aca | 6518 | if (Ekind (gnat_temp) == E_Constant |
8df2e902 EB |
6519 | && Present (Address_Clause (gnat_temp)) |
6520 | && Present (Full_View (gnat_temp))) | |
6521 | save_gnu_tree (Full_View (gnat_temp), error_mark_node, true); | |
6522 | ||
545b4923 EB |
6523 | /* If this object has its elaboration delayed, we must force evaluation |
6524 | of GNU_EXPR now and save it for the freeze point. Note that we need | |
6525 | not do anything special at the global level since the lifetime of the | |
6526 | temporary is fully contained within the elaboration routine. */ | |
6527 | if (Present (Freeze_Node (gnat_temp))) | |
6528 | { | |
6529 | if (gnu_expr) | |
6530 | { | |
6531 | gnu_result = gnat_save_expr (gnu_expr); | |
6532 | save_gnu_tree (gnat_node, gnu_result, true); | |
6533 | } | |
6534 | } | |
6535 | else | |
afc737f0 | 6536 | gnat_to_gnu_entity (gnat_temp, gnu_expr, true); |
a1ab4c31 AC |
6537 | break; |
6538 | ||
6539 | case N_Object_Renaming_Declaration: | |
6540 | gnat_temp = Defining_Entity (gnat_node); | |
ea292448 | 6541 | gnu_result = alloc_stmt_list (); |
a1ab4c31 | 6542 | |
656b0311 PMR |
6543 | /* Don't do anything if this renaming is handled by the front end and it |
6544 | does not need debug info. Note that we consider renamings don't need | |
6545 | debug info when optimizing: our way to describe them has a | |
6546 | memory/elaboration footprint. | |
6547 | ||
6548 | Don't do anything neither if we are just annotating types and this | |
6549 | object has a composite or task type, don't elaborate it. */ | |
6550 | if ((!Is_Renaming_Of_Object (gnat_temp) | |
6551 | || (Needs_Debug_Info (gnat_temp) | |
6552 | && !optimize | |
6553 | && can_materialize_object_renaming_p | |
6554 | (Renamed_Object (gnat_temp)))) | |
a1ab4c31 AC |
6555 | && ! (type_annotate_only |
6556 | && (Is_Array_Type (Etype (gnat_temp)) | |
6557 | || Is_Record_Type (Etype (gnat_temp)) | |
6558 | || Is_Concurrent_Type (Etype (gnat_temp))))) | |
5bdd063b EB |
6559 | gnat_to_gnu_entity (gnat_temp, |
6560 | gnat_to_gnu (Renamed_Object (gnat_temp)), | |
6561 | true); | |
a1ab4c31 AC |
6562 | break; |
6563 | ||
ac43e11e AC |
6564 | case N_Exception_Renaming_Declaration: |
6565 | gnat_temp = Defining_Entity (gnat_node); | |
ea292448 EB |
6566 | gnu_result = alloc_stmt_list (); |
6567 | ||
ea292448 | 6568 | if (Present (Renamed_Entity (gnat_temp))) |
5bdd063b EB |
6569 | gnat_to_gnu_entity (gnat_temp, |
6570 | gnat_to_gnu (Renamed_Entity (gnat_temp)), | |
6571 | true); | |
ac43e11e AC |
6572 | break; |
6573 | ||
1d4b96e0 AC |
6574 | case N_Subprogram_Renaming_Declaration: |
6575 | { | |
6576 | const Node_Id gnat_renaming = Defining_Entity (gnat_node); | |
6577 | const Node_Id gnat_renamed = Renamed_Entity (gnat_renaming); | |
6578 | ||
6579 | gnu_result = alloc_stmt_list (); | |
6580 | ||
6581 | /* Materializing renamed subprograms will only benefit the debugging | |
6582 | information as they aren't referenced in the generated code. So | |
6583 | skip them when they aren't needed. Avoid doing this if: | |
6584 | ||
6585 | - there is a freeze node: in this case the renamed entity is not | |
16f19962 | 6586 | elaborated yet, |
1d4b96e0 AC |
6587 | - the renamed subprogram is intrinsic: it will not be available in |
6588 | the debugging information (note that both or only one of the | |
6589 | renaming and the renamed subprograms can be intrinsic). */ | |
16f19962 | 6590 | if (!type_annotate_only |
1d4b96e0 | 6591 | && Needs_Debug_Info (gnat_renaming) |
16f19962 | 6592 | && No (Freeze_Node (gnat_renaming)) |
1d4b96e0 AC |
6593 | && Present (gnat_renamed) |
6594 | && (Ekind (gnat_renamed) == E_Function | |
6595 | || Ekind (gnat_renamed) == E_Procedure) | |
6596 | && !Is_Intrinsic_Subprogram (gnat_renaming) | |
6597 | && !Is_Intrinsic_Subprogram (gnat_renamed)) | |
afc737f0 | 6598 | gnat_to_gnu_entity (gnat_renaming, gnat_to_gnu (gnat_renamed), true); |
1d4b96e0 AC |
6599 | break; |
6600 | } | |
6601 | ||
a1ab4c31 | 6602 | case N_Implicit_Label_Declaration: |
afc737f0 | 6603 | gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true); |
a1ab4c31 AC |
6604 | gnu_result = alloc_stmt_list (); |
6605 | break; | |
6606 | ||
ac43e11e | 6607 | case N_Package_Renaming_Declaration: |
a1ab4c31 | 6608 | /* These are fully handled in the front end. */ |
ac43e11e AC |
6609 | /* ??? For package renamings, find a way to use GENERIC namespaces so |
6610 | that we get proper debug information for them. */ | |
a1ab4c31 AC |
6611 | gnu_result = alloc_stmt_list (); |
6612 | break; | |
6613 | ||
6614 | /*************************************/ | |
1e17ef87 | 6615 | /* Chapter 4: Names and Expressions */ |
a1ab4c31 AC |
6616 | /*************************************/ |
6617 | ||
6618 | case N_Explicit_Dereference: | |
806fcf71 | 6619 | /* Make sure the designated type is complete before dereferencing. */ |
a1ab4c31 | 6620 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
806fcf71 | 6621 | gnu_result = gnat_to_gnu (Prefix (gnat_node)); |
a1ab4c31 | 6622 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result); |
033ba5bf | 6623 | |
f797c2b7 | 6624 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6625 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6626 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6627 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
544d14e1 EB |
6628 | |
6629 | /* If storage model access is required on the RHS, build the load. */ | |
6630 | else if (storage_model_access_required_p (gnat_node, &gnat_smo) | |
6631 | && Present (Storage_Model_Copy_From (gnat_smo)) | |
6632 | && !present_in_lhs_or_actual_p (gnat_node)) | |
6633 | gnu_result = build_storage_model_load (gnat_smo, gnu_result); | |
a1ab4c31 AC |
6634 | break; |
6635 | ||
6636 | case N_Indexed_Component: | |
6637 | { | |
544d14e1 EB |
6638 | const Entity_Id gnat_array_object = Prefix (gnat_node); |
6639 | tree gnu_array_object = gnat_to_gnu (gnat_array_object); | |
a1ab4c31 | 6640 | tree gnu_type; |
bb24f343 | 6641 | int ndim, i; |
a1ab4c31 AC |
6642 | Node_Id *gnat_expr_array; |
6643 | ||
544d14e1 EB |
6644 | /* Get the storage model of the array. */ |
6645 | gnat_smo = get_storage_model (gnat_array_object); | |
6646 | ||
bb24f343 | 6647 | gnu_array_object = maybe_padded_object (gnu_array_object); |
ad00a297 | 6648 | gnu_array_object = maybe_unconstrained_array (gnu_array_object); |
7948ae37 OH |
6649 | |
6650 | /* Convert vector inputs to their representative array type, to fit | |
6651 | what the code below expects. */ | |
f71d5704 EB |
6652 | if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object))) |
6653 | { | |
6654 | if (present_in_lhs_or_actual_p (gnat_node)) | |
6655 | gnat_mark_addressable (gnu_array_object); | |
6656 | gnu_array_object = maybe_vector_array (gnu_array_object); | |
6657 | } | |
7948ae37 | 6658 | |
54441a34 EB |
6659 | /* The failure of this assertion will very likely come from a missing |
6660 | expansion for a packed array access. */ | |
6661 | gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE); | |
6662 | ||
a1ab4c31 AC |
6663 | /* First compute the number of dimensions of the array, then |
6664 | fill the expression array, the order depending on whether | |
6665 | this is a Convention_Fortran array or not. */ | |
6666 | for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object); | |
6667 | TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE | |
6668 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)); | |
6669 | ndim++, gnu_type = TREE_TYPE (gnu_type)) | |
6670 | ; | |
6671 | ||
2bb1fc26 | 6672 | gnat_expr_array = XALLOCAVEC (Node_Id, ndim); |
a1ab4c31 AC |
6673 | |
6674 | if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object))) | |
6675 | for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node)); | |
6676 | i >= 0; | |
6677 | i--, gnat_temp = Next (gnat_temp)) | |
6678 | gnat_expr_array[i] = gnat_temp; | |
6679 | else | |
6680 | for (i = 0, gnat_temp = First (Expressions (gnat_node)); | |
6681 | i < ndim; | |
6682 | i++, gnat_temp = Next (gnat_temp)) | |
6683 | gnat_expr_array[i] = gnat_temp; | |
6684 | ||
825da0d2 EB |
6685 | /* Start with the prefix and build the successive references. */ |
6686 | gnu_result = gnu_array_object; | |
6687 | ||
a1ab4c31 | 6688 | for (i = 0, gnu_type = TREE_TYPE (gnu_array_object); |
87ab2b04 EB |
6689 | i < ndim; |
6690 | i++, gnu_type = TREE_TYPE (gnu_type)) | |
a1ab4c31 AC |
6691 | { |
6692 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
6693 | gnat_temp = gnat_expr_array[i]; | |
825da0d2 | 6694 | gnu_expr = maybe_character_value (gnat_to_gnu (gnat_temp)); |
a1ab4c31 | 6695 | |
384e3fb1 JM |
6696 | gnu_result |
6697 | = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr); | |
544d14e1 EB |
6698 | |
6699 | if (Present (gnat_smo)) | |
6700 | instantiate_load_in_array_ref (gnu_result, gnat_smo); | |
a1ab4c31 | 6701 | } |
a1ab4c31 | 6702 | |
033ba5bf EB |
6703 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6704 | ||
f797c2b7 | 6705 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6706 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6707 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6708 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
544d14e1 EB |
6709 | |
6710 | /* If storage model access is required on the RHS, build the load. */ | |
6711 | else if (storage_model_access_required_p (gnat_node, &gnat_smo) | |
6712 | && Present (Storage_Model_Copy_From (gnat_smo)) | |
6713 | && !present_in_lhs_or_actual_p (gnat_node)) | |
6714 | gnu_result = build_storage_model_load (gnat_smo, gnu_result); | |
033ba5bf | 6715 | } |
a1ab4c31 AC |
6716 | break; |
6717 | ||
6718 | case N_Slice: | |
6719 | { | |
544d14e1 EB |
6720 | const Entity_Id gnat_array_object = Prefix (gnat_node); |
6721 | tree gnu_array_object = gnat_to_gnu (gnat_array_object); | |
a1ab4c31 | 6722 | |
544d14e1 EB |
6723 | /* Get the storage model of the array. */ |
6724 | gnat_smo = get_storage_model (gnat_array_object); | |
a1ab4c31 | 6725 | |
bb24f343 | 6726 | gnu_array_object = maybe_padded_object (gnu_array_object); |
825da0d2 EB |
6727 | gnu_array_object = maybe_unconstrained_array (gnu_array_object); |
6728 | ||
544d14e1 EB |
6729 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6730 | ||
825da0d2 EB |
6731 | gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type)); |
6732 | gnu_expr = maybe_character_value (gnu_expr); | |
a1ab4c31 | 6733 | |
f76d6e6f EB |
6734 | /* If this is a slice with non-constant size of an array with constant |
6735 | size, set the maximum size for the allocation of temporaries. */ | |
6736 | if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type)) | |
825da0d2 EB |
6737 | && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object)))) |
6738 | TYPE_ARRAY_MAX_SIZE (gnu_result_type) | |
6739 | = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object)); | |
f76d6e6f | 6740 | |
a1ab4c31 | 6741 | gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type, |
825da0d2 | 6742 | gnu_array_object, gnu_expr); |
544d14e1 EB |
6743 | |
6744 | if (Present (gnat_smo)) | |
6745 | instantiate_load_in_array_ref (gnu_result, gnat_smo); | |
6746 | ||
6747 | /* If storage model access is required on the RHS, build the load. */ | |
6748 | if (storage_model_access_required_p (gnat_node, &gnat_smo) | |
6749 | && Present (Storage_Model_Copy_From (gnat_smo)) | |
6750 | && !present_in_lhs_or_actual_p (gnat_node)) | |
6751 | gnu_result = build_storage_model_load (gnat_smo, gnu_result); | |
a1ab4c31 AC |
6752 | } |
6753 | break; | |
6754 | ||
6755 | case N_Selected_Component: | |
6756 | { | |
bb24f343 | 6757 | const Entity_Id gnat_prefix = Prefix (gnat_node); |
a1ab4c31 | 6758 | Entity_Id gnat_field = Entity (Selector_Name (gnat_node)); |
1eb58520 | 6759 | tree gnu_prefix = gnat_to_gnu (gnat_prefix); |
a1ab4c31 | 6760 | |
bb24f343 | 6761 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 | 6762 | |
4ec7c4ec EB |
6763 | /* gnat_to_gnu_entity does not save the GNU tree made for renamed |
6764 | discriminants so avoid making recursive calls on each reference | |
6765 | to them by following the appropriate link directly here. */ | |
6766 | if (Ekind (gnat_field) == E_Discriminant) | |
6767 | { | |
6768 | /* For discriminant references in tagged types always substitute | |
6769 | the corresponding discriminant as the actual component. */ | |
6770 | if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix)))) | |
6771 | while (Present (Corresponding_Discriminant (gnat_field))) | |
6772 | gnat_field = Corresponding_Discriminant (gnat_field); | |
6773 | ||
6774 | /* For discriminant references in untagged types always substitute | |
6775 | the corresponding stored discriminant. */ | |
6776 | else if (Present (Corresponding_Discriminant (gnat_field))) | |
6777 | gnat_field = Original_Record_Component (gnat_field); | |
6778 | } | |
a1ab4c31 AC |
6779 | |
6780 | /* Handle extracting the real or imaginary part of a complex. | |
6781 | The real part is the first field and the imaginary the last. */ | |
a1ab4c31 AC |
6782 | if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE) |
6783 | gnu_result = build_unary_op (Present (Next_Entity (gnat_field)) | |
6784 | ? REALPART_EXPR : IMAGPART_EXPR, | |
6785 | NULL_TREE, gnu_prefix); | |
6786 | else | |
6787 | { | |
d3271136 | 6788 | tree gnu_field = gnat_to_gnu_field_decl (gnat_field); |
852dd866 EB |
6789 | tree gnu_offset; |
6790 | struct loop_info_d *loop; | |
d3271136 | 6791 | |
a1ab4c31 | 6792 | gnu_result |
64235766 | 6793 | = build_component_ref (gnu_prefix, gnu_field, |
a1ab4c31 | 6794 | (Nkind (Parent (gnat_node)) |
3cd64bab EB |
6795 | == N_Attribute_Reference) |
6796 | && lvalue_required_for_attribute_p | |
6797 | (Parent (gnat_node))); | |
852dd866 EB |
6798 | |
6799 | /* If optimization is enabled and we are inside a loop, we try to | |
6800 | hoist nonconstant but invariant offset computations outside of | |
6801 | the loop, since they very likely contain loads that could turn | |
6802 | out to be hard to move if they end up in active EH regions. */ | |
6803 | if (optimize | |
6804 | && inside_loop_p () | |
6805 | && TREE_CODE (gnu_result) == COMPONENT_REF | |
6806 | && (gnu_offset = component_ref_field_offset (gnu_result)) | |
6807 | && !TREE_CONSTANT (gnu_offset) | |
6808 | && (gnu_offset = gnat_invariant_expr (gnu_offset)) | |
6809 | && (loop = find_loop ())) | |
6810 | { | |
6811 | tree invariant | |
6812 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_offset), gnu_offset); | |
6813 | vec_safe_push (loop->invariants, invariant); | |
6814 | tree field = TREE_OPERAND (gnu_result, 1); | |
6815 | tree factor | |
6816 | = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT); | |
6817 | /* Divide the offset by its alignment. */ | |
6818 | TREE_OPERAND (gnu_result, 2) | |
6819 | = size_binop (EXACT_DIV_EXPR, invariant, factor); | |
6820 | } | |
a1ab4c31 AC |
6821 | } |
6822 | ||
a1ab4c31 | 6823 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
033ba5bf | 6824 | |
f797c2b7 | 6825 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6826 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6827 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6828 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
544d14e1 EB |
6829 | |
6830 | /* If storage model access is required on the RHS, build the load. */ | |
6831 | else if (storage_model_access_required_p (gnat_node, &gnat_smo) | |
6832 | && Present (Storage_Model_Copy_From (gnat_smo)) | |
6833 | && !present_in_lhs_or_actual_p (gnat_node)) | |
6834 | gnu_result = build_storage_model_load (gnat_smo, gnu_result); | |
a1ab4c31 AC |
6835 | } |
6836 | break; | |
6837 | ||
6838 | case N_Attribute_Reference: | |
6839 | { | |
86060344 EB |
6840 | /* The attribute designator. */ |
6841 | const int attr = Get_Attribute_Id (Attribute_Name (gnat_node)); | |
6842 | ||
6843 | /* The Elab_Spec and Elab_Body attributes are special in that Prefix | |
6844 | is a unit, not an object with a GCC equivalent. */ | |
6845 | if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body) | |
6846 | return | |
6847 | create_subprog_decl (create_concat_name | |
6848 | (Entity (Prefix (gnat_node)), | |
6849 | attr == Attr_Elab_Body ? "elabb" : "elabs"), | |
13a6dfe3 | 6850 | NULL_TREE, void_ftype, NULL_TREE, is_default, |
ff9baa5f PMR |
6851 | true, true, true, true, false, NULL, |
6852 | gnat_node); | |
86060344 EB |
6853 | |
6854 | gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr); | |
a1ab4c31 AC |
6855 | } |
6856 | break; | |
6857 | ||
6858 | case N_Reference: | |
6859 | /* Like 'Access as far as we are concerned. */ | |
6860 | gnu_result = gnat_to_gnu (Prefix (gnat_node)); | |
6861 | gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result); | |
6862 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6863 | break; | |
6864 | ||
6865 | case N_Aggregate: | |
6866 | case N_Extension_Aggregate: | |
6867 | { | |
6868 | tree gnu_aggr_type; | |
6869 | ||
14cc7b26 | 6870 | /* Check that this aggregate has not slipped through the cracks. */ |
a1ab4c31 AC |
6871 | gcc_assert (!Expansion_Delayed (gnat_node)); |
6872 | ||
14cc7b26 | 6873 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
a1ab4c31 AC |
6874 | |
6875 | if (TREE_CODE (gnu_result_type) == RECORD_TYPE | |
6876 | && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type)) | |
6877 | gnu_aggr_type | |
7d76717d | 6878 | = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type))); |
04682fe7 | 6879 | else if (VECTOR_TYPE_P (gnu_result_type)) |
7948ae37 | 6880 | gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type); |
14cc7b26 EB |
6881 | else |
6882 | gnu_aggr_type = gnu_result_type; | |
a1ab4c31 AC |
6883 | |
6884 | if (Null_Record_Present (gnat_node)) | |
b1b2b511 | 6885 | gnu_result = gnat_build_constructor (gnu_aggr_type, NULL); |
a1ab4c31 AC |
6886 | |
6887 | else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE | |
6888 | || TREE_CODE (gnu_aggr_type) == UNION_TYPE) | |
6889 | gnu_result | |
6890 | = assoc_to_constructor (Etype (gnat_node), | |
6891 | First (Component_Associations (gnat_node)), | |
6892 | gnu_aggr_type); | |
6893 | else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE) | |
6894 | gnu_result = pos_to_constructor (First (Expressions (gnat_node)), | |
815b5368 | 6895 | gnu_aggr_type); |
a1ab4c31 AC |
6896 | else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE) |
6897 | gnu_result | |
6898 | = build_binary_op | |
6899 | (COMPLEX_EXPR, gnu_aggr_type, | |
6900 | gnat_to_gnu (Expression (First | |
6901 | (Component_Associations (gnat_node)))), | |
6902 | gnat_to_gnu (Expression | |
6903 | (Next | |
6904 | (First (Component_Associations (gnat_node)))))); | |
6905 | else | |
6906 | gcc_unreachable (); | |
6907 | ||
6908 | gnu_result = convert (gnu_result_type, gnu_result); | |
6909 | } | |
6910 | break; | |
6911 | ||
6912 | case N_Null: | |
6913 | if (TARGET_VTABLE_USES_DESCRIPTORS | |
6914 | && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type | |
6915 | && Is_Dispatch_Table_Entity (Etype (gnat_node))) | |
6916 | gnu_result = null_fdesc_node; | |
6917 | else | |
6918 | gnu_result = null_pointer_node; | |
6919 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6920 | break; | |
6921 | ||
6922 | case N_Type_Conversion: | |
6923 | case N_Qualified_Expression: | |
825da0d2 | 6924 | gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node))); |
a1ab4c31 AC |
6925 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6926 | ||
0029bafd EB |
6927 | /* If this is a qualified expression for a tagged type, we mark the type |
6928 | as used. Because of polymorphism, this might be the only reference to | |
6929 | the tagged type in the program while objects have it as dynamic type. | |
6930 | The debugger needs to see it to display these objects properly. */ | |
6931 | if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node))) | |
6932 | used_types_insert (gnu_result_type); | |
6933 | ||
815b5368 EB |
6934 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_node))); |
6935 | ||
a1ab4c31 | 6936 | gnu_result |
825da0d2 | 6937 | = convert_with_check (Etype (gnat_node), gnu_expr, |
a1ab4c31 | 6938 | Do_Overflow_Check (gnat_node), |
3f13dd77 | 6939 | kind == N_Type_Conversion |
10069d53 | 6940 | && Float_Truncate (gnat_node), gnat_node); |
a1ab4c31 AC |
6941 | break; |
6942 | ||
6943 | case N_Unchecked_Type_Conversion: | |
4ec7c4ec | 6944 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
825da0d2 | 6945 | gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node))); |
c2efda0d EB |
6946 | |
6947 | /* Skip further processing if the conversion is deemed a no-op. */ | |
4f8a6678 | 6948 | if (unchecked_conversion_nop (gnat_node)) |
c2efda0d | 6949 | { |
825da0d2 | 6950 | gnu_result = gnu_expr; |
c2efda0d EB |
6951 | gnu_result_type = TREE_TYPE (gnu_result); |
6952 | break; | |
6953 | } | |
6954 | ||
a1ab4c31 AC |
6955 | /* If the result is a pointer type, see if we are improperly |
6956 | converting to a stricter alignment. */ | |
6957 | if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type) | |
7ed9919d | 6958 | && Is_Access_Type (Etype (gnat_node))) |
a1ab4c31 | 6959 | { |
825da0d2 | 6960 | unsigned int align = known_alignment (gnu_expr); |
a1ab4c31 AC |
6961 | tree gnu_obj_type = TREE_TYPE (gnu_result_type); |
6962 | unsigned int oalign = TYPE_ALIGN (gnu_obj_type); | |
6963 | ||
6964 | if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type)) | |
6965 | post_error_ne_tree_2 | |
4a29b8d6 | 6966 | ("??source alignment (^) '< alignment of & (^)", |
a1ab4c31 AC |
6967 | gnat_node, Designated_Type (Etype (gnat_node)), |
6968 | size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT); | |
6969 | } | |
6970 | ||
6971 | /* If we are converting a descriptor to a function pointer, first | |
6972 | build the pointer. */ | |
6973 | if (TARGET_VTABLE_USES_DESCRIPTORS | |
825da0d2 | 6974 | && TREE_TYPE (gnu_expr) == fdesc_type_node |
a1ab4c31 | 6975 | && POINTER_TYPE_P (gnu_result_type)) |
825da0d2 | 6976 | gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); |
a1ab4c31 | 6977 | |
825da0d2 | 6978 | gnu_result = unchecked_convert (gnu_result_type, gnu_expr, |
a1ab4c31 AC |
6979 | No_Truncation (gnat_node)); |
6980 | break; | |
6981 | ||
6982 | case N_In: | |
6983 | case N_Not_In: | |
6984 | { | |
da49a783 | 6985 | tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node)); |
da49a783 | 6986 | tree gnu_low, gnu_high; |
a1ab4c31 | 6987 | |
3eaec285 | 6988 | Range_to_gnu (Right_Opnd (gnat_node), &gnu_low, &gnu_high); |
a1ab4c31 AC |
6989 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6990 | ||
825da0d2 EB |
6991 | tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_obj)); |
6992 | if (TREE_TYPE (gnu_obj) != gnu_op_type) | |
6993 | { | |
6994 | gnu_obj = convert (gnu_op_type, gnu_obj); | |
6995 | gnu_low = convert (gnu_op_type, gnu_low); | |
6996 | gnu_high = convert (gnu_op_type, gnu_high); | |
6997 | } | |
6998 | ||
da49a783 EB |
6999 | /* If LOW and HIGH are identical, perform an equality test. Otherwise, |
7000 | ensure that GNU_OBJ is evaluated only once and perform a full range | |
7001 | test. */ | |
a1ab4c31 | 7002 | if (operand_equal_p (gnu_low, gnu_high, 0)) |
da49a783 EB |
7003 | gnu_result |
7004 | = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low); | |
a1ab4c31 AC |
7005 | else |
7006 | { | |
da49a783 | 7007 | tree t1, t2; |
7d7a1fe8 | 7008 | gnu_obj = gnat_protect_expr (gnu_obj); |
da49a783 EB |
7009 | t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low); |
7010 | if (EXPR_P (t1)) | |
7011 | set_expr_location_from_node (t1, gnat_node); | |
7012 | t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high); | |
7013 | if (EXPR_P (t2)) | |
7014 | set_expr_location_from_node (t2, gnat_node); | |
a1ab4c31 | 7015 | gnu_result |
da49a783 | 7016 | = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2); |
a1ab4c31 AC |
7017 | } |
7018 | ||
3f13dd77 | 7019 | if (kind == N_Not_In) |
658a41ac EB |
7020 | gnu_result |
7021 | = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result); | |
a1ab4c31 AC |
7022 | } |
7023 | break; | |
7024 | ||
7025 | case N_Op_Divide: | |
7026 | gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node)); | |
7027 | gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7028 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7029 | gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type) | |
7030 | ? RDIV_EXPR | |
7031 | : (Rounded_Result (gnat_node) | |
7032 | ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR), | |
7033 | gnu_result_type, gnu_lhs, gnu_rhs); | |
a80e0583 EB |
7034 | /* If the result type is larger than a word, then declare the dependence |
7035 | on the libgcc routine. */ | |
7036 | if (INTEGRAL_TYPE_P (gnu_result_type) | |
7037 | && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
7038 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
a1ab4c31 AC |
7039 | break; |
7040 | ||
9a1bdc31 EB |
7041 | case N_Op_Eq: |
7042 | case N_Op_Ne: | |
7043 | case N_Op_Lt: | |
7044 | case N_Op_Le: | |
7045 | case N_Op_Gt: | |
7046 | case N_Op_Ge: | |
7047 | case N_Op_Add: | |
7048 | case N_Op_Subtract: | |
7049 | case N_Op_Multiply: | |
7050 | case N_Op_Mod: | |
7051 | case N_Op_Rem: | |
a1ab4c31 AC |
7052 | case N_Op_Rotate_Left: |
7053 | case N_Op_Rotate_Right: | |
7054 | case N_Op_Shift_Left: | |
7055 | case N_Op_Shift_Right: | |
7056 | case N_Op_Shift_Right_Arithmetic: | |
22903f22 EB |
7057 | case N_Op_And: |
7058 | case N_Op_Or: | |
7059 | case N_Op_Xor: | |
9a1bdc31 EB |
7060 | case N_And_Then: |
7061 | case N_Or_Else: | |
a1ab4c31 | 7062 | { |
3f13dd77 | 7063 | enum tree_code code = gnu_codes[kind]; |
a1ab4c31 | 7064 | bool ignore_lhs_overflow = false; |
1fc24649 | 7065 | location_t saved_location = input_location; |
73cd514a | 7066 | tree gnu_type, gnu_max_shift = NULL_TREE; |
a1ab4c31 | 7067 | |
22903f22 EB |
7068 | /* Fix operations set up for boolean types in GNU_CODES above. */ |
7069 | if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node)))) | |
7070 | switch (kind) | |
7071 | { | |
7072 | case N_Op_And: | |
7073 | code = BIT_AND_EXPR; | |
7074 | break; | |
7075 | case N_Op_Or: | |
7076 | code = BIT_IOR_EXPR; | |
7077 | break; | |
7078 | case N_Op_Xor: | |
7079 | code = BIT_XOR_EXPR; | |
7080 | break; | |
7081 | default: | |
7082 | break; | |
7083 | } | |
7084 | ||
a1ab4c31 AC |
7085 | gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node)); |
7086 | gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7087 | gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7088 | ||
73cd514a EB |
7089 | /* If this is a shift, take the count as unsigned since that is what |
7090 | most machines do and will generate simpler adjustments below. */ | |
7091 | if (IN (kind, N_Op_Shift)) | |
7092 | { | |
7093 | tree gnu_count_type | |
7094 | = gnat_unsigned_type_for (get_base_type (TREE_TYPE (gnu_rhs))); | |
7095 | gnu_rhs = convert (gnu_count_type, gnu_rhs); | |
7096 | gnu_max_shift | |
7097 | = convert (TREE_TYPE (gnu_rhs), TYPE_SIZE (gnu_type)); | |
a80e0583 EB |
7098 | /* If the result type is larger than a word, then declare the dependence |
7099 | on the libgcc routine. */ | |
7100 | if (TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
7101 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
73cd514a EB |
7102 | } |
7103 | ||
1f3f64b9 EB |
7104 | /* If this is a comparison between (potentially) large aggregates, then |
7105 | declare the dependence on the memcmp routine. */ | |
7106 | else if ((kind == N_Op_Eq || kind == N_Op_Ne) | |
7107 | && AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs)) | |
7108 | && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs))) | |
7109 | || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)), | |
7110 | 2 * BITS_PER_WORD) > 0)) | |
7111 | Check_Restriction_No_Dependence_On_System (Name_Memory_Compare, | |
7112 | gnat_node); | |
7113 | ||
a80e0583 EB |
7114 | /* If this is a modulo/remainder and the result type is larger than a |
7115 | word, then declare the dependence on the libgcc routine. */ | |
7116 | else if ((kind == N_Op_Mod ||kind == N_Op_Rem) | |
7117 | && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
7118 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
7119 | ||
7948ae37 | 7120 | /* Pending generic support for efficient vector logical operations in |
1f3f64b9 | 7121 | GCC, convert vectors to their representative array type view. */ |
7948ae37 OH |
7122 | gnu_lhs = maybe_vector_array (gnu_lhs); |
7123 | gnu_rhs = maybe_vector_array (gnu_rhs); | |
7124 | ||
43a4dd82 EB |
7125 | /* If this is a comparison operator, convert any references to an |
7126 | unconstrained array value into a reference to the actual array. */ | |
a1ab4c31 AC |
7127 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
7128 | { | |
7129 | gnu_lhs = maybe_unconstrained_array (gnu_lhs); | |
7130 | gnu_rhs = maybe_unconstrained_array (gnu_rhs); | |
825da0d2 EB |
7131 | |
7132 | tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_lhs)); | |
7133 | if (TREE_TYPE (gnu_lhs) != gnu_op_type) | |
7134 | { | |
7135 | gnu_lhs = convert (gnu_op_type, gnu_lhs); | |
7136 | gnu_rhs = convert (gnu_op_type, gnu_rhs); | |
7137 | } | |
a1ab4c31 AC |
7138 | } |
7139 | ||
a1ab4c31 AC |
7140 | /* If this is a shift whose count is not guaranteed to be correct, |
7141 | we need to adjust the shift count. */ | |
73cd514a EB |
7142 | if ((kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right) |
7143 | && !Shift_Count_OK (gnat_node)) | |
7144 | gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, TREE_TYPE (gnu_rhs), | |
7145 | gnu_rhs, gnu_max_shift); | |
7146 | else if (kind == N_Op_Shift_Right_Arithmetic | |
7147 | && !Shift_Count_OK (gnat_node)) | |
7148 | gnu_rhs | |
7149 | = build_binary_op (MIN_EXPR, TREE_TYPE (gnu_rhs), | |
7150 | build_binary_op (MINUS_EXPR, | |
7151 | TREE_TYPE (gnu_rhs), | |
7152 | gnu_max_shift, | |
7153 | build_int_cst | |
7154 | (TREE_TYPE (gnu_rhs), 1)), | |
7155 | gnu_rhs); | |
a1ab4c31 AC |
7156 | |
7157 | /* For right shifts, the type says what kind of shift to do, | |
7158 | so we may need to choose a different type. In this case, | |
7159 | we have to ignore integer overflow lest it propagates all | |
7160 | the way down and causes a CE to be explicitly raised. */ | |
3f13dd77 | 7161 | if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type)) |
a1ab4c31 | 7162 | { |
9a1bdc31 | 7163 | gnu_type = gnat_unsigned_type_for (gnu_type); |
a1ab4c31 AC |
7164 | ignore_lhs_overflow = true; |
7165 | } | |
3f13dd77 | 7166 | else if (kind == N_Op_Shift_Right_Arithmetic |
a1ab4c31 AC |
7167 | && TYPE_UNSIGNED (gnu_type)) |
7168 | { | |
9a1bdc31 | 7169 | gnu_type = gnat_signed_type_for (gnu_type); |
a1ab4c31 AC |
7170 | ignore_lhs_overflow = true; |
7171 | } | |
7172 | ||
7173 | if (gnu_type != gnu_result_type) | |
7174 | { | |
7175 | tree gnu_old_lhs = gnu_lhs; | |
7176 | gnu_lhs = convert (gnu_type, gnu_lhs); | |
7177 | if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow) | |
7178 | TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs); | |
7179 | gnu_rhs = convert (gnu_type, gnu_rhs); | |
1f8fc1f4 EB |
7180 | if (gnu_max_shift) |
7181 | gnu_max_shift = convert (gnu_type, gnu_max_shift); | |
a1ab4c31 AC |
7182 | } |
7183 | ||
f552aa62 EB |
7184 | /* For signed integer addition, subtraction and multiplication, do an |
7185 | overflow check if required. */ | |
9a1bdc31 | 7186 | if (Do_Overflow_Check (gnat_node) |
9a1bdc31 | 7187 | && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR) |
b666e568 GB |
7188 | && !TYPE_UNSIGNED (gnu_type) |
7189 | && !FLOAT_TYPE_P (gnu_type)) | |
f552aa62 EB |
7190 | gnu_result |
7191 | = build_binary_op_trapv (code, gnu_type, gnu_lhs, gnu_rhs, | |
7192 | gnat_node); | |
b666e568 | 7193 | else |
1fc24649 EB |
7194 | { |
7195 | /* Some operations, e.g. comparisons of arrays, generate complex | |
7196 | trees that need to be annotated while they are being built. */ | |
7197 | input_location = saved_location; | |
7198 | gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs); | |
7199 | } | |
a1ab4c31 AC |
7200 | |
7201 | /* If this is a logical shift with the shift count not verified, | |
7202 | we must return zero if it is too large. We cannot compensate | |
73cd514a | 7203 | beforehand in this case. */ |
3f13dd77 | 7204 | if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right) |
a1ab4c31 AC |
7205 | && !Shift_Count_OK (gnat_node)) |
7206 | gnu_result | |
73cd514a EB |
7207 | = build_cond_expr (gnu_type, |
7208 | build_binary_op (GE_EXPR, boolean_type_node, | |
7209 | gnu_rhs, gnu_max_shift), | |
7210 | build_int_cst (gnu_type, 0), | |
7211 | gnu_result); | |
a1ab4c31 AC |
7212 | } |
7213 | break; | |
7214 | ||
9b16cb57 | 7215 | case N_If_Expression: |
a1ab4c31 | 7216 | { |
1e17ef87 EB |
7217 | tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node))); |
7218 | tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node)))); | |
7219 | tree gnu_false | |
7220 | = gnat_to_gnu (Next (Next (First (Expressions (gnat_node))))); | |
a1ab4c31 AC |
7221 | |
7222 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
3f13dd77 EB |
7223 | gnu_result |
7224 | = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false); | |
a1ab4c31 AC |
7225 | } |
7226 | break; | |
7227 | ||
7228 | case N_Op_Plus: | |
7229 | gnu_result = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7230 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7231 | break; | |
7232 | ||
7233 | case N_Op_Not: | |
7234 | /* This case can apply to a boolean or a modular type. | |
7235 | Fall through for a boolean operand since GNU_CODES is set | |
7236 | up to handle this. */ | |
43a4dd82 | 7237 | if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node)))) |
a1ab4c31 AC |
7238 | { |
7239 | gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7240 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7241 | gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type, | |
7242 | gnu_expr); | |
7243 | break; | |
7244 | } | |
7245 | ||
9c453de7 | 7246 | /* ... fall through ... */ |
a1ab4c31 | 7247 | |
9a1bdc31 EB |
7248 | case N_Op_Minus: |
7249 | case N_Op_Abs: | |
a1ab4c31 | 7250 | gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node)); |
fd6e497e | 7251 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
a1ab4c31 | 7252 | |
f552aa62 EB |
7253 | /* For signed integer negation and absolute value, do an overflow check |
7254 | if required. */ | |
b666e568 GB |
7255 | if (Do_Overflow_Check (gnat_node) |
7256 | && !TYPE_UNSIGNED (gnu_result_type) | |
7257 | && !FLOAT_TYPE_P (gnu_result_type)) | |
10069d53 | 7258 | gnu_result |
f552aa62 EB |
7259 | = build_unary_op_trapv (gnu_codes[kind], gnu_result_type, gnu_expr, |
7260 | gnat_node); | |
b666e568 | 7261 | else |
f552aa62 EB |
7262 | gnu_result |
7263 | = build_unary_op (gnu_codes[kind], gnu_result_type, gnu_expr); | |
a1ab4c31 AC |
7264 | break; |
7265 | ||
7266 | case N_Allocator: | |
7267 | { | |
d63fbcf8 EB |
7268 | tree gnu_type, gnu_init; |
7269 | bool ignore_init_type; | |
a1ab4c31 AC |
7270 | |
7271 | gnat_temp = Expression (gnat_node); | |
7272 | ||
815b5368 EB |
7273 | /* The expression can be either an N_Identifier or an Expanded_Name, |
7274 | which must represent a type, or a N_Qualified_Expression, which | |
7275 | contains both the type and an initial value for the object. */ | |
a1ab4c31 AC |
7276 | if (Nkind (gnat_temp) == N_Identifier |
7277 | || Nkind (gnat_temp) == N_Expanded_Name) | |
d63fbcf8 EB |
7278 | { |
7279 | ignore_init_type = false; | |
7280 | gnu_init = NULL_TREE; | |
7281 | gnu_type = gnat_to_gnu_type (Entity (gnat_temp)); | |
7282 | } | |
7283 | ||
a1ab4c31 AC |
7284 | else if (Nkind (gnat_temp) == N_Qualified_Expression) |
7285 | { | |
bb24f343 | 7286 | const Entity_Id gnat_desig_type |
a1ab4c31 AC |
7287 | = Designated_Type (Underlying_Type (Etype (gnat_node))); |
7288 | ||
ce2d0ce2 | 7289 | ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type); |
a1ab4c31 | 7290 | |
d63fbcf8 | 7291 | gnu_init = gnat_to_gnu (Expression (gnat_temp)); |
a1ab4c31 | 7292 | gnu_init = maybe_unconstrained_array (gnu_init); |
815b5368 EB |
7293 | |
7294 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_temp))); | |
a1ab4c31 AC |
7295 | |
7296 | if (Is_Elementary_Type (gnat_desig_type) | |
7297 | || Is_Constrained (gnat_desig_type)) | |
0029bafd | 7298 | gnu_type = gnat_to_gnu_type (gnat_desig_type); |
a1ab4c31 AC |
7299 | else |
7300 | { | |
7301 | gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp))); | |
7302 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
7303 | gnu_type = TREE_TYPE (gnu_init); | |
a1ab4c31 | 7304 | } |
0029bafd EB |
7305 | |
7306 | /* See the N_Qualified_Expression case for the rationale. */ | |
7307 | if (Is_Tagged_Type (gnat_desig_type)) | |
7308 | used_types_insert (gnu_type); | |
7309 | ||
7310 | gnu_init = convert (gnu_type, gnu_init); | |
a1ab4c31 AC |
7311 | } |
7312 | else | |
7313 | gcc_unreachable (); | |
7314 | ||
7315 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7316 | return build_allocator (gnu_type, gnu_init, gnu_result_type, | |
7317 | Procedure_To_Call (gnat_node), | |
7318 | Storage_Pool (gnat_node), gnat_node, | |
7319 | ignore_init_type); | |
7320 | } | |
7321 | break; | |
7322 | ||
1e17ef87 EB |
7323 | /**************************/ |
7324 | /* Chapter 5: Statements */ | |
7325 | /**************************/ | |
a1ab4c31 AC |
7326 | |
7327 | case N_Label: | |
7328 | gnu_result = build1 (LABEL_EXPR, void_type_node, | |
7329 | gnat_to_gnu (Identifier (gnat_node))); | |
7330 | break; | |
7331 | ||
7332 | case N_Null_Statement: | |
9c69c3af EB |
7333 | /* When not optimizing, turn null statements from source into gotos to |
7334 | the next statement that the middle-end knows how to preserve. */ | |
7335 | if (!optimize && Comes_From_Source (gnat_node)) | |
7336 | { | |
88a94e2b EB |
7337 | tree stmt, label = create_label_decl (NULL_TREE, gnat_node); |
7338 | DECL_IGNORED_P (label) = 1; | |
9c69c3af EB |
7339 | start_stmt_group (); |
7340 | stmt = build1 (GOTO_EXPR, void_type_node, label); | |
7341 | set_expr_location_from_node (stmt, gnat_node); | |
7342 | add_stmt (stmt); | |
7343 | stmt = build1 (LABEL_EXPR, void_type_node, label); | |
7344 | set_expr_location_from_node (stmt, gnat_node); | |
7345 | add_stmt (stmt); | |
7346 | gnu_result = end_stmt_group (); | |
7347 | } | |
7348 | else | |
7349 | gnu_result = alloc_stmt_list (); | |
a1ab4c31 AC |
7350 | break; |
7351 | ||
7352 | case N_Assignment_Statement: | |
0c5730a6 | 7353 | /* First get the LHS of the statement and convert any reference to an |
0b3467c4 | 7354 | unconstrained array into a reference to the underlying array. */ |
a1ab4c31 AC |
7355 | gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node))); |
7356 | ||
7357 | /* If the type has a size that overflows, convert this into raise of | |
7358 | Storage_Error: execution shouldn't have gotten here anyway. */ | |
7359 | if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST | |
ce3da0d0 | 7360 | && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs)))) |
a1ab4c31 AC |
7361 | gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node, |
7362 | N_Raise_Storage_Error); | |
0c5730a6 EB |
7363 | |
7364 | /* If the RHS is a function call, let Call_to_gnu do the entire work. */ | |
0b3467c4 | 7365 | else if (Nkind (Expression (gnat_node)) == N_Function_Call) |
f797c2b7 | 7366 | { |
17a98a3d | 7367 | get_atomic_access (Name (gnat_node), &aa_type, &aa_sync); |
544d14e1 | 7368 | get_storage_model_access (Name (gnat_node), &gnat_smo); |
f797c2b7 EB |
7369 | gnu_result |
7370 | = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs, | |
544d14e1 | 7371 | aa_type, aa_sync, gnat_smo); |
f797c2b7 | 7372 | } |
0c5730a6 EB |
7373 | |
7374 | /* Otherwise we need to build the assignment statement manually. */ | |
a1ab4c31 AC |
7375 | else |
7376 | { | |
d5518dab | 7377 | const Node_Id gnat_name = Name (gnat_node); |
ac43e11e | 7378 | const Node_Id gnat_expr = Expression (gnat_node); |
b9364a56 EB |
7379 | const Node_Id gnat_inner |
7380 | = Nkind (gnat_expr) == N_Qualified_Expression | |
7381 | ? Expression (gnat_expr) | |
7382 | : gnat_expr; | |
d5518dab | 7383 | const Entity_Id gnat_type = Underlying_Type (Etype (gnat_name)); |
ac43e11e | 7384 | const bool use_memset_p |
95c9c1c0 | 7385 | = Is_Array_Type (gnat_type) |
b9364a56 | 7386 | && Nkind (gnat_inner) == N_Aggregate |
27c3d986 | 7387 | && Is_Single_Aggregate (gnat_inner); |
ac43e11e | 7388 | |
b9364a56 | 7389 | /* If we use memset, we need to find the innermost expression. */ |
ac43e11e AC |
7390 | if (use_memset_p) |
7391 | { | |
b9364a56 EB |
7392 | gnat_temp = gnat_inner; |
7393 | do { | |
7394 | gnat_temp | |
7395 | = Expression (First (Component_Associations (gnat_temp))); | |
7396 | } while (Nkind (gnat_temp) == N_Aggregate | |
27c3d986 | 7397 | && Is_Single_Aggregate (gnat_temp)); |
b9364a56 | 7398 | gnu_rhs = gnat_to_gnu (gnat_temp); |
ac43e11e | 7399 | } |
0c5730a6 EB |
7400 | |
7401 | /* Otherwise get the RHS of the statement and do the same processing | |
7402 | as for the LHS above. */ | |
ac43e11e AC |
7403 | else |
7404 | gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr)); | |
a1ab4c31 | 7405 | |
815b5368 | 7406 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 | 7407 | |
d5518dab EB |
7408 | get_atomic_access (gnat_name, &aa_type, &aa_sync); |
7409 | get_storage_model_access (gnat_name, &gnat_smo); | |
17a98a3d | 7410 | |
f797c2b7 EB |
7411 | /* If an outer atomic access is required on the LHS, build the load- |
7412 | modify-store sequence. */ | |
17a98a3d | 7413 | if (aa_type == OUTER_ATOMIC) |
f797c2b7 EB |
7414 | gnu_result = build_load_modify_store (gnu_lhs, gnu_rhs, gnat_node); |
7415 | ||
17a98a3d EB |
7416 | /* Or else, if a simple atomic access is required, build the atomic |
7417 | store. */ | |
7418 | else if (aa_type == SIMPLE_ATOMIC) | |
7419 | gnu_result = build_atomic_store (gnu_lhs, gnu_rhs, aa_sync); | |
ac43e11e | 7420 | |
544d14e1 EB |
7421 | /* Or else, if a storage model access is required, build the special |
7422 | store. */ | |
7423 | else if (Present (gnat_smo) | |
7424 | && Present (Storage_Model_Copy_To (gnat_smo))) | |
7425 | { | |
d5518dab EB |
7426 | tree gnu_size; |
7427 | ||
7f77aa6b EB |
7428 | /* We obviously cannot use memset in this case. */ |
7429 | gcc_assert (!use_memset_p); | |
7430 | ||
d5518dab EB |
7431 | /* If this is a dereference with a special dynamic constrained |
7432 | subtype on the node, use it to compute the size. */ | |
7433 | if (Nkind (gnat_name) == N_Explicit_Dereference | |
7434 | && Present (Actual_Designated_Subtype (gnat_name))) | |
7435 | { | |
7436 | tree gnu_actual_obj_type | |
7437 | = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_name)); | |
7438 | gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type); | |
7439 | } | |
7440 | else | |
7441 | gnu_size = NULL_TREE; | |
544d14e1 | 7442 | |
2d4883a1 | 7443 | gnu_result |
d5518dab EB |
7444 | = build_storage_model_store (gnat_smo, gnu_lhs, gnu_rhs, |
7445 | gnu_size); | |
544d14e1 EB |
7446 | } |
7447 | ||
df8aa2b7 EB |
7448 | /* Or else, use memset when the conditions are met. This has already |
7449 | been validated by Aggr_Assignment_OK_For_Backend in the front-end | |
7450 | and the RHS is thus guaranteed to be of the appropriate form. */ | |
ac43e11e AC |
7451 | else if (use_memset_p) |
7452 | { | |
333e4f86 AC |
7453 | tree value |
7454 | = real_zerop (gnu_rhs) | |
7455 | ? integer_zero_node | |
7456 | : fold_convert (integer_type_node, gnu_rhs); | |
e54b6268 | 7457 | tree dest = build_fold_addr_expr (gnu_lhs); |
881cdd61 | 7458 | tree t = builtin_decl_explicit (BUILT_IN_MEMSET); |
e54b6268 EB |
7459 | /* Be extra careful not to write too much data. */ |
7460 | tree size; | |
7461 | if (TREE_CODE (gnu_lhs) == COMPONENT_REF) | |
7462 | size = DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs, 1)); | |
7463 | else if (DECL_P (gnu_lhs)) | |
7464 | size = DECL_SIZE_UNIT (gnu_lhs); | |
7465 | else | |
7466 | size = TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs)); | |
7467 | size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_lhs); | |
333e4f86 | 7468 | if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value)) |
ac43e11e AC |
7469 | { |
7470 | tree mask | |
7471 | = build_int_cst (integer_type_node, | |
7472 | ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1); | |
7473 | value = int_const_binop (BIT_AND_EXPR, value, mask); | |
7474 | } | |
e54b6268 | 7475 | gnu_result = build_call_expr (t, 3, dest, value, size); |
1f3f64b9 EB |
7476 | Check_Restriction_No_Dependence_On_System (Name_Memory_Set, |
7477 | gnat_node); | |
ac43e11e AC |
7478 | } |
7479 | ||
033ba5bf | 7480 | else |
544d14e1 EB |
7481 | { |
7482 | tree t = remove_conversions (gnu_rhs, false); | |
7483 | ||
7484 | /* If a storage model load is present on the RHS, then elide the | |
7485 | temporary associated with it. */ | |
7486 | if (TREE_CODE (t) == LOAD_EXPR) | |
7487 | { | |
7488 | gnu_result = TREE_OPERAND (t, 1); | |
7489 | gcc_assert (TREE_CODE (gnu_result) == CALL_EXPR); | |
7490 | ||
7491 | tree arg = CALL_EXPR_ARG (gnu_result, 1); | |
7492 | CALL_EXPR_ARG (gnu_result, 1) | |
7493 | = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), gnu_lhs); | |
7494 | } | |
7495 | ||
7496 | /* Otherwise build a regular assignment. */ | |
7497 | else | |
7498 | gnu_result | |
7499 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs); | |
7500 | } | |
8b659f79 | 7501 | |
ac43e11e | 7502 | /* If the assignment type is a regular array and the two sides are |
82d6f532 EB |
7503 | not completely disjoint, play safe and use memmove. But don't do |
7504 | it for a bit-packed array as it might not be byte-aligned. */ | |
8b659f79 | 7505 | if (TREE_CODE (gnu_result) == MODIFY_EXPR |
95c9c1c0 EB |
7506 | && Is_Array_Type (gnat_type) |
7507 | && !Is_Bit_Packed_Array (gnat_type) | |
8b659f79 EB |
7508 | && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node))) |
7509 | { | |
ac43e11e AC |
7510 | tree to = TREE_OPERAND (gnu_result, 0); |
7511 | tree from = TREE_OPERAND (gnu_result, 1); | |
7512 | tree type = TREE_TYPE (from); | |
7513 | tree size | |
7514 | = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from); | |
7515 | tree to_ptr = build_fold_addr_expr (to); | |
7516 | tree from_ptr = build_fold_addr_expr (from); | |
881cdd61 | 7517 | tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE); |
8b659f79 | 7518 | gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size); |
1f3f64b9 EB |
7519 | Check_Restriction_No_Dependence_On_System (Name_Memory_Move, |
7520 | gnat_node); | |
8b659f79 | 7521 | } |
1f3f64b9 EB |
7522 | |
7523 | /* If this is an assignment between (potentially) large aggregates, | |
7524 | then declare the dependence on the memcpy routine. */ | |
7525 | else if (AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs)) | |
7526 | && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs))) | |
7527 | || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)), | |
7528 | 2 * BITS_PER_WORD) > 0)) | |
7529 | Check_Restriction_No_Dependence_On_System (Name_Memory_Copy, | |
7530 | gnat_node); | |
a1ab4c31 AC |
7531 | } |
7532 | break; | |
7533 | ||
7534 | case N_If_Statement: | |
7535 | { | |
1e17ef87 | 7536 | tree *gnu_else_ptr; /* Point to put next "else if" or "else". */ |
a1ab4c31 AC |
7537 | |
7538 | /* Make the outer COND_EXPR. Avoid non-determinism. */ | |
7539 | gnu_result = build3 (COND_EXPR, void_type_node, | |
7540 | gnat_to_gnu (Condition (gnat_node)), | |
7541 | NULL_TREE, NULL_TREE); | |
7542 | COND_EXPR_THEN (gnu_result) | |
7543 | = build_stmt_group (Then_Statements (gnat_node), false); | |
7544 | TREE_SIDE_EFFECTS (gnu_result) = 1; | |
7545 | gnu_else_ptr = &COND_EXPR_ELSE (gnu_result); | |
7546 | ||
7547 | /* Now make a COND_EXPR for each of the "else if" parts. Put each | |
7548 | into the previous "else" part and point to where to put any | |
7549 | outer "else". Also avoid non-determinism. */ | |
7550 | if (Present (Elsif_Parts (gnat_node))) | |
7551 | for (gnat_temp = First (Elsif_Parts (gnat_node)); | |
7552 | Present (gnat_temp); gnat_temp = Next (gnat_temp)) | |
7553 | { | |
7554 | gnu_expr = build3 (COND_EXPR, void_type_node, | |
7555 | gnat_to_gnu (Condition (gnat_temp)), | |
7556 | NULL_TREE, NULL_TREE); | |
7557 | COND_EXPR_THEN (gnu_expr) | |
7558 | = build_stmt_group (Then_Statements (gnat_temp), false); | |
7559 | TREE_SIDE_EFFECTS (gnu_expr) = 1; | |
7560 | set_expr_location_from_node (gnu_expr, gnat_temp); | |
7561 | *gnu_else_ptr = gnu_expr; | |
7562 | gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr); | |
7563 | } | |
7564 | ||
7565 | *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false); | |
7566 | } | |
7567 | break; | |
7568 | ||
7569 | case N_Case_Statement: | |
7570 | gnu_result = Case_Statement_to_gnu (gnat_node); | |
7571 | break; | |
7572 | ||
7573 | case N_Loop_Statement: | |
7574 | gnu_result = Loop_Statement_to_gnu (gnat_node); | |
7575 | break; | |
7576 | ||
7577 | case N_Block_Statement: | |
a712b009 EB |
7578 | /* The only way to enter the block is to fall through to it. */ |
7579 | if (stmt_group_may_fallthru ()) | |
7580 | { | |
7581 | start_stmt_group (); | |
7582 | gnat_pushlevel (); | |
6d16658d | 7583 | process_decls (Declarations (gnat_node), Empty, true, true); |
a712b009 | 7584 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); |
6d16658d BD |
7585 | if (Present (At_End_Proc (gnat_node))) |
7586 | At_End_Proc_to_gnu (gnat_node); | |
a712b009 EB |
7587 | gnat_poplevel (); |
7588 | gnu_result = end_stmt_group (); | |
7589 | } | |
7590 | else | |
7591 | gnu_result = alloc_stmt_list (); | |
a1ab4c31 AC |
7592 | break; |
7593 | ||
7594 | case N_Exit_Statement: | |
7595 | gnu_result | |
7596 | = build2 (EXIT_STMT, void_type_node, | |
7597 | (Present (Condition (gnat_node)) | |
7598 | ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE), | |
7599 | (Present (Name (gnat_node)) | |
7600 | ? get_gnu_tree (Entity (Name (gnat_node))) | |
633a3f2a | 7601 | : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt))); |
a1ab4c31 AC |
7602 | break; |
7603 | ||
7640ef8a | 7604 | case N_Simple_Return_Statement: |
a1ab4c31 | 7605 | { |
f3d34576 | 7606 | tree gnu_ret_obj, gnu_ret_val; |
a1ab4c31 | 7607 | |
d47d0a8d EB |
7608 | /* If the subprogram is a function, we must return the expression. */ |
7609 | if (Present (Expression (gnat_node))) | |
a1ab4c31 | 7610 | { |
d47d0a8d | 7611 | tree gnu_subprog_type = TREE_TYPE (current_function_decl); |
d47d0a8d | 7612 | |
2374257a EB |
7613 | /* If this function has copy-in/copy-out parameters parameters and |
7614 | doesn't return by invisible reference, get the real object for | |
7615 | the return. See Subprogram_Body_to_gnu. */ | |
7616 | if (TYPE_CI_CO_LIST (gnu_subprog_type) | |
7617 | && !TREE_ADDRESSABLE (gnu_subprog_type)) | |
9771b263 | 7618 | gnu_ret_obj = gnu_return_var_stack->last (); |
f3d34576 EB |
7619 | else |
7620 | gnu_ret_obj = DECL_RESULT (current_function_decl); | |
7621 | ||
7622 | /* Get the GCC tree for the expression to be returned. */ | |
7623 | gnu_ret_val = gnat_to_gnu (Expression (gnat_node)); | |
35a382b8 | 7624 | |
d47d0a8d EB |
7625 | /* Do not remove the padding from GNU_RET_VAL if the inner type is |
7626 | self-referential since we want to allocate the fixed size. */ | |
7627 | if (TREE_CODE (gnu_ret_val) == COMPONENT_REF | |
fc7a823e | 7628 | && type_is_padding_self_referential |
d020df06 | 7629 | (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))) |
d47d0a8d EB |
7630 | gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0); |
7631 | ||
f3d34576 | 7632 | /* If the function returns by direct reference, return a pointer |
89e037d0 EB |
7633 | to the return value, possibly after allocating it. */ |
7634 | if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)) | |
a1ab4c31 | 7635 | { |
89e037d0 | 7636 | if (Present (Storage_Pool (gnat_node))) |
088b91c7 | 7637 | { |
89e037d0 EB |
7638 | gnu_ret_val = maybe_unconstrained_array (gnu_ret_val); |
7639 | ||
7640 | /* And find out whether it is a candidate for Named Return | |
fc84bb8c | 7641 | Value. If so, record it. */ |
89e037d0 EB |
7642 | if (optimize |
7643 | && !optimize_debug | |
7644 | && !TYPE_CI_CO_LIST (gnu_subprog_type)) | |
088b91c7 | 7645 | { |
89e037d0 EB |
7646 | tree ret_val = gnu_ret_val; |
7647 | ||
7648 | /* Strip conversions around the return value. */ | |
7649 | if (gnat_useless_type_conversion (ret_val)) | |
7650 | ret_val = TREE_OPERAND (ret_val, 0); | |
7651 | ||
7652 | /* Strip unpadding around the return value. */ | |
7653 | if (TREE_CODE (ret_val) == COMPONENT_REF | |
7654 | && TYPE_IS_PADDING_P | |
7655 | (TREE_TYPE (TREE_OPERAND (ret_val, 0)))) | |
7656 | ret_val = TREE_OPERAND (ret_val, 0); | |
7657 | ||
7658 | /* Now apply the test to the return value. */ | |
7659 | if (return_value_ok_for_nrv_p (NULL_TREE, ret_val)) | |
7660 | { | |
7661 | if (!f_named_ret_val) | |
7662 | f_named_ret_val = BITMAP_GGC_ALLOC (); | |
7663 | bitmap_set_bit (f_named_ret_val, | |
7664 | DECL_UID (ret_val)); | |
7665 | if (!f_gnat_ret) | |
7666 | f_gnat_ret = gnat_node; | |
7667 | } | |
088b91c7 | 7668 | } |
89e037d0 EB |
7669 | |
7670 | gnu_ret_val | |
7671 | = build_allocator (TREE_TYPE (gnu_ret_val), | |
7672 | gnu_ret_val, | |
7673 | TREE_TYPE (gnu_ret_obj), | |
7674 | Procedure_To_Call (gnat_node), | |
7675 | Storage_Pool (gnat_node), | |
7676 | gnat_node, | |
7677 | false); | |
088b91c7 EB |
7678 | } |
7679 | ||
89e037d0 EB |
7680 | else |
7681 | gnu_ret_val | |
7682 | = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val); | |
a1ab4c31 | 7683 | } |
d47d0a8d | 7684 | |
0d24bf76 | 7685 | /* Otherwise, if it returns by invisible reference, dereference |
d47d0a8d EB |
7686 | the pointer it is passed using the type of the return value |
7687 | and build the copy operation manually. This ensures that we | |
7688 | don't copy too much data, for example if the return type is | |
7689 | unconstrained with a maximum size. */ | |
0d24bf76 | 7690 | else if (TREE_ADDRESSABLE (gnu_subprog_type)) |
a1ab4c31 | 7691 | { |
f3d34576 | 7692 | tree gnu_ret_deref |
d47d0a8d | 7693 | = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val), |
f3d34576 | 7694 | gnu_ret_obj); |
b24f7345 | 7695 | gnu_result = build2 (INIT_EXPR, void_type_node, |
2374257a | 7696 | gnu_ret_deref, gnu_ret_val); |
c697f593 | 7697 | /* Avoid a useless copy with __builtin_return_slot. */ |
04682fe7 | 7698 | if (INDIRECT_REF_P (gnu_ret_val)) |
c697f593 EB |
7699 | gnu_result |
7700 | = build3 (COND_EXPR, void_type_node, | |
7701 | fold_build2 (NE_EXPR, boolean_type_node, | |
7702 | TREE_OPERAND (gnu_ret_val, 0), | |
7703 | gnu_ret_obj), | |
7704 | gnu_result, NULL_TREE); | |
d47d0a8d EB |
7705 | add_stmt_with_node (gnu_result, gnat_node); |
7706 | gnu_ret_val = NULL_TREE; | |
a1ab4c31 AC |
7707 | } |
7708 | } | |
0d24bf76 | 7709 | |
a1ab4c31 | 7710 | else |
0d24bf76 | 7711 | gnu_ret_obj = gnu_ret_val = NULL_TREE; |
a1ab4c31 | 7712 | |
35a382b8 EB |
7713 | /* If we have a return label defined, convert this into a branch to |
7714 | that label. The return proper will be handled elsewhere. */ | |
9771b263 | 7715 | if (gnu_return_label_stack->last ()) |
35a382b8 | 7716 | { |
2374257a | 7717 | if (gnu_ret_val) |
b1cd7461 EB |
7718 | add_stmt_with_node (build_binary_op (MODIFY_EXPR, |
7719 | NULL_TREE, gnu_ret_obj, | |
7720 | gnu_ret_val), | |
7721 | gnat_node); | |
35a382b8 EB |
7722 | |
7723 | gnu_result = build1 (GOTO_EXPR, void_type_node, | |
9771b263 | 7724 | gnu_return_label_stack->last ()); |
f3d34576 | 7725 | |
35a382b8 EB |
7726 | /* When not optimizing, make sure the return is preserved. */ |
7727 | if (!optimize && Comes_From_Source (gnat_node)) | |
9771b263 | 7728 | DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0; |
35a382b8 EB |
7729 | } |
7730 | ||
f3d34576 EB |
7731 | /* Otherwise, build a regular return. */ |
7732 | else | |
7733 | gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val); | |
a1ab4c31 AC |
7734 | } |
7735 | break; | |
7736 | ||
7737 | case N_Goto_Statement: | |
8f8f531f PMR |
7738 | gnu_expr = gnat_to_gnu (Name (gnat_node)); |
7739 | gnu_result = build1 (GOTO_EXPR, void_type_node, gnu_expr); | |
7740 | TREE_USED (gnu_expr) = 1; | |
a1ab4c31 AC |
7741 | break; |
7742 | ||
1e17ef87 EB |
7743 | /***************************/ |
7744 | /* Chapter 6: Subprograms */ | |
7745 | /***************************/ | |
a1ab4c31 AC |
7746 | |
7747 | case N_Subprogram_Declaration: | |
d5ebeb8c EB |
7748 | /* Unless there is a freeze node, declare the entity. We consider |
7749 | this a definition even though we're not generating code for the | |
7750 | subprogram because we will be making the corresponding GCC node. | |
7751 | When there is a freeze node, it is considered the definition of | |
7752 | the subprogram and we do nothing until after it is encountered. | |
7753 | That's an efficiency issue: the types involved in the profile | |
7754 | are far more likely to be frozen between the declaration and | |
7755 | the freeze node than before the declaration, so we save some | |
7756 | updates of the GCC node by waiting until the freeze node. | |
7757 | The counterpart is that we assume that there is no reference | |
7758 | to the subprogram between the declaration and the freeze node | |
7759 | in the expanded code; otherwise, it will be interpreted as an | |
7760 | external reference and very likely give rise to a link failure. */ | |
a1ab4c31 AC |
7761 | if (No (Freeze_Node (Defining_Entity (Specification (gnat_node))))) |
7762 | gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)), | |
afc737f0 | 7763 | NULL_TREE, true); |
a1ab4c31 AC |
7764 | gnu_result = alloc_stmt_list (); |
7765 | break; | |
7766 | ||
7767 | case N_Abstract_Subprogram_Declaration: | |
7768 | /* This subprogram doesn't exist for code generation purposes, but we | |
7769 | have to elaborate the types of any parameters and result, unless | |
76e3504f | 7770 | they are imported types (nothing to generate in this case). |
a1ab4c31 | 7771 | |
76e3504f AC |
7772 | The parameter list may contain types with freeze nodes, e.g. not null |
7773 | subtypes, so the subprogram itself may carry a freeze node, in which | |
7774 | case its elaboration must be deferred. */ | |
a1ab4c31 | 7775 | |
76e3504f AC |
7776 | /* Process the parameter types first. */ |
7777 | if (No (Freeze_Node (Defining_Entity (Specification (gnat_node))))) | |
a1ab4c31 AC |
7778 | for (gnat_temp |
7779 | = First_Formal_With_Extras | |
7780 | (Defining_Entity (Specification (gnat_node))); | |
7781 | Present (gnat_temp); | |
7782 | gnat_temp = Next_Formal_With_Extras (gnat_temp)) | |
7783 | if (Is_Itype (Etype (gnat_temp)) | |
7b56a91b | 7784 | && !From_Limited_With (Etype (gnat_temp))) |
afc737f0 | 7785 | gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false); |
a1ab4c31 | 7786 | |
a1ab4c31 | 7787 | /* Then the result type, set to Standard_Void_Type for procedures. */ |
a1ab4c31 AC |
7788 | { |
7789 | Entity_Id gnat_temp_type | |
7790 | = Etype (Defining_Entity (Specification (gnat_node))); | |
7791 | ||
7b56a91b | 7792 | if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type)) |
afc737f0 | 7793 | gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, false); |
a1ab4c31 AC |
7794 | } |
7795 | ||
7796 | gnu_result = alloc_stmt_list (); | |
7797 | break; | |
7798 | ||
7799 | case N_Defining_Program_Unit_Name: | |
1e17ef87 EB |
7800 | /* For a child unit identifier go up a level to get the specification. |
7801 | We get this when we try to find the spec of a child unit package | |
7802 | that is the compilation unit being compiled. */ | |
a1ab4c31 AC |
7803 | gnu_result = gnat_to_gnu (Parent (gnat_node)); |
7804 | break; | |
7805 | ||
7806 | case N_Subprogram_Body: | |
7807 | Subprogram_Body_to_gnu (gnat_node); | |
7808 | gnu_result = alloc_stmt_list (); | |
7809 | break; | |
7810 | ||
7811 | case N_Function_Call: | |
7812 | case N_Procedure_Call_Statement: | |
f797c2b7 | 7813 | gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, |
544d14e1 | 7814 | NOT_ATOMIC, false, Empty); |
a1ab4c31 AC |
7815 | break; |
7816 | ||
1e17ef87 EB |
7817 | /************************/ |
7818 | /* Chapter 7: Packages */ | |
7819 | /************************/ | |
a1ab4c31 AC |
7820 | |
7821 | case N_Package_Declaration: | |
7822 | gnu_result = gnat_to_gnu (Specification (gnat_node)); | |
7823 | break; | |
7824 | ||
7825 | case N_Package_Specification: | |
a1ab4c31 AC |
7826 | start_stmt_group (); |
7827 | process_decls (Visible_Declarations (gnat_node), | |
6d16658d BD |
7828 | Private_Declarations (gnat_node), |
7829 | true, true); | |
a1ab4c31 AC |
7830 | gnu_result = end_stmt_group (); |
7831 | break; | |
7832 | ||
7833 | case N_Package_Body: | |
1e17ef87 | 7834 | /* If this is the body of a generic package - do nothing. */ |
a1ab4c31 AC |
7835 | if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package) |
7836 | { | |
7837 | gnu_result = alloc_stmt_list (); | |
7838 | break; | |
7839 | } | |
7840 | ||
7841 | start_stmt_group (); | |
6d16658d | 7842 | process_decls (Declarations (gnat_node), Empty, true, true); |
a1ab4c31 AC |
7843 | if (Present (Handled_Statement_Sequence (gnat_node))) |
7844 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); | |
6d16658d BD |
7845 | if (Present (At_End_Proc (gnat_node))) |
7846 | At_End_Proc_to_gnu (gnat_node); | |
a1ab4c31 AC |
7847 | gnu_result = end_stmt_group (); |
7848 | break; | |
7849 | ||
1e17ef87 EB |
7850 | /********************************/ |
7851 | /* Chapter 8: Visibility Rules */ | |
7852 | /********************************/ | |
a1ab4c31 AC |
7853 | |
7854 | case N_Use_Package_Clause: | |
7855 | case N_Use_Type_Clause: | |
1e17ef87 | 7856 | /* Nothing to do here - but these may appear in list of declarations. */ |
a1ab4c31 AC |
7857 | gnu_result = alloc_stmt_list (); |
7858 | break; | |
7859 | ||
1e17ef87 EB |
7860 | /*********************/ |
7861 | /* Chapter 9: Tasks */ | |
7862 | /*********************/ | |
a1ab4c31 AC |
7863 | |
7864 | case N_Protected_Type_Declaration: | |
7865 | gnu_result = alloc_stmt_list (); | |
7866 | break; | |
7867 | ||
7868 | case N_Single_Task_Declaration: | |
afc737f0 | 7869 | gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true); |
a1ab4c31 AC |
7870 | gnu_result = alloc_stmt_list (); |
7871 | break; | |
7872 | ||
1e17ef87 EB |
7873 | /*********************************************************/ |
7874 | /* Chapter 10: Program Structure and Compilation Issues */ | |
7875 | /*********************************************************/ | |
a1ab4c31 AC |
7876 | |
7877 | case N_Compilation_Unit: | |
a09d56d8 | 7878 | /* This is not called for the main unit on which gigi is invoked. */ |
a1ab4c31 AC |
7879 | Compilation_Unit_to_gnu (gnat_node); |
7880 | gnu_result = alloc_stmt_list (); | |
7881 | break; | |
7882 | ||
c4833de1 EB |
7883 | case N_Subunit: |
7884 | gnu_result = gnat_to_gnu (Proper_Body (gnat_node)); | |
7885 | break; | |
7886 | ||
7887 | case N_Entry_Body: | |
7888 | case N_Protected_Body: | |
7889 | case N_Task_Body: | |
7890 | /* These nodes should only be present when annotating types. */ | |
7891 | gcc_assert (type_annotate_only); | |
6d16658d | 7892 | process_decls (Declarations (gnat_node), Empty, true, true); |
c4833de1 EB |
7893 | gnu_result = alloc_stmt_list (); |
7894 | break; | |
7895 | ||
a1ab4c31 AC |
7896 | case N_Subprogram_Body_Stub: |
7897 | case N_Package_Body_Stub: | |
7898 | case N_Protected_Body_Stub: | |
7899 | case N_Task_Body_Stub: | |
7900 | /* Simply process whatever unit is being inserted. */ | |
4e568a15 EB |
7901 | if (Present (Library_Unit (gnat_node))) |
7902 | gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node))); | |
7903 | else | |
7904 | { | |
7905 | gcc_assert (type_annotate_only); | |
7906 | gnu_result = alloc_stmt_list (); | |
7907 | } | |
a1ab4c31 AC |
7908 | break; |
7909 | ||
a1ab4c31 | 7910 | /***************************/ |
1e17ef87 | 7911 | /* Chapter 11: Exceptions */ |
a1ab4c31 AC |
7912 | /***************************/ |
7913 | ||
7914 | case N_Handled_Sequence_Of_Statements: | |
a1ab4c31 AC |
7915 | gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node); |
7916 | break; | |
7917 | ||
7918 | case N_Exception_Handler: | |
e08f1aad | 7919 | gnu_result = Exception_Handler_to_gnu (gnat_node); |
624e1688 AC |
7920 | break; |
7921 | ||
7922 | case N_Raise_Statement: | |
7923 | /* Only for reraise in back-end exceptions mode. */ | |
ce19ac12 | 7924 | gcc_assert (No (Name (gnat_node))); |
624e1688 AC |
7925 | |
7926 | start_stmt_group (); | |
a1ab4c31 | 7927 | |
5d733372 AO |
7928 | add_stmt_with_node (build_call_n_expr (reraise_zcx_decl, 1, |
7929 | gnu_incoming_exc_ptr), | |
7930 | gnat_node); | |
624e1688 | 7931 | |
624e1688 | 7932 | gnu_result = end_stmt_group (); |
a1ab4c31 AC |
7933 | break; |
7934 | ||
7935 | case N_Push_Constraint_Error_Label: | |
8f8f531f | 7936 | gnu_constraint_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7937 | break; |
7938 | ||
7939 | case N_Push_Storage_Error_Label: | |
8f8f531f | 7940 | gnu_storage_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7941 | break; |
7942 | ||
7943 | case N_Push_Program_Error_Label: | |
8f8f531f | 7944 | gnu_program_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7945 | break; |
7946 | ||
7947 | case N_Pop_Constraint_Error_Label: | |
8f8f531f PMR |
7948 | gnat_temp = gnu_constraint_error_label_stack.pop (); |
7949 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7950 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7951 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7952 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7953 | break; |
7954 | ||
7955 | case N_Pop_Storage_Error_Label: | |
8f8f531f PMR |
7956 | gnat_temp = gnu_storage_error_label_stack.pop (); |
7957 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7958 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7959 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7960 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7961 | break; |
7962 | ||
7963 | case N_Pop_Program_Error_Label: | |
8f8f531f PMR |
7964 | gnat_temp = gnu_program_error_label_stack.pop (); |
7965 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7966 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7967 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7968 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7969 | break; |
7970 | ||
1e17ef87 EB |
7971 | /******************************/ |
7972 | /* Chapter 12: Generic Units */ | |
7973 | /******************************/ | |
a1ab4c31 AC |
7974 | |
7975 | case N_Generic_Function_Renaming_Declaration: | |
7976 | case N_Generic_Package_Renaming_Declaration: | |
7977 | case N_Generic_Procedure_Renaming_Declaration: | |
7978 | case N_Generic_Package_Declaration: | |
7979 | case N_Generic_Subprogram_Declaration: | |
7980 | case N_Package_Instantiation: | |
7981 | case N_Procedure_Instantiation: | |
7982 | case N_Function_Instantiation: | |
7983 | /* These nodes can appear on a declaration list but there is nothing to | |
7984 | to be done with them. */ | |
7985 | gnu_result = alloc_stmt_list (); | |
7986 | break; | |
7987 | ||
1e17ef87 EB |
7988 | /**************************************************/ |
7989 | /* Chapter 13: Representation Clauses and */ | |
7990 | /* Implementation-Dependent Features */ | |
7991 | /**************************************************/ | |
a1ab4c31 AC |
7992 | |
7993 | case N_Attribute_Definition_Clause: | |
a1ab4c31 AC |
7994 | gnu_result = alloc_stmt_list (); |
7995 | ||
8df2e902 EB |
7996 | /* The only one we need to deal with is 'Address since, for the others, |
7997 | the front-end puts the information elsewhere. */ | |
7998 | if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address) | |
7999 | break; | |
8000 | ||
8001 | /* And we only deal with 'Address if the object has a Freeze node. */ | |
8002 | gnat_temp = Entity (Name (gnat_node)); | |
3b9d1594 EB |
8003 | if (Freeze_Node (gnat_temp)) |
8004 | { | |
d587d1e4 | 8005 | tree gnu_address = gnat_to_gnu (Expression (gnat_node)), gnu_temp; |
3b9d1594 EB |
8006 | |
8007 | /* Get the value to use as the address and save it as the equivalent | |
8008 | for the object; when it is frozen, gnat_to_gnu_entity will do the | |
8009 | right thing. For a subprogram, put the naked address but build a | |
8010 | meaningfull expression for an object in case its address is taken | |
8011 | before the Freeze node is encountered; this can happen if the type | |
8012 | of the object is limited and it is initialized with the result of | |
8013 | a function call. */ | |
8014 | if (Is_Subprogram (gnat_temp)) | |
d587d1e4 | 8015 | gnu_temp = gnu_address; |
3b9d1594 EB |
8016 | else |
8017 | { | |
8018 | tree gnu_type = gnat_to_gnu_type (Etype (gnat_temp)); | |
8019 | /* Drop atomic and volatile qualifiers for the expression. */ | |
8020 | gnu_type = TYPE_MAIN_VARIANT (gnu_type); | |
8021 | gnu_type | |
8022 | = build_reference_type_for_mode (gnu_type, ptr_mode, true); | |
8023 | gnu_address = convert (gnu_type, gnu_address); | |
d587d1e4 | 8024 | gnu_temp |
3b9d1594 EB |
8025 | = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_address); |
8026 | } | |
a1ab4c31 | 8027 | |
d587d1e4 | 8028 | save_gnu_tree (gnat_temp, gnu_temp, true); |
3b9d1594 | 8029 | } |
a1ab4c31 AC |
8030 | break; |
8031 | ||
8032 | case N_Enumeration_Representation_Clause: | |
8033 | case N_Record_Representation_Clause: | |
8034 | case N_At_Clause: | |
8035 | /* We do nothing with these. SEM puts the information elsewhere. */ | |
8036 | gnu_result = alloc_stmt_list (); | |
8037 | break; | |
8038 | ||
8039 | case N_Code_Statement: | |
8040 | if (!type_annotate_only) | |
8041 | { | |
8042 | tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node)); | |
8043 | tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE; | |
8044 | tree gnu_clobbers = NULL_TREE, tail; | |
8045 | bool allows_mem, allows_reg, fake; | |
8046 | int ninputs, noutputs, i; | |
8047 | const char **oconstraints; | |
8048 | const char *constraint; | |
8049 | char *clobber; | |
8050 | ||
8051 | /* First retrieve the 3 operand lists built by the front-end. */ | |
8052 | Setup_Asm_Outputs (gnat_node); | |
8053 | while (Present (gnat_temp = Asm_Output_Variable ())) | |
8054 | { | |
8055 | tree gnu_value = gnat_to_gnu (gnat_temp); | |
8056 | tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu | |
8057 | (Asm_Output_Constraint ())); | |
8058 | ||
8059 | gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs); | |
8060 | Next_Asm_Output (); | |
8061 | } | |
8062 | ||
8063 | Setup_Asm_Inputs (gnat_node); | |
8064 | while (Present (gnat_temp = Asm_Input_Value ())) | |
8065 | { | |
8066 | tree gnu_value = gnat_to_gnu (gnat_temp); | |
8067 | tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu | |
8068 | (Asm_Input_Constraint ())); | |
8069 | ||
8070 | gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs); | |
8071 | Next_Asm_Input (); | |
8072 | } | |
8073 | ||
8074 | Clobber_Setup (gnat_node); | |
b0ba442b | 8075 | while ((clobber = (char *) Clobber_Get_Next ())) |
a1ab4c31 AC |
8076 | gnu_clobbers |
8077 | = tree_cons (NULL_TREE, | |
8078 | build_string (strlen (clobber) + 1, clobber), | |
8079 | gnu_clobbers); | |
8080 | ||
1e17ef87 | 8081 | /* Then perform some standard checking and processing on the |
a1ab4c31 AC |
8082 | operands. In particular, mark them addressable if needed. */ |
8083 | gnu_outputs = nreverse (gnu_outputs); | |
8084 | noutputs = list_length (gnu_outputs); | |
8085 | gnu_inputs = nreverse (gnu_inputs); | |
8086 | ninputs = list_length (gnu_inputs); | |
2bb1fc26 | 8087 | oconstraints = XALLOCAVEC (const char *, noutputs); |
a1ab4c31 AC |
8088 | |
8089 | for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
8090 | { | |
8091 | tree output = TREE_VALUE (tail); | |
8092 | constraint | |
8093 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8094 | oconstraints[i] = constraint; | |
8095 | ||
8096 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8097 | &allows_mem, &allows_reg, &fake)) | |
8098 | { | |
8099 | /* If the operand is going to end up in memory, | |
8100 | mark it addressable. Note that we don't test | |
8101 | allows_mem like in the input case below; this | |
17a98a3d | 8102 | is modeled on the C front-end. */ |
7e4680c1 EB |
8103 | if (!allows_reg) |
8104 | { | |
722356ce | 8105 | output = remove_conversions (output, false); |
7e4680c1 EB |
8106 | if (TREE_CODE (output) == CONST_DECL |
8107 | && DECL_CONST_CORRESPONDING_VAR (output)) | |
8108 | output = DECL_CONST_CORRESPONDING_VAR (output); | |
8109 | if (!gnat_mark_addressable (output)) | |
8110 | output = error_mark_node; | |
8111 | } | |
a1ab4c31 AC |
8112 | } |
8113 | else | |
8114 | output = error_mark_node; | |
8115 | ||
8116 | TREE_VALUE (tail) = output; | |
8117 | } | |
8118 | ||
8119 | for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
8120 | { | |
8121 | tree input = TREE_VALUE (tail); | |
8122 | constraint | |
8123 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8124 | ||
8125 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, | |
8126 | 0, oconstraints, | |
8127 | &allows_mem, &allows_reg)) | |
8128 | { | |
8129 | /* If the operand is going to end up in memory, | |
8130 | mark it addressable. */ | |
7e4680c1 EB |
8131 | if (!allows_reg && allows_mem) |
8132 | { | |
722356ce | 8133 | input = remove_conversions (input, false); |
7e4680c1 EB |
8134 | if (TREE_CODE (input) == CONST_DECL |
8135 | && DECL_CONST_CORRESPONDING_VAR (input)) | |
8136 | input = DECL_CONST_CORRESPONDING_VAR (input); | |
8137 | if (!gnat_mark_addressable (input)) | |
8138 | input = error_mark_node; | |
8139 | } | |
a1ab4c31 AC |
8140 | } |
8141 | else | |
8142 | input = error_mark_node; | |
8143 | ||
8144 | TREE_VALUE (tail) = input; | |
8145 | } | |
8146 | ||
1c384bf1 | 8147 | gnu_result = build5 (ASM_EXPR, void_type_node, |
a1ab4c31 | 8148 | gnu_template, gnu_outputs, |
1c384bf1 | 8149 | gnu_inputs, gnu_clobbers, NULL_TREE); |
a1ab4c31 AC |
8150 | ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node); |
8151 | } | |
8152 | else | |
8153 | gnu_result = alloc_stmt_list (); | |
8154 | ||
8155 | break; | |
8156 | ||
1e17ef87 EB |
8157 | /****************/ |
8158 | /* Added Nodes */ | |
8159 | /****************/ | |
a1ab4c31 | 8160 | |
a3a6d41f PMR |
8161 | /* Markers are created by the ABE mechanism to capture information which |
8162 | is either unavailable of expensive to recompute. Markers do not have | |
8163 | and runtime semantics, and should be ignored. */ | |
90e491a7 PMR |
8164 | |
8165 | case N_Call_Marker: | |
a3a6d41f | 8166 | case N_Variable_Reference_Marker: |
90e491a7 PMR |
8167 | gnu_result = alloc_stmt_list (); |
8168 | break; | |
8169 | ||
17c168fe | 8170 | case N_Expression_With_Actions: |
1a9ee222 OH |
8171 | /* This construct doesn't define a scope so we don't push a binding |
8172 | level around the statement list, but we wrap it in a SAVE_EXPR to | |
8173 | protect it from unsharing. Elaborate the expression as part of the | |
8174 | same statement group as the actions so that the type declaration | |
8175 | gets inserted there as well. This ensures that the type elaboration | |
8176 | code is issued past the actions computing values on which it might | |
8177 | depend. */ | |
1a9ee222 OH |
8178 | start_stmt_group (); |
8179 | add_stmt_list (Actions (gnat_node)); | |
8180 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); | |
8181 | gnu_result = end_stmt_group (); | |
8182 | ||
17c168fe EB |
8183 | gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result); |
8184 | TREE_SIDE_EFFECTS (gnu_result) = 1; | |
1a9ee222 | 8185 | |
17c168fe | 8186 | gnu_result |
39ab2e8f | 8187 | = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr); |
e1860041 | 8188 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
17c168fe EB |
8189 | break; |
8190 | ||
a1ab4c31 AC |
8191 | case N_Freeze_Entity: |
8192 | start_stmt_group (); | |
8193 | process_freeze_entity (gnat_node); | |
6d16658d | 8194 | process_decls (Actions (gnat_node), Empty, true, true); |
a1ab4c31 AC |
8195 | gnu_result = end_stmt_group (); |
8196 | break; | |
8197 | ||
3cd4a210 AC |
8198 | case N_Freeze_Generic_Entity: |
8199 | gnu_result = alloc_stmt_list (); | |
8200 | break; | |
8201 | ||
a1ab4c31 AC |
8202 | case N_Itype_Reference: |
8203 | if (!present_gnu_tree (Itype (gnat_node))) | |
8204 | process_type (Itype (gnat_node)); | |
a1ab4c31 AC |
8205 | gnu_result = alloc_stmt_list (); |
8206 | break; | |
8207 | ||
8208 | case N_Free_Statement: | |
bb24f343 EB |
8209 | gnat_temp = Expression (gnat_node); |
8210 | ||
a1ab4c31 AC |
8211 | if (!type_annotate_only) |
8212 | { | |
bb24f343 EB |
8213 | const Entity_Id gnat_desig_type |
8214 | = Designated_Type (Underlying_Type (Etype (gnat_temp))); | |
544d14e1 EB |
8215 | const Entity_Id gnat_pool = Storage_Pool (gnat_node); |
8216 | const bool pool_is_storage_model | |
8217 | = Present (gnat_pool) | |
8218 | && Has_Storage_Model_Type_Aspect (Etype (gnat_pool)) | |
8219 | && Present (Storage_Model_Copy_From (gnat_pool)); | |
8220 | tree gnu_ptr, gnu_ptr_type, gnu_obj_type, gnu_actual_obj_type; | |
bb24f343 EB |
8221 | |
8222 | /* Make sure the designated type is complete before dereferencing, | |
8223 | in case it is a Taft Amendment type. */ | |
8224 | (void) gnat_to_gnu_entity (gnat_desig_type, NULL_TREE, false); | |
8225 | ||
8226 | gnu_ptr = gnat_to_gnu (gnat_temp); | |
8227 | gnu_ptr_type = TREE_TYPE (gnu_ptr); | |
0d7de0e1 EB |
8228 | |
8229 | /* If this is a thin pointer, we must first dereference it to create | |
8230 | a fat pointer, then go back below to a thin pointer. The reason | |
8231 | for this is that we need to have a fat pointer someplace in order | |
8232 | to properly compute the size. */ | |
315cff15 | 8233 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
8234 | gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE, |
8235 | build_unary_op (INDIRECT_REF, NULL_TREE, | |
8236 | gnu_ptr)); | |
8237 | ||
0d7de0e1 EB |
8238 | /* If this is a fat pointer, the object must have been allocated with |
8239 | the template in front of the array. So pass the template address, | |
8240 | and get the total size; do it by converting to a thin pointer. */ | |
315cff15 | 8241 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
8242 | gnu_ptr |
8243 | = convert (build_pointer_type | |
8244 | (TYPE_OBJECT_RECORD_TYPE | |
8245 | (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))), | |
8246 | gnu_ptr); | |
8247 | ||
8248 | gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr)); | |
8249 | ||
0d7de0e1 EB |
8250 | /* If this is a thin pointer, the object must have been allocated with |
8251 | the template in front of the array. So pass the template address, | |
8252 | and get the total size. */ | |
8253 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) | |
8254 | gnu_ptr | |
8255 | = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr), | |
8256 | gnu_ptr, | |
2b45154d EB |
8257 | fold_build1 (NEGATE_EXPR, sizetype, |
8258 | byte_position | |
8259 | (DECL_CHAIN | |
8260 | TYPE_FIELDS ((gnu_obj_type))))); | |
0d7de0e1 EB |
8261 | |
8262 | /* If we have a special dynamic constrained subtype on the node, use | |
8263 | it to compute the size; otherwise, use the designated subtype. */ | |
a1ab4c31 AC |
8264 | if (Present (Actual_Designated_Subtype (gnat_node))) |
8265 | { | |
8266 | gnu_actual_obj_type | |
1e17ef87 | 8267 | = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node)); |
a1ab4c31 | 8268 | |
315cff15 | 8269 | if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)) |
1e17ef87 EB |
8270 | gnu_actual_obj_type |
8271 | = build_unc_object_type_from_ptr (gnu_ptr_type, | |
8272 | gnu_actual_obj_type, | |
928dfa4b EB |
8273 | get_identifier ("DEALLOC"), |
8274 | false); | |
a1ab4c31 AC |
8275 | } |
8276 | else | |
8277 | gnu_actual_obj_type = gnu_obj_type; | |
8278 | ||
62801a77 EB |
8279 | tree gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type); |
8280 | gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_ptr); | |
544d14e1 EB |
8281 | if (pool_is_storage_model) |
8282 | gnu_size = INSTANTIATE_LOAD_IN_EXPR (gnu_size, gnat_pool); | |
62801a77 | 8283 | |
ff346f70 | 8284 | gnu_result |
62801a77 | 8285 | = build_call_alloc_dealloc (gnu_ptr, gnu_size, gnu_obj_type, |
ff346f70 | 8286 | Procedure_To_Call (gnat_node), |
544d14e1 | 8287 | gnat_pool, gnat_node); |
a1ab4c31 AC |
8288 | } |
8289 | break; | |
8290 | ||
8291 | case N_Raise_Constraint_Error: | |
8292 | case N_Raise_Program_Error: | |
8293 | case N_Raise_Storage_Error: | |
80096613 EB |
8294 | if (type_annotate_only) |
8295 | gnu_result = alloc_stmt_list (); | |
8296 | else | |
8297 | gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type); | |
a1ab4c31 AC |
8298 | break; |
8299 | ||
8300 | case N_Validate_Unchecked_Conversion: | |
f04b8d69 EB |
8301 | /* The only validation we currently do on an unchecked conversion is |
8302 | that of aliasing assumptions. */ | |
8303 | if (flag_strict_aliasing) | |
9771b263 | 8304 | gnat_validate_uc_list.safe_push (gnat_node); |
a1ab4c31 AC |
8305 | gnu_result = alloc_stmt_list (); |
8306 | break; | |
8307 | ||
4e568a15 EB |
8308 | case N_Function_Specification: |
8309 | case N_Procedure_Specification: | |
8310 | case N_Op_Concat: | |
8311 | case N_Component_Association: | |
4e568a15 EB |
8312 | /* These nodes should only be present when annotating types. */ |
8313 | gcc_assert (type_annotate_only); | |
a1ab4c31 | 8314 | gnu_result = alloc_stmt_list (); |
4e568a15 EB |
8315 | break; |
8316 | ||
8317 | default: | |
8318 | /* Other nodes are not supposed to reach here. */ | |
8319 | gcc_unreachable (); | |
a1ab4c31 AC |
8320 | } |
8321 | ||
f53aff92 EB |
8322 | /* If we are in the elaboration procedure, check if we are violating the |
8323 | No_Elaboration_Code restriction by having a non-empty statement. */ | |
8324 | if (statement_node_p (gnat_node) | |
8325 | && !(TREE_CODE (gnu_result) == STATEMENT_LIST | |
8326 | && empty_stmt_list_p (gnu_result)) | |
8327 | && current_function_decl == get_elaboration_procedure ()) | |
8328 | Check_Elaboration_Code_Allowed (gnat_node); | |
8329 | ||
a09d56d8 | 8330 | /* If we pushed the processing of the elaboration routine, pop it back. */ |
a1ab4c31 | 8331 | if (went_into_elab_proc) |
a09d56d8 | 8332 | current_function_decl = NULL_TREE; |
a1ab4c31 | 8333 | |
1fc24649 | 8334 | /* When not optimizing, turn boolean rvalues B into B != false tests |
f1ff07ec EB |
8335 | so that we can put the location information of the reference to B on |
8336 | the inequality operator for better debug info. */ | |
1fc24649 | 8337 | if (!optimize |
054d6b83 | 8338 | && TREE_CODE (gnu_result) != INTEGER_CST |
f1ff07ec | 8339 | && TREE_CODE (gnu_result) != TYPE_DECL |
1fc24649 EB |
8340 | && (kind == N_Identifier |
8341 | || kind == N_Expanded_Name | |
8342 | || kind == N_Explicit_Dereference | |
1fc24649 EB |
8343 | || kind == N_Indexed_Component |
8344 | || kind == N_Selected_Component) | |
8345 | && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE | |
aff22074 | 8346 | && Nkind (Parent (gnat_node)) != N_Attribute_Reference |
bbdf2d39 | 8347 | && Nkind (Parent (gnat_node)) != N_Pragma_Argument_Association |
aff22074 EB |
8348 | && Nkind (Parent (gnat_node)) != N_Variant_Part |
8349 | && !lvalue_required_p (gnat_node, gnu_result_type, false, false)) | |
f1ff07ec EB |
8350 | { |
8351 | gnu_result | |
8352 | = build_binary_op (NE_EXPR, gnu_result_type, | |
8353 | convert (gnu_result_type, gnu_result), | |
8354 | convert (gnu_result_type, boolean_false_node)); | |
8355 | if (TREE_CODE (gnu_result) != INTEGER_CST) | |
8356 | set_gnu_expr_location_from_node (gnu_result, gnat_node); | |
8357 | } | |
8358 | ||
5e017b1e EB |
8359 | /* Set the location information on the result if it's not a simple name |
8360 | or something that contains a simple name, for example a tag, because | |
8361 | we don"t want all the references to get the location of the first use. | |
f1ff07ec EB |
8362 | Note that we may have no result if we tried to build a CALL_EXPR node |
8363 | to a procedure with no side-effects and optimization is enabled. */ | |
5e017b1e EB |
8364 | else if (kind != N_Identifier |
8365 | && !(kind == N_Selected_Component | |
8366 | && Chars (Selector_Name (gnat_node)) == Name_uTag) | |
8367 | && gnu_result | |
8368 | && EXPR_P (gnu_result)) | |
17c168fe | 8369 | set_gnu_expr_location_from_node (gnu_result, gnat_node); |
a1ab4c31 AC |
8370 | |
8371 | /* If we're supposed to return something of void_type, it means we have | |
8372 | something we're elaborating for effect, so just return. */ | |
04682fe7 | 8373 | if (VOID_TYPE_P (gnu_result_type)) |
a1ab4c31 AC |
8374 | return gnu_result; |
8375 | ||
c1abd261 EB |
8376 | /* If the result is a constant that overflowed, raise Constraint_Error. */ |
8377 | if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result)) | |
a1ab4c31 | 8378 | { |
57136d60 | 8379 | post_error ("??Constraint_Error will be raised at run time", gnat_node); |
a1ab4c31 AC |
8380 | gnu_result |
8381 | = build1 (NULL_EXPR, gnu_result_type, | |
8382 | build_call_raise (CE_Overflow_Check_Failed, gnat_node, | |
8383 | N_Raise_Constraint_Error)); | |
8384 | } | |
8385 | ||
62801a77 EB |
8386 | /* If the result has side-effects and is of an unconstrained type, protect |
8387 | the expression in case it will be referenced multiple times, i.e. for | |
8388 | its value and to compute the size of an object. But do it neither for | |
8389 | an object nor a renaming declaration, nor a return statement of a call | |
8390 | to a function that returns an unconstrained record type with default | |
8391 | discriminant, because there is no size to be computed in these cases | |
8392 | and this will create a useless temporary. We must do this before any | |
8393 | conversions. */ | |
a1ab4c31 AC |
8394 | if (TREE_SIDE_EFFECTS (gnu_result) |
8395 | && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE | |
62801a77 EB |
8396 | || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))) |
8397 | && !(TREE_CODE (gnu_result) == CALL_EXPR | |
8398 | && type_is_padding_self_referential (TREE_TYPE (gnu_result)) | |
8399 | && (Nkind (Parent (gnat_node)) == N_Object_Declaration | |
8400 | || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration | |
8401 | || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement))) | |
7194767c | 8402 | gnu_result = gnat_protect_expr (gnu_result); |
a1ab4c31 AC |
8403 | |
8404 | /* Now convert the result to the result type, unless we are in one of the | |
8405 | following cases: | |
8406 | ||
27ab5bd8 EB |
8407 | 1. If this is the LHS of an assignment or an actual parameter of a |
8408 | call, return the result almost unmodified since the RHS will have | |
8409 | to be converted to our type in that case, unless the result type | |
5dce843f EB |
8410 | has a simpler size or for array types because this size might be |
8411 | changed in-between. Likewise if there is just a no-op unchecked | |
27ab5bd8 EB |
8412 | conversion in-between. Similarly, don't convert integral types |
8413 | that are the operands of an unchecked conversion since we need | |
8414 | to ignore those conversions (for 'Valid). | |
a1ab4c31 AC |
8415 | |
8416 | 2. If we have a label (which doesn't have any well-defined type), a | |
abbc8c7b EB |
8417 | field or an error, return the result almost unmodified. Similarly, |
8418 | if the two types are record types with the same name, don't convert. | |
8419 | This will be the case when we are converting from a packable version | |
8420 | of a type to its original type and we need those conversions to be | |
8421 | NOPs in order for assignments into these types to work properly. | |
a1ab4c31 AC |
8422 | |
8423 | 3. If the type is void or if we have no result, return error_mark_node | |
8424 | to show we have no result. | |
8425 | ||
fc7a823e EB |
8426 | 4. If this is a call to a function that returns with variable size and |
8427 | the call is used as the expression in either an object or a renaming | |
8428 | declaration, return the result unmodified because we want to use the | |
8429 | return slot optimization in this case. | |
16934bbf | 8430 | |
ea588d41 EB |
8431 | 5. If this is a reference to an unconstrained array which is used either |
8432 | as the prefix of an attribute reference that requires an lvalue or in | |
8433 | a return statement, then return the result unmodified because we want | |
8434 | to return the original bounds. | |
eb0f4e48 EB |
8435 | |
8436 | 6. Finally, if the type of the result is already correct. */ | |
a1ab4c31 AC |
8437 | |
8438 | if (Present (Parent (gnat_node)) | |
27ab5bd8 | 8439 | && (lhs_or_actual_p (gnat_node) |
c2efda0d | 8440 | || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion |
4f8a6678 | 8441 | && unchecked_conversion_nop (Parent (gnat_node))) |
a1ab4c31 AC |
8442 | || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion |
8443 | && !AGGREGATE_TYPE_P (gnu_result_type) | |
8444 | && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))) | |
8445 | && !(TYPE_SIZE (gnu_result_type) | |
8446 | && TYPE_SIZE (TREE_TYPE (gnu_result)) | |
5dce843f EB |
8447 | && AGGREGATE_TYPE_P (gnu_result_type) |
8448 | == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)) | |
a1ab4c31 AC |
8449 | && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST |
8450 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result))) | |
8451 | != INTEGER_CST)) | |
8452 | || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST | |
8453 | && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)) | |
8454 | && (CONTAINS_PLACEHOLDER_P | |
5dce843f EB |
8455 | (TYPE_SIZE (TREE_TYPE (gnu_result))))) |
8456 | || (TREE_CODE (gnu_result_type) == ARRAY_TYPE | |
8457 | && TREE_CODE (TREE_TYPE (gnu_result)) == ARRAY_TYPE)) | |
a1ab4c31 AC |
8458 | && !(TREE_CODE (gnu_result_type) == RECORD_TYPE |
8459 | && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type)))) | |
8460 | { | |
8461 | /* Remove padding only if the inner object is of self-referential | |
8462 | size: in that case it must be an object of unconstrained type | |
8463 | with a default discriminant and we want to avoid copying too | |
02221bed EB |
8464 | much data. But do not remove it if it is already too small. */ |
8465 | if (type_is_padding_self_referential (TREE_TYPE (gnu_result)) | |
8466 | && !(TREE_CODE (gnu_result) == COMPONENT_REF | |
1e66a917 EB |
8467 | && DECL_BIT_FIELD (TREE_OPERAND (gnu_result, 1)) |
8468 | && DECL_SIZE (TREE_OPERAND (gnu_result, 1)) | |
8469 | != TYPE_SIZE (TREE_TYPE (gnu_result)))) | |
a1ab4c31 AC |
8470 | gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))), |
8471 | gnu_result); | |
8472 | } | |
8473 | ||
8474 | else if (TREE_CODE (gnu_result) == LABEL_DECL | |
8475 | || TREE_CODE (gnu_result) == FIELD_DECL | |
8476 | || TREE_CODE (gnu_result) == ERROR_MARK | |
abbc8c7b EB |
8477 | || (TYPE_NAME (gnu_result_type) |
8478 | == TYPE_NAME (TREE_TYPE (gnu_result)) | |
a1ab4c31 AC |
8479 | && TREE_CODE (gnu_result_type) == RECORD_TYPE |
8480 | && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE)) | |
8481 | { | |
8482 | /* Remove any padding. */ | |
ad00a297 | 8483 | gnu_result = maybe_padded_object (gnu_result); |
a1ab4c31 AC |
8484 | } |
8485 | ||
8486 | else if (gnu_result == error_mark_node || gnu_result_type == void_type_node) | |
8487 | gnu_result = error_mark_node; | |
8488 | ||
eb0f4e48 EB |
8489 | else if (TREE_CODE (gnu_result) == CALL_EXPR |
8490 | && Present (Parent (gnat_node)) | |
fc7a823e EB |
8491 | && (Nkind (Parent (gnat_node)) == N_Object_Declaration |
8492 | || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration) | |
fc7a823e | 8493 | && return_type_with_variable_size_p (TREE_TYPE (gnu_result))) |
842d4ee2 | 8494 | ; |
16934bbf | 8495 | |
544d14e1 | 8496 | else if (TREE_CODE (TREE_TYPE (gnu_result)) == UNCONSTRAINED_ARRAY_TYPE |
eb0f4e48 | 8497 | && Present (Parent (gnat_node)) |
ea588d41 EB |
8498 | && ((Nkind (Parent (gnat_node)) == N_Attribute_Reference |
8499 | && lvalue_required_for_attribute_p (Parent (gnat_node))) | |
8500 | || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement)) | |
eb0f4e48 EB |
8501 | ; |
8502 | ||
16934bbf | 8503 | else if (TREE_TYPE (gnu_result) != gnu_result_type) |
a1ab4c31 AC |
8504 | gnu_result = convert (gnu_result_type, gnu_result); |
8505 | ||
8506 | /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */ | |
8507 | while ((TREE_CODE (gnu_result) == NOP_EXPR | |
8508 | || TREE_CODE (gnu_result) == NON_LVALUE_EXPR) | |
8509 | && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result)) | |
8510 | gnu_result = TREE_OPERAND (gnu_result, 0); | |
8511 | ||
8512 | return gnu_result; | |
8513 | } | |
93e708f9 EB |
8514 | |
8515 | /* Similar to gnat_to_gnu, but discard any object that might be created in | |
8516 | the course of the translation of GNAT_NODE, which must be an "external" | |
8517 | expression in the sense that it will be elaborated elsewhere. */ | |
8518 | ||
8519 | tree | |
8520 | gnat_to_gnu_external (Node_Id gnat_node) | |
8521 | { | |
8522 | const int save_force_global = force_global; | |
f53aff92 | 8523 | bool went_into_elab_proc; |
93e708f9 EB |
8524 | |
8525 | /* Force the local context and create a fake scope that we zap | |
8526 | at the end so declarations will not be stuck either in the | |
8527 | global varpool or in the current scope. */ | |
8528 | if (!current_function_decl) | |
8529 | { | |
8530 | current_function_decl = get_elaboration_procedure (); | |
8531 | went_into_elab_proc = true; | |
8532 | } | |
f53aff92 EB |
8533 | else |
8534 | went_into_elab_proc = false; | |
93e708f9 EB |
8535 | force_global = 0; |
8536 | gnat_pushlevel (); | |
8537 | ||
8538 | tree gnu_result = gnat_to_gnu (gnat_node); | |
8539 | ||
8540 | gnat_zaplevel (); | |
8541 | force_global = save_force_global; | |
8542 | if (went_into_elab_proc) | |
8543 | current_function_decl = NULL_TREE; | |
8544 | ||
f1ff07ec | 8545 | /* Do not import locations from external units. */ |
04682fe7 | 8546 | if (CAN_HAVE_LOCATION_P (gnu_result)) |
f1ff07ec EB |
8547 | SET_EXPR_LOCATION (gnu_result, UNKNOWN_LOCATION); |
8548 | ||
93e708f9 EB |
8549 | return gnu_result; |
8550 | } | |
ce2d0ce2 | 8551 | |
102a1631 EB |
8552 | /* Return true if the statement list STMT_LIST is empty. */ |
8553 | ||
8554 | static bool | |
8555 | empty_stmt_list_p (tree stmt_list) | |
8556 | { | |
8557 | tree_stmt_iterator tsi; | |
8558 | ||
8559 | for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi)) | |
8560 | { | |
8561 | tree stmt = tsi_stmt (tsi); | |
8562 | ||
8563 | /* Anything else than an empty STMT_STMT counts as something. */ | |
8564 | if (TREE_CODE (stmt) != STMT_STMT || STMT_STMT_STMT (stmt)) | |
8565 | return false; | |
8566 | } | |
8567 | ||
8568 | return true; | |
8569 | } | |
8570 | ||
a1ab4c31 AC |
8571 | /* Record the current code position in GNAT_NODE. */ |
8572 | ||
8573 | static void | |
8574 | record_code_position (Node_Id gnat_node) | |
8575 | { | |
8576 | tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE); | |
8577 | ||
8578 | add_stmt_with_node (stmt_stmt, gnat_node); | |
8579 | save_gnu_tree (gnat_node, stmt_stmt, true); | |
8580 | } | |
8581 | ||
8582 | /* Insert the code for GNAT_NODE at the position saved for that node. */ | |
8583 | ||
8584 | static void | |
8585 | insert_code_for (Node_Id gnat_node) | |
8586 | { | |
102a1631 EB |
8587 | tree code = gnat_to_gnu (gnat_node); |
8588 | ||
8589 | /* It's too late to remove the STMT_STMT itself at this point. */ | |
8590 | if (!empty_stmt_list_p (code)) | |
8591 | STMT_STMT_STMT (get_gnu_tree (gnat_node)) = code; | |
8592 | ||
a1ab4c31 AC |
8593 | save_gnu_tree (gnat_node, NULL_TREE, true); |
8594 | } | |
ce2d0ce2 | 8595 | |
a1ab4c31 AC |
8596 | /* Start a new statement group chained to the previous group. */ |
8597 | ||
8598 | void | |
8599 | start_stmt_group (void) | |
8600 | { | |
8601 | struct stmt_group *group = stmt_group_free_list; | |
8602 | ||
8603 | /* First see if we can get one from the free list. */ | |
8604 | if (group) | |
8605 | stmt_group_free_list = group->previous; | |
8606 | else | |
766090c2 | 8607 | group = ggc_alloc<stmt_group> (); |
a1ab4c31 AC |
8608 | |
8609 | group->previous = current_stmt_group; | |
8610 | group->stmt_list = group->block = group->cleanups = NULL_TREE; | |
8611 | current_stmt_group = group; | |
8612 | } | |
8613 | ||
586fea26 EB |
8614 | /* Add GNU_STMT to the current statement group. If it is an expression with |
8615 | no effects, it is ignored. */ | |
a1ab4c31 AC |
8616 | |
8617 | void | |
8618 | add_stmt (tree gnu_stmt) | |
8619 | { | |
8620 | append_to_statement_list (gnu_stmt, ¤t_stmt_group->stmt_list); | |
8621 | } | |
8622 | ||
586fea26 EB |
8623 | /* Similar, but the statement is always added, regardless of side-effects. */ |
8624 | ||
8625 | void | |
8626 | add_stmt_force (tree gnu_stmt) | |
8627 | { | |
8628 | append_to_statement_list_force (gnu_stmt, ¤t_stmt_group->stmt_list); | |
8629 | } | |
8630 | ||
8631 | /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */ | |
a1ab4c31 AC |
8632 | |
8633 | void | |
8634 | add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node) | |
8635 | { | |
56b8aa0c | 8636 | if (Present (gnat_node)) |
a1ab4c31 AC |
8637 | set_expr_location_from_node (gnu_stmt, gnat_node); |
8638 | add_stmt (gnu_stmt); | |
8639 | } | |
8640 | ||
586fea26 EB |
8641 | /* Similar, but the statement is always added, regardless of side-effects. */ |
8642 | ||
8643 | void | |
8644 | add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node) | |
8645 | { | |
8646 | if (Present (gnat_node)) | |
8647 | set_expr_location_from_node (gnu_stmt, gnat_node); | |
8648 | add_stmt_force (gnu_stmt); | |
8649 | } | |
8650 | ||
a1ab4c31 | 8651 | /* Add a declaration statement for GNU_DECL to the current statement group. |
56b8aa0c | 8652 | Get the SLOC to be put onto the statement from GNAT_NODE. */ |
a1ab4c31 AC |
8653 | |
8654 | void | |
56b8aa0c | 8655 | add_decl_expr (tree gnu_decl, Node_Id gnat_node) |
a1ab4c31 AC |
8656 | { |
8657 | tree type = TREE_TYPE (gnu_decl); | |
7352723b | 8658 | tree gnu_stmt, gnu_init; |
a1ab4c31 AC |
8659 | |
8660 | /* If this is a variable that Gigi is to ignore, we may have been given | |
8661 | an ERROR_MARK. So test for it. We also might have been given a | |
8662 | reference for a renaming. So only do something for a decl. Also | |
8663 | ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */ | |
8664 | if (!DECL_P (gnu_decl) | |
8665 | || (TREE_CODE (gnu_decl) == TYPE_DECL | |
8666 | && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)) | |
8667 | return; | |
8668 | ||
8669 | gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl); | |
8670 | ||
6ba4f08f EB |
8671 | /* If we are external or global, we don't want to output the DECL_EXPR for |
8672 | this DECL node since we already have evaluated the expressions in the | |
a1ab4c31 | 8673 | sizes and positions as globals and doing it again would be wrong. */ |
6ba4f08f | 8674 | if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ()) |
a1ab4c31 AC |
8675 | { |
8676 | /* Mark everything as used to prevent node sharing with subprograms. | |
8677 | Note that walk_tree knows how to deal with TYPE_DECL, but neither | |
8678 | VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */ | |
3f13dd77 | 8679 | MARK_VISITED (gnu_stmt); |
04682fe7 | 8680 | if (VAR_P (gnu_decl) |
a1ab4c31 AC |
8681 | || TREE_CODE (gnu_decl) == CONST_DECL) |
8682 | { | |
3f13dd77 EB |
8683 | MARK_VISITED (DECL_SIZE (gnu_decl)); |
8684 | MARK_VISITED (DECL_SIZE_UNIT (gnu_decl)); | |
8685 | MARK_VISITED (DECL_INITIAL (gnu_decl)); | |
a1ab4c31 | 8686 | } |
a1ab4c31 | 8687 | } |
6ba4f08f | 8688 | else |
56b8aa0c | 8689 | add_stmt_with_node (gnu_stmt, gnat_node); |
a1ab4c31 | 8690 | |
665e80ca EB |
8691 | /* Mark our TYPE_ADA_SIZE field now since it will not be gimplified. */ |
8692 | if (TREE_CODE (gnu_decl) == TYPE_DECL | |
8693 | && RECORD_OR_UNION_TYPE_P (type) | |
8694 | && !TYPE_FAT_POINTER_P (type)) | |
8695 | MARK_VISITED (TYPE_ADA_SIZE (type)); | |
8696 | ||
04682fe7 | 8697 | if (VAR_P (gnu_decl) && (gnu_init = DECL_INITIAL (gnu_decl))) |
1f3f64b9 EB |
8698 | { |
8699 | /* If this is a variable and an initializer is attached to it, it must be | |
8700 | valid for the context. Similar to init_const in create_var_decl. */ | |
8701 | if (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init)) | |
a1ab4c31 AC |
8702 | || (TREE_STATIC (gnu_decl) |
8703 | && !initializer_constant_valid_p (gnu_init, | |
1f3f64b9 | 8704 | TREE_TYPE (gnu_init)))) |
a1ab4c31 | 8705 | { |
1f3f64b9 EB |
8706 | DECL_INITIAL (gnu_decl) = NULL_TREE; |
8707 | if (TREE_READONLY (gnu_decl)) | |
8708 | { | |
8709 | TREE_READONLY (gnu_decl) = 0; | |
8710 | DECL_READONLY_ONCE_ELAB (gnu_decl) = 1; | |
8711 | } | |
8712 | ||
8713 | /* Remove any padding so the assignment is done properly. */ | |
8714 | gnu_decl = maybe_padded_object (gnu_decl); | |
a1ab4c31 | 8715 | |
1f3f64b9 EB |
8716 | gnu_stmt |
8717 | = build_binary_op (INIT_EXPR, NULL_TREE, gnu_decl, gnu_init); | |
8718 | add_stmt_with_node (gnu_stmt, gnat_node); | |
8719 | } | |
7352723b | 8720 | |
1f3f64b9 EB |
8721 | /* If this is the initialization of a (potentially) large aggregate, then |
8722 | declare the dependence on the memcpy routine. */ | |
8723 | if (AGGREGATE_TYPE_P (type) | |
8724 | && (!TREE_CONSTANT (TYPE_SIZE (type)) | |
8725 | || compare_tree_int (TYPE_SIZE (type), 2 * BITS_PER_WORD) > 0)) | |
8726 | Check_Restriction_No_Dependence_On_System (Name_Memory_Copy, | |
8727 | gnat_node); | |
a1ab4c31 AC |
8728 | } |
8729 | } | |
8730 | ||
8731 | /* Callback for walk_tree to mark the visited trees rooted at *TP. */ | |
8732 | ||
8733 | static tree | |
8734 | mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) | |
8735 | { | |
3f13dd77 EB |
8736 | tree t = *tp; |
8737 | ||
8738 | if (TREE_VISITED (t)) | |
a1ab4c31 AC |
8739 | *walk_subtrees = 0; |
8740 | ||
8741 | /* Don't mark a dummy type as visited because we want to mark its sizes | |
8742 | and fields once it's filled in. */ | |
3f13dd77 EB |
8743 | else if (!TYPE_IS_DUMMY_P (t)) |
8744 | TREE_VISITED (t) = 1; | |
a1ab4c31 | 8745 | |
4ed9ab2d | 8746 | /* The test in gimplify_type_sizes is on the main variant. */ |
3f13dd77 | 8747 | if (TYPE_P (t)) |
4ed9ab2d | 8748 | TYPE_SIZES_GIMPLIFIED (TYPE_MAIN_VARIANT (t)) = 1; |
a1ab4c31 AC |
8749 | |
8750 | return NULL_TREE; | |
8751 | } | |
8752 | ||
3f13dd77 EB |
8753 | /* Mark nodes rooted at T with TREE_VISITED and types as having their |
8754 | sized gimplified. We use this to indicate all variable sizes and | |
8755 | positions in global types may not be shared by any subprogram. */ | |
8756 | ||
8757 | void | |
8758 | mark_visited (tree t) | |
8759 | { | |
8760 | walk_tree (&t, mark_visited_r, NULL, NULL); | |
8761 | } | |
8762 | ||
a1ab4c31 | 8763 | /* Add GNU_CLEANUP, a cleanup action, to the current code group and |
362db0b2 TQ |
8764 | set its location to that of GNAT_NODE if present, but with column info |
8765 | cleared so that conditional branches generated as part of the cleanup | |
8766 | code do not interfere with coverage analysis tools. */ | |
a1ab4c31 AC |
8767 | |
8768 | static void | |
8769 | add_cleanup (tree gnu_cleanup, Node_Id gnat_node) | |
8770 | { | |
8771 | if (Present (gnat_node)) | |
ba464315 | 8772 | set_expr_location_from_node (gnu_cleanup, gnat_node, true); |
c900c700 | 8773 | |
5d733372 AO |
8774 | /* An EH_ELSE_EXPR must be by itself, and that's all we need when we |
8775 | use it. The assert below makes sure that is so. Should we ever | |
8776 | need more than that, we could combine EH_ELSE_EXPRs, and copy | |
8777 | non-EH_ELSE_EXPR stmts into both cleanup paths of an | |
8778 | EH_ELSE_EXPR. */ | |
8779 | if (TREE_CODE (gnu_cleanup) == EH_ELSE_EXPR) | |
8780 | { | |
8781 | gcc_assert (!current_stmt_group->cleanups); | |
8782 | current_stmt_group->cleanups = gnu_cleanup; | |
8783 | } | |
8784 | else | |
8785 | { | |
8786 | gcc_assert (!current_stmt_group->cleanups | |
8787 | || (TREE_CODE (current_stmt_group->cleanups) | |
8788 | != EH_ELSE_EXPR)); | |
8789 | append_to_statement_list (gnu_cleanup, ¤t_stmt_group->cleanups); | |
8790 | } | |
a1ab4c31 AC |
8791 | } |
8792 | ||
8793 | /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */ | |
8794 | ||
8795 | void | |
8796 | set_block_for_group (tree gnu_block) | |
8797 | { | |
8798 | gcc_assert (!current_stmt_group->block); | |
8799 | current_stmt_group->block = gnu_block; | |
8800 | } | |
8801 | ||
8802 | /* Return code corresponding to the current code group. It is normally | |
8803 | a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if | |
8804 | BLOCK or cleanups were set. */ | |
8805 | ||
8806 | tree | |
8807 | end_stmt_group (void) | |
8808 | { | |
8809 | struct stmt_group *group = current_stmt_group; | |
8810 | tree gnu_retval = group->stmt_list; | |
8811 | ||
8812 | /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there | |
8813 | are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK, | |
8814 | make a BIND_EXPR. Note that we nest in that because the cleanup may | |
8815 | reference variables in the block. */ | |
7c775aca | 8816 | if (!gnu_retval) |
a1ab4c31 AC |
8817 | gnu_retval = alloc_stmt_list (); |
8818 | ||
8819 | if (group->cleanups) | |
8820 | gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval, | |
8821 | group->cleanups); | |
8822 | ||
8823 | if (current_stmt_group->block) | |
8824 | gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block), | |
8825 | gnu_retval, group->block); | |
8826 | ||
8827 | /* Remove this group from the stack and add it to the free list. */ | |
8828 | current_stmt_group = group->previous; | |
8829 | group->previous = stmt_group_free_list; | |
8830 | stmt_group_free_list = group; | |
8831 | ||
8832 | return gnu_retval; | |
8833 | } | |
8834 | ||
a712b009 EB |
8835 | /* Return whether the current statement group may fall through. */ |
8836 | ||
8837 | static inline bool | |
8838 | stmt_group_may_fallthru (void) | |
8839 | { | |
8840 | if (current_stmt_group->stmt_list) | |
8841 | return block_may_fallthru (current_stmt_group->stmt_list); | |
8842 | else | |
8843 | return true; | |
8844 | } | |
8845 | ||
a1ab4c31 AC |
8846 | /* Add a list of statements from GNAT_LIST, a possibly-empty list of |
8847 | statements.*/ | |
8848 | ||
8849 | static void | |
8850 | add_stmt_list (List_Id gnat_list) | |
8851 | { | |
8852 | Node_Id gnat_node; | |
8853 | ||
8854 | if (Present (gnat_list)) | |
8855 | for (gnat_node = First (gnat_list); Present (gnat_node); | |
8856 | gnat_node = Next (gnat_node)) | |
8857 | add_stmt (gnat_to_gnu (gnat_node)); | |
8858 | } | |
8859 | ||
8860 | /* Build a tree from GNAT_LIST, a possibly-empty list of statements. | |
8861 | If BINDING_P is true, push and pop a binding level around the list. */ | |
8862 | ||
8863 | static tree | |
8864 | build_stmt_group (List_Id gnat_list, bool binding_p) | |
8865 | { | |
8866 | start_stmt_group (); | |
547bbe49 | 8867 | |
a1ab4c31 AC |
8868 | if (binding_p) |
8869 | gnat_pushlevel (); | |
8870 | ||
8871 | add_stmt_list (gnat_list); | |
547bbe49 | 8872 | |
a1ab4c31 AC |
8873 | if (binding_p) |
8874 | gnat_poplevel (); | |
8875 | ||
8876 | return end_stmt_group (); | |
8877 | } | |
ce2d0ce2 | 8878 | |
a1ab4c31 AC |
8879 | /* Generate GIMPLE in place for the expression at *EXPR_P. */ |
8880 | ||
8881 | int | |
8882 | gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p, | |
8883 | gimple_seq *post_p ATTRIBUTE_UNUSED) | |
8884 | { | |
8885 | tree expr = *expr_p; | |
5e0f1fca | 8886 | tree type = TREE_TYPE (expr); |
a1ab4c31 AC |
8887 | tree op; |
8888 | ||
8889 | if (IS_ADA_STMT (expr)) | |
8890 | return gnat_gimplify_stmt (expr_p); | |
8891 | ||
8892 | switch (TREE_CODE (expr)) | |
8893 | { | |
a1ab4c31 AC |
8894 | case ADDR_EXPR: |
8895 | op = TREE_OPERAND (expr, 0); | |
8896 | ||
bb021771 EB |
8897 | /* If we are taking the address of a constant CONSTRUCTOR, make sure it |
8898 | is put into static memory. We know that it's going to be read-only | |
8899 | given the semantics we have and it must be in static memory when the | |
8900 | reference is in an elaboration procedure. */ | |
8901 | if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op)) | |
a1ab4c31 | 8902 | { |
bb021771 | 8903 | tree addr = build_fold_addr_expr (tree_output_constant_def (op)); |
5e0f1fca | 8904 | *expr_p = fold_convert (type, addr); |
bb021771 EB |
8905 | return GS_ALL_DONE; |
8906 | } | |
cb3d597d | 8907 | |
241125b2 EB |
8908 | /* Replace atomic loads with their first argument. That's necessary |
8909 | because the gimplifier would create a temporary otherwise. */ | |
8910 | if (TREE_SIDE_EFFECTS (op)) | |
8911 | while (handled_component_p (op) || CONVERT_EXPR_P (op)) | |
8912 | { | |
8913 | tree inner = TREE_OPERAND (op, 0); | |
8914 | if (TREE_CODE (inner) == CALL_EXPR && call_is_atomic_load (inner)) | |
8915 | { | |
8916 | tree t = CALL_EXPR_ARG (inner, 0); | |
8917 | if (TREE_CODE (t) == NOP_EXPR) | |
8918 | t = TREE_OPERAND (t, 0); | |
8919 | if (TREE_CODE (t) == ADDR_EXPR) | |
8920 | TREE_OPERAND (op, 0) = TREE_OPERAND (t, 0); | |
8921 | else | |
8922 | TREE_OPERAND (op, 0) = build_fold_indirect_ref (t); | |
8923 | } | |
8924 | else | |
8925 | op = inner; | |
8926 | } | |
9ae38416 | 8927 | break; |
456976d8 | 8928 | |
fccc47dd EB |
8929 | case CALL_EXPR: |
8930 | /* If we are passing a constant fat pointer CONSTRUCTOR, make sure it is | |
8931 | put into static memory; this performs a restricted version of constant | |
8932 | propagation on fat pointers in calls. But do not do it for strings to | |
8933 | avoid blocking concatenation in the caller when it is inlined. */ | |
8934 | for (int i = 0; i < call_expr_nargs (expr); i++) | |
8935 | { | |
544d14e1 | 8936 | tree arg = CALL_EXPR_ARG (expr, i); |
fccc47dd EB |
8937 | |
8938 | if (TREE_CODE (arg) == CONSTRUCTOR | |
8939 | && TREE_CONSTANT (arg) | |
8940 | && TYPE_IS_FAT_POINTER_P (TREE_TYPE (arg))) | |
8941 | { | |
8942 | tree t = CONSTRUCTOR_ELT (arg, 0)->value; | |
8943 | if (TREE_CODE (t) == NOP_EXPR) | |
8944 | t = TREE_OPERAND (t, 0); | |
8945 | if (TREE_CODE (t) == ADDR_EXPR) | |
8946 | t = TREE_OPERAND (t, 0); | |
8947 | if (TREE_CODE (t) != STRING_CST) | |
544d14e1 | 8948 | CALL_EXPR_ARG (expr, i) = tree_output_constant_def (arg); |
fccc47dd EB |
8949 | } |
8950 | } | |
9ae38416 EB |
8951 | break; |
8952 | ||
8953 | case DECL_EXPR: | |
8954 | op = DECL_EXPR_DECL (expr); | |
8955 | ||
8956 | /* The expressions for the RM bounds must be gimplified to ensure that | |
8957 | they are properly elaborated. See gimplify_decl_expr. */ | |
04682fe7 | 8958 | if ((TREE_CODE (op) == TYPE_DECL || VAR_P (op)) |
9ae38416 EB |
8959 | && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)) |
8960 | && (INTEGRAL_TYPE_P (TREE_TYPE (op)) | |
8961 | || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))) | |
8962 | { | |
8963 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val; | |
8964 | ||
8965 | val = TYPE_RM_MIN_VALUE (type); | |
8966 | if (val) | |
8967 | { | |
8968 | gimplify_one_sizepos (&val, pre_p); | |
8969 | for (t = type; t; t = TYPE_NEXT_VARIANT (t)) | |
8970 | SET_TYPE_RM_MIN_VALUE (t, val); | |
8971 | } | |
8972 | ||
8973 | val = TYPE_RM_MAX_VALUE (type); | |
8974 | if (val) | |
8975 | { | |
8976 | gimplify_one_sizepos (&val, pre_p); | |
8977 | for (t = type; t; t = TYPE_NEXT_VARIANT (t)) | |
8978 | SET_TYPE_RM_MAX_VALUE (t, val); | |
8979 | } | |
8980 | } | |
8981 | break; | |
8982 | ||
8983 | case NULL_EXPR: | |
8984 | /* If this is an aggregate type, build a null pointer of the appropriate | |
8985 | type and dereference it. */ | |
8986 | if (AGGREGATE_TYPE_P (type) | |
8987 | || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE) | |
8988 | *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE, | |
8989 | convert (build_pointer_type (type), | |
e2c9982e | 8990 | null_pointer_node)); |
9ae38416 EB |
8991 | |
8992 | /* Otherwise, just make a VAR_DECL. */ | |
8993 | else | |
8994 | { | |
8995 | *expr_p = create_tmp_var (type, NULL); | |
8996 | suppress_warning (*expr_p); | |
8997 | } | |
fccc47dd | 8998 | |
9ae38416 EB |
8999 | gimplify_and_add (TREE_OPERAND (expr, 0), pre_p); |
9000 | return GS_OK; | |
9001 | ||
889db59e EB |
9002 | case SAVE_EXPR: |
9003 | op = TREE_OPERAND (expr, 0); | |
9004 | ||
9005 | /* Propagate TREE_NO_WARNING from expression to temporary by using the | |
9006 | SAVE_EXPR itself as an intermediate step. See gimplify_save_expr. */ | |
1434eee5 EB |
9007 | if (type == void_type_node) |
9008 | ; | |
9009 | else if (SAVE_EXPR_RESOLVED_P (expr)) | |
889db59e EB |
9010 | TREE_NO_WARNING (op) = TREE_NO_WARNING (expr); |
9011 | else | |
9012 | TREE_NO_WARNING (expr) = TREE_NO_WARNING (op); | |
9013 | break; | |
9014 | ||
544d14e1 EB |
9015 | case LOAD_EXPR: |
9016 | { | |
9017 | tree new_var = create_tmp_var (type, "L"); | |
9018 | TREE_ADDRESSABLE (new_var) = 1; | |
9019 | ||
9020 | tree init = TREE_OPERAND (expr, 1); | |
9021 | gcc_assert (TREE_CODE (init) == CALL_EXPR); | |
9022 | tree arg = CALL_EXPR_ARG (init, 1); | |
9023 | CALL_EXPR_ARG (init, 1) | |
9024 | = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), new_var); | |
9025 | gimplify_and_add (init, pre_p); | |
9026 | ||
9027 | *expr_p = new_var; | |
9028 | return GS_OK; | |
9029 | } | |
fccc47dd | 9030 | |
819a653e EB |
9031 | case VIEW_CONVERT_EXPR: |
9032 | op = TREE_OPERAND (expr, 0); | |
9033 | ||
9034 | /* If we are view-converting a CONSTRUCTOR or a call from an aggregate | |
9035 | type to a scalar one, explicitly create the local temporary. That's | |
9036 | required if the type is passed by reference. */ | |
9037 | if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR) | |
9038 | && AGGREGATE_TYPE_P (TREE_TYPE (op)) | |
5e0f1fca | 9039 | && !AGGREGATE_TYPE_P (type)) |
819a653e | 9040 | { |
544d14e1 | 9041 | tree new_var = create_tmp_var_raw (TREE_TYPE (op), "C"); |
819a653e EB |
9042 | gimple_add_tmp_var (new_var); |
9043 | ||
544d14e1 | 9044 | tree mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op); |
819a653e EB |
9045 | gimplify_and_add (mod, pre_p); |
9046 | ||
9047 | TREE_OPERAND (expr, 0) = new_var; | |
9048 | return GS_OK; | |
9049 | } | |
9ae38416 | 9050 | break; |
a1ab4c31 | 9051 | |
544d14e1 EB |
9052 | case UNCONSTRAINED_ARRAY_REF: |
9053 | /* We should only do this if we are just elaborating for side effects, | |
9054 | but we can't know that yet. */ | |
9055 | *expr_p = TREE_OPERAND (expr, 0); | |
9056 | return GS_OK; | |
9057 | ||
a1ab4c31 | 9058 | default: |
9ae38416 | 9059 | break; |
a1ab4c31 | 9060 | } |
9ae38416 EB |
9061 | |
9062 | return GS_UNHANDLED; | |
a1ab4c31 AC |
9063 | } |
9064 | ||
9065 | /* Generate GIMPLE in place for the statement at *STMT_P. */ | |
9066 | ||
9067 | static enum gimplify_status | |
9068 | gnat_gimplify_stmt (tree *stmt_p) | |
9069 | { | |
9070 | tree stmt = *stmt_p; | |
9071 | ||
9072 | switch (TREE_CODE (stmt)) | |
9073 | { | |
9074 | case STMT_STMT: | |
9075 | *stmt_p = STMT_STMT_STMT (stmt); | |
9076 | return GS_OK; | |
9077 | ||
9078 | case LOOP_STMT: | |
9079 | { | |
c172df28 | 9080 | tree gnu_start_label = create_artificial_label (input_location); |
d88bbbb9 EB |
9081 | tree gnu_cond = LOOP_STMT_COND (stmt); |
9082 | tree gnu_update = LOOP_STMT_UPDATE (stmt); | |
a1ab4c31 | 9083 | tree gnu_end_label = LOOP_STMT_LABEL (stmt); |
a1ab4c31 | 9084 | |
d88bbbb9 EB |
9085 | /* Build the condition expression from the test, if any. */ |
9086 | if (gnu_cond) | |
3418f5e9 EB |
9087 | { |
9088 | /* Deal with the optimization hints. */ | |
9089 | if (LOOP_STMT_IVDEP (stmt)) | |
ac9effed | 9090 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
3418f5e9 | 9091 | build_int_cst (integer_type_node, |
ac9effed EB |
9092 | annot_expr_ivdep_kind), |
9093 | integer_zero_node); | |
9094 | if (LOOP_STMT_NO_UNROLL (stmt)) | |
9095 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, | |
9096 | build_int_cst (integer_type_node, | |
9097 | annot_expr_unroll_kind), | |
9098 | integer_one_node); | |
9099 | if (LOOP_STMT_UNROLL (stmt)) | |
9100 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, | |
9101 | build_int_cst (integer_type_node, | |
9102 | annot_expr_unroll_kind), | |
9103 | build_int_cst (NULL_TREE, USHRT_MAX)); | |
718c4601 | 9104 | if (LOOP_STMT_NO_VECTOR (stmt)) |
ac9effed | 9105 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
718c4601 | 9106 | build_int_cst (integer_type_node, |
ac9effed EB |
9107 | annot_expr_no_vector_kind), |
9108 | integer_zero_node); | |
718c4601 | 9109 | if (LOOP_STMT_VECTOR (stmt)) |
ac9effed | 9110 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
718c4601 | 9111 | build_int_cst (integer_type_node, |
ac9effed EB |
9112 | annot_expr_vector_kind), |
9113 | integer_zero_node); | |
718c4601 | 9114 | |
3418f5e9 EB |
9115 | gnu_cond |
9116 | = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE, | |
9117 | build1 (GOTO_EXPR, void_type_node, gnu_end_label)); | |
9118 | } | |
d88bbbb9 | 9119 | |
a1ab4c31 AC |
9120 | /* Set to emit the statements of the loop. */ |
9121 | *stmt_p = NULL_TREE; | |
9122 | ||
d88bbbb9 EB |
9123 | /* We first emit the start label and then a conditional jump to the |
9124 | end label if there's a top condition, then the update if it's at | |
9125 | the top, then the body of the loop, then a conditional jump to | |
9126 | the end label if there's a bottom condition, then the update if | |
9127 | it's at the bottom, and finally a jump to the start label and the | |
9128 | definition of the end label. */ | |
a1ab4c31 AC |
9129 | append_to_statement_list (build1 (LABEL_EXPR, void_type_node, |
9130 | gnu_start_label), | |
9131 | stmt_p); | |
9132 | ||
d88bbbb9 EB |
9133 | if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt)) |
9134 | append_to_statement_list (gnu_cond, stmt_p); | |
9135 | ||
9136 | if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt)) | |
9137 | append_to_statement_list (gnu_update, stmt_p); | |
a1ab4c31 AC |
9138 | |
9139 | append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p); | |
9140 | ||
d88bbbb9 EB |
9141 | if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt)) |
9142 | append_to_statement_list (gnu_cond, stmt_p); | |
9143 | ||
9144 | if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt)) | |
9145 | append_to_statement_list (gnu_update, stmt_p); | |
a1ab4c31 | 9146 | |
3418f5e9 | 9147 | tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label); |
a1ab4c31 AC |
9148 | SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label)); |
9149 | append_to_statement_list (t, stmt_p); | |
9150 | ||
9151 | append_to_statement_list (build1 (LABEL_EXPR, void_type_node, | |
9152 | gnu_end_label), | |
9153 | stmt_p); | |
9154 | return GS_OK; | |
9155 | } | |
9156 | ||
9157 | case EXIT_STMT: | |
9158 | /* Build a statement to jump to the corresponding end label, then | |
9159 | see if it needs to be conditional. */ | |
9160 | *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt)); | |
9161 | if (EXIT_STMT_COND (stmt)) | |
9162 | *stmt_p = build3 (COND_EXPR, void_type_node, | |
9163 | EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ()); | |
9164 | return GS_OK; | |
9165 | ||
9166 | default: | |
9167 | gcc_unreachable (); | |
9168 | } | |
9169 | } | |
ce2d0ce2 | 9170 | |
2b2a2e9e EB |
9171 | /* Force a reference to each of the entities in GNAT_PACKAGE recursively. |
9172 | ||
9173 | This routine is exclusively called in type_annotate mode, to compute DDA | |
9174 | information for types in withed units, for ASIS use. */ | |
9175 | ||
9176 | static void | |
9177 | elaborate_all_entities_for_package (Entity_Id gnat_package) | |
9178 | { | |
9179 | Entity_Id gnat_entity; | |
9180 | ||
9181 | for (gnat_entity = First_Entity (gnat_package); | |
9182 | Present (gnat_entity); | |
9183 | gnat_entity = Next_Entity (gnat_entity)) | |
9184 | { | |
9185 | const Entity_Kind kind = Ekind (gnat_entity); | |
9186 | ||
9187 | /* We are interested only in entities visible from the main unit. */ | |
9188 | if (!Is_Public (gnat_entity)) | |
9189 | continue; | |
9190 | ||
9191 | /* Skip stuff internal to the compiler. */ | |
abb540a7 | 9192 | if (Is_Intrinsic_Subprogram (gnat_entity)) |
2b2a2e9e EB |
9193 | continue; |
9194 | if (kind == E_Operator) | |
9195 | continue; | |
56bad7db EB |
9196 | if (IN (kind, Subprogram_Kind) |
9197 | && (Present (Alias (gnat_entity)) | |
9198 | || Is_Intrinsic_Subprogram (gnat_entity))) | |
2b2a2e9e | 9199 | continue; |
f8a9b81f EB |
9200 | if (Is_Itype (gnat_entity)) |
9201 | continue; | |
2b2a2e9e EB |
9202 | |
9203 | /* Skip named numbers. */ | |
9204 | if (IN (kind, Named_Kind)) | |
9205 | continue; | |
9206 | ||
9207 | /* Skip generic declarations. */ | |
9208 | if (IN (kind, Generic_Unit_Kind)) | |
9209 | continue; | |
9210 | ||
cce309d7 EB |
9211 | /* Skip formal objects. */ |
9212 | if (IN (kind, Formal_Object_Kind)) | |
9213 | continue; | |
9214 | ||
2b2a2e9e EB |
9215 | /* Skip package bodies. */ |
9216 | if (kind == E_Package_Body) | |
9217 | continue; | |
9218 | ||
9219 | /* Skip limited views that point back to the main unit. */ | |
9220 | if (IN (kind, Incomplete_Kind) | |
9221 | && From_Limited_With (gnat_entity) | |
9222 | && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity))) | |
9223 | continue; | |
9224 | ||
9225 | /* Skip types that aren't frozen. */ | |
9226 | if (IN (kind, Type_Kind) && !Is_Frozen (gnat_entity)) | |
9227 | continue; | |
9228 | ||
9229 | /* Recurse on real packages that aren't in the main unit. */ | |
9230 | if (kind == E_Package) | |
9231 | { | |
9232 | if (No (Renamed_Entity (gnat_entity)) | |
9233 | && !In_Extended_Main_Code_Unit (gnat_entity)) | |
9234 | elaborate_all_entities_for_package (gnat_entity); | |
9235 | } | |
9236 | else | |
afc737f0 | 9237 | gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
2b2a2e9e EB |
9238 | } |
9239 | } | |
9240 | ||
9241 | /* Force a reference to each of the entities in packages withed by GNAT_NODE. | |
a1ab4c31 AC |
9242 | Operate recursively but check that we aren't elaborating something more |
9243 | than once. | |
9244 | ||
9245 | This routine is exclusively called in type_annotate mode, to compute DDA | |
9246 | information for types in withed units, for ASIS use. */ | |
9247 | ||
9248 | static void | |
9249 | elaborate_all_entities (Node_Id gnat_node) | |
9250 | { | |
2b2a2e9e | 9251 | Entity_Id gnat_with_clause; |
a1ab4c31 AC |
9252 | |
9253 | /* Process each unit only once. As we trace the context of all relevant | |
9254 | units transitively, including generic bodies, we may encounter the | |
9255 | same generic unit repeatedly. */ | |
9256 | if (!present_gnu_tree (gnat_node)) | |
9257 | save_gnu_tree (gnat_node, integer_zero_node, true); | |
9258 | ||
9259 | /* Save entities in all context units. A body may have an implicit_with | |
9260 | on its own spec, if the context includes a child unit, so don't save | |
9261 | the spec twice. */ | |
9262 | for (gnat_with_clause = First (Context_Items (gnat_node)); | |
9263 | Present (gnat_with_clause); | |
9264 | gnat_with_clause = Next (gnat_with_clause)) | |
9265 | if (Nkind (gnat_with_clause) == N_With_Clause | |
9266 | && !present_gnu_tree (Library_Unit (gnat_with_clause)) | |
9267 | && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit))) | |
9268 | { | |
2b2a2e9e EB |
9269 | Node_Id gnat_unit = Library_Unit (gnat_with_clause); |
9270 | Entity_Id gnat_entity = Entity (Name (gnat_with_clause)); | |
a1ab4c31 | 9271 | |
2b2a2e9e EB |
9272 | elaborate_all_entities (gnat_unit); |
9273 | ||
0850f23b EB |
9274 | if (Ekind (gnat_entity) == E_Package |
9275 | && No (Renamed_Entity (gnat_entity))) | |
2b2a2e9e EB |
9276 | elaborate_all_entities_for_package (gnat_entity); |
9277 | ||
9278 | else if (Ekind (gnat_entity) == E_Generic_Package) | |
a1ab4c31 | 9279 | { |
2b2a2e9e | 9280 | Node_Id gnat_body = Corresponding_Body (Unit (gnat_unit)); |
a1ab4c31 AC |
9281 | |
9282 | /* Retrieve compilation unit node of generic body. */ | |
9283 | while (Present (gnat_body) | |
9284 | && Nkind (gnat_body) != N_Compilation_Unit) | |
9285 | gnat_body = Parent (gnat_body); | |
9286 | ||
9287 | /* If body is available, elaborate its context. */ | |
9288 | if (Present (gnat_body)) | |
9289 | elaborate_all_entities (gnat_body); | |
9290 | } | |
9291 | } | |
9292 | ||
9293 | if (Nkind (Unit (gnat_node)) == N_Package_Body) | |
9294 | elaborate_all_entities (Library_Unit (gnat_node)); | |
9295 | } | |
ce2d0ce2 | 9296 | |
f08863f9 | 9297 | /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */ |
a1ab4c31 AC |
9298 | |
9299 | static void | |
9300 | process_freeze_entity (Node_Id gnat_node) | |
9301 | { | |
f08863f9 EB |
9302 | const Entity_Id gnat_entity = Entity (gnat_node); |
9303 | const Entity_Kind kind = Ekind (gnat_entity); | |
9304 | tree gnu_old, gnu_new; | |
9305 | ||
692317f4 | 9306 | /* If this is a package, generate code for the package body, if any. */ |
f08863f9 | 9307 | if (kind == E_Package) |
a1ab4c31 | 9308 | { |
692317f4 EB |
9309 | const Node_Id gnat_decl = Parent (Declaration_Node (gnat_entity)); |
9310 | if (Present (Corresponding_Body (gnat_decl))) | |
9311 | insert_code_for (Parent (Corresponding_Body (gnat_decl))); | |
a1ab4c31 AC |
9312 | return; |
9313 | } | |
9314 | ||
f08863f9 EB |
9315 | /* Don't do anything for class-wide types as they are always transformed |
9316 | into their root type. */ | |
9317 | if (kind == E_Class_Wide_Type) | |
9318 | return; | |
9319 | ||
d4090614 EB |
9320 | /* Likewise for the entities internally used by the front-end to register |
9321 | primitives covering abstract interfaces, see Expand_N_Freeze_Entity. */ | |
9322 | if (Is_Subprogram (gnat_entity) && Present (Interface_Alias (gnat_entity))) | |
9323 | return; | |
9324 | ||
1e55d29a EB |
9325 | /* Check for an old definition if this isn't an object with address clause, |
9326 | since the saved GCC tree is the address expression in that case. */ | |
a1ab4c31 | 9327 | gnu_old |
1e55d29a EB |
9328 | = present_gnu_tree (gnat_entity) && No (Address_Clause (gnat_entity)) |
9329 | ? get_gnu_tree (gnat_entity) : NULL_TREE; | |
a1ab4c31 AC |
9330 | |
9331 | /* Don't do anything for subprograms that may have been elaborated before | |
f08863f9 EB |
9332 | their freeze nodes. This can happen, for example, because of an inner |
9333 | call in an instance body or because of previous compilation of a spec | |
9334 | for inlining purposes. */ | |
a1ab4c31 AC |
9335 | if (gnu_old |
9336 | && ((TREE_CODE (gnu_old) == FUNCTION_DECL | |
f08863f9 | 9337 | && (kind == E_Function || kind == E_Procedure)) |
69720717 | 9338 | || (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_old)) |
f08863f9 | 9339 | && kind == E_Subprogram_Type))) |
a1ab4c31 AC |
9340 | return; |
9341 | ||
d5ebeb8c EB |
9342 | /* If we have a non-dummy type old tree, we have nothing to do, except for |
9343 | aborting, since this node was never delayed as it should have been. We | |
a1ab4c31 | 9344 | let this happen for concurrent types and their Corresponding_Record_Type, |
f08863f9 | 9345 | however, because each might legitimately be elaborated before its own |
a1ab4c31 AC |
9346 | freeze node, e.g. while processing the other. */ |
9347 | if (gnu_old | |
9348 | && !(TREE_CODE (gnu_old) == TYPE_DECL | |
9349 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)))) | |
9350 | { | |
d5ebeb8c | 9351 | gcc_assert (Is_Concurrent_Type (gnat_entity) |
4ec7c4ec | 9352 | || (Is_Record_Type (gnat_entity) |
a1ab4c31 AC |
9353 | && Is_Concurrent_Record_Type (gnat_entity))); |
9354 | return; | |
9355 | } | |
9356 | ||
9357 | /* Reset the saved tree, if any, and elaborate the object or type for real. | |
f08863f9 EB |
9358 | If there is a full view, elaborate it and use the result. And, if this |
9359 | is the root type of a class-wide type, reuse it for the latter. */ | |
a1ab4c31 AC |
9360 | if (gnu_old) |
9361 | { | |
9362 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
bf0b0e5e | 9363 | |
7ed9919d | 9364 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
bf0b0e5e AC |
9365 | && Present (Full_View (gnat_entity))) |
9366 | { | |
9367 | Entity_Id full_view = Full_View (gnat_entity); | |
9368 | ||
f10ff6cc AC |
9369 | save_gnu_tree (full_view, NULL_TREE, false); |
9370 | ||
7ed9919d | 9371 | if (Is_Private_Type (full_view) |
bf0b0e5e | 9372 | && Present (Underlying_Full_View (full_view))) |
f10ff6cc AC |
9373 | { |
9374 | full_view = Underlying_Full_View (full_view); | |
9375 | save_gnu_tree (full_view, NULL_TREE, false); | |
9376 | } | |
bf0b0e5e AC |
9377 | } |
9378 | ||
7ed9919d | 9379 | if (Is_Type (gnat_entity) |
f08863f9 EB |
9380 | && Present (Class_Wide_Type (gnat_entity)) |
9381 | && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity) | |
a1ab4c31 AC |
9382 | save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false); |
9383 | } | |
9384 | ||
7ed9919d | 9385 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
a1ab4c31 AC |
9386 | && Present (Full_View (gnat_entity))) |
9387 | { | |
bf0b0e5e AC |
9388 | Entity_Id full_view = Full_View (gnat_entity); |
9389 | ||
7ed9919d | 9390 | if (Is_Private_Type (full_view) |
bf0b0e5e AC |
9391 | && Present (Underlying_Full_View (full_view))) |
9392 | full_view = Underlying_Full_View (full_view); | |
9393 | ||
afc737f0 | 9394 | gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, true); |
a1ab4c31 AC |
9395 | |
9396 | /* Propagate back-annotations from full view to partial view. */ | |
8de68eb3 | 9397 | if (!Known_Alignment (gnat_entity)) |
0c8ff35e | 9398 | Copy_Alignment (gnat_entity, full_view); |
a1ab4c31 | 9399 | |
8de68eb3 | 9400 | if (!Known_Esize (gnat_entity)) |
b23cdc01 | 9401 | Copy_Esize (gnat_entity, full_view); |
a1ab4c31 | 9402 | |
8de68eb3 | 9403 | if (!Known_RM_Size (gnat_entity)) |
b23cdc01 | 9404 | Copy_RM_Size (gnat_entity, full_view); |
a1ab4c31 AC |
9405 | |
9406 | /* The above call may have defined this entity (the simplest example | |
f08863f9 EB |
9407 | of this is when we have a private enumeral type since the bounds |
9408 | will have the public view). */ | |
a1ab4c31 | 9409 | if (!present_gnu_tree (gnat_entity)) |
f08863f9 | 9410 | save_gnu_tree (gnat_entity, gnu_new, false); |
a1ab4c31 AC |
9411 | } |
9412 | else | |
f08863f9 EB |
9413 | { |
9414 | tree gnu_init | |
9415 | = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration | |
9416 | && present_gnu_tree (Declaration_Node (gnat_entity))) | |
9417 | ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE; | |
9418 | ||
afc737f0 | 9419 | gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, true); |
f08863f9 EB |
9420 | } |
9421 | ||
7ed9919d | 9422 | if (Is_Type (gnat_entity) |
f08863f9 EB |
9423 | && Present (Class_Wide_Type (gnat_entity)) |
9424 | && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity) | |
9425 | save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false); | |
a1ab4c31 | 9426 | |
65444786 EB |
9427 | /* If we have an old type and we've made pointers to this type, update those |
9428 | pointers. If this is a Taft amendment type in the main unit, we need to | |
9429 | mark the type as used since other units referencing it don't see the full | |
9430 | declaration and, therefore, cannot mark it as used themselves. */ | |
a1ab4c31 | 9431 | if (gnu_old) |
65444786 EB |
9432 | { |
9433 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)), | |
9434 | TREE_TYPE (gnu_new)); | |
1e55d29a EB |
9435 | if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old))) |
9436 | update_profiles_with (TREE_TYPE (gnu_old)); | |
65444786 EB |
9437 | if (DECL_TAFT_TYPE_P (gnu_old)) |
9438 | used_types_insert (TREE_TYPE (gnu_new)); | |
9439 | } | |
a1ab4c31 | 9440 | } |
ce2d0ce2 | 9441 | |
a1ab4c31 AC |
9442 | /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present. |
9443 | We make two passes, one to elaborate anything other than bodies (but | |
9444 | we declare a function if there was no spec). The second pass | |
9445 | elaborates the bodies. | |
9446 | ||
a1ab4c31 AC |
9447 | We make a complete pass through both lists if PASS1P is true, then make |
9448 | the second pass over both lists if PASS2P is true. The lists usually | |
9449 | correspond to the public and private parts of a package. */ | |
9450 | ||
9451 | static void | |
9452 | process_decls (List_Id gnat_decls, List_Id gnat_decls2, | |
6d16658d | 9453 | bool pass1p, bool pass2p) |
a1ab4c31 AC |
9454 | { |
9455 | List_Id gnat_decl_array[2]; | |
9456 | Node_Id gnat_decl; | |
9457 | int i; | |
9458 | ||
9459 | gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2; | |
9460 | ||
9461 | if (pass1p) | |
9462 | for (i = 0; i <= 1; i++) | |
9463 | if (Present (gnat_decl_array[i])) | |
9464 | for (gnat_decl = First (gnat_decl_array[i]); | |
6d16658d BD |
9465 | Present (gnat_decl); |
9466 | gnat_decl = Next (gnat_decl)) | |
a1ab4c31 AC |
9467 | { |
9468 | /* For package specs, we recurse inside the declarations, | |
9469 | thus taking the two pass approach inside the boundary. */ | |
9470 | if (Nkind (gnat_decl) == N_Package_Declaration | |
9471 | && (Nkind (Specification (gnat_decl) | |
9472 | == N_Package_Specification))) | |
9473 | process_decls (Visible_Declarations (Specification (gnat_decl)), | |
9474 | Private_Declarations (Specification (gnat_decl)), | |
6d16658d | 9475 | true, false); |
a1ab4c31 AC |
9476 | |
9477 | /* Similarly for any declarations in the actions of a | |
9478 | freeze node. */ | |
9479 | else if (Nkind (gnat_decl) == N_Freeze_Entity) | |
9480 | { | |
9481 | process_freeze_entity (gnat_decl); | |
6d16658d | 9482 | process_decls (Actions (gnat_decl), Empty, true, false); |
a1ab4c31 AC |
9483 | } |
9484 | ||
9485 | /* Package bodies with freeze nodes get their elaboration deferred | |
9486 | until the freeze node, but the code must be placed in the right | |
9487 | place, so record the code position now. */ | |
9488 | else if (Nkind (gnat_decl) == N_Package_Body | |
9489 | && Present (Freeze_Node (Corresponding_Spec (gnat_decl)))) | |
9490 | record_code_position (gnat_decl); | |
9491 | ||
1e17ef87 | 9492 | else if (Nkind (gnat_decl) == N_Package_Body_Stub |
a1ab4c31 AC |
9493 | && Present (Library_Unit (gnat_decl)) |
9494 | && Present (Freeze_Node | |
9495 | (Corresponding_Spec | |
9496 | (Proper_Body (Unit | |
9497 | (Library_Unit (gnat_decl))))))) | |
9498 | record_code_position | |
9499 | (Proper_Body (Unit (Library_Unit (gnat_decl)))); | |
9500 | ||
4e279bd9 EB |
9501 | /* We defer most subprogram bodies to the second pass. For bodies |
9502 | that act as their own specs and stubs, the entity itself must be | |
9503 | elaborated in the first pass, because it may be used in other | |
9504 | declarations. */ | |
a1ab4c31 AC |
9505 | else if (Nkind (gnat_decl) == N_Subprogram_Body) |
9506 | { | |
9507 | if (Acts_As_Spec (gnat_decl)) | |
9508 | { | |
4e279bd9 | 9509 | Entity_Id gnat_subprog = Defining_Entity (gnat_decl); |
a1ab4c31 | 9510 | |
4e279bd9 EB |
9511 | if (!Is_Generic_Subprogram (gnat_subprog)) |
9512 | gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true); | |
a1ab4c31 AC |
9513 | } |
9514 | } | |
1e17ef87 | 9515 | |
a1ab4c31 AC |
9516 | else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub) |
9517 | { | |
4e279bd9 | 9518 | Entity_Id gnat_subprog |
1e17ef87 | 9519 | = Defining_Entity (Specification (gnat_decl)); |
a1ab4c31 | 9520 | |
4e279bd9 EB |
9521 | if (!Is_Generic_Subprogram (gnat_subprog) |
9522 | && Ekind (gnat_subprog) != E_Subprogram_Body) | |
9523 | gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true); | |
1e17ef87 | 9524 | } |
a1ab4c31 AC |
9525 | |
9526 | /* Concurrent stubs stand for the corresponding subprogram bodies, | |
9527 | which are deferred like other bodies. */ | |
9528 | else if (Nkind (gnat_decl) == N_Task_Body_Stub | |
9529 | || Nkind (gnat_decl) == N_Protected_Body_Stub) | |
9530 | ; | |
1e17ef87 | 9531 | |
1d4b96e0 AC |
9532 | /* Renamed subprograms may not be elaborated yet at this point |
9533 | since renamings do not trigger freezing. Wait for the second | |
9534 | pass to take care of them. */ | |
9535 | else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration) | |
9536 | ; | |
9537 | ||
a1ab4c31 AC |
9538 | else |
9539 | add_stmt (gnat_to_gnu (gnat_decl)); | |
9540 | } | |
9541 | ||
9542 | /* Here we elaborate everything we deferred above except for package bodies, | |
9543 | which are elaborated at their freeze nodes. Note that we must also | |
9544 | go inside things (package specs and freeze nodes) the first pass did. */ | |
9545 | if (pass2p) | |
9546 | for (i = 0; i <= 1; i++) | |
9547 | if (Present (gnat_decl_array[i])) | |
9548 | for (gnat_decl = First (gnat_decl_array[i]); | |
6d16658d BD |
9549 | Present (gnat_decl); |
9550 | gnat_decl = Next (gnat_decl)) | |
a1ab4c31 AC |
9551 | { |
9552 | if (Nkind (gnat_decl) == N_Subprogram_Body | |
9553 | || Nkind (gnat_decl) == N_Subprogram_Body_Stub | |
9554 | || Nkind (gnat_decl) == N_Task_Body_Stub | |
9555 | || Nkind (gnat_decl) == N_Protected_Body_Stub) | |
9556 | add_stmt (gnat_to_gnu (gnat_decl)); | |
9557 | ||
9558 | else if (Nkind (gnat_decl) == N_Package_Declaration | |
9559 | && (Nkind (Specification (gnat_decl) | |
9560 | == N_Package_Specification))) | |
9561 | process_decls (Visible_Declarations (Specification (gnat_decl)), | |
9562 | Private_Declarations (Specification (gnat_decl)), | |
6d16658d | 9563 | false, true); |
a1ab4c31 AC |
9564 | |
9565 | else if (Nkind (gnat_decl) == N_Freeze_Entity) | |
6d16658d | 9566 | process_decls (Actions (gnat_decl), Empty, false, true); |
1d4b96e0 AC |
9567 | |
9568 | else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration) | |
9569 | add_stmt (gnat_to_gnu (gnat_decl)); | |
a1ab4c31 AC |
9570 | } |
9571 | } | |
ce2d0ce2 | 9572 | |
b666e568 | 9573 | /* Make a unary operation of kind CODE using build_unary_op, but guard |
a7c43bbc EB |
9574 | the operation by an overflow check. CODE can be one of NEGATE_EXPR |
9575 | or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually | |
10069d53 EB |
9576 | the operation is to be performed in that type. GNAT_NODE is the gnat |
9577 | node conveying the source location for which the error should be | |
9578 | signaled. */ | |
b666e568 GB |
9579 | |
9580 | static tree | |
10069d53 EB |
9581 | build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand, |
9582 | Node_Id gnat_node) | |
b666e568 | 9583 | { |
a7c43bbc | 9584 | gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR); |
b666e568 | 9585 | |
7d7a1fe8 | 9586 | operand = gnat_protect_expr (operand); |
b666e568 | 9587 | |
1139f2e8 | 9588 | return emit_check (build_binary_op (EQ_EXPR, boolean_type_node, |
b666e568 GB |
9589 | operand, TYPE_MIN_VALUE (gnu_type)), |
9590 | build_unary_op (code, gnu_type, operand), | |
10069d53 | 9591 | CE_Overflow_Check_Failed, gnat_node); |
b666e568 GB |
9592 | } |
9593 | ||
a7c43bbc EB |
9594 | /* Make a binary operation of kind CODE using build_binary_op, but guard |
9595 | the operation by an overflow check. CODE can be one of PLUS_EXPR, | |
9596 | MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result. | |
10069d53 EB |
9597 | Usually the operation is to be performed in that type. GNAT_NODE is |
9598 | the GNAT node conveying the source location for which the error should | |
9599 | be signaled. */ | |
b666e568 GB |
9600 | |
9601 | static tree | |
a7c43bbc | 9602 | build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left, |
10069d53 | 9603 | tree right, Node_Id gnat_node) |
b666e568 | 9604 | { |
3f1be5be | 9605 | const unsigned int precision = TYPE_PRECISION (gnu_type); |
7d7a1fe8 EB |
9606 | tree lhs = gnat_protect_expr (left); |
9607 | tree rhs = gnat_protect_expr (right); | |
b666e568 GB |
9608 | tree type_max = TYPE_MAX_VALUE (gnu_type); |
9609 | tree type_min = TYPE_MIN_VALUE (gnu_type); | |
aa18f65a EB |
9610 | tree gnu_expr, check; |
9611 | int sgn; | |
b666e568 | 9612 | |
3f1be5be AC |
9613 | /* Assert that the precision is a power of 2. */ |
9614 | gcc_assert ((precision & (precision - 1)) == 0); | |
b666e568 | 9615 | |
aa18f65a EB |
9616 | /* Prefer a constant on the RHS to simplify checks. */ |
9617 | if (TREE_CODE (rhs) != INTEGER_CST | |
9618 | && TREE_CODE (lhs) == INTEGER_CST | |
9619 | && (code == PLUS_EXPR || code == MULT_EXPR)) | |
b666e568 | 9620 | { |
a7c43bbc EB |
9621 | tree tmp = lhs; |
9622 | lhs = rhs; | |
9623 | rhs = tmp; | |
4ae39383 GB |
9624 | } |
9625 | ||
3f1be5be AC |
9626 | gnu_expr = build_binary_op (code, gnu_type, lhs, rhs); |
9627 | ||
9628 | /* If we can fold the expression to a constant, just return it. | |
9629 | The caller will deal with overflow, no need to generate a check. */ | |
aa18f65a | 9630 | if (TREE_CODE (gnu_expr) == INTEGER_CST) |
3f1be5be AC |
9631 | return gnu_expr; |
9632 | ||
aa18f65a EB |
9633 | /* If no operand is a constant, we use the generic implementation. */ |
9634 | if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST) | |
b666e568 | 9635 | { |
4e1a2158 EB |
9636 | /* First convert the operands to the result type like build_binary_op. |
9637 | This is where the bias is made explicit for biased types. */ | |
9638 | lhs = convert (gnu_type, lhs); | |
9639 | rhs = convert (gnu_type, rhs); | |
9640 | ||
aa18f65a EB |
9641 | /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */ |
9642 | if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64) | |
f7ebc6a8 | 9643 | { |
aa18f65a | 9644 | tree int64 = gnat_type_for_size (64, 0); |
1f3f64b9 | 9645 | Check_Restriction_No_Dependence_On_System (Name_Arith_64, gnat_node); |
dddf8120 | 9646 | return convert (gnu_type, build_call_n_expr (mulv64_decl, 2, |
aa18f65a EB |
9647 | convert (int64, lhs), |
9648 | convert (int64, rhs))); | |
58e94443 | 9649 | } |
a7c43bbc | 9650 | |
f2d9f95e EB |
9651 | /* Likewise for a 128-bit mult and a 64-bit target. */ |
9652 | else if (code == MULT_EXPR && precision == 128 && BITS_PER_WORD < 128) | |
9653 | { | |
9654 | tree int128 = gnat_type_for_size (128, 0); | |
1f3f64b9 | 9655 | Check_Restriction_No_Dependence_On_System (Name_Arith_128, gnat_node); |
f2d9f95e EB |
9656 | return convert (gnu_type, build_call_n_expr (mulv128_decl, 2, |
9657 | convert (int128, lhs), | |
9658 | convert (int128, rhs))); | |
9659 | } | |
9660 | ||
aa18f65a | 9661 | enum internal_fn icode; |
4ae39383 | 9662 | |
aa18f65a | 9663 | switch (code) |
4ae39383 | 9664 | { |
aa18f65a EB |
9665 | case PLUS_EXPR: |
9666 | icode = IFN_ADD_OVERFLOW; | |
9667 | break; | |
9668 | case MINUS_EXPR: | |
9669 | icode = IFN_SUB_OVERFLOW; | |
9670 | break; | |
9671 | case MULT_EXPR: | |
9672 | icode = IFN_MUL_OVERFLOW; | |
9673 | break; | |
9674 | default: | |
9675 | gcc_unreachable (); | |
4ae39383 | 9676 | } |
aa18f65a EB |
9677 | |
9678 | tree gnu_ctype = build_complex_type (gnu_type); | |
9679 | tree call | |
9680 | = build_call_expr_internal_loc (UNKNOWN_LOCATION, icode, gnu_ctype, 2, | |
9681 | lhs, rhs); | |
9682 | tree tgt = save_expr (call); | |
9683 | gnu_expr = build1 (REALPART_EXPR, gnu_type, tgt); | |
87e25aac EB |
9684 | check = fold_build2 (NE_EXPR, boolean_type_node, |
9685 | build1 (IMAGPART_EXPR, gnu_type, tgt), | |
9686 | build_int_cst (gnu_type, 0)); | |
aa18f65a EB |
9687 | return |
9688 | emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node); | |
4ae39383 | 9689 | } |
b666e568 | 9690 | |
aa18f65a EB |
9691 | /* If one operand is a constant, we expose the overflow condition to enable |
9692 | a subsequent simplication or even elimination. */ | |
b666e568 GB |
9693 | switch (code) |
9694 | { | |
9695 | case PLUS_EXPR: | |
aa18f65a EB |
9696 | sgn = tree_int_cst_sgn (rhs); |
9697 | if (sgn > 0) | |
9698 | /* When rhs > 0, overflow when lhs > type_max - rhs. */ | |
9699 | check = build_binary_op (GT_EXPR, boolean_type_node, lhs, | |
9700 | build_binary_op (MINUS_EXPR, gnu_type, | |
9701 | type_max, rhs)); | |
9702 | else if (sgn < 0) | |
9703 | /* When rhs < 0, overflow when lhs < type_min - rhs. */ | |
9704 | check = build_binary_op (LT_EXPR, boolean_type_node, lhs, | |
9705 | build_binary_op (MINUS_EXPR, gnu_type, | |
9706 | type_min, rhs)); | |
9707 | else | |
9708 | return gnu_expr; | |
b666e568 GB |
9709 | break; |
9710 | ||
9711 | case MINUS_EXPR: | |
aa18f65a EB |
9712 | if (TREE_CODE (lhs) == INTEGER_CST) |
9713 | { | |
9714 | sgn = tree_int_cst_sgn (lhs); | |
9715 | if (sgn > 0) | |
9716 | /* When lhs > 0, overflow when rhs < lhs - type_max. */ | |
9717 | check = build_binary_op (LT_EXPR, boolean_type_node, rhs, | |
9718 | build_binary_op (MINUS_EXPR, gnu_type, | |
9719 | lhs, type_max)); | |
9720 | else if (sgn < 0) | |
9721 | /* When lhs < 0, overflow when rhs > lhs - type_min. */ | |
9722 | check = build_binary_op (GT_EXPR, boolean_type_node, rhs, | |
9723 | build_binary_op (MINUS_EXPR, gnu_type, | |
9724 | lhs, type_min)); | |
9725 | else | |
9726 | return gnu_expr; | |
9727 | } | |
9728 | else | |
9729 | { | |
9730 | sgn = tree_int_cst_sgn (rhs); | |
9731 | if (sgn > 0) | |
9732 | /* When rhs > 0, overflow when lhs < type_min + rhs. */ | |
9733 | check = build_binary_op (LT_EXPR, boolean_type_node, lhs, | |
9734 | build_binary_op (PLUS_EXPR, gnu_type, | |
9735 | type_min, rhs)); | |
9736 | else if (sgn < 0) | |
9737 | /* When rhs < 0, overflow when lhs > type_max + rhs. */ | |
9738 | check = build_binary_op (GT_EXPR, boolean_type_node, lhs, | |
9739 | build_binary_op (PLUS_EXPR, gnu_type, | |
9740 | type_max, rhs)); | |
9741 | else | |
9742 | return gnu_expr; | |
9743 | } | |
b666e568 GB |
9744 | break; |
9745 | ||
9746 | case MULT_EXPR: | |
aa18f65a EB |
9747 | sgn = tree_int_cst_sgn (rhs); |
9748 | if (sgn > 0) | |
9749 | { | |
9750 | if (integer_onep (rhs)) | |
9751 | return gnu_expr; | |
9752 | ||
9753 | tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs); | |
9754 | tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs); | |
9755 | ||
9756 | /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */ | |
9757 | check | |
9758 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, | |
9759 | build_binary_op (LT_EXPR, boolean_type_node, | |
9760 | lhs, lb), | |
9761 | build_binary_op (GT_EXPR, boolean_type_node, | |
9762 | lhs, ub)); | |
9763 | } | |
9764 | else if (sgn < 0) | |
9765 | { | |
9766 | tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs); | |
9767 | tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs); | |
9768 | ||
9769 | if (integer_minus_onep (rhs)) | |
9770 | /* When rhs == -1, overflow if lhs == type_min. */ | |
9771 | check | |
9772 | = build_binary_op (EQ_EXPR, boolean_type_node, lhs, type_min); | |
9773 | else | |
9774 | /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */ | |
9775 | check | |
9776 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, | |
9777 | build_binary_op (LT_EXPR, boolean_type_node, | |
9778 | lhs, lb), | |
9779 | build_binary_op (GT_EXPR, boolean_type_node, | |
9780 | lhs, ub)); | |
9781 | } | |
9782 | else | |
9783 | return gnu_expr; | |
b666e568 GB |
9784 | break; |
9785 | ||
9786 | default: | |
7c775aca | 9787 | gcc_unreachable (); |
b666e568 GB |
9788 | } |
9789 | ||
10069d53 | 9790 | return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node); |
b666e568 GB |
9791 | } |
9792 | ||
4e9a8214 EB |
9793 | /* GNU_COND contains the condition corresponding to an index, overflow or |
9794 | range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR | |
9795 | if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true. | |
9796 | REASON is the code that says why the exception is raised. GNAT_NODE is | |
9797 | the node conveying the source location for which the error should be | |
9798 | signaled. | |
9799 | ||
9800 | We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR, | |
9801 | overwriting the setting inherited from the call statement, on the ground | |
9802 | that the expression need not be evaluated just for the check. However | |
9803 | that's incorrect because, in the GCC type system, its value is presumed | |
9804 | to be valid so its comparison against the type bounds always yields true | |
9805 | and, therefore, could be done without evaluating it; given that it can | |
9806 | be a computation that overflows the bounds, the language may require the | |
9807 | check to fail and thus the expression to be evaluated in this case. */ | |
a1ab4c31 AC |
9808 | |
9809 | static tree | |
10069d53 | 9810 | emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node) |
a1ab4c31 | 9811 | { |
10069d53 EB |
9812 | tree gnu_call |
9813 | = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error); | |
4e9a8214 EB |
9814 | return |
9815 | fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond, | |
9816 | build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call, | |
9a1bdc31 EB |
9817 | SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr)) |
9818 | ? build_real (TREE_TYPE (gnu_expr), dconst0) | |
9819 | : build_int_cst (TREE_TYPE (gnu_expr), 0)), | |
4e9a8214 | 9820 | gnu_expr); |
a1ab4c31 | 9821 | } |
ce2d0ce2 | 9822 | |
1e17ef87 | 9823 | /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow |
815b5368 EB |
9824 | checks if OVERFLOW_P is true. If TRUNCATE_P is true, do a fp-to-integer |
9825 | conversion with truncation, otherwise round. GNAT_NODE is the GNAT node | |
9826 | conveying the source location for which the error should be signaled. */ | |
a1ab4c31 AC |
9827 | |
9828 | static tree | |
a4f7374e | 9829 | convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflow_p, |
815b5368 | 9830 | bool truncate_p, Node_Id gnat_node) |
a1ab4c31 AC |
9831 | { |
9832 | tree gnu_type = get_unpadded_type (gnat_type); | |
a1ab4c31 | 9833 | tree gnu_base_type = get_base_type (gnu_type); |
a4f7374e EB |
9834 | tree gnu_in_type = TREE_TYPE (gnu_expr); |
9835 | tree gnu_in_base_type = get_base_type (gnu_in_type); | |
a1ab4c31 AC |
9836 | tree gnu_result = gnu_expr; |
9837 | ||
1eb58520 AC |
9838 | /* If we are not doing any checks, the output is an integral type and the |
9839 | input is not a floating-point type, just do the conversion. This is | |
9840 | required for packed array types and is simpler in all cases anyway. */ | |
815b5368 | 9841 | if (!overflow_p |
1eb58520 | 9842 | && INTEGRAL_TYPE_P (gnu_base_type) |
a4f7374e | 9843 | && !FLOAT_TYPE_P (gnu_in_base_type)) |
a1ab4c31 AC |
9844 | return convert (gnu_type, gnu_expr); |
9845 | ||
a4f7374e EB |
9846 | /* If the mode of the input base type is larger, then converting to it below |
9847 | may pessimize the final conversion step, for example generate a libcall | |
9848 | instead of a simple instruction, so use a narrower type in this case. */ | |
9849 | if (TYPE_MODE (gnu_in_base_type) != TYPE_MODE (gnu_in_type) | |
9850 | && !(TREE_CODE (gnu_in_type) == INTEGER_TYPE | |
9851 | && TYPE_BIASED_REPRESENTATION_P (gnu_in_type))) | |
9852 | gnu_in_base_type = gnat_type_for_mode (TYPE_MODE (gnu_in_type), | |
9853 | TYPE_UNSIGNED (gnu_in_type)); | |
9854 | ||
9855 | /* First convert the expression to the base type. This will never generate | |
9856 | code, but makes the tests below simpler. But don't do this if converting | |
9857 | from an integer type to an unconstrained array type since then we need to | |
9858 | get the bounds from the original (unpacked) type. */ | |
a1ab4c31 | 9859 | if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE) |
a4f7374e | 9860 | gnu_result = convert (gnu_in_base_type, gnu_result); |
a1ab4c31 | 9861 | |
a4f7374e EB |
9862 | /* If overflow checks are requested, we need to be sure the result will fit |
9863 | in the output base type. But don't do this if the input is integer and | |
9864 | the output floating-point. */ | |
9865 | if (overflow_p | |
9866 | && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_base_type))) | |
a1ab4c31 AC |
9867 | { |
9868 | /* Ensure GNU_EXPR only gets evaluated once. */ | |
7d7a1fe8 | 9869 | tree gnu_input = gnat_protect_expr (gnu_result); |
bf6490b5 | 9870 | tree gnu_cond = boolean_false_node; |
a4f7374e EB |
9871 | tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_base_type); |
9872 | tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_base_type); | |
a1ab4c31 | 9873 | tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type); |
815b5368 EB |
9874 | tree gnu_out_ub |
9875 | = (TREE_CODE (gnu_base_type) == INTEGER_TYPE | |
9876 | && TYPE_MODULAR_P (gnu_base_type)) | |
9877 | ? fold_build2 (MINUS_EXPR, gnu_base_type, | |
9878 | TYPE_MODULUS (gnu_base_type), | |
9879 | build_int_cst (gnu_base_type, 1)) | |
9880 | : TYPE_MAX_VALUE (gnu_base_type); | |
a1ab4c31 AC |
9881 | |
9882 | /* Convert the lower bounds to signed types, so we're sure we're | |
9883 | comparing them properly. Likewise, convert the upper bounds | |
9884 | to unsigned types. */ | |
a4f7374e EB |
9885 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
9886 | && TYPE_UNSIGNED (gnu_in_base_type)) | |
9a1bdc31 | 9887 | gnu_in_lb |
a4f7374e | 9888 | = convert (gnat_signed_type_for (gnu_in_base_type), gnu_in_lb); |
a1ab4c31 | 9889 | |
a4f7374e EB |
9890 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
9891 | && !TYPE_UNSIGNED (gnu_in_base_type)) | |
9a1bdc31 | 9892 | gnu_in_ub |
a4f7374e | 9893 | = convert (gnat_unsigned_type_for (gnu_in_base_type), gnu_in_ub); |
a1ab4c31 AC |
9894 | |
9895 | if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type)) | |
9a1bdc31 EB |
9896 | gnu_out_lb |
9897 | = convert (gnat_signed_type_for (gnu_base_type), gnu_out_lb); | |
a1ab4c31 AC |
9898 | |
9899 | if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type)) | |
9a1bdc31 EB |
9900 | gnu_out_ub |
9901 | = convert (gnat_unsigned_type_for (gnu_base_type), gnu_out_ub); | |
a1ab4c31 AC |
9902 | |
9903 | /* Check each bound separately and only if the result bound | |
9904 | is tighter than the bound on the input type. Note that all the | |
9905 | types are base types, so the bounds must be constant. Also, | |
9906 | the comparison is done in the base type of the input, which | |
9907 | always has the proper signedness. First check for input | |
9908 | integer (which means output integer), output float (which means | |
9909 | both float), or mixed, in which case we always compare. | |
9910 | Note that we have to do the comparison which would *fail* in the | |
9911 | case of an error since if it's an FP comparison and one of the | |
9912 | values is a NaN or Inf, the comparison will fail. */ | |
a4f7374e | 9913 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
a1ab4c31 AC |
9914 | ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb) |
9915 | : (FLOAT_TYPE_P (gnu_base_type) | |
8cb41028 RS |
9916 | ? real_less (&TREE_REAL_CST (gnu_in_lb), |
9917 | &TREE_REAL_CST (gnu_out_lb)) | |
a1ab4c31 AC |
9918 | : 1)) |
9919 | gnu_cond | |
9920 | = invert_truthvalue | |
1139f2e8 | 9921 | (build_binary_op (GE_EXPR, boolean_type_node, |
a4f7374e | 9922 | gnu_input, convert (gnu_in_base_type, |
a1ab4c31 AC |
9923 | gnu_out_lb))); |
9924 | ||
a4f7374e | 9925 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
a1ab4c31 AC |
9926 | ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub) |
9927 | : (FLOAT_TYPE_P (gnu_base_type) | |
8cb41028 | 9928 | ? real_less (&TREE_REAL_CST (gnu_out_ub), |
21288963 | 9929 | &TREE_REAL_CST (gnu_in_ub)) |
a1ab4c31 AC |
9930 | : 1)) |
9931 | gnu_cond | |
1139f2e8 | 9932 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond, |
a1ab4c31 | 9933 | invert_truthvalue |
1139f2e8 | 9934 | (build_binary_op (LE_EXPR, boolean_type_node, |
a1ab4c31 | 9935 | gnu_input, |
a4f7374e | 9936 | convert (gnu_in_base_type, |
a1ab4c31 AC |
9937 | gnu_out_ub)))); |
9938 | ||
9939 | if (!integer_zerop (gnu_cond)) | |
10069d53 EB |
9940 | gnu_result = emit_check (gnu_cond, gnu_input, |
9941 | CE_Overflow_Check_Failed, gnat_node); | |
a1ab4c31 AC |
9942 | } |
9943 | ||
9944 | /* Now convert to the result base type. If this is a non-truncating | |
9945 | float-to-integer conversion, round. */ | |
24228312 | 9946 | if (INTEGRAL_TYPE_P (gnu_base_type) |
a4f7374e EB |
9947 | && FLOAT_TYPE_P (gnu_in_base_type) |
9948 | && !truncate_p) | |
a1ab4c31 AC |
9949 | { |
9950 | REAL_VALUE_TYPE half_minus_pred_half, pred_half; | |
ced57283 | 9951 | tree gnu_conv, gnu_zero, gnu_comp, calc_type; |
a1ab4c31 AC |
9952 | tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half; |
9953 | const struct real_format *fmt; | |
9954 | ||
9955 | /* The following calculations depend on proper rounding to even | |
24228312 | 9956 | of each arithmetic operation. In order to prevent excess |
1e17ef87 EB |
9957 | precision from spoiling this property, use the widest hardware |
9958 | floating-point type if FP_ARITH_MAY_WIDEN is true. */ | |
9959 | calc_type | |
a4f7374e | 9960 | = fp_arith_may_widen ? longest_float_type_node : gnu_in_base_type; |
a1ab4c31 | 9961 | |
1e17ef87 | 9962 | /* Compute the exact value calc_type'Pred (0.5) at compile time. */ |
a1ab4c31 AC |
9963 | fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type)); |
9964 | real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type)); | |
5c88ea94 RS |
9965 | real_arithmetic (&pred_half, MINUS_EXPR, &dconsthalf, |
9966 | &half_minus_pred_half); | |
a1ab4c31 AC |
9967 | gnu_pred_half = build_real (calc_type, pred_half); |
9968 | ||
9969 | /* If the input is strictly negative, subtract this value | |
ced57283 | 9970 | and otherwise add it from the input. For 0.5, the result |
1e17ef87 | 9971 | is exactly between 1.0 and the machine number preceding 1.0 |
ced57283 | 9972 | (for calc_type). Since the last bit of 1.0 is even, this 0.5 |
1e17ef87 | 9973 | will round to 1.0, while all other number with an absolute |
ced57283 | 9974 | value less than 0.5 round to 0.0. For larger numbers exactly |
1e17ef87 EB |
9975 | halfway between integers, rounding will always be correct as |
9976 | the true mathematical result will be closer to the higher | |
ced57283 | 9977 | integer compared to the lower one. So, this constant works |
1e17ef87 EB |
9978 | for all floating-point numbers. |
9979 | ||
9980 | The reason to use the same constant with subtract/add instead | |
9981 | of a positive and negative constant is to allow the comparison | |
9982 | to be scheduled in parallel with retrieval of the constant and | |
9983 | conversion of the input to the calc_type (if necessary). */ | |
a1ab4c31 | 9984 | |
a4f7374e | 9985 | gnu_zero = build_real (gnu_in_base_type, dconst0); |
7d7a1fe8 | 9986 | gnu_result = gnat_protect_expr (gnu_result); |
ced57283 EB |
9987 | gnu_conv = convert (calc_type, gnu_result); |
9988 | gnu_comp | |
1139f2e8 | 9989 | = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero); |
a1ab4c31 | 9990 | gnu_add_pred_half |
ced57283 | 9991 | = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half); |
a1ab4c31 | 9992 | gnu_subtract_pred_half |
ced57283 EB |
9993 | = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half); |
9994 | gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp, | |
9995 | gnu_add_pred_half, gnu_subtract_pred_half); | |
a1ab4c31 AC |
9996 | } |
9997 | ||
9998 | if (TREE_CODE (gnu_base_type) == INTEGER_TYPE | |
9999 | && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type) | |
10000 | && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF) | |
10001 | gnu_result = unchecked_convert (gnu_base_type, gnu_result, false); | |
10002 | else | |
10003 | gnu_result = convert (gnu_base_type, gnu_result); | |
10004 | ||
a80e0583 EB |
10005 | /* If this is a conversion between an integer type larger than a word and a |
10006 | floating-point type, then declare the dependence on the libgcc routine. */ | |
10007 | if ((INTEGRAL_TYPE_P (gnu_in_base_type) | |
10008 | && TYPE_PRECISION (gnu_in_base_type) > BITS_PER_WORD | |
10009 | && FLOAT_TYPE_P (gnu_base_type)) | |
10010 | || (FLOAT_TYPE_P (gnu_in_base_type) | |
10011 | && INTEGRAL_TYPE_P (gnu_base_type) | |
10012 | && TYPE_PRECISION (gnu_base_type) > BITS_PER_WORD)) | |
10013 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
10014 | ||
a1ab4c31 AC |
10015 | return convert (gnu_type, gnu_result); |
10016 | } | |
ce2d0ce2 | 10017 | |
a1ab4c31 AC |
10018 | /* Return true if GNU_EXPR can be directly addressed. This is the case |
10019 | unless it is an expression involving computation or if it involves a | |
10020 | reference to a bitfield or to an object not sufficiently aligned for | |
10021 | its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can | |
10022 | be directly addressed as an object of this type. | |
10023 | ||
10024 | *** Notes on addressability issues in the Ada compiler *** | |
10025 | ||
10026 | This predicate is necessary in order to bridge the gap between Gigi | |
10027 | and the middle-end about addressability of GENERIC trees. A tree | |
10028 | is said to be addressable if it can be directly addressed, i.e. if | |
10029 | its address can be taken, is a multiple of the type's alignment on | |
10030 | strict-alignment architectures and returns the first storage unit | |
10031 | assigned to the object represented by the tree. | |
10032 | ||
10033 | In the C family of languages, everything is in practice addressable | |
10034 | at the language level, except for bit-fields. This means that these | |
10035 | compilers will take the address of any tree that doesn't represent | |
10036 | a bit-field reference and expect the result to be the first storage | |
10037 | unit assigned to the object. Even in cases where this will result | |
10038 | in unaligned accesses at run time, nothing is supposed to be done | |
10039 | and the program is considered as erroneous instead (see PR c/18287). | |
10040 | ||
10041 | The implicit assumptions made in the middle-end are in keeping with | |
10042 | the C viewpoint described above: | |
10043 | - the address of a bit-field reference is supposed to be never | |
10044 | taken; the compiler (generally) will stop on such a construct, | |
10045 | - any other tree is addressable if it is formally addressable, | |
10046 | i.e. if it is formally allowed to be the operand of ADDR_EXPR. | |
10047 | ||
10048 | In Ada, the viewpoint is the opposite one: nothing is addressable | |
10049 | at the language level unless explicitly declared so. This means | |
10050 | that the compiler will both make sure that the trees representing | |
10051 | references to addressable ("aliased" in Ada parlance) objects are | |
10052 | addressable and make no real attempts at ensuring that the trees | |
10053 | representing references to non-addressable objects are addressable. | |
10054 | ||
10055 | In the first case, Ada is effectively equivalent to C and handing | |
10056 | down the direct result of applying ADDR_EXPR to these trees to the | |
10057 | middle-end works flawlessly. In the second case, Ada cannot afford | |
10058 | to consider the program as erroneous if the address of trees that | |
10059 | are not addressable is requested for technical reasons, unlike C; | |
10060 | as a consequence, the Ada compiler must arrange for either making | |
10061 | sure that this address is not requested in the middle-end or for | |
10062 | compensating by inserting temporaries if it is requested in Gigi. | |
10063 | ||
10064 | The first goal can be achieved because the middle-end should not | |
10065 | request the address of non-addressable trees on its own; the only | |
10066 | exception is for the invocation of low-level block operations like | |
10067 | memcpy, for which the addressability requirements are lower since | |
10068 | the type's alignment can be disregarded. In practice, this means | |
10069 | that Gigi must make sure that such operations cannot be applied to | |
10070 | non-BLKmode bit-fields. | |
10071 | ||
5a19bc0a EB |
10072 | The second goal is achieved by means of the addressable_p predicate, |
10073 | which computes whether a temporary must be inserted by Gigi when the | |
10074 | address of a tree is requested; if so, the address of the temporary | |
10075 | will be used in lieu of that of the original tree and some glue code | |
10076 | generated to connect everything together. */ | |
a1ab4c31 AC |
10077 | |
10078 | static bool | |
10079 | addressable_p (tree gnu_expr, tree gnu_type) | |
10080 | { | |
169afcb9 EB |
10081 | /* For an integral type, the size of the actual type of the object may not |
10082 | be greater than that of the expected type, otherwise an indirect access | |
10083 | in the latter type wouldn't correctly set all the bits of the object. */ | |
10084 | if (gnu_type | |
10085 | && INTEGRAL_TYPE_P (gnu_type) | |
10086 | && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr))) | |
10087 | return false; | |
10088 | ||
10089 | /* The size of the actual type of the object may not be smaller than that | |
10090 | of the expected type, otherwise an indirect access in the latter type | |
10091 | would be larger than the object. But only record types need to be | |
10092 | considered in practice for this case. */ | |
a1ab4c31 AC |
10093 | if (gnu_type |
10094 | && TREE_CODE (gnu_type) == RECORD_TYPE | |
169afcb9 | 10095 | && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type)) |
a1ab4c31 AC |
10096 | return false; |
10097 | ||
10098 | switch (TREE_CODE (gnu_expr)) | |
10099 | { | |
10100 | case VAR_DECL: | |
10101 | case PARM_DECL: | |
10102 | case FUNCTION_DECL: | |
10103 | case RESULT_DECL: | |
10104 | /* All DECLs are addressable: if they are in a register, we can force | |
10105 | them to memory. */ | |
10106 | return true; | |
10107 | ||
10108 | case UNCONSTRAINED_ARRAY_REF: | |
10109 | case INDIRECT_REF: | |
0b3467c4 | 10110 | /* Taking the address of a dereference yields the original pointer. */ |
42c08997 EB |
10111 | return true; |
10112 | ||
a1ab4c31 AC |
10113 | case STRING_CST: |
10114 | case INTEGER_CST: | |
1edbeb15 | 10115 | case REAL_CST: |
0b3467c4 EB |
10116 | /* Taking the address yields a pointer to the constant pool. */ |
10117 | return true; | |
10118 | ||
10119 | case CONSTRUCTOR: | |
10120 | /* Taking the address of a static constructor yields a pointer to the | |
10121 | tree constant pool. */ | |
10122 | return TREE_STATIC (gnu_expr) ? true : false; | |
10123 | ||
a1ab4c31 | 10124 | case NULL_EXPR: |
1edbeb15 | 10125 | case ADDR_EXPR: |
a1ab4c31 AC |
10126 | case SAVE_EXPR: |
10127 | case CALL_EXPR: | |
42c08997 EB |
10128 | case PLUS_EXPR: |
10129 | case MINUS_EXPR: | |
9f4afcd4 EB |
10130 | case BIT_IOR_EXPR: |
10131 | case BIT_XOR_EXPR: | |
10132 | case BIT_AND_EXPR: | |
10133 | case BIT_NOT_EXPR: | |
42c08997 EB |
10134 | /* All rvalues are deemed addressable since taking their address will |
10135 | force a temporary to be created by the middle-end. */ | |
a1ab4c31 AC |
10136 | return true; |
10137 | ||
0b3467c4 EB |
10138 | case COMPOUND_EXPR: |
10139 | /* The address of a compound expression is that of its 2nd operand. */ | |
10140 | return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type); | |
10141 | ||
a1ab4c31 AC |
10142 | case COND_EXPR: |
10143 | /* We accept &COND_EXPR as soon as both operands are addressable and | |
10144 | expect the outcome to be the address of the selected operand. */ | |
10145 | return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE) | |
10146 | && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE)); | |
10147 | ||
10148 | case COMPONENT_REF: | |
10149 | return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1)) | |
10150 | /* Even with DECL_BIT_FIELD cleared, we have to ensure that | |
10151 | the field is sufficiently aligned, in case it is subject | |
10152 | to a pragma Component_Alignment. But we don't need to | |
10153 | check the alignment of the containing record, as it is | |
10154 | guaranteed to be not smaller than that of its most | |
10155 | aligned field that is not a bit-field. */ | |
1e17ef87 | 10156 | && (!STRICT_ALIGNMENT |
a1ab4c31 AC |
10157 | || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1)) |
10158 | >= TYPE_ALIGN (TREE_TYPE (gnu_expr)))) | |
10159 | /* The field of a padding record is always addressable. */ | |
3c157c27 | 10160 | || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))) |
a1ab4c31 AC |
10161 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); |
10162 | ||
10163 | case ARRAY_REF: case ARRAY_RANGE_REF: | |
10164 | case REALPART_EXPR: case IMAGPART_EXPR: | |
10165 | case NOP_EXPR: | |
10166 | return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE); | |
10167 | ||
10168 | case CONVERT_EXPR: | |
10169 | return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr)) | |
10170 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); | |
10171 | ||
10172 | case VIEW_CONVERT_EXPR: | |
10173 | { | |
10174 | /* This is addressable if we can avoid a copy. */ | |
10175 | tree type = TREE_TYPE (gnu_expr); | |
10176 | tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0)); | |
10177 | return (((TYPE_MODE (type) == TYPE_MODE (inner_type) | |
10178 | && (!STRICT_ALIGNMENT | |
10179 | || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type) | |
10180 | || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT)) | |
10181 | || ((TYPE_MODE (type) == BLKmode | |
10182 | || TYPE_MODE (inner_type) == BLKmode) | |
10183 | && (!STRICT_ALIGNMENT | |
10184 | || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type) | |
10185 | || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT | |
10186 | || TYPE_ALIGN_OK (type) | |
10187 | || TYPE_ALIGN_OK (inner_type)))) | |
10188 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); | |
10189 | } | |
10190 | ||
10191 | default: | |
10192 | return false; | |
10193 | } | |
10194 | } | |
ce2d0ce2 | 10195 | |
d5ebeb8c EB |
10196 | /* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype. |
10197 | If a Freeze node exists for the entity, delay the bulk of the processing. | |
10198 | Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */ | |
a1ab4c31 AC |
10199 | |
10200 | void | |
10201 | process_type (Entity_Id gnat_entity) | |
10202 | { | |
10203 | tree gnu_old | |
d5ebeb8c EB |
10204 | = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE; |
10205 | ||
10206 | /* If we are to delay elaboration of this type, just do any elaboration | |
10207 | needed for expressions within the declaration and make a dummy node | |
10208 | for it and its Full_View (if any), in case something points to it. | |
10209 | Do not do this if it has already been done (the only way that can | |
10210 | happen is if the private completion is also delayed). */ | |
10211 | if (Present (Freeze_Node (gnat_entity))) | |
a1ab4c31 AC |
10212 | { |
10213 | elaborate_entity (gnat_entity); | |
10214 | ||
10215 | if (!gnu_old) | |
1e17ef87 | 10216 | { |
10069d53 | 10217 | tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity)); |
a1ab4c31 | 10218 | save_gnu_tree (gnat_entity, gnu_decl, false); |
7ed9919d | 10219 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
a1ab4c31 | 10220 | && Present (Full_View (gnat_entity))) |
65444786 EB |
10221 | { |
10222 | if (Has_Completion_In_Body (gnat_entity)) | |
10223 | DECL_TAFT_TYPE_P (gnu_decl) = 1; | |
10224 | save_gnu_tree (Full_View (gnat_entity), gnu_decl, false); | |
10225 | } | |
a1ab4c31 AC |
10226 | } |
10227 | ||
10228 | return; | |
10229 | } | |
10230 | ||
d5ebeb8c EB |
10231 | /* If we saved away a dummy type for this node, it means that this made the |
10232 | type that corresponds to the full type of an incomplete type. Clear that | |
5a4916be EB |
10233 | type for now and then update the type in the pointers below. But, if the |
10234 | saved type is not dummy, it very likely means that we have a use before | |
10235 | declaration for the type in the tree, what we really cannot handle. */ | |
a1ab4c31 AC |
10236 | if (gnu_old) |
10237 | { | |
10238 | gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL | |
10239 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))); | |
10240 | ||
10241 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
10242 | } | |
10243 | ||
10244 | /* Now fully elaborate the type. */ | |
d5ebeb8c | 10245 | tree gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, true); |
a1ab4c31 AC |
10246 | gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL); |
10247 | ||
65444786 EB |
10248 | /* If we have an old type and we've made pointers to this type, update those |
10249 | pointers. If this is a Taft amendment type in the main unit, we need to | |
10250 | mark the type as used since other units referencing it don't see the full | |
10251 | declaration and, therefore, cannot mark it as used themselves. */ | |
a1ab4c31 | 10252 | if (gnu_old) |
65444786 EB |
10253 | { |
10254 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)), | |
10255 | TREE_TYPE (gnu_new)); | |
10256 | if (DECL_TAFT_TYPE_P (gnu_old)) | |
10257 | used_types_insert (TREE_TYPE (gnu_new)); | |
10258 | } | |
a1ab4c31 AC |
10259 | |
10260 | /* If this is a record type corresponding to a task or protected type | |
10261 | that is a completion of an incomplete type, perform a similar update | |
1e17ef87 | 10262 | on the type. ??? Including protected types here is a guess. */ |
4ec7c4ec | 10263 | if (Is_Record_Type (gnat_entity) |
a1ab4c31 AC |
10264 | && Is_Concurrent_Record_Type (gnat_entity) |
10265 | && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity))) | |
10266 | { | |
10267 | tree gnu_task_old | |
10268 | = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)); | |
10269 | ||
10270 | save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity), | |
10271 | NULL_TREE, false); | |
10272 | save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity), | |
10273 | gnu_new, false); | |
10274 | ||
10275 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)), | |
10276 | TREE_TYPE (gnu_new)); | |
10277 | } | |
10278 | } | |
ce2d0ce2 | 10279 | |
b1b2b511 EB |
10280 | /* Subroutine of assoc_to_constructor: VALUES is a list of field associations, |
10281 | some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the | |
10282 | associations that are from RECORD_TYPE. If we see an internal record, make | |
10283 | a recursive call to fill it in as well. */ | |
10284 | ||
10285 | static tree | |
10286 | extract_values (tree values, tree record_type) | |
10287 | { | |
10288 | vec<constructor_elt, va_gc> *v = NULL; | |
10289 | tree field; | |
10290 | ||
10291 | for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field)) | |
10292 | { | |
10293 | tree tem, value = NULL_TREE; | |
10294 | ||
10295 | /* _Parent is an internal field, but may have values in the aggregate, | |
10296 | so check for values first. */ | |
10297 | if ((tem = purpose_member (field, values))) | |
10298 | { | |
10299 | value = TREE_VALUE (tem); | |
10300 | TREE_ADDRESSABLE (tem) = 1; | |
10301 | } | |
10302 | ||
10303 | else if (DECL_INTERNAL_P (field)) | |
10304 | { | |
10305 | value = extract_values (values, TREE_TYPE (field)); | |
10306 | if (TREE_CODE (value) == CONSTRUCTOR | |
10307 | && vec_safe_is_empty (CONSTRUCTOR_ELTS (value))) | |
10308 | value = NULL_TREE; | |
10309 | } | |
10310 | else | |
10311 | /* If we have a record subtype, the names will match, but not the | |
10312 | actual FIELD_DECLs. */ | |
10313 | for (tem = values; tem; tem = TREE_CHAIN (tem)) | |
10314 | if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field)) | |
10315 | { | |
10316 | value = convert (TREE_TYPE (field), TREE_VALUE (tem)); | |
10317 | TREE_ADDRESSABLE (tem) = 1; | |
10318 | } | |
10319 | ||
10320 | if (!value) | |
10321 | continue; | |
10322 | ||
10323 | CONSTRUCTOR_APPEND_ELT (v, field, value); | |
10324 | } | |
10325 | ||
10326 | return gnat_build_constructor (record_type, v); | |
10327 | } | |
10328 | ||
42acad07 EB |
10329 | /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the |
10330 | front of the Component_Associations of an N_Aggregate and GNU_TYPE is the | |
10331 | GCC type of the corresponding record type. Return the CONSTRUCTOR. */ | |
a1ab4c31 AC |
10332 | |
10333 | static tree | |
10334 | assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type) | |
10335 | { | |
42acad07 | 10336 | tree gnu_list = NULL_TREE, gnu_result; |
a1ab4c31 AC |
10337 | |
10338 | /* We test for GNU_FIELD being empty in the case where a variant | |
10339 | was the last thing since we don't take things off GNAT_ASSOC in | |
10340 | that case. We check GNAT_ASSOC in case we have a variant, but it | |
10341 | has no fields. */ | |
10342 | ||
42acad07 | 10343 | for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc)) |
a1ab4c31 | 10344 | { |
815b5368 EB |
10345 | const Node_Id gnat_field = First (Choices (gnat_assoc)); |
10346 | const Node_Id gnat_expr = Expression (gnat_assoc); | |
a1ab4c31 AC |
10347 | tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field)); |
10348 | tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc)); | |
10349 | ||
10350 | /* The expander is supposed to put a single component selector name | |
1e17ef87 | 10351 | in every record component association. */ |
a1ab4c31 AC |
10352 | gcc_assert (No (Next (gnat_field))); |
10353 | ||
4ec7c4ec EB |
10354 | /* Ignore discriminants that have Corresponding_Discriminants in tagged |
10355 | types since we'll be setting those fields in the parent subtype. */ | |
10356 | if (Ekind (Entity (gnat_field)) == E_Discriminant | |
10357 | && Present (Corresponding_Discriminant (Entity (gnat_field))) | |
a1ab4c31 AC |
10358 | && Is_Tagged_Type (Scope (Entity (gnat_field)))) |
10359 | continue; | |
10360 | ||
10361 | /* Also ignore discriminants of Unchecked_Unions. */ | |
4ec7c4ec EB |
10362 | if (Ekind (Entity (gnat_field)) == E_Discriminant |
10363 | && Is_Unchecked_Union (gnat_entity)) | |
a1ab4c31 AC |
10364 | continue; |
10365 | ||
815b5368 | 10366 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 | 10367 | |
815b5368 | 10368 | /* Convert to the type of the field. */ |
a1ab4c31 AC |
10369 | gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr); |
10370 | ||
10371 | /* Add the field and expression to the list. */ | |
10372 | gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list); | |
10373 | } | |
10374 | ||
10375 | gnu_result = extract_values (gnu_list, gnu_type); | |
10376 | ||
9abe8b74 MM |
10377 | if (flag_checking) |
10378 | { | |
10379 | /* Verify that every entry in GNU_LIST was used. */ | |
10380 | for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list)) | |
10381 | gcc_assert (TREE_ADDRESSABLE (gnu_list)); | |
10382 | } | |
a1ab4c31 AC |
10383 | |
10384 | return gnu_result; | |
10385 | } | |
10386 | ||
1e17ef87 EB |
10387 | /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is |
10388 | the first element of an array aggregate. It may itself be an aggregate. | |
815b5368 | 10389 | GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate. */ |
a1ab4c31 AC |
10390 | |
10391 | static tree | |
815b5368 | 10392 | pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type) |
a1ab4c31 | 10393 | { |
a1ab4c31 | 10394 | tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type)); |
9771b263 | 10395 | vec<constructor_elt, va_gc> *gnu_expr_vec = NULL; |
a1ab4c31 | 10396 | |
b1b2b511 | 10397 | for (; Present (gnat_expr); gnat_expr = Next (gnat_expr)) |
a1ab4c31 | 10398 | { |
b1b2b511 EB |
10399 | tree gnu_expr; |
10400 | ||
a1ab4c31 AC |
10401 | /* If the expression is itself an array aggregate then first build the |
10402 | innermost constructor if it is part of our array (multi-dimensional | |
10403 | case). */ | |
a1ab4c31 AC |
10404 | if (Nkind (gnat_expr) == N_Aggregate |
10405 | && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE | |
10406 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type))) | |
10407 | gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)), | |
815b5368 | 10408 | TREE_TYPE (gnu_array_type)); |
a1ab4c31 AC |
10409 | else |
10410 | { | |
babf697a EB |
10411 | /* If the expression is a conversion to an unconstrained array type, |
10412 | skip it to avoid spilling to memory. */ | |
10413 | if (Nkind (gnat_expr) == N_Type_Conversion | |
10414 | && Is_Array_Type (Etype (gnat_expr)) | |
10415 | && !Is_Constrained (Etype (gnat_expr))) | |
10416 | gnu_expr = gnat_to_gnu (Expression (gnat_expr)); | |
10417 | else | |
10418 | gnu_expr = gnat_to_gnu (gnat_expr); | |
a1ab4c31 | 10419 | |
815b5368 | 10420 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 AC |
10421 | } |
10422 | ||
0e228dd9 NF |
10423 | CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index, |
10424 | convert (TREE_TYPE (gnu_array_type), gnu_expr)); | |
a1ab4c31 | 10425 | |
8b9aec86 RS |
10426 | gnu_index = int_const_binop (PLUS_EXPR, gnu_index, |
10427 | convert (TREE_TYPE (gnu_index), | |
10428 | integer_one_node)); | |
a1ab4c31 AC |
10429 | } |
10430 | ||
0e228dd9 | 10431 | return gnat_build_constructor (gnu_array_type, gnu_expr_vec); |
a1ab4c31 | 10432 | } |
ce2d0ce2 | 10433 | |
f04b8d69 EB |
10434 | /* Process a N_Validate_Unchecked_Conversion node. */ |
10435 | ||
10436 | static void | |
10437 | validate_unchecked_conversion (Node_Id gnat_node) | |
10438 | { | |
10439 | tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node)); | |
10440 | tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node)); | |
10441 | ||
10442 | /* If the target is a pointer type, see if we are either converting from a | |
10443 | non-pointer or from a pointer to a type with a different alias set and | |
10444 | warn if so, unless the pointer has been marked to alias everything. */ | |
10445 | if (POINTER_TYPE_P (gnu_target_type) | |
10446 | && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type)) | |
10447 | { | |
10448 | tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type) | |
10449 | ? TREE_TYPE (gnu_source_type) | |
10450 | : NULL_TREE; | |
10451 | tree gnu_target_desig_type = TREE_TYPE (gnu_target_type); | |
10452 | alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type); | |
10453 | ||
10454 | if (target_alias_set != 0 | |
10455 | && (!POINTER_TYPE_P (gnu_source_type) | |
10456 | || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type), | |
10457 | target_alias_set))) | |
10458 | { | |
4a29b8d6 | 10459 | post_error_ne ("??possible aliasing problem for type&", |
f04b8d69 EB |
10460 | gnat_node, Target_Type (gnat_node)); |
10461 | post_error ("\\?use -fno-strict-aliasing switch for references", | |
10462 | gnat_node); | |
10463 | post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`", | |
10464 | gnat_node, Target_Type (gnat_node)); | |
10465 | } | |
10466 | } | |
10467 | ||
10468 | /* Likewise if the target is a fat pointer type, but we have no mechanism to | |
10469 | mitigate the problem in this case, so we unconditionally warn. */ | |
10470 | else if (TYPE_IS_FAT_POINTER_P (gnu_target_type)) | |
10471 | { | |
10472 | tree gnu_source_desig_type | |
10473 | = TYPE_IS_FAT_POINTER_P (gnu_source_type) | |
10474 | ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type))) | |
10475 | : NULL_TREE; | |
10476 | tree gnu_target_desig_type | |
10477 | = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type))); | |
10478 | alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type); | |
10479 | ||
10480 | if (target_alias_set != 0 | |
10481 | && (!TYPE_IS_FAT_POINTER_P (gnu_source_type) | |
10482 | || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type), | |
10483 | target_alias_set))) | |
10484 | { | |
4a29b8d6 | 10485 | post_error_ne ("??possible aliasing problem for type&", |
f04b8d69 EB |
10486 | gnat_node, Target_Type (gnat_node)); |
10487 | post_error ("\\?use -fno-strict-aliasing switch for references", | |
10488 | gnat_node); | |
10489 | } | |
10490 | } | |
10491 | } | |
ce2d0ce2 | 10492 | |
fa6fd7b7 AO |
10493 | /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a |
10494 | source code location and false if it doesn't. If CLEAR_COLUMN is | |
10495 | true, set the column information to 0. If DECL is given and SLOC | |
10496 | refers to a File with an instance, map DECL to that instance. */ | |
a1ab4c31 | 10497 | |
ba464315 | 10498 | bool |
fa6fd7b7 AO |
10499 | Sloc_to_locus (Source_Ptr Sloc, location_t *locus, bool clear_column, |
10500 | const_tree decl) | |
a1ab4c31 AC |
10501 | { |
10502 | if (Sloc == No_Location) | |
10503 | return false; | |
10504 | ||
10505 | if (Sloc <= Standard_Location) | |
10506 | { | |
10069d53 | 10507 | *locus = BUILTINS_LOCATION; |
a1ab4c31 AC |
10508 | return false; |
10509 | } | |
a1ab4c31 | 10510 | |
ba464315 | 10511 | Source_File_Index file = Get_Source_File_Index (Sloc); |
b67e2ad8 EB |
10512 | Line_Number_Type line = Get_Logical_Line_Number (Sloc); |
10513 | Column_Number_Type column = (clear_column ? 0 : Get_Column_Number (Sloc)); | |
ba464315 | 10514 | line_map_ordinary *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1); |
b7562769 | 10515 | |
ba464315 EB |
10516 | /* We can have zero if pragma Source_Reference is in effect. */ |
10517 | if (line < 1) | |
10518 | line = 1; | |
a1ab4c31 | 10519 | |
ba464315 | 10520 | /* Translate the location. */ |
b1b2b511 EB |
10521 | *locus |
10522 | = linemap_position_for_line_and_column (line_table, map, line, column); | |
a1ab4c31 | 10523 | |
025e3d57 | 10524 | if (decl && file_map && file_map[file - 1].Instance) |
fa6fd7b7 AO |
10525 | decl_to_instance_map->put (decl, file_map[file - 1].Instance); |
10526 | ||
a1ab4c31 AC |
10527 | return true; |
10528 | } | |
10529 | ||
56b8aa0c EB |
10530 | /* Return whether GNAT_NODE is a defining identifier for a renaming that comes |
10531 | from the parameter association for the instantiation of a generic. We do | |
10532 | not want to emit source location for them: the code generated for their | |
10533 | initialization is likely to disturb debugging. */ | |
10534 | ||
10535 | bool | |
10536 | renaming_from_instantiation_p (Node_Id gnat_node) | |
10537 | { | |
10538 | if (Nkind (gnat_node) != N_Defining_Identifier | |
10539 | || !Is_Object (gnat_node) | |
10540 | || Comes_From_Source (gnat_node) | |
10541 | || !Present (Renamed_Object (gnat_node))) | |
10542 | return false; | |
10543 | ||
10544 | /* Get the object declaration of the renamed object, if any and if the | |
10545 | renamed object is a mere identifier. */ | |
10546 | gnat_node = Renamed_Object (gnat_node); | |
10547 | if (Nkind (gnat_node) != N_Identifier) | |
10548 | return false; | |
10549 | ||
10550 | gnat_node = Parent (Entity (gnat_node)); | |
10551 | return (Present (gnat_node) | |
10552 | && Nkind (gnat_node) == N_Object_Declaration | |
10553 | && Present (Corresponding_Generic_Association (gnat_node))); | |
10554 | } | |
10555 | ||
a1ab4c31 | 10556 | /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and |
ba464315 EB |
10557 | don't do anything if it doesn't correspond to a source location. And, |
10558 | if CLEAR_COLUMN is true, set the column information to 0. */ | |
a1ab4c31 AC |
10559 | |
10560 | static void | |
ba464315 | 10561 | set_expr_location_from_node (tree node, Node_Id gnat_node, bool clear_column) |
a1ab4c31 AC |
10562 | { |
10563 | location_t locus; | |
10564 | ||
56b8aa0c EB |
10565 | /* Do not set a location for constructs likely to disturb debugging. */ |
10566 | if (Nkind (gnat_node) == N_Defining_Identifier) | |
10567 | { | |
10568 | if (Is_Type (gnat_node) && Is_Actual_Subtype (gnat_node)) | |
10569 | return; | |
10570 | ||
10571 | if (renaming_from_instantiation_p (gnat_node)) | |
10572 | return; | |
10573 | } | |
10574 | ||
ba464315 | 10575 | if (!Sloc_to_locus (Sloc (gnat_node), &locus, clear_column)) |
a1ab4c31 AC |
10576 | return; |
10577 | ||
10578 | SET_EXPR_LOCATION (node, locus); | |
10579 | } | |
17c168fe EB |
10580 | |
10581 | /* More elaborate version of set_expr_location_from_node to be used in more | |
10582 | general contexts, for example the result of the translation of a generic | |
10583 | GNAT node. */ | |
10584 | ||
10585 | static void | |
10586 | set_gnu_expr_location_from_node (tree node, Node_Id gnat_node) | |
10587 | { | |
10588 | /* Set the location information on the node if it is a real expression. | |
10589 | References can be reused for multiple GNAT nodes and they would get | |
10590 | the location information of their last use. Also make sure not to | |
10591 | overwrite an existing location as it is probably more precise. */ | |
10592 | ||
10593 | switch (TREE_CODE (node)) | |
10594 | { | |
10595 | CASE_CONVERT: | |
10596 | case NON_LVALUE_EXPR: | |
384e3fb1 | 10597 | case SAVE_EXPR: |
17c168fe EB |
10598 | break; |
10599 | ||
10600 | case COMPOUND_EXPR: | |
10601 | if (EXPR_P (TREE_OPERAND (node, 1))) | |
10602 | set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node); | |
10603 | ||
9c453de7 | 10604 | /* ... fall through ... */ |
17c168fe EB |
10605 | |
10606 | default: | |
10607 | if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node)) | |
2a02d090 OH |
10608 | { |
10609 | set_expr_location_from_node (node, gnat_node); | |
10610 | set_end_locus_from_node (node, gnat_node); | |
10611 | } | |
17c168fe EB |
10612 | break; |
10613 | } | |
10614 | } | |
ba464315 EB |
10615 | |
10616 | /* Set the end_locus information for GNU_NODE, if any, from an explicit end | |
10617 | location associated with GNAT_NODE or GNAT_NODE itself, whichever makes | |
10618 | most sense. Return true if a sensible assignment was performed. */ | |
10619 | ||
10620 | static bool | |
10621 | set_end_locus_from_node (tree gnu_node, Node_Id gnat_node) | |
10622 | { | |
10623 | Node_Id gnat_end_label; | |
10624 | location_t end_locus; | |
10625 | ||
10626 | /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node | |
10627 | end_locus when there is one. We consider only GNAT nodes with a possible | |
10628 | End_Label attached. If the End_Label actually was unassigned, fallback | |
10629 | on the original node. We'd better assign an explicit sloc associated with | |
10630 | the outer construct in any case. */ | |
10631 | ||
10632 | switch (Nkind (gnat_node)) | |
10633 | { | |
10634 | case N_Package_Body: | |
10635 | case N_Subprogram_Body: | |
10636 | case N_Block_Statement: | |
650ad524 BD |
10637 | if (Present (Handled_Statement_Sequence (gnat_node))) |
10638 | gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node)); | |
10639 | else | |
10640 | gnat_end_label = Empty; | |
ba464315 EB |
10641 | break; |
10642 | ||
10643 | case N_Package_Declaration: | |
650ad524 | 10644 | gcc_checking_assert (Present (Specification (gnat_node))); |
ba464315 EB |
10645 | gnat_end_label = End_Label (Specification (gnat_node)); |
10646 | break; | |
10647 | ||
10648 | default: | |
10649 | return false; | |
10650 | } | |
10651 | ||
10652 | if (Present (gnat_end_label)) | |
10653 | gnat_node = gnat_end_label; | |
10654 | ||
10655 | /* Some expanded subprograms have neither an End_Label nor a Sloc | |
10656 | attached. Notify that to callers. For a block statement with no | |
10657 | End_Label, clear column information, so that the tree for a | |
10658 | transient block does not receive the sloc of a source condition. */ | |
10659 | if (!Sloc_to_locus (Sloc (gnat_node), &end_locus, | |
10660 | No (gnat_end_label) | |
0b66f882 | 10661 | && Nkind (gnat_node) == N_Block_Statement)) |
ba464315 EB |
10662 | return false; |
10663 | ||
10664 | switch (TREE_CODE (gnu_node)) | |
10665 | { | |
10666 | case BIND_EXPR: | |
10667 | BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus; | |
10668 | return true; | |
10669 | ||
10670 | case FUNCTION_DECL: | |
10671 | DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus; | |
10672 | return true; | |
10673 | ||
10674 | default: | |
10675 | return false; | |
10676 | } | |
10677 | } | |
ce2d0ce2 | 10678 | |
a1ab4c31 AC |
10679 | /* Post an error message. MSG is the error message, properly annotated. |
10680 | NODE is the node at which to post the error and the node to use for the | |
586388fd | 10681 | '&' substitution. */ |
a1ab4c31 AC |
10682 | |
10683 | void | |
10684 | post_error (const char *msg, Node_Id node) | |
10685 | { | |
10686 | String_Template temp; | |
93582885 | 10687 | String_Pointer sp; |
a1ab4c31 | 10688 | |
457f72ac EB |
10689 | if (No (node)) |
10690 | return; | |
10691 | ||
10692 | temp.Low_Bound = 1; | |
10693 | temp.High_Bound = strlen (msg); | |
93582885 EB |
10694 | sp.Bounds = &temp; |
10695 | sp.Array = msg; | |
10696 | Error_Msg_N (sp, node); | |
a1ab4c31 AC |
10697 | } |
10698 | ||
586388fd EB |
10699 | /* Similar to post_error, but NODE is the node at which to post the error and |
10700 | ENT is the node to use for the '&' substitution. */ | |
a1ab4c31 AC |
10701 | |
10702 | void | |
10703 | post_error_ne (const char *msg, Node_Id node, Entity_Id ent) | |
10704 | { | |
10705 | String_Template temp; | |
93582885 | 10706 | String_Pointer sp; |
a1ab4c31 | 10707 | |
457f72ac EB |
10708 | if (No (node)) |
10709 | return; | |
10710 | ||
10711 | temp.Low_Bound = 1; | |
10712 | temp.High_Bound = strlen (msg); | |
93582885 EB |
10713 | sp.Bounds = &temp; |
10714 | sp.Array = msg; | |
10715 | Error_Msg_NE (sp, node, ent); | |
a1ab4c31 AC |
10716 | } |
10717 | ||
586388fd | 10718 | /* Similar to post_error_ne, but NUM is the number to use for the '^'. */ |
a1ab4c31 AC |
10719 | |
10720 | void | |
58c8f770 | 10721 | post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num) |
a1ab4c31 | 10722 | { |
58c8f770 | 10723 | Error_Msg_Uint_1 = UI_From_Int (num); |
586388fd | 10724 | post_error_ne (msg, node, ent); |
a1ab4c31 | 10725 | } |
2a02d090 | 10726 | |
586388fd EB |
10727 | /* Similar to post_error_ne, but T is a GCC tree representing the number to |
10728 | write. If T represents a constant, the text inside curly brackets in | |
10729 | MSG will be output (presumably including a '^'). Otherwise it will not | |
10730 | be output and the text inside square brackets will be output instead. */ | |
a1ab4c31 AC |
10731 | |
10732 | void | |
10733 | post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t) | |
10734 | { | |
586388fd | 10735 | char *new_msg = XALLOCAVEC (char, strlen (msg) + 1); |
a1ab4c31 AC |
10736 | char start_yes, end_yes, start_no, end_no; |
10737 | const char *p; | |
10738 | char *q; | |
10739 | ||
586388fd | 10740 | if (TREE_CODE (t) == INTEGER_CST) |
a1ab4c31 | 10741 | { |
586388fd | 10742 | Error_Msg_Uint_1 = UI_From_gnu (t); |
a1ab4c31 AC |
10743 | start_yes = '{', end_yes = '}', start_no = '[', end_no = ']'; |
10744 | } | |
10745 | else | |
10746 | start_yes = '[', end_yes = ']', start_no = '{', end_no = '}'; | |
10747 | ||
586388fd | 10748 | for (p = msg, q = new_msg; *p; p++) |
a1ab4c31 AC |
10749 | { |
10750 | if (*p == start_yes) | |
10751 | for (p++; *p != end_yes; p++) | |
10752 | *q++ = *p; | |
10753 | else if (*p == start_no) | |
10754 | for (p++; *p != end_no; p++) | |
10755 | ; | |
10756 | else | |
10757 | *q++ = *p; | |
10758 | } | |
10759 | ||
10760 | *q = 0; | |
10761 | ||
586388fd | 10762 | post_error_ne (new_msg, node, ent); |
a1ab4c31 AC |
10763 | } |
10764 | ||
586388fd | 10765 | /* Similar to post_error_ne_tree, but NUM is a second integer to write. */ |
a1ab4c31 AC |
10766 | |
10767 | void | |
1e17ef87 EB |
10768 | post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t, |
10769 | int num) | |
a1ab4c31 AC |
10770 | { |
10771 | Error_Msg_Uint_2 = UI_From_Int (num); | |
10772 | post_error_ne_tree (msg, node, ent, t); | |
10773 | } | |
7c775aca | 10774 | |
8f8f531f | 10775 | /* Return a label to branch to for the exception type in KIND or Empty |
7c775aca EB |
10776 | if none. */ |
10777 | ||
8f8f531f | 10778 | Entity_Id |
7c775aca EB |
10779 | get_exception_label (char kind) |
10780 | { | |
10781 | switch (kind) | |
10782 | { | |
10783 | case N_Raise_Constraint_Error: | |
8f8f531f | 10784 | return gnu_constraint_error_label_stack.last (); |
7c775aca EB |
10785 | |
10786 | case N_Raise_Storage_Error: | |
8f8f531f | 10787 | return gnu_storage_error_label_stack.last (); |
7c775aca EB |
10788 | |
10789 | case N_Raise_Program_Error: | |
8f8f531f | 10790 | return gnu_program_error_label_stack.last (); |
7c775aca EB |
10791 | |
10792 | default: | |
8f8f531f | 10793 | return Empty; |
7c775aca EB |
10794 | } |
10795 | ||
8f8f531f | 10796 | gcc_unreachable (); |
7c775aca EB |
10797 | } |
10798 | ||
10799 | /* Return the decl for the current elaboration procedure. */ | |
10800 | ||
7543c8b6 | 10801 | static tree |
7c775aca EB |
10802 | get_elaboration_procedure (void) |
10803 | { | |
10804 | return gnu_elab_proc_stack->last (); | |
10805 | } | |
10806 | ||
44662f68 EB |
10807 | /* Return the controlling type of a dispatching subprogram. */ |
10808 | ||
10809 | static Entity_Id | |
10810 | get_controlling_type (Entity_Id subprog) | |
10811 | { | |
17a98a3d | 10812 | /* This is modeled on Expand_Interface_Thunk. */ |
44662f68 EB |
10813 | Entity_Id controlling_type = Etype (First_Formal (subprog)); |
10814 | if (Is_Access_Type (controlling_type)) | |
10815 | controlling_type = Directly_Designated_Type (controlling_type); | |
10816 | controlling_type = Underlying_Type (controlling_type); | |
10817 | if (Is_Concurrent_Type (controlling_type)) | |
10818 | controlling_type = Corresponding_Record_Type (controlling_type); | |
10819 | controlling_type = Base_Type (controlling_type); | |
10820 | return controlling_type; | |
10821 | } | |
10822 | ||
10823 | /* Return whether we should use an alias for the TARGET of a thunk | |
10824 | in order to make the call generated in the thunk local. */ | |
10825 | ||
10826 | static bool | |
10827 | use_alias_for_thunk_p (tree target) | |
10828 | { | |
10829 | /* We cannot generate a local call in this case. */ | |
10830 | if (DECL_EXTERNAL (target)) | |
10831 | return false; | |
10832 | ||
10833 | /* The call is already local in this case. */ | |
10834 | if (TREE_CODE (DECL_CONTEXT (target)) == FUNCTION_DECL) | |
10835 | return false; | |
10836 | ||
10837 | return TARGET_USE_LOCAL_THUNK_ALIAS_P (target); | |
10838 | } | |
10839 | ||
10840 | static GTY(()) unsigned long thunk_labelno = 0; | |
10841 | ||
10842 | /* Create an alias for TARGET to be used as the target of a thunk. */ | |
10843 | ||
10844 | static tree | |
10845 | make_alias_for_thunk (tree target) | |
10846 | { | |
10847 | char buf[64]; | |
10848 | targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno++); | |
10849 | ||
10850 | tree alias = build_decl (DECL_SOURCE_LOCATION (target), TREE_CODE (target), | |
10851 | get_identifier (buf), TREE_TYPE (target)); | |
10852 | ||
10853 | DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target); | |
10854 | DECL_CONTEXT (alias) = DECL_CONTEXT (target); | |
10855 | TREE_READONLY (alias) = TREE_READONLY (target); | |
10856 | TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target); | |
10857 | DECL_ARTIFICIAL (alias) = 1; | |
10858 | DECL_INITIAL (alias) = error_mark_node; | |
10859 | DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target)); | |
10860 | TREE_ADDRESSABLE (alias) = 1; | |
10861 | SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias)); | |
10862 | ||
10863 | cgraph_node *n = cgraph_node::create_same_body_alias (alias, target); | |
10864 | gcc_assert (n); | |
10865 | ||
10866 | return alias; | |
10867 | } | |
10868 | ||
5d01fc7c | 10869 | /* Create the local covariant part of {GNAT,GNU}_THUNK. */ |
44662f68 EB |
10870 | |
10871 | static tree | |
10872 | make_covariant_thunk (Entity_Id gnat_thunk, tree gnu_thunk) | |
10873 | { | |
10874 | tree gnu_name = create_concat_name (gnat_thunk, "CV"); | |
10875 | tree gnu_cv_thunk | |
10876 | = build_decl (DECL_SOURCE_LOCATION (gnu_thunk), TREE_CODE (gnu_thunk), | |
10877 | gnu_name, TREE_TYPE (gnu_thunk)); | |
10878 | ||
10879 | DECL_ARGUMENTS (gnu_cv_thunk) = copy_list (DECL_ARGUMENTS (gnu_thunk)); | |
5d01fc7c EB |
10880 | for (tree param_decl = DECL_ARGUMENTS (gnu_cv_thunk); |
10881 | param_decl; | |
10882 | param_decl = DECL_CHAIN (param_decl)) | |
10883 | DECL_CONTEXT (param_decl) = gnu_cv_thunk; | |
10884 | ||
44662f68 EB |
10885 | DECL_RESULT (gnu_cv_thunk) = copy_node (DECL_RESULT (gnu_thunk)); |
10886 | DECL_CONTEXT (DECL_RESULT (gnu_cv_thunk)) = gnu_cv_thunk; | |
10887 | ||
10888 | DECL_LANG_SPECIFIC (gnu_cv_thunk) = DECL_LANG_SPECIFIC (gnu_thunk); | |
10889 | DECL_CONTEXT (gnu_cv_thunk) = DECL_CONTEXT (gnu_thunk); | |
10890 | TREE_READONLY (gnu_cv_thunk) = TREE_READONLY (gnu_thunk); | |
10891 | TREE_THIS_VOLATILE (gnu_cv_thunk) = TREE_THIS_VOLATILE (gnu_thunk); | |
44662f68 EB |
10892 | DECL_ARTIFICIAL (gnu_cv_thunk) = 1; |
10893 | ||
10894 | return gnu_cv_thunk; | |
10895 | } | |
10896 | ||
10897 | /* Try to create a GNU thunk for {GNAT,GNU}_THUNK and return true on success. | |
10898 | ||
10899 | GNU thunks are more efficient than GNAT thunks because they don't call into | |
10900 | the runtime to retrieve the offset used in the displacement operation, but | |
10901 | they are tailored to C++ and thus too limited to support the full range of | |
10902 | thunks generated in Ada. Here's the complete list of limitations: | |
10903 | ||
10904 | 1. Multi-controlling thunks, i.e thunks with more than one controlling | |
10905 | parameter, are simply not supported. | |
10906 | ||
10907 | 2. Covariant thunks, i.e. thunks for which the result is also controlling, | |
10908 | are split into a pair of (this, covariant-only) thunks. | |
10909 | ||
10910 | 3. Variable-offset thunks, i.e. thunks for which the offset depends on the | |
10911 | object and not only on its type, are supported as 2nd class citizens. | |
10912 | ||
10913 | 4. External thunks, i.e. thunks for which the target is not declared in | |
10914 | the same unit as the thunk, are supported as 2nd class citizens. | |
10915 | ||
10916 | 5. Local thunks, i.e. thunks generated for a local type, are supported as | |
10917 | 2nd class citizens. */ | |
10918 | ||
10919 | static bool | |
10920 | maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk) | |
10921 | { | |
89e037d0 EB |
10922 | /* We use the Thunk_Target to compute the properties of the thunk. */ |
10923 | const Entity_Id gnat_target = Thunk_Target (gnat_thunk); | |
44662f68 EB |
10924 | |
10925 | /* Check that the first formal of the target is the only controlling one. */ | |
10926 | Entity_Id gnat_formal = First_Formal (gnat_target); | |
10927 | if (!Is_Controlling_Formal (gnat_formal)) | |
10928 | return false; | |
10929 | for (gnat_formal = Next_Formal (gnat_formal); | |
10930 | Present (gnat_formal); | |
10931 | gnat_formal = Next_Formal (gnat_formal)) | |
10932 | if (Is_Controlling_Formal (gnat_formal)) | |
10933 | return false; | |
10934 | ||
10935 | /* Look for the types that control the target and the thunk. */ | |
10936 | const Entity_Id gnat_controlling_type = get_controlling_type (gnat_target); | |
10937 | const Entity_Id gnat_interface_type = get_controlling_type (gnat_thunk); | |
10938 | ||
20dc266e JM |
10939 | /* We must have an interface type at this point. */ |
10940 | gcc_assert (Is_Interface (gnat_interface_type)); | |
10941 | ||
44662f68 EB |
10942 | /* Now compute whether the former covers the latter. */ |
10943 | const Entity_Id gnat_interface_tag | |
20dc266e | 10944 | = Find_Interface_Tag (gnat_controlling_type, gnat_interface_type); |
44662f68 EB |
10945 | tree gnu_interface_tag |
10946 | = Present (gnat_interface_tag) | |
10947 | ? gnat_to_gnu_field_decl (gnat_interface_tag) | |
10948 | : NULL_TREE; | |
10949 | tree gnu_interface_offset | |
10950 | = gnu_interface_tag ? byte_position (gnu_interface_tag) : NULL_TREE; | |
10951 | ||
10952 | /* There are three ways to retrieve the offset between the interface view | |
10953 | and the base object. Either the controlling type covers the interface | |
10954 | type and the offset of the corresponding tag is fixed, in which case it | |
10955 | can be statically encoded in the thunk (see FIXED_OFFSET below). Or the | |
10956 | controlling type doesn't cover the interface type but is of fixed size, | |
10957 | in which case the offset is stored in the dispatch table, two pointers | |
10958 | above the dispatch table address (see VIRTUAL_VALUE below). Otherwise, | |
10959 | the offset is variable and is stored right after the tag in every object | |
10960 | (see INDIRECT_OFFSET below). See also a-tags.ads for more details. */ | |
10961 | HOST_WIDE_INT fixed_offset, virtual_value, indirect_offset; | |
10962 | tree virtual_offset; | |
10963 | ||
10964 | if (gnu_interface_offset && TREE_CODE (gnu_interface_offset) == INTEGER_CST) | |
10965 | { | |
10966 | fixed_offset = - tree_to_shwi (gnu_interface_offset); | |
10967 | virtual_value = 0; | |
10968 | virtual_offset = NULL_TREE; | |
10969 | indirect_offset = 0; | |
10970 | } | |
10971 | else if (!gnu_interface_offset | |
10972 | && !Is_Variable_Size_Record (gnat_controlling_type)) | |
10973 | { | |
10974 | fixed_offset = 0; | |
10975 | virtual_value = - 2 * (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT); | |
10976 | virtual_offset = build_int_cst (integer_type_node, virtual_value); | |
10977 | indirect_offset = 0; | |
10978 | } | |
10979 | else | |
10980 | { | |
10981 | /* Covariant thunks with variable offset are not supported. */ | |
10982 | if (Has_Controlling_Result (gnat_target)) | |
10983 | return false; | |
10984 | ||
10985 | fixed_offset = 0; | |
10986 | virtual_value = 0; | |
10987 | virtual_offset = NULL_TREE; | |
10988 | indirect_offset = (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT); | |
10989 | } | |
10990 | ||
89e037d0 EB |
10991 | /* But we generate a call to the Thunk_Entity in the thunk. */ |
10992 | tree gnu_target | |
10993 | = gnat_to_gnu_entity (Thunk_Entity (gnat_thunk), NULL_TREE, false); | |
44662f68 | 10994 | |
67c4d1c7 EB |
10995 | /* If the target is local, then thunk and target must have the same context |
10996 | because cgraph_node::expand_thunk can only forward the static chain. */ | |
10997 | if (DECL_STATIC_CHAIN (gnu_target) | |
10998 | && DECL_CONTEXT (gnu_thunk) != DECL_CONTEXT (gnu_target)) | |
10999 | return false; | |
44662f68 EB |
11000 | |
11001 | /* If the target returns by invisible reference and is external, apply the | |
11002 | same transformation as Subprogram_Body_to_gnu here. */ | |
11003 | if (TREE_ADDRESSABLE (TREE_TYPE (gnu_target)) | |
11004 | && DECL_EXTERNAL (gnu_target) | |
11005 | && !POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (gnu_target)))) | |
11006 | { | |
11007 | TREE_TYPE (DECL_RESULT (gnu_target)) | |
11008 | = build_reference_type (TREE_TYPE (DECL_RESULT (gnu_target))); | |
11009 | relayout_decl (DECL_RESULT (gnu_target)); | |
11010 | } | |
11011 | ||
11012 | /* The thunk expander requires the return types of thunk and target to be | |
11013 | compatible, which is not fully the case with the CICO mechanism. */ | |
11014 | if (TYPE_CI_CO_LIST (TREE_TYPE (gnu_thunk))) | |
11015 | { | |
11016 | tree gnu_target_type = TREE_TYPE (gnu_target); | |
11017 | gcc_assert (TYPE_CI_CO_LIST (gnu_target_type)); | |
11018 | TYPE_CANONICAL (TREE_TYPE (TREE_TYPE (gnu_thunk))) | |
11019 | = TYPE_CANONICAL (TREE_TYPE (gnu_target_type)); | |
11020 | } | |
11021 | ||
11022 | cgraph_node *target_node = cgraph_node::get_create (gnu_target); | |
11023 | ||
5d01fc7c EB |
11024 | /* We may also need to create an alias for the target in order to make |
11025 | the call local, depending on the linkage of the target. */ | |
11026 | tree gnu_alias = use_alias_for_thunk_p (gnu_target) | |
11027 | ? make_alias_for_thunk (gnu_target) | |
11028 | : gnu_target; | |
11029 | ||
44662f68 EB |
11030 | /* If the return type of the target is a controlling type, then we need |
11031 | both an usual this thunk and a covariant thunk in this order: | |
11032 | ||
11033 | this thunk --> covariant thunk --> target | |
11034 | ||
11035 | For covariant thunks, we can only handle a fixed offset. */ | |
11036 | if (Has_Controlling_Result (gnat_target)) | |
11037 | { | |
11038 | gcc_assert (fixed_offset < 0); | |
11039 | tree gnu_cv_thunk = make_covariant_thunk (gnat_thunk, gnu_thunk); | |
11040 | target_node->create_thunk (gnu_cv_thunk, gnu_target, false, | |
11041 | - fixed_offset, 0, 0, | |
5d01fc7c | 11042 | NULL_TREE, gnu_alias); |
44662f68 | 11043 | |
5d01fc7c | 11044 | gnu_alias = gnu_target = gnu_cv_thunk; |
44662f68 EB |
11045 | } |
11046 | ||
44662f68 EB |
11047 | target_node->create_thunk (gnu_thunk, gnu_target, true, |
11048 | fixed_offset, virtual_value, indirect_offset, | |
11049 | virtual_offset, gnu_alias); | |
11050 | ||
11051 | return true; | |
11052 | } | |
11053 | ||
a1ab4c31 AC |
11054 | /* Initialize the table that maps GNAT codes to GCC codes for simple |
11055 | binary and unary operations. */ | |
11056 | ||
11057 | static void | |
11058 | init_code_table (void) | |
11059 | { | |
a1ab4c31 AC |
11060 | gnu_codes[N_Op_And] = TRUTH_AND_EXPR; |
11061 | gnu_codes[N_Op_Or] = TRUTH_OR_EXPR; | |
11062 | gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR; | |
11063 | gnu_codes[N_Op_Eq] = EQ_EXPR; | |
11064 | gnu_codes[N_Op_Ne] = NE_EXPR; | |
11065 | gnu_codes[N_Op_Lt] = LT_EXPR; | |
11066 | gnu_codes[N_Op_Le] = LE_EXPR; | |
11067 | gnu_codes[N_Op_Gt] = GT_EXPR; | |
11068 | gnu_codes[N_Op_Ge] = GE_EXPR; | |
11069 | gnu_codes[N_Op_Add] = PLUS_EXPR; | |
11070 | gnu_codes[N_Op_Subtract] = MINUS_EXPR; | |
11071 | gnu_codes[N_Op_Multiply] = MULT_EXPR; | |
11072 | gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR; | |
11073 | gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR; | |
11074 | gnu_codes[N_Op_Minus] = NEGATE_EXPR; | |
11075 | gnu_codes[N_Op_Abs] = ABS_EXPR; | |
11076 | gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR; | |
11077 | gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR; | |
11078 | gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR; | |
11079 | gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR; | |
11080 | gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR; | |
11081 | gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR; | |
9a1bdc31 EB |
11082 | gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR; |
11083 | gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR; | |
a1ab4c31 AC |
11084 | } |
11085 | ||
a1ab4c31 | 11086 | #include "gt-ada-trans.h" |