annotate gcc/ada/sem_case.adb @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S E M _ C A S E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1996-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 with Atree; use Atree;
kono
parents:
diff changeset
27 with Einfo; use Einfo;
kono
parents:
diff changeset
28 with Errout; use Errout;
kono
parents:
diff changeset
29 with Namet; use Namet;
kono
parents:
diff changeset
30 with Nlists; use Nlists;
kono
parents:
diff changeset
31 with Nmake; use Nmake;
kono
parents:
diff changeset
32 with Opt; use Opt;
kono
parents:
diff changeset
33 with Sem; use Sem;
kono
parents:
diff changeset
34 with Sem_Aux; use Sem_Aux;
kono
parents:
diff changeset
35 with Sem_Eval; use Sem_Eval;
kono
parents:
diff changeset
36 with Sem_Res; use Sem_Res;
kono
parents:
diff changeset
37 with Sem_Util; use Sem_Util;
kono
parents:
diff changeset
38 with Sem_Type; use Sem_Type;
kono
parents:
diff changeset
39 with Snames; use Snames;
kono
parents:
diff changeset
40 with Stand; use Stand;
kono
parents:
diff changeset
41 with Sinfo; use Sinfo;
kono
parents:
diff changeset
42 with Tbuild; use Tbuild;
kono
parents:
diff changeset
43 with Uintp; use Uintp;
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 with Ada.Unchecked_Deallocation;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 with GNAT.Heap_Sort_G;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 package body Sem_Case is
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 type Choice_Bounds is record
kono
parents:
diff changeset
52 Lo : Node_Id;
kono
parents:
diff changeset
53 Hi : Node_Id;
kono
parents:
diff changeset
54 Node : Node_Id;
kono
parents:
diff changeset
55 end record;
kono
parents:
diff changeset
56 -- Represent one choice bounds entry with Lo and Hi values, Node points
kono
parents:
diff changeset
57 -- to the choice node itself.
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 type Choice_Table_Type is array (Nat range <>) of Choice_Bounds;
kono
parents:
diff changeset
60 -- Table type used to sort the choices present in a case statement or
kono
parents:
diff changeset
61 -- record variant. The actual entries are stored in 1 .. Last, but we
kono
parents:
diff changeset
62 -- have a 0 entry for use in sorting.
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 -----------------------
kono
parents:
diff changeset
65 -- Local Subprograms --
kono
parents:
diff changeset
66 -----------------------
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 procedure Check_Choice_Set
kono
parents:
diff changeset
69 (Choice_Table : in out Choice_Table_Type;
kono
parents:
diff changeset
70 Bounds_Type : Entity_Id;
kono
parents:
diff changeset
71 Subtyp : Entity_Id;
kono
parents:
diff changeset
72 Others_Present : Boolean;
kono
parents:
diff changeset
73 Case_Node : Node_Id);
kono
parents:
diff changeset
74 -- This is the procedure which verifies that a set of case alternatives
kono
parents:
diff changeset
75 -- or record variant choices has no duplicates, and covers the range
kono
parents:
diff changeset
76 -- specified by Bounds_Type. Choice_Table contains the discrete choices
kono
parents:
diff changeset
77 -- to check. These must start at position 1.
kono
parents:
diff changeset
78 --
kono
parents:
diff changeset
79 -- Furthermore Choice_Table (0) must exist. This element is used by
kono
parents:
diff changeset
80 -- the sorting algorithm as a temporary. Others_Present is a flag
kono
parents:
diff changeset
81 -- indicating whether or not an Others choice is present. Finally
kono
parents:
diff changeset
82 -- Msg_Sloc gives the source location of the construct containing the
kono
parents:
diff changeset
83 -- choices in the Choice_Table.
kono
parents:
diff changeset
84 --
kono
parents:
diff changeset
85 -- Bounds_Type is the type whose range must be covered by the alternatives
kono
parents:
diff changeset
86 --
kono
parents:
diff changeset
87 -- Subtyp is the subtype of the expression. If its bounds are non-static
kono
parents:
diff changeset
88 -- the alternatives must cover its base type.
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id;
kono
parents:
diff changeset
91 -- Given a Pos value of enumeration type Ctype, returns the name
kono
parents:
diff changeset
92 -- ID of an appropriate string to be used in error message output.
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 procedure Expand_Others_Choice
kono
parents:
diff changeset
95 (Case_Table : Choice_Table_Type;
kono
parents:
diff changeset
96 Others_Choice : Node_Id;
kono
parents:
diff changeset
97 Choice_Type : Entity_Id);
kono
parents:
diff changeset
98 -- The case table is the table generated by a call to Check_Choices
kono
parents:
diff changeset
99 -- (with just 1 .. Last_Choice entries present). Others_Choice is a
kono
parents:
diff changeset
100 -- pointer to the N_Others_Choice node (this routine is only called if
kono
parents:
diff changeset
101 -- an others choice is present), and Choice_Type is the discrete type
kono
parents:
diff changeset
102 -- of the bounds. The effect of this call is to analyze the cases and
kono
parents:
diff changeset
103 -- determine the set of values covered by others. This choice list is
kono
parents:
diff changeset
104 -- set in the Others_Discrete_Choices field of the N_Others_Choice node.
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 ----------------------
kono
parents:
diff changeset
107 -- Check_Choice_Set --
kono
parents:
diff changeset
108 ----------------------
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 procedure Check_Choice_Set
kono
parents:
diff changeset
111 (Choice_Table : in out Choice_Table_Type;
kono
parents:
diff changeset
112 Bounds_Type : Entity_Id;
kono
parents:
diff changeset
113 Subtyp : Entity_Id;
kono
parents:
diff changeset
114 Others_Present : Boolean;
kono
parents:
diff changeset
115 Case_Node : Node_Id)
kono
parents:
diff changeset
116 is
kono
parents:
diff changeset
117 Predicate_Error : Boolean := False;
kono
parents:
diff changeset
118 -- Flag to prevent cascaded errors when a static predicate is known to
kono
parents:
diff changeset
119 -- be violated by one choice.
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 Num_Choices : constant Nat := Choice_Table'Last;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 procedure Check_Against_Predicate
kono
parents:
diff changeset
124 (Pred : in out Node_Id;
kono
parents:
diff changeset
125 Choice : Choice_Bounds;
kono
parents:
diff changeset
126 Prev_Lo : in out Uint;
kono
parents:
diff changeset
127 Prev_Hi : in out Uint;
kono
parents:
diff changeset
128 Error : in out Boolean);
kono
parents:
diff changeset
129 -- Determine whether a choice covers legal values as defined by a static
kono
parents:
diff changeset
130 -- predicate set. Pred is a static predicate range. Choice is the choice
kono
parents:
diff changeset
131 -- to be examined. Prev_Lo and Prev_Hi are the bounds of the previous
kono
parents:
diff changeset
132 -- choice that covered a predicate set. Error denotes whether the check
kono
parents:
diff changeset
133 -- found an illegal intersection.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 procedure Check_Duplicates;
kono
parents:
diff changeset
136 -- Check for duplicate choices, and call Dup_Choice if there are any
kono
parents:
diff changeset
137 -- such errors. Note that predicates are irrelevant here.
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 procedure Dup_Choice (Lo, Hi : Uint; C : Node_Id);
kono
parents:
diff changeset
140 -- Post message "duplication of choice value(s) bla bla at xx". Message
kono
parents:
diff changeset
141 -- is posted at location C. Caller sets Error_Msg_Sloc for xx.
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 procedure Explain_Non_Static_Bound;
kono
parents:
diff changeset
144 -- Called when we find a non-static bound, requiring the base type to
kono
parents:
diff changeset
145 -- be covered. Provides where possible a helpful explanation of why the
kono
parents:
diff changeset
146 -- bounds are non-static, since this is not always obvious.
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 function Lt_Choice (C1, C2 : Natural) return Boolean;
kono
parents:
diff changeset
149 -- Comparison routine for comparing Choice_Table entries. Use the lower
kono
parents:
diff changeset
150 -- bound of each Choice as the key.
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 procedure Missing_Choice (Value1 : Node_Id; Value2 : Node_Id);
kono
parents:
diff changeset
153 procedure Missing_Choice (Value1 : Node_Id; Value2 : Uint);
kono
parents:
diff changeset
154 procedure Missing_Choice (Value1 : Uint; Value2 : Node_Id);
kono
parents:
diff changeset
155 procedure Missing_Choice (Value1 : Uint; Value2 : Uint);
kono
parents:
diff changeset
156 -- Issue an error message indicating that there are missing choices,
kono
parents:
diff changeset
157 -- followed by the image of the missing choices themselves which lie
kono
parents:
diff changeset
158 -- between Value1 and Value2 inclusive.
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 procedure Missing_Choices (Pred : Node_Id; Prev_Hi : Uint);
kono
parents:
diff changeset
161 -- Emit an error message for each non-covered static predicate set.
kono
parents:
diff changeset
162 -- Prev_Hi denotes the upper bound of the last choice covering a set.
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 procedure Move_Choice (From : Natural; To : Natural);
kono
parents:
diff changeset
165 -- Move routine for sorting the Choice_Table
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 package Sorting is new GNAT.Heap_Sort_G (Move_Choice, Lt_Choice);
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 -----------------------------
kono
parents:
diff changeset
170 -- Check_Against_Predicate --
kono
parents:
diff changeset
171 -----------------------------
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 procedure Check_Against_Predicate
kono
parents:
diff changeset
174 (Pred : in out Node_Id;
kono
parents:
diff changeset
175 Choice : Choice_Bounds;
kono
parents:
diff changeset
176 Prev_Lo : in out Uint;
kono
parents:
diff changeset
177 Prev_Hi : in out Uint;
kono
parents:
diff changeset
178 Error : in out Boolean)
kono
parents:
diff changeset
179 is
kono
parents:
diff changeset
180 procedure Illegal_Range
kono
parents:
diff changeset
181 (Loc : Source_Ptr;
kono
parents:
diff changeset
182 Lo : Uint;
kono
parents:
diff changeset
183 Hi : Uint);
kono
parents:
diff changeset
184 -- Emit an error message regarding a choice that clashes with the
kono
parents:
diff changeset
185 -- legal static predicate sets. Loc is the location of the choice
kono
parents:
diff changeset
186 -- that introduced the illegal range. Lo .. Hi is the range.
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 function Inside_Range
kono
parents:
diff changeset
189 (Lo : Uint;
kono
parents:
diff changeset
190 Hi : Uint;
kono
parents:
diff changeset
191 Val : Uint) return Boolean;
kono
parents:
diff changeset
192 -- Determine whether position Val within a discrete type is within
kono
parents:
diff changeset
193 -- the range Lo .. Hi inclusive.
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 -------------------
kono
parents:
diff changeset
196 -- Illegal_Range --
kono
parents:
diff changeset
197 -------------------
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 procedure Illegal_Range
kono
parents:
diff changeset
200 (Loc : Source_Ptr;
kono
parents:
diff changeset
201 Lo : Uint;
kono
parents:
diff changeset
202 Hi : Uint)
kono
parents:
diff changeset
203 is
kono
parents:
diff changeset
204 begin
kono
parents:
diff changeset
205 Error_Msg_Name_1 := Chars (Bounds_Type);
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 -- Single value
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 if Lo = Hi then
kono
parents:
diff changeset
210 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
211 Error_Msg_Uint_1 := Lo;
kono
parents:
diff changeset
212 Error_Msg ("static predicate on % excludes value ^!", Loc);
kono
parents:
diff changeset
213 else
kono
parents:
diff changeset
214 Error_Msg_Name_2 := Choice_Image (Lo, Bounds_Type);
kono
parents:
diff changeset
215 Error_Msg ("static predicate on % excludes value %!", Loc);
kono
parents:
diff changeset
216 end if;
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 -- Range
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 else
kono
parents:
diff changeset
221 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
222 Error_Msg_Uint_1 := Lo;
kono
parents:
diff changeset
223 Error_Msg_Uint_2 := Hi;
kono
parents:
diff changeset
224 Error_Msg
kono
parents:
diff changeset
225 ("static predicate on % excludes range ^ .. ^!", Loc);
kono
parents:
diff changeset
226 else
kono
parents:
diff changeset
227 Error_Msg_Name_2 := Choice_Image (Lo, Bounds_Type);
kono
parents:
diff changeset
228 Error_Msg_Name_3 := Choice_Image (Hi, Bounds_Type);
kono
parents:
diff changeset
229 Error_Msg
kono
parents:
diff changeset
230 ("static predicate on % excludes range % .. %!", Loc);
kono
parents:
diff changeset
231 end if;
kono
parents:
diff changeset
232 end if;
kono
parents:
diff changeset
233 end Illegal_Range;
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 ------------------
kono
parents:
diff changeset
236 -- Inside_Range --
kono
parents:
diff changeset
237 ------------------
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 function Inside_Range
kono
parents:
diff changeset
240 (Lo : Uint;
kono
parents:
diff changeset
241 Hi : Uint;
kono
parents:
diff changeset
242 Val : Uint) return Boolean
kono
parents:
diff changeset
243 is
kono
parents:
diff changeset
244 begin
kono
parents:
diff changeset
245 return Lo <= Val and then Val <= Hi;
kono
parents:
diff changeset
246 end Inside_Range;
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 -- Local variables
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 Choice_Hi : constant Uint := Expr_Value (Choice.Hi);
kono
parents:
diff changeset
251 Choice_Lo : constant Uint := Expr_Value (Choice.Lo);
kono
parents:
diff changeset
252 Loc : Source_Ptr;
kono
parents:
diff changeset
253 LocN : Node_Id;
kono
parents:
diff changeset
254 Next_Hi : Uint;
kono
parents:
diff changeset
255 Next_Lo : Uint;
kono
parents:
diff changeset
256 Pred_Hi : Uint;
kono
parents:
diff changeset
257 Pred_Lo : Uint;
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 -- Start of processing for Check_Against_Predicate
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 begin
kono
parents:
diff changeset
262 -- Find the proper error message location
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 if Present (Choice.Node) then
kono
parents:
diff changeset
265 LocN := Choice.Node;
kono
parents:
diff changeset
266 else
kono
parents:
diff changeset
267 LocN := Case_Node;
kono
parents:
diff changeset
268 end if;
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 Loc := Sloc (LocN);
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 if Present (Pred) then
kono
parents:
diff changeset
273 Pred_Lo := Expr_Value (Low_Bound (Pred));
kono
parents:
diff changeset
274 Pred_Hi := Expr_Value (High_Bound (Pred));
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 -- Previous choices managed to satisfy all static predicate sets
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 else
kono
parents:
diff changeset
279 Illegal_Range (Loc, Choice_Lo, Choice_Hi);
kono
parents:
diff changeset
280 Error := True;
kono
parents:
diff changeset
281 return;
kono
parents:
diff changeset
282 end if;
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 -- Step 1: Ignore duplicate choices, other than to set the flag,
kono
parents:
diff changeset
285 -- because these were already detected by Check_Duplicates.
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 if Inside_Range (Choice_Lo, Choice_Hi, Prev_Lo)
kono
parents:
diff changeset
288 or else Inside_Range (Choice_Lo, Choice_Hi, Prev_Hi)
kono
parents:
diff changeset
289 then
kono
parents:
diff changeset
290 Error := True;
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 -- Step 2: Detect full coverage
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 -- Choice_Lo Choice_Hi
kono
parents:
diff changeset
295 -- +============+
kono
parents:
diff changeset
296 -- Pred_Lo Pred_Hi
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 elsif Choice_Lo = Pred_Lo and then Choice_Hi = Pred_Hi then
kono
parents:
diff changeset
299 Prev_Lo := Choice_Lo;
kono
parents:
diff changeset
300 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
301 Next (Pred);
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 -- Step 3: Detect all cases where a choice mentions values that are
kono
parents:
diff changeset
304 -- not part of the static predicate sets.
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 -- Choice_Lo Choice_Hi Pred_Lo Pred_Hi
kono
parents:
diff changeset
307 -- +-----------+ . . . . . +=========+
kono
parents:
diff changeset
308 -- ^ illegal ^
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 elsif Choice_Lo < Pred_Lo and then Choice_Hi < Pred_Lo then
kono
parents:
diff changeset
311 Illegal_Range (Loc, Choice_Lo, Choice_Hi);
kono
parents:
diff changeset
312 Error := True;
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 -- Choice_Lo Pred_Lo Choice_Hi Pred_Hi
kono
parents:
diff changeset
315 -- +-----------+=========+===========+
kono
parents:
diff changeset
316 -- ^ illegal ^
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 elsif Choice_Lo < Pred_Lo
kono
parents:
diff changeset
319 and then Inside_Range (Pred_Lo, Pred_Hi, Choice_Hi)
kono
parents:
diff changeset
320 then
kono
parents:
diff changeset
321 Illegal_Range (Loc, Choice_Lo, Pred_Lo - 1);
kono
parents:
diff changeset
322 Error := True;
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 -- Pred_Lo Pred_Hi Choice_Lo Choice_Hi
kono
parents:
diff changeset
325 -- +=========+ . . . . +-----------+
kono
parents:
diff changeset
326 -- ^ illegal ^
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 elsif Pred_Lo < Choice_Lo and then Pred_Hi < Choice_Lo then
kono
parents:
diff changeset
329 if Others_Present then
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 -- Current predicate set is covered by others clause.
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 null;
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 else
kono
parents:
diff changeset
336 Missing_Choice (Pred_Lo, Pred_Hi);
kono
parents:
diff changeset
337 Error := True;
kono
parents:
diff changeset
338 end if;
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 -- There may be several static predicate sets between the current
kono
parents:
diff changeset
341 -- one and the choice. Inspect the next static predicate set.
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 Next (Pred);
kono
parents:
diff changeset
344 Check_Against_Predicate
kono
parents:
diff changeset
345 (Pred => Pred,
kono
parents:
diff changeset
346 Choice => Choice,
kono
parents:
diff changeset
347 Prev_Lo => Prev_Lo,
kono
parents:
diff changeset
348 Prev_Hi => Prev_Hi,
kono
parents:
diff changeset
349 Error => Error);
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 -- Pred_Lo Choice_Lo Pred_Hi Choice_Hi
kono
parents:
diff changeset
352 -- +=========+===========+-----------+
kono
parents:
diff changeset
353 -- ^ illegal ^
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 elsif Pred_Hi < Choice_Hi
kono
parents:
diff changeset
356 and then Inside_Range (Pred_Lo, Pred_Hi, Choice_Lo)
kono
parents:
diff changeset
357 then
kono
parents:
diff changeset
358 Next (Pred);
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 -- The choice may fall in a static predicate set. If this is the
kono
parents:
diff changeset
361 -- case, avoid mentioning legal values in the error message.
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 if Present (Pred) then
kono
parents:
diff changeset
364 Next_Lo := Expr_Value (Low_Bound (Pred));
kono
parents:
diff changeset
365 Next_Hi := Expr_Value (High_Bound (Pred));
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 -- The next static predicate set is to the right of the choice
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 if Choice_Hi < Next_Lo and then Choice_Hi < Next_Hi then
kono
parents:
diff changeset
370 Illegal_Range (Loc, Pred_Hi + 1, Choice_Hi);
kono
parents:
diff changeset
371 else
kono
parents:
diff changeset
372 Illegal_Range (Loc, Pred_Hi + 1, Next_Lo - 1);
kono
parents:
diff changeset
373 end if;
kono
parents:
diff changeset
374 else
kono
parents:
diff changeset
375 Illegal_Range (Loc, Pred_Hi + 1, Choice_Hi);
kono
parents:
diff changeset
376 end if;
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 Error := True;
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 -- Choice_Lo Pred_Lo Pred_Hi Choice_Hi
kono
parents:
diff changeset
381 -- +-----------+=========+-----------+
kono
parents:
diff changeset
382 -- ^ illegal ^ ^ illegal ^
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 -- Emit an error on the low gap, disregard the upper gap
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 elsif Choice_Lo < Pred_Lo and then Pred_Hi < Choice_Hi then
kono
parents:
diff changeset
387 Illegal_Range (Loc, Choice_Lo, Pred_Lo - 1);
kono
parents:
diff changeset
388 Error := True;
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 -- Step 4: Detect all cases of partial or missing coverage
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 -- Pred_Lo Choice_Lo Choice_Hi Pred_Hi
kono
parents:
diff changeset
393 -- +=========+==========+===========+
kono
parents:
diff changeset
394 -- ^ gap ^ ^ gap ^
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 else
kono
parents:
diff changeset
397 -- An "others" choice covers all gaps
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 if Others_Present then
kono
parents:
diff changeset
400 Prev_Lo := Choice_Lo;
kono
parents:
diff changeset
401 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 -- Check whether predicate set is fully covered by choice
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 if Pred_Hi = Choice_Hi then
kono
parents:
diff changeset
406 Next (Pred);
kono
parents:
diff changeset
407 end if;
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 -- Choice_Lo Choice_Hi Pred_Hi
kono
parents:
diff changeset
410 -- +===========+===========+
kono
parents:
diff changeset
411 -- Pred_Lo ^ gap ^
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 -- The upper gap may be covered by a subsequent choice
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 elsif Pred_Lo = Choice_Lo then
kono
parents:
diff changeset
416 Prev_Lo := Choice_Lo;
kono
parents:
diff changeset
417 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 -- Pred_Lo Prev_Hi Choice_Lo Choice_Hi Pred_Hi
kono
parents:
diff changeset
420 -- +===========+=========+===========+===========+
kono
parents:
diff changeset
421 -- ^ covered ^ ^ gap ^
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 else pragma Assert (Pred_Lo < Choice_Lo);
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 -- A previous choice covered the gap up to the current choice
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 if Prev_Hi = Choice_Lo - 1 then
kono
parents:
diff changeset
428 Prev_Lo := Choice_Lo;
kono
parents:
diff changeset
429 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 if Choice_Hi = Pred_Hi then
kono
parents:
diff changeset
432 Next (Pred);
kono
parents:
diff changeset
433 end if;
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 -- The previous choice did not intersect with the current
kono
parents:
diff changeset
436 -- static predicate set.
kono
parents:
diff changeset
437
kono
parents:
diff changeset
438 elsif Prev_Hi < Pred_Lo then
kono
parents:
diff changeset
439 Missing_Choice (Pred_Lo, Choice_Lo - 1);
kono
parents:
diff changeset
440 Error := True;
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 -- The previous choice covered part of the static predicate set
kono
parents:
diff changeset
443 -- but there is a gap after Prev_Hi.
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 else
kono
parents:
diff changeset
446 Missing_Choice (Prev_Hi + 1, Choice_Lo - 1);
kono
parents:
diff changeset
447 Error := True;
kono
parents:
diff changeset
448 end if;
kono
parents:
diff changeset
449 end if;
kono
parents:
diff changeset
450 end if;
kono
parents:
diff changeset
451 end Check_Against_Predicate;
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 ----------------------
kono
parents:
diff changeset
454 -- Check_Duplicates --
kono
parents:
diff changeset
455 ----------------------
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 procedure Check_Duplicates is
kono
parents:
diff changeset
458 Choice : Node_Id;
kono
parents:
diff changeset
459 Choice_Hi : Uint;
kono
parents:
diff changeset
460 Choice_Lo : Uint;
kono
parents:
diff changeset
461 Prev_Choice : Node_Id;
kono
parents:
diff changeset
462 pragma Warnings (Off, Prev_Choice);
kono
parents:
diff changeset
463 Prev_Hi : Uint;
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 begin
kono
parents:
diff changeset
466 Prev_Hi := Expr_Value (Choice_Table (1).Hi);
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 for Outer_Index in 2 .. Num_Choices loop
kono
parents:
diff changeset
469 Choice_Lo := Expr_Value (Choice_Table (Outer_Index).Lo);
kono
parents:
diff changeset
470 Choice_Hi := Expr_Value (Choice_Table (Outer_Index).Hi);
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 -- Choices overlap; this is an error
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 if Choice_Lo <= Prev_Hi then
kono
parents:
diff changeset
475 Choice := Choice_Table (Outer_Index).Node;
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 -- Find first previous choice that overlaps
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 for Inner_Index in 1 .. Outer_Index - 1 loop
kono
parents:
diff changeset
480 if Choice_Lo <=
kono
parents:
diff changeset
481 Expr_Value (Choice_Table (Inner_Index).Hi)
kono
parents:
diff changeset
482 then
kono
parents:
diff changeset
483 Prev_Choice := Choice_Table (Inner_Index).Node;
kono
parents:
diff changeset
484 exit;
kono
parents:
diff changeset
485 end if;
kono
parents:
diff changeset
486 end loop;
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 if Sloc (Prev_Choice) <= Sloc (Choice) then
kono
parents:
diff changeset
489 Error_Msg_Sloc := Sloc (Prev_Choice);
kono
parents:
diff changeset
490 Dup_Choice (Choice_Lo, UI_Min (Choice_Hi, Prev_Hi), Choice);
kono
parents:
diff changeset
491 else
kono
parents:
diff changeset
492 Error_Msg_Sloc := Sloc (Choice);
kono
parents:
diff changeset
493 Dup_Choice
kono
parents:
diff changeset
494 (Choice_Lo, UI_Min (Choice_Hi, Prev_Hi), Prev_Choice);
kono
parents:
diff changeset
495 end if;
kono
parents:
diff changeset
496 end if;
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 if Choice_Hi > Prev_Hi then
kono
parents:
diff changeset
499 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
500 end if;
kono
parents:
diff changeset
501 end loop;
kono
parents:
diff changeset
502 end Check_Duplicates;
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 ----------------
kono
parents:
diff changeset
505 -- Dup_Choice --
kono
parents:
diff changeset
506 ----------------
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 procedure Dup_Choice (Lo, Hi : Uint; C : Node_Id) is
kono
parents:
diff changeset
509 begin
kono
parents:
diff changeset
510 -- In some situations, we call this with a null range, and obviously
kono
parents:
diff changeset
511 -- we don't want to complain in this case.
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 if Lo > Hi then
kono
parents:
diff changeset
514 return;
kono
parents:
diff changeset
515 end if;
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 -- Case of only one value that is duplicated
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 if Lo = Hi then
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 -- Integer type
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 -- We have an integer value, Lo, but if the given choice
kono
parents:
diff changeset
526 -- placement is a constant with that value, then use the
kono
parents:
diff changeset
527 -- name of that constant instead in the message:
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 if Nkind (C) = N_Identifier
kono
parents:
diff changeset
530 and then Compile_Time_Known_Value (C)
kono
parents:
diff changeset
531 and then Expr_Value (C) = Lo
kono
parents:
diff changeset
532 then
kono
parents:
diff changeset
533 Error_Msg_N ("duplication of choice value: &#!", C);
kono
parents:
diff changeset
534
kono
parents:
diff changeset
535 -- Not that special case, so just output the integer value
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 else
kono
parents:
diff changeset
538 Error_Msg_Uint_1 := Lo;
kono
parents:
diff changeset
539 Error_Msg_N ("duplication of choice value: ^#!", C);
kono
parents:
diff changeset
540 end if;
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 -- Enumeration type
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 else
kono
parents:
diff changeset
545 Error_Msg_Name_1 := Choice_Image (Lo, Bounds_Type);
kono
parents:
diff changeset
546 Error_Msg_N ("duplication of choice value: %#!", C);
kono
parents:
diff changeset
547 end if;
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 -- More than one choice value, so print range of values
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 else
kono
parents:
diff changeset
552 -- Integer type
kono
parents:
diff changeset
553
kono
parents:
diff changeset
554 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
555
kono
parents:
diff changeset
556 -- Similar to the above, if C is a range of known values which
kono
parents:
diff changeset
557 -- match Lo and Hi, then use the names. We have to go to the
kono
parents:
diff changeset
558 -- original nodes, since the values will have been rewritten
kono
parents:
diff changeset
559 -- to their integer values.
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 if Nkind (C) = N_Range
kono
parents:
diff changeset
562 and then Nkind (Original_Node (Low_Bound (C))) = N_Identifier
kono
parents:
diff changeset
563 and then Nkind (Original_Node (High_Bound (C))) = N_Identifier
kono
parents:
diff changeset
564 and then Compile_Time_Known_Value (Low_Bound (C))
kono
parents:
diff changeset
565 and then Compile_Time_Known_Value (High_Bound (C))
kono
parents:
diff changeset
566 and then Expr_Value (Low_Bound (C)) = Lo
kono
parents:
diff changeset
567 and then Expr_Value (High_Bound (C)) = Hi
kono
parents:
diff changeset
568 then
kono
parents:
diff changeset
569 Error_Msg_Node_2 := Original_Node (High_Bound (C));
kono
parents:
diff changeset
570 Error_Msg_N
kono
parents:
diff changeset
571 ("duplication of choice values: & .. &#!",
kono
parents:
diff changeset
572 Original_Node (Low_Bound (C)));
kono
parents:
diff changeset
573
kono
parents:
diff changeset
574 -- Not that special case, output integer values
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 else
kono
parents:
diff changeset
577 Error_Msg_Uint_1 := Lo;
kono
parents:
diff changeset
578 Error_Msg_Uint_2 := Hi;
kono
parents:
diff changeset
579 Error_Msg_N ("duplication of choice values: ^ .. ^#!", C);
kono
parents:
diff changeset
580 end if;
kono
parents:
diff changeset
581
kono
parents:
diff changeset
582 -- Enumeration type
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 else
kono
parents:
diff changeset
585 Error_Msg_Name_1 := Choice_Image (Lo, Bounds_Type);
kono
parents:
diff changeset
586 Error_Msg_Name_2 := Choice_Image (Hi, Bounds_Type);
kono
parents:
diff changeset
587 Error_Msg_N ("duplication of choice values: % .. %#!", C);
kono
parents:
diff changeset
588 end if;
kono
parents:
diff changeset
589 end if;
kono
parents:
diff changeset
590 end Dup_Choice;
kono
parents:
diff changeset
591
kono
parents:
diff changeset
592 ------------------------------
kono
parents:
diff changeset
593 -- Explain_Non_Static_Bound --
kono
parents:
diff changeset
594 ------------------------------
kono
parents:
diff changeset
595
kono
parents:
diff changeset
596 procedure Explain_Non_Static_Bound is
kono
parents:
diff changeset
597 Expr : Node_Id;
kono
parents:
diff changeset
598
kono
parents:
diff changeset
599 begin
kono
parents:
diff changeset
600 if Nkind (Case_Node) = N_Variant_Part then
kono
parents:
diff changeset
601 Expr := Name (Case_Node);
kono
parents:
diff changeset
602 else
kono
parents:
diff changeset
603 Expr := Expression (Case_Node);
kono
parents:
diff changeset
604 end if;
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 if Bounds_Type /= Subtyp then
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 -- If the case is a variant part, the expression is given by the
kono
parents:
diff changeset
609 -- discriminant itself, and the bounds are the culprits.
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 if Nkind (Case_Node) = N_Variant_Part then
kono
parents:
diff changeset
612 Error_Msg_NE
kono
parents:
diff changeset
613 ("bounds of & are not static, "
kono
parents:
diff changeset
614 & "alternatives must cover base type!", Expr, Expr);
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 -- If this is a case statement, the expression may be non-static
kono
parents:
diff changeset
617 -- or else the subtype may be at fault.
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 elsif Is_Entity_Name (Expr) then
kono
parents:
diff changeset
620 Error_Msg_NE
kono
parents:
diff changeset
621 ("bounds of & are not static, "
kono
parents:
diff changeset
622 & "alternatives must cover base type!", Expr, Expr);
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 else
kono
parents:
diff changeset
625 Error_Msg_N
kono
parents:
diff changeset
626 ("subtype of expression is not static, "
kono
parents:
diff changeset
627 & "alternatives must cover base type!", Expr);
kono
parents:
diff changeset
628 end if;
kono
parents:
diff changeset
629
kono
parents:
diff changeset
630 -- Otherwise the expression is not static, even if the bounds of the
kono
parents:
diff changeset
631 -- type are, or else there are missing alternatives. If both, the
kono
parents:
diff changeset
632 -- additional information may be redundant but harmless. Examine
kono
parents:
diff changeset
633 -- whether original node is an entity, because it may have been
kono
parents:
diff changeset
634 -- constant-folded to a literal if value is known.
kono
parents:
diff changeset
635
kono
parents:
diff changeset
636 elsif not Is_Entity_Name (Original_Node (Expr)) then
kono
parents:
diff changeset
637 Error_Msg_N
kono
parents:
diff changeset
638 ("subtype of expression is not static, "
kono
parents:
diff changeset
639 & "alternatives must cover base type!", Expr);
kono
parents:
diff changeset
640 end if;
kono
parents:
diff changeset
641 end Explain_Non_Static_Bound;
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 ---------------
kono
parents:
diff changeset
644 -- Lt_Choice --
kono
parents:
diff changeset
645 ---------------
kono
parents:
diff changeset
646
kono
parents:
diff changeset
647 function Lt_Choice (C1, C2 : Natural) return Boolean is
kono
parents:
diff changeset
648 begin
kono
parents:
diff changeset
649 return
kono
parents:
diff changeset
650 Expr_Value (Choice_Table (Nat (C1)).Lo)
kono
parents:
diff changeset
651 <
kono
parents:
diff changeset
652 Expr_Value (Choice_Table (Nat (C2)).Lo);
kono
parents:
diff changeset
653 end Lt_Choice;
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 --------------------
kono
parents:
diff changeset
656 -- Missing_Choice --
kono
parents:
diff changeset
657 --------------------
kono
parents:
diff changeset
658
kono
parents:
diff changeset
659 procedure Missing_Choice (Value1 : Node_Id; Value2 : Node_Id) is
kono
parents:
diff changeset
660 begin
kono
parents:
diff changeset
661 Missing_Choice (Expr_Value (Value1), Expr_Value (Value2));
kono
parents:
diff changeset
662 end Missing_Choice;
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 procedure Missing_Choice (Value1 : Node_Id; Value2 : Uint) is
kono
parents:
diff changeset
665 begin
kono
parents:
diff changeset
666 Missing_Choice (Expr_Value (Value1), Value2);
kono
parents:
diff changeset
667 end Missing_Choice;
kono
parents:
diff changeset
668
kono
parents:
diff changeset
669 procedure Missing_Choice (Value1 : Uint; Value2 : Node_Id) is
kono
parents:
diff changeset
670 begin
kono
parents:
diff changeset
671 Missing_Choice (Value1, Expr_Value (Value2));
kono
parents:
diff changeset
672 end Missing_Choice;
kono
parents:
diff changeset
673
kono
parents:
diff changeset
674 --------------------
kono
parents:
diff changeset
675 -- Missing_Choice --
kono
parents:
diff changeset
676 --------------------
kono
parents:
diff changeset
677
kono
parents:
diff changeset
678 procedure Missing_Choice (Value1 : Uint; Value2 : Uint) is
kono
parents:
diff changeset
679 Msg_Sloc : constant Source_Ptr := Sloc (Case_Node);
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 begin
kono
parents:
diff changeset
682 -- AI05-0188 : within an instance the non-others choices do not have
kono
parents:
diff changeset
683 -- to belong to the actual subtype.
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 if Ada_Version >= Ada_2012 and then In_Instance then
kono
parents:
diff changeset
686 return;
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 -- In some situations, we call this with a null range, and obviously
kono
parents:
diff changeset
689 -- we don't want to complain in this case.
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 elsif Value1 > Value2 then
kono
parents:
diff changeset
692 return;
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 -- If predicate is already known to be violated, do no check for
kono
parents:
diff changeset
695 -- coverage error, to prevent cascaded messages.
kono
parents:
diff changeset
696
kono
parents:
diff changeset
697 elsif Predicate_Error then
kono
parents:
diff changeset
698 return;
kono
parents:
diff changeset
699 end if;
kono
parents:
diff changeset
700
kono
parents:
diff changeset
701 -- Case of only one value that is missing
kono
parents:
diff changeset
702
kono
parents:
diff changeset
703 if Value1 = Value2 then
kono
parents:
diff changeset
704 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
705 Error_Msg_Uint_1 := Value1;
kono
parents:
diff changeset
706 Error_Msg ("missing case value: ^!", Msg_Sloc);
kono
parents:
diff changeset
707 else
kono
parents:
diff changeset
708 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
kono
parents:
diff changeset
709 Error_Msg ("missing case value: %!", Msg_Sloc);
kono
parents:
diff changeset
710 end if;
kono
parents:
diff changeset
711
kono
parents:
diff changeset
712 -- More than one choice value, so print range of values
kono
parents:
diff changeset
713
kono
parents:
diff changeset
714 else
kono
parents:
diff changeset
715 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
716 Error_Msg_Uint_1 := Value1;
kono
parents:
diff changeset
717 Error_Msg_Uint_2 := Value2;
kono
parents:
diff changeset
718 Error_Msg ("missing case values: ^ .. ^!", Msg_Sloc);
kono
parents:
diff changeset
719 else
kono
parents:
diff changeset
720 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
kono
parents:
diff changeset
721 Error_Msg_Name_2 := Choice_Image (Value2, Bounds_Type);
kono
parents:
diff changeset
722 Error_Msg ("missing case values: % .. %!", Msg_Sloc);
kono
parents:
diff changeset
723 end if;
kono
parents:
diff changeset
724 end if;
kono
parents:
diff changeset
725 end Missing_Choice;
kono
parents:
diff changeset
726
kono
parents:
diff changeset
727 ---------------------
kono
parents:
diff changeset
728 -- Missing_Choices --
kono
parents:
diff changeset
729 ---------------------
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 procedure Missing_Choices (Pred : Node_Id; Prev_Hi : Uint) is
kono
parents:
diff changeset
732 Hi : Uint;
kono
parents:
diff changeset
733 Lo : Uint;
kono
parents:
diff changeset
734 Set : Node_Id;
kono
parents:
diff changeset
735
kono
parents:
diff changeset
736 begin
kono
parents:
diff changeset
737 Set := Pred;
kono
parents:
diff changeset
738 while Present (Set) loop
kono
parents:
diff changeset
739 Lo := Expr_Value (Low_Bound (Set));
kono
parents:
diff changeset
740 Hi := Expr_Value (High_Bound (Set));
kono
parents:
diff changeset
741
kono
parents:
diff changeset
742 -- A choice covered part of a static predicate set
kono
parents:
diff changeset
743
kono
parents:
diff changeset
744 if Lo <= Prev_Hi and then Prev_Hi < Hi then
kono
parents:
diff changeset
745 Missing_Choice (Prev_Hi + 1, Hi);
kono
parents:
diff changeset
746
kono
parents:
diff changeset
747 else
kono
parents:
diff changeset
748 Missing_Choice (Lo, Hi);
kono
parents:
diff changeset
749 end if;
kono
parents:
diff changeset
750
kono
parents:
diff changeset
751 Next (Set);
kono
parents:
diff changeset
752 end loop;
kono
parents:
diff changeset
753 end Missing_Choices;
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 -----------------
kono
parents:
diff changeset
756 -- Move_Choice --
kono
parents:
diff changeset
757 -----------------
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 procedure Move_Choice (From : Natural; To : Natural) is
kono
parents:
diff changeset
760 begin
kono
parents:
diff changeset
761 Choice_Table (Nat (To)) := Choice_Table (Nat (From));
kono
parents:
diff changeset
762 end Move_Choice;
kono
parents:
diff changeset
763
kono
parents:
diff changeset
764 -- Local variables
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 Bounds_Hi : constant Node_Id := Type_High_Bound (Bounds_Type);
kono
parents:
diff changeset
767 Bounds_Lo : constant Node_Id := Type_Low_Bound (Bounds_Type);
kono
parents:
diff changeset
768 Has_Predicate : constant Boolean :=
kono
parents:
diff changeset
769 Is_OK_Static_Subtype (Bounds_Type)
kono
parents:
diff changeset
770 and then Has_Static_Predicate (Bounds_Type);
kono
parents:
diff changeset
771
kono
parents:
diff changeset
772 Choice_Hi : Uint;
kono
parents:
diff changeset
773 Choice_Lo : Uint;
kono
parents:
diff changeset
774 Pred : Node_Id;
kono
parents:
diff changeset
775 Prev_Lo : Uint;
kono
parents:
diff changeset
776 Prev_Hi : Uint;
kono
parents:
diff changeset
777
kono
parents:
diff changeset
778 -- Start of processing for Check_Choice_Set
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 begin
kono
parents:
diff changeset
781 -- If the case is part of a predicate aspect specification, do not
kono
parents:
diff changeset
782 -- recheck it against itself.
kono
parents:
diff changeset
783
kono
parents:
diff changeset
784 if Present (Parent (Case_Node))
kono
parents:
diff changeset
785 and then Nkind (Parent (Case_Node)) = N_Aspect_Specification
kono
parents:
diff changeset
786 then
kono
parents:
diff changeset
787 return;
kono
parents:
diff changeset
788 end if;
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 -- Choice_Table must start at 0 which is an unused location used by the
kono
parents:
diff changeset
791 -- sorting algorithm. However the first valid position for a discrete
kono
parents:
diff changeset
792 -- choice is 1.
kono
parents:
diff changeset
793
kono
parents:
diff changeset
794 pragma Assert (Choice_Table'First = 0);
kono
parents:
diff changeset
795
kono
parents:
diff changeset
796 -- The choices do not cover the base range. Emit an error if "others" is
kono
parents:
diff changeset
797 -- not available and return as there is no need for further processing.
kono
parents:
diff changeset
798
kono
parents:
diff changeset
799 if Num_Choices = 0 then
kono
parents:
diff changeset
800 if not Others_Present then
kono
parents:
diff changeset
801 Missing_Choice (Bounds_Lo, Bounds_Hi);
kono
parents:
diff changeset
802 end if;
kono
parents:
diff changeset
803
kono
parents:
diff changeset
804 return;
kono
parents:
diff changeset
805 end if;
kono
parents:
diff changeset
806
kono
parents:
diff changeset
807 Sorting.Sort (Positive (Choice_Table'Last));
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 -- First check for duplicates. This involved the choices; predicates, if
kono
parents:
diff changeset
810 -- any, are irrelevant.
kono
parents:
diff changeset
811
kono
parents:
diff changeset
812 Check_Duplicates;
kono
parents:
diff changeset
813
kono
parents:
diff changeset
814 -- Then check for overlaps
kono
parents:
diff changeset
815
kono
parents:
diff changeset
816 -- If the subtype has a static predicate, the predicate defines subsets
kono
parents:
diff changeset
817 -- of legal values and requires finer-grained analysis.
kono
parents:
diff changeset
818
kono
parents:
diff changeset
819 -- Note that in GNAT the predicate is considered static if the predicate
kono
parents:
diff changeset
820 -- expression is static, independently of whether the aspect mentions
kono
parents:
diff changeset
821 -- Static explicitly.
kono
parents:
diff changeset
822
kono
parents:
diff changeset
823 if Has_Predicate then
kono
parents:
diff changeset
824 Pred := First (Static_Discrete_Predicate (Bounds_Type));
kono
parents:
diff changeset
825
kono
parents:
diff changeset
826 -- Make initial value smaller than 'First of type, so that first
kono
parents:
diff changeset
827 -- range comparison succeeds. This applies both to integer types
kono
parents:
diff changeset
828 -- and to enumeration types.
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 Prev_Lo := Expr_Value (Type_Low_Bound (Bounds_Type)) - 1;
kono
parents:
diff changeset
831 Prev_Hi := Prev_Lo;
kono
parents:
diff changeset
832
kono
parents:
diff changeset
833 declare
kono
parents:
diff changeset
834 Error : Boolean := False;
kono
parents:
diff changeset
835 begin
kono
parents:
diff changeset
836 for Index in 1 .. Num_Choices loop
kono
parents:
diff changeset
837 Check_Against_Predicate
kono
parents:
diff changeset
838 (Pred => Pred,
kono
parents:
diff changeset
839 Choice => Choice_Table (Index),
kono
parents:
diff changeset
840 Prev_Lo => Prev_Lo,
kono
parents:
diff changeset
841 Prev_Hi => Prev_Hi,
kono
parents:
diff changeset
842 Error => Error);
kono
parents:
diff changeset
843
kono
parents:
diff changeset
844 -- The analysis detected an illegal intersection between a
kono
parents:
diff changeset
845 -- choice and a static predicate set. Do not examine other
kono
parents:
diff changeset
846 -- choices unless all errors are requested.
kono
parents:
diff changeset
847
kono
parents:
diff changeset
848 if Error then
kono
parents:
diff changeset
849 Predicate_Error := True;
kono
parents:
diff changeset
850
kono
parents:
diff changeset
851 if not All_Errors_Mode then
kono
parents:
diff changeset
852 return;
kono
parents:
diff changeset
853 end if;
kono
parents:
diff changeset
854 end if;
kono
parents:
diff changeset
855 end loop;
kono
parents:
diff changeset
856 end;
kono
parents:
diff changeset
857
kono
parents:
diff changeset
858 if Predicate_Error then
kono
parents:
diff changeset
859 return;
kono
parents:
diff changeset
860 end if;
kono
parents:
diff changeset
861
kono
parents:
diff changeset
862 -- The choices may legally cover some of the static predicate sets,
kono
parents:
diff changeset
863 -- but not all. Emit an error for each non-covered set.
kono
parents:
diff changeset
864
kono
parents:
diff changeset
865 if not Others_Present then
kono
parents:
diff changeset
866 Missing_Choices (Pred, Prev_Hi);
kono
parents:
diff changeset
867 end if;
kono
parents:
diff changeset
868
kono
parents:
diff changeset
869 -- Default analysis
kono
parents:
diff changeset
870
kono
parents:
diff changeset
871 else
kono
parents:
diff changeset
872 Choice_Lo := Expr_Value (Choice_Table (1).Lo);
kono
parents:
diff changeset
873 Choice_Hi := Expr_Value (Choice_Table (1).Hi);
kono
parents:
diff changeset
874 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
875
kono
parents:
diff changeset
876 if not Others_Present and then Expr_Value (Bounds_Lo) < Choice_Lo then
kono
parents:
diff changeset
877 Missing_Choice (Bounds_Lo, Choice_Lo - 1);
kono
parents:
diff changeset
878
kono
parents:
diff changeset
879 -- If values are missing outside of the subtype, add explanation.
kono
parents:
diff changeset
880 -- No additional message if only one value is missing.
kono
parents:
diff changeset
881
kono
parents:
diff changeset
882 if Expr_Value (Bounds_Lo) < Choice_Lo - 1 then
kono
parents:
diff changeset
883 Explain_Non_Static_Bound;
kono
parents:
diff changeset
884 end if;
kono
parents:
diff changeset
885 end if;
kono
parents:
diff changeset
886
kono
parents:
diff changeset
887 for Index in 2 .. Num_Choices loop
kono
parents:
diff changeset
888 Choice_Lo := Expr_Value (Choice_Table (Index).Lo);
kono
parents:
diff changeset
889 Choice_Hi := Expr_Value (Choice_Table (Index).Hi);
kono
parents:
diff changeset
890
kono
parents:
diff changeset
891 if Choice_Lo > Prev_Hi + 1 and then not Others_Present then
kono
parents:
diff changeset
892 Missing_Choice (Prev_Hi + 1, Choice_Lo - 1);
kono
parents:
diff changeset
893 end if;
kono
parents:
diff changeset
894
kono
parents:
diff changeset
895 if Choice_Hi > Prev_Hi then
kono
parents:
diff changeset
896 Prev_Hi := Choice_Hi;
kono
parents:
diff changeset
897 end if;
kono
parents:
diff changeset
898 end loop;
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 if not Others_Present and then Expr_Value (Bounds_Hi) > Prev_Hi then
kono
parents:
diff changeset
901 Missing_Choice (Prev_Hi + 1, Bounds_Hi);
kono
parents:
diff changeset
902
kono
parents:
diff changeset
903 if Expr_Value (Bounds_Hi) > Prev_Hi + 1 then
kono
parents:
diff changeset
904 Explain_Non_Static_Bound;
kono
parents:
diff changeset
905 end if;
kono
parents:
diff changeset
906 end if;
kono
parents:
diff changeset
907 end if;
kono
parents:
diff changeset
908 end Check_Choice_Set;
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 ------------------
kono
parents:
diff changeset
911 -- Choice_Image --
kono
parents:
diff changeset
912 ------------------
kono
parents:
diff changeset
913
kono
parents:
diff changeset
914 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id is
kono
parents:
diff changeset
915 Rtp : constant Entity_Id := Root_Type (Ctype);
kono
parents:
diff changeset
916 Lit : Entity_Id;
kono
parents:
diff changeset
917 C : Int;
kono
parents:
diff changeset
918
kono
parents:
diff changeset
919 begin
kono
parents:
diff changeset
920 -- For character, or wide [wide] character. If 7-bit ASCII graphic
kono
parents:
diff changeset
921 -- range, then build and return appropriate character literal name
kono
parents:
diff changeset
922
kono
parents:
diff changeset
923 if Is_Standard_Character_Type (Ctype) then
kono
parents:
diff changeset
924 C := UI_To_Int (Value);
kono
parents:
diff changeset
925
kono
parents:
diff changeset
926 if C in 16#20# .. 16#7E# then
kono
parents:
diff changeset
927 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
kono
parents:
diff changeset
928 return Name_Find;
kono
parents:
diff changeset
929 end if;
kono
parents:
diff changeset
930
kono
parents:
diff changeset
931 -- For user defined enumeration type, find enum/char literal
kono
parents:
diff changeset
932
kono
parents:
diff changeset
933 else
kono
parents:
diff changeset
934 Lit := First_Literal (Rtp);
kono
parents:
diff changeset
935
kono
parents:
diff changeset
936 for J in 1 .. UI_To_Int (Value) loop
kono
parents:
diff changeset
937 Next_Literal (Lit);
kono
parents:
diff changeset
938 end loop;
kono
parents:
diff changeset
939
kono
parents:
diff changeset
940 -- If enumeration literal, just return its value
kono
parents:
diff changeset
941
kono
parents:
diff changeset
942 if Nkind (Lit) = N_Defining_Identifier then
kono
parents:
diff changeset
943 return Chars (Lit);
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 -- For character literal, get the name and use it if it is
kono
parents:
diff changeset
946 -- for a 7-bit ASCII graphic character in 16#20#..16#7E#.
kono
parents:
diff changeset
947
kono
parents:
diff changeset
948 else
kono
parents:
diff changeset
949 Get_Decoded_Name_String (Chars (Lit));
kono
parents:
diff changeset
950
kono
parents:
diff changeset
951 if Name_Len = 3
kono
parents:
diff changeset
952 and then Name_Buffer (2) in
kono
parents:
diff changeset
953 Character'Val (16#20#) .. Character'Val (16#7E#)
kono
parents:
diff changeset
954 then
kono
parents:
diff changeset
955 return Chars (Lit);
kono
parents:
diff changeset
956 end if;
kono
parents:
diff changeset
957 end if;
kono
parents:
diff changeset
958 end if;
kono
parents:
diff changeset
959
kono
parents:
diff changeset
960 -- If we fall through, we have a character literal which is not in
kono
parents:
diff changeset
961 -- the 7-bit ASCII graphic set. For such cases, we construct the
kono
parents:
diff changeset
962 -- name "type'val(nnn)" where type is the choice type, and nnn is
kono
parents:
diff changeset
963 -- the pos value passed as an argument to Choice_Image.
kono
parents:
diff changeset
964
kono
parents:
diff changeset
965 Get_Name_String (Chars (First_Subtype (Ctype)));
kono
parents:
diff changeset
966
kono
parents:
diff changeset
967 Add_Str_To_Name_Buffer ("'val(");
kono
parents:
diff changeset
968 UI_Image (Value);
kono
parents:
diff changeset
969 Add_Str_To_Name_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
kono
parents:
diff changeset
970 Add_Char_To_Name_Buffer (')');
kono
parents:
diff changeset
971 return Name_Find;
kono
parents:
diff changeset
972 end Choice_Image;
kono
parents:
diff changeset
973
kono
parents:
diff changeset
974 --------------------------
kono
parents:
diff changeset
975 -- Expand_Others_Choice --
kono
parents:
diff changeset
976 --------------------------
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978 procedure Expand_Others_Choice
kono
parents:
diff changeset
979 (Case_Table : Choice_Table_Type;
kono
parents:
diff changeset
980 Others_Choice : Node_Id;
kono
parents:
diff changeset
981 Choice_Type : Entity_Id)
kono
parents:
diff changeset
982 is
kono
parents:
diff changeset
983 Loc : constant Source_Ptr := Sloc (Others_Choice);
kono
parents:
diff changeset
984 Choice_List : constant List_Id := New_List;
kono
parents:
diff changeset
985 Choice : Node_Id;
kono
parents:
diff changeset
986 Exp_Lo : Node_Id;
kono
parents:
diff changeset
987 Exp_Hi : Node_Id;
kono
parents:
diff changeset
988 Hi : Uint;
kono
parents:
diff changeset
989 Lo : Uint;
kono
parents:
diff changeset
990 Previous_Hi : Uint;
kono
parents:
diff changeset
991
kono
parents:
diff changeset
992 function Build_Choice (Value1, Value2 : Uint) return Node_Id;
kono
parents:
diff changeset
993 -- Builds a node representing the missing choices given by Value1 and
kono
parents:
diff changeset
994 -- Value2. A N_Range node is built if there is more than one literal
kono
parents:
diff changeset
995 -- value missing. Otherwise a single N_Integer_Literal, N_Identifier
kono
parents:
diff changeset
996 -- or N_Character_Literal is built depending on what Choice_Type is.
kono
parents:
diff changeset
997
kono
parents:
diff changeset
998 function Lit_Of (Value : Uint) return Node_Id;
kono
parents:
diff changeset
999 -- Returns the Node_Id for the enumeration literal corresponding to the
kono
parents:
diff changeset
1000 -- position given by Value within the enumeration type Choice_Type.
kono
parents:
diff changeset
1001
kono
parents:
diff changeset
1002 ------------------
kono
parents:
diff changeset
1003 -- Build_Choice --
kono
parents:
diff changeset
1004 ------------------
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 function Build_Choice (Value1, Value2 : Uint) return Node_Id is
kono
parents:
diff changeset
1007 Lit_Node : Node_Id;
kono
parents:
diff changeset
1008 Lo, Hi : Node_Id;
kono
parents:
diff changeset
1009
kono
parents:
diff changeset
1010 begin
kono
parents:
diff changeset
1011 -- If there is only one choice value missing between Value1 and
kono
parents:
diff changeset
1012 -- Value2, build an integer or enumeration literal to represent it.
kono
parents:
diff changeset
1013
kono
parents:
diff changeset
1014 if (Value2 - Value1) = 0 then
kono
parents:
diff changeset
1015 if Is_Integer_Type (Choice_Type) then
kono
parents:
diff changeset
1016 Lit_Node := Make_Integer_Literal (Loc, Value1);
kono
parents:
diff changeset
1017 Set_Etype (Lit_Node, Choice_Type);
kono
parents:
diff changeset
1018 else
kono
parents:
diff changeset
1019 Lit_Node := Lit_Of (Value1);
kono
parents:
diff changeset
1020 end if;
kono
parents:
diff changeset
1021
kono
parents:
diff changeset
1022 -- Otherwise is more that one choice value that is missing between
kono
parents:
diff changeset
1023 -- Value1 and Value2, therefore build a N_Range node of either
kono
parents:
diff changeset
1024 -- integer or enumeration literals.
kono
parents:
diff changeset
1025
kono
parents:
diff changeset
1026 else
kono
parents:
diff changeset
1027 if Is_Integer_Type (Choice_Type) then
kono
parents:
diff changeset
1028 Lo := Make_Integer_Literal (Loc, Value1);
kono
parents:
diff changeset
1029 Set_Etype (Lo, Choice_Type);
kono
parents:
diff changeset
1030 Hi := Make_Integer_Literal (Loc, Value2);
kono
parents:
diff changeset
1031 Set_Etype (Hi, Choice_Type);
kono
parents:
diff changeset
1032 Lit_Node :=
kono
parents:
diff changeset
1033 Make_Range (Loc,
kono
parents:
diff changeset
1034 Low_Bound => Lo,
kono
parents:
diff changeset
1035 High_Bound => Hi);
kono
parents:
diff changeset
1036
kono
parents:
diff changeset
1037 else
kono
parents:
diff changeset
1038 Lit_Node :=
kono
parents:
diff changeset
1039 Make_Range (Loc,
kono
parents:
diff changeset
1040 Low_Bound => Lit_Of (Value1),
kono
parents:
diff changeset
1041 High_Bound => Lit_Of (Value2));
kono
parents:
diff changeset
1042 end if;
kono
parents:
diff changeset
1043 end if;
kono
parents:
diff changeset
1044
kono
parents:
diff changeset
1045 return Lit_Node;
kono
parents:
diff changeset
1046 end Build_Choice;
kono
parents:
diff changeset
1047
kono
parents:
diff changeset
1048 ------------
kono
parents:
diff changeset
1049 -- Lit_Of --
kono
parents:
diff changeset
1050 ------------
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 function Lit_Of (Value : Uint) return Node_Id is
kono
parents:
diff changeset
1053 Lit : Entity_Id;
kono
parents:
diff changeset
1054
kono
parents:
diff changeset
1055 begin
kono
parents:
diff changeset
1056 -- In the case where the literal is of type Character, there needs
kono
parents:
diff changeset
1057 -- to be some special handling since there is no explicit chain
kono
parents:
diff changeset
1058 -- of literals to search. Instead, a N_Character_Literal node
kono
parents:
diff changeset
1059 -- is created with the appropriate Char_Code and Chars fields.
kono
parents:
diff changeset
1060
kono
parents:
diff changeset
1061 if Is_Standard_Character_Type (Choice_Type) then
kono
parents:
diff changeset
1062 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
kono
parents:
diff changeset
1063 Lit := New_Node (N_Character_Literal, Loc);
kono
parents:
diff changeset
1064 Set_Chars (Lit, Name_Find);
kono
parents:
diff changeset
1065 Set_Char_Literal_Value (Lit, Value);
kono
parents:
diff changeset
1066 Set_Etype (Lit, Choice_Type);
kono
parents:
diff changeset
1067 Set_Is_Static_Expression (Lit, True);
kono
parents:
diff changeset
1068 return Lit;
kono
parents:
diff changeset
1069
kono
parents:
diff changeset
1070 -- Otherwise, iterate through the literals list of Choice_Type
kono
parents:
diff changeset
1071 -- "Value" number of times until the desired literal is reached
kono
parents:
diff changeset
1072 -- and then return an occurrence of it.
kono
parents:
diff changeset
1073
kono
parents:
diff changeset
1074 else
kono
parents:
diff changeset
1075 Lit := First_Literal (Choice_Type);
kono
parents:
diff changeset
1076 for J in 1 .. UI_To_Int (Value) loop
kono
parents:
diff changeset
1077 Next_Literal (Lit);
kono
parents:
diff changeset
1078 end loop;
kono
parents:
diff changeset
1079
kono
parents:
diff changeset
1080 return New_Occurrence_Of (Lit, Loc);
kono
parents:
diff changeset
1081 end if;
kono
parents:
diff changeset
1082 end Lit_Of;
kono
parents:
diff changeset
1083
kono
parents:
diff changeset
1084 -- Start of processing for Expand_Others_Choice
kono
parents:
diff changeset
1085
kono
parents:
diff changeset
1086 begin
kono
parents:
diff changeset
1087 if Case_Table'Last = 0 then
kono
parents:
diff changeset
1088
kono
parents:
diff changeset
1089 -- Special case: only an others case is present. The others case
kono
parents:
diff changeset
1090 -- covers the full range of the type.
kono
parents:
diff changeset
1091
kono
parents:
diff changeset
1092 if Is_OK_Static_Subtype (Choice_Type) then
kono
parents:
diff changeset
1093 Choice := New_Occurrence_Of (Choice_Type, Loc);
kono
parents:
diff changeset
1094 else
kono
parents:
diff changeset
1095 Choice := New_Occurrence_Of (Base_Type (Choice_Type), Loc);
kono
parents:
diff changeset
1096 end if;
kono
parents:
diff changeset
1097
kono
parents:
diff changeset
1098 Set_Others_Discrete_Choices (Others_Choice, New_List (Choice));
kono
parents:
diff changeset
1099 return;
kono
parents:
diff changeset
1100 end if;
kono
parents:
diff changeset
1101
kono
parents:
diff changeset
1102 -- Establish the bound values for the choice depending upon whether the
kono
parents:
diff changeset
1103 -- type of the case statement is static or not.
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 if Is_OK_Static_Subtype (Choice_Type) then
kono
parents:
diff changeset
1106 Exp_Lo := Type_Low_Bound (Choice_Type);
kono
parents:
diff changeset
1107 Exp_Hi := Type_High_Bound (Choice_Type);
kono
parents:
diff changeset
1108 else
kono
parents:
diff changeset
1109 Exp_Lo := Type_Low_Bound (Base_Type (Choice_Type));
kono
parents:
diff changeset
1110 Exp_Hi := Type_High_Bound (Base_Type (Choice_Type));
kono
parents:
diff changeset
1111 end if;
kono
parents:
diff changeset
1112
kono
parents:
diff changeset
1113 Lo := Expr_Value (Case_Table (1).Lo);
kono
parents:
diff changeset
1114 Hi := Expr_Value (Case_Table (1).Hi);
kono
parents:
diff changeset
1115 Previous_Hi := Expr_Value (Case_Table (1).Hi);
kono
parents:
diff changeset
1116
kono
parents:
diff changeset
1117 -- Build the node for any missing choices that are smaller than any
kono
parents:
diff changeset
1118 -- explicit choices given in the case.
kono
parents:
diff changeset
1119
kono
parents:
diff changeset
1120 if Expr_Value (Exp_Lo) < Lo then
kono
parents:
diff changeset
1121 Append (Build_Choice (Expr_Value (Exp_Lo), Lo - 1), Choice_List);
kono
parents:
diff changeset
1122 end if;
kono
parents:
diff changeset
1123
kono
parents:
diff changeset
1124 -- Build the nodes representing any missing choices that lie between
kono
parents:
diff changeset
1125 -- the explicit ones given in the case.
kono
parents:
diff changeset
1126
kono
parents:
diff changeset
1127 for J in 2 .. Case_Table'Last loop
kono
parents:
diff changeset
1128 Lo := Expr_Value (Case_Table (J).Lo);
kono
parents:
diff changeset
1129 Hi := Expr_Value (Case_Table (J).Hi);
kono
parents:
diff changeset
1130
kono
parents:
diff changeset
1131 if Lo /= (Previous_Hi + 1) then
kono
parents:
diff changeset
1132 Append_To (Choice_List, Build_Choice (Previous_Hi + 1, Lo - 1));
kono
parents:
diff changeset
1133 end if;
kono
parents:
diff changeset
1134
kono
parents:
diff changeset
1135 Previous_Hi := Hi;
kono
parents:
diff changeset
1136 end loop;
kono
parents:
diff changeset
1137
kono
parents:
diff changeset
1138 -- Build the node for any missing choices that are greater than any
kono
parents:
diff changeset
1139 -- explicit choices given in the case.
kono
parents:
diff changeset
1140
kono
parents:
diff changeset
1141 if Expr_Value (Exp_Hi) > Hi then
kono
parents:
diff changeset
1142 Append (Build_Choice (Hi + 1, Expr_Value (Exp_Hi)), Choice_List);
kono
parents:
diff changeset
1143 end if;
kono
parents:
diff changeset
1144
kono
parents:
diff changeset
1145 Set_Others_Discrete_Choices (Others_Choice, Choice_List);
kono
parents:
diff changeset
1146
kono
parents:
diff changeset
1147 -- Warn on null others list if warning option set
kono
parents:
diff changeset
1148
kono
parents:
diff changeset
1149 if Warn_On_Redundant_Constructs
kono
parents:
diff changeset
1150 and then Comes_From_Source (Others_Choice)
kono
parents:
diff changeset
1151 and then Is_Empty_List (Choice_List)
kono
parents:
diff changeset
1152 then
kono
parents:
diff changeset
1153 Error_Msg_N ("?r?OTHERS choice is redundant", Others_Choice);
kono
parents:
diff changeset
1154 Error_Msg_N ("\?r?previous choices cover all values", Others_Choice);
kono
parents:
diff changeset
1155 end if;
kono
parents:
diff changeset
1156 end Expand_Others_Choice;
kono
parents:
diff changeset
1157
kono
parents:
diff changeset
1158 -----------
kono
parents:
diff changeset
1159 -- No_OP --
kono
parents:
diff changeset
1160 -----------
kono
parents:
diff changeset
1161
kono
parents:
diff changeset
1162 procedure No_OP (C : Node_Id) is
kono
parents:
diff changeset
1163 begin
kono
parents:
diff changeset
1164 if Nkind (C) = N_Range and then Warn_On_Redundant_Constructs then
kono
parents:
diff changeset
1165 Error_Msg_N ("choice is an empty range?r?", C);
kono
parents:
diff changeset
1166 end if;
kono
parents:
diff changeset
1167 end No_OP;
kono
parents:
diff changeset
1168
kono
parents:
diff changeset
1169 -----------------------------
kono
parents:
diff changeset
1170 -- Generic_Analyze_Choices --
kono
parents:
diff changeset
1171 -----------------------------
kono
parents:
diff changeset
1172
kono
parents:
diff changeset
1173 package body Generic_Analyze_Choices is
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 -- The following type is used to gather the entries for the choice
kono
parents:
diff changeset
1176 -- table, so that we can then allocate the right length.
kono
parents:
diff changeset
1177
kono
parents:
diff changeset
1178 type Link;
kono
parents:
diff changeset
1179 type Link_Ptr is access all Link;
kono
parents:
diff changeset
1180
kono
parents:
diff changeset
1181 type Link is record
kono
parents:
diff changeset
1182 Val : Choice_Bounds;
kono
parents:
diff changeset
1183 Nxt : Link_Ptr;
kono
parents:
diff changeset
1184 end record;
kono
parents:
diff changeset
1185
kono
parents:
diff changeset
1186 ---------------------
kono
parents:
diff changeset
1187 -- Analyze_Choices --
kono
parents:
diff changeset
1188 ---------------------
kono
parents:
diff changeset
1189
kono
parents:
diff changeset
1190 procedure Analyze_Choices
kono
parents:
diff changeset
1191 (Alternatives : List_Id;
kono
parents:
diff changeset
1192 Subtyp : Entity_Id)
kono
parents:
diff changeset
1193 is
kono
parents:
diff changeset
1194 Choice_Type : constant Entity_Id := Base_Type (Subtyp);
kono
parents:
diff changeset
1195 -- The actual type against which the discrete choices are resolved.
kono
parents:
diff changeset
1196 -- Note that this type is always the base type not the subtype of the
kono
parents:
diff changeset
1197 -- ruling expression, index or discriminant.
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 Expected_Type : Entity_Id;
kono
parents:
diff changeset
1200 -- The expected type of each choice. Equal to Choice_Type, except if
kono
parents:
diff changeset
1201 -- the expression is universal, in which case the choices can be of
kono
parents:
diff changeset
1202 -- any integer type.
kono
parents:
diff changeset
1203
kono
parents:
diff changeset
1204 Alt : Node_Id;
kono
parents:
diff changeset
1205 -- A case statement alternative or a variant in a record type
kono
parents:
diff changeset
1206 -- declaration.
kono
parents:
diff changeset
1207
kono
parents:
diff changeset
1208 Choice : Node_Id;
kono
parents:
diff changeset
1209 Kind : Node_Kind;
kono
parents:
diff changeset
1210 -- The node kind of the current Choice
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 begin
kono
parents:
diff changeset
1213 -- Set Expected type (= choice type except for universal integer,
kono
parents:
diff changeset
1214 -- where we accept any integer type as a choice).
kono
parents:
diff changeset
1215
kono
parents:
diff changeset
1216 if Choice_Type = Universal_Integer then
kono
parents:
diff changeset
1217 Expected_Type := Any_Integer;
kono
parents:
diff changeset
1218 else
kono
parents:
diff changeset
1219 Expected_Type := Choice_Type;
kono
parents:
diff changeset
1220 end if;
kono
parents:
diff changeset
1221
kono
parents:
diff changeset
1222 -- Now loop through the case alternatives or record variants
kono
parents:
diff changeset
1223
kono
parents:
diff changeset
1224 Alt := First (Alternatives);
kono
parents:
diff changeset
1225 while Present (Alt) loop
kono
parents:
diff changeset
1226
kono
parents:
diff changeset
1227 -- If pragma, just analyze it
kono
parents:
diff changeset
1228
kono
parents:
diff changeset
1229 if Nkind (Alt) = N_Pragma then
kono
parents:
diff changeset
1230 Analyze (Alt);
kono
parents:
diff changeset
1231
kono
parents:
diff changeset
1232 -- Otherwise we have an alternative. In most cases the semantic
kono
parents:
diff changeset
1233 -- processing leaves the list of choices unchanged
kono
parents:
diff changeset
1234
kono
parents:
diff changeset
1235 -- Check each choice against its base type
kono
parents:
diff changeset
1236
kono
parents:
diff changeset
1237 else
kono
parents:
diff changeset
1238 Choice := First (Discrete_Choices (Alt));
kono
parents:
diff changeset
1239 while Present (Choice) loop
kono
parents:
diff changeset
1240 Analyze (Choice);
kono
parents:
diff changeset
1241 Kind := Nkind (Choice);
kono
parents:
diff changeset
1242
kono
parents:
diff changeset
1243 -- Choice is a Range
kono
parents:
diff changeset
1244
kono
parents:
diff changeset
1245 if Kind = N_Range
kono
parents:
diff changeset
1246 or else (Kind = N_Attribute_Reference
kono
parents:
diff changeset
1247 and then Attribute_Name (Choice) = Name_Range)
kono
parents:
diff changeset
1248 then
kono
parents:
diff changeset
1249 Resolve (Choice, Expected_Type);
kono
parents:
diff changeset
1250
kono
parents:
diff changeset
1251 -- Choice is a subtype name, nothing further to do now
kono
parents:
diff changeset
1252
kono
parents:
diff changeset
1253 elsif Is_Entity_Name (Choice)
kono
parents:
diff changeset
1254 and then Is_Type (Entity (Choice))
kono
parents:
diff changeset
1255 then
kono
parents:
diff changeset
1256 null;
kono
parents:
diff changeset
1257
kono
parents:
diff changeset
1258 -- Choice is a subtype indication
kono
parents:
diff changeset
1259
kono
parents:
diff changeset
1260 elsif Kind = N_Subtype_Indication then
kono
parents:
diff changeset
1261 Resolve_Discrete_Subtype_Indication
kono
parents:
diff changeset
1262 (Choice, Expected_Type);
kono
parents:
diff changeset
1263
kono
parents:
diff changeset
1264 -- Others choice, no analysis needed
kono
parents:
diff changeset
1265
kono
parents:
diff changeset
1266 elsif Kind = N_Others_Choice then
kono
parents:
diff changeset
1267 null;
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 -- Only other possibility is an expression
kono
parents:
diff changeset
1270
kono
parents:
diff changeset
1271 else
kono
parents:
diff changeset
1272 Resolve (Choice, Expected_Type);
kono
parents:
diff changeset
1273 end if;
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 -- Move to next choice
kono
parents:
diff changeset
1276
kono
parents:
diff changeset
1277 Next (Choice);
kono
parents:
diff changeset
1278 end loop;
kono
parents:
diff changeset
1279
kono
parents:
diff changeset
1280 Process_Associated_Node (Alt);
kono
parents:
diff changeset
1281 end if;
kono
parents:
diff changeset
1282
kono
parents:
diff changeset
1283 Next (Alt);
kono
parents:
diff changeset
1284 end loop;
kono
parents:
diff changeset
1285 end Analyze_Choices;
kono
parents:
diff changeset
1286
kono
parents:
diff changeset
1287 end Generic_Analyze_Choices;
kono
parents:
diff changeset
1288
kono
parents:
diff changeset
1289 ---------------------------
kono
parents:
diff changeset
1290 -- Generic_Check_Choices --
kono
parents:
diff changeset
1291 ---------------------------
kono
parents:
diff changeset
1292
kono
parents:
diff changeset
1293 package body Generic_Check_Choices is
kono
parents:
diff changeset
1294
kono
parents:
diff changeset
1295 -- The following type is used to gather the entries for the choice
kono
parents:
diff changeset
1296 -- table, so that we can then allocate the right length.
kono
parents:
diff changeset
1297
kono
parents:
diff changeset
1298 type Link;
kono
parents:
diff changeset
1299 type Link_Ptr is access all Link;
kono
parents:
diff changeset
1300
kono
parents:
diff changeset
1301 type Link is record
kono
parents:
diff changeset
1302 Val : Choice_Bounds;
kono
parents:
diff changeset
1303 Nxt : Link_Ptr;
kono
parents:
diff changeset
1304 end record;
kono
parents:
diff changeset
1305
kono
parents:
diff changeset
1306 procedure Free is new Ada.Unchecked_Deallocation (Link, Link_Ptr);
kono
parents:
diff changeset
1307
kono
parents:
diff changeset
1308 -------------------
kono
parents:
diff changeset
1309 -- Check_Choices --
kono
parents:
diff changeset
1310 -------------------
kono
parents:
diff changeset
1311
kono
parents:
diff changeset
1312 procedure Check_Choices
kono
parents:
diff changeset
1313 (N : Node_Id;
kono
parents:
diff changeset
1314 Alternatives : List_Id;
kono
parents:
diff changeset
1315 Subtyp : Entity_Id;
kono
parents:
diff changeset
1316 Others_Present : out Boolean)
kono
parents:
diff changeset
1317 is
kono
parents:
diff changeset
1318 E : Entity_Id;
kono
parents:
diff changeset
1319
kono
parents:
diff changeset
1320 Raises_CE : Boolean;
kono
parents:
diff changeset
1321 -- Set True if one of the bounds of a choice raises CE
kono
parents:
diff changeset
1322
kono
parents:
diff changeset
1323 Enode : Node_Id;
kono
parents:
diff changeset
1324 -- This is where we post error messages for bounds out of range
kono
parents:
diff changeset
1325
kono
parents:
diff changeset
1326 Choice_List : Link_Ptr := null;
kono
parents:
diff changeset
1327 -- Gather list of choices
kono
parents:
diff changeset
1328
kono
parents:
diff changeset
1329 Num_Choices : Nat := 0;
kono
parents:
diff changeset
1330 -- Number of entries in Choice_List
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 Choice_Type : constant Entity_Id := Base_Type (Subtyp);
kono
parents:
diff changeset
1333 -- The actual type against which the discrete choices are resolved.
kono
parents:
diff changeset
1334 -- Note that this type is always the base type not the subtype of the
kono
parents:
diff changeset
1335 -- ruling expression, index or discriminant.
kono
parents:
diff changeset
1336
kono
parents:
diff changeset
1337 Bounds_Type : Entity_Id;
kono
parents:
diff changeset
1338 -- The type from which are derived the bounds of the values covered
kono
parents:
diff changeset
1339 -- by the discrete choices (see 3.8.1 (4)). If a discrete choice
kono
parents:
diff changeset
1340 -- specifies a value outside of these bounds we have an error.
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 Bounds_Lo : Uint;
kono
parents:
diff changeset
1343 Bounds_Hi : Uint;
kono
parents:
diff changeset
1344 -- The actual bounds of the above type
kono
parents:
diff changeset
1345
kono
parents:
diff changeset
1346 Expected_Type : Entity_Id;
kono
parents:
diff changeset
1347 -- The expected type of each choice. Equal to Choice_Type, except if
kono
parents:
diff changeset
1348 -- the expression is universal, in which case the choices can be of
kono
parents:
diff changeset
1349 -- any integer type.
kono
parents:
diff changeset
1350
kono
parents:
diff changeset
1351 Alt : Node_Id;
kono
parents:
diff changeset
1352 -- A case statement alternative or a variant in a record type
kono
parents:
diff changeset
1353 -- declaration.
kono
parents:
diff changeset
1354
kono
parents:
diff changeset
1355 Choice : Node_Id;
kono
parents:
diff changeset
1356 Kind : Node_Kind;
kono
parents:
diff changeset
1357 -- The node kind of the current Choice
kono
parents:
diff changeset
1358
kono
parents:
diff changeset
1359 Others_Choice : Node_Id := Empty;
kono
parents:
diff changeset
1360 -- Remember others choice if it is present (empty otherwise)
kono
parents:
diff changeset
1361
kono
parents:
diff changeset
1362 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id);
kono
parents:
diff changeset
1363 -- Checks the validity of the bounds of a choice. When the bounds
kono
parents:
diff changeset
1364 -- are static and no error occurred the bounds are collected for
kono
parents:
diff changeset
1365 -- later entry into the choices table so that they can be sorted
kono
parents:
diff changeset
1366 -- later on.
kono
parents:
diff changeset
1367
kono
parents:
diff changeset
1368 procedure Handle_Static_Predicate
kono
parents:
diff changeset
1369 (Typ : Entity_Id;
kono
parents:
diff changeset
1370 Lo : Node_Id;
kono
parents:
diff changeset
1371 Hi : Node_Id);
kono
parents:
diff changeset
1372 -- If the type of the alternative has predicates, we must examine
kono
parents:
diff changeset
1373 -- each subset of the predicate rather than the bounds of the type
kono
parents:
diff changeset
1374 -- itself. This is relevant when the choice is a subtype mark or a
kono
parents:
diff changeset
1375 -- subtype indication.
kono
parents:
diff changeset
1376
kono
parents:
diff changeset
1377 -----------
kono
parents:
diff changeset
1378 -- Check --
kono
parents:
diff changeset
1379 -----------
kono
parents:
diff changeset
1380
kono
parents:
diff changeset
1381 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id) is
kono
parents:
diff changeset
1382 Lo_Val : Uint;
kono
parents:
diff changeset
1383 Hi_Val : Uint;
kono
parents:
diff changeset
1384
kono
parents:
diff changeset
1385 begin
kono
parents:
diff changeset
1386 -- First check if an error was already detected on either bounds
kono
parents:
diff changeset
1387
kono
parents:
diff changeset
1388 if Etype (Lo) = Any_Type or else Etype (Hi) = Any_Type then
kono
parents:
diff changeset
1389 return;
kono
parents:
diff changeset
1390
kono
parents:
diff changeset
1391 -- Do not insert non static choices in the table to be sorted
kono
parents:
diff changeset
1392
kono
parents:
diff changeset
1393 elsif not Is_OK_Static_Expression (Lo)
kono
parents:
diff changeset
1394 or else
kono
parents:
diff changeset
1395 not Is_OK_Static_Expression (Hi)
kono
parents:
diff changeset
1396 then
kono
parents:
diff changeset
1397 Process_Non_Static_Choice (Choice);
kono
parents:
diff changeset
1398 return;
kono
parents:
diff changeset
1399
kono
parents:
diff changeset
1400 -- Ignore range which raise constraint error
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 elsif Raises_Constraint_Error (Lo)
kono
parents:
diff changeset
1403 or else Raises_Constraint_Error (Hi)
kono
parents:
diff changeset
1404 then
kono
parents:
diff changeset
1405 Raises_CE := True;
kono
parents:
diff changeset
1406 return;
kono
parents:
diff changeset
1407
kono
parents:
diff changeset
1408 -- AI05-0188 : Within an instance the non-others choices do not
kono
parents:
diff changeset
1409 -- have to belong to the actual subtype.
kono
parents:
diff changeset
1410
kono
parents:
diff changeset
1411 elsif Ada_Version >= Ada_2012 and then In_Instance then
kono
parents:
diff changeset
1412 return;
kono
parents:
diff changeset
1413
kono
parents:
diff changeset
1414 -- Otherwise we have an OK static choice
kono
parents:
diff changeset
1415
kono
parents:
diff changeset
1416 else
kono
parents:
diff changeset
1417 Lo_Val := Expr_Value (Lo);
kono
parents:
diff changeset
1418 Hi_Val := Expr_Value (Hi);
kono
parents:
diff changeset
1419
kono
parents:
diff changeset
1420 -- Do not insert null ranges in the choices table
kono
parents:
diff changeset
1421
kono
parents:
diff changeset
1422 if Lo_Val > Hi_Val then
kono
parents:
diff changeset
1423 Process_Empty_Choice (Choice);
kono
parents:
diff changeset
1424 return;
kono
parents:
diff changeset
1425 end if;
kono
parents:
diff changeset
1426 end if;
kono
parents:
diff changeset
1427
kono
parents:
diff changeset
1428 -- Check for low bound out of range
kono
parents:
diff changeset
1429
kono
parents:
diff changeset
1430 if Lo_Val < Bounds_Lo then
kono
parents:
diff changeset
1431
kono
parents:
diff changeset
1432 -- If the choice is an entity name, then it is a type, and we
kono
parents:
diff changeset
1433 -- want to post the message on the reference to this entity.
kono
parents:
diff changeset
1434 -- Otherwise post it on the lower bound of the range.
kono
parents:
diff changeset
1435
kono
parents:
diff changeset
1436 if Is_Entity_Name (Choice) then
kono
parents:
diff changeset
1437 Enode := Choice;
kono
parents:
diff changeset
1438 else
kono
parents:
diff changeset
1439 Enode := Lo;
kono
parents:
diff changeset
1440 end if;
kono
parents:
diff changeset
1441
kono
parents:
diff changeset
1442 -- Specialize message for integer/enum type
kono
parents:
diff changeset
1443
kono
parents:
diff changeset
1444 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
1445 Error_Msg_Uint_1 := Bounds_Lo;
kono
parents:
diff changeset
1446 Error_Msg_N ("minimum allowed choice value is^", Enode);
kono
parents:
diff changeset
1447 else
kono
parents:
diff changeset
1448 Error_Msg_Name_1 := Choice_Image (Bounds_Lo, Bounds_Type);
kono
parents:
diff changeset
1449 Error_Msg_N ("minimum allowed choice value is%", Enode);
kono
parents:
diff changeset
1450 end if;
kono
parents:
diff changeset
1451 end if;
kono
parents:
diff changeset
1452
kono
parents:
diff changeset
1453 -- Check for high bound out of range
kono
parents:
diff changeset
1454
kono
parents:
diff changeset
1455 if Hi_Val > Bounds_Hi then
kono
parents:
diff changeset
1456
kono
parents:
diff changeset
1457 -- If the choice is an entity name, then it is a type, and we
kono
parents:
diff changeset
1458 -- want to post the message on the reference to this entity.
kono
parents:
diff changeset
1459 -- Otherwise post it on the upper bound of the range.
kono
parents:
diff changeset
1460
kono
parents:
diff changeset
1461 if Is_Entity_Name (Choice) then
kono
parents:
diff changeset
1462 Enode := Choice;
kono
parents:
diff changeset
1463 else
kono
parents:
diff changeset
1464 Enode := Hi;
kono
parents:
diff changeset
1465 end if;
kono
parents:
diff changeset
1466
kono
parents:
diff changeset
1467 -- Specialize message for integer/enum type
kono
parents:
diff changeset
1468
kono
parents:
diff changeset
1469 if Is_Integer_Type (Bounds_Type) then
kono
parents:
diff changeset
1470 Error_Msg_Uint_1 := Bounds_Hi;
kono
parents:
diff changeset
1471 Error_Msg_N ("maximum allowed choice value is^", Enode);
kono
parents:
diff changeset
1472 else
kono
parents:
diff changeset
1473 Error_Msg_Name_1 := Choice_Image (Bounds_Hi, Bounds_Type);
kono
parents:
diff changeset
1474 Error_Msg_N ("maximum allowed choice value is%", Enode);
kono
parents:
diff changeset
1475 end if;
kono
parents:
diff changeset
1476 end if;
kono
parents:
diff changeset
1477
kono
parents:
diff changeset
1478 -- Collect bounds in the list
kono
parents:
diff changeset
1479
kono
parents:
diff changeset
1480 -- Note: we still store the bounds, even if they are out of range,
kono
parents:
diff changeset
1481 -- since this may prevent unnecessary cascaded errors for values
kono
parents:
diff changeset
1482 -- that are covered by such an excessive range.
kono
parents:
diff changeset
1483
kono
parents:
diff changeset
1484 Choice_List :=
kono
parents:
diff changeset
1485 new Link'(Val => (Lo, Hi, Choice), Nxt => Choice_List);
kono
parents:
diff changeset
1486 Num_Choices := Num_Choices + 1;
kono
parents:
diff changeset
1487 end Check;
kono
parents:
diff changeset
1488
kono
parents:
diff changeset
1489 -----------------------------
kono
parents:
diff changeset
1490 -- Handle_Static_Predicate --
kono
parents:
diff changeset
1491 -----------------------------
kono
parents:
diff changeset
1492
kono
parents:
diff changeset
1493 procedure Handle_Static_Predicate
kono
parents:
diff changeset
1494 (Typ : Entity_Id;
kono
parents:
diff changeset
1495 Lo : Node_Id;
kono
parents:
diff changeset
1496 Hi : Node_Id)
kono
parents:
diff changeset
1497 is
kono
parents:
diff changeset
1498 P : Node_Id;
kono
parents:
diff changeset
1499 C : Node_Id;
kono
parents:
diff changeset
1500
kono
parents:
diff changeset
1501 begin
kono
parents:
diff changeset
1502 -- Loop through entries in predicate list, checking each entry.
kono
parents:
diff changeset
1503 -- Note that if the list is empty, corresponding to a False
kono
parents:
diff changeset
1504 -- predicate, then no choices are checked. If the choice comes
kono
parents:
diff changeset
1505 -- from a subtype indication, the given range may have bounds
kono
parents:
diff changeset
1506 -- that narrow the predicate choices themselves, so we must
kono
parents:
diff changeset
1507 -- consider only those entries within the range of the given
kono
parents:
diff changeset
1508 -- subtype indication..
kono
parents:
diff changeset
1509
kono
parents:
diff changeset
1510 P := First (Static_Discrete_Predicate (Typ));
kono
parents:
diff changeset
1511 while Present (P) loop
kono
parents:
diff changeset
1512
kono
parents:
diff changeset
1513 -- Check that part of the predicate choice is included in the
kono
parents:
diff changeset
1514 -- given bounds.
kono
parents:
diff changeset
1515
kono
parents:
diff changeset
1516 if Expr_Value (High_Bound (P)) >= Expr_Value (Lo)
kono
parents:
diff changeset
1517 and then Expr_Value (Low_Bound (P)) <= Expr_Value (Hi)
kono
parents:
diff changeset
1518 then
kono
parents:
diff changeset
1519 C := New_Copy (P);
kono
parents:
diff changeset
1520 Set_Sloc (C, Sloc (Choice));
kono
parents:
diff changeset
1521
kono
parents:
diff changeset
1522 if Expr_Value (Low_Bound (C)) < Expr_Value (Lo) then
kono
parents:
diff changeset
1523 Set_Low_Bound (C, Lo);
kono
parents:
diff changeset
1524 end if;
kono
parents:
diff changeset
1525
kono
parents:
diff changeset
1526 if Expr_Value (High_Bound (C)) > Expr_Value (Hi) then
kono
parents:
diff changeset
1527 Set_High_Bound (C, Hi);
kono
parents:
diff changeset
1528 end if;
kono
parents:
diff changeset
1529
kono
parents:
diff changeset
1530 Check (C, Low_Bound (C), High_Bound (C));
kono
parents:
diff changeset
1531 end if;
kono
parents:
diff changeset
1532
kono
parents:
diff changeset
1533 Next (P);
kono
parents:
diff changeset
1534 end loop;
kono
parents:
diff changeset
1535
kono
parents:
diff changeset
1536 Set_Has_SP_Choice (Alt);
kono
parents:
diff changeset
1537 end Handle_Static_Predicate;
kono
parents:
diff changeset
1538
kono
parents:
diff changeset
1539 -- Start of processing for Check_Choices
kono
parents:
diff changeset
1540
kono
parents:
diff changeset
1541 begin
kono
parents:
diff changeset
1542 Raises_CE := False;
kono
parents:
diff changeset
1543 Others_Present := False;
kono
parents:
diff changeset
1544
kono
parents:
diff changeset
1545 -- If Subtyp is not a discrete type or there was some other error,
kono
parents:
diff changeset
1546 -- then don't try any semantic checking on the choices since we have
kono
parents:
diff changeset
1547 -- a complete mess.
kono
parents:
diff changeset
1548
kono
parents:
diff changeset
1549 if not Is_Discrete_Type (Subtyp) or else Subtyp = Any_Type then
kono
parents:
diff changeset
1550 return;
kono
parents:
diff changeset
1551 end if;
kono
parents:
diff changeset
1552
kono
parents:
diff changeset
1553 -- If Subtyp is not a static subtype Ada 95 requires then we use the
kono
parents:
diff changeset
1554 -- bounds of its base type to determine the values covered by the
kono
parents:
diff changeset
1555 -- discrete choices.
kono
parents:
diff changeset
1556
kono
parents:
diff changeset
1557 -- In Ada 2012, if the subtype has a non-static predicate the full
kono
parents:
diff changeset
1558 -- range of the base type must be covered as well.
kono
parents:
diff changeset
1559
kono
parents:
diff changeset
1560 if Is_OK_Static_Subtype (Subtyp) then
kono
parents:
diff changeset
1561 if not Has_Predicates (Subtyp)
kono
parents:
diff changeset
1562 or else Has_Static_Predicate (Subtyp)
kono
parents:
diff changeset
1563 then
kono
parents:
diff changeset
1564 Bounds_Type := Subtyp;
kono
parents:
diff changeset
1565 else
kono
parents:
diff changeset
1566 Bounds_Type := Choice_Type;
kono
parents:
diff changeset
1567 end if;
kono
parents:
diff changeset
1568
kono
parents:
diff changeset
1569 else
kono
parents:
diff changeset
1570 Bounds_Type := Choice_Type;
kono
parents:
diff changeset
1571 end if;
kono
parents:
diff changeset
1572
kono
parents:
diff changeset
1573 -- Obtain static bounds of type, unless this is a generic formal
kono
parents:
diff changeset
1574 -- discrete type for which all choices will be non-static.
kono
parents:
diff changeset
1575
kono
parents:
diff changeset
1576 if not Is_Generic_Type (Root_Type (Bounds_Type))
kono
parents:
diff changeset
1577 or else Ekind (Bounds_Type) /= E_Enumeration_Type
kono
parents:
diff changeset
1578 then
kono
parents:
diff changeset
1579 Bounds_Lo := Expr_Value (Type_Low_Bound (Bounds_Type));
kono
parents:
diff changeset
1580 Bounds_Hi := Expr_Value (Type_High_Bound (Bounds_Type));
kono
parents:
diff changeset
1581 end if;
kono
parents:
diff changeset
1582
kono
parents:
diff changeset
1583 if Choice_Type = Universal_Integer then
kono
parents:
diff changeset
1584 Expected_Type := Any_Integer;
kono
parents:
diff changeset
1585 else
kono
parents:
diff changeset
1586 Expected_Type := Choice_Type;
kono
parents:
diff changeset
1587 end if;
kono
parents:
diff changeset
1588
kono
parents:
diff changeset
1589 -- Now loop through the case alternatives or record variants
kono
parents:
diff changeset
1590
kono
parents:
diff changeset
1591 Alt := First (Alternatives);
kono
parents:
diff changeset
1592 while Present (Alt) loop
kono
parents:
diff changeset
1593
kono
parents:
diff changeset
1594 -- If pragma, just analyze it
kono
parents:
diff changeset
1595
kono
parents:
diff changeset
1596 if Nkind (Alt) = N_Pragma then
kono
parents:
diff changeset
1597 Analyze (Alt);
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 -- Otherwise we have an alternative. In most cases the semantic
kono
parents:
diff changeset
1600 -- processing leaves the list of choices unchanged
kono
parents:
diff changeset
1601
kono
parents:
diff changeset
1602 -- Check each choice against its base type
kono
parents:
diff changeset
1603
kono
parents:
diff changeset
1604 else
kono
parents:
diff changeset
1605 Choice := First (Discrete_Choices (Alt));
kono
parents:
diff changeset
1606 while Present (Choice) loop
kono
parents:
diff changeset
1607 Kind := Nkind (Choice);
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 -- Choice is a Range
kono
parents:
diff changeset
1610
kono
parents:
diff changeset
1611 if Kind = N_Range
kono
parents:
diff changeset
1612 or else (Kind = N_Attribute_Reference
kono
parents:
diff changeset
1613 and then Attribute_Name (Choice) = Name_Range)
kono
parents:
diff changeset
1614 then
kono
parents:
diff changeset
1615 Check (Choice, Low_Bound (Choice), High_Bound (Choice));
kono
parents:
diff changeset
1616
kono
parents:
diff changeset
1617 -- Choice is a subtype name
kono
parents:
diff changeset
1618
kono
parents:
diff changeset
1619 elsif Is_Entity_Name (Choice)
kono
parents:
diff changeset
1620 and then Is_Type (Entity (Choice))
kono
parents:
diff changeset
1621 then
kono
parents:
diff changeset
1622 -- Check for inappropriate type
kono
parents:
diff changeset
1623
kono
parents:
diff changeset
1624 if not Covers (Expected_Type, Etype (Choice)) then
kono
parents:
diff changeset
1625 Wrong_Type (Choice, Choice_Type);
kono
parents:
diff changeset
1626
kono
parents:
diff changeset
1627 -- Type is OK, so check further
kono
parents:
diff changeset
1628
kono
parents:
diff changeset
1629 else
kono
parents:
diff changeset
1630 E := Entity (Choice);
kono
parents:
diff changeset
1631
kono
parents:
diff changeset
1632 -- Case of predicated subtype
kono
parents:
diff changeset
1633
kono
parents:
diff changeset
1634 if Has_Predicates (E) then
kono
parents:
diff changeset
1635
kono
parents:
diff changeset
1636 -- Use of non-static predicate is an error
kono
parents:
diff changeset
1637
kono
parents:
diff changeset
1638 if not Is_Discrete_Type (E)
kono
parents:
diff changeset
1639 or else not Has_Static_Predicate (E)
kono
parents:
diff changeset
1640 or else Has_Dynamic_Predicate_Aspect (E)
kono
parents:
diff changeset
1641 then
kono
parents:
diff changeset
1642 Bad_Predicated_Subtype_Use
kono
parents:
diff changeset
1643 ("cannot use subtype& with non-static "
kono
parents:
diff changeset
1644 & "predicate as case alternative",
kono
parents:
diff changeset
1645 Choice, E, Suggest_Static => True);
kono
parents:
diff changeset
1646
kono
parents:
diff changeset
1647 -- Static predicate case. The bounds are those of
kono
parents:
diff changeset
1648 -- the given subtype.
kono
parents:
diff changeset
1649
kono
parents:
diff changeset
1650 else
kono
parents:
diff changeset
1651 Handle_Static_Predicate (E,
kono
parents:
diff changeset
1652 Type_Low_Bound (E), Type_High_Bound (E));
kono
parents:
diff changeset
1653 end if;
kono
parents:
diff changeset
1654
kono
parents:
diff changeset
1655 -- Not predicated subtype case
kono
parents:
diff changeset
1656
kono
parents:
diff changeset
1657 elsif not Is_OK_Static_Subtype (E) then
kono
parents:
diff changeset
1658 Process_Non_Static_Choice (Choice);
kono
parents:
diff changeset
1659 else
kono
parents:
diff changeset
1660 Check
kono
parents:
diff changeset
1661 (Choice, Type_Low_Bound (E), Type_High_Bound (E));
kono
parents:
diff changeset
1662 end if;
kono
parents:
diff changeset
1663 end if;
kono
parents:
diff changeset
1664
kono
parents:
diff changeset
1665 -- Choice is a subtype indication
kono
parents:
diff changeset
1666
kono
parents:
diff changeset
1667 elsif Kind = N_Subtype_Indication then
kono
parents:
diff changeset
1668 Resolve_Discrete_Subtype_Indication
kono
parents:
diff changeset
1669 (Choice, Expected_Type);
kono
parents:
diff changeset
1670
kono
parents:
diff changeset
1671 if Etype (Choice) /= Any_Type then
kono
parents:
diff changeset
1672 declare
kono
parents:
diff changeset
1673 C : constant Node_Id := Constraint (Choice);
kono
parents:
diff changeset
1674 R : constant Node_Id := Range_Expression (C);
kono
parents:
diff changeset
1675 L : constant Node_Id := Low_Bound (R);
kono
parents:
diff changeset
1676 H : constant Node_Id := High_Bound (R);
kono
parents:
diff changeset
1677
kono
parents:
diff changeset
1678 begin
kono
parents:
diff changeset
1679 E := Entity (Subtype_Mark (Choice));
kono
parents:
diff changeset
1680
kono
parents:
diff changeset
1681 if not Is_OK_Static_Subtype (E) then
kono
parents:
diff changeset
1682 Process_Non_Static_Choice (Choice);
kono
parents:
diff changeset
1683
kono
parents:
diff changeset
1684 else
kono
parents:
diff changeset
1685 if Is_OK_Static_Expression (L)
kono
parents:
diff changeset
1686 and then
kono
parents:
diff changeset
1687 Is_OK_Static_Expression (H)
kono
parents:
diff changeset
1688 then
kono
parents:
diff changeset
1689 if Expr_Value (L) > Expr_Value (H) then
kono
parents:
diff changeset
1690 Process_Empty_Choice (Choice);
kono
parents:
diff changeset
1691 else
kono
parents:
diff changeset
1692 if Is_Out_Of_Range (L, E) then
kono
parents:
diff changeset
1693 Apply_Compile_Time_Constraint_Error
kono
parents:
diff changeset
1694 (L, "static value out of range",
kono
parents:
diff changeset
1695 CE_Range_Check_Failed);
kono
parents:
diff changeset
1696 end if;
kono
parents:
diff changeset
1697
kono
parents:
diff changeset
1698 if Is_Out_Of_Range (H, E) then
kono
parents:
diff changeset
1699 Apply_Compile_Time_Constraint_Error
kono
parents:
diff changeset
1700 (H, "static value out of range",
kono
parents:
diff changeset
1701 CE_Range_Check_Failed);
kono
parents:
diff changeset
1702 end if;
kono
parents:
diff changeset
1703 end if;
kono
parents:
diff changeset
1704 end if;
kono
parents:
diff changeset
1705
kono
parents:
diff changeset
1706 -- Check applicable predicate values within the
kono
parents:
diff changeset
1707 -- bounds of the given range.
kono
parents:
diff changeset
1708
kono
parents:
diff changeset
1709 if Has_Static_Predicate (E) then
kono
parents:
diff changeset
1710 Handle_Static_Predicate (E, L, H);
kono
parents:
diff changeset
1711
kono
parents:
diff changeset
1712 else
kono
parents:
diff changeset
1713 Check (Choice, L, H);
kono
parents:
diff changeset
1714 end if;
kono
parents:
diff changeset
1715 end if;
kono
parents:
diff changeset
1716 end;
kono
parents:
diff changeset
1717 end if;
kono
parents:
diff changeset
1718
kono
parents:
diff changeset
1719 -- The others choice is only allowed for the last
kono
parents:
diff changeset
1720 -- alternative and as its only choice.
kono
parents:
diff changeset
1721
kono
parents:
diff changeset
1722 elsif Kind = N_Others_Choice then
kono
parents:
diff changeset
1723 if not (Choice = First (Discrete_Choices (Alt))
kono
parents:
diff changeset
1724 and then Choice = Last (Discrete_Choices (Alt))
kono
parents:
diff changeset
1725 and then Alt = Last (Alternatives))
kono
parents:
diff changeset
1726 then
kono
parents:
diff changeset
1727 Error_Msg_N
kono
parents:
diff changeset
1728 ("the choice OTHERS must appear alone and last",
kono
parents:
diff changeset
1729 Choice);
kono
parents:
diff changeset
1730 return;
kono
parents:
diff changeset
1731 end if;
kono
parents:
diff changeset
1732
kono
parents:
diff changeset
1733 Others_Present := True;
kono
parents:
diff changeset
1734 Others_Choice := Choice;
kono
parents:
diff changeset
1735
kono
parents:
diff changeset
1736 -- Only other possibility is an expression
kono
parents:
diff changeset
1737
kono
parents:
diff changeset
1738 else
kono
parents:
diff changeset
1739 Check (Choice, Choice, Choice);
kono
parents:
diff changeset
1740 end if;
kono
parents:
diff changeset
1741
kono
parents:
diff changeset
1742 -- Move to next choice
kono
parents:
diff changeset
1743
kono
parents:
diff changeset
1744 Next (Choice);
kono
parents:
diff changeset
1745 end loop;
kono
parents:
diff changeset
1746
kono
parents:
diff changeset
1747 Process_Associated_Node (Alt);
kono
parents:
diff changeset
1748 end if;
kono
parents:
diff changeset
1749
kono
parents:
diff changeset
1750 Next (Alt);
kono
parents:
diff changeset
1751 end loop;
kono
parents:
diff changeset
1752
kono
parents:
diff changeset
1753 -- Now we can create the Choice_Table, since we know how long
kono
parents:
diff changeset
1754 -- it needs to be so we can allocate exactly the right length.
kono
parents:
diff changeset
1755
kono
parents:
diff changeset
1756 declare
kono
parents:
diff changeset
1757 Choice_Table : Choice_Table_Type (0 .. Num_Choices);
kono
parents:
diff changeset
1758
kono
parents:
diff changeset
1759 begin
kono
parents:
diff changeset
1760 -- Now copy the items we collected in the linked list into this
kono
parents:
diff changeset
1761 -- newly allocated table (leave entry 0 unused for sorting).
kono
parents:
diff changeset
1762
kono
parents:
diff changeset
1763 declare
kono
parents:
diff changeset
1764 T : Link_Ptr;
kono
parents:
diff changeset
1765 begin
kono
parents:
diff changeset
1766 for J in 1 .. Num_Choices loop
kono
parents:
diff changeset
1767 T := Choice_List;
kono
parents:
diff changeset
1768 Choice_List := T.Nxt;
kono
parents:
diff changeset
1769 Choice_Table (J) := T.Val;
kono
parents:
diff changeset
1770 Free (T);
kono
parents:
diff changeset
1771 end loop;
kono
parents:
diff changeset
1772 end;
kono
parents:
diff changeset
1773
kono
parents:
diff changeset
1774 Check_Choice_Set
kono
parents:
diff changeset
1775 (Choice_Table,
kono
parents:
diff changeset
1776 Bounds_Type,
kono
parents:
diff changeset
1777 Subtyp,
kono
parents:
diff changeset
1778 Others_Present or else (Choice_Type = Universal_Integer),
kono
parents:
diff changeset
1779 N);
kono
parents:
diff changeset
1780
kono
parents:
diff changeset
1781 -- If no others choice we are all done, otherwise we have one more
kono
parents:
diff changeset
1782 -- step, which is to set the Others_Discrete_Choices field of the
kono
parents:
diff changeset
1783 -- others choice (to contain all otherwise unspecified choices).
kono
parents:
diff changeset
1784 -- Skip this if CE is known to be raised.
kono
parents:
diff changeset
1785
kono
parents:
diff changeset
1786 if Others_Present and not Raises_CE then
kono
parents:
diff changeset
1787 Expand_Others_Choice
kono
parents:
diff changeset
1788 (Case_Table => Choice_Table,
kono
parents:
diff changeset
1789 Others_Choice => Others_Choice,
kono
parents:
diff changeset
1790 Choice_Type => Bounds_Type);
kono
parents:
diff changeset
1791 end if;
kono
parents:
diff changeset
1792 end;
kono
parents:
diff changeset
1793 end Check_Choices;
kono
parents:
diff changeset
1794
kono
parents:
diff changeset
1795 end Generic_Check_Choices;
kono
parents:
diff changeset
1796
kono
parents:
diff changeset
1797 end Sem_Case;