comparison gcc/ada/sem_aggr.adb @ 111:04ced10e8804

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