[multiple changes]
[gcc.git] / gcc / ada / sem_aggr.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3023 Loc : constant Source_Ptr := Sloc (N);
3024 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3025 Obj_Decl : constant Node_Id :=
3026 Make_Object_Declaration
3027 (Loc,
3028 Defining_Identifier => Def_Id,
3029 Constant_Present => True,
3030 Object_Definition => New_Occurrence_Of (Typ, Loc),
3031 Expression => A,
3032 Has_Init_Expression => True);
3033 begin
3034 Set_Etype (Def_Id, Typ);
3035 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3036 Insert_Action (N, Obj_Decl);
3037 end Transform_BIP_Assignment;
3038
3039 -- Start of processing for Resolve_Extension_Aggregate
3040
3041 begin
3042 -- Analyze the ancestor part and account for the case where it is a
3043 -- parameterless function call.
3044
3045 Analyze (A);
3046 Check_Parameterless_Call (A);
3047
3048 -- In SPARK, the ancestor part cannot be a type mark
3049
3050 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3051 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3052
3053 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3054 -- must not have unknown discriminants.
3055
3056 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3057 Error_Msg_NE
3058 ("aggregate not available for type& whose ancestor "
3059 & "has unknown discriminants", N, Typ);
3060 end if;
3061 end if;
3062
3063 if not Is_Tagged_Type (Typ) then
3064 Error_Msg_N ("type of extension aggregate must be tagged", N);
3065 return;
3066
3067 elsif Is_Limited_Type (Typ) then
3068
3069 -- Ada 2005 (AI-287): Limited aggregates are allowed
3070
3071 if Ada_Version < Ada_2005 then
3072 Error_Msg_N ("aggregate type cannot be limited", N);
3073 Explain_Limited_Type (Typ, N);
3074 return;
3075
3076 elsif Valid_Limited_Ancestor (A) then
3077 null;
3078
3079 else
3080 Error_Msg_N
3081 ("limited ancestor part must be aggregate or function call", A);
3082 end if;
3083
3084 elsif Is_Class_Wide_Type (Typ) then
3085 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3086 return;
3087 end if;
3088
3089 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3090 A_Type := Get_Full_View (Entity (A));
3091
3092 if Valid_Ancestor_Type then
3093 Set_Entity (A, A_Type);
3094 Set_Etype (A, A_Type);
3095
3096 Validate_Ancestor_Part (N);
3097 Resolve_Record_Aggregate (N, Typ);
3098 end if;
3099
3100 elsif Nkind (A) /= N_Aggregate then
3101 if Is_Overloaded (A) then
3102 A_Type := Any_Type;
3103
3104 Get_First_Interp (A, I, It);
3105 while Present (It.Typ) loop
3106
3107 -- Consider limited interpretations if Ada 2005 or higher
3108
3109 if Is_Tagged_Type (It.Typ)
3110 and then (Ada_Version >= Ada_2005
3111 or else not Is_Limited_Type (It.Typ))
3112 then
3113 if A_Type /= Any_Type then
3114 Error_Msg_N ("cannot resolve expression", A);
3115 return;
3116 else
3117 A_Type := It.Typ;
3118 end if;
3119 end if;
3120
3121 Get_Next_Interp (I, It);
3122 end loop;
3123
3124 if A_Type = Any_Type then
3125 if Ada_Version >= Ada_2005 then
3126 Error_Msg_N
3127 ("ancestor part must be of a tagged type", A);
3128 else
3129 Error_Msg_N
3130 ("ancestor part must be of a nonlimited tagged type", A);
3131 end if;
3132
3133 return;
3134 end if;
3135
3136 else
3137 A_Type := Etype (A);
3138 end if;
3139
3140 if Valid_Ancestor_Type then
3141 Resolve (A, A_Type);
3142 Check_Unset_Reference (A);
3143 Check_Non_Static_Context (A);
3144
3145 -- The aggregate is illegal if the ancestor expression is a call
3146 -- to a function with a limited unconstrained result, unless the
3147 -- type of the aggregate is a null extension. This restriction
3148 -- was added in AI05-67 to simplify implementation.
3149
3150 if Nkind (A) = N_Function_Call
3151 and then Is_Limited_Type (A_Type)
3152 and then not Is_Null_Extension (Typ)
3153 and then not Is_Constrained (A_Type)
3154 then
3155 Error_Msg_N
3156 ("type of limited ancestor part must be constrained", A);
3157
3158 -- Reject the use of CPP constructors that leave objects partially
3159 -- initialized. For example:
3160
3161 -- type CPP_Root is tagged limited record ...
3162 -- pragma Import (CPP, CPP_Root);
3163
3164 -- type CPP_DT is new CPP_Root and Iface ...
3165 -- pragma Import (CPP, CPP_DT);
3166
3167 -- type Ada_DT is new CPP_DT with ...
3168
3169 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3170
3171 -- Using the constructor of CPP_Root the slots of the dispatch
3172 -- table of CPP_DT cannot be set, and the secondary tag of
3173 -- CPP_DT is unknown.
3174
3175 elsif Nkind (A) = N_Function_Call
3176 and then Is_CPP_Constructor_Call (A)
3177 and then Enclosing_CPP_Parent (Typ) /= A_Type
3178 then
3179 Error_Msg_NE
3180 ("??must use 'C'P'P constructor for type &", A,
3181 Enclosing_CPP_Parent (Typ));
3182
3183 -- The following call is not needed if the previous warning
3184 -- is promoted to an error.
3185
3186 Resolve_Record_Aggregate (N, Typ);
3187
3188 elsif Is_Class_Wide_Type (Etype (A))
3189 and then Nkind (Original_Node (A)) = N_Function_Call
3190 then
3191 -- If the ancestor part is a dispatching call, it appears
3192 -- statically to be a legal ancestor, but it yields any member
3193 -- of the class, and it is not possible to determine whether
3194 -- it is an ancestor of the extension aggregate (much less
3195 -- which ancestor). It is not possible to determine the
3196 -- components of the extension part.
3197
3198 -- This check implements AI-306, which in fact was motivated by
3199 -- an AdaCore query to the ARG after this test was added.
3200
3201 Error_Msg_N ("ancestor part must be statically tagged", A);
3202 else
3203 -- We are using the build-in-place protocol, but we can't build
3204 -- in place, because we need to call the function before
3205 -- allocating the aggregate. Could do better for null
3206 -- extensions, and maybe for nondiscriminated types.
3207 -- This is wrong for limited, but those were wrong already.
3208
3209 if not Is_Limited_View (A_Type)
3210 and then Is_Build_In_Place_Function_Call (A)
3211 then
3212 Transform_BIP_Assignment (A_Type);
3213 end if;
3214
3215 Resolve_Record_Aggregate (N, Typ);
3216 end if;
3217 end if;
3218
3219 else
3220 Error_Msg_N ("no unique type for this aggregate", A);
3221 end if;
3222
3223 Check_Function_Writable_Actuals (N);
3224 end Resolve_Extension_Aggregate;
3225
3226 ------------------------------
3227 -- Resolve_Record_Aggregate --
3228 ------------------------------
3229
3230 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3231 New_Assoc_List : constant List_Id := New_List;
3232 -- New_Assoc_List is the newly built list of N_Component_Association
3233 -- nodes.
3234
3235 Others_Etype : Entity_Id := Empty;
3236 -- This variable is used to save the Etype of the last record component
3237 -- that takes its value from the others choice. Its purpose is:
3238 --
3239 -- (a) make sure the others choice is useful
3240 --
3241 -- (b) make sure the type of all the components whose value is
3242 -- subsumed by the others choice are the same.
3243 --
3244 -- This variable is updated as a side effect of function Get_Value.
3245
3246 Box_Node : Node_Id := Empty;
3247 Is_Box_Present : Boolean := False;
3248 Others_Box : Integer := 0;
3249 -- Ada 2005 (AI-287): Variables used in case of default initialization
3250 -- to provide a functionality similar to Others_Etype. Box_Present
3251 -- indicates that the component takes its default initialization;
3252 -- Others_Box counts the number of components of the current aggregate
3253 -- (which may be a sub-aggregate of a larger one) that are default-
3254 -- initialized. A value of One indicates that an others_box is present.
3255 -- Any larger value indicates that the others_box is not redundant.
3256 -- These variables, similar to Others_Etype, are also updated as a side
3257 -- effect of function Get_Value. Box_Node is used to place a warning on
3258 -- a redundant others_box.
3259
3260 procedure Add_Association
3261 (Component : Entity_Id;
3262 Expr : Node_Id;
3263 Assoc_List : List_Id;
3264 Is_Box_Present : Boolean := False);
3265 -- Builds a new N_Component_Association node which associates Component
3266 -- to expression Expr and adds it to the association list being built,
3267 -- either New_Assoc_List, or the association being built for an inner
3268 -- aggregate.
3269
3270 procedure Add_Discriminant_Values
3271 (New_Aggr : Node_Id;
3272 Assoc_List : List_Id);
3273 -- The constraint to a component may be given by a discriminant of the
3274 -- enclosing type, in which case we have to retrieve its value, which is
3275 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3276 -- associations of the current type or of some enclosing record.
3277
3278 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3279 -- If aggregate N is a regular aggregate this routine will return True.
3280 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3281 -- a discriminant whose value may already have been specified by N's
3282 -- ancestor part. This routine checks whether this is indeed the case
3283 -- and if so returns False, signaling that no value for Input_Discr
3284 -- should appear in N's aggregate part. Also, in this case, the routine
3285 -- appends to New_Assoc_List the discriminant value specified in the
3286 -- ancestor part.
3287 --
3288 -- If the aggregate is in a context with expansion delayed, it will be
3289 -- reanalyzed. The inherited discriminant values must not be reinserted
3290 -- in the component list to prevent spurious errors, but they must be
3291 -- present on first analysis to build the proper subtype indications.
3292 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3293
3294 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3295 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3296 -- derived from private view Typ. Whether the aggregate is legal depends
3297 -- on the current visibility of the type as well as that of the parent
3298 -- of the ancestor.
3299
3300 function Get_Value
3301 (Compon : Node_Id;
3302 From : List_Id;
3303 Consider_Others_Choice : Boolean := False) return Node_Id;
3304 -- Given a record component stored in parameter Compon, this function
3305 -- returns its value as it appears in the list From, which is a list
3306 -- of N_Component_Association nodes.
3307 --
3308 -- If no component association has a choice for the searched component,
3309 -- the value provided by the others choice is returned, if there is one,
3310 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3311 -- returned. If there is more than one component association giving a
3312 -- value for the searched record component, an error message is emitted
3313 -- and the first found value is returned.
3314 --
3315 -- If Consider_Others_Choice is set and the returned expression comes
3316 -- from the others choice, then Others_Etype is set as a side effect.
3317 -- An error message is emitted if the components taking their value from
3318 -- the others choice do not have same type.
3319
3320 procedure Propagate_Discriminants
3321 (Aggr : Node_Id;
3322 Assoc_List : List_Id);
3323 -- Nested components may themselves be discriminated types constrained
3324 -- by outer discriminants, whose values must be captured before the
3325 -- aggregate is expanded into assignments.
3326
3327 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3328 -- Analyzes and resolves expression Expr against the Etype of the
3329 -- Component. This routine also applies all appropriate checks to Expr.
3330 -- It finally saves a Expr in the newly created association list that
3331 -- will be attached to the final record aggregate. Note that if the
3332 -- Parent pointer of Expr is not set then Expr was produced with a
3333 -- New_Copy_Tree or some such.
3334
3335 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3336 -- Rewrite a range node Rge when its bounds refer to non-stored
3337 -- discriminants from Root_Type, to replace them with the stored
3338 -- discriminant values. This is required in GNATprove mode, and is
3339 -- adopted in all modes to avoid special-casing GNATprove mode.
3340
3341 ---------------------
3342 -- Add_Association --
3343 ---------------------
3344
3345 procedure Add_Association
3346 (Component : Entity_Id;
3347 Expr : Node_Id;
3348 Assoc_List : List_Id;
3349 Is_Box_Present : Boolean := False)
3350 is
3351 Choice_List : constant List_Id := New_List;
3352 Loc : Source_Ptr;
3353
3354 begin
3355 -- If this is a box association the expression is missing, so use the
3356 -- Sloc of the aggregate itself for the new association.
3357
3358 if Present (Expr) then
3359 Loc := Sloc (Expr);
3360 else
3361 Loc := Sloc (N);
3362 end if;
3363
3364 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3365
3366 Append_To (Assoc_List,
3367 Make_Component_Association (Loc,
3368 Choices => Choice_List,
3369 Expression => Expr,
3370 Box_Present => Is_Box_Present));
3371 end Add_Association;
3372
3373 -----------------------------
3374 -- Add_Discriminant_Values --
3375 -----------------------------
3376
3377 procedure Add_Discriminant_Values
3378 (New_Aggr : Node_Id;
3379 Assoc_List : List_Id)
3380 is
3381 Assoc : Node_Id;
3382 Discr : Entity_Id;
3383 Discr_Elmt : Elmt_Id;
3384 Discr_Val : Node_Id;
3385 Val : Entity_Id;
3386
3387 begin
3388 Discr := First_Discriminant (Etype (New_Aggr));
3389 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3390 while Present (Discr_Elmt) loop
3391 Discr_Val := Node (Discr_Elmt);
3392
3393 -- If the constraint is given by a discriminant then it is a
3394 -- discriminant of an enclosing record, and its value has already
3395 -- been placed in the association list.
3396
3397 if Is_Entity_Name (Discr_Val)
3398 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3399 then
3400 Val := Entity (Discr_Val);
3401
3402 Assoc := First (Assoc_List);
3403 while Present (Assoc) loop
3404 if Present (Entity (First (Choices (Assoc))))
3405 and then Entity (First (Choices (Assoc))) = Val
3406 then
3407 Discr_Val := Expression (Assoc);
3408 exit;
3409 end if;
3410
3411 Next (Assoc);
3412 end loop;
3413 end if;
3414
3415 Add_Association
3416 (Discr, New_Copy_Tree (Discr_Val),
3417 Component_Associations (New_Aggr));
3418
3419 -- If the discriminant constraint is a current instance, mark the
3420 -- current aggregate so that the self-reference can be expanded
3421 -- later. The constraint may refer to the subtype of aggregate, so
3422 -- use base type for comparison.
3423
3424 if Nkind (Discr_Val) = N_Attribute_Reference
3425 and then Is_Entity_Name (Prefix (Discr_Val))
3426 and then Is_Type (Entity (Prefix (Discr_Val)))
3427 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3428 then
3429 Set_Has_Self_Reference (N);
3430 end if;
3431
3432 Next_Elmt (Discr_Elmt);
3433 Next_Discriminant (Discr);
3434 end loop;
3435 end Add_Discriminant_Values;
3436
3437 --------------------------
3438 -- Discriminant_Present --
3439 --------------------------
3440
3441 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3442 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3443
3444 Ancestor_Is_Subtyp : Boolean;
3445
3446 Loc : Source_Ptr;
3447
3448 Ancestor : Node_Id;
3449 Ancestor_Typ : Entity_Id;
3450 Comp_Assoc : Node_Id;
3451 Discr : Entity_Id;
3452 Discr_Expr : Node_Id;
3453 Discr_Val : Elmt_Id := No_Elmt;
3454 Orig_Discr : Entity_Id;
3455
3456 begin
3457 if Regular_Aggr then
3458 return True;
3459 end if;
3460
3461 -- Check whether inherited discriminant values have already been
3462 -- inserted in the aggregate. This will be the case if we are
3463 -- re-analyzing an aggregate whose expansion was delayed.
3464
3465 if Present (Component_Associations (N)) then
3466 Comp_Assoc := First (Component_Associations (N));
3467 while Present (Comp_Assoc) loop
3468 if Inherited_Discriminant (Comp_Assoc) then
3469 return True;
3470 end if;
3471
3472 Next (Comp_Assoc);
3473 end loop;
3474 end if;
3475
3476 Ancestor := Ancestor_Part (N);
3477 Ancestor_Typ := Etype (Ancestor);
3478 Loc := Sloc (Ancestor);
3479
3480 -- For a private type with unknown discriminants, use the underlying
3481 -- record view if it is available.
3482
3483 if Has_Unknown_Discriminants (Ancestor_Typ)
3484 and then Present (Full_View (Ancestor_Typ))
3485 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3486 then
3487 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3488 end if;
3489
3490 Ancestor_Is_Subtyp :=
3491 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3492
3493 -- If the ancestor part has no discriminants clearly N's aggregate
3494 -- part must provide a value for Discr.
3495
3496 if not Has_Discriminants (Ancestor_Typ) then
3497 return True;
3498
3499 -- If the ancestor part is an unconstrained subtype mark then the
3500 -- Discr must be present in N's aggregate part.
3501
3502 elsif Ancestor_Is_Subtyp
3503 and then not Is_Constrained (Entity (Ancestor))
3504 then
3505 return True;
3506 end if;
3507
3508 -- Now look to see if Discr was specified in the ancestor part
3509
3510 if Ancestor_Is_Subtyp then
3511 Discr_Val :=
3512 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3513 end if;
3514
3515 Orig_Discr := Original_Record_Component (Input_Discr);
3516
3517 Discr := First_Discriminant (Ancestor_Typ);
3518 while Present (Discr) loop
3519
3520 -- If Ancestor has already specified Disc value then insert its
3521 -- value in the final aggregate.
3522
3523 if Original_Record_Component (Discr) = Orig_Discr then
3524 if Ancestor_Is_Subtyp then
3525 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3526 else
3527 Discr_Expr :=
3528 Make_Selected_Component (Loc,
3529 Prefix => Duplicate_Subexpr (Ancestor),
3530 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3531 end if;
3532
3533 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3534 Set_Inherited_Discriminant (Last (New_Assoc_List));
3535 return False;
3536 end if;
3537
3538 Next_Discriminant (Discr);
3539
3540 if Ancestor_Is_Subtyp then
3541 Next_Elmt (Discr_Val);
3542 end if;
3543 end loop;
3544
3545 return True;
3546 end Discriminant_Present;
3547
3548 ---------------------------
3549 -- Find_Private_Ancestor --
3550 ---------------------------
3551
3552 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3553 Par : Entity_Id;
3554
3555 begin
3556 Par := Typ;
3557 loop
3558 if Has_Private_Ancestor (Par)
3559 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3560 then
3561 return Par;
3562
3563 elsif not Is_Derived_Type (Par) then
3564 return Empty;
3565
3566 else
3567 Par := Etype (Base_Type (Par));
3568 end if;
3569 end loop;
3570 end Find_Private_Ancestor;
3571
3572 ---------------
3573 -- Get_Value --
3574 ---------------
3575
3576 function Get_Value
3577 (Compon : Node_Id;
3578 From : List_Id;
3579 Consider_Others_Choice : Boolean := False) return Node_Id
3580 is
3581 Typ : constant Entity_Id := Etype (Compon);
3582 Assoc : Node_Id;
3583 Expr : Node_Id := Empty;
3584 Selector_Name : Node_Id;
3585
3586 begin
3587 Is_Box_Present := False;
3588
3589 if No (From) then
3590 return Empty;
3591 end if;
3592
3593 Assoc := First (From);
3594 while Present (Assoc) loop
3595 Selector_Name := First (Choices (Assoc));
3596 while Present (Selector_Name) loop
3597 if Nkind (Selector_Name) = N_Others_Choice then
3598 if Consider_Others_Choice and then No (Expr) then
3599
3600 -- We need to duplicate the expression for each
3601 -- successive component covered by the others choice.
3602 -- This is redundant if the others_choice covers only
3603 -- one component (small optimization possible???), but
3604 -- indispensable otherwise, because each one must be
3605 -- expanded individually to preserve side-effects.
3606
3607 -- Ada 2005 (AI-287): In case of default initialization
3608 -- of components, we duplicate the corresponding default
3609 -- expression (from the record type declaration). The
3610 -- copy must carry the sloc of the association (not the
3611 -- original expression) to prevent spurious elaboration
3612 -- checks when the default includes function calls.
3613
3614 if Box_Present (Assoc) then
3615 Others_Box := Others_Box + 1;
3616 Is_Box_Present := True;
3617
3618 if Expander_Active then
3619 return
3620 New_Copy_Tree_And_Copy_Dimensions
3621 (Expression (Parent (Compon)),
3622 New_Sloc => Sloc (Assoc));
3623 else
3624 return Expression (Parent (Compon));
3625 end if;
3626
3627 else
3628 if Present (Others_Etype)
3629 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3630 then
3631 -- If the components are of an anonymous access
3632 -- type they are distinct, but this is legal in
3633 -- Ada 2012 as long as designated types match.
3634
3635 if (Ekind (Typ) = E_Anonymous_Access_Type
3636 or else Ekind (Typ) =
3637 E_Anonymous_Access_Subprogram_Type)
3638 and then Designated_Type (Typ) =
3639 Designated_Type (Others_Etype)
3640 then
3641 null;
3642 else
3643 Error_Msg_N
3644 ("components in OTHERS choice must have same "
3645 & "type", Selector_Name);
3646 end if;
3647 end if;
3648
3649 Others_Etype := Typ;
3650
3651 -- Copy the expression so that it is resolved
3652 -- independently for each component, This is needed
3653 -- for accessibility checks on compoents of anonymous
3654 -- access types, even in compile_only mode.
3655
3656 if not Inside_A_Generic then
3657
3658 -- In ASIS mode, preanalyze the expression in an
3659 -- others association before making copies for
3660 -- separate resolution and accessibility checks.
3661 -- This ensures that the type of the expression is
3662 -- available to ASIS in all cases, in particular if
3663 -- the expression is itself an aggregate.
3664
3665 if ASIS_Mode then
3666 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3667 end if;
3668
3669 return
3670 New_Copy_Tree_And_Copy_Dimensions
3671 (Expression (Assoc));
3672
3673 else
3674 return Expression (Assoc);
3675 end if;
3676 end if;
3677 end if;
3678
3679 elsif Chars (Compon) = Chars (Selector_Name) then
3680 if No (Expr) then
3681
3682 -- Ada 2005 (AI-231)
3683
3684 if Ada_Version >= Ada_2005
3685 and then Known_Null (Expression (Assoc))
3686 then
3687 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3688 end if;
3689
3690 -- We need to duplicate the expression when several
3691 -- components are grouped together with a "|" choice.
3692 -- For instance "filed1 | filed2 => Expr"
3693
3694 -- Ada 2005 (AI-287)
3695
3696 if Box_Present (Assoc) then
3697 Is_Box_Present := True;
3698
3699 -- Duplicate the default expression of the component
3700 -- from the record type declaration, so a new copy
3701 -- can be attached to the association.
3702
3703 -- Note that we always copy the default expression,
3704 -- even when the association has a single choice, in
3705 -- order to create a proper association for the
3706 -- expanded aggregate.
3707
3708 -- Component may have no default, in which case the
3709 -- expression is empty and the component is default-
3710 -- initialized, but an association for the component
3711 -- exists, and it is not covered by an others clause.
3712
3713 -- Scalar and private types have no initialization
3714 -- procedure, so they remain uninitialized. If the
3715 -- target of the aggregate is a constant this
3716 -- deserves a warning.
3717
3718 if No (Expression (Parent (Compon)))
3719 and then not Has_Non_Null_Base_Init_Proc (Typ)
3720 and then not Has_Aspect (Typ, Aspect_Default_Value)
3721 and then not Is_Concurrent_Type (Typ)
3722 and then Nkind (Parent (N)) = N_Object_Declaration
3723 and then Constant_Present (Parent (N))
3724 then
3725 Error_Msg_Node_2 := Typ;
3726 Error_Msg_NE
3727 ("component&? of type& is uninitialized",
3728 Assoc, Selector_Name);
3729
3730 -- An additional reminder if the component type
3731 -- is a generic formal.
3732
3733 if Is_Generic_Type (Base_Type (Typ)) then
3734 Error_Msg_NE
3735 ("\instance should provide actual type with "
3736 & "initialization for&", Assoc, Typ);
3737 end if;
3738 end if;
3739
3740 return
3741 New_Copy_Tree_And_Copy_Dimensions
3742 (Expression (Parent (Compon)));
3743
3744 else
3745 if Present (Next (Selector_Name)) then
3746 Expr := New_Copy_Tree_And_Copy_Dimensions
3747 (Expression (Assoc));
3748 else
3749 Expr := Expression (Assoc);
3750 end if;
3751 end if;
3752
3753 Generate_Reference (Compon, Selector_Name, 'm');
3754
3755 else
3756 Error_Msg_NE
3757 ("more than one value supplied for &",
3758 Selector_Name, Compon);
3759
3760 end if;
3761 end if;
3762
3763 Next (Selector_Name);
3764 end loop;
3765
3766 Next (Assoc);
3767 end loop;
3768
3769 return Expr;
3770 end Get_Value;
3771
3772 -----------------------------
3773 -- Propagate_Discriminants --
3774 -----------------------------
3775
3776 procedure Propagate_Discriminants
3777 (Aggr : Node_Id;
3778 Assoc_List : List_Id)
3779 is
3780 Loc : constant Source_Ptr := Sloc (N);
3781
3782 Needs_Box : Boolean := False;
3783
3784 procedure Process_Component (Comp : Entity_Id);
3785 -- Add one component with a box association to the inner aggregate,
3786 -- and recurse if component is itself composite.
3787
3788 -----------------------
3789 -- Process_Component --
3790 -----------------------
3791
3792 procedure Process_Component (Comp : Entity_Id) is
3793 T : constant Entity_Id := Etype (Comp);
3794 New_Aggr : Node_Id;
3795
3796 begin
3797 if Is_Record_Type (T) and then Has_Discriminants (T) then
3798 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3799 Set_Etype (New_Aggr, T);
3800
3801 Add_Association
3802 (Comp, New_Aggr, Component_Associations (Aggr));
3803
3804 -- Collect discriminant values and recurse
3805
3806 Add_Discriminant_Values (New_Aggr, Assoc_List);
3807 Propagate_Discriminants (New_Aggr, Assoc_List);
3808
3809 else
3810 Needs_Box := True;
3811 end if;
3812 end Process_Component;
3813
3814 -- Local variables
3815
3816 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3817 Components : constant Elist_Id := New_Elmt_List;
3818 Def_Node : constant Node_Id :=
3819 Type_Definition (Declaration_Node (Aggr_Type));
3820
3821 Comp : Node_Id;
3822 Comp_Elmt : Elmt_Id;
3823 Errors : Boolean;
3824
3825 -- Start of processing for Propagate_Discriminants
3826
3827 begin
3828 -- The component type may be a variant type. Collect the components
3829 -- that are ruled by the known values of the discriminants. Their
3830 -- values have already been inserted into the component list of the
3831 -- current aggregate.
3832
3833 if Nkind (Def_Node) = N_Record_Definition
3834 and then Present (Component_List (Def_Node))
3835 and then Present (Variant_Part (Component_List (Def_Node)))
3836 then
3837 Gather_Components (Aggr_Type,
3838 Component_List (Def_Node),
3839 Governed_By => Component_Associations (Aggr),
3840 Into => Components,
3841 Report_Errors => Errors);
3842
3843 Comp_Elmt := First_Elmt (Components);
3844 while Present (Comp_Elmt) loop
3845 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
3846 Process_Component (Node (Comp_Elmt));
3847 end if;
3848
3849 Next_Elmt (Comp_Elmt);
3850 end loop;
3851
3852 -- No variant part, iterate over all components
3853
3854 else
3855 Comp := First_Component (Etype (Aggr));
3856 while Present (Comp) loop
3857 Process_Component (Comp);
3858 Next_Component (Comp);
3859 end loop;
3860 end if;
3861
3862 if Needs_Box then
3863 Append_To (Component_Associations (Aggr),
3864 Make_Component_Association (Loc,
3865 Choices => New_List (Make_Others_Choice (Loc)),
3866 Expression => Empty,
3867 Box_Present => True));
3868 end if;
3869 end Propagate_Discriminants;
3870
3871 -----------------------
3872 -- Resolve_Aggr_Expr --
3873 -----------------------
3874
3875 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
3876 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3877 -- If the expression is an aggregate (possibly qualified) then its
3878 -- expansion is delayed until the enclosing aggregate is expanded
3879 -- into assignments. In that case, do not generate checks on the
3880 -- expression, because they will be generated later, and will other-
3881 -- wise force a copy (to remove side-effects) that would leave a
3882 -- dynamic-sized aggregate in the code, something that gigi cannot
3883 -- handle.
3884
3885 ---------------------------
3886 -- Has_Expansion_Delayed --
3887 ---------------------------
3888
3889 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3890 begin
3891 return
3892 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
3893 and then Present (Etype (Expr))
3894 and then Is_Record_Type (Etype (Expr))
3895 and then Expansion_Delayed (Expr))
3896 or else
3897 (Nkind (Expr) = N_Qualified_Expression
3898 and then Has_Expansion_Delayed (Expression (Expr)));
3899 end Has_Expansion_Delayed;
3900
3901 -- Local variables
3902
3903 Expr_Type : Entity_Id := Empty;
3904 New_C : Entity_Id := Component;
3905 New_Expr : Node_Id;
3906
3907 Relocate : Boolean;
3908 -- Set to True if the resolved Expr node needs to be relocated when
3909 -- attached to the newly created association list. This node need not
3910 -- be relocated if its parent pointer is not set. In fact in this
3911 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3912 -- True then we have analyzed the expression node in the original
3913 -- aggregate and hence it needs to be relocated when moved over to
3914 -- the new association list.
3915
3916 -- Start of processing for Resolve_Aggr_Expr
3917
3918 begin
3919 -- If the type of the component is elementary or the type of the
3920 -- aggregate does not contain discriminants, use the type of the
3921 -- component to resolve Expr.
3922
3923 if Is_Elementary_Type (Etype (Component))
3924 or else not Has_Discriminants (Etype (N))
3925 then
3926 Expr_Type := Etype (Component);
3927
3928 -- Otherwise we have to pick up the new type of the component from
3929 -- the new constrained subtype of the aggregate. In fact components
3930 -- which are of a composite type might be constrained by a
3931 -- discriminant, and we want to resolve Expr against the subtype were
3932 -- all discriminant occurrences are replaced with their actual value.
3933
3934 else
3935 New_C := First_Component (Etype (N));
3936 while Present (New_C) loop
3937 if Chars (New_C) = Chars (Component) then
3938 Expr_Type := Etype (New_C);
3939 exit;
3940 end if;
3941
3942 Next_Component (New_C);
3943 end loop;
3944
3945 pragma Assert (Present (Expr_Type));
3946
3947 -- For each range in an array type where a discriminant has been
3948 -- replaced with the constraint, check that this range is within
3949 -- the range of the base type. This checks is done in the init
3950 -- proc for regular objects, but has to be done here for
3951 -- aggregates since no init proc is called for them.
3952
3953 if Is_Array_Type (Expr_Type) then
3954 declare
3955 Index : Node_Id;
3956 -- Range of the current constrained index in the array
3957
3958 Orig_Index : Node_Id := First_Index (Etype (Component));
3959 -- Range corresponding to the range Index above in the
3960 -- original unconstrained record type. The bounds of this
3961 -- range may be governed by discriminants.
3962
3963 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3964 -- Range corresponding to the range Index above for the
3965 -- unconstrained array type. This range is needed to apply
3966 -- range checks.
3967
3968 begin
3969 Index := First_Index (Expr_Type);
3970 while Present (Index) loop
3971 if Depends_On_Discriminant (Orig_Index) then
3972 Apply_Range_Check (Index, Etype (Unconstr_Index));
3973 end if;
3974
3975 Next_Index (Index);
3976 Next_Index (Orig_Index);
3977 Next_Index (Unconstr_Index);
3978 end loop;
3979 end;
3980 end if;
3981 end if;
3982
3983 -- If the Parent pointer of Expr is not set, Expr is an expression
3984 -- duplicated by New_Tree_Copy (this happens for record aggregates
3985 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3986 -- Such a duplicated expression must be attached to the tree
3987 -- before analysis and resolution to enforce the rule that a tree
3988 -- fragment should never be analyzed or resolved unless it is
3989 -- attached to the current compilation unit.
3990
3991 if No (Parent (Expr)) then
3992 Set_Parent (Expr, N);
3993 Relocate := False;
3994 else
3995 Relocate := True;
3996 end if;
3997
3998 Analyze_And_Resolve (Expr, Expr_Type);
3999 Check_Expr_OK_In_Limited_Aggregate (Expr);
4000 Check_Non_Static_Context (Expr);
4001 Check_Unset_Reference (Expr);
4002
4003 -- Check wrong use of class-wide types
4004
4005 if Is_Class_Wide_Type (Etype (Expr)) then
4006 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4007 end if;
4008
4009 if not Has_Expansion_Delayed (Expr) then
4010 Aggregate_Constraint_Checks (Expr, Expr_Type);
4011 end if;
4012
4013 -- If an aggregate component has a type with predicates, an explicit
4014 -- predicate check must be applied, as for an assignment statement,
4015 -- because the aggegate might not be expanded into individual
4016 -- component assignments.
4017
4018 if Present (Predicate_Function (Expr_Type))
4019 and then Analyzed (Expr)
4020 then
4021 Apply_Predicate_Check (Expr, Expr_Type);
4022 end if;
4023
4024 if Raises_Constraint_Error (Expr) then
4025 Set_Raises_Constraint_Error (N);
4026 end if;
4027
4028 -- If the expression has been marked as requiring a range check, then
4029 -- generate it here. It's a bit odd to be generating such checks in
4030 -- the analyzer, but harmless since Generate_Range_Check does nothing
4031 -- (other than making sure Do_Range_Check is set) if the expander is
4032 -- not active.
4033
4034 if Do_Range_Check (Expr) then
4035 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4036 end if;
4037
4038 -- Add association Component => Expr if the caller requests it
4039
4040 if Relocate then
4041 New_Expr := Relocate_Node (Expr);
4042
4043 -- Since New_Expr is not gonna be analyzed later on, we need to
4044 -- propagate here the dimensions form Expr to New_Expr.
4045
4046 Copy_Dimensions (Expr, New_Expr);
4047
4048 else
4049 New_Expr := Expr;
4050 end if;
4051
4052 Add_Association (New_C, New_Expr, New_Assoc_List);
4053 end Resolve_Aggr_Expr;
4054
4055 -------------------
4056 -- Rewrite_Range --
4057 -------------------
4058
4059 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4060 procedure Rewrite_Bound
4061 (Bound : Node_Id;
4062 Disc : Entity_Id;
4063 Expr_Disc : Node_Id);
4064 -- Rewrite a bound of the range Bound, when it is equal to the
4065 -- non-stored discriminant Disc, into the stored discriminant
4066 -- value Expr_Disc.
4067
4068 -------------------
4069 -- Rewrite_Bound --
4070 -------------------
4071
4072 procedure Rewrite_Bound
4073 (Bound : Node_Id;
4074 Disc : Entity_Id;
4075 Expr_Disc : Node_Id)
4076 is
4077 begin
4078 if Nkind (Bound) = N_Identifier
4079 and then Entity (Bound) = Disc
4080 then
4081 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4082 end if;
4083 end Rewrite_Bound;
4084
4085 -- Local variables
4086
4087 Low, High : Node_Id;
4088 Disc : Entity_Id;
4089 Expr_Disc : Elmt_Id;
4090
4091 -- Start of processing for Rewrite_Range
4092
4093 begin
4094 if Has_Discriminants (Root_Type)
4095 and then Nkind (Rge) = N_Range
4096 then
4097 Low := Low_Bound (Rge);
4098 High := High_Bound (Rge);
4099
4100 Disc := First_Discriminant (Root_Type);
4101 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4102 while Present (Disc) loop
4103 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4104 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4105 Next_Discriminant (Disc);
4106 Next_Elmt (Expr_Disc);
4107 end loop;
4108 end if;
4109 end Rewrite_Range;
4110
4111 -- Local variables
4112
4113 Components : constant Elist_Id := New_Elmt_List;
4114 -- Components is the list of the record components whose value must be
4115 -- provided in the aggregate. This list does include discriminants.
4116
4117 Component : Entity_Id;
4118 Component_Elmt : Elmt_Id;
4119 Expr : Node_Id;
4120 Positional_Expr : Node_Id;
4121
4122 -- Start of processing for Resolve_Record_Aggregate
4123
4124 begin
4125 -- A record aggregate is restricted in SPARK:
4126
4127 -- Each named association can have only a single choice.
4128 -- OTHERS cannot be used.
4129 -- Positional and named associations cannot be mixed.
4130
4131 if Present (Component_Associations (N))
4132 and then Present (First (Component_Associations (N)))
4133 then
4134 if Present (Expressions (N)) then
4135 Check_SPARK_05_Restriction
4136 ("named association cannot follow positional one",
4137 First (Choices (First (Component_Associations (N)))));
4138 end if;
4139
4140 declare
4141 Assoc : Node_Id;
4142
4143 begin
4144 Assoc := First (Component_Associations (N));
4145 while Present (Assoc) loop
4146 if Nkind (Assoc) = N_Iterated_Component_Association then
4147 Error_Msg_N
4148 ("iterated component association can only appear in an "
4149 & "array aggregate", N);
4150 raise Unrecoverable_Error;
4151
4152 else
4153 if List_Length (Choices (Assoc)) > 1 then
4154 Check_SPARK_05_Restriction
4155 ("component association in record aggregate must "
4156 & "contain a single choice", Assoc);
4157 end if;
4158
4159 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4160 Check_SPARK_05_Restriction
4161 ("record aggregate cannot contain OTHERS", Assoc);
4162 end if;
4163 end if;
4164
4165 Assoc := Next (Assoc);
4166 end loop;
4167 end;
4168 end if;
4169
4170 -- We may end up calling Duplicate_Subexpr on expressions that are
4171 -- attached to New_Assoc_List. For this reason we need to attach it
4172 -- to the tree by setting its parent pointer to N. This parent point
4173 -- will change in STEP 8 below.
4174
4175 Set_Parent (New_Assoc_List, N);
4176
4177 -- STEP 1: abstract type and null record verification
4178
4179 if Is_Abstract_Type (Typ) then
4180 Error_Msg_N ("type of aggregate cannot be abstract", N);
4181 end if;
4182
4183 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4184 Set_Etype (N, Typ);
4185 return;
4186
4187 elsif Present (First_Entity (Typ))
4188 and then Null_Record_Present (N)
4189 and then not Is_Tagged_Type (Typ)
4190 then
4191 Error_Msg_N ("record aggregate cannot be null", N);
4192 return;
4193
4194 -- If the type has no components, then the aggregate should either
4195 -- have "null record", or in Ada 2005 it could instead have a single
4196 -- component association given by "others => <>". For Ada 95 we flag an
4197 -- error at this point, but for Ada 2005 we proceed with checking the
4198 -- associations below, which will catch the case where it's not an
4199 -- aggregate with "others => <>". Note that the legality of a <>
4200 -- aggregate for a null record type was established by AI05-016.
4201
4202 elsif No (First_Entity (Typ))
4203 and then Ada_Version < Ada_2005
4204 then
4205 Error_Msg_N ("record aggregate must be null", N);
4206 return;
4207 end if;
4208
4209 -- STEP 2: Verify aggregate structure
4210
4211 Step_2 : declare
4212 Assoc : Node_Id;
4213 Bad_Aggregate : Boolean := False;
4214 Selector_Name : Node_Id;
4215
4216 begin
4217 if Present (Component_Associations (N)) then
4218 Assoc := First (Component_Associations (N));
4219 else
4220 Assoc := Empty;
4221 end if;
4222
4223 while Present (Assoc) loop
4224 Selector_Name := First (Choices (Assoc));
4225 while Present (Selector_Name) loop
4226 if Nkind (Selector_Name) = N_Identifier then
4227 null;
4228
4229 elsif Nkind (Selector_Name) = N_Others_Choice then
4230 if Selector_Name /= First (Choices (Assoc))
4231 or else Present (Next (Selector_Name))
4232 then
4233 Error_Msg_N
4234 ("OTHERS must appear alone in a choice list",
4235 Selector_Name);
4236 return;
4237
4238 elsif Present (Next (Assoc)) then
4239 Error_Msg_N
4240 ("OTHERS must appear last in an aggregate",
4241 Selector_Name);
4242 return;
4243
4244 -- (Ada 2005): If this is an association with a box,
4245 -- indicate that the association need not represent
4246 -- any component.
4247
4248 elsif Box_Present (Assoc) then
4249 Others_Box := 1;
4250 Box_Node := Assoc;
4251 end if;
4252
4253 else
4254 Error_Msg_N
4255 ("selector name should be identifier or OTHERS",
4256 Selector_Name);
4257 Bad_Aggregate := True;
4258 end if;
4259
4260 Next (Selector_Name);
4261 end loop;
4262
4263 Next (Assoc);
4264 end loop;
4265
4266 if Bad_Aggregate then
4267 return;
4268 end if;
4269 end Step_2;
4270
4271 -- STEP 3: Find discriminant Values
4272
4273 Step_3 : declare
4274 Discrim : Entity_Id;
4275 Missing_Discriminants : Boolean := False;
4276
4277 begin
4278 if Present (Expressions (N)) then
4279 Positional_Expr := First (Expressions (N));
4280 else
4281 Positional_Expr := Empty;
4282 end if;
4283
4284 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4285 -- must not have unknown discriminants.
4286
4287 if Is_Derived_Type (Typ)
4288 and then Has_Unknown_Discriminants (Root_Type (Typ))
4289 and then Nkind (N) /= N_Extension_Aggregate
4290 then
4291 Error_Msg_NE
4292 ("aggregate not available for type& whose ancestor "
4293 & "has unknown discriminants ", N, Typ);
4294 end if;
4295
4296 if Has_Unknown_Discriminants (Typ)
4297 and then Present (Underlying_Record_View (Typ))
4298 then
4299 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4300 elsif Has_Discriminants (Typ) then
4301 Discrim := First_Discriminant (Typ);
4302 else
4303 Discrim := Empty;
4304 end if;
4305
4306 -- First find the discriminant values in the positional components
4307
4308 while Present (Discrim) and then Present (Positional_Expr) loop
4309 if Discriminant_Present (Discrim) then
4310 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4311
4312 -- Ada 2005 (AI-231)
4313
4314 if Ada_Version >= Ada_2005
4315 and then Known_Null (Positional_Expr)
4316 then
4317 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4318 end if;
4319
4320 Next (Positional_Expr);
4321 end if;
4322
4323 if Present (Get_Value (Discrim, Component_Associations (N))) then
4324 Error_Msg_NE
4325 ("more than one value supplied for discriminant&",
4326 N, Discrim);
4327 end if;
4328
4329 Next_Discriminant (Discrim);
4330 end loop;
4331
4332 -- Find remaining discriminant values if any among named components
4333
4334 while Present (Discrim) loop
4335 Expr := Get_Value (Discrim, Component_Associations (N), True);
4336
4337 if not Discriminant_Present (Discrim) then
4338 if Present (Expr) then
4339 Error_Msg_NE
4340 ("more than one value supplied for discriminant &",
4341 N, Discrim);
4342 end if;
4343
4344 elsif No (Expr) then
4345 Error_Msg_NE
4346 ("no value supplied for discriminant &", N, Discrim);
4347 Missing_Discriminants := True;
4348
4349 else
4350 Resolve_Aggr_Expr (Expr, Discrim);
4351 end if;
4352
4353 Next_Discriminant (Discrim);
4354 end loop;
4355
4356 if Missing_Discriminants then
4357 return;
4358 end if;
4359
4360 -- At this point and until the beginning of STEP 6, New_Assoc_List
4361 -- contains only the discriminants and their values.
4362
4363 end Step_3;
4364
4365 -- STEP 4: Set the Etype of the record aggregate
4366
4367 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4368 -- routine should really be exported in sem_util or some such and used
4369 -- in sem_ch3 and here rather than have a copy of the code which is a
4370 -- maintenance nightmare.
4371
4372 -- ??? Performance WARNING. The current implementation creates a new
4373 -- itype for all aggregates whose base type is discriminated. This means
4374 -- that for record aggregates nested inside an array aggregate we will
4375 -- create a new itype for each record aggregate if the array component
4376 -- type has discriminants. For large aggregates this may be a problem.
4377 -- What should be done in this case is to reuse itypes as much as
4378 -- possible.
4379
4380 if Has_Discriminants (Typ)
4381 or else (Has_Unknown_Discriminants (Typ)
4382 and then Present (Underlying_Record_View (Typ)))
4383 then
4384 Build_Constrained_Itype : declare
4385 Constrs : constant List_Id := New_List;
4386 Loc : constant Source_Ptr := Sloc (N);
4387 Def_Id : Entity_Id;
4388 Indic : Node_Id;
4389 New_Assoc : Node_Id;
4390 Subtyp_Decl : Node_Id;
4391
4392 begin
4393 New_Assoc := First (New_Assoc_List);
4394 while Present (New_Assoc) loop
4395 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4396 Next (New_Assoc);
4397 end loop;
4398
4399 if Has_Unknown_Discriminants (Typ)
4400 and then Present (Underlying_Record_View (Typ))
4401 then
4402 Indic :=
4403 Make_Subtype_Indication (Loc,
4404 Subtype_Mark =>
4405 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4406 Constraint =>
4407 Make_Index_Or_Discriminant_Constraint (Loc,
4408 Constraints => Constrs));
4409 else
4410 Indic :=
4411 Make_Subtype_Indication (Loc,
4412 Subtype_Mark =>
4413 New_Occurrence_Of (Base_Type (Typ), Loc),
4414 Constraint =>
4415 Make_Index_Or_Discriminant_Constraint (Loc,
4416 Constraints => Constrs));
4417 end if;
4418
4419 Def_Id := Create_Itype (Ekind (Typ), N);
4420
4421 Subtyp_Decl :=
4422 Make_Subtype_Declaration (Loc,
4423 Defining_Identifier => Def_Id,
4424 Subtype_Indication => Indic);
4425 Set_Parent (Subtyp_Decl, Parent (N));
4426
4427 -- Itypes must be analyzed with checks off (see itypes.ads)
4428
4429 Analyze (Subtyp_Decl, Suppress => All_Checks);
4430
4431 Set_Etype (N, Def_Id);
4432 Check_Static_Discriminated_Subtype
4433 (Def_Id, Expression (First (New_Assoc_List)));
4434 end Build_Constrained_Itype;
4435
4436 else
4437 Set_Etype (N, Typ);
4438 end if;
4439
4440 -- STEP 5: Get remaining components according to discriminant values
4441
4442 Step_5 : declare
4443 Dnode : Node_Id;
4444 Errors_Found : Boolean := False;
4445 Record_Def : Node_Id;
4446 Parent_Typ : Entity_Id;
4447 Parent_Typ_List : Elist_Id;
4448 Parent_Elmt : Elmt_Id;
4449 Root_Typ : Entity_Id;
4450
4451 begin
4452 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4453 Parent_Typ_List := New_Elmt_List;
4454
4455 -- If this is an extension aggregate, the component list must
4456 -- include all components that are not in the given ancestor type.
4457 -- Otherwise, the component list must include components of all
4458 -- ancestors, starting with the root.
4459
4460 if Nkind (N) = N_Extension_Aggregate then
4461 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4462
4463 else
4464 -- AI05-0115: check legality of aggregate for type with a
4465 -- private ancestor.
4466
4467 Root_Typ := Root_Type (Typ);
4468 if Has_Private_Ancestor (Typ) then
4469 declare
4470 Ancestor : constant Entity_Id :=
4471 Find_Private_Ancestor (Typ);
4472 Ancestor_Unit : constant Entity_Id :=
4473 Cunit_Entity
4474 (Get_Source_Unit (Ancestor));
4475 Parent_Unit : constant Entity_Id :=
4476 Cunit_Entity (Get_Source_Unit
4477 (Base_Type (Etype (Ancestor))));
4478 begin
4479 -- Check whether we are in a scope that has full view
4480 -- over the private ancestor and its parent. This can
4481 -- only happen if the derivation takes place in a child
4482 -- unit of the unit that declares the parent, and we are
4483 -- in the private part or body of that child unit, else
4484 -- the aggregate is illegal.
4485
4486 if Is_Child_Unit (Ancestor_Unit)
4487 and then Scope (Ancestor_Unit) = Parent_Unit
4488 and then In_Open_Scopes (Scope (Ancestor))
4489 and then
4490 (In_Private_Part (Scope (Ancestor))
4491 or else In_Package_Body (Scope (Ancestor)))
4492 then
4493 null;
4494
4495 else
4496 Error_Msg_NE
4497 ("type of aggregate has private ancestor&!",
4498 N, Root_Typ);
4499 Error_Msg_N ("must use extension aggregate!", N);
4500 return;
4501 end if;
4502 end;
4503 end if;
4504
4505 Dnode := Declaration_Node (Base_Type (Root_Typ));
4506
4507 -- If we don't get a full declaration, then we have some error
4508 -- which will get signalled later so skip this part. Otherwise
4509 -- gather components of root that apply to the aggregate type.
4510 -- We use the base type in case there is an applicable stored
4511 -- constraint that renames the discriminants of the root.
4512
4513 if Nkind (Dnode) = N_Full_Type_Declaration then
4514 Record_Def := Type_Definition (Dnode);
4515 Gather_Components
4516 (Base_Type (Typ),
4517 Component_List (Record_Def),
4518 Governed_By => New_Assoc_List,
4519 Into => Components,
4520 Report_Errors => Errors_Found);
4521
4522 if Errors_Found then
4523 Error_Msg_N
4524 ("discriminant controlling variant part is not static",
4525 N);
4526 return;
4527 end if;
4528 end if;
4529 end if;
4530
4531 Parent_Typ := Base_Type (Typ);
4532 while Parent_Typ /= Root_Typ loop
4533 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4534 Parent_Typ := Etype (Parent_Typ);
4535
4536 if Nkind (Parent (Base_Type (Parent_Typ))) =
4537 N_Private_Type_Declaration
4538 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4539 N_Private_Extension_Declaration
4540 then
4541 if Nkind (N) /= N_Extension_Aggregate then
4542 Error_Msg_NE
4543 ("type of aggregate has private ancestor&!",
4544 N, Parent_Typ);
4545 Error_Msg_N ("must use extension aggregate!", N);
4546 return;
4547
4548 elsif Parent_Typ /= Root_Typ then
4549 Error_Msg_NE
4550 ("ancestor part of aggregate must be private type&",
4551 Ancestor_Part (N), Parent_Typ);
4552 return;
4553 end if;
4554
4555 -- The current view of ancestor part may be a private type,
4556 -- while the context type is always non-private.
4557
4558 elsif Is_Private_Type (Root_Typ)
4559 and then Present (Full_View (Root_Typ))
4560 and then Nkind (N) = N_Extension_Aggregate
4561 then
4562 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4563 end if;
4564 end loop;
4565
4566 -- Now collect components from all other ancestors, beginning
4567 -- with the current type. If the type has unknown discriminants
4568 -- use the component list of the Underlying_Record_View, which
4569 -- needs to be used for the subsequent expansion of the aggregate
4570 -- into assignments.
4571
4572 Parent_Elmt := First_Elmt (Parent_Typ_List);
4573 while Present (Parent_Elmt) loop
4574 Parent_Typ := Node (Parent_Elmt);
4575
4576 if Has_Unknown_Discriminants (Parent_Typ)
4577 and then Present (Underlying_Record_View (Typ))
4578 then
4579 Parent_Typ := Underlying_Record_View (Parent_Typ);
4580 end if;
4581
4582 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4583 Gather_Components (Empty,
4584 Component_List (Record_Extension_Part (Record_Def)),
4585 Governed_By => New_Assoc_List,
4586 Into => Components,
4587 Report_Errors => Errors_Found);
4588
4589 Next_Elmt (Parent_Elmt);
4590 end loop;
4591
4592 -- Typ is not a derived tagged type
4593
4594 else
4595 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4596
4597 if Null_Present (Record_Def) then
4598 null;
4599
4600 elsif not Has_Unknown_Discriminants (Typ) then
4601 Gather_Components
4602 (Base_Type (Typ),
4603 Component_List (Record_Def),
4604 Governed_By => New_Assoc_List,
4605 Into => Components,
4606 Report_Errors => Errors_Found);
4607
4608 else
4609 Gather_Components
4610 (Base_Type (Underlying_Record_View (Typ)),
4611 Component_List (Record_Def),
4612 Governed_By => New_Assoc_List,
4613 Into => Components,
4614 Report_Errors => Errors_Found);
4615 end if;
4616 end if;
4617
4618 if Errors_Found then
4619 return;
4620 end if;
4621 end Step_5;
4622
4623 -- STEP 6: Find component Values
4624
4625 Component := Empty;
4626 Component_Elmt := First_Elmt (Components);
4627
4628 -- First scan the remaining positional associations in the aggregate.
4629 -- Remember that at this point Positional_Expr contains the current
4630 -- positional association if any is left after looking for discriminant
4631 -- values in step 3.
4632
4633 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4634 Component := Node (Component_Elmt);
4635 Resolve_Aggr_Expr (Positional_Expr, Component);
4636
4637 -- Ada 2005 (AI-231)
4638
4639 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4640 Check_Can_Never_Be_Null (Component, Positional_Expr);
4641 end if;
4642
4643 if Present (Get_Value (Component, Component_Associations (N))) then
4644 Error_Msg_NE
4645 ("more than one value supplied for Component &", N, Component);
4646 end if;
4647
4648 Next (Positional_Expr);
4649 Next_Elmt (Component_Elmt);
4650 end loop;
4651
4652 if Present (Positional_Expr) then
4653 Error_Msg_N
4654 ("too many components for record aggregate", Positional_Expr);
4655 end if;
4656
4657 -- Now scan for the named arguments of the aggregate
4658
4659 while Present (Component_Elmt) loop
4660 Component := Node (Component_Elmt);
4661 Expr := Get_Value (Component, Component_Associations (N), True);
4662
4663 -- Note: The previous call to Get_Value sets the value of the
4664 -- variable Is_Box_Present.
4665
4666 -- Ada 2005 (AI-287): Handle components with default initialization.
4667 -- Note: This feature was originally added to Ada 2005 for limited
4668 -- but it was finally allowed with any type.
4669
4670 if Is_Box_Present then
4671 Check_Box_Component : declare
4672 Ctyp : constant Entity_Id := Etype (Component);
4673
4674 begin
4675 -- If there is a default expression for the aggregate, copy
4676 -- it into a new association. This copy must modify the scopes
4677 -- of internal types that may be attached to the expression
4678 -- (e.g. index subtypes of arrays) because in general the type
4679 -- declaration and the aggregate appear in different scopes,
4680 -- and the backend requires the scope of the type to match the
4681 -- point at which it is elaborated.
4682
4683 -- If the component has an initialization procedure (IP) we
4684 -- pass the component to the expander, which will generate
4685 -- the call to such IP.
4686
4687 -- If the component has discriminants, their values must
4688 -- be taken from their subtype. This is indispensable for
4689 -- constraints that are given by the current instance of an
4690 -- enclosing type, to allow the expansion of the aggregate to
4691 -- replace the reference to the current instance by the target
4692 -- object of the aggregate.
4693
4694 if Present (Parent (Component))
4695 and then Nkind (Parent (Component)) = N_Component_Declaration
4696 and then Present (Expression (Parent (Component)))
4697 then
4698 Expr :=
4699 New_Copy_Tree_And_Copy_Dimensions
4700 (Expression (Parent (Component)),
4701 New_Scope => Current_Scope,
4702 New_Sloc => Sloc (N));
4703
4704 -- As the type of the copied default expression may refer
4705 -- to discriminants of the record type declaration, these
4706 -- non-stored discriminants need to be rewritten into stored
4707 -- discriminant values for the aggregate. This is required
4708 -- in GNATprove mode, and is adopted in all modes to avoid
4709 -- special-casing GNATprove mode.
4710
4711 if Is_Array_Type (Etype (Expr)) then
4712 declare
4713 Rec_Typ : constant Entity_Id := Scope (Component);
4714 -- Root record type whose discriminants may be used as
4715 -- bounds in range nodes.
4716
4717 Index : Node_Id;
4718
4719 begin
4720 -- Rewrite the range nodes occurring in the indexes
4721 -- and their types.
4722
4723 Index := First_Index (Etype (Expr));
4724 while Present (Index) loop
4725 Rewrite_Range (Rec_Typ, Index);
4726 Rewrite_Range
4727 (Rec_Typ, Scalar_Range (Etype (Index)));
4728
4729 Next_Index (Index);
4730 end loop;
4731
4732 -- Rewrite the range nodes occurring as aggregate
4733 -- bounds.
4734
4735 if Nkind (Expr) = N_Aggregate
4736 and then Present (Aggregate_Bounds (Expr))
4737 then
4738 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4739 end if;
4740 end;
4741 end if;
4742
4743 Add_Association
4744 (Component => Component,
4745 Expr => Expr,
4746 Assoc_List => New_Assoc_List);
4747 Set_Has_Self_Reference (N);
4748
4749 -- A box-defaulted access component gets the value null. Also
4750 -- included are components of private types whose underlying
4751 -- type is an access type. In either case set the type of the
4752 -- literal, for subsequent use in semantic checks.
4753
4754 elsif Present (Underlying_Type (Ctyp))
4755 and then Is_Access_Type (Underlying_Type (Ctyp))
4756 then
4757 -- If the component's type is private with an access type as
4758 -- its underlying type then we have to create an unchecked
4759 -- conversion to satisfy type checking.
4760
4761 if Is_Private_Type (Ctyp) then
4762 declare
4763 Qual_Null : constant Node_Id :=
4764 Make_Qualified_Expression (Sloc (N),
4765 Subtype_Mark =>
4766 New_Occurrence_Of
4767 (Underlying_Type (Ctyp), Sloc (N)),
4768 Expression => Make_Null (Sloc (N)));
4769
4770 Convert_Null : constant Node_Id :=
4771 Unchecked_Convert_To
4772 (Ctyp, Qual_Null);
4773
4774 begin
4775 Analyze_And_Resolve (Convert_Null, Ctyp);
4776 Add_Association
4777 (Component => Component,
4778 Expr => Convert_Null,
4779 Assoc_List => New_Assoc_List);
4780 end;
4781
4782 -- Otherwise the component type is non-private
4783
4784 else
4785 Expr := Make_Null (Sloc (N));
4786 Set_Etype (Expr, Ctyp);
4787
4788 Add_Association
4789 (Component => Component,
4790 Expr => Expr,
4791 Assoc_List => New_Assoc_List);
4792 end if;
4793
4794 -- Ada 2012: If component is scalar with default value, use it
4795
4796 elsif Is_Scalar_Type (Ctyp)
4797 and then Has_Default_Aspect (Ctyp)
4798 then
4799 Add_Association
4800 (Component => Component,
4801 Expr =>
4802 Default_Aspect_Value
4803 (First_Subtype (Underlying_Type (Ctyp))),
4804 Assoc_List => New_Assoc_List);
4805
4806 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4807 or else not Expander_Active
4808 then
4809 if Is_Record_Type (Ctyp)
4810 and then Has_Discriminants (Ctyp)
4811 and then not Is_Private_Type (Ctyp)
4812 then
4813 -- We build a partially initialized aggregate with the
4814 -- values of the discriminants and box initialization
4815 -- for the rest, if other components are present.
4816
4817 -- The type of the aggregate is the known subtype of
4818 -- the component. The capture of discriminants must be
4819 -- recursive because subcomponents may be constrained
4820 -- (transitively) by discriminants of enclosing types.
4821 -- For a private type with discriminants, a call to the
4822 -- initialization procedure will be generated, and no
4823 -- subaggregate is needed.
4824
4825 Capture_Discriminants : declare
4826 Loc : constant Source_Ptr := Sloc (N);
4827 Expr : Node_Id;
4828
4829 begin
4830 Expr := Make_Aggregate (Loc, New_List, New_List);
4831 Set_Etype (Expr, Ctyp);
4832
4833 -- If the enclosing type has discriminants, they have
4834 -- been collected in the aggregate earlier, and they
4835 -- may appear as constraints of subcomponents.
4836
4837 -- Similarly if this component has discriminants, they
4838 -- might in turn be propagated to their components.
4839
4840 if Has_Discriminants (Typ) then
4841 Add_Discriminant_Values (Expr, New_Assoc_List);
4842 Propagate_Discriminants (Expr, New_Assoc_List);
4843
4844 elsif Has_Discriminants (Ctyp) then
4845 Add_Discriminant_Values
4846 (Expr, Component_Associations (Expr));
4847 Propagate_Discriminants
4848 (Expr, Component_Associations (Expr));
4849
4850 else
4851 declare
4852 Comp : Entity_Id;
4853
4854 begin
4855 -- If the type has additional components, create
4856 -- an OTHERS box association for them.
4857
4858 Comp := First_Component (Ctyp);
4859 while Present (Comp) loop
4860 if Ekind (Comp) = E_Component then
4861 if not Is_Record_Type (Etype (Comp)) then
4862 Append_To
4863 (Component_Associations (Expr),
4864 Make_Component_Association (Loc,
4865 Choices =>
4866 New_List (
4867 Make_Others_Choice (Loc)),
4868 Expression => Empty,
4869 Box_Present => True));
4870 end if;
4871
4872 exit;
4873 end if;
4874
4875 Next_Component (Comp);
4876 end loop;
4877 end;
4878 end if;
4879
4880 Add_Association
4881 (Component => Component,
4882 Expr => Expr,
4883 Assoc_List => New_Assoc_List);
4884 end Capture_Discriminants;
4885
4886 -- Otherwise the component type is not a record, or it has
4887 -- not discriminants, or it is private.
4888
4889 else
4890 Add_Association
4891 (Component => Component,
4892 Expr => Empty,
4893 Assoc_List => New_Assoc_List,
4894 Is_Box_Present => True);
4895 end if;
4896
4897 -- Otherwise we only need to resolve the expression if the
4898 -- component has partially initialized values (required to
4899 -- expand the corresponding assignments and run-time checks).
4900
4901 elsif Present (Expr)
4902 and then Is_Partially_Initialized_Type (Ctyp)
4903 then
4904 Resolve_Aggr_Expr (Expr, Component);
4905 end if;
4906 end Check_Box_Component;
4907
4908 elsif No (Expr) then
4909
4910 -- Ignore hidden components associated with the position of the
4911 -- interface tags: these are initialized dynamically.
4912
4913 if not Present (Related_Type (Component)) then
4914 Error_Msg_NE
4915 ("no value supplied for component &!", N, Component);
4916 end if;
4917
4918 else
4919 Resolve_Aggr_Expr (Expr, Component);
4920 end if;
4921
4922 Next_Elmt (Component_Elmt);
4923 end loop;
4924
4925 -- STEP 7: check for invalid components + check type in choice list
4926
4927 Step_7 : declare
4928 Assoc : Node_Id;
4929 New_Assoc : Node_Id;
4930
4931 Selectr : Node_Id;
4932 -- Selector name
4933
4934 Typech : Entity_Id;
4935 -- Type of first component in choice list
4936
4937 begin
4938 if Present (Component_Associations (N)) then
4939 Assoc := First (Component_Associations (N));
4940 else
4941 Assoc := Empty;
4942 end if;
4943
4944 Verification : while Present (Assoc) loop
4945 Selectr := First (Choices (Assoc));
4946 Typech := Empty;
4947
4948 if Nkind (Selectr) = N_Others_Choice then
4949
4950 -- Ada 2005 (AI-287): others choice may have expression or box
4951
4952 if No (Others_Etype) and then Others_Box = 0 then
4953 Error_Msg_N
4954 ("OTHERS must represent at least one component", Selectr);
4955
4956 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
4957 Error_Msg_N ("others choice is redundant?", Box_Node);
4958 Error_Msg_N
4959 ("\previous choices cover all components?", Box_Node);
4960 end if;
4961
4962 exit Verification;
4963 end if;
4964
4965 while Present (Selectr) loop
4966 New_Assoc := First (New_Assoc_List);
4967 while Present (New_Assoc) loop
4968 Component := First (Choices (New_Assoc));
4969
4970 if Chars (Selectr) = Chars (Component) then
4971 if Style_Check then
4972 Check_Identifier (Selectr, Entity (Component));
4973 end if;
4974
4975 exit;
4976 end if;
4977
4978 Next (New_Assoc);
4979 end loop;
4980
4981 -- If no association, this is not a legal component of the type
4982 -- in question, unless its association is provided with a box.
4983
4984 if No (New_Assoc) then
4985 if Box_Present (Parent (Selectr)) then
4986
4987 -- This may still be a bogus component with a box. Scan
4988 -- list of components to verify that a component with
4989 -- that name exists.
4990
4991 declare
4992 C : Entity_Id;
4993
4994 begin
4995 C := First_Component (Typ);
4996 while Present (C) loop
4997 if Chars (C) = Chars (Selectr) then
4998
4999 -- If the context is an extension aggregate,
5000 -- the component must not be inherited from
5001 -- the ancestor part of the aggregate.
5002
5003 if Nkind (N) /= N_Extension_Aggregate
5004 or else
5005 Scope (Original_Record_Component (C)) /=
5006 Etype (Ancestor_Part (N))
5007 then
5008 exit;
5009 end if;
5010 end if;
5011
5012 Next_Component (C);
5013 end loop;
5014
5015 if No (C) then
5016 Error_Msg_Node_2 := Typ;
5017 Error_Msg_N ("& is not a component of}", Selectr);
5018 end if;
5019 end;
5020
5021 elsif Chars (Selectr) /= Name_uTag
5022 and then Chars (Selectr) /= Name_uParent
5023 then
5024 if not Has_Discriminants (Typ) then
5025 Error_Msg_Node_2 := Typ;
5026 Error_Msg_N ("& is not a component of}", Selectr);
5027 else
5028 Error_Msg_N
5029 ("& is not a component of the aggregate subtype",
5030 Selectr);
5031 end if;
5032
5033 Check_Misspelled_Component (Components, Selectr);
5034 end if;
5035
5036 elsif No (Typech) then
5037 Typech := Base_Type (Etype (Component));
5038
5039 -- AI05-0199: In Ada 2012, several components of anonymous
5040 -- access types can appear in a choice list, as long as the
5041 -- designated types match.
5042
5043 elsif Typech /= Base_Type (Etype (Component)) then
5044 if Ada_Version >= Ada_2012
5045 and then Ekind (Typech) = E_Anonymous_Access_Type
5046 and then
5047 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5048 and then Base_Type (Designated_Type (Typech)) =
5049 Base_Type (Designated_Type (Etype (Component)))
5050 and then
5051 Subtypes_Statically_Match (Typech, (Etype (Component)))
5052 then
5053 null;
5054
5055 elsif not Box_Present (Parent (Selectr)) then
5056 Error_Msg_N
5057 ("components in choice list must have same type",
5058 Selectr);
5059 end if;
5060 end if;
5061
5062 Next (Selectr);
5063 end loop;
5064
5065 Next (Assoc);
5066 end loop Verification;
5067 end Step_7;
5068
5069 -- STEP 8: replace the original aggregate
5070
5071 Step_8 : declare
5072 New_Aggregate : constant Node_Id := New_Copy (N);
5073
5074 begin
5075 Set_Expressions (New_Aggregate, No_List);
5076 Set_Etype (New_Aggregate, Etype (N));
5077 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5078 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5079
5080 Rewrite (N, New_Aggregate);
5081 end Step_8;
5082
5083 -- Check the dimensions of the components in the record aggregate
5084
5085 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5086 end Resolve_Record_Aggregate;
5087
5088 -----------------------------
5089 -- Check_Can_Never_Be_Null --
5090 -----------------------------
5091
5092 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5093 Comp_Typ : Entity_Id;
5094
5095 begin
5096 pragma Assert
5097 (Ada_Version >= Ada_2005
5098 and then Present (Expr)
5099 and then Known_Null (Expr));
5100
5101 case Ekind (Typ) is
5102 when E_Array_Type =>
5103 Comp_Typ := Component_Type (Typ);
5104
5105 when E_Component
5106 | E_Discriminant
5107 =>
5108 Comp_Typ := Etype (Typ);
5109
5110 when others =>
5111 return;
5112 end case;
5113
5114 if Can_Never_Be_Null (Comp_Typ) then
5115
5116 -- Here we know we have a constraint error. Note that we do not use
5117 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5118 -- seem the more natural approach. That's because in some cases the
5119 -- components are rewritten, and the replacement would be missed.
5120 -- We do not mark the whole aggregate as raising a constraint error,
5121 -- because the association may be a null array range.
5122
5123 Error_Msg_N
5124 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5125 Error_Msg_N
5126 ("\Constraint_Error will be raised at run time??", Expr);
5127
5128 Rewrite (Expr,
5129 Make_Raise_Constraint_Error
5130 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5131 Set_Etype (Expr, Comp_Typ);
5132 Set_Analyzed (Expr);
5133 end if;
5134 end Check_Can_Never_Be_Null;
5135
5136 ---------------------
5137 -- Sort_Case_Table --
5138 ---------------------
5139
5140 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5141 U : constant Int := Case_Table'Last;
5142 K : Int;
5143 J : Int;
5144 T : Case_Bounds;
5145
5146 begin
5147 K := 1;
5148 while K < U loop
5149 T := Case_Table (K + 1);
5150
5151 J := K + 1;
5152 while J > 1
5153 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5154 loop
5155 Case_Table (J) := Case_Table (J - 1);
5156 J := J - 1;
5157 end loop;
5158
5159 Case_Table (J) := T;
5160 K := K + 1;
5161 end loop;
5162 end Sort_Case_Table;
5163
5164 end Sem_Aggr;