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