]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ A G G R -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
cccef051 | 9 | -- Copyright (C) 1992-2023, 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 | ||
104f58db BD |
26 | with Aspects; use Aspects; |
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
104f58db BD |
30 | with Einfo.Utils; use Einfo.Utils; |
31 | with Elists; use Elists; | |
32 | with Errout; use Errout; | |
33 | with Expander; use Expander; | |
34 | with Exp_Ch6; use Exp_Ch6; | |
35 | with Exp_Tss; use Exp_Tss; | |
36 | with Exp_Util; use Exp_Util; | |
37 | with Freeze; use Freeze; | |
38 | with Itypes; use Itypes; | |
39 | with Lib; use Lib; | |
40 | with Lib.Xref; use Lib.Xref; | |
41 | with Namet; use Namet; | |
42 | with Namet.Sp; use Namet.Sp; | |
43 | with Nmake; use Nmake; | |
44 | with Nlists; use Nlists; | |
45 | with Opt; use Opt; | |
46 | with Restrict; use Restrict; | |
47 | with Rident; use Rident; | |
48 | with Sem; use Sem; | |
49 | with Sem_Aux; use Sem_Aux; | |
e1dfbb03 | 50 | with Sem_Case; use Sem_Case; |
104f58db BD |
51 | with Sem_Cat; use Sem_Cat; |
52 | with Sem_Ch3; use Sem_Ch3; | |
104f58db BD |
53 | with Sem_Ch8; use Sem_Ch8; |
54 | with Sem_Ch13; use Sem_Ch13; | |
55 | with Sem_Dim; use Sem_Dim; | |
56 | with Sem_Eval; use Sem_Eval; | |
57 | with Sem_Res; use Sem_Res; | |
58 | with Sem_Util; use Sem_Util; | |
59 | with Sem_Type; use Sem_Type; | |
60 | with Sem_Warn; use Sem_Warn; | |
61 | with Sinfo; use Sinfo; | |
62 | with Sinfo.Nodes; use Sinfo.Nodes; | |
63 | with Sinfo.Utils; use Sinfo.Utils; | |
64 | with Snames; use Snames; | |
65 | with Stringt; use Stringt; | |
66 | with Stand; use Stand; | |
67 | with Style; use Style; | |
68 | with Targparm; use Targparm; | |
69 | with Tbuild; use Tbuild; | |
70 | with Ttypes; use Ttypes; | |
71 | with Uintp; use Uintp; | |
bc50ac71 | 72 | with Warnsw; use Warnsw; |
996ae0b0 | 73 | |
996ae0b0 RK |
74 | package body Sem_Aggr is |
75 | ||
76 | type Case_Bounds is record | |
82893775 AC |
77 | Lo : Node_Id; |
78 | -- Low bound of choice. Once we sort the Case_Table, then entries | |
79 | -- will be in order of ascending Choice_Lo values. | |
80 | ||
81 | Hi : Node_Id; | |
82 | -- High Bound of choice. The sort does not pay any attention to the | |
83 | -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order. | |
84 | ||
85 | Highest : Uint; | |
86 | -- If there are duplicates or missing entries, then in the sorted | |
87 | -- table, this records the highest value among Choice_Hi values | |
88 | -- seen so far, including this entry. | |
89 | ||
90 | Choice : Node_Id; | |
91 | -- The node of the choice | |
996ae0b0 RK |
92 | end record; |
93 | ||
38f44fd6 PT |
94 | type Case_Table_Type is array (Pos range <>) of Case_Bounds; |
95 | -- Table type used by Check_Case_Choices procedure | |
996ae0b0 RK |
96 | |
97 | ----------------------- | |
98 | -- Local Subprograms -- | |
99 | ----------------------- | |
100 | ||
101 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type); | |
82893775 AC |
102 | -- Sort the Case Table using the Lower Bound of each Choice as the key. A |
103 | -- simple insertion sort is used since the choices in a case statement will | |
104 | -- usually be in near sorted order. | |
996ae0b0 | 105 | |
9b96e234 JM |
106 | procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id); |
107 | -- Ada 2005 (AI-231): Check bad usage of null for a component for which | |
108 | -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for | |
109 | -- the array case (the component type of the array will be used) or an | |
110 | -- E_Component/E_Discriminant entity in the record case, in which case the | |
111 | -- type of the component will be used for the test. If Typ is any other | |
112 | -- kind of entity, the call is ignored. Expr is the component node in the | |
8133b9d1 | 113 | -- aggregate which is known to have a null value. A warning message will be |
9b96e234 JM |
114 | -- issued if the component is null excluding. |
115 | -- | |
116 | -- It would be better to pass the proper type for Typ ??? | |
2820d220 | 117 | |
ca44152f ES |
118 | procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id); |
119 | -- Check that Expr is either not limited or else is one of the cases of | |
120 | -- expressions allowed for a limited component association (namely, an | |
121 | -- aggregate, function call, or <> notation). Report error for violations. | |
480156b2 | 122 | -- Expression is also OK in an instance or inlining context, because we |
812e6118 | 123 | -- have already preanalyzed and it is known to be type correct. |
ca44152f | 124 | |
996ae0b0 RK |
125 | ------------------------------------------------------ |
126 | -- Subprograms used for RECORD AGGREGATE Processing -- | |
127 | ------------------------------------------------------ | |
128 | ||
129 | procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id); | |
130 | -- This procedure performs all the semantic checks required for record | |
131 | -- aggregates. Note that for aggregates analysis and resolution go | |
132 | -- hand in hand. Aggregate analysis has been delayed up to here and | |
133 | -- it is done while resolving the aggregate. | |
134 | -- | |
135 | -- N is the N_Aggregate node. | |
136 | -- Typ is the record type for the aggregate resolution | |
137 | -- | |
9b96e234 JM |
138 | -- While performing the semantic checks, this procedure builds a new |
139 | -- Component_Association_List where each record field appears alone in a | |
140 | -- Component_Choice_List along with its corresponding expression. The | |
141 | -- record fields in the Component_Association_List appear in the same order | |
142 | -- in which they appear in the record type Typ. | |
996ae0b0 | 143 | -- |
9b96e234 JM |
144 | -- Once this new Component_Association_List is built and all the semantic |
145 | -- checks performed, the original aggregate subtree is replaced with the | |
0ad46f04 PT |
146 | -- new named record aggregate just built. This new record aggregate has no |
147 | -- positional associations, so its Expressions field is set to No_List. | |
148 | -- Note that subtree substitution is performed with Rewrite so as to be | |
149 | -- able to retrieve the original aggregate. | |
996ae0b0 RK |
150 | -- |
151 | -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate | |
152 | -- yields the aggregate format expected by Gigi. Typically, this kind of | |
153 | -- tree manipulations are done in the expander. However, because the | |
9b96e234 JM |
154 | -- semantic checks that need to be performed on record aggregates really go |
155 | -- hand in hand with the record aggregate normalization, the aggregate | |
996ae0b0 | 156 | -- subtree transformation is performed during resolution rather than |
9b96e234 JM |
157 | -- expansion. Had we decided otherwise we would have had to duplicate most |
158 | -- of the code in the expansion procedure Expand_Record_Aggregate. Note, | |
c7ce71c2 | 159 | -- however, that all the expansion concerning aggregates for tagged records |
9b96e234 | 160 | -- is done in Expand_Record_Aggregate. |
996ae0b0 RK |
161 | -- |
162 | -- The algorithm of Resolve_Record_Aggregate proceeds as follows: | |
163 | -- | |
164 | -- 1. Make sure that the record type against which the record aggregate | |
c9a1acdc AC |
165 | -- has to be resolved is not abstract. Furthermore if the type is a |
166 | -- null aggregate make sure the input aggregate N is also null. | |
996ae0b0 RK |
167 | -- |
168 | -- 2. Verify that the structure of the aggregate is that of a record | |
169 | -- aggregate. Specifically, look for component associations and ensure | |
170 | -- that each choice list only has identifiers or the N_Others_Choice | |
171 | -- node. Also make sure that if present, the N_Others_Choice occurs | |
172 | -- last and by itself. | |
173 | -- | |
c9a1acdc AC |
174 | -- 3. If Typ contains discriminants, the values for each discriminant is |
175 | -- looked for. If the record type Typ has variants, we check that the | |
176 | -- expressions corresponding to each discriminant ruling the (possibly | |
177 | -- nested) variant parts of Typ, are static. This allows us to determine | |
178 | -- the variant parts to which the rest of the aggregate must conform. | |
179 | -- The names of discriminants with their values are saved in a new | |
180 | -- association list, New_Assoc_List which is later augmented with the | |
181 | -- names and values of the remaining components in the record type. | |
996ae0b0 RK |
182 | -- |
183 | -- During this phase we also make sure that every discriminant is | |
c9a1acdc AC |
184 | -- assigned exactly one value. Note that when several values for a given |
185 | -- discriminant are found, semantic processing continues looking for | |
186 | -- further errors. In this case it's the first discriminant value found | |
187 | -- which we will be recorded. | |
996ae0b0 RK |
188 | -- |
189 | -- IMPORTANT NOTE: For derived tagged types this procedure expects | |
190 | -- First_Discriminant and Next_Discriminant to give the correct list | |
191 | -- of discriminants, in the correct order. | |
192 | -- | |
c9a1acdc AC |
193 | -- 4. After all the discriminant values have been gathered, we can set the |
194 | -- Etype of the record aggregate. If Typ contains no discriminants this | |
195 | -- is straightforward: the Etype of N is just Typ, otherwise a new | |
196 | -- implicit constrained subtype of Typ is built to be the Etype of N. | |
996ae0b0 RK |
197 | -- |
198 | -- 5. Gather the remaining record components according to the discriminant | |
199 | -- values. This involves recursively traversing the record type | |
200 | -- structure to see what variants are selected by the given discriminant | |
201 | -- values. This processing is a little more convoluted if Typ is a | |
202 | -- derived tagged types since we need to retrieve the record structure | |
203 | -- of all the ancestors of Typ. | |
204 | -- | |
c9a1acdc AC |
205 | -- 6. After gathering the record components we look for their values in the |
206 | -- record aggregate and emit appropriate error messages should we not | |
207 | -- find such values or should they be duplicated. | |
208 | -- | |
209 | -- 7. We then make sure no illegal component names appear in the record | |
210 | -- aggregate and make sure that the type of the record components | |
211 | -- appearing in a same choice list is the same. Finally we ensure that | |
212 | -- the others choice, if present, is used to provide the value of at | |
213 | -- least a record component. | |
214 | -- | |
215 | -- 8. The original aggregate node is replaced with the new named aggregate | |
216 | -- built in steps 3 through 6, as explained earlier. | |
217 | -- | |
218 | -- Given the complexity of record aggregate resolution, the primary goal of | |
219 | -- this routine is clarity and simplicity rather than execution and storage | |
220 | -- efficiency. If there are only positional components in the aggregate the | |
221 | -- running time is linear. If there are associations the running time is | |
222 | -- still linear as long as the order of the associations is not too far off | |
223 | -- the order of the components in the record type. If this is not the case | |
224 | -- the running time is at worst quadratic in the size of the association | |
225 | -- list. | |
996ae0b0 RK |
226 | |
227 | procedure Check_Misspelled_Component | |
9c290e69 PO |
228 | (Elements : Elist_Id; |
229 | Component : Node_Id); | |
c9a1acdc AC |
230 | -- Give possible misspelling diagnostic if Component is likely to be a |
231 | -- misspelling of one of the components of the Assoc_List. This is called | |
232 | -- by Resolve_Aggr_Expr after producing an invalid component error message. | |
996ae0b0 | 233 | |
996ae0b0 RK |
234 | ----------------------------------------------------- |
235 | -- Subprograms used for ARRAY AGGREGATE Processing -- | |
236 | ----------------------------------------------------- | |
237 | ||
238 | function Resolve_Array_Aggregate | |
239 | (N : Node_Id; | |
240 | Index : Node_Id; | |
241 | Index_Constr : Node_Id; | |
242 | Component_Typ : Entity_Id; | |
ca44152f | 243 | Others_Allowed : Boolean) return Boolean; |
996ae0b0 RK |
244 | -- This procedure performs the semantic checks for an array aggregate. |
245 | -- True is returned if the aggregate resolution succeeds. | |
ca44152f | 246 | -- |
996ae0b0 | 247 | -- The procedure works by recursively checking each nested aggregate. |
9f4fd324 | 248 | -- Specifically, after checking a sub-aggregate nested at the i-th level |
996ae0b0 | 249 | -- we recursively check all the subaggregates at the i+1-st level (if any). |
b045f2f2 | 250 | -- Note that aggregates analysis and resolution go hand in hand. |
996ae0b0 RK |
251 | -- Aggregate analysis has been delayed up to here and it is done while |
252 | -- resolving the aggregate. | |
253 | -- | |
254 | -- N is the current N_Aggregate node to be checked. | |
255 | -- | |
256 | -- Index is the index node corresponding to the array sub-aggregate that | |
257 | -- we are currently checking (RM 4.3.3 (8)). Its Etype is the | |
258 | -- corresponding index type (or subtype). | |
259 | -- | |
260 | -- Index_Constr is the node giving the applicable index constraint if | |
261 | -- any (RM 4.3.3 (10)). It "is a constraint provided by certain | |
262 | -- contexts [...] that can be used to determine the bounds of the array | |
263 | -- value specified by the aggregate". If Others_Allowed below is False | |
264 | -- there is no applicable index constraint and this node is set to Index. | |
265 | -- | |
266 | -- Component_Typ is the array component type. | |
267 | -- | |
268 | -- Others_Allowed indicates whether an others choice is allowed | |
269 | -- in the context where the top-level aggregate appeared. | |
270 | -- | |
271 | -- The algorithm of Resolve_Array_Aggregate proceeds as follows: | |
272 | -- | |
273 | -- 1. Make sure that the others choice, if present, is by itself and | |
274 | -- appears last in the sub-aggregate. Check that we do not have | |
275 | -- positional and named components in the array sub-aggregate (unless | |
276 | -- the named association is an others choice). Finally if an others | |
12a13f01 | 277 | -- choice is present, make sure it is allowed in the aggregate context. |
996ae0b0 RK |
278 | -- |
279 | -- 2. If the array sub-aggregate contains discrete_choices: | |
280 | -- | |
281 | -- (A) Verify their validity. Specifically verify that: | |
282 | -- | |
283 | -- (a) If a null range is present it must be the only possible | |
284 | -- choice in the array aggregate. | |
285 | -- | |
286 | -- (b) Ditto for a non static range. | |
287 | -- | |
288 | -- (c) Ditto for a non static expression. | |
289 | -- | |
290 | -- In addition this step analyzes and resolves each discrete_choice, | |
291 | -- making sure that its type is the type of the corresponding Index. | |
292 | -- If we are not at the lowest array aggregate level (in the case of | |
293 | -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate | |
294 | -- recursively on each component expression. Otherwise, resolve the | |
295 | -- bottom level component expressions against the expected component | |
296 | -- type ONLY IF the component corresponds to a single discrete choice | |
297 | -- which is not an others choice (to see why read the DELAYED | |
298 | -- COMPONENT RESOLUTION below). | |
299 | -- | |
300 | -- (B) Determine the bounds of the sub-aggregate and lowest and | |
301 | -- highest choice values. | |
302 | -- | |
303 | -- 3. For positional aggregates: | |
304 | -- | |
305 | -- (A) Loop over the component expressions either recursively invoking | |
306 | -- Resolve_Array_Aggregate on each of these for multi-dimensional | |
307 | -- array aggregates or resolving the bottom level component | |
308 | -- expressions against the expected component type. | |
309 | -- | |
310 | -- (B) Determine the bounds of the positional sub-aggregates. | |
311 | -- | |
312 | -- 4. Try to determine statically whether the evaluation of the array | |
313 | -- sub-aggregate raises Constraint_Error. If yes emit proper | |
314 | -- warnings. The precise checks are the following: | |
315 | -- | |
316 | -- (A) Check that the index range defined by aggregate bounds is | |
317 | -- compatible with corresponding index subtype. | |
318 | -- We also check against the base type. In fact it could be that | |
319 | -- Low/High bounds of the base type are static whereas those of | |
320 | -- the index subtype are not. Thus if we can statically catch | |
321 | -- a problem with respect to the base type we are guaranteed | |
322 | -- that the same problem will arise with the index subtype | |
323 | -- | |
324 | -- (B) If we are dealing with a named aggregate containing an others | |
325 | -- choice and at least one discrete choice then make sure the range | |
326 | -- specified by the discrete choices does not overflow the | |
327 | -- aggregate bounds. We also check against the index type and base | |
328 | -- type bounds for the same reasons given in (A). | |
329 | -- | |
330 | -- (C) If we are dealing with a positional aggregate with an others | |
331 | -- choice make sure the number of positional elements specified | |
332 | -- does not overflow the aggregate bounds. We also check against | |
333 | -- the index type and base type bounds as mentioned in (A). | |
334 | -- | |
335 | -- Finally construct an N_Range node giving the sub-aggregate bounds. | |
336 | -- Set the Aggregate_Bounds field of the sub-aggregate to be this | |
337 | -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges | |
338 | -- to build the appropriate aggregate subtype. Aggregate_Bounds | |
339 | -- information is needed during expansion. | |
340 | -- | |
341 | -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component | |
342 | -- expressions in an array aggregate may call Duplicate_Subexpr or some | |
343 | -- other routine that inserts code just outside the outermost aggregate. | |
344 | -- If the array aggregate contains discrete choices or an others choice, | |
345 | -- this may be wrong. Consider for instance the following example. | |
346 | -- | |
347 | -- type Rec is record | |
348 | -- V : Integer := 0; | |
349 | -- end record; | |
350 | -- | |
351 | -- type Acc_Rec is access Rec; | |
352 | -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec); | |
353 | -- | |
354 | -- Then the transformation of "new Rec" that occurs during resolution | |
355 | -- entails the following code modifications | |
356 | -- | |
357 | -- P7b : constant Acc_Rec := new Rec; | |
fbf5a39b | 358 | -- RecIP (P7b.all); |
996ae0b0 RK |
359 | -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b); |
360 | -- | |
361 | -- This code transformation is clearly wrong, since we need to call | |
362 | -- "new Rec" for each of the 3 array elements. To avoid this problem we | |
363 | -- delay resolution of the components of non positional array aggregates | |
364 | -- to the expansion phase. As an optimization, if the discrete choice | |
365 | -- specifies a single value we do not delay resolution. | |
366 | ||
c0471c61 | 367 | function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id; |
996ae0b0 RK |
368 | -- This routine returns the type or subtype of an array aggregate. |
369 | -- | |
370 | -- N is the array aggregate node whose type we return. | |
371 | -- | |
372 | -- Typ is the context type in which N occurs. | |
373 | -- | |
c45b6ae0 | 374 | -- This routine creates an implicit array subtype whose bounds are |
996ae0b0 RK |
375 | -- those defined by the aggregate. When this routine is invoked |
376 | -- Resolve_Array_Aggregate has already processed aggregate N. Thus the | |
377 | -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the | |
c7ce71c2 | 378 | -- sub-aggregate bounds. When building the aggregate itype, this function |
996ae0b0 RK |
379 | -- traverses the array aggregate N collecting such Aggregate_Bounds and |
380 | -- constructs the proper array aggregate itype. | |
381 | -- | |
382 | -- Note that in the case of multidimensional aggregates each inner | |
383 | -- sub-aggregate corresponding to a given array dimension, may provide a | |
384 | -- different bounds. If it is possible to determine statically that | |
385 | -- some sub-aggregates corresponding to the same index do not have the | |
386 | -- same bounds, then a warning is emitted. If such check is not possible | |
387 | -- statically (because some sub-aggregate bounds are dynamic expressions) | |
388 | -- then this job is left to the expander. In all cases the particular | |
389 | -- bounds that this function will chose for a given dimension is the first | |
390 | -- N_Range node for a sub-aggregate corresponding to that dimension. | |
391 | -- | |
392 | -- Note that the Raises_Constraint_Error flag of an array aggregate | |
393 | -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate, | |
394 | -- is set in Resolve_Array_Aggregate but the aggregate is not | |
395 | -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must | |
396 | -- first construct the proper itype for the aggregate (Gigi needs | |
21d7ef70 | 397 | -- this). After constructing the proper itype we will eventually replace |
996ae0b0 RK |
398 | -- the top-level aggregate with a raise CE (done in Resolve_Aggregate). |
399 | -- Of course in cases such as: | |
400 | -- | |
401 | -- type Arr is array (integer range <>) of Integer; | |
402 | -- A : Arr := (positive range -1 .. 2 => 0); | |
403 | -- | |
404 | -- The bounds of the aggregate itype are cooked up to look reasonable | |
405 | -- (in this particular case the bounds will be 1 .. 2). | |
406 | ||
996ae0b0 | 407 | procedure Make_String_Into_Aggregate (N : Node_Id); |
21d7ef70 | 408 | -- A string literal can appear in a context in which a one dimensional |
996ae0b0 RK |
409 | -- array of characters is expected. This procedure simply rewrites the |
410 | -- string as an aggregate, prior to resolution. | |
411 | ||
10c257af ES |
412 | function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean; |
413 | -- For the Ada 2022 construct, build a subtype with a null range for each | |
414 | -- dimension, using the bounds from the context subtype (if the subtype | |
415 | -- is constrained). If the subtype is unconstrained, then the bounds | |
416 | -- are determined in much the same way as the bounds for a null string | |
417 | -- literal with no applicable index constraint. | |
10c257af | 418 | |
8d9a1ba7 PMR |
419 | --------------------------------- |
420 | -- Delta aggregate processing -- | |
421 | --------------------------------- | |
422 | ||
423 | procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id); | |
424 | procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id); | |
0191a24e SB |
425 | procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id); |
426 | -- Resolve the names/expressions in a component association for | |
427 | -- a deep delta aggregate. Typ is the type of the enclosing object. | |
8d9a1ba7 | 428 | |
996ae0b0 RK |
429 | ------------------------ |
430 | -- Array_Aggr_Subtype -- | |
431 | ------------------------ | |
432 | ||
433 | function Array_Aggr_Subtype | |
b87971f3 AC |
434 | (N : Node_Id; |
435 | Typ : Entity_Id) return Entity_Id | |
996ae0b0 RK |
436 | is |
437 | Aggr_Dimension : constant Pos := Number_Dimensions (Typ); | |
ec53a6da | 438 | -- Number of aggregate index dimensions |
996ae0b0 RK |
439 | |
440 | Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); | |
ec53a6da | 441 | -- Constrained N_Range of each index dimension in our aggregate itype |
996ae0b0 | 442 | |
58009744 AC |
443 | Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); |
444 | Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); | |
ec53a6da | 445 | -- Low and High bounds for each index dimension in our aggregate itype |
996ae0b0 RK |
446 | |
447 | Is_Fully_Positional : Boolean := True; | |
448 | ||
449 | procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos); | |
fb468a94 AC |
450 | -- N is an array (sub-)aggregate. Dim is the dimension corresponding |
451 | -- to (sub-)aggregate N. This procedure collects and removes the side | |
452 | -- effects of the constrained N_Range nodes corresponding to each index | |
2b3d67a5 AC |
453 | -- dimension of our aggregate itype. These N_Range nodes are collected |
454 | -- in Aggr_Range above. | |
ec53a6da | 455 | -- |
996ae0b0 RK |
456 | -- Likewise collect in Aggr_Low & Aggr_High above the low and high |
457 | -- bounds of each index dimension. If, when collecting, two bounds | |
458 | -- corresponding to the same dimension are static and found to differ, | |
459 | -- then emit a warning, and mark N as raising Constraint_Error. | |
460 | ||
461 | ------------------------- | |
462 | -- Collect_Aggr_Bounds -- | |
463 | ------------------------- | |
464 | ||
465 | procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is | |
466 | This_Range : constant Node_Id := Aggregate_Bounds (N); | |
ec53a6da | 467 | -- The aggregate range node of this specific sub-aggregate |
996ae0b0 | 468 | |
93d0457e PT |
469 | This_Low : constant Node_Id := Low_Bound (This_Range); |
470 | This_High : constant Node_Id := High_Bound (This_Range); | |
ec53a6da | 471 | -- The aggregate bounds of this specific sub-aggregate |
996ae0b0 RK |
472 | |
473 | Assoc : Node_Id; | |
474 | Expr : Node_Id; | |
475 | ||
476 | begin | |
fb468a94 AC |
477 | Remove_Side_Effects (This_Low, Variable_Ref => True); |
478 | Remove_Side_Effects (This_High, Variable_Ref => True); | |
479 | ||
996ae0b0 RK |
480 | -- Collect the first N_Range for a given dimension that you find. |
481 | -- For a given dimension they must be all equal anyway. | |
482 | ||
483 | if No (Aggr_Range (Dim)) then | |
484 | Aggr_Low (Dim) := This_Low; | |
485 | Aggr_High (Dim) := This_High; | |
486 | Aggr_Range (Dim) := This_Range; | |
487 | ||
488 | else | |
489 | if Compile_Time_Known_Value (This_Low) then | |
490 | if not Compile_Time_Known_Value (Aggr_Low (Dim)) then | |
be035558 | 491 | Aggr_Low (Dim) := This_Low; |
996ae0b0 RK |
492 | |
493 | elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then | |
494 | Set_Raises_Constraint_Error (N); | |
43417b90 | 495 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
496 | Error_Msg_N ("sub-aggregate low bound mismatch<<", N); |
497 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
498 | end if; |
499 | end if; | |
500 | ||
501 | if Compile_Time_Known_Value (This_High) then | |
502 | if not Compile_Time_Known_Value (Aggr_High (Dim)) then | |
be035558 | 503 | Aggr_High (Dim) := This_High; |
996ae0b0 RK |
504 | |
505 | elsif | |
506 | Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim)) | |
507 | then | |
508 | Set_Raises_Constraint_Error (N); | |
43417b90 | 509 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
510 | Error_Msg_N ("sub-aggregate high bound mismatch<<", N); |
511 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
512 | end if; |
513 | end if; | |
514 | end if; | |
515 | ||
516 | if Dim < Aggr_Dimension then | |
517 | ||
518 | -- Process positional components | |
519 | ||
520 | if Present (Expressions (N)) then | |
521 | Expr := First (Expressions (N)); | |
522 | while Present (Expr) loop | |
523 | Collect_Aggr_Bounds (Expr, Dim + 1); | |
524 | Next (Expr); | |
525 | end loop; | |
526 | end if; | |
527 | ||
528 | -- Process component associations | |
529 | ||
530 | if Present (Component_Associations (N)) then | |
531 | Is_Fully_Positional := False; | |
532 | ||
533 | Assoc := First (Component_Associations (N)); | |
534 | while Present (Assoc) loop | |
535 | Expr := Expression (Assoc); | |
536 | Collect_Aggr_Bounds (Expr, Dim + 1); | |
537 | Next (Assoc); | |
538 | end loop; | |
539 | end if; | |
540 | end if; | |
541 | end Collect_Aggr_Bounds; | |
542 | ||
543 | -- Array_Aggr_Subtype variables | |
544 | ||
545 | Itype : Entity_Id; | |
b87971f3 | 546 | -- The final itype of the overall aggregate |
996ae0b0 | 547 | |
fbf5a39b | 548 | Index_Constraints : constant List_Id := New_List; |
ec53a6da | 549 | -- The list of index constraints of the aggregate itype |
996ae0b0 RK |
550 | |
551 | -- Start of processing for Array_Aggr_Subtype | |
552 | ||
553 | begin | |
b87971f3 AC |
554 | -- Make sure that the list of index constraints is properly attached to |
555 | -- the tree, and then collect the aggregate bounds. | |
996ae0b0 | 556 | |
fe43084c ES |
557 | -- If no aggregaate bounds have been set, this is an aggregate with |
558 | -- iterator specifications and a dynamic size to be determined by | |
559 | -- first pass of expanded code. | |
560 | ||
561 | if No (Aggregate_Bounds (N)) then | |
562 | return Typ; | |
563 | end if; | |
564 | ||
996ae0b0 | 565 | Set_Parent (Index_Constraints, N); |
59a23beb PT |
566 | |
567 | -- When resolving a null aggregate we created a list of aggregate bounds | |
568 | -- for the consecutive dimensions. The bounds for the first dimension | |
569 | -- are attached as the Aggregate_Bounds of the aggregate node. | |
570 | ||
571 | if Is_Null_Aggregate (N) then | |
572 | declare | |
573 | This_Range : Node_Id := Aggregate_Bounds (N); | |
574 | begin | |
575 | for J in 1 .. Aggr_Dimension loop | |
576 | Aggr_Range (J) := This_Range; | |
577 | Next_Index (This_Range); | |
578 | ||
579 | -- Remove bounds from the list, so they can be reattached as | |
580 | -- the First_Index/Next_Index again by the code that also | |
581 | -- handles non-null aggregates. | |
582 | ||
583 | Remove (Aggr_Range (J)); | |
584 | end loop; | |
585 | end; | |
586 | else | |
587 | Collect_Aggr_Bounds (N, 1); | |
588 | end if; | |
996ae0b0 | 589 | |
3b42c566 | 590 | -- Build the list of constrained indexes of our aggregate itype |
996ae0b0 RK |
591 | |
592 | for J in 1 .. Aggr_Dimension loop | |
593 | Create_Index : declare | |
fbf5a39b AC |
594 | Index_Base : constant Entity_Id := |
595 | Base_Type (Etype (Aggr_Range (J))); | |
996ae0b0 RK |
596 | Index_Typ : Entity_Id; |
597 | ||
598 | begin | |
8133b9d1 ES |
599 | -- Construct the Index subtype, and associate it with the range |
600 | -- construct that generates it. | |
996ae0b0 | 601 | |
8133b9d1 ES |
602 | Index_Typ := |
603 | Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J)); | |
996ae0b0 RK |
604 | |
605 | Set_Etype (Index_Typ, Index_Base); | |
606 | ||
607 | if Is_Character_Type (Index_Base) then | |
608 | Set_Is_Character_Type (Index_Typ); | |
609 | end if; | |
610 | ||
611 | Set_Size_Info (Index_Typ, (Index_Base)); | |
612 | Set_RM_Size (Index_Typ, RM_Size (Index_Base)); | |
613 | Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base)); | |
614 | Set_Scalar_Range (Index_Typ, Aggr_Range (J)); | |
615 | ||
616 | if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then | |
617 | Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ))); | |
618 | end if; | |
619 | ||
620 | Set_Etype (Aggr_Range (J), Index_Typ); | |
621 | ||
622 | Append (Aggr_Range (J), To => Index_Constraints); | |
623 | end Create_Index; | |
624 | end loop; | |
625 | ||
626 | -- Now build the Itype | |
627 | ||
628 | Itype := Create_Itype (E_Array_Subtype, N); | |
629 | ||
b87971f3 AC |
630 | Set_First_Rep_Item (Itype, First_Rep_Item (Typ)); |
631 | Set_Convention (Itype, Convention (Typ)); | |
632 | Set_Depends_On_Private (Itype, Has_Private_Component (Typ)); | |
633 | Set_Etype (Itype, Base_Type (Typ)); | |
634 | Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ)); | |
635 | Set_Is_Aliased (Itype, Is_Aliased (Typ)); | |
a517d6c1 | 636 | Set_Is_Independent (Itype, Is_Independent (Typ)); |
b87971f3 | 637 | Set_Depends_On_Private (Itype, Depends_On_Private (Typ)); |
996ae0b0 | 638 | |
fbf5a39b AC |
639 | Copy_Suppress_Status (Index_Check, Typ, Itype); |
640 | Copy_Suppress_Status (Length_Check, Typ, Itype); | |
641 | ||
996ae0b0 RK |
642 | Set_First_Index (Itype, First (Index_Constraints)); |
643 | Set_Is_Constrained (Itype, True); | |
644 | Set_Is_Internal (Itype, True); | |
996ae0b0 | 645 | |
619bfd9f ES |
646 | if Has_Predicates (Typ) then |
647 | Set_Has_Predicates (Itype); | |
648 | ||
a096f12e ES |
649 | -- If the base type has a predicate, capture the predicated parent |
650 | -- or the existing predicate function for SPARK use. | |
651 | ||
619bfd9f ES |
652 | if Present (Predicate_Function (Typ)) then |
653 | Set_Predicate_Function (Itype, Predicate_Function (Typ)); | |
a096f12e ES |
654 | |
655 | elsif Is_Itype (Typ) then | |
619bfd9f | 656 | Set_Predicated_Parent (Itype, Predicated_Parent (Typ)); |
a096f12e ES |
657 | |
658 | else | |
659 | Set_Predicated_Parent (Itype, Typ); | |
619bfd9f ES |
660 | end if; |
661 | end if; | |
662 | ||
996ae0b0 | 663 | -- A simple optimization: purely positional aggregates of static |
b87971f3 AC |
664 | -- components should be passed to gigi unexpanded whenever possible, and |
665 | -- regardless of the staticness of the bounds themselves. Subsequent | |
666 | -- checks in exp_aggr verify that type is not packed, etc. | |
996ae0b0 | 667 | |
58009744 AC |
668 | Set_Size_Known_At_Compile_Time |
669 | (Itype, | |
8133b9d1 ES |
670 | Is_Fully_Positional |
671 | and then Comes_From_Source (N) | |
672 | and then Size_Known_At_Compile_Time (Component_Type (Typ))); | |
996ae0b0 | 673 | |
b87971f3 | 674 | -- We always need a freeze node for a packed array subtype, so that we |
8ca597af | 675 | -- can build the Packed_Array_Impl_Type corresponding to the subtype. If |
b87971f3 AC |
676 | -- expansion is disabled, the packed array subtype is not built, and we |
677 | -- must not generate a freeze node for the type, or else it will appear | |
678 | -- incomplete to gigi. | |
996ae0b0 | 679 | |
b87971f3 AC |
680 | if Is_Packed (Itype) |
681 | and then not In_Spec_Expression | |
996ae0b0 RK |
682 | and then Expander_Active |
683 | then | |
684 | Freeze_Itype (Itype, N); | |
685 | end if; | |
686 | ||
687 | return Itype; | |
688 | end Array_Aggr_Subtype; | |
689 | ||
690 | -------------------------------- | |
691 | -- Check_Misspelled_Component -- | |
692 | -------------------------------- | |
693 | ||
694 | procedure Check_Misspelled_Component | |
9c290e69 PO |
695 | (Elements : Elist_Id; |
696 | Component : Node_Id) | |
996ae0b0 RK |
697 | is |
698 | Max_Suggestions : constant := 2; | |
699 | ||
700 | Nr_Of_Suggestions : Natural := 0; | |
701 | Suggestion_1 : Entity_Id := Empty; | |
702 | Suggestion_2 : Entity_Id := Empty; | |
703 | Component_Elmt : Elmt_Id; | |
704 | ||
705 | begin | |
b87971f3 | 706 | -- All the components of List are matched against Component and a count |
e49de265 | 707 | -- is maintained of possible misspellings. When at the end of the |
a90bd866 | 708 | -- analysis there are one or two (not more) possible misspellings, |
e49de265 | 709 | -- these misspellings will be suggested as possible corrections. |
996ae0b0 | 710 | |
c80d4855 RD |
711 | Component_Elmt := First_Elmt (Elements); |
712 | while Nr_Of_Suggestions <= Max_Suggestions | |
713 | and then Present (Component_Elmt) | |
714 | loop | |
715 | if Is_Bad_Spelling_Of | |
716 | (Chars (Node (Component_Elmt)), | |
717 | Chars (Component)) | |
718 | then | |
719 | Nr_Of_Suggestions := Nr_Of_Suggestions + 1; | |
996ae0b0 | 720 | |
c80d4855 RD |
721 | case Nr_Of_Suggestions is |
722 | when 1 => Suggestion_1 := Node (Component_Elmt); | |
723 | when 2 => Suggestion_2 := Node (Component_Elmt); | |
e49de265 | 724 | when others => null; |
c80d4855 RD |
725 | end case; |
726 | end if; | |
996ae0b0 | 727 | |
c80d4855 RD |
728 | Next_Elmt (Component_Elmt); |
729 | end loop; | |
996ae0b0 | 730 | |
c80d4855 | 731 | -- Report at most two suggestions |
996ae0b0 | 732 | |
c80d4855 | 733 | if Nr_Of_Suggestions = 1 then |
4e7a4f6e | 734 | Error_Msg_NE -- CODEFIX |
c80d4855 | 735 | ("\possible misspelling of&", Component, Suggestion_1); |
996ae0b0 | 736 | |
c80d4855 RD |
737 | elsif Nr_Of_Suggestions = 2 then |
738 | Error_Msg_Node_2 := Suggestion_2; | |
4e7a4f6e | 739 | Error_Msg_NE -- CODEFIX |
c80d4855 RD |
740 | ("\possible misspelling of& or&", Component, Suggestion_1); |
741 | end if; | |
996ae0b0 RK |
742 | end Check_Misspelled_Component; |
743 | ||
ca44152f ES |
744 | ---------------------------------------- |
745 | -- Check_Expr_OK_In_Limited_Aggregate -- | |
746 | ---------------------------------------- | |
747 | ||
748 | procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is | |
749 | begin | |
750 | if Is_Limited_Type (Etype (Expr)) | |
751 | and then Comes_From_Source (Expr) | |
ca44152f | 752 | then |
36428cc4 AC |
753 | if In_Instance_Body or else In_Inlined_Body then |
754 | null; | |
755 | ||
756 | elsif not OK_For_Limited_Init (Etype (Expr), Expr) then | |
757 | Error_Msg_N | |
758 | ("initialization not allowed for limited types", Expr); | |
ca44152f ES |
759 | Explain_Limited_Type (Etype (Expr), Expr); |
760 | end if; | |
761 | end if; | |
762 | end Check_Expr_OK_In_Limited_Aggregate; | |
763 | ||
0191a24e SB |
764 | -------------------- |
765 | -- Is_Deep_Choice -- | |
766 | -------------------- | |
767 | ||
768 | function Is_Deep_Choice | |
769 | (Choice : Node_Id; | |
770 | Aggr_Type : Type_Kind_Id) return Boolean | |
771 | is | |
772 | Pref : Node_Id := Choice; | |
773 | begin | |
774 | while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop | |
775 | Pref := Prefix (Pref); | |
776 | end loop; | |
777 | ||
778 | if Is_Array_Type (Aggr_Type) then | |
779 | return Paren_Count (Pref) > 0 | |
780 | and then Pref /= Choice; | |
781 | else | |
782 | return Pref /= Choice; | |
783 | end if; | |
784 | end Is_Deep_Choice; | |
785 | ||
9f90d123 AC |
786 | ------------------------- |
787 | -- Is_Others_Aggregate -- | |
788 | ------------------------- | |
789 | ||
790 | function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is | |
27c3d986 EB |
791 | Assoc : constant List_Id := Component_Associations (Aggr); |
792 | ||
9f90d123 AC |
793 | begin |
794 | return No (Expressions (Aggr)) | |
27c3d986 | 795 | and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice; |
9f90d123 AC |
796 | end Is_Others_Aggregate; |
797 | ||
0191a24e SB |
798 | ----------------------------------- |
799 | -- Is_Root_Prefix_Of_Deep_Choice -- | |
800 | ----------------------------------- | |
801 | ||
802 | function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is | |
803 | begin | |
804 | return Paren_Count (Pref) > 0 | |
805 | or else Nkind (Pref) not in N_Indexed_Component | |
806 | | N_Selected_Component; | |
807 | end Is_Root_Prefix_Of_Deep_Choice; | |
808 | ||
27c3d986 EB |
809 | ------------------------- |
810 | -- Is_Single_Aggregate -- | |
811 | ------------------------- | |
812 | ||
813 | function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is | |
814 | Assoc : constant List_Id := Component_Associations (Aggr); | |
815 | ||
816 | begin | |
817 | return No (Expressions (Aggr)) | |
818 | and then No (Next (First (Assoc))) | |
819 | and then No (Next (First (Choice_List (First (Assoc))))); | |
820 | end Is_Single_Aggregate; | |
821 | ||
10c257af ES |
822 | ----------------------- |
823 | -- Is_Null_Aggregate -- | |
824 | ----------------------- | |
825 | ||
826 | function Is_Null_Aggregate (N : Node_Id) return Boolean is | |
827 | begin | |
828 | return Ada_Version >= Ada_2022 | |
829 | and then Is_Homogeneous_Aggregate (N) | |
830 | and then Is_Empty_List (Expressions (N)) | |
831 | and then Is_Empty_List (Component_Associations (N)); | |
832 | end Is_Null_Aggregate; | |
833 | ||
834 | ---------------------------------------- | |
835 | -- Is_Null_Array_Aggregate_High_Bound -- | |
836 | ---------------------------------------- | |
837 | ||
838 | function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is | |
839 | Original_N : constant Node_Id := Original_Node (N); | |
840 | begin | |
841 | return Ada_Version >= Ada_2022 | |
842 | and then not Comes_From_Source (Original_N) | |
843 | and then Nkind (Original_N) = N_Attribute_Reference | |
844 | and then | |
845 | Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred | |
846 | and then Nkind (Parent (N)) in N_Range | N_Op_Le | |
847 | and then not Comes_From_Source (Parent (N)); | |
848 | end Is_Null_Array_Aggregate_High_Bound; | |
849 | ||
996ae0b0 RK |
850 | -------------------------------- |
851 | -- Make_String_Into_Aggregate -- | |
852 | -------------------------------- | |
853 | ||
854 | procedure Make_String_Into_Aggregate (N : Node_Id) is | |
fbf5a39b | 855 | Exprs : constant List_Id := New_List; |
996ae0b0 | 856 | Loc : constant Source_Ptr := Sloc (N); |
996ae0b0 RK |
857 | Str : constant String_Id := Strval (N); |
858 | Strlen : constant Nat := String_Length (Str); | |
fbf5a39b AC |
859 | C : Char_Code; |
860 | C_Node : Node_Id; | |
861 | New_N : Node_Id; | |
862 | P : Source_Ptr; | |
996ae0b0 RK |
863 | |
864 | begin | |
fbf5a39b | 865 | P := Loc + 1; |
95349238 | 866 | for J in 1 .. Strlen loop |
996ae0b0 RK |
867 | C := Get_String_Char (Str, J); |
868 | Set_Character_Literal_Name (C); | |
869 | ||
82c80734 RD |
870 | C_Node := |
871 | Make_Character_Literal (P, | |
872 | Chars => Name_Find, | |
873 | Char_Literal_Value => UI_From_CC (C)); | |
996ae0b0 | 874 | Set_Etype (C_Node, Any_Character); |
996ae0b0 RK |
875 | Append_To (Exprs, C_Node); |
876 | ||
877 | P := P + 1; | |
b87971f3 | 878 | -- Something special for wide strings??? |
996ae0b0 RK |
879 | end loop; |
880 | ||
881 | New_N := Make_Aggregate (Loc, Expressions => Exprs); | |
882 | Set_Analyzed (New_N); | |
883 | Set_Etype (New_N, Any_Composite); | |
884 | ||
885 | Rewrite (N, New_N); | |
886 | end Make_String_Into_Aggregate; | |
887 | ||
888 | ----------------------- | |
889 | -- Resolve_Aggregate -- | |
890 | ----------------------- | |
891 | ||
892 | procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fb00cc70 | 893 | Loc : constant Source_Ptr := Sloc (N); |
996ae0b0 RK |
894 | |
895 | Aggr_Subtyp : Entity_Id; | |
896 | -- The actual aggregate subtype. This is not necessarily the same as Typ | |
897 | -- which is the subtype of the context in which the aggregate was found. | |
898 | ||
66b69678 AC |
899 | Others_Box : Boolean := False; |
900 | -- Set to True if N represents a simple aggregate with only | |
901 | -- (others => <>), not nested as part of another aggregate. | |
902 | ||
c061e99b EB |
903 | function Is_Full_Access_Aggregate (N : Node_Id) return Boolean; |
904 | -- If a full access object is initialized with an aggregate or is | |
905 | -- assigned an aggregate, we have to prevent a piecemeal access or | |
906 | -- assignment to the object, even if the aggregate is to be expanded. | |
907 | -- We create a temporary for the aggregate, and assign the temporary | |
908 | -- instead, so that the back end can generate an atomic move for it. | |
909 | -- This is only done in the context of an object declaration or an | |
910 | -- assignment. Function is a noop and returns false in other contexts. | |
911 | ||
66b69678 AC |
912 | function Within_Aggregate (N : Node_Id) return Boolean; |
913 | -- Return True if N is part of an N_Aggregate | |
914 | ||
c061e99b EB |
915 | ------------------------------ |
916 | -- Is_Full_Access_Aggregate -- | |
917 | ------------------------------ | |
918 | ||
919 | function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is | |
920 | Loc : constant Source_Ptr := Sloc (N); | |
921 | ||
922 | New_N : Node_Id; | |
923 | Par : Node_Id; | |
924 | Temp : Entity_Id; | |
925 | Typ : Entity_Id; | |
926 | ||
927 | begin | |
928 | Par := Parent (N); | |
929 | ||
930 | -- Aggregate may be qualified, so find outer context | |
931 | ||
932 | if Nkind (Par) = N_Qualified_Expression then | |
933 | Par := Parent (Par); | |
934 | end if; | |
935 | ||
936 | if not Comes_From_Source (Par) then | |
937 | return False; | |
938 | end if; | |
939 | ||
940 | case Nkind (Par) is | |
941 | when N_Assignment_Statement => | |
942 | Typ := Etype (Name (Par)); | |
943 | ||
944 | if not Is_Full_Access (Typ) | |
945 | and then not Is_Full_Access_Object (Name (Par)) | |
946 | then | |
947 | return False; | |
948 | end if; | |
949 | ||
950 | when N_Object_Declaration => | |
951 | Typ := Etype (Defining_Identifier (Par)); | |
952 | ||
953 | if not Is_Full_Access (Typ) | |
954 | and then not Is_Full_Access (Defining_Identifier (Par)) | |
955 | then | |
956 | return False; | |
957 | end if; | |
958 | ||
959 | when others => | |
960 | return False; | |
961 | end case; | |
962 | ||
963 | Temp := Make_Temporary (Loc, 'T', N); | |
964 | New_N := | |
965 | Make_Object_Declaration (Loc, | |
966 | Defining_Identifier => Temp, | |
967 | Constant_Present => True, | |
968 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
969 | Expression => Relocate_Node (N)); | |
970 | Insert_Action (Par, New_N); | |
971 | ||
972 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
973 | Analyze_And_Resolve (N, Typ); | |
974 | ||
975 | return True; | |
976 | end Is_Full_Access_Aggregate; | |
977 | ||
66b69678 AC |
978 | ---------------------- |
979 | -- Within_Aggregate -- | |
980 | ---------------------- | |
981 | ||
982 | function Within_Aggregate (N : Node_Id) return Boolean is | |
983 | P : Node_Id := Parent (N); | |
984 | begin | |
985 | while Present (P) loop | |
986 | if Nkind (P) = N_Aggregate then | |
987 | return True; | |
988 | end if; | |
989 | ||
990 | P := Parent (P); | |
991 | end loop; | |
992 | ||
993 | return False; | |
994 | end Within_Aggregate; | |
995 | ||
fb00cc70 ES |
996 | -- Start of processing for Resolve_Aggregate |
997 | ||
996ae0b0 | 998 | begin |
6d2a1120 RD |
999 | -- Ignore junk empty aggregate resulting from parser error |
1000 | ||
1001 | if No (Expressions (N)) | |
1002 | and then No (Component_Associations (N)) | |
1003 | and then not Null_Record_Present (N) | |
1004 | then | |
1005 | return; | |
c061e99b EB |
1006 | |
1007 | -- If the aggregate is assigned to a full access variable, we have | |
1008 | -- to prevent a piecemeal assignment even if the aggregate is to be | |
1009 | -- expanded. We create a temporary for the aggregate, and assign the | |
1010 | -- temporary instead, so that the back end can generate an atomic move | |
1011 | -- for it. This is properly an expansion activity but it must be done | |
1012 | -- before resolution because aggregate resolution cannot be done twice. | |
1013 | ||
1014 | elsif Expander_Active and then Is_Full_Access_Aggregate (N) then | |
1015 | return; | |
6d2a1120 RD |
1016 | end if; |
1017 | ||
0180fd26 AC |
1018 | -- If the aggregate has box-initialized components, its type must be |
1019 | -- frozen so that initialization procedures can properly be called | |
64ac53f4 | 1020 | -- in the resolution that follows. The replacement of boxes with |
0180fd26 | 1021 | -- initialization calls is properly an expansion activity but it must |
f5da7a97 | 1022 | -- be done during resolution. |
0180fd26 AC |
1023 | |
1024 | if Expander_Active | |
f5da7a97 | 1025 | and then Present (Component_Associations (N)) |
0180fd26 AC |
1026 | then |
1027 | declare | |
66b69678 AC |
1028 | Comp : Node_Id; |
1029 | First_Comp : Boolean := True; | |
0180fd26 AC |
1030 | |
1031 | begin | |
1032 | Comp := First (Component_Associations (N)); | |
1033 | while Present (Comp) loop | |
1034 | if Box_Present (Comp) then | |
66b69678 AC |
1035 | if First_Comp |
1036 | and then No (Expressions (N)) | |
1037 | and then Nkind (First (Choices (Comp))) = N_Others_Choice | |
1038 | and then not Within_Aggregate (N) | |
1039 | then | |
1040 | Others_Box := True; | |
1041 | end if; | |
1042 | ||
0180fd26 AC |
1043 | Insert_Actions (N, Freeze_Entity (Typ, N)); |
1044 | exit; | |
1045 | end if; | |
fe0ec02f | 1046 | |
66b69678 | 1047 | First_Comp := False; |
0180fd26 AC |
1048 | Next (Comp); |
1049 | end loop; | |
1050 | end; | |
1051 | end if; | |
1052 | ||
fbf5a39b | 1053 | -- Check for aggregates not allowed in configurable run-time mode. |
b87971f3 AC |
1054 | -- We allow all cases of aggregates that do not come from source, since |
1055 | -- these are all assumed to be small (e.g. bounds of a string literal). | |
1056 | -- We also allow aggregates of types we know to be small. | |
fbf5a39b AC |
1057 | |
1058 | if not Support_Aggregates_On_Target | |
1059 | and then Comes_From_Source (N) | |
c7c7dd3a EB |
1060 | and then (not Known_Static_Esize (Typ) |
1061 | or else Esize (Typ) > System_Max_Integer_Size) | |
fbf5a39b AC |
1062 | then |
1063 | Error_Msg_CRT ("aggregate", N); | |
1064 | end if; | |
996ae0b0 | 1065 | |
0ab80019 | 1066 | -- Ada 2005 (AI-287): Limited aggregates allowed |
579fda56 | 1067 | |
17fa48b1 | 1068 | -- In an instance, ignore aggregate subcomponents that may be limited, |
67645bde AC |
1069 | -- because they originate in view conflicts. If the original aggregate |
1070 | -- is legal and the actuals are legal, the aggregate itself is legal. | |
19f0526a | 1071 | |
67645bde AC |
1072 | if Is_Limited_Type (Typ) |
1073 | and then Ada_Version < Ada_2005 | |
1074 | and then not In_Instance | |
1075 | then | |
fbf5a39b AC |
1076 | Error_Msg_N ("aggregate type cannot be limited", N); |
1077 | Explain_Limited_Type (Typ, N); | |
996ae0b0 RK |
1078 | |
1079 | elsif Is_Class_Wide_Type (Typ) then | |
1080 | Error_Msg_N ("type of aggregate cannot be class-wide", N); | |
1081 | ||
1082 | elsif Typ = Any_String | |
1083 | or else Typ = Any_Composite | |
1084 | then | |
1085 | Error_Msg_N ("no unique type for aggregate", N); | |
1086 | Set_Etype (N, Any_Composite); | |
1087 | ||
1088 | elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then | |
1089 | Error_Msg_N ("null record forbidden in array aggregate", N); | |
1090 | ||
11f89257 | 1091 | elsif Has_Aspect (Typ, Aspect_Aggregate) |
3bb4836f | 1092 | and then Ekind (Typ) /= E_Record_Type |
81e68a19 | 1093 | and then Ada_Version >= Ada_2022 |
3bb4836f | 1094 | then |
b045f2f2 MP |
1095 | -- Check for Ada 2022 and () aggregate. |
1096 | ||
1097 | if not Is_Homogeneous_Aggregate (N) then | |
1098 | Error_Msg_N ("container aggregate must use '['], not ()", N); | |
1099 | end if; | |
1100 | ||
3bb4836f ES |
1101 | Resolve_Container_Aggregate (N, Typ); |
1102 | ||
67138e09 JM |
1103 | -- Check Ada 2022 empty aggregate [] initializing a record type that has |
1104 | -- aspect aggregate; the empty aggregate will be expanded into a call to | |
1105 | -- the empty function specified in the aspect aggregate. | |
1106 | ||
1107 | elsif Has_Aspect (Typ, Aspect_Aggregate) | |
1108 | and then Ekind (Typ) = E_Record_Type | |
1109 | and then Is_Homogeneous_Aggregate (N) | |
1110 | and then Is_Empty_List (Expressions (N)) | |
1111 | and then Is_Empty_List (Component_Associations (N)) | |
1112 | and then Ada_Version >= Ada_2022 | |
1113 | then | |
1114 | Resolve_Container_Aggregate (N, Typ); | |
1115 | ||
996ae0b0 RK |
1116 | elsif Is_Record_Type (Typ) then |
1117 | Resolve_Record_Aggregate (N, Typ); | |
1118 | ||
1119 | elsif Is_Array_Type (Typ) then | |
1120 | ||
54740d7d AC |
1121 | -- First a special test, for the case of a positional aggregate of |
1122 | -- characters which can be replaced by a string literal. | |
ca44152f | 1123 | |
54740d7d AC |
1124 | -- Do not perform this transformation if this was a string literal |
1125 | -- to start with, whose components needed constraint checks, or if | |
1126 | -- the component type is non-static, because it will require those | |
1127 | -- checks and be transformed back into an aggregate. If the index | |
1128 | -- type is not Integer the aggregate may represent a user-defined | |
1129 | -- string type but the context might need the original type so we | |
1130 | -- do not perform the transformation at this point. | |
996ae0b0 RK |
1131 | |
1132 | if Number_Dimensions (Typ) = 1 | |
ca44152f | 1133 | and then Is_Standard_Character_Type (Component_Type (Typ)) |
996ae0b0 RK |
1134 | and then No (Component_Associations (N)) |
1135 | and then not Is_Limited_Composite (Typ) | |
1136 | and then not Is_Private_Composite (Typ) | |
1137 | and then not Is_Bit_Packed_Array (Typ) | |
1138 | and then Nkind (Original_Node (Parent (N))) /= N_String_Literal | |
edab6088 | 1139 | and then Is_OK_Static_Subtype (Component_Type (Typ)) |
54740d7d AC |
1140 | and then Base_Type (Etype (First_Index (Typ))) = |
1141 | Base_Type (Standard_Integer) | |
996ae0b0 RK |
1142 | then |
1143 | declare | |
1144 | Expr : Node_Id; | |
1145 | ||
1146 | begin | |
1147 | Expr := First (Expressions (N)); | |
1148 | while Present (Expr) loop | |
1149 | exit when Nkind (Expr) /= N_Character_Literal; | |
1150 | Next (Expr); | |
1151 | end loop; | |
1152 | ||
1153 | if No (Expr) then | |
1154 | Start_String; | |
1155 | ||
1156 | Expr := First (Expressions (N)); | |
1157 | while Present (Expr) loop | |
82c80734 | 1158 | Store_String_Char (UI_To_CC (Char_Literal_Value (Expr))); |
996ae0b0 RK |
1159 | Next (Expr); |
1160 | end loop; | |
1161 | ||
f915704f | 1162 | Rewrite (N, Make_String_Literal (Loc, End_String)); |
996ae0b0 RK |
1163 | |
1164 | Analyze_And_Resolve (N, Typ); | |
1165 | return; | |
1166 | end if; | |
1167 | end; | |
1168 | end if; | |
1169 | ||
1170 | -- Here if we have a real aggregate to deal with | |
1171 | ||
1172 | Array_Aggregate : declare | |
1173 | Aggr_Resolved : Boolean; | |
fbf5a39b | 1174 | Aggr_Typ : constant Entity_Id := Etype (Typ); |
b87971f3 AC |
1175 | -- This is the unconstrained array type, which is the type against |
1176 | -- which the aggregate is to be resolved. Typ itself is the array | |
1177 | -- type of the context which may not be the same subtype as the | |
1178 | -- subtype for the final aggregate. | |
996ae0b0 | 1179 | |
10c257af ES |
1180 | Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N); |
1181 | ||
996ae0b0 | 1182 | begin |
f915704f | 1183 | -- In the following we determine whether an OTHERS choice is |
996ae0b0 RK |
1184 | -- allowed inside the array aggregate. The test checks the context |
1185 | -- in which the array aggregate occurs. If the context does not | |
f915704f | 1186 | -- permit it, or the aggregate type is unconstrained, an OTHERS |
56e94186 AC |
1187 | -- choice is not allowed (except that it is always allowed on the |
1188 | -- right-hand side of an assignment statement; in this case the | |
f18344b7 BD |
1189 | -- constrainedness of the type doesn't matter, because an array |
1190 | -- object is always constrained). | |
d8387153 ES |
1191 | |
1192 | -- If expansion is disabled (generic context, or semantics-only | |
b87971f3 AC |
1193 | -- mode) actual subtypes cannot be constructed, and the type of an |
1194 | -- object may be its unconstrained nominal type. However, if the | |
f18344b7 BD |
1195 | -- context is an assignment statement, OTHERS is allowed, because |
1196 | -- the target of the assignment will have a constrained subtype | |
1197 | -- when fully compiled. Ditto if the context is an initialization | |
1198 | -- procedure where a component may have a predicate function that | |
1199 | -- carries the base type. | |
d8387153 | 1200 | |
996ae0b0 RK |
1201 | -- Note that there is no node for Explicit_Actual_Parameter. |
1202 | -- To test for this context we therefore have to test for node | |
1203 | -- N_Parameter_Association which itself appears only if there is a | |
1204 | -- formal parameter. Consequently we also need to test for | |
1205 | -- N_Procedure_Call_Statement or N_Function_Call. | |
1206 | ||
5f6fb720 AC |
1207 | -- The context may be an N_Reference node, created by expansion. |
1208 | -- Legality of the others clause was established in the source, | |
1209 | -- so the context is legal. | |
1210 | ||
b87971f3 | 1211 | Set_Etype (N, Aggr_Typ); -- May be overridden later on |
c45b6ae0 | 1212 | |
10c257af ES |
1213 | if Is_Null_Aggr then |
1214 | Set_Etype (N, Typ); | |
1215 | Aggr_Resolved := Resolve_Null_Array_Aggregate (N); | |
1216 | ||
1217 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
ffdd5248 | 1218 | or else Inside_Init_Proc |
e917aec2 | 1219 | or else (Is_Constrained (Typ) |
4a08c95c AC |
1220 | and then Nkind (Parent (N)) in |
1221 | N_Parameter_Association | |
1222 | | N_Function_Call | |
1223 | | N_Procedure_Call_Statement | |
1224 | | N_Generic_Association | |
1225 | | N_Formal_Object_Declaration | |
1226 | | N_Simple_Return_Statement | |
1227 | | N_Object_Declaration | |
1228 | | N_Component_Declaration | |
1229 | | N_Parameter_Specification | |
1230 | | N_Qualified_Expression | |
4590d973 | 1231 | | N_Unchecked_Type_Conversion |
4a08c95c AC |
1232 | | N_Reference |
1233 | | N_Aggregate | |
1234 | | N_Extension_Aggregate | |
1235 | | N_Component_Association | |
1236 | | N_Case_Expression_Alternative | |
1237 | | N_If_Expression | |
1238 | | N_Expression_With_Actions) | |
996ae0b0 RK |
1239 | then |
1240 | Aggr_Resolved := | |
1241 | Resolve_Array_Aggregate | |
1242 | (N, | |
1243 | Index => First_Index (Aggr_Typ), | |
1244 | Index_Constr => First_Index (Typ), | |
1245 | Component_Typ => Component_Type (Typ), | |
1246 | Others_Allowed => True); | |
996ae0b0 RK |
1247 | else |
1248 | Aggr_Resolved := | |
1249 | Resolve_Array_Aggregate | |
1250 | (N, | |
1251 | Index => First_Index (Aggr_Typ), | |
1252 | Index_Constr => First_Index (Aggr_Typ), | |
1253 | Component_Typ => Component_Type (Typ), | |
1254 | Others_Allowed => False); | |
1255 | end if; | |
1256 | ||
1257 | if not Aggr_Resolved then | |
f5afb270 AC |
1258 | |
1259 | -- A parenthesized expression may have been intended as an | |
1260 | -- aggregate, leading to a type error when analyzing the | |
1261 | -- component. This can also happen for a nested component | |
1262 | -- (see Analyze_Aggr_Expr). | |
1263 | ||
1264 | if Paren_Count (N) > 0 then | |
1265 | Error_Msg_N | |
1266 | ("positional aggregate cannot have one component", N); | |
1267 | end if; | |
1268 | ||
996ae0b0 | 1269 | Aggr_Subtyp := Any_Composite; |
e917aec2 | 1270 | |
996ae0b0 RK |
1271 | else |
1272 | Aggr_Subtyp := Array_Aggr_Subtype (N, Typ); | |
1273 | end if; | |
1274 | ||
1275 | Set_Etype (N, Aggr_Subtyp); | |
1276 | end Array_Aggregate; | |
1277 | ||
d8387153 ES |
1278 | elsif Is_Private_Type (Typ) |
1279 | and then Present (Full_View (Typ)) | |
8256c1bf | 1280 | and then (In_Inlined_Body or In_Instance_Body) |
d8387153 ES |
1281 | and then Is_Composite_Type (Full_View (Typ)) |
1282 | then | |
1283 | Resolve (N, Full_View (Typ)); | |
1284 | ||
996ae0b0 RK |
1285 | else |
1286 | Error_Msg_N ("illegal context for aggregate", N); | |
996ae0b0 RK |
1287 | end if; |
1288 | ||
b87971f3 AC |
1289 | -- If we can determine statically that the evaluation of the aggregate |
1290 | -- raises Constraint_Error, then replace the aggregate with an | |
1291 | -- N_Raise_Constraint_Error node, but set the Etype to the right | |
1292 | -- aggregate subtype. Gigi needs this. | |
996ae0b0 RK |
1293 | |
1294 | if Raises_Constraint_Error (N) then | |
1295 | Aggr_Subtyp := Etype (N); | |
07fc65c4 | 1296 | Rewrite (N, |
bd65a2d7 | 1297 | Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed)); |
996ae0b0 RK |
1298 | Set_Raises_Constraint_Error (N); |
1299 | Set_Etype (N, Aggr_Subtyp); | |
1300 | Set_Analyzed (N); | |
1301 | end if; | |
d3820795 | 1302 | |
66b69678 AC |
1303 | if Warn_On_No_Value_Assigned |
1304 | and then Others_Box | |
1305 | and then not Is_Fully_Initialized_Type (Etype (N)) | |
1306 | then | |
1307 | Error_Msg_N ("?v?aggregate not fully initialized", N); | |
1308 | end if; | |
1309 | ||
22e89283 | 1310 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1311 | end Resolve_Aggregate; |
1312 | ||
1313 | ----------------------------- | |
1314 | -- Resolve_Array_Aggregate -- | |
1315 | ----------------------------- | |
1316 | ||
1317 | function Resolve_Array_Aggregate | |
1318 | (N : Node_Id; | |
1319 | Index : Node_Id; | |
1320 | Index_Constr : Node_Id; | |
1321 | Component_Typ : Entity_Id; | |
ca44152f | 1322 | Others_Allowed : Boolean) return Boolean |
996ae0b0 RK |
1323 | is |
1324 | Loc : constant Source_Ptr := Sloc (N); | |
1325 | ||
1326 | Failure : constant Boolean := False; | |
1327 | Success : constant Boolean := True; | |
1328 | ||
1329 | Index_Typ : constant Entity_Id := Etype (Index); | |
1330 | Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ); | |
1331 | Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ); | |
b87971f3 AC |
1332 | -- The type of the index corresponding to the array sub-aggregate along |
1333 | -- with its low and upper bounds. | |
996ae0b0 RK |
1334 | |
1335 | Index_Base : constant Entity_Id := Base_Type (Index_Typ); | |
1336 | Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base); | |
1337 | Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base); | |
b87971f3 | 1338 | -- Ditto for the base type |
996ae0b0 | 1339 | |
ef74daea AC |
1340 | Others_Present : Boolean := False; |
1341 | ||
1342 | Nb_Choices : Nat := 0; | |
1343 | -- Contains the overall number of named choices in this sub-aggregate | |
1344 | ||
996ae0b0 RK |
1345 | function Add (Val : Uint; To : Node_Id) return Node_Id; |
1346 | -- Creates a new expression node where Val is added to expression To. | |
1347 | -- Tries to constant fold whenever possible. To must be an already | |
1348 | -- analyzed expression. | |
1349 | ||
1350 | procedure Check_Bound (BH : Node_Id; AH : in out Node_Id); | |
f915704f AC |
1351 | -- Checks that AH (the upper bound of an array aggregate) is less than |
1352 | -- or equal to BH (the upper bound of the index base type). If the check | |
1353 | -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is | |
1354 | -- set, and AH is replaced with a duplicate of BH. | |
996ae0b0 RK |
1355 | |
1356 | procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id); | |
1357 | -- Checks that range AL .. AH is compatible with range L .. H. Emits a | |
b87971f3 | 1358 | -- warning if not and sets the Raises_Constraint_Error flag in N. |
996ae0b0 RK |
1359 | |
1360 | procedure Check_Length (L, H : Node_Id; Len : Uint); | |
1361 | -- Checks that range L .. H contains at least Len elements. Emits a | |
b87971f3 | 1362 | -- warning if not and sets the Raises_Constraint_Error flag in N. |
996ae0b0 RK |
1363 | |
1364 | function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean; | |
ec53a6da | 1365 | -- Returns True if range L .. H is dynamic or null |
996ae0b0 RK |
1366 | |
1367 | procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean); | |
1368 | -- Given expression node From, this routine sets OK to False if it | |
1369 | -- cannot statically evaluate From. Otherwise it stores this static | |
1370 | -- value into Value. | |
1371 | ||
1372 | function Resolve_Aggr_Expr | |
1373 | (Expr : Node_Id; | |
ca44152f | 1374 | Single_Elmt : Boolean) return Boolean; |
12a13f01 | 1375 | -- Resolves aggregate expression Expr. Returns False if resolution |
996ae0b0 | 1376 | -- fails. If Single_Elmt is set to False, the expression Expr may be |
b87971f3 | 1377 | -- used to initialize several array aggregate elements (this can happen |
f915704f | 1378 | -- for discrete choices such as "L .. H => Expr" or the OTHERS choice). |
b87971f3 AC |
1379 | -- In this event we do not resolve Expr unless expansion is disabled. |
1380 | -- To know why, see the DELAYED COMPONENT RESOLUTION note above. | |
ca5af305 | 1381 | -- |
be54247f PT |
1382 | -- NOTE: In the case of "... => <>", we pass the N_Component_Association |
1383 | -- node as Expr, since there is no Expression and we need a Sloc for the | |
1384 | -- error message. | |
996ae0b0 | 1385 | |
ef74daea AC |
1386 | procedure Resolve_Iterated_Component_Association |
1387 | (N : Node_Id; | |
1388 | Index_Typ : Entity_Id); | |
1389 | -- For AI12-061 | |
1390 | ||
65ab836d JM |
1391 | procedure Warn_On_Null_Component_Association (Expr : Node_Id); |
1392 | -- Expr is either a conditional expression or a case expression of an | |
1393 | -- iterated component association initializing the aggregate N with | |
1394 | -- components that can never be null. Report warning on associations | |
1395 | -- that may initialize some component with a null value. | |
1396 | ||
996ae0b0 RK |
1397 | --------- |
1398 | -- Add -- | |
1399 | --------- | |
1400 | ||
1401 | function Add (Val : Uint; To : Node_Id) return Node_Id is | |
1402 | Expr_Pos : Node_Id; | |
1403 | Expr : Node_Id; | |
1404 | To_Pos : Node_Id; | |
1405 | ||
1406 | begin | |
1407 | if Raises_Constraint_Error (To) then | |
1408 | return To; | |
1409 | end if; | |
1410 | ||
1411 | -- First test if we can do constant folding | |
1412 | ||
1413 | if Compile_Time_Known_Value (To) | |
1414 | or else Nkind (To) = N_Integer_Literal | |
1415 | then | |
1416 | Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val); | |
1417 | Set_Is_Static_Expression (Expr_Pos); | |
1418 | Set_Etype (Expr_Pos, Etype (To)); | |
1419 | Set_Analyzed (Expr_Pos, Analyzed (To)); | |
1420 | ||
1421 | if not Is_Enumeration_Type (Index_Typ) then | |
1422 | Expr := Expr_Pos; | |
1423 | ||
1424 | -- If we are dealing with enumeration return | |
1425 | -- Index_Typ'Val (Expr_Pos) | |
1426 | ||
1427 | else | |
1428 | Expr := | |
1429 | Make_Attribute_Reference | |
1430 | (Loc, | |
e4494292 | 1431 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1432 | Attribute_Name => Name_Val, |
1433 | Expressions => New_List (Expr_Pos)); | |
1434 | end if; | |
1435 | ||
1436 | return Expr; | |
1437 | end if; | |
1438 | ||
1439 | -- If we are here no constant folding possible | |
1440 | ||
1441 | if not Is_Enumeration_Type (Index_Base) then | |
1442 | Expr := | |
1443 | Make_Op_Add (Loc, | |
f915704f AC |
1444 | Left_Opnd => Duplicate_Subexpr (To), |
1445 | Right_Opnd => Make_Integer_Literal (Loc, Val)); | |
996ae0b0 RK |
1446 | |
1447 | -- If we are dealing with enumeration return | |
1448 | -- Index_Typ'Val (Index_Typ'Pos (To) + Val) | |
1449 | ||
1450 | else | |
1451 | To_Pos := | |
1452 | Make_Attribute_Reference | |
1453 | (Loc, | |
e4494292 | 1454 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1455 | Attribute_Name => Name_Pos, |
1456 | Expressions => New_List (Duplicate_Subexpr (To))); | |
1457 | ||
1458 | Expr_Pos := | |
1459 | Make_Op_Add (Loc, | |
58009744 AC |
1460 | Left_Opnd => To_Pos, |
1461 | Right_Opnd => Make_Integer_Literal (Loc, Val)); | |
996ae0b0 RK |
1462 | |
1463 | Expr := | |
1464 | Make_Attribute_Reference | |
1465 | (Loc, | |
e4494292 | 1466 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1467 | Attribute_Name => Name_Val, |
1468 | Expressions => New_List (Expr_Pos)); | |
f915704f AC |
1469 | |
1470 | -- If the index type has a non standard representation, the | |
1471 | -- attributes 'Val and 'Pos expand into function calls and the | |
1472 | -- resulting expression is considered non-safe for reevaluation | |
1473 | -- by the backend. Relocate it into a constant temporary in order | |
1474 | -- to make it safe for reevaluation. | |
1475 | ||
1476 | if Has_Non_Standard_Rep (Etype (N)) then | |
1477 | declare | |
1478 | Def_Id : Entity_Id; | |
1479 | ||
1480 | begin | |
1481 | Def_Id := Make_Temporary (Loc, 'R', Expr); | |
1482 | Set_Etype (Def_Id, Index_Typ); | |
1483 | Insert_Action (N, | |
1484 | Make_Object_Declaration (Loc, | |
1485 | Defining_Identifier => Def_Id, | |
e4494292 RD |
1486 | Object_Definition => |
1487 | New_Occurrence_Of (Index_Typ, Loc), | |
f915704f AC |
1488 | Constant_Present => True, |
1489 | Expression => Relocate_Node (Expr))); | |
1490 | ||
e4494292 | 1491 | Expr := New_Occurrence_Of (Def_Id, Loc); |
f915704f AC |
1492 | end; |
1493 | end if; | |
996ae0b0 RK |
1494 | end if; |
1495 | ||
1496 | return Expr; | |
1497 | end Add; | |
1498 | ||
1499 | ----------------- | |
1500 | -- Check_Bound -- | |
1501 | ----------------- | |
1502 | ||
1503 | procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is | |
1504 | Val_BH : Uint; | |
1505 | Val_AH : Uint; | |
1506 | ||
1507 | OK_BH : Boolean; | |
1508 | OK_AH : Boolean; | |
1509 | ||
1510 | begin | |
1511 | Get (Value => Val_BH, From => BH, OK => OK_BH); | |
1512 | Get (Value => Val_AH, From => AH, OK => OK_AH); | |
1513 | ||
1514 | if OK_BH and then OK_AH and then Val_BH < Val_AH then | |
1515 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1516 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1517 | Error_Msg_N ("upper bound out of range<<", AH); |
1518 | Error_Msg_N ("\Constraint_Error [<<", AH); | |
996ae0b0 RK |
1519 | |
1520 | -- You need to set AH to BH or else in the case of enumerations | |
3b42c566 | 1521 | -- indexes we will not be able to resolve the aggregate bounds. |
996ae0b0 RK |
1522 | |
1523 | AH := Duplicate_Subexpr (BH); | |
1524 | end if; | |
1525 | end Check_Bound; | |
1526 | ||
1527 | ------------------ | |
1528 | -- Check_Bounds -- | |
1529 | ------------------ | |
1530 | ||
1531 | procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is | |
1532 | Val_L : Uint; | |
1533 | Val_H : Uint; | |
1534 | Val_AL : Uint; | |
1535 | Val_AH : Uint; | |
1536 | ||
f91e8020 GD |
1537 | OK_L : Boolean; |
1538 | OK_H : Boolean; | |
1539 | ||
996ae0b0 | 1540 | OK_AL : Boolean; |
f91e8020 GD |
1541 | OK_AH : Boolean; |
1542 | pragma Warnings (Off, OK_AL); | |
1543 | pragma Warnings (Off, OK_AH); | |
996ae0b0 RK |
1544 | |
1545 | begin | |
1546 | if Raises_Constraint_Error (N) | |
1547 | or else Dynamic_Or_Null_Range (AL, AH) | |
1548 | then | |
1549 | return; | |
1550 | end if; | |
1551 | ||
1552 | Get (Value => Val_L, From => L, OK => OK_L); | |
1553 | Get (Value => Val_H, From => H, OK => OK_H); | |
1554 | ||
1555 | Get (Value => Val_AL, From => AL, OK => OK_AL); | |
1556 | Get (Value => Val_AH, From => AH, OK => OK_AH); | |
1557 | ||
1558 | if OK_L and then Val_L > Val_AL then | |
1559 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1560 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1561 | Error_Msg_N ("lower bound of aggregate out of range<<", N); |
1562 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1563 | end if; |
1564 | ||
1565 | if OK_H and then Val_H < Val_AH then | |
1566 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1567 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1568 | Error_Msg_N ("upper bound of aggregate out of range<<", N); |
1569 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1570 | end if; |
1571 | end Check_Bounds; | |
1572 | ||
1573 | ------------------ | |
1574 | -- Check_Length -- | |
1575 | ------------------ | |
1576 | ||
1577 | procedure Check_Length (L, H : Node_Id; Len : Uint) is | |
1578 | Val_L : Uint; | |
1579 | Val_H : Uint; | |
1580 | ||
1581 | OK_L : Boolean; | |
1582 | OK_H : Boolean; | |
1583 | ||
1584 | Range_Len : Uint; | |
1585 | ||
1586 | begin | |
1587 | if Raises_Constraint_Error (N) then | |
1588 | return; | |
1589 | end if; | |
1590 | ||
1591 | Get (Value => Val_L, From => L, OK => OK_L); | |
1592 | Get (Value => Val_H, From => H, OK => OK_H); | |
1593 | ||
1594 | if not OK_L or else not OK_H then | |
1595 | return; | |
1596 | end if; | |
1597 | ||
1598 | -- If null range length is zero | |
1599 | ||
1600 | if Val_L > Val_H then | |
1601 | Range_Len := Uint_0; | |
1602 | else | |
1603 | Range_Len := Val_H - Val_L + 1; | |
1604 | end if; | |
1605 | ||
1606 | if Range_Len < Len then | |
1607 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1608 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1609 | Error_Msg_N ("too many elements<<", N); |
1610 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1611 | end if; |
1612 | end Check_Length; | |
1613 | ||
1614 | --------------------------- | |
1615 | -- Dynamic_Or_Null_Range -- | |
1616 | --------------------------- | |
1617 | ||
1618 | function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is | |
1619 | Val_L : Uint; | |
1620 | Val_H : Uint; | |
1621 | ||
1622 | OK_L : Boolean; | |
1623 | OK_H : Boolean; | |
1624 | ||
1625 | begin | |
1626 | Get (Value => Val_L, From => L, OK => OK_L); | |
1627 | Get (Value => Val_H, From => H, OK => OK_H); | |
1628 | ||
1629 | return not OK_L or else not OK_H | |
1630 | or else not Is_OK_Static_Expression (L) | |
1631 | or else not Is_OK_Static_Expression (H) | |
1632 | or else Val_L > Val_H; | |
1633 | end Dynamic_Or_Null_Range; | |
1634 | ||
1635 | --------- | |
1636 | -- Get -- | |
1637 | --------- | |
1638 | ||
1639 | procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is | |
1640 | begin | |
1641 | OK := True; | |
1642 | ||
1643 | if Compile_Time_Known_Value (From) then | |
1644 | Value := Expr_Value (From); | |
1645 | ||
1646 | -- If expression From is something like Some_Type'Val (10) then | |
1f0b1e48 | 1647 | -- Value = 10. |
996ae0b0 RK |
1648 | |
1649 | elsif Nkind (From) = N_Attribute_Reference | |
1650 | and then Attribute_Name (From) = Name_Val | |
1651 | and then Compile_Time_Known_Value (First (Expressions (From))) | |
1652 | then | |
1653 | Value := Expr_Value (First (Expressions (From))); | |
996ae0b0 RK |
1654 | else |
1655 | Value := Uint_0; | |
1656 | OK := False; | |
1657 | end if; | |
1658 | end Get; | |
1659 | ||
1660 | ----------------------- | |
1661 | -- Resolve_Aggr_Expr -- | |
1662 | ----------------------- | |
1663 | ||
1664 | function Resolve_Aggr_Expr | |
1665 | (Expr : Node_Id; | |
ca44152f | 1666 | Single_Elmt : Boolean) return Boolean |
996ae0b0 | 1667 | is |
fbf5a39b AC |
1668 | Nxt_Ind : constant Node_Id := Next_Index (Index); |
1669 | Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr); | |
12a13f01 | 1670 | -- Index is the current index corresponding to the expression |
996ae0b0 RK |
1671 | |
1672 | Resolution_OK : Boolean := True; | |
ec53a6da | 1673 | -- Set to False if resolution of the expression failed |
996ae0b0 RK |
1674 | |
1675 | begin | |
199c6a10 AC |
1676 | -- Defend against previous errors |
1677 | ||
1678 | if Nkind (Expr) = N_Error | |
1679 | or else Error_Posted (Expr) | |
1680 | then | |
1681 | return True; | |
1682 | end if; | |
1683 | ||
996ae0b0 RK |
1684 | -- If the array type against which we are resolving the aggregate |
1685 | -- has several dimensions, the expressions nested inside the | |
1686 | -- aggregate must be further aggregates (or strings). | |
1687 | ||
1688 | if Present (Nxt_Ind) then | |
1689 | if Nkind (Expr) /= N_Aggregate then | |
1690 | ||
1691 | -- A string literal can appear where a one-dimensional array | |
1692 | -- of characters is expected. If the literal looks like an | |
1693 | -- operator, it is still an operator symbol, which will be | |
1694 | -- transformed into a string when analyzed. | |
1695 | ||
1696 | if Is_Character_Type (Component_Typ) | |
1697 | and then No (Next_Index (Nxt_Ind)) | |
4a08c95c | 1698 | and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol |
996ae0b0 RK |
1699 | then |
1700 | -- A string literal used in a multidimensional array | |
1701 | -- aggregate in place of the final one-dimensional | |
1702 | -- aggregate must not be enclosed in parentheses. | |
1703 | ||
1704 | if Paren_Count (Expr) /= 0 then | |
ed2233dc | 1705 | Error_Msg_N ("no parenthesis allowed here", Expr); |
996ae0b0 RK |
1706 | end if; |
1707 | ||
1708 | Make_String_Into_Aggregate (Expr); | |
1709 | ||
1710 | else | |
1711 | Error_Msg_N ("nested array aggregate expected", Expr); | |
9d0c3761 AC |
1712 | |
1713 | -- If the expression is parenthesized, this may be | |
1714 | -- a missing component association for a 1-aggregate. | |
1715 | ||
1716 | if Paren_Count (Expr) > 0 then | |
ed2233dc | 1717 | Error_Msg_N |
58009744 AC |
1718 | ("\if single-component aggregate is intended, " |
1719 | & "write e.g. (1 ='> ...)", Expr); | |
9d0c3761 | 1720 | end if; |
f5afb270 | 1721 | |
996ae0b0 RK |
1722 | return Failure; |
1723 | end if; | |
1724 | end if; | |
1725 | ||
ca5af305 AC |
1726 | -- If it's "... => <>", nothing to resolve |
1727 | ||
1728 | if Nkind (Expr) = N_Component_Association then | |
1729 | pragma Assert (Box_Present (Expr)); | |
1730 | return Success; | |
1731 | end if; | |
1732 | ||
0ab80019 | 1733 | -- Ada 2005 (AI-231): Propagate the type to the nested aggregate. |
35b7fa6a AC |
1734 | -- Required to check the null-exclusion attribute (if present). |
1735 | -- This value may be overridden later on. | |
1736 | ||
1737 | Set_Etype (Expr, Etype (N)); | |
1738 | ||
996ae0b0 RK |
1739 | Resolution_OK := Resolve_Array_Aggregate |
1740 | (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed); | |
1741 | ||
ca5af305 | 1742 | else |
ca5af305 AC |
1743 | -- If it's "... => <>", nothing to resolve |
1744 | ||
1745 | if Nkind (Expr) = N_Component_Association then | |
1746 | pragma Assert (Box_Present (Expr)); | |
1747 | return Success; | |
1748 | end if; | |
1749 | ||
1750 | -- Do not resolve the expressions of discrete or others choices | |
1751 | -- unless the expression covers a single component, or the | |
1752 | -- expander is inactive. | |
1753 | ||
92b751fd | 1754 | -- In SPARK mode, expressions that can perform side effects will |
06b599fd YM |
1755 | -- be recognized by the gnat2why back-end, and the whole |
1756 | -- subprogram will be ignored. So semantic analysis can be | |
1757 | -- performed safely. | |
9f8d1e5c | 1758 | |
ca5af305 | 1759 | if Single_Elmt |
4460a9bc | 1760 | or else not Expander_Active |
ca5af305 AC |
1761 | or else In_Spec_Expression |
1762 | then | |
1763 | Analyze_And_Resolve (Expr, Component_Typ); | |
1764 | Check_Expr_OK_In_Limited_Aggregate (Expr); | |
1765 | Check_Non_Static_Context (Expr); | |
1766 | Aggregate_Constraint_Checks (Expr, Component_Typ); | |
1767 | Check_Unset_Reference (Expr); | |
1768 | end if; | |
996ae0b0 RK |
1769 | end if; |
1770 | ||
dec6faf1 AC |
1771 | -- If an aggregate component has a type with predicates, an explicit |
1772 | -- predicate check must be applied, as for an assignment statement, | |
152f64c2 | 1773 | -- because the aggregate might not be expanded into individual |
07eb872e AC |
1774 | -- component assignments. If the expression covers several components |
1775 | -- the analysis and the predicate check take place later. | |
dec6faf1 | 1776 | |
619bfd9f | 1777 | if Has_Predicates (Component_Typ) |
07eb872e AC |
1778 | and then Analyzed (Expr) |
1779 | then | |
887d102a AC |
1780 | Apply_Predicate_Check (Expr, Component_Typ); |
1781 | end if; | |
1782 | ||
996ae0b0 RK |
1783 | if Raises_Constraint_Error (Expr) |
1784 | and then Nkind (Parent (Expr)) /= N_Component_Association | |
1785 | then | |
1786 | Set_Raises_Constraint_Error (N); | |
1787 | end if; | |
1788 | ||
d79e621a | 1789 | -- If the expression has been marked as requiring a range check, |
edab6088 RD |
1790 | -- then generate it here. It's a bit odd to be generating such |
1791 | -- checks in the analyzer, but harmless since Generate_Range_Check | |
1792 | -- does nothing (other than making sure Do_Range_Check is set) if | |
1793 | -- the expander is not active. | |
d79e621a GD |
1794 | |
1795 | if Do_Range_Check (Expr) then | |
d79e621a GD |
1796 | Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed); |
1797 | end if; | |
1798 | ||
996ae0b0 RK |
1799 | return Resolution_OK; |
1800 | end Resolve_Aggr_Expr; | |
1801 | ||
ef74daea AC |
1802 | -------------------------------------------- |
1803 | -- Resolve_Iterated_Component_Association -- | |
1804 | -------------------------------------------- | |
1805 | ||
1806 | procedure Resolve_Iterated_Component_Association | |
1807 | (N : Node_Id; | |
1808 | Index_Typ : Entity_Id) | |
1809 | is | |
ef74daea | 1810 | Loc : constant Source_Ptr := Sloc (N); |
fb00cc70 ES |
1811 | Id : constant Entity_Id := Defining_Identifier (N); |
1812 | ||
1813 | ----------------------- | |
1814 | -- Remove_References -- | |
1815 | ----------------------- | |
1816 | ||
022f9894 PT |
1817 | function Remove_Reference (N : Node_Id) return Traverse_Result; |
1818 | -- Remove reference to the entity Id after analysis, so it can be | |
fb00cc70 ES |
1819 | -- properly reanalyzed after construct is expanded into a loop. |
1820 | ||
022f9894 | 1821 | function Remove_Reference (N : Node_Id) return Traverse_Result is |
fb00cc70 ES |
1822 | begin |
1823 | if Nkind (N) = N_Identifier | |
1824 | and then Present (Entity (N)) | |
1825 | and then Entity (N) = Id | |
1826 | then | |
1827 | Set_Entity (N, Empty); | |
1828 | Set_Etype (N, Empty); | |
1829 | end if; | |
1830 | Set_Analyzed (N, False); | |
1831 | return OK; | |
022f9894 | 1832 | end Remove_Reference; |
fb00cc70 | 1833 | |
022f9894 | 1834 | procedure Remove_References is new Traverse_Proc (Remove_Reference); |
fb00cc70 ES |
1835 | |
1836 | -- Local variables | |
ef74daea AC |
1837 | |
1838 | Choice : Node_Id; | |
1839 | Dummy : Boolean; | |
022f9894 | 1840 | Scop : Entity_Id; |
be54247f | 1841 | Expr : constant Node_Id := Expression (N); |
fb00cc70 ES |
1842 | |
1843 | -- Start of processing for Resolve_Iterated_Component_Association | |
ef74daea AC |
1844 | |
1845 | begin | |
439dae60 BD |
1846 | Error_Msg_Ada_2022_Feature ("iterated component", Loc); |
1847 | ||
44bd2755 PT |
1848 | -- Create a scope in which to introduce an index, to make it visible |
1849 | -- for the analysis of component expression. | |
ff49b805 | 1850 | |
44bd2755 PT |
1851 | Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L'); |
1852 | Set_Etype (Scop, Standard_Void_Type); | |
1853 | Set_Parent (Scop, Parent (N)); | |
1854 | Push_Scope (Scop); | |
ef74daea | 1855 | |
44bd2755 PT |
1856 | -- If there is iterator specification, then its preanalysis will make |
1857 | -- the index visible. | |
ef74daea | 1858 | |
44bd2755 PT |
1859 | if Present (Iterator_Specification (N)) then |
1860 | Preanalyze (Iterator_Specification (N)); | |
1861 | ||
1862 | -- Otherwise, analyze discrete choices and make the index visible | |
b03d3f73 | 1863 | |
fe43084c | 1864 | else |
44bd2755 PT |
1865 | -- Insert index name into current scope but don't decorate it yet, |
1866 | -- so that a premature usage of this name in discrete choices will | |
1867 | -- be nicely diagnosed. | |
1868 | ||
1869 | Enter_Name (Id); | |
1870 | ||
fe43084c | 1871 | Choice := First (Discrete_Choices (N)); |
b03d3f73 | 1872 | |
fe43084c ES |
1873 | while Present (Choice) loop |
1874 | if Nkind (Choice) = N_Others_Choice then | |
1875 | Others_Present := True; | |
b03d3f73 AC |
1876 | |
1877 | else | |
fe43084c ES |
1878 | Analyze (Choice); |
1879 | ||
1880 | -- Choice can be a subtype name, a range, or an expression | |
1881 | ||
1882 | if Is_Entity_Name (Choice) | |
1883 | and then Is_Type (Entity (Choice)) | |
1884 | and then | |
1885 | Base_Type (Entity (Choice)) = Base_Type (Index_Typ) | |
1886 | then | |
1887 | null; | |
1888 | ||
1889 | else | |
1890 | Analyze_And_Resolve (Choice, Index_Typ); | |
1891 | end if; | |
b03d3f73 | 1892 | end if; |
ef74daea | 1893 | |
fe43084c ES |
1894 | Next (Choice); |
1895 | end loop; | |
ef74daea | 1896 | |
44bd2755 | 1897 | -- Decorate the index variable |
9eb8d5b4 | 1898 | |
44bd2755 PT |
1899 | Set_Etype (Id, Index_Typ); |
1900 | Mutate_Ekind (Id, E_Variable); | |
25e4024c | 1901 | Set_Is_Not_Self_Hidden (Id); |
44bd2755 PT |
1902 | Set_Scope (Id, Scop); |
1903 | end if; | |
d940c627 | 1904 | |
be54247f | 1905 | -- Analyze expression without expansion, to verify legality. |
02fb1280 PT |
1906 | -- When generating code, we then remove references to the index |
1907 | -- variable, because the expression will be analyzed anew after | |
1908 | -- rewritting as a loop with a new index variable; when not | |
1909 | -- generating code we leave the analyzed expression as it is. | |
fb00cc70 | 1910 | |
f34b3115 | 1911 | Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False); |
02fb1280 PT |
1912 | |
1913 | if Operating_Mode /= Check_Semantics then | |
1914 | Remove_References (Expr); | |
1915 | end if; | |
d940c627 ES |
1916 | |
1917 | -- An iterated_component_association may appear in a nested | |
1918 | -- aggregate for a multidimensional structure: preserve the bounds | |
1919 | -- computed for the expression, as well as the anonymous array | |
1920 | -- type generated for it; both are needed during array expansion. | |
d940c627 ES |
1921 | |
1922 | if Nkind (Expr) = N_Aggregate then | |
1923 | Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr)); | |
1924 | Set_Etype (Expression (N), Etype (Expr)); | |
9eb8d5b4 | 1925 | end if; |
ef74daea | 1926 | |
ef74daea AC |
1927 | End_Scope; |
1928 | end Resolve_Iterated_Component_Association; | |
1929 | ||
65ab836d JM |
1930 | ---------------------------------------- |
1931 | -- Warn_On_Null_Component_Association -- | |
1932 | ---------------------------------------- | |
1933 | ||
1934 | procedure Warn_On_Null_Component_Association (Expr : Node_Id) is | |
1935 | Comp_Typ : constant Entity_Id := Component_Type (Etype (N)); | |
1936 | ||
1937 | procedure Check_Case_Expr (N : Node_Id); | |
1938 | -- Check if a case expression may initialize some component with a | |
1939 | -- null value. | |
1940 | ||
1941 | procedure Check_Cond_Expr (N : Node_Id); | |
1942 | -- Check if a conditional expression may initialize some component | |
1943 | -- with a null value. | |
1944 | ||
1945 | procedure Check_Expr (Expr : Node_Id); | |
1946 | -- Check if an expression may initialize some component with a | |
1947 | -- null value. | |
1948 | ||
1949 | procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id); | |
1950 | -- Report warning on known null expression and replace the expression | |
1951 | -- by a raise constraint error node. | |
1952 | ||
1953 | --------------------- | |
1954 | -- Check_Case_Expr -- | |
1955 | --------------------- | |
1956 | ||
1957 | procedure Check_Case_Expr (N : Node_Id) is | |
1958 | Alt_Node : Node_Id := First (Alternatives (N)); | |
1959 | ||
1960 | begin | |
1961 | while Present (Alt_Node) loop | |
1962 | Check_Expr (Expression (Alt_Node)); | |
1963 | Next (Alt_Node); | |
1964 | end loop; | |
1965 | end Check_Case_Expr; | |
1966 | ||
1967 | --------------------- | |
1968 | -- Check_Cond_Expr -- | |
1969 | --------------------- | |
1970 | ||
1971 | procedure Check_Cond_Expr (N : Node_Id) is | |
1972 | If_Expr : Node_Id := N; | |
1973 | Then_Expr : Node_Id; | |
1974 | Else_Expr : Node_Id; | |
1975 | ||
1976 | begin | |
1977 | Then_Expr := Next (First (Expressions (If_Expr))); | |
1978 | Else_Expr := Next (Then_Expr); | |
1979 | ||
1980 | Check_Expr (Then_Expr); | |
1981 | ||
1982 | -- Process elsif parts (if any) | |
1983 | ||
1984 | while Nkind (Else_Expr) = N_If_Expression loop | |
1985 | If_Expr := Else_Expr; | |
1986 | Then_Expr := Next (First (Expressions (If_Expr))); | |
1987 | Else_Expr := Next (Then_Expr); | |
1988 | ||
1989 | Check_Expr (Then_Expr); | |
1990 | end loop; | |
1991 | ||
1992 | if Known_Null (Else_Expr) then | |
1993 | Warn_On_Null_Expression_And_Rewrite (Else_Expr); | |
1994 | end if; | |
1995 | end Check_Cond_Expr; | |
1996 | ||
1997 | ---------------- | |
1998 | -- Check_Expr -- | |
1999 | ---------------- | |
2000 | ||
2001 | procedure Check_Expr (Expr : Node_Id) is | |
2002 | begin | |
2003 | if Known_Null (Expr) then | |
2004 | Warn_On_Null_Expression_And_Rewrite (Expr); | |
2005 | ||
2006 | elsif Nkind (Expr) = N_If_Expression then | |
2007 | Check_Cond_Expr (Expr); | |
2008 | ||
2009 | elsif Nkind (Expr) = N_Case_Expression then | |
2010 | Check_Case_Expr (Expr); | |
2011 | end if; | |
2012 | end Check_Expr; | |
2013 | ||
2014 | ----------------------------------------- | |
2015 | -- Warn_On_Null_Expression_And_Rewrite -- | |
2016 | ----------------------------------------- | |
2017 | ||
2018 | procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is | |
2019 | begin | |
2020 | Error_Msg_N | |
2021 | ("(Ada 2005) NULL not allowed in null-excluding component??", | |
2022 | Null_Expr); | |
2023 | Error_Msg_N | |
2024 | ("\Constraint_Error might be raised at run time??", Null_Expr); | |
2025 | ||
2026 | -- We cannot use Apply_Compile_Time_Constraint_Error because in | |
2027 | -- some cases the components are rewritten and the runtime error | |
2028 | -- would be missed. | |
2029 | ||
2030 | Rewrite (Null_Expr, | |
2031 | Make_Raise_Constraint_Error (Sloc (Null_Expr), | |
2032 | Reason => CE_Access_Check_Failed)); | |
2033 | ||
2034 | Set_Etype (Null_Expr, Comp_Typ); | |
2035 | Set_Analyzed (Null_Expr); | |
2036 | end Warn_On_Null_Expression_And_Rewrite; | |
2037 | ||
2038 | -- Start of processing for Warn_On_Null_Component_Association | |
2039 | ||
2040 | begin | |
2041 | pragma Assert (Can_Never_Be_Null (Comp_Typ)); | |
2042 | ||
2043 | case Nkind (Expr) is | |
2044 | when N_If_Expression => | |
2045 | Check_Cond_Expr (Expr); | |
2046 | ||
2047 | when N_Case_Expression => | |
2048 | Check_Case_Expr (Expr); | |
2049 | ||
2050 | when others => | |
2051 | pragma Assert (False); | |
2052 | null; | |
2053 | end case; | |
2054 | end Warn_On_Null_Component_Association; | |
2055 | ||
ef74daea | 2056 | -- Local variables |
996ae0b0 | 2057 | |
58009744 AC |
2058 | Assoc : Node_Id; |
2059 | Choice : Node_Id; | |
2060 | Expr : Node_Id; | |
f91e8020 | 2061 | Discard : Node_Id; |
996ae0b0 RK |
2062 | |
2063 | Aggr_Low : Node_Id := Empty; | |
2064 | Aggr_High : Node_Id := Empty; | |
c7ce71c2 | 2065 | -- The actual low and high bounds of this sub-aggregate |
996ae0b0 | 2066 | |
ef74daea AC |
2067 | Case_Table_Size : Nat; |
2068 | -- Contains the size of the case table needed to sort aggregate choices | |
2069 | ||
996ae0b0 RK |
2070 | Choices_Low : Node_Id := Empty; |
2071 | Choices_High : Node_Id := Empty; | |
2072 | -- The lowest and highest discrete choices values for a named aggregate | |
2073 | ||
ef74daea AC |
2074 | Delete_Choice : Boolean; |
2075 | -- Used when replacing a subtype choice with predicate by a list | |
2076 | ||
fe43084c ES |
2077 | Has_Iterator_Specifications : Boolean := False; |
2078 | -- Flag to indicate that all named associations are iterated component | |
2079 | -- associations with iterator specifications, in which case the | |
2080 | -- expansion will create two loops: one to evaluate the size and one | |
2081 | -- to generate the elements (4.3.3 (20.2/5)). | |
2082 | ||
996ae0b0 | 2083 | Nb_Elements : Uint := Uint_0; |
c7ce71c2 | 2084 | -- The number of elements in a positional aggregate |
996ae0b0 | 2085 | |
996ae0b0 RK |
2086 | Nb_Discrete_Choices : Nat := 0; |
2087 | -- The overall number of discrete choices (not counting others choice) | |
2088 | ||
996ae0b0 RK |
2089 | -- Start of processing for Resolve_Array_Aggregate |
2090 | ||
2091 | begin | |
6d2a1120 RD |
2092 | -- Ignore junk empty aggregate resulting from parser error |
2093 | ||
2094 | if No (Expressions (N)) | |
2095 | and then No (Component_Associations (N)) | |
2096 | and then not Null_Record_Present (N) | |
2097 | then | |
f85d7dee | 2098 | return Failure; |
6d2a1120 RD |
2099 | end if; |
2100 | ||
b045f2f2 MP |
2101 | -- Disable the warning for GNAT Mode to allow for easier transition. |
2102 | ||
a124b526 | 2103 | if Ada_Version_Explicit >= Ada_2022 |
b045f2f2 MP |
2104 | and then Warn_On_Obsolescent_Feature |
2105 | and then not GNAT_Mode | |
2106 | and then not Is_Homogeneous_Aggregate (N) | |
2107 | and then not Is_Enum_Array_Aggregate (N) | |
2108 | and then Is_Parenthesis_Aggregate (N) | |
2109 | and then Nkind (Parent (N)) /= N_Qualified_Expression | |
5a35fe64 | 2110 | and then Comes_From_Source (N) |
b045f2f2 MP |
2111 | then |
2112 | Error_Msg_N | |
2113 | ("?j?array aggregate using () is an" & | |
2114 | " obsolescent syntax, use '['] instead", N); | |
2115 | end if; | |
2116 | ||
996ae0b0 RK |
2117 | -- STEP 1: make sure the aggregate is correctly formatted |
2118 | ||
d9275e87 PT |
2119 | if Is_Null_Aggregate (N) then |
2120 | null; | |
2121 | ||
2122 | elsif Present (Component_Associations (N)) then | |
fe43084c ES |
2123 | |
2124 | -- Verify that all or none of the component associations | |
2125 | -- include an iterator specification. | |
2126 | ||
2127 | Assoc := First (Component_Associations (N)); | |
2128 | if Nkind (Assoc) = N_Iterated_Component_Association | |
2129 | and then Present (Iterator_Specification (Assoc)) | |
2130 | then | |
2131 | -- All other component associations must have an iterator spec. | |
2132 | ||
2133 | Next (Assoc); | |
2134 | while Present (Assoc) loop | |
2135 | if Nkind (Assoc) /= N_Iterated_Component_Association | |
2136 | or else No (Iterator_Specification (Assoc)) | |
2137 | then | |
2138 | Error_Msg_N ("mixed iterated component association" | |
84f20139 | 2139 | & " (RM 4.3.3 (17.1/5))", |
fe43084c | 2140 | Assoc); |
f85d7dee | 2141 | return Failure; |
fe43084c ES |
2142 | end if; |
2143 | ||
2144 | Next (Assoc); | |
2145 | end loop; | |
2146 | ||
2147 | Has_Iterator_Specifications := True; | |
2148 | ||
2149 | else | |
2150 | -- or none of them do. | |
2151 | ||
2152 | Next (Assoc); | |
2153 | while Present (Assoc) loop | |
2154 | if Nkind (Assoc) = N_Iterated_Component_Association | |
2155 | and then Present (Iterator_Specification (Assoc)) | |
2156 | then | |
2157 | Error_Msg_N ("mixed iterated component association" | |
84f20139 | 2158 | & " (RM 4.3.3 (17.1/5))", |
fe43084c | 2159 | Assoc); |
f85d7dee | 2160 | return Failure; |
fe43084c ES |
2161 | end if; |
2162 | ||
2163 | Next (Assoc); | |
2164 | end loop; | |
2165 | ||
fe43084c ES |
2166 | end if; |
2167 | ||
996ae0b0 RK |
2168 | Assoc := First (Component_Associations (N)); |
2169 | while Present (Assoc) loop | |
ef74daea AC |
2170 | if Nkind (Assoc) = N_Iterated_Component_Association then |
2171 | Resolve_Iterated_Component_Association (Assoc, Index_Typ); | |
9bde1fc6 SB |
2172 | |
2173 | elsif Nkind (Assoc) /= N_Component_Association then | |
2174 | Error_Msg_N | |
2175 | ("invalid component association for aggregate", Assoc); | |
2176 | return Failure; | |
ef74daea AC |
2177 | end if; |
2178 | ||
b2c1aa8f | 2179 | Choice := First (Choice_List (Assoc)); |
2791be24 | 2180 | Delete_Choice := False; |
996ae0b0 RK |
2181 | while Present (Choice) loop |
2182 | if Nkind (Choice) = N_Others_Choice then | |
2183 | Others_Present := True; | |
2184 | ||
b2c1aa8f | 2185 | if Choice /= First (Choice_List (Assoc)) |
996ae0b0 RK |
2186 | or else Present (Next (Choice)) |
2187 | then | |
ed2233dc | 2188 | Error_Msg_N |
996ae0b0 RK |
2189 | ("OTHERS must appear alone in a choice list", Choice); |
2190 | return Failure; | |
2191 | end if; | |
2192 | ||
2193 | if Present (Next (Assoc)) then | |
ed2233dc | 2194 | Error_Msg_N |
996ae0b0 RK |
2195 | ("OTHERS must appear last in an aggregate", Choice); |
2196 | return Failure; | |
2197 | end if; | |
2198 | ||
0ab80019 | 2199 | if Ada_Version = Ada_83 |
996ae0b0 | 2200 | and then Assoc /= First (Component_Associations (N)) |
4a08c95c AC |
2201 | and then Nkind (Parent (N)) in |
2202 | N_Assignment_Statement | N_Object_Declaration | |
996ae0b0 RK |
2203 | then |
2204 | Error_Msg_N | |
2205 | ("(Ada 83) illegal context for OTHERS choice", N); | |
2206 | end if; | |
2791be24 AC |
2207 | |
2208 | elsif Is_Entity_Name (Choice) then | |
2209 | Analyze (Choice); | |
2210 | ||
2211 | declare | |
2212 | E : constant Entity_Id := Entity (Choice); | |
2213 | New_Cs : List_Id; | |
2214 | P : Node_Id; | |
2215 | C : Node_Id; | |
2216 | ||
2217 | begin | |
2218 | if Is_Type (E) and then Has_Predicates (E) then | |
2219 | Freeze_Before (N, E); | |
2220 | ||
067d80d8 YM |
2221 | if Has_Dynamic_Predicate_Aspect (E) |
2222 | or else Has_Ghost_Predicate_Aspect (E) | |
2223 | then | |
b330e3c8 | 2224 | Error_Msg_NE |
067d80d8 | 2225 | ("subtype& has non-static predicate, not allowed " |
b330e3c8 | 2226 | & "in aggregate choice", Choice, E); |
24de083f | 2227 | |
4b259b2d | 2228 | elsif not Is_OK_Static_Subtype (E) then |
b330e3c8 AC |
2229 | Error_Msg_NE |
2230 | ("non-static subtype& has predicate, not allowed " | |
2231 | & "in aggregate choice", Choice, E); | |
24de083f AC |
2232 | end if; |
2233 | ||
2791be24 AC |
2234 | -- If the subtype has a static predicate, replace the |
2235 | -- original choice with the list of individual values | |
65f1ca2e | 2236 | -- covered by the predicate. |
3abbc5c2 | 2237 | -- This should be deferred to expansion time ??? |
2791be24 | 2238 | |
65f1ca2e | 2239 | if Present (Static_Discrete_Predicate (E)) then |
2791be24 AC |
2240 | Delete_Choice := True; |
2241 | ||
2242 | New_Cs := New_List; | |
60f908dd | 2243 | P := First (Static_Discrete_Predicate (E)); |
2791be24 AC |
2244 | while Present (P) loop |
2245 | C := New_Copy (P); | |
2246 | Set_Sloc (C, Sloc (Choice)); | |
2247 | Append_To (New_Cs, C); | |
2248 | Next (P); | |
2249 | end loop; | |
2250 | ||
2251 | Insert_List_After (Choice, New_Cs); | |
2252 | end if; | |
2253 | end if; | |
2254 | end; | |
996ae0b0 RK |
2255 | end if; |
2256 | ||
2257 | Nb_Choices := Nb_Choices + 1; | |
2791be24 AC |
2258 | |
2259 | declare | |
2260 | C : constant Node_Id := Choice; | |
2261 | ||
2262 | begin | |
2263 | Next (Choice); | |
2264 | ||
2265 | if Delete_Choice then | |
2266 | Remove (C); | |
2267 | Nb_Choices := Nb_Choices - 1; | |
2268 | Delete_Choice := False; | |
2269 | end if; | |
2270 | end; | |
996ae0b0 RK |
2271 | end loop; |
2272 | ||
2273 | Next (Assoc); | |
2274 | end loop; | |
2275 | end if; | |
2276 | ||
2277 | -- At this point we know that the others choice, if present, is by | |
2278 | -- itself and appears last in the aggregate. Check if we have mixed | |
2279 | -- positional and discrete associations (other than the others choice). | |
2280 | ||
2281 | if Present (Expressions (N)) | |
2282 | and then (Nb_Choices > 1 | |
2283 | or else (Nb_Choices = 1 and then not Others_Present)) | |
2284 | then | |
2285 | Error_Msg_N | |
4f3a016f | 2286 | ("cannot mix named and positional associations in array aggregate", |
ef74daea | 2287 | First (Choice_List (First (Component_Associations (N))))); |
996ae0b0 RK |
2288 | return Failure; |
2289 | end if; | |
2290 | ||
2291 | -- Test for the validity of an others choice if present | |
2292 | ||
2293 | if Others_Present and then not Others_Allowed then | |
f456de4c YM |
2294 | declare |
2295 | Others_N : constant Node_Id := | |
2296 | First (Choice_List (First (Component_Associations (N)))); | |
2297 | begin | |
2298 | Error_Msg_N ("OTHERS choice not allowed here", Others_N); | |
2299 | Error_Msg_N ("\qualify the aggregate with a constrained subtype " | |
2300 | & "to provide bounds for it", Others_N); | |
2301 | return Failure; | |
2302 | end; | |
996ae0b0 RK |
2303 | end if; |
2304 | ||
07fc65c4 GB |
2305 | -- Protect against cascaded errors |
2306 | ||
2307 | if Etype (Index_Typ) = Any_Type then | |
2308 | return Failure; | |
2309 | end if; | |
2310 | ||
996ae0b0 RK |
2311 | -- STEP 2: Process named components |
2312 | ||
2313 | if No (Expressions (N)) then | |
996ae0b0 RK |
2314 | if Others_Present then |
2315 | Case_Table_Size := Nb_Choices - 1; | |
2316 | else | |
2317 | Case_Table_Size := Nb_Choices; | |
2318 | end if; | |
2319 | ||
2320 | Step_2 : declare | |
10edebe7 AC |
2321 | function Empty_Range (A : Node_Id) return Boolean; |
2322 | -- If an association covers an empty range, some warnings on the | |
2323 | -- expression of the association can be disabled. | |
2324 | ||
2325 | ----------------- | |
2326 | -- Empty_Range -- | |
2327 | ----------------- | |
2328 | ||
2329 | function Empty_Range (A : Node_Id) return Boolean is | |
65ab836d JM |
2330 | R : Node_Id; |
2331 | ||
10edebe7 | 2332 | begin |
65ab836d JM |
2333 | if Nkind (A) = N_Iterated_Component_Association then |
2334 | R := First (Discrete_Choices (A)); | |
2335 | else | |
2336 | R := First (Choices (A)); | |
2337 | end if; | |
2338 | ||
10edebe7 AC |
2339 | return No (Next (R)) |
2340 | and then Nkind (R) = N_Range | |
2341 | and then Compile_Time_Compare | |
2342 | (Low_Bound (R), High_Bound (R), False) = GT; | |
2343 | end Empty_Range; | |
2344 | ||
2345 | -- Local variables | |
2346 | ||
996ae0b0 RK |
2347 | Low : Node_Id; |
2348 | High : Node_Id; | |
2349 | -- Denote the lowest and highest values in an aggregate choice | |
2350 | ||
996ae0b0 RK |
2351 | S_Low : Node_Id := Empty; |
2352 | S_High : Node_Id := Empty; | |
2353 | -- if a choice in an aggregate is a subtype indication these | |
2354 | -- denote the lowest and highest values of the subtype | |
2355 | ||
38f44fd6 PT |
2356 | Table : Case_Table_Type (1 .. Case_Table_Size); |
2357 | -- Used to sort all the different choice values | |
996ae0b0 RK |
2358 | |
2359 | Single_Choice : Boolean; | |
2360 | -- Set to true every time there is a single discrete choice in a | |
2361 | -- discrete association | |
2362 | ||
2363 | Prev_Nb_Discrete_Choices : Nat; | |
b87971f3 AC |
2364 | -- Used to keep track of the number of discrete choices in the |
2365 | -- current association. | |
996ae0b0 | 2366 | |
c0b11850 AC |
2367 | Errors_Posted_On_Choices : Boolean := False; |
2368 | -- Keeps track of whether any choices have semantic errors | |
2369 | ||
5a521b8a AC |
2370 | -- Start of processing for Step_2 |
2371 | ||
996ae0b0 | 2372 | begin |
ec53a6da | 2373 | -- STEP 2 (A): Check discrete choices validity |
fe43084c | 2374 | -- No need if this is an element iteration. |
996ae0b0 RK |
2375 | |
2376 | Assoc := First (Component_Associations (N)); | |
fe43084c ES |
2377 | while Present (Assoc) |
2378 | and then Present (Choice_List (Assoc)) | |
2379 | loop | |
996ae0b0 | 2380 | Prev_Nb_Discrete_Choices := Nb_Discrete_Choices; |
ef74daea AC |
2381 | Choice := First (Choice_List (Assoc)); |
2382 | ||
996ae0b0 RK |
2383 | loop |
2384 | Analyze (Choice); | |
2385 | ||
2386 | if Nkind (Choice) = N_Others_Choice then | |
2387 | Single_Choice := False; | |
2388 | exit; | |
2389 | ||
2390 | -- Test for subtype mark without constraint | |
2391 | ||
2392 | elsif Is_Entity_Name (Choice) and then | |
2393 | Is_Type (Entity (Choice)) | |
2394 | then | |
2395 | if Base_Type (Entity (Choice)) /= Index_Base then | |
2396 | Error_Msg_N | |
2397 | ("invalid subtype mark in aggregate choice", | |
2398 | Choice); | |
2399 | return Failure; | |
2400 | end if; | |
2401 | ||
ca44152f ES |
2402 | -- Case of subtype indication |
2403 | ||
996ae0b0 RK |
2404 | elsif Nkind (Choice) = N_Subtype_Indication then |
2405 | Resolve_Discrete_Subtype_Indication (Choice, Index_Base); | |
2406 | ||
24de083f | 2407 | if Has_Dynamic_Predicate_Aspect |
067d80d8 YM |
2408 | (Entity (Subtype_Mark (Choice))) |
2409 | or else Has_Ghost_Predicate_Aspect | |
2410 | (Entity (Subtype_Mark (Choice))) | |
24de083f | 2411 | then |
58009744 | 2412 | Error_Msg_NE |
067d80d8 | 2413 | ("subtype& has non-static predicate, " |
58009744 AC |
2414 | & "not allowed in aggregate choice", |
2415 | Choice, Entity (Subtype_Mark (Choice))); | |
24de083f AC |
2416 | end if; |
2417 | ||
324ac540 | 2418 | -- Does the subtype indication evaluation raise CE? |
996ae0b0 RK |
2419 | |
2420 | Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High); | |
2421 | Get_Index_Bounds (Choice, Low, High); | |
2422 | Check_Bounds (S_Low, S_High, Low, High); | |
2423 | ||
ca44152f ES |
2424 | -- Case of range or expression |
2425 | ||
2426 | else | |
996ae0b0 | 2427 | Resolve (Choice, Index_Base); |
fbf5a39b | 2428 | Check_Unset_Reference (Choice); |
996ae0b0 RK |
2429 | Check_Non_Static_Context (Choice); |
2430 | ||
c0b11850 AC |
2431 | -- If semantic errors were posted on the choice, then |
2432 | -- record that for possible early return from later | |
2433 | -- processing (see handling of enumeration choices). | |
2434 | ||
2435 | if Error_Posted (Choice) then | |
2436 | Errors_Posted_On_Choices := True; | |
2437 | end if; | |
2438 | ||
996ae0b0 | 2439 | -- Do not range check a choice. This check is redundant |
b87971f3 AC |
2440 | -- since this test is already done when we check that the |
2441 | -- bounds of the array aggregate are within range. | |
996ae0b0 RK |
2442 | |
2443 | Set_Do_Range_Check (Choice, False); | |
2444 | end if; | |
2445 | ||
2446 | -- If we could not resolve the discrete choice stop here | |
2447 | ||
2448 | if Etype (Choice) = Any_Type then | |
2449 | return Failure; | |
2450 | ||
ec53a6da | 2451 | -- If the discrete choice raises CE get its original bounds |
996ae0b0 RK |
2452 | |
2453 | elsif Nkind (Choice) = N_Raise_Constraint_Error then | |
2454 | Set_Raises_Constraint_Error (N); | |
2455 | Get_Index_Bounds (Original_Node (Choice), Low, High); | |
2456 | ||
2457 | -- Otherwise get its bounds as usual | |
2458 | ||
2459 | else | |
2460 | Get_Index_Bounds (Choice, Low, High); | |
2461 | end if; | |
2462 | ||
2463 | if (Dynamic_Or_Null_Range (Low, High) | |
2464 | or else (Nkind (Choice) = N_Subtype_Indication | |
2465 | and then | |
2466 | Dynamic_Or_Null_Range (S_Low, S_High))) | |
2467 | and then Nb_Choices /= 1 | |
2468 | then | |
2469 | Error_Msg_N | |
58009744 AC |
2470 | ("dynamic or empty choice in aggregate " |
2471 | & "must be the only choice", Choice); | |
996ae0b0 RK |
2472 | return Failure; |
2473 | end if; | |
2474 | ||
2f7ae2aa BD |
2475 | if not (All_Composite_Constraints_Static (Low) |
2476 | and then All_Composite_Constraints_Static (High) | |
2477 | and then All_Composite_Constraints_Static (S_Low) | |
2478 | and then All_Composite_Constraints_Static (S_High)) | |
2479 | then | |
2480 | Check_Restriction (No_Dynamic_Sized_Objects, Choice); | |
2481 | end if; | |
2482 | ||
996ae0b0 | 2483 | Nb_Discrete_Choices := Nb_Discrete_Choices + 1; |
82893775 AC |
2484 | Table (Nb_Discrete_Choices).Lo := Low; |
2485 | Table (Nb_Discrete_Choices).Hi := High; | |
2486 | Table (Nb_Discrete_Choices).Choice := Choice; | |
996ae0b0 RK |
2487 | |
2488 | Next (Choice); | |
2489 | ||
2490 | if No (Choice) then | |
9b96e234 | 2491 | |
996ae0b0 RK |
2492 | -- Check if we have a single discrete choice and whether |
2493 | -- this discrete choice specifies a single value. | |
2494 | ||
2495 | Single_Choice := | |
8f563162 AC |
2496 | Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1 |
2497 | and then Low = High; | |
996ae0b0 RK |
2498 | |
2499 | exit; | |
2500 | end if; | |
2501 | end loop; | |
2502 | ||
0ab80019 | 2503 | -- Ada 2005 (AI-231) |
2820d220 | 2504 | |
0791fbe9 | 2505 | if Ada_Version >= Ada_2005 |
5a521b8a | 2506 | and then not Empty_Range (Assoc) |
ec53a6da | 2507 | then |
65ab836d JM |
2508 | if Known_Null (Expression (Assoc)) then |
2509 | Check_Can_Never_Be_Null (Etype (N), Expression (Assoc)); | |
2510 | ||
2511 | -- Report warning on iterated component association that may | |
2512 | -- initialize some component of an array of null-excluding | |
2513 | -- access type components with a null value. For example: | |
2514 | ||
2515 | -- type AList is array (...) of not null access Integer; | |
2516 | -- L : AList := | |
2517 | -- [for J in A'Range => | |
2518 | -- (if Func (J) = 0 then A(J)'Access else Null)]; | |
2519 | ||
2520 | elsif Ada_Version >= Ada_2022 | |
2521 | and then Can_Never_Be_Null (Component_Type (Etype (N))) | |
2522 | and then Nkind (Assoc) = N_Iterated_Component_Association | |
2523 | and then Nkind (Expression (Assoc)) in N_If_Expression | |
2524 | | N_Case_Expression | |
2525 | then | |
2526 | Warn_On_Null_Component_Association (Expression (Assoc)); | |
2527 | end if; | |
82c80734 | 2528 | end if; |
2820d220 | 2529 | |
0ab80019 | 2530 | -- Ada 2005 (AI-287): In case of default initialized component |
b87971f3 | 2531 | -- we delay the resolution to the expansion phase. |
c45b6ae0 AC |
2532 | |
2533 | if Box_Present (Assoc) then | |
2534 | ||
b87971f3 AC |
2535 | -- Ada 2005 (AI-287): In case of default initialization of a |
2536 | -- component the expander will generate calls to the | |
ca5af305 AC |
2537 | -- corresponding initialization subprogram. We need to call |
2538 | -- Resolve_Aggr_Expr to check the rules about | |
2539 | -- dimensionality. | |
c45b6ae0 | 2540 | |
882eadaf RD |
2541 | if not Resolve_Aggr_Expr |
2542 | (Assoc, Single_Elmt => Single_Choice) | |
ca5af305 AC |
2543 | then |
2544 | return Failure; | |
2545 | end if; | |
c45b6ae0 | 2546 | |
3b612313 PT |
2547 | -- ??? Checks for dynamically tagged expressions below will |
2548 | -- be only applied to iterated_component_association after | |
2549 | -- expansion; in particular, errors might not be reported when | |
2550 | -- -gnatc switch is used. | |
2551 | ||
9eb8d5b4 | 2552 | elsif Nkind (Assoc) = N_Iterated_Component_Association then |
3b612313 | 2553 | null; -- handled above, in a loop context |
9eb8d5b4 | 2554 | |
882eadaf RD |
2555 | elsif not Resolve_Aggr_Expr |
2556 | (Expression (Assoc), Single_Elmt => Single_Choice) | |
996ae0b0 RK |
2557 | then |
2558 | return Failure; | |
4755cce9 JM |
2559 | |
2560 | -- Check incorrect use of dynamically tagged expression | |
2561 | ||
2562 | -- We differentiate here two cases because the expression may | |
2563 | -- not be decorated. For example, the analysis and resolution | |
b87971f3 AC |
2564 | -- of the expression associated with the others choice will be |
2565 | -- done later with the full aggregate. In such case we | |
4755cce9 JM |
2566 | -- duplicate the expression tree to analyze the copy and |
2567 | -- perform the required check. | |
2568 | ||
7d0d27d9 | 2569 | elsif No (Etype (Expression (Assoc))) then |
4755cce9 JM |
2570 | declare |
2571 | Save_Analysis : constant Boolean := Full_Analysis; | |
2572 | Expr : constant Node_Id := | |
2573 | New_Copy_Tree (Expression (Assoc)); | |
2574 | ||
2575 | begin | |
2576 | Expander_Mode_Save_And_Set (False); | |
2577 | Full_Analysis := False; | |
6ff6152d ES |
2578 | |
2579 | -- Analyze the expression, making sure it is properly | |
2580 | -- attached to the tree before we do the analysis. | |
2581 | ||
2582 | Set_Parent (Expr, Parent (Expression (Assoc))); | |
4755cce9 | 2583 | Analyze (Expr); |
094cefda | 2584 | |
a6ac7311 AC |
2585 | -- Compute its dimensions now, rather than at the end of |
2586 | -- resolution, because in the case of multidimensional | |
888be6b1 AC |
2587 | -- aggregates subsequent expansion may lead to spurious |
2588 | -- errors. | |
2589 | ||
2590 | Check_Expression_Dimensions (Expr, Component_Typ); | |
2591 | ||
094cefda AC |
2592 | -- If the expression is a literal, propagate this info |
2593 | -- to the expression in the association, to enable some | |
2594 | -- optimizations downstream. | |
2595 | ||
2596 | if Is_Entity_Name (Expr) | |
2597 | and then Present (Entity (Expr)) | |
2598 | and then Ekind (Entity (Expr)) = E_Enumeration_Literal | |
2599 | then | |
2600 | Analyze_And_Resolve | |
2601 | (Expression (Assoc), Component_Typ); | |
2602 | end if; | |
2603 | ||
4755cce9 JM |
2604 | Full_Analysis := Save_Analysis; |
2605 | Expander_Mode_Restore; | |
2606 | ||
2607 | if Is_Tagged_Type (Etype (Expr)) then | |
2608 | Check_Dynamically_Tagged_Expression | |
2609 | (Expr => Expr, | |
2610 | Typ => Component_Type (Etype (N)), | |
2611 | Related_Nod => N); | |
2612 | end if; | |
2613 | end; | |
2614 | ||
2615 | elsif Is_Tagged_Type (Etype (Expression (Assoc))) then | |
2616 | Check_Dynamically_Tagged_Expression | |
1c612f29 RD |
2617 | (Expr => Expression (Assoc), |
2618 | Typ => Component_Type (Etype (N)), | |
4755cce9 | 2619 | Related_Nod => N); |
996ae0b0 RK |
2620 | end if; |
2621 | ||
2622 | Next (Assoc); | |
2623 | end loop; | |
2624 | ||
2625 | -- If aggregate contains more than one choice then these must be | |
882eadaf RD |
2626 | -- static. Check for duplicate and missing values. |
2627 | ||
2628 | -- Note: there is duplicated code here wrt Check_Choice_Set in | |
2629 | -- the body of Sem_Case, and it is possible we could just reuse | |
2630 | -- that procedure. To be checked ??? | |
996ae0b0 RK |
2631 | |
2632 | if Nb_Discrete_Choices > 1 then | |
882eadaf RD |
2633 | Check_Choices : declare |
2634 | Choice : Node_Id; | |
2635 | -- Location of choice for messages | |
996ae0b0 | 2636 | |
882eadaf RD |
2637 | Hi_Val : Uint; |
2638 | Lo_Val : Uint; | |
2639 | -- High end of one range and Low end of the next. Should be | |
2640 | -- contiguous if there is no hole in the list of values. | |
996ae0b0 | 2641 | |
82893775 AC |
2642 | Lo_Dup : Uint; |
2643 | Hi_Dup : Uint; | |
2644 | -- End points of duplicated range | |
2645 | ||
882eadaf RD |
2646 | Missing_Or_Duplicates : Boolean := False; |
2647 | -- Set True if missing or duplicate choices found | |
996ae0b0 | 2648 | |
882eadaf RD |
2649 | procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id); |
2650 | -- Output continuation message with a representation of the | |
2651 | -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the | |
2652 | -- choice node where the message is to be posted. | |
996ae0b0 | 2653 | |
882eadaf RD |
2654 | ------------------------ |
2655 | -- Output_Bad_Choices -- | |
2656 | ------------------------ | |
996ae0b0 | 2657 | |
882eadaf RD |
2658 | procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is |
2659 | begin | |
2660 | -- Enumeration type case | |
996ae0b0 | 2661 | |
882eadaf RD |
2662 | if Is_Enumeration_Type (Index_Typ) then |
2663 | Error_Msg_Name_1 := | |
2664 | Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc)); | |
2665 | Error_Msg_Name_2 := | |
2666 | Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc)); | |
2667 | ||
2668 | if Lo = Hi then | |
2669 | Error_Msg_N ("\\ %!", C); | |
2670 | else | |
2671 | Error_Msg_N ("\\ % .. %!", C); | |
996ae0b0 RK |
2672 | end if; |
2673 | ||
882eadaf | 2674 | -- Integer types case |
996ae0b0 | 2675 | |
882eadaf RD |
2676 | else |
2677 | Error_Msg_Uint_1 := Lo; | |
2678 | Error_Msg_Uint_2 := Hi; | |
996ae0b0 | 2679 | |
882eadaf RD |
2680 | if Lo = Hi then |
2681 | Error_Msg_N ("\\ ^!", C); | |
2682 | else | |
2683 | Error_Msg_N ("\\ ^ .. ^!", C); | |
2684 | end if; | |
2685 | end if; | |
2686 | end Output_Bad_Choices; | |
996ae0b0 | 2687 | |
882eadaf | 2688 | -- Start of processing for Check_Choices |
996ae0b0 | 2689 | |
882eadaf RD |
2690 | begin |
2691 | Sort_Case_Table (Table); | |
996ae0b0 | 2692 | |
82893775 AC |
2693 | -- First we do a quick linear loop to find out if we have |
2694 | -- any duplicates or missing entries (usually we have a | |
2695 | -- legal aggregate, so this will get us out quickly). | |
996ae0b0 | 2696 | |
882eadaf | 2697 | for J in 1 .. Nb_Discrete_Choices - 1 loop |
82893775 AC |
2698 | Hi_Val := Expr_Value (Table (J).Hi); |
2699 | Lo_Val := Expr_Value (Table (J + 1).Lo); | |
882eadaf | 2700 | |
82893775 AC |
2701 | if Lo_Val <= Hi_Val |
2702 | or else (Lo_Val > Hi_Val + 1 | |
2703 | and then not Others_Present) | |
2704 | then | |
882eadaf | 2705 | Missing_Or_Duplicates := True; |
82893775 | 2706 | exit; |
882eadaf RD |
2707 | end if; |
2708 | end loop; | |
996ae0b0 | 2709 | |
82893775 AC |
2710 | -- If we have missing or duplicate entries, first fill in |
2711 | -- the Highest entries to make life easier in the following | |
2712 | -- loops to detect bad entries. | |
882eadaf | 2713 | |
82893775 AC |
2714 | if Missing_Or_Duplicates then |
2715 | Table (1).Highest := Expr_Value (Table (1).Hi); | |
882eadaf | 2716 | |
82893775 AC |
2717 | for J in 2 .. Nb_Discrete_Choices loop |
2718 | Table (J).Highest := | |
2719 | UI_Max | |
2720 | (Table (J - 1).Highest, Expr_Value (Table (J).Hi)); | |
2721 | end loop; | |
882eadaf | 2722 | |
82893775 AC |
2723 | -- Loop through table entries to find duplicate indexes |
2724 | ||
2725 | for J in 2 .. Nb_Discrete_Choices loop | |
2726 | Lo_Val := Expr_Value (Table (J).Lo); | |
2727 | Hi_Val := Expr_Value (Table (J).Hi); | |
2728 | ||
2729 | -- Case where we have duplicates (the lower bound of | |
2730 | -- this choice is less than or equal to the highest | |
2731 | -- high bound found so far). | |
2732 | ||
2733 | if Lo_Val <= Table (J - 1).Highest then | |
2734 | ||
2735 | -- We move backwards looking for duplicates. We can | |
2736 | -- abandon this loop as soon as we reach a choice | |
2737 | -- highest value that is less than Lo_Val. | |
2738 | ||
2739 | for K in reverse 1 .. J - 1 loop | |
2740 | exit when Table (K).Highest < Lo_Val; | |
2741 | ||
2742 | -- Here we may have duplicates between entries | |
2743 | -- for K and J. Get range of duplicates. | |
2744 | ||
2745 | Lo_Dup := | |
2746 | UI_Max (Lo_Val, Expr_Value (Table (K).Lo)); | |
2747 | Hi_Dup := | |
2748 | UI_Min (Hi_Val, Expr_Value (Table (K).Hi)); | |
2749 | ||
2750 | -- Nothing to do if duplicate range is null | |
882eadaf | 2751 | |
82893775 AC |
2752 | if Lo_Dup > Hi_Dup then |
2753 | null; | |
2754 | ||
65f1ca2e | 2755 | -- Otherwise place proper message |
82893775 AC |
2756 | |
2757 | else | |
2758 | -- We place message on later choice, with a | |
2759 | -- line reference to the earlier choice. | |
2760 | ||
2761 | if Sloc (Table (J).Choice) < | |
2762 | Sloc (Table (K).Choice) | |
2763 | then | |
2764 | Choice := Table (K).Choice; | |
2765 | Error_Msg_Sloc := Sloc (Table (J).Choice); | |
2766 | else | |
2767 | Choice := Table (J).Choice; | |
2768 | Error_Msg_Sloc := Sloc (Table (K).Choice); | |
2769 | end if; | |
2770 | ||
2771 | if Lo_Dup = Hi_Dup then | |
2772 | Error_Msg_N | |
2773 | ("index value in array aggregate " | |
2774 | & "duplicates the one given#!", Choice); | |
2775 | else | |
2776 | Error_Msg_N | |
2777 | ("index values in array aggregate " | |
2778 | & "duplicate those given#!", Choice); | |
2779 | end if; | |
2780 | ||
2781 | Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice); | |
2782 | end if; | |
2783 | end loop; | |
996ae0b0 | 2784 | end if; |
882eadaf | 2785 | end loop; |
996ae0b0 | 2786 | |
82893775 AC |
2787 | -- Loop through entries in table to find missing indexes. |
2788 | -- Not needed if others, since missing impossible. | |
2789 | ||
2790 | if not Others_Present then | |
2791 | for J in 2 .. Nb_Discrete_Choices loop | |
2792 | Lo_Val := Expr_Value (Table (J).Lo); | |
2793 | Hi_Val := Table (J - 1).Highest; | |
2794 | ||
2795 | if Lo_Val > Hi_Val + 1 then | |
16a569d2 | 2796 | |
7b4ebba5 AC |
2797 | declare |
2798 | Error_Node : Node_Id; | |
16a569d2 | 2799 | |
7b4ebba5 AC |
2800 | begin |
2801 | -- If the choice is the bound of a range in | |
2802 | -- a subtype indication, it is not in the | |
2803 | -- source lists for the aggregate itself, so | |
2804 | -- post the error on the aggregate. Otherwise | |
2805 | -- post it on choice itself. | |
16a569d2 | 2806 | |
7b4ebba5 | 2807 | Choice := Table (J).Choice; |
16a569d2 | 2808 | |
7b4ebba5 AC |
2809 | if Is_List_Member (Choice) then |
2810 | Error_Node := Choice; | |
2811 | else | |
2812 | Error_Node := N; | |
2813 | end if; | |
82893775 | 2814 | |
7b4ebba5 AC |
2815 | if Hi_Val + 1 = Lo_Val - 1 then |
2816 | Error_Msg_N | |
2817 | ("missing index value " | |
2818 | & "in array aggregate!", Error_Node); | |
2819 | else | |
2820 | Error_Msg_N | |
2821 | ("missing index values " | |
2822 | & "in array aggregate!", Error_Node); | |
2823 | end if; | |
82893775 | 2824 | |
7b4ebba5 AC |
2825 | Output_Bad_Choices |
2826 | (Hi_Val + 1, Lo_Val - 1, Error_Node); | |
2827 | end; | |
82893775 AC |
2828 | end if; |
2829 | end loop; | |
2830 | end if; | |
2831 | ||
2832 | -- If either missing or duplicate values, return failure | |
882eadaf | 2833 | |
882eadaf RD |
2834 | Set_Etype (N, Any_Composite); |
2835 | return Failure; | |
2836 | end if; | |
2837 | end Check_Choices; | |
996ae0b0 RK |
2838 | end if; |
2839 | ||
fe43084c ES |
2840 | if Has_Iterator_Specifications then |
2841 | -- Bounds will be determined dynamically. | |
2842 | ||
2843 | return Success; | |
2844 | end if; | |
2845 | ||
996ae0b0 RK |
2846 | -- STEP 2 (B): Compute aggregate bounds and min/max choices values |
2847 | ||
2848 | if Nb_Discrete_Choices > 0 then | |
82893775 AC |
2849 | Choices_Low := Table (1).Lo; |
2850 | Choices_High := Table (Nb_Discrete_Choices).Hi; | |
996ae0b0 RK |
2851 | end if; |
2852 | ||
ca44152f ES |
2853 | -- If Others is present, then bounds of aggregate come from the |
2854 | -- index constraint (not the choices in the aggregate itself). | |
2855 | ||
996ae0b0 RK |
2856 | if Others_Present then |
2857 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
2858 | ||
596b25f9 AC |
2859 | -- Abandon processing if either bound is already signalled as |
2860 | -- an error (prevents junk cascaded messages and blow ups). | |
2861 | ||
2862 | if Nkind (Aggr_Low) = N_Error | |
2863 | or else | |
2864 | Nkind (Aggr_High) = N_Error | |
2865 | then | |
2866 | return False; | |
2867 | end if; | |
2868 | ||
ca44152f ES |
2869 | -- No others clause present |
2870 | ||
996ae0b0 | 2871 | else |
ca44152f ES |
2872 | -- Special processing if others allowed and not present. This |
2873 | -- means that the bounds of the aggregate come from the index | |
2874 | -- constraint (and the length must match). | |
2875 | ||
2876 | if Others_Allowed then | |
2877 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
2878 | ||
596b25f9 AC |
2879 | -- Abandon processing if either bound is already signalled |
2880 | -- as an error (stop junk cascaded messages and blow ups). | |
2881 | ||
2882 | if Nkind (Aggr_Low) = N_Error | |
2883 | or else | |
2884 | Nkind (Aggr_High) = N_Error | |
2885 | then | |
2886 | return False; | |
2887 | end if; | |
2888 | ||
ca44152f ES |
2889 | -- If others allowed, and no others present, then the array |
2890 | -- should cover all index values. If it does not, we will | |
2891 | -- get a length check warning, but there is two cases where | |
2892 | -- an additional warning is useful: | |
2893 | ||
2894 | -- If we have no positional components, and the length is | |
2895 | -- wrong (which we can tell by others being allowed with | |
2896 | -- missing components), and the index type is an enumeration | |
2897 | -- type, then issue appropriate warnings about these missing | |
2898 | -- components. They are only warnings, since the aggregate | |
2899 | -- is fine, it's just the wrong length. We skip this check | |
2900 | -- for standard character types (since there are no literals | |
2901 | -- and it is too much trouble to concoct them), and also if | |
50ea6357 AC |
2902 | -- any of the bounds have values that are not known at |
2903 | -- compile time. | |
ca44152f | 2904 | |
58009744 AC |
2905 | -- Another case warranting a warning is when the length |
2906 | -- is right, but as above we have an index type that is | |
2907 | -- an enumeration, and the bounds do not match. This is a | |
2908 | -- case where dubious sliding is allowed and we generate a | |
2909 | -- warning that the bounds do not match. | |
ca44152f ES |
2910 | |
2911 | if No (Expressions (N)) | |
2912 | and then Nkind (Index) = N_Range | |
2913 | and then Is_Enumeration_Type (Etype (Index)) | |
2914 | and then not Is_Standard_Character_Type (Etype (Index)) | |
2915 | and then Compile_Time_Known_Value (Aggr_Low) | |
2916 | and then Compile_Time_Known_Value (Aggr_High) | |
2917 | and then Compile_Time_Known_Value (Choices_Low) | |
2918 | and then Compile_Time_Known_Value (Choices_High) | |
2919 | then | |
c0b11850 AC |
2920 | -- If any of the expressions or range bounds in choices |
2921 | -- have semantic errors, then do not attempt further | |
2922 | -- resolution, to prevent cascaded errors. | |
d610088d | 2923 | |
c0b11850 AC |
2924 | if Errors_Posted_On_Choices then |
2925 | return Failure; | |
d610088d AC |
2926 | end if; |
2927 | ||
ca44152f ES |
2928 | declare |
2929 | ALo : constant Node_Id := Expr_Value_E (Aggr_Low); | |
2930 | AHi : constant Node_Id := Expr_Value_E (Aggr_High); | |
2931 | CLo : constant Node_Id := Expr_Value_E (Choices_Low); | |
2932 | CHi : constant Node_Id := Expr_Value_E (Choices_High); | |
2933 | ||
2934 | Ent : Entity_Id; | |
2935 | ||
2936 | begin | |
ebd34478 | 2937 | -- Warning case 1, missing values at start/end. Only |
ca44152f ES |
2938 | -- do the check if the number of entries is too small. |
2939 | ||
2940 | if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo)) | |
2941 | < | |
2942 | (Enumeration_Pos (AHi) - Enumeration_Pos (ALo)) | |
2943 | then | |
2944 | Error_Msg_N | |
324ac540 AC |
2945 | ("missing index value(s) in array aggregate??", |
2946 | N); | |
ca44152f ES |
2947 | |
2948 | -- Output missing value(s) at start | |
2949 | ||
2950 | if Chars (ALo) /= Chars (CLo) then | |
2951 | Ent := Prev (CLo); | |
2952 | ||
2953 | if Chars (ALo) = Chars (Ent) then | |
2954 | Error_Msg_Name_1 := Chars (ALo); | |
324ac540 | 2955 | Error_Msg_N ("\ %??", N); |
ca44152f ES |
2956 | else |
2957 | Error_Msg_Name_1 := Chars (ALo); | |
2958 | Error_Msg_Name_2 := Chars (Ent); | |
324ac540 | 2959 | Error_Msg_N ("\ % .. %??", N); |
ca44152f ES |
2960 | end if; |
2961 | end if; | |
2962 | ||
2963 | -- Output missing value(s) at end | |
2964 | ||
2965 | if Chars (AHi) /= Chars (CHi) then | |
2966 | Ent := Next (CHi); | |
2967 | ||
2968 | if Chars (AHi) = Chars (Ent) then | |
2969 | Error_Msg_Name_1 := Chars (Ent); | |
324ac540 | 2970 | Error_Msg_N ("\ %??", N); |
ca44152f ES |
2971 | else |
2972 | Error_Msg_Name_1 := Chars (Ent); | |
2973 | Error_Msg_Name_2 := Chars (AHi); | |
324ac540 | 2974 | Error_Msg_N ("\ % .. %??", N); |
ca44152f ES |
2975 | end if; |
2976 | end if; | |
2977 | ||
2978 | -- Warning case 2, dubious sliding. The First_Subtype | |
2979 | -- test distinguishes between a constrained type where | |
2980 | -- sliding is not allowed (so we will get a warning | |
2981 | -- later that Constraint_Error will be raised), and | |
2982 | -- the unconstrained case where sliding is permitted. | |
2983 | ||
2984 | elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo)) | |
2985 | = | |
2986 | (Enumeration_Pos (AHi) - Enumeration_Pos (ALo)) | |
2987 | and then Chars (ALo) /= Chars (CLo) | |
2988 | and then | |
2989 | not Is_Constrained (First_Subtype (Etype (N))) | |
2990 | then | |
2991 | Error_Msg_N | |
324ac540 | 2992 | ("bounds of aggregate do not match target??", N); |
ca44152f ES |
2993 | end if; |
2994 | end; | |
2995 | end if; | |
2996 | end if; | |
2997 | ||
f3d0f304 | 2998 | -- If no others, aggregate bounds come from aggregate |
ca44152f | 2999 | |
996ae0b0 RK |
3000 | Aggr_Low := Choices_Low; |
3001 | Aggr_High := Choices_High; | |
3002 | end if; | |
3003 | end Step_2; | |
3004 | ||
3005 | -- STEP 3: Process positional components | |
3006 | ||
3007 | else | |
3008 | -- STEP 3 (A): Process positional elements | |
3009 | ||
3010 | Expr := First (Expressions (N)); | |
3011 | Nb_Elements := Uint_0; | |
3012 | while Present (Expr) loop | |
3013 | Nb_Elements := Nb_Elements + 1; | |
3014 | ||
82c80734 RD |
3015 | -- Ada 2005 (AI-231) |
3016 | ||
58009744 | 3017 | if Ada_Version >= Ada_2005 and then Known_Null (Expr) then |
82c80734 RD |
3018 | Check_Can_Never_Be_Null (Etype (N), Expr); |
3019 | end if; | |
2820d220 | 3020 | |
ef74daea | 3021 | if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then |
996ae0b0 RK |
3022 | return Failure; |
3023 | end if; | |
3024 | ||
4755cce9 JM |
3025 | -- Check incorrect use of dynamically tagged expression |
3026 | ||
3027 | if Is_Tagged_Type (Etype (Expr)) then | |
3028 | Check_Dynamically_Tagged_Expression | |
3029 | (Expr => Expr, | |
3030 | Typ => Component_Type (Etype (N)), | |
3031 | Related_Nod => N); | |
3032 | end if; | |
3033 | ||
996ae0b0 RK |
3034 | Next (Expr); |
3035 | end loop; | |
3036 | ||
3037 | if Others_Present then | |
3038 | Assoc := Last (Component_Associations (N)); | |
c45b6ae0 | 3039 | |
82c80734 RD |
3040 | -- Ada 2005 (AI-231) |
3041 | ||
58009744 | 3042 | if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then |
9b96e234 | 3043 | Check_Can_Never_Be_Null (Etype (N), Expression (Assoc)); |
82c80734 | 3044 | end if; |
2820d220 | 3045 | |
ebd34478 | 3046 | -- Ada 2005 (AI-287): In case of default initialized component, |
c45b6ae0 AC |
3047 | -- we delay the resolution to the expansion phase. |
3048 | ||
3049 | if Box_Present (Assoc) then | |
3050 | ||
ebd34478 AC |
3051 | -- Ada 2005 (AI-287): In case of default initialization of a |
3052 | -- component the expander will generate calls to the | |
ca5af305 AC |
3053 | -- corresponding initialization subprogram. We need to call |
3054 | -- Resolve_Aggr_Expr to check the rules about | |
3055 | -- dimensionality. | |
c45b6ae0 | 3056 | |
ca5af305 AC |
3057 | if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then |
3058 | return Failure; | |
3059 | end if; | |
c45b6ae0 AC |
3060 | |
3061 | elsif not Resolve_Aggr_Expr (Expression (Assoc), | |
3062 | Single_Elmt => False) | |
996ae0b0 RK |
3063 | then |
3064 | return Failure; | |
4755cce9 JM |
3065 | |
3066 | -- Check incorrect use of dynamically tagged expression. The | |
3067 | -- expression of the others choice has not been resolved yet. | |
3068 | -- In order to diagnose the semantic error we create a duplicate | |
3069 | -- tree to analyze it and perform the check. | |
3070 | ||
fb00cc70 | 3071 | elsif Nkind (Assoc) /= N_Iterated_Component_Association then |
4755cce9 JM |
3072 | declare |
3073 | Save_Analysis : constant Boolean := Full_Analysis; | |
3074 | Expr : constant Node_Id := | |
3075 | New_Copy_Tree (Expression (Assoc)); | |
3076 | ||
3077 | begin | |
3078 | Expander_Mode_Save_And_Set (False); | |
3079 | Full_Analysis := False; | |
3080 | Analyze (Expr); | |
3081 | Full_Analysis := Save_Analysis; | |
3082 | Expander_Mode_Restore; | |
3083 | ||
3084 | if Is_Tagged_Type (Etype (Expr)) then | |
3085 | Check_Dynamically_Tagged_Expression | |
58009744 AC |
3086 | (Expr => Expr, |
3087 | Typ => Component_Type (Etype (N)), | |
4755cce9 JM |
3088 | Related_Nod => N); |
3089 | end if; | |
3090 | end; | |
996ae0b0 RK |
3091 | end if; |
3092 | end if; | |
3093 | ||
3094 | -- STEP 3 (B): Compute the aggregate bounds | |
3095 | ||
3096 | if Others_Present then | |
3097 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
3098 | ||
3099 | else | |
3100 | if Others_Allowed then | |
f91e8020 | 3101 | Get_Index_Bounds (Index_Constr, Aggr_Low, Discard); |
996ae0b0 RK |
3102 | else |
3103 | Aggr_Low := Index_Typ_Low; | |
3104 | end if; | |
3105 | ||
3106 | Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low); | |
3107 | Check_Bound (Index_Base_High, Aggr_High); | |
3108 | end if; | |
3109 | end if; | |
3110 | ||
3111 | -- STEP 4: Perform static aggregate checks and save the bounds | |
3112 | ||
3113 | -- Check (A) | |
3114 | ||
3115 | Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High); | |
3116 | Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High); | |
3117 | ||
3118 | -- Check (B) | |
3119 | ||
3120 | if Others_Present and then Nb_Discrete_Choices > 0 then | |
3121 | Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High); | |
3122 | Check_Bounds (Index_Typ_Low, Index_Typ_High, | |
3123 | Choices_Low, Choices_High); | |
3124 | Check_Bounds (Index_Base_Low, Index_Base_High, | |
3125 | Choices_Low, Choices_High); | |
3126 | ||
3127 | -- Check (C) | |
3128 | ||
3129 | elsif Others_Present and then Nb_Elements > 0 then | |
3130 | Check_Length (Aggr_Low, Aggr_High, Nb_Elements); | |
3131 | Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements); | |
3132 | Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements); | |
996ae0b0 RK |
3133 | end if; |
3134 | ||
3135 | if Raises_Constraint_Error (Aggr_Low) | |
3136 | or else Raises_Constraint_Error (Aggr_High) | |
3137 | then | |
3138 | Set_Raises_Constraint_Error (N); | |
3139 | end if; | |
3140 | ||
3141 | Aggr_Low := Duplicate_Subexpr (Aggr_Low); | |
3142 | ||
3143 | -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements | |
3144 | -- since the addition node returned by Add is not yet analyzed. Attach | |
ebd34478 | 3145 | -- to tree and analyze first. Reset analyzed flag to ensure it will get |
9b96e234 | 3146 | -- analyzed when it is a literal bound whose type must be properly set. |
996ae0b0 RK |
3147 | |
3148 | if Others_Present or else Nb_Discrete_Choices > 0 then | |
3149 | Aggr_High := Duplicate_Subexpr (Aggr_High); | |
3150 | ||
3151 | if Etype (Aggr_High) = Universal_Integer then | |
3152 | Set_Analyzed (Aggr_High, False); | |
3153 | end if; | |
3154 | end if; | |
3155 | ||
3d923671 AC |
3156 | -- If the aggregate already has bounds attached to it, it means this is |
3157 | -- a positional aggregate created as an optimization by | |
3158 | -- Exp_Aggr.Convert_To_Positional, so we don't want to change those | |
3159 | -- bounds. | |
3160 | ||
f7d1b4a7 PT |
3161 | if Present (Aggregate_Bounds (N)) |
3162 | and then not Others_Allowed | |
3163 | and then not Comes_From_Source (N) | |
3164 | then | |
ebd34478 | 3165 | Aggr_Low := Low_Bound (Aggregate_Bounds (N)); |
3d923671 AC |
3166 | Aggr_High := High_Bound (Aggregate_Bounds (N)); |
3167 | end if; | |
3168 | ||
996ae0b0 RK |
3169 | Set_Aggregate_Bounds |
3170 | (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High)); | |
3171 | ||
3172 | -- The bounds may contain expressions that must be inserted upwards. | |
3173 | -- Attach them fully to the tree. After analysis, remove side effects | |
3174 | -- from upper bound, if still needed. | |
3175 | ||
3176 | Set_Parent (Aggregate_Bounds (N), N); | |
3177 | Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ); | |
fbf5a39b | 3178 | Check_Unset_Reference (Aggregate_Bounds (N)); |
996ae0b0 RK |
3179 | |
3180 | if not Others_Present and then Nb_Discrete_Choices = 0 then | |
82893775 AC |
3181 | Set_High_Bound |
3182 | (Aggregate_Bounds (N), | |
3183 | Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N)))); | |
996ae0b0 RK |
3184 | end if; |
3185 | ||
d976bf74 | 3186 | -- Check the dimensions of each component in the array aggregate |
0929eaeb AC |
3187 | |
3188 | Analyze_Dimension_Array_Aggregate (N, Component_Typ); | |
3189 | ||
996ae0b0 RK |
3190 | return Success; |
3191 | end Resolve_Array_Aggregate; | |
3192 | ||
745f5698 ES |
3193 | --------------------------------- |
3194 | -- Resolve_Container_Aggregate -- | |
3195 | --------------------------------- | |
3196 | ||
3197 | procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
c0bab60b | 3198 | procedure Resolve_Iterated_Association |
4f6ebe2a ES |
3199 | (Comp : Node_Id; |
3200 | Key_Type : Entity_Id; | |
3201 | Elmt_Type : Entity_Id); | |
c0bab60b ES |
3202 | -- Resolve choices and expression in an iterated component association |
3203 | -- or an iterated element association, which has a key_expression. | |
0b4034c0 GD |
3204 | -- This is similar but not identical to the handling of this construct |
3205 | -- in an array aggregate. | |
4f6ebe2a | 3206 | -- For a named container, the type of each choice must be compatible |
0b4034c0 | 3207 | -- with the key type. For a positional container, the choice must be |
4f6ebe2a ES |
3208 | -- a subtype indication or an iterator specification that determines |
3209 | -- an element type. | |
3210 | ||
745f5698 ES |
3211 | Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate); |
3212 | ||
3213 | Empty_Subp : Node_Id := Empty; | |
3214 | Add_Named_Subp : Node_Id := Empty; | |
3215 | Add_Unnamed_Subp : Node_Id := Empty; | |
3216 | New_Indexed_Subp : Node_Id := Empty; | |
3217 | Assign_Indexed_Subp : Node_Id := Empty; | |
3218 | ||
c0bab60b ES |
3219 | ---------------------------------- |
3220 | -- Resolve_Iterated_Association -- | |
3221 | ---------------------------------- | |
4f6ebe2a | 3222 | |
c0bab60b | 3223 | procedure Resolve_Iterated_Association |
4f6ebe2a ES |
3224 | (Comp : Node_Id; |
3225 | Key_Type : Entity_Id; | |
3226 | Elmt_Type : Entity_Id) | |
3227 | is | |
439dae60 | 3228 | Loc : constant Source_Ptr := Sloc (N); |
c0bab60b | 3229 | Choice : Node_Id; |
aacbf376 | 3230 | Copy : Node_Id; |
c0bab60b ES |
3231 | Ent : Entity_Id; |
3232 | Expr : Node_Id; | |
3233 | Key_Expr : Node_Id; | |
3234 | Id : Entity_Id; | |
3235 | Id_Name : Name_Id; | |
c0bab60b | 3236 | Typ : Entity_Id := Empty; |
4f6ebe2a ES |
3237 | |
3238 | begin | |
439dae60 BD |
3239 | Error_Msg_Ada_2022_Feature ("iterated component", Loc); |
3240 | ||
c0bab60b ES |
3241 | -- If this is an Iterated_Element_Association then either a |
3242 | -- an Iterator_Specification or a Loop_Parameter specification | |
3243 | -- is present. In both cases a Key_Expression is present. | |
3244 | ||
3245 | if Nkind (Comp) = N_Iterated_Element_Association then | |
aacbf376 MP |
3246 | |
3247 | -- Create a temporary scope to avoid some modifications from | |
3248 | -- escaping the Analyze call below. The original Tree will be | |
3249 | -- reanalyzed later. | |
3250 | ||
3251 | Ent := New_Internal_Entity | |
3252 | (E_Loop, Current_Scope, Sloc (Comp), 'L'); | |
3253 | Set_Etype (Ent, Standard_Void_Type); | |
3254 | Set_Parent (Ent, Parent (Comp)); | |
3255 | Push_Scope (Ent); | |
3256 | ||
c0bab60b | 3257 | if Present (Loop_Parameter_Specification (Comp)) then |
aacbf376 MP |
3258 | Copy := Copy_Separate_Tree (Comp); |
3259 | ||
3260 | Analyze | |
3261 | (Loop_Parameter_Specification (Copy)); | |
3262 | ||
c0bab60b ES |
3263 | Id_Name := Chars (Defining_Identifier |
3264 | (Loop_Parameter_Specification (Comp))); | |
3265 | else | |
aacbf376 MP |
3266 | Copy := Copy_Separate_Tree (Iterator_Specification (Comp)); |
3267 | Analyze (Copy); | |
3268 | ||
c0bab60b ES |
3269 | Id_Name := Chars (Defining_Identifier |
3270 | (Iterator_Specification (Comp))); | |
3271 | end if; | |
3272 | ||
3273 | -- Key expression must have the type of the key. We analyze | |
3274 | -- a copy of the original expression, because it will be | |
3275 | -- reanalyzed and copied as needed during expansion of the | |
3276 | -- corresponding loop. | |
3277 | ||
3278 | Key_Expr := Key_Expression (Comp); | |
3279 | Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type); | |
aacbf376 | 3280 | End_Scope; |
c0bab60b | 3281 | |
99c419b2 VI |
3282 | Typ := Key_Type; |
3283 | ||
c0bab60b | 3284 | elsif Present (Iterator_Specification (Comp)) then |
c57fbb12 VI |
3285 | -- Create a temporary scope to avoid some modifications from |
3286 | -- escaping the Analyze call below. The original Tree will be | |
3287 | -- reanalyzed later. | |
3288 | ||
3289 | Ent := New_Internal_Entity | |
3290 | (E_Loop, Current_Scope, Sloc (Comp), 'L'); | |
3291 | Set_Etype (Ent, Standard_Void_Type); | |
3292 | Set_Parent (Ent, Parent (Comp)); | |
3293 | Push_Scope (Ent); | |
3294 | ||
aacbf376 | 3295 | Copy := Copy_Separate_Tree (Iterator_Specification (Comp)); |
d983abeb PT |
3296 | Id_Name := |
3297 | Chars (Defining_Identifier (Iterator_Specification (Comp))); | |
aacbf376 | 3298 | |
c57fbb12 VI |
3299 | Preanalyze (Copy); |
3300 | ||
3301 | End_Scope; | |
3302 | ||
aacbf376 | 3303 | Typ := Etype (Defining_Identifier (Copy)); |
4f6ebe2a | 3304 | |
8092c199 AC |
3305 | else |
3306 | Choice := First (Discrete_Choices (Comp)); | |
4f6ebe2a | 3307 | |
8092c199 AC |
3308 | while Present (Choice) loop |
3309 | Analyze (Choice); | |
4f6ebe2a | 3310 | |
8092c199 | 3311 | -- Choice can be a subtype name, a range, or an expression |
4f6ebe2a | 3312 | |
8092c199 AC |
3313 | if Is_Entity_Name (Choice) |
3314 | and then Is_Type (Entity (Choice)) | |
3315 | and then Base_Type (Entity (Choice)) = Base_Type (Key_Type) | |
3316 | then | |
3317 | null; | |
4f6ebe2a | 3318 | |
8092c199 AC |
3319 | elsif Present (Key_Type) then |
3320 | Analyze_And_Resolve (Choice, Key_Type); | |
99c419b2 | 3321 | Typ := Key_Type; |
8092c199 AC |
3322 | else |
3323 | Typ := Etype (Choice); -- assume unique for now | |
3324 | end if; | |
4f6ebe2a | 3325 | |
8092c199 AC |
3326 | Next (Choice); |
3327 | end loop; | |
c0bab60b ES |
3328 | |
3329 | Id_Name := Chars (Defining_Identifier (Comp)); | |
8092c199 | 3330 | end if; |
4f6ebe2a ES |
3331 | |
3332 | -- Create a scope in which to introduce an index, which is usually | |
3333 | -- visible in the expression for the component, and needed for its | |
3334 | -- analysis. | |
3335 | ||
c0bab60b | 3336 | Id := Make_Defining_Identifier (Sloc (Comp), Id_Name); |
aacbf376 MP |
3337 | Ent := New_Internal_Entity (E_Loop, |
3338 | Current_Scope, Sloc (Comp), 'L'); | |
4f6ebe2a ES |
3339 | Set_Etype (Ent, Standard_Void_Type); |
3340 | Set_Parent (Ent, Parent (Comp)); | |
3341 | Push_Scope (Ent); | |
4f6ebe2a ES |
3342 | |
3343 | -- Insert and decorate the loop variable in the current scope. | |
3344 | -- The expression has to be analyzed once the loop variable is | |
3345 | -- directly visible. Mark the variable as referenced to prevent | |
3346 | -- spurious warnings, given that subsequent uses of its name in the | |
3347 | -- expression will reference the internal (synonym) loop variable. | |
3348 | ||
3349 | Enter_Name (Id); | |
fa02302b | 3350 | |
99c419b2 VI |
3351 | pragma Assert (Present (Typ)); |
3352 | Set_Etype (Id, Typ); | |
4f6ebe2a | 3353 | |
2e02ab86 | 3354 | Mutate_Ekind (Id, E_Variable); |
25e4024c | 3355 | Set_Is_Not_Self_Hidden (Id); |
4f6ebe2a ES |
3356 | Set_Scope (Id, Ent); |
3357 | Set_Referenced (Id); | |
3358 | ||
3359 | -- Analyze a copy of the expression, to verify legality. We use | |
3360 | -- a copy because the expression will be analyzed anew when the | |
3361 | -- enclosing aggregate is expanded, and the construct is rewritten | |
3362 | -- as a loop with a new index variable. | |
3363 | ||
3364 | Expr := New_Copy_Tree (Expression (Comp)); | |
3365 | Preanalyze_And_Resolve (Expr, Elmt_Type); | |
3366 | End_Scope; | |
c0bab60b ES |
3367 | |
3368 | end Resolve_Iterated_Association; | |
4f6ebe2a | 3369 | |
aacbf376 MP |
3370 | -- Start of processing for Resolve_Container_Aggregate |
3371 | ||
745f5698 | 3372 | begin |
4f6ebe2a | 3373 | pragma Assert (Nkind (Asp) = N_Aggregate); |
745f5698 | 3374 | |
4f6ebe2a ES |
3375 | Set_Etype (N, Typ); |
3376 | Parse_Aspect_Aggregate (Asp, | |
3377 | Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp, | |
3378 | New_Indexed_Subp, Assign_Indexed_Subp); | |
3379 | ||
7a21651f ES |
3380 | if Present (Add_Unnamed_Subp) |
3381 | and then No (New_Indexed_Subp) | |
67138e09 | 3382 | and then Present (Etype (Add_Unnamed_Subp)) |
3da0e4ae | 3383 | and then Etype (Add_Unnamed_Subp) /= Any_Type |
7a21651f | 3384 | then |
4f6ebe2a ES |
3385 | declare |
3386 | Elmt_Type : constant Entity_Id := | |
3387 | Etype (Next_Formal | |
3388 | (First_Formal (Entity (Add_Unnamed_Subp)))); | |
3389 | Comp : Node_Id; | |
3390 | ||
3391 | begin | |
3392 | if Present (Expressions (N)) then | |
3393 | -- positional aggregate | |
3394 | ||
3395 | Comp := First (Expressions (N)); | |
3396 | while Present (Comp) loop | |
3397 | Analyze_And_Resolve (Comp, Elmt_Type); | |
3398 | Next (Comp); | |
3399 | end loop; | |
3400 | end if; | |
745f5698 | 3401 | |
4f6ebe2a ES |
3402 | -- Empty aggregate, to be replaced by Empty during |
3403 | -- expansion, or iterated component association. | |
3404 | ||
3405 | if Present (Component_Associations (N)) then | |
3406 | declare | |
3407 | Comp : Node_Id := First (Component_Associations (N)); | |
3408 | begin | |
745f5698 | 3409 | while Present (Comp) loop |
4f6ebe2a ES |
3410 | if Nkind (Comp) /= |
3411 | N_Iterated_Component_Association | |
3412 | then | |
3413 | Error_Msg_N ("illegal component association " | |
3414 | & "for unnamed container aggregate", Comp); | |
3415 | return; | |
3416 | else | |
c0bab60b | 3417 | Resolve_Iterated_Association |
4f6ebe2a ES |
3418 | (Comp, Empty, Elmt_Type); |
3419 | end if; | |
3420 | ||
745f5698 ES |
3421 | Next (Comp); |
3422 | end loop; | |
4f6ebe2a ES |
3423 | end; |
3424 | end if; | |
3425 | end; | |
745f5698 | 3426 | |
3da0e4ae MP |
3427 | elsif Present (Add_Named_Subp) |
3428 | and then Etype (Add_Named_Subp) /= Any_Type | |
3429 | then | |
4f6ebe2a ES |
3430 | declare |
3431 | -- Retrieves types of container, key, and element from the | |
3432 | -- specified insertion procedure. | |
3433 | ||
3434 | Container : constant Entity_Id := | |
3435 | First_Formal (Entity (Add_Named_Subp)); | |
3436 | Key_Type : constant Entity_Id := Etype (Next_Formal (Container)); | |
3437 | Elmt_Type : constant Entity_Id := | |
3438 | Etype (Next_Formal (Next_Formal (Container))); | |
3439 | Comp : Node_Id; | |
3440 | Choice : Node_Id; | |
3441 | ||
3442 | begin | |
3443 | Comp := First (Component_Associations (N)); | |
3444 | while Present (Comp) loop | |
3445 | if Nkind (Comp) = N_Component_Association then | |
3446 | Choice := First (Choices (Comp)); | |
3447 | ||
3448 | while Present (Choice) loop | |
3449 | Analyze_And_Resolve (Choice, Key_Type); | |
7a21651f | 3450 | if not Is_Static_Expression (Choice) then |
9ed2b86d | 3451 | Error_Msg_N ("choice must be static", Choice); |
7a21651f ES |
3452 | end if; |
3453 | ||
4f6ebe2a ES |
3454 | Next (Choice); |
3455 | end loop; | |
3456 | ||
3457 | Analyze_And_Resolve (Expression (Comp), Elmt_Type); | |
3458 | ||
c0bab60b ES |
3459 | elsif Nkind (Comp) in |
3460 | N_Iterated_Component_Association | | |
3461 | N_Iterated_Element_Association | |
3462 | then | |
3463 | Resolve_Iterated_Association | |
4f6ebe2a | 3464 | (Comp, Key_Type, Elmt_Type); |
745f5698 | 3465 | end if; |
4f6ebe2a ES |
3466 | |
3467 | Next (Comp); | |
3468 | end loop; | |
3469 | end; | |
7a21651f | 3470 | |
3da0e4ae MP |
3471 | elsif Present (Assign_Indexed_Subp) |
3472 | and then Etype (Assign_Indexed_Subp) /= Any_Type | |
3473 | then | |
08c8883f ES |
3474 | -- Indexed Aggregate. Positional or indexed component |
3475 | -- can be present, but not both. Choices must be static | |
3476 | -- values or ranges with static bounds. | |
7a21651f ES |
3477 | |
3478 | declare | |
3479 | Container : constant Entity_Id := | |
3480 | First_Formal (Entity (Assign_Indexed_Subp)); | |
3481 | Index_Type : constant Entity_Id := Etype (Next_Formal (Container)); | |
3482 | Comp_Type : constant Entity_Id := | |
3483 | Etype (Next_Formal (Next_Formal (Container))); | |
d4780139 ES |
3484 | Comp : Node_Id; |
3485 | Choice : Node_Id; | |
3486 | Num_Choices : Nat := 0; | |
7a21651f | 3487 | |
d4780139 ES |
3488 | Hi_Val : Uint; |
3489 | Lo_Val : Uint; | |
7a21651f ES |
3490 | begin |
3491 | if Present (Expressions (N)) then | |
3492 | Comp := First (Expressions (N)); | |
3493 | while Present (Comp) loop | |
3494 | Analyze_And_Resolve (Comp, Comp_Type); | |
3495 | Next (Comp); | |
3496 | end loop; | |
3497 | end if; | |
3498 | ||
10c257af ES |
3499 | if Present (Component_Associations (N)) |
3500 | and then not Is_Empty_List (Component_Associations (N)) | |
3501 | then | |
3502 | if Present (Expressions (N)) | |
3503 | and then not Is_Empty_List (Expressions (N)) | |
3504 | then | |
9ed2b86d | 3505 | Error_Msg_N ("container aggregate cannot be " |
08c8883f ES |
3506 | & "both positional and named", N); |
3507 | return; | |
3508 | end if; | |
3509 | ||
d4780139 | 3510 | Comp := First (Component_Associations (N)); |
7a21651f ES |
3511 | |
3512 | while Present (Comp) loop | |
3513 | if Nkind (Comp) = N_Component_Association then | |
3514 | Choice := First (Choices (Comp)); | |
3515 | ||
3516 | while Present (Choice) loop | |
3517 | Analyze_And_Resolve (Choice, Index_Type); | |
d4780139 | 3518 | Num_Choices := Num_Choices + 1; |
7a21651f ES |
3519 | Next (Choice); |
3520 | end loop; | |
3521 | ||
3522 | Analyze_And_Resolve (Expression (Comp), Comp_Type); | |
3523 | ||
c0bab60b ES |
3524 | elsif Nkind (Comp) in |
3525 | N_Iterated_Component_Association | | |
3526 | N_Iterated_Element_Association | |
3527 | then | |
3528 | Resolve_Iterated_Association | |
7a21651f | 3529 | (Comp, Index_Type, Comp_Type); |
d4780139 | 3530 | Num_Choices := Num_Choices + 1; |
7a21651f ES |
3531 | end if; |
3532 | ||
3533 | Next (Comp); | |
3534 | end loop; | |
d4780139 ES |
3535 | |
3536 | -- The component associations in an indexed aggregate | |
3537 | -- must denote a contiguous set of static values. We | |
3538 | -- build a table of values/ranges and sort it, as is done | |
3539 | -- elsewhere for case statements and array aggregates. | |
3540 | -- If the aggregate has a single iterated association it | |
3541 | -- is allowed to be nonstatic and there is nothing to check. | |
3542 | ||
3543 | if Num_Choices > 1 then | |
3544 | declare | |
3545 | Table : Case_Table_Type (1 .. Num_Choices); | |
3546 | No_Choice : Pos := 1; | |
3547 | Lo, Hi : Node_Id; | |
3548 | ||
3549 | -- Traverse aggregate to determine size of needed table. | |
3550 | -- Verify that bounds are static and that loops have no | |
3551 | -- filters or key expressions. | |
3552 | ||
3553 | begin | |
3554 | Comp := First (Component_Associations (N)); | |
3555 | while Present (Comp) loop | |
3556 | if Nkind (Comp) = N_Iterated_Element_Association then | |
3557 | if Present | |
3558 | (Loop_Parameter_Specification (Comp)) | |
3559 | then | |
3560 | if Present (Iterator_Filter | |
3561 | (Loop_Parameter_Specification (Comp))) | |
3562 | then | |
3563 | Error_Msg_N | |
3564 | ("iterator filter not allowed " & | |
3565 | "in indexed aggregate", Comp); | |
3566 | return; | |
3567 | ||
3568 | elsif Present (Key_Expression | |
3569 | (Loop_Parameter_Specification (Comp))) | |
3570 | then | |
3571 | Error_Msg_N | |
3572 | ("key expression not allowed " & | |
3573 | "in indexed aggregate", Comp); | |
3574 | return; | |
3575 | end if; | |
3576 | end if; | |
3577 | else | |
3578 | Choice := First (Choices (Comp)); | |
3579 | ||
3580 | while Present (Choice) loop | |
3581 | Get_Index_Bounds (Choice, Lo, Hi); | |
3582 | Table (No_Choice).Choice := Choice; | |
3583 | Table (No_Choice).Lo := Lo; | |
3584 | Table (No_Choice).Hi := Hi; | |
3585 | ||
3586 | -- Verify staticness of value or range | |
3587 | ||
3588 | if not Is_Static_Expression (Lo) | |
3589 | or else not Is_Static_Expression (Hi) | |
3590 | then | |
3591 | Error_Msg_N | |
3592 | ("nonstatic expression for index " & | |
3593 | "for indexed aggregate", Choice); | |
3594 | return; | |
3595 | end if; | |
3596 | ||
3597 | No_Choice := No_Choice + 1; | |
3598 | Next (Choice); | |
3599 | end loop; | |
3600 | end if; | |
3601 | ||
3602 | Next (Comp); | |
3603 | end loop; | |
3604 | ||
3605 | Sort_Case_Table (Table); | |
3606 | ||
3607 | for J in 1 .. Num_Choices - 1 loop | |
3608 | Hi_Val := Expr_Value (Table (J).Hi); | |
3609 | Lo_Val := Expr_Value (Table (J + 1).Lo); | |
3610 | ||
3611 | if Lo_Val = Hi_Val then | |
3612 | Error_Msg_N | |
3613 | ("duplicate index in indexed aggregate", | |
3614 | Table (J + 1).Choice); | |
3615 | exit; | |
3616 | ||
3617 | elsif Lo_Val < Hi_Val then | |
3618 | Error_Msg_N | |
3619 | ("overlapping indices in indexed aggregate", | |
3620 | Table (J + 1).Choice); | |
3621 | exit; | |
3622 | ||
3623 | elsif Lo_Val > Hi_Val + 1 then | |
3624 | Error_Msg_N | |
3625 | ("missing index values", Table (J + 1).Choice); | |
3626 | exit; | |
3627 | end if; | |
3628 | end loop; | |
3629 | end; | |
3630 | end if; | |
7a21651f ES |
3631 | end if; |
3632 | end; | |
745f5698 ES |
3633 | end if; |
3634 | end Resolve_Container_Aggregate; | |
3635 | ||
9eb8d5b4 AC |
3636 | ----------------------------- |
3637 | -- Resolve_Delta_Aggregate -- | |
3638 | ----------------------------- | |
3639 | ||
3640 | procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
d63199d8 | 3641 | Base : constant Node_Id := Expression (N); |
8d9a1ba7 PMR |
3642 | |
3643 | begin | |
81e68a19 | 3644 | Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N)); |
32501d71 | 3645 | |
8d9a1ba7 PMR |
3646 | if not Is_Composite_Type (Typ) then |
3647 | Error_Msg_N ("not a composite type", N); | |
3648 | end if; | |
3649 | ||
3650 | Analyze_And_Resolve (Base, Typ); | |
3651 | ||
3652 | if Is_Array_Type (Typ) then | |
79e02673 PT |
3653 | -- For an array_delta_aggregate, the base_expression and each |
3654 | -- expression in every array_component_association shall be of a | |
3655 | -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated | |
3656 | -- errors we only check the base expression and not array component | |
3657 | -- associations. | |
3658 | ||
3659 | if Is_Limited_Type (Etype (Base)) then | |
3660 | Error_Msg_N | |
3661 | ("array delta aggregate shall be of a nonlimited type", Base); | |
3662 | Explain_Limited_Type (Etype (Base), Base); | |
3663 | end if; | |
3664 | ||
8d9a1ba7 PMR |
3665 | Resolve_Delta_Array_Aggregate (N, Typ); |
3666 | else | |
9af8c27f MP |
3667 | |
3668 | -- Delta aggregates for record types must use parentheses, | |
3669 | -- not square brackets. | |
3670 | ||
3671 | if Is_Homogeneous_Aggregate (N) then | |
3672 | Error_Msg_N | |
3673 | ("delta aggregates for record types must use (), not '[']", N); | |
3674 | end if; | |
3675 | ||
79e02673 PT |
3676 | -- The base_expression of a record_delta_aggregate can be of a |
3677 | -- limited type only if it is newly constructed; RM 7.5(2.1/5). | |
3678 | ||
3679 | Check_Expr_OK_In_Limited_Aggregate (Base); | |
3680 | ||
8d9a1ba7 PMR |
3681 | Resolve_Delta_Record_Aggregate (N, Typ); |
3682 | end if; | |
3683 | ||
3684 | Set_Etype (N, Typ); | |
3685 | end Resolve_Delta_Aggregate; | |
3686 | ||
3687 | ----------------------------------- | |
3688 | -- Resolve_Delta_Array_Aggregate -- | |
3689 | ----------------------------------- | |
3690 | ||
3691 | procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
845c4936 PT |
3692 | Deltas : constant List_Id := Component_Associations (N); |
3693 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
d63199d8 | 3694 | |
845c4936 PT |
3695 | Assoc : Node_Id; |
3696 | Choice : Node_Id; | |
cfc3a1db | 3697 | Expr : Node_Id; |
9eb8d5b4 | 3698 | |
8d9a1ba7 | 3699 | begin |
8d9a1ba7 PMR |
3700 | Assoc := First (Deltas); |
3701 | while Present (Assoc) loop | |
3702 | if Nkind (Assoc) = N_Iterated_Component_Association then | |
3703 | Choice := First (Choice_List (Assoc)); | |
3704 | while Present (Choice) loop | |
3705 | if Nkind (Choice) = N_Others_Choice then | |
3706 | Error_Msg_N | |
9ed2b86d | 3707 | ("OTHERS not allowed in delta aggregate", Choice); |
9eb8d5b4 | 3708 | |
3e65b68d PT |
3709 | elsif Nkind (Choice) = N_Subtype_Indication then |
3710 | Resolve_Discrete_Subtype_Indication | |
3711 | (Choice, Base_Type (Index_Type)); | |
3712 | ||
8d9a1ba7 PMR |
3713 | else |
3714 | Analyze_And_Resolve (Choice, Index_Type); | |
3715 | end if; | |
9eb8d5b4 | 3716 | |
8d9a1ba7 PMR |
3717 | Next (Choice); |
3718 | end loop; | |
9eb8d5b4 | 3719 | |
8d9a1ba7 PMR |
3720 | declare |
3721 | Id : constant Entity_Id := Defining_Identifier (Assoc); | |
3722 | Ent : constant Entity_Id := | |
3723 | New_Internal_Entity | |
3724 | (E_Loop, Current_Scope, Sloc (Assoc), 'L'); | |
9eb8d5b4 | 3725 | |
8d9a1ba7 PMR |
3726 | begin |
3727 | Set_Etype (Ent, Standard_Void_Type); | |
3728 | Set_Parent (Ent, Assoc); | |
70b590e2 | 3729 | Push_Scope (Ent); |
8d9a1ba7 PMR |
3730 | |
3731 | if No (Scope (Id)) then | |
8d9a1ba7 | 3732 | Set_Etype (Id, Index_Type); |
2e02ab86 | 3733 | Mutate_Ekind (Id, E_Variable); |
25e4024c | 3734 | Set_Is_Not_Self_Hidden (Id); |
8d9a1ba7 | 3735 | Set_Scope (Id, Ent); |
9eb8d5b4 | 3736 | end if; |
70b590e2 | 3737 | Enter_Name (Id); |
9eb8d5b4 | 3738 | |
cfc3a1db ES |
3739 | -- Resolve a copy of the expression, after setting |
3740 | -- its parent properly to preserve its context. | |
3741 | ||
3742 | Expr := New_Copy_Tree (Expression (Assoc)); | |
3743 | Set_Parent (Expr, Assoc); | |
3744 | Analyze_And_Resolve (Expr, Component_Type (Typ)); | |
8d9a1ba7 PMR |
3745 | End_Scope; |
3746 | end; | |
9eb8d5b4 | 3747 | |
8d9a1ba7 PMR |
3748 | else |
3749 | Choice := First (Choice_List (Assoc)); | |
3750 | while Present (Choice) loop | |
0191a24e SB |
3751 | if Is_Deep_Choice (Choice, Typ) then |
3752 | pragma Assert (All_Extensions_Allowed); | |
3e65b68d | 3753 | |
0191a24e SB |
3754 | -- a deep delta aggregate |
3755 | Resolve_Deep_Delta_Assoc (Assoc, Typ); | |
3756 | else | |
3757 | Analyze (Choice); | |
9eb8d5b4 | 3758 | |
0191a24e SB |
3759 | if Nkind (Choice) = N_Others_Choice then |
3760 | Error_Msg_N | |
3761 | ("OTHERS not allowed in delta aggregate", Choice); | |
d63199d8 | 3762 | |
0191a24e SB |
3763 | elsif Is_Entity_Name (Choice) |
3764 | and then Is_Type (Entity (Choice)) | |
8d9a1ba7 | 3765 | then |
0191a24e | 3766 | -- Choice covers a range of values |
3e65b68d | 3767 | |
0191a24e SB |
3768 | if Base_Type (Entity (Choice)) /= |
3769 | Base_Type (Index_Type) | |
3770 | then | |
3771 | Error_Msg_NE | |
3772 | ("choice does not match index type of &", | |
3773 | Choice, Typ); | |
3774 | end if; | |
3e65b68d | 3775 | |
0191a24e SB |
3776 | elsif Nkind (Choice) = N_Subtype_Indication then |
3777 | Resolve_Discrete_Subtype_Indication | |
3778 | (Choice, Base_Type (Index_Type)); | |
3779 | ||
3780 | else | |
3781 | Resolve (Choice, Index_Type); | |
3782 | end if; | |
8d9a1ba7 | 3783 | end if; |
9eb8d5b4 | 3784 | |
8d9a1ba7 PMR |
3785 | Next (Choice); |
3786 | end loop; | |
3787 | ||
2caaa4bf PT |
3788 | -- For an array_delta_aggregate, the array_component_association |
3789 | -- shall not use the box symbol <>; RM 4.3.4(11/5). | |
3790 | ||
3791 | pragma Assert | |
3792 | (Box_Present (Assoc) xor Present (Expression (Assoc))); | |
3793 | ||
3794 | if Box_Present (Assoc) then | |
3795 | Error_Msg_N | |
3796 | ("'<'> in array delta aggregate is not allowed", Assoc); | |
3797 | else | |
3798 | Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ)); | |
3799 | end if; | |
8d9a1ba7 | 3800 | end if; |
9313a26a | 3801 | |
8d9a1ba7 PMR |
3802 | Next (Assoc); |
3803 | end loop; | |
3804 | end Resolve_Delta_Array_Aggregate; | |
3805 | ||
3806 | ------------------------------------ | |
3807 | -- Resolve_Delta_Record_Aggregate -- | |
3808 | ------------------------------------ | |
3809 | ||
3ec54569 PMR |
3810 | procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is |
3811 | ||
3812 | -- Variables used to verify that discriminant-dependent components | |
3813 | -- appear in the same variant. | |
3814 | ||
5612989e | 3815 | Comp_Ref : Entity_Id := Empty; -- init to avoid warning |
3ec54569 PMR |
3816 | Variant : Node_Id; |
3817 | ||
0191a24e | 3818 | procedure Check_Variant (Id : Node_Id); |
8d9a1ba7 PMR |
3819 | -- If a given component of the delta aggregate appears in a variant |
3820 | -- part, verify that it is within the same variant as that of previous | |
3821 | -- specified variant components of the delta. | |
9313a26a | 3822 | |
0191a24e SB |
3823 | function Get_Component_Type |
3824 | (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id; | |
3825 | -- Locate component with a given name and return its type. | |
3826 | -- If none found then report error and return Empty. | |
d63199d8 PMR |
3827 | |
3828 | function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean; | |
3829 | -- Determine whether variant V1 is within variant V2 | |
9eb8d5b4 | 3830 | |
2aa814cb PT |
3831 | function Variant_Depth (N : Node_Id) return Natural; |
3832 | -- Determine the distance of a variant to the enclosing type declaration | |
8d9a1ba7 PMR |
3833 | |
3834 | -------------------- | |
3835 | -- Check_Variant -- | |
3836 | -------------------- | |
3837 | ||
0191a24e | 3838 | procedure Check_Variant (Id : Node_Id) is |
8d9a1ba7 PMR |
3839 | Comp : Entity_Id; |
3840 | Comp_Variant : Node_Id; | |
3841 | ||
3842 | begin | |
3843 | if not Has_Discriminants (Typ) then | |
3844 | return; | |
3845 | end if; | |
9eb8d5b4 | 3846 | |
8d9a1ba7 PMR |
3847 | Comp := First_Entity (Typ); |
3848 | while Present (Comp) loop | |
3849 | exit when Chars (Comp) = Chars (Id); | |
3850 | Next_Component (Comp); | |
3851 | end loop; | |
3852 | ||
3853 | -- Find the variant, if any, whose component list includes the | |
3854 | -- component declaration. | |
3855 | ||
3856 | Comp_Variant := Parent (Parent (List_Containing (Parent (Comp)))); | |
3857 | if Nkind (Comp_Variant) = N_Variant then | |
3858 | if No (Variant) then | |
3859 | Variant := Comp_Variant; | |
3860 | Comp_Ref := Comp; | |
3861 | ||
3862 | elsif Variant /= Comp_Variant then | |
9eb8d5b4 | 3863 | declare |
8d9a1ba7 PMR |
3864 | D1 : constant Integer := Variant_Depth (Variant); |
3865 | D2 : constant Integer := Variant_Depth (Comp_Variant); | |
9eb8d5b4 AC |
3866 | |
3867 | begin | |
8d9a1ba7 PMR |
3868 | if D1 = D2 |
3869 | or else | |
d63199d8 | 3870 | (D1 > D2 and then not Nested_In (Variant, Comp_Variant)) |
8d9a1ba7 | 3871 | or else |
d63199d8 | 3872 | (D2 > D1 and then not Nested_In (Comp_Variant, Variant)) |
8d9a1ba7 | 3873 | then |
5612989e | 3874 | pragma Assert (Present (Comp_Ref)); |
8d9a1ba7 PMR |
3875 | Error_Msg_Node_2 := Comp_Ref; |
3876 | Error_Msg_NE | |
3877 | ("& and & appear in different variants", Id, Comp); | |
3878 | ||
3879 | -- Otherwise retain the deeper variant for subsequent tests | |
9eb8d5b4 | 3880 | |
8d9a1ba7 PMR |
3881 | elsif D2 > D1 then |
3882 | Variant := Comp_Variant; | |
3883 | end if; | |
9eb8d5b4 | 3884 | end; |
8d9a1ba7 PMR |
3885 | end if; |
3886 | end if; | |
3887 | end Check_Variant; | |
9eb8d5b4 | 3888 | |
0191a24e SB |
3889 | ------------------------ |
3890 | -- Get_Component_Type -- | |
3891 | ------------------------ | |
d63199d8 | 3892 | |
0191a24e SB |
3893 | function Get_Component_Type |
3894 | (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id | |
3895 | is | |
d63199d8 | 3896 | Comp : Entity_Id; |
d63199d8 | 3897 | begin |
0191a24e SB |
3898 | case Nkind (Selector) is |
3899 | when N_Selected_Component | N_Indexed_Component => | |
3900 | -- a deep delta aggregate choice | |
3901 | ||
3902 | declare | |
3903 | Prefix_Type : constant Entity_Id := | |
3904 | Get_Component_Type (Prefix (Selector), Enclosing_Type); | |
3905 | begin | |
d75ca8a6 | 3906 | if No (Prefix_Type) then |
0191a24e SB |
3907 | pragma Assert (Serious_Errors_Detected > 0); |
3908 | return Empty; | |
3909 | end if; | |
3910 | ||
3911 | -- Set the type of the prefix for GNATprove | |
3912 | ||
3913 | Set_Etype (Prefix (Selector), Prefix_Type); | |
3914 | ||
3915 | if Nkind (Selector) = N_Selected_Component then | |
3916 | return Get_Component_Type | |
3917 | (Selector_Name (Selector), | |
3918 | Enclosing_Type => Prefix_Type); | |
3919 | elsif not Is_Array_Type (Prefix_Type) then | |
3920 | Error_Msg_NE | |
3921 | ("type& is not an array type", | |
3922 | Selector, Prefix_Type); | |
3923 | elsif Number_Dimensions (Prefix_Type) /= 1 then | |
3924 | Error_Msg_NE | |
3925 | ("array type& not one-dimensional", | |
3926 | Selector, Prefix_Type); | |
3927 | elsif List_Length (Expressions (Selector)) /= 1 then | |
3928 | Error_Msg_NE | |
3929 | ("wrong number of indices for array type&", | |
3930 | Selector, Prefix_Type); | |
3931 | else | |
3932 | Analyze_And_Resolve | |
3933 | (First (Expressions (Selector)), | |
3934 | Etype (First_Index (Prefix_Type))); | |
3935 | return Component_Type (Prefix_Type); | |
3936 | end if; | |
3937 | end; | |
3938 | ||
3939 | when others => | |
3940 | null; | |
3941 | end case; | |
3942 | ||
3943 | Comp := First_Entity (Enclosing_Type); | |
d63199d8 | 3944 | while Present (Comp) loop |
0191a24e | 3945 | if Chars (Comp) = Chars (Selector) then |
d63199d8 | 3946 | if Ekind (Comp) = E_Discriminant then |
0191a24e | 3947 | Error_Msg_N ("delta cannot apply to discriminant", Selector); |
d63199d8 PMR |
3948 | end if; |
3949 | ||
0191a24e SB |
3950 | Set_Entity (Selector, Comp); |
3951 | Set_Etype (Selector, Etype (Comp)); | |
3952 | ||
3953 | return Etype (Comp); | |
d63199d8 PMR |
3954 | end if; |
3955 | ||
99859ea7 | 3956 | Next_Entity (Comp); |
d63199d8 PMR |
3957 | end loop; |
3958 | ||
0191a24e SB |
3959 | Error_Msg_NE |
3960 | ("type& has no component with this name", Selector, Enclosing_Type); | |
484d58c5 | 3961 | return Empty; |
0191a24e | 3962 | end Get_Component_Type; |
d63199d8 | 3963 | |
8d9a1ba7 PMR |
3964 | --------------- |
3965 | -- Nested_In -- | |
3966 | --------------- | |
9eb8d5b4 | 3967 | |
8d9a1ba7 PMR |
3968 | function Nested_In (V1, V2 : Node_Id) return Boolean is |
3969 | Par : Node_Id; | |
d63199d8 | 3970 | |
8d9a1ba7 PMR |
3971 | begin |
3972 | Par := Parent (V1); | |
3973 | while Nkind (Par) /= N_Full_Type_Declaration loop | |
3974 | if Par = V2 then | |
3975 | return True; | |
3976 | end if; | |
d63199d8 | 3977 | |
8d9a1ba7 PMR |
3978 | Par := Parent (Par); |
3979 | end loop; | |
9eb8d5b4 | 3980 | |
8d9a1ba7 PMR |
3981 | return False; |
3982 | end Nested_In; | |
3983 | ||
3984 | ------------------- | |
3985 | -- Variant_Depth -- | |
3986 | ------------------- | |
9eb8d5b4 | 3987 | |
2aa814cb PT |
3988 | function Variant_Depth (N : Node_Id) return Natural is |
3989 | Depth : Natural; | |
8d9a1ba7 | 3990 | Par : Node_Id; |
d63199d8 | 3991 | |
8d9a1ba7 PMR |
3992 | begin |
3993 | Depth := 0; | |
3994 | Par := Parent (N); | |
3995 | while Nkind (Par) /= N_Full_Type_Declaration loop | |
3996 | Depth := Depth + 1; | |
d63199d8 | 3997 | Par := Parent (Par); |
8d9a1ba7 PMR |
3998 | end loop; |
3999 | ||
4000 | return Depth; | |
4001 | end Variant_Depth; | |
4002 | ||
d63199d8 | 4003 | -- Local variables |
9eb8d5b4 | 4004 | |
d63199d8 | 4005 | Deltas : constant List_Id := Component_Associations (N); |
9eb8d5b4 | 4006 | |
0191a24e SB |
4007 | Assoc : Node_Id; |
4008 | Choice : Node_Id; | |
4009 | Comp_Type : Entity_Id := Empty; -- init to avoid warning | |
4010 | Deep_Choice : Boolean; | |
9eb8d5b4 | 4011 | |
8d9a1ba7 PMR |
4012 | -- Start of processing for Resolve_Delta_Record_Aggregate |
4013 | ||
4014 | begin | |
3ec54569 PMR |
4015 | Variant := Empty; |
4016 | ||
8d9a1ba7 | 4017 | Assoc := First (Deltas); |
8d9a1ba7 PMR |
4018 | while Present (Assoc) loop |
4019 | Choice := First (Choice_List (Assoc)); | |
4020 | while Present (Choice) loop | |
0191a24e SB |
4021 | Deep_Choice := Nkind (Choice) /= N_Identifier; |
4022 | if Deep_Choice then | |
4023 | Error_Msg_GNAT_Extension | |
4024 | ("deep delta aggregate", Sloc (Choice)); | |
4025 | end if; | |
d63199d8 | 4026 | |
0191a24e SB |
4027 | Comp_Type := Get_Component_Type |
4028 | (Selector => Choice, Enclosing_Type => Typ); | |
484d58c5 | 4029 | |
0191a24e | 4030 | -- Set the type of the choice for GNATprove |
484d58c5 | 4031 | |
0191a24e SB |
4032 | if Deep_Choice then |
4033 | Set_Etype (Choice, Comp_Type); | |
4034 | end if; | |
484d58c5 | 4035 | |
0191a24e SB |
4036 | if Present (Comp_Type) then |
4037 | if not Deep_Choice then | |
4038 | -- ??? Not clear yet how RM 4.3.1(17.7) applies to a | |
4039 | -- deep delta aggregate. | |
4040 | Check_Variant (Choice); | |
4041 | end if; | |
484d58c5 PT |
4042 | else |
4043 | Comp_Type := Any_Type; | |
8d9a1ba7 PMR |
4044 | end if; |
4045 | ||
4046 | Next (Choice); | |
9eb8d5b4 | 4047 | end loop; |
9eb8d5b4 | 4048 | |
5612989e | 4049 | pragma Assert (Present (Comp_Type)); |
6cf01c92 PT |
4050 | |
4051 | -- A record_component_association in record_delta_aggregate shall not | |
4052 | -- use the box compound delimiter <> rather than an expression; see | |
4053 | -- RM 4.3.1(17.3/5). | |
4054 | ||
4055 | pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc)); | |
4056 | ||
4057 | if Box_Present (Assoc) then | |
4058 | Error_Msg_N | |
4059 | ("'<'> in record delta aggregate is not allowed", Assoc); | |
4060 | else | |
4061 | Analyze_And_Resolve (Expression (Assoc), Comp_Type); | |
4a22fdac PT |
4062 | |
4063 | -- The expression must not be of a limited type; RM 4.3.1(17.4/5) | |
4064 | ||
4065 | if Is_Limited_Type (Etype (Expression (Assoc))) then | |
4066 | Error_Msg_N | |
4067 | ("expression of a limited type in record delta aggregate " & | |
4068 | "is not allowed", | |
4069 | Expression (Assoc)); | |
4070 | end if; | |
6cf01c92 | 4071 | end if; |
4a22fdac | 4072 | |
8d9a1ba7 PMR |
4073 | Next (Assoc); |
4074 | end loop; | |
4075 | end Resolve_Delta_Record_Aggregate; | |
9eb8d5b4 | 4076 | |
0191a24e SB |
4077 | ------------------------------ |
4078 | -- Resolve_Deep_Delta_Assoc -- | |
4079 | ------------------------------ | |
4080 | ||
4081 | procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is | |
4082 | Choice : constant Node_Id := First (Choice_List (N)); | |
4083 | Enclosing_Type : Entity_Id := Typ; | |
4084 | ||
4085 | procedure Resolve_Choice_Prefix | |
4086 | (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id); | |
4087 | -- Recursively analyze selectors. Enclosing_Type is set to | |
4088 | -- type of the last component. | |
4089 | ||
4090 | --------------------------- | |
4091 | -- Resolve_Choice_Prefix -- | |
4092 | --------------------------- | |
4093 | ||
4094 | procedure Resolve_Choice_Prefix | |
4095 | (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id) | |
4096 | is | |
4097 | Selector : Node_Id := Choice_Prefix; | |
4098 | begin | |
4099 | if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then | |
4100 | Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type); | |
4101 | ||
4102 | if Nkind (Choice_Prefix) = N_Selected_Component then | |
4103 | Selector := Selector_Name (Choice_Prefix); | |
4104 | else | |
4105 | pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component); | |
4106 | Selector := First (Expressions (Choice_Prefix)); | |
4107 | end if; | |
4108 | end if; | |
4109 | ||
4110 | if Is_Array_Type (Enclosing_Type) then | |
4111 | Analyze_And_Resolve (Selector, | |
4112 | Etype (First_Index (Enclosing_Type))); | |
4113 | Enclosing_Type := Component_Type (Enclosing_Type); | |
4114 | else | |
4115 | declare | |
4116 | Comp : Entity_Id := First_Entity (Enclosing_Type); | |
4117 | Found : Boolean := False; | |
4118 | begin | |
4119 | while Present (Comp) and not Found loop | |
4120 | if Chars (Comp) = Chars (Selector) then | |
4121 | if Ekind (Comp) = E_Discriminant then | |
4122 | Error_Msg_N ("delta cannot apply to discriminant", | |
4123 | Selector); | |
4124 | end if; | |
4125 | Found := True; | |
4126 | Set_Entity (Selector, Comp); | |
4127 | Set_Etype (Selector, Etype (Comp)); | |
4128 | Set_Analyzed (Selector); | |
4129 | Enclosing_Type := Etype (Comp); | |
4130 | else | |
4131 | Next_Entity (Comp); | |
4132 | end if; | |
4133 | end loop; | |
4134 | if not Found then | |
4135 | Error_Msg_NE | |
4136 | ("type& has no component with this name", | |
4137 | Selector, Enclosing_Type); | |
4138 | end if; | |
4139 | end; | |
4140 | end if; | |
4141 | ||
4142 | -- Set the type of the prefix for GNATprove, except for the root | |
4143 | -- prefix, whose type is already the expected one for a record | |
4144 | -- delta aggregate, or the type of the array index for an | |
4145 | -- array delta aggregate (the only case here really since | |
4146 | -- Resolve_Deep_Delta_Assoc is only called for array delta | |
4147 | -- aggregates). | |
4148 | ||
4149 | if Selector /= Choice_Prefix then | |
4150 | Set_Etype (Choice_Prefix, Enclosing_Type); | |
4151 | end if; | |
4152 | end Resolve_Choice_Prefix; | |
4153 | begin | |
4154 | declare | |
4155 | Unimplemented : exception; -- TEMPORARY | |
4156 | begin | |
4157 | if Present (Next (Choice)) then | |
4158 | raise Unimplemented; | |
4159 | end if; | |
4160 | end; | |
4161 | ||
4162 | Resolve_Choice_Prefix (Choice, Enclosing_Type); | |
4163 | Analyze_And_Resolve (Expression (N), Enclosing_Type); | |
4164 | end Resolve_Deep_Delta_Assoc; | |
4165 | ||
996ae0b0 RK |
4166 | --------------------------------- |
4167 | -- Resolve_Extension_Aggregate -- | |
4168 | --------------------------------- | |
4169 | ||
4170 | -- There are two cases to consider: | |
4171 | ||
ebd34478 AC |
4172 | -- a) If the ancestor part is a type mark, the components needed are the |
4173 | -- difference between the components of the expected type and the | |
996ae0b0 RK |
4174 | -- components of the given type mark. |
4175 | ||
ebd34478 | 4176 | -- b) If the ancestor part is an expression, it must be unambiguous, and |
12f0c50c | 4177 | -- once we have its type we can also compute the needed components as in |
ebd34478 AC |
4178 | -- the previous case. In both cases, if the ancestor type is not the |
4179 | -- immediate ancestor, we have to build this ancestor recursively. | |
996ae0b0 | 4180 | |
12f0c50c | 4181 | -- In both cases, discriminants of the ancestor type do not play a role in |
ebd34478 AC |
4182 | -- the resolution of the needed components, because inherited discriminants |
4183 | -- cannot be used in a type extension. As a result we can compute | |
4184 | -- independently the list of components of the ancestor type and of the | |
4185 | -- expected type. | |
996ae0b0 RK |
4186 | |
4187 | procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4188 | A : constant Node_Id := Ancestor_Part (N); |
4189 | A_Type : Entity_Id; | |
4190 | I : Interp_Index; | |
4191 | It : Interp; | |
996ae0b0 | 4192 | |
ca44152f ES |
4193 | function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean; |
4194 | -- If the type is limited, verify that the ancestor part is a legal | |
ebd34478 AC |
4195 | -- expression (aggregate or function call, including 'Input)) that does |
4196 | -- not require a copy, as specified in 7.5(2). | |
ca44152f | 4197 | |
996ae0b0 RK |
4198 | function Valid_Ancestor_Type return Boolean; |
4199 | -- Verify that the type of the ancestor part is a non-private ancestor | |
1543e3ab | 4200 | -- of the expected type, which must be a type extension. |
996ae0b0 | 4201 | |
5168a9b3 PMR |
4202 | procedure Transform_BIP_Assignment (Typ : Entity_Id); |
4203 | -- For an extension aggregate whose ancestor part is a build-in-place | |
4204 | -- call returning a nonlimited type, this is used to transform the | |
4205 | -- assignment to the ancestor part to use a temp. | |
4206 | ||
ca44152f ES |
4207 | ---------------------------- |
4208 | -- Valid_Limited_Ancestor -- | |
4209 | ---------------------------- | |
4210 | ||
4211 | function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is | |
4212 | begin | |
d8d7e809 AC |
4213 | if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then |
4214 | return True; | |
4215 | ||
4216 | -- The ancestor must be a call or an aggregate, but a call may | |
4217 | -- have been expanded into a temporary, so check original node. | |
4218 | ||
4a08c95c AC |
4219 | elsif Nkind (Anc) in N_Aggregate |
4220 | | N_Extension_Aggregate | |
4221 | | N_Function_Call | |
ca44152f ES |
4222 | then |
4223 | return True; | |
4224 | ||
d8d7e809 | 4225 | elsif Nkind (Original_Node (Anc)) = N_Function_Call then |
ca44152f ES |
4226 | return True; |
4227 | ||
4228 | elsif Nkind (Anc) = N_Attribute_Reference | |
4229 | and then Attribute_Name (Anc) = Name_Input | |
4230 | then | |
4231 | return True; | |
4232 | ||
ebd34478 | 4233 | elsif Nkind (Anc) = N_Qualified_Expression then |
ca44152f ES |
4234 | return Valid_Limited_Ancestor (Expression (Anc)); |
4235 | ||
32794080 JM |
4236 | elsif Nkind (Anc) = N_Raise_Expression then |
4237 | return True; | |
4238 | ||
ca44152f ES |
4239 | else |
4240 | return False; | |
4241 | end if; | |
4242 | end Valid_Limited_Ancestor; | |
4243 | ||
fbf5a39b AC |
4244 | ------------------------- |
4245 | -- Valid_Ancestor_Type -- | |
4246 | ------------------------- | |
4247 | ||
996ae0b0 RK |
4248 | function Valid_Ancestor_Type return Boolean is |
4249 | Imm_Type : Entity_Id; | |
4250 | ||
4251 | begin | |
4252 | Imm_Type := Base_Type (Typ); | |
2af92e28 ES |
4253 | while Is_Derived_Type (Imm_Type) loop |
4254 | if Etype (Imm_Type) = Base_Type (A_Type) then | |
4255 | return True; | |
4256 | ||
21d7ef70 | 4257 | -- The base type of the parent type may appear as a private |
ebd34478 AC |
4258 | -- extension if it is declared as such in a parent unit of the |
4259 | -- current one. For consistency of the subsequent analysis use | |
4260 | -- the partial view for the ancestor part. | |
2af92e28 ES |
4261 | |
4262 | elsif Is_Private_Type (Etype (Imm_Type)) | |
4263 | and then Present (Full_View (Etype (Imm_Type))) | |
4264 | and then Base_Type (A_Type) = Full_View (Etype (Imm_Type)) | |
4265 | then | |
4266 | A_Type := Etype (Imm_Type); | |
4267 | return True; | |
4519314c AC |
4268 | |
4269 | -- The parent type may be a private extension. The aggregate is | |
4270 | -- legal if the type of the aggregate is an extension of it that | |
4271 | -- is not a private extension. | |
4272 | ||
4273 | elsif Is_Private_Type (A_Type) | |
4274 | and then not Is_Private_Type (Imm_Type) | |
4275 | and then Present (Full_View (A_Type)) | |
4276 | and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type) | |
4277 | then | |
4278 | return True; | |
4279 | ||
32794080 JM |
4280 | -- The parent type may be a raise expression (which is legal in |
4281 | -- any expression context). | |
4282 | ||
4283 | elsif A_Type = Raise_Type then | |
4284 | A_Type := Etype (Imm_Type); | |
4285 | return True; | |
4286 | ||
2af92e28 ES |
4287 | else |
4288 | Imm_Type := Etype (Base_Type (Imm_Type)); | |
4289 | end if; | |
996ae0b0 RK |
4290 | end loop; |
4291 | ||
6d2a1120 | 4292 | -- If previous loop did not find a proper ancestor, report error |
2af92e28 ES |
4293 | |
4294 | Error_Msg_NE ("expect ancestor type of &", A, Typ); | |
4295 | return False; | |
996ae0b0 RK |
4296 | end Valid_Ancestor_Type; |
4297 | ||
3fc40cd7 PMR |
4298 | ------------------------------ |
4299 | -- Transform_BIP_Assignment -- | |
4300 | ------------------------------ | |
4301 | ||
5168a9b3 | 4302 | procedure Transform_BIP_Assignment (Typ : Entity_Id) is |
3fc40cd7 PMR |
4303 | Loc : constant Source_Ptr := Sloc (N); |
4304 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A); | |
4305 | Obj_Decl : constant Node_Id := | |
4306 | Make_Object_Declaration (Loc, | |
4307 | Defining_Identifier => Def_Id, | |
4308 | Constant_Present => True, | |
4309 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
4310 | Expression => A, | |
4311 | Has_Init_Expression => True); | |
5168a9b3 PMR |
4312 | begin |
4313 | Set_Etype (Def_Id, Typ); | |
4314 | Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc)); | |
4315 | Insert_Action (N, Obj_Decl); | |
4316 | end Transform_BIP_Assignment; | |
4317 | ||
996ae0b0 RK |
4318 | -- Start of processing for Resolve_Extension_Aggregate |
4319 | ||
4320 | begin | |
ebd34478 AC |
4321 | -- Analyze the ancestor part and account for the case where it is a |
4322 | -- parameterless function call. | |
70b70ce8 | 4323 | |
996ae0b0 | 4324 | Analyze (A); |
70b70ce8 | 4325 | Check_Parameterless_Call (A); |
996ae0b0 | 4326 | |
58009744 | 4327 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then |
87729e5a | 4328 | |
5c6430d6 GD |
4329 | -- AI05-0115: If the ancestor part is a subtype mark, the ancestor |
4330 | -- must not have unknown discriminants. To catch cases where the | |
4331 | -- aggregate occurs at a place where the full view of the ancestor | |
4332 | -- type is visible and doesn't have unknown discriminants, but the | |
4333 | -- aggregate type was derived from a partial view that has unknown | |
4334 | -- discriminants, we check whether the aggregate type has unknown | |
4335 | -- discriminants (unknown discriminants were inherited), along | |
4336 | -- with checking that the partial view of the ancestor has unknown | |
4337 | -- discriminants. (It might be sufficient to replace the entire | |
4338 | -- condition with Has_Unknown_Discriminants (Typ), but that might | |
4339 | -- miss some cases, not clear, and causes error changes in some tests | |
4340 | -- such as class-wide cases, that aren't clearly improvements. ???) | |
4341 | ||
4342 | if Has_Unknown_Discriminants (Entity (A)) | |
4343 | or else (Has_Unknown_Discriminants (Typ) | |
4344 | and then Partial_View_Has_Unknown_Discr (Entity (A))) | |
4345 | then | |
87729e5a AC |
4346 | Error_Msg_NE |
4347 | ("aggregate not available for type& whose ancestor " | |
4348 | & "has unknown discriminants", N, Typ); | |
4349 | end if; | |
9f90d123 AC |
4350 | end if; |
4351 | ||
996ae0b0 RK |
4352 | if not Is_Tagged_Type (Typ) then |
4353 | Error_Msg_N ("type of extension aggregate must be tagged", N); | |
4354 | return; | |
4355 | ||
19f0526a AC |
4356 | elsif Is_Limited_Type (Typ) then |
4357 | ||
0ab80019 | 4358 | -- Ada 2005 (AI-287): Limited aggregates are allowed |
19f0526a | 4359 | |
0791fbe9 | 4360 | if Ada_Version < Ada_2005 then |
19f0526a AC |
4361 | Error_Msg_N ("aggregate type cannot be limited", N); |
4362 | Explain_Limited_Type (Typ, N); | |
4363 | return; | |
ca44152f ES |
4364 | |
4365 | elsif Valid_Limited_Ancestor (A) then | |
4366 | null; | |
4367 | ||
4368 | else | |
4369 | Error_Msg_N | |
4370 | ("limited ancestor part must be aggregate or function call", A); | |
19f0526a | 4371 | end if; |
996ae0b0 RK |
4372 | |
4373 | elsif Is_Class_Wide_Type (Typ) then | |
4374 | Error_Msg_N ("aggregate cannot be of a class-wide type", N); | |
4375 | return; | |
4376 | end if; | |
4377 | ||
58009744 | 4378 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then |
fbf5a39b | 4379 | A_Type := Get_Full_View (Entity (A)); |
996ae0b0 RK |
4380 | |
4381 | if Valid_Ancestor_Type then | |
4382 | Set_Entity (A, A_Type); | |
4383 | Set_Etype (A, A_Type); | |
4384 | ||
4385 | Validate_Ancestor_Part (N); | |
4386 | Resolve_Record_Aggregate (N, Typ); | |
4387 | end if; | |
4388 | ||
4389 | elsif Nkind (A) /= N_Aggregate then | |
4390 | if Is_Overloaded (A) then | |
4391 | A_Type := Any_Type; | |
996ae0b0 | 4392 | |
7f9747c6 | 4393 | Get_First_Interp (A, I, It); |
996ae0b0 | 4394 | while Present (It.Typ) loop |
58009744 | 4395 | |
5168a9b3 | 4396 | -- Consider limited interpretations if Ada 2005 or higher |
70b70ce8 | 4397 | |
996ae0b0 | 4398 | if Is_Tagged_Type (It.Typ) |
0791fbe9 | 4399 | and then (Ada_Version >= Ada_2005 |
70b70ce8 | 4400 | or else not Is_Limited_Type (It.Typ)) |
996ae0b0 RK |
4401 | then |
4402 | if A_Type /= Any_Type then | |
4403 | Error_Msg_N ("cannot resolve expression", A); | |
4404 | return; | |
4405 | else | |
4406 | A_Type := It.Typ; | |
4407 | end if; | |
4408 | end if; | |
4409 | ||
4410 | Get_Next_Interp (I, It); | |
4411 | end loop; | |
4412 | ||
4413 | if A_Type = Any_Type then | |
0791fbe9 | 4414 | if Ada_Version >= Ada_2005 then |
58009744 AC |
4415 | Error_Msg_N |
4416 | ("ancestor part must be of a tagged type", A); | |
70b70ce8 AC |
4417 | else |
4418 | Error_Msg_N | |
4419 | ("ancestor part must be of a nonlimited tagged type", A); | |
4420 | end if; | |
4421 | ||
996ae0b0 RK |
4422 | return; |
4423 | end if; | |
4424 | ||
4425 | else | |
4426 | A_Type := Etype (A); | |
4427 | end if; | |
4428 | ||
4429 | if Valid_Ancestor_Type then | |
4430 | Resolve (A, A_Type); | |
fbf5a39b | 4431 | Check_Unset_Reference (A); |
996ae0b0 | 4432 | Check_Non_Static_Context (A); |
fbf5a39b | 4433 | |
1646c947 GD |
4434 | -- The aggregate is illegal if the ancestor expression is a call |
4435 | -- to a function with a limited unconstrained result, unless the | |
4436 | -- type of the aggregate is a null extension. This restriction | |
4437 | -- was added in AI05-67 to simplify implementation. | |
4438 | ||
4439 | if Nkind (A) = N_Function_Call | |
4440 | and then Is_Limited_Type (A_Type) | |
4441 | and then not Is_Null_Extension (Typ) | |
4442 | and then not Is_Constrained (A_Type) | |
4443 | then | |
4444 | Error_Msg_N | |
4445 | ("type of limited ancestor part must be constrained", A); | |
4446 | ||
cefce34c JM |
4447 | -- Reject the use of CPP constructors that leave objects partially |
4448 | -- initialized. For example: | |
4449 | ||
4450 | -- type CPP_Root is tagged limited record ... | |
4451 | -- pragma Import (CPP, CPP_Root); | |
4452 | ||
4453 | -- type CPP_DT is new CPP_Root and Iface ... | |
4454 | -- pragma Import (CPP, CPP_DT); | |
4455 | ||
4456 | -- type Ada_DT is new CPP_DT with ... | |
4457 | ||
4458 | -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>); | |
4459 | ||
4460 | -- Using the constructor of CPP_Root the slots of the dispatch | |
4461 | -- table of CPP_DT cannot be set, and the secondary tag of | |
4462 | -- CPP_DT is unknown. | |
4463 | ||
4464 | elsif Nkind (A) = N_Function_Call | |
4465 | and then Is_CPP_Constructor_Call (A) | |
4466 | and then Enclosing_CPP_Parent (Typ) /= A_Type | |
4467 | then | |
4468 | Error_Msg_NE | |
324ac540 | 4469 | ("??must use 'C'P'P constructor for type &", A, |
cefce34c JM |
4470 | Enclosing_CPP_Parent (Typ)); |
4471 | ||
4472 | -- The following call is not needed if the previous warning | |
4473 | -- is promoted to an error. | |
4474 | ||
4475 | Resolve_Record_Aggregate (N, Typ); | |
4476 | ||
1646c947 | 4477 | elsif Is_Class_Wide_Type (Etype (A)) |
fbf5a39b AC |
4478 | and then Nkind (Original_Node (A)) = N_Function_Call |
4479 | then | |
4480 | -- If the ancestor part is a dispatching call, it appears | |
ebd34478 AC |
4481 | -- statically to be a legal ancestor, but it yields any member |
4482 | -- of the class, and it is not possible to determine whether | |
4483 | -- it is an ancestor of the extension aggregate (much less | |
4484 | -- which ancestor). It is not possible to determine the | |
4485 | -- components of the extension part. | |
fbf5a39b | 4486 | |
ebd34478 AC |
4487 | -- This check implements AI-306, which in fact was motivated by |
4488 | -- an AdaCore query to the ARG after this test was added. | |
82c80734 | 4489 | |
fbf5a39b AC |
4490 | Error_Msg_N ("ancestor part must be statically tagged", A); |
4491 | else | |
5168a9b3 PMR |
4492 | -- We are using the build-in-place protocol, but we can't build |
4493 | -- in place, because we need to call the function before | |
4494 | -- allocating the aggregate. Could do better for null | |
4495 | -- extensions, and maybe for nondiscriminated types. | |
4496 | -- This is wrong for limited, but those were wrong already. | |
4497 | ||
36fcb4b9 | 4498 | if not Is_Inherently_Limited_Type (A_Type) |
5168a9b3 PMR |
4499 | and then Is_Build_In_Place_Function_Call (A) |
4500 | then | |
4501 | Transform_BIP_Assignment (A_Type); | |
4502 | end if; | |
4503 | ||
fbf5a39b AC |
4504 | Resolve_Record_Aggregate (N, Typ); |
4505 | end if; | |
996ae0b0 RK |
4506 | end if; |
4507 | ||
4508 | else | |
937e9676 | 4509 | Error_Msg_N ("no unique type for this aggregate", A); |
996ae0b0 | 4510 | end if; |
d3820795 | 4511 | |
22e89283 | 4512 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
4513 | end Resolve_Extension_Aggregate; |
4514 | ||
10c257af ES |
4515 | ---------------------------------- |
4516 | -- Resolve_Null_Array_Aggregate -- | |
4517 | ---------------------------------- | |
4518 | ||
4519 | function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is | |
4520 | -- Never returns False, but declared as a function to match | |
4521 | -- other Resolve_Mumble functions. | |
4522 | ||
4523 | Loc : constant Source_Ptr := Sloc (N); | |
4524 | Typ : constant Entity_Id := Etype (N); | |
4525 | ||
10c257af ES |
4526 | Index : Node_Id; |
4527 | Lo, Hi : Node_Id; | |
4528 | Constr : constant List_Id := New_List; | |
10c257af ES |
4529 | |
4530 | begin | |
59a23beb PT |
4531 | -- Attach the list of constraints at the location of the aggregate, so |
4532 | -- the individual constraints can be analyzed. | |
4533 | ||
4534 | Set_Parent (Constr, N); | |
4535 | ||
10c257af ES |
4536 | -- Create a constrained subtype with null dimensions |
4537 | ||
4538 | Index := First_Index (Typ); | |
4539 | while Present (Index) loop | |
4540 | Get_Index_Bounds (Index, L => Lo, H => Hi); | |
4541 | ||
4542 | -- The upper bound is the predecessor of the lower bound | |
4543 | ||
4544 | Hi := Make_Attribute_Reference | |
4545 | (Loc, | |
4546 | Prefix => New_Occurrence_Of (Etype (Index), Loc), | |
4547 | Attribute_Name => Name_Pred, | |
4548 | Expressions => New_List (New_Copy_Tree (Lo))); | |
4549 | ||
59a23beb PT |
4550 | Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr); |
4551 | Analyze_And_Resolve (Last (Constr), Etype (Index)); | |
10c257af | 4552 | |
93d0457e | 4553 | Next_Index (Index); |
10c257af ES |
4554 | end loop; |
4555 | ||
10c257af | 4556 | Set_Compile_Time_Known_Aggregate (N); |
59a23beb | 4557 | Set_Aggregate_Bounds (N, First (Constr)); |
10c257af ES |
4558 | |
4559 | return True; | |
4560 | end Resolve_Null_Array_Aggregate; | |
4561 | ||
996ae0b0 RK |
4562 | ------------------------------ |
4563 | -- Resolve_Record_Aggregate -- | |
4564 | ------------------------------ | |
4565 | ||
4566 | procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b | 4567 | New_Assoc_List : constant List_Id := New_List; |
996ae0b0 | 4568 | -- New_Assoc_List is the newly built list of N_Component_Association |
937e9676 | 4569 | -- nodes. |
996ae0b0 RK |
4570 | |
4571 | Others_Etype : Entity_Id := Empty; | |
4572 | -- This variable is used to save the Etype of the last record component | |
4573 | -- that takes its value from the others choice. Its purpose is: | |
4574 | -- | |
4575 | -- (a) make sure the others choice is useful | |
4576 | -- | |
4577 | -- (b) make sure the type of all the components whose value is | |
4578 | -- subsumed by the others choice are the same. | |
4579 | -- | |
ebd34478 | 4580 | -- This variable is updated as a side effect of function Get_Value. |
996ae0b0 | 4581 | |
f7937111 GD |
4582 | Box_Node : Node_Id := Empty; |
4583 | Is_Box_Present : Boolean := False; | |
4584 | Is_Box_Init_By_Default : Boolean := False; | |
4585 | Others_Box : Natural := 0; | |
0ab80019 | 4586 | -- Ada 2005 (AI-287): Variables used in case of default initialization |
9b96e234 | 4587 | -- to provide a functionality similar to Others_Etype. Box_Present |
19f0526a | 4588 | -- indicates that the component takes its default initialization; |
ec3c7387 AC |
4589 | -- Others_Box counts the number of components of the current aggregate |
4590 | -- (which may be a sub-aggregate of a larger one) that are default- | |
4591 | -- initialized. A value of One indicates that an others_box is present. | |
4592 | -- Any larger value indicates that the others_box is not redundant. | |
937e9676 AC |
4593 | -- These variables, similar to Others_Etype, are also updated as a side |
4594 | -- effect of function Get_Value. Box_Node is used to place a warning on | |
4595 | -- a redundant others_box. | |
65356e64 AC |
4596 | |
4597 | procedure Add_Association | |
9b96e234 JM |
4598 | (Component : Entity_Id; |
4599 | Expr : Node_Id; | |
107b023c | 4600 | Assoc_List : List_Id; |
9b96e234 | 4601 | Is_Box_Present : Boolean := False); |
ebd34478 AC |
4602 | -- Builds a new N_Component_Association node which associates Component |
4603 | -- to expression Expr and adds it to the association list being built, | |
4604 | -- either New_Assoc_List, or the association being built for an inner | |
4605 | -- aggregate. | |
996ae0b0 | 4606 | |
937e9676 AC |
4607 | procedure Add_Discriminant_Values |
4608 | (New_Aggr : Node_Id; | |
4609 | Assoc_List : List_Id); | |
4610 | -- The constraint to a component may be given by a discriminant of the | |
4611 | -- enclosing type, in which case we have to retrieve its value, which is | |
4612 | -- part of the enclosing aggregate. Assoc_List provides the discriminant | |
4613 | -- associations of the current type or of some enclosing record. | |
4614 | ||
4615 | function Discriminant_Present (Input_Discr : Entity_Id) return Boolean; | |
996ae0b0 | 4616 | -- If aggregate N is a regular aggregate this routine will return True. |
937e9676 AC |
4617 | -- Otherwise, if N is an extension aggregate, then Input_Discr denotes |
4618 | -- a discriminant whose value may already have been specified by N's | |
4619 | -- ancestor part. This routine checks whether this is indeed the case | |
4620 | -- and if so returns False, signaling that no value for Input_Discr | |
4621 | -- should appear in N's aggregate part. Also, in this case, the routine | |
4622 | -- appends to New_Assoc_List the discriminant value specified in the | |
4623 | -- ancestor part. | |
2383acbd | 4624 | -- |
f104fca1 | 4625 | -- If the aggregate is in a context with expansion delayed, it will be |
22cb89b5 AC |
4626 | -- reanalyzed. The inherited discriminant values must not be reinserted |
4627 | -- in the component list to prevent spurious errors, but they must be | |
f104fca1 AC |
4628 | -- present on first analysis to build the proper subtype indications. |
4629 | -- The flag Inherited_Discriminant is used to prevent the re-insertion. | |
996ae0b0 | 4630 | |
937e9676 AC |
4631 | function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id; |
4632 | -- AI05-0115: Find earlier ancestor in the derivation chain that is | |
4633 | -- derived from private view Typ. Whether the aggregate is legal depends | |
4634 | -- on the current visibility of the type as well as that of the parent | |
4635 | -- of the ancestor. | |
4636 | ||
996ae0b0 | 4637 | function Get_Value |
0bfa32f9 | 4638 | (Compon : Entity_Id; |
996ae0b0 | 4639 | From : List_Id; |
937e9676 | 4640 | Consider_Others_Choice : Boolean := False) return Node_Id; |
4519314c AC |
4641 | -- Given a record component stored in parameter Compon, this function |
4642 | -- returns its value as it appears in the list From, which is a list | |
4643 | -- of N_Component_Association nodes. | |
2383acbd | 4644 | -- |
ebd34478 AC |
4645 | -- If no component association has a choice for the searched component, |
4646 | -- the value provided by the others choice is returned, if there is one, | |
4647 | -- and Consider_Others_Choice is set to true. Otherwise Empty is | |
4648 | -- returned. If there is more than one component association giving a | |
4649 | -- value for the searched record component, an error message is emitted | |
4650 | -- and the first found value is returned. | |
996ae0b0 RK |
4651 | -- |
4652 | -- If Consider_Others_Choice is set and the returned expression comes | |
4653 | -- from the others choice, then Others_Etype is set as a side effect. | |
ebd34478 AC |
4654 | -- An error message is emitted if the components taking their value from |
4655 | -- the others choice do not have same type. | |
996ae0b0 | 4656 | |
937e9676 AC |
4657 | procedure Propagate_Discriminants |
4658 | (Aggr : Node_Id; | |
4659 | Assoc_List : List_Id); | |
4660 | -- Nested components may themselves be discriminated types constrained | |
4661 | -- by outer discriminants, whose values must be captured before the | |
4662 | -- aggregate is expanded into assignments. | |
4663 | ||
4664 | procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id); | |
996ae0b0 | 4665 | -- Analyzes and resolves expression Expr against the Etype of the |
638e383e | 4666 | -- Component. This routine also applies all appropriate checks to Expr. |
996ae0b0 RK |
4667 | -- It finally saves a Expr in the newly created association list that |
4668 | -- will be attached to the final record aggregate. Note that if the | |
4669 | -- Parent pointer of Expr is not set then Expr was produced with a | |
fbf5a39b | 4670 | -- New_Copy_Tree or some such. |
996ae0b0 | 4671 | |
333e4f86 AC |
4672 | procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id); |
4673 | -- Rewrite a range node Rge when its bounds refer to non-stored | |
4674 | -- discriminants from Root_Type, to replace them with the stored | |
4675 | -- discriminant values. This is required in GNATprove mode, and is | |
4676 | -- adopted in all modes to avoid special-casing GNATprove mode. | |
4677 | ||
996ae0b0 RK |
4678 | --------------------- |
4679 | -- Add_Association -- | |
4680 | --------------------- | |
4681 | ||
65356e64 | 4682 | procedure Add_Association |
9b96e234 JM |
4683 | (Component : Entity_Id; |
4684 | Expr : Node_Id; | |
107b023c | 4685 | Assoc_List : List_Id; |
9b96e234 | 4686 | Is_Box_Present : Boolean := False) |
65356e64 | 4687 | is |
fbf5a39b | 4688 | Choice_List : constant List_Id := New_List; |
937e9676 | 4689 | Loc : Source_Ptr; |
996ae0b0 RK |
4690 | |
4691 | begin | |
937e9676 AC |
4692 | -- If this is a box association the expression is missing, so use the |
4693 | -- Sloc of the aggregate itself for the new association. | |
f5afb270 | 4694 | |
0c25b783 PT |
4695 | pragma Assert (Present (Expr) xor Is_Box_Present); |
4696 | ||
f5afb270 AC |
4697 | if Present (Expr) then |
4698 | Loc := Sloc (Expr); | |
4699 | else | |
4700 | Loc := Sloc (N); | |
4701 | end if; | |
4702 | ||
937e9676 AC |
4703 | Append_To (Choice_List, New_Occurrence_Of (Component, Loc)); |
4704 | ||
4705 | Append_To (Assoc_List, | |
f5afb270 | 4706 | Make_Component_Association (Loc, |
65356e64 AC |
4707 | Choices => Choice_List, |
4708 | Expression => Expr, | |
937e9676 | 4709 | Box_Present => Is_Box_Present)); |
f7937111 GD |
4710 | |
4711 | -- If this association has a box for a component that is initialized | |
4712 | -- by default, then set flag on the new association to indicate that | |
4713 | -- the original association was for such a box-initialized component. | |
4714 | ||
097826df | 4715 | if Is_Box_Init_By_Default then |
f7937111 GD |
4716 | Set_Was_Default_Init_Box_Association (Last (Assoc_List)); |
4717 | end if; | |
996ae0b0 RK |
4718 | end Add_Association; |
4719 | ||
937e9676 AC |
4720 | ----------------------------- |
4721 | -- Add_Discriminant_Values -- | |
4722 | ----------------------------- | |
4723 | ||
4724 | procedure Add_Discriminant_Values | |
4725 | (New_Aggr : Node_Id; | |
4726 | Assoc_List : List_Id) | |
4727 | is | |
4728 | Assoc : Node_Id; | |
4729 | Discr : Entity_Id; | |
4730 | Discr_Elmt : Elmt_Id; | |
4731 | Discr_Val : Node_Id; | |
4732 | Val : Entity_Id; | |
4733 | ||
4734 | begin | |
4735 | Discr := First_Discriminant (Etype (New_Aggr)); | |
4736 | Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr))); | |
4737 | while Present (Discr_Elmt) loop | |
4738 | Discr_Val := Node (Discr_Elmt); | |
4739 | ||
4740 | -- If the constraint is given by a discriminant then it is a | |
4741 | -- discriminant of an enclosing record, and its value has already | |
4742 | -- been placed in the association list. | |
996ae0b0 | 4743 | |
937e9676 AC |
4744 | if Is_Entity_Name (Discr_Val) |
4745 | and then Ekind (Entity (Discr_Val)) = E_Discriminant | |
4746 | then | |
4747 | Val := Entity (Discr_Val); | |
4748 | ||
4749 | Assoc := First (Assoc_List); | |
4750 | while Present (Assoc) loop | |
4751 | if Present (Entity (First (Choices (Assoc)))) | |
4752 | and then Entity (First (Choices (Assoc))) = Val | |
4753 | then | |
4754 | Discr_Val := Expression (Assoc); | |
4755 | exit; | |
4756 | end if; | |
4757 | ||
4758 | Next (Assoc); | |
4759 | end loop; | |
4760 | end if; | |
4761 | ||
4762 | Add_Association | |
4763 | (Discr, New_Copy_Tree (Discr_Val), | |
4764 | Component_Associations (New_Aggr)); | |
4765 | ||
4766 | -- If the discriminant constraint is a current instance, mark the | |
ae250f9e EB |
4767 | -- current aggregate so that the self-reference can be expanded by |
4768 | -- Build_Record_Aggr_Code.Replace_Type later. | |
937e9676 AC |
4769 | |
4770 | if Nkind (Discr_Val) = N_Attribute_Reference | |
4771 | and then Is_Entity_Name (Prefix (Discr_Val)) | |
4772 | and then Is_Type (Entity (Prefix (Discr_Val))) | |
ae250f9e EB |
4773 | and then |
4774 | Is_Ancestor | |
4775 | (Entity (Prefix (Discr_Val)), | |
4776 | Etype (N), | |
4777 | Use_Full_View => True) | |
937e9676 AC |
4778 | then |
4779 | Set_Has_Self_Reference (N); | |
4780 | end if; | |
4781 | ||
4782 | Next_Elmt (Discr_Elmt); | |
4783 | Next_Discriminant (Discr); | |
4784 | end loop; | |
4785 | end Add_Discriminant_Values; | |
4786 | ||
4787 | -------------------------- | |
4788 | -- Discriminant_Present -- | |
4789 | -------------------------- | |
4790 | ||
4791 | function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is | |
fbf5a39b AC |
4792 | Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate; |
4793 | ||
937e9676 AC |
4794 | Ancestor_Is_Subtyp : Boolean; |
4795 | ||
996ae0b0 RK |
4796 | Loc : Source_Ptr; |
4797 | ||
4798 | Ancestor : Node_Id; | |
937e9676 | 4799 | Ancestor_Typ : Entity_Id; |
f104fca1 | 4800 | Comp_Assoc : Node_Id; |
937e9676 | 4801 | Discr : Entity_Id; |
996ae0b0 | 4802 | Discr_Expr : Node_Id; |
937e9676 | 4803 | Discr_Val : Elmt_Id := No_Elmt; |
996ae0b0 | 4804 | Orig_Discr : Entity_Id; |
996ae0b0 RK |
4805 | |
4806 | begin | |
4807 | if Regular_Aggr then | |
4808 | return True; | |
4809 | end if; | |
4810 | ||
f104fca1 AC |
4811 | -- Check whether inherited discriminant values have already been |
4812 | -- inserted in the aggregate. This will be the case if we are | |
4813 | -- re-analyzing an aggregate whose expansion was delayed. | |
4814 | ||
4815 | if Present (Component_Associations (N)) then | |
4816 | Comp_Assoc := First (Component_Associations (N)); | |
4817 | while Present (Comp_Assoc) loop | |
4818 | if Inherited_Discriminant (Comp_Assoc) then | |
4819 | return True; | |
4820 | end if; | |
2383acbd | 4821 | |
f104fca1 AC |
4822 | Next (Comp_Assoc); |
4823 | end loop; | |
4824 | end if; | |
4825 | ||
996ae0b0 RK |
4826 | Ancestor := Ancestor_Part (N); |
4827 | Ancestor_Typ := Etype (Ancestor); | |
4828 | Loc := Sloc (Ancestor); | |
4829 | ||
5987e59c AC |
4830 | -- For a private type with unknown discriminants, use the underlying |
4831 | -- record view if it is available. | |
9013065b AC |
4832 | |
4833 | if Has_Unknown_Discriminants (Ancestor_Typ) | |
4834 | and then Present (Full_View (Ancestor_Typ)) | |
4835 | and then Present (Underlying_Record_View (Full_View (Ancestor_Typ))) | |
4836 | then | |
4837 | Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ)); | |
4838 | end if; | |
4839 | ||
996ae0b0 RK |
4840 | Ancestor_Is_Subtyp := |
4841 | Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor)); | |
4842 | ||
4843 | -- If the ancestor part has no discriminants clearly N's aggregate | |
4844 | -- part must provide a value for Discr. | |
4845 | ||
4846 | if not Has_Discriminants (Ancestor_Typ) then | |
4847 | return True; | |
4848 | ||
4849 | -- If the ancestor part is an unconstrained subtype mark then the | |
4850 | -- Discr must be present in N's aggregate part. | |
4851 | ||
4852 | elsif Ancestor_Is_Subtyp | |
4853 | and then not Is_Constrained (Entity (Ancestor)) | |
4854 | then | |
4855 | return True; | |
4856 | end if; | |
4857 | ||
ec53a6da | 4858 | -- Now look to see if Discr was specified in the ancestor part |
996ae0b0 RK |
4859 | |
4860 | if Ancestor_Is_Subtyp then | |
937e9676 AC |
4861 | Discr_Val := |
4862 | First_Elmt (Discriminant_Constraint (Entity (Ancestor))); | |
996ae0b0 RK |
4863 | end if; |
4864 | ||
937e9676 | 4865 | Orig_Discr := Original_Record_Component (Input_Discr); |
ec53a6da | 4866 | |
937e9676 AC |
4867 | Discr := First_Discriminant (Ancestor_Typ); |
4868 | while Present (Discr) loop | |
ec53a6da | 4869 | |
ebd34478 | 4870 | -- If Ancestor has already specified Disc value then insert its |
ec53a6da | 4871 | -- value in the final aggregate. |
996ae0b0 | 4872 | |
937e9676 | 4873 | if Original_Record_Component (Discr) = Orig_Discr then |
996ae0b0 | 4874 | if Ancestor_Is_Subtyp then |
937e9676 | 4875 | Discr_Expr := New_Copy_Tree (Node (Discr_Val)); |
996ae0b0 RK |
4876 | else |
4877 | Discr_Expr := | |
4878 | Make_Selected_Component (Loc, | |
4879 | Prefix => Duplicate_Subexpr (Ancestor), | |
937e9676 | 4880 | Selector_Name => New_Occurrence_Of (Input_Discr, Loc)); |
996ae0b0 RK |
4881 | end if; |
4882 | ||
937e9676 | 4883 | Resolve_Aggr_Expr (Discr_Expr, Input_Discr); |
f104fca1 | 4884 | Set_Inherited_Discriminant (Last (New_Assoc_List)); |
996ae0b0 RK |
4885 | return False; |
4886 | end if; | |
4887 | ||
937e9676 | 4888 | Next_Discriminant (Discr); |
996ae0b0 RK |
4889 | |
4890 | if Ancestor_Is_Subtyp then | |
937e9676 | 4891 | Next_Elmt (Discr_Val); |
996ae0b0 RK |
4892 | end if; |
4893 | end loop; | |
4894 | ||
4895 | return True; | |
937e9676 AC |
4896 | end Discriminant_Present; |
4897 | ||
4898 | --------------------------- | |
4899 | -- Find_Private_Ancestor -- | |
4900 | --------------------------- | |
4901 | ||
4902 | function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is | |
4903 | Par : Entity_Id; | |
4904 | ||
4905 | begin | |
4906 | Par := Typ; | |
4907 | loop | |
4908 | if Has_Private_Ancestor (Par) | |
4909 | and then not Has_Private_Ancestor (Etype (Base_Type (Par))) | |
4910 | then | |
4911 | return Par; | |
4912 | ||
4913 | elsif not Is_Derived_Type (Par) then | |
4914 | return Empty; | |
4915 | ||
4916 | else | |
4917 | Par := Etype (Base_Type (Par)); | |
4918 | end if; | |
4919 | end loop; | |
4920 | end Find_Private_Ancestor; | |
996ae0b0 RK |
4921 | |
4922 | --------------- | |
4923 | -- Get_Value -- | |
4924 | --------------- | |
4925 | ||
4926 | function Get_Value | |
0bfa32f9 | 4927 | (Compon : Entity_Id; |
996ae0b0 | 4928 | From : List_Id; |
937e9676 | 4929 | Consider_Others_Choice : Boolean := False) return Node_Id |
996ae0b0 | 4930 | is |
d10bb9d5 | 4931 | Typ : constant Entity_Id := Etype (Compon); |
996ae0b0 RK |
4932 | Assoc : Node_Id; |
4933 | Expr : Node_Id := Empty; | |
4934 | Selector_Name : Node_Id; | |
4935 | ||
4936 | begin | |
9b96e234 | 4937 | Is_Box_Present := False; |
f7937111 | 4938 | Is_Box_Init_By_Default := False; |
65356e64 | 4939 | |
58009744 | 4940 | if No (From) then |
996ae0b0 RK |
4941 | return Empty; |
4942 | end if; | |
4943 | ||
58009744 | 4944 | Assoc := First (From); |
996ae0b0 RK |
4945 | while Present (Assoc) loop |
4946 | Selector_Name := First (Choices (Assoc)); | |
4947 | while Present (Selector_Name) loop | |
4948 | if Nkind (Selector_Name) = N_Others_Choice then | |
4949 | if Consider_Others_Choice and then No (Expr) then | |
996ae0b0 RK |
4950 | |
4951 | -- We need to duplicate the expression for each | |
4952 | -- successive component covered by the others choice. | |
fbf5a39b AC |
4953 | -- This is redundant if the others_choice covers only |
4954 | -- one component (small optimization possible???), but | |
4955 | -- indispensable otherwise, because each one must be | |
92b751fd | 4956 | -- expanded individually to preserve side effects. |
996ae0b0 | 4957 | |
0ab80019 AC |
4958 | -- Ada 2005 (AI-287): In case of default initialization |
4959 | -- of components, we duplicate the corresponding default | |
88b32fc3 BD |
4960 | -- expression (from the record type declaration). The |
4961 | -- copy must carry the sloc of the association (not the | |
4962 | -- original expression) to prevent spurious elaboration | |
4963 | -- checks when the default includes function calls. | |
19f0526a | 4964 | |
65356e64 | 4965 | if Box_Present (Assoc) then |
ec3c7387 | 4966 | Others_Box := Others_Box + 1; |
9b96e234 | 4967 | Is_Box_Present := True; |
65356e64 AC |
4968 | |
4969 | if Expander_Active then | |
88b32fc3 | 4970 | return |
ba914484 | 4971 | New_Copy_Tree_And_Copy_Dimensions |
88b32fc3 BD |
4972 | (Expression (Parent (Compon)), |
4973 | New_Sloc => Sloc (Assoc)); | |
65356e64 AC |
4974 | else |
4975 | return Expression (Parent (Compon)); | |
4976 | end if; | |
65356e64 | 4977 | |
d05ef0ab | 4978 | else |
d10bb9d5 AC |
4979 | if Present (Others_Etype) |
4980 | and then Base_Type (Others_Etype) /= Base_Type (Typ) | |
65356e64 | 4981 | then |
a921e83c AC |
4982 | -- If the components are of an anonymous access |
4983 | -- type they are distinct, but this is legal in | |
4984 | -- Ada 2012 as long as designated types match. | |
4985 | ||
4986 | if (Ekind (Typ) = E_Anonymous_Access_Type | |
4987 | or else Ekind (Typ) = | |
4988 | E_Anonymous_Access_Subprogram_Type) | |
4989 | and then Designated_Type (Typ) = | |
4990 | Designated_Type (Others_Etype) | |
4991 | then | |
4992 | null; | |
4993 | else | |
4994 | Error_Msg_N | |
937e9676 AC |
4995 | ("components in OTHERS choice must have same " |
4996 | & "type", Selector_Name); | |
a921e83c | 4997 | end if; |
65356e64 AC |
4998 | end if; |
4999 | ||
d10bb9d5 | 5000 | Others_Etype := Typ; |
65356e64 | 5001 | |
937e9676 | 5002 | -- Copy the expression so that it is resolved |
a921e83c | 5003 | -- independently for each component, This is needed |
23a9215f | 5004 | -- for accessibility checks on components of anonymous |
a921e83c AC |
5005 | -- access types, even in compile_only mode. |
5006 | ||
5007 | if not Inside_A_Generic then | |
ba914484 VP |
5008 | return |
5009 | New_Copy_Tree_And_Copy_Dimensions | |
5010 | (Expression (Assoc)); | |
65356e64 AC |
5011 | else |
5012 | return Expression (Assoc); | |
5013 | end if; | |
996ae0b0 RK |
5014 | end if; |
5015 | end if; | |
5016 | ||
5017 | elsif Chars (Compon) = Chars (Selector_Name) then | |
5018 | if No (Expr) then | |
fbf5a39b | 5019 | |
0ab80019 | 5020 | -- Ada 2005 (AI-231) |
2820d220 | 5021 | |
0791fbe9 | 5022 | if Ada_Version >= Ada_2005 |
8133b9d1 | 5023 | and then Known_Null (Expression (Assoc)) |
2820d220 | 5024 | then |
82c80734 | 5025 | Check_Can_Never_Be_Null (Compon, Expression (Assoc)); |
2820d220 AC |
5026 | end if; |
5027 | ||
996ae0b0 RK |
5028 | -- We need to duplicate the expression when several |
5029 | -- components are grouped together with a "|" choice. | |
5030 | -- For instance "filed1 | filed2 => Expr" | |
5031 | ||
0ab80019 | 5032 | -- Ada 2005 (AI-287) |
2820d220 | 5033 | |
65356e64 | 5034 | if Box_Present (Assoc) then |
9b96e234 | 5035 | Is_Box_Present := True; |
65356e64 AC |
5036 | |
5037 | -- Duplicate the default expression of the component | |
c7ce71c2 ES |
5038 | -- from the record type declaration, so a new copy |
5039 | -- can be attached to the association. | |
65356e64 | 5040 | |
c7ce71c2 ES |
5041 | -- Note that we always copy the default expression, |
5042 | -- even when the association has a single choice, in | |
5043 | -- order to create a proper association for the | |
5044 | -- expanded aggregate. | |
5045 | ||
8097203f AC |
5046 | -- Component may have no default, in which case the |
5047 | -- expression is empty and the component is default- | |
5048 | -- initialized, but an association for the component | |
5049 | -- exists, and it is not covered by an others clause. | |
5050 | ||
d10bb9d5 AC |
5051 | -- Scalar and private types have no initialization |
5052 | -- procedure, so they remain uninitialized. If the | |
5053 | -- target of the aggregate is a constant this | |
5054 | -- deserves a warning. | |
5055 | ||
5056 | if No (Expression (Parent (Compon))) | |
5057 | and then not Has_Non_Null_Base_Init_Proc (Typ) | |
5058 | and then not Has_Aspect (Typ, Aspect_Default_Value) | |
5059 | and then not Is_Concurrent_Type (Typ) | |
5060 | and then Nkind (Parent (N)) = N_Object_Declaration | |
5061 | and then Constant_Present (Parent (N)) | |
5062 | then | |
5063 | Error_Msg_Node_2 := Typ; | |
5064 | Error_Msg_NE | |
d516e77b | 5065 | ("??component& of type& is uninitialized", |
d10bb9d5 AC |
5066 | Assoc, Selector_Name); |
5067 | ||
5068 | -- An additional reminder if the component type | |
5069 | -- is a generic formal. | |
5070 | ||
5071 | if Is_Generic_Type (Base_Type (Typ)) then | |
5072 | Error_Msg_NE | |
58009744 AC |
5073 | ("\instance should provide actual type with " |
5074 | & "initialization for&", Assoc, Typ); | |
d10bb9d5 AC |
5075 | end if; |
5076 | end if; | |
5077 | ||
ba914484 VP |
5078 | return |
5079 | New_Copy_Tree_And_Copy_Dimensions | |
5080 | (Expression (Parent (Compon))); | |
8097203f | 5081 | |
d05ef0ab | 5082 | else |
65356e64 | 5083 | if Present (Next (Selector_Name)) then |
d10bb9d5 AC |
5084 | Expr := New_Copy_Tree_And_Copy_Dimensions |
5085 | (Expression (Assoc)); | |
65356e64 AC |
5086 | else |
5087 | Expr := Expression (Assoc); | |
5088 | end if; | |
996ae0b0 RK |
5089 | end if; |
5090 | ||
55603e5e | 5091 | Generate_Reference (Compon, Selector_Name, 'm'); |
fbf5a39b | 5092 | |
996ae0b0 RK |
5093 | else |
5094 | Error_Msg_NE | |
5095 | ("more than one value supplied for &", | |
5096 | Selector_Name, Compon); | |
5097 | ||
5098 | end if; | |
5099 | end if; | |
5100 | ||
5101 | Next (Selector_Name); | |
5102 | end loop; | |
5103 | ||
5104 | Next (Assoc); | |
5105 | end loop; | |
5106 | ||
5107 | return Expr; | |
5108 | end Get_Value; | |
5109 | ||
937e9676 AC |
5110 | ----------------------------- |
5111 | -- Propagate_Discriminants -- | |
5112 | ----------------------------- | |
5113 | ||
5114 | procedure Propagate_Discriminants | |
5115 | (Aggr : Node_Id; | |
5116 | Assoc_List : List_Id) | |
5117 | is | |
5118 | Loc : constant Source_Ptr := Sloc (N); | |
5119 | ||
937e9676 AC |
5120 | procedure Process_Component (Comp : Entity_Id); |
5121 | -- Add one component with a box association to the inner aggregate, | |
5122 | -- and recurse if component is itself composite. | |
5123 | ||
5124 | ----------------------- | |
5125 | -- Process_Component -- | |
5126 | ----------------------- | |
5127 | ||
5128 | procedure Process_Component (Comp : Entity_Id) is | |
5129 | T : constant Entity_Id := Etype (Comp); | |
5130 | New_Aggr : Node_Id; | |
5131 | ||
5132 | begin | |
5133 | if Is_Record_Type (T) and then Has_Discriminants (T) then | |
0ad46f04 | 5134 | New_Aggr := Make_Aggregate (Loc, No_List, New_List); |
937e9676 AC |
5135 | Set_Etype (New_Aggr, T); |
5136 | ||
5137 | Add_Association | |
5138 | (Comp, New_Aggr, Component_Associations (Aggr)); | |
5139 | ||
5140 | -- Collect discriminant values and recurse | |
5141 | ||
5142 | Add_Discriminant_Values (New_Aggr, Assoc_List); | |
5143 | Propagate_Discriminants (New_Aggr, Assoc_List); | |
5144 | ||
fc4c7348 PT |
5145 | Build_Constrained_Itype |
5146 | (New_Aggr, T, Component_Associations (New_Aggr)); | |
937e9676 | 5147 | else |
0c25b783 PT |
5148 | Add_Association |
5149 | (Comp, Empty, Component_Associations (Aggr), | |
5150 | Is_Box_Present => True); | |
937e9676 AC |
5151 | end if; |
5152 | end Process_Component; | |
5153 | ||
5154 | -- Local variables | |
5155 | ||
5156 | Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr)); | |
5157 | Components : constant Elist_Id := New_Elmt_List; | |
5158 | Def_Node : constant Node_Id := | |
5159 | Type_Definition (Declaration_Node (Aggr_Type)); | |
5160 | ||
5161 | Comp : Node_Id; | |
5162 | Comp_Elmt : Elmt_Id; | |
5163 | Errors : Boolean; | |
5164 | ||
5165 | -- Start of processing for Propagate_Discriminants | |
5166 | ||
5167 | begin | |
5168 | -- The component type may be a variant type. Collect the components | |
5169 | -- that are ruled by the known values of the discriminants. Their | |
5170 | -- values have already been inserted into the component list of the | |
5171 | -- current aggregate. | |
5172 | ||
5173 | if Nkind (Def_Node) = N_Record_Definition | |
5174 | and then Present (Component_List (Def_Node)) | |
5175 | and then Present (Variant_Part (Component_List (Def_Node))) | |
5176 | then | |
5177 | Gather_Components (Aggr_Type, | |
5178 | Component_List (Def_Node), | |
5179 | Governed_By => Component_Associations (Aggr), | |
5180 | Into => Components, | |
5181 | Report_Errors => Errors); | |
5182 | ||
5183 | Comp_Elmt := First_Elmt (Components); | |
5184 | while Present (Comp_Elmt) loop | |
5185 | if Ekind (Node (Comp_Elmt)) /= E_Discriminant then | |
5186 | Process_Component (Node (Comp_Elmt)); | |
5187 | end if; | |
5188 | ||
5189 | Next_Elmt (Comp_Elmt); | |
5190 | end loop; | |
5191 | ||
5192 | -- No variant part, iterate over all components | |
5193 | ||
5194 | else | |
5195 | Comp := First_Component (Etype (Aggr)); | |
5196 | while Present (Comp) loop | |
5197 | Process_Component (Comp); | |
5198 | Next_Component (Comp); | |
5199 | end loop; | |
5200 | end if; | |
937e9676 AC |
5201 | end Propagate_Discriminants; |
5202 | ||
996ae0b0 RK |
5203 | ----------------------- |
5204 | -- Resolve_Aggr_Expr -- | |
5205 | ----------------------- | |
5206 | ||
937e9676 | 5207 | procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is |
996ae0b0 RK |
5208 | function Has_Expansion_Delayed (Expr : Node_Id) return Boolean; |
5209 | -- If the expression is an aggregate (possibly qualified) then its | |
5210 | -- expansion is delayed until the enclosing aggregate is expanded | |
5211 | -- into assignments. In that case, do not generate checks on the | |
5212 | -- expression, because they will be generated later, and will other- | |
92b751fd | 5213 | -- wise force a copy (to remove side effects) that would leave a |
996ae0b0 RK |
5214 | -- dynamic-sized aggregate in the code, something that gigi cannot |
5215 | -- handle. | |
5216 | ||
22243c12 RD |
5217 | --------------------------- |
5218 | -- Has_Expansion_Delayed -- | |
5219 | --------------------------- | |
996ae0b0 RK |
5220 | |
5221 | function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is | |
996ae0b0 | 5222 | begin |
937e9676 | 5223 | return |
4a08c95c | 5224 | (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate |
937e9676 AC |
5225 | and then Present (Etype (Expr)) |
5226 | and then Is_Record_Type (Etype (Expr)) | |
5227 | and then Expansion_Delayed (Expr)) | |
5228 | or else | |
5229 | (Nkind (Expr) = N_Qualified_Expression | |
5230 | and then Has_Expansion_Delayed (Expression (Expr))); | |
996ae0b0 RK |
5231 | end Has_Expansion_Delayed; |
5232 | ||
10edebe7 AC |
5233 | -- Local variables |
5234 | ||
5235 | Expr_Type : Entity_Id := Empty; | |
5236 | New_C : Entity_Id := Component; | |
5237 | New_Expr : Node_Id; | |
5238 | ||
5239 | Relocate : Boolean; | |
5240 | -- Set to True if the resolved Expr node needs to be relocated when | |
5241 | -- attached to the newly created association list. This node need not | |
5242 | -- be relocated if its parent pointer is not set. In fact in this | |
5243 | -- case Expr is the output of a New_Copy_Tree call. If Relocate is | |
5244 | -- True then we have analyzed the expression node in the original | |
5245 | -- aggregate and hence it needs to be relocated when moved over to | |
5246 | -- the new association list. | |
5247 | ||
22243c12 | 5248 | -- Start of processing for Resolve_Aggr_Expr |
996ae0b0 RK |
5249 | |
5250 | begin | |
5251 | -- If the type of the component is elementary or the type of the | |
5252 | -- aggregate does not contain discriminants, use the type of the | |
5253 | -- component to resolve Expr. | |
5254 | ||
5255 | if Is_Elementary_Type (Etype (Component)) | |
5256 | or else not Has_Discriminants (Etype (N)) | |
5257 | then | |
5258 | Expr_Type := Etype (Component); | |
5259 | ||
5260 | -- Otherwise we have to pick up the new type of the component from | |
12a13f01 | 5261 | -- the new constrained subtype of the aggregate. In fact components |
996ae0b0 RK |
5262 | -- which are of a composite type might be constrained by a |
5263 | -- discriminant, and we want to resolve Expr against the subtype were | |
5264 | -- all discriminant occurrences are replaced with their actual value. | |
5265 | ||
5266 | else | |
5267 | New_C := First_Component (Etype (N)); | |
5268 | while Present (New_C) loop | |
5269 | if Chars (New_C) = Chars (Component) then | |
5270 | Expr_Type := Etype (New_C); | |
5271 | exit; | |
5272 | end if; | |
5273 | ||
5274 | Next_Component (New_C); | |
5275 | end loop; | |
5276 | ||
5277 | pragma Assert (Present (Expr_Type)); | |
5278 | ||
5279 | -- For each range in an array type where a discriminant has been | |
5280 | -- replaced with the constraint, check that this range is within | |
ec53a6da JM |
5281 | -- the range of the base type. This checks is done in the init |
5282 | -- proc for regular objects, but has to be done here for | |
fbf5a39b | 5283 | -- aggregates since no init proc is called for them. |
996ae0b0 RK |
5284 | |
5285 | if Is_Array_Type (Expr_Type) then | |
5286 | declare | |
7f9747c6 | 5287 | Index : Node_Id; |
ec53a6da | 5288 | -- Range of the current constrained index in the array |
996ae0b0 | 5289 | |
ec53a6da | 5290 | Orig_Index : Node_Id := First_Index (Etype (Component)); |
996ae0b0 RK |
5291 | -- Range corresponding to the range Index above in the |
5292 | -- original unconstrained record type. The bounds of this | |
5293 | -- range may be governed by discriminants. | |
5294 | ||
5295 | Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type)); | |
5296 | -- Range corresponding to the range Index above for the | |
5297 | -- unconstrained array type. This range is needed to apply | |
5298 | -- range checks. | |
5299 | ||
5300 | begin | |
7f9747c6 | 5301 | Index := First_Index (Expr_Type); |
996ae0b0 RK |
5302 | while Present (Index) loop |
5303 | if Depends_On_Discriminant (Orig_Index) then | |
5304 | Apply_Range_Check (Index, Etype (Unconstr_Index)); | |
5305 | end if; | |
5306 | ||
5307 | Next_Index (Index); | |
5308 | Next_Index (Orig_Index); | |
5309 | Next_Index (Unconstr_Index); | |
5310 | end loop; | |
5311 | end; | |
5312 | end if; | |
5313 | end if; | |
5314 | ||
5315 | -- If the Parent pointer of Expr is not set, Expr is an expression | |
5316 | -- duplicated by New_Tree_Copy (this happens for record aggregates | |
5317 | -- that look like (Field1 | Filed2 => Expr) or (others => Expr)). | |
5318 | -- Such a duplicated expression must be attached to the tree | |
5319 | -- before analysis and resolution to enforce the rule that a tree | |
5320 | -- fragment should never be analyzed or resolved unless it is | |
5321 | -- attached to the current compilation unit. | |
5322 | ||
5323 | if No (Parent (Expr)) then | |
5324 | Set_Parent (Expr, N); | |
5325 | Relocate := False; | |
5326 | else | |
5327 | Relocate := True; | |
5328 | end if; | |
5329 | ||
5330 | Analyze_And_Resolve (Expr, Expr_Type); | |
ca44152f | 5331 | Check_Expr_OK_In_Limited_Aggregate (Expr); |
996ae0b0 | 5332 | Check_Non_Static_Context (Expr); |
fbf5a39b | 5333 | Check_Unset_Reference (Expr); |
996ae0b0 | 5334 | |
0c020dde AC |
5335 | -- Check wrong use of class-wide types |
5336 | ||
7b4db06c | 5337 | if Is_Class_Wide_Type (Etype (Expr)) then |
0c020dde AC |
5338 | Error_Msg_N ("dynamically tagged expression not allowed", Expr); |
5339 | end if; | |
5340 | ||
996ae0b0 RK |
5341 | if not Has_Expansion_Delayed (Expr) then |
5342 | Aggregate_Constraint_Checks (Expr, Expr_Type); | |
887d102a AC |
5343 | end if; |
5344 | ||
dec6faf1 AC |
5345 | -- If an aggregate component has a type with predicates, an explicit |
5346 | -- predicate check must be applied, as for an assignment statement, | |
152f64c2 | 5347 | -- because the aggregate might not be expanded into individual |
dec6faf1 AC |
5348 | -- component assignments. |
5349 | ||
619bfd9f | 5350 | if Has_Predicates (Expr_Type) |
07eb872e AC |
5351 | and then Analyzed (Expr) |
5352 | then | |
887d102a | 5353 | Apply_Predicate_Check (Expr, Expr_Type); |
996ae0b0 RK |
5354 | end if; |
5355 | ||
5356 | if Raises_Constraint_Error (Expr) then | |
5357 | Set_Raises_Constraint_Error (N); | |
5358 | end if; | |
5359 | ||
22243c12 | 5360 | -- If the expression has been marked as requiring a range check, then |
edab6088 RD |
5361 | -- generate it here. It's a bit odd to be generating such checks in |
5362 | -- the analyzer, but harmless since Generate_Range_Check does nothing | |
5363 | -- (other than making sure Do_Range_Check is set) if the expander is | |
5364 | -- not active. | |
d79e621a GD |
5365 | |
5366 | if Do_Range_Check (Expr) then | |
d79e621a GD |
5367 | Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed); |
5368 | end if; | |
5369 | ||
937e9676 AC |
5370 | -- Add association Component => Expr if the caller requests it |
5371 | ||
996ae0b0 | 5372 | if Relocate then |
0929eaeb AC |
5373 | New_Expr := Relocate_Node (Expr); |
5374 | ||
5375 | -- Since New_Expr is not gonna be analyzed later on, we need to | |
5376 | -- propagate here the dimensions form Expr to New_Expr. | |
5377 | ||
ba914484 | 5378 | Copy_Dimensions (Expr, New_Expr); |
d976bf74 | 5379 | |
996ae0b0 | 5380 | else |
0929eaeb | 5381 | New_Expr := Expr; |
996ae0b0 | 5382 | end if; |
0929eaeb AC |
5383 | |
5384 | Add_Association (New_C, New_Expr, New_Assoc_List); | |
996ae0b0 RK |
5385 | end Resolve_Aggr_Expr; |
5386 | ||
333e4f86 AC |
5387 | ------------------- |
5388 | -- Rewrite_Range -- | |
5389 | ------------------- | |
5390 | ||
5391 | procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is | |
333e4f86 AC |
5392 | procedure Rewrite_Bound |
5393 | (Bound : Node_Id; | |
5394 | Disc : Entity_Id; | |
5395 | Expr_Disc : Node_Id); | |
5396 | -- Rewrite a bound of the range Bound, when it is equal to the | |
5397 | -- non-stored discriminant Disc, into the stored discriminant | |
5398 | -- value Expr_Disc. | |
5399 | ||
5400 | ------------------- | |
5401 | -- Rewrite_Bound -- | |
5402 | ------------------- | |
5403 | ||
5404 | procedure Rewrite_Bound | |
5405 | (Bound : Node_Id; | |
5406 | Disc : Entity_Id; | |
5407 | Expr_Disc : Node_Id) | |
5408 | is | |
5409 | begin | |
2c26d262 EB |
5410 | if Nkind (Bound) /= N_Identifier then |
5411 | return; | |
5412 | end if; | |
5413 | ||
5414 | -- We expect either the discriminant or the discriminal | |
5415 | ||
5416 | if Entity (Bound) = Disc | |
5417 | or else (Ekind (Entity (Bound)) = E_In_Parameter | |
5418 | and then Discriminal_Link (Entity (Bound)) = Disc) | |
333e4f86 AC |
5419 | then |
5420 | Rewrite (Bound, New_Copy_Tree (Expr_Disc)); | |
5421 | end if; | |
5422 | end Rewrite_Bound; | |
5423 | ||
9c5719f6 | 5424 | -- Local variables |
333e4f86 AC |
5425 | |
5426 | Low, High : Node_Id; | |
5427 | Disc : Entity_Id; | |
5428 | Expr_Disc : Elmt_Id; | |
5429 | ||
5430 | -- Start of processing for Rewrite_Range | |
5431 | ||
5432 | begin | |
2c26d262 | 5433 | if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then |
333e4f86 AC |
5434 | Low := Low_Bound (Rge); |
5435 | High := High_Bound (Rge); | |
5436 | ||
13126368 YM |
5437 | Disc := First_Discriminant (Root_Type); |
5438 | Expr_Disc := First_Elmt (Stored_Constraint (Etype (N))); | |
333e4f86 AC |
5439 | while Present (Disc) loop |
5440 | Rewrite_Bound (Low, Disc, Node (Expr_Disc)); | |
5441 | Rewrite_Bound (High, Disc, Node (Expr_Disc)); | |
5442 | Next_Discriminant (Disc); | |
5443 | Next_Elmt (Expr_Disc); | |
5444 | end loop; | |
5445 | end if; | |
5446 | end Rewrite_Range; | |
5447 | ||
937e9676 AC |
5448 | -- Local variables |
5449 | ||
5450 | Components : constant Elist_Id := New_Elmt_List; | |
5451 | -- Components is the list of the record components whose value must be | |
5452 | -- provided in the aggregate. This list does include discriminants. | |
5453 | ||
937e9676 AC |
5454 | Component : Entity_Id; |
5455 | Component_Elmt : Elmt_Id; | |
13126368 | 5456 | Expr : Node_Id; |
937e9676 AC |
5457 | Positional_Expr : Node_Id; |
5458 | ||
996ae0b0 RK |
5459 | -- Start of processing for Resolve_Record_Aggregate |
5460 | ||
5461 | begin | |
2ba431e5 | 5462 | -- A record aggregate is restricted in SPARK: |
15918371 | 5463 | |
bd65a2d7 AC |
5464 | -- Each named association can have only a single choice. |
5465 | -- OTHERS cannot be used. | |
5466 | -- Positional and named associations cannot be mixed. | |
9f90d123 | 5467 | |
e35ecc88 | 5468 | if Present (Component_Associations (N)) then |
9f90d123 AC |
5469 | declare |
5470 | Assoc : Node_Id; | |
bd65a2d7 | 5471 | |
9f90d123 AC |
5472 | begin |
5473 | Assoc := First (Component_Associations (N)); | |
5474 | while Present (Assoc) loop | |
ccd0ed95 | 5475 | if Nkind (Assoc) = N_Iterated_Component_Association then |
7f5e671b PMR |
5476 | Error_Msg_N |
5477 | ("iterated component association can only appear in an " | |
5478 | & "array aggregate", N); | |
ccd0ed95 | 5479 | raise Unrecoverable_Error; |
9f90d123 | 5480 | end if; |
bd65a2d7 | 5481 | |
cbbe41d1 | 5482 | Next (Assoc); |
9f90d123 AC |
5483 | end loop; |
5484 | end; | |
5485 | end if; | |
5486 | ||
996ae0b0 RK |
5487 | -- We may end up calling Duplicate_Subexpr on expressions that are |
5488 | -- attached to New_Assoc_List. For this reason we need to attach it | |
5489 | -- to the tree by setting its parent pointer to N. This parent point | |
5490 | -- will change in STEP 8 below. | |
5491 | ||
5492 | Set_Parent (New_Assoc_List, N); | |
5493 | ||
5494 | -- STEP 1: abstract type and null record verification | |
5495 | ||
aad93b55 | 5496 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
5497 | Error_Msg_N ("type of aggregate cannot be abstract", N); |
5498 | end if; | |
5499 | ||
5500 | if No (First_Entity (Typ)) and then Null_Record_Present (N) then | |
5501 | Set_Etype (N, Typ); | |
5502 | return; | |
5503 | ||
5504 | elsif Present (First_Entity (Typ)) | |
5505 | and then Null_Record_Present (N) | |
5506 | and then not Is_Tagged_Type (Typ) | |
5507 | then | |
5508 | Error_Msg_N ("record aggregate cannot be null", N); | |
5509 | return; | |
5510 | ||
eff332d9 GD |
5511 | -- If the type has no components, then the aggregate should either |
5512 | -- have "null record", or in Ada 2005 it could instead have a single | |
22243c12 RD |
5513 | -- component association given by "others => <>". For Ada 95 we flag an |
5514 | -- error at this point, but for Ada 2005 we proceed with checking the | |
5515 | -- associations below, which will catch the case where it's not an | |
5516 | -- aggregate with "others => <>". Note that the legality of a <> | |
eff332d9 GD |
5517 | -- aggregate for a null record type was established by AI05-016. |
5518 | ||
5519 | elsif No (First_Entity (Typ)) | |
0791fbe9 | 5520 | and then Ada_Version < Ada_2005 |
eff332d9 | 5521 | then |
996ae0b0 RK |
5522 | Error_Msg_N ("record aggregate must be null", N); |
5523 | return; | |
5524 | end if; | |
5525 | ||
fe664d36 MP |
5526 | -- A record aggregate can only use parentheses |
5527 | ||
5528 | if Nkind (N) = N_Aggregate | |
5529 | and then Is_Homogeneous_Aggregate (N) | |
5530 | then | |
9af8c27f | 5531 | Error_Msg_N ("record aggregate must use (), not '[']", N); |
fe664d36 MP |
5532 | return; |
5533 | end if; | |
5534 | ||
996ae0b0 RK |
5535 | -- STEP 2: Verify aggregate structure |
5536 | ||
5537 | Step_2 : declare | |
937e9676 | 5538 | Assoc : Node_Id; |
996ae0b0 | 5539 | Bad_Aggregate : Boolean := False; |
937e9676 | 5540 | Selector_Name : Node_Id; |
996ae0b0 RK |
5541 | |
5542 | begin | |
5543 | if Present (Component_Associations (N)) then | |
5544 | Assoc := First (Component_Associations (N)); | |
5545 | else | |
5546 | Assoc := Empty; | |
5547 | end if; | |
5548 | ||
5549 | while Present (Assoc) loop | |
5550 | Selector_Name := First (Choices (Assoc)); | |
5551 | while Present (Selector_Name) loop | |
5552 | if Nkind (Selector_Name) = N_Identifier then | |
5553 | null; | |
5554 | ||
5555 | elsif Nkind (Selector_Name) = N_Others_Choice then | |
5556 | if Selector_Name /= First (Choices (Assoc)) | |
5557 | or else Present (Next (Selector_Name)) | |
5558 | then | |
ed2233dc | 5559 | Error_Msg_N |
22cb89b5 AC |
5560 | ("OTHERS must appear alone in a choice list", |
5561 | Selector_Name); | |
996ae0b0 RK |
5562 | return; |
5563 | ||
5564 | elsif Present (Next (Assoc)) then | |
ed2233dc | 5565 | Error_Msg_N |
22cb89b5 AC |
5566 | ("OTHERS must appear last in an aggregate", |
5567 | Selector_Name); | |
996ae0b0 | 5568 | return; |
1ab9541b | 5569 | |
885c4871 | 5570 | -- (Ada 2005): If this is an association with a box, |
1ab9541b ES |
5571 | -- indicate that the association need not represent |
5572 | -- any component. | |
5573 | ||
5574 | elsif Box_Present (Assoc) then | |
ec3c7387 AC |
5575 | Others_Box := 1; |
5576 | Box_Node := Assoc; | |
996ae0b0 RK |
5577 | end if; |
5578 | ||
5579 | else | |
5580 | Error_Msg_N | |
5581 | ("selector name should be identifier or OTHERS", | |
5582 | Selector_Name); | |
5583 | Bad_Aggregate := True; | |
5584 | end if; | |
5585 | ||
5586 | Next (Selector_Name); | |
5587 | end loop; | |
5588 | ||
5589 | Next (Assoc); | |
5590 | end loop; | |
5591 | ||
5592 | if Bad_Aggregate then | |
5593 | return; | |
5594 | end if; | |
5595 | end Step_2; | |
5596 | ||
5597 | -- STEP 3: Find discriminant Values | |
5598 | ||
5599 | Step_3 : declare | |
5600 | Discrim : Entity_Id; | |
5601 | Missing_Discriminants : Boolean := False; | |
5602 | ||
5603 | begin | |
5604 | if Present (Expressions (N)) then | |
5605 | Positional_Expr := First (Expressions (N)); | |
5606 | else | |
5607 | Positional_Expr := Empty; | |
5608 | end if; | |
5609 | ||
87729e5a | 5610 | -- AI05-0115: if the ancestor part is a subtype mark, the ancestor |
33bd17e7 | 5611 | -- must not have unknown discriminants. |
51e2de47 AC |
5612 | -- ??? We are not checking any subtype mark here and this code is not |
5613 | -- exercised by any test, so it's likely wrong (in particular | |
5614 | -- we should not use Root_Type here but the subtype mark, if any), | |
5615 | -- and possibly not needed. | |
87729e5a AC |
5616 | |
5617 | if Is_Derived_Type (Typ) | |
5618 | and then Has_Unknown_Discriminants (Root_Type (Typ)) | |
5619 | and then Nkind (N) /= N_Extension_Aggregate | |
5620 | then | |
5621 | Error_Msg_NE | |
5622 | ("aggregate not available for type& whose ancestor " | |
0bfa2f3c | 5623 | & "has unknown discriminants", N, Typ); |
87729e5a AC |
5624 | end if; |
5625 | ||
9013065b AC |
5626 | if Has_Unknown_Discriminants (Typ) |
5627 | and then Present (Underlying_Record_View (Typ)) | |
5628 | then | |
5629 | Discrim := First_Discriminant (Underlying_Record_View (Typ)); | |
5630 | elsif Has_Discriminants (Typ) then | |
996ae0b0 RK |
5631 | Discrim := First_Discriminant (Typ); |
5632 | else | |
5633 | Discrim := Empty; | |
5634 | end if; | |
5635 | ||
5636 | -- First find the discriminant values in the positional components | |
5637 | ||
5638 | while Present (Discrim) and then Present (Positional_Expr) loop | |
937e9676 | 5639 | if Discriminant_Present (Discrim) then |
996ae0b0 | 5640 | Resolve_Aggr_Expr (Positional_Expr, Discrim); |
2820d220 | 5641 | |
0ab80019 | 5642 | -- Ada 2005 (AI-231) |
2820d220 | 5643 | |
0791fbe9 | 5644 | if Ada_Version >= Ada_2005 |
8133b9d1 | 5645 | and then Known_Null (Positional_Expr) |
ec53a6da | 5646 | then |
82c80734 | 5647 | Check_Can_Never_Be_Null (Discrim, Positional_Expr); |
2820d220 AC |
5648 | end if; |
5649 | ||
996ae0b0 RK |
5650 | Next (Positional_Expr); |
5651 | end if; | |
5652 | ||
5653 | if Present (Get_Value (Discrim, Component_Associations (N))) then | |
5654 | Error_Msg_NE | |
5655 | ("more than one value supplied for discriminant&", | |
5656 | N, Discrim); | |
5657 | end if; | |
5658 | ||
5659 | Next_Discriminant (Discrim); | |
5660 | end loop; | |
5661 | ||
50decc81 | 5662 | -- Find remaining discriminant values if any among named components |
996ae0b0 RK |
5663 | |
5664 | while Present (Discrim) loop | |
5665 | Expr := Get_Value (Discrim, Component_Associations (N), True); | |
5666 | ||
937e9676 | 5667 | if not Discriminant_Present (Discrim) then |
996ae0b0 RK |
5668 | if Present (Expr) then |
5669 | Error_Msg_NE | |
58009744 | 5670 | ("more than one value supplied for discriminant &", |
996ae0b0 RK |
5671 | N, Discrim); |
5672 | end if; | |
5673 | ||
5674 | elsif No (Expr) then | |
5675 | Error_Msg_NE | |
5676 | ("no value supplied for discriminant &", N, Discrim); | |
5677 | Missing_Discriminants := True; | |
5678 | ||
5679 | else | |
5680 | Resolve_Aggr_Expr (Expr, Discrim); | |
5681 | end if; | |
5682 | ||
5683 | Next_Discriminant (Discrim); | |
5684 | end loop; | |
5685 | ||
5686 | if Missing_Discriminants then | |
5687 | return; | |
5688 | end if; | |
5689 | ||
5690 | -- At this point and until the beginning of STEP 6, New_Assoc_List | |
5691 | -- contains only the discriminants and their values. | |
5692 | ||
5693 | end Step_3; | |
5694 | ||
5695 | -- STEP 4: Set the Etype of the record aggregate | |
5696 | ||
9013065b AC |
5697 | if Has_Discriminants (Typ) |
5698 | or else (Has_Unknown_Discriminants (Typ) | |
58009744 | 5699 | and then Present (Underlying_Record_View (Typ))) |
9013065b | 5700 | then |
fc4c7348 | 5701 | Build_Constrained_Itype (N, Typ, New_Assoc_List); |
996ae0b0 RK |
5702 | else |
5703 | Set_Etype (N, Typ); | |
5704 | end if; | |
5705 | ||
5706 | -- STEP 5: Get remaining components according to discriminant values | |
5707 | ||
5708 | Step_5 : declare | |
937e9676 AC |
5709 | Dnode : Node_Id; |
5710 | Errors_Found : Boolean := False; | |
996ae0b0 RK |
5711 | Record_Def : Node_Id; |
5712 | Parent_Typ : Entity_Id; | |
996ae0b0 RK |
5713 | Parent_Typ_List : Elist_Id; |
5714 | Parent_Elmt : Elmt_Id; | |
937e9676 | 5715 | Root_Typ : Entity_Id; |
15918371 | 5716 | |
996ae0b0 RK |
5717 | begin |
5718 | if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then | |
5719 | Parent_Typ_List := New_Elmt_List; | |
5720 | ||
5721 | -- If this is an extension aggregate, the component list must | |
965dbd5c AC |
5722 | -- include all components that are not in the given ancestor type. |
5723 | -- Otherwise, the component list must include components of all | |
5724 | -- ancestors, starting with the root. | |
996ae0b0 RK |
5725 | |
5726 | if Nkind (N) = N_Extension_Aggregate then | |
7b4db06c | 5727 | Root_Typ := Base_Type (Etype (Ancestor_Part (N))); |
69a0c174 | 5728 | |
996ae0b0 | 5729 | else |
937e9676 AC |
5730 | -- AI05-0115: check legality of aggregate for type with a |
5731 | -- private ancestor. | |
87729e5a | 5732 | |
996ae0b0 | 5733 | Root_Typ := Root_Type (Typ); |
87729e5a AC |
5734 | if Has_Private_Ancestor (Typ) then |
5735 | declare | |
5736 | Ancestor : constant Entity_Id := | |
937e9676 | 5737 | Find_Private_Ancestor (Typ); |
87729e5a | 5738 | Ancestor_Unit : constant Entity_Id := |
937e9676 AC |
5739 | Cunit_Entity |
5740 | (Get_Source_Unit (Ancestor)); | |
87729e5a | 5741 | Parent_Unit : constant Entity_Id := |
937e9676 AC |
5742 | Cunit_Entity (Get_Source_Unit |
5743 | (Base_Type (Etype (Ancestor)))); | |
87729e5a | 5744 | begin |
58009744 | 5745 | -- Check whether we are in a scope that has full view |
87729e5a AC |
5746 | -- over the private ancestor and its parent. This can |
5747 | -- only happen if the derivation takes place in a child | |
5748 | -- unit of the unit that declares the parent, and we are | |
5749 | -- in the private part or body of that child unit, else | |
5750 | -- the aggregate is illegal. | |
5751 | ||
5752 | if Is_Child_Unit (Ancestor_Unit) | |
5753 | and then Scope (Ancestor_Unit) = Parent_Unit | |
5754 | and then In_Open_Scopes (Scope (Ancestor)) | |
5755 | and then | |
5756 | (In_Private_Part (Scope (Ancestor)) | |
58009744 | 5757 | or else In_Package_Body (Scope (Ancestor))) |
87729e5a AC |
5758 | then |
5759 | null; | |
5760 | ||
5761 | else | |
5762 | Error_Msg_NE | |
5763 | ("type of aggregate has private ancestor&!", | |
58009744 | 5764 | N, Root_Typ); |
87729e5a AC |
5765 | Error_Msg_N ("must use extension aggregate!", N); |
5766 | return; | |
5767 | end if; | |
5768 | end; | |
996ae0b0 RK |
5769 | end if; |
5770 | ||
5771 | Dnode := Declaration_Node (Base_Type (Root_Typ)); | |
5772 | ||
4519314c AC |
5773 | -- If we don't get a full declaration, then we have some error |
5774 | -- which will get signalled later so skip this part. Otherwise | |
5775 | -- gather components of root that apply to the aggregate type. | |
5776 | -- We use the base type in case there is an applicable stored | |
5777 | -- constraint that renames the discriminants of the root. | |
996ae0b0 RK |
5778 | |
5779 | if Nkind (Dnode) = N_Full_Type_Declaration then | |
5780 | Record_Def := Type_Definition (Dnode); | |
15918371 AC |
5781 | Gather_Components |
5782 | (Base_Type (Typ), | |
5783 | Component_List (Record_Def), | |
5784 | Governed_By => New_Assoc_List, | |
5785 | Into => Components, | |
5786 | Report_Errors => Errors_Found); | |
4ffafd86 AC |
5787 | |
5788 | if Errors_Found then | |
5789 | Error_Msg_N | |
5790 | ("discriminant controlling variant part is not static", | |
5791 | N); | |
5792 | return; | |
5793 | end if; | |
996ae0b0 RK |
5794 | end if; |
5795 | end if; | |
5796 | ||
9013065b | 5797 | Parent_Typ := Base_Type (Typ); |
996ae0b0 | 5798 | while Parent_Typ /= Root_Typ loop |
996ae0b0 RK |
5799 | Prepend_Elmt (Parent_Typ, To => Parent_Typ_List); |
5800 | Parent_Typ := Etype (Parent_Typ); | |
5801 | ||
0b888042 ES |
5802 | -- Check whether a private parent requires the use of |
5803 | -- an extension aggregate. This test does not apply in | |
5804 | -- an instantiation: if the generic unit is legal so is | |
5805 | -- the instance. | |
5806 | ||
fbf5a39b | 5807 | if Nkind (Parent (Base_Type (Parent_Typ))) = |
996ae0b0 | 5808 | N_Private_Type_Declaration |
fbf5a39b AC |
5809 | or else Nkind (Parent (Base_Type (Parent_Typ))) = |
5810 | N_Private_Extension_Declaration | |
996ae0b0 | 5811 | then |
0b888042 ES |
5812 | if Nkind (N) /= N_Extension_Aggregate |
5813 | and then not In_Instance | |
5814 | then | |
ed2233dc | 5815 | Error_Msg_NE |
996ae0b0 RK |
5816 | ("type of aggregate has private ancestor&!", |
5817 | N, Parent_Typ); | |
ed2233dc | 5818 | Error_Msg_N ("must use extension aggregate!", N); |
996ae0b0 RK |
5819 | return; |
5820 | ||
5821 | elsif Parent_Typ /= Root_Typ then | |
5822 | Error_Msg_NE | |
5823 | ("ancestor part of aggregate must be private type&", | |
5824 | Ancestor_Part (N), Parent_Typ); | |
5825 | return; | |
5826 | end if; | |
4519314c AC |
5827 | |
5828 | -- The current view of ancestor part may be a private type, | |
5829 | -- while the context type is always non-private. | |
5830 | ||
5831 | elsif Is_Private_Type (Root_Typ) | |
5832 | and then Present (Full_View (Root_Typ)) | |
5833 | and then Nkind (N) = N_Extension_Aggregate | |
5834 | then | |
5835 | exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ; | |
996ae0b0 RK |
5836 | end if; |
5837 | end loop; | |
5838 | ||
bf06d37f AC |
5839 | -- Now collect components from all other ancestors, beginning |
5840 | -- with the current type. If the type has unknown discriminants | |
349ff68f | 5841 | -- use the component list of the Underlying_Record_View, which |
bf06d37f AC |
5842 | -- needs to be used for the subsequent expansion of the aggregate |
5843 | -- into assignments. | |
996ae0b0 RK |
5844 | |
5845 | Parent_Elmt := First_Elmt (Parent_Typ_List); | |
5846 | while Present (Parent_Elmt) loop | |
5847 | Parent_Typ := Node (Parent_Elmt); | |
bf06d37f AC |
5848 | |
5849 | if Has_Unknown_Discriminants (Parent_Typ) | |
5850 | and then Present (Underlying_Record_View (Typ)) | |
5851 | then | |
5852 | Parent_Typ := Underlying_Record_View (Parent_Typ); | |
5853 | end if; | |
5854 | ||
996ae0b0 | 5855 | Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ))); |
265c571d | 5856 | Gather_Components (Parent_Typ, |
996ae0b0 RK |
5857 | Component_List (Record_Extension_Part (Record_Def)), |
5858 | Governed_By => New_Assoc_List, | |
5859 | Into => Components, | |
5860 | Report_Errors => Errors_Found); | |
5861 | ||
5862 | Next_Elmt (Parent_Elmt); | |
5863 | end loop; | |
5864 | ||
33bd17e7 ES |
5865 | -- Typ is not a derived tagged type |
5866 | ||
996ae0b0 | 5867 | else |
6bde3eb5 | 5868 | Record_Def := Type_Definition (Parent (Base_Type (Typ))); |
996ae0b0 RK |
5869 | |
5870 | if Null_Present (Record_Def) then | |
5871 | null; | |
bf06d37f AC |
5872 | |
5873 | elsif not Has_Unknown_Discriminants (Typ) then | |
15918371 AC |
5874 | Gather_Components |
5875 | (Base_Type (Typ), | |
5876 | Component_List (Record_Def), | |
5877 | Governed_By => New_Assoc_List, | |
5878 | Into => Components, | |
5879 | Report_Errors => Errors_Found); | |
bf06d37f AC |
5880 | |
5881 | else | |
5882 | Gather_Components | |
5883 | (Base_Type (Underlying_Record_View (Typ)), | |
15918371 AC |
5884 | Component_List (Record_Def), |
5885 | Governed_By => New_Assoc_List, | |
5886 | Into => Components, | |
5887 | Report_Errors => Errors_Found); | |
996ae0b0 RK |
5888 | end if; |
5889 | end if; | |
5890 | ||
5891 | if Errors_Found then | |
5892 | return; | |
5893 | end if; | |
5894 | end Step_5; | |
5895 | ||
5896 | -- STEP 6: Find component Values | |
5897 | ||
996ae0b0 RK |
5898 | Component_Elmt := First_Elmt (Components); |
5899 | ||
5900 | -- First scan the remaining positional associations in the aggregate. | |
5901 | -- Remember that at this point Positional_Expr contains the current | |
5902 | -- positional association if any is left after looking for discriminant | |
5903 | -- values in step 3. | |
5904 | ||
5905 | while Present (Positional_Expr) and then Present (Component_Elmt) loop | |
5906 | Component := Node (Component_Elmt); | |
5907 | Resolve_Aggr_Expr (Positional_Expr, Component); | |
5908 | ||
0ab80019 AC |
5909 | -- Ada 2005 (AI-231) |
5910 | ||
58009744 | 5911 | if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then |
82c80734 | 5912 | Check_Can_Never_Be_Null (Component, Positional_Expr); |
2820d220 AC |
5913 | end if; |
5914 | ||
996ae0b0 RK |
5915 | if Present (Get_Value (Component, Component_Associations (N))) then |
5916 | Error_Msg_NE | |
add27f7a | 5917 | ("more than one value supplied for component &", N, Component); |
996ae0b0 RK |
5918 | end if; |
5919 | ||
5920 | Next (Positional_Expr); | |
5921 | Next_Elmt (Component_Elmt); | |
5922 | end loop; | |
5923 | ||
5924 | if Present (Positional_Expr) then | |
5925 | Error_Msg_N | |
5926 | ("too many components for record aggregate", Positional_Expr); | |
5927 | end if; | |
5928 | ||
5929 | -- Now scan for the named arguments of the aggregate | |
5930 | ||
5931 | while Present (Component_Elmt) loop | |
5932 | Component := Node (Component_Elmt); | |
5933 | Expr := Get_Value (Component, Component_Associations (N), True); | |
5934 | ||
9b96e234 | 5935 | -- Note: The previous call to Get_Value sets the value of the |
f91e8020 | 5936 | -- variable Is_Box_Present. |
65356e64 | 5937 | |
9b96e234 JM |
5938 | -- Ada 2005 (AI-287): Handle components with default initialization. |
5939 | -- Note: This feature was originally added to Ada 2005 for limited | |
5940 | -- but it was finally allowed with any type. | |
65356e64 | 5941 | |
9b96e234 | 5942 | if Is_Box_Present then |
f91e8020 GD |
5943 | Check_Box_Component : declare |
5944 | Ctyp : constant Entity_Id := Etype (Component); | |
9b96e234 JM |
5945 | |
5946 | begin | |
f7937111 GD |
5947 | -- Initially assume that the box is for a default-initialized |
5948 | -- component and reset to False in cases where that's not true. | |
5949 | ||
5950 | Is_Box_Init_By_Default := True; | |
5951 | ||
c7ce71c2 | 5952 | -- If there is a default expression for the aggregate, copy |
0df7e2d0 AC |
5953 | -- it into a new association. This copy must modify the scopes |
5954 | -- of internal types that may be attached to the expression | |
5955 | -- (e.g. index subtypes of arrays) because in general the type | |
5956 | -- declaration and the aggregate appear in different scopes, | |
5957 | -- and the backend requires the scope of the type to match the | |
5958 | -- point at which it is elaborated. | |
c7ce71c2 | 5959 | |
9b96e234 JM |
5960 | -- If the component has an initialization procedure (IP) we |
5961 | -- pass the component to the expander, which will generate | |
5962 | -- the call to such IP. | |
5963 | ||
c7ce71c2 ES |
5964 | -- If the component has discriminants, their values must |
5965 | -- be taken from their subtype. This is indispensable for | |
5966 | -- constraints that are given by the current instance of an | |
50decc81 RD |
5967 | -- enclosing type, to allow the expansion of the aggregate to |
5968 | -- replace the reference to the current instance by the target | |
5969 | -- object of the aggregate. | |
c7ce71c2 | 5970 | |
e1dfbb03 SB |
5971 | if Is_Case_Choice_Pattern (N) then |
5972 | ||
5973 | -- Do not transform box component values in a case-choice | |
5974 | -- aggregate. | |
5975 | ||
5976 | Add_Association | |
5977 | (Component => Component, | |
07118f48 PT |
5978 | Expr => Empty, |
5979 | Assoc_List => New_Assoc_List, | |
e1dfbb03 SB |
5980 | Is_Box_Present => True); |
5981 | ||
5982 | elsif Present (Parent (Component)) | |
937e9676 | 5983 | and then Nkind (Parent (Component)) = N_Component_Declaration |
c7ce71c2 | 5984 | and then Present (Expression (Parent (Component))) |
aad93b55 | 5985 | then |
f7937111 GD |
5986 | -- If component declaration has an initialization expression |
5987 | -- then this is not a case of default initialization. | |
5988 | ||
5989 | Is_Box_Init_By_Default := False; | |
5990 | ||
c7ce71c2 | 5991 | Expr := |
ba914484 | 5992 | New_Copy_Tree_And_Copy_Dimensions |
2293611f AC |
5993 | (Expression (Parent (Component)), |
5994 | New_Scope => Current_Scope, | |
5995 | New_Sloc => Sloc (N)); | |
c7ce71c2 | 5996 | |
333e4f86 AC |
5997 | -- As the type of the copied default expression may refer |
5998 | -- to discriminants of the record type declaration, these | |
5999 | -- non-stored discriminants need to be rewritten into stored | |
6000 | -- discriminant values for the aggregate. This is required | |
6001 | -- in GNATprove mode, and is adopted in all modes to avoid | |
6002 | -- special-casing GNATprove mode. | |
6003 | ||
6004 | if Is_Array_Type (Etype (Expr)) then | |
6005 | declare | |
13126368 YM |
6006 | Rec_Typ : constant Entity_Id := Scope (Component); |
6007 | -- Root record type whose discriminants may be used as | |
6008 | -- bounds in range nodes. | |
6009 | ||
2c26d262 EB |
6010 | Assoc : Node_Id; |
6011 | Choice : Node_Id; | |
6012 | Index : Node_Id; | |
333e4f86 AC |
6013 | |
6014 | begin | |
6015 | -- Rewrite the range nodes occurring in the indexes | |
6016 | -- and their types. | |
6017 | ||
6018 | Index := First_Index (Etype (Expr)); | |
6019 | while Present (Index) loop | |
13126368 | 6020 | Rewrite_Range (Rec_Typ, Index); |
333e4f86 | 6021 | Rewrite_Range |
13126368 YM |
6022 | (Rec_Typ, Scalar_Range (Etype (Index))); |
6023 | ||
333e4f86 AC |
6024 | Next_Index (Index); |
6025 | end loop; | |
6026 | ||
6027 | -- Rewrite the range nodes occurring as aggregate | |
2c26d262 | 6028 | -- bounds and component associations. |
333e4f86 | 6029 | |
2c26d262 EB |
6030 | if Nkind (Expr) = N_Aggregate then |
6031 | if Present (Aggregate_Bounds (Expr)) then | |
6032 | Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr)); | |
6033 | end if; | |
6034 | ||
6035 | if Present (Component_Associations (Expr)) then | |
6036 | Assoc := First (Component_Associations (Expr)); | |
6037 | while Present (Assoc) loop | |
6038 | Choice := First (Choices (Assoc)); | |
6039 | while Present (Choice) loop | |
6040 | Rewrite_Range (Rec_Typ, Choice); | |
6041 | ||
6042 | Next (Choice); | |
6043 | end loop; | |
6044 | ||
6045 | Next (Assoc); | |
6046 | end loop; | |
6047 | end if; | |
333e4f86 AC |
6048 | end if; |
6049 | end; | |
6050 | end if; | |
6051 | ||
9b96e234 | 6052 | Add_Association |
107b023c AC |
6053 | (Component => Component, |
6054 | Expr => Expr, | |
6055 | Assoc_List => New_Assoc_List); | |
c7ce71c2 ES |
6056 | Set_Has_Self_Reference (N); |
6057 | ||
736f9bed PT |
6058 | elsif Needs_Simple_Initialization (Ctyp) then |
6059 | Add_Association | |
6060 | (Component => Component, | |
6061 | Expr => Empty, | |
6062 | Assoc_List => New_Assoc_List, | |
6063 | Is_Box_Present => True); | |
7610fee8 | 6064 | |
c7ce71c2 ES |
6065 | elsif Has_Non_Null_Base_Init_Proc (Ctyp) |
6066 | or else not Expander_Active | |
6067 | then | |
6068 | if Is_Record_Type (Ctyp) | |
6069 | and then Has_Discriminants (Ctyp) | |
6bde3eb5 | 6070 | and then not Is_Private_Type (Ctyp) |
c7ce71c2 ES |
6071 | then |
6072 | -- We build a partially initialized aggregate with the | |
6073 | -- values of the discriminants and box initialization | |
8133b9d1 | 6074 | -- for the rest, if other components are present. |
c7e152b5 | 6075 | |
51ec70b8 | 6076 | -- The type of the aggregate is the known subtype of |
937e9676 AC |
6077 | -- the component. The capture of discriminants must be |
6078 | -- recursive because subcomponents may be constrained | |
107b023c | 6079 | -- (transitively) by discriminants of enclosing types. |
6bde3eb5 AC |
6080 | -- For a private type with discriminants, a call to the |
6081 | -- initialization procedure will be generated, and no | |
6082 | -- subaggregate is needed. | |
c7ce71c2 | 6083 | |
107b023c | 6084 | Capture_Discriminants : declare |
719aaf4d AC |
6085 | Loc : constant Source_Ptr := Sloc (N); |
6086 | Expr : Node_Id; | |
c7ce71c2 | 6087 | |
107b023c | 6088 | begin |
0ad46f04 | 6089 | Expr := Make_Aggregate (Loc, No_List, New_List); |
107b023c AC |
6090 | Set_Etype (Expr, Ctyp); |
6091 | ||
3786bbd1 RD |
6092 | -- If the enclosing type has discriminants, they have |
6093 | -- been collected in the aggregate earlier, and they | |
6094 | -- may appear as constraints of subcomponents. | |
6095 | ||
107b023c | 6096 | -- Similarly if this component has discriminants, they |
2be0bff8 | 6097 | -- might in turn be propagated to their components. |
107b023c AC |
6098 | |
6099 | if Has_Discriminants (Typ) then | |
6100 | Add_Discriminant_Values (Expr, New_Assoc_List); | |
105b5e65 | 6101 | Propagate_Discriminants (Expr, New_Assoc_List); |
107b023c AC |
6102 | |
6103 | elsif Has_Discriminants (Ctyp) then | |
6104 | Add_Discriminant_Values | |
937e9676 | 6105 | (Expr, Component_Associations (Expr)); |
107b023c | 6106 | Propagate_Discriminants |
937e9676 | 6107 | (Expr, Component_Associations (Expr)); |
107b023c | 6108 | |
fc4c7348 PT |
6109 | Build_Constrained_Itype |
6110 | (Expr, Ctyp, Component_Associations (Expr)); | |
6111 | ||
107b023c AC |
6112 | else |
6113 | declare | |
719aaf4d | 6114 | Comp : Entity_Id; |
107b023c AC |
6115 | |
6116 | begin | |
6117 | -- If the type has additional components, create | |
2be0bff8 | 6118 | -- an OTHERS box association for them. |
107b023c AC |
6119 | |
6120 | Comp := First_Component (Ctyp); | |
6121 | while Present (Comp) loop | |
6122 | if Ekind (Comp) = E_Component then | |
6123 | if not Is_Record_Type (Etype (Comp)) then | |
37368818 RD |
6124 | Append_To |
6125 | (Component_Associations (Expr), | |
6126 | Make_Component_Association (Loc, | |
107b023c | 6127 | Choices => |
58009744 AC |
6128 | New_List ( |
6129 | Make_Others_Choice (Loc)), | |
107b023c | 6130 | Expression => Empty, |
58009744 | 6131 | Box_Present => True)); |
107b023c | 6132 | end if; |
937e9676 | 6133 | |
107b023c AC |
6134 | exit; |
6135 | end if; | |
6136 | ||
6137 | Next_Component (Comp); | |
6138 | end loop; | |
6139 | end; | |
6140 | end if; | |
c7ce71c2 ES |
6141 | |
6142 | Add_Association | |
107b023c AC |
6143 | (Component => Component, |
6144 | Expr => Expr, | |
6145 | Assoc_List => New_Assoc_List); | |
6146 | end Capture_Discriminants; | |
c7ce71c2 | 6147 | |
937e9676 AC |
6148 | -- Otherwise the component type is not a record, or it has |
6149 | -- not discriminants, or it is private. | |
6150 | ||
c7ce71c2 ES |
6151 | else |
6152 | Add_Association | |
6153 | (Component => Component, | |
6154 | Expr => Empty, | |
107b023c | 6155 | Assoc_List => New_Assoc_List, |
c7ce71c2 ES |
6156 | Is_Box_Present => True); |
6157 | end if; | |
9b96e234 JM |
6158 | |
6159 | -- Otherwise we only need to resolve the expression if the | |
6160 | -- component has partially initialized values (required to | |
6161 | -- expand the corresponding assignments and run-time checks). | |
6162 | ||
6163 | elsif Present (Expr) | |
f91e8020 | 6164 | and then Is_Partially_Initialized_Type (Ctyp) |
9b96e234 JM |
6165 | then |
6166 | Resolve_Aggr_Expr (Expr, Component); | |
6167 | end if; | |
f91e8020 | 6168 | end Check_Box_Component; |
615cbd95 | 6169 | |
65356e64 | 6170 | elsif No (Expr) then |
c7ce71c2 ES |
6171 | |
6172 | -- Ignore hidden components associated with the position of the | |
6173 | -- interface tags: these are initialized dynamically. | |
6174 | ||
7d0d27d9 | 6175 | if No (Related_Type (Component)) then |
c7ce71c2 ES |
6176 | Error_Msg_NE |
6177 | ("no value supplied for component &!", N, Component); | |
6178 | end if; | |
615cbd95 | 6179 | |
996ae0b0 RK |
6180 | else |
6181 | Resolve_Aggr_Expr (Expr, Component); | |
6182 | end if; | |
6183 | ||
6184 | Next_Elmt (Component_Elmt); | |
6185 | end loop; | |
6186 | ||
6187 | -- STEP 7: check for invalid components + check type in choice list | |
6188 | ||
6189 | Step_7 : declare | |
937e9676 AC |
6190 | Assoc : Node_Id; |
6191 | New_Assoc : Node_Id; | |
6192 | ||
996ae0b0 RK |
6193 | Selectr : Node_Id; |
6194 | -- Selector name | |
6195 | ||
9b96e234 | 6196 | Typech : Entity_Id; |
996ae0b0 RK |
6197 | -- Type of first component in choice list |
6198 | ||
6199 | begin | |
6200 | if Present (Component_Associations (N)) then | |
6201 | Assoc := First (Component_Associations (N)); | |
6202 | else | |
6203 | Assoc := Empty; | |
6204 | end if; | |
6205 | ||
6206 | Verification : while Present (Assoc) loop | |
6207 | Selectr := First (Choices (Assoc)); | |
6208 | Typech := Empty; | |
6209 | ||
6210 | if Nkind (Selectr) = N_Others_Choice then | |
19f0526a | 6211 | |
9b96e234 | 6212 | -- Ada 2005 (AI-287): others choice may have expression or box |
19f0526a | 6213 | |
ec3c7387 | 6214 | if No (Others_Etype) and then Others_Box = 0 then |
ed2233dc | 6215 | Error_Msg_N |
996ae0b0 | 6216 | ("OTHERS must represent at least one component", Selectr); |
ec3c7387 AC |
6217 | |
6218 | elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then | |
e77a66ee | 6219 | Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node); |
bd717ec9 | 6220 | Error_Msg_N |
e77a66ee | 6221 | ("\previous choices cover all components?r?", Box_Node); |
996ae0b0 RK |
6222 | end if; |
6223 | ||
6224 | exit Verification; | |
6225 | end if; | |
6226 | ||
6227 | while Present (Selectr) loop | |
54783e61 | 6228 | Component := Empty; |
996ae0b0 RK |
6229 | New_Assoc := First (New_Assoc_List); |
6230 | while Present (New_Assoc) loop | |
6231 | Component := First (Choices (New_Assoc)); | |
6989bc1f AC |
6232 | |
6233 | if Chars (Selectr) = Chars (Component) then | |
6234 | if Style_Check then | |
6235 | Check_Identifier (Selectr, Entity (Component)); | |
6236 | end if; | |
6237 | ||
6238 | exit; | |
6239 | end if; | |
6240 | ||
996ae0b0 RK |
6241 | Next (New_Assoc); |
6242 | end loop; | |
6243 | ||
54783e61 YM |
6244 | -- If we found an association, then this is a legal component |
6245 | -- of the type in question. | |
6246 | ||
6247 | pragma Assert (if Present (New_Assoc) then Present (Component)); | |
6248 | ||
c7e152b5 AC |
6249 | -- If no association, this is not a legal component of the type |
6250 | -- in question, unless its association is provided with a box. | |
996ae0b0 RK |
6251 | |
6252 | if No (New_Assoc) then | |
65356e64 | 6253 | if Box_Present (Parent (Selectr)) then |
aad93b55 ES |
6254 | |
6255 | -- This may still be a bogus component with a box. Scan | |
6256 | -- list of components to verify that a component with | |
6257 | -- that name exists. | |
6258 | ||
6259 | declare | |
6260 | C : Entity_Id; | |
6261 | ||
6262 | begin | |
6263 | C := First_Component (Typ); | |
6264 | while Present (C) loop | |
6265 | if Chars (C) = Chars (Selectr) then | |
ca44152f ES |
6266 | |
6267 | -- If the context is an extension aggregate, | |
6268 | -- the component must not be inherited from | |
6269 | -- the ancestor part of the aggregate. | |
6270 | ||
6271 | if Nkind (N) /= N_Extension_Aggregate | |
6272 | or else | |
6273 | Scope (Original_Record_Component (C)) /= | |
937e9676 | 6274 | Etype (Ancestor_Part (N)) |
ca44152f ES |
6275 | then |
6276 | exit; | |
6277 | end if; | |
aad93b55 ES |
6278 | end if; |
6279 | ||
6280 | Next_Component (C); | |
6281 | end loop; | |
6282 | ||
6283 | if No (C) then | |
6284 | Error_Msg_Node_2 := Typ; | |
6285 | Error_Msg_N ("& is not a component of}", Selectr); | |
6286 | end if; | |
6287 | end; | |
996ae0b0 | 6288 | |
65356e64 | 6289 | elsif Chars (Selectr) /= Name_uTag |
996ae0b0 | 6290 | and then Chars (Selectr) /= Name_uParent |
996ae0b0 RK |
6291 | then |
6292 | if not Has_Discriminants (Typ) then | |
6293 | Error_Msg_Node_2 := Typ; | |
aad93b55 | 6294 | Error_Msg_N ("& is not a component of}", Selectr); |
996ae0b0 RK |
6295 | else |
6296 | Error_Msg_N | |
6297 | ("& is not a component of the aggregate subtype", | |
6298 | Selectr); | |
6299 | end if; | |
6300 | ||
6301 | Check_Misspelled_Component (Components, Selectr); | |
6302 | end if; | |
6303 | ||
6304 | elsif No (Typech) then | |
6305 | Typech := Base_Type (Etype (Component)); | |
6306 | ||
feab3549 | 6307 | -- AI05-0199: In Ada 2012, several components of anonymous |
8da337c5 AC |
6308 | -- access types can appear in a choice list, as long as the |
6309 | -- designated types match. | |
6310 | ||
996ae0b0 | 6311 | elsif Typech /= Base_Type (Etype (Component)) then |
dbe945f1 | 6312 | if Ada_Version >= Ada_2012 |
8da337c5 AC |
6313 | and then Ekind (Typech) = E_Anonymous_Access_Type |
6314 | and then | |
6315 | Ekind (Etype (Component)) = E_Anonymous_Access_Type | |
6316 | and then Base_Type (Designated_Type (Typech)) = | |
6317 | Base_Type (Designated_Type (Etype (Component))) | |
6318 | and then | |
6319 | Subtypes_Statically_Match (Typech, (Etype (Component))) | |
6320 | then | |
6321 | null; | |
6322 | ||
6323 | elsif not Box_Present (Parent (Selectr)) then | |
65356e64 AC |
6324 | Error_Msg_N |
6325 | ("components in choice list must have same type", | |
6326 | Selectr); | |
6327 | end if; | |
996ae0b0 RK |
6328 | end if; |
6329 | ||
6330 | Next (Selectr); | |
6331 | end loop; | |
6332 | ||
6333 | Next (Assoc); | |
6334 | end loop Verification; | |
6335 | end Step_7; | |
6336 | ||
6337 | -- STEP 8: replace the original aggregate | |
6338 | ||
6339 | Step_8 : declare | |
fbf5a39b | 6340 | New_Aggregate : constant Node_Id := New_Copy (N); |
996ae0b0 RK |
6341 | |
6342 | begin | |
6343 | Set_Expressions (New_Aggregate, No_List); | |
6344 | Set_Etype (New_Aggregate, Etype (N)); | |
6345 | Set_Component_Associations (New_Aggregate, New_Assoc_List); | |
288cbbbd | 6346 | Set_Check_Actuals (New_Aggregate, Check_Actuals (N)); |
996ae0b0 RK |
6347 | |
6348 | Rewrite (N, New_Aggregate); | |
6349 | end Step_8; | |
0929eaeb | 6350 | |
d976bf74 | 6351 | -- Check the dimensions of the components in the record aggregate |
0929eaeb AC |
6352 | |
6353 | Analyze_Dimension_Extension_Or_Record_Aggregate (N); | |
996ae0b0 RK |
6354 | end Resolve_Record_Aggregate; |
6355 | ||
2820d220 AC |
6356 | ----------------------------- |
6357 | -- Check_Can_Never_Be_Null -- | |
6358 | ----------------------------- | |
6359 | ||
9b96e234 | 6360 | procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is |
ec53a6da JM |
6361 | Comp_Typ : Entity_Id; |
6362 | ||
2820d220 | 6363 | begin |
9b96e234 | 6364 | pragma Assert |
0791fbe9 | 6365 | (Ada_Version >= Ada_2005 |
9b96e234 | 6366 | and then Present (Expr) |
8133b9d1 | 6367 | and then Known_Null (Expr)); |
82c80734 | 6368 | |
ec53a6da JM |
6369 | case Ekind (Typ) is |
6370 | when E_Array_Type => | |
6371 | Comp_Typ := Component_Type (Typ); | |
6372 | ||
d8f43ee6 HK |
6373 | when E_Component |
6374 | | E_Discriminant | |
6375 | => | |
ec53a6da JM |
6376 | Comp_Typ := Etype (Typ); |
6377 | ||
6378 | when others => | |
6379 | return; | |
6380 | end case; | |
6381 | ||
9b96e234 JM |
6382 | if Can_Never_Be_Null (Comp_Typ) then |
6383 | ||
6384 | -- Here we know we have a constraint error. Note that we do not use | |
6385 | -- Apply_Compile_Time_Constraint_Error here to the Expr, which might | |
6386 | -- seem the more natural approach. That's because in some cases the | |
6387 | -- components are rewritten, and the replacement would be missed. | |
5a521b8a AC |
6388 | -- We do not mark the whole aggregate as raising a constraint error, |
6389 | -- because the association may be a null array range. | |
9b96e234 | 6390 | |
5a521b8a | 6391 | Error_Msg_N |
9ed2b86d | 6392 | ("(Ada 2005) NULL not allowed in null-excluding component??", Expr); |
5a521b8a | 6393 | Error_Msg_N |
b785e0b8 | 6394 | ("\Constraint_Error will be raised at run time??", Expr); |
9b96e234 | 6395 | |
5a521b8a AC |
6396 | Rewrite (Expr, |
6397 | Make_Raise_Constraint_Error | |
6398 | (Sloc (Expr), Reason => CE_Access_Check_Failed)); | |
9b96e234 JM |
6399 | Set_Etype (Expr, Comp_Typ); |
6400 | Set_Analyzed (Expr); | |
2820d220 AC |
6401 | end if; |
6402 | end Check_Can_Never_Be_Null; | |
6403 | ||
996ae0b0 RK |
6404 | --------------------- |
6405 | -- Sort_Case_Table -- | |
6406 | --------------------- | |
6407 | ||
6408 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is | |
fbf5a39b | 6409 | U : constant Int := Case_Table'Last; |
996ae0b0 RK |
6410 | K : Int; |
6411 | J : Int; | |
6412 | T : Case_Bounds; | |
6413 | ||
6414 | begin | |
82893775 AC |
6415 | K := 1; |
6416 | while K < U loop | |
996ae0b0 | 6417 | T := Case_Table (K + 1); |
996ae0b0 | 6418 | |
7f9747c6 | 6419 | J := K + 1; |
82893775 AC |
6420 | while J > 1 |
6421 | and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo) | |
996ae0b0 RK |
6422 | loop |
6423 | Case_Table (J) := Case_Table (J - 1); | |
6424 | J := J - 1; | |
6425 | end loop; | |
6426 | ||
6427 | Case_Table (J) := T; | |
6428 | K := K + 1; | |
6429 | end loop; | |
6430 | end Sort_Case_Table; | |
6431 | ||
6432 | end Sem_Aggr; |