]>
Commit | Line | Data |
---|---|---|
a1ab4c31 AC |
1 | /**************************************************************************** |
2 | * * | |
3 | * GNAT COMPILER COMPONENTS * | |
4 | * * | |
5 | * D E C L * | |
6 | * * | |
7 | * C Implementation File * | |
8 | * * | |
8d0d46f4 | 9 | * Copyright (C) 1992-2021, 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- * | |
13 | * ware Foundation; either version 3, or (at your option) any later ver- * | |
14 | * sion. GNAT is distributed in the hope that it will be useful, but WITH- * | |
15 | * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * | |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * | |
17 | * for more details. You should have received a copy of the GNU General * | |
18 | * Public License along with GCC; see the file COPYING3. If not see * | |
19 | * <http://www.gnu.org/licenses/>. * | |
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 | 29 | #include "target.h" |
a1ab4c31 | 30 | #include "tree.h" |
fad54055 | 31 | #include "gimple-expr.h" |
d8a2d370 | 32 | #include "stringpool.h" |
2adfab87 AM |
33 | #include "diagnostic-core.h" |
34 | #include "alias.h" | |
35 | #include "fold-const.h" | |
d8a2d370 | 36 | #include "stor-layout.h" |
f82a627c | 37 | #include "tree-inline.h" |
59909673 | 38 | #include "demangle.h" |
a1ab4c31 AC |
39 | |
40 | #include "ada.h" | |
41 | #include "types.h" | |
42 | #include "atree.h" | |
43 | #include "elists.h" | |
44 | #include "namet.h" | |
45 | #include "nlists.h" | |
46 | #include "repinfo.h" | |
47 | #include "snames.h" | |
a1ab4c31 | 48 | #include "uintp.h" |
2971780e | 49 | #include "urealp.h" |
a1ab4c31 AC |
50 | #include "fe.h" |
51 | #include "sinfo.h" | |
52 | #include "einfo.h" | |
a1ab4c31 AC |
53 | #include "ada-tree.h" |
54 | #include "gigi.h" | |
55 | ||
69720717 EB |
56 | /* The "stdcall" convention is really supported on 32-bit x86/Windows only. |
57 | The following macro is a helper to avoid having to check for a Windows | |
58 | specific attribute throughout this unit. */ | |
a1ab4c31 AC |
59 | |
60 | #if TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
c6eecbd8 PO |
61 | #ifdef TARGET_64BIT |
62 | #define Has_Stdcall_Convention(E) \ | |
63 | (!TARGET_64BIT && Convention (E) == Convention_Stdcall) | |
64 | #else | |
a1ab4c31 | 65 | #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall) |
c6eecbd8 | 66 | #endif |
a1ab4c31 | 67 | #else |
c6eecbd8 | 68 | #define Has_Stdcall_Convention(E) 0 |
a1ab4c31 AC |
69 | #endif |
70 | ||
93582885 EB |
71 | #define STDCALL_PREFIX "_imp__" |
72 | ||
66194a98 OH |
73 | /* Stack realignment is necessary for functions with foreign conventions when |
74 | the ABI doesn't mandate as much as what the compiler assumes - that is, up | |
75 | to PREFERRED_STACK_BOUNDARY. | |
76 | ||
77 | Such realignment can be requested with a dedicated function type attribute | |
78 | on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to | |
79 | characterize the situations where the attribute should be set. We rely on | |
80 | compiler configuration settings for 'main' to decide. */ | |
81 | ||
82 | #ifdef MAIN_STACK_BOUNDARY | |
83 | #define FOREIGN_FORCE_REALIGN_STACK \ | |
84 | (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY) | |
85 | #else | |
86 | #define FOREIGN_FORCE_REALIGN_STACK 0 | |
a1ab4c31 AC |
87 | #endif |
88 | ||
683ccd05 EB |
89 | /* The largest TYPE_ARRAY_MAX_SIZE value we set on an array type. |
90 | It's an artibrary limit (256 MB) above which we consider that | |
91 | the allocation is essentially unbounded. */ | |
92 | ||
93 | #define TYPE_ARRAY_SIZE_LIMIT (1 << 28) | |
94 | ||
a1ab4c31 AC |
95 | struct incomplete |
96 | { | |
97 | struct incomplete *next; | |
98 | tree old_type; | |
99 | Entity_Id full_type; | |
100 | }; | |
101 | ||
102 | /* These variables are used to defer recursively expanding incomplete types | |
1e55d29a | 103 | while we are processing a record, an array or a subprogram type. */ |
a1ab4c31 AC |
104 | static int defer_incomplete_level = 0; |
105 | static struct incomplete *defer_incomplete_list; | |
106 | ||
d3271136 EB |
107 | /* This variable is used to delay expanding types coming from a limited with |
108 | clause and completed Taft Amendment types until the end of the spec. */ | |
1e55d29a | 109 | static struct incomplete *defer_limited_with_list; |
a1ab4c31 | 110 | |
1aa67003 | 111 | typedef struct subst_pair_d { |
e3554601 NF |
112 | tree discriminant; |
113 | tree replacement; | |
114 | } subst_pair; | |
115 | ||
e3554601 | 116 | |
1aa67003 | 117 | typedef struct variant_desc_d { |
fb7fb701 NF |
118 | /* The type of the variant. */ |
119 | tree type; | |
120 | ||
121 | /* The associated field. */ | |
122 | tree field; | |
123 | ||
124 | /* The value of the qualifier. */ | |
125 | tree qual; | |
126 | ||
82ea8185 EB |
127 | /* The type of the variant after transformation. */ |
128 | tree new_type; | |
cd8ad459 EB |
129 | |
130 | /* The auxiliary data. */ | |
131 | tree aux; | |
fb7fb701 NF |
132 | } variant_desc; |
133 | ||
fb7fb701 | 134 | |
1e55d29a | 135 | /* A map used to cache the result of annotate_value. */ |
6c907cff | 136 | struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map> |
d242408f TS |
137 | { |
138 | static inline hashval_t | |
139 | hash (tree_int_map *m) | |
140 | { | |
141 | return htab_hash_pointer (m->base.from); | |
142 | } | |
143 | ||
144 | static inline bool | |
145 | equal (tree_int_map *a, tree_int_map *b) | |
146 | { | |
147 | return a->base.from == b->base.from; | |
148 | } | |
149 | ||
08ec2754 RS |
150 | static int |
151 | keep_cache_entry (tree_int_map *&m) | |
d242408f | 152 | { |
08ec2754 | 153 | return ggc_marked_p (m->base.from); |
d242408f TS |
154 | } |
155 | }; | |
156 | ||
157 | static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache; | |
a1ab4c31 | 158 | |
1e55d29a EB |
159 | /* A map used to associate a dummy type with a list of subprogram entities. */ |
160 | struct GTY((for_user)) tree_entity_vec_map | |
161 | { | |
162 | struct tree_map_base base; | |
163 | vec<Entity_Id, va_gc_atomic> *to; | |
164 | }; | |
165 | ||
166 | void | |
167 | gt_pch_nx (Entity_Id &) | |
168 | { | |
169 | } | |
170 | ||
171 | void | |
172 | gt_pch_nx (Entity_Id *x, gt_pointer_operator op, void *cookie) | |
173 | { | |
174 | op (x, cookie); | |
175 | } | |
176 | ||
177 | struct dummy_type_hasher : ggc_cache_ptr_hash<tree_entity_vec_map> | |
178 | { | |
179 | static inline hashval_t | |
180 | hash (tree_entity_vec_map *m) | |
181 | { | |
182 | return htab_hash_pointer (m->base.from); | |
183 | } | |
184 | ||
185 | static inline bool | |
186 | equal (tree_entity_vec_map *a, tree_entity_vec_map *b) | |
187 | { | |
188 | return a->base.from == b->base.from; | |
189 | } | |
190 | ||
191 | static int | |
192 | keep_cache_entry (tree_entity_vec_map *&m) | |
193 | { | |
194 | return ggc_marked_p (m->base.from); | |
195 | } | |
196 | }; | |
197 | ||
198 | static GTY ((cache)) hash_table<dummy_type_hasher> *dummy_to_subprog_map; | |
199 | ||
0567ae8d | 200 | static void prepend_one_attribute (struct attrib **, |
e0ef6912 | 201 | enum attrib_type, tree, tree, Node_Id); |
0567ae8d AC |
202 | static void prepend_one_attribute_pragma (struct attrib **, Node_Id); |
203 | static void prepend_attributes (struct attrib **, Entity_Id); | |
bf44701f EB |
204 | static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool, |
205 | bool); | |
bf44701f EB |
206 | static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool); |
207 | static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool, | |
da01bfee | 208 | unsigned int); |
fc7a823e | 209 | static tree elaborate_reference (tree, Entity_Id, bool, tree *); |
2cac6017 | 210 | static tree gnat_to_gnu_component_type (Entity_Id, bool, bool); |
1e55d29a | 211 | static tree gnat_to_gnu_subprog_type (Entity_Id, bool, bool, tree *); |
04bc3c93 | 212 | static int adjust_packed (tree, tree, int); |
2cac6017 | 213 | static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool); |
13a6dfe3 | 214 | static enum inline_status_t inline_status_for_subprog (Entity_Id); |
7414a3c3 | 215 | static tree gnu_ext_name_for_subprog (Entity_Id, tree); |
d42b7559 EB |
216 | static void set_nonaliased_component_on_array_type (tree); |
217 | static void set_reverse_storage_order_on_array_type (tree); | |
a1ab4c31 | 218 | static bool same_discriminant_p (Entity_Id, Entity_Id); |
d8e94f79 | 219 | static bool array_type_has_nonaliased_component (tree, Entity_Id); |
229077b0 | 220 | static bool compile_time_known_address_p (Node_Id); |
3ccd5d71 EB |
221 | static bool flb_cannot_be_superflat (Node_Id); |
222 | static bool range_cannot_be_superflat (Node_Id); | |
cb3d597d | 223 | static bool constructor_address_p (tree); |
fc7a823e EB |
224 | static bool allocatable_size_p (tree, bool); |
225 | static bool initial_value_needs_conversion (tree, tree); | |
683ccd05 | 226 | static tree update_n_elem (tree, tree, tree); |
44e9e3ec | 227 | static int compare_field_bitpos (const PTR, const PTR); |
8ab31c0c AC |
228 | static bool components_to_record (Node_Id, Entity_Id, tree, tree, int, bool, |
229 | bool, bool, bool, bool, bool, bool, tree, | |
230 | tree *); | |
a1ab4c31 AC |
231 | static Uint annotate_value (tree); |
232 | static void annotate_rep (Entity_Id, tree); | |
95c1c4bb | 233 | static tree build_position_list (tree, bool, tree, tree, unsigned int, tree); |
9771b263 | 234 | static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool); |
036c83b6 | 235 | static vec<variant_desc> build_variant_list (tree, Node_Id, vec<subst_pair>, |
05dbb83f | 236 | vec<variant_desc>); |
88795e14 | 237 | static tree maybe_saturate_size (tree, unsigned int align); |
a517d6c1 EB |
238 | static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool, |
239 | const char *, const char *); | |
a1ab4c31 | 240 | static void set_rm_size (Uint, tree, Entity_Id); |
a1ab4c31 | 241 | static unsigned int validate_alignment (Uint, Entity_Id, unsigned int); |
5ea133c6 | 242 | static unsigned int promote_object_alignment (tree, tree, Entity_Id); |
86a8ba5b | 243 | static void check_ok_for_atomic_type (tree, Entity_Id, bool); |
a40970cf EB |
244 | static bool type_for_atomic_builtin_p (tree); |
245 | static tree resolve_atomic_builtin (enum built_in_function, tree); | |
e3554601 | 246 | static tree create_field_decl_from (tree, tree, tree, tree, tree, |
05dbb83f | 247 | vec<subst_pair>); |
b1a785fb | 248 | static tree create_rep_part (tree, tree, tree); |
95c1c4bb | 249 | static tree get_rep_part (tree); |
05dbb83f AC |
250 | static tree create_variant_part_from (tree, vec<variant_desc>, tree, |
251 | tree, vec<subst_pair>, bool); | |
252 | static void copy_and_substitute_in_size (tree, tree, vec<subst_pair>); | |
253 | static void copy_and_substitute_in_layout (Entity_Id, Entity_Id, tree, tree, | |
254 | vec<subst_pair>, bool); | |
1e3cabd4 | 255 | static tree associate_original_type_to_packed_array (tree, Entity_Id); |
bf44701f | 256 | static const char *get_entity_char (Entity_Id); |
1515785d OH |
257 | |
258 | /* The relevant constituents of a subprogram binding to a GCC builtin. Used | |
308e6f3a | 259 | to pass around calls performing profile compatibility checks. */ |
1515785d OH |
260 | |
261 | typedef struct { | |
262 | Entity_Id gnat_entity; /* The Ada subprogram entity. */ | |
263 | tree ada_fntype; /* The corresponding GCC type node. */ | |
264 | tree btin_fntype; /* The GCC builtin function type node. */ | |
265 | } intrin_binding_t; | |
266 | ||
26864014 | 267 | static bool intrin_profiles_compatible_p (const intrin_binding_t *); |
ce2d0ce2 | 268 | |
a1ab4c31 | 269 | /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada |
1e17ef87 EB |
270 | entity, return the equivalent GCC tree for that entity (a ..._DECL node) |
271 | and associate the ..._DECL node with the input GNAT defining identifier. | |
a1ab4c31 AC |
272 | |
273 | If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its | |
1e17ef87 EB |
274 | initial value (in GCC tree form). This is optional for a variable. For |
275 | a renamed entity, GNU_EXPR gives the object being renamed. | |
a1ab4c31 | 276 | |
afc737f0 EB |
277 | DEFINITION is true if this call is intended for a definition. This is used |
278 | for separate compilation where it is necessary to know whether an external | |
279 | declaration or a definition must be created if the GCC equivalent was not | |
280 | created previously. */ | |
a1ab4c31 AC |
281 | |
282 | tree | |
afc737f0 | 283 | gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition) |
a1ab4c31 | 284 | { |
87668878 EB |
285 | /* The construct that declared the entity. */ |
286 | const Node_Id gnat_decl = Declaration_Node (gnat_entity); | |
af62ba41 EB |
287 | /* The object that the entity renames, if any. */ |
288 | const Entity_Id gnat_renamed_obj = Renamed_Object (gnat_entity); | |
87668878 | 289 | /* The kind of the entity. */ |
a8e05f92 EB |
290 | const Entity_Kind kind = Ekind (gnat_entity); |
291 | /* True if this is a type. */ | |
292 | const bool is_type = IN (kind, Type_Kind); | |
c1a569ef EB |
293 | /* True if this is an artificial entity. */ |
294 | const bool artificial_p = !Comes_From_Source (gnat_entity); | |
86060344 EB |
295 | /* True if debug info is requested for this entity. */ |
296 | const bool debug_info_p = Needs_Debug_Info (gnat_entity); | |
297 | /* True if this entity is to be considered as imported. */ | |
298 | const bool imported_p | |
299 | = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity))); | |
0d0cd281 EB |
300 | /* True if this entity has a foreign convention. */ |
301 | const bool foreign = Has_Foreign_Convention (gnat_entity); | |
a8e05f92 EB |
302 | /* For a type, contains the equivalent GNAT node to be used in gigi. */ |
303 | Entity_Id gnat_equiv_type = Empty; | |
f2bee239 EB |
304 | /* For a type, contains the GNAT node to be used for back-annotation. */ |
305 | Entity_Id gnat_annotate_type = Empty; | |
a8e05f92 | 306 | /* Temporary used to walk the GNAT tree. */ |
1e17ef87 | 307 | Entity_Id gnat_temp; |
1e17ef87 EB |
308 | /* Contains the GCC DECL node which is equivalent to the input GNAT node. |
309 | This node will be associated with the GNAT node by calling at the end | |
310 | of the `switch' statement. */ | |
a1ab4c31 | 311 | tree gnu_decl = NULL_TREE; |
1e17ef87 EB |
312 | /* Contains the GCC type to be used for the GCC node. */ |
313 | tree gnu_type = NULL_TREE; | |
314 | /* Contains the GCC size tree to be used for the GCC node. */ | |
315 | tree gnu_size = NULL_TREE; | |
316 | /* Contains the GCC name to be used for the GCC node. */ | |
0fb2335d | 317 | tree gnu_entity_name; |
7fddde95 EB |
318 | /* True if we have already saved gnu_decl as a GNAT association. This can |
319 | also be used to purposely avoid making such an association but this use | |
320 | case ought not to be applied to types because it can break the deferral | |
321 | mechanism implemented for access types. */ | |
a1ab4c31 | 322 | bool saved = false; |
1e17ef87 | 323 | /* True if we incremented defer_incomplete_level. */ |
a1ab4c31 | 324 | bool this_deferred = false; |
1e17ef87 | 325 | /* True if we incremented force_global. */ |
a1ab4c31 | 326 | bool this_global = false; |
1e17ef87 | 327 | /* True if we should check to see if elaborated during processing. */ |
a1ab4c31 | 328 | bool maybe_present = false; |
1e17ef87 | 329 | /* True if we made GNU_DECL and its type here. */ |
a1ab4c31 | 330 | bool this_made_decl = false; |
a8e05f92 EB |
331 | /* Size and alignment of the GCC node, if meaningful. */ |
332 | unsigned int esize = 0, align = 0; | |
333 | /* Contains the list of attributes directly attached to the entity. */ | |
1e17ef87 | 334 | struct attrib *attr_list = NULL; |
a1ab4c31 | 335 | |
af62ba41 | 336 | /* Since a use of an itype is a definition, process it as such if it is in |
fbb1c7d4 | 337 | the main unit, except for E_Access_Subtype because it's actually a use |
7fddde95 | 338 | of its base type, see below. */ |
1e17ef87 | 339 | if (!definition |
a8e05f92 | 340 | && is_type |
1e17ef87 | 341 | && Is_Itype (gnat_entity) |
7fddde95 | 342 | && Ekind (gnat_entity) != E_Access_Subtype |
a1ab4c31 AC |
343 | && !present_gnu_tree (gnat_entity) |
344 | && In_Extended_Main_Code_Unit (gnat_entity)) | |
345 | { | |
1e17ef87 EB |
346 | /* Ensure that we are in a subprogram mentioned in the Scope chain of |
347 | this entity, our current scope is global, or we encountered a task | |
348 | or entry (where we can't currently accurately check scoping). */ | |
a1ab4c31 AC |
349 | if (!current_function_decl |
350 | || DECL_ELABORATION_PROC_P (current_function_decl)) | |
351 | { | |
352 | process_type (gnat_entity); | |
353 | return get_gnu_tree (gnat_entity); | |
354 | } | |
355 | ||
356 | for (gnat_temp = Scope (gnat_entity); | |
1e17ef87 EB |
357 | Present (gnat_temp); |
358 | gnat_temp = Scope (gnat_temp)) | |
a1ab4c31 AC |
359 | { |
360 | if (Is_Type (gnat_temp)) | |
361 | gnat_temp = Underlying_Type (gnat_temp); | |
362 | ||
363 | if (Ekind (gnat_temp) == E_Subprogram_Body) | |
364 | gnat_temp | |
365 | = Corresponding_Spec (Parent (Declaration_Node (gnat_temp))); | |
366 | ||
7ed9919d | 367 | if (Is_Subprogram (gnat_temp) |
a1ab4c31 AC |
368 | && Present (Protected_Body_Subprogram (gnat_temp))) |
369 | gnat_temp = Protected_Body_Subprogram (gnat_temp); | |
370 | ||
371 | if (Ekind (gnat_temp) == E_Entry | |
372 | || Ekind (gnat_temp) == E_Entry_Family | |
373 | || Ekind (gnat_temp) == E_Task_Type | |
7ed9919d | 374 | || (Is_Subprogram (gnat_temp) |
a1ab4c31 AC |
375 | && present_gnu_tree (gnat_temp) |
376 | && (current_function_decl | |
afc737f0 | 377 | == gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)))) |
a1ab4c31 AC |
378 | { |
379 | process_type (gnat_entity); | |
380 | return get_gnu_tree (gnat_entity); | |
381 | } | |
382 | } | |
383 | ||
af62ba41 | 384 | /* This abort means the itype has an incorrect scope, i.e. that its |
7fddde95 | 385 | scope does not correspond to the subprogram it is first used in. */ |
a1ab4c31 AC |
386 | gcc_unreachable (); |
387 | } | |
388 | ||
a1ab4c31 AC |
389 | /* If we've already processed this entity, return what we got last time. |
390 | If we are defining the node, we should not have already processed it. | |
1e17ef87 EB |
391 | In that case, we will abort below when we try to save a new GCC tree |
392 | for this object. We also need to handle the case of getting a dummy | |
3fd7a66f | 393 | type when a Full_View exists but be careful so as not to trigger its |
7fddde95 EB |
394 | premature elaboration. Likewise for a cloned subtype without its own |
395 | freeze node, which typically happens when a generic gets instantiated | |
396 | on an incomplete or private type. */ | |
a8e05f92 EB |
397 | if ((!definition || (is_type && imported_p)) |
398 | && present_gnu_tree (gnat_entity)) | |
a1ab4c31 AC |
399 | { |
400 | gnu_decl = get_gnu_tree (gnat_entity); | |
401 | ||
402 | if (TREE_CODE (gnu_decl) == TYPE_DECL | |
403 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)) | |
404 | && IN (kind, Incomplete_Or_Private_Kind) | |
3fd7a66f EB |
405 | && Present (Full_View (gnat_entity)) |
406 | && (present_gnu_tree (Full_View (gnat_entity)) | |
407 | || No (Freeze_Node (Full_View (gnat_entity))))) | |
a1ab4c31 | 408 | { |
1e17ef87 | 409 | gnu_decl |
7fddde95 EB |
410 | = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, |
411 | false); | |
412 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
413 | save_gnu_tree (gnat_entity, gnu_decl, false); | |
414 | } | |
415 | ||
416 | if (TREE_CODE (gnu_decl) == TYPE_DECL | |
417 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)) | |
418 | && Ekind (gnat_entity) == E_Record_Subtype | |
419 | && No (Freeze_Node (gnat_entity)) | |
420 | && Present (Cloned_Subtype (gnat_entity)) | |
421 | && (present_gnu_tree (Cloned_Subtype (gnat_entity)) | |
422 | || No (Freeze_Node (Cloned_Subtype (gnat_entity))))) | |
423 | { | |
424 | gnu_decl | |
425 | = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity), NULL_TREE, | |
426 | false); | |
a1ab4c31 AC |
427 | save_gnu_tree (gnat_entity, NULL_TREE, false); |
428 | save_gnu_tree (gnat_entity, gnu_decl, false); | |
429 | } | |
430 | ||
431 | return gnu_decl; | |
432 | } | |
433 | ||
1f1b69e5 EB |
434 | /* If this is a numeric or enumeral type, or an access type, a nonzero Esize |
435 | must be specified unless it was specified by the programmer. Exceptions | |
436 | are for access-to-protected-subprogram types and all access subtypes, as | |
437 | another GNAT type is used to lay out the GCC type for them. */ | |
8d5a1b4f BD |
438 | gcc_assert (!is_type |
439 | || Known_Esize (gnat_entity) | |
a1ab4c31 | 440 | || Has_Size_Clause (gnat_entity) |
76f9c7f4 | 441 | || (!Is_In_Numeric_Kind (kind) |
1e17ef87 | 442 | && !IN (kind, Enumeration_Kind) |
a1ab4c31 AC |
443 | && (!IN (kind, Access_Kind) |
444 | || kind == E_Access_Protected_Subprogram_Type | |
445 | || kind == E_Anonymous_Access_Protected_Subprogram_Type | |
1f1b69e5 EB |
446 | || kind == E_Access_Subtype |
447 | || type_annotate_only))); | |
a1ab4c31 | 448 | |
b4680ca1 | 449 | /* The RM size must be specified for all discrete and fixed-point types. */ |
76f9c7f4 | 450 | gcc_assert (!(Is_In_Discrete_Or_Fixed_Point_Kind (kind) |
8de68eb3 | 451 | && !Known_RM_Size (gnat_entity))); |
a8e05f92 EB |
452 | |
453 | /* If we get here, it means we have not yet done anything with this entity. | |
454 | If we are not defining it, it must be a type or an entity that is defined | |
a5aac267 EB |
455 | elsewhere or externally, otherwise we should have defined it already. |
456 | ||
af62ba41 EB |
457 | In other words, the failure of this assertion typically arises when a |
458 | reference to an entity (type or object) is made before its declaration, | |
459 | either directly or by means of a freeze node which is incorrectly placed. | |
460 | This can also happen for an entity referenced out of context, for example | |
461 | a parameter outside of the subprogram where it is declared. GNAT_ENTITY | |
462 | is the N_Defining_Identifier of the entity, the problematic N_Identifier | |
463 | being the argument passed to Identifier_to_gnu in the parent frame. | |
464 | ||
a5aac267 EB |
465 | One exception is for an entity, typically an inherited operation, which is |
466 | a local alias for the parent's operation. It is neither defined, since it | |
467 | is an inherited operation, nor public, since it is declared in the current | |
468 | compilation unit, so we test Is_Public on the Alias entity instead. */ | |
a8e05f92 | 469 | gcc_assert (definition |
a8e05f92 EB |
470 | || is_type |
471 | || kind == E_Discriminant | |
472 | || kind == E_Component | |
473 | || kind == E_Label | |
474 | || (kind == E_Constant && Present (Full_View (gnat_entity))) | |
815b5368 | 475 | || Is_Public (gnat_entity) |
a5aac267 EB |
476 | || (Present (Alias (gnat_entity)) |
477 | && Is_Public (Alias (gnat_entity))) | |
815b5368 | 478 | || type_annotate_only); |
a1ab4c31 AC |
479 | |
480 | /* Get the name of the entity and set up the line number and filename of | |
56b8aa0c EB |
481 | the original definition for use in any decl we make. Make sure we do |
482 | not inherit another source location. */ | |
0fb2335d | 483 | gnu_entity_name = get_entity_name (gnat_entity); |
56b8aa0c | 484 | if (!renaming_from_instantiation_p (gnat_entity)) |
e8fa3dcd | 485 | Sloc_to_locus (Sloc (gnat_entity), &input_location); |
a1ab4c31 | 486 | |
a1ab4c31 | 487 | /* For cases when we are not defining (i.e., we are referencing from |
1e17ef87 | 488 | another compilation unit) public entities, show we are at global level |
a1ab4c31 AC |
489 | for the purpose of computing scopes. Don't do this for components or |
490 | discriminants since the relevant test is whether or not the record is | |
9083aacd | 491 | being defined. */ |
a962b0a1 | 492 | if (!definition |
a962b0a1 | 493 | && kind != E_Component |
a8e05f92 EB |
494 | && kind != E_Discriminant |
495 | && Is_Public (gnat_entity) | |
496 | && !Is_Statically_Allocated (gnat_entity)) | |
a1ab4c31 AC |
497 | force_global++, this_global = true; |
498 | ||
499 | /* Handle any attributes directly attached to the entity. */ | |
500 | if (Has_Gigi_Rep_Item (gnat_entity)) | |
0567ae8d | 501 | prepend_attributes (&attr_list, gnat_entity); |
a1ab4c31 | 502 | |
a8e05f92 EB |
503 | /* Do some common processing for types. */ |
504 | if (is_type) | |
505 | { | |
506 | /* Compute the equivalent type to be used in gigi. */ | |
507 | gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity); | |
508 | ||
509 | /* Machine_Attributes on types are expected to be propagated to | |
510 | subtypes. The corresponding Gigi_Rep_Items are only attached | |
511 | to the first subtype though, so we handle the propagation here. */ | |
512 | if (Base_Type (gnat_entity) != gnat_entity | |
513 | && !Is_First_Subtype (gnat_entity) | |
514 | && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity)))) | |
0567ae8d AC |
515 | prepend_attributes (&attr_list, |
516 | First_Subtype (Base_Type (gnat_entity))); | |
a8e05f92 | 517 | |
9cbad0a3 EB |
518 | /* Compute a default value for the size of an elementary type. */ |
519 | if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity)) | |
a8e05f92 EB |
520 | { |
521 | unsigned int max_esize; | |
9cbad0a3 EB |
522 | |
523 | gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity))); | |
a8e05f92 EB |
524 | esize = UI_To_Int (Esize (gnat_entity)); |
525 | ||
526 | if (IN (kind, Float_Kind)) | |
527 | max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE); | |
528 | else if (IN (kind, Access_Kind)) | |
529 | max_esize = POINTER_SIZE * 2; | |
530 | else | |
f2d9f95e | 531 | max_esize = Enable_128bit_Types ? 128 : LONG_LONG_TYPE_SIZE; |
a8e05f92 | 532 | |
feec4372 EB |
533 | if (esize > max_esize) |
534 | esize = max_esize; | |
a8e05f92 | 535 | } |
a8e05f92 | 536 | } |
a1ab4c31 AC |
537 | |
538 | switch (kind) | |
539 | { | |
a1ab4c31 | 540 | case E_Component: |
59f5c969 | 541 | case E_Discriminant: |
a1ab4c31 | 542 | { |
2ddc34ba | 543 | /* The GNAT record where the component was defined. */ |
a1ab4c31 AC |
544 | Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity)); |
545 | ||
f10ff6cc AC |
546 | /* If the entity is a discriminant of an extended tagged type used to |
547 | rename a discriminant of the parent type, return the latter. */ | |
05dbb83f AC |
548 | if (kind == E_Discriminant |
549 | && Present (Corresponding_Discriminant (gnat_entity)) | |
550 | && Is_Tagged_Type (gnat_record)) | |
a1ab4c31 AC |
551 | { |
552 | gnu_decl | |
f10ff6cc | 553 | = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity), |
a1ab4c31 AC |
554 | gnu_expr, definition); |
555 | saved = true; | |
556 | break; | |
557 | } | |
558 | ||
f10ff6cc AC |
559 | /* If the entity is an inherited component (in the case of extended |
560 | tagged record types), just return the original entity, which must | |
561 | be a FIELD_DECL. Likewise for discriminants. If the entity is a | |
96783cae | 562 | non-stored discriminant (in the case of derived untagged record |
f10ff6cc | 563 | types), return the stored discriminant it renames. */ |
d5ebeb8c EB |
564 | if (Present (Original_Record_Component (gnat_entity)) |
565 | && Original_Record_Component (gnat_entity) != gnat_entity) | |
a1ab4c31 | 566 | { |
a1ab4c31 | 567 | gnu_decl |
f10ff6cc | 568 | = gnat_to_gnu_entity (Original_Record_Component (gnat_entity), |
a1ab4c31 | 569 | gnu_expr, definition); |
05dbb83f AC |
570 | /* GNU_DECL contains a PLACEHOLDER_EXPR for discriminants. */ |
571 | if (kind == E_Discriminant) | |
572 | saved = true; | |
a1ab4c31 AC |
573 | break; |
574 | } | |
575 | ||
a1ab4c31 AC |
576 | /* Otherwise, if we are not defining this and we have no GCC type |
577 | for the containing record, make one for it. Then we should | |
578 | have made our own equivalent. */ | |
d5ebeb8c | 579 | if (!definition && !present_gnu_tree (gnat_record)) |
a1ab4c31 AC |
580 | { |
581 | /* ??? If this is in a record whose scope is a protected | |
582 | type and we have an Original_Record_Component, use it. | |
583 | This is a workaround for major problems in protected type | |
584 | handling. */ | |
585 | Entity_Id Scop = Scope (Scope (gnat_entity)); | |
43a4dd82 | 586 | if (Is_Protected_Type (Underlying_Type (Scop)) |
a1ab4c31 AC |
587 | && Present (Original_Record_Component (gnat_entity))) |
588 | { | |
589 | gnu_decl | |
590 | = gnat_to_gnu_entity (Original_Record_Component | |
591 | (gnat_entity), | |
afc737f0 | 592 | gnu_expr, false); |
d5ebeb8c EB |
593 | } |
594 | else | |
595 | { | |
596 | gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, false); | |
597 | gnu_decl = get_gnu_tree (gnat_entity); | |
a1ab4c31 AC |
598 | } |
599 | ||
a1ab4c31 AC |
600 | saved = true; |
601 | break; | |
602 | } | |
603 | ||
d5ebeb8c EB |
604 | /* Here we have no GCC type and this is a reference rather than a |
605 | definition. This should never happen. Most likely the cause is | |
606 | reference before declaration in the GNAT tree for gnat_entity. */ | |
607 | gcc_unreachable (); | |
a1ab4c31 AC |
608 | } |
609 | ||
104099b8 EB |
610 | case E_Named_Integer: |
611 | case E_Named_Real: | |
612 | { | |
613 | tree gnu_ext_name = NULL_TREE; | |
614 | ||
615 | if (Is_Public (gnat_entity)) | |
616 | gnu_ext_name = create_concat_name (gnat_entity, NULL); | |
617 | ||
618 | /* All references are supposed to be folded in the front-end. */ | |
619 | gcc_assert (definition && gnu_expr); | |
620 | ||
621 | gnu_type = gnat_to_gnu_type (Etype (gnat_entity)); | |
622 | gnu_expr = convert (gnu_type, gnu_expr); | |
623 | ||
624 | /* Build a CONST_DECL for debugging purposes exclusively. */ | |
625 | gnu_decl | |
626 | = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type, | |
627 | gnu_expr, true, Is_Public (gnat_entity), | |
628 | false, false, false, artificial_p, | |
3553d8c2 | 629 | debug_info_p, NULL, gnat_entity); |
104099b8 EB |
630 | } |
631 | break; | |
632 | ||
5277688b EB |
633 | case E_Constant: |
634 | /* Ignore constant definitions already marked with the error node. See | |
635 | the N_Object_Declaration case of gnat_to_gnu for the rationale. */ | |
636 | if (definition | |
5277688b EB |
637 | && present_gnu_tree (gnat_entity) |
638 | && get_gnu_tree (gnat_entity) == error_mark_node) | |
639 | { | |
640 | maybe_present = true; | |
641 | break; | |
642 | } | |
643 | ||
644 | /* Ignore deferred constant definitions without address clause since | |
645 | they are processed fully in the front-end. If No_Initialization | |
646 | is set, this is not a deferred constant but a constant whose value | |
647 | is built manually. And constants that are renamings are handled | |
648 | like variables. */ | |
649 | if (definition | |
650 | && !gnu_expr | |
651 | && No (Address_Clause (gnat_entity)) | |
87668878 | 652 | && !No_Initialization (gnat_decl) |
af62ba41 | 653 | && No (gnat_renamed_obj)) |
5277688b EB |
654 | { |
655 | gnu_decl = error_mark_node; | |
656 | saved = true; | |
657 | break; | |
658 | } | |
659 | ||
660 | /* If this is a use of a deferred constant without address clause, | |
661 | get its full definition. */ | |
662 | if (!definition | |
663 | && No (Address_Clause (gnat_entity)) | |
664 | && Present (Full_View (gnat_entity))) | |
665 | { | |
666 | gnu_decl | |
afc737f0 | 667 | = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, false); |
5277688b EB |
668 | saved = true; |
669 | break; | |
670 | } | |
671 | ||
241125b2 EB |
672 | /* If we have a constant that we are not defining, get the expression it |
673 | was defined to represent. This is necessary to avoid generating dumb | |
1c91516a | 674 | elaboration code in simple cases, and we may throw it away later if it |
541bb35d EB |
675 | is not a constant. But do not do it for dispatch tables because they |
676 | are only referenced indirectly and we need to have a consistent view | |
677 | of the exported and of the imported declarations of the tables from | |
678 | external units for them to be properly merged in LTO mode. Moreover | |
1c91516a | 679 | simply do not retrieve the expression if it is an allocator because |
e812d4dd EB |
680 | the designated type might still be dummy at this point. Note that we |
681 | invoke gnat_to_gnu_external and not gnat_to_gnu because the expression | |
682 | may contain N_Expression_With_Actions nodes and thus declarations of | |
1c91516a EB |
683 | objects from other units that we need to discard. Note also that we |
684 | need to do it even if we are only annotating types, so as to be able | |
685 | to validate representation clauses using constants. */ | |
5277688b | 686 | if (!definition |
87668878 | 687 | && !No_Initialization (gnat_decl) |
541bb35d | 688 | && !Is_Dispatch_Table_Entity (gnat_entity) |
87668878 | 689 | && Present (gnat_temp = Expression (gnat_decl)) |
1c91516a EB |
690 | && Nkind (gnat_temp) != N_Allocator |
691 | && (Is_Elementary_Type (Etype (gnat_entity)) || !type_annotate_only)) | |
e812d4dd | 692 | gnu_expr = gnat_to_gnu_external (gnat_temp); |
5277688b | 693 | |
9c453de7 | 694 | /* ... fall through ... */ |
5277688b EB |
695 | |
696 | case E_Exception: | |
a1ab4c31 AC |
697 | case E_Loop_Parameter: |
698 | case E_Out_Parameter: | |
699 | case E_Variable: | |
a1ab4c31 | 700 | { |
9182f718 | 701 | const Entity_Id gnat_type = Etype (gnat_entity); |
ae56e442 TG |
702 | /* Always create a variable for volatile objects and variables seen |
703 | constant but with a Linker_Section pragma. */ | |
a1ab4c31 AC |
704 | bool const_flag |
705 | = ((kind == E_Constant || kind == E_Variable) | |
706 | && Is_True_Constant (gnat_entity) | |
ae56e442 TG |
707 | && !(kind == E_Variable |
708 | && Present (Linker_Section_Pragma (gnat_entity))) | |
22868cbf | 709 | && !Treat_As_Volatile (gnat_entity) |
87668878 EB |
710 | && (((Nkind (gnat_decl) == N_Object_Declaration) |
711 | && Present (Expression (gnat_decl))) | |
af62ba41 | 712 | || Present (gnat_renamed_obj) |
c679a915 | 713 | || imported_p)); |
a1ab4c31 | 714 | bool inner_const_flag = const_flag; |
2056c5ed EB |
715 | bool static_flag = Is_Statically_Allocated (gnat_entity); |
716 | /* We implement RM 13.3(19) for exported and imported (non-constant) | |
717 | objects by making them volatile. */ | |
718 | bool volatile_flag | |
719 | = (Treat_As_Volatile (gnat_entity) | |
720 | || (!const_flag && (Is_Exported (gnat_entity) || imported_p))); | |
a1ab4c31 | 721 | bool mutable_p = false; |
86060344 | 722 | bool used_by_ref = false; |
a1ab4c31 | 723 | tree gnu_ext_name = NULL_TREE; |
87668878 | 724 | tree gnu_ada_size = NULL_TREE; |
a1ab4c31 | 725 | |
93e708f9 EB |
726 | /* We need to translate the renamed object even though we are only |
727 | referencing the renaming. But it may contain a call for which | |
728 | we'll generate a temporary to hold the return value and which | |
729 | is part of the definition of the renaming, so discard it. */ | |
af62ba41 | 730 | if (Present (gnat_renamed_obj) && !definition) |
a1ab4c31 AC |
731 | { |
732 | if (kind == E_Exception) | |
733 | gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity), | |
afc737f0 | 734 | NULL_TREE, false); |
a1ab4c31 | 735 | else |
af62ba41 | 736 | gnu_expr = gnat_to_gnu_external (gnat_renamed_obj); |
a1ab4c31 AC |
737 | } |
738 | ||
739 | /* Get the type after elaborating the renamed object. */ | |
0d0cd281 | 740 | if (foreign && Is_Descendant_Of_Address (Underlying_Type (gnat_type))) |
9182f718 EB |
741 | gnu_type = ptr_type_node; |
742 | else | |
17ba0ad5 | 743 | gnu_type = gnat_to_gnu_type (gnat_type); |
871fda0a | 744 | |
56345d11 | 745 | /* For a debug renaming declaration, build a debug-only entity. */ |
a1ab4c31 AC |
746 | if (Present (Debug_Renaming_Link (gnat_entity))) |
747 | { | |
56345d11 EB |
748 | /* Force a non-null value to make sure the symbol is retained. */ |
749 | tree value = build1 (INDIRECT_REF, gnu_type, | |
750 | build1 (NOP_EXPR, | |
751 | build_pointer_type (gnu_type), | |
752 | integer_minus_one_node)); | |
c172df28 AH |
753 | gnu_decl = build_decl (input_location, |
754 | VAR_DECL, gnu_entity_name, gnu_type); | |
56345d11 EB |
755 | SET_DECL_VALUE_EXPR (gnu_decl, value); |
756 | DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1; | |
bbe9a71d | 757 | TREE_STATIC (gnu_decl) = global_bindings_p (); |
a1ab4c31 AC |
758 | gnat_pushdecl (gnu_decl, gnat_entity); |
759 | break; | |
760 | } | |
761 | ||
762 | /* If this is a loop variable, its type should be the base type. | |
763 | This is because the code for processing a loop determines whether | |
764 | a normal loop end test can be done by comparing the bounds of the | |
765 | loop against those of the base type, which is presumed to be the | |
766 | size used for computation. But this is not correct when the size | |
767 | of the subtype is smaller than the type. */ | |
768 | if (kind == E_Loop_Parameter) | |
769 | gnu_type = get_base_type (gnu_type); | |
770 | ||
86060344 EB |
771 | /* Reject non-renamed objects whose type is an unconstrained array or |
772 | any object whose type is a dummy type or void. */ | |
a1ab4c31 | 773 | if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE |
af62ba41 | 774 | && No (gnat_renamed_obj)) |
a1ab4c31 AC |
775 | || TYPE_IS_DUMMY_P (gnu_type) |
776 | || TREE_CODE (gnu_type) == VOID_TYPE) | |
777 | { | |
778 | gcc_assert (type_annotate_only); | |
779 | if (this_global) | |
780 | force_global--; | |
781 | return error_mark_node; | |
782 | } | |
783 | ||
aae8570a | 784 | /* If an alignment is specified, use it if valid. Note that exceptions |
4d39941e EB |
785 | are objects but don't have an alignment and there is also no point in |
786 | setting it for an address clause, since the final type of the object | |
787 | will be a reference type. */ | |
788 | if (Known_Alignment (gnat_entity) | |
789 | && kind != E_Exception | |
790 | && No (Address_Clause (gnat_entity))) | |
791 | align = validate_alignment (Alignment (gnat_entity), gnat_entity, | |
792 | TYPE_ALIGN (gnu_type)); | |
a1ab4c31 | 793 | |
4d39941e | 794 | /* Likewise, if a size is specified, use it if valid. */ |
0e5b9de3 | 795 | if (Known_Esize (gnat_entity)) |
4d39941e EB |
796 | gnu_size |
797 | = validate_size (Esize (gnat_entity), gnu_type, gnat_entity, | |
a517d6c1 EB |
798 | VAR_DECL, false, Has_Size_Clause (gnat_entity), |
799 | NULL, NULL); | |
a1ab4c31 AC |
800 | if (gnu_size) |
801 | { | |
802 | gnu_type | |
803 | = make_type_from_size (gnu_type, gnu_size, | |
804 | Has_Biased_Representation (gnat_entity)); | |
805 | ||
806 | if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)) | |
807 | gnu_size = NULL_TREE; | |
808 | } | |
809 | ||
810 | /* If this object has self-referential size, it must be a record with | |
86060344 EB |
811 | a default discriminant. We are supposed to allocate an object of |
812 | the maximum size in this case, unless it is a constant with an | |
a1ab4c31 AC |
813 | initializing expression, in which case we can get the size from |
814 | that. Note that the resulting size may still be a variable, so | |
815 | this may end up with an indirect allocation. */ | |
af62ba41 | 816 | if (No (gnat_renamed_obj) |
a1ab4c31 AC |
817 | && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))) |
818 | { | |
819 | if (gnu_expr && kind == E_Constant) | |
820 | { | |
87668878 EB |
821 | gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr)); |
822 | gnu_ada_size = TYPE_ADA_SIZE (TREE_TYPE (gnu_expr)); | |
823 | if (CONTAINS_PLACEHOLDER_P (gnu_size)) | |
a1ab4c31 AC |
824 | { |
825 | /* If the initializing expression is itself a constant, | |
826 | despite having a nominal type with self-referential | |
827 | size, we can get the size directly from it. */ | |
828 | if (TREE_CODE (gnu_expr) == COMPONENT_REF | |
a1ab4c31 AC |
829 | && TYPE_IS_PADDING_P |
830 | (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))) | |
831 | && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL | |
832 | && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0)) | |
833 | || DECL_READONLY_ONCE_ELAB | |
834 | (TREE_OPERAND (gnu_expr, 0)))) | |
87668878 EB |
835 | { |
836 | gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0)); | |
837 | gnu_ada_size = gnu_size; | |
838 | } | |
a1ab4c31 | 839 | else |
87668878 EB |
840 | { |
841 | gnu_size | |
842 | = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, | |
843 | gnu_expr); | |
844 | gnu_ada_size | |
845 | = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_ada_size, | |
846 | gnu_expr); | |
847 | } | |
a1ab4c31 | 848 | } |
a1ab4c31 AC |
849 | } |
850 | /* We may have no GNU_EXPR because No_Initialization is | |
851 | set even though there's an Expression. */ | |
852 | else if (kind == E_Constant | |
87668878 EB |
853 | && Nkind (gnat_decl) == N_Object_Declaration |
854 | && Present (Expression (gnat_decl))) | |
855 | { | |
856 | tree gnu_expr_type | |
857 | = gnat_to_gnu_type (Etype (Expression (gnat_decl))); | |
858 | gnu_size = TYPE_SIZE (gnu_expr_type); | |
859 | gnu_ada_size = TYPE_ADA_SIZE (gnu_expr_type); | |
860 | } | |
a1ab4c31 AC |
861 | else |
862 | { | |
863 | gnu_size = max_size (TYPE_SIZE (gnu_type), true); | |
87668878 EB |
864 | /* We can be called on unconstrained arrays in this mode. */ |
865 | if (!type_annotate_only) | |
866 | gnu_ada_size = max_size (TYPE_ADA_SIZE (gnu_type), true); | |
a1ab4c31 AC |
867 | mutable_p = true; |
868 | } | |
1d5bfe97 | 869 | |
b0ad2d78 | 870 | /* If the size isn't constant and we are at global level, call |
1d5bfe97 EB |
871 | elaborate_expression_1 to make a variable for it rather than |
872 | calculating it each time. */ | |
b0ad2d78 | 873 | if (!TREE_CONSTANT (gnu_size) && global_bindings_p ()) |
1d5bfe97 | 874 | gnu_size = elaborate_expression_1 (gnu_size, gnat_entity, |
bf44701f | 875 | "SIZE", definition, false); |
a1ab4c31 AC |
876 | } |
877 | ||
86060344 EB |
878 | /* If the size is zero byte, make it one byte since some linkers have |
879 | troubles with zero-sized objects. If the object will have a | |
a1ab4c31 AC |
880 | template, that will make it nonzero so don't bother. Also avoid |
881 | doing that for an object renaming or an object with an address | |
882 | clause, as we would lose useful information on the view size | |
883 | (e.g. for null array slices) and we are not allocating the object | |
884 | here anyway. */ | |
885 | if (((gnu_size | |
886 | && integer_zerop (gnu_size) | |
887 | && !TREE_OVERFLOW (gnu_size)) | |
888 | || (TYPE_SIZE (gnu_type) | |
889 | && integer_zerop (TYPE_SIZE (gnu_type)) | |
890 | && !TREE_OVERFLOW (TYPE_SIZE (gnu_type)))) | |
9182f718 | 891 | && !Is_Constr_Subt_For_UN_Aliased (gnat_type) |
af62ba41 | 892 | && No (gnat_renamed_obj) |
a8e05f92 | 893 | && No (Address_Clause (gnat_entity))) |
a1ab4c31 AC |
894 | gnu_size = bitsize_unit_node; |
895 | ||
896 | /* If this is an object with no specified size and alignment, and | |
b120ca61 | 897 | if either it is full access or we are not optimizing alignment for |
a1ab4c31 AC |
898 | space and it is composite and not an exception, an Out parameter |
899 | or a reference to another object, and the size of its type is a | |
900 | constant, set the alignment to the smallest one which is not | |
901 | smaller than the size, with an appropriate cap. */ | |
5ea133c6 EB |
902 | if (!Known_Esize (gnat_entity) |
903 | && !Known_Alignment (gnat_entity) | |
b120ca61 | 904 | && (Is_Full_Access (gnat_entity) |
a1ab4c31 AC |
905 | || (!Optimize_Alignment_Space (gnat_entity) |
906 | && kind != E_Exception | |
907 | && kind != E_Out_Parameter | |
9182f718 EB |
908 | && Is_Composite_Type (gnat_type) |
909 | && !Is_Constr_Subt_For_UN_Aliased (gnat_type) | |
c679a915 | 910 | && !Is_Exported (gnat_entity) |
a1ab4c31 | 911 | && !imported_p |
af62ba41 | 912 | && No (gnat_renamed_obj) |
a1ab4c31 | 913 | && No (Address_Clause (gnat_entity)))) |
5ea133c6 EB |
914 | && (TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST || gnu_size)) |
915 | align = promote_object_alignment (gnu_type, gnu_size, gnat_entity); | |
a1ab4c31 AC |
916 | |
917 | /* If the object is set to have atomic components, find the component | |
918 | type and validate it. | |
919 | ||
920 | ??? Note that we ignore Has_Volatile_Components on objects; it's | |
2ddc34ba | 921 | not at all clear what to do in that case. */ |
a1ab4c31 AC |
922 | if (Has_Atomic_Components (gnat_entity)) |
923 | { | |
924 | tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE | |
925 | ? TREE_TYPE (gnu_type) : gnu_type); | |
926 | ||
927 | while (TREE_CODE (gnu_inner) == ARRAY_TYPE | |
928 | && TYPE_MULTI_ARRAY_P (gnu_inner)) | |
929 | gnu_inner = TREE_TYPE (gnu_inner); | |
930 | ||
86a8ba5b | 931 | check_ok_for_atomic_type (gnu_inner, gnat_entity, true); |
a1ab4c31 AC |
932 | } |
933 | ||
73a1a803 EB |
934 | /* If this is an aliased object with an unconstrained array nominal |
935 | subtype, make a type that includes the template. We will either | |
936 | allocate or create a variable of that type, see below. */ | |
9182f718 EB |
937 | if (Is_Constr_Subt_For_UN_Aliased (gnat_type) |
938 | && Is_Array_Type (Underlying_Type (gnat_type)) | |
a1ab4c31 | 939 | && !type_annotate_only) |
4184ef1b | 940 | { |
9182f718 | 941 | tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type)); |
4184ef1b | 942 | gnu_type |
6b318bf2 EB |
943 | = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array), |
944 | gnu_type, | |
4184ef1b EB |
945 | concat_name (gnu_entity_name, |
946 | "UNC"), | |
947 | debug_info_p); | |
948 | } | |
a1ab4c31 | 949 | |
b42ff0a5 EB |
950 | /* ??? If this is an object of CW type initialized to a value, try to |
951 | ensure that the object is sufficient aligned for this value, but | |
952 | without pessimizing the allocation. This is a kludge necessary | |
953 | because we don't support dynamic alignment. */ | |
954 | if (align == 0 | |
9182f718 | 955 | && Ekind (gnat_type) == E_Class_Wide_Subtype |
af62ba41 | 956 | && No (gnat_renamed_obj) |
b42ff0a5 EB |
957 | && No (Address_Clause (gnat_entity))) |
958 | align = get_target_system_allocator_alignment () * BITS_PER_UNIT; | |
959 | ||
a1ab4c31 AC |
960 | #ifdef MINIMUM_ATOMIC_ALIGNMENT |
961 | /* If the size is a constant and no alignment is specified, force | |
962 | the alignment to be the minimum valid atomic alignment. The | |
963 | restriction on constant size avoids problems with variable-size | |
964 | temporaries; if the size is variable, there's no issue with | |
965 | atomic access. Also don't do this for a constant, since it isn't | |
966 | necessary and can interfere with constant replacement. Finally, | |
967 | do not do it for Out parameters since that creates an | |
968 | size inconsistency with In parameters. */ | |
b42ff0a5 EB |
969 | if (align == 0 |
970 | && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type) | |
a1ab4c31 | 971 | && !FLOAT_TYPE_P (gnu_type) |
af62ba41 | 972 | && !const_flag && No (gnat_renamed_obj) |
a1ab4c31 AC |
973 | && !imported_p && No (Address_Clause (gnat_entity)) |
974 | && kind != E_Out_Parameter | |
975 | && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST | |
976 | : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)) | |
977 | align = MINIMUM_ATOMIC_ALIGNMENT; | |
978 | #endif | |
979 | ||
e3449598 EB |
980 | /* Do not take into account aliased adjustments or alignment promotions |
981 | to compute the size of the object. */ | |
87668878 | 982 | tree gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type); |
e3449598 EB |
983 | |
984 | /* If the object is aliased, of a constrained nominal subtype and its | |
985 | size might be zero at run time, we force at least the unit size. */ | |
986 | if (Is_Aliased (gnat_entity) | |
987 | && !Is_Constr_Subt_For_UN_Aliased (gnat_type) | |
988 | && Is_Array_Type (Underlying_Type (gnat_type)) | |
989 | && !TREE_CONSTANT (gnu_object_size)) | |
990 | gnu_size = size_binop (MAX_EXPR, gnu_object_size, bitsize_unit_node); | |
991 | ||
992 | /* Make a new type with the desired size and alignment, if needed. */ | |
a1ab4c31 | 993 | if (gnu_size || align > 0) |
51c7954d EB |
994 | { |
995 | tree orig_type = gnu_type; | |
996 | ||
997 | gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, | |
1e3cabd4 | 998 | false, definition, true); |
51c7954d | 999 | |
87668878 EB |
1000 | /* If the nominal subtype of the object is unconstrained and its |
1001 | size is not fixed, compute the Ada size from the Ada size of | |
1002 | the subtype and/or the expression; this will make it possible | |
1003 | for gnat_type_max_size to easily compute a maximum size. */ | |
1004 | if (gnu_ada_size && gnu_size && !TREE_CONSTANT (gnu_size)) | |
1005 | SET_TYPE_ADA_SIZE (gnu_type, gnu_ada_size); | |
1006 | ||
51c7954d EB |
1007 | /* If a padding record was made, declare it now since it will |
1008 | never be declared otherwise. This is necessary to ensure | |
1009 | that its subtrees are properly marked. */ | |
1010 | if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type))) | |
74746d49 | 1011 | create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, |
51c7954d EB |
1012 | debug_info_p, gnat_entity); |
1013 | } | |
a1ab4c31 | 1014 | |
e590690e | 1015 | /* Now check if the type of the object allows atomic access. */ |
b120ca61 | 1016 | if (Is_Full_Access (gnat_entity)) |
e590690e EB |
1017 | check_ok_for_atomic_type (gnu_type, gnat_entity, false); |
1018 | ||
a1ab4c31 | 1019 | /* If this is a renaming, avoid as much as possible to create a new |
7194767c EB |
1020 | object. However, in some cases, creating it is required because |
1021 | renaming can be applied to objects that are not names in Ada. | |
1022 | This processing needs to be applied to the raw expression so as | |
1023 | to make it more likely to rename the underlying object. */ | |
af62ba41 | 1024 | if (Present (gnat_renamed_obj)) |
a1ab4c31 | 1025 | { |
fc7a823e EB |
1026 | /* If the renamed object had padding, strip off the reference to |
1027 | the inner object and reset our type. */ | |
a1ab4c31 | 1028 | if ((TREE_CODE (gnu_expr) == COMPONENT_REF |
a1ab4c31 AC |
1029 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))) |
1030 | /* Strip useless conversions around the object. */ | |
71196d4e | 1031 | || gnat_useless_type_conversion (gnu_expr)) |
a1ab4c31 AC |
1032 | { |
1033 | gnu_expr = TREE_OPERAND (gnu_expr, 0); | |
1034 | gnu_type = TREE_TYPE (gnu_expr); | |
1035 | } | |
1036 | ||
9422c886 EB |
1037 | /* Or else, if the renamed object has an unconstrained type with |
1038 | default discriminant, use the padded type. */ | |
fc7a823e | 1039 | else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr))) |
9422c886 EB |
1040 | gnu_type = TREE_TYPE (gnu_expr); |
1041 | ||
5bdd063b EB |
1042 | /* If this is a constant renaming stemming from a function call, |
1043 | treat it as a normal object whose initial value is what is being | |
1044 | renamed. RM 3.3 says that the result of evaluating a function | |
1045 | call is a constant object. Therefore, it can be the inner | |
1046 | object of a constant renaming and the renaming must be fully | |
1047 | instantiated, i.e. it cannot be a reference to (part of) an | |
1048 | existing object. And treat other rvalues the same way. */ | |
7194767c EB |
1049 | tree inner = gnu_expr; |
1050 | while (handled_component_p (inner) || CONVERT_EXPR_P (inner)) | |
1051 | inner = TREE_OPERAND (inner, 0); | |
1052 | /* Expand_Dispatching_Call can prepend a comparison of the tags | |
1053 | before the call to "=". */ | |
93e708f9 EB |
1054 | if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR |
1055 | || TREE_CODE (inner) == COMPOUND_EXPR) | |
7194767c | 1056 | inner = TREE_OPERAND (inner, 1); |
241125b2 EB |
1057 | if ((TREE_CODE (inner) == CALL_EXPR |
1058 | && !call_is_atomic_load (inner)) | |
241125b2 | 1059 | || TREE_CODE (inner) == CONSTRUCTOR |
93e708f9 | 1060 | || CONSTANT_CLASS_P (inner) |
03b4b15e EB |
1061 | || COMPARISON_CLASS_P (inner) |
1062 | || BINARY_CLASS_P (inner) | |
1063 | || EXPRESSION_CLASS_P (inner) | |
93e708f9 EB |
1064 | /* We need to detect the case where a temporary is created to |
1065 | hold the return value, since we cannot safely rename it at | |
1066 | top level as it lives only in the elaboration routine. */ | |
1067 | || (TREE_CODE (inner) == VAR_DECL | |
1068 | && DECL_RETURN_VALUE_P (inner)) | |
1069 | /* We also need to detect the case where the front-end creates | |
1070 | a dangling 'reference to a function call at top level and | |
1071 | substitutes it in the renaming, for example: | |
1072 | ||
1073 | q__b : boolean renames r__f.e (1); | |
1074 | ||
1075 | can be rewritten into: | |
1076 | ||
1077 | q__R1s : constant q__A2s := r__f'reference; | |
1078 | [...] | |
1079 | q__b : boolean renames q__R1s.all.e (1); | |
1080 | ||
1081 | We cannot safely rename the rewritten expression since the | |
1082 | underlying object lives only in the elaboration routine. */ | |
1083 | || (TREE_CODE (inner) == INDIRECT_REF | |
1084 | && (inner | |
03b4b15e | 1085 | = remove_conversions (TREE_OPERAND (inner, 0), true)) |
93e708f9 EB |
1086 | && TREE_CODE (inner) == VAR_DECL |
1087 | && DECL_RETURN_VALUE_P (inner))) | |
7194767c | 1088 | ; |
a1ab4c31 | 1089 | |
5bdd063b EB |
1090 | /* Otherwise, this is an lvalue being renamed, so it needs to be |
1091 | elaborated as a reference and substituted for the entity. But | |
1092 | this means that we must evaluate the address of the renaming | |
1093 | in the definition case to instantiate the SAVE_EXPRs. */ | |
1094 | else | |
a1ab4c31 | 1095 | { |
5bdd063b | 1096 | tree gnu_init = NULL_TREE; |
fc7a823e | 1097 | |
5bdd063b EB |
1098 | if (type_annotate_only && TREE_CODE (gnu_expr) == ERROR_MARK) |
1099 | break; | |
fc7a823e | 1100 | |
5bdd063b EB |
1101 | gnu_expr |
1102 | = elaborate_reference (gnu_expr, gnat_entity, definition, | |
1103 | &gnu_init); | |
a1ab4c31 | 1104 | |
5bdd063b | 1105 | /* No DECL_EXPR might be created so the expression needs to be |
241125b2 | 1106 | marked manually because it will likely be shared. */ |
7194767c | 1107 | if (global_bindings_p ()) |
5bdd063b | 1108 | MARK_VISITED (gnu_expr); |
a1ab4c31 | 1109 | |
241125b2 EB |
1110 | /* This assertion will fail if the renamed object isn't aligned |
1111 | enough as to make it possible to honor the alignment set on | |
1112 | the renaming. */ | |
7194767c EB |
1113 | if (align) |
1114 | { | |
5bdd063b EB |
1115 | const unsigned int ralign |
1116 | = DECL_P (gnu_expr) | |
1117 | ? DECL_ALIGN (gnu_expr) | |
1118 | : TYPE_ALIGN (TREE_TYPE (gnu_expr)); | |
7194767c | 1119 | gcc_assert (ralign >= align); |
a1ab4c31 AC |
1120 | } |
1121 | ||
d5ebeb8c | 1122 | /* The expression might not be a DECL so save it manually. */ |
5bdd063b | 1123 | gnu_decl = gnu_expr; |
7194767c EB |
1124 | save_gnu_tree (gnat_entity, gnu_decl, true); |
1125 | saved = true; | |
1126 | annotate_object (gnat_entity, gnu_type, NULL_TREE, false); | |
a1ab4c31 | 1127 | |
5bdd063b EB |
1128 | /* If this is only a reference to the entity, we are done. */ |
1129 | if (!definition) | |
1130 | break; | |
fc7a823e | 1131 | |
5bdd063b EB |
1132 | /* Otherwise, emit the initialization statement, if any. */ |
1133 | if (gnu_init) | |
1134 | add_stmt (gnu_init); | |
a1ab4c31 | 1135 | |
5bdd063b EB |
1136 | /* If it needs to be materialized for debugging purposes, build |
1137 | the entity as indirect reference to the renamed object. */ | |
1138 | if (Materialize_Entity (gnat_entity)) | |
1139 | { | |
1140 | gnu_type = build_reference_type (gnu_type); | |
1141 | const_flag = true; | |
1142 | volatile_flag = false; | |
e297e2ea | 1143 | |
5bdd063b | 1144 | gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr); |
a1ab4c31 | 1145 | |
5bdd063b EB |
1146 | create_var_decl (gnu_entity_name, gnu_ext_name, |
1147 | TREE_TYPE (gnu_expr), gnu_expr, | |
1148 | const_flag, Is_Public (gnat_entity), | |
1149 | imported_p, static_flag, volatile_flag, | |
1150 | artificial_p, debug_info_p, attr_list, | |
1151 | gnat_entity, false); | |
fc7a823e | 1152 | } |
5bdd063b EB |
1153 | |
1154 | /* Otherwise, instantiate the SAVE_EXPRs if needed. */ | |
1155 | else if (TREE_SIDE_EFFECTS (gnu_expr)) | |
1156 | add_stmt (build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)); | |
1157 | ||
1158 | break; | |
a1ab4c31 AC |
1159 | } |
1160 | } | |
1161 | ||
9cf18af8 EB |
1162 | /* If we are defining an aliased object whose nominal subtype is |
1163 | unconstrained, the object is a record that contains both the | |
1164 | template and the object. If there is an initializer, it will | |
1165 | have already been converted to the right type, but we need to | |
1166 | create the template if there is no initializer. */ | |
1167 | if (definition | |
1168 | && !gnu_expr | |
1169 | && TREE_CODE (gnu_type) == RECORD_TYPE | |
1170 | && (TYPE_CONTAINS_TEMPLATE_P (gnu_type) | |
afb4afcd | 1171 | /* Beware that padding might have been introduced above. */ |
315cff15 | 1172 | || (TYPE_PADDING_P (gnu_type) |
9cf18af8 EB |
1173 | && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) |
1174 | == RECORD_TYPE | |
1175 | && TYPE_CONTAINS_TEMPLATE_P | |
1176 | (TREE_TYPE (TYPE_FIELDS (gnu_type)))))) | |
a1ab4c31 AC |
1177 | { |
1178 | tree template_field | |
315cff15 | 1179 | = TYPE_PADDING_P (gnu_type) |
a1ab4c31 AC |
1180 | ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type))) |
1181 | : TYPE_FIELDS (gnu_type); | |
9771b263 DN |
1182 | vec<constructor_elt, va_gc> *v; |
1183 | vec_alloc (v, 1); | |
0e228dd9 | 1184 | tree t = build_template (TREE_TYPE (template_field), |
910ad8de | 1185 | TREE_TYPE (DECL_CHAIN (template_field)), |
0e228dd9 NF |
1186 | NULL_TREE); |
1187 | CONSTRUCTOR_APPEND_ELT (v, template_field, t); | |
1188 | gnu_expr = gnat_build_constructor (gnu_type, v); | |
a1ab4c31 AC |
1189 | } |
1190 | ||
fc7a823e EB |
1191 | /* Convert the expression to the type of the object if need be. */ |
1192 | if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr)) | |
a1ab4c31 AC |
1193 | gnu_expr = convert (gnu_type, gnu_expr); |
1194 | ||
86060344 | 1195 | /* If this is a pointer that doesn't have an initializing expression, |
b3b5c6a2 EB |
1196 | initialize it to NULL, unless the object is declared imported as |
1197 | per RM B.1(24). */ | |
a1ab4c31 | 1198 | if (definition |
315cff15 | 1199 | && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type)) |
86060344 EB |
1200 | && !gnu_expr |
1201 | && !Is_Imported (gnat_entity)) | |
a1ab4c31 AC |
1202 | gnu_expr = integer_zero_node; |
1203 | ||
8df2e902 EB |
1204 | /* If we are defining the object and it has an Address clause, we must |
1205 | either get the address expression from the saved GCC tree for the | |
1206 | object if it has a Freeze node, or elaborate the address expression | |
1207 | here since the front-end has guaranteed that the elaboration has no | |
1208 | effects in this case. */ | |
a1ab4c31 AC |
1209 | if (definition && Present (Address_Clause (gnat_entity))) |
1210 | { | |
73a1a803 | 1211 | const Node_Id gnat_clause = Address_Clause (gnat_entity); |
3b9d1594 EB |
1212 | const Node_Id gnat_address = Expression (gnat_clause); |
1213 | tree gnu_address = present_gnu_tree (gnat_entity) | |
1214 | ? TREE_OPERAND (get_gnu_tree (gnat_entity), 0) | |
1215 | : gnat_to_gnu (gnat_address); | |
a1ab4c31 AC |
1216 | |
1217 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
1218 | ||
a1ab4c31 | 1219 | /* Convert the type of the object to a reference type that can |
b3b5c6a2 | 1220 | alias everything as per RM 13.3(19). */ |
2056c5ed EB |
1221 | if (volatile_flag && !TYPE_VOLATILE (gnu_type)) |
1222 | gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE); | |
a1ab4c31 AC |
1223 | gnu_type |
1224 | = build_reference_type_for_mode (gnu_type, ptr_mode, true); | |
1225 | gnu_address = convert (gnu_type, gnu_address); | |
1226 | used_by_ref = true; | |
86060344 | 1227 | const_flag |
2056c5ed | 1228 | = (!Is_Public (gnat_entity) |
1e55d29a | 1229 | || compile_time_known_address_p (gnat_address)); |
2056c5ed | 1230 | volatile_flag = false; |
241125b2 | 1231 | gnu_size = NULL_TREE; |
a1ab4c31 | 1232 | |
73a1a803 EB |
1233 | /* If this is an aliased object with an unconstrained array nominal |
1234 | subtype, then it can overlay only another aliased object with an | |
1235 | unconstrained array nominal subtype and compatible template. */ | |
9182f718 EB |
1236 | if (Is_Constr_Subt_For_UN_Aliased (gnat_type) |
1237 | && Is_Array_Type (Underlying_Type (gnat_type)) | |
73a1a803 EB |
1238 | && !type_annotate_only) |
1239 | { | |
1240 | tree rec_type = TREE_TYPE (gnu_type); | |
1241 | tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type))); | |
1242 | ||
1243 | /* This is the pattern built for a regular object. */ | |
1244 | if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR | |
1245 | && TREE_OPERAND (gnu_address, 1) == off) | |
1246 | gnu_address = TREE_OPERAND (gnu_address, 0); | |
4965be0b | 1247 | |
73a1a803 EB |
1248 | /* This is the pattern built for an overaligned object. */ |
1249 | else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR | |
1250 | && TREE_CODE (TREE_OPERAND (gnu_address, 1)) | |
1251 | == PLUS_EXPR | |
1252 | && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1) | |
1253 | == off) | |
1254 | gnu_address | |
1255 | = build2 (POINTER_PLUS_EXPR, gnu_type, | |
1256 | TREE_OPERAND (gnu_address, 0), | |
1257 | TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0)); | |
4965be0b EB |
1258 | |
1259 | /* We make an exception for an absolute address but we warn | |
1260 | that there is a descriptor at the start of the object. */ | |
1261 | else if (TREE_CODE (gnu_address) == INTEGER_CST) | |
1262 | { | |
1263 | post_error_ne ("??aliased object& with unconstrained " | |
1264 | "array nominal subtype", gnat_clause, | |
1265 | gnat_entity); | |
1266 | post_error ("\\starts with a descriptor whose size is " | |
1267 | "given by ''Descriptor_Size", gnat_clause); | |
1268 | } | |
1269 | ||
73a1a803 EB |
1270 | else |
1271 | { | |
1272 | post_error_ne ("aliased object& with unconstrained array " | |
1273 | "nominal subtype", gnat_clause, | |
1274 | gnat_entity); | |
1275 | post_error ("\\can overlay only aliased object with " | |
1276 | "compatible subtype", gnat_clause); | |
1277 | } | |
1278 | } | |
1279 | ||
a1ab4c31 AC |
1280 | /* If we don't have an initializing expression for the underlying |
1281 | variable, the initializing expression for the pointer is the | |
1282 | specified address. Otherwise, we have to make a COMPOUND_EXPR | |
1283 | to assign both the address and the initial value. */ | |
1284 | if (!gnu_expr) | |
1285 | gnu_expr = gnu_address; | |
1286 | else | |
1287 | gnu_expr | |
1288 | = build2 (COMPOUND_EXPR, gnu_type, | |
73a1a803 EB |
1289 | build_binary_op (INIT_EXPR, NULL_TREE, |
1290 | build_unary_op (INDIRECT_REF, | |
1291 | NULL_TREE, | |
1292 | gnu_address), | |
1293 | gnu_expr), | |
a1ab4c31 AC |
1294 | gnu_address); |
1295 | } | |
1296 | ||
1297 | /* If it has an address clause and we are not defining it, mark it | |
1298 | as an indirect object. Likewise for Stdcall objects that are | |
1299 | imported. */ | |
1300 | if ((!definition && Present (Address_Clause (gnat_entity))) | |
b3b5c6a2 | 1301 | || (imported_p && Has_Stdcall_Convention (gnat_entity))) |
a1ab4c31 AC |
1302 | { |
1303 | /* Convert the type of the object to a reference type that can | |
b3b5c6a2 | 1304 | alias everything as per RM 13.3(19). */ |
2056c5ed EB |
1305 | if (volatile_flag && !TYPE_VOLATILE (gnu_type)) |
1306 | gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE); | |
a1ab4c31 AC |
1307 | gnu_type |
1308 | = build_reference_type_for_mode (gnu_type, ptr_mode, true); | |
241125b2 | 1309 | used_by_ref = true; |
2056c5ed EB |
1310 | const_flag = false; |
1311 | volatile_flag = false; | |
a1ab4c31 AC |
1312 | gnu_size = NULL_TREE; |
1313 | ||
1314 | /* No point in taking the address of an initializing expression | |
1315 | that isn't going to be used. */ | |
1316 | gnu_expr = NULL_TREE; | |
1317 | ||
1318 | /* If it has an address clause whose value is known at compile | |
1319 | time, make the object a CONST_DECL. This will avoid a | |
1320 | useless dereference. */ | |
1321 | if (Present (Address_Clause (gnat_entity))) | |
1322 | { | |
1323 | Node_Id gnat_address | |
1324 | = Expression (Address_Clause (gnat_entity)); | |
1325 | ||
1326 | if (compile_time_known_address_p (gnat_address)) | |
1327 | { | |
1328 | gnu_expr = gnat_to_gnu (gnat_address); | |
1329 | const_flag = true; | |
1330 | } | |
1331 | } | |
a1ab4c31 AC |
1332 | } |
1333 | ||
1334 | /* If we are at top level and this object is of variable size, | |
1335 | make the actual type a hidden pointer to the real type and | |
1336 | make the initializer be a memory allocation and initialization. | |
1337 | Likewise for objects we aren't defining (presumed to be | |
1338 | external references from other packages), but there we do | |
1339 | not set up an initialization. | |
1340 | ||
1341 | If the object's size overflows, make an allocator too, so that | |
1342 | Storage_Error gets raised. Note that we will never free | |
1343 | such memory, so we presume it never will get allocated. */ | |
a1ab4c31 | 1344 | if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type), |
86060344 EB |
1345 | global_bindings_p () |
1346 | || !definition | |
2056c5ed | 1347 | || static_flag) |
f54ee980 EB |
1348 | || (gnu_size |
1349 | && !allocatable_size_p (convert (sizetype, | |
1350 | size_binop | |
e5bfda02 | 1351 | (EXACT_DIV_EXPR, gnu_size, |
f54ee980 EB |
1352 | bitsize_unit_node)), |
1353 | global_bindings_p () | |
1354 | || !definition | |
2056c5ed | 1355 | || static_flag))) |
a1ab4c31 | 1356 | { |
2056c5ed EB |
1357 | if (volatile_flag && !TYPE_VOLATILE (gnu_type)) |
1358 | gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE); | |
a1ab4c31 | 1359 | gnu_type = build_reference_type (gnu_type); |
a1ab4c31 | 1360 | used_by_ref = true; |
241125b2 | 1361 | const_flag = true; |
2056c5ed | 1362 | volatile_flag = false; |
241125b2 | 1363 | gnu_size = NULL_TREE; |
a1ab4c31 AC |
1364 | |
1365 | /* In case this was a aliased object whose nominal subtype is | |
1366 | unconstrained, the pointer above will be a thin pointer and | |
1367 | build_allocator will automatically make the template. | |
1368 | ||
1369 | If we have a template initializer only (that we made above), | |
1370 | pretend there is none and rely on what build_allocator creates | |
1371 | again anyway. Otherwise (if we have a full initializer), get | |
1372 | the data part and feed that to build_allocator. | |
1373 | ||
1374 | If we are elaborating a mutable object, tell build_allocator to | |
1375 | ignore a possibly simpler size from the initializer, if any, as | |
1376 | we must allocate the maximum possible size in this case. */ | |
f25496f3 | 1377 | if (definition && !imported_p) |
a1ab4c31 AC |
1378 | { |
1379 | tree gnu_alloc_type = TREE_TYPE (gnu_type); | |
1380 | ||
1381 | if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE | |
1382 | && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type)) | |
1383 | { | |
1384 | gnu_alloc_type | |
910ad8de | 1385 | = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type))); |
a1ab4c31 AC |
1386 | |
1387 | if (TREE_CODE (gnu_expr) == CONSTRUCTOR | |
aaa1b10f | 1388 | && CONSTRUCTOR_NELTS (gnu_expr) == 1) |
2117b9bb | 1389 | gnu_expr = NULL_TREE; |
a1ab4c31 AC |
1390 | else |
1391 | gnu_expr | |
1392 | = build_component_ref | |
64235766 | 1393 | (gnu_expr, |
910ad8de | 1394 | DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))), |
a1ab4c31 AC |
1395 | false); |
1396 | } | |
1397 | ||
1398 | if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST | |
ce3da0d0 | 1399 | && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type))) |
4a29b8d6 | 1400 | post_error ("??`Storage_Error` will be raised at run time!", |
a1ab4c31 AC |
1401 | gnat_entity); |
1402 | ||
6f61bd41 EB |
1403 | gnu_expr |
1404 | = build_allocator (gnu_alloc_type, gnu_expr, gnu_type, | |
1405 | Empty, Empty, gnat_entity, mutable_p); | |
a1ab4c31 AC |
1406 | } |
1407 | else | |
241125b2 | 1408 | gnu_expr = NULL_TREE; |
a1ab4c31 AC |
1409 | } |
1410 | ||
1411 | /* If this object would go into the stack and has an alignment larger | |
1412 | than the largest stack alignment the back-end can honor, resort to | |
1413 | a variable of "aligning type". */ | |
73a1a803 | 1414 | if (definition |
b0ad2d78 | 1415 | && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT |
73a1a803 | 1416 | && !imported_p |
b0ad2d78 EB |
1417 | && !static_flag |
1418 | && !global_bindings_p ()) | |
a1ab4c31 AC |
1419 | { |
1420 | /* Create the new variable. No need for extra room before the | |
1421 | aligned field as this is in automatic storage. */ | |
1422 | tree gnu_new_type | |
1423 | = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type), | |
1424 | TYPE_SIZE_UNIT (gnu_type), | |
0746af5e | 1425 | BIGGEST_ALIGNMENT, 0, gnat_entity); |
a1ab4c31 AC |
1426 | tree gnu_new_var |
1427 | = create_var_decl (create_concat_name (gnat_entity, "ALIGN"), | |
2056c5ed EB |
1428 | NULL_TREE, gnu_new_type, NULL_TREE, |
1429 | false, false, false, false, false, | |
ff9baa5f PMR |
1430 | true, debug_info_p && definition, NULL, |
1431 | gnat_entity); | |
a1ab4c31 AC |
1432 | |
1433 | /* Initialize the aligned field if we have an initializer. */ | |
1434 | if (gnu_expr) | |
1435 | add_stmt_with_node | |
73a1a803 | 1436 | (build_binary_op (INIT_EXPR, NULL_TREE, |
a1ab4c31 | 1437 | build_component_ref |
64235766 EB |
1438 | (gnu_new_var, TYPE_FIELDS (gnu_new_type), |
1439 | false), | |
a1ab4c31 AC |
1440 | gnu_expr), |
1441 | gnat_entity); | |
1442 | ||
1443 | /* And setup this entity as a reference to the aligned field. */ | |
1444 | gnu_type = build_reference_type (gnu_type); | |
1445 | gnu_expr | |
1446 | = build_unary_op | |
73a1a803 | 1447 | (ADDR_EXPR, NULL_TREE, |
64235766 EB |
1448 | build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type), |
1449 | false)); | |
73a1a803 | 1450 | TREE_CONSTANT (gnu_expr) = 1; |
a1ab4c31 | 1451 | |
a1ab4c31 AC |
1452 | used_by_ref = true; |
1453 | const_flag = true; | |
2056c5ed | 1454 | volatile_flag = false; |
241125b2 | 1455 | gnu_size = NULL_TREE; |
a1ab4c31 AC |
1456 | } |
1457 | ||
7f46ecf6 EB |
1458 | /* If this is an aggregate constant initialized to a constant, force it |
1459 | to be statically allocated. This saves an initialization copy. */ | |
1460 | if (!static_flag | |
1461 | && const_flag | |
1462 | && gnu_expr | |
1463 | && TREE_CONSTANT (gnu_expr) | |
1464 | && AGGREGATE_TYPE_P (gnu_type) | |
1465 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type)) | |
1466 | && !(TYPE_IS_PADDING_P (gnu_type) | |
1467 | && !tree_fits_uhwi_p (TYPE_SIZE_UNIT | |
1468 | (TREE_TYPE (TYPE_FIELDS (gnu_type)))))) | |
1469 | static_flag = true; | |
1470 | ||
73a1a803 EB |
1471 | /* If this is an aliased object with an unconstrained array nominal |
1472 | subtype, we make its type a thin reference, i.e. the reference | |
1473 | counterpart of a thin pointer, so it points to the array part. | |
1474 | This is aimed to make it easier for the debugger to decode the | |
1475 | object. Note that we have to do it this late because of the | |
1476 | couple of allocation adjustments that might be made above. */ | |
9182f718 EB |
1477 | if (Is_Constr_Subt_For_UN_Aliased (gnat_type) |
1478 | && Is_Array_Type (Underlying_Type (gnat_type)) | |
184d436a EB |
1479 | && !type_annotate_only) |
1480 | { | |
184d436a EB |
1481 | /* In case the object with the template has already been allocated |
1482 | just above, we have nothing to do here. */ | |
1483 | if (!TYPE_IS_THIN_POINTER_P (gnu_type)) | |
1484 | { | |
c1a569ef EB |
1485 | /* This variable is a GNAT encoding used by Workbench: let it |
1486 | go through the debugging information but mark it as | |
1487 | artificial: users are not interested in it. */ | |
184179f1 EB |
1488 | tree gnu_unc_var |
1489 | = create_var_decl (concat_name (gnu_entity_name, "UNC"), | |
1490 | NULL_TREE, gnu_type, gnu_expr, | |
1491 | const_flag, Is_Public (gnat_entity), | |
2056c5ed | 1492 | imported_p || !definition, static_flag, |
ff9baa5f PMR |
1493 | volatile_flag, true, |
1494 | debug_info_p && definition, | |
2056c5ed | 1495 | NULL, gnat_entity); |
73a1a803 | 1496 | gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var); |
184179f1 | 1497 | TREE_CONSTANT (gnu_expr) = 1; |
184d436a | 1498 | |
184179f1 EB |
1499 | used_by_ref = true; |
1500 | const_flag = true; | |
2056c5ed | 1501 | volatile_flag = false; |
241125b2 EB |
1502 | inner_const_flag = TREE_READONLY (gnu_unc_var); |
1503 | gnu_size = NULL_TREE; | |
184d436a EB |
1504 | } |
1505 | ||
9182f718 | 1506 | tree gnu_array = gnat_to_gnu_type (Base_Type (gnat_type)); |
184d436a EB |
1507 | gnu_type |
1508 | = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array)); | |
1509 | } | |
1510 | ||
fc7a823e EB |
1511 | /* Convert the expression to the type of the object if need be. */ |
1512 | if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr)) | |
a1ab4c31 AC |
1513 | gnu_expr = convert (gnu_type, gnu_expr); |
1514 | ||
1eb58520 AC |
1515 | /* If this name is external or a name was specified, use it, but don't |
1516 | use the Interface_Name with an address clause (see cd30005). */ | |
b3b5c6a2 EB |
1517 | if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity)) |
1518 | || (Present (Interface_Name (gnat_entity)) | |
1519 | && No (Address_Clause (gnat_entity)))) | |
0fb2335d | 1520 | gnu_ext_name = create_concat_name (gnat_entity, NULL); |
a1ab4c31 | 1521 | |
0567ae8d AC |
1522 | /* Deal with a pragma Linker_Section on a constant or variable. */ |
1523 | if ((kind == E_Constant || kind == E_Variable) | |
1524 | && Present (Linker_Section_Pragma (gnat_entity))) | |
1525 | prepend_one_attribute_pragma (&attr_list, | |
1526 | Linker_Section_Pragma (gnat_entity)); | |
1527 | ||
86060344 | 1528 | /* Now create the variable or the constant and set various flags. */ |
58c8f770 | 1529 | gnu_decl |
6249559b EB |
1530 | = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type, |
1531 | gnu_expr, const_flag, Is_Public (gnat_entity), | |
2056c5ed | 1532 | imported_p || !definition, static_flag, |
ff9baa5f PMR |
1533 | volatile_flag, artificial_p, |
1534 | debug_info_p && definition, attr_list, | |
3553d8c2 | 1535 | gnat_entity); |
a1ab4c31 AC |
1536 | DECL_BY_REF_P (gnu_decl) = used_by_ref; |
1537 | DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag; | |
a1c7d797 | 1538 | DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity); |
86060344 EB |
1539 | |
1540 | /* If we are defining an Out parameter and optimization isn't enabled, | |
1541 | create a fake PARM_DECL for debugging purposes and make it point to | |
1542 | the VAR_DECL. Suppress debug info for the latter but make sure it | |
f036807a | 1543 | will live in memory so that it can be accessed from within the |
86060344 | 1544 | debugger through the PARM_DECL. */ |
cd177257 EB |
1545 | if (kind == E_Out_Parameter |
1546 | && definition | |
1547 | && debug_info_p | |
1548 | && !optimize | |
1549 | && !flag_generate_lto) | |
86060344 | 1550 | { |
1e55d29a | 1551 | tree param = create_param_decl (gnu_entity_name, gnu_type); |
86060344 EB |
1552 | gnat_pushdecl (param, gnat_entity); |
1553 | SET_DECL_VALUE_EXPR (param, gnu_decl); | |
1554 | DECL_HAS_VALUE_EXPR_P (param) = 1; | |
1555 | DECL_IGNORED_P (gnu_decl) = 1; | |
1556 | TREE_ADDRESSABLE (gnu_decl) = 1; | |
1557 | } | |
1558 | ||
15bf7d19 EB |
1559 | /* If this is a loop parameter, set the corresponding flag. */ |
1560 | else if (kind == E_Loop_Parameter) | |
1561 | DECL_LOOP_PARM_P (gnu_decl) = 1; | |
1562 | ||
86060344 EB |
1563 | /* If this is a constant and we are defining it or it generates a real |
1564 | symbol at the object level and we are referencing it, we may want | |
1565 | or need to have a true variable to represent it: | |
86060344 EB |
1566 | - if the constant is public and not overlaid on something else, |
1567 | - if its address is taken, | |
104099b8 EB |
1568 | - if it is aliased, |
1569 | - if optimization isn't enabled, for debugging purposes. */ | |
a1ab4c31 AC |
1570 | if (TREE_CODE (gnu_decl) == CONST_DECL |
1571 | && (definition || Sloc (gnat_entity) > Standard_Location) | |
104099b8 | 1572 | && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity))) |
a1ab4c31 AC |
1573 | || Address_Taken (gnat_entity) |
1574 | || Is_Aliased (gnat_entity) | |
104099b8 | 1575 | || (!optimize && debug_info_p))) |
a1ab4c31 AC |
1576 | { |
1577 | tree gnu_corr_var | |
6249559b EB |
1578 | = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type, |
1579 | gnu_expr, true, Is_Public (gnat_entity), | |
2056c5ed | 1580 | !definition, static_flag, volatile_flag, |
ff9baa5f PMR |
1581 | artificial_p, debug_info_p && definition, |
1582 | attr_list, gnat_entity, false); | |
a1ab4c31 AC |
1583 | |
1584 | SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var); | |
104099b8 | 1585 | DECL_IGNORED_P (gnu_decl) = 1; |
a1ab4c31 AC |
1586 | } |
1587 | ||
cb3d597d EB |
1588 | /* If this is a constant, even if we don't need a true variable, we |
1589 | may need to avoid returning the initializer in every case. That | |
1590 | can happen for the address of a (constant) constructor because, | |
1591 | upon dereferencing it, the constructor will be reinjected in the | |
1592 | tree, which may not be valid in every case; see lvalue_required_p | |
1593 | for more details. */ | |
1594 | if (TREE_CODE (gnu_decl) == CONST_DECL) | |
1595 | DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr); | |
1596 | ||
86060344 EB |
1597 | /* If this object is declared in a block that contains a block with an |
1598 | exception handler, and we aren't using the GCC exception mechanism, | |
1599 | we must force this variable in memory in order to avoid an invalid | |
1600 | optimization. */ | |
0ab0bf95 | 1601 | if (Front_End_Exceptions () |
86060344 | 1602 | && Has_Nested_Block_With_Handler (Scope (gnat_entity))) |
a1ab4c31 AC |
1603 | TREE_ADDRESSABLE (gnu_decl) = 1; |
1604 | ||
f036807a EB |
1605 | /* If this is a local variable with non-BLKmode and aggregate type, |
1606 | and optimization isn't enabled, then force it in memory so that | |
1607 | a register won't be allocated to it with possible subparts left | |
1608 | uninitialized and reaching the register allocator. */ | |
1609 | else if (TREE_CODE (gnu_decl) == VAR_DECL | |
1610 | && !DECL_EXTERNAL (gnu_decl) | |
1611 | && !TREE_STATIC (gnu_decl) | |
1612 | && DECL_MODE (gnu_decl) != BLKmode | |
1613 | && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl)) | |
1614 | && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl)) | |
1615 | && !optimize) | |
1616 | TREE_ADDRESSABLE (gnu_decl) = 1; | |
1617 | ||
86060344 EB |
1618 | /* If we are defining an object with variable size or an object with |
1619 | fixed size that will be dynamically allocated, and we are using the | |
0ab0bf95 OH |
1620 | front-end setjmp/longjmp exception mechanism, update the setjmp |
1621 | buffer. */ | |
86060344 | 1622 | if (definition |
0ab0bf95 | 1623 | && Exception_Mechanism == Front_End_SJLJ |
86060344 EB |
1624 | && get_block_jmpbuf_decl () |
1625 | && DECL_SIZE_UNIT (gnu_decl) | |
1626 | && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST | |
1627 | || (flag_stack_check == GENERIC_STACK_CHECK | |
1628 | && compare_tree_int (DECL_SIZE_UNIT (gnu_decl), | |
1629 | STACK_CHECK_MAX_VAR_SIZE) > 0))) | |
dddf8120 EB |
1630 | add_stmt_with_node (build_call_n_expr |
1631 | (update_setjmp_buf_decl, 1, | |
86060344 EB |
1632 | build_unary_op (ADDR_EXPR, NULL_TREE, |
1633 | get_block_jmpbuf_decl ())), | |
1634 | gnat_entity); | |
1635 | ||
f4cd2542 EB |
1636 | /* Back-annotate Esize and Alignment of the object if not already |
1637 | known. Note that we pick the values of the type, not those of | |
1638 | the object, to shield ourselves from low-level platform-dependent | |
1639 | adjustments like alignment promotion. This is both consistent with | |
1640 | all the treatment above, where alignment and size are set on the | |
1641 | type of the object and not on the object directly, and makes it | |
1642 | possible to support all confirming representation clauses. */ | |
1643 | annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size, | |
491f54a7 | 1644 | used_by_ref); |
a1ab4c31 AC |
1645 | } |
1646 | break; | |
1647 | ||
1648 | case E_Void: | |
1649 | /* Return a TYPE_DECL for "void" that we previously made. */ | |
10069d53 | 1650 | gnu_decl = TYPE_NAME (void_type_node); |
a1ab4c31 AC |
1651 | break; |
1652 | ||
1653 | case E_Enumeration_Type: | |
a8e05f92 | 1654 | /* A special case: for the types Character and Wide_Character in |
2ddc34ba | 1655 | Standard, we do not list all the literals. So if the literals |
825da0d2 | 1656 | are not specified, make this an integer type. */ |
a1ab4c31 AC |
1657 | if (No (First_Literal (gnat_entity))) |
1658 | { | |
825da0d2 EB |
1659 | if (esize == CHAR_TYPE_SIZE && flag_signed_char) |
1660 | gnu_type = make_signed_type (CHAR_TYPE_SIZE); | |
1661 | else | |
1662 | gnu_type = make_unsigned_type (esize); | |
0fb2335d | 1663 | TYPE_NAME (gnu_type) = gnu_entity_name; |
a1ab4c31 | 1664 | |
a8e05f92 | 1665 | /* Set TYPE_STRING_FLAG for Character and Wide_Character types. |
2ddc34ba EB |
1666 | This is needed by the DWARF-2 back-end to distinguish between |
1667 | unsigned integer types and character types. */ | |
a1ab4c31 | 1668 | TYPE_STRING_FLAG (gnu_type) = 1; |
825da0d2 EB |
1669 | |
1670 | /* This flag is needed by the call just below. */ | |
1671 | TYPE_ARTIFICIAL (gnu_type) = artificial_p; | |
1672 | ||
1673 | finish_character_type (gnu_type); | |
a1ab4c31 | 1674 | } |
74746d49 EB |
1675 | else |
1676 | { | |
1677 | /* We have a list of enumeral constants in First_Literal. We make a | |
1678 | CONST_DECL for each one and build into GNU_LITERAL_LIST the list | |
1679 | to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST | |
1680 | whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the | |
1681 | value of the literal. But when we have a regular boolean type, we | |
1682 | simplify this a little by using a BOOLEAN_TYPE. */ | |
1683 | const bool is_boolean = Is_Boolean_Type (gnat_entity) | |
1684 | && !Has_Non_Standard_Rep (gnat_entity); | |
1685 | const bool is_unsigned = Is_Unsigned_Type (gnat_entity); | |
1686 | tree gnu_list = NULL_TREE; | |
1687 | Entity_Id gnat_literal; | |
1688 | ||
0d0cd281 EB |
1689 | /* Boolean types with foreign convention have precision 1. */ |
1690 | if (is_boolean && foreign) | |
1691 | esize = 1; | |
1692 | ||
74746d49 EB |
1693 | gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE); |
1694 | TYPE_PRECISION (gnu_type) = esize; | |
1695 | TYPE_UNSIGNED (gnu_type) = is_unsigned; | |
1696 | set_min_and_max_values_for_integral_type (gnu_type, esize, | |
807e902e | 1697 | TYPE_SIGN (gnu_type)); |
74746d49 EB |
1698 | process_attributes (&gnu_type, &attr_list, true, gnat_entity); |
1699 | layout_type (gnu_type); | |
1700 | ||
1701 | for (gnat_literal = First_Literal (gnat_entity); | |
1702 | Present (gnat_literal); | |
1703 | gnat_literal = Next_Literal (gnat_literal)) | |
1704 | { | |
1705 | tree gnu_value | |
1706 | = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type); | |
c1a569ef | 1707 | /* Do not generate debug info for individual enumerators. */ |
74746d49 EB |
1708 | tree gnu_literal |
1709 | = create_var_decl (get_entity_name (gnat_literal), NULL_TREE, | |
1710 | gnu_type, gnu_value, true, false, false, | |
2056c5ed EB |
1711 | false, false, artificial_p, false, |
1712 | NULL, gnat_literal); | |
74746d49 EB |
1713 | save_gnu_tree (gnat_literal, gnu_literal, false); |
1714 | gnu_list | |
1715 | = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list); | |
1716 | } | |
a1ab4c31 | 1717 | |
74746d49 EB |
1718 | if (!is_boolean) |
1719 | TYPE_VALUES (gnu_type) = nreverse (gnu_list); | |
a1ab4c31 | 1720 | |
74746d49 EB |
1721 | /* Note that the bounds are updated at the end of this function |
1722 | to avoid an infinite recursion since they refer to the type. */ | |
1723 | goto discrete_type; | |
1724 | } | |
1725 | break; | |
a1ab4c31 AC |
1726 | |
1727 | case E_Signed_Integer_Type: | |
a1ab4c31 AC |
1728 | /* For integer types, just make a signed type the appropriate number |
1729 | of bits. */ | |
1730 | gnu_type = make_signed_type (esize); | |
40d1f6af | 1731 | goto discrete_type; |
a1ab4c31 | 1732 | |
2971780e PMR |
1733 | case E_Ordinary_Fixed_Point_Type: |
1734 | case E_Decimal_Fixed_Point_Type: | |
1735 | { | |
1736 | /* Small_Value is the scale factor. */ | |
1737 | const Ureal gnat_small_value = Small_Value (gnat_entity); | |
1738 | tree scale_factor = NULL_TREE; | |
1739 | ||
1740 | gnu_type = make_signed_type (esize); | |
1741 | ||
2971780e PMR |
1742 | /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a |
1743 | binary or decimal scale: it is easier to read for humans. */ | |
1744 | if (UI_Eq (Numerator (gnat_small_value), Uint_1) | |
1745 | && (Rbase (gnat_small_value) == 2 | |
1746 | || Rbase (gnat_small_value) == 10)) | |
1747 | { | |
1e3cabd4 EB |
1748 | tree base |
1749 | = build_int_cst (integer_type_node, Rbase (gnat_small_value)); | |
1750 | tree exponent | |
2971780e PMR |
1751 | = build_int_cst (integer_type_node, |
1752 | UI_To_Int (Denominator (gnat_small_value))); | |
1753 | scale_factor | |
1754 | = build2 (RDIV_EXPR, integer_type_node, | |
1755 | integer_one_node, | |
1756 | build2 (POWER_EXPR, integer_type_node, | |
1757 | base, exponent)); | |
1758 | } | |
1759 | ||
43a0debd EB |
1760 | /* Use the arbitrary scale factor description. Note that we support |
1761 | a Small_Value whose magnitude is larger than 64-bit even on 32-bit | |
1762 | platforms, so we unconditionally use a (dummy) 128-bit type. */ | |
6fb8da75 | 1763 | else |
2971780e | 1764 | { |
43a0debd EB |
1765 | const Uint gnat_num = Norm_Num (gnat_small_value); |
1766 | const Uint gnat_den = Norm_Den (gnat_small_value); | |
1767 | tree gnu_small_type = make_unsigned_type (128); | |
1768 | tree gnu_num = UI_To_gnu (gnat_num, gnu_small_type); | |
1769 | tree gnu_den = UI_To_gnu (gnat_den, gnu_small_type); | |
2971780e | 1770 | |
43a0debd EB |
1771 | scale_factor |
1772 | = build2 (RDIV_EXPR, gnu_small_type, gnu_num, gnu_den); | |
2971780e PMR |
1773 | } |
1774 | ||
1775 | TYPE_FIXED_POINT_P (gnu_type) = 1; | |
1776 | SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor); | |
1777 | } | |
1778 | goto discrete_type; | |
1779 | ||
a1ab4c31 | 1780 | case E_Modular_Integer_Type: |
a1ab4c31 | 1781 | { |
1a4cb227 AC |
1782 | /* Packed Array Impl. Types are supposed to be subtypes only. */ |
1783 | gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity)); | |
a1ab4c31 | 1784 | |
815b5368 EB |
1785 | /* For modular types, make the unsigned type of the proper number |
1786 | of bits and then set up the modulus, if required. */ | |
a8e05f92 | 1787 | gnu_type = make_unsigned_type (esize); |
a1ab4c31 | 1788 | |
815b5368 EB |
1789 | /* Get the modulus in this type. If the modulus overflows, assume |
1790 | that this is because it was equal to 2**Esize. Note that there | |
1791 | is no overflow checking done on unsigned types, so we detect the | |
1792 | overflow by looking for a modulus of zero, which is invalid. */ | |
1793 | tree gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type); | |
a1ab4c31 | 1794 | |
815b5368 EB |
1795 | /* If the modulus is not 2**Esize, then this also means that the upper |
1796 | bound of the type, i.e. modulus - 1, is not maximal, so we create an | |
1797 | extra subtype to carry it and set the modulus on the base type. */ | |
a1ab4c31 AC |
1798 | if (!integer_zerop (gnu_modulus)) |
1799 | { | |
815b5368 | 1800 | TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT"); |
a1ab4c31 AC |
1801 | TYPE_MODULAR_P (gnu_type) = 1; |
1802 | SET_TYPE_MODULUS (gnu_type, gnu_modulus); | |
815b5368 EB |
1803 | tree gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus, |
1804 | build_int_cst (gnu_type, 1)); | |
683ccd05 EB |
1805 | gnu_type |
1806 | = create_extra_subtype (gnu_type, TYPE_MIN_VALUE (gnu_type), | |
1807 | gnu_high); | |
a1ab4c31 AC |
1808 | } |
1809 | } | |
40d1f6af | 1810 | goto discrete_type; |
a1ab4c31 AC |
1811 | |
1812 | case E_Signed_Integer_Subtype: | |
1813 | case E_Enumeration_Subtype: | |
1814 | case E_Modular_Integer_Subtype: | |
1815 | case E_Ordinary_Fixed_Point_Subtype: | |
1816 | case E_Decimal_Fixed_Point_Subtype: | |
1817 | ||
26383c64 | 1818 | /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do |
84fb43a1 | 1819 | not want to call create_range_type since we would like each subtype |
26383c64 | 1820 | node to be distinct. ??? Historically this was in preparation for |
c1abd261 | 1821 | when memory aliasing is implemented, but that's obsolete now given |
26383c64 | 1822 | the call to relate_alias_sets below. |
a1ab4c31 | 1823 | |
a8e05f92 EB |
1824 | The TREE_TYPE field of the INTEGER_TYPE points to the base type; |
1825 | this fact is used by the arithmetic conversion functions. | |
a1ab4c31 | 1826 | |
a8e05f92 EB |
1827 | We elaborate the Ancestor_Subtype if it is not in the current unit |
1828 | and one of our bounds is non-static. We do this to ensure consistent | |
1829 | naming in the case where several subtypes share the same bounds, by | |
1830 | elaborating the first such subtype first, thus using its name. */ | |
a1ab4c31 AC |
1831 | |
1832 | if (!definition | |
1833 | && Present (Ancestor_Subtype (gnat_entity)) | |
1834 | && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity)) | |
1835 | && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity)) | |
1836 | || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity)))) | |
afc737f0 | 1837 | gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false); |
a1ab4c31 | 1838 | |
84fb43a1 | 1839 | /* Set the precision to the Esize except for bit-packed arrays. */ |
1e3cabd4 | 1840 | if (Is_Packed_Array_Impl_Type (gnat_entity)) |
6e0f0975 | 1841 | esize = UI_To_Int (RM_Size (gnat_entity)); |
a1ab4c31 | 1842 | |
0d0cd281 EB |
1843 | /* Boolean types with foreign convention have precision 1. */ |
1844 | if (Is_Boolean_Type (gnat_entity) && foreign) | |
1845 | { | |
1846 | gnu_type = make_node (BOOLEAN_TYPE); | |
1847 | TYPE_PRECISION (gnu_type) = 1; | |
1848 | TYPE_UNSIGNED (gnu_type) = 1; | |
1849 | set_min_and_max_values_for_integral_type (gnu_type, 1, UNSIGNED); | |
1850 | layout_type (gnu_type); | |
1851 | } | |
825da0d2 EB |
1852 | /* First subtypes of Character are treated as Character; otherwise |
1853 | this should be an unsigned type if the base type is unsigned or | |
84fb43a1 | 1854 | if the lower bound is constant and non-negative or if the type |
55c8849f EB |
1855 | is biased. However, even if the lower bound is constant and |
1856 | non-negative, we use a signed type for a subtype with the same | |
1857 | size as its signed base type, because this eliminates useless | |
1858 | conversions to it and gives more leeway to the optimizer; but | |
1859 | this means that we will need to explicitly test for this case | |
1860 | when we change the representation based on the RM size. */ | |
0d0cd281 | 1861 | else if (kind == E_Enumeration_Subtype |
825da0d2 EB |
1862 | && No (First_Literal (Etype (gnat_entity))) |
1863 | && Esize (gnat_entity) == RM_Size (gnat_entity) | |
1864 | && esize == CHAR_TYPE_SIZE | |
1865 | && flag_signed_char) | |
1866 | gnu_type = make_signed_type (CHAR_TYPE_SIZE); | |
47605312 | 1867 | else if (Is_Unsigned_Type (Underlying_Type (Etype (gnat_entity))) |
55c8849f EB |
1868 | || (Esize (Etype (gnat_entity)) != Esize (gnat_entity) |
1869 | && Is_Unsigned_Type (gnat_entity)) | |
825da0d2 | 1870 | || Has_Biased_Representation (gnat_entity)) |
84fb43a1 EB |
1871 | gnu_type = make_unsigned_type (esize); |
1872 | else | |
1873 | gnu_type = make_signed_type (esize); | |
1874 | TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity)); | |
a1ab4c31 | 1875 | |
84fb43a1 | 1876 | SET_TYPE_RM_MIN_VALUE |
1eb58520 | 1877 | (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity), |
bf44701f | 1878 | gnat_entity, "L", definition, true, |
c1a569ef | 1879 | debug_info_p)); |
84fb43a1 EB |
1880 | |
1881 | SET_TYPE_RM_MAX_VALUE | |
1eb58520 | 1882 | (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity), |
bf44701f | 1883 | gnat_entity, "U", definition, true, |
c1a569ef | 1884 | debug_info_p)); |
a1ab4c31 | 1885 | |
0d0cd281 EB |
1886 | if (TREE_CODE (gnu_type) == INTEGER_TYPE) |
1887 | TYPE_BIASED_REPRESENTATION_P (gnu_type) | |
1888 | = Has_Biased_Representation (gnat_entity); | |
74746d49 | 1889 | |
2c1f5c0a | 1890 | /* Do the same processing for Character subtypes as for types. */ |
c2352415 | 1891 | if (TREE_CODE (TREE_TYPE (gnu_type)) == INTEGER_TYPE |
f4af4019 | 1892 | && TYPE_STRING_FLAG (TREE_TYPE (gnu_type))) |
2c1f5c0a EB |
1893 | { |
1894 | TYPE_NAME (gnu_type) = gnu_entity_name; | |
1895 | TYPE_STRING_FLAG (gnu_type) = 1; | |
1896 | TYPE_ARTIFICIAL (gnu_type) = artificial_p; | |
1897 | finish_character_type (gnu_type); | |
1898 | } | |
825da0d2 | 1899 | |
74746d49 EB |
1900 | /* Inherit our alias set from what we're a subtype of. Subtypes |
1901 | are not different types and a pointer can designate any instance | |
1902 | within a subtype hierarchy. */ | |
1903 | relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY); | |
1904 | ||
a1ab4c31 AC |
1905 | /* One of the above calls might have caused us to be elaborated, |
1906 | so don't blow up if so. */ | |
1907 | if (present_gnu_tree (gnat_entity)) | |
1908 | { | |
1909 | maybe_present = true; | |
1910 | break; | |
1911 | } | |
1912 | ||
4fd78fe6 EB |
1913 | /* Attach the TYPE_STUB_DECL in case we have a parallel type. */ |
1914 | TYPE_STUB_DECL (gnu_type) | |
1915 | = create_type_stub_decl (gnu_entity_name, gnu_type); | |
1916 | ||
40d1f6af EB |
1917 | discrete_type: |
1918 | ||
b1fa9126 EB |
1919 | /* We have to handle clauses that under-align the type specially. */ |
1920 | if ((Present (Alignment_Clause (gnat_entity)) | |
1a4cb227 | 1921 | || (Is_Packed_Array_Impl_Type (gnat_entity) |
b1fa9126 EB |
1922 | && Present |
1923 | (Alignment_Clause (Original_Array_Type (gnat_entity))))) | |
1924 | && UI_Is_In_Int_Range (Alignment (gnat_entity))) | |
1925 | { | |
1926 | align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT; | |
1927 | if (align >= TYPE_ALIGN (gnu_type)) | |
1928 | align = 0; | |
1929 | } | |
1930 | ||
6e0f0975 | 1931 | /* If the type we are dealing with represents a bit-packed array, |
a1ab4c31 AC |
1932 | we need to have the bits left justified on big-endian targets |
1933 | and right justified on little-endian targets. We also need to | |
1934 | ensure that when the value is read (e.g. for comparison of two | |
1935 | such values), we only get the good bits, since the unused bits | |
6e0f0975 EB |
1936 | are uninitialized. Both goals are accomplished by wrapping up |
1937 | the modular type in an enclosing record type. */ | |
1e3cabd4 | 1938 | if (Is_Packed_Array_Impl_Type (gnat_entity)) |
a1ab4c31 | 1939 | { |
1e3cabd4 EB |
1940 | tree gnu_field_type, gnu_field, t; |
1941 | ||
1942 | gcc_assert (Is_Bit_Packed_Array (Original_Array_Type (gnat_entity))); | |
1943 | TYPE_BIT_PACKED_ARRAY_TYPE_P (gnu_type) = 1; | |
1944 | ||
1945 | /* Make the original array type a parallel/debug type. */ | |
1946 | if (debug_info_p) | |
1947 | { | |
1948 | tree gnu_name | |
1949 | = associate_original_type_to_packed_array (gnu_type, | |
1950 | gnat_entity); | |
1951 | if (gnu_name) | |
1952 | gnu_entity_name = gnu_name; | |
1953 | } | |
a1ab4c31 | 1954 | |
b1fa9126 | 1955 | /* Set the RM size before wrapping up the original type. */ |
84fb43a1 EB |
1956 | SET_TYPE_RM_SIZE (gnu_type, |
1957 | UI_To_gnu (RM_Size (gnat_entity), bitsizetype)); | |
b1fa9126 EB |
1958 | |
1959 | /* Create a stripped-down declaration, mainly for debugging. */ | |
1e3cabd4 EB |
1960 | t = create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p, |
1961 | gnat_entity); | |
b1fa9126 EB |
1962 | |
1963 | /* Now save it and build the enclosing record type. */ | |
6e0f0975 EB |
1964 | gnu_field_type = gnu_type; |
1965 | ||
a1ab4c31 AC |
1966 | gnu_type = make_node (RECORD_TYPE); |
1967 | TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM"); | |
a1ab4c31 | 1968 | TYPE_PACKED (gnu_type) = 1; |
b1fa9126 EB |
1969 | TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type); |
1970 | TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type); | |
1971 | SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type)); | |
1972 | ||
1973 | /* Propagate the alignment of the modular type to the record type, | |
1974 | unless there is an alignment clause that under-aligns the type. | |
1975 | This means that bit-packed arrays are given "ceil" alignment for | |
1976 | their size by default, which may seem counter-intuitive but makes | |
1977 | it possible to overlay them on modular types easily. */ | |
fe37c7af MM |
1978 | SET_TYPE_ALIGN (gnu_type, |
1979 | align > 0 ? align : TYPE_ALIGN (gnu_field_type)); | |
a1ab4c31 | 1980 | |
ee45a32d EB |
1981 | /* Propagate the reverse storage order flag to the record type so |
1982 | that the required byte swapping is performed when retrieving the | |
1983 | enclosed modular value. */ | |
1984 | TYPE_REVERSE_STORAGE_ORDER (gnu_type) | |
1985 | = Reverse_Storage_Order (Original_Array_Type (gnat_entity)); | |
1986 | ||
b1fa9126 | 1987 | relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY); |
a1ab4c31 | 1988 | |
40d1f6af EB |
1989 | /* Don't declare the field as addressable since we won't be taking |
1990 | its address and this would prevent create_field_decl from making | |
1991 | a bitfield. */ | |
da01bfee EB |
1992 | gnu_field |
1993 | = create_field_decl (get_identifier ("OBJECT"), gnu_field_type, | |
1994 | gnu_type, NULL_TREE, bitsize_zero_node, 1, 0); | |
a1ab4c31 | 1995 | |
afc737f0 | 1996 | /* We will output additional debug info manually below. */ |
b1fa9126 | 1997 | finish_record_type (gnu_type, gnu_field, 2, false); |
a1ab4c31 | 1998 | TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1; |
a1ab4c31 | 1999 | |
1e3cabd4 EB |
2000 | /* Make the original array type a parallel/debug type. Note that |
2001 | gnat_get_array_descr_info needs a TYPE_IMPL_PACKED_ARRAY_P type | |
2002 | so we use an intermediate step for standard DWARF. */ | |
032d1b71 EB |
2003 | if (debug_info_p) |
2004 | { | |
88ef1a14 | 2005 | if (gnat_encodings != DWARF_GNAT_ENCODINGS_ALL) |
58d32c72 | 2006 | SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type); |
88ef1a14 EB |
2007 | else if (DECL_PARALLEL_TYPE (t)) |
2008 | add_parallel_type (gnu_type, DECL_PARALLEL_TYPE (t)); | |
032d1b71 | 2009 | } |
a1ab4c31 AC |
2010 | } |
2011 | ||
2012 | /* If the type we are dealing with has got a smaller alignment than the | |
940ff20c | 2013 | natural one, we need to wrap it up in a record type and misalign the |
b3f75672 | 2014 | latter; we reuse the padding machinery for this purpose. */ |
b1fa9126 | 2015 | else if (align > 0) |
a1ab4c31 | 2016 | { |
b3f75672 | 2017 | tree gnu_size = UI_To_gnu (RM_Size (gnat_entity), bitsizetype); |
b1fa9126 | 2018 | |
b3f75672 EB |
2019 | /* Set the RM size before wrapping the type. */ |
2020 | SET_TYPE_RM_SIZE (gnu_type, gnu_size); | |
b1fa9126 | 2021 | |
1e3cabd4 EB |
2022 | /* Create a stripped-down declaration, mainly for debugging. */ |
2023 | create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p, | |
2024 | gnat_entity); | |
2025 | ||
b3f75672 EB |
2026 | gnu_type |
2027 | = maybe_pad_type (gnu_type, TYPE_SIZE (gnu_type), align, | |
1e3cabd4 | 2028 | gnat_entity, false, definition, false); |
a1ab4c31 | 2029 | |
a1ab4c31 | 2030 | TYPE_PACKED (gnu_type) = 1; |
b3f75672 | 2031 | SET_TYPE_ADA_SIZE (gnu_type, gnu_size); |
a1ab4c31 AC |
2032 | } |
2033 | ||
a1ab4c31 AC |
2034 | break; |
2035 | ||
2036 | case E_Floating_Point_Type: | |
a1ab4c31 AC |
2037 | /* The type of the Low and High bounds can be our type if this is |
2038 | a type from Standard, so set them at the end of the function. */ | |
2039 | gnu_type = make_node (REAL_TYPE); | |
2040 | TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize); | |
2041 | layout_type (gnu_type); | |
2042 | break; | |
2043 | ||
2044 | case E_Floating_Point_Subtype: | |
74746d49 EB |
2045 | /* See the E_Signed_Integer_Subtype case for the rationale. */ |
2046 | if (!definition | |
2047 | && Present (Ancestor_Subtype (gnat_entity)) | |
2048 | && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity)) | |
2049 | && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity)) | |
2050 | || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity)))) | |
afc737f0 | 2051 | gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, false); |
a1ab4c31 | 2052 | |
74746d49 EB |
2053 | gnu_type = make_node (REAL_TYPE); |
2054 | TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity)); | |
2055 | TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize); | |
2056 | TYPE_GCC_MIN_VALUE (gnu_type) | |
2057 | = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type)); | |
2058 | TYPE_GCC_MAX_VALUE (gnu_type) | |
2059 | = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type)); | |
2060 | layout_type (gnu_type); | |
2061 | ||
2062 | SET_TYPE_RM_MIN_VALUE | |
1eb58520 | 2063 | (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity), |
bf44701f | 2064 | gnat_entity, "L", definition, true, |
c1a569ef | 2065 | debug_info_p)); |
74746d49 EB |
2066 | |
2067 | SET_TYPE_RM_MAX_VALUE | |
1eb58520 | 2068 | (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity), |
bf44701f | 2069 | gnat_entity, "U", definition, true, |
c1a569ef | 2070 | debug_info_p)); |
74746d49 EB |
2071 | |
2072 | /* Inherit our alias set from what we're a subtype of, as for | |
2073 | integer subtypes. */ | |
2074 | relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY); | |
2075 | ||
2076 | /* One of the above calls might have caused us to be elaborated, | |
2077 | so don't blow up if so. */ | |
2078 | maybe_present = true; | |
2079 | break; | |
a1ab4c31 | 2080 | |
e8fa3dcd | 2081 | /* Array Types and Subtypes |
a1ab4c31 | 2082 | |
a27aceb9 EB |
2083 | In GNAT unconstrained array types are represented by E_Array_Type and |
2084 | constrained array types are represented by E_Array_Subtype. They are | |
2085 | translated into UNCONSTRAINED_ARRAY_TYPE and ARRAY_TYPE respectively. | |
2086 | But there are no actual objects of an unconstrained array type; all we | |
2087 | have are pointers to that type. In addition to the type node itself, | |
2088 | 4 other types associated with it are built in the process: | |
a1ab4c31 | 2089 | |
a27aceb9 | 2090 | 1. the array type (suffix XUA) containing the actual data, |
a1ab4c31 | 2091 | |
a27aceb9 EB |
2092 | 2. the template type (suffix XUB) containng the bounds, |
2093 | ||
2094 | 3. the fat pointer type (suffix XUP) representing a pointer or a | |
2095 | reference to the unconstrained array type: | |
2096 | XUP = struct { XUA *, XUB * } | |
2097 | ||
2098 | 4. the object record type (suffix XUT) containing bounds and data: | |
2099 | XUT = struct { XUB, XUA } | |
2100 | ||
2101 | The bounds of the array type XUA (de)reference the XUB * field of a | |
2102 | PLACEHOLDER_EXPR for the fat pointer type XUP, so the array type XUA | |
2103 | is to be interpreted in the context of the fat pointer type XUB for | |
2104 | debug info purposes. */ | |
a1ab4c31 | 2105 | |
a1ab4c31 AC |
2106 | case E_Array_Type: |
2107 | { | |
1eff5289 | 2108 | const Entity_Id PAT = Packed_Array_Impl_Type (gnat_entity); |
4e6602a8 EB |
2109 | const bool convention_fortran_p |
2110 | = (Convention (gnat_entity) == Convention_Fortran); | |
2111 | const int ndim = Number_Dimensions (gnat_entity); | |
2afda005 TG |
2112 | tree gnu_template_type; |
2113 | tree gnu_ptr_template; | |
e3edbd56 | 2114 | tree gnu_template_reference, gnu_template_fields, gnu_fat_type; |
2bb1fc26 NF |
2115 | tree *gnu_index_types = XALLOCAVEC (tree, ndim); |
2116 | tree *gnu_temp_fields = XALLOCAVEC (tree, ndim); | |
a27aceb9 | 2117 | tree gnu_max_size = size_one_node, tem, obj; |
1e3cabd4 | 2118 | Entity_Id gnat_index; |
4e6602a8 | 2119 | int index; |
9aa04cc7 AC |
2120 | tree comp_type; |
2121 | ||
2122 | /* Create the type for the component now, as it simplifies breaking | |
2123 | type reference loops. */ | |
2124 | comp_type | |
2125 | = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p); | |
2126 | if (present_gnu_tree (gnat_entity)) | |
2127 | { | |
2128 | /* As a side effect, the type may have been translated. */ | |
2129 | maybe_present = true; | |
2130 | break; | |
2131 | } | |
a1ab4c31 | 2132 | |
e3edbd56 EB |
2133 | /* We complete an existing dummy fat pointer type in place. This both |
2134 | avoids further complex adjustments in update_pointer_to and yields | |
2135 | better debugging information in DWARF by leveraging the support for | |
2136 | incomplete declarations of "tagged" types in the DWARF back-end. */ | |
2137 | gnu_type = get_dummy_type (gnat_entity); | |
2138 | if (gnu_type && TYPE_POINTER_TO (gnu_type)) | |
2139 | { | |
2140 | gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type)); | |
2141 | TYPE_NAME (gnu_fat_type) = NULL_TREE; | |
2afda005 | 2142 | gnu_ptr_template = |
259cc9a7 | 2143 | TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type))); |
2afda005 | 2144 | gnu_template_type = TREE_TYPE (gnu_ptr_template); |
259cc9a7 EB |
2145 | |
2146 | /* Save the contents of the dummy type for update_pointer_to. */ | |
2147 | TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type); | |
2148 | TYPE_FIELDS (TYPE_POINTER_TO (gnu_type)) | |
2149 | = copy_node (TYPE_FIELDS (gnu_fat_type)); | |
2150 | DECL_CHAIN (TYPE_FIELDS (TYPE_POINTER_TO (gnu_type))) | |
2151 | = copy_node (DECL_CHAIN (TYPE_FIELDS (gnu_fat_type))); | |
e3edbd56 EB |
2152 | } |
2153 | else | |
2afda005 TG |
2154 | { |
2155 | gnu_fat_type = make_node (RECORD_TYPE); | |
2156 | gnu_template_type = make_node (RECORD_TYPE); | |
2157 | gnu_ptr_template = build_pointer_type (gnu_template_type); | |
2158 | } | |
a1ab4c31 AC |
2159 | |
2160 | /* Make a node for the array. If we are not defining the array | |
2161 | suppress expanding incomplete types. */ | |
2162 | gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE); | |
2163 | ||
2164 | if (!definition) | |
8cd28148 EB |
2165 | { |
2166 | defer_incomplete_level++; | |
2167 | this_deferred = true; | |
2168 | } | |
a1ab4c31 AC |
2169 | |
2170 | /* Build the fat pointer type. Use a "void *" object instead of | |
2171 | a pointer to the array type since we don't have the array type | |
259cc9a7 EB |
2172 | yet (it will reference the fat pointer via the bounds). Note |
2173 | that we reuse the existing fields of a dummy type because for: | |
2174 | ||
2175 | type Arr is array (Positive range <>) of Element_Type; | |
2176 | type Array_Ref is access Arr; | |
2177 | Var : Array_Ref := Null; | |
2178 | ||
2179 | in a declarative part, Arr will be frozen only after Var, which | |
2180 | means that the fields used in the CONSTRUCTOR built for Null are | |
2181 | those of the dummy type, which in turn means that COMPONENT_REFs | |
2182 | of Var may be built with these fields. Now if COMPONENT_REFs of | |
2183 | Var are also built later with the fields of the final type, the | |
2184 | aliasing machinery may consider that the accesses are distinct | |
2185 | if the FIELD_DECLs are distinct as objects. */ | |
e3edbd56 EB |
2186 | if (COMPLETE_TYPE_P (gnu_fat_type)) |
2187 | { | |
259cc9a7 EB |
2188 | tem = TYPE_FIELDS (gnu_fat_type); |
2189 | TREE_TYPE (tem) = ptr_type_node; | |
2190 | TREE_TYPE (DECL_CHAIN (tem)) = gnu_ptr_template; | |
2191 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (gnu_fat_type)) = 0; | |
a27aceb9 | 2192 | for (tree t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t)) |
259cc9a7 | 2193 | SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type); |
e3edbd56 EB |
2194 | } |
2195 | else | |
2196 | { | |
fc130ab5 EB |
2197 | /* We make the fields addressable for the sake of compatibility |
2198 | with languages for which the regular fields are addressable. */ | |
259cc9a7 EB |
2199 | tem |
2200 | = create_field_decl (get_identifier ("P_ARRAY"), | |
2201 | ptr_type_node, gnu_fat_type, | |
fc130ab5 | 2202 | NULL_TREE, NULL_TREE, 0, 1); |
259cc9a7 EB |
2203 | DECL_CHAIN (tem) |
2204 | = create_field_decl (get_identifier ("P_BOUNDS"), | |
2205 | gnu_ptr_template, gnu_fat_type, | |
fc130ab5 | 2206 | NULL_TREE, NULL_TREE, 0, 1); |
e3edbd56 EB |
2207 | finish_fat_pointer_type (gnu_fat_type, tem); |
2208 | SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type); | |
2209 | } | |
a1ab4c31 | 2210 | |
a27aceb9 | 2211 | /* If the GNAT encodings are used, give the fat pointer type a name. |
1eff5289 EB |
2212 | If this is a packed type implemented specially, tell the debugger |
2213 | how to interpret the underlying bits by fetching the name of the | |
2214 | implementation type. But, in any case, mark it as artificial so | |
2215 | the debugger can skip it. */ | |
a27aceb9 | 2216 | const Entity_Id gnat_name |
58d32c72 | 2217 | = Present (PAT) && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL |
1eff5289 | 2218 | ? PAT |
a27aceb9 EB |
2219 | : gnat_entity; |
2220 | tree xup_name | |
58d32c72 | 2221 | = gnat_encodings == DWARF_GNAT_ENCODINGS_ALL |
a27aceb9 EB |
2222 | ? create_concat_name (gnat_name, "XUP") |
2223 | : gnu_entity_name; | |
2224 | create_type_decl (xup_name, gnu_fat_type, true, debug_info_p, | |
2225 | gnat_entity); | |
2226 | ||
a1ab4c31 AC |
2227 | /* Build a reference to the template from a PLACEHOLDER_EXPR that |
2228 | is the fat pointer. This will be used to access the individual | |
2229 | fields once we build them. */ | |
2230 | tem = build3 (COMPONENT_REF, gnu_ptr_template, | |
2231 | build0 (PLACEHOLDER_EXPR, gnu_fat_type), | |
910ad8de | 2232 | DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE); |
a1ab4c31 AC |
2233 | gnu_template_reference |
2234 | = build_unary_op (INDIRECT_REF, gnu_template_type, tem); | |
2235 | TREE_READONLY (gnu_template_reference) = 1; | |
50179d58 | 2236 | TREE_THIS_NOTRAP (gnu_template_reference) = 1; |
a1ab4c31 | 2237 | |
4e6602a8 EB |
2238 | /* Now create the GCC type for each index and add the fields for that |
2239 | index to the template. */ | |
2240 | for (index = (convention_fortran_p ? ndim - 1 : 0), | |
2241 | gnat_index = First_Index (gnat_entity); | |
278f422c | 2242 | IN_RANGE (index, 0, ndim - 1); |
4e6602a8 EB |
2243 | index += (convention_fortran_p ? - 1 : 1), |
2244 | gnat_index = Next_Index (gnat_index)) | |
a1ab4c31 | 2245 | { |
3ccd5d71 EB |
2246 | const bool is_flb |
2247 | = Is_Fixed_Lower_Bound_Index_Subtype (Etype (gnat_index)); | |
9a1bdc31 | 2248 | tree gnu_index_type = get_unpadded_type (Etype (gnat_index)); |
683ccd05 EB |
2249 | tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type); |
2250 | tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type); | |
2251 | tree gnu_index_base_type = get_base_type (gnu_index_type); | |
2252 | tree gnu_lb_field, gnu_hb_field; | |
b6c056fe | 2253 | tree gnu_min, gnu_max, gnu_high; |
3ccd5d71 | 2254 | char field_name[16]; |
4e6602a8 | 2255 | |
683ccd05 EB |
2256 | /* Update the maximum size of the array in elements. */ |
2257 | if (gnu_max_size) | |
2258 | gnu_max_size | |
2259 | = update_n_elem (gnu_max_size, gnu_orig_min, gnu_orig_max); | |
2260 | ||
2261 | /* Now build the self-referential bounds of the index type. */ | |
2262 | gnu_index_type = maybe_character_type (gnu_index_type); | |
2263 | gnu_index_base_type = maybe_character_type (gnu_index_base_type); | |
2264 | ||
4e6602a8 EB |
2265 | /* Make the FIELD_DECLs for the low and high bounds of this |
2266 | type and then make extractions of these fields from the | |
a1ab4c31 AC |
2267 | template. */ |
2268 | sprintf (field_name, "LB%d", index); | |
b6c056fe | 2269 | gnu_lb_field = create_field_decl (get_identifier (field_name), |
683ccd05 | 2270 | gnu_index_type, |
da01bfee EB |
2271 | gnu_template_type, NULL_TREE, |
2272 | NULL_TREE, 0, 0); | |
a1ab4c31 | 2273 | Sloc_to_locus (Sloc (gnat_entity), |
b6c056fe | 2274 | &DECL_SOURCE_LOCATION (gnu_lb_field)); |
4e6602a8 EB |
2275 | |
2276 | field_name[0] = 'U'; | |
b6c056fe | 2277 | gnu_hb_field = create_field_decl (get_identifier (field_name), |
683ccd05 | 2278 | gnu_index_type, |
da01bfee EB |
2279 | gnu_template_type, NULL_TREE, |
2280 | NULL_TREE, 0, 0); | |
a1ab4c31 | 2281 | Sloc_to_locus (Sloc (gnat_entity), |
b6c056fe | 2282 | &DECL_SOURCE_LOCATION (gnu_hb_field)); |
a1ab4c31 | 2283 | |
b6c056fe | 2284 | gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field); |
4e6602a8 EB |
2285 | |
2286 | /* We can't use build_component_ref here since the template type | |
2287 | isn't complete yet. */ | |
3ccd5d71 EB |
2288 | if (!is_flb) |
2289 | { | |
2290 | gnu_orig_min = build3 (COMPONENT_REF, TREE_TYPE (gnu_lb_field), | |
2291 | gnu_template_reference, gnu_lb_field, | |
2292 | NULL_TREE); | |
2293 | TREE_READONLY (gnu_orig_min) = 1; | |
2294 | } | |
2295 | ||
683ccd05 | 2296 | gnu_orig_max = build3 (COMPONENT_REF, TREE_TYPE (gnu_hb_field), |
b6c056fe EB |
2297 | gnu_template_reference, gnu_hb_field, |
2298 | NULL_TREE); | |
3ccd5d71 | 2299 | TREE_READONLY (gnu_orig_max) = 1; |
b6c056fe EB |
2300 | |
2301 | gnu_min = convert (sizetype, gnu_orig_min); | |
2302 | gnu_max = convert (sizetype, gnu_orig_max); | |
2303 | ||
2304 | /* Compute the size of this dimension. See the E_Array_Subtype | |
2305 | case below for the rationale. */ | |
3ccd5d71 EB |
2306 | if (is_flb |
2307 | && Nkind (gnat_index) == N_Subtype_Indication | |
2308 | && flb_cannot_be_superflat (gnat_index)) | |
2309 | gnu_high = gnu_max; | |
2310 | ||
2311 | else | |
2312 | gnu_high | |
2313 | = build3 (COND_EXPR, sizetype, | |
2314 | build2 (GE_EXPR, boolean_type_node, | |
2315 | gnu_orig_max, gnu_orig_min), | |
2316 | gnu_max, | |
2317 | TREE_CODE (gnu_min) == INTEGER_CST | |
2318 | ? int_const_binop (MINUS_EXPR, gnu_min, size_one_node) | |
2319 | : size_binop (MINUS_EXPR, gnu_min, size_one_node)); | |
03b6f8a2 | 2320 | |
4e6602a8 | 2321 | /* Make a range type with the new range in the Ada base type. |
03b6f8a2 | 2322 | Then make an index type with the size range in sizetype. */ |
a1ab4c31 | 2323 | gnu_index_types[index] |
b6c056fe | 2324 | = create_index_type (gnu_min, gnu_high, |
4e6602a8 | 2325 | create_range_type (gnu_index_base_type, |
b6c056fe EB |
2326 | gnu_orig_min, |
2327 | gnu_orig_max), | |
a1ab4c31 | 2328 | gnat_entity); |
4e6602a8 | 2329 | |
a1ab4c31 AC |
2330 | TYPE_NAME (gnu_index_types[index]) |
2331 | = create_concat_name (gnat_entity, field_name); | |
2332 | } | |
2333 | ||
e3edbd56 EB |
2334 | /* Install all the fields into the template. */ |
2335 | TYPE_NAME (gnu_template_type) | |
2336 | = create_concat_name (gnat_entity, "XUB"); | |
2337 | gnu_template_fields = NULL_TREE; | |
a1ab4c31 AC |
2338 | for (index = 0; index < ndim; index++) |
2339 | gnu_template_fields | |
2340 | = chainon (gnu_template_fields, gnu_temp_fields[index]); | |
032d1b71 EB |
2341 | finish_record_type (gnu_template_type, gnu_template_fields, 0, |
2342 | debug_info_p); | |
a27aceb9 | 2343 | TYPE_CONTEXT (gnu_template_type) = current_function_decl; |
a1ab4c31 | 2344 | |
a1ab4c31 AC |
2345 | /* If Component_Size is not already specified, annotate it with the |
2346 | size of the component. */ | |
8de68eb3 | 2347 | if (!Known_Component_Size (gnat_entity)) |
9aa04cc7 AC |
2348 | Set_Component_Size (gnat_entity, |
2349 | annotate_value (TYPE_SIZE (comp_type))); | |
a1ab4c31 | 2350 | |
683ccd05 | 2351 | /* Compute the maximum size of the array in units. */ |
4e6602a8 | 2352 | if (gnu_max_size) |
683ccd05 EB |
2353 | gnu_max_size |
2354 | = size_binop (MULT_EXPR, gnu_max_size, TYPE_SIZE_UNIT (comp_type)); | |
a1ab4c31 | 2355 | |
4e6602a8 | 2356 | /* Now build the array type. */ |
9aa04cc7 | 2357 | tem = comp_type; |
a1ab4c31 AC |
2358 | for (index = ndim - 1; index >= 0; index--) |
2359 | { | |
523e82a7 | 2360 | tem = build_nonshared_array_type (tem, gnu_index_types[index]); |
a1ab4c31 | 2361 | TYPE_MULTI_ARRAY_P (tem) = (index > 0); |
d42b7559 EB |
2362 | TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p; |
2363 | if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity)) | |
2364 | set_reverse_storage_order_on_array_type (tem); | |
d8e94f79 | 2365 | if (array_type_has_nonaliased_component (tem, gnat_entity)) |
d42b7559 | 2366 | set_nonaliased_component_on_array_type (tem); |
a1ab4c31 AC |
2367 | } |
2368 | ||
dd9a8fff EB |
2369 | /* If this is a packed type implemented specially, then process the |
2370 | implementation type so it is elaborated in the proper scope. */ | |
1eff5289 EB |
2371 | if (Present (PAT)) |
2372 | gnat_to_gnu_entity (PAT, NULL_TREE, false); | |
dd9a8fff EB |
2373 | |
2374 | /* Otherwise, if an alignment is specified, use it if valid and, if | |
2375 | the alignment was requested with an explicit clause, state so. */ | |
2376 | else if (Known_Alignment (gnat_entity)) | |
a1ab4c31 | 2377 | { |
fe37c7af MM |
2378 | SET_TYPE_ALIGN (tem, |
2379 | validate_alignment (Alignment (gnat_entity), | |
2380 | gnat_entity, | |
2381 | TYPE_ALIGN (tem))); | |
a1ab4c31 AC |
2382 | if (Present (Alignment_Clause (gnat_entity))) |
2383 | TYPE_USER_ALIGN (tem) = 1; | |
2384 | } | |
2385 | ||
2d595887 PMR |
2386 | /* Tag top-level ARRAY_TYPE nodes for packed arrays and their |
2387 | implementation types as such so that the debug information back-end | |
2388 | can output the appropriate description for them. */ | |
2389 | TYPE_PACKED (tem) | |
2390 | = (Is_Packed (gnat_entity) | |
2391 | || Is_Packed_Array_Impl_Type (gnat_entity)); | |
2392 | ||
f797c2b7 EB |
2393 | if (Treat_As_Volatile (gnat_entity)) |
2394 | tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE); | |
2395 | ||
e3edbd56 | 2396 | /* Adjust the type of the pointer-to-array field of the fat pointer |
1eff5289 EB |
2397 | and record the aliasing relationships if necessary. If this is |
2398 | a packed type implemented specially, then use a ref-all pointer | |
2399 | type since the implementation type may vary between constrained | |
2400 | subtypes and unconstrained base type. */ | |
2401 | if (Present (PAT)) | |
2402 | TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) | |
2403 | = build_pointer_type_for_mode (tem, ptr_mode, true); | |
2404 | else | |
2405 | TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem); | |
e3edbd56 EB |
2406 | if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type)) |
2407 | record_component_aliases (gnu_fat_type); | |
a1ab4c31 | 2408 | |
a1ab4c31 | 2409 | /* If the maximum size doesn't overflow, use it. */ |
86060344 | 2410 | if (gnu_max_size |
4e6602a8 EB |
2411 | && TREE_CODE (gnu_max_size) == INTEGER_CST |
2412 | && !TREE_OVERFLOW (gnu_max_size) | |
683ccd05 EB |
2413 | && compare_tree_int (gnu_max_size, TYPE_ARRAY_SIZE_LIMIT) <= 0) |
2414 | TYPE_ARRAY_MAX_SIZE (tem) = gnu_max_size; | |
a1ab4c31 | 2415 | |
a27aceb9 | 2416 | /* See the above description for the rationale. */ |
74746d49 | 2417 | create_type_decl (create_concat_name (gnat_entity, "XUA"), tem, |
c1a569ef | 2418 | artificial_p, debug_info_p, gnat_entity); |
a27aceb9 EB |
2419 | TYPE_CONTEXT (tem) = gnu_fat_type; |
2420 | TYPE_CONTEXT (TYPE_POINTER_TO (tem)) = gnu_fat_type; | |
a1ab4c31 | 2421 | |
2b45154d EB |
2422 | /* Create the type to be designated by thin pointers: a record type for |
2423 | the array and its template. We used to shift the fields to have the | |
2424 | template at a negative offset, but this was somewhat of a kludge; we | |
2425 | now shift thin pointer values explicitly but only those which have a | |
24bd3c6e | 2426 | TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. |
58d32c72 | 2427 | If the GNAT encodings are used, give it a name. */ |
773392af | 2428 | tree xut_name |
58d32c72 | 2429 | = (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
a27aceb9 EB |
2430 | ? create_concat_name (gnat_name, "XUT") |
2431 | : gnu_entity_name; | |
2432 | obj = build_unc_object_type (gnu_template_type, tem, xut_name, | |
928dfa4b | 2433 | debug_info_p); |
a1ab4c31 | 2434 | |
a27aceb9 EB |
2435 | SET_TYPE_UNCONSTRAINED_ARRAY (obj, gnu_type); |
2436 | TYPE_OBJECT_RECORD_TYPE (gnu_type) = obj; | |
2437 | ||
2438 | /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the | |
2439 | corresponding fat pointer. */ | |
2440 | TREE_TYPE (gnu_type) = gnu_fat_type; | |
2441 | TYPE_POINTER_TO (gnu_type) = gnu_fat_type; | |
2442 | TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type; | |
2443 | SET_TYPE_MODE (gnu_type, BLKmode); | |
2444 | SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (tem)); | |
a1ab4c31 AC |
2445 | } |
2446 | break; | |
2447 | ||
a1ab4c31 AC |
2448 | case E_Array_Subtype: |
2449 | ||
2450 | /* This is the actual data type for array variables. Multidimensional | |
4e6602a8 | 2451 | arrays are implemented as arrays of arrays. Note that arrays which |
7c20033e | 2452 | have sparse enumeration subtypes as index components create sparse |
4e6602a8 EB |
2453 | arrays, which is obviously space inefficient but so much easier to |
2454 | code for now. | |
a1ab4c31 | 2455 | |
4e6602a8 EB |
2456 | Also note that the subtype never refers to the unconstrained array |
2457 | type, which is somewhat at variance with Ada semantics. | |
a1ab4c31 | 2458 | |
4e6602a8 EB |
2459 | First check to see if this is simply a renaming of the array type. |
2460 | If so, the result is the array type. */ | |
a1ab4c31 | 2461 | |
f797c2b7 | 2462 | gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity))); |
a1ab4c31 | 2463 | if (!Is_Constrained (gnat_entity)) |
7c20033e | 2464 | ; |
a1ab4c31 AC |
2465 | else |
2466 | { | |
1eff5289 | 2467 | const Entity_Id PAT = Packed_Array_Impl_Type (gnat_entity); |
4e6602a8 EB |
2468 | Entity_Id gnat_index, gnat_base_index; |
2469 | const bool convention_fortran_p | |
2470 | = (Convention (gnat_entity) == Convention_Fortran); | |
2471 | const int ndim = Number_Dimensions (gnat_entity); | |
a1ab4c31 | 2472 | tree gnu_base_type = gnu_type; |
2bb1fc26 | 2473 | tree *gnu_index_types = XALLOCAVEC (tree, ndim); |
683ccd05 | 2474 | tree gnu_max_size = size_one_node; |
a1ab4c31 | 2475 | bool need_index_type_struct = false; |
4e6602a8 | 2476 | int index; |
a1ab4c31 | 2477 | |
4e6602a8 EB |
2478 | /* First create the GCC type for each index and find out whether |
2479 | special types are needed for debugging information. */ | |
2480 | for (index = (convention_fortran_p ? ndim - 1 : 0), | |
2481 | gnat_index = First_Index (gnat_entity), | |
2482 | gnat_base_index | |
a1ab4c31 | 2483 | = First_Index (Implementation_Base_Type (gnat_entity)); |
278f422c | 2484 | IN_RANGE (index, 0, ndim - 1); |
4e6602a8 EB |
2485 | index += (convention_fortran_p ? - 1 : 1), |
2486 | gnat_index = Next_Index (gnat_index), | |
2487 | gnat_base_index = Next_Index (gnat_base_index)) | |
a1ab4c31 | 2488 | { |
4e6602a8 | 2489 | tree gnu_index_type = get_unpadded_type (Etype (gnat_index)); |
683ccd05 EB |
2490 | tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type); |
2491 | tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type); | |
2492 | tree gnu_index_base_type = get_base_type (gnu_index_type); | |
4e6602a8 EB |
2493 | tree gnu_base_index_type |
2494 | = get_unpadded_type (Etype (gnat_base_index)); | |
683ccd05 EB |
2495 | tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type); |
2496 | tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type); | |
2497 | tree gnu_min, gnu_max, gnu_high; | |
2498 | ||
7c919c12 EB |
2499 | /* We try to create subtypes for discriminants used as bounds |
2500 | that are more restrictive than those declared, by using the | |
683ccd05 EB |
2501 | bounds of the index type of the base array type. This will |
2502 | make it possible to calculate the maximum size of the record | |
2503 | type more conservatively. This may have already been done by | |
2504 | the front-end (Exp_Ch3.Adjust_Discriminants), in which case | |
2505 | there will be a conversion that needs to be removed first. */ | |
2506 | if (CONTAINS_PLACEHOLDER_P (gnu_orig_min) | |
2507 | && TYPE_RM_SIZE (gnu_base_index_type) | |
7c919c12 EB |
2508 | && tree_int_cst_lt (TYPE_RM_SIZE (gnu_base_index_type), |
2509 | TYPE_RM_SIZE (gnu_index_type))) | |
683ccd05 EB |
2510 | { |
2511 | gnu_orig_min = remove_conversions (gnu_orig_min, false); | |
2512 | TREE_TYPE (gnu_orig_min) | |
2513 | = create_extra_subtype (TREE_TYPE (gnu_orig_min), | |
2514 | gnu_base_orig_min, | |
2515 | gnu_base_orig_max); | |
2516 | } | |
2517 | ||
2518 | if (CONTAINS_PLACEHOLDER_P (gnu_orig_max) | |
2519 | && TYPE_RM_SIZE (gnu_base_index_type) | |
7c919c12 EB |
2520 | && tree_int_cst_lt (TYPE_RM_SIZE (gnu_base_index_type), |
2521 | TYPE_RM_SIZE (gnu_index_type))) | |
683ccd05 EB |
2522 | { |
2523 | gnu_orig_max = remove_conversions (gnu_orig_max, false); | |
2524 | TREE_TYPE (gnu_orig_max) | |
2525 | = create_extra_subtype (TREE_TYPE (gnu_orig_max), | |
2526 | gnu_base_orig_min, | |
2527 | gnu_base_orig_max); | |
2528 | } | |
2529 | ||
2530 | /* Update the maximum size of the array in elements. Here we | |
2531 | see if any constraint on the index type of the base type | |
2532 | can be used in the case of self-referential bounds on the | |
2533 | index type of the array type. We look for a non-"infinite" | |
2534 | and non-self-referential bound from any type involved and | |
2535 | handle each bound separately. */ | |
2536 | if (gnu_max_size) | |
2537 | { | |
2538 | if (CONTAINS_PLACEHOLDER_P (gnu_orig_min)) | |
2539 | gnu_min = gnu_base_orig_min; | |
2540 | else | |
2541 | gnu_min = gnu_orig_min; | |
2542 | ||
2543 | if (TREE_CODE (gnu_min) != INTEGER_CST | |
2544 | || TREE_OVERFLOW (gnu_min)) | |
2545 | gnu_min = TYPE_MIN_VALUE (TREE_TYPE (gnu_min)); | |
2546 | ||
2547 | if (CONTAINS_PLACEHOLDER_P (gnu_orig_max)) | |
2548 | gnu_max = gnu_base_orig_max; | |
2549 | else | |
2550 | gnu_max = gnu_orig_max; | |
2551 | ||
2552 | if (TREE_CODE (gnu_max) != INTEGER_CST | |
2553 | || TREE_OVERFLOW (gnu_max)) | |
2554 | gnu_max = TYPE_MAX_VALUE (TREE_TYPE (gnu_max)); | |
2555 | ||
2556 | gnu_max_size | |
2557 | = update_n_elem (gnu_max_size, gnu_min, gnu_max); | |
2558 | } | |
2559 | ||
2560 | /* Convert the bounds to the base type for consistency below. */ | |
2561 | gnu_index_base_type = maybe_character_type (gnu_index_base_type); | |
2562 | gnu_orig_min = convert (gnu_index_base_type, gnu_orig_min); | |
2563 | gnu_orig_max = convert (gnu_index_base_type, gnu_orig_max); | |
2564 | ||
2565 | gnu_min = convert (sizetype, gnu_orig_min); | |
2566 | gnu_max = convert (sizetype, gnu_orig_max); | |
4e6602a8 EB |
2567 | |
2568 | /* See if the base array type is already flat. If it is, we | |
2569 | are probably compiling an ACATS test but it will cause the | |
2570 | code below to malfunction if we don't handle it specially. */ | |
2571 | if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST | |
2572 | && TREE_CODE (gnu_base_orig_max) == INTEGER_CST | |
2573 | && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min)) | |
a1ab4c31 | 2574 | { |
4e6602a8 EB |
2575 | gnu_min = size_one_node; |
2576 | gnu_max = size_zero_node; | |
feec4372 | 2577 | gnu_high = gnu_max; |
a1ab4c31 AC |
2578 | } |
2579 | ||
4e6602a8 EB |
2580 | /* Similarly, if one of the values overflows in sizetype and the |
2581 | range is null, use 1..0 for the sizetype bounds. */ | |
728936bb | 2582 | else if (TREE_CODE (gnu_min) == INTEGER_CST |
a1ab4c31 AC |
2583 | && TREE_CODE (gnu_max) == INTEGER_CST |
2584 | && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max)) | |
4e6602a8 | 2585 | && tree_int_cst_lt (gnu_orig_max, gnu_orig_min)) |
feec4372 EB |
2586 | { |
2587 | gnu_min = size_one_node; | |
2588 | gnu_max = size_zero_node; | |
2589 | gnu_high = gnu_max; | |
2590 | } | |
a1ab4c31 | 2591 | |
4e6602a8 EB |
2592 | /* If the minimum and maximum values both overflow in sizetype, |
2593 | but the difference in the original type does not overflow in | |
2594 | sizetype, ignore the overflow indication. */ | |
728936bb | 2595 | else if (TREE_CODE (gnu_min) == INTEGER_CST |
4e6602a8 EB |
2596 | && TREE_CODE (gnu_max) == INTEGER_CST |
2597 | && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max) | |
2598 | && !TREE_OVERFLOW | |
2599 | (convert (sizetype, | |
683ccd05 EB |
2600 | fold_build2 (MINUS_EXPR, |
2601 | gnu_index_base_type, | |
4e6602a8 EB |
2602 | gnu_orig_max, |
2603 | gnu_orig_min)))) | |
feec4372 | 2604 | { |
4e6602a8 EB |
2605 | TREE_OVERFLOW (gnu_min) = 0; |
2606 | TREE_OVERFLOW (gnu_max) = 0; | |
feec4372 EB |
2607 | gnu_high = gnu_max; |
2608 | } | |
2609 | ||
f45f9664 EB |
2610 | /* Compute the size of this dimension in the general case. We |
2611 | need to provide GCC with an upper bound to use but have to | |
2612 | deal with the "superflat" case. There are three ways to do | |
2613 | this. If we can prove that the array can never be superflat, | |
2614 | we can just use the high bound of the index type. */ | |
728936bb | 2615 | else if ((Nkind (gnat_index) == N_Range |
3ccd5d71 | 2616 | && range_cannot_be_superflat (gnat_index)) |
53f3f4e3 | 2617 | /* Bit-Packed Array Impl. Types are never superflat. */ |
1a4cb227 | 2618 | || (Is_Packed_Array_Impl_Type (gnat_entity) |
f9d7d7c1 EB |
2619 | && Is_Bit_Packed_Array |
2620 | (Original_Array_Type (gnat_entity)))) | |
f45f9664 EB |
2621 | gnu_high = gnu_max; |
2622 | ||
728936bb EB |
2623 | /* Otherwise, if the high bound is constant but the low bound is |
2624 | not, we use the expression (hb >= lb) ? lb : hb + 1 for the | |
2625 | lower bound. Note that the comparison must be done in the | |
2626 | original type to avoid any overflow during the conversion. */ | |
2627 | else if (TREE_CODE (gnu_max) == INTEGER_CST | |
2628 | && TREE_CODE (gnu_min) != INTEGER_CST) | |
feec4372 | 2629 | { |
728936bb EB |
2630 | gnu_high = gnu_max; |
2631 | gnu_min | |
2632 | = build_cond_expr (sizetype, | |
2633 | build_binary_op (GE_EXPR, | |
2634 | boolean_type_node, | |
2635 | gnu_orig_max, | |
2636 | gnu_orig_min), | |
2637 | gnu_min, | |
dcbac1a4 EB |
2638 | int_const_binop (PLUS_EXPR, gnu_max, |
2639 | size_one_node)); | |
feec4372 | 2640 | } |
a1ab4c31 | 2641 | |
728936bb | 2642 | /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound |
683ccd05 EB |
2643 | in all the other cases. Note that we use int_const_binop for |
2644 | the shift by 1 if the bound is constant to avoid any unwanted | |
2645 | overflow. */ | |
728936bb EB |
2646 | else |
2647 | gnu_high | |
2648 | = build_cond_expr (sizetype, | |
2649 | build_binary_op (GE_EXPR, | |
2650 | boolean_type_node, | |
2651 | gnu_orig_max, | |
2652 | gnu_orig_min), | |
2653 | gnu_max, | |
dcbac1a4 EB |
2654 | TREE_CODE (gnu_min) == INTEGER_CST |
2655 | ? int_const_binop (MINUS_EXPR, gnu_min, | |
2656 | size_one_node) | |
2657 | : size_binop (MINUS_EXPR, gnu_min, | |
2658 | size_one_node)); | |
728936bb | 2659 | |
b6c056fe EB |
2660 | /* Reuse the index type for the range type. Then make an index |
2661 | type with the size range in sizetype. */ | |
4e6602a8 EB |
2662 | gnu_index_types[index] |
2663 | = create_index_type (gnu_min, gnu_high, gnu_index_type, | |
a1ab4c31 AC |
2664 | gnat_entity); |
2665 | ||
4e6602a8 EB |
2666 | /* We need special types for debugging information to point to |
2667 | the index types if they have variable bounds, are not integer | |
24bd3c6e PMR |
2668 | types, are biased or are wider than sizetype. These are GNAT |
2669 | encodings, so we have to include them only when all encodings | |
2670 | are requested. */ | |
7c775aca EB |
2671 | if ((TREE_CODE (gnu_orig_min) != INTEGER_CST |
2672 | || TREE_CODE (gnu_orig_max) != INTEGER_CST | |
2673 | || TREE_CODE (gnu_index_type) != INTEGER_TYPE | |
2674 | || (TREE_TYPE (gnu_index_type) | |
2675 | && TREE_CODE (TREE_TYPE (gnu_index_type)) | |
2676 | != INTEGER_TYPE) | |
2677 | || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)) | |
58d32c72 | 2678 | && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
a1ab4c31 AC |
2679 | need_index_type_struct = true; |
2680 | } | |
2681 | ||
2682 | /* Then flatten: create the array of arrays. For an array type | |
2683 | used to implement a packed array, get the component type from | |
2684 | the original array type since the representation clauses that | |
2685 | can affect it are on the latter. */ | |
1a4cb227 | 2686 | if (Is_Packed_Array_Impl_Type (gnat_entity) |
a1ab4c31 AC |
2687 | && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity))) |
2688 | { | |
2689 | gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity)); | |
4e6602a8 | 2690 | for (index = ndim - 1; index >= 0; index--) |
a1ab4c31 AC |
2691 | gnu_type = TREE_TYPE (gnu_type); |
2692 | ||
2693 | /* One of the above calls might have caused us to be elaborated, | |
2694 | so don't blow up if so. */ | |
2695 | if (present_gnu_tree (gnat_entity)) | |
2696 | { | |
2697 | maybe_present = true; | |
2698 | break; | |
2699 | } | |
2700 | } | |
2701 | else | |
2702 | { | |
2cac6017 EB |
2703 | gnu_type = gnat_to_gnu_component_type (gnat_entity, definition, |
2704 | debug_info_p); | |
a1ab4c31 AC |
2705 | |
2706 | /* One of the above calls might have caused us to be elaborated, | |
2707 | so don't blow up if so. */ | |
2708 | if (present_gnu_tree (gnat_entity)) | |
2709 | { | |
2710 | maybe_present = true; | |
2711 | break; | |
2712 | } | |
a1ab4c31 AC |
2713 | } |
2714 | ||
683ccd05 | 2715 | /* Compute the maximum size of the array in units. */ |
4e6602a8 | 2716 | if (gnu_max_size) |
683ccd05 EB |
2717 | gnu_max_size |
2718 | = size_binop (MULT_EXPR, gnu_max_size, TYPE_SIZE_UNIT (gnu_type)); | |
a1ab4c31 | 2719 | |
4e6602a8 EB |
2720 | /* Now build the array type. */ |
2721 | for (index = ndim - 1; index >= 0; index --) | |
a1ab4c31 | 2722 | { |
523e82a7 EB |
2723 | gnu_type = build_nonshared_array_type (gnu_type, |
2724 | gnu_index_types[index]); | |
a1ab4c31 | 2725 | TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0); |
d42b7559 EB |
2726 | TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p; |
2727 | if (index == ndim - 1 && Reverse_Storage_Order (gnat_entity)) | |
2728 | set_reverse_storage_order_on_array_type (gnu_type); | |
d8e94f79 | 2729 | if (array_type_has_nonaliased_component (gnu_type, gnat_entity)) |
d42b7559 | 2730 | set_nonaliased_component_on_array_type (gnu_type); |
bb1ec477 EB |
2731 | |
2732 | /* Kludge to remove the TREE_OVERFLOW flag for the sake of LTO | |
2733 | on maximally-sized array types designed by access types. */ | |
2734 | if (integer_zerop (TYPE_SIZE (gnu_type)) | |
2735 | && TREE_OVERFLOW (TYPE_SIZE (gnu_type)) | |
2736 | && Is_Itype (gnat_entity) | |
2737 | && (gnat_temp = Associated_Node_For_Itype (gnat_entity)) | |
2738 | && IN (Nkind (gnat_temp), N_Declaration) | |
2739 | && Is_Access_Type (Defining_Entity (gnat_temp)) | |
2740 | && Is_Entity_Name (First_Index (gnat_entity)) | |
2741 | && UI_To_Int (RM_Size (Entity (First_Index (gnat_entity)))) | |
2742 | == BITS_PER_WORD) | |
2743 | { | |
2744 | TYPE_SIZE (gnu_type) = bitsize_zero_node; | |
2745 | TYPE_SIZE_UNIT (gnu_type) = size_zero_node; | |
2746 | } | |
a1ab4c31 AC |
2747 | } |
2748 | ||
10069d53 | 2749 | /* Attach the TYPE_STUB_DECL in case we have a parallel type. */ |
4fd78fe6 EB |
2750 | TYPE_STUB_DECL (gnu_type) |
2751 | = create_type_stub_decl (gnu_entity_name, gnu_type); | |
10069d53 | 2752 | |
b0ad2d78 | 2753 | /* If this is a multi-dimensional array and we are at global level, |
4e6602a8 | 2754 | we need to make a variable corresponding to the stride of the |
a1ab4c31 | 2755 | inner dimensions. */ |
b0ad2d78 | 2756 | if (ndim > 1 && global_bindings_p ()) |
a1ab4c31 | 2757 | { |
a1ab4c31 AC |
2758 | tree gnu_arr_type; |
2759 | ||
bf44701f | 2760 | for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1; |
a1ab4c31 | 2761 | TREE_CODE (gnu_arr_type) == ARRAY_TYPE; |
bf44701f | 2762 | gnu_arr_type = TREE_TYPE (gnu_arr_type), index++) |
a1ab4c31 AC |
2763 | { |
2764 | tree eltype = TREE_TYPE (gnu_arr_type); | |
bf44701f | 2765 | char stride_name[32]; |
a1ab4c31 | 2766 | |
bf44701f | 2767 | sprintf (stride_name, "ST%d", index); |
a1ab4c31 | 2768 | TYPE_SIZE (gnu_arr_type) |
a531043b | 2769 | = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type), |
bf44701f | 2770 | gnat_entity, stride_name, |
a531043b | 2771 | definition, false); |
a1ab4c31 AC |
2772 | |
2773 | /* ??? For now, store the size as a multiple of the | |
2774 | alignment of the element type in bytes so that we | |
2775 | can see the alignment from the tree. */ | |
bf44701f | 2776 | sprintf (stride_name, "ST%d_A_UNIT", index); |
a1ab4c31 | 2777 | TYPE_SIZE_UNIT (gnu_arr_type) |
da01bfee | 2778 | = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type), |
bf44701f | 2779 | gnat_entity, stride_name, |
da01bfee EB |
2780 | definition, false, |
2781 | TYPE_ALIGN (eltype)); | |
a1ab4c31 AC |
2782 | |
2783 | /* ??? create_type_decl is not invoked on the inner types so | |
2784 | the MULT_EXPR node built above will never be marked. */ | |
3f13dd77 | 2785 | MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type)); |
a1ab4c31 AC |
2786 | } |
2787 | } | |
2788 | ||
1e3cabd4 EB |
2789 | /* Set the TYPE_PACKED flag on packed array types and also on their |
2790 | implementation types, so that the DWARF back-end can output the | |
2791 | appropriate description for them. */ | |
2792 | TYPE_PACKED (gnu_type) | |
2793 | = (Is_Packed (gnat_entity) | |
2794 | || Is_Packed_Array_Impl_Type (gnat_entity)); | |
2795 | ||
2796 | TYPE_BIT_PACKED_ARRAY_TYPE_P (gnu_type) | |
2797 | = (Is_Packed_Array_Impl_Type (gnat_entity) | |
2798 | && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity))); | |
2799 | ||
2800 | /* If the maximum size doesn't overflow, use it. */ | |
2801 | if (gnu_max_size | |
2802 | && TREE_CODE (gnu_max_size) == INTEGER_CST | |
2803 | && !TREE_OVERFLOW (gnu_max_size) | |
2804 | && compare_tree_int (gnu_max_size, TYPE_ARRAY_SIZE_LIMIT) <= 0) | |
2805 | TYPE_ARRAY_MAX_SIZE (gnu_type) = gnu_max_size; | |
2806 | ||
4fd78fe6 EB |
2807 | /* If we need to write out a record type giving the names of the |
2808 | bounds for debugging purposes, do it now and make the record | |
2809 | type a parallel type. This is not needed for a packed array | |
2810 | since the bounds are conveyed by the original array type. */ | |
2811 | if (need_index_type_struct | |
2812 | && debug_info_p | |
1a4cb227 | 2813 | && !Is_Packed_Array_Impl_Type (gnat_entity)) |
a1ab4c31 | 2814 | { |
10069d53 | 2815 | tree gnu_bound_rec = make_node (RECORD_TYPE); |
a1ab4c31 AC |
2816 | tree gnu_field_list = NULL_TREE; |
2817 | tree gnu_field; | |
2818 | ||
10069d53 | 2819 | TYPE_NAME (gnu_bound_rec) |
a1ab4c31 AC |
2820 | = create_concat_name (gnat_entity, "XA"); |
2821 | ||
4e6602a8 | 2822 | for (index = ndim - 1; index >= 0; index--) |
a1ab4c31 | 2823 | { |
4e6602a8 | 2824 | tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]); |
9dba4b55 | 2825 | tree gnu_index_name = TYPE_IDENTIFIER (gnu_index); |
a1ab4c31 | 2826 | |
4fd78fe6 EB |
2827 | /* Make sure to reference the types themselves, and not just |
2828 | their names, as the debugger may fall back on them. */ | |
10069d53 | 2829 | gnu_field = create_field_decl (gnu_index_name, gnu_index, |
da01bfee EB |
2830 | gnu_bound_rec, NULL_TREE, |
2831 | NULL_TREE, 0, 0); | |
910ad8de | 2832 | DECL_CHAIN (gnu_field) = gnu_field_list; |
a1ab4c31 AC |
2833 | gnu_field_list = gnu_field; |
2834 | } | |
2835 | ||
032d1b71 | 2836 | finish_record_type (gnu_bound_rec, gnu_field_list, 0, true); |
a5695aa2 | 2837 | add_parallel_type (gnu_type, gnu_bound_rec); |
a1ab4c31 AC |
2838 | } |
2839 | ||
583eb0c9 | 2840 | /* If this is a packed array type, make the original array type a |
1e3cabd4 EB |
2841 | parallel/debug type. Otherwise, if GNAT encodings are used, do |
2842 | it for the base array type if it is not artificial to make sure | |
2843 | that it is kept in the debug info. */ | |
583eb0c9 EB |
2844 | if (debug_info_p) |
2845 | { | |
1eb58520 | 2846 | if (Is_Packed_Array_Impl_Type (gnat_entity)) |
1e3cabd4 EB |
2847 | { |
2848 | tree gnu_name | |
2849 | = associate_original_type_to_packed_array (gnu_type, | |
2850 | gnat_entity); | |
2851 | if (gnu_name) | |
2852 | gnu_entity_name = gnu_name; | |
2853 | } | |
2854 | ||
58d32c72 | 2855 | else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
583eb0c9 EB |
2856 | { |
2857 | tree gnu_base_decl | |
afc737f0 EB |
2858 | = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, |
2859 | false); | |
1e3cabd4 EB |
2860 | |
2861 | if (!DECL_ARTIFICIAL (gnu_base_decl)) | |
a5695aa2 | 2862 | add_parallel_type (gnu_type, |
583eb0c9 EB |
2863 | TREE_TYPE (TREE_TYPE (gnu_base_decl))); |
2864 | } | |
2865 | } | |
4fd78fe6 | 2866 | |
a1ab4c31 AC |
2867 | /* Set our alias set to that of our base type. This gives all |
2868 | array subtypes the same alias set. */ | |
794511d2 | 2869 | relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY); |
a1ab4c31 | 2870 | |
21afc4fa EB |
2871 | /* If this is a packed type implemented specially, then replace our |
2872 | type with the implementation type. */ | |
1eff5289 | 2873 | if (Present (PAT)) |
a1ab4c31 | 2874 | { |
7c20033e EB |
2875 | /* First finish the type we had been making so that we output |
2876 | debugging information for it. */ | |
74746d49 | 2877 | process_attributes (&gnu_type, &attr_list, false, gnat_entity); |
7c20033e | 2878 | if (Treat_As_Volatile (gnat_entity)) |
f797c2b7 EB |
2879 | { |
2880 | const int quals | |
2881 | = TYPE_QUAL_VOLATILE | |
b120ca61 | 2882 | | (Is_Full_Access (gnat_entity) ? TYPE_QUAL_ATOMIC : 0); |
f797c2b7 EB |
2883 | gnu_type = change_qualified_type (gnu_type, quals); |
2884 | } | |
7c20033e EB |
2885 | /* Make it artificial only if the base type was artificial too. |
2886 | That's sort of "morally" true and will make it possible for | |
2887 | the debugger to look it up by name in DWARF, which is needed | |
2888 | in order to decode the packed array type. */ | |
21afc4fa | 2889 | tree gnu_tmp_decl |
74746d49 | 2890 | = create_type_decl (gnu_entity_name, gnu_type, |
7c20033e | 2891 | !Comes_From_Source (Etype (gnat_entity)) |
c1a569ef EB |
2892 | && artificial_p, debug_info_p, |
2893 | gnat_entity); | |
7c20033e EB |
2894 | /* Save it as our equivalent in case the call below elaborates |
2895 | this type again. */ | |
21afc4fa | 2896 | save_gnu_tree (gnat_entity, gnu_tmp_decl, false); |
7c20033e | 2897 | |
1eff5289 | 2898 | gnu_type = gnat_to_gnu_type (PAT); |
7c20033e EB |
2899 | save_gnu_tree (gnat_entity, NULL_TREE, false); |
2900 | ||
21afc4fa | 2901 | /* Set the ___XP suffix for GNAT encodings. */ |
58d32c72 | 2902 | if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
21afc4fa EB |
2903 | gnu_entity_name = DECL_NAME (TYPE_NAME (gnu_type)); |
2904 | ||
2905 | tree gnu_inner = gnu_type; | |
7c20033e EB |
2906 | while (TREE_CODE (gnu_inner) == RECORD_TYPE |
2907 | && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner) | |
315cff15 | 2908 | || TYPE_PADDING_P (gnu_inner))) |
7c20033e EB |
2909 | gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner)); |
2910 | ||
2911 | /* We need to attach the index type to the type we just made so | |
2912 | that the actual bounds can later be put into a template. */ | |
2913 | if ((TREE_CODE (gnu_inner) == ARRAY_TYPE | |
2914 | && !TYPE_ACTUAL_BOUNDS (gnu_inner)) | |
2915 | || (TREE_CODE (gnu_inner) == INTEGER_TYPE | |
2916 | && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner))) | |
a1ab4c31 | 2917 | { |
7c20033e | 2918 | if (TREE_CODE (gnu_inner) == INTEGER_TYPE) |
a1ab4c31 | 2919 | { |
7c20033e EB |
2920 | /* The TYPE_ACTUAL_BOUNDS field is overloaded with the |
2921 | TYPE_MODULUS for modular types so we make an extra | |
2922 | subtype if necessary. */ | |
2923 | if (TYPE_MODULAR_P (gnu_inner)) | |
683ccd05 EB |
2924 | gnu_inner |
2925 | = create_extra_subtype (gnu_inner, | |
2926 | TYPE_MIN_VALUE (gnu_inner), | |
2927 | TYPE_MAX_VALUE (gnu_inner)); | |
7c20033e EB |
2928 | |
2929 | TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1; | |
26383c64 | 2930 | |
7c20033e | 2931 | /* Check for other cases of overloading. */ |
9abe8b74 | 2932 | gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner)); |
7c20033e | 2933 | } |
a1ab4c31 | 2934 | |
21afc4fa | 2935 | for (Entity_Id gnat_index = First_Index (gnat_entity); |
7c20033e EB |
2936 | Present (gnat_index); |
2937 | gnat_index = Next_Index (gnat_index)) | |
2938 | SET_TYPE_ACTUAL_BOUNDS | |
2939 | (gnu_inner, | |
2940 | tree_cons (NULL_TREE, | |
2941 | get_unpadded_type (Etype (gnat_index)), | |
2942 | TYPE_ACTUAL_BOUNDS (gnu_inner))); | |
2943 | ||
2944 | if (Convention (gnat_entity) != Convention_Fortran) | |
2945 | SET_TYPE_ACTUAL_BOUNDS | |
2946 | (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner))); | |
2947 | ||
2948 | if (TREE_CODE (gnu_type) == RECORD_TYPE | |
2949 | && TYPE_JUSTIFIED_MODULAR_P (gnu_type)) | |
2950 | TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner; | |
2951 | } | |
a1ab4c31 | 2952 | } |
7c20033e | 2953 | } |
a1ab4c31 AC |
2954 | break; |
2955 | ||
2956 | case E_String_Literal_Subtype: | |
2ddc34ba | 2957 | /* Create the type for a string literal. */ |
a1ab4c31 AC |
2958 | { |
2959 | Entity_Id gnat_full_type | |
7ed9919d | 2960 | = (Is_Private_Type (Etype (gnat_entity)) |
a1ab4c31 AC |
2961 | && Present (Full_View (Etype (gnat_entity))) |
2962 | ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity)); | |
2963 | tree gnu_string_type = get_unpadded_type (gnat_full_type); | |
2964 | tree gnu_string_array_type | |
2965 | = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type)))); | |
2966 | tree gnu_string_index_type | |
2967 | = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE | |
2968 | (TYPE_DOMAIN (gnu_string_array_type)))); | |
2969 | tree gnu_lower_bound | |
2970 | = convert (gnu_string_index_type, | |
2971 | gnat_to_gnu (String_Literal_Low_Bound (gnat_entity))); | |
f54ee980 EB |
2972 | tree gnu_length |
2973 | = UI_To_gnu (String_Literal_Length (gnat_entity), | |
2974 | gnu_string_index_type); | |
a1ab4c31 AC |
2975 | tree gnu_upper_bound |
2976 | = build_binary_op (PLUS_EXPR, gnu_string_index_type, | |
2977 | gnu_lower_bound, | |
f54ee980 | 2978 | int_const_binop (MINUS_EXPR, gnu_length, |
8b9aec86 RS |
2979 | convert (gnu_string_index_type, |
2980 | integer_one_node))); | |
a1ab4c31 | 2981 | tree gnu_index_type |
c1abd261 EB |
2982 | = create_index_type (convert (sizetype, gnu_lower_bound), |
2983 | convert (sizetype, gnu_upper_bound), | |
84fb43a1 EB |
2984 | create_range_type (gnu_string_index_type, |
2985 | gnu_lower_bound, | |
2986 | gnu_upper_bound), | |
c1abd261 | 2987 | gnat_entity); |
a1ab4c31 AC |
2988 | |
2989 | gnu_type | |
523e82a7 EB |
2990 | = build_nonshared_array_type (gnat_to_gnu_type |
2991 | (Component_Type (gnat_entity)), | |
2992 | gnu_index_type); | |
d8e94f79 | 2993 | if (array_type_has_nonaliased_component (gnu_type, gnat_entity)) |
d42b7559 | 2994 | set_nonaliased_component_on_array_type (gnu_type); |
794511d2 | 2995 | relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY); |
a1ab4c31 AC |
2996 | } |
2997 | break; | |
2998 | ||
2999 | /* Record Types and Subtypes | |
3000 | ||
a1ab4c31 AC |
3001 | A record type definition is transformed into the equivalent of a C |
3002 | struct definition. The fields that are the discriminants which are | |
3003 | found in the Full_Type_Declaration node and the elements of the | |
3004 | Component_List found in the Record_Type_Definition node. The | |
3005 | Component_List can be a recursive structure since each Variant of | |
3006 | the Variant_Part of the Component_List has a Component_List. | |
3007 | ||
3008 | Processing of a record type definition comprises starting the list of | |
3009 | field declarations here from the discriminants and the calling the | |
3010 | function components_to_record to add the rest of the fields from the | |
2ddc34ba | 3011 | component list and return the gnu type node. The function |
a1ab4c31 AC |
3012 | components_to_record will call itself recursively as it traverses |
3013 | the tree. */ | |
3014 | ||
3015 | case E_Record_Type: | |
87668878 EB |
3016 | { |
3017 | Node_Id record_definition = Type_Definition (gnat_decl); | |
a1ab4c31 | 3018 | |
87668878 EB |
3019 | if (Has_Complex_Representation (gnat_entity)) |
3020 | { | |
3021 | const Node_Id first_component | |
3022 | = First (Component_Items (Component_List (record_definition))); | |
3023 | tree gnu_component_type | |
3024 | = get_unpadded_type (Etype (Defining_Entity (first_component))); | |
3025 | gnu_type = build_complex_type (gnu_component_type); | |
3026 | break; | |
3027 | } | |
a1ab4c31 | 3028 | |
908ba941 | 3029 | Node_Id gnat_constr; |
05dbb83f | 3030 | Entity_Id gnat_field, gnat_parent_type; |
908ba941 EB |
3031 | tree gnu_field, gnu_field_list = NULL_TREE; |
3032 | tree gnu_get_parent; | |
a1ab4c31 | 3033 | /* Set PACKED in keeping with gnat_to_gnu_field. */ |
908ba941 | 3034 | const int packed |
a1ab4c31 AC |
3035 | = Is_Packed (gnat_entity) |
3036 | ? 1 | |
3037 | : Component_Alignment (gnat_entity) == Calign_Storage_Unit | |
3038 | ? -1 | |
14ecca2e EB |
3039 | : 0; |
3040 | const bool has_align = Known_Alignment (gnat_entity); | |
908ba941 | 3041 | const bool has_discr = Has_Discriminants (gnat_entity); |
908ba941 | 3042 | const bool is_extension |
a1ab4c31 AC |
3043 | = (Is_Tagged_Type (gnat_entity) |
3044 | && Nkind (record_definition) == N_Derived_Type_Definition); | |
0c2837b5 EB |
3045 | const bool has_rep |
3046 | = is_extension | |
3047 | ? Has_Record_Rep_Clause (gnat_entity) | |
3048 | : Has_Specified_Layout (gnat_entity); | |
908ba941 EB |
3049 | const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity); |
3050 | bool all_rep = has_rep; | |
a1ab4c31 AC |
3051 | |
3052 | /* See if all fields have a rep clause. Stop when we find one | |
3053 | that doesn't. */ | |
8cd28148 EB |
3054 | if (all_rep) |
3055 | for (gnat_field = First_Entity (gnat_entity); | |
3056 | Present (gnat_field); | |
3057 | gnat_field = Next_Entity (gnat_field)) | |
3058 | if ((Ekind (gnat_field) == E_Component | |
3059 | || Ekind (gnat_field) == E_Discriminant) | |
3060 | && No (Component_Clause (gnat_field))) | |
3061 | { | |
3062 | all_rep = false; | |
3063 | break; | |
3064 | } | |
a1ab4c31 AC |
3065 | |
3066 | /* If this is a record extension, go a level further to find the | |
3067 | record definition. Also, verify we have a Parent_Subtype. */ | |
3068 | if (is_extension) | |
3069 | { | |
3070 | if (!type_annotate_only | |
3071 | || Present (Record_Extension_Part (record_definition))) | |
3072 | record_definition = Record_Extension_Part (record_definition); | |
3073 | ||
815b5368 EB |
3074 | gcc_assert (Present (Parent_Subtype (gnat_entity)) |
3075 | || type_annotate_only); | |
a1ab4c31 AC |
3076 | } |
3077 | ||
fc130ab5 | 3078 | /* Make a node for the record type. */ |
a1ab4c31 | 3079 | gnu_type = make_node (tree_code_for_record_type (gnat_entity)); |
0fb2335d | 3080 | TYPE_NAME (gnu_type) = gnu_entity_name; |
14ecca2e | 3081 | TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep; |
ee45a32d EB |
3082 | TYPE_REVERSE_STORAGE_ORDER (gnu_type) |
3083 | = Reverse_Storage_Order (gnat_entity); | |
fc130ab5 EB |
3084 | |
3085 | /* If the record type has discriminants, pointers to it may also point | |
3086 | to constrained subtypes of it, so mark it as may_alias for LTO. */ | |
3087 | if (has_discr) | |
3088 | prepend_one_attribute | |
3089 | (&attr_list, ATTR_MACHINE_ATTRIBUTE, | |
3090 | get_identifier ("may_alias"), NULL_TREE, | |
3091 | gnat_entity); | |
3092 | ||
74746d49 | 3093 | process_attributes (&gnu_type, &attr_list, true, gnat_entity); |
a1ab4c31 | 3094 | |
fc130ab5 | 3095 | /* If we are not defining it, suppress expanding incomplete types. */ |
a1ab4c31 | 3096 | if (!definition) |
8cd28148 EB |
3097 | { |
3098 | defer_incomplete_level++; | |
3099 | this_deferred = true; | |
3100 | } | |
a1ab4c31 | 3101 | |
14ecca2e EB |
3102 | /* If both a size and rep clause were specified, put the size on |
3103 | the record type now so that it can get the proper layout. */ | |
fc893455 AC |
3104 | if (has_rep && Known_RM_Size (gnat_entity)) |
3105 | TYPE_SIZE (gnu_type) | |
3106 | = UI_To_gnu (RM_Size (gnat_entity), bitsizetype); | |
a1ab4c31 | 3107 | |
14ecca2e EB |
3108 | /* Always set the alignment on the record type here so that it can |
3109 | get the proper layout. */ | |
3110 | if (has_align) | |
fe37c7af MM |
3111 | SET_TYPE_ALIGN (gnu_type, |
3112 | validate_alignment (Alignment (gnat_entity), | |
3113 | gnat_entity, 0)); | |
14ecca2e | 3114 | else |
a1ab4c31 | 3115 | { |
fe37c7af | 3116 | SET_TYPE_ALIGN (gnu_type, 0); |
14ecca2e | 3117 | |
8623afc4 EB |
3118 | /* If a type needs strict alignment, then its type size will also |
3119 | be the RM size (see below). Cap the alignment if needed, lest | |
3120 | it may cause this type size to become too large. */ | |
14ecca2e EB |
3121 | if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity)) |
3122 | { | |
3123 | unsigned int max_size = UI_To_Int (RM_Size (gnat_entity)); | |
3124 | unsigned int max_align = max_size & -max_size; | |
3125 | if (max_align < BIGGEST_ALIGNMENT) | |
3126 | TYPE_MAX_ALIGN (gnu_type) = max_align; | |
3127 | } | |
3a4425fd EB |
3128 | |
3129 | /* Similarly if an Object_Size clause has been specified. */ | |
3130 | else if (Known_Esize (gnat_entity)) | |
3131 | { | |
3132 | unsigned int max_size = UI_To_Int (Esize (gnat_entity)); | |
3133 | unsigned int max_align = max_size & -max_size; | |
3134 | if (max_align < BIGGEST_ALIGNMENT) | |
3135 | TYPE_MAX_ALIGN (gnu_type) = max_align; | |
3136 | } | |
a1ab4c31 | 3137 | } |
a1ab4c31 AC |
3138 | |
3139 | /* If we have a Parent_Subtype, make a field for the parent. If | |
3140 | this record has rep clauses, force the position to zero. */ | |
3141 | if (Present (Parent_Subtype (gnat_entity))) | |
3142 | { | |
3143 | Entity_Id gnat_parent = Parent_Subtype (gnat_entity); | |
08cb7d42 | 3144 | tree gnu_dummy_parent_type = make_node (RECORD_TYPE); |
a1ab4c31 | 3145 | tree gnu_parent; |
04bc3c93 | 3146 | int parent_packed = 0; |
a1ab4c31 AC |
3147 | |
3148 | /* A major complexity here is that the parent subtype will | |
a8c4c75a EB |
3149 | reference our discriminants in its Stored_Constraint list. |
3150 | But those must reference the parent component of this record | |
3151 | which is precisely of the parent subtype we have not built yet! | |
a1ab4c31 AC |
3152 | To break the circle we first build a dummy COMPONENT_REF which |
3153 | represents the "get to the parent" operation and initialize | |
3154 | each of those discriminants to a COMPONENT_REF of the above | |
3155 | dummy parent referencing the corresponding discriminant of the | |
3156 | base type of the parent subtype. */ | |
08cb7d42 | 3157 | gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type, |
a1ab4c31 | 3158 | build0 (PLACEHOLDER_EXPR, gnu_type), |
c172df28 AH |
3159 | build_decl (input_location, |
3160 | FIELD_DECL, NULL_TREE, | |
08cb7d42 | 3161 | gnu_dummy_parent_type), |
a1ab4c31 AC |
3162 | NULL_TREE); |
3163 | ||
c244bf8f | 3164 | if (has_discr) |
a1ab4c31 AC |
3165 | for (gnat_field = First_Stored_Discriminant (gnat_entity); |
3166 | Present (gnat_field); | |
3167 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
3168 | if (Present (Corresponding_Discriminant (gnat_field))) | |
e99c3ccc EB |
3169 | { |
3170 | tree gnu_field | |
3171 | = gnat_to_gnu_field_decl (Corresponding_Discriminant | |
3172 | (gnat_field)); | |
3173 | save_gnu_tree | |
3174 | (gnat_field, | |
3175 | build3 (COMPONENT_REF, TREE_TYPE (gnu_field), | |
3176 | gnu_get_parent, gnu_field, NULL_TREE), | |
3177 | true); | |
3178 | } | |
a1ab4c31 | 3179 | |
77022fa8 EB |
3180 | /* Then we build the parent subtype. If it has discriminants but |
3181 | the type itself has unknown discriminants, this means that it | |
3182 | doesn't contain information about how the discriminants are | |
3183 | derived from those of the ancestor type, so it cannot be used | |
3184 | directly. Instead it is built by cloning the parent subtype | |
3185 | of the underlying record view of the type, for which the above | |
3186 | derivation of discriminants has been made explicit. */ | |
3187 | if (Has_Discriminants (gnat_parent) | |
3188 | && Has_Unknown_Discriminants (gnat_entity)) | |
3189 | { | |
3190 | Entity_Id gnat_uview = Underlying_Record_View (gnat_entity); | |
3191 | ||
3192 | /* If we are defining the type, the underlying record | |
3193 | view must already have been elaborated at this point. | |
3194 | Otherwise do it now as its parent subtype cannot be | |
3195 | technically elaborated on its own. */ | |
3196 | if (definition) | |
3197 | gcc_assert (present_gnu_tree (gnat_uview)); | |
3198 | else | |
afc737f0 | 3199 | gnat_to_gnu_entity (gnat_uview, NULL_TREE, false); |
77022fa8 EB |
3200 | |
3201 | gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview)); | |
3202 | ||
3203 | /* Substitute the "get to the parent" of the type for that | |
3204 | of its underlying record view in the cloned type. */ | |
3205 | for (gnat_field = First_Stored_Discriminant (gnat_uview); | |
3206 | Present (gnat_field); | |
3207 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
3208 | if (Present (Corresponding_Discriminant (gnat_field))) | |
3209 | { | |
c6bd4220 | 3210 | tree gnu_field = gnat_to_gnu_field_decl (gnat_field); |
77022fa8 EB |
3211 | tree gnu_ref |
3212 | = build3 (COMPONENT_REF, TREE_TYPE (gnu_field), | |
3213 | gnu_get_parent, gnu_field, NULL_TREE); | |
3214 | gnu_parent | |
3215 | = substitute_in_type (gnu_parent, gnu_field, gnu_ref); | |
3216 | } | |
3217 | } | |
3218 | else | |
3219 | gnu_parent = gnat_to_gnu_type (gnat_parent); | |
a1ab4c31 | 3220 | |
8c41a1c8 EB |
3221 | /* The parent field needs strict alignment so, if it is to |
3222 | be created with a component clause below, then we need | |
3223 | to apply the same adjustment as in gnat_to_gnu_field. */ | |
3224 | if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent)) | |
04bc3c93 EB |
3225 | { |
3226 | /* ??? For historical reasons, we do it on strict-alignment | |
3227 | platforms only, where it is really required. This means | |
3228 | that a confirming representation clause will change the | |
3229 | behavior of the compiler on the other platforms. */ | |
3230 | if (STRICT_ALIGNMENT) | |
3231 | SET_TYPE_ALIGN (gnu_type, TYPE_ALIGN (gnu_parent)); | |
3232 | else | |
3233 | parent_packed | |
3234 | = adjust_packed (gnu_parent, gnu_type, parent_packed); | |
3235 | } | |
8c41a1c8 | 3236 | |
a1ab4c31 AC |
3237 | /* Finally we fix up both kinds of twisted COMPONENT_REF we have |
3238 | initially built. The discriminants must reference the fields | |
3239 | of the parent subtype and not those of its base type for the | |
3240 | placeholder machinery to properly work. */ | |
c244bf8f | 3241 | if (has_discr) |
cdaa0e0b EB |
3242 | { |
3243 | /* The actual parent subtype is the full view. */ | |
7ed9919d | 3244 | if (Is_Private_Type (gnat_parent)) |
a1ab4c31 | 3245 | { |
cdaa0e0b EB |
3246 | if (Present (Full_View (gnat_parent))) |
3247 | gnat_parent = Full_View (gnat_parent); | |
3248 | else | |
3249 | gnat_parent = Underlying_Full_View (gnat_parent); | |
a1ab4c31 AC |
3250 | } |
3251 | ||
cdaa0e0b EB |
3252 | for (gnat_field = First_Stored_Discriminant (gnat_entity); |
3253 | Present (gnat_field); | |
3254 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
3255 | if (Present (Corresponding_Discriminant (gnat_field))) | |
3256 | { | |
e028b0bb | 3257 | Entity_Id field; |
cdaa0e0b EB |
3258 | for (field = First_Stored_Discriminant (gnat_parent); |
3259 | Present (field); | |
3260 | field = Next_Stored_Discriminant (field)) | |
3261 | if (same_discriminant_p (gnat_field, field)) | |
3262 | break; | |
3263 | gcc_assert (Present (field)); | |
3264 | TREE_OPERAND (get_gnu_tree (gnat_field), 1) | |
3265 | = gnat_to_gnu_field_decl (field); | |
3266 | } | |
3267 | } | |
3268 | ||
a1ab4c31 AC |
3269 | /* The "get to the parent" COMPONENT_REF must be given its |
3270 | proper type... */ | |
3271 | TREE_TYPE (gnu_get_parent) = gnu_parent; | |
3272 | ||
8cd28148 | 3273 | /* ...and reference the _Parent field of this record. */ |
a6a29d0c | 3274 | gnu_field |
76af763d | 3275 | = create_field_decl (parent_name_id, |
da01bfee | 3276 | gnu_parent, gnu_type, |
c244bf8f EB |
3277 | has_rep |
3278 | ? TYPE_SIZE (gnu_parent) : NULL_TREE, | |
3279 | has_rep | |
da01bfee | 3280 | ? bitsize_zero_node : NULL_TREE, |
04bc3c93 | 3281 | parent_packed, 1); |
a6a29d0c EB |
3282 | DECL_INTERNAL_P (gnu_field) = 1; |
3283 | TREE_OPERAND (gnu_get_parent, 1) = gnu_field; | |
3284 | TYPE_FIELDS (gnu_type) = gnu_field; | |
a1ab4c31 AC |
3285 | } |
3286 | ||
3287 | /* Make the fields for the discriminants and put them into the record | |
3288 | unless it's an Unchecked_Union. */ | |
c244bf8f | 3289 | if (has_discr) |
a1ab4c31 AC |
3290 | for (gnat_field = First_Stored_Discriminant (gnat_entity); |
3291 | Present (gnat_field); | |
3292 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
3293 | { | |
8cd28148 EB |
3294 | /* If this is a record extension and this discriminant is the |
3295 | renaming of another discriminant, we've handled it above. */ | |
05dbb83f | 3296 | if (is_extension |
c00d5b12 EB |
3297 | && Present (Corresponding_Discriminant (gnat_field))) |
3298 | continue; | |
3299 | ||
a1ab4c31 | 3300 | gnu_field |
839f2864 EB |
3301 | = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition, |
3302 | debug_info_p); | |
a1ab4c31 AC |
3303 | |
3304 | /* Make an expression using a PLACEHOLDER_EXPR from the | |
3305 | FIELD_DECL node just created and link that with the | |
8cd28148 | 3306 | corresponding GNAT defining identifier. */ |
a1ab4c31 AC |
3307 | save_gnu_tree (gnat_field, |
3308 | build3 (COMPONENT_REF, TREE_TYPE (gnu_field), | |
8cd28148 | 3309 | build0 (PLACEHOLDER_EXPR, gnu_type), |
a1ab4c31 AC |
3310 | gnu_field, NULL_TREE), |
3311 | true); | |
3312 | ||
8cd28148 | 3313 | if (!is_unchecked_union) |
a1ab4c31 | 3314 | { |
910ad8de | 3315 | DECL_CHAIN (gnu_field) = gnu_field_list; |
a1ab4c31 AC |
3316 | gnu_field_list = gnu_field; |
3317 | } | |
3318 | } | |
3319 | ||
908ba941 | 3320 | /* If we have a derived untagged type that renames discriminants in |
b1b2b511 EB |
3321 | the parent type, the (stored) discriminants are just a copy of the |
3322 | discriminants of the parent type. This means that any constraints | |
3323 | added by the renaming in the derivation are disregarded as far as | |
3324 | the layout of the derived type is concerned. To rescue them, we | |
3325 | change the type of the (stored) discriminants to a subtype with | |
3326 | the bounds of the type of the visible discriminants. */ | |
908ba941 EB |
3327 | if (has_discr |
3328 | && !is_extension | |
3329 | && Stored_Constraint (gnat_entity) != No_Elist) | |
3330 | for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity)); | |
3331 | gnat_constr != No_Elmt; | |
3332 | gnat_constr = Next_Elmt (gnat_constr)) | |
3333 | if (Nkind (Node (gnat_constr)) == N_Identifier | |
3334 | /* Ignore access discriminants. */ | |
3335 | && !Is_Access_Type (Etype (Node (gnat_constr))) | |
3336 | && Ekind (Entity (Node (gnat_constr))) == E_Discriminant) | |
3337 | { | |
683ccd05 | 3338 | const Entity_Id gnat_discr = Entity (Node (gnat_constr)); |
05dbb83f AC |
3339 | tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr)); |
3340 | tree gnu_ref | |
908ba941 | 3341 | = gnat_to_gnu_entity (Original_Record_Component (gnat_discr), |
afc737f0 | 3342 | NULL_TREE, false); |
908ba941 EB |
3343 | |
3344 | /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built | |
3345 | just above for one of the stored discriminants. */ | |
3346 | gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type); | |
3347 | ||
3348 | if (gnu_discr_type != TREE_TYPE (gnu_ref)) | |
683ccd05 EB |
3349 | TREE_TYPE (gnu_ref) |
3350 | = create_extra_subtype (TREE_TYPE (gnu_ref), | |
3351 | TYPE_MIN_VALUE (gnu_discr_type), | |
3352 | TYPE_MAX_VALUE (gnu_discr_type)); | |
908ba941 EB |
3353 | } |
3354 | ||
05dbb83f | 3355 | /* If this is a derived type with discriminants and these discriminants |
87eddedc | 3356 | affect the initial shape it has inherited, factor them in. */ |
05dbb83f AC |
3357 | if (has_discr |
3358 | && !is_extension | |
3359 | && !Has_Record_Rep_Clause (gnat_entity) | |
3360 | && Stored_Constraint (gnat_entity) != No_Elist | |
3361 | && (gnat_parent_type = Underlying_Type (Etype (gnat_entity))) | |
3362 | && Is_Record_Type (gnat_parent_type) | |
87eddedc EB |
3363 | && Is_Unchecked_Union (gnat_entity) |
3364 | == Is_Unchecked_Union (gnat_parent_type) | |
8489c295 | 3365 | && No_Reordering (gnat_entity) == No_Reordering (gnat_parent_type)) |
05dbb83f AC |
3366 | { |
3367 | tree gnu_parent_type | |
3368 | = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_parent_type)); | |
3369 | ||
3370 | if (TYPE_IS_PADDING_P (gnu_parent_type)) | |
3371 | gnu_parent_type = TREE_TYPE (TYPE_FIELDS (gnu_parent_type)); | |
3372 | ||
3373 | vec<subst_pair> gnu_subst_list | |
3374 | = build_subst_list (gnat_entity, gnat_parent_type, definition); | |
3375 | ||
3376 | /* Set the layout of the type to match that of the parent type, | |
58d32c72 EB |
3377 | doing required substitutions. Note that, if we do not use the |
3378 | GNAT encodings, we don't need debug info for the inner record | |
95b7c2e0 PMR |
3379 | types, as they will be part of the embedding variant record's |
3380 | debug info. */ | |
3381 | copy_and_substitute_in_layout | |
3382 | (gnat_entity, gnat_parent_type, gnu_type, gnu_parent_type, | |
3383 | gnu_subst_list, | |
58d32c72 | 3384 | debug_info_p && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL); |
05dbb83f AC |
3385 | } |
3386 | else | |
3387 | { | |
3388 | /* Add the fields into the record type and finish it up. */ | |
3389 | components_to_record (Component_List (record_definition), | |
3390 | gnat_entity, gnu_field_list, gnu_type, | |
3391 | packed, definition, false, all_rep, | |
3392 | is_unchecked_union, artificial_p, | |
3393 | debug_info_p, false, | |
3394 | all_rep ? NULL_TREE : bitsize_zero_node, | |
3395 | NULL); | |
3396 | ||
0d0cd281 EB |
3397 | /* Empty classes have the size of a storage unit in C++. */ |
3398 | if (TYPE_SIZE (gnu_type) == bitsize_zero_node | |
3399 | && Convention (gnat_entity) == Convention_CPP) | |
3400 | { | |
3401 | TYPE_SIZE (gnu_type) = bitsize_unit_node; | |
3402 | TYPE_SIZE_UNIT (gnu_type) = size_one_node; | |
3403 | compute_record_mode (gnu_type); | |
3404 | } | |
3405 | ||
8623afc4 EB |
3406 | /* If the type needs strict alignment, then no object of the type |
3407 | may have a size smaller than the natural size, which means that | |
3408 | the RM size of the type is equal to the type size. */ | |
3409 | if (Strict_Alignment (gnat_entity)) | |
3410 | SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type)); | |
3411 | ||
05dbb83f AC |
3412 | /* If there are entities in the chain corresponding to components |
3413 | that we did not elaborate, ensure we elaborate their types if | |
af62ba41 | 3414 | they are itypes. */ |
05dbb83f AC |
3415 | for (gnat_temp = First_Entity (gnat_entity); |
3416 | Present (gnat_temp); | |
3417 | gnat_temp = Next_Entity (gnat_temp)) | |
3418 | if ((Ekind (gnat_temp) == E_Component | |
3419 | || Ekind (gnat_temp) == E_Discriminant) | |
3420 | && Is_Itype (Etype (gnat_temp)) | |
3421 | && !present_gnu_tree (gnat_temp)) | |
3422 | gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false); | |
3423 | } | |
a1ab4c31 | 3424 | |
a1ab4c31 AC |
3425 | /* Fill in locations of fields. */ |
3426 | annotate_rep (gnat_entity, gnu_type); | |
a1ab4c31 AC |
3427 | } |
3428 | break; | |
3429 | ||
3430 | case E_Class_Wide_Subtype: | |
3431 | /* If an equivalent type is present, that is what we should use. | |
3432 | Otherwise, fall through to handle this like a record subtype | |
3433 | since it may have constraints. */ | |
3434 | if (gnat_equiv_type != gnat_entity) | |
3435 | { | |
afc737f0 | 3436 | gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false); |
a1ab4c31 AC |
3437 | maybe_present = true; |
3438 | break; | |
3439 | } | |
3440 | ||
9c453de7 | 3441 | /* ... fall through ... */ |
a1ab4c31 AC |
3442 | |
3443 | case E_Record_Subtype: | |
a1ab4c31 AC |
3444 | /* If Cloned_Subtype is Present it means this record subtype has |
3445 | identical layout to that type or subtype and we should use | |
7fddde95 | 3446 | that GCC type for this one. The front-end guarantees that |
a1ab4c31 AC |
3447 | the component list is shared. */ |
3448 | if (Present (Cloned_Subtype (gnat_entity))) | |
3449 | { | |
3450 | gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity), | |
afc737f0 | 3451 | NULL_TREE, false); |
f2bee239 | 3452 | gnat_annotate_type = Cloned_Subtype (gnat_entity); |
7fddde95 | 3453 | maybe_present = true; |
8cd28148 | 3454 | break; |
a1ab4c31 AC |
3455 | } |
3456 | ||
3457 | /* Otherwise, first ensure the base type is elaborated. Then, if we are | |
8cd28148 EB |
3458 | changing the type, make a new type with each field having the type of |
3459 | the field in the new subtype but the position computed by transforming | |
3460 | every discriminant reference according to the constraints. We don't | |
3461 | see any difference between private and non-private type here since | |
3462 | derivations from types should have been deferred until the completion | |
3463 | of the private type. */ | |
a1ab4c31 AC |
3464 | else |
3465 | { | |
3466 | Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity); | |
a1ab4c31 AC |
3467 | |
3468 | if (!definition) | |
8cd28148 EB |
3469 | { |
3470 | defer_incomplete_level++; | |
3471 | this_deferred = true; | |
3472 | } | |
a1ab4c31 | 3473 | |
05dbb83f | 3474 | tree gnu_base_type |
f797c2b7 | 3475 | = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type)); |
a1ab4c31 | 3476 | |
a1ab4c31 AC |
3477 | if (present_gnu_tree (gnat_entity)) |
3478 | { | |
3479 | maybe_present = true; | |
3480 | break; | |
3481 | } | |
3482 | ||
8cd28148 | 3483 | /* When the subtype has discriminants and these discriminants affect |
95c1c4bb | 3484 | the initial shape it has inherited, factor them in. But for an |
af62ba41 | 3485 | Unchecked_Union (it must be an itype), just return the type. */ |
05dbb83f AC |
3486 | if (Has_Discriminants (gnat_entity) |
3487 | && Stored_Constraint (gnat_entity) != No_Elist | |
05dbb83f AC |
3488 | && Is_Record_Type (gnat_base_type) |
3489 | && !Is_Unchecked_Union (gnat_base_type)) | |
a1ab4c31 | 3490 | { |
9771b263 | 3491 | vec<subst_pair> gnu_subst_list |
8cd28148 | 3492 | = build_subst_list (gnat_entity, gnat_base_type, definition); |
05dbb83f | 3493 | tree gnu_unpad_base_type; |
a1ab4c31 AC |
3494 | |
3495 | gnu_type = make_node (RECORD_TYPE); | |
0fb2335d | 3496 | TYPE_NAME (gnu_type) = gnu_entity_name; |
92eee8f8 | 3497 | TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type); |
ee45a32d EB |
3498 | TYPE_REVERSE_STORAGE_ORDER (gnu_type) |
3499 | = Reverse_Storage_Order (gnat_entity); | |
74746d49 | 3500 | process_attributes (&gnu_type, &attr_list, true, gnat_entity); |
a1ab4c31 | 3501 | |
05dbb83f AC |
3502 | /* Set the size, alignment and alias set of the type to match |
3503 | those of the base type, doing required substitutions. */ | |
95c1c4bb EB |
3504 | copy_and_substitute_in_size (gnu_type, gnu_base_type, |
3505 | gnu_subst_list); | |
c244bf8f | 3506 | |
315cff15 | 3507 | if (TYPE_IS_PADDING_P (gnu_base_type)) |
c244bf8f EB |
3508 | gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type)); |
3509 | else | |
3510 | gnu_unpad_base_type = gnu_base_type; | |
3511 | ||
05dbb83f AC |
3512 | /* Set the layout of the type to match that of the base type, |
3513 | doing required substitutions. We will output debug info | |
3514 | manually below so pass false as last argument. */ | |
3515 | copy_and_substitute_in_layout (gnat_entity, gnat_base_type, | |
3516 | gnu_type, gnu_unpad_base_type, | |
3517 | gnu_subst_list, false); | |
a1ab4c31 | 3518 | |
a1ab4c31 AC |
3519 | /* Fill in locations of fields. */ |
3520 | annotate_rep (gnat_entity, gnu_type); | |
3521 | ||
986ccd21 | 3522 | /* If debugging information is being written for the type and if |
58d32c72 | 3523 | we are asked to output GNAT encodings, write a record that |
986ccd21 PMR |
3524 | shows what we are a subtype of and also make a variable that |
3525 | indicates our size, if still variable. */ | |
1e3cabd4 | 3526 | if (debug_info_p |
58d32c72 | 3527 | && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
a1ab4c31 AC |
3528 | { |
3529 | tree gnu_subtype_marker = make_node (RECORD_TYPE); | |
9dba4b55 PC |
3530 | tree gnu_unpad_base_name |
3531 | = TYPE_IDENTIFIER (gnu_unpad_base_type); | |
e9cfc9b5 | 3532 | tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type); |
a1ab4c31 | 3533 | |
a1ab4c31 AC |
3534 | TYPE_NAME (gnu_subtype_marker) |
3535 | = create_concat_name (gnat_entity, "XVS"); | |
3536 | finish_record_type (gnu_subtype_marker, | |
c244bf8f EB |
3537 | create_field_decl (gnu_unpad_base_name, |
3538 | build_reference_type | |
3539 | (gnu_unpad_base_type), | |
a1ab4c31 | 3540 | gnu_subtype_marker, |
da01bfee EB |
3541 | NULL_TREE, NULL_TREE, |
3542 | 0, 0), | |
032d1b71 | 3543 | 0, true); |
a1ab4c31 | 3544 | |
a5695aa2 | 3545 | add_parallel_type (gnu_type, gnu_subtype_marker); |
e9cfc9b5 EB |
3546 | |
3547 | if (definition | |
3548 | && TREE_CODE (gnu_size_unit) != INTEGER_CST | |
3549 | && !CONTAINS_PLACEHOLDER_P (gnu_size_unit)) | |
b5bba4a6 EB |
3550 | TYPE_SIZE_UNIT (gnu_subtype_marker) |
3551 | = create_var_decl (create_concat_name (gnat_entity, | |
3552 | "XVZ"), | |
3553 | NULL_TREE, sizetype, gnu_size_unit, | |
3553d8c2 EB |
3554 | true, false, false, false, false, |
3555 | true, true, NULL, gnat_entity, false); | |
a1ab4c31 | 3556 | } |
fa0588db | 3557 | |
58d32c72 EB |
3558 | /* Or else, if the subtype is artificial and GNAT encodings are |
3559 | not used, use the base record type as the debug type. */ | |
fa0588db EB |
3560 | else if (debug_info_p |
3561 | && artificial_p | |
58d32c72 | 3562 | && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL) |
fa0588db | 3563 | SET_TYPE_DEBUG_TYPE (gnu_type, gnu_unpad_base_type); |
a1ab4c31 AC |
3564 | } |
3565 | ||
8cd28148 EB |
3566 | /* Otherwise, go down all the components in the new type and make |
3567 | them equivalent to those in the base type. */ | |
a1ab4c31 | 3568 | else |
8cd28148 | 3569 | { |
c244bf8f | 3570 | gnu_type = gnu_base_type; |
8cd28148 EB |
3571 | |
3572 | for (gnat_temp = First_Entity (gnat_entity); | |
3573 | Present (gnat_temp); | |
3574 | gnat_temp = Next_Entity (gnat_temp)) | |
3575 | if ((Ekind (gnat_temp) == E_Discriminant | |
3576 | && !Is_Unchecked_Union (gnat_base_type)) | |
3577 | || Ekind (gnat_temp) == E_Component) | |
3578 | save_gnu_tree (gnat_temp, | |
3579 | gnat_to_gnu_field_decl | |
3580 | (Original_Record_Component (gnat_temp)), | |
3581 | false); | |
3582 | } | |
a1ab4c31 AC |
3583 | } |
3584 | break; | |
3585 | ||
3586 | case E_Access_Subprogram_Type: | |
1e55d29a | 3587 | case E_Anonymous_Access_Subprogram_Type: |
a1ab4c31 AC |
3588 | /* Use the special descriptor type for dispatch tables if needed, |
3589 | that is to say for the Prim_Ptr of a-tags.ads and its clones. | |
3590 | Note that we are only required to do so for static tables in | |
3591 | order to be compatible with the C++ ABI, but Ada 2005 allows | |
3592 | to extend library level tagged types at the local level so | |
3593 | we do it in the non-static case as well. */ | |
3594 | if (TARGET_VTABLE_USES_DESCRIPTORS | |
3595 | && Is_Dispatch_Table_Entity (gnat_entity)) | |
3596 | { | |
3597 | gnu_type = fdesc_type_node; | |
3598 | gnu_size = TYPE_SIZE (gnu_type); | |
3599 | break; | |
3600 | } | |
3601 | ||
9c453de7 | 3602 | /* ... fall through ... */ |
a1ab4c31 | 3603 | |
a1ab4c31 AC |
3604 | case E_Allocator_Type: |
3605 | case E_Access_Type: | |
3606 | case E_Access_Attribute_Type: | |
3607 | case E_Anonymous_Access_Type: | |
3608 | case E_General_Access_Type: | |
3609 | { | |
d0c26312 | 3610 | /* The designated type and its equivalent type for gigi. */ |
a1ab4c31 AC |
3611 | Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity); |
3612 | Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type); | |
d0c26312 | 3613 | /* Whether it comes from a limited with. */ |
1e55d29a | 3614 | const bool is_from_limited_with |
7ed9919d | 3615 | = (Is_Incomplete_Type (gnat_desig_equiv) |
7b56a91b | 3616 | && From_Limited_With (gnat_desig_equiv)); |
d3271136 EB |
3617 | /* Whether it is a completed Taft Amendment type. Such a type is to |
3618 | be treated as coming from a limited with clause if it is not in | |
3619 | the main unit, i.e. we break potential circularities here in case | |
3620 | the body of an external unit is loaded for inter-unit inlining. */ | |
3621 | const bool is_completed_taft_type | |
7ed9919d | 3622 | = (Is_Incomplete_Type (gnat_desig_equiv) |
d3271136 EB |
3623 | && Has_Completion_In_Body (gnat_desig_equiv) |
3624 | && Present (Full_View (gnat_desig_equiv))); | |
d0c26312 | 3625 | /* The "full view" of the designated type. If this is an incomplete |
a1ab4c31 AC |
3626 | entity from a limited with, treat its non-limited view as the full |
3627 | view. Otherwise, if this is an incomplete or private type, use the | |
3628 | full view. In the former case, we might point to a private type, | |
3629 | in which case, we need its full view. Also, we want to look at the | |
3630 | actual type used for the representation, so this takes a total of | |
3631 | three steps. */ | |
3632 | Entity_Id gnat_desig_full_direct_first | |
d0c26312 EB |
3633 | = (is_from_limited_with |
3634 | ? Non_Limited_View (gnat_desig_equiv) | |
7ed9919d | 3635 | : (Is_Incomplete_Or_Private_Type (gnat_desig_equiv) |
a1ab4c31 AC |
3636 | ? Full_View (gnat_desig_equiv) : Empty)); |
3637 | Entity_Id gnat_desig_full_direct | |
3638 | = ((is_from_limited_with | |
3639 | && Present (gnat_desig_full_direct_first) | |
7ed9919d | 3640 | && Is_Private_Type (gnat_desig_full_direct_first)) |
a1ab4c31 AC |
3641 | ? Full_View (gnat_desig_full_direct_first) |
3642 | : gnat_desig_full_direct_first); | |
3643 | Entity_Id gnat_desig_full | |
3644 | = Gigi_Equivalent_Type (gnat_desig_full_direct); | |
d0c26312 EB |
3645 | /* The type actually used to represent the designated type, either |
3646 | gnat_desig_full or gnat_desig_equiv. */ | |
a1ab4c31 | 3647 | Entity_Id gnat_desig_rep; |
a1ab4c31 AC |
3648 | /* We want to know if we'll be seeing the freeze node for any |
3649 | incomplete type we may be pointing to. */ | |
1e55d29a | 3650 | const bool in_main_unit |
a1ab4c31 AC |
3651 | = (Present (gnat_desig_full) |
3652 | ? In_Extended_Main_Code_Unit (gnat_desig_full) | |
3653 | : In_Extended_Main_Code_Unit (gnat_desig_type)); | |
1e17ef87 | 3654 | /* True if we make a dummy type here. */ |
a1ab4c31 | 3655 | bool made_dummy = false; |
d0c26312 | 3656 | /* The mode to be used for the pointer type. */ |
fffbab82 | 3657 | scalar_int_mode p_mode; |
d0c26312 EB |
3658 | /* The GCC type used for the designated type. */ |
3659 | tree gnu_desig_type = NULL_TREE; | |
a1ab4c31 | 3660 | |
fffbab82 RS |
3661 | if (!int_mode_for_size (esize, 0).exists (&p_mode) |
3662 | || !targetm.valid_pointer_mode (p_mode)) | |
a1ab4c31 AC |
3663 | p_mode = ptr_mode; |
3664 | ||
3665 | /* If either the designated type or its full view is an unconstrained | |
3666 | array subtype, replace it with the type it's a subtype of. This | |
3667 | avoids problems with multiple copies of unconstrained array types. | |
3668 | Likewise, if the designated type is a subtype of an incomplete | |
3669 | record type, use the parent type to avoid order of elaboration | |
3670 | issues. This can lose some code efficiency, but there is no | |
3671 | alternative. */ | |
3672 | if (Ekind (gnat_desig_equiv) == E_Array_Subtype | |
d0c26312 | 3673 | && !Is_Constrained (gnat_desig_equiv)) |
a1ab4c31 AC |
3674 | gnat_desig_equiv = Etype (gnat_desig_equiv); |
3675 | if (Present (gnat_desig_full) | |
3676 | && ((Ekind (gnat_desig_full) == E_Array_Subtype | |
d0c26312 | 3677 | && !Is_Constrained (gnat_desig_full)) |
a1ab4c31 AC |
3678 | || (Ekind (gnat_desig_full) == E_Record_Subtype |
3679 | && Ekind (Etype (gnat_desig_full)) == E_Record_Type))) | |
3680 | gnat_desig_full = Etype (gnat_desig_full); | |
3681 | ||
8ea456b9 | 3682 | /* Set the type that's the representation of the designated type. */ |
d0c26312 EB |
3683 | gnat_desig_rep |
3684 | = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv; | |
a1ab4c31 AC |
3685 | |
3686 | /* If we already know what the full type is, use it. */ | |
8ea456b9 | 3687 | if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full)) |
a1ab4c31 AC |
3688 | gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full)); |
3689 | ||
d0c26312 EB |
3690 | /* Get the type of the thing we are to point to and build a pointer to |
3691 | it. If it is a reference to an incomplete or private type with a | |
d3271136 EB |
3692 | full view that is a record, an array or an access, make a dummy type |
3693 | and get the actual type later when we have verified it is safe. */ | |
d0c26312 EB |
3694 | else if ((!in_main_unit |
3695 | && !present_gnu_tree (gnat_desig_equiv) | |
a1ab4c31 | 3696 | && Present (gnat_desig_full) |
8ea456b9 | 3697 | && (Is_Record_Type (gnat_desig_full) |
d3271136 EB |
3698 | || Is_Array_Type (gnat_desig_full) |
3699 | || Is_Access_Type (gnat_desig_full))) | |
1e55d29a EB |
3700 | /* Likewise if this is a reference to a record, an array or a |
3701 | subprogram type and we are to defer elaborating incomplete | |
3702 | types. We do this because this access type may be the full | |
3703 | view of a private type. */ | |
d0c26312 | 3704 | || ((!in_main_unit || imported_p) |
a10623fb | 3705 | && defer_incomplete_level != 0 |
d0c26312 EB |
3706 | && !present_gnu_tree (gnat_desig_equiv) |
3707 | && (Is_Record_Type (gnat_desig_rep) | |
1e55d29a EB |
3708 | || Is_Array_Type (gnat_desig_rep) |
3709 | || Ekind (gnat_desig_rep) == E_Subprogram_Type)) | |
a1ab4c31 | 3710 | /* If this is a reference from a limited_with type back to our |
d0c26312 | 3711 | main unit and there's a freeze node for it, either we have |
a1ab4c31 AC |
3712 | already processed the declaration and made the dummy type, |
3713 | in which case we just reuse the latter, or we have not yet, | |
3714 | in which case we make the dummy type and it will be reused | |
d0c26312 EB |
3715 | when the declaration is finally processed. In both cases, |
3716 | the pointer eventually created below will be automatically | |
8ea456b9 EB |
3717 | adjusted when the freeze node is processed. */ |
3718 | || (in_main_unit | |
3719 | && is_from_limited_with | |
3720 | && Present (Freeze_Node (gnat_desig_rep)))) | |
a1ab4c31 AC |
3721 | { |
3722 | gnu_desig_type = make_dummy_type (gnat_desig_equiv); | |
3723 | made_dummy = true; | |
3724 | } | |
3725 | ||
3726 | /* Otherwise handle the case of a pointer to itself. */ | |
3727 | else if (gnat_desig_equiv == gnat_entity) | |
3728 | { | |
3729 | gnu_type | |
3730 | = build_pointer_type_for_mode (void_type_node, p_mode, | |
3731 | No_Strict_Aliasing (gnat_entity)); | |
3732 | TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type; | |
3733 | } | |
3734 | ||
d0c26312 | 3735 | /* If expansion is disabled, the equivalent type of a concurrent type |
8234d02a | 3736 | is absent, so we use the void pointer type. */ |
a1ab4c31 | 3737 | else if (type_annotate_only && No (gnat_desig_equiv)) |
1366ba41 | 3738 | gnu_type = ptr_type_node; |
a1ab4c31 | 3739 | |
8234d02a EB |
3740 | /* If the ultimately designated type is an incomplete type with no full |
3741 | view, we use the void pointer type in LTO mode to avoid emitting a | |
3742 | dummy type in the GIMPLE IR. We cannot do that in regular mode as | |
3743 | the name of the dummy type in used by GDB for a global lookup. */ | |
3744 | else if (Ekind (gnat_desig_rep) == E_Incomplete_Type | |
3745 | && No (Full_View (gnat_desig_rep)) | |
3746 | && flag_generate_lto) | |
3747 | gnu_type = ptr_type_node; | |
3748 | ||
d0c26312 EB |
3749 | /* Finally, handle the default case where we can just elaborate our |
3750 | designated type. */ | |
a1ab4c31 AC |
3751 | else |
3752 | gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv); | |
3753 | ||
3754 | /* It is possible that a call to gnat_to_gnu_type above resolved our | |
3755 | type. If so, just return it. */ | |
3756 | if (present_gnu_tree (gnat_entity)) | |
3757 | { | |
3758 | maybe_present = true; | |
3759 | break; | |
3760 | } | |
3761 | ||
1e55d29a | 3762 | /* Access-to-unconstrained-array types need a special treatment. */ |
8ea456b9 EB |
3763 | if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep)) |
3764 | { | |
3765 | /* If the processing above got something that has a pointer, then | |
3766 | we are done. This could have happened either because the type | |
3767 | was elaborated or because somebody else executed the code. */ | |
3768 | if (!TYPE_POINTER_TO (gnu_desig_type)) | |
3769 | build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type); | |
1e55d29a | 3770 | |
8ea456b9 EB |
3771 | gnu_type = TYPE_POINTER_TO (gnu_desig_type); |
3772 | } | |
3773 | ||
1228a6a6 | 3774 | /* If we haven't done it yet, build the pointer type the usual way. */ |
8ea456b9 | 3775 | else if (!gnu_type) |
a1ab4c31 | 3776 | { |
d0c26312 | 3777 | /* Modify the designated type if we are pointing only to constant |
1e55d29a | 3778 | objects, but don't do it for a dummy type. */ |
a1ab4c31 | 3779 | if (Is_Access_Constant (gnat_entity) |
1e55d29a EB |
3780 | && !TYPE_IS_DUMMY_P (gnu_desig_type)) |
3781 | gnu_desig_type | |
3782 | = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST); | |
a1ab4c31 AC |
3783 | |
3784 | gnu_type | |
3785 | = build_pointer_type_for_mode (gnu_desig_type, p_mode, | |
3786 | No_Strict_Aliasing (gnat_entity)); | |
3787 | } | |
3788 | ||
1e55d29a EB |
3789 | /* If the designated type is not declared in the main unit and we made |
3790 | a dummy node for it, save our definition, elaborate the actual type | |
3791 | and replace the dummy type we made with the actual one. But if we | |
3792 | are to defer actually looking up the actual type, make an entry in | |
3793 | the deferred list instead. If this is from a limited with, we may | |
3794 | have to defer until the end of the current unit. */ | |
3795 | if (!in_main_unit && made_dummy) | |
a1ab4c31 | 3796 | { |
1e55d29a EB |
3797 | if (TYPE_IS_FAT_POINTER_P (gnu_type) && esize == POINTER_SIZE) |
3798 | gnu_type | |
3799 | = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)); | |
a1ab4c31 | 3800 | |
74746d49 EB |
3801 | process_attributes (&gnu_type, &attr_list, false, gnat_entity); |
3802 | gnu_decl = create_type_decl (gnu_entity_name, gnu_type, | |
c1a569ef EB |
3803 | artificial_p, debug_info_p, |
3804 | gnat_entity); | |
a1ab4c31 AC |
3805 | this_made_decl = true; |
3806 | gnu_type = TREE_TYPE (gnu_decl); | |
3807 | save_gnu_tree (gnat_entity, gnu_decl, false); | |
3808 | saved = true; | |
3809 | ||
d3271136 EB |
3810 | if (defer_incomplete_level == 0 |
3811 | && !is_from_limited_with | |
3812 | && !is_completed_taft_type) | |
80ec8b4c | 3813 | { |
1e55d29a | 3814 | update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type), |
80ec8b4c | 3815 | gnat_to_gnu_type (gnat_desig_equiv)); |
80ec8b4c | 3816 | } |
a1ab4c31 AC |
3817 | else |
3818 | { | |
d0c26312 | 3819 | struct incomplete *p = XNEW (struct incomplete); |
a1ab4c31 | 3820 | struct incomplete **head |
d3271136 | 3821 | = (is_from_limited_with || is_completed_taft_type |
1e55d29a EB |
3822 | ? &defer_limited_with_list : &defer_incomplete_list); |
3823 | ||
3824 | p->old_type = gnu_desig_type; | |
a1ab4c31 AC |
3825 | p->full_type = gnat_desig_equiv; |
3826 | p->next = *head; | |
3827 | *head = p; | |
3828 | } | |
3829 | } | |
3830 | } | |
3831 | break; | |
3832 | ||
3833 | case E_Access_Protected_Subprogram_Type: | |
3834 | case E_Anonymous_Access_Protected_Subprogram_Type: | |
42a5e410 | 3835 | /* If we are just annotating types and have no equivalent record type, |
8234d02a | 3836 | just use the void pointer type. */ |
42a5e410 | 3837 | if (type_annotate_only && gnat_equiv_type == gnat_entity) |
1366ba41 | 3838 | gnu_type = ptr_type_node; |
42a5e410 EB |
3839 | |
3840 | /* The run-time representation is the equivalent type. */ | |
a1ab4c31 AC |
3841 | else |
3842 | { | |
a1ab4c31 | 3843 | gnu_type = gnat_to_gnu_type (gnat_equiv_type); |
2ddc34ba | 3844 | maybe_present = true; |
a1ab4c31 AC |
3845 | } |
3846 | ||
1e55d29a EB |
3847 | /* The designated subtype must be elaborated as well, if it does |
3848 | not have its own freeze node. */ | |
a1ab4c31 AC |
3849 | if (Is_Itype (Directly_Designated_Type (gnat_entity)) |
3850 | && !present_gnu_tree (Directly_Designated_Type (gnat_entity)) | |
3851 | && No (Freeze_Node (Directly_Designated_Type (gnat_entity))) | |
3852 | && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity)))) | |
3853 | gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity), | |
afc737f0 | 3854 | NULL_TREE, false); |
a1ab4c31 AC |
3855 | |
3856 | break; | |
3857 | ||
3858 | case E_Access_Subtype: | |
a1ab4c31 | 3859 | /* We treat this as identical to its base type; any constraint is |
1e55d29a | 3860 | meaningful only to the front-end. */ |
7fddde95 EB |
3861 | gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false); |
3862 | maybe_present = true; | |
a1ab4c31 | 3863 | |
1e55d29a EB |
3864 | /* The designated subtype must be elaborated as well, if it does |
3865 | not have its own freeze node. But designated subtypes created | |
a1ab4c31 | 3866 | for constrained components of records with discriminants are |
1e55d29a EB |
3867 | not frozen by the front-end and not elaborated here, because |
3868 | their use may appear before the base type is frozen and it is | |
3869 | not clear that they are needed in gigi. With the current model, | |
3870 | there is no correct place where they could be elaborated. */ | |
a1ab4c31 AC |
3871 | if (Is_Itype (Directly_Designated_Type (gnat_entity)) |
3872 | && !present_gnu_tree (Directly_Designated_Type (gnat_entity)) | |
3873 | && Is_Frozen (Directly_Designated_Type (gnat_entity)) | |
3874 | && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))) | |
3875 | { | |
1e55d29a EB |
3876 | /* If we are to defer elaborating incomplete types, make a dummy |
3877 | type node and elaborate it later. */ | |
3878 | if (defer_incomplete_level != 0) | |
a1ab4c31 | 3879 | { |
dee12fcd | 3880 | struct incomplete *p = XNEW (struct incomplete); |
a1ab4c31 | 3881 | |
dee12fcd EB |
3882 | p->old_type |
3883 | = make_dummy_type (Directly_Designated_Type (gnat_entity)); | |
a1ab4c31 AC |
3884 | p->full_type = Directly_Designated_Type (gnat_entity); |
3885 | p->next = defer_incomplete_list; | |
3886 | defer_incomplete_list = p; | |
3887 | } | |
7ed9919d EB |
3888 | else if (!Is_Incomplete_Or_Private_Type |
3889 | (Base_Type (Directly_Designated_Type (gnat_entity)))) | |
a1ab4c31 | 3890 | gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity), |
afc737f0 | 3891 | NULL_TREE, false); |
a1ab4c31 | 3892 | } |
a1ab4c31 AC |
3893 | break; |
3894 | ||
3895 | /* Subprogram Entities | |
3896 | ||
c9d84d0e | 3897 | The following access functions are defined for subprograms: |
a1ab4c31 | 3898 | |
c9d84d0e | 3899 | Etype Return type or Standard_Void_Type. |
a1ab4c31 AC |
3900 | First_Formal The first formal parameter. |
3901 | Is_Imported Indicates that the subprogram has appeared in | |
2ddc34ba | 3902 | an INTERFACE or IMPORT pragma. For now we |
a1ab4c31 AC |
3903 | assume that the external language is C. |
3904 | Is_Exported Likewise but for an EXPORT pragma. | |
3905 | Is_Inlined True if the subprogram is to be inlined. | |
3906 | ||
a1ab4c31 AC |
3907 | Each parameter is first checked by calling must_pass_by_ref on its |
3908 | type to determine if it is passed by reference. For parameters which | |
3909 | are copied in, if they are Ada In Out or Out parameters, their return | |
3910 | value becomes part of a record which becomes the return type of the | |
3911 | function (C function - note that this applies only to Ada procedures | |
2ddc34ba | 3912 | so there is no Ada return type). Additional code to store back the |
a1ab4c31 AC |
3913 | parameters will be generated on the caller side. This transformation |
3914 | is done here, not in the front-end. | |
3915 | ||
3916 | The intended result of the transformation can be seen from the | |
3917 | equivalent source rewritings that follow: | |
3918 | ||
3919 | struct temp {int a,b}; | |
3920 | procedure P (A,B: In Out ...) is temp P (int A,B) | |
3921 | begin { | |
3922 | .. .. | |
3923 | end P; return {A,B}; | |
3924 | } | |
3925 | ||
3926 | temp t; | |
3927 | P(X,Y); t = P(X,Y); | |
3928 | X = t.a , Y = t.b; | |
3929 | ||
3930 | For subprogram types we need to perform mainly the same conversions to | |
3931 | GCC form that are needed for procedures and function declarations. The | |
3932 | only difference is that at the end, we make a type declaration instead | |
3933 | of a function declaration. */ | |
3934 | ||
3935 | case E_Subprogram_Type: | |
3936 | case E_Function: | |
3937 | case E_Procedure: | |
3938 | { | |
7414a3c3 EB |
3939 | tree gnu_ext_name |
3940 | = gnu_ext_name_for_subprog (gnat_entity, gnu_entity_name); | |
13a6dfe3 EB |
3941 | const enum inline_status_t inline_status |
3942 | = inline_status_for_subprog (gnat_entity); | |
a1ab4c31 | 3943 | bool public_flag = Is_Public (gnat_entity) || imported_p; |
5865a63d AC |
3944 | /* Subprograms marked both Intrinsic and Always_Inline need not |
3945 | have a body of their own. */ | |
a1ab4c31 | 3946 | bool extern_flag |
5865a63d AC |
3947 | = ((Is_Public (gnat_entity) && !definition) |
3948 | || imported_p | |
abb540a7 | 3949 | || (Is_Intrinsic_Subprogram (gnat_entity) |
5865a63d | 3950 | && Has_Pragma_Inline_Always (gnat_entity))); |
1e55d29a | 3951 | tree gnu_param_list; |
a1ab4c31 | 3952 | |
8cd28148 EB |
3953 | /* A parameter may refer to this type, so defer completion of any |
3954 | incomplete types. */ | |
a1ab4c31 | 3955 | if (kind == E_Subprogram_Type && !definition) |
8cd28148 EB |
3956 | { |
3957 | defer_incomplete_level++; | |
3958 | this_deferred = true; | |
3959 | } | |
a1ab4c31 AC |
3960 | |
3961 | /* If the subprogram has an alias, it is probably inherited, so | |
3962 | we can use the original one. If the original "subprogram" | |
3963 | is actually an enumeration literal, it may be the first use | |
3964 | of its type, so we must elaborate that type now. */ | |
3965 | if (Present (Alias (gnat_entity))) | |
3966 | { | |
af62ba41 | 3967 | const Entity_Id gnat_alias = Alias (gnat_entity); |
1d4b96e0 | 3968 | |
af62ba41 EB |
3969 | if (Ekind (gnat_alias) == E_Enumeration_Literal) |
3970 | gnat_to_gnu_entity (Etype (gnat_alias), NULL_TREE, false); | |
a1ab4c31 | 3971 | |
af62ba41 | 3972 | gnu_decl = gnat_to_gnu_entity (gnat_alias, gnu_expr, false); |
a1ab4c31 | 3973 | |
af62ba41 | 3974 | /* Elaborate any itypes in the parameters of this entity. */ |
a1ab4c31 AC |
3975 | for (gnat_temp = First_Formal_With_Extras (gnat_entity); |
3976 | Present (gnat_temp); | |
3977 | gnat_temp = Next_Formal_With_Extras (gnat_temp)) | |
3978 | if (Is_Itype (Etype (gnat_temp))) | |
afc737f0 | 3979 | gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false); |
a1ab4c31 | 3980 | |
1d4b96e0 | 3981 | /* Materialize renamed subprograms in the debugging information |
af62ba41 | 3982 | when the renamed object is known at compile time; we consider |
1d4b96e0 AC |
3983 | such renamings as imported declarations. |
3984 | ||
af62ba41 EB |
3985 | Because the parameters in generic instantiations are generally |
3986 | materialized as renamings, we often end up having both the | |
1d4b96e0 | 3987 | renamed subprogram and the renaming in the same context and with |
af62ba41 | 3988 | the same name; in this case, renaming is both useless debug-wise |
1d4b96e0 AC |
3989 | and potentially harmful as name resolution in the debugger could |
3990 | return twice the same entity! So avoid this case. */ | |
af62ba41 EB |
3991 | if (debug_info_p |
3992 | && !artificial_p | |
3993 | && (Ekind (gnat_alias) == E_Function | |
3994 | || Ekind (gnat_alias) == E_Procedure) | |
1d4b96e0 | 3995 | && !(get_debug_scope (gnat_entity, NULL) |
af62ba41 EB |
3996 | == get_debug_scope (gnat_alias, NULL) |
3997 | && Name_Equals (Chars (gnat_entity), Chars (gnat_alias))) | |
1d4b96e0 AC |
3998 | && TREE_CODE (gnu_decl) == FUNCTION_DECL) |
3999 | { | |
4000 | tree decl = build_decl (input_location, IMPORTED_DECL, | |
4001 | gnu_entity_name, void_type_node); | |
4002 | IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl; | |
4003 | gnat_pushdecl (decl, gnat_entity); | |
4004 | } | |
4005 | ||
a1ab4c31 AC |
4006 | break; |
4007 | } | |
4008 | ||
1e55d29a EB |
4009 | /* Get the GCC tree for the (underlying) subprogram type. If the |
4010 | entity is an actual subprogram, also get the parameter list. */ | |
4011 | gnu_type | |
4012 | = gnat_to_gnu_subprog_type (gnat_entity, definition, debug_info_p, | |
4013 | &gnu_param_list); | |
7414a3c3 | 4014 | if (DECL_P (gnu_type)) |
1515785d | 4015 | { |
7414a3c3 EB |
4016 | gnu_decl = gnu_type; |
4017 | gnu_type = TREE_TYPE (gnu_decl); | |
4018 | break; | |
a1ab4c31 AC |
4019 | } |
4020 | ||
0567ae8d | 4021 | /* Deal with platform-specific calling conventions. */ |
a1ab4c31 | 4022 | if (Has_Stdcall_Convention (gnat_entity)) |
0567ae8d | 4023 | prepend_one_attribute |
a1ab4c31 AC |
4024 | (&attr_list, ATTR_MACHINE_ATTRIBUTE, |
4025 | get_identifier ("stdcall"), NULL_TREE, | |
4026 | gnat_entity); | |
4027 | ||
66194a98 | 4028 | /* If we should request stack realignment for a foreign convention |
0567ae8d AC |
4029 | subprogram, do so. Note that this applies to task entry points |
4030 | in particular. */ | |
0d0cd281 | 4031 | if (FOREIGN_FORCE_REALIGN_STACK && foreign) |
0567ae8d | 4032 | prepend_one_attribute |
a1ab4c31 AC |
4033 | (&attr_list, ATTR_MACHINE_ATTRIBUTE, |
4034 | get_identifier ("force_align_arg_pointer"), NULL_TREE, | |
4035 | gnat_entity); | |
4036 | ||
0567ae8d AC |
4037 | /* Deal with a pragma Linker_Section on a subprogram. */ |
4038 | if ((kind == E_Function || kind == E_Procedure) | |
4039 | && Present (Linker_Section_Pragma (gnat_entity))) | |
4040 | prepend_one_attribute_pragma (&attr_list, | |
4041 | Linker_Section_Pragma (gnat_entity)); | |
4042 | ||
a1ab4c31 AC |
4043 | /* If we are defining the subprogram and it has an Address clause |
4044 | we must get the address expression from the saved GCC tree for the | |
4045 | subprogram if it has a Freeze_Node. Otherwise, we elaborate | |
4046 | the address expression here since the front-end has guaranteed | |
4047 | in that case that the elaboration has no effects. If there is | |
4048 | an Address clause and we are not defining the object, just | |
4049 | make it a constant. */ | |
4050 | if (Present (Address_Clause (gnat_entity))) | |
4051 | { | |
4052 | tree gnu_address = NULL_TREE; | |
4053 | ||
4054 | if (definition) | |
4055 | gnu_address | |
4056 | = (present_gnu_tree (gnat_entity) | |
4057 | ? get_gnu_tree (gnat_entity) | |
4058 | : gnat_to_gnu (Expression (Address_Clause (gnat_entity)))); | |
4059 | ||
4060 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
4061 | ||
4062 | /* Convert the type of the object to a reference type that can | |
b3b5c6a2 | 4063 | alias everything as per RM 13.3(19). */ |
a1ab4c31 AC |
4064 | gnu_type |
4065 | = build_reference_type_for_mode (gnu_type, ptr_mode, true); | |
4066 | if (gnu_address) | |
4067 | gnu_address = convert (gnu_type, gnu_address); | |
4068 | ||
4069 | gnu_decl | |
0fb2335d | 4070 | = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type, |
a1ab4c31 | 4071 | gnu_address, false, Is_Public (gnat_entity), |
2056c5ed | 4072 | extern_flag, false, false, artificial_p, |
c1a569ef | 4073 | debug_info_p, NULL, gnat_entity); |
a1ab4c31 AC |
4074 | DECL_BY_REF_P (gnu_decl) = 1; |
4075 | } | |
4076 | ||
9182f718 | 4077 | /* If this is a mere subprogram type, just create the declaration. */ |
a1ab4c31 | 4078 | else if (kind == E_Subprogram_Type) |
74746d49 EB |
4079 | { |
4080 | process_attributes (&gnu_type, &attr_list, false, gnat_entity); | |
2056c5ed | 4081 | |
74746d49 | 4082 | gnu_decl |
c1a569ef | 4083 | = create_type_decl (gnu_entity_name, gnu_type, artificial_p, |
74746d49 EB |
4084 | debug_info_p, gnat_entity); |
4085 | } | |
1e55d29a | 4086 | |
9182f718 EB |
4087 | /* Otherwise create the subprogram declaration with the external name, |
4088 | the type and the parameter list. However, if this a reference to | |
4089 | the allocation routines, reuse the canonical declaration nodes as | |
4090 | they come with special properties. */ | |
a1ab4c31 AC |
4091 | else |
4092 | { | |
9182f718 EB |
4093 | if (extern_flag && gnu_ext_name == DECL_NAME (malloc_decl)) |
4094 | gnu_decl = malloc_decl; | |
4095 | else if (extern_flag && gnu_ext_name == DECL_NAME (realloc_decl)) | |
4096 | gnu_decl = realloc_decl; | |
4097 | else | |
4098 | { | |
4099 | gnu_decl | |
4100 | = create_subprog_decl (gnu_entity_name, gnu_ext_name, | |
4101 | gnu_type, gnu_param_list, | |
4102 | inline_status, public_flag, | |
4103 | extern_flag, artificial_p, | |
ff9baa5f PMR |
4104 | debug_info_p, |
4105 | definition && imported_p, attr_list, | |
4106 | gnat_entity); | |
9182f718 EB |
4107 | |
4108 | DECL_STUBBED_P (gnu_decl) | |
4109 | = (Convention (gnat_entity) == Convention_Stubbed); | |
4110 | } | |
a1ab4c31 AC |
4111 | } |
4112 | } | |
4113 | break; | |
4114 | ||
4115 | case E_Incomplete_Type: | |
4116 | case E_Incomplete_Subtype: | |
4117 | case E_Private_Type: | |
4118 | case E_Private_Subtype: | |
4119 | case E_Limited_Private_Type: | |
4120 | case E_Limited_Private_Subtype: | |
4121 | case E_Record_Type_With_Private: | |
4122 | case E_Record_Subtype_With_Private: | |
4123 | { | |
1e55d29a | 4124 | const bool is_from_limited_with |
bd769c83 | 4125 | = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity)); |
a1ab4c31 AC |
4126 | /* Get the "full view" of this entity. If this is an incomplete |
4127 | entity from a limited with, treat its non-limited view as the | |
4128 | full view. Otherwise, use either the full view or the underlying | |
4129 | full view, whichever is present. This is used in all the tests | |
4130 | below. */ | |
1e55d29a | 4131 | const Entity_Id full_view |
bd769c83 | 4132 | = is_from_limited_with |
a1ab4c31 AC |
4133 | ? Non_Limited_View (gnat_entity) |
4134 | : Present (Full_View (gnat_entity)) | |
4135 | ? Full_View (gnat_entity) | |
bf0b0e5e AC |
4136 | : IN (kind, Private_Kind) |
4137 | ? Underlying_Full_View (gnat_entity) | |
4138 | : Empty; | |
a1ab4c31 AC |
4139 | |
4140 | /* If this is an incomplete type with no full view, it must be a Taft | |
8234d02a EB |
4141 | Amendment type or an incomplete type coming from a limited context, |
4142 | in which cases we return a dummy type. Otherwise, we just get the | |
4143 | type from its Etype. */ | |
a1ab4c31 AC |
4144 | if (No (full_view)) |
4145 | { | |
4146 | if (kind == E_Incomplete_Type) | |
10069d53 EB |
4147 | { |
4148 | gnu_type = make_dummy_type (gnat_entity); | |
4149 | gnu_decl = TYPE_STUB_DECL (gnu_type); | |
4150 | } | |
a1ab4c31 AC |
4151 | else |
4152 | { | |
afc737f0 EB |
4153 | gnu_decl |
4154 | = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, false); | |
a1ab4c31 AC |
4155 | maybe_present = true; |
4156 | } | |
a1ab4c31 AC |
4157 | } |
4158 | ||
1e55d29a | 4159 | /* Or else, if we already made a type for the full view, reuse it. */ |
a1ab4c31 | 4160 | else if (present_gnu_tree (full_view)) |
1e55d29a | 4161 | gnu_decl = get_gnu_tree (full_view); |
a1ab4c31 | 4162 | |
1e55d29a EB |
4163 | /* Or else, if we are not defining the type or there is no freeze |
4164 | node on it, get the type for the full view. Likewise if this is | |
4165 | a limited_with'ed type not declared in the main unit, which can | |
4166 | happen for incomplete formal types instantiated on a type coming | |
4167 | from a limited_with clause. */ | |
a1ab4c31 | 4168 | else if (!definition |
1e55d29a | 4169 | || No (Freeze_Node (full_view)) |
bd769c83 EB |
4170 | || (is_from_limited_with |
4171 | && !In_Extended_Main_Code_Unit (full_view))) | |
a1ab4c31 | 4172 | { |
afc737f0 | 4173 | gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, false); |
a1ab4c31 | 4174 | maybe_present = true; |
a1ab4c31 AC |
4175 | } |
4176 | ||
1e55d29a EB |
4177 | /* Otherwise, make a dummy type entry which will be replaced later. |
4178 | Save it as the full declaration's type so we can do any needed | |
4179 | updates when we see it. */ | |
4180 | else | |
4181 | { | |
4182 | gnu_type = make_dummy_type (gnat_entity); | |
4183 | gnu_decl = TYPE_STUB_DECL (gnu_type); | |
4184 | if (Has_Completion_In_Body (gnat_entity)) | |
4185 | DECL_TAFT_TYPE_P (gnu_decl) = 1; | |
d5ebeb8c | 4186 | save_gnu_tree (full_view, gnu_decl, false); |
1e55d29a | 4187 | } |
a1ab4c31 | 4188 | } |
1e55d29a | 4189 | break; |
a1ab4c31 | 4190 | |
a1ab4c31 | 4191 | case E_Class_Wide_Type: |
f08863f9 | 4192 | /* Class-wide types are always transformed into their root type. */ |
afc737f0 | 4193 | gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false); |
a1ab4c31 AC |
4194 | maybe_present = true; |
4195 | break; | |
4196 | ||
a1ab4c31 AC |
4197 | case E_Protected_Type: |
4198 | case E_Protected_Subtype: | |
c4833de1 EB |
4199 | case E_Task_Type: |
4200 | case E_Task_Subtype: | |
4201 | /* If we are just annotating types and have no equivalent record type, | |
4202 | just return void_type, except for root types that have discriminants | |
4203 | because the discriminants will very likely be used in the declarative | |
4204 | part of the associated body so they need to be translated. */ | |
42a5e410 | 4205 | if (type_annotate_only && gnat_equiv_type == gnat_entity) |
c4833de1 | 4206 | { |
4453a822 EB |
4207 | if (definition |
4208 | && Has_Discriminants (gnat_entity) | |
c4833de1 EB |
4209 | && Root_Type (gnat_entity) == gnat_entity) |
4210 | { | |
4211 | tree gnu_field_list = NULL_TREE; | |
4212 | Entity_Id gnat_field; | |
4213 | ||
4214 | /* This is a minimal version of the E_Record_Type handling. */ | |
4215 | gnu_type = make_node (RECORD_TYPE); | |
4216 | TYPE_NAME (gnu_type) = gnu_entity_name; | |
4217 | ||
4218 | for (gnat_field = First_Stored_Discriminant (gnat_entity); | |
4219 | Present (gnat_field); | |
4220 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
4221 | { | |
4222 | tree gnu_field | |
4223 | = gnat_to_gnu_field (gnat_field, gnu_type, false, | |
4224 | definition, debug_info_p); | |
4225 | ||
4226 | save_gnu_tree (gnat_field, | |
4227 | build3 (COMPONENT_REF, TREE_TYPE (gnu_field), | |
4228 | build0 (PLACEHOLDER_EXPR, gnu_type), | |
4229 | gnu_field, NULL_TREE), | |
4230 | true); | |
4231 | ||
4232 | DECL_CHAIN (gnu_field) = gnu_field_list; | |
4233 | gnu_field_list = gnu_field; | |
4234 | } | |
4235 | ||
68ec5613 EB |
4236 | finish_record_type (gnu_type, nreverse (gnu_field_list), 0, |
4237 | false); | |
c4833de1 EB |
4238 | } |
4239 | else | |
4240 | gnu_type = void_type_node; | |
4241 | } | |
4242 | ||
4243 | /* Concurrent types are always transformed into their record type. */ | |
a1ab4c31 | 4244 | else |
afc737f0 | 4245 | gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, false); |
a1ab4c31 AC |
4246 | maybe_present = true; |
4247 | break; | |
4248 | ||
4249 | case E_Label: | |
88a94e2b | 4250 | gnu_decl = create_label_decl (gnu_entity_name, gnat_entity); |
a1ab4c31 AC |
4251 | break; |
4252 | ||
4253 | case E_Block: | |
4254 | case E_Loop: | |
4255 | /* Nothing at all to do here, so just return an ERROR_MARK and claim | |
4256 | we've already saved it, so we don't try to. */ | |
4257 | gnu_decl = error_mark_node; | |
4258 | saved = true; | |
4259 | break; | |
4260 | ||
d2c03c72 EB |
4261 | case E_Abstract_State: |
4262 | /* This is a SPARK annotation that only reaches here when compiling in | |
c8dbf886 | 4263 | ASIS mode. */ |
d2c03c72 | 4264 | gcc_assert (type_annotate_only); |
c8dbf886 EB |
4265 | gnu_decl = error_mark_node; |
4266 | saved = true; | |
4267 | break; | |
d2c03c72 | 4268 | |
a1ab4c31 AC |
4269 | default: |
4270 | gcc_unreachable (); | |
4271 | } | |
4272 | ||
4273 | /* If we had a case where we evaluated another type and it might have | |
4274 | defined this one, handle it here. */ | |
4275 | if (maybe_present && present_gnu_tree (gnat_entity)) | |
4276 | { | |
4277 | gnu_decl = get_gnu_tree (gnat_entity); | |
4278 | saved = true; | |
4279 | } | |
4280 | ||
f2bee239 | 4281 | /* If we are processing a type and there is either no DECL for it or |
a1ab4c31 AC |
4282 | we just made one, do some common processing for the type, such as |
4283 | handling alignment and possible padding. */ | |
a8e05f92 | 4284 | if (is_type && (!gnu_decl || this_made_decl)) |
a1ab4c31 | 4285 | { |
f1f5b1fb EB |
4286 | const bool is_by_ref = Is_By_Reference_Type (gnat_entity); |
4287 | ||
d5ebeb8c EB |
4288 | gcc_assert (!TYPE_IS_DUMMY_P (gnu_type)); |
4289 | ||
74746d49 | 4290 | /* Process the attributes, if not already done. Note that the type is |
78df6221 | 4291 | already defined so we cannot pass true for IN_PLACE here. */ |
74746d49 EB |
4292 | process_attributes (&gnu_type, &attr_list, false, gnat_entity); |
4293 | ||
8623afc4 EB |
4294 | /* See if a size was specified, by means of either an Object_Size or |
4295 | a regular Size clause, and validate it if so. | |
4296 | ||
4297 | ??? Don't set the size for a String_Literal since it is either | |
a1ab4c31 AC |
4298 | confirming or we don't handle it properly (if the low bound is |
4299 | non-constant). */ | |
4300 | if (!gnu_size && kind != E_String_Literal_Subtype) | |
fc893455 | 4301 | { |
f1f5b1fb EB |
4302 | const char *size_s = "size for %s too small{, minimum allowed is ^}"; |
4303 | const char *type_s = is_by_ref ? "by-reference type &" : "&"; | |
4304 | ||
3a4425fd EB |
4305 | if (Known_Esize (gnat_entity)) |
4306 | gnu_size | |
4307 | = validate_size (Esize (gnat_entity), gnu_type, gnat_entity, | |
f1f5b1fb | 4308 | VAR_DECL, false, false, size_s, type_s); |
b23cdc01 BD |
4309 | |
4310 | /* ??? The test on Has_Size_Clause must be removed when "unknown" is | |
4311 | no longer represented as Uint_0 (i.e. Use_New_Unknown_Rep). */ | |
4312 | else if (Known_RM_Size (gnat_entity) | |
4313 | || Has_Size_Clause (gnat_entity)) | |
3a4425fd EB |
4314 | gnu_size |
4315 | = validate_size (RM_Size (gnat_entity), gnu_type, gnat_entity, | |
4316 | TYPE_DECL, false, Has_Size_Clause (gnat_entity), | |
f1f5b1fb | 4317 | size_s, type_s); |
fc893455 | 4318 | } |
a1ab4c31 AC |
4319 | |
4320 | /* If a size was specified, see if we can make a new type of that size | |
4321 | by rearranging the type, for example from a fat to a thin pointer. */ | |
4322 | if (gnu_size) | |
4323 | { | |
4324 | gnu_type | |
4325 | = make_type_from_size (gnu_type, gnu_size, | |
4326 | Has_Biased_Representation (gnat_entity)); | |
4327 | ||
4328 | if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0) | |
4329 | && operand_equal_p (rm_size (gnu_type), gnu_size, 0)) | |
842d4ee2 | 4330 | gnu_size = NULL_TREE; |
a1ab4c31 AC |
4331 | } |
4332 | ||
4aecc2f8 EB |
4333 | /* If the alignment has not already been processed and this is not |
4334 | an unconstrained array type, see if an alignment is specified. | |
a1ab4c31 AC |
4335 | If not, we pick a default alignment for atomic objects. */ |
4336 | if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
4337 | ; | |
4338 | else if (Known_Alignment (gnat_entity)) | |
4339 | { | |
4340 | align = validate_alignment (Alignment (gnat_entity), gnat_entity, | |
4341 | TYPE_ALIGN (gnu_type)); | |
4342 | ||
4343 | /* Warn on suspiciously large alignments. This should catch | |
4344 | errors about the (alignment,byte)/(size,bit) discrepancy. */ | |
4345 | if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity)) | |
4346 | { | |
4347 | tree size; | |
4348 | ||
4349 | /* If a size was specified, take it into account. Otherwise | |
e1e5852c EB |
4350 | use the RM size for records or unions as the type size has |
4351 | already been adjusted to the alignment. */ | |
a1ab4c31 AC |
4352 | if (gnu_size) |
4353 | size = gnu_size; | |
e1e5852c | 4354 | else if (RECORD_OR_UNION_TYPE_P (gnu_type) |
315cff15 | 4355 | && !TYPE_FAT_POINTER_P (gnu_type)) |
a1ab4c31 AC |
4356 | size = rm_size (gnu_type); |
4357 | else | |
4358 | size = TYPE_SIZE (gnu_type); | |
4359 | ||
4360 | /* Consider an alignment as suspicious if the alignment/size | |
4361 | ratio is greater or equal to the byte/bit ratio. */ | |
cc269bb6 | 4362 | if (tree_fits_uhwi_p (size) |
eb1ce453 | 4363 | && align >= tree_to_uhwi (size) * BITS_PER_UNIT) |
4a29b8d6 | 4364 | post_error_ne ("??suspiciously large alignment specified for&", |
a1ab4c31 AC |
4365 | Expression (Alignment_Clause (gnat_entity)), |
4366 | gnat_entity); | |
4367 | } | |
4368 | } | |
b120ca61 | 4369 | else if (Is_Full_Access (gnat_entity) && !gnu_size |
cc269bb6 | 4370 | && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)) |
a1ab4c31 AC |
4371 | && integer_pow2p (TYPE_SIZE (gnu_type))) |
4372 | align = MIN (BIGGEST_ALIGNMENT, | |
ae7e9ddd | 4373 | tree_to_uhwi (TYPE_SIZE (gnu_type))); |
b120ca61 | 4374 | else if (Is_Full_Access (gnat_entity) && gnu_size |
cc269bb6 | 4375 | && tree_fits_uhwi_p (gnu_size) |
a1ab4c31 | 4376 | && integer_pow2p (gnu_size)) |
ae7e9ddd | 4377 | align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size)); |
a1ab4c31 | 4378 | |
1e3cabd4 EB |
4379 | /* See if we need to pad the type. If we did and built a new type, |
4380 | then create a stripped-down declaration for the original type, | |
4381 | mainly for debugging, unless there was already one. */ | |
a1ab4c31 | 4382 | if (gnu_size || align > 0) |
1e3cabd4 EB |
4383 | { |
4384 | tree orig_type = gnu_type; | |
4385 | ||
4386 | gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, | |
4387 | false, definition, false); | |
a1ab4c31 | 4388 | |
1e3cabd4 EB |
4389 | if (gnu_type != orig_type && !gnu_decl) |
4390 | create_type_decl (gnu_entity_name, orig_type, true, debug_info_p, | |
4391 | gnat_entity); | |
4392 | } | |
a1ab4c31 | 4393 | |
842d4ee2 EB |
4394 | /* Now set the RM size of the type. We cannot do it before padding |
4395 | because we need to accept arbitrary RM sizes on integral types. */ | |
b23cdc01 BD |
4396 | if (Known_RM_Size (gnat_entity)) |
4397 | set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity); | |
a1ab4c31 | 4398 | |
f2bee239 | 4399 | /* Back-annotate the alignment of the type if not already set. */ |
8de68eb3 | 4400 | if (!Known_Alignment (gnat_entity)) |
f2bee239 EB |
4401 | { |
4402 | unsigned int double_align, align; | |
4403 | bool is_capped_double, align_clause; | |
4404 | ||
4405 | /* If the default alignment of "double" or larger scalar types is | |
4406 | specifically capped and this is not an array with an alignment | |
4407 | clause on the component type, return the cap. */ | |
4408 | if ((double_align = double_float_alignment) > 0) | |
4409 | is_capped_double | |
4410 | = is_double_float_or_array (gnat_entity, &align_clause); | |
4411 | else if ((double_align = double_scalar_alignment) > 0) | |
4412 | is_capped_double | |
4413 | = is_double_scalar_or_array (gnat_entity, &align_clause); | |
4414 | else | |
4415 | is_capped_double = align_clause = false; | |
4416 | ||
4417 | if (is_capped_double && !align_clause) | |
4418 | align = double_align; | |
4419 | else | |
4420 | align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT; | |
4421 | ||
4422 | Set_Alignment (gnat_entity, UI_From_Int (align)); | |
4423 | } | |
4424 | ||
4425 | /* Likewise for the size, if any. */ | |
8de68eb3 | 4426 | if (!Known_Esize (gnat_entity) && TYPE_SIZE (gnu_type)) |
f2bee239 | 4427 | { |
b23cdc01 | 4428 | tree size = TYPE_SIZE (gnu_type); |
f2bee239 | 4429 | |
875bdbe2 EB |
4430 | /* If the size is self-referential, annotate the maximum value |
4431 | after saturating it, if need be, to avoid a No_Uint value. */ | |
b23cdc01 | 4432 | if (CONTAINS_PLACEHOLDER_P (size)) |
88795e14 EB |
4433 | { |
4434 | const unsigned int align | |
4435 | = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT; | |
b23cdc01 | 4436 | size = maybe_saturate_size (max_size (size, true), align); |
88795e14 | 4437 | } |
f2bee239 EB |
4438 | |
4439 | /* If we are just annotating types and the type is tagged, the tag | |
4440 | and the parent components are not generated by the front-end so | |
8623afc4 EB |
4441 | alignment and sizes must be adjusted. */ |
4442 | if (type_annotate_only && Is_Tagged_Type (gnat_entity)) | |
f2bee239 | 4443 | { |
8623afc4 EB |
4444 | const bool derived_p = Is_Derived_Type (gnat_entity); |
4445 | const Entity_Id gnat_parent | |
4446 | = derived_p ? Etype (Base_Type (gnat_entity)) : Empty; | |
0c8ff35e BD |
4447 | /* The following test for Known_Alignment preserves the old behavior, |
4448 | but is probably wrong. */ | |
8623afc4 EB |
4449 | const unsigned int inherited_align |
4450 | = derived_p | |
0c8ff35e BD |
4451 | ? (Known_Alignment (gnat_parent) |
4452 | ? UI_To_Int (Alignment (gnat_parent)) * BITS_PER_UNIT | |
4453 | : 0) | |
8623afc4 EB |
4454 | : POINTER_SIZE; |
4455 | const unsigned int align | |
4456 | = MAX (TYPE_ALIGN (gnu_type), inherited_align); | |
4457 | ||
4458 | Set_Alignment (gnat_entity, UI_From_Int (align / BITS_PER_UNIT)); | |
4459 | ||
4460 | /* If there is neither size clause nor representation clause, the | |
4461 | sizes need to be adjusted. */ | |
8de68eb3 | 4462 | if (!Known_RM_Size (gnat_entity) |
8623afc4 EB |
4463 | && !VOID_TYPE_P (gnu_type) |
4464 | && (!TYPE_FIELDS (gnu_type) | |
4465 | || integer_zerop (bit_position (TYPE_FIELDS (gnu_type))))) | |
f2bee239 | 4466 | { |
8623afc4 EB |
4467 | tree offset |
4468 | = derived_p | |
4469 | ? UI_To_gnu (Esize (gnat_parent), bitsizetype) | |
4470 | : bitsize_int (POINTER_SIZE); | |
4471 | if (TYPE_FIELDS (gnu_type)) | |
4472 | offset | |
4473 | = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type))); | |
b23cdc01 | 4474 | size = size_binop (PLUS_EXPR, size, offset); |
f2bee239 | 4475 | } |
f2bee239 | 4476 | |
b23cdc01 BD |
4477 | size = maybe_saturate_size (round_up (size, align), align); |
4478 | Set_Esize (gnat_entity, annotate_value (size)); | |
8623afc4 EB |
4479 | |
4480 | /* Tagged types are Strict_Alignment so RM_Size = Esize. */ | |
8de68eb3 | 4481 | if (!Known_RM_Size (gnat_entity)) |
8623afc4 | 4482 | Set_RM_Size (gnat_entity, Esize (gnat_entity)); |
f2bee239 EB |
4483 | } |
4484 | ||
4485 | /* Otherwise no adjustment is needed. */ | |
4486 | else | |
b23cdc01 | 4487 | Set_Esize (gnat_entity, No_Uint_To_0 (annotate_value (size))); |
f2bee239 EB |
4488 | } |
4489 | ||
4490 | /* Likewise for the RM size, if any. */ | |
8de68eb3 | 4491 | if (!Known_RM_Size (gnat_entity) && TYPE_SIZE (gnu_type)) |
b23cdc01 BD |
4492 | Set_RM_Size (gnat_entity, |
4493 | No_Uint_To_0 (annotate_value (rm_size (gnu_type)))); | |
f2bee239 | 4494 | |
3553d8c2 EB |
4495 | /* If we are at global level, GCC applied variable_size to the size but |
4496 | this has done nothing. So, if it's not constant or self-referential, | |
4497 | call elaborate_expression_1 to make a variable for it rather than | |
4498 | calculating it each time. */ | |
b0ad2d78 | 4499 | if (TYPE_SIZE (gnu_type) |
a1ab4c31 | 4500 | && !TREE_CONSTANT (TYPE_SIZE (gnu_type)) |
b0ad2d78 EB |
4501 | && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)) |
4502 | && global_bindings_p ()) | |
a1ab4c31 | 4503 | { |
3553d8c2 | 4504 | tree orig_size = TYPE_SIZE (gnu_type); |
da01bfee EB |
4505 | |
4506 | TYPE_SIZE (gnu_type) | |
3553d8c2 EB |
4507 | = elaborate_expression_1 (TYPE_SIZE (gnu_type), gnat_entity, |
4508 | "SIZE", definition, false); | |
da01bfee EB |
4509 | |
4510 | /* ??? For now, store the size as a multiple of the alignment in | |
4511 | bytes so that we can see the alignment from the tree. */ | |
4512 | TYPE_SIZE_UNIT (gnu_type) | |
4513 | = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity, | |
bf44701f | 4514 | "SIZE_A_UNIT", definition, false, |
da01bfee EB |
4515 | TYPE_ALIGN (gnu_type)); |
4516 | ||
4517 | /* ??? gnu_type may come from an existing type so the MULT_EXPR node | |
4518 | may not be marked by the call to create_type_decl below. */ | |
4519 | MARK_VISITED (TYPE_SIZE_UNIT (gnu_type)); | |
4520 | ||
3553d8c2 EB |
4521 | /* For a record type, deal with the variant part, if any, and handle |
4522 | the Ada size as well. */ | |
4523 | if (RECORD_OR_UNION_TYPE_P (gnu_type)) | |
a1ab4c31 | 4524 | { |
35e2a4b8 | 4525 | tree variant_part = get_variant_part (gnu_type); |
da01bfee | 4526 | tree ada_size = TYPE_ADA_SIZE (gnu_type); |
a1ab4c31 | 4527 | |
35e2a4b8 EB |
4528 | if (variant_part) |
4529 | { | |
4530 | tree union_type = TREE_TYPE (variant_part); | |
4531 | tree offset = DECL_FIELD_OFFSET (variant_part); | |
4532 | ||
4533 | /* If the position of the variant part is constant, subtract | |
4534 | it from the size of the type of the parent to get the new | |
4535 | size. This manual CSE reduces the data size. */ | |
4536 | if (TREE_CODE (offset) == INTEGER_CST) | |
4537 | { | |
4538 | tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part); | |
4539 | TYPE_SIZE (union_type) | |
4540 | = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type), | |
4541 | bit_from_pos (offset, bitpos)); | |
4542 | TYPE_SIZE_UNIT (union_type) | |
4543 | = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type), | |
4544 | byte_from_pos (offset, bitpos)); | |
4545 | } | |
4546 | else | |
4547 | { | |
4548 | TYPE_SIZE (union_type) | |
4549 | = elaborate_expression_1 (TYPE_SIZE (union_type), | |
bf44701f | 4550 | gnat_entity, "VSIZE", |
35e2a4b8 EB |
4551 | definition, false); |
4552 | ||
4553 | /* ??? For now, store the size as a multiple of the | |
4554 | alignment in bytes so that we can see the alignment | |
4555 | from the tree. */ | |
4556 | TYPE_SIZE_UNIT (union_type) | |
4557 | = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type), | |
bf44701f | 4558 | gnat_entity, "VSIZE_A_UNIT", |
35e2a4b8 EB |
4559 | definition, false, |
4560 | TYPE_ALIGN (union_type)); | |
4561 | ||
4562 | /* ??? For now, store the offset as a multiple of the | |
4563 | alignment in bytes so that we can see the alignment | |
4564 | from the tree. */ | |
4565 | DECL_FIELD_OFFSET (variant_part) | |
bf44701f EB |
4566 | = elaborate_expression_2 (offset, gnat_entity, |
4567 | "VOFFSET", definition, false, | |
35e2a4b8 EB |
4568 | DECL_OFFSET_ALIGN |
4569 | (variant_part)); | |
4570 | } | |
4571 | ||
4572 | DECL_SIZE (variant_part) = TYPE_SIZE (union_type); | |
4573 | DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type); | |
4574 | } | |
4575 | ||
3553d8c2 | 4576 | if (operand_equal_p (ada_size, orig_size, 0)) |
da01bfee EB |
4577 | ada_size = TYPE_SIZE (gnu_type); |
4578 | else | |
4579 | ada_size | |
bf44701f | 4580 | = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE", |
da01bfee EB |
4581 | definition, false); |
4582 | SET_TYPE_ADA_SIZE (gnu_type, ada_size); | |
4583 | } | |
a1ab4c31 AC |
4584 | } |
4585 | ||
b0ad2d78 EB |
4586 | /* Similarly, if this is a record type or subtype at global level, call |
4587 | elaborate_expression_2 on any field position. Skip any fields that | |
4588 | we haven't made trees for to avoid problems with class-wide types. */ | |
76f9c7f4 | 4589 | if (Is_In_Record_Kind (kind) && global_bindings_p ()) |
a1ab4c31 AC |
4590 | for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp); |
4591 | gnat_temp = Next_Entity (gnat_temp)) | |
4592 | if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp)) | |
4593 | { | |
4594 | tree gnu_field = get_gnu_tree (gnat_temp); | |
4595 | ||
da01bfee EB |
4596 | /* ??? For now, store the offset as a multiple of the alignment |
4597 | in bytes so that we can see the alignment from the tree. */ | |
b0ad2d78 EB |
4598 | if (!TREE_CONSTANT (DECL_FIELD_OFFSET (gnu_field)) |
4599 | && !CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field))) | |
a1ab4c31 | 4600 | { |
da01bfee EB |
4601 | DECL_FIELD_OFFSET (gnu_field) |
4602 | = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field), | |
bf44701f EB |
4603 | gnat_temp, "OFFSET", definition, |
4604 | false, | |
da01bfee EB |
4605 | DECL_OFFSET_ALIGN (gnu_field)); |
4606 | ||
4607 | /* ??? The context of gnu_field is not necessarily gnu_type | |
4608 | so the MULT_EXPR node built above may not be marked by | |
4609 | the call to create_type_decl below. */ | |
b0ad2d78 | 4610 | MARK_VISITED (DECL_FIELD_OFFSET (gnu_field)); |
a1ab4c31 AC |
4611 | } |
4612 | } | |
4613 | ||
b1af4cb2 | 4614 | /* Now check if the type allows atomic access. */ |
b120ca61 | 4615 | if (Is_Full_Access (gnat_entity)) |
86a8ba5b | 4616 | check_ok_for_atomic_type (gnu_type, gnat_entity, false); |
a1ab4c31 | 4617 | |
4aecc2f8 EB |
4618 | /* If this is not an unconstrained array type, set some flags. */ |
4619 | if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE) | |
4620 | { | |
57d0f7c6 | 4621 | /* Record the property that objects of tagged types are guaranteed to |
ea09ecc5 EB |
4622 | be properly aligned. This is necessary because conversions to the |
4623 | class-wide type are translated into conversions to the root type, | |
4624 | which can be less aligned than some of its derived types. */ | |
4625 | if (Is_Tagged_Type (gnat_entity) | |
4626 | || Is_Class_Wide_Equivalent_Type (gnat_entity)) | |
4627 | TYPE_ALIGN_OK (gnu_type) = 1; | |
4628 | ||
4629 | /* Record whether the type is passed by reference. */ | |
f1f5b1fb | 4630 | if (is_by_ref && !VOID_TYPE_P (gnu_type)) |
ea09ecc5 EB |
4631 | TYPE_BY_REFERENCE_P (gnu_type) = 1; |
4632 | ||
4633 | /* Record whether an alignment clause was specified. */ | |
4aecc2f8 EB |
4634 | if (Present (Alignment_Clause (gnat_entity))) |
4635 | TYPE_USER_ALIGN (gnu_type) = 1; | |
4636 | ||
ea09ecc5 | 4637 | /* Record whether a pragma Universal_Aliasing was specified. */ |
1e55d29a | 4638 | if (Universal_Aliasing (gnat_entity) && !TYPE_IS_DUMMY_P (gnu_type)) |
f797c2b7 EB |
4639 | TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1; |
4640 | ||
4641 | /* If it is passed by reference, force BLKmode to ensure that | |
4642 | objects of this type will always be put in memory. */ | |
ea09ecc5 | 4643 | if (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type)) |
f797c2b7 | 4644 | SET_TYPE_MODE (gnu_type, BLKmode); |
4aecc2f8 | 4645 | } |
a1ab4c31 | 4646 | |
794511d2 EB |
4647 | /* If this is a derived type, relate its alias set to that of its parent |
4648 | to avoid troubles when a call to an inherited primitive is inlined in | |
4649 | a context where a derived object is accessed. The inlined code works | |
4650 | on the parent view so the resulting code may access the same object | |
4651 | using both the parent and the derived alias sets, which thus have to | |
4652 | conflict. As the same issue arises with component references, the | |
4653 | parent alias set also has to conflict with composite types enclosing | |
4654 | derived components. For instance, if we have: | |
4655 | ||
4656 | type D is new T; | |
4657 | type R is record | |
4658 | Component : D; | |
4659 | end record; | |
4660 | ||
4661 | we want T to conflict with both D and R, in addition to R being a | |
4662 | superset of D by record/component construction. | |
4663 | ||
4664 | One way to achieve this is to perform an alias set copy from the | |
4665 | parent to the derived type. This is not quite appropriate, though, | |
4666 | as we don't want separate derived types to conflict with each other: | |
4667 | ||
4668 | type I1 is new Integer; | |
4669 | type I2 is new Integer; | |
4670 | ||
4671 | We want I1 and I2 to both conflict with Integer but we do not want | |
4672 | I1 to conflict with I2, and an alias set copy on derivation would | |
4673 | have that effect. | |
4674 | ||
4675 | The option chosen is to make the alias set of the derived type a | |
4676 | superset of that of its parent type. It trivially fulfills the | |
4677 | simple requirement for the Integer derivation example above, and | |
4678 | the component case as well by superset transitivity: | |
4679 | ||
4680 | superset superset | |
4681 | R ----------> D ----------> T | |
4682 | ||
d8e94f79 EB |
4683 | However, for composite types, conversions between derived types are |
4684 | translated into VIEW_CONVERT_EXPRs so a sequence like: | |
4685 | ||
4686 | type Comp1 is new Comp; | |
4687 | type Comp2 is new Comp; | |
4688 | procedure Proc (C : Comp1); | |
4689 | ||
4690 | C : Comp2; | |
4691 | Proc (Comp1 (C)); | |
4692 | ||
4693 | is translated into: | |
4694 | ||
4695 | C : Comp2; | |
4696 | Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C)); | |
4697 | ||
4698 | and gimplified into: | |
4699 | ||
4700 | C : Comp2; | |
4701 | Comp1 *C.0; | |
4702 | C.0 = (Comp1 *) &C; | |
4703 | Proc (C.0); | |
4704 | ||
4705 | i.e. generates code involving type punning. Therefore, Comp1 needs | |
4706 | to conflict with Comp2 and an alias set copy is required. | |
4707 | ||
794511d2 | 4708 | The language rules ensure the parent type is already frozen here. */ |
9d11273c EB |
4709 | if (kind != E_Subprogram_Type |
4710 | && Is_Derived_Type (gnat_entity) | |
4711 | && !type_annotate_only) | |
794511d2 | 4712 | { |
384e3fb1 | 4713 | Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity)); |
8c44fc0f EB |
4714 | /* For constrained packed array subtypes, the implementation type is |
4715 | used instead of the nominal type. */ | |
384e3fb1 | 4716 | if (kind == E_Array_Subtype |
8c44fc0f | 4717 | && Is_Constrained (gnat_entity) |
384e3fb1 JM |
4718 | && Present (Packed_Array_Impl_Type (gnat_parent_type))) |
4719 | gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type); | |
4720 | relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type), | |
d8e94f79 EB |
4721 | Is_Composite_Type (gnat_entity) |
4722 | ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET); | |
794511d2 EB |
4723 | } |
4724 | ||
773076a5 EB |
4725 | /* Finally get to the appropriate variant, except for the implementation |
4726 | type of a packed array because the GNU type might be further adjusted | |
4727 | when the original array type is itself processed. */ | |
4728 | if (Treat_As_Volatile (gnat_entity) | |
4729 | && !Is_Packed_Array_Impl_Type (gnat_entity)) | |
41683e1a EB |
4730 | { |
4731 | const int quals | |
4732 | = TYPE_QUAL_VOLATILE | |
b120ca61 | 4733 | | (Is_Full_Access (gnat_entity) ? TYPE_QUAL_ATOMIC : 0); |
1c3c12b0 EB |
4734 | /* This is required by free_lang_data_in_type to disable the ODR. */ |
4735 | if (TREE_CODE (gnu_type) == ENUMERAL_TYPE) | |
4736 | TYPE_STUB_DECL (gnu_type) | |
4737 | = create_type_stub_decl (TYPE_NAME (gnu_type), gnu_type); | |
41683e1a EB |
4738 | gnu_type = change_qualified_type (gnu_type, quals); |
4739 | } | |
4740 | ||
4d39941e EB |
4741 | /* If we already made a decl, just set the type, otherwise create it. */ |
4742 | if (gnu_decl) | |
d5ebeb8c EB |
4743 | { |
4744 | TREE_TYPE (gnu_decl) = gnu_type; | |
4745 | TYPE_STUB_DECL (gnu_type) = gnu_decl; | |
4746 | } | |
4d39941e EB |
4747 | else |
4748 | gnu_decl = create_type_decl (gnu_entity_name, gnu_type, artificial_p, | |
4749 | debug_info_p, gnat_entity); | |
b9c35857 EB |
4750 | |
4751 | /* For vector types, make the representative array the debug type. */ | |
4752 | if (VECTOR_TYPE_P (gnu_type)) | |
4753 | { | |
4754 | tree rep = TYPE_REPRESENTATIVE_ARRAY (gnu_type); | |
4755 | TYPE_NAME (rep) = DECL_NAME (gnu_decl); | |
4756 | SET_TYPE_DEBUG_TYPE (gnu_type, rep); | |
4757 | } | |
d5ebeb8c EB |
4758 | } |
4759 | ||
f2bee239 EB |
4760 | /* Otherwise, for a type reusing an existing DECL, back-annotate values. */ |
4761 | else if (is_type | |
4762 | && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)) | |
4763 | && Present (gnat_annotate_type)) | |
d5ebeb8c | 4764 | { |
8de68eb3 | 4765 | if (!Known_Alignment (gnat_entity)) |
0c8ff35e | 4766 | Copy_Alignment (gnat_entity, gnat_annotate_type); |
8de68eb3 | 4767 | if (!Known_Esize (gnat_entity)) |
b23cdc01 | 4768 | Copy_Esize (gnat_entity, gnat_annotate_type); |
8de68eb3 | 4769 | if (!Known_RM_Size (gnat_entity)) |
b23cdc01 | 4770 | Copy_RM_Size (gnat_entity, gnat_annotate_type); |
a1ab4c31 AC |
4771 | } |
4772 | ||
a1ab4c31 | 4773 | /* If we haven't already, associate the ..._DECL node that we just made with |
2ddc34ba | 4774 | the input GNAT entity node. */ |
a1ab4c31 AC |
4775 | if (!saved) |
4776 | save_gnu_tree (gnat_entity, gnu_decl, false); | |
4777 | ||
9a30c7c4 AC |
4778 | /* Now we are sure gnat_entity has a corresponding ..._DECL node, |
4779 | eliminate as many deferred computations as possible. */ | |
4780 | process_deferred_decl_context (false); | |
4781 | ||
c1abd261 EB |
4782 | /* If this is an enumeration or floating-point type, we were not able to set |
4783 | the bounds since they refer to the type. These are always static. */ | |
a1ab4c31 | 4784 | if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity))) |
e08add8e | 4785 | || (kind == E_Floating_Point_Type)) |
a1ab4c31 AC |
4786 | { |
4787 | tree gnu_scalar_type = gnu_type; | |
84fb43a1 | 4788 | tree gnu_low_bound, gnu_high_bound; |
a1ab4c31 AC |
4789 | |
4790 | /* If this is a padded type, we need to use the underlying type. */ | |
315cff15 | 4791 | if (TYPE_IS_PADDING_P (gnu_scalar_type)) |
a1ab4c31 AC |
4792 | gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type)); |
4793 | ||
4794 | /* If this is a floating point type and we haven't set a floating | |
4795 | point type yet, use this in the evaluation of the bounds. */ | |
4796 | if (!longest_float_type_node && kind == E_Floating_Point_Type) | |
c1abd261 | 4797 | longest_float_type_node = gnu_scalar_type; |
a1ab4c31 | 4798 | |
84fb43a1 EB |
4799 | gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity)); |
4800 | gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity)); | |
a1ab4c31 | 4801 | |
c1abd261 | 4802 | if (kind == E_Enumeration_Type) |
a1ab4c31 | 4803 | { |
84fb43a1 EB |
4804 | /* Enumeration types have specific RM bounds. */ |
4805 | SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound); | |
4806 | SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound); | |
a1ab4c31 | 4807 | } |
84fb43a1 EB |
4808 | else |
4809 | { | |
4810 | /* Floating-point types don't have specific RM bounds. */ | |
4811 | TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound; | |
4812 | TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound; | |
4813 | } | |
a1ab4c31 AC |
4814 | } |
4815 | ||
4816 | /* If we deferred processing of incomplete types, re-enable it. If there | |
80ec8b4c EB |
4817 | were no other disables and we have deferred types to process, do so. */ |
4818 | if (this_deferred | |
4819 | && --defer_incomplete_level == 0 | |
4820 | && defer_incomplete_list) | |
a1ab4c31 | 4821 | { |
80ec8b4c | 4822 | struct incomplete *p, *next; |
a1ab4c31 | 4823 | |
80ec8b4c EB |
4824 | /* We are back to level 0 for the deferring of incomplete types. |
4825 | But processing these incomplete types below may itself require | |
4826 | deferring, so preserve what we have and restart from scratch. */ | |
4827 | p = defer_incomplete_list; | |
4828 | defer_incomplete_list = NULL; | |
a1ab4c31 | 4829 | |
80ec8b4c EB |
4830 | for (; p; p = next) |
4831 | { | |
4832 | next = p->next; | |
a1ab4c31 | 4833 | |
80ec8b4c EB |
4834 | if (p->old_type) |
4835 | update_pointer_to (TYPE_MAIN_VARIANT (p->old_type), | |
4836 | gnat_to_gnu_type (p->full_type)); | |
4837 | free (p); | |
a1ab4c31 | 4838 | } |
a1ab4c31 AC |
4839 | } |
4840 | ||
6ddf9843 EB |
4841 | /* If we are not defining this type, see if it's on one of the lists of |
4842 | incomplete types. If so, handle the list entry now. */ | |
4843 | if (is_type && !definition) | |
a1ab4c31 | 4844 | { |
6ddf9843 | 4845 | struct incomplete *p; |
a1ab4c31 | 4846 | |
6ddf9843 EB |
4847 | for (p = defer_incomplete_list; p; p = p->next) |
4848 | if (p->old_type && p->full_type == gnat_entity) | |
a1ab4c31 | 4849 | { |
6ddf9843 | 4850 | update_pointer_to (TYPE_MAIN_VARIANT (p->old_type), |
a1ab4c31 | 4851 | TREE_TYPE (gnu_decl)); |
6ddf9843 EB |
4852 | p->old_type = NULL_TREE; |
4853 | } | |
4854 | ||
1e55d29a | 4855 | for (p = defer_limited_with_list; p; p = p->next) |
d3271136 EB |
4856 | if (p->old_type |
4857 | && (Non_Limited_View (p->full_type) == gnat_entity | |
4858 | || Full_View (p->full_type) == gnat_entity)) | |
6ddf9843 EB |
4859 | { |
4860 | update_pointer_to (TYPE_MAIN_VARIANT (p->old_type), | |
4861 | TREE_TYPE (gnu_decl)); | |
7414a3c3 EB |
4862 | if (TYPE_DUMMY_IN_PROFILE_P (p->old_type)) |
4863 | update_profiles_with (p->old_type); | |
6ddf9843 | 4864 | p->old_type = NULL_TREE; |
a1ab4c31 AC |
4865 | } |
4866 | } | |
4867 | ||
4868 | if (this_global) | |
4869 | force_global--; | |
4870 | ||
b4680ca1 | 4871 | /* If this is a packed array type whose original array type is itself |
af62ba41 | 4872 | an itype without freeze node, make sure the latter is processed. */ |
1a4cb227 | 4873 | if (Is_Packed_Array_Impl_Type (gnat_entity) |
b4680ca1 EB |
4874 | && Is_Itype (Original_Array_Type (gnat_entity)) |
4875 | && No (Freeze_Node (Original_Array_Type (gnat_entity))) | |
4876 | && !present_gnu_tree (Original_Array_Type (gnat_entity))) | |
afc737f0 | 4877 | gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, false); |
a1ab4c31 AC |
4878 | |
4879 | return gnu_decl; | |
4880 | } | |
4881 | ||
4882 | /* Similar, but if the returned value is a COMPONENT_REF, return the | |
4883 | FIELD_DECL. */ | |
4884 | ||
4885 | tree | |
4886 | gnat_to_gnu_field_decl (Entity_Id gnat_entity) | |
4887 | { | |
afc737f0 | 4888 | tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
a1ab4c31 AC |
4889 | |
4890 | if (TREE_CODE (gnu_field) == COMPONENT_REF) | |
4891 | gnu_field = TREE_OPERAND (gnu_field, 1); | |
4892 | ||
4893 | return gnu_field; | |
4894 | } | |
4895 | ||
229077b0 EB |
4896 | /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return |
4897 | the GCC type corresponding to that entity. */ | |
4898 | ||
4899 | tree | |
4900 | gnat_to_gnu_type (Entity_Id gnat_entity) | |
4901 | { | |
4902 | tree gnu_decl; | |
4903 | ||
4904 | /* The back end never attempts to annotate generic types. */ | |
4905 | if (Is_Generic_Type (gnat_entity) && type_annotate_only) | |
4906 | return void_type_node; | |
4907 | ||
afc737f0 | 4908 | gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
229077b0 EB |
4909 | gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL); |
4910 | ||
4911 | return TREE_TYPE (gnu_decl); | |
4912 | } | |
4913 | ||
4914 | /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return | |
4915 | the unpadded version of the GCC type corresponding to that entity. */ | |
4916 | ||
4917 | tree | |
4918 | get_unpadded_type (Entity_Id gnat_entity) | |
4919 | { | |
4920 | tree type = gnat_to_gnu_type (gnat_entity); | |
4921 | ||
315cff15 | 4922 | if (TYPE_IS_PADDING_P (type)) |
229077b0 EB |
4923 | type = TREE_TYPE (TYPE_FIELDS (type)); |
4924 | ||
4925 | return type; | |
4926 | } | |
1228a6a6 | 4927 | |
28dd0055 EB |
4928 | /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is |
4929 | a C++ imported method or equivalent. | |
4930 | ||
69720717 EB |
4931 | We use the predicate to find out whether we need to use METHOD_TYPE instead |
4932 | of FUNCTION_TYPE for GNAT_ENTITY for the sake compatibility with C++. This | |
4933 | in turn determines whether the "thiscall" calling convention is used by the | |
4934 | back-end for GNAT_ENTITY on 32-bit x86/Windows. */ | |
28dd0055 | 4935 | |
69720717 | 4936 | static bool |
28dd0055 EB |
4937 | is_cplusplus_method (Entity_Id gnat_entity) |
4938 | { | |
eae6758d EB |
4939 | /* A constructor is a method on the C++ side. We deal with it now because |
4940 | it is declared without the 'this' parameter in the sources and, although | |
4941 | the front-end will create a version with the 'this' parameter for code | |
4942 | generation purposes, we want to return true for both versions. */ | |
4943 | if (Is_Constructor (gnat_entity)) | |
4944 | return true; | |
4945 | ||
59909673 EB |
4946 | /* Check that the subprogram has C++ convention. */ |
4947 | if (Convention (gnat_entity) != Convention_CPP) | |
4948 | return false; | |
4949 | ||
44662f68 EB |
4950 | /* And that the type of the first parameter (indirectly) has it too, but |
4951 | we make an exception for Interfaces because they need not be imported. */ | |
eae6758d EB |
4952 | Entity_Id gnat_first = First_Formal (gnat_entity); |
4953 | if (No (gnat_first)) | |
4954 | return false; | |
eae6758d EB |
4955 | Entity_Id gnat_type = Etype (gnat_first); |
4956 | if (Is_Access_Type (gnat_type)) | |
4957 | gnat_type = Directly_Designated_Type (gnat_type); | |
44662f68 | 4958 | if (Convention (gnat_type) != Convention_CPP && !Is_Interface (gnat_type)) |
eae6758d EB |
4959 | return false; |
4960 | ||
59909673 EB |
4961 | /* This is the main case: a C++ virtual method imported as a primitive |
4962 | operation of a tagged type. */ | |
4963 | if (Is_Dispatching_Operation (gnat_entity)) | |
4964 | return true; | |
4965 | ||
4966 | /* This is set on the E_Subprogram_Type built for a dispatching call. */ | |
4967 | if (Is_Dispatch_Table_Entity (gnat_entity)) | |
78df6221 | 4968 | return true; |
28dd0055 EB |
4969 | |
4970 | /* A thunk needs to be handled like its associated primitive operation. */ | |
4971 | if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity)) | |
78df6221 | 4972 | return true; |
28dd0055 | 4973 | |
59909673 EB |
4974 | /* Now on to the annoying case: a C++ non-virtual method, imported either |
4975 | as a non-primitive operation of a tagged type or as a primitive operation | |
4976 | of an untagged type. We cannot reliably differentiate these cases from | |
4977 | their static member or regular function equivalents in Ada, so we ask | |
4978 | the C++ side through the mangled name of the function, as the implicit | |
4979 | 'this' parameter is not encoded in the mangled name of a method. */ | |
4980 | if (Is_Subprogram (gnat_entity) && Present (Interface_Name (gnat_entity))) | |
4981 | { | |
4982 | String_Pointer sp = { NULL, NULL }; | |
4983 | Get_External_Name (gnat_entity, false, sp); | |
4984 | ||
4985 | void *mem; | |
4986 | struct demangle_component *cmp | |
4987 | = cplus_demangle_v3_components (Name_Buffer, | |
4988 | DMGL_GNU_V3 | |
4989 | | DMGL_TYPES | |
4990 | | DMGL_PARAMS | |
4991 | | DMGL_RET_DROP, | |
4992 | &mem); | |
4993 | if (!cmp) | |
4994 | return false; | |
4995 | ||
4996 | /* We need to release MEM once we have a successful demangling. */ | |
4997 | bool ret = false; | |
4998 | ||
4999 | if (cmp->type == DEMANGLE_COMPONENT_TYPED_NAME | |
5000 | && cmp->u.s_binary.right->type == DEMANGLE_COMPONENT_FUNCTION_TYPE | |
5001 | && (cmp = cmp->u.s_binary.right->u.s_binary.right) != NULL | |
5002 | && cmp->type == DEMANGLE_COMPONENT_ARGLIST) | |
5003 | { | |
5004 | /* Make sure there is at least one parameter in C++ too. */ | |
5005 | if (cmp->u.s_binary.left) | |
5006 | { | |
5007 | unsigned int n_ada_args = 0; | |
5008 | do { | |
5009 | n_ada_args++; | |
5010 | gnat_first = Next_Formal (gnat_first); | |
5011 | } while (Present (gnat_first)); | |
5012 | ||
5013 | unsigned int n_cpp_args = 0; | |
5014 | do { | |
5015 | n_cpp_args++; | |
5016 | cmp = cmp->u.s_binary.right; | |
5017 | } while (cmp); | |
5018 | ||
5019 | if (n_cpp_args < n_ada_args) | |
5020 | ret = true; | |
5021 | } | |
5022 | else | |
5023 | ret = true; | |
5024 | } | |
5025 | ||
5026 | free (mem); | |
5027 | ||
5028 | return ret; | |
5029 | } | |
28dd0055 | 5030 | |
78df6221 | 5031 | return false; |
28dd0055 EB |
5032 | } |
5033 | ||
13a6dfe3 EB |
5034 | /* Return the inlining status of the GNAT subprogram SUBPROG. */ |
5035 | ||
5036 | static enum inline_status_t | |
5037 | inline_status_for_subprog (Entity_Id subprog) | |
5038 | { | |
5039 | if (Has_Pragma_No_Inline (subprog)) | |
5040 | return is_suppressed; | |
5041 | ||
5042 | if (Has_Pragma_Inline_Always (subprog)) | |
5043 | return is_required; | |
5044 | ||
5045 | if (Is_Inlined (subprog)) | |
5046 | { | |
5047 | tree gnu_type; | |
5048 | ||
5049 | /* This is a kludge to work around a pass ordering issue: for small | |
5050 | record types with many components, i.e. typically bit-fields, the | |
5051 | initialization routine can contain many assignments that will be | |
5052 | merged by the GIMPLE store merging pass. But this pass runs very | |
5053 | late in the pipeline, in particular after the inlining decisions | |
5054 | are made, so the inlining heuristics cannot take its outcome into | |
5055 | account. Therefore, we optimistically override the heuristics for | |
5056 | the initialization routine in this case. */ | |
5057 | if (Is_Init_Proc (subprog) | |
5058 | && flag_store_merging | |
5059 | && Is_Record_Type (Etype (First_Formal (subprog))) | |
5060 | && (gnu_type = gnat_to_gnu_type (Etype (First_Formal (subprog)))) | |
5061 | && !TYPE_IS_BY_REFERENCE_P (gnu_type) | |
5062 | && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)) | |
5063 | && compare_tree_int (TYPE_SIZE (gnu_type), MAX_FIXED_MODE_SIZE) <= 0) | |
5064 | return is_prescribed; | |
5065 | ||
5066 | return is_requested; | |
5067 | } | |
5068 | ||
5069 | return is_default; | |
5070 | } | |
5071 | ||
7b56a91b | 5072 | /* Finalize the processing of From_Limited_With incomplete types. */ |
a1ab4c31 AC |
5073 | |
5074 | void | |
7b56a91b | 5075 | finalize_from_limited_with (void) |
a1ab4c31 | 5076 | { |
6ddf9843 EB |
5077 | struct incomplete *p, *next; |
5078 | ||
1e55d29a EB |
5079 | p = defer_limited_with_list; |
5080 | defer_limited_with_list = NULL; | |
a1ab4c31 | 5081 | |
6ddf9843 | 5082 | for (; p; p = next) |
a1ab4c31 | 5083 | { |
6ddf9843 | 5084 | next = p->next; |
a1ab4c31 | 5085 | |
6ddf9843 | 5086 | if (p->old_type) |
1e55d29a EB |
5087 | { |
5088 | update_pointer_to (TYPE_MAIN_VARIANT (p->old_type), | |
5089 | gnat_to_gnu_type (p->full_type)); | |
5090 | if (TYPE_DUMMY_IN_PROFILE_P (p->old_type)) | |
5091 | update_profiles_with (p->old_type); | |
5092 | } | |
5093 | ||
6ddf9843 | 5094 | free (p); |
a1ab4c31 AC |
5095 | } |
5096 | } | |
5097 | ||
b1b2b511 EB |
5098 | /* Return the equivalent type to be used for GNAT_ENTITY, if it's a kind |
5099 | of type (such E_Task_Type) that has a different type which Gigi uses | |
5100 | for its representation. If the type does not have a special type for | |
5101 | its representation, return GNAT_ENTITY. */ | |
a1ab4c31 AC |
5102 | |
5103 | Entity_Id | |
5104 | Gigi_Equivalent_Type (Entity_Id gnat_entity) | |
5105 | { | |
5106 | Entity_Id gnat_equiv = gnat_entity; | |
5107 | ||
5108 | if (No (gnat_entity)) | |
5109 | return gnat_entity; | |
5110 | ||
5111 | switch (Ekind (gnat_entity)) | |
5112 | { | |
5113 | case E_Class_Wide_Subtype: | |
5114 | if (Present (Equivalent_Type (gnat_entity))) | |
5115 | gnat_equiv = Equivalent_Type (gnat_entity); | |
5116 | break; | |
5117 | ||
5118 | case E_Access_Protected_Subprogram_Type: | |
5119 | case E_Anonymous_Access_Protected_Subprogram_Type: | |
42a5e410 EB |
5120 | if (Present (Equivalent_Type (gnat_entity))) |
5121 | gnat_equiv = Equivalent_Type (gnat_entity); | |
a1ab4c31 AC |
5122 | break; |
5123 | ||
7fddde95 EB |
5124 | case E_Access_Subtype: |
5125 | gnat_equiv = Etype (gnat_entity); | |
5126 | break; | |
5127 | ||
43b60e57 EB |
5128 | case E_Array_Subtype: |
5129 | if (!Is_Constrained (gnat_entity)) | |
5130 | gnat_equiv = Etype (gnat_entity); | |
5131 | break; | |
5132 | ||
a1ab4c31 | 5133 | case E_Class_Wide_Type: |
cbae498b | 5134 | gnat_equiv = Root_Type (gnat_entity); |
a1ab4c31 AC |
5135 | break; |
5136 | ||
a1ab4c31 AC |
5137 | case E_Protected_Type: |
5138 | case E_Protected_Subtype: | |
42a5e410 EB |
5139 | case E_Task_Type: |
5140 | case E_Task_Subtype: | |
5141 | if (Present (Corresponding_Record_Type (gnat_entity))) | |
5142 | gnat_equiv = Corresponding_Record_Type (gnat_entity); | |
a1ab4c31 AC |
5143 | break; |
5144 | ||
5145 | default: | |
5146 | break; | |
5147 | } | |
5148 | ||
a1ab4c31 AC |
5149 | return gnat_equiv; |
5150 | } | |
5151 | ||
2cac6017 EB |
5152 | /* Return a GCC tree for a type corresponding to the component type of the |
5153 | array type or subtype GNAT_ARRAY. DEFINITION is true if this component | |
5154 | is for an array being defined. DEBUG_INFO_P is true if we need to write | |
5155 | debug information for other types that we may create in the process. */ | |
5156 | ||
5157 | static tree | |
5158 | gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition, | |
5159 | bool debug_info_p) | |
5160 | { | |
c020c92b | 5161 | const Entity_Id gnat_type = Component_Type (gnat_array); |
1e3cabd4 | 5162 | const bool is_bit_packed = Is_Bit_Packed_Array (gnat_array); |
c020c92b | 5163 | tree gnu_type = gnat_to_gnu_type (gnat_type); |
2cac6017 | 5164 | tree gnu_comp_size; |
1e3cabd4 | 5165 | bool has_packed_components; |
b3f75672 EB |
5166 | unsigned int max_align; |
5167 | ||
5168 | /* If an alignment is specified, use it as a cap on the component type | |
15c55b96 | 5169 | so that it can be honored for the whole type, but ignore it for the |
b3f75672 EB |
5170 | original type of packed array types. */ |
5171 | if (No (Packed_Array_Impl_Type (gnat_array)) | |
5172 | && Known_Alignment (gnat_array)) | |
5173 | max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0); | |
5174 | else | |
5175 | max_align = 0; | |
2cac6017 | 5176 | |
6186a6ef | 5177 | /* Try to get a packable form of the component if needed. */ |
afc737f0 | 5178 | if ((Is_Packed (gnat_array) || Has_Component_Size_Clause (gnat_array)) |
1e3cabd4 | 5179 | && !is_bit_packed |
2cac6017 | 5180 | && !Has_Aliased_Components (gnat_array) |
c020c92b | 5181 | && !Strict_Alignment (gnat_type) |
e1e5852c | 5182 | && RECORD_OR_UNION_TYPE_P (gnu_type) |
315cff15 | 5183 | && !TYPE_FAT_POINTER_P (gnu_type) |
cc269bb6 | 5184 | && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))) |
6186a6ef EB |
5185 | { |
5186 | gnu_type = make_packable_type (gnu_type, false, max_align); | |
5187 | has_packed_components = true; | |
5188 | } | |
1e3cabd4 EB |
5189 | else |
5190 | has_packed_components = is_bit_packed; | |
2cac6017 | 5191 | |
2cac6017 EB |
5192 | /* Get and validate any specified Component_Size. */ |
5193 | gnu_comp_size | |
5194 | = validate_size (Component_Size (gnat_array), gnu_type, gnat_array, | |
a517d6c1 EB |
5195 | has_packed_components ? TYPE_DECL : VAR_DECL, true, |
5196 | Has_Component_Size_Clause (gnat_array), NULL, NULL); | |
2cac6017 EB |
5197 | |
5198 | /* If the component type is a RECORD_TYPE that has a self-referential size, | |
5199 | then use the maximum size for the component size. */ | |
5200 | if (!gnu_comp_size | |
5201 | && TREE_CODE (gnu_type) == RECORD_TYPE | |
5202 | && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))) | |
5203 | gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true); | |
5204 | ||
988ee9bc EB |
5205 | /* If the array has aliased components and the component size is zero, force |
5206 | the unit size to ensure that the components have distinct addresses. */ | |
5207 | if (!gnu_comp_size | |
5208 | && Has_Aliased_Components (gnat_array) | |
5209 | && integer_zerop (TYPE_SIZE (gnu_type))) | |
5210 | gnu_comp_size = bitsize_unit_node; | |
5211 | ||
2cac6017 | 5212 | /* Honor the component size. This is not needed for bit-packed arrays. */ |
1e3cabd4 | 5213 | if (gnu_comp_size && !is_bit_packed) |
2cac6017 EB |
5214 | { |
5215 | tree orig_type = gnu_type; | |
15c55b96 | 5216 | unsigned int gnu_comp_align; |
2cac6017 EB |
5217 | |
5218 | gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false); | |
5219 | if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align) | |
5220 | gnu_type = orig_type; | |
5221 | else | |
5222 | orig_type = gnu_type; | |
5223 | ||
15c55b96 EB |
5224 | /* We need to make sure that the size is a multiple of the alignment. |
5225 | But we do not misalign the component type because of the alignment | |
5226 | of the array type here; this either must have been done earlier in | |
5227 | the packed case or should be rejected in the non-packed case. */ | |
5228 | if (TREE_CODE (gnu_comp_size) == INTEGER_CST) | |
5229 | { | |
5230 | const unsigned HOST_WIDE_INT int_size = tree_to_uhwi (gnu_comp_size); | |
5231 | gnu_comp_align = int_size & -int_size; | |
5232 | if (gnu_comp_align > TYPE_ALIGN (gnu_type)) | |
5233 | gnu_comp_align = 0; | |
5234 | } | |
5235 | else | |
5236 | gnu_comp_align = 0; | |
5237 | ||
5238 | gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, gnu_comp_align, | |
5239 | gnat_array, true, definition, true); | |
2cac6017 EB |
5240 | |
5241 | /* If a padding record was made, declare it now since it will never be | |
5242 | declared otherwise. This is necessary to ensure that its subtrees | |
5243 | are properly marked. */ | |
5244 | if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type))) | |
74746d49 EB |
5245 | create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p, |
5246 | gnat_array); | |
2cac6017 EB |
5247 | } |
5248 | ||
988ee9bc EB |
5249 | /* This is a very special case where the array has aliased components and the |
5250 | component size might be zero at run time. As explained above, we force at | |
5251 | least the unit size but we don't want to build a distinct padding type for | |
5252 | each invocation (they are not canonicalized if they have variable size) so | |
5253 | we cache this special padding type as TYPE_PADDING_FOR_COMPONENT. */ | |
5254 | else if (Has_Aliased_Components (gnat_array) | |
5255 | && TREE_CODE (gnu_type) == ARRAY_TYPE | |
5256 | && !TREE_CONSTANT (TYPE_SIZE (gnu_type))) | |
5257 | { | |
5258 | if (TYPE_PADDING_FOR_COMPONENT (gnu_type)) | |
5259 | gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type); | |
5260 | else | |
5261 | { | |
5262 | gnu_comp_size | |
5263 | = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node); | |
5264 | TYPE_PADDING_FOR_COMPONENT (gnu_type) | |
5265 | = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array, | |
1e3cabd4 | 5266 | true, definition, true); |
988ee9bc EB |
5267 | gnu_type = TYPE_PADDING_FOR_COMPONENT (gnu_type); |
5268 | create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p, | |
5269 | gnat_array); | |
5270 | } | |
5271 | } | |
5272 | ||
b1af4cb2 | 5273 | /* Now check if the type of the component allows atomic access. */ |
b120ca61 | 5274 | if (Has_Atomic_Components (gnat_array) || Is_Full_Access (gnat_type)) |
af95bb26 EB |
5275 | check_ok_for_atomic_type (gnu_type, gnat_array, true); |
5276 | ||
ee45a32d EB |
5277 | /* If the component type is a padded type made for a non-bit-packed array |
5278 | of scalars with reverse storage order, we need to propagate the reverse | |
5279 | storage order to the padding type since it is the innermost enclosing | |
5280 | aggregate type around the scalar. */ | |
5281 | if (TYPE_IS_PADDING_P (gnu_type) | |
1e3cabd4 | 5282 | && !is_bit_packed |
ee45a32d | 5283 | && Reverse_Storage_Order (gnat_array) |
ee45a32d EB |
5284 | && Is_Scalar_Type (gnat_type)) |
5285 | gnu_type = set_reverse_storage_order_on_pad_type (gnu_type); | |
5286 | ||
c020c92b | 5287 | if (Has_Volatile_Components (gnat_array)) |
f797c2b7 EB |
5288 | { |
5289 | const int quals | |
5290 | = TYPE_QUAL_VOLATILE | |
5291 | | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0); | |
5292 | gnu_type = change_qualified_type (gnu_type, quals); | |
5293 | } | |
2cac6017 EB |
5294 | |
5295 | return gnu_type; | |
5296 | } | |
5297 | ||
8dcefdc0 EB |
5298 | /* Return whether TYPE requires that formal parameters of TYPE be initialized |
5299 | when they are Out parameters passed by copy. | |
5300 | ||
5301 | This just implements the set of conditions listed in RM 6.4.1(12). */ | |
5302 | ||
5303 | static bool | |
5304 | type_requires_init_of_formal (Entity_Id type) | |
5305 | { | |
5306 | type = Underlying_Type (type); | |
5307 | ||
5308 | if (Is_Access_Type (type)) | |
5309 | return true; | |
5310 | ||
5311 | if (Is_Scalar_Type (type)) | |
5312 | return Has_Default_Aspect (type); | |
5313 | ||
5314 | if (Is_Array_Type (type)) | |
5315 | return Has_Default_Aspect (type) | |
5316 | || type_requires_init_of_formal (Component_Type (type)); | |
5317 | ||
5318 | if (Is_Record_Type (type)) | |
5319 | for (Entity_Id field = First_Entity (type); | |
5320 | Present (field); | |
5321 | field = Next_Entity (field)) | |
5322 | { | |
c743425f | 5323 | if (Ekind (field) == E_Discriminant && !Is_Unchecked_Union (type)) |
8dcefdc0 EB |
5324 | return true; |
5325 | ||
5326 | if (Ekind (field) == E_Component | |
5327 | && (Present (Expression (Parent (field))) | |
5328 | || type_requires_init_of_formal (Etype (field)))) | |
5329 | return true; | |
5330 | } | |
5331 | ||
5332 | return false; | |
5333 | } | |
5334 | ||
1e55d29a | 5335 | /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed |
d5ebeb8c EB |
5336 | in the parameter list of GNAT_SUBPROG. GNU_PARAM_TYPE is the GCC tree for |
5337 | the type of the parameter. FIRST is true if this is the first parameter in | |
5338 | the list of GNAT_SUBPROG. Also set CICO to true if the parameter must use | |
5339 | the copy-in copy-out implementation mechanism. | |
a1ab4c31 | 5340 | |
d5ebeb8c EB |
5341 | The returned tree is a PARM_DECL, except for the cases where no parameter |
5342 | needs to be actually passed to the subprogram; the type of this "shadow" | |
5343 | parameter is then returned instead. */ | |
a1ab4c31 AC |
5344 | |
5345 | static tree | |
d5ebeb8c EB |
5346 | gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first, |
5347 | Entity_Id gnat_subprog, bool *cico) | |
a1ab4c31 | 5348 | { |
1e55d29a | 5349 | Mechanism_Type mech = Mechanism (gnat_param); |
a1ab4c31 | 5350 | tree gnu_param_name = get_entity_name (gnat_param); |
1e55d29a | 5351 | bool foreign = Has_Foreign_Convention (gnat_subprog); |
a1ab4c31 AC |
5352 | bool in_param = (Ekind (gnat_param) == E_In_Parameter); |
5353 | /* The parameter can be indirectly modified if its address is taken. */ | |
5354 | bool ro_param = in_param && !Address_Taken (gnat_param); | |
0c700259 | 5355 | bool by_return = false, by_component_ptr = false; |
491f54a7 | 5356 | bool by_ref = false; |
1edbeb15 | 5357 | bool forced_by_ref = false; |
1ddde8dc | 5358 | bool restricted_aliasing_p = false; |
7414a3c3 | 5359 | location_t saved_location = input_location; |
a1ab4c31 AC |
5360 | tree gnu_param; |
5361 | ||
7414a3c3 EB |
5362 | /* Make sure to use the proper SLOC for vector ABI warnings. */ |
5363 | if (VECTOR_TYPE_P (gnu_param_type)) | |
5364 | Sloc_to_locus (Sloc (gnat_subprog), &input_location); | |
5365 | ||
1e55d29a EB |
5366 | /* Builtins are expanded inline and there is no real call sequence involved. |
5367 | So the type expected by the underlying expander is always the type of the | |
5368 | argument "as is". */ | |
abb540a7 | 5369 | if (Is_Intrinsic_Subprogram (gnat_subprog) |
1e55d29a EB |
5370 | && Present (Interface_Name (gnat_subprog))) |
5371 | mech = By_Copy; | |
5372 | ||
5373 | /* Handle the first parameter of a valued procedure specially: it's a copy | |
5374 | mechanism for which the parameter is never allocated. */ | |
5375 | else if (first && Is_Valued_Procedure (gnat_subprog)) | |
a1ab4c31 AC |
5376 | { |
5377 | gcc_assert (Ekind (gnat_param) == E_Out_Parameter); | |
5378 | mech = By_Copy; | |
5379 | by_return = true; | |
5380 | } | |
5381 | ||
1e55d29a EB |
5382 | /* Or else, see if a Mechanism was supplied that forced this parameter |
5383 | to be passed one way or another. */ | |
5384 | else if (mech == Default || mech == By_Copy || mech == By_Reference) | |
1edbeb15 EB |
5385 | forced_by_ref |
5386 | = (mech == By_Reference | |
5387 | && !foreign | |
5388 | && !TYPE_IS_BY_REFERENCE_P (gnu_param_type) | |
5389 | && !Is_Aliased (gnat_param)); | |
1e55d29a EB |
5390 | |
5391 | /* Positive mechanism means by copy for sufficiently small parameters. */ | |
5392 | else if (mech > 0) | |
5393 | { | |
5394 | if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE | |
5395 | || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST | |
5396 | || compare_tree_int (TYPE_SIZE (gnu_param_type), mech) > 0) | |
5397 | mech = By_Reference; | |
5398 | else | |
5399 | mech = By_Copy; | |
5400 | } | |
5401 | ||
5402 | /* Otherwise, it's an unsupported mechanism so error out. */ | |
5403 | else | |
5404 | { | |
5405 | post_error ("unsupported mechanism for&", gnat_param); | |
5406 | mech = Default; | |
5407 | } | |
5408 | ||
92961bdf EB |
5409 | /* Either for foreign conventions, or if the underlying type is not passed |
5410 | by reference and is as large and aligned as the original type, strip off | |
5411 | a possible padding type. */ | |
315cff15 | 5412 | if (TYPE_IS_PADDING_P (gnu_param_type)) |
a1ab4c31 | 5413 | { |
92961bdf | 5414 | tree inner_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type)); |
a1ab4c31 | 5415 | |
57f4f0d5 | 5416 | if (foreign |
c95f808d | 5417 | || (mech != By_Reference |
92961bdf EB |
5418 | && !must_pass_by_ref (inner_type) |
5419 | && (mech == By_Copy || !default_pass_by_ref (inner_type)) | |
5420 | && ((TYPE_SIZE (inner_type) == TYPE_SIZE (gnu_param_type) | |
5421 | && TYPE_ALIGN (inner_type) >= TYPE_ALIGN (gnu_param_type)) | |
5422 | || Is_Init_Proc (gnat_subprog)))) | |
5423 | gnu_param_type = inner_type; | |
a1ab4c31 AC |
5424 | } |
5425 | ||
a1ab4c31 AC |
5426 | /* For foreign conventions, pass arrays as pointers to the element type. |
5427 | First check for unconstrained array and get the underlying array. */ | |
5428 | if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE) | |
5429 | gnu_param_type | |
5430 | = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type)))); | |
5431 | ||
a1ab4c31 | 5432 | /* Arrays are passed as pointers to element type for foreign conventions. */ |
1eb58520 | 5433 | if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE) |
a1ab4c31 AC |
5434 | { |
5435 | /* Strip off any multi-dimensional entries, then strip | |
5436 | off the last array to get the component type. */ | |
5437 | while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE | |
5438 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type))) | |
5439 | gnu_param_type = TREE_TYPE (gnu_param_type); | |
5440 | ||
a1ab4c31 | 5441 | gnu_param_type = TREE_TYPE (gnu_param_type); |
a1ab4c31 | 5442 | gnu_param_type = build_pointer_type (gnu_param_type); |
71836434 | 5443 | by_component_ptr = true; |
a1ab4c31 AC |
5444 | } |
5445 | ||
5446 | /* Fat pointers are passed as thin pointers for foreign conventions. */ | |
315cff15 | 5447 | else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type)) |
a1ab4c31 AC |
5448 | gnu_param_type |
5449 | = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0); | |
5450 | ||
69720717 EB |
5451 | /* Use a pointer type for the "this" pointer of C++ constructors. */ |
5452 | else if (Chars (gnat_param) == Name_uInit && Is_Constructor (gnat_subprog)) | |
5453 | { | |
5454 | gcc_assert (mech == By_Reference); | |
5455 | gnu_param_type = build_pointer_type (gnu_param_type); | |
5456 | by_ref = true; | |
5457 | } | |
5458 | ||
1e55d29a | 5459 | /* If we were requested or muss pass by reference, do so. |
a1ab4c31 AC |
5460 | If we were requested to pass by copy, do so. |
5461 | Otherwise, for foreign conventions, pass In Out or Out parameters | |
5462 | or aggregates by reference. For COBOL and Fortran, pass all | |
5463 | integer and FP types that way too. For Convention Ada, use | |
5464 | the standard Ada default. */ | |
1e55d29a EB |
5465 | else if (mech == By_Reference |
5466 | || must_pass_by_ref (gnu_param_type) | |
a1ab4c31 AC |
5467 | || (mech != By_Copy |
5468 | && ((foreign | |
5469 | && (!in_param || AGGREGATE_TYPE_P (gnu_param_type))) | |
5470 | || (foreign | |
5471 | && (Convention (gnat_subprog) == Convention_Fortran | |
5472 | || Convention (gnat_subprog) == Convention_COBOL) | |
5473 | && (INTEGRAL_TYPE_P (gnu_param_type) | |
5474 | || FLOAT_TYPE_P (gnu_param_type))) | |
5475 | || (!foreign | |
5476 | && default_pass_by_ref (gnu_param_type))))) | |
5477 | { | |
4f96985d EB |
5478 | /* We take advantage of 6.2(12) by considering that references built for |
5479 | parameters whose type isn't by-ref and for which the mechanism hasn't | |
1ddde8dc EB |
5480 | been forced to by-ref allow only a restricted form of aliasing. */ |
5481 | restricted_aliasing_p | |
a0b8b1b7 | 5482 | = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference; |
1e55d29a | 5483 | gnu_param_type = build_reference_type (gnu_param_type); |
a1ab4c31 AC |
5484 | by_ref = true; |
5485 | } | |
5486 | ||
5487 | /* Pass In Out or Out parameters using copy-in copy-out mechanism. */ | |
5488 | else if (!in_param) | |
5489 | *cico = true; | |
5490 | ||
7414a3c3 EB |
5491 | input_location = saved_location; |
5492 | ||
a1ab4c31 | 5493 | if (mech == By_Copy && (by_ref || by_component_ptr)) |
4a29b8d6 | 5494 | post_error ("??cannot pass & by copy", gnat_param); |
a1ab4c31 | 5495 | |
8dcefdc0 EB |
5496 | /* If this is an Out parameter that isn't passed by reference and whose |
5497 | type doesn't require the initialization of formals, we don't make a | |
5498 | PARM_DECL for it. Instead, it will be a VAR_DECL created when we | |
5499 | process the procedure, so just return its type here. Likewise for | |
c743425f EB |
5500 | the _Init parameter of an initialization procedure or the special |
5501 | parameter of a valued procedure, never pass them in. */ | |
a1ab4c31 AC |
5502 | if (Ekind (gnat_param) == E_Out_Parameter |
5503 | && !by_ref | |
8dcefdc0 | 5504 | && !by_component_ptr |
c743425f EB |
5505 | && (!type_requires_init_of_formal (Etype (gnat_param)) |
5506 | || Is_Init_Proc (gnat_subprog) | |
5507 | || by_return)) | |
40bd5a53 EB |
5508 | { |
5509 | Set_Mechanism (gnat_param, By_Copy); | |
5510 | return gnu_param_type; | |
5511 | } | |
a1ab4c31 | 5512 | |
1e55d29a EB |
5513 | gnu_param = create_param_decl (gnu_param_name, gnu_param_type); |
5514 | TREE_READONLY (gnu_param) = ro_param || by_ref || by_component_ptr; | |
a1ab4c31 | 5515 | DECL_BY_REF_P (gnu_param) = by_ref; |
1edbeb15 | 5516 | DECL_FORCED_BY_REF_P (gnu_param) = forced_by_ref; |
a1ab4c31 | 5517 | DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr; |
a1ab4c31 AC |
5518 | DECL_POINTS_TO_READONLY_P (gnu_param) |
5519 | = (ro_param && (by_ref || by_component_ptr)); | |
a1c7d797 | 5520 | DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param); |
1ddde8dc | 5521 | DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p; |
1e55d29a | 5522 | Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param)); |
a1ab4c31 AC |
5523 | |
5524 | /* If no Mechanism was specified, indicate what we're using, then | |
5525 | back-annotate it. */ | |
5526 | if (mech == Default) | |
5527 | mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy; | |
5528 | ||
5529 | Set_Mechanism (gnat_param, mech); | |
5530 | return gnu_param; | |
5531 | } | |
5532 | ||
1e55d29a | 5533 | /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that |
d5ebeb8c | 5534 | GNAT_SUBPROG is updated when GNU_TYPE is completed. |
7414a3c3 EB |
5535 | |
5536 | Ada 2012 (AI05-019) says that freezing a subprogram does not always freeze | |
5537 | the corresponding profile, which means that, by the time the freeze node | |
5538 | of the subprogram is encountered, types involved in its profile may still | |
d5ebeb8c EB |
5539 | be not yet frozen. That's why we need to update GNAT_SUBPROG when we see |
5540 | the freeze node of types involved in its profile, either types of formal | |
5541 | parameters or the return type. */ | |
cb55aefb | 5542 | |
1e55d29a EB |
5543 | static void |
5544 | associate_subprog_with_dummy_type (Entity_Id gnat_subprog, tree gnu_type) | |
cb55aefb | 5545 | { |
1e55d29a | 5546 | gcc_assert (TYPE_IS_DUMMY_P (gnu_type)); |
cb55aefb | 5547 | |
1e55d29a EB |
5548 | struct tree_entity_vec_map in; |
5549 | in.base.from = gnu_type; | |
5550 | struct tree_entity_vec_map **slot | |
5551 | = dummy_to_subprog_map->find_slot (&in, INSERT); | |
5552 | if (!*slot) | |
cb55aefb | 5553 | { |
1e55d29a EB |
5554 | tree_entity_vec_map *e = ggc_alloc<tree_entity_vec_map> (); |
5555 | e->base.from = gnu_type; | |
5556 | e->to = NULL; | |
5557 | *slot = e; | |
1e55d29a | 5558 | } |
7414a3c3 EB |
5559 | |
5560 | /* Even if there is already a slot for GNU_TYPE, we need to set the flag | |
5561 | because the vector might have been just emptied by update_profiles_with. | |
5562 | This can happen when there are 2 freeze nodes associated with different | |
5563 | views of the same type; the type will be really complete only after the | |
5564 | second freeze node is encountered. */ | |
5565 | TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 1; | |
5566 | ||
1e55d29a | 5567 | vec<Entity_Id, va_gc_atomic> *v = (*slot)->to; |
cb55aefb | 5568 | |
1e55d29a EB |
5569 | /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type, |
5570 | since this would mean updating twice its profile. */ | |
5571 | if (v) | |
5572 | { | |
5573 | const unsigned len = v->length (); | |
5574 | unsigned int l = 0, u = len; | |
5575 | ||
5576 | /* Entity_Id is a simple integer so we can implement a stable order on | |
5577 | the vector with an ordered insertion scheme and binary search. */ | |
5578 | while (l < u) | |
5579 | { | |
5580 | unsigned int m = (l + u) / 2; | |
5581 | int diff = (int) (*v)[m] - (int) gnat_subprog; | |
5582 | if (diff > 0) | |
5583 | u = m; | |
5584 | else if (diff < 0) | |
5585 | l = m + 1; | |
5586 | else | |
5587 | return; | |
5588 | } | |
cb55aefb | 5589 | |
1e55d29a EB |
5590 | /* l == u and therefore is the insertion point. */ |
5591 | vec_safe_insert (v, l, gnat_subprog); | |
cb55aefb | 5592 | } |
1e55d29a EB |
5593 | else |
5594 | vec_safe_push (v, gnat_subprog); | |
cb55aefb | 5595 | |
1e55d29a EB |
5596 | (*slot)->to = v; |
5597 | } | |
5598 | ||
5599 | /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */ | |
5600 | ||
5601 | static void | |
5602 | update_profile (Entity_Id gnat_subprog) | |
5603 | { | |
5604 | tree gnu_param_list; | |
5605 | tree gnu_type = gnat_to_gnu_subprog_type (gnat_subprog, true, | |
5606 | Needs_Debug_Info (gnat_subprog), | |
5607 | &gnu_param_list); | |
7414a3c3 EB |
5608 | if (DECL_P (gnu_type)) |
5609 | { | |
5610 | /* Builtins cannot have their address taken so we can reset them. */ | |
3d78e008 | 5611 | gcc_assert (fndecl_built_in_p (gnu_type)); |
7414a3c3 EB |
5612 | save_gnu_tree (gnat_subprog, NULL_TREE, false); |
5613 | save_gnu_tree (gnat_subprog, gnu_type, false); | |
5614 | return; | |
5615 | } | |
5616 | ||
1e55d29a EB |
5617 | tree gnu_subprog = get_gnu_tree (gnat_subprog); |
5618 | ||
5619 | TREE_TYPE (gnu_subprog) = gnu_type; | |
5620 | ||
5621 | /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL | |
5622 | and needs to be adjusted too. */ | |
5623 | if (Ekind (gnat_subprog) != E_Subprogram_Type) | |
5624 | { | |
7414a3c3 EB |
5625 | tree gnu_entity_name = get_entity_name (gnat_subprog); |
5626 | tree gnu_ext_name | |
5627 | = gnu_ext_name_for_subprog (gnat_subprog, gnu_entity_name); | |
5628 | ||
1e55d29a | 5629 | DECL_ARGUMENTS (gnu_subprog) = gnu_param_list; |
7414a3c3 | 5630 | finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type); |
1e55d29a EB |
5631 | } |
5632 | } | |
5633 | ||
5634 | /* Update the GCC trees previously built for the profiles involving GNU_TYPE, | |
5635 | a dummy type which appears in profiles. */ | |
5636 | ||
5637 | void | |
5638 | update_profiles_with (tree gnu_type) | |
5639 | { | |
5640 | struct tree_entity_vec_map in; | |
5641 | in.base.from = gnu_type; | |
5642 | struct tree_entity_vec_map *e = dummy_to_subprog_map->find (&in); | |
5643 | gcc_assert (e); | |
5644 | vec<Entity_Id, va_gc_atomic> *v = e->to; | |
5645 | e->to = NULL; | |
7414a3c3 EB |
5646 | |
5647 | /* The flag needs to be reset before calling update_profile, in case | |
5648 | associate_subprog_with_dummy_type is again invoked on GNU_TYPE. */ | |
1e55d29a EB |
5649 | TYPE_DUMMY_IN_PROFILE_P (gnu_type) = 0; |
5650 | ||
5651 | unsigned int i; | |
5652 | Entity_Id *iter; | |
5653 | FOR_EACH_VEC_ELT (*v, i, iter) | |
5654 | update_profile (*iter); | |
5655 | ||
5656 | vec_free (v); | |
5657 | } | |
5658 | ||
5659 | /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram. | |
5660 | ||
5661 | Ada 2012 (AI05-0151) says that incomplete types coming from a limited | |
5662 | context may now appear as parameter and result types. As a consequence, | |
5663 | we may need to defer their translation until after a freeze node is seen | |
5664 | or to the end of the current unit. We also aim at handling temporarily | |
5665 | incomplete types created by the usual delayed elaboration scheme. */ | |
5666 | ||
5667 | static tree | |
5668 | gnat_to_gnu_profile_type (Entity_Id gnat_type) | |
5669 | { | |
5670 | /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity | |
5671 | so the rationale is exposed in that place. These processings probably | |
5672 | ought to be merged at some point. */ | |
5673 | Entity_Id gnat_equiv = Gigi_Equivalent_Type (gnat_type); | |
5674 | const bool is_from_limited_with | |
7ed9919d | 5675 | = (Is_Incomplete_Type (gnat_equiv) |
1e55d29a EB |
5676 | && From_Limited_With (gnat_equiv)); |
5677 | Entity_Id gnat_full_direct_first | |
5678 | = (is_from_limited_with | |
5679 | ? Non_Limited_View (gnat_equiv) | |
7ed9919d | 5680 | : (Is_Incomplete_Or_Private_Type (gnat_equiv) |
1e55d29a EB |
5681 | ? Full_View (gnat_equiv) : Empty)); |
5682 | Entity_Id gnat_full_direct | |
5683 | = ((is_from_limited_with | |
5684 | && Present (gnat_full_direct_first) | |
7ed9919d | 5685 | && Is_Private_Type (gnat_full_direct_first)) |
1e55d29a EB |
5686 | ? Full_View (gnat_full_direct_first) |
5687 | : gnat_full_direct_first); | |
5688 | Entity_Id gnat_full = Gigi_Equivalent_Type (gnat_full_direct); | |
5689 | Entity_Id gnat_rep = Present (gnat_full) ? gnat_full : gnat_equiv; | |
5690 | const bool in_main_unit = In_Extended_Main_Code_Unit (gnat_rep); | |
5691 | tree gnu_type; | |
5692 | ||
5693 | if (Present (gnat_full) && present_gnu_tree (gnat_full)) | |
5694 | gnu_type = TREE_TYPE (get_gnu_tree (gnat_full)); | |
5695 | ||
5696 | else if (is_from_limited_with | |
5697 | && ((!in_main_unit | |
5698 | && !present_gnu_tree (gnat_equiv) | |
5699 | && Present (gnat_full) | |
d5ebeb8c EB |
5700 | && (Is_Record_Type (gnat_full) |
5701 | || Is_Array_Type (gnat_full) | |
5702 | || Is_Access_Type (gnat_full))) | |
1e55d29a EB |
5703 | || (in_main_unit && Present (Freeze_Node (gnat_rep))))) |
5704 | { | |
5705 | gnu_type = make_dummy_type (gnat_equiv); | |
5706 | ||
5707 | if (!in_main_unit) | |
5708 | { | |
5709 | struct incomplete *p = XNEW (struct incomplete); | |
5710 | ||
5711 | p->old_type = gnu_type; | |
5712 | p->full_type = gnat_equiv; | |
5713 | p->next = defer_limited_with_list; | |
5714 | defer_limited_with_list = p; | |
5715 | } | |
5716 | } | |
5717 | ||
5718 | else if (type_annotate_only && No (gnat_equiv)) | |
5719 | gnu_type = void_type_node; | |
5720 | ||
5721 | else | |
5722 | gnu_type = gnat_to_gnu_type (gnat_equiv); | |
5723 | ||
5724 | /* Access-to-unconstrained-array types need a special treatment. */ | |
5725 | if (Is_Array_Type (gnat_rep) && !Is_Constrained (gnat_rep)) | |
5726 | { | |
5727 | if (!TYPE_POINTER_TO (gnu_type)) | |
5728 | build_dummy_unc_pointer_types (gnat_equiv, gnu_type); | |
5729 | } | |
5730 | ||
5731 | return gnu_type; | |
5732 | } | |
5733 | ||
64c8ebc7 EB |
5734 | /* Return true if TYPE contains only integral data, recursively if need be. */ |
5735 | ||
5736 | static bool | |
5737 | type_contains_only_integral_data (tree type) | |
5738 | { | |
5739 | switch (TREE_CODE (type)) | |
5740 | { | |
5741 | case RECORD_TYPE: | |
5742 | case UNION_TYPE: | |
5743 | case QUAL_UNION_TYPE: | |
5744 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
5745 | if (!type_contains_only_integral_data (TREE_TYPE (field))) | |
5746 | return false; | |
5747 | return true; | |
5748 | ||
5749 | case ARRAY_TYPE: | |
5750 | case COMPLEX_TYPE: | |
5751 | return type_contains_only_integral_data (TREE_TYPE (type)); | |
5752 | ||
5753 | default: | |
5754 | return INTEGRAL_TYPE_P (type); | |
5755 | } | |
5756 | ||
5757 | gcc_unreachable (); | |
5758 | } | |
5759 | ||
1e55d29a EB |
5760 | /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG. |
5761 | DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P | |
5762 | is true if we need to write debug information for other types that we may | |
7414a3c3 EB |
5763 | create in the process. Also set PARAM_LIST to the list of parameters. |
5764 | If GNAT_SUBPROG is bound to a GCC builtin, return the DECL for the builtin | |
5765 | directly instead of its type. */ | |
1e55d29a EB |
5766 | |
5767 | static tree | |
5768 | gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition, | |
5769 | bool debug_info_p, tree *param_list) | |
5770 | { | |
5771 | const Entity_Kind kind = Ekind (gnat_subprog); | |
69720717 | 5772 | const bool method_p = is_cplusplus_method (gnat_subprog); |
c95f808d | 5773 | const bool variadic = IN (Convention (gnat_subprog), Convention_C_Variadic); |
1e55d29a EB |
5774 | Entity_Id gnat_return_type = Etype (gnat_subprog); |
5775 | Entity_Id gnat_param; | |
7414a3c3 EB |
5776 | tree gnu_type = present_gnu_tree (gnat_subprog) |
5777 | ? TREE_TYPE (get_gnu_tree (gnat_subprog)) : NULL_TREE; | |
1e55d29a EB |
5778 | tree gnu_return_type; |
5779 | tree gnu_param_type_list = NULL_TREE; | |
5780 | tree gnu_param_list = NULL_TREE; | |
5781 | /* Non-null for subprograms containing parameters passed by copy-in copy-out | |
5782 | (In Out or Out parameters not passed by reference), in which case it is | |
5783 | the list of nodes used to specify the values of the In Out/Out parameters | |
5784 | that are returned as a record upon procedure return. The TREE_PURPOSE of | |
5785 | an element of this list is a FIELD_DECL of the record and the TREE_VALUE | |
5786 | is the PARM_DECL corresponding to that field. This list will be saved in | |
5787 | the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */ | |
5788 | tree gnu_cico_list = NULL_TREE; | |
7414a3c3 | 5789 | tree gnu_cico_return_type = NULL_TREE; |
64c8ebc7 EB |
5790 | tree gnu_cico_field_list = NULL_TREE; |
5791 | bool gnu_cico_only_integral_type = true; | |
932198a8 EB |
5792 | /* Although the semantics of "pure" units in Ada essentially match those of |
5793 | "const" in GNU C, the semantics of the Is_Pure flag in GNAT do not say | |
5794 | anything about access to global memory, that's why it needs to be mapped | |
5795 | to "pure" instead of "const" in GNU C. The property is orthogonal to the | |
5796 | "nothrow" property only if the EH circuitry is explicit in the internal | |
5797 | representation of the middle-end: if we are to completely hide the EH | |
5798 | circuitry from it, we need to declare that calls to pure Ada subprograms | |
5799 | that can throw have side effects, since they can trigger an "abnormal" | |
5800 | transfer of control; therefore they cannot be "pure" in the GCC sense. */ | |
5801 | bool pure_flag = Is_Pure (gnat_subprog) && Back_End_Exceptions (); | |
1e55d29a EB |
5802 | bool return_by_direct_ref_p = false; |
5803 | bool return_by_invisi_ref_p = false; | |
5804 | bool return_unconstrained_p = false; | |
5805 | bool incomplete_profile_p = false; | |
c95f808d | 5806 | int num; |
1e55d29a | 5807 | |
7414a3c3 EB |
5808 | /* Look into the return type and get its associated GCC tree if it is not |
5809 | void, and then compute various flags for the subprogram type. But make | |
5810 | sure not to do this processing multiple times. */ | |
1e55d29a EB |
5811 | if (Ekind (gnat_return_type) == E_Void) |
5812 | gnu_return_type = void_type_node; | |
7414a3c3 EB |
5813 | |
5814 | else if (gnu_type | |
69720717 | 5815 | && FUNC_OR_METHOD_TYPE_P (gnu_type) |
7414a3c3 EB |
5816 | && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_type))) |
5817 | { | |
5818 | gnu_return_type = TREE_TYPE (gnu_type); | |
5819 | return_unconstrained_p = TYPE_RETURN_UNCONSTRAINED_P (gnu_type); | |
5820 | return_by_direct_ref_p = TYPE_RETURN_BY_DIRECT_REF_P (gnu_type); | |
5821 | return_by_invisi_ref_p = TREE_ADDRESSABLE (gnu_type); | |
5822 | } | |
5823 | ||
1e55d29a EB |
5824 | else |
5825 | { | |
abb540a7 EB |
5826 | /* For foreign convention/intrinsic subprograms, return System.Address |
5827 | as void * or equivalent; this comprises GCC builtins. */ | |
5828 | if ((Has_Foreign_Convention (gnat_subprog) | |
5829 | || Is_Intrinsic_Subprogram (gnat_subprog)) | |
a3fc8f16 | 5830 | && Is_Descendant_Of_Address (Underlying_Type (gnat_return_type))) |
9182f718 EB |
5831 | gnu_return_type = ptr_type_node; |
5832 | else | |
5833 | gnu_return_type = gnat_to_gnu_profile_type (gnat_return_type); | |
1e55d29a EB |
5834 | |
5835 | /* If this function returns by reference, make the actual return type | |
5836 | the reference type and make a note of that. */ | |
5837 | if (Returns_By_Ref (gnat_subprog)) | |
5838 | { | |
5839 | gnu_return_type = build_reference_type (gnu_return_type); | |
5840 | return_by_direct_ref_p = true; | |
5841 | } | |
5842 | ||
5843 | /* If the return type is an unconstrained array type, the return value | |
5844 | will be allocated on the secondary stack so the actual return type | |
5845 | is the fat pointer type. */ | |
5846 | else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE) | |
5847 | { | |
5848 | gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type); | |
5849 | return_unconstrained_p = true; | |
5850 | } | |
5851 | ||
5852 | /* This is the same unconstrained array case, but for a dummy type. */ | |
5853 | else if (TYPE_REFERENCE_TO (gnu_return_type) | |
5854 | && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type))) | |
5855 | { | |
5856 | gnu_return_type = TYPE_REFERENCE_TO (gnu_return_type); | |
5857 | return_unconstrained_p = true; | |
5858 | } | |
5859 | ||
5860 | /* Likewise, if the return type requires a transient scope, the return | |
5861 | value will also be allocated on the secondary stack so the actual | |
5862 | return type is the reference type. */ | |
5863 | else if (Requires_Transient_Scope (gnat_return_type)) | |
5864 | { | |
5865 | gnu_return_type = build_reference_type (gnu_return_type); | |
5866 | return_unconstrained_p = true; | |
5867 | } | |
5868 | ||
5869 | /* If the Mechanism is By_Reference, ensure this function uses the | |
5870 | target's by-invisible-reference mechanism, which may not be the | |
5871 | same as above (e.g. it might be passing an extra parameter). */ | |
5872 | else if (kind == E_Function && Mechanism (gnat_subprog) == By_Reference) | |
5873 | return_by_invisi_ref_p = true; | |
5874 | ||
5875 | /* Likewise, if the return type is itself By_Reference. */ | |
5876 | else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type)) | |
5877 | return_by_invisi_ref_p = true; | |
5878 | ||
5879 | /* If the type is a padded type and the underlying type would not be | |
5880 | passed by reference or the function has a foreign convention, return | |
5881 | the underlying type. */ | |
5882 | else if (TYPE_IS_PADDING_P (gnu_return_type) | |
5883 | && (!default_pass_by_ref | |
5884 | (TREE_TYPE (TYPE_FIELDS (gnu_return_type))) | |
5885 | || Has_Foreign_Convention (gnat_subprog))) | |
5886 | gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type)); | |
5887 | ||
5888 | /* If the return type is unconstrained, it must have a maximum size. | |
5889 | Use the padded type as the effective return type. And ensure the | |
5890 | function uses the target's by-invisible-reference mechanism to | |
5891 | avoid copying too much data when it returns. */ | |
5892 | if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type))) | |
5893 | { | |
5894 | tree orig_type = gnu_return_type; | |
5895 | tree max_return_size = max_size (TYPE_SIZE (gnu_return_type), true); | |
5896 | ||
5897 | /* If the size overflows to 0, set it to an arbitrary positive | |
5898 | value so that assignments in the type are preserved. Their | |
5899 | actual size is independent of this positive value. */ | |
5900 | if (TREE_CODE (max_return_size) == INTEGER_CST | |
5901 | && TREE_OVERFLOW (max_return_size) | |
5902 | && integer_zerop (max_return_size)) | |
5903 | { | |
5904 | max_return_size = copy_node (bitsize_unit_node); | |
5905 | TREE_OVERFLOW (max_return_size) = 1; | |
5906 | } | |
5907 | ||
5908 | gnu_return_type = maybe_pad_type (gnu_return_type, max_return_size, | |
1e3cabd4 EB |
5909 | 0, gnat_subprog, false, definition, |
5910 | true); | |
1e55d29a EB |
5911 | |
5912 | /* Declare it now since it will never be declared otherwise. This | |
5913 | is necessary to ensure that its subtrees are properly marked. */ | |
5914 | if (gnu_return_type != orig_type | |
5915 | && !DECL_P (TYPE_NAME (gnu_return_type))) | |
5916 | create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type, | |
5917 | true, debug_info_p, gnat_subprog); | |
5918 | ||
5919 | return_by_invisi_ref_p = true; | |
5920 | } | |
5921 | ||
5922 | /* If the return type has a size that overflows, we usually cannot have | |
5923 | a function that returns that type. This usage doesn't really make | |
5924 | sense anyway, so issue an error here. */ | |
5925 | if (!return_by_invisi_ref_p | |
5926 | && TYPE_SIZE_UNIT (gnu_return_type) | |
5927 | && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST | |
5928 | && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type))) | |
5929 | { | |
5930 | post_error ("cannot return type whose size overflows", gnat_subprog); | |
5931 | gnu_return_type = copy_type (gnu_return_type); | |
5932 | TYPE_SIZE (gnu_return_type) = bitsize_zero_node; | |
5933 | TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node; | |
5934 | } | |
5935 | ||
5936 | /* If the return type is incomplete, there are 2 cases: if the function | |
5937 | returns by reference, then the return type is only linked indirectly | |
5938 | in the profile, so the profile can be seen as complete since it need | |
5939 | not be further modified, only the reference types need be adjusted; | |
7414a3c3 | 5940 | otherwise the profile is incomplete and need be adjusted too. */ |
1e55d29a EB |
5941 | if (TYPE_IS_DUMMY_P (gnu_return_type)) |
5942 | { | |
5943 | associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type); | |
5944 | incomplete_profile_p = true; | |
5945 | } | |
5946 | ||
5947 | if (kind == E_Function) | |
5948 | Set_Mechanism (gnat_subprog, return_unconstrained_p | |
5949 | || return_by_direct_ref_p | |
5950 | || return_by_invisi_ref_p | |
5951 | ? By_Reference : By_Copy); | |
5952 | } | |
5953 | ||
5954 | /* A procedure (something that doesn't return anything) shouldn't be | |
932198a8 | 5955 | considered pure since there would be no reason for calling such a |
1e55d29a EB |
5956 | subprogram. Note that procedures with Out (or In Out) parameters |
5957 | have already been converted into a function with a return type. | |
5958 | Similarly, if the function returns an unconstrained type, then the | |
5959 | function will allocate the return value on the secondary stack and | |
5960 | thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */ | |
69720717 | 5961 | if (VOID_TYPE_P (gnu_return_type) || return_unconstrained_p) |
932198a8 | 5962 | pure_flag = false; |
1e55d29a EB |
5963 | |
5964 | /* Loop over the parameters and get their associated GCC tree. While doing | |
5965 | this, build a copy-in copy-out structure if we need one. */ | |
5966 | for (gnat_param = First_Formal_With_Extras (gnat_subprog), num = 0; | |
5967 | Present (gnat_param); | |
5968 | gnat_param = Next_Formal_With_Extras (gnat_param), num++) | |
5969 | { | |
7414a3c3 EB |
5970 | const bool mech_is_by_ref |
5971 | = Mechanism (gnat_param) == By_Reference | |
5972 | && !(num == 0 && Is_Valued_Procedure (gnat_subprog)); | |
1e55d29a | 5973 | tree gnu_param_name = get_entity_name (gnat_param); |
7414a3c3 | 5974 | tree gnu_param, gnu_param_type; |
1e55d29a EB |
5975 | bool cico = false; |
5976 | ||
c95f808d EB |
5977 | /* For a variadic C function, do not build unnamed parameters. */ |
5978 | if (variadic | |
5979 | && num == (Convention (gnat_subprog) - Convention_C_Variadic_0)) | |
5980 | break; | |
5981 | ||
7414a3c3 EB |
5982 | /* Fetch an existing parameter with complete type and reuse it. But we |
5983 | didn't save the CICO property so we can only do it for In parameters | |
5984 | or parameters passed by reference. */ | |
5985 | if ((Ekind (gnat_param) == E_In_Parameter || mech_is_by_ref) | |
5986 | && present_gnu_tree (gnat_param) | |
5987 | && (gnu_param = get_gnu_tree (gnat_param)) | |
5988 | && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_param))) | |
1e55d29a | 5989 | { |
7414a3c3 EB |
5990 | DECL_CHAIN (gnu_param) = NULL_TREE; |
5991 | gnu_param_type = TREE_TYPE (gnu_param); | |
5992 | } | |
1e55d29a | 5993 | |
7414a3c3 EB |
5994 | /* Otherwise translate the parameter type and act accordingly. */ |
5995 | else | |
5996 | { | |
5997 | Entity_Id gnat_param_type = Etype (gnat_param); | |
9182f718 | 5998 | |
abb540a7 EB |
5999 | /* For foreign convention/intrinsic subprograms, pass System.Address |
6000 | as void * or equivalent; this comprises GCC builtins. */ | |
6001 | if ((Has_Foreign_Convention (gnat_subprog) | |
6002 | || Is_Intrinsic_Subprogram (gnat_subprog)) | |
a3fc8f16 | 6003 | && Is_Descendant_Of_Address (Underlying_Type (gnat_param_type))) |
9182f718 EB |
6004 | gnu_param_type = ptr_type_node; |
6005 | else | |
6006 | gnu_param_type = gnat_to_gnu_profile_type (gnat_param_type); | |
7414a3c3 EB |
6007 | |
6008 | /* If the parameter type is incomplete, there are 2 cases: if it is | |
6009 | passed by reference, then the type is only linked indirectly in | |
6010 | the profile, so the profile can be seen as complete since it need | |
6011 | not be further modified, only the reference type need be adjusted; | |
6012 | otherwise the profile is incomplete and need be adjusted too. */ | |
6013 | if (TYPE_IS_DUMMY_P (gnu_param_type)) | |
1e55d29a | 6014 | { |
7414a3c3 | 6015 | Node_Id gnat_decl; |
1e55d29a | 6016 | |
7414a3c3 EB |
6017 | if (mech_is_by_ref |
6018 | || (TYPE_REFERENCE_TO (gnu_param_type) | |
6019 | && TYPE_IS_FAT_POINTER_P | |
6020 | (TYPE_REFERENCE_TO (gnu_param_type))) | |
6021 | || TYPE_IS_BY_REFERENCE_P (gnu_param_type)) | |
6022 | { | |
6023 | gnu_param_type = build_reference_type (gnu_param_type); | |
6024 | gnu_param | |
6025 | = create_param_decl (gnu_param_name, gnu_param_type); | |
6026 | TREE_READONLY (gnu_param) = 1; | |
6027 | DECL_BY_REF_P (gnu_param) = 1; | |
6028 | DECL_POINTS_TO_READONLY_P (gnu_param) | |
6029 | = (Ekind (gnat_param) == E_In_Parameter | |
6030 | && !Address_Taken (gnat_param)); | |
6031 | Set_Mechanism (gnat_param, By_Reference); | |
6032 | Sloc_to_locus (Sloc (gnat_param), | |
6033 | &DECL_SOURCE_LOCATION (gnu_param)); | |
6034 | } | |
1e55d29a | 6035 | |
7414a3c3 EB |
6036 | /* ??? This is a kludge to support null procedures in spec taking |
6037 | a parameter with an untagged incomplete type coming from a | |
6038 | limited context. The front-end creates a body without knowing | |
6039 | anything about the non-limited view, which is illegal Ada and | |
6040 | cannot be supported. Create a parameter with a fake type. */ | |
6041 | else if (kind == E_Procedure | |
6042 | && (gnat_decl = Parent (gnat_subprog)) | |
6043 | && Nkind (gnat_decl) == N_Procedure_Specification | |
6044 | && Null_Present (gnat_decl) | |
7ed9919d | 6045 | && Is_Incomplete_Type (gnat_param_type)) |
7414a3c3 | 6046 | gnu_param = create_param_decl (gnu_param_name, ptr_type_node); |
1e55d29a | 6047 | |
7414a3c3 EB |
6048 | else |
6049 | { | |
7cdb6871 EB |
6050 | /* Build a minimal PARM_DECL without DECL_ARG_TYPE so that |
6051 | Call_to_gnu will stop if it encounters the PARM_DECL. */ | |
7414a3c3 | 6052 | gnu_param |
7cdb6871 EB |
6053 | = build_decl (input_location, PARM_DECL, gnu_param_name, |
6054 | gnu_param_type); | |
7414a3c3 EB |
6055 | associate_subprog_with_dummy_type (gnat_subprog, |
6056 | gnu_param_type); | |
6057 | incomplete_profile_p = true; | |
6058 | } | |
6059 | } | |
1e55d29a | 6060 | |
7414a3c3 | 6061 | /* Otherwise build the parameter declaration normally. */ |
1e55d29a EB |
6062 | else |
6063 | { | |
7414a3c3 | 6064 | gnu_param |
d5ebeb8c EB |
6065 | = gnat_to_gnu_param (gnat_param, gnu_param_type, num == 0, |
6066 | gnat_subprog, &cico); | |
7414a3c3 EB |
6067 | |
6068 | /* We are returned either a PARM_DECL or a type if no parameter | |
6069 | needs to be passed; in either case, adjust the type. */ | |
6070 | if (DECL_P (gnu_param)) | |
6071 | gnu_param_type = TREE_TYPE (gnu_param); | |
6072 | else | |
6073 | { | |
6074 | gnu_param_type = gnu_param; | |
6075 | gnu_param = NULL_TREE; | |
6076 | } | |
1e55d29a EB |
6077 | } |
6078 | } | |
6079 | ||
7414a3c3 EB |
6080 | /* If we have a GCC tree for the parameter, register it. */ |
6081 | save_gnu_tree (gnat_param, NULL_TREE, false); | |
1e55d29a EB |
6082 | if (gnu_param) |
6083 | { | |
6084 | gnu_param_type_list | |
6085 | = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list); | |
69720717 EB |
6086 | DECL_CHAIN (gnu_param) = gnu_param_list; |
6087 | gnu_param_list = gnu_param; | |
1e55d29a EB |
6088 | save_gnu_tree (gnat_param, gnu_param, false); |
6089 | ||
71836434 | 6090 | /* A pure function in the Ada sense which takes an access parameter |
932198a8 EB |
6091 | may modify memory through it and thus cannot be considered pure |
6092 | in the GCC sense, unless it's access-to-function. Likewise it if | |
6093 | takes a by-ref In Out or Out parameter. But if it takes a by-ref | |
6094 | In parameter, then it may only read memory through it and can be | |
6095 | considered pure in the GCC sense. */ | |
6096 | if (pure_flag | |
fccc47dd EB |
6097 | && ((POINTER_TYPE_P (gnu_param_type) |
6098 | && TREE_CODE (TREE_TYPE (gnu_param_type)) != FUNCTION_TYPE) | |
71836434 | 6099 | || TYPE_IS_FAT_POINTER_P (gnu_param_type))) |
932198a8 | 6100 | pure_flag = DECL_POINTS_TO_READONLY_P (gnu_param); |
1e55d29a EB |
6101 | } |
6102 | ||
6103 | /* If the parameter uses the copy-in copy-out mechanism, allocate a field | |
6104 | for it in the return type and register the association. */ | |
6105 | if (cico && !incomplete_profile_p) | |
6106 | { | |
6107 | if (!gnu_cico_list) | |
6108 | { | |
7414a3c3 | 6109 | gnu_cico_return_type = make_node (RECORD_TYPE); |
1e55d29a EB |
6110 | |
6111 | /* If this is a function, we also need a field for the | |
6112 | return value to be placed. */ | |
7414a3c3 | 6113 | if (!VOID_TYPE_P (gnu_return_type)) |
1e55d29a | 6114 | { |
7414a3c3 | 6115 | tree gnu_field |
1e55d29a EB |
6116 | = create_field_decl (get_identifier ("RETVAL"), |
6117 | gnu_return_type, | |
7414a3c3 | 6118 | gnu_cico_return_type, NULL_TREE, |
1e55d29a EB |
6119 | NULL_TREE, 0, 0); |
6120 | Sloc_to_locus (Sloc (gnat_subprog), | |
6121 | &DECL_SOURCE_LOCATION (gnu_field)); | |
64c8ebc7 | 6122 | gnu_cico_field_list = gnu_field; |
1e55d29a EB |
6123 | gnu_cico_list |
6124 | = tree_cons (gnu_field, void_type_node, NULL_TREE); | |
64c8ebc7 EB |
6125 | if (!type_contains_only_integral_data (gnu_return_type)) |
6126 | gnu_cico_only_integral_type = false; | |
1e55d29a EB |
6127 | } |
6128 | ||
7414a3c3 | 6129 | TYPE_NAME (gnu_cico_return_type) = get_identifier ("RETURN"); |
1e55d29a EB |
6130 | /* Set a default alignment to speed up accesses. But we should |
6131 | not increase the size of the structure too much, lest it does | |
6132 | not fit in return registers anymore. */ | |
7414a3c3 EB |
6133 | SET_TYPE_ALIGN (gnu_cico_return_type, |
6134 | get_mode_alignment (ptr_mode)); | |
1e55d29a EB |
6135 | } |
6136 | ||
7414a3c3 | 6137 | tree gnu_field |
1e55d29a | 6138 | = create_field_decl (gnu_param_name, gnu_param_type, |
7414a3c3 EB |
6139 | gnu_cico_return_type, NULL_TREE, NULL_TREE, |
6140 | 0, 0); | |
1e55d29a EB |
6141 | Sloc_to_locus (Sloc (gnat_param), |
6142 | &DECL_SOURCE_LOCATION (gnu_field)); | |
64c8ebc7 EB |
6143 | DECL_CHAIN (gnu_field) = gnu_cico_field_list; |
6144 | gnu_cico_field_list = gnu_field; | |
1e55d29a | 6145 | gnu_cico_list = tree_cons (gnu_field, gnu_param, gnu_cico_list); |
64c8ebc7 EB |
6146 | if (!type_contains_only_integral_data (gnu_param_type)) |
6147 | gnu_cico_only_integral_type = false; | |
1e55d29a EB |
6148 | } |
6149 | } | |
6150 | ||
6151 | /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust | |
6152 | and finish up the return type. */ | |
6153 | if (gnu_cico_list && !incomplete_profile_p) | |
6154 | { | |
6155 | /* If we have a CICO list but it has only one entry, we convert | |
6156 | this function into a function that returns this object. */ | |
6157 | if (list_length (gnu_cico_list) == 1) | |
7414a3c3 | 6158 | gnu_cico_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list)); |
1e55d29a EB |
6159 | |
6160 | /* Do not finalize the return type if the subprogram is stubbed | |
6161 | since structures are incomplete for the back-end. */ | |
6162 | else if (Convention (gnat_subprog) != Convention_Stubbed) | |
6163 | { | |
64c8ebc7 EB |
6164 | finish_record_type (gnu_cico_return_type, |
6165 | nreverse (gnu_cico_field_list), | |
7414a3c3 | 6166 | 0, false); |
1e55d29a | 6167 | |
64c8ebc7 EB |
6168 | /* Try to promote the mode if the return type is fully returned |
6169 | in integer registers, again to speed up accesses. */ | |
7414a3c3 | 6170 | if (TYPE_MODE (gnu_cico_return_type) == BLKmode |
64c8ebc7 | 6171 | && gnu_cico_only_integral_type |
7414a3c3 EB |
6172 | && !targetm.calls.return_in_memory (gnu_cico_return_type, |
6173 | NULL_TREE)) | |
1e55d29a EB |
6174 | { |
6175 | unsigned int size | |
7414a3c3 | 6176 | = TREE_INT_CST_LOW (TYPE_SIZE (gnu_cico_return_type)); |
1e55d29a | 6177 | unsigned int i = BITS_PER_UNIT; |
fffbab82 | 6178 | scalar_int_mode mode; |
1e55d29a EB |
6179 | |
6180 | while (i < size) | |
6181 | i <<= 1; | |
fffbab82 | 6182 | if (int_mode_for_size (i, 0).exists (&mode)) |
1e55d29a | 6183 | { |
7414a3c3 EB |
6184 | SET_TYPE_MODE (gnu_cico_return_type, mode); |
6185 | SET_TYPE_ALIGN (gnu_cico_return_type, | |
6186 | GET_MODE_ALIGNMENT (mode)); | |
6187 | TYPE_SIZE (gnu_cico_return_type) | |
1e55d29a | 6188 | = bitsize_int (GET_MODE_BITSIZE (mode)); |
7414a3c3 | 6189 | TYPE_SIZE_UNIT (gnu_cico_return_type) |
1e55d29a EB |
6190 | = size_int (GET_MODE_SIZE (mode)); |
6191 | } | |
6192 | } | |
6193 | ||
64c8ebc7 EB |
6194 | /* But demote the mode if the return type is partly returned in FP |
6195 | registers to avoid creating problematic paradoxical subregs. | |
6196 | Note that we need to cater to historical 32-bit architectures | |
6197 | that incorrectly use the mode to select the return mechanism. */ | |
6198 | else if (INTEGRAL_MODE_P (TYPE_MODE (gnu_cico_return_type)) | |
6199 | && !gnu_cico_only_integral_type | |
6200 | && BITS_PER_WORD >= 64 | |
6201 | && !targetm.calls.return_in_memory (gnu_cico_return_type, | |
6202 | NULL_TREE)) | |
6203 | SET_TYPE_MODE (gnu_cico_return_type, BLKmode); | |
6204 | ||
1e55d29a | 6205 | if (debug_info_p) |
7414a3c3 | 6206 | rest_of_record_type_compilation (gnu_cico_return_type); |
1e55d29a | 6207 | } |
7414a3c3 EB |
6208 | |
6209 | gnu_return_type = gnu_cico_return_type; | |
1e55d29a EB |
6210 | } |
6211 | ||
6212 | /* The lists have been built in reverse. */ | |
6213 | gnu_param_type_list = nreverse (gnu_param_type_list); | |
c95f808d EB |
6214 | if (!variadic) |
6215 | gnu_param_type_list = chainon (gnu_param_type_list, void_list_node); | |
69720717 | 6216 | gnu_param_list = nreverse (gnu_param_list); |
1e55d29a EB |
6217 | gnu_cico_list = nreverse (gnu_cico_list); |
6218 | ||
69720717 EB |
6219 | /* Turn imported C++ constructors into their callable form as done in the |
6220 | front-end, i.e. add the "this" pointer and void the return type. */ | |
6221 | if (method_p | |
6222 | && Is_Constructor (gnat_subprog) | |
6223 | && !VOID_TYPE_P (gnu_return_type)) | |
6224 | { | |
6225 | tree gnu_param_type | |
6226 | = build_pointer_type (gnat_to_gnu_profile_type (gnat_return_type)); | |
6227 | tree gnu_param_name = get_identifier (Get_Name_String (Name_uInit)); | |
6228 | tree gnu_param | |
6229 | = build_decl (input_location, PARM_DECL, gnu_param_name, | |
6230 | gnu_param_type); | |
6231 | gnu_param_type_list | |
6232 | = tree_cons (NULL_TREE, gnu_param_type, gnu_param_type_list); | |
6233 | DECL_CHAIN (gnu_param) = gnu_param_list; | |
6234 | gnu_param_list = gnu_param; | |
6235 | gnu_return_type = void_type_node; | |
6236 | } | |
6237 | ||
1e55d29a EB |
6238 | /* If the profile is incomplete, we only set the (temporary) return and |
6239 | parameter types; otherwise, we build the full type. In either case, | |
6240 | we reuse an already existing GCC tree that we built previously here. */ | |
1e55d29a EB |
6241 | if (incomplete_profile_p) |
6242 | { | |
69720717 | 6243 | if (gnu_type && FUNC_OR_METHOD_TYPE_P (gnu_type)) |
1e55d29a EB |
6244 | ; |
6245 | else | |
69720717 | 6246 | gnu_type = make_node (method_p ? METHOD_TYPE : FUNCTION_TYPE); |
1e55d29a EB |
6247 | TREE_TYPE (gnu_type) = gnu_return_type; |
6248 | TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list; | |
7414a3c3 EB |
6249 | TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p; |
6250 | TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p; | |
6251 | TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p; | |
1e55d29a EB |
6252 | } |
6253 | else | |
6254 | { | |
69720717 | 6255 | if (gnu_type && FUNC_OR_METHOD_TYPE_P (gnu_type)) |
1e55d29a EB |
6256 | { |
6257 | TREE_TYPE (gnu_type) = gnu_return_type; | |
6258 | TYPE_ARG_TYPES (gnu_type) = gnu_param_type_list; | |
69720717 EB |
6259 | if (method_p) |
6260 | { | |
6261 | tree gnu_basetype = TREE_TYPE (TREE_VALUE (gnu_param_type_list)); | |
6262 | TYPE_METHOD_BASETYPE (gnu_type) | |
6263 | = TYPE_MAIN_VARIANT (gnu_basetype); | |
6264 | } | |
1e55d29a EB |
6265 | TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list; |
6266 | TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p; | |
6267 | TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p; | |
6268 | TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p; | |
6269 | TYPE_CANONICAL (gnu_type) = gnu_type; | |
6270 | layout_type (gnu_type); | |
6271 | } | |
6272 | else | |
6273 | { | |
69720717 EB |
6274 | if (method_p) |
6275 | { | |
6276 | tree gnu_basetype = TREE_TYPE (TREE_VALUE (gnu_param_type_list)); | |
6277 | gnu_type | |
6278 | = build_method_type_directly (gnu_basetype, gnu_return_type, | |
6279 | TREE_CHAIN (gnu_param_type_list)); | |
6280 | } | |
6281 | else | |
6282 | gnu_type | |
6283 | = build_function_type (gnu_return_type, gnu_param_type_list); | |
1e55d29a EB |
6284 | |
6285 | /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it | |
6286 | has a different TYPE_CI_CO_LIST or flags. */ | |
6287 | if (!fntype_same_flags_p (gnu_type, gnu_cico_list, | |
6288 | return_unconstrained_p, | |
6289 | return_by_direct_ref_p, | |
6290 | return_by_invisi_ref_p)) | |
6291 | { | |
6292 | gnu_type = copy_type (gnu_type); | |
6293 | TYPE_CI_CO_LIST (gnu_type) = gnu_cico_list; | |
6294 | TYPE_RETURN_UNCONSTRAINED_P (gnu_type) = return_unconstrained_p; | |
6295 | TYPE_RETURN_BY_DIRECT_REF_P (gnu_type) = return_by_direct_ref_p; | |
6296 | TREE_ADDRESSABLE (gnu_type) = return_by_invisi_ref_p; | |
6297 | } | |
6298 | } | |
6299 | ||
71836434 EB |
6300 | if (pure_flag) |
6301 | gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_RESTRICT); | |
6302 | ||
1e55d29a EB |
6303 | if (No_Return (gnat_subprog)) |
6304 | gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE); | |
7414a3c3 EB |
6305 | |
6306 | /* If this subprogram is expectedly bound to a GCC builtin, fetch the | |
6307 | corresponding DECL node and check the parameter association. */ | |
abb540a7 | 6308 | if (Is_Intrinsic_Subprogram (gnat_subprog) |
7414a3c3 EB |
6309 | && Present (Interface_Name (gnat_subprog))) |
6310 | { | |
6311 | tree gnu_ext_name = create_concat_name (gnat_subprog, NULL); | |
6312 | tree gnu_builtin_decl = builtin_decl_for (gnu_ext_name); | |
6313 | ||
6314 | /* If we have a builtin DECL for that function, use it. Check if | |
6315 | the profiles are compatible and warn if they are not. Note that | |
6316 | the checker is expected to post diagnostics in this case. */ | |
6317 | if (gnu_builtin_decl) | |
6318 | { | |
a40970cf EB |
6319 | if (fndecl_built_in_p (gnu_builtin_decl, BUILT_IN_NORMAL)) |
6320 | { | |
6321 | const enum built_in_function fncode | |
6322 | = DECL_FUNCTION_CODE (gnu_builtin_decl); | |
6323 | ||
6324 | switch (fncode) | |
6325 | { | |
6326 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: | |
6327 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
6328 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
6329 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
6330 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
6331 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
6332 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
6333 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
6334 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
6335 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
6336 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
6337 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
6338 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
6339 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
6340 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
6341 | case BUILT_IN_ATOMIC_LOAD_N: | |
6342 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
6343 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
6344 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
6345 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
6346 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
6347 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
6348 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
6349 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
6350 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
6351 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
6352 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
6353 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
6354 | /* This is a generic builtin overloaded on its return | |
6355 | type, so do type resolution based on it. */ | |
6356 | if (!VOID_TYPE_P (gnu_return_type) | |
6357 | && type_for_atomic_builtin_p (gnu_return_type)) | |
6358 | gnu_builtin_decl | |
6359 | = resolve_atomic_builtin (fncode, gnu_return_type); | |
6360 | else | |
6361 | { | |
6362 | post_error | |
6363 | ("??cannot import type-generic 'G'C'C builtin!", | |
6364 | gnat_subprog); | |
6365 | post_error | |
6366 | ("\\?use a supported result type", | |
6367 | gnat_subprog); | |
6368 | gnu_builtin_decl = NULL_TREE; | |
6369 | } | |
6370 | break; | |
6371 | ||
6372 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
6373 | /* This is a generic builtin overloaded on its third | |
6374 | parameter type, so do type resolution based on it. */ | |
6375 | if (list_length (gnu_param_type_list) >= 4 | |
6376 | && type_for_atomic_builtin_p | |
6377 | (list_third (gnu_param_type_list))) | |
6378 | gnu_builtin_decl | |
6379 | = resolve_atomic_builtin | |
6380 | (fncode, list_third (gnu_param_type_list)); | |
6381 | else | |
6382 | { | |
6383 | post_error | |
6384 | ("??cannot import type-generic 'G'C'C builtin!", | |
6385 | gnat_subprog); | |
6386 | post_error | |
6387 | ("\\?use a supported third parameter type", | |
6388 | gnat_subprog); | |
6389 | gnu_builtin_decl = NULL_TREE; | |
6390 | } | |
6391 | break; | |
6392 | ||
6393 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
6394 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
6395 | case BUILT_IN_ATOMIC_STORE_N: | |
6396 | post_error | |
6397 | ("??unsupported type-generic 'G'C'C builtin!", | |
6398 | gnat_subprog); | |
6399 | gnu_builtin_decl = NULL_TREE; | |
6400 | break; | |
6401 | ||
6402 | default: | |
6403 | break; | |
6404 | } | |
6405 | } | |
6406 | ||
6407 | if (gnu_builtin_decl) | |
6408 | { | |
6409 | const intrin_binding_t inb | |
6410 | = { gnat_subprog, gnu_type, TREE_TYPE (gnu_builtin_decl) }; | |
6411 | ||
6412 | if (!intrin_profiles_compatible_p (&inb)) | |
6413 | post_error | |
6414 | ("??profile of& doesn''t match the builtin it binds!", | |
6415 | gnat_subprog); | |
6416 | ||
6417 | return gnu_builtin_decl; | |
6418 | } | |
7414a3c3 EB |
6419 | } |
6420 | ||
6421 | /* Inability to find the builtin DECL most often indicates a genuine | |
6422 | mistake, but imports of unregistered intrinsics are sometimes used | |
6423 | on purpose to allow hooking in alternate bodies; we post a warning | |
6424 | conditioned on Wshadow in this case, to let developers be notified | |
6425 | on demand without risking false positives with common default sets | |
6426 | of options. */ | |
6427 | if (warn_shadow) | |
a40970cf | 6428 | post_error ("'G'C'C builtin not found for&!??", gnat_subprog); |
7414a3c3 | 6429 | } |
1e55d29a EB |
6430 | } |
6431 | ||
69720717 EB |
6432 | *param_list = gnu_param_list; |
6433 | ||
1e55d29a | 6434 | return gnu_type; |
cb55aefb EB |
6435 | } |
6436 | ||
7414a3c3 EB |
6437 | /* Return the external name for GNAT_SUBPROG given its entity name. */ |
6438 | ||
6439 | static tree | |
6440 | gnu_ext_name_for_subprog (Entity_Id gnat_subprog, tree gnu_entity_name) | |
6441 | { | |
6442 | tree gnu_ext_name = create_concat_name (gnat_subprog, NULL); | |
6443 | ||
6444 | /* If there was no specified Interface_Name and the external and | |
6445 | internal names of the subprogram are the same, only use the | |
6446 | internal name to allow disambiguation of nested subprograms. */ | |
6447 | if (No (Interface_Name (gnat_subprog)) && gnu_ext_name == gnu_entity_name) | |
6448 | gnu_ext_name = NULL_TREE; | |
6449 | ||
6450 | return gnu_ext_name; | |
6451 | } | |
6452 | ||
d42b7559 EB |
6453 | /* Set TYPE_NONALIASED_COMPONENT on an array type built by means of |
6454 | build_nonshared_array_type. */ | |
6455 | ||
6456 | static void | |
6457 | set_nonaliased_component_on_array_type (tree type) | |
6458 | { | |
6459 | TYPE_NONALIASED_COMPONENT (type) = 1; | |
d9888378 EB |
6460 | if (TYPE_CANONICAL (type)) |
6461 | TYPE_NONALIASED_COMPONENT (TYPE_CANONICAL (type)) = 1; | |
d42b7559 EB |
6462 | } |
6463 | ||
6464 | /* Set TYPE_REVERSE_STORAGE_ORDER on an array type built by means of | |
6465 | build_nonshared_array_type. */ | |
6466 | ||
6467 | static void | |
6468 | set_reverse_storage_order_on_array_type (tree type) | |
6469 | { | |
6470 | TYPE_REVERSE_STORAGE_ORDER (type) = 1; | |
d9888378 EB |
6471 | if (TYPE_CANONICAL (type)) |
6472 | TYPE_REVERSE_STORAGE_ORDER (TYPE_CANONICAL (type)) = 1; | |
d42b7559 EB |
6473 | } |
6474 | ||
a1ab4c31 AC |
6475 | /* Return true if DISCR1 and DISCR2 represent the same discriminant. */ |
6476 | ||
6477 | static bool | |
6478 | same_discriminant_p (Entity_Id discr1, Entity_Id discr2) | |
6479 | { | |
6480 | while (Present (Corresponding_Discriminant (discr1))) | |
6481 | discr1 = Corresponding_Discriminant (discr1); | |
6482 | ||
6483 | while (Present (Corresponding_Discriminant (discr2))) | |
6484 | discr2 = Corresponding_Discriminant (discr2); | |
6485 | ||
6486 | return | |
6487 | Original_Record_Component (discr1) == Original_Record_Component (discr2); | |
6488 | } | |
6489 | ||
d8e94f79 EB |
6490 | /* Return true if the array type GNU_TYPE, which represents a dimension of |
6491 | GNAT_TYPE, has a non-aliased component in the back-end sense. */ | |
a1ab4c31 AC |
6492 | |
6493 | static bool | |
d8e94f79 | 6494 | array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type) |
a1ab4c31 | 6495 | { |
d8e94f79 EB |
6496 | /* If the array type has an aliased component in the front-end sense, |
6497 | then it also has an aliased component in the back-end sense. */ | |
a1ab4c31 AC |
6498 | if (Has_Aliased_Components (gnat_type)) |
6499 | return false; | |
6500 | ||
d8e94f79 EB |
6501 | /* If this is a derived type, then it has a non-aliased component if |
6502 | and only if its parent type also has one. */ | |
6503 | if (Is_Derived_Type (gnat_type)) | |
6504 | { | |
6505 | tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type)); | |
d8e94f79 EB |
6506 | if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE) |
6507 | gnu_parent_type | |
6508 | = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type)))); | |
d8e94f79 EB |
6509 | return TYPE_NONALIASED_COMPONENT (gnu_parent_type); |
6510 | } | |
6511 | ||
33731c66 EB |
6512 | /* For a multi-dimensional array type, find the component type. */ |
6513 | while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE | |
6514 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) | |
6515 | gnu_type = TREE_TYPE (gnu_type); | |
6516 | ||
dacdc68f EB |
6517 | /* Consider that an array of pointers has an aliased component, which is |
6518 | sort of logical and helps with Taft Amendment types in LTO mode. */ | |
6519 | if (POINTER_TYPE_P (TREE_TYPE (gnu_type))) | |
6520 | return false; | |
6521 | ||
d8e94f79 | 6522 | /* Otherwise, rely exclusively on properties of the element type. */ |
a1ab4c31 AC |
6523 | return type_for_nonaliased_component_p (TREE_TYPE (gnu_type)); |
6524 | } | |
229077b0 EB |
6525 | |
6526 | /* Return true if GNAT_ADDRESS is a value known at compile-time. */ | |
6527 | ||
6528 | static bool | |
6529 | compile_time_known_address_p (Node_Id gnat_address) | |
6530 | { | |
abb3ea16 TG |
6531 | /* Handle reference to a constant. */ |
6532 | if (Is_Entity_Name (gnat_address) | |
6533 | && Ekind (Entity (gnat_address)) == E_Constant) | |
6534 | { | |
6535 | gnat_address = Constant_Value (Entity (gnat_address)); | |
6536 | if (No (gnat_address)) | |
6537 | return false; | |
6538 | } | |
6539 | ||
229077b0 EB |
6540 | /* Catch System'To_Address. */ |
6541 | if (Nkind (gnat_address) == N_Unchecked_Type_Conversion) | |
6542 | gnat_address = Expression (gnat_address); | |
6543 | ||
6544 | return Compile_Time_Known_Value (gnat_address); | |
6545 | } | |
f45f9664 | 6546 | |
3ccd5d71 EB |
6547 | /* Return true if GNAT_INDIC, a N_Subtype_Indication node for the index of a |
6548 | FLB, cannot yield superflat objects, i.e. if the inequality HB >= LB - 1 | |
6549 | is true for these objects. LB and HB are the low and high bounds. */ | |
6550 | ||
6551 | static bool | |
6552 | flb_cannot_be_superflat (Node_Id gnat_indic) | |
6553 | { | |
6554 | const Entity_Id gnat_type = Entity (Subtype_Mark (gnat_indic)); | |
6555 | const Entity_Id gnat_subtype = Etype (gnat_indic); | |
6556 | Node_Id gnat_scalar_range, gnat_lb, gnat_hb; | |
6557 | tree gnu_lb, gnu_hb, gnu_lb_minus_one; | |
6558 | ||
6559 | /* This is a FLB so LB is fixed. */ | |
6560 | if ((Ekind (gnat_subtype) == E_Signed_Integer_Subtype | |
6561 | || Ekind (gnat_subtype) == E_Modular_Integer_Subtype) | |
6562 | && (gnat_scalar_range = Scalar_Range (gnat_subtype))) | |
6563 | { | |
6564 | gnat_lb = Low_Bound (gnat_scalar_range); | |
6565 | gcc_assert (Nkind (gnat_lb) == N_Integer_Literal); | |
6566 | } | |
6567 | else | |
6568 | return false; | |
6569 | ||
6570 | /* The low bound of the type is a lower bound for HB. */ | |
6571 | if ((Ekind (gnat_type) == E_Signed_Integer_Subtype | |
6572 | || Ekind (gnat_type) == E_Modular_Integer_Subtype) | |
6573 | && (gnat_scalar_range = Scalar_Range (gnat_type))) | |
6574 | { | |
6575 | gnat_hb = Low_Bound (gnat_scalar_range); | |
6576 | gcc_assert (Nkind (gnat_hb) == N_Integer_Literal); | |
6577 | } | |
6578 | else | |
6579 | return false; | |
6580 | ||
6581 | /* We need at least a signed 64-bit type to catch most cases. */ | |
6582 | gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype); | |
6583 | gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype); | |
6584 | if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb)) | |
6585 | return false; | |
6586 | ||
6587 | /* If the low bound is the smallest integer, nothing can be smaller. */ | |
6588 | gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node); | |
6589 | if (TREE_OVERFLOW (gnu_lb_minus_one)) | |
6590 | return true; | |
6591 | ||
6592 | return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one); | |
6593 | } | |
6594 | ||
58c8f770 | 6595 | /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the |
3ccd5d71 | 6596 | inequality HB >= LB - 1 is true. LB and HB are the low and high bounds. */ |
f45f9664 EB |
6597 | |
6598 | static bool | |
3ccd5d71 | 6599 | range_cannot_be_superflat (Node_Id gnat_range) |
f45f9664 EB |
6600 | { |
6601 | Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range); | |
3ccd5d71 | 6602 | Node_Id gnat_scalar_range; |
1081f5a7 | 6603 | tree gnu_lb, gnu_hb, gnu_lb_minus_one; |
f45f9664 | 6604 | |
3ae5e6fb EB |
6605 | /* If the low bound is not constant, take the worst case by finding an upper |
6606 | bound for its type, repeatedly if need be. */ | |
f45f9664 EB |
6607 | while (Nkind (gnat_lb) != N_Integer_Literal |
6608 | && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype | |
6609 | || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype) | |
3ccd5d71 EB |
6610 | && (gnat_scalar_range = Scalar_Range (Etype (gnat_lb))) |
6611 | && (Nkind (gnat_scalar_range) == N_Signed_Integer_Type_Definition | |
6612 | || Nkind (gnat_scalar_range) == N_Range)) | |
6613 | gnat_lb = High_Bound (gnat_scalar_range); | |
f45f9664 | 6614 | |
3ae5e6fb EB |
6615 | /* If the high bound is not constant, take the worst case by finding a lower |
6616 | bound for its type, repeatedly if need be. */ | |
f45f9664 EB |
6617 | while (Nkind (gnat_hb) != N_Integer_Literal |
6618 | && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype | |
6619 | || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype) | |
3ccd5d71 EB |
6620 | && (gnat_scalar_range = Scalar_Range (Etype (gnat_hb))) |
6621 | && (Nkind (gnat_scalar_range) == N_Signed_Integer_Type_Definition | |
6622 | || Nkind (gnat_scalar_range) == N_Range)) | |
6623 | gnat_hb = Low_Bound (gnat_scalar_range); | |
f45f9664 | 6624 | |
1081f5a7 EB |
6625 | /* If we have failed to find constant bounds, punt. */ |
6626 | if (Nkind (gnat_lb) != N_Integer_Literal | |
6627 | || Nkind (gnat_hb) != N_Integer_Literal) | |
f45f9664 EB |
6628 | return false; |
6629 | ||
1081f5a7 EB |
6630 | /* We need at least a signed 64-bit type to catch most cases. */ |
6631 | gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype); | |
6632 | gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype); | |
6633 | if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb)) | |
6634 | return false; | |
f45f9664 EB |
6635 | |
6636 | /* If the low bound is the smallest integer, nothing can be smaller. */ | |
1081f5a7 EB |
6637 | gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node); |
6638 | if (TREE_OVERFLOW (gnu_lb_minus_one)) | |
f45f9664 EB |
6639 | return true; |
6640 | ||
1081f5a7 | 6641 | return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one); |
f45f9664 | 6642 | } |
cb3d597d EB |
6643 | |
6644 | /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */ | |
6645 | ||
6646 | static bool | |
6647 | constructor_address_p (tree gnu_expr) | |
6648 | { | |
6649 | while (TREE_CODE (gnu_expr) == NOP_EXPR | |
6650 | || TREE_CODE (gnu_expr) == CONVERT_EXPR | |
6651 | || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR) | |
6652 | gnu_expr = TREE_OPERAND (gnu_expr, 0); | |
6653 | ||
6654 | return (TREE_CODE (gnu_expr) == ADDR_EXPR | |
6655 | && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR); | |
6656 | } | |
fc7a823e EB |
6657 | |
6658 | /* Return true if the size in units represented by GNU_SIZE can be handled by | |
6659 | an allocation. If STATIC_P is true, consider only what can be done with a | |
6660 | static allocation. */ | |
6661 | ||
6662 | static bool | |
6663 | allocatable_size_p (tree gnu_size, bool static_p) | |
6664 | { | |
6665 | /* We can allocate a fixed size if it is a valid for the middle-end. */ | |
6666 | if (TREE_CODE (gnu_size) == INTEGER_CST) | |
6667 | return valid_constant_size_p (gnu_size); | |
6668 | ||
6669 | /* We can allocate a variable size if this isn't a static allocation. */ | |
6670 | else | |
6671 | return !static_p; | |
6672 | } | |
6673 | ||
6674 | /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the | |
6675 | initial value of an object of GNU_TYPE. */ | |
6676 | ||
6677 | static bool | |
6678 | initial_value_needs_conversion (tree gnu_type, tree gnu_expr) | |
6679 | { | |
6680 | /* Do not convert if the object's type is unconstrained because this would | |
6681 | generate useless evaluations of the CONSTRUCTOR to compute the size. */ | |
6682 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE | |
6683 | || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))) | |
6684 | return false; | |
6685 | ||
6686 | /* Do not convert if the object's type is a padding record whose field is of | |
6687 | self-referential size because we want to copy only the actual data. */ | |
6688 | if (type_is_padding_self_referential (gnu_type)) | |
6689 | return false; | |
6690 | ||
6691 | /* Do not convert a call to a function that returns with variable size since | |
6692 | we want to use the return slot optimization in this case. */ | |
6693 | if (TREE_CODE (gnu_expr) == CALL_EXPR | |
6694 | && return_type_with_variable_size_p (TREE_TYPE (gnu_expr))) | |
6695 | return false; | |
6696 | ||
6697 | /* Do not convert to a record type with a variant part from a record type | |
6698 | without one, to keep the object simpler. */ | |
6699 | if (TREE_CODE (gnu_type) == RECORD_TYPE | |
6700 | && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE | |
7c775aca EB |
6701 | && get_variant_part (gnu_type) |
6702 | && !get_variant_part (TREE_TYPE (gnu_expr))) | |
fc7a823e EB |
6703 | return false; |
6704 | ||
6705 | /* In all the other cases, convert the expression to the object's type. */ | |
6706 | return true; | |
6707 | } | |
683ccd05 EB |
6708 | |
6709 | /* Add the contribution of [MIN, MAX] to the current number of elements N_ELEM | |
6710 | of an array type and return the result, or NULL_TREE if it overflowed. */ | |
6711 | ||
6712 | static tree | |
6713 | update_n_elem (tree n_elem, tree min, tree max) | |
6714 | { | |
6715 | /* First deal with the empty case. */ | |
6716 | if (TREE_CODE (min) == INTEGER_CST | |
6717 | && TREE_CODE (max) == INTEGER_CST | |
6718 | && tree_int_cst_lt (max, min)) | |
6719 | return size_zero_node; | |
6720 | ||
6721 | min = convert (sizetype, min); | |
6722 | max = convert (sizetype, max); | |
6723 | ||
6724 | /* Compute the number of elements in this dimension. */ | |
6725 | tree this_n_elem | |
6726 | = size_binop (PLUS_EXPR, size_one_node, size_binop (MINUS_EXPR, max, min)); | |
6727 | ||
6728 | if (TREE_CODE (this_n_elem) == INTEGER_CST && TREE_OVERFLOW (this_n_elem)) | |
6729 | return NULL_TREE; | |
6730 | ||
6731 | /* Multiply the current number of elements by the result. */ | |
6732 | n_elem = size_binop (MULT_EXPR, n_elem, this_n_elem); | |
6733 | ||
6734 | if (TREE_CODE (n_elem) == INTEGER_CST && TREE_OVERFLOW (n_elem)) | |
6735 | return NULL_TREE; | |
6736 | ||
6737 | return n_elem; | |
6738 | } | |
ce2d0ce2 | 6739 | |
a1ab4c31 AC |
6740 | /* Given GNAT_ENTITY, elaborate all expressions that are required to |
6741 | be elaborated at the point of its definition, but do nothing else. */ | |
6742 | ||
6743 | void | |
6744 | elaborate_entity (Entity_Id gnat_entity) | |
6745 | { | |
6746 | switch (Ekind (gnat_entity)) | |
6747 | { | |
6748 | case E_Signed_Integer_Subtype: | |
6749 | case E_Modular_Integer_Subtype: | |
6750 | case E_Enumeration_Subtype: | |
6751 | case E_Ordinary_Fixed_Point_Subtype: | |
6752 | case E_Decimal_Fixed_Point_Subtype: | |
6753 | case E_Floating_Point_Subtype: | |
6754 | { | |
6755 | Node_Id gnat_lb = Type_Low_Bound (gnat_entity); | |
6756 | Node_Id gnat_hb = Type_High_Bound (gnat_entity); | |
6757 | ||
c1abd261 EB |
6758 | /* ??? Tests to avoid Constraint_Error in static expressions |
6759 | are needed until after the front stops generating bogus | |
6760 | conversions on bounds of real types. */ | |
a1ab4c31 | 6761 | if (!Raises_Constraint_Error (gnat_lb)) |
bf44701f EB |
6762 | elaborate_expression (gnat_lb, gnat_entity, "L", true, false, |
6763 | Needs_Debug_Info (gnat_entity)); | |
a1ab4c31 | 6764 | if (!Raises_Constraint_Error (gnat_hb)) |
bf44701f EB |
6765 | elaborate_expression (gnat_hb, gnat_entity, "U", true, false, |
6766 | Needs_Debug_Info (gnat_entity)); | |
a1ab4c31 AC |
6767 | break; |
6768 | } | |
6769 | ||
a1ab4c31 AC |
6770 | case E_Record_Subtype: |
6771 | case E_Private_Subtype: | |
6772 | case E_Limited_Private_Subtype: | |
6773 | case E_Record_Subtype_With_Private: | |
a8c4c75a | 6774 | if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity)) |
a1ab4c31 AC |
6775 | { |
6776 | Node_Id gnat_discriminant_expr; | |
6777 | Entity_Id gnat_field; | |
6778 | ||
8cd28148 EB |
6779 | for (gnat_field |
6780 | = First_Discriminant (Implementation_Base_Type (gnat_entity)), | |
a1ab4c31 AC |
6781 | gnat_discriminant_expr |
6782 | = First_Elmt (Discriminant_Constraint (gnat_entity)); | |
6783 | Present (gnat_field); | |
6784 | gnat_field = Next_Discriminant (gnat_field), | |
6785 | gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr)) | |
908ba941 | 6786 | /* Ignore access discriminants. */ |
a1ab4c31 AC |
6787 | if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr)))) |
6788 | elaborate_expression (Node (gnat_discriminant_expr), | |
bf44701f | 6789 | gnat_entity, get_entity_char (gnat_field), |
a531043b | 6790 | true, false, false); |
a1ab4c31 AC |
6791 | } |
6792 | break; | |
6793 | ||
6794 | } | |
6795 | } | |
ce2d0ce2 | 6796 | |
a1ab4c31 AC |
6797 | /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE, |
6798 | NAME, ARGS and ERROR_POINT. */ | |
6799 | ||
6800 | static void | |
0567ae8d | 6801 | prepend_one_attribute (struct attrib **attr_list, |
e0ef6912 | 6802 | enum attrib_type attrib_type, |
0567ae8d AC |
6803 | tree attr_name, |
6804 | tree attr_args, | |
6805 | Node_Id attr_error_point) | |
a1ab4c31 AC |
6806 | { |
6807 | struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib)); | |
6808 | ||
e0ef6912 | 6809 | attr->type = attrib_type; |
a1ab4c31 AC |
6810 | attr->name = attr_name; |
6811 | attr->args = attr_args; | |
6812 | attr->error_point = attr_error_point; | |
6813 | ||
6814 | attr->next = *attr_list; | |
6815 | *attr_list = attr; | |
6816 | } | |
6817 | ||
0567ae8d | 6818 | /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */ |
a1ab4c31 AC |
6819 | |
6820 | static void | |
0567ae8d | 6821 | prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma) |
a1ab4c31 | 6822 | { |
5ca5ef68 EB |
6823 | const Node_Id gnat_arg = First (Pragma_Argument_Associations (gnat_pragma)); |
6824 | Node_Id gnat_next_arg = Next (gnat_arg); | |
6825 | tree gnu_arg1 = NULL_TREE, gnu_arg_list = NULL_TREE; | |
e0ef6912 | 6826 | enum attrib_type etype; |
d81b4c61 | 6827 | |
0567ae8d AC |
6828 | /* Map the pragma at hand. Skip if this isn't one we know how to handle. */ |
6829 | switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma)))) | |
6830 | { | |
0567ae8d AC |
6831 | case Pragma_Linker_Alias: |
6832 | etype = ATTR_LINK_ALIAS; | |
6833 | break; | |
a1ab4c31 | 6834 | |
0567ae8d AC |
6835 | case Pragma_Linker_Constructor: |
6836 | etype = ATTR_LINK_CONSTRUCTOR; | |
6837 | break; | |
a1ab4c31 | 6838 | |
0567ae8d AC |
6839 | case Pragma_Linker_Destructor: |
6840 | etype = ATTR_LINK_DESTRUCTOR; | |
6841 | break; | |
a1ab4c31 | 6842 | |
5ca5ef68 EB |
6843 | case Pragma_Linker_Section: |
6844 | etype = ATTR_LINK_SECTION; | |
6845 | break; | |
6846 | ||
6847 | case Pragma_Machine_Attribute: | |
6848 | etype = ATTR_MACHINE_ATTRIBUTE; | |
0567ae8d | 6849 | break; |
a1ab4c31 | 6850 | |
0567ae8d AC |
6851 | case Pragma_Thread_Local_Storage: |
6852 | etype = ATTR_THREAD_LOCAL_STORAGE; | |
6853 | break; | |
a1ab4c31 | 6854 | |
5ca5ef68 EB |
6855 | case Pragma_Weak_External: |
6856 | etype = ATTR_WEAK_EXTERNAL; | |
6857 | break; | |
6858 | ||
0567ae8d AC |
6859 | default: |
6860 | return; | |
6861 | } | |
a1ab4c31 | 6862 | |
0567ae8d | 6863 | /* See what arguments we have and turn them into GCC trees for attribute |
5ca5ef68 EB |
6864 | handlers. The first one is always expected to be a string meant to be |
6865 | turned into an identifier. The next ones are all static expressions, | |
6866 | among which strings meant to be turned into an identifier, except for | |
6867 | a couple of specific attributes that require raw strings. */ | |
6868 | if (Present (gnat_next_arg)) | |
0567ae8d | 6869 | { |
5ca5ef68 EB |
6870 | gnu_arg1 = gnat_to_gnu (Expression (gnat_next_arg)); |
6871 | gcc_assert (TREE_CODE (gnu_arg1) == STRING_CST); | |
6872 | ||
6873 | const char *const p = TREE_STRING_POINTER (gnu_arg1); | |
6874 | const bool string_args | |
6875 | = strcmp (p, "target") == 0 || strcmp (p, "target_clones") == 0; | |
6876 | gnu_arg1 = get_identifier (p); | |
6877 | if (IDENTIFIER_LENGTH (gnu_arg1) == 0) | |
6878 | return; | |
6879 | gnat_next_arg = Next (gnat_next_arg); | |
6880 | ||
6881 | while (Present (gnat_next_arg)) | |
0567ae8d | 6882 | { |
5ca5ef68 EB |
6883 | tree gnu_arg = gnat_to_gnu (Expression (gnat_next_arg)); |
6884 | if (TREE_CODE (gnu_arg) == STRING_CST && !string_args) | |
6885 | gnu_arg = get_identifier (TREE_STRING_POINTER (gnu_arg)); | |
6886 | gnu_arg_list | |
6887 | = chainon (gnu_arg_list, build_tree_list (NULL_TREE, gnu_arg)); | |
6888 | gnat_next_arg = Next (gnat_next_arg); | |
0567ae8d AC |
6889 | } |
6890 | } | |
d81b4c61 | 6891 | |
5ca5ef68 EB |
6892 | prepend_one_attribute (attr_list, etype, gnu_arg1, gnu_arg_list, |
6893 | Present (Next (gnat_arg)) | |
6894 | ? Expression (Next (gnat_arg)) : gnat_pragma); | |
0567ae8d | 6895 | } |
d81b4c61 | 6896 | |
0567ae8d | 6897 | /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */ |
d81b4c61 | 6898 | |
0567ae8d AC |
6899 | static void |
6900 | prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity) | |
6901 | { | |
6902 | Node_Id gnat_temp; | |
a1ab4c31 | 6903 | |
0567ae8d AC |
6904 | /* Attributes are stored as Representation Item pragmas. */ |
6905 | for (gnat_temp = First_Rep_Item (gnat_entity); | |
6906 | Present (gnat_temp); | |
6907 | gnat_temp = Next_Rep_Item (gnat_temp)) | |
6908 | if (Nkind (gnat_temp) == N_Pragma) | |
6909 | prepend_one_attribute_pragma (attr_list, gnat_temp); | |
a1ab4c31 | 6910 | } |
ce2d0ce2 | 6911 | |
a1ab4c31 AC |
6912 | /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a |
6913 | type definition (either a bound or a discriminant value) for GNAT_ENTITY, | |
bf44701f | 6914 | return the GCC tree to use for that expression. S is the suffix to use |
241125b2 | 6915 | if a variable needs to be created and DEFINITION is true if this is done |
bf44701f | 6916 | for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result; |
a531043b | 6917 | otherwise, we are just elaborating the expression for side-effects. If |
3553d8c2 EB |
6918 | NEED_FOR_DEBUG is true, we need a variable for debugging purposes even |
6919 | if it isn't needed for code generation. */ | |
a1ab4c31 AC |
6920 | |
6921 | static tree | |
bf44701f | 6922 | elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s, |
3553d8c2 | 6923 | bool definition, bool need_value, bool need_for_debug) |
a1ab4c31 AC |
6924 | { |
6925 | tree gnu_expr; | |
6926 | ||
a531043b | 6927 | /* If we already elaborated this expression (e.g. it was involved |
a1ab4c31 AC |
6928 | in the definition of a private type), use the old value. */ |
6929 | if (present_gnu_tree (gnat_expr)) | |
6930 | return get_gnu_tree (gnat_expr); | |
6931 | ||
a531043b EB |
6932 | /* If we don't need a value and this is static or a discriminant, |
6933 | we don't need to do anything. */ | |
6934 | if (!need_value | |
cd42cdc2 | 6935 | && (Compile_Time_Known_Value (gnat_expr) |
a531043b EB |
6936 | || (Nkind (gnat_expr) == N_Identifier |
6937 | && Ekind (Entity (gnat_expr)) == E_Discriminant))) | |
6938 | return NULL_TREE; | |
6939 | ||
6940 | /* If it's a static expression, we don't need a variable for debugging. */ | |
3553d8c2 EB |
6941 | if (need_for_debug && Compile_Time_Known_Value (gnat_expr)) |
6942 | need_for_debug = false; | |
a1ab4c31 | 6943 | |
a531043b | 6944 | /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */ |
bf44701f | 6945 | gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s, |
3553d8c2 | 6946 | definition, need_for_debug); |
a1ab4c31 AC |
6947 | |
6948 | /* Save the expression in case we try to elaborate this entity again. Since | |
2ddc34ba | 6949 | it's not a DECL, don't check it. Don't save if it's a discriminant. */ |
a1ab4c31 AC |
6950 | if (!CONTAINS_PLACEHOLDER_P (gnu_expr)) |
6951 | save_gnu_tree (gnat_expr, gnu_expr, true); | |
6952 | ||
6953 | return need_value ? gnu_expr : error_mark_node; | |
6954 | } | |
6955 | ||
a531043b | 6956 | /* Similar, but take a GNU expression and always return a result. */ |
a1ab4c31 AC |
6957 | |
6958 | static tree | |
bf44701f | 6959 | elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s, |
3553d8c2 | 6960 | bool definition, bool need_for_debug) |
a1ab4c31 | 6961 | { |
1586f8a3 EB |
6962 | const bool expr_public_p = Is_Public (gnat_entity); |
6963 | const bool expr_global_p = expr_public_p || global_bindings_p (); | |
646f9414 | 6964 | bool expr_variable_p, use_variable; |
a1ab4c31 | 6965 | |
f230d759 EB |
6966 | /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact |
6967 | that an expression cannot contain both a discriminant and a variable. */ | |
6968 | if (CONTAINS_PLACEHOLDER_P (gnu_expr)) | |
6969 | return gnu_expr; | |
6970 | ||
6971 | /* If GNU_EXPR is neither a constant nor based on a read-only variable, make | |
6972 | a variable that is initialized to contain the expression when the package | |
6973 | containing the definition is elaborated. If this entity is defined at top | |
6974 | level, replace the expression by the variable; otherwise use a SAVE_EXPR | |
6975 | if this is necessary. */ | |
7194767c | 6976 | if (TREE_CONSTANT (gnu_expr)) |
f230d759 EB |
6977 | expr_variable_p = false; |
6978 | else | |
6979 | { | |
966b587e | 6980 | /* Skip any conversions and simple constant arithmetics to see if the |
7194767c | 6981 | expression is based on a read-only variable. */ |
966b587e EB |
6982 | tree inner = remove_conversions (gnu_expr, true); |
6983 | ||
6984 | inner = skip_simple_constant_arithmetic (inner); | |
f230d759 EB |
6985 | |
6986 | if (handled_component_p (inner)) | |
ea292448 | 6987 | inner = get_inner_constant_reference (inner); |
f230d759 EB |
6988 | |
6989 | expr_variable_p | |
6990 | = !(inner | |
6991 | && TREE_CODE (inner) == VAR_DECL | |
6992 | && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner))); | |
6993 | } | |
a1ab4c31 | 6994 | |
646f9414 EB |
6995 | /* We only need to use the variable if we are in a global context since GCC |
6996 | can do the right thing in the local case. However, when not optimizing, | |
6997 | use it for bounds of loop iteration scheme to avoid code duplication. */ | |
6998 | use_variable = expr_variable_p | |
6999 | && (expr_global_p | |
7000 | || (!optimize | |
f563ce55 | 7001 | && definition |
646f9414 EB |
7002 | && Is_Itype (gnat_entity) |
7003 | && Nkind (Associated_Node_For_Itype (gnat_entity)) | |
7004 | == N_Loop_Parameter_Specification)); | |
7005 | ||
ce36abee EB |
7006 | /* If the GNAT encodings are not used, we don't need a variable for debug |
7007 | info purposes if the expression is a constant or another variable, but | |
3553d8c2 | 7008 | we must be careful because we do not generate debug info for external |
ce36abee | 7009 | variables so DECL_IGNORED_P is not stable across units. */ |
3553d8c2 | 7010 | if (need_for_debug |
58d32c72 | 7011 | && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL |
ce36abee EB |
7012 | && (TREE_CONSTANT (gnu_expr) |
7013 | || (!expr_public_p | |
7014 | && DECL_P (gnu_expr) | |
7015 | && !DECL_IGNORED_P (gnu_expr)))) | |
3553d8c2 | 7016 | need_for_debug = false; |
ce36abee | 7017 | |
646f9414 | 7018 | /* Now create it, possibly only for debugging purposes. */ |
3553d8c2 | 7019 | if (use_variable || need_for_debug) |
bf7eefab | 7020 | { |
bf44701f | 7021 | /* The following variable creation can happen when processing the body |
3553d8c2 | 7022 | of subprograms that are defined outside of the extended main unit and |
bf44701f | 7023 | inlined. In this case, we are not at the global scope, and thus the |
9a30c7c4 | 7024 | new variable must not be tagged "external", as we used to do here as |
3553d8c2 EB |
7025 | soon as DEFINITION was false. And note that we test Needs_Debug_Info |
7026 | here instead of NEED_FOR_DEBUG because, once the variable is created, | |
7027 | whether or not debug information is generated for it is orthogonal to | |
7028 | the reason why it was created in the first place. */ | |
bf7eefab | 7029 | tree gnu_decl |
c1a569ef EB |
7030 | = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE, |
7031 | TREE_TYPE (gnu_expr), gnu_expr, true, | |
7032 | expr_public_p, !definition && expr_global_p, | |
3553d8c2 EB |
7033 | expr_global_p, false, true, |
7034 | Needs_Debug_Info (gnat_entity), | |
7035 | NULL, gnat_entity, false); | |
9a30c7c4 | 7036 | |
3553d8c2 EB |
7037 | /* Using this variable for debug (if need_for_debug is true) requires |
7038 | a proper location. The back-end will compute a location for this | |
9a30c7c4 AC |
7039 | variable only if the variable is used by the generated code. |
7040 | Returning the variable ensures the caller will use it in generated | |
7041 | code. Note that there is no need for a location if the debug info | |
ce36abee | 7042 | contains an integer constant. */ |
3553d8c2 | 7043 | if (use_variable || (need_for_debug && !TREE_CONSTANT (gnu_expr))) |
bf7eefab EB |
7044 | return gnu_decl; |
7045 | } | |
a531043b | 7046 | |
f230d759 | 7047 | return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr; |
a1ab4c31 | 7048 | } |
da01bfee EB |
7049 | |
7050 | /* Similar, but take an alignment factor and make it explicit in the tree. */ | |
7051 | ||
7052 | static tree | |
bf44701f | 7053 | elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s, |
3553d8c2 | 7054 | bool definition, bool need_for_debug, unsigned int align) |
da01bfee EB |
7055 | { |
7056 | tree unit_align = size_int (align / BITS_PER_UNIT); | |
7057 | return | |
7058 | size_binop (MULT_EXPR, | |
7059 | elaborate_expression_1 (size_binop (EXACT_DIV_EXPR, | |
7060 | gnu_expr, | |
7061 | unit_align), | |
bf44701f | 7062 | gnat_entity, s, definition, |
3553d8c2 | 7063 | need_for_debug), |
da01bfee EB |
7064 | unit_align); |
7065 | } | |
241125b2 EB |
7066 | |
7067 | /* Structure to hold internal data for elaborate_reference. */ | |
7068 | ||
7069 | struct er_data | |
7070 | { | |
7071 | Entity_Id entity; | |
7072 | bool definition; | |
fc7a823e | 7073 | unsigned int n; |
241125b2 EB |
7074 | }; |
7075 | ||
7076 | /* Wrapper function around elaborate_expression_1 for elaborate_reference. */ | |
7077 | ||
7078 | static tree | |
fc7a823e | 7079 | elaborate_reference_1 (tree ref, void *data) |
241125b2 EB |
7080 | { |
7081 | struct er_data *er = (struct er_data *)data; | |
7082 | char suffix[16]; | |
7083 | ||
7084 | /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */ | |
7085 | if (TREE_CONSTANT (ref)) | |
7086 | return ref; | |
7087 | ||
7088 | /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat | |
7089 | pointer. This may be more efficient, but will also allow us to more | |
7090 | easily find the match for the PLACEHOLDER_EXPR. */ | |
7091 | if (TREE_CODE (ref) == COMPONENT_REF | |
7092 | && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0)))) | |
7093 | return build3 (COMPONENT_REF, TREE_TYPE (ref), | |
fc7a823e | 7094 | elaborate_reference_1 (TREE_OPERAND (ref, 0), data), |
552cc590 | 7095 | TREE_OPERAND (ref, 1), NULL_TREE); |
241125b2 | 7096 | |
b67e2ad8 EB |
7097 | /* If this is the displacement of a pointer, elaborate the pointer and then |
7098 | displace the result. The actual purpose here is to drop the location on | |
7099 | the expression, which may be problematic if replicated on references. */ | |
7100 | if (TREE_CODE (ref) == POINTER_PLUS_EXPR | |
7101 | && TREE_CODE (TREE_OPERAND (ref, 1)) == INTEGER_CST) | |
7102 | return build2 (POINTER_PLUS_EXPR, TREE_TYPE (ref), | |
7103 | elaborate_reference_1 (TREE_OPERAND (ref, 0), data), | |
7104 | TREE_OPERAND (ref, 1)); | |
7105 | ||
fc7a823e | 7106 | sprintf (suffix, "EXP%d", ++er->n); |
241125b2 EB |
7107 | return |
7108 | elaborate_expression_1 (ref, er->entity, suffix, er->definition, false); | |
7109 | } | |
7110 | ||
7111 | /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY. | |
fc7a823e EB |
7112 | DEFINITION is true if this is done for a definition of GNAT_ENTITY and |
7113 | INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */ | |
241125b2 EB |
7114 | |
7115 | static tree | |
fc7a823e EB |
7116 | elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition, |
7117 | tree *init) | |
241125b2 | 7118 | { |
fc7a823e EB |
7119 | struct er_data er = { gnat_entity, definition, 0 }; |
7120 | return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init); | |
241125b2 | 7121 | } |
ce2d0ce2 | 7122 | |
a1ab4c31 AC |
7123 | /* Given a GNU tree and a GNAT list of choices, generate an expression to test |
7124 | the value passed against the list of choices. */ | |
7125 | ||
08ef2c16 | 7126 | static tree |
8e93ce66 | 7127 | choices_to_gnu (tree gnu_operand, Node_Id gnat_choices) |
a1ab4c31 | 7128 | { |
8e93ce66 EB |
7129 | tree gnu_result = boolean_false_node, gnu_type; |
7130 | ||
7131 | gnu_operand = maybe_character_value (gnu_operand); | |
7132 | gnu_type = TREE_TYPE (gnu_operand); | |
a1ab4c31 | 7133 | |
8e93ce66 EB |
7134 | for (Node_Id gnat_choice = First (gnat_choices); |
7135 | Present (gnat_choice); | |
7136 | gnat_choice = Next (gnat_choice)) | |
a1ab4c31 | 7137 | { |
8e93ce66 EB |
7138 | tree gnu_low = NULL_TREE, gnu_high = NULL_TREE; |
7139 | tree gnu_test; | |
7140 | ||
7141 | switch (Nkind (gnat_choice)) | |
a1ab4c31 AC |
7142 | { |
7143 | case N_Range: | |
8e93ce66 EB |
7144 | gnu_low = gnat_to_gnu (Low_Bound (gnat_choice)); |
7145 | gnu_high = gnat_to_gnu (High_Bound (gnat_choice)); | |
a1ab4c31 AC |
7146 | break; |
7147 | ||
7148 | case N_Subtype_Indication: | |
8e93ce66 EB |
7149 | gnu_low = gnat_to_gnu (Low_Bound (Range_Expression |
7150 | (Constraint (gnat_choice)))); | |
7151 | gnu_high = gnat_to_gnu (High_Bound (Range_Expression | |
7152 | (Constraint (gnat_choice)))); | |
a1ab4c31 AC |
7153 | break; |
7154 | ||
7155 | case N_Identifier: | |
7156 | case N_Expanded_Name: | |
8e93ce66 EB |
7157 | /* This represents either a subtype range or a static value of |
7158 | some kind; Ekind says which. */ | |
7159 | if (Is_Type (Entity (gnat_choice))) | |
a1ab4c31 | 7160 | { |
8e93ce66 EB |
7161 | tree gnu_type = get_unpadded_type (Entity (gnat_choice)); |
7162 | ||
7163 | gnu_low = TYPE_MIN_VALUE (gnu_type); | |
7164 | gnu_high = TYPE_MAX_VALUE (gnu_type); | |
a1ab4c31 AC |
7165 | break; |
7166 | } | |
2ddc34ba | 7167 | |
9c453de7 | 7168 | /* ... fall through ... */ |
2ddc34ba | 7169 | |
a1ab4c31 AC |
7170 | case N_Character_Literal: |
7171 | case N_Integer_Literal: | |
8e93ce66 | 7172 | gnu_low = gnat_to_gnu (gnat_choice); |
a1ab4c31 AC |
7173 | break; |
7174 | ||
7175 | case N_Others_Choice: | |
a1ab4c31 AC |
7176 | break; |
7177 | ||
7178 | default: | |
7179 | gcc_unreachable (); | |
7180 | } | |
7181 | ||
8e93ce66 EB |
7182 | /* Everything should be folded into constants at this point. */ |
7183 | gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST); | |
7184 | gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST); | |
7185 | ||
7186 | if (gnu_low && TREE_TYPE (gnu_low) != gnu_type) | |
7187 | gnu_low = convert (gnu_type, gnu_low); | |
7188 | if (gnu_high && TREE_TYPE (gnu_high) != gnu_type) | |
7189 | gnu_high = convert (gnu_type, gnu_high); | |
7190 | ||
7191 | if (gnu_low && gnu_high) | |
7192 | gnu_test | |
7193 | = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node, | |
7194 | build_binary_op (GE_EXPR, boolean_type_node, | |
7195 | gnu_operand, gnu_low, true), | |
7196 | build_binary_op (LE_EXPR, boolean_type_node, | |
7197 | gnu_operand, gnu_high, true), | |
7198 | true); | |
fcdc7fd5 EB |
7199 | else if (gnu_low == boolean_true_node |
7200 | && TREE_TYPE (gnu_operand) == boolean_type_node) | |
7201 | gnu_test = gnu_operand; | |
8e93ce66 EB |
7202 | else if (gnu_low) |
7203 | gnu_test | |
7204 | = build_binary_op (EQ_EXPR, boolean_type_node, gnu_operand, gnu_low, | |
7205 | true); | |
7206 | else | |
7207 | gnu_test = boolean_true_node; | |
7208 | ||
7209 | if (gnu_result == boolean_false_node) | |
7210 | gnu_result = gnu_test; | |
08ef2c16 | 7211 | else |
8e93ce66 EB |
7212 | gnu_result |
7213 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_result, | |
7214 | gnu_test, true); | |
a1ab4c31 AC |
7215 | } |
7216 | ||
8e93ce66 | 7217 | return gnu_result; |
a1ab4c31 | 7218 | } |
ce2d0ce2 | 7219 | |
a1ab4c31 AC |
7220 | /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of |
7221 | type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */ | |
7222 | ||
7223 | static int | |
7224 | adjust_packed (tree field_type, tree record_type, int packed) | |
7225 | { | |
0c2837b5 EB |
7226 | /* If the field contains an array with self-referential size, we'd better |
7227 | not pack it because this would misalign it and, therefore, cause large | |
7228 | temporaries to be created in case we need to take the address of the | |
7229 | field. See addressable_p and the notes on the addressability issues | |
7230 | for further details. */ | |
7231 | if (AGGREGATE_TYPE_P (field_type) | |
7232 | && aggregate_type_contains_array_p (field_type, true)) | |
a1ab4c31 AC |
7233 | return 0; |
7234 | ||
14ecca2e EB |
7235 | /* In the other cases, we can honor the packing. */ |
7236 | if (packed) | |
7237 | return packed; | |
7238 | ||
a1ab4c31 AC |
7239 | /* If the alignment of the record is specified and the field type |
7240 | is over-aligned, request Storage_Unit alignment for the field. */ | |
14ecca2e EB |
7241 | if (TYPE_ALIGN (record_type) |
7242 | && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type)) | |
7243 | return -1; | |
7244 | ||
7245 | /* Likewise if the maximum alignment of the record is specified. */ | |
7246 | if (TYPE_MAX_ALIGN (record_type) | |
7247 | && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type)) | |
7248 | return -1; | |
a1ab4c31 | 7249 | |
14ecca2e | 7250 | return 0; |
a1ab4c31 AC |
7251 | } |
7252 | ||
7253 | /* Return a GCC tree for a field corresponding to GNAT_FIELD to be | |
7254 | placed in GNU_RECORD_TYPE. | |
7255 | ||
14ecca2e EB |
7256 | PACKED is 1 if the enclosing record is packed or -1 if the enclosing |
7257 | record has Component_Alignment of Storage_Unit. | |
a1ab4c31 | 7258 | |
839f2864 EB |
7259 | DEFINITION is true if this field is for a record being defined. |
7260 | ||
7261 | DEBUG_INFO_P is true if we need to write debug information for types | |
7262 | that we may create in the process. */ | |
a1ab4c31 AC |
7263 | |
7264 | static tree | |
7265 | gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed, | |
839f2864 | 7266 | bool definition, bool debug_info_p) |
a1ab4c31 | 7267 | { |
f2bee239 | 7268 | const Node_Id gnat_clause = Component_Clause (gnat_field); |
741bd9b1 | 7269 | const Entity_Id gnat_record_type = Underlying_Type (Scope (gnat_field)); |
c020c92b | 7270 | const Entity_Id gnat_field_type = Etype (gnat_field); |
a517d6c1 EB |
7271 | tree gnu_field_type = gnat_to_gnu_type (gnat_field_type); |
7272 | tree gnu_field_id = get_entity_name (gnat_field); | |
4c24ec6d | 7273 | const bool is_aliased = Is_Aliased (gnat_field); |
b120ca61 EB |
7274 | const bool is_full_access |
7275 | = (Is_Full_Access (gnat_field) || Is_Full_Access (gnat_field_type)); | |
07aff4e3 AC |
7276 | const bool is_independent |
7277 | = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type)); | |
7278 | const bool is_volatile | |
c020c92b | 7279 | = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type)); |
a517d6c1 | 7280 | const bool is_by_ref = TYPE_IS_BY_REFERENCE_P (gnu_field_type); |
4c24ec6d EB |
7281 | const bool is_strict_alignment = Strict_Alignment (gnat_field_type); |
7282 | /* We used to consider that volatile fields also require strict alignment, | |
7283 | but that was an interpolation and would cause us to reject a pragma | |
7284 | volatile on a packed record type containing boolean components, while | |
7285 | there is no basis to do so in the RM. In such cases, the writes will | |
7286 | involve load-modify-store sequences, but that's OK for volatile. The | |
7287 | only constraint is the implementation advice whereby only the bits of | |
7288 | the components should be accessed if they both start and end on byte | |
a517d6c1 | 7289 | boundaries, but that should be guaranteed by the GCC memory model. |
b120ca61 | 7290 | Note that we have some redundancies (is_full_access => is_independent, |
a517d6c1 EB |
7291 | is_aliased => is_independent and is_by_ref => is_strict_alignment) |
7292 | so the following formula is sufficient. */ | |
7293 | const bool needs_strict_alignment = (is_independent || is_strict_alignment); | |
7294 | const char *field_s, *size_s; | |
07aff4e3 | 7295 | tree gnu_field, gnu_size, gnu_pos; |
a517d6c1 EB |
7296 | bool is_bitfield; |
7297 | ||
17ba0ad5 EB |
7298 | /* Force the type of the Not_Handled_By_Others field to be that of the |
7299 | field in struct Exception_Data declared in raise.h instead of using | |
7300 | the declared boolean type. We need to do that because there is no | |
7301 | easy way to make use of a C compatible boolean type for the latter. */ | |
7302 | if (gnu_field_id == not_handled_by_others_name_id | |
7303 | && gnu_field_type == boolean_type_node) | |
7304 | gnu_field_type = char_type_node; | |
7305 | ||
a517d6c1 | 7306 | /* The qualifier to be used in messages. */ |
b120ca61 | 7307 | if (is_aliased) |
a517d6c1 | 7308 | field_s = "aliased&"; |
b120ca61 EB |
7309 | else if (is_full_access) |
7310 | { | |
7311 | if (Is_Volatile_Full_Access (gnat_field) | |
7312 | || Is_Volatile_Full_Access (gnat_field_type)) | |
7313 | field_s = "volatile full access&"; | |
7314 | else | |
7315 | field_s = "atomic&"; | |
7316 | } | |
a517d6c1 EB |
7317 | else if (is_independent) |
7318 | field_s = "independent&"; | |
7319 | else if (is_by_ref) | |
7320 | field_s = "& with by-reference type"; | |
7321 | else if (is_strict_alignment) | |
7322 | field_s = "& with aliased part"; | |
7323 | else | |
7324 | field_s = "&"; | |
7325 | ||
7326 | /* The message to be used for incompatible size. */ | |
b120ca61 | 7327 | if (is_aliased || is_full_access) |
a517d6c1 EB |
7328 | size_s = "size for %s must be ^"; |
7329 | else if (field_s) | |
7330 | size_s = "size for %s too small{, minimum allowed is ^}"; | |
a1ab4c31 | 7331 | |
a517d6c1 | 7332 | /* If a field requires strict alignment, we cannot pack it (RM 13.2(7)). */ |
a1ab4c31 AC |
7333 | if (needs_strict_alignment) |
7334 | packed = 0; | |
7335 | else | |
7336 | packed = adjust_packed (gnu_field_type, gnu_record_type, packed); | |
7337 | ||
7338 | /* If a size is specified, use it. Otherwise, if the record type is packed, | |
7339 | use the official RM size. See "Handling of Type'Size Values" in Einfo | |
7340 | for further details. */ | |
b1af4cb2 | 7341 | if (Present (gnat_clause) || Known_Esize (gnat_field)) |
f2bee239 | 7342 | gnu_size = validate_size (Esize (gnat_field), gnu_field_type, gnat_field, |
a517d6c1 | 7343 | FIELD_DECL, false, true, size_s, field_s); |
a1ab4c31 | 7344 | else if (packed == 1) |
f2bee239 EB |
7345 | { |
7346 | gnu_size = rm_size (gnu_field_type); | |
7347 | if (TREE_CODE (gnu_size) != INTEGER_CST) | |
7348 | gnu_size = NULL_TREE; | |
7349 | } | |
a1ab4c31 AC |
7350 | else |
7351 | gnu_size = NULL_TREE; | |
7352 | ||
b1af4cb2 EB |
7353 | /* Likewise for the position. */ |
7354 | if (Present (gnat_clause)) | |
7355 | { | |
7356 | gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype); | |
7357 | is_bitfield = !value_factor_p (gnu_pos, BITS_PER_UNIT); | |
7358 | } | |
7359 | ||
7360 | /* If the record has rep clauses and this is the tag field, make a rep | |
7361 | clause for it as well. */ | |
7362 | else if (Has_Specified_Layout (gnat_record_type) | |
7363 | && Chars (gnat_field) == Name_uTag) | |
7364 | { | |
7365 | gnu_pos = bitsize_zero_node; | |
7366 | gnu_size = TYPE_SIZE (gnu_field_type); | |
7367 | is_bitfield = false; | |
7368 | } | |
7369 | ||
7370 | else | |
7371 | { | |
7372 | gnu_pos = NULL_TREE; | |
7373 | is_bitfield = false; | |
7374 | } | |
7375 | ||
7376 | /* If the field's type is a fixed-size record that does not require strict | |
7377 | alignment, and the record is packed or we have a position specified for | |
7378 | the field that makes it a bitfield or we have a specified size that is | |
7379 | smaller than that of the field's type, then see if we can get either an | |
7380 | integral mode form of the field's type or a smaller form. If we can, | |
7381 | consider that a size was specified for the field if there wasn't one | |
7382 | already, so we know to make it a bitfield and avoid making things wider. | |
a1ab4c31 | 7383 | |
d770e88d EB |
7384 | Changing to an integral mode form is useful when the record is packed as |
7385 | we can then place the field at a non-byte-aligned position and so achieve | |
7386 | tighter packing. This is in addition required if the field shares a byte | |
7387 | with another field and the front-end lets the back-end handle the access | |
7388 | to the field, because GCC cannot handle non-byte-aligned BLKmode fields. | |
a1ab4c31 | 7389 | |
d770e88d EB |
7390 | Changing to a smaller form is required if the specified size is smaller |
7391 | than that of the field's type and the type contains sub-fields that are | |
7392 | padded, in order to avoid generating accesses to these sub-fields that | |
7393 | are wider than the field. | |
a1ab4c31 AC |
7394 | |
7395 | We avoid the transformation if it is not required or potentially useful, | |
7396 | as it might entail an increase of the field's alignment and have ripple | |
7397 | effects on the outer record type. A typical case is a field known to be | |
d770e88d EB |
7398 | byte-aligned and not to share a byte with another field. */ |
7399 | if (!needs_strict_alignment | |
e1e5852c | 7400 | && RECORD_OR_UNION_TYPE_P (gnu_field_type) |
315cff15 | 7401 | && !TYPE_FAT_POINTER_P (gnu_field_type) |
cc269bb6 | 7402 | && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)) |
a1ab4c31 | 7403 | && (packed == 1 |
b1af4cb2 | 7404 | || is_bitfield |
a1ab4c31 | 7405 | || (gnu_size |
b1af4cb2 | 7406 | && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))))) |
a1ab4c31 | 7407 | { |
b1af4cb2 EB |
7408 | tree gnu_packable_type |
7409 | = make_packable_type (gnu_field_type, true, is_bitfield ? 1 : 0); | |
d770e88d | 7410 | if (gnu_packable_type != gnu_field_type) |
a1ab4c31 AC |
7411 | { |
7412 | gnu_field_type = gnu_packable_type; | |
a1ab4c31 AC |
7413 | if (!gnu_size) |
7414 | gnu_size = rm_size (gnu_field_type); | |
7415 | } | |
7416 | } | |
7417 | ||
b1af4cb2 | 7418 | /* Now check if the type of the field allows atomic access. */ |
b120ca61 | 7419 | if (Is_Full_Access (gnat_field)) |
89ec98ed EB |
7420 | { |
7421 | const unsigned int align | |
5ea133c6 | 7422 | = promote_object_alignment (gnu_field_type, NULL_TREE, gnat_field); |
89ec98ed EB |
7423 | if (align > 0) |
7424 | gnu_field_type | |
7425 | = maybe_pad_type (gnu_field_type, NULL_TREE, align, gnat_field, | |
1e3cabd4 | 7426 | false, definition, true); |
89ec98ed EB |
7427 | check_ok_for_atomic_type (gnu_field_type, gnat_field, false); |
7428 | } | |
a1ab4c31 | 7429 | |
b1af4cb2 EB |
7430 | /* If a position is specified, check that it is valid. */ |
7431 | if (gnu_pos) | |
a1ab4c31 | 7432 | { |
741bd9b1 | 7433 | Entity_Id gnat_parent = Parent_Subtype (gnat_record_type); |
ec88784d | 7434 | |
6153cfd7 EB |
7435 | /* Ensure the position doesn't overlap with the parent subtype if there |
7436 | is one. It would be impossible to build CONSTRUCTORs and accessing | |
7437 | the parent could clobber the component in the extension if directly | |
7438 | done. We accept it with -gnatd.K for the sake of compatibility. */ | |
7439 | if (Present (gnat_parent) | |
7440 | && !(Debug_Flag_Dot_KK && Is_Fully_Repped_Tagged_Type (gnat_parent))) | |
a1ab4c31 | 7441 | { |
ec88784d | 7442 | tree gnu_parent = gnat_to_gnu_type (gnat_parent); |
a1ab4c31 AC |
7443 | |
7444 | if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST | |
7445 | && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent))) | |
35786aad | 7446 | post_error_ne_tree |
26cf7899 | 7447 | ("position for& must be beyond parent{, minimum allowed is ^}", |
35786aad | 7448 | Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent)); |
a1ab4c31 AC |
7449 | } |
7450 | ||
35786aad EB |
7451 | /* If this field needs strict alignment, make sure that the record is |
7452 | sufficiently aligned and that the position and size are consistent | |
7453 | with the type. But don't do it if we are just annotating types and | |
bd95368b OH |
7454 | the field's type is tagged, since tagged types aren't fully laid out |
7455 | in this mode. Also, note that atomic implies volatile so the inner | |
7456 | test sequences ordering is significant here. */ | |
b38086f0 EB |
7457 | if (needs_strict_alignment |
7458 | && !(type_annotate_only && Is_Tagged_Type (gnat_field_type))) | |
a1ab4c31 | 7459 | { |
35786aad EB |
7460 | const unsigned int type_align = TYPE_ALIGN (gnu_field_type); |
7461 | ||
9df60a5d EB |
7462 | if (TYPE_ALIGN (gnu_record_type) |
7463 | && TYPE_ALIGN (gnu_record_type) < type_align) | |
fe37c7af | 7464 | SET_TYPE_ALIGN (gnu_record_type, type_align); |
a1ab4c31 | 7465 | |
26cf7899 EB |
7466 | /* If the position is not a multiple of the storage unit, then error |
7467 | out and reset the position. */ | |
35786aad | 7468 | if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos, |
26cf7899 | 7469 | bitsize_unit_node))) |
a1ab4c31 | 7470 | { |
26cf7899 EB |
7471 | char s[128]; |
7472 | snprintf (s, sizeof (s), "position for %s must be " | |
7473 | "multiple of Storage_Unit", field_s); | |
7474 | post_error_ne (s, First_Bit (gnat_clause), gnat_field); | |
7475 | gnu_pos = NULL_TREE; | |
7476 | } | |
bd95368b | 7477 | |
26cf7899 EB |
7478 | /* If the position is not a multiple of the alignment of the type, |
7479 | then error out and reset the position. */ | |
7480 | else if (type_align > BITS_PER_UNIT | |
7481 | && !integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos, | |
7482 | bitsize_int (type_align)))) | |
7483 | { | |
7484 | char s[128]; | |
7485 | snprintf (s, sizeof (s), "position for %s must be multiple of ^", | |
7486 | field_s); | |
35786aad | 7487 | post_error_ne_num (s, First_Bit (gnat_clause), gnat_field, |
26cf7899 EB |
7488 | type_align / BITS_PER_UNIT); |
7489 | post_error_ne_num ("\\because alignment of its type& is ^", | |
7490 | First_Bit (gnat_clause), Etype (gnat_field), | |
7491 | type_align / BITS_PER_UNIT); | |
35786aad | 7492 | gnu_pos = NULL_TREE; |
a1ab4c31 AC |
7493 | } |
7494 | ||
35786aad | 7495 | if (gnu_size) |
a1ab4c31 | 7496 | { |
26cf7899 EB |
7497 | tree type_size = TYPE_SIZE (gnu_field_type); |
7498 | int cmp; | |
a1ab4c31 | 7499 | |
26cf7899 EB |
7500 | /* If the size is not a multiple of the storage unit, then error |
7501 | out and reset the size. */ | |
7502 | if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size, | |
7503 | bitsize_unit_node))) | |
35786aad | 7504 | { |
26cf7899 EB |
7505 | char s[128]; |
7506 | snprintf (s, sizeof (s), "size for %s must be " | |
7507 | "multiple of Storage_Unit", field_s); | |
7508 | post_error_ne (s, Last_Bit (gnat_clause), gnat_field); | |
35786aad EB |
7509 | gnu_size = NULL_TREE; |
7510 | } | |
a1ab4c31 | 7511 | |
26cf7899 EB |
7512 | /* If the size is lower than that of the type, or greater for |
7513 | atomic and aliased, then error out and reset the size. */ | |
7514 | else if ((cmp = tree_int_cst_compare (gnu_size, type_size)) < 0 | |
b120ca61 | 7515 | || (cmp > 0 && (is_aliased || is_full_access))) |
35786aad | 7516 | { |
26cf7899 | 7517 | char s[128]; |
a517d6c1 | 7518 | snprintf (s, sizeof (s), size_s, field_s); |
26cf7899 EB |
7519 | post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field, |
7520 | type_size); | |
35786aad EB |
7521 | gnu_size = NULL_TREE; |
7522 | } | |
a1ab4c31 AC |
7523 | } |
7524 | } | |
a1ab4c31 AC |
7525 | } |
7526 | ||
a1ab4c31 | 7527 | else |
0025cb63 | 7528 | { |
0025cb63 EB |
7529 | /* If we are packing the record and the field is BLKmode, round the |
7530 | size up to a byte boundary. */ | |
7531 | if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size) | |
7532 | gnu_size = round_up (gnu_size, BITS_PER_UNIT); | |
7533 | } | |
a1ab4c31 AC |
7534 | |
7535 | /* We need to make the size the maximum for the type if it is | |
7536 | self-referential and an unconstrained type. In that case, we can't | |
7537 | pack the field since we can't make a copy to align it. */ | |
7538 | if (TREE_CODE (gnu_field_type) == RECORD_TYPE | |
7539 | && !gnu_size | |
7540 | && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type)) | |
c020c92b | 7541 | && !Is_Constrained (Underlying_Type (gnat_field_type))) |
a1ab4c31 AC |
7542 | { |
7543 | gnu_size = max_size (TYPE_SIZE (gnu_field_type), true); | |
7544 | packed = 0; | |
7545 | } | |
7546 | ||
7547 | /* If a size is specified, adjust the field's type to it. */ | |
7548 | if (gnu_size) | |
7549 | { | |
839f2864 EB |
7550 | tree orig_field_type; |
7551 | ||
a1ab4c31 AC |
7552 | /* If the field's type is justified modular, we would need to remove |
7553 | the wrapper to (better) meet the layout requirements. However we | |
7554 | can do so only if the field is not aliased to preserve the unique | |
741bd9b1 EB |
7555 | layout, if it has the same storage order as the enclosing record |
7556 | and if the prescribed size is not greater than that of the packed | |
7557 | array to preserve the justification. */ | |
a1ab4c31 AC |
7558 | if (!needs_strict_alignment |
7559 | && TREE_CODE (gnu_field_type) == RECORD_TYPE | |
7560 | && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type) | |
741bd9b1 EB |
7561 | && TYPE_REVERSE_STORAGE_ORDER (gnu_field_type) |
7562 | == Reverse_Storage_Order (gnat_record_type) | |
a1ab4c31 AC |
7563 | && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type)) |
7564 | <= 0) | |
7565 | gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type)); | |
7566 | ||
afb0fadf EB |
7567 | /* Similarly if the field's type is a misaligned integral type, but |
7568 | there is no restriction on the size as there is no justification. */ | |
7569 | if (!needs_strict_alignment | |
7570 | && TYPE_IS_PADDING_P (gnu_field_type) | |
7571 | && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type)))) | |
7572 | gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type)); | |
7573 | ||
75a582cd | 7574 | orig_field_type = gnu_field_type; |
a1ab4c31 AC |
7575 | gnu_field_type |
7576 | = make_type_from_size (gnu_field_type, gnu_size, | |
7577 | Has_Biased_Representation (gnat_field)); | |
839f2864 | 7578 | |
75a582cd EB |
7579 | /* If the type has been extended, we may need to cap the alignment. */ |
7580 | if (!needs_strict_alignment | |
7581 | && gnu_field_type != orig_field_type | |
7582 | && tree_int_cst_lt (TYPE_SIZE (orig_field_type), gnu_size)) | |
7583 | packed = adjust_packed (gnu_field_type, gnu_record_type, packed); | |
7584 | ||
839f2864 | 7585 | orig_field_type = gnu_field_type; |
a1ab4c31 | 7586 | gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field, |
1e3cabd4 | 7587 | false, definition, true); |
839f2864 EB |
7588 | |
7589 | /* If a padding record was made, declare it now since it will never be | |
7590 | declared otherwise. This is necessary to ensure that its subtrees | |
7591 | are properly marked. */ | |
7592 | if (gnu_field_type != orig_field_type | |
7593 | && !DECL_P (TYPE_NAME (gnu_field_type))) | |
74746d49 EB |
7594 | create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true, |
7595 | debug_info_p, gnat_field); | |
a1ab4c31 AC |
7596 | } |
7597 | ||
7598 | /* Otherwise (or if there was an error), don't specify a position. */ | |
7599 | else | |
7600 | gnu_pos = NULL_TREE; | |
7601 | ||
ee45a32d EB |
7602 | /* If the field's type is a padded type made for a scalar field of a record |
7603 | type with reverse storage order, we need to propagate the reverse storage | |
7604 | order to the padding type since it is the innermost enclosing aggregate | |
7605 | type around the scalar. */ | |
7606 | if (TYPE_IS_PADDING_P (gnu_field_type) | |
7607 | && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type) | |
7608 | && Is_Scalar_Type (gnat_field_type)) | |
7609 | gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type); | |
7610 | ||
a1ab4c31 AC |
7611 | gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE |
7612 | || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type)); | |
7613 | ||
7614 | /* Now create the decl for the field. */ | |
da01bfee EB |
7615 | gnu_field |
7616 | = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type, | |
4c24ec6d | 7617 | gnu_size, gnu_pos, packed, is_aliased); |
a1ab4c31 | 7618 | Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field)); |
4c24ec6d | 7619 | DECL_ALIASED_P (gnu_field) = is_aliased; |
2056c5ed | 7620 | TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile; |
a1ab4c31 | 7621 | |
683ccd05 EB |
7622 | /* If this is a discriminant, then we treat it specially: first, we set its |
7623 | index number for the back-annotation; second, we record whether it cannot | |
7624 | be changed once it has been set for the computation of loop invariants; | |
7625 | third, we make it addressable in order for the optimizer to more easily | |
7626 | see that it cannot be modified by assignments to the other fields of the | |
7627 | record (see create_field_decl for a more detailed explanation), which is | |
7628 | crucial to hoist the offset and size computations of dynamic fields. */ | |
a1ab4c31 | 7629 | if (Ekind (gnat_field) == E_Discriminant) |
64235766 | 7630 | { |
64235766 EB |
7631 | DECL_DISCRIMINANT_NUMBER (gnu_field) |
7632 | = UI_To_gnu (Discriminant_Number (gnat_field), sizetype); | |
683ccd05 EB |
7633 | DECL_INVARIANT_P (gnu_field) |
7634 | = No (Discriminant_Default_Value (gnat_field)); | |
7635 | DECL_NONADDRESSABLE_P (gnu_field) = 0; | |
64235766 | 7636 | } |
a1ab4c31 AC |
7637 | |
7638 | return gnu_field; | |
7639 | } | |
ce2d0ce2 | 7640 | |
29e100b3 EB |
7641 | /* Return true if at least one member of COMPONENT_LIST needs strict |
7642 | alignment. */ | |
7643 | ||
7644 | static bool | |
7645 | components_need_strict_alignment (Node_Id component_list) | |
7646 | { | |
7647 | Node_Id component_decl; | |
7648 | ||
7649 | for (component_decl = First_Non_Pragma (Component_Items (component_list)); | |
7650 | Present (component_decl); | |
7651 | component_decl = Next_Non_Pragma (component_decl)) | |
7652 | { | |
7653 | Entity_Id gnat_field = Defining_Entity (component_decl); | |
7654 | ||
a517d6c1 | 7655 | if (Is_Independent (gnat_field) || Is_Independent (Etype (gnat_field))) |
78df6221 | 7656 | return true; |
29e100b3 EB |
7657 | |
7658 | if (Strict_Alignment (Etype (gnat_field))) | |
78df6221 | 7659 | return true; |
29e100b3 EB |
7660 | } |
7661 | ||
78df6221 | 7662 | return false; |
29e100b3 EB |
7663 | } |
7664 | ||
5f2e59d4 EB |
7665 | /* Return true if FIELD is an artificial field. */ |
7666 | ||
7667 | static bool | |
7668 | field_is_artificial (tree field) | |
7669 | { | |
7670 | /* These fields are generated by the front-end proper. */ | |
7671 | if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_') | |
7672 | return true; | |
7673 | ||
7674 | /* These fields are generated by gigi. */ | |
7675 | if (DECL_INTERNAL_P (field)) | |
7676 | return true; | |
7677 | ||
7678 | return false; | |
7679 | } | |
7680 | ||
5f2e59d4 EB |
7681 | /* Return true if FIELD is a non-artificial field with self-referential |
7682 | size. */ | |
7683 | ||
7684 | static bool | |
7685 | field_has_self_size (tree field) | |
7686 | { | |
7687 | if (field_is_artificial (field)) | |
7688 | return false; | |
7689 | ||
7690 | if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) | |
7691 | return false; | |
7692 | ||
7693 | return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field))); | |
7694 | } | |
7695 | ||
7696 | /* Return true if FIELD is a non-artificial field with variable size. */ | |
7697 | ||
7698 | static bool | |
7699 | field_has_variable_size (tree field) | |
7700 | { | |
7701 | if (field_is_artificial (field)) | |
7702 | return false; | |
7703 | ||
7704 | if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) | |
7705 | return false; | |
7706 | ||
7707 | return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST; | |
7708 | } | |
7709 | ||
a1ab4c31 AC |
7710 | /* qsort comparer for the bit positions of two record components. */ |
7711 | ||
7712 | static int | |
7713 | compare_field_bitpos (const PTR rt1, const PTR rt2) | |
7714 | { | |
7715 | const_tree const field1 = * (const_tree const *) rt1; | |
7716 | const_tree const field2 = * (const_tree const *) rt2; | |
7717 | const int ret | |
7718 | = tree_int_cst_compare (bit_position (field1), bit_position (field2)); | |
7719 | ||
7720 | return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2)); | |
7721 | } | |
7722 | ||
cd8ad459 EB |
7723 | /* Sort the LIST of fields in reverse order of increasing position. */ |
7724 | ||
7725 | static tree | |
7726 | reverse_sort_field_list (tree list) | |
7727 | { | |
7728 | const int len = list_length (list); | |
7729 | tree *field_arr = XALLOCAVEC (tree, len); | |
7730 | ||
7731 | for (int i = 0; list; list = DECL_CHAIN (list), i++) | |
7732 | field_arr[i] = list; | |
7733 | ||
7734 | qsort (field_arr, len, sizeof (tree), compare_field_bitpos); | |
7735 | ||
7736 | for (int i = 0; i < len; i++) | |
7737 | { | |
7738 | DECL_CHAIN (field_arr[i]) = list; | |
7739 | list = field_arr[i]; | |
7740 | } | |
7741 | ||
7742 | return list; | |
7743 | } | |
7744 | ||
8ab31c0c AC |
7745 | /* Reverse function from gnat_to_gnu_field: return the GNAT field present in |
7746 | either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE, and | |
7747 | corresponding to the GNU tree GNU_FIELD. */ | |
7748 | ||
7749 | static Entity_Id | |
7750 | gnu_field_to_gnat (tree gnu_field, Node_Id gnat_component_list, | |
7751 | Entity_Id gnat_record_type) | |
7752 | { | |
7753 | Entity_Id gnat_component_decl, gnat_field; | |
7754 | ||
7755 | if (Present (Component_Items (gnat_component_list))) | |
7756 | for (gnat_component_decl | |
7757 | = First_Non_Pragma (Component_Items (gnat_component_list)); | |
7758 | Present (gnat_component_decl); | |
7759 | gnat_component_decl = Next_Non_Pragma (gnat_component_decl)) | |
7760 | { | |
7761 | gnat_field = Defining_Entity (gnat_component_decl); | |
7762 | if (gnat_to_gnu_field_decl (gnat_field) == gnu_field) | |
7763 | return gnat_field; | |
7764 | } | |
7765 | ||
7766 | if (Has_Discriminants (gnat_record_type)) | |
7767 | for (gnat_field = First_Stored_Discriminant (gnat_record_type); | |
7768 | Present (gnat_field); | |
7769 | gnat_field = Next_Stored_Discriminant (gnat_field)) | |
7770 | if (gnat_to_gnu_field_decl (gnat_field) == gnu_field) | |
7771 | return gnat_field; | |
7772 | ||
7773 | return Empty; | |
7774 | } | |
7775 | ||
7776 | /* Issue a warning for the problematic placement of GNU_FIELD present in | |
7777 | either GNAT_COMPONENT_LIST or the discriminants of GNAT_RECORD_TYPE. | |
7778 | IN_VARIANT is true if GNAT_COMPONENT_LIST is the list of a variant. | |
7779 | DO_REORDER is true if fields of GNAT_RECORD_TYPE are being reordered. */ | |
7780 | ||
7781 | static void | |
7782 | warn_on_field_placement (tree gnu_field, Node_Id gnat_component_list, | |
7783 | Entity_Id gnat_record_type, bool in_variant, | |
7784 | bool do_reorder) | |
7785 | { | |
3f8cf834 EB |
7786 | if (!Comes_From_Source (gnat_record_type)) |
7787 | return; | |
7788 | ||
81034751 EB |
7789 | Entity_Id gnat_field |
7790 | = gnu_field_to_gnat (gnu_field, gnat_component_list, gnat_record_type); | |
7791 | gcc_assert (Present (gnat_field)); | |
7792 | ||
8ab31c0c AC |
7793 | const char *msg1 |
7794 | = in_variant | |
4a29b8d6 GL |
7795 | ? "??variant layout may cause performance issues" |
7796 | : "??record layout may cause performance issues"; | |
8ab31c0c | 7797 | const char *msg2 |
81034751 | 7798 | = Ekind (gnat_field) == E_Discriminant |
4a29b8d6 | 7799 | ? "??discriminant & whose length is not multiple of a byte" |
81034751 | 7800 | : field_has_self_size (gnu_field) |
4a29b8d6 | 7801 | ? "??component & whose length depends on a discriminant" |
81034751 | 7802 | : field_has_variable_size (gnu_field) |
4a29b8d6 GL |
7803 | ? "??component & whose length is not fixed" |
7804 | : "??component & whose length is not multiple of a byte"; | |
8ab31c0c AC |
7805 | const char *msg3 |
7806 | = do_reorder | |
4a29b8d6 GL |
7807 | ? "??comes too early and was moved down" |
7808 | : "??comes too early and ought to be moved down"; | |
3f8cf834 | 7809 | |
8ab31c0c AC |
7810 | post_error (msg1, gnat_field); |
7811 | post_error_ne (msg2, gnat_field, gnat_field); | |
7812 | post_error (msg3, gnat_field); | |
7813 | } | |
7814 | ||
81034751 EB |
7815 | /* Likewise but for every field present on GNU_FIELD_LIST. */ |
7816 | ||
7817 | static void | |
7818 | warn_on_list_placement (tree gnu_field_list, Node_Id gnat_component_list, | |
7819 | Entity_Id gnat_record_type, bool in_variant, | |
7820 | bool do_reorder) | |
7821 | { | |
7822 | for (tree gnu_tmp = gnu_field_list; gnu_tmp; gnu_tmp = DECL_CHAIN (gnu_tmp)) | |
7823 | warn_on_field_placement (gnu_tmp, gnat_component_list, gnat_record_type, | |
7824 | in_variant, do_reorder); | |
7825 | } | |
7826 | ||
9580628d EB |
7827 | /* Structure holding information for a given variant. */ |
7828 | typedef struct vinfo | |
7829 | { | |
7830 | /* The record type of the variant. */ | |
7831 | tree type; | |
7832 | ||
7833 | /* The name of the variant. */ | |
7834 | tree name; | |
7835 | ||
7836 | /* The qualifier of the variant. */ | |
7837 | tree qual; | |
7838 | ||
7839 | /* Whether the variant has a rep clause. */ | |
7840 | bool has_rep; | |
7841 | ||
7842 | /* Whether the variant is packed. */ | |
7843 | bool packed; | |
7844 | ||
7845 | } vinfo_t; | |
7846 | ||
8ab31c0c AC |
7847 | /* Translate and chain GNAT_COMPONENT_LIST present in GNAT_RECORD_TYPE to |
7848 | GNU_FIELD_LIST, set the result as the field list of GNU_RECORD_TYPE and | |
7849 | finish it up. Return true if GNU_RECORD_TYPE has a rep clause that affects | |
7850 | the layout (see below). When called from gnat_to_gnu_entity during the | |
7851 | processing of a record definition, the GCC node for the parent, if any, | |
7852 | will be the single field of GNU_RECORD_TYPE and the GCC nodes for the | |
7853 | discriminants will be on GNU_FIELD_LIST. The other call to this function | |
7854 | is a recursive call for the component list of a variant and, in this case, | |
76f9c7f4 | 7855 | GNU_FIELD_LIST is empty. Note that GNAT_COMPONENT_LIST may be Empty. |
a1ab4c31 | 7856 | |
14ecca2e EB |
7857 | PACKED is 1 if this is for a packed record or -1 if this is for a record |
7858 | with Component_Alignment of Storage_Unit. | |
a1ab4c31 | 7859 | |
032d1b71 | 7860 | DEFINITION is true if we are defining this record type. |
a1ab4c31 | 7861 | |
032d1b71 EB |
7862 | CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying |
7863 | out the record. This means the alignment only serves to force fields to | |
7864 | be bitfields, but not to require the record to be that aligned. This is | |
7865 | used for variants. | |
7866 | ||
7867 | ALL_REP is true if a rep clause is present for all the fields. | |
a1ab4c31 | 7868 | |
032d1b71 EB |
7869 | UNCHECKED_UNION is true if we are building this type for a record with a |
7870 | Pragma Unchecked_Union. | |
a1ab4c31 | 7871 | |
fd787640 EB |
7872 | ARTIFICIAL is true if this is a type that was generated by the compiler. |
7873 | ||
ef0feeb2 | 7874 | DEBUG_INFO is true if we need to write debug information about the type. |
a1ab4c31 | 7875 | |
032d1b71 | 7876 | MAYBE_UNUSED is true if this type may be unused in the end; this doesn't |
ef0feeb2 | 7877 | mean that its contents may be unused as well, only the container itself. |
839f2864 | 7878 | |
b1a785fb EB |
7879 | FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in |
7880 | the outer record type down to this variant level. It is nonzero only if | |
7881 | all the fields down to this level have a rep clause and ALL_REP is false. | |
7882 | ||
ef0feeb2 EB |
7883 | P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field |
7884 | with a rep clause is to be added; in this case, that is all that should | |
9580628d | 7885 | be done with such fields and the return value will be false. */ |
a1ab4c31 | 7886 | |
9580628d | 7887 | static bool |
8ab31c0c AC |
7888 | components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, |
7889 | tree gnu_field_list, tree gnu_record_type, int packed, | |
7890 | bool definition, bool cancel_alignment, bool all_rep, | |
7891 | bool unchecked_union, bool artificial, bool debug_info, | |
7892 | bool maybe_unused, tree first_free_pos, | |
7893 | tree *p_gnu_rep_list) | |
a1ab4c31 | 7894 | { |
986ccd21 | 7895 | const bool needs_xv_encodings |
58d32c72 | 7896 | = debug_info && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL; |
a1ab4c31 | 7897 | bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type); |
9580628d | 7898 | bool variants_have_rep = all_rep; |
8cd28148 | 7899 | bool layout_with_rep = false; |
fdfa0e44 | 7900 | bool has_non_packed_fixed_size_field = false; |
5f2e59d4 EB |
7901 | bool has_self_field = false; |
7902 | bool has_aliased_after_self_field = false; | |
8ab31c0c | 7903 | Entity_Id gnat_component_decl, gnat_variant_part; |
ef0feeb2 EB |
7904 | tree gnu_field, gnu_next, gnu_last; |
7905 | tree gnu_variant_part = NULL_TREE; | |
7906 | tree gnu_rep_list = NULL_TREE; | |
a1ab4c31 | 7907 | |
8cd28148 EB |
7908 | /* For each component referenced in a component declaration create a GCC |
7909 | field and add it to the list, skipping pragmas in the GNAT list. */ | |
ef0feeb2 | 7910 | gnu_last = tree_last (gnu_field_list); |
76f9c7f4 BD |
7911 | if (Present (gnat_component_list) |
7912 | && (Present (Component_Items (gnat_component_list)))) | |
8ab31c0c | 7913 | for (gnat_component_decl |
8cd28148 | 7914 | = First_Non_Pragma (Component_Items (gnat_component_list)); |
8ab31c0c AC |
7915 | Present (gnat_component_decl); |
7916 | gnat_component_decl = Next_Non_Pragma (gnat_component_decl)) | |
a1ab4c31 | 7917 | { |
8ab31c0c | 7918 | Entity_Id gnat_field = Defining_Entity (gnat_component_decl); |
a6a29d0c | 7919 | Name_Id gnat_name = Chars (gnat_field); |
a1ab4c31 | 7920 | |
a6a29d0c EB |
7921 | /* If present, the _Parent field must have been created as the single |
7922 | field of the record type. Put it before any other fields. */ | |
7923 | if (gnat_name == Name_uParent) | |
7924 | { | |
7925 | gnu_field = TYPE_FIELDS (gnu_record_type); | |
7926 | gnu_field_list = chainon (gnu_field_list, gnu_field); | |
7927 | } | |
a1ab4c31 AC |
7928 | else |
7929 | { | |
839f2864 | 7930 | gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed, |
ef0feeb2 | 7931 | definition, debug_info); |
a1ab4c31 | 7932 | |
a6a29d0c EB |
7933 | /* If this is the _Tag field, put it before any other fields. */ |
7934 | if (gnat_name == Name_uTag) | |
a1ab4c31 | 7935 | gnu_field_list = chainon (gnu_field_list, gnu_field); |
a6a29d0c EB |
7936 | |
7937 | /* If this is the _Controller field, put it before the other | |
7938 | fields except for the _Tag or _Parent field. */ | |
7939 | else if (gnat_name == Name_uController && gnu_last) | |
7940 | { | |
910ad8de NF |
7941 | DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last); |
7942 | DECL_CHAIN (gnu_last) = gnu_field; | |
a6a29d0c EB |
7943 | } |
7944 | ||
7945 | /* If this is a regular field, put it after the other fields. */ | |
a1ab4c31 AC |
7946 | else |
7947 | { | |
910ad8de | 7948 | DECL_CHAIN (gnu_field) = gnu_field_list; |
a1ab4c31 | 7949 | gnu_field_list = gnu_field; |
a6a29d0c EB |
7950 | if (!gnu_last) |
7951 | gnu_last = gnu_field; | |
5f2e59d4 EB |
7952 | |
7953 | /* And record information for the final layout. */ | |
7954 | if (field_has_self_size (gnu_field)) | |
7955 | has_self_field = true; | |
05dbb83f | 7956 | else if (has_self_field && DECL_ALIASED_P (gnu_field)) |
5f2e59d4 | 7957 | has_aliased_after_self_field = true; |
fdfa0e44 EB |
7958 | else if (!DECL_FIELD_OFFSET (gnu_field) |
7959 | && !DECL_PACKED (gnu_field) | |
7960 | && !field_has_variable_size (gnu_field)) | |
7961 | has_non_packed_fixed_size_field = true; | |
a1ab4c31 AC |
7962 | } |
7963 | } | |
7964 | ||
2ddc34ba | 7965 | save_gnu_tree (gnat_field, gnu_field, false); |
a1ab4c31 AC |
7966 | } |
7967 | ||
7968 | /* At the end of the component list there may be a variant part. */ | |
76f9c7f4 BD |
7969 | if (Present (gnat_component_list)) |
7970 | gnat_variant_part = Variant_Part (gnat_component_list); | |
7971 | else | |
7972 | gnat_variant_part = Empty; | |
a1ab4c31 AC |
7973 | |
7974 | /* We create a QUAL_UNION_TYPE for the variant part since the variants are | |
7975 | mutually exclusive and should go in the same memory. To do this we need | |
7976 | to treat each variant as a record whose elements are created from the | |
7977 | component list for the variant. So here we create the records from the | |
7978 | lists for the variants and put them all into the QUAL_UNION_TYPE. | |
7979 | If this is an Unchecked_Union, we make a UNION_TYPE instead or | |
7980 | use GNU_RECORD_TYPE if there are no fields so far. */ | |
8ab31c0c | 7981 | if (Present (gnat_variant_part)) |
a1ab4c31 | 7982 | { |
8ab31c0c | 7983 | Node_Id gnat_discr = Name (gnat_variant_part), variant; |
0fb2335d | 7984 | tree gnu_discr = gnat_to_gnu (gnat_discr); |
9dba4b55 | 7985 | tree gnu_name = TYPE_IDENTIFIER (gnu_record_type); |
a1ab4c31 | 7986 | tree gnu_var_name |
0fb2335d EB |
7987 | = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))), |
7988 | "XVN"); | |
f2bee239 EB |
7989 | tree gnu_union_name |
7990 | = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name)); | |
7991 | tree gnu_union_type; | |
b1a785fb | 7992 | tree this_first_free_pos, gnu_variant_list = NULL_TREE; |
29e100b3 | 7993 | bool union_field_needs_strict_alignment = false; |
00f96dc9 | 7994 | auto_vec <vinfo_t, 16> variant_types; |
9580628d EB |
7995 | vinfo_t *gnu_variant; |
7996 | unsigned int variants_align = 0; | |
7997 | unsigned int i; | |
7998 | ||
b1a785fb EB |
7999 | /* Reuse the enclosing union if this is an Unchecked_Union whose fields |
8000 | are all in the variant part, to match the layout of C unions. There | |
8001 | is an associated check below. */ | |
8002 | if (TREE_CODE (gnu_record_type) == UNION_TYPE) | |
a1ab4c31 AC |
8003 | gnu_union_type = gnu_record_type; |
8004 | else | |
8005 | { | |
8006 | gnu_union_type | |
8007 | = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE); | |
8008 | ||
8009 | TYPE_NAME (gnu_union_type) = gnu_union_name; | |
fe37c7af | 8010 | SET_TYPE_ALIGN (gnu_union_type, 0); |
a1ab4c31 | 8011 | TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type); |
ee45a32d EB |
8012 | TYPE_REVERSE_STORAGE_ORDER (gnu_union_type) |
8013 | = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type); | |
a1ab4c31 AC |
8014 | } |
8015 | ||
b1a785fb EB |
8016 | /* If all the fields down to this level have a rep clause, find out |
8017 | whether all the fields at this level also have one. If so, then | |
8018 | compute the new first free position to be passed downward. */ | |
8019 | this_first_free_pos = first_free_pos; | |
8020 | if (this_first_free_pos) | |
8021 | { | |
8022 | for (gnu_field = gnu_field_list; | |
8023 | gnu_field; | |
8024 | gnu_field = DECL_CHAIN (gnu_field)) | |
8025 | if (DECL_FIELD_OFFSET (gnu_field)) | |
8026 | { | |
8027 | tree pos = bit_position (gnu_field); | |
8028 | if (!tree_int_cst_lt (pos, this_first_free_pos)) | |
8029 | this_first_free_pos | |
8030 | = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field)); | |
8031 | } | |
8032 | else | |
8033 | { | |
8034 | this_first_free_pos = NULL_TREE; | |
8035 | break; | |
8036 | } | |
8037 | } | |
8038 | ||
9580628d EB |
8039 | /* We build the variants in two passes. The bulk of the work is done in |
8040 | the first pass, that is to say translating the GNAT nodes, building | |
8041 | the container types and computing the associated properties. However | |
8042 | we cannot finish up the container types during this pass because we | |
8043 | don't know where the variant part will be placed until the end. */ | |
8ab31c0c | 8044 | for (variant = First_Non_Pragma (Variants (gnat_variant_part)); |
a1ab4c31 AC |
8045 | Present (variant); |
8046 | variant = Next_Non_Pragma (variant)) | |
8047 | { | |
8048 | tree gnu_variant_type = make_node (RECORD_TYPE); | |
9580628d EB |
8049 | tree gnu_inner_name, gnu_qual; |
8050 | bool has_rep; | |
8051 | int field_packed; | |
8052 | vinfo_t vinfo; | |
a1ab4c31 AC |
8053 | |
8054 | Get_Variant_Encoding (variant); | |
0fb2335d | 8055 | gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len); |
a1ab4c31 | 8056 | TYPE_NAME (gnu_variant_type) |
0fb2335d EB |
8057 | = concat_name (gnu_union_name, |
8058 | IDENTIFIER_POINTER (gnu_inner_name)); | |
a1ab4c31 AC |
8059 | |
8060 | /* Set the alignment of the inner type in case we need to make | |
8cd28148 EB |
8061 | inner objects into bitfields, but then clear it out so the |
8062 | record actually gets only the alignment required. */ | |
fe37c7af | 8063 | SET_TYPE_ALIGN (gnu_variant_type, TYPE_ALIGN (gnu_record_type)); |
a1ab4c31 | 8064 | TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type); |
ee45a32d EB |
8065 | TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type) |
8066 | = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type); | |
a1ab4c31 | 8067 | |
8cd28148 | 8068 | /* Similarly, if the outer record has a size specified and all |
b1a785fb | 8069 | the fields have a rep clause, we can propagate the size. */ |
a1ab4c31 AC |
8070 | if (all_rep_and_size) |
8071 | { | |
8072 | TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type); | |
8073 | TYPE_SIZE_UNIT (gnu_variant_type) | |
8074 | = TYPE_SIZE_UNIT (gnu_record_type); | |
8075 | } | |
8076 | ||
032d1b71 EB |
8077 | /* Add the fields into the record type for the variant. Note that |
8078 | we aren't sure to really use it at this point, see below. */ | |
9580628d | 8079 | has_rep |
8ab31c0c AC |
8080 | = components_to_record (Component_List (variant), gnat_record_type, |
8081 | NULL_TREE, gnu_variant_type, packed, | |
8082 | definition, !all_rep_and_size, all_rep, | |
8083 | unchecked_union, true, needs_xv_encodings, | |
8084 | true, this_first_free_pos, | |
9580628d EB |
8085 | all_rep || this_first_free_pos |
8086 | ? NULL : &gnu_rep_list); | |
8087 | ||
8088 | /* Translate the qualifier and annotate the GNAT node. */ | |
0fb2335d | 8089 | gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant)); |
a1ab4c31 AC |
8090 | Set_Present_Expr (variant, annotate_value (gnu_qual)); |
8091 | ||
9580628d EB |
8092 | /* Deal with packedness like in gnat_to_gnu_field. */ |
8093 | if (components_need_strict_alignment (Component_List (variant))) | |
8094 | { | |
8095 | field_packed = 0; | |
8096 | union_field_needs_strict_alignment = true; | |
8097 | } | |
8098 | else | |
8099 | field_packed | |
8100 | = adjust_packed (gnu_variant_type, gnu_record_type, packed); | |
8101 | ||
8102 | /* Push this variant onto the stack for the second pass. */ | |
8103 | vinfo.type = gnu_variant_type; | |
8104 | vinfo.name = gnu_inner_name; | |
8105 | vinfo.qual = gnu_qual; | |
8106 | vinfo.has_rep = has_rep; | |
8107 | vinfo.packed = field_packed; | |
8108 | variant_types.safe_push (vinfo); | |
8109 | ||
8110 | /* Compute the global properties that will determine the placement of | |
8111 | the variant part. */ | |
8112 | variants_have_rep |= has_rep; | |
8113 | if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align) | |
8114 | variants_align = TYPE_ALIGN (gnu_variant_type); | |
8115 | } | |
8116 | ||
8117 | /* Round up the first free position to the alignment of the variant part | |
8118 | for the variants without rep clause. This will guarantee a consistent | |
8119 | layout independently of the placement of the variant part. */ | |
8120 | if (variants_have_rep && variants_align > 0 && this_first_free_pos) | |
8121 | this_first_free_pos = round_up (this_first_free_pos, variants_align); | |
8122 | ||
8123 | /* In the second pass, the container types are adjusted if necessary and | |
8124 | finished up, then the corresponding fields of the variant part are | |
8125 | built with their qualifier, unless this is an unchecked union. */ | |
8126 | FOR_EACH_VEC_ELT (variant_types, i, gnu_variant) | |
8127 | { | |
8128 | tree gnu_variant_type = gnu_variant->type; | |
8129 | tree gnu_field_list = TYPE_FIELDS (gnu_variant_type); | |
8130 | ||
b1a785fb EB |
8131 | /* If this is an Unchecked_Union whose fields are all in the variant |
8132 | part and we have a single field with no representation clause or | |
8133 | placed at offset zero, use the field directly to match the layout | |
8134 | of C unions. */ | |
8135 | if (TREE_CODE (gnu_record_type) == UNION_TYPE | |
9580628d EB |
8136 | && gnu_field_list |
8137 | && !DECL_CHAIN (gnu_field_list) | |
8138 | && (!DECL_FIELD_OFFSET (gnu_field_list) | |
8139 | || integer_zerop (bit_position (gnu_field_list)))) | |
8140 | { | |
8141 | gnu_field = gnu_field_list; | |
8142 | DECL_CONTEXT (gnu_field) = gnu_record_type; | |
8143 | } | |
a1ab4c31 AC |
8144 | else |
8145 | { | |
9580628d EB |
8146 | /* Finalize the variant type now. We used to throw away empty |
8147 | record types but we no longer do that because we need them to | |
8148 | generate complete debug info for the variant; otherwise, the | |
8149 | union type definition will be lacking the fields associated | |
8150 | with these empty variants. */ | |
8151 | if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep) | |
29e100b3 | 8152 | { |
9580628d EB |
8153 | /* The variant part will be at offset 0 so we need to ensure |
8154 | that the fields are laid out starting from the first free | |
8155 | position at this level. */ | |
8156 | tree gnu_rep_type = make_node (RECORD_TYPE); | |
8157 | tree gnu_rep_part; | |
ee45a32d EB |
8158 | TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type) |
8159 | = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type); | |
9580628d EB |
8160 | finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info); |
8161 | gnu_rep_part | |
8162 | = create_rep_part (gnu_rep_type, gnu_variant_type, | |
8163 | this_first_free_pos); | |
8164 | DECL_CHAIN (gnu_rep_part) = gnu_field_list; | |
8165 | gnu_field_list = gnu_rep_part; | |
8166 | finish_record_type (gnu_variant_type, gnu_field_list, 0, | |
8167 | false); | |
29e100b3 | 8168 | } |
9580628d EB |
8169 | |
8170 | if (debug_info) | |
8171 | rest_of_record_type_compilation (gnu_variant_type); | |
95c1c4bb | 8172 | create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type, |
986ccd21 | 8173 | true, needs_xv_encodings, gnat_component_list); |
a1ab4c31 | 8174 | |
da01bfee | 8175 | gnu_field |
9580628d | 8176 | = create_field_decl (gnu_variant->name, gnu_variant_type, |
da01bfee EB |
8177 | gnu_union_type, |
8178 | all_rep_and_size | |
8179 | ? TYPE_SIZE (gnu_variant_type) : 0, | |
9580628d EB |
8180 | variants_have_rep ? bitsize_zero_node : 0, |
8181 | gnu_variant->packed, 0); | |
a1ab4c31 AC |
8182 | |
8183 | DECL_INTERNAL_P (gnu_field) = 1; | |
8184 | ||
8185 | if (!unchecked_union) | |
9580628d | 8186 | DECL_QUALIFIER (gnu_field) = gnu_variant->qual; |
a1ab4c31 AC |
8187 | } |
8188 | ||
910ad8de | 8189 | DECL_CHAIN (gnu_field) = gnu_variant_list; |
a1ab4c31 AC |
8190 | gnu_variant_list = gnu_field; |
8191 | } | |
8192 | ||
8cd28148 | 8193 | /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */ |
a1ab4c31 AC |
8194 | if (gnu_variant_list) |
8195 | { | |
8196 | int union_field_packed; | |
8197 | ||
8198 | if (all_rep_and_size) | |
8199 | { | |
8200 | TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type); | |
8201 | TYPE_SIZE_UNIT (gnu_union_type) | |
8202 | = TYPE_SIZE_UNIT (gnu_record_type); | |
8203 | } | |
8204 | ||
8205 | finish_record_type (gnu_union_type, nreverse (gnu_variant_list), | |
986ccd21 | 8206 | all_rep_and_size ? 1 : 0, needs_xv_encodings); |
a1ab4c31 AC |
8207 | |
8208 | /* If GNU_UNION_TYPE is our record type, it means we must have an | |
8209 | Unchecked_Union with no fields. Verify that and, if so, just | |
8210 | return. */ | |
8211 | if (gnu_union_type == gnu_record_type) | |
8212 | { | |
8213 | gcc_assert (unchecked_union | |
8214 | && !gnu_field_list | |
ef0feeb2 | 8215 | && !gnu_rep_list); |
9580628d | 8216 | return variants_have_rep; |
a1ab4c31 AC |
8217 | } |
8218 | ||
74746d49 | 8219 | create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true, |
986ccd21 | 8220 | needs_xv_encodings, gnat_component_list); |
95c1c4bb | 8221 | |
a1ab4c31 | 8222 | /* Deal with packedness like in gnat_to_gnu_field. */ |
29e100b3 EB |
8223 | if (union_field_needs_strict_alignment) |
8224 | union_field_packed = 0; | |
8225 | else | |
8226 | union_field_packed | |
8227 | = adjust_packed (gnu_union_type, gnu_record_type, packed); | |
a1ab4c31 | 8228 | |
ef0feeb2 | 8229 | gnu_variant_part |
a1ab4c31 | 8230 | = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type, |
29e100b3 EB |
8231 | all_rep_and_size |
8232 | ? TYPE_SIZE (gnu_union_type) : 0, | |
9580628d | 8233 | variants_have_rep ? bitsize_zero_node : 0, |
da01bfee | 8234 | union_field_packed, 0); |
a1ab4c31 | 8235 | |
ef0feeb2 | 8236 | DECL_INTERNAL_P (gnu_variant_part) = 1; |
a1ab4c31 AC |
8237 | } |
8238 | } | |
8239 | ||
8ab31c0c | 8240 | /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they do, |
8489c295 | 8241 | pull them out and put them onto the appropriate list. |
8cd28148 | 8242 | |
6bc8df24 EB |
8243 | Similarly, pull out the fields with zero size and no rep clause, as they |
8244 | would otherwise modify the layout and thus very likely run afoul of the | |
8245 | Ada semantics, which are different from those of C here. | |
8246 | ||
8ab31c0c AC |
8247 | Finally, if there is an aliased field placed in the list after fields |
8248 | with self-referential size, pull out the latter in the same way. | |
8249 | ||
8250 | Optionally, if the reordering mechanism is enabled, pull out the fields | |
8251 | with self-referential size, variable size and fixed size not a multiple | |
8252 | of a byte, so that they don't cause the regular fields to be either at | |
8253 | self-referential/variable offset or misaligned. Note, in the latter | |
8254 | case, that this can only happen in packed record types so the alignment | |
a713e7bb | 8255 | is effectively capped to the byte for the whole record. But we don't |
fdfa0e44 EB |
8256 | do it for packed record types if not all fixed-size fiels can be packed |
8257 | and for non-packed record types if pragma Optimize_Alignment (Space) is | |
8258 | specified, because this can prevent alignment gaps from being filled. | |
8ab31c0c AC |
8259 | |
8260 | Optionally, if the layout warning is enabled, keep track of the above 4 | |
8261 | different kinds of fields and issue a warning if some of them would be | |
8262 | (or are being) reordered by the reordering mechanism. | |
8263 | ||
8489c295 AC |
8264 | ??? If we reorder fields, the debugging information will be affected and |
8265 | the debugger print fields in a different order from the source code. */ | |
8266 | const bool do_reorder | |
8267 | = (Convention (gnat_record_type) == Convention_Ada | |
8268 | && !No_Reordering (gnat_record_type) | |
fdfa0e44 EB |
8269 | && !(Is_Packed (gnat_record_type) |
8270 | ? has_non_packed_fixed_size_field | |
8271 | : Optimize_Alignment_Space (gnat_record_type)) | |
b67e2ad8 | 8272 | && !Debug_Flag_Dot_R); |
8ab31c0c | 8273 | const bool w_reorder |
8489c295 AC |
8274 | = (Convention (gnat_record_type) == Convention_Ada |
8275 | && Warn_On_Questionable_Layout | |
8276 | && !(No_Reordering (gnat_record_type) && GNAT_Mode)); | |
8ab31c0c AC |
8277 | const bool in_variant = (p_gnu_rep_list != NULL); |
8278 | tree gnu_zero_list = NULL_TREE; | |
8279 | tree gnu_self_list = NULL_TREE; | |
8280 | tree gnu_var_list = NULL_TREE; | |
8281 | tree gnu_bitp_list = NULL_TREE; | |
8282 | tree gnu_tmp_bitp_list = NULL_TREE; | |
8283 | unsigned int tmp_bitp_size = 0; | |
8284 | unsigned int last_reorder_field_type = -1; | |
8285 | unsigned int tmp_last_reorder_field_type = -1; | |
ef0feeb2 EB |
8286 | |
8287 | #define MOVE_FROM_FIELD_LIST_TO(LIST) \ | |
8288 | do { \ | |
8289 | if (gnu_last) \ | |
8290 | DECL_CHAIN (gnu_last) = gnu_next; \ | |
8291 | else \ | |
8292 | gnu_field_list = gnu_next; \ | |
8293 | \ | |
8294 | DECL_CHAIN (gnu_field) = (LIST); \ | |
8295 | (LIST) = gnu_field; \ | |
8296 | } while (0) | |
8297 | ||
8ab31c0c | 8298 | gnu_last = NULL_TREE; |
8cd28148 | 8299 | for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next) |
a1ab4c31 | 8300 | { |
910ad8de | 8301 | gnu_next = DECL_CHAIN (gnu_field); |
8cd28148 | 8302 | |
a1ab4c31 AC |
8303 | if (DECL_FIELD_OFFSET (gnu_field)) |
8304 | { | |
ef0feeb2 EB |
8305 | MOVE_FROM_FIELD_LIST_TO (gnu_rep_list); |
8306 | continue; | |
8307 | } | |
8308 | ||
6bc8df24 EB |
8309 | if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field))) |
8310 | { | |
639a28ba | 8311 | DECL_SIZE_UNIT (gnu_field) = size_zero_node; |
6bc8df24 EB |
8312 | DECL_FIELD_OFFSET (gnu_field) = size_zero_node; |
8313 | SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT); | |
8314 | DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node; | |
05dbb83f | 8315 | if (DECL_ALIASED_P (gnu_field)) |
fe37c7af MM |
8316 | SET_TYPE_ALIGN (gnu_record_type, |
8317 | MAX (TYPE_ALIGN (gnu_record_type), | |
8318 | TYPE_ALIGN (TREE_TYPE (gnu_field)))); | |
6bc8df24 EB |
8319 | MOVE_FROM_FIELD_LIST_TO (gnu_zero_list); |
8320 | continue; | |
8321 | } | |
8322 | ||
8ab31c0c AC |
8323 | if (has_aliased_after_self_field && field_has_self_size (gnu_field)) |
8324 | { | |
8325 | MOVE_FROM_FIELD_LIST_TO (gnu_self_list); | |
8326 | continue; | |
8327 | } | |
8328 | ||
8329 | /* We don't need further processing in default mode. */ | |
8330 | if (!w_reorder && !do_reorder) | |
8331 | { | |
8332 | gnu_last = gnu_field; | |
8333 | continue; | |
8334 | } | |
8335 | ||
8336 | if (field_has_self_size (gnu_field)) | |
8337 | { | |
8338 | if (w_reorder) | |
8339 | { | |
8340 | if (last_reorder_field_type < 4) | |
8341 | warn_on_field_placement (gnu_field, gnat_component_list, | |
8342 | gnat_record_type, in_variant, | |
8343 | do_reorder); | |
8344 | else | |
8345 | last_reorder_field_type = 4; | |
8346 | } | |
8347 | ||
8348 | if (do_reorder) | |
8349 | { | |
8350 | MOVE_FROM_FIELD_LIST_TO (gnu_self_list); | |
8351 | continue; | |
8352 | } | |
8353 | } | |
8354 | ||
8355 | else if (field_has_variable_size (gnu_field)) | |
8356 | { | |
8357 | if (w_reorder) | |
8358 | { | |
8359 | if (last_reorder_field_type < 3) | |
8360 | warn_on_field_placement (gnu_field, gnat_component_list, | |
8361 | gnat_record_type, in_variant, | |
8362 | do_reorder); | |
8363 | else | |
8364 | last_reorder_field_type = 3; | |
8365 | } | |
8366 | ||
8367 | if (do_reorder) | |
8368 | { | |
8369 | MOVE_FROM_FIELD_LIST_TO (gnu_var_list); | |
8370 | continue; | |
8371 | } | |
8372 | } | |
8373 | ||
8374 | else | |
8375 | { | |
8376 | /* If the field has no size, then it cannot be bit-packed. */ | |
8377 | const unsigned int bitp_size | |
8378 | = DECL_SIZE (gnu_field) | |
8379 | ? TREE_INT_CST_LOW (DECL_SIZE (gnu_field)) % BITS_PER_UNIT | |
8380 | : 0; | |
8381 | ||
8382 | /* If the field is bit-packed, we move it to a temporary list that | |
8383 | contains the contiguously preceding bit-packed fields, because | |
8384 | we want to be able to put them back if the misalignment happens | |
8385 | to cancel itself after several bit-packed fields. */ | |
8386 | if (bitp_size != 0) | |
8387 | { | |
8388 | tmp_bitp_size = (tmp_bitp_size + bitp_size) % BITS_PER_UNIT; | |
8389 | ||
8390 | if (last_reorder_field_type != 2) | |
8391 | { | |
8392 | tmp_last_reorder_field_type = last_reorder_field_type; | |
8393 | last_reorder_field_type = 2; | |
8394 | } | |
8395 | ||
8396 | if (do_reorder) | |
8397 | { | |
8398 | MOVE_FROM_FIELD_LIST_TO (gnu_tmp_bitp_list); | |
8399 | continue; | |
8400 | } | |
8401 | } | |
8402 | ||
8403 | /* No more bit-packed fields, move the existing ones to the end or | |
8404 | put them back at their original location. */ | |
8405 | else if (last_reorder_field_type == 2 || gnu_tmp_bitp_list) | |
8406 | { | |
8407 | last_reorder_field_type = 1; | |
8408 | ||
8409 | if (tmp_bitp_size != 0) | |
8410 | { | |
8411 | if (w_reorder && tmp_last_reorder_field_type < 2) | |
81034751 EB |
8412 | { |
8413 | if (gnu_tmp_bitp_list) | |
8414 | warn_on_list_placement (gnu_tmp_bitp_list, | |
8415 | gnat_component_list, | |
8416 | gnat_record_type, in_variant, | |
8417 | do_reorder); | |
8418 | else | |
8419 | warn_on_field_placement (gnu_last, | |
8420 | gnat_component_list, | |
8421 | gnat_record_type, in_variant, | |
8422 | do_reorder); | |
8423 | } | |
8ab31c0c AC |
8424 | |
8425 | if (do_reorder) | |
8426 | gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list); | |
8427 | ||
8428 | gnu_tmp_bitp_list = NULL_TREE; | |
8429 | tmp_bitp_size = 0; | |
8430 | } | |
8431 | else | |
8432 | { | |
8433 | /* Rechain the temporary list in front of GNU_FIELD. */ | |
8434 | tree gnu_bitp_field = gnu_field; | |
8435 | while (gnu_tmp_bitp_list) | |
8436 | { | |
8437 | tree gnu_bitp_next = DECL_CHAIN (gnu_tmp_bitp_list); | |
8438 | DECL_CHAIN (gnu_tmp_bitp_list) = gnu_bitp_field; | |
8439 | if (gnu_last) | |
8440 | DECL_CHAIN (gnu_last) = gnu_tmp_bitp_list; | |
8441 | else | |
8442 | gnu_field_list = gnu_tmp_bitp_list; | |
8443 | gnu_bitp_field = gnu_tmp_bitp_list; | |
8444 | gnu_tmp_bitp_list = gnu_bitp_next; | |
8445 | } | |
8446 | } | |
8447 | } | |
8448 | ||
8449 | else | |
8450 | last_reorder_field_type = 1; | |
8451 | } | |
8452 | ||
ef0feeb2 | 8453 | gnu_last = gnu_field; |
a1ab4c31 AC |
8454 | } |
8455 | ||
ef0feeb2 EB |
8456 | #undef MOVE_FROM_FIELD_LIST_TO |
8457 | ||
9580628d EB |
8458 | gnu_field_list = nreverse (gnu_field_list); |
8459 | ||
5f2e59d4 | 8460 | /* If permitted, we reorder the fields as follows: |
ef0feeb2 | 8461 | |
8ab31c0c AC |
8462 | 1) all (groups of) fields whose length is fixed and multiple of a byte, |
8463 | 2) the remaining fields whose length is fixed and not multiple of a byte, | |
8464 | 3) the remaining fields whose length doesn't depend on discriminants, | |
8465 | 4) all fields whose length depends on discriminants, | |
8466 | 5) the variant part, | |
ef0feeb2 EB |
8467 | |
8468 | within the record and within each variant recursively. */ | |
a01ebdf5 EB |
8469 | |
8470 | if (w_reorder) | |
8471 | { | |
8472 | /* If we have pending bit-packed fields, warn if they would be moved | |
8473 | to after regular fields. */ | |
8474 | if (last_reorder_field_type == 2 | |
8475 | && tmp_bitp_size != 0 | |
8476 | && tmp_last_reorder_field_type < 2) | |
81034751 EB |
8477 | { |
8478 | if (gnu_tmp_bitp_list) | |
8479 | warn_on_list_placement (gnu_tmp_bitp_list, | |
8480 | gnat_component_list, gnat_record_type, | |
8481 | in_variant, do_reorder); | |
8482 | else | |
8483 | warn_on_field_placement (gnu_field_list, | |
8484 | gnat_component_list, gnat_record_type, | |
8485 | in_variant, do_reorder); | |
8486 | } | |
a01ebdf5 EB |
8487 | } |
8488 | ||
8ab31c0c AC |
8489 | if (do_reorder) |
8490 | { | |
0a69d9bd EB |
8491 | /* If we have pending bit-packed fields on the temporary list, we put |
8492 | them either on the bit-packed list or back on the regular list. */ | |
8ab31c0c | 8493 | if (gnu_tmp_bitp_list) |
0a69d9bd EB |
8494 | { |
8495 | if (tmp_bitp_size != 0) | |
8496 | gnu_bitp_list = chainon (gnu_tmp_bitp_list, gnu_bitp_list); | |
8497 | else | |
8498 | gnu_field_list = chainon (gnu_tmp_bitp_list, gnu_field_list); | |
8499 | } | |
8ab31c0c AC |
8500 | |
8501 | gnu_field_list | |
8502 | = chainon (gnu_field_list, | |
8503 | chainon (gnu_bitp_list, | |
8504 | chainon (gnu_var_list, gnu_self_list))); | |
8505 | } | |
ef0feeb2 | 8506 | |
5f2e59d4 EB |
8507 | /* Otherwise, if there is an aliased field placed after a field whose length |
8508 | depends on discriminants, we put all the fields of the latter sort, last. | |
8509 | We need to do this in case an object of this record type is mutable. */ | |
8510 | else if (has_aliased_after_self_field) | |
9580628d | 8511 | gnu_field_list = chainon (gnu_field_list, gnu_self_list); |
5f2e59d4 | 8512 | |
b1a785fb EB |
8513 | /* If P_REP_LIST is nonzero, this means that we are asked to move the fields |
8514 | in our REP list to the previous level because this level needs them in | |
8515 | order to do a correct layout, i.e. avoid having overlapping fields. */ | |
8516 | if (p_gnu_rep_list && gnu_rep_list) | |
ef0feeb2 | 8517 | *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list); |
8cd28148 | 8518 | |
e8c87bc0 EB |
8519 | /* Deal with the case of an extension of a record type with variable size and |
8520 | partial rep clause, for which the _Parent field is forced at offset 0 and | |
8521 | has variable size. Note that we cannot do it if the field has fixed size | |
8522 | because we rely on the presence of the REP part built below to trigger the | |
8523 | reordering of the fields in a derived record type when all the fields have | |
8524 | a fixed position. */ | |
a1799e5e EB |
8525 | else if (gnu_rep_list |
8526 | && !DECL_CHAIN (gnu_rep_list) | |
7d9979e6 | 8527 | && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST |
a1799e5e EB |
8528 | && !variants_have_rep |
8529 | && first_free_pos | |
8530 | && integer_zerop (first_free_pos) | |
8531 | && integer_zerop (bit_position (gnu_rep_list))) | |
8532 | { | |
8533 | DECL_CHAIN (gnu_rep_list) = gnu_field_list; | |
8534 | gnu_field_list = gnu_rep_list; | |
8535 | gnu_rep_list = NULL_TREE; | |
8536 | } | |
8537 | ||
8cd28148 | 8538 | /* Otherwise, sort the fields by bit position and put them into their own |
b1a785fb | 8539 | record, before the others, if we also have fields without rep clause. */ |
ef0feeb2 | 8540 | else if (gnu_rep_list) |
a1ab4c31 | 8541 | { |
e8c87bc0 | 8542 | tree gnu_parent, gnu_rep_type; |
a1ab4c31 | 8543 | |
9580628d EB |
8544 | /* If all the fields have a rep clause, we can do a flat layout. */ |
8545 | layout_with_rep = !gnu_field_list | |
8546 | && (!gnu_variant_part || variants_have_rep); | |
e8c87bc0 EB |
8547 | |
8548 | /* Same as above but the extension itself has a rep clause, in which case | |
8549 | we need to set aside the _Parent field to lay out the REP part. */ | |
8550 | if (TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST | |
8551 | && !layout_with_rep | |
8552 | && !variants_have_rep | |
8553 | && first_free_pos | |
8554 | && integer_zerop (first_free_pos) | |
8555 | && integer_zerop (bit_position (gnu_rep_list))) | |
8556 | { | |
8557 | gnu_parent = gnu_rep_list; | |
8558 | gnu_rep_list = DECL_CHAIN (gnu_rep_list); | |
8559 | } | |
8560 | else | |
8561 | gnu_parent = NULL_TREE; | |
8562 | ||
9580628d EB |
8563 | gnu_rep_type |
8564 | = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE); | |
8565 | ||
e8c87bc0 EB |
8566 | /* Sort the fields in order of increasing bit position. */ |
8567 | const int len = list_length (gnu_rep_list); | |
8568 | tree *gnu_arr = XALLOCAVEC (tree, len); | |
8569 | ||
8570 | gnu_field = gnu_rep_list; | |
8571 | for (int i = 0; i < len; i++) | |
8572 | { | |
8573 | gnu_arr[i] = gnu_field; | |
8574 | gnu_field = DECL_CHAIN (gnu_field); | |
8575 | } | |
a1ab4c31 AC |
8576 | |
8577 | qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos); | |
8578 | ||
ef0feeb2 | 8579 | gnu_rep_list = NULL_TREE; |
e8c87bc0 | 8580 | for (int i = len - 1; i >= 0; i--) |
a1ab4c31 | 8581 | { |
ef0feeb2 EB |
8582 | DECL_CHAIN (gnu_arr[i]) = gnu_rep_list; |
8583 | gnu_rep_list = gnu_arr[i]; | |
a1ab4c31 AC |
8584 | DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type; |
8585 | } | |
8586 | ||
e8c87bc0 | 8587 | /* Do the layout of the REP part, if any. */ |
9580628d EB |
8588 | if (layout_with_rep) |
8589 | gnu_field_list = gnu_rep_list; | |
8590 | else | |
a1ab4c31 | 8591 | { |
f65f371b EB |
8592 | TYPE_NAME (gnu_rep_type) |
8593 | = create_concat_name (gnat_record_type, "REP"); | |
ee45a32d EB |
8594 | TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type) |
8595 | = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type); | |
e8c87bc0 | 8596 | finish_record_type (gnu_rep_type, gnu_rep_list, 1, false); |
b1a785fb EB |
8597 | |
8598 | /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields | |
8599 | without rep clause are laid out starting from this position. | |
8600 | Therefore, we force it as a minimal size on the REP part. */ | |
e8c87bc0 | 8601 | tree gnu_rep_part |
b1a785fb | 8602 | = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos); |
a1ab4c31 | 8603 | |
e8c87bc0 EB |
8604 | /* If this is an extension, put back the _Parent field as the first |
8605 | field of the REP part at offset 0 and update its layout. */ | |
8606 | if (gnu_parent) | |
8607 | { | |
8608 | const unsigned int align = DECL_ALIGN (gnu_parent); | |
8609 | DECL_CHAIN (gnu_parent) = TYPE_FIELDS (gnu_rep_type); | |
8610 | TYPE_FIELDS (gnu_rep_type) = gnu_parent; | |
8611 | DECL_CONTEXT (gnu_parent) = gnu_rep_type; | |
8612 | if (align > TYPE_ALIGN (gnu_rep_type)) | |
8613 | { | |
8614 | SET_TYPE_ALIGN (gnu_rep_type, align); | |
8615 | TYPE_SIZE (gnu_rep_type) | |
8616 | = round_up (TYPE_SIZE (gnu_rep_type), align); | |
8617 | TYPE_SIZE_UNIT (gnu_rep_type) | |
8618 | = round_up (TYPE_SIZE_UNIT (gnu_rep_type), align); | |
8619 | SET_DECL_ALIGN (gnu_rep_part, align); | |
8620 | } | |
8621 | } | |
8622 | ||
8623 | if (debug_info) | |
8624 | rest_of_record_type_compilation (gnu_rep_type); | |
8625 | ||
9580628d EB |
8626 | /* Chain the REP part at the beginning of the field list. */ |
8627 | DECL_CHAIN (gnu_rep_part) = gnu_field_list; | |
8628 | gnu_field_list = gnu_rep_part; | |
8629 | } | |
b1a785fb EB |
8630 | } |
8631 | ||
9580628d | 8632 | /* Chain the variant part at the end of the field list. */ |
b1a785fb | 8633 | if (gnu_variant_part) |
0d8f74b4 | 8634 | gnu_field_list = chainon (gnu_field_list, gnu_variant_part); |
b1a785fb | 8635 | |
a1ab4c31 | 8636 | if (cancel_alignment) |
fe37c7af | 8637 | SET_TYPE_ALIGN (gnu_record_type, 0); |
a1ab4c31 | 8638 | |
fd787640 | 8639 | TYPE_ARTIFICIAL (gnu_record_type) = artificial; |
9580628d EB |
8640 | |
8641 | finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0, | |
8642 | debug_info && !maybe_unused); | |
8643 | ||
6bc8df24 EB |
8644 | /* Chain the fields with zero size at the beginning of the field list. */ |
8645 | if (gnu_zero_list) | |
8646 | TYPE_FIELDS (gnu_record_type) | |
8647 | = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type)); | |
8648 | ||
9580628d | 8649 | return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep; |
a1ab4c31 | 8650 | } |
ce2d0ce2 | 8651 | |
a1ab4c31 AC |
8652 | /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be |
8653 | placed into an Esize, Component_Bit_Offset, or Component_Size value | |
8654 | in the GNAT tree. */ | |
8655 | ||
8656 | static Uint | |
8657 | annotate_value (tree gnu_size) | |
8658 | { | |
e45f84a5 | 8659 | static int var_count = 0; |
a1ab4c31 | 8660 | TCode tcode; |
e45f84a5 | 8661 | Node_Ref_Or_Val ops[3] = { No_Uint, No_Uint, No_Uint }; |
0e871c15 | 8662 | struct tree_int_map in; |
a1ab4c31 AC |
8663 | |
8664 | /* See if we've already saved the value for this node. */ | |
e45f84a5 | 8665 | if (EXPR_P (gnu_size) || DECL_P (gnu_size)) |
a1ab4c31 | 8666 | { |
0e871c15 AO |
8667 | struct tree_int_map *e; |
8668 | ||
a1ab4c31 | 8669 | in.base.from = gnu_size; |
d242408f | 8670 | e = annotate_value_cache->find (&in); |
a1ab4c31 | 8671 | |
0e871c15 AO |
8672 | if (e) |
8673 | return (Node_Ref_Or_Val) e->to; | |
a1ab4c31 | 8674 | } |
0e871c15 AO |
8675 | else |
8676 | in.base.from = NULL_TREE; | |
a1ab4c31 AC |
8677 | |
8678 | /* If we do not return inside this switch, TCODE will be set to the | |
e45f84a5 | 8679 | code to be used in a call to Create_Node. */ |
a1ab4c31 AC |
8680 | switch (TREE_CODE (gnu_size)) |
8681 | { | |
8682 | case INTEGER_CST: | |
c0c54de6 | 8683 | /* For negative values, build NEGATE_EXPR of the opposite. Such values |
05626b02 EB |
8684 | can appear for discriminants in expressions for variants. */ |
8685 | if (tree_int_cst_sgn (gnu_size) < 0) | |
c0c54de6 | 8686 | { |
8e6cdc90 | 8687 | tree t = wide_int_to_tree (sizetype, -wi::to_wide (gnu_size)); |
e45f84a5 EB |
8688 | tcode = Negate_Expr; |
8689 | ops[0] = UI_From_gnu (t); | |
c0c54de6 | 8690 | } |
e45f84a5 EB |
8691 | else |
8692 | return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size); | |
8693 | break; | |
a1ab4c31 AC |
8694 | |
8695 | case COMPONENT_REF: | |
8696 | /* The only case we handle here is a simple discriminant reference. */ | |
c19ff724 EB |
8697 | if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1))) |
8698 | { | |
e45f84a5 EB |
8699 | tree ref = gnu_size; |
8700 | gnu_size = TREE_OPERAND (ref, 1); | |
c19ff724 EB |
8701 | |
8702 | /* Climb up the chain of successive extensions, if any. */ | |
e45f84a5 EB |
8703 | while (TREE_CODE (TREE_OPERAND (ref, 0)) == COMPONENT_REF |
8704 | && DECL_NAME (TREE_OPERAND (TREE_OPERAND (ref, 0), 1)) | |
c19ff724 | 8705 | == parent_name_id) |
e45f84a5 | 8706 | ref = TREE_OPERAND (ref, 0); |
c19ff724 | 8707 | |
e45f84a5 EB |
8708 | if (TREE_CODE (TREE_OPERAND (ref, 0)) == PLACEHOLDER_EXPR) |
8709 | { | |
8710 | /* Fall through to common processing as a FIELD_DECL. */ | |
8711 | tcode = Discrim_Val; | |
8712 | ops[0] = UI_From_gnu (DECL_DISCRIMINANT_NUMBER (gnu_size)); | |
8713 | } | |
8714 | else | |
8715 | return No_Uint; | |
c19ff724 | 8716 | } |
e45f84a5 EB |
8717 | else |
8718 | return No_Uint; | |
8719 | break; | |
c19ff724 | 8720 | |
e45f84a5 EB |
8721 | case VAR_DECL: |
8722 | tcode = Dynamic_Val; | |
8723 | ops[0] = UI_From_Int (++var_count); | |
8724 | break; | |
a1ab4c31 | 8725 | |
e45f84a5 EB |
8726 | CASE_CONVERT: |
8727 | case NON_LVALUE_EXPR: | |
a1ab4c31 AC |
8728 | return annotate_value (TREE_OPERAND (gnu_size, 0)); |
8729 | ||
8730 | /* Now just list the operations we handle. */ | |
8731 | case COND_EXPR: tcode = Cond_Expr; break; | |
a1ab4c31 | 8732 | case MINUS_EXPR: tcode = Minus_Expr; break; |
a1ab4c31 AC |
8733 | case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break; |
8734 | case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break; | |
8735 | case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break; | |
8736 | case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break; | |
8737 | case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break; | |
8738 | case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break; | |
8739 | case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break; | |
8740 | case NEGATE_EXPR: tcode = Negate_Expr; break; | |
8741 | case MIN_EXPR: tcode = Min_Expr; break; | |
8742 | case MAX_EXPR: tcode = Max_Expr; break; | |
8743 | case ABS_EXPR: tcode = Abs_Expr; break; | |
72da915b | 8744 | case TRUTH_ANDIF_EXPR: |
a1ab4c31 | 8745 | case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break; |
72da915b | 8746 | case TRUTH_ORIF_EXPR: |
a1ab4c31 AC |
8747 | case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break; |
8748 | case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break; | |
8749 | case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break; | |
a1ab4c31 AC |
8750 | case LT_EXPR: tcode = Lt_Expr; break; |
8751 | case LE_EXPR: tcode = Le_Expr; break; | |
8752 | case GT_EXPR: tcode = Gt_Expr; break; | |
8753 | case GE_EXPR: tcode = Ge_Expr; break; | |
8754 | case EQ_EXPR: tcode = Eq_Expr; break; | |
8755 | case NE_EXPR: tcode = Ne_Expr; break; | |
8756 | ||
e45f84a5 | 8757 | case PLUS_EXPR: |
03160cc9 EB |
8758 | /* Turn addition of negative constant into subtraction. */ |
8759 | if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST | |
8760 | && tree_int_cst_sign_bit (TREE_OPERAND (gnu_size, 1))) | |
8761 | { | |
8762 | tcode = Minus_Expr; | |
05626b02 EB |
8763 | wide_int wop1 = -wi::to_wide (TREE_OPERAND (gnu_size, 1)); |
8764 | ops[1] = annotate_value (wide_int_to_tree (sizetype, wop1)); | |
03160cc9 EB |
8765 | break; |
8766 | } | |
8767 | ||
8768 | /* ... fall through ... */ | |
8769 | ||
8770 | case MULT_EXPR: | |
e45f84a5 EB |
8771 | tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr); |
8772 | /* Fold conversions from bytes to bits into inner operations. */ | |
8773 | if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST | |
8774 | && CONVERT_EXPR_P (TREE_OPERAND (gnu_size, 0))) | |
8775 | { | |
8776 | tree inner_op = TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 0); | |
8777 | if (TREE_CODE (inner_op) == TREE_CODE (gnu_size) | |
8778 | && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST) | |
8779 | { | |
03160cc9 | 8780 | ops[0] = annotate_value (TREE_OPERAND (inner_op, 0)); |
e45f84a5 EB |
8781 | tree inner_op_op1 = TREE_OPERAND (inner_op, 1); |
8782 | tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1); | |
a1488398 | 8783 | widest_int op1; |
e45f84a5 | 8784 | if (TREE_CODE (gnu_size) == MULT_EXPR) |
a1488398 RS |
8785 | op1 = (wi::to_widest (inner_op_op1) |
8786 | * wi::to_widest (gnu_size_op1)); | |
e45f84a5 | 8787 | else |
03160cc9 EB |
8788 | { |
8789 | op1 = (wi::to_widest (inner_op_op1) | |
8790 | + wi::to_widest (gnu_size_op1)); | |
8791 | if (wi::zext (op1, TYPE_PRECISION (sizetype)) == 0) | |
8792 | return ops[0]; | |
8793 | } | |
8794 | ops[1] = annotate_value (wide_int_to_tree (sizetype, op1)); | |
e45f84a5 EB |
8795 | } |
8796 | } | |
8797 | break; | |
8798 | ||
ce3da0d0 EB |
8799 | case BIT_AND_EXPR: |
8800 | tcode = Bit_And_Expr; | |
f0035dca | 8801 | /* For negative values in sizetype, build NEGATE_EXPR of the opposite. |
03160cc9 | 8802 | Such values can appear in expressions with aligning patterns. */ |
ce3da0d0 EB |
8803 | if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST) |
8804 | { | |
05626b02 EB |
8805 | wide_int wop1 = -wi::to_wide (TREE_OPERAND (gnu_size, 1)); |
8806 | tree op1 = wide_int_to_tree (sizetype, wop1); | |
8807 | ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1)); | |
ce3da0d0 EB |
8808 | } |
8809 | break; | |
8810 | ||
f82a627c | 8811 | case CALL_EXPR: |
4116e7d0 EB |
8812 | /* In regular mode, inline back only if symbolic annotation is requested |
8813 | in order to avoid memory explosion on big discriminated record types. | |
8814 | But not in ASIS mode, as symbolic annotation is required for DDA. */ | |
37cf9302 | 8815 | if (List_Representation_Info >= 3 || type_annotate_only) |
4116e7d0 EB |
8816 | { |
8817 | tree t = maybe_inline_call_in_expr (gnu_size); | |
e45f84a5 | 8818 | return t ? annotate_value (t) : No_Uint; |
4116e7d0 EB |
8819 | } |
8820 | else | |
8821 | return Uint_Minus_1; | |
f82a627c | 8822 | |
a1ab4c31 AC |
8823 | default: |
8824 | return No_Uint; | |
8825 | } | |
8826 | ||
8827 | /* Now get each of the operands that's relevant for this code. If any | |
8828 | cannot be expressed as a repinfo node, say we can't. */ | |
e45f84a5 EB |
8829 | for (int i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++) |
8830 | if (ops[i] == No_Uint) | |
8831 | { | |
ce3da0d0 | 8832 | ops[i] = annotate_value (TREE_OPERAND (gnu_size, i)); |
e45f84a5 EB |
8833 | if (ops[i] == No_Uint) |
8834 | return No_Uint; | |
8835 | } | |
a1ab4c31 | 8836 | |
e45f84a5 | 8837 | Node_Ref_Or_Val ret = Create_Node (tcode, ops[0], ops[1], ops[2]); |
a1ab4c31 AC |
8838 | |
8839 | /* Save the result in the cache. */ | |
0e871c15 | 8840 | if (in.base.from) |
a1ab4c31 | 8841 | { |
0e871c15 | 8842 | struct tree_int_map **h; |
4116e7d0 EB |
8843 | /* We can't assume the hash table data hasn't moved since the initial |
8844 | look up, so we have to search again. Allocating and inserting an | |
8845 | entry at that point would be an alternative, but then we'd better | |
8846 | discard the entry if we decided not to cache it. */ | |
d242408f | 8847 | h = annotate_value_cache->find_slot (&in, INSERT); |
0e871c15 | 8848 | gcc_assert (!*h); |
766090c2 | 8849 | *h = ggc_alloc<tree_int_map> (); |
e45f84a5 | 8850 | (*h)->base.from = in.base.from; |
a1ab4c31 AC |
8851 | (*h)->to = ret; |
8852 | } | |
8853 | ||
8854 | return ret; | |
8855 | } | |
8856 | ||
f4cd2542 EB |
8857 | /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception) |
8858 | and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the | |
8859 | size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null. | |
491f54a7 | 8860 | BY_REF is true if the object is used by reference. */ |
f4cd2542 EB |
8861 | |
8862 | void | |
491f54a7 | 8863 | annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref) |
f4cd2542 EB |
8864 | { |
8865 | if (by_ref) | |
8866 | { | |
315cff15 | 8867 | if (TYPE_IS_FAT_POINTER_P (gnu_type)) |
f4cd2542 EB |
8868 | gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type); |
8869 | else | |
8870 | gnu_type = TREE_TYPE (gnu_type); | |
8871 | } | |
8872 | ||
8de68eb3 | 8873 | if (!Known_Esize (gnat_entity)) |
f4cd2542 EB |
8874 | { |
8875 | if (TREE_CODE (gnu_type) == RECORD_TYPE | |
8876 | && TYPE_CONTAINS_TEMPLATE_P (gnu_type)) | |
910ad8de | 8877 | size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))); |
f4cd2542 EB |
8878 | else if (!size) |
8879 | size = TYPE_SIZE (gnu_type); | |
8880 | ||
8881 | if (size) | |
b23cdc01 | 8882 | Set_Esize (gnat_entity, No_Uint_To_0 (annotate_value (size))); |
f4cd2542 EB |
8883 | } |
8884 | ||
8de68eb3 | 8885 | if (!Known_Alignment (gnat_entity)) |
f4cd2542 EB |
8886 | Set_Alignment (gnat_entity, |
8887 | UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT)); | |
8888 | } | |
8889 | ||
cb3d597d EB |
8890 | /* Return first element of field list whose TREE_PURPOSE is the same as ELEM. |
8891 | Return NULL_TREE if there is no such element in the list. */ | |
73d28034 EB |
8892 | |
8893 | static tree | |
8894 | purpose_member_field (const_tree elem, tree list) | |
8895 | { | |
8896 | while (list) | |
8897 | { | |
8898 | tree field = TREE_PURPOSE (list); | |
cb3d597d | 8899 | if (SAME_FIELD_P (field, elem)) |
73d28034 EB |
8900 | return list; |
8901 | list = TREE_CHAIN (list); | |
8902 | } | |
8903 | return NULL_TREE; | |
8904 | } | |
8905 | ||
3f13dd77 EB |
8906 | /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type, |
8907 | set Component_Bit_Offset and Esize of the components to the position and | |
8908 | size used by Gigi. */ | |
a1ab4c31 AC |
8909 | |
8910 | static void | |
8911 | annotate_rep (Entity_Id gnat_entity, tree gnu_type) | |
8912 | { | |
05dbb83f AC |
8913 | /* For an extension, the inherited components have not been translated because |
8914 | they are fetched from the _Parent component on the fly. */ | |
8915 | const bool is_extension | |
8916 | = Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity); | |
a1ab4c31 | 8917 | |
3f13dd77 EB |
8918 | /* We operate by first making a list of all fields and their position (we |
8919 | can get the size easily) and then update all the sizes in the tree. */ | |
05dbb83f | 8920 | tree gnu_list |
95c1c4bb EB |
8921 | = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node, |
8922 | BIGGEST_ALIGNMENT, NULL_TREE); | |
a1ab4c31 | 8923 | |
05dbb83f | 8924 | for (Entity_Id gnat_field = First_Entity (gnat_entity); |
3f13dd77 | 8925 | Present (gnat_field); |
a1ab4c31 | 8926 | gnat_field = Next_Entity (gnat_field)) |
05dbb83f AC |
8927 | if ((Ekind (gnat_field) == E_Component |
8928 | && (is_extension || present_gnu_tree (gnat_field))) | |
3f13dd77 EB |
8929 | || (Ekind (gnat_field) == E_Discriminant |
8930 | && !Is_Unchecked_Union (Scope (gnat_field)))) | |
a1ab4c31 | 8931 | { |
73d28034 EB |
8932 | tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field), |
8933 | gnu_list); | |
3f13dd77 | 8934 | if (t) |
a1ab4c31 | 8935 | { |
63a329f8 EB |
8936 | tree offset = TREE_VEC_ELT (TREE_VALUE (t), 0); |
8937 | tree bit_offset = TREE_VEC_ELT (TREE_VALUE (t), 2); | |
73d28034 | 8938 | |
b38086f0 EB |
8939 | /* If we are just annotating types and the type is tagged, the tag |
8940 | and the parent components are not generated by the front-end so | |
8941 | we need to add the appropriate offset to each component without | |
8942 | representation clause. */ | |
8943 | if (type_annotate_only | |
8944 | && Is_Tagged_Type (gnat_entity) | |
8945 | && No (Component_Clause (gnat_field))) | |
a1ab4c31 | 8946 | { |
63a329f8 EB |
8947 | tree parent_bit_offset; |
8948 | ||
b38086f0 EB |
8949 | /* For a component appearing in the current extension, the |
8950 | offset is the size of the parent. */ | |
3f13dd77 EB |
8951 | if (Is_Derived_Type (gnat_entity) |
8952 | && Original_Record_Component (gnat_field) == gnat_field) | |
63a329f8 | 8953 | parent_bit_offset |
3f13dd77 EB |
8954 | = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))), |
8955 | bitsizetype); | |
8956 | else | |
63a329f8 | 8957 | parent_bit_offset = bitsize_int (POINTER_SIZE); |
b38086f0 EB |
8958 | |
8959 | if (TYPE_FIELDS (gnu_type)) | |
63a329f8 EB |
8960 | parent_bit_offset |
8961 | = round_up (parent_bit_offset, | |
b38086f0 | 8962 | DECL_ALIGN (TYPE_FIELDS (gnu_type))); |
63a329f8 EB |
8963 | |
8964 | offset | |
8965 | = size_binop (PLUS_EXPR, offset, | |
8966 | fold_convert (sizetype, | |
8967 | size_binop (TRUNC_DIV_EXPR, | |
8968 | parent_bit_offset, | |
8969 | bitsize_unit_node))); | |
8970 | } | |
8971 | ||
8972 | /* If the field has a variable offset, also compute the normalized | |
8973 | position since it's easier to do on trees here than to deduce | |
8974 | it from the annotated expression of Component_Bit_Offset. */ | |
8975 | if (TREE_CODE (offset) != INTEGER_CST) | |
8976 | { | |
8977 | normalize_offset (&offset, &bit_offset, BITS_PER_UNIT); | |
8978 | Set_Normalized_Position (gnat_field, | |
8979 | annotate_value (offset)); | |
8980 | Set_Normalized_First_Bit (gnat_field, | |
8981 | annotate_value (bit_offset)); | |
a1ab4c31 AC |
8982 | } |
8983 | ||
3f13dd77 EB |
8984 | Set_Component_Bit_Offset |
8985 | (gnat_field, | |
63a329f8 | 8986 | annotate_value (bit_from_pos (offset, bit_offset))); |
a1ab4c31 | 8987 | |
b23cdc01 BD |
8988 | Set_Esize |
8989 | (gnat_field, | |
8990 | No_Uint_To_0 (annotate_value (DECL_SIZE (TREE_PURPOSE (t))))); | |
a1ab4c31 | 8991 | } |
05dbb83f | 8992 | else if (is_extension) |
a1ab4c31 | 8993 | { |
3f13dd77 | 8994 | /* If there is no entry, this is an inherited component whose |
a1ab4c31 | 8995 | position is the same as in the parent type. */ |
63a329f8 | 8996 | Entity_Id gnat_orig = Original_Record_Component (gnat_field); |
3f13dd77 | 8997 | |
c00d5b12 EB |
8998 | /* If we are just annotating types, discriminants renaming those of |
8999 | the parent have no entry so deal with them specifically. */ | |
9000 | if (type_annotate_only | |
63a329f8 | 9001 | && gnat_orig == gnat_field |
c00d5b12 | 9002 | && Ekind (gnat_field) == E_Discriminant) |
63a329f8 EB |
9003 | gnat_orig = Corresponding_Discriminant (gnat_field); |
9004 | ||
9005 | if (Known_Normalized_Position (gnat_orig)) | |
9006 | { | |
9007 | Set_Normalized_Position (gnat_field, | |
9008 | Normalized_Position (gnat_orig)); | |
9009 | Set_Normalized_First_Bit (gnat_field, | |
9010 | Normalized_First_Bit (gnat_orig)); | |
9011 | } | |
c00d5b12 EB |
9012 | |
9013 | Set_Component_Bit_Offset (gnat_field, | |
63a329f8 | 9014 | Component_Bit_Offset (gnat_orig)); |
c00d5b12 | 9015 | |
63a329f8 | 9016 | Set_Esize (gnat_field, Esize (gnat_orig)); |
a1ab4c31 AC |
9017 | } |
9018 | } | |
9019 | } | |
ce2d0ce2 | 9020 | |
95c1c4bb EB |
9021 | /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is |
9022 | the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the | |
9023 | value to be placed into DECL_OFFSET_ALIGN and the bit position. The list | |
9024 | of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT | |
9025 | is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the | |
9026 | bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a | |
9027 | pre-existing list to be chained to the newly created entries. */ | |
a1ab4c31 AC |
9028 | |
9029 | static tree | |
95c1c4bb EB |
9030 | build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos, |
9031 | tree gnu_bitpos, unsigned int offset_align, tree gnu_list) | |
a1ab4c31 AC |
9032 | { |
9033 | tree gnu_field; | |
a1ab4c31 | 9034 | |
3f13dd77 EB |
9035 | for (gnu_field = TYPE_FIELDS (gnu_type); |
9036 | gnu_field; | |
910ad8de | 9037 | gnu_field = DECL_CHAIN (gnu_field)) |
a1ab4c31 AC |
9038 | { |
9039 | tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos, | |
9040 | DECL_FIELD_BIT_OFFSET (gnu_field)); | |
9041 | tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos, | |
9042 | DECL_FIELD_OFFSET (gnu_field)); | |
9043 | unsigned int our_offset_align | |
9044 | = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field)); | |
95c1c4bb | 9045 | tree v = make_tree_vec (3); |
a1ab4c31 | 9046 | |
95c1c4bb EB |
9047 | TREE_VEC_ELT (v, 0) = gnu_our_offset; |
9048 | TREE_VEC_ELT (v, 1) = size_int (our_offset_align); | |
9049 | TREE_VEC_ELT (v, 2) = gnu_our_bitpos; | |
9050 | gnu_list = tree_cons (gnu_field, v, gnu_list); | |
a1ab4c31 | 9051 | |
95c1c4bb EB |
9052 | /* Recurse on internal fields, flattening the nested fields except for |
9053 | those in the variant part, if requested. */ | |
a1ab4c31 | 9054 | if (DECL_INTERNAL_P (gnu_field)) |
95c1c4bb EB |
9055 | { |
9056 | tree gnu_field_type = TREE_TYPE (gnu_field); | |
9057 | if (do_not_flatten_variant | |
9058 | && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE) | |
9059 | gnu_list | |
9060 | = build_position_list (gnu_field_type, do_not_flatten_variant, | |
9061 | size_zero_node, bitsize_zero_node, | |
9062 | BIGGEST_ALIGNMENT, gnu_list); | |
9063 | else | |
9064 | gnu_list | |
9065 | = build_position_list (gnu_field_type, do_not_flatten_variant, | |
a1ab4c31 | 9066 | gnu_our_offset, gnu_our_bitpos, |
95c1c4bb EB |
9067 | our_offset_align, gnu_list); |
9068 | } | |
9069 | } | |
9070 | ||
9071 | return gnu_list; | |
9072 | } | |
9073 | ||
f54ee980 | 9074 | /* Return a list describing the substitutions needed to reflect the |
95c1c4bb | 9075 | discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can |
f54ee980 | 9076 | be in any order. The values in an element of the list are in the form |
e3554601 NF |
9077 | of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for |
9078 | a definition of GNAT_SUBTYPE. */ | |
95c1c4bb | 9079 | |
b16b6cc9 | 9080 | static vec<subst_pair> |
95c1c4bb EB |
9081 | build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition) |
9082 | { | |
6e1aa848 | 9083 | vec<subst_pair> gnu_list = vNULL; |
95c1c4bb | 9084 | Entity_Id gnat_discrim; |
908ba941 | 9085 | Node_Id gnat_constr; |
95c1c4bb EB |
9086 | |
9087 | for (gnat_discrim = First_Stored_Discriminant (gnat_type), | |
908ba941 | 9088 | gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype)); |
95c1c4bb EB |
9089 | Present (gnat_discrim); |
9090 | gnat_discrim = Next_Stored_Discriminant (gnat_discrim), | |
908ba941 | 9091 | gnat_constr = Next_Elmt (gnat_constr)) |
95c1c4bb | 9092 | /* Ignore access discriminants. */ |
908ba941 | 9093 | if (!Is_Access_Type (Etype (Node (gnat_constr)))) |
3c28a5f4 EB |
9094 | { |
9095 | tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim); | |
71465223 EB |
9096 | tree replacement |
9097 | = elaborate_expression (Node (gnat_constr), gnat_subtype, | |
9098 | get_entity_char (gnat_discrim), | |
9099 | definition, true, false); | |
9100 | /* If this is a definition, we need to make sure that the SAVE_EXPRs | |
9101 | are instantiated on every possibly path in size computations. */ | |
9102 | if (definition && TREE_CODE (replacement) == SAVE_EXPR) | |
9103 | add_stmt (replacement); | |
9104 | replacement = convert (TREE_TYPE (gnu_field), replacement); | |
05dbb83f | 9105 | subst_pair s = { gnu_field, replacement }; |
9771b263 | 9106 | gnu_list.safe_push (s); |
3c28a5f4 | 9107 | } |
95c1c4bb | 9108 | |
f54ee980 | 9109 | return gnu_list; |
95c1c4bb EB |
9110 | } |
9111 | ||
036c83b6 EB |
9112 | /* Scan all fields in {GNU_QUAL_UNION_TYPE,GNAT_VARIANT_PART} and return a list |
9113 | describing the variants of GNU_QUAL_UNION_TYPE that are still relevant after | |
9114 | applying the substitutions described in SUBST_LIST. GNU_LIST is an existing | |
f54ee980 | 9115 | list to be prepended to the newly created entries. */ |
95c1c4bb | 9116 | |
b16b6cc9 | 9117 | static vec<variant_desc> |
036c83b6 EB |
9118 | build_variant_list (tree gnu_qual_union_type, Node_Id gnat_variant_part, |
9119 | vec<subst_pair> subst_list, vec<variant_desc> gnu_list) | |
95c1c4bb | 9120 | { |
036c83b6 | 9121 | Node_Id gnat_variant; |
95c1c4bb EB |
9122 | tree gnu_field; |
9123 | ||
036c83b6 EB |
9124 | for (gnu_field = TYPE_FIELDS (gnu_qual_union_type), |
9125 | gnat_variant | |
9126 | = Present (gnat_variant_part) | |
9127 | ? First_Non_Pragma (Variants (gnat_variant_part)) | |
9128 | : Empty; | |
95c1c4bb | 9129 | gnu_field; |
036c83b6 EB |
9130 | gnu_field = DECL_CHAIN (gnu_field), |
9131 | gnat_variant | |
9132 | = Present (gnat_variant_part) | |
9133 | ? Next_Non_Pragma (gnat_variant) | |
9134 | : Empty) | |
95c1c4bb | 9135 | { |
e3554601 | 9136 | tree qual = DECL_QUALIFIER (gnu_field); |
f54ee980 | 9137 | unsigned int i; |
e3554601 | 9138 | subst_pair *s; |
95c1c4bb | 9139 | |
9771b263 | 9140 | FOR_EACH_VEC_ELT (subst_list, i, s) |
e3554601 | 9141 | qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement); |
95c1c4bb EB |
9142 | |
9143 | /* If the new qualifier is not unconditionally false, its variant may | |
9144 | still be accessed. */ | |
9145 | if (!integer_zerop (qual)) | |
9146 | { | |
9147 | tree variant_type = TREE_TYPE (gnu_field), variant_subpart; | |
cd8ad459 EB |
9148 | variant_desc v |
9149 | = { variant_type, gnu_field, qual, NULL_TREE, NULL_TREE }; | |
fb7fb701 | 9150 | |
9771b263 | 9151 | gnu_list.safe_push (v); |
95c1c4bb | 9152 | |
036c83b6 EB |
9153 | /* Annotate the GNAT node if present. */ |
9154 | if (Present (gnat_variant)) | |
9155 | Set_Present_Expr (gnat_variant, annotate_value (qual)); | |
9156 | ||
95c1c4bb EB |
9157 | /* Recurse on the variant subpart of the variant, if any. */ |
9158 | variant_subpart = get_variant_part (variant_type); | |
9159 | if (variant_subpart) | |
036c83b6 EB |
9160 | gnu_list |
9161 | = build_variant_list (TREE_TYPE (variant_subpart), | |
9162 | Present (gnat_variant) | |
9163 | ? Variant_Part | |
9164 | (Component_List (gnat_variant)) | |
9165 | : Empty, | |
9166 | subst_list, | |
9167 | gnu_list); | |
95c1c4bb EB |
9168 | |
9169 | /* If the new qualifier is unconditionally true, the subsequent | |
9170 | variants cannot be accessed. */ | |
9171 | if (integer_onep (qual)) | |
9172 | break; | |
9173 | } | |
a1ab4c31 AC |
9174 | } |
9175 | ||
f54ee980 | 9176 | return gnu_list; |
a1ab4c31 | 9177 | } |
ce2d0ce2 | 9178 | |
875bdbe2 | 9179 | /* If SIZE has overflowed, return the maximum valid size, which is the upper |
88795e14 EB |
9180 | bound of the signed sizetype in bits, rounded down to ALIGN. Otherwise |
9181 | return SIZE unmodified. */ | |
875bdbe2 EB |
9182 | |
9183 | static tree | |
88795e14 | 9184 | maybe_saturate_size (tree size, unsigned int align) |
875bdbe2 EB |
9185 | { |
9186 | if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size)) | |
88795e14 EB |
9187 | { |
9188 | size | |
9189 | = size_binop (MULT_EXPR, | |
9190 | fold_convert (bitsizetype, TYPE_MAX_VALUE (ssizetype)), | |
9191 | build_int_cst (bitsizetype, BITS_PER_UNIT)); | |
9192 | size = round_down (size, align); | |
9193 | } | |
9194 | ||
875bdbe2 EB |
9195 | return size; |
9196 | } | |
9197 | ||
a1ab4c31 | 9198 | /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE |
0d853156 EB |
9199 | corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST |
9200 | corresponding to its value. Otherwise, return NULL_TREE. KIND is set to | |
9201 | VAR_DECL if we are specifying the size of an object, TYPE_DECL for the | |
9202 | size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is | |
9203 | true if we are being called to process the Component_Size of GNAT_OBJECT; | |
9204 | this is used only for error messages. ZERO_OK is true if a size of zero | |
9205 | is permitted; if ZERO_OK is false, it means that a size of zero should be | |
a517d6c1 | 9206 | treated as an unspecified size. S1 and S2 are used for error messages. */ |
a1ab4c31 AC |
9207 | |
9208 | static tree | |
9209 | validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object, | |
a517d6c1 EB |
9210 | enum tree_code kind, bool component_p, bool zero_ok, |
9211 | const char *s1, const char *s2) | |
a1ab4c31 AC |
9212 | { |
9213 | Node_Id gnat_error_node; | |
8623afc4 | 9214 | tree old_size, size; |
a1ab4c31 | 9215 | |
8ff6c664 EB |
9216 | /* Return 0 if no size was specified. */ |
9217 | if (uint_size == No_Uint) | |
9218 | return NULL_TREE; | |
a1ab4c31 | 9219 | |
728936bb EB |
9220 | /* Ignore a negative size since that corresponds to our back-annotation. */ |
9221 | if (UI_Lt (uint_size, Uint_0)) | |
9222 | return NULL_TREE; | |
9223 | ||
0d853156 | 9224 | /* Find the node to use for error messages. */ |
a1ab4c31 AC |
9225 | if ((Ekind (gnat_object) == E_Component |
9226 | || Ekind (gnat_object) == E_Discriminant) | |
9227 | && Present (Component_Clause (gnat_object))) | |
9228 | gnat_error_node = Last_Bit (Component_Clause (gnat_object)); | |
9229 | else if (Present (Size_Clause (gnat_object))) | |
9230 | gnat_error_node = Expression (Size_Clause (gnat_object)); | |
3a4425fd EB |
9231 | else if (Has_Object_Size_Clause (gnat_object)) |
9232 | gnat_error_node = Expression (Object_Size_Clause (gnat_object)); | |
a1ab4c31 AC |
9233 | else |
9234 | gnat_error_node = gnat_object; | |
9235 | ||
0d853156 EB |
9236 | /* Get the size as an INTEGER_CST. Issue an error if a size was specified |
9237 | but cannot be represented in bitsizetype. */ | |
a1ab4c31 AC |
9238 | size = UI_To_gnu (uint_size, bitsizetype); |
9239 | if (TREE_OVERFLOW (size)) | |
9240 | { | |
8ff6c664 | 9241 | if (component_p) |
0d853156 | 9242 | post_error_ne ("component size for& is too large", gnat_error_node, |
8ff6c664 EB |
9243 | gnat_object); |
9244 | else | |
0d853156 | 9245 | post_error_ne ("size for& is too large", gnat_error_node, |
8ff6c664 | 9246 | gnat_object); |
a1ab4c31 AC |
9247 | return NULL_TREE; |
9248 | } | |
9249 | ||
728936bb EB |
9250 | /* Ignore a zero size if it is not permitted. */ |
9251 | if (!zero_ok && integer_zerop (size)) | |
a1ab4c31 AC |
9252 | return NULL_TREE; |
9253 | ||
9254 | /* The size of objects is always a multiple of a byte. */ | |
9255 | if (kind == VAR_DECL | |
9256 | && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node))) | |
9257 | { | |
9258 | if (component_p) | |
a517d6c1 | 9259 | post_error_ne ("component size for& must be multiple of Storage_Unit", |
a1ab4c31 AC |
9260 | gnat_error_node, gnat_object); |
9261 | else | |
a517d6c1 | 9262 | post_error_ne ("size for& must be multiple of Storage_Unit", |
a1ab4c31 AC |
9263 | gnat_error_node, gnat_object); |
9264 | return NULL_TREE; | |
9265 | } | |
9266 | ||
1e3cabd4 EB |
9267 | /* If this is an integral type or a bit-packed array type, the front-end has |
9268 | already verified the size, so we need not do it again (which would mean | |
9269 | checking against the bounds). However, if this is an aliased object, it | |
9270 | may not be smaller than the type of the object. */ | |
9271 | if ((INTEGRAL_TYPE_P (gnu_type) || BIT_PACKED_ARRAY_TYPE_P (gnu_type)) | |
a1ab4c31 AC |
9272 | && !(kind == VAR_DECL && Is_Aliased (gnat_object))) |
9273 | return size; | |
9274 | ||
0d853156 EB |
9275 | /* If the object is a record that contains a template, add the size of the |
9276 | template to the specified size. */ | |
a1ab4c31 AC |
9277 | if (TREE_CODE (gnu_type) == RECORD_TYPE |
9278 | && TYPE_CONTAINS_TEMPLATE_P (gnu_type)) | |
9279 | size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size); | |
9280 | ||
8623afc4 | 9281 | old_size = (kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type)); |
8ff6c664 | 9282 | |
8623afc4 EB |
9283 | /* If the old size is self-referential, get the maximum size. */ |
9284 | if (CONTAINS_PLACEHOLDER_P (old_size)) | |
9285 | old_size = max_size (old_size, true); | |
a1ab4c31 AC |
9286 | |
9287 | /* If this is an access type or a fat pointer, the minimum size is that given | |
9288 | by the smallest integral mode that's valid for pointers. */ | |
315cff15 | 9289 | if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type)) |
a1ab4c31 | 9290 | { |
e72b0ef4 | 9291 | scalar_int_mode p_mode = NARROWEST_INT_MODE; |
8ff6c664 | 9292 | while (!targetm.valid_pointer_mode (p_mode)) |
490d0f6c | 9293 | p_mode = GET_MODE_WIDER_MODE (p_mode).require (); |
8623afc4 | 9294 | old_size = bitsize_int (GET_MODE_BITSIZE (p_mode)); |
a1ab4c31 AC |
9295 | } |
9296 | ||
0d853156 EB |
9297 | /* Issue an error either if the default size of the object isn't a constant |
9298 | or if the new size is smaller than it. */ | |
8623afc4 | 9299 | if (TREE_CODE (old_size) != INTEGER_CST |
f349a8b5 | 9300 | || (!TREE_OVERFLOW (old_size) && tree_int_cst_lt (size, old_size))) |
a1ab4c31 | 9301 | { |
a517d6c1 EB |
9302 | char buf[128]; |
9303 | const char *s; | |
9304 | ||
f1f5b1fb | 9305 | if (s1 && s2) |
a517d6c1 EB |
9306 | { |
9307 | snprintf (buf, sizeof (buf), s1, s2); | |
9308 | s = buf; | |
9309 | } | |
9310 | else if (component_p) | |
9311 | s = "component size for& too small{, minimum allowed is ^}"; | |
a1ab4c31 | 9312 | else |
a517d6c1 | 9313 | s = "size for& too small{, minimum allowed is ^}"; |
f1f5b1fb | 9314 | |
a517d6c1 EB |
9315 | post_error_ne_tree (s, gnat_error_node, gnat_object, old_size); |
9316 | ||
0d853156 | 9317 | return NULL_TREE; |
a1ab4c31 AC |
9318 | } |
9319 | ||
9320 | return size; | |
9321 | } | |
ce2d0ce2 | 9322 | |
0d853156 EB |
9323 | /* Similarly, but both validate and process a value of RM size. This routine |
9324 | is only called for types. */ | |
a1ab4c31 AC |
9325 | |
9326 | static void | |
9327 | set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity) | |
9328 | { | |
8ff6c664 EB |
9329 | Node_Id gnat_attr_node; |
9330 | tree old_size, size; | |
9331 | ||
9332 | /* Do nothing if no size was specified. */ | |
9333 | if (uint_size == No_Uint) | |
9334 | return; | |
9335 | ||
e63eb26d EB |
9336 | /* Only issue an error if a Value_Size clause was explicitly given for the |
9337 | entity; otherwise, we'd be duplicating an error on the Size clause. */ | |
8ff6c664 | 9338 | gnat_attr_node |
a1ab4c31 | 9339 | = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size); |
e63eb26d EB |
9340 | if (Present (gnat_attr_node) && Entity (gnat_attr_node) != gnat_entity) |
9341 | gnat_attr_node = Empty; | |
a1ab4c31 | 9342 | |
0d853156 EB |
9343 | /* Get the size as an INTEGER_CST. Issue an error if a size was specified |
9344 | but cannot be represented in bitsizetype. */ | |
a1ab4c31 AC |
9345 | size = UI_To_gnu (uint_size, bitsizetype); |
9346 | if (TREE_OVERFLOW (size)) | |
9347 | { | |
9348 | if (Present (gnat_attr_node)) | |
0d853156 | 9349 | post_error_ne ("Value_Size for& is too large", gnat_attr_node, |
a1ab4c31 | 9350 | gnat_entity); |
a1ab4c31 AC |
9351 | return; |
9352 | } | |
9353 | ||
728936bb EB |
9354 | /* Ignore a zero size unless a Value_Size clause exists, or a size clause |
9355 | exists, or this is an integer type, in which case the front-end will | |
9356 | have always set it. */ | |
9357 | if (No (gnat_attr_node) | |
9358 | && integer_zerop (size) | |
9359 | && !Has_Size_Clause (gnat_entity) | |
9360 | && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)) | |
a1ab4c31 AC |
9361 | return; |
9362 | ||
8ff6c664 EB |
9363 | old_size = rm_size (gnu_type); |
9364 | ||
a1ab4c31 AC |
9365 | /* If the old size is self-referential, get the maximum size. */ |
9366 | if (CONTAINS_PLACEHOLDER_P (old_size)) | |
9367 | old_size = max_size (old_size, true); | |
9368 | ||
0d853156 EB |
9369 | /* Issue an error either if the old size of the object isn't a constant or |
9370 | if the new size is smaller than it. The front-end has already verified | |
1e3cabd4 | 9371 | this for scalar and bit-packed array types. */ |
a1ab4c31 AC |
9372 | if (TREE_CODE (old_size) != INTEGER_CST |
9373 | || TREE_OVERFLOW (old_size) | |
03049a4e | 9374 | || (AGGREGATE_TYPE_P (gnu_type) |
1e3cabd4 | 9375 | && !BIT_PACKED_ARRAY_TYPE_P (gnu_type) |
315cff15 | 9376 | && !(TYPE_IS_PADDING_P (gnu_type) |
1e3cabd4 | 9377 | && BIT_PACKED_ARRAY_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_type)))) |
03049a4e | 9378 | && tree_int_cst_lt (size, old_size))) |
a1ab4c31 AC |
9379 | { |
9380 | if (Present (gnat_attr_node)) | |
9381 | post_error_ne_tree | |
9382 | ("Value_Size for& too small{, minimum allowed is ^}", | |
9383 | gnat_attr_node, gnat_entity, old_size); | |
a1ab4c31 AC |
9384 | return; |
9385 | } | |
9386 | ||
e6e15ec9 | 9387 | /* Otherwise, set the RM size proper for integral types... */ |
b4680ca1 EB |
9388 | if ((TREE_CODE (gnu_type) == INTEGER_TYPE |
9389 | && Is_Discrete_Or_Fixed_Point_Type (gnat_entity)) | |
9390 | || (TREE_CODE (gnu_type) == ENUMERAL_TYPE | |
9391 | || TREE_CODE (gnu_type) == BOOLEAN_TYPE)) | |
84fb43a1 | 9392 | SET_TYPE_RM_SIZE (gnu_type, size); |
b4680ca1 EB |
9393 | |
9394 | /* ...or the Ada size for record and union types. */ | |
e1e5852c | 9395 | else if (RECORD_OR_UNION_TYPE_P (gnu_type) |
315cff15 | 9396 | && !TYPE_FAT_POINTER_P (gnu_type)) |
a1ab4c31 AC |
9397 | SET_TYPE_ADA_SIZE (gnu_type, size); |
9398 | } | |
ce2d0ce2 | 9399 | |
a1ab4c31 AC |
9400 | /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY, |
9401 | a type or object whose present alignment is ALIGN. If this alignment is | |
9402 | valid, return it. Otherwise, give an error and return ALIGN. */ | |
9403 | ||
9404 | static unsigned int | |
9405 | validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align) | |
9406 | { | |
9407 | unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment (); | |
9408 | unsigned int new_align; | |
9409 | Node_Id gnat_error_node; | |
9410 | ||
9411 | /* Don't worry about checking alignment if alignment was not specified | |
9412 | by the source program and we already posted an error for this entity. */ | |
9413 | if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity)) | |
9414 | return align; | |
9415 | ||
ec88784d AC |
9416 | /* Post the error on the alignment clause if any. Note, for the implicit |
9417 | base type of an array type, the alignment clause is on the first | |
9418 | subtype. */ | |
a1ab4c31 AC |
9419 | if (Present (Alignment_Clause (gnat_entity))) |
9420 | gnat_error_node = Expression (Alignment_Clause (gnat_entity)); | |
ec88784d AC |
9421 | |
9422 | else if (Is_Itype (gnat_entity) | |
9423 | && Is_Array_Type (gnat_entity) | |
9424 | && Etype (gnat_entity) == gnat_entity | |
9425 | && Present (Alignment_Clause (First_Subtype (gnat_entity)))) | |
9426 | gnat_error_node = | |
9427 | Expression (Alignment_Clause (First_Subtype (gnat_entity))); | |
9428 | ||
a1ab4c31 AC |
9429 | else |
9430 | gnat_error_node = gnat_entity; | |
9431 | ||
9432 | /* Within GCC, an alignment is an integer, so we must make sure a value is | |
9433 | specified that fits in that range. Also, there is an upper bound to | |
9434 | alignments we can support/allow. */ | |
9435 | if (!UI_Is_In_Int_Range (alignment) | |
9436 | || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment)) | |
9437 | post_error_ne_num ("largest supported alignment for& is ^", | |
9438 | gnat_error_node, gnat_entity, max_allowed_alignment); | |
9439 | else if (!(Present (Alignment_Clause (gnat_entity)) | |
9440 | && From_At_Mod (Alignment_Clause (gnat_entity))) | |
9441 | && new_align * BITS_PER_UNIT < align) | |
caa9d12a EB |
9442 | { |
9443 | unsigned int double_align; | |
9444 | bool is_capped_double, align_clause; | |
9445 | ||
9446 | /* If the default alignment of "double" or larger scalar types is | |
9447 | specifically capped and the new alignment is above the cap, do | |
9448 | not post an error and change the alignment only if there is an | |
9449 | alignment clause; this makes it possible to have the associated | |
9450 | GCC type overaligned by default for performance reasons. */ | |
9451 | if ((double_align = double_float_alignment) > 0) | |
9452 | { | |
9453 | Entity_Id gnat_type | |
9454 | = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity); | |
9455 | is_capped_double | |
9456 | = is_double_float_or_array (gnat_type, &align_clause); | |
9457 | } | |
9458 | else if ((double_align = double_scalar_alignment) > 0) | |
9459 | { | |
9460 | Entity_Id gnat_type | |
9461 | = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity); | |
9462 | is_capped_double | |
9463 | = is_double_scalar_or_array (gnat_type, &align_clause); | |
9464 | } | |
9465 | else | |
9466 | is_capped_double = align_clause = false; | |
9467 | ||
9468 | if (is_capped_double && new_align >= double_align) | |
9469 | { | |
9470 | if (align_clause) | |
9471 | align = new_align * BITS_PER_UNIT; | |
9472 | } | |
9473 | else | |
9474 | { | |
9475 | if (is_capped_double) | |
9476 | align = double_align * BITS_PER_UNIT; | |
9477 | ||
9478 | post_error_ne_num ("alignment for& must be at least ^", | |
9479 | gnat_error_node, gnat_entity, | |
9480 | align / BITS_PER_UNIT); | |
9481 | } | |
9482 | } | |
a1ab4c31 AC |
9483 | else |
9484 | { | |
9485 | new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1); | |
9486 | if (new_align > align) | |
9487 | align = new_align; | |
9488 | } | |
9489 | ||
9490 | return align; | |
9491 | } | |
ce2d0ce2 | 9492 | |
5ea133c6 EB |
9493 | /* Promote the alignment of GNU_TYPE for an object with GNU_SIZE corresponding |
9494 | to GNAT_ENTITY. Return a positive value on success or zero on failure. */ | |
89ec98ed EB |
9495 | |
9496 | static unsigned int | |
5ea133c6 | 9497 | promote_object_alignment (tree gnu_type, tree gnu_size, Entity_Id gnat_entity) |
89ec98ed EB |
9498 | { |
9499 | unsigned int align, size_cap, align_cap; | |
9500 | ||
9501 | /* No point in promoting the alignment if this doesn't prevent BLKmode access | |
9502 | to the object, in particular block copy, as this will for example disable | |
9503 | the NRV optimization for it. No point in jumping through all the hoops | |
9504 | needed in order to support BIGGEST_ALIGNMENT if we don't really have to. | |
9505 | So we cap to the smallest alignment that corresponds to a known efficient | |
b120ca61 EB |
9506 | memory access pattern, except for a full access entity. */ |
9507 | if (Is_Full_Access (gnat_entity)) | |
89ec98ed EB |
9508 | { |
9509 | size_cap = UINT_MAX; | |
9510 | align_cap = BIGGEST_ALIGNMENT; | |
9511 | } | |
9512 | else | |
9513 | { | |
9514 | size_cap = MAX_FIXED_MODE_SIZE; | |
9515 | align_cap = get_mode_alignment (ptr_mode); | |
9516 | } | |
9517 | ||
5ea133c6 EB |
9518 | if (!gnu_size) |
9519 | gnu_size = TYPE_SIZE (gnu_type); | |
9520 | ||
89ec98ed | 9521 | /* Do the promotion within the above limits. */ |
5ea133c6 EB |
9522 | if (!tree_fits_uhwi_p (gnu_size) |
9523 | || compare_tree_int (gnu_size, size_cap) > 0) | |
89ec98ed | 9524 | align = 0; |
5ea133c6 | 9525 | else if (compare_tree_int (gnu_size, align_cap) > 0) |
89ec98ed EB |
9526 | align = align_cap; |
9527 | else | |
5ea133c6 | 9528 | align = ceil_pow2 (tree_to_uhwi (gnu_size)); |
89ec98ed EB |
9529 | |
9530 | /* But make sure not to under-align the object. */ | |
9531 | if (align <= TYPE_ALIGN (gnu_type)) | |
9532 | align = 0; | |
9533 | ||
9534 | /* And honor the minimum valid atomic alignment, if any. */ | |
9535 | #ifdef MINIMUM_ATOMIC_ALIGNMENT | |
9536 | else if (align < MINIMUM_ATOMIC_ALIGNMENT) | |
9537 | align = MINIMUM_ATOMIC_ALIGNMENT; | |
9538 | #endif | |
9539 | ||
9540 | return align; | |
9541 | } | |
ce2d0ce2 | 9542 | |
86a8ba5b EB |
9543 | /* Verify that TYPE is something we can implement atomically. If not, issue |
9544 | an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to | |
9545 | process a component type. */ | |
a1ab4c31 AC |
9546 | |
9547 | static void | |
86a8ba5b | 9548 | check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p) |
a1ab4c31 AC |
9549 | { |
9550 | Node_Id gnat_error_point = gnat_entity; | |
9551 | Node_Id gnat_node; | |
ef4bddc2 | 9552 | machine_mode mode; |
86a8ba5b | 9553 | enum mode_class mclass; |
a1ab4c31 AC |
9554 | unsigned int align; |
9555 | tree size; | |
9556 | ||
86a8ba5b EB |
9557 | /* If this is an anonymous base type, nothing to check, the error will be |
9558 | reported on the source type if need be. */ | |
9559 | if (!Comes_From_Source (gnat_entity)) | |
9560 | return; | |
a1ab4c31 | 9561 | |
86a8ba5b EB |
9562 | mode = TYPE_MODE (type); |
9563 | mclass = GET_MODE_CLASS (mode); | |
9564 | align = TYPE_ALIGN (type); | |
9565 | size = TYPE_SIZE (type); | |
9566 | ||
9567 | /* Consider all aligned floating-point types atomic and any aligned types | |
9568 | that are represented by integers no wider than a machine word. */ | |
b0567726 | 9569 | scalar_int_mode int_mode; |
86a8ba5b | 9570 | if ((mclass == MODE_FLOAT |
b0567726 RS |
9571 | || (is_a <scalar_int_mode> (mode, &int_mode) |
9572 | && GET_MODE_BITSIZE (int_mode) <= BITS_PER_WORD)) | |
86a8ba5b | 9573 | && align >= GET_MODE_ALIGNMENT (mode)) |
a1ab4c31 AC |
9574 | return; |
9575 | ||
86a8ba5b EB |
9576 | /* For the moment, also allow anything that has an alignment equal to its |
9577 | size and which is smaller than a word. */ | |
9578 | if (size | |
9579 | && TREE_CODE (size) == INTEGER_CST | |
a1ab4c31 AC |
9580 | && compare_tree_int (size, align) == 0 |
9581 | && align <= BITS_PER_WORD) | |
9582 | return; | |
9583 | ||
86a8ba5b EB |
9584 | for (gnat_node = First_Rep_Item (gnat_entity); |
9585 | Present (gnat_node); | |
a1ab4c31 | 9586 | gnat_node = Next_Rep_Item (gnat_node)) |
86a8ba5b EB |
9587 | if (Nkind (gnat_node) == N_Pragma) |
9588 | { | |
9589 | unsigned char pragma_id | |
9590 | = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node))); | |
9591 | ||
9592 | if ((pragma_id == Pragma_Atomic && !component_p) | |
9593 | || (pragma_id == Pragma_Atomic_Components && component_p)) | |
9594 | { | |
9595 | gnat_error_point = First (Pragma_Argument_Associations (gnat_node)); | |
9596 | break; | |
9597 | } | |
9598 | } | |
a1ab4c31 | 9599 | |
86a8ba5b | 9600 | if (component_p) |
a1ab4c31 AC |
9601 | post_error_ne ("atomic access to component of & cannot be guaranteed", |
9602 | gnat_error_point, gnat_entity); | |
f797c2b7 EB |
9603 | else if (Is_Volatile_Full_Access (gnat_entity)) |
9604 | post_error_ne ("volatile full access to & cannot be guaranteed", | |
9605 | gnat_error_point, gnat_entity); | |
a1ab4c31 AC |
9606 | else |
9607 | post_error_ne ("atomic access to & cannot be guaranteed", | |
9608 | gnat_error_point, gnat_entity); | |
9609 | } | |
ce2d0ce2 | 9610 | |
a40970cf EB |
9611 | /* Return true if TYPE is suitable for a type-generic atomic builtin. */ |
9612 | ||
9613 | static bool | |
9614 | type_for_atomic_builtin_p (tree type) | |
9615 | { | |
9616 | const enum machine_mode mode = TYPE_MODE (type); | |
9617 | if (GET_MODE_CLASS (mode) == MODE_FLOAT) | |
9618 | return true; | |
9619 | ||
9620 | scalar_int_mode imode; | |
9621 | if (is_a <scalar_int_mode> (mode, &imode) && GET_MODE_SIZE (imode) <= 16) | |
9622 | return true; | |
9623 | ||
9624 | return false; | |
9625 | } | |
9626 | ||
9627 | /* Return the GCC atomic builtin based on CODE and sized for TYPE. */ | |
9628 | ||
9629 | static tree | |
9630 | resolve_atomic_builtin (enum built_in_function code, tree type) | |
9631 | { | |
9632 | const unsigned int size = resolve_atomic_size (type); | |
9633 | code = (enum built_in_function) ((int) code + exact_log2 (size) + 1); | |
9634 | ||
9635 | return builtin_decl_implicit (code); | |
9636 | } | |
9637 | ||
1515785d OH |
9638 | /* Helper for intrin_profiles_compatible_p, to perform compatibility checks |
9639 | on the Ada/builtin argument lists for the INB binding. */ | |
9640 | ||
9641 | static bool | |
26864014 | 9642 | intrin_arglists_compatible_p (const intrin_binding_t *inb) |
a1ab4c31 | 9643 | { |
d7d058c5 NF |
9644 | function_args_iterator ada_iter, btin_iter; |
9645 | ||
9646 | function_args_iter_init (&ada_iter, inb->ada_fntype); | |
9647 | function_args_iter_init (&btin_iter, inb->btin_fntype); | |
1515785d OH |
9648 | |
9649 | /* Sequence position of the last argument we checked. */ | |
9650 | int argpos = 0; | |
9651 | ||
7c775aca | 9652 | while (true) |
1515785d | 9653 | { |
d7d058c5 NF |
9654 | tree ada_type = function_args_iter_cond (&ada_iter); |
9655 | tree btin_type = function_args_iter_cond (&btin_iter); | |
9656 | ||
9657 | /* If we've exhausted both lists simultaneously, we're done. */ | |
7c775aca | 9658 | if (!ada_type && !btin_type) |
d7d058c5 | 9659 | break; |
1515785d | 9660 | |
eabf2b44 EB |
9661 | /* If the internal builtin uses a variable list, accept anything. */ |
9662 | if (!btin_type) | |
9663 | break; | |
1515785d | 9664 | |
1515785d | 9665 | /* If we're done with the Ada args and not with the internal builtin |
bb511fbd | 9666 | args, or the other way around, complain. */ |
26864014 | 9667 | if (ada_type == void_type_node && btin_type != void_type_node) |
1515785d | 9668 | { |
26864014 | 9669 | post_error ("??Ada parameter list too short!", inb->gnat_entity); |
1515785d OH |
9670 | return false; |
9671 | } | |
9672 | ||
26864014 | 9673 | if (btin_type == void_type_node && ada_type != void_type_node) |
1515785d | 9674 | { |
26864014 | 9675 | post_error_ne_num ("??Ada parameter list too long ('> ^)!", |
bb511fbd OH |
9676 | inb->gnat_entity, inb->gnat_entity, argpos); |
9677 | return false; | |
1515785d OH |
9678 | } |
9679 | ||
9680 | /* Otherwise, check that types match for the current argument. */ | |
fad54055 EB |
9681 | argpos++; |
9682 | if (!types_compatible_p (ada_type, btin_type)) | |
1515785d | 9683 | { |
26864014 EB |
9684 | /* For vector builtins, issue an error to avoid an ICE. */ |
9685 | if (VECTOR_TYPE_P (btin_type)) | |
9686 | post_error_ne_num | |
9687 | ("intrinsic binding type mismatch on parameter ^", | |
9688 | inb->gnat_entity, inb->gnat_entity, argpos); | |
9689 | else | |
9690 | post_error_ne_num | |
9691 | ("??intrinsic binding type mismatch on parameter ^!", | |
9692 | inb->gnat_entity, inb->gnat_entity, argpos); | |
1515785d OH |
9693 | return false; |
9694 | } | |
9695 | ||
f620bd21 | 9696 | |
d7d058c5 NF |
9697 | function_args_iter_next (&ada_iter); |
9698 | function_args_iter_next (&btin_iter); | |
1515785d OH |
9699 | } |
9700 | ||
9701 | return true; | |
9702 | } | |
9703 | ||
9704 | /* Helper for intrin_profiles_compatible_p, to perform compatibility checks | |
9705 | on the Ada/builtin return values for the INB binding. */ | |
9706 | ||
9707 | static bool | |
26864014 | 9708 | intrin_return_compatible_p (const intrin_binding_t *inb) |
1515785d OH |
9709 | { |
9710 | tree ada_return_type = TREE_TYPE (inb->ada_fntype); | |
9711 | tree btin_return_type = TREE_TYPE (inb->btin_fntype); | |
9712 | ||
bb511fbd | 9713 | /* Accept function imported as procedure, common and convenient. */ |
26864014 | 9714 | if (VOID_TYPE_P (ada_return_type) && !VOID_TYPE_P (btin_return_type)) |
bb511fbd | 9715 | return true; |
1515785d | 9716 | |
bb511fbd OH |
9717 | /* Check return types compatibility otherwise. Note that this |
9718 | handles void/void as well. */ | |
fad54055 | 9719 | if (!types_compatible_p (btin_return_type, ada_return_type)) |
1515785d | 9720 | { |
26864014 EB |
9721 | /* For vector builtins, issue an error to avoid an ICE. */ |
9722 | if (VECTOR_TYPE_P (btin_return_type)) | |
9723 | post_error ("intrinsic binding type mismatch on result", | |
9724 | inb->gnat_entity); | |
9725 | else | |
9726 | post_error ("??intrinsic binding type mismatch on result", | |
9727 | inb->gnat_entity); | |
1515785d OH |
9728 | return false; |
9729 | } | |
9730 | ||
9731 | return true; | |
9732 | } | |
9733 | ||
9734 | /* Check and return whether the Ada and gcc builtin profiles bound by INB are | |
9735 | compatible. Issue relevant warnings when they are not. | |
9736 | ||
9737 | This is intended as a light check to diagnose the most obvious cases, not | |
308e6f3a | 9738 | as a full fledged type compatibility predicate. It is the programmer's |
1515785d OH |
9739 | responsibility to ensure correctness of the Ada declarations in Imports, |
9740 | especially when binding straight to a compiler internal. */ | |
9741 | ||
9742 | static bool | |
26864014 | 9743 | intrin_profiles_compatible_p (const intrin_binding_t *inb) |
1515785d OH |
9744 | { |
9745 | /* Check compatibility on return values and argument lists, each responsible | |
9746 | for posting warnings as appropriate. Ensure use of the proper sloc for | |
9747 | this purpose. */ | |
9748 | ||
9749 | bool arglists_compatible_p, return_compatible_p; | |
9750 | location_t saved_location = input_location; | |
9751 | ||
9752 | Sloc_to_locus (Sloc (inb->gnat_entity), &input_location); | |
a1ab4c31 | 9753 | |
1515785d OH |
9754 | return_compatible_p = intrin_return_compatible_p (inb); |
9755 | arglists_compatible_p = intrin_arglists_compatible_p (inb); | |
a1ab4c31 | 9756 | |
1515785d | 9757 | input_location = saved_location; |
a1ab4c31 | 9758 | |
1515785d | 9759 | return return_compatible_p && arglists_compatible_p; |
a1ab4c31 | 9760 | } |
ce2d0ce2 | 9761 | |
95c1c4bb EB |
9762 | /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type |
9763 | and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the | |
9764 | specified size for this field. POS_LIST is a position list describing | |
9765 | the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied | |
9766 | to this layout. */ | |
9767 | ||
9768 | static tree | |
9769 | create_field_decl_from (tree old_field, tree field_type, tree record_type, | |
e3554601 | 9770 | tree size, tree pos_list, |
9771b263 | 9771 | vec<subst_pair> subst_list) |
95c1c4bb EB |
9772 | { |
9773 | tree t = TREE_VALUE (purpose_member (old_field, pos_list)); | |
9774 | tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2); | |
ae7e9ddd | 9775 | unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1)); |
95c1c4bb | 9776 | tree new_pos, new_field; |
f54ee980 | 9777 | unsigned int i; |
e3554601 | 9778 | subst_pair *s; |
95c1c4bb EB |
9779 | |
9780 | if (CONTAINS_PLACEHOLDER_P (pos)) | |
9771b263 | 9781 | FOR_EACH_VEC_ELT (subst_list, i, s) |
e3554601 | 9782 | pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement); |
95c1c4bb EB |
9783 | |
9784 | /* If the position is now a constant, we can set it as the position of the | |
9785 | field when we make it. Otherwise, we need to deal with it specially. */ | |
9786 | if (TREE_CONSTANT (pos)) | |
9787 | new_pos = bit_from_pos (pos, bitpos); | |
9788 | else | |
9789 | new_pos = NULL_TREE; | |
9790 | ||
9791 | new_field | |
9792 | = create_field_decl (DECL_NAME (old_field), field_type, record_type, | |
da01bfee | 9793 | size, new_pos, DECL_PACKED (old_field), |
95c1c4bb EB |
9794 | !DECL_NONADDRESSABLE_P (old_field)); |
9795 | ||
9796 | if (!new_pos) | |
9797 | { | |
9798 | normalize_offset (&pos, &bitpos, offset_align); | |
cb27986c EB |
9799 | /* Finalize the position. */ |
9800 | DECL_FIELD_OFFSET (new_field) = variable_size (pos); | |
95c1c4bb EB |
9801 | DECL_FIELD_BIT_OFFSET (new_field) = bitpos; |
9802 | SET_DECL_OFFSET_ALIGN (new_field, offset_align); | |
9803 | DECL_SIZE (new_field) = size; | |
9804 | DECL_SIZE_UNIT (new_field) | |
9805 | = convert (sizetype, | |
9806 | size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node)); | |
9807 | layout_decl (new_field, DECL_OFFSET_ALIGN (new_field)); | |
9808 | } | |
9809 | ||
9810 | DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field); | |
cb3d597d | 9811 | SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field); |
95c1c4bb EB |
9812 | DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field); |
9813 | TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field); | |
9814 | ||
9815 | return new_field; | |
9816 | } | |
9817 | ||
b1a785fb EB |
9818 | /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero, |
9819 | it is the minimal size the REP_PART must have. */ | |
9820 | ||
9821 | static tree | |
9822 | create_rep_part (tree rep_type, tree record_type, tree min_size) | |
9823 | { | |
9824 | tree field; | |
9825 | ||
9826 | if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size)) | |
9827 | min_size = NULL_TREE; | |
9828 | ||
9829 | field = create_field_decl (get_identifier ("REP"), rep_type, record_type, | |
9580628d | 9830 | min_size, NULL_TREE, 0, 1); |
b1a785fb EB |
9831 | DECL_INTERNAL_P (field) = 1; |
9832 | ||
9833 | return field; | |
9834 | } | |
9835 | ||
95c1c4bb EB |
9836 | /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */ |
9837 | ||
9838 | static tree | |
9839 | get_rep_part (tree record_type) | |
9840 | { | |
9841 | tree field = TYPE_FIELDS (record_type); | |
9842 | ||
9843 | /* The REP part is the first field, internal, another record, and its name | |
b1a785fb | 9844 | starts with an 'R'. */ |
638eeae8 EB |
9845 | if (field |
9846 | && DECL_INTERNAL_P (field) | |
95c1c4bb | 9847 | && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
b1a785fb | 9848 | && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R') |
95c1c4bb EB |
9849 | return field; |
9850 | ||
9851 | return NULL_TREE; | |
9852 | } | |
9853 | ||
9854 | /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */ | |
9855 | ||
805e60a0 | 9856 | tree |
95c1c4bb EB |
9857 | get_variant_part (tree record_type) |
9858 | { | |
9859 | tree field; | |
9860 | ||
9861 | /* The variant part is the only internal field that is a qualified union. */ | |
910ad8de | 9862 | for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field)) |
95c1c4bb EB |
9863 | if (DECL_INTERNAL_P (field) |
9864 | && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE) | |
9865 | return field; | |
9866 | ||
9867 | return NULL_TREE; | |
9868 | } | |
9869 | ||
9870 | /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is | |
9871 | the list of variants to be used and RECORD_TYPE is the type of the parent. | |
9872 | POS_LIST is a position list describing the layout of fields present in | |
9873 | OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this | |
05dbb83f | 9874 | layout. DEBUG_INFO_P is true if we need to write debug information. */ |
95c1c4bb EB |
9875 | |
9876 | static tree | |
fb7fb701 | 9877 | create_variant_part_from (tree old_variant_part, |
9771b263 | 9878 | vec<variant_desc> variant_list, |
e3554601 | 9879 | tree record_type, tree pos_list, |
05dbb83f AC |
9880 | vec<subst_pair> subst_list, |
9881 | bool debug_info_p) | |
95c1c4bb EB |
9882 | { |
9883 | tree offset = DECL_FIELD_OFFSET (old_variant_part); | |
95c1c4bb | 9884 | tree old_union_type = TREE_TYPE (old_variant_part); |
fb7fb701 | 9885 | tree new_union_type, new_variant_part; |
95c1c4bb | 9886 | tree union_field_list = NULL_TREE; |
fb7fb701 | 9887 | variant_desc *v; |
f54ee980 | 9888 | unsigned int i; |
95c1c4bb EB |
9889 | |
9890 | /* First create the type of the variant part from that of the old one. */ | |
9891 | new_union_type = make_node (QUAL_UNION_TYPE); | |
82ea8185 EB |
9892 | TYPE_NAME (new_union_type) |
9893 | = concat_name (TYPE_NAME (record_type), | |
9894 | IDENTIFIER_POINTER (DECL_NAME (old_variant_part))); | |
95c1c4bb EB |
9895 | |
9896 | /* If the position of the variant part is constant, subtract it from the | |
9897 | size of the type of the parent to get the new size. This manual CSE | |
9898 | reduces the code size when not optimizing. */ | |
05dbb83f AC |
9899 | if (TREE_CODE (offset) == INTEGER_CST |
9900 | && TYPE_SIZE (record_type) | |
9901 | && TYPE_SIZE_UNIT (record_type)) | |
95c1c4bb | 9902 | { |
da01bfee | 9903 | tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part); |
95c1c4bb EB |
9904 | tree first_bit = bit_from_pos (offset, bitpos); |
9905 | TYPE_SIZE (new_union_type) | |
9906 | = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit); | |
9907 | TYPE_SIZE_UNIT (new_union_type) | |
9908 | = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type), | |
9909 | byte_from_pos (offset, bitpos)); | |
9910 | SET_TYPE_ADA_SIZE (new_union_type, | |
9911 | size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type), | |
9912 | first_bit)); | |
fe37c7af | 9913 | SET_TYPE_ALIGN (new_union_type, TYPE_ALIGN (old_union_type)); |
95c1c4bb EB |
9914 | relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY); |
9915 | } | |
9916 | else | |
9917 | copy_and_substitute_in_size (new_union_type, old_union_type, subst_list); | |
9918 | ||
9919 | /* Now finish up the new variants and populate the union type. */ | |
9771b263 | 9920 | FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v) |
95c1c4bb | 9921 | { |
fb7fb701 | 9922 | tree old_field = v->field, new_field; |
95c1c4bb EB |
9923 | tree old_variant, old_variant_subpart, new_variant, field_list; |
9924 | ||
9925 | /* Skip variants that don't belong to this nesting level. */ | |
9926 | if (DECL_CONTEXT (old_field) != old_union_type) | |
9927 | continue; | |
9928 | ||
9929 | /* Retrieve the list of fields already added to the new variant. */ | |
82ea8185 | 9930 | new_variant = v->new_type; |
95c1c4bb EB |
9931 | field_list = TYPE_FIELDS (new_variant); |
9932 | ||
9933 | /* If the old variant had a variant subpart, we need to create a new | |
9934 | variant subpart and add it to the field list. */ | |
fb7fb701 | 9935 | old_variant = v->type; |
95c1c4bb EB |
9936 | old_variant_subpart = get_variant_part (old_variant); |
9937 | if (old_variant_subpart) | |
9938 | { | |
9939 | tree new_variant_subpart | |
9940 | = create_variant_part_from (old_variant_subpart, variant_list, | |
05dbb83f AC |
9941 | new_variant, pos_list, subst_list, |
9942 | debug_info_p); | |
910ad8de | 9943 | DECL_CHAIN (new_variant_subpart) = field_list; |
95c1c4bb EB |
9944 | field_list = new_variant_subpart; |
9945 | } | |
9946 | ||
05dbb83f AC |
9947 | /* Finish up the new variant and create the field. */ |
9948 | finish_record_type (new_variant, nreverse (field_list), 2, debug_info_p); | |
05dbb83f AC |
9949 | create_type_decl (TYPE_NAME (new_variant), new_variant, true, |
9950 | debug_info_p, Empty); | |
95c1c4bb EB |
9951 | |
9952 | new_field | |
9953 | = create_field_decl_from (old_field, new_variant, new_union_type, | |
9954 | TYPE_SIZE (new_variant), | |
9955 | pos_list, subst_list); | |
fb7fb701 | 9956 | DECL_QUALIFIER (new_field) = v->qual; |
95c1c4bb | 9957 | DECL_INTERNAL_P (new_field) = 1; |
910ad8de | 9958 | DECL_CHAIN (new_field) = union_field_list; |
95c1c4bb EB |
9959 | union_field_list = new_field; |
9960 | } | |
9961 | ||
05dbb83f AC |
9962 | /* Finish up the union type and create the variant part. Note that we don't |
9963 | reverse the field list because VARIANT_LIST has been traversed in reverse | |
9964 | order. */ | |
9965 | finish_record_type (new_union_type, union_field_list, 2, debug_info_p); | |
05dbb83f AC |
9966 | create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, |
9967 | debug_info_p, Empty); | |
95c1c4bb EB |
9968 | |
9969 | new_variant_part | |
9970 | = create_field_decl_from (old_variant_part, new_union_type, record_type, | |
9971 | TYPE_SIZE (new_union_type), | |
9972 | pos_list, subst_list); | |
9973 | DECL_INTERNAL_P (new_variant_part) = 1; | |
9974 | ||
9975 | /* With multiple discriminants it is possible for an inner variant to be | |
9976 | statically selected while outer ones are not; in this case, the list | |
9977 | of fields of the inner variant is not flattened and we end up with a | |
9978 | qualified union with a single member. Drop the useless container. */ | |
910ad8de | 9979 | if (!DECL_CHAIN (union_field_list)) |
95c1c4bb EB |
9980 | { |
9981 | DECL_CONTEXT (union_field_list) = record_type; | |
9982 | DECL_FIELD_OFFSET (union_field_list) | |
9983 | = DECL_FIELD_OFFSET (new_variant_part); | |
9984 | DECL_FIELD_BIT_OFFSET (union_field_list) | |
9985 | = DECL_FIELD_BIT_OFFSET (new_variant_part); | |
9986 | SET_DECL_OFFSET_ALIGN (union_field_list, | |
9987 | DECL_OFFSET_ALIGN (new_variant_part)); | |
9988 | new_variant_part = union_field_list; | |
9989 | } | |
9990 | ||
9991 | return new_variant_part; | |
9992 | } | |
9993 | ||
9994 | /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE, | |
9995 | which are both RECORD_TYPE, after applying the substitutions described | |
9996 | in SUBST_LIST. */ | |
9997 | ||
9998 | static void | |
e3554601 | 9999 | copy_and_substitute_in_size (tree new_type, tree old_type, |
9771b263 | 10000 | vec<subst_pair> subst_list) |
95c1c4bb | 10001 | { |
f54ee980 | 10002 | unsigned int i; |
e3554601 | 10003 | subst_pair *s; |
95c1c4bb EB |
10004 | |
10005 | TYPE_SIZE (new_type) = TYPE_SIZE (old_type); | |
10006 | TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type); | |
10007 | SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type)); | |
fe37c7af | 10008 | SET_TYPE_ALIGN (new_type, TYPE_ALIGN (old_type)); |
95c1c4bb EB |
10009 | relate_alias_sets (new_type, old_type, ALIAS_SET_COPY); |
10010 | ||
10011 | if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type))) | |
9771b263 | 10012 | FOR_EACH_VEC_ELT (subst_list, i, s) |
95c1c4bb EB |
10013 | TYPE_SIZE (new_type) |
10014 | = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type), | |
e3554601 | 10015 | s->discriminant, s->replacement); |
95c1c4bb EB |
10016 | |
10017 | if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type))) | |
9771b263 | 10018 | FOR_EACH_VEC_ELT (subst_list, i, s) |
95c1c4bb EB |
10019 | TYPE_SIZE_UNIT (new_type) |
10020 | = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type), | |
e3554601 | 10021 | s->discriminant, s->replacement); |
95c1c4bb EB |
10022 | |
10023 | if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type))) | |
9771b263 | 10024 | FOR_EACH_VEC_ELT (subst_list, i, s) |
95c1c4bb EB |
10025 | SET_TYPE_ADA_SIZE |
10026 | (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type), | |
e3554601 | 10027 | s->discriminant, s->replacement)); |
95c1c4bb EB |
10028 | |
10029 | /* Finalize the size. */ | |
10030 | TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type)); | |
10031 | TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type)); | |
10032 | } | |
1eb58520 | 10033 | |
05dbb83f AC |
10034 | /* Return true if DISC is a stored discriminant of RECORD_TYPE. */ |
10035 | ||
10036 | static inline bool | |
10037 | is_stored_discriminant (Entity_Id discr, Entity_Id record_type) | |
10038 | { | |
87eddedc EB |
10039 | if (Is_Unchecked_Union (record_type)) |
10040 | return false; | |
10041 | else if (Is_Tagged_Type (record_type)) | |
05dbb83f AC |
10042 | return No (Corresponding_Discriminant (discr)); |
10043 | else if (Ekind (record_type) == E_Record_Type) | |
10044 | return Original_Record_Component (discr) == discr; | |
10045 | else | |
10046 | return true; | |
10047 | } | |
10048 | ||
10049 | /* Copy the layout from {GNAT,GNU}_OLD_TYPE to {GNAT,GNU}_NEW_TYPE, which are | |
10050 | both record types, after applying the substitutions described in SUBST_LIST. | |
10051 | DEBUG_INFO_P is true if we need to write debug information for NEW_TYPE. */ | |
10052 | ||
10053 | static void | |
10054 | copy_and_substitute_in_layout (Entity_Id gnat_new_type, | |
10055 | Entity_Id gnat_old_type, | |
10056 | tree gnu_new_type, | |
10057 | tree gnu_old_type, | |
036c83b6 | 10058 | vec<subst_pair> subst_list, |
05dbb83f AC |
10059 | bool debug_info_p) |
10060 | { | |
10061 | const bool is_subtype = (Ekind (gnat_new_type) == E_Record_Subtype); | |
10062 | tree gnu_field_list = NULL_TREE; | |
cd8ad459 EB |
10063 | tree gnu_variable_field_list = NULL_TREE; |
10064 | bool selected_variant; | |
05dbb83f AC |
10065 | vec<variant_desc> gnu_variant_list; |
10066 | ||
10067 | /* Look for REP and variant parts in the old type. */ | |
10068 | tree gnu_rep_part = get_rep_part (gnu_old_type); | |
10069 | tree gnu_variant_part = get_variant_part (gnu_old_type); | |
10070 | ||
10071 | /* If there is a variant part, we must compute whether the constraints | |
10072 | statically select a particular variant. If so, we simply drop the | |
10073 | qualified union and flatten the list of fields. Otherwise we will | |
10074 | build a new qualified union for the variants that are still relevant. */ | |
10075 | if (gnu_variant_part) | |
10076 | { | |
036c83b6 | 10077 | const Node_Id gnat_decl = Declaration_Node (gnat_new_type); |
05dbb83f AC |
10078 | variant_desc *v; |
10079 | unsigned int i; | |
10080 | ||
036c83b6 EB |
10081 | gnu_variant_list |
10082 | = build_variant_list (TREE_TYPE (gnu_variant_part), | |
10083 | is_subtype | |
10084 | ? Empty | |
10085 | : Variant_Part | |
10086 | (Component_List (Type_Definition (gnat_decl))), | |
10087 | subst_list, | |
10088 | vNULL); | |
05dbb83f AC |
10089 | |
10090 | /* If all the qualifiers are unconditionally true, the innermost variant | |
10091 | is statically selected. */ | |
10092 | selected_variant = true; | |
10093 | FOR_EACH_VEC_ELT (gnu_variant_list, i, v) | |
10094 | if (!integer_onep (v->qual)) | |
10095 | { | |
10096 | selected_variant = false; | |
10097 | break; | |
10098 | } | |
10099 | ||
10100 | /* Otherwise, create the new variants. */ | |
10101 | if (!selected_variant) | |
10102 | FOR_EACH_VEC_ELT (gnu_variant_list, i, v) | |
10103 | { | |
10104 | tree old_variant = v->type; | |
10105 | tree new_variant = make_node (RECORD_TYPE); | |
10106 | tree suffix | |
10107 | = concat_name (DECL_NAME (gnu_variant_part), | |
10108 | IDENTIFIER_POINTER (DECL_NAME (v->field))); | |
10109 | TYPE_NAME (new_variant) | |
10110 | = concat_name (TYPE_NAME (gnu_new_type), | |
10111 | IDENTIFIER_POINTER (suffix)); | |
10112 | TYPE_REVERSE_STORAGE_ORDER (new_variant) | |
10113 | = TYPE_REVERSE_STORAGE_ORDER (gnu_new_type); | |
036c83b6 | 10114 | copy_and_substitute_in_size (new_variant, old_variant, subst_list); |
05dbb83f AC |
10115 | v->new_type = new_variant; |
10116 | } | |
10117 | } | |
10118 | else | |
10119 | { | |
10120 | gnu_variant_list.create (0); | |
10121 | selected_variant = false; | |
10122 | } | |
10123 | ||
10124 | /* Make a list of fields and their position in the old type. */ | |
10125 | tree gnu_pos_list | |
10126 | = build_position_list (gnu_old_type, | |
10127 | gnu_variant_list.exists () && !selected_variant, | |
10128 | size_zero_node, bitsize_zero_node, | |
10129 | BIGGEST_ALIGNMENT, NULL_TREE); | |
10130 | ||
10131 | /* Now go down every component in the new type and compute its size and | |
10132 | position from those of the component in the old type and the stored | |
10133 | constraints of the new type. */ | |
10134 | Entity_Id gnat_field, gnat_old_field; | |
10135 | for (gnat_field = First_Entity (gnat_new_type); | |
10136 | Present (gnat_field); | |
10137 | gnat_field = Next_Entity (gnat_field)) | |
10138 | if ((Ekind (gnat_field) == E_Component | |
10139 | || (Ekind (gnat_field) == E_Discriminant | |
10140 | && is_stored_discriminant (gnat_field, gnat_new_type))) | |
10141 | && (gnat_old_field = is_subtype | |
10142 | ? Original_Record_Component (gnat_field) | |
10143 | : Corresponding_Record_Component (gnat_field)) | |
10144 | && Underlying_Type (Scope (gnat_old_field)) == gnat_old_type | |
10145 | && present_gnu_tree (gnat_old_field)) | |
10146 | { | |
10147 | Name_Id gnat_name = Chars (gnat_field); | |
10148 | tree gnu_old_field = get_gnu_tree (gnat_old_field); | |
10149 | if (TREE_CODE (gnu_old_field) == COMPONENT_REF) | |
10150 | gnu_old_field = TREE_OPERAND (gnu_old_field, 1); | |
10151 | tree gnu_context = DECL_CONTEXT (gnu_old_field); | |
10152 | tree gnu_field, gnu_field_type, gnu_size, gnu_pos; | |
10153 | tree gnu_cont_type, gnu_last = NULL_TREE; | |
cd8ad459 | 10154 | variant_desc *v = NULL; |
05dbb83f AC |
10155 | |
10156 | /* If the type is the same, retrieve the GCC type from the | |
10157 | old field to take into account possible adjustments. */ | |
10158 | if (Etype (gnat_field) == Etype (gnat_old_field)) | |
10159 | gnu_field_type = TREE_TYPE (gnu_old_field); | |
10160 | else | |
10161 | gnu_field_type = gnat_to_gnu_type (Etype (gnat_field)); | |
10162 | ||
10163 | /* If there was a component clause, the field types must be the same | |
10164 | for the old and new types, so copy the data from the old field to | |
10165 | avoid recomputation here. Also if the field is justified modular | |
10166 | and the optimization in gnat_to_gnu_field was applied. */ | |
10167 | if (Present (Component_Clause (gnat_old_field)) | |
10168 | || (TREE_CODE (gnu_field_type) == RECORD_TYPE | |
10169 | && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type) | |
10170 | && TREE_TYPE (TYPE_FIELDS (gnu_field_type)) | |
10171 | == TREE_TYPE (gnu_old_field))) | |
10172 | { | |
10173 | gnu_size = DECL_SIZE (gnu_old_field); | |
10174 | gnu_field_type = TREE_TYPE (gnu_old_field); | |
10175 | } | |
10176 | ||
10177 | /* If the old field was packed and of constant size, we have to get the | |
10178 | old size here as it might differ from what the Etype conveys and the | |
10179 | latter might overlap with the following field. Try to arrange the | |
10180 | type for possible better packing along the way. */ | |
10181 | else if (DECL_PACKED (gnu_old_field) | |
10182 | && TREE_CODE (DECL_SIZE (gnu_old_field)) == INTEGER_CST) | |
10183 | { | |
10184 | gnu_size = DECL_SIZE (gnu_old_field); | |
10185 | if (RECORD_OR_UNION_TYPE_P (gnu_field_type) | |
10186 | && !TYPE_FAT_POINTER_P (gnu_field_type) | |
10187 | && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))) | |
b1af4cb2 | 10188 | gnu_field_type = make_packable_type (gnu_field_type, true, 0); |
05dbb83f AC |
10189 | } |
10190 | ||
10191 | else | |
10192 | gnu_size = TYPE_SIZE (gnu_field_type); | |
10193 | ||
10194 | /* If the context of the old field is the old type or its REP part, | |
10195 | put the field directly in the new type; otherwise look up the | |
10196 | context in the variant list and put the field either in the new | |
10197 | type if there is a selected variant or in one new variant. */ | |
10198 | if (gnu_context == gnu_old_type | |
10199 | || (gnu_rep_part && gnu_context == TREE_TYPE (gnu_rep_part))) | |
10200 | gnu_cont_type = gnu_new_type; | |
10201 | else | |
10202 | { | |
05dbb83f AC |
10203 | unsigned int i; |
10204 | tree rep_part; | |
10205 | ||
10206 | FOR_EACH_VEC_ELT (gnu_variant_list, i, v) | |
10207 | if (gnu_context == v->type | |
10208 | || ((rep_part = get_rep_part (v->type)) | |
10209 | && gnu_context == TREE_TYPE (rep_part))) | |
10210 | break; | |
10211 | ||
10212 | if (v) | |
10213 | gnu_cont_type = selected_variant ? gnu_new_type : v->new_type; | |
10214 | else | |
cd8ad459 | 10215 | /* The front-end may pass us zombie components if it fails to |
05dbb83f AC |
10216 | recognize that a constrain statically selects a particular |
10217 | variant. Discard them. */ | |
10218 | continue; | |
10219 | } | |
10220 | ||
10221 | /* Now create the new field modeled on the old one. */ | |
10222 | gnu_field | |
10223 | = create_field_decl_from (gnu_old_field, gnu_field_type, | |
10224 | gnu_cont_type, gnu_size, | |
036c83b6 | 10225 | gnu_pos_list, subst_list); |
05dbb83f AC |
10226 | gnu_pos = DECL_FIELD_OFFSET (gnu_field); |
10227 | ||
10228 | /* If the context is a variant, put it in the new variant directly. */ | |
10229 | if (gnu_cont_type != gnu_new_type) | |
10230 | { | |
cd8ad459 EB |
10231 | if (TREE_CODE (gnu_pos) == INTEGER_CST) |
10232 | { | |
10233 | DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type); | |
10234 | TYPE_FIELDS (gnu_cont_type) = gnu_field; | |
10235 | } | |
10236 | else | |
10237 | { | |
10238 | DECL_CHAIN (gnu_field) = v->aux; | |
10239 | v->aux = gnu_field; | |
10240 | } | |
05dbb83f AC |
10241 | } |
10242 | ||
10243 | /* To match the layout crafted in components_to_record, if this is | |
10244 | the _Tag or _Parent field, put it before any other fields. */ | |
10245 | else if (gnat_name == Name_uTag || gnat_name == Name_uParent) | |
10246 | gnu_field_list = chainon (gnu_field_list, gnu_field); | |
10247 | ||
10248 | /* Similarly, if this is the _Controller field, put it before the | |
10249 | other fields except for the _Tag or _Parent field. */ | |
10250 | else if (gnat_name == Name_uController && gnu_last) | |
10251 | { | |
10252 | DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last); | |
10253 | DECL_CHAIN (gnu_last) = gnu_field; | |
10254 | } | |
10255 | ||
10256 | /* Otherwise, put it after the other fields. */ | |
10257 | else | |
10258 | { | |
cd8ad459 EB |
10259 | if (TREE_CODE (gnu_pos) == INTEGER_CST) |
10260 | { | |
10261 | DECL_CHAIN (gnu_field) = gnu_field_list; | |
10262 | gnu_field_list = gnu_field; | |
10263 | if (!gnu_last) | |
10264 | gnu_last = gnu_field; | |
10265 | } | |
10266 | else | |
10267 | { | |
10268 | DECL_CHAIN (gnu_field) = gnu_variable_field_list; | |
10269 | gnu_variable_field_list = gnu_field; | |
10270 | } | |
05dbb83f AC |
10271 | } |
10272 | ||
10273 | /* For a stored discriminant in a derived type, replace the field. */ | |
10274 | if (!is_subtype && Ekind (gnat_field) == E_Discriminant) | |
10275 | { | |
10276 | tree gnu_ref = get_gnu_tree (gnat_field); | |
10277 | TREE_OPERAND (gnu_ref, 1) = gnu_field; | |
10278 | } | |
10279 | else | |
10280 | save_gnu_tree (gnat_field, gnu_field, false); | |
10281 | } | |
10282 | ||
cd8ad459 EB |
10283 | /* Put the fields with fixed position in order of increasing position. */ |
10284 | if (gnu_field_list) | |
10285 | gnu_field_list = reverse_sort_field_list (gnu_field_list); | |
05dbb83f | 10286 | |
cd8ad459 EB |
10287 | /* Put the fields with variable position at the end. */ |
10288 | if (gnu_variable_field_list) | |
10289 | gnu_field_list = chainon (gnu_variable_field_list, gnu_field_list); | |
05dbb83f | 10290 | |
cd8ad459 EB |
10291 | /* If there is a variant list and no selected variant, we need to create the |
10292 | nest of variant parts from the old nest. */ | |
10293 | if (gnu_variant_list.exists () && !selected_variant) | |
10294 | { | |
10295 | variant_desc *v; | |
10296 | unsigned int i; | |
05dbb83f | 10297 | |
cd8ad459 EB |
10298 | /* Same processing as above for the fields of each variant. */ |
10299 | FOR_EACH_VEC_ELT (gnu_variant_list, i, v) | |
05dbb83f | 10300 | { |
cd8ad459 EB |
10301 | if (TYPE_FIELDS (v->new_type)) |
10302 | TYPE_FIELDS (v->new_type) | |
10303 | = reverse_sort_field_list (TYPE_FIELDS (v->new_type)); | |
10304 | if (v->aux) | |
10305 | TYPE_FIELDS (v->new_type) | |
10306 | = chainon (v->aux, TYPE_FIELDS (v->new_type)); | |
05dbb83f | 10307 | } |
05dbb83f | 10308 | |
05dbb83f AC |
10309 | tree new_variant_part |
10310 | = create_variant_part_from (gnu_variant_part, gnu_variant_list, | |
10311 | gnu_new_type, gnu_pos_list, | |
036c83b6 | 10312 | subst_list, debug_info_p); |
05dbb83f AC |
10313 | DECL_CHAIN (new_variant_part) = gnu_field_list; |
10314 | gnu_field_list = new_variant_part; | |
10315 | } | |
10316 | ||
10317 | gnu_variant_list.release (); | |
036c83b6 | 10318 | subst_list.release (); |
05dbb83f | 10319 | |
05dbb83f AC |
10320 | /* If NEW_TYPE is a subtype, it inherits all the attributes from OLD_TYPE. |
10321 | Otherwise sizes and alignment must be computed independently. */ | |
cd8ad459 EB |
10322 | finish_record_type (gnu_new_type, nreverse (gnu_field_list), |
10323 | is_subtype ? 2 : 1, debug_info_p); | |
05dbb83f | 10324 | |
af62ba41 | 10325 | /* Now go through the entities again looking for itypes that we have not yet |
05dbb83f AC |
10326 | elaborated (e.g. Etypes of fields that have Original_Components). */ |
10327 | for (Entity_Id gnat_field = First_Entity (gnat_new_type); | |
10328 | Present (gnat_field); | |
10329 | gnat_field = Next_Entity (gnat_field)) | |
10330 | if ((Ekind (gnat_field) == E_Component | |
10331 | || Ekind (gnat_field) == E_Discriminant) | |
10332 | && Is_Itype (Etype (gnat_field)) | |
10333 | && !present_gnu_tree (Etype (gnat_field))) | |
10334 | gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, false); | |
10335 | } | |
10336 | ||
1e3cabd4 EB |
10337 | /* Associate to the implementation type of a packed array type specified by |
10338 | GNU_TYPE, which is the translation of GNAT_ENTITY, the original array type | |
10339 | if it has been translated. This association is a parallel type for GNAT | |
10340 | encodings or a debug type for standard DWARF. Note that for standard DWARF, | |
10341 | we also want to get the original type name and therefore we return it. */ | |
1eb58520 | 10342 | |
1e3cabd4 | 10343 | static tree |
2d595887 | 10344 | associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity) |
1eb58520 | 10345 | { |
1e3cabd4 | 10346 | const Entity_Id gnat_original_array_type |
1eb58520 AC |
10347 | = Underlying_Type (Original_Array_Type (gnat_entity)); |
10348 | tree gnu_original_array_type; | |
10349 | ||
10350 | if (!present_gnu_tree (gnat_original_array_type)) | |
1e3cabd4 | 10351 | return NULL_TREE; |
1eb58520 AC |
10352 | |
10353 | gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type); | |
10354 | ||
10355 | if (TYPE_IS_DUMMY_P (gnu_original_array_type)) | |
1e3cabd4 EB |
10356 | return NULL_TREE; |
10357 | ||
10358 | gcc_assert (TYPE_IMPL_PACKED_ARRAY_P (gnu_type)); | |
1eb58520 | 10359 | |
58d32c72 EB |
10360 | if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL) |
10361 | { | |
10362 | add_parallel_type (gnu_type, gnu_original_array_type); | |
10363 | return NULL_TREE; | |
10364 | } | |
10365 | else | |
2d595887 | 10366 | { |
1e3cabd4 | 10367 | SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type); |
2d595887 | 10368 | |
1e3cabd4 | 10369 | tree original_name = TYPE_NAME (gnu_original_array_type); |
2d595887 PMR |
10370 | if (TREE_CODE (original_name) == TYPE_DECL) |
10371 | original_name = DECL_NAME (original_name); | |
1e3cabd4 | 10372 | return original_name; |
2d595887 | 10373 | } |
1eb58520 | 10374 | } |
ce2d0ce2 | 10375 | |
05dbb83f AC |
10376 | /* Given a type T, a FIELD_DECL F, and a replacement value R, return an |
10377 | equivalent type with adjusted size expressions where all occurrences | |
10378 | of references to F in a PLACEHOLDER_EXPR have been replaced by R. | |
77022fa8 EB |
10379 | |
10380 | The function doesn't update the layout of the type, i.e. it assumes | |
10381 | that the substitution is purely formal. That's why the replacement | |
10382 | value R must itself contain a PLACEHOLDER_EXPR. */ | |
a1ab4c31 AC |
10383 | |
10384 | tree | |
10385 | substitute_in_type (tree t, tree f, tree r) | |
10386 | { | |
c6bd4220 | 10387 | tree nt; |
77022fa8 EB |
10388 | |
10389 | gcc_assert (CONTAINS_PLACEHOLDER_P (r)); | |
a1ab4c31 AC |
10390 | |
10391 | switch (TREE_CODE (t)) | |
10392 | { | |
10393 | case INTEGER_TYPE: | |
10394 | case ENUMERAL_TYPE: | |
10395 | case BOOLEAN_TYPE: | |
a531043b | 10396 | case REAL_TYPE: |
84fb43a1 EB |
10397 | |
10398 | /* First the domain types of arrays. */ | |
10399 | if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t)) | |
10400 | || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t))) | |
a1ab4c31 | 10401 | { |
84fb43a1 EB |
10402 | tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r); |
10403 | tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r); | |
a1ab4c31 | 10404 | |
84fb43a1 | 10405 | if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t)) |
a1ab4c31 AC |
10406 | return t; |
10407 | ||
c6bd4220 EB |
10408 | nt = copy_type (t); |
10409 | TYPE_GCC_MIN_VALUE (nt) = low; | |
10410 | TYPE_GCC_MAX_VALUE (nt) = high; | |
a531043b EB |
10411 | |
10412 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t)) | |
a1ab4c31 | 10413 | SET_TYPE_INDEX_TYPE |
c6bd4220 | 10414 | (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r)); |
a1ab4c31 | 10415 | |
c6bd4220 | 10416 | return nt; |
a1ab4c31 | 10417 | } |
77022fa8 | 10418 | |
84fb43a1 EB |
10419 | /* Then the subtypes. */ |
10420 | if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t)) | |
10421 | || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t))) | |
10422 | { | |
10423 | tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r); | |
10424 | tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r); | |
10425 | ||
10426 | if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t)) | |
10427 | return t; | |
10428 | ||
c6bd4220 EB |
10429 | nt = copy_type (t); |
10430 | SET_TYPE_RM_MIN_VALUE (nt, low); | |
10431 | SET_TYPE_RM_MAX_VALUE (nt, high); | |
84fb43a1 | 10432 | |
c6bd4220 | 10433 | return nt; |
84fb43a1 EB |
10434 | } |
10435 | ||
a1ab4c31 AC |
10436 | return t; |
10437 | ||
10438 | case COMPLEX_TYPE: | |
c6bd4220 EB |
10439 | nt = substitute_in_type (TREE_TYPE (t), f, r); |
10440 | if (nt == TREE_TYPE (t)) | |
a1ab4c31 AC |
10441 | return t; |
10442 | ||
c6bd4220 | 10443 | return build_complex_type (nt); |
a1ab4c31 | 10444 | |
a1ab4c31 | 10445 | case FUNCTION_TYPE: |
69720717 | 10446 | case METHOD_TYPE: |
77022fa8 | 10447 | /* These should never show up here. */ |
a1ab4c31 AC |
10448 | gcc_unreachable (); |
10449 | ||
10450 | case ARRAY_TYPE: | |
10451 | { | |
10452 | tree component = substitute_in_type (TREE_TYPE (t), f, r); | |
10453 | tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r); | |
10454 | ||
10455 | if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) | |
10456 | return t; | |
10457 | ||
523e82a7 | 10458 | nt = build_nonshared_array_type (component, domain); |
fe37c7af | 10459 | SET_TYPE_ALIGN (nt, TYPE_ALIGN (t)); |
c6bd4220 EB |
10460 | TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t); |
10461 | SET_TYPE_MODE (nt, TYPE_MODE (t)); | |
10462 | TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r); | |
10463 | TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r); | |
c6bd4220 EB |
10464 | TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t); |
10465 | TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t); | |
d42b7559 EB |
10466 | if (TYPE_REVERSE_STORAGE_ORDER (t)) |
10467 | set_reverse_storage_order_on_array_type (nt); | |
10468 | if (TYPE_NONALIASED_COMPONENT (t)) | |
10469 | set_nonaliased_component_on_array_type (nt); | |
c6bd4220 | 10470 | return nt; |
a1ab4c31 AC |
10471 | } |
10472 | ||
10473 | case RECORD_TYPE: | |
10474 | case UNION_TYPE: | |
10475 | case QUAL_UNION_TYPE: | |
10476 | { | |
77022fa8 | 10477 | bool changed_field = false; |
a1ab4c31 | 10478 | tree field; |
a1ab4c31 AC |
10479 | |
10480 | /* Start out with no fields, make new fields, and chain them | |
10481 | in. If we haven't actually changed the type of any field, | |
10482 | discard everything we've done and return the old type. */ | |
c6bd4220 EB |
10483 | nt = copy_type (t); |
10484 | TYPE_FIELDS (nt) = NULL_TREE; | |
a1ab4c31 | 10485 | |
910ad8de | 10486 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
a1ab4c31 | 10487 | { |
77022fa8 EB |
10488 | tree new_field = copy_node (field), new_n; |
10489 | ||
10490 | new_n = substitute_in_type (TREE_TYPE (field), f, r); | |
10491 | if (new_n != TREE_TYPE (field)) | |
a1ab4c31 | 10492 | { |
77022fa8 EB |
10493 | TREE_TYPE (new_field) = new_n; |
10494 | changed_field = true; | |
10495 | } | |
a1ab4c31 | 10496 | |
77022fa8 EB |
10497 | new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r); |
10498 | if (new_n != DECL_FIELD_OFFSET (field)) | |
10499 | { | |
10500 | DECL_FIELD_OFFSET (new_field) = new_n; | |
10501 | changed_field = true; | |
10502 | } | |
a1ab4c31 | 10503 | |
77022fa8 EB |
10504 | /* Do the substitution inside the qualifier, if any. */ |
10505 | if (TREE_CODE (t) == QUAL_UNION_TYPE) | |
10506 | { | |
10507 | new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r); | |
10508 | if (new_n != DECL_QUALIFIER (field)) | |
10509 | { | |
10510 | DECL_QUALIFIER (new_field) = new_n; | |
10511 | changed_field = true; | |
a1ab4c31 AC |
10512 | } |
10513 | } | |
10514 | ||
c6bd4220 | 10515 | DECL_CONTEXT (new_field) = nt; |
cb3d597d | 10516 | SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field); |
a1ab4c31 | 10517 | |
910ad8de | 10518 | DECL_CHAIN (new_field) = TYPE_FIELDS (nt); |
c6bd4220 | 10519 | TYPE_FIELDS (nt) = new_field; |
a1ab4c31 AC |
10520 | } |
10521 | ||
77022fa8 | 10522 | if (!changed_field) |
a1ab4c31 AC |
10523 | return t; |
10524 | ||
c6bd4220 EB |
10525 | TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt)); |
10526 | TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r); | |
10527 | TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r); | |
10528 | SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r)); | |
10529 | return nt; | |
a1ab4c31 AC |
10530 | } |
10531 | ||
10532 | default: | |
10533 | return t; | |
10534 | } | |
10535 | } | |
ce2d0ce2 | 10536 | |
b4680ca1 | 10537 | /* Return the RM size of GNU_TYPE. This is the actual number of bits |
a1ab4c31 AC |
10538 | needed to represent the object. */ |
10539 | ||
10540 | tree | |
10541 | rm_size (tree gnu_type) | |
10542 | { | |
e6e15ec9 | 10543 | /* For integral types, we store the RM size explicitly. */ |
a1ab4c31 AC |
10544 | if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type)) |
10545 | return TYPE_RM_SIZE (gnu_type); | |
b4680ca1 | 10546 | |
65e0a92b EB |
10547 | /* If the type contains a template, return the padded size of the template |
10548 | plus the RM size of the actual data. */ | |
b4680ca1 EB |
10549 | if (TREE_CODE (gnu_type) == RECORD_TYPE |
10550 | && TYPE_CONTAINS_TEMPLATE_P (gnu_type)) | |
a1ab4c31 AC |
10551 | return |
10552 | size_binop (PLUS_EXPR, | |
65e0a92b EB |
10553 | bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type))), |
10554 | rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))))); | |
b4680ca1 | 10555 | |
e1e5852c EB |
10556 | /* For record or union types, we store the size explicitly. */ |
10557 | if (RECORD_OR_UNION_TYPE_P (gnu_type) | |
315cff15 | 10558 | && !TYPE_FAT_POINTER_P (gnu_type) |
b4680ca1 | 10559 | && TYPE_ADA_SIZE (gnu_type)) |
a1ab4c31 | 10560 | return TYPE_ADA_SIZE (gnu_type); |
b4680ca1 EB |
10561 | |
10562 | /* For other types, this is just the size. */ | |
10563 | return TYPE_SIZE (gnu_type); | |
a1ab4c31 | 10564 | } |
ce2d0ce2 | 10565 | |
0fb2335d EB |
10566 | /* Return the name to be used for GNAT_ENTITY. If a type, create a |
10567 | fully-qualified name, possibly with type information encoding. | |
10568 | Otherwise, return the name. */ | |
10569 | ||
bf44701f EB |
10570 | static const char * |
10571 | get_entity_char (Entity_Id gnat_entity) | |
10572 | { | |
10573 | Get_Encoded_Name (gnat_entity); | |
10574 | return ggc_strdup (Name_Buffer); | |
10575 | } | |
10576 | ||
0fb2335d EB |
10577 | tree |
10578 | get_entity_name (Entity_Id gnat_entity) | |
10579 | { | |
10580 | Get_Encoded_Name (gnat_entity); | |
10581 | return get_identifier_with_length (Name_Buffer, Name_Len); | |
10582 | } | |
10583 | ||
a1ab4c31 AC |
10584 | /* Return an identifier representing the external name to be used for |
10585 | GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___" | |
10586 | and the specified suffix. */ | |
10587 | ||
10588 | tree | |
10589 | create_concat_name (Entity_Id gnat_entity, const char *suffix) | |
10590 | { | |
93582885 EB |
10591 | const Entity_Kind kind = Ekind (gnat_entity); |
10592 | const bool has_suffix = (suffix != NULL); | |
f8fb01fb | 10593 | String_Template temp = {1, has_suffix ? (int) strlen (suffix) : 0}; |
93582885 | 10594 | String_Pointer sp = {suffix, &temp}; |
a1ab4c31 | 10595 | |
93582885 | 10596 | Get_External_Name (gnat_entity, has_suffix, sp); |
a1ab4c31 | 10597 | |
0fb2335d EB |
10598 | /* A variable using the Stdcall convention lives in a DLL. We adjust |
10599 | its name to use the jump table, the _imp__NAME contains the address | |
10600 | for the NAME variable. */ | |
a1ab4c31 AC |
10601 | if ((kind == E_Variable || kind == E_Constant) |
10602 | && Has_Stdcall_Convention (gnat_entity)) | |
10603 | { | |
93582885 | 10604 | const int len = strlen (STDCALL_PREFIX) + Name_Len; |
0fb2335d | 10605 | char *new_name = (char *) alloca (len + 1); |
93582885 | 10606 | strcpy (new_name, STDCALL_PREFIX); |
0fb2335d EB |
10607 | strcat (new_name, Name_Buffer); |
10608 | return get_identifier_with_length (new_name, len); | |
a1ab4c31 AC |
10609 | } |
10610 | ||
0fb2335d | 10611 | return get_identifier_with_length (Name_Buffer, Name_Len); |
a1ab4c31 AC |
10612 | } |
10613 | ||
0fb2335d | 10614 | /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a |
a1ab4c31 | 10615 | string, return a new IDENTIFIER_NODE that is the concatenation of |
0fb2335d | 10616 | the name followed by "___" and the specified suffix. */ |
a1ab4c31 AC |
10617 | |
10618 | tree | |
0fb2335d | 10619 | concat_name (tree gnu_name, const char *suffix) |
a1ab4c31 | 10620 | { |
0fb2335d EB |
10621 | const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix); |
10622 | char *new_name = (char *) alloca (len + 1); | |
10623 | strcpy (new_name, IDENTIFIER_POINTER (gnu_name)); | |
10624 | strcat (new_name, "___"); | |
10625 | strcat (new_name, suffix); | |
10626 | return get_identifier_with_length (new_name, len); | |
a1ab4c31 AC |
10627 | } |
10628 | ||
875bdbe2 | 10629 | /* Initialize the data structures of the decl.c module. */ |
4116e7d0 EB |
10630 | |
10631 | void | |
10632 | init_gnat_decl (void) | |
10633 | { | |
10634 | /* Initialize the cache of annotated values. */ | |
d242408f | 10635 | annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512); |
1e55d29a EB |
10636 | |
10637 | /* Initialize the association of dummy types with subprograms. */ | |
10638 | dummy_to_subprog_map = hash_table<dummy_type_hasher>::create_ggc (512); | |
4116e7d0 EB |
10639 | } |
10640 | ||
875bdbe2 | 10641 | /* Destroy the data structures of the decl.c module. */ |
4116e7d0 EB |
10642 | |
10643 | void | |
10644 | destroy_gnat_decl (void) | |
10645 | { | |
10646 | /* Destroy the cache of annotated values. */ | |
d242408f | 10647 | annotate_value_cache->empty (); |
4116e7d0 | 10648 | annotate_value_cache = NULL; |
1e55d29a EB |
10649 | |
10650 | /* Destroy the association of dummy types with subprograms. */ | |
10651 | dummy_to_subprog_map->empty (); | |
10652 | dummy_to_subprog_map = NULL; | |
4116e7d0 EB |
10653 | } |
10654 | ||
a1ab4c31 | 10655 | #include "gt-ada-decl.h" |