]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/sem_aggr.adb
[Ada] Fix expansion of delta aggregates with slices
[gcc.git] / gcc / ada / sem_aggr.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ A G G R --
6-- --
7-- B o d y --
8-- --
4b490c1e 9-- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
157a9bf5 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
157a9bf5
ES
18-- Public License distributed with GNAT; see file COPYING3. If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
d10bb9d5 26with Aspects; use Aspects;
996ae0b0
RK
27with Atree; use Atree;
28with Checks; use Checks;
29with Einfo; use Einfo;
30with Elists; use Elists;
31with Errout; use Errout;
4755cce9 32with Expander; use Expander;
5168a9b3 33with Exp_Ch6; use Exp_Ch6;
52739835 34with Exp_Tss; use Exp_Tss;
996ae0b0
RK
35with Exp_Util; use Exp_Util;
36with Freeze; use Freeze;
37with Itypes; use Itypes;
c7ce71c2 38with Lib; use Lib;
fbf5a39b 39with Lib.Xref; use Lib.Xref;
996ae0b0 40with Namet; use Namet;
c80d4855 41with Namet.Sp; use Namet.Sp;
996ae0b0
RK
42with Nmake; use Nmake;
43with Nlists; use Nlists;
44with Opt; use Opt;
bd65a2d7 45with Restrict; use Restrict;
2f7ae2aa 46with Rident; use Rident;
996ae0b0 47with Sem; use Sem;
a4100e55 48with Sem_Aux; use Sem_Aux;
996ae0b0 49with Sem_Cat; use Sem_Cat;
88b32fc3 50with Sem_Ch3; use Sem_Ch3;
87729e5a 51with Sem_Ch8; use Sem_Ch8;
996ae0b0 52with Sem_Ch13; use Sem_Ch13;
0929eaeb 53with Sem_Dim; use Sem_Dim;
996ae0b0
RK
54with Sem_Eval; use Sem_Eval;
55with Sem_Res; use Sem_Res;
56with Sem_Util; use Sem_Util;
57with Sem_Type; use Sem_Type;
fbf5a39b 58with Sem_Warn; use Sem_Warn;
996ae0b0
RK
59with Sinfo; use Sinfo;
60with Snames; use Snames;
61with Stringt; use Stringt;
62with Stand; use Stand;
6989bc1f 63with Style; use Style;
fbf5a39b 64with Targparm; use Targparm;
996ae0b0
RK
65with Tbuild; use Tbuild;
66with Uintp; use Uintp;
67
996ae0b0
RK
68package body Sem_Aggr is
69
70 type Case_Bounds is record
82893775
AC
71 Lo : Node_Id;
72 -- Low bound of choice. Once we sort the Case_Table, then entries
73 -- will be in order of ascending Choice_Lo values.
74
75 Hi : Node_Id;
76 -- High Bound of choice. The sort does not pay any attention to the
77 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
78
79 Highest : Uint;
80 -- If there are duplicates or missing entries, then in the sorted
81 -- table, this records the highest value among Choice_Hi values
82 -- seen so far, including this entry.
83
84 Choice : Node_Id;
85 -- The node of the choice
996ae0b0
RK
86 end record;
87
38f44fd6
PT
88 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
89 -- Table type used by Check_Case_Choices procedure
996ae0b0
RK
90
91 -----------------------
92 -- Local Subprograms --
93 -----------------------
94
95 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
82893775
AC
96 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
97 -- simple insertion sort is used since the choices in a case statement will
98 -- usually be in near sorted order.
996ae0b0 99
9b96e234
JM
100 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
101 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
102 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
103 -- the array case (the component type of the array will be used) or an
104 -- E_Component/E_Discriminant entity in the record case, in which case the
105 -- type of the component will be used for the test. If Typ is any other
106 -- kind of entity, the call is ignored. Expr is the component node in the
8133b9d1 107 -- aggregate which is known to have a null value. A warning message will be
9b96e234
JM
108 -- issued if the component is null excluding.
109 --
110 -- It would be better to pass the proper type for Typ ???
2820d220 111
ca44152f
ES
112 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
113 -- Check that Expr is either not limited or else is one of the cases of
114 -- expressions allowed for a limited component association (namely, an
115 -- aggregate, function call, or <> notation). Report error for violations.
480156b2 116 -- Expression is also OK in an instance or inlining context, because we
812e6118 117 -- have already preanalyzed and it is known to be type correct.
ca44152f 118
996ae0b0
RK
119 ------------------------------------------------------
120 -- Subprograms used for RECORD AGGREGATE Processing --
121 ------------------------------------------------------
122
123 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
124 -- This procedure performs all the semantic checks required for record
125 -- aggregates. Note that for aggregates analysis and resolution go
126 -- hand in hand. Aggregate analysis has been delayed up to here and
127 -- it is done while resolving the aggregate.
128 --
129 -- N is the N_Aggregate node.
130 -- Typ is the record type for the aggregate resolution
131 --
9b96e234
JM
132 -- While performing the semantic checks, this procedure builds a new
133 -- Component_Association_List where each record field appears alone in a
134 -- Component_Choice_List along with its corresponding expression. The
135 -- record fields in the Component_Association_List appear in the same order
136 -- in which they appear in the record type Typ.
996ae0b0 137 --
9b96e234
JM
138 -- Once this new Component_Association_List is built and all the semantic
139 -- checks performed, the original aggregate subtree is replaced with the
0ad46f04
PT
140 -- new named record aggregate just built. This new record aggregate has no
141 -- positional associations, so its Expressions field is set to No_List.
142 -- Note that subtree substitution is performed with Rewrite so as to be
143 -- able to retrieve the original aggregate.
996ae0b0
RK
144 --
145 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
146 -- yields the aggregate format expected by Gigi. Typically, this kind of
147 -- tree manipulations are done in the expander. However, because the
9b96e234
JM
148 -- semantic checks that need to be performed on record aggregates really go
149 -- hand in hand with the record aggregate normalization, the aggregate
996ae0b0 150 -- subtree transformation is performed during resolution rather than
9b96e234
JM
151 -- expansion. Had we decided otherwise we would have had to duplicate most
152 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
c7ce71c2 153 -- however, that all the expansion concerning aggregates for tagged records
9b96e234 154 -- is done in Expand_Record_Aggregate.
996ae0b0
RK
155 --
156 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
157 --
158 -- 1. Make sure that the record type against which the record aggregate
c9a1acdc
AC
159 -- has to be resolved is not abstract. Furthermore if the type is a
160 -- null aggregate make sure the input aggregate N is also null.
996ae0b0
RK
161 --
162 -- 2. Verify that the structure of the aggregate is that of a record
163 -- aggregate. Specifically, look for component associations and ensure
164 -- that each choice list only has identifiers or the N_Others_Choice
165 -- node. Also make sure that if present, the N_Others_Choice occurs
166 -- last and by itself.
167 --
c9a1acdc
AC
168 -- 3. If Typ contains discriminants, the values for each discriminant is
169 -- looked for. If the record type Typ has variants, we check that the
170 -- expressions corresponding to each discriminant ruling the (possibly
171 -- nested) variant parts of Typ, are static. This allows us to determine
172 -- the variant parts to which the rest of the aggregate must conform.
173 -- The names of discriminants with their values are saved in a new
174 -- association list, New_Assoc_List which is later augmented with the
175 -- names and values of the remaining components in the record type.
996ae0b0
RK
176 --
177 -- During this phase we also make sure that every discriminant is
c9a1acdc
AC
178 -- assigned exactly one value. Note that when several values for a given
179 -- discriminant are found, semantic processing continues looking for
180 -- further errors. In this case it's the first discriminant value found
181 -- which we will be recorded.
996ae0b0
RK
182 --
183 -- IMPORTANT NOTE: For derived tagged types this procedure expects
184 -- First_Discriminant and Next_Discriminant to give the correct list
185 -- of discriminants, in the correct order.
186 --
c9a1acdc
AC
187 -- 4. After all the discriminant values have been gathered, we can set the
188 -- Etype of the record aggregate. If Typ contains no discriminants this
189 -- is straightforward: the Etype of N is just Typ, otherwise a new
190 -- implicit constrained subtype of Typ is built to be the Etype of N.
996ae0b0
RK
191 --
192 -- 5. Gather the remaining record components according to the discriminant
193 -- values. This involves recursively traversing the record type
194 -- structure to see what variants are selected by the given discriminant
195 -- values. This processing is a little more convoluted if Typ is a
196 -- derived tagged types since we need to retrieve the record structure
197 -- of all the ancestors of Typ.
198 --
c9a1acdc
AC
199 -- 6. After gathering the record components we look for their values in the
200 -- record aggregate and emit appropriate error messages should we not
201 -- find such values or should they be duplicated.
202 --
203 -- 7. We then make sure no illegal component names appear in the record
204 -- aggregate and make sure that the type of the record components
205 -- appearing in a same choice list is the same. Finally we ensure that
206 -- the others choice, if present, is used to provide the value of at
207 -- least a record component.
208 --
209 -- 8. The original aggregate node is replaced with the new named aggregate
210 -- built in steps 3 through 6, as explained earlier.
211 --
212 -- Given the complexity of record aggregate resolution, the primary goal of
213 -- this routine is clarity and simplicity rather than execution and storage
214 -- efficiency. If there are only positional components in the aggregate the
215 -- running time is linear. If there are associations the running time is
216 -- still linear as long as the order of the associations is not too far off
217 -- the order of the components in the record type. If this is not the case
218 -- the running time is at worst quadratic in the size of the association
219 -- list.
996ae0b0
RK
220
221 procedure Check_Misspelled_Component
9c290e69
PO
222 (Elements : Elist_Id;
223 Component : Node_Id);
c9a1acdc
AC
224 -- Give possible misspelling diagnostic if Component is likely to be a
225 -- misspelling of one of the components of the Assoc_List. This is called
226 -- by Resolve_Aggr_Expr after producing an invalid component error message.
996ae0b0 227
996ae0b0
RK
228 -----------------------------------------------------
229 -- Subprograms used for ARRAY AGGREGATE Processing --
230 -----------------------------------------------------
231
232 function Resolve_Array_Aggregate
233 (N : Node_Id;
234 Index : Node_Id;
235 Index_Constr : Node_Id;
236 Component_Typ : Entity_Id;
ca44152f 237 Others_Allowed : Boolean) return Boolean;
996ae0b0
RK
238 -- This procedure performs the semantic checks for an array aggregate.
239 -- True is returned if the aggregate resolution succeeds.
ca44152f 240 --
996ae0b0 241 -- The procedure works by recursively checking each nested aggregate.
9f4fd324 242 -- Specifically, after checking a sub-aggregate nested at the i-th level
996ae0b0
RK
243 -- we recursively check all the subaggregates at the i+1-st level (if any).
244 -- Note that for aggregates analysis and resolution go hand in hand.
245 -- Aggregate analysis has been delayed up to here and it is done while
246 -- resolving the aggregate.
247 --
248 -- N is the current N_Aggregate node to be checked.
249 --
250 -- Index is the index node corresponding to the array sub-aggregate that
251 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
252 -- corresponding index type (or subtype).
253 --
254 -- Index_Constr is the node giving the applicable index constraint if
255 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
256 -- contexts [...] that can be used to determine the bounds of the array
257 -- value specified by the aggregate". If Others_Allowed below is False
258 -- there is no applicable index constraint and this node is set to Index.
259 --
260 -- Component_Typ is the array component type.
261 --
262 -- Others_Allowed indicates whether an others choice is allowed
263 -- in the context where the top-level aggregate appeared.
264 --
265 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
266 --
267 -- 1. Make sure that the others choice, if present, is by itself and
268 -- appears last in the sub-aggregate. Check that we do not have
269 -- positional and named components in the array sub-aggregate (unless
270 -- the named association is an others choice). Finally if an others
12a13f01 271 -- choice is present, make sure it is allowed in the aggregate context.
996ae0b0
RK
272 --
273 -- 2. If the array sub-aggregate contains discrete_choices:
274 --
275 -- (A) Verify their validity. Specifically verify that:
276 --
277 -- (a) If a null range is present it must be the only possible
278 -- choice in the array aggregate.
279 --
280 -- (b) Ditto for a non static range.
281 --
282 -- (c) Ditto for a non static expression.
283 --
284 -- In addition this step analyzes and resolves each discrete_choice,
285 -- making sure that its type is the type of the corresponding Index.
286 -- If we are not at the lowest array aggregate level (in the case of
287 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
288 -- recursively on each component expression. Otherwise, resolve the
289 -- bottom level component expressions against the expected component
290 -- type ONLY IF the component corresponds to a single discrete choice
291 -- which is not an others choice (to see why read the DELAYED
292 -- COMPONENT RESOLUTION below).
293 --
294 -- (B) Determine the bounds of the sub-aggregate and lowest and
295 -- highest choice values.
296 --
297 -- 3. For positional aggregates:
298 --
299 -- (A) Loop over the component expressions either recursively invoking
300 -- Resolve_Array_Aggregate on each of these for multi-dimensional
301 -- array aggregates or resolving the bottom level component
302 -- expressions against the expected component type.
303 --
304 -- (B) Determine the bounds of the positional sub-aggregates.
305 --
306 -- 4. Try to determine statically whether the evaluation of the array
307 -- sub-aggregate raises Constraint_Error. If yes emit proper
308 -- warnings. The precise checks are the following:
309 --
310 -- (A) Check that the index range defined by aggregate bounds is
311 -- compatible with corresponding index subtype.
312 -- We also check against the base type. In fact it could be that
313 -- Low/High bounds of the base type are static whereas those of
314 -- the index subtype are not. Thus if we can statically catch
315 -- a problem with respect to the base type we are guaranteed
316 -- that the same problem will arise with the index subtype
317 --
318 -- (B) If we are dealing with a named aggregate containing an others
319 -- choice and at least one discrete choice then make sure the range
320 -- specified by the discrete choices does not overflow the
321 -- aggregate bounds. We also check against the index type and base
322 -- type bounds for the same reasons given in (A).
323 --
324 -- (C) If we are dealing with a positional aggregate with an others
325 -- choice make sure the number of positional elements specified
326 -- does not overflow the aggregate bounds. We also check against
327 -- the index type and base type bounds as mentioned in (A).
328 --
329 -- Finally construct an N_Range node giving the sub-aggregate bounds.
330 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
331 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
332 -- to build the appropriate aggregate subtype. Aggregate_Bounds
333 -- information is needed during expansion.
334 --
335 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
336 -- expressions in an array aggregate may call Duplicate_Subexpr or some
337 -- other routine that inserts code just outside the outermost aggregate.
338 -- If the array aggregate contains discrete choices or an others choice,
339 -- this may be wrong. Consider for instance the following example.
340 --
341 -- type Rec is record
342 -- V : Integer := 0;
343 -- end record;
344 --
345 -- type Acc_Rec is access Rec;
346 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
347 --
348 -- Then the transformation of "new Rec" that occurs during resolution
349 -- entails the following code modifications
350 --
351 -- P7b : constant Acc_Rec := new Rec;
fbf5a39b 352 -- RecIP (P7b.all);
996ae0b0
RK
353 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
354 --
355 -- This code transformation is clearly wrong, since we need to call
356 -- "new Rec" for each of the 3 array elements. To avoid this problem we
357 -- delay resolution of the components of non positional array aggregates
358 -- to the expansion phase. As an optimization, if the discrete choice
359 -- specifies a single value we do not delay resolution.
360
361 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
362 -- This routine returns the type or subtype of an array aggregate.
363 --
364 -- N is the array aggregate node whose type we return.
365 --
366 -- Typ is the context type in which N occurs.
367 --
c45b6ae0 368 -- This routine creates an implicit array subtype whose bounds are
996ae0b0
RK
369 -- those defined by the aggregate. When this routine is invoked
370 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
371 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
c7ce71c2 372 -- sub-aggregate bounds. When building the aggregate itype, this function
996ae0b0
RK
373 -- traverses the array aggregate N collecting such Aggregate_Bounds and
374 -- constructs the proper array aggregate itype.
375 --
376 -- Note that in the case of multidimensional aggregates each inner
377 -- sub-aggregate corresponding to a given array dimension, may provide a
378 -- different bounds. If it is possible to determine statically that
379 -- some sub-aggregates corresponding to the same index do not have the
380 -- same bounds, then a warning is emitted. If such check is not possible
381 -- statically (because some sub-aggregate bounds are dynamic expressions)
382 -- then this job is left to the expander. In all cases the particular
383 -- bounds that this function will chose for a given dimension is the first
384 -- N_Range node for a sub-aggregate corresponding to that dimension.
385 --
386 -- Note that the Raises_Constraint_Error flag of an array aggregate
387 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
388 -- is set in Resolve_Array_Aggregate but the aggregate is not
389 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
390 -- first construct the proper itype for the aggregate (Gigi needs
21d7ef70 391 -- this). After constructing the proper itype we will eventually replace
996ae0b0
RK
392 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
393 -- Of course in cases such as:
394 --
395 -- type Arr is array (integer range <>) of Integer;
396 -- A : Arr := (positive range -1 .. 2 => 0);
397 --
398 -- The bounds of the aggregate itype are cooked up to look reasonable
399 -- (in this particular case the bounds will be 1 .. 2).
400
996ae0b0 401 procedure Make_String_Into_Aggregate (N : Node_Id);
21d7ef70 402 -- A string literal can appear in a context in which a one dimensional
996ae0b0
RK
403 -- array of characters is expected. This procedure simply rewrites the
404 -- string as an aggregate, prior to resolution.
405
8d9a1ba7
PMR
406 ---------------------------------
407 -- Delta aggregate processing --
408 ---------------------------------
409
410 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
411 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
412
996ae0b0
RK
413 ------------------------
414 -- Array_Aggr_Subtype --
415 ------------------------
416
417 function Array_Aggr_Subtype
b87971f3
AC
418 (N : Node_Id;
419 Typ : Entity_Id) return Entity_Id
996ae0b0
RK
420 is
421 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
ec53a6da 422 -- Number of aggregate index dimensions
996ae0b0
RK
423
424 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
ec53a6da 425 -- Constrained N_Range of each index dimension in our aggregate itype
996ae0b0 426
58009744
AC
427 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
428 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
ec53a6da 429 -- Low and High bounds for each index dimension in our aggregate itype
996ae0b0
RK
430
431 Is_Fully_Positional : Boolean := True;
432
433 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
fb468a94
AC
434 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
435 -- to (sub-)aggregate N. This procedure collects and removes the side
436 -- effects of the constrained N_Range nodes corresponding to each index
2b3d67a5
AC
437 -- dimension of our aggregate itype. These N_Range nodes are collected
438 -- in Aggr_Range above.
ec53a6da 439 --
996ae0b0
RK
440 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
441 -- bounds of each index dimension. If, when collecting, two bounds
442 -- corresponding to the same dimension are static and found to differ,
443 -- then emit a warning, and mark N as raising Constraint_Error.
444
445 -------------------------
446 -- Collect_Aggr_Bounds --
447 -------------------------
448
449 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
450 This_Range : constant Node_Id := Aggregate_Bounds (N);
ec53a6da 451 -- The aggregate range node of this specific sub-aggregate
996ae0b0
RK
452
453 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
454 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
ec53a6da 455 -- The aggregate bounds of this specific sub-aggregate
996ae0b0
RK
456
457 Assoc : Node_Id;
458 Expr : Node_Id;
459
460 begin
fb468a94
AC
461 Remove_Side_Effects (This_Low, Variable_Ref => True);
462 Remove_Side_Effects (This_High, Variable_Ref => True);
463
996ae0b0
RK
464 -- Collect the first N_Range for a given dimension that you find.
465 -- For a given dimension they must be all equal anyway.
466
467 if No (Aggr_Range (Dim)) then
468 Aggr_Low (Dim) := This_Low;
469 Aggr_High (Dim) := This_High;
470 Aggr_Range (Dim) := This_Range;
471
472 else
473 if Compile_Time_Known_Value (This_Low) then
474 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
be035558 475 Aggr_Low (Dim) := This_Low;
996ae0b0
RK
476
477 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
478 Set_Raises_Constraint_Error (N);
43417b90 479 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
480 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
481 Error_Msg_N ("\Constraint_Error [<<", N);
996ae0b0
RK
482 end if;
483 end if;
484
485 if Compile_Time_Known_Value (This_High) then
486 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
be035558 487 Aggr_High (Dim) := This_High;
996ae0b0
RK
488
489 elsif
490 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
491 then
492 Set_Raises_Constraint_Error (N);
43417b90 493 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
494 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
495 Error_Msg_N ("\Constraint_Error [<<", N);
996ae0b0
RK
496 end if;
497 end if;
498 end if;
499
500 if Dim < Aggr_Dimension then
501
502 -- Process positional components
503
504 if Present (Expressions (N)) then
505 Expr := First (Expressions (N));
506 while Present (Expr) loop
507 Collect_Aggr_Bounds (Expr, Dim + 1);
508 Next (Expr);
509 end loop;
510 end if;
511
512 -- Process component associations
513
514 if Present (Component_Associations (N)) then
515 Is_Fully_Positional := False;
516
517 Assoc := First (Component_Associations (N));
518 while Present (Assoc) loop
519 Expr := Expression (Assoc);
520 Collect_Aggr_Bounds (Expr, Dim + 1);
521 Next (Assoc);
522 end loop;
523 end if;
524 end if;
525 end Collect_Aggr_Bounds;
526
527 -- Array_Aggr_Subtype variables
528
529 Itype : Entity_Id;
b87971f3 530 -- The final itype of the overall aggregate
996ae0b0 531
fbf5a39b 532 Index_Constraints : constant List_Id := New_List;
ec53a6da 533 -- The list of index constraints of the aggregate itype
996ae0b0
RK
534
535 -- Start of processing for Array_Aggr_Subtype
536
537 begin
b87971f3
AC
538 -- Make sure that the list of index constraints is properly attached to
539 -- the tree, and then collect the aggregate bounds.
996ae0b0
RK
540
541 Set_Parent (Index_Constraints, N);
542 Collect_Aggr_Bounds (N, 1);
543
3b42c566 544 -- Build the list of constrained indexes of our aggregate itype
996ae0b0
RK
545
546 for J in 1 .. Aggr_Dimension loop
547 Create_Index : declare
fbf5a39b
AC
548 Index_Base : constant Entity_Id :=
549 Base_Type (Etype (Aggr_Range (J)));
996ae0b0
RK
550 Index_Typ : Entity_Id;
551
552 begin
8133b9d1
ES
553 -- Construct the Index subtype, and associate it with the range
554 -- construct that generates it.
996ae0b0 555
8133b9d1
ES
556 Index_Typ :=
557 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
996ae0b0
RK
558
559 Set_Etype (Index_Typ, Index_Base);
560
561 if Is_Character_Type (Index_Base) then
562 Set_Is_Character_Type (Index_Typ);
563 end if;
564
565 Set_Size_Info (Index_Typ, (Index_Base));
566 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
567 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
568 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
569
570 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
571 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
572 end if;
573
574 Set_Etype (Aggr_Range (J), Index_Typ);
575
576 Append (Aggr_Range (J), To => Index_Constraints);
577 end Create_Index;
578 end loop;
579
580 -- Now build the Itype
581
582 Itype := Create_Itype (E_Array_Subtype, N);
583
b87971f3
AC
584 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
585 Set_Convention (Itype, Convention (Typ));
586 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
587 Set_Etype (Itype, Base_Type (Typ));
588 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
589 Set_Is_Aliased (Itype, Is_Aliased (Typ));
a517d6c1 590 Set_Is_Independent (Itype, Is_Independent (Typ));
b87971f3 591 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
996ae0b0 592
fbf5a39b
AC
593 Copy_Suppress_Status (Index_Check, Typ, Itype);
594 Copy_Suppress_Status (Length_Check, Typ, Itype);
595
996ae0b0
RK
596 Set_First_Index (Itype, First (Index_Constraints));
597 Set_Is_Constrained (Itype, True);
598 Set_Is_Internal (Itype, True);
996ae0b0 599
619bfd9f
ES
600 if Has_Predicates (Typ) then
601 Set_Has_Predicates (Itype);
602
a096f12e
ES
603 -- If the base type has a predicate, capture the predicated parent
604 -- or the existing predicate function for SPARK use.
605
619bfd9f
ES
606 if Present (Predicate_Function (Typ)) then
607 Set_Predicate_Function (Itype, Predicate_Function (Typ));
a096f12e
ES
608
609 elsif Is_Itype (Typ) then
619bfd9f 610 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
a096f12e
ES
611
612 else
613 Set_Predicated_Parent (Itype, Typ);
619bfd9f
ES
614 end if;
615 end if;
616
996ae0b0 617 -- A simple optimization: purely positional aggregates of static
b87971f3
AC
618 -- components should be passed to gigi unexpanded whenever possible, and
619 -- regardless of the staticness of the bounds themselves. Subsequent
620 -- checks in exp_aggr verify that type is not packed, etc.
996ae0b0 621
58009744
AC
622 Set_Size_Known_At_Compile_Time
623 (Itype,
8133b9d1
ES
624 Is_Fully_Positional
625 and then Comes_From_Source (N)
626 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
996ae0b0 627
b87971f3 628 -- We always need a freeze node for a packed array subtype, so that we
8ca597af 629 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
b87971f3
AC
630 -- expansion is disabled, the packed array subtype is not built, and we
631 -- must not generate a freeze node for the type, or else it will appear
632 -- incomplete to gigi.
996ae0b0 633
b87971f3
AC
634 if Is_Packed (Itype)
635 and then not In_Spec_Expression
996ae0b0
RK
636 and then Expander_Active
637 then
638 Freeze_Itype (Itype, N);
639 end if;
640
641 return Itype;
642 end Array_Aggr_Subtype;
643
644 --------------------------------
645 -- Check_Misspelled_Component --
646 --------------------------------
647
648 procedure Check_Misspelled_Component
9c290e69
PO
649 (Elements : Elist_Id;
650 Component : Node_Id)
996ae0b0
RK
651 is
652 Max_Suggestions : constant := 2;
653
654 Nr_Of_Suggestions : Natural := 0;
655 Suggestion_1 : Entity_Id := Empty;
656 Suggestion_2 : Entity_Id := Empty;
657 Component_Elmt : Elmt_Id;
658
659 begin
b87971f3 660 -- All the components of List are matched against Component and a count
e49de265 661 -- is maintained of possible misspellings. When at the end of the
a90bd866 662 -- analysis there are one or two (not more) possible misspellings,
e49de265 663 -- these misspellings will be suggested as possible corrections.
996ae0b0 664
c80d4855
RD
665 Component_Elmt := First_Elmt (Elements);
666 while Nr_Of_Suggestions <= Max_Suggestions
667 and then Present (Component_Elmt)
668 loop
669 if Is_Bad_Spelling_Of
670 (Chars (Node (Component_Elmt)),
671 Chars (Component))
672 then
673 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
996ae0b0 674
c80d4855
RD
675 case Nr_Of_Suggestions is
676 when 1 => Suggestion_1 := Node (Component_Elmt);
677 when 2 => Suggestion_2 := Node (Component_Elmt);
e49de265 678 when others => null;
c80d4855
RD
679 end case;
680 end if;
996ae0b0 681
c80d4855
RD
682 Next_Elmt (Component_Elmt);
683 end loop;
996ae0b0 684
c80d4855 685 -- Report at most two suggestions
996ae0b0 686
c80d4855 687 if Nr_Of_Suggestions = 1 then
4e7a4f6e 688 Error_Msg_NE -- CODEFIX
c80d4855 689 ("\possible misspelling of&", Component, Suggestion_1);
996ae0b0 690
c80d4855
RD
691 elsif Nr_Of_Suggestions = 2 then
692 Error_Msg_Node_2 := Suggestion_2;
4e7a4f6e 693 Error_Msg_NE -- CODEFIX
c80d4855
RD
694 ("\possible misspelling of& or&", Component, Suggestion_1);
695 end if;
996ae0b0
RK
696 end Check_Misspelled_Component;
697
ca44152f
ES
698 ----------------------------------------
699 -- Check_Expr_OK_In_Limited_Aggregate --
700 ----------------------------------------
701
702 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
703 begin
704 if Is_Limited_Type (Etype (Expr))
705 and then Comes_From_Source (Expr)
ca44152f 706 then
36428cc4
AC
707 if In_Instance_Body or else In_Inlined_Body then
708 null;
709
710 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
711 Error_Msg_N
712 ("initialization not allowed for limited types", Expr);
ca44152f
ES
713 Explain_Limited_Type (Etype (Expr), Expr);
714 end if;
715 end if;
716 end Check_Expr_OK_In_Limited_Aggregate;
717
9f90d123
AC
718 -------------------------
719 -- Is_Others_Aggregate --
720 -------------------------
721
722 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
27c3d986
EB
723 Assoc : constant List_Id := Component_Associations (Aggr);
724
9f90d123
AC
725 begin
726 return No (Expressions (Aggr))
27c3d986 727 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
9f90d123
AC
728 end Is_Others_Aggregate;
729
27c3d986
EB
730 -------------------------
731 -- Is_Single_Aggregate --
732 -------------------------
733
734 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
735 Assoc : constant List_Id := Component_Associations (Aggr);
736
737 begin
738 return No (Expressions (Aggr))
739 and then No (Next (First (Assoc)))
740 and then No (Next (First (Choice_List (First (Assoc)))));
741 end Is_Single_Aggregate;
742
996ae0b0
RK
743 --------------------------------
744 -- Make_String_Into_Aggregate --
745 --------------------------------
746
747 procedure Make_String_Into_Aggregate (N : Node_Id) is
fbf5a39b 748 Exprs : constant List_Id := New_List;
996ae0b0 749 Loc : constant Source_Ptr := Sloc (N);
996ae0b0
RK
750 Str : constant String_Id := Strval (N);
751 Strlen : constant Nat := String_Length (Str);
fbf5a39b
AC
752 C : Char_Code;
753 C_Node : Node_Id;
754 New_N : Node_Id;
755 P : Source_Ptr;
996ae0b0
RK
756
757 begin
fbf5a39b 758 P := Loc + 1;
996ae0b0
RK
759 for J in 1 .. Strlen loop
760 C := Get_String_Char (Str, J);
761 Set_Character_Literal_Name (C);
762
82c80734
RD
763 C_Node :=
764 Make_Character_Literal (P,
765 Chars => Name_Find,
766 Char_Literal_Value => UI_From_CC (C));
996ae0b0 767 Set_Etype (C_Node, Any_Character);
996ae0b0
RK
768 Append_To (Exprs, C_Node);
769
770 P := P + 1;
b87971f3 771 -- Something special for wide strings???
996ae0b0
RK
772 end loop;
773
774 New_N := Make_Aggregate (Loc, Expressions => Exprs);
775 Set_Analyzed (New_N);
776 Set_Etype (New_N, Any_Composite);
777
778 Rewrite (N, New_N);
779 end Make_String_Into_Aggregate;
780
781 -----------------------
782 -- Resolve_Aggregate --
783 -----------------------
784
785 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
f915704f 786 Loc : constant Source_Ptr := Sloc (N);
996ae0b0
RK
787
788 Aggr_Subtyp : Entity_Id;
789 -- The actual aggregate subtype. This is not necessarily the same as Typ
790 -- which is the subtype of the context in which the aggregate was found.
791
792 begin
6d2a1120
RD
793 -- Ignore junk empty aggregate resulting from parser error
794
795 if No (Expressions (N))
796 and then No (Component_Associations (N))
797 and then not Null_Record_Present (N)
798 then
799 return;
800 end if;
801
0180fd26
AC
802 -- If the aggregate has box-initialized components, its type must be
803 -- frozen so that initialization procedures can properly be called
64ac53f4 804 -- in the resolution that follows. The replacement of boxes with
0180fd26 805 -- initialization calls is properly an expansion activity but it must
f5da7a97 806 -- be done during resolution.
0180fd26
AC
807
808 if Expander_Active
f5da7a97 809 and then Present (Component_Associations (N))
0180fd26
AC
810 then
811 declare
812 Comp : Node_Id;
813
814 begin
815 Comp := First (Component_Associations (N));
816 while Present (Comp) loop
817 if Box_Present (Comp) then
818 Insert_Actions (N, Freeze_Entity (Typ, N));
819 exit;
820 end if;
fe0ec02f 821
0180fd26
AC
822 Next (Comp);
823 end loop;
824 end;
825 end if;
826
fbf5a39b 827 -- Check for aggregates not allowed in configurable run-time mode.
b87971f3
AC
828 -- We allow all cases of aggregates that do not come from source, since
829 -- these are all assumed to be small (e.g. bounds of a string literal).
830 -- We also allow aggregates of types we know to be small.
fbf5a39b
AC
831
832 if not Support_Aggregates_On_Target
833 and then Comes_From_Source (N)
834 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
835 then
836 Error_Msg_CRT ("aggregate", N);
837 end if;
996ae0b0 838
0ab80019 839 -- Ada 2005 (AI-287): Limited aggregates allowed
579fda56 840
67645bde
AC
841 -- In an instance, ignore aggregate subcomponents tnat may be limited,
842 -- because they originate in view conflicts. If the original aggregate
843 -- is legal and the actuals are legal, the aggregate itself is legal.
19f0526a 844
67645bde
AC
845 if Is_Limited_Type (Typ)
846 and then Ada_Version < Ada_2005
847 and then not In_Instance
848 then
fbf5a39b
AC
849 Error_Msg_N ("aggregate type cannot be limited", N);
850 Explain_Limited_Type (Typ, N);
996ae0b0
RK
851
852 elsif Is_Class_Wide_Type (Typ) then
853 Error_Msg_N ("type of aggregate cannot be class-wide", N);
854
855 elsif Typ = Any_String
856 or else Typ = Any_Composite
857 then
858 Error_Msg_N ("no unique type for aggregate", N);
859 Set_Etype (N, Any_Composite);
860
861 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
862 Error_Msg_N ("null record forbidden in array aggregate", N);
863
864 elsif Is_Record_Type (Typ) then
865 Resolve_Record_Aggregate (N, Typ);
866
867 elsif Is_Array_Type (Typ) then
868
54740d7d
AC
869 -- First a special test, for the case of a positional aggregate of
870 -- characters which can be replaced by a string literal.
ca44152f 871
54740d7d
AC
872 -- Do not perform this transformation if this was a string literal
873 -- to start with, whose components needed constraint checks, or if
874 -- the component type is non-static, because it will require those
875 -- checks and be transformed back into an aggregate. If the index
876 -- type is not Integer the aggregate may represent a user-defined
877 -- string type but the context might need the original type so we
878 -- do not perform the transformation at this point.
996ae0b0
RK
879
880 if Number_Dimensions (Typ) = 1
ca44152f 881 and then Is_Standard_Character_Type (Component_Type (Typ))
996ae0b0
RK
882 and then No (Component_Associations (N))
883 and then not Is_Limited_Composite (Typ)
884 and then not Is_Private_Composite (Typ)
885 and then not Is_Bit_Packed_Array (Typ)
886 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
edab6088 887 and then Is_OK_Static_Subtype (Component_Type (Typ))
54740d7d
AC
888 and then Base_Type (Etype (First_Index (Typ))) =
889 Base_Type (Standard_Integer)
996ae0b0
RK
890 then
891 declare
892 Expr : Node_Id;
893
894 begin
895 Expr := First (Expressions (N));
896 while Present (Expr) loop
897 exit when Nkind (Expr) /= N_Character_Literal;
898 Next (Expr);
899 end loop;
900
901 if No (Expr) then
902 Start_String;
903
904 Expr := First (Expressions (N));
905 while Present (Expr) loop
82c80734 906 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
996ae0b0
RK
907 Next (Expr);
908 end loop;
909
f915704f 910 Rewrite (N, Make_String_Literal (Loc, End_String));
996ae0b0
RK
911
912 Analyze_And_Resolve (N, Typ);
913 return;
914 end if;
915 end;
916 end if;
917
918 -- Here if we have a real aggregate to deal with
919
920 Array_Aggregate : declare
921 Aggr_Resolved : Boolean;
fbf5a39b
AC
922
923 Aggr_Typ : constant Entity_Id := Etype (Typ);
b87971f3
AC
924 -- This is the unconstrained array type, which is the type against
925 -- which the aggregate is to be resolved. Typ itself is the array
926 -- type of the context which may not be the same subtype as the
927 -- subtype for the final aggregate.
996ae0b0
RK
928
929 begin
f915704f 930 -- In the following we determine whether an OTHERS choice is
996ae0b0
RK
931 -- allowed inside the array aggregate. The test checks the context
932 -- in which the array aggregate occurs. If the context does not
f915704f 933 -- permit it, or the aggregate type is unconstrained, an OTHERS
56e94186
AC
934 -- choice is not allowed (except that it is always allowed on the
935 -- right-hand side of an assignment statement; in this case the
f18344b7
BD
936 -- constrainedness of the type doesn't matter, because an array
937 -- object is always constrained).
d8387153
ES
938
939 -- If expansion is disabled (generic context, or semantics-only
b87971f3
AC
940 -- mode) actual subtypes cannot be constructed, and the type of an
941 -- object may be its unconstrained nominal type. However, if the
f18344b7
BD
942 -- context is an assignment statement, OTHERS is allowed, because
943 -- the target of the assignment will have a constrained subtype
944 -- when fully compiled. Ditto if the context is an initialization
945 -- procedure where a component may have a predicate function that
946 -- carries the base type.
d8387153 947
996ae0b0
RK
948 -- Note that there is no node for Explicit_Actual_Parameter.
949 -- To test for this context we therefore have to test for node
950 -- N_Parameter_Association which itself appears only if there is a
951 -- formal parameter. Consequently we also need to test for
952 -- N_Procedure_Call_Statement or N_Function_Call.
953
5f6fb720
AC
954 -- The context may be an N_Reference node, created by expansion.
955 -- Legality of the others clause was established in the source,
956 -- so the context is legal.
957
b87971f3 958 Set_Etype (N, Aggr_Typ); -- May be overridden later on
c45b6ae0 959
f18344b7 960 if Nkind (Parent (N)) = N_Assignment_Statement
ffdd5248 961 or else Inside_Init_Proc
e917aec2 962 or else (Is_Constrained (Typ)
f18344b7
BD
963 and then Nkind_In (Parent (N),
964 N_Parameter_Association,
965 N_Function_Call,
966 N_Procedure_Call_Statement,
967 N_Generic_Association,
968 N_Formal_Object_Declaration,
969 N_Simple_Return_Statement,
970 N_Object_Declaration,
971 N_Component_Declaration,
972 N_Parameter_Specification,
973 N_Qualified_Expression,
974 N_Reference,
975 N_Aggregate,
976 N_Extension_Aggregate,
977 N_Component_Association,
978 N_Case_Expression_Alternative,
bcc0f556
BD
979 N_If_Expression,
980 N_Expression_With_Actions))
996ae0b0
RK
981 then
982 Aggr_Resolved :=
983 Resolve_Array_Aggregate
984 (N,
985 Index => First_Index (Aggr_Typ),
986 Index_Constr => First_Index (Typ),
987 Component_Typ => Component_Type (Typ),
988 Others_Allowed => True);
996ae0b0
RK
989 else
990 Aggr_Resolved :=
991 Resolve_Array_Aggregate
992 (N,
993 Index => First_Index (Aggr_Typ),
994 Index_Constr => First_Index (Aggr_Typ),
995 Component_Typ => Component_Type (Typ),
996 Others_Allowed => False);
997 end if;
998
999 if not Aggr_Resolved then
f5afb270
AC
1000
1001 -- A parenthesized expression may have been intended as an
1002 -- aggregate, leading to a type error when analyzing the
1003 -- component. This can also happen for a nested component
1004 -- (see Analyze_Aggr_Expr).
1005
1006 if Paren_Count (N) > 0 then
1007 Error_Msg_N
1008 ("positional aggregate cannot have one component", N);
1009 end if;
1010
996ae0b0 1011 Aggr_Subtyp := Any_Composite;
e917aec2 1012
996ae0b0
RK
1013 else
1014 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1015 end if;
1016
1017 Set_Etype (N, Aggr_Subtyp);
1018 end Array_Aggregate;
1019
d8387153
ES
1020 elsif Is_Private_Type (Typ)
1021 and then Present (Full_View (Typ))
8256c1bf 1022 and then (In_Inlined_Body or In_Instance_Body)
d8387153
ES
1023 and then Is_Composite_Type (Full_View (Typ))
1024 then
1025 Resolve (N, Full_View (Typ));
1026
996ae0b0
RK
1027 else
1028 Error_Msg_N ("illegal context for aggregate", N);
996ae0b0
RK
1029 end if;
1030
b87971f3
AC
1031 -- If we can determine statically that the evaluation of the aggregate
1032 -- raises Constraint_Error, then replace the aggregate with an
1033 -- N_Raise_Constraint_Error node, but set the Etype to the right
1034 -- aggregate subtype. Gigi needs this.
996ae0b0
RK
1035
1036 if Raises_Constraint_Error (N) then
1037 Aggr_Subtyp := Etype (N);
07fc65c4 1038 Rewrite (N,
bd65a2d7 1039 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
996ae0b0
RK
1040 Set_Raises_Constraint_Error (N);
1041 Set_Etype (N, Aggr_Subtyp);
1042 Set_Analyzed (N);
1043 end if;
d3820795 1044
22e89283 1045 Check_Function_Writable_Actuals (N);
996ae0b0
RK
1046 end Resolve_Aggregate;
1047
1048 -----------------------------
1049 -- Resolve_Array_Aggregate --
1050 -----------------------------
1051
1052 function Resolve_Array_Aggregate
1053 (N : Node_Id;
1054 Index : Node_Id;
1055 Index_Constr : Node_Id;
1056 Component_Typ : Entity_Id;
ca44152f 1057 Others_Allowed : Boolean) return Boolean
996ae0b0
RK
1058 is
1059 Loc : constant Source_Ptr := Sloc (N);
1060
1061 Failure : constant Boolean := False;
1062 Success : constant Boolean := True;
1063
1064 Index_Typ : constant Entity_Id := Etype (Index);
1065 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1066 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
b87971f3
AC
1067 -- The type of the index corresponding to the array sub-aggregate along
1068 -- with its low and upper bounds.
996ae0b0
RK
1069
1070 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1071 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1072 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
b87971f3 1073 -- Ditto for the base type
996ae0b0 1074
ef74daea
AC
1075 Others_Present : Boolean := False;
1076
1077 Nb_Choices : Nat := 0;
1078 -- Contains the overall number of named choices in this sub-aggregate
1079
996ae0b0
RK
1080 function Add (Val : Uint; To : Node_Id) return Node_Id;
1081 -- Creates a new expression node where Val is added to expression To.
1082 -- Tries to constant fold whenever possible. To must be an already
1083 -- analyzed expression.
1084
1085 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
f915704f
AC
1086 -- Checks that AH (the upper bound of an array aggregate) is less than
1087 -- or equal to BH (the upper bound of the index base type). If the check
1088 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1089 -- set, and AH is replaced with a duplicate of BH.
996ae0b0
RK
1090
1091 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1092 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
b87971f3 1093 -- warning if not and sets the Raises_Constraint_Error flag in N.
996ae0b0
RK
1094
1095 procedure Check_Length (L, H : Node_Id; Len : Uint);
1096 -- Checks that range L .. H contains at least Len elements. Emits a
b87971f3 1097 -- warning if not and sets the Raises_Constraint_Error flag in N.
996ae0b0
RK
1098
1099 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
ec53a6da 1100 -- Returns True if range L .. H is dynamic or null
996ae0b0
RK
1101
1102 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1103 -- Given expression node From, this routine sets OK to False if it
1104 -- cannot statically evaluate From. Otherwise it stores this static
1105 -- value into Value.
1106
1107 function Resolve_Aggr_Expr
1108 (Expr : Node_Id;
ca44152f 1109 Single_Elmt : Boolean) return Boolean;
12a13f01 1110 -- Resolves aggregate expression Expr. Returns False if resolution
996ae0b0 1111 -- fails. If Single_Elmt is set to False, the expression Expr may be
b87971f3 1112 -- used to initialize several array aggregate elements (this can happen
f915704f 1113 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
b87971f3
AC
1114 -- In this event we do not resolve Expr unless expansion is disabled.
1115 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
ca5af305
AC
1116 --
1117 -- NOTE: In the case of "... => <>", we pass the in the
1118 -- N_Component_Association node as Expr, since there is no Expression in
1119 -- that case, and we need a Sloc for the error message.
996ae0b0 1120
ef74daea
AC
1121 procedure Resolve_Iterated_Component_Association
1122 (N : Node_Id;
1123 Index_Typ : Entity_Id);
1124 -- For AI12-061
1125
996ae0b0
RK
1126 ---------
1127 -- Add --
1128 ---------
1129
1130 function Add (Val : Uint; To : Node_Id) return Node_Id is
1131 Expr_Pos : Node_Id;
1132 Expr : Node_Id;
1133 To_Pos : Node_Id;
1134
1135 begin
1136 if Raises_Constraint_Error (To) then
1137 return To;
1138 end if;
1139
1140 -- First test if we can do constant folding
1141
1142 if Compile_Time_Known_Value (To)
1143 or else Nkind (To) = N_Integer_Literal
1144 then
1145 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1146 Set_Is_Static_Expression (Expr_Pos);
1147 Set_Etype (Expr_Pos, Etype (To));
1148 Set_Analyzed (Expr_Pos, Analyzed (To));
1149
1150 if not Is_Enumeration_Type (Index_Typ) then
1151 Expr := Expr_Pos;
1152
1153 -- If we are dealing with enumeration return
1154 -- Index_Typ'Val (Expr_Pos)
1155
1156 else
1157 Expr :=
1158 Make_Attribute_Reference
1159 (Loc,
e4494292 1160 Prefix => New_Occurrence_Of (Index_Typ, Loc),
996ae0b0
RK
1161 Attribute_Name => Name_Val,
1162 Expressions => New_List (Expr_Pos));
1163 end if;
1164
1165 return Expr;
1166 end if;
1167
1168 -- If we are here no constant folding possible
1169
1170 if not Is_Enumeration_Type (Index_Base) then
1171 Expr :=
1172 Make_Op_Add (Loc,
f915704f
AC
1173 Left_Opnd => Duplicate_Subexpr (To),
1174 Right_Opnd => Make_Integer_Literal (Loc, Val));
996ae0b0
RK
1175
1176 -- If we are dealing with enumeration return
1177 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1178
1179 else
1180 To_Pos :=
1181 Make_Attribute_Reference
1182 (Loc,
e4494292 1183 Prefix => New_Occurrence_Of (Index_Typ, Loc),
996ae0b0
RK
1184 Attribute_Name => Name_Pos,
1185 Expressions => New_List (Duplicate_Subexpr (To)));
1186
1187 Expr_Pos :=
1188 Make_Op_Add (Loc,
58009744
AC
1189 Left_Opnd => To_Pos,
1190 Right_Opnd => Make_Integer_Literal (Loc, Val));
996ae0b0
RK
1191
1192 Expr :=
1193 Make_Attribute_Reference
1194 (Loc,
e4494292 1195 Prefix => New_Occurrence_Of (Index_Typ, Loc),
996ae0b0
RK
1196 Attribute_Name => Name_Val,
1197 Expressions => New_List (Expr_Pos));
f915704f
AC
1198
1199 -- If the index type has a non standard representation, the
1200 -- attributes 'Val and 'Pos expand into function calls and the
1201 -- resulting expression is considered non-safe for reevaluation
1202 -- by the backend. Relocate it into a constant temporary in order
1203 -- to make it safe for reevaluation.
1204
1205 if Has_Non_Standard_Rep (Etype (N)) then
1206 declare
1207 Def_Id : Entity_Id;
1208
1209 begin
1210 Def_Id := Make_Temporary (Loc, 'R', Expr);
1211 Set_Etype (Def_Id, Index_Typ);
1212 Insert_Action (N,
1213 Make_Object_Declaration (Loc,
1214 Defining_Identifier => Def_Id,
e4494292
RD
1215 Object_Definition =>
1216 New_Occurrence_Of (Index_Typ, Loc),
f915704f
AC
1217 Constant_Present => True,
1218 Expression => Relocate_Node (Expr)));
1219
e4494292 1220 Expr := New_Occurrence_Of (Def_Id, Loc);
f915704f
AC
1221 end;
1222 end if;
996ae0b0
RK
1223 end if;
1224
1225 return Expr;
1226 end Add;
1227
1228 -----------------
1229 -- Check_Bound --
1230 -----------------
1231
1232 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1233 Val_BH : Uint;
1234 Val_AH : Uint;
1235
1236 OK_BH : Boolean;
1237 OK_AH : Boolean;
1238
1239 begin
1240 Get (Value => Val_BH, From => BH, OK => OK_BH);
1241 Get (Value => Val_AH, From => AH, OK => OK_AH);
1242
1243 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1244 Set_Raises_Constraint_Error (N);
43417b90 1245 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1246 Error_Msg_N ("upper bound out of range<<", AH);
1247 Error_Msg_N ("\Constraint_Error [<<", AH);
996ae0b0
RK
1248
1249 -- You need to set AH to BH or else in the case of enumerations
3b42c566 1250 -- indexes we will not be able to resolve the aggregate bounds.
996ae0b0
RK
1251
1252 AH := Duplicate_Subexpr (BH);
1253 end if;
1254 end Check_Bound;
1255
1256 ------------------
1257 -- Check_Bounds --
1258 ------------------
1259
1260 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1261 Val_L : Uint;
1262 Val_H : Uint;
1263 Val_AL : Uint;
1264 Val_AH : Uint;
1265
f91e8020
GD
1266 OK_L : Boolean;
1267 OK_H : Boolean;
1268
996ae0b0 1269 OK_AL : Boolean;
f91e8020
GD
1270 OK_AH : Boolean;
1271 pragma Warnings (Off, OK_AL);
1272 pragma Warnings (Off, OK_AH);
996ae0b0
RK
1273
1274 begin
1275 if Raises_Constraint_Error (N)
1276 or else Dynamic_Or_Null_Range (AL, AH)
1277 then
1278 return;
1279 end if;
1280
1281 Get (Value => Val_L, From => L, OK => OK_L);
1282 Get (Value => Val_H, From => H, OK => OK_H);
1283
1284 Get (Value => Val_AL, From => AL, OK => OK_AL);
1285 Get (Value => Val_AH, From => AH, OK => OK_AH);
1286
1287 if OK_L and then Val_L > Val_AL then
1288 Set_Raises_Constraint_Error (N);
43417b90 1289 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1290 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1291 Error_Msg_N ("\Constraint_Error [<<", N);
996ae0b0
RK
1292 end if;
1293
1294 if OK_H and then Val_H < Val_AH then
1295 Set_Raises_Constraint_Error (N);
43417b90 1296 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1297 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1298 Error_Msg_N ("\Constraint_Error [<<", N);
996ae0b0
RK
1299 end if;
1300 end Check_Bounds;
1301
1302 ------------------
1303 -- Check_Length --
1304 ------------------
1305
1306 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1307 Val_L : Uint;
1308 Val_H : Uint;
1309
1310 OK_L : Boolean;
1311 OK_H : Boolean;
1312
1313 Range_Len : Uint;
1314
1315 begin
1316 if Raises_Constraint_Error (N) then
1317 return;
1318 end if;
1319
1320 Get (Value => Val_L, From => L, OK => OK_L);
1321 Get (Value => Val_H, From => H, OK => OK_H);
1322
1323 if not OK_L or else not OK_H then
1324 return;
1325 end if;
1326
1327 -- If null range length is zero
1328
1329 if Val_L > Val_H then
1330 Range_Len := Uint_0;
1331 else
1332 Range_Len := Val_H - Val_L + 1;
1333 end if;
1334
1335 if Range_Len < Len then
1336 Set_Raises_Constraint_Error (N);
43417b90 1337 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1338 Error_Msg_N ("too many elements<<", N);
1339 Error_Msg_N ("\Constraint_Error [<<", N);
996ae0b0
RK
1340 end if;
1341 end Check_Length;
1342
1343 ---------------------------
1344 -- Dynamic_Or_Null_Range --
1345 ---------------------------
1346
1347 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1348 Val_L : Uint;
1349 Val_H : Uint;
1350
1351 OK_L : Boolean;
1352 OK_H : Boolean;
1353
1354 begin
1355 Get (Value => Val_L, From => L, OK => OK_L);
1356 Get (Value => Val_H, From => H, OK => OK_H);
1357
1358 return not OK_L or else not OK_H
1359 or else not Is_OK_Static_Expression (L)
1360 or else not Is_OK_Static_Expression (H)
1361 or else Val_L > Val_H;
1362 end Dynamic_Or_Null_Range;
1363
1364 ---------
1365 -- Get --
1366 ---------
1367
1368 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1369 begin
1370 OK := True;
1371
1372 if Compile_Time_Known_Value (From) then
1373 Value := Expr_Value (From);
1374
1375 -- If expression From is something like Some_Type'Val (10) then
1f0b1e48 1376 -- Value = 10.
996ae0b0
RK
1377
1378 elsif Nkind (From) = N_Attribute_Reference
1379 and then Attribute_Name (From) = Name_Val
1380 and then Compile_Time_Known_Value (First (Expressions (From)))
1381 then
1382 Value := Expr_Value (First (Expressions (From)));
996ae0b0
RK
1383 else
1384 Value := Uint_0;
1385 OK := False;
1386 end if;
1387 end Get;
1388
1389 -----------------------
1390 -- Resolve_Aggr_Expr --
1391 -----------------------
1392
1393 function Resolve_Aggr_Expr
1394 (Expr : Node_Id;
ca44152f 1395 Single_Elmt : Boolean) return Boolean
996ae0b0 1396 is
fbf5a39b
AC
1397 Nxt_Ind : constant Node_Id := Next_Index (Index);
1398 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
12a13f01 1399 -- Index is the current index corresponding to the expression
996ae0b0
RK
1400
1401 Resolution_OK : Boolean := True;
ec53a6da 1402 -- Set to False if resolution of the expression failed
996ae0b0
RK
1403
1404 begin
199c6a10
AC
1405 -- Defend against previous errors
1406
1407 if Nkind (Expr) = N_Error
1408 or else Error_Posted (Expr)
1409 then
1410 return True;
1411 end if;
1412
996ae0b0
RK
1413 -- If the array type against which we are resolving the aggregate
1414 -- has several dimensions, the expressions nested inside the
1415 -- aggregate must be further aggregates (or strings).
1416
1417 if Present (Nxt_Ind) then
1418 if Nkind (Expr) /= N_Aggregate then
1419
1420 -- A string literal can appear where a one-dimensional array
1421 -- of characters is expected. If the literal looks like an
1422 -- operator, it is still an operator symbol, which will be
1423 -- transformed into a string when analyzed.
1424
1425 if Is_Character_Type (Component_Typ)
1426 and then No (Next_Index (Nxt_Ind))
f53f9dd7 1427 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
996ae0b0
RK
1428 then
1429 -- A string literal used in a multidimensional array
1430 -- aggregate in place of the final one-dimensional
1431 -- aggregate must not be enclosed in parentheses.
1432
1433 if Paren_Count (Expr) /= 0 then
ed2233dc 1434 Error_Msg_N ("no parenthesis allowed here", Expr);
996ae0b0
RK
1435 end if;
1436
1437 Make_String_Into_Aggregate (Expr);
1438
1439 else
1440 Error_Msg_N ("nested array aggregate expected", Expr);
9d0c3761
AC
1441
1442 -- If the expression is parenthesized, this may be
1443 -- a missing component association for a 1-aggregate.
1444
1445 if Paren_Count (Expr) > 0 then
ed2233dc 1446 Error_Msg_N
58009744
AC
1447 ("\if single-component aggregate is intended, "
1448 & "write e.g. (1 ='> ...)", Expr);
9d0c3761 1449 end if;
f5afb270 1450
996ae0b0
RK
1451 return Failure;
1452 end if;
1453 end if;
1454
ca5af305
AC
1455 -- If it's "... => <>", nothing to resolve
1456
1457 if Nkind (Expr) = N_Component_Association then
1458 pragma Assert (Box_Present (Expr));
1459 return Success;
1460 end if;
1461
0ab80019 1462 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
35b7fa6a
AC
1463 -- Required to check the null-exclusion attribute (if present).
1464 -- This value may be overridden later on.
1465
1466 Set_Etype (Expr, Etype (N));
1467
996ae0b0
RK
1468 Resolution_OK := Resolve_Array_Aggregate
1469 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1470
ca5af305 1471 else
ca5af305
AC
1472 -- If it's "... => <>", nothing to resolve
1473
1474 if Nkind (Expr) = N_Component_Association then
1475 pragma Assert (Box_Present (Expr));
1476 return Success;
1477 end if;
1478
1479 -- Do not resolve the expressions of discrete or others choices
1480 -- unless the expression covers a single component, or the
1481 -- expander is inactive.
1482
92b751fd 1483 -- In SPARK mode, expressions that can perform side effects will
06b599fd
YM
1484 -- be recognized by the gnat2why back-end, and the whole
1485 -- subprogram will be ignored. So semantic analysis can be
1486 -- performed safely.
9f8d1e5c 1487
ca5af305 1488 if Single_Elmt
4460a9bc 1489 or else not Expander_Active
ca5af305
AC
1490 or else In_Spec_Expression
1491 then
1492 Analyze_And_Resolve (Expr, Component_Typ);
1493 Check_Expr_OK_In_Limited_Aggregate (Expr);
1494 Check_Non_Static_Context (Expr);
1495 Aggregate_Constraint_Checks (Expr, Component_Typ);
1496 Check_Unset_Reference (Expr);
1497 end if;
996ae0b0
RK
1498 end if;
1499
dec6faf1
AC
1500 -- If an aggregate component has a type with predicates, an explicit
1501 -- predicate check must be applied, as for an assignment statement,
1502 -- because the aggegate might not be expanded into individual
07eb872e
AC
1503 -- component assignments. If the expression covers several components
1504 -- the analysis and the predicate check take place later.
dec6faf1 1505
619bfd9f 1506 if Has_Predicates (Component_Typ)
07eb872e
AC
1507 and then Analyzed (Expr)
1508 then
887d102a
AC
1509 Apply_Predicate_Check (Expr, Component_Typ);
1510 end if;
1511
996ae0b0
RK
1512 if Raises_Constraint_Error (Expr)
1513 and then Nkind (Parent (Expr)) /= N_Component_Association
1514 then
1515 Set_Raises_Constraint_Error (N);
1516 end if;
1517
d79e621a 1518 -- If the expression has been marked as requiring a range check,
edab6088
RD
1519 -- then generate it here. It's a bit odd to be generating such
1520 -- checks in the analyzer, but harmless since Generate_Range_Check
1521 -- does nothing (other than making sure Do_Range_Check is set) if
1522 -- the expander is not active.
d79e621a
GD
1523
1524 if Do_Range_Check (Expr) then
d79e621a
GD
1525 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1526 end if;
1527
996ae0b0
RK
1528 return Resolution_OK;
1529 end Resolve_Aggr_Expr;
1530
ef74daea
AC
1531 --------------------------------------------
1532 -- Resolve_Iterated_Component_Association --
1533 --------------------------------------------
1534
1535 procedure Resolve_Iterated_Component_Association
1536 (N : Node_Id;
1537 Index_Typ : Entity_Id)
1538 is
ef74daea
AC
1539 Loc : constant Source_Ptr := Sloc (N);
1540
1541 Choice : Node_Id;
1542 Dummy : Boolean;
1543 Ent : Entity_Id;
d940c627
ES
1544 Expr : Node_Id;
1545 Id : Entity_Id;
ef74daea
AC
1546
1547 begin
1548 Choice := First (Discrete_Choices (N));
1549
1550 while Present (Choice) loop
1551 if Nkind (Choice) = N_Others_Choice then
ef74daea
AC
1552 Others_Present := True;
1553
1554 else
b03d3f73
AC
1555 Analyze (Choice);
1556
2bb988bb 1557 -- Choice can be a subtype name, a range, or an expression
b03d3f73
AC
1558
1559 if Is_Entity_Name (Choice)
1560 and then Is_Type (Entity (Choice))
1561 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1562 then
1563 null;
1564
1565 else
1566 Analyze_And_Resolve (Choice, Index_Typ);
1567 end if;
ef74daea
AC
1568 end if;
1569
ef74daea
AC
1570 Next (Choice);
1571 end loop;
1572
1573 -- Create a scope in which to introduce an index, which is usually
b2c1aa8f
AC
1574 -- visible in the expression for the component, and needed for its
1575 -- analysis.
ef74daea
AC
1576
1577 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1578 Set_Etype (Ent, Standard_Void_Type);
1579 Set_Parent (Ent, Parent (N));
d940c627 1580 Push_Scope (Ent);
10fdda1c
HK
1581 Id :=
1582 Make_Defining_Identifier (Loc,
1583 Chars => Chars (Defining_Identifier (N)));
ef74daea 1584
d940c627 1585 -- Insert and decorate the index variable in the current scope.
b03d3f73 1586 -- The expression has to be analyzed once the index variable is
72a26637
AC
1587 -- directly visible. Mark the variable as referenced to prevent
1588 -- spurious warnings, given that subsequent uses of its name in the
1589 -- expression will reference the internal (synonym) loop variable.
9eb8d5b4 1590
d940c627
ES
1591 Enter_Name (Id);
1592 Set_Etype (Id, Index_Typ);
1593 Set_Ekind (Id, E_Variable);
1594 Set_Scope (Id, Ent);
1595 Set_Referenced (Id);
1596
1597 -- Analyze a copy of the expression, to verify legality. We use
1598 -- a copy because the expression will be analyzed anew when the
1599 -- enclosing aggregate is expanded, and the construct is rewritten
1600 -- as a loop with a new index variable.
1601
1602 Expr := New_Copy_Tree (Expression (N));
1603 Dummy := Resolve_Aggr_Expr (Expr, False);
1604
1605 -- An iterated_component_association may appear in a nested
1606 -- aggregate for a multidimensional structure: preserve the bounds
1607 -- computed for the expression, as well as the anonymous array
1608 -- type generated for it; both are needed during array expansion.
1609 -- This does not work for more than two levels of nesting. ???
1610
1611 if Nkind (Expr) = N_Aggregate then
1612 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1613 Set_Etype (Expression (N), Etype (Expr));
9eb8d5b4 1614 end if;
ef74daea 1615
ef74daea
AC
1616 End_Scope;
1617 end Resolve_Iterated_Component_Association;
1618
1619 -- Local variables
996ae0b0 1620
58009744
AC
1621 Assoc : Node_Id;
1622 Choice : Node_Id;
1623 Expr : Node_Id;
f91e8020 1624 Discard : Node_Id;
996ae0b0
RK
1625
1626 Aggr_Low : Node_Id := Empty;
1627 Aggr_High : Node_Id := Empty;
c7ce71c2 1628 -- The actual low and high bounds of this sub-aggregate
996ae0b0 1629
ef74daea
AC
1630 Case_Table_Size : Nat;
1631 -- Contains the size of the case table needed to sort aggregate choices
1632
996ae0b0
RK
1633 Choices_Low : Node_Id := Empty;
1634 Choices_High : Node_Id := Empty;
1635 -- The lowest and highest discrete choices values for a named aggregate
1636
ef74daea
AC
1637 Delete_Choice : Boolean;
1638 -- Used when replacing a subtype choice with predicate by a list
1639
996ae0b0 1640 Nb_Elements : Uint := Uint_0;
c7ce71c2 1641 -- The number of elements in a positional aggregate
996ae0b0 1642
996ae0b0
RK
1643 Nb_Discrete_Choices : Nat := 0;
1644 -- The overall number of discrete choices (not counting others choice)
1645
996ae0b0
RK
1646 -- Start of processing for Resolve_Array_Aggregate
1647
1648 begin
6d2a1120
RD
1649 -- Ignore junk empty aggregate resulting from parser error
1650
1651 if No (Expressions (N))
1652 and then No (Component_Associations (N))
1653 and then not Null_Record_Present (N)
1654 then
1655 return False;
1656 end if;
1657
996ae0b0
RK
1658 -- STEP 1: make sure the aggregate is correctly formatted
1659
1660 if Present (Component_Associations (N)) then
1661 Assoc := First (Component_Associations (N));
1662 while Present (Assoc) loop
ef74daea
AC
1663 if Nkind (Assoc) = N_Iterated_Component_Association then
1664 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
ef74daea
AC
1665 end if;
1666
b2c1aa8f 1667 Choice := First (Choice_List (Assoc));
2791be24 1668 Delete_Choice := False;
996ae0b0
RK
1669 while Present (Choice) loop
1670 if Nkind (Choice) = N_Others_Choice then
1671 Others_Present := True;
1672
b2c1aa8f 1673 if Choice /= First (Choice_List (Assoc))
996ae0b0
RK
1674 or else Present (Next (Choice))
1675 then
ed2233dc 1676 Error_Msg_N
996ae0b0
RK
1677 ("OTHERS must appear alone in a choice list", Choice);
1678 return Failure;
1679 end if;
1680
1681 if Present (Next (Assoc)) then
ed2233dc 1682 Error_Msg_N
996ae0b0
RK
1683 ("OTHERS must appear last in an aggregate", Choice);
1684 return Failure;
1685 end if;
1686
0ab80019 1687 if Ada_Version = Ada_83
996ae0b0 1688 and then Assoc /= First (Component_Associations (N))
f53f9dd7
RD
1689 and then Nkind_In (Parent (N), N_Assignment_Statement,
1690 N_Object_Declaration)
996ae0b0
RK
1691 then
1692 Error_Msg_N
1693 ("(Ada 83) illegal context for OTHERS choice", N);
1694 end if;
2791be24
AC
1695
1696 elsif Is_Entity_Name (Choice) then
1697 Analyze (Choice);
1698
1699 declare
1700 E : constant Entity_Id := Entity (Choice);
1701 New_Cs : List_Id;
1702 P : Node_Id;
1703 C : Node_Id;
1704
1705 begin
1706 if Is_Type (E) and then Has_Predicates (E) then
1707 Freeze_Before (N, E);
1708
24de083f 1709 if Has_Dynamic_Predicate_Aspect (E) then
b330e3c8
AC
1710 Error_Msg_NE
1711 ("subtype& has dynamic predicate, not allowed "
1712 & "in aggregate choice", Choice, E);
24de083f 1713
4b259b2d 1714 elsif not Is_OK_Static_Subtype (E) then
b330e3c8
AC
1715 Error_Msg_NE
1716 ("non-static subtype& has predicate, not allowed "
1717 & "in aggregate choice", Choice, E);
24de083f
AC
1718 end if;
1719
2791be24
AC
1720 -- If the subtype has a static predicate, replace the
1721 -- original choice with the list of individual values
65f1ca2e 1722 -- covered by the predicate.
3abbc5c2 1723 -- This should be deferred to expansion time ???
2791be24 1724
65f1ca2e 1725 if Present (Static_Discrete_Predicate (E)) then
2791be24
AC
1726 Delete_Choice := True;
1727
1728 New_Cs := New_List;
60f908dd 1729 P := First (Static_Discrete_Predicate (E));
2791be24
AC
1730 while Present (P) loop
1731 C := New_Copy (P);
1732 Set_Sloc (C, Sloc (Choice));
1733 Append_To (New_Cs, C);
1734 Next (P);
1735 end loop;
1736
1737 Insert_List_After (Choice, New_Cs);
1738 end if;
1739 end if;
1740 end;
996ae0b0
RK
1741 end if;
1742
1743 Nb_Choices := Nb_Choices + 1;
2791be24
AC
1744
1745 declare
1746 C : constant Node_Id := Choice;
1747
1748 begin
1749 Next (Choice);
1750
1751 if Delete_Choice then
1752 Remove (C);
1753 Nb_Choices := Nb_Choices - 1;
1754 Delete_Choice := False;
1755 end if;
1756 end;
996ae0b0
RK
1757 end loop;
1758
1759 Next (Assoc);
1760 end loop;
1761 end if;
1762
1763 -- At this point we know that the others choice, if present, is by
1764 -- itself and appears last in the aggregate. Check if we have mixed
1765 -- positional and discrete associations (other than the others choice).
1766
1767 if Present (Expressions (N))
1768 and then (Nb_Choices > 1
1769 or else (Nb_Choices = 1 and then not Others_Present))
1770 then
1771 Error_Msg_N
1772 ("named association cannot follow positional association",
ef74daea 1773 First (Choice_List (First (Component_Associations (N)))));
996ae0b0
RK
1774 return Failure;
1775 end if;
1776
1777 -- Test for the validity of an others choice if present
1778
1779 if Others_Present and then not Others_Allowed then
1780 Error_Msg_N
1781 ("OTHERS choice not allowed here",
1782 First (Choices (First (Component_Associations (N)))));
1783 return Failure;
1784 end if;
1785
07fc65c4
GB
1786 -- Protect against cascaded errors
1787
1788 if Etype (Index_Typ) = Any_Type then
1789 return Failure;
1790 end if;
1791
996ae0b0
RK
1792 -- STEP 2: Process named components
1793
1794 if No (Expressions (N)) then
996ae0b0
RK
1795 if Others_Present then
1796 Case_Table_Size := Nb_Choices - 1;
1797 else
1798 Case_Table_Size := Nb_Choices;
1799 end if;
1800
1801 Step_2 : declare
10edebe7
AC
1802 function Empty_Range (A : Node_Id) return Boolean;
1803 -- If an association covers an empty range, some warnings on the
1804 -- expression of the association can be disabled.
1805
1806 -----------------
1807 -- Empty_Range --
1808 -----------------
1809
1810 function Empty_Range (A : Node_Id) return Boolean is
1811 R : constant Node_Id := First (Choices (A));
1812 begin
1813 return No (Next (R))
1814 and then Nkind (R) = N_Range
1815 and then Compile_Time_Compare
1816 (Low_Bound (R), High_Bound (R), False) = GT;
1817 end Empty_Range;
1818
1819 -- Local variables
1820
996ae0b0
RK
1821 Low : Node_Id;
1822 High : Node_Id;
1823 -- Denote the lowest and highest values in an aggregate choice
1824
996ae0b0
RK
1825 S_Low : Node_Id := Empty;
1826 S_High : Node_Id := Empty;
1827 -- if a choice in an aggregate is a subtype indication these
1828 -- denote the lowest and highest values of the subtype
1829
38f44fd6
PT
1830 Table : Case_Table_Type (1 .. Case_Table_Size);
1831 -- Used to sort all the different choice values
996ae0b0
RK
1832
1833 Single_Choice : Boolean;
1834 -- Set to true every time there is a single discrete choice in a
1835 -- discrete association
1836
1837 Prev_Nb_Discrete_Choices : Nat;
b87971f3
AC
1838 -- Used to keep track of the number of discrete choices in the
1839 -- current association.
996ae0b0 1840
c0b11850
AC
1841 Errors_Posted_On_Choices : Boolean := False;
1842 -- Keeps track of whether any choices have semantic errors
1843
5a521b8a
AC
1844 -- Start of processing for Step_2
1845
996ae0b0 1846 begin
ec53a6da 1847 -- STEP 2 (A): Check discrete choices validity
996ae0b0
RK
1848
1849 Assoc := First (Component_Associations (N));
1850 while Present (Assoc) loop
996ae0b0 1851 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
ef74daea
AC
1852 Choice := First (Choice_List (Assoc));
1853
996ae0b0
RK
1854 loop
1855 Analyze (Choice);
1856
1857 if Nkind (Choice) = N_Others_Choice then
1858 Single_Choice := False;
1859 exit;
1860
1861 -- Test for subtype mark without constraint
1862
1863 elsif Is_Entity_Name (Choice) and then
1864 Is_Type (Entity (Choice))
1865 then
1866 if Base_Type (Entity (Choice)) /= Index_Base then
1867 Error_Msg_N
1868 ("invalid subtype mark in aggregate choice",
1869 Choice);
1870 return Failure;
1871 end if;
1872
ca44152f
ES
1873 -- Case of subtype indication
1874
996ae0b0
RK
1875 elsif Nkind (Choice) = N_Subtype_Indication then
1876 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1877
24de083f
AC
1878 if Has_Dynamic_Predicate_Aspect
1879 (Entity (Subtype_Mark (Choice)))
1880 then
58009744
AC
1881 Error_Msg_NE
1882 ("subtype& has dynamic predicate, "
1883 & "not allowed in aggregate choice",
1884 Choice, Entity (Subtype_Mark (Choice)));
24de083f
AC
1885 end if;
1886
324ac540 1887 -- Does the subtype indication evaluation raise CE?
996ae0b0
RK
1888
1889 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1890 Get_Index_Bounds (Choice, Low, High);
1891 Check_Bounds (S_Low, S_High, Low, High);
1892
ca44152f
ES
1893 -- Case of range or expression
1894
1895 else
996ae0b0 1896 Resolve (Choice, Index_Base);
fbf5a39b 1897 Check_Unset_Reference (Choice);
996ae0b0
RK
1898 Check_Non_Static_Context (Choice);
1899
c0b11850
AC
1900 -- If semantic errors were posted on the choice, then
1901 -- record that for possible early return from later
1902 -- processing (see handling of enumeration choices).
1903
1904 if Error_Posted (Choice) then
1905 Errors_Posted_On_Choices := True;
1906 end if;
1907
996ae0b0 1908 -- Do not range check a choice. This check is redundant
b87971f3
AC
1909 -- since this test is already done when we check that the
1910 -- bounds of the array aggregate are within range.
996ae0b0
RK
1911
1912 Set_Do_Range_Check (Choice, False);
1913 end if;
1914
1915 -- If we could not resolve the discrete choice stop here
1916
1917 if Etype (Choice) = Any_Type then
1918 return Failure;
1919
ec53a6da 1920 -- If the discrete choice raises CE get its original bounds
996ae0b0
RK
1921
1922 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1923 Set_Raises_Constraint_Error (N);
1924 Get_Index_Bounds (Original_Node (Choice), Low, High);
1925
1926 -- Otherwise get its bounds as usual
1927
1928 else
1929 Get_Index_Bounds (Choice, Low, High);
1930 end if;
1931
1932 if (Dynamic_Or_Null_Range (Low, High)
1933 or else (Nkind (Choice) = N_Subtype_Indication
1934 and then
1935 Dynamic_Or_Null_Range (S_Low, S_High)))
1936 and then Nb_Choices /= 1
1937 then
1938 Error_Msg_N
58009744
AC
1939 ("dynamic or empty choice in aggregate "
1940 & "must be the only choice", Choice);
996ae0b0
RK
1941 return Failure;
1942 end if;
1943
2f7ae2aa
BD
1944 if not (All_Composite_Constraints_Static (Low)
1945 and then All_Composite_Constraints_Static (High)
1946 and then All_Composite_Constraints_Static (S_Low)
1947 and then All_Composite_Constraints_Static (S_High))
1948 then
1949 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
1950 end if;
1951
996ae0b0 1952 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
82893775
AC
1953 Table (Nb_Discrete_Choices).Lo := Low;
1954 Table (Nb_Discrete_Choices).Hi := High;
1955 Table (Nb_Discrete_Choices).Choice := Choice;
996ae0b0
RK
1956
1957 Next (Choice);
1958
1959 if No (Choice) then
9b96e234 1960
996ae0b0
RK
1961 -- Check if we have a single discrete choice and whether
1962 -- this discrete choice specifies a single value.
1963
1964 Single_Choice :=
1965 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1966 and then (Low = High);
1967
1968 exit;
1969 end if;
1970 end loop;
1971
0ab80019 1972 -- Ada 2005 (AI-231)
2820d220 1973
0791fbe9 1974 if Ada_Version >= Ada_2005
8133b9d1 1975 and then Known_Null (Expression (Assoc))
5a521b8a 1976 and then not Empty_Range (Assoc)
ec53a6da 1977 then
82c80734
RD
1978 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1979 end if;
2820d220 1980
0ab80019 1981 -- Ada 2005 (AI-287): In case of default initialized component
b87971f3 1982 -- we delay the resolution to the expansion phase.
c45b6ae0
AC
1983
1984 if Box_Present (Assoc) then
1985
b87971f3
AC
1986 -- Ada 2005 (AI-287): In case of default initialization of a
1987 -- component the expander will generate calls to the
ca5af305
AC
1988 -- corresponding initialization subprogram. We need to call
1989 -- Resolve_Aggr_Expr to check the rules about
1990 -- dimensionality.
c45b6ae0 1991
882eadaf
RD
1992 if not Resolve_Aggr_Expr
1993 (Assoc, Single_Elmt => Single_Choice)
ca5af305
AC
1994 then
1995 return Failure;
1996 end if;
c45b6ae0 1997
9eb8d5b4
AC
1998 elsif Nkind (Assoc) = N_Iterated_Component_Association then
1999 null; -- handled above, in a loop context.
2000
882eadaf
RD
2001 elsif not Resolve_Aggr_Expr
2002 (Expression (Assoc), Single_Elmt => Single_Choice)
996ae0b0
RK
2003 then
2004 return Failure;
4755cce9
JM
2005
2006 -- Check incorrect use of dynamically tagged expression
2007
2008 -- We differentiate here two cases because the expression may
2009 -- not be decorated. For example, the analysis and resolution
b87971f3
AC
2010 -- of the expression associated with the others choice will be
2011 -- done later with the full aggregate. In such case we
4755cce9
JM
2012 -- duplicate the expression tree to analyze the copy and
2013 -- perform the required check.
2014
2015 elsif not Present (Etype (Expression (Assoc))) then
2016 declare
2017 Save_Analysis : constant Boolean := Full_Analysis;
2018 Expr : constant Node_Id :=
2019 New_Copy_Tree (Expression (Assoc));
2020
2021 begin
2022 Expander_Mode_Save_And_Set (False);
2023 Full_Analysis := False;
6ff6152d
ES
2024
2025 -- Analyze the expression, making sure it is properly
2026 -- attached to the tree before we do the analysis.
2027
2028 Set_Parent (Expr, Parent (Expression (Assoc)));
4755cce9 2029 Analyze (Expr);
094cefda 2030
a6ac7311
AC
2031 -- Compute its dimensions now, rather than at the end of
2032 -- resolution, because in the case of multidimensional
888be6b1
AC
2033 -- aggregates subsequent expansion may lead to spurious
2034 -- errors.
2035
2036 Check_Expression_Dimensions (Expr, Component_Typ);
2037
094cefda
AC
2038 -- If the expression is a literal, propagate this info
2039 -- to the expression in the association, to enable some
2040 -- optimizations downstream.
2041
2042 if Is_Entity_Name (Expr)
2043 and then Present (Entity (Expr))
2044 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2045 then
2046 Analyze_And_Resolve
2047 (Expression (Assoc), Component_Typ);
2048 end if;
2049
4755cce9
JM
2050 Full_Analysis := Save_Analysis;
2051 Expander_Mode_Restore;
2052
2053 if Is_Tagged_Type (Etype (Expr)) then
2054 Check_Dynamically_Tagged_Expression
2055 (Expr => Expr,
2056 Typ => Component_Type (Etype (N)),
2057 Related_Nod => N);
2058 end if;
2059 end;
2060
2061 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2062 Check_Dynamically_Tagged_Expression
1c612f29
RD
2063 (Expr => Expression (Assoc),
2064 Typ => Component_Type (Etype (N)),
4755cce9 2065 Related_Nod => N);
996ae0b0
RK
2066 end if;
2067
2068 Next (Assoc);
2069 end loop;
2070
2071 -- If aggregate contains more than one choice then these must be
882eadaf
RD
2072 -- static. Check for duplicate and missing values.
2073
2074 -- Note: there is duplicated code here wrt Check_Choice_Set in
2075 -- the body of Sem_Case, and it is possible we could just reuse
2076 -- that procedure. To be checked ???
996ae0b0
RK
2077
2078 if Nb_Discrete_Choices > 1 then
882eadaf
RD
2079 Check_Choices : declare
2080 Choice : Node_Id;
2081 -- Location of choice for messages
996ae0b0 2082
882eadaf
RD
2083 Hi_Val : Uint;
2084 Lo_Val : Uint;
2085 -- High end of one range and Low end of the next. Should be
2086 -- contiguous if there is no hole in the list of values.
996ae0b0 2087
82893775
AC
2088 Lo_Dup : Uint;
2089 Hi_Dup : Uint;
2090 -- End points of duplicated range
2091
882eadaf
RD
2092 Missing_Or_Duplicates : Boolean := False;
2093 -- Set True if missing or duplicate choices found
996ae0b0 2094
882eadaf
RD
2095 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2096 -- Output continuation message with a representation of the
2097 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2098 -- choice node where the message is to be posted.
996ae0b0 2099
882eadaf
RD
2100 ------------------------
2101 -- Output_Bad_Choices --
2102 ------------------------
996ae0b0 2103
882eadaf
RD
2104 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2105 begin
2106 -- Enumeration type case
996ae0b0 2107
882eadaf
RD
2108 if Is_Enumeration_Type (Index_Typ) then
2109 Error_Msg_Name_1 :=
2110 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2111 Error_Msg_Name_2 :=
2112 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2113
2114 if Lo = Hi then
2115 Error_Msg_N ("\\ %!", C);
2116 else
2117 Error_Msg_N ("\\ % .. %!", C);
996ae0b0
RK
2118 end if;
2119
882eadaf 2120 -- Integer types case
996ae0b0 2121
882eadaf
RD
2122 else
2123 Error_Msg_Uint_1 := Lo;
2124 Error_Msg_Uint_2 := Hi;
996ae0b0 2125
882eadaf
RD
2126 if Lo = Hi then
2127 Error_Msg_N ("\\ ^!", C);
2128 else
2129 Error_Msg_N ("\\ ^ .. ^!", C);
2130 end if;
2131 end if;
2132 end Output_Bad_Choices;
996ae0b0 2133
882eadaf 2134 -- Start of processing for Check_Choices
996ae0b0 2135
882eadaf
RD
2136 begin
2137 Sort_Case_Table (Table);
996ae0b0 2138
82893775
AC
2139 -- First we do a quick linear loop to find out if we have
2140 -- any duplicates or missing entries (usually we have a
2141 -- legal aggregate, so this will get us out quickly).
996ae0b0 2142
882eadaf 2143 for J in 1 .. Nb_Discrete_Choices - 1 loop
82893775
AC
2144 Hi_Val := Expr_Value (Table (J).Hi);
2145 Lo_Val := Expr_Value (Table (J + 1).Lo);
882eadaf 2146
82893775
AC
2147 if Lo_Val <= Hi_Val
2148 or else (Lo_Val > Hi_Val + 1
2149 and then not Others_Present)
2150 then
882eadaf 2151 Missing_Or_Duplicates := True;
82893775 2152 exit;
882eadaf
RD
2153 end if;
2154 end loop;
996ae0b0 2155
82893775
AC
2156 -- If we have missing or duplicate entries, first fill in
2157 -- the Highest entries to make life easier in the following
2158 -- loops to detect bad entries.
882eadaf 2159
82893775
AC
2160 if Missing_Or_Duplicates then
2161 Table (1).Highest := Expr_Value (Table (1).Hi);
882eadaf 2162
82893775
AC
2163 for J in 2 .. Nb_Discrete_Choices loop
2164 Table (J).Highest :=
2165 UI_Max
2166 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2167 end loop;
882eadaf 2168
82893775
AC
2169 -- Loop through table entries to find duplicate indexes
2170
2171 for J in 2 .. Nb_Discrete_Choices loop
2172 Lo_Val := Expr_Value (Table (J).Lo);
2173 Hi_Val := Expr_Value (Table (J).Hi);
2174
2175 -- Case where we have duplicates (the lower bound of
2176 -- this choice is less than or equal to the highest
2177 -- high bound found so far).
2178
2179 if Lo_Val <= Table (J - 1).Highest then
2180
2181 -- We move backwards looking for duplicates. We can
2182 -- abandon this loop as soon as we reach a choice
2183 -- highest value that is less than Lo_Val.
2184
2185 for K in reverse 1 .. J - 1 loop
2186 exit when Table (K).Highest < Lo_Val;
2187
2188 -- Here we may have duplicates between entries
2189 -- for K and J. Get range of duplicates.
2190
2191 Lo_Dup :=
2192 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2193 Hi_Dup :=
2194 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2195
2196 -- Nothing to do if duplicate range is null
882eadaf 2197
82893775
AC
2198 if Lo_Dup > Hi_Dup then
2199 null;
2200
65f1ca2e 2201 -- Otherwise place proper message
82893775
AC
2202
2203 else
2204 -- We place message on later choice, with a
2205 -- line reference to the earlier choice.
2206
2207 if Sloc (Table (J).Choice) <
2208 Sloc (Table (K).Choice)
2209 then
2210 Choice := Table (K).Choice;
2211 Error_Msg_Sloc := Sloc (Table (J).Choice);
2212 else
2213 Choice := Table (J).Choice;
2214 Error_Msg_Sloc := Sloc (Table (K).Choice);
2215 end if;
2216
2217 if Lo_Dup = Hi_Dup then
2218 Error_Msg_N
2219 ("index value in array aggregate "
2220 & "duplicates the one given#!", Choice);
2221 else
2222 Error_Msg_N
2223 ("index values in array aggregate "
2224 & "duplicate those given#!", Choice);
2225 end if;
2226
2227 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2228 end if;
2229 end loop;
996ae0b0 2230 end if;
882eadaf 2231 end loop;
996ae0b0 2232
82893775
AC
2233 -- Loop through entries in table to find missing indexes.
2234 -- Not needed if others, since missing impossible.
2235
2236 if not Others_Present then
2237 for J in 2 .. Nb_Discrete_Choices loop
2238 Lo_Val := Expr_Value (Table (J).Lo);
2239 Hi_Val := Table (J - 1).Highest;
2240
2241 if Lo_Val > Hi_Val + 1 then
16a569d2 2242
7b4ebba5
AC
2243 declare
2244 Error_Node : Node_Id;
16a569d2 2245
7b4ebba5
AC
2246 begin
2247 -- If the choice is the bound of a range in
2248 -- a subtype indication, it is not in the
2249 -- source lists for the aggregate itself, so
2250 -- post the error on the aggregate. Otherwise
2251 -- post it on choice itself.
16a569d2 2252
7b4ebba5 2253 Choice := Table (J).Choice;
16a569d2 2254
7b4ebba5
AC
2255 if Is_List_Member (Choice) then
2256 Error_Node := Choice;
2257 else
2258 Error_Node := N;
2259 end if;
82893775 2260
7b4ebba5
AC
2261 if Hi_Val + 1 = Lo_Val - 1 then
2262 Error_Msg_N
2263 ("missing index value "
2264 & "in array aggregate!", Error_Node);
2265 else
2266 Error_Msg_N
2267 ("missing index values "
2268 & "in array aggregate!", Error_Node);
2269 end if;
82893775 2270
7b4ebba5
AC
2271 Output_Bad_Choices
2272 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2273 end;
82893775
AC
2274 end if;
2275 end loop;
2276 end if;
2277
2278 -- If either missing or duplicate values, return failure
882eadaf 2279
882eadaf
RD
2280 Set_Etype (N, Any_Composite);
2281 return Failure;
2282 end if;
2283 end Check_Choices;
996ae0b0
RK
2284 end if;
2285
2286 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2287
2288 if Nb_Discrete_Choices > 0 then
82893775
AC
2289 Choices_Low := Table (1).Lo;
2290 Choices_High := Table (Nb_Discrete_Choices).Hi;
996ae0b0
RK
2291 end if;
2292
ca44152f
ES
2293 -- If Others is present, then bounds of aggregate come from the
2294 -- index constraint (not the choices in the aggregate itself).
2295
996ae0b0
RK
2296 if Others_Present then
2297 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2298
596b25f9
AC
2299 -- Abandon processing if either bound is already signalled as
2300 -- an error (prevents junk cascaded messages and blow ups).
2301
2302 if Nkind (Aggr_Low) = N_Error
2303 or else
2304 Nkind (Aggr_High) = N_Error
2305 then
2306 return False;
2307 end if;
2308
ca44152f
ES
2309 -- No others clause present
2310
996ae0b0 2311 else
ca44152f
ES
2312 -- Special processing if others allowed and not present. This
2313 -- means that the bounds of the aggregate come from the index
2314 -- constraint (and the length must match).
2315
2316 if Others_Allowed then
2317 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2318
596b25f9
AC
2319 -- Abandon processing if either bound is already signalled
2320 -- as an error (stop junk cascaded messages and blow ups).
2321
2322 if Nkind (Aggr_Low) = N_Error
2323 or else
2324 Nkind (Aggr_High) = N_Error
2325 then
2326 return False;
2327 end if;
2328
ca44152f
ES
2329 -- If others allowed, and no others present, then the array
2330 -- should cover all index values. If it does not, we will
2331 -- get a length check warning, but there is two cases where
2332 -- an additional warning is useful:
2333
2334 -- If we have no positional components, and the length is
2335 -- wrong (which we can tell by others being allowed with
2336 -- missing components), and the index type is an enumeration
2337 -- type, then issue appropriate warnings about these missing
2338 -- components. They are only warnings, since the aggregate
2339 -- is fine, it's just the wrong length. We skip this check
2340 -- for standard character types (since there are no literals
2341 -- and it is too much trouble to concoct them), and also if
50ea6357
AC
2342 -- any of the bounds have values that are not known at
2343 -- compile time.
ca44152f 2344
58009744
AC
2345 -- Another case warranting a warning is when the length
2346 -- is right, but as above we have an index type that is
2347 -- an enumeration, and the bounds do not match. This is a
2348 -- case where dubious sliding is allowed and we generate a
2349 -- warning that the bounds do not match.
ca44152f
ES
2350
2351 if No (Expressions (N))
2352 and then Nkind (Index) = N_Range
2353 and then Is_Enumeration_Type (Etype (Index))
2354 and then not Is_Standard_Character_Type (Etype (Index))
2355 and then Compile_Time_Known_Value (Aggr_Low)
2356 and then Compile_Time_Known_Value (Aggr_High)
2357 and then Compile_Time_Known_Value (Choices_Low)
2358 and then Compile_Time_Known_Value (Choices_High)
2359 then
c0b11850
AC
2360 -- If any of the expressions or range bounds in choices
2361 -- have semantic errors, then do not attempt further
2362 -- resolution, to prevent cascaded errors.
d610088d 2363
c0b11850
AC
2364 if Errors_Posted_On_Choices then
2365 return Failure;
d610088d
AC
2366 end if;
2367
ca44152f
ES
2368 declare
2369 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2370 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2371 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2372 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2373
2374 Ent : Entity_Id;
2375
2376 begin
ebd34478 2377 -- Warning case 1, missing values at start/end. Only
ca44152f
ES
2378 -- do the check if the number of entries is too small.
2379
2380 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2381 <
2382 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2383 then
2384 Error_Msg_N
324ac540
AC
2385 ("missing index value(s) in array aggregate??",
2386 N);
ca44152f
ES
2387
2388 -- Output missing value(s) at start
2389
2390 if Chars (ALo) /= Chars (CLo) then
2391 Ent := Prev (CLo);
2392
2393 if Chars (ALo) = Chars (Ent) then
2394 Error_Msg_Name_1 := Chars (ALo);
324ac540 2395 Error_Msg_N ("\ %??", N);
ca44152f
ES
2396 else
2397 Error_Msg_Name_1 := Chars (ALo);
2398 Error_Msg_Name_2 := Chars (Ent);
324ac540 2399 Error_Msg_N ("\ % .. %??", N);
ca44152f
ES
2400 end if;
2401 end if;
2402
2403 -- Output missing value(s) at end
2404
2405 if Chars (AHi) /= Chars (CHi) then
2406 Ent := Next (CHi);
2407
2408 if Chars (AHi) = Chars (Ent) then
2409 Error_Msg_Name_1 := Chars (Ent);
324ac540 2410 Error_Msg_N ("\ %??", N);
ca44152f
ES
2411 else
2412 Error_Msg_Name_1 := Chars (Ent);
2413 Error_Msg_Name_2 := Chars (AHi);
324ac540 2414 Error_Msg_N ("\ % .. %??", N);
ca44152f
ES
2415 end if;
2416 end if;
2417
2418 -- Warning case 2, dubious sliding. The First_Subtype
2419 -- test distinguishes between a constrained type where
2420 -- sliding is not allowed (so we will get a warning
2421 -- later that Constraint_Error will be raised), and
2422 -- the unconstrained case where sliding is permitted.
2423
2424 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2425 =
2426 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2427 and then Chars (ALo) /= Chars (CLo)
2428 and then
2429 not Is_Constrained (First_Subtype (Etype (N)))
2430 then
2431 Error_Msg_N
324ac540 2432 ("bounds of aggregate do not match target??", N);
ca44152f
ES
2433 end if;
2434 end;
2435 end if;
2436 end if;
2437
f3d0f304 2438 -- If no others, aggregate bounds come from aggregate
ca44152f 2439
996ae0b0
RK
2440 Aggr_Low := Choices_Low;
2441 Aggr_High := Choices_High;
2442 end if;
2443 end Step_2;
2444
2445 -- STEP 3: Process positional components
2446
2447 else
2448 -- STEP 3 (A): Process positional elements
2449
2450 Expr := First (Expressions (N));
2451 Nb_Elements := Uint_0;
2452 while Present (Expr) loop
2453 Nb_Elements := Nb_Elements + 1;
2454
82c80734
RD
2455 -- Ada 2005 (AI-231)
2456
58009744 2457 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
82c80734
RD
2458 Check_Can_Never_Be_Null (Etype (N), Expr);
2459 end if;
2820d220 2460
ef74daea 2461 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
996ae0b0
RK
2462 return Failure;
2463 end if;
2464
4755cce9
JM
2465 -- Check incorrect use of dynamically tagged expression
2466
2467 if Is_Tagged_Type (Etype (Expr)) then
2468 Check_Dynamically_Tagged_Expression
2469 (Expr => Expr,
2470 Typ => Component_Type (Etype (N)),
2471 Related_Nod => N);
2472 end if;
2473
996ae0b0
RK
2474 Next (Expr);
2475 end loop;
2476
2477 if Others_Present then
2478 Assoc := Last (Component_Associations (N));
c45b6ae0 2479
82c80734
RD
2480 -- Ada 2005 (AI-231)
2481
58009744 2482 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
9b96e234 2483 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
82c80734 2484 end if;
2820d220 2485
ebd34478 2486 -- Ada 2005 (AI-287): In case of default initialized component,
c45b6ae0
AC
2487 -- we delay the resolution to the expansion phase.
2488
2489 if Box_Present (Assoc) then
2490
ebd34478
AC
2491 -- Ada 2005 (AI-287): In case of default initialization of a
2492 -- component the expander will generate calls to the
ca5af305
AC
2493 -- corresponding initialization subprogram. We need to call
2494 -- Resolve_Aggr_Expr to check the rules about
2495 -- dimensionality.
c45b6ae0 2496
ca5af305
AC
2497 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2498 return Failure;
2499 end if;
c45b6ae0
AC
2500
2501 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2502 Single_Elmt => False)
996ae0b0
RK
2503 then
2504 return Failure;
4755cce9
JM
2505
2506 -- Check incorrect use of dynamically tagged expression. The
2507 -- expression of the others choice has not been resolved yet.
2508 -- In order to diagnose the semantic error we create a duplicate
2509 -- tree to analyze it and perform the check.
2510
2511 else
2512 declare
2513 Save_Analysis : constant Boolean := Full_Analysis;
2514 Expr : constant Node_Id :=
2515 New_Copy_Tree (Expression (Assoc));
2516
2517 begin
2518 Expander_Mode_Save_And_Set (False);
2519 Full_Analysis := False;
2520 Analyze (Expr);
2521 Full_Analysis := Save_Analysis;
2522 Expander_Mode_Restore;
2523
2524 if Is_Tagged_Type (Etype (Expr)) then
2525 Check_Dynamically_Tagged_Expression
58009744
AC
2526 (Expr => Expr,
2527 Typ => Component_Type (Etype (N)),
4755cce9
JM
2528 Related_Nod => N);
2529 end if;
2530 end;
996ae0b0
RK
2531 end if;
2532 end if;
2533
2534 -- STEP 3 (B): Compute the aggregate bounds
2535
2536 if Others_Present then
2537 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2538
2539 else
2540 if Others_Allowed then
f91e8020 2541 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
996ae0b0
RK
2542 else
2543 Aggr_Low := Index_Typ_Low;
2544 end if;
2545
2546 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2547 Check_Bound (Index_Base_High, Aggr_High);
2548 end if;
2549 end if;
2550
2551 -- STEP 4: Perform static aggregate checks and save the bounds
2552
2553 -- Check (A)
2554
2555 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2556 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2557
2558 -- Check (B)
2559
2560 if Others_Present and then Nb_Discrete_Choices > 0 then
2561 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2562 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2563 Choices_Low, Choices_High);
2564 Check_Bounds (Index_Base_Low, Index_Base_High,
2565 Choices_Low, Choices_High);
2566
2567 -- Check (C)
2568
2569 elsif Others_Present and then Nb_Elements > 0 then
2570 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2571 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2572 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
996ae0b0
RK
2573 end if;
2574
2575 if Raises_Constraint_Error (Aggr_Low)
2576 or else Raises_Constraint_Error (Aggr_High)
2577 then
2578 Set_Raises_Constraint_Error (N);
2579 end if;
2580
2581 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2582
2583 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2584 -- since the addition node returned by Add is not yet analyzed. Attach
ebd34478 2585 -- to tree and analyze first. Reset analyzed flag to ensure it will get
9b96e234 2586 -- analyzed when it is a literal bound whose type must be properly set.
996ae0b0
RK
2587
2588 if Others_Present or else Nb_Discrete_Choices > 0 then
2589 Aggr_High := Duplicate_Subexpr (Aggr_High);
2590
2591 if Etype (Aggr_High) = Universal_Integer then
2592 Set_Analyzed (Aggr_High, False);
2593 end if;
2594 end if;
2595
3d923671
AC
2596 -- If the aggregate already has bounds attached to it, it means this is
2597 -- a positional aggregate created as an optimization by
2598 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2599 -- bounds.
2600
2601 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
ebd34478 2602 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3d923671
AC
2603 Aggr_High := High_Bound (Aggregate_Bounds (N));
2604 end if;
2605
996ae0b0
RK
2606 Set_Aggregate_Bounds
2607 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2608
2609 -- The bounds may contain expressions that must be inserted upwards.
2610 -- Attach them fully to the tree. After analysis, remove side effects
2611 -- from upper bound, if still needed.
2612
2613 Set_Parent (Aggregate_Bounds (N), N);
2614 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
fbf5a39b 2615 Check_Unset_Reference (Aggregate_Bounds (N));
996ae0b0
RK
2616
2617 if not Others_Present and then Nb_Discrete_Choices = 0 then
82893775
AC
2618 Set_High_Bound
2619 (Aggregate_Bounds (N),
2620 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
996ae0b0
RK
2621 end if;
2622
d976bf74 2623 -- Check the dimensions of each component in the array aggregate
0929eaeb
AC
2624
2625 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2626
996ae0b0
RK
2627 return Success;
2628 end Resolve_Array_Aggregate;
2629
9eb8d5b4
AC
2630 -----------------------------
2631 -- Resolve_Delta_Aggregate --
2632 -----------------------------
2633
2634 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
d63199d8 2635 Base : constant Node_Id := Expression (N);
8d9a1ba7
PMR
2636
2637 begin
32501d71
BD
2638 if Ada_Version < Ada_2020 then
2639 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
a4c17870 2640 Error_Msg_N ("\compile with -gnat2020", N);
32501d71
BD
2641 end if;
2642
8d9a1ba7
PMR
2643 if not Is_Composite_Type (Typ) then
2644 Error_Msg_N ("not a composite type", N);
2645 end if;
2646
2647 Analyze_And_Resolve (Base, Typ);
2648
2649 if Is_Array_Type (Typ) then
2650 Resolve_Delta_Array_Aggregate (N, Typ);
2651 else
2652 Resolve_Delta_Record_Aggregate (N, Typ);
2653 end if;
2654
2655 Set_Etype (N, Typ);
2656 end Resolve_Delta_Aggregate;
2657
2658 -----------------------------------
2659 -- Resolve_Delta_Array_Aggregate --
2660 -----------------------------------
2661
2662 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
9313a26a 2663 Deltas : constant List_Id := Component_Associations (N);
d63199d8 2664
8d9a1ba7
PMR
2665 Assoc : Node_Id;
2666 Choice : Node_Id;
2667 Index_Type : Entity_Id;
9eb8d5b4 2668
8d9a1ba7
PMR
2669 begin
2670 Index_Type := Etype (First_Index (Typ));
d63199d8 2671
8d9a1ba7
PMR
2672 Assoc := First (Deltas);
2673 while Present (Assoc) loop
2674 if Nkind (Assoc) = N_Iterated_Component_Association then
2675 Choice := First (Choice_List (Assoc));
2676 while Present (Choice) loop
2677 if Nkind (Choice) = N_Others_Choice then
2678 Error_Msg_N
2679 ("others not allowed in delta aggregate", Choice);
9eb8d5b4 2680
8d9a1ba7
PMR
2681 else
2682 Analyze_And_Resolve (Choice, Index_Type);
2683 end if;
9eb8d5b4 2684
8d9a1ba7
PMR
2685 Next (Choice);
2686 end loop;
9eb8d5b4 2687
8d9a1ba7
PMR
2688 declare
2689 Id : constant Entity_Id := Defining_Identifier (Assoc);
2690 Ent : constant Entity_Id :=
2691 New_Internal_Entity
2692 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
9eb8d5b4 2693
8d9a1ba7
PMR
2694 begin
2695 Set_Etype (Ent, Standard_Void_Type);
2696 Set_Parent (Ent, Assoc);
2697
2698 if No (Scope (Id)) then
2699 Enter_Name (Id);
2700 Set_Etype (Id, Index_Type);
2701 Set_Ekind (Id, E_Variable);
2702 Set_Scope (Id, Ent);
9eb8d5b4
AC
2703 end if;
2704
8d9a1ba7
PMR
2705 Push_Scope (Ent);
2706 Analyze_And_Resolve
2707 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2708 End_Scope;
2709 end;
9eb8d5b4 2710
8d9a1ba7
PMR
2711 else
2712 Choice := First (Choice_List (Assoc));
2713 while Present (Choice) loop
2714 if Nkind (Choice) = N_Others_Choice then
2715 Error_Msg_N
2716 ("others not allowed in delta aggregate", Choice);
9eb8d5b4 2717
8d9a1ba7
PMR
2718 else
2719 Analyze (Choice);
d63199d8 2720
8d9a1ba7
PMR
2721 if Is_Entity_Name (Choice)
2722 and then Is_Type (Entity (Choice))
2723 then
d63199d8
PMR
2724 -- Choice covers a range of values
2725
8d9a1ba7
PMR
2726 if Base_Type (Entity (Choice)) /=
2727 Base_Type (Index_Type)
2728 then
2729 Error_Msg_NE
2730 ("choice does mat match index type of",
2731 Choice, Typ);
2732 end if;
2733 else
2734 Resolve (Choice, Index_Type);
2735 end if;
2736 end if;
9eb8d5b4 2737
8d9a1ba7
PMR
2738 Next (Choice);
2739 end loop;
2740
2741 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2742 end if;
9313a26a 2743
8d9a1ba7
PMR
2744 Next (Assoc);
2745 end loop;
2746 end Resolve_Delta_Array_Aggregate;
2747
2748 ------------------------------------
2749 -- Resolve_Delta_Record_Aggregate --
2750 ------------------------------------
2751
3ec54569
PMR
2752 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2753
2754 -- Variables used to verify that discriminant-dependent components
2755 -- appear in the same variant.
2756
5612989e 2757 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3ec54569
PMR
2758 Variant : Node_Id;
2759
8d9a1ba7
PMR
2760 procedure Check_Variant (Id : Entity_Id);
2761 -- If a given component of the delta aggregate appears in a variant
2762 -- part, verify that it is within the same variant as that of previous
2763 -- specified variant components of the delta.
9313a26a 2764
484d58c5
PT
2765 function Get_Component (Nam : Node_Id) return Entity_Id;
2766 -- Locate component with a given name and return it. If none found then
2767 -- report error and return Empty.
d63199d8
PMR
2768
2769 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2770 -- Determine whether variant V1 is within variant V2
9eb8d5b4 2771
8d9a1ba7
PMR
2772 function Variant_Depth (N : Node_Id) return Integer;
2773 -- Determine the distance of a variant to the enclosing type
2774 -- declaration.
2775
2776 --------------------
2777 -- Check_Variant --
2778 --------------------
2779
2780 procedure Check_Variant (Id : Entity_Id) is
2781 Comp : Entity_Id;
2782 Comp_Variant : Node_Id;
2783
2784 begin
2785 if not Has_Discriminants (Typ) then
2786 return;
2787 end if;
9eb8d5b4 2788
8d9a1ba7
PMR
2789 Comp := First_Entity (Typ);
2790 while Present (Comp) loop
2791 exit when Chars (Comp) = Chars (Id);
2792 Next_Component (Comp);
2793 end loop;
2794
2795 -- Find the variant, if any, whose component list includes the
2796 -- component declaration.
2797
2798 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2799 if Nkind (Comp_Variant) = N_Variant then
2800 if No (Variant) then
2801 Variant := Comp_Variant;
2802 Comp_Ref := Comp;
2803
2804 elsif Variant /= Comp_Variant then
9eb8d5b4 2805 declare
8d9a1ba7
PMR
2806 D1 : constant Integer := Variant_Depth (Variant);
2807 D2 : constant Integer := Variant_Depth (Comp_Variant);
9eb8d5b4
AC
2808
2809 begin
8d9a1ba7
PMR
2810 if D1 = D2
2811 or else
d63199d8 2812 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
8d9a1ba7 2813 or else
d63199d8 2814 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
8d9a1ba7 2815 then
5612989e 2816 pragma Assert (Present (Comp_Ref));
8d9a1ba7
PMR
2817 Error_Msg_Node_2 := Comp_Ref;
2818 Error_Msg_NE
2819 ("& and & appear in different variants", Id, Comp);
2820
2821 -- Otherwise retain the deeper variant for subsequent tests
9eb8d5b4 2822
8d9a1ba7
PMR
2823 elsif D2 > D1 then
2824 Variant := Comp_Variant;
2825 end if;
9eb8d5b4 2826 end;
8d9a1ba7
PMR
2827 end if;
2828 end if;
2829 end Check_Variant;
9eb8d5b4 2830
484d58c5
PT
2831 -------------------
2832 -- Get_Component --
2833 -------------------
d63199d8 2834
484d58c5 2835 function Get_Component (Nam : Node_Id) return Entity_Id is
d63199d8
PMR
2836 Comp : Entity_Id;
2837
2838 begin
2839 Comp := First_Entity (Typ);
2840 while Present (Comp) loop
2841 if Chars (Comp) = Chars (Nam) then
2842 if Ekind (Comp) = E_Discriminant then
2843 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2844 end if;
2845
484d58c5 2846 return Comp;
d63199d8
PMR
2847 end if;
2848
99859ea7 2849 Next_Entity (Comp);
d63199d8
PMR
2850 end loop;
2851
2852 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
484d58c5
PT
2853 return Empty;
2854 end Get_Component;
d63199d8 2855
8d9a1ba7
PMR
2856 ---------------
2857 -- Nested_In --
2858 ---------------
9eb8d5b4 2859
8d9a1ba7
PMR
2860 function Nested_In (V1, V2 : Node_Id) return Boolean is
2861 Par : Node_Id;
d63199d8 2862
8d9a1ba7
PMR
2863 begin
2864 Par := Parent (V1);
2865 while Nkind (Par) /= N_Full_Type_Declaration loop
2866 if Par = V2 then
2867 return True;
2868 end if;
d63199d8 2869
8d9a1ba7
PMR
2870 Par := Parent (Par);
2871 end loop;
9eb8d5b4 2872
8d9a1ba7
PMR
2873 return False;
2874 end Nested_In;
2875
2876 -------------------
2877 -- Variant_Depth --
2878 -------------------
9eb8d5b4 2879
8d9a1ba7
PMR
2880 function Variant_Depth (N : Node_Id) return Integer is
2881 Depth : Integer;
2882 Par : Node_Id;
d63199d8 2883
8d9a1ba7
PMR
2884 begin
2885 Depth := 0;
2886 Par := Parent (N);
2887 while Nkind (Par) /= N_Full_Type_Declaration loop
2888 Depth := Depth + 1;
d63199d8 2889 Par := Parent (Par);
8d9a1ba7
PMR
2890 end loop;
2891
2892 return Depth;
2893 end Variant_Depth;
2894
d63199d8 2895 -- Local variables
9eb8d5b4 2896
d63199d8 2897 Deltas : constant List_Id := Component_Associations (N);
9eb8d5b4 2898
d63199d8
PMR
2899 Assoc : Node_Id;
2900 Choice : Node_Id;
484d58c5 2901 Comp : Entity_Id;
5612989e 2902 Comp_Type : Entity_Id := Empty; -- init to avoid warning
9eb8d5b4 2903
8d9a1ba7
PMR
2904 -- Start of processing for Resolve_Delta_Record_Aggregate
2905
2906 begin
3ec54569
PMR
2907 Variant := Empty;
2908
8d9a1ba7 2909 Assoc := First (Deltas);
8d9a1ba7
PMR
2910 while Present (Assoc) loop
2911 Choice := First (Choice_List (Assoc));
2912 while Present (Choice) loop
484d58c5 2913 Comp := Get_Component (Choice);
d63199d8 2914
484d58c5 2915 if Present (Comp) then
8d9a1ba7 2916 Check_Variant (Choice);
484d58c5
PT
2917
2918 Comp_Type := Etype (Comp);
2919
2920 -- Decorate the component reference by setting its entity and
2921 -- type, as otherwise backends like GNATprove would have to
2922 -- rediscover this information by themselves.
2923
2924 Set_Entity (Choice, Comp);
2925 Set_Etype (Choice, Comp_Type);
2926 else
2927 Comp_Type := Any_Type;
8d9a1ba7
PMR
2928 end if;
2929
2930 Next (Choice);
9eb8d5b4 2931 end loop;
9eb8d5b4 2932
5612989e 2933 pragma Assert (Present (Comp_Type));
8d9a1ba7
PMR
2934 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
2935 Next (Assoc);
2936 end loop;
2937 end Resolve_Delta_Record_Aggregate;
9eb8d5b4 2938
996ae0b0
RK
2939 ---------------------------------
2940 -- Resolve_Extension_Aggregate --
2941 ---------------------------------
2942
2943 -- There are two cases to consider:
2944
ebd34478
AC
2945 -- a) If the ancestor part is a type mark, the components needed are the
2946 -- difference between the components of the expected type and the
996ae0b0
RK
2947 -- components of the given type mark.
2948
ebd34478 2949 -- b) If the ancestor part is an expression, it must be unambiguous, and
12f0c50c 2950 -- once we have its type we can also compute the needed components as in
ebd34478
AC
2951 -- the previous case. In both cases, if the ancestor type is not the
2952 -- immediate ancestor, we have to build this ancestor recursively.
996ae0b0 2953
12f0c50c 2954 -- In both cases, discriminants of the ancestor type do not play a role in
ebd34478
AC
2955 -- the resolution of the needed components, because inherited discriminants
2956 -- cannot be used in a type extension. As a result we can compute
2957 -- independently the list of components of the ancestor type and of the
2958 -- expected type.
996ae0b0
RK
2959
2960 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
2961 A : constant Node_Id := Ancestor_Part (N);
2962 A_Type : Entity_Id;
2963 I : Interp_Index;
2964 It : Interp;
996ae0b0 2965
ca44152f
ES
2966 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2967 -- If the type is limited, verify that the ancestor part is a legal
ebd34478
AC
2968 -- expression (aggregate or function call, including 'Input)) that does
2969 -- not require a copy, as specified in 7.5(2).
ca44152f 2970
996ae0b0
RK
2971 function Valid_Ancestor_Type return Boolean;
2972 -- Verify that the type of the ancestor part is a non-private ancestor
1543e3ab 2973 -- of the expected type, which must be a type extension.
996ae0b0 2974
5168a9b3
PMR
2975 procedure Transform_BIP_Assignment (Typ : Entity_Id);
2976 -- For an extension aggregate whose ancestor part is a build-in-place
2977 -- call returning a nonlimited type, this is used to transform the
2978 -- assignment to the ancestor part to use a temp.
2979
ca44152f
ES
2980 ----------------------------
2981 -- Valid_Limited_Ancestor --
2982 ----------------------------
2983
2984 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2985 begin
d8d7e809
AC
2986 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
2987 return True;
2988
2989 -- The ancestor must be a call or an aggregate, but a call may
2990 -- have been expanded into a temporary, so check original node.
2991
2992 elsif Nkind_In (Anc, N_Aggregate,
2993 N_Extension_Aggregate,
2994 N_Function_Call)
ca44152f
ES
2995 then
2996 return True;
2997
d8d7e809 2998 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
ca44152f
ES
2999 return True;
3000
3001 elsif Nkind (Anc) = N_Attribute_Reference
3002 and then Attribute_Name (Anc) = Name_Input
3003 then
3004 return True;
3005
ebd34478 3006 elsif Nkind (Anc) = N_Qualified_Expression then
ca44152f
ES
3007 return Valid_Limited_Ancestor (Expression (Anc));
3008
32794080
JM
3009 elsif Nkind (Anc) = N_Raise_Expression then
3010 return True;
3011
ca44152f
ES
3012 else
3013 return False;
3014 end if;
3015 end Valid_Limited_Ancestor;
3016
fbf5a39b
AC
3017 -------------------------
3018 -- Valid_Ancestor_Type --
3019 -------------------------
3020
996ae0b0
RK
3021 function Valid_Ancestor_Type return Boolean is
3022 Imm_Type : Entity_Id;
3023
3024 begin
3025 Imm_Type := Base_Type (Typ);
2af92e28
ES
3026 while Is_Derived_Type (Imm_Type) loop
3027 if Etype (Imm_Type) = Base_Type (A_Type) then
3028 return True;
3029
21d7ef70 3030 -- The base type of the parent type may appear as a private
ebd34478
AC
3031 -- extension if it is declared as such in a parent unit of the
3032 -- current one. For consistency of the subsequent analysis use
3033 -- the partial view for the ancestor part.
2af92e28
ES
3034
3035 elsif Is_Private_Type (Etype (Imm_Type))
3036 and then Present (Full_View (Etype (Imm_Type)))
3037 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3038 then
3039 A_Type := Etype (Imm_Type);
3040 return True;
4519314c
AC
3041
3042 -- The parent type may be a private extension. The aggregate is
3043 -- legal if the type of the aggregate is an extension of it that
3044 -- is not a private extension.
3045
3046 elsif Is_Private_Type (A_Type)
3047 and then not Is_Private_Type (Imm_Type)
3048 and then Present (Full_View (A_Type))
3049 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3050 then
3051 return True;
3052
32794080
JM
3053 -- The parent type may be a raise expression (which is legal in
3054 -- any expression context).
3055
3056 elsif A_Type = Raise_Type then
3057 A_Type := Etype (Imm_Type);
3058 return True;
3059
2af92e28
ES
3060 else
3061 Imm_Type := Etype (Base_Type (Imm_Type));
3062 end if;
996ae0b0
RK
3063 end loop;
3064
6d2a1120 3065 -- If previous loop did not find a proper ancestor, report error
2af92e28
ES
3066
3067 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3068 return False;
996ae0b0
RK
3069 end Valid_Ancestor_Type;
3070
3fc40cd7
PMR
3071 ------------------------------
3072 -- Transform_BIP_Assignment --
3073 ------------------------------
3074
5168a9b3 3075 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3fc40cd7
PMR
3076 Loc : constant Source_Ptr := Sloc (N);
3077 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3078 Obj_Decl : constant Node_Id :=
3079 Make_Object_Declaration (Loc,
3080 Defining_Identifier => Def_Id,
3081 Constant_Present => True,
3082 Object_Definition => New_Occurrence_Of (Typ, Loc),
3083 Expression => A,
3084 Has_Init_Expression => True);
5168a9b3
PMR
3085 begin
3086 Set_Etype (Def_Id, Typ);
3087 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3088 Insert_Action (N, Obj_Decl);
3089 end Transform_BIP_Assignment;
3090
996ae0b0
RK
3091 -- Start of processing for Resolve_Extension_Aggregate
3092
3093 begin
ebd34478
AC
3094 -- Analyze the ancestor part and account for the case where it is a
3095 -- parameterless function call.
70b70ce8 3096
996ae0b0 3097 Analyze (A);
70b70ce8 3098 Check_Parameterless_Call (A);
996ae0b0 3099
58009744 3100 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
87729e5a
AC
3101
3102 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3103 -- must not have unknown discriminants.
3104
51e2de47 3105 if Has_Unknown_Discriminants (Entity (A)) then
87729e5a
AC
3106 Error_Msg_NE
3107 ("aggregate not available for type& whose ancestor "
3108 & "has unknown discriminants", N, Typ);
3109 end if;
9f90d123
AC
3110 end if;
3111
996ae0b0
RK
3112 if not Is_Tagged_Type (Typ) then
3113 Error_Msg_N ("type of extension aggregate must be tagged", N);
3114 return;
3115
19f0526a
AC
3116 elsif Is_Limited_Type (Typ) then
3117
0ab80019 3118 -- Ada 2005 (AI-287): Limited aggregates are allowed
19f0526a 3119
0791fbe9 3120 if Ada_Version < Ada_2005 then
19f0526a
AC
3121 Error_Msg_N ("aggregate type cannot be limited", N);
3122 Explain_Limited_Type (Typ, N);
3123 return;
ca44152f
ES
3124
3125 elsif Valid_Limited_Ancestor (A) then
3126 null;
3127
3128 else
3129 Error_Msg_N
3130 ("limited ancestor part must be aggregate or function call", A);
19f0526a 3131 end if;
996ae0b0
RK
3132
3133 elsif Is_Class_Wide_Type (Typ) then
3134 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3135 return;
3136 end if;
3137
58009744 3138 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
fbf5a39b 3139 A_Type := Get_Full_View (Entity (A));
996ae0b0
RK
3140
3141 if Valid_Ancestor_Type then
3142 Set_Entity (A, A_Type);
3143 Set_Etype (A, A_Type);
3144
3145 Validate_Ancestor_Part (N);
3146 Resolve_Record_Aggregate (N, Typ);
3147 end if;
3148
3149 elsif Nkind (A) /= N_Aggregate then
3150 if Is_Overloaded (A) then
3151 A_Type := Any_Type;
996ae0b0 3152
7f9747c6 3153 Get_First_Interp (A, I, It);
996ae0b0 3154 while Present (It.Typ) loop
58009744 3155
5168a9b3 3156 -- Consider limited interpretations if Ada 2005 or higher
70b70ce8 3157
996ae0b0 3158 if Is_Tagged_Type (It.Typ)
0791fbe9 3159 and then (Ada_Version >= Ada_2005
70b70ce8 3160 or else not Is_Limited_Type (It.Typ))
996ae0b0
RK
3161 then
3162 if A_Type /= Any_Type then
3163 Error_Msg_N ("cannot resolve expression", A);
3164 return;
3165 else
3166 A_Type := It.Typ;
3167 end if;
3168 end if;
3169
3170 Get_Next_Interp (I, It);
3171 end loop;
3172
3173 if A_Type = Any_Type then
0791fbe9 3174 if Ada_Version >= Ada_2005 then
58009744
AC
3175 Error_Msg_N
3176 ("ancestor part must be of a tagged type", A);
70b70ce8
AC
3177 else
3178 Error_Msg_N
3179 ("ancestor part must be of a nonlimited tagged type", A);
3180 end if;
3181
996ae0b0
RK
3182 return;
3183 end if;
3184
3185 else
3186 A_Type := Etype (A);
3187 end if;
3188
3189 if Valid_Ancestor_Type then
3190 Resolve (A, A_Type);
fbf5a39b 3191 Check_Unset_Reference (A);
996ae0b0 3192 Check_Non_Static_Context (A);
fbf5a39b 3193
1646c947
GD
3194 -- The aggregate is illegal if the ancestor expression is a call
3195 -- to a function with a limited unconstrained result, unless the
3196 -- type of the aggregate is a null extension. This restriction
3197 -- was added in AI05-67 to simplify implementation.
3198
3199 if Nkind (A) = N_Function_Call
3200 and then Is_Limited_Type (A_Type)
3201 and then not Is_Null_Extension (Typ)
3202 and then not Is_Constrained (A_Type)
3203 then
3204 Error_Msg_N
3205 ("type of limited ancestor part must be constrained", A);
3206
cefce34c
JM
3207 -- Reject the use of CPP constructors that leave objects partially
3208 -- initialized. For example:
3209
3210 -- type CPP_Root is tagged limited record ...
3211 -- pragma Import (CPP, CPP_Root);
3212
3213 -- type CPP_DT is new CPP_Root and Iface ...
3214 -- pragma Import (CPP, CPP_DT);
3215
3216 -- type Ada_DT is new CPP_DT with ...
3217
3218 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3219
3220 -- Using the constructor of CPP_Root the slots of the dispatch
3221 -- table of CPP_DT cannot be set, and the secondary tag of
3222 -- CPP_DT is unknown.
3223
3224 elsif Nkind (A) = N_Function_Call
3225 and then Is_CPP_Constructor_Call (A)
3226 and then Enclosing_CPP_Parent (Typ) /= A_Type
3227 then
3228 Error_Msg_NE
324ac540 3229 ("??must use 'C'P'P constructor for type &", A,
cefce34c
JM
3230 Enclosing_CPP_Parent (Typ));
3231
3232 -- The following call is not needed if the previous warning
3233 -- is promoted to an error.
3234
3235 Resolve_Record_Aggregate (N, Typ);
3236
1646c947 3237 elsif Is_Class_Wide_Type (Etype (A))
fbf5a39b
AC
3238 and then Nkind (Original_Node (A)) = N_Function_Call
3239 then
3240 -- If the ancestor part is a dispatching call, it appears
ebd34478
AC
3241 -- statically to be a legal ancestor, but it yields any member
3242 -- of the class, and it is not possible to determine whether
3243 -- it is an ancestor of the extension aggregate (much less
3244 -- which ancestor). It is not possible to determine the
3245 -- components of the extension part.
fbf5a39b 3246
ebd34478
AC
3247 -- This check implements AI-306, which in fact was motivated by
3248 -- an AdaCore query to the ARG after this test was added.
82c80734 3249
fbf5a39b
AC
3250 Error_Msg_N ("ancestor part must be statically tagged", A);
3251 else
5168a9b3
PMR
3252 -- We are using the build-in-place protocol, but we can't build
3253 -- in place, because we need to call the function before
3254 -- allocating the aggregate. Could do better for null
3255 -- extensions, and maybe for nondiscriminated types.
3256 -- This is wrong for limited, but those were wrong already.
3257
3258 if not Is_Limited_View (A_Type)
3259 and then Is_Build_In_Place_Function_Call (A)
3260 then
3261 Transform_BIP_Assignment (A_Type);
3262 end if;
3263
fbf5a39b
AC
3264 Resolve_Record_Aggregate (N, Typ);
3265 end if;
996ae0b0
RK
3266 end if;
3267
3268 else
937e9676 3269 Error_Msg_N ("no unique type for this aggregate", A);
996ae0b0 3270 end if;
d3820795 3271
22e89283 3272 Check_Function_Writable_Actuals (N);
996ae0b0
RK
3273 end Resolve_Extension_Aggregate;
3274
3275 ------------------------------
3276 -- Resolve_Record_Aggregate --
3277 ------------------------------
3278
3279 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
fbf5a39b 3280 New_Assoc_List : constant List_Id := New_List;
996ae0b0 3281 -- New_Assoc_List is the newly built list of N_Component_Association
937e9676 3282 -- nodes.
996ae0b0
RK
3283
3284 Others_Etype : Entity_Id := Empty;
3285 -- This variable is used to save the Etype of the last record component
3286 -- that takes its value from the others choice. Its purpose is:
3287 --
3288 -- (a) make sure the others choice is useful
3289 --
3290 -- (b) make sure the type of all the components whose value is
3291 -- subsumed by the others choice are the same.
3292 --
ebd34478 3293 -- This variable is updated as a side effect of function Get_Value.
996ae0b0 3294
66c19cd4 3295 Box_Node : Node_Id := Empty;
9b96e234 3296 Is_Box_Present : Boolean := False;
5c4adf10 3297 Others_Box : Natural := 0;
0ab80019 3298 -- Ada 2005 (AI-287): Variables used in case of default initialization
9b96e234 3299 -- to provide a functionality similar to Others_Etype. Box_Present
19f0526a 3300 -- indicates that the component takes its default initialization;
ec3c7387
AC
3301 -- Others_Box counts the number of components of the current aggregate
3302 -- (which may be a sub-aggregate of a larger one) that are default-
3303 -- initialized. A value of One indicates that an others_box is present.
3304 -- Any larger value indicates that the others_box is not redundant.
937e9676
AC
3305 -- These variables, similar to Others_Etype, are also updated as a side
3306 -- effect of function Get_Value. Box_Node is used to place a warning on
3307 -- a redundant others_box.
65356e64
AC
3308
3309 procedure Add_Association
9b96e234
JM
3310 (Component : Entity_Id;
3311 Expr : Node_Id;
107b023c 3312 Assoc_List : List_Id;
9b96e234 3313 Is_Box_Present : Boolean := False);
ebd34478
AC
3314 -- Builds a new N_Component_Association node which associates Component
3315 -- to expression Expr and adds it to the association list being built,
3316 -- either New_Assoc_List, or the association being built for an inner
3317 -- aggregate.
996ae0b0 3318
937e9676
AC
3319 procedure Add_Discriminant_Values
3320 (New_Aggr : Node_Id;
3321 Assoc_List : List_Id);
3322 -- The constraint to a component may be given by a discriminant of the
3323 -- enclosing type, in which case we have to retrieve its value, which is
3324 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3325 -- associations of the current type or of some enclosing record.
3326
3327 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
996ae0b0 3328 -- If aggregate N is a regular aggregate this routine will return True.
937e9676
AC
3329 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3330 -- a discriminant whose value may already have been specified by N's
3331 -- ancestor part. This routine checks whether this is indeed the case
3332 -- and if so returns False, signaling that no value for Input_Discr
3333 -- should appear in N's aggregate part. Also, in this case, the routine
3334 -- appends to New_Assoc_List the discriminant value specified in the
3335 -- ancestor part.
2383acbd 3336 --
f104fca1 3337 -- If the aggregate is in a context with expansion delayed, it will be
22cb89b5
AC
3338 -- reanalyzed. The inherited discriminant values must not be reinserted
3339 -- in the component list to prevent spurious errors, but they must be
f104fca1
AC
3340 -- present on first analysis to build the proper subtype indications.
3341 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
996ae0b0 3342
937e9676
AC
3343 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3344 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3345 -- derived from private view Typ. Whether the aggregate is legal depends
3346 -- on the current visibility of the type as well as that of the parent
3347 -- of the ancestor.
3348
996ae0b0 3349 function Get_Value
0bfa32f9 3350 (Compon : Entity_Id;
996ae0b0 3351 From : List_Id;
937e9676 3352 Consider_Others_Choice : Boolean := False) return Node_Id;
4519314c
AC
3353 -- Given a record component stored in parameter Compon, this function
3354 -- returns its value as it appears in the list From, which is a list
3355 -- of N_Component_Association nodes.
2383acbd 3356 --
ebd34478
AC
3357 -- If no component association has a choice for the searched component,
3358 -- the value provided by the others choice is returned, if there is one,
3359 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3360 -- returned. If there is more than one component association giving a
3361 -- value for the searched record component, an error message is emitted
3362 -- and the first found value is returned.
996ae0b0
RK
3363 --
3364 -- If Consider_Others_Choice is set and the returned expression comes
3365 -- from the others choice, then Others_Etype is set as a side effect.
ebd34478
AC
3366 -- An error message is emitted if the components taking their value from
3367 -- the others choice do not have same type.
996ae0b0 3368
937e9676
AC
3369 procedure Propagate_Discriminants
3370 (Aggr : Node_Id;
3371 Assoc_List : List_Id);
3372 -- Nested components may themselves be discriminated types constrained
3373 -- by outer discriminants, whose values must be captured before the
3374 -- aggregate is expanded into assignments.
3375
3376 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
996ae0b0 3377 -- Analyzes and resolves expression Expr against the Etype of the
638e383e 3378 -- Component. This routine also applies all appropriate checks to Expr.
996ae0b0
RK
3379 -- It finally saves a Expr in the newly created association list that
3380 -- will be attached to the final record aggregate. Note that if the
3381 -- Parent pointer of Expr is not set then Expr was produced with a
fbf5a39b 3382 -- New_Copy_Tree or some such.
996ae0b0 3383
333e4f86
AC
3384 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3385 -- Rewrite a range node Rge when its bounds refer to non-stored
3386 -- discriminants from Root_Type, to replace them with the stored
3387 -- discriminant values. This is required in GNATprove mode, and is
3388 -- adopted in all modes to avoid special-casing GNATprove mode.
3389
996ae0b0
RK
3390 ---------------------
3391 -- Add_Association --
3392 ---------------------
3393
65356e64 3394 procedure Add_Association
9b96e234
JM
3395 (Component : Entity_Id;
3396 Expr : Node_Id;
107b023c 3397 Assoc_List : List_Id;
9b96e234 3398 Is_Box_Present : Boolean := False)
65356e64 3399 is
fbf5a39b 3400 Choice_List : constant List_Id := New_List;
937e9676 3401 Loc : Source_Ptr;
996ae0b0
RK
3402
3403 begin
937e9676
AC
3404 -- If this is a box association the expression is missing, so use the
3405 -- Sloc of the aggregate itself for the new association.
f5afb270 3406
0c25b783
PT
3407 pragma Assert (Present (Expr) xor Is_Box_Present);
3408
f5afb270
AC
3409 if Present (Expr) then
3410 Loc := Sloc (Expr);
3411 else
3412 Loc := Sloc (N);
3413 end if;
3414
937e9676
AC
3415 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3416
3417 Append_To (Assoc_List,
f5afb270 3418 Make_Component_Association (Loc,
65356e64
AC
3419 Choices => Choice_List,
3420 Expression => Expr,
937e9676 3421 Box_Present => Is_Box_Present));
996ae0b0
RK
3422 end Add_Association;
3423
937e9676
AC
3424 -----------------------------
3425 -- Add_Discriminant_Values --
3426 -----------------------------
3427
3428 procedure Add_Discriminant_Values
3429 (New_Aggr : Node_Id;
3430 Assoc_List : List_Id)
3431 is
3432 Assoc : Node_Id;
3433 Discr : Entity_Id;
3434 Discr_Elmt : Elmt_Id;
3435 Discr_Val : Node_Id;
3436 Val : Entity_Id;
3437
3438 begin
3439 Discr := First_Discriminant (Etype (New_Aggr));
3440 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3441 while Present (Discr_Elmt) loop
3442 Discr_Val := Node (Discr_Elmt);
3443
3444 -- If the constraint is given by a discriminant then it is a
3445 -- discriminant of an enclosing record, and its value has already
3446 -- been placed in the association list.
996ae0b0 3447
937e9676
AC
3448 if Is_Entity_Name (Discr_Val)
3449 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3450 then
3451 Val := Entity (Discr_Val);
3452
3453 Assoc := First (Assoc_List);
3454 while Present (Assoc) loop
3455 if Present (Entity (First (Choices (Assoc))))
3456 and then Entity (First (Choices (Assoc))) = Val
3457 then
3458 Discr_Val := Expression (Assoc);
3459 exit;
3460 end if;
3461
3462 Next (Assoc);
3463 end loop;
3464 end if;
3465
3466 Add_Association
3467 (Discr, New_Copy_Tree (Discr_Val),
3468 Component_Associations (New_Aggr));
3469
3470 -- If the discriminant constraint is a current instance, mark the
3471 -- current aggregate so that the self-reference can be expanded
3472 -- later. The constraint may refer to the subtype of aggregate, so
3473 -- use base type for comparison.
3474
3475 if Nkind (Discr_Val) = N_Attribute_Reference
3476 and then Is_Entity_Name (Prefix (Discr_Val))
3477 and then Is_Type (Entity (Prefix (Discr_Val)))
3478 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3479 then
3480 Set_Has_Self_Reference (N);
3481 end if;
3482
3483 Next_Elmt (Discr_Elmt);
3484 Next_Discriminant (Discr);
3485 end loop;
3486 end Add_Discriminant_Values;
3487
3488 --------------------------
3489 -- Discriminant_Present --
3490 --------------------------
3491
3492 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
fbf5a39b
AC
3493 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3494
937e9676
AC
3495 Ancestor_Is_Subtyp : Boolean;
3496
996ae0b0
RK
3497 Loc : Source_Ptr;
3498
3499 Ancestor : Node_Id;
937e9676 3500 Ancestor_Typ : Entity_Id;
f104fca1 3501 Comp_Assoc : Node_Id;
937e9676 3502 Discr : Entity_Id;
996ae0b0 3503 Discr_Expr : Node_Id;
937e9676 3504 Discr_Val : Elmt_Id := No_Elmt;
996ae0b0 3505 Orig_Discr : Entity_Id;
996ae0b0
RK
3506
3507 begin
3508 if Regular_Aggr then
3509 return True;
3510 end if;
3511
f104fca1
AC
3512 -- Check whether inherited discriminant values have already been
3513 -- inserted in the aggregate. This will be the case if we are
3514 -- re-analyzing an aggregate whose expansion was delayed.
3515
3516 if Present (Component_Associations (N)) then
3517 Comp_Assoc := First (Component_Associations (N));
3518 while Present (Comp_Assoc) loop
3519 if Inherited_Discriminant (Comp_Assoc) then
3520 return True;
3521 end if;
2383acbd 3522
f104fca1
AC
3523 Next (Comp_Assoc);
3524 end loop;
3525 end if;
3526
996ae0b0
RK
3527 Ancestor := Ancestor_Part (N);
3528 Ancestor_Typ := Etype (Ancestor);
3529 Loc := Sloc (Ancestor);
3530
5987e59c
AC
3531 -- For a private type with unknown discriminants, use the underlying
3532 -- record view if it is available.
9013065b
AC
3533
3534 if Has_Unknown_Discriminants (Ancestor_Typ)
3535 and then Present (Full_View (Ancestor_Typ))
3536 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3537 then
3538 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3539 end if;
3540
996ae0b0
RK
3541 Ancestor_Is_Subtyp :=
3542 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3543
3544 -- If the ancestor part has no discriminants clearly N's aggregate
3545 -- part must provide a value for Discr.
3546
3547 if not Has_Discriminants (Ancestor_Typ) then
3548 return True;
3549
3550 -- If the ancestor part is an unconstrained subtype mark then the
3551 -- Discr must be present in N's aggregate part.
3552
3553 elsif Ancestor_Is_Subtyp
3554 and then not Is_Constrained (Entity (Ancestor))
3555 then
3556 return True;
3557 end if;
3558
ec53a6da 3559 -- Now look to see if Discr was specified in the ancestor part
996ae0b0
RK
3560
3561 if Ancestor_Is_Subtyp then
937e9676
AC
3562 Discr_Val :=
3563 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
996ae0b0
RK
3564 end if;
3565
937e9676 3566 Orig_Discr := Original_Record_Component (Input_Discr);
ec53a6da 3567
937e9676
AC
3568 Discr := First_Discriminant (Ancestor_Typ);
3569 while Present (Discr) loop
ec53a6da 3570
ebd34478 3571 -- If Ancestor has already specified Disc value then insert its
ec53a6da 3572 -- value in the final aggregate.
996ae0b0 3573
937e9676 3574 if Original_Record_Component (Discr) = Orig_Discr then
996ae0b0 3575 if Ancestor_Is_Subtyp then
937e9676 3576 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
996ae0b0
RK
3577 else
3578 Discr_Expr :=
3579 Make_Selected_Component (Loc,
3580 Prefix => Duplicate_Subexpr (Ancestor),
937e9676 3581 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
996ae0b0
RK
3582 end if;
3583
937e9676 3584 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
f104fca1 3585 Set_Inherited_Discriminant (Last (New_Assoc_List));
996ae0b0
RK
3586 return False;
3587 end if;
3588
937e9676 3589 Next_Discriminant (Discr);
996ae0b0
RK
3590
3591 if Ancestor_Is_Subtyp then
937e9676 3592 Next_Elmt (Discr_Val);
996ae0b0
RK
3593 end if;
3594 end loop;
3595
3596 return True;
937e9676
AC
3597 end Discriminant_Present;
3598
3599 ---------------------------
3600 -- Find_Private_Ancestor --
3601 ---------------------------
3602
3603 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3604 Par : Entity_Id;
3605
3606 begin
3607 Par := Typ;
3608 loop
3609 if Has_Private_Ancestor (Par)
3610 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3611 then
3612 return Par;
3613
3614 elsif not Is_Derived_Type (Par) then
3615 return Empty;
3616
3617 else
3618 Par := Etype (Base_Type (Par));
3619 end if;
3620 end loop;
3621 end Find_Private_Ancestor;
996ae0b0
RK
3622
3623 ---------------
3624 -- Get_Value --
3625 ---------------
3626
3627 function Get_Value
0bfa32f9 3628 (Compon : Entity_Id;
996ae0b0 3629 From : List_Id;
937e9676 3630 Consider_Others_Choice : Boolean := False) return Node_Id
996ae0b0 3631 is
d10bb9d5 3632 Typ : constant Entity_Id := Etype (Compon);
996ae0b0
RK
3633 Assoc : Node_Id;
3634 Expr : Node_Id := Empty;
3635 Selector_Name : Node_Id;
3636
3637 begin
9b96e234 3638 Is_Box_Present := False;
65356e64 3639
58009744 3640 if No (From) then
996ae0b0
RK
3641 return Empty;
3642 end if;
3643
58009744 3644 Assoc := First (From);
996ae0b0
RK
3645 while Present (Assoc) loop
3646 Selector_Name := First (Choices (Assoc));
3647 while Present (Selector_Name) loop
3648 if Nkind (Selector_Name) = N_Others_Choice then
3649 if Consider_Others_Choice and then No (Expr) then
996ae0b0
RK
3650
3651 -- We need to duplicate the expression for each
3652 -- successive component covered by the others choice.
fbf5a39b
AC
3653 -- This is redundant if the others_choice covers only
3654 -- one component (small optimization possible???), but
3655 -- indispensable otherwise, because each one must be
92b751fd 3656 -- expanded individually to preserve side effects.
996ae0b0 3657
0ab80019
AC
3658 -- Ada 2005 (AI-287): In case of default initialization
3659 -- of components, we duplicate the corresponding default
88b32fc3
BD
3660 -- expression (from the record type declaration). The
3661 -- copy must carry the sloc of the association (not the
3662 -- original expression) to prevent spurious elaboration
3663 -- checks when the default includes function calls.
19f0526a 3664
65356e64 3665 if Box_Present (Assoc) then
ec3c7387 3666 Others_Box := Others_Box + 1;
9b96e234 3667 Is_Box_Present := True;
65356e64
AC
3668
3669 if Expander_Active then
88b32fc3 3670 return
ba914484 3671 New_Copy_Tree_And_Copy_Dimensions
88b32fc3
BD
3672 (Expression (Parent (Compon)),
3673 New_Sloc => Sloc (Assoc));
65356e64
AC
3674 else
3675 return Expression (Parent (Compon));
3676 end if;
65356e64 3677
d05ef0ab 3678 else
d10bb9d5
AC
3679 if Present (Others_Etype)
3680 and then Base_Type (Others_Etype) /= Base_Type (Typ)
65356e64 3681 then
a921e83c
AC
3682 -- If the components are of an anonymous access
3683 -- type they are distinct, but this is legal in
3684 -- Ada 2012 as long as designated types match.
3685
3686 if (Ekind (Typ) = E_Anonymous_Access_Type
3687 or else Ekind (Typ) =
3688 E_Anonymous_Access_Subprogram_Type)
3689 and then Designated_Type (Typ) =
3690 Designated_Type (Others_Etype)
3691 then
3692 null;
3693 else
3694 Error_Msg_N
937e9676
AC
3695 ("components in OTHERS choice must have same "
3696 & "type", Selector_Name);
a921e83c 3697 end if;
65356e64
AC
3698 end if;
3699
d10bb9d5 3700 Others_Etype := Typ;
65356e64 3701
937e9676 3702 -- Copy the expression so that it is resolved
a921e83c
AC
3703 -- independently for each component, This is needed
3704 -- for accessibility checks on compoents of anonymous
3705 -- access types, even in compile_only mode.
3706
3707 if not Inside_A_Generic then
ba914484
VP
3708 return
3709 New_Copy_Tree_And_Copy_Dimensions
3710 (Expression (Assoc));
65356e64
AC
3711 else
3712 return Expression (Assoc);
3713 end if;
996ae0b0
RK
3714 end if;
3715 end if;
3716
3717 elsif Chars (Compon) = Chars (Selector_Name) then
3718 if No (Expr) then
fbf5a39b 3719
0ab80019 3720 -- Ada 2005 (AI-231)
2820d220 3721
0791fbe9 3722 if Ada_Version >= Ada_2005
8133b9d1 3723 and then Known_Null (Expression (Assoc))
2820d220 3724 then
82c80734 3725 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2820d220
AC
3726 end if;
3727
996ae0b0
RK
3728 -- We need to duplicate the expression when several
3729 -- components are grouped together with a "|" choice.
3730 -- For instance "filed1 | filed2 => Expr"
3731
0ab80019 3732 -- Ada 2005 (AI-287)
2820d220 3733
65356e64 3734 if Box_Present (Assoc) then
9b96e234 3735 Is_Box_Present := True;
65356e64
AC
3736
3737 -- Duplicate the default expression of the component
c7ce71c2
ES
3738 -- from the record type declaration, so a new copy
3739 -- can be attached to the association.
65356e64 3740
c7ce71c2
ES
3741 -- Note that we always copy the default expression,
3742 -- even when the association has a single choice, in
3743 -- order to create a proper association for the
3744 -- expanded aggregate.
3745
8097203f
AC
3746 -- Component may have no default, in which case the
3747 -- expression is empty and the component is default-
3748 -- initialized, but an association for the component
3749 -- exists, and it is not covered by an others clause.
3750
d10bb9d5
AC
3751 -- Scalar and private types have no initialization
3752 -- procedure, so they remain uninitialized. If the
3753 -- target of the aggregate is a constant this
3754 -- deserves a warning.
3755
3756 if No (Expression (Parent (Compon)))
3757 and then not Has_Non_Null_Base_Init_Proc (Typ)
3758 and then not Has_Aspect (Typ, Aspect_Default_Value)
3759 and then not Is_Concurrent_Type (Typ)
3760 and then Nkind (Parent (N)) = N_Object_Declaration
3761 and then Constant_Present (Parent (N))
3762 then
3763 Error_Msg_Node_2 := Typ;
3764 Error_Msg_NE
3765 ("component&? of type& is uninitialized",
3766 Assoc, Selector_Name);
3767
3768 -- An additional reminder if the component type
3769 -- is a generic formal.
3770
3771 if Is_Generic_Type (Base_Type (Typ)) then
3772 Error_Msg_NE
58009744
AC
3773 ("\instance should provide actual type with "
3774 & "initialization for&", Assoc, Typ);
d10bb9d5
AC
3775 end if;
3776 end if;
3777
ba914484
VP
3778 return
3779 New_Copy_Tree_And_Copy_Dimensions
3780 (Expression (Parent (Compon)));
8097203f 3781
d05ef0ab 3782 else
65356e64 3783 if Present (Next (Selector_Name)) then
d10bb9d5
AC
3784 Expr := New_Copy_Tree_And_Copy_Dimensions
3785 (Expression (Assoc));
65356e64
AC
3786 else
3787 Expr := Expression (Assoc);
3788 end if;
996ae0b0
RK
3789 end if;
3790
55603e5e 3791 Generate_Reference (Compon, Selector_Name, 'm');
fbf5a39b 3792
996ae0b0
RK
3793 else
3794 Error_Msg_NE
3795 ("more than one value supplied for &",
3796 Selector_Name, Compon);
3797
3798 end if;
3799 end if;
3800
3801 Next (Selector_Name);
3802 end loop;
3803
3804 Next (Assoc);
3805 end loop;
3806
3807 return Expr;
3808 end Get_Value;
3809
937e9676
AC
3810 -----------------------------
3811 -- Propagate_Discriminants --
3812 -----------------------------
3813
3814 procedure Propagate_Discriminants
3815 (Aggr : Node_Id;
3816 Assoc_List : List_Id)
3817 is
3818 Loc : constant Source_Ptr := Sloc (N);
3819
937e9676
AC
3820 procedure Process_Component (Comp : Entity_Id);
3821 -- Add one component with a box association to the inner aggregate,
3822 -- and recurse if component is itself composite.
3823
3824 -----------------------
3825 -- Process_Component --
3826 -----------------------
3827
3828 procedure Process_Component (Comp : Entity_Id) is
3829 T : constant Entity_Id := Etype (Comp);
3830 New_Aggr : Node_Id;
3831
3832 begin
3833 if Is_Record_Type (T) and then Has_Discriminants (T) then
0ad46f04 3834 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
937e9676
AC
3835 Set_Etype (New_Aggr, T);
3836
3837 Add_Association
3838 (Comp, New_Aggr, Component_Associations (Aggr));
3839
3840 -- Collect discriminant values and recurse
3841
3842 Add_Discriminant_Values (New_Aggr, Assoc_List);
3843 Propagate_Discriminants (New_Aggr, Assoc_List);
3844
fc4c7348
PT
3845 Build_Constrained_Itype
3846 (New_Aggr, T, Component_Associations (New_Aggr));
937e9676 3847 else
0c25b783
PT
3848 Add_Association
3849 (Comp, Empty, Component_Associations (Aggr),
3850 Is_Box_Present => True);
937e9676
AC
3851 end if;
3852 end Process_Component;
3853
3854 -- Local variables
3855
3856 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3857 Components : constant Elist_Id := New_Elmt_List;
3858 Def_Node : constant Node_Id :=
3859 Type_Definition (Declaration_Node (Aggr_Type));
3860
3861 Comp : Node_Id;
3862 Comp_Elmt : Elmt_Id;
3863 Errors : Boolean;
3864
3865 -- Start of processing for Propagate_Discriminants
3866
3867 begin
3868 -- The component type may be a variant type. Collect the components
3869 -- that are ruled by the known values of the discriminants. Their
3870 -- values have already been inserted into the component list of the
3871 -- current aggregate.
3872
3873 if Nkind (Def_Node) = N_Record_Definition
3874 and then Present (Component_List (Def_Node))
3875 and then Present (Variant_Part (Component_List (Def_Node)))
3876 then
3877 Gather_Components (Aggr_Type,
3878 Component_List (Def_Node),
3879 Governed_By => Component_Associations (Aggr),
3880 Into => Components,
3881 Report_Errors => Errors);
3882
3883 Comp_Elmt := First_Elmt (Components);
3884 while Present (Comp_Elmt) loop
3885 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
3886 Process_Component (Node (Comp_Elmt));
3887 end if;
3888
3889 Next_Elmt (Comp_Elmt);
3890 end loop;
3891
3892 -- No variant part, iterate over all components
3893
3894 else
3895 Comp := First_Component (Etype (Aggr));
3896 while Present (Comp) loop
3897 Process_Component (Comp);
3898 Next_Component (Comp);
3899 end loop;
3900 end if;
937e9676
AC
3901 end Propagate_Discriminants;
3902
996ae0b0
RK
3903 -----------------------
3904 -- Resolve_Aggr_Expr --
3905 -----------------------
3906
937e9676 3907 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
996ae0b0
RK
3908 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3909 -- If the expression is an aggregate (possibly qualified) then its
3910 -- expansion is delayed until the enclosing aggregate is expanded
3911 -- into assignments. In that case, do not generate checks on the
3912 -- expression, because they will be generated later, and will other-
92b751fd 3913 -- wise force a copy (to remove side effects) that would leave a
996ae0b0
RK
3914 -- dynamic-sized aggregate in the code, something that gigi cannot
3915 -- handle.
3916
22243c12
RD
3917 ---------------------------
3918 -- Has_Expansion_Delayed --
3919 ---------------------------
996ae0b0
RK
3920
3921 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
996ae0b0 3922 begin
937e9676
AC
3923 return
3924 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
3925 and then Present (Etype (Expr))
3926 and then Is_Record_Type (Etype (Expr))
3927 and then Expansion_Delayed (Expr))
3928 or else
3929 (Nkind (Expr) = N_Qualified_Expression
3930 and then Has_Expansion_Delayed (Expression (Expr)));
996ae0b0
RK
3931 end Has_Expansion_Delayed;
3932
10edebe7
AC
3933 -- Local variables
3934
3935 Expr_Type : Entity_Id := Empty;
3936 New_C : Entity_Id := Component;
3937 New_Expr : Node_Id;
3938
3939 Relocate : Boolean;
3940 -- Set to True if the resolved Expr node needs to be relocated when
3941 -- attached to the newly created association list. This node need not
3942 -- be relocated if its parent pointer is not set. In fact in this
3943 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3944 -- True then we have analyzed the expression node in the original
3945 -- aggregate and hence it needs to be relocated when moved over to
3946 -- the new association list.
3947
22243c12 3948 -- Start of processing for Resolve_Aggr_Expr
996ae0b0
RK
3949
3950 begin
3951 -- If the type of the component is elementary or the type of the
3952 -- aggregate does not contain discriminants, use the type of the
3953 -- component to resolve Expr.
3954
3955 if Is_Elementary_Type (Etype (Component))
3956 or else not Has_Discriminants (Etype (N))
3957 then
3958 Expr_Type := Etype (Component);
3959
3960 -- Otherwise we have to pick up the new type of the component from
12a13f01 3961 -- the new constrained subtype of the aggregate. In fact components
996ae0b0
RK
3962 -- which are of a composite type might be constrained by a
3963 -- discriminant, and we want to resolve Expr against the subtype were
3964 -- all discriminant occurrences are replaced with their actual value.
3965
3966 else
3967 New_C := First_Component (Etype (N));
3968 while Present (New_C) loop
3969 if Chars (New_C) = Chars (Component) then
3970 Expr_Type := Etype (New_C);
3971 exit;
3972 end if;
3973
3974 Next_Component (New_C);
3975 end loop;
3976
3977 pragma Assert (Present (Expr_Type));
3978
3979 -- For each range in an array type where a discriminant has been
3980 -- replaced with the constraint, check that this range is within
ec53a6da
JM
3981 -- the range of the base type. This checks is done in the init
3982 -- proc for regular objects, but has to be done here for
fbf5a39b 3983 -- aggregates since no init proc is called for them.
996ae0b0
RK
3984
3985 if Is_Array_Type (Expr_Type) then
3986 declare
7f9747c6 3987 Index : Node_Id;
ec53a6da 3988 -- Range of the current constrained index in the array
996ae0b0 3989
ec53a6da 3990 Orig_Index : Node_Id := First_Index (Etype (Component));
996ae0b0
RK
3991 -- Range corresponding to the range Index above in the
3992 -- original unconstrained record type. The bounds of this
3993 -- range may be governed by discriminants.
3994
3995 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3996 -- Range corresponding to the range Index above for the
3997 -- unconstrained array type. This range is needed to apply
3998 -- range checks.
3999
4000 begin
7f9747c6 4001 Index := First_Index (Expr_Type);
996ae0b0
RK
4002 while Present (Index) loop
4003 if Depends_On_Discriminant (Orig_Index) then
4004 Apply_Range_Check (Index, Etype (Unconstr_Index));
4005 end if;
4006
4007 Next_Index (Index);
4008 Next_Index (Orig_Index);
4009 Next_Index (Unconstr_Index);
4010 end loop;
4011 end;
4012 end if;
4013 end if;
4014
4015 -- If the Parent pointer of Expr is not set, Expr is an expression
4016 -- duplicated by New_Tree_Copy (this happens for record aggregates
4017 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4018 -- Such a duplicated expression must be attached to the tree
4019 -- before analysis and resolution to enforce the rule that a tree
4020 -- fragment should never be analyzed or resolved unless it is
4021 -- attached to the current compilation unit.
4022
4023 if No (Parent (Expr)) then
4024 Set_Parent (Expr, N);
4025 Relocate := False;
4026 else
4027 Relocate := True;
4028 end if;
4029
4030 Analyze_And_Resolve (Expr, Expr_Type);
ca44152f 4031 Check_Expr_OK_In_Limited_Aggregate (Expr);
996ae0b0 4032 Check_Non_Static_Context (Expr);
fbf5a39b 4033 Check_Unset_Reference (Expr);
996ae0b0 4034
0c020dde
AC
4035 -- Check wrong use of class-wide types
4036
7b4db06c 4037 if Is_Class_Wide_Type (Etype (Expr)) then
0c020dde
AC
4038 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4039 end if;
4040
996ae0b0
RK
4041 if not Has_Expansion_Delayed (Expr) then
4042 Aggregate_Constraint_Checks (Expr, Expr_Type);
887d102a
AC
4043 end if;
4044
dec6faf1
AC
4045 -- If an aggregate component has a type with predicates, an explicit
4046 -- predicate check must be applied, as for an assignment statement,
4047 -- because the aggegate might not be expanded into individual
4048 -- component assignments.
4049
619bfd9f 4050 if Has_Predicates (Expr_Type)
07eb872e
AC
4051 and then Analyzed (Expr)
4052 then
887d102a 4053 Apply_Predicate_Check (Expr, Expr_Type);
996ae0b0
RK
4054 end if;
4055
4056 if Raises_Constraint_Error (Expr) then
4057 Set_Raises_Constraint_Error (N);
4058 end if;
4059
22243c12 4060 -- If the expression has been marked as requiring a range check, then
edab6088
RD
4061 -- generate it here. It's a bit odd to be generating such checks in
4062 -- the analyzer, but harmless since Generate_Range_Check does nothing
4063 -- (other than making sure Do_Range_Check is set) if the expander is
4064 -- not active.
d79e621a
GD
4065
4066 if Do_Range_Check (Expr) then
d79e621a
GD
4067 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4068 end if;
4069
937e9676
AC
4070 -- Add association Component => Expr if the caller requests it
4071
996ae0b0 4072 if Relocate then
0929eaeb
AC
4073 New_Expr := Relocate_Node (Expr);
4074
4075 -- Since New_Expr is not gonna be analyzed later on, we need to
4076 -- propagate here the dimensions form Expr to New_Expr.
4077
ba914484 4078 Copy_Dimensions (Expr, New_Expr);
d976bf74 4079
996ae0b0 4080 else
0929eaeb 4081 New_Expr := Expr;
996ae0b0 4082 end if;
0929eaeb
AC
4083
4084 Add_Association (New_C, New_Expr, New_Assoc_List);
996ae0b0
RK
4085 end Resolve_Aggr_Expr;
4086
333e4f86
AC
4087 -------------------
4088 -- Rewrite_Range --
4089 -------------------
4090
4091 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
333e4f86
AC
4092 procedure Rewrite_Bound
4093 (Bound : Node_Id;
4094 Disc : Entity_Id;
4095 Expr_Disc : Node_Id);
4096 -- Rewrite a bound of the range Bound, when it is equal to the
4097 -- non-stored discriminant Disc, into the stored discriminant
4098 -- value Expr_Disc.
4099
4100 -------------------
4101 -- Rewrite_Bound --
4102 -------------------
4103
4104 procedure Rewrite_Bound
4105 (Bound : Node_Id;
4106 Disc : Entity_Id;
4107 Expr_Disc : Node_Id)
4108 is
4109 begin
2c26d262
EB
4110 if Nkind (Bound) /= N_Identifier then
4111 return;
4112 end if;
4113
4114 -- We expect either the discriminant or the discriminal
4115
4116 if Entity (Bound) = Disc
4117 or else (Ekind (Entity (Bound)) = E_In_Parameter
4118 and then Discriminal_Link (Entity (Bound)) = Disc)
333e4f86
AC
4119 then
4120 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4121 end if;
4122 end Rewrite_Bound;
4123
9c5719f6 4124 -- Local variables
333e4f86
AC
4125
4126 Low, High : Node_Id;
4127 Disc : Entity_Id;
4128 Expr_Disc : Elmt_Id;
4129
4130 -- Start of processing for Rewrite_Range
4131
4132 begin
2c26d262 4133 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
333e4f86
AC
4134 Low := Low_Bound (Rge);
4135 High := High_Bound (Rge);
4136
13126368
YM
4137 Disc := First_Discriminant (Root_Type);
4138 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
333e4f86
AC
4139 while Present (Disc) loop
4140 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4141 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4142 Next_Discriminant (Disc);
4143 Next_Elmt (Expr_Disc);
4144 end loop;
4145 end if;
4146 end Rewrite_Range;
4147
937e9676
AC
4148 -- Local variables
4149
4150 Components : constant Elist_Id := New_Elmt_List;
4151 -- Components is the list of the record components whose value must be
4152 -- provided in the aggregate. This list does include discriminants.
4153
937e9676
AC
4154 Component : Entity_Id;
4155 Component_Elmt : Elmt_Id;
13126368 4156 Expr : Node_Id;
937e9676
AC
4157 Positional_Expr : Node_Id;
4158
996ae0b0
RK
4159 -- Start of processing for Resolve_Record_Aggregate
4160
4161 begin
2ba431e5 4162 -- A record aggregate is restricted in SPARK:
15918371 4163
bd65a2d7
AC
4164 -- Each named association can have only a single choice.
4165 -- OTHERS cannot be used.
4166 -- Positional and named associations cannot be mixed.
9f90d123 4167
fe5d3068
YM
4168 if Present (Component_Associations (N))
4169 and then Present (First (Component_Associations (N)))
9f90d123 4170 then
9f90d123
AC
4171 declare
4172 Assoc : Node_Id;
bd65a2d7 4173
9f90d123
AC
4174 begin
4175 Assoc := First (Component_Associations (N));
4176 while Present (Assoc) loop
ccd0ed95 4177 if Nkind (Assoc) = N_Iterated_Component_Association then
7f5e671b
PMR
4178 Error_Msg_N
4179 ("iterated component association can only appear in an "
4180 & "array aggregate", N);
ccd0ed95 4181 raise Unrecoverable_Error;
9f90d123 4182 end if;
bd65a2d7 4183
cbbe41d1 4184 Next (Assoc);
9f90d123
AC
4185 end loop;
4186 end;
4187 end if;
4188
996ae0b0
RK
4189 -- We may end up calling Duplicate_Subexpr on expressions that are
4190 -- attached to New_Assoc_List. For this reason we need to attach it
4191 -- to the tree by setting its parent pointer to N. This parent point
4192 -- will change in STEP 8 below.
4193
4194 Set_Parent (New_Assoc_List, N);
4195
4196 -- STEP 1: abstract type and null record verification
4197
aad93b55 4198 if Is_Abstract_Type (Typ) then
996ae0b0
RK
4199 Error_Msg_N ("type of aggregate cannot be abstract", N);
4200 end if;
4201
4202 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4203 Set_Etype (N, Typ);
4204 return;
4205
4206 elsif Present (First_Entity (Typ))
4207 and then Null_Record_Present (N)
4208 and then not Is_Tagged_Type (Typ)
4209 then
4210 Error_Msg_N ("record aggregate cannot be null", N);
4211 return;
4212
eff332d9
GD
4213 -- If the type has no components, then the aggregate should either
4214 -- have "null record", or in Ada 2005 it could instead have a single
22243c12
RD
4215 -- component association given by "others => <>". For Ada 95 we flag an
4216 -- error at this point, but for Ada 2005 we proceed with checking the
4217 -- associations below, which will catch the case where it's not an
4218 -- aggregate with "others => <>". Note that the legality of a <>
eff332d9
GD
4219 -- aggregate for a null record type was established by AI05-016.
4220
4221 elsif No (First_Entity (Typ))
0791fbe9 4222 and then Ada_Version < Ada_2005
eff332d9 4223 then
996ae0b0
RK
4224 Error_Msg_N ("record aggregate must be null", N);
4225 return;
4226 end if;
4227
4228 -- STEP 2: Verify aggregate structure
4229
4230 Step_2 : declare
937e9676 4231 Assoc : Node_Id;
996ae0b0 4232 Bad_Aggregate : Boolean := False;
937e9676 4233 Selector_Name : Node_Id;
996ae0b0
RK
4234
4235 begin
4236 if Present (Component_Associations (N)) then
4237 Assoc := First (Component_Associations (N));
4238 else
4239 Assoc := Empty;
4240 end if;
4241
4242 while Present (Assoc) loop
4243 Selector_Name := First (Choices (Assoc));
4244 while Present (Selector_Name) loop
4245 if Nkind (Selector_Name) = N_Identifier then
4246 null;
4247
4248 elsif Nkind (Selector_Name) = N_Others_Choice then
4249 if Selector_Name /= First (Choices (Assoc))
4250 or else Present (Next (Selector_Name))
4251 then
ed2233dc 4252 Error_Msg_N
22cb89b5
AC
4253 ("OTHERS must appear alone in a choice list",
4254 Selector_Name);
996ae0b0
RK
4255 return;
4256
4257 elsif Present (Next (Assoc)) then
ed2233dc 4258 Error_Msg_N
22cb89b5
AC
4259 ("OTHERS must appear last in an aggregate",
4260 Selector_Name);
996ae0b0 4261 return;
1ab9541b 4262
885c4871 4263 -- (Ada 2005): If this is an association with a box,
1ab9541b
ES
4264 -- indicate that the association need not represent
4265 -- any component.
4266
4267 elsif Box_Present (Assoc) then
ec3c7387
AC
4268 Others_Box := 1;
4269 Box_Node := Assoc;
996ae0b0
RK
4270 end if;
4271
4272 else
4273 Error_Msg_N
4274 ("selector name should be identifier or OTHERS",
4275 Selector_Name);
4276 Bad_Aggregate := True;
4277 end if;
4278
4279 Next (Selector_Name);
4280 end loop;
4281
4282 Next (Assoc);
4283 end loop;
4284
4285 if Bad_Aggregate then
4286 return;
4287 end if;
4288 end Step_2;
4289
4290 -- STEP 3: Find discriminant Values
4291
4292 Step_3 : declare
4293 Discrim : Entity_Id;
4294 Missing_Discriminants : Boolean := False;
4295
4296 begin
4297 if Present (Expressions (N)) then
4298 Positional_Expr := First (Expressions (N));
4299 else
4300 Positional_Expr := Empty;
4301 end if;
4302
87729e5a 4303 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
33bd17e7 4304 -- must not have unknown discriminants.
51e2de47
AC
4305 -- ??? We are not checking any subtype mark here and this code is not
4306 -- exercised by any test, so it's likely wrong (in particular
4307 -- we should not use Root_Type here but the subtype mark, if any),
4308 -- and possibly not needed.
87729e5a
AC
4309
4310 if Is_Derived_Type (Typ)
4311 and then Has_Unknown_Discriminants (Root_Type (Typ))
4312 and then Nkind (N) /= N_Extension_Aggregate
4313 then
4314 Error_Msg_NE
4315 ("aggregate not available for type& whose ancestor "
58009744 4316 & "has unknown discriminants ", N, Typ);
87729e5a
AC
4317 end if;
4318
9013065b
AC
4319 if Has_Unknown_Discriminants (Typ)
4320 and then Present (Underlying_Record_View (Typ))
4321 then
4322 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4323 elsif Has_Discriminants (Typ) then
996ae0b0
RK
4324 Discrim := First_Discriminant (Typ);
4325 else
4326 Discrim := Empty;
4327 end if;
4328
4329 -- First find the discriminant values in the positional components
4330
4331 while Present (Discrim) and then Present (Positional_Expr) loop
937e9676 4332 if Discriminant_Present (Discrim) then
996ae0b0 4333 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2820d220 4334
0ab80019 4335 -- Ada 2005 (AI-231)
2820d220 4336
0791fbe9 4337 if Ada_Version >= Ada_2005
8133b9d1 4338 and then Known_Null (Positional_Expr)
ec53a6da 4339 then
82c80734 4340 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2820d220
AC
4341 end if;
4342
996ae0b0
RK
4343 Next (Positional_Expr);
4344 end if;
4345
4346 if Present (Get_Value (Discrim, Component_Associations (N))) then
4347 Error_Msg_NE
4348 ("more than one value supplied for discriminant&",
4349 N, Discrim);
4350 end if;
4351
4352 Next_Discriminant (Discrim);
4353 end loop;
4354
50decc81 4355 -- Find remaining discriminant values if any among named components
996ae0b0
RK
4356
4357 while Present (Discrim) loop
4358 Expr := Get_Value (Discrim, Component_Associations (N), True);
4359
937e9676 4360 if not Discriminant_Present (Discrim) then
996ae0b0
RK
4361 if Present (Expr) then
4362 Error_Msg_NE
58009744 4363 ("more than one value supplied for discriminant &",
996ae0b0
RK
4364 N, Discrim);
4365 end if;
4366
4367 elsif No (Expr) then
4368 Error_Msg_NE
4369 ("no value supplied for discriminant &", N, Discrim);
4370 Missing_Discriminants := True;
4371
4372 else
4373 Resolve_Aggr_Expr (Expr, Discrim);
4374 end if;
4375
4376 Next_Discriminant (Discrim);
4377 end loop;
4378
4379 if Missing_Discriminants then
4380 return;
4381 end if;
4382
4383 -- At this point and until the beginning of STEP 6, New_Assoc_List
4384 -- contains only the discriminants and their values.
4385
4386 end Step_3;
4387
4388 -- STEP 4: Set the Etype of the record aggregate
4389
9013065b
AC
4390 if Has_Discriminants (Typ)
4391 or else (Has_Unknown_Discriminants (Typ)
58009744 4392 and then Present (Underlying_Record_View (Typ)))
9013065b 4393 then
fc4c7348 4394 Build_Constrained_Itype (N, Typ, New_Assoc_List);
996ae0b0
RK
4395 else
4396 Set_Etype (N, Typ);
4397 end if;
4398
4399 -- STEP 5: Get remaining components according to discriminant values
4400
4401 Step_5 : declare
937e9676
AC
4402 Dnode : Node_Id;
4403 Errors_Found : Boolean := False;
996ae0b0
RK
4404 Record_Def : Node_Id;
4405 Parent_Typ : Entity_Id;
996ae0b0
RK
4406 Parent_Typ_List : Elist_Id;
4407 Parent_Elmt : Elmt_Id;
937e9676 4408 Root_Typ : Entity_Id;
15918371 4409
996ae0b0
RK
4410 begin
4411 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4412 Parent_Typ_List := New_Elmt_List;
4413
4414 -- If this is an extension aggregate, the component list must
965dbd5c
AC
4415 -- include all components that are not in the given ancestor type.
4416 -- Otherwise, the component list must include components of all
4417 -- ancestors, starting with the root.
996ae0b0
RK
4418
4419 if Nkind (N) = N_Extension_Aggregate then
7b4db06c 4420 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
69a0c174 4421
996ae0b0 4422 else
937e9676
AC
4423 -- AI05-0115: check legality of aggregate for type with a
4424 -- private ancestor.
87729e5a 4425
996ae0b0 4426 Root_Typ := Root_Type (Typ);
87729e5a
AC
4427 if Has_Private_Ancestor (Typ) then
4428 declare
4429 Ancestor : constant Entity_Id :=
937e9676 4430 Find_Private_Ancestor (Typ);
87729e5a 4431 Ancestor_Unit : constant Entity_Id :=
937e9676
AC
4432 Cunit_Entity
4433 (Get_Source_Unit (Ancestor));
87729e5a 4434 Parent_Unit : constant Entity_Id :=
937e9676
AC
4435 Cunit_Entity (Get_Source_Unit
4436 (Base_Type (Etype (Ancestor))));
87729e5a 4437 begin
58009744 4438 -- Check whether we are in a scope that has full view
87729e5a
AC
4439 -- over the private ancestor and its parent. This can
4440 -- only happen if the derivation takes place in a child
4441 -- unit of the unit that declares the parent, and we are
4442 -- in the private part or body of that child unit, else
4443 -- the aggregate is illegal.
4444
4445 if Is_Child_Unit (Ancestor_Unit)
4446 and then Scope (Ancestor_Unit) = Parent_Unit
4447 and then In_Open_Scopes (Scope (Ancestor))
4448 and then
4449 (In_Private_Part (Scope (Ancestor))
58009744 4450 or else In_Package_Body (Scope (Ancestor)))
87729e5a
AC
4451 then
4452 null;
4453
4454 else
4455 Error_Msg_NE
4456 ("type of aggregate has private ancestor&!",
58009744 4457 N, Root_Typ);
87729e5a
AC
4458 Error_Msg_N ("must use extension aggregate!", N);
4459 return;
4460 end if;
4461 end;
996ae0b0
RK
4462 end if;
4463
4464 Dnode := Declaration_Node (Base_Type (Root_Typ));
4465
4519314c
AC
4466 -- If we don't get a full declaration, then we have some error
4467 -- which will get signalled later so skip this part. Otherwise
4468 -- gather components of root that apply to the aggregate type.
4469 -- We use the base type in case there is an applicable stored
4470 -- constraint that renames the discriminants of the root.
996ae0b0
RK
4471
4472 if Nkind (Dnode) = N_Full_Type_Declaration then
4473 Record_Def := Type_Definition (Dnode);
15918371
AC
4474 Gather_Components
4475 (Base_Type (Typ),
4476 Component_List (Record_Def),
4477 Governed_By => New_Assoc_List,
4478 Into => Components,
4479 Report_Errors => Errors_Found);
4ffafd86
AC
4480
4481 if Errors_Found then
4482 Error_Msg_N
4483 ("discriminant controlling variant part is not static",
4484 N);
4485 return;
4486 end if;
996ae0b0
RK
4487 end if;
4488 end if;
4489
9013065b 4490 Parent_Typ := Base_Type (Typ);
996ae0b0 4491 while Parent_Typ /= Root_Typ loop
996ae0b0
RK
4492 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4493 Parent_Typ := Etype (Parent_Typ);
4494
fbf5a39b 4495 if Nkind (Parent (Base_Type (Parent_Typ))) =
996ae0b0 4496 N_Private_Type_Declaration
fbf5a39b
AC
4497 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4498 N_Private_Extension_Declaration
996ae0b0
RK
4499 then
4500 if Nkind (N) /= N_Extension_Aggregate then
ed2233dc 4501 Error_Msg_NE
996ae0b0
RK
4502 ("type of aggregate has private ancestor&!",
4503 N, Parent_Typ);
ed2233dc 4504 Error_Msg_N ("must use extension aggregate!", N);
996ae0b0
RK
4505 return;
4506
4507 elsif Parent_Typ /= Root_Typ then
4508 Error_Msg_NE
4509 ("ancestor part of aggregate must be private type&",
4510 Ancestor_Part (N), Parent_Typ);
4511 return;
4512 end if;
4519314c
AC
4513
4514 -- The current view of ancestor part may be a private type,
4515 -- while the context type is always non-private.
4516
4517 elsif Is_Private_Type (Root_Typ)
4518 and then Present (Full_View (Root_Typ))
4519 and then Nkind (N) = N_Extension_Aggregate
4520 then
4521 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
996ae0b0
RK
4522 end if;
4523 end loop;
4524
bf06d37f
AC
4525 -- Now collect components from all other ancestors, beginning
4526 -- with the current type. If the type has unknown discriminants
349ff68f 4527 -- use the component list of the Underlying_Record_View, which
bf06d37f
AC
4528 -- needs to be used for the subsequent expansion of the aggregate
4529 -- into assignments.
996ae0b0
RK
4530
4531 Parent_Elmt := First_Elmt (Parent_Typ_List);
4532 while Present (Parent_Elmt) loop
4533 Parent_Typ := Node (Parent_Elmt);
bf06d37f
AC
4534
4535 if Has_Unknown_Discriminants (Parent_Typ)
4536 and then Present (Underlying_Record_View (Typ))
4537 then
4538 Parent_Typ := Underlying_Record_View (Parent_Typ);
4539 end if;
4540
996ae0b0
RK
4541 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4542 Gather_Components (Empty,
4543 Component_List (Record_Extension_Part (Record_Def)),
4544 Governed_By => New_Assoc_List,
4545 Into => Components,
4546 Report_Errors => Errors_Found);
4547
4548 Next_Elmt (Parent_Elmt);
4549 end loop;
4550
33bd17e7
ES
4551 -- Typ is not a derived tagged type
4552
996ae0b0 4553 else
6bde3eb5 4554 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
996ae0b0
RK
4555
4556 if Null_Present (Record_Def) then
4557 null;
bf06d37f
AC
4558
4559 elsif not Has_Unknown_Discriminants (Typ) then
15918371
AC
4560 Gather_Components
4561 (Base_Type (Typ),
4562 Component_List (Record_Def),
4563 Governed_By => New_Assoc_List,
4564 Into => Components,
4565 Report_Errors => Errors_Found);
bf06d37f
AC
4566
4567 else
4568 Gather_Components
4569 (Base_Type (Underlying_Record_View (Typ)),
15918371
AC
4570 Component_List (Record_Def),
4571 Governed_By => New_Assoc_List,
4572 Into => Components,
4573 Report_Errors => Errors_Found);
996ae0b0
RK
4574 end if;
4575 end if;
4576
4577 if Errors_Found then
4578 return;
4579 end if;
4580 end Step_5;
4581
4582 -- STEP 6: Find component Values
4583
4584 Component := Empty;
4585 Component_Elmt := First_Elmt (Components);
4586
4587 -- First scan the remaining positional associations in the aggregate.
4588 -- Remember that at this point Positional_Expr contains the current
4589 -- positional association if any is left after looking for discriminant
4590 -- values in step 3.
4591
4592 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4593 Component := Node (Component_Elmt);
4594 Resolve_Aggr_Expr (Positional_Expr, Component);
4595
0ab80019
AC
4596 -- Ada 2005 (AI-231)
4597
58009744 4598 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
82c80734 4599 Check_Can_Never_Be_Null (Component, Positional_Expr);
2820d220
AC
4600 end if;
4601
996ae0b0
RK
4602 if Present (Get_Value (Component, Component_Associations (N))) then
4603 Error_Msg_NE
4604 ("more than one value supplied for Component &", N, Component);
4605 end if;
4606
4607 Next (Positional_Expr);
4608 Next_Elmt (Component_Elmt);
4609 end loop;
4610
4611 if Present (Positional_Expr) then
4612 Error_Msg_N
4613 ("too many components for record aggregate", Positional_Expr);
4614 end if;
4615
4616 -- Now scan for the named arguments of the aggregate
4617
4618 while Present (Component_Elmt) loop
4619 Component := Node (Component_Elmt);
4620 Expr := Get_Value (Component, Component_Associations (N), True);
4621
9b96e234 4622 -- Note: The previous call to Get_Value sets the value of the
f91e8020 4623 -- variable Is_Box_Present.
65356e64 4624
9b96e234
JM
4625 -- Ada 2005 (AI-287): Handle components with default initialization.
4626 -- Note: This feature was originally added to Ada 2005 for limited
4627 -- but it was finally allowed with any type.
65356e64 4628
9b96e234 4629 if Is_Box_Present then
f91e8020
GD
4630 Check_Box_Component : declare
4631 Ctyp : constant Entity_Id := Etype (Component);
9b96e234
JM
4632
4633 begin
c7ce71c2 4634 -- If there is a default expression for the aggregate, copy
0df7e2d0
AC
4635 -- it into a new association. This copy must modify the scopes
4636 -- of internal types that may be attached to the expression
4637 -- (e.g. index subtypes of arrays) because in general the type
4638 -- declaration and the aggregate appear in different scopes,
4639 -- and the backend requires the scope of the type to match the
4640 -- point at which it is elaborated.
c7ce71c2 4641
9b96e234
JM
4642 -- If the component has an initialization procedure (IP) we
4643 -- pass the component to the expander, which will generate
4644 -- the call to such IP.
4645
c7ce71c2
ES
4646 -- If the component has discriminants, their values must
4647 -- be taken from their subtype. This is indispensable for
4648 -- constraints that are given by the current instance of an
50decc81
RD
4649 -- enclosing type, to allow the expansion of the aggregate to
4650 -- replace the reference to the current instance by the target
4651 -- object of the aggregate.
c7ce71c2
ES
4652
4653 if Present (Parent (Component))
937e9676 4654 and then Nkind (Parent (Component)) = N_Component_Declaration
c7ce71c2 4655 and then Present (Expression (Parent (Component)))
aad93b55 4656 then
c7ce71c2 4657 Expr :=
ba914484 4658 New_Copy_Tree_And_Copy_Dimensions
2293611f
AC
4659 (Expression (Parent (Component)),
4660 New_Scope => Current_Scope,
4661 New_Sloc => Sloc (N));
c7ce71c2 4662
333e4f86
AC
4663 -- As the type of the copied default expression may refer
4664 -- to discriminants of the record type declaration, these
4665 -- non-stored discriminants need to be rewritten into stored
4666 -- discriminant values for the aggregate. This is required
4667 -- in GNATprove mode, and is adopted in all modes to avoid
4668 -- special-casing GNATprove mode.
4669
4670 if Is_Array_Type (Etype (Expr)) then
4671 declare
13126368
YM
4672 Rec_Typ : constant Entity_Id := Scope (Component);
4673 -- Root record type whose discriminants may be used as
4674 -- bounds in range nodes.
4675
2c26d262
EB
4676 Assoc : Node_Id;
4677 Choice : Node_Id;
4678 Index : Node_Id;
333e4f86
AC
4679
4680 begin
4681 -- Rewrite the range nodes occurring in the indexes
4682 -- and their types.
4683
4684 Index := First_Index (Etype (Expr));
4685 while Present (Index) loop
13126368 4686 Rewrite_Range (Rec_Typ, Index);
333e4f86 4687 Rewrite_Range
13126368
YM
4688 (Rec_Typ, Scalar_Range (Etype (Index)));
4689
333e4f86
AC
4690 Next_Index (Index);
4691 end loop;
4692
4693 -- Rewrite the range nodes occurring as aggregate
2c26d262 4694 -- bounds and component associations.
333e4f86 4695
2c26d262
EB
4696 if Nkind (Expr) = N_Aggregate then
4697 if Present (Aggregate_Bounds (Expr)) then
4698 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4699 end if;
4700
4701 if Present (Component_Associations (Expr)) then
4702 Assoc := First (Component_Associations (Expr));
4703 while Present (Assoc) loop
4704 Choice := First (Choices (Assoc));
4705 while Present (Choice) loop
4706 Rewrite_Range (Rec_Typ, Choice);
4707
4708 Next (Choice);
4709 end loop;
4710
4711 Next (Assoc);
4712 end loop;
4713 end if;
333e4f86
AC
4714 end if;
4715 end;
4716 end if;
4717
9b96e234 4718 Add_Association
107b023c
AC
4719 (Component => Component,
4720 Expr => Expr,
4721 Assoc_List => New_Assoc_List);
c7ce71c2
ES
4722 Set_Has_Self_Reference (N);
4723
f91e8020
GD
4724 -- A box-defaulted access component gets the value null. Also
4725 -- included are components of private types whose underlying
c80d4855
RD
4726 -- type is an access type. In either case set the type of the
4727 -- literal, for subsequent use in semantic checks.
f91e8020
GD
4728
4729 elsif Present (Underlying_Type (Ctyp))
4730 and then Is_Access_Type (Underlying_Type (Ctyp))
4731 then
f91e8020
GD
4732 -- If the component's type is private with an access type as
4733 -- its underlying type then we have to create an unchecked
4734 -- conversion to satisfy type checking.
4735
937e9676 4736 if Is_Private_Type (Ctyp) then
f91e8020
GD
4737 declare
4738 Qual_Null : constant Node_Id :=
4739 Make_Qualified_Expression (Sloc (N),
4740 Subtype_Mark =>
4741 New_Occurrence_Of
4742 (Underlying_Type (Ctyp), Sloc (N)),
937e9676 4743 Expression => Make_Null (Sloc (N)));
f91e8020
GD
4744
4745 Convert_Null : constant Node_Id :=
4746 Unchecked_Convert_To
4747 (Ctyp, Qual_Null);
4748
4749 begin
4750 Analyze_And_Resolve (Convert_Null, Ctyp);
4751 Add_Association
107b023c
AC
4752 (Component => Component,
4753 Expr => Convert_Null,
4754 Assoc_List => New_Assoc_List);
f91e8020 4755 end;
937e9676
AC
4756
4757 -- Otherwise the component type is non-private
4758
4759 else
4760 Expr := Make_Null (Sloc (N));
4761 Set_Etype (Expr, Ctyp);
4762
4763 Add_Association
4764 (Component => Component,
4765 Expr => Expr,
4766 Assoc_List => New_Assoc_List);
f91e8020
GD
4767 end if;
4768
7610fee8
AC
4769 -- Ada 2012: If component is scalar with default value, use it
4770
4771 elsif Is_Scalar_Type (Ctyp)
4772 and then Has_Default_Aspect (Ctyp)
4773 then
4774 Add_Association
4775 (Component => Component,
937e9676
AC
4776 Expr =>
4777 Default_Aspect_Value
4778 (First_Subtype (Underlying_Type (Ctyp))),
7610fee8
AC
4779 Assoc_List => New_Assoc_List);
4780
c7ce71c2
ES
4781 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4782 or else not Expander_Active
4783 then
4784 if Is_Record_Type (Ctyp)
4785 and then Has_Discriminants (Ctyp)
6bde3eb5 4786 and then not Is_Private_Type (Ctyp)
c7ce71c2
ES
4787 then
4788 -- We build a partially initialized aggregate with the
4789 -- values of the discriminants and box initialization
8133b9d1 4790 -- for the rest, if other components are present.
c7e152b5 4791
51ec70b8 4792 -- The type of the aggregate is the known subtype of
937e9676
AC
4793 -- the component. The capture of discriminants must be
4794 -- recursive because subcomponents may be constrained
107b023c 4795 -- (transitively) by discriminants of enclosing types.
6bde3eb5
AC
4796 -- For a private type with discriminants, a call to the
4797 -- initialization procedure will be generated, and no
4798 -- subaggregate is needed.
c7ce71c2 4799
107b023c 4800 Capture_Discriminants : declare
719aaf4d
AC
4801 Loc : constant Source_Ptr := Sloc (N);
4802 Expr : Node_Id;
c7ce71c2 4803
107b023c 4804 begin
0ad46f04 4805 Expr := Make_Aggregate (Loc, No_List, New_List);
107b023c
AC
4806 Set_Etype (Expr, Ctyp);
4807
3786bbd1
RD
4808 -- If the enclosing type has discriminants, they have
4809 -- been collected in the aggregate earlier, and they
4810 -- may appear as constraints of subcomponents.
4811
107b023c 4812 -- Similarly if this component has discriminants, they
2be0bff8 4813 -- might in turn be propagated to their components.
107b023c
AC
4814
4815 if Has_Discriminants (Typ) then
4816 Add_Discriminant_Values (Expr, New_Assoc_List);
105b5e65 4817 Propagate_Discriminants (Expr, New_Assoc_List);
107b023c
AC
4818
4819 elsif Has_Discriminants (Ctyp) then
4820 Add_Discriminant_Values
937e9676 4821 (Expr, Component_Associations (Expr));
107b023c 4822 Propagate_Discriminants
937e9676 4823 (Expr, Component_Associations (Expr));
107b023c 4824
fc4c7348
PT
4825 Build_Constrained_Itype
4826 (Expr, Ctyp, Component_Associations (Expr));
4827
107b023c
AC
4828 else
4829 declare
719aaf4d 4830 Comp : Entity_Id;
107b023c
AC
4831
4832 begin
4833 -- If the type has additional components, create
2be0bff8 4834 -- an OTHERS box association for them.
107b023c
AC
4835
4836 Comp := First_Component (Ctyp);
4837 while Present (Comp) loop
4838 if Ekind (Comp) = E_Component then
4839 if not Is_Record_Type (Etype (Comp)) then
37368818
RD
4840 Append_To
4841 (Component_Associations (Expr),
4842 Make_Component_Association (Loc,
107b023c 4843 Choices =>
58009744
AC
4844 New_List (
4845 Make_Others_Choice (Loc)),
107b023c 4846 Expression => Empty,
58009744 4847 Box_Present => True));
107b023c 4848 end if;
937e9676 4849
107b023c
AC
4850 exit;
4851 end if;
4852
4853 Next_Component (Comp);
4854 end loop;
4855 end;
4856 end if;
c7ce71c2
ES
4857
4858 Add_Association
107b023c
AC
4859 (Component => Component,
4860 Expr => Expr,
4861 Assoc_List => New_Assoc_List);
4862 end Capture_Discriminants;
c7ce71c2 4863
937e9676
AC
4864 -- Otherwise the component type is not a record, or it has
4865 -- not discriminants, or it is private.
4866
c7ce71c2
ES
4867 else
4868 Add_Association
4869 (Component => Component,
4870 Expr => Empty,
107b023c 4871 Assoc_List => New_Assoc_List,
c7ce71c2
ES
4872 Is_Box_Present => True);
4873 end if;
9b96e234
JM
4874
4875 -- Otherwise we only need to resolve the expression if the
4876 -- component has partially initialized values (required to
4877 -- expand the corresponding assignments and run-time checks).
4878
4879 elsif Present (Expr)
f91e8020 4880 and then Is_Partially_Initialized_Type (Ctyp)
9b96e234
JM
4881 then
4882 Resolve_Aggr_Expr (Expr, Component);
4883 end if;
f91e8020 4884 end Check_Box_Component;
615cbd95 4885
65356e64 4886 elsif No (Expr) then
c7ce71c2
ES
4887
4888 -- Ignore hidden components associated with the position of the
4889 -- interface tags: these are initialized dynamically.
4890
c80d4855 4891 if not Present (Related_Type (Component)) then
c7ce71c2
ES
4892 Error_Msg_NE
4893 ("no value supplied for component &!", N, Component);
4894 end if;
615cbd95 4895
996ae0b0
RK
4896 else
4897 Resolve_Aggr_Expr (Expr, Component);
4898 end if;
4899
4900 Next_Elmt (Component_Elmt);
4901 end loop;
4902
4903 -- STEP 7: check for invalid components + check type in choice list
4904
4905 Step_7 : declare
937e9676
AC
4906 Assoc : Node_Id;
4907 New_Assoc : Node_Id;
4908
996ae0b0
RK
4909 Selectr : Node_Id;
4910 -- Selector name
4911
9b96e234 4912 Typech : Entity_Id;
996ae0b0
RK
4913 -- Type of first component in choice list
4914
4915 begin
4916 if Present (Component_Associations (N)) then
4917 Assoc := First (Component_Associations (N));
4918 else
4919 Assoc := Empty;
4920 end if;
4921
4922 Verification : while Present (Assoc) loop
4923 Selectr := First (Choices (Assoc));
4924 Typech := Empty;
4925
4926 if Nkind (Selectr) = N_Others_Choice then
19f0526a 4927
9b96e234 4928 -- Ada 2005 (AI-287): others choice may have expression or box
19f0526a 4929
ec3c7387 4930 if No (Others_Etype) and then Others_Box = 0 then
ed2233dc 4931 Error_Msg_N
996ae0b0 4932 ("OTHERS must represent at least one component", Selectr);
ec3c7387
AC
4933
4934 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
4935 Error_Msg_N ("others choice is redundant?", Box_Node);
bd717ec9
AC
4936 Error_Msg_N
4937 ("\previous choices cover all components?", Box_Node);
996ae0b0
RK
4938 end if;
4939
4940 exit Verification;
4941 end if;
4942
4943 while Present (Selectr) loop
4944 New_Assoc := First (New_Assoc_List);
4945 while Present (New_Assoc) loop
4946 Component := First (Choices (New_Assoc));
6989bc1f
AC
4947
4948 if Chars (Selectr) = Chars (Component) then
4949 if Style_Check then
4950 Check_Identifier (Selectr, Entity (Component));
4951 end if;
4952
4953 exit;
4954 end if;
4955
996ae0b0
RK
4956 Next (New_Assoc);
4957 end loop;
4958
c7e152b5
AC
4959 -- If no association, this is not a legal component of the type
4960 -- in question, unless its association is provided with a box.
996ae0b0
RK
4961
4962 if No (New_Assoc) then
65356e64 4963 if Box_Present (Parent (Selectr)) then
aad93b55
ES
4964
4965 -- This may still be a bogus component with a box. Scan
4966 -- list of components to verify that a component with
4967 -- that name exists.
4968
4969 declare
4970 C : Entity_Id;
4971
4972 begin
4973 C := First_Component (Typ);
4974 while Present (C) loop
4975 if Chars (C) = Chars (Selectr) then
ca44152f
ES
4976
4977 -- If the context is an extension aggregate,
4978 -- the component must not be inherited from
4979 -- the ancestor part of the aggregate.
4980
4981 if Nkind (N) /= N_Extension_Aggregate
4982 or else
4983 Scope (Original_Record_Component (C)) /=
937e9676 4984 Etype (Ancestor_Part (N))
ca44152f
ES
4985 then
4986 exit;
4987 end if;
aad93b55
ES
4988 end if;
4989
4990 Next_Component (C);
4991 end loop;
4992
4993 if No (C) then
4994 Error_Msg_Node_2 := Typ;
4995 Error_Msg_N ("& is not a component of}", Selectr);
4996 end if;
4997 end;
996ae0b0 4998
65356e64 4999 elsif Chars (Selectr) /= Name_uTag
996ae0b0 5000 and then Chars (Selectr) /= Name_uParent
996ae0b0
RK
5001 then
5002 if not Has_Discriminants (Typ) then
5003 Error_Msg_Node_2 := Typ;
aad93b55 5004 Error_Msg_N ("& is not a component of}", Selectr);
996ae0b0
RK
5005 else
5006 Error_Msg_N
5007 ("& is not a component of the aggregate subtype",
5008 Selectr);
5009 end if;
5010
5011 Check_Misspelled_Component (Components, Selectr);
5012 end if;
5013
5014 elsif No (Typech) then
5015 Typech := Base_Type (Etype (Component));
5016
feab3549 5017 -- AI05-0199: In Ada 2012, several components of anonymous
8da337c5
AC
5018 -- access types can appear in a choice list, as long as the
5019 -- designated types match.
5020
996ae0b0 5021 elsif Typech /= Base_Type (Etype (Component)) then
dbe945f1 5022 if Ada_Version >= Ada_2012
8da337c5
AC
5023 and then Ekind (Typech) = E_Anonymous_Access_Type
5024 and then
5025 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5026 and then Base_Type (Designated_Type (Typech)) =
5027 Base_Type (Designated_Type (Etype (Component)))
5028 and then
5029 Subtypes_Statically_Match (Typech, (Etype (Component)))
5030 then
5031 null;
5032
5033 elsif not Box_Present (Parent (Selectr)) then
65356e64
AC
5034 Error_Msg_N
5035 ("components in choice list must have same type",
5036 Selectr);
5037 end if;
996ae0b0
RK
5038 end if;
5039
5040 Next (Selectr);
5041 end loop;
5042
5043 Next (Assoc);
5044 end loop Verification;
5045 end Step_7;
5046
5047 -- STEP 8: replace the original aggregate
5048
5049 Step_8 : declare
fbf5a39b 5050 New_Aggregate : constant Node_Id := New_Copy (N);
996ae0b0
RK
5051
5052 begin
5053 Set_Expressions (New_Aggregate, No_List);
5054 Set_Etype (New_Aggregate, Etype (N));
5055 Set_Component_Associations (New_Aggregate, New_Assoc_List);
288cbbbd 5056 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
996ae0b0
RK
5057
5058 Rewrite (N, New_Aggregate);
5059 end Step_8;
0929eaeb 5060
d976bf74 5061 -- Check the dimensions of the components in the record aggregate
0929eaeb
AC
5062
5063 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
996ae0b0
RK
5064 end Resolve_Record_Aggregate;
5065
2820d220
AC
5066 -----------------------------
5067 -- Check_Can_Never_Be_Null --
5068 -----------------------------
5069
9b96e234 5070 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
ec53a6da
JM
5071 Comp_Typ : Entity_Id;
5072
2820d220 5073 begin
9b96e234 5074 pragma Assert
0791fbe9 5075 (Ada_Version >= Ada_2005
9b96e234 5076 and then Present (Expr)
8133b9d1 5077 and then Known_Null (Expr));
82c80734 5078
ec53a6da
JM
5079 case Ekind (Typ) is
5080 when E_Array_Type =>
5081 Comp_Typ := Component_Type (Typ);
5082
d8f43ee6
HK
5083 when E_Component
5084 | E_Discriminant
5085 =>
ec53a6da
JM
5086 Comp_Typ := Etype (Typ);
5087
5088 when others =>
5089 return;
5090 end case;
5091
9b96e234
JM
5092 if Can_Never_Be_Null (Comp_Typ) then
5093
5094 -- Here we know we have a constraint error. Note that we do not use
5095 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5096 -- seem the more natural approach. That's because in some cases the
5097 -- components are rewritten, and the replacement would be missed.
5a521b8a
AC
5098 -- We do not mark the whole aggregate as raising a constraint error,
5099 -- because the association may be a null array range.
9b96e234 5100
5a521b8a
AC
5101 Error_Msg_N
5102 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5103 Error_Msg_N
b785e0b8 5104 ("\Constraint_Error will be raised at run time??", Expr);
9b96e234 5105
5a521b8a
AC
5106 Rewrite (Expr,
5107 Make_Raise_Constraint_Error
5108 (Sloc (Expr), Reason => CE_Access_Check_Failed));
9b96e234
JM
5109 Set_Etype (Expr, Comp_Typ);
5110 Set_Analyzed (Expr);
2820d220
AC
5111 end if;
5112 end Check_Can_Never_Be_Null;
5113
996ae0b0
RK
5114 ---------------------
5115 -- Sort_Case_Table --
5116 ---------------------
5117
5118 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
fbf5a39b 5119 U : constant Int := Case_Table'Last;
996ae0b0
RK
5120 K : Int;
5121 J : Int;
5122 T : Case_Bounds;
5123
5124 begin
82893775
AC
5125 K := 1;
5126 while K < U loop
996ae0b0 5127 T := Case_Table (K + 1);
996ae0b0 5128
7f9747c6 5129 J := K + 1;
82893775
AC
5130 while J > 1
5131 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
996ae0b0
RK
5132 loop
5133 Case_Table (J) := Case_Table (J - 1);
5134 J := J - 1;
5135 end loop;
5136
5137 Case_Table (J) := T;
5138 K := K + 1;
5139 end loop;
5140 end Sort_Case_Table;
5141
5142end Sem_Aggr;
This page took 6.694891 seconds and 5 git commands to generate.