]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ C H 6 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
996ae0b0 | 8 | -- -- |
3e2399ba | 9 | -- Copyright (C) 1992-2010, Free Software Foundation, Inc. -- |
996ae0b0 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
996ae0b0 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
b5c84c3c RD |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
996ae0b0 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
996ae0b0 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
0f1a6a0b | 26 | with Aspects; use Aspects; |
996ae0b0 RK |
27 | with Atree; use Atree; |
28 | with Checks; use Checks; | |
29 | with Debug; use Debug; | |
30 | with Einfo; use Einfo; | |
31 | with Elists; use Elists; | |
32 | with Errout; use Errout; | |
33 | with Expander; use Expander; | |
ec4867fa | 34 | with Exp_Ch6; use Exp_Ch6; |
996ae0b0 | 35 | with Exp_Ch7; use Exp_Ch7; |
21d27997 | 36 | with Exp_Ch9; use Exp_Ch9; |
ce2b6ba5 | 37 | with Exp_Disp; use Exp_Disp; |
e660dbf7 | 38 | with Exp_Tss; use Exp_Tss; |
ec4867fa | 39 | with Exp_Util; use Exp_Util; |
fbf5a39b | 40 | with Fname; use Fname; |
996ae0b0 | 41 | with Freeze; use Freeze; |
41251c60 | 42 | with Itypes; use Itypes; |
996ae0b0 | 43 | with Lib.Xref; use Lib.Xref; |
ec4867fa | 44 | with Layout; use Layout; |
996ae0b0 RK |
45 | with Namet; use Namet; |
46 | with Lib; use Lib; | |
47 | with Nlists; use Nlists; | |
48 | with Nmake; use Nmake; | |
49 | with Opt; use Opt; | |
50 | with Output; use Output; | |
b20de9b9 AC |
51 | with Restrict; use Restrict; |
52 | with Rident; use Rident; | |
996ae0b0 RK |
53 | with Rtsfind; use Rtsfind; |
54 | with Sem; use Sem; | |
a4100e55 | 55 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
56 | with Sem_Cat; use Sem_Cat; |
57 | with Sem_Ch3; use Sem_Ch3; | |
58 | with Sem_Ch4; use Sem_Ch4; | |
59 | with Sem_Ch5; use Sem_Ch5; | |
60 | with Sem_Ch8; use Sem_Ch8; | |
9bc856dd | 61 | with Sem_Ch10; use Sem_Ch10; |
996ae0b0 | 62 | with Sem_Ch12; use Sem_Ch12; |
0f1a6a0b | 63 | with Sem_Ch13; use Sem_Ch13; |
996ae0b0 RK |
64 | with Sem_Disp; use Sem_Disp; |
65 | with Sem_Dist; use Sem_Dist; | |
66 | with Sem_Elim; use Sem_Elim; | |
67 | with Sem_Eval; use Sem_Eval; | |
68 | with Sem_Mech; use Sem_Mech; | |
69 | with Sem_Prag; use Sem_Prag; | |
70 | with Sem_Res; use Sem_Res; | |
71 | with Sem_Util; use Sem_Util; | |
72 | with Sem_Type; use Sem_Type; | |
73 | with Sem_Warn; use Sem_Warn; | |
74 | with Sinput; use Sinput; | |
75 | with Stand; use Stand; | |
76 | with Sinfo; use Sinfo; | |
77 | with Sinfo.CN; use Sinfo.CN; | |
78 | with Snames; use Snames; | |
79 | with Stringt; use Stringt; | |
80 | with Style; | |
81 | with Stylesw; use Stylesw; | |
82 | with Tbuild; use Tbuild; | |
83 | with Uintp; use Uintp; | |
84 | with Urealp; use Urealp; | |
85 | with Validsw; use Validsw; | |
86 | ||
87 | package body Sem_Ch6 is | |
88 | ||
c8ef728f | 89 | May_Hide_Profile : Boolean := False; |
ec4867fa ES |
90 | -- This flag is used to indicate that two formals in two subprograms being |
91 | -- checked for conformance differ only in that one is an access parameter | |
92 | -- while the other is of a general access type with the same designated | |
93 | -- type. In this case, if the rest of the signatures match, a call to | |
94 | -- either subprogram may be ambiguous, which is worth a warning. The flag | |
95 | -- is set in Compatible_Types, and the warning emitted in | |
96 | -- New_Overloaded_Entity. | |
c8ef728f | 97 | |
996ae0b0 RK |
98 | ----------------------- |
99 | -- Local Subprograms -- | |
100 | ----------------------- | |
101 | ||
5d37ba92 | 102 | procedure Analyze_Return_Statement (N : Node_Id); |
5b9c3fc4 | 103 | -- Common processing for simple and extended return statements |
ec4867fa ES |
104 | |
105 | procedure Analyze_Function_Return (N : Node_Id); | |
81db9d77 ES |
106 | -- Subsidiary to Analyze_Return_Statement. Called when the return statement |
107 | -- applies to a [generic] function. | |
ec4867fa | 108 | |
82c80734 RD |
109 | procedure Analyze_Return_Type (N : Node_Id); |
110 | -- Subsidiary to Process_Formals: analyze subtype mark in function | |
5b9c3fc4 | 111 | -- specification in a context where the formals are visible and hide |
82c80734 RD |
112 | -- outer homographs. |
113 | ||
b1b543d2 | 114 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id); |
13d923cc RD |
115 | -- Does all the real work of Analyze_Subprogram_Body. This is split out so |
116 | -- that we can use RETURN but not skip the debug output at the end. | |
b1b543d2 | 117 | |
996ae0b0 | 118 | procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); |
82c80734 RD |
119 | -- Analyze a generic subprogram body. N is the body to be analyzed, and |
120 | -- Gen_Id is the defining entity Id for the corresponding spec. | |
996ae0b0 | 121 | |
d05ef0ab | 122 | procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id); |
996ae0b0 RK |
123 | -- If a subprogram has pragma Inline and inlining is active, use generic |
124 | -- machinery to build an unexpanded body for the subprogram. This body is | |
f3d57416 | 125 | -- subsequently used for inline expansions at call sites. If subprogram can |
996ae0b0 RK |
126 | -- be inlined (depending on size and nature of local declarations) this |
127 | -- function returns true. Otherwise subprogram body is treated normally. | |
aa720a54 AC |
128 | -- If proper warnings are enabled and the subprogram contains a construct |
129 | -- that cannot be inlined, the offending construct is flagged accordingly. | |
996ae0b0 | 130 | |
996ae0b0 | 131 | procedure Check_Conformance |
41251c60 JM |
132 | (New_Id : Entity_Id; |
133 | Old_Id : Entity_Id; | |
134 | Ctype : Conformance_Type; | |
135 | Errmsg : Boolean; | |
136 | Conforms : out Boolean; | |
137 | Err_Loc : Node_Id := Empty; | |
138 | Get_Inst : Boolean := False; | |
139 | Skip_Controlling_Formals : Boolean := False); | |
996ae0b0 RK |
140 | -- Given two entities, this procedure checks that the profiles associated |
141 | -- with these entities meet the conformance criterion given by the third | |
142 | -- parameter. If they conform, Conforms is set True and control returns | |
143 | -- to the caller. If they do not conform, Conforms is set to False, and | |
144 | -- in addition, if Errmsg is True on the call, proper messages are output | |
145 | -- to complain about the conformance failure. If Err_Loc is non_Empty | |
146 | -- the error messages are placed on Err_Loc, if Err_Loc is empty, then | |
147 | -- error messages are placed on the appropriate part of the construct | |
148 | -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance | |
149 | -- against a formal access-to-subprogram type so Get_Instance_Of must | |
150 | -- be called. | |
151 | ||
152 | procedure Check_Subprogram_Order (N : Node_Id); | |
153 | -- N is the N_Subprogram_Body node for a subprogram. This routine applies | |
154 | -- the alpha ordering rule for N if this ordering requirement applicable. | |
155 | ||
996ae0b0 RK |
156 | procedure Check_Returns |
157 | (HSS : Node_Id; | |
158 | Mode : Character; | |
c8ef728f ES |
159 | Err : out Boolean; |
160 | Proc : Entity_Id := Empty); | |
161 | -- Called to check for missing return statements in a function body, or for | |
0a36105d | 162 | -- returns present in a procedure body which has No_Return set. HSS is the |
c8ef728f ES |
163 | -- handled statement sequence for the subprogram body. This procedure |
164 | -- checks all flow paths to make sure they either have return (Mode = 'F', | |
165 | -- used for functions) or do not have a return (Mode = 'P', used for | |
166 | -- No_Return procedures). The flag Err is set if there are any control | |
167 | -- paths not explicitly terminated by a return in the function case, and is | |
168 | -- True otherwise. Proc is the entity for the procedure case and is used | |
169 | -- in posting the warning message. | |
996ae0b0 | 170 | |
e5a58fac AC |
171 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id); |
172 | -- In Ada 2012, a primitive equality operator on an untagged record type | |
173 | -- must appear before the type is frozen, and have the same visibility as | |
174 | -- that of the type. This procedure checks that this rule is met, and | |
175 | -- otherwise emits an error on the subprogram declaration and a warning | |
176 | -- on the earlier freeze point if it is easy to locate. | |
177 | ||
996ae0b0 | 178 | procedure Enter_Overloaded_Entity (S : Entity_Id); |
82c80734 RD |
179 | -- This procedure makes S, a new overloaded entity, into the first visible |
180 | -- entity with that name. | |
996ae0b0 RK |
181 | |
182 | procedure Install_Entity (E : Entity_Id); | |
e895b435 | 183 | -- Make single entity visible. Used for generic formals as well |
996ae0b0 | 184 | |
a5b62485 AC |
185 | function Is_Non_Overriding_Operation |
186 | (Prev_E : Entity_Id; | |
187 | New_E : Entity_Id) return Boolean; | |
188 | -- Enforce the rule given in 12.3(18): a private operation in an instance | |
189 | -- overrides an inherited operation only if the corresponding operation | |
190 | -- was overriding in the generic. This can happen for primitive operations | |
191 | -- of types derived (in the generic unit) from formal private or formal | |
192 | -- derived types. | |
193 | ||
996ae0b0 RK |
194 | procedure Make_Inequality_Operator (S : Entity_Id); |
195 | -- Create the declaration for an inequality operator that is implicitly | |
196 | -- created by a user-defined equality operator that yields a boolean. | |
197 | ||
198 | procedure May_Need_Actuals (Fun : Entity_Id); | |
199 | -- Flag functions that can be called without parameters, i.e. those that | |
200 | -- have no parameters, or those for which defaults exist for all parameters | |
201 | ||
21d27997 RD |
202 | procedure Process_PPCs |
203 | (N : Node_Id; | |
204 | Spec_Id : Entity_Id; | |
205 | Body_Id : Entity_Id); | |
3764bb00 BD |
206 | -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post |
207 | -- conditions for the body and assembling and inserting the _postconditions | |
208 | -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are | |
209 | -- the entities for the body and separate spec (if there is no separate | |
210 | -- spec, Spec_Id is Empty). | |
21d27997 | 211 | |
996ae0b0 RK |
212 | procedure Set_Formal_Validity (Formal_Id : Entity_Id); |
213 | -- Formal_Id is an formal parameter entity. This procedure deals with | |
e358346d AC |
214 | -- setting the proper validity status for this entity, which depends on |
215 | -- the kind of parameter and the validity checking mode. | |
996ae0b0 | 216 | |
5d37ba92 ES |
217 | ------------------------------ |
218 | -- Analyze_Return_Statement -- | |
219 | ------------------------------ | |
ec4867fa | 220 | |
5d37ba92 | 221 | procedure Analyze_Return_Statement (N : Node_Id) is |
ec4867fa | 222 | |
800621e0 RD |
223 | pragma Assert (Nkind_In (N, N_Simple_Return_Statement, |
224 | N_Extended_Return_Statement)); | |
ec4867fa ES |
225 | |
226 | Returns_Object : constant Boolean := | |
5d37ba92 ES |
227 | Nkind (N) = N_Extended_Return_Statement |
228 | or else | |
229 | (Nkind (N) = N_Simple_Return_Statement | |
230 | and then Present (Expression (N))); | |
ec4867fa | 231 | -- True if we're returning something; that is, "return <expression>;" |
5d37ba92 ES |
232 | -- or "return Result : T [:= ...]". False for "return;". Used for error |
233 | -- checking: If Returns_Object is True, N should apply to a function | |
234 | -- body; otherwise N should apply to a procedure body, entry body, | |
235 | -- accept statement, or extended return statement. | |
ec4867fa ES |
236 | |
237 | function Find_What_It_Applies_To return Entity_Id; | |
238 | -- Find the entity representing the innermost enclosing body, accept | |
5d37ba92 ES |
239 | -- statement, or extended return statement. If the result is a callable |
240 | -- construct or extended return statement, then this will be the value | |
241 | -- of the Return_Applies_To attribute. Otherwise, the program is | |
242 | -- illegal. See RM-6.5(4/2). | |
ec4867fa ES |
243 | |
244 | ----------------------------- | |
245 | -- Find_What_It_Applies_To -- | |
246 | ----------------------------- | |
247 | ||
248 | function Find_What_It_Applies_To return Entity_Id is | |
249 | Result : Entity_Id := Empty; | |
250 | ||
251 | begin | |
252 | -- Loop outward through the Scope_Stack, skipping blocks and loops | |
253 | ||
254 | for J in reverse 0 .. Scope_Stack.Last loop | |
255 | Result := Scope_Stack.Table (J).Entity; | |
256 | exit when Ekind (Result) /= E_Block and then | |
257 | Ekind (Result) /= E_Loop; | |
258 | end loop; | |
259 | ||
260 | pragma Assert (Present (Result)); | |
261 | return Result; | |
ec4867fa ES |
262 | end Find_What_It_Applies_To; |
263 | ||
5d37ba92 ES |
264 | -- Local declarations |
265 | ||
ec4867fa ES |
266 | Scope_Id : constant Entity_Id := Find_What_It_Applies_To; |
267 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
ec4867fa ES |
268 | Loc : constant Source_Ptr := Sloc (N); |
269 | Stm_Entity : constant Entity_Id := | |
270 | New_Internal_Entity | |
271 | (E_Return_Statement, Current_Scope, Loc, 'R'); | |
272 | ||
5d37ba92 | 273 | -- Start of processing for Analyze_Return_Statement |
ec4867fa ES |
274 | |
275 | begin | |
ec4867fa ES |
276 | Set_Return_Statement_Entity (N, Stm_Entity); |
277 | ||
278 | Set_Etype (Stm_Entity, Standard_Void_Type); | |
279 | Set_Return_Applies_To (Stm_Entity, Scope_Id); | |
280 | ||
5d37ba92 ES |
281 | -- Place Return entity on scope stack, to simplify enforcement of 6.5 |
282 | -- (4/2): an inner return statement will apply to this extended return. | |
ec4867fa ES |
283 | |
284 | if Nkind (N) = N_Extended_Return_Statement then | |
0a36105d | 285 | Push_Scope (Stm_Entity); |
ec4867fa ES |
286 | end if; |
287 | ||
5334d18f BD |
288 | -- Check that pragma No_Return is obeyed. Don't complain about the |
289 | -- implicitly-generated return that is placed at the end. | |
ec4867fa | 290 | |
5334d18f | 291 | if No_Return (Scope_Id) and then Comes_From_Source (N) then |
ec4867fa ES |
292 | Error_Msg_N ("RETURN statement not allowed (No_Return)", N); |
293 | end if; | |
294 | ||
5d37ba92 ES |
295 | -- Warn on any unassigned OUT parameters if in procedure |
296 | ||
297 | if Ekind (Scope_Id) = E_Procedure then | |
298 | Warn_On_Unassigned_Out_Parameter (N, Scope_Id); | |
299 | end if; | |
300 | ||
301 | -- Check that functions return objects, and other things do not | |
ec4867fa ES |
302 | |
303 | if Kind = E_Function or else Kind = E_Generic_Function then | |
304 | if not Returns_Object then | |
305 | Error_Msg_N ("missing expression in return from function", N); | |
306 | end if; | |
307 | ||
308 | elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
309 | if Returns_Object then | |
310 | Error_Msg_N ("procedure cannot return value (use function)", N); | |
311 | end if; | |
312 | ||
313 | elsif Kind = E_Entry or else Kind = E_Entry_Family then | |
314 | if Returns_Object then | |
315 | if Is_Protected_Type (Scope (Scope_Id)) then | |
316 | Error_Msg_N ("entry body cannot return value", N); | |
317 | else | |
318 | Error_Msg_N ("accept statement cannot return value", N); | |
319 | end if; | |
320 | end if; | |
321 | ||
322 | elsif Kind = E_Return_Statement then | |
323 | ||
324 | -- We are nested within another return statement, which must be an | |
325 | -- extended_return_statement. | |
326 | ||
327 | if Returns_Object then | |
ed2233dc | 328 | Error_Msg_N |
ec4867fa ES |
329 | ("extended_return_statement cannot return value; " & |
330 | "use `""RETURN;""`", N); | |
331 | end if; | |
332 | ||
333 | else | |
334 | Error_Msg_N ("illegal context for return statement", N); | |
335 | end if; | |
336 | ||
337 | if Kind = E_Function or else Kind = E_Generic_Function then | |
338 | Analyze_Function_Return (N); | |
339 | end if; | |
340 | ||
341 | if Nkind (N) = N_Extended_Return_Statement then | |
342 | End_Scope; | |
343 | end if; | |
344 | ||
81db9d77 | 345 | Kill_Current_Values (Last_Assignment_Only => True); |
ec4867fa | 346 | Check_Unreachable_Code (N); |
5d37ba92 | 347 | end Analyze_Return_Statement; |
ec4867fa | 348 | |
996ae0b0 RK |
349 | --------------------------------------------- |
350 | -- Analyze_Abstract_Subprogram_Declaration -- | |
351 | --------------------------------------------- | |
352 | ||
353 | procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is | |
fbf5a39b AC |
354 | Designator : constant Entity_Id := |
355 | Analyze_Subprogram_Specification (Specification (N)); | |
996ae0b0 | 356 | Scop : constant Entity_Id := Current_Scope; |
0f1a6a0b | 357 | AS : constant List_Id := Aspect_Specifications (N); |
996ae0b0 RK |
358 | |
359 | begin | |
360 | Generate_Definition (Designator); | |
f937473f | 361 | Set_Is_Abstract_Subprogram (Designator); |
996ae0b0 RK |
362 | New_Overloaded_Entity (Designator); |
363 | Check_Delayed_Subprogram (Designator); | |
364 | ||
fbf5a39b | 365 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 RK |
366 | |
367 | if Ekind (Scope (Designator)) = E_Protected_Type then | |
368 | Error_Msg_N | |
369 | ("abstract subprogram not allowed in protected type", N); | |
5d37ba92 ES |
370 | |
371 | -- Issue a warning if the abstract subprogram is neither a dispatching | |
372 | -- operation nor an operation that overrides an inherited subprogram or | |
373 | -- predefined operator, since this most likely indicates a mistake. | |
374 | ||
375 | elsif Warn_On_Redundant_Constructs | |
376 | and then not Is_Dispatching_Operation (Designator) | |
377 | and then not Is_Overriding_Operation (Designator) | |
378 | and then (not Is_Operator_Symbol_Name (Chars (Designator)) | |
379 | or else Scop /= Scope (Etype (First_Formal (Designator)))) | |
380 | then | |
381 | Error_Msg_N | |
382 | ("?abstract subprogram is not dispatching or overriding", N); | |
996ae0b0 | 383 | end if; |
fbf5a39b AC |
384 | |
385 | Generate_Reference_To_Formals (Designator); | |
361effb1 | 386 | Check_Eliminated (Designator); |
0f1a6a0b | 387 | Analyze_Aspect_Specifications (N, Designator, AS); |
996ae0b0 RK |
388 | end Analyze_Abstract_Subprogram_Declaration; |
389 | ||
ec4867fa ES |
390 | ---------------------------------------- |
391 | -- Analyze_Extended_Return_Statement -- | |
392 | ---------------------------------------- | |
393 | ||
394 | procedure Analyze_Extended_Return_Statement (N : Node_Id) is | |
395 | begin | |
5d37ba92 | 396 | Analyze_Return_Statement (N); |
ec4867fa ES |
397 | end Analyze_Extended_Return_Statement; |
398 | ||
996ae0b0 RK |
399 | ---------------------------- |
400 | -- Analyze_Function_Call -- | |
401 | ---------------------------- | |
402 | ||
403 | procedure Analyze_Function_Call (N : Node_Id) is | |
404 | P : constant Node_Id := Name (N); | |
405 | L : constant List_Id := Parameter_Associations (N); | |
406 | Actual : Node_Id; | |
407 | ||
408 | begin | |
409 | Analyze (P); | |
410 | ||
82c80734 | 411 | -- A call of the form A.B (X) may be an Ada05 call, which is rewritten |
e660dbf7 | 412 | -- as B (A, X). If the rewriting is successful, the call has been |
82c80734 RD |
413 | -- analyzed and we just return. |
414 | ||
415 | if Nkind (P) = N_Selected_Component | |
416 | and then Name (N) /= P | |
417 | and then Is_Rewrite_Substitution (N) | |
418 | and then Present (Etype (N)) | |
419 | then | |
420 | return; | |
421 | end if; | |
422 | ||
996ae0b0 RK |
423 | -- If error analyzing name, then set Any_Type as result type and return |
424 | ||
425 | if Etype (P) = Any_Type then | |
426 | Set_Etype (N, Any_Type); | |
427 | return; | |
428 | end if; | |
429 | ||
430 | -- Otherwise analyze the parameters | |
431 | ||
432 | if Present (L) then | |
433 | Actual := First (L); | |
996ae0b0 RK |
434 | while Present (Actual) loop |
435 | Analyze (Actual); | |
436 | Check_Parameterless_Call (Actual); | |
437 | Next (Actual); | |
438 | end loop; | |
439 | end if; | |
440 | ||
441 | Analyze_Call (N); | |
996ae0b0 RK |
442 | end Analyze_Function_Call; |
443 | ||
ec4867fa ES |
444 | ----------------------------- |
445 | -- Analyze_Function_Return -- | |
446 | ----------------------------- | |
447 | ||
448 | procedure Analyze_Function_Return (N : Node_Id) is | |
449 | Loc : constant Source_Ptr := Sloc (N); | |
450 | Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); | |
451 | Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); | |
452 | ||
5d37ba92 | 453 | R_Type : constant Entity_Id := Etype (Scope_Id); |
ec4867fa ES |
454 | -- Function result subtype |
455 | ||
456 | procedure Check_Limited_Return (Expr : Node_Id); | |
457 | -- Check the appropriate (Ada 95 or Ada 2005) rules for returning | |
458 | -- limited types. Used only for simple return statements. | |
459 | -- Expr is the expression returned. | |
460 | ||
461 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); | |
462 | -- Check that the return_subtype_indication properly matches the result | |
463 | -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). | |
464 | ||
465 | -------------------------- | |
466 | -- Check_Limited_Return -- | |
467 | -------------------------- | |
468 | ||
469 | procedure Check_Limited_Return (Expr : Node_Id) is | |
470 | begin | |
471 | -- Ada 2005 (AI-318-02): Return-by-reference types have been | |
472 | -- removed and replaced by anonymous access results. This is an | |
473 | -- incompatibility with Ada 95. Not clear whether this should be | |
474 | -- enforced yet or perhaps controllable with special switch. ??? | |
475 | ||
476 | if Is_Limited_Type (R_Type) | |
477 | and then Comes_From_Source (N) | |
478 | and then not In_Instance_Body | |
2a31c32b | 479 | and then not OK_For_Limited_Init_In_05 (R_Type, Expr) |
ec4867fa ES |
480 | then |
481 | -- Error in Ada 2005 | |
482 | ||
0791fbe9 | 483 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
484 | and then not Debug_Flag_Dot_L |
485 | and then not GNAT_Mode | |
486 | then | |
487 | Error_Msg_N | |
488 | ("(Ada 2005) cannot copy object of a limited type " & | |
5d37ba92 | 489 | "(RM-2005 6.5(5.5/2))", Expr); |
e0ae93e2 | 490 | |
40f07b4b | 491 | if Is_Immutably_Limited_Type (R_Type) then |
ec4867fa ES |
492 | Error_Msg_N |
493 | ("\return by reference not permitted in Ada 2005", Expr); | |
494 | end if; | |
495 | ||
496 | -- Warn in Ada 95 mode, to give folks a heads up about this | |
497 | -- incompatibility. | |
498 | ||
499 | -- In GNAT mode, this is just a warning, to allow it to be | |
500 | -- evilly turned off. Otherwise it is a real error. | |
501 | ||
9694c039 AC |
502 | -- In a generic context, simplify the warning because it makes |
503 | -- no sense to discuss pass-by-reference or copy. | |
504 | ||
ec4867fa | 505 | elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then |
9694c039 AC |
506 | if Inside_A_Generic then |
507 | Error_Msg_N | |
20261dc1 AC |
508 | ("return of limited object not permitted in Ada2005 " |
509 | & "(RM-2005 6.5(5.5/2))?", Expr); | |
9694c039 AC |
510 | |
511 | elsif Is_Immutably_Limited_Type (R_Type) then | |
ec4867fa | 512 | Error_Msg_N |
20261dc1 AC |
513 | ("return by reference not permitted in Ada 2005 " |
514 | & "(RM-2005 6.5(5.5/2))?", Expr); | |
ec4867fa ES |
515 | else |
516 | Error_Msg_N | |
20261dc1 AC |
517 | ("cannot copy object of a limited type in Ada 2005 " |
518 | & "(RM-2005 6.5(5.5/2))?", Expr); | |
ec4867fa ES |
519 | end if; |
520 | ||
521 | -- Ada 95 mode, compatibility warnings disabled | |
522 | ||
523 | else | |
524 | return; -- skip continuation messages below | |
525 | end if; | |
526 | ||
9694c039 AC |
527 | if not Inside_A_Generic then |
528 | Error_Msg_N | |
529 | ("\consider switching to return of access type", Expr); | |
530 | Explain_Limited_Type (R_Type, Expr); | |
531 | end if; | |
ec4867fa ES |
532 | end if; |
533 | end Check_Limited_Return; | |
534 | ||
535 | ------------------------------------- | |
536 | -- Check_Return_Subtype_Indication -- | |
537 | ------------------------------------- | |
538 | ||
539 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is | |
7665e4bd AC |
540 | Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); |
541 | ||
542 | R_Stm_Type : constant Entity_Id := Etype (Return_Obj); | |
543 | -- Subtype given in the extended return statement (must match R_Type) | |
ec4867fa ES |
544 | |
545 | Subtype_Ind : constant Node_Id := | |
546 | Object_Definition (Original_Node (Obj_Decl)); | |
547 | ||
548 | R_Type_Is_Anon_Access : | |
549 | constant Boolean := | |
550 | Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type | |
551 | or else | |
552 | Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type | |
553 | or else | |
554 | Ekind (R_Type) = E_Anonymous_Access_Type; | |
555 | -- True if return type of the function is an anonymous access type | |
556 | -- Can't we make Is_Anonymous_Access_Type in einfo ??? | |
557 | ||
558 | R_Stm_Type_Is_Anon_Access : | |
559 | constant Boolean := | |
0a36105d | 560 | Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type |
ec4867fa | 561 | or else |
0a36105d | 562 | Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type |
ec4867fa | 563 | or else |
0a36105d | 564 | Ekind (R_Stm_Type) = E_Anonymous_Access_Type; |
ec4867fa ES |
565 | -- True if type of the return object is an anonymous access type |
566 | ||
567 | begin | |
7665e4bd | 568 | -- First, avoid cascaded errors |
ec4867fa ES |
569 | |
570 | if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then | |
571 | return; | |
572 | end if; | |
573 | ||
574 | -- "return access T" case; check that the return statement also has | |
575 | -- "access T", and that the subtypes statically match: | |
53cf4600 | 576 | -- if this is an access to subprogram the signatures must match. |
ec4867fa ES |
577 | |
578 | if R_Type_Is_Anon_Access then | |
579 | if R_Stm_Type_Is_Anon_Access then | |
53cf4600 ES |
580 | if |
581 | Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type | |
0a36105d | 582 | then |
53cf4600 ES |
583 | if Base_Type (Designated_Type (R_Stm_Type)) /= |
584 | Base_Type (Designated_Type (R_Type)) | |
585 | or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) | |
586 | then | |
587 | Error_Msg_N | |
588 | ("subtype must statically match function result subtype", | |
589 | Subtype_Mark (Subtype_Ind)); | |
590 | end if; | |
591 | ||
592 | else | |
593 | -- For two anonymous access to subprogram types, the | |
594 | -- types themselves must be type conformant. | |
595 | ||
596 | if not Conforming_Types | |
597 | (R_Stm_Type, R_Type, Fully_Conformant) | |
598 | then | |
599 | Error_Msg_N | |
600 | ("subtype must statically match function result subtype", | |
601 | Subtype_Ind); | |
602 | end if; | |
ec4867fa | 603 | end if; |
0a36105d | 604 | |
ec4867fa ES |
605 | else |
606 | Error_Msg_N ("must use anonymous access type", Subtype_Ind); | |
607 | end if; | |
608 | ||
81d93365 AC |
609 | -- Subtype indication case: check that the return object's type is |
610 | -- covered by the result type, and that the subtypes statically match | |
611 | -- when the result subtype is constrained. Also handle record types | |
612 | -- with unknown discriminants for which we have built the underlying | |
613 | -- record view. Coverage is needed to allow specific-type return | |
614 | -- objects when the result type is class-wide (see AI05-32). | |
615 | ||
616 | elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) | |
9013065b | 617 | or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) |
212863c0 AC |
618 | and then |
619 | Covers | |
620 | (Base_Type (R_Type), | |
621 | Underlying_Record_View (Base_Type (R_Stm_Type)))) | |
9013065b AC |
622 | then |
623 | -- A null exclusion may be present on the return type, on the | |
624 | -- function specification, on the object declaration or on the | |
625 | -- subtype itself. | |
ec4867fa | 626 | |
21d27997 RD |
627 | if Is_Access_Type (R_Type) |
628 | and then | |
629 | (Can_Never_Be_Null (R_Type) | |
630 | or else Null_Exclusion_Present (Parent (Scope_Id))) /= | |
631 | Can_Never_Be_Null (R_Stm_Type) | |
632 | then | |
633 | Error_Msg_N | |
634 | ("subtype must statically match function result subtype", | |
635 | Subtype_Ind); | |
636 | end if; | |
637 | ||
105b5e65 | 638 | -- AI05-103: for elementary types, subtypes must statically match |
8779dffa AC |
639 | |
640 | if Is_Constrained (R_Type) | |
641 | or else Is_Access_Type (R_Type) | |
642 | then | |
ec4867fa ES |
643 | if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then |
644 | Error_Msg_N | |
0a36105d JM |
645 | ("subtype must statically match function result subtype", |
646 | Subtype_Ind); | |
ec4867fa ES |
647 | end if; |
648 | end if; | |
649 | ||
ff7139c3 AC |
650 | elsif Etype (Base_Type (R_Type)) = R_Stm_Type |
651 | and then Is_Null_Extension (Base_Type (R_Type)) | |
652 | then | |
653 | null; | |
654 | ||
ec4867fa ES |
655 | else |
656 | Error_Msg_N | |
657 | ("wrong type for return_subtype_indication", Subtype_Ind); | |
658 | end if; | |
659 | end Check_Return_Subtype_Indication; | |
660 | ||
661 | --------------------- | |
662 | -- Local Variables -- | |
663 | --------------------- | |
664 | ||
665 | Expr : Node_Id; | |
666 | ||
667 | -- Start of processing for Analyze_Function_Return | |
668 | ||
669 | begin | |
670 | Set_Return_Present (Scope_Id); | |
671 | ||
5d37ba92 | 672 | if Nkind (N) = N_Simple_Return_Statement then |
ec4867fa ES |
673 | Expr := Expression (N); |
674 | Analyze_And_Resolve (Expr, R_Type); | |
675 | Check_Limited_Return (Expr); | |
676 | ||
677 | else | |
678 | -- Analyze parts specific to extended_return_statement: | |
679 | ||
680 | declare | |
681 | Obj_Decl : constant Node_Id := | |
682 | Last (Return_Object_Declarations (N)); | |
683 | ||
684 | HSS : constant Node_Id := Handled_Statement_Sequence (N); | |
685 | ||
686 | begin | |
687 | Expr := Expression (Obj_Decl); | |
688 | ||
689 | -- Note: The check for OK_For_Limited_Init will happen in | |
690 | -- Analyze_Object_Declaration; we treat it as a normal | |
691 | -- object declaration. | |
692 | ||
cd1c668b | 693 | Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); |
ec4867fa ES |
694 | Analyze (Obj_Decl); |
695 | ||
ec4867fa ES |
696 | Check_Return_Subtype_Indication (Obj_Decl); |
697 | ||
698 | if Present (HSS) then | |
699 | Analyze (HSS); | |
700 | ||
701 | if Present (Exception_Handlers (HSS)) then | |
702 | ||
703 | -- ???Has_Nested_Block_With_Handler needs to be set. | |
704 | -- Probably by creating an actual N_Block_Statement. | |
705 | -- Probably in Expand. | |
706 | ||
707 | null; | |
708 | end if; | |
709 | end if; | |
710 | ||
9337aa0a AC |
711 | -- Mark the return object as referenced, since the return is an |
712 | -- implicit reference of the object. | |
713 | ||
714 | Set_Referenced (Defining_Identifier (Obj_Decl)); | |
715 | ||
ec4867fa ES |
716 | Check_References (Stm_Entity); |
717 | end; | |
718 | end if; | |
719 | ||
21d27997 | 720 | -- Case of Expr present |
5d37ba92 | 721 | |
ec4867fa | 722 | if Present (Expr) |
21d27997 RD |
723 | |
724 | -- Defend against previous errors | |
725 | ||
726 | and then Nkind (Expr) /= N_Empty | |
5d37ba92 | 727 | and then Present (Etype (Expr)) |
ec4867fa | 728 | then |
5d37ba92 ES |
729 | -- Apply constraint check. Note that this is done before the implicit |
730 | -- conversion of the expression done for anonymous access types to | |
f3d57416 | 731 | -- ensure correct generation of the null-excluding check associated |
5d37ba92 ES |
732 | -- with null-excluding expressions found in return statements. |
733 | ||
734 | Apply_Constraint_Check (Expr, R_Type); | |
735 | ||
736 | -- Ada 2005 (AI-318-02): When the result type is an anonymous access | |
737 | -- type, apply an implicit conversion of the expression to that type | |
738 | -- to force appropriate static and run-time accessibility checks. | |
ec4867fa | 739 | |
0791fbe9 | 740 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
741 | and then Ekind (R_Type) = E_Anonymous_Access_Type |
742 | then | |
743 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); | |
744 | Analyze_And_Resolve (Expr, R_Type); | |
745 | end if; | |
746 | ||
21d27997 RD |
747 | -- If the result type is class-wide, then check that the return |
748 | -- expression's type is not declared at a deeper level than the | |
749 | -- function (RM05-6.5(5.6/2)). | |
750 | ||
0791fbe9 | 751 | if Ada_Version >= Ada_2005 |
21d27997 RD |
752 | and then Is_Class_Wide_Type (R_Type) |
753 | then | |
754 | if Type_Access_Level (Etype (Expr)) > | |
755 | Subprogram_Access_Level (Scope_Id) | |
756 | then | |
757 | Error_Msg_N | |
758 | ("level of return expression type is deeper than " & | |
759 | "class-wide function!", Expr); | |
760 | end if; | |
761 | end if; | |
762 | ||
4755cce9 JM |
763 | -- Check incorrect use of dynamically tagged expression |
764 | ||
765 | if Is_Tagged_Type (R_Type) then | |
766 | Check_Dynamically_Tagged_Expression | |
767 | (Expr => Expr, | |
768 | Typ => R_Type, | |
769 | Related_Nod => N); | |
ec4867fa ES |
770 | end if; |
771 | ||
ec4867fa ES |
772 | -- ??? A real run-time accessibility check is needed in cases |
773 | -- involving dereferences of access parameters. For now we just | |
774 | -- check the static cases. | |
775 | ||
0791fbe9 | 776 | if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) |
40f07b4b | 777 | and then Is_Immutably_Limited_Type (Etype (Scope_Id)) |
ec4867fa ES |
778 | and then Object_Access_Level (Expr) > |
779 | Subprogram_Access_Level (Scope_Id) | |
780 | then | |
ec4867fa | 781 | |
9694c039 AC |
782 | -- Suppress the message in a generic, where the rewriting |
783 | -- is irrelevant. | |
784 | ||
785 | if Inside_A_Generic then | |
786 | null; | |
787 | ||
788 | else | |
789 | Rewrite (N, | |
790 | Make_Raise_Program_Error (Loc, | |
791 | Reason => PE_Accessibility_Check_Failed)); | |
792 | Analyze (N); | |
793 | ||
794 | Error_Msg_N | |
795 | ("cannot return a local value by reference?", N); | |
796 | Error_Msg_NE | |
797 | ("\& will be raised at run time?", | |
798 | N, Standard_Program_Error); | |
799 | end if; | |
ec4867fa | 800 | end if; |
5d37ba92 ES |
801 | |
802 | if Known_Null (Expr) | |
803 | and then Nkind (Parent (Scope_Id)) = N_Function_Specification | |
804 | and then Null_Exclusion_Present (Parent (Scope_Id)) | |
805 | then | |
806 | Apply_Compile_Time_Constraint_Error | |
807 | (N => Expr, | |
808 | Msg => "(Ada 2005) null not allowed for " | |
809 | & "null-excluding return?", | |
810 | Reason => CE_Null_Not_Allowed); | |
811 | end if; | |
cd5a9750 AC |
812 | |
813 | -- Apply checks suggested by AI05-0144 (dangerous order dependence) | |
814 | -- (Disabled for now) | |
815 | ||
816 | -- Check_Order_Dependence; | |
ec4867fa ES |
817 | end if; |
818 | end Analyze_Function_Return; | |
819 | ||
996ae0b0 RK |
820 | ------------------------------------- |
821 | -- Analyze_Generic_Subprogram_Body -- | |
822 | ------------------------------------- | |
823 | ||
824 | procedure Analyze_Generic_Subprogram_Body | |
825 | (N : Node_Id; | |
826 | Gen_Id : Entity_Id) | |
827 | is | |
fbf5a39b | 828 | Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); |
996ae0b0 | 829 | Kind : constant Entity_Kind := Ekind (Gen_Id); |
fbf5a39b | 830 | Body_Id : Entity_Id; |
996ae0b0 | 831 | New_N : Node_Id; |
fbf5a39b | 832 | Spec : Node_Id; |
996ae0b0 RK |
833 | |
834 | begin | |
82c80734 RD |
835 | -- Copy body and disable expansion while analyzing the generic For a |
836 | -- stub, do not copy the stub (which would load the proper body), this | |
837 | -- will be done when the proper body is analyzed. | |
996ae0b0 RK |
838 | |
839 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
840 | New_N := Copy_Generic_Node (N, Empty, Instantiating => False); | |
841 | Rewrite (N, New_N); | |
842 | Start_Generic; | |
843 | end if; | |
844 | ||
845 | Spec := Specification (N); | |
846 | ||
847 | -- Within the body of the generic, the subprogram is callable, and | |
848 | -- behaves like the corresponding non-generic unit. | |
849 | ||
fbf5a39b | 850 | Body_Id := Defining_Entity (Spec); |
996ae0b0 RK |
851 | |
852 | if Kind = E_Generic_Procedure | |
853 | and then Nkind (Spec) /= N_Procedure_Specification | |
854 | then | |
fbf5a39b | 855 | Error_Msg_N ("invalid body for generic procedure ", Body_Id); |
996ae0b0 RK |
856 | return; |
857 | ||
858 | elsif Kind = E_Generic_Function | |
859 | and then Nkind (Spec) /= N_Function_Specification | |
860 | then | |
fbf5a39b | 861 | Error_Msg_N ("invalid body for generic function ", Body_Id); |
996ae0b0 RK |
862 | return; |
863 | end if; | |
864 | ||
fbf5a39b | 865 | Set_Corresponding_Body (Gen_Decl, Body_Id); |
996ae0b0 RK |
866 | |
867 | if Has_Completion (Gen_Id) | |
868 | and then Nkind (Parent (N)) /= N_Subunit | |
869 | then | |
870 | Error_Msg_N ("duplicate generic body", N); | |
871 | return; | |
872 | else | |
873 | Set_Has_Completion (Gen_Id); | |
874 | end if; | |
875 | ||
876 | if Nkind (N) = N_Subprogram_Body_Stub then | |
877 | Set_Ekind (Defining_Entity (Specification (N)), Kind); | |
878 | else | |
879 | Set_Corresponding_Spec (N, Gen_Id); | |
880 | end if; | |
881 | ||
882 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
883 | Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); | |
884 | end if; | |
885 | ||
886 | -- Make generic parameters immediately visible in the body. They are | |
887 | -- needed to process the formals declarations. Then make the formals | |
888 | -- visible in a separate step. | |
889 | ||
0a36105d | 890 | Push_Scope (Gen_Id); |
996ae0b0 RK |
891 | |
892 | declare | |
893 | E : Entity_Id; | |
894 | First_Ent : Entity_Id; | |
895 | ||
896 | begin | |
897 | First_Ent := First_Entity (Gen_Id); | |
898 | ||
899 | E := First_Ent; | |
900 | while Present (E) and then not Is_Formal (E) loop | |
901 | Install_Entity (E); | |
902 | Next_Entity (E); | |
903 | end loop; | |
904 | ||
905 | Set_Use (Generic_Formal_Declarations (Gen_Decl)); | |
906 | ||
907 | -- Now generic formals are visible, and the specification can be | |
908 | -- analyzed, for subsequent conformance check. | |
909 | ||
fbf5a39b | 910 | Body_Id := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 911 | |
fbf5a39b | 912 | -- Make formal parameters visible |
996ae0b0 RK |
913 | |
914 | if Present (E) then | |
915 | ||
fbf5a39b AC |
916 | -- E is the first formal parameter, we loop through the formals |
917 | -- installing them so that they will be visible. | |
996ae0b0 RK |
918 | |
919 | Set_First_Entity (Gen_Id, E); | |
996ae0b0 RK |
920 | while Present (E) loop |
921 | Install_Entity (E); | |
922 | Next_Formal (E); | |
923 | end loop; | |
924 | end if; | |
925 | ||
e895b435 | 926 | -- Visible generic entity is callable within its own body |
996ae0b0 | 927 | |
ec4867fa ES |
928 | Set_Ekind (Gen_Id, Ekind (Body_Id)); |
929 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
930 | Set_Convention (Body_Id, Convention (Gen_Id)); | |
931 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); | |
932 | Set_Scope (Body_Id, Scope (Gen_Id)); | |
fbf5a39b AC |
933 | Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); |
934 | ||
935 | if Nkind (N) = N_Subprogram_Body_Stub then | |
936 | ||
e895b435 | 937 | -- No body to analyze, so restore state of generic unit |
fbf5a39b AC |
938 | |
939 | Set_Ekind (Gen_Id, Kind); | |
940 | Set_Ekind (Body_Id, Kind); | |
941 | ||
942 | if Present (First_Ent) then | |
943 | Set_First_Entity (Gen_Id, First_Ent); | |
944 | end if; | |
945 | ||
946 | End_Scope; | |
947 | return; | |
948 | end if; | |
996ae0b0 | 949 | |
82c80734 RD |
950 | -- If this is a compilation unit, it must be made visible explicitly, |
951 | -- because the compilation of the declaration, unlike other library | |
952 | -- unit declarations, does not. If it is not a unit, the following | |
953 | -- is redundant but harmless. | |
996ae0b0 RK |
954 | |
955 | Set_Is_Immediately_Visible (Gen_Id); | |
fbf5a39b | 956 | Reference_Body_Formals (Gen_Id, Body_Id); |
996ae0b0 | 957 | |
ec4867fa ES |
958 | if Is_Child_Unit (Gen_Id) then |
959 | Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); | |
960 | end if; | |
961 | ||
996ae0b0 | 962 | Set_Actual_Subtypes (N, Current_Scope); |
0dabde3a ES |
963 | Process_PPCs (N, Gen_Id, Body_Id); |
964 | ||
965 | -- If the generic unit carries pre- or post-conditions, copy them | |
966 | -- to the original generic tree, so that they are properly added | |
967 | -- to any instantiation. | |
968 | ||
969 | declare | |
970 | Orig : constant Node_Id := Original_Node (N); | |
971 | Cond : Node_Id; | |
972 | ||
973 | begin | |
974 | Cond := First (Declarations (N)); | |
975 | while Present (Cond) loop | |
976 | if Nkind (Cond) = N_Pragma | |
977 | and then Pragma_Name (Cond) = Name_Check | |
978 | then | |
979 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
980 | ||
981 | elsif Nkind (Cond) = N_Pragma | |
982 | and then Pragma_Name (Cond) = Name_Postcondition | |
983 | then | |
984 | Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id)); | |
985 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
986 | else | |
987 | exit; | |
988 | end if; | |
989 | ||
990 | Next (Cond); | |
991 | end loop; | |
992 | end; | |
993 | ||
996ae0b0 RK |
994 | Analyze_Declarations (Declarations (N)); |
995 | Check_Completion; | |
996 | Analyze (Handled_Statement_Sequence (N)); | |
997 | ||
998 | Save_Global_References (Original_Node (N)); | |
999 | ||
82c80734 RD |
1000 | -- Prior to exiting the scope, include generic formals again (if any |
1001 | -- are present) in the set of local entities. | |
996ae0b0 RK |
1002 | |
1003 | if Present (First_Ent) then | |
1004 | Set_First_Entity (Gen_Id, First_Ent); | |
1005 | end if; | |
1006 | ||
fbf5a39b | 1007 | Check_References (Gen_Id); |
996ae0b0 RK |
1008 | end; |
1009 | ||
e6f69614 | 1010 | Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); |
996ae0b0 RK |
1011 | End_Scope; |
1012 | Check_Subprogram_Order (N); | |
1013 | ||
e895b435 | 1014 | -- Outside of its body, unit is generic again |
996ae0b0 RK |
1015 | |
1016 | Set_Ekind (Gen_Id, Kind); | |
fbf5a39b | 1017 | Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); |
5d37ba92 ES |
1018 | |
1019 | if Style_Check then | |
1020 | Style.Check_Identifier (Body_Id, Gen_Id); | |
1021 | end if; | |
13d923cc | 1022 | |
996ae0b0 | 1023 | End_Generic; |
996ae0b0 RK |
1024 | end Analyze_Generic_Subprogram_Body; |
1025 | ||
1026 | ----------------------------- | |
1027 | -- Analyze_Operator_Symbol -- | |
1028 | ----------------------------- | |
1029 | ||
82c80734 RD |
1030 | -- An operator symbol such as "+" or "and" may appear in context where the |
1031 | -- literal denotes an entity name, such as "+"(x, y) or in context when it | |
1032 | -- is just a string, as in (conjunction = "or"). In these cases the parser | |
1033 | -- generates this node, and the semantics does the disambiguation. Other | |
1034 | -- such case are actuals in an instantiation, the generic unit in an | |
1035 | -- instantiation, and pragma arguments. | |
996ae0b0 RK |
1036 | |
1037 | procedure Analyze_Operator_Symbol (N : Node_Id) is | |
1038 | Par : constant Node_Id := Parent (N); | |
1039 | ||
1040 | begin | |
800621e0 RD |
1041 | if (Nkind (Par) = N_Function_Call |
1042 | and then N = Name (Par)) | |
996ae0b0 | 1043 | or else Nkind (Par) = N_Function_Instantiation |
800621e0 RD |
1044 | or else (Nkind (Par) = N_Indexed_Component |
1045 | and then N = Prefix (Par)) | |
996ae0b0 RK |
1046 | or else (Nkind (Par) = N_Pragma_Argument_Association |
1047 | and then not Is_Pragma_String_Literal (Par)) | |
1048 | or else Nkind (Par) = N_Subprogram_Renaming_Declaration | |
800621e0 RD |
1049 | or else (Nkind (Par) = N_Attribute_Reference |
1050 | and then Attribute_Name (Par) /= Name_Value) | |
996ae0b0 RK |
1051 | then |
1052 | Find_Direct_Name (N); | |
1053 | ||
1054 | else | |
1055 | Change_Operator_Symbol_To_String_Literal (N); | |
1056 | Analyze (N); | |
1057 | end if; | |
1058 | end Analyze_Operator_Symbol; | |
1059 | ||
1060 | ----------------------------------- | |
1061 | -- Analyze_Parameter_Association -- | |
1062 | ----------------------------------- | |
1063 | ||
1064 | procedure Analyze_Parameter_Association (N : Node_Id) is | |
1065 | begin | |
1066 | Analyze (Explicit_Actual_Parameter (N)); | |
1067 | end Analyze_Parameter_Association; | |
1068 | ||
dcffd515 RD |
1069 | -------------------------------------- |
1070 | -- Analyze_Parameterized_Expression -- | |
1071 | -------------------------------------- | |
ad110ee8 | 1072 | |
dcffd515 | 1073 | procedure Analyze_Parameterized_Expression (N : Node_Id) is |
ad110ee8 RD |
1074 | Loc : constant Source_Ptr := Sloc (N); |
1075 | LocX : constant Source_Ptr := Sloc (Expression (N)); | |
1076 | ||
1077 | begin | |
1078 | -- This is one of the occasions on which we write things during semantic | |
dcffd515 | 1079 | -- analysis. Transform the parameterized expression into an equivalent |
ad110ee8 RD |
1080 | -- subprogram body, and then analyze that. |
1081 | ||
1082 | Rewrite (N, | |
1083 | Make_Subprogram_Body (Loc, | |
1084 | Specification => Specification (N), | |
1085 | Declarations => Empty_List, | |
1086 | Handled_Statement_Sequence => | |
1087 | Make_Handled_Sequence_Of_Statements (LocX, | |
1088 | Statements => New_List ( | |
1089 | Make_Simple_Return_Statement (LocX, | |
1090 | Expression => Expression (N)))))); | |
1091 | Analyze (N); | |
dcffd515 | 1092 | end Analyze_Parameterized_Expression; |
ad110ee8 | 1093 | |
996ae0b0 RK |
1094 | ---------------------------- |
1095 | -- Analyze_Procedure_Call -- | |
1096 | ---------------------------- | |
1097 | ||
1098 | procedure Analyze_Procedure_Call (N : Node_Id) is | |
1099 | Loc : constant Source_Ptr := Sloc (N); | |
1100 | P : constant Node_Id := Name (N); | |
1101 | Actuals : constant List_Id := Parameter_Associations (N); | |
1102 | Actual : Node_Id; | |
1103 | New_N : Node_Id; | |
1104 | ||
1105 | procedure Analyze_Call_And_Resolve; | |
1106 | -- Do Analyze and Resolve calls for procedure call | |
cd5a9750 | 1107 | -- At end, check illegal order dependence. |
996ae0b0 | 1108 | |
fbf5a39b AC |
1109 | ------------------------------ |
1110 | -- Analyze_Call_And_Resolve -- | |
1111 | ------------------------------ | |
1112 | ||
996ae0b0 RK |
1113 | procedure Analyze_Call_And_Resolve is |
1114 | begin | |
1115 | if Nkind (N) = N_Procedure_Call_Statement then | |
1116 | Analyze_Call (N); | |
1117 | Resolve (N, Standard_Void_Type); | |
cd5a9750 AC |
1118 | |
1119 | -- Apply checks suggested by AI05-0144 (Disabled for now) | |
1120 | ||
1121 | -- Check_Order_Dependence; | |
1122 | ||
996ae0b0 RK |
1123 | else |
1124 | Analyze (N); | |
1125 | end if; | |
1126 | end Analyze_Call_And_Resolve; | |
1127 | ||
1128 | -- Start of processing for Analyze_Procedure_Call | |
1129 | ||
1130 | begin | |
1131 | -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote | |
1132 | -- a procedure call or an entry call. The prefix may denote an access | |
1133 | -- to subprogram type, in which case an implicit dereference applies. | |
f3d57416 | 1134 | -- If the prefix is an indexed component (without implicit dereference) |
996ae0b0 RK |
1135 | -- then the construct denotes a call to a member of an entire family. |
1136 | -- If the prefix is a simple name, it may still denote a call to a | |
1137 | -- parameterless member of an entry family. Resolution of these various | |
1138 | -- interpretations is delicate. | |
1139 | ||
1140 | Analyze (P); | |
1141 | ||
758c442c GD |
1142 | -- If this is a call of the form Obj.Op, the call may have been |
1143 | -- analyzed and possibly rewritten into a block, in which case | |
1144 | -- we are done. | |
1145 | ||
1146 | if Analyzed (N) then | |
1147 | return; | |
1148 | end if; | |
1149 | ||
7415029d AC |
1150 | -- If there is an error analyzing the name (which may have been |
1151 | -- rewritten if the original call was in prefix notation) then error | |
1152 | -- has been emitted already, mark node and return. | |
996ae0b0 | 1153 | |
7415029d AC |
1154 | if Error_Posted (N) |
1155 | or else Etype (Name (N)) = Any_Type | |
1156 | then | |
996ae0b0 RK |
1157 | Set_Etype (N, Any_Type); |
1158 | return; | |
1159 | end if; | |
1160 | ||
1161 | -- Otherwise analyze the parameters | |
1162 | ||
1163 | if Present (Actuals) then | |
1164 | Actual := First (Actuals); | |
1165 | ||
1166 | while Present (Actual) loop | |
1167 | Analyze (Actual); | |
1168 | Check_Parameterless_Call (Actual); | |
1169 | Next (Actual); | |
1170 | end loop; | |
1171 | end if; | |
1172 | ||
1173 | -- Special processing for Elab_Spec and Elab_Body calls | |
1174 | ||
1175 | if Nkind (P) = N_Attribute_Reference | |
1176 | and then (Attribute_Name (P) = Name_Elab_Spec | |
1177 | or else Attribute_Name (P) = Name_Elab_Body) | |
1178 | then | |
1179 | if Present (Actuals) then | |
1180 | Error_Msg_N | |
1181 | ("no parameters allowed for this call", First (Actuals)); | |
1182 | return; | |
1183 | end if; | |
1184 | ||
1185 | Set_Etype (N, Standard_Void_Type); | |
1186 | Set_Analyzed (N); | |
1187 | ||
1188 | elsif Is_Entity_Name (P) | |
1189 | and then Is_Record_Type (Etype (Entity (P))) | |
1190 | and then Remote_AST_I_Dereference (P) | |
1191 | then | |
1192 | return; | |
1193 | ||
1194 | elsif Is_Entity_Name (P) | |
1195 | and then Ekind (Entity (P)) /= E_Entry_Family | |
1196 | then | |
1197 | if Is_Access_Type (Etype (P)) | |
1198 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1199 | and then No (Actuals) | |
1200 | and then Comes_From_Source (N) | |
1201 | then | |
ed2233dc | 1202 | Error_Msg_N ("missing explicit dereference in call", N); |
996ae0b0 RK |
1203 | end if; |
1204 | ||
1205 | Analyze_Call_And_Resolve; | |
1206 | ||
1207 | -- If the prefix is the simple name of an entry family, this is | |
1208 | -- a parameterless call from within the task body itself. | |
1209 | ||
1210 | elsif Is_Entity_Name (P) | |
1211 | and then Nkind (P) = N_Identifier | |
1212 | and then Ekind (Entity (P)) = E_Entry_Family | |
1213 | and then Present (Actuals) | |
1214 | and then No (Next (First (Actuals))) | |
1215 | then | |
82c80734 RD |
1216 | -- Can be call to parameterless entry family. What appears to be the |
1217 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1218 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1219 | -- transformation. |
1220 | ||
1221 | New_N := | |
1222 | Make_Indexed_Component (Loc, | |
1223 | Prefix => | |
1224 | Make_Selected_Component (Loc, | |
1225 | Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc), | |
1226 | Selector_Name => New_Occurrence_Of (Entity (P), Loc)), | |
1227 | Expressions => Actuals); | |
1228 | Set_Name (N, New_N); | |
1229 | Set_Etype (New_N, Standard_Void_Type); | |
1230 | Set_Parameter_Associations (N, No_List); | |
1231 | Analyze_Call_And_Resolve; | |
1232 | ||
1233 | elsif Nkind (P) = N_Explicit_Dereference then | |
1234 | if Ekind (Etype (P)) = E_Subprogram_Type then | |
1235 | Analyze_Call_And_Resolve; | |
1236 | else | |
1237 | Error_Msg_N ("expect access to procedure in call", P); | |
1238 | end if; | |
1239 | ||
82c80734 RD |
1240 | -- The name can be a selected component or an indexed component that |
1241 | -- yields an access to subprogram. Such a prefix is legal if the call | |
1242 | -- has parameter associations. | |
996ae0b0 RK |
1243 | |
1244 | elsif Is_Access_Type (Etype (P)) | |
1245 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1246 | then | |
1247 | if Present (Actuals) then | |
1248 | Analyze_Call_And_Resolve; | |
1249 | else | |
ed2233dc | 1250 | Error_Msg_N ("missing explicit dereference in call ", N); |
996ae0b0 RK |
1251 | end if; |
1252 | ||
82c80734 RD |
1253 | -- If not an access to subprogram, then the prefix must resolve to the |
1254 | -- name of an entry, entry family, or protected operation. | |
996ae0b0 | 1255 | |
82c80734 RD |
1256 | -- For the case of a simple entry call, P is a selected component where |
1257 | -- the prefix is the task and the selector name is the entry. A call to | |
1258 | -- a protected procedure will have the same syntax. If the protected | |
1259 | -- object contains overloaded operations, the entity may appear as a | |
1260 | -- function, the context will select the operation whose type is Void. | |
996ae0b0 RK |
1261 | |
1262 | elsif Nkind (P) = N_Selected_Component | |
1263 | and then (Ekind (Entity (Selector_Name (P))) = E_Entry | |
1264 | or else | |
1265 | Ekind (Entity (Selector_Name (P))) = E_Procedure | |
1266 | or else | |
1267 | Ekind (Entity (Selector_Name (P))) = E_Function) | |
1268 | then | |
1269 | Analyze_Call_And_Resolve; | |
1270 | ||
1271 | elsif Nkind (P) = N_Selected_Component | |
1272 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family | |
1273 | and then Present (Actuals) | |
1274 | and then No (Next (First (Actuals))) | |
1275 | then | |
82c80734 RD |
1276 | -- Can be call to parameterless entry family. What appears to be the |
1277 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1278 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1279 | -- transformation. |
1280 | ||
1281 | New_N := | |
1282 | Make_Indexed_Component (Loc, | |
1283 | Prefix => New_Copy (P), | |
1284 | Expressions => Actuals); | |
1285 | Set_Name (N, New_N); | |
1286 | Set_Etype (New_N, Standard_Void_Type); | |
1287 | Set_Parameter_Associations (N, No_List); | |
1288 | Analyze_Call_And_Resolve; | |
1289 | ||
1290 | -- For the case of a reference to an element of an entry family, P is | |
1291 | -- an indexed component whose prefix is a selected component (task and | |
1292 | -- entry family), and whose index is the entry family index. | |
1293 | ||
1294 | elsif Nkind (P) = N_Indexed_Component | |
1295 | and then Nkind (Prefix (P)) = N_Selected_Component | |
1296 | and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family | |
1297 | then | |
1298 | Analyze_Call_And_Resolve; | |
1299 | ||
1300 | -- If the prefix is the name of an entry family, it is a call from | |
1301 | -- within the task body itself. | |
1302 | ||
1303 | elsif Nkind (P) = N_Indexed_Component | |
1304 | and then Nkind (Prefix (P)) = N_Identifier | |
1305 | and then Ekind (Entity (Prefix (P))) = E_Entry_Family | |
1306 | then | |
1307 | New_N := | |
1308 | Make_Selected_Component (Loc, | |
1309 | Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), | |
1310 | Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); | |
1311 | Rewrite (Prefix (P), New_N); | |
1312 | Analyze (P); | |
1313 | Analyze_Call_And_Resolve; | |
1314 | ||
e895b435 | 1315 | -- Anything else is an error |
996ae0b0 RK |
1316 | |
1317 | else | |
758c442c | 1318 | Error_Msg_N ("invalid procedure or entry call", N); |
996ae0b0 RK |
1319 | end if; |
1320 | end Analyze_Procedure_Call; | |
1321 | ||
5d37ba92 ES |
1322 | ------------------------------------- |
1323 | -- Analyze_Simple_Return_Statement -- | |
1324 | ------------------------------------- | |
ec4867fa | 1325 | |
5d37ba92 | 1326 | procedure Analyze_Simple_Return_Statement (N : Node_Id) is |
996ae0b0 | 1327 | begin |
5d37ba92 ES |
1328 | if Present (Expression (N)) then |
1329 | Mark_Coextensions (N, Expression (N)); | |
996ae0b0 RK |
1330 | end if; |
1331 | ||
5d37ba92 ES |
1332 | Analyze_Return_Statement (N); |
1333 | end Analyze_Simple_Return_Statement; | |
996ae0b0 | 1334 | |
82c80734 RD |
1335 | ------------------------- |
1336 | -- Analyze_Return_Type -- | |
1337 | ------------------------- | |
1338 | ||
1339 | procedure Analyze_Return_Type (N : Node_Id) is | |
1340 | Designator : constant Entity_Id := Defining_Entity (N); | |
1341 | Typ : Entity_Id := Empty; | |
1342 | ||
1343 | begin | |
ec4867fa ES |
1344 | -- Normal case where result definition does not indicate an error |
1345 | ||
41251c60 JM |
1346 | if Result_Definition (N) /= Error then |
1347 | if Nkind (Result_Definition (N)) = N_Access_Definition then | |
b1c11e0e JM |
1348 | |
1349 | -- Ada 2005 (AI-254): Handle anonymous access to subprograms | |
1350 | ||
1351 | declare | |
1352 | AD : constant Node_Id := | |
1353 | Access_To_Subprogram_Definition (Result_Definition (N)); | |
1354 | begin | |
1355 | if Present (AD) and then Protected_Present (AD) then | |
1356 | Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); | |
1357 | else | |
1358 | Typ := Access_Definition (N, Result_Definition (N)); | |
1359 | end if; | |
1360 | end; | |
1361 | ||
41251c60 JM |
1362 | Set_Parent (Typ, Result_Definition (N)); |
1363 | Set_Is_Local_Anonymous_Access (Typ); | |
1364 | Set_Etype (Designator, Typ); | |
1365 | ||
b66c3ff4 AC |
1366 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1367 | ||
1368 | Null_Exclusion_Static_Checks (N); | |
1369 | ||
41251c60 JM |
1370 | -- Subtype_Mark case |
1371 | ||
1372 | else | |
1373 | Find_Type (Result_Definition (N)); | |
1374 | Typ := Entity (Result_Definition (N)); | |
1375 | Set_Etype (Designator, Typ); | |
1376 | ||
b66c3ff4 AC |
1377 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1378 | ||
1379 | Null_Exclusion_Static_Checks (N); | |
1380 | ||
1381 | -- If a null exclusion is imposed on the result type, then create | |
1382 | -- a null-excluding itype (an access subtype) and use it as the | |
1383 | -- function's Etype. Note that the null exclusion checks are done | |
1384 | -- right before this, because they don't get applied to types that | |
1385 | -- do not come from source. | |
1386 | ||
1387 | if Is_Access_Type (Typ) | |
1388 | and then Null_Exclusion_Present (N) | |
1389 | then | |
1390 | Set_Etype (Designator, | |
1391 | Create_Null_Excluding_Itype | |
ff7139c3 AC |
1392 | (T => Typ, |
1393 | Related_Nod => N, | |
1394 | Scope_Id => Scope (Current_Scope))); | |
1395 | ||
1396 | -- The new subtype must be elaborated before use because | |
1397 | -- it is visible outside of the function. However its base | |
1398 | -- type may not be frozen yet, so the reference that will | |
1399 | -- force elaboration must be attached to the freezing of | |
1400 | -- the base type. | |
1401 | ||
212863c0 AC |
1402 | -- If the return specification appears on a proper body, |
1403 | -- the subtype will have been created already on the spec. | |
1404 | ||
ff7139c3 | 1405 | if Is_Frozen (Typ) then |
212863c0 AC |
1406 | if Nkind (Parent (N)) = N_Subprogram_Body |
1407 | and then Nkind (Parent (Parent (N))) = N_Subunit | |
1408 | then | |
1409 | null; | |
1410 | else | |
1411 | Build_Itype_Reference (Etype (Designator), Parent (N)); | |
1412 | end if; | |
1413 | ||
ff7139c3 AC |
1414 | else |
1415 | Ensure_Freeze_Node (Typ); | |
1416 | ||
1417 | declare | |
212863c0 | 1418 | IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); |
ff7139c3 AC |
1419 | begin |
1420 | Set_Itype (IR, Etype (Designator)); | |
1421 | Append_Freeze_Actions (Typ, New_List (IR)); | |
1422 | end; | |
1423 | end if; | |
1424 | ||
b66c3ff4 AC |
1425 | else |
1426 | Set_Etype (Designator, Typ); | |
1427 | end if; | |
1428 | ||
41251c60 | 1429 | if Ekind (Typ) = E_Incomplete_Type |
0a36105d JM |
1430 | and then Is_Value_Type (Typ) |
1431 | then | |
1432 | null; | |
1433 | ||
1434 | elsif Ekind (Typ) = E_Incomplete_Type | |
41251c60 JM |
1435 | or else (Is_Class_Wide_Type (Typ) |
1436 | and then | |
1437 | Ekind (Root_Type (Typ)) = E_Incomplete_Type) | |
1438 | then | |
dd386db0 AC |
1439 | -- AI05-0151: Tagged incomplete types are allowed in all formal |
1440 | -- parts. Untagged incomplete types are not allowed in bodies. | |
1441 | ||
1442 | if Ada_Version >= Ada_2012 then | |
1443 | if Is_Tagged_Type (Typ) then | |
1444 | null; | |
1445 | ||
1446 | elsif Nkind_In (Parent (Parent (N)), | |
1447 | N_Accept_Statement, | |
1448 | N_Entry_Body, | |
1449 | N_Subprogram_Body) | |
1450 | then | |
1451 | Error_Msg_NE | |
1452 | ("invalid use of untagged incomplete type&", | |
1453 | Designator, Typ); | |
1454 | end if; | |
1455 | ||
1456 | else | |
1457 | Error_Msg_NE | |
1458 | ("invalid use of incomplete type&", Designator, Typ); | |
1459 | end if; | |
41251c60 | 1460 | end if; |
82c80734 RD |
1461 | end if; |
1462 | ||
ec4867fa ES |
1463 | -- Case where result definition does indicate an error |
1464 | ||
82c80734 RD |
1465 | else |
1466 | Set_Etype (Designator, Any_Type); | |
1467 | end if; | |
1468 | end Analyze_Return_Type; | |
1469 | ||
996ae0b0 RK |
1470 | ----------------------------- |
1471 | -- Analyze_Subprogram_Body -- | |
1472 | ----------------------------- | |
1473 | ||
b1b543d2 BD |
1474 | procedure Analyze_Subprogram_Body (N : Node_Id) is |
1475 | Loc : constant Source_Ptr := Sloc (N); | |
1476 | Body_Spec : constant Node_Id := Specification (N); | |
1477 | Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); | |
1478 | ||
1479 | begin | |
1480 | if Debug_Flag_C then | |
1481 | Write_Str ("==> subprogram body "); | |
1482 | Write_Name (Chars (Body_Id)); | |
1483 | Write_Str (" from "); | |
1484 | Write_Location (Loc); | |
1485 | Write_Eol; | |
1486 | Indent; | |
1487 | end if; | |
1488 | ||
1489 | Trace_Scope (N, Body_Id, " Analyze subprogram: "); | |
1490 | ||
1491 | -- The real work is split out into the helper, so it can do "return;" | |
1492 | -- without skipping the debug output: | |
1493 | ||
1494 | Analyze_Subprogram_Body_Helper (N); | |
1495 | ||
1496 | if Debug_Flag_C then | |
1497 | Outdent; | |
1498 | Write_Str ("<== subprogram body "); | |
1499 | Write_Name (Chars (Body_Id)); | |
1500 | Write_Str (" from "); | |
1501 | Write_Location (Loc); | |
1502 | Write_Eol; | |
1503 | end if; | |
1504 | end Analyze_Subprogram_Body; | |
1505 | ||
1506 | ------------------------------------ | |
1507 | -- Analyze_Subprogram_Body_Helper -- | |
1508 | ------------------------------------ | |
1509 | ||
996ae0b0 RK |
1510 | -- This procedure is called for regular subprogram bodies, generic bodies, |
1511 | -- and for subprogram stubs of both kinds. In the case of stubs, only the | |
1512 | -- specification matters, and is used to create a proper declaration for | |
1513 | -- the subprogram, or to perform conformance checks. | |
1514 | ||
b1b543d2 | 1515 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is |
fbf5a39b | 1516 | Loc : constant Source_Ptr := Sloc (N); |
33931112 | 1517 | Body_Deleted : constant Boolean := False; |
fbf5a39b AC |
1518 | Body_Spec : constant Node_Id := Specification (N); |
1519 | Body_Id : Entity_Id := Defining_Entity (Body_Spec); | |
1520 | Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); | |
0868e09c | 1521 | Conformant : Boolean; |
21d27997 | 1522 | HSS : Node_Id; |
07fc65c4 | 1523 | P_Ent : Entity_Id; |
21d27997 RD |
1524 | Prot_Typ : Entity_Id := Empty; |
1525 | Spec_Id : Entity_Id; | |
1526 | Spec_Decl : Node_Id := Empty; | |
1527 | ||
1528 | Last_Real_Spec_Entity : Entity_Id := Empty; | |
1529 | -- When we analyze a separate spec, the entity chain ends up containing | |
1530 | -- the formals, as well as any itypes generated during analysis of the | |
1531 | -- default expressions for parameters, or the arguments of associated | |
1532 | -- precondition/postcondition pragmas (which are analyzed in the context | |
1533 | -- of the spec since they have visibility on formals). | |
1534 | -- | |
1535 | -- These entities belong with the spec and not the body. However we do | |
1536 | -- the analysis of the body in the context of the spec (again to obtain | |
1537 | -- visibility to the formals), and all the entities generated during | |
1538 | -- this analysis end up also chained to the entity chain of the spec. | |
1539 | -- But they really belong to the body, and there is circuitry to move | |
1540 | -- them from the spec to the body. | |
1541 | -- | |
1542 | -- However, when we do this move, we don't want to move the real spec | |
1543 | -- entities (first para above) to the body. The Last_Real_Spec_Entity | |
1544 | -- variable points to the last real spec entity, so we only move those | |
1545 | -- chained beyond that point. It is initialized to Empty to deal with | |
1546 | -- the case where there is no separate spec. | |
996ae0b0 | 1547 | |
ec4867fa | 1548 | procedure Check_Anonymous_Return; |
e50e1c5e | 1549 | -- Ada 2005: if a function returns an access type that denotes a task, |
ec4867fa ES |
1550 | -- or a type that contains tasks, we must create a master entity for |
1551 | -- the anonymous type, which typically will be used in an allocator | |
1552 | -- in the body of the function. | |
1553 | ||
e660dbf7 JM |
1554 | procedure Check_Inline_Pragma (Spec : in out Node_Id); |
1555 | -- Look ahead to recognize a pragma that may appear after the body. | |
1556 | -- If there is a previous spec, check that it appears in the same | |
1557 | -- declarative part. If the pragma is Inline_Always, perform inlining | |
1558 | -- unconditionally, otherwise only if Front_End_Inlining is requested. | |
1559 | -- If the body acts as a spec, and inlining is required, we create a | |
1560 | -- subprogram declaration for it, in order to attach the body to inline. | |
21d27997 RD |
1561 | -- If pragma does not appear after the body, check whether there is |
1562 | -- an inline pragma before any local declarations. | |
c37bb106 | 1563 | |
7665e4bd AC |
1564 | procedure Check_Missing_Return; |
1565 | -- Checks for a function with a no return statements, and also performs | |
1566 | -- the warning checks implemented by Check_Returns. | |
1567 | ||
d44202ba HK |
1568 | function Disambiguate_Spec return Entity_Id; |
1569 | -- When a primitive is declared between the private view and the full | |
1570 | -- view of a concurrent type which implements an interface, a special | |
1571 | -- mechanism is used to find the corresponding spec of the primitive | |
1572 | -- body. | |
1573 | ||
1574 | function Is_Private_Concurrent_Primitive | |
1575 | (Subp_Id : Entity_Id) return Boolean; | |
1576 | -- Determine whether subprogram Subp_Id is a primitive of a concurrent | |
1577 | -- type that implements an interface and has a private view. | |
1578 | ||
76a69663 ES |
1579 | procedure Set_Trivial_Subprogram (N : Node_Id); |
1580 | -- Sets the Is_Trivial_Subprogram flag in both spec and body of the | |
1581 | -- subprogram whose body is being analyzed. N is the statement node | |
1582 | -- causing the flag to be set, if the following statement is a return | |
1583 | -- of an entity, we mark the entity as set in source to suppress any | |
1584 | -- warning on the stylized use of function stubs with a dummy return. | |
1585 | ||
758c442c GD |
1586 | procedure Verify_Overriding_Indicator; |
1587 | -- If there was a previous spec, the entity has been entered in the | |
1588 | -- current scope previously. If the body itself carries an overriding | |
1589 | -- indicator, check that it is consistent with the known status of the | |
1590 | -- entity. | |
1591 | ||
ec4867fa ES |
1592 | ---------------------------- |
1593 | -- Check_Anonymous_Return -- | |
1594 | ---------------------------- | |
1595 | ||
1596 | procedure Check_Anonymous_Return is | |
1597 | Decl : Node_Id; | |
a523b302 | 1598 | Par : Node_Id; |
ec4867fa ES |
1599 | Scop : Entity_Id; |
1600 | ||
1601 | begin | |
1602 | if Present (Spec_Id) then | |
1603 | Scop := Spec_Id; | |
1604 | else | |
1605 | Scop := Body_Id; | |
1606 | end if; | |
1607 | ||
1608 | if Ekind (Scop) = E_Function | |
1609 | and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type | |
a523b302 JM |
1610 | and then not Is_Thunk (Scop) |
1611 | and then (Has_Task (Designated_Type (Etype (Scop))) | |
1612 | or else | |
1613 | (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) | |
1614 | and then | |
1615 | Is_Limited_Record (Designated_Type (Etype (Scop))))) | |
ec4867fa | 1616 | and then Expander_Active |
b20de9b9 AC |
1617 | |
1618 | -- Avoid cases with no tasking support | |
1619 | ||
1620 | and then RTE_Available (RE_Current_Master) | |
1621 | and then not Restriction_Active (No_Task_Hierarchy) | |
ec4867fa ES |
1622 | then |
1623 | Decl := | |
1624 | Make_Object_Declaration (Loc, | |
1625 | Defining_Identifier => | |
1626 | Make_Defining_Identifier (Loc, Name_uMaster), | |
1627 | Constant_Present => True, | |
1628 | Object_Definition => | |
1629 | New_Reference_To (RTE (RE_Master_Id), Loc), | |
1630 | Expression => | |
1631 | Make_Explicit_Dereference (Loc, | |
1632 | New_Reference_To (RTE (RE_Current_Master), Loc))); | |
1633 | ||
1634 | if Present (Declarations (N)) then | |
1635 | Prepend (Decl, Declarations (N)); | |
1636 | else | |
1637 | Set_Declarations (N, New_List (Decl)); | |
1638 | end if; | |
1639 | ||
1640 | Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); | |
1641 | Set_Has_Master_Entity (Scop); | |
a523b302 JM |
1642 | |
1643 | -- Now mark the containing scope as a task master | |
1644 | ||
1645 | Par := N; | |
1646 | while Nkind (Par) /= N_Compilation_Unit loop | |
1647 | Par := Parent (Par); | |
1648 | pragma Assert (Present (Par)); | |
1649 | ||
1650 | -- If we fall off the top, we are at the outer level, and | |
1651 | -- the environment task is our effective master, so nothing | |
1652 | -- to mark. | |
1653 | ||
1654 | if Nkind_In | |
1655 | (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) | |
1656 | then | |
1657 | Set_Is_Task_Master (Par, True); | |
1658 | exit; | |
1659 | end if; | |
1660 | end loop; | |
ec4867fa ES |
1661 | end if; |
1662 | end Check_Anonymous_Return; | |
1663 | ||
e660dbf7 JM |
1664 | ------------------------- |
1665 | -- Check_Inline_Pragma -- | |
1666 | ------------------------- | |
758c442c | 1667 | |
e660dbf7 JM |
1668 | procedure Check_Inline_Pragma (Spec : in out Node_Id) is |
1669 | Prag : Node_Id; | |
1670 | Plist : List_Id; | |
0fb2ea01 | 1671 | |
21d27997 | 1672 | function Is_Inline_Pragma (N : Node_Id) return Boolean; |
30783513 | 1673 | -- True when N is a pragma Inline or Inline_Always that applies |
33931112 | 1674 | -- to this subprogram. |
21d27997 RD |
1675 | |
1676 | ----------------------- | |
1677 | -- Is_Inline_Pragma -- | |
1678 | ----------------------- | |
1679 | ||
1680 | function Is_Inline_Pragma (N : Node_Id) return Boolean is | |
1681 | begin | |
1682 | return | |
1683 | Nkind (N) = N_Pragma | |
1684 | and then | |
1685 | (Pragma_Name (N) = Name_Inline_Always | |
1686 | or else | |
1687 | (Front_End_Inlining | |
1688 | and then Pragma_Name (N) = Name_Inline)) | |
1689 | and then | |
1690 | Chars | |
1691 | (Expression (First (Pragma_Argument_Associations (N)))) | |
1692 | = Chars (Body_Id); | |
1693 | end Is_Inline_Pragma; | |
1694 | ||
1695 | -- Start of processing for Check_Inline_Pragma | |
1696 | ||
c37bb106 | 1697 | begin |
e660dbf7 JM |
1698 | if not Expander_Active then |
1699 | return; | |
1700 | end if; | |
1701 | ||
1702 | if Is_List_Member (N) | |
1703 | and then Present (Next (N)) | |
21d27997 | 1704 | and then Is_Inline_Pragma (Next (N)) |
c37bb106 AC |
1705 | then |
1706 | Prag := Next (N); | |
1707 | ||
21d27997 RD |
1708 | elsif Nkind (N) /= N_Subprogram_Body_Stub |
1709 | and then Present (Declarations (N)) | |
1710 | and then Is_Inline_Pragma (First (Declarations (N))) | |
1711 | then | |
1712 | Prag := First (Declarations (N)); | |
1713 | ||
e660dbf7 JM |
1714 | else |
1715 | Prag := Empty; | |
c37bb106 | 1716 | end if; |
e660dbf7 JM |
1717 | |
1718 | if Present (Prag) then | |
1719 | if Present (Spec_Id) then | |
30196a76 | 1720 | if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then |
e660dbf7 JM |
1721 | Analyze (Prag); |
1722 | end if; | |
1723 | ||
1724 | else | |
d39d6bb8 | 1725 | -- Create a subprogram declaration, to make treatment uniform |
e660dbf7 JM |
1726 | |
1727 | declare | |
1728 | Subp : constant Entity_Id := | |
30196a76 | 1729 | Make_Defining_Identifier (Loc, Chars (Body_Id)); |
e660dbf7 | 1730 | Decl : constant Node_Id := |
30196a76 RD |
1731 | Make_Subprogram_Declaration (Loc, |
1732 | Specification => | |
1733 | New_Copy_Tree (Specification (N))); | |
1734 | ||
e660dbf7 JM |
1735 | begin |
1736 | Set_Defining_Unit_Name (Specification (Decl), Subp); | |
1737 | ||
1738 | if Present (First_Formal (Body_Id)) then | |
21d27997 | 1739 | Plist := Copy_Parameter_List (Body_Id); |
e660dbf7 JM |
1740 | Set_Parameter_Specifications |
1741 | (Specification (Decl), Plist); | |
1742 | end if; | |
1743 | ||
1744 | Insert_Before (N, Decl); | |
1745 | Analyze (Decl); | |
1746 | Analyze (Prag); | |
1747 | Set_Has_Pragma_Inline (Subp); | |
1748 | ||
76a69663 | 1749 | if Pragma_Name (Prag) = Name_Inline_Always then |
e660dbf7 | 1750 | Set_Is_Inlined (Subp); |
21d27997 | 1751 | Set_Has_Pragma_Inline_Always (Subp); |
e660dbf7 JM |
1752 | end if; |
1753 | ||
1754 | Spec := Subp; | |
1755 | end; | |
1756 | end if; | |
1757 | end if; | |
1758 | end Check_Inline_Pragma; | |
1759 | ||
7665e4bd AC |
1760 | -------------------------- |
1761 | -- Check_Missing_Return -- | |
1762 | -------------------------- | |
1763 | ||
1764 | procedure Check_Missing_Return is | |
1765 | Id : Entity_Id; | |
1766 | Missing_Ret : Boolean; | |
1767 | ||
1768 | begin | |
1769 | if Nkind (Body_Spec) = N_Function_Specification then | |
1770 | if Present (Spec_Id) then | |
1771 | Id := Spec_Id; | |
1772 | else | |
1773 | Id := Body_Id; | |
1774 | end if; | |
1775 | ||
1776 | if Return_Present (Id) then | |
1777 | Check_Returns (HSS, 'F', Missing_Ret); | |
1778 | ||
1779 | if Missing_Ret then | |
1780 | Set_Has_Missing_Return (Id); | |
1781 | end if; | |
1782 | ||
1783 | elsif (Is_Generic_Subprogram (Id) | |
1784 | or else not Is_Machine_Code_Subprogram (Id)) | |
1785 | and then not Body_Deleted | |
1786 | then | |
1787 | Error_Msg_N ("missing RETURN statement in function body", N); | |
1788 | end if; | |
1789 | ||
1790 | -- If procedure with No_Return, check returns | |
1791 | ||
1792 | elsif Nkind (Body_Spec) = N_Procedure_Specification | |
1793 | and then Present (Spec_Id) | |
1794 | and then No_Return (Spec_Id) | |
1795 | then | |
0052da20 | 1796 | Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); |
7665e4bd AC |
1797 | end if; |
1798 | end Check_Missing_Return; | |
1799 | ||
d44202ba HK |
1800 | ----------------------- |
1801 | -- Disambiguate_Spec -- | |
1802 | ----------------------- | |
1803 | ||
1804 | function Disambiguate_Spec return Entity_Id is | |
1805 | Priv_Spec : Entity_Id; | |
1806 | Spec_N : Entity_Id; | |
1807 | ||
1808 | procedure Replace_Types (To_Corresponding : Boolean); | |
1809 | -- Depending on the flag, replace the type of formal parameters of | |
1810 | -- Body_Id if it is a concurrent type implementing interfaces with | |
1811 | -- the corresponding record type or the other way around. | |
1812 | ||
1813 | procedure Replace_Types (To_Corresponding : Boolean) is | |
1814 | Formal : Entity_Id; | |
1815 | Formal_Typ : Entity_Id; | |
1816 | ||
1817 | begin | |
1818 | Formal := First_Formal (Body_Id); | |
1819 | while Present (Formal) loop | |
1820 | Formal_Typ := Etype (Formal); | |
1821 | ||
1822 | -- From concurrent type to corresponding record | |
1823 | ||
1824 | if To_Corresponding then | |
1825 | if Is_Concurrent_Type (Formal_Typ) | |
1826 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
1827 | and then Present (Interfaces ( | |
1828 | Corresponding_Record_Type (Formal_Typ))) | |
1829 | then | |
1830 | Set_Etype (Formal, | |
1831 | Corresponding_Record_Type (Formal_Typ)); | |
1832 | end if; | |
1833 | ||
1834 | -- From corresponding record to concurrent type | |
1835 | ||
1836 | else | |
1837 | if Is_Concurrent_Record_Type (Formal_Typ) | |
1838 | and then Present (Interfaces (Formal_Typ)) | |
1839 | then | |
1840 | Set_Etype (Formal, | |
1841 | Corresponding_Concurrent_Type (Formal_Typ)); | |
1842 | end if; | |
1843 | end if; | |
1844 | ||
1845 | Next_Formal (Formal); | |
1846 | end loop; | |
1847 | end Replace_Types; | |
1848 | ||
1849 | -- Start of processing for Disambiguate_Spec | |
1850 | ||
1851 | begin | |
1852 | -- Try to retrieve the specification of the body as is. All error | |
1853 | -- messages are suppressed because the body may not have a spec in | |
1854 | -- its current state. | |
1855 | ||
1856 | Spec_N := Find_Corresponding_Spec (N, False); | |
1857 | ||
1858 | -- It is possible that this is the body of a primitive declared | |
1859 | -- between a private and a full view of a concurrent type. The | |
1860 | -- controlling parameter of the spec carries the concurrent type, | |
1861 | -- not the corresponding record type as transformed by Analyze_ | |
1862 | -- Subprogram_Specification. In such cases, we undo the change | |
1863 | -- made by the analysis of the specification and try to find the | |
1864 | -- spec again. | |
766d7add | 1865 | |
8198b93d HK |
1866 | -- Note that wrappers already have their corresponding specs and |
1867 | -- bodies set during their creation, so if the candidate spec is | |
16b05213 | 1868 | -- a wrapper, then we definitely need to swap all types to their |
8198b93d | 1869 | -- original concurrent status. |
d44202ba | 1870 | |
8198b93d HK |
1871 | if No (Spec_N) |
1872 | or else Is_Primitive_Wrapper (Spec_N) | |
1873 | then | |
d44202ba HK |
1874 | -- Restore all references of corresponding record types to the |
1875 | -- original concurrent types. | |
1876 | ||
1877 | Replace_Types (To_Corresponding => False); | |
1878 | Priv_Spec := Find_Corresponding_Spec (N, False); | |
1879 | ||
1880 | -- The current body truly belongs to a primitive declared between | |
1881 | -- a private and a full view. We leave the modified body as is, | |
1882 | -- and return the true spec. | |
1883 | ||
1884 | if Present (Priv_Spec) | |
1885 | and then Is_Private_Primitive (Priv_Spec) | |
1886 | then | |
1887 | return Priv_Spec; | |
1888 | end if; | |
1889 | ||
1890 | -- In case that this is some sort of error, restore the original | |
1891 | -- state of the body. | |
1892 | ||
1893 | Replace_Types (To_Corresponding => True); | |
1894 | end if; | |
1895 | ||
1896 | return Spec_N; | |
1897 | end Disambiguate_Spec; | |
1898 | ||
1899 | ------------------------------------- | |
1900 | -- Is_Private_Concurrent_Primitive -- | |
1901 | ------------------------------------- | |
1902 | ||
1903 | function Is_Private_Concurrent_Primitive | |
1904 | (Subp_Id : Entity_Id) return Boolean | |
1905 | is | |
1906 | Formal_Typ : Entity_Id; | |
1907 | ||
1908 | begin | |
1909 | if Present (First_Formal (Subp_Id)) then | |
1910 | Formal_Typ := Etype (First_Formal (Subp_Id)); | |
1911 | ||
1912 | if Is_Concurrent_Record_Type (Formal_Typ) then | |
1913 | Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); | |
1914 | end if; | |
1915 | ||
1916 | -- The type of the first formal is a concurrent tagged type with | |
1917 | -- a private view. | |
1918 | ||
1919 | return | |
1920 | Is_Concurrent_Type (Formal_Typ) | |
1921 | and then Is_Tagged_Type (Formal_Typ) | |
1922 | and then Has_Private_Declaration (Formal_Typ); | |
1923 | end if; | |
1924 | ||
1925 | return False; | |
1926 | end Is_Private_Concurrent_Primitive; | |
1927 | ||
76a69663 ES |
1928 | ---------------------------- |
1929 | -- Set_Trivial_Subprogram -- | |
1930 | ---------------------------- | |
1931 | ||
1932 | procedure Set_Trivial_Subprogram (N : Node_Id) is | |
1933 | Nxt : constant Node_Id := Next (N); | |
1934 | ||
1935 | begin | |
1936 | Set_Is_Trivial_Subprogram (Body_Id); | |
1937 | ||
1938 | if Present (Spec_Id) then | |
1939 | Set_Is_Trivial_Subprogram (Spec_Id); | |
1940 | end if; | |
1941 | ||
1942 | if Present (Nxt) | |
1943 | and then Nkind (Nxt) = N_Simple_Return_Statement | |
1944 | and then No (Next (Nxt)) | |
1945 | and then Present (Expression (Nxt)) | |
1946 | and then Is_Entity_Name (Expression (Nxt)) | |
1947 | then | |
1948 | Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); | |
1949 | end if; | |
1950 | end Set_Trivial_Subprogram; | |
1951 | ||
758c442c GD |
1952 | --------------------------------- |
1953 | -- Verify_Overriding_Indicator -- | |
1954 | --------------------------------- | |
1955 | ||
1956 | procedure Verify_Overriding_Indicator is | |
1957 | begin | |
21d27997 RD |
1958 | if Must_Override (Body_Spec) then |
1959 | if Nkind (Spec_Id) = N_Defining_Operator_Symbol | |
1960 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
1961 | then | |
1962 | null; | |
1963 | ||
1964 | elsif not Is_Overriding_Operation (Spec_Id) then | |
ed2233dc | 1965 | Error_Msg_NE |
21d27997 RD |
1966 | ("subprogram& is not overriding", Body_Spec, Spec_Id); |
1967 | end if; | |
758c442c | 1968 | |
5d37ba92 ES |
1969 | elsif Must_Not_Override (Body_Spec) then |
1970 | if Is_Overriding_Operation (Spec_Id) then | |
ed2233dc | 1971 | Error_Msg_NE |
5d37ba92 | 1972 | ("subprogram& overrides inherited operation", |
76a69663 | 1973 | Body_Spec, Spec_Id); |
5d37ba92 | 1974 | |
21d27997 RD |
1975 | elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol |
1976 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
1977 | then | |
ed2233dc | 1978 | Error_Msg_NE |
21d27997 RD |
1979 | ("subprogram & overrides predefined operator ", |
1980 | Body_Spec, Spec_Id); | |
1981 | ||
618fb570 AC |
1982 | -- If this is not a primitive operation or protected subprogram, |
1983 | -- then the overriding indicator is altogether illegal. | |
5d37ba92 | 1984 | |
618fb570 AC |
1985 | elsif not Is_Primitive (Spec_Id) |
1986 | and then Ekind (Scope (Spec_Id)) /= E_Protected_Type | |
1987 | then | |
ed2233dc | 1988 | Error_Msg_N |
19d846a0 RD |
1989 | ("overriding indicator only allowed " & |
1990 | "if subprogram is primitive", | |
1991 | Body_Spec); | |
5d37ba92 | 1992 | end if; |
235f4375 | 1993 | |
d88a51b1 | 1994 | elsif Style_Check -- ??? incorrect use of Style_Check! |
235f4375 AC |
1995 | and then Is_Overriding_Operation (Spec_Id) |
1996 | then | |
1997 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
1998 | Style.Missing_Overriding (N, Body_Id); | |
758c442c GD |
1999 | end if; |
2000 | end Verify_Overriding_Indicator; | |
2001 | ||
b1b543d2 | 2002 | -- Start of processing for Analyze_Subprogram_Body_Helper |
0fb2ea01 | 2003 | |
996ae0b0 | 2004 | begin |
82c80734 RD |
2005 | -- Generic subprograms are handled separately. They always have a |
2006 | -- generic specification. Determine whether current scope has a | |
2007 | -- previous declaration. | |
996ae0b0 | 2008 | |
82c80734 RD |
2009 | -- If the subprogram body is defined within an instance of the same |
2010 | -- name, the instance appears as a package renaming, and will be hidden | |
2011 | -- within the subprogram. | |
996ae0b0 RK |
2012 | |
2013 | if Present (Prev_Id) | |
2014 | and then not Is_Overloadable (Prev_Id) | |
2015 | and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration | |
2016 | or else Comes_From_Source (Prev_Id)) | |
2017 | then | |
fbf5a39b | 2018 | if Is_Generic_Subprogram (Prev_Id) then |
996ae0b0 RK |
2019 | Spec_Id := Prev_Id; |
2020 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
2021 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
2022 | ||
2023 | Analyze_Generic_Subprogram_Body (N, Spec_Id); | |
7665e4bd AC |
2024 | |
2025 | if Nkind (N) = N_Subprogram_Body then | |
2026 | HSS := Handled_Statement_Sequence (N); | |
2027 | Check_Missing_Return; | |
2028 | end if; | |
2029 | ||
996ae0b0 RK |
2030 | return; |
2031 | ||
2032 | else | |
82c80734 RD |
2033 | -- Previous entity conflicts with subprogram name. Attempting to |
2034 | -- enter name will post error. | |
996ae0b0 RK |
2035 | |
2036 | Enter_Name (Body_Id); | |
2037 | return; | |
2038 | end if; | |
2039 | ||
82c80734 RD |
2040 | -- Non-generic case, find the subprogram declaration, if one was seen, |
2041 | -- or enter new overloaded entity in the current scope. If the | |
2042 | -- Current_Entity is the Body_Id itself, the unit is being analyzed as | |
2043 | -- part of the context of one of its subunits. No need to redo the | |
2044 | -- analysis. | |
996ae0b0 RK |
2045 | |
2046 | elsif Prev_Id = Body_Id | |
2047 | and then Has_Completion (Body_Id) | |
2048 | then | |
2049 | return; | |
2050 | ||
2051 | else | |
fbf5a39b | 2052 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); |
996ae0b0 RK |
2053 | |
2054 | if Nkind (N) = N_Subprogram_Body_Stub | |
2055 | or else No (Corresponding_Spec (N)) | |
2056 | then | |
d44202ba HK |
2057 | if Is_Private_Concurrent_Primitive (Body_Id) then |
2058 | Spec_Id := Disambiguate_Spec; | |
2059 | else | |
2060 | Spec_Id := Find_Corresponding_Spec (N); | |
2061 | end if; | |
996ae0b0 RK |
2062 | |
2063 | -- If this is a duplicate body, no point in analyzing it | |
2064 | ||
2065 | if Error_Posted (N) then | |
2066 | return; | |
2067 | end if; | |
2068 | ||
82c80734 RD |
2069 | -- A subprogram body should cause freezing of its own declaration, |
2070 | -- but if there was no previous explicit declaration, then the | |
2071 | -- subprogram will get frozen too late (there may be code within | |
2072 | -- the body that depends on the subprogram having been frozen, | |
2073 | -- such as uses of extra formals), so we force it to be frozen | |
76a69663 | 2074 | -- here. Same holds if the body and spec are compilation units. |
cd1c668b ES |
2075 | -- Finally, if the return type is an anonymous access to protected |
2076 | -- subprogram, it must be frozen before the body because its | |
2077 | -- expansion has generated an equivalent type that is used when | |
2078 | -- elaborating the body. | |
996ae0b0 RK |
2079 | |
2080 | if No (Spec_Id) then | |
2081 | Freeze_Before (N, Body_Id); | |
2082 | ||
2083 | elsif Nkind (Parent (N)) = N_Compilation_Unit then | |
2084 | Freeze_Before (N, Spec_Id); | |
cd1c668b ES |
2085 | |
2086 | elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then | |
2087 | Freeze_Before (N, Etype (Body_Id)); | |
996ae0b0 | 2088 | end if; |
a38ff9b1 | 2089 | |
996ae0b0 RK |
2090 | else |
2091 | Spec_Id := Corresponding_Spec (N); | |
2092 | end if; | |
2093 | end if; | |
2094 | ||
82c80734 RD |
2095 | -- Do not inline any subprogram that contains nested subprograms, since |
2096 | -- the backend inlining circuit seems to generate uninitialized | |
07fc65c4 | 2097 | -- references in this case. We know this happens in the case of front |
82c80734 RD |
2098 | -- end ZCX support, but it also appears it can happen in other cases as |
2099 | -- well. The backend often rejects attempts to inline in the case of | |
2100 | -- nested procedures anyway, so little if anything is lost by this. | |
2101 | -- Note that this is test is for the benefit of the back-end. There is | |
2102 | -- a separate test for front-end inlining that also rejects nested | |
2103 | -- subprograms. | |
07fc65c4 GB |
2104 | |
2105 | -- Do not do this test if errors have been detected, because in some | |
2106 | -- error cases, this code blows up, and we don't need it anyway if | |
2107 | -- there have been errors, since we won't get to the linker anyway. | |
2108 | ||
82c80734 RD |
2109 | if Comes_From_Source (Body_Id) |
2110 | and then Serious_Errors_Detected = 0 | |
2111 | then | |
07fc65c4 GB |
2112 | P_Ent := Body_Id; |
2113 | loop | |
2114 | P_Ent := Scope (P_Ent); | |
2115 | exit when No (P_Ent) or else P_Ent = Standard_Standard; | |
2116 | ||
fbf5a39b | 2117 | if Is_Subprogram (P_Ent) then |
07fc65c4 GB |
2118 | Set_Is_Inlined (P_Ent, False); |
2119 | ||
2120 | if Comes_From_Source (P_Ent) | |
07fc65c4 GB |
2121 | and then Has_Pragma_Inline (P_Ent) |
2122 | then | |
fbf5a39b AC |
2123 | Cannot_Inline |
2124 | ("cannot inline& (nested subprogram)?", | |
2125 | N, P_Ent); | |
07fc65c4 GB |
2126 | end if; |
2127 | end if; | |
2128 | end loop; | |
2129 | end if; | |
2130 | ||
e660dbf7 JM |
2131 | Check_Inline_Pragma (Spec_Id); |
2132 | ||
701b7fbb RD |
2133 | -- Deal with special case of a fully private operation in the body of |
2134 | -- the protected type. We must create a declaration for the subprogram, | |
2135 | -- in order to attach the protected subprogram that will be used in | |
2136 | -- internal calls. We exclude compiler generated bodies from the | |
2137 | -- expander since the issue does not arise for those cases. | |
07fc65c4 | 2138 | |
996ae0b0 RK |
2139 | if No (Spec_Id) |
2140 | and then Comes_From_Source (N) | |
2141 | and then Is_Protected_Type (Current_Scope) | |
2142 | then | |
47bfea3a | 2143 | Spec_Id := Build_Private_Protected_Declaration (N); |
701b7fbb | 2144 | end if; |
996ae0b0 | 2145 | |
5334d18f | 2146 | -- If a separate spec is present, then deal with freezing issues |
7ca78bba | 2147 | |
701b7fbb | 2148 | if Present (Spec_Id) then |
996ae0b0 | 2149 | Spec_Decl := Unit_Declaration_Node (Spec_Id); |
758c442c | 2150 | Verify_Overriding_Indicator; |
5d37ba92 ES |
2151 | |
2152 | -- In general, the spec will be frozen when we start analyzing the | |
2153 | -- body. However, for internally generated operations, such as | |
2154 | -- wrapper functions for inherited operations with controlling | |
2155 | -- results, the spec may not have been frozen by the time we | |
2156 | -- expand the freeze actions that include the bodies. In particular, | |
2157 | -- extra formals for accessibility or for return-in-place may need | |
2158 | -- to be generated. Freeze nodes, if any, are inserted before the | |
2159 | -- current body. | |
2160 | ||
2161 | if not Is_Frozen (Spec_Id) | |
2162 | and then Expander_Active | |
2163 | then | |
2164 | -- Force the generation of its freezing node to ensure proper | |
2165 | -- management of access types in the backend. | |
2166 | ||
2167 | -- This is definitely needed for some cases, but it is not clear | |
2168 | -- why, to be investigated further??? | |
2169 | ||
2170 | Set_Has_Delayed_Freeze (Spec_Id); | |
2171 | Insert_Actions (N, Freeze_Entity (Spec_Id, Loc)); | |
2172 | end if; | |
996ae0b0 RK |
2173 | end if; |
2174 | ||
a5d83d61 AC |
2175 | -- Mark presence of postcondition procedure in current scope and mark |
2176 | -- the procedure itself as needing debug info. The latter is important | |
2177 | -- when analyzing decision coverage (for example, for MC/DC coverage). | |
7ca78bba | 2178 | |
0dabde3a ES |
2179 | if Chars (Body_Id) = Name_uPostconditions then |
2180 | Set_Has_Postconditions (Current_Scope); | |
a5d83d61 | 2181 | Set_Debug_Info_Needed (Body_Id); |
0dabde3a ES |
2182 | end if; |
2183 | ||
996ae0b0 RK |
2184 | -- Place subprogram on scope stack, and make formals visible. If there |
2185 | -- is a spec, the visible entity remains that of the spec. | |
2186 | ||
2187 | if Present (Spec_Id) then | |
07fc65c4 | 2188 | Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); |
758c442c GD |
2189 | |
2190 | if Is_Child_Unit (Spec_Id) then | |
2191 | Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False); | |
2192 | end if; | |
2193 | ||
fbf5a39b AC |
2194 | if Style_Check then |
2195 | Style.Check_Identifier (Body_Id, Spec_Id); | |
2196 | end if; | |
996ae0b0 RK |
2197 | |
2198 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
2199 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
2200 | ||
f937473f | 2201 | if Is_Abstract_Subprogram (Spec_Id) then |
ed2233dc | 2202 | Error_Msg_N ("an abstract subprogram cannot have a body", N); |
996ae0b0 | 2203 | return; |
21d27997 | 2204 | |
996ae0b0 RK |
2205 | else |
2206 | Set_Convention (Body_Id, Convention (Spec_Id)); | |
2207 | Set_Has_Completion (Spec_Id); | |
2208 | ||
2209 | if Is_Protected_Type (Scope (Spec_Id)) then | |
21d27997 | 2210 | Prot_Typ := Scope (Spec_Id); |
996ae0b0 RK |
2211 | end if; |
2212 | ||
2213 | -- If this is a body generated for a renaming, do not check for | |
2214 | -- full conformance. The check is redundant, because the spec of | |
2215 | -- the body is a copy of the spec in the renaming declaration, | |
2216 | -- and the test can lead to spurious errors on nested defaults. | |
2217 | ||
2218 | if Present (Spec_Decl) | |
996ae0b0 | 2219 | and then not Comes_From_Source (N) |
93a81b02 GB |
2220 | and then |
2221 | (Nkind (Original_Node (Spec_Decl)) = | |
d2f97d3e GB |
2222 | N_Subprogram_Renaming_Declaration |
2223 | or else (Present (Corresponding_Body (Spec_Decl)) | |
2224 | and then | |
2225 | Nkind (Unit_Declaration_Node | |
2226 | (Corresponding_Body (Spec_Decl))) = | |
2227 | N_Subprogram_Renaming_Declaration)) | |
996ae0b0 RK |
2228 | then |
2229 | Conformant := True; | |
cabe9abc AC |
2230 | |
2231 | -- Conversely, the spec may have been generated for specless body | |
2232 | -- with an inline pragma. | |
2233 | ||
2234 | elsif Comes_From_Source (N) | |
2235 | and then not Comes_From_Source (Spec_Id) | |
2236 | and then Has_Pragma_Inline (Spec_Id) | |
2237 | then | |
2238 | Conformant := True; | |
76a69663 | 2239 | |
996ae0b0 RK |
2240 | else |
2241 | Check_Conformance | |
2242 | (Body_Id, Spec_Id, | |
76a69663 | 2243 | Fully_Conformant, True, Conformant, Body_Id); |
996ae0b0 RK |
2244 | end if; |
2245 | ||
2246 | -- If the body is not fully conformant, we have to decide if we | |
2247 | -- should analyze it or not. If it has a really messed up profile | |
2248 | -- then we probably should not analyze it, since we will get too | |
2249 | -- many bogus messages. | |
2250 | ||
2251 | -- Our decision is to go ahead in the non-fully conformant case | |
2252 | -- only if it is at least mode conformant with the spec. Note | |
2253 | -- that the call to Check_Fully_Conformant has issued the proper | |
2254 | -- error messages to complain about the lack of conformance. | |
2255 | ||
2256 | if not Conformant | |
2257 | and then not Mode_Conformant (Body_Id, Spec_Id) | |
2258 | then | |
2259 | return; | |
2260 | end if; | |
2261 | end if; | |
2262 | ||
996ae0b0 | 2263 | if Spec_Id /= Body_Id then |
fbf5a39b | 2264 | Reference_Body_Formals (Spec_Id, Body_Id); |
996ae0b0 RK |
2265 | end if; |
2266 | ||
2267 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
2268 | Set_Corresponding_Spec (N, Spec_Id); | |
758c442c | 2269 | |
5d37ba92 ES |
2270 | -- Ada 2005 (AI-345): If the operation is a primitive operation |
2271 | -- of a concurrent type, the type of the first parameter has been | |
2272 | -- replaced with the corresponding record, which is the proper | |
2273 | -- run-time structure to use. However, within the body there may | |
2274 | -- be uses of the formals that depend on primitive operations | |
2275 | -- of the type (in particular calls in prefixed form) for which | |
2276 | -- we need the original concurrent type. The operation may have | |
2277 | -- several controlling formals, so the replacement must be done | |
2278 | -- for all of them. | |
758c442c GD |
2279 | |
2280 | if Comes_From_Source (Spec_Id) | |
2281 | and then Present (First_Entity (Spec_Id)) | |
2282 | and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type | |
2283 | and then Is_Tagged_Type (Etype (First_Entity (Spec_Id))) | |
5d37ba92 | 2284 | and then |
ce2b6ba5 | 2285 | Present (Interfaces (Etype (First_Entity (Spec_Id)))) |
5d37ba92 ES |
2286 | and then |
2287 | Present | |
21d27997 RD |
2288 | (Corresponding_Concurrent_Type |
2289 | (Etype (First_Entity (Spec_Id)))) | |
758c442c | 2290 | then |
5d37ba92 ES |
2291 | declare |
2292 | Typ : constant Entity_Id := Etype (First_Entity (Spec_Id)); | |
2293 | Form : Entity_Id; | |
2294 | ||
2295 | begin | |
2296 | Form := First_Formal (Spec_Id); | |
2297 | while Present (Form) loop | |
2298 | if Etype (Form) = Typ then | |
2299 | Set_Etype (Form, Corresponding_Concurrent_Type (Typ)); | |
2300 | end if; | |
2301 | ||
2302 | Next_Formal (Form); | |
2303 | end loop; | |
2304 | end; | |
758c442c GD |
2305 | end if; |
2306 | ||
21d27997 RD |
2307 | -- Make the formals visible, and place subprogram on scope stack. |
2308 | -- This is also the point at which we set Last_Real_Spec_Entity | |
2309 | -- to mark the entities which will not be moved to the body. | |
758c442c | 2310 | |
996ae0b0 | 2311 | Install_Formals (Spec_Id); |
21d27997 | 2312 | Last_Real_Spec_Entity := Last_Entity (Spec_Id); |
0a36105d | 2313 | Push_Scope (Spec_Id); |
996ae0b0 RK |
2314 | |
2315 | -- Make sure that the subprogram is immediately visible. For | |
2316 | -- child units that have no separate spec this is indispensable. | |
2317 | -- Otherwise it is safe albeit redundant. | |
2318 | ||
2319 | Set_Is_Immediately_Visible (Spec_Id); | |
2320 | end if; | |
2321 | ||
2322 | Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id); | |
2323 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
2324 | Set_Scope (Body_Id, Scope (Spec_Id)); | |
ec4867fa | 2325 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); |
996ae0b0 RK |
2326 | |
2327 | -- Case of subprogram body with no previous spec | |
2328 | ||
2329 | else | |
2330 | if Style_Check | |
2331 | and then Comes_From_Source (Body_Id) | |
2332 | and then not Suppress_Style_Checks (Body_Id) | |
2333 | and then not In_Instance | |
2334 | then | |
2335 | Style.Body_With_No_Spec (N); | |
2336 | end if; | |
2337 | ||
2338 | New_Overloaded_Entity (Body_Id); | |
2339 | ||
2340 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
2341 | Set_Acts_As_Spec (N); | |
2342 | Generate_Definition (Body_Id); | |
fbf5a39b AC |
2343 | Generate_Reference |
2344 | (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True); | |
2345 | Generate_Reference_To_Formals (Body_Id); | |
996ae0b0 | 2346 | Install_Formals (Body_Id); |
0a36105d | 2347 | Push_Scope (Body_Id); |
996ae0b0 RK |
2348 | end if; |
2349 | end if; | |
2350 | ||
76a69663 ES |
2351 | -- If the return type is an anonymous access type whose designated type |
2352 | -- is the limited view of a class-wide type and the non-limited view is | |
2353 | -- available, update the return type accordingly. | |
ec4867fa | 2354 | |
0791fbe9 | 2355 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
2356 | and then Comes_From_Source (N) |
2357 | then | |
2358 | declare | |
ec4867fa | 2359 | Etyp : Entity_Id; |
0a36105d | 2360 | Rtyp : Entity_Id; |
ec4867fa ES |
2361 | |
2362 | begin | |
0a36105d JM |
2363 | Rtyp := Etype (Current_Scope); |
2364 | ||
2365 | if Ekind (Rtyp) = E_Anonymous_Access_Type then | |
2366 | Etyp := Directly_Designated_Type (Rtyp); | |
2367 | ||
2368 | if Is_Class_Wide_Type (Etyp) | |
2369 | and then From_With_Type (Etyp) | |
2370 | then | |
2371 | Set_Directly_Designated_Type | |
2372 | (Etype (Current_Scope), Available_View (Etyp)); | |
2373 | end if; | |
2374 | end if; | |
ec4867fa ES |
2375 | end; |
2376 | end if; | |
2377 | ||
996ae0b0 RK |
2378 | -- If this is the proper body of a stub, we must verify that the stub |
2379 | -- conforms to the body, and to the previous spec if one was present. | |
2380 | -- we know already that the body conforms to that spec. This test is | |
2381 | -- only required for subprograms that come from source. | |
2382 | ||
2383 | if Nkind (Parent (N)) = N_Subunit | |
2384 | and then Comes_From_Source (N) | |
2385 | and then not Error_Posted (Body_Id) | |
e895b435 ES |
2386 | and then Nkind (Corresponding_Stub (Parent (N))) = |
2387 | N_Subprogram_Body_Stub | |
996ae0b0 RK |
2388 | then |
2389 | declare | |
fbf5a39b AC |
2390 | Old_Id : constant Entity_Id := |
2391 | Defining_Entity | |
2392 | (Specification (Corresponding_Stub (Parent (N)))); | |
2393 | ||
996ae0b0 | 2394 | Conformant : Boolean := False; |
996ae0b0 RK |
2395 | |
2396 | begin | |
2397 | if No (Spec_Id) then | |
2398 | Check_Fully_Conformant (Body_Id, Old_Id); | |
2399 | ||
2400 | else | |
2401 | Check_Conformance | |
2402 | (Body_Id, Old_Id, Fully_Conformant, False, Conformant); | |
2403 | ||
2404 | if not Conformant then | |
2405 | ||
2406 | -- The stub was taken to be a new declaration. Indicate | |
2407 | -- that it lacks a body. | |
2408 | ||
2409 | Set_Has_Completion (Old_Id, False); | |
2410 | end if; | |
2411 | end if; | |
2412 | end; | |
2413 | end if; | |
2414 | ||
2415 | Set_Has_Completion (Body_Id); | |
2416 | Check_Eliminated (Body_Id); | |
2417 | ||
2418 | if Nkind (N) = N_Subprogram_Body_Stub then | |
2419 | return; | |
2420 | ||
ec4867fa | 2421 | elsif Present (Spec_Id) |
996ae0b0 | 2422 | and then Expander_Active |
e660dbf7 | 2423 | and then |
800621e0 | 2424 | (Has_Pragma_Inline_Always (Spec_Id) |
e660dbf7 | 2425 | or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)) |
996ae0b0 | 2426 | then |
e660dbf7 | 2427 | Build_Body_To_Inline (N, Spec_Id); |
996ae0b0 RK |
2428 | end if; |
2429 | ||
0ab80019 | 2430 | -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis |
9bc856dd | 2431 | -- if its specification we have to install the private withed units. |
21d27997 | 2432 | -- This holds for child units as well. |
9bc856dd AC |
2433 | |
2434 | if Is_Compilation_Unit (Body_Id) | |
21d27997 | 2435 | or else Nkind (Parent (N)) = N_Compilation_Unit |
9bc856dd AC |
2436 | then |
2437 | Install_Private_With_Clauses (Body_Id); | |
2438 | end if; | |
2439 | ||
ec4867fa ES |
2440 | Check_Anonymous_Return; |
2441 | ||
fdce4bb7 JM |
2442 | -- Set the Protected_Formal field of each extra formal of the protected |
2443 | -- subprogram to reference the corresponding extra formal of the | |
2444 | -- subprogram that implements it. For regular formals this occurs when | |
2445 | -- the protected subprogram's declaration is expanded, but the extra | |
2446 | -- formals don't get created until the subprogram is frozen. We need to | |
2447 | -- do this before analyzing the protected subprogram's body so that any | |
2448 | -- references to the original subprogram's extra formals will be changed | |
2449 | -- refer to the implementing subprogram's formals (see Expand_Formal). | |
2450 | ||
2451 | if Present (Spec_Id) | |
2452 | and then Is_Protected_Type (Scope (Spec_Id)) | |
2453 | and then Present (Protected_Body_Subprogram (Spec_Id)) | |
2454 | then | |
2455 | declare | |
2456 | Impl_Subp : constant Entity_Id := | |
2457 | Protected_Body_Subprogram (Spec_Id); | |
2458 | Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id); | |
2459 | Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp); | |
fdce4bb7 JM |
2460 | begin |
2461 | while Present (Prot_Ext_Formal) loop | |
2462 | pragma Assert (Present (Impl_Ext_Formal)); | |
fdce4bb7 | 2463 | Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal); |
fdce4bb7 JM |
2464 | Next_Formal_With_Extras (Prot_Ext_Formal); |
2465 | Next_Formal_With_Extras (Impl_Ext_Formal); | |
2466 | end loop; | |
2467 | end; | |
2468 | end if; | |
2469 | ||
0868e09c | 2470 | -- Now we can go on to analyze the body |
996ae0b0 RK |
2471 | |
2472 | HSS := Handled_Statement_Sequence (N); | |
2473 | Set_Actual_Subtypes (N, Current_Scope); | |
21d27997 RD |
2474 | |
2475 | -- Deal with preconditions and postconditions | |
2476 | ||
2477 | Process_PPCs (N, Spec_Id, Body_Id); | |
2478 | ||
f3d0f304 | 2479 | -- Add a declaration for the Protection object, renaming declarations |
21d27997 RD |
2480 | -- for discriminals and privals and finally a declaration for the entry |
2481 | -- family index (if applicable). This form of early expansion is done | |
2482 | -- when the Expander is active because Install_Private_Data_Declarations | |
2483 | -- references entities which were created during regular expansion. | |
2484 | ||
2485 | if Expander_Active | |
2486 | and then Comes_From_Source (N) | |
2487 | and then Present (Prot_Typ) | |
2488 | and then Present (Spec_Id) | |
2489 | and then not Is_Eliminated (Spec_Id) | |
2490 | then | |
2491 | Install_Private_Data_Declarations | |
2492 | (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N)); | |
2493 | end if; | |
2494 | ||
2495 | -- Analyze the declarations (this call will analyze the precondition | |
2496 | -- Check pragmas we prepended to the list, as well as the declaration | |
2497 | -- of the _Postconditions procedure). | |
2498 | ||
996ae0b0 | 2499 | Analyze_Declarations (Declarations (N)); |
21d27997 RD |
2500 | |
2501 | -- Check completion, and analyze the statements | |
2502 | ||
996ae0b0 | 2503 | Check_Completion; |
33931112 | 2504 | Inspect_Deferred_Constant_Completion (Declarations (N)); |
996ae0b0 | 2505 | Analyze (HSS); |
21d27997 RD |
2506 | |
2507 | -- Deal with end of scope processing for the body | |
2508 | ||
07fc65c4 | 2509 | Process_End_Label (HSS, 't', Current_Scope); |
996ae0b0 RK |
2510 | End_Scope; |
2511 | Check_Subprogram_Order (N); | |
c37bb106 | 2512 | Set_Analyzed (Body_Id); |
996ae0b0 RK |
2513 | |
2514 | -- If we have a separate spec, then the analysis of the declarations | |
2515 | -- caused the entities in the body to be chained to the spec id, but | |
2516 | -- we want them chained to the body id. Only the formal parameters | |
2517 | -- end up chained to the spec id in this case. | |
2518 | ||
2519 | if Present (Spec_Id) then | |
2520 | ||
d39d6bb8 | 2521 | -- We must conform to the categorization of our spec |
996ae0b0 | 2522 | |
d39d6bb8 | 2523 | Validate_Categorization_Dependency (N, Spec_Id); |
996ae0b0 | 2524 | |
d39d6bb8 RD |
2525 | -- And if this is a child unit, the parent units must conform |
2526 | ||
2527 | if Is_Child_Unit (Spec_Id) then | |
996ae0b0 RK |
2528 | Validate_Categorization_Dependency |
2529 | (Unit_Declaration_Node (Spec_Id), Spec_Id); | |
2530 | end if; | |
2531 | ||
21d27997 RD |
2532 | -- Here is where we move entities from the spec to the body |
2533 | ||
2534 | -- Case where there are entities that stay with the spec | |
2535 | ||
2536 | if Present (Last_Real_Spec_Entity) then | |
2537 | ||
2538 | -- No body entities (happens when the only real spec entities | |
2539 | -- come from precondition and postcondition pragmas) | |
2540 | ||
2541 | if No (Last_Entity (Body_Id)) then | |
2542 | Set_First_Entity | |
2543 | (Body_Id, Next_Entity (Last_Real_Spec_Entity)); | |
2544 | ||
2545 | -- Body entities present (formals), so chain stuff past them | |
2546 | ||
2547 | else | |
2548 | Set_Next_Entity | |
2549 | (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity)); | |
2550 | end if; | |
2551 | ||
2552 | Set_Next_Entity (Last_Real_Spec_Entity, Empty); | |
996ae0b0 | 2553 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); |
21d27997 RD |
2554 | Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity); |
2555 | ||
2556 | -- Case where there are no spec entities, in this case there can | |
2557 | -- be no body entities either, so just move everything. | |
996ae0b0 RK |
2558 | |
2559 | else | |
21d27997 | 2560 | pragma Assert (No (Last_Entity (Body_Id))); |
996ae0b0 RK |
2561 | Set_First_Entity (Body_Id, First_Entity (Spec_Id)); |
2562 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); | |
2563 | Set_First_Entity (Spec_Id, Empty); | |
2564 | Set_Last_Entity (Spec_Id, Empty); | |
2565 | end if; | |
2566 | end if; | |
2567 | ||
7665e4bd | 2568 | Check_Missing_Return; |
996ae0b0 | 2569 | |
82c80734 | 2570 | -- Now we are going to check for variables that are never modified in |
76a69663 ES |
2571 | -- the body of the procedure. But first we deal with a special case |
2572 | -- where we want to modify this check. If the body of the subprogram | |
2573 | -- starts with a raise statement or its equivalent, or if the body | |
2574 | -- consists entirely of a null statement, then it is pretty obvious | |
2575 | -- that it is OK to not reference the parameters. For example, this | |
2576 | -- might be the following common idiom for a stubbed function: | |
82c80734 RD |
2577 | -- statement of the procedure raises an exception. In particular this |
2578 | -- deals with the common idiom of a stubbed function, which might | |
2579 | -- appear as something like | |
fbf5a39b AC |
2580 | |
2581 | -- function F (A : Integer) return Some_Type; | |
2582 | -- X : Some_Type; | |
2583 | -- begin | |
2584 | -- raise Program_Error; | |
2585 | -- return X; | |
2586 | -- end F; | |
2587 | ||
76a69663 ES |
2588 | -- Here the purpose of X is simply to satisfy the annoying requirement |
2589 | -- in Ada that there be at least one return, and we certainly do not | |
2590 | -- want to go posting warnings on X that it is not initialized! On | |
2591 | -- the other hand, if X is entirely unreferenced that should still | |
2592 | -- get a warning. | |
2593 | ||
2594 | -- What we do is to detect these cases, and if we find them, flag the | |
2595 | -- subprogram as being Is_Trivial_Subprogram and then use that flag to | |
2596 | -- suppress unwanted warnings. For the case of the function stub above | |
2597 | -- we have a special test to set X as apparently assigned to suppress | |
2598 | -- the warning. | |
996ae0b0 RK |
2599 | |
2600 | declare | |
800621e0 | 2601 | Stm : Node_Id; |
996ae0b0 RK |
2602 | |
2603 | begin | |
0a36105d JM |
2604 | -- Skip initial labels (for one thing this occurs when we are in |
2605 | -- front end ZCX mode, but in any case it is irrelevant), and also | |
2606 | -- initial Push_xxx_Error_Label nodes, which are also irrelevant. | |
fbf5a39b | 2607 | |
800621e0 | 2608 | Stm := First (Statements (HSS)); |
0a36105d JM |
2609 | while Nkind (Stm) = N_Label |
2610 | or else Nkind (Stm) in N_Push_xxx_Label | |
2611 | loop | |
996ae0b0 | 2612 | Next (Stm); |
0a36105d | 2613 | end loop; |
996ae0b0 | 2614 | |
fbf5a39b AC |
2615 | -- Do the test on the original statement before expansion |
2616 | ||
2617 | declare | |
2618 | Ostm : constant Node_Id := Original_Node (Stm); | |
2619 | ||
2620 | begin | |
76a69663 | 2621 | -- If explicit raise statement, turn on flag |
fbf5a39b AC |
2622 | |
2623 | if Nkind (Ostm) = N_Raise_Statement then | |
76a69663 ES |
2624 | Set_Trivial_Subprogram (Stm); |
2625 | ||
f3d57416 | 2626 | -- If null statement, and no following statements, turn on flag |
76a69663 ES |
2627 | |
2628 | elsif Nkind (Stm) = N_Null_Statement | |
2629 | and then Comes_From_Source (Stm) | |
2630 | and then No (Next (Stm)) | |
2631 | then | |
2632 | Set_Trivial_Subprogram (Stm); | |
fbf5a39b AC |
2633 | |
2634 | -- Check for explicit call cases which likely raise an exception | |
2635 | ||
2636 | elsif Nkind (Ostm) = N_Procedure_Call_Statement then | |
2637 | if Is_Entity_Name (Name (Ostm)) then | |
2638 | declare | |
2639 | Ent : constant Entity_Id := Entity (Name (Ostm)); | |
2640 | ||
2641 | begin | |
2642 | -- If the procedure is marked No_Return, then likely it | |
2643 | -- raises an exception, but in any case it is not coming | |
76a69663 | 2644 | -- back here, so turn on the flag. |
fbf5a39b AC |
2645 | |
2646 | if Ekind (Ent) = E_Procedure | |
2647 | and then No_Return (Ent) | |
2648 | then | |
76a69663 | 2649 | Set_Trivial_Subprogram (Stm); |
fbf5a39b AC |
2650 | end if; |
2651 | end; | |
2652 | end if; | |
2653 | end if; | |
2654 | end; | |
996ae0b0 RK |
2655 | end; |
2656 | ||
2657 | -- Check for variables that are never modified | |
2658 | ||
2659 | declare | |
2660 | E1, E2 : Entity_Id; | |
2661 | ||
2662 | begin | |
fbf5a39b | 2663 | -- If there is a separate spec, then transfer Never_Set_In_Source |
996ae0b0 RK |
2664 | -- flags from out parameters to the corresponding entities in the |
2665 | -- body. The reason we do that is we want to post error flags on | |
2666 | -- the body entities, not the spec entities. | |
2667 | ||
2668 | if Present (Spec_Id) then | |
2669 | E1 := First_Entity (Spec_Id); | |
996ae0b0 RK |
2670 | while Present (E1) loop |
2671 | if Ekind (E1) = E_Out_Parameter then | |
2672 | E2 := First_Entity (Body_Id); | |
fbf5a39b | 2673 | while Present (E2) loop |
996ae0b0 RK |
2674 | exit when Chars (E1) = Chars (E2); |
2675 | Next_Entity (E2); | |
2676 | end loop; | |
2677 | ||
fbf5a39b AC |
2678 | if Present (E2) then |
2679 | Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1)); | |
2680 | end if; | |
996ae0b0 RK |
2681 | end if; |
2682 | ||
2683 | Next_Entity (E1); | |
2684 | end loop; | |
2685 | end if; | |
2686 | ||
0868e09c RD |
2687 | -- Check references in body unless it was deleted. Note that the |
2688 | -- check of Body_Deleted here is not just for efficiency, it is | |
2689 | -- necessary to avoid junk warnings on formal parameters. | |
2690 | ||
2691 | if not Body_Deleted then | |
2692 | Check_References (Body_Id); | |
2693 | end if; | |
996ae0b0 | 2694 | end; |
b1b543d2 | 2695 | end Analyze_Subprogram_Body_Helper; |
996ae0b0 RK |
2696 | |
2697 | ------------------------------------ | |
2698 | -- Analyze_Subprogram_Declaration -- | |
2699 | ------------------------------------ | |
2700 | ||
2701 | procedure Analyze_Subprogram_Declaration (N : Node_Id) is | |
5d5832bc | 2702 | Loc : constant Source_Ptr := Sloc (N); |
0f1a6a0b AC |
2703 | AS : constant List_Id := Aspect_Specifications (N); |
2704 | Scop : constant Entity_Id := Current_Scope; | |
5d5832bc AC |
2705 | Designator : Entity_Id; |
2706 | Form : Node_Id; | |
5d5832bc | 2707 | Null_Body : Node_Id := Empty; |
996ae0b0 RK |
2708 | |
2709 | -- Start of processing for Analyze_Subprogram_Declaration | |
2710 | ||
2711 | begin | |
349ff68f AC |
2712 | -- For a null procedure, capture the profile before analysis, for |
2713 | -- expansion at the freeze point and at each point of call. | |
5d5832bc AC |
2714 | -- The body will only be used if the procedure has preconditions. |
2715 | -- In that case the body is analyzed at the freeze point. | |
2716 | ||
2717 | if Nkind (Specification (N)) = N_Procedure_Specification | |
2718 | and then Null_Present (Specification (N)) | |
2719 | and then Expander_Active | |
2720 | then | |
2721 | Null_Body := | |
2722 | Make_Subprogram_Body (Loc, | |
2723 | Specification => | |
2724 | New_Copy_Tree (Specification (N)), | |
349ff68f AC |
2725 | Declarations => |
2726 | New_List, | |
5d5832bc AC |
2727 | Handled_Statement_Sequence => |
2728 | Make_Handled_Sequence_Of_Statements (Loc, | |
2729 | Statements => New_List (Make_Null_Statement (Loc)))); | |
2730 | ||
01957849 | 2731 | -- Create new entities for body and formals |
5d5832bc AC |
2732 | |
2733 | Set_Defining_Unit_Name (Specification (Null_Body), | |
2734 | Make_Defining_Identifier (Loc, Chars (Defining_Entity (N)))); | |
2735 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
2736 | ||
2737 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
2738 | while Present (Form) loop | |
2739 | Set_Defining_Identifier (Form, | |
2740 | Make_Defining_Identifier (Loc, | |
2741 | Chars (Defining_Identifier (Form)))); | |
2742 | Next (Form); | |
2743 | end loop; | |
2744 | ||
2745 | if Is_Protected_Type (Current_Scope) then | |
ed2233dc | 2746 | Error_Msg_N ("protected operation cannot be a null procedure", N); |
5d5832bc AC |
2747 | end if; |
2748 | end if; | |
2749 | ||
2750 | Designator := Analyze_Subprogram_Specification (Specification (N)); | |
2751 | Generate_Definition (Designator); | |
2752 | ||
b1b543d2 BD |
2753 | if Debug_Flag_C then |
2754 | Write_Str ("==> subprogram spec "); | |
2755 | Write_Name (Chars (Designator)); | |
2756 | Write_Str (" from "); | |
2757 | Write_Location (Sloc (N)); | |
2758 | Write_Eol; | |
2759 | Indent; | |
2760 | end if; | |
2761 | ||
5d5832bc AC |
2762 | if Nkind (Specification (N)) = N_Procedure_Specification |
2763 | and then Null_Present (Specification (N)) | |
2764 | then | |
2765 | Set_Has_Completion (Designator); | |
996ae0b0 | 2766 | |
5d5832bc AC |
2767 | if Present (Null_Body) then |
2768 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
2769 | Set_Body_To_Inline (N, Null_Body); | |
2770 | Set_Is_Inlined (Designator); | |
2771 | end if; | |
2772 | end if; | |
996ae0b0 RK |
2773 | |
2774 | Validate_RCI_Subprogram_Declaration (N); | |
996ae0b0 RK |
2775 | New_Overloaded_Entity (Designator); |
2776 | Check_Delayed_Subprogram (Designator); | |
fbf5a39b | 2777 | |
6ca063eb AC |
2778 | -- If the type of the first formal of the current subprogram is a |
2779 | -- nongeneric tagged private type, mark the subprogram as being a | |
2780 | -- private primitive. Ditto if this is a function with controlling | |
b7d5e87b AC |
2781 | -- result, and the return type is currently private. In both cases, |
2782 | -- the type of the controlling argument or result must be in the | |
2783 | -- current scope for the operation to be primitive. | |
6ca063eb AC |
2784 | |
2785 | if Has_Controlling_Result (Designator) | |
2786 | and then Is_Private_Type (Etype (Designator)) | |
b7d5e87b | 2787 | and then Scope (Etype (Designator)) = Current_Scope |
6ca063eb AC |
2788 | and then not Is_Generic_Actual_Type (Etype (Designator)) |
2789 | then | |
2790 | Set_Is_Private_Primitive (Designator); | |
d44202ba | 2791 | |
6ca063eb | 2792 | elsif Present (First_Formal (Designator)) then |
d44202ba HK |
2793 | declare |
2794 | Formal_Typ : constant Entity_Id := | |
2795 | Etype (First_Formal (Designator)); | |
2796 | begin | |
2797 | Set_Is_Private_Primitive (Designator, | |
2798 | Is_Tagged_Type (Formal_Typ) | |
b7d5e87b | 2799 | and then Scope (Formal_Typ) = Current_Scope |
d44202ba HK |
2800 | and then Is_Private_Type (Formal_Typ) |
2801 | and then not Is_Generic_Actual_Type (Formal_Typ)); | |
2802 | end; | |
2803 | end if; | |
2804 | ||
ec4867fa ES |
2805 | -- Ada 2005 (AI-251): Abstract interface primitives must be abstract |
2806 | -- or null. | |
2807 | ||
0791fbe9 | 2808 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
2809 | and then Comes_From_Source (N) |
2810 | and then Is_Dispatching_Operation (Designator) | |
2811 | then | |
2812 | declare | |
2813 | E : Entity_Id; | |
2814 | Etyp : Entity_Id; | |
2815 | ||
2816 | begin | |
2817 | if Has_Controlling_Result (Designator) then | |
2818 | Etyp := Etype (Designator); | |
2819 | ||
2820 | else | |
2821 | E := First_Entity (Designator); | |
2822 | while Present (E) | |
2823 | and then Is_Formal (E) | |
2824 | and then not Is_Controlling_Formal (E) | |
2825 | loop | |
2826 | Next_Entity (E); | |
2827 | end loop; | |
2828 | ||
2829 | Etyp := Etype (E); | |
2830 | end if; | |
2831 | ||
2832 | if Is_Access_Type (Etyp) then | |
2833 | Etyp := Directly_Designated_Type (Etyp); | |
2834 | end if; | |
2835 | ||
2836 | if Is_Interface (Etyp) | |
f937473f | 2837 | and then not Is_Abstract_Subprogram (Designator) |
ec4867fa ES |
2838 | and then not (Ekind (Designator) = E_Procedure |
2839 | and then Null_Present (Specification (N))) | |
2840 | then | |
2841 | Error_Msg_Name_1 := Chars (Defining_Entity (N)); | |
ed2233dc | 2842 | Error_Msg_N |
ec4867fa ES |
2843 | ("(Ada 2005) interface subprogram % must be abstract or null", |
2844 | N); | |
2845 | end if; | |
2846 | end; | |
2847 | end if; | |
2848 | ||
fbf5a39b AC |
2849 | -- What is the following code for, it used to be |
2850 | ||
2851 | -- ??? Set_Suppress_Elaboration_Checks | |
2852 | -- ??? (Designator, Elaboration_Checks_Suppressed (Designator)); | |
2853 | ||
2854 | -- The following seems equivalent, but a bit dubious | |
2855 | ||
2856 | if Elaboration_Checks_Suppressed (Designator) then | |
2857 | Set_Kill_Elaboration_Checks (Designator); | |
2858 | end if; | |
996ae0b0 RK |
2859 | |
2860 | if Scop /= Standard_Standard | |
2861 | and then not Is_Child_Unit (Designator) | |
2862 | then | |
fbf5a39b | 2863 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 | 2864 | else |
e895b435 | 2865 | -- For a compilation unit, check for library-unit pragmas |
996ae0b0 | 2866 | |
0a36105d | 2867 | Push_Scope (Designator); |
996ae0b0 RK |
2868 | Set_Categorization_From_Pragmas (N); |
2869 | Validate_Categorization_Dependency (N, Designator); | |
2870 | Pop_Scope; | |
2871 | end if; | |
2872 | ||
2873 | -- For a compilation unit, set body required. This flag will only be | |
2874 | -- reset if a valid Import or Interface pragma is processed later on. | |
2875 | ||
2876 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
2877 | Set_Body_Required (Parent (N), True); | |
758c442c | 2878 | |
0791fbe9 | 2879 | if Ada_Version >= Ada_2005 |
758c442c GD |
2880 | and then Nkind (Specification (N)) = N_Procedure_Specification |
2881 | and then Null_Present (Specification (N)) | |
2882 | then | |
2883 | Error_Msg_N | |
2884 | ("null procedure cannot be declared at library level", N); | |
2885 | end if; | |
996ae0b0 RK |
2886 | end if; |
2887 | ||
fbf5a39b | 2888 | Generate_Reference_To_Formals (Designator); |
996ae0b0 | 2889 | Check_Eliminated (Designator); |
fbf5a39b | 2890 | |
b1b543d2 BD |
2891 | if Debug_Flag_C then |
2892 | Outdent; | |
2893 | Write_Str ("<== subprogram spec "); | |
2894 | Write_Name (Chars (Designator)); | |
2895 | Write_Str (" from "); | |
2896 | Write_Location (Sloc (N)); | |
2897 | Write_Eol; | |
2898 | end if; | |
0f1a6a0b AC |
2899 | |
2900 | Analyze_Aspect_Specifications (N, Designator, AS); | |
996ae0b0 RK |
2901 | end Analyze_Subprogram_Declaration; |
2902 | ||
fbf5a39b AC |
2903 | -------------------------------------- |
2904 | -- Analyze_Subprogram_Specification -- | |
2905 | -------------------------------------- | |
2906 | ||
2907 | -- Reminder: N here really is a subprogram specification (not a subprogram | |
2908 | -- declaration). This procedure is called to analyze the specification in | |
2909 | -- both subprogram bodies and subprogram declarations (specs). | |
2910 | ||
2911 | function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is | |
2912 | Designator : constant Entity_Id := Defining_Entity (N); | |
21d27997 | 2913 | Formals : constant List_Id := Parameter_Specifications (N); |
fbf5a39b | 2914 | |
758c442c GD |
2915 | -- Start of processing for Analyze_Subprogram_Specification |
2916 | ||
fbf5a39b AC |
2917 | begin |
2918 | Generate_Definition (Designator); | |
2919 | ||
2920 | if Nkind (N) = N_Function_Specification then | |
2921 | Set_Ekind (Designator, E_Function); | |
2922 | Set_Mechanism (Designator, Default_Mechanism); | |
2923 | ||
fbf5a39b AC |
2924 | else |
2925 | Set_Ekind (Designator, E_Procedure); | |
2926 | Set_Etype (Designator, Standard_Void_Type); | |
2927 | end if; | |
2928 | ||
800621e0 | 2929 | -- Introduce new scope for analysis of the formals and the return type |
82c80734 RD |
2930 | |
2931 | Set_Scope (Designator, Current_Scope); | |
2932 | ||
fbf5a39b | 2933 | if Present (Formals) then |
0a36105d | 2934 | Push_Scope (Designator); |
fbf5a39b | 2935 | Process_Formals (Formals, N); |
758c442c | 2936 | |
a38ff9b1 ES |
2937 | -- Ada 2005 (AI-345): If this is an overriding operation of an |
2938 | -- inherited interface operation, and the controlling type is | |
2939 | -- a synchronized type, replace the type with its corresponding | |
2940 | -- record, to match the proper signature of an overriding operation. | |
69cb258c AC |
2941 | -- Same processing for an access parameter whose designated type is |
2942 | -- derived from a synchronized interface. | |
758c442c | 2943 | |
0791fbe9 | 2944 | if Ada_Version >= Ada_2005 then |
d44202ba HK |
2945 | declare |
2946 | Formal : Entity_Id; | |
2947 | Formal_Typ : Entity_Id; | |
2948 | Rec_Typ : Entity_Id; | |
69cb258c | 2949 | Desig_Typ : Entity_Id; |
0a36105d | 2950 | |
d44202ba HK |
2951 | begin |
2952 | Formal := First_Formal (Designator); | |
2953 | while Present (Formal) loop | |
2954 | Formal_Typ := Etype (Formal); | |
0a36105d | 2955 | |
d44202ba HK |
2956 | if Is_Concurrent_Type (Formal_Typ) |
2957 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
2958 | then | |
2959 | Rec_Typ := Corresponding_Record_Type (Formal_Typ); | |
2960 | ||
2961 | if Present (Interfaces (Rec_Typ)) then | |
2962 | Set_Etype (Formal, Rec_Typ); | |
2963 | end if; | |
69cb258c AC |
2964 | |
2965 | elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then | |
2966 | Desig_Typ := Designated_Type (Formal_Typ); | |
2967 | ||
2968 | if Is_Concurrent_Type (Desig_Typ) | |
2969 | and then Present (Corresponding_Record_Type (Desig_Typ)) | |
2970 | then | |
2971 | Rec_Typ := Corresponding_Record_Type (Desig_Typ); | |
2972 | ||
2973 | if Present (Interfaces (Rec_Typ)) then | |
2974 | Set_Directly_Designated_Type (Formal_Typ, Rec_Typ); | |
2975 | end if; | |
2976 | end if; | |
d44202ba HK |
2977 | end if; |
2978 | ||
2979 | Next_Formal (Formal); | |
2980 | end loop; | |
2981 | end; | |
758c442c GD |
2982 | end if; |
2983 | ||
fbf5a39b | 2984 | End_Scope; |
82c80734 | 2985 | |
b66c3ff4 AC |
2986 | -- The subprogram scope is pushed and popped around the processing of |
2987 | -- the return type for consistency with call above to Process_Formals | |
2988 | -- (which itself can call Analyze_Return_Type), and to ensure that any | |
2989 | -- itype created for the return type will be associated with the proper | |
2990 | -- scope. | |
2991 | ||
82c80734 | 2992 | elsif Nkind (N) = N_Function_Specification then |
b66c3ff4 AC |
2993 | Push_Scope (Designator); |
2994 | ||
82c80734 | 2995 | Analyze_Return_Type (N); |
b66c3ff4 AC |
2996 | |
2997 | End_Scope; | |
fbf5a39b AC |
2998 | end if; |
2999 | ||
3000 | if Nkind (N) = N_Function_Specification then | |
3001 | if Nkind (Designator) = N_Defining_Operator_Symbol then | |
3002 | Valid_Operator_Definition (Designator); | |
3003 | end if; | |
3004 | ||
3005 | May_Need_Actuals (Designator); | |
3006 | ||
fe63b1b1 ES |
3007 | -- Ada 2005 (AI-251): If the return type is abstract, verify that |
3008 | -- the subprogram is abstract also. This does not apply to renaming | |
3009 | -- declarations, where abstractness is inherited. | |
2bfb1b72 | 3010 | |
fe63b1b1 ES |
3011 | -- In case of primitives associated with abstract interface types |
3012 | -- the check is applied later (see Analyze_Subprogram_Declaration). | |
ec4867fa | 3013 | |
2bfb1b72 RD |
3014 | if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration, |
3015 | N_Abstract_Subprogram_Declaration, | |
3016 | N_Formal_Abstract_Subprogram_Declaration) | |
fbf5a39b | 3017 | then |
2e79de51 AC |
3018 | if Is_Abstract_Type (Etype (Designator)) |
3019 | and then not Is_Interface (Etype (Designator)) | |
3020 | then | |
3021 | Error_Msg_N | |
3022 | ("function that returns abstract type must be abstract", N); | |
3023 | ||
2bfb1b72 | 3024 | -- Ada 2012 (AI-0073): extend this test to subprograms with an |
2e79de51 AC |
3025 | -- access result whose designated type is abstract. |
3026 | ||
3027 | elsif Nkind (Result_Definition (N)) = N_Access_Definition | |
3028 | and then | |
3029 | not Is_Class_Wide_Type (Designated_Type (Etype (Designator))) | |
3030 | and then Is_Abstract_Type (Designated_Type (Etype (Designator))) | |
dbe945f1 | 3031 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
3032 | then |
3033 | Error_Msg_N ("function whose access result designates " | |
3034 | & "abstract type must be abstract", N); | |
3035 | end if; | |
fbf5a39b AC |
3036 | end if; |
3037 | end if; | |
3038 | ||
3039 | return Designator; | |
3040 | end Analyze_Subprogram_Specification; | |
3041 | ||
996ae0b0 RK |
3042 | -------------------------- |
3043 | -- Build_Body_To_Inline -- | |
3044 | -------------------------- | |
3045 | ||
d05ef0ab | 3046 | procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is |
f937473f | 3047 | Decl : constant Node_Id := Unit_Declaration_Node (Subp); |
996ae0b0 RK |
3048 | Original_Body : Node_Id; |
3049 | Body_To_Analyze : Node_Id; | |
3050 | Max_Size : constant := 10; | |
3051 | Stat_Count : Integer := 0; | |
3052 | ||
3053 | function Has_Excluded_Declaration (Decls : List_Id) return Boolean; | |
e895b435 | 3054 | -- Check for declarations that make inlining not worthwhile |
996ae0b0 RK |
3055 | |
3056 | function Has_Excluded_Statement (Stats : List_Id) return Boolean; | |
82c80734 RD |
3057 | -- Check for statements that make inlining not worthwhile: any tasking |
3058 | -- statement, nested at any level. Keep track of total number of | |
3059 | -- elementary statements, as a measure of acceptable size. | |
996ae0b0 RK |
3060 | |
3061 | function Has_Pending_Instantiation return Boolean; | |
f937473f RD |
3062 | -- If some enclosing body contains instantiations that appear before the |
3063 | -- corresponding generic body, the enclosing body has a freeze node so | |
3064 | -- that it can be elaborated after the generic itself. This might | |
996ae0b0 RK |
3065 | -- conflict with subsequent inlinings, so that it is unsafe to try to |
3066 | -- inline in such a case. | |
3067 | ||
c8ef728f | 3068 | function Has_Single_Return return Boolean; |
f937473f RD |
3069 | -- In general we cannot inline functions that return unconstrained type. |
3070 | -- However, we can handle such functions if all return statements return | |
3071 | -- a local variable that is the only declaration in the body of the | |
3072 | -- function. In that case the call can be replaced by that local | |
3073 | -- variable as is done for other inlined calls. | |
c8ef728f | 3074 | |
fbf5a39b | 3075 | procedure Remove_Pragmas; |
76a69663 ES |
3076 | -- A pragma Unreferenced or pragma Unmodified that mentions a formal |
3077 | -- parameter has no meaning when the body is inlined and the formals | |
3078 | -- are rewritten. Remove it from body to inline. The analysis of the | |
3079 | -- non-inlined body will handle the pragma properly. | |
996ae0b0 | 3080 | |
e895b435 ES |
3081 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean; |
3082 | -- If the body of the subprogram includes a call that returns an | |
3083 | -- unconstrained type, the secondary stack is involved, and it | |
3084 | -- is not worth inlining. | |
3085 | ||
996ae0b0 RK |
3086 | ------------------------------ |
3087 | -- Has_Excluded_Declaration -- | |
3088 | ------------------------------ | |
3089 | ||
3090 | function Has_Excluded_Declaration (Decls : List_Id) return Boolean is | |
3091 | D : Node_Id; | |
3092 | ||
fbf5a39b | 3093 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean; |
82c80734 RD |
3094 | -- Nested subprograms make a given body ineligible for inlining, but |
3095 | -- we make an exception for instantiations of unchecked conversion. | |
3096 | -- The body has not been analyzed yet, so check the name, and verify | |
3097 | -- that the visible entity with that name is the predefined unit. | |
3098 | ||
3099 | ----------------------------- | |
3100 | -- Is_Unchecked_Conversion -- | |
3101 | ----------------------------- | |
fbf5a39b AC |
3102 | |
3103 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean is | |
82c80734 | 3104 | Id : constant Node_Id := Name (D); |
fbf5a39b AC |
3105 | Conv : Entity_Id; |
3106 | ||
3107 | begin | |
3108 | if Nkind (Id) = N_Identifier | |
3109 | and then Chars (Id) = Name_Unchecked_Conversion | |
3110 | then | |
3111 | Conv := Current_Entity (Id); | |
3112 | ||
800621e0 | 3113 | elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name) |
fbf5a39b AC |
3114 | and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion |
3115 | then | |
3116 | Conv := Current_Entity (Selector_Name (Id)); | |
fbf5a39b AC |
3117 | else |
3118 | return False; | |
3119 | end if; | |
3120 | ||
758c442c GD |
3121 | return Present (Conv) |
3122 | and then Is_Predefined_File_Name | |
3123 | (Unit_File_Name (Get_Source_Unit (Conv))) | |
fbf5a39b AC |
3124 | and then Is_Intrinsic_Subprogram (Conv); |
3125 | end Is_Unchecked_Conversion; | |
3126 | ||
3127 | -- Start of processing for Has_Excluded_Declaration | |
3128 | ||
996ae0b0 RK |
3129 | begin |
3130 | D := First (Decls); | |
996ae0b0 | 3131 | while Present (D) loop |
800621e0 RD |
3132 | if (Nkind (D) = N_Function_Instantiation |
3133 | and then not Is_Unchecked_Conversion (D)) | |
3134 | or else Nkind_In (D, N_Protected_Type_Declaration, | |
3135 | N_Package_Declaration, | |
3136 | N_Package_Instantiation, | |
3137 | N_Subprogram_Body, | |
3138 | N_Procedure_Instantiation, | |
3139 | N_Task_Type_Declaration) | |
996ae0b0 RK |
3140 | then |
3141 | Cannot_Inline | |
fbf5a39b | 3142 | ("cannot inline & (non-allowed declaration)?", D, Subp); |
996ae0b0 RK |
3143 | return True; |
3144 | end if; | |
3145 | ||
3146 | Next (D); | |
3147 | end loop; | |
3148 | ||
3149 | return False; | |
996ae0b0 RK |
3150 | end Has_Excluded_Declaration; |
3151 | ||
3152 | ---------------------------- | |
3153 | -- Has_Excluded_Statement -- | |
3154 | ---------------------------- | |
3155 | ||
3156 | function Has_Excluded_Statement (Stats : List_Id) return Boolean is | |
3157 | S : Node_Id; | |
3158 | E : Node_Id; | |
3159 | ||
3160 | begin | |
3161 | S := First (Stats); | |
996ae0b0 RK |
3162 | while Present (S) loop |
3163 | Stat_Count := Stat_Count + 1; | |
3164 | ||
800621e0 RD |
3165 | if Nkind_In (S, N_Abort_Statement, |
3166 | N_Asynchronous_Select, | |
3167 | N_Conditional_Entry_Call, | |
3168 | N_Delay_Relative_Statement, | |
3169 | N_Delay_Until_Statement, | |
3170 | N_Selective_Accept, | |
3171 | N_Timed_Entry_Call) | |
996ae0b0 RK |
3172 | then |
3173 | Cannot_Inline | |
fbf5a39b | 3174 | ("cannot inline & (non-allowed statement)?", S, Subp); |
996ae0b0 RK |
3175 | return True; |
3176 | ||
3177 | elsif Nkind (S) = N_Block_Statement then | |
3178 | if Present (Declarations (S)) | |
3179 | and then Has_Excluded_Declaration (Declarations (S)) | |
3180 | then | |
3181 | return True; | |
3182 | ||
3183 | elsif Present (Handled_Statement_Sequence (S)) | |
3184 | and then | |
3185 | (Present | |
3186 | (Exception_Handlers (Handled_Statement_Sequence (S))) | |
3187 | or else | |
3188 | Has_Excluded_Statement | |
3189 | (Statements (Handled_Statement_Sequence (S)))) | |
3190 | then | |
3191 | return True; | |
3192 | end if; | |
3193 | ||
3194 | elsif Nkind (S) = N_Case_Statement then | |
3195 | E := First (Alternatives (S)); | |
996ae0b0 RK |
3196 | while Present (E) loop |
3197 | if Has_Excluded_Statement (Statements (E)) then | |
3198 | return True; | |
3199 | end if; | |
3200 | ||
3201 | Next (E); | |
3202 | end loop; | |
3203 | ||
3204 | elsif Nkind (S) = N_If_Statement then | |
3205 | if Has_Excluded_Statement (Then_Statements (S)) then | |
3206 | return True; | |
3207 | end if; | |
3208 | ||
3209 | if Present (Elsif_Parts (S)) then | |
3210 | E := First (Elsif_Parts (S)); | |
996ae0b0 RK |
3211 | while Present (E) loop |
3212 | if Has_Excluded_Statement (Then_Statements (E)) then | |
3213 | return True; | |
3214 | end if; | |
3215 | Next (E); | |
3216 | end loop; | |
3217 | end if; | |
3218 | ||
3219 | if Present (Else_Statements (S)) | |
3220 | and then Has_Excluded_Statement (Else_Statements (S)) | |
3221 | then | |
3222 | return True; | |
3223 | end if; | |
3224 | ||
3225 | elsif Nkind (S) = N_Loop_Statement | |
3226 | and then Has_Excluded_Statement (Statements (S)) | |
3227 | then | |
3228 | return True; | |
3e2399ba AC |
3229 | |
3230 | elsif Nkind (S) = N_Extended_Return_Statement then | |
3231 | if Has_Excluded_Statement | |
3232 | (Statements (Handled_Statement_Sequence (S))) | |
3233 | or else Present | |
3234 | (Exception_Handlers (Handled_Statement_Sequence (S))) | |
3235 | then | |
3236 | return True; | |
3237 | end if; | |
996ae0b0 RK |
3238 | end if; |
3239 | ||
3240 | Next (S); | |
3241 | end loop; | |
3242 | ||
3243 | return False; | |
3244 | end Has_Excluded_Statement; | |
3245 | ||
3246 | ------------------------------- | |
3247 | -- Has_Pending_Instantiation -- | |
3248 | ------------------------------- | |
3249 | ||
3250 | function Has_Pending_Instantiation return Boolean is | |
ec4867fa | 3251 | S : Entity_Id; |
996ae0b0 RK |
3252 | |
3253 | begin | |
ec4867fa | 3254 | S := Current_Scope; |
996ae0b0 RK |
3255 | while Present (S) loop |
3256 | if Is_Compilation_Unit (S) | |
3257 | or else Is_Child_Unit (S) | |
3258 | then | |
3259 | return False; | |
bce79204 | 3260 | |
996ae0b0 RK |
3261 | elsif Ekind (S) = E_Package |
3262 | and then Has_Forward_Instantiation (S) | |
3263 | then | |
3264 | return True; | |
3265 | end if; | |
3266 | ||
3267 | S := Scope (S); | |
3268 | end loop; | |
3269 | ||
3270 | return False; | |
3271 | end Has_Pending_Instantiation; | |
3272 | ||
c8ef728f ES |
3273 | ------------------------ |
3274 | -- Has_Single_Return -- | |
3275 | ------------------------ | |
3276 | ||
3277 | function Has_Single_Return return Boolean is | |
3278 | Return_Statement : Node_Id := Empty; | |
3279 | ||
3280 | function Check_Return (N : Node_Id) return Traverse_Result; | |
3281 | ||
3282 | ------------------ | |
3283 | -- Check_Return -- | |
3284 | ------------------ | |
3285 | ||
3286 | function Check_Return (N : Node_Id) return Traverse_Result is | |
3287 | begin | |
5d37ba92 | 3288 | if Nkind (N) = N_Simple_Return_Statement then |
c8ef728f ES |
3289 | if Present (Expression (N)) |
3290 | and then Is_Entity_Name (Expression (N)) | |
3291 | then | |
3292 | if No (Return_Statement) then | |
3293 | Return_Statement := N; | |
3294 | return OK; | |
3295 | ||
3296 | elsif Chars (Expression (N)) = | |
3297 | Chars (Expression (Return_Statement)) | |
3298 | then | |
3299 | return OK; | |
3300 | ||
3301 | else | |
3302 | return Abandon; | |
3303 | end if; | |
3304 | ||
3e2399ba AC |
3305 | -- A return statement within an extended return is a noop |
3306 | -- after inlining. | |
3307 | ||
3308 | elsif No (Expression (N)) | |
3309 | and then Nkind (Parent (Parent (N))) = | |
3310 | N_Extended_Return_Statement | |
3311 | then | |
3312 | return OK; | |
3313 | ||
c8ef728f ES |
3314 | else |
3315 | -- Expression has wrong form | |
3316 | ||
3317 | return Abandon; | |
3318 | end if; | |
3319 | ||
3e2399ba AC |
3320 | -- We can only inline a build-in-place function if |
3321 | -- it has a single extended return. | |
3322 | ||
3323 | elsif Nkind (N) = N_Extended_Return_Statement then | |
3324 | if No (Return_Statement) then | |
3325 | Return_Statement := N; | |
3326 | return OK; | |
3327 | ||
3328 | else | |
3329 | return Abandon; | |
3330 | end if; | |
3331 | ||
c8ef728f ES |
3332 | else |
3333 | return OK; | |
3334 | end if; | |
3335 | end Check_Return; | |
3336 | ||
3337 | function Check_All_Returns is new Traverse_Func (Check_Return); | |
3338 | ||
3339 | -- Start of processing for Has_Single_Return | |
3340 | ||
3341 | begin | |
3e2399ba AC |
3342 | if Check_All_Returns (N) /= OK then |
3343 | return False; | |
3344 | ||
3345 | elsif Nkind (Return_Statement) = N_Extended_Return_Statement then | |
3346 | return True; | |
3347 | ||
3348 | else | |
3349 | return Present (Declarations (N)) | |
3350 | and then Present (First (Declarations (N))) | |
3351 | and then Chars (Expression (Return_Statement)) = | |
3352 | Chars (Defining_Identifier (First (Declarations (N)))); | |
3353 | end if; | |
c8ef728f ES |
3354 | end Has_Single_Return; |
3355 | ||
fbf5a39b AC |
3356 | -------------------- |
3357 | -- Remove_Pragmas -- | |
3358 | -------------------- | |
3359 | ||
3360 | procedure Remove_Pragmas is | |
3361 | Decl : Node_Id; | |
3362 | Nxt : Node_Id; | |
3363 | ||
3364 | begin | |
3365 | Decl := First (Declarations (Body_To_Analyze)); | |
3366 | while Present (Decl) loop | |
3367 | Nxt := Next (Decl); | |
3368 | ||
3369 | if Nkind (Decl) = N_Pragma | |
76a69663 ES |
3370 | and then (Pragma_Name (Decl) = Name_Unreferenced |
3371 | or else | |
3372 | Pragma_Name (Decl) = Name_Unmodified) | |
fbf5a39b AC |
3373 | then |
3374 | Remove (Decl); | |
3375 | end if; | |
3376 | ||
3377 | Decl := Nxt; | |
3378 | end loop; | |
3379 | end Remove_Pragmas; | |
3380 | ||
e895b435 ES |
3381 | -------------------------- |
3382 | -- Uses_Secondary_Stack -- | |
3383 | -------------------------- | |
3384 | ||
3385 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is | |
3386 | function Check_Call (N : Node_Id) return Traverse_Result; | |
3387 | -- Look for function calls that return an unconstrained type | |
3388 | ||
3389 | ---------------- | |
3390 | -- Check_Call -- | |
3391 | ---------------- | |
3392 | ||
3393 | function Check_Call (N : Node_Id) return Traverse_Result is | |
3394 | begin | |
3395 | if Nkind (N) = N_Function_Call | |
3396 | and then Is_Entity_Name (Name (N)) | |
3397 | and then Is_Composite_Type (Etype (Entity (Name (N)))) | |
3398 | and then not Is_Constrained (Etype (Entity (Name (N)))) | |
3399 | then | |
3400 | Cannot_Inline | |
3401 | ("cannot inline & (call returns unconstrained type)?", | |
3402 | N, Subp); | |
3403 | return Abandon; | |
3404 | else | |
3405 | return OK; | |
3406 | end if; | |
3407 | end Check_Call; | |
3408 | ||
3409 | function Check_Calls is new Traverse_Func (Check_Call); | |
3410 | ||
3411 | begin | |
3412 | return Check_Calls (Bod) = Abandon; | |
3413 | end Uses_Secondary_Stack; | |
3414 | ||
996ae0b0 RK |
3415 | -- Start of processing for Build_Body_To_Inline |
3416 | ||
3417 | begin | |
8dbd1460 AC |
3418 | -- Return immediately if done already |
3419 | ||
996ae0b0 RK |
3420 | if Nkind (Decl) = N_Subprogram_Declaration |
3421 | and then Present (Body_To_Inline (Decl)) | |
3422 | then | |
8dbd1460 | 3423 | return; |
996ae0b0 | 3424 | |
08402a6d ES |
3425 | -- Functions that return unconstrained composite types require |
3426 | -- secondary stack handling, and cannot currently be inlined, unless | |
3427 | -- all return statements return a local variable that is the first | |
3428 | -- local declaration in the body. | |
996ae0b0 RK |
3429 | |
3430 | elsif Ekind (Subp) = E_Function | |
3431 | and then not Is_Scalar_Type (Etype (Subp)) | |
3432 | and then not Is_Access_Type (Etype (Subp)) | |
3433 | and then not Is_Constrained (Etype (Subp)) | |
3434 | then | |
08402a6d ES |
3435 | if not Has_Single_Return then |
3436 | Cannot_Inline | |
3437 | ("cannot inline & (unconstrained return type)?", N, Subp); | |
3438 | return; | |
3439 | end if; | |
3440 | ||
3441 | -- Ditto for functions that return controlled types, where controlled | |
3442 | -- actions interfere in complex ways with inlining. | |
2820d220 AC |
3443 | |
3444 | elsif Ekind (Subp) = E_Function | |
048e5cef | 3445 | and then Needs_Finalization (Etype (Subp)) |
2820d220 AC |
3446 | then |
3447 | Cannot_Inline | |
3448 | ("cannot inline & (controlled return type)?", N, Subp); | |
3449 | return; | |
996ae0b0 RK |
3450 | end if; |
3451 | ||
d05ef0ab AC |
3452 | if Present (Declarations (N)) |
3453 | and then Has_Excluded_Declaration (Declarations (N)) | |
996ae0b0 | 3454 | then |
d05ef0ab | 3455 | return; |
996ae0b0 RK |
3456 | end if; |
3457 | ||
3458 | if Present (Handled_Statement_Sequence (N)) then | |
fbf5a39b AC |
3459 | if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then |
3460 | Cannot_Inline | |
3461 | ("cannot inline& (exception handler)?", | |
3462 | First (Exception_Handlers (Handled_Statement_Sequence (N))), | |
3463 | Subp); | |
d05ef0ab | 3464 | return; |
996ae0b0 RK |
3465 | elsif |
3466 | Has_Excluded_Statement | |
3467 | (Statements (Handled_Statement_Sequence (N))) | |
3468 | then | |
d05ef0ab | 3469 | return; |
996ae0b0 RK |
3470 | end if; |
3471 | end if; | |
3472 | ||
3473 | -- We do not inline a subprogram that is too large, unless it is | |
3474 | -- marked Inline_Always. This pragma does not suppress the other | |
3475 | -- checks on inlining (forbidden declarations, handlers, etc). | |
3476 | ||
3477 | if Stat_Count > Max_Size | |
800621e0 | 3478 | and then not Has_Pragma_Inline_Always (Subp) |
996ae0b0 | 3479 | then |
fbf5a39b | 3480 | Cannot_Inline ("cannot inline& (body too large)?", N, Subp); |
d05ef0ab | 3481 | return; |
996ae0b0 RK |
3482 | end if; |
3483 | ||
3484 | if Has_Pending_Instantiation then | |
3485 | Cannot_Inline | |
fbf5a39b AC |
3486 | ("cannot inline& (forward instance within enclosing body)?", |
3487 | N, Subp); | |
d05ef0ab AC |
3488 | return; |
3489 | end if; | |
3490 | ||
3491 | -- Within an instance, the body to inline must be treated as a nested | |
3492 | -- generic, so that the proper global references are preserved. | |
3493 | ||
ce4e59c4 ST |
3494 | -- Note that we do not do this at the library level, because it is not |
3495 | -- needed, and furthermore this causes trouble if front end inlining | |
3496 | -- is activated (-gnatN). | |
3497 | ||
3498 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
d05ef0ab AC |
3499 | Save_Env (Scope (Current_Scope), Scope (Current_Scope)); |
3500 | Original_Body := Copy_Generic_Node (N, Empty, True); | |
3501 | else | |
3502 | Original_Body := Copy_Separate_Tree (N); | |
996ae0b0 RK |
3503 | end if; |
3504 | ||
d05ef0ab AC |
3505 | -- We need to capture references to the formals in order to substitute |
3506 | -- the actuals at the point of inlining, i.e. instantiation. To treat | |
3507 | -- the formals as globals to the body to inline, we nest it within | |
3508 | -- a dummy parameterless subprogram, declared within the real one. | |
24105bab AC |
3509 | -- To avoid generating an internal name (which is never public, and |
3510 | -- which affects serial numbers of other generated names), we use | |
3511 | -- an internal symbol that cannot conflict with user declarations. | |
d05ef0ab AC |
3512 | |
3513 | Set_Parameter_Specifications (Specification (Original_Body), No_List); | |
24105bab AC |
3514 | Set_Defining_Unit_Name |
3515 | (Specification (Original_Body), | |
3516 | Make_Defining_Identifier (Sloc (N), Name_uParent)); | |
d05ef0ab AC |
3517 | Set_Corresponding_Spec (Original_Body, Empty); |
3518 | ||
996ae0b0 RK |
3519 | Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False); |
3520 | ||
3521 | -- Set return type of function, which is also global and does not need | |
3522 | -- to be resolved. | |
3523 | ||
3524 | if Ekind (Subp) = E_Function then | |
41251c60 | 3525 | Set_Result_Definition (Specification (Body_To_Analyze), |
996ae0b0 RK |
3526 | New_Occurrence_Of (Etype (Subp), Sloc (N))); |
3527 | end if; | |
3528 | ||
3529 | if No (Declarations (N)) then | |
3530 | Set_Declarations (N, New_List (Body_To_Analyze)); | |
3531 | else | |
3532 | Append (Body_To_Analyze, Declarations (N)); | |
3533 | end if; | |
3534 | ||
3535 | Expander_Mode_Save_And_Set (False); | |
fbf5a39b | 3536 | Remove_Pragmas; |
996ae0b0 RK |
3537 | |
3538 | Analyze (Body_To_Analyze); | |
0a36105d | 3539 | Push_Scope (Defining_Entity (Body_To_Analyze)); |
996ae0b0 RK |
3540 | Save_Global_References (Original_Body); |
3541 | End_Scope; | |
3542 | Remove (Body_To_Analyze); | |
3543 | ||
3544 | Expander_Mode_Restore; | |
d05ef0ab | 3545 | |
ce4e59c4 ST |
3546 | -- Restore environment if previously saved |
3547 | ||
3548 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
d05ef0ab AC |
3549 | Restore_Env; |
3550 | end if; | |
e895b435 ES |
3551 | |
3552 | -- If secondary stk used there is no point in inlining. We have | |
3553 | -- already issued the warning in this case, so nothing to do. | |
3554 | ||
3555 | if Uses_Secondary_Stack (Body_To_Analyze) then | |
3556 | return; | |
3557 | end if; | |
3558 | ||
3559 | Set_Body_To_Inline (Decl, Original_Body); | |
3560 | Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp)); | |
3561 | Set_Is_Inlined (Subp); | |
996ae0b0 RK |
3562 | end Build_Body_To_Inline; |
3563 | ||
fbf5a39b AC |
3564 | ------------------- |
3565 | -- Cannot_Inline -- | |
3566 | ------------------- | |
3567 | ||
3568 | procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is | |
3569 | begin | |
676e8420 AC |
3570 | -- Do not emit warning if this is a predefined unit which is not the |
3571 | -- main unit. With validity checks enabled, some predefined subprograms | |
3572 | -- may contain nested subprograms and become ineligible for inlining. | |
fbf5a39b AC |
3573 | |
3574 | if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp))) | |
3575 | and then not In_Extended_Main_Source_Unit (Subp) | |
3576 | then | |
3577 | null; | |
3578 | ||
800621e0 | 3579 | elsif Has_Pragma_Inline_Always (Subp) then |
e895b435 ES |
3580 | |
3581 | -- Remove last character (question mark) to make this into an error, | |
3582 | -- because the Inline_Always pragma cannot be obeyed. | |
3583 | ||
ec4867fa | 3584 | Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp); |
fbf5a39b AC |
3585 | |
3586 | elsif Ineffective_Inline_Warnings then | |
3587 | Error_Msg_NE (Msg, N, Subp); | |
3588 | end if; | |
3589 | end Cannot_Inline; | |
3590 | ||
996ae0b0 RK |
3591 | ----------------------- |
3592 | -- Check_Conformance -- | |
3593 | ----------------------- | |
3594 | ||
3595 | procedure Check_Conformance | |
41251c60 JM |
3596 | (New_Id : Entity_Id; |
3597 | Old_Id : Entity_Id; | |
3598 | Ctype : Conformance_Type; | |
3599 | Errmsg : Boolean; | |
3600 | Conforms : out Boolean; | |
3601 | Err_Loc : Node_Id := Empty; | |
3602 | Get_Inst : Boolean := False; | |
3603 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 | 3604 | is |
996ae0b0 | 3605 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id); |
c27f2f15 RD |
3606 | -- Sets Conforms to False. If Errmsg is False, then that's all it does. |
3607 | -- If Errmsg is True, then processing continues to post an error message | |
3608 | -- for conformance error on given node. Two messages are output. The | |
3609 | -- first message points to the previous declaration with a general "no | |
3610 | -- conformance" message. The second is the detailed reason, supplied as | |
3611 | -- Msg. The parameter N provide information for a possible & insertion | |
3612 | -- in the message, and also provides the location for posting the | |
3613 | -- message in the absence of a specified Err_Loc location. | |
996ae0b0 RK |
3614 | |
3615 | ----------------------- | |
3616 | -- Conformance_Error -- | |
3617 | ----------------------- | |
3618 | ||
3619 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is | |
3620 | Enode : Node_Id; | |
3621 | ||
3622 | begin | |
3623 | Conforms := False; | |
3624 | ||
3625 | if Errmsg then | |
3626 | if No (Err_Loc) then | |
3627 | Enode := N; | |
3628 | else | |
3629 | Enode := Err_Loc; | |
3630 | end if; | |
3631 | ||
3632 | Error_Msg_Sloc := Sloc (Old_Id); | |
3633 | ||
3634 | case Ctype is | |
3635 | when Type_Conformant => | |
483c78cb | 3636 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
3637 | ("not type conformant with declaration#!", Enode); |
3638 | ||
3639 | when Mode_Conformant => | |
19590d70 | 3640 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 3641 | Error_Msg_N |
19590d70 GD |
3642 | ("not mode conformant with operation inherited#!", |
3643 | Enode); | |
3644 | else | |
ed2233dc | 3645 | Error_Msg_N |
19590d70 GD |
3646 | ("not mode conformant with declaration#!", Enode); |
3647 | end if; | |
996ae0b0 RK |
3648 | |
3649 | when Subtype_Conformant => | |
19590d70 | 3650 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 3651 | Error_Msg_N |
19590d70 GD |
3652 | ("not subtype conformant with operation inherited#!", |
3653 | Enode); | |
3654 | else | |
ed2233dc | 3655 | Error_Msg_N |
19590d70 GD |
3656 | ("not subtype conformant with declaration#!", Enode); |
3657 | end if; | |
996ae0b0 RK |
3658 | |
3659 | when Fully_Conformant => | |
19590d70 | 3660 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
483c78cb | 3661 | Error_Msg_N -- CODEFIX |
19590d70 GD |
3662 | ("not fully conformant with operation inherited#!", |
3663 | Enode); | |
3664 | else | |
483c78cb | 3665 | Error_Msg_N -- CODEFIX |
19590d70 GD |
3666 | ("not fully conformant with declaration#!", Enode); |
3667 | end if; | |
996ae0b0 RK |
3668 | end case; |
3669 | ||
3670 | Error_Msg_NE (Msg, Enode, N); | |
3671 | end if; | |
3672 | end Conformance_Error; | |
3673 | ||
ec4867fa ES |
3674 | -- Local Variables |
3675 | ||
3676 | Old_Type : constant Entity_Id := Etype (Old_Id); | |
3677 | New_Type : constant Entity_Id := Etype (New_Id); | |
3678 | Old_Formal : Entity_Id; | |
3679 | New_Formal : Entity_Id; | |
3680 | Access_Types_Match : Boolean; | |
3681 | Old_Formal_Base : Entity_Id; | |
3682 | New_Formal_Base : Entity_Id; | |
3683 | ||
996ae0b0 RK |
3684 | -- Start of processing for Check_Conformance |
3685 | ||
3686 | begin | |
3687 | Conforms := True; | |
3688 | ||
82c80734 RD |
3689 | -- We need a special case for operators, since they don't appear |
3690 | -- explicitly. | |
996ae0b0 RK |
3691 | |
3692 | if Ctype = Type_Conformant then | |
3693 | if Ekind (New_Id) = E_Operator | |
3694 | and then Operator_Matches_Spec (New_Id, Old_Id) | |
3695 | then | |
3696 | return; | |
3697 | end if; | |
3698 | end if; | |
3699 | ||
3700 | -- If both are functions/operators, check return types conform | |
3701 | ||
3702 | if Old_Type /= Standard_Void_Type | |
3703 | and then New_Type /= Standard_Void_Type | |
3704 | then | |
fceeaab6 ES |
3705 | |
3706 | -- If we are checking interface conformance we omit controlling | |
3707 | -- arguments and result, because we are only checking the conformance | |
3708 | -- of the remaining parameters. | |
3709 | ||
3710 | if Has_Controlling_Result (Old_Id) | |
3711 | and then Has_Controlling_Result (New_Id) | |
3712 | and then Skip_Controlling_Formals | |
3713 | then | |
3714 | null; | |
3715 | ||
3716 | elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then | |
5d37ba92 | 3717 | Conformance_Error ("\return type does not match!", New_Id); |
996ae0b0 RK |
3718 | return; |
3719 | end if; | |
3720 | ||
41251c60 | 3721 | -- Ada 2005 (AI-231): In case of anonymous access types check the |
0a36105d | 3722 | -- null-exclusion and access-to-constant attributes match. |
41251c60 | 3723 | |
0791fbe9 | 3724 | if Ada_Version >= Ada_2005 |
41251c60 JM |
3725 | and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type |
3726 | and then | |
3727 | (Can_Never_Be_Null (Old_Type) | |
3728 | /= Can_Never_Be_Null (New_Type) | |
3729 | or else Is_Access_Constant (Etype (Old_Type)) | |
3730 | /= Is_Access_Constant (Etype (New_Type))) | |
3731 | then | |
5d37ba92 | 3732 | Conformance_Error ("\return type does not match!", New_Id); |
41251c60 JM |
3733 | return; |
3734 | end if; | |
3735 | ||
996ae0b0 RK |
3736 | -- If either is a function/operator and the other isn't, error |
3737 | ||
3738 | elsif Old_Type /= Standard_Void_Type | |
3739 | or else New_Type /= Standard_Void_Type | |
3740 | then | |
5d37ba92 | 3741 | Conformance_Error ("\functions can only match functions!", New_Id); |
996ae0b0 RK |
3742 | return; |
3743 | end if; | |
3744 | ||
0a36105d | 3745 | -- In subtype conformant case, conventions must match (RM 6.3.1(16)). |
996ae0b0 RK |
3746 | -- If this is a renaming as body, refine error message to indicate that |
3747 | -- the conflict is with the original declaration. If the entity is not | |
3748 | -- frozen, the conventions don't have to match, the one of the renamed | |
3749 | -- entity is inherited. | |
3750 | ||
3751 | if Ctype >= Subtype_Conformant then | |
996ae0b0 RK |
3752 | if Convention (Old_Id) /= Convention (New_Id) then |
3753 | ||
3754 | if not Is_Frozen (New_Id) then | |
3755 | null; | |
3756 | ||
3757 | elsif Present (Err_Loc) | |
3758 | and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration | |
3759 | and then Present (Corresponding_Spec (Err_Loc)) | |
3760 | then | |
3761 | Error_Msg_Name_1 := Chars (New_Id); | |
3762 | Error_Msg_Name_2 := | |
3763 | Name_Ada + Convention_Id'Pos (Convention (New_Id)); | |
5d37ba92 | 3764 | Conformance_Error ("\prior declaration for% has convention %!"); |
996ae0b0 RK |
3765 | |
3766 | else | |
5d37ba92 | 3767 | Conformance_Error ("\calling conventions do not match!"); |
996ae0b0 RK |
3768 | end if; |
3769 | ||
3770 | return; | |
3771 | ||
3772 | elsif Is_Formal_Subprogram (Old_Id) | |
3773 | or else Is_Formal_Subprogram (New_Id) | |
3774 | then | |
5d37ba92 | 3775 | Conformance_Error ("\formal subprograms not allowed!"); |
996ae0b0 RK |
3776 | return; |
3777 | end if; | |
3778 | end if; | |
3779 | ||
3780 | -- Deal with parameters | |
3781 | ||
3782 | -- Note: we use the entity information, rather than going directly | |
3783 | -- to the specification in the tree. This is not only simpler, but | |
3784 | -- absolutely necessary for some cases of conformance tests between | |
3785 | -- operators, where the declaration tree simply does not exist! | |
3786 | ||
3787 | Old_Formal := First_Formal (Old_Id); | |
3788 | New_Formal := First_Formal (New_Id); | |
996ae0b0 | 3789 | while Present (Old_Formal) and then Present (New_Formal) loop |
41251c60 JM |
3790 | if Is_Controlling_Formal (Old_Formal) |
3791 | and then Is_Controlling_Formal (New_Formal) | |
3792 | and then Skip_Controlling_Formals | |
3793 | then | |
a2dc5812 AC |
3794 | -- The controlling formals will have different types when |
3795 | -- comparing an interface operation with its match, but both | |
3796 | -- or neither must be access parameters. | |
3797 | ||
3798 | if Is_Access_Type (Etype (Old_Formal)) | |
3799 | = | |
3800 | Is_Access_Type (Etype (New_Formal)) | |
3801 | then | |
3802 | goto Skip_Controlling_Formal; | |
3803 | else | |
3804 | Conformance_Error | |
3805 | ("\access parameter does not match!", New_Formal); | |
3806 | end if; | |
41251c60 JM |
3807 | end if; |
3808 | ||
fbf5a39b AC |
3809 | if Ctype = Fully_Conformant then |
3810 | ||
3811 | -- Names must match. Error message is more accurate if we do | |
3812 | -- this before checking that the types of the formals match. | |
3813 | ||
3814 | if Chars (Old_Formal) /= Chars (New_Formal) then | |
5d37ba92 | 3815 | Conformance_Error ("\name & does not match!", New_Formal); |
fbf5a39b AC |
3816 | |
3817 | -- Set error posted flag on new formal as well to stop | |
3818 | -- junk cascaded messages in some cases. | |
3819 | ||
3820 | Set_Error_Posted (New_Formal); | |
3821 | return; | |
3822 | end if; | |
40b93859 RD |
3823 | |
3824 | -- Null exclusion must match | |
3825 | ||
3826 | if Null_Exclusion_Present (Parent (Old_Formal)) | |
3827 | /= | |
3828 | Null_Exclusion_Present (Parent (New_Formal)) | |
3829 | then | |
3830 | -- Only give error if both come from source. This should be | |
3831 | -- investigated some time, since it should not be needed ??? | |
3832 | ||
3833 | if Comes_From_Source (Old_Formal) | |
3834 | and then | |
3835 | Comes_From_Source (New_Formal) | |
3836 | then | |
3837 | Conformance_Error | |
3838 | ("\null exclusion for & does not match", New_Formal); | |
3839 | ||
3840 | -- Mark error posted on the new formal to avoid duplicated | |
3841 | -- complaint about types not matching. | |
3842 | ||
3843 | Set_Error_Posted (New_Formal); | |
3844 | end if; | |
3845 | end if; | |
fbf5a39b | 3846 | end if; |
996ae0b0 | 3847 | |
ec4867fa ES |
3848 | -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This |
3849 | -- case occurs whenever a subprogram is being renamed and one of its | |
3850 | -- parameters imposes a null exclusion. For example: | |
3851 | ||
3852 | -- type T is null record; | |
3853 | -- type Acc_T is access T; | |
3854 | -- subtype Acc_T_Sub is Acc_T; | |
3855 | ||
3856 | -- procedure P (Obj : not null Acc_T_Sub); -- itype | |
3857 | -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype | |
3858 | -- renames P; | |
3859 | ||
3860 | Old_Formal_Base := Etype (Old_Formal); | |
3861 | New_Formal_Base := Etype (New_Formal); | |
3862 | ||
3863 | if Get_Inst then | |
3864 | Old_Formal_Base := Get_Instance_Of (Old_Formal_Base); | |
3865 | New_Formal_Base := Get_Instance_Of (New_Formal_Base); | |
3866 | end if; | |
3867 | ||
0791fbe9 | 3868 | Access_Types_Match := Ada_Version >= Ada_2005 |
ec4867fa ES |
3869 | |
3870 | -- Ensure that this rule is only applied when New_Id is a | |
5d37ba92 | 3871 | -- renaming of Old_Id. |
ec4867fa | 3872 | |
5d37ba92 ES |
3873 | and then Nkind (Parent (Parent (New_Id))) = |
3874 | N_Subprogram_Renaming_Declaration | |
ec4867fa ES |
3875 | and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity |
3876 | and then Present (Entity (Name (Parent (Parent (New_Id))))) | |
3877 | and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id | |
3878 | ||
3879 | -- Now handle the allowed access-type case | |
3880 | ||
3881 | and then Is_Access_Type (Old_Formal_Base) | |
3882 | and then Is_Access_Type (New_Formal_Base) | |
5d37ba92 ES |
3883 | |
3884 | -- The type kinds must match. The only exception occurs with | |
3885 | -- multiple generics of the form: | |
3886 | ||
3887 | -- generic generic | |
3888 | -- type F is private; type A is private; | |
3889 | -- type F_Ptr is access F; type A_Ptr is access A; | |
3890 | -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr); | |
3891 | -- package F_Pack is ... package A_Pack is | |
3892 | -- package F_Inst is | |
3893 | -- new F_Pack (A, A_Ptr, A_P); | |
3894 | ||
3895 | -- When checking for conformance between the parameters of A_P | |
3896 | -- and F_P, the type kinds of F_Ptr and A_Ptr will not match | |
3897 | -- because the compiler has transformed A_Ptr into a subtype of | |
3898 | -- F_Ptr. We catch this case in the code below. | |
3899 | ||
3900 | and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base) | |
3901 | or else | |
3902 | (Is_Generic_Type (Old_Formal_Base) | |
3903 | and then Is_Generic_Type (New_Formal_Base) | |
3904 | and then Is_Internal (New_Formal_Base) | |
3905 | and then Etype (Etype (New_Formal_Base)) = | |
3906 | Old_Formal_Base)) | |
ec4867fa ES |
3907 | and then Directly_Designated_Type (Old_Formal_Base) = |
3908 | Directly_Designated_Type (New_Formal_Base) | |
3909 | and then ((Is_Itype (Old_Formal_Base) | |
3910 | and then Can_Never_Be_Null (Old_Formal_Base)) | |
3911 | or else | |
3912 | (Is_Itype (New_Formal_Base) | |
3913 | and then Can_Never_Be_Null (New_Formal_Base))); | |
3914 | ||
996ae0b0 RK |
3915 | -- Types must always match. In the visible part of an instance, |
3916 | -- usual overloading rules for dispatching operations apply, and | |
3917 | -- we check base types (not the actual subtypes). | |
3918 | ||
3919 | if In_Instance_Visible_Part | |
3920 | and then Is_Dispatching_Operation (New_Id) | |
3921 | then | |
3922 | if not Conforming_Types | |
ec4867fa ES |
3923 | (T1 => Base_Type (Etype (Old_Formal)), |
3924 | T2 => Base_Type (Etype (New_Formal)), | |
3925 | Ctype => Ctype, | |
3926 | Get_Inst => Get_Inst) | |
3927 | and then not Access_Types_Match | |
996ae0b0 | 3928 | then |
5d37ba92 | 3929 | Conformance_Error ("\type of & does not match!", New_Formal); |
996ae0b0 RK |
3930 | return; |
3931 | end if; | |
3932 | ||
3933 | elsif not Conforming_Types | |
5d37ba92 ES |
3934 | (T1 => Old_Formal_Base, |
3935 | T2 => New_Formal_Base, | |
ec4867fa ES |
3936 | Ctype => Ctype, |
3937 | Get_Inst => Get_Inst) | |
3938 | and then not Access_Types_Match | |
996ae0b0 | 3939 | then |
c27f2f15 RD |
3940 | -- Don't give error message if old type is Any_Type. This test |
3941 | -- avoids some cascaded errors, e.g. in case of a bad spec. | |
3942 | ||
3943 | if Errmsg and then Old_Formal_Base = Any_Type then | |
3944 | Conforms := False; | |
3945 | else | |
3946 | Conformance_Error ("\type of & does not match!", New_Formal); | |
3947 | end if; | |
3948 | ||
996ae0b0 RK |
3949 | return; |
3950 | end if; | |
3951 | ||
3952 | -- For mode conformance, mode must match | |
3953 | ||
5d37ba92 ES |
3954 | if Ctype >= Mode_Conformant then |
3955 | if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then | |
3956 | Conformance_Error ("\mode of & does not match!", New_Formal); | |
3957 | return; | |
3958 | ||
3959 | -- Part of mode conformance for access types is having the same | |
3960 | -- constant modifier. | |
3961 | ||
3962 | elsif Access_Types_Match | |
3963 | and then Is_Access_Constant (Old_Formal_Base) /= | |
3964 | Is_Access_Constant (New_Formal_Base) | |
3965 | then | |
3966 | Conformance_Error | |
3967 | ("\constant modifier does not match!", New_Formal); | |
3968 | return; | |
3969 | end if; | |
996ae0b0 RK |
3970 | end if; |
3971 | ||
0a36105d | 3972 | if Ctype >= Subtype_Conformant then |
996ae0b0 | 3973 | |
0a36105d JM |
3974 | -- Ada 2005 (AI-231): In case of anonymous access types check |
3975 | -- the null-exclusion and access-to-constant attributes must | |
3976 | -- match. | |
996ae0b0 | 3977 | |
0791fbe9 | 3978 | if Ada_Version >= Ada_2005 |
0a36105d JM |
3979 | and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type |
3980 | and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type | |
3981 | and then | |
3982 | (Can_Never_Be_Null (Old_Formal) /= | |
3983 | Can_Never_Be_Null (New_Formal) | |
3984 | or else | |
3985 | Is_Access_Constant (Etype (Old_Formal)) /= | |
3986 | Is_Access_Constant (Etype (New_Formal))) | |
40b93859 RD |
3987 | |
3988 | -- Do not complain if error already posted on New_Formal. This | |
3989 | -- avoids some redundant error messages. | |
3990 | ||
3991 | and then not Error_Posted (New_Formal) | |
0a36105d JM |
3992 | then |
3993 | -- It is allowed to omit the null-exclusion in case of stream | |
3994 | -- attribute subprograms. We recognize stream subprograms | |
3995 | -- through their TSS-generated suffix. | |
996ae0b0 | 3996 | |
0a36105d JM |
3997 | declare |
3998 | TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id); | |
3999 | begin | |
4000 | if TSS_Name /= TSS_Stream_Read | |
4001 | and then TSS_Name /= TSS_Stream_Write | |
4002 | and then TSS_Name /= TSS_Stream_Input | |
4003 | and then TSS_Name /= TSS_Stream_Output | |
4004 | then | |
4005 | Conformance_Error | |
5d37ba92 | 4006 | ("\type of & does not match!", New_Formal); |
0a36105d JM |
4007 | return; |
4008 | end if; | |
4009 | end; | |
4010 | end if; | |
4011 | end if; | |
41251c60 | 4012 | |
0a36105d | 4013 | -- Full conformance checks |
41251c60 | 4014 | |
0a36105d | 4015 | if Ctype = Fully_Conformant then |
e660dbf7 | 4016 | |
0a36105d | 4017 | -- We have checked already that names match |
e660dbf7 | 4018 | |
0a36105d | 4019 | if Parameter_Mode (Old_Formal) = E_In_Parameter then |
41251c60 JM |
4020 | |
4021 | -- Check default expressions for in parameters | |
4022 | ||
996ae0b0 RK |
4023 | declare |
4024 | NewD : constant Boolean := | |
4025 | Present (Default_Value (New_Formal)); | |
4026 | OldD : constant Boolean := | |
4027 | Present (Default_Value (Old_Formal)); | |
4028 | begin | |
4029 | if NewD or OldD then | |
4030 | ||
82c80734 RD |
4031 | -- The old default value has been analyzed because the |
4032 | -- current full declaration will have frozen everything | |
0a36105d JM |
4033 | -- before. The new default value has not been analyzed, |
4034 | -- so analyze it now before we check for conformance. | |
996ae0b0 RK |
4035 | |
4036 | if NewD then | |
0a36105d | 4037 | Push_Scope (New_Id); |
21d27997 | 4038 | Preanalyze_Spec_Expression |
fbf5a39b | 4039 | (Default_Value (New_Formal), Etype (New_Formal)); |
996ae0b0 RK |
4040 | End_Scope; |
4041 | end if; | |
4042 | ||
4043 | if not (NewD and OldD) | |
4044 | or else not Fully_Conformant_Expressions | |
4045 | (Default_Value (Old_Formal), | |
4046 | Default_Value (New_Formal)) | |
4047 | then | |
4048 | Conformance_Error | |
5d37ba92 | 4049 | ("\default expression for & does not match!", |
996ae0b0 RK |
4050 | New_Formal); |
4051 | return; | |
4052 | end if; | |
4053 | end if; | |
4054 | end; | |
4055 | end if; | |
4056 | end if; | |
4057 | ||
4058 | -- A couple of special checks for Ada 83 mode. These checks are | |
0a36105d | 4059 | -- skipped if either entity is an operator in package Standard, |
996ae0b0 RK |
4060 | -- or if either old or new instance is not from the source program. |
4061 | ||
0ab80019 | 4062 | if Ada_Version = Ada_83 |
996ae0b0 RK |
4063 | and then Sloc (Old_Id) > Standard_Location |
4064 | and then Sloc (New_Id) > Standard_Location | |
4065 | and then Comes_From_Source (Old_Id) | |
4066 | and then Comes_From_Source (New_Id) | |
4067 | then | |
4068 | declare | |
4069 | Old_Param : constant Node_Id := Declaration_Node (Old_Formal); | |
4070 | New_Param : constant Node_Id := Declaration_Node (New_Formal); | |
4071 | ||
4072 | begin | |
4073 | -- Explicit IN must be present or absent in both cases. This | |
4074 | -- test is required only in the full conformance case. | |
4075 | ||
4076 | if In_Present (Old_Param) /= In_Present (New_Param) | |
4077 | and then Ctype = Fully_Conformant | |
4078 | then | |
4079 | Conformance_Error | |
5d37ba92 | 4080 | ("\(Ada 83) IN must appear in both declarations", |
996ae0b0 RK |
4081 | New_Formal); |
4082 | return; | |
4083 | end if; | |
4084 | ||
4085 | -- Grouping (use of comma in param lists) must be the same | |
4086 | -- This is where we catch a misconformance like: | |
4087 | ||
0a36105d | 4088 | -- A, B : Integer |
996ae0b0 RK |
4089 | -- A : Integer; B : Integer |
4090 | ||
4091 | -- which are represented identically in the tree except | |
4092 | -- for the setting of the flags More_Ids and Prev_Ids. | |
4093 | ||
4094 | if More_Ids (Old_Param) /= More_Ids (New_Param) | |
4095 | or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param) | |
4096 | then | |
4097 | Conformance_Error | |
5d37ba92 | 4098 | ("\grouping of & does not match!", New_Formal); |
996ae0b0 RK |
4099 | return; |
4100 | end if; | |
4101 | end; | |
4102 | end if; | |
4103 | ||
41251c60 JM |
4104 | -- This label is required when skipping controlling formals |
4105 | ||
4106 | <<Skip_Controlling_Formal>> | |
4107 | ||
996ae0b0 RK |
4108 | Next_Formal (Old_Formal); |
4109 | Next_Formal (New_Formal); | |
4110 | end loop; | |
4111 | ||
4112 | if Present (Old_Formal) then | |
5d37ba92 | 4113 | Conformance_Error ("\too few parameters!"); |
996ae0b0 RK |
4114 | return; |
4115 | ||
4116 | elsif Present (New_Formal) then | |
5d37ba92 | 4117 | Conformance_Error ("\too many parameters!", New_Formal); |
996ae0b0 RK |
4118 | return; |
4119 | end if; | |
996ae0b0 RK |
4120 | end Check_Conformance; |
4121 | ||
ec4867fa ES |
4122 | ----------------------- |
4123 | -- Check_Conventions -- | |
4124 | ----------------------- | |
4125 | ||
4126 | procedure Check_Conventions (Typ : Entity_Id) is | |
ce2b6ba5 | 4127 | Ifaces_List : Elist_Id; |
0a36105d | 4128 | |
ce2b6ba5 | 4129 | procedure Check_Convention (Op : Entity_Id); |
0a36105d JM |
4130 | -- Verify that the convention of inherited dispatching operation Op is |
4131 | -- consistent among all subprograms it overrides. In order to minimize | |
4132 | -- the search, Search_From is utilized to designate a specific point in | |
4133 | -- the list rather than iterating over the whole list once more. | |
ec4867fa ES |
4134 | |
4135 | ---------------------- | |
4136 | -- Check_Convention -- | |
4137 | ---------------------- | |
4138 | ||
ce2b6ba5 JM |
4139 | procedure Check_Convention (Op : Entity_Id) is |
4140 | Iface_Elmt : Elmt_Id; | |
4141 | Iface_Prim_Elmt : Elmt_Id; | |
4142 | Iface_Prim : Entity_Id; | |
ec4867fa | 4143 | |
ce2b6ba5 JM |
4144 | begin |
4145 | Iface_Elmt := First_Elmt (Ifaces_List); | |
4146 | while Present (Iface_Elmt) loop | |
4147 | Iface_Prim_Elmt := | |
4148 | First_Elmt (Primitive_Operations (Node (Iface_Elmt))); | |
4149 | while Present (Iface_Prim_Elmt) loop | |
4150 | Iface_Prim := Node (Iface_Prim_Elmt); | |
4151 | ||
4152 | if Is_Interface_Conformant (Typ, Iface_Prim, Op) | |
4153 | and then Convention (Iface_Prim) /= Convention (Op) | |
4154 | then | |
ed2233dc | 4155 | Error_Msg_N |
ce2b6ba5 | 4156 | ("inconsistent conventions in primitive operations", Typ); |
ec4867fa | 4157 | |
ce2b6ba5 JM |
4158 | Error_Msg_Name_1 := Chars (Op); |
4159 | Error_Msg_Name_2 := Get_Convention_Name (Convention (Op)); | |
4160 | Error_Msg_Sloc := Sloc (Op); | |
ec4867fa | 4161 | |
7a963087 | 4162 | if Comes_From_Source (Op) or else No (Alias (Op)) then |
ce2b6ba5 | 4163 | if not Is_Overriding_Operation (Op) then |
ed2233dc | 4164 | Error_Msg_N ("\\primitive % defined #", Typ); |
ce2b6ba5 | 4165 | else |
ed2233dc | 4166 | Error_Msg_N |
19d846a0 RD |
4167 | ("\\overriding operation % with " & |
4168 | "convention % defined #", Typ); | |
ce2b6ba5 | 4169 | end if; |
ec4867fa | 4170 | |
ce2b6ba5 JM |
4171 | else pragma Assert (Present (Alias (Op))); |
4172 | Error_Msg_Sloc := Sloc (Alias (Op)); | |
ed2233dc | 4173 | Error_Msg_N |
19d846a0 RD |
4174 | ("\\inherited operation % with " & |
4175 | "convention % defined #", Typ); | |
ce2b6ba5 | 4176 | end if; |
ec4867fa | 4177 | |
ce2b6ba5 JM |
4178 | Error_Msg_Name_1 := Chars (Op); |
4179 | Error_Msg_Name_2 := | |
4180 | Get_Convention_Name (Convention (Iface_Prim)); | |
4181 | Error_Msg_Sloc := Sloc (Iface_Prim); | |
ed2233dc | 4182 | Error_Msg_N |
19d846a0 RD |
4183 | ("\\overridden operation % with " & |
4184 | "convention % defined #", Typ); | |
ec4867fa | 4185 | |
ce2b6ba5 | 4186 | -- Avoid cascading errors |
ec4867fa | 4187 | |
ce2b6ba5 JM |
4188 | return; |
4189 | end if; | |
ec4867fa | 4190 | |
ce2b6ba5 JM |
4191 | Next_Elmt (Iface_Prim_Elmt); |
4192 | end loop; | |
ec4867fa | 4193 | |
ce2b6ba5 | 4194 | Next_Elmt (Iface_Elmt); |
ec4867fa ES |
4195 | end loop; |
4196 | end Check_Convention; | |
4197 | ||
4198 | -- Local variables | |
4199 | ||
4200 | Prim_Op : Entity_Id; | |
4201 | Prim_Op_Elmt : Elmt_Id; | |
4202 | ||
4203 | -- Start of processing for Check_Conventions | |
4204 | ||
4205 | begin | |
ce2b6ba5 JM |
4206 | if not Has_Interfaces (Typ) then |
4207 | return; | |
4208 | end if; | |
4209 | ||
4210 | Collect_Interfaces (Typ, Ifaces_List); | |
4211 | ||
0a36105d JM |
4212 | -- The algorithm checks every overriding dispatching operation against |
4213 | -- all the corresponding overridden dispatching operations, detecting | |
f3d57416 | 4214 | -- differences in conventions. |
ec4867fa ES |
4215 | |
4216 | Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
4217 | while Present (Prim_Op_Elmt) loop | |
4218 | Prim_Op := Node (Prim_Op_Elmt); | |
4219 | ||
0a36105d | 4220 | -- A small optimization: skip the predefined dispatching operations |
ce2b6ba5 | 4221 | -- since they always have the same convention. |
ec4867fa | 4222 | |
ce2b6ba5 JM |
4223 | if not Is_Predefined_Dispatching_Operation (Prim_Op) then |
4224 | Check_Convention (Prim_Op); | |
ec4867fa ES |
4225 | end if; |
4226 | ||
4227 | Next_Elmt (Prim_Op_Elmt); | |
4228 | end loop; | |
4229 | end Check_Conventions; | |
4230 | ||
996ae0b0 RK |
4231 | ------------------------------ |
4232 | -- Check_Delayed_Subprogram -- | |
4233 | ------------------------------ | |
4234 | ||
4235 | procedure Check_Delayed_Subprogram (Designator : Entity_Id) is | |
4236 | F : Entity_Id; | |
4237 | ||
4238 | procedure Possible_Freeze (T : Entity_Id); | |
4239 | -- T is the type of either a formal parameter or of the return type. | |
4240 | -- If T is not yet frozen and needs a delayed freeze, then the | |
4a13695c AC |
4241 | -- subprogram itself must be delayed. If T is the limited view of an |
4242 | -- incomplete type the subprogram must be frozen as well, because | |
4243 | -- T may depend on local types that have not been frozen yet. | |
996ae0b0 | 4244 | |
82c80734 RD |
4245 | --------------------- |
4246 | -- Possible_Freeze -- | |
4247 | --------------------- | |
4248 | ||
996ae0b0 RK |
4249 | procedure Possible_Freeze (T : Entity_Id) is |
4250 | begin | |
4a13695c | 4251 | if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then |
996ae0b0 RK |
4252 | Set_Has_Delayed_Freeze (Designator); |
4253 | ||
4254 | elsif Is_Access_Type (T) | |
4255 | and then Has_Delayed_Freeze (Designated_Type (T)) | |
4256 | and then not Is_Frozen (Designated_Type (T)) | |
4257 | then | |
4258 | Set_Has_Delayed_Freeze (Designator); | |
e358346d | 4259 | |
4a13695c | 4260 | elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then |
e358346d | 4261 | Set_Has_Delayed_Freeze (Designator); |
996ae0b0 | 4262 | end if; |
4a13695c | 4263 | |
996ae0b0 RK |
4264 | end Possible_Freeze; |
4265 | ||
4266 | -- Start of processing for Check_Delayed_Subprogram | |
4267 | ||
4268 | begin | |
76e3504f AC |
4269 | -- All subprograms, including abstract subprograms, may need a freeze |
4270 | -- node if some formal type or the return type needs one. | |
996ae0b0 | 4271 | |
76e3504f AC |
4272 | Possible_Freeze (Etype (Designator)); |
4273 | Possible_Freeze (Base_Type (Etype (Designator))); -- needed ??? | |
996ae0b0 | 4274 | |
76e3504f AC |
4275 | -- Need delayed freeze if any of the formal types themselves need |
4276 | -- a delayed freeze and are not yet frozen. | |
996ae0b0 | 4277 | |
76e3504f AC |
4278 | F := First_Formal (Designator); |
4279 | while Present (F) loop | |
4280 | Possible_Freeze (Etype (F)); | |
4281 | Possible_Freeze (Base_Type (Etype (F))); -- needed ??? | |
4282 | Next_Formal (F); | |
4283 | end loop; | |
996ae0b0 RK |
4284 | |
4285 | -- Mark functions that return by reference. Note that it cannot be | |
4286 | -- done for delayed_freeze subprograms because the underlying | |
4287 | -- returned type may not be known yet (for private types) | |
4288 | ||
4289 | if not Has_Delayed_Freeze (Designator) | |
4290 | and then Expander_Active | |
4291 | then | |
4292 | declare | |
4293 | Typ : constant Entity_Id := Etype (Designator); | |
4294 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
9694c039 | 4295 | |
996ae0b0 | 4296 | begin |
40f07b4b | 4297 | if Is_Immutably_Limited_Type (Typ) then |
996ae0b0 | 4298 | Set_Returns_By_Ref (Designator); |
9694c039 | 4299 | |
048e5cef | 4300 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
996ae0b0 RK |
4301 | Set_Returns_By_Ref (Designator); |
4302 | end if; | |
4303 | end; | |
4304 | end if; | |
4305 | end Check_Delayed_Subprogram; | |
4306 | ||
4307 | ------------------------------------ | |
4308 | -- Check_Discriminant_Conformance -- | |
4309 | ------------------------------------ | |
4310 | ||
4311 | procedure Check_Discriminant_Conformance | |
4312 | (N : Node_Id; | |
4313 | Prev : Entity_Id; | |
4314 | Prev_Loc : Node_Id) | |
4315 | is | |
4316 | Old_Discr : Entity_Id := First_Discriminant (Prev); | |
4317 | New_Discr : Node_Id := First (Discriminant_Specifications (N)); | |
4318 | New_Discr_Id : Entity_Id; | |
4319 | New_Discr_Type : Entity_Id; | |
4320 | ||
4321 | procedure Conformance_Error (Msg : String; N : Node_Id); | |
82c80734 RD |
4322 | -- Post error message for conformance error on given node. Two messages |
4323 | -- are output. The first points to the previous declaration with a | |
4324 | -- general "no conformance" message. The second is the detailed reason, | |
4325 | -- supplied as Msg. The parameter N provide information for a possible | |
4326 | -- & insertion in the message. | |
996ae0b0 RK |
4327 | |
4328 | ----------------------- | |
4329 | -- Conformance_Error -- | |
4330 | ----------------------- | |
4331 | ||
4332 | procedure Conformance_Error (Msg : String; N : Node_Id) is | |
4333 | begin | |
4334 | Error_Msg_Sloc := Sloc (Prev_Loc); | |
483c78cb RD |
4335 | Error_Msg_N -- CODEFIX |
4336 | ("not fully conformant with declaration#!", N); | |
996ae0b0 RK |
4337 | Error_Msg_NE (Msg, N, N); |
4338 | end Conformance_Error; | |
4339 | ||
4340 | -- Start of processing for Check_Discriminant_Conformance | |
4341 | ||
4342 | begin | |
4343 | while Present (Old_Discr) and then Present (New_Discr) loop | |
4344 | ||
4345 | New_Discr_Id := Defining_Identifier (New_Discr); | |
4346 | ||
82c80734 RD |
4347 | -- The subtype mark of the discriminant on the full type has not |
4348 | -- been analyzed so we do it here. For an access discriminant a new | |
4349 | -- type is created. | |
996ae0b0 RK |
4350 | |
4351 | if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then | |
4352 | New_Discr_Type := | |
4353 | Access_Definition (N, Discriminant_Type (New_Discr)); | |
4354 | ||
4355 | else | |
4356 | Analyze (Discriminant_Type (New_Discr)); | |
4357 | New_Discr_Type := Etype (Discriminant_Type (New_Discr)); | |
e50e1c5e AC |
4358 | |
4359 | -- Ada 2005: if the discriminant definition carries a null | |
4360 | -- exclusion, create an itype to check properly for consistency | |
4361 | -- with partial declaration. | |
4362 | ||
4363 | if Is_Access_Type (New_Discr_Type) | |
4364 | and then Null_Exclusion_Present (New_Discr) | |
4365 | then | |
4366 | New_Discr_Type := | |
4367 | Create_Null_Excluding_Itype | |
4368 | (T => New_Discr_Type, | |
4369 | Related_Nod => New_Discr, | |
4370 | Scope_Id => Current_Scope); | |
4371 | end if; | |
996ae0b0 RK |
4372 | end if; |
4373 | ||
4374 | if not Conforming_Types | |
4375 | (Etype (Old_Discr), New_Discr_Type, Fully_Conformant) | |
4376 | then | |
4377 | Conformance_Error ("type of & does not match!", New_Discr_Id); | |
4378 | return; | |
fbf5a39b | 4379 | else |
82c80734 RD |
4380 | -- Treat the new discriminant as an occurrence of the old one, |
4381 | -- for navigation purposes, and fill in some semantic | |
fbf5a39b AC |
4382 | -- information, for completeness. |
4383 | ||
4384 | Generate_Reference (Old_Discr, New_Discr_Id, 'r'); | |
4385 | Set_Etype (New_Discr_Id, Etype (Old_Discr)); | |
4386 | Set_Scope (New_Discr_Id, Scope (Old_Discr)); | |
996ae0b0 RK |
4387 | end if; |
4388 | ||
4389 | -- Names must match | |
4390 | ||
4391 | if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then | |
4392 | Conformance_Error ("name & does not match!", New_Discr_Id); | |
4393 | return; | |
4394 | end if; | |
4395 | ||
4396 | -- Default expressions must match | |
4397 | ||
4398 | declare | |
4399 | NewD : constant Boolean := | |
4400 | Present (Expression (New_Discr)); | |
4401 | OldD : constant Boolean := | |
4402 | Present (Expression (Parent (Old_Discr))); | |
4403 | ||
4404 | begin | |
4405 | if NewD or OldD then | |
4406 | ||
4407 | -- The old default value has been analyzed and expanded, | |
4408 | -- because the current full declaration will have frozen | |
82c80734 RD |
4409 | -- everything before. The new default values have not been |
4410 | -- expanded, so expand now to check conformance. | |
996ae0b0 RK |
4411 | |
4412 | if NewD then | |
21d27997 | 4413 | Preanalyze_Spec_Expression |
996ae0b0 RK |
4414 | (Expression (New_Discr), New_Discr_Type); |
4415 | end if; | |
4416 | ||
4417 | if not (NewD and OldD) | |
4418 | or else not Fully_Conformant_Expressions | |
4419 | (Expression (Parent (Old_Discr)), | |
4420 | Expression (New_Discr)) | |
4421 | ||
4422 | then | |
4423 | Conformance_Error | |
4424 | ("default expression for & does not match!", | |
4425 | New_Discr_Id); | |
4426 | return; | |
4427 | end if; | |
4428 | end if; | |
4429 | end; | |
4430 | ||
4431 | -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X) | |
4432 | ||
0ab80019 | 4433 | if Ada_Version = Ada_83 then |
996ae0b0 RK |
4434 | declare |
4435 | Old_Disc : constant Node_Id := Declaration_Node (Old_Discr); | |
4436 | ||
4437 | begin | |
4438 | -- Grouping (use of comma in param lists) must be the same | |
4439 | -- This is where we catch a misconformance like: | |
4440 | ||
4441 | -- A,B : Integer | |
4442 | -- A : Integer; B : Integer | |
4443 | ||
4444 | -- which are represented identically in the tree except | |
4445 | -- for the setting of the flags More_Ids and Prev_Ids. | |
4446 | ||
4447 | if More_Ids (Old_Disc) /= More_Ids (New_Discr) | |
4448 | or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr) | |
4449 | then | |
4450 | Conformance_Error | |
4451 | ("grouping of & does not match!", New_Discr_Id); | |
4452 | return; | |
4453 | end if; | |
4454 | end; | |
4455 | end if; | |
4456 | ||
4457 | Next_Discriminant (Old_Discr); | |
4458 | Next (New_Discr); | |
4459 | end loop; | |
4460 | ||
4461 | if Present (Old_Discr) then | |
4462 | Conformance_Error ("too few discriminants!", Defining_Identifier (N)); | |
4463 | return; | |
4464 | ||
4465 | elsif Present (New_Discr) then | |
4466 | Conformance_Error | |
4467 | ("too many discriminants!", Defining_Identifier (New_Discr)); | |
4468 | return; | |
4469 | end if; | |
4470 | end Check_Discriminant_Conformance; | |
4471 | ||
4472 | ---------------------------- | |
4473 | -- Check_Fully_Conformant -- | |
4474 | ---------------------------- | |
4475 | ||
4476 | procedure Check_Fully_Conformant | |
4477 | (New_Id : Entity_Id; | |
4478 | Old_Id : Entity_Id; | |
4479 | Err_Loc : Node_Id := Empty) | |
4480 | is | |
4481 | Result : Boolean; | |
81db9d77 | 4482 | pragma Warnings (Off, Result); |
996ae0b0 RK |
4483 | begin |
4484 | Check_Conformance | |
4485 | (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc); | |
4486 | end Check_Fully_Conformant; | |
4487 | ||
4488 | --------------------------- | |
4489 | -- Check_Mode_Conformant -- | |
4490 | --------------------------- | |
4491 | ||
4492 | procedure Check_Mode_Conformant | |
4493 | (New_Id : Entity_Id; | |
4494 | Old_Id : Entity_Id; | |
4495 | Err_Loc : Node_Id := Empty; | |
4496 | Get_Inst : Boolean := False) | |
4497 | is | |
4498 | Result : Boolean; | |
81db9d77 | 4499 | pragma Warnings (Off, Result); |
996ae0b0 RK |
4500 | begin |
4501 | Check_Conformance | |
4502 | (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst); | |
4503 | end Check_Mode_Conformant; | |
4504 | ||
fbf5a39b | 4505 | -------------------------------- |
758c442c | 4506 | -- Check_Overriding_Indicator -- |
fbf5a39b AC |
4507 | -------------------------------- |
4508 | ||
758c442c | 4509 | procedure Check_Overriding_Indicator |
ec4867fa | 4510 | (Subp : Entity_Id; |
5d37ba92 ES |
4511 | Overridden_Subp : Entity_Id; |
4512 | Is_Primitive : Boolean) | |
fbf5a39b | 4513 | is |
758c442c GD |
4514 | Decl : Node_Id; |
4515 | Spec : Node_Id; | |
fbf5a39b AC |
4516 | |
4517 | begin | |
ec4867fa | 4518 | -- No overriding indicator for literals |
fbf5a39b | 4519 | |
ec4867fa | 4520 | if Ekind (Subp) = E_Enumeration_Literal then |
758c442c | 4521 | return; |
fbf5a39b | 4522 | |
ec4867fa ES |
4523 | elsif Ekind (Subp) = E_Entry then |
4524 | Decl := Parent (Subp); | |
4525 | ||
53b10ce9 AC |
4526 | -- No point in analyzing a malformed operator |
4527 | ||
4528 | elsif Nkind (Subp) = N_Defining_Operator_Symbol | |
4529 | and then Error_Posted (Subp) | |
4530 | then | |
4531 | return; | |
4532 | ||
758c442c GD |
4533 | else |
4534 | Decl := Unit_Declaration_Node (Subp); | |
4535 | end if; | |
fbf5a39b | 4536 | |
800621e0 RD |
4537 | if Nkind_In (Decl, N_Subprogram_Body, |
4538 | N_Subprogram_Body_Stub, | |
4539 | N_Subprogram_Declaration, | |
4540 | N_Abstract_Subprogram_Declaration, | |
4541 | N_Subprogram_Renaming_Declaration) | |
758c442c GD |
4542 | then |
4543 | Spec := Specification (Decl); | |
ec4867fa ES |
4544 | |
4545 | elsif Nkind (Decl) = N_Entry_Declaration then | |
4546 | Spec := Decl; | |
4547 | ||
758c442c GD |
4548 | else |
4549 | return; | |
4550 | end if; | |
fbf5a39b | 4551 | |
e7d72fb9 AC |
4552 | -- The overriding operation is type conformant with the overridden one, |
4553 | -- but the names of the formals are not required to match. If the names | |
6823270c | 4554 | -- appear permuted in the overriding operation, this is a possible |
e7d72fb9 AC |
4555 | -- source of confusion that is worth diagnosing. Controlling formals |
4556 | -- often carry names that reflect the type, and it is not worthwhile | |
4557 | -- requiring that their names match. | |
4558 | ||
c9e7bd8e | 4559 | if Present (Overridden_Subp) |
e7d72fb9 AC |
4560 | and then Nkind (Subp) /= N_Defining_Operator_Symbol |
4561 | then | |
4562 | declare | |
4563 | Form1 : Entity_Id; | |
4564 | Form2 : Entity_Id; | |
4565 | ||
4566 | begin | |
4567 | Form1 := First_Formal (Subp); | |
4568 | Form2 := First_Formal (Overridden_Subp); | |
4569 | ||
c9e7bd8e AC |
4570 | -- If the overriding operation is a synchronized operation, skip |
4571 | -- the first parameter of the overridden operation, which is | |
6823270c AC |
4572 | -- implicit in the new one. If the operation is declared in the |
4573 | -- body it is not primitive and all formals must match. | |
c9e7bd8e | 4574 | |
6823270c AC |
4575 | if Is_Concurrent_Type (Scope (Subp)) |
4576 | and then Is_Tagged_Type (Scope (Subp)) | |
4577 | and then not Has_Completion (Scope (Subp)) | |
4578 | then | |
c9e7bd8e AC |
4579 | Form2 := Next_Formal (Form2); |
4580 | end if; | |
4581 | ||
e7d72fb9 AC |
4582 | if Present (Form1) then |
4583 | Form1 := Next_Formal (Form1); | |
4584 | Form2 := Next_Formal (Form2); | |
4585 | end if; | |
4586 | ||
4587 | while Present (Form1) loop | |
4588 | if not Is_Controlling_Formal (Form1) | |
4589 | and then Present (Next_Formal (Form2)) | |
4590 | and then Chars (Form1) = Chars (Next_Formal (Form2)) | |
4591 | then | |
4592 | Error_Msg_Node_2 := Alias (Overridden_Subp); | |
4593 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
ed2233dc | 4594 | Error_Msg_NE |
19d846a0 | 4595 | ("& does not match corresponding formal of&#", |
e7d72fb9 AC |
4596 | Form1, Form1); |
4597 | exit; | |
4598 | end if; | |
4599 | ||
4600 | Next_Formal (Form1); | |
4601 | Next_Formal (Form2); | |
4602 | end loop; | |
4603 | end; | |
4604 | end if; | |
4605 | ||
676e8420 AC |
4606 | -- If there is an overridden subprogram, then check that there is no |
4607 | -- "not overriding" indicator, and mark the subprogram as overriding. | |
51bf9bdf AC |
4608 | -- This is not done if the overridden subprogram is marked as hidden, |
4609 | -- which can occur for the case of inherited controlled operations | |
4610 | -- (see Derive_Subprogram), unless the inherited subprogram's parent | |
4611 | -- subprogram is not itself hidden. (Note: This condition could probably | |
4612 | -- be simplified, leaving out the testing for the specific controlled | |
4613 | -- cases, but it seems safer and clearer this way, and echoes similar | |
4614 | -- special-case tests of this kind in other places.) | |
4615 | ||
fd0d899b | 4616 | if Present (Overridden_Subp) |
51bf9bdf AC |
4617 | and then (not Is_Hidden (Overridden_Subp) |
4618 | or else | |
4619 | ((Chars (Overridden_Subp) = Name_Initialize | |
4620 | or else Chars (Overridden_Subp) = Name_Adjust | |
4621 | or else Chars (Overridden_Subp) = Name_Finalize) | |
4622 | and then Present (Alias (Overridden_Subp)) | |
4623 | and then not Is_Hidden (Alias (Overridden_Subp)))) | |
fd0d899b | 4624 | then |
ec4867fa ES |
4625 | if Must_Not_Override (Spec) then |
4626 | Error_Msg_Sloc := Sloc (Overridden_Subp); | |
fbf5a39b | 4627 | |
ec4867fa | 4628 | if Ekind (Subp) = E_Entry then |
ed2233dc | 4629 | Error_Msg_NE |
5d37ba92 | 4630 | ("entry & overrides inherited operation #", Spec, Subp); |
ec4867fa | 4631 | else |
ed2233dc | 4632 | Error_Msg_NE |
5d37ba92 | 4633 | ("subprogram & overrides inherited operation #", Spec, Subp); |
ec4867fa | 4634 | end if; |
21d27997 RD |
4635 | |
4636 | elsif Is_Subprogram (Subp) then | |
4637 | Set_Is_Overriding_Operation (Subp); | |
ec4867fa | 4638 | end if; |
f937473f | 4639 | |
618fb570 AC |
4640 | -- If primitive flag is set or this is a protected operation, then |
4641 | -- the operation is overriding at the point of its declaration, so | |
4642 | -- warn if necessary. Otherwise it may have been declared before the | |
4643 | -- operation it overrides and no check is required. | |
3c25856a AC |
4644 | |
4645 | if Style_Check | |
618fb570 AC |
4646 | and then not Must_Override (Spec) |
4647 | and then (Is_Primitive | |
4648 | or else Ekind (Scope (Subp)) = E_Protected_Type) | |
3c25856a | 4649 | then |
235f4375 AC |
4650 | Style.Missing_Overriding (Decl, Subp); |
4651 | end if; | |
4652 | ||
53b10ce9 AC |
4653 | -- If Subp is an operator, it may override a predefined operation, if |
4654 | -- it is defined in the same scope as the type to which it applies. | |
676e8420 | 4655 | -- In that case Overridden_Subp is empty because of our implicit |
5d37ba92 ES |
4656 | -- representation for predefined operators. We have to check whether the |
4657 | -- signature of Subp matches that of a predefined operator. Note that | |
4658 | -- first argument provides the name of the operator, and the second | |
4659 | -- argument the signature that may match that of a standard operation. | |
21d27997 RD |
4660 | -- If the indicator is overriding, then the operator must match a |
4661 | -- predefined signature, because we know already that there is no | |
4662 | -- explicit overridden operation. | |
f937473f | 4663 | |
21d27997 | 4664 | elsif Nkind (Subp) = N_Defining_Operator_Symbol then |
53b10ce9 | 4665 | declare |
16c5f1c6 | 4666 | Typ : constant Entity_Id := |
676e8420 | 4667 | Base_Type (Etype (First_Formal (Subp))); |
16c5f1c6 | 4668 | |
53b10ce9 | 4669 | Can_Override : constant Boolean := |
16c5f1c6 RD |
4670 | Operator_Matches_Spec (Subp, Subp) |
4671 | and then Scope (Subp) = Scope (Typ) | |
4672 | and then not Is_Class_Wide_Type (Typ); | |
f937473f | 4673 | |
53b10ce9 AC |
4674 | begin |
4675 | if Must_Not_Override (Spec) then | |
618fb570 | 4676 | |
16c5f1c6 RD |
4677 | -- If this is not a primitive or a protected subprogram, then |
4678 | -- "not overriding" is illegal. | |
618fb570 | 4679 | |
53b10ce9 AC |
4680 | if not Is_Primitive |
4681 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
4682 | then | |
ed2233dc | 4683 | Error_Msg_N |
53b10ce9 | 4684 | ("overriding indicator only allowed " |
16c5f1c6 | 4685 | & "if subprogram is primitive", Subp); |
f937473f | 4686 | |
53b10ce9 | 4687 | elsif Can_Override then |
ed2233dc | 4688 | Error_Msg_NE |
676e8420 | 4689 | ("subprogram& overrides predefined operator ", Spec, Subp); |
53b10ce9 | 4690 | end if; |
5d37ba92 | 4691 | |
53b10ce9 AC |
4692 | elsif Must_Override (Spec) then |
4693 | if Is_Overriding_Operation (Subp) then | |
b4d7b435 | 4694 | null; |
235f4375 | 4695 | |
53b10ce9 | 4696 | elsif not Can_Override then |
ed2233dc | 4697 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
53b10ce9 | 4698 | end if; |
235f4375 | 4699 | |
53b10ce9 AC |
4700 | elsif not Error_Posted (Subp) |
4701 | and then Style_Check | |
4702 | and then Can_Override | |
4703 | and then | |
4704 | not Is_Predefined_File_Name | |
16c5f1c6 | 4705 | (Unit_File_Name (Get_Source_Unit (Subp))) |
53b10ce9 AC |
4706 | then |
4707 | Set_Is_Overriding_Operation (Subp); | |
5d5832bc | 4708 | |
53b10ce9 AC |
4709 | -- If style checks are enabled, indicate that the indicator is |
4710 | -- missing. However, at the point of declaration, the type of | |
4711 | -- which this is a primitive operation may be private, in which | |
4712 | -- case the indicator would be premature. | |
5d5832bc | 4713 | |
53b10ce9 AC |
4714 | if Has_Private_Declaration (Etype (Subp)) |
4715 | or else Has_Private_Declaration (Etype (First_Formal (Subp))) | |
4716 | then | |
4717 | null; | |
4718 | else | |
4719 | Style.Missing_Overriding (Decl, Subp); | |
4720 | end if; | |
5d5832bc | 4721 | end if; |
53b10ce9 | 4722 | end; |
21d27997 RD |
4723 | |
4724 | elsif Must_Override (Spec) then | |
4725 | if Ekind (Subp) = E_Entry then | |
ed2233dc | 4726 | Error_Msg_NE ("entry & is not overriding", Spec, Subp); |
5d37ba92 | 4727 | else |
ed2233dc | 4728 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
758c442c | 4729 | end if; |
5d37ba92 ES |
4730 | |
4731 | -- If the operation is marked "not overriding" and it's not primitive | |
4732 | -- then an error is issued, unless this is an operation of a task or | |
4733 | -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding" | |
4734 | -- has been specified have already been checked above. | |
4735 | ||
4736 | elsif Must_Not_Override (Spec) | |
4737 | and then not Is_Primitive | |
4738 | and then Ekind (Subp) /= E_Entry | |
4739 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
4740 | then | |
ed2233dc | 4741 | Error_Msg_N |
5d37ba92 ES |
4742 | ("overriding indicator only allowed if subprogram is primitive", |
4743 | Subp); | |
5d37ba92 | 4744 | return; |
fbf5a39b | 4745 | end if; |
758c442c | 4746 | end Check_Overriding_Indicator; |
fbf5a39b | 4747 | |
996ae0b0 RK |
4748 | ------------------- |
4749 | -- Check_Returns -- | |
4750 | ------------------- | |
4751 | ||
0a36105d JM |
4752 | -- Note: this procedure needs to know far too much about how the expander |
4753 | -- messes with exceptions. The use of the flag Exception_Junk and the | |
4754 | -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers | |
4755 | -- works, but is not very clean. It would be better if the expansion | |
4756 | -- routines would leave Original_Node working nicely, and we could use | |
4757 | -- Original_Node here to ignore all the peculiar expander messing ??? | |
4758 | ||
996ae0b0 RK |
4759 | procedure Check_Returns |
4760 | (HSS : Node_Id; | |
4761 | Mode : Character; | |
c8ef728f ES |
4762 | Err : out Boolean; |
4763 | Proc : Entity_Id := Empty) | |
996ae0b0 RK |
4764 | is |
4765 | Handler : Node_Id; | |
4766 | ||
4767 | procedure Check_Statement_Sequence (L : List_Id); | |
4768 | -- Internal recursive procedure to check a list of statements for proper | |
4769 | -- termination by a return statement (or a transfer of control or a | |
4770 | -- compound statement that is itself internally properly terminated). | |
4771 | ||
4772 | ------------------------------ | |
4773 | -- Check_Statement_Sequence -- | |
4774 | ------------------------------ | |
4775 | ||
4776 | procedure Check_Statement_Sequence (L : List_Id) is | |
4777 | Last_Stm : Node_Id; | |
0a36105d | 4778 | Stm : Node_Id; |
996ae0b0 RK |
4779 | Kind : Node_Kind; |
4780 | ||
4781 | Raise_Exception_Call : Boolean; | |
4782 | -- Set True if statement sequence terminated by Raise_Exception call | |
4783 | -- or a Reraise_Occurrence call. | |
4784 | ||
4785 | begin | |
4786 | Raise_Exception_Call := False; | |
4787 | ||
4788 | -- Get last real statement | |
4789 | ||
4790 | Last_Stm := Last (L); | |
4791 | ||
0a36105d JM |
4792 | -- Deal with digging out exception handler statement sequences that |
4793 | -- have been transformed by the local raise to goto optimization. | |
4794 | -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this | |
4795 | -- optimization has occurred, we are looking at something like: | |
4796 | ||
4797 | -- begin | |
4798 | -- original stmts in block | |
4799 | ||
4800 | -- exception \ | |
4801 | -- when excep1 => | | |
4802 | -- goto L1; | omitted if No_Exception_Propagation | |
4803 | -- when excep2 => | | |
4804 | -- goto L2; / | |
4805 | -- end; | |
4806 | ||
4807 | -- goto L3; -- skip handler when exception not raised | |
4808 | ||
4809 | -- <<L1>> -- target label for local exception | |
4810 | -- begin | |
4811 | -- estmts1 | |
4812 | -- end; | |
4813 | ||
4814 | -- goto L3; | |
4815 | ||
4816 | -- <<L2>> | |
4817 | -- begin | |
4818 | -- estmts2 | |
4819 | -- end; | |
4820 | ||
4821 | -- <<L3>> | |
4822 | ||
4823 | -- and what we have to do is to dig out the estmts1 and estmts2 | |
4824 | -- sequences (which were the original sequences of statements in | |
4825 | -- the exception handlers) and check them. | |
4826 | ||
4827 | if Nkind (Last_Stm) = N_Label | |
4828 | and then Exception_Junk (Last_Stm) | |
4829 | then | |
4830 | Stm := Last_Stm; | |
4831 | loop | |
4832 | Prev (Stm); | |
4833 | exit when No (Stm); | |
4834 | exit when Nkind (Stm) /= N_Block_Statement; | |
4835 | exit when not Exception_Junk (Stm); | |
4836 | Prev (Stm); | |
4837 | exit when No (Stm); | |
4838 | exit when Nkind (Stm) /= N_Label; | |
4839 | exit when not Exception_Junk (Stm); | |
4840 | Check_Statement_Sequence | |
4841 | (Statements (Handled_Statement_Sequence (Next (Stm)))); | |
4842 | ||
4843 | Prev (Stm); | |
4844 | Last_Stm := Stm; | |
4845 | exit when No (Stm); | |
4846 | exit when Nkind (Stm) /= N_Goto_Statement; | |
4847 | exit when not Exception_Junk (Stm); | |
4848 | end loop; | |
4849 | end if; | |
4850 | ||
996ae0b0 RK |
4851 | -- Don't count pragmas |
4852 | ||
4853 | while Nkind (Last_Stm) = N_Pragma | |
4854 | ||
4855 | -- Don't count call to SS_Release (can happen after Raise_Exception) | |
4856 | ||
4857 | or else | |
4858 | (Nkind (Last_Stm) = N_Procedure_Call_Statement | |
4859 | and then | |
4860 | Nkind (Name (Last_Stm)) = N_Identifier | |
4861 | and then | |
4862 | Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release)) | |
4863 | ||
4864 | -- Don't count exception junk | |
4865 | ||
4866 | or else | |
800621e0 RD |
4867 | (Nkind_In (Last_Stm, N_Goto_Statement, |
4868 | N_Label, | |
4869 | N_Object_Declaration) | |
0a36105d JM |
4870 | and then Exception_Junk (Last_Stm)) |
4871 | or else Nkind (Last_Stm) in N_Push_xxx_Label | |
4872 | or else Nkind (Last_Stm) in N_Pop_xxx_Label | |
996ae0b0 RK |
4873 | loop |
4874 | Prev (Last_Stm); | |
4875 | end loop; | |
4876 | ||
4877 | -- Here we have the "real" last statement | |
4878 | ||
4879 | Kind := Nkind (Last_Stm); | |
4880 | ||
4881 | -- Transfer of control, OK. Note that in the No_Return procedure | |
4882 | -- case, we already diagnosed any explicit return statements, so | |
4883 | -- we can treat them as OK in this context. | |
4884 | ||
4885 | if Is_Transfer (Last_Stm) then | |
4886 | return; | |
4887 | ||
4888 | -- Check cases of explicit non-indirect procedure calls | |
4889 | ||
4890 | elsif Kind = N_Procedure_Call_Statement | |
4891 | and then Is_Entity_Name (Name (Last_Stm)) | |
4892 | then | |
4893 | -- Check call to Raise_Exception procedure which is treated | |
4894 | -- specially, as is a call to Reraise_Occurrence. | |
4895 | ||
4896 | -- We suppress the warning in these cases since it is likely that | |
4897 | -- the programmer really does not expect to deal with the case | |
4898 | -- of Null_Occurrence, and thus would find a warning about a | |
4899 | -- missing return curious, and raising Program_Error does not | |
4900 | -- seem such a bad behavior if this does occur. | |
4901 | ||
c8ef728f ES |
4902 | -- Note that in the Ada 2005 case for Raise_Exception, the actual |
4903 | -- behavior will be to raise Constraint_Error (see AI-329). | |
4904 | ||
996ae0b0 RK |
4905 | if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception) |
4906 | or else | |
4907 | Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence) | |
4908 | then | |
4909 | Raise_Exception_Call := True; | |
4910 | ||
4911 | -- For Raise_Exception call, test first argument, if it is | |
4912 | -- an attribute reference for a 'Identity call, then we know | |
4913 | -- that the call cannot possibly return. | |
4914 | ||
4915 | declare | |
4916 | Arg : constant Node_Id := | |
4917 | Original_Node (First_Actual (Last_Stm)); | |
996ae0b0 RK |
4918 | begin |
4919 | if Nkind (Arg) = N_Attribute_Reference | |
4920 | and then Attribute_Name (Arg) = Name_Identity | |
4921 | then | |
4922 | return; | |
4923 | end if; | |
4924 | end; | |
4925 | end if; | |
4926 | ||
4927 | -- If statement, need to look inside if there is an else and check | |
4928 | -- each constituent statement sequence for proper termination. | |
4929 | ||
4930 | elsif Kind = N_If_Statement | |
4931 | and then Present (Else_Statements (Last_Stm)) | |
4932 | then | |
4933 | Check_Statement_Sequence (Then_Statements (Last_Stm)); | |
4934 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
4935 | ||
4936 | if Present (Elsif_Parts (Last_Stm)) then | |
4937 | declare | |
4938 | Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm)); | |
4939 | ||
4940 | begin | |
4941 | while Present (Elsif_Part) loop | |
4942 | Check_Statement_Sequence (Then_Statements (Elsif_Part)); | |
4943 | Next (Elsif_Part); | |
4944 | end loop; | |
4945 | end; | |
4946 | end if; | |
4947 | ||
4948 | return; | |
4949 | ||
4950 | -- Case statement, check each case for proper termination | |
4951 | ||
4952 | elsif Kind = N_Case_Statement then | |
4953 | declare | |
4954 | Case_Alt : Node_Id; | |
996ae0b0 RK |
4955 | begin |
4956 | Case_Alt := First_Non_Pragma (Alternatives (Last_Stm)); | |
4957 | while Present (Case_Alt) loop | |
4958 | Check_Statement_Sequence (Statements (Case_Alt)); | |
4959 | Next_Non_Pragma (Case_Alt); | |
4960 | end loop; | |
4961 | end; | |
4962 | ||
4963 | return; | |
4964 | ||
4965 | -- Block statement, check its handled sequence of statements | |
4966 | ||
4967 | elsif Kind = N_Block_Statement then | |
4968 | declare | |
4969 | Err1 : Boolean; | |
4970 | ||
4971 | begin | |
4972 | Check_Returns | |
4973 | (Handled_Statement_Sequence (Last_Stm), Mode, Err1); | |
4974 | ||
4975 | if Err1 then | |
4976 | Err := True; | |
4977 | end if; | |
4978 | ||
4979 | return; | |
4980 | end; | |
4981 | ||
4982 | -- Loop statement. If there is an iteration scheme, we can definitely | |
4983 | -- fall out of the loop. Similarly if there is an exit statement, we | |
4984 | -- can fall out. In either case we need a following return. | |
4985 | ||
4986 | elsif Kind = N_Loop_Statement then | |
4987 | if Present (Iteration_Scheme (Last_Stm)) | |
4988 | or else Has_Exit (Entity (Identifier (Last_Stm))) | |
4989 | then | |
4990 | null; | |
4991 | ||
f3d57416 RW |
4992 | -- A loop with no exit statement or iteration scheme is either |
4993 | -- an infinite loop, or it has some other exit (raise/return). | |
996ae0b0 RK |
4994 | -- In either case, no warning is required. |
4995 | ||
4996 | else | |
4997 | return; | |
4998 | end if; | |
4999 | ||
5000 | -- Timed entry call, check entry call and delay alternatives | |
5001 | ||
5002 | -- Note: in expanded code, the timed entry call has been converted | |
5003 | -- to a set of expanded statements on which the check will work | |
5004 | -- correctly in any case. | |
5005 | ||
5006 | elsif Kind = N_Timed_Entry_Call then | |
5007 | declare | |
5008 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
5009 | DCA : constant Node_Id := Delay_Alternative (Last_Stm); | |
5010 | ||
5011 | begin | |
5012 | -- If statement sequence of entry call alternative is missing, | |
5013 | -- then we can definitely fall through, and we post the error | |
5014 | -- message on the entry call alternative itself. | |
5015 | ||
5016 | if No (Statements (ECA)) then | |
5017 | Last_Stm := ECA; | |
5018 | ||
5019 | -- If statement sequence of delay alternative is missing, then | |
5020 | -- we can definitely fall through, and we post the error | |
5021 | -- message on the delay alternative itself. | |
5022 | ||
5023 | -- Note: if both ECA and DCA are missing the return, then we | |
5024 | -- post only one message, should be enough to fix the bugs. | |
5025 | -- If not we will get a message next time on the DCA when the | |
5026 | -- ECA is fixed! | |
5027 | ||
5028 | elsif No (Statements (DCA)) then | |
5029 | Last_Stm := DCA; | |
5030 | ||
5031 | -- Else check both statement sequences | |
5032 | ||
5033 | else | |
5034 | Check_Statement_Sequence (Statements (ECA)); | |
5035 | Check_Statement_Sequence (Statements (DCA)); | |
5036 | return; | |
5037 | end if; | |
5038 | end; | |
5039 | ||
5040 | -- Conditional entry call, check entry call and else part | |
5041 | ||
5042 | -- Note: in expanded code, the conditional entry call has been | |
5043 | -- converted to a set of expanded statements on which the check | |
5044 | -- will work correctly in any case. | |
5045 | ||
5046 | elsif Kind = N_Conditional_Entry_Call then | |
5047 | declare | |
5048 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
5049 | ||
5050 | begin | |
5051 | -- If statement sequence of entry call alternative is missing, | |
5052 | -- then we can definitely fall through, and we post the error | |
5053 | -- message on the entry call alternative itself. | |
5054 | ||
5055 | if No (Statements (ECA)) then | |
5056 | Last_Stm := ECA; | |
5057 | ||
5058 | -- Else check statement sequence and else part | |
5059 | ||
5060 | else | |
5061 | Check_Statement_Sequence (Statements (ECA)); | |
5062 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
5063 | return; | |
5064 | end if; | |
5065 | end; | |
5066 | end if; | |
5067 | ||
5068 | -- If we fall through, issue appropriate message | |
5069 | ||
5070 | if Mode = 'F' then | |
996ae0b0 RK |
5071 | if not Raise_Exception_Call then |
5072 | Error_Msg_N | |
5d37ba92 | 5073 | ("?RETURN statement missing following this statement!", |
996ae0b0 RK |
5074 | Last_Stm); |
5075 | Error_Msg_N | |
5d37ba92 | 5076 | ("\?Program_Error may be raised at run time!", |
996ae0b0 RK |
5077 | Last_Stm); |
5078 | end if; | |
5079 | ||
5080 | -- Note: we set Err even though we have not issued a warning | |
5081 | -- because we still have a case of a missing return. This is | |
5082 | -- an extremely marginal case, probably will never be noticed | |
5083 | -- but we might as well get it right. | |
5084 | ||
5085 | Err := True; | |
5086 | ||
c8ef728f ES |
5087 | -- Otherwise we have the case of a procedure marked No_Return |
5088 | ||
996ae0b0 | 5089 | else |
800621e0 RD |
5090 | if not Raise_Exception_Call then |
5091 | Error_Msg_N | |
5092 | ("?implied return after this statement " & | |
5093 | "will raise Program_Error", | |
5094 | Last_Stm); | |
5095 | Error_Msg_NE | |
5096 | ("\?procedure & is marked as No_Return!", | |
5097 | Last_Stm, Proc); | |
5098 | end if; | |
c8ef728f ES |
5099 | |
5100 | declare | |
5101 | RE : constant Node_Id := | |
5102 | Make_Raise_Program_Error (Sloc (Last_Stm), | |
5103 | Reason => PE_Implicit_Return); | |
5104 | begin | |
5105 | Insert_After (Last_Stm, RE); | |
5106 | Analyze (RE); | |
5107 | end; | |
996ae0b0 RK |
5108 | end if; |
5109 | end Check_Statement_Sequence; | |
5110 | ||
5111 | -- Start of processing for Check_Returns | |
5112 | ||
5113 | begin | |
5114 | Err := False; | |
5115 | Check_Statement_Sequence (Statements (HSS)); | |
5116 | ||
5117 | if Present (Exception_Handlers (HSS)) then | |
5118 | Handler := First_Non_Pragma (Exception_Handlers (HSS)); | |
5119 | while Present (Handler) loop | |
5120 | Check_Statement_Sequence (Statements (Handler)); | |
5121 | Next_Non_Pragma (Handler); | |
5122 | end loop; | |
5123 | end if; | |
5124 | end Check_Returns; | |
5125 | ||
5126 | ---------------------------- | |
5127 | -- Check_Subprogram_Order -- | |
5128 | ---------------------------- | |
5129 | ||
5130 | procedure Check_Subprogram_Order (N : Node_Id) is | |
5131 | ||
5132 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean; | |
5133 | -- This is used to check if S1 > S2 in the sense required by this | |
5134 | -- test, for example nameab < namec, but name2 < name10. | |
5135 | ||
82c80734 RD |
5136 | ----------------------------- |
5137 | -- Subprogram_Name_Greater -- | |
5138 | ----------------------------- | |
5139 | ||
996ae0b0 RK |
5140 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean is |
5141 | L1, L2 : Positive; | |
5142 | N1, N2 : Natural; | |
5143 | ||
5144 | begin | |
5145 | -- Remove trailing numeric parts | |
5146 | ||
5147 | L1 := S1'Last; | |
5148 | while S1 (L1) in '0' .. '9' loop | |
5149 | L1 := L1 - 1; | |
5150 | end loop; | |
5151 | ||
5152 | L2 := S2'Last; | |
5153 | while S2 (L2) in '0' .. '9' loop | |
5154 | L2 := L2 - 1; | |
5155 | end loop; | |
5156 | ||
5157 | -- If non-numeric parts non-equal, that's decisive | |
5158 | ||
5159 | if S1 (S1'First .. L1) < S2 (S2'First .. L2) then | |
5160 | return False; | |
5161 | ||
5162 | elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then | |
5163 | return True; | |
5164 | ||
5165 | -- If non-numeric parts equal, compare suffixed numeric parts. Note | |
5166 | -- that a missing suffix is treated as numeric zero in this test. | |
5167 | ||
5168 | else | |
5169 | N1 := 0; | |
5170 | while L1 < S1'Last loop | |
5171 | L1 := L1 + 1; | |
5172 | N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0'); | |
5173 | end loop; | |
5174 | ||
5175 | N2 := 0; | |
5176 | while L2 < S2'Last loop | |
5177 | L2 := L2 + 1; | |
5178 | N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0'); | |
5179 | end loop; | |
5180 | ||
5181 | return N1 > N2; | |
5182 | end if; | |
5183 | end Subprogram_Name_Greater; | |
5184 | ||
5185 | -- Start of processing for Check_Subprogram_Order | |
5186 | ||
5187 | begin | |
5188 | -- Check body in alpha order if this is option | |
5189 | ||
fbf5a39b | 5190 | if Style_Check |
bc202b70 | 5191 | and then Style_Check_Order_Subprograms |
996ae0b0 RK |
5192 | and then Nkind (N) = N_Subprogram_Body |
5193 | and then Comes_From_Source (N) | |
5194 | and then In_Extended_Main_Source_Unit (N) | |
5195 | then | |
5196 | declare | |
5197 | LSN : String_Ptr | |
5198 | renames Scope_Stack.Table | |
5199 | (Scope_Stack.Last).Last_Subprogram_Name; | |
5200 | ||
5201 | Body_Id : constant Entity_Id := | |
5202 | Defining_Entity (Specification (N)); | |
5203 | ||
5204 | begin | |
5205 | Get_Decoded_Name_String (Chars (Body_Id)); | |
5206 | ||
5207 | if LSN /= null then | |
5208 | if Subprogram_Name_Greater | |
5209 | (LSN.all, Name_Buffer (1 .. Name_Len)) | |
5210 | then | |
5211 | Style.Subprogram_Not_In_Alpha_Order (Body_Id); | |
5212 | end if; | |
5213 | ||
5214 | Free (LSN); | |
5215 | end if; | |
5216 | ||
5217 | LSN := new String'(Name_Buffer (1 .. Name_Len)); | |
5218 | end; | |
5219 | end if; | |
5220 | end Check_Subprogram_Order; | |
5221 | ||
5222 | ------------------------------ | |
5223 | -- Check_Subtype_Conformant -- | |
5224 | ------------------------------ | |
5225 | ||
5226 | procedure Check_Subtype_Conformant | |
ce2b6ba5 JM |
5227 | (New_Id : Entity_Id; |
5228 | Old_Id : Entity_Id; | |
5229 | Err_Loc : Node_Id := Empty; | |
5230 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 RK |
5231 | is |
5232 | Result : Boolean; | |
81db9d77 | 5233 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5234 | begin |
5235 | Check_Conformance | |
ce2b6ba5 JM |
5236 | (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc, |
5237 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
5238 | end Check_Subtype_Conformant; |
5239 | ||
5240 | --------------------------- | |
5241 | -- Check_Type_Conformant -- | |
5242 | --------------------------- | |
5243 | ||
5244 | procedure Check_Type_Conformant | |
5245 | (New_Id : Entity_Id; | |
5246 | Old_Id : Entity_Id; | |
5247 | Err_Loc : Node_Id := Empty) | |
5248 | is | |
5249 | Result : Boolean; | |
81db9d77 | 5250 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5251 | begin |
5252 | Check_Conformance | |
5253 | (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc); | |
5254 | end Check_Type_Conformant; | |
5255 | ||
5256 | ---------------------- | |
5257 | -- Conforming_Types -- | |
5258 | ---------------------- | |
5259 | ||
5260 | function Conforming_Types | |
5261 | (T1 : Entity_Id; | |
5262 | T2 : Entity_Id; | |
5263 | Ctype : Conformance_Type; | |
d05ef0ab | 5264 | Get_Inst : Boolean := False) return Boolean |
996ae0b0 RK |
5265 | is |
5266 | Type_1 : Entity_Id := T1; | |
5267 | Type_2 : Entity_Id := T2; | |
af4b9434 | 5268 | Are_Anonymous_Access_To_Subprogram_Types : Boolean := False; |
996ae0b0 RK |
5269 | |
5270 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean; | |
0a36105d JM |
5271 | -- If neither T1 nor T2 are generic actual types, or if they are in |
5272 | -- different scopes (e.g. parent and child instances), then verify that | |
5273 | -- the base types are equal. Otherwise T1 and T2 must be on the same | |
5274 | -- subtype chain. The whole purpose of this procedure is to prevent | |
5275 | -- spurious ambiguities in an instantiation that may arise if two | |
5276 | -- distinct generic types are instantiated with the same actual. | |
5277 | ||
5d37ba92 ES |
5278 | function Find_Designated_Type (T : Entity_Id) return Entity_Id; |
5279 | -- An access parameter can designate an incomplete type. If the | |
5280 | -- incomplete type is the limited view of a type from a limited_ | |
5281 | -- with_clause, check whether the non-limited view is available. If | |
5282 | -- it is a (non-limited) incomplete type, get the full view. | |
5283 | ||
0a36105d JM |
5284 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean; |
5285 | -- Returns True if and only if either T1 denotes a limited view of T2 | |
5286 | -- or T2 denotes a limited view of T1. This can arise when the limited | |
5287 | -- with view of a type is used in a subprogram declaration and the | |
5288 | -- subprogram body is in the scope of a regular with clause for the | |
5289 | -- same unit. In such a case, the two type entities can be considered | |
5290 | -- identical for purposes of conformance checking. | |
996ae0b0 RK |
5291 | |
5292 | ---------------------- | |
5293 | -- Base_Types_Match -- | |
5294 | ---------------------- | |
5295 | ||
5296 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is | |
5297 | begin | |
5298 | if T1 = T2 then | |
5299 | return True; | |
5300 | ||
5301 | elsif Base_Type (T1) = Base_Type (T2) then | |
5302 | ||
0a36105d | 5303 | -- The following is too permissive. A more precise test should |
996ae0b0 RK |
5304 | -- check that the generic actual is an ancestor subtype of the |
5305 | -- other ???. | |
5306 | ||
5307 | return not Is_Generic_Actual_Type (T1) | |
07fc65c4 GB |
5308 | or else not Is_Generic_Actual_Type (T2) |
5309 | or else Scope (T1) /= Scope (T2); | |
996ae0b0 | 5310 | |
0a36105d JM |
5311 | else |
5312 | return False; | |
5313 | end if; | |
5314 | end Base_Types_Match; | |
aa720a54 | 5315 | |
5d37ba92 ES |
5316 | -------------------------- |
5317 | -- Find_Designated_Type -- | |
5318 | -------------------------- | |
5319 | ||
5320 | function Find_Designated_Type (T : Entity_Id) return Entity_Id is | |
5321 | Desig : Entity_Id; | |
5322 | ||
5323 | begin | |
5324 | Desig := Directly_Designated_Type (T); | |
5325 | ||
5326 | if Ekind (Desig) = E_Incomplete_Type then | |
5327 | ||
5328 | -- If regular incomplete type, get full view if available | |
5329 | ||
5330 | if Present (Full_View (Desig)) then | |
5331 | Desig := Full_View (Desig); | |
5332 | ||
5333 | -- If limited view of a type, get non-limited view if available, | |
5334 | -- and check again for a regular incomplete type. | |
5335 | ||
5336 | elsif Present (Non_Limited_View (Desig)) then | |
5337 | Desig := Get_Full_View (Non_Limited_View (Desig)); | |
5338 | end if; | |
5339 | end if; | |
5340 | ||
5341 | return Desig; | |
5342 | end Find_Designated_Type; | |
5343 | ||
0a36105d JM |
5344 | ------------------------------- |
5345 | -- Matches_Limited_With_View -- | |
5346 | ------------------------------- | |
5347 | ||
5348 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is | |
5349 | begin | |
5350 | -- In some cases a type imported through a limited_with clause, and | |
5351 | -- its nonlimited view are both visible, for example in an anonymous | |
5352 | -- access-to-class-wide type in a formal. Both entities designate the | |
5353 | -- same type. | |
5354 | ||
5355 | if From_With_Type (T1) | |
5356 | and then T2 = Available_View (T1) | |
aa720a54 AC |
5357 | then |
5358 | return True; | |
5359 | ||
41251c60 | 5360 | elsif From_With_Type (T2) |
0a36105d | 5361 | and then T1 = Available_View (T2) |
41251c60 JM |
5362 | then |
5363 | return True; | |
5364 | ||
996ae0b0 RK |
5365 | else |
5366 | return False; | |
5367 | end if; | |
0a36105d | 5368 | end Matches_Limited_With_View; |
996ae0b0 | 5369 | |
ec4867fa | 5370 | -- Start of processing for Conforming_Types |
758c442c | 5371 | |
996ae0b0 RK |
5372 | begin |
5373 | -- The context is an instance association for a formal | |
82c80734 RD |
5374 | -- access-to-subprogram type; the formal parameter types require |
5375 | -- mapping because they may denote other formal parameters of the | |
5376 | -- generic unit. | |
996ae0b0 RK |
5377 | |
5378 | if Get_Inst then | |
5379 | Type_1 := Get_Instance_Of (T1); | |
5380 | Type_2 := Get_Instance_Of (T2); | |
5381 | end if; | |
5382 | ||
0a36105d JM |
5383 | -- If one of the types is a view of the other introduced by a limited |
5384 | -- with clause, treat these as conforming for all purposes. | |
996ae0b0 | 5385 | |
0a36105d JM |
5386 | if Matches_Limited_With_View (T1, T2) then |
5387 | return True; | |
5388 | ||
5389 | elsif Base_Types_Match (Type_1, Type_2) then | |
996ae0b0 RK |
5390 | return Ctype <= Mode_Conformant |
5391 | or else Subtypes_Statically_Match (Type_1, Type_2); | |
5392 | ||
5393 | elsif Is_Incomplete_Or_Private_Type (Type_1) | |
5394 | and then Present (Full_View (Type_1)) | |
5395 | and then Base_Types_Match (Full_View (Type_1), Type_2) | |
5396 | then | |
5397 | return Ctype <= Mode_Conformant | |
5398 | or else Subtypes_Statically_Match (Full_View (Type_1), Type_2); | |
5399 | ||
5400 | elsif Ekind (Type_2) = E_Incomplete_Type | |
5401 | and then Present (Full_View (Type_2)) | |
5402 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
5403 | then | |
5404 | return Ctype <= Mode_Conformant | |
5405 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
fbf5a39b AC |
5406 | |
5407 | elsif Is_Private_Type (Type_2) | |
5408 | and then In_Instance | |
5409 | and then Present (Full_View (Type_2)) | |
5410 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
5411 | then | |
5412 | return Ctype <= Mode_Conformant | |
5413 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
996ae0b0 RK |
5414 | end if; |
5415 | ||
0a36105d | 5416 | -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be |
758c442c | 5417 | -- treated recursively because they carry a signature. |
af4b9434 AC |
5418 | |
5419 | Are_Anonymous_Access_To_Subprogram_Types := | |
f937473f RD |
5420 | Ekind (Type_1) = Ekind (Type_2) |
5421 | and then | |
800621e0 | 5422 | (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type |
f937473f RD |
5423 | or else |
5424 | Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type); | |
af4b9434 | 5425 | |
996ae0b0 | 5426 | -- Test anonymous access type case. For this case, static subtype |
5d37ba92 ES |
5427 | -- matching is required for mode conformance (RM 6.3.1(15)). We check |
5428 | -- the base types because we may have built internal subtype entities | |
5429 | -- to handle null-excluding types (see Process_Formals). | |
996ae0b0 | 5430 | |
5d37ba92 ES |
5431 | if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type |
5432 | and then | |
5433 | Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type) | |
0ab80019 | 5434 | or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254) |
996ae0b0 RK |
5435 | then |
5436 | declare | |
5437 | Desig_1 : Entity_Id; | |
5438 | Desig_2 : Entity_Id; | |
5439 | ||
5440 | begin | |
5d37ba92 ES |
5441 | -- In Ada2005, access constant indicators must match for |
5442 | -- subtype conformance. | |
9dcb52e1 | 5443 | |
0791fbe9 | 5444 | if Ada_Version >= Ada_2005 |
5d37ba92 ES |
5445 | and then Ctype >= Subtype_Conformant |
5446 | and then | |
5447 | Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2) | |
5448 | then | |
5449 | return False; | |
996ae0b0 RK |
5450 | end if; |
5451 | ||
5d37ba92 | 5452 | Desig_1 := Find_Designated_Type (Type_1); |
996ae0b0 | 5453 | |
5d37ba92 | 5454 | Desig_2 := Find_Designated_Type (Type_2); |
996ae0b0 | 5455 | |
5d37ba92 | 5456 | -- If the context is an instance association for a formal |
82c80734 RD |
5457 | -- access-to-subprogram type; formal access parameter designated |
5458 | -- types require mapping because they may denote other formal | |
5459 | -- parameters of the generic unit. | |
996ae0b0 RK |
5460 | |
5461 | if Get_Inst then | |
5462 | Desig_1 := Get_Instance_Of (Desig_1); | |
5463 | Desig_2 := Get_Instance_Of (Desig_2); | |
5464 | end if; | |
5465 | ||
82c80734 RD |
5466 | -- It is possible for a Class_Wide_Type to be introduced for an |
5467 | -- incomplete type, in which case there is a separate class_ wide | |
5468 | -- type for the full view. The types conform if their Etypes | |
5469 | -- conform, i.e. one may be the full view of the other. This can | |
5470 | -- only happen in the context of an access parameter, other uses | |
5471 | -- of an incomplete Class_Wide_Type are illegal. | |
996ae0b0 | 5472 | |
fbf5a39b AC |
5473 | if Is_Class_Wide_Type (Desig_1) |
5474 | and then Is_Class_Wide_Type (Desig_2) | |
996ae0b0 RK |
5475 | then |
5476 | return | |
fbf5a39b AC |
5477 | Conforming_Types |
5478 | (Etype (Base_Type (Desig_1)), | |
5479 | Etype (Base_Type (Desig_2)), Ctype); | |
af4b9434 AC |
5480 | |
5481 | elsif Are_Anonymous_Access_To_Subprogram_Types then | |
0791fbe9 | 5482 | if Ada_Version < Ada_2005 then |
758c442c GD |
5483 | return Ctype = Type_Conformant |
5484 | or else | |
af4b9434 AC |
5485 | Subtypes_Statically_Match (Desig_1, Desig_2); |
5486 | ||
758c442c GD |
5487 | -- We must check the conformance of the signatures themselves |
5488 | ||
5489 | else | |
5490 | declare | |
5491 | Conformant : Boolean; | |
5492 | begin | |
5493 | Check_Conformance | |
5494 | (Desig_1, Desig_2, Ctype, False, Conformant); | |
5495 | return Conformant; | |
5496 | end; | |
5497 | end if; | |
5498 | ||
996ae0b0 RK |
5499 | else |
5500 | return Base_Type (Desig_1) = Base_Type (Desig_2) | |
5501 | and then (Ctype = Type_Conformant | |
af4b9434 AC |
5502 | or else |
5503 | Subtypes_Statically_Match (Desig_1, Desig_2)); | |
996ae0b0 RK |
5504 | end if; |
5505 | end; | |
5506 | ||
5507 | -- Otherwise definitely no match | |
5508 | ||
5509 | else | |
c8ef728f ES |
5510 | if ((Ekind (Type_1) = E_Anonymous_Access_Type |
5511 | and then Is_Access_Type (Type_2)) | |
5512 | or else (Ekind (Type_2) = E_Anonymous_Access_Type | |
5513 | and then Is_Access_Type (Type_1))) | |
5514 | and then | |
5515 | Conforming_Types | |
5516 | (Designated_Type (Type_1), Designated_Type (Type_2), Ctype) | |
5517 | then | |
5518 | May_Hide_Profile := True; | |
5519 | end if; | |
5520 | ||
996ae0b0 RK |
5521 | return False; |
5522 | end if; | |
996ae0b0 RK |
5523 | end Conforming_Types; |
5524 | ||
5525 | -------------------------- | |
5526 | -- Create_Extra_Formals -- | |
5527 | -------------------------- | |
5528 | ||
5529 | procedure Create_Extra_Formals (E : Entity_Id) is | |
5530 | Formal : Entity_Id; | |
ec4867fa | 5531 | First_Extra : Entity_Id := Empty; |
996ae0b0 RK |
5532 | Last_Extra : Entity_Id; |
5533 | Formal_Type : Entity_Id; | |
5534 | P_Formal : Entity_Id := Empty; | |
5535 | ||
ec4867fa ES |
5536 | function Add_Extra_Formal |
5537 | (Assoc_Entity : Entity_Id; | |
5538 | Typ : Entity_Id; | |
5539 | Scope : Entity_Id; | |
5540 | Suffix : String) return Entity_Id; | |
5541 | -- Add an extra formal to the current list of formals and extra formals. | |
5542 | -- The extra formal is added to the end of the list of extra formals, | |
5543 | -- and also returned as the result. These formals are always of mode IN. | |
5544 | -- The new formal has the type Typ, is declared in Scope, and its name | |
5545 | -- is given by a concatenation of the name of Assoc_Entity and Suffix. | |
cd5a9750 AC |
5546 | -- The following suffixes are currently used. They should not be changed |
5547 | -- without coordinating with CodePeer, which makes use of these to | |
5548 | -- provide better messages. | |
5549 | ||
d92eccc3 AC |
5550 | -- O denotes the Constrained bit. |
5551 | -- L denotes the accessibility level. | |
cd5a9750 AC |
5552 | -- BIP_xxx denotes an extra formal for a build-in-place function. See |
5553 | -- the full list in exp_ch6.BIP_Formal_Kind. | |
996ae0b0 | 5554 | |
fbf5a39b AC |
5555 | ---------------------- |
5556 | -- Add_Extra_Formal -- | |
5557 | ---------------------- | |
5558 | ||
ec4867fa ES |
5559 | function Add_Extra_Formal |
5560 | (Assoc_Entity : Entity_Id; | |
5561 | Typ : Entity_Id; | |
5562 | Scope : Entity_Id; | |
5563 | Suffix : String) return Entity_Id | |
5564 | is | |
996ae0b0 | 5565 | EF : constant Entity_Id := |
ec4867fa ES |
5566 | Make_Defining_Identifier (Sloc (Assoc_Entity), |
5567 | Chars => New_External_Name (Chars (Assoc_Entity), | |
f937473f | 5568 | Suffix => Suffix)); |
996ae0b0 RK |
5569 | |
5570 | begin | |
82c80734 RD |
5571 | -- A little optimization. Never generate an extra formal for the |
5572 | -- _init operand of an initialization procedure, since it could | |
5573 | -- never be used. | |
996ae0b0 RK |
5574 | |
5575 | if Chars (Formal) = Name_uInit then | |
5576 | return Empty; | |
5577 | end if; | |
5578 | ||
5579 | Set_Ekind (EF, E_In_Parameter); | |
5580 | Set_Actual_Subtype (EF, Typ); | |
5581 | Set_Etype (EF, Typ); | |
ec4867fa | 5582 | Set_Scope (EF, Scope); |
996ae0b0 RK |
5583 | Set_Mechanism (EF, Default_Mechanism); |
5584 | Set_Formal_Validity (EF); | |
5585 | ||
ec4867fa ES |
5586 | if No (First_Extra) then |
5587 | First_Extra := EF; | |
5588 | Set_Extra_Formals (Scope, First_Extra); | |
5589 | end if; | |
5590 | ||
5591 | if Present (Last_Extra) then | |
5592 | Set_Extra_Formal (Last_Extra, EF); | |
5593 | end if; | |
5594 | ||
996ae0b0 | 5595 | Last_Extra := EF; |
ec4867fa | 5596 | |
996ae0b0 RK |
5597 | return EF; |
5598 | end Add_Extra_Formal; | |
5599 | ||
5600 | -- Start of processing for Create_Extra_Formals | |
5601 | ||
5602 | begin | |
f937473f RD |
5603 | -- We never generate extra formals if expansion is not active |
5604 | -- because we don't need them unless we are generating code. | |
5605 | ||
5606 | if not Expander_Active then | |
5607 | return; | |
5608 | end if; | |
5609 | ||
82c80734 | 5610 | -- If this is a derived subprogram then the subtypes of the parent |
16b05213 | 5611 | -- subprogram's formal parameters will be used to determine the need |
82c80734 | 5612 | -- for extra formals. |
996ae0b0 RK |
5613 | |
5614 | if Is_Overloadable (E) and then Present (Alias (E)) then | |
5615 | P_Formal := First_Formal (Alias (E)); | |
5616 | end if; | |
5617 | ||
5618 | Last_Extra := Empty; | |
5619 | Formal := First_Formal (E); | |
5620 | while Present (Formal) loop | |
5621 | Last_Extra := Formal; | |
5622 | Next_Formal (Formal); | |
5623 | end loop; | |
5624 | ||
f937473f | 5625 | -- If Extra_formals were already created, don't do it again. This |
82c80734 RD |
5626 | -- situation may arise for subprogram types created as part of |
5627 | -- dispatching calls (see Expand_Dispatching_Call) | |
996ae0b0 RK |
5628 | |
5629 | if Present (Last_Extra) and then | |
5630 | Present (Extra_Formal (Last_Extra)) | |
5631 | then | |
5632 | return; | |
5633 | end if; | |
5634 | ||
19590d70 GD |
5635 | -- If the subprogram is a predefined dispatching subprogram then don't |
5636 | -- generate any extra constrained or accessibility level formals. In | |
5637 | -- general we suppress these for internal subprograms (by not calling | |
5638 | -- Freeze_Subprogram and Create_Extra_Formals at all), but internally | |
5639 | -- generated stream attributes do get passed through because extra | |
5640 | -- build-in-place formals are needed in some cases (limited 'Input). | |
5641 | ||
bac7206d | 5642 | if Is_Predefined_Internal_Operation (E) then |
19590d70 GD |
5643 | goto Test_For_BIP_Extras; |
5644 | end if; | |
5645 | ||
996ae0b0 | 5646 | Formal := First_Formal (E); |
996ae0b0 RK |
5647 | while Present (Formal) loop |
5648 | ||
5649 | -- Create extra formal for supporting the attribute 'Constrained. | |
5650 | -- The case of a private type view without discriminants also | |
5651 | -- requires the extra formal if the underlying type has defaulted | |
5652 | -- discriminants. | |
5653 | ||
5654 | if Ekind (Formal) /= E_In_Parameter then | |
5655 | if Present (P_Formal) then | |
5656 | Formal_Type := Etype (P_Formal); | |
5657 | else | |
5658 | Formal_Type := Etype (Formal); | |
5659 | end if; | |
5660 | ||
5d09245e AC |
5661 | -- Do not produce extra formals for Unchecked_Union parameters. |
5662 | -- Jump directly to the end of the loop. | |
5663 | ||
5664 | if Is_Unchecked_Union (Base_Type (Formal_Type)) then | |
5665 | goto Skip_Extra_Formal_Generation; | |
5666 | end if; | |
5667 | ||
996ae0b0 RK |
5668 | if not Has_Discriminants (Formal_Type) |
5669 | and then Ekind (Formal_Type) in Private_Kind | |
5670 | and then Present (Underlying_Type (Formal_Type)) | |
5671 | then | |
5672 | Formal_Type := Underlying_Type (Formal_Type); | |
5673 | end if; | |
5674 | ||
5675 | if Has_Discriminants (Formal_Type) | |
f937473f RD |
5676 | and then not Is_Constrained (Formal_Type) |
5677 | and then not Is_Indefinite_Subtype (Formal_Type) | |
996ae0b0 RK |
5678 | then |
5679 | Set_Extra_Constrained | |
d92eccc3 | 5680 | (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O")); |
996ae0b0 RK |
5681 | end if; |
5682 | end if; | |
5683 | ||
0a36105d JM |
5684 | -- Create extra formal for supporting accessibility checking. This |
5685 | -- is done for both anonymous access formals and formals of named | |
5686 | -- access types that are marked as controlling formals. The latter | |
5687 | -- case can occur when Expand_Dispatching_Call creates a subprogram | |
5688 | -- type and substitutes the types of access-to-class-wide actuals | |
5689 | -- for the anonymous access-to-specific-type of controlling formals. | |
5d37ba92 ES |
5690 | -- Base_Type is applied because in cases where there is a null |
5691 | -- exclusion the formal may have an access subtype. | |
996ae0b0 RK |
5692 | |
5693 | -- This is suppressed if we specifically suppress accessibility | |
f937473f | 5694 | -- checks at the package level for either the subprogram, or the |
fbf5a39b AC |
5695 | -- package in which it resides. However, we do not suppress it |
5696 | -- simply if the scope has accessibility checks suppressed, since | |
5697 | -- this could cause trouble when clients are compiled with a | |
5698 | -- different suppression setting. The explicit checks at the | |
5699 | -- package level are safe from this point of view. | |
996ae0b0 | 5700 | |
5d37ba92 | 5701 | if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type |
0a36105d | 5702 | or else (Is_Controlling_Formal (Formal) |
5d37ba92 | 5703 | and then Is_Access_Type (Base_Type (Etype (Formal))))) |
996ae0b0 | 5704 | and then not |
fbf5a39b | 5705 | (Explicit_Suppress (E, Accessibility_Check) |
996ae0b0 | 5706 | or else |
fbf5a39b | 5707 | Explicit_Suppress (Scope (E), Accessibility_Check)) |
996ae0b0 | 5708 | and then |
c8ef728f | 5709 | (No (P_Formal) |
996ae0b0 RK |
5710 | or else Present (Extra_Accessibility (P_Formal))) |
5711 | then | |
811c6a85 | 5712 | Set_Extra_Accessibility |
d92eccc3 | 5713 | (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L")); |
996ae0b0 RK |
5714 | end if; |
5715 | ||
5d09245e AC |
5716 | -- This label is required when skipping extra formal generation for |
5717 | -- Unchecked_Union parameters. | |
5718 | ||
5719 | <<Skip_Extra_Formal_Generation>> | |
5720 | ||
f937473f RD |
5721 | if Present (P_Formal) then |
5722 | Next_Formal (P_Formal); | |
5723 | end if; | |
5724 | ||
996ae0b0 RK |
5725 | Next_Formal (Formal); |
5726 | end loop; | |
ec4867fa | 5727 | |
19590d70 GD |
5728 | <<Test_For_BIP_Extras>> |
5729 | ||
ec4867fa | 5730 | -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add |
f937473f RD |
5731 | -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind. |
5732 | ||
0791fbe9 | 5733 | if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then |
ec4867fa | 5734 | declare |
f937473f RD |
5735 | Result_Subt : constant Entity_Id := Etype (E); |
5736 | ||
5737 | Discard : Entity_Id; | |
5738 | pragma Warnings (Off, Discard); | |
ec4867fa ES |
5739 | |
5740 | begin | |
f937473f | 5741 | -- In the case of functions with unconstrained result subtypes, |
9a1bc6d5 AC |
5742 | -- add a 4-state formal indicating whether the return object is |
5743 | -- allocated by the caller (1), or should be allocated by the | |
5744 | -- callee on the secondary stack (2), in the global heap (3), or | |
5745 | -- in a user-defined storage pool (4). For the moment we just use | |
5746 | -- Natural for the type of this formal. Note that this formal | |
5747 | -- isn't usually needed in the case where the result subtype is | |
5748 | -- constrained, but it is needed when the function has a tagged | |
5749 | -- result, because generally such functions can be called in a | |
5750 | -- dispatching context and such calls must be handled like calls | |
5751 | -- to a class-wide function. | |
0a36105d | 5752 | |
a38ff9b1 | 5753 | if not Is_Constrained (Underlying_Type (Result_Subt)) |
0a36105d JM |
5754 | or else Is_Tagged_Type (Underlying_Type (Result_Subt)) |
5755 | then | |
f937473f RD |
5756 | Discard := |
5757 | Add_Extra_Formal | |
5758 | (E, Standard_Natural, | |
5759 | E, BIP_Formal_Suffix (BIP_Alloc_Form)); | |
5760 | end if; | |
ec4867fa | 5761 | |
e5536c09 AC |
5762 | -- For functions whose result type has controlled parts, we have |
5763 | -- an extra formal of type System.Finalization_Implementation. | |
5764 | -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a | |
5765 | -- finalization list (which is itself a pointer). This extra | |
5766 | -- formal is then passed along to Move_Final_List in case of | |
5767 | -- successful completion of a return statement. We cannot pass an | |
5768 | -- 'in out' parameter, because we need to update the finalization | |
5769 | -- list during an abort-deferred region, rather than using | |
5770 | -- copy-back after the function returns. This is true even if we | |
5771 | -- are able to get away with having 'in out' parameters, which are | |
5772 | -- normally illegal for functions. This formal is also needed when | |
5773 | -- the function has a tagged result. | |
f937473f | 5774 | |
048e5cef | 5775 | if Needs_BIP_Final_List (E) then |
f937473f RD |
5776 | Discard := |
5777 | Add_Extra_Formal | |
5778 | (E, RTE (RE_Finalizable_Ptr_Ptr), | |
5779 | E, BIP_Formal_Suffix (BIP_Final_List)); | |
5780 | end if; | |
5781 | ||
5782 | -- If the result type contains tasks, we have two extra formals: | |
5783 | -- the master of the tasks to be created, and the caller's | |
5784 | -- activation chain. | |
5785 | ||
5786 | if Has_Task (Result_Subt) then | |
5787 | Discard := | |
5788 | Add_Extra_Formal | |
5789 | (E, RTE (RE_Master_Id), | |
5790 | E, BIP_Formal_Suffix (BIP_Master)); | |
5791 | Discard := | |
5792 | Add_Extra_Formal | |
5793 | (E, RTE (RE_Activation_Chain_Access), | |
5794 | E, BIP_Formal_Suffix (BIP_Activation_Chain)); | |
5795 | end if; | |
ec4867fa | 5796 | |
f937473f RD |
5797 | -- All build-in-place functions get an extra formal that will be |
5798 | -- passed the address of the return object within the caller. | |
ec4867fa | 5799 | |
f937473f RD |
5800 | declare |
5801 | Formal_Type : constant Entity_Id := | |
5802 | Create_Itype | |
5803 | (E_Anonymous_Access_Type, E, | |
5804 | Scope_Id => Scope (E)); | |
5805 | begin | |
5806 | Set_Directly_Designated_Type (Formal_Type, Result_Subt); | |
5807 | Set_Etype (Formal_Type, Formal_Type); | |
f937473f RD |
5808 | Set_Depends_On_Private |
5809 | (Formal_Type, Has_Private_Component (Formal_Type)); | |
5810 | Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type))); | |
5811 | Set_Is_Access_Constant (Formal_Type, False); | |
ec4867fa | 5812 | |
f937473f RD |
5813 | -- Ada 2005 (AI-50217): Propagate the attribute that indicates |
5814 | -- the designated type comes from the limited view (for | |
5815 | -- back-end purposes). | |
ec4867fa | 5816 | |
f937473f | 5817 | Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt)); |
ec4867fa | 5818 | |
f937473f RD |
5819 | Layout_Type (Formal_Type); |
5820 | ||
5821 | Discard := | |
5822 | Add_Extra_Formal | |
5823 | (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access)); | |
5824 | end; | |
ec4867fa ES |
5825 | end; |
5826 | end if; | |
996ae0b0 RK |
5827 | end Create_Extra_Formals; |
5828 | ||
5829 | ----------------------------- | |
5830 | -- Enter_Overloaded_Entity -- | |
5831 | ----------------------------- | |
5832 | ||
5833 | procedure Enter_Overloaded_Entity (S : Entity_Id) is | |
5834 | E : Entity_Id := Current_Entity_In_Scope (S); | |
5835 | C_E : Entity_Id := Current_Entity (S); | |
5836 | ||
5837 | begin | |
5838 | if Present (E) then | |
5839 | Set_Has_Homonym (E); | |
5840 | Set_Has_Homonym (S); | |
5841 | end if; | |
5842 | ||
5843 | Set_Is_Immediately_Visible (S); | |
5844 | Set_Scope (S, Current_Scope); | |
5845 | ||
5846 | -- Chain new entity if front of homonym in current scope, so that | |
5847 | -- homonyms are contiguous. | |
5848 | ||
5849 | if Present (E) | |
5850 | and then E /= C_E | |
5851 | then | |
5852 | while Homonym (C_E) /= E loop | |
5853 | C_E := Homonym (C_E); | |
5854 | end loop; | |
5855 | ||
5856 | Set_Homonym (C_E, S); | |
5857 | ||
5858 | else | |
5859 | E := C_E; | |
5860 | Set_Current_Entity (S); | |
5861 | end if; | |
5862 | ||
5863 | Set_Homonym (S, E); | |
5864 | ||
5865 | Append_Entity (S, Current_Scope); | |
5866 | Set_Public_Status (S); | |
5867 | ||
5868 | if Debug_Flag_E then | |
5869 | Write_Str ("New overloaded entity chain: "); | |
5870 | Write_Name (Chars (S)); | |
996ae0b0 | 5871 | |
82c80734 | 5872 | E := S; |
996ae0b0 RK |
5873 | while Present (E) loop |
5874 | Write_Str (" "); Write_Int (Int (E)); | |
5875 | E := Homonym (E); | |
5876 | end loop; | |
5877 | ||
5878 | Write_Eol; | |
5879 | end if; | |
5880 | ||
5881 | -- Generate warning for hiding | |
5882 | ||
5883 | if Warn_On_Hiding | |
5884 | and then Comes_From_Source (S) | |
5885 | and then In_Extended_Main_Source_Unit (S) | |
5886 | then | |
5887 | E := S; | |
5888 | loop | |
5889 | E := Homonym (E); | |
5890 | exit when No (E); | |
5891 | ||
5892 | -- Warn unless genuine overloading | |
5893 | ||
f937473f RD |
5894 | if (not Is_Overloadable (E) or else Subtype_Conformant (E, S)) |
5895 | and then (Is_Immediately_Visible (E) | |
5896 | or else | |
5897 | Is_Potentially_Use_Visible (S)) | |
996ae0b0 RK |
5898 | then |
5899 | Error_Msg_Sloc := Sloc (E); | |
5900 | Error_Msg_N ("declaration of & hides one#?", S); | |
5901 | end if; | |
5902 | end loop; | |
5903 | end if; | |
5904 | end Enter_Overloaded_Entity; | |
5905 | ||
e5a58fac AC |
5906 | ----------------------------- |
5907 | -- Check_Untagged_Equality -- | |
5908 | ----------------------------- | |
5909 | ||
5910 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is | |
5911 | Typ : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
5912 | Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op); | |
5913 | Obj_Decl : Node_Id; | |
5914 | ||
5915 | begin | |
5916 | if Nkind (Decl) = N_Subprogram_Declaration | |
5917 | and then Is_Record_Type (Typ) | |
5918 | and then not Is_Tagged_Type (Typ) | |
5919 | then | |
21a5b575 AC |
5920 | -- If the type is not declared in a package, or if we are in the |
5921 | -- body of the package or in some other scope, the new operation is | |
5922 | -- not primitive, and therefore legal, though suspicious. If the | |
5923 | -- type is a generic actual (sub)type, the operation is not primitive | |
5924 | -- either because the base type is declared elsewhere. | |
5925 | ||
e5a58fac | 5926 | if Is_Frozen (Typ) then |
21a5b575 AC |
5927 | if Ekind (Scope (Typ)) /= E_Package |
5928 | or else Scope (Typ) /= Current_Scope | |
5929 | then | |
5930 | null; | |
e5a58fac | 5931 | |
21a5b575 AC |
5932 | elsif Is_Generic_Actual_Type (Typ) then |
5933 | null; | |
e5a58fac | 5934 | |
21a5b575 | 5935 | elsif In_Package_Body (Scope (Typ)) then |
ae6ede77 AC |
5936 | Error_Msg_NE |
5937 | ("equality operator must be declared " | |
5938 | & "before type& is frozen", Eq_Op, Typ); | |
5939 | Error_Msg_N | |
5940 | ("\move declaration to package spec", Eq_Op); | |
21a5b575 AC |
5941 | |
5942 | else | |
5943 | Error_Msg_NE | |
5944 | ("equality operator must be declared " | |
5945 | & "before type& is frozen", Eq_Op, Typ); | |
5946 | ||
5947 | Obj_Decl := Next (Parent (Typ)); | |
5948 | while Present (Obj_Decl) | |
5949 | and then Obj_Decl /= Decl | |
5950 | loop | |
5951 | if Nkind (Obj_Decl) = N_Object_Declaration | |
5952 | and then Etype (Defining_Identifier (Obj_Decl)) = Typ | |
5953 | then | |
5954 | Error_Msg_NE ("type& is frozen by declaration?", | |
5955 | Obj_Decl, Typ); | |
5956 | Error_Msg_N | |
5957 | ("\an equality operator cannot be declared after this " | |
5958 | & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl); | |
5959 | exit; | |
5960 | end if; | |
5961 | ||
5962 | Next (Obj_Decl); | |
5963 | end loop; | |
5964 | end if; | |
e5a58fac AC |
5965 | |
5966 | elsif not In_Same_List (Parent (Typ), Decl) | |
5967 | and then not Is_Limited_Type (Typ) | |
5968 | then | |
21a5b575 AC |
5969 | |
5970 | -- This makes it illegal to have a primitive equality declared in | |
5971 | -- the private part if the type is visible. | |
5972 | ||
e5a58fac AC |
5973 | Error_Msg_N ("equality operator appears too late", Eq_Op); |
5974 | end if; | |
5975 | end if; | |
5976 | end Check_Untagged_Equality; | |
5977 | ||
996ae0b0 RK |
5978 | ----------------------------- |
5979 | -- Find_Corresponding_Spec -- | |
5980 | ----------------------------- | |
5981 | ||
d44202ba HK |
5982 | function Find_Corresponding_Spec |
5983 | (N : Node_Id; | |
5984 | Post_Error : Boolean := True) return Entity_Id | |
5985 | is | |
996ae0b0 RK |
5986 | Spec : constant Node_Id := Specification (N); |
5987 | Designator : constant Entity_Id := Defining_Entity (Spec); | |
5988 | ||
5989 | E : Entity_Id; | |
5990 | ||
5991 | begin | |
5992 | E := Current_Entity (Designator); | |
996ae0b0 RK |
5993 | while Present (E) loop |
5994 | ||
5995 | -- We are looking for a matching spec. It must have the same scope, | |
5996 | -- and the same name, and either be type conformant, or be the case | |
5997 | -- of a library procedure spec and its body (which belong to one | |
5998 | -- another regardless of whether they are type conformant or not). | |
5999 | ||
6000 | if Scope (E) = Current_Scope then | |
fbf5a39b AC |
6001 | if Current_Scope = Standard_Standard |
6002 | or else (Ekind (E) = Ekind (Designator) | |
6003 | and then Type_Conformant (E, Designator)) | |
996ae0b0 RK |
6004 | then |
6005 | -- Within an instantiation, we know that spec and body are | |
6006 | -- subtype conformant, because they were subtype conformant | |
6007 | -- in the generic. We choose the subtype-conformant entity | |
6008 | -- here as well, to resolve spurious ambiguities in the | |
6009 | -- instance that were not present in the generic (i.e. when | |
6010 | -- two different types are given the same actual). If we are | |
6011 | -- looking for a spec to match a body, full conformance is | |
6012 | -- expected. | |
6013 | ||
6014 | if In_Instance then | |
6015 | Set_Convention (Designator, Convention (E)); | |
6016 | ||
6017 | if Nkind (N) = N_Subprogram_Body | |
6018 | and then Present (Homonym (E)) | |
6019 | and then not Fully_Conformant (E, Designator) | |
6020 | then | |
6021 | goto Next_Entity; | |
6022 | ||
6023 | elsif not Subtype_Conformant (E, Designator) then | |
6024 | goto Next_Entity; | |
6025 | end if; | |
6026 | end if; | |
6027 | ||
6028 | if not Has_Completion (E) then | |
996ae0b0 RK |
6029 | if Nkind (N) /= N_Subprogram_Body_Stub then |
6030 | Set_Corresponding_Spec (N, E); | |
6031 | end if; | |
6032 | ||
6033 | Set_Has_Completion (E); | |
6034 | return E; | |
6035 | ||
6036 | elsif Nkind (Parent (N)) = N_Subunit then | |
6037 | ||
6038 | -- If this is the proper body of a subunit, the completion | |
6039 | -- flag is set when analyzing the stub. | |
6040 | ||
6041 | return E; | |
6042 | ||
81db9d77 ES |
6043 | -- If E is an internal function with a controlling result |
6044 | -- that was created for an operation inherited by a null | |
6045 | -- extension, it may be overridden by a body without a previous | |
6046 | -- spec (one more reason why these should be shunned). In that | |
1366997b AC |
6047 | -- case remove the generated body if present, because the |
6048 | -- current one is the explicit overriding. | |
81db9d77 ES |
6049 | |
6050 | elsif Ekind (E) = E_Function | |
0791fbe9 | 6051 | and then Ada_Version >= Ada_2005 |
81db9d77 ES |
6052 | and then not Comes_From_Source (E) |
6053 | and then Has_Controlling_Result (E) | |
6054 | and then Is_Null_Extension (Etype (E)) | |
6055 | and then Comes_From_Source (Spec) | |
6056 | then | |
6057 | Set_Has_Completion (E, False); | |
6058 | ||
1366997b AC |
6059 | if Expander_Active |
6060 | and then Nkind (Parent (E)) = N_Function_Specification | |
6061 | then | |
81db9d77 ES |
6062 | Remove |
6063 | (Unit_Declaration_Node | |
1366997b AC |
6064 | (Corresponding_Body (Unit_Declaration_Node (E)))); |
6065 | ||
81db9d77 ES |
6066 | return E; |
6067 | ||
1366997b AC |
6068 | -- If expansion is disabled, or if the wrapper function has |
6069 | -- not been generated yet, this a late body overriding an | |
6070 | -- inherited operation, or it is an overriding by some other | |
6071 | -- declaration before the controlling result is frozen. In | |
6072 | -- either case this is a declaration of a new entity. | |
81db9d77 ES |
6073 | |
6074 | else | |
6075 | return Empty; | |
6076 | end if; | |
6077 | ||
d44202ba HK |
6078 | -- If the body already exists, then this is an error unless |
6079 | -- the previous declaration is the implicit declaration of a | |
6080 | -- derived subprogram, or this is a spurious overloading in an | |
6081 | -- instance. | |
996ae0b0 RK |
6082 | |
6083 | elsif No (Alias (E)) | |
6084 | and then not Is_Intrinsic_Subprogram (E) | |
6085 | and then not In_Instance | |
d44202ba | 6086 | and then Post_Error |
996ae0b0 RK |
6087 | then |
6088 | Error_Msg_Sloc := Sloc (E); | |
8dbd1460 | 6089 | |
07fc65c4 GB |
6090 | if Is_Imported (E) then |
6091 | Error_Msg_NE | |
6092 | ("body not allowed for imported subprogram & declared#", | |
6093 | N, E); | |
6094 | else | |
6095 | Error_Msg_NE ("duplicate body for & declared#", N, E); | |
6096 | end if; | |
996ae0b0 RK |
6097 | end if; |
6098 | ||
d44202ba HK |
6099 | -- Child units cannot be overloaded, so a conformance mismatch |
6100 | -- between body and a previous spec is an error. | |
6101 | ||
996ae0b0 RK |
6102 | elsif Is_Child_Unit (E) |
6103 | and then | |
6104 | Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body | |
6105 | and then | |
5d37ba92 | 6106 | Nkind (Parent (Unit_Declaration_Node (Designator))) = |
d44202ba HK |
6107 | N_Compilation_Unit |
6108 | and then Post_Error | |
996ae0b0 | 6109 | then |
996ae0b0 RK |
6110 | Error_Msg_N |
6111 | ("body of child unit does not match previous declaration", N); | |
6112 | end if; | |
6113 | end if; | |
6114 | ||
6115 | <<Next_Entity>> | |
6116 | E := Homonym (E); | |
6117 | end loop; | |
6118 | ||
6119 | -- On exit, we know that no previous declaration of subprogram exists | |
6120 | ||
6121 | return Empty; | |
6122 | end Find_Corresponding_Spec; | |
6123 | ||
6124 | ---------------------- | |
6125 | -- Fully_Conformant -- | |
6126 | ---------------------- | |
6127 | ||
6128 | function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
6129 | Result : Boolean; | |
996ae0b0 RK |
6130 | begin |
6131 | Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result); | |
6132 | return Result; | |
6133 | end Fully_Conformant; | |
6134 | ||
6135 | ---------------------------------- | |
6136 | -- Fully_Conformant_Expressions -- | |
6137 | ---------------------------------- | |
6138 | ||
6139 | function Fully_Conformant_Expressions | |
6140 | (Given_E1 : Node_Id; | |
d05ef0ab | 6141 | Given_E2 : Node_Id) return Boolean |
996ae0b0 RK |
6142 | is |
6143 | E1 : constant Node_Id := Original_Node (Given_E1); | |
6144 | E2 : constant Node_Id := Original_Node (Given_E2); | |
6145 | -- We always test conformance on original nodes, since it is possible | |
6146 | -- for analysis and/or expansion to make things look as though they | |
6147 | -- conform when they do not, e.g. by converting 1+2 into 3. | |
6148 | ||
6149 | function FCE (Given_E1, Given_E2 : Node_Id) return Boolean | |
6150 | renames Fully_Conformant_Expressions; | |
6151 | ||
6152 | function FCL (L1, L2 : List_Id) return Boolean; | |
6153 | -- Compare elements of two lists for conformance. Elements have to | |
6154 | -- be conformant, and actuals inserted as default parameters do not | |
6155 | -- match explicit actuals with the same value. | |
6156 | ||
6157 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean; | |
e895b435 | 6158 | -- Compare an operator node with a function call |
996ae0b0 RK |
6159 | |
6160 | --------- | |
6161 | -- FCL -- | |
6162 | --------- | |
6163 | ||
6164 | function FCL (L1, L2 : List_Id) return Boolean is | |
6165 | N1, N2 : Node_Id; | |
6166 | ||
6167 | begin | |
6168 | if L1 = No_List then | |
6169 | N1 := Empty; | |
6170 | else | |
6171 | N1 := First (L1); | |
6172 | end if; | |
6173 | ||
6174 | if L2 = No_List then | |
6175 | N2 := Empty; | |
6176 | else | |
6177 | N2 := First (L2); | |
6178 | end if; | |
6179 | ||
6180 | -- Compare two lists, skipping rewrite insertions (we want to | |
6181 | -- compare the original trees, not the expanded versions!) | |
6182 | ||
6183 | loop | |
6184 | if Is_Rewrite_Insertion (N1) then | |
6185 | Next (N1); | |
6186 | elsif Is_Rewrite_Insertion (N2) then | |
6187 | Next (N2); | |
6188 | elsif No (N1) then | |
6189 | return No (N2); | |
6190 | elsif No (N2) then | |
6191 | return False; | |
6192 | elsif not FCE (N1, N2) then | |
6193 | return False; | |
6194 | else | |
6195 | Next (N1); | |
6196 | Next (N2); | |
6197 | end if; | |
6198 | end loop; | |
6199 | end FCL; | |
6200 | ||
6201 | --------- | |
6202 | -- FCO -- | |
6203 | --------- | |
6204 | ||
6205 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean is | |
6206 | Actuals : constant List_Id := Parameter_Associations (Call_Node); | |
6207 | Act : Node_Id; | |
6208 | ||
6209 | begin | |
6210 | if No (Actuals) | |
6211 | or else Entity (Op_Node) /= Entity (Name (Call_Node)) | |
6212 | then | |
6213 | return False; | |
6214 | ||
6215 | else | |
6216 | Act := First (Actuals); | |
6217 | ||
6218 | if Nkind (Op_Node) in N_Binary_Op then | |
996ae0b0 RK |
6219 | if not FCE (Left_Opnd (Op_Node), Act) then |
6220 | return False; | |
6221 | end if; | |
6222 | ||
6223 | Next (Act); | |
6224 | end if; | |
6225 | ||
6226 | return Present (Act) | |
6227 | and then FCE (Right_Opnd (Op_Node), Act) | |
6228 | and then No (Next (Act)); | |
6229 | end if; | |
6230 | end FCO; | |
6231 | ||
6232 | -- Start of processing for Fully_Conformant_Expressions | |
6233 | ||
6234 | begin | |
6235 | -- Non-conformant if paren count does not match. Note: if some idiot | |
6236 | -- complains that we don't do this right for more than 3 levels of | |
0a36105d | 6237 | -- parentheses, they will be treated with the respect they deserve! |
996ae0b0 RK |
6238 | |
6239 | if Paren_Count (E1) /= Paren_Count (E2) then | |
6240 | return False; | |
6241 | ||
82c80734 RD |
6242 | -- If same entities are referenced, then they are conformant even if |
6243 | -- they have different forms (RM 8.3.1(19-20)). | |
996ae0b0 RK |
6244 | |
6245 | elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then | |
6246 | if Present (Entity (E1)) then | |
6247 | return Entity (E1) = Entity (E2) | |
6248 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) | |
6249 | and then Ekind (Entity (E1)) = E_Discriminant | |
6250 | and then Ekind (Entity (E2)) = E_In_Parameter); | |
6251 | ||
6252 | elsif Nkind (E1) = N_Expanded_Name | |
6253 | and then Nkind (E2) = N_Expanded_Name | |
6254 | and then Nkind (Selector_Name (E1)) = N_Character_Literal | |
6255 | and then Nkind (Selector_Name (E2)) = N_Character_Literal | |
6256 | then | |
6257 | return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)); | |
6258 | ||
6259 | else | |
6260 | -- Identifiers in component associations don't always have | |
6261 | -- entities, but their names must conform. | |
6262 | ||
6263 | return Nkind (E1) = N_Identifier | |
6264 | and then Nkind (E2) = N_Identifier | |
6265 | and then Chars (E1) = Chars (E2); | |
6266 | end if; | |
6267 | ||
6268 | elsif Nkind (E1) = N_Character_Literal | |
6269 | and then Nkind (E2) = N_Expanded_Name | |
6270 | then | |
6271 | return Nkind (Selector_Name (E2)) = N_Character_Literal | |
6272 | and then Chars (E1) = Chars (Selector_Name (E2)); | |
6273 | ||
6274 | elsif Nkind (E2) = N_Character_Literal | |
6275 | and then Nkind (E1) = N_Expanded_Name | |
6276 | then | |
6277 | return Nkind (Selector_Name (E1)) = N_Character_Literal | |
6278 | and then Chars (E2) = Chars (Selector_Name (E1)); | |
6279 | ||
6280 | elsif Nkind (E1) in N_Op | |
6281 | and then Nkind (E2) = N_Function_Call | |
6282 | then | |
6283 | return FCO (E1, E2); | |
6284 | ||
6285 | elsif Nkind (E2) in N_Op | |
6286 | and then Nkind (E1) = N_Function_Call | |
6287 | then | |
6288 | return FCO (E2, E1); | |
6289 | ||
6290 | -- Otherwise we must have the same syntactic entity | |
6291 | ||
6292 | elsif Nkind (E1) /= Nkind (E2) then | |
6293 | return False; | |
6294 | ||
6295 | -- At this point, we specialize by node type | |
6296 | ||
6297 | else | |
6298 | case Nkind (E1) is | |
6299 | ||
6300 | when N_Aggregate => | |
6301 | return | |
6302 | FCL (Expressions (E1), Expressions (E2)) | |
19d846a0 RD |
6303 | and then |
6304 | FCL (Component_Associations (E1), | |
6305 | Component_Associations (E2)); | |
996ae0b0 RK |
6306 | |
6307 | when N_Allocator => | |
6308 | if Nkind (Expression (E1)) = N_Qualified_Expression | |
6309 | or else | |
6310 | Nkind (Expression (E2)) = N_Qualified_Expression | |
6311 | then | |
6312 | return FCE (Expression (E1), Expression (E2)); | |
6313 | ||
6314 | -- Check that the subtype marks and any constraints | |
6315 | -- are conformant | |
6316 | ||
6317 | else | |
6318 | declare | |
6319 | Indic1 : constant Node_Id := Expression (E1); | |
6320 | Indic2 : constant Node_Id := Expression (E2); | |
6321 | Elt1 : Node_Id; | |
6322 | Elt2 : Node_Id; | |
6323 | ||
6324 | begin | |
6325 | if Nkind (Indic1) /= N_Subtype_Indication then | |
6326 | return | |
6327 | Nkind (Indic2) /= N_Subtype_Indication | |
6328 | and then Entity (Indic1) = Entity (Indic2); | |
6329 | ||
6330 | elsif Nkind (Indic2) /= N_Subtype_Indication then | |
6331 | return | |
6332 | Nkind (Indic1) /= N_Subtype_Indication | |
6333 | and then Entity (Indic1) = Entity (Indic2); | |
6334 | ||
6335 | else | |
6336 | if Entity (Subtype_Mark (Indic1)) /= | |
6337 | Entity (Subtype_Mark (Indic2)) | |
6338 | then | |
6339 | return False; | |
6340 | end if; | |
6341 | ||
6342 | Elt1 := First (Constraints (Constraint (Indic1))); | |
6343 | Elt2 := First (Constraints (Constraint (Indic2))); | |
996ae0b0 RK |
6344 | while Present (Elt1) and then Present (Elt2) loop |
6345 | if not FCE (Elt1, Elt2) then | |
6346 | return False; | |
6347 | end if; | |
6348 | ||
6349 | Next (Elt1); | |
6350 | Next (Elt2); | |
6351 | end loop; | |
6352 | ||
6353 | return True; | |
6354 | end if; | |
6355 | end; | |
6356 | end if; | |
6357 | ||
6358 | when N_Attribute_Reference => | |
6359 | return | |
6360 | Attribute_Name (E1) = Attribute_Name (E2) | |
6361 | and then FCL (Expressions (E1), Expressions (E2)); | |
6362 | ||
6363 | when N_Binary_Op => | |
6364 | return | |
6365 | Entity (E1) = Entity (E2) | |
6366 | and then FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
6367 | and then FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
6368 | ||
514d0fc5 | 6369 | when N_Short_Circuit | N_Membership_Test => |
996ae0b0 RK |
6370 | return |
6371 | FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
6372 | and then | |
6373 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
6374 | ||
19d846a0 RD |
6375 | when N_Case_Expression => |
6376 | declare | |
6377 | Alt1 : Node_Id; | |
6378 | Alt2 : Node_Id; | |
6379 | ||
6380 | begin | |
6381 | if not FCE (Expression (E1), Expression (E2)) then | |
6382 | return False; | |
6383 | ||
6384 | else | |
6385 | Alt1 := First (Alternatives (E1)); | |
6386 | Alt2 := First (Alternatives (E2)); | |
6387 | loop | |
6388 | if Present (Alt1) /= Present (Alt2) then | |
6389 | return False; | |
6390 | elsif No (Alt1) then | |
6391 | return True; | |
6392 | end if; | |
6393 | ||
6394 | if not FCE (Expression (Alt1), Expression (Alt2)) | |
6395 | or else not FCL (Discrete_Choices (Alt1), | |
6396 | Discrete_Choices (Alt2)) | |
6397 | then | |
6398 | return False; | |
6399 | end if; | |
6400 | ||
6401 | Next (Alt1); | |
6402 | Next (Alt2); | |
6403 | end loop; | |
6404 | end if; | |
6405 | end; | |
6406 | ||
996ae0b0 RK |
6407 | when N_Character_Literal => |
6408 | return | |
6409 | Char_Literal_Value (E1) = Char_Literal_Value (E2); | |
6410 | ||
6411 | when N_Component_Association => | |
6412 | return | |
6413 | FCL (Choices (E1), Choices (E2)) | |
19d846a0 RD |
6414 | and then |
6415 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
6416 | |
6417 | when N_Conditional_Expression => | |
6418 | return | |
6419 | FCL (Expressions (E1), Expressions (E2)); | |
6420 | ||
6421 | when N_Explicit_Dereference => | |
6422 | return | |
6423 | FCE (Prefix (E1), Prefix (E2)); | |
6424 | ||
6425 | when N_Extension_Aggregate => | |
6426 | return | |
6427 | FCL (Expressions (E1), Expressions (E2)) | |
6428 | and then Null_Record_Present (E1) = | |
6429 | Null_Record_Present (E2) | |
6430 | and then FCL (Component_Associations (E1), | |
6431 | Component_Associations (E2)); | |
6432 | ||
6433 | when N_Function_Call => | |
6434 | return | |
6435 | FCE (Name (E1), Name (E2)) | |
19d846a0 RD |
6436 | and then |
6437 | FCL (Parameter_Associations (E1), | |
6438 | Parameter_Associations (E2)); | |
996ae0b0 RK |
6439 | |
6440 | when N_Indexed_Component => | |
6441 | return | |
6442 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
6443 | and then |
6444 | FCL (Expressions (E1), Expressions (E2)); | |
996ae0b0 RK |
6445 | |
6446 | when N_Integer_Literal => | |
6447 | return (Intval (E1) = Intval (E2)); | |
6448 | ||
6449 | when N_Null => | |
6450 | return True; | |
6451 | ||
6452 | when N_Operator_Symbol => | |
6453 | return | |
6454 | Chars (E1) = Chars (E2); | |
6455 | ||
6456 | when N_Others_Choice => | |
6457 | return True; | |
6458 | ||
6459 | when N_Parameter_Association => | |
6460 | return | |
996ae0b0 RK |
6461 | Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)) |
6462 | and then FCE (Explicit_Actual_Parameter (E1), | |
6463 | Explicit_Actual_Parameter (E2)); | |
6464 | ||
6465 | when N_Qualified_Expression => | |
6466 | return | |
6467 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
6468 | and then |
6469 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
6470 | |
6471 | when N_Range => | |
6472 | return | |
6473 | FCE (Low_Bound (E1), Low_Bound (E2)) | |
19d846a0 RD |
6474 | and then |
6475 | FCE (High_Bound (E1), High_Bound (E2)); | |
996ae0b0 RK |
6476 | |
6477 | when N_Real_Literal => | |
6478 | return (Realval (E1) = Realval (E2)); | |
6479 | ||
6480 | when N_Selected_Component => | |
6481 | return | |
6482 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
6483 | and then |
6484 | FCE (Selector_Name (E1), Selector_Name (E2)); | |
996ae0b0 RK |
6485 | |
6486 | when N_Slice => | |
6487 | return | |
6488 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
6489 | and then |
6490 | FCE (Discrete_Range (E1), Discrete_Range (E2)); | |
996ae0b0 RK |
6491 | |
6492 | when N_String_Literal => | |
6493 | declare | |
6494 | S1 : constant String_Id := Strval (E1); | |
6495 | S2 : constant String_Id := Strval (E2); | |
6496 | L1 : constant Nat := String_Length (S1); | |
6497 | L2 : constant Nat := String_Length (S2); | |
6498 | ||
6499 | begin | |
6500 | if L1 /= L2 then | |
6501 | return False; | |
6502 | ||
6503 | else | |
6504 | for J in 1 .. L1 loop | |
6505 | if Get_String_Char (S1, J) /= | |
6506 | Get_String_Char (S2, J) | |
6507 | then | |
6508 | return False; | |
6509 | end if; | |
6510 | end loop; | |
6511 | ||
6512 | return True; | |
6513 | end if; | |
6514 | end; | |
6515 | ||
6516 | when N_Type_Conversion => | |
6517 | return | |
6518 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
6519 | and then |
6520 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
6521 | |
6522 | when N_Unary_Op => | |
6523 | return | |
6524 | Entity (E1) = Entity (E2) | |
19d846a0 RD |
6525 | and then |
6526 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
996ae0b0 RK |
6527 | |
6528 | when N_Unchecked_Type_Conversion => | |
6529 | return | |
6530 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
6531 | and then |
6532 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
6533 | |
6534 | -- All other node types cannot appear in this context. Strictly | |
6535 | -- we should raise a fatal internal error. Instead we just ignore | |
6536 | -- the nodes. This means that if anyone makes a mistake in the | |
6537 | -- expander and mucks an expression tree irretrievably, the | |
6538 | -- result will be a failure to detect a (probably very obscure) | |
6539 | -- case of non-conformance, which is better than bombing on some | |
6540 | -- case where two expressions do in fact conform. | |
6541 | ||
6542 | when others => | |
6543 | return True; | |
6544 | ||
6545 | end case; | |
6546 | end if; | |
6547 | end Fully_Conformant_Expressions; | |
6548 | ||
fbf5a39b AC |
6549 | ---------------------------------------- |
6550 | -- Fully_Conformant_Discrete_Subtypes -- | |
6551 | ---------------------------------------- | |
6552 | ||
6553 | function Fully_Conformant_Discrete_Subtypes | |
6554 | (Given_S1 : Node_Id; | |
d05ef0ab | 6555 | Given_S2 : Node_Id) return Boolean |
fbf5a39b AC |
6556 | is |
6557 | S1 : constant Node_Id := Original_Node (Given_S1); | |
6558 | S2 : constant Node_Id := Original_Node (Given_S2); | |
6559 | ||
6560 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean; | |
82c80734 RD |
6561 | -- Special-case for a bound given by a discriminant, which in the body |
6562 | -- is replaced with the discriminal of the enclosing type. | |
fbf5a39b AC |
6563 | |
6564 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean; | |
e895b435 | 6565 | -- Check both bounds |
fbf5a39b | 6566 | |
5d37ba92 ES |
6567 | ----------------------- |
6568 | -- Conforming_Bounds -- | |
6569 | ----------------------- | |
6570 | ||
fbf5a39b AC |
6571 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is |
6572 | begin | |
6573 | if Is_Entity_Name (B1) | |
6574 | and then Is_Entity_Name (B2) | |
6575 | and then Ekind (Entity (B1)) = E_Discriminant | |
6576 | then | |
6577 | return Chars (B1) = Chars (B2); | |
6578 | ||
6579 | else | |
6580 | return Fully_Conformant_Expressions (B1, B2); | |
6581 | end if; | |
6582 | end Conforming_Bounds; | |
6583 | ||
5d37ba92 ES |
6584 | ----------------------- |
6585 | -- Conforming_Ranges -- | |
6586 | ----------------------- | |
6587 | ||
fbf5a39b AC |
6588 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is |
6589 | begin | |
6590 | return | |
6591 | Conforming_Bounds (Low_Bound (R1), Low_Bound (R2)) | |
6592 | and then | |
6593 | Conforming_Bounds (High_Bound (R1), High_Bound (R2)); | |
6594 | end Conforming_Ranges; | |
6595 | ||
6596 | -- Start of processing for Fully_Conformant_Discrete_Subtypes | |
6597 | ||
6598 | begin | |
6599 | if Nkind (S1) /= Nkind (S2) then | |
6600 | return False; | |
6601 | ||
6602 | elsif Is_Entity_Name (S1) then | |
6603 | return Entity (S1) = Entity (S2); | |
6604 | ||
6605 | elsif Nkind (S1) = N_Range then | |
6606 | return Conforming_Ranges (S1, S2); | |
6607 | ||
6608 | elsif Nkind (S1) = N_Subtype_Indication then | |
6609 | return | |
6610 | Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2)) | |
6611 | and then | |
6612 | Conforming_Ranges | |
6613 | (Range_Expression (Constraint (S1)), | |
6614 | Range_Expression (Constraint (S2))); | |
6615 | else | |
6616 | return True; | |
6617 | end if; | |
6618 | end Fully_Conformant_Discrete_Subtypes; | |
6619 | ||
996ae0b0 RK |
6620 | -------------------- |
6621 | -- Install_Entity -- | |
6622 | -------------------- | |
6623 | ||
6624 | procedure Install_Entity (E : Entity_Id) is | |
6625 | Prev : constant Entity_Id := Current_Entity (E); | |
996ae0b0 RK |
6626 | begin |
6627 | Set_Is_Immediately_Visible (E); | |
6628 | Set_Current_Entity (E); | |
6629 | Set_Homonym (E, Prev); | |
6630 | end Install_Entity; | |
6631 | ||
6632 | --------------------- | |
6633 | -- Install_Formals -- | |
6634 | --------------------- | |
6635 | ||
6636 | procedure Install_Formals (Id : Entity_Id) is | |
6637 | F : Entity_Id; | |
996ae0b0 RK |
6638 | begin |
6639 | F := First_Formal (Id); | |
996ae0b0 RK |
6640 | while Present (F) loop |
6641 | Install_Entity (F); | |
6642 | Next_Formal (F); | |
6643 | end loop; | |
6644 | end Install_Formals; | |
6645 | ||
ce2b6ba5 JM |
6646 | ----------------------------- |
6647 | -- Is_Interface_Conformant -- | |
6648 | ----------------------------- | |
6649 | ||
6650 | function Is_Interface_Conformant | |
6651 | (Tagged_Type : Entity_Id; | |
6652 | Iface_Prim : Entity_Id; | |
6653 | Prim : Entity_Id) return Boolean | |
6654 | is | |
fceeaab6 ES |
6655 | Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim); |
6656 | Typ : constant Entity_Id := Find_Dispatching_Type (Prim); | |
6657 | ||
ce2b6ba5 JM |
6658 | begin |
6659 | pragma Assert (Is_Subprogram (Iface_Prim) | |
6660 | and then Is_Subprogram (Prim) | |
6661 | and then Is_Dispatching_Operation (Iface_Prim) | |
6662 | and then Is_Dispatching_Operation (Prim)); | |
6663 | ||
fceeaab6 | 6664 | pragma Assert (Is_Interface (Iface) |
ce2b6ba5 JM |
6665 | or else (Present (Alias (Iface_Prim)) |
6666 | and then | |
6667 | Is_Interface | |
6668 | (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim))))); | |
6669 | ||
6670 | if Prim = Iface_Prim | |
6671 | or else not Is_Subprogram (Prim) | |
6672 | or else Ekind (Prim) /= Ekind (Iface_Prim) | |
6673 | or else not Is_Dispatching_Operation (Prim) | |
6674 | or else Scope (Prim) /= Scope (Tagged_Type) | |
fceeaab6 ES |
6675 | or else No (Typ) |
6676 | or else Base_Type (Typ) /= Tagged_Type | |
ce2b6ba5 JM |
6677 | or else not Primitive_Names_Match (Iface_Prim, Prim) |
6678 | then | |
6679 | return False; | |
6680 | ||
fceeaab6 ES |
6681 | -- Case of a procedure, or a function that does not have a controlling |
6682 | -- result (I or access I). | |
ce2b6ba5 JM |
6683 | |
6684 | elsif Ekind (Iface_Prim) = E_Procedure | |
6685 | or else Etype (Prim) = Etype (Iface_Prim) | |
fceeaab6 | 6686 | or else not Has_Controlling_Result (Prim) |
ce2b6ba5 | 6687 | then |
b4d7b435 AC |
6688 | return Type_Conformant |
6689 | (Iface_Prim, Prim, Skip_Controlling_Formals => True); | |
ce2b6ba5 | 6690 | |
fceeaab6 ES |
6691 | -- Case of a function returning an interface, or an access to one. |
6692 | -- Check that the return types correspond. | |
ce2b6ba5 | 6693 | |
fceeaab6 ES |
6694 | elsif Implements_Interface (Typ, Iface) then |
6695 | if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type) | |
9a3c9940 RD |
6696 | /= |
6697 | (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type) | |
fceeaab6 ES |
6698 | then |
6699 | return False; | |
fceeaab6 ES |
6700 | else |
6701 | return | |
ce2b6ba5 JM |
6702 | Type_Conformant (Prim, Iface_Prim, |
6703 | Skip_Controlling_Formals => True); | |
fceeaab6 | 6704 | end if; |
ce2b6ba5 | 6705 | |
fceeaab6 ES |
6706 | else |
6707 | return False; | |
ce2b6ba5 | 6708 | end if; |
ce2b6ba5 JM |
6709 | end Is_Interface_Conformant; |
6710 | ||
996ae0b0 RK |
6711 | --------------------------------- |
6712 | -- Is_Non_Overriding_Operation -- | |
6713 | --------------------------------- | |
6714 | ||
6715 | function Is_Non_Overriding_Operation | |
6716 | (Prev_E : Entity_Id; | |
d05ef0ab | 6717 | New_E : Entity_Id) return Boolean |
996ae0b0 RK |
6718 | is |
6719 | Formal : Entity_Id; | |
6720 | F_Typ : Entity_Id; | |
6721 | G_Typ : Entity_Id := Empty; | |
6722 | ||
6723 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
6724 | -- If F_Type is a derived type associated with a generic actual subtype, |
6725 | -- then return its Generic_Parent_Type attribute, else return Empty. | |
996ae0b0 RK |
6726 | |
6727 | function Types_Correspond | |
6728 | (P_Type : Entity_Id; | |
d05ef0ab | 6729 | N_Type : Entity_Id) return Boolean; |
82c80734 RD |
6730 | -- Returns true if and only if the types (or designated types in the |
6731 | -- case of anonymous access types) are the same or N_Type is derived | |
6732 | -- directly or indirectly from P_Type. | |
996ae0b0 RK |
6733 | |
6734 | ----------------------------- | |
6735 | -- Get_Generic_Parent_Type -- | |
6736 | ----------------------------- | |
6737 | ||
6738 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is | |
6739 | G_Typ : Entity_Id; | |
6740 | Indic : Node_Id; | |
6741 | ||
6742 | begin | |
6743 | if Is_Derived_Type (F_Typ) | |
6744 | and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration | |
6745 | then | |
82c80734 RD |
6746 | -- The tree must be traversed to determine the parent subtype in |
6747 | -- the generic unit, which unfortunately isn't always available | |
6748 | -- via semantic attributes. ??? (Note: The use of Original_Node | |
6749 | -- is needed for cases where a full derived type has been | |
6750 | -- rewritten.) | |
996ae0b0 RK |
6751 | |
6752 | Indic := Subtype_Indication | |
6753 | (Type_Definition (Original_Node (Parent (F_Typ)))); | |
6754 | ||
6755 | if Nkind (Indic) = N_Subtype_Indication then | |
6756 | G_Typ := Entity (Subtype_Mark (Indic)); | |
6757 | else | |
6758 | G_Typ := Entity (Indic); | |
6759 | end if; | |
6760 | ||
6761 | if Nkind (Parent (G_Typ)) = N_Subtype_Declaration | |
6762 | and then Present (Generic_Parent_Type (Parent (G_Typ))) | |
6763 | then | |
6764 | return Generic_Parent_Type (Parent (G_Typ)); | |
6765 | end if; | |
6766 | end if; | |
6767 | ||
6768 | return Empty; | |
6769 | end Get_Generic_Parent_Type; | |
6770 | ||
6771 | ---------------------- | |
6772 | -- Types_Correspond -- | |
6773 | ---------------------- | |
6774 | ||
6775 | function Types_Correspond | |
6776 | (P_Type : Entity_Id; | |
d05ef0ab | 6777 | N_Type : Entity_Id) return Boolean |
996ae0b0 RK |
6778 | is |
6779 | Prev_Type : Entity_Id := Base_Type (P_Type); | |
6780 | New_Type : Entity_Id := Base_Type (N_Type); | |
6781 | ||
6782 | begin | |
6783 | if Ekind (Prev_Type) = E_Anonymous_Access_Type then | |
6784 | Prev_Type := Designated_Type (Prev_Type); | |
6785 | end if; | |
6786 | ||
6787 | if Ekind (New_Type) = E_Anonymous_Access_Type then | |
6788 | New_Type := Designated_Type (New_Type); | |
6789 | end if; | |
6790 | ||
6791 | if Prev_Type = New_Type then | |
6792 | return True; | |
6793 | ||
6794 | elsif not Is_Class_Wide_Type (New_Type) then | |
6795 | while Etype (New_Type) /= New_Type loop | |
6796 | New_Type := Etype (New_Type); | |
6797 | if New_Type = Prev_Type then | |
6798 | return True; | |
6799 | end if; | |
6800 | end loop; | |
6801 | end if; | |
6802 | return False; | |
6803 | end Types_Correspond; | |
6804 | ||
6805 | -- Start of processing for Is_Non_Overriding_Operation | |
6806 | ||
6807 | begin | |
82c80734 RD |
6808 | -- In the case where both operations are implicit derived subprograms |
6809 | -- then neither overrides the other. This can only occur in certain | |
6810 | -- obscure cases (e.g., derivation from homographs created in a generic | |
6811 | -- instantiation). | |
996ae0b0 RK |
6812 | |
6813 | if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then | |
6814 | return True; | |
6815 | ||
6816 | elsif Ekind (Current_Scope) = E_Package | |
6817 | and then Is_Generic_Instance (Current_Scope) | |
6818 | and then In_Private_Part (Current_Scope) | |
6819 | and then Comes_From_Source (New_E) | |
6820 | then | |
6821 | -- We examine the formals and result subtype of the inherited | |
82c80734 RD |
6822 | -- operation, to determine whether their type is derived from (the |
6823 | -- instance of) a generic type. | |
996ae0b0 RK |
6824 | |
6825 | Formal := First_Formal (Prev_E); | |
996ae0b0 RK |
6826 | while Present (Formal) loop |
6827 | F_Typ := Base_Type (Etype (Formal)); | |
6828 | ||
6829 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
6830 | F_Typ := Designated_Type (F_Typ); | |
6831 | end if; | |
6832 | ||
6833 | G_Typ := Get_Generic_Parent_Type (F_Typ); | |
6834 | ||
6835 | Next_Formal (Formal); | |
6836 | end loop; | |
6837 | ||
c8ef728f | 6838 | if No (G_Typ) and then Ekind (Prev_E) = E_Function then |
996ae0b0 RK |
6839 | G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E))); |
6840 | end if; | |
6841 | ||
6842 | if No (G_Typ) then | |
6843 | return False; | |
6844 | end if; | |
6845 | ||
8dbd1460 AC |
6846 | -- If the generic type is a private type, then the original operation |
6847 | -- was not overriding in the generic, because there was no primitive | |
6848 | -- operation to override. | |
996ae0b0 RK |
6849 | |
6850 | if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration | |
6851 | and then Nkind (Formal_Type_Definition (Parent (G_Typ))) = | |
8dbd1460 | 6852 | N_Formal_Private_Type_Definition |
996ae0b0 RK |
6853 | then |
6854 | return True; | |
6855 | ||
6856 | -- The generic parent type is the ancestor of a formal derived | |
6857 | -- type declaration. We need to check whether it has a primitive | |
6858 | -- operation that should be overridden by New_E in the generic. | |
6859 | ||
6860 | else | |
6861 | declare | |
6862 | P_Formal : Entity_Id; | |
6863 | N_Formal : Entity_Id; | |
6864 | P_Typ : Entity_Id; | |
6865 | N_Typ : Entity_Id; | |
6866 | P_Prim : Entity_Id; | |
6867 | Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ)); | |
6868 | ||
6869 | begin | |
6870 | while Present (Prim_Elt) loop | |
6871 | P_Prim := Node (Prim_Elt); | |
fbf5a39b | 6872 | |
996ae0b0 RK |
6873 | if Chars (P_Prim) = Chars (New_E) |
6874 | and then Ekind (P_Prim) = Ekind (New_E) | |
6875 | then | |
6876 | P_Formal := First_Formal (P_Prim); | |
6877 | N_Formal := First_Formal (New_E); | |
6878 | while Present (P_Formal) and then Present (N_Formal) loop | |
6879 | P_Typ := Etype (P_Formal); | |
6880 | N_Typ := Etype (N_Formal); | |
6881 | ||
6882 | if not Types_Correspond (P_Typ, N_Typ) then | |
6883 | exit; | |
6884 | end if; | |
6885 | ||
6886 | Next_Entity (P_Formal); | |
6887 | Next_Entity (N_Formal); | |
6888 | end loop; | |
6889 | ||
82c80734 RD |
6890 | -- Found a matching primitive operation belonging to the |
6891 | -- formal ancestor type, so the new subprogram is | |
6892 | -- overriding. | |
996ae0b0 | 6893 | |
c8ef728f ES |
6894 | if No (P_Formal) |
6895 | and then No (N_Formal) | |
996ae0b0 RK |
6896 | and then (Ekind (New_E) /= E_Function |
6897 | or else | |
6898 | Types_Correspond | |
6899 | (Etype (P_Prim), Etype (New_E))) | |
6900 | then | |
6901 | return False; | |
6902 | end if; | |
6903 | end if; | |
6904 | ||
6905 | Next_Elmt (Prim_Elt); | |
6906 | end loop; | |
6907 | ||
82c80734 RD |
6908 | -- If no match found, then the new subprogram does not |
6909 | -- override in the generic (nor in the instance). | |
996ae0b0 RK |
6910 | |
6911 | return True; | |
6912 | end; | |
6913 | end if; | |
6914 | else | |
6915 | return False; | |
6916 | end if; | |
6917 | end Is_Non_Overriding_Operation; | |
6918 | ||
6919 | ------------------------------ | |
6920 | -- Make_Inequality_Operator -- | |
6921 | ------------------------------ | |
6922 | ||
6923 | -- S is the defining identifier of an equality operator. We build a | |
6924 | -- subprogram declaration with the right signature. This operation is | |
6925 | -- intrinsic, because it is always expanded as the negation of the | |
6926 | -- call to the equality function. | |
6927 | ||
6928 | procedure Make_Inequality_Operator (S : Entity_Id) is | |
6929 | Loc : constant Source_Ptr := Sloc (S); | |
6930 | Decl : Node_Id; | |
6931 | Formals : List_Id; | |
6932 | Op_Name : Entity_Id; | |
6933 | ||
c8ef728f ES |
6934 | FF : constant Entity_Id := First_Formal (S); |
6935 | NF : constant Entity_Id := Next_Formal (FF); | |
996ae0b0 RK |
6936 | |
6937 | begin | |
c8ef728f | 6938 | -- Check that equality was properly defined, ignore call if not |
996ae0b0 | 6939 | |
c8ef728f | 6940 | if No (NF) then |
996ae0b0 RK |
6941 | return; |
6942 | end if; | |
6943 | ||
c8ef728f ES |
6944 | declare |
6945 | A : constant Entity_Id := | |
6946 | Make_Defining_Identifier (Sloc (FF), | |
6947 | Chars => Chars (FF)); | |
6948 | ||
5d37ba92 ES |
6949 | B : constant Entity_Id := |
6950 | Make_Defining_Identifier (Sloc (NF), | |
6951 | Chars => Chars (NF)); | |
c8ef728f ES |
6952 | |
6953 | begin | |
6954 | Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne); | |
6955 | ||
6956 | Formals := New_List ( | |
6957 | Make_Parameter_Specification (Loc, | |
6958 | Defining_Identifier => A, | |
6959 | Parameter_Type => | |
6960 | New_Reference_To (Etype (First_Formal (S)), | |
6961 | Sloc (Etype (First_Formal (S))))), | |
6962 | ||
6963 | Make_Parameter_Specification (Loc, | |
6964 | Defining_Identifier => B, | |
6965 | Parameter_Type => | |
6966 | New_Reference_To (Etype (Next_Formal (First_Formal (S))), | |
6967 | Sloc (Etype (Next_Formal (First_Formal (S))))))); | |
6968 | ||
6969 | Decl := | |
6970 | Make_Subprogram_Declaration (Loc, | |
6971 | Specification => | |
6972 | Make_Function_Specification (Loc, | |
6973 | Defining_Unit_Name => Op_Name, | |
6974 | Parameter_Specifications => Formals, | |
6975 | Result_Definition => | |
6976 | New_Reference_To (Standard_Boolean, Loc))); | |
6977 | ||
6978 | -- Insert inequality right after equality if it is explicit or after | |
6979 | -- the derived type when implicit. These entities are created only | |
6980 | -- for visibility purposes, and eventually replaced in the course of | |
6981 | -- expansion, so they do not need to be attached to the tree and seen | |
6982 | -- by the back-end. Keeping them internal also avoids spurious | |
6983 | -- freezing problems. The declaration is inserted in the tree for | |
6984 | -- analysis, and removed afterwards. If the equality operator comes | |
6985 | -- from an explicit declaration, attach the inequality immediately | |
6986 | -- after. Else the equality is inherited from a derived type | |
6987 | -- declaration, so insert inequality after that declaration. | |
6988 | ||
6989 | if No (Alias (S)) then | |
6990 | Insert_After (Unit_Declaration_Node (S), Decl); | |
6991 | elsif Is_List_Member (Parent (S)) then | |
6992 | Insert_After (Parent (S), Decl); | |
6993 | else | |
6994 | Insert_After (Parent (Etype (First_Formal (S))), Decl); | |
6995 | end if; | |
996ae0b0 | 6996 | |
c8ef728f ES |
6997 | Mark_Rewrite_Insertion (Decl); |
6998 | Set_Is_Intrinsic_Subprogram (Op_Name); | |
6999 | Analyze (Decl); | |
7000 | Remove (Decl); | |
7001 | Set_Has_Completion (Op_Name); | |
7002 | Set_Corresponding_Equality (Op_Name, S); | |
f937473f | 7003 | Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S)); |
c8ef728f | 7004 | end; |
996ae0b0 RK |
7005 | end Make_Inequality_Operator; |
7006 | ||
7007 | ---------------------- | |
7008 | -- May_Need_Actuals -- | |
7009 | ---------------------- | |
7010 | ||
7011 | procedure May_Need_Actuals (Fun : Entity_Id) is | |
7012 | F : Entity_Id; | |
7013 | B : Boolean; | |
7014 | ||
7015 | begin | |
7016 | F := First_Formal (Fun); | |
7017 | B := True; | |
996ae0b0 RK |
7018 | while Present (F) loop |
7019 | if No (Default_Value (F)) then | |
7020 | B := False; | |
7021 | exit; | |
7022 | end if; | |
7023 | ||
7024 | Next_Formal (F); | |
7025 | end loop; | |
7026 | ||
7027 | Set_Needs_No_Actuals (Fun, B); | |
7028 | end May_Need_Actuals; | |
7029 | ||
7030 | --------------------- | |
7031 | -- Mode_Conformant -- | |
7032 | --------------------- | |
7033 | ||
7034 | function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
7035 | Result : Boolean; | |
996ae0b0 RK |
7036 | begin |
7037 | Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result); | |
7038 | return Result; | |
7039 | end Mode_Conformant; | |
7040 | ||
7041 | --------------------------- | |
7042 | -- New_Overloaded_Entity -- | |
7043 | --------------------------- | |
7044 | ||
7045 | procedure New_Overloaded_Entity | |
7046 | (S : Entity_Id; | |
7047 | Derived_Type : Entity_Id := Empty) | |
7048 | is | |
ec4867fa | 7049 | Overridden_Subp : Entity_Id := Empty; |
758c442c GD |
7050 | -- Set if the current scope has an operation that is type-conformant |
7051 | -- with S, and becomes hidden by S. | |
7052 | ||
5d37ba92 ES |
7053 | Is_Primitive_Subp : Boolean; |
7054 | -- Set to True if the new subprogram is primitive | |
7055 | ||
fbf5a39b AC |
7056 | E : Entity_Id; |
7057 | -- Entity that S overrides | |
7058 | ||
996ae0b0 | 7059 | Prev_Vis : Entity_Id := Empty; |
ec4867fa ES |
7060 | -- Predecessor of E in Homonym chain |
7061 | ||
5d37ba92 ES |
7062 | procedure Check_For_Primitive_Subprogram |
7063 | (Is_Primitive : out Boolean; | |
7064 | Is_Overriding : Boolean := False); | |
7065 | -- If the subprogram being analyzed is a primitive operation of the type | |
7066 | -- of a formal or result, set the Has_Primitive_Operations flag on the | |
7067 | -- type, and set Is_Primitive to True (otherwise set to False). Set the | |
7068 | -- corresponding flag on the entity itself for later use. | |
7069 | ||
ec4867fa ES |
7070 | procedure Check_Synchronized_Overriding |
7071 | (Def_Id : Entity_Id; | |
ec4867fa ES |
7072 | Overridden_Subp : out Entity_Id); |
7073 | -- First determine if Def_Id is an entry or a subprogram either defined | |
7074 | -- in the scope of a task or protected type, or is a primitive of such | |
7075 | -- a type. Check whether Def_Id overrides a subprogram of an interface | |
7076 | -- implemented by the synchronized type, return the overridden entity | |
7077 | -- or Empty. | |
758c442c | 7078 | |
996ae0b0 RK |
7079 | function Is_Private_Declaration (E : Entity_Id) return Boolean; |
7080 | -- Check that E is declared in the private part of the current package, | |
7081 | -- or in the package body, where it may hide a previous declaration. | |
fbf5a39b | 7082 | -- We can't use In_Private_Part by itself because this flag is also |
996ae0b0 RK |
7083 | -- set when freezing entities, so we must examine the place of the |
7084 | -- declaration in the tree, and recognize wrapper packages as well. | |
7085 | ||
2ddc2000 AC |
7086 | function Is_Overriding_Alias |
7087 | (Old_E : Entity_Id; | |
7088 | New_E : Entity_Id) return Boolean; | |
7089 | -- Check whether new subprogram and old subprogram are both inherited | |
7090 | -- from subprograms that have distinct dispatch table entries. This can | |
7091 | -- occur with derivations from instances with accidental homonyms. | |
7092 | -- The function is conservative given that the converse is only true | |
7093 | -- within instances that contain accidental overloadings. | |
7094 | ||
5d37ba92 ES |
7095 | ------------------------------------ |
7096 | -- Check_For_Primitive_Subprogram -- | |
7097 | ------------------------------------ | |
996ae0b0 | 7098 | |
5d37ba92 ES |
7099 | procedure Check_For_Primitive_Subprogram |
7100 | (Is_Primitive : out Boolean; | |
7101 | Is_Overriding : Boolean := False) | |
ec4867fa | 7102 | is |
996ae0b0 RK |
7103 | Formal : Entity_Id; |
7104 | F_Typ : Entity_Id; | |
07fc65c4 | 7105 | B_Typ : Entity_Id; |
996ae0b0 RK |
7106 | |
7107 | function Visible_Part_Type (T : Entity_Id) return Boolean; | |
8dbd1460 AC |
7108 | -- Returns true if T is declared in the visible part of the current |
7109 | -- package scope; otherwise returns false. Assumes that T is declared | |
7110 | -- in a package. | |
996ae0b0 RK |
7111 | |
7112 | procedure Check_Private_Overriding (T : Entity_Id); | |
7113 | -- Checks that if a primitive abstract subprogram of a visible | |
8dbd1460 AC |
7114 | -- abstract type is declared in a private part, then it must override |
7115 | -- an abstract subprogram declared in the visible part. Also checks | |
7116 | -- that if a primitive function with a controlling result is declared | |
7117 | -- in a private part, then it must override a function declared in | |
7118 | -- the visible part. | |
996ae0b0 RK |
7119 | |
7120 | ------------------------------ | |
7121 | -- Check_Private_Overriding -- | |
7122 | ------------------------------ | |
7123 | ||
7124 | procedure Check_Private_Overriding (T : Entity_Id) is | |
7125 | begin | |
51c16e29 | 7126 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
7127 | and then In_Private_Part (Current_Scope) |
7128 | and then Visible_Part_Type (T) | |
7129 | and then not In_Instance | |
7130 | then | |
f937473f RD |
7131 | if Is_Abstract_Type (T) |
7132 | and then Is_Abstract_Subprogram (S) | |
7133 | and then (not Is_Overriding | |
8dbd1460 | 7134 | or else not Is_Abstract_Subprogram (E)) |
996ae0b0 | 7135 | then |
ed2233dc | 7136 | Error_Msg_N |
19d846a0 RD |
7137 | ("abstract subprograms must be visible " |
7138 | & "(RM 3.9.3(10))!", S); | |
758c442c | 7139 | |
996ae0b0 | 7140 | elsif Ekind (S) = E_Function |
82c80734 | 7141 | and then not Is_Overriding |
996ae0b0 | 7142 | then |
2e79de51 AC |
7143 | if Is_Tagged_Type (T) |
7144 | and then T = Base_Type (Etype (S)) | |
7145 | then | |
7146 | Error_Msg_N | |
7147 | ("private function with tagged result must" | |
7148 | & " override visible-part function", S); | |
7149 | Error_Msg_N | |
7150 | ("\move subprogram to the visible part" | |
7151 | & " (RM 3.9.3(10))", S); | |
7152 | ||
7153 | -- AI05-0073: extend this test to the case of a function | |
7154 | -- with a controlling access result. | |
7155 | ||
7156 | elsif Ekind (Etype (S)) = E_Anonymous_Access_Type | |
7157 | and then Is_Tagged_Type (Designated_Type (Etype (S))) | |
7158 | and then | |
7159 | not Is_Class_Wide_Type (Designated_Type (Etype (S))) | |
dbe945f1 | 7160 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
7161 | then |
7162 | Error_Msg_N | |
7163 | ("private function with controlling access result " | |
7164 | & "must override visible-part function", S); | |
7165 | Error_Msg_N | |
7166 | ("\move subprogram to the visible part" | |
7167 | & " (RM 3.9.3(10))", S); | |
7168 | end if; | |
996ae0b0 RK |
7169 | end if; |
7170 | end if; | |
7171 | end Check_Private_Overriding; | |
7172 | ||
7173 | ----------------------- | |
7174 | -- Visible_Part_Type -- | |
7175 | ----------------------- | |
7176 | ||
7177 | function Visible_Part_Type (T : Entity_Id) return Boolean is | |
07fc65c4 GB |
7178 | P : constant Node_Id := Unit_Declaration_Node (Scope (T)); |
7179 | N : Node_Id; | |
996ae0b0 RK |
7180 | |
7181 | begin | |
8dbd1460 AC |
7182 | -- If the entity is a private type, then it must be declared in a |
7183 | -- visible part. | |
996ae0b0 RK |
7184 | |
7185 | if Ekind (T) in Private_Kind then | |
7186 | return True; | |
7187 | end if; | |
7188 | ||
7189 | -- Otherwise, we traverse the visible part looking for its | |
7190 | -- corresponding declaration. We cannot use the declaration | |
7191 | -- node directly because in the private part the entity of a | |
7192 | -- private type is the one in the full view, which does not | |
7193 | -- indicate that it is the completion of something visible. | |
7194 | ||
07fc65c4 | 7195 | N := First (Visible_Declarations (Specification (P))); |
996ae0b0 RK |
7196 | while Present (N) loop |
7197 | if Nkind (N) = N_Full_Type_Declaration | |
7198 | and then Present (Defining_Identifier (N)) | |
7199 | and then T = Defining_Identifier (N) | |
7200 | then | |
7201 | return True; | |
7202 | ||
800621e0 RD |
7203 | elsif Nkind_In (N, N_Private_Type_Declaration, |
7204 | N_Private_Extension_Declaration) | |
996ae0b0 RK |
7205 | and then Present (Defining_Identifier (N)) |
7206 | and then T = Full_View (Defining_Identifier (N)) | |
7207 | then | |
7208 | return True; | |
7209 | end if; | |
7210 | ||
7211 | Next (N); | |
7212 | end loop; | |
7213 | ||
7214 | return False; | |
7215 | end Visible_Part_Type; | |
7216 | ||
5d37ba92 | 7217 | -- Start of processing for Check_For_Primitive_Subprogram |
996ae0b0 RK |
7218 | |
7219 | begin | |
5d37ba92 ES |
7220 | Is_Primitive := False; |
7221 | ||
996ae0b0 RK |
7222 | if not Comes_From_Source (S) then |
7223 | null; | |
7224 | ||
5d37ba92 | 7225 | -- If subprogram is at library level, it is not primitive operation |
15ce9ca2 AC |
7226 | |
7227 | elsif Current_Scope = Standard_Standard then | |
7228 | null; | |
7229 | ||
b9b2405f | 7230 | elsif (Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 | 7231 | and then not In_Package_Body (Current_Scope)) |
82c80734 | 7232 | or else Is_Overriding |
996ae0b0 | 7233 | then |
07fc65c4 | 7234 | -- For function, check return type |
996ae0b0 | 7235 | |
07fc65c4 | 7236 | if Ekind (S) = E_Function then |
5d37ba92 ES |
7237 | if Ekind (Etype (S)) = E_Anonymous_Access_Type then |
7238 | F_Typ := Designated_Type (Etype (S)); | |
7239 | else | |
7240 | F_Typ := Etype (S); | |
7241 | end if; | |
7242 | ||
7243 | B_Typ := Base_Type (F_Typ); | |
07fc65c4 | 7244 | |
5d37ba92 ES |
7245 | if Scope (B_Typ) = Current_Scope |
7246 | and then not Is_Class_Wide_Type (B_Typ) | |
7247 | and then not Is_Generic_Type (B_Typ) | |
7248 | then | |
7249 | Is_Primitive := True; | |
07fc65c4 | 7250 | Set_Has_Primitive_Operations (B_Typ); |
5d37ba92 | 7251 | Set_Is_Primitive (S); |
07fc65c4 GB |
7252 | Check_Private_Overriding (B_Typ); |
7253 | end if; | |
996ae0b0 RK |
7254 | end if; |
7255 | ||
07fc65c4 | 7256 | -- For all subprograms, check formals |
996ae0b0 | 7257 | |
07fc65c4 | 7258 | Formal := First_Formal (S); |
996ae0b0 RK |
7259 | while Present (Formal) loop |
7260 | if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then | |
7261 | F_Typ := Designated_Type (Etype (Formal)); | |
7262 | else | |
7263 | F_Typ := Etype (Formal); | |
7264 | end if; | |
7265 | ||
07fc65c4 GB |
7266 | B_Typ := Base_Type (F_Typ); |
7267 | ||
ec4867fa ES |
7268 | if Ekind (B_Typ) = E_Access_Subtype then |
7269 | B_Typ := Base_Type (B_Typ); | |
7270 | end if; | |
7271 | ||
5d37ba92 ES |
7272 | if Scope (B_Typ) = Current_Scope |
7273 | and then not Is_Class_Wide_Type (B_Typ) | |
7274 | and then not Is_Generic_Type (B_Typ) | |
7275 | then | |
7276 | Is_Primitive := True; | |
7277 | Set_Is_Primitive (S); | |
07fc65c4 GB |
7278 | Set_Has_Primitive_Operations (B_Typ); |
7279 | Check_Private_Overriding (B_Typ); | |
996ae0b0 RK |
7280 | end if; |
7281 | ||
7282 | Next_Formal (Formal); | |
7283 | end loop; | |
996ae0b0 | 7284 | end if; |
5d37ba92 ES |
7285 | end Check_For_Primitive_Subprogram; |
7286 | ||
7287 | ----------------------------------- | |
7288 | -- Check_Synchronized_Overriding -- | |
7289 | ----------------------------------- | |
7290 | ||
7291 | procedure Check_Synchronized_Overriding | |
7292 | (Def_Id : Entity_Id; | |
5d37ba92 ES |
7293 | Overridden_Subp : out Entity_Id) |
7294 | is | |
5d37ba92 ES |
7295 | Ifaces_List : Elist_Id; |
7296 | In_Scope : Boolean; | |
7297 | Typ : Entity_Id; | |
7298 | ||
8aa15e3b JM |
7299 | function Matches_Prefixed_View_Profile |
7300 | (Prim_Params : List_Id; | |
7301 | Iface_Params : List_Id) return Boolean; | |
7302 | -- Determine whether a subprogram's parameter profile Prim_Params | |
7303 | -- matches that of a potentially overridden interface subprogram | |
7304 | -- Iface_Params. Also determine if the type of first parameter of | |
7305 | -- Iface_Params is an implemented interface. | |
7306 | ||
8aa15e3b JM |
7307 | ----------------------------------- |
7308 | -- Matches_Prefixed_View_Profile -- | |
7309 | ----------------------------------- | |
7310 | ||
7311 | function Matches_Prefixed_View_Profile | |
7312 | (Prim_Params : List_Id; | |
7313 | Iface_Params : List_Id) return Boolean | |
7314 | is | |
7315 | Iface_Id : Entity_Id; | |
7316 | Iface_Param : Node_Id; | |
7317 | Iface_Typ : Entity_Id; | |
7318 | Prim_Id : Entity_Id; | |
7319 | Prim_Param : Node_Id; | |
7320 | Prim_Typ : Entity_Id; | |
7321 | ||
7322 | function Is_Implemented | |
7323 | (Ifaces_List : Elist_Id; | |
7324 | Iface : Entity_Id) return Boolean; | |
7325 | -- Determine if Iface is implemented by the current task or | |
7326 | -- protected type. | |
7327 | ||
7328 | -------------------- | |
7329 | -- Is_Implemented -- | |
7330 | -------------------- | |
7331 | ||
7332 | function Is_Implemented | |
7333 | (Ifaces_List : Elist_Id; | |
7334 | Iface : Entity_Id) return Boolean | |
7335 | is | |
7336 | Iface_Elmt : Elmt_Id; | |
7337 | ||
7338 | begin | |
7339 | Iface_Elmt := First_Elmt (Ifaces_List); | |
7340 | while Present (Iface_Elmt) loop | |
7341 | if Node (Iface_Elmt) = Iface then | |
7342 | return True; | |
7343 | end if; | |
7344 | ||
7345 | Next_Elmt (Iface_Elmt); | |
7346 | end loop; | |
7347 | ||
7348 | return False; | |
7349 | end Is_Implemented; | |
7350 | ||
7351 | -- Start of processing for Matches_Prefixed_View_Profile | |
7352 | ||
7353 | begin | |
7354 | Iface_Param := First (Iface_Params); | |
7355 | Iface_Typ := Etype (Defining_Identifier (Iface_Param)); | |
7356 | ||
7357 | if Is_Access_Type (Iface_Typ) then | |
7358 | Iface_Typ := Designated_Type (Iface_Typ); | |
7359 | end if; | |
7360 | ||
7361 | Prim_Param := First (Prim_Params); | |
7362 | ||
7363 | -- The first parameter of the potentially overridden subprogram | |
7364 | -- must be an interface implemented by Prim. | |
7365 | ||
7366 | if not Is_Interface (Iface_Typ) | |
7367 | or else not Is_Implemented (Ifaces_List, Iface_Typ) | |
7368 | then | |
7369 | return False; | |
7370 | end if; | |
7371 | ||
7372 | -- The checks on the object parameters are done, move onto the | |
7373 | -- rest of the parameters. | |
7374 | ||
7375 | if not In_Scope then | |
7376 | Prim_Param := Next (Prim_Param); | |
7377 | end if; | |
7378 | ||
7379 | Iface_Param := Next (Iface_Param); | |
7380 | while Present (Iface_Param) and then Present (Prim_Param) loop | |
7381 | Iface_Id := Defining_Identifier (Iface_Param); | |
7382 | Iface_Typ := Find_Parameter_Type (Iface_Param); | |
7383 | ||
8aa15e3b JM |
7384 | Prim_Id := Defining_Identifier (Prim_Param); |
7385 | Prim_Typ := Find_Parameter_Type (Prim_Param); | |
7386 | ||
15e4986c JM |
7387 | if Ekind (Iface_Typ) = E_Anonymous_Access_Type |
7388 | and then Ekind (Prim_Typ) = E_Anonymous_Access_Type | |
7389 | and then Is_Concurrent_Type (Designated_Type (Prim_Typ)) | |
7390 | then | |
7391 | Iface_Typ := Designated_Type (Iface_Typ); | |
7392 | Prim_Typ := Designated_Type (Prim_Typ); | |
8aa15e3b JM |
7393 | end if; |
7394 | ||
7395 | -- Case of multiple interface types inside a parameter profile | |
7396 | ||
7397 | -- (Obj_Param : in out Iface; ...; Param : Iface) | |
7398 | ||
7399 | -- If the interface type is implemented, then the matching type | |
7400 | -- in the primitive should be the implementing record type. | |
7401 | ||
7402 | if Ekind (Iface_Typ) = E_Record_Type | |
7403 | and then Is_Interface (Iface_Typ) | |
7404 | and then Is_Implemented (Ifaces_List, Iface_Typ) | |
7405 | then | |
7406 | if Prim_Typ /= Typ then | |
7407 | return False; | |
7408 | end if; | |
7409 | ||
7410 | -- The two parameters must be both mode and subtype conformant | |
7411 | ||
7412 | elsif Ekind (Iface_Id) /= Ekind (Prim_Id) | |
7413 | or else not | |
7414 | Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant) | |
7415 | then | |
7416 | return False; | |
7417 | end if; | |
7418 | ||
7419 | Next (Iface_Param); | |
7420 | Next (Prim_Param); | |
7421 | end loop; | |
7422 | ||
7423 | -- One of the two lists contains more parameters than the other | |
7424 | ||
7425 | if Present (Iface_Param) or else Present (Prim_Param) then | |
7426 | return False; | |
7427 | end if; | |
7428 | ||
7429 | return True; | |
7430 | end Matches_Prefixed_View_Profile; | |
7431 | ||
7432 | -- Start of processing for Check_Synchronized_Overriding | |
7433 | ||
5d37ba92 ES |
7434 | begin |
7435 | Overridden_Subp := Empty; | |
7436 | ||
8aa15e3b JM |
7437 | -- Def_Id must be an entry or a subprogram. We should skip predefined |
7438 | -- primitives internally generated by the frontend; however at this | |
7439 | -- stage predefined primitives are still not fully decorated. As a | |
7440 | -- minor optimization we skip here internally generated subprograms. | |
5d37ba92 | 7441 | |
8aa15e3b JM |
7442 | if (Ekind (Def_Id) /= E_Entry |
7443 | and then Ekind (Def_Id) /= E_Function | |
7444 | and then Ekind (Def_Id) /= E_Procedure) | |
7445 | or else not Comes_From_Source (Def_Id) | |
5d37ba92 ES |
7446 | then |
7447 | return; | |
7448 | end if; | |
7449 | ||
7450 | -- Search for the concurrent declaration since it contains the list | |
7451 | -- of all implemented interfaces. In this case, the subprogram is | |
7452 | -- declared within the scope of a protected or a task type. | |
7453 | ||
7454 | if Present (Scope (Def_Id)) | |
7455 | and then Is_Concurrent_Type (Scope (Def_Id)) | |
7456 | and then not Is_Generic_Actual_Type (Scope (Def_Id)) | |
7457 | then | |
7458 | Typ := Scope (Def_Id); | |
7459 | In_Scope := True; | |
7460 | ||
8aa15e3b JM |
7461 | -- The enclosing scope is not a synchronized type and the subprogram |
7462 | -- has no formals | |
7463 | ||
7464 | elsif No (First_Formal (Def_Id)) then | |
7465 | return; | |
5d37ba92 | 7466 | |
8aa15e3b JM |
7467 | -- The subprogram has formals and hence it may be a primitive of a |
7468 | -- concurrent type | |
5d37ba92 | 7469 | |
8aa15e3b JM |
7470 | else |
7471 | Typ := Etype (First_Formal (Def_Id)); | |
7472 | ||
7473 | if Is_Access_Type (Typ) then | |
7474 | Typ := Directly_Designated_Type (Typ); | |
8c3dd7a8 JM |
7475 | end if; |
7476 | ||
8aa15e3b JM |
7477 | if Is_Concurrent_Type (Typ) |
7478 | and then not Is_Generic_Actual_Type (Typ) | |
5d37ba92 | 7479 | then |
5d37ba92 ES |
7480 | In_Scope := False; |
7481 | ||
7482 | -- This case occurs when the concurrent type is declared within | |
7483 | -- a generic unit. As a result the corresponding record has been | |
7484 | -- built and used as the type of the first formal, we just have | |
7485 | -- to retrieve the corresponding concurrent type. | |
7486 | ||
8aa15e3b JM |
7487 | elsif Is_Concurrent_Record_Type (Typ) |
7488 | and then Present (Corresponding_Concurrent_Type (Typ)) | |
5d37ba92 | 7489 | then |
8aa15e3b | 7490 | Typ := Corresponding_Concurrent_Type (Typ); |
5d37ba92 ES |
7491 | In_Scope := False; |
7492 | ||
7493 | else | |
7494 | return; | |
7495 | end if; | |
8aa15e3b JM |
7496 | end if; |
7497 | ||
7498 | -- There is no overriding to check if is an inherited operation in a | |
7499 | -- type derivation on for a generic actual. | |
7500 | ||
7501 | Collect_Interfaces (Typ, Ifaces_List); | |
7502 | ||
7503 | if Is_Empty_Elmt_List (Ifaces_List) then | |
5d37ba92 ES |
7504 | return; |
7505 | end if; | |
7506 | ||
8aa15e3b JM |
7507 | -- Determine whether entry or subprogram Def_Id overrides a primitive |
7508 | -- operation that belongs to one of the interfaces in Ifaces_List. | |
5d37ba92 | 7509 | |
8aa15e3b JM |
7510 | declare |
7511 | Candidate : Entity_Id := Empty; | |
7512 | Hom : Entity_Id := Empty; | |
7513 | Iface_Typ : Entity_Id; | |
7514 | Subp : Entity_Id := Empty; | |
7515 | ||
7516 | begin | |
7517 | -- Traverse the homonym chain, looking at a potentially | |
7518 | -- overridden subprogram that belongs to an implemented | |
7519 | -- interface. | |
7520 | ||
7521 | Hom := Current_Entity_In_Scope (Def_Id); | |
7522 | while Present (Hom) loop | |
7523 | Subp := Hom; | |
7524 | ||
15e4986c JM |
7525 | if Subp = Def_Id |
7526 | or else not Is_Overloadable (Subp) | |
7527 | or else not Is_Primitive (Subp) | |
7528 | or else not Is_Dispatching_Operation (Subp) | |
79afa047 | 7529 | or else not Present (Find_Dispatching_Type (Subp)) |
15e4986c | 7530 | or else not Is_Interface (Find_Dispatching_Type (Subp)) |
8aa15e3b | 7531 | then |
15e4986c | 7532 | null; |
8aa15e3b | 7533 | |
15e4986c JM |
7534 | -- Entries and procedures can override abstract or null |
7535 | -- interface procedures | |
8aa15e3b | 7536 | |
15e4986c JM |
7537 | elsif (Ekind (Def_Id) = E_Procedure |
7538 | or else Ekind (Def_Id) = E_Entry) | |
8aa15e3b | 7539 | and then Ekind (Subp) = E_Procedure |
8aa15e3b JM |
7540 | and then Matches_Prefixed_View_Profile |
7541 | (Parameter_Specifications (Parent (Def_Id)), | |
7542 | Parameter_Specifications (Parent (Subp))) | |
7543 | then | |
7544 | Candidate := Subp; | |
7545 | ||
15e4986c JM |
7546 | -- For an overridden subprogram Subp, check whether the mode |
7547 | -- of its first parameter is correct depending on the kind | |
7548 | -- of synchronized type. | |
8aa15e3b | 7549 | |
15e4986c JM |
7550 | declare |
7551 | Formal : constant Node_Id := First_Formal (Candidate); | |
7552 | ||
7553 | begin | |
7554 | -- In order for an entry or a protected procedure to | |
7555 | -- override, the first parameter of the overridden | |
7556 | -- routine must be of mode "out", "in out" or | |
7557 | -- access-to-variable. | |
7558 | ||
7559 | if (Ekind (Candidate) = E_Entry | |
7560 | or else Ekind (Candidate) = E_Procedure) | |
7561 | and then Is_Protected_Type (Typ) | |
7562 | and then Ekind (Formal) /= E_In_Out_Parameter | |
7563 | and then Ekind (Formal) /= E_Out_Parameter | |
7564 | and then Nkind (Parameter_Type (Parent (Formal))) | |
7565 | /= N_Access_Definition | |
7566 | then | |
7567 | null; | |
7568 | ||
7569 | -- All other cases are OK since a task entry or routine | |
7570 | -- does not have a restriction on the mode of the first | |
7571 | -- parameter of the overridden interface routine. | |
7572 | ||
7573 | else | |
7574 | Overridden_Subp := Candidate; | |
7575 | return; | |
7576 | end if; | |
7577 | end; | |
8aa15e3b JM |
7578 | |
7579 | -- Functions can override abstract interface functions | |
7580 | ||
7581 | elsif Ekind (Def_Id) = E_Function | |
7582 | and then Ekind (Subp) = E_Function | |
8aa15e3b JM |
7583 | and then Matches_Prefixed_View_Profile |
7584 | (Parameter_Specifications (Parent (Def_Id)), | |
7585 | Parameter_Specifications (Parent (Subp))) | |
7586 | and then Etype (Result_Definition (Parent (Def_Id))) = | |
7587 | Etype (Result_Definition (Parent (Subp))) | |
7588 | then | |
7589 | Overridden_Subp := Subp; | |
7590 | return; | |
7591 | end if; | |
7592 | ||
7593 | Hom := Homonym (Hom); | |
7594 | end loop; | |
7595 | ||
7596 | -- After examining all candidates for overriding, we are | |
7597 | -- left with the best match which is a mode incompatible | |
7598 | -- interface routine. Do not emit an error if the Expander | |
7599 | -- is active since this error will be detected later on | |
7600 | -- after all concurrent types are expanded and all wrappers | |
7601 | -- are built. This check is meant for spec-only | |
7602 | -- compilations. | |
7603 | ||
7604 | if Present (Candidate) | |
7605 | and then not Expander_Active | |
7606 | then | |
7607 | Iface_Typ := | |
7608 | Find_Parameter_Type (Parent (First_Formal (Candidate))); | |
7609 | ||
7610 | -- Def_Id is primitive of a protected type, declared | |
7611 | -- inside the type, and the candidate is primitive of a | |
7612 | -- limited or synchronized interface. | |
7613 | ||
7614 | if In_Scope | |
7615 | and then Is_Protected_Type (Typ) | |
7616 | and then | |
7617 | (Is_Limited_Interface (Iface_Typ) | |
7618 | or else Is_Protected_Interface (Iface_Typ) | |
7619 | or else Is_Synchronized_Interface (Iface_Typ) | |
7620 | or else Is_Task_Interface (Iface_Typ)) | |
7621 | then | |
7622 | -- Must reword this message, comma before to in -gnatj | |
7623 | -- mode ??? | |
7624 | ||
7625 | Error_Msg_NE | |
7626 | ("first formal of & must be of mode `OUT`, `IN OUT`" | |
7627 | & " or access-to-variable", Typ, Candidate); | |
7628 | Error_Msg_N | |
7629 | ("\to be overridden by protected procedure or entry " | |
7630 | & "(RM 9.4(11.9/2))", Typ); | |
7631 | end if; | |
5d37ba92 | 7632 | end if; |
8aa15e3b JM |
7633 | |
7634 | Overridden_Subp := Candidate; | |
7635 | return; | |
7636 | end; | |
5d37ba92 ES |
7637 | end Check_Synchronized_Overriding; |
7638 | ||
7639 | ---------------------------- | |
7640 | -- Is_Private_Declaration -- | |
7641 | ---------------------------- | |
7642 | ||
7643 | function Is_Private_Declaration (E : Entity_Id) return Boolean is | |
7644 | Priv_Decls : List_Id; | |
7645 | Decl : constant Node_Id := Unit_Declaration_Node (E); | |
7646 | ||
7647 | begin | |
7648 | if Is_Package_Or_Generic_Package (Current_Scope) | |
7649 | and then In_Private_Part (Current_Scope) | |
7650 | then | |
7651 | Priv_Decls := | |
7652 | Private_Declarations ( | |
7653 | Specification (Unit_Declaration_Node (Current_Scope))); | |
7654 | ||
7655 | return In_Package_Body (Current_Scope) | |
7656 | or else | |
7657 | (Is_List_Member (Decl) | |
7658 | and then List_Containing (Decl) = Priv_Decls) | |
7659 | or else (Nkind (Parent (Decl)) = N_Package_Specification | |
8dbd1460 AC |
7660 | and then not |
7661 | Is_Compilation_Unit | |
7662 | (Defining_Entity (Parent (Decl))) | |
5d37ba92 | 7663 | and then List_Containing (Parent (Parent (Decl))) |
8dbd1460 | 7664 | = Priv_Decls); |
5d37ba92 ES |
7665 | else |
7666 | return False; | |
7667 | end if; | |
7668 | end Is_Private_Declaration; | |
996ae0b0 | 7669 | |
2ddc2000 AC |
7670 | -------------------------- |
7671 | -- Is_Overriding_Alias -- | |
7672 | -------------------------- | |
7673 | ||
7674 | function Is_Overriding_Alias | |
7675 | (Old_E : Entity_Id; | |
7676 | New_E : Entity_Id) return Boolean | |
7677 | is | |
7678 | AO : constant Entity_Id := Alias (Old_E); | |
7679 | AN : constant Entity_Id := Alias (New_E); | |
7680 | ||
7681 | begin | |
7682 | return Scope (AO) /= Scope (AN) | |
7683 | or else No (DTC_Entity (AO)) | |
7684 | or else No (DTC_Entity (AN)) | |
7685 | or else DT_Position (AO) = DT_Position (AN); | |
7686 | end Is_Overriding_Alias; | |
7687 | ||
996ae0b0 RK |
7688 | -- Start of processing for New_Overloaded_Entity |
7689 | ||
7690 | begin | |
fbf5a39b AC |
7691 | -- We need to look for an entity that S may override. This must be a |
7692 | -- homonym in the current scope, so we look for the first homonym of | |
7693 | -- S in the current scope as the starting point for the search. | |
7694 | ||
7695 | E := Current_Entity_In_Scope (S); | |
7696 | ||
947430d5 AC |
7697 | -- Ada 2005 (AI-251): Derivation of abstract interface primitives. |
7698 | -- They are directly added to the list of primitive operations of | |
7699 | -- Derived_Type, unless this is a rederivation in the private part | |
7700 | -- of an operation that was already derived in the visible part of | |
7701 | -- the current package. | |
7702 | ||
0791fbe9 | 7703 | if Ada_Version >= Ada_2005 |
947430d5 AC |
7704 | and then Present (Derived_Type) |
7705 | and then Present (Alias (S)) | |
7706 | and then Is_Dispatching_Operation (Alias (S)) | |
7707 | and then Present (Find_Dispatching_Type (Alias (S))) | |
7708 | and then Is_Interface (Find_Dispatching_Type (Alias (S))) | |
7709 | then | |
7710 | -- For private types, when the full-view is processed we propagate to | |
7711 | -- the full view the non-overridden entities whose attribute "alias" | |
7712 | -- references an interface primitive. These entities were added by | |
7713 | -- Derive_Subprograms to ensure that interface primitives are | |
7714 | -- covered. | |
7715 | ||
7716 | -- Inside_Freeze_Actions is non zero when S corresponds with an | |
7717 | -- internal entity that links an interface primitive with its | |
7718 | -- covering primitive through attribute Interface_Alias (see | |
7719 | -- Add_Internal_Interface_Entities) | |
7720 | ||
7721 | if Inside_Freezing_Actions = 0 | |
7722 | and then Is_Package_Or_Generic_Package (Current_Scope) | |
7723 | and then In_Private_Part (Current_Scope) | |
7724 | and then Nkind (Parent (E)) = N_Private_Extension_Declaration | |
7725 | and then Nkind (Parent (S)) = N_Full_Type_Declaration | |
7726 | and then Full_View (Defining_Identifier (Parent (E))) | |
7727 | = Defining_Identifier (Parent (S)) | |
7728 | and then Alias (E) = Alias (S) | |
7729 | then | |
7730 | Check_Operation_From_Private_View (S, E); | |
7731 | Set_Is_Dispatching_Operation (S); | |
7732 | ||
7733 | -- Common case | |
7734 | ||
7735 | else | |
7736 | Enter_Overloaded_Entity (S); | |
7737 | Check_Dispatching_Operation (S, Empty); | |
7738 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); | |
7739 | end if; | |
7740 | ||
7741 | return; | |
7742 | end if; | |
7743 | ||
fbf5a39b AC |
7744 | -- If there is no homonym then this is definitely not overriding |
7745 | ||
996ae0b0 RK |
7746 | if No (E) then |
7747 | Enter_Overloaded_Entity (S); | |
7748 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 7749 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
996ae0b0 | 7750 | |
ec4867fa ES |
7751 | -- If subprogram has an explicit declaration, check whether it |
7752 | -- has an overriding indicator. | |
758c442c | 7753 | |
ec4867fa | 7754 | if Comes_From_Source (S) then |
8aa15e3b | 7755 | Check_Synchronized_Overriding (S, Overridden_Subp); |
5d37ba92 ES |
7756 | Check_Overriding_Indicator |
7757 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
758c442c GD |
7758 | end if; |
7759 | ||
fbf5a39b AC |
7760 | -- If there is a homonym that is not overloadable, then we have an |
7761 | -- error, except for the special cases checked explicitly below. | |
7762 | ||
996ae0b0 RK |
7763 | elsif not Is_Overloadable (E) then |
7764 | ||
7765 | -- Check for spurious conflict produced by a subprogram that has the | |
7766 | -- same name as that of the enclosing generic package. The conflict | |
7767 | -- occurs within an instance, between the subprogram and the renaming | |
7768 | -- declaration for the package. After the subprogram, the package | |
7769 | -- renaming declaration becomes hidden. | |
7770 | ||
7771 | if Ekind (E) = E_Package | |
7772 | and then Present (Renamed_Object (E)) | |
7773 | and then Renamed_Object (E) = Current_Scope | |
7774 | and then Nkind (Parent (Renamed_Object (E))) = | |
7775 | N_Package_Specification | |
7776 | and then Present (Generic_Parent (Parent (Renamed_Object (E)))) | |
7777 | then | |
7778 | Set_Is_Hidden (E); | |
7779 | Set_Is_Immediately_Visible (E, False); | |
7780 | Enter_Overloaded_Entity (S); | |
7781 | Set_Homonym (S, Homonym (E)); | |
7782 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 7783 | Check_Overriding_Indicator (S, Empty, Is_Primitive => False); |
996ae0b0 RK |
7784 | |
7785 | -- If the subprogram is implicit it is hidden by the previous | |
82c80734 RD |
7786 | -- declaration. However if it is dispatching, it must appear in the |
7787 | -- dispatch table anyway, because it can be dispatched to even if it | |
7788 | -- cannot be called directly. | |
996ae0b0 RK |
7789 | |
7790 | elsif Present (Alias (S)) | |
7791 | and then not Comes_From_Source (S) | |
7792 | then | |
7793 | Set_Scope (S, Current_Scope); | |
7794 | ||
7795 | if Is_Dispatching_Operation (Alias (S)) then | |
7796 | Check_Dispatching_Operation (S, Empty); | |
7797 | end if; | |
7798 | ||
7799 | return; | |
7800 | ||
7801 | else | |
7802 | Error_Msg_Sloc := Sloc (E); | |
996ae0b0 | 7803 | |
f3d57416 | 7804 | -- Generate message, with useful additional warning if in generic |
996ae0b0 RK |
7805 | |
7806 | if Is_Generic_Unit (E) then | |
5d37ba92 ES |
7807 | Error_Msg_N ("previous generic unit cannot be overloaded", S); |
7808 | Error_Msg_N ("\& conflicts with declaration#", S); | |
7809 | else | |
7810 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
7811 | end if; |
7812 | ||
7813 | return; | |
7814 | end if; | |
7815 | ||
fbf5a39b AC |
7816 | -- E exists and is overloadable |
7817 | ||
996ae0b0 | 7818 | else |
8aa15e3b | 7819 | Check_Synchronized_Overriding (S, Overridden_Subp); |
758c442c | 7820 | |
82c80734 RD |
7821 | -- Loop through E and its homonyms to determine if any of them is |
7822 | -- the candidate for overriding by S. | |
996ae0b0 RK |
7823 | |
7824 | while Present (E) loop | |
fbf5a39b AC |
7825 | |
7826 | -- Definitely not interesting if not in the current scope | |
7827 | ||
996ae0b0 RK |
7828 | if Scope (E) /= Current_Scope then |
7829 | null; | |
7830 | ||
fbf5a39b AC |
7831 | -- Check if we have type conformance |
7832 | ||
ec4867fa | 7833 | elsif Type_Conformant (E, S) then |
c8ef728f | 7834 | |
82c80734 RD |
7835 | -- If the old and new entities have the same profile and one |
7836 | -- is not the body of the other, then this is an error, unless | |
7837 | -- one of them is implicitly declared. | |
996ae0b0 RK |
7838 | |
7839 | -- There are some cases when both can be implicit, for example | |
7840 | -- when both a literal and a function that overrides it are | |
f3d57416 | 7841 | -- inherited in a derivation, or when an inherited operation |
ec4867fa | 7842 | -- of a tagged full type overrides the inherited operation of |
f3d57416 | 7843 | -- a private extension. Ada 83 had a special rule for the |
82c80734 RD |
7844 | -- literal case. In Ada95, the later implicit operation hides |
7845 | -- the former, and the literal is always the former. In the | |
7846 | -- odd case where both are derived operations declared at the | |
7847 | -- same point, both operations should be declared, and in that | |
7848 | -- case we bypass the following test and proceed to the next | |
df46b832 AC |
7849 | -- part. This can only occur for certain obscure cases in |
7850 | -- instances, when an operation on a type derived from a formal | |
7851 | -- private type does not override a homograph inherited from | |
7852 | -- the actual. In subsequent derivations of such a type, the | |
7853 | -- DT positions of these operations remain distinct, if they | |
7854 | -- have been set. | |
996ae0b0 RK |
7855 | |
7856 | if Present (Alias (S)) | |
7857 | and then (No (Alias (E)) | |
7858 | or else Comes_From_Source (E) | |
2ddc2000 | 7859 | or else Is_Abstract_Subprogram (S) |
df46b832 AC |
7860 | or else |
7861 | (Is_Dispatching_Operation (E) | |
2ddc2000 | 7862 | and then Is_Overriding_Alias (E, S))) |
df46b832 | 7863 | and then Ekind (E) /= E_Enumeration_Literal |
996ae0b0 | 7864 | then |
82c80734 RD |
7865 | -- When an derived operation is overloaded it may be due to |
7866 | -- the fact that the full view of a private extension | |
996ae0b0 RK |
7867 | -- re-inherits. It has to be dealt with. |
7868 | ||
e660dbf7 | 7869 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
7870 | and then In_Private_Part (Current_Scope) |
7871 | then | |
7872 | Check_Operation_From_Private_View (S, E); | |
7873 | end if; | |
7874 | ||
7875 | -- In any case the implicit operation remains hidden by | |
5950a3ac | 7876 | -- the existing declaration, which is overriding. |
996ae0b0 | 7877 | |
5950a3ac | 7878 | Set_Is_Overriding_Operation (E); |
758c442c GD |
7879 | |
7880 | if Comes_From_Source (E) then | |
5d37ba92 | 7881 | Check_Overriding_Indicator (E, S, Is_Primitive => False); |
41251c60 JM |
7882 | |
7883 | -- Indicate that E overrides the operation from which | |
7884 | -- S is inherited. | |
7885 | ||
0a36105d | 7886 | if Present (Alias (S)) then |
41251c60 JM |
7887 | Set_Overridden_Operation (E, Alias (S)); |
7888 | else | |
7889 | Set_Overridden_Operation (E, S); | |
7890 | end if; | |
758c442c GD |
7891 | end if; |
7892 | ||
996ae0b0 RK |
7893 | return; |
7894 | ||
26a43556 AC |
7895 | -- Within an instance, the renaming declarations for actual |
7896 | -- subprograms may become ambiguous, but they do not hide each | |
7897 | -- other. | |
996ae0b0 RK |
7898 | |
7899 | elsif Ekind (E) /= E_Entry | |
7900 | and then not Comes_From_Source (E) | |
7901 | and then not Is_Generic_Instance (E) | |
7902 | and then (Present (Alias (E)) | |
7903 | or else Is_Intrinsic_Subprogram (E)) | |
7904 | and then (not In_Instance | |
7905 | or else No (Parent (E)) | |
7906 | or else Nkind (Unit_Declaration_Node (E)) /= | |
8dbd1460 | 7907 | N_Subprogram_Renaming_Declaration) |
996ae0b0 | 7908 | then |
26a43556 AC |
7909 | -- A subprogram child unit is not allowed to override an |
7910 | -- inherited subprogram (10.1.1(20)). | |
996ae0b0 RK |
7911 | |
7912 | if Is_Child_Unit (S) then | |
7913 | Error_Msg_N | |
7914 | ("child unit overrides inherited subprogram in parent", | |
7915 | S); | |
7916 | return; | |
7917 | end if; | |
7918 | ||
7919 | if Is_Non_Overriding_Operation (E, S) then | |
7920 | Enter_Overloaded_Entity (S); | |
8dbd1460 | 7921 | |
c8ef728f | 7922 | if No (Derived_Type) |
996ae0b0 RK |
7923 | or else Is_Tagged_Type (Derived_Type) |
7924 | then | |
7925 | Check_Dispatching_Operation (S, Empty); | |
7926 | end if; | |
7927 | ||
7928 | return; | |
7929 | end if; | |
7930 | ||
7931 | -- E is a derived operation or an internal operator which | |
7932 | -- is being overridden. Remove E from further visibility. | |
7933 | -- Furthermore, if E is a dispatching operation, it must be | |
7934 | -- replaced in the list of primitive operations of its type | |
7935 | -- (see Override_Dispatching_Operation). | |
7936 | ||
ec4867fa | 7937 | Overridden_Subp := E; |
758c442c | 7938 | |
996ae0b0 RK |
7939 | declare |
7940 | Prev : Entity_Id; | |
7941 | ||
7942 | begin | |
7943 | Prev := First_Entity (Current_Scope); | |
996ae0b0 RK |
7944 | while Present (Prev) |
7945 | and then Next_Entity (Prev) /= E | |
7946 | loop | |
7947 | Next_Entity (Prev); | |
7948 | end loop; | |
7949 | ||
7950 | -- It is possible for E to be in the current scope and | |
7951 | -- yet not in the entity chain. This can only occur in a | |
7952 | -- generic context where E is an implicit concatenation | |
7953 | -- in the formal part, because in a generic body the | |
7954 | -- entity chain starts with the formals. | |
7955 | ||
7956 | pragma Assert | |
7957 | (Present (Prev) or else Chars (E) = Name_Op_Concat); | |
7958 | ||
7959 | -- E must be removed both from the entity_list of the | |
7960 | -- current scope, and from the visibility chain | |
7961 | ||
7962 | if Debug_Flag_E then | |
7963 | Write_Str ("Override implicit operation "); | |
7964 | Write_Int (Int (E)); | |
7965 | Write_Eol; | |
7966 | end if; | |
7967 | ||
7968 | -- If E is a predefined concatenation, it stands for four | |
7969 | -- different operations. As a result, a single explicit | |
7970 | -- declaration does not hide it. In a possible ambiguous | |
7971 | -- situation, Disambiguate chooses the user-defined op, | |
7972 | -- so it is correct to retain the previous internal one. | |
7973 | ||
7974 | if Chars (E) /= Name_Op_Concat | |
7975 | or else Ekind (E) /= E_Operator | |
7976 | then | |
7977 | -- For nondispatching derived operations that are | |
7978 | -- overridden by a subprogram declared in the private | |
8dbd1460 AC |
7979 | -- part of a package, we retain the derived subprogram |
7980 | -- but mark it as not immediately visible. If the | |
7981 | -- derived operation was declared in the visible part | |
7982 | -- then this ensures that it will still be visible | |
7983 | -- outside the package with the proper signature | |
7984 | -- (calls from outside must also be directed to this | |
7985 | -- version rather than the overriding one, unlike the | |
7986 | -- dispatching case). Calls from inside the package | |
7987 | -- will still resolve to the overriding subprogram | |
7988 | -- since the derived one is marked as not visible | |
7989 | -- within the package. | |
996ae0b0 RK |
7990 | |
7991 | -- If the private operation is dispatching, we achieve | |
7992 | -- the overriding by keeping the implicit operation | |
9865d858 | 7993 | -- but setting its alias to be the overriding one. In |
996ae0b0 RK |
7994 | -- this fashion the proper body is executed in all |
7995 | -- cases, but the original signature is used outside | |
7996 | -- of the package. | |
7997 | ||
7998 | -- If the overriding is not in the private part, we | |
7999 | -- remove the implicit operation altogether. | |
8000 | ||
8001 | if Is_Private_Declaration (S) then | |
996ae0b0 RK |
8002 | if not Is_Dispatching_Operation (E) then |
8003 | Set_Is_Immediately_Visible (E, False); | |
8004 | else | |
e895b435 ES |
8005 | -- Work done in Override_Dispatching_Operation, |
8006 | -- so nothing else need to be done here. | |
996ae0b0 RK |
8007 | |
8008 | null; | |
8009 | end if; | |
996ae0b0 | 8010 | |
fbf5a39b AC |
8011 | else |
8012 | -- Find predecessor of E in Homonym chain | |
996ae0b0 RK |
8013 | |
8014 | if E = Current_Entity (E) then | |
8015 | Prev_Vis := Empty; | |
8016 | else | |
8017 | Prev_Vis := Current_Entity (E); | |
8018 | while Homonym (Prev_Vis) /= E loop | |
8019 | Prev_Vis := Homonym (Prev_Vis); | |
8020 | end loop; | |
8021 | end if; | |
8022 | ||
8023 | if Prev_Vis /= Empty then | |
8024 | ||
8025 | -- Skip E in the visibility chain | |
8026 | ||
8027 | Set_Homonym (Prev_Vis, Homonym (E)); | |
8028 | ||
8029 | else | |
8030 | Set_Name_Entity_Id (Chars (E), Homonym (E)); | |
8031 | end if; | |
8032 | ||
8033 | Set_Next_Entity (Prev, Next_Entity (E)); | |
8034 | ||
8035 | if No (Next_Entity (Prev)) then | |
8036 | Set_Last_Entity (Current_Scope, Prev); | |
8037 | end if; | |
8038 | ||
8039 | end if; | |
8040 | end if; | |
8041 | ||
8042 | Enter_Overloaded_Entity (S); | |
fbf5a39b | 8043 | Set_Is_Overriding_Operation (S); |
5d37ba92 | 8044 | Check_Overriding_Indicator (S, E, Is_Primitive => True); |
996ae0b0 | 8045 | |
fc53fe76 AC |
8046 | -- If S is a user-defined subprogram or a null procedure |
8047 | -- expanded to override an inherited null procedure, then | |
8048 | -- indicate that E overrides the operation from which S | |
8049 | -- is inherited. It seems odd that Overridden_Operation | |
8050 | -- isn't set in all cases where Is_Overriding_Operation | |
8051 | -- is true, but doing so causes infinite loops in the | |
8052 | -- compiler for implicit overriding subprograms. ??? | |
8053 | ||
8054 | if Comes_From_Source (S) | |
8055 | or else | |
8056 | (Present (Parent (S)) | |
8057 | and then | |
8058 | Nkind (Parent (S)) = N_Procedure_Specification | |
8059 | and then | |
8060 | Null_Present (Parent (S))) | |
8061 | then | |
c8ef728f | 8062 | if Present (Alias (E)) then |
41251c60 JM |
8063 | Set_Overridden_Operation (S, Alias (E)); |
8064 | else | |
8065 | Set_Overridden_Operation (S, E); | |
8066 | end if; | |
8067 | end if; | |
8068 | ||
996ae0b0 | 8069 | if Is_Dispatching_Operation (E) then |
fbf5a39b | 8070 | |
82c80734 RD |
8071 | -- An overriding dispatching subprogram inherits the |
8072 | -- convention of the overridden subprogram (by | |
8073 | -- AI-117). | |
996ae0b0 RK |
8074 | |
8075 | Set_Convention (S, Convention (E)); | |
41251c60 JM |
8076 | Check_Dispatching_Operation (S, E); |
8077 | ||
996ae0b0 RK |
8078 | else |
8079 | Check_Dispatching_Operation (S, Empty); | |
8080 | end if; | |
8081 | ||
5d37ba92 ES |
8082 | Check_For_Primitive_Subprogram |
8083 | (Is_Primitive_Subp, Is_Overriding => True); | |
996ae0b0 RK |
8084 | goto Check_Inequality; |
8085 | end; | |
8086 | ||
8087 | -- Apparent redeclarations in instances can occur when two | |
8088 | -- formal types get the same actual type. The subprograms in | |
8089 | -- in the instance are legal, even if not callable from the | |
8090 | -- outside. Calls from within are disambiguated elsewhere. | |
8091 | -- For dispatching operations in the visible part, the usual | |
8092 | -- rules apply, and operations with the same profile are not | |
8093 | -- legal (B830001). | |
8094 | ||
8095 | elsif (In_Instance_Visible_Part | |
8096 | and then not Is_Dispatching_Operation (E)) | |
8097 | or else In_Instance_Not_Visible | |
8098 | then | |
8099 | null; | |
8100 | ||
8101 | -- Here we have a real error (identical profile) | |
8102 | ||
8103 | else | |
8104 | Error_Msg_Sloc := Sloc (E); | |
8105 | ||
8106 | -- Avoid cascaded errors if the entity appears in | |
8107 | -- subsequent calls. | |
8108 | ||
8109 | Set_Scope (S, Current_Scope); | |
8110 | ||
5d37ba92 ES |
8111 | -- Generate error, with extra useful warning for the case |
8112 | -- of a generic instance with no completion. | |
996ae0b0 RK |
8113 | |
8114 | if Is_Generic_Instance (S) | |
8115 | and then not Has_Completion (E) | |
8116 | then | |
8117 | Error_Msg_N | |
5d37ba92 ES |
8118 | ("instantiation cannot provide body for&", S); |
8119 | Error_Msg_N ("\& conflicts with declaration#", S); | |
8120 | else | |
8121 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
8122 | end if; |
8123 | ||
8124 | return; | |
8125 | end if; | |
8126 | ||
8127 | else | |
c8ef728f ES |
8128 | -- If one subprogram has an access parameter and the other |
8129 | -- a parameter of an access type, calls to either might be | |
8130 | -- ambiguous. Verify that parameters match except for the | |
8131 | -- access parameter. | |
8132 | ||
8133 | if May_Hide_Profile then | |
8134 | declare | |
ec4867fa ES |
8135 | F1 : Entity_Id; |
8136 | F2 : Entity_Id; | |
8dbd1460 | 8137 | |
c8ef728f ES |
8138 | begin |
8139 | F1 := First_Formal (S); | |
8140 | F2 := First_Formal (E); | |
8141 | while Present (F1) and then Present (F2) loop | |
8142 | if Is_Access_Type (Etype (F1)) then | |
8143 | if not Is_Access_Type (Etype (F2)) | |
8144 | or else not Conforming_Types | |
8145 | (Designated_Type (Etype (F1)), | |
8146 | Designated_Type (Etype (F2)), | |
8147 | Type_Conformant) | |
8148 | then | |
8149 | May_Hide_Profile := False; | |
8150 | end if; | |
8151 | ||
8152 | elsif | |
8153 | not Conforming_Types | |
8154 | (Etype (F1), Etype (F2), Type_Conformant) | |
8155 | then | |
8156 | May_Hide_Profile := False; | |
8157 | end if; | |
8158 | ||
8159 | Next_Formal (F1); | |
8160 | Next_Formal (F2); | |
8161 | end loop; | |
8162 | ||
8163 | if May_Hide_Profile | |
8164 | and then No (F1) | |
8165 | and then No (F2) | |
8166 | then | |
8167 | Error_Msg_NE ("calls to& may be ambiguous?", S, S); | |
8168 | end if; | |
8169 | end; | |
8170 | end if; | |
996ae0b0 RK |
8171 | end if; |
8172 | ||
996ae0b0 RK |
8173 | E := Homonym (E); |
8174 | end loop; | |
8175 | ||
8176 | -- On exit, we know that S is a new entity | |
8177 | ||
8178 | Enter_Overloaded_Entity (S); | |
5d37ba92 ES |
8179 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
8180 | Check_Overriding_Indicator | |
8181 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
996ae0b0 | 8182 | |
82c80734 RD |
8183 | -- If S is a derived operation for an untagged type then by |
8184 | -- definition it's not a dispatching operation (even if the parent | |
8185 | -- operation was dispatching), so we don't call | |
8186 | -- Check_Dispatching_Operation in that case. | |
996ae0b0 | 8187 | |
c8ef728f | 8188 | if No (Derived_Type) |
996ae0b0 RK |
8189 | or else Is_Tagged_Type (Derived_Type) |
8190 | then | |
8191 | Check_Dispatching_Operation (S, Empty); | |
8192 | end if; | |
8193 | end if; | |
8194 | ||
82c80734 RD |
8195 | -- If this is a user-defined equality operator that is not a derived |
8196 | -- subprogram, create the corresponding inequality. If the operation is | |
8197 | -- dispatching, the expansion is done elsewhere, and we do not create | |
8198 | -- an explicit inequality operation. | |
996ae0b0 RK |
8199 | |
8200 | <<Check_Inequality>> | |
8201 | if Chars (S) = Name_Op_Eq | |
8202 | and then Etype (S) = Standard_Boolean | |
8203 | and then Present (Parent (S)) | |
8204 | and then not Is_Dispatching_Operation (S) | |
8205 | then | |
8206 | Make_Inequality_Operator (S); | |
d151d6a3 | 8207 | |
dbe945f1 | 8208 | if Ada_Version >= Ada_2012 then |
e5a58fac AC |
8209 | Check_Untagged_Equality (S); |
8210 | end if; | |
996ae0b0 | 8211 | end if; |
996ae0b0 RK |
8212 | end New_Overloaded_Entity; |
8213 | ||
8214 | --------------------- | |
8215 | -- Process_Formals -- | |
8216 | --------------------- | |
8217 | ||
8218 | procedure Process_Formals | |
07fc65c4 | 8219 | (T : List_Id; |
996ae0b0 RK |
8220 | Related_Nod : Node_Id) |
8221 | is | |
8222 | Param_Spec : Node_Id; | |
8223 | Formal : Entity_Id; | |
8224 | Formal_Type : Entity_Id; | |
8225 | Default : Node_Id; | |
8226 | Ptype : Entity_Id; | |
8227 | ||
800621e0 RD |
8228 | Num_Out_Params : Nat := 0; |
8229 | First_Out_Param : Entity_Id := Empty; | |
21d27997 | 8230 | -- Used for setting Is_Only_Out_Parameter |
800621e0 | 8231 | |
950d217a AC |
8232 | function Designates_From_With_Type (Typ : Entity_Id) return Boolean; |
8233 | -- Determine whether an access type designates a type coming from a | |
8234 | -- limited view. | |
8235 | ||
07fc65c4 | 8236 | function Is_Class_Wide_Default (D : Node_Id) return Boolean; |
82c80734 RD |
8237 | -- Check whether the default has a class-wide type. After analysis the |
8238 | -- default has the type of the formal, so we must also check explicitly | |
8239 | -- for an access attribute. | |
07fc65c4 | 8240 | |
950d217a AC |
8241 | ------------------------------- |
8242 | -- Designates_From_With_Type -- | |
8243 | ------------------------------- | |
8244 | ||
8245 | function Designates_From_With_Type (Typ : Entity_Id) return Boolean is | |
8246 | Desig : Entity_Id := Typ; | |
8247 | ||
8248 | begin | |
8249 | if Is_Access_Type (Desig) then | |
8250 | Desig := Directly_Designated_Type (Desig); | |
8251 | end if; | |
8252 | ||
8253 | if Is_Class_Wide_Type (Desig) then | |
8254 | Desig := Root_Type (Desig); | |
8255 | end if; | |
8256 | ||
8257 | return | |
8258 | Ekind (Desig) = E_Incomplete_Type | |
8259 | and then From_With_Type (Desig); | |
8260 | end Designates_From_With_Type; | |
8261 | ||
07fc65c4 GB |
8262 | --------------------------- |
8263 | -- Is_Class_Wide_Default -- | |
8264 | --------------------------- | |
8265 | ||
8266 | function Is_Class_Wide_Default (D : Node_Id) return Boolean is | |
8267 | begin | |
8268 | return Is_Class_Wide_Type (Designated_Type (Etype (D))) | |
8269 | or else (Nkind (D) = N_Attribute_Reference | |
8270 | and then Attribute_Name (D) = Name_Access | |
8271 | and then Is_Class_Wide_Type (Etype (Prefix (D)))); | |
8272 | end Is_Class_Wide_Default; | |
8273 | ||
8274 | -- Start of processing for Process_Formals | |
8275 | ||
996ae0b0 RK |
8276 | begin |
8277 | -- In order to prevent premature use of the formals in the same formal | |
8278 | -- part, the Ekind is left undefined until all default expressions are | |
8279 | -- analyzed. The Ekind is established in a separate loop at the end. | |
8280 | ||
8281 | Param_Spec := First (T); | |
996ae0b0 | 8282 | while Present (Param_Spec) loop |
996ae0b0 | 8283 | Formal := Defining_Identifier (Param_Spec); |
5d37ba92 | 8284 | Set_Never_Set_In_Source (Formal, True); |
996ae0b0 RK |
8285 | Enter_Name (Formal); |
8286 | ||
8287 | -- Case of ordinary parameters | |
8288 | ||
8289 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
8290 | Find_Type (Parameter_Type (Param_Spec)); | |
8291 | Ptype := Parameter_Type (Param_Spec); | |
8292 | ||
8293 | if Ptype = Error then | |
8294 | goto Continue; | |
8295 | end if; | |
8296 | ||
8297 | Formal_Type := Entity (Ptype); | |
8298 | ||
ec4867fa ES |
8299 | if Is_Incomplete_Type (Formal_Type) |
8300 | or else | |
8301 | (Is_Class_Wide_Type (Formal_Type) | |
8302 | and then Is_Incomplete_Type (Root_Type (Formal_Type))) | |
996ae0b0 | 8303 | then |
93bcda23 AC |
8304 | -- Ada 2005 (AI-326): Tagged incomplete types allowed in |
8305 | -- primitive operations, as long as their completion is | |
8306 | -- in the same declarative part. If in the private part | |
8307 | -- this means that the type cannot be a Taft-amendment type. | |
cec29135 ES |
8308 | -- Check is done on package exit. For access to subprograms, |
8309 | -- the use is legal for Taft-amendment types. | |
fbf5a39b | 8310 | |
d8db0bca | 8311 | if Is_Tagged_Type (Formal_Type) then |
93bcda23 AC |
8312 | if Ekind (Scope (Current_Scope)) = E_Package |
8313 | and then In_Private_Part (Scope (Current_Scope)) | |
8314 | and then not From_With_Type (Formal_Type) | |
8315 | and then not Is_Class_Wide_Type (Formal_Type) | |
8316 | then | |
cec29135 ES |
8317 | if not Nkind_In |
8318 | (Parent (T), N_Access_Function_Definition, | |
8319 | N_Access_Procedure_Definition) | |
8320 | then | |
8321 | Append_Elmt | |
8322 | (Current_Scope, | |
8323 | Private_Dependents (Base_Type (Formal_Type))); | |
8324 | end if; | |
93bcda23 | 8325 | end if; |
fbf5a39b | 8326 | |
0a36105d JM |
8327 | -- Special handling of Value_Type for CIL case |
8328 | ||
8329 | elsif Is_Value_Type (Formal_Type) then | |
8330 | null; | |
8331 | ||
800621e0 RD |
8332 | elsif not Nkind_In (Parent (T), N_Access_Function_Definition, |
8333 | N_Access_Procedure_Definition) | |
996ae0b0 | 8334 | then |
0a36105d | 8335 | |
dd386db0 AC |
8336 | -- AI05-0151: Tagged incomplete types are allowed in all |
8337 | -- formal parts. Untagged incomplete types are not allowed | |
8338 | -- in bodies. | |
8339 | ||
8340 | if Ada_Version >= Ada_2012 then | |
8341 | if Is_Tagged_Type (Formal_Type) then | |
8342 | null; | |
8343 | ||
0f1a6a0b AC |
8344 | elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement, |
8345 | N_Entry_Body, | |
8346 | N_Subprogram_Body) | |
dd386db0 AC |
8347 | then |
8348 | Error_Msg_NE | |
8349 | ("invalid use of untagged incomplete type&", | |
0f1a6a0b | 8350 | Ptype, Formal_Type); |
dd386db0 AC |
8351 | end if; |
8352 | ||
8353 | else | |
8354 | Error_Msg_NE | |
8355 | ("invalid use of incomplete type&", | |
0f1a6a0b | 8356 | Param_Spec, Formal_Type); |
dd386db0 AC |
8357 | |
8358 | -- Further checks on the legality of incomplete types | |
8359 | -- in formal parts are delayed until the freeze point | |
8360 | -- of the enclosing subprogram or access to subprogram. | |
8361 | end if; | |
996ae0b0 RK |
8362 | end if; |
8363 | ||
8364 | elsif Ekind (Formal_Type) = E_Void then | |
0f1a6a0b AC |
8365 | Error_Msg_NE |
8366 | ("premature use of&", | |
8367 | Parameter_Type (Param_Spec), Formal_Type); | |
996ae0b0 RK |
8368 | end if; |
8369 | ||
0ab80019 | 8370 | -- Ada 2005 (AI-231): Create and decorate an internal subtype |
7324bf49 | 8371 | -- declaration corresponding to the null-excluding type of the |
d8db0bca JM |
8372 | -- formal in the enclosing scope. Finally, replace the parameter |
8373 | -- type of the formal with the internal subtype. | |
7324bf49 | 8374 | |
0791fbe9 | 8375 | if Ada_Version >= Ada_2005 |
41251c60 | 8376 | and then Null_Exclusion_Present (Param_Spec) |
7324bf49 | 8377 | then |
ec4867fa | 8378 | if not Is_Access_Type (Formal_Type) then |
ed2233dc | 8379 | Error_Msg_N |
0a36105d JM |
8380 | ("`NOT NULL` allowed only for an access type", Param_Spec); |
8381 | ||
ec4867fa ES |
8382 | else |
8383 | if Can_Never_Be_Null (Formal_Type) | |
8384 | and then Comes_From_Source (Related_Nod) | |
8385 | then | |
ed2233dc | 8386 | Error_Msg_NE |
0a36105d | 8387 | ("`NOT NULL` not allowed (& already excludes null)", |
0f1a6a0b | 8388 | Param_Spec, Formal_Type); |
ec4867fa | 8389 | end if; |
41251c60 | 8390 | |
ec4867fa ES |
8391 | Formal_Type := |
8392 | Create_Null_Excluding_Itype | |
8393 | (T => Formal_Type, | |
8394 | Related_Nod => Related_Nod, | |
8395 | Scope_Id => Scope (Current_Scope)); | |
0a36105d JM |
8396 | |
8397 | -- If the designated type of the itype is an itype we | |
8398 | -- decorate it with the Has_Delayed_Freeze attribute to | |
8399 | -- avoid problems with the backend. | |
8400 | ||
8401 | -- Example: | |
8402 | -- type T is access procedure; | |
8403 | -- procedure Op (O : not null T); | |
8404 | ||
8405 | if Is_Itype (Directly_Designated_Type (Formal_Type)) then | |
8406 | Set_Has_Delayed_Freeze (Formal_Type); | |
8407 | end if; | |
ec4867fa | 8408 | end if; |
7324bf49 AC |
8409 | end if; |
8410 | ||
996ae0b0 RK |
8411 | -- An access formal type |
8412 | ||
8413 | else | |
8414 | Formal_Type := | |
8415 | Access_Definition (Related_Nod, Parameter_Type (Param_Spec)); | |
7324bf49 | 8416 | |
f937473f RD |
8417 | -- No need to continue if we already notified errors |
8418 | ||
8419 | if not Present (Formal_Type) then | |
8420 | return; | |
8421 | end if; | |
8422 | ||
0ab80019 | 8423 | -- Ada 2005 (AI-254) |
7324bf49 | 8424 | |
af4b9434 AC |
8425 | declare |
8426 | AD : constant Node_Id := | |
8427 | Access_To_Subprogram_Definition | |
8428 | (Parameter_Type (Param_Spec)); | |
8429 | begin | |
8430 | if Present (AD) and then Protected_Present (AD) then | |
8431 | Formal_Type := | |
8432 | Replace_Anonymous_Access_To_Protected_Subprogram | |
f937473f | 8433 | (Param_Spec); |
af4b9434 AC |
8434 | end if; |
8435 | end; | |
996ae0b0 RK |
8436 | end if; |
8437 | ||
8438 | Set_Etype (Formal, Formal_Type); | |
fbf5a39b | 8439 | Default := Expression (Param_Spec); |
996ae0b0 RK |
8440 | |
8441 | if Present (Default) then | |
8442 | if Out_Present (Param_Spec) then | |
ed2233dc | 8443 | Error_Msg_N |
996ae0b0 RK |
8444 | ("default initialization only allowed for IN parameters", |
8445 | Param_Spec); | |
8446 | end if; | |
8447 | ||
8448 | -- Do the special preanalysis of the expression (see section on | |
8449 | -- "Handling of Default Expressions" in the spec of package Sem). | |
8450 | ||
21d27997 | 8451 | Preanalyze_Spec_Expression (Default, Formal_Type); |
996ae0b0 | 8452 | |
f29b857f ES |
8453 | -- An access to constant cannot be the default for |
8454 | -- an access parameter that is an access to variable. | |
2eb160f2 ST |
8455 | |
8456 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
8457 | and then not Is_Access_Constant (Formal_Type) | |
8458 | and then Is_Access_Type (Etype (Default)) | |
8459 | and then Is_Access_Constant (Etype (Default)) | |
8460 | then | |
f29b857f ES |
8461 | Error_Msg_N |
8462 | ("formal that is access to variable cannot be initialized " & | |
8463 | "with an access-to-constant expression", Default); | |
2eb160f2 ST |
8464 | end if; |
8465 | ||
d8db0bca JM |
8466 | -- Check that the designated type of an access parameter's default |
8467 | -- is not a class-wide type unless the parameter's designated type | |
8468 | -- is also class-wide. | |
996ae0b0 RK |
8469 | |
8470 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
950d217a | 8471 | and then not Designates_From_With_Type (Formal_Type) |
07fc65c4 | 8472 | and then Is_Class_Wide_Default (Default) |
996ae0b0 RK |
8473 | and then not Is_Class_Wide_Type (Designated_Type (Formal_Type)) |
8474 | then | |
07fc65c4 GB |
8475 | Error_Msg_N |
8476 | ("access to class-wide expression not allowed here", Default); | |
996ae0b0 | 8477 | end if; |
4755cce9 JM |
8478 | |
8479 | -- Check incorrect use of dynamically tagged expressions | |
8480 | ||
8481 | if Is_Tagged_Type (Formal_Type) then | |
8482 | Check_Dynamically_Tagged_Expression | |
8483 | (Expr => Default, | |
8484 | Typ => Formal_Type, | |
8485 | Related_Nod => Default); | |
8486 | end if; | |
996ae0b0 RK |
8487 | end if; |
8488 | ||
41251c60 JM |
8489 | -- Ada 2005 (AI-231): Static checks |
8490 | ||
0791fbe9 | 8491 | if Ada_Version >= Ada_2005 |
41251c60 JM |
8492 | and then Is_Access_Type (Etype (Formal)) |
8493 | and then Can_Never_Be_Null (Etype (Formal)) | |
8494 | then | |
8495 | Null_Exclusion_Static_Checks (Param_Spec); | |
8496 | end if; | |
8497 | ||
996ae0b0 RK |
8498 | <<Continue>> |
8499 | Next (Param_Spec); | |
8500 | end loop; | |
8501 | ||
82c80734 RD |
8502 | -- If this is the formal part of a function specification, analyze the |
8503 | -- subtype mark in the context where the formals are visible but not | |
8504 | -- yet usable, and may hide outer homographs. | |
8505 | ||
8506 | if Nkind (Related_Nod) = N_Function_Specification then | |
8507 | Analyze_Return_Type (Related_Nod); | |
8508 | end if; | |
8509 | ||
996ae0b0 RK |
8510 | -- Now set the kind (mode) of each formal |
8511 | ||
8512 | Param_Spec := First (T); | |
8513 | ||
8514 | while Present (Param_Spec) loop | |
8515 | Formal := Defining_Identifier (Param_Spec); | |
8516 | Set_Formal_Mode (Formal); | |
8517 | ||
8518 | if Ekind (Formal) = E_In_Parameter then | |
8519 | Set_Default_Value (Formal, Expression (Param_Spec)); | |
8520 | ||
8521 | if Present (Expression (Param_Spec)) then | |
8522 | Default := Expression (Param_Spec); | |
8523 | ||
8524 | if Is_Scalar_Type (Etype (Default)) then | |
8525 | if Nkind | |
8526 | (Parameter_Type (Param_Spec)) /= N_Access_Definition | |
8527 | then | |
8528 | Formal_Type := Entity (Parameter_Type (Param_Spec)); | |
8529 | ||
8530 | else | |
8531 | Formal_Type := Access_Definition | |
8532 | (Related_Nod, Parameter_Type (Param_Spec)); | |
8533 | end if; | |
8534 | ||
8535 | Apply_Scalar_Range_Check (Default, Formal_Type); | |
8536 | end if; | |
2820d220 | 8537 | end if; |
800621e0 RD |
8538 | |
8539 | elsif Ekind (Formal) = E_Out_Parameter then | |
8540 | Num_Out_Params := Num_Out_Params + 1; | |
8541 | ||
8542 | if Num_Out_Params = 1 then | |
8543 | First_Out_Param := Formal; | |
8544 | end if; | |
8545 | ||
8546 | elsif Ekind (Formal) = E_In_Out_Parameter then | |
8547 | Num_Out_Params := Num_Out_Params + 1; | |
996ae0b0 RK |
8548 | end if; |
8549 | ||
8550 | Next (Param_Spec); | |
8551 | end loop; | |
800621e0 RD |
8552 | |
8553 | if Present (First_Out_Param) and then Num_Out_Params = 1 then | |
8554 | Set_Is_Only_Out_Parameter (First_Out_Param); | |
8555 | end if; | |
996ae0b0 RK |
8556 | end Process_Formals; |
8557 | ||
21d27997 RD |
8558 | ------------------ |
8559 | -- Process_PPCs -- | |
8560 | ------------------ | |
8561 | ||
8562 | procedure Process_PPCs | |
8563 | (N : Node_Id; | |
8564 | Spec_Id : Entity_Id; | |
8565 | Body_Id : Entity_Id) | |
8566 | is | |
8567 | Loc : constant Source_Ptr := Sloc (N); | |
8568 | Prag : Node_Id; | |
8569 | Plist : List_Id := No_List; | |
8570 | Subp : Entity_Id; | |
8571 | Parms : List_Id; | |
8572 | ||
8573 | function Grab_PPC (Nam : Name_Id) return Node_Id; | |
8574 | -- Prag contains an analyzed precondition or postcondition pragma. | |
8575 | -- This function copies the pragma, changes it to the corresponding | |
8576 | -- Check pragma and returns the Check pragma as the result. The | |
8577 | -- argument Nam is either Name_Precondition or Name_Postcondition. | |
8578 | ||
8579 | -------------- | |
8580 | -- Grab_PPC -- | |
8581 | -------------- | |
8582 | ||
8583 | function Grab_PPC (Nam : Name_Id) return Node_Id is | |
8584 | CP : constant Node_Id := New_Copy_Tree (Prag); | |
8585 | ||
8586 | begin | |
8587 | -- Set Analyzed to false, since we want to reanalyze the check | |
8588 | -- procedure. Note that it is only at the outer level that we | |
8589 | -- do this fiddling, for the spec cases, the already preanalyzed | |
8590 | -- parameters are not affected. | |
766d7add | 8591 | |
0dabde3a ES |
8592 | -- For a postcondition pragma within a generic, preserve the pragma |
8593 | -- for later expansion. | |
21d27997 RD |
8594 | |
8595 | Set_Analyzed (CP, False); | |
8596 | ||
0dabde3a ES |
8597 | if Nam = Name_Postcondition |
8598 | and then not Expander_Active | |
8599 | then | |
8600 | return CP; | |
8601 | end if; | |
8602 | ||
21d27997 RD |
8603 | -- Change pragma into corresponding pragma Check |
8604 | ||
8605 | Prepend_To (Pragma_Argument_Associations (CP), | |
8606 | Make_Pragma_Argument_Association (Sloc (Prag), | |
8607 | Expression => | |
8608 | Make_Identifier (Loc, | |
8609 | Chars => Nam))); | |
8610 | Set_Pragma_Identifier (CP, | |
8611 | Make_Identifier (Sloc (Prag), | |
8612 | Chars => Name_Check)); | |
8613 | ||
8614 | return CP; | |
8615 | end Grab_PPC; | |
8616 | ||
8617 | -- Start of processing for Process_PPCs | |
8618 | ||
8619 | begin | |
b4311214 RD |
8620 | -- Nothing to do if we are not generating code |
8621 | ||
8622 | if Operating_Mode /= Generate_Code then | |
8623 | return; | |
8624 | end if; | |
8625 | ||
21d27997 RD |
8626 | -- Grab preconditions from spec |
8627 | ||
8628 | if Present (Spec_Id) then | |
8629 | ||
8630 | -- Loop through PPC pragmas from spec. Note that preconditions from | |
8631 | -- the body will be analyzed and converted when we scan the body | |
8632 | -- declarations below. | |
8633 | ||
8634 | Prag := Spec_PPC_List (Spec_Id); | |
8635 | while Present (Prag) loop | |
8636 | if Pragma_Name (Prag) = Name_Precondition | |
65564d08 | 8637 | and then Pragma_Enabled (Prag) |
21d27997 RD |
8638 | then |
8639 | -- Add pragma Check at the start of the declarations of N. | |
8640 | -- Note that this processing reverses the order of the list, | |
8641 | -- which is what we want since new entries were chained to | |
8642 | -- the head of the list. | |
8643 | ||
8644 | Prepend (Grab_PPC (Name_Precondition), Declarations (N)); | |
8645 | end if; | |
8646 | ||
8647 | Prag := Next_Pragma (Prag); | |
8648 | end loop; | |
8649 | end if; | |
8650 | ||
8651 | -- Build postconditions procedure if needed and prepend the following | |
8652 | -- declaration to the start of the declarations for the subprogram. | |
8653 | ||
8654 | -- procedure _postconditions [(_Result : resulttype)] is | |
8655 | -- begin | |
8656 | -- pragma Check (Postcondition, condition [,message]); | |
8657 | -- pragma Check (Postcondition, condition [,message]); | |
8658 | -- ... | |
8659 | -- end; | |
8660 | ||
8661 | -- First we deal with the postconditions in the body | |
8662 | ||
8663 | if Is_Non_Empty_List (Declarations (N)) then | |
8664 | ||
8665 | -- Loop through declarations | |
8666 | ||
8667 | Prag := First (Declarations (N)); | |
8668 | while Present (Prag) loop | |
8669 | if Nkind (Prag) = N_Pragma then | |
8670 | ||
8671 | -- If pragma, capture if enabled postcondition, else ignore | |
8672 | ||
8673 | if Pragma_Name (Prag) = Name_Postcondition | |
8674 | and then Check_Enabled (Name_Postcondition) | |
8675 | then | |
8676 | if Plist = No_List then | |
8677 | Plist := Empty_List; | |
8678 | end if; | |
8679 | ||
8680 | Analyze (Prag); | |
0dabde3a ES |
8681 | |
8682 | -- If expansion is disabled, as in a generic unit, | |
8683 | -- save pragma for later expansion. | |
8684 | ||
8685 | if not Expander_Active then | |
8686 | Prepend (Grab_PPC (Name_Postcondition), Declarations (N)); | |
8687 | else | |
8688 | Append (Grab_PPC (Name_Postcondition), Plist); | |
8689 | end if; | |
21d27997 RD |
8690 | end if; |
8691 | ||
8692 | Next (Prag); | |
8693 | ||
043ce308 | 8694 | -- Not a pragma, if comes from source, then end scan |
21d27997 RD |
8695 | |
8696 | elsif Comes_From_Source (Prag) then | |
8697 | exit; | |
8698 | ||
043ce308 | 8699 | -- Skip stuff not coming from source |
21d27997 RD |
8700 | |
8701 | else | |
8702 | Next (Prag); | |
8703 | end if; | |
8704 | end loop; | |
8705 | end if; | |
8706 | ||
8707 | -- Now deal with any postconditions from the spec | |
8708 | ||
8709 | if Present (Spec_Id) then | |
8710 | ||
8711 | -- Loop through PPC pragmas from spec | |
8712 | ||
8713 | Prag := Spec_PPC_List (Spec_Id); | |
8714 | while Present (Prag) loop | |
8715 | if Pragma_Name (Prag) = Name_Postcondition | |
65564d08 | 8716 | and then Pragma_Enabled (Prag) |
21d27997 RD |
8717 | then |
8718 | if Plist = No_List then | |
8719 | Plist := Empty_List; | |
8720 | end if; | |
8721 | ||
0dabde3a ES |
8722 | if not Expander_Active then |
8723 | Prepend (Grab_PPC (Name_Postcondition), Declarations (N)); | |
8724 | else | |
8725 | Append (Grab_PPC (Name_Postcondition), Plist); | |
8726 | end if; | |
21d27997 RD |
8727 | end if; |
8728 | ||
8729 | Prag := Next_Pragma (Prag); | |
8730 | end loop; | |
8731 | end if; | |
8732 | ||
766d7add | 8733 | -- If we had any postconditions and expansion is enabled, build |
043ce308 | 8734 | -- the _Postconditions procedure. |
21d27997 | 8735 | |
0dabde3a ES |
8736 | if Present (Plist) |
8737 | and then Expander_Active | |
8738 | then | |
21d27997 RD |
8739 | Subp := Defining_Entity (N); |
8740 | ||
8741 | if Etype (Subp) /= Standard_Void_Type then | |
8742 | Parms := New_List ( | |
8743 | Make_Parameter_Specification (Loc, | |
8744 | Defining_Identifier => | |
8745 | Make_Defining_Identifier (Loc, | |
8746 | Chars => Name_uResult), | |
8747 | Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc))); | |
8748 | else | |
8749 | Parms := No_List; | |
8750 | end if; | |
8751 | ||
043ce308 AC |
8752 | declare |
8753 | Post_Proc : constant Entity_Id := | |
21d27997 | 8754 | Make_Defining_Identifier (Loc, |
043ce308 AC |
8755 | Chars => Name_uPostconditions); |
8756 | -- The entity for the _Postconditions procedure | |
043ce308 | 8757 | begin |
043ce308 AC |
8758 | Prepend_To (Declarations (N), |
8759 | Make_Subprogram_Body (Loc, | |
8760 | Specification => | |
8761 | Make_Procedure_Specification (Loc, | |
8762 | Defining_Unit_Name => Post_Proc, | |
8763 | Parameter_Specifications => Parms), | |
8764 | ||
8765 | Declarations => Empty_List, | |
8766 | ||
8767 | Handled_Statement_Sequence => | |
8768 | Make_Handled_Sequence_Of_Statements (Loc, | |
8769 | Statements => Plist))); | |
21d27997 | 8770 | |
3bb3f6d6 AC |
8771 | -- If this is a procedure, set the Postcondition_Proc attribute on |
8772 | -- the proper defining entity for the subprogram. | |
21d27997 | 8773 | |
043ce308 | 8774 | if Etype (Subp) = Standard_Void_Type then |
3bb3f6d6 AC |
8775 | if Present (Spec_Id) then |
8776 | Set_Postcondition_Proc (Spec_Id, Post_Proc); | |
8777 | else | |
8778 | Set_Postcondition_Proc (Body_Id, Post_Proc); | |
8779 | end if; | |
043ce308 AC |
8780 | end if; |
8781 | end; | |
21d27997 RD |
8782 | |
8783 | if Present (Spec_Id) then | |
8784 | Set_Has_Postconditions (Spec_Id); | |
8785 | else | |
8786 | Set_Has_Postconditions (Body_Id); | |
8787 | end if; | |
8788 | end if; | |
8789 | end Process_PPCs; | |
8790 | ||
fbf5a39b AC |
8791 | ---------------------------- |
8792 | -- Reference_Body_Formals -- | |
8793 | ---------------------------- | |
8794 | ||
8795 | procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is | |
8796 | Fs : Entity_Id; | |
8797 | Fb : Entity_Id; | |
8798 | ||
8799 | begin | |
8800 | if Error_Posted (Spec) then | |
8801 | return; | |
8802 | end if; | |
8803 | ||
0a36105d JM |
8804 | -- Iterate over both lists. They may be of different lengths if the two |
8805 | -- specs are not conformant. | |
8806 | ||
fbf5a39b AC |
8807 | Fs := First_Formal (Spec); |
8808 | Fb := First_Formal (Bod); | |
0a36105d | 8809 | while Present (Fs) and then Present (Fb) loop |
fbf5a39b AC |
8810 | Generate_Reference (Fs, Fb, 'b'); |
8811 | ||
8812 | if Style_Check then | |
8813 | Style.Check_Identifier (Fb, Fs); | |
8814 | end if; | |
8815 | ||
8816 | Set_Spec_Entity (Fb, Fs); | |
8817 | Set_Referenced (Fs, False); | |
8818 | Next_Formal (Fs); | |
8819 | Next_Formal (Fb); | |
8820 | end loop; | |
8821 | end Reference_Body_Formals; | |
8822 | ||
996ae0b0 RK |
8823 | ------------------------- |
8824 | -- Set_Actual_Subtypes -- | |
8825 | ------------------------- | |
8826 | ||
8827 | procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is | |
2820d220 AC |
8828 | Loc : constant Source_Ptr := Sloc (N); |
8829 | Decl : Node_Id; | |
8830 | Formal : Entity_Id; | |
8831 | T : Entity_Id; | |
8832 | First_Stmt : Node_Id := Empty; | |
8833 | AS_Needed : Boolean; | |
996ae0b0 RK |
8834 | |
8835 | begin | |
f3d57416 | 8836 | -- If this is an empty initialization procedure, no need to create |
fbf5a39b AC |
8837 | -- actual subtypes (small optimization). |
8838 | ||
8839 | if Ekind (Subp) = E_Procedure | |
8840 | and then Is_Null_Init_Proc (Subp) | |
8841 | then | |
8842 | return; | |
8843 | end if; | |
8844 | ||
996ae0b0 RK |
8845 | Formal := First_Formal (Subp); |
8846 | while Present (Formal) loop | |
8847 | T := Etype (Formal); | |
8848 | ||
e895b435 | 8849 | -- We never need an actual subtype for a constrained formal |
996ae0b0 RK |
8850 | |
8851 | if Is_Constrained (T) then | |
8852 | AS_Needed := False; | |
8853 | ||
82c80734 RD |
8854 | -- If we have unknown discriminants, then we do not need an actual |
8855 | -- subtype, or more accurately we cannot figure it out! Note that | |
8856 | -- all class-wide types have unknown discriminants. | |
996ae0b0 RK |
8857 | |
8858 | elsif Has_Unknown_Discriminants (T) then | |
8859 | AS_Needed := False; | |
8860 | ||
82c80734 RD |
8861 | -- At this stage we have an unconstrained type that may need an |
8862 | -- actual subtype. For sure the actual subtype is needed if we have | |
8863 | -- an unconstrained array type. | |
996ae0b0 RK |
8864 | |
8865 | elsif Is_Array_Type (T) then | |
8866 | AS_Needed := True; | |
8867 | ||
d8db0bca JM |
8868 | -- The only other case needing an actual subtype is an unconstrained |
8869 | -- record type which is an IN parameter (we cannot generate actual | |
8870 | -- subtypes for the OUT or IN OUT case, since an assignment can | |
8871 | -- change the discriminant values. However we exclude the case of | |
8872 | -- initialization procedures, since discriminants are handled very | |
8873 | -- specially in this context, see the section entitled "Handling of | |
8874 | -- Discriminants" in Einfo. | |
8875 | ||
8876 | -- We also exclude the case of Discrim_SO_Functions (functions used | |
8877 | -- in front end layout mode for size/offset values), since in such | |
8878 | -- functions only discriminants are referenced, and not only are such | |
8879 | -- subtypes not needed, but they cannot always be generated, because | |
8880 | -- of order of elaboration issues. | |
996ae0b0 RK |
8881 | |
8882 | elsif Is_Record_Type (T) | |
8883 | and then Ekind (Formal) = E_In_Parameter | |
8884 | and then Chars (Formal) /= Name_uInit | |
5d09245e | 8885 | and then not Is_Unchecked_Union (T) |
996ae0b0 RK |
8886 | and then not Is_Discrim_SO_Function (Subp) |
8887 | then | |
8888 | AS_Needed := True; | |
8889 | ||
8890 | -- All other cases do not need an actual subtype | |
8891 | ||
8892 | else | |
8893 | AS_Needed := False; | |
8894 | end if; | |
8895 | ||
8896 | -- Generate actual subtypes for unconstrained arrays and | |
8897 | -- unconstrained discriminated records. | |
8898 | ||
8899 | if AS_Needed then | |
7324bf49 | 8900 | if Nkind (N) = N_Accept_Statement then |
fbf5a39b AC |
8901 | |
8902 | -- If expansion is active, The formal is replaced by a local | |
8903 | -- variable that renames the corresponding entry of the | |
8904 | -- parameter block, and it is this local variable that may | |
8905 | -- require an actual subtype. | |
8906 | ||
8907 | if Expander_Active then | |
8908 | Decl := Build_Actual_Subtype (T, Renamed_Object (Formal)); | |
8909 | else | |
8910 | Decl := Build_Actual_Subtype (T, Formal); | |
8911 | end if; | |
8912 | ||
996ae0b0 RK |
8913 | if Present (Handled_Statement_Sequence (N)) then |
8914 | First_Stmt := | |
8915 | First (Statements (Handled_Statement_Sequence (N))); | |
8916 | Prepend (Decl, Statements (Handled_Statement_Sequence (N))); | |
8917 | Mark_Rewrite_Insertion (Decl); | |
8918 | else | |
82c80734 RD |
8919 | -- If the accept statement has no body, there will be no |
8920 | -- reference to the actuals, so no need to compute actual | |
8921 | -- subtypes. | |
996ae0b0 RK |
8922 | |
8923 | return; | |
8924 | end if; | |
8925 | ||
8926 | else | |
fbf5a39b | 8927 | Decl := Build_Actual_Subtype (T, Formal); |
996ae0b0 RK |
8928 | Prepend (Decl, Declarations (N)); |
8929 | Mark_Rewrite_Insertion (Decl); | |
8930 | end if; | |
8931 | ||
82c80734 RD |
8932 | -- The declaration uses the bounds of an existing object, and |
8933 | -- therefore needs no constraint checks. | |
2820d220 | 8934 | |
7324bf49 | 8935 | Analyze (Decl, Suppress => All_Checks); |
2820d220 | 8936 | |
996ae0b0 RK |
8937 | -- We need to freeze manually the generated type when it is |
8938 | -- inserted anywhere else than in a declarative part. | |
8939 | ||
8940 | if Present (First_Stmt) then | |
8941 | Insert_List_Before_And_Analyze (First_Stmt, | |
8942 | Freeze_Entity (Defining_Identifier (Decl), Loc)); | |
8943 | end if; | |
8944 | ||
fbf5a39b AC |
8945 | if Nkind (N) = N_Accept_Statement |
8946 | and then Expander_Active | |
8947 | then | |
8948 | Set_Actual_Subtype (Renamed_Object (Formal), | |
8949 | Defining_Identifier (Decl)); | |
8950 | else | |
8951 | Set_Actual_Subtype (Formal, Defining_Identifier (Decl)); | |
8952 | end if; | |
996ae0b0 RK |
8953 | end if; |
8954 | ||
8955 | Next_Formal (Formal); | |
8956 | end loop; | |
8957 | end Set_Actual_Subtypes; | |
8958 | ||
8959 | --------------------- | |
8960 | -- Set_Formal_Mode -- | |
8961 | --------------------- | |
8962 | ||
8963 | procedure Set_Formal_Mode (Formal_Id : Entity_Id) is | |
8964 | Spec : constant Node_Id := Parent (Formal_Id); | |
8965 | ||
8966 | begin | |
8967 | -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters | |
8968 | -- since we ensure that corresponding actuals are always valid at the | |
8969 | -- point of the call. | |
8970 | ||
8971 | if Out_Present (Spec) then | |
996ae0b0 RK |
8972 | if Ekind (Scope (Formal_Id)) = E_Function |
8973 | or else Ekind (Scope (Formal_Id)) = E_Generic_Function | |
8974 | then | |
8975 | Error_Msg_N ("functions can only have IN parameters", Spec); | |
8976 | Set_Ekind (Formal_Id, E_In_Parameter); | |
8977 | ||
8978 | elsif In_Present (Spec) then | |
8979 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
8980 | ||
8981 | else | |
fbf5a39b AC |
8982 | Set_Ekind (Formal_Id, E_Out_Parameter); |
8983 | Set_Never_Set_In_Source (Formal_Id, True); | |
8984 | Set_Is_True_Constant (Formal_Id, False); | |
8985 | Set_Current_Value (Formal_Id, Empty); | |
996ae0b0 RK |
8986 | end if; |
8987 | ||
8988 | else | |
8989 | Set_Ekind (Formal_Id, E_In_Parameter); | |
8990 | end if; | |
8991 | ||
fbf5a39b | 8992 | -- Set Is_Known_Non_Null for access parameters since the language |
82c80734 RD |
8993 | -- guarantees that access parameters are always non-null. We also set |
8994 | -- Can_Never_Be_Null, since there is no way to change the value. | |
fbf5a39b AC |
8995 | |
8996 | if Nkind (Parameter_Type (Spec)) = N_Access_Definition then | |
2820d220 | 8997 | |
2813bb6b ES |
8998 | -- Ada 2005 (AI-231): In Ada95, access parameters are always non- |
8999 | -- null; In Ada 2005, only if then null_exclusion is explicit. | |
2820d220 | 9000 | |
0791fbe9 | 9001 | if Ada_Version < Ada_2005 |
2813bb6b | 9002 | or else Can_Never_Be_Null (Etype (Formal_Id)) |
2820d220 AC |
9003 | then |
9004 | Set_Is_Known_Non_Null (Formal_Id); | |
9005 | Set_Can_Never_Be_Null (Formal_Id); | |
9006 | end if; | |
2813bb6b | 9007 | |
41251c60 JM |
9008 | -- Ada 2005 (AI-231): Null-exclusion access subtype |
9009 | ||
2813bb6b ES |
9010 | elsif Is_Access_Type (Etype (Formal_Id)) |
9011 | and then Can_Never_Be_Null (Etype (Formal_Id)) | |
9012 | then | |
2813bb6b | 9013 | Set_Is_Known_Non_Null (Formal_Id); |
fbf5a39b AC |
9014 | end if; |
9015 | ||
996ae0b0 RK |
9016 | Set_Mechanism (Formal_Id, Default_Mechanism); |
9017 | Set_Formal_Validity (Formal_Id); | |
9018 | end Set_Formal_Mode; | |
9019 | ||
9020 | ------------------------- | |
9021 | -- Set_Formal_Validity -- | |
9022 | ------------------------- | |
9023 | ||
9024 | procedure Set_Formal_Validity (Formal_Id : Entity_Id) is | |
9025 | begin | |
82c80734 RD |
9026 | -- If no validity checking, then we cannot assume anything about the |
9027 | -- validity of parameters, since we do not know there is any checking | |
9028 | -- of the validity on the call side. | |
996ae0b0 RK |
9029 | |
9030 | if not Validity_Checks_On then | |
9031 | return; | |
9032 | ||
fbf5a39b AC |
9033 | -- If validity checking for parameters is enabled, this means we are |
9034 | -- not supposed to make any assumptions about argument values. | |
9035 | ||
9036 | elsif Validity_Check_Parameters then | |
9037 | return; | |
9038 | ||
9039 | -- If we are checking in parameters, we will assume that the caller is | |
9040 | -- also checking parameters, so we can assume the parameter is valid. | |
9041 | ||
996ae0b0 RK |
9042 | elsif Ekind (Formal_Id) = E_In_Parameter |
9043 | and then Validity_Check_In_Params | |
9044 | then | |
9045 | Set_Is_Known_Valid (Formal_Id, True); | |
9046 | ||
fbf5a39b AC |
9047 | -- Similar treatment for IN OUT parameters |
9048 | ||
996ae0b0 RK |
9049 | elsif Ekind (Formal_Id) = E_In_Out_Parameter |
9050 | and then Validity_Check_In_Out_Params | |
9051 | then | |
9052 | Set_Is_Known_Valid (Formal_Id, True); | |
9053 | end if; | |
9054 | end Set_Formal_Validity; | |
9055 | ||
9056 | ------------------------ | |
9057 | -- Subtype_Conformant -- | |
9058 | ------------------------ | |
9059 | ||
ce2b6ba5 JM |
9060 | function Subtype_Conformant |
9061 | (New_Id : Entity_Id; | |
9062 | Old_Id : Entity_Id; | |
9063 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
9064 | is | |
996ae0b0 | 9065 | Result : Boolean; |
996ae0b0 | 9066 | begin |
ce2b6ba5 JM |
9067 | Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result, |
9068 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
9069 | return Result; |
9070 | end Subtype_Conformant; | |
9071 | ||
9072 | --------------------- | |
9073 | -- Type_Conformant -- | |
9074 | --------------------- | |
9075 | ||
41251c60 JM |
9076 | function Type_Conformant |
9077 | (New_Id : Entity_Id; | |
9078 | Old_Id : Entity_Id; | |
9079 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
9080 | is | |
996ae0b0 | 9081 | Result : Boolean; |
996ae0b0 | 9082 | begin |
c8ef728f ES |
9083 | May_Hide_Profile := False; |
9084 | ||
41251c60 JM |
9085 | Check_Conformance |
9086 | (New_Id, Old_Id, Type_Conformant, False, Result, | |
9087 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
9088 | return Result; |
9089 | end Type_Conformant; | |
9090 | ||
9091 | ------------------------------- | |
9092 | -- Valid_Operator_Definition -- | |
9093 | ------------------------------- | |
9094 | ||
9095 | procedure Valid_Operator_Definition (Designator : Entity_Id) is | |
9096 | N : Integer := 0; | |
9097 | F : Entity_Id; | |
9098 | Id : constant Name_Id := Chars (Designator); | |
9099 | N_OK : Boolean; | |
9100 | ||
9101 | begin | |
9102 | F := First_Formal (Designator); | |
996ae0b0 RK |
9103 | while Present (F) loop |
9104 | N := N + 1; | |
9105 | ||
9106 | if Present (Default_Value (F)) then | |
ed2233dc | 9107 | Error_Msg_N |
996ae0b0 RK |
9108 | ("default values not allowed for operator parameters", |
9109 | Parent (F)); | |
9110 | end if; | |
9111 | ||
9112 | Next_Formal (F); | |
9113 | end loop; | |
9114 | ||
9115 | -- Verify that user-defined operators have proper number of arguments | |
9116 | -- First case of operators which can only be unary | |
9117 | ||
9118 | if Id = Name_Op_Not | |
9119 | or else Id = Name_Op_Abs | |
9120 | then | |
9121 | N_OK := (N = 1); | |
9122 | ||
9123 | -- Case of operators which can be unary or binary | |
9124 | ||
9125 | elsif Id = Name_Op_Add | |
9126 | or Id = Name_Op_Subtract | |
9127 | then | |
9128 | N_OK := (N in 1 .. 2); | |
9129 | ||
9130 | -- All other operators can only be binary | |
9131 | ||
9132 | else | |
9133 | N_OK := (N = 2); | |
9134 | end if; | |
9135 | ||
9136 | if not N_OK then | |
9137 | Error_Msg_N | |
9138 | ("incorrect number of arguments for operator", Designator); | |
9139 | end if; | |
9140 | ||
9141 | if Id = Name_Op_Ne | |
9142 | and then Base_Type (Etype (Designator)) = Standard_Boolean | |
9143 | and then not Is_Intrinsic_Subprogram (Designator) | |
9144 | then | |
9145 | Error_Msg_N | |
9146 | ("explicit definition of inequality not allowed", Designator); | |
9147 | end if; | |
9148 | end Valid_Operator_Definition; | |
9149 | ||
9150 | end Sem_Ch6; |