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