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