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