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