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