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