]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_aggr.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_aggr.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
26
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 Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Sem; use Sem;
42 with Sem_Cat; use Sem_Cat;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sem_Warn; use Sem_Warn;
50 with Sinfo; use Sinfo;
51 with Snames; use Snames;
52 with Stringt; use Stringt;
53 with Stand; use Stand;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
57
58 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
59
60 package body Sem_Aggr is
61
62 type Case_Bounds is record
63 Choice_Lo : Node_Id;
64 Choice_Hi : Node_Id;
65 Choice_Node : Node_Id;
66 end record;
67
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
70
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
74
75 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
76 -- Sort the Case Table using the Lower Bound of each Choice as the key.
77 -- A simple insertion sort is used since the number of choices in a case
78 -- statement of variant part will usually be small and probably in near
79 -- sorted order.
80
81 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id);
82 -- Ada 0Y (AI-231): Check bad usage of the null-exclusion issue
83
84 ------------------------------------------------------
85 -- Subprograms used for RECORD AGGREGATE Processing --
86 ------------------------------------------------------
87
88 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
89 -- This procedure performs all the semantic checks required for record
90 -- aggregates. Note that for aggregates analysis and resolution go
91 -- hand in hand. Aggregate analysis has been delayed up to here and
92 -- it is done while resolving the aggregate.
93 --
94 -- N is the N_Aggregate node.
95 -- Typ is the record type for the aggregate resolution
96 --
97 -- While performing the semantic checks, this procedure
98 -- builds a new Component_Association_List where each record field
99 -- appears alone in a Component_Choice_List along with its corresponding
100 -- expression. The record fields in the Component_Association_List
101 -- appear in the same order in which they appear in the record type Typ.
102 --
103 -- Once this new Component_Association_List is built and all the
104 -- semantic checks performed, the original aggregate subtree is replaced
105 -- with the new named record aggregate just built. Note that the subtree
106 -- substitution is performed with Rewrite so as to be
107 -- able to retrieve the original aggregate.
108 --
109 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
110 -- yields the aggregate format expected by Gigi. Typically, this kind of
111 -- tree manipulations are done in the expander. However, because the
112 -- semantic checks that need to be performed on record aggregates really
113 -- go hand in hand with the record aggregate normalization, the aggregate
114 -- subtree transformation is performed during resolution rather than
115 -- expansion. Had we decided otherwise we would have had to duplicate
116 -- most of the code in the expansion procedure Expand_Record_Aggregate.
117 -- Note, however, that all the expansion concerning aggegates for tagged
118 -- records is done in Expand_Record_Aggregate.
119 --
120 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
121 --
122 -- 1. Make sure that the record type against which the record aggregate
123 -- has to be resolved is not abstract. Furthermore if the type is
124 -- a null aggregate make sure the input aggregate N is also null.
125 --
126 -- 2. Verify that the structure of the aggregate is that of a record
127 -- aggregate. Specifically, look for component associations and ensure
128 -- that each choice list only has identifiers or the N_Others_Choice
129 -- node. Also make sure that if present, the N_Others_Choice occurs
130 -- last and by itself.
131 --
132 -- 3. If Typ contains discriminants, the values for each discriminant
133 -- is looked for. If the record type Typ has variants, we check
134 -- that the expressions corresponding to each discriminant ruling
135 -- the (possibly nested) variant parts of Typ, are static. This
136 -- allows us to determine the variant parts to which the rest of
137 -- the aggregate must conform. The names of discriminants with their
138 -- values are saved in a new association list, New_Assoc_List which
139 -- is later augmented with the names and values of the remaining
140 -- components in the record type.
141 --
142 -- During this phase we also make sure that every discriminant is
143 -- assigned exactly one value. Note that when several values
144 -- for a given discriminant are found, semantic processing continues
145 -- looking for further errors. In this case it's the first
146 -- discriminant value found which we will be recorded.
147 --
148 -- IMPORTANT NOTE: For derived tagged types this procedure expects
149 -- First_Discriminant and Next_Discriminant to give the correct list
150 -- of discriminants, in the correct order.
151 --
152 -- 4. After all the discriminant values have been gathered, we can
153 -- set the Etype of the record aggregate. If Typ contains no
154 -- discriminants this is straightforward: the Etype of N is just
155 -- Typ, otherwise a new implicit constrained subtype of Typ is
156 -- built to be the Etype of N.
157 --
158 -- 5. Gather the remaining record components according to the discriminant
159 -- values. This involves recursively traversing the record type
160 -- structure to see what variants are selected by the given discriminant
161 -- values. This processing is a little more convoluted if Typ is a
162 -- derived tagged types since we need to retrieve the record structure
163 -- of all the ancestors of Typ.
164 --
165 -- 6. After gathering the record components we look for their values
166 -- in the record aggregate and emit appropriate error messages
167 -- should we not find such values or should they be duplicated.
168 --
169 -- 7. We then make sure no illegal component names appear in the
170 -- record aggegate and make sure that the type of the record
171 -- components appearing in a same choice list is the same.
172 -- Finally we ensure that the others choice, if present, is
173 -- used to provide the value of at least a record component.
174 --
175 -- 8. The original aggregate node is replaced with the new named
176 -- aggregate built in steps 3 through 6, as explained earlier.
177 --
178 -- Given the complexity of record aggregate resolution, the primary
179 -- goal of this routine is clarity and simplicity rather than execution
180 -- and storage efficiency. If there are only positional components in the
181 -- aggregate the running time is linear. If there are associations
182 -- the running time is still linear as long as the order of the
183 -- associations is not too far off the order of the components in the
184 -- record type. If this is not the case the running time is at worst
185 -- quadratic in the size of the association list.
186
187 procedure Check_Misspelled_Component
188 (Elements : Elist_Id;
189 Component : Node_Id);
190 -- Give possible misspelling diagnostic if Component is likely to be
191 -- a misspelling of one of the components of the Assoc_List.
192 -- This is called by Resolv_Aggr_Expr after producing
193 -- an invalid component error message.
194
195 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
196 -- An optimization: determine whether a discriminated subtype has a
197 -- static constraint, and contains array components whose length is also
198 -- static, either because they are constrained by the discriminant, or
199 -- because the original component bounds are static.
200
201 -----------------------------------------------------
202 -- Subprograms used for ARRAY AGGREGATE Processing --
203 -----------------------------------------------------
204
205 function Resolve_Array_Aggregate
206 (N : Node_Id;
207 Index : Node_Id;
208 Index_Constr : Node_Id;
209 Component_Typ : Entity_Id;
210 Others_Allowed : Boolean)
211 return Boolean;
212 -- This procedure performs the semantic checks for an array aggregate.
213 -- True is returned if the aggregate resolution succeeds.
214 -- The procedure works by recursively checking each nested aggregate.
215 -- Specifically, after checking a sub-aggreate nested at the i-th level
216 -- we recursively check all the subaggregates at the i+1-st level (if any).
217 -- Note that for aggregates analysis and resolution go hand in hand.
218 -- Aggregate analysis has been delayed up to here and it is done while
219 -- resolving the aggregate.
220 --
221 -- N is the current N_Aggregate node to be checked.
222 --
223 -- Index is the index node corresponding to the array sub-aggregate that
224 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
225 -- corresponding index type (or subtype).
226 --
227 -- Index_Constr is the node giving the applicable index constraint if
228 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
229 -- contexts [...] that can be used to determine the bounds of the array
230 -- value specified by the aggregate". If Others_Allowed below is False
231 -- there is no applicable index constraint and this node is set to Index.
232 --
233 -- Component_Typ is the array component type.
234 --
235 -- Others_Allowed indicates whether an others choice is allowed
236 -- in the context where the top-level aggregate appeared.
237 --
238 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
239 --
240 -- 1. Make sure that the others choice, if present, is by itself and
241 -- appears last in the sub-aggregate. Check that we do not have
242 -- positional and named components in the array sub-aggregate (unless
243 -- the named association is an others choice). Finally if an others
244 -- choice is present, make sure it is allowed in the aggregate contex.
245 --
246 -- 2. If the array sub-aggregate contains discrete_choices:
247 --
248 -- (A) Verify their validity. Specifically verify that:
249 --
250 -- (a) If a null range is present it must be the only possible
251 -- choice in the array aggregate.
252 --
253 -- (b) Ditto for a non static range.
254 --
255 -- (c) Ditto for a non static expression.
256 --
257 -- In addition this step analyzes and resolves each discrete_choice,
258 -- making sure that its type is the type of the corresponding Index.
259 -- If we are not at the lowest array aggregate level (in the case of
260 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
261 -- recursively on each component expression. Otherwise, resolve the
262 -- bottom level component expressions against the expected component
263 -- type ONLY IF the component corresponds to a single discrete choice
264 -- which is not an others choice (to see why read the DELAYED
265 -- COMPONENT RESOLUTION below).
266 --
267 -- (B) Determine the bounds of the sub-aggregate and lowest and
268 -- highest choice values.
269 --
270 -- 3. For positional aggregates:
271 --
272 -- (A) Loop over the component expressions either recursively invoking
273 -- Resolve_Array_Aggregate on each of these for multi-dimensional
274 -- array aggregates or resolving the bottom level component
275 -- expressions against the expected component type.
276 --
277 -- (B) Determine the bounds of the positional sub-aggregates.
278 --
279 -- 4. Try to determine statically whether the evaluation of the array
280 -- sub-aggregate raises Constraint_Error. If yes emit proper
281 -- warnings. The precise checks are the following:
282 --
283 -- (A) Check that the index range defined by aggregate bounds is
284 -- compatible with corresponding index subtype.
285 -- We also check against the base type. In fact it could be that
286 -- Low/High bounds of the base type are static whereas those of
287 -- the index subtype are not. Thus if we can statically catch
288 -- a problem with respect to the base type we are guaranteed
289 -- that the same problem will arise with the index subtype
290 --
291 -- (B) If we are dealing with a named aggregate containing an others
292 -- choice and at least one discrete choice then make sure the range
293 -- specified by the discrete choices does not overflow the
294 -- aggregate bounds. We also check against the index type and base
295 -- type bounds for the same reasons given in (A).
296 --
297 -- (C) If we are dealing with a positional aggregate with an others
298 -- choice make sure the number of positional elements specified
299 -- does not overflow the aggregate bounds. We also check against
300 -- the index type and base type bounds as mentioned in (A).
301 --
302 -- Finally construct an N_Range node giving the sub-aggregate bounds.
303 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
304 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
305 -- to build the appropriate aggregate subtype. Aggregate_Bounds
306 -- information is needed during expansion.
307 --
308 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
309 -- expressions in an array aggregate may call Duplicate_Subexpr or some
310 -- other routine that inserts code just outside the outermost aggregate.
311 -- If the array aggregate contains discrete choices or an others choice,
312 -- this may be wrong. Consider for instance the following example.
313 --
314 -- type Rec is record
315 -- V : Integer := 0;
316 -- end record;
317 --
318 -- type Acc_Rec is access Rec;
319 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
320 --
321 -- Then the transformation of "new Rec" that occurs during resolution
322 -- entails the following code modifications
323 --
324 -- P7b : constant Acc_Rec := new Rec;
325 -- RecIP (P7b.all);
326 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
327 --
328 -- This code transformation is clearly wrong, since we need to call
329 -- "new Rec" for each of the 3 array elements. To avoid this problem we
330 -- delay resolution of the components of non positional array aggregates
331 -- to the expansion phase. As an optimization, if the discrete choice
332 -- specifies a single value we do not delay resolution.
333
334 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
335 -- This routine returns the type or subtype of an array aggregate.
336 --
337 -- N is the array aggregate node whose type we return.
338 --
339 -- Typ is the context type in which N occurs.
340 --
341 -- This routine creates an implicit array subtype whose bounds are
342 -- those defined by the aggregate. When this routine is invoked
343 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
344 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
345 -- sub-aggregate bounds. When building the aggegate itype, this function
346 -- traverses the array aggregate N collecting such Aggregate_Bounds and
347 -- constructs the proper array aggregate itype.
348 --
349 -- Note that in the case of multidimensional aggregates each inner
350 -- sub-aggregate corresponding to a given array dimension, may provide a
351 -- different bounds. If it is possible to determine statically that
352 -- some sub-aggregates corresponding to the same index do not have the
353 -- same bounds, then a warning is emitted. If such check is not possible
354 -- statically (because some sub-aggregate bounds are dynamic expressions)
355 -- then this job is left to the expander. In all cases the particular
356 -- bounds that this function will chose for a given dimension is the first
357 -- N_Range node for a sub-aggregate corresponding to that dimension.
358 --
359 -- Note that the Raises_Constraint_Error flag of an array aggregate
360 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
361 -- is set in Resolve_Array_Aggregate but the aggregate is not
362 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
363 -- first construct the proper itype for the aggregate (Gigi needs
364 -- this). After constructing the proper itype we will eventually replace
365 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
366 -- Of course in cases such as:
367 --
368 -- type Arr is array (integer range <>) of Integer;
369 -- A : Arr := (positive range -1 .. 2 => 0);
370 --
371 -- The bounds of the aggregate itype are cooked up to look reasonable
372 -- (in this particular case the bounds will be 1 .. 2).
373
374 procedure Aggregate_Constraint_Checks
375 (Exp : Node_Id;
376 Check_Typ : Entity_Id);
377 -- Checks expression Exp against subtype Check_Typ. If Exp is an
378 -- aggregate and Check_Typ a constrained record type with discriminants,
379 -- we generate the appropriate discriminant checks. If Exp is an array
380 -- aggregate then emit the appropriate length checks. If Exp is a scalar
381 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
382 -- ensure that range checks are performed at run time.
383
384 procedure Make_String_Into_Aggregate (N : Node_Id);
385 -- A string literal can appear in a context in which a one dimensional
386 -- array of characters is expected. This procedure simply rewrites the
387 -- string as an aggregate, prior to resolution.
388
389 ---------------------------------
390 -- Aggregate_Constraint_Checks --
391 ---------------------------------
392
393 procedure Aggregate_Constraint_Checks
394 (Exp : Node_Id;
395 Check_Typ : Entity_Id)
396 is
397 Exp_Typ : constant Entity_Id := Etype (Exp);
398
399 begin
400 if Raises_Constraint_Error (Exp) then
401 return;
402 end if;
403
404 -- This is really expansion activity, so make sure that expansion
405 -- is on and is allowed.
406
407 if not Expander_Active or else In_Default_Expression then
408 return;
409 end if;
410
411 -- First check if we have to insert discriminant checks
412
413 if Has_Discriminants (Exp_Typ) then
414 Apply_Discriminant_Check (Exp, Check_Typ);
415
416 -- Next emit length checks for array aggregates
417
418 elsif Is_Array_Type (Exp_Typ) then
419 Apply_Length_Check (Exp, Check_Typ);
420
421 -- Finally emit scalar and string checks. If we are dealing with a
422 -- scalar literal we need to check by hand because the Etype of
423 -- literals is not necessarily correct.
424
425 elsif Is_Scalar_Type (Exp_Typ)
426 and then Compile_Time_Known_Value (Exp)
427 then
428 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
429 Apply_Compile_Time_Constraint_Error
430 (Exp, "value not in range of}?", CE_Range_Check_Failed,
431 Ent => Base_Type (Check_Typ),
432 Typ => Base_Type (Check_Typ));
433
434 elsif Is_Out_Of_Range (Exp, Check_Typ) then
435 Apply_Compile_Time_Constraint_Error
436 (Exp, "value not in range of}?", CE_Range_Check_Failed,
437 Ent => Check_Typ,
438 Typ => Check_Typ);
439
440 elsif not Range_Checks_Suppressed (Check_Typ) then
441 Apply_Scalar_Range_Check (Exp, Check_Typ);
442 end if;
443
444 elsif (Is_Scalar_Type (Exp_Typ)
445 or else Nkind (Exp) = N_String_Literal)
446 and then Exp_Typ /= Check_Typ
447 then
448 if Is_Entity_Name (Exp)
449 and then Ekind (Entity (Exp)) = E_Constant
450 then
451 -- If expression is a constant, it is worthwhile checking whether
452 -- it is a bound of the type.
453
454 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
455 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
456 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
457 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
458 then
459 return;
460
461 else
462 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
463 Analyze_And_Resolve (Exp, Check_Typ);
464 Check_Unset_Reference (Exp);
465 end if;
466 else
467 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
468 Analyze_And_Resolve (Exp, Check_Typ);
469 Check_Unset_Reference (Exp);
470 end if;
471
472 -- Ada 0Y (AI-231): Generate conversion to the null-excluding
473 -- type to force the corresponding run-time check
474
475 elsif Is_Access_Type (Check_Typ)
476 and then Can_Never_Be_Null (Check_Typ)
477 and then not Can_Never_Be_Null (Exp_Typ)
478 then
479 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
480 Analyze_And_Resolve (Exp, Check_Typ);
481 Check_Unset_Reference (Exp);
482 end if;
483 end Aggregate_Constraint_Checks;
484
485 ------------------------
486 -- Array_Aggr_Subtype --
487 ------------------------
488
489 function Array_Aggr_Subtype
490 (N : Node_Id;
491 Typ : Entity_Id)
492 return Entity_Id
493 is
494 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
495 -- Number of aggregate index dimensions.
496
497 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
498 -- Constrained N_Range of each index dimension in our aggregate itype.
499
500 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
501 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
502 -- Low and High bounds for each index dimension in our aggregate itype.
503
504 Is_Fully_Positional : Boolean := True;
505
506 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
507 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
508 -- (sub-)aggregate N. This procedure collects the constrained N_Range
509 -- nodes corresponding to each index dimension of our aggregate itype.
510 -- These N_Range nodes are collected in Aggr_Range above.
511 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
512 -- bounds of each index dimension. If, when collecting, two bounds
513 -- corresponding to the same dimension are static and found to differ,
514 -- then emit a warning, and mark N as raising Constraint_Error.
515
516 -------------------------
517 -- Collect_Aggr_Bounds --
518 -------------------------
519
520 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
521 This_Range : constant Node_Id := Aggregate_Bounds (N);
522 -- The aggregate range node of this specific sub-aggregate.
523
524 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
525 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
526 -- The aggregate bounds of this specific sub-aggregate.
527
528 Assoc : Node_Id;
529 Expr : Node_Id;
530
531 begin
532 -- Collect the first N_Range for a given dimension that you find.
533 -- For a given dimension they must be all equal anyway.
534
535 if No (Aggr_Range (Dim)) then
536 Aggr_Low (Dim) := This_Low;
537 Aggr_High (Dim) := This_High;
538 Aggr_Range (Dim) := This_Range;
539
540 else
541 if Compile_Time_Known_Value (This_Low) then
542 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
543 Aggr_Low (Dim) := This_Low;
544
545 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
546 Set_Raises_Constraint_Error (N);
547 Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
548 Error_Msg_N ("Constraint_Error will be raised at run-time?",
549 N);
550 end if;
551 end if;
552
553 if Compile_Time_Known_Value (This_High) then
554 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
555 Aggr_High (Dim) := This_High;
556
557 elsif
558 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
559 then
560 Set_Raises_Constraint_Error (N);
561 Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
562 Error_Msg_N ("Constraint_Error will be raised at run-time?",
563 N);
564 end if;
565 end if;
566 end if;
567
568 if Dim < Aggr_Dimension then
569
570 -- Process positional components
571
572 if Present (Expressions (N)) then
573 Expr := First (Expressions (N));
574 while Present (Expr) loop
575 Collect_Aggr_Bounds (Expr, Dim + 1);
576 Next (Expr);
577 end loop;
578 end if;
579
580 -- Process component associations
581
582 if Present (Component_Associations (N)) then
583 Is_Fully_Positional := False;
584
585 Assoc := First (Component_Associations (N));
586 while Present (Assoc) loop
587 Expr := Expression (Assoc);
588 Collect_Aggr_Bounds (Expr, Dim + 1);
589 Next (Assoc);
590 end loop;
591 end if;
592 end if;
593 end Collect_Aggr_Bounds;
594
595 -- Array_Aggr_Subtype variables
596
597 Itype : Entity_Id;
598 -- the final itype of the overall aggregate
599
600 Index_Constraints : constant List_Id := New_List;
601 -- The list of index constraints of the aggregate itype.
602
603 -- Start of processing for Array_Aggr_Subtype
604
605 begin
606 -- Make sure that the list of index constraints is properly attached
607 -- to the tree, and then collect the aggregate bounds.
608
609 Set_Parent (Index_Constraints, N);
610 Collect_Aggr_Bounds (N, 1);
611
612 -- Build the list of constrained indices of our aggregate itype.
613
614 for J in 1 .. Aggr_Dimension loop
615 Create_Index : declare
616 Index_Base : constant Entity_Id :=
617 Base_Type (Etype (Aggr_Range (J)));
618 Index_Typ : Entity_Id;
619
620 begin
621 -- Construct the Index subtype
622
623 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
624
625 Set_Etype (Index_Typ, Index_Base);
626
627 if Is_Character_Type (Index_Base) then
628 Set_Is_Character_Type (Index_Typ);
629 end if;
630
631 Set_Size_Info (Index_Typ, (Index_Base));
632 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
633 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
634 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
635
636 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
637 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
638 end if;
639
640 Set_Etype (Aggr_Range (J), Index_Typ);
641
642 Append (Aggr_Range (J), To => Index_Constraints);
643 end Create_Index;
644 end loop;
645
646 -- Now build the Itype
647
648 Itype := Create_Itype (E_Array_Subtype, N);
649
650 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
651 Set_Convention (Itype, Convention (Typ));
652 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
653 Set_Etype (Itype, Base_Type (Typ));
654 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
655 Set_Is_Aliased (Itype, Is_Aliased (Typ));
656 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
657
658 Copy_Suppress_Status (Index_Check, Typ, Itype);
659 Copy_Suppress_Status (Length_Check, Typ, Itype);
660
661 Set_First_Index (Itype, First (Index_Constraints));
662 Set_Is_Constrained (Itype, True);
663 Set_Is_Internal (Itype, True);
664 Init_Size_Align (Itype);
665
666 -- A simple optimization: purely positional aggregates of static
667 -- components should be passed to gigi unexpanded whenever possible,
668 -- and regardless of the staticness of the bounds themselves. Subse-
669 -- quent checks in exp_aggr verify that type is not packed, etc.
670
671 Set_Size_Known_At_Compile_Time (Itype,
672 Is_Fully_Positional
673 and then Comes_From_Source (N)
674 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
675
676 -- We always need a freeze node for a packed array subtype, so that
677 -- we can build the Packed_Array_Type corresponding to the subtype.
678 -- If expansion is disabled, the packed array subtype is not built,
679 -- and we must not generate a freeze node for the type, or else it
680 -- will appear incomplete to gigi.
681
682 if Is_Packed (Itype) and then not In_Default_Expression
683 and then Expander_Active
684 then
685 Freeze_Itype (Itype, N);
686 end if;
687
688 return Itype;
689 end Array_Aggr_Subtype;
690
691 --------------------------------
692 -- Check_Misspelled_Component --
693 --------------------------------
694
695 procedure Check_Misspelled_Component
696 (Elements : Elist_Id;
697 Component : Node_Id)
698 is
699 Max_Suggestions : constant := 2;
700
701 Nr_Of_Suggestions : Natural := 0;
702 Suggestion_1 : Entity_Id := Empty;
703 Suggestion_2 : Entity_Id := Empty;
704 Component_Elmt : Elmt_Id;
705
706 begin
707 -- All the components of List are matched against Component and
708 -- a count is maintained of possible misspellings. When at the
709 -- end of the analysis there are one or two (not more!) possible
710 -- misspellings, these misspellings will be suggested as
711 -- possible correction.
712
713 Get_Name_String (Chars (Component));
714
715 declare
716 S : constant String (1 .. Name_Len) :=
717 Name_Buffer (1 .. Name_Len);
718
719 begin
720
721 Component_Elmt := First_Elmt (Elements);
722
723 while Nr_Of_Suggestions <= Max_Suggestions
724 and then Present (Component_Elmt)
725 loop
726
727 Get_Name_String (Chars (Node (Component_Elmt)));
728
729 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
730 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
731
732 case Nr_Of_Suggestions is
733 when 1 => Suggestion_1 := Node (Component_Elmt);
734 when 2 => Suggestion_2 := Node (Component_Elmt);
735 when others => exit;
736 end case;
737 end if;
738
739 Next_Elmt (Component_Elmt);
740 end loop;
741
742 -- Report at most two suggestions
743
744 if Nr_Of_Suggestions = 1 then
745 Error_Msg_NE ("\possible misspelling of&",
746 Component, Suggestion_1);
747
748 elsif Nr_Of_Suggestions = 2 then
749 Error_Msg_Node_2 := Suggestion_2;
750 Error_Msg_NE ("\possible misspelling of& or&",
751 Component, Suggestion_1);
752 end if;
753 end;
754 end Check_Misspelled_Component;
755
756 ----------------------------------------
757 -- Check_Static_Discriminated_Subtype --
758 ----------------------------------------
759
760 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
761 Disc : constant Entity_Id := First_Discriminant (T);
762 Comp : Entity_Id;
763 Ind : Entity_Id;
764
765 begin
766 if Has_Record_Rep_Clause (T) then
767 return;
768
769 elsif Present (Next_Discriminant (Disc)) then
770 return;
771
772 elsif Nkind (V) /= N_Integer_Literal then
773 return;
774 end if;
775
776 Comp := First_Component (T);
777
778 while Present (Comp) loop
779
780 if Is_Scalar_Type (Etype (Comp)) then
781 null;
782
783 elsif Is_Private_Type (Etype (Comp))
784 and then Present (Full_View (Etype (Comp)))
785 and then Is_Scalar_Type (Full_View (Etype (Comp)))
786 then
787 null;
788
789 elsif Is_Array_Type (Etype (Comp)) then
790
791 if Is_Bit_Packed_Array (Etype (Comp)) then
792 return;
793 end if;
794
795 Ind := First_Index (Etype (Comp));
796
797 while Present (Ind) loop
798
799 if Nkind (Ind) /= N_Range
800 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
801 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
802 then
803 return;
804 end if;
805
806 Next_Index (Ind);
807 end loop;
808
809 else
810 return;
811 end if;
812
813 Next_Component (Comp);
814 end loop;
815
816 -- On exit, all components have statically known sizes.
817
818 Set_Size_Known_At_Compile_Time (T);
819 end Check_Static_Discriminated_Subtype;
820
821 --------------------------------
822 -- Make_String_Into_Aggregate --
823 --------------------------------
824
825 procedure Make_String_Into_Aggregate (N : Node_Id) is
826 Exprs : constant List_Id := New_List;
827 Loc : constant Source_Ptr := Sloc (N);
828 Str : constant String_Id := Strval (N);
829 Strlen : constant Nat := String_Length (Str);
830 C : Char_Code;
831 C_Node : Node_Id;
832 New_N : Node_Id;
833 P : Source_Ptr;
834
835 begin
836 P := Loc + 1;
837 for J in 1 .. Strlen loop
838 C := Get_String_Char (Str, J);
839 Set_Character_Literal_Name (C);
840
841 C_Node := Make_Character_Literal (P, Name_Find, C);
842 Set_Etype (C_Node, Any_Character);
843 Append_To (Exprs, C_Node);
844
845 P := P + 1;
846 -- something special for wide strings ???
847 end loop;
848
849 New_N := Make_Aggregate (Loc, Expressions => Exprs);
850 Set_Analyzed (New_N);
851 Set_Etype (New_N, Any_Composite);
852
853 Rewrite (N, New_N);
854 end Make_String_Into_Aggregate;
855
856 -----------------------
857 -- Resolve_Aggregate --
858 -----------------------
859
860 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
861 Pkind : constant Node_Kind := Nkind (Parent (N));
862
863 Aggr_Subtyp : Entity_Id;
864 -- The actual aggregate subtype. This is not necessarily the same as Typ
865 -- which is the subtype of the context in which the aggregate was found.
866
867 begin
868 -- Check for aggregates not allowed in configurable run-time mode.
869 -- We allow all cases of aggregates that do not come from source,
870 -- since these are all assumed to be small (e.g. bounds of a string
871 -- literal). We also allow aggregates of types we know to be small.
872
873 if not Support_Aggregates_On_Target
874 and then Comes_From_Source (N)
875 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
876 then
877 Error_Msg_CRT ("aggregate", N);
878 end if;
879
880 if Is_Limited_Composite (Typ) then
881 Error_Msg_N ("aggregate type cannot have limited component", N);
882 Explain_Limited_Type (Typ, N);
883
884 -- Ada 0Y (AI-287): Limited aggregates allowed
885
886 elsif Is_Limited_Type (Typ)
887 and not Extensions_Allowed
888 then
889 Error_Msg_N ("aggregate type cannot be limited", N);
890 Explain_Limited_Type (Typ, N);
891
892 elsif Is_Class_Wide_Type (Typ) then
893 Error_Msg_N ("type of aggregate cannot be class-wide", N);
894
895 elsif Typ = Any_String
896 or else Typ = Any_Composite
897 then
898 Error_Msg_N ("no unique type for aggregate", N);
899 Set_Etype (N, Any_Composite);
900
901 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
902 Error_Msg_N ("null record forbidden in array aggregate", N);
903
904 elsif Is_Record_Type (Typ) then
905 Resolve_Record_Aggregate (N, Typ);
906
907 elsif Is_Array_Type (Typ) then
908
909 -- First a special test, for the case of a positional aggregate
910 -- of characters which can be replaced by a string literal.
911 -- Do not perform this transformation if this was a string literal
912 -- to start with, whose components needed constraint checks, or if
913 -- the component type is non-static, because it will require those
914 -- checks and be transformed back into an aggregate.
915
916 if Number_Dimensions (Typ) = 1
917 and then
918 (Root_Type (Component_Type (Typ)) = Standard_Character
919 or else
920 Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
921 and then No (Component_Associations (N))
922 and then not Is_Limited_Composite (Typ)
923 and then not Is_Private_Composite (Typ)
924 and then not Is_Bit_Packed_Array (Typ)
925 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
926 and then Is_Static_Subtype (Component_Type (Typ))
927 then
928 declare
929 Expr : Node_Id;
930
931 begin
932 Expr := First (Expressions (N));
933 while Present (Expr) loop
934 exit when Nkind (Expr) /= N_Character_Literal;
935 Next (Expr);
936 end loop;
937
938 if No (Expr) then
939 Start_String;
940
941 Expr := First (Expressions (N));
942 while Present (Expr) loop
943 Store_String_Char (Char_Literal_Value (Expr));
944 Next (Expr);
945 end loop;
946
947 Rewrite (N,
948 Make_String_Literal (Sloc (N), End_String));
949
950 Analyze_And_Resolve (N, Typ);
951 return;
952 end if;
953 end;
954 end if;
955
956 -- Here if we have a real aggregate to deal with
957
958 Array_Aggregate : declare
959 Aggr_Resolved : Boolean;
960
961 Aggr_Typ : constant Entity_Id := Etype (Typ);
962 -- This is the unconstrained array type, which is the type
963 -- against which the aggregate is to be resolved. Typ itself
964 -- is the array type of the context which may not be the same
965 -- subtype as the subtype for the final aggregate.
966
967 begin
968 -- In the following we determine whether an others choice is
969 -- allowed inside the array aggregate. The test checks the context
970 -- in which the array aggregate occurs. If the context does not
971 -- permit it, or the aggregate type is unconstrained, an others
972 -- choice is not allowed.
973 --
974 -- Note that there is no node for Explicit_Actual_Parameter.
975 -- To test for this context we therefore have to test for node
976 -- N_Parameter_Association which itself appears only if there is a
977 -- formal parameter. Consequently we also need to test for
978 -- N_Procedure_Call_Statement or N_Function_Call.
979
980 Set_Etype (N, Aggr_Typ); -- may be overridden later on
981
982 -- Ada 0Y (AI-231): Propagate the null_exclusion attribute to the
983 -- components of the array aggregate
984
985 if Extensions_Allowed then
986 Set_Can_Never_Be_Null (Aggr_Typ, Can_Never_Be_Null (Typ));
987 end if;
988
989 if Is_Constrained (Typ) and then
990 (Pkind = N_Assignment_Statement or else
991 Pkind = N_Parameter_Association or else
992 Pkind = N_Function_Call or else
993 Pkind = N_Procedure_Call_Statement or else
994 Pkind = N_Generic_Association or else
995 Pkind = N_Formal_Object_Declaration or else
996 Pkind = N_Return_Statement or else
997 Pkind = N_Object_Declaration or else
998 Pkind = N_Component_Declaration or else
999 Pkind = N_Parameter_Specification or else
1000 Pkind = N_Qualified_Expression or else
1001 Pkind = N_Aggregate or else
1002 Pkind = N_Extension_Aggregate or else
1003 Pkind = N_Component_Association)
1004 then
1005 Aggr_Resolved :=
1006 Resolve_Array_Aggregate
1007 (N,
1008 Index => First_Index (Aggr_Typ),
1009 Index_Constr => First_Index (Typ),
1010 Component_Typ => Component_Type (Typ),
1011 Others_Allowed => True);
1012
1013 else
1014 Aggr_Resolved :=
1015 Resolve_Array_Aggregate
1016 (N,
1017 Index => First_Index (Aggr_Typ),
1018 Index_Constr => First_Index (Aggr_Typ),
1019 Component_Typ => Component_Type (Typ),
1020 Others_Allowed => False);
1021 end if;
1022
1023 if not Aggr_Resolved then
1024 Aggr_Subtyp := Any_Composite;
1025 else
1026 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1027 end if;
1028
1029 Set_Etype (N, Aggr_Subtyp);
1030 end Array_Aggregate;
1031
1032 else
1033 Error_Msg_N ("illegal context for aggregate", N);
1034
1035 end if;
1036
1037 -- If we can determine statically that the evaluation of the
1038 -- aggregate raises Constraint_Error, then replace the
1039 -- aggregate with an N_Raise_Constraint_Error node, but set the
1040 -- Etype to the right aggregate subtype. Gigi needs this.
1041
1042 if Raises_Constraint_Error (N) then
1043 Aggr_Subtyp := Etype (N);
1044 Rewrite (N,
1045 Make_Raise_Constraint_Error (Sloc (N),
1046 Reason => CE_Range_Check_Failed));
1047 Set_Raises_Constraint_Error (N);
1048 Set_Etype (N, Aggr_Subtyp);
1049 Set_Analyzed (N);
1050 end if;
1051 end Resolve_Aggregate;
1052
1053 -----------------------------
1054 -- Resolve_Array_Aggregate --
1055 -----------------------------
1056
1057 function Resolve_Array_Aggregate
1058 (N : Node_Id;
1059 Index : Node_Id;
1060 Index_Constr : Node_Id;
1061 Component_Typ : Entity_Id;
1062 Others_Allowed : Boolean)
1063 return Boolean
1064 is
1065 Loc : constant Source_Ptr := Sloc (N);
1066
1067 Failure : constant Boolean := False;
1068 Success : constant Boolean := True;
1069
1070 Index_Typ : constant Entity_Id := Etype (Index);
1071 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1072 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1073 -- The type of the index corresponding to the array sub-aggregate
1074 -- along with its low and upper bounds
1075
1076 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1077 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1078 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1079 -- ditto for the base type
1080
1081 function Add (Val : Uint; To : Node_Id) return Node_Id;
1082 -- Creates a new expression node where Val is added to expression To.
1083 -- Tries to constant fold whenever possible. To must be an already
1084 -- analyzed expression.
1085
1086 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1087 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1088 -- (the upper bound of the index base type). If the check fails a
1089 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1090 -- and AH is replaced with a duplicate of BH.
1091
1092 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1093 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1094 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1095
1096 procedure Check_Length (L, H : Node_Id; Len : Uint);
1097 -- Checks that range L .. H contains at least Len elements. Emits a
1098 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1099
1100 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1101 -- Returns True if range L .. H is dynamic or null.
1102
1103 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1104 -- Given expression node From, this routine sets OK to False if it
1105 -- cannot statically evaluate From. Otherwise it stores this static
1106 -- value into Value.
1107
1108 function Resolve_Aggr_Expr
1109 (Expr : Node_Id;
1110 Single_Elmt : Boolean)
1111 return Boolean;
1112 -- Resolves aggregate expression Expr. Returs False if resolution
1113 -- fails. If Single_Elmt is set to False, the expression Expr may be
1114 -- used to initialize several array aggregate elements (this can
1115 -- happen for discrete choices such as "L .. H => Expr" or the others
1116 -- choice). In this event we do not resolve Expr unless expansion is
1117 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1118 -- note above.
1119
1120 ---------
1121 -- Add --
1122 ---------
1123
1124 function Add (Val : Uint; To : Node_Id) return Node_Id is
1125 Expr_Pos : Node_Id;
1126 Expr : Node_Id;
1127 To_Pos : Node_Id;
1128
1129 begin
1130 if Raises_Constraint_Error (To) then
1131 return To;
1132 end if;
1133
1134 -- First test if we can do constant folding
1135
1136 if Compile_Time_Known_Value (To)
1137 or else Nkind (To) = N_Integer_Literal
1138 then
1139 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1140 Set_Is_Static_Expression (Expr_Pos);
1141 Set_Etype (Expr_Pos, Etype (To));
1142 Set_Analyzed (Expr_Pos, Analyzed (To));
1143
1144 if not Is_Enumeration_Type (Index_Typ) then
1145 Expr := Expr_Pos;
1146
1147 -- If we are dealing with enumeration return
1148 -- Index_Typ'Val (Expr_Pos)
1149
1150 else
1151 Expr :=
1152 Make_Attribute_Reference
1153 (Loc,
1154 Prefix => New_Reference_To (Index_Typ, Loc),
1155 Attribute_Name => Name_Val,
1156 Expressions => New_List (Expr_Pos));
1157 end if;
1158
1159 return Expr;
1160 end if;
1161
1162 -- If we are here no constant folding possible
1163
1164 if not Is_Enumeration_Type (Index_Base) then
1165 Expr :=
1166 Make_Op_Add (Loc,
1167 Left_Opnd => Duplicate_Subexpr (To),
1168 Right_Opnd => Make_Integer_Literal (Loc, Val));
1169
1170 -- If we are dealing with enumeration return
1171 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1172
1173 else
1174 To_Pos :=
1175 Make_Attribute_Reference
1176 (Loc,
1177 Prefix => New_Reference_To (Index_Typ, Loc),
1178 Attribute_Name => Name_Pos,
1179 Expressions => New_List (Duplicate_Subexpr (To)));
1180
1181 Expr_Pos :=
1182 Make_Op_Add (Loc,
1183 Left_Opnd => To_Pos,
1184 Right_Opnd => Make_Integer_Literal (Loc, Val));
1185
1186 Expr :=
1187 Make_Attribute_Reference
1188 (Loc,
1189 Prefix => New_Reference_To (Index_Typ, Loc),
1190 Attribute_Name => Name_Val,
1191 Expressions => New_List (Expr_Pos));
1192 end if;
1193
1194 return Expr;
1195 end Add;
1196
1197 -----------------
1198 -- Check_Bound --
1199 -----------------
1200
1201 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1202 Val_BH : Uint;
1203 Val_AH : Uint;
1204
1205 OK_BH : Boolean;
1206 OK_AH : Boolean;
1207
1208 begin
1209 Get (Value => Val_BH, From => BH, OK => OK_BH);
1210 Get (Value => Val_AH, From => AH, OK => OK_AH);
1211
1212 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1213 Set_Raises_Constraint_Error (N);
1214 Error_Msg_N ("upper bound out of range?", AH);
1215 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1216
1217 -- You need to set AH to BH or else in the case of enumerations
1218 -- indices we will not be able to resolve the aggregate bounds.
1219
1220 AH := Duplicate_Subexpr (BH);
1221 end if;
1222 end Check_Bound;
1223
1224 ------------------
1225 -- Check_Bounds --
1226 ------------------
1227
1228 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1229 Val_L : Uint;
1230 Val_H : Uint;
1231 Val_AL : Uint;
1232 Val_AH : Uint;
1233
1234 OK_L : Boolean;
1235 OK_H : Boolean;
1236 OK_AL : Boolean;
1237 OK_AH : Boolean;
1238
1239 begin
1240 if Raises_Constraint_Error (N)
1241 or else Dynamic_Or_Null_Range (AL, AH)
1242 then
1243 return;
1244 end if;
1245
1246 Get (Value => Val_L, From => L, OK => OK_L);
1247 Get (Value => Val_H, From => H, OK => OK_H);
1248
1249 Get (Value => Val_AL, From => AL, OK => OK_AL);
1250 Get (Value => Val_AH, From => AH, OK => OK_AH);
1251
1252 if OK_L and then Val_L > Val_AL then
1253 Set_Raises_Constraint_Error (N);
1254 Error_Msg_N ("lower bound of aggregate out of range?", N);
1255 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1256 end if;
1257
1258 if OK_H and then Val_H < Val_AH then
1259 Set_Raises_Constraint_Error (N);
1260 Error_Msg_N ("upper bound of aggregate out of range?", N);
1261 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1262 end if;
1263 end Check_Bounds;
1264
1265 ------------------
1266 -- Check_Length --
1267 ------------------
1268
1269 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1270 Val_L : Uint;
1271 Val_H : Uint;
1272
1273 OK_L : Boolean;
1274 OK_H : Boolean;
1275
1276 Range_Len : Uint;
1277
1278 begin
1279 if Raises_Constraint_Error (N) then
1280 return;
1281 end if;
1282
1283 Get (Value => Val_L, From => L, OK => OK_L);
1284 Get (Value => Val_H, From => H, OK => OK_H);
1285
1286 if not OK_L or else not OK_H then
1287 return;
1288 end if;
1289
1290 -- If null range length is zero
1291
1292 if Val_L > Val_H then
1293 Range_Len := Uint_0;
1294 else
1295 Range_Len := Val_H - Val_L + 1;
1296 end if;
1297
1298 if Range_Len < Len then
1299 Set_Raises_Constraint_Error (N);
1300 Error_Msg_N ("Too many elements?", N);
1301 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1302 end if;
1303 end Check_Length;
1304
1305 ---------------------------
1306 -- Dynamic_Or_Null_Range --
1307 ---------------------------
1308
1309 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1310 Val_L : Uint;
1311 Val_H : Uint;
1312
1313 OK_L : Boolean;
1314 OK_H : Boolean;
1315
1316 begin
1317 Get (Value => Val_L, From => L, OK => OK_L);
1318 Get (Value => Val_H, From => H, OK => OK_H);
1319
1320 return not OK_L or else not OK_H
1321 or else not Is_OK_Static_Expression (L)
1322 or else not Is_OK_Static_Expression (H)
1323 or else Val_L > Val_H;
1324 end Dynamic_Or_Null_Range;
1325
1326 ---------
1327 -- Get --
1328 ---------
1329
1330 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1331 begin
1332 OK := True;
1333
1334 if Compile_Time_Known_Value (From) then
1335 Value := Expr_Value (From);
1336
1337 -- If expression From is something like Some_Type'Val (10) then
1338 -- Value = 10
1339
1340 elsif Nkind (From) = N_Attribute_Reference
1341 and then Attribute_Name (From) = Name_Val
1342 and then Compile_Time_Known_Value (First (Expressions (From)))
1343 then
1344 Value := Expr_Value (First (Expressions (From)));
1345
1346 else
1347 Value := Uint_0;
1348 OK := False;
1349 end if;
1350 end Get;
1351
1352 -----------------------
1353 -- Resolve_Aggr_Expr --
1354 -----------------------
1355
1356 function Resolve_Aggr_Expr
1357 (Expr : Node_Id;
1358 Single_Elmt : Boolean)
1359 return Boolean
1360 is
1361 Nxt_Ind : constant Node_Id := Next_Index (Index);
1362 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1363 -- Index is the current index corresponding to the expresion.
1364
1365 Resolution_OK : Boolean := True;
1366 -- Set to False if resolution of the expression failed.
1367
1368 begin
1369 -- If the array type against which we are resolving the aggregate
1370 -- has several dimensions, the expressions nested inside the
1371 -- aggregate must be further aggregates (or strings).
1372
1373 if Present (Nxt_Ind) then
1374 if Nkind (Expr) /= N_Aggregate then
1375
1376 -- A string literal can appear where a one-dimensional array
1377 -- of characters is expected. If the literal looks like an
1378 -- operator, it is still an operator symbol, which will be
1379 -- transformed into a string when analyzed.
1380
1381 if Is_Character_Type (Component_Typ)
1382 and then No (Next_Index (Nxt_Ind))
1383 and then (Nkind (Expr) = N_String_Literal
1384 or else Nkind (Expr) = N_Operator_Symbol)
1385 then
1386 -- A string literal used in a multidimensional array
1387 -- aggregate in place of the final one-dimensional
1388 -- aggregate must not be enclosed in parentheses.
1389
1390 if Paren_Count (Expr) /= 0 then
1391 Error_Msg_N ("No parenthesis allowed here", Expr);
1392 end if;
1393
1394 Make_String_Into_Aggregate (Expr);
1395
1396 else
1397 Error_Msg_N ("nested array aggregate expected", Expr);
1398 return Failure;
1399 end if;
1400 end if;
1401
1402 -- Ada 0Y (AI-231): Propagate the type to the nested aggregate.
1403 -- Required to check the null-exclusion attribute (if present).
1404 -- This value may be overridden later on.
1405
1406 Set_Etype (Expr, Etype (N));
1407
1408 Resolution_OK := Resolve_Array_Aggregate
1409 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1410
1411 -- Do not resolve the expressions of discrete or others choices
1412 -- unless the expression covers a single component, or the expander
1413 -- is inactive.
1414
1415 elsif Single_Elmt
1416 or else not Expander_Active
1417 or else In_Default_Expression
1418 then
1419 Analyze_And_Resolve (Expr, Component_Typ);
1420 Check_Non_Static_Context (Expr);
1421 Aggregate_Constraint_Checks (Expr, Component_Typ);
1422 Check_Unset_Reference (Expr);
1423 end if;
1424
1425 if Raises_Constraint_Error (Expr)
1426 and then Nkind (Parent (Expr)) /= N_Component_Association
1427 then
1428 Set_Raises_Constraint_Error (N);
1429 end if;
1430
1431 return Resolution_OK;
1432 end Resolve_Aggr_Expr;
1433
1434 -- Variables local to Resolve_Array_Aggregate
1435
1436 Assoc : Node_Id;
1437 Choice : Node_Id;
1438 Expr : Node_Id;
1439
1440 Who_Cares : Node_Id;
1441
1442 Aggr_Low : Node_Id := Empty;
1443 Aggr_High : Node_Id := Empty;
1444 -- The actual low and high bounds of this sub-aggegate
1445
1446 Choices_Low : Node_Id := Empty;
1447 Choices_High : Node_Id := Empty;
1448 -- The lowest and highest discrete choices values for a named aggregate
1449
1450 Nb_Elements : Uint := Uint_0;
1451 -- The number of elements in a positional aggegate
1452
1453 Others_Present : Boolean := False;
1454
1455 Nb_Choices : Nat := 0;
1456 -- Contains the overall number of named choices in this sub-aggregate
1457
1458 Nb_Discrete_Choices : Nat := 0;
1459 -- The overall number of discrete choices (not counting others choice)
1460
1461 Case_Table_Size : Nat;
1462 -- Contains the size of the case table needed to sort aggregate choices
1463
1464 -- Start of processing for Resolve_Array_Aggregate
1465
1466 begin
1467 -- STEP 1: make sure the aggregate is correctly formatted
1468
1469 if Present (Component_Associations (N)) then
1470 Assoc := First (Component_Associations (N));
1471 while Present (Assoc) loop
1472 Choice := First (Choices (Assoc));
1473 while Present (Choice) loop
1474 if Nkind (Choice) = N_Others_Choice then
1475 Others_Present := True;
1476
1477 if Choice /= First (Choices (Assoc))
1478 or else Present (Next (Choice))
1479 then
1480 Error_Msg_N
1481 ("OTHERS must appear alone in a choice list", Choice);
1482 return Failure;
1483 end if;
1484
1485 if Present (Next (Assoc)) then
1486 Error_Msg_N
1487 ("OTHERS must appear last in an aggregate", Choice);
1488 return Failure;
1489 end if;
1490
1491 if Ada_83
1492 and then Assoc /= First (Component_Associations (N))
1493 and then (Nkind (Parent (N)) = N_Assignment_Statement
1494 or else
1495 Nkind (Parent (N)) = N_Object_Declaration)
1496 then
1497 Error_Msg_N
1498 ("(Ada 83) illegal context for OTHERS choice", N);
1499 end if;
1500 end if;
1501
1502 Nb_Choices := Nb_Choices + 1;
1503 Next (Choice);
1504 end loop;
1505
1506 Next (Assoc);
1507 end loop;
1508 end if;
1509
1510 -- At this point we know that the others choice, if present, is by
1511 -- itself and appears last in the aggregate. Check if we have mixed
1512 -- positional and discrete associations (other than the others choice).
1513
1514 if Present (Expressions (N))
1515 and then (Nb_Choices > 1
1516 or else (Nb_Choices = 1 and then not Others_Present))
1517 then
1518 Error_Msg_N
1519 ("named association cannot follow positional association",
1520 First (Choices (First (Component_Associations (N)))));
1521 return Failure;
1522 end if;
1523
1524 -- Test for the validity of an others choice if present
1525
1526 if Others_Present and then not Others_Allowed then
1527 Error_Msg_N
1528 ("OTHERS choice not allowed here",
1529 First (Choices (First (Component_Associations (N)))));
1530 return Failure;
1531 end if;
1532
1533 -- Protect against cascaded errors
1534
1535 if Etype (Index_Typ) = Any_Type then
1536 return Failure;
1537 end if;
1538
1539 -- STEP 2: Process named components
1540
1541 if No (Expressions (N)) then
1542
1543 if Others_Present then
1544 Case_Table_Size := Nb_Choices - 1;
1545 else
1546 Case_Table_Size := Nb_Choices;
1547 end if;
1548
1549 Step_2 : declare
1550 Low : Node_Id;
1551 High : Node_Id;
1552 -- Denote the lowest and highest values in an aggregate choice
1553
1554 Hi_Val : Uint;
1555 Lo_Val : Uint;
1556 -- High end of one range and Low end of the next. Should be
1557 -- contiguous if there is no hole in the list of values.
1558
1559 Missing_Values : Boolean;
1560 -- Set True if missing index values
1561
1562 S_Low : Node_Id := Empty;
1563 S_High : Node_Id := Empty;
1564 -- if a choice in an aggregate is a subtype indication these
1565 -- denote the lowest and highest values of the subtype
1566
1567 Table : Case_Table_Type (1 .. Case_Table_Size);
1568 -- Used to sort all the different choice values
1569
1570 Single_Choice : Boolean;
1571 -- Set to true every time there is a single discrete choice in a
1572 -- discrete association
1573
1574 Prev_Nb_Discrete_Choices : Nat;
1575 -- Used to keep track of the number of discrete choices
1576 -- in the current association.
1577
1578 begin
1579 -- STEP 2 (A): Check discrete choices validity.
1580
1581 Assoc := First (Component_Associations (N));
1582 while Present (Assoc) loop
1583
1584 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1585 Choice := First (Choices (Assoc));
1586 loop
1587 Analyze (Choice);
1588
1589 if Nkind (Choice) = N_Others_Choice then
1590 Single_Choice := False;
1591 exit;
1592
1593 -- Test for subtype mark without constraint
1594
1595 elsif Is_Entity_Name (Choice) and then
1596 Is_Type (Entity (Choice))
1597 then
1598 if Base_Type (Entity (Choice)) /= Index_Base then
1599 Error_Msg_N
1600 ("invalid subtype mark in aggregate choice",
1601 Choice);
1602 return Failure;
1603 end if;
1604
1605 elsif Nkind (Choice) = N_Subtype_Indication then
1606 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1607
1608 -- Does the subtype indication evaluation raise CE ?
1609
1610 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1611 Get_Index_Bounds (Choice, Low, High);
1612 Check_Bounds (S_Low, S_High, Low, High);
1613
1614 else -- Choice is a range or an expression
1615 Resolve (Choice, Index_Base);
1616 Check_Unset_Reference (Choice);
1617 Check_Non_Static_Context (Choice);
1618
1619 -- Do not range check a choice. This check is redundant
1620 -- since this test is already performed when we check
1621 -- that the bounds of the array aggregate are within
1622 -- range.
1623
1624 Set_Do_Range_Check (Choice, False);
1625 end if;
1626
1627 -- If we could not resolve the discrete choice stop here
1628
1629 if Etype (Choice) = Any_Type then
1630 return Failure;
1631
1632 -- If the discrete choice raises CE get its original bounds.
1633
1634 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1635 Set_Raises_Constraint_Error (N);
1636 Get_Index_Bounds (Original_Node (Choice), Low, High);
1637
1638 -- Otherwise get its bounds as usual
1639
1640 else
1641 Get_Index_Bounds (Choice, Low, High);
1642 end if;
1643
1644 if (Dynamic_Or_Null_Range (Low, High)
1645 or else (Nkind (Choice) = N_Subtype_Indication
1646 and then
1647 Dynamic_Or_Null_Range (S_Low, S_High)))
1648 and then Nb_Choices /= 1
1649 then
1650 Error_Msg_N
1651 ("dynamic or empty choice in aggregate " &
1652 "must be the only choice", Choice);
1653 return Failure;
1654 end if;
1655
1656 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1657 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1658 Table (Nb_Discrete_Choices).Choice_Hi := High;
1659
1660 Next (Choice);
1661
1662 if No (Choice) then
1663 -- Check if we have a single discrete choice and whether
1664 -- this discrete choice specifies a single value.
1665
1666 Single_Choice :=
1667 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1668 and then (Low = High);
1669
1670 exit;
1671 end if;
1672 end loop;
1673
1674 -- Ada 0Y (AI-231)
1675
1676 Check_Can_Never_Be_Null (N, Expression (Assoc));
1677
1678 -- Ada 0Y (AI-287): In case of default initialized component
1679 -- we delay the resolution to the expansion phase
1680
1681 if Box_Present (Assoc) then
1682
1683 -- Ada 0Y (AI-287): In case of default initialization of a
1684 -- component the expander will generate calls to the
1685 -- corresponding initialization subprogram.
1686
1687 if Present (Base_Init_Proc (Etype (Component_Typ)))
1688 or else Has_Task (Base_Type (Component_Typ))
1689 then
1690 null;
1691 else
1692 Error_Msg_N
1693 ("(Ada 0Y): no value supplied for this component",
1694 Assoc);
1695 end if;
1696
1697 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1698 Single_Elmt => Single_Choice)
1699 then
1700 return Failure;
1701 end if;
1702
1703 Next (Assoc);
1704 end loop;
1705
1706 -- If aggregate contains more than one choice then these must be
1707 -- static. Sort them and check that they are contiguous
1708
1709 if Nb_Discrete_Choices > 1 then
1710 Sort_Case_Table (Table);
1711 Missing_Values := False;
1712
1713 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1714 if Expr_Value (Table (J).Choice_Hi) >=
1715 Expr_Value (Table (J + 1).Choice_Lo)
1716 then
1717 Error_Msg_N
1718 ("duplicate choice values in array aggregate",
1719 Table (J).Choice_Hi);
1720 return Failure;
1721
1722 elsif not Others_Present then
1723
1724 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1725 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1726
1727 -- If missing values, output error messages
1728
1729 if Lo_Val - Hi_Val > 1 then
1730
1731 -- Header message if not first missing value
1732
1733 if not Missing_Values then
1734 Error_Msg_N
1735 ("missing index value(s) in array aggregate", N);
1736 Missing_Values := True;
1737 end if;
1738
1739 -- Output values of missing indexes
1740
1741 Lo_Val := Lo_Val - 1;
1742 Hi_Val := Hi_Val + 1;
1743
1744 -- Enumeration type case
1745
1746 if Is_Enumeration_Type (Index_Typ) then
1747 Error_Msg_Name_1 :=
1748 Chars
1749 (Get_Enum_Lit_From_Pos
1750 (Index_Typ, Hi_Val, Loc));
1751
1752 if Lo_Val = Hi_Val then
1753 Error_Msg_N ("\ %", N);
1754 else
1755 Error_Msg_Name_2 :=
1756 Chars
1757 (Get_Enum_Lit_From_Pos
1758 (Index_Typ, Lo_Val, Loc));
1759 Error_Msg_N ("\ % .. %", N);
1760 end if;
1761
1762 -- Integer types case
1763
1764 else
1765 Error_Msg_Uint_1 := Hi_Val;
1766
1767 if Lo_Val = Hi_Val then
1768 Error_Msg_N ("\ ^", N);
1769 else
1770 Error_Msg_Uint_2 := Lo_Val;
1771 Error_Msg_N ("\ ^ .. ^", N);
1772 end if;
1773 end if;
1774 end if;
1775 end if;
1776 end loop Outer;
1777
1778 if Missing_Values then
1779 Set_Etype (N, Any_Composite);
1780 return Failure;
1781 end if;
1782 end if;
1783
1784 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1785
1786 if Nb_Discrete_Choices > 0 then
1787 Choices_Low := Table (1).Choice_Lo;
1788 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1789 end if;
1790
1791 if Others_Present then
1792 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1793
1794 else
1795 Aggr_Low := Choices_Low;
1796 Aggr_High := Choices_High;
1797 end if;
1798 end Step_2;
1799
1800 -- STEP 3: Process positional components
1801
1802 else
1803 -- STEP 3 (A): Process positional elements
1804
1805 Expr := First (Expressions (N));
1806 Nb_Elements := Uint_0;
1807 while Present (Expr) loop
1808 Nb_Elements := Nb_Elements + 1;
1809
1810 Check_Can_Never_Be_Null (N, Expr); -- Ada 0Y (AI-231)
1811
1812 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1813 return Failure;
1814 end if;
1815
1816 Next (Expr);
1817 end loop;
1818
1819 if Others_Present then
1820 Assoc := Last (Component_Associations (N));
1821
1822 Check_Can_Never_Be_Null (N, Expression (Assoc)); -- Ada 0Y (AI-231)
1823
1824 -- Ada 0Y (AI-287): In case of default initialized component
1825 -- we delay the resolution to the expansion phase.
1826
1827 if Box_Present (Assoc) then
1828
1829 -- Ada 0Y (AI-287): In case of default initialization of a
1830 -- component the expander will generate calls to the
1831 -- corresponding initialization subprogram.
1832
1833 if Present (Base_Init_Proc (Etype (Component_Typ))) then
1834 null;
1835 else
1836 Error_Msg_N
1837 ("(Ada 0Y): no value supplied for these components",
1838 Assoc);
1839 end if;
1840
1841 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1842 Single_Elmt => False)
1843 then
1844 return Failure;
1845 end if;
1846 end if;
1847
1848 -- STEP 3 (B): Compute the aggregate bounds
1849
1850 if Others_Present then
1851 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1852
1853 else
1854 if Others_Allowed then
1855 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1856 else
1857 Aggr_Low := Index_Typ_Low;
1858 end if;
1859
1860 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1861 Check_Bound (Index_Base_High, Aggr_High);
1862 end if;
1863 end if;
1864
1865 -- STEP 4: Perform static aggregate checks and save the bounds
1866
1867 -- Check (A)
1868
1869 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1870 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1871
1872 -- Check (B)
1873
1874 if Others_Present and then Nb_Discrete_Choices > 0 then
1875 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1876 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1877 Choices_Low, Choices_High);
1878 Check_Bounds (Index_Base_Low, Index_Base_High,
1879 Choices_Low, Choices_High);
1880
1881 -- Check (C)
1882
1883 elsif Others_Present and then Nb_Elements > 0 then
1884 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1885 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1886 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1887
1888 end if;
1889
1890 if Raises_Constraint_Error (Aggr_Low)
1891 or else Raises_Constraint_Error (Aggr_High)
1892 then
1893 Set_Raises_Constraint_Error (N);
1894 end if;
1895
1896 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1897
1898 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1899 -- since the addition node returned by Add is not yet analyzed. Attach
1900 -- to tree and analyze first. Reset analyzed flag to insure it will get
1901 -- analyzed when it is a literal bound whose type must be properly
1902 -- set.
1903
1904 if Others_Present or else Nb_Discrete_Choices > 0 then
1905 Aggr_High := Duplicate_Subexpr (Aggr_High);
1906
1907 if Etype (Aggr_High) = Universal_Integer then
1908 Set_Analyzed (Aggr_High, False);
1909 end if;
1910 end if;
1911
1912 Set_Aggregate_Bounds
1913 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1914
1915 -- The bounds may contain expressions that must be inserted upwards.
1916 -- Attach them fully to the tree. After analysis, remove side effects
1917 -- from upper bound, if still needed.
1918
1919 Set_Parent (Aggregate_Bounds (N), N);
1920 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1921 Check_Unset_Reference (Aggregate_Bounds (N));
1922
1923 if not Others_Present and then Nb_Discrete_Choices = 0 then
1924 Set_High_Bound (Aggregate_Bounds (N),
1925 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1926 end if;
1927
1928 return Success;
1929 end Resolve_Array_Aggregate;
1930
1931 ---------------------------------
1932 -- Resolve_Extension_Aggregate --
1933 ---------------------------------
1934
1935 -- There are two cases to consider:
1936
1937 -- a) If the ancestor part is a type mark, the components needed are
1938 -- the difference between the components of the expected type and the
1939 -- components of the given type mark.
1940
1941 -- b) If the ancestor part is an expression, it must be unambiguous,
1942 -- and once we have its type we can also compute the needed components
1943 -- as in the previous case. In both cases, if the ancestor type is not
1944 -- the immediate ancestor, we have to build this ancestor recursively.
1945
1946 -- In both cases discriminants of the ancestor type do not play a
1947 -- role in the resolution of the needed components, because inherited
1948 -- discriminants cannot be used in a type extension. As a result we can
1949 -- compute independently the list of components of the ancestor type and
1950 -- of the expected type.
1951
1952 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1953 A : constant Node_Id := Ancestor_Part (N);
1954 A_Type : Entity_Id;
1955 I : Interp_Index;
1956 It : Interp;
1957
1958 function Valid_Ancestor_Type return Boolean;
1959 -- Verify that the type of the ancestor part is a non-private ancestor
1960 -- of the expected type.
1961
1962 -------------------------
1963 -- Valid_Ancestor_Type --
1964 -------------------------
1965
1966 function Valid_Ancestor_Type return Boolean is
1967 Imm_Type : Entity_Id;
1968
1969 begin
1970 Imm_Type := Base_Type (Typ);
1971 while Is_Derived_Type (Imm_Type)
1972 and then Etype (Imm_Type) /= Base_Type (A_Type)
1973 loop
1974 Imm_Type := Etype (Base_Type (Imm_Type));
1975 end loop;
1976
1977 if Etype (Imm_Type) /= Base_Type (A_Type) then
1978 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1979 return False;
1980 else
1981 return True;
1982 end if;
1983 end Valid_Ancestor_Type;
1984
1985 -- Start of processing for Resolve_Extension_Aggregate
1986
1987 begin
1988 Analyze (A);
1989
1990 if not Is_Tagged_Type (Typ) then
1991 Error_Msg_N ("type of extension aggregate must be tagged", N);
1992 return;
1993
1994 elsif Is_Limited_Type (Typ) then
1995
1996 -- Ada 0Y (AI-287): Limited aggregates are allowed
1997
1998 if Extensions_Allowed then
1999 null;
2000 else
2001 Error_Msg_N ("aggregate type cannot be limited", N);
2002 Explain_Limited_Type (Typ, N);
2003 return;
2004 end if;
2005
2006 elsif Is_Class_Wide_Type (Typ) then
2007 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2008 return;
2009 end if;
2010
2011 if Is_Entity_Name (A)
2012 and then Is_Type (Entity (A))
2013 then
2014 A_Type := Get_Full_View (Entity (A));
2015
2016 if Valid_Ancestor_Type then
2017 Set_Entity (A, A_Type);
2018 Set_Etype (A, A_Type);
2019
2020 Validate_Ancestor_Part (N);
2021 Resolve_Record_Aggregate (N, Typ);
2022 end if;
2023
2024 elsif Nkind (A) /= N_Aggregate then
2025 if Is_Overloaded (A) then
2026 A_Type := Any_Type;
2027 Get_First_Interp (A, I, It);
2028
2029 while Present (It.Typ) loop
2030
2031 if Is_Tagged_Type (It.Typ)
2032 and then not Is_Limited_Type (It.Typ)
2033 then
2034 if A_Type /= Any_Type then
2035 Error_Msg_N ("cannot resolve expression", A);
2036 return;
2037 else
2038 A_Type := It.Typ;
2039 end if;
2040 end if;
2041
2042 Get_Next_Interp (I, It);
2043 end loop;
2044
2045 if A_Type = Any_Type then
2046 Error_Msg_N
2047 ("ancestor part must be non-limited tagged type", A);
2048 return;
2049 end if;
2050
2051 else
2052 A_Type := Etype (A);
2053 end if;
2054
2055 if Valid_Ancestor_Type then
2056 Resolve (A, A_Type);
2057 Check_Unset_Reference (A);
2058 Check_Non_Static_Context (A);
2059
2060 if Is_Class_Wide_Type (Etype (A))
2061 and then Nkind (Original_Node (A)) = N_Function_Call
2062 then
2063 -- If the ancestor part is a dispatching call, it appears
2064 -- statically to be a legal ancestor, but it yields any
2065 -- member of the class, and it is not possible to determine
2066 -- whether it is an ancestor of the extension aggregate (much
2067 -- less which ancestor). It is not possible to determine the
2068 -- required components of the extension part.
2069
2070 Error_Msg_N ("ancestor part must be statically tagged", A);
2071 else
2072 Resolve_Record_Aggregate (N, Typ);
2073 end if;
2074 end if;
2075
2076 else
2077 Error_Msg_N (" No unique type for this aggregate", A);
2078 end if;
2079 end Resolve_Extension_Aggregate;
2080
2081 ------------------------------
2082 -- Resolve_Record_Aggregate --
2083 ------------------------------
2084
2085 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2086 New_Assoc_List : constant List_Id := New_List;
2087 New_Assoc : Node_Id;
2088 -- New_Assoc_List is the newly built list of N_Component_Association
2089 -- nodes. New_Assoc is one such N_Component_Association node in it.
2090 -- Please note that while Assoc and New_Assoc contain the same
2091 -- kind of nodes, they are used to iterate over two different
2092 -- N_Component_Association lists.
2093
2094 Others_Etype : Entity_Id := Empty;
2095 -- This variable is used to save the Etype of the last record component
2096 -- that takes its value from the others choice. Its purpose is:
2097 --
2098 -- (a) make sure the others choice is useful
2099 --
2100 -- (b) make sure the type of all the components whose value is
2101 -- subsumed by the others choice are the same.
2102 --
2103 -- This variable is updated as a side effect of function Get_Value
2104
2105 Mbox_Present : Boolean := False;
2106 Others_Mbox : Boolean := False;
2107 -- Ada 0Y (AI-287): Variables used in case of default initialization to
2108 -- provide a functionality similar to Others_Etype. Mbox_Present
2109 -- indicates that the component takes its default initialization;
2110 -- Others_Mbox indicates that at least one component takes its default
2111 -- initialization. Similar to Others_Etype, they are also updated as a
2112 -- side effect of function Get_Value.
2113
2114 procedure Add_Association
2115 (Component : Entity_Id;
2116 Expr : Node_Id;
2117 Box_Present : Boolean := False);
2118 -- Builds a new N_Component_Association node which associates
2119 -- Component to expression Expr and adds it to the new association
2120 -- list New_Assoc_List being built.
2121
2122 function Discr_Present (Discr : Entity_Id) return Boolean;
2123 -- If aggregate N is a regular aggregate this routine will return True.
2124 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2125 -- whose value may already have been specified by N's ancestor part,
2126 -- this routine checks whether this is indeed the case and if so
2127 -- returns False, signaling that no value for Discr should appear in the
2128 -- N's aggregate part. Also, in this case, the routine appends to
2129 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2130 -- part.
2131
2132 function Get_Value
2133 (Compon : Node_Id;
2134 From : List_Id;
2135 Consider_Others_Choice : Boolean := False)
2136 return Node_Id;
2137 -- Given a record component stored in parameter Compon, the
2138 -- following function returns its value as it appears in the list
2139 -- From, which is a list of N_Component_Association nodes. If no
2140 -- component association has a choice for the searched component,
2141 -- the value provided by the others choice is returned, if there
2142 -- is one and Consider_Others_Choice is set to true. Otherwise
2143 -- Empty is returned. If there is more than one component association
2144 -- giving a value for the searched record component, an error message
2145 -- is emitted and the first found value is returned.
2146 --
2147 -- If Consider_Others_Choice is set and the returned expression comes
2148 -- from the others choice, then Others_Etype is set as a side effect.
2149 -- An error message is emitted if the components taking their value
2150 -- from the others choice do not have same type.
2151
2152 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2153 -- Analyzes and resolves expression Expr against the Etype of the
2154 -- Component. This routine also applies all appropriate checks to Expr.
2155 -- It finally saves a Expr in the newly created association list that
2156 -- will be attached to the final record aggregate. Note that if the
2157 -- Parent pointer of Expr is not set then Expr was produced with a
2158 -- New_Copy_Tree or some such.
2159
2160 ---------------------
2161 -- Add_Association --
2162 ---------------------
2163
2164 procedure Add_Association
2165 (Component : Entity_Id;
2166 Expr : Node_Id;
2167 Box_Present : Boolean := False)
2168 is
2169 Choice_List : constant List_Id := New_List;
2170 New_Assoc : Node_Id;
2171
2172 begin
2173 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2174 New_Assoc :=
2175 Make_Component_Association (Sloc (Expr),
2176 Choices => Choice_List,
2177 Expression => Expr,
2178 Box_Present => Box_Present);
2179 Append (New_Assoc, New_Assoc_List);
2180 end Add_Association;
2181
2182 -------------------
2183 -- Discr_Present --
2184 -------------------
2185
2186 function Discr_Present (Discr : Entity_Id) return Boolean is
2187 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2188
2189 Loc : Source_Ptr;
2190
2191 Ancestor : Node_Id;
2192 Discr_Expr : Node_Id;
2193
2194 Ancestor_Typ : Entity_Id;
2195 Orig_Discr : Entity_Id;
2196 D : Entity_Id;
2197 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2198
2199 Ancestor_Is_Subtyp : Boolean;
2200
2201 begin
2202 if Regular_Aggr then
2203 return True;
2204 end if;
2205
2206 Ancestor := Ancestor_Part (N);
2207 Ancestor_Typ := Etype (Ancestor);
2208 Loc := Sloc (Ancestor);
2209
2210 Ancestor_Is_Subtyp :=
2211 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2212
2213 -- If the ancestor part has no discriminants clearly N's aggregate
2214 -- part must provide a value for Discr.
2215
2216 if not Has_Discriminants (Ancestor_Typ) then
2217 return True;
2218
2219 -- If the ancestor part is an unconstrained subtype mark then the
2220 -- Discr must be present in N's aggregate part.
2221
2222 elsif Ancestor_Is_Subtyp
2223 and then not Is_Constrained (Entity (Ancestor))
2224 then
2225 return True;
2226 end if;
2227
2228 -- Now look to see if Discr was specified in the ancestor part.
2229
2230 Orig_Discr := Original_Record_Component (Discr);
2231 D := First_Discriminant (Ancestor_Typ);
2232
2233 if Ancestor_Is_Subtyp then
2234 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2235 end if;
2236
2237 while Present (D) loop
2238 -- If Ancestor has already specified Disc value than
2239 -- insert its value in the final aggregate.
2240
2241 if Original_Record_Component (D) = Orig_Discr then
2242 if Ancestor_Is_Subtyp then
2243 Discr_Expr := New_Copy_Tree (Node (D_Val));
2244 else
2245 Discr_Expr :=
2246 Make_Selected_Component (Loc,
2247 Prefix => Duplicate_Subexpr (Ancestor),
2248 Selector_Name => New_Occurrence_Of (Discr, Loc));
2249 end if;
2250
2251 Resolve_Aggr_Expr (Discr_Expr, Discr);
2252 return False;
2253 end if;
2254
2255 Next_Discriminant (D);
2256
2257 if Ancestor_Is_Subtyp then
2258 Next_Elmt (D_Val);
2259 end if;
2260 end loop;
2261
2262 return True;
2263 end Discr_Present;
2264
2265 ---------------
2266 -- Get_Value --
2267 ---------------
2268
2269 function Get_Value
2270 (Compon : Node_Id;
2271 From : List_Id;
2272 Consider_Others_Choice : Boolean := False)
2273 return Node_Id
2274 is
2275 Assoc : Node_Id;
2276 Expr : Node_Id := Empty;
2277 Selector_Name : Node_Id;
2278
2279 procedure Check_Non_Limited_Type;
2280 -- Relax check to allow the default initialization of limited types.
2281 -- For example:
2282 -- record
2283 -- C : Lim := (..., others => <>);
2284 -- end record;
2285
2286 ----------------------------
2287 -- Check_Non_Limited_Type --
2288 ----------------------------
2289
2290 procedure Check_Non_Limited_Type is
2291 begin
2292 if Is_Limited_Type (Etype (Compon))
2293 and then Comes_From_Source (Compon)
2294 and then not In_Instance_Body
2295 then
2296 -- Ada 0Y (AI-287): Limited aggregates are allowed
2297
2298 if Extensions_Allowed
2299 and then Present (Expression (Assoc))
2300 and then Nkind (Expression (Assoc)) = N_Aggregate
2301 then
2302 null;
2303 else
2304 Error_Msg_N
2305 ("initialization not allowed for limited types", N);
2306 Explain_Limited_Type (Etype (Compon), Compon);
2307 end if;
2308
2309 end if;
2310 end Check_Non_Limited_Type;
2311
2312 -- Start of processing for Get_Value
2313
2314 begin
2315 Mbox_Present := False;
2316
2317 if Present (From) then
2318 Assoc := First (From);
2319 else
2320 return Empty;
2321 end if;
2322
2323 while Present (Assoc) loop
2324 Selector_Name := First (Choices (Assoc));
2325 while Present (Selector_Name) loop
2326 if Nkind (Selector_Name) = N_Others_Choice then
2327 if Consider_Others_Choice and then No (Expr) then
2328
2329 -- We need to duplicate the expression for each
2330 -- successive component covered by the others choice.
2331 -- This is redundant if the others_choice covers only
2332 -- one component (small optimization possible???), but
2333 -- indispensable otherwise, because each one must be
2334 -- expanded individually to preserve side-effects.
2335
2336 -- Ada 0Y (AI-287): In case of default initialization of
2337 -- components, we duplicate the corresponding default
2338 -- expression (from the record type declaration).
2339
2340 if Box_Present (Assoc) then
2341 Others_Mbox := True;
2342 Mbox_Present := True;
2343
2344 if Expander_Active then
2345 return New_Copy_Tree (Expression (Parent (Compon)));
2346 else
2347 return Expression (Parent (Compon));
2348 end if;
2349
2350 else
2351 Check_Non_Limited_Type;
2352
2353 if Present (Others_Etype) and then
2354 Base_Type (Others_Etype) /= Base_Type (Etype
2355 (Compon))
2356 then
2357 Error_Msg_N ("components in OTHERS choice must " &
2358 "have same type", Selector_Name);
2359 end if;
2360
2361 Others_Etype := Etype (Compon);
2362
2363 if Expander_Active then
2364 return New_Copy_Tree (Expression (Assoc));
2365 else
2366 return Expression (Assoc);
2367 end if;
2368 end if;
2369 end if;
2370
2371 elsif Chars (Compon) = Chars (Selector_Name) then
2372 if No (Expr) then
2373
2374 -- Ada 0Y (AI-231)
2375
2376 if Extensions_Allowed
2377 and then Present (Expression (Assoc))
2378 and then Nkind (Expression (Assoc)) = N_Null
2379 and then Can_Never_Be_Null (Compon)
2380 then
2381 Error_Msg_N
2382 ("(Ada 0Y) NULL not allowed in null-excluding " &
2383 "components", Expression (Assoc));
2384 end if;
2385
2386 -- We need to duplicate the expression when several
2387 -- components are grouped together with a "|" choice.
2388 -- For instance "filed1 | filed2 => Expr"
2389
2390 -- Ada 0Y (AI-287)
2391
2392 if Box_Present (Assoc) then
2393 Mbox_Present := True;
2394
2395 -- Duplicate the default expression of the component
2396 -- from the record type declaration
2397
2398 if Present (Next (Selector_Name)) then
2399 Expr := New_Copy_Tree
2400 (Expression (Parent (Compon)));
2401 else
2402 Expr := Expression (Parent (Compon));
2403 end if;
2404
2405 else
2406 Check_Non_Limited_Type;
2407
2408 if Present (Next (Selector_Name)) then
2409 Expr := New_Copy_Tree (Expression (Assoc));
2410 else
2411 Expr := Expression (Assoc);
2412 end if;
2413 end if;
2414
2415 Generate_Reference (Compon, Selector_Name);
2416
2417 else
2418 Error_Msg_NE
2419 ("more than one value supplied for &",
2420 Selector_Name, Compon);
2421
2422 end if;
2423 end if;
2424
2425 Next (Selector_Name);
2426 end loop;
2427
2428 Next (Assoc);
2429 end loop;
2430
2431 return Expr;
2432 end Get_Value;
2433
2434 -----------------------
2435 -- Resolve_Aggr_Expr --
2436 -----------------------
2437
2438 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2439 New_C : Entity_Id := Component;
2440 Expr_Type : Entity_Id := Empty;
2441
2442 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2443 -- If the expression is an aggregate (possibly qualified) then its
2444 -- expansion is delayed until the enclosing aggregate is expanded
2445 -- into assignments. In that case, do not generate checks on the
2446 -- expression, because they will be generated later, and will other-
2447 -- wise force a copy (to remove side-effects) that would leave a
2448 -- dynamic-sized aggregate in the code, something that gigi cannot
2449 -- handle.
2450
2451 Relocate : Boolean;
2452 -- Set to True if the resolved Expr node needs to be relocated
2453 -- when attached to the newly created association list. This node
2454 -- need not be relocated if its parent pointer is not set.
2455 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2456 -- if Relocate is True then we have analyzed the expression node
2457 -- in the original aggregate and hence it needs to be relocated
2458 -- when moved over the new association list.
2459
2460 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2461 Kind : constant Node_Kind := Nkind (Expr);
2462
2463 begin
2464 return ((Kind = N_Aggregate
2465 or else Kind = N_Extension_Aggregate)
2466 and then Present (Etype (Expr))
2467 and then Is_Record_Type (Etype (Expr))
2468 and then Expansion_Delayed (Expr))
2469
2470 or else (Kind = N_Qualified_Expression
2471 and then Has_Expansion_Delayed (Expression (Expr)));
2472 end Has_Expansion_Delayed;
2473
2474 -- Start of processing for Resolve_Aggr_Expr
2475
2476 begin
2477 -- If the type of the component is elementary or the type of the
2478 -- aggregate does not contain discriminants, use the type of the
2479 -- component to resolve Expr.
2480
2481 if Is_Elementary_Type (Etype (Component))
2482 or else not Has_Discriminants (Etype (N))
2483 then
2484 Expr_Type := Etype (Component);
2485
2486 -- Otherwise we have to pick up the new type of the component from
2487 -- the new costrained subtype of the aggregate. In fact components
2488 -- which are of a composite type might be constrained by a
2489 -- discriminant, and we want to resolve Expr against the subtype were
2490 -- all discriminant occurrences are replaced with their actual value.
2491
2492 else
2493 New_C := First_Component (Etype (N));
2494 while Present (New_C) loop
2495 if Chars (New_C) = Chars (Component) then
2496 Expr_Type := Etype (New_C);
2497 exit;
2498 end if;
2499
2500 Next_Component (New_C);
2501 end loop;
2502
2503 pragma Assert (Present (Expr_Type));
2504
2505 -- For each range in an array type where a discriminant has been
2506 -- replaced with the constraint, check that this range is within
2507 -- the range of the base type. This checks is done in the
2508 -- init proc for regular objects, but has to be done here for
2509 -- aggregates since no init proc is called for them.
2510
2511 if Is_Array_Type (Expr_Type) then
2512 declare
2513 Index : Node_Id := First_Index (Expr_Type);
2514 -- Range of the current constrained index in the array.
2515
2516 Orig_Index : Node_Id := First_Index (Etype (Component));
2517 -- Range corresponding to the range Index above in the
2518 -- original unconstrained record type. The bounds of this
2519 -- range may be governed by discriminants.
2520
2521 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2522 -- Range corresponding to the range Index above for the
2523 -- unconstrained array type. This range is needed to apply
2524 -- range checks.
2525
2526 begin
2527 while Present (Index) loop
2528 if Depends_On_Discriminant (Orig_Index) then
2529 Apply_Range_Check (Index, Etype (Unconstr_Index));
2530 end if;
2531
2532 Next_Index (Index);
2533 Next_Index (Orig_Index);
2534 Next_Index (Unconstr_Index);
2535 end loop;
2536 end;
2537 end if;
2538 end if;
2539
2540 -- If the Parent pointer of Expr is not set, Expr is an expression
2541 -- duplicated by New_Tree_Copy (this happens for record aggregates
2542 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2543 -- Such a duplicated expression must be attached to the tree
2544 -- before analysis and resolution to enforce the rule that a tree
2545 -- fragment should never be analyzed or resolved unless it is
2546 -- attached to the current compilation unit.
2547
2548 if No (Parent (Expr)) then
2549 Set_Parent (Expr, N);
2550 Relocate := False;
2551 else
2552 Relocate := True;
2553 end if;
2554
2555 Analyze_And_Resolve (Expr, Expr_Type);
2556 Check_Non_Static_Context (Expr);
2557 Check_Unset_Reference (Expr);
2558
2559 if not Has_Expansion_Delayed (Expr) then
2560 Aggregate_Constraint_Checks (Expr, Expr_Type);
2561 end if;
2562
2563 if Raises_Constraint_Error (Expr) then
2564 Set_Raises_Constraint_Error (N);
2565 end if;
2566
2567 if Relocate then
2568 Add_Association (New_C, Relocate_Node (Expr));
2569 else
2570 Add_Association (New_C, Expr);
2571 end if;
2572 end Resolve_Aggr_Expr;
2573
2574 -- Resolve_Record_Aggregate local variables
2575
2576 Assoc : Node_Id;
2577 -- N_Component_Association node belonging to the input aggregate N
2578
2579 Expr : Node_Id;
2580 Positional_Expr : Node_Id;
2581 Component : Entity_Id;
2582 Component_Elmt : Elmt_Id;
2583
2584 Components : constant Elist_Id := New_Elmt_List;
2585 -- Components is the list of the record components whose value must
2586 -- be provided in the aggregate. This list does include discriminants.
2587
2588 -- Start of processing for Resolve_Record_Aggregate
2589
2590 begin
2591 -- We may end up calling Duplicate_Subexpr on expressions that are
2592 -- attached to New_Assoc_List. For this reason we need to attach it
2593 -- to the tree by setting its parent pointer to N. This parent point
2594 -- will change in STEP 8 below.
2595
2596 Set_Parent (New_Assoc_List, N);
2597
2598 -- STEP 1: abstract type and null record verification
2599
2600 if Is_Abstract (Typ) then
2601 Error_Msg_N ("type of aggregate cannot be abstract", N);
2602 end if;
2603
2604 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2605 Set_Etype (N, Typ);
2606 return;
2607
2608 elsif Present (First_Entity (Typ))
2609 and then Null_Record_Present (N)
2610 and then not Is_Tagged_Type (Typ)
2611 then
2612 Error_Msg_N ("record aggregate cannot be null", N);
2613 return;
2614
2615 elsif No (First_Entity (Typ)) then
2616 Error_Msg_N ("record aggregate must be null", N);
2617 return;
2618 end if;
2619
2620 -- STEP 2: Verify aggregate structure
2621
2622 Step_2 : declare
2623 Selector_Name : Node_Id;
2624 Bad_Aggregate : Boolean := False;
2625
2626 begin
2627 if Present (Component_Associations (N)) then
2628 Assoc := First (Component_Associations (N));
2629 else
2630 Assoc := Empty;
2631 end if;
2632
2633 while Present (Assoc) loop
2634 Selector_Name := First (Choices (Assoc));
2635 while Present (Selector_Name) loop
2636 if Nkind (Selector_Name) = N_Identifier then
2637 null;
2638
2639 elsif Nkind (Selector_Name) = N_Others_Choice then
2640 if Selector_Name /= First (Choices (Assoc))
2641 or else Present (Next (Selector_Name))
2642 then
2643 Error_Msg_N ("OTHERS must appear alone in a choice list",
2644 Selector_Name);
2645 return;
2646
2647 elsif Present (Next (Assoc)) then
2648 Error_Msg_N ("OTHERS must appear last in an aggregate",
2649 Selector_Name);
2650 return;
2651 end if;
2652
2653 else
2654 Error_Msg_N
2655 ("selector name should be identifier or OTHERS",
2656 Selector_Name);
2657 Bad_Aggregate := True;
2658 end if;
2659
2660 Next (Selector_Name);
2661 end loop;
2662
2663 Next (Assoc);
2664 end loop;
2665
2666 if Bad_Aggregate then
2667 return;
2668 end if;
2669 end Step_2;
2670
2671 -- STEP 3: Find discriminant Values
2672
2673 Step_3 : declare
2674 Discrim : Entity_Id;
2675 Missing_Discriminants : Boolean := False;
2676
2677 begin
2678 if Present (Expressions (N)) then
2679 Positional_Expr := First (Expressions (N));
2680 else
2681 Positional_Expr := Empty;
2682 end if;
2683
2684 if Has_Discriminants (Typ) then
2685 Discrim := First_Discriminant (Typ);
2686 else
2687 Discrim := Empty;
2688 end if;
2689
2690 -- First find the discriminant values in the positional components
2691
2692 while Present (Discrim) and then Present (Positional_Expr) loop
2693 if Discr_Present (Discrim) then
2694 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2695
2696 -- Ada 0Y (AI-231)
2697
2698 if Extensions_Allowed
2699 and then Nkind (Positional_Expr) = N_Null
2700 and then Can_Never_Be_Null (Discrim)
2701 then
2702 Error_Msg_N
2703 ("(Ada 0Y) NULL not allowed in null-excluding components",
2704 Positional_Expr);
2705 end if;
2706
2707 Next (Positional_Expr);
2708 end if;
2709
2710 if Present (Get_Value (Discrim, Component_Associations (N))) then
2711 Error_Msg_NE
2712 ("more than one value supplied for discriminant&",
2713 N, Discrim);
2714 end if;
2715
2716 Next_Discriminant (Discrim);
2717 end loop;
2718
2719 -- Find remaining discriminant values, if any, among named components
2720
2721 while Present (Discrim) loop
2722 Expr := Get_Value (Discrim, Component_Associations (N), True);
2723
2724 if not Discr_Present (Discrim) then
2725 if Present (Expr) then
2726 Error_Msg_NE
2727 ("more than one value supplied for discriminant&",
2728 N, Discrim);
2729 end if;
2730
2731 elsif No (Expr) then
2732 Error_Msg_NE
2733 ("no value supplied for discriminant &", N, Discrim);
2734 Missing_Discriminants := True;
2735
2736 else
2737 Resolve_Aggr_Expr (Expr, Discrim);
2738 end if;
2739
2740 Next_Discriminant (Discrim);
2741 end loop;
2742
2743 if Missing_Discriminants then
2744 return;
2745 end if;
2746
2747 -- At this point and until the beginning of STEP 6, New_Assoc_List
2748 -- contains only the discriminants and their values.
2749
2750 end Step_3;
2751
2752 -- STEP 4: Set the Etype of the record aggregate
2753
2754 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2755 -- routine should really be exported in sem_util or some such and used
2756 -- in sem_ch3 and here rather than have a copy of the code which is a
2757 -- maintenance nightmare.
2758
2759 -- ??? Performace WARNING. The current implementation creates a new
2760 -- itype for all aggregates whose base type is discriminated.
2761 -- This means that for record aggregates nested inside an array
2762 -- aggregate we will create a new itype for each record aggregate
2763 -- if the array cmponent type has discriminants. For large aggregates
2764 -- this may be a problem. What should be done in this case is
2765 -- to reuse itypes as much as possible.
2766
2767 if Has_Discriminants (Typ) then
2768 Build_Constrained_Itype : declare
2769 Loc : constant Source_Ptr := Sloc (N);
2770 Indic : Node_Id;
2771 Subtyp_Decl : Node_Id;
2772 Def_Id : Entity_Id;
2773
2774 C : constant List_Id := New_List;
2775
2776 begin
2777 New_Assoc := First (New_Assoc_List);
2778 while Present (New_Assoc) loop
2779 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2780 Next (New_Assoc);
2781 end loop;
2782
2783 Indic :=
2784 Make_Subtype_Indication (Loc,
2785 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2786 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2787
2788 Def_Id := Create_Itype (Ekind (Typ), N);
2789
2790 Subtyp_Decl :=
2791 Make_Subtype_Declaration (Loc,
2792 Defining_Identifier => Def_Id,
2793 Subtype_Indication => Indic);
2794 Set_Parent (Subtyp_Decl, Parent (N));
2795
2796 -- Itypes must be analyzed with checks off (see itypes.ads).
2797
2798 Analyze (Subtyp_Decl, Suppress => All_Checks);
2799
2800 Set_Etype (N, Def_Id);
2801 Check_Static_Discriminated_Subtype
2802 (Def_Id, Expression (First (New_Assoc_List)));
2803 end Build_Constrained_Itype;
2804
2805 else
2806 Set_Etype (N, Typ);
2807 end if;
2808
2809 -- STEP 5: Get remaining components according to discriminant values
2810
2811 Step_5 : declare
2812 Record_Def : Node_Id;
2813 Parent_Typ : Entity_Id;
2814 Root_Typ : Entity_Id;
2815 Parent_Typ_List : Elist_Id;
2816 Parent_Elmt : Elmt_Id;
2817 Errors_Found : Boolean := False;
2818 Dnode : Node_Id;
2819
2820 begin
2821 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2822 Parent_Typ_List := New_Elmt_List;
2823
2824 -- If this is an extension aggregate, the component list must
2825 -- include all components that are not in the given ancestor
2826 -- type. Otherwise, the component list must include components
2827 -- of all ancestors, starting with the root.
2828
2829 if Nkind (N) = N_Extension_Aggregate then
2830 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2831 else
2832 Root_Typ := Root_Type (Typ);
2833
2834 if Nkind (Parent (Base_Type (Root_Typ)))
2835 = N_Private_Type_Declaration
2836 then
2837 Error_Msg_NE
2838 ("type of aggregate has private ancestor&!",
2839 N, Root_Typ);
2840 Error_Msg_N ("must use extension aggregate!", N);
2841 return;
2842 end if;
2843
2844 Dnode := Declaration_Node (Base_Type (Root_Typ));
2845
2846 -- If we don't get a full declaration, then we have some
2847 -- error which will get signalled later so skip this part.
2848 -- Otherwise, gather components of root that apply to the
2849 -- aggregate type. We use the base type in case there is an
2850 -- applicable stored constraint that renames the discriminants
2851 -- of the root.
2852
2853 if Nkind (Dnode) = N_Full_Type_Declaration then
2854 Record_Def := Type_Definition (Dnode);
2855 Gather_Components (Base_Type (Typ),
2856 Component_List (Record_Def),
2857 Governed_By => New_Assoc_List,
2858 Into => Components,
2859 Report_Errors => Errors_Found);
2860 end if;
2861 end if;
2862
2863 Parent_Typ := Base_Type (Typ);
2864 while Parent_Typ /= Root_Typ loop
2865
2866 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2867 Parent_Typ := Etype (Parent_Typ);
2868
2869 if Nkind (Parent (Base_Type (Parent_Typ))) =
2870 N_Private_Type_Declaration
2871 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2872 N_Private_Extension_Declaration
2873 then
2874 if Nkind (N) /= N_Extension_Aggregate then
2875 Error_Msg_NE
2876 ("type of aggregate has private ancestor&!",
2877 N, Parent_Typ);
2878 Error_Msg_N ("must use extension aggregate!", N);
2879 return;
2880
2881 elsif Parent_Typ /= Root_Typ then
2882 Error_Msg_NE
2883 ("ancestor part of aggregate must be private type&",
2884 Ancestor_Part (N), Parent_Typ);
2885 return;
2886 end if;
2887 end if;
2888 end loop;
2889
2890 -- Now collect components from all other ancestors.
2891
2892 Parent_Elmt := First_Elmt (Parent_Typ_List);
2893 while Present (Parent_Elmt) loop
2894 Parent_Typ := Node (Parent_Elmt);
2895 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2896 Gather_Components (Empty,
2897 Component_List (Record_Extension_Part (Record_Def)),
2898 Governed_By => New_Assoc_List,
2899 Into => Components,
2900 Report_Errors => Errors_Found);
2901
2902 Next_Elmt (Parent_Elmt);
2903 end loop;
2904
2905 else
2906 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2907
2908 if Null_Present (Record_Def) then
2909 null;
2910 else
2911 Gather_Components (Base_Type (Typ),
2912 Component_List (Record_Def),
2913 Governed_By => New_Assoc_List,
2914 Into => Components,
2915 Report_Errors => Errors_Found);
2916 end if;
2917 end if;
2918
2919 if Errors_Found then
2920 return;
2921 end if;
2922 end Step_5;
2923
2924 -- STEP 6: Find component Values
2925
2926 Component := Empty;
2927 Component_Elmt := First_Elmt (Components);
2928
2929 -- First scan the remaining positional associations in the aggregate.
2930 -- Remember that at this point Positional_Expr contains the current
2931 -- positional association if any is left after looking for discriminant
2932 -- values in step 3.
2933
2934 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2935 Component := Node (Component_Elmt);
2936 Resolve_Aggr_Expr (Positional_Expr, Component);
2937
2938 -- Ada 0Y (AI-231)
2939 if Extensions_Allowed
2940 and then Nkind (Positional_Expr) = N_Null
2941 and then Can_Never_Be_Null (Component)
2942 then
2943 Error_Msg_N
2944 ("(Ada 0Y) NULL not allowed in null-excluding components",
2945 Positional_Expr);
2946 end if;
2947
2948 if Present (Get_Value (Component, Component_Associations (N))) then
2949 Error_Msg_NE
2950 ("more than one value supplied for Component &", N, Component);
2951 end if;
2952
2953 Next (Positional_Expr);
2954 Next_Elmt (Component_Elmt);
2955 end loop;
2956
2957 if Present (Positional_Expr) then
2958 Error_Msg_N
2959 ("too many components for record aggregate", Positional_Expr);
2960 end if;
2961
2962 -- Now scan for the named arguments of the aggregate
2963
2964 while Present (Component_Elmt) loop
2965 Component := Node (Component_Elmt);
2966 Expr := Get_Value (Component, Component_Associations (N), True);
2967
2968 if Mbox_Present and then Is_Limited_Type (Etype (Component)) then
2969
2970 -- Ada 0Y (AI-287): In case of default initialization of a limited
2971 -- component we pass the limited component to the expander. The
2972 -- expander will generate calls to the corresponding initiali-
2973 -- zation subprograms.
2974
2975 Add_Association
2976 (Component => Component,
2977 Expr => Empty,
2978 Box_Present => True);
2979
2980 elsif No (Expr) then
2981 Error_Msg_NE ("no value supplied for component &!", N, Component);
2982 else
2983 Resolve_Aggr_Expr (Expr, Component);
2984 end if;
2985
2986 Next_Elmt (Component_Elmt);
2987 end loop;
2988
2989 -- STEP 7: check for invalid components + check type in choice list
2990
2991 Step_7 : declare
2992 Selectr : Node_Id;
2993 -- Selector name
2994
2995 Typech : Entity_Id;
2996 -- Type of first component in choice list
2997
2998 begin
2999 if Present (Component_Associations (N)) then
3000 Assoc := First (Component_Associations (N));
3001 else
3002 Assoc := Empty;
3003 end if;
3004
3005 Verification : while Present (Assoc) loop
3006 Selectr := First (Choices (Assoc));
3007 Typech := Empty;
3008
3009 if Nkind (Selectr) = N_Others_Choice then
3010
3011 -- Ada 0Y (AI-287): others choice may have expression or mbox
3012
3013 if No (Others_Etype)
3014 and then not Others_Mbox
3015 then
3016 Error_Msg_N
3017 ("OTHERS must represent at least one component", Selectr);
3018 end if;
3019
3020 exit Verification;
3021 end if;
3022
3023 while Present (Selectr) loop
3024 New_Assoc := First (New_Assoc_List);
3025 while Present (New_Assoc) loop
3026 Component := First (Choices (New_Assoc));
3027 exit when Chars (Selectr) = Chars (Component);
3028 Next (New_Assoc);
3029 end loop;
3030
3031 -- If no association, this is not a legal component of
3032 -- of the type in question, except if this is an internal
3033 -- component supplied by a previous expansion.
3034
3035 if No (New_Assoc) then
3036 if Box_Present (Parent (Selectr)) then
3037 null;
3038
3039 elsif Chars (Selectr) /= Name_uTag
3040 and then Chars (Selectr) /= Name_uParent
3041 and then Chars (Selectr) /= Name_uController
3042 then
3043 if not Has_Discriminants (Typ) then
3044 Error_Msg_Node_2 := Typ;
3045 Error_Msg_N
3046 ("& is not a component of}",
3047 Selectr);
3048 else
3049 Error_Msg_N
3050 ("& is not a component of the aggregate subtype",
3051 Selectr);
3052 end if;
3053
3054 Check_Misspelled_Component (Components, Selectr);
3055 end if;
3056
3057 elsif No (Typech) then
3058 Typech := Base_Type (Etype (Component));
3059
3060 elsif Typech /= Base_Type (Etype (Component)) then
3061 if not Box_Present (Parent (Selectr)) then
3062 Error_Msg_N
3063 ("components in choice list must have same type",
3064 Selectr);
3065 end if;
3066 end if;
3067
3068 Next (Selectr);
3069 end loop;
3070
3071 Next (Assoc);
3072 end loop Verification;
3073 end Step_7;
3074
3075 -- STEP 8: replace the original aggregate
3076
3077 Step_8 : declare
3078 New_Aggregate : constant Node_Id := New_Copy (N);
3079
3080 begin
3081 Set_Expressions (New_Aggregate, No_List);
3082 Set_Etype (New_Aggregate, Etype (N));
3083 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3084
3085 Rewrite (N, New_Aggregate);
3086 end Step_8;
3087 end Resolve_Record_Aggregate;
3088
3089 -----------------------------
3090 -- Check_Can_Never_Be_Null --
3091 -----------------------------
3092
3093 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id) is
3094 begin
3095 if Extensions_Allowed
3096 and then Nkind (Expr) = N_Null
3097 and then Can_Never_Be_Null (Etype (N))
3098 then
3099 Error_Msg_N
3100 ("(Ada 0Y) NULL not allowed in null-excluding components", Expr);
3101 end if;
3102 end Check_Can_Never_Be_Null;
3103
3104 ---------------------
3105 -- Sort_Case_Table --
3106 ---------------------
3107
3108 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3109 L : constant Int := Case_Table'First;
3110 U : constant Int := Case_Table'Last;
3111 K : Int;
3112 J : Int;
3113 T : Case_Bounds;
3114
3115 begin
3116 K := L;
3117
3118 while K /= U loop
3119 T := Case_Table (K + 1);
3120 J := K + 1;
3121
3122 while J /= L
3123 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3124 Expr_Value (T.Choice_Lo)
3125 loop
3126 Case_Table (J) := Case_Table (J - 1);
3127 J := J - 1;
3128 end loop;
3129
3130 Case_Table (J) := T;
3131 K := K + 1;
3132 end loop;
3133 end Sort_Case_Table;
3134
3135 end Sem_Aggr;