]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/sem_aggr.adb
sem_ch8.adb: Update comment.
[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-- --
22cb89b5 9-- Copyright (C) 1992-2010, 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
ed2233dc 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
ed2233dc 1446 Error_Msg_N
22cb89b5
AC
1447 ("\if single-component aggregate is intended,"
1448 & " write e.g. (1 ='> ...)", Expr);
9d0c3761 1449 end if;
996ae0b0
RK
1450 return Failure;
1451 end if;
1452 end if;
1453
0ab80019 1454 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
35b7fa6a
AC
1455 -- Required to check the null-exclusion attribute (if present).
1456 -- This value may be overridden later on.
1457
1458 Set_Etype (Expr, Etype (N));
1459
996ae0b0
RK
1460 Resolution_OK := Resolve_Array_Aggregate
1461 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1462
1463 -- Do not resolve the expressions of discrete or others choices
1464 -- unless the expression covers a single component, or the expander
1465 -- is inactive.
1466
1467 elsif Single_Elmt
1468 or else not Expander_Active
ca44152f 1469 or else In_Spec_Expression
996ae0b0
RK
1470 then
1471 Analyze_And_Resolve (Expr, Component_Typ);
ca44152f 1472 Check_Expr_OK_In_Limited_Aggregate (Expr);
996ae0b0
RK
1473 Check_Non_Static_Context (Expr);
1474 Aggregate_Constraint_Checks (Expr, Component_Typ);
fbf5a39b 1475 Check_Unset_Reference (Expr);
996ae0b0
RK
1476 end if;
1477
1478 if Raises_Constraint_Error (Expr)
1479 and then Nkind (Parent (Expr)) /= N_Component_Association
1480 then
1481 Set_Raises_Constraint_Error (N);
1482 end if;
1483
d79e621a
GD
1484 -- If the expression has been marked as requiring a range check,
1485 -- then generate it here.
1486
1487 if Do_Range_Check (Expr) then
1488 Set_Do_Range_Check (Expr, False);
1489 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1490 end if;
1491
996ae0b0
RK
1492 return Resolution_OK;
1493 end Resolve_Aggr_Expr;
1494
1495 -- Variables local to Resolve_Array_Aggregate
1496
1497 Assoc : Node_Id;
1498 Choice : Node_Id;
1499 Expr : Node_Id;
1500
f91e8020
GD
1501 Discard : Node_Id;
1502 pragma Warnings (Off, Discard);
996ae0b0
RK
1503
1504 Aggr_Low : Node_Id := Empty;
1505 Aggr_High : Node_Id := Empty;
c7ce71c2 1506 -- The actual low and high bounds of this sub-aggregate
996ae0b0
RK
1507
1508 Choices_Low : Node_Id := Empty;
1509 Choices_High : Node_Id := Empty;
1510 -- The lowest and highest discrete choices values for a named aggregate
1511
1512 Nb_Elements : Uint := Uint_0;
c7ce71c2 1513 -- The number of elements in a positional aggregate
996ae0b0
RK
1514
1515 Others_Present : Boolean := False;
1516
1517 Nb_Choices : Nat := 0;
1518 -- Contains the overall number of named choices in this sub-aggregate
1519
1520 Nb_Discrete_Choices : Nat := 0;
1521 -- The overall number of discrete choices (not counting others choice)
1522
1523 Case_Table_Size : Nat;
1524 -- Contains the size of the case table needed to sort aggregate choices
1525
1526 -- Start of processing for Resolve_Array_Aggregate
1527
1528 begin
6d2a1120
RD
1529 -- Ignore junk empty aggregate resulting from parser error
1530
1531 if No (Expressions (N))
1532 and then No (Component_Associations (N))
1533 and then not Null_Record_Present (N)
1534 then
1535 return False;
1536 end if;
1537
996ae0b0
RK
1538 -- STEP 1: make sure the aggregate is correctly formatted
1539
1540 if Present (Component_Associations (N)) then
1541 Assoc := First (Component_Associations (N));
1542 while Present (Assoc) loop
1543 Choice := First (Choices (Assoc));
1544 while Present (Choice) loop
1545 if Nkind (Choice) = N_Others_Choice then
1546 Others_Present := True;
1547
1548 if Choice /= First (Choices (Assoc))
1549 or else Present (Next (Choice))
1550 then
ed2233dc 1551 Error_Msg_N
996ae0b0
RK
1552 ("OTHERS must appear alone in a choice list", Choice);
1553 return Failure;
1554 end if;
1555
1556 if Present (Next (Assoc)) then
ed2233dc 1557 Error_Msg_N
996ae0b0
RK
1558 ("OTHERS must appear last in an aggregate", Choice);
1559 return Failure;
1560 end if;
1561
0ab80019 1562 if Ada_Version = Ada_83
996ae0b0 1563 and then Assoc /= First (Component_Associations (N))
f53f9dd7
RD
1564 and then Nkind_In (Parent (N), N_Assignment_Statement,
1565 N_Object_Declaration)
996ae0b0
RK
1566 then
1567 Error_Msg_N
1568 ("(Ada 83) illegal context for OTHERS choice", N);
1569 end if;
1570 end if;
1571
1572 Nb_Choices := Nb_Choices + 1;
1573 Next (Choice);
1574 end loop;
1575
1576 Next (Assoc);
1577 end loop;
1578 end if;
1579
1580 -- At this point we know that the others choice, if present, is by
1581 -- itself and appears last in the aggregate. Check if we have mixed
1582 -- positional and discrete associations (other than the others choice).
1583
1584 if Present (Expressions (N))
1585 and then (Nb_Choices > 1
1586 or else (Nb_Choices = 1 and then not Others_Present))
1587 then
1588 Error_Msg_N
1589 ("named association cannot follow positional association",
1590 First (Choices (First (Component_Associations (N)))));
1591 return Failure;
1592 end if;
1593
1594 -- Test for the validity of an others choice if present
1595
1596 if Others_Present and then not Others_Allowed then
1597 Error_Msg_N
1598 ("OTHERS choice not allowed here",
1599 First (Choices (First (Component_Associations (N)))));
1600 return Failure;
1601 end if;
1602
07fc65c4
GB
1603 -- Protect against cascaded errors
1604
1605 if Etype (Index_Typ) = Any_Type then
1606 return Failure;
1607 end if;
1608
996ae0b0
RK
1609 -- STEP 2: Process named components
1610
1611 if No (Expressions (N)) then
996ae0b0
RK
1612 if Others_Present then
1613 Case_Table_Size := Nb_Choices - 1;
1614 else
1615 Case_Table_Size := Nb_Choices;
1616 end if;
1617
1618 Step_2 : declare
1619 Low : Node_Id;
1620 High : Node_Id;
1621 -- Denote the lowest and highest values in an aggregate choice
1622
1623 Hi_Val : Uint;
1624 Lo_Val : Uint;
1625 -- High end of one range and Low end of the next. Should be
1626 -- contiguous if there is no hole in the list of values.
1627
1628 Missing_Values : Boolean;
1629 -- Set True if missing index values
1630
1631 S_Low : Node_Id := Empty;
1632 S_High : Node_Id := Empty;
1633 -- if a choice in an aggregate is a subtype indication these
1634 -- denote the lowest and highest values of the subtype
1635
1636 Table : Case_Table_Type (1 .. Case_Table_Size);
1637 -- Used to sort all the different choice values
1638
1639 Single_Choice : Boolean;
1640 -- Set to true every time there is a single discrete choice in a
1641 -- discrete association
1642
1643 Prev_Nb_Discrete_Choices : Nat;
b87971f3
AC
1644 -- Used to keep track of the number of discrete choices in the
1645 -- current association.
996ae0b0
RK
1646
1647 begin
ec53a6da 1648 -- STEP 2 (A): Check discrete choices validity
996ae0b0
RK
1649
1650 Assoc := First (Component_Associations (N));
1651 while Present (Assoc) loop
996ae0b0
RK
1652 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1653 Choice := First (Choices (Assoc));
1654 loop
1655 Analyze (Choice);
1656
1657 if Nkind (Choice) = N_Others_Choice then
1658 Single_Choice := False;
1659 exit;
1660
1661 -- Test for subtype mark without constraint
1662
1663 elsif Is_Entity_Name (Choice) and then
1664 Is_Type (Entity (Choice))
1665 then
1666 if Base_Type (Entity (Choice)) /= Index_Base then
1667 Error_Msg_N
1668 ("invalid subtype mark in aggregate choice",
1669 Choice);
1670 return Failure;
1671 end if;
1672
ca44152f
ES
1673 -- Case of subtype indication
1674
996ae0b0
RK
1675 elsif Nkind (Choice) = N_Subtype_Indication then
1676 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1677
1678 -- Does the subtype indication evaluation raise CE ?
1679
1680 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1681 Get_Index_Bounds (Choice, Low, High);
1682 Check_Bounds (S_Low, S_High, Low, High);
1683
ca44152f
ES
1684 -- Case of range or expression
1685
1686 else
996ae0b0 1687 Resolve (Choice, Index_Base);
fbf5a39b 1688 Check_Unset_Reference (Choice);
996ae0b0
RK
1689 Check_Non_Static_Context (Choice);
1690
1691 -- Do not range check a choice. This check is redundant
b87971f3
AC
1692 -- since this test is already done when we check that the
1693 -- bounds of the array aggregate are within range.
996ae0b0
RK
1694
1695 Set_Do_Range_Check (Choice, False);
1696 end if;
1697
1698 -- If we could not resolve the discrete choice stop here
1699
1700 if Etype (Choice) = Any_Type then
1701 return Failure;
1702
ec53a6da 1703 -- If the discrete choice raises CE get its original bounds
996ae0b0
RK
1704
1705 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1706 Set_Raises_Constraint_Error (N);
1707 Get_Index_Bounds (Original_Node (Choice), Low, High);
1708
1709 -- Otherwise get its bounds as usual
1710
1711 else
1712 Get_Index_Bounds (Choice, Low, High);
1713 end if;
1714
1715 if (Dynamic_Or_Null_Range (Low, High)
1716 or else (Nkind (Choice) = N_Subtype_Indication
1717 and then
1718 Dynamic_Or_Null_Range (S_Low, S_High)))
1719 and then Nb_Choices /= 1
1720 then
1721 Error_Msg_N
1722 ("dynamic or empty choice in aggregate " &
1723 "must be the only choice", Choice);
1724 return Failure;
1725 end if;
1726
1727 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1728 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1729 Table (Nb_Discrete_Choices).Choice_Hi := High;
1730
1731 Next (Choice);
1732
1733 if No (Choice) then
9b96e234 1734
996ae0b0
RK
1735 -- Check if we have a single discrete choice and whether
1736 -- this discrete choice specifies a single value.
1737
1738 Single_Choice :=
1739 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1740 and then (Low = High);
1741
1742 exit;
1743 end if;
1744 end loop;
1745
0ab80019 1746 -- Ada 2005 (AI-231)
2820d220 1747
ec53a6da 1748 if Ada_Version >= Ada_05
8133b9d1 1749 and then Known_Null (Expression (Assoc))
ec53a6da 1750 then
82c80734
RD
1751 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1752 end if;
2820d220 1753
0ab80019 1754 -- Ada 2005 (AI-287): In case of default initialized component
b87971f3 1755 -- we delay the resolution to the expansion phase.
c45b6ae0
AC
1756
1757 if Box_Present (Assoc) then
1758
b87971f3
AC
1759 -- Ada 2005 (AI-287): In case of default initialization of a
1760 -- component the expander will generate calls to the
1761 -- corresponding initialization subprogram.
c45b6ae0 1762
615cbd95 1763 null;
c45b6ae0
AC
1764
1765 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1766 Single_Elmt => Single_Choice)
996ae0b0
RK
1767 then
1768 return Failure;
4755cce9
JM
1769
1770 -- Check incorrect use of dynamically tagged expression
1771
1772 -- We differentiate here two cases because the expression may
1773 -- not be decorated. For example, the analysis and resolution
b87971f3
AC
1774 -- of the expression associated with the others choice will be
1775 -- done later with the full aggregate. In such case we
4755cce9
JM
1776 -- duplicate the expression tree to analyze the copy and
1777 -- perform the required check.
1778
1779 elsif not Present (Etype (Expression (Assoc))) then
1780 declare
1781 Save_Analysis : constant Boolean := Full_Analysis;
1782 Expr : constant Node_Id :=
1783 New_Copy_Tree (Expression (Assoc));
1784
1785 begin
1786 Expander_Mode_Save_And_Set (False);
1787 Full_Analysis := False;
1788 Analyze (Expr);
1789 Full_Analysis := Save_Analysis;
1790 Expander_Mode_Restore;
1791
1792 if Is_Tagged_Type (Etype (Expr)) then
1793 Check_Dynamically_Tagged_Expression
1794 (Expr => Expr,
1795 Typ => Component_Type (Etype (N)),
1796 Related_Nod => N);
1797 end if;
1798 end;
1799
1800 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
1801 Check_Dynamically_Tagged_Expression
1802 (Expr => Expression (Assoc),
1803 Typ => Component_Type (Etype (N)),
1804 Related_Nod => N);
996ae0b0
RK
1805 end if;
1806
1807 Next (Assoc);
1808 end loop;
1809
1810 -- If aggregate contains more than one choice then these must be
b87971f3 1811 -- static. Sort them and check that they are contiguous.
996ae0b0
RK
1812
1813 if Nb_Discrete_Choices > 1 then
1814 Sort_Case_Table (Table);
1815 Missing_Values := False;
1816
1817 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1818 if Expr_Value (Table (J).Choice_Hi) >=
1819 Expr_Value (Table (J + 1).Choice_Lo)
1820 then
1821 Error_Msg_N
1822 ("duplicate choice values in array aggregate",
1823 Table (J).Choice_Hi);
1824 return Failure;
1825
1826 elsif not Others_Present then
996ae0b0
RK
1827 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1828 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1829
1830 -- If missing values, output error messages
1831
1832 if Lo_Val - Hi_Val > 1 then
1833
1834 -- Header message if not first missing value
1835
1836 if not Missing_Values then
1837 Error_Msg_N
1838 ("missing index value(s) in array aggregate", N);
1839 Missing_Values := True;
1840 end if;
1841
1842 -- Output values of missing indexes
1843
1844 Lo_Val := Lo_Val - 1;
1845 Hi_Val := Hi_Val + 1;
1846
1847 -- Enumeration type case
1848
1849 if Is_Enumeration_Type (Index_Typ) then
1850 Error_Msg_Name_1 :=
1851 Chars
1852 (Get_Enum_Lit_From_Pos
1853 (Index_Typ, Hi_Val, Loc));
1854
1855 if Lo_Val = Hi_Val then
1856 Error_Msg_N ("\ %", N);
1857 else
1858 Error_Msg_Name_2 :=
1859 Chars
1860 (Get_Enum_Lit_From_Pos
1861 (Index_Typ, Lo_Val, Loc));
1862 Error_Msg_N ("\ % .. %", N);
1863 end if;
1864
1865 -- Integer types case
1866
1867 else
1868 Error_Msg_Uint_1 := Hi_Val;
1869
1870 if Lo_Val = Hi_Val then
1871 Error_Msg_N ("\ ^", N);
1872 else
1873 Error_Msg_Uint_2 := Lo_Val;
1874 Error_Msg_N ("\ ^ .. ^", N);
1875 end if;
1876 end if;
1877 end if;
1878 end if;
1879 end loop Outer;
1880
1881 if Missing_Values then
1882 Set_Etype (N, Any_Composite);
1883 return Failure;
1884 end if;
1885 end if;
1886
1887 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1888
1889 if Nb_Discrete_Choices > 0 then
1890 Choices_Low := Table (1).Choice_Lo;
1891 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1892 end if;
1893
ca44152f
ES
1894 -- If Others is present, then bounds of aggregate come from the
1895 -- index constraint (not the choices in the aggregate itself).
1896
996ae0b0
RK
1897 if Others_Present then
1898 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1899
ca44152f
ES
1900 -- No others clause present
1901
996ae0b0 1902 else
ca44152f
ES
1903 -- Special processing if others allowed and not present. This
1904 -- means that the bounds of the aggregate come from the index
1905 -- constraint (and the length must match).
1906
1907 if Others_Allowed then
1908 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1909
1910 -- If others allowed, and no others present, then the array
1911 -- should cover all index values. If it does not, we will
1912 -- get a length check warning, but there is two cases where
1913 -- an additional warning is useful:
1914
1915 -- If we have no positional components, and the length is
1916 -- wrong (which we can tell by others being allowed with
1917 -- missing components), and the index type is an enumeration
1918 -- type, then issue appropriate warnings about these missing
1919 -- components. They are only warnings, since the aggregate
1920 -- is fine, it's just the wrong length. We skip this check
1921 -- for standard character types (since there are no literals
1922 -- and it is too much trouble to concoct them), and also if
1923 -- any of the bounds have not-known-at-compile-time values.
1924
1925 -- Another case warranting a warning is when the length is
1926 -- right, but as above we have an index type that is an
1927 -- enumeration, and the bounds do not match. This is a
1928 -- case where dubious sliding is allowed and we generate
1929 -- a warning that the bounds do not match.
1930
1931 if No (Expressions (N))
1932 and then Nkind (Index) = N_Range
1933 and then Is_Enumeration_Type (Etype (Index))
1934 and then not Is_Standard_Character_Type (Etype (Index))
1935 and then Compile_Time_Known_Value (Aggr_Low)
1936 and then Compile_Time_Known_Value (Aggr_High)
1937 and then Compile_Time_Known_Value (Choices_Low)
1938 and then Compile_Time_Known_Value (Choices_High)
1939 then
d610088d 1940 -- If the bounds have semantic errors, do not attempt
ebd34478 1941 -- further resolution to prevent cascaded errors.
d610088d
AC
1942
1943 if Error_Posted (Choices_Low)
1944 or else Error_Posted (Choices_High)
1945 then
1946 return False;
1947 end if;
1948
ca44152f
ES
1949 declare
1950 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
1951 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
1952 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
1953 CHi : constant Node_Id := Expr_Value_E (Choices_High);
1954
1955 Ent : Entity_Id;
1956
1957 begin
ebd34478 1958 -- Warning case 1, missing values at start/end. Only
ca44152f
ES
1959 -- do the check if the number of entries is too small.
1960
1961 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
1962 <
1963 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
1964 then
1965 Error_Msg_N
1966 ("missing index value(s) in array aggregate?", N);
1967
1968 -- Output missing value(s) at start
1969
1970 if Chars (ALo) /= Chars (CLo) then
1971 Ent := Prev (CLo);
1972
1973 if Chars (ALo) = Chars (Ent) then
1974 Error_Msg_Name_1 := Chars (ALo);
1975 Error_Msg_N ("\ %?", N);
1976 else
1977 Error_Msg_Name_1 := Chars (ALo);
1978 Error_Msg_Name_2 := Chars (Ent);
1979 Error_Msg_N ("\ % .. %?", N);
1980 end if;
1981 end if;
1982
1983 -- Output missing value(s) at end
1984
1985 if Chars (AHi) /= Chars (CHi) then
1986 Ent := Next (CHi);
1987
1988 if Chars (AHi) = Chars (Ent) then
1989 Error_Msg_Name_1 := Chars (Ent);
1990 Error_Msg_N ("\ %?", N);
1991 else
1992 Error_Msg_Name_1 := Chars (Ent);
1993 Error_Msg_Name_2 := Chars (AHi);
1994 Error_Msg_N ("\ % .. %?", N);
1995 end if;
1996 end if;
1997
1998 -- Warning case 2, dubious sliding. The First_Subtype
1999 -- test distinguishes between a constrained type where
2000 -- sliding is not allowed (so we will get a warning
2001 -- later that Constraint_Error will be raised), and
2002 -- the unconstrained case where sliding is permitted.
2003
2004 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2005 =
2006 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2007 and then Chars (ALo) /= Chars (CLo)
2008 and then
2009 not Is_Constrained (First_Subtype (Etype (N)))
2010 then
2011 Error_Msg_N
2012 ("bounds of aggregate do not match target?", N);
2013 end if;
2014 end;
2015 end if;
2016 end if;
2017
f3d0f304 2018 -- If no others, aggregate bounds come from aggregate
ca44152f 2019
996ae0b0
RK
2020 Aggr_Low := Choices_Low;
2021 Aggr_High := Choices_High;
2022 end if;
2023 end Step_2;
2024
2025 -- STEP 3: Process positional components
2026
2027 else
2028 -- STEP 3 (A): Process positional elements
2029
2030 Expr := First (Expressions (N));
2031 Nb_Elements := Uint_0;
2032 while Present (Expr) loop
2033 Nb_Elements := Nb_Elements + 1;
2034
82c80734
RD
2035 -- Ada 2005 (AI-231)
2036
ec53a6da 2037 if Ada_Version >= Ada_05
8133b9d1 2038 and then Known_Null (Expr)
ec53a6da 2039 then
82c80734
RD
2040 Check_Can_Never_Be_Null (Etype (N), Expr);
2041 end if;
2820d220 2042
996ae0b0
RK
2043 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2044 return Failure;
2045 end if;
2046
4755cce9
JM
2047 -- Check incorrect use of dynamically tagged expression
2048
2049 if Is_Tagged_Type (Etype (Expr)) then
2050 Check_Dynamically_Tagged_Expression
2051 (Expr => Expr,
2052 Typ => Component_Type (Etype (N)),
2053 Related_Nod => N);
2054 end if;
2055
996ae0b0
RK
2056 Next (Expr);
2057 end loop;
2058
2059 if Others_Present then
2060 Assoc := Last (Component_Associations (N));
c45b6ae0 2061
82c80734
RD
2062 -- Ada 2005 (AI-231)
2063
ec53a6da 2064 if Ada_Version >= Ada_05
8133b9d1 2065 and then Known_Null (Assoc)
ec53a6da 2066 then
9b96e234 2067 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
82c80734 2068 end if;
2820d220 2069
ebd34478 2070 -- Ada 2005 (AI-287): In case of default initialized component,
c45b6ae0
AC
2071 -- we delay the resolution to the expansion phase.
2072
2073 if Box_Present (Assoc) then
2074
ebd34478
AC
2075 -- Ada 2005 (AI-287): In case of default initialization of a
2076 -- component the expander will generate calls to the
2077 -- corresponding initialization subprogram.
c45b6ae0 2078
615cbd95 2079 null;
c45b6ae0
AC
2080
2081 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2082 Single_Elmt => False)
996ae0b0
RK
2083 then
2084 return Failure;
4755cce9
JM
2085
2086 -- Check incorrect use of dynamically tagged expression. The
2087 -- expression of the others choice has not been resolved yet.
2088 -- In order to diagnose the semantic error we create a duplicate
2089 -- tree to analyze it and perform the check.
2090
2091 else
2092 declare
2093 Save_Analysis : constant Boolean := Full_Analysis;
2094 Expr : constant Node_Id :=
2095 New_Copy_Tree (Expression (Assoc));
2096
2097 begin
2098 Expander_Mode_Save_And_Set (False);
2099 Full_Analysis := False;
2100 Analyze (Expr);
2101 Full_Analysis := Save_Analysis;
2102 Expander_Mode_Restore;
2103
2104 if Is_Tagged_Type (Etype (Expr)) then
2105 Check_Dynamically_Tagged_Expression
2106 (Expr => Expr,
2107 Typ => Component_Type (Etype (N)),
2108 Related_Nod => N);
2109 end if;
2110 end;
996ae0b0
RK
2111 end if;
2112 end if;
2113
2114 -- STEP 3 (B): Compute the aggregate bounds
2115
2116 if Others_Present then
2117 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2118
2119 else
2120 if Others_Allowed then
f91e8020 2121 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
996ae0b0
RK
2122 else
2123 Aggr_Low := Index_Typ_Low;
2124 end if;
2125
2126 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2127 Check_Bound (Index_Base_High, Aggr_High);
2128 end if;
2129 end if;
2130
2131 -- STEP 4: Perform static aggregate checks and save the bounds
2132
2133 -- Check (A)
2134
2135 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2136 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2137
2138 -- Check (B)
2139
2140 if Others_Present and then Nb_Discrete_Choices > 0 then
2141 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2142 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2143 Choices_Low, Choices_High);
2144 Check_Bounds (Index_Base_Low, Index_Base_High,
2145 Choices_Low, Choices_High);
2146
2147 -- Check (C)
2148
2149 elsif Others_Present and then Nb_Elements > 0 then
2150 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2151 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2152 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
996ae0b0
RK
2153 end if;
2154
2155 if Raises_Constraint_Error (Aggr_Low)
2156 or else Raises_Constraint_Error (Aggr_High)
2157 then
2158 Set_Raises_Constraint_Error (N);
2159 end if;
2160
2161 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2162
2163 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2164 -- since the addition node returned by Add is not yet analyzed. Attach
ebd34478 2165 -- to tree and analyze first. Reset analyzed flag to ensure it will get
9b96e234 2166 -- analyzed when it is a literal bound whose type must be properly set.
996ae0b0
RK
2167
2168 if Others_Present or else Nb_Discrete_Choices > 0 then
2169 Aggr_High := Duplicate_Subexpr (Aggr_High);
2170
2171 if Etype (Aggr_High) = Universal_Integer then
2172 Set_Analyzed (Aggr_High, False);
2173 end if;
2174 end if;
2175
3d923671
AC
2176 -- If the aggregate already has bounds attached to it, it means this is
2177 -- a positional aggregate created as an optimization by
2178 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2179 -- bounds.
2180
2181 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
ebd34478 2182 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3d923671
AC
2183 Aggr_High := High_Bound (Aggregate_Bounds (N));
2184 end if;
2185
996ae0b0
RK
2186 Set_Aggregate_Bounds
2187 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2188
2189 -- The bounds may contain expressions that must be inserted upwards.
2190 -- Attach them fully to the tree. After analysis, remove side effects
2191 -- from upper bound, if still needed.
2192
2193 Set_Parent (Aggregate_Bounds (N), N);
2194 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
fbf5a39b 2195 Check_Unset_Reference (Aggregate_Bounds (N));
996ae0b0
RK
2196
2197 if not Others_Present and then Nb_Discrete_Choices = 0 then
2198 Set_High_Bound (Aggregate_Bounds (N),
2199 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2200 end if;
2201
2202 return Success;
2203 end Resolve_Array_Aggregate;
2204
2205 ---------------------------------
2206 -- Resolve_Extension_Aggregate --
2207 ---------------------------------
2208
2209 -- There are two cases to consider:
2210
ebd34478
AC
2211 -- a) If the ancestor part is a type mark, the components needed are the
2212 -- difference between the components of the expected type and the
996ae0b0
RK
2213 -- components of the given type mark.
2214
ebd34478
AC
2215 -- b) If the ancestor part is an expression, it must be unambiguous, and
2216 -- once we have its type we can also compute the needed components as in
2217 -- the previous case. In both cases, if the ancestor type is not the
2218 -- immediate ancestor, we have to build this ancestor recursively.
996ae0b0 2219
ebd34478
AC
2220 -- In both cases discriminants of the ancestor type do not play a role in
2221 -- the resolution of the needed components, because inherited discriminants
2222 -- cannot be used in a type extension. As a result we can compute
2223 -- independently the list of components of the ancestor type and of the
2224 -- expected type.
996ae0b0
RK
2225
2226 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
2227 A : constant Node_Id := Ancestor_Part (N);
2228 A_Type : Entity_Id;
2229 I : Interp_Index;
2230 It : Interp;
996ae0b0 2231
ca44152f
ES
2232 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2233 -- If the type is limited, verify that the ancestor part is a legal
ebd34478
AC
2234 -- expression (aggregate or function call, including 'Input)) that does
2235 -- not require a copy, as specified in 7.5(2).
ca44152f 2236
996ae0b0
RK
2237 function Valid_Ancestor_Type return Boolean;
2238 -- Verify that the type of the ancestor part is a non-private ancestor
1543e3ab 2239 -- of the expected type, which must be a type extension.
996ae0b0 2240
ca44152f
ES
2241 ----------------------------
2242 -- Valid_Limited_Ancestor --
2243 ----------------------------
2244
2245 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2246 begin
2247 if Is_Entity_Name (Anc)
2248 and then Is_Type (Entity (Anc))
2249 then
2250 return True;
2251
2252 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2253 return True;
2254
2255 elsif Nkind (Anc) = N_Attribute_Reference
2256 and then Attribute_Name (Anc) = Name_Input
2257 then
2258 return True;
2259
ebd34478 2260 elsif Nkind (Anc) = N_Qualified_Expression then
ca44152f
ES
2261 return Valid_Limited_Ancestor (Expression (Anc));
2262
2263 else
2264 return False;
2265 end if;
2266 end Valid_Limited_Ancestor;
2267
fbf5a39b
AC
2268 -------------------------
2269 -- Valid_Ancestor_Type --
2270 -------------------------
2271
996ae0b0
RK
2272 function Valid_Ancestor_Type return Boolean is
2273 Imm_Type : Entity_Id;
2274
2275 begin
2276 Imm_Type := Base_Type (Typ);
2af92e28
ES
2277 while Is_Derived_Type (Imm_Type) loop
2278 if Etype (Imm_Type) = Base_Type (A_Type) then
2279 return True;
2280
2281 -- The base type of the parent type may appear as a private
ebd34478
AC
2282 -- extension if it is declared as such in a parent unit of the
2283 -- current one. For consistency of the subsequent analysis use
2284 -- the partial view for the ancestor part.
2af92e28
ES
2285
2286 elsif Is_Private_Type (Etype (Imm_Type))
2287 and then Present (Full_View (Etype (Imm_Type)))
2288 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2289 then
2290 A_Type := Etype (Imm_Type);
2291 return True;
4519314c
AC
2292
2293 -- The parent type may be a private extension. The aggregate is
2294 -- legal if the type of the aggregate is an extension of it that
2295 -- is not a private extension.
2296
2297 elsif Is_Private_Type (A_Type)
2298 and then not Is_Private_Type (Imm_Type)
2299 and then Present (Full_View (A_Type))
2300 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2301 then
2302 return True;
2303
2af92e28
ES
2304 else
2305 Imm_Type := Etype (Base_Type (Imm_Type));
2306 end if;
996ae0b0
RK
2307 end loop;
2308
6d2a1120 2309 -- If previous loop did not find a proper ancestor, report error
2af92e28
ES
2310
2311 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2312 return False;
996ae0b0
RK
2313 end Valid_Ancestor_Type;
2314
2315 -- Start of processing for Resolve_Extension_Aggregate
2316
2317 begin
ebd34478
AC
2318 -- Analyze the ancestor part and account for the case where it is a
2319 -- parameterless function call.
70b70ce8 2320
996ae0b0 2321 Analyze (A);
70b70ce8 2322 Check_Parameterless_Call (A);
996ae0b0
RK
2323
2324 if not Is_Tagged_Type (Typ) then
2325 Error_Msg_N ("type of extension aggregate must be tagged", N);
2326 return;
2327
19f0526a
AC
2328 elsif Is_Limited_Type (Typ) then
2329
0ab80019 2330 -- Ada 2005 (AI-287): Limited aggregates are allowed
19f0526a 2331
0ab80019 2332 if Ada_Version < Ada_05 then
19f0526a
AC
2333 Error_Msg_N ("aggregate type cannot be limited", N);
2334 Explain_Limited_Type (Typ, N);
2335 return;
ca44152f
ES
2336
2337 elsif Valid_Limited_Ancestor (A) then
2338 null;
2339
2340 else
2341 Error_Msg_N
2342 ("limited ancestor part must be aggregate or function call", A);
19f0526a 2343 end if;
996ae0b0
RK
2344
2345 elsif Is_Class_Wide_Type (Typ) then
2346 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2347 return;
2348 end if;
2349
2350 if Is_Entity_Name (A)
2351 and then Is_Type (Entity (A))
2352 then
fbf5a39b 2353 A_Type := Get_Full_View (Entity (A));
996ae0b0
RK
2354
2355 if Valid_Ancestor_Type then
2356 Set_Entity (A, A_Type);
2357 Set_Etype (A, A_Type);
2358
2359 Validate_Ancestor_Part (N);
2360 Resolve_Record_Aggregate (N, Typ);
2361 end if;
2362
2363 elsif Nkind (A) /= N_Aggregate then
2364 if Is_Overloaded (A) then
2365 A_Type := Any_Type;
996ae0b0 2366
7f9747c6 2367 Get_First_Interp (A, I, It);
996ae0b0 2368 while Present (It.Typ) loop
70b70ce8
AC
2369 -- Only consider limited interpretations in the Ada 2005 case
2370
996ae0b0 2371 if Is_Tagged_Type (It.Typ)
70b70ce8
AC
2372 and then (Ada_Version >= Ada_05
2373 or else not Is_Limited_Type (It.Typ))
996ae0b0
RK
2374 then
2375 if A_Type /= Any_Type then
2376 Error_Msg_N ("cannot resolve expression", A);
2377 return;
2378 else
2379 A_Type := It.Typ;
2380 end if;
2381 end if;
2382
2383 Get_Next_Interp (I, It);
2384 end loop;
2385
2386 if A_Type = Any_Type then
70b70ce8
AC
2387 if Ada_Version >= Ada_05 then
2388 Error_Msg_N ("ancestor part must be of a tagged type", A);
2389 else
2390 Error_Msg_N
2391 ("ancestor part must be of a nonlimited tagged type", A);
2392 end if;
2393
996ae0b0
RK
2394 return;
2395 end if;
2396
2397 else
2398 A_Type := Etype (A);
2399 end if;
2400
2401 if Valid_Ancestor_Type then
2402 Resolve (A, A_Type);
fbf5a39b 2403 Check_Unset_Reference (A);
996ae0b0 2404 Check_Non_Static_Context (A);
fbf5a39b 2405
1646c947
GD
2406 -- The aggregate is illegal if the ancestor expression is a call
2407 -- to a function with a limited unconstrained result, unless the
2408 -- type of the aggregate is a null extension. This restriction
2409 -- was added in AI05-67 to simplify implementation.
2410
2411 if Nkind (A) = N_Function_Call
2412 and then Is_Limited_Type (A_Type)
2413 and then not Is_Null_Extension (Typ)
2414 and then not Is_Constrained (A_Type)
2415 then
2416 Error_Msg_N
2417 ("type of limited ancestor part must be constrained", A);
2418
2419 elsif Is_Class_Wide_Type (Etype (A))
fbf5a39b
AC
2420 and then Nkind (Original_Node (A)) = N_Function_Call
2421 then
2422 -- If the ancestor part is a dispatching call, it appears
ebd34478
AC
2423 -- statically to be a legal ancestor, but it yields any member
2424 -- of the class, and it is not possible to determine whether
2425 -- it is an ancestor of the extension aggregate (much less
2426 -- which ancestor). It is not possible to determine the
2427 -- components of the extension part.
fbf5a39b 2428
ebd34478
AC
2429 -- This check implements AI-306, which in fact was motivated by
2430 -- an AdaCore query to the ARG after this test was added.
82c80734 2431
fbf5a39b
AC
2432 Error_Msg_N ("ancestor part must be statically tagged", A);
2433 else
2434 Resolve_Record_Aggregate (N, Typ);
2435 end if;
996ae0b0
RK
2436 end if;
2437
2438 else
88b32fc3 2439 Error_Msg_N ("no unique type for this aggregate", A);
996ae0b0 2440 end if;
996ae0b0
RK
2441 end Resolve_Extension_Aggregate;
2442
2443 ------------------------------
2444 -- Resolve_Record_Aggregate --
2445 ------------------------------
2446
2447 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
9b96e234
JM
2448 Assoc : Node_Id;
2449 -- N_Component_Association node belonging to the input aggregate N
2450
2451 Expr : Node_Id;
2452 Positional_Expr : Node_Id;
2453 Component : Entity_Id;
2454 Component_Elmt : Elmt_Id;
2455
2456 Components : constant Elist_Id := New_Elmt_List;
ebd34478
AC
2457 -- Components is the list of the record components whose value must be
2458 -- provided in the aggregate. This list does include discriminants.
9b96e234 2459
fbf5a39b
AC
2460 New_Assoc_List : constant List_Id := New_List;
2461 New_Assoc : Node_Id;
996ae0b0
RK
2462 -- New_Assoc_List is the newly built list of N_Component_Association
2463 -- nodes. New_Assoc is one such N_Component_Association node in it.
ebd34478
AC
2464 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2465 -- they are used to iterate over two different N_Component_Association
2466 -- lists.
996ae0b0
RK
2467
2468 Others_Etype : Entity_Id := Empty;
2469 -- This variable is used to save the Etype of the last record component
2470 -- that takes its value from the others choice. Its purpose is:
2471 --
2472 -- (a) make sure the others choice is useful
2473 --
2474 -- (b) make sure the type of all the components whose value is
2475 -- subsumed by the others choice are the same.
2476 --
ebd34478 2477 -- This variable is updated as a side effect of function Get_Value.
996ae0b0 2478
9b96e234
JM
2479 Is_Box_Present : Boolean := False;
2480 Others_Box : Boolean := False;
0ab80019 2481 -- Ada 2005 (AI-287): Variables used in case of default initialization
9b96e234 2482 -- to provide a functionality similar to Others_Etype. Box_Present
19f0526a 2483 -- indicates that the component takes its default initialization;
9b96e234 2484 -- Others_Box indicates that at least one component takes its default
19f0526a
AC
2485 -- initialization. Similar to Others_Etype, they are also updated as a
2486 -- side effect of function Get_Value.
65356e64
AC
2487
2488 procedure Add_Association
9b96e234
JM
2489 (Component : Entity_Id;
2490 Expr : Node_Id;
107b023c 2491 Assoc_List : List_Id;
9b96e234 2492 Is_Box_Present : Boolean := False);
ebd34478
AC
2493 -- Builds a new N_Component_Association node which associates Component
2494 -- to expression Expr and adds it to the association list being built,
2495 -- either New_Assoc_List, or the association being built for an inner
2496 -- aggregate.
996ae0b0
RK
2497
2498 function Discr_Present (Discr : Entity_Id) return Boolean;
2499 -- If aggregate N is a regular aggregate this routine will return True.
fbf5a39b 2500 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
ebd34478
AC
2501 -- whose value may already have been specified by N's ancestor part.
2502 -- This routine checks whether this is indeed the case and if so returns
2503 -- False, signaling that no value for Discr should appear in N's
f104fca1 2504 -- aggregate part. Also, in this case, the routine appends to
2383acbd
AC
2505 -- New_Assoc_List the discriminant value specified in the ancestor part.
2506 --
f104fca1 2507 -- If the aggregate is in a context with expansion delayed, it will be
22cb89b5
AC
2508 -- reanalyzed. The inherited discriminant values must not be reinserted
2509 -- in the component list to prevent spurious errors, but they must be
f104fca1
AC
2510 -- present on first analysis to build the proper subtype indications.
2511 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
996ae0b0
RK
2512
2513 function Get_Value
2514 (Compon : Node_Id;
2515 From : List_Id;
2516 Consider_Others_Choice : Boolean := False)
2517 return Node_Id;
4519314c
AC
2518 -- Given a record component stored in parameter Compon, this function
2519 -- returns its value as it appears in the list From, which is a list
2520 -- of N_Component_Association nodes.
2383acbd 2521 --
ebd34478
AC
2522 -- If no component association has a choice for the searched component,
2523 -- the value provided by the others choice is returned, if there is one,
2524 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2525 -- returned. If there is more than one component association giving a
2526 -- value for the searched record component, an error message is emitted
2527 -- and the first found value is returned.
996ae0b0
RK
2528 --
2529 -- If Consider_Others_Choice is set and the returned expression comes
2530 -- from the others choice, then Others_Etype is set as a side effect.
ebd34478
AC
2531 -- An error message is emitted if the components taking their value from
2532 -- the others choice do not have same type.
996ae0b0
RK
2533
2534 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2535 -- Analyzes and resolves expression Expr against the Etype of the
638e383e 2536 -- Component. This routine also applies all appropriate checks to Expr.
996ae0b0
RK
2537 -- It finally saves a Expr in the newly created association list that
2538 -- will be attached to the final record aggregate. Note that if the
2539 -- Parent pointer of Expr is not set then Expr was produced with a
fbf5a39b 2540 -- New_Copy_Tree or some such.
996ae0b0
RK
2541
2542 ---------------------
2543 -- Add_Association --
2544 ---------------------
2545
65356e64 2546 procedure Add_Association
9b96e234
JM
2547 (Component : Entity_Id;
2548 Expr : Node_Id;
107b023c 2549 Assoc_List : List_Id;
9b96e234 2550 Is_Box_Present : Boolean := False)
65356e64 2551 is
fbf5a39b 2552 Choice_List : constant List_Id := New_List;
996ae0b0 2553 New_Assoc : Node_Id;
996ae0b0
RK
2554
2555 begin
2556 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2557 New_Assoc :=
2558 Make_Component_Association (Sloc (Expr),
65356e64
AC
2559 Choices => Choice_List,
2560 Expression => Expr,
9b96e234 2561 Box_Present => Is_Box_Present);
107b023c 2562 Append (New_Assoc, Assoc_List);
996ae0b0
RK
2563 end Add_Association;
2564
2565 -------------------
2566 -- Discr_Present --
2567 -------------------
2568
2569 function Discr_Present (Discr : Entity_Id) return Boolean is
fbf5a39b
AC
2570 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2571
996ae0b0
RK
2572 Loc : Source_Ptr;
2573
2574 Ancestor : Node_Id;
f104fca1 2575 Comp_Assoc : Node_Id;
996ae0b0
RK
2576 Discr_Expr : Node_Id;
2577
2578 Ancestor_Typ : Entity_Id;
2579 Orig_Discr : Entity_Id;
2580 D : Entity_Id;
2581 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2582
2583 Ancestor_Is_Subtyp : Boolean;
2584
2585 begin
2586 if Regular_Aggr then
2587 return True;
2588 end if;
2589
f104fca1
AC
2590 -- Check whether inherited discriminant values have already been
2591 -- inserted in the aggregate. This will be the case if we are
2592 -- re-analyzing an aggregate whose expansion was delayed.
2593
2594 if Present (Component_Associations (N)) then
2595 Comp_Assoc := First (Component_Associations (N));
2596 while Present (Comp_Assoc) loop
2597 if Inherited_Discriminant (Comp_Assoc) then
2598 return True;
2599 end if;
2383acbd 2600
f104fca1
AC
2601 Next (Comp_Assoc);
2602 end loop;
2603 end if;
2604
996ae0b0
RK
2605 Ancestor := Ancestor_Part (N);
2606 Ancestor_Typ := Etype (Ancestor);
2607 Loc := Sloc (Ancestor);
2608
5987e59c
AC
2609 -- For a private type with unknown discriminants, use the underlying
2610 -- record view if it is available.
9013065b
AC
2611
2612 if Has_Unknown_Discriminants (Ancestor_Typ)
2613 and then Present (Full_View (Ancestor_Typ))
2614 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
2615 then
2616 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
2617 end if;
2618
996ae0b0
RK
2619 Ancestor_Is_Subtyp :=
2620 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2621
2622 -- If the ancestor part has no discriminants clearly N's aggregate
2623 -- part must provide a value for Discr.
2624
2625 if not Has_Discriminants (Ancestor_Typ) then
2626 return True;
2627
2628 -- If the ancestor part is an unconstrained subtype mark then the
2629 -- Discr must be present in N's aggregate part.
2630
2631 elsif Ancestor_Is_Subtyp
2632 and then not Is_Constrained (Entity (Ancestor))
2633 then
2634 return True;
2635 end if;
2636
ec53a6da 2637 -- Now look to see if Discr was specified in the ancestor part
996ae0b0
RK
2638
2639 if Ancestor_Is_Subtyp then
2640 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2641 end if;
2642
ec53a6da
JM
2643 Orig_Discr := Original_Record_Component (Discr);
2644
2645 D := First_Discriminant (Ancestor_Typ);
996ae0b0 2646 while Present (D) loop
ec53a6da 2647
ebd34478 2648 -- If Ancestor has already specified Disc value then insert its
ec53a6da 2649 -- value in the final aggregate.
996ae0b0
RK
2650
2651 if Original_Record_Component (D) = Orig_Discr then
2652 if Ancestor_Is_Subtyp then
2653 Discr_Expr := New_Copy_Tree (Node (D_Val));
2654 else
2655 Discr_Expr :=
2656 Make_Selected_Component (Loc,
2657 Prefix => Duplicate_Subexpr (Ancestor),
2658 Selector_Name => New_Occurrence_Of (Discr, Loc));
2659 end if;
2660
2661 Resolve_Aggr_Expr (Discr_Expr, Discr);
f104fca1 2662 Set_Inherited_Discriminant (Last (New_Assoc_List));
996ae0b0
RK
2663 return False;
2664 end if;
2665
2666 Next_Discriminant (D);
2667
2668 if Ancestor_Is_Subtyp then
2669 Next_Elmt (D_Val);
2670 end if;
2671 end loop;
2672
2673 return True;
2674 end Discr_Present;
2675
2676 ---------------
2677 -- Get_Value --
2678 ---------------
2679
2680 function Get_Value
2681 (Compon : Node_Id;
2682 From : List_Id;
2683 Consider_Others_Choice : Boolean := False)
2684 return Node_Id
2685 is
2686 Assoc : Node_Id;
2687 Expr : Node_Id := Empty;
2688 Selector_Name : Node_Id;
2689
2690 begin
9b96e234 2691 Is_Box_Present := False;
65356e64 2692
996ae0b0
RK
2693 if Present (From) then
2694 Assoc := First (From);
2695 else
2696 return Empty;
2697 end if;
2698
2699 while Present (Assoc) loop
2700 Selector_Name := First (Choices (Assoc));
2701 while Present (Selector_Name) loop
2702 if Nkind (Selector_Name) = N_Others_Choice then
2703 if Consider_Others_Choice and then No (Expr) then
996ae0b0
RK
2704
2705 -- We need to duplicate the expression for each
2706 -- successive component covered by the others choice.
fbf5a39b
AC
2707 -- This is redundant if the others_choice covers only
2708 -- one component (small optimization possible???), but
2709 -- indispensable otherwise, because each one must be
2710 -- expanded individually to preserve side-effects.
996ae0b0 2711
0ab80019
AC
2712 -- Ada 2005 (AI-287): In case of default initialization
2713 -- of components, we duplicate the corresponding default
88b32fc3
BD
2714 -- expression (from the record type declaration). The
2715 -- copy must carry the sloc of the association (not the
2716 -- original expression) to prevent spurious elaboration
2717 -- checks when the default includes function calls.
19f0526a 2718
65356e64 2719 if Box_Present (Assoc) then
9b96e234
JM
2720 Others_Box := True;
2721 Is_Box_Present := True;
65356e64
AC
2722
2723 if Expander_Active then
88b32fc3
BD
2724 return
2725 New_Copy_Tree
2726 (Expression (Parent (Compon)),
2727 New_Sloc => Sloc (Assoc));
65356e64
AC
2728 else
2729 return Expression (Parent (Compon));
2730 end if;
65356e64 2731
d05ef0ab 2732 else
65356e64
AC
2733 if Present (Others_Etype) and then
2734 Base_Type (Others_Etype) /= Base_Type (Etype
2735 (Compon))
2736 then
2737 Error_Msg_N ("components in OTHERS choice must " &
2738 "have same type", Selector_Name);
2739 end if;
2740
2741 Others_Etype := Etype (Compon);
2742
2743 if Expander_Active then
2744 return New_Copy_Tree (Expression (Assoc));
2745 else
2746 return Expression (Assoc);
2747 end if;
996ae0b0
RK
2748 end if;
2749 end if;
2750
2751 elsif Chars (Compon) = Chars (Selector_Name) then
2752 if No (Expr) then
fbf5a39b 2753
0ab80019 2754 -- Ada 2005 (AI-231)
2820d220 2755
0ab80019 2756 if Ada_Version >= Ada_05
8133b9d1 2757 and then Known_Null (Expression (Assoc))
2820d220 2758 then
82c80734 2759 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2820d220
AC
2760 end if;
2761
996ae0b0
RK
2762 -- We need to duplicate the expression when several
2763 -- components are grouped together with a "|" choice.
2764 -- For instance "filed1 | filed2 => Expr"
2765
0ab80019 2766 -- Ada 2005 (AI-287)
2820d220 2767
65356e64 2768 if Box_Present (Assoc) then
9b96e234 2769 Is_Box_Present := True;
65356e64
AC
2770
2771 -- Duplicate the default expression of the component
c7ce71c2
ES
2772 -- from the record type declaration, so a new copy
2773 -- can be attached to the association.
65356e64 2774
c7ce71c2
ES
2775 -- Note that we always copy the default expression,
2776 -- even when the association has a single choice, in
2777 -- order to create a proper association for the
2778 -- expanded aggregate.
2779
2780 Expr := New_Copy_Tree (Expression (Parent (Compon)));
65356e64 2781
d05ef0ab 2782 else
65356e64
AC
2783 if Present (Next (Selector_Name)) then
2784 Expr := New_Copy_Tree (Expression (Assoc));
2785 else
2786 Expr := Expression (Assoc);
2787 end if;
996ae0b0
RK
2788 end if;
2789
55603e5e 2790 Generate_Reference (Compon, Selector_Name, 'm');
fbf5a39b 2791
996ae0b0
RK
2792 else
2793 Error_Msg_NE
2794 ("more than one value supplied for &",
2795 Selector_Name, Compon);
2796
2797 end if;
2798 end if;
2799
2800 Next (Selector_Name);
2801 end loop;
2802
2803 Next (Assoc);
2804 end loop;
2805
2806 return Expr;
2807 end Get_Value;
2808
2809 -----------------------
2810 -- Resolve_Aggr_Expr --
2811 -----------------------
2812
2813 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2814 New_C : Entity_Id := Component;
2815 Expr_Type : Entity_Id := Empty;
2816
2817 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2818 -- If the expression is an aggregate (possibly qualified) then its
2819 -- expansion is delayed until the enclosing aggregate is expanded
2820 -- into assignments. In that case, do not generate checks on the
2821 -- expression, because they will be generated later, and will other-
2822 -- wise force a copy (to remove side-effects) that would leave a
2823 -- dynamic-sized aggregate in the code, something that gigi cannot
2824 -- handle.
2825
2826 Relocate : Boolean;
2827 -- Set to True if the resolved Expr node needs to be relocated
2828 -- when attached to the newly created association list. This node
2829 -- need not be relocated if its parent pointer is not set.
2830 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2831 -- if Relocate is True then we have analyzed the expression node
2832 -- in the original aggregate and hence it needs to be relocated
2833 -- when moved over the new association list.
2834
2835 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2836 Kind : constant Node_Kind := Nkind (Expr);
996ae0b0 2837 begin
f53f9dd7 2838 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
996ae0b0
RK
2839 and then Present (Etype (Expr))
2840 and then Is_Record_Type (Etype (Expr))
2841 and then Expansion_Delayed (Expr))
996ae0b0
RK
2842 or else (Kind = N_Qualified_Expression
2843 and then Has_Expansion_Delayed (Expression (Expr)));
2844 end Has_Expansion_Delayed;
2845
2846 -- Start of processing for Resolve_Aggr_Expr
2847
2848 begin
2849 -- If the type of the component is elementary or the type of the
2850 -- aggregate does not contain discriminants, use the type of the
2851 -- component to resolve Expr.
2852
2853 if Is_Elementary_Type (Etype (Component))
2854 or else not Has_Discriminants (Etype (N))
2855 then
2856 Expr_Type := Etype (Component);
2857
2858 -- Otherwise we have to pick up the new type of the component from
12a13f01 2859 -- the new constrained subtype of the aggregate. In fact components
996ae0b0
RK
2860 -- which are of a composite type might be constrained by a
2861 -- discriminant, and we want to resolve Expr against the subtype were
2862 -- all discriminant occurrences are replaced with their actual value.
2863
2864 else
2865 New_C := First_Component (Etype (N));
2866 while Present (New_C) loop
2867 if Chars (New_C) = Chars (Component) then
2868 Expr_Type := Etype (New_C);
2869 exit;
2870 end if;
2871
2872 Next_Component (New_C);
2873 end loop;
2874
2875 pragma Assert (Present (Expr_Type));
2876
2877 -- For each range in an array type where a discriminant has been
2878 -- replaced with the constraint, check that this range is within
ec53a6da
JM
2879 -- the range of the base type. This checks is done in the init
2880 -- proc for regular objects, but has to be done here for
fbf5a39b 2881 -- aggregates since no init proc is called for them.
996ae0b0
RK
2882
2883 if Is_Array_Type (Expr_Type) then
2884 declare
7f9747c6 2885 Index : Node_Id;
ec53a6da 2886 -- Range of the current constrained index in the array
996ae0b0 2887
ec53a6da 2888 Orig_Index : Node_Id := First_Index (Etype (Component));
996ae0b0
RK
2889 -- Range corresponding to the range Index above in the
2890 -- original unconstrained record type. The bounds of this
2891 -- range may be governed by discriminants.
2892
2893 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2894 -- Range corresponding to the range Index above for the
2895 -- unconstrained array type. This range is needed to apply
2896 -- range checks.
2897
2898 begin
7f9747c6 2899 Index := First_Index (Expr_Type);
996ae0b0
RK
2900 while Present (Index) loop
2901 if Depends_On_Discriminant (Orig_Index) then
2902 Apply_Range_Check (Index, Etype (Unconstr_Index));
2903 end if;
2904
2905 Next_Index (Index);
2906 Next_Index (Orig_Index);
2907 Next_Index (Unconstr_Index);
2908 end loop;
2909 end;
2910 end if;
2911 end if;
2912
2913 -- If the Parent pointer of Expr is not set, Expr is an expression
2914 -- duplicated by New_Tree_Copy (this happens for record aggregates
2915 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2916 -- Such a duplicated expression must be attached to the tree
2917 -- before analysis and resolution to enforce the rule that a tree
2918 -- fragment should never be analyzed or resolved unless it is
2919 -- attached to the current compilation unit.
2920
2921 if No (Parent (Expr)) then
2922 Set_Parent (Expr, N);
2923 Relocate := False;
2924 else
2925 Relocate := True;
2926 end if;
2927
2928 Analyze_And_Resolve (Expr, Expr_Type);
ca44152f 2929 Check_Expr_OK_In_Limited_Aggregate (Expr);
996ae0b0 2930 Check_Non_Static_Context (Expr);
fbf5a39b 2931 Check_Unset_Reference (Expr);
996ae0b0 2932
0c020dde
AC
2933 -- Check wrong use of class-wide types
2934
7b4db06c 2935 if Is_Class_Wide_Type (Etype (Expr)) then
0c020dde
AC
2936 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
2937 end if;
2938
996ae0b0
RK
2939 if not Has_Expansion_Delayed (Expr) then
2940 Aggregate_Constraint_Checks (Expr, Expr_Type);
2941 end if;
2942
2943 if Raises_Constraint_Error (Expr) then
2944 Set_Raises_Constraint_Error (N);
2945 end if;
2946
d79e621a
GD
2947 -- If the expression has been marked as requiring a range check,
2948 -- then generate it here.
2949
2950 if Do_Range_Check (Expr) then
2951 Set_Do_Range_Check (Expr, False);
2952 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
2953 end if;
2954
996ae0b0 2955 if Relocate then
107b023c 2956 Add_Association (New_C, Relocate_Node (Expr), New_Assoc_List);
996ae0b0 2957 else
107b023c 2958 Add_Association (New_C, Expr, New_Assoc_List);
996ae0b0 2959 end if;
996ae0b0
RK
2960 end Resolve_Aggr_Expr;
2961
996ae0b0
RK
2962 -- Start of processing for Resolve_Record_Aggregate
2963
2964 begin
2965 -- We may end up calling Duplicate_Subexpr on expressions that are
2966 -- attached to New_Assoc_List. For this reason we need to attach it
2967 -- to the tree by setting its parent pointer to N. This parent point
2968 -- will change in STEP 8 below.
2969
2970 Set_Parent (New_Assoc_List, N);
2971
2972 -- STEP 1: abstract type and null record verification
2973
aad93b55 2974 if Is_Abstract_Type (Typ) then
996ae0b0
RK
2975 Error_Msg_N ("type of aggregate cannot be abstract", N);
2976 end if;
2977
2978 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2979 Set_Etype (N, Typ);
2980 return;
2981
2982 elsif Present (First_Entity (Typ))
2983 and then Null_Record_Present (N)
2984 and then not Is_Tagged_Type (Typ)
2985 then
2986 Error_Msg_N ("record aggregate cannot be null", N);
2987 return;
2988
eff332d9
GD
2989 -- If the type has no components, then the aggregate should either
2990 -- have "null record", or in Ada 2005 it could instead have a single
2991 -- component association given by "others => <>". For Ada 95 we flag
2992 -- an error at this point, but for Ada 2005 we proceed with checking
2993 -- the associations below, which will catch the case where it's not
2994 -- an aggregate with "others => <>". Note that the legality of a <>
2995 -- aggregate for a null record type was established by AI05-016.
2996
2997 elsif No (First_Entity (Typ))
2998 and then Ada_Version < Ada_05
2999 then
996ae0b0
RK
3000 Error_Msg_N ("record aggregate must be null", N);
3001 return;
3002 end if;
3003
3004 -- STEP 2: Verify aggregate structure
3005
3006 Step_2 : declare
3007 Selector_Name : Node_Id;
3008 Bad_Aggregate : Boolean := False;
3009
3010 begin
3011 if Present (Component_Associations (N)) then
3012 Assoc := First (Component_Associations (N));
3013 else
3014 Assoc := Empty;
3015 end if;
3016
3017 while Present (Assoc) loop
3018 Selector_Name := First (Choices (Assoc));
3019 while Present (Selector_Name) loop
3020 if Nkind (Selector_Name) = N_Identifier then
3021 null;
3022
3023 elsif Nkind (Selector_Name) = N_Others_Choice then
3024 if Selector_Name /= First (Choices (Assoc))
3025 or else Present (Next (Selector_Name))
3026 then
ed2233dc 3027 Error_Msg_N
22cb89b5
AC
3028 ("OTHERS must appear alone in a choice list",
3029 Selector_Name);
996ae0b0
RK
3030 return;
3031
3032 elsif Present (Next (Assoc)) then
ed2233dc 3033 Error_Msg_N
22cb89b5
AC
3034 ("OTHERS must appear last in an aggregate",
3035 Selector_Name);
996ae0b0 3036 return;
1ab9541b
ES
3037
3038 -- (Ada2005): If this is an association with a box,
3039 -- indicate that the association need not represent
3040 -- any component.
3041
3042 elsif Box_Present (Assoc) then
3043 Others_Box := True;
996ae0b0
RK
3044 end if;
3045
3046 else
3047 Error_Msg_N
3048 ("selector name should be identifier or OTHERS",
3049 Selector_Name);
3050 Bad_Aggregate := True;
3051 end if;
3052
3053 Next (Selector_Name);
3054 end loop;
3055
3056 Next (Assoc);
3057 end loop;
3058
3059 if Bad_Aggregate then
3060 return;
3061 end if;
3062 end Step_2;
3063
3064 -- STEP 3: Find discriminant Values
3065
3066 Step_3 : declare
3067 Discrim : Entity_Id;
3068 Missing_Discriminants : Boolean := False;
3069
3070 begin
3071 if Present (Expressions (N)) then
3072 Positional_Expr := First (Expressions (N));
3073 else
3074 Positional_Expr := Empty;
3075 end if;
3076
9013065b
AC
3077 if Has_Unknown_Discriminants (Typ)
3078 and then Present (Underlying_Record_View (Typ))
3079 then
3080 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3081 elsif Has_Discriminants (Typ) then
996ae0b0
RK
3082 Discrim := First_Discriminant (Typ);
3083 else
3084 Discrim := Empty;
3085 end if;
3086
3087 -- First find the discriminant values in the positional components
3088
3089 while Present (Discrim) and then Present (Positional_Expr) loop
3090 if Discr_Present (Discrim) then
3091 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2820d220 3092
0ab80019 3093 -- Ada 2005 (AI-231)
2820d220 3094
ec53a6da 3095 if Ada_Version >= Ada_05
8133b9d1 3096 and then Known_Null (Positional_Expr)
ec53a6da 3097 then
82c80734 3098 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2820d220
AC
3099 end if;
3100
996ae0b0
RK
3101 Next (Positional_Expr);
3102 end if;
3103
3104 if Present (Get_Value (Discrim, Component_Associations (N))) then
3105 Error_Msg_NE
3106 ("more than one value supplied for discriminant&",
3107 N, Discrim);
3108 end if;
3109
3110 Next_Discriminant (Discrim);
3111 end loop;
3112
3113 -- Find remaining discriminant values, if any, among named components
3114
3115 while Present (Discrim) loop
3116 Expr := Get_Value (Discrim, Component_Associations (N), True);
3117
3118 if not Discr_Present (Discrim) then
3119 if Present (Expr) then
3120 Error_Msg_NE
3121 ("more than one value supplied for discriminant&",
3122 N, Discrim);
3123 end if;
3124
3125 elsif No (Expr) then
3126 Error_Msg_NE
3127 ("no value supplied for discriminant &", N, Discrim);
3128 Missing_Discriminants := True;
3129
3130 else
3131 Resolve_Aggr_Expr (Expr, Discrim);
3132 end if;
3133
3134 Next_Discriminant (Discrim);
3135 end loop;
3136
3137 if Missing_Discriminants then
3138 return;
3139 end if;
3140
3141 -- At this point and until the beginning of STEP 6, New_Assoc_List
3142 -- contains only the discriminants and their values.
3143
3144 end Step_3;
3145
3146 -- STEP 4: Set the Etype of the record aggregate
3147
3148 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3149 -- routine should really be exported in sem_util or some such and used
3150 -- in sem_ch3 and here rather than have a copy of the code which is a
3151 -- maintenance nightmare.
3152
12a13f01 3153 -- ??? Performance WARNING. The current implementation creates a new
996ae0b0
RK
3154 -- itype for all aggregates whose base type is discriminated.
3155 -- This means that for record aggregates nested inside an array
3156 -- aggregate we will create a new itype for each record aggregate
12a13f01 3157 -- if the array component type has discriminants. For large aggregates
996ae0b0
RK
3158 -- this may be a problem. What should be done in this case is
3159 -- to reuse itypes as much as possible.
3160
9013065b
AC
3161 if Has_Discriminants (Typ)
3162 or else (Has_Unknown_Discriminants (Typ)
3163 and then Present (Underlying_Record_View (Typ)))
3164 then
996ae0b0
RK
3165 Build_Constrained_Itype : declare
3166 Loc : constant Source_Ptr := Sloc (N);
3167 Indic : Node_Id;
3168 Subtyp_Decl : Node_Id;
3169 Def_Id : Entity_Id;
3170
fbf5a39b 3171 C : constant List_Id := New_List;
996ae0b0
RK
3172
3173 begin
3174 New_Assoc := First (New_Assoc_List);
3175 while Present (New_Assoc) loop
3176 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3177 Next (New_Assoc);
3178 end loop;
3179
9013065b
AC
3180 if Has_Unknown_Discriminants (Typ)
3181 and then Present (Underlying_Record_View (Typ))
3182 then
3183 Indic :=
3184 Make_Subtype_Indication (Loc,
3185 Subtype_Mark =>
3186 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3187 Constraint =>
3188 Make_Index_Or_Discriminant_Constraint (Loc, C));
3189 else
3190 Indic :=
3191 Make_Subtype_Indication (Loc,
3192 Subtype_Mark =>
3193 New_Occurrence_Of (Base_Type (Typ), Loc),
3194 Constraint =>
3195 Make_Index_Or_Discriminant_Constraint (Loc, C));
3196 end if;
996ae0b0
RK
3197
3198 Def_Id := Create_Itype (Ekind (Typ), N);
3199
3200 Subtyp_Decl :=
3201 Make_Subtype_Declaration (Loc,
3202 Defining_Identifier => Def_Id,
3203 Subtype_Indication => Indic);
3204 Set_Parent (Subtyp_Decl, Parent (N));
3205
ec53a6da 3206 -- Itypes must be analyzed with checks off (see itypes.ads)
996ae0b0
RK
3207
3208 Analyze (Subtyp_Decl, Suppress => All_Checks);
3209
3210 Set_Etype (N, Def_Id);
3211 Check_Static_Discriminated_Subtype
3212 (Def_Id, Expression (First (New_Assoc_List)));
3213 end Build_Constrained_Itype;
3214
3215 else
3216 Set_Etype (N, Typ);
3217 end if;
3218
3219 -- STEP 5: Get remaining components according to discriminant values
3220
3221 Step_5 : declare
3222 Record_Def : Node_Id;
3223 Parent_Typ : Entity_Id;
3224 Root_Typ : Entity_Id;
3225 Parent_Typ_List : Elist_Id;
3226 Parent_Elmt : Elmt_Id;
3227 Errors_Found : Boolean := False;
3228 Dnode : Node_Id;
3229
3230 begin
3231 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3232 Parent_Typ_List := New_Elmt_List;
3233
3234 -- If this is an extension aggregate, the component list must
965dbd5c
AC
3235 -- include all components that are not in the given ancestor type.
3236 -- Otherwise, the component list must include components of all
3237 -- ancestors, starting with the root.
996ae0b0
RK
3238
3239 if Nkind (N) = N_Extension_Aggregate then
7b4db06c 3240 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
69a0c174 3241
996ae0b0
RK
3242 else
3243 Root_Typ := Root_Type (Typ);
3244
f53f9dd7
RD
3245 if Nkind (Parent (Base_Type (Root_Typ))) =
3246 N_Private_Type_Declaration
996ae0b0 3247 then
ed2233dc 3248 Error_Msg_NE
996ae0b0
RK
3249 ("type of aggregate has private ancestor&!",
3250 N, Root_Typ);
ed2233dc 3251 Error_Msg_N ("must use extension aggregate!", N);
996ae0b0
RK
3252 return;
3253 end if;
3254
3255 Dnode := Declaration_Node (Base_Type (Root_Typ));
3256
4519314c
AC
3257 -- If we don't get a full declaration, then we have some error
3258 -- which will get signalled later so skip this part. Otherwise
3259 -- gather components of root that apply to the aggregate type.
3260 -- We use the base type in case there is an applicable stored
3261 -- constraint that renames the discriminants of the root.
996ae0b0
RK
3262
3263 if Nkind (Dnode) = N_Full_Type_Declaration then
3264 Record_Def := Type_Definition (Dnode);
07fc65c4 3265 Gather_Components (Base_Type (Typ),
996ae0b0
RK
3266 Component_List (Record_Def),
3267 Governed_By => New_Assoc_List,
3268 Into => Components,
3269 Report_Errors => Errors_Found);
3270 end if;
3271 end if;
3272
9013065b 3273 Parent_Typ := Base_Type (Typ);
996ae0b0 3274 while Parent_Typ /= Root_Typ loop
996ae0b0
RK
3275 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3276 Parent_Typ := Etype (Parent_Typ);
3277
fbf5a39b 3278 if Nkind (Parent (Base_Type (Parent_Typ))) =
996ae0b0 3279 N_Private_Type_Declaration
fbf5a39b
AC
3280 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3281 N_Private_Extension_Declaration
996ae0b0
RK
3282 then
3283 if Nkind (N) /= N_Extension_Aggregate then
ed2233dc 3284 Error_Msg_NE
996ae0b0
RK
3285 ("type of aggregate has private ancestor&!",
3286 N, Parent_Typ);
ed2233dc 3287 Error_Msg_N ("must use extension aggregate!", N);
996ae0b0
RK
3288 return;
3289
3290 elsif Parent_Typ /= Root_Typ then
3291 Error_Msg_NE
3292 ("ancestor part of aggregate must be private type&",
3293 Ancestor_Part (N), Parent_Typ);
3294 return;
3295 end if;
4519314c
AC
3296
3297 -- The current view of ancestor part may be a private type,
3298 -- while the context type is always non-private.
3299
3300 elsif Is_Private_Type (Root_Typ)
3301 and then Present (Full_View (Root_Typ))
3302 and then Nkind (N) = N_Extension_Aggregate
3303 then
3304 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
996ae0b0
RK
3305 end if;
3306 end loop;
3307
bf06d37f
AC
3308 -- Now collect components from all other ancestors, beginning
3309 -- with the current type. If the type has unknown discriminants
349ff68f 3310 -- use the component list of the Underlying_Record_View, which
bf06d37f
AC
3311 -- needs to be used for the subsequent expansion of the aggregate
3312 -- into assignments.
996ae0b0
RK
3313
3314 Parent_Elmt := First_Elmt (Parent_Typ_List);
3315 while Present (Parent_Elmt) loop
3316 Parent_Typ := Node (Parent_Elmt);
bf06d37f
AC
3317
3318 if Has_Unknown_Discriminants (Parent_Typ)
3319 and then Present (Underlying_Record_View (Typ))
3320 then
3321 Parent_Typ := Underlying_Record_View (Parent_Typ);
3322 end if;
3323
996ae0b0
RK
3324 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3325 Gather_Components (Empty,
3326 Component_List (Record_Extension_Part (Record_Def)),
3327 Governed_By => New_Assoc_List,
3328 Into => Components,
3329 Report_Errors => Errors_Found);
3330
3331 Next_Elmt (Parent_Elmt);
3332 end loop;
3333
3334 else
6bde3eb5 3335 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
996ae0b0
RK
3336
3337 if Null_Present (Record_Def) then
3338 null;
bf06d37f
AC
3339
3340 elsif not Has_Unknown_Discriminants (Typ) then
07fc65c4 3341 Gather_Components (Base_Type (Typ),
996ae0b0
RK
3342 Component_List (Record_Def),
3343 Governed_By => New_Assoc_List,
3344 Into => Components,
3345 Report_Errors => Errors_Found);
bf06d37f
AC
3346
3347 else
3348 Gather_Components
3349 (Base_Type (Underlying_Record_View (Typ)),
3350 Component_List (Record_Def),
3351 Governed_By => New_Assoc_List,
3352 Into => Components,
3353 Report_Errors => Errors_Found);
996ae0b0
RK
3354 end if;
3355 end if;
3356
3357 if Errors_Found then
3358 return;
3359 end if;
3360 end Step_5;
3361
3362 -- STEP 6: Find component Values
3363
3364 Component := Empty;
3365 Component_Elmt := First_Elmt (Components);
3366
3367 -- First scan the remaining positional associations in the aggregate.
3368 -- Remember that at this point Positional_Expr contains the current
3369 -- positional association if any is left after looking for discriminant
3370 -- values in step 3.
3371
3372 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3373 Component := Node (Component_Elmt);
3374 Resolve_Aggr_Expr (Positional_Expr, Component);
3375
0ab80019
AC
3376 -- Ada 2005 (AI-231)
3377
ec53a6da 3378 if Ada_Version >= Ada_05
8133b9d1 3379 and then Known_Null (Positional_Expr)
ec53a6da 3380 then
82c80734 3381 Check_Can_Never_Be_Null (Component, Positional_Expr);
2820d220
AC
3382 end if;
3383
996ae0b0
RK
3384 if Present (Get_Value (Component, Component_Associations (N))) then
3385 Error_Msg_NE
3386 ("more than one value supplied for Component &", N, Component);
3387 end if;
3388
3389 Next (Positional_Expr);
3390 Next_Elmt (Component_Elmt);
3391 end loop;
3392
3393 if Present (Positional_Expr) then
3394 Error_Msg_N
3395 ("too many components for record aggregate", Positional_Expr);
3396 end if;
3397
3398 -- Now scan for the named arguments of the aggregate
3399
3400 while Present (Component_Elmt) loop
3401 Component := Node (Component_Elmt);
3402 Expr := Get_Value (Component, Component_Associations (N), True);
3403
9b96e234 3404 -- Note: The previous call to Get_Value sets the value of the
f91e8020 3405 -- variable Is_Box_Present.
65356e64 3406
9b96e234
JM
3407 -- Ada 2005 (AI-287): Handle components with default initialization.
3408 -- Note: This feature was originally added to Ada 2005 for limited
3409 -- but it was finally allowed with any type.
65356e64 3410
9b96e234 3411 if Is_Box_Present then
f91e8020
GD
3412 Check_Box_Component : declare
3413 Ctyp : constant Entity_Id := Etype (Component);
9b96e234
JM
3414
3415 begin
c7ce71c2
ES
3416 -- If there is a default expression for the aggregate, copy
3417 -- it into a new association.
3418
9b96e234
JM
3419 -- If the component has an initialization procedure (IP) we
3420 -- pass the component to the expander, which will generate
3421 -- the call to such IP.
3422
c7ce71c2
ES
3423 -- If the component has discriminants, their values must
3424 -- be taken from their subtype. This is indispensable for
3425 -- constraints that are given by the current instance of an
3426 -- enclosing type, to allow the expansion of the aggregate
3427 -- to replace the reference to the current instance by the
3428 -- target object of the aggregate.
3429
3430 if Present (Parent (Component))
3431 and then
3432 Nkind (Parent (Component)) = N_Component_Declaration
3433 and then Present (Expression (Parent (Component)))
aad93b55 3434 then
c7ce71c2
ES
3435 Expr :=
3436 New_Copy_Tree (Expression (Parent (Component)),
3437 New_Sloc => Sloc (N));
3438
9b96e234 3439 Add_Association
107b023c
AC
3440 (Component => Component,
3441 Expr => Expr,
3442 Assoc_List => New_Assoc_List);
c7ce71c2
ES
3443 Set_Has_Self_Reference (N);
3444
f91e8020
GD
3445 -- A box-defaulted access component gets the value null. Also
3446 -- included are components of private types whose underlying
c80d4855
RD
3447 -- type is an access type. In either case set the type of the
3448 -- literal, for subsequent use in semantic checks.
f91e8020
GD
3449
3450 elsif Present (Underlying_Type (Ctyp))
3451 and then Is_Access_Type (Underlying_Type (Ctyp))
3452 then
3453 if not Is_Private_Type (Ctyp) then
c80d4855
RD
3454 Expr := Make_Null (Sloc (N));
3455 Set_Etype (Expr, Ctyp);
f91e8020 3456 Add_Association
107b023c
AC
3457 (Component => Component,
3458 Expr => Expr,
3459 Assoc_List => New_Assoc_List);
f91e8020
GD
3460
3461 -- If the component's type is private with an access type as
3462 -- its underlying type then we have to create an unchecked
3463 -- conversion to satisfy type checking.
3464
3465 else
3466 declare
3467 Qual_Null : constant Node_Id :=
3468 Make_Qualified_Expression (Sloc (N),
3469 Subtype_Mark =>
3470 New_Occurrence_Of
3471 (Underlying_Type (Ctyp), Sloc (N)),
3472 Expression => Make_Null (Sloc (N)));
3473
3474 Convert_Null : constant Node_Id :=
3475 Unchecked_Convert_To
3476 (Ctyp, Qual_Null);
3477
3478 begin
3479 Analyze_And_Resolve (Convert_Null, Ctyp);
3480 Add_Association
107b023c
AC
3481 (Component => Component,
3482 Expr => Convert_Null,
3483 Assoc_List => New_Assoc_List);
f91e8020
GD
3484 end;
3485 end if;
3486
c7ce71c2
ES
3487 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3488 or else not Expander_Active
3489 then
3490 if Is_Record_Type (Ctyp)
3491 and then Has_Discriminants (Ctyp)
6bde3eb5 3492 and then not Is_Private_Type (Ctyp)
c7ce71c2
ES
3493 then
3494 -- We build a partially initialized aggregate with the
3495 -- values of the discriminants and box initialization
8133b9d1 3496 -- for the rest, if other components are present.
51ec70b8 3497 -- The type of the aggregate is the known subtype of
107b023c
AC
3498 -- the component. The capture of discriminants must
3499 -- be recursive because subcomponents may be contrained
3500 -- (transitively) by discriminants of enclosing types.
6bde3eb5
AC
3501 -- For a private type with discriminants, a call to the
3502 -- initialization procedure will be generated, and no
3503 -- subaggregate is needed.
c7ce71c2 3504
107b023c 3505 Capture_Discriminants : declare
719aaf4d
AC
3506 Loc : constant Source_Ptr := Sloc (N);
3507 Expr : Node_Id;
c7ce71c2 3508
107b023c
AC
3509 procedure Add_Discriminant_Values
3510 (New_Aggr : Node_Id;
3511 Assoc_List : List_Id);
3512 -- The constraint to a component may be given by a
3513 -- discriminant of the enclosing type, in which case
3514 -- we have to retrieve its value, which is part of the
3515 -- enclosing aggregate. Assoc_List provides the
3516 -- discriminant associations of the current type or
3517 -- of some enclosing record.
3518
3519 procedure Propagate_Discriminants
3520 (Aggr : Node_Id;
3521 Assoc_List : List_Id;
3522 Comp : Entity_Id);
3523 -- Nested components may themselves be discriminated
2be0bff8 3524 -- types constrained by outer discriminants, whose
107b023c
AC
3525 -- values must be captured before the aggregate is
3526 -- expanded into assignments.
3527
3528 -----------------------------
3529 -- Add_Discriminant_Values --
3530 -----------------------------
3531
3532 procedure Add_Discriminant_Values
3533 (New_Aggr : Node_Id;
3534 Assoc_List : List_Id)
3535 is
3536 Assoc : Node_Id;
3537 Discr : Entity_Id;
3538 Discr_Elmt : Elmt_Id;
3539 Discr_Val : Node_Id;
3540 Val : Entity_Id;
c7ce71c2 3541
107b023c
AC
3542 begin
3543 Discr := First_Discriminant (Etype (New_Aggr));
3544 Discr_Elmt :=
3545 First_Elmt
3546 (Discriminant_Constraint (Etype (New_Aggr)));
3547 while Present (Discr_Elmt) loop
3548 Discr_Val := Node (Discr_Elmt);
3549
3550 -- If the constraint is given by a discriminant
3551 -- it is a discriminant of an enclosing record,
3552 -- and its value has already been placed in the
3553 -- association list.
3554
3555 if Is_Entity_Name (Discr_Val)
3556 and then
3557 Ekind (Entity (Discr_Val)) = E_Discriminant
3558 then
3559 Val := Entity (Discr_Val);
3560
3561 Assoc := First (Assoc_List);
3562 while Present (Assoc) loop
3563 if Present
3564 (Entity (First (Choices (Assoc))))
3565 and then
3566 Entity (First (Choices (Assoc)))
3567 = Val
3568 then
3569 Discr_Val := Expression (Assoc);
3570 exit;
3571 end if;
3572 Next (Assoc);
3573 end loop;
3574 end if;
157a9bf5 3575
107b023c
AC
3576 Add_Association
3577 (Discr, New_Copy_Tree (Discr_Val),
3578 Component_Associations (New_Aggr));
3579
3580 -- If the discriminant constraint is a current
3581 -- instance, mark the current aggregate so that
3582 -- the self-reference can be expanded later.
3583
3584 if Nkind (Discr_Val) = N_Attribute_Reference
3585 and then Is_Entity_Name (Prefix (Discr_Val))
3586 and then Is_Type (Entity (Prefix (Discr_Val)))
3587 and then Etype (N) =
3588 Entity (Prefix (Discr_Val))
3589 then
3590 Set_Has_Self_Reference (N);
3591 end if;
c7ce71c2 3592
107b023c
AC
3593 Next_Elmt (Discr_Elmt);
3594 Next_Discriminant (Discr);
3595 end loop;
3596 end Add_Discriminant_Values;
3597
3598 ------------------------------
3599 -- Propagate_Discriminants --
3600 ------------------------------
3601
3602 procedure Propagate_Discriminants
3603 (Aggr : Node_Id;
3604 Assoc_List : List_Id;
3605 Comp : Entity_Id)
3606 is
3607 Inner_Comp : Entity_Id;
3608 Comp_Type : Entity_Id;
3609 Needs_Box : Boolean := False;
3610 New_Aggr : Node_Id;
c7ce71c2 3611
107b023c 3612 begin
107b023c
AC
3613 Inner_Comp := First_Component (Etype (Comp));
3614 while Present (Inner_Comp) loop
3615 Comp_Type := Etype (Inner_Comp);
c7ce71c2 3616
107b023c
AC
3617 if Is_Record_Type (Comp_Type)
3618 and then Has_Discriminants (Comp_Type)
3619 then
3620 New_Aggr :=
3621 Make_Aggregate (Loc, New_List, New_List);
3622 Set_Etype (New_Aggr, Comp_Type);
3623 Add_Association
3624 (Inner_Comp, New_Aggr,
719aaf4d 3625 Component_Associations (Aggr));
8133b9d1 3626
e264efcc 3627 -- Collect discriminant values and recurse
107b023c
AC
3628
3629 Add_Discriminant_Values
3630 (New_Aggr, Assoc_List);
3631 Propagate_Discriminants
3632 (New_Aggr, Assoc_List, Inner_Comp);
3633
3634 else
3635 Needs_Box := True;
8133b9d1
ES
3636 end if;
3637
107b023c 3638 Next_Component (Inner_Comp);
8133b9d1 3639 end loop;
107b023c
AC
3640
3641 if Needs_Box then
3642 Append
3643 (Make_Component_Association (Loc,
3644 Choices =>
3645 New_List (Make_Others_Choice (Loc)),
3646 Expression => Empty,
3647 Box_Present => True),
3648 Component_Associations (Aggr));
3649 end if;
3650 end Propagate_Discriminants;
3651
3652 begin
3653 Expr := Make_Aggregate (Loc, New_List, New_List);
3654 Set_Etype (Expr, Ctyp);
3655
3656 -- If the enclosing type has discriminants, they
3657 -- have been collected in the aggregate earlier, and
3658 -- they may appear as constraints of subcomponents.
3659 -- Similarly if this component has discriminants, they
2be0bff8 3660 -- might in turn be propagated to their components.
107b023c
AC
3661
3662 if Has_Discriminants (Typ) then
3663 Add_Discriminant_Values (Expr, New_Assoc_List);
3664 Propagate_Discriminants
3665 (Expr, New_Assoc_List, Component);
3666
3667 elsif Has_Discriminants (Ctyp) then
3668 Add_Discriminant_Values
3669 (Expr, Component_Associations (Expr));
3670 Propagate_Discriminants
3671 (Expr, Component_Associations (Expr), Component);
3672
3673 else
3674 declare
719aaf4d 3675 Comp : Entity_Id;
107b023c
AC
3676
3677 begin
3678 -- If the type has additional components, create
2be0bff8 3679 -- an OTHERS box association for them.
107b023c
AC
3680
3681 Comp := First_Component (Ctyp);
3682 while Present (Comp) loop
3683 if Ekind (Comp) = E_Component then
3684 if not Is_Record_Type (Etype (Comp)) then
3685 Append
3686 (Make_Component_Association (Loc,
3687 Choices =>
3688 New_List
3689 (Make_Others_Choice (Loc)),
3690 Expression => Empty,
3691 Box_Present => True),
3692 Component_Associations (Expr));
3693 end if;
3694 exit;
3695 end if;
3696
3697 Next_Component (Comp);
3698 end loop;
3699 end;
3700 end if;
c7ce71c2
ES
3701
3702 Add_Association
107b023c
AC
3703 (Component => Component,
3704 Expr => Expr,
3705 Assoc_List => New_Assoc_List);
3706 end Capture_Discriminants;
c7ce71c2
ES
3707
3708 else
3709 Add_Association
3710 (Component => Component,
3711 Expr => Empty,
107b023c 3712 Assoc_List => New_Assoc_List,
c7ce71c2
ES
3713 Is_Box_Present => True);
3714 end if;
9b96e234
JM
3715
3716 -- Otherwise we only need to resolve the expression if the
3717 -- component has partially initialized values (required to
3718 -- expand the corresponding assignments and run-time checks).
3719
3720 elsif Present (Expr)
f91e8020 3721 and then Is_Partially_Initialized_Type (Ctyp)
9b96e234
JM
3722 then
3723 Resolve_Aggr_Expr (Expr, Component);
3724 end if;
f91e8020 3725 end Check_Box_Component;
615cbd95 3726
65356e64 3727 elsif No (Expr) then
c7ce71c2
ES
3728
3729 -- Ignore hidden components associated with the position of the
3730 -- interface tags: these are initialized dynamically.
3731
c80d4855 3732 if not Present (Related_Type (Component)) then
c7ce71c2
ES
3733 Error_Msg_NE
3734 ("no value supplied for component &!", N, Component);
3735 end if;
615cbd95 3736
996ae0b0
RK
3737 else
3738 Resolve_Aggr_Expr (Expr, Component);
3739 end if;
3740
3741 Next_Elmt (Component_Elmt);
3742 end loop;
3743
3744 -- STEP 7: check for invalid components + check type in choice list
3745
3746 Step_7 : declare
3747 Selectr : Node_Id;
3748 -- Selector name
3749
9b96e234 3750 Typech : Entity_Id;
996ae0b0
RK
3751 -- Type of first component in choice list
3752
3753 begin
3754 if Present (Component_Associations (N)) then
3755 Assoc := First (Component_Associations (N));
3756 else
3757 Assoc := Empty;
3758 end if;
3759
3760 Verification : while Present (Assoc) loop
3761 Selectr := First (Choices (Assoc));
3762 Typech := Empty;
3763
3764 if Nkind (Selectr) = N_Others_Choice then
19f0526a 3765
9b96e234 3766 -- Ada 2005 (AI-287): others choice may have expression or box
19f0526a 3767
65356e64 3768 if No (Others_Etype)
9b96e234 3769 and then not Others_Box
65356e64 3770 then
ed2233dc 3771 Error_Msg_N
996ae0b0
RK
3772 ("OTHERS must represent at least one component", Selectr);
3773 end if;
3774
3775 exit Verification;
3776 end if;
3777
3778 while Present (Selectr) loop
3779 New_Assoc := First (New_Assoc_List);
3780 while Present (New_Assoc) loop
3781 Component := First (Choices (New_Assoc));
3782 exit when Chars (Selectr) = Chars (Component);
3783 Next (New_Assoc);
3784 end loop;
3785
3786 -- If no association, this is not a legal component of
aad93b55
ES
3787 -- of the type in question, except if its association
3788 -- is provided with a box.
996ae0b0
RK
3789
3790 if No (New_Assoc) then
65356e64 3791 if Box_Present (Parent (Selectr)) then
aad93b55
ES
3792
3793 -- This may still be a bogus component with a box. Scan
3794 -- list of components to verify that a component with
3795 -- that name exists.
3796
3797 declare
3798 C : Entity_Id;
3799
3800 begin
3801 C := First_Component (Typ);
3802 while Present (C) loop
3803 if Chars (C) = Chars (Selectr) then
ca44152f
ES
3804
3805 -- If the context is an extension aggregate,
3806 -- the component must not be inherited from
3807 -- the ancestor part of the aggregate.
3808
3809 if Nkind (N) /= N_Extension_Aggregate
3810 or else
3811 Scope (Original_Record_Component (C)) /=
3812 Etype (Ancestor_Part (N))
3813 then
3814 exit;
3815 end if;
aad93b55
ES
3816 end if;
3817
3818 Next_Component (C);
3819 end loop;
3820
3821 if No (C) then
3822 Error_Msg_Node_2 := Typ;
3823 Error_Msg_N ("& is not a component of}", Selectr);
3824 end if;
3825 end;
996ae0b0 3826
65356e64 3827 elsif Chars (Selectr) /= Name_uTag
996ae0b0
RK
3828 and then Chars (Selectr) /= Name_uParent
3829 and then Chars (Selectr) /= Name_uController
3830 then
3831 if not Has_Discriminants (Typ) then
3832 Error_Msg_Node_2 := Typ;
aad93b55 3833 Error_Msg_N ("& is not a component of}", Selectr);
996ae0b0
RK
3834 else
3835 Error_Msg_N
3836 ("& is not a component of the aggregate subtype",
3837 Selectr);
3838 end if;
3839
3840 Check_Misspelled_Component (Components, Selectr);
3841 end if;
3842
3843 elsif No (Typech) then
3844 Typech := Base_Type (Etype (Component));
3845
3846 elsif Typech /= Base_Type (Etype (Component)) then
65356e64
AC
3847 if not Box_Present (Parent (Selectr)) then
3848 Error_Msg_N
3849 ("components in choice list must have same type",
3850 Selectr);
3851 end if;
996ae0b0
RK
3852 end if;
3853
3854 Next (Selectr);
3855 end loop;
3856
3857 Next (Assoc);
3858 end loop Verification;
3859 end Step_7;
3860
3861 -- STEP 8: replace the original aggregate
3862
3863 Step_8 : declare
fbf5a39b 3864 New_Aggregate : constant Node_Id := New_Copy (N);
996ae0b0
RK
3865
3866 begin
3867 Set_Expressions (New_Aggregate, No_List);
3868 Set_Etype (New_Aggregate, Etype (N));
3869 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3870
3871 Rewrite (N, New_Aggregate);
3872 end Step_8;
3873 end Resolve_Record_Aggregate;
3874
2820d220
AC
3875 -----------------------------
3876 -- Check_Can_Never_Be_Null --
3877 -----------------------------
3878
9b96e234 3879 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
ec53a6da
JM
3880 Comp_Typ : Entity_Id;
3881
2820d220 3882 begin
9b96e234
JM
3883 pragma Assert
3884 (Ada_Version >= Ada_05
3885 and then Present (Expr)
8133b9d1 3886 and then Known_Null (Expr));
82c80734 3887
ec53a6da
JM
3888 case Ekind (Typ) is
3889 when E_Array_Type =>
3890 Comp_Typ := Component_Type (Typ);
3891
3892 when E_Component |
3893 E_Discriminant =>
3894 Comp_Typ := Etype (Typ);
3895
3896 when others =>
3897 return;
3898 end case;
3899
9b96e234
JM
3900 if Can_Never_Be_Null (Comp_Typ) then
3901
3902 -- Here we know we have a constraint error. Note that we do not use
3903 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3904 -- seem the more natural approach. That's because in some cases the
3905 -- components are rewritten, and the replacement would be missed.
3906
3907 Insert_Action
3908 (Compile_Time_Constraint_Error
3909 (Expr,
8133b9d1 3910 "(Ada 2005) null not allowed in null-excluding component?"),
9b96e234
JM
3911 Make_Raise_Constraint_Error (Sloc (Expr),
3912 Reason => CE_Access_Check_Failed));
3913
3914 -- Set proper type for bogus component (why is this needed???)
3915
3916 Set_Etype (Expr, Comp_Typ);
3917 Set_Analyzed (Expr);
2820d220
AC
3918 end if;
3919 end Check_Can_Never_Be_Null;
3920
996ae0b0
RK
3921 ---------------------
3922 -- Sort_Case_Table --
3923 ---------------------
3924
3925 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
fbf5a39b
AC
3926 L : constant Int := Case_Table'First;
3927 U : constant Int := Case_Table'Last;
996ae0b0
RK
3928 K : Int;
3929 J : Int;
3930 T : Case_Bounds;
3931
3932 begin
3933 K := L;
996ae0b0
RK
3934 while K /= U loop
3935 T := Case_Table (K + 1);
996ae0b0 3936
7f9747c6 3937 J := K + 1;
996ae0b0
RK
3938 while J /= L
3939 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3940 Expr_Value (T.Choice_Lo)
3941 loop
3942 Case_Table (J) := Case_Table (J - 1);
3943 J := J - 1;
3944 end loop;
3945
3946 Case_Table (J) := T;
3947 K := K + 1;
3948 end loop;
3949 end Sort_Case_Table;
3950
3951end Sem_Aggr;
This page took 3.021842 seconds and 5 git commands to generate.