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