]>
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 | -- -- |
77a40ec1 | 9 | -- Copyright (C) 1992-2014, 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 | ||
80e59506 | 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; |
616547fa | 37 | with Exp_Dbug; use Exp_Dbug; |
ce2b6ba5 | 38 | with Exp_Disp; use Exp_Disp; |
e660dbf7 | 39 | with Exp_Tss; use Exp_Tss; |
ec4867fa | 40 | with Exp_Util; use Exp_Util; |
fbf5a39b | 41 | with Fname; use Fname; |
996ae0b0 | 42 | with Freeze; use Freeze; |
540d8610 | 43 | with Inline; use Inline; |
41251c60 | 44 | with Itypes; use Itypes; |
996ae0b0 | 45 | with Lib.Xref; use Lib.Xref; |
ec4867fa | 46 | with Layout; use Layout; |
996ae0b0 RK |
47 | with Namet; use Namet; |
48 | with Lib; use Lib; | |
49 | with Nlists; use Nlists; | |
50 | with Nmake; use Nmake; | |
51 | with Opt; use Opt; | |
52 | with Output; use Output; | |
b20de9b9 AC |
53 | with Restrict; use Restrict; |
54 | with Rident; use Rident; | |
996ae0b0 RK |
55 | with Rtsfind; use Rtsfind; |
56 | with Sem; use Sem; | |
a4100e55 | 57 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
58 | with Sem_Cat; use Sem_Cat; |
59 | with Sem_Ch3; use Sem_Ch3; | |
60 | with Sem_Ch4; use Sem_Ch4; | |
61 | with Sem_Ch5; use Sem_Ch5; | |
62 | with Sem_Ch8; use Sem_Ch8; | |
9bc856dd | 63 | with Sem_Ch10; use Sem_Ch10; |
996ae0b0 | 64 | with Sem_Ch12; use Sem_Ch12; |
0f1a6a0b | 65 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 66 | with Sem_Dim; use Sem_Dim; |
996ae0b0 RK |
67 | with Sem_Disp; use Sem_Disp; |
68 | with Sem_Dist; use Sem_Dist; | |
69 | with Sem_Elim; use Sem_Elim; | |
70 | with Sem_Eval; use Sem_Eval; | |
71 | with Sem_Mech; use Sem_Mech; | |
72 | with Sem_Prag; use Sem_Prag; | |
73 | with Sem_Res; use Sem_Res; | |
74 | with Sem_Util; use Sem_Util; | |
75 | with Sem_Type; use Sem_Type; | |
76 | with Sem_Warn; use Sem_Warn; | |
77 | with Sinput; use Sinput; | |
78 | with Stand; use Stand; | |
79 | with Sinfo; use Sinfo; | |
80 | with Sinfo.CN; use Sinfo.CN; | |
81 | with Snames; use Snames; | |
82 | with Stringt; use Stringt; | |
83 | with Style; | |
84 | with Stylesw; use Stylesw; | |
8417f4b2 | 85 | with Targparm; use Targparm; |
996ae0b0 RK |
86 | with Tbuild; use Tbuild; |
87 | with Uintp; use Uintp; | |
88 | with Urealp; use Urealp; | |
89 | with Validsw; use Validsw; | |
90 | ||
91 | package body Sem_Ch6 is | |
92 | ||
c8ef728f | 93 | May_Hide_Profile : Boolean := False; |
ec4867fa ES |
94 | -- This flag is used to indicate that two formals in two subprograms being |
95 | -- checked for conformance differ only in that one is an access parameter | |
96 | -- while the other is of a general access type with the same designated | |
97 | -- type. In this case, if the rest of the signatures match, a call to | |
98 | -- either subprogram may be ambiguous, which is worth a warning. The flag | |
99 | -- is set in Compatible_Types, and the warning emitted in | |
100 | -- New_Overloaded_Entity. | |
c8ef728f | 101 | |
996ae0b0 RK |
102 | ----------------------- |
103 | -- Local Subprograms -- | |
104 | ----------------------- | |
105 | ||
4d8f3296 ES |
106 | procedure Analyze_Null_Procedure |
107 | (N : Node_Id; | |
108 | Is_Completion : out Boolean); | |
9d2a2071 | 109 | -- A null procedure can be a declaration or (Ada 2012) a completion |
4d8f3296 | 110 | |
5d37ba92 | 111 | procedure Analyze_Return_Statement (N : Node_Id); |
5b9c3fc4 | 112 | -- Common processing for simple and extended return statements |
ec4867fa ES |
113 | |
114 | procedure Analyze_Function_Return (N : Node_Id); | |
81db9d77 ES |
115 | -- Subsidiary to Analyze_Return_Statement. Called when the return statement |
116 | -- applies to a [generic] function. | |
ec4867fa | 117 | |
82c80734 RD |
118 | procedure Analyze_Return_Type (N : Node_Id); |
119 | -- Subsidiary to Process_Formals: analyze subtype mark in function | |
5b9c3fc4 | 120 | -- specification in a context where the formals are visible and hide |
82c80734 RD |
121 | -- outer homographs. |
122 | ||
b1b543d2 | 123 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id); |
13d923cc RD |
124 | -- Does all the real work of Analyze_Subprogram_Body. This is split out so |
125 | -- that we can use RETURN but not skip the debug output at the end. | |
b1b543d2 | 126 | |
996ae0b0 | 127 | procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); |
82c80734 RD |
128 | -- Analyze a generic subprogram body. N is the body to be analyzed, and |
129 | -- Gen_Id is the defining entity Id for the corresponding spec. | |
996ae0b0 | 130 | |
806f6d37 AC |
131 | function Can_Override_Operator (Subp : Entity_Id) return Boolean; |
132 | -- Returns true if Subp can override a predefined operator. | |
133 | ||
996ae0b0 | 134 | procedure Check_Conformance |
41251c60 JM |
135 | (New_Id : Entity_Id; |
136 | Old_Id : Entity_Id; | |
137 | Ctype : Conformance_Type; | |
138 | Errmsg : Boolean; | |
139 | Conforms : out Boolean; | |
140 | Err_Loc : Node_Id := Empty; | |
141 | Get_Inst : Boolean := False; | |
142 | Skip_Controlling_Formals : Boolean := False); | |
996ae0b0 RK |
143 | -- Given two entities, this procedure checks that the profiles associated |
144 | -- with these entities meet the conformance criterion given by the third | |
145 | -- parameter. If they conform, Conforms is set True and control returns | |
146 | -- to the caller. If they do not conform, Conforms is set to False, and | |
147 | -- in addition, if Errmsg is True on the call, proper messages are output | |
148 | -- to complain about the conformance failure. If Err_Loc is non_Empty | |
149 | -- the error messages are placed on Err_Loc, if Err_Loc is empty, then | |
150 | -- error messages are placed on the appropriate part of the construct | |
151 | -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance | |
152 | -- against a formal access-to-subprogram type so Get_Instance_Of must | |
153 | -- be called. | |
154 | ||
155 | procedure Check_Subprogram_Order (N : Node_Id); | |
156 | -- N is the N_Subprogram_Body node for a subprogram. This routine applies | |
157 | -- the alpha ordering rule for N if this ordering requirement applicable. | |
158 | ||
996ae0b0 RK |
159 | procedure Check_Returns |
160 | (HSS : Node_Id; | |
161 | Mode : Character; | |
c8ef728f ES |
162 | Err : out Boolean; |
163 | Proc : Entity_Id := Empty); | |
164 | -- Called to check for missing return statements in a function body, or for | |
0a36105d | 165 | -- returns present in a procedure body which has No_Return set. HSS is the |
c8ef728f ES |
166 | -- handled statement sequence for the subprogram body. This procedure |
167 | -- checks all flow paths to make sure they either have return (Mode = 'F', | |
168 | -- used for functions) or do not have a return (Mode = 'P', used for | |
169 | -- No_Return procedures). The flag Err is set if there are any control | |
170 | -- paths not explicitly terminated by a return in the function case, and is | |
171 | -- True otherwise. Proc is the entity for the procedure case and is used | |
172 | -- in posting the warning message. | |
996ae0b0 | 173 | |
e5a58fac AC |
174 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id); |
175 | -- In Ada 2012, a primitive equality operator on an untagged record type | |
176 | -- must appear before the type is frozen, and have the same visibility as | |
177 | -- that of the type. This procedure checks that this rule is met, and | |
178 | -- otherwise emits an error on the subprogram declaration and a warning | |
b2834fbd AC |
179 | -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode, |
180 | -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier | |
181 | -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility | |
182 | -- is set, otherwise the call has no effect. | |
e5a58fac | 183 | |
996ae0b0 | 184 | procedure Enter_Overloaded_Entity (S : Entity_Id); |
82c80734 RD |
185 | -- This procedure makes S, a new overloaded entity, into the first visible |
186 | -- entity with that name. | |
996ae0b0 | 187 | |
a5b62485 AC |
188 | function Is_Non_Overriding_Operation |
189 | (Prev_E : Entity_Id; | |
190 | New_E : Entity_Id) return Boolean; | |
191 | -- Enforce the rule given in 12.3(18): a private operation in an instance | |
192 | -- overrides an inherited operation only if the corresponding operation | |
260359e3 AC |
193 | -- was overriding in the generic. This needs to be checked for primitive |
194 | -- operations of types derived (in the generic unit) from formal private | |
195 | -- or formal derived types. | |
a5b62485 | 196 | |
996ae0b0 RK |
197 | procedure Make_Inequality_Operator (S : Entity_Id); |
198 | -- Create the declaration for an inequality operator that is implicitly | |
199 | -- created by a user-defined equality operator that yields a boolean. | |
200 | ||
996ae0b0 RK |
201 | procedure Set_Formal_Validity (Formal_Id : Entity_Id); |
202 | -- Formal_Id is an formal parameter entity. This procedure deals with | |
e358346d AC |
203 | -- setting the proper validity status for this entity, which depends on |
204 | -- the kind of parameter and the validity checking mode. | |
996ae0b0 RK |
205 | |
206 | --------------------------------------------- | |
207 | -- Analyze_Abstract_Subprogram_Declaration -- | |
208 | --------------------------------------------- | |
209 | ||
210 | procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is | |
fbf5a39b AC |
211 | Designator : constant Entity_Id := |
212 | Analyze_Subprogram_Specification (Specification (N)); | |
996ae0b0 RK |
213 | Scop : constant Entity_Id := Current_Scope; |
214 | ||
215 | begin | |
ce5ba43a | 216 | Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N); |
38171f43 | 217 | |
996ae0b0 | 218 | Generate_Definition (Designator); |
dac3bede | 219 | Set_Contract (Designator, Make_Contract (Sloc (Designator))); |
f937473f | 220 | Set_Is_Abstract_Subprogram (Designator); |
996ae0b0 RK |
221 | New_Overloaded_Entity (Designator); |
222 | Check_Delayed_Subprogram (Designator); | |
223 | ||
fbf5a39b | 224 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 | 225 | |
c5cec2fe AC |
226 | -- An abstract subprogram declared within a Ghost scope is automatically |
227 | -- Ghost (SPARK RM 6.9(2)). | |
228 | ||
229 | if Comes_From_Source (Designator) and then Within_Ghost_Scope then | |
230 | Set_Is_Ghost_Entity (Designator); | |
231 | end if; | |
232 | ||
996ae0b0 RK |
233 | if Ekind (Scope (Designator)) = E_Protected_Type then |
234 | Error_Msg_N | |
235 | ("abstract subprogram not allowed in protected type", N); | |
5d37ba92 ES |
236 | |
237 | -- Issue a warning if the abstract subprogram is neither a dispatching | |
238 | -- operation nor an operation that overrides an inherited subprogram or | |
239 | -- predefined operator, since this most likely indicates a mistake. | |
240 | ||
241 | elsif Warn_On_Redundant_Constructs | |
242 | and then not Is_Dispatching_Operation (Designator) | |
038140ed | 243 | and then not Present (Overridden_Operation (Designator)) |
5d37ba92 ES |
244 | and then (not Is_Operator_Symbol_Name (Chars (Designator)) |
245 | or else Scop /= Scope (Etype (First_Formal (Designator)))) | |
246 | then | |
247 | Error_Msg_N | |
dbfeb4fa | 248 | ("abstract subprogram is not dispatching or overriding?r?", N); |
996ae0b0 | 249 | end if; |
fbf5a39b AC |
250 | |
251 | Generate_Reference_To_Formals (Designator); | |
361effb1 | 252 | Check_Eliminated (Designator); |
eaba57fb RD |
253 | |
254 | if Has_Aspects (N) then | |
255 | Analyze_Aspect_Specifications (N, Designator); | |
256 | end if; | |
996ae0b0 RK |
257 | end Analyze_Abstract_Subprogram_Declaration; |
258 | ||
b0186f71 AC |
259 | --------------------------------- |
260 | -- Analyze_Expression_Function -- | |
261 | --------------------------------- | |
262 | ||
263 | procedure Analyze_Expression_Function (N : Node_Id) is | |
264 | Loc : constant Source_Ptr := Sloc (N); | |
265 | LocX : constant Source_Ptr := Sloc (Expression (N)); | |
0b5b2bbc | 266 | Expr : constant Node_Id := Expression (N); |
d2d4b355 AC |
267 | Spec : constant Node_Id := Specification (N); |
268 | ||
8a06151a | 269 | Def_Id : Entity_Id; |
b0186f71 | 270 | |
8a06151a | 271 | Prev : Entity_Id; |
b0186f71 | 272 | -- If the expression is a completion, Prev is the entity whose |
d2d4b355 AC |
273 | -- declaration is completed. Def_Id is needed to analyze the spec. |
274 | ||
275 | New_Body : Node_Id; | |
d2d4b355 | 276 | New_Spec : Node_Id; |
b913199e | 277 | Ret : Node_Id; |
b0186f71 AC |
278 | |
279 | begin | |
280 | -- This is one of the occasions on which we transform the tree during | |
afc8324d | 281 | -- semantic analysis. If this is a completion, transform the expression |
d2b10647 ES |
282 | -- function into an equivalent subprogram body, and analyze it. |
283 | ||
284 | -- Expression functions are inlined unconditionally. The back-end will | |
285 | -- determine whether this is possible. | |
286 | ||
287 | Inline_Processing_Required := True; | |
b727a82b AC |
288 | |
289 | -- Create a specification for the generated body. Types and defauts in | |
290 | -- the profile are copies of the spec, but new entities must be created | |
291 | -- for the unit name and the formals. | |
292 | ||
293 | New_Spec := New_Copy_Tree (Spec); | |
294 | Set_Defining_Unit_Name (New_Spec, | |
295 | Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)), | |
296 | Chars (Defining_Unit_Name (Spec)))); | |
297 | ||
298 | if Present (Parameter_Specifications (New_Spec)) then | |
299 | declare | |
300 | Formal_Spec : Node_Id; | |
a6abfd78 AC |
301 | Def : Entity_Id; |
302 | ||
b727a82b AC |
303 | begin |
304 | Formal_Spec := First (Parameter_Specifications (New_Spec)); | |
b2afe274 AC |
305 | |
306 | -- Create a new formal parameter at the same source position | |
307 | ||
b727a82b | 308 | while Present (Formal_Spec) loop |
a6abfd78 AC |
309 | Def := Defining_Identifier (Formal_Spec); |
310 | Set_Defining_Identifier (Formal_Spec, | |
311 | Make_Defining_Identifier (Sloc (Def), | |
312 | Chars => Chars (Def))); | |
b727a82b AC |
313 | Next (Formal_Spec); |
314 | end loop; | |
315 | end; | |
316 | end if; | |
317 | ||
51597c23 | 318 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); |
d2d4b355 AC |
319 | |
320 | -- If there are previous overloadable entities with the same name, | |
321 | -- check whether any of them is completed by the expression function. | |
b04d926e | 322 | -- In a generic context a formal subprogram has no completion. |
d2d4b355 | 323 | |
b04d926e AC |
324 | if Present (Prev) and then Is_Overloadable (Prev) |
325 | and then not Is_Formal_Subprogram (Prev) | |
326 | then | |
51597c23 AC |
327 | Def_Id := Analyze_Subprogram_Specification (Spec); |
328 | Prev := Find_Corresponding_Spec (N); | |
d2d4b355 | 329 | end if; |
b0186f71 | 330 | |
b913199e AC |
331 | Ret := Make_Simple_Return_Statement (LocX, Expression (N)); |
332 | ||
b0186f71 AC |
333 | New_Body := |
334 | Make_Subprogram_Body (Loc, | |
d2d4b355 | 335 | Specification => New_Spec, |
b0186f71 AC |
336 | Declarations => Empty_List, |
337 | Handled_Statement_Sequence => | |
338 | Make_Handled_Sequence_Of_Statements (LocX, | |
b913199e | 339 | Statements => New_List (Ret))); |
b0186f71 | 340 | |
e7f23f06 AC |
341 | -- If the expression completes a generic subprogram, we must create a |
342 | -- separate node for the body, because at instantiation the original | |
343 | -- node of the generic copy must be a generic subprogram body, and | |
344 | -- cannot be a expression function. Otherwise we just rewrite the | |
345 | -- expression with the non-generic body. | |
346 | ||
6d7e5c54 | 347 | if Present (Prev) and then Ekind (Prev) = E_Generic_Function then |
e7f23f06 | 348 | Insert_After (N, New_Body); |
6d7e5c54 | 349 | |
e7f23f06 AC |
350 | -- Propagate any aspects or pragmas that apply to the expression |
351 | -- function to the proper body when the expression function acts | |
352 | -- as a completion. | |
353 | ||
354 | if Has_Aspects (N) then | |
355 | Move_Aspects (N, To => New_Body); | |
356 | end if; | |
357 | ||
358 | Relocate_Pragmas_To_Body (New_Body); | |
b0186f71 | 359 | |
b0186f71 | 360 | Rewrite (N, Make_Null_Statement (Loc)); |
d2d4b355 | 361 | Set_Has_Completion (Prev, False); |
b0186f71 AC |
362 | Analyze (N); |
363 | Analyze (New_Body); | |
d2b10647 | 364 | Set_Is_Inlined (Prev); |
b0186f71 | 365 | |
e5c4e2bc AC |
366 | -- If the expression function is a completion, the previous declaration |
367 | -- must come from source. We know already that appears in the current | |
368 | -- scope. The entity itself may be internally created if within a body | |
369 | -- to be inlined. | |
370 | ||
b04d926e AC |
371 | elsif Present (Prev) and then Comes_From_Source (Parent (Prev)) |
372 | and then not Is_Formal_Subprogram (Prev) | |
373 | then | |
d2d4b355 | 374 | Set_Has_Completion (Prev, False); |
76264f60 | 375 | |
c6d2191a AC |
376 | -- An expression function that is a completion freezes the |
377 | -- expression. This means freezing the return type, and if it is | |
378 | -- an access type, freezing its designated type as well. | |
1b31321b | 379 | |
c6d2191a | 380 | -- Note that we cannot defer this freezing to the analysis of the |
1b31321b AC |
381 | -- expression itself, because a freeze node might appear in a nested |
382 | -- scope, leading to an elaboration order issue in gigi. | |
c6d2191a AC |
383 | |
384 | Freeze_Before (N, Etype (Prev)); | |
1b31321b | 385 | |
c6d2191a AC |
386 | if Is_Access_Type (Etype (Prev)) then |
387 | Freeze_Before (N, Designated_Type (Etype (Prev))); | |
388 | end if; | |
389 | ||
76264f60 AC |
390 | -- For navigation purposes, indicate that the function is a body |
391 | ||
392 | Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True); | |
b0186f71 | 393 | Rewrite (N, New_Body); |
e7f23f06 | 394 | |
c0cdbd39 AC |
395 | -- Correct the parent pointer of the aspect specification list to |
396 | -- reference the rewritten node. | |
397 | ||
398 | if Has_Aspects (N) then | |
399 | Set_Parent (Aspect_Specifications (N), N); | |
400 | end if; | |
401 | ||
e7f23f06 AC |
402 | -- Propagate any pragmas that apply to the expression function to the |
403 | -- proper body when the expression function acts as a completion. | |
404 | -- Aspects are automatically transfered because of node rewriting. | |
405 | ||
406 | Relocate_Pragmas_To_Body (N); | |
d2b10647 ES |
407 | Analyze (N); |
408 | ||
6d7e5c54 AC |
409 | -- Prev is the previous entity with the same name, but it is can |
410 | -- be an unrelated spec that is not completed by the expression | |
411 | -- function. In that case the relevant entity is the one in the body. | |
412 | -- Not clear that the backend can inline it in this case ??? | |
413 | ||
414 | if Has_Completion (Prev) then | |
415 | Set_Is_Inlined (Prev); | |
31af8899 AC |
416 | |
417 | -- The formals of the expression function are body formals, | |
418 | -- and do not appear in the ali file, which will only contain | |
419 | -- references to the formals of the original subprogram spec. | |
420 | ||
421 | declare | |
422 | F1 : Entity_Id; | |
423 | F2 : Entity_Id; | |
424 | ||
425 | begin | |
426 | F1 := First_Formal (Def_Id); | |
427 | F2 := First_Formal (Prev); | |
428 | ||
429 | while Present (F1) loop | |
430 | Set_Spec_Entity (F1, F2); | |
431 | Next_Formal (F1); | |
432 | Next_Formal (F2); | |
433 | end loop; | |
434 | end; | |
435 | ||
6d7e5c54 AC |
436 | else |
437 | Set_Is_Inlined (Defining_Entity (New_Body)); | |
438 | end if; | |
439 | ||
0b5b2bbc | 440 | -- If this is not a completion, create both a declaration and a body, so |
6d7e5c54 | 441 | -- that the expression can be inlined whenever possible. |
d2b10647 ES |
442 | |
443 | else | |
a52e6d7e AC |
444 | -- An expression function that is not a completion is not a |
445 | -- subprogram declaration, and thus cannot appear in a protected | |
446 | -- definition. | |
447 | ||
448 | if Nkind (Parent (N)) = N_Protected_Definition then | |
449 | Error_Msg_N | |
450 | ("an expression function is not a legal protected operation", N); | |
451 | end if; | |
452 | ||
b8e6830b | 453 | Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec)); |
c0cdbd39 AC |
454 | |
455 | -- Correct the parent pointer of the aspect specification list to | |
456 | -- reference the rewritten node. | |
457 | ||
458 | if Has_Aspects (N) then | |
459 | Set_Parent (Aspect_Specifications (N), N); | |
460 | end if; | |
461 | ||
b0186f71 | 462 | Analyze (N); |
b04d926e | 463 | |
e699b76e AC |
464 | -- Within a generic pre-analyze the original expression for name |
465 | -- capture. The body is also generated but plays no role in | |
466 | -- this because it is not part of the original source. | |
b04d926e AC |
467 | |
468 | if Inside_A_Generic then | |
469 | declare | |
470 | Id : constant Entity_Id := Defining_Entity (N); | |
b04d926e AC |
471 | |
472 | begin | |
473 | Set_Has_Completion (Id); | |
b04d926e AC |
474 | Push_Scope (Id); |
475 | Install_Formals (Id); | |
e699b76e | 476 | Preanalyze_Spec_Expression (Expr, Etype (Id)); |
b04d926e | 477 | End_Scope; |
b04d926e AC |
478 | end; |
479 | end if; | |
480 | ||
b8e6830b AC |
481 | Set_Is_Inlined (Defining_Entity (N)); |
482 | ||
483 | -- Establish the linkages between the spec and the body. These are | |
484 | -- used when the expression function acts as the prefix of attribute | |
485 | -- 'Access in order to freeze the original expression which has been | |
486 | -- moved to the generated body. | |
487 | ||
488 | Set_Corresponding_Body (N, Defining_Entity (New_Body)); | |
489 | Set_Corresponding_Spec (New_Body, Defining_Entity (N)); | |
d2b10647 | 490 | |
6d7e5c54 AC |
491 | -- To prevent premature freeze action, insert the new body at the end |
492 | -- of the current declarations, or at the end of the package spec. | |
b913199e | 493 | -- However, resolve usage names now, to prevent spurious visibility |
ad4e3362 ES |
494 | -- on later entities. Note that the function can now be called in |
495 | -- the current declarative part, which will appear to be prior to | |
496 | -- the presence of the body in the code. There are nevertheless no | |
497 | -- order of elaboration issues because all name resolution has taken | |
498 | -- place at the point of declaration. | |
6d7e5c54 AC |
499 | |
500 | declare | |
e876c43a AC |
501 | Decls : List_Id := List_Containing (N); |
502 | Par : constant Node_Id := Parent (Decls); | |
b8e6830b | 503 | Id : constant Entity_Id := Defining_Entity (N); |
6d7e5c54 AC |
504 | |
505 | begin | |
fce54763 AC |
506 | -- If this is a wrapper created for in an instance for a formal |
507 | -- subprogram, insert body after declaration, to be analyzed when | |
508 | -- the enclosing instance is analyzed. | |
509 | ||
510 | if GNATprove_Mode | |
511 | and then Is_Generic_Actual_Subprogram (Defining_Entity (N)) | |
6d7e5c54 | 512 | then |
fce54763 AC |
513 | Insert_After (N, New_Body); |
514 | ||
515 | else | |
516 | if Nkind (Par) = N_Package_Specification | |
517 | and then Decls = Visible_Declarations (Par) | |
518 | and then Present (Private_Declarations (Par)) | |
519 | and then not Is_Empty_List (Private_Declarations (Par)) | |
520 | then | |
521 | Decls := Private_Declarations (Par); | |
522 | end if; | |
6d7e5c54 | 523 | |
fce54763 AC |
524 | Insert_After (Last (Decls), New_Body); |
525 | Push_Scope (Id); | |
526 | Install_Formals (Id); | |
3a8e3f63 | 527 | |
fce54763 AC |
528 | -- Preanalyze the expression for name capture, except in an |
529 | -- instance, where this has been done during generic analysis, | |
530 | -- and will be redone when analyzing the body. | |
845f06e2 | 531 | |
fce54763 AC |
532 | declare |
533 | Expr : constant Node_Id := Expression (Ret); | |
4058ddcc | 534 | |
fce54763 AC |
535 | begin |
536 | Set_Parent (Expr, Ret); | |
4058ddcc | 537 | |
fce54763 AC |
538 | if not In_Instance then |
539 | Preanalyze_Spec_Expression (Expr, Etype (Id)); | |
540 | end if; | |
541 | end; | |
3a8e3f63 | 542 | |
fce54763 AC |
543 | End_Scope; |
544 | end if; | |
6d7e5c54 | 545 | end; |
b0186f71 | 546 | end if; |
0b5b2bbc AC |
547 | |
548 | -- If the return expression is a static constant, we suppress warning | |
549 | -- messages on unused formals, which in most cases will be noise. | |
550 | ||
551 | Set_Is_Trivial_Subprogram (Defining_Entity (New_Body), | |
552 | Is_OK_Static_Expression (Expr)); | |
b0186f71 AC |
553 | end Analyze_Expression_Function; |
554 | ||
ec4867fa ES |
555 | ---------------------------------------- |
556 | -- Analyze_Extended_Return_Statement -- | |
557 | ---------------------------------------- | |
558 | ||
559 | procedure Analyze_Extended_Return_Statement (N : Node_Id) is | |
560 | begin | |
c86cf714 | 561 | Check_Compiler_Unit ("extended return statement", N); |
5d37ba92 | 562 | Analyze_Return_Statement (N); |
ec4867fa ES |
563 | end Analyze_Extended_Return_Statement; |
564 | ||
996ae0b0 RK |
565 | ---------------------------- |
566 | -- Analyze_Function_Call -- | |
567 | ---------------------------- | |
568 | ||
569 | procedure Analyze_Function_Call (N : Node_Id) is | |
a7e68e7f HK |
570 | Actuals : constant List_Id := Parameter_Associations (N); |
571 | Func_Nam : constant Node_Id := Name (N); | |
572 | Actual : Node_Id; | |
573 | ||
996ae0b0 | 574 | begin |
a7e68e7f | 575 | Analyze (Func_Nam); |
996ae0b0 | 576 | |
3e7302c3 AC |
577 | -- A call of the form A.B (X) may be an Ada 2005 call, which is |
578 | -- rewritten as B (A, X). If the rewriting is successful, the call | |
579 | -- has been analyzed and we just return. | |
82c80734 | 580 | |
a7e68e7f HK |
581 | if Nkind (Func_Nam) = N_Selected_Component |
582 | and then Name (N) /= Func_Nam | |
82c80734 RD |
583 | and then Is_Rewrite_Substitution (N) |
584 | and then Present (Etype (N)) | |
585 | then | |
586 | return; | |
587 | end if; | |
588 | ||
996ae0b0 RK |
589 | -- If error analyzing name, then set Any_Type as result type and return |
590 | ||
a7e68e7f | 591 | if Etype (Func_Nam) = Any_Type then |
996ae0b0 RK |
592 | Set_Etype (N, Any_Type); |
593 | return; | |
594 | end if; | |
595 | ||
596 | -- Otherwise analyze the parameters | |
597 | ||
e24329cd YM |
598 | if Present (Actuals) then |
599 | Actual := First (Actuals); | |
996ae0b0 RK |
600 | while Present (Actual) loop |
601 | Analyze (Actual); | |
602 | Check_Parameterless_Call (Actual); | |
603 | Next (Actual); | |
604 | end loop; | |
605 | end if; | |
606 | ||
607 | Analyze_Call (N); | |
996ae0b0 RK |
608 | end Analyze_Function_Call; |
609 | ||
ec4867fa ES |
610 | ----------------------------- |
611 | -- Analyze_Function_Return -- | |
612 | ----------------------------- | |
613 | ||
614 | procedure Analyze_Function_Return (N : Node_Id) is | |
a7e68e7f HK |
615 | Loc : constant Source_Ptr := Sloc (N); |
616 | Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); | |
617 | Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); | |
ec4867fa | 618 | |
5d37ba92 | 619 | R_Type : constant Entity_Id := Etype (Scope_Id); |
ec4867fa ES |
620 | -- Function result subtype |
621 | ||
622 | procedure Check_Limited_Return (Expr : Node_Id); | |
623 | -- Check the appropriate (Ada 95 or Ada 2005) rules for returning | |
624 | -- limited types. Used only for simple return statements. | |
625 | -- Expr is the expression returned. | |
626 | ||
627 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); | |
628 | -- Check that the return_subtype_indication properly matches the result | |
629 | -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). | |
630 | ||
631 | -------------------------- | |
632 | -- Check_Limited_Return -- | |
633 | -------------------------- | |
634 | ||
635 | procedure Check_Limited_Return (Expr : Node_Id) is | |
636 | begin | |
637 | -- Ada 2005 (AI-318-02): Return-by-reference types have been | |
638 | -- removed and replaced by anonymous access results. This is an | |
639 | -- incompatibility with Ada 95. Not clear whether this should be | |
640 | -- enforced yet or perhaps controllable with special switch. ??? | |
641 | ||
ce72a9a3 AC |
642 | -- A limited interface that is not immutably limited is OK. |
643 | ||
644 | if Is_Limited_Interface (R_Type) | |
645 | and then | |
646 | not (Is_Task_Interface (R_Type) | |
647 | or else Is_Protected_Interface (R_Type) | |
648 | or else Is_Synchronized_Interface (R_Type)) | |
649 | then | |
650 | null; | |
651 | ||
652 | elsif Is_Limited_Type (R_Type) | |
653 | and then not Is_Interface (R_Type) | |
ec4867fa ES |
654 | and then Comes_From_Source (N) |
655 | and then not In_Instance_Body | |
2a31c32b | 656 | and then not OK_For_Limited_Init_In_05 (R_Type, Expr) |
ec4867fa ES |
657 | then |
658 | -- Error in Ada 2005 | |
659 | ||
0791fbe9 | 660 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
661 | and then not Debug_Flag_Dot_L |
662 | and then not GNAT_Mode | |
663 | then | |
664 | Error_Msg_N | |
3ccedacc AC |
665 | ("(Ada 2005) cannot copy object of a limited type " |
666 | & "(RM-2005 6.5(5.5/2))", Expr); | |
e0ae93e2 | 667 | |
51245e2d | 668 | if Is_Limited_View (R_Type) then |
ec4867fa ES |
669 | Error_Msg_N |
670 | ("\return by reference not permitted in Ada 2005", Expr); | |
671 | end if; | |
672 | ||
673 | -- Warn in Ada 95 mode, to give folks a heads up about this | |
674 | -- incompatibility. | |
675 | ||
676 | -- In GNAT mode, this is just a warning, to allow it to be | |
677 | -- evilly turned off. Otherwise it is a real error. | |
678 | ||
9694c039 AC |
679 | -- In a generic context, simplify the warning because it makes |
680 | -- no sense to discuss pass-by-reference or copy. | |
681 | ||
ec4867fa | 682 | elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then |
9694c039 AC |
683 | if Inside_A_Generic then |
684 | Error_Msg_N | |
885c4871 | 685 | ("return of limited object not permitted in Ada 2005 " |
dbfeb4fa | 686 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
9694c039 | 687 | |
51245e2d | 688 | elsif Is_Limited_View (R_Type) then |
ec4867fa | 689 | Error_Msg_N |
20261dc1 | 690 | ("return by reference not permitted in Ada 2005 " |
dbfeb4fa | 691 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
ec4867fa ES |
692 | else |
693 | Error_Msg_N | |
20261dc1 | 694 | ("cannot copy object of a limited type in Ada 2005 " |
dbfeb4fa | 695 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
ec4867fa ES |
696 | end if; |
697 | ||
698 | -- Ada 95 mode, compatibility warnings disabled | |
699 | ||
700 | else | |
701 | return; -- skip continuation messages below | |
702 | end if; | |
703 | ||
9694c039 AC |
704 | if not Inside_A_Generic then |
705 | Error_Msg_N | |
706 | ("\consider switching to return of access type", Expr); | |
707 | Explain_Limited_Type (R_Type, Expr); | |
708 | end if; | |
ec4867fa ES |
709 | end if; |
710 | end Check_Limited_Return; | |
711 | ||
712 | ------------------------------------- | |
713 | -- Check_Return_Subtype_Indication -- | |
714 | ------------------------------------- | |
715 | ||
716 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is | |
7665e4bd AC |
717 | Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); |
718 | ||
719 | R_Stm_Type : constant Entity_Id := Etype (Return_Obj); | |
720 | -- Subtype given in the extended return statement (must match R_Type) | |
ec4867fa ES |
721 | |
722 | Subtype_Ind : constant Node_Id := | |
723 | Object_Definition (Original_Node (Obj_Decl)); | |
724 | ||
7f568bfa AC |
725 | R_Type_Is_Anon_Access : constant Boolean := |
726 | Ekind_In (R_Type, | |
727 | E_Anonymous_Access_Subprogram_Type, | |
728 | E_Anonymous_Access_Protected_Subprogram_Type, | |
729 | E_Anonymous_Access_Type); | |
ec4867fa ES |
730 | -- True if return type of the function is an anonymous access type |
731 | -- Can't we make Is_Anonymous_Access_Type in einfo ??? | |
732 | ||
7f568bfa AC |
733 | R_Stm_Type_Is_Anon_Access : constant Boolean := |
734 | Ekind_In (R_Stm_Type, | |
735 | E_Anonymous_Access_Subprogram_Type, | |
736 | E_Anonymous_Access_Protected_Subprogram_Type, | |
737 | E_Anonymous_Access_Type); | |
ec4867fa ES |
738 | -- True if type of the return object is an anonymous access type |
739 | ||
7f568bfa AC |
740 | procedure Error_No_Match (N : Node_Id); |
741 | -- Output error messages for case where types do not statically | |
742 | -- match. N is the location for the messages. | |
743 | ||
744 | -------------------- | |
745 | -- Error_No_Match -- | |
746 | -------------------- | |
747 | ||
748 | procedure Error_No_Match (N : Node_Id) is | |
749 | begin | |
750 | Error_Msg_N | |
751 | ("subtype must statically match function result subtype", N); | |
752 | ||
753 | if not Predicates_Match (R_Stm_Type, R_Type) then | |
754 | Error_Msg_Node_2 := R_Type; | |
755 | Error_Msg_NE | |
3ccedacc | 756 | ("\predicate of& does not match predicate of&", |
7f568bfa AC |
757 | N, R_Stm_Type); |
758 | end if; | |
759 | end Error_No_Match; | |
760 | ||
761 | -- Start of processing for Check_Return_Subtype_Indication | |
762 | ||
ec4867fa | 763 | begin |
7665e4bd | 764 | -- First, avoid cascaded errors |
ec4867fa ES |
765 | |
766 | if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then | |
767 | return; | |
768 | end if; | |
769 | ||
770 | -- "return access T" case; check that the return statement also has | |
771 | -- "access T", and that the subtypes statically match: | |
53cf4600 | 772 | -- if this is an access to subprogram the signatures must match. |
ec4867fa ES |
773 | |
774 | if R_Type_Is_Anon_Access then | |
775 | if R_Stm_Type_Is_Anon_Access then | |
53cf4600 ES |
776 | if |
777 | Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type | |
0a36105d | 778 | then |
53cf4600 ES |
779 | if Base_Type (Designated_Type (R_Stm_Type)) /= |
780 | Base_Type (Designated_Type (R_Type)) | |
781 | or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) | |
782 | then | |
7f568bfa | 783 | Error_No_Match (Subtype_Mark (Subtype_Ind)); |
53cf4600 ES |
784 | end if; |
785 | ||
786 | else | |
787 | -- For two anonymous access to subprogram types, the | |
788 | -- types themselves must be type conformant. | |
789 | ||
790 | if not Conforming_Types | |
791 | (R_Stm_Type, R_Type, Fully_Conformant) | |
792 | then | |
7f568bfa | 793 | Error_No_Match (Subtype_Ind); |
53cf4600 | 794 | end if; |
ec4867fa | 795 | end if; |
0a36105d | 796 | |
ec4867fa ES |
797 | else |
798 | Error_Msg_N ("must use anonymous access type", Subtype_Ind); | |
799 | end if; | |
800 | ||
6cce2156 GD |
801 | -- If the return object is of an anonymous access type, then report |
802 | -- an error if the function's result type is not also anonymous. | |
803 | ||
804 | elsif R_Stm_Type_Is_Anon_Access | |
805 | and then not R_Type_Is_Anon_Access | |
806 | then | |
3ccedacc AC |
807 | Error_Msg_N ("anonymous access not allowed for function with " |
808 | & "named access result", Subtype_Ind); | |
6cce2156 | 809 | |
81d93365 AC |
810 | -- Subtype indication case: check that the return object's type is |
811 | -- covered by the result type, and that the subtypes statically match | |
812 | -- when the result subtype is constrained. Also handle record types | |
813 | -- with unknown discriminants for which we have built the underlying | |
814 | -- record view. Coverage is needed to allow specific-type return | |
815 | -- objects when the result type is class-wide (see AI05-32). | |
816 | ||
817 | elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) | |
9013065b | 818 | or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) |
212863c0 AC |
819 | and then |
820 | Covers | |
821 | (Base_Type (R_Type), | |
822 | Underlying_Record_View (Base_Type (R_Stm_Type)))) | |
9013065b AC |
823 | then |
824 | -- A null exclusion may be present on the return type, on the | |
825 | -- function specification, on the object declaration or on the | |
826 | -- subtype itself. | |
ec4867fa | 827 | |
21d27997 RD |
828 | if Is_Access_Type (R_Type) |
829 | and then | |
830 | (Can_Never_Be_Null (R_Type) | |
831 | or else Null_Exclusion_Present (Parent (Scope_Id))) /= | |
832 | Can_Never_Be_Null (R_Stm_Type) | |
833 | then | |
7f568bfa | 834 | Error_No_Match (Subtype_Ind); |
21d27997 RD |
835 | end if; |
836 | ||
105b5e65 | 837 | -- AI05-103: for elementary types, subtypes must statically match |
8779dffa AC |
838 | |
839 | if Is_Constrained (R_Type) | |
840 | or else Is_Access_Type (R_Type) | |
841 | then | |
ec4867fa | 842 | if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then |
7f568bfa | 843 | Error_No_Match (Subtype_Ind); |
ec4867fa ES |
844 | end if; |
845 | end if; | |
846 | ||
a8b346d2 RD |
847 | -- All remaining cases are illegal |
848 | ||
849 | -- Note: previous versions of this subprogram allowed the return | |
850 | -- value to be the ancestor of the return type if the return type | |
851 | -- was a null extension. This was plainly incorrect. | |
ff7139c3 | 852 | |
ec4867fa ES |
853 | else |
854 | Error_Msg_N | |
855 | ("wrong type for return_subtype_indication", Subtype_Ind); | |
856 | end if; | |
857 | end Check_Return_Subtype_Indication; | |
858 | ||
859 | --------------------- | |
860 | -- Local Variables -- | |
861 | --------------------- | |
862 | ||
863 | Expr : Node_Id; | |
864 | ||
865 | -- Start of processing for Analyze_Function_Return | |
866 | ||
867 | begin | |
868 | Set_Return_Present (Scope_Id); | |
869 | ||
5d37ba92 | 870 | if Nkind (N) = N_Simple_Return_Statement then |
ec4867fa | 871 | Expr := Expression (N); |
4ee646da | 872 | |
e917aec2 RD |
873 | -- Guard against a malformed expression. The parser may have tried to |
874 | -- recover but the node is not analyzable. | |
4ee646da AC |
875 | |
876 | if Nkind (Expr) = N_Error then | |
877 | Set_Etype (Expr, Any_Type); | |
878 | Expander_Mode_Save_And_Set (False); | |
879 | return; | |
880 | ||
881 | else | |
0180fd26 AC |
882 | -- The resolution of a controlled [extension] aggregate associated |
883 | -- with a return statement creates a temporary which needs to be | |
884 | -- finalized on function exit. Wrap the return statement inside a | |
885 | -- block so that the finalization machinery can detect this case. | |
886 | -- This early expansion is done only when the return statement is | |
887 | -- not part of a handled sequence of statements. | |
888 | ||
889 | if Nkind_In (Expr, N_Aggregate, | |
890 | N_Extension_Aggregate) | |
891 | and then Needs_Finalization (R_Type) | |
892 | and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements | |
893 | then | |
894 | Rewrite (N, | |
895 | Make_Block_Statement (Loc, | |
896 | Handled_Statement_Sequence => | |
897 | Make_Handled_Sequence_Of_Statements (Loc, | |
898 | Statements => New_List (Relocate_Node (N))))); | |
899 | ||
900 | Analyze (N); | |
901 | return; | |
902 | end if; | |
903 | ||
4ee646da AC |
904 | Analyze_And_Resolve (Expr, R_Type); |
905 | Check_Limited_Return (Expr); | |
906 | end if; | |
ec4867fa | 907 | |
ad05f2e9 | 908 | -- RETURN only allowed in SPARK as the last statement in function |
607d0635 | 909 | |
fe5d3068 | 910 | if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements |
607d0635 AC |
911 | and then |
912 | (Nkind (Parent (Parent (N))) /= N_Subprogram_Body | |
8d606a78 | 913 | or else Present (Next (N))) |
607d0635 | 914 | then |
ce5ba43a | 915 | Check_SPARK_05_Restriction |
fe5d3068 | 916 | ("RETURN should be the last statement in function", N); |
607d0635 AC |
917 | end if; |
918 | ||
ec4867fa | 919 | else |
ce5ba43a | 920 | Check_SPARK_05_Restriction ("extended RETURN is not allowed", N); |
607d0635 | 921 | |
ec4867fa ES |
922 | -- Analyze parts specific to extended_return_statement: |
923 | ||
924 | declare | |
de6cad7c | 925 | Obj_Decl : constant Node_Id := |
b9daa96e | 926 | Last (Return_Object_Declarations (N)); |
de6cad7c | 927 | Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl); |
b9daa96e | 928 | HSS : constant Node_Id := Handled_Statement_Sequence (N); |
ec4867fa ES |
929 | |
930 | begin | |
931 | Expr := Expression (Obj_Decl); | |
932 | ||
933 | -- Note: The check for OK_For_Limited_Init will happen in | |
934 | -- Analyze_Object_Declaration; we treat it as a normal | |
935 | -- object declaration. | |
936 | ||
cd1c668b | 937 | Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); |
ec4867fa ES |
938 | Analyze (Obj_Decl); |
939 | ||
ec4867fa ES |
940 | Check_Return_Subtype_Indication (Obj_Decl); |
941 | ||
942 | if Present (HSS) then | |
943 | Analyze (HSS); | |
944 | ||
945 | if Present (Exception_Handlers (HSS)) then | |
946 | ||
947 | -- ???Has_Nested_Block_With_Handler needs to be set. | |
948 | -- Probably by creating an actual N_Block_Statement. | |
949 | -- Probably in Expand. | |
950 | ||
951 | null; | |
952 | end if; | |
953 | end if; | |
954 | ||
9337aa0a AC |
955 | -- Mark the return object as referenced, since the return is an |
956 | -- implicit reference of the object. | |
957 | ||
958 | Set_Referenced (Defining_Identifier (Obj_Decl)); | |
959 | ||
ec4867fa | 960 | Check_References (Stm_Entity); |
de6cad7c AC |
961 | |
962 | -- Check RM 6.5 (5.9/3) | |
963 | ||
964 | if Has_Aliased then | |
965 | if Ada_Version < Ada_2012 then | |
dbfeb4fa RD |
966 | |
967 | -- Shouldn't this test Warn_On_Ada_2012_Compatibility ??? | |
968 | -- Can it really happen (extended return???) | |
969 | ||
970 | Error_Msg_N | |
b785e0b8 AC |
971 | ("aliased only allowed for limited return objects " |
972 | & "in Ada 2012??", N); | |
de6cad7c | 973 | |
51245e2d | 974 | elsif not Is_Limited_View (R_Type) then |
3ccedacc AC |
975 | Error_Msg_N |
976 | ("aliased only allowed for limited return objects", N); | |
de6cad7c AC |
977 | end if; |
978 | end if; | |
ec4867fa ES |
979 | end; |
980 | end if; | |
981 | ||
21d27997 | 982 | -- Case of Expr present |
5d37ba92 | 983 | |
ec4867fa | 984 | if Present (Expr) |
21d27997 | 985 | |
8fde064e | 986 | -- Defend against previous errors |
21d27997 RD |
987 | |
988 | and then Nkind (Expr) /= N_Empty | |
5d37ba92 | 989 | and then Present (Etype (Expr)) |
ec4867fa | 990 | then |
5d37ba92 ES |
991 | -- Apply constraint check. Note that this is done before the implicit |
992 | -- conversion of the expression done for anonymous access types to | |
f3d57416 | 993 | -- ensure correct generation of the null-excluding check associated |
5d37ba92 ES |
994 | -- with null-excluding expressions found in return statements. |
995 | ||
996 | Apply_Constraint_Check (Expr, R_Type); | |
997 | ||
998 | -- Ada 2005 (AI-318-02): When the result type is an anonymous access | |
999 | -- type, apply an implicit conversion of the expression to that type | |
1000 | -- to force appropriate static and run-time accessibility checks. | |
ec4867fa | 1001 | |
0791fbe9 | 1002 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
1003 | and then Ekind (R_Type) = E_Anonymous_Access_Type |
1004 | then | |
1005 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); | |
1006 | Analyze_And_Resolve (Expr, R_Type); | |
b6b5cca8 AC |
1007 | |
1008 | -- If this is a local anonymous access to subprogram, the | |
1009 | -- accessibility check can be applied statically. The return is | |
1010 | -- illegal if the access type of the return expression is declared | |
1011 | -- inside of the subprogram (except if it is the subtype indication | |
1012 | -- of an extended return statement). | |
1013 | ||
1014 | elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then | |
1015 | if not Comes_From_Source (Current_Scope) | |
1016 | or else Ekind (Current_Scope) = E_Return_Statement | |
1017 | then | |
1018 | null; | |
1019 | ||
1020 | elsif | |
1021 | Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id) | |
1022 | then | |
1023 | Error_Msg_N ("cannot return local access to subprogram", N); | |
1024 | end if; | |
1ebc2612 AC |
1025 | |
1026 | -- The expression cannot be of a formal incomplete type | |
1027 | ||
1028 | elsif Ekind (Etype (Expr)) = E_Incomplete_Type | |
1029 | and then Is_Generic_Type (Etype (Expr)) | |
1030 | then | |
1031 | Error_Msg_N | |
1032 | ("cannot return expression of a formal incomplete type", N); | |
ec4867fa ES |
1033 | end if; |
1034 | ||
21d27997 RD |
1035 | -- If the result type is class-wide, then check that the return |
1036 | -- expression's type is not declared at a deeper level than the | |
1037 | -- function (RM05-6.5(5.6/2)). | |
1038 | ||
0791fbe9 | 1039 | if Ada_Version >= Ada_2005 |
21d27997 RD |
1040 | and then Is_Class_Wide_Type (R_Type) |
1041 | then | |
1042 | if Type_Access_Level (Etype (Expr)) > | |
1043 | Subprogram_Access_Level (Scope_Id) | |
1044 | then | |
1045 | Error_Msg_N | |
3ccedacc AC |
1046 | ("level of return expression type is deeper than " |
1047 | & "class-wide function!", Expr); | |
21d27997 RD |
1048 | end if; |
1049 | end if; | |
1050 | ||
4755cce9 JM |
1051 | -- Check incorrect use of dynamically tagged expression |
1052 | ||
1053 | if Is_Tagged_Type (R_Type) then | |
1054 | Check_Dynamically_Tagged_Expression | |
1055 | (Expr => Expr, | |
1056 | Typ => R_Type, | |
1057 | Related_Nod => N); | |
ec4867fa ES |
1058 | end if; |
1059 | ||
ec4867fa ES |
1060 | -- ??? A real run-time accessibility check is needed in cases |
1061 | -- involving dereferences of access parameters. For now we just | |
1062 | -- check the static cases. | |
1063 | ||
0791fbe9 | 1064 | if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) |
51245e2d | 1065 | and then Is_Limited_View (Etype (Scope_Id)) |
ec4867fa ES |
1066 | and then Object_Access_Level (Expr) > |
1067 | Subprogram_Access_Level (Scope_Id) | |
1068 | then | |
9694c039 AC |
1069 | -- Suppress the message in a generic, where the rewriting |
1070 | -- is irrelevant. | |
1071 | ||
1072 | if Inside_A_Generic then | |
1073 | null; | |
1074 | ||
1075 | else | |
1076 | Rewrite (N, | |
1077 | Make_Raise_Program_Error (Loc, | |
1078 | Reason => PE_Accessibility_Check_Failed)); | |
1079 | Analyze (N); | |
1080 | ||
43417b90 | 1081 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1082 | Error_Msg_N ("cannot return a local value by reference<<", N); |
1083 | Error_Msg_NE ("\& [<<", N, Standard_Program_Error); | |
9694c039 | 1084 | end if; |
ec4867fa | 1085 | end if; |
5d37ba92 ES |
1086 | |
1087 | if Known_Null (Expr) | |
1088 | and then Nkind (Parent (Scope_Id)) = N_Function_Specification | |
1089 | and then Null_Exclusion_Present (Parent (Scope_Id)) | |
1090 | then | |
1091 | Apply_Compile_Time_Constraint_Error | |
1092 | (N => Expr, | |
1093 | Msg => "(Ada 2005) null not allowed for " | |
dbfeb4fa | 1094 | & "null-excluding return??", |
5d37ba92 ES |
1095 | Reason => CE_Null_Not_Allowed); |
1096 | end if; | |
ec4867fa ES |
1097 | end if; |
1098 | end Analyze_Function_Return; | |
1099 | ||
996ae0b0 RK |
1100 | ------------------------------------- |
1101 | -- Analyze_Generic_Subprogram_Body -- | |
1102 | ------------------------------------- | |
1103 | ||
1104 | procedure Analyze_Generic_Subprogram_Body | |
1105 | (N : Node_Id; | |
1106 | Gen_Id : Entity_Id) | |
1107 | is | |
fbf5a39b | 1108 | Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); |
996ae0b0 | 1109 | Kind : constant Entity_Kind := Ekind (Gen_Id); |
fbf5a39b | 1110 | Body_Id : Entity_Id; |
996ae0b0 | 1111 | New_N : Node_Id; |
fbf5a39b | 1112 | Spec : Node_Id; |
996ae0b0 RK |
1113 | |
1114 | begin | |
82c80734 RD |
1115 | -- Copy body and disable expansion while analyzing the generic For a |
1116 | -- stub, do not copy the stub (which would load the proper body), this | |
1117 | -- will be done when the proper body is analyzed. | |
996ae0b0 RK |
1118 | |
1119 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
1120 | New_N := Copy_Generic_Node (N, Empty, Instantiating => False); | |
1121 | Rewrite (N, New_N); | |
1122 | Start_Generic; | |
1123 | end if; | |
1124 | ||
1125 | Spec := Specification (N); | |
1126 | ||
1127 | -- Within the body of the generic, the subprogram is callable, and | |
1128 | -- behaves like the corresponding non-generic unit. | |
1129 | ||
fbf5a39b | 1130 | Body_Id := Defining_Entity (Spec); |
996ae0b0 RK |
1131 | |
1132 | if Kind = E_Generic_Procedure | |
1133 | and then Nkind (Spec) /= N_Procedure_Specification | |
1134 | then | |
fbf5a39b | 1135 | Error_Msg_N ("invalid body for generic procedure ", Body_Id); |
996ae0b0 RK |
1136 | return; |
1137 | ||
1138 | elsif Kind = E_Generic_Function | |
1139 | and then Nkind (Spec) /= N_Function_Specification | |
1140 | then | |
fbf5a39b | 1141 | Error_Msg_N ("invalid body for generic function ", Body_Id); |
996ae0b0 RK |
1142 | return; |
1143 | end if; | |
1144 | ||
fbf5a39b | 1145 | Set_Corresponding_Body (Gen_Decl, Body_Id); |
996ae0b0 RK |
1146 | |
1147 | if Has_Completion (Gen_Id) | |
1148 | and then Nkind (Parent (N)) /= N_Subunit | |
1149 | then | |
1150 | Error_Msg_N ("duplicate generic body", N); | |
1151 | return; | |
1152 | else | |
1153 | Set_Has_Completion (Gen_Id); | |
1154 | end if; | |
1155 | ||
1156 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1157 | Set_Ekind (Defining_Entity (Specification (N)), Kind); | |
1158 | else | |
1159 | Set_Corresponding_Spec (N, Gen_Id); | |
1160 | end if; | |
1161 | ||
1162 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1163 | Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); | |
1164 | end if; | |
1165 | ||
1166 | -- Make generic parameters immediately visible in the body. They are | |
1167 | -- needed to process the formals declarations. Then make the formals | |
1168 | -- visible in a separate step. | |
1169 | ||
0a36105d | 1170 | Push_Scope (Gen_Id); |
996ae0b0 RK |
1171 | |
1172 | declare | |
1173 | E : Entity_Id; | |
1174 | First_Ent : Entity_Id; | |
1175 | ||
1176 | begin | |
1177 | First_Ent := First_Entity (Gen_Id); | |
1178 | ||
1179 | E := First_Ent; | |
1180 | while Present (E) and then not Is_Formal (E) loop | |
1181 | Install_Entity (E); | |
1182 | Next_Entity (E); | |
1183 | end loop; | |
1184 | ||
1185 | Set_Use (Generic_Formal_Declarations (Gen_Decl)); | |
1186 | ||
1187 | -- Now generic formals are visible, and the specification can be | |
1188 | -- analyzed, for subsequent conformance check. | |
1189 | ||
fbf5a39b | 1190 | Body_Id := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 1191 | |
fbf5a39b | 1192 | -- Make formal parameters visible |
996ae0b0 RK |
1193 | |
1194 | if Present (E) then | |
1195 | ||
fbf5a39b AC |
1196 | -- E is the first formal parameter, we loop through the formals |
1197 | -- installing them so that they will be visible. | |
996ae0b0 RK |
1198 | |
1199 | Set_First_Entity (Gen_Id, E); | |
996ae0b0 RK |
1200 | while Present (E) loop |
1201 | Install_Entity (E); | |
1202 | Next_Formal (E); | |
1203 | end loop; | |
1204 | end if; | |
1205 | ||
e895b435 | 1206 | -- Visible generic entity is callable within its own body |
996ae0b0 | 1207 | |
ec4867fa | 1208 | Set_Ekind (Gen_Id, Ekind (Body_Id)); |
ea3c0651 | 1209 | Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id))); |
ec4867fa ES |
1210 | Set_Ekind (Body_Id, E_Subprogram_Body); |
1211 | Set_Convention (Body_Id, Convention (Gen_Id)); | |
1212 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); | |
1213 | Set_Scope (Body_Id, Scope (Gen_Id)); | |
c5cec2fe AC |
1214 | |
1215 | -- Inherit the "ghostness" of the generic spec. Note that this | |
1216 | -- property is not directly inherited as the body may be subject | |
1217 | -- to a different Ghost assertion policy. | |
1218 | ||
1219 | if Is_Ghost_Entity (Gen_Id) or else Within_Ghost_Scope then | |
1220 | Set_Is_Ghost_Entity (Body_Id); | |
1221 | ||
1222 | -- The Ghost policy in effect at the point of declaration and at | |
1223 | -- the point of completion must match (SPARK RM 6.9(14)). | |
1224 | ||
1225 | Check_Ghost_Completion (Gen_Id, Body_Id); | |
1226 | end if; | |
1227 | ||
fbf5a39b AC |
1228 | Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); |
1229 | ||
1230 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1231 | ||
e895b435 | 1232 | -- No body to analyze, so restore state of generic unit |
fbf5a39b AC |
1233 | |
1234 | Set_Ekind (Gen_Id, Kind); | |
1235 | Set_Ekind (Body_Id, Kind); | |
1236 | ||
1237 | if Present (First_Ent) then | |
1238 | Set_First_Entity (Gen_Id, First_Ent); | |
1239 | end if; | |
1240 | ||
1241 | End_Scope; | |
1242 | return; | |
1243 | end if; | |
996ae0b0 | 1244 | |
82c80734 RD |
1245 | -- If this is a compilation unit, it must be made visible explicitly, |
1246 | -- because the compilation of the declaration, unlike other library | |
1247 | -- unit declarations, does not. If it is not a unit, the following | |
1248 | -- is redundant but harmless. | |
996ae0b0 RK |
1249 | |
1250 | Set_Is_Immediately_Visible (Gen_Id); | |
fbf5a39b | 1251 | Reference_Body_Formals (Gen_Id, Body_Id); |
996ae0b0 | 1252 | |
ec4867fa ES |
1253 | if Is_Child_Unit (Gen_Id) then |
1254 | Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); | |
1255 | end if; | |
1256 | ||
996ae0b0 | 1257 | Set_Actual_Subtypes (N, Current_Scope); |
483361a6 | 1258 | |
ea3c0651 AC |
1259 | -- Deal with [refined] preconditions, postconditions, Contract_Cases, |
1260 | -- invariants and predicates associated with the body and its spec. | |
1261 | -- Note that this is not pure expansion as Expand_Subprogram_Contract | |
1262 | -- prepares the contract assertions for generic subprograms or for | |
1263 | -- ASIS. Do not generate contract checks in SPARK mode. | |
483361a6 | 1264 | |
f5da7a97 | 1265 | if not GNATprove_Mode then |
ea3c0651 | 1266 | Expand_Subprogram_Contract (N, Gen_Id, Body_Id); |
483361a6 | 1267 | end if; |
0dabde3a ES |
1268 | |
1269 | -- If the generic unit carries pre- or post-conditions, copy them | |
1270 | -- to the original generic tree, so that they are properly added | |
1271 | -- to any instantiation. | |
1272 | ||
1273 | declare | |
1274 | Orig : constant Node_Id := Original_Node (N); | |
1275 | Cond : Node_Id; | |
1276 | ||
1277 | begin | |
1278 | Cond := First (Declarations (N)); | |
1279 | while Present (Cond) loop | |
1280 | if Nkind (Cond) = N_Pragma | |
1281 | and then Pragma_Name (Cond) = Name_Check | |
1282 | then | |
1283 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
1284 | ||
1285 | elsif Nkind (Cond) = N_Pragma | |
1286 | and then Pragma_Name (Cond) = Name_Postcondition | |
1287 | then | |
1288 | Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id)); | |
1289 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
1290 | else | |
1291 | exit; | |
1292 | end if; | |
1293 | ||
1294 | Next (Cond); | |
1295 | end loop; | |
1296 | end; | |
1297 | ||
579847c2 AC |
1298 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); |
1299 | Set_SPARK_Pragma_Inherited (Body_Id, True); | |
1300 | ||
996ae0b0 RK |
1301 | Analyze_Declarations (Declarations (N)); |
1302 | Check_Completion; | |
1303 | Analyze (Handled_Statement_Sequence (N)); | |
1304 | ||
1305 | Save_Global_References (Original_Node (N)); | |
1306 | ||
82c80734 RD |
1307 | -- Prior to exiting the scope, include generic formals again (if any |
1308 | -- are present) in the set of local entities. | |
996ae0b0 RK |
1309 | |
1310 | if Present (First_Ent) then | |
1311 | Set_First_Entity (Gen_Id, First_Ent); | |
1312 | end if; | |
1313 | ||
fbf5a39b | 1314 | Check_References (Gen_Id); |
996ae0b0 RK |
1315 | end; |
1316 | ||
e6f69614 | 1317 | Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); |
996ae0b0 RK |
1318 | End_Scope; |
1319 | Check_Subprogram_Order (N); | |
1320 | ||
e895b435 | 1321 | -- Outside of its body, unit is generic again |
996ae0b0 RK |
1322 | |
1323 | Set_Ekind (Gen_Id, Kind); | |
fbf5a39b | 1324 | Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); |
5d37ba92 ES |
1325 | |
1326 | if Style_Check then | |
1327 | Style.Check_Identifier (Body_Id, Gen_Id); | |
1328 | end if; | |
13d923cc | 1329 | |
996ae0b0 | 1330 | End_Generic; |
996ae0b0 RK |
1331 | end Analyze_Generic_Subprogram_Body; |
1332 | ||
4d8f3296 ES |
1333 | ---------------------------- |
1334 | -- Analyze_Null_Procedure -- | |
1335 | ---------------------------- | |
1336 | ||
1337 | procedure Analyze_Null_Procedure | |
1338 | (N : Node_Id; | |
1339 | Is_Completion : out Boolean) | |
1340 | is | |
1341 | Loc : constant Source_Ptr := Sloc (N); | |
1342 | Spec : constant Node_Id := Specification (N); | |
1343 | Designator : Entity_Id; | |
1344 | Form : Node_Id; | |
1345 | Null_Body : Node_Id := Empty; | |
1346 | Prev : Entity_Id; | |
1347 | ||
1348 | begin | |
1349 | -- Capture the profile of the null procedure before analysis, for | |
1350 | -- expansion at the freeze point and at each point of call. The body is | |
1351 | -- used if the procedure has preconditions, or if it is a completion. In | |
1352 | -- the first case the body is analyzed at the freeze point, in the other | |
1353 | -- it replaces the null procedure declaration. | |
1354 | ||
1355 | Null_Body := | |
1356 | Make_Subprogram_Body (Loc, | |
1357 | Specification => New_Copy_Tree (Spec), | |
8c35b40a | 1358 | Declarations => New_List, |
4d8f3296 ES |
1359 | Handled_Statement_Sequence => |
1360 | Make_Handled_Sequence_Of_Statements (Loc, | |
1361 | Statements => New_List (Make_Null_Statement (Loc)))); | |
1362 | ||
1363 | -- Create new entities for body and formals | |
1364 | ||
1365 | Set_Defining_Unit_Name (Specification (Null_Body), | |
9d2a2071 AC |
1366 | Make_Defining_Identifier |
1367 | (Sloc (Defining_Entity (N)), | |
1368 | Chars (Defining_Entity (N)))); | |
4d8f3296 ES |
1369 | |
1370 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1371 | while Present (Form) loop | |
1372 | Set_Defining_Identifier (Form, | |
9d2a2071 AC |
1373 | Make_Defining_Identifier |
1374 | (Sloc (Defining_Identifier (Form)), | |
1375 | Chars (Defining_Identifier (Form)))); | |
4d8f3296 ES |
1376 | Next (Form); |
1377 | end loop; | |
1378 | ||
1379 | -- Determine whether the null procedure may be a completion of a generic | |
1380 | -- suprogram, in which case we use the new null body as the completion | |
1381 | -- and set minimal semantic information on the original declaration, | |
1382 | -- which is rewritten as a null statement. | |
1383 | ||
1384 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
1385 | ||
1386 | if Present (Prev) and then Is_Generic_Subprogram (Prev) then | |
1387 | Insert_Before (N, Null_Body); | |
1388 | Set_Ekind (Defining_Entity (N), Ekind (Prev)); | |
1389 | Set_Contract (Defining_Entity (N), Make_Contract (Loc)); | |
1390 | ||
1391 | Rewrite (N, Make_Null_Statement (Loc)); | |
1392 | Analyze_Generic_Subprogram_Body (Null_Body, Prev); | |
1393 | Is_Completion := True; | |
1394 | return; | |
1395 | ||
1396 | else | |
4d8f3296 ES |
1397 | -- Resolve the types of the formals now, because the freeze point |
1398 | -- may appear in a different context, e.g. an instantiation. | |
1399 | ||
1400 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1401 | while Present (Form) loop | |
1402 | if Nkind (Parameter_Type (Form)) /= N_Access_Definition then | |
1403 | Find_Type (Parameter_Type (Form)); | |
1404 | ||
1405 | elsif | |
1406 | No (Access_To_Subprogram_Definition (Parameter_Type (Form))) | |
1407 | then | |
1408 | Find_Type (Subtype_Mark (Parameter_Type (Form))); | |
1409 | ||
1410 | else | |
1411 | -- The case of a null procedure with a formal that is an | |
1412 | -- access_to_subprogram type, and that is used as an actual | |
1413 | -- in an instantiation is left to the enthusiastic reader. | |
1414 | ||
1415 | null; | |
1416 | end if; | |
1417 | ||
1418 | Next (Form); | |
1419 | end loop; | |
1420 | end if; | |
1421 | ||
1422 | -- If there are previous overloadable entities with the same name, | |
1423 | -- check whether any of them is completed by the null procedure. | |
1424 | ||
1425 | if Present (Prev) and then Is_Overloadable (Prev) then | |
1426 | Designator := Analyze_Subprogram_Specification (Spec); | |
1427 | Prev := Find_Corresponding_Spec (N); | |
1428 | end if; | |
1429 | ||
1430 | if No (Prev) or else not Comes_From_Source (Prev) then | |
1431 | Designator := Analyze_Subprogram_Specification (Spec); | |
1432 | Set_Has_Completion (Designator); | |
1433 | ||
1434 | -- Signal to caller that this is a procedure declaration | |
1435 | ||
1436 | Is_Completion := False; | |
1437 | ||
1438 | -- Null procedures are always inlined, but generic formal subprograms | |
1439 | -- which appear as such in the internal instance of formal packages, | |
1440 | -- need no completion and are not marked Inline. | |
1441 | ||
1442 | if Expander_Active | |
1443 | and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration | |
1444 | then | |
1445 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
1446 | Set_Body_To_Inline (N, Null_Body); | |
1447 | Set_Is_Inlined (Designator); | |
1448 | end if; | |
1449 | ||
1450 | else | |
2fe258bf AC |
1451 | -- The null procedure is a completion. We unconditionally rewrite |
1452 | -- this as a null body (even if expansion is not active), because | |
1453 | -- there are various error checks that are applied on this body | |
1454 | -- when it is analyzed (e.g. correct aspect placement). | |
4d8f3296 | 1455 | |
a98480dd AC |
1456 | if Has_Completion (Prev) then |
1457 | Error_Msg_Sloc := Sloc (Prev); | |
1458 | Error_Msg_NE ("duplicate body for & declared#", N, Prev); | |
1459 | end if; | |
1460 | ||
4d8f3296 | 1461 | Is_Completion := True; |
2fe258bf AC |
1462 | Rewrite (N, Null_Body); |
1463 | Analyze (N); | |
4d8f3296 ES |
1464 | end if; |
1465 | end Analyze_Null_Procedure; | |
1466 | ||
996ae0b0 RK |
1467 | ----------------------------- |
1468 | -- Analyze_Operator_Symbol -- | |
1469 | ----------------------------- | |
1470 | ||
82c80734 RD |
1471 | -- An operator symbol such as "+" or "and" may appear in context where the |
1472 | -- literal denotes an entity name, such as "+"(x, y) or in context when it | |
1473 | -- is just a string, as in (conjunction = "or"). In these cases the parser | |
1474 | -- generates this node, and the semantics does the disambiguation. Other | |
1475 | -- such case are actuals in an instantiation, the generic unit in an | |
1476 | -- instantiation, and pragma arguments. | |
996ae0b0 RK |
1477 | |
1478 | procedure Analyze_Operator_Symbol (N : Node_Id) is | |
1479 | Par : constant Node_Id := Parent (N); | |
1480 | ||
1481 | begin | |
1f0b1e48 | 1482 | if (Nkind (Par) = N_Function_Call and then N = Name (Par)) |
996ae0b0 | 1483 | or else Nkind (Par) = N_Function_Instantiation |
1f0b1e48 | 1484 | or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par)) |
996ae0b0 | 1485 | or else (Nkind (Par) = N_Pragma_Argument_Association |
1f0b1e48 | 1486 | and then not Is_Pragma_String_Literal (Par)) |
996ae0b0 | 1487 | or else Nkind (Par) = N_Subprogram_Renaming_Declaration |
800621e0 RD |
1488 | or else (Nkind (Par) = N_Attribute_Reference |
1489 | and then Attribute_Name (Par) /= Name_Value) | |
996ae0b0 RK |
1490 | then |
1491 | Find_Direct_Name (N); | |
1492 | ||
1493 | else | |
1494 | Change_Operator_Symbol_To_String_Literal (N); | |
1495 | Analyze (N); | |
1496 | end if; | |
1497 | end Analyze_Operator_Symbol; | |
1498 | ||
1499 | ----------------------------------- | |
1500 | -- Analyze_Parameter_Association -- | |
1501 | ----------------------------------- | |
1502 | ||
1503 | procedure Analyze_Parameter_Association (N : Node_Id) is | |
1504 | begin | |
1505 | Analyze (Explicit_Actual_Parameter (N)); | |
1506 | end Analyze_Parameter_Association; | |
1507 | ||
1508 | ---------------------------- | |
1509 | -- Analyze_Procedure_Call -- | |
1510 | ---------------------------- | |
1511 | ||
1512 | procedure Analyze_Procedure_Call (N : Node_Id) is | |
1513 | Loc : constant Source_Ptr := Sloc (N); | |
1514 | P : constant Node_Id := Name (N); | |
1515 | Actuals : constant List_Id := Parameter_Associations (N); | |
1516 | Actual : Node_Id; | |
1517 | New_N : Node_Id; | |
1518 | ||
1519 | procedure Analyze_Call_And_Resolve; | |
1520 | -- Do Analyze and Resolve calls for procedure call | |
cd5a9750 | 1521 | -- At end, check illegal order dependence. |
996ae0b0 | 1522 | |
fbf5a39b AC |
1523 | ------------------------------ |
1524 | -- Analyze_Call_And_Resolve -- | |
1525 | ------------------------------ | |
1526 | ||
996ae0b0 RK |
1527 | procedure Analyze_Call_And_Resolve is |
1528 | begin | |
1529 | if Nkind (N) = N_Procedure_Call_Statement then | |
1530 | Analyze_Call (N); | |
1531 | Resolve (N, Standard_Void_Type); | |
1532 | else | |
1533 | Analyze (N); | |
1534 | end if; | |
1535 | end Analyze_Call_And_Resolve; | |
1536 | ||
1537 | -- Start of processing for Analyze_Procedure_Call | |
1538 | ||
1539 | begin | |
1540 | -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote | |
1541 | -- a procedure call or an entry call. The prefix may denote an access | |
1542 | -- to subprogram type, in which case an implicit dereference applies. | |
f3d57416 | 1543 | -- If the prefix is an indexed component (without implicit dereference) |
996ae0b0 RK |
1544 | -- then the construct denotes a call to a member of an entire family. |
1545 | -- If the prefix is a simple name, it may still denote a call to a | |
1546 | -- parameterless member of an entry family. Resolution of these various | |
1547 | -- interpretations is delicate. | |
1548 | ||
1549 | Analyze (P); | |
1550 | ||
758c442c GD |
1551 | -- If this is a call of the form Obj.Op, the call may have been |
1552 | -- analyzed and possibly rewritten into a block, in which case | |
1553 | -- we are done. | |
1554 | ||
1555 | if Analyzed (N) then | |
1556 | return; | |
1557 | end if; | |
1558 | ||
7415029d AC |
1559 | -- If there is an error analyzing the name (which may have been |
1560 | -- rewritten if the original call was in prefix notation) then error | |
1561 | -- has been emitted already, mark node and return. | |
996ae0b0 | 1562 | |
21791d97 | 1563 | if Error_Posted (N) or else Etype (Name (N)) = Any_Type then |
996ae0b0 RK |
1564 | Set_Etype (N, Any_Type); |
1565 | return; | |
1566 | end if; | |
1567 | ||
1568 | -- Otherwise analyze the parameters | |
1569 | ||
1570 | if Present (Actuals) then | |
1571 | Actual := First (Actuals); | |
1572 | ||
1573 | while Present (Actual) loop | |
1574 | Analyze (Actual); | |
1575 | Check_Parameterless_Call (Actual); | |
1576 | Next (Actual); | |
1577 | end loop; | |
1578 | end if; | |
1579 | ||
0bfc9a64 | 1580 | -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls |
996ae0b0 RK |
1581 | |
1582 | if Nkind (P) = N_Attribute_Reference | |
b69cd36a AC |
1583 | and then Nam_In (Attribute_Name (P), Name_Elab_Spec, |
1584 | Name_Elab_Body, | |
1585 | Name_Elab_Subp_Body) | |
996ae0b0 RK |
1586 | then |
1587 | if Present (Actuals) then | |
1588 | Error_Msg_N | |
1589 | ("no parameters allowed for this call", First (Actuals)); | |
1590 | return; | |
1591 | end if; | |
1592 | ||
1593 | Set_Etype (N, Standard_Void_Type); | |
1594 | Set_Analyzed (N); | |
1595 | ||
1596 | elsif Is_Entity_Name (P) | |
1597 | and then Is_Record_Type (Etype (Entity (P))) | |
1598 | and then Remote_AST_I_Dereference (P) | |
1599 | then | |
1600 | return; | |
1601 | ||
1602 | elsif Is_Entity_Name (P) | |
1603 | and then Ekind (Entity (P)) /= E_Entry_Family | |
1604 | then | |
1605 | if Is_Access_Type (Etype (P)) | |
1606 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1607 | and then No (Actuals) | |
1608 | and then Comes_From_Source (N) | |
1609 | then | |
ed2233dc | 1610 | Error_Msg_N ("missing explicit dereference in call", N); |
996ae0b0 RK |
1611 | end if; |
1612 | ||
1613 | Analyze_Call_And_Resolve; | |
1614 | ||
1615 | -- If the prefix is the simple name of an entry family, this is | |
1616 | -- a parameterless call from within the task body itself. | |
1617 | ||
1618 | elsif Is_Entity_Name (P) | |
1619 | and then Nkind (P) = N_Identifier | |
1620 | and then Ekind (Entity (P)) = E_Entry_Family | |
1621 | and then Present (Actuals) | |
1622 | and then No (Next (First (Actuals))) | |
1623 | then | |
82c80734 RD |
1624 | -- Can be call to parameterless entry family. What appears to be the |
1625 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1626 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1627 | -- transformation. |
1628 | ||
1629 | New_N := | |
1630 | Make_Indexed_Component (Loc, | |
1631 | Prefix => | |
1632 | Make_Selected_Component (Loc, | |
1633 | Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc), | |
1634 | Selector_Name => New_Occurrence_Of (Entity (P), Loc)), | |
1635 | Expressions => Actuals); | |
1636 | Set_Name (N, New_N); | |
1637 | Set_Etype (New_N, Standard_Void_Type); | |
1638 | Set_Parameter_Associations (N, No_List); | |
1639 | Analyze_Call_And_Resolve; | |
1640 | ||
1641 | elsif Nkind (P) = N_Explicit_Dereference then | |
1642 | if Ekind (Etype (P)) = E_Subprogram_Type then | |
1643 | Analyze_Call_And_Resolve; | |
1644 | else | |
1645 | Error_Msg_N ("expect access to procedure in call", P); | |
1646 | end if; | |
1647 | ||
82c80734 RD |
1648 | -- The name can be a selected component or an indexed component that |
1649 | -- yields an access to subprogram. Such a prefix is legal if the call | |
1650 | -- has parameter associations. | |
996ae0b0 RK |
1651 | |
1652 | elsif Is_Access_Type (Etype (P)) | |
1653 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1654 | then | |
1655 | if Present (Actuals) then | |
1656 | Analyze_Call_And_Resolve; | |
1657 | else | |
ed2233dc | 1658 | Error_Msg_N ("missing explicit dereference in call ", N); |
996ae0b0 RK |
1659 | end if; |
1660 | ||
82c80734 RD |
1661 | -- If not an access to subprogram, then the prefix must resolve to the |
1662 | -- name of an entry, entry family, or protected operation. | |
996ae0b0 | 1663 | |
82c80734 RD |
1664 | -- For the case of a simple entry call, P is a selected component where |
1665 | -- the prefix is the task and the selector name is the entry. A call to | |
1666 | -- a protected procedure will have the same syntax. If the protected | |
1667 | -- object contains overloaded operations, the entity may appear as a | |
1668 | -- function, the context will select the operation whose type is Void. | |
996ae0b0 RK |
1669 | |
1670 | elsif Nkind (P) = N_Selected_Component | |
8fde064e AC |
1671 | and then Ekind_In (Entity (Selector_Name (P)), E_Entry, |
1672 | E_Procedure, | |
1673 | E_Function) | |
996ae0b0 RK |
1674 | then |
1675 | Analyze_Call_And_Resolve; | |
1676 | ||
1677 | elsif Nkind (P) = N_Selected_Component | |
1678 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family | |
1679 | and then Present (Actuals) | |
1680 | and then No (Next (First (Actuals))) | |
1681 | then | |
82c80734 RD |
1682 | -- Can be call to parameterless entry family. What appears to be the |
1683 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1684 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1685 | -- transformation. |
1686 | ||
1687 | New_N := | |
1688 | Make_Indexed_Component (Loc, | |
1689 | Prefix => New_Copy (P), | |
1690 | Expressions => Actuals); | |
1691 | Set_Name (N, New_N); | |
1692 | Set_Etype (New_N, Standard_Void_Type); | |
1693 | Set_Parameter_Associations (N, No_List); | |
1694 | Analyze_Call_And_Resolve; | |
1695 | ||
1696 | -- For the case of a reference to an element of an entry family, P is | |
1697 | -- an indexed component whose prefix is a selected component (task and | |
1698 | -- entry family), and whose index is the entry family index. | |
1699 | ||
1700 | elsif Nkind (P) = N_Indexed_Component | |
1701 | and then Nkind (Prefix (P)) = N_Selected_Component | |
1702 | and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family | |
1703 | then | |
1704 | Analyze_Call_And_Resolve; | |
1705 | ||
1706 | -- If the prefix is the name of an entry family, it is a call from | |
1707 | -- within the task body itself. | |
1708 | ||
1709 | elsif Nkind (P) = N_Indexed_Component | |
1710 | and then Nkind (Prefix (P)) = N_Identifier | |
1711 | and then Ekind (Entity (Prefix (P))) = E_Entry_Family | |
1712 | then | |
1713 | New_N := | |
1714 | Make_Selected_Component (Loc, | |
1715 | Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), | |
1716 | Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); | |
1717 | Rewrite (Prefix (P), New_N); | |
1718 | Analyze (P); | |
1719 | Analyze_Call_And_Resolve; | |
1720 | ||
9f8d1e5c AC |
1721 | -- In Ada 2012. a qualified expression is a name, but it cannot be a |
1722 | -- procedure name, so the construct can only be a qualified expression. | |
1723 | ||
1724 | elsif Nkind (P) = N_Qualified_Expression | |
1725 | and then Ada_Version >= Ada_2012 | |
1726 | then | |
1727 | Rewrite (N, Make_Code_Statement (Loc, Expression => P)); | |
1728 | Analyze (N); | |
1729 | ||
e895b435 | 1730 | -- Anything else is an error |
996ae0b0 RK |
1731 | |
1732 | else | |
758c442c | 1733 | Error_Msg_N ("invalid procedure or entry call", N); |
996ae0b0 RK |
1734 | end if; |
1735 | end Analyze_Procedure_Call; | |
1736 | ||
b0186f71 AC |
1737 | ------------------------------ |
1738 | -- Analyze_Return_Statement -- | |
1739 | ------------------------------ | |
1740 | ||
1741 | procedure Analyze_Return_Statement (N : Node_Id) is | |
1742 | ||
1743 | pragma Assert (Nkind_In (N, N_Simple_Return_Statement, | |
1744 | N_Extended_Return_Statement)); | |
1745 | ||
1746 | Returns_Object : constant Boolean := | |
1747 | Nkind (N) = N_Extended_Return_Statement | |
1748 | or else | |
8fde064e AC |
1749 | (Nkind (N) = N_Simple_Return_Statement |
1750 | and then Present (Expression (N))); | |
b0186f71 AC |
1751 | -- True if we're returning something; that is, "return <expression>;" |
1752 | -- or "return Result : T [:= ...]". False for "return;". Used for error | |
1753 | -- checking: If Returns_Object is True, N should apply to a function | |
1754 | -- body; otherwise N should apply to a procedure body, entry body, | |
1755 | -- accept statement, or extended return statement. | |
1756 | ||
1757 | function Find_What_It_Applies_To return Entity_Id; | |
1758 | -- Find the entity representing the innermost enclosing body, accept | |
1759 | -- statement, or extended return statement. If the result is a callable | |
1760 | -- construct or extended return statement, then this will be the value | |
1761 | -- of the Return_Applies_To attribute. Otherwise, the program is | |
1762 | -- illegal. See RM-6.5(4/2). | |
1763 | ||
1764 | ----------------------------- | |
1765 | -- Find_What_It_Applies_To -- | |
1766 | ----------------------------- | |
1767 | ||
1768 | function Find_What_It_Applies_To return Entity_Id is | |
1769 | Result : Entity_Id := Empty; | |
1770 | ||
1771 | begin | |
36b8f95f AC |
1772 | -- Loop outward through the Scope_Stack, skipping blocks, loops, |
1773 | -- and postconditions. | |
b0186f71 AC |
1774 | |
1775 | for J in reverse 0 .. Scope_Stack.Last loop | |
1776 | Result := Scope_Stack.Table (J).Entity; | |
11bc76df AC |
1777 | exit when not Ekind_In (Result, E_Block, E_Loop) |
1778 | and then Chars (Result) /= Name_uPostconditions; | |
b0186f71 AC |
1779 | end loop; |
1780 | ||
1781 | pragma Assert (Present (Result)); | |
1782 | return Result; | |
1783 | end Find_What_It_Applies_To; | |
1784 | ||
1785 | -- Local declarations | |
1786 | ||
1787 | Scope_Id : constant Entity_Id := Find_What_It_Applies_To; | |
1788 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
1789 | Loc : constant Source_Ptr := Sloc (N); | |
1790 | Stm_Entity : constant Entity_Id := | |
1791 | New_Internal_Entity | |
1792 | (E_Return_Statement, Current_Scope, Loc, 'R'); | |
1793 | ||
1794 | -- Start of processing for Analyze_Return_Statement | |
1795 | ||
1796 | begin | |
1797 | Set_Return_Statement_Entity (N, Stm_Entity); | |
1798 | ||
1799 | Set_Etype (Stm_Entity, Standard_Void_Type); | |
1800 | Set_Return_Applies_To (Stm_Entity, Scope_Id); | |
1801 | ||
1802 | -- Place Return entity on scope stack, to simplify enforcement of 6.5 | |
1803 | -- (4/2): an inner return statement will apply to this extended return. | |
1804 | ||
1805 | if Nkind (N) = N_Extended_Return_Statement then | |
1806 | Push_Scope (Stm_Entity); | |
1807 | end if; | |
1808 | ||
1809 | -- Check that pragma No_Return is obeyed. Don't complain about the | |
1810 | -- implicitly-generated return that is placed at the end. | |
1811 | ||
1812 | if No_Return (Scope_Id) and then Comes_From_Source (N) then | |
1813 | Error_Msg_N ("RETURN statement not allowed (No_Return)", N); | |
1814 | end if; | |
1815 | ||
1816 | -- Warn on any unassigned OUT parameters if in procedure | |
1817 | ||
1818 | if Ekind (Scope_Id) = E_Procedure then | |
1819 | Warn_On_Unassigned_Out_Parameter (N, Scope_Id); | |
1820 | end if; | |
1821 | ||
1822 | -- Check that functions return objects, and other things do not | |
1823 | ||
1824 | if Kind = E_Function or else Kind = E_Generic_Function then | |
1825 | if not Returns_Object then | |
1826 | Error_Msg_N ("missing expression in return from function", N); | |
1827 | end if; | |
1828 | ||
1829 | elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
1830 | if Returns_Object then | |
1831 | Error_Msg_N ("procedure cannot return value (use function)", N); | |
1832 | end if; | |
1833 | ||
1834 | elsif Kind = E_Entry or else Kind = E_Entry_Family then | |
1835 | if Returns_Object then | |
1836 | if Is_Protected_Type (Scope (Scope_Id)) then | |
1837 | Error_Msg_N ("entry body cannot return value", N); | |
1838 | else | |
1839 | Error_Msg_N ("accept statement cannot return value", N); | |
1840 | end if; | |
1841 | end if; | |
1842 | ||
1843 | elsif Kind = E_Return_Statement then | |
1844 | ||
1845 | -- We are nested within another return statement, which must be an | |
1846 | -- extended_return_statement. | |
1847 | ||
1848 | if Returns_Object then | |
d0dcb2b1 AC |
1849 | if Nkind (N) = N_Extended_Return_Statement then |
1850 | Error_Msg_N | |
cc96a1b8 | 1851 | ("extended return statement cannot be nested (use `RETURN;`)", |
d0dcb2b1 AC |
1852 | N); |
1853 | ||
1854 | -- Case of a simple return statement with a value inside extended | |
1855 | -- return statement. | |
1856 | ||
1857 | else | |
1858 | Error_Msg_N | |
3ccedacc AC |
1859 | ("return nested in extended return statement cannot return " |
1860 | & "value (use `RETURN;`)", N); | |
d0dcb2b1 | 1861 | end if; |
b0186f71 AC |
1862 | end if; |
1863 | ||
1864 | else | |
1865 | Error_Msg_N ("illegal context for return statement", N); | |
1866 | end if; | |
1867 | ||
1868 | if Ekind_In (Kind, E_Function, E_Generic_Function) then | |
1869 | Analyze_Function_Return (N); | |
1870 | ||
1871 | elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then | |
1872 | Set_Return_Present (Scope_Id); | |
1873 | end if; | |
1874 | ||
1875 | if Nkind (N) = N_Extended_Return_Statement then | |
1876 | End_Scope; | |
1877 | end if; | |
1878 | ||
1879 | Kill_Current_Values (Last_Assignment_Only => True); | |
1880 | Check_Unreachable_Code (N); | |
dec6faf1 AC |
1881 | |
1882 | Analyze_Dimension (N); | |
b0186f71 AC |
1883 | end Analyze_Return_Statement; |
1884 | ||
5d37ba92 ES |
1885 | ------------------------------------- |
1886 | -- Analyze_Simple_Return_Statement -- | |
1887 | ------------------------------------- | |
ec4867fa | 1888 | |
5d37ba92 | 1889 | procedure Analyze_Simple_Return_Statement (N : Node_Id) is |
996ae0b0 | 1890 | begin |
5d37ba92 ES |
1891 | if Present (Expression (N)) then |
1892 | Mark_Coextensions (N, Expression (N)); | |
996ae0b0 RK |
1893 | end if; |
1894 | ||
5d37ba92 ES |
1895 | Analyze_Return_Statement (N); |
1896 | end Analyze_Simple_Return_Statement; | |
996ae0b0 | 1897 | |
82c80734 RD |
1898 | ------------------------- |
1899 | -- Analyze_Return_Type -- | |
1900 | ------------------------- | |
1901 | ||
1902 | procedure Analyze_Return_Type (N : Node_Id) is | |
1903 | Designator : constant Entity_Id := Defining_Entity (N); | |
1904 | Typ : Entity_Id := Empty; | |
1905 | ||
1906 | begin | |
ec4867fa ES |
1907 | -- Normal case where result definition does not indicate an error |
1908 | ||
41251c60 JM |
1909 | if Result_Definition (N) /= Error then |
1910 | if Nkind (Result_Definition (N)) = N_Access_Definition then | |
ce5ba43a | 1911 | Check_SPARK_05_Restriction |
fe5d3068 | 1912 | ("access result is not allowed", Result_Definition (N)); |
daec8eeb | 1913 | |
b1c11e0e JM |
1914 | -- Ada 2005 (AI-254): Handle anonymous access to subprograms |
1915 | ||
1916 | declare | |
1917 | AD : constant Node_Id := | |
1918 | Access_To_Subprogram_Definition (Result_Definition (N)); | |
1919 | begin | |
1920 | if Present (AD) and then Protected_Present (AD) then | |
1921 | Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); | |
1922 | else | |
1923 | Typ := Access_Definition (N, Result_Definition (N)); | |
1924 | end if; | |
1925 | end; | |
1926 | ||
41251c60 JM |
1927 | Set_Parent (Typ, Result_Definition (N)); |
1928 | Set_Is_Local_Anonymous_Access (Typ); | |
1929 | Set_Etype (Designator, Typ); | |
1930 | ||
b66c3ff4 AC |
1931 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1932 | ||
1933 | Null_Exclusion_Static_Checks (N); | |
1934 | ||
41251c60 JM |
1935 | -- Subtype_Mark case |
1936 | ||
1937 | else | |
1938 | Find_Type (Result_Definition (N)); | |
1939 | Typ := Entity (Result_Definition (N)); | |
1940 | Set_Etype (Designator, Typ); | |
1941 | ||
2ba431e5 | 1942 | -- Unconstrained array as result is not allowed in SPARK |
daec8eeb | 1943 | |
8fde064e | 1944 | if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then |
ce5ba43a | 1945 | Check_SPARK_05_Restriction |
fe5d3068 | 1946 | ("returning an unconstrained array is not allowed", |
7394c8cc | 1947 | Result_Definition (N)); |
daec8eeb YM |
1948 | end if; |
1949 | ||
b66c3ff4 AC |
1950 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1951 | ||
1952 | Null_Exclusion_Static_Checks (N); | |
1953 | ||
1954 | -- If a null exclusion is imposed on the result type, then create | |
1955 | -- a null-excluding itype (an access subtype) and use it as the | |
1956 | -- function's Etype. Note that the null exclusion checks are done | |
1957 | -- right before this, because they don't get applied to types that | |
1958 | -- do not come from source. | |
1959 | ||
8fde064e | 1960 | if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then |
b66c3ff4 AC |
1961 | Set_Etype (Designator, |
1962 | Create_Null_Excluding_Itype | |
ff7139c3 AC |
1963 | (T => Typ, |
1964 | Related_Nod => N, | |
1965 | Scope_Id => Scope (Current_Scope))); | |
1966 | ||
1967 | -- The new subtype must be elaborated before use because | |
1968 | -- it is visible outside of the function. However its base | |
1969 | -- type may not be frozen yet, so the reference that will | |
1970 | -- force elaboration must be attached to the freezing of | |
1971 | -- the base type. | |
1972 | ||
212863c0 AC |
1973 | -- If the return specification appears on a proper body, |
1974 | -- the subtype will have been created already on the spec. | |
1975 | ||
ff7139c3 | 1976 | if Is_Frozen (Typ) then |
212863c0 AC |
1977 | if Nkind (Parent (N)) = N_Subprogram_Body |
1978 | and then Nkind (Parent (Parent (N))) = N_Subunit | |
1979 | then | |
1980 | null; | |
1981 | else | |
1982 | Build_Itype_Reference (Etype (Designator), Parent (N)); | |
1983 | end if; | |
1984 | ||
ff7139c3 AC |
1985 | else |
1986 | Ensure_Freeze_Node (Typ); | |
1987 | ||
1988 | declare | |
212863c0 | 1989 | IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); |
ff7139c3 AC |
1990 | begin |
1991 | Set_Itype (IR, Etype (Designator)); | |
1992 | Append_Freeze_Actions (Typ, New_List (IR)); | |
1993 | end; | |
1994 | end if; | |
1995 | ||
b66c3ff4 AC |
1996 | else |
1997 | Set_Etype (Designator, Typ); | |
1998 | end if; | |
1999 | ||
41251c60 | 2000 | if Ekind (Typ) = E_Incomplete_Type |
0a36105d JM |
2001 | and then Is_Value_Type (Typ) |
2002 | then | |
2003 | null; | |
2004 | ||
2005 | elsif Ekind (Typ) = E_Incomplete_Type | |
41251c60 | 2006 | or else (Is_Class_Wide_Type (Typ) |
8fde064e | 2007 | and then Ekind (Root_Type (Typ)) = E_Incomplete_Type) |
41251c60 | 2008 | then |
dd386db0 AC |
2009 | -- AI05-0151: Tagged incomplete types are allowed in all formal |
2010 | -- parts. Untagged incomplete types are not allowed in bodies. | |
b973629e AC |
2011 | -- As a consequence, limited views cannot appear in a basic |
2012 | -- declaration that is itself within a body, because there is | |
2013 | -- no point at which the non-limited view will become visible. | |
dd386db0 AC |
2014 | |
2015 | if Ada_Version >= Ada_2012 then | |
b973629e AC |
2016 | if From_Limited_With (Typ) and then In_Package_Body then |
2017 | Error_Msg_NE | |
2018 | ("invalid use of incomplete type&", | |
3f80a182 | 2019 | Result_Definition (N), Typ); |
b973629e | 2020 | |
1ebc2612 AC |
2021 | -- The return type of a subprogram body cannot be of a |
2022 | -- formal incomplete type. | |
2023 | ||
2024 | elsif Is_Generic_Type (Typ) | |
2025 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2026 | then | |
2027 | Error_Msg_N | |
2028 | ("return type cannot be a formal incomplete type", | |
2029 | Result_Definition (N)); | |
2030 | ||
2031 | elsif Is_Class_Wide_Type (Typ) | |
2032 | and then Is_Generic_Type (Root_Type (Typ)) | |
2033 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2034 | then | |
2035 | Error_Msg_N | |
2036 | ("return type cannot be a formal incomplete type", | |
2037 | Result_Definition (N)); | |
2038 | ||
b973629e | 2039 | elsif Is_Tagged_Type (Typ) then |
dd386db0 AC |
2040 | null; |
2041 | ||
5b6f12c7 | 2042 | elsif Nkind (Parent (N)) = N_Subprogram_Body |
31d922e3 AC |
2043 | or else Nkind_In (Parent (Parent (N)), N_Accept_Statement, |
2044 | N_Entry_Body) | |
dd386db0 AC |
2045 | then |
2046 | Error_Msg_NE | |
2047 | ("invalid use of untagged incomplete type&", | |
2048 | Designator, Typ); | |
2049 | end if; | |
2050 | ||
63be2a5a | 2051 | -- The type must be completed in the current package. This |
31d922e3 | 2052 | -- is checked at the end of the package declaration when |
7b7a0c2b AC |
2053 | -- Taft-amendment types are identified. If the return type |
2054 | -- is class-wide, there is no required check, the type can | |
2055 | -- be a bona fide TAT. | |
63be2a5a AC |
2056 | |
2057 | if Ekind (Scope (Current_Scope)) = E_Package | |
c199ccf7 | 2058 | and then In_Private_Part (Scope (Current_Scope)) |
7b7a0c2b | 2059 | and then not Is_Class_Wide_Type (Typ) |
63be2a5a AC |
2060 | then |
2061 | Append_Elmt (Designator, Private_Dependents (Typ)); | |
2062 | end if; | |
2063 | ||
dd386db0 AC |
2064 | else |
2065 | Error_Msg_NE | |
2066 | ("invalid use of incomplete type&", Designator, Typ); | |
2067 | end if; | |
41251c60 | 2068 | end if; |
82c80734 RD |
2069 | end if; |
2070 | ||
ec4867fa ES |
2071 | -- Case where result definition does indicate an error |
2072 | ||
82c80734 RD |
2073 | else |
2074 | Set_Etype (Designator, Any_Type); | |
2075 | end if; | |
2076 | end Analyze_Return_Type; | |
2077 | ||
996ae0b0 RK |
2078 | ----------------------------- |
2079 | -- Analyze_Subprogram_Body -- | |
2080 | ----------------------------- | |
2081 | ||
b1b543d2 BD |
2082 | procedure Analyze_Subprogram_Body (N : Node_Id) is |
2083 | Loc : constant Source_Ptr := Sloc (N); | |
2084 | Body_Spec : constant Node_Id := Specification (N); | |
2085 | Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); | |
2086 | ||
2087 | begin | |
2088 | if Debug_Flag_C then | |
2089 | Write_Str ("==> subprogram body "); | |
2090 | Write_Name (Chars (Body_Id)); | |
2091 | Write_Str (" from "); | |
2092 | Write_Location (Loc); | |
2093 | Write_Eol; | |
2094 | Indent; | |
2095 | end if; | |
2096 | ||
2097 | Trace_Scope (N, Body_Id, " Analyze subprogram: "); | |
2098 | ||
2099 | -- The real work is split out into the helper, so it can do "return;" | |
2100 | -- without skipping the debug output: | |
2101 | ||
2102 | Analyze_Subprogram_Body_Helper (N); | |
2103 | ||
2104 | if Debug_Flag_C then | |
2105 | Outdent; | |
2106 | Write_Str ("<== subprogram body "); | |
2107 | Write_Name (Chars (Body_Id)); | |
2108 | Write_Str (" from "); | |
2109 | Write_Location (Loc); | |
2110 | Write_Eol; | |
2111 | end if; | |
2112 | end Analyze_Subprogram_Body; | |
2113 | ||
ea3c0651 AC |
2114 | -------------------------------------- |
2115 | -- Analyze_Subprogram_Body_Contract -- | |
2116 | -------------------------------------- | |
2117 | ||
ab8843fa | 2118 | procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is |
2c8d828a | 2119 | Body_Decl : constant Node_Id := Parent (Parent (Body_Id)); |
c61ef416 | 2120 | Mode : SPARK_Mode_Type; |
54e28df2 HK |
2121 | Prag : Node_Id; |
2122 | Ref_Depends : Node_Id := Empty; | |
2123 | Ref_Global : Node_Id := Empty; | |
2c8d828a | 2124 | Spec_Id : Entity_Id; |
ea3c0651 | 2125 | |
ea3c0651 | 2126 | begin |
fc999c5d RD |
2127 | -- Due to the timing of contract analysis, delayed pragmas may be |
2128 | -- subject to the wrong SPARK_Mode, usually that of the enclosing | |
2129 | -- context. To remedy this, restore the original SPARK_Mode of the | |
2130 | -- related subprogram body. | |
2131 | ||
c61ef416 AC |
2132 | Save_SPARK_Mode_And_Set (Body_Id, Mode); |
2133 | ||
77a40ec1 AC |
2134 | -- When a subprogram body declaration is illegal, its defining entity is |
2135 | -- left unanalyzed. There is nothing left to do in this case because the | |
dda38714 | 2136 | -- body lacks a contract, or even a proper Ekind. |
ab8843fa | 2137 | |
dda38714 | 2138 | if Ekind (Body_Id) = E_Void then |
ab8843fa HK |
2139 | return; |
2140 | end if; | |
2141 | ||
2c8d828a AC |
2142 | if Nkind (Body_Decl) = N_Subprogram_Body_Stub then |
2143 | Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl); | |
2144 | else | |
2145 | Spec_Id := Corresponding_Spec (Body_Decl); | |
2146 | end if; | |
2147 | ||
54e28df2 HK |
2148 | -- Locate and store pragmas Refined_Depends and Refined_Global since |
2149 | -- their order of analysis matters. | |
2150 | ||
ab8843fa HK |
2151 | Prag := Classifications (Contract (Body_Id)); |
2152 | while Present (Prag) loop | |
2153 | if Pragma_Name (Prag) = Name_Refined_Depends then | |
54e28df2 | 2154 | Ref_Depends := Prag; |
ab8843fa | 2155 | elsif Pragma_Name (Prag) = Name_Refined_Global then |
54e28df2 | 2156 | Ref_Global := Prag; |
ab8843fa HK |
2157 | end if; |
2158 | ||
2159 | Prag := Next_Pragma (Prag); | |
2160 | end loop; | |
2161 | ||
54e28df2 HK |
2162 | -- Analyze Refined_Global first as Refined_Depends may mention items |
2163 | -- classified in the global refinement. | |
2164 | ||
2165 | if Present (Ref_Global) then | |
2166 | Analyze_Refined_Global_In_Decl_Part (Ref_Global); | |
2167 | ||
ab8843fa | 2168 | -- When the corresponding Global aspect/pragma references a state with |
2dade097 HK |
2169 | -- visible refinement, the body requires Refined_Global. Refinement is |
2170 | -- not required when SPARK checks are suppressed. | |
ab8843fa | 2171 | |
54e28df2 | 2172 | elsif Present (Spec_Id) then |
ab8843fa HK |
2173 | Prag := Get_Pragma (Spec_Id, Pragma_Global); |
2174 | ||
2dade097 HK |
2175 | if SPARK_Mode /= Off |
2176 | and then Present (Prag) | |
2177 | and then Contains_Refined_State (Prag) | |
2178 | then | |
ab8843fa | 2179 | Error_Msg_NE |
3ccedacc | 2180 | ("body of subprogram& requires global refinement", |
ab8843fa HK |
2181 | Body_Decl, Spec_Id); |
2182 | end if; | |
2183 | end if; | |
54e28df2 HK |
2184 | |
2185 | -- Refined_Depends must be analyzed after Refined_Global in order to see | |
2186 | -- the modes of all global refinements. | |
2187 | ||
2188 | if Present (Ref_Depends) then | |
2189 | Analyze_Refined_Depends_In_Decl_Part (Ref_Depends); | |
39d3009f AC |
2190 | |
2191 | -- When the corresponding Depends aspect/pragma references a state with | |
2dade097 HK |
2192 | -- visible refinement, the body requires Refined_Depends. Refinement is |
2193 | -- not required when SPARK checks are suppressed. | |
39d3009f AC |
2194 | |
2195 | elsif Present (Spec_Id) then | |
2196 | Prag := Get_Pragma (Spec_Id, Pragma_Depends); | |
2197 | ||
2dade097 HK |
2198 | if SPARK_Mode /= Off |
2199 | and then Present (Prag) | |
2200 | and then Contains_Refined_State (Prag) | |
2201 | then | |
39d3009f | 2202 | Error_Msg_NE |
3ccedacc | 2203 | ("body of subprogram& requires dependance refinement", |
39d3009f AC |
2204 | Body_Decl, Spec_Id); |
2205 | end if; | |
54e28df2 | 2206 | end if; |
c61ef416 | 2207 | |
fc999c5d RD |
2208 | -- Restore the SPARK_Mode of the enclosing context after all delayed |
2209 | -- pragmas have been analyzed. | |
2210 | ||
c61ef416 | 2211 | Restore_SPARK_Mode (Mode); |
ea3c0651 AC |
2212 | end Analyze_Subprogram_Body_Contract; |
2213 | ||
b1b543d2 BD |
2214 | ------------------------------------ |
2215 | -- Analyze_Subprogram_Body_Helper -- | |
2216 | ------------------------------------ | |
2217 | ||
996ae0b0 RK |
2218 | -- This procedure is called for regular subprogram bodies, generic bodies, |
2219 | -- and for subprogram stubs of both kinds. In the case of stubs, only the | |
2220 | -- specification matters, and is used to create a proper declaration for | |
2221 | -- the subprogram, or to perform conformance checks. | |
2222 | ||
b1b543d2 | 2223 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is |
fbf5a39b AC |
2224 | Loc : constant Source_Ptr := Sloc (N); |
2225 | Body_Spec : constant Node_Id := Specification (N); | |
2226 | Body_Id : Entity_Id := Defining_Entity (Body_Spec); | |
2227 | Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); | |
0868e09c | 2228 | Conformant : Boolean; |
21d27997 | 2229 | HSS : Node_Id; |
21d27997 RD |
2230 | Prot_Typ : Entity_Id := Empty; |
2231 | Spec_Id : Entity_Id; | |
2232 | Spec_Decl : Node_Id := Empty; | |
2233 | ||
2234 | Last_Real_Spec_Entity : Entity_Id := Empty; | |
2235 | -- When we analyze a separate spec, the entity chain ends up containing | |
2236 | -- the formals, as well as any itypes generated during analysis of the | |
2237 | -- default expressions for parameters, or the arguments of associated | |
2238 | -- precondition/postcondition pragmas (which are analyzed in the context | |
2239 | -- of the spec since they have visibility on formals). | |
2240 | -- | |
2241 | -- These entities belong with the spec and not the body. However we do | |
2242 | -- the analysis of the body in the context of the spec (again to obtain | |
2243 | -- visibility to the formals), and all the entities generated during | |
2244 | -- this analysis end up also chained to the entity chain of the spec. | |
2245 | -- But they really belong to the body, and there is circuitry to move | |
2246 | -- them from the spec to the body. | |
2247 | -- | |
2248 | -- However, when we do this move, we don't want to move the real spec | |
2249 | -- entities (first para above) to the body. The Last_Real_Spec_Entity | |
2250 | -- variable points to the last real spec entity, so we only move those | |
2251 | -- chained beyond that point. It is initialized to Empty to deal with | |
2252 | -- the case where there is no separate spec. | |
996ae0b0 | 2253 | |
c8a3028c AC |
2254 | procedure Analyze_Aspects_On_Body_Or_Stub; |
2255 | -- Analyze the aspect specifications of a subprogram body [stub]. It is | |
2256 | -- assumed that N has aspects. | |
2257 | ||
ac072cb2 AC |
2258 | function Body_Has_Contract return Boolean; |
2259 | -- Check whether unanalyzed body has an aspect or pragma that may | |
1399d355 | 2260 | -- generate a SPARK contract. |
ac072cb2 | 2261 | |
ec4867fa | 2262 | procedure Check_Anonymous_Return; |
e50e1c5e | 2263 | -- Ada 2005: if a function returns an access type that denotes a task, |
ec4867fa ES |
2264 | -- or a type that contains tasks, we must create a master entity for |
2265 | -- the anonymous type, which typically will be used in an allocator | |
2266 | -- in the body of the function. | |
2267 | ||
e660dbf7 JM |
2268 | procedure Check_Inline_Pragma (Spec : in out Node_Id); |
2269 | -- Look ahead to recognize a pragma that may appear after the body. | |
2270 | -- If there is a previous spec, check that it appears in the same | |
2271 | -- declarative part. If the pragma is Inline_Always, perform inlining | |
2272 | -- unconditionally, otherwise only if Front_End_Inlining is requested. | |
2273 | -- If the body acts as a spec, and inlining is required, we create a | |
2274 | -- subprogram declaration for it, in order to attach the body to inline. | |
21d27997 RD |
2275 | -- If pragma does not appear after the body, check whether there is |
2276 | -- an inline pragma before any local declarations. | |
c37bb106 | 2277 | |
7665e4bd AC |
2278 | procedure Check_Missing_Return; |
2279 | -- Checks for a function with a no return statements, and also performs | |
8d606a78 RD |
2280 | -- the warning checks implemented by Check_Returns. In formal mode, also |
2281 | -- verify that a function ends with a RETURN and that a procedure does | |
2282 | -- not contain any RETURN. | |
7665e4bd | 2283 | |
d44202ba HK |
2284 | function Disambiguate_Spec return Entity_Id; |
2285 | -- When a primitive is declared between the private view and the full | |
2286 | -- view of a concurrent type which implements an interface, a special | |
2287 | -- mechanism is used to find the corresponding spec of the primitive | |
2288 | -- body. | |
2289 | ||
5dcab3ca AC |
2290 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id); |
2291 | -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains | |
2292 | -- incomplete types coming from a limited context and swap their limited | |
2293 | -- views with the non-limited ones. | |
2294 | ||
d44202ba HK |
2295 | function Is_Private_Concurrent_Primitive |
2296 | (Subp_Id : Entity_Id) return Boolean; | |
2297 | -- Determine whether subprogram Subp_Id is a primitive of a concurrent | |
2298 | -- type that implements an interface and has a private view. | |
2299 | ||
76a69663 ES |
2300 | procedure Set_Trivial_Subprogram (N : Node_Id); |
2301 | -- Sets the Is_Trivial_Subprogram flag in both spec and body of the | |
2302 | -- subprogram whose body is being analyzed. N is the statement node | |
2303 | -- causing the flag to be set, if the following statement is a return | |
2304 | -- of an entity, we mark the entity as set in source to suppress any | |
2305 | -- warning on the stylized use of function stubs with a dummy return. | |
2306 | ||
758c442c GD |
2307 | procedure Verify_Overriding_Indicator; |
2308 | -- If there was a previous spec, the entity has been entered in the | |
2309 | -- current scope previously. If the body itself carries an overriding | |
2310 | -- indicator, check that it is consistent with the known status of the | |
2311 | -- entity. | |
2312 | ||
c8a3028c AC |
2313 | ------------------------------------- |
2314 | -- Analyze_Aspects_On_Body_Or_Stub -- | |
2315 | ------------------------------------- | |
2316 | ||
2317 | procedure Analyze_Aspects_On_Body_Or_Stub is | |
2318 | procedure Diagnose_Misplaced_Aspects; | |
2319 | -- Subprogram body [stub] N has aspects, but they are not properly | |
2320 | -- placed. Provide precise diagnostics depending on the aspects | |
2321 | -- involved. | |
2322 | ||
2323 | -------------------------------- | |
2324 | -- Diagnose_Misplaced_Aspects -- | |
2325 | -------------------------------- | |
2326 | ||
2327 | procedure Diagnose_Misplaced_Aspects is | |
2328 | Asp : Node_Id; | |
2329 | Asp_Nam : Name_Id; | |
2330 | Asp_Id : Aspect_Id; | |
2331 | -- The current aspect along with its name and id | |
2332 | ||
2333 | procedure SPARK_Aspect_Error (Ref_Nam : Name_Id); | |
2334 | -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is | |
2335 | -- the name of the refined version of the aspect. | |
2336 | ||
2337 | ------------------------ | |
2338 | -- SPARK_Aspect_Error -- | |
2339 | ------------------------ | |
2340 | ||
2341 | procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is | |
2342 | begin | |
2343 | -- The corresponding spec already contains the aspect in | |
2344 | -- question and the one appearing on the body must be the | |
2345 | -- refined form: | |
2346 | ||
2347 | -- procedure P with Global ...; | |
2348 | -- procedure P with Global ... is ... end P; | |
2349 | -- ^ | |
2350 | -- Refined_Global | |
2351 | ||
2352 | if Has_Aspect (Spec_Id, Asp_Id) then | |
2353 | Error_Msg_Name_1 := Asp_Nam; | |
2354 | ||
2355 | -- Subunits cannot carry aspects that apply to a subprogram | |
2356 | -- declaration. | |
2357 | ||
2358 | if Nkind (Parent (N)) = N_Subunit then | |
2359 | Error_Msg_N ("aspect % cannot apply to a subunit", Asp); | |
2360 | ||
2361 | else | |
2362 | Error_Msg_Name_2 := Ref_Nam; | |
2363 | Error_Msg_N ("aspect % should be %", Asp); | |
2364 | end if; | |
2365 | ||
2366 | -- Otherwise the aspect must appear in the spec, not in the | |
2367 | -- body: | |
2368 | ||
2369 | -- procedure P; | |
2370 | -- procedure P with Global ... is ... end P; | |
2371 | ||
2372 | else | |
2373 | Error_Msg_N | |
2374 | ("aspect specification must appear in subprogram " | |
2375 | & "declaration", Asp); | |
2376 | end if; | |
2377 | end SPARK_Aspect_Error; | |
2378 | ||
2379 | -- Start of processing for Diagnose_Misplaced_Aspects | |
2380 | ||
2381 | begin | |
2382 | -- Iterate over the aspect specifications and emit specific errors | |
2383 | -- where applicable. | |
2384 | ||
2385 | Asp := First (Aspect_Specifications (N)); | |
2386 | while Present (Asp) loop | |
2387 | Asp_Nam := Chars (Identifier (Asp)); | |
2388 | Asp_Id := Get_Aspect_Id (Asp_Nam); | |
2389 | ||
2390 | -- Do not emit errors on aspects that can appear on a | |
2391 | -- subprogram body. This scenario occurs when the aspect | |
2392 | -- specification list contains both misplaced and properly | |
2393 | -- placed aspects. | |
2394 | ||
2395 | if Aspect_On_Body_Or_Stub_OK (Asp_Id) then | |
2396 | null; | |
2397 | ||
2398 | -- Special diagnostics for SPARK aspects | |
2399 | ||
2400 | elsif Asp_Nam = Name_Depends then | |
2401 | SPARK_Aspect_Error (Name_Refined_Depends); | |
2402 | ||
2403 | elsif Asp_Nam = Name_Global then | |
2404 | SPARK_Aspect_Error (Name_Refined_Global); | |
2405 | ||
2406 | elsif Asp_Nam = Name_Post then | |
2407 | SPARK_Aspect_Error (Name_Refined_Post); | |
2408 | ||
2409 | else | |
2410 | Error_Msg_N | |
2411 | ("aspect specification must appear in subprogram " | |
2412 | & "declaration", Asp); | |
2413 | end if; | |
2414 | ||
2415 | Next (Asp); | |
2416 | end loop; | |
2417 | end Diagnose_Misplaced_Aspects; | |
2418 | ||
2419 | -- Start of processing for Analyze_Aspects_On_Body_Or_Stub | |
2420 | ||
2421 | begin | |
2422 | -- Language-defined aspects cannot be associated with a subprogram | |
2423 | -- body [stub] if the subprogram has a spec. Certain implementation | |
2424 | -- defined aspects are allowed to break this rule (for list, see | |
2425 | -- table Aspect_On_Body_Or_Stub_OK). | |
2426 | ||
2427 | if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then | |
2428 | Diagnose_Misplaced_Aspects; | |
2429 | else | |
2430 | Analyze_Aspect_Specifications (N, Body_Id); | |
2431 | end if; | |
2432 | end Analyze_Aspects_On_Body_Or_Stub; | |
2433 | ||
ac072cb2 AC |
2434 | ----------------------- |
2435 | -- Body_Has_Contract -- | |
2436 | ----------------------- | |
2437 | ||
2438 | function Body_Has_Contract return Boolean is | |
2439 | Decls : constant List_Id := Declarations (N); | |
2440 | A_Spec : Node_Id; | |
2441 | A : Aspect_Id; | |
2442 | Decl : Node_Id; | |
2443 | P_Id : Pragma_Id; | |
2444 | ||
2445 | begin | |
2446 | -- Check for unanalyzed aspects in the body that will | |
2447 | -- generate a contract. | |
2448 | ||
2449 | if Present (Aspect_Specifications (N)) then | |
2450 | A_Spec := First (Aspect_Specifications (N)); | |
2451 | while Present (A_Spec) loop | |
2452 | A := Get_Aspect_Id (Chars (Identifier (A_Spec))); | |
2453 | ||
1399d355 AC |
2454 | if A = Aspect_Contract_Cases or else |
2455 | A = Aspect_Depends or else | |
2456 | A = Aspect_Global or else | |
2457 | A = Aspect_Pre or else | |
2458 | A = Aspect_Precondition or else | |
2459 | A = Aspect_Post or else | |
2460 | A = Aspect_Postcondition | |
ac072cb2 AC |
2461 | then |
2462 | return True; | |
2463 | end if; | |
2464 | ||
2465 | Next (A_Spec); | |
2466 | end loop; | |
2467 | end if; | |
2468 | ||
1399d355 | 2469 | -- Check for pragmas that may generate a contract |
ac072cb2 AC |
2470 | |
2471 | if Present (Decls) then | |
2472 | Decl := First (Decls); | |
2473 | while Present (Decl) loop | |
2474 | if Nkind (Decl) = N_Pragma then | |
2475 | P_Id := Get_Pragma_Id (Pragma_Name (Decl)); | |
2476 | ||
1399d355 AC |
2477 | if P_Id = Pragma_Contract_Cases or else |
2478 | P_Id = Pragma_Depends or else | |
2479 | P_Id = Pragma_Global or else | |
2480 | P_Id = Pragma_Pre or else | |
2481 | P_Id = Pragma_Precondition or else | |
2482 | P_Id = Pragma_Post or else | |
2483 | P_Id = Pragma_Postcondition | |
ac072cb2 AC |
2484 | then |
2485 | return True; | |
2486 | end if; | |
2487 | end if; | |
2488 | ||
2489 | Next (Decl); | |
2490 | end loop; | |
2491 | end if; | |
2492 | ||
2493 | return False; | |
2494 | end Body_Has_Contract; | |
2495 | ||
ec4867fa ES |
2496 | ---------------------------- |
2497 | -- Check_Anonymous_Return -- | |
2498 | ---------------------------- | |
2499 | ||
2500 | procedure Check_Anonymous_Return is | |
2501 | Decl : Node_Id; | |
a523b302 | 2502 | Par : Node_Id; |
ec4867fa ES |
2503 | Scop : Entity_Id; |
2504 | ||
2505 | begin | |
2506 | if Present (Spec_Id) then | |
2507 | Scop := Spec_Id; | |
2508 | else | |
2509 | Scop := Body_Id; | |
2510 | end if; | |
2511 | ||
2512 | if Ekind (Scop) = E_Function | |
2513 | and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type | |
a523b302 JM |
2514 | and then not Is_Thunk (Scop) |
2515 | and then (Has_Task (Designated_Type (Etype (Scop))) | |
2516 | or else | |
2517 | (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) | |
2518 | and then | |
2519 | Is_Limited_Record (Designated_Type (Etype (Scop))))) | |
ec4867fa | 2520 | and then Expander_Active |
b20de9b9 | 2521 | |
8fde064e | 2522 | -- Avoid cases with no tasking support |
b20de9b9 AC |
2523 | |
2524 | and then RTE_Available (RE_Current_Master) | |
2525 | and then not Restriction_Active (No_Task_Hierarchy) | |
ec4867fa ES |
2526 | then |
2527 | Decl := | |
2528 | Make_Object_Declaration (Loc, | |
2529 | Defining_Identifier => | |
2530 | Make_Defining_Identifier (Loc, Name_uMaster), | |
2531 | Constant_Present => True, | |
2532 | Object_Definition => | |
e4494292 | 2533 | New_Occurrence_Of (RTE (RE_Master_Id), Loc), |
ec4867fa ES |
2534 | Expression => |
2535 | Make_Explicit_Dereference (Loc, | |
e4494292 | 2536 | New_Occurrence_Of (RTE (RE_Current_Master), Loc))); |
ec4867fa ES |
2537 | |
2538 | if Present (Declarations (N)) then | |
2539 | Prepend (Decl, Declarations (N)); | |
2540 | else | |
2541 | Set_Declarations (N, New_List (Decl)); | |
2542 | end if; | |
2543 | ||
2544 | Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); | |
2545 | Set_Has_Master_Entity (Scop); | |
a523b302 JM |
2546 | |
2547 | -- Now mark the containing scope as a task master | |
2548 | ||
2549 | Par := N; | |
2550 | while Nkind (Par) /= N_Compilation_Unit loop | |
2551 | Par := Parent (Par); | |
2552 | pragma Assert (Present (Par)); | |
2553 | ||
2554 | -- If we fall off the top, we are at the outer level, and | |
2555 | -- the environment task is our effective master, so nothing | |
2556 | -- to mark. | |
2557 | ||
2558 | if Nkind_In | |
2559 | (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) | |
2560 | then | |
2561 | Set_Is_Task_Master (Par, True); | |
2562 | exit; | |
2563 | end if; | |
2564 | end loop; | |
ec4867fa ES |
2565 | end if; |
2566 | end Check_Anonymous_Return; | |
2567 | ||
e660dbf7 JM |
2568 | ------------------------- |
2569 | -- Check_Inline_Pragma -- | |
2570 | ------------------------- | |
758c442c | 2571 | |
e660dbf7 JM |
2572 | procedure Check_Inline_Pragma (Spec : in out Node_Id) is |
2573 | Prag : Node_Id; | |
2574 | Plist : List_Id; | |
0fb2ea01 | 2575 | |
21d27997 | 2576 | function Is_Inline_Pragma (N : Node_Id) return Boolean; |
30783513 | 2577 | -- True when N is a pragma Inline or Inline_Always that applies |
33931112 | 2578 | -- to this subprogram. |
21d27997 RD |
2579 | |
2580 | ----------------------- | |
2581 | -- Is_Inline_Pragma -- | |
2582 | ----------------------- | |
2583 | ||
2584 | function Is_Inline_Pragma (N : Node_Id) return Boolean is | |
2585 | begin | |
2586 | return | |
2587 | Nkind (N) = N_Pragma | |
2588 | and then | |
8fde064e AC |
2589 | (Pragma_Name (N) = Name_Inline_Always |
2590 | or else | |
21d27997 RD |
2591 | (Front_End_Inlining |
2592 | and then Pragma_Name (N) = Name_Inline)) | |
2593 | and then | |
8fde064e AC |
2594 | Chars |
2595 | (Expression (First (Pragma_Argument_Associations (N)))) = | |
2596 | Chars (Body_Id); | |
21d27997 RD |
2597 | end Is_Inline_Pragma; |
2598 | ||
2599 | -- Start of processing for Check_Inline_Pragma | |
2600 | ||
c37bb106 | 2601 | begin |
e660dbf7 JM |
2602 | if not Expander_Active then |
2603 | return; | |
2604 | end if; | |
2605 | ||
2606 | if Is_List_Member (N) | |
2607 | and then Present (Next (N)) | |
21d27997 | 2608 | and then Is_Inline_Pragma (Next (N)) |
c37bb106 AC |
2609 | then |
2610 | Prag := Next (N); | |
2611 | ||
21d27997 RD |
2612 | elsif Nkind (N) /= N_Subprogram_Body_Stub |
2613 | and then Present (Declarations (N)) | |
2614 | and then Is_Inline_Pragma (First (Declarations (N))) | |
2615 | then | |
2616 | Prag := First (Declarations (N)); | |
2617 | ||
e660dbf7 JM |
2618 | else |
2619 | Prag := Empty; | |
c37bb106 | 2620 | end if; |
e660dbf7 JM |
2621 | |
2622 | if Present (Prag) then | |
2623 | if Present (Spec_Id) then | |
30196a76 | 2624 | if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then |
e660dbf7 JM |
2625 | Analyze (Prag); |
2626 | end if; | |
2627 | ||
2628 | else | |
d39d6bb8 | 2629 | -- Create a subprogram declaration, to make treatment uniform |
e660dbf7 JM |
2630 | |
2631 | declare | |
2632 | Subp : constant Entity_Id := | |
30196a76 | 2633 | Make_Defining_Identifier (Loc, Chars (Body_Id)); |
e660dbf7 | 2634 | Decl : constant Node_Id := |
30196a76 RD |
2635 | Make_Subprogram_Declaration (Loc, |
2636 | Specification => | |
2637 | New_Copy_Tree (Specification (N))); | |
2638 | ||
e660dbf7 JM |
2639 | begin |
2640 | Set_Defining_Unit_Name (Specification (Decl), Subp); | |
2641 | ||
2642 | if Present (First_Formal (Body_Id)) then | |
21d27997 | 2643 | Plist := Copy_Parameter_List (Body_Id); |
e660dbf7 JM |
2644 | Set_Parameter_Specifications |
2645 | (Specification (Decl), Plist); | |
2646 | end if; | |
2647 | ||
2648 | Insert_Before (N, Decl); | |
2649 | Analyze (Decl); | |
2650 | Analyze (Prag); | |
2651 | Set_Has_Pragma_Inline (Subp); | |
2652 | ||
76a69663 | 2653 | if Pragma_Name (Prag) = Name_Inline_Always then |
e660dbf7 | 2654 | Set_Is_Inlined (Subp); |
21d27997 | 2655 | Set_Has_Pragma_Inline_Always (Subp); |
e660dbf7 JM |
2656 | end if; |
2657 | ||
158d55fa AC |
2658 | -- Prior to copying the subprogram body to create a template |
2659 | -- for it for subsequent inlining, remove the pragma from | |
2660 | -- the current body so that the copy that will produce the | |
2661 | -- new body will start from a completely unanalyzed tree. | |
2662 | ||
2663 | if Nkind (Parent (Prag)) = N_Subprogram_Body then | |
2664 | Rewrite (Prag, Make_Null_Statement (Sloc (Prag))); | |
2665 | end if; | |
2666 | ||
e660dbf7 JM |
2667 | Spec := Subp; |
2668 | end; | |
2669 | end if; | |
2670 | end if; | |
2671 | end Check_Inline_Pragma; | |
2672 | ||
7665e4bd AC |
2673 | -------------------------- |
2674 | -- Check_Missing_Return -- | |
2675 | -------------------------- | |
2676 | ||
2677 | procedure Check_Missing_Return is | |
2678 | Id : Entity_Id; | |
2679 | Missing_Ret : Boolean; | |
2680 | ||
2681 | begin | |
2682 | if Nkind (Body_Spec) = N_Function_Specification then | |
2683 | if Present (Spec_Id) then | |
2684 | Id := Spec_Id; | |
2685 | else | |
2686 | Id := Body_Id; | |
2687 | end if; | |
2688 | ||
fe5d3068 | 2689 | if Return_Present (Id) then |
7665e4bd AC |
2690 | Check_Returns (HSS, 'F', Missing_Ret); |
2691 | ||
2692 | if Missing_Ret then | |
2693 | Set_Has_Missing_Return (Id); | |
2694 | end if; | |
2695 | ||
2aca76d6 AC |
2696 | elsif Is_Generic_Subprogram (Id) |
2697 | or else not Is_Machine_Code_Subprogram (Id) | |
7665e4bd AC |
2698 | then |
2699 | Error_Msg_N ("missing RETURN statement in function body", N); | |
2700 | end if; | |
2701 | ||
fe5d3068 | 2702 | -- If procedure with No_Return, check returns |
607d0635 | 2703 | |
fe5d3068 YM |
2704 | elsif Nkind (Body_Spec) = N_Procedure_Specification |
2705 | and then Present (Spec_Id) | |
2706 | and then No_Return (Spec_Id) | |
607d0635 | 2707 | then |
fe5d3068 YM |
2708 | Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); |
2709 | end if; | |
2710 | ||
ad05f2e9 | 2711 | -- Special checks in SPARK mode |
fe5d3068 YM |
2712 | |
2713 | if Nkind (Body_Spec) = N_Function_Specification then | |
7394c8cc | 2714 | |
ad05f2e9 | 2715 | -- In SPARK mode, last statement of a function should be a return |
fe5d3068 YM |
2716 | |
2717 | declare | |
2718 | Stat : constant Node_Id := Last_Source_Statement (HSS); | |
2719 | begin | |
2720 | if Present (Stat) | |
7394c8cc AC |
2721 | and then not Nkind_In (Stat, N_Simple_Return_Statement, |
2722 | N_Extended_Return_Statement) | |
fe5d3068 | 2723 | then |
ce5ba43a | 2724 | Check_SPARK_05_Restriction |
fe5d3068 YM |
2725 | ("last statement in function should be RETURN", Stat); |
2726 | end if; | |
2727 | end; | |
2728 | ||
ad05f2e9 | 2729 | -- In SPARK mode, verify that a procedure has no return |
fe5d3068 YM |
2730 | |
2731 | elsif Nkind (Body_Spec) = N_Procedure_Specification then | |
607d0635 AC |
2732 | if Present (Spec_Id) then |
2733 | Id := Spec_Id; | |
2734 | else | |
2735 | Id := Body_Id; | |
2736 | end if; | |
2737 | ||
8d606a78 RD |
2738 | -- Would be nice to point to return statement here, can we |
2739 | -- borrow the Check_Returns procedure here ??? | |
2740 | ||
607d0635 | 2741 | if Return_Present (Id) then |
ce5ba43a | 2742 | Check_SPARK_05_Restriction |
fe5d3068 | 2743 | ("procedure should not have RETURN", N); |
607d0635 | 2744 | end if; |
7665e4bd AC |
2745 | end if; |
2746 | end Check_Missing_Return; | |
2747 | ||
d44202ba HK |
2748 | ----------------------- |
2749 | -- Disambiguate_Spec -- | |
2750 | ----------------------- | |
2751 | ||
2752 | function Disambiguate_Spec return Entity_Id is | |
2753 | Priv_Spec : Entity_Id; | |
2754 | Spec_N : Entity_Id; | |
2755 | ||
2756 | procedure Replace_Types (To_Corresponding : Boolean); | |
2757 | -- Depending on the flag, replace the type of formal parameters of | |
2758 | -- Body_Id if it is a concurrent type implementing interfaces with | |
2759 | -- the corresponding record type or the other way around. | |
2760 | ||
2761 | procedure Replace_Types (To_Corresponding : Boolean) is | |
2762 | Formal : Entity_Id; | |
2763 | Formal_Typ : Entity_Id; | |
2764 | ||
2765 | begin | |
2766 | Formal := First_Formal (Body_Id); | |
2767 | while Present (Formal) loop | |
2768 | Formal_Typ := Etype (Formal); | |
2769 | ||
df3e68b1 HK |
2770 | if Is_Class_Wide_Type (Formal_Typ) then |
2771 | Formal_Typ := Root_Type (Formal_Typ); | |
2772 | end if; | |
2773 | ||
d44202ba HK |
2774 | -- From concurrent type to corresponding record |
2775 | ||
2776 | if To_Corresponding then | |
2777 | if Is_Concurrent_Type (Formal_Typ) | |
2778 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
2779 | and then Present (Interfaces ( | |
2780 | Corresponding_Record_Type (Formal_Typ))) | |
2781 | then | |
2782 | Set_Etype (Formal, | |
2783 | Corresponding_Record_Type (Formal_Typ)); | |
2784 | end if; | |
2785 | ||
2786 | -- From corresponding record to concurrent type | |
2787 | ||
2788 | else | |
2789 | if Is_Concurrent_Record_Type (Formal_Typ) | |
2790 | and then Present (Interfaces (Formal_Typ)) | |
2791 | then | |
2792 | Set_Etype (Formal, | |
2793 | Corresponding_Concurrent_Type (Formal_Typ)); | |
2794 | end if; | |
2795 | end if; | |
2796 | ||
2797 | Next_Formal (Formal); | |
2798 | end loop; | |
2799 | end Replace_Types; | |
2800 | ||
2801 | -- Start of processing for Disambiguate_Spec | |
2802 | ||
2803 | begin | |
2804 | -- Try to retrieve the specification of the body as is. All error | |
2805 | -- messages are suppressed because the body may not have a spec in | |
2806 | -- its current state. | |
2807 | ||
2808 | Spec_N := Find_Corresponding_Spec (N, False); | |
2809 | ||
2810 | -- It is possible that this is the body of a primitive declared | |
2811 | -- between a private and a full view of a concurrent type. The | |
2812 | -- controlling parameter of the spec carries the concurrent type, | |
2813 | -- not the corresponding record type as transformed by Analyze_ | |
2814 | -- Subprogram_Specification. In such cases, we undo the change | |
2815 | -- made by the analysis of the specification and try to find the | |
2816 | -- spec again. | |
766d7add | 2817 | |
8198b93d HK |
2818 | -- Note that wrappers already have their corresponding specs and |
2819 | -- bodies set during their creation, so if the candidate spec is | |
16b05213 | 2820 | -- a wrapper, then we definitely need to swap all types to their |
8198b93d | 2821 | -- original concurrent status. |
d44202ba | 2822 | |
8198b93d HK |
2823 | if No (Spec_N) |
2824 | or else Is_Primitive_Wrapper (Spec_N) | |
2825 | then | |
d44202ba HK |
2826 | -- Restore all references of corresponding record types to the |
2827 | -- original concurrent types. | |
2828 | ||
2829 | Replace_Types (To_Corresponding => False); | |
2830 | Priv_Spec := Find_Corresponding_Spec (N, False); | |
2831 | ||
2832 | -- The current body truly belongs to a primitive declared between | |
2833 | -- a private and a full view. We leave the modified body as is, | |
2834 | -- and return the true spec. | |
2835 | ||
2836 | if Present (Priv_Spec) | |
2837 | and then Is_Private_Primitive (Priv_Spec) | |
2838 | then | |
2839 | return Priv_Spec; | |
2840 | end if; | |
2841 | ||
2842 | -- In case that this is some sort of error, restore the original | |
2843 | -- state of the body. | |
2844 | ||
2845 | Replace_Types (To_Corresponding => True); | |
2846 | end if; | |
2847 | ||
2848 | return Spec_N; | |
2849 | end Disambiguate_Spec; | |
2850 | ||
5dcab3ca AC |
2851 | ---------------------------- |
2852 | -- Exchange_Limited_Views -- | |
2853 | ---------------------------- | |
2854 | ||
2855 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is | |
2856 | procedure Detect_And_Exchange (Id : Entity_Id); | |
2857 | -- Determine whether Id's type denotes an incomplete type associated | |
2858 | -- with a limited with clause and exchange the limited view with the | |
2859 | -- non-limited one. | |
2860 | ||
2861 | ------------------------- | |
2862 | -- Detect_And_Exchange -- | |
2863 | ------------------------- | |
2864 | ||
2865 | procedure Detect_And_Exchange (Id : Entity_Id) is | |
2866 | Typ : constant Entity_Id := Etype (Id); | |
2867 | ||
2868 | begin | |
2869 | if Ekind (Typ) = E_Incomplete_Type | |
7b56a91b | 2870 | and then From_Limited_With (Typ) |
5dcab3ca AC |
2871 | and then Present (Non_Limited_View (Typ)) |
2872 | then | |
2873 | Set_Etype (Id, Non_Limited_View (Typ)); | |
2874 | end if; | |
2875 | end Detect_And_Exchange; | |
2876 | ||
2877 | -- Local variables | |
2878 | ||
2879 | Formal : Entity_Id; | |
2880 | ||
2881 | -- Start of processing for Exchange_Limited_Views | |
2882 | ||
2883 | begin | |
2884 | if No (Subp_Id) then | |
2885 | return; | |
2886 | ||
2887 | -- Do not process subprogram bodies as they already use the non- | |
2888 | -- limited view of types. | |
2889 | ||
2890 | elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then | |
2891 | return; | |
2892 | end if; | |
2893 | ||
2894 | -- Examine all formals and swap views when applicable | |
2895 | ||
2896 | Formal := First_Formal (Subp_Id); | |
2897 | while Present (Formal) loop | |
2898 | Detect_And_Exchange (Formal); | |
2899 | ||
2900 | Next_Formal (Formal); | |
2901 | end loop; | |
2902 | ||
2903 | -- Process the return type of a function | |
2904 | ||
2905 | if Ekind (Subp_Id) = E_Function then | |
2906 | Detect_And_Exchange (Subp_Id); | |
2907 | end if; | |
2908 | end Exchange_Limited_Views; | |
2909 | ||
d44202ba HK |
2910 | ------------------------------------- |
2911 | -- Is_Private_Concurrent_Primitive -- | |
2912 | ------------------------------------- | |
2913 | ||
2914 | function Is_Private_Concurrent_Primitive | |
2915 | (Subp_Id : Entity_Id) return Boolean | |
2916 | is | |
2917 | Formal_Typ : Entity_Id; | |
2918 | ||
2919 | begin | |
2920 | if Present (First_Formal (Subp_Id)) then | |
2921 | Formal_Typ := Etype (First_Formal (Subp_Id)); | |
2922 | ||
2923 | if Is_Concurrent_Record_Type (Formal_Typ) then | |
df3e68b1 HK |
2924 | if Is_Class_Wide_Type (Formal_Typ) then |
2925 | Formal_Typ := Root_Type (Formal_Typ); | |
2926 | end if; | |
2927 | ||
d44202ba HK |
2928 | Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); |
2929 | end if; | |
2930 | ||
2931 | -- The type of the first formal is a concurrent tagged type with | |
2932 | -- a private view. | |
2933 | ||
2934 | return | |
2935 | Is_Concurrent_Type (Formal_Typ) | |
2936 | and then Is_Tagged_Type (Formal_Typ) | |
2937 | and then Has_Private_Declaration (Formal_Typ); | |
2938 | end if; | |
2939 | ||
2940 | return False; | |
2941 | end Is_Private_Concurrent_Primitive; | |
2942 | ||
76a69663 ES |
2943 | ---------------------------- |
2944 | -- Set_Trivial_Subprogram -- | |
2945 | ---------------------------- | |
2946 | ||
2947 | procedure Set_Trivial_Subprogram (N : Node_Id) is | |
2948 | Nxt : constant Node_Id := Next (N); | |
2949 | ||
2950 | begin | |
2951 | Set_Is_Trivial_Subprogram (Body_Id); | |
2952 | ||
2953 | if Present (Spec_Id) then | |
2954 | Set_Is_Trivial_Subprogram (Spec_Id); | |
2955 | end if; | |
2956 | ||
2957 | if Present (Nxt) | |
2958 | and then Nkind (Nxt) = N_Simple_Return_Statement | |
2959 | and then No (Next (Nxt)) | |
2960 | and then Present (Expression (Nxt)) | |
2961 | and then Is_Entity_Name (Expression (Nxt)) | |
2962 | then | |
2963 | Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); | |
2964 | end if; | |
2965 | end Set_Trivial_Subprogram; | |
2966 | ||
758c442c GD |
2967 | --------------------------------- |
2968 | -- Verify_Overriding_Indicator -- | |
2969 | --------------------------------- | |
2970 | ||
2971 | procedure Verify_Overriding_Indicator is | |
2972 | begin | |
21d27997 RD |
2973 | if Must_Override (Body_Spec) then |
2974 | if Nkind (Spec_Id) = N_Defining_Operator_Symbol | |
2975 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
2976 | then | |
2977 | null; | |
2978 | ||
038140ed | 2979 | elsif not Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2980 | Error_Msg_NE |
21d27997 | 2981 | ("subprogram& is not overriding", Body_Spec, Spec_Id); |
23e28b42 AC |
2982 | |
2983 | -- Overriding indicators aren't allowed for protected subprogram | |
2984 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
2985 | -- this to a warning if -gnatd.E is enabled. | |
2986 | ||
2987 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
2988 | Error_Msg_Warn := Error_To_Warning; | |
2989 | Error_Msg_N | |
b785e0b8 | 2990 | ("<<overriding indicator not allowed for protected " |
23e28b42 | 2991 | & "subprogram body", Body_Spec); |
21d27997 | 2992 | end if; |
758c442c | 2993 | |
5d37ba92 | 2994 | elsif Must_Not_Override (Body_Spec) then |
038140ed | 2995 | if Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2996 | Error_Msg_NE |
5d37ba92 | 2997 | ("subprogram& overrides inherited operation", |
76a69663 | 2998 | Body_Spec, Spec_Id); |
5d37ba92 | 2999 | |
21d27997 RD |
3000 | elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol |
3001 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
3002 | then | |
ed2233dc | 3003 | Error_Msg_NE |
3ccedacc | 3004 | ("subprogram& overrides predefined operator ", |
21d27997 RD |
3005 | Body_Spec, Spec_Id); |
3006 | ||
23e28b42 AC |
3007 | -- Overriding indicators aren't allowed for protected subprogram |
3008 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
3009 | -- this to a warning if -gnatd.E is enabled. | |
3010 | ||
3011 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
3012 | Error_Msg_Warn := Error_To_Warning; | |
5d37ba92 | 3013 | |
23e28b42 | 3014 | Error_Msg_N |
3ccedacc AC |
3015 | ("<<overriding indicator not allowed " |
3016 | & "for protected subprogram body", Body_Spec); | |
23e28b42 AC |
3017 | |
3018 | -- If this is not a primitive operation, then the overriding | |
3019 | -- indicator is altogether illegal. | |
3020 | ||
3021 | elsif not Is_Primitive (Spec_Id) then | |
ed2233dc | 3022 | Error_Msg_N |
3ccedacc AC |
3023 | ("overriding indicator only allowed " |
3024 | & "if subprogram is primitive", Body_Spec); | |
5d37ba92 | 3025 | end if; |
235f4375 | 3026 | |
23e28b42 AC |
3027 | -- If checking the style rule and the operation overrides, then |
3028 | -- issue a warning about a missing overriding_indicator. Protected | |
3029 | -- subprogram bodies are excluded from this style checking, since | |
3030 | -- they aren't primitives (even though their declarations can | |
3031 | -- override) and aren't allowed to have an overriding_indicator. | |
3032 | ||
806f6d37 | 3033 | elsif Style_Check |
038140ed | 3034 | and then Present (Overridden_Operation (Spec_Id)) |
23e28b42 | 3035 | and then Ekind (Scope (Spec_Id)) /= E_Protected_Type |
235f4375 AC |
3036 | then |
3037 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3038 | Style.Missing_Overriding (N, Body_Id); | |
806f6d37 AC |
3039 | |
3040 | elsif Style_Check | |
3041 | and then Can_Override_Operator (Spec_Id) | |
3042 | and then not Is_Predefined_File_Name | |
3043 | (Unit_File_Name (Get_Source_Unit (Spec_Id))) | |
3044 | then | |
3045 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3046 | Style.Missing_Overriding (N, Body_Id); | |
758c442c GD |
3047 | end if; |
3048 | end Verify_Overriding_Indicator; | |
3049 | ||
b1b543d2 | 3050 | -- Start of processing for Analyze_Subprogram_Body_Helper |
0fb2ea01 | 3051 | |
996ae0b0 | 3052 | begin |
82c80734 RD |
3053 | -- Generic subprograms are handled separately. They always have a |
3054 | -- generic specification. Determine whether current scope has a | |
3055 | -- previous declaration. | |
996ae0b0 | 3056 | |
82c80734 RD |
3057 | -- If the subprogram body is defined within an instance of the same |
3058 | -- name, the instance appears as a package renaming, and will be hidden | |
3059 | -- within the subprogram. | |
996ae0b0 RK |
3060 | |
3061 | if Present (Prev_Id) | |
3062 | and then not Is_Overloadable (Prev_Id) | |
3063 | and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration | |
3064 | or else Comes_From_Source (Prev_Id)) | |
3065 | then | |
fbf5a39b | 3066 | if Is_Generic_Subprogram (Prev_Id) then |
996ae0b0 RK |
3067 | Spec_Id := Prev_Id; |
3068 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
3069 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3070 | ||
3071 | Analyze_Generic_Subprogram_Body (N, Spec_Id); | |
7665e4bd AC |
3072 | |
3073 | if Nkind (N) = N_Subprogram_Body then | |
3074 | HSS := Handled_Statement_Sequence (N); | |
3075 | Check_Missing_Return; | |
3076 | end if; | |
3077 | ||
996ae0b0 RK |
3078 | return; |
3079 | ||
3080 | else | |
82c80734 RD |
3081 | -- Previous entity conflicts with subprogram name. Attempting to |
3082 | -- enter name will post error. | |
996ae0b0 RK |
3083 | |
3084 | Enter_Name (Body_Id); | |
3085 | return; | |
3086 | end if; | |
3087 | ||
82c80734 RD |
3088 | -- Non-generic case, find the subprogram declaration, if one was seen, |
3089 | -- or enter new overloaded entity in the current scope. If the | |
3090 | -- Current_Entity is the Body_Id itself, the unit is being analyzed as | |
3091 | -- part of the context of one of its subunits. No need to redo the | |
3092 | -- analysis. | |
996ae0b0 | 3093 | |
8fde064e | 3094 | elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then |
996ae0b0 RK |
3095 | return; |
3096 | ||
3097 | else | |
fbf5a39b | 3098 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); |
996ae0b0 RK |
3099 | |
3100 | if Nkind (N) = N_Subprogram_Body_Stub | |
3101 | or else No (Corresponding_Spec (N)) | |
3102 | then | |
d44202ba HK |
3103 | if Is_Private_Concurrent_Primitive (Body_Id) then |
3104 | Spec_Id := Disambiguate_Spec; | |
3105 | else | |
3106 | Spec_Id := Find_Corresponding_Spec (N); | |
b6c8e5be AC |
3107 | |
3108 | -- In GNATprove mode, if the body has no previous spec, create | |
3109 | -- one so that the inlining machinery can operate properly. | |
3110 | -- Transfer aspects, if any, to the new spec, so that they | |
3111 | -- are legal and can be processed ahead of the body. | |
3112 | -- We make two copies of the given spec, one for the new | |
3113 | -- declaration, and one for the body. | |
3114 | ||
b6c8e5be AC |
3115 | if No (Spec_Id) |
3116 | and then GNATprove_Mode | |
480156b2 | 3117 | |
480156b2 AC |
3118 | -- Inlining does not apply during pre-analysis of code |
3119 | ||
b6c8e5be | 3120 | and then Full_Analysis |
480156b2 AC |
3121 | |
3122 | -- Inlining only applies to full bodies, not stubs | |
3123 | ||
7b2888e6 | 3124 | and then Nkind (N) /= N_Subprogram_Body_Stub |
480156b2 AC |
3125 | |
3126 | -- Inlining only applies to bodies in the source code, not to | |
3127 | -- those generated by the compiler. In particular, expression | |
3128 | -- functions, whose body is generated by the compiler, are | |
3129 | -- treated specially by GNATprove. | |
3130 | ||
b6c8e5be | 3131 | and then Comes_From_Source (Body_Id) |
480156b2 AC |
3132 | |
3133 | -- This cannot be done for a compilation unit, which is not | |
3134 | -- in a context where we can insert a new spec. | |
3135 | ||
b6c8e5be | 3136 | and then Is_List_Member (N) |
480156b2 AC |
3137 | |
3138 | -- Inlining only applies to subprograms without contracts, | |
3139 | -- as a contract is a sign that GNATprove should perform a | |
3140 | -- modular analysis of the subprogram instead of a contextual | |
3141 | -- analysis at each call site. The same test is performed in | |
3142 | -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated | |
3143 | -- here in another form (because the contract has not | |
3144 | -- been attached to the body) to avoid frontend errors in | |
3145 | -- case pragmas are used instead of aspects, because the | |
3146 | -- corresponding pragmas in the body would not be transferred | |
3147 | -- to the spec, leading to legality errors. | |
3148 | ||
ac072cb2 | 3149 | and then not Body_Has_Contract |
b6c8e5be AC |
3150 | then |
3151 | declare | |
3152 | Body_Spec : constant Node_Id := | |
662c2ad4 | 3153 | Copy_Separate_Tree (Specification (N)); |
eb1ee757 AC |
3154 | New_Decl : constant Node_Id := |
3155 | Make_Subprogram_Declaration (Loc, | |
3156 | Copy_Separate_Tree (Specification (N))); | |
3157 | ||
2178830b | 3158 | SPARK_Mode_Aspect : Node_Id; |
eb1ee757 AC |
3159 | Aspects : List_Id; |
3160 | Prag, Aspect : Node_Id; | |
b6c8e5be AC |
3161 | |
3162 | begin | |
3163 | Insert_Before (N, New_Decl); | |
3164 | Move_Aspects (From => N, To => New_Decl); | |
2178830b AC |
3165 | |
3166 | -- Mark the newly moved aspects as not analyzed, so that | |
3167 | -- their effect on New_Decl is properly analyzed. | |
3168 | ||
3169 | Aspect := First (Aspect_Specifications (New_Decl)); | |
3170 | while Present (Aspect) loop | |
3171 | Set_Analyzed (Aspect, False); | |
3172 | Next (Aspect); | |
3173 | end loop; | |
3174 | ||
b6c8e5be | 3175 | Analyze (New_Decl); |
2178830b AC |
3176 | |
3177 | -- The analysis of the generated subprogram declaration | |
eb1ee757 | 3178 | -- may have introduced pragmas that need to be analyzed. |
2178830b AC |
3179 | |
3180 | Prag := Next (New_Decl); | |
3181 | while Prag /= N loop | |
3182 | Analyze (Prag); | |
3183 | Next (Prag); | |
3184 | end loop; | |
3185 | ||
b6c8e5be AC |
3186 | Spec_Id := Defining_Entity (New_Decl); |
3187 | ||
793c5f05 AC |
3188 | -- As Body_Id originally comes from source, mark the new |
3189 | -- Spec_Id as such, which is required so that calls to | |
3190 | -- this subprogram are registered in the local effects | |
3191 | -- stored in ALI files for GNATprove. | |
3192 | ||
3193 | Set_Comes_From_Source (Spec_Id, True); | |
3194 | ||
2178830b AC |
3195 | -- If aspect SPARK_Mode was specified on the body, it |
3196 | -- needs to be repeated on the generated decl and the | |
3197 | -- body. Since the original aspect was moved to the | |
3198 | -- generated decl, copy it for the body. | |
3199 | ||
3200 | if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then | |
3201 | SPARK_Mode_Aspect := | |
3202 | New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode)); | |
3203 | Set_Analyzed (SPARK_Mode_Aspect, False); | |
eb1ee757 | 3204 | Aspects := New_List (SPARK_Mode_Aspect); |
2178830b AC |
3205 | Set_Aspect_Specifications (N, Aspects); |
3206 | end if; | |
3207 | ||
b6c8e5be AC |
3208 | Set_Specification (N, Body_Spec); |
3209 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); | |
3210 | Set_Corresponding_Spec (N, Spec_Id); | |
3211 | end; | |
3212 | end if; | |
d44202ba | 3213 | end if; |
996ae0b0 RK |
3214 | |
3215 | -- If this is a duplicate body, no point in analyzing it | |
3216 | ||
3217 | if Error_Posted (N) then | |
3218 | return; | |
3219 | end if; | |
3220 | ||
82c80734 RD |
3221 | -- A subprogram body should cause freezing of its own declaration, |
3222 | -- but if there was no previous explicit declaration, then the | |
3223 | -- subprogram will get frozen too late (there may be code within | |
3224 | -- the body that depends on the subprogram having been frozen, | |
3225 | -- such as uses of extra formals), so we force it to be frozen | |
76a69663 | 3226 | -- here. Same holds if the body and spec are compilation units. |
cd1c668b ES |
3227 | -- Finally, if the return type is an anonymous access to protected |
3228 | -- subprogram, it must be frozen before the body because its | |
3229 | -- expansion has generated an equivalent type that is used when | |
3230 | -- elaborating the body. | |
996ae0b0 | 3231 | |
885c4871 | 3232 | -- An exception in the case of Ada 2012, AI05-177: The bodies |
ebb6faaa AC |
3233 | -- created for expression functions do not freeze. |
3234 | ||
3235 | if No (Spec_Id) | |
3236 | and then Nkind (Original_Node (N)) /= N_Expression_Function | |
3237 | then | |
996ae0b0 RK |
3238 | Freeze_Before (N, Body_Id); |
3239 | ||
3240 | elsif Nkind (Parent (N)) = N_Compilation_Unit then | |
3241 | Freeze_Before (N, Spec_Id); | |
cd1c668b ES |
3242 | |
3243 | elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then | |
3244 | Freeze_Before (N, Etype (Body_Id)); | |
996ae0b0 | 3245 | end if; |
a38ff9b1 | 3246 | |
996ae0b0 RK |
3247 | else |
3248 | Spec_Id := Corresponding_Spec (N); | |
3249 | end if; | |
3250 | end if; | |
3251 | ||
799d0e05 AC |
3252 | -- Previously we scanned the body to look for nested subprograms, and |
3253 | -- rejected an inline directive if nested subprograms were present, | |
3254 | -- because the back-end would generate conflicting symbols for the | |
c8957aae | 3255 | -- nested bodies. This is now unnecessary. |
07fc65c4 | 3256 | |
c8957aae | 3257 | -- Look ahead to recognize a pragma Inline that appears after the body |
84f4072a | 3258 | |
e660dbf7 JM |
3259 | Check_Inline_Pragma (Spec_Id); |
3260 | ||
701b7fbb RD |
3261 | -- Deal with special case of a fully private operation in the body of |
3262 | -- the protected type. We must create a declaration for the subprogram, | |
3263 | -- in order to attach the protected subprogram that will be used in | |
3264 | -- internal calls. We exclude compiler generated bodies from the | |
3265 | -- expander since the issue does not arise for those cases. | |
07fc65c4 | 3266 | |
996ae0b0 RK |
3267 | if No (Spec_Id) |
3268 | and then Comes_From_Source (N) | |
3269 | and then Is_Protected_Type (Current_Scope) | |
3270 | then | |
47bfea3a | 3271 | Spec_Id := Build_Private_Protected_Declaration (N); |
701b7fbb | 3272 | end if; |
996ae0b0 | 3273 | |
5334d18f | 3274 | -- If a separate spec is present, then deal with freezing issues |
7ca78bba | 3275 | |
701b7fbb | 3276 | if Present (Spec_Id) then |
996ae0b0 | 3277 | Spec_Decl := Unit_Declaration_Node (Spec_Id); |
758c442c | 3278 | Verify_Overriding_Indicator; |
5d37ba92 ES |
3279 | |
3280 | -- In general, the spec will be frozen when we start analyzing the | |
3281 | -- body. However, for internally generated operations, such as | |
3282 | -- wrapper functions for inherited operations with controlling | |
164e06c6 AC |
3283 | -- results, the spec may not have been frozen by the time we expand |
3284 | -- the freeze actions that include the bodies. In particular, extra | |
3285 | -- formals for accessibility or for return-in-place may need to be | |
3286 | -- generated. Freeze nodes, if any, are inserted before the current | |
3287 | -- body. These freeze actions are also needed in ASIS mode to enable | |
3288 | -- the proper back-annotations. | |
5d37ba92 ES |
3289 | |
3290 | if not Is_Frozen (Spec_Id) | |
7134062a | 3291 | and then (Expander_Active or ASIS_Mode) |
5d37ba92 ES |
3292 | then |
3293 | -- Force the generation of its freezing node to ensure proper | |
3294 | -- management of access types in the backend. | |
3295 | ||
3296 | -- This is definitely needed for some cases, but it is not clear | |
3297 | -- why, to be investigated further??? | |
3298 | ||
3299 | Set_Has_Delayed_Freeze (Spec_Id); | |
6b958cec | 3300 | Freeze_Before (N, Spec_Id); |
5d37ba92 | 3301 | end if; |
996ae0b0 RK |
3302 | end if; |
3303 | ||
a5d83d61 AC |
3304 | -- Mark presence of postcondition procedure in current scope and mark |
3305 | -- the procedure itself as needing debug info. The latter is important | |
3306 | -- when analyzing decision coverage (for example, for MC/DC coverage). | |
7ca78bba | 3307 | |
0dabde3a ES |
3308 | if Chars (Body_Id) = Name_uPostconditions then |
3309 | Set_Has_Postconditions (Current_Scope); | |
a5d83d61 | 3310 | Set_Debug_Info_Needed (Body_Id); |
0dabde3a ES |
3311 | end if; |
3312 | ||
996ae0b0 RK |
3313 | -- Place subprogram on scope stack, and make formals visible. If there |
3314 | -- is a spec, the visible entity remains that of the spec. | |
3315 | ||
3316 | if Present (Spec_Id) then | |
07fc65c4 | 3317 | Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); |
758c442c GD |
3318 | |
3319 | if Is_Child_Unit (Spec_Id) then | |
3320 | Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False); | |
3321 | end if; | |
3322 | ||
fbf5a39b AC |
3323 | if Style_Check then |
3324 | Style.Check_Identifier (Body_Id, Spec_Id); | |
3325 | end if; | |
996ae0b0 RK |
3326 | |
3327 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
3328 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3329 | ||
f937473f | 3330 | if Is_Abstract_Subprogram (Spec_Id) then |
ed2233dc | 3331 | Error_Msg_N ("an abstract subprogram cannot have a body", N); |
996ae0b0 | 3332 | return; |
21d27997 | 3333 | |
996ae0b0 RK |
3334 | else |
3335 | Set_Convention (Body_Id, Convention (Spec_Id)); | |
3336 | Set_Has_Completion (Spec_Id); | |
3337 | ||
c5cec2fe AC |
3338 | -- Inherit the "ghostness" of the subprogram spec. Note that this |
3339 | -- property is not directly inherited as the body may be subject | |
3340 | -- to a different Ghost assertion policy. | |
3341 | ||
3342 | if Is_Ghost_Entity (Spec_Id) or else Within_Ghost_Scope then | |
3343 | Set_Is_Ghost_Entity (Body_Id); | |
3344 | ||
3345 | -- The Ghost policy in effect at the point of declaration and | |
3346 | -- at the point of completion must match (SPARK RM 6.9(14)). | |
3347 | ||
3348 | Check_Ghost_Completion (Spec_Id, Body_Id); | |
3349 | end if; | |
3350 | ||
996ae0b0 | 3351 | if Is_Protected_Type (Scope (Spec_Id)) then |
21d27997 | 3352 | Prot_Typ := Scope (Spec_Id); |
996ae0b0 RK |
3353 | end if; |
3354 | ||
3355 | -- If this is a body generated for a renaming, do not check for | |
3356 | -- full conformance. The check is redundant, because the spec of | |
3357 | -- the body is a copy of the spec in the renaming declaration, | |
3358 | -- and the test can lead to spurious errors on nested defaults. | |
3359 | ||
3360 | if Present (Spec_Decl) | |
996ae0b0 | 3361 | and then not Comes_From_Source (N) |
93a81b02 GB |
3362 | and then |
3363 | (Nkind (Original_Node (Spec_Decl)) = | |
d2f97d3e | 3364 | N_Subprogram_Renaming_Declaration |
466c2127 AC |
3365 | or else (Present (Corresponding_Body (Spec_Decl)) |
3366 | and then | |
3367 | Nkind (Unit_Declaration_Node | |
3368 | (Corresponding_Body (Spec_Decl))) = | |
3369 | N_Subprogram_Renaming_Declaration)) | |
996ae0b0 RK |
3370 | then |
3371 | Conformant := True; | |
cabe9abc AC |
3372 | |
3373 | -- Conversely, the spec may have been generated for specless body | |
3374 | -- with an inline pragma. | |
3375 | ||
3376 | elsif Comes_From_Source (N) | |
3377 | and then not Comes_From_Source (Spec_Id) | |
3378 | and then Has_Pragma_Inline (Spec_Id) | |
3379 | then | |
3380 | Conformant := True; | |
76a69663 | 3381 | |
996ae0b0 RK |
3382 | else |
3383 | Check_Conformance | |
3384 | (Body_Id, Spec_Id, | |
76a69663 | 3385 | Fully_Conformant, True, Conformant, Body_Id); |
996ae0b0 RK |
3386 | end if; |
3387 | ||
3388 | -- If the body is not fully conformant, we have to decide if we | |
3389 | -- should analyze it or not. If it has a really messed up profile | |
3390 | -- then we probably should not analyze it, since we will get too | |
3391 | -- many bogus messages. | |
3392 | ||
3393 | -- Our decision is to go ahead in the non-fully conformant case | |
3394 | -- only if it is at least mode conformant with the spec. Note | |
3395 | -- that the call to Check_Fully_Conformant has issued the proper | |
3396 | -- error messages to complain about the lack of conformance. | |
3397 | ||
3398 | if not Conformant | |
3399 | and then not Mode_Conformant (Body_Id, Spec_Id) | |
3400 | then | |
3401 | return; | |
3402 | end if; | |
3403 | end if; | |
3404 | ||
996ae0b0 | 3405 | if Spec_Id /= Body_Id then |
fbf5a39b | 3406 | Reference_Body_Formals (Spec_Id, Body_Id); |
996ae0b0 RK |
3407 | end if; |
3408 | ||
579847c2 AC |
3409 | Set_Ekind (Body_Id, E_Subprogram_Body); |
3410 | ||
e28072cd AC |
3411 | if Nkind (N) = N_Subprogram_Body_Stub then |
3412 | Set_Corresponding_Spec_Of_Stub (N, Spec_Id); | |
3413 | ||
3414 | -- Regular body | |
3415 | ||
3416 | else | |
996ae0b0 | 3417 | Set_Corresponding_Spec (N, Spec_Id); |
758c442c | 3418 | |
5d37ba92 ES |
3419 | -- Ada 2005 (AI-345): If the operation is a primitive operation |
3420 | -- of a concurrent type, the type of the first parameter has been | |
3421 | -- replaced with the corresponding record, which is the proper | |
3422 | -- run-time structure to use. However, within the body there may | |
3423 | -- be uses of the formals that depend on primitive operations | |
3424 | -- of the type (in particular calls in prefixed form) for which | |
3425 | -- we need the original concurrent type. The operation may have | |
3426 | -- several controlling formals, so the replacement must be done | |
3427 | -- for all of them. | |
758c442c GD |
3428 | |
3429 | if Comes_From_Source (Spec_Id) | |
3430 | and then Present (First_Entity (Spec_Id)) | |
3431 | and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type | |
3432 | and then Is_Tagged_Type (Etype (First_Entity (Spec_Id))) | |
15918371 AC |
3433 | and then Present (Interfaces (Etype (First_Entity (Spec_Id)))) |
3434 | and then Present (Corresponding_Concurrent_Type | |
3435 | (Etype (First_Entity (Spec_Id)))) | |
758c442c | 3436 | then |
5d37ba92 ES |
3437 | declare |
3438 | Typ : constant Entity_Id := Etype (First_Entity (Spec_Id)); | |
3439 | Form : Entity_Id; | |
3440 | ||
3441 | begin | |
3442 | Form := First_Formal (Spec_Id); | |
3443 | while Present (Form) loop | |
3444 | if Etype (Form) = Typ then | |
3445 | Set_Etype (Form, Corresponding_Concurrent_Type (Typ)); | |
3446 | end if; | |
3447 | ||
3448 | Next_Formal (Form); | |
3449 | end loop; | |
3450 | end; | |
758c442c GD |
3451 | end if; |
3452 | ||
21d27997 RD |
3453 | -- Make the formals visible, and place subprogram on scope stack. |
3454 | -- This is also the point at which we set Last_Real_Spec_Entity | |
3455 | -- to mark the entities which will not be moved to the body. | |
758c442c | 3456 | |
996ae0b0 | 3457 | Install_Formals (Spec_Id); |
21d27997 | 3458 | Last_Real_Spec_Entity := Last_Entity (Spec_Id); |
616547fa AC |
3459 | |
3460 | -- Within an instance, add local renaming declarations so that | |
a5a809b2 AC |
3461 | -- gdb can retrieve the values of actuals more easily. This is |
3462 | -- only relevant if generating code (and indeed we definitely | |
3463 | -- do not want these definitions -gnatc mode, because that would | |
3464 | -- confuse ASIS). | |
616547fa AC |
3465 | |
3466 | if Is_Generic_Instance (Spec_Id) | |
3467 | and then Is_Wrapper_Package (Current_Scope) | |
a5a809b2 | 3468 | and then Expander_Active |
616547fa AC |
3469 | then |
3470 | Build_Subprogram_Instance_Renamings (N, Current_Scope); | |
3471 | end if; | |
3472 | ||
0a36105d | 3473 | Push_Scope (Spec_Id); |
996ae0b0 RK |
3474 | |
3475 | -- Make sure that the subprogram is immediately visible. For | |
3476 | -- child units that have no separate spec this is indispensable. | |
3477 | -- Otherwise it is safe albeit redundant. | |
3478 | ||
3479 | Set_Is_Immediately_Visible (Spec_Id); | |
3480 | end if; | |
3481 | ||
3482 | Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id); | |
ea3c0651 | 3483 | Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id))); |
996ae0b0 | 3484 | Set_Scope (Body_Id, Scope (Spec_Id)); |
ec4867fa | 3485 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); |
996ae0b0 RK |
3486 | |
3487 | -- Case of subprogram body with no previous spec | |
3488 | ||
3489 | else | |
3e5daac4 AC |
3490 | -- Check for style warning required |
3491 | ||
996ae0b0 | 3492 | if Style_Check |
3e5daac4 AC |
3493 | |
3494 | -- Only apply check for source level subprograms for which checks | |
3495 | -- have not been suppressed. | |
3496 | ||
996ae0b0 RK |
3497 | and then Comes_From_Source (Body_Id) |
3498 | and then not Suppress_Style_Checks (Body_Id) | |
3e5daac4 AC |
3499 | |
3500 | -- No warnings within an instance | |
3501 | ||
996ae0b0 | 3502 | and then not In_Instance |
3e5daac4 | 3503 | |
b0186f71 | 3504 | -- No warnings for expression functions |
3e5daac4 | 3505 | |
b0186f71 | 3506 | and then Nkind (Original_Node (N)) /= N_Expression_Function |
996ae0b0 RK |
3507 | then |
3508 | Style.Body_With_No_Spec (N); | |
3509 | end if; | |
3510 | ||
3511 | New_Overloaded_Entity (Body_Id); | |
3512 | ||
3513 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
3514 | Set_Acts_As_Spec (N); | |
3515 | Generate_Definition (Body_Id); | |
dac3bede | 3516 | Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id))); |
fbf5a39b AC |
3517 | Generate_Reference |
3518 | (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True); | |
996ae0b0 | 3519 | Install_Formals (Body_Id); |
e949ee22 | 3520 | |
4a854847 | 3521 | Push_Scope (Body_Id); |
996ae0b0 | 3522 | end if; |
dbe36d67 AC |
3523 | |
3524 | -- For stubs and bodies with no previous spec, generate references to | |
3525 | -- formals. | |
3526 | ||
3527 | Generate_Reference_To_Formals (Body_Id); | |
996ae0b0 RK |
3528 | end if; |
3529 | ||
5216b599 AC |
3530 | -- Set SPARK_Mode from context |
3531 | ||
3532 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); | |
3533 | Set_SPARK_Pragma_Inherited (Body_Id, True); | |
3534 | ||
76a69663 ES |
3535 | -- If the return type is an anonymous access type whose designated type |
3536 | -- is the limited view of a class-wide type and the non-limited view is | |
3537 | -- available, update the return type accordingly. | |
ec4867fa | 3538 | |
8fde064e | 3539 | if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then |
ec4867fa | 3540 | declare |
ec4867fa | 3541 | Etyp : Entity_Id; |
0a36105d | 3542 | Rtyp : Entity_Id; |
ec4867fa ES |
3543 | |
3544 | begin | |
0a36105d JM |
3545 | Rtyp := Etype (Current_Scope); |
3546 | ||
3547 | if Ekind (Rtyp) = E_Anonymous_Access_Type then | |
3548 | Etyp := Directly_Designated_Type (Rtyp); | |
3549 | ||
7b56a91b AC |
3550 | if Is_Class_Wide_Type (Etyp) |
3551 | and then From_Limited_With (Etyp) | |
3552 | then | |
0a36105d JM |
3553 | Set_Directly_Designated_Type |
3554 | (Etype (Current_Scope), Available_View (Etyp)); | |
3555 | end if; | |
3556 | end if; | |
ec4867fa ES |
3557 | end; |
3558 | end if; | |
3559 | ||
996ae0b0 RK |
3560 | -- If this is the proper body of a stub, we must verify that the stub |
3561 | -- conforms to the body, and to the previous spec if one was present. | |
dbe36d67 | 3562 | -- We know already that the body conforms to that spec. This test is |
996ae0b0 RK |
3563 | -- only required for subprograms that come from source. |
3564 | ||
3565 | if Nkind (Parent (N)) = N_Subunit | |
3566 | and then Comes_From_Source (N) | |
3567 | and then not Error_Posted (Body_Id) | |
e895b435 ES |
3568 | and then Nkind (Corresponding_Stub (Parent (N))) = |
3569 | N_Subprogram_Body_Stub | |
996ae0b0 RK |
3570 | then |
3571 | declare | |
fbf5a39b AC |
3572 | Old_Id : constant Entity_Id := |
3573 | Defining_Entity | |
3574 | (Specification (Corresponding_Stub (Parent (N)))); | |
3575 | ||
996ae0b0 | 3576 | Conformant : Boolean := False; |
996ae0b0 RK |
3577 | |
3578 | begin | |
3579 | if No (Spec_Id) then | |
3580 | Check_Fully_Conformant (Body_Id, Old_Id); | |
3581 | ||
3582 | else | |
3583 | Check_Conformance | |
3584 | (Body_Id, Old_Id, Fully_Conformant, False, Conformant); | |
3585 | ||
3586 | if not Conformant then | |
3587 | ||
dbe36d67 AC |
3588 | -- The stub was taken to be a new declaration. Indicate that |
3589 | -- it lacks a body. | |
996ae0b0 RK |
3590 | |
3591 | Set_Has_Completion (Old_Id, False); | |
3592 | end if; | |
3593 | end if; | |
3594 | end; | |
3595 | end if; | |
3596 | ||
3597 | Set_Has_Completion (Body_Id); | |
3598 | Check_Eliminated (Body_Id); | |
3599 | ||
3600 | if Nkind (N) = N_Subprogram_Body_Stub then | |
c8a3028c AC |
3601 | |
3602 | -- Analyze any aspect specifications that appear on the subprogram | |
3603 | -- body stub. | |
3604 | ||
3605 | if Has_Aspects (N) then | |
3606 | Analyze_Aspects_On_Body_Or_Stub; | |
3607 | end if; | |
3608 | ||
3609 | -- Stop the analysis now as the stub cannot be inlined, plus it does | |
3610 | -- not have declarative or statement lists. | |
3611 | ||
996ae0b0 | 3612 | return; |
84f4072a | 3613 | end if; |
996ae0b0 | 3614 | |
b94b6c56 | 3615 | -- Handle frontend inlining |
84f4072a | 3616 | |
b94b6c56 RD |
3617 | -- Note: Normally we don't do any inlining if expansion is off, since |
3618 | -- we won't generate code in any case. An exception arises in GNATprove | |
2d180af1 YM |
3619 | -- mode where we want to expand some calls in place, even with expansion |
3620 | -- disabled, since the inlining eases formal verification. | |
ecad37f3 | 3621 | |
6c26bac2 AC |
3622 | if not GNATprove_Mode |
3623 | and then Expander_Active | |
3624 | and then Serious_Errors_Detected = 0 | |
3625 | and then Present (Spec_Id) | |
3626 | and then Has_Pragma_Inline (Spec_Id) | |
3627 | then | |
3628 | -- Legacy implementation (relying on frontend inlining) | |
84f4072a | 3629 | |
6c26bac2 AC |
3630 | if not Back_End_Inlining then |
3631 | if Has_Pragma_Inline_Always (Spec_Id) | |
ea0c8cfb | 3632 | or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining) |
6c26bac2 AC |
3633 | then |
3634 | Build_Body_To_Inline (N, Spec_Id); | |
3635 | end if; | |
f087ea44 | 3636 | |
88f7d2d1 | 3637 | -- New implementation (relying on backend inlining) |
f087ea44 | 3638 | |
6c26bac2 AC |
3639 | else |
3640 | if Has_Pragma_Inline_Always (Spec_Id) | |
3641 | or else Optimization_Level > 0 | |
3642 | then | |
3643 | -- Handle function returning an unconstrained type | |
f087ea44 | 3644 | |
6c26bac2 AC |
3645 | if Comes_From_Source (Body_Id) |
3646 | and then Ekind (Spec_Id) = E_Function | |
3647 | and then Returns_Unconstrained_Type (Spec_Id) | |
3648 | then | |
16b10ccc | 3649 | Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id); |
84f4072a | 3650 | |
6c26bac2 AC |
3651 | else |
3652 | declare | |
b80a2b4b AC |
3653 | Subp_Body : constant Node_Id := |
3654 | Unit_Declaration_Node (Body_Id); | |
6c26bac2 | 3655 | Subp_Decl : constant List_Id := Declarations (Subp_Body); |
84f4072a | 3656 | |
6c26bac2 AC |
3657 | begin |
3658 | -- Do not pass inlining to the backend if the subprogram | |
3659 | -- has declarations or statements which cannot be inlined | |
3660 | -- by the backend. This check is done here to emit an | |
3661 | -- error instead of the generic warning message reported | |
3662 | -- by the GCC backend (ie. "function might not be | |
3663 | -- inlinable"). | |
3664 | ||
3665 | if Present (Subp_Decl) | |
3666 | and then Has_Excluded_Declaration (Spec_Id, Subp_Decl) | |
3667 | then | |
3668 | null; | |
3669 | ||
3670 | elsif Has_Excluded_Statement | |
3671 | (Spec_Id, | |
3672 | Statements | |
3673 | (Handled_Statement_Sequence (Subp_Body))) | |
3674 | then | |
3675 | null; | |
3676 | ||
3677 | -- If the backend inlining is available then at this | |
3678 | -- stage we only have to mark the subprogram as inlined. | |
3679 | -- The expander will take care of registering it in the | |
3680 | -- table of subprograms inlined by the backend a part of | |
3681 | -- processing calls to it (cf. Expand_Call) | |
3682 | ||
3683 | else | |
3684 | Set_Is_Inlined (Spec_Id); | |
3685 | end if; | |
3686 | end; | |
3687 | end if; | |
3688 | end if; | |
3689 | end if; | |
2d180af1 YM |
3690 | |
3691 | -- In GNATprove mode, inline only when there is a separate subprogram | |
3692 | -- declaration for now, as inlining of subprogram bodies acting as | |
3693 | -- declarations, or subprogram stubs, are not supported by frontend | |
3694 | -- inlining. This inlining should occur after analysis of the body, so | |
3695 | -- that it is known whether the value of SPARK_Mode applicable to the | |
3696 | -- body, which can be defined by a pragma inside the body. | |
3697 | ||
3698 | elsif GNATprove_Mode | |
2d180af1 YM |
3699 | and then Full_Analysis |
3700 | and then not Inside_A_Generic | |
3701 | and then Present (Spec_Id) | |
3702 | and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration | |
3703 | and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id) | |
ac072cb2 | 3704 | and then not Body_Has_Contract |
2d180af1 | 3705 | then |
6c26bac2 | 3706 | Build_Body_To_Inline (N, Spec_Id); |
996ae0b0 RK |
3707 | end if; |
3708 | ||
0ab80019 | 3709 | -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis |
dbe36d67 | 3710 | -- of the specification we have to install the private withed units. |
21d27997 | 3711 | -- This holds for child units as well. |
9bc856dd AC |
3712 | |
3713 | if Is_Compilation_Unit (Body_Id) | |
21d27997 | 3714 | or else Nkind (Parent (N)) = N_Compilation_Unit |
9bc856dd AC |
3715 | then |
3716 | Install_Private_With_Clauses (Body_Id); | |
3717 | end if; | |
3718 | ||
ec4867fa ES |
3719 | Check_Anonymous_Return; |
3720 | ||
fdce4bb7 JM |
3721 | -- Set the Protected_Formal field of each extra formal of the protected |
3722 | -- subprogram to reference the corresponding extra formal of the | |
3723 | -- subprogram that implements it. For regular formals this occurs when | |
3724 | -- the protected subprogram's declaration is expanded, but the extra | |
3725 | -- formals don't get created until the subprogram is frozen. We need to | |
3726 | -- do this before analyzing the protected subprogram's body so that any | |
3727 | -- references to the original subprogram's extra formals will be changed | |
3728 | -- refer to the implementing subprogram's formals (see Expand_Formal). | |
3729 | ||
3730 | if Present (Spec_Id) | |
3731 | and then Is_Protected_Type (Scope (Spec_Id)) | |
3732 | and then Present (Protected_Body_Subprogram (Spec_Id)) | |
3733 | then | |
3734 | declare | |
3735 | Impl_Subp : constant Entity_Id := | |
3736 | Protected_Body_Subprogram (Spec_Id); | |
3737 | Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id); | |
3738 | Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp); | |
fdce4bb7 JM |
3739 | begin |
3740 | while Present (Prot_Ext_Formal) loop | |
3741 | pragma Assert (Present (Impl_Ext_Formal)); | |
fdce4bb7 | 3742 | Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal); |
fdce4bb7 JM |
3743 | Next_Formal_With_Extras (Prot_Ext_Formal); |
3744 | Next_Formal_With_Extras (Impl_Ext_Formal); | |
3745 | end loop; | |
3746 | end; | |
3747 | end if; | |
3748 | ||
0868e09c | 3749 | -- Now we can go on to analyze the body |
996ae0b0 RK |
3750 | |
3751 | HSS := Handled_Statement_Sequence (N); | |
3752 | Set_Actual_Subtypes (N, Current_Scope); | |
21d27997 | 3753 | |
f3d0f304 | 3754 | -- Add a declaration for the Protection object, renaming declarations |
21d27997 RD |
3755 | -- for discriminals and privals and finally a declaration for the entry |
3756 | -- family index (if applicable). This form of early expansion is done | |
3757 | -- when the Expander is active because Install_Private_Data_Declarations | |
81bf2382 | 3758 | -- references entities which were created during regular expansion. The |
3b8056a5 AC |
3759 | -- subprogram entity must come from source, and not be an internally |
3760 | -- generated subprogram. | |
21d27997 | 3761 | |
4460a9bc | 3762 | if Expander_Active |
21d27997 RD |
3763 | and then Present (Prot_Typ) |
3764 | and then Present (Spec_Id) | |
3b8056a5 | 3765 | and then Comes_From_Source (Spec_Id) |
21d27997 RD |
3766 | and then not Is_Eliminated (Spec_Id) |
3767 | then | |
3768 | Install_Private_Data_Declarations | |
3769 | (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N)); | |
3770 | end if; | |
3771 | ||
5dcab3ca AC |
3772 | -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context |
3773 | -- may now appear in parameter and result profiles. Since the analysis | |
3774 | -- of a subprogram body may use the parameter and result profile of the | |
3775 | -- spec, swap any limited views with their non-limited counterpart. | |
3776 | ||
3777 | if Ada_Version >= Ada_2012 then | |
3778 | Exchange_Limited_Views (Spec_Id); | |
3779 | end if; | |
3780 | ||
c8a3028c AC |
3781 | -- Analyze any aspect specifications that appear on the subprogram body |
3782 | ||
3783 | if Has_Aspects (N) then | |
3784 | Analyze_Aspects_On_Body_Or_Stub; | |
3785 | end if; | |
3786 | ||
3787 | -- Deal with [refined] preconditions, postconditions, Contract_Cases, | |
3788 | -- invariants and predicates associated with the body and its spec. | |
3789 | -- Note that this is not pure expansion as Expand_Subprogram_Contract | |
3790 | -- prepares the contract assertions for generic subprograms or for ASIS. | |
3791 | -- Do not generate contract checks in SPARK mode. | |
3792 | ||
3793 | if not GNATprove_Mode then | |
3794 | Expand_Subprogram_Contract (N, Spec_Id, Body_Id); | |
3795 | end if; | |
3796 | ||
21d27997 RD |
3797 | -- Analyze the declarations (this call will analyze the precondition |
3798 | -- Check pragmas we prepended to the list, as well as the declaration | |
3799 | -- of the _Postconditions procedure). | |
3800 | ||
996ae0b0 | 3801 | Analyze_Declarations (Declarations (N)); |
21d27997 | 3802 | |
f3124d8f | 3803 | -- Verify that the SPARK_Mode of the body agrees with that of its spec |
f90d14ac | 3804 | |
f1c7be38 | 3805 | if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then |
f90d14ac AC |
3806 | if Present (SPARK_Pragma (Spec_Id)) then |
3807 | if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off | |
3808 | and then | |
3809 | Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On | |
3810 | then | |
3811 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
3812 | Error_Msg_N ("incorrect application of SPARK_Mode#", N); | |
3813 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id)); | |
3814 | Error_Msg_NE | |
f3124d8f | 3815 | ("\value Off was set for SPARK_Mode on & #", N, Spec_Id); |
f90d14ac AC |
3816 | end if; |
3817 | ||
3818 | elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then | |
3819 | null; | |
3820 | ||
3821 | else | |
3822 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
3ccedacc | 3823 | Error_Msg_N ("incorrect application of SPARK_Mode #", N); |
f90d14ac | 3824 | Error_Msg_Sloc := Sloc (Spec_Id); |
f3124d8f HK |
3825 | Error_Msg_NE |
3826 | ("\no value was set for SPARK_Mode on & #", N, Spec_Id); | |
f90d14ac AC |
3827 | end if; |
3828 | end if; | |
3829 | ||
ac43e11e AC |
3830 | -- If SPARK_Mode for body is not On, disable frontend inlining for this |
3831 | -- subprogram in GNATprove mode, as its body should not be analyzed. | |
3832 | ||
3833 | if SPARK_Mode /= On | |
3834 | and then GNATprove_Mode | |
ac43e11e AC |
3835 | and then Present (Spec_Id) |
3836 | and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration | |
3837 | then | |
3838 | Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty); | |
4bd4bb7f | 3839 | Set_Is_Inlined_Always (Spec_Id, False); |
ac43e11e AC |
3840 | end if; |
3841 | ||
21d27997 RD |
3842 | -- Check completion, and analyze the statements |
3843 | ||
996ae0b0 | 3844 | Check_Completion; |
33931112 | 3845 | Inspect_Deferred_Constant_Completion (Declarations (N)); |
996ae0b0 | 3846 | Analyze (HSS); |
21d27997 RD |
3847 | |
3848 | -- Deal with end of scope processing for the body | |
3849 | ||
07fc65c4 | 3850 | Process_End_Label (HSS, 't', Current_Scope); |
996ae0b0 RK |
3851 | End_Scope; |
3852 | Check_Subprogram_Order (N); | |
c37bb106 | 3853 | Set_Analyzed (Body_Id); |
996ae0b0 RK |
3854 | |
3855 | -- If we have a separate spec, then the analysis of the declarations | |
3856 | -- caused the entities in the body to be chained to the spec id, but | |
3857 | -- we want them chained to the body id. Only the formal parameters | |
3858 | -- end up chained to the spec id in this case. | |
3859 | ||
3860 | if Present (Spec_Id) then | |
3861 | ||
d39d6bb8 | 3862 | -- We must conform to the categorization of our spec |
996ae0b0 | 3863 | |
d39d6bb8 | 3864 | Validate_Categorization_Dependency (N, Spec_Id); |
996ae0b0 | 3865 | |
d39d6bb8 RD |
3866 | -- And if this is a child unit, the parent units must conform |
3867 | ||
3868 | if Is_Child_Unit (Spec_Id) then | |
996ae0b0 RK |
3869 | Validate_Categorization_Dependency |
3870 | (Unit_Declaration_Node (Spec_Id), Spec_Id); | |
3871 | end if; | |
3872 | ||
21d27997 RD |
3873 | -- Here is where we move entities from the spec to the body |
3874 | ||
3875 | -- Case where there are entities that stay with the spec | |
3876 | ||
3877 | if Present (Last_Real_Spec_Entity) then | |
3878 | ||
dbe36d67 AC |
3879 | -- No body entities (happens when the only real spec entities come |
3880 | -- from precondition and postcondition pragmas). | |
21d27997 RD |
3881 | |
3882 | if No (Last_Entity (Body_Id)) then | |
3883 | Set_First_Entity | |
3884 | (Body_Id, Next_Entity (Last_Real_Spec_Entity)); | |
3885 | ||
3886 | -- Body entities present (formals), so chain stuff past them | |
3887 | ||
3888 | else | |
3889 | Set_Next_Entity | |
3890 | (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity)); | |
3891 | end if; | |
3892 | ||
3893 | Set_Next_Entity (Last_Real_Spec_Entity, Empty); | |
996ae0b0 | 3894 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); |
21d27997 RD |
3895 | Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity); |
3896 | ||
dbe36d67 AC |
3897 | -- Case where there are no spec entities, in this case there can be |
3898 | -- no body entities either, so just move everything. | |
996ae0b0 RK |
3899 | |
3900 | else | |
21d27997 | 3901 | pragma Assert (No (Last_Entity (Body_Id))); |
996ae0b0 RK |
3902 | Set_First_Entity (Body_Id, First_Entity (Spec_Id)); |
3903 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); | |
3904 | Set_First_Entity (Spec_Id, Empty); | |
3905 | Set_Last_Entity (Spec_Id, Empty); | |
3906 | end if; | |
3907 | end if; | |
3908 | ||
7665e4bd | 3909 | Check_Missing_Return; |
996ae0b0 | 3910 | |
82c80734 | 3911 | -- Now we are going to check for variables that are never modified in |
76a69663 ES |
3912 | -- the body of the procedure. But first we deal with a special case |
3913 | -- where we want to modify this check. If the body of the subprogram | |
3914 | -- starts with a raise statement or its equivalent, or if the body | |
cf3e6845 AC |
3915 | -- consists entirely of a null statement, then it is pretty obvious that |
3916 | -- it is OK to not reference the parameters. For example, this might be | |
3917 | -- the following common idiom for a stubbed function: statement of the | |
3918 | -- procedure raises an exception. In particular this deals with the | |
3919 | -- common idiom of a stubbed function, which appears something like: | |
fbf5a39b AC |
3920 | |
3921 | -- function F (A : Integer) return Some_Type; | |
3922 | -- X : Some_Type; | |
3923 | -- begin | |
3924 | -- raise Program_Error; | |
3925 | -- return X; | |
3926 | -- end F; | |
3927 | ||
76a69663 ES |
3928 | -- Here the purpose of X is simply to satisfy the annoying requirement |
3929 | -- in Ada that there be at least one return, and we certainly do not | |
a90bd866 | 3930 | -- want to go posting warnings on X that it is not initialized. On |
76a69663 ES |
3931 | -- the other hand, if X is entirely unreferenced that should still |
3932 | -- get a warning. | |
3933 | ||
3934 | -- What we do is to detect these cases, and if we find them, flag the | |
3935 | -- subprogram as being Is_Trivial_Subprogram and then use that flag to | |
3936 | -- suppress unwanted warnings. For the case of the function stub above | |
3937 | -- we have a special test to set X as apparently assigned to suppress | |
3938 | -- the warning. | |
996ae0b0 RK |
3939 | |
3940 | declare | |
800621e0 | 3941 | Stm : Node_Id; |
996ae0b0 RK |
3942 | |
3943 | begin | |
0a36105d JM |
3944 | -- Skip initial labels (for one thing this occurs when we are in |
3945 | -- front end ZCX mode, but in any case it is irrelevant), and also | |
3946 | -- initial Push_xxx_Error_Label nodes, which are also irrelevant. | |
fbf5a39b | 3947 | |
800621e0 | 3948 | Stm := First (Statements (HSS)); |
0a36105d JM |
3949 | while Nkind (Stm) = N_Label |
3950 | or else Nkind (Stm) in N_Push_xxx_Label | |
3951 | loop | |
996ae0b0 | 3952 | Next (Stm); |
0a36105d | 3953 | end loop; |
996ae0b0 | 3954 | |
fbf5a39b AC |
3955 | -- Do the test on the original statement before expansion |
3956 | ||
3957 | declare | |
3958 | Ostm : constant Node_Id := Original_Node (Stm); | |
3959 | ||
3960 | begin | |
76a69663 | 3961 | -- If explicit raise statement, turn on flag |
fbf5a39b AC |
3962 | |
3963 | if Nkind (Ostm) = N_Raise_Statement then | |
76a69663 ES |
3964 | Set_Trivial_Subprogram (Stm); |
3965 | ||
f3d57416 | 3966 | -- If null statement, and no following statements, turn on flag |
76a69663 ES |
3967 | |
3968 | elsif Nkind (Stm) = N_Null_Statement | |
3969 | and then Comes_From_Source (Stm) | |
3970 | and then No (Next (Stm)) | |
3971 | then | |
3972 | Set_Trivial_Subprogram (Stm); | |
fbf5a39b AC |
3973 | |
3974 | -- Check for explicit call cases which likely raise an exception | |
3975 | ||
3976 | elsif Nkind (Ostm) = N_Procedure_Call_Statement then | |
3977 | if Is_Entity_Name (Name (Ostm)) then | |
3978 | declare | |
3979 | Ent : constant Entity_Id := Entity (Name (Ostm)); | |
3980 | ||
3981 | begin | |
3982 | -- If the procedure is marked No_Return, then likely it | |
3983 | -- raises an exception, but in any case it is not coming | |
76a69663 | 3984 | -- back here, so turn on the flag. |
fbf5a39b | 3985 | |
f46faa08 AC |
3986 | if Present (Ent) |
3987 | and then Ekind (Ent) = E_Procedure | |
fbf5a39b AC |
3988 | and then No_Return (Ent) |
3989 | then | |
76a69663 | 3990 | Set_Trivial_Subprogram (Stm); |
fbf5a39b AC |
3991 | end if; |
3992 | end; | |
3993 | end if; | |
3994 | end if; | |
3995 | end; | |
996ae0b0 RK |
3996 | end; |
3997 | ||
3998 | -- Check for variables that are never modified | |
3999 | ||
4000 | declare | |
4001 | E1, E2 : Entity_Id; | |
4002 | ||
4003 | begin | |
fbf5a39b | 4004 | -- If there is a separate spec, then transfer Never_Set_In_Source |
996ae0b0 RK |
4005 | -- flags from out parameters to the corresponding entities in the |
4006 | -- body. The reason we do that is we want to post error flags on | |
4007 | -- the body entities, not the spec entities. | |
4008 | ||
4009 | if Present (Spec_Id) then | |
4010 | E1 := First_Entity (Spec_Id); | |
996ae0b0 RK |
4011 | while Present (E1) loop |
4012 | if Ekind (E1) = E_Out_Parameter then | |
4013 | E2 := First_Entity (Body_Id); | |
fbf5a39b | 4014 | while Present (E2) loop |
996ae0b0 RK |
4015 | exit when Chars (E1) = Chars (E2); |
4016 | Next_Entity (E2); | |
4017 | end loop; | |
4018 | ||
fbf5a39b AC |
4019 | if Present (E2) then |
4020 | Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1)); | |
4021 | end if; | |
996ae0b0 RK |
4022 | end if; |
4023 | ||
4024 | Next_Entity (E1); | |
4025 | end loop; | |
4026 | end if; | |
4027 | ||
2aca76d6 | 4028 | -- Check references in body |
0868e09c | 4029 | |
2aca76d6 | 4030 | Check_References (Body_Id); |
996ae0b0 | 4031 | end; |
b1b543d2 | 4032 | end Analyze_Subprogram_Body_Helper; |
996ae0b0 | 4033 | |
5afe5d2d HK |
4034 | --------------------------------- |
4035 | -- Analyze_Subprogram_Contract -- | |
4036 | --------------------------------- | |
4037 | ||
4038 | procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is | |
36eef04a AC |
4039 | Items : constant Node_Id := Contract (Subp); |
4040 | Case_Prag : Node_Id := Empty; | |
4041 | Depends : Node_Id := Empty; | |
4042 | Global : Node_Id := Empty; | |
c61ef416 | 4043 | Mode : SPARK_Mode_Type; |
36eef04a AC |
4044 | Nam : Name_Id; |
4045 | Post_Prag : Node_Id := Empty; | |
4046 | Prag : Node_Id; | |
4047 | Seen_In_Case : Boolean := False; | |
4048 | Seen_In_Post : Boolean := False; | |
5afe5d2d HK |
4049 | |
4050 | begin | |
fc999c5d RD |
4051 | -- Due to the timing of contract analysis, delayed pragmas may be |
4052 | -- subject to the wrong SPARK_Mode, usually that of the enclosing | |
4053 | -- context. To remedy this, restore the original SPARK_Mode of the | |
4054 | -- related subprogram body. | |
4055 | ||
c61ef416 AC |
4056 | Save_SPARK_Mode_And_Set (Subp, Mode); |
4057 | ||
5afe5d2d HK |
4058 | if Present (Items) then |
4059 | ||
4060 | -- Analyze pre- and postconditions | |
4061 | ||
4062 | Prag := Pre_Post_Conditions (Items); | |
4063 | while Present (Prag) loop | |
ea3c0651 | 4064 | Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp); |
5afe5d2d HK |
4065 | |
4066 | -- Verify whether a postcondition mentions attribute 'Result and | |
4067 | -- its expression introduces a post-state. | |
4068 | ||
4069 | if Warn_On_Suspicious_Contract | |
4070 | and then Pragma_Name (Prag) = Name_Postcondition | |
4071 | then | |
36eef04a AC |
4072 | Post_Prag := Prag; |
4073 | Check_Result_And_Post_State (Prag, Seen_In_Post); | |
5afe5d2d HK |
4074 | end if; |
4075 | ||
4076 | Prag := Next_Pragma (Prag); | |
4077 | end loop; | |
4078 | ||
4079 | -- Analyze contract-cases and test-cases | |
4080 | ||
4081 | Prag := Contract_Test_Cases (Items); | |
4082 | while Present (Prag) loop | |
ea3c0651 AC |
4083 | Nam := Pragma_Name (Prag); |
4084 | ||
4085 | if Nam = Name_Contract_Cases then | |
5afe5d2d HK |
4086 | Analyze_Contract_Cases_In_Decl_Part (Prag); |
4087 | ||
4088 | -- Verify whether contract-cases mention attribute 'Result and | |
4089 | -- its expression introduces a post-state. Perform the check | |
4090 | -- only when the pragma is legal. | |
4091 | ||
4092 | if Warn_On_Suspicious_Contract | |
4093 | and then not Error_Posted (Prag) | |
4094 | then | |
36eef04a AC |
4095 | Case_Prag := Prag; |
4096 | Check_Result_And_Post_State (Prag, Seen_In_Case); | |
5afe5d2d HK |
4097 | end if; |
4098 | ||
4099 | else | |
ea3c0651 | 4100 | pragma Assert (Nam = Name_Test_Case); |
5afe5d2d HK |
4101 | Analyze_Test_Case_In_Decl_Part (Prag, Subp); |
4102 | end if; | |
4103 | ||
4104 | Prag := Next_Pragma (Prag); | |
4105 | end loop; | |
4106 | ||
4107 | -- Analyze classification pragmas | |
4108 | ||
6c3c671e | 4109 | Prag := Classifications (Items); |
5afe5d2d | 4110 | while Present (Prag) loop |
ea3c0651 AC |
4111 | Nam := Pragma_Name (Prag); |
4112 | ||
4113 | if Nam = Name_Depends then | |
54e28df2 | 4114 | Depends := Prag; |
039538bc AC |
4115 | |
4116 | elsif Nam = Name_Global then | |
54e28df2 | 4117 | Global := Prag; |
039538bc AC |
4118 | |
4119 | -- Note that pragma Extensions_Visible has already been analyzed | |
4120 | ||
5afe5d2d HK |
4121 | end if; |
4122 | ||
4123 | Prag := Next_Pragma (Prag); | |
4124 | end loop; | |
54e28df2 HK |
4125 | |
4126 | -- Analyze Global first as Depends may mention items classified in | |
4127 | -- the global categorization. | |
4128 | ||
4129 | if Present (Global) then | |
4130 | Analyze_Global_In_Decl_Part (Global); | |
4131 | end if; | |
4132 | ||
4133 | -- Depends must be analyzed after Global in order to see the modes of | |
4134 | -- all global items. | |
4135 | ||
4136 | if Present (Depends) then | |
4137 | Analyze_Depends_In_Decl_Part (Depends); | |
4138 | end if; | |
5afe5d2d HK |
4139 | end if; |
4140 | ||
36eef04a | 4141 | -- Emit an error when neither the postconditions nor the contract-cases |
5afe5d2d HK |
4142 | -- mention attribute 'Result in the context of a function. |
4143 | ||
4144 | if Warn_On_Suspicious_Contract | |
4145 | and then Ekind_In (Subp, E_Function, E_Generic_Function) | |
5afe5d2d | 4146 | then |
36eef04a AC |
4147 | if Present (Case_Prag) |
4148 | and then not Seen_In_Case | |
4149 | and then Present (Post_Prag) | |
4150 | and then not Seen_In_Post | |
4151 | then | |
5afe5d2d HK |
4152 | Error_Msg_N |
4153 | ("neither function postcondition nor contract cases mention " | |
36eef04a | 4154 | & "result?T?", Post_Prag); |
5afe5d2d | 4155 | |
36eef04a | 4156 | elsif Present (Case_Prag) and then not Seen_In_Case then |
5afe5d2d | 4157 | Error_Msg_N |
36eef04a | 4158 | ("contract cases do not mention result?T?", Case_Prag); |
5afe5d2d | 4159 | |
f660fba6 AC |
4160 | -- OK if we have at least one IN OUT parameter |
4161 | ||
36eef04a | 4162 | elsif Present (Post_Prag) and then not Seen_In_Post then |
f660fba6 AC |
4163 | declare |
4164 | F : Entity_Id; | |
4165 | begin | |
4166 | F := First_Formal (Subp); | |
4167 | while Present (F) loop | |
4168 | if Ekind (F) = E_In_Out_Parameter then | |
4169 | return; | |
4170 | else | |
4171 | Next_Formal (F); | |
4172 | end if; | |
4173 | end loop; | |
4174 | end; | |
4175 | ||
4176 | -- If no in-out parameters and no mention of Result, the contract | |
4177 | -- is certainly suspicious. | |
4178 | ||
5afe5d2d | 4179 | Error_Msg_N |
36eef04a | 4180 | ("function postcondition does not mention result?T?", Post_Prag); |
5afe5d2d HK |
4181 | end if; |
4182 | end if; | |
c61ef416 | 4183 | |
fc999c5d RD |
4184 | -- Restore the SPARK_Mode of the enclosing context after all delayed |
4185 | -- pragmas have been analyzed. | |
4186 | ||
c61ef416 | 4187 | Restore_SPARK_Mode (Mode); |
5afe5d2d HK |
4188 | end Analyze_Subprogram_Contract; |
4189 | ||
996ae0b0 RK |
4190 | ------------------------------------ |
4191 | -- Analyze_Subprogram_Declaration -- | |
4192 | ------------------------------------ | |
4193 | ||
4194 | procedure Analyze_Subprogram_Declaration (N : Node_Id) is | |
579847c2 | 4195 | Scop : constant Entity_Id := Current_Scope; |
5d5832bc | 4196 | Designator : Entity_Id; |
579847c2 | 4197 | |
4d8f3296 ES |
4198 | Is_Completion : Boolean; |
4199 | -- Indicates whether a null procedure declaration is a completion | |
996ae0b0 RK |
4200 | |
4201 | begin | |
2ba431e5 | 4202 | -- Null procedures are not allowed in SPARK |
daec8eeb | 4203 | |
fe5d3068 | 4204 | if Nkind (Specification (N)) = N_Procedure_Specification |
daec8eeb YM |
4205 | and then Null_Present (Specification (N)) |
4206 | then | |
ce5ba43a | 4207 | Check_SPARK_05_Restriction ("null procedure is not allowed", N); |
718deaf1 | 4208 | |
4d8f3296 ES |
4209 | if Is_Protected_Type (Current_Scope) then |
4210 | Error_Msg_N ("protected operation cannot be a null procedure", N); | |
4211 | end if; | |
718deaf1 | 4212 | |
4d8f3296 | 4213 | Analyze_Null_Procedure (N, Is_Completion); |
718deaf1 | 4214 | |
4d8f3296 | 4215 | if Is_Completion then |
718deaf1 | 4216 | |
4d8f3296 | 4217 | -- The null procedure acts as a body, nothing further is needed. |
5d5832bc | 4218 | |
4d8f3296 | 4219 | return; |
5d5832bc AC |
4220 | end if; |
4221 | end if; | |
4222 | ||
beacce02 | 4223 | Designator := Analyze_Subprogram_Specification (Specification (N)); |
31af8899 AC |
4224 | |
4225 | -- A reference may already have been generated for the unit name, in | |
4226 | -- which case the following call is redundant. However it is needed for | |
4227 | -- declarations that are the rewriting of an expression function. | |
4228 | ||
5d5832bc AC |
4229 | Generate_Definition (Designator); |
4230 | ||
f90d14ac AC |
4231 | -- Set SPARK mode from current context (may be overwritten later with |
4232 | -- explicit pragma). | |
4a854847 | 4233 | |
f90d14ac AC |
4234 | Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma); |
4235 | Set_SPARK_Pragma_Inherited (Designator, True); | |
579847c2 | 4236 | |
c5cec2fe AC |
4237 | -- A subprogram declared within a Ghost scope is automatically Ghost |
4238 | -- (SPARK RM 6.9(2)). | |
4239 | ||
4240 | if Comes_From_Source (Designator) and then Within_Ghost_Scope then | |
4241 | Set_Is_Ghost_Entity (Designator); | |
4242 | end if; | |
4243 | ||
b1b543d2 BD |
4244 | if Debug_Flag_C then |
4245 | Write_Str ("==> subprogram spec "); | |
4246 | Write_Name (Chars (Designator)); | |
4247 | Write_Str (" from "); | |
4248 | Write_Location (Sloc (N)); | |
4249 | Write_Eol; | |
4250 | Indent; | |
4251 | end if; | |
4252 | ||
996ae0b0 | 4253 | Validate_RCI_Subprogram_Declaration (N); |
996ae0b0 RK |
4254 | New_Overloaded_Entity (Designator); |
4255 | Check_Delayed_Subprogram (Designator); | |
fbf5a39b | 4256 | |
cf3e6845 AC |
4257 | -- If the type of the first formal of the current subprogram is a non- |
4258 | -- generic tagged private type, mark the subprogram as being a private | |
4259 | -- primitive. Ditto if this is a function with controlling result, and | |
4260 | -- the return type is currently private. In both cases, the type of the | |
4261 | -- controlling argument or result must be in the current scope for the | |
4262 | -- operation to be primitive. | |
6ca063eb AC |
4263 | |
4264 | if Has_Controlling_Result (Designator) | |
4265 | and then Is_Private_Type (Etype (Designator)) | |
b7d5e87b | 4266 | and then Scope (Etype (Designator)) = Current_Scope |
6ca063eb AC |
4267 | and then not Is_Generic_Actual_Type (Etype (Designator)) |
4268 | then | |
4269 | Set_Is_Private_Primitive (Designator); | |
d44202ba | 4270 | |
6ca063eb | 4271 | elsif Present (First_Formal (Designator)) then |
d44202ba HK |
4272 | declare |
4273 | Formal_Typ : constant Entity_Id := | |
4274 | Etype (First_Formal (Designator)); | |
4275 | begin | |
4276 | Set_Is_Private_Primitive (Designator, | |
4277 | Is_Tagged_Type (Formal_Typ) | |
b7d5e87b | 4278 | and then Scope (Formal_Typ) = Current_Scope |
d44202ba HK |
4279 | and then Is_Private_Type (Formal_Typ) |
4280 | and then not Is_Generic_Actual_Type (Formal_Typ)); | |
4281 | end; | |
4282 | end if; | |
4283 | ||
ec4867fa ES |
4284 | -- Ada 2005 (AI-251): Abstract interface primitives must be abstract |
4285 | -- or null. | |
4286 | ||
0791fbe9 | 4287 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
4288 | and then Comes_From_Source (N) |
4289 | and then Is_Dispatching_Operation (Designator) | |
4290 | then | |
4291 | declare | |
4292 | E : Entity_Id; | |
4293 | Etyp : Entity_Id; | |
4294 | ||
4295 | begin | |
4296 | if Has_Controlling_Result (Designator) then | |
4297 | Etyp := Etype (Designator); | |
4298 | ||
4299 | else | |
4300 | E := First_Entity (Designator); | |
4301 | while Present (E) | |
4302 | and then Is_Formal (E) | |
4303 | and then not Is_Controlling_Formal (E) | |
4304 | loop | |
4305 | Next_Entity (E); | |
4306 | end loop; | |
4307 | ||
4308 | Etyp := Etype (E); | |
4309 | end if; | |
4310 | ||
4311 | if Is_Access_Type (Etyp) then | |
4312 | Etyp := Directly_Designated_Type (Etyp); | |
4313 | end if; | |
4314 | ||
4315 | if Is_Interface (Etyp) | |
f937473f | 4316 | and then not Is_Abstract_Subprogram (Designator) |
ec4867fa | 4317 | and then not (Ekind (Designator) = E_Procedure |
8fde064e | 4318 | and then Null_Present (Specification (N))) |
ec4867fa ES |
4319 | then |
4320 | Error_Msg_Name_1 := Chars (Defining_Entity (N)); | |
033eaf85 AC |
4321 | |
4322 | -- Specialize error message based on procedures vs. functions, | |
4323 | -- since functions can't be null subprograms. | |
4324 | ||
4325 | if Ekind (Designator) = E_Procedure then | |
4326 | Error_Msg_N | |
4327 | ("interface procedure % must be abstract or null", N); | |
4328 | else | |
3f80a182 AC |
4329 | Error_Msg_N |
4330 | ("interface function % must be abstract", N); | |
033eaf85 | 4331 | end if; |
ec4867fa ES |
4332 | end if; |
4333 | end; | |
4334 | end if; | |
4335 | ||
fbf5a39b AC |
4336 | -- What is the following code for, it used to be |
4337 | ||
4338 | -- ??? Set_Suppress_Elaboration_Checks | |
4339 | -- ??? (Designator, Elaboration_Checks_Suppressed (Designator)); | |
4340 | ||
4341 | -- The following seems equivalent, but a bit dubious | |
4342 | ||
4343 | if Elaboration_Checks_Suppressed (Designator) then | |
4344 | Set_Kill_Elaboration_Checks (Designator); | |
4345 | end if; | |
996ae0b0 | 4346 | |
8fde064e | 4347 | if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then |
fbf5a39b | 4348 | Set_Categorization_From_Scope (Designator, Scop); |
8fde064e | 4349 | |
996ae0b0 | 4350 | else |
e895b435 | 4351 | -- For a compilation unit, check for library-unit pragmas |
996ae0b0 | 4352 | |
0a36105d | 4353 | Push_Scope (Designator); |
996ae0b0 RK |
4354 | Set_Categorization_From_Pragmas (N); |
4355 | Validate_Categorization_Dependency (N, Designator); | |
4356 | Pop_Scope; | |
4357 | end if; | |
4358 | ||
4359 | -- For a compilation unit, set body required. This flag will only be | |
4360 | -- reset if a valid Import or Interface pragma is processed later on. | |
4361 | ||
4362 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
4363 | Set_Body_Required (Parent (N), True); | |
758c442c | 4364 | |
0791fbe9 | 4365 | if Ada_Version >= Ada_2005 |
758c442c GD |
4366 | and then Nkind (Specification (N)) = N_Procedure_Specification |
4367 | and then Null_Present (Specification (N)) | |
4368 | then | |
4369 | Error_Msg_N | |
4370 | ("null procedure cannot be declared at library level", N); | |
4371 | end if; | |
996ae0b0 RK |
4372 | end if; |
4373 | ||
fbf5a39b | 4374 | Generate_Reference_To_Formals (Designator); |
996ae0b0 | 4375 | Check_Eliminated (Designator); |
fbf5a39b | 4376 | |
b1b543d2 BD |
4377 | if Debug_Flag_C then |
4378 | Outdent; | |
4379 | Write_Str ("<== subprogram spec "); | |
4380 | Write_Name (Chars (Designator)); | |
4381 | Write_Str (" from "); | |
4382 | Write_Location (Sloc (N)); | |
4383 | Write_Eol; | |
4384 | end if; | |
0f1a6a0b | 4385 | |
1a265e78 AC |
4386 | if Is_Protected_Type (Current_Scope) then |
4387 | ||
4388 | -- Indicate that this is a protected operation, because it may be | |
4389 | -- used in subsequent declarations within the protected type. | |
4390 | ||
4391 | Set_Convention (Designator, Convention_Protected); | |
4392 | end if; | |
4393 | ||
beacce02 | 4394 | List_Inherited_Pre_Post_Aspects (Designator); |
eaba57fb RD |
4395 | |
4396 | if Has_Aspects (N) then | |
4397 | Analyze_Aspect_Specifications (N, Designator); | |
4398 | end if; | |
996ae0b0 RK |
4399 | end Analyze_Subprogram_Declaration; |
4400 | ||
fbf5a39b AC |
4401 | -------------------------------------- |
4402 | -- Analyze_Subprogram_Specification -- | |
4403 | -------------------------------------- | |
4404 | ||
4405 | -- Reminder: N here really is a subprogram specification (not a subprogram | |
4406 | -- declaration). This procedure is called to analyze the specification in | |
4407 | -- both subprogram bodies and subprogram declarations (specs). | |
4408 | ||
4409 | function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is | |
4410 | Designator : constant Entity_Id := Defining_Entity (N); | |
21d27997 | 4411 | Formals : constant List_Id := Parameter_Specifications (N); |
fbf5a39b | 4412 | |
758c442c GD |
4413 | -- Start of processing for Analyze_Subprogram_Specification |
4414 | ||
fbf5a39b | 4415 | begin |
2ba431e5 | 4416 | -- User-defined operator is not allowed in SPARK, except as a renaming |
38171f43 | 4417 | |
db72f10a AC |
4418 | if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol |
4419 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
4420 | then | |
ce5ba43a AC |
4421 | Check_SPARK_05_Restriction |
4422 | ("user-defined operator is not allowed", N); | |
38171f43 AC |
4423 | end if; |
4424 | ||
31af8899 AC |
4425 | -- Proceed with analysis. Do not emit a cross-reference entry if the |
4426 | -- specification comes from an expression function, because it may be | |
4427 | -- the completion of a previous declaration. It is is not, the cross- | |
4428 | -- reference entry will be emitted for the new subprogram declaration. | |
4429 | ||
4430 | if Nkind (Parent (N)) /= N_Expression_Function then | |
4431 | Generate_Definition (Designator); | |
4432 | end if; | |
38171f43 | 4433 | |
dac3bede | 4434 | Set_Contract (Designator, Make_Contract (Sloc (Designator))); |
fbf5a39b AC |
4435 | |
4436 | if Nkind (N) = N_Function_Specification then | |
4437 | Set_Ekind (Designator, E_Function); | |
4438 | Set_Mechanism (Designator, Default_Mechanism); | |
fbf5a39b AC |
4439 | else |
4440 | Set_Ekind (Designator, E_Procedure); | |
4441 | Set_Etype (Designator, Standard_Void_Type); | |
4442 | end if; | |
4443 | ||
4bd4bb7f AC |
4444 | -- Flag Is_Inlined_Always is True by default, and reversed to False for |
4445 | -- those subprograms which could be inlined in GNATprove mode (because | |
4446 | -- Body_To_Inline is non-Empty) but cannot be inlined. | |
4447 | ||
4448 | if GNATprove_Mode then | |
4449 | Set_Is_Inlined_Always (Designator); | |
4450 | end if; | |
4451 | ||
800621e0 | 4452 | -- Introduce new scope for analysis of the formals and the return type |
82c80734 RD |
4453 | |
4454 | Set_Scope (Designator, Current_Scope); | |
4455 | ||
fbf5a39b | 4456 | if Present (Formals) then |
0a36105d | 4457 | Push_Scope (Designator); |
fbf5a39b | 4458 | Process_Formals (Formals, N); |
758c442c | 4459 | |
0929eaeb AC |
4460 | -- Check dimensions in N for formals with default expression |
4461 | ||
4462 | Analyze_Dimension_Formals (N, Formals); | |
4463 | ||
a38ff9b1 ES |
4464 | -- Ada 2005 (AI-345): If this is an overriding operation of an |
4465 | -- inherited interface operation, and the controlling type is | |
4466 | -- a synchronized type, replace the type with its corresponding | |
4467 | -- record, to match the proper signature of an overriding operation. | |
69cb258c AC |
4468 | -- Same processing for an access parameter whose designated type is |
4469 | -- derived from a synchronized interface. | |
758c442c | 4470 | |
0791fbe9 | 4471 | if Ada_Version >= Ada_2005 then |
d44202ba HK |
4472 | declare |
4473 | Formal : Entity_Id; | |
4474 | Formal_Typ : Entity_Id; | |
4475 | Rec_Typ : Entity_Id; | |
69cb258c | 4476 | Desig_Typ : Entity_Id; |
0a36105d | 4477 | |
d44202ba HK |
4478 | begin |
4479 | Formal := First_Formal (Designator); | |
4480 | while Present (Formal) loop | |
4481 | Formal_Typ := Etype (Formal); | |
0a36105d | 4482 | |
d44202ba HK |
4483 | if Is_Concurrent_Type (Formal_Typ) |
4484 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
4485 | then | |
4486 | Rec_Typ := Corresponding_Record_Type (Formal_Typ); | |
4487 | ||
4488 | if Present (Interfaces (Rec_Typ)) then | |
4489 | Set_Etype (Formal, Rec_Typ); | |
4490 | end if; | |
69cb258c AC |
4491 | |
4492 | elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then | |
4493 | Desig_Typ := Designated_Type (Formal_Typ); | |
4494 | ||
4495 | if Is_Concurrent_Type (Desig_Typ) | |
4496 | and then Present (Corresponding_Record_Type (Desig_Typ)) | |
4497 | then | |
4498 | Rec_Typ := Corresponding_Record_Type (Desig_Typ); | |
4499 | ||
4500 | if Present (Interfaces (Rec_Typ)) then | |
4501 | Set_Directly_Designated_Type (Formal_Typ, Rec_Typ); | |
4502 | end if; | |
4503 | end if; | |
d44202ba HK |
4504 | end if; |
4505 | ||
4506 | Next_Formal (Formal); | |
4507 | end loop; | |
4508 | end; | |
758c442c GD |
4509 | end if; |
4510 | ||
fbf5a39b | 4511 | End_Scope; |
82c80734 | 4512 | |
b66c3ff4 AC |
4513 | -- The subprogram scope is pushed and popped around the processing of |
4514 | -- the return type for consistency with call above to Process_Formals | |
4515 | -- (which itself can call Analyze_Return_Type), and to ensure that any | |
4516 | -- itype created for the return type will be associated with the proper | |
4517 | -- scope. | |
4518 | ||
82c80734 | 4519 | elsif Nkind (N) = N_Function_Specification then |
b66c3ff4 | 4520 | Push_Scope (Designator); |
82c80734 | 4521 | Analyze_Return_Type (N); |
b66c3ff4 | 4522 | End_Scope; |
fbf5a39b AC |
4523 | end if; |
4524 | ||
e606088a AC |
4525 | -- Function case |
4526 | ||
fbf5a39b | 4527 | if Nkind (N) = N_Function_Specification then |
e606088a AC |
4528 | |
4529 | -- Deal with operator symbol case | |
4530 | ||
fbf5a39b AC |
4531 | if Nkind (Designator) = N_Defining_Operator_Symbol then |
4532 | Valid_Operator_Definition (Designator); | |
4533 | end if; | |
4534 | ||
4535 | May_Need_Actuals (Designator); | |
4536 | ||
fe63b1b1 ES |
4537 | -- Ada 2005 (AI-251): If the return type is abstract, verify that |
4538 | -- the subprogram is abstract also. This does not apply to renaming | |
1adaea16 AC |
4539 | -- declarations, where abstractness is inherited, and to subprogram |
4540 | -- bodies generated for stream operations, which become renamings as | |
4541 | -- bodies. | |
2bfb1b72 | 4542 | |
fe63b1b1 ES |
4543 | -- In case of primitives associated with abstract interface types |
4544 | -- the check is applied later (see Analyze_Subprogram_Declaration). | |
ec4867fa | 4545 | |
1adaea16 | 4546 | if not Nkind_In (Original_Node (Parent (N)), |
3f80a182 AC |
4547 | N_Subprogram_Renaming_Declaration, |
4548 | N_Abstract_Subprogram_Declaration, | |
4549 | N_Formal_Abstract_Subprogram_Declaration) | |
fbf5a39b | 4550 | then |
2e79de51 AC |
4551 | if Is_Abstract_Type (Etype (Designator)) |
4552 | and then not Is_Interface (Etype (Designator)) | |
4553 | then | |
4554 | Error_Msg_N | |
4555 | ("function that returns abstract type must be abstract", N); | |
4556 | ||
e606088a | 4557 | -- Ada 2012 (AI-0073): Extend this test to subprograms with an |
2e79de51 AC |
4558 | -- access result whose designated type is abstract. |
4559 | ||
4560 | elsif Nkind (Result_Definition (N)) = N_Access_Definition | |
4561 | and then | |
4562 | not Is_Class_Wide_Type (Designated_Type (Etype (Designator))) | |
4563 | and then Is_Abstract_Type (Designated_Type (Etype (Designator))) | |
dbe945f1 | 4564 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
4565 | then |
4566 | Error_Msg_N ("function whose access result designates " | |
3f80a182 | 4567 | & "abstract type must be abstract", N); |
2e79de51 | 4568 | end if; |
fbf5a39b AC |
4569 | end if; |
4570 | end if; | |
4571 | ||
4572 | return Designator; | |
4573 | end Analyze_Subprogram_Specification; | |
4574 | ||
996ae0b0 RK |
4575 | ----------------------- |
4576 | -- Check_Conformance -- | |
4577 | ----------------------- | |
4578 | ||
4579 | procedure Check_Conformance | |
41251c60 JM |
4580 | (New_Id : Entity_Id; |
4581 | Old_Id : Entity_Id; | |
4582 | Ctype : Conformance_Type; | |
4583 | Errmsg : Boolean; | |
4584 | Conforms : out Boolean; | |
4585 | Err_Loc : Node_Id := Empty; | |
4586 | Get_Inst : Boolean := False; | |
4587 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 | 4588 | is |
996ae0b0 | 4589 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id); |
c27f2f15 RD |
4590 | -- Sets Conforms to False. If Errmsg is False, then that's all it does. |
4591 | -- If Errmsg is True, then processing continues to post an error message | |
4592 | -- for conformance error on given node. Two messages are output. The | |
4593 | -- first message points to the previous declaration with a general "no | |
4594 | -- conformance" message. The second is the detailed reason, supplied as | |
4595 | -- Msg. The parameter N provide information for a possible & insertion | |
4596 | -- in the message, and also provides the location for posting the | |
4597 | -- message in the absence of a specified Err_Loc location. | |
996ae0b0 RK |
4598 | |
4599 | ----------------------- | |
4600 | -- Conformance_Error -- | |
4601 | ----------------------- | |
4602 | ||
4603 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is | |
4604 | Enode : Node_Id; | |
4605 | ||
4606 | begin | |
4607 | Conforms := False; | |
4608 | ||
4609 | if Errmsg then | |
4610 | if No (Err_Loc) then | |
4611 | Enode := N; | |
4612 | else | |
4613 | Enode := Err_Loc; | |
4614 | end if; | |
4615 | ||
4616 | Error_Msg_Sloc := Sloc (Old_Id); | |
4617 | ||
4618 | case Ctype is | |
4619 | when Type_Conformant => | |
483c78cb | 4620 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
4621 | ("not type conformant with declaration#!", Enode); |
4622 | ||
4623 | when Mode_Conformant => | |
19590d70 | 4624 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4625 | Error_Msg_N |
19590d70 GD |
4626 | ("not mode conformant with operation inherited#!", |
4627 | Enode); | |
4628 | else | |
ed2233dc | 4629 | Error_Msg_N |
19590d70 GD |
4630 | ("not mode conformant with declaration#!", Enode); |
4631 | end if; | |
996ae0b0 RK |
4632 | |
4633 | when Subtype_Conformant => | |
19590d70 | 4634 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4635 | Error_Msg_N |
19590d70 GD |
4636 | ("not subtype conformant with operation inherited#!", |
4637 | Enode); | |
4638 | else | |
ed2233dc | 4639 | Error_Msg_N |
19590d70 GD |
4640 | ("not subtype conformant with declaration#!", Enode); |
4641 | end if; | |
996ae0b0 RK |
4642 | |
4643 | when Fully_Conformant => | |
19590d70 | 4644 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
483c78cb | 4645 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4646 | ("not fully conformant with operation inherited#!", |
4647 | Enode); | |
4648 | else | |
483c78cb | 4649 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4650 | ("not fully conformant with declaration#!", Enode); |
4651 | end if; | |
996ae0b0 RK |
4652 | end case; |
4653 | ||
4654 | Error_Msg_NE (Msg, Enode, N); | |
4655 | end if; | |
4656 | end Conformance_Error; | |
4657 | ||
ec4867fa ES |
4658 | -- Local Variables |
4659 | ||
4660 | Old_Type : constant Entity_Id := Etype (Old_Id); | |
4661 | New_Type : constant Entity_Id := Etype (New_Id); | |
4662 | Old_Formal : Entity_Id; | |
4663 | New_Formal : Entity_Id; | |
4664 | Access_Types_Match : Boolean; | |
4665 | Old_Formal_Base : Entity_Id; | |
4666 | New_Formal_Base : Entity_Id; | |
4667 | ||
996ae0b0 RK |
4668 | -- Start of processing for Check_Conformance |
4669 | ||
4670 | begin | |
4671 | Conforms := True; | |
4672 | ||
82c80734 RD |
4673 | -- We need a special case for operators, since they don't appear |
4674 | -- explicitly. | |
996ae0b0 RK |
4675 | |
4676 | if Ctype = Type_Conformant then | |
4677 | if Ekind (New_Id) = E_Operator | |
4678 | and then Operator_Matches_Spec (New_Id, Old_Id) | |
4679 | then | |
4680 | return; | |
4681 | end if; | |
4682 | end if; | |
4683 | ||
4684 | -- If both are functions/operators, check return types conform | |
4685 | ||
4686 | if Old_Type /= Standard_Void_Type | |
4687 | and then New_Type /= Standard_Void_Type | |
4688 | then | |
fceeaab6 ES |
4689 | -- If we are checking interface conformance we omit controlling |
4690 | -- arguments and result, because we are only checking the conformance | |
4691 | -- of the remaining parameters. | |
4692 | ||
4693 | if Has_Controlling_Result (Old_Id) | |
4694 | and then Has_Controlling_Result (New_Id) | |
4695 | and then Skip_Controlling_Formals | |
4696 | then | |
4697 | null; | |
4698 | ||
4699 | elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then | |
7f568bfa AC |
4700 | if Ctype >= Subtype_Conformant |
4701 | and then not Predicates_Match (Old_Type, New_Type) | |
4702 | then | |
4703 | Conformance_Error | |
4704 | ("\predicate of return type does not match!", New_Id); | |
4705 | else | |
4706 | Conformance_Error | |
4707 | ("\return type does not match!", New_Id); | |
4708 | end if; | |
4709 | ||
996ae0b0 RK |
4710 | return; |
4711 | end if; | |
4712 | ||
41251c60 | 4713 | -- Ada 2005 (AI-231): In case of anonymous access types check the |
0a36105d | 4714 | -- null-exclusion and access-to-constant attributes match. |
41251c60 | 4715 | |
0791fbe9 | 4716 | if Ada_Version >= Ada_2005 |
41251c60 JM |
4717 | and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type |
4718 | and then | |
8fde064e AC |
4719 | (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type) |
4720 | or else Is_Access_Constant (Etype (Old_Type)) /= | |
4721 | Is_Access_Constant (Etype (New_Type))) | |
41251c60 | 4722 | then |
5d37ba92 | 4723 | Conformance_Error ("\return type does not match!", New_Id); |
41251c60 JM |
4724 | return; |
4725 | end if; | |
4726 | ||
996ae0b0 RK |
4727 | -- If either is a function/operator and the other isn't, error |
4728 | ||
4729 | elsif Old_Type /= Standard_Void_Type | |
4730 | or else New_Type /= Standard_Void_Type | |
4731 | then | |
5d37ba92 | 4732 | Conformance_Error ("\functions can only match functions!", New_Id); |
996ae0b0 RK |
4733 | return; |
4734 | end if; | |
4735 | ||
0a36105d | 4736 | -- In subtype conformant case, conventions must match (RM 6.3.1(16)). |
996ae0b0 RK |
4737 | -- If this is a renaming as body, refine error message to indicate that |
4738 | -- the conflict is with the original declaration. If the entity is not | |
4739 | -- frozen, the conventions don't have to match, the one of the renamed | |
4740 | -- entity is inherited. | |
4741 | ||
4742 | if Ctype >= Subtype_Conformant then | |
996ae0b0 | 4743 | if Convention (Old_Id) /= Convention (New_Id) then |
996ae0b0 RK |
4744 | if not Is_Frozen (New_Id) then |
4745 | null; | |
4746 | ||
4747 | elsif Present (Err_Loc) | |
4748 | and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration | |
4749 | and then Present (Corresponding_Spec (Err_Loc)) | |
4750 | then | |
4751 | Error_Msg_Name_1 := Chars (New_Id); | |
4752 | Error_Msg_Name_2 := | |
4753 | Name_Ada + Convention_Id'Pos (Convention (New_Id)); | |
5d37ba92 | 4754 | Conformance_Error ("\prior declaration for% has convention %!"); |
996ae0b0 RK |
4755 | |
4756 | else | |
5d37ba92 | 4757 | Conformance_Error ("\calling conventions do not match!"); |
996ae0b0 RK |
4758 | end if; |
4759 | ||
4760 | return; | |
4761 | ||
4762 | elsif Is_Formal_Subprogram (Old_Id) | |
4763 | or else Is_Formal_Subprogram (New_Id) | |
4764 | then | |
5d37ba92 | 4765 | Conformance_Error ("\formal subprograms not allowed!"); |
996ae0b0 | 4766 | return; |
c5cec2fe AC |
4767 | |
4768 | -- Pragma Ghost behaves as a convention in the context of subtype | |
4769 | -- conformance (SPARK RM 6.9(5)). | |
4770 | ||
4771 | elsif Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id) then | |
4772 | Conformance_Error ("\ghost modes do not match!"); | |
4773 | return; | |
996ae0b0 RK |
4774 | end if; |
4775 | end if; | |
4776 | ||
4777 | -- Deal with parameters | |
4778 | ||
4779 | -- Note: we use the entity information, rather than going directly | |
4780 | -- to the specification in the tree. This is not only simpler, but | |
4781 | -- absolutely necessary for some cases of conformance tests between | |
a90bd866 | 4782 | -- operators, where the declaration tree simply does not exist. |
996ae0b0 RK |
4783 | |
4784 | Old_Formal := First_Formal (Old_Id); | |
4785 | New_Formal := First_Formal (New_Id); | |
996ae0b0 | 4786 | while Present (Old_Formal) and then Present (New_Formal) loop |
41251c60 JM |
4787 | if Is_Controlling_Formal (Old_Formal) |
4788 | and then Is_Controlling_Formal (New_Formal) | |
4789 | and then Skip_Controlling_Formals | |
4790 | then | |
a2dc5812 AC |
4791 | -- The controlling formals will have different types when |
4792 | -- comparing an interface operation with its match, but both | |
4793 | -- or neither must be access parameters. | |
4794 | ||
4795 | if Is_Access_Type (Etype (Old_Formal)) | |
4796 | = | |
4797 | Is_Access_Type (Etype (New_Formal)) | |
4798 | then | |
4799 | goto Skip_Controlling_Formal; | |
4800 | else | |
4801 | Conformance_Error | |
4802 | ("\access parameter does not match!", New_Formal); | |
4803 | end if; | |
41251c60 JM |
4804 | end if; |
4805 | ||
21791d97 | 4806 | -- Ada 2012: Mode conformance also requires that formal parameters |
2a290fec AC |
4807 | -- be both aliased, or neither. |
4808 | ||
21791d97 | 4809 | if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then |
2a290fec AC |
4810 | if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then |
4811 | Conformance_Error | |
4812 | ("\aliased parameter mismatch!", New_Formal); | |
4813 | end if; | |
4814 | end if; | |
4815 | ||
fbf5a39b AC |
4816 | if Ctype = Fully_Conformant then |
4817 | ||
4818 | -- Names must match. Error message is more accurate if we do | |
4819 | -- this before checking that the types of the formals match. | |
4820 | ||
4821 | if Chars (Old_Formal) /= Chars (New_Formal) then | |
3ccedacc | 4822 | Conformance_Error ("\name& does not match!", New_Formal); |
fbf5a39b AC |
4823 | |
4824 | -- Set error posted flag on new formal as well to stop | |
4825 | -- junk cascaded messages in some cases. | |
4826 | ||
4827 | Set_Error_Posted (New_Formal); | |
4828 | return; | |
4829 | end if; | |
40b93859 RD |
4830 | |
4831 | -- Null exclusion must match | |
4832 | ||
4833 | if Null_Exclusion_Present (Parent (Old_Formal)) | |
4834 | /= | |
4835 | Null_Exclusion_Present (Parent (New_Formal)) | |
4836 | then | |
4837 | -- Only give error if both come from source. This should be | |
4838 | -- investigated some time, since it should not be needed ??? | |
4839 | ||
4840 | if Comes_From_Source (Old_Formal) | |
4841 | and then | |
4842 | Comes_From_Source (New_Formal) | |
4843 | then | |
4844 | Conformance_Error | |
3ccedacc | 4845 | ("\null exclusion for& does not match", New_Formal); |
40b93859 RD |
4846 | |
4847 | -- Mark error posted on the new formal to avoid duplicated | |
4848 | -- complaint about types not matching. | |
4849 | ||
4850 | Set_Error_Posted (New_Formal); | |
4851 | end if; | |
4852 | end if; | |
fbf5a39b | 4853 | end if; |
996ae0b0 | 4854 | |
ec4867fa ES |
4855 | -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This |
4856 | -- case occurs whenever a subprogram is being renamed and one of its | |
4857 | -- parameters imposes a null exclusion. For example: | |
4858 | ||
4859 | -- type T is null record; | |
4860 | -- type Acc_T is access T; | |
4861 | -- subtype Acc_T_Sub is Acc_T; | |
4862 | ||
4863 | -- procedure P (Obj : not null Acc_T_Sub); -- itype | |
4864 | -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype | |
4865 | -- renames P; | |
4866 | ||
4867 | Old_Formal_Base := Etype (Old_Formal); | |
4868 | New_Formal_Base := Etype (New_Formal); | |
4869 | ||
4870 | if Get_Inst then | |
4871 | Old_Formal_Base := Get_Instance_Of (Old_Formal_Base); | |
4872 | New_Formal_Base := Get_Instance_Of (New_Formal_Base); | |
4873 | end if; | |
4874 | ||
0791fbe9 | 4875 | Access_Types_Match := Ada_Version >= Ada_2005 |
ec4867fa | 4876 | |
8fde064e AC |
4877 | -- Ensure that this rule is only applied when New_Id is a |
4878 | -- renaming of Old_Id. | |
ec4867fa | 4879 | |
5d37ba92 ES |
4880 | and then Nkind (Parent (Parent (New_Id))) = |
4881 | N_Subprogram_Renaming_Declaration | |
ec4867fa ES |
4882 | and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity |
4883 | and then Present (Entity (Name (Parent (Parent (New_Id))))) | |
4884 | and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id | |
4885 | ||
8fde064e | 4886 | -- Now handle the allowed access-type case |
ec4867fa ES |
4887 | |
4888 | and then Is_Access_Type (Old_Formal_Base) | |
4889 | and then Is_Access_Type (New_Formal_Base) | |
5d37ba92 | 4890 | |
8fde064e AC |
4891 | -- The type kinds must match. The only exception occurs with |
4892 | -- multiple generics of the form: | |
5d37ba92 | 4893 | |
8fde064e AC |
4894 | -- generic generic |
4895 | -- type F is private; type A is private; | |
4896 | -- type F_Ptr is access F; type A_Ptr is access A; | |
4897 | -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr); | |
4898 | -- package F_Pack is ... package A_Pack is | |
4899 | -- package F_Inst is | |
4900 | -- new F_Pack (A, A_Ptr, A_P); | |
5d37ba92 | 4901 | |
8fde064e AC |
4902 | -- When checking for conformance between the parameters of A_P |
4903 | -- and F_P, the type kinds of F_Ptr and A_Ptr will not match | |
4904 | -- because the compiler has transformed A_Ptr into a subtype of | |
4905 | -- F_Ptr. We catch this case in the code below. | |
5d37ba92 ES |
4906 | |
4907 | and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base) | |
4908 | or else | |
4909 | (Is_Generic_Type (Old_Formal_Base) | |
4910 | and then Is_Generic_Type (New_Formal_Base) | |
4911 | and then Is_Internal (New_Formal_Base) | |
4912 | and then Etype (Etype (New_Formal_Base)) = | |
4913 | Old_Formal_Base)) | |
ec4867fa | 4914 | and then Directly_Designated_Type (Old_Formal_Base) = |
8fde064e | 4915 | Directly_Designated_Type (New_Formal_Base) |
ec4867fa ES |
4916 | and then ((Is_Itype (Old_Formal_Base) |
4917 | and then Can_Never_Be_Null (Old_Formal_Base)) | |
4918 | or else | |
4919 | (Is_Itype (New_Formal_Base) | |
4920 | and then Can_Never_Be_Null (New_Formal_Base))); | |
4921 | ||
996ae0b0 RK |
4922 | -- Types must always match. In the visible part of an instance, |
4923 | -- usual overloading rules for dispatching operations apply, and | |
4924 | -- we check base types (not the actual subtypes). | |
4925 | ||
4926 | if In_Instance_Visible_Part | |
4927 | and then Is_Dispatching_Operation (New_Id) | |
4928 | then | |
4929 | if not Conforming_Types | |
ec4867fa ES |
4930 | (T1 => Base_Type (Etype (Old_Formal)), |
4931 | T2 => Base_Type (Etype (New_Formal)), | |
4932 | Ctype => Ctype, | |
4933 | Get_Inst => Get_Inst) | |
4934 | and then not Access_Types_Match | |
996ae0b0 | 4935 | then |
5d37ba92 | 4936 | Conformance_Error ("\type of & does not match!", New_Formal); |
996ae0b0 RK |
4937 | return; |
4938 | end if; | |
4939 | ||
4940 | elsif not Conforming_Types | |
5d37ba92 ES |
4941 | (T1 => Old_Formal_Base, |
4942 | T2 => New_Formal_Base, | |
ec4867fa ES |
4943 | Ctype => Ctype, |
4944 | Get_Inst => Get_Inst) | |
4945 | and then not Access_Types_Match | |
996ae0b0 | 4946 | then |
c27f2f15 RD |
4947 | -- Don't give error message if old type is Any_Type. This test |
4948 | -- avoids some cascaded errors, e.g. in case of a bad spec. | |
4949 | ||
4950 | if Errmsg and then Old_Formal_Base = Any_Type then | |
4951 | Conforms := False; | |
4952 | else | |
7f568bfa AC |
4953 | if Ctype >= Subtype_Conformant |
4954 | and then | |
4955 | not Predicates_Match (Old_Formal_Base, New_Formal_Base) | |
4956 | then | |
4957 | Conformance_Error | |
4958 | ("\predicate of & does not match!", New_Formal); | |
4959 | else | |
4960 | Conformance_Error | |
4961 | ("\type of & does not match!", New_Formal); | |
4962 | end if; | |
c27f2f15 RD |
4963 | end if; |
4964 | ||
996ae0b0 RK |
4965 | return; |
4966 | end if; | |
4967 | ||
4968 | -- For mode conformance, mode must match | |
4969 | ||
5d37ba92 ES |
4970 | if Ctype >= Mode_Conformant then |
4971 | if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then | |
dd54644b JM |
4972 | if not Ekind_In (New_Id, E_Function, E_Procedure) |
4973 | or else not Is_Primitive_Wrapper (New_Id) | |
4974 | then | |
4975 | Conformance_Error ("\mode of & does not match!", New_Formal); | |
c199ccf7 | 4976 | |
dd54644b JM |
4977 | else |
4978 | declare | |
c199ccf7 | 4979 | T : constant Entity_Id := Find_Dispatching_Type (New_Id); |
dd54644b | 4980 | begin |
3ccedacc | 4981 | if Is_Protected_Type (Corresponding_Concurrent_Type (T)) |
dd54644b JM |
4982 | then |
4983 | Error_Msg_PT (T, New_Id); | |
4984 | else | |
4985 | Conformance_Error | |
4986 | ("\mode of & does not match!", New_Formal); | |
4987 | end if; | |
4988 | end; | |
4989 | end if; | |
4990 | ||
5d37ba92 ES |
4991 | return; |
4992 | ||
4993 | -- Part of mode conformance for access types is having the same | |
4994 | -- constant modifier. | |
4995 | ||
4996 | elsif Access_Types_Match | |
4997 | and then Is_Access_Constant (Old_Formal_Base) /= | |
4998 | Is_Access_Constant (New_Formal_Base) | |
4999 | then | |
5000 | Conformance_Error | |
5001 | ("\constant modifier does not match!", New_Formal); | |
5002 | return; | |
5003 | end if; | |
996ae0b0 RK |
5004 | end if; |
5005 | ||
0a36105d | 5006 | if Ctype >= Subtype_Conformant then |
996ae0b0 | 5007 | |
0a36105d JM |
5008 | -- Ada 2005 (AI-231): In case of anonymous access types check |
5009 | -- the null-exclusion and access-to-constant attributes must | |
c7b9d548 AC |
5010 | -- match. For null exclusion, we test the types rather than the |
5011 | -- formals themselves, since the attribute is only set reliably | |
5012 | -- on the formals in the Ada 95 case, and we exclude the case | |
5013 | -- where Old_Formal is marked as controlling, to avoid errors | |
5014 | -- when matching completing bodies with dispatching declarations | |
5015 | -- (access formals in the bodies aren't marked Can_Never_Be_Null). | |
996ae0b0 | 5016 | |
0791fbe9 | 5017 | if Ada_Version >= Ada_2005 |
0a36105d JM |
5018 | and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type |
5019 | and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type | |
5020 | and then | |
c7b9d548 AC |
5021 | ((Can_Never_Be_Null (Etype (Old_Formal)) /= |
5022 | Can_Never_Be_Null (Etype (New_Formal)) | |
5023 | and then | |
5024 | not Is_Controlling_Formal (Old_Formal)) | |
0a36105d JM |
5025 | or else |
5026 | Is_Access_Constant (Etype (Old_Formal)) /= | |
5027 | Is_Access_Constant (Etype (New_Formal))) | |
40b93859 RD |
5028 | |
5029 | -- Do not complain if error already posted on New_Formal. This | |
5030 | -- avoids some redundant error messages. | |
5031 | ||
5032 | and then not Error_Posted (New_Formal) | |
0a36105d JM |
5033 | then |
5034 | -- It is allowed to omit the null-exclusion in case of stream | |
5035 | -- attribute subprograms. We recognize stream subprograms | |
5036 | -- through their TSS-generated suffix. | |
996ae0b0 | 5037 | |
0a36105d JM |
5038 | declare |
5039 | TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id); | |
3ada950b | 5040 | |
0a36105d JM |
5041 | begin |
5042 | if TSS_Name /= TSS_Stream_Read | |
5043 | and then TSS_Name /= TSS_Stream_Write | |
5044 | and then TSS_Name /= TSS_Stream_Input | |
5045 | and then TSS_Name /= TSS_Stream_Output | |
5046 | then | |
3ada950b | 5047 | -- Here we have a definite conformance error. It is worth |
71fb4dc8 | 5048 | -- special casing the error message for the case of a |
3ada950b AC |
5049 | -- controlling formal (which excludes null). |
5050 | ||
5051 | if Is_Controlling_Formal (New_Formal) then | |
5052 | Error_Msg_Node_2 := Scope (New_Formal); | |
5053 | Conformance_Error | |
3ccedacc AC |
5054 | ("\controlling formal & of & excludes null, " |
5055 | & "declaration must exclude null as well", | |
5056 | New_Formal); | |
3ada950b AC |
5057 | |
5058 | -- Normal case (couldn't we give more detail here???) | |
5059 | ||
5060 | else | |
5061 | Conformance_Error | |
5062 | ("\type of & does not match!", New_Formal); | |
5063 | end if; | |
5064 | ||
0a36105d JM |
5065 | return; |
5066 | end if; | |
5067 | end; | |
5068 | end if; | |
5069 | end if; | |
41251c60 | 5070 | |
0a36105d | 5071 | -- Full conformance checks |
41251c60 | 5072 | |
0a36105d | 5073 | if Ctype = Fully_Conformant then |
e660dbf7 | 5074 | |
0a36105d | 5075 | -- We have checked already that names match |
e660dbf7 | 5076 | |
0a36105d | 5077 | if Parameter_Mode (Old_Formal) = E_In_Parameter then |
41251c60 JM |
5078 | |
5079 | -- Check default expressions for in parameters | |
5080 | ||
996ae0b0 RK |
5081 | declare |
5082 | NewD : constant Boolean := | |
5083 | Present (Default_Value (New_Formal)); | |
5084 | OldD : constant Boolean := | |
5085 | Present (Default_Value (Old_Formal)); | |
5086 | begin | |
5087 | if NewD or OldD then | |
5088 | ||
82c80734 RD |
5089 | -- The old default value has been analyzed because the |
5090 | -- current full declaration will have frozen everything | |
0a36105d JM |
5091 | -- before. The new default value has not been analyzed, |
5092 | -- so analyze it now before we check for conformance. | |
996ae0b0 RK |
5093 | |
5094 | if NewD then | |
0a36105d | 5095 | Push_Scope (New_Id); |
21d27997 | 5096 | Preanalyze_Spec_Expression |
fbf5a39b | 5097 | (Default_Value (New_Formal), Etype (New_Formal)); |
996ae0b0 RK |
5098 | End_Scope; |
5099 | end if; | |
5100 | ||
5101 | if not (NewD and OldD) | |
5102 | or else not Fully_Conformant_Expressions | |
5103 | (Default_Value (Old_Formal), | |
5104 | Default_Value (New_Formal)) | |
5105 | then | |
5106 | Conformance_Error | |
5d37ba92 | 5107 | ("\default expression for & does not match!", |
996ae0b0 RK |
5108 | New_Formal); |
5109 | return; | |
5110 | end if; | |
5111 | end if; | |
5112 | end; | |
5113 | end if; | |
5114 | end if; | |
5115 | ||
5116 | -- A couple of special checks for Ada 83 mode. These checks are | |
0a36105d | 5117 | -- skipped if either entity is an operator in package Standard, |
996ae0b0 RK |
5118 | -- or if either old or new instance is not from the source program. |
5119 | ||
0ab80019 | 5120 | if Ada_Version = Ada_83 |
996ae0b0 RK |
5121 | and then Sloc (Old_Id) > Standard_Location |
5122 | and then Sloc (New_Id) > Standard_Location | |
5123 | and then Comes_From_Source (Old_Id) | |
5124 | and then Comes_From_Source (New_Id) | |
5125 | then | |
5126 | declare | |
5127 | Old_Param : constant Node_Id := Declaration_Node (Old_Formal); | |
5128 | New_Param : constant Node_Id := Declaration_Node (New_Formal); | |
5129 | ||
5130 | begin | |
5131 | -- Explicit IN must be present or absent in both cases. This | |
5132 | -- test is required only in the full conformance case. | |
5133 | ||
5134 | if In_Present (Old_Param) /= In_Present (New_Param) | |
5135 | and then Ctype = Fully_Conformant | |
5136 | then | |
5137 | Conformance_Error | |
5d37ba92 | 5138 | ("\(Ada 83) IN must appear in both declarations", |
996ae0b0 RK |
5139 | New_Formal); |
5140 | return; | |
5141 | end if; | |
5142 | ||
5143 | -- Grouping (use of comma in param lists) must be the same | |
5144 | -- This is where we catch a misconformance like: | |
5145 | ||
0a36105d | 5146 | -- A, B : Integer |
996ae0b0 RK |
5147 | -- A : Integer; B : Integer |
5148 | ||
5149 | -- which are represented identically in the tree except | |
5150 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5151 | ||
5152 | if More_Ids (Old_Param) /= More_Ids (New_Param) | |
5153 | or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param) | |
5154 | then | |
5155 | Conformance_Error | |
5d37ba92 | 5156 | ("\grouping of & does not match!", New_Formal); |
996ae0b0 RK |
5157 | return; |
5158 | end if; | |
5159 | end; | |
5160 | end if; | |
5161 | ||
41251c60 JM |
5162 | -- This label is required when skipping controlling formals |
5163 | ||
5164 | <<Skip_Controlling_Formal>> | |
5165 | ||
996ae0b0 RK |
5166 | Next_Formal (Old_Formal); |
5167 | Next_Formal (New_Formal); | |
5168 | end loop; | |
5169 | ||
5170 | if Present (Old_Formal) then | |
5d37ba92 | 5171 | Conformance_Error ("\too few parameters!"); |
996ae0b0 RK |
5172 | return; |
5173 | ||
5174 | elsif Present (New_Formal) then | |
5d37ba92 | 5175 | Conformance_Error ("\too many parameters!", New_Formal); |
996ae0b0 RK |
5176 | return; |
5177 | end if; | |
996ae0b0 RK |
5178 | end Check_Conformance; |
5179 | ||
ec4867fa ES |
5180 | ----------------------- |
5181 | -- Check_Conventions -- | |
5182 | ----------------------- | |
5183 | ||
5184 | procedure Check_Conventions (Typ : Entity_Id) is | |
ce2b6ba5 | 5185 | Ifaces_List : Elist_Id; |
0a36105d | 5186 | |
ce2b6ba5 | 5187 | procedure Check_Convention (Op : Entity_Id); |
0a36105d JM |
5188 | -- Verify that the convention of inherited dispatching operation Op is |
5189 | -- consistent among all subprograms it overrides. In order to minimize | |
5190 | -- the search, Search_From is utilized to designate a specific point in | |
5191 | -- the list rather than iterating over the whole list once more. | |
ec4867fa ES |
5192 | |
5193 | ---------------------- | |
5194 | -- Check_Convention -- | |
5195 | ---------------------- | |
5196 | ||
ce2b6ba5 | 5197 | procedure Check_Convention (Op : Entity_Id) is |
c5cec2fe | 5198 | Op_Conv : constant Convention_Id := Convention (Op); |
9f6aaa5c | 5199 | Iface_Conv : Convention_Id; |
ce2b6ba5 JM |
5200 | Iface_Elmt : Elmt_Id; |
5201 | Iface_Prim_Elmt : Elmt_Id; | |
5202 | Iface_Prim : Entity_Id; | |
ec4867fa | 5203 | |
ce2b6ba5 JM |
5204 | begin |
5205 | Iface_Elmt := First_Elmt (Ifaces_List); | |
5206 | while Present (Iface_Elmt) loop | |
5207 | Iface_Prim_Elmt := | |
9f6aaa5c | 5208 | First_Elmt (Primitive_Operations (Node (Iface_Elmt))); |
ce2b6ba5 JM |
5209 | while Present (Iface_Prim_Elmt) loop |
5210 | Iface_Prim := Node (Iface_Prim_Elmt); | |
c5cec2fe | 5211 | Iface_Conv := Convention (Iface_Prim); |
ce2b6ba5 JM |
5212 | |
5213 | if Is_Interface_Conformant (Typ, Iface_Prim, Op) | |
9f6aaa5c | 5214 | and then Iface_Conv /= Op_Conv |
ce2b6ba5 | 5215 | then |
ed2233dc | 5216 | Error_Msg_N |
ce2b6ba5 | 5217 | ("inconsistent conventions in primitive operations", Typ); |
ec4867fa | 5218 | |
ce2b6ba5 | 5219 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c | 5220 | Error_Msg_Name_2 := Get_Convention_Name (Op_Conv); |
ce2b6ba5 | 5221 | Error_Msg_Sloc := Sloc (Op); |
ec4867fa | 5222 | |
7a963087 | 5223 | if Comes_From_Source (Op) or else No (Alias (Op)) then |
038140ed | 5224 | if not Present (Overridden_Operation (Op)) then |
ed2233dc | 5225 | Error_Msg_N ("\\primitive % defined #", Typ); |
ce2b6ba5 | 5226 | else |
ed2233dc | 5227 | Error_Msg_N |
3ccedacc AC |
5228 | ("\\overriding operation % with " |
5229 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5230 | end if; |
ec4867fa | 5231 | |
ce2b6ba5 JM |
5232 | else pragma Assert (Present (Alias (Op))); |
5233 | Error_Msg_Sloc := Sloc (Alias (Op)); | |
3ccedacc AC |
5234 | Error_Msg_N ("\\inherited operation % with " |
5235 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5236 | end if; |
ec4867fa | 5237 | |
ce2b6ba5 | 5238 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c AC |
5239 | Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv); |
5240 | Error_Msg_Sloc := Sloc (Iface_Prim); | |
3ccedacc AC |
5241 | Error_Msg_N ("\\overridden operation % with " |
5242 | & "convention % defined #", Typ); | |
ec4867fa | 5243 | |
ce2b6ba5 | 5244 | -- Avoid cascading errors |
ec4867fa | 5245 | |
ce2b6ba5 JM |
5246 | return; |
5247 | end if; | |
ec4867fa | 5248 | |
ce2b6ba5 JM |
5249 | Next_Elmt (Iface_Prim_Elmt); |
5250 | end loop; | |
ec4867fa | 5251 | |
ce2b6ba5 | 5252 | Next_Elmt (Iface_Elmt); |
ec4867fa ES |
5253 | end loop; |
5254 | end Check_Convention; | |
5255 | ||
5256 | -- Local variables | |
5257 | ||
5258 | Prim_Op : Entity_Id; | |
5259 | Prim_Op_Elmt : Elmt_Id; | |
5260 | ||
5261 | -- Start of processing for Check_Conventions | |
5262 | ||
5263 | begin | |
ce2b6ba5 JM |
5264 | if not Has_Interfaces (Typ) then |
5265 | return; | |
5266 | end if; | |
5267 | ||
5268 | Collect_Interfaces (Typ, Ifaces_List); | |
5269 | ||
0a36105d JM |
5270 | -- The algorithm checks every overriding dispatching operation against |
5271 | -- all the corresponding overridden dispatching operations, detecting | |
f3d57416 | 5272 | -- differences in conventions. |
ec4867fa ES |
5273 | |
5274 | Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
5275 | while Present (Prim_Op_Elmt) loop | |
5276 | Prim_Op := Node (Prim_Op_Elmt); | |
5277 | ||
0a36105d | 5278 | -- A small optimization: skip the predefined dispatching operations |
ce2b6ba5 | 5279 | -- since they always have the same convention. |
ec4867fa | 5280 | |
ce2b6ba5 JM |
5281 | if not Is_Predefined_Dispatching_Operation (Prim_Op) then |
5282 | Check_Convention (Prim_Op); | |
ec4867fa ES |
5283 | end if; |
5284 | ||
5285 | Next_Elmt (Prim_Op_Elmt); | |
5286 | end loop; | |
5287 | end Check_Conventions; | |
5288 | ||
996ae0b0 RK |
5289 | ------------------------------ |
5290 | -- Check_Delayed_Subprogram -- | |
5291 | ------------------------------ | |
5292 | ||
5293 | procedure Check_Delayed_Subprogram (Designator : Entity_Id) is | |
5294 | F : Entity_Id; | |
5295 | ||
5296 | procedure Possible_Freeze (T : Entity_Id); | |
5297 | -- T is the type of either a formal parameter or of the return type. | |
5298 | -- If T is not yet frozen and needs a delayed freeze, then the | |
4a13695c AC |
5299 | -- subprogram itself must be delayed. If T is the limited view of an |
5300 | -- incomplete type the subprogram must be frozen as well, because | |
5301 | -- T may depend on local types that have not been frozen yet. | |
996ae0b0 | 5302 | |
82c80734 RD |
5303 | --------------------- |
5304 | -- Possible_Freeze -- | |
5305 | --------------------- | |
5306 | ||
996ae0b0 RK |
5307 | procedure Possible_Freeze (T : Entity_Id) is |
5308 | begin | |
4a13695c | 5309 | if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then |
996ae0b0 RK |
5310 | Set_Has_Delayed_Freeze (Designator); |
5311 | ||
5312 | elsif Is_Access_Type (T) | |
5313 | and then Has_Delayed_Freeze (Designated_Type (T)) | |
5314 | and then not Is_Frozen (Designated_Type (T)) | |
5315 | then | |
5316 | Set_Has_Delayed_Freeze (Designator); | |
e358346d | 5317 | |
7b56a91b AC |
5318 | elsif Ekind (T) = E_Incomplete_Type |
5319 | and then From_Limited_With (T) | |
5320 | then | |
e358346d | 5321 | Set_Has_Delayed_Freeze (Designator); |
406935b6 | 5322 | |
9aff36e9 RD |
5323 | -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile |
5324 | -- of a subprogram or entry declaration. | |
406935b6 AC |
5325 | |
5326 | elsif Ekind (T) = E_Incomplete_Type | |
5327 | and then Ada_Version >= Ada_2012 | |
5328 | then | |
5329 | Set_Has_Delayed_Freeze (Designator); | |
996ae0b0 | 5330 | end if; |
4a13695c | 5331 | |
996ae0b0 RK |
5332 | end Possible_Freeze; |
5333 | ||
5334 | -- Start of processing for Check_Delayed_Subprogram | |
5335 | ||
5336 | begin | |
76e3504f AC |
5337 | -- All subprograms, including abstract subprograms, may need a freeze |
5338 | -- node if some formal type or the return type needs one. | |
996ae0b0 | 5339 | |
76e3504f AC |
5340 | Possible_Freeze (Etype (Designator)); |
5341 | Possible_Freeze (Base_Type (Etype (Designator))); -- needed ??? | |
996ae0b0 | 5342 | |
76e3504f AC |
5343 | -- Need delayed freeze if any of the formal types themselves need |
5344 | -- a delayed freeze and are not yet frozen. | |
996ae0b0 | 5345 | |
76e3504f AC |
5346 | F := First_Formal (Designator); |
5347 | while Present (F) loop | |
5348 | Possible_Freeze (Etype (F)); | |
5349 | Possible_Freeze (Base_Type (Etype (F))); -- needed ??? | |
5350 | Next_Formal (F); | |
5351 | end loop; | |
996ae0b0 RK |
5352 | |
5353 | -- Mark functions that return by reference. Note that it cannot be | |
5354 | -- done for delayed_freeze subprograms because the underlying | |
5355 | -- returned type may not be known yet (for private types) | |
5356 | ||
8fde064e | 5357 | if not Has_Delayed_Freeze (Designator) and then Expander_Active then |
996ae0b0 RK |
5358 | declare |
5359 | Typ : constant Entity_Id := Etype (Designator); | |
5360 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
996ae0b0 | 5361 | begin |
51245e2d | 5362 | if Is_Limited_View (Typ) then |
996ae0b0 | 5363 | Set_Returns_By_Ref (Designator); |
048e5cef | 5364 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
996ae0b0 RK |
5365 | Set_Returns_By_Ref (Designator); |
5366 | end if; | |
5367 | end; | |
5368 | end if; | |
5369 | end Check_Delayed_Subprogram; | |
5370 | ||
5371 | ------------------------------------ | |
5372 | -- Check_Discriminant_Conformance -- | |
5373 | ------------------------------------ | |
5374 | ||
5375 | procedure Check_Discriminant_Conformance | |
5376 | (N : Node_Id; | |
5377 | Prev : Entity_Id; | |
5378 | Prev_Loc : Node_Id) | |
5379 | is | |
5380 | Old_Discr : Entity_Id := First_Discriminant (Prev); | |
5381 | New_Discr : Node_Id := First (Discriminant_Specifications (N)); | |
5382 | New_Discr_Id : Entity_Id; | |
5383 | New_Discr_Type : Entity_Id; | |
5384 | ||
5385 | procedure Conformance_Error (Msg : String; N : Node_Id); | |
82c80734 RD |
5386 | -- Post error message for conformance error on given node. Two messages |
5387 | -- are output. The first points to the previous declaration with a | |
5388 | -- general "no conformance" message. The second is the detailed reason, | |
5389 | -- supplied as Msg. The parameter N provide information for a possible | |
5390 | -- & insertion in the message. | |
996ae0b0 RK |
5391 | |
5392 | ----------------------- | |
5393 | -- Conformance_Error -- | |
5394 | ----------------------- | |
5395 | ||
5396 | procedure Conformance_Error (Msg : String; N : Node_Id) is | |
5397 | begin | |
5398 | Error_Msg_Sloc := Sloc (Prev_Loc); | |
483c78cb RD |
5399 | Error_Msg_N -- CODEFIX |
5400 | ("not fully conformant with declaration#!", N); | |
996ae0b0 RK |
5401 | Error_Msg_NE (Msg, N, N); |
5402 | end Conformance_Error; | |
5403 | ||
5404 | -- Start of processing for Check_Discriminant_Conformance | |
5405 | ||
5406 | begin | |
5407 | while Present (Old_Discr) and then Present (New_Discr) loop | |
996ae0b0 RK |
5408 | New_Discr_Id := Defining_Identifier (New_Discr); |
5409 | ||
82c80734 RD |
5410 | -- The subtype mark of the discriminant on the full type has not |
5411 | -- been analyzed so we do it here. For an access discriminant a new | |
5412 | -- type is created. | |
996ae0b0 RK |
5413 | |
5414 | if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then | |
5415 | New_Discr_Type := | |
5416 | Access_Definition (N, Discriminant_Type (New_Discr)); | |
5417 | ||
5418 | else | |
5419 | Analyze (Discriminant_Type (New_Discr)); | |
5420 | New_Discr_Type := Etype (Discriminant_Type (New_Discr)); | |
e50e1c5e AC |
5421 | |
5422 | -- Ada 2005: if the discriminant definition carries a null | |
5423 | -- exclusion, create an itype to check properly for consistency | |
5424 | -- with partial declaration. | |
5425 | ||
5426 | if Is_Access_Type (New_Discr_Type) | |
8fde064e | 5427 | and then Null_Exclusion_Present (New_Discr) |
e50e1c5e AC |
5428 | then |
5429 | New_Discr_Type := | |
5430 | Create_Null_Excluding_Itype | |
5431 | (T => New_Discr_Type, | |
5432 | Related_Nod => New_Discr, | |
5433 | Scope_Id => Current_Scope); | |
5434 | end if; | |
996ae0b0 RK |
5435 | end if; |
5436 | ||
5437 | if not Conforming_Types | |
5438 | (Etype (Old_Discr), New_Discr_Type, Fully_Conformant) | |
5439 | then | |
5440 | Conformance_Error ("type of & does not match!", New_Discr_Id); | |
5441 | return; | |
fbf5a39b | 5442 | else |
82c80734 RD |
5443 | -- Treat the new discriminant as an occurrence of the old one, |
5444 | -- for navigation purposes, and fill in some semantic | |
fbf5a39b AC |
5445 | -- information, for completeness. |
5446 | ||
5447 | Generate_Reference (Old_Discr, New_Discr_Id, 'r'); | |
5448 | Set_Etype (New_Discr_Id, Etype (Old_Discr)); | |
5449 | Set_Scope (New_Discr_Id, Scope (Old_Discr)); | |
996ae0b0 RK |
5450 | end if; |
5451 | ||
5452 | -- Names must match | |
5453 | ||
5454 | if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then | |
5455 | Conformance_Error ("name & does not match!", New_Discr_Id); | |
5456 | return; | |
5457 | end if; | |
5458 | ||
5459 | -- Default expressions must match | |
5460 | ||
5461 | declare | |
5462 | NewD : constant Boolean := | |
5463 | Present (Expression (New_Discr)); | |
5464 | OldD : constant Boolean := | |
5465 | Present (Expression (Parent (Old_Discr))); | |
5466 | ||
5467 | begin | |
5468 | if NewD or OldD then | |
5469 | ||
5470 | -- The old default value has been analyzed and expanded, | |
5471 | -- because the current full declaration will have frozen | |
82c80734 RD |
5472 | -- everything before. The new default values have not been |
5473 | -- expanded, so expand now to check conformance. | |
996ae0b0 RK |
5474 | |
5475 | if NewD then | |
21d27997 | 5476 | Preanalyze_Spec_Expression |
996ae0b0 RK |
5477 | (Expression (New_Discr), New_Discr_Type); |
5478 | end if; | |
5479 | ||
5480 | if not (NewD and OldD) | |
5481 | or else not Fully_Conformant_Expressions | |
5482 | (Expression (Parent (Old_Discr)), | |
5483 | Expression (New_Discr)) | |
5484 | ||
5485 | then | |
5486 | Conformance_Error | |
5487 | ("default expression for & does not match!", | |
5488 | New_Discr_Id); | |
5489 | return; | |
5490 | end if; | |
5491 | end if; | |
5492 | end; | |
5493 | ||
5494 | -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X) | |
5495 | ||
0ab80019 | 5496 | if Ada_Version = Ada_83 then |
996ae0b0 RK |
5497 | declare |
5498 | Old_Disc : constant Node_Id := Declaration_Node (Old_Discr); | |
5499 | ||
5500 | begin | |
5501 | -- Grouping (use of comma in param lists) must be the same | |
5502 | -- This is where we catch a misconformance like: | |
5503 | ||
60370fb1 | 5504 | -- A, B : Integer |
996ae0b0 RK |
5505 | -- A : Integer; B : Integer |
5506 | ||
5507 | -- which are represented identically in the tree except | |
5508 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5509 | ||
5510 | if More_Ids (Old_Disc) /= More_Ids (New_Discr) | |
5511 | or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr) | |
5512 | then | |
5513 | Conformance_Error | |
5514 | ("grouping of & does not match!", New_Discr_Id); | |
5515 | return; | |
5516 | end if; | |
5517 | end; | |
5518 | end if; | |
5519 | ||
5520 | Next_Discriminant (Old_Discr); | |
5521 | Next (New_Discr); | |
5522 | end loop; | |
5523 | ||
5524 | if Present (Old_Discr) then | |
5525 | Conformance_Error ("too few discriminants!", Defining_Identifier (N)); | |
5526 | return; | |
5527 | ||
5528 | elsif Present (New_Discr) then | |
5529 | Conformance_Error | |
5530 | ("too many discriminants!", Defining_Identifier (New_Discr)); | |
5531 | return; | |
5532 | end if; | |
5533 | end Check_Discriminant_Conformance; | |
5534 | ||
5535 | ---------------------------- | |
5536 | -- Check_Fully_Conformant -- | |
5537 | ---------------------------- | |
5538 | ||
5539 | procedure Check_Fully_Conformant | |
5540 | (New_Id : Entity_Id; | |
5541 | Old_Id : Entity_Id; | |
5542 | Err_Loc : Node_Id := Empty) | |
5543 | is | |
5544 | Result : Boolean; | |
81db9d77 | 5545 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5546 | begin |
5547 | Check_Conformance | |
5548 | (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc); | |
5549 | end Check_Fully_Conformant; | |
5550 | ||
5551 | --------------------------- | |
5552 | -- Check_Mode_Conformant -- | |
5553 | --------------------------- | |
5554 | ||
5555 | procedure Check_Mode_Conformant | |
5556 | (New_Id : Entity_Id; | |
5557 | Old_Id : Entity_Id; | |
5558 | Err_Loc : Node_Id := Empty; | |
5559 | Get_Inst : Boolean := False) | |
5560 | is | |
5561 | Result : Boolean; | |
81db9d77 | 5562 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5563 | begin |
5564 | Check_Conformance | |
5565 | (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst); | |
5566 | end Check_Mode_Conformant; | |
5567 | ||
fbf5a39b | 5568 | -------------------------------- |
758c442c | 5569 | -- Check_Overriding_Indicator -- |
fbf5a39b AC |
5570 | -------------------------------- |
5571 | ||
758c442c | 5572 | procedure Check_Overriding_Indicator |
ec4867fa | 5573 | (Subp : Entity_Id; |
5d37ba92 ES |
5574 | Overridden_Subp : Entity_Id; |
5575 | Is_Primitive : Boolean) | |
fbf5a39b | 5576 | is |
758c442c GD |
5577 | Decl : Node_Id; |
5578 | Spec : Node_Id; | |
fbf5a39b AC |
5579 | |
5580 | begin | |
ec4867fa | 5581 | -- No overriding indicator for literals |
fbf5a39b | 5582 | |
ec4867fa | 5583 | if Ekind (Subp) = E_Enumeration_Literal then |
758c442c | 5584 | return; |
fbf5a39b | 5585 | |
ec4867fa ES |
5586 | elsif Ekind (Subp) = E_Entry then |
5587 | Decl := Parent (Subp); | |
5588 | ||
53b10ce9 AC |
5589 | -- No point in analyzing a malformed operator |
5590 | ||
5591 | elsif Nkind (Subp) = N_Defining_Operator_Symbol | |
5592 | and then Error_Posted (Subp) | |
5593 | then | |
5594 | return; | |
5595 | ||
758c442c GD |
5596 | else |
5597 | Decl := Unit_Declaration_Node (Subp); | |
5598 | end if; | |
fbf5a39b | 5599 | |
800621e0 RD |
5600 | if Nkind_In (Decl, N_Subprogram_Body, |
5601 | N_Subprogram_Body_Stub, | |
5602 | N_Subprogram_Declaration, | |
5603 | N_Abstract_Subprogram_Declaration, | |
5604 | N_Subprogram_Renaming_Declaration) | |
758c442c GD |
5605 | then |
5606 | Spec := Specification (Decl); | |
ec4867fa ES |
5607 | |
5608 | elsif Nkind (Decl) = N_Entry_Declaration then | |
5609 | Spec := Decl; | |
5610 | ||
758c442c GD |
5611 | else |
5612 | return; | |
5613 | end if; | |
fbf5a39b | 5614 | |
e7d72fb9 AC |
5615 | -- The overriding operation is type conformant with the overridden one, |
5616 | -- but the names of the formals are not required to match. If the names | |
6823270c | 5617 | -- appear permuted in the overriding operation, this is a possible |
e7d72fb9 AC |
5618 | -- source of confusion that is worth diagnosing. Controlling formals |
5619 | -- often carry names that reflect the type, and it is not worthwhile | |
5620 | -- requiring that their names match. | |
5621 | ||
c9e7bd8e | 5622 | if Present (Overridden_Subp) |
e7d72fb9 AC |
5623 | and then Nkind (Subp) /= N_Defining_Operator_Symbol |
5624 | then | |
5625 | declare | |
5626 | Form1 : Entity_Id; | |
5627 | Form2 : Entity_Id; | |
5628 | ||
5629 | begin | |
5630 | Form1 := First_Formal (Subp); | |
5631 | Form2 := First_Formal (Overridden_Subp); | |
5632 | ||
c9e7bd8e AC |
5633 | -- If the overriding operation is a synchronized operation, skip |
5634 | -- the first parameter of the overridden operation, which is | |
6823270c AC |
5635 | -- implicit in the new one. If the operation is declared in the |
5636 | -- body it is not primitive and all formals must match. | |
c9e7bd8e | 5637 | |
6823270c AC |
5638 | if Is_Concurrent_Type (Scope (Subp)) |
5639 | and then Is_Tagged_Type (Scope (Subp)) | |
5640 | and then not Has_Completion (Scope (Subp)) | |
5641 | then | |
c9e7bd8e AC |
5642 | Form2 := Next_Formal (Form2); |
5643 | end if; | |
5644 | ||
e7d72fb9 AC |
5645 | if Present (Form1) then |
5646 | Form1 := Next_Formal (Form1); | |
5647 | Form2 := Next_Formal (Form2); | |
5648 | end if; | |
5649 | ||
5650 | while Present (Form1) loop | |
5651 | if not Is_Controlling_Formal (Form1) | |
5652 | and then Present (Next_Formal (Form2)) | |
5653 | and then Chars (Form1) = Chars (Next_Formal (Form2)) | |
5654 | then | |
5655 | Error_Msg_Node_2 := Alias (Overridden_Subp); | |
5656 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
ed2233dc | 5657 | Error_Msg_NE |
19d846a0 | 5658 | ("& does not match corresponding formal of&#", |
e7d72fb9 AC |
5659 | Form1, Form1); |
5660 | exit; | |
5661 | end if; | |
5662 | ||
5663 | Next_Formal (Form1); | |
5664 | Next_Formal (Form2); | |
5665 | end loop; | |
5666 | end; | |
5667 | end if; | |
5668 | ||
676e8420 AC |
5669 | -- If there is an overridden subprogram, then check that there is no |
5670 | -- "not overriding" indicator, and mark the subprogram as overriding. | |
51bf9bdf AC |
5671 | -- This is not done if the overridden subprogram is marked as hidden, |
5672 | -- which can occur for the case of inherited controlled operations | |
5673 | -- (see Derive_Subprogram), unless the inherited subprogram's parent | |
5674 | -- subprogram is not itself hidden. (Note: This condition could probably | |
5675 | -- be simplified, leaving out the testing for the specific controlled | |
5676 | -- cases, but it seems safer and clearer this way, and echoes similar | |
5677 | -- special-case tests of this kind in other places.) | |
5678 | ||
fd0d899b | 5679 | if Present (Overridden_Subp) |
51bf9bdf AC |
5680 | and then (not Is_Hidden (Overridden_Subp) |
5681 | or else | |
b69cd36a AC |
5682 | (Nam_In (Chars (Overridden_Subp), Name_Initialize, |
5683 | Name_Adjust, | |
5684 | Name_Finalize) | |
f0709ca6 AC |
5685 | and then Present (Alias (Overridden_Subp)) |
5686 | and then not Is_Hidden (Alias (Overridden_Subp)))) | |
fd0d899b | 5687 | then |
ec4867fa ES |
5688 | if Must_Not_Override (Spec) then |
5689 | Error_Msg_Sloc := Sloc (Overridden_Subp); | |
fbf5a39b | 5690 | |
ec4867fa | 5691 | if Ekind (Subp) = E_Entry then |
ed2233dc | 5692 | Error_Msg_NE |
5d37ba92 | 5693 | ("entry & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5694 | else |
ed2233dc | 5695 | Error_Msg_NE |
5d37ba92 | 5696 | ("subprogram & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5697 | end if; |
21d27997 | 5698 | |
bd603506 | 5699 | -- Special-case to fix a GNAT oddity: Limited_Controlled is declared |
24a120ac AC |
5700 | -- as an extension of Root_Controlled, and thus has a useless Adjust |
5701 | -- operation. This operation should not be inherited by other limited | |
5702 | -- controlled types. An explicit Adjust for them is not overriding. | |
5703 | ||
5704 | elsif Must_Override (Spec) | |
5705 | and then Chars (Overridden_Subp) = Name_Adjust | |
5706 | and then Is_Limited_Type (Etype (First_Formal (Subp))) | |
5707 | and then Present (Alias (Overridden_Subp)) | |
bd603506 RD |
5708 | and then |
5709 | Is_Predefined_File_Name | |
5710 | (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp)))) | |
24a120ac AC |
5711 | then |
5712 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5713 | ||
21d27997 | 5714 | elsif Is_Subprogram (Subp) then |
2fe829ae ES |
5715 | if Is_Init_Proc (Subp) then |
5716 | null; | |
5717 | ||
5718 | elsif No (Overridden_Operation (Subp)) then | |
1c1289e7 AC |
5719 | |
5720 | -- For entities generated by Derive_Subprograms the overridden | |
5721 | -- operation is the inherited primitive (which is available | |
5722 | -- through the attribute alias) | |
5723 | ||
5724 | if (Is_Dispatching_Operation (Subp) | |
f9673bb0 | 5725 | or else Is_Dispatching_Operation (Overridden_Subp)) |
1c1289e7 | 5726 | and then not Comes_From_Source (Overridden_Subp) |
f9673bb0 AC |
5727 | and then Find_Dispatching_Type (Overridden_Subp) = |
5728 | Find_Dispatching_Type (Subp) | |
1c1289e7 AC |
5729 | and then Present (Alias (Overridden_Subp)) |
5730 | and then Comes_From_Source (Alias (Overridden_Subp)) | |
5731 | then | |
039538bc AC |
5732 | Set_Overridden_Operation (Subp, Alias (Overridden_Subp)); |
5733 | Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp)); | |
2fe829ae | 5734 | |
1c1289e7 | 5735 | else |
039538bc AC |
5736 | Set_Overridden_Operation (Subp, Overridden_Subp); |
5737 | Inherit_Subprogram_Contract (Subp, Overridden_Subp); | |
1c1289e7 AC |
5738 | end if; |
5739 | end if; | |
ec4867fa | 5740 | end if; |
f937473f | 5741 | |
618fb570 AC |
5742 | -- If primitive flag is set or this is a protected operation, then |
5743 | -- the operation is overriding at the point of its declaration, so | |
5744 | -- warn if necessary. Otherwise it may have been declared before the | |
5745 | -- operation it overrides and no check is required. | |
3c25856a AC |
5746 | |
5747 | if Style_Check | |
618fb570 AC |
5748 | and then not Must_Override (Spec) |
5749 | and then (Is_Primitive | |
5750 | or else Ekind (Scope (Subp)) = E_Protected_Type) | |
3c25856a | 5751 | then |
235f4375 AC |
5752 | Style.Missing_Overriding (Decl, Subp); |
5753 | end if; | |
5754 | ||
53b10ce9 AC |
5755 | -- If Subp is an operator, it may override a predefined operation, if |
5756 | -- it is defined in the same scope as the type to which it applies. | |
676e8420 | 5757 | -- In that case Overridden_Subp is empty because of our implicit |
5d37ba92 ES |
5758 | -- representation for predefined operators. We have to check whether the |
5759 | -- signature of Subp matches that of a predefined operator. Note that | |
5760 | -- first argument provides the name of the operator, and the second | |
5761 | -- argument the signature that may match that of a standard operation. | |
21d27997 RD |
5762 | -- If the indicator is overriding, then the operator must match a |
5763 | -- predefined signature, because we know already that there is no | |
5764 | -- explicit overridden operation. | |
f937473f | 5765 | |
21d27997 | 5766 | elsif Nkind (Subp) = N_Defining_Operator_Symbol then |
806f6d37 | 5767 | if Must_Not_Override (Spec) then |
f937473f | 5768 | |
806f6d37 AC |
5769 | -- If this is not a primitive or a protected subprogram, then |
5770 | -- "not overriding" is illegal. | |
618fb570 | 5771 | |
806f6d37 AC |
5772 | if not Is_Primitive |
5773 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5774 | then | |
3ccedacc AC |
5775 | Error_Msg_N ("overriding indicator only allowed " |
5776 | & "if subprogram is primitive", Subp); | |
618fb570 | 5777 | |
806f6d37 AC |
5778 | elsif Can_Override_Operator (Subp) then |
5779 | Error_Msg_NE | |
5780 | ("subprogram& overrides predefined operator ", Spec, Subp); | |
5781 | end if; | |
f937473f | 5782 | |
806f6d37 AC |
5783 | elsif Must_Override (Spec) then |
5784 | if No (Overridden_Operation (Subp)) | |
5785 | and then not Can_Override_Operator (Subp) | |
5786 | then | |
5787 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5788 | end if; | |
5d37ba92 | 5789 | |
806f6d37 AC |
5790 | elsif not Error_Posted (Subp) |
5791 | and then Style_Check | |
5792 | and then Can_Override_Operator (Subp) | |
5793 | and then | |
5794 | not Is_Predefined_File_Name | |
5795 | (Unit_File_Name (Get_Source_Unit (Subp))) | |
5796 | then | |
5797 | -- If style checks are enabled, indicate that the indicator is | |
5798 | -- missing. However, at the point of declaration, the type of | |
5799 | -- which this is a primitive operation may be private, in which | |
5800 | -- case the indicator would be premature. | |
235f4375 | 5801 | |
806f6d37 AC |
5802 | if Has_Private_Declaration (Etype (Subp)) |
5803 | or else Has_Private_Declaration (Etype (First_Formal (Subp))) | |
53b10ce9 | 5804 | then |
806f6d37 AC |
5805 | null; |
5806 | else | |
5807 | Style.Missing_Overriding (Decl, Subp); | |
5d5832bc | 5808 | end if; |
806f6d37 | 5809 | end if; |
21d27997 RD |
5810 | |
5811 | elsif Must_Override (Spec) then | |
5812 | if Ekind (Subp) = E_Entry then | |
ed2233dc | 5813 | Error_Msg_NE ("entry & is not overriding", Spec, Subp); |
5d37ba92 | 5814 | else |
ed2233dc | 5815 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
758c442c | 5816 | end if; |
5d37ba92 ES |
5817 | |
5818 | -- If the operation is marked "not overriding" and it's not primitive | |
5819 | -- then an error is issued, unless this is an operation of a task or | |
5820 | -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding" | |
5821 | -- has been specified have already been checked above. | |
5822 | ||
5823 | elsif Must_Not_Override (Spec) | |
5824 | and then not Is_Primitive | |
5825 | and then Ekind (Subp) /= E_Entry | |
5826 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5827 | then | |
ed2233dc | 5828 | Error_Msg_N |
5d37ba92 ES |
5829 | ("overriding indicator only allowed if subprogram is primitive", |
5830 | Subp); | |
5d37ba92 | 5831 | return; |
fbf5a39b | 5832 | end if; |
758c442c | 5833 | end Check_Overriding_Indicator; |
fbf5a39b | 5834 | |
996ae0b0 RK |
5835 | ------------------- |
5836 | -- Check_Returns -- | |
5837 | ------------------- | |
5838 | ||
0a36105d JM |
5839 | -- Note: this procedure needs to know far too much about how the expander |
5840 | -- messes with exceptions. The use of the flag Exception_Junk and the | |
5841 | -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers | |
5842 | -- works, but is not very clean. It would be better if the expansion | |
5843 | -- routines would leave Original_Node working nicely, and we could use | |
5844 | -- Original_Node here to ignore all the peculiar expander messing ??? | |
5845 | ||
996ae0b0 RK |
5846 | procedure Check_Returns |
5847 | (HSS : Node_Id; | |
5848 | Mode : Character; | |
c8ef728f ES |
5849 | Err : out Boolean; |
5850 | Proc : Entity_Id := Empty) | |
996ae0b0 RK |
5851 | is |
5852 | Handler : Node_Id; | |
5853 | ||
5854 | procedure Check_Statement_Sequence (L : List_Id); | |
5855 | -- Internal recursive procedure to check a list of statements for proper | |
5856 | -- termination by a return statement (or a transfer of control or a | |
5857 | -- compound statement that is itself internally properly terminated). | |
5858 | ||
5859 | ------------------------------ | |
5860 | -- Check_Statement_Sequence -- | |
5861 | ------------------------------ | |
5862 | ||
5863 | procedure Check_Statement_Sequence (L : List_Id) is | |
5864 | Last_Stm : Node_Id; | |
0a36105d | 5865 | Stm : Node_Id; |
996ae0b0 RK |
5866 | Kind : Node_Kind; |
5867 | ||
7b27e183 AC |
5868 | function Assert_False return Boolean; |
5869 | -- Returns True if Last_Stm is a pragma Assert (False) that has been | |
5870 | -- rewritten as a null statement when assertions are off. The assert | |
5871 | -- is not active, but it is still enough to kill the warning. | |
5872 | ||
5873 | ------------------ | |
5874 | -- Assert_False -- | |
5875 | ------------------ | |
5876 | ||
5877 | function Assert_False return Boolean is | |
5878 | Orig : constant Node_Id := Original_Node (Last_Stm); | |
5879 | ||
5880 | begin | |
5881 | if Nkind (Orig) = N_Pragma | |
5882 | and then Pragma_Name (Orig) = Name_Assert | |
5883 | and then not Error_Posted (Orig) | |
5884 | then | |
5885 | declare | |
5886 | Arg : constant Node_Id := | |
5887 | First (Pragma_Argument_Associations (Orig)); | |
5888 | Exp : constant Node_Id := Expression (Arg); | |
5889 | begin | |
5890 | return Nkind (Exp) = N_Identifier | |
5891 | and then Chars (Exp) = Name_False; | |
5892 | end; | |
5893 | ||
5894 | else | |
5895 | return False; | |
5896 | end if; | |
5897 | end Assert_False; | |
5898 | ||
5899 | -- Local variables | |
5900 | ||
996ae0b0 RK |
5901 | Raise_Exception_Call : Boolean; |
5902 | -- Set True if statement sequence terminated by Raise_Exception call | |
5903 | -- or a Reraise_Occurrence call. | |
5904 | ||
7b27e183 AC |
5905 | -- Start of processing for Check_Statement_Sequence |
5906 | ||
996ae0b0 RK |
5907 | begin |
5908 | Raise_Exception_Call := False; | |
5909 | ||
5910 | -- Get last real statement | |
5911 | ||
5912 | Last_Stm := Last (L); | |
5913 | ||
0a36105d JM |
5914 | -- Deal with digging out exception handler statement sequences that |
5915 | -- have been transformed by the local raise to goto optimization. | |
5916 | -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this | |
5917 | -- optimization has occurred, we are looking at something like: | |
5918 | ||
5919 | -- begin | |
5920 | -- original stmts in block | |
5921 | ||
5922 | -- exception \ | |
5923 | -- when excep1 => | | |
5924 | -- goto L1; | omitted if No_Exception_Propagation | |
5925 | -- when excep2 => | | |
5926 | -- goto L2; / | |
5927 | -- end; | |
5928 | ||
5929 | -- goto L3; -- skip handler when exception not raised | |
5930 | ||
5931 | -- <<L1>> -- target label for local exception | |
5932 | -- begin | |
5933 | -- estmts1 | |
5934 | -- end; | |
5935 | ||
5936 | -- goto L3; | |
5937 | ||
5938 | -- <<L2>> | |
5939 | -- begin | |
5940 | -- estmts2 | |
5941 | -- end; | |
5942 | ||
5943 | -- <<L3>> | |
5944 | ||
5945 | -- and what we have to do is to dig out the estmts1 and estmts2 | |
5946 | -- sequences (which were the original sequences of statements in | |
5947 | -- the exception handlers) and check them. | |
5948 | ||
8fde064e | 5949 | if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then |
0a36105d JM |
5950 | Stm := Last_Stm; |
5951 | loop | |
5952 | Prev (Stm); | |
5953 | exit when No (Stm); | |
5954 | exit when Nkind (Stm) /= N_Block_Statement; | |
5955 | exit when not Exception_Junk (Stm); | |
5956 | Prev (Stm); | |
5957 | exit when No (Stm); | |
5958 | exit when Nkind (Stm) /= N_Label; | |
5959 | exit when not Exception_Junk (Stm); | |
5960 | Check_Statement_Sequence | |
5961 | (Statements (Handled_Statement_Sequence (Next (Stm)))); | |
5962 | ||
5963 | Prev (Stm); | |
5964 | Last_Stm := Stm; | |
5965 | exit when No (Stm); | |
5966 | exit when Nkind (Stm) /= N_Goto_Statement; | |
5967 | exit when not Exception_Junk (Stm); | |
5968 | end loop; | |
5969 | end if; | |
5970 | ||
996ae0b0 RK |
5971 | -- Don't count pragmas |
5972 | ||
5973 | while Nkind (Last_Stm) = N_Pragma | |
5974 | ||
5975 | -- Don't count call to SS_Release (can happen after Raise_Exception) | |
5976 | ||
5977 | or else | |
5978 | (Nkind (Last_Stm) = N_Procedure_Call_Statement | |
5979 | and then | |
5980 | Nkind (Name (Last_Stm)) = N_Identifier | |
5981 | and then | |
5982 | Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release)) | |
5983 | ||
5984 | -- Don't count exception junk | |
5985 | ||
5986 | or else | |
800621e0 RD |
5987 | (Nkind_In (Last_Stm, N_Goto_Statement, |
5988 | N_Label, | |
5989 | N_Object_Declaration) | |
8fde064e | 5990 | and then Exception_Junk (Last_Stm)) |
0a36105d JM |
5991 | or else Nkind (Last_Stm) in N_Push_xxx_Label |
5992 | or else Nkind (Last_Stm) in N_Pop_xxx_Label | |
e3b3266c AC |
5993 | |
5994 | -- Inserted code, such as finalization calls, is irrelevant: we only | |
5995 | -- need to check original source. | |
5996 | ||
5997 | or else Is_Rewrite_Insertion (Last_Stm) | |
996ae0b0 RK |
5998 | loop |
5999 | Prev (Last_Stm); | |
6000 | end loop; | |
6001 | ||
6002 | -- Here we have the "real" last statement | |
6003 | ||
6004 | Kind := Nkind (Last_Stm); | |
6005 | ||
6006 | -- Transfer of control, OK. Note that in the No_Return procedure | |
6007 | -- case, we already diagnosed any explicit return statements, so | |
6008 | -- we can treat them as OK in this context. | |
6009 | ||
6010 | if Is_Transfer (Last_Stm) then | |
6011 | return; | |
6012 | ||
6013 | -- Check cases of explicit non-indirect procedure calls | |
6014 | ||
6015 | elsif Kind = N_Procedure_Call_Statement | |
6016 | and then Is_Entity_Name (Name (Last_Stm)) | |
6017 | then | |
6018 | -- Check call to Raise_Exception procedure which is treated | |
6019 | -- specially, as is a call to Reraise_Occurrence. | |
6020 | ||
6021 | -- We suppress the warning in these cases since it is likely that | |
6022 | -- the programmer really does not expect to deal with the case | |
6023 | -- of Null_Occurrence, and thus would find a warning about a | |
6024 | -- missing return curious, and raising Program_Error does not | |
6025 | -- seem such a bad behavior if this does occur. | |
6026 | ||
c8ef728f ES |
6027 | -- Note that in the Ada 2005 case for Raise_Exception, the actual |
6028 | -- behavior will be to raise Constraint_Error (see AI-329). | |
6029 | ||
996ae0b0 RK |
6030 | if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception) |
6031 | or else | |
6032 | Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence) | |
6033 | then | |
6034 | Raise_Exception_Call := True; | |
6035 | ||
6036 | -- For Raise_Exception call, test first argument, if it is | |
6037 | -- an attribute reference for a 'Identity call, then we know | |
6038 | -- that the call cannot possibly return. | |
6039 | ||
6040 | declare | |
6041 | Arg : constant Node_Id := | |
6042 | Original_Node (First_Actual (Last_Stm)); | |
996ae0b0 RK |
6043 | begin |
6044 | if Nkind (Arg) = N_Attribute_Reference | |
6045 | and then Attribute_Name (Arg) = Name_Identity | |
6046 | then | |
6047 | return; | |
6048 | end if; | |
6049 | end; | |
6050 | end if; | |
6051 | ||
6052 | -- If statement, need to look inside if there is an else and check | |
6053 | -- each constituent statement sequence for proper termination. | |
6054 | ||
6055 | elsif Kind = N_If_Statement | |
6056 | and then Present (Else_Statements (Last_Stm)) | |
6057 | then | |
6058 | Check_Statement_Sequence (Then_Statements (Last_Stm)); | |
6059 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
6060 | ||
6061 | if Present (Elsif_Parts (Last_Stm)) then | |
6062 | declare | |
6063 | Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm)); | |
6064 | ||
6065 | begin | |
6066 | while Present (Elsif_Part) loop | |
6067 | Check_Statement_Sequence (Then_Statements (Elsif_Part)); | |
6068 | Next (Elsif_Part); | |
6069 | end loop; | |
6070 | end; | |
6071 | end if; | |
6072 | ||
6073 | return; | |
6074 | ||
6075 | -- Case statement, check each case for proper termination | |
6076 | ||
6077 | elsif Kind = N_Case_Statement then | |
6078 | declare | |
6079 | Case_Alt : Node_Id; | |
996ae0b0 RK |
6080 | begin |
6081 | Case_Alt := First_Non_Pragma (Alternatives (Last_Stm)); | |
6082 | while Present (Case_Alt) loop | |
6083 | Check_Statement_Sequence (Statements (Case_Alt)); | |
6084 | Next_Non_Pragma (Case_Alt); | |
6085 | end loop; | |
6086 | end; | |
6087 | ||
6088 | return; | |
6089 | ||
6090 | -- Block statement, check its handled sequence of statements | |
6091 | ||
6092 | elsif Kind = N_Block_Statement then | |
6093 | declare | |
6094 | Err1 : Boolean; | |
6095 | ||
6096 | begin | |
6097 | Check_Returns | |
6098 | (Handled_Statement_Sequence (Last_Stm), Mode, Err1); | |
6099 | ||
6100 | if Err1 then | |
6101 | Err := True; | |
6102 | end if; | |
6103 | ||
6104 | return; | |
6105 | end; | |
6106 | ||
6107 | -- Loop statement. If there is an iteration scheme, we can definitely | |
6108 | -- fall out of the loop. Similarly if there is an exit statement, we | |
6109 | -- can fall out. In either case we need a following return. | |
6110 | ||
6111 | elsif Kind = N_Loop_Statement then | |
6112 | if Present (Iteration_Scheme (Last_Stm)) | |
6113 | or else Has_Exit (Entity (Identifier (Last_Stm))) | |
6114 | then | |
6115 | null; | |
6116 | ||
f3d57416 RW |
6117 | -- A loop with no exit statement or iteration scheme is either |
6118 | -- an infinite loop, or it has some other exit (raise/return). | |
996ae0b0 RK |
6119 | -- In either case, no warning is required. |
6120 | ||
6121 | else | |
6122 | return; | |
6123 | end if; | |
6124 | ||
6125 | -- Timed entry call, check entry call and delay alternatives | |
6126 | ||
6127 | -- Note: in expanded code, the timed entry call has been converted | |
6128 | -- to a set of expanded statements on which the check will work | |
6129 | -- correctly in any case. | |
6130 | ||
6131 | elsif Kind = N_Timed_Entry_Call then | |
6132 | declare | |
6133 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6134 | DCA : constant Node_Id := Delay_Alternative (Last_Stm); | |
6135 | ||
6136 | begin | |
6137 | -- If statement sequence of entry call alternative is missing, | |
6138 | -- then we can definitely fall through, and we post the error | |
6139 | -- message on the entry call alternative itself. | |
6140 | ||
6141 | if No (Statements (ECA)) then | |
6142 | Last_Stm := ECA; | |
6143 | ||
6144 | -- If statement sequence of delay alternative is missing, then | |
6145 | -- we can definitely fall through, and we post the error | |
6146 | -- message on the delay alternative itself. | |
6147 | ||
6148 | -- Note: if both ECA and DCA are missing the return, then we | |
6149 | -- post only one message, should be enough to fix the bugs. | |
6150 | -- If not we will get a message next time on the DCA when the | |
a90bd866 | 6151 | -- ECA is fixed. |
996ae0b0 RK |
6152 | |
6153 | elsif No (Statements (DCA)) then | |
6154 | Last_Stm := DCA; | |
6155 | ||
6156 | -- Else check both statement sequences | |
6157 | ||
6158 | else | |
6159 | Check_Statement_Sequence (Statements (ECA)); | |
6160 | Check_Statement_Sequence (Statements (DCA)); | |
6161 | return; | |
6162 | end if; | |
6163 | end; | |
6164 | ||
6165 | -- Conditional entry call, check entry call and else part | |
6166 | ||
6167 | -- Note: in expanded code, the conditional entry call has been | |
6168 | -- converted to a set of expanded statements on which the check | |
6169 | -- will work correctly in any case. | |
6170 | ||
6171 | elsif Kind = N_Conditional_Entry_Call then | |
6172 | declare | |
6173 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6174 | ||
6175 | begin | |
6176 | -- If statement sequence of entry call alternative is missing, | |
6177 | -- then we can definitely fall through, and we post the error | |
6178 | -- message on the entry call alternative itself. | |
6179 | ||
6180 | if No (Statements (ECA)) then | |
6181 | Last_Stm := ECA; | |
6182 | ||
6183 | -- Else check statement sequence and else part | |
6184 | ||
6185 | else | |
6186 | Check_Statement_Sequence (Statements (ECA)); | |
6187 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
6188 | return; | |
6189 | end if; | |
6190 | end; | |
6191 | end if; | |
6192 | ||
6193 | -- If we fall through, issue appropriate message | |
6194 | ||
6195 | if Mode = 'F' then | |
7b27e183 AC |
6196 | |
6197 | -- Kill warning if last statement is a raise exception call, | |
6198 | -- or a pragma Assert (False). Note that with assertions enabled, | |
6199 | -- such a pragma has been converted into a raise exception call | |
6200 | -- already, so the Assert_False is for the assertions off case. | |
6201 | ||
6202 | if not Raise_Exception_Call and then not Assert_False then | |
b465ef6f AC |
6203 | |
6204 | -- In GNATprove mode, it is an error to have a missing return | |
6205 | ||
43417b90 | 6206 | Error_Msg_Warn := SPARK_Mode /= On; |
7b27e183 AC |
6207 | |
6208 | -- Issue error message or warning | |
6209 | ||
4a28b181 AC |
6210 | Error_Msg_N |
6211 | ("RETURN statement missing following this statement<<!", | |
6212 | Last_Stm); | |
6213 | Error_Msg_N | |
6214 | ("\Program_Error ]<<!", Last_Stm); | |
996ae0b0 RK |
6215 | end if; |
6216 | ||
6217 | -- Note: we set Err even though we have not issued a warning | |
6218 | -- because we still have a case of a missing return. This is | |
6219 | -- an extremely marginal case, probably will never be noticed | |
6220 | -- but we might as well get it right. | |
6221 | ||
6222 | Err := True; | |
6223 | ||
c8ef728f ES |
6224 | -- Otherwise we have the case of a procedure marked No_Return |
6225 | ||
996ae0b0 | 6226 | else |
800621e0 | 6227 | if not Raise_Exception_Call then |
4a28b181 AC |
6228 | if GNATprove_Mode then |
6229 | Error_Msg_N | |
6230 | ("implied return after this statement " | |
6231 | & "would have raised Program_Error", Last_Stm); | |
6232 | else | |
6233 | Error_Msg_N | |
6234 | ("implied return after this statement " | |
6235 | & "will raise Program_Error??", Last_Stm); | |
6236 | end if; | |
6237 | ||
43417b90 | 6238 | Error_Msg_Warn := SPARK_Mode /= On; |
800621e0 | 6239 | Error_Msg_NE |
4a28b181 | 6240 | ("\procedure & is marked as No_Return<<!", Last_Stm, Proc); |
800621e0 | 6241 | end if; |
c8ef728f ES |
6242 | |
6243 | declare | |
6244 | RE : constant Node_Id := | |
6245 | Make_Raise_Program_Error (Sloc (Last_Stm), | |
6246 | Reason => PE_Implicit_Return); | |
6247 | begin | |
6248 | Insert_After (Last_Stm, RE); | |
6249 | Analyze (RE); | |
6250 | end; | |
996ae0b0 RK |
6251 | end if; |
6252 | end Check_Statement_Sequence; | |
6253 | ||
6254 | -- Start of processing for Check_Returns | |
6255 | ||
6256 | begin | |
6257 | Err := False; | |
6258 | Check_Statement_Sequence (Statements (HSS)); | |
6259 | ||
6260 | if Present (Exception_Handlers (HSS)) then | |
6261 | Handler := First_Non_Pragma (Exception_Handlers (HSS)); | |
6262 | while Present (Handler) loop | |
6263 | Check_Statement_Sequence (Statements (Handler)); | |
6264 | Next_Non_Pragma (Handler); | |
6265 | end loop; | |
6266 | end if; | |
6267 | end Check_Returns; | |
6268 | ||
6269 | ---------------------------- | |
6270 | -- Check_Subprogram_Order -- | |
6271 | ---------------------------- | |
6272 | ||
6273 | procedure Check_Subprogram_Order (N : Node_Id) is | |
6274 | ||
6275 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean; | |
dc36a7e3 RD |
6276 | -- This is used to check if S1 > S2 in the sense required by this test, |
6277 | -- for example nameab < namec, but name2 < name10. | |
996ae0b0 | 6278 | |
82c80734 RD |
6279 | ----------------------------- |
6280 | -- Subprogram_Name_Greater -- | |
6281 | ----------------------------- | |
6282 | ||
996ae0b0 RK |
6283 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean is |
6284 | L1, L2 : Positive; | |
6285 | N1, N2 : Natural; | |
6286 | ||
6287 | begin | |
67336960 AC |
6288 | -- Deal with special case where names are identical except for a |
6289 | -- numerical suffix. These are handled specially, taking the numeric | |
6290 | -- ordering from the suffix into account. | |
996ae0b0 RK |
6291 | |
6292 | L1 := S1'Last; | |
6293 | while S1 (L1) in '0' .. '9' loop | |
6294 | L1 := L1 - 1; | |
6295 | end loop; | |
6296 | ||
6297 | L2 := S2'Last; | |
6298 | while S2 (L2) in '0' .. '9' loop | |
6299 | L2 := L2 - 1; | |
6300 | end loop; | |
6301 | ||
67336960 | 6302 | -- If non-numeric parts non-equal, do straight compare |
996ae0b0 | 6303 | |
67336960 AC |
6304 | if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then |
6305 | return S1 > S2; | |
996ae0b0 RK |
6306 | |
6307 | -- If non-numeric parts equal, compare suffixed numeric parts. Note | |
6308 | -- that a missing suffix is treated as numeric zero in this test. | |
6309 | ||
6310 | else | |
6311 | N1 := 0; | |
6312 | while L1 < S1'Last loop | |
6313 | L1 := L1 + 1; | |
6314 | N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0'); | |
6315 | end loop; | |
6316 | ||
6317 | N2 := 0; | |
6318 | while L2 < S2'Last loop | |
6319 | L2 := L2 + 1; | |
6320 | N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0'); | |
6321 | end loop; | |
6322 | ||
6323 | return N1 > N2; | |
6324 | end if; | |
6325 | end Subprogram_Name_Greater; | |
6326 | ||
6327 | -- Start of processing for Check_Subprogram_Order | |
6328 | ||
6329 | begin | |
6330 | -- Check body in alpha order if this is option | |
6331 | ||
fbf5a39b | 6332 | if Style_Check |
bc202b70 | 6333 | and then Style_Check_Order_Subprograms |
996ae0b0 RK |
6334 | and then Nkind (N) = N_Subprogram_Body |
6335 | and then Comes_From_Source (N) | |
6336 | and then In_Extended_Main_Source_Unit (N) | |
6337 | then | |
6338 | declare | |
6339 | LSN : String_Ptr | |
6340 | renames Scope_Stack.Table | |
6341 | (Scope_Stack.Last).Last_Subprogram_Name; | |
6342 | ||
6343 | Body_Id : constant Entity_Id := | |
6344 | Defining_Entity (Specification (N)); | |
6345 | ||
6346 | begin | |
6347 | Get_Decoded_Name_String (Chars (Body_Id)); | |
6348 | ||
6349 | if LSN /= null then | |
6350 | if Subprogram_Name_Greater | |
6351 | (LSN.all, Name_Buffer (1 .. Name_Len)) | |
6352 | then | |
6353 | Style.Subprogram_Not_In_Alpha_Order (Body_Id); | |
6354 | end if; | |
6355 | ||
6356 | Free (LSN); | |
6357 | end if; | |
6358 | ||
6359 | LSN := new String'(Name_Buffer (1 .. Name_Len)); | |
6360 | end; | |
6361 | end if; | |
6362 | end Check_Subprogram_Order; | |
6363 | ||
6364 | ------------------------------ | |
6365 | -- Check_Subtype_Conformant -- | |
6366 | ------------------------------ | |
6367 | ||
6368 | procedure Check_Subtype_Conformant | |
ce2b6ba5 JM |
6369 | (New_Id : Entity_Id; |
6370 | Old_Id : Entity_Id; | |
6371 | Err_Loc : Node_Id := Empty; | |
f307415a AC |
6372 | Skip_Controlling_Formals : Boolean := False; |
6373 | Get_Inst : Boolean := False) | |
996ae0b0 RK |
6374 | is |
6375 | Result : Boolean; | |
81db9d77 | 6376 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6377 | begin |
6378 | Check_Conformance | |
ce2b6ba5 | 6379 | (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc, |
f307415a AC |
6380 | Skip_Controlling_Formals => Skip_Controlling_Formals, |
6381 | Get_Inst => Get_Inst); | |
996ae0b0 RK |
6382 | end Check_Subtype_Conformant; |
6383 | ||
6384 | --------------------------- | |
6385 | -- Check_Type_Conformant -- | |
6386 | --------------------------- | |
6387 | ||
6388 | procedure Check_Type_Conformant | |
6389 | (New_Id : Entity_Id; | |
6390 | Old_Id : Entity_Id; | |
6391 | Err_Loc : Node_Id := Empty) | |
6392 | is | |
6393 | Result : Boolean; | |
81db9d77 | 6394 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6395 | begin |
6396 | Check_Conformance | |
6397 | (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc); | |
6398 | end Check_Type_Conformant; | |
6399 | ||
806f6d37 AC |
6400 | --------------------------- |
6401 | -- Can_Override_Operator -- | |
6402 | --------------------------- | |
6403 | ||
6404 | function Can_Override_Operator (Subp : Entity_Id) return Boolean is | |
6405 | Typ : Entity_Id; | |
f146302c | 6406 | |
806f6d37 AC |
6407 | begin |
6408 | if Nkind (Subp) /= N_Defining_Operator_Symbol then | |
6409 | return False; | |
6410 | ||
6411 | else | |
6412 | Typ := Base_Type (Etype (First_Formal (Subp))); | |
6413 | ||
f146302c AC |
6414 | -- Check explicitly that the operation is a primitive of the type |
6415 | ||
806f6d37 | 6416 | return Operator_Matches_Spec (Subp, Subp) |
f146302c | 6417 | and then not Is_Generic_Type (Typ) |
806f6d37 AC |
6418 | and then Scope (Subp) = Scope (Typ) |
6419 | and then not Is_Class_Wide_Type (Typ); | |
6420 | end if; | |
6421 | end Can_Override_Operator; | |
6422 | ||
996ae0b0 RK |
6423 | ---------------------- |
6424 | -- Conforming_Types -- | |
6425 | ---------------------- | |
6426 | ||
6427 | function Conforming_Types | |
6428 | (T1 : Entity_Id; | |
6429 | T2 : Entity_Id; | |
6430 | Ctype : Conformance_Type; | |
d05ef0ab | 6431 | Get_Inst : Boolean := False) return Boolean |
996ae0b0 RK |
6432 | is |
6433 | Type_1 : Entity_Id := T1; | |
6434 | Type_2 : Entity_Id := T2; | |
af4b9434 | 6435 | Are_Anonymous_Access_To_Subprogram_Types : Boolean := False; |
996ae0b0 RK |
6436 | |
6437 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean; | |
0a36105d JM |
6438 | -- If neither T1 nor T2 are generic actual types, or if they are in |
6439 | -- different scopes (e.g. parent and child instances), then verify that | |
6440 | -- the base types are equal. Otherwise T1 and T2 must be on the same | |
6441 | -- subtype chain. The whole purpose of this procedure is to prevent | |
6442 | -- spurious ambiguities in an instantiation that may arise if two | |
6443 | -- distinct generic types are instantiated with the same actual. | |
6444 | ||
5d37ba92 ES |
6445 | function Find_Designated_Type (T : Entity_Id) return Entity_Id; |
6446 | -- An access parameter can designate an incomplete type. If the | |
6447 | -- incomplete type is the limited view of a type from a limited_ | |
6448 | -- with_clause, check whether the non-limited view is available. If | |
6449 | -- it is a (non-limited) incomplete type, get the full view. | |
6450 | ||
0a36105d JM |
6451 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean; |
6452 | -- Returns True if and only if either T1 denotes a limited view of T2 | |
6453 | -- or T2 denotes a limited view of T1. This can arise when the limited | |
6454 | -- with view of a type is used in a subprogram declaration and the | |
6455 | -- subprogram body is in the scope of a regular with clause for the | |
6456 | -- same unit. In such a case, the two type entities can be considered | |
6457 | -- identical for purposes of conformance checking. | |
996ae0b0 RK |
6458 | |
6459 | ---------------------- | |
6460 | -- Base_Types_Match -- | |
6461 | ---------------------- | |
6462 | ||
6463 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is | |
8fde064e AC |
6464 | BT1 : constant Entity_Id := Base_Type (T1); |
6465 | BT2 : constant Entity_Id := Base_Type (T2); | |
6466 | ||
996ae0b0 RK |
6467 | begin |
6468 | if T1 = T2 then | |
6469 | return True; | |
6470 | ||
8fde064e | 6471 | elsif BT1 = BT2 then |
996ae0b0 | 6472 | |
0a36105d | 6473 | -- The following is too permissive. A more precise test should |
996ae0b0 RK |
6474 | -- check that the generic actual is an ancestor subtype of the |
6475 | -- other ???. | |
586ecbf3 | 6476 | |
70f4ad20 AC |
6477 | -- See code in Find_Corresponding_Spec that applies an additional |
6478 | -- filter to handle accidental amiguities in instances. | |
996ae0b0 RK |
6479 | |
6480 | return not Is_Generic_Actual_Type (T1) | |
07fc65c4 GB |
6481 | or else not Is_Generic_Actual_Type (T2) |
6482 | or else Scope (T1) /= Scope (T2); | |
996ae0b0 | 6483 | |
8fde064e | 6484 | -- If T2 is a generic actual type it is declared as the subtype of |
2995860f AC |
6485 | -- the actual. If that actual is itself a subtype we need to use its |
6486 | -- own base type to check for compatibility. | |
8fde064e AC |
6487 | |
6488 | elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then | |
6489 | return True; | |
6490 | ||
6491 | elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then | |
6492 | return True; | |
6493 | ||
0a36105d JM |
6494 | else |
6495 | return False; | |
6496 | end if; | |
6497 | end Base_Types_Match; | |
aa720a54 | 6498 | |
5d37ba92 ES |
6499 | -------------------------- |
6500 | -- Find_Designated_Type -- | |
6501 | -------------------------- | |
6502 | ||
6503 | function Find_Designated_Type (T : Entity_Id) return Entity_Id is | |
6504 | Desig : Entity_Id; | |
6505 | ||
6506 | begin | |
6507 | Desig := Directly_Designated_Type (T); | |
6508 | ||
6509 | if Ekind (Desig) = E_Incomplete_Type then | |
6510 | ||
6511 | -- If regular incomplete type, get full view if available | |
6512 | ||
6513 | if Present (Full_View (Desig)) then | |
6514 | Desig := Full_View (Desig); | |
6515 | ||
6516 | -- If limited view of a type, get non-limited view if available, | |
6517 | -- and check again for a regular incomplete type. | |
6518 | ||
6519 | elsif Present (Non_Limited_View (Desig)) then | |
6520 | Desig := Get_Full_View (Non_Limited_View (Desig)); | |
6521 | end if; | |
6522 | end if; | |
6523 | ||
6524 | return Desig; | |
6525 | end Find_Designated_Type; | |
6526 | ||
0a36105d JM |
6527 | ------------------------------- |
6528 | -- Matches_Limited_With_View -- | |
6529 | ------------------------------- | |
6530 | ||
6531 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is | |
6532 | begin | |
6533 | -- In some cases a type imported through a limited_with clause, and | |
6534 | -- its nonlimited view are both visible, for example in an anonymous | |
6535 | -- access-to-class-wide type in a formal. Both entities designate the | |
6536 | -- same type. | |
6537 | ||
7b56a91b | 6538 | if From_Limited_With (T1) and then T2 = Available_View (T1) then |
aa720a54 AC |
6539 | return True; |
6540 | ||
7b56a91b | 6541 | elsif From_Limited_With (T2) and then T1 = Available_View (T2) then |
41251c60 | 6542 | return True; |
3e24afaa | 6543 | |
7b56a91b AC |
6544 | elsif From_Limited_With (T1) |
6545 | and then From_Limited_With (T2) | |
3e24afaa AC |
6546 | and then Available_View (T1) = Available_View (T2) |
6547 | then | |
6548 | return True; | |
41251c60 | 6549 | |
996ae0b0 RK |
6550 | else |
6551 | return False; | |
6552 | end if; | |
0a36105d | 6553 | end Matches_Limited_With_View; |
996ae0b0 | 6554 | |
ec4867fa | 6555 | -- Start of processing for Conforming_Types |
758c442c | 6556 | |
996ae0b0 | 6557 | begin |
8fde064e AC |
6558 | -- The context is an instance association for a formal access-to- |
6559 | -- subprogram type; the formal parameter types require mapping because | |
6560 | -- they may denote other formal parameters of the generic unit. | |
996ae0b0 RK |
6561 | |
6562 | if Get_Inst then | |
6563 | Type_1 := Get_Instance_Of (T1); | |
6564 | Type_2 := Get_Instance_Of (T2); | |
6565 | end if; | |
6566 | ||
0a36105d JM |
6567 | -- If one of the types is a view of the other introduced by a limited |
6568 | -- with clause, treat these as conforming for all purposes. | |
996ae0b0 | 6569 | |
0a36105d JM |
6570 | if Matches_Limited_With_View (T1, T2) then |
6571 | return True; | |
6572 | ||
6573 | elsif Base_Types_Match (Type_1, Type_2) then | |
996ae0b0 RK |
6574 | return Ctype <= Mode_Conformant |
6575 | or else Subtypes_Statically_Match (Type_1, Type_2); | |
6576 | ||
6577 | elsif Is_Incomplete_Or_Private_Type (Type_1) | |
6578 | and then Present (Full_View (Type_1)) | |
6579 | and then Base_Types_Match (Full_View (Type_1), Type_2) | |
6580 | then | |
6581 | return Ctype <= Mode_Conformant | |
6582 | or else Subtypes_Statically_Match (Full_View (Type_1), Type_2); | |
6583 | ||
6584 | elsif Ekind (Type_2) = E_Incomplete_Type | |
6585 | and then Present (Full_View (Type_2)) | |
6586 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6587 | then | |
6588 | return Ctype <= Mode_Conformant | |
6589 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
fbf5a39b AC |
6590 | |
6591 | elsif Is_Private_Type (Type_2) | |
6592 | and then In_Instance | |
6593 | and then Present (Full_View (Type_2)) | |
6594 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6595 | then | |
6596 | return Ctype <= Mode_Conformant | |
6597 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
996ae0b0 RK |
6598 | end if; |
6599 | ||
0a36105d | 6600 | -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be |
466c2127 AC |
6601 | -- treated recursively because they carry a signature. As far as |
6602 | -- conformance is concerned, convention plays no role, and either | |
6603 | -- or both could be access to protected subprograms. | |
af4b9434 AC |
6604 | |
6605 | Are_Anonymous_Access_To_Subprogram_Types := | |
466c2127 AC |
6606 | Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type, |
6607 | E_Anonymous_Access_Protected_Subprogram_Type) | |
f937473f | 6608 | and then |
466c2127 AC |
6609 | Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type, |
6610 | E_Anonymous_Access_Protected_Subprogram_Type); | |
af4b9434 | 6611 | |
996ae0b0 | 6612 | -- Test anonymous access type case. For this case, static subtype |
5d37ba92 ES |
6613 | -- matching is required for mode conformance (RM 6.3.1(15)). We check |
6614 | -- the base types because we may have built internal subtype entities | |
6615 | -- to handle null-excluding types (see Process_Formals). | |
996ae0b0 | 6616 | |
5d37ba92 ES |
6617 | if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type |
6618 | and then | |
6619 | Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type) | |
8fde064e AC |
6620 | |
6621 | -- Ada 2005 (AI-254) | |
6622 | ||
6623 | or else Are_Anonymous_Access_To_Subprogram_Types | |
996ae0b0 RK |
6624 | then |
6625 | declare | |
6626 | Desig_1 : Entity_Id; | |
6627 | Desig_2 : Entity_Id; | |
6628 | ||
6629 | begin | |
885c4871 | 6630 | -- In Ada 2005, access constant indicators must match for |
5d37ba92 | 6631 | -- subtype conformance. |
9dcb52e1 | 6632 | |
0791fbe9 | 6633 | if Ada_Version >= Ada_2005 |
5d37ba92 ES |
6634 | and then Ctype >= Subtype_Conformant |
6635 | and then | |
6636 | Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2) | |
6637 | then | |
6638 | return False; | |
996ae0b0 RK |
6639 | end if; |
6640 | ||
5d37ba92 | 6641 | Desig_1 := Find_Designated_Type (Type_1); |
5d37ba92 | 6642 | Desig_2 := Find_Designated_Type (Type_2); |
996ae0b0 | 6643 | |
5d37ba92 | 6644 | -- If the context is an instance association for a formal |
82c80734 RD |
6645 | -- access-to-subprogram type; formal access parameter designated |
6646 | -- types require mapping because they may denote other formal | |
6647 | -- parameters of the generic unit. | |
996ae0b0 RK |
6648 | |
6649 | if Get_Inst then | |
6650 | Desig_1 := Get_Instance_Of (Desig_1); | |
6651 | Desig_2 := Get_Instance_Of (Desig_2); | |
6652 | end if; | |
6653 | ||
82c80734 RD |
6654 | -- It is possible for a Class_Wide_Type to be introduced for an |
6655 | -- incomplete type, in which case there is a separate class_ wide | |
6656 | -- type for the full view. The types conform if their Etypes | |
6657 | -- conform, i.e. one may be the full view of the other. This can | |
6658 | -- only happen in the context of an access parameter, other uses | |
6659 | -- of an incomplete Class_Wide_Type are illegal. | |
996ae0b0 | 6660 | |
fbf5a39b | 6661 | if Is_Class_Wide_Type (Desig_1) |
4adf3c50 AC |
6662 | and then |
6663 | Is_Class_Wide_Type (Desig_2) | |
996ae0b0 RK |
6664 | then |
6665 | return | |
fbf5a39b AC |
6666 | Conforming_Types |
6667 | (Etype (Base_Type (Desig_1)), | |
6668 | Etype (Base_Type (Desig_2)), Ctype); | |
af4b9434 AC |
6669 | |
6670 | elsif Are_Anonymous_Access_To_Subprogram_Types then | |
0791fbe9 | 6671 | if Ada_Version < Ada_2005 then |
758c442c GD |
6672 | return Ctype = Type_Conformant |
6673 | or else | |
af4b9434 AC |
6674 | Subtypes_Statically_Match (Desig_1, Desig_2); |
6675 | ||
758c442c GD |
6676 | -- We must check the conformance of the signatures themselves |
6677 | ||
6678 | else | |
6679 | declare | |
6680 | Conformant : Boolean; | |
6681 | begin | |
6682 | Check_Conformance | |
6683 | (Desig_1, Desig_2, Ctype, False, Conformant); | |
6684 | return Conformant; | |
6685 | end; | |
6686 | end if; | |
6687 | ||
996ae0b0 RK |
6688 | else |
6689 | return Base_Type (Desig_1) = Base_Type (Desig_2) | |
6690 | and then (Ctype = Type_Conformant | |
8fde064e AC |
6691 | or else |
6692 | Subtypes_Statically_Match (Desig_1, Desig_2)); | |
996ae0b0 RK |
6693 | end if; |
6694 | end; | |
6695 | ||
6696 | -- Otherwise definitely no match | |
6697 | ||
6698 | else | |
c8ef728f ES |
6699 | if ((Ekind (Type_1) = E_Anonymous_Access_Type |
6700 | and then Is_Access_Type (Type_2)) | |
6701 | or else (Ekind (Type_2) = E_Anonymous_Access_Type | |
8fde064e | 6702 | and then Is_Access_Type (Type_1))) |
c8ef728f ES |
6703 | and then |
6704 | Conforming_Types | |
6705 | (Designated_Type (Type_1), Designated_Type (Type_2), Ctype) | |
6706 | then | |
6707 | May_Hide_Profile := True; | |
6708 | end if; | |
6709 | ||
996ae0b0 RK |
6710 | return False; |
6711 | end if; | |
996ae0b0 RK |
6712 | end Conforming_Types; |
6713 | ||
6714 | -------------------------- | |
6715 | -- Create_Extra_Formals -- | |
6716 | -------------------------- | |
6717 | ||
6718 | procedure Create_Extra_Formals (E : Entity_Id) is | |
6719 | Formal : Entity_Id; | |
ec4867fa | 6720 | First_Extra : Entity_Id := Empty; |
996ae0b0 RK |
6721 | Last_Extra : Entity_Id; |
6722 | Formal_Type : Entity_Id; | |
6723 | P_Formal : Entity_Id := Empty; | |
6724 | ||
ec4867fa ES |
6725 | function Add_Extra_Formal |
6726 | (Assoc_Entity : Entity_Id; | |
6727 | Typ : Entity_Id; | |
6728 | Scope : Entity_Id; | |
6729 | Suffix : String) return Entity_Id; | |
6730 | -- Add an extra formal to the current list of formals and extra formals. | |
6731 | -- The extra formal is added to the end of the list of extra formals, | |
6732 | -- and also returned as the result. These formals are always of mode IN. | |
6733 | -- The new formal has the type Typ, is declared in Scope, and its name | |
6734 | -- is given by a concatenation of the name of Assoc_Entity and Suffix. | |
cd5a9750 AC |
6735 | -- The following suffixes are currently used. They should not be changed |
6736 | -- without coordinating with CodePeer, which makes use of these to | |
6737 | -- provide better messages. | |
6738 | ||
d92eccc3 AC |
6739 | -- O denotes the Constrained bit. |
6740 | -- L denotes the accessibility level. | |
cd5a9750 AC |
6741 | -- BIP_xxx denotes an extra formal for a build-in-place function. See |
6742 | -- the full list in exp_ch6.BIP_Formal_Kind. | |
996ae0b0 | 6743 | |
fbf5a39b AC |
6744 | ---------------------- |
6745 | -- Add_Extra_Formal -- | |
6746 | ---------------------- | |
6747 | ||
ec4867fa ES |
6748 | function Add_Extra_Formal |
6749 | (Assoc_Entity : Entity_Id; | |
6750 | Typ : Entity_Id; | |
6751 | Scope : Entity_Id; | |
6752 | Suffix : String) return Entity_Id | |
6753 | is | |
996ae0b0 | 6754 | EF : constant Entity_Id := |
ec4867fa ES |
6755 | Make_Defining_Identifier (Sloc (Assoc_Entity), |
6756 | Chars => New_External_Name (Chars (Assoc_Entity), | |
f937473f | 6757 | Suffix => Suffix)); |
996ae0b0 RK |
6758 | |
6759 | begin | |
82c80734 RD |
6760 | -- A little optimization. Never generate an extra formal for the |
6761 | -- _init operand of an initialization procedure, since it could | |
6762 | -- never be used. | |
996ae0b0 RK |
6763 | |
6764 | if Chars (Formal) = Name_uInit then | |
6765 | return Empty; | |
6766 | end if; | |
6767 | ||
6768 | Set_Ekind (EF, E_In_Parameter); | |
6769 | Set_Actual_Subtype (EF, Typ); | |
6770 | Set_Etype (EF, Typ); | |
ec4867fa | 6771 | Set_Scope (EF, Scope); |
996ae0b0 RK |
6772 | Set_Mechanism (EF, Default_Mechanism); |
6773 | Set_Formal_Validity (EF); | |
6774 | ||
ec4867fa ES |
6775 | if No (First_Extra) then |
6776 | First_Extra := EF; | |
6777 | Set_Extra_Formals (Scope, First_Extra); | |
6778 | end if; | |
6779 | ||
6780 | if Present (Last_Extra) then | |
6781 | Set_Extra_Formal (Last_Extra, EF); | |
6782 | end if; | |
6783 | ||
996ae0b0 | 6784 | Last_Extra := EF; |
ec4867fa | 6785 | |
996ae0b0 RK |
6786 | return EF; |
6787 | end Add_Extra_Formal; | |
6788 | ||
6789 | -- Start of processing for Create_Extra_Formals | |
6790 | ||
6791 | begin | |
8fde064e AC |
6792 | -- We never generate extra formals if expansion is not active because we |
6793 | -- don't need them unless we are generating code. | |
f937473f RD |
6794 | |
6795 | if not Expander_Active then | |
6796 | return; | |
6797 | end if; | |
6798 | ||
e2441021 AC |
6799 | -- No need to generate extra formals in interface thunks whose target |
6800 | -- primitive has no extra formals. | |
6801 | ||
6802 | if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then | |
6803 | return; | |
6804 | end if; | |
6805 | ||
82c80734 | 6806 | -- If this is a derived subprogram then the subtypes of the parent |
16b05213 | 6807 | -- subprogram's formal parameters will be used to determine the need |
82c80734 | 6808 | -- for extra formals. |
996ae0b0 RK |
6809 | |
6810 | if Is_Overloadable (E) and then Present (Alias (E)) then | |
6811 | P_Formal := First_Formal (Alias (E)); | |
6812 | end if; | |
6813 | ||
6814 | Last_Extra := Empty; | |
6815 | Formal := First_Formal (E); | |
6816 | while Present (Formal) loop | |
6817 | Last_Extra := Formal; | |
6818 | Next_Formal (Formal); | |
6819 | end loop; | |
6820 | ||
f937473f | 6821 | -- If Extra_formals were already created, don't do it again. This |
82c80734 RD |
6822 | -- situation may arise for subprogram types created as part of |
6823 | -- dispatching calls (see Expand_Dispatching_Call) | |
996ae0b0 | 6824 | |
8fde064e | 6825 | if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then |
996ae0b0 RK |
6826 | return; |
6827 | end if; | |
6828 | ||
19590d70 GD |
6829 | -- If the subprogram is a predefined dispatching subprogram then don't |
6830 | -- generate any extra constrained or accessibility level formals. In | |
6831 | -- general we suppress these for internal subprograms (by not calling | |
6832 | -- Freeze_Subprogram and Create_Extra_Formals at all), but internally | |
6833 | -- generated stream attributes do get passed through because extra | |
6834 | -- build-in-place formals are needed in some cases (limited 'Input). | |
6835 | ||
bac7206d | 6836 | if Is_Predefined_Internal_Operation (E) then |
63585f75 | 6837 | goto Test_For_Func_Result_Extras; |
19590d70 GD |
6838 | end if; |
6839 | ||
996ae0b0 | 6840 | Formal := First_Formal (E); |
996ae0b0 RK |
6841 | while Present (Formal) loop |
6842 | ||
6843 | -- Create extra formal for supporting the attribute 'Constrained. | |
6844 | -- The case of a private type view without discriminants also | |
6845 | -- requires the extra formal if the underlying type has defaulted | |
6846 | -- discriminants. | |
6847 | ||
6848 | if Ekind (Formal) /= E_In_Parameter then | |
6849 | if Present (P_Formal) then | |
6850 | Formal_Type := Etype (P_Formal); | |
6851 | else | |
6852 | Formal_Type := Etype (Formal); | |
6853 | end if; | |
6854 | ||
5d09245e AC |
6855 | -- Do not produce extra formals for Unchecked_Union parameters. |
6856 | -- Jump directly to the end of the loop. | |
6857 | ||
6858 | if Is_Unchecked_Union (Base_Type (Formal_Type)) then | |
6859 | goto Skip_Extra_Formal_Generation; | |
6860 | end if; | |
6861 | ||
996ae0b0 RK |
6862 | if not Has_Discriminants (Formal_Type) |
6863 | and then Ekind (Formal_Type) in Private_Kind | |
6864 | and then Present (Underlying_Type (Formal_Type)) | |
6865 | then | |
6866 | Formal_Type := Underlying_Type (Formal_Type); | |
6867 | end if; | |
6868 | ||
5e5db3b4 GD |
6869 | -- Suppress the extra formal if formal's subtype is constrained or |
6870 | -- indefinite, or we're compiling for Ada 2012 and the underlying | |
6871 | -- type is tagged and limited. In Ada 2012, a limited tagged type | |
6872 | -- can have defaulted discriminants, but 'Constrained is required | |
6873 | -- to return True, so the formal is never needed (see AI05-0214). | |
6874 | -- Note that this ensures consistency of calling sequences for | |
6875 | -- dispatching operations when some types in a class have defaults | |
6876 | -- on discriminants and others do not (and requiring the extra | |
6877 | -- formal would introduce distributed overhead). | |
6878 | ||
b5bdffcc AC |
6879 | -- If the type does not have a completion yet, treat as prior to |
6880 | -- Ada 2012 for consistency. | |
6881 | ||
996ae0b0 | 6882 | if Has_Discriminants (Formal_Type) |
f937473f RD |
6883 | and then not Is_Constrained (Formal_Type) |
6884 | and then not Is_Indefinite_Subtype (Formal_Type) | |
5e5db3b4 | 6885 | and then (Ada_Version < Ada_2012 |
b5bdffcc AC |
6886 | or else No (Underlying_Type (Formal_Type)) |
6887 | or else not | |
6888 | (Is_Limited_Type (Formal_Type) | |
6889 | and then | |
6890 | (Is_Tagged_Type | |
6891 | (Underlying_Type (Formal_Type))))) | |
996ae0b0 RK |
6892 | then |
6893 | Set_Extra_Constrained | |
d92eccc3 | 6894 | (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O")); |
996ae0b0 RK |
6895 | end if; |
6896 | end if; | |
6897 | ||
0a36105d JM |
6898 | -- Create extra formal for supporting accessibility checking. This |
6899 | -- is done for both anonymous access formals and formals of named | |
6900 | -- access types that are marked as controlling formals. The latter | |
6901 | -- case can occur when Expand_Dispatching_Call creates a subprogram | |
6902 | -- type and substitutes the types of access-to-class-wide actuals | |
6903 | -- for the anonymous access-to-specific-type of controlling formals. | |
5d37ba92 ES |
6904 | -- Base_Type is applied because in cases where there is a null |
6905 | -- exclusion the formal may have an access subtype. | |
996ae0b0 RK |
6906 | |
6907 | -- This is suppressed if we specifically suppress accessibility | |
f937473f | 6908 | -- checks at the package level for either the subprogram, or the |
fbf5a39b AC |
6909 | -- package in which it resides. However, we do not suppress it |
6910 | -- simply if the scope has accessibility checks suppressed, since | |
6911 | -- this could cause trouble when clients are compiled with a | |
6912 | -- different suppression setting. The explicit checks at the | |
6913 | -- package level are safe from this point of view. | |
996ae0b0 | 6914 | |
5d37ba92 | 6915 | if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type |
0a36105d | 6916 | or else (Is_Controlling_Formal (Formal) |
5d37ba92 | 6917 | and then Is_Access_Type (Base_Type (Etype (Formal))))) |
996ae0b0 | 6918 | and then not |
fbf5a39b | 6919 | (Explicit_Suppress (E, Accessibility_Check) |
996ae0b0 | 6920 | or else |
fbf5a39b | 6921 | Explicit_Suppress (Scope (E), Accessibility_Check)) |
996ae0b0 | 6922 | and then |
c8ef728f | 6923 | (No (P_Formal) |
996ae0b0 RK |
6924 | or else Present (Extra_Accessibility (P_Formal))) |
6925 | then | |
811c6a85 | 6926 | Set_Extra_Accessibility |
d92eccc3 | 6927 | (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L")); |
996ae0b0 RK |
6928 | end if; |
6929 | ||
5d09245e AC |
6930 | -- This label is required when skipping extra formal generation for |
6931 | -- Unchecked_Union parameters. | |
6932 | ||
6933 | <<Skip_Extra_Formal_Generation>> | |
6934 | ||
f937473f RD |
6935 | if Present (P_Formal) then |
6936 | Next_Formal (P_Formal); | |
6937 | end if; | |
6938 | ||
996ae0b0 RK |
6939 | Next_Formal (Formal); |
6940 | end loop; | |
ec4867fa | 6941 | |
63585f75 SB |
6942 | <<Test_For_Func_Result_Extras>> |
6943 | ||
6944 | -- Ada 2012 (AI05-234): "the accessibility level of the result of a | |
6945 | -- function call is ... determined by the point of call ...". | |
6946 | ||
6947 | if Needs_Result_Accessibility_Level (E) then | |
6948 | Set_Extra_Accessibility_Of_Result | |
6949 | (E, Add_Extra_Formal (E, Standard_Natural, E, "L")); | |
6950 | end if; | |
19590d70 | 6951 | |
ec4867fa | 6952 | -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add |
f937473f RD |
6953 | -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind. |
6954 | ||
0791fbe9 | 6955 | if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then |
ec4867fa | 6956 | declare |
f937473f | 6957 | Result_Subt : constant Entity_Id := Etype (E); |
1a36a0cd | 6958 | Full_Subt : constant Entity_Id := Available_View (Result_Subt); |
2fcc44fa | 6959 | Formal_Typ : Entity_Id; |
f937473f | 6960 | |
2fcc44fa | 6961 | Discard : Entity_Id; |
f937473f | 6962 | pragma Warnings (Off, Discard); |
ec4867fa ES |
6963 | |
6964 | begin | |
f937473f | 6965 | -- In the case of functions with unconstrained result subtypes, |
9a1bc6d5 AC |
6966 | -- add a 4-state formal indicating whether the return object is |
6967 | -- allocated by the caller (1), or should be allocated by the | |
6968 | -- callee on the secondary stack (2), in the global heap (3), or | |
6969 | -- in a user-defined storage pool (4). For the moment we just use | |
6970 | -- Natural for the type of this formal. Note that this formal | |
6971 | -- isn't usually needed in the case where the result subtype is | |
6972 | -- constrained, but it is needed when the function has a tagged | |
6973 | -- result, because generally such functions can be called in a | |
6974 | -- dispatching context and such calls must be handled like calls | |
6975 | -- to a class-wide function. | |
0a36105d | 6976 | |
1bb6e262 | 6977 | if Needs_BIP_Alloc_Form (E) then |
f937473f RD |
6978 | Discard := |
6979 | Add_Extra_Formal | |
6980 | (E, Standard_Natural, | |
6981 | E, BIP_Formal_Suffix (BIP_Alloc_Form)); | |
200b7162 | 6982 | |
8417f4b2 | 6983 | -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to |
3e452820 AC |
6984 | -- use a user-defined pool. This formal is not added on |
6985 | -- .NET/JVM/ZFP as those targets do not support pools. | |
200b7162 | 6986 | |
ea10ca9c AC |
6987 | if VM_Target = No_VM |
6988 | and then RTE_Available (RE_Root_Storage_Pool_Ptr) | |
3e452820 | 6989 | then |
8417f4b2 AC |
6990 | Discard := |
6991 | Add_Extra_Formal | |
6992 | (E, RTE (RE_Root_Storage_Pool_Ptr), | |
6993 | E, BIP_Formal_Suffix (BIP_Storage_Pool)); | |
6994 | end if; | |
f937473f | 6995 | end if; |
ec4867fa | 6996 | |
df3e68b1 | 6997 | -- In the case of functions whose result type needs finalization, |
ca5af305 | 6998 | -- add an extra formal which represents the finalization master. |
df3e68b1 | 6999 | |
ca5af305 | 7000 | if Needs_BIP_Finalization_Master (E) then |
f937473f RD |
7001 | Discard := |
7002 | Add_Extra_Formal | |
ca5af305 AC |
7003 | (E, RTE (RE_Finalization_Master_Ptr), |
7004 | E, BIP_Formal_Suffix (BIP_Finalization_Master)); | |
f937473f RD |
7005 | end if; |
7006 | ||
94bbf008 AC |
7007 | -- When the result type contains tasks, add two extra formals: the |
7008 | -- master of the tasks to be created, and the caller's activation | |
7009 | -- chain. | |
f937473f | 7010 | |
1a36a0cd | 7011 | if Has_Task (Full_Subt) then |
f937473f RD |
7012 | Discard := |
7013 | Add_Extra_Formal | |
7014 | (E, RTE (RE_Master_Id), | |
af89615f | 7015 | E, BIP_Formal_Suffix (BIP_Task_Master)); |
f937473f RD |
7016 | Discard := |
7017 | Add_Extra_Formal | |
7018 | (E, RTE (RE_Activation_Chain_Access), | |
7019 | E, BIP_Formal_Suffix (BIP_Activation_Chain)); | |
7020 | end if; | |
ec4867fa | 7021 | |
f937473f RD |
7022 | -- All build-in-place functions get an extra formal that will be |
7023 | -- passed the address of the return object within the caller. | |
ec4867fa | 7024 | |
1a36a0cd AC |
7025 | Formal_Typ := |
7026 | Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E)); | |
ec4867fa | 7027 | |
1a36a0cd AC |
7028 | Set_Directly_Designated_Type (Formal_Typ, Result_Subt); |
7029 | Set_Etype (Formal_Typ, Formal_Typ); | |
7030 | Set_Depends_On_Private | |
7031 | (Formal_Typ, Has_Private_Component (Formal_Typ)); | |
7032 | Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ))); | |
7033 | Set_Is_Access_Constant (Formal_Typ, False); | |
ec4867fa | 7034 | |
1a36a0cd AC |
7035 | -- Ada 2005 (AI-50217): Propagate the attribute that indicates |
7036 | -- the designated type comes from the limited view (for back-end | |
7037 | -- purposes). | |
ec4867fa | 7038 | |
7b56a91b AC |
7039 | Set_From_Limited_With |
7040 | (Formal_Typ, From_Limited_With (Result_Subt)); | |
f937473f | 7041 | |
1a36a0cd AC |
7042 | Layout_Type (Formal_Typ); |
7043 | ||
7044 | Discard := | |
7045 | Add_Extra_Formal | |
7046 | (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access)); | |
ec4867fa ES |
7047 | end; |
7048 | end if; | |
996ae0b0 RK |
7049 | end Create_Extra_Formals; |
7050 | ||
7051 | ----------------------------- | |
7052 | -- Enter_Overloaded_Entity -- | |
7053 | ----------------------------- | |
7054 | ||
7055 | procedure Enter_Overloaded_Entity (S : Entity_Id) is | |
7056 | E : Entity_Id := Current_Entity_In_Scope (S); | |
7057 | C_E : Entity_Id := Current_Entity (S); | |
7058 | ||
7059 | begin | |
7060 | if Present (E) then | |
7061 | Set_Has_Homonym (E); | |
7062 | Set_Has_Homonym (S); | |
7063 | end if; | |
7064 | ||
7065 | Set_Is_Immediately_Visible (S); | |
7066 | Set_Scope (S, Current_Scope); | |
7067 | ||
7068 | -- Chain new entity if front of homonym in current scope, so that | |
7069 | -- homonyms are contiguous. | |
7070 | ||
8fde064e | 7071 | if Present (E) and then E /= C_E then |
996ae0b0 RK |
7072 | while Homonym (C_E) /= E loop |
7073 | C_E := Homonym (C_E); | |
7074 | end loop; | |
7075 | ||
7076 | Set_Homonym (C_E, S); | |
7077 | ||
7078 | else | |
7079 | E := C_E; | |
7080 | Set_Current_Entity (S); | |
7081 | end if; | |
7082 | ||
7083 | Set_Homonym (S, E); | |
7084 | ||
2352eadb AC |
7085 | if Is_Inherited_Operation (S) then |
7086 | Append_Inherited_Subprogram (S); | |
7087 | else | |
7088 | Append_Entity (S, Current_Scope); | |
7089 | end if; | |
7090 | ||
996ae0b0 RK |
7091 | Set_Public_Status (S); |
7092 | ||
7093 | if Debug_Flag_E then | |
7094 | Write_Str ("New overloaded entity chain: "); | |
7095 | Write_Name (Chars (S)); | |
996ae0b0 | 7096 | |
82c80734 | 7097 | E := S; |
996ae0b0 RK |
7098 | while Present (E) loop |
7099 | Write_Str (" "); Write_Int (Int (E)); | |
7100 | E := Homonym (E); | |
7101 | end loop; | |
7102 | ||
7103 | Write_Eol; | |
7104 | end if; | |
7105 | ||
7106 | -- Generate warning for hiding | |
7107 | ||
7108 | if Warn_On_Hiding | |
7109 | and then Comes_From_Source (S) | |
7110 | and then In_Extended_Main_Source_Unit (S) | |
7111 | then | |
7112 | E := S; | |
7113 | loop | |
7114 | E := Homonym (E); | |
7115 | exit when No (E); | |
7116 | ||
7fc53871 AC |
7117 | -- Warn unless genuine overloading. Do not emit warning on |
7118 | -- hiding predefined operators in Standard (these are either an | |
7119 | -- (artifact of our implicit declarations, or simple noise) but | |
7120 | -- keep warning on a operator defined on a local subtype, because | |
7121 | -- of the real danger that different operators may be applied in | |
7122 | -- various parts of the program. | |
996ae0b0 | 7123 | |
1f250383 AC |
7124 | -- Note that if E and S have the same scope, there is never any |
7125 | -- hiding. Either the two conflict, and the program is illegal, | |
7126 | -- or S is overriding an implicit inherited subprogram. | |
7127 | ||
7128 | if Scope (E) /= Scope (S) | |
7129 | and then (not Is_Overloadable (E) | |
8d606a78 | 7130 | or else Subtype_Conformant (E, S)) |
f937473f RD |
7131 | and then (Is_Immediately_Visible (E) |
7132 | or else | |
7133 | Is_Potentially_Use_Visible (S)) | |
996ae0b0 | 7134 | then |
7fc53871 AC |
7135 | if Scope (E) /= Standard_Standard then |
7136 | Error_Msg_Sloc := Sloc (E); | |
3ccedacc | 7137 | Error_Msg_N ("declaration of & hides one #?h?", S); |
7fc53871 AC |
7138 | |
7139 | elsif Nkind (S) = N_Defining_Operator_Symbol | |
7140 | and then | |
1f250383 | 7141 | Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S) |
7fc53871 AC |
7142 | then |
7143 | Error_Msg_N | |
dbfeb4fa | 7144 | ("declaration of & hides predefined operator?h?", S); |
7fc53871 | 7145 | end if; |
996ae0b0 RK |
7146 | end if; |
7147 | end loop; | |
7148 | end if; | |
7149 | end Enter_Overloaded_Entity; | |
7150 | ||
e5a58fac AC |
7151 | ----------------------------- |
7152 | -- Check_Untagged_Equality -- | |
7153 | ----------------------------- | |
7154 | ||
7155 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is | |
7156 | Typ : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
7157 | Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op); | |
7158 | Obj_Decl : Node_Id; | |
7159 | ||
7160 | begin | |
7c0c194b AC |
7161 | -- This check applies only if we have a subprogram declaration with an |
7162 | -- untagged record type. | |
b2834fbd AC |
7163 | |
7164 | if Nkind (Decl) /= N_Subprogram_Declaration | |
7165 | or else not Is_Record_Type (Typ) | |
7166 | or else Is_Tagged_Type (Typ) | |
e5a58fac | 7167 | then |
b2834fbd AC |
7168 | return; |
7169 | end if; | |
e5a58fac | 7170 | |
b2834fbd AC |
7171 | -- In Ada 2012 case, we will output errors or warnings depending on |
7172 | -- the setting of debug flag -gnatd.E. | |
7173 | ||
7174 | if Ada_Version >= Ada_2012 then | |
7175 | Error_Msg_Warn := Debug_Flag_Dot_EE; | |
7176 | ||
7177 | -- In earlier versions of Ada, nothing to do unless we are warning on | |
7178 | -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set). | |
7179 | ||
7180 | else | |
7181 | if not Warn_On_Ada_2012_Compatibility then | |
7182 | return; | |
7183 | end if; | |
7184 | end if; | |
7185 | ||
7186 | -- Cases where the type has already been frozen | |
e5a58fac | 7187 | |
b2834fbd AC |
7188 | if Is_Frozen (Typ) then |
7189 | ||
7190 | -- If the type is not declared in a package, or if we are in the body | |
7191 | -- of the package or in some other scope, the new operation is not | |
7192 | -- primitive, and therefore legal, though suspicious. Should we | |
7193 | -- generate a warning in this case ??? | |
7194 | ||
7195 | if Ekind (Scope (Typ)) /= E_Package | |
7196 | or else Scope (Typ) /= Current_Scope | |
7197 | then | |
7198 | return; | |
7199 | ||
7200 | -- If the type is a generic actual (sub)type, the operation is not | |
7201 | -- primitive either because the base type is declared elsewhere. | |
7202 | ||
7203 | elsif Is_Generic_Actual_Type (Typ) then | |
7204 | return; | |
7205 | ||
7206 | -- Here we have a definite error of declaration after freezing | |
7207 | ||
7208 | else | |
7209 | if Ada_Version >= Ada_2012 then | |
ae6ede77 | 7210 | Error_Msg_NE |
3ccedacc | 7211 | ("equality operator must be declared before type & is " |
b2834fbd AC |
7212 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ); |
7213 | ||
7214 | -- In Ada 2012 mode with error turned to warning, output one | |
7215 | -- more warning to warn that the equality operation may not | |
7216 | -- compose. This is the consequence of ignoring the error. | |
7217 | ||
7218 | if Error_Msg_Warn then | |
7219 | Error_Msg_N ("\equality operation may not compose??", Eq_Op); | |
7220 | end if; | |
21a5b575 AC |
7221 | |
7222 | else | |
7223 | Error_Msg_NE | |
b2834fbd AC |
7224 | ("equality operator must be declared before type& is " |
7225 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ); | |
7226 | end if; | |
7227 | ||
7228 | -- If we are in the package body, we could just move the | |
7229 | -- declaration to the package spec, so add a message saying that. | |
7230 | ||
7231 | if In_Package_Body (Scope (Typ)) then | |
7232 | if Ada_Version >= Ada_2012 then | |
7233 | Error_Msg_N | |
7234 | ("\move declaration to package spec<<", Eq_Op); | |
7235 | else | |
7236 | Error_Msg_N | |
7237 | ("\move declaration to package spec (Ada 2012)?y?", Eq_Op); | |
7238 | end if; | |
21a5b575 | 7239 | |
b2834fbd AC |
7240 | -- Otherwise try to find the freezing point |
7241 | ||
7242 | else | |
21a5b575 | 7243 | Obj_Decl := Next (Parent (Typ)); |
dbfeb4fa | 7244 | while Present (Obj_Decl) and then Obj_Decl /= Decl loop |
21a5b575 AC |
7245 | if Nkind (Obj_Decl) = N_Object_Declaration |
7246 | and then Etype (Defining_Identifier (Obj_Decl)) = Typ | |
7247 | then | |
b2834fbd AC |
7248 | -- Freezing point, output warnings |
7249 | ||
7250 | if Ada_Version >= Ada_2012 then | |
7251 | Error_Msg_NE | |
7252 | ("type& is frozen by declaration??", Obj_Decl, Typ); | |
7253 | Error_Msg_N | |
7254 | ("\an equality operator cannot be declared after " | |
7255 | & "this point??", | |
7256 | Obj_Decl); | |
7257 | else | |
7258 | Error_Msg_NE | |
7259 | ("type& is frozen by declaration (Ada 2012)?y?", | |
7260 | Obj_Decl, Typ); | |
7261 | Error_Msg_N | |
7262 | ("\an equality operator cannot be declared after " | |
7263 | & "this point (Ada 2012)?y?", | |
7264 | Obj_Decl); | |
7265 | end if; | |
7266 | ||
21a5b575 AC |
7267 | exit; |
7268 | end if; | |
7269 | ||
7270 | Next (Obj_Decl); | |
7271 | end loop; | |
7272 | end if; | |
b2834fbd | 7273 | end if; |
e5a58fac | 7274 | |
b2834fbd AC |
7275 | -- Here if type is not frozen yet. It is illegal to have a primitive |
7276 | -- equality declared in the private part if the type is visible. | |
21a5b575 | 7277 | |
b2834fbd AC |
7278 | elsif not In_Same_List (Parent (Typ), Decl) |
7279 | and then not Is_Limited_Type (Typ) | |
7280 | then | |
7281 | -- Shouldn't we give an RM reference here??? | |
21a5b575 | 7282 | |
b2834fbd AC |
7283 | if Ada_Version >= Ada_2012 then |
7284 | Error_Msg_N | |
7285 | ("equality operator appears too late<<", Eq_Op); | |
7286 | else | |
7287 | Error_Msg_N | |
7288 | ("equality operator appears too late (Ada 2012)?y?", Eq_Op); | |
e5a58fac | 7289 | end if; |
b2834fbd AC |
7290 | |
7291 | -- No error detected | |
7292 | ||
7293 | else | |
7294 | return; | |
e5a58fac AC |
7295 | end if; |
7296 | end Check_Untagged_Equality; | |
7297 | ||
996ae0b0 RK |
7298 | ----------------------------- |
7299 | -- Find_Corresponding_Spec -- | |
7300 | ----------------------------- | |
7301 | ||
d44202ba HK |
7302 | function Find_Corresponding_Spec |
7303 | (N : Node_Id; | |
7304 | Post_Error : Boolean := True) return Entity_Id | |
7305 | is | |
996ae0b0 RK |
7306 | Spec : constant Node_Id := Specification (N); |
7307 | Designator : constant Entity_Id := Defining_Entity (Spec); | |
7308 | ||
7309 | E : Entity_Id; | |
7310 | ||
70f4ad20 AC |
7311 | function Different_Generic_Profile (E : Entity_Id) return Boolean; |
7312 | -- Even if fully conformant, a body may depend on a generic actual when | |
7313 | -- the spec does not, or vice versa, in which case they were distinct | |
7314 | -- entities in the generic. | |
7315 | ||
7316 | ------------------------------- | |
7317 | -- Different_Generic_Profile -- | |
7318 | ------------------------------- | |
7319 | ||
7320 | function Different_Generic_Profile (E : Entity_Id) return Boolean is | |
7321 | F1, F2 : Entity_Id; | |
7322 | ||
2995860f AC |
7323 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean; |
7324 | -- Check that the types of corresponding formals have the same | |
7325 | -- generic actual if any. We have to account for subtypes of a | |
7326 | -- generic formal, declared between a spec and a body, which may | |
73999267 AC |
7327 | -- appear distinct in an instance but matched in the generic, and |
7328 | -- the subtype may be used either in the spec or the body of the | |
7329 | -- subprogram being checked. | |
2995860f AC |
7330 | |
7331 | ------------------------- | |
7332 | -- Same_Generic_Actual -- | |
7333 | ------------------------- | |
7334 | ||
7335 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is | |
73999267 AC |
7336 | |
7337 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean; | |
7338 | -- Predicate to check whether S1 is a subtype of S2 in the source | |
7339 | -- of the instance. | |
7340 | ||
7341 | ------------------------- | |
7342 | -- Is_Declared_Subtype -- | |
7343 | ------------------------- | |
7344 | ||
7345 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is | |
7346 | begin | |
7347 | return Comes_From_Source (Parent (S1)) | |
7348 | and then Nkind (Parent (S1)) = N_Subtype_Declaration | |
7349 | and then Is_Entity_Name (Subtype_Indication (Parent (S1))) | |
7350 | and then Entity (Subtype_Indication (Parent (S1))) = S2; | |
7351 | end Is_Declared_Subtype; | |
7352 | ||
7353 | -- Start of processing for Same_Generic_Actual | |
7354 | ||
2995860f AC |
7355 | begin |
7356 | return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2) | |
73999267 AC |
7357 | or else Is_Declared_Subtype (T1, T2) |
7358 | or else Is_Declared_Subtype (T2, T1); | |
2995860f AC |
7359 | end Same_Generic_Actual; |
7360 | ||
7361 | -- Start of processing for Different_Generic_Profile | |
7362 | ||
70f4ad20 | 7363 | begin |
2995860f AC |
7364 | if not In_Instance then |
7365 | return False; | |
7366 | ||
7367 | elsif Ekind (E) = E_Function | |
7368 | and then not Same_Generic_Actual (Etype (E), Etype (Designator)) | |
70f4ad20 AC |
7369 | then |
7370 | return True; | |
7371 | end if; | |
7372 | ||
7373 | F1 := First_Formal (Designator); | |
7374 | F2 := First_Formal (E); | |
70f4ad20 | 7375 | while Present (F1) loop |
2995860f | 7376 | if not Same_Generic_Actual (Etype (F1), Etype (F2)) then |
70f4ad20 AC |
7377 | return True; |
7378 | end if; | |
7379 | ||
7380 | Next_Formal (F1); | |
7381 | Next_Formal (F2); | |
7382 | end loop; | |
7383 | ||
7384 | return False; | |
7385 | end Different_Generic_Profile; | |
7386 | ||
7387 | -- Start of processing for Find_Corresponding_Spec | |
7388 | ||
996ae0b0 RK |
7389 | begin |
7390 | E := Current_Entity (Designator); | |
996ae0b0 RK |
7391 | while Present (E) loop |
7392 | ||
7393 | -- We are looking for a matching spec. It must have the same scope, | |
7394 | -- and the same name, and either be type conformant, or be the case | |
7395 | -- of a library procedure spec and its body (which belong to one | |
7396 | -- another regardless of whether they are type conformant or not). | |
7397 | ||
7398 | if Scope (E) = Current_Scope then | |
fbf5a39b AC |
7399 | if Current_Scope = Standard_Standard |
7400 | or else (Ekind (E) = Ekind (Designator) | |
586ecbf3 | 7401 | and then Type_Conformant (E, Designator)) |
996ae0b0 RK |
7402 | then |
7403 | -- Within an instantiation, we know that spec and body are | |
70f4ad20 AC |
7404 | -- subtype conformant, because they were subtype conformant in |
7405 | -- the generic. We choose the subtype-conformant entity here as | |
7406 | -- well, to resolve spurious ambiguities in the instance that | |
7407 | -- were not present in the generic (i.e. when two different | |
7408 | -- types are given the same actual). If we are looking for a | |
7409 | -- spec to match a body, full conformance is expected. | |
996ae0b0 RK |
7410 | |
7411 | if In_Instance then | |
7412 | Set_Convention (Designator, Convention (E)); | |
7413 | ||
0187b60e AC |
7414 | -- Skip past subprogram bodies and subprogram renamings that |
7415 | -- may appear to have a matching spec, but that aren't fully | |
7416 | -- conformant with it. That can occur in cases where an | |
7417 | -- actual type causes unrelated homographs in the instance. | |
7418 | ||
7419 | if Nkind_In (N, N_Subprogram_Body, | |
7420 | N_Subprogram_Renaming_Declaration) | |
996ae0b0 | 7421 | and then Present (Homonym (E)) |
c7b9d548 | 7422 | and then not Fully_Conformant (Designator, E) |
996ae0b0 RK |
7423 | then |
7424 | goto Next_Entity; | |
7425 | ||
c7b9d548 | 7426 | elsif not Subtype_Conformant (Designator, E) then |
996ae0b0 | 7427 | goto Next_Entity; |
70f4ad20 AC |
7428 | |
7429 | elsif Different_Generic_Profile (E) then | |
7430 | goto Next_Entity; | |
996ae0b0 RK |
7431 | end if; |
7432 | end if; | |
7433 | ||
25ebc085 AC |
7434 | -- Ada 2012 (AI05-0165): For internally generated bodies of |
7435 | -- null procedures locate the internally generated spec. We | |
7436 | -- enforce mode conformance since a tagged type may inherit | |
7437 | -- from interfaces several null primitives which differ only | |
7438 | -- in the mode of the formals. | |
7439 | ||
7440 | if not (Comes_From_Source (E)) | |
7441 | and then Is_Null_Procedure (E) | |
7442 | and then not Mode_Conformant (Designator, E) | |
7443 | then | |
7444 | null; | |
7445 | ||
4d8f3296 ES |
7446 | -- For null procedures coming from source that are completions, |
7447 | -- analysis of the generated body will establish the link. | |
7448 | ||
7449 | elsif Comes_From_Source (E) | |
7450 | and then Nkind (Spec) = N_Procedure_Specification | |
7451 | and then Null_Present (Spec) | |
7452 | then | |
7453 | return E; | |
7454 | ||
25ebc085 | 7455 | elsif not Has_Completion (E) then |
996ae0b0 RK |
7456 | if Nkind (N) /= N_Subprogram_Body_Stub then |
7457 | Set_Corresponding_Spec (N, E); | |
7458 | end if; | |
7459 | ||
7460 | Set_Has_Completion (E); | |
7461 | return E; | |
7462 | ||
7463 | elsif Nkind (Parent (N)) = N_Subunit then | |
7464 | ||
7465 | -- If this is the proper body of a subunit, the completion | |
7466 | -- flag is set when analyzing the stub. | |
7467 | ||
7468 | return E; | |
7469 | ||
70f4ad20 AC |
7470 | -- If E is an internal function with a controlling result that |
7471 | -- was created for an operation inherited by a null extension, | |
7472 | -- it may be overridden by a body without a previous spec (one | |
2995860f | 7473 | -- more reason why these should be shunned). In that case we |
70f4ad20 AC |
7474 | -- remove the generated body if present, because the current |
7475 | -- one is the explicit overriding. | |
81db9d77 ES |
7476 | |
7477 | elsif Ekind (E) = E_Function | |
0791fbe9 | 7478 | and then Ada_Version >= Ada_2005 |
81db9d77 ES |
7479 | and then not Comes_From_Source (E) |
7480 | and then Has_Controlling_Result (E) | |
7481 | and then Is_Null_Extension (Etype (E)) | |
7482 | and then Comes_From_Source (Spec) | |
7483 | then | |
7484 | Set_Has_Completion (E, False); | |
7485 | ||
1366997b AC |
7486 | if Expander_Active |
7487 | and then Nkind (Parent (E)) = N_Function_Specification | |
7488 | then | |
81db9d77 ES |
7489 | Remove |
7490 | (Unit_Declaration_Node | |
1366997b AC |
7491 | (Corresponding_Body (Unit_Declaration_Node (E)))); |
7492 | ||
81db9d77 ES |
7493 | return E; |
7494 | ||
1366997b AC |
7495 | -- If expansion is disabled, or if the wrapper function has |
7496 | -- not been generated yet, this a late body overriding an | |
7497 | -- inherited operation, or it is an overriding by some other | |
7498 | -- declaration before the controlling result is frozen. In | |
7499 | -- either case this is a declaration of a new entity. | |
81db9d77 ES |
7500 | |
7501 | else | |
7502 | return Empty; | |
7503 | end if; | |
7504 | ||
d44202ba HK |
7505 | -- If the body already exists, then this is an error unless |
7506 | -- the previous declaration is the implicit declaration of a | |
756ef2a0 AC |
7507 | -- derived subprogram. It is also legal for an instance to |
7508 | -- contain type conformant overloadable declarations (but the | |
7509 | -- generic declaration may not), per 8.3(26/2). | |
996ae0b0 RK |
7510 | |
7511 | elsif No (Alias (E)) | |
7512 | and then not Is_Intrinsic_Subprogram (E) | |
7513 | and then not In_Instance | |
d44202ba | 7514 | and then Post_Error |
996ae0b0 RK |
7515 | then |
7516 | Error_Msg_Sloc := Sloc (E); | |
8dbd1460 | 7517 | |
07fc65c4 GB |
7518 | if Is_Imported (E) then |
7519 | Error_Msg_NE | |
7520 | ("body not allowed for imported subprogram & declared#", | |
7521 | N, E); | |
7522 | else | |
7523 | Error_Msg_NE ("duplicate body for & declared#", N, E); | |
7524 | end if; | |
996ae0b0 RK |
7525 | end if; |
7526 | ||
d44202ba HK |
7527 | -- Child units cannot be overloaded, so a conformance mismatch |
7528 | -- between body and a previous spec is an error. | |
7529 | ||
996ae0b0 RK |
7530 | elsif Is_Child_Unit (E) |
7531 | and then | |
7532 | Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body | |
7533 | and then | |
5d37ba92 | 7534 | Nkind (Parent (Unit_Declaration_Node (Designator))) = |
d44202ba HK |
7535 | N_Compilation_Unit |
7536 | and then Post_Error | |
996ae0b0 | 7537 | then |
996ae0b0 RK |
7538 | Error_Msg_N |
7539 | ("body of child unit does not match previous declaration", N); | |
7540 | end if; | |
7541 | end if; | |
7542 | ||
7543 | <<Next_Entity>> | |
7544 | E := Homonym (E); | |
7545 | end loop; | |
7546 | ||
7547 | -- On exit, we know that no previous declaration of subprogram exists | |
7548 | ||
7549 | return Empty; | |
7550 | end Find_Corresponding_Spec; | |
7551 | ||
7552 | ---------------------- | |
7553 | -- Fully_Conformant -- | |
7554 | ---------------------- | |
7555 | ||
7556 | function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
7557 | Result : Boolean; | |
996ae0b0 RK |
7558 | begin |
7559 | Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result); | |
7560 | return Result; | |
7561 | end Fully_Conformant; | |
7562 | ||
7563 | ---------------------------------- | |
7564 | -- Fully_Conformant_Expressions -- | |
7565 | ---------------------------------- | |
7566 | ||
7567 | function Fully_Conformant_Expressions | |
7568 | (Given_E1 : Node_Id; | |
d05ef0ab | 7569 | Given_E2 : Node_Id) return Boolean |
996ae0b0 RK |
7570 | is |
7571 | E1 : constant Node_Id := Original_Node (Given_E1); | |
7572 | E2 : constant Node_Id := Original_Node (Given_E2); | |
7573 | -- We always test conformance on original nodes, since it is possible | |
7574 | -- for analysis and/or expansion to make things look as though they | |
7575 | -- conform when they do not, e.g. by converting 1+2 into 3. | |
7576 | ||
7577 | function FCE (Given_E1, Given_E2 : Node_Id) return Boolean | |
7578 | renames Fully_Conformant_Expressions; | |
7579 | ||
7580 | function FCL (L1, L2 : List_Id) return Boolean; | |
70f4ad20 AC |
7581 | -- Compare elements of two lists for conformance. Elements have to be |
7582 | -- conformant, and actuals inserted as default parameters do not match | |
7583 | -- explicit actuals with the same value. | |
996ae0b0 RK |
7584 | |
7585 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean; | |
e895b435 | 7586 | -- Compare an operator node with a function call |
996ae0b0 RK |
7587 | |
7588 | --------- | |
7589 | -- FCL -- | |
7590 | --------- | |
7591 | ||
7592 | function FCL (L1, L2 : List_Id) return Boolean is | |
7593 | N1, N2 : Node_Id; | |
7594 | ||
7595 | begin | |
7596 | if L1 = No_List then | |
7597 | N1 := Empty; | |
7598 | else | |
7599 | N1 := First (L1); | |
7600 | end if; | |
7601 | ||
7602 | if L2 = No_List then | |
7603 | N2 := Empty; | |
7604 | else | |
7605 | N2 := First (L2); | |
7606 | end if; | |
7607 | ||
70f4ad20 | 7608 | -- Compare two lists, skipping rewrite insertions (we want to compare |
a90bd866 | 7609 | -- the original trees, not the expanded versions). |
996ae0b0 RK |
7610 | |
7611 | loop | |
7612 | if Is_Rewrite_Insertion (N1) then | |
7613 | Next (N1); | |
7614 | elsif Is_Rewrite_Insertion (N2) then | |
7615 | Next (N2); | |
7616 | elsif No (N1) then | |
7617 | return No (N2); | |
7618 | elsif No (N2) then | |
7619 | return False; | |
7620 | elsif not FCE (N1, N2) then | |
7621 | return False; | |
7622 | else | |
7623 | Next (N1); | |
7624 | Next (N2); | |
7625 | end if; | |
7626 | end loop; | |
7627 | end FCL; | |
7628 | ||
7629 | --------- | |
7630 | -- FCO -- | |
7631 | --------- | |
7632 | ||
7633 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean is | |
7634 | Actuals : constant List_Id := Parameter_Associations (Call_Node); | |
7635 | Act : Node_Id; | |
7636 | ||
7637 | begin | |
7638 | if No (Actuals) | |
7639 | or else Entity (Op_Node) /= Entity (Name (Call_Node)) | |
7640 | then | |
7641 | return False; | |
7642 | ||
7643 | else | |
7644 | Act := First (Actuals); | |
7645 | ||
7646 | if Nkind (Op_Node) in N_Binary_Op then | |
996ae0b0 RK |
7647 | if not FCE (Left_Opnd (Op_Node), Act) then |
7648 | return False; | |
7649 | end if; | |
7650 | ||
7651 | Next (Act); | |
7652 | end if; | |
7653 | ||
7654 | return Present (Act) | |
7655 | and then FCE (Right_Opnd (Op_Node), Act) | |
7656 | and then No (Next (Act)); | |
7657 | end if; | |
7658 | end FCO; | |
7659 | ||
7660 | -- Start of processing for Fully_Conformant_Expressions | |
7661 | ||
7662 | begin | |
7663 | -- Non-conformant if paren count does not match. Note: if some idiot | |
7664 | -- complains that we don't do this right for more than 3 levels of | |
a90bd866 | 7665 | -- parentheses, they will be treated with the respect they deserve. |
996ae0b0 RK |
7666 | |
7667 | if Paren_Count (E1) /= Paren_Count (E2) then | |
7668 | return False; | |
7669 | ||
82c80734 RD |
7670 | -- If same entities are referenced, then they are conformant even if |
7671 | -- they have different forms (RM 8.3.1(19-20)). | |
996ae0b0 RK |
7672 | |
7673 | elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then | |
7674 | if Present (Entity (E1)) then | |
7675 | return Entity (E1) = Entity (E2) | |
7676 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) | |
7677 | and then Ekind (Entity (E1)) = E_Discriminant | |
7678 | and then Ekind (Entity (E2)) = E_In_Parameter); | |
7679 | ||
7680 | elsif Nkind (E1) = N_Expanded_Name | |
7681 | and then Nkind (E2) = N_Expanded_Name | |
7682 | and then Nkind (Selector_Name (E1)) = N_Character_Literal | |
7683 | and then Nkind (Selector_Name (E2)) = N_Character_Literal | |
7684 | then | |
7685 | return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)); | |
7686 | ||
7687 | else | |
7688 | -- Identifiers in component associations don't always have | |
7689 | -- entities, but their names must conform. | |
7690 | ||
7691 | return Nkind (E1) = N_Identifier | |
7692 | and then Nkind (E2) = N_Identifier | |
7693 | and then Chars (E1) = Chars (E2); | |
7694 | end if; | |
7695 | ||
7696 | elsif Nkind (E1) = N_Character_Literal | |
7697 | and then Nkind (E2) = N_Expanded_Name | |
7698 | then | |
7699 | return Nkind (Selector_Name (E2)) = N_Character_Literal | |
7700 | and then Chars (E1) = Chars (Selector_Name (E2)); | |
7701 | ||
7702 | elsif Nkind (E2) = N_Character_Literal | |
7703 | and then Nkind (E1) = N_Expanded_Name | |
7704 | then | |
7705 | return Nkind (Selector_Name (E1)) = N_Character_Literal | |
7706 | and then Chars (E2) = Chars (Selector_Name (E1)); | |
7707 | ||
8fde064e | 7708 | elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then |
996ae0b0 RK |
7709 | return FCO (E1, E2); |
7710 | ||
8fde064e | 7711 | elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then |
996ae0b0 RK |
7712 | return FCO (E2, E1); |
7713 | ||
7714 | -- Otherwise we must have the same syntactic entity | |
7715 | ||
7716 | elsif Nkind (E1) /= Nkind (E2) then | |
7717 | return False; | |
7718 | ||
7719 | -- At this point, we specialize by node type | |
7720 | ||
7721 | else | |
7722 | case Nkind (E1) is | |
7723 | ||
7724 | when N_Aggregate => | |
7725 | return | |
7726 | FCL (Expressions (E1), Expressions (E2)) | |
19d846a0 RD |
7727 | and then |
7728 | FCL (Component_Associations (E1), | |
7729 | Component_Associations (E2)); | |
996ae0b0 RK |
7730 | |
7731 | when N_Allocator => | |
7732 | if Nkind (Expression (E1)) = N_Qualified_Expression | |
7733 | or else | |
7734 | Nkind (Expression (E2)) = N_Qualified_Expression | |
7735 | then | |
7736 | return FCE (Expression (E1), Expression (E2)); | |
7737 | ||
7738 | -- Check that the subtype marks and any constraints | |
7739 | -- are conformant | |
7740 | ||
7741 | else | |
7742 | declare | |
7743 | Indic1 : constant Node_Id := Expression (E1); | |
7744 | Indic2 : constant Node_Id := Expression (E2); | |
7745 | Elt1 : Node_Id; | |
7746 | Elt2 : Node_Id; | |
7747 | ||
7748 | begin | |
7749 | if Nkind (Indic1) /= N_Subtype_Indication then | |
7750 | return | |
7751 | Nkind (Indic2) /= N_Subtype_Indication | |
7752 | and then Entity (Indic1) = Entity (Indic2); | |
7753 | ||
7754 | elsif Nkind (Indic2) /= N_Subtype_Indication then | |
7755 | return | |
7756 | Nkind (Indic1) /= N_Subtype_Indication | |
7757 | and then Entity (Indic1) = Entity (Indic2); | |
7758 | ||
7759 | else | |
7760 | if Entity (Subtype_Mark (Indic1)) /= | |
7761 | Entity (Subtype_Mark (Indic2)) | |
7762 | then | |
7763 | return False; | |
7764 | end if; | |
7765 | ||
7766 | Elt1 := First (Constraints (Constraint (Indic1))); | |
7767 | Elt2 := First (Constraints (Constraint (Indic2))); | |
996ae0b0 RK |
7768 | while Present (Elt1) and then Present (Elt2) loop |
7769 | if not FCE (Elt1, Elt2) then | |
7770 | return False; | |
7771 | end if; | |
7772 | ||
7773 | Next (Elt1); | |
7774 | Next (Elt2); | |
7775 | end loop; | |
7776 | ||
7777 | return True; | |
7778 | end if; | |
7779 | end; | |
7780 | end if; | |
7781 | ||
7782 | when N_Attribute_Reference => | |
7783 | return | |
7784 | Attribute_Name (E1) = Attribute_Name (E2) | |
7785 | and then FCL (Expressions (E1), Expressions (E2)); | |
7786 | ||
7787 | when N_Binary_Op => | |
7788 | return | |
7789 | Entity (E1) = Entity (E2) | |
7790 | and then FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7791 | and then FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7792 | ||
514d0fc5 | 7793 | when N_Short_Circuit | N_Membership_Test => |
996ae0b0 RK |
7794 | return |
7795 | FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7796 | and then | |
7797 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7798 | ||
19d846a0 RD |
7799 | when N_Case_Expression => |
7800 | declare | |
7801 | Alt1 : Node_Id; | |
7802 | Alt2 : Node_Id; | |
7803 | ||
7804 | begin | |
7805 | if not FCE (Expression (E1), Expression (E2)) then | |
7806 | return False; | |
7807 | ||
7808 | else | |
7809 | Alt1 := First (Alternatives (E1)); | |
7810 | Alt2 := First (Alternatives (E2)); | |
7811 | loop | |
7812 | if Present (Alt1) /= Present (Alt2) then | |
7813 | return False; | |
7814 | elsif No (Alt1) then | |
7815 | return True; | |
7816 | end if; | |
7817 | ||
7818 | if not FCE (Expression (Alt1), Expression (Alt2)) | |
7819 | or else not FCL (Discrete_Choices (Alt1), | |
7820 | Discrete_Choices (Alt2)) | |
7821 | then | |
7822 | return False; | |
7823 | end if; | |
7824 | ||
7825 | Next (Alt1); | |
7826 | Next (Alt2); | |
7827 | end loop; | |
7828 | end if; | |
7829 | end; | |
7830 | ||
996ae0b0 RK |
7831 | when N_Character_Literal => |
7832 | return | |
7833 | Char_Literal_Value (E1) = Char_Literal_Value (E2); | |
7834 | ||
7835 | when N_Component_Association => | |
7836 | return | |
7837 | FCL (Choices (E1), Choices (E2)) | |
19d846a0 RD |
7838 | and then |
7839 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 7840 | |
996ae0b0 RK |
7841 | when N_Explicit_Dereference => |
7842 | return | |
7843 | FCE (Prefix (E1), Prefix (E2)); | |
7844 | ||
7845 | when N_Extension_Aggregate => | |
7846 | return | |
7847 | FCL (Expressions (E1), Expressions (E2)) | |
7848 | and then Null_Record_Present (E1) = | |
7849 | Null_Record_Present (E2) | |
7850 | and then FCL (Component_Associations (E1), | |
7851 | Component_Associations (E2)); | |
7852 | ||
7853 | when N_Function_Call => | |
7854 | return | |
7855 | FCE (Name (E1), Name (E2)) | |
19d846a0 RD |
7856 | and then |
7857 | FCL (Parameter_Associations (E1), | |
7858 | Parameter_Associations (E2)); | |
996ae0b0 | 7859 | |
9b16cb57 RD |
7860 | when N_If_Expression => |
7861 | return | |
7862 | FCL (Expressions (E1), Expressions (E2)); | |
7863 | ||
996ae0b0 RK |
7864 | when N_Indexed_Component => |
7865 | return | |
7866 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7867 | and then |
7868 | FCL (Expressions (E1), Expressions (E2)); | |
996ae0b0 RK |
7869 | |
7870 | when N_Integer_Literal => | |
7871 | return (Intval (E1) = Intval (E2)); | |
7872 | ||
7873 | when N_Null => | |
7874 | return True; | |
7875 | ||
7876 | when N_Operator_Symbol => | |
7877 | return | |
7878 | Chars (E1) = Chars (E2); | |
7879 | ||
7880 | when N_Others_Choice => | |
7881 | return True; | |
7882 | ||
7883 | when N_Parameter_Association => | |
7884 | return | |
996ae0b0 RK |
7885 | Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)) |
7886 | and then FCE (Explicit_Actual_Parameter (E1), | |
7887 | Explicit_Actual_Parameter (E2)); | |
7888 | ||
7889 | when N_Qualified_Expression => | |
7890 | return | |
7891 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7892 | and then |
7893 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 7894 | |
2010d078 AC |
7895 | when N_Quantified_Expression => |
7896 | if not FCE (Condition (E1), Condition (E2)) then | |
7897 | return False; | |
7898 | end if; | |
7899 | ||
7900 | if Present (Loop_Parameter_Specification (E1)) | |
7901 | and then Present (Loop_Parameter_Specification (E2)) | |
7902 | then | |
7903 | declare | |
7904 | L1 : constant Node_Id := | |
7905 | Loop_Parameter_Specification (E1); | |
7906 | L2 : constant Node_Id := | |
7907 | Loop_Parameter_Specification (E2); | |
7908 | ||
7909 | begin | |
7910 | return | |
7911 | Reverse_Present (L1) = Reverse_Present (L2) | |
7912 | and then | |
7913 | FCE (Defining_Identifier (L1), | |
7914 | Defining_Identifier (L2)) | |
7915 | and then | |
7916 | FCE (Discrete_Subtype_Definition (L1), | |
7917 | Discrete_Subtype_Definition (L2)); | |
7918 | end; | |
7919 | ||
804670f1 AC |
7920 | elsif Present (Iterator_Specification (E1)) |
7921 | and then Present (Iterator_Specification (E2)) | |
7922 | then | |
2010d078 AC |
7923 | declare |
7924 | I1 : constant Node_Id := Iterator_Specification (E1); | |
7925 | I2 : constant Node_Id := Iterator_Specification (E2); | |
7926 | ||
7927 | begin | |
7928 | return | |
7929 | FCE (Defining_Identifier (I1), | |
7930 | Defining_Identifier (I2)) | |
7931 | and then | |
7932 | Of_Present (I1) = Of_Present (I2) | |
7933 | and then | |
7934 | Reverse_Present (I1) = Reverse_Present (I2) | |
7935 | and then FCE (Name (I1), Name (I2)) | |
7936 | and then FCE (Subtype_Indication (I1), | |
7937 | Subtype_Indication (I2)); | |
7938 | end; | |
804670f1 AC |
7939 | |
7940 | -- The quantified expressions used different specifications to | |
7941 | -- walk their respective ranges. | |
7942 | ||
7943 | else | |
7944 | return False; | |
2010d078 AC |
7945 | end if; |
7946 | ||
996ae0b0 RK |
7947 | when N_Range => |
7948 | return | |
7949 | FCE (Low_Bound (E1), Low_Bound (E2)) | |
19d846a0 RD |
7950 | and then |
7951 | FCE (High_Bound (E1), High_Bound (E2)); | |
996ae0b0 RK |
7952 | |
7953 | when N_Real_Literal => | |
7954 | return (Realval (E1) = Realval (E2)); | |
7955 | ||
7956 | when N_Selected_Component => | |
7957 | return | |
7958 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7959 | and then |
7960 | FCE (Selector_Name (E1), Selector_Name (E2)); | |
996ae0b0 RK |
7961 | |
7962 | when N_Slice => | |
7963 | return | |
7964 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7965 | and then |
7966 | FCE (Discrete_Range (E1), Discrete_Range (E2)); | |
996ae0b0 RK |
7967 | |
7968 | when N_String_Literal => | |
7969 | declare | |
7970 | S1 : constant String_Id := Strval (E1); | |
7971 | S2 : constant String_Id := Strval (E2); | |
7972 | L1 : constant Nat := String_Length (S1); | |
7973 | L2 : constant Nat := String_Length (S2); | |
7974 | ||
7975 | begin | |
7976 | if L1 /= L2 then | |
7977 | return False; | |
7978 | ||
7979 | else | |
7980 | for J in 1 .. L1 loop | |
7981 | if Get_String_Char (S1, J) /= | |
7982 | Get_String_Char (S2, J) | |
7983 | then | |
7984 | return False; | |
7985 | end if; | |
7986 | end loop; | |
7987 | ||
7988 | return True; | |
7989 | end if; | |
7990 | end; | |
7991 | ||
7992 | when N_Type_Conversion => | |
7993 | return | |
7994 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7995 | and then |
7996 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7997 | |
7998 | when N_Unary_Op => | |
7999 | return | |
8000 | Entity (E1) = Entity (E2) | |
19d846a0 RD |
8001 | and then |
8002 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
996ae0b0 RK |
8003 | |
8004 | when N_Unchecked_Type_Conversion => | |
8005 | return | |
8006 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
8007 | and then |
8008 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
8009 | |
8010 | -- All other node types cannot appear in this context. Strictly | |
8011 | -- we should raise a fatal internal error. Instead we just ignore | |
8012 | -- the nodes. This means that if anyone makes a mistake in the | |
2995860f AC |
8013 | -- expander and mucks an expression tree irretrievably, the result |
8014 | -- will be a failure to detect a (probably very obscure) case | |
8015 | -- of non-conformance, which is better than bombing on some | |
996ae0b0 RK |
8016 | -- case where two expressions do in fact conform. |
8017 | ||
8018 | when others => | |
8019 | return True; | |
8020 | ||
8021 | end case; | |
8022 | end if; | |
8023 | end Fully_Conformant_Expressions; | |
8024 | ||
fbf5a39b AC |
8025 | ---------------------------------------- |
8026 | -- Fully_Conformant_Discrete_Subtypes -- | |
8027 | ---------------------------------------- | |
8028 | ||
8029 | function Fully_Conformant_Discrete_Subtypes | |
8030 | (Given_S1 : Node_Id; | |
d05ef0ab | 8031 | Given_S2 : Node_Id) return Boolean |
fbf5a39b AC |
8032 | is |
8033 | S1 : constant Node_Id := Original_Node (Given_S1); | |
8034 | S2 : constant Node_Id := Original_Node (Given_S2); | |
8035 | ||
8036 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean; | |
82c80734 RD |
8037 | -- Special-case for a bound given by a discriminant, which in the body |
8038 | -- is replaced with the discriminal of the enclosing type. | |
fbf5a39b AC |
8039 | |
8040 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean; | |
e895b435 | 8041 | -- Check both bounds |
fbf5a39b | 8042 | |
5d37ba92 ES |
8043 | ----------------------- |
8044 | -- Conforming_Bounds -- | |
8045 | ----------------------- | |
8046 | ||
fbf5a39b AC |
8047 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is |
8048 | begin | |
8049 | if Is_Entity_Name (B1) | |
8050 | and then Is_Entity_Name (B2) | |
8051 | and then Ekind (Entity (B1)) = E_Discriminant | |
8052 | then | |
8053 | return Chars (B1) = Chars (B2); | |
8054 | ||
8055 | else | |
8056 | return Fully_Conformant_Expressions (B1, B2); | |
8057 | end if; | |
8058 | end Conforming_Bounds; | |
8059 | ||
5d37ba92 ES |
8060 | ----------------------- |
8061 | -- Conforming_Ranges -- | |
8062 | ----------------------- | |
8063 | ||
fbf5a39b AC |
8064 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is |
8065 | begin | |
8066 | return | |
8067 | Conforming_Bounds (Low_Bound (R1), Low_Bound (R2)) | |
8068 | and then | |
8069 | Conforming_Bounds (High_Bound (R1), High_Bound (R2)); | |
8070 | end Conforming_Ranges; | |
8071 | ||
8072 | -- Start of processing for Fully_Conformant_Discrete_Subtypes | |
8073 | ||
8074 | begin | |
8075 | if Nkind (S1) /= Nkind (S2) then | |
8076 | return False; | |
8077 | ||
8078 | elsif Is_Entity_Name (S1) then | |
8079 | return Entity (S1) = Entity (S2); | |
8080 | ||
8081 | elsif Nkind (S1) = N_Range then | |
8082 | return Conforming_Ranges (S1, S2); | |
8083 | ||
8084 | elsif Nkind (S1) = N_Subtype_Indication then | |
8085 | return | |
8086 | Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2)) | |
8087 | and then | |
8088 | Conforming_Ranges | |
8089 | (Range_Expression (Constraint (S1)), | |
8090 | Range_Expression (Constraint (S2))); | |
8091 | else | |
8092 | return True; | |
8093 | end if; | |
8094 | end Fully_Conformant_Discrete_Subtypes; | |
8095 | ||
996ae0b0 RK |
8096 | -------------------- |
8097 | -- Install_Entity -- | |
8098 | -------------------- | |
8099 | ||
8100 | procedure Install_Entity (E : Entity_Id) is | |
8101 | Prev : constant Entity_Id := Current_Entity (E); | |
996ae0b0 RK |
8102 | begin |
8103 | Set_Is_Immediately_Visible (E); | |
8104 | Set_Current_Entity (E); | |
8105 | Set_Homonym (E, Prev); | |
8106 | end Install_Entity; | |
8107 | ||
8108 | --------------------- | |
8109 | -- Install_Formals -- | |
8110 | --------------------- | |
8111 | ||
8112 | procedure Install_Formals (Id : Entity_Id) is | |
8113 | F : Entity_Id; | |
996ae0b0 RK |
8114 | begin |
8115 | F := First_Formal (Id); | |
996ae0b0 RK |
8116 | while Present (F) loop |
8117 | Install_Entity (F); | |
8118 | Next_Formal (F); | |
8119 | end loop; | |
8120 | end Install_Formals; | |
8121 | ||
ce2b6ba5 JM |
8122 | ----------------------------- |
8123 | -- Is_Interface_Conformant -- | |
8124 | ----------------------------- | |
8125 | ||
8126 | function Is_Interface_Conformant | |
8127 | (Tagged_Type : Entity_Id; | |
8128 | Iface_Prim : Entity_Id; | |
8129 | Prim : Entity_Id) return Boolean | |
8130 | is | |
9e92ad49 AC |
8131 | -- The operation may in fact be an inherited (implicit) operation |
8132 | -- rather than the original interface primitive, so retrieve the | |
8133 | -- ultimate ancestor. | |
8134 | ||
8135 | Iface : constant Entity_Id := | |
8136 | Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)); | |
fceeaab6 ES |
8137 | Typ : constant Entity_Id := Find_Dispatching_Type (Prim); |
8138 | ||
25ebc085 AC |
8139 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id; |
8140 | -- Return the controlling formal of Prim | |
8141 | ||
59e6b23c AC |
8142 | ------------------------ |
8143 | -- Controlling_Formal -- | |
8144 | ------------------------ | |
8145 | ||
25ebc085 | 8146 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id is |
15918371 | 8147 | E : Entity_Id; |
59e6b23c | 8148 | |
25ebc085 | 8149 | begin |
15918371 | 8150 | E := First_Entity (Prim); |
25ebc085 AC |
8151 | while Present (E) loop |
8152 | if Is_Formal (E) and then Is_Controlling_Formal (E) then | |
8153 | return E; | |
8154 | end if; | |
8155 | ||
8156 | Next_Entity (E); | |
8157 | end loop; | |
8158 | ||
8159 | return Empty; | |
8160 | end Controlling_Formal; | |
8161 | ||
8162 | -- Local variables | |
8163 | ||
8164 | Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim); | |
8165 | Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim); | |
8166 | ||
8167 | -- Start of processing for Is_Interface_Conformant | |
8168 | ||
ce2b6ba5 JM |
8169 | begin |
8170 | pragma Assert (Is_Subprogram (Iface_Prim) | |
8171 | and then Is_Subprogram (Prim) | |
8172 | and then Is_Dispatching_Operation (Iface_Prim) | |
8173 | and then Is_Dispatching_Operation (Prim)); | |
8174 | ||
fceeaab6 | 8175 | pragma Assert (Is_Interface (Iface) |
ce2b6ba5 JM |
8176 | or else (Present (Alias (Iface_Prim)) |
8177 | and then | |
8178 | Is_Interface | |
8179 | (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim))))); | |
8180 | ||
8181 | if Prim = Iface_Prim | |
8182 | or else not Is_Subprogram (Prim) | |
8183 | or else Ekind (Prim) /= Ekind (Iface_Prim) | |
8184 | or else not Is_Dispatching_Operation (Prim) | |
8185 | or else Scope (Prim) /= Scope (Tagged_Type) | |
fceeaab6 | 8186 | or else No (Typ) |
8a49a499 | 8187 | or else Base_Type (Typ) /= Base_Type (Tagged_Type) |
ce2b6ba5 JM |
8188 | or else not Primitive_Names_Match (Iface_Prim, Prim) |
8189 | then | |
8190 | return False; | |
8191 | ||
25ebc085 AC |
8192 | -- The mode of the controlling formals must match |
8193 | ||
8194 | elsif Present (Iface_Ctrl_F) | |
15918371 AC |
8195 | and then Present (Prim_Ctrl_F) |
8196 | and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F) | |
25ebc085 AC |
8197 | then |
8198 | return False; | |
8199 | ||
8200 | -- Case of a procedure, or a function whose result type matches the | |
8201 | -- result type of the interface primitive, or a function that has no | |
8202 | -- controlling result (I or access I). | |
ce2b6ba5 JM |
8203 | |
8204 | elsif Ekind (Iface_Prim) = E_Procedure | |
8205 | or else Etype (Prim) = Etype (Iface_Prim) | |
fceeaab6 | 8206 | or else not Has_Controlling_Result (Prim) |
ce2b6ba5 | 8207 | then |
b4d7b435 AC |
8208 | return Type_Conformant |
8209 | (Iface_Prim, Prim, Skip_Controlling_Formals => True); | |
ce2b6ba5 | 8210 | |
2995860f AC |
8211 | -- Case of a function returning an interface, or an access to one. Check |
8212 | -- that the return types correspond. | |
ce2b6ba5 | 8213 | |
fceeaab6 ES |
8214 | elsif Implements_Interface (Typ, Iface) then |
8215 | if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type) | |
9a3c9940 RD |
8216 | /= |
8217 | (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type) | |
fceeaab6 ES |
8218 | then |
8219 | return False; | |
fceeaab6 ES |
8220 | else |
8221 | return | |
9e92ad49 | 8222 | Type_Conformant (Prim, Ultimate_Alias (Iface_Prim), |
ce2b6ba5 | 8223 | Skip_Controlling_Formals => True); |
fceeaab6 | 8224 | end if; |
ce2b6ba5 | 8225 | |
fceeaab6 ES |
8226 | else |
8227 | return False; | |
ce2b6ba5 | 8228 | end if; |
ce2b6ba5 JM |
8229 | end Is_Interface_Conformant; |
8230 | ||
996ae0b0 RK |
8231 | --------------------------------- |
8232 | -- Is_Non_Overriding_Operation -- | |
8233 | --------------------------------- | |
8234 | ||
8235 | function Is_Non_Overriding_Operation | |
8236 | (Prev_E : Entity_Id; | |
d05ef0ab | 8237 | New_E : Entity_Id) return Boolean |
996ae0b0 RK |
8238 | is |
8239 | Formal : Entity_Id; | |
8240 | F_Typ : Entity_Id; | |
8241 | G_Typ : Entity_Id := Empty; | |
8242 | ||
8243 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
8244 | -- If F_Type is a derived type associated with a generic actual subtype, |
8245 | -- then return its Generic_Parent_Type attribute, else return Empty. | |
996ae0b0 RK |
8246 | |
8247 | function Types_Correspond | |
8248 | (P_Type : Entity_Id; | |
d05ef0ab | 8249 | N_Type : Entity_Id) return Boolean; |
82c80734 RD |
8250 | -- Returns true if and only if the types (or designated types in the |
8251 | -- case of anonymous access types) are the same or N_Type is derived | |
8252 | -- directly or indirectly from P_Type. | |
996ae0b0 RK |
8253 | |
8254 | ----------------------------- | |
8255 | -- Get_Generic_Parent_Type -- | |
8256 | ----------------------------- | |
8257 | ||
8258 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is | |
8259 | G_Typ : Entity_Id; | |
702d2020 | 8260 | Defn : Node_Id; |
996ae0b0 RK |
8261 | Indic : Node_Id; |
8262 | ||
8263 | begin | |
8264 | if Is_Derived_Type (F_Typ) | |
8265 | and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration | |
8266 | then | |
82c80734 RD |
8267 | -- The tree must be traversed to determine the parent subtype in |
8268 | -- the generic unit, which unfortunately isn't always available | |
8269 | -- via semantic attributes. ??? (Note: The use of Original_Node | |
8270 | -- is needed for cases where a full derived type has been | |
8271 | -- rewritten.) | |
996ae0b0 | 8272 | |
702d2020 AC |
8273 | Defn := Type_Definition (Original_Node (Parent (F_Typ))); |
8274 | if Nkind (Defn) = N_Derived_Type_Definition then | |
8275 | Indic := Subtype_Indication (Defn); | |
996ae0b0 | 8276 | |
702d2020 AC |
8277 | if Nkind (Indic) = N_Subtype_Indication then |
8278 | G_Typ := Entity (Subtype_Mark (Indic)); | |
8279 | else | |
8280 | G_Typ := Entity (Indic); | |
8281 | end if; | |
996ae0b0 | 8282 | |
702d2020 AC |
8283 | if Nkind (Parent (G_Typ)) = N_Subtype_Declaration |
8284 | and then Present (Generic_Parent_Type (Parent (G_Typ))) | |
8285 | then | |
8286 | return Generic_Parent_Type (Parent (G_Typ)); | |
8287 | end if; | |
996ae0b0 RK |
8288 | end if; |
8289 | end if; | |
8290 | ||
8291 | return Empty; | |
8292 | end Get_Generic_Parent_Type; | |
8293 | ||
8294 | ---------------------- | |
8295 | -- Types_Correspond -- | |
8296 | ---------------------- | |
8297 | ||
8298 | function Types_Correspond | |
8299 | (P_Type : Entity_Id; | |
d05ef0ab | 8300 | N_Type : Entity_Id) return Boolean |
996ae0b0 RK |
8301 | is |
8302 | Prev_Type : Entity_Id := Base_Type (P_Type); | |
8303 | New_Type : Entity_Id := Base_Type (N_Type); | |
8304 | ||
8305 | begin | |
8306 | if Ekind (Prev_Type) = E_Anonymous_Access_Type then | |
8307 | Prev_Type := Designated_Type (Prev_Type); | |
8308 | end if; | |
8309 | ||
8310 | if Ekind (New_Type) = E_Anonymous_Access_Type then | |
8311 | New_Type := Designated_Type (New_Type); | |
8312 | end if; | |
8313 | ||
8314 | if Prev_Type = New_Type then | |
8315 | return True; | |
8316 | ||
8317 | elsif not Is_Class_Wide_Type (New_Type) then | |
8318 | while Etype (New_Type) /= New_Type loop | |
8319 | New_Type := Etype (New_Type); | |
8320 | if New_Type = Prev_Type then | |
8321 | return True; | |
8322 | end if; | |
8323 | end loop; | |
8324 | end if; | |
8325 | return False; | |
8326 | end Types_Correspond; | |
8327 | ||
8328 | -- Start of processing for Is_Non_Overriding_Operation | |
8329 | ||
8330 | begin | |
82c80734 RD |
8331 | -- In the case where both operations are implicit derived subprograms |
8332 | -- then neither overrides the other. This can only occur in certain | |
8333 | -- obscure cases (e.g., derivation from homographs created in a generic | |
8334 | -- instantiation). | |
996ae0b0 RK |
8335 | |
8336 | if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then | |
8337 | return True; | |
8338 | ||
8339 | elsif Ekind (Current_Scope) = E_Package | |
8340 | and then Is_Generic_Instance (Current_Scope) | |
8341 | and then In_Private_Part (Current_Scope) | |
8342 | and then Comes_From_Source (New_E) | |
8343 | then | |
702d2020 AC |
8344 | -- We examine the formals and result type of the inherited operation, |
8345 | -- to determine whether their type is derived from (the instance of) | |
8346 | -- a generic type. The first such formal or result type is the one | |
8347 | -- tested. | |
996ae0b0 RK |
8348 | |
8349 | Formal := First_Formal (Prev_E); | |
996ae0b0 RK |
8350 | while Present (Formal) loop |
8351 | F_Typ := Base_Type (Etype (Formal)); | |
8352 | ||
8353 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
8354 | F_Typ := Designated_Type (F_Typ); | |
8355 | end if; | |
8356 | ||
8357 | G_Typ := Get_Generic_Parent_Type (F_Typ); | |
702d2020 | 8358 | exit when Present (G_Typ); |
996ae0b0 RK |
8359 | |
8360 | Next_Formal (Formal); | |
8361 | end loop; | |
8362 | ||
c8ef728f | 8363 | if No (G_Typ) and then Ekind (Prev_E) = E_Function then |
996ae0b0 RK |
8364 | G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E))); |
8365 | end if; | |
8366 | ||
8367 | if No (G_Typ) then | |
8368 | return False; | |
8369 | end if; | |
8370 | ||
8dbd1460 AC |
8371 | -- If the generic type is a private type, then the original operation |
8372 | -- was not overriding in the generic, because there was no primitive | |
8373 | -- operation to override. | |
996ae0b0 RK |
8374 | |
8375 | if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration | |
8376 | and then Nkind (Formal_Type_Definition (Parent (G_Typ))) = | |
8dbd1460 | 8377 | N_Formal_Private_Type_Definition |
996ae0b0 RK |
8378 | then |
8379 | return True; | |
8380 | ||
8381 | -- The generic parent type is the ancestor of a formal derived | |
8382 | -- type declaration. We need to check whether it has a primitive | |
8383 | -- operation that should be overridden by New_E in the generic. | |
8384 | ||
8385 | else | |
8386 | declare | |
8387 | P_Formal : Entity_Id; | |
8388 | N_Formal : Entity_Id; | |
8389 | P_Typ : Entity_Id; | |
8390 | N_Typ : Entity_Id; | |
8391 | P_Prim : Entity_Id; | |
8392 | Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ)); | |
8393 | ||
8394 | begin | |
8395 | while Present (Prim_Elt) loop | |
8396 | P_Prim := Node (Prim_Elt); | |
fbf5a39b | 8397 | |
996ae0b0 RK |
8398 | if Chars (P_Prim) = Chars (New_E) |
8399 | and then Ekind (P_Prim) = Ekind (New_E) | |
8400 | then | |
8401 | P_Formal := First_Formal (P_Prim); | |
8402 | N_Formal := First_Formal (New_E); | |
8403 | while Present (P_Formal) and then Present (N_Formal) loop | |
8404 | P_Typ := Etype (P_Formal); | |
8405 | N_Typ := Etype (N_Formal); | |
8406 | ||
8407 | if not Types_Correspond (P_Typ, N_Typ) then | |
8408 | exit; | |
8409 | end if; | |
8410 | ||
8411 | Next_Entity (P_Formal); | |
8412 | Next_Entity (N_Formal); | |
8413 | end loop; | |
8414 | ||
82c80734 RD |
8415 | -- Found a matching primitive operation belonging to the |
8416 | -- formal ancestor type, so the new subprogram is | |
8417 | -- overriding. | |
996ae0b0 | 8418 | |
c8ef728f ES |
8419 | if No (P_Formal) |
8420 | and then No (N_Formal) | |
996ae0b0 RK |
8421 | and then (Ekind (New_E) /= E_Function |
8422 | or else | |
8fde064e AC |
8423 | Types_Correspond |
8424 | (Etype (P_Prim), Etype (New_E))) | |
996ae0b0 RK |
8425 | then |
8426 | return False; | |
8427 | end if; | |
8428 | end if; | |
8429 | ||
8430 | Next_Elmt (Prim_Elt); | |
8431 | end loop; | |
8432 | ||
2995860f AC |
8433 | -- If no match found, then the new subprogram does not override |
8434 | -- in the generic (nor in the instance). | |
996ae0b0 | 8435 | |
260359e3 AC |
8436 | -- If the type in question is not abstract, and the subprogram |
8437 | -- is, this will be an error if the new operation is in the | |
8438 | -- private part of the instance. Emit a warning now, which will | |
8439 | -- make the subsequent error message easier to understand. | |
8440 | ||
8441 | if not Is_Abstract_Type (F_Typ) | |
8442 | and then Is_Abstract_Subprogram (Prev_E) | |
8443 | and then In_Private_Part (Current_Scope) | |
8444 | then | |
8445 | Error_Msg_Node_2 := F_Typ; | |
8446 | Error_Msg_NE | |
3ccedacc AC |
8447 | ("private operation& in generic unit does not override " |
8448 | & "any primitive operation of& (RM 12.3 (18))??", | |
260359e3 AC |
8449 | New_E, New_E); |
8450 | end if; | |
8451 | ||
996ae0b0 RK |
8452 | return True; |
8453 | end; | |
8454 | end if; | |
8455 | else | |
8456 | return False; | |
8457 | end if; | |
8458 | end Is_Non_Overriding_Operation; | |
8459 | ||
beacce02 AC |
8460 | ------------------------------------- |
8461 | -- List_Inherited_Pre_Post_Aspects -- | |
8462 | ------------------------------------- | |
8463 | ||
8464 | procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is | |
8465 | begin | |
e606088a | 8466 | if Opt.List_Inherited_Aspects |
b9696ffb | 8467 | and then Is_Subprogram_Or_Generic_Subprogram (E) |
beacce02 AC |
8468 | then |
8469 | declare | |
dbfeb4fa | 8470 | Inherited : constant Subprogram_List := Inherited_Subprograms (E); |
beacce02 AC |
8471 | P : Node_Id; |
8472 | ||
8473 | begin | |
8474 | for J in Inherited'Range loop | |
d6095153 | 8475 | P := Pre_Post_Conditions (Contract (Inherited (J))); |
beacce02 AC |
8476 | while Present (P) loop |
8477 | Error_Msg_Sloc := Sloc (P); | |
8478 | ||
8479 | if Class_Present (P) and then not Split_PPC (P) then | |
8480 | if Pragma_Name (P) = Name_Precondition then | |
3ccedacc AC |
8481 | Error_Msg_N ("info: & inherits `Pre''Class` aspect " |
8482 | & "from #?L?", E); | |
beacce02 | 8483 | else |
3ccedacc AC |
8484 | Error_Msg_N ("info: & inherits `Post''Class` aspect " |
8485 | & "from #?L?", E); | |
beacce02 AC |
8486 | end if; |
8487 | end if; | |
8488 | ||
8489 | P := Next_Pragma (P); | |
8490 | end loop; | |
8491 | end loop; | |
8492 | end; | |
8493 | end if; | |
8494 | end List_Inherited_Pre_Post_Aspects; | |
8495 | ||
996ae0b0 RK |
8496 | ------------------------------ |
8497 | -- Make_Inequality_Operator -- | |
8498 | ------------------------------ | |
8499 | ||
8500 | -- S is the defining identifier of an equality operator. We build a | |
8501 | -- subprogram declaration with the right signature. This operation is | |
8502 | -- intrinsic, because it is always expanded as the negation of the | |
8503 | -- call to the equality function. | |
8504 | ||
8505 | procedure Make_Inequality_Operator (S : Entity_Id) is | |
8506 | Loc : constant Source_Ptr := Sloc (S); | |
8507 | Decl : Node_Id; | |
8508 | Formals : List_Id; | |
8509 | Op_Name : Entity_Id; | |
8510 | ||
c8ef728f ES |
8511 | FF : constant Entity_Id := First_Formal (S); |
8512 | NF : constant Entity_Id := Next_Formal (FF); | |
996ae0b0 RK |
8513 | |
8514 | begin | |
c8ef728f | 8515 | -- Check that equality was properly defined, ignore call if not |
996ae0b0 | 8516 | |
c8ef728f | 8517 | if No (NF) then |
996ae0b0 RK |
8518 | return; |
8519 | end if; | |
8520 | ||
c8ef728f ES |
8521 | declare |
8522 | A : constant Entity_Id := | |
8523 | Make_Defining_Identifier (Sloc (FF), | |
8524 | Chars => Chars (FF)); | |
8525 | ||
5d37ba92 ES |
8526 | B : constant Entity_Id := |
8527 | Make_Defining_Identifier (Sloc (NF), | |
8528 | Chars => Chars (NF)); | |
c8ef728f ES |
8529 | |
8530 | begin | |
8531 | Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne); | |
8532 | ||
8533 | Formals := New_List ( | |
8534 | Make_Parameter_Specification (Loc, | |
8535 | Defining_Identifier => A, | |
8536 | Parameter_Type => | |
e4494292 | 8537 | New_Occurrence_Of (Etype (First_Formal (S)), |
c8ef728f ES |
8538 | Sloc (Etype (First_Formal (S))))), |
8539 | ||
8540 | Make_Parameter_Specification (Loc, | |
8541 | Defining_Identifier => B, | |
8542 | Parameter_Type => | |
e4494292 | 8543 | New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))), |
c8ef728f ES |
8544 | Sloc (Etype (Next_Formal (First_Formal (S))))))); |
8545 | ||
8546 | Decl := | |
8547 | Make_Subprogram_Declaration (Loc, | |
8548 | Specification => | |
8549 | Make_Function_Specification (Loc, | |
8550 | Defining_Unit_Name => Op_Name, | |
8551 | Parameter_Specifications => Formals, | |
8552 | Result_Definition => | |
e4494292 | 8553 | New_Occurrence_Of (Standard_Boolean, Loc))); |
c8ef728f ES |
8554 | |
8555 | -- Insert inequality right after equality if it is explicit or after | |
8556 | -- the derived type when implicit. These entities are created only | |
2995860f AC |
8557 | -- for visibility purposes, and eventually replaced in the course |
8558 | -- of expansion, so they do not need to be attached to the tree and | |
8559 | -- seen by the back-end. Keeping them internal also avoids spurious | |
c8ef728f ES |
8560 | -- freezing problems. The declaration is inserted in the tree for |
8561 | -- analysis, and removed afterwards. If the equality operator comes | |
8562 | -- from an explicit declaration, attach the inequality immediately | |
8563 | -- after. Else the equality is inherited from a derived type | |
8564 | -- declaration, so insert inequality after that declaration. | |
8565 | ||
8566 | if No (Alias (S)) then | |
8567 | Insert_After (Unit_Declaration_Node (S), Decl); | |
8568 | elsif Is_List_Member (Parent (S)) then | |
8569 | Insert_After (Parent (S), Decl); | |
8570 | else | |
8571 | Insert_After (Parent (Etype (First_Formal (S))), Decl); | |
8572 | end if; | |
996ae0b0 | 8573 | |
c8ef728f ES |
8574 | Mark_Rewrite_Insertion (Decl); |
8575 | Set_Is_Intrinsic_Subprogram (Op_Name); | |
8576 | Analyze (Decl); | |
8577 | Remove (Decl); | |
8578 | Set_Has_Completion (Op_Name); | |
8579 | Set_Corresponding_Equality (Op_Name, S); | |
f937473f | 8580 | Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S)); |
c8ef728f | 8581 | end; |
996ae0b0 RK |
8582 | end Make_Inequality_Operator; |
8583 | ||
8584 | ---------------------- | |
8585 | -- May_Need_Actuals -- | |
8586 | ---------------------- | |
8587 | ||
8588 | procedure May_Need_Actuals (Fun : Entity_Id) is | |
8589 | F : Entity_Id; | |
8590 | B : Boolean; | |
8591 | ||
8592 | begin | |
8593 | F := First_Formal (Fun); | |
8594 | B := True; | |
996ae0b0 RK |
8595 | while Present (F) loop |
8596 | if No (Default_Value (F)) then | |
8597 | B := False; | |
8598 | exit; | |
8599 | end if; | |
8600 | ||
8601 | Next_Formal (F); | |
8602 | end loop; | |
8603 | ||
8604 | Set_Needs_No_Actuals (Fun, B); | |
8605 | end May_Need_Actuals; | |
8606 | ||
8607 | --------------------- | |
8608 | -- Mode_Conformant -- | |
8609 | --------------------- | |
8610 | ||
8611 | function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
8612 | Result : Boolean; | |
996ae0b0 RK |
8613 | begin |
8614 | Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result); | |
8615 | return Result; | |
8616 | end Mode_Conformant; | |
8617 | ||
8618 | --------------------------- | |
8619 | -- New_Overloaded_Entity -- | |
8620 | --------------------------- | |
8621 | ||
8622 | procedure New_Overloaded_Entity | |
8623 | (S : Entity_Id; | |
8624 | Derived_Type : Entity_Id := Empty) | |
8625 | is | |
ec4867fa | 8626 | Overridden_Subp : Entity_Id := Empty; |
758c442c GD |
8627 | -- Set if the current scope has an operation that is type-conformant |
8628 | -- with S, and becomes hidden by S. | |
8629 | ||
5d37ba92 ES |
8630 | Is_Primitive_Subp : Boolean; |
8631 | -- Set to True if the new subprogram is primitive | |
8632 | ||
fbf5a39b AC |
8633 | E : Entity_Id; |
8634 | -- Entity that S overrides | |
8635 | ||
996ae0b0 | 8636 | Prev_Vis : Entity_Id := Empty; |
ec4867fa ES |
8637 | -- Predecessor of E in Homonym chain |
8638 | ||
5d37ba92 ES |
8639 | procedure Check_For_Primitive_Subprogram |
8640 | (Is_Primitive : out Boolean; | |
8641 | Is_Overriding : Boolean := False); | |
8642 | -- If the subprogram being analyzed is a primitive operation of the type | |
8643 | -- of a formal or result, set the Has_Primitive_Operations flag on the | |
8644 | -- type, and set Is_Primitive to True (otherwise set to False). Set the | |
8645 | -- corresponding flag on the entity itself for later use. | |
8646 | ||
ec4867fa ES |
8647 | procedure Check_Synchronized_Overriding |
8648 | (Def_Id : Entity_Id; | |
ec4867fa ES |
8649 | Overridden_Subp : out Entity_Id); |
8650 | -- First determine if Def_Id is an entry or a subprogram either defined | |
8651 | -- in the scope of a task or protected type, or is a primitive of such | |
8652 | -- a type. Check whether Def_Id overrides a subprogram of an interface | |
8653 | -- implemented by the synchronized type, return the overridden entity | |
8654 | -- or Empty. | |
758c442c | 8655 | |
996ae0b0 RK |
8656 | function Is_Private_Declaration (E : Entity_Id) return Boolean; |
8657 | -- Check that E is declared in the private part of the current package, | |
8658 | -- or in the package body, where it may hide a previous declaration. | |
fbf5a39b | 8659 | -- We can't use In_Private_Part by itself because this flag is also |
996ae0b0 RK |
8660 | -- set when freezing entities, so we must examine the place of the |
8661 | -- declaration in the tree, and recognize wrapper packages as well. | |
8662 | ||
2ddc2000 AC |
8663 | function Is_Overriding_Alias |
8664 | (Old_E : Entity_Id; | |
8665 | New_E : Entity_Id) return Boolean; | |
8666 | -- Check whether new subprogram and old subprogram are both inherited | |
8667 | -- from subprograms that have distinct dispatch table entries. This can | |
2995860f AC |
8668 | -- occur with derivations from instances with accidental homonyms. The |
8669 | -- function is conservative given that the converse is only true within | |
8670 | -- instances that contain accidental overloadings. | |
2ddc2000 | 8671 | |
5d37ba92 ES |
8672 | ------------------------------------ |
8673 | -- Check_For_Primitive_Subprogram -- | |
8674 | ------------------------------------ | |
996ae0b0 | 8675 | |
5d37ba92 ES |
8676 | procedure Check_For_Primitive_Subprogram |
8677 | (Is_Primitive : out Boolean; | |
8678 | Is_Overriding : Boolean := False) | |
ec4867fa | 8679 | is |
996ae0b0 RK |
8680 | Formal : Entity_Id; |
8681 | F_Typ : Entity_Id; | |
07fc65c4 | 8682 | B_Typ : Entity_Id; |
996ae0b0 RK |
8683 | |
8684 | function Visible_Part_Type (T : Entity_Id) return Boolean; | |
8dbd1460 AC |
8685 | -- Returns true if T is declared in the visible part of the current |
8686 | -- package scope; otherwise returns false. Assumes that T is declared | |
8687 | -- in a package. | |
996ae0b0 RK |
8688 | |
8689 | procedure Check_Private_Overriding (T : Entity_Id); | |
8690 | -- Checks that if a primitive abstract subprogram of a visible | |
8dbd1460 AC |
8691 | -- abstract type is declared in a private part, then it must override |
8692 | -- an abstract subprogram declared in the visible part. Also checks | |
8693 | -- that if a primitive function with a controlling result is declared | |
8694 | -- in a private part, then it must override a function declared in | |
8695 | -- the visible part. | |
996ae0b0 RK |
8696 | |
8697 | ------------------------------ | |
8698 | -- Check_Private_Overriding -- | |
8699 | ------------------------------ | |
8700 | ||
8701 | procedure Check_Private_Overriding (T : Entity_Id) is | |
8702 | begin | |
51c16e29 | 8703 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
8704 | and then In_Private_Part (Current_Scope) |
8705 | and then Visible_Part_Type (T) | |
8706 | and then not In_Instance | |
8707 | then | |
f937473f RD |
8708 | if Is_Abstract_Type (T) |
8709 | and then Is_Abstract_Subprogram (S) | |
8710 | and then (not Is_Overriding | |
8dbd1460 | 8711 | or else not Is_Abstract_Subprogram (E)) |
996ae0b0 | 8712 | then |
3ccedacc AC |
8713 | Error_Msg_N ("abstract subprograms must be visible " |
8714 | & "(RM 3.9.3(10))!", S); | |
758c442c | 8715 | |
8fde064e AC |
8716 | elsif Ekind (S) = E_Function and then not Is_Overriding then |
8717 | if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then | |
3ccedacc AC |
8718 | Error_Msg_N ("private function with tagged result must" |
8719 | & " override visible-part function", S); | |
8720 | Error_Msg_N ("\move subprogram to the visible part" | |
8721 | & " (RM 3.9.3(10))", S); | |
2e79de51 AC |
8722 | |
8723 | -- AI05-0073: extend this test to the case of a function | |
8724 | -- with a controlling access result. | |
8725 | ||
8726 | elsif Ekind (Etype (S)) = E_Anonymous_Access_Type | |
8727 | and then Is_Tagged_Type (Designated_Type (Etype (S))) | |
8728 | and then | |
8729 | not Is_Class_Wide_Type (Designated_Type (Etype (S))) | |
dbe945f1 | 8730 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
8731 | then |
8732 | Error_Msg_N | |
8733 | ("private function with controlling access result " | |
3ccedacc | 8734 | & "must override visible-part function", S); |
2e79de51 AC |
8735 | Error_Msg_N |
8736 | ("\move subprogram to the visible part" | |
3ccedacc | 8737 | & " (RM 3.9.3(10))", S); |
2e79de51 | 8738 | end if; |
996ae0b0 RK |
8739 | end if; |
8740 | end if; | |
8741 | end Check_Private_Overriding; | |
8742 | ||
8743 | ----------------------- | |
8744 | -- Visible_Part_Type -- | |
8745 | ----------------------- | |
8746 | ||
8747 | function Visible_Part_Type (T : Entity_Id) return Boolean is | |
07fc65c4 GB |
8748 | P : constant Node_Id := Unit_Declaration_Node (Scope (T)); |
8749 | N : Node_Id; | |
996ae0b0 RK |
8750 | |
8751 | begin | |
8dbd1460 AC |
8752 | -- If the entity is a private type, then it must be declared in a |
8753 | -- visible part. | |
996ae0b0 RK |
8754 | |
8755 | if Ekind (T) in Private_Kind then | |
8756 | return True; | |
8757 | end if; | |
8758 | ||
8759 | -- Otherwise, we traverse the visible part looking for its | |
8760 | -- corresponding declaration. We cannot use the declaration | |
8761 | -- node directly because in the private part the entity of a | |
8762 | -- private type is the one in the full view, which does not | |
8763 | -- indicate that it is the completion of something visible. | |
8764 | ||
07fc65c4 | 8765 | N := First (Visible_Declarations (Specification (P))); |
996ae0b0 RK |
8766 | while Present (N) loop |
8767 | if Nkind (N) = N_Full_Type_Declaration | |
8768 | and then Present (Defining_Identifier (N)) | |
8769 | and then T = Defining_Identifier (N) | |
8770 | then | |
8771 | return True; | |
8772 | ||
800621e0 RD |
8773 | elsif Nkind_In (N, N_Private_Type_Declaration, |
8774 | N_Private_Extension_Declaration) | |
996ae0b0 RK |
8775 | and then Present (Defining_Identifier (N)) |
8776 | and then T = Full_View (Defining_Identifier (N)) | |
8777 | then | |
8778 | return True; | |
8779 | end if; | |
8780 | ||
8781 | Next (N); | |
8782 | end loop; | |
8783 | ||
8784 | return False; | |
8785 | end Visible_Part_Type; | |
8786 | ||
5d37ba92 | 8787 | -- Start of processing for Check_For_Primitive_Subprogram |
996ae0b0 RK |
8788 | |
8789 | begin | |
5d37ba92 ES |
8790 | Is_Primitive := False; |
8791 | ||
996ae0b0 RK |
8792 | if not Comes_From_Source (S) then |
8793 | null; | |
8794 | ||
5d37ba92 | 8795 | -- If subprogram is at library level, it is not primitive operation |
15ce9ca2 AC |
8796 | |
8797 | elsif Current_Scope = Standard_Standard then | |
8798 | null; | |
8799 | ||
b9b2405f | 8800 | elsif (Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 | 8801 | and then not In_Package_Body (Current_Scope)) |
82c80734 | 8802 | or else Is_Overriding |
996ae0b0 | 8803 | then |
07fc65c4 | 8804 | -- For function, check return type |
996ae0b0 | 8805 | |
07fc65c4 | 8806 | if Ekind (S) = E_Function then |
5d37ba92 ES |
8807 | if Ekind (Etype (S)) = E_Anonymous_Access_Type then |
8808 | F_Typ := Designated_Type (Etype (S)); | |
8809 | else | |
8810 | F_Typ := Etype (S); | |
8811 | end if; | |
8812 | ||
8813 | B_Typ := Base_Type (F_Typ); | |
07fc65c4 | 8814 | |
5d37ba92 ES |
8815 | if Scope (B_Typ) = Current_Scope |
8816 | and then not Is_Class_Wide_Type (B_Typ) | |
8817 | and then not Is_Generic_Type (B_Typ) | |
8818 | then | |
8819 | Is_Primitive := True; | |
07fc65c4 | 8820 | Set_Has_Primitive_Operations (B_Typ); |
5d37ba92 | 8821 | Set_Is_Primitive (S); |
07fc65c4 GB |
8822 | Check_Private_Overriding (B_Typ); |
8823 | end if; | |
996ae0b0 RK |
8824 | end if; |
8825 | ||
07fc65c4 | 8826 | -- For all subprograms, check formals |
996ae0b0 | 8827 | |
07fc65c4 | 8828 | Formal := First_Formal (S); |
996ae0b0 RK |
8829 | while Present (Formal) loop |
8830 | if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then | |
8831 | F_Typ := Designated_Type (Etype (Formal)); | |
8832 | else | |
8833 | F_Typ := Etype (Formal); | |
8834 | end if; | |
8835 | ||
07fc65c4 GB |
8836 | B_Typ := Base_Type (F_Typ); |
8837 | ||
ec4867fa ES |
8838 | if Ekind (B_Typ) = E_Access_Subtype then |
8839 | B_Typ := Base_Type (B_Typ); | |
8840 | end if; | |
8841 | ||
5d37ba92 ES |
8842 | if Scope (B_Typ) = Current_Scope |
8843 | and then not Is_Class_Wide_Type (B_Typ) | |
8844 | and then not Is_Generic_Type (B_Typ) | |
8845 | then | |
8846 | Is_Primitive := True; | |
8847 | Set_Is_Primitive (S); | |
07fc65c4 GB |
8848 | Set_Has_Primitive_Operations (B_Typ); |
8849 | Check_Private_Overriding (B_Typ); | |
996ae0b0 RK |
8850 | end if; |
8851 | ||
8852 | Next_Formal (Formal); | |
8853 | end loop; | |
1aee1fb3 AC |
8854 | |
8855 | -- Special case: An equality function can be redefined for a type | |
8856 | -- occurring in a declarative part, and won't otherwise be treated as | |
8857 | -- a primitive because it doesn't occur in a package spec and doesn't | |
8858 | -- override an inherited subprogram. It's important that we mark it | |
8859 | -- primitive so it can be returned by Collect_Primitive_Operations | |
8860 | -- and be used in composing the equality operation of later types | |
8861 | -- that have a component of the type. | |
8862 | ||
8863 | elsif Chars (S) = Name_Op_Eq | |
8864 | and then Etype (S) = Standard_Boolean | |
8865 | then | |
8866 | B_Typ := Base_Type (Etype (First_Formal (S))); | |
8867 | ||
8868 | if Scope (B_Typ) = Current_Scope | |
8869 | and then | |
8870 | Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ | |
8871 | and then not Is_Limited_Type (B_Typ) | |
8872 | then | |
8873 | Is_Primitive := True; | |
8874 | Set_Is_Primitive (S); | |
8875 | Set_Has_Primitive_Operations (B_Typ); | |
8876 | Check_Private_Overriding (B_Typ); | |
8877 | end if; | |
996ae0b0 | 8878 | end if; |
5d37ba92 ES |
8879 | end Check_For_Primitive_Subprogram; |
8880 | ||
8881 | ----------------------------------- | |
8882 | -- Check_Synchronized_Overriding -- | |
8883 | ----------------------------------- | |
8884 | ||
8885 | procedure Check_Synchronized_Overriding | |
8886 | (Def_Id : Entity_Id; | |
5d37ba92 ES |
8887 | Overridden_Subp : out Entity_Id) |
8888 | is | |
5d37ba92 ES |
8889 | Ifaces_List : Elist_Id; |
8890 | In_Scope : Boolean; | |
8891 | Typ : Entity_Id; | |
8892 | ||
8aa15e3b JM |
8893 | function Matches_Prefixed_View_Profile |
8894 | (Prim_Params : List_Id; | |
8895 | Iface_Params : List_Id) return Boolean; | |
8896 | -- Determine whether a subprogram's parameter profile Prim_Params | |
8897 | -- matches that of a potentially overridden interface subprogram | |
8898 | -- Iface_Params. Also determine if the type of first parameter of | |
8899 | -- Iface_Params is an implemented interface. | |
8900 | ||
8aa15e3b JM |
8901 | ----------------------------------- |
8902 | -- Matches_Prefixed_View_Profile -- | |
8903 | ----------------------------------- | |
8904 | ||
8905 | function Matches_Prefixed_View_Profile | |
8906 | (Prim_Params : List_Id; | |
8907 | Iface_Params : List_Id) return Boolean | |
8908 | is | |
8909 | Iface_Id : Entity_Id; | |
8910 | Iface_Param : Node_Id; | |
8911 | Iface_Typ : Entity_Id; | |
8912 | Prim_Id : Entity_Id; | |
8913 | Prim_Param : Node_Id; | |
8914 | Prim_Typ : Entity_Id; | |
8915 | ||
8916 | function Is_Implemented | |
8917 | (Ifaces_List : Elist_Id; | |
8918 | Iface : Entity_Id) return Boolean; | |
8919 | -- Determine if Iface is implemented by the current task or | |
8920 | -- protected type. | |
8921 | ||
8922 | -------------------- | |
8923 | -- Is_Implemented -- | |
8924 | -------------------- | |
8925 | ||
8926 | function Is_Implemented | |
8927 | (Ifaces_List : Elist_Id; | |
8928 | Iface : Entity_Id) return Boolean | |
8929 | is | |
8930 | Iface_Elmt : Elmt_Id; | |
8931 | ||
8932 | begin | |
8933 | Iface_Elmt := First_Elmt (Ifaces_List); | |
8934 | while Present (Iface_Elmt) loop | |
8935 | if Node (Iface_Elmt) = Iface then | |
8936 | return True; | |
8937 | end if; | |
8938 | ||
8939 | Next_Elmt (Iface_Elmt); | |
8940 | end loop; | |
8941 | ||
8942 | return False; | |
8943 | end Is_Implemented; | |
8944 | ||
8945 | -- Start of processing for Matches_Prefixed_View_Profile | |
8946 | ||
8947 | begin | |
8948 | Iface_Param := First (Iface_Params); | |
8949 | Iface_Typ := Etype (Defining_Identifier (Iface_Param)); | |
8950 | ||
8951 | if Is_Access_Type (Iface_Typ) then | |
8952 | Iface_Typ := Designated_Type (Iface_Typ); | |
8953 | end if; | |
8954 | ||
8955 | Prim_Param := First (Prim_Params); | |
8956 | ||
8957 | -- The first parameter of the potentially overridden subprogram | |
8958 | -- must be an interface implemented by Prim. | |
8959 | ||
8960 | if not Is_Interface (Iface_Typ) | |
8961 | or else not Is_Implemented (Ifaces_List, Iface_Typ) | |
8962 | then | |
8963 | return False; | |
8964 | end if; | |
8965 | ||
8966 | -- The checks on the object parameters are done, move onto the | |
8967 | -- rest of the parameters. | |
8968 | ||
8969 | if not In_Scope then | |
8970 | Prim_Param := Next (Prim_Param); | |
8971 | end if; | |
8972 | ||
8973 | Iface_Param := Next (Iface_Param); | |
8974 | while Present (Iface_Param) and then Present (Prim_Param) loop | |
8975 | Iface_Id := Defining_Identifier (Iface_Param); | |
8976 | Iface_Typ := Find_Parameter_Type (Iface_Param); | |
8977 | ||
8aa15e3b JM |
8978 | Prim_Id := Defining_Identifier (Prim_Param); |
8979 | Prim_Typ := Find_Parameter_Type (Prim_Param); | |
8980 | ||
15e4986c JM |
8981 | if Ekind (Iface_Typ) = E_Anonymous_Access_Type |
8982 | and then Ekind (Prim_Typ) = E_Anonymous_Access_Type | |
8983 | and then Is_Concurrent_Type (Designated_Type (Prim_Typ)) | |
8984 | then | |
8985 | Iface_Typ := Designated_Type (Iface_Typ); | |
8986 | Prim_Typ := Designated_Type (Prim_Typ); | |
8aa15e3b JM |
8987 | end if; |
8988 | ||
8989 | -- Case of multiple interface types inside a parameter profile | |
8990 | ||
8991 | -- (Obj_Param : in out Iface; ...; Param : Iface) | |
8992 | ||
8993 | -- If the interface type is implemented, then the matching type | |
8994 | -- in the primitive should be the implementing record type. | |
8995 | ||
8996 | if Ekind (Iface_Typ) = E_Record_Type | |
8997 | and then Is_Interface (Iface_Typ) | |
8998 | and then Is_Implemented (Ifaces_List, Iface_Typ) | |
8999 | then | |
9000 | if Prim_Typ /= Typ then | |
9001 | return False; | |
9002 | end if; | |
9003 | ||
9004 | -- The two parameters must be both mode and subtype conformant | |
9005 | ||
9006 | elsif Ekind (Iface_Id) /= Ekind (Prim_Id) | |
9007 | or else not | |
9008 | Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant) | |
9009 | then | |
9010 | return False; | |
9011 | end if; | |
9012 | ||
9013 | Next (Iface_Param); | |
9014 | Next (Prim_Param); | |
9015 | end loop; | |
9016 | ||
9017 | -- One of the two lists contains more parameters than the other | |
9018 | ||
9019 | if Present (Iface_Param) or else Present (Prim_Param) then | |
9020 | return False; | |
9021 | end if; | |
9022 | ||
9023 | return True; | |
9024 | end Matches_Prefixed_View_Profile; | |
9025 | ||
9026 | -- Start of processing for Check_Synchronized_Overriding | |
9027 | ||
5d37ba92 ES |
9028 | begin |
9029 | Overridden_Subp := Empty; | |
9030 | ||
8aa15e3b JM |
9031 | -- Def_Id must be an entry or a subprogram. We should skip predefined |
9032 | -- primitives internally generated by the frontend; however at this | |
9033 | -- stage predefined primitives are still not fully decorated. As a | |
9034 | -- minor optimization we skip here internally generated subprograms. | |
5d37ba92 | 9035 | |
8aa15e3b JM |
9036 | if (Ekind (Def_Id) /= E_Entry |
9037 | and then Ekind (Def_Id) /= E_Function | |
9038 | and then Ekind (Def_Id) /= E_Procedure) | |
9039 | or else not Comes_From_Source (Def_Id) | |
5d37ba92 ES |
9040 | then |
9041 | return; | |
9042 | end if; | |
9043 | ||
9044 | -- Search for the concurrent declaration since it contains the list | |
9045 | -- of all implemented interfaces. In this case, the subprogram is | |
9046 | -- declared within the scope of a protected or a task type. | |
9047 | ||
9048 | if Present (Scope (Def_Id)) | |
9049 | and then Is_Concurrent_Type (Scope (Def_Id)) | |
9050 | and then not Is_Generic_Actual_Type (Scope (Def_Id)) | |
9051 | then | |
9052 | Typ := Scope (Def_Id); | |
9053 | In_Scope := True; | |
9054 | ||
8aa15e3b | 9055 | -- The enclosing scope is not a synchronized type and the subprogram |
4adf3c50 | 9056 | -- has no formals. |
8aa15e3b JM |
9057 | |
9058 | elsif No (First_Formal (Def_Id)) then | |
9059 | return; | |
5d37ba92 | 9060 | |
8aa15e3b | 9061 | -- The subprogram has formals and hence it may be a primitive of a |
4adf3c50 | 9062 | -- concurrent type. |
5d37ba92 | 9063 | |
8aa15e3b JM |
9064 | else |
9065 | Typ := Etype (First_Formal (Def_Id)); | |
9066 | ||
9067 | if Is_Access_Type (Typ) then | |
9068 | Typ := Directly_Designated_Type (Typ); | |
8c3dd7a8 JM |
9069 | end if; |
9070 | ||
8aa15e3b JM |
9071 | if Is_Concurrent_Type (Typ) |
9072 | and then not Is_Generic_Actual_Type (Typ) | |
5d37ba92 | 9073 | then |
5d37ba92 ES |
9074 | In_Scope := False; |
9075 | ||
9076 | -- This case occurs when the concurrent type is declared within | |
9077 | -- a generic unit. As a result the corresponding record has been | |
9078 | -- built and used as the type of the first formal, we just have | |
9079 | -- to retrieve the corresponding concurrent type. | |
9080 | ||
8aa15e3b | 9081 | elsif Is_Concurrent_Record_Type (Typ) |
dd54644b | 9082 | and then not Is_Class_Wide_Type (Typ) |
8aa15e3b | 9083 | and then Present (Corresponding_Concurrent_Type (Typ)) |
5d37ba92 | 9084 | then |
8aa15e3b | 9085 | Typ := Corresponding_Concurrent_Type (Typ); |
5d37ba92 ES |
9086 | In_Scope := False; |
9087 | ||
9088 | else | |
9089 | return; | |
9090 | end if; | |
8aa15e3b JM |
9091 | end if; |
9092 | ||
9093 | -- There is no overriding to check if is an inherited operation in a | |
9094 | -- type derivation on for a generic actual. | |
9095 | ||
9096 | Collect_Interfaces (Typ, Ifaces_List); | |
9097 | ||
9098 | if Is_Empty_Elmt_List (Ifaces_List) then | |
5d37ba92 ES |
9099 | return; |
9100 | end if; | |
9101 | ||
8aa15e3b JM |
9102 | -- Determine whether entry or subprogram Def_Id overrides a primitive |
9103 | -- operation that belongs to one of the interfaces in Ifaces_List. | |
5d37ba92 | 9104 | |
8aa15e3b JM |
9105 | declare |
9106 | Candidate : Entity_Id := Empty; | |
9107 | Hom : Entity_Id := Empty; | |
9108 | Iface_Typ : Entity_Id; | |
9109 | Subp : Entity_Id := Empty; | |
9110 | ||
9111 | begin | |
4adf3c50 | 9112 | -- Traverse the homonym chain, looking for a potentially |
8aa15e3b JM |
9113 | -- overridden subprogram that belongs to an implemented |
9114 | -- interface. | |
9115 | ||
9116 | Hom := Current_Entity_In_Scope (Def_Id); | |
9117 | while Present (Hom) loop | |
9118 | Subp := Hom; | |
9119 | ||
15e4986c JM |
9120 | if Subp = Def_Id |
9121 | or else not Is_Overloadable (Subp) | |
9122 | or else not Is_Primitive (Subp) | |
9123 | or else not Is_Dispatching_Operation (Subp) | |
79afa047 | 9124 | or else not Present (Find_Dispatching_Type (Subp)) |
15e4986c | 9125 | or else not Is_Interface (Find_Dispatching_Type (Subp)) |
8aa15e3b | 9126 | then |
15e4986c | 9127 | null; |
8aa15e3b | 9128 | |
15e4986c | 9129 | -- Entries and procedures can override abstract or null |
4adf3c50 | 9130 | -- interface procedures. |
8aa15e3b | 9131 | |
15e4986c | 9132 | elsif (Ekind (Def_Id) = E_Procedure |
8fde064e | 9133 | or else Ekind (Def_Id) = E_Entry) |
8aa15e3b | 9134 | and then Ekind (Subp) = E_Procedure |
8aa15e3b JM |
9135 | and then Matches_Prefixed_View_Profile |
9136 | (Parameter_Specifications (Parent (Def_Id)), | |
9137 | Parameter_Specifications (Parent (Subp))) | |
9138 | then | |
9139 | Candidate := Subp; | |
9140 | ||
15e4986c JM |
9141 | -- For an overridden subprogram Subp, check whether the mode |
9142 | -- of its first parameter is correct depending on the kind | |
9143 | -- of synchronized type. | |
8aa15e3b | 9144 | |
15e4986c JM |
9145 | declare |
9146 | Formal : constant Node_Id := First_Formal (Candidate); | |
9147 | ||
9148 | begin | |
9149 | -- In order for an entry or a protected procedure to | |
9150 | -- override, the first parameter of the overridden | |
9151 | -- routine must be of mode "out", "in out" or | |
9152 | -- access-to-variable. | |
9153 | ||
8fde064e | 9154 | if Ekind_In (Candidate, E_Entry, E_Procedure) |
15e4986c JM |
9155 | and then Is_Protected_Type (Typ) |
9156 | and then Ekind (Formal) /= E_In_Out_Parameter | |
9157 | and then Ekind (Formal) /= E_Out_Parameter | |
8fde064e AC |
9158 | and then Nkind (Parameter_Type (Parent (Formal))) /= |
9159 | N_Access_Definition | |
15e4986c JM |
9160 | then |
9161 | null; | |
9162 | ||
9163 | -- All other cases are OK since a task entry or routine | |
9164 | -- does not have a restriction on the mode of the first | |
9165 | -- parameter of the overridden interface routine. | |
9166 | ||
9167 | else | |
9168 | Overridden_Subp := Candidate; | |
9169 | return; | |
9170 | end if; | |
9171 | end; | |
8aa15e3b JM |
9172 | |
9173 | -- Functions can override abstract interface functions | |
9174 | ||
9175 | elsif Ekind (Def_Id) = E_Function | |
9176 | and then Ekind (Subp) = E_Function | |
8aa15e3b JM |
9177 | and then Matches_Prefixed_View_Profile |
9178 | (Parameter_Specifications (Parent (Def_Id)), | |
9179 | Parameter_Specifications (Parent (Subp))) | |
9180 | and then Etype (Result_Definition (Parent (Def_Id))) = | |
9181 | Etype (Result_Definition (Parent (Subp))) | |
9182 | then | |
9183 | Overridden_Subp := Subp; | |
9184 | return; | |
9185 | end if; | |
9186 | ||
9187 | Hom := Homonym (Hom); | |
9188 | end loop; | |
9189 | ||
4adf3c50 AC |
9190 | -- After examining all candidates for overriding, we are left with |
9191 | -- the best match which is a mode incompatible interface routine. | |
9192 | -- Do not emit an error if the Expander is active since this error | |
9193 | -- will be detected later on after all concurrent types are | |
9194 | -- expanded and all wrappers are built. This check is meant for | |
9195 | -- spec-only compilations. | |
8aa15e3b | 9196 | |
4adf3c50 | 9197 | if Present (Candidate) and then not Expander_Active then |
8aa15e3b JM |
9198 | Iface_Typ := |
9199 | Find_Parameter_Type (Parent (First_Formal (Candidate))); | |
9200 | ||
4adf3c50 AC |
9201 | -- Def_Id is primitive of a protected type, declared inside the |
9202 | -- type, and the candidate is primitive of a limited or | |
9203 | -- synchronized interface. | |
8aa15e3b JM |
9204 | |
9205 | if In_Scope | |
9206 | and then Is_Protected_Type (Typ) | |
9207 | and then | |
9208 | (Is_Limited_Interface (Iface_Typ) | |
c199ccf7 AC |
9209 | or else Is_Protected_Interface (Iface_Typ) |
9210 | or else Is_Synchronized_Interface (Iface_Typ) | |
9211 | or else Is_Task_Interface (Iface_Typ)) | |
8aa15e3b | 9212 | then |
dd54644b | 9213 | Error_Msg_PT (Parent (Typ), Candidate); |
8aa15e3b | 9214 | end if; |
5d37ba92 | 9215 | end if; |
8aa15e3b JM |
9216 | |
9217 | Overridden_Subp := Candidate; | |
9218 | return; | |
9219 | end; | |
5d37ba92 ES |
9220 | end Check_Synchronized_Overriding; |
9221 | ||
9222 | ---------------------------- | |
9223 | -- Is_Private_Declaration -- | |
9224 | ---------------------------- | |
9225 | ||
9226 | function Is_Private_Declaration (E : Entity_Id) return Boolean is | |
9227 | Priv_Decls : List_Id; | |
9228 | Decl : constant Node_Id := Unit_Declaration_Node (E); | |
9229 | ||
9230 | begin | |
9231 | if Is_Package_Or_Generic_Package (Current_Scope) | |
9232 | and then In_Private_Part (Current_Scope) | |
9233 | then | |
9234 | Priv_Decls := | |
d12b19fa | 9235 | Private_Declarations (Package_Specification (Current_Scope)); |
5d37ba92 ES |
9236 | |
9237 | return In_Package_Body (Current_Scope) | |
9238 | or else | |
9239 | (Is_List_Member (Decl) | |
a4901c08 | 9240 | and then List_Containing (Decl) = Priv_Decls) |
5d37ba92 | 9241 | or else (Nkind (Parent (Decl)) = N_Package_Specification |
a4901c08 AC |
9242 | and then not |
9243 | Is_Compilation_Unit | |
9244 | (Defining_Entity (Parent (Decl))) | |
9245 | and then List_Containing (Parent (Parent (Decl))) = | |
9246 | Priv_Decls); | |
5d37ba92 ES |
9247 | else |
9248 | return False; | |
9249 | end if; | |
9250 | end Is_Private_Declaration; | |
996ae0b0 | 9251 | |
2ddc2000 AC |
9252 | -------------------------- |
9253 | -- Is_Overriding_Alias -- | |
9254 | -------------------------- | |
9255 | ||
9256 | function Is_Overriding_Alias | |
9257 | (Old_E : Entity_Id; | |
9258 | New_E : Entity_Id) return Boolean | |
9259 | is | |
9260 | AO : constant Entity_Id := Alias (Old_E); | |
9261 | AN : constant Entity_Id := Alias (New_E); | |
2ddc2000 AC |
9262 | begin |
9263 | return Scope (AO) /= Scope (AN) | |
9264 | or else No (DTC_Entity (AO)) | |
9265 | or else No (DTC_Entity (AN)) | |
9266 | or else DT_Position (AO) = DT_Position (AN); | |
9267 | end Is_Overriding_Alias; | |
9268 | ||
996ae0b0 RK |
9269 | -- Start of processing for New_Overloaded_Entity |
9270 | ||
9271 | begin | |
fbf5a39b AC |
9272 | -- We need to look for an entity that S may override. This must be a |
9273 | -- homonym in the current scope, so we look for the first homonym of | |
9274 | -- S in the current scope as the starting point for the search. | |
9275 | ||
9276 | E := Current_Entity_In_Scope (S); | |
9277 | ||
947430d5 AC |
9278 | -- Ada 2005 (AI-251): Derivation of abstract interface primitives. |
9279 | -- They are directly added to the list of primitive operations of | |
9280 | -- Derived_Type, unless this is a rederivation in the private part | |
9281 | -- of an operation that was already derived in the visible part of | |
9282 | -- the current package. | |
9283 | ||
0791fbe9 | 9284 | if Ada_Version >= Ada_2005 |
947430d5 AC |
9285 | and then Present (Derived_Type) |
9286 | and then Present (Alias (S)) | |
9287 | and then Is_Dispatching_Operation (Alias (S)) | |
9288 | and then Present (Find_Dispatching_Type (Alias (S))) | |
9289 | and then Is_Interface (Find_Dispatching_Type (Alias (S))) | |
9290 | then | |
9291 | -- For private types, when the full-view is processed we propagate to | |
9292 | -- the full view the non-overridden entities whose attribute "alias" | |
9293 | -- references an interface primitive. These entities were added by | |
9294 | -- Derive_Subprograms to ensure that interface primitives are | |
9295 | -- covered. | |
9296 | ||
9297 | -- Inside_Freeze_Actions is non zero when S corresponds with an | |
9298 | -- internal entity that links an interface primitive with its | |
9299 | -- covering primitive through attribute Interface_Alias (see | |
4adf3c50 | 9300 | -- Add_Internal_Interface_Entities). |
947430d5 AC |
9301 | |
9302 | if Inside_Freezing_Actions = 0 | |
9303 | and then Is_Package_Or_Generic_Package (Current_Scope) | |
9304 | and then In_Private_Part (Current_Scope) | |
9305 | and then Nkind (Parent (E)) = N_Private_Extension_Declaration | |
9306 | and then Nkind (Parent (S)) = N_Full_Type_Declaration | |
9307 | and then Full_View (Defining_Identifier (Parent (E))) | |
9308 | = Defining_Identifier (Parent (S)) | |
9309 | and then Alias (E) = Alias (S) | |
9310 | then | |
9311 | Check_Operation_From_Private_View (S, E); | |
9312 | Set_Is_Dispatching_Operation (S); | |
9313 | ||
9314 | -- Common case | |
9315 | ||
9316 | else | |
9317 | Enter_Overloaded_Entity (S); | |
9318 | Check_Dispatching_Operation (S, Empty); | |
9319 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); | |
9320 | end if; | |
9321 | ||
9322 | return; | |
9323 | end if; | |
9324 | ||
fbf5a39b AC |
9325 | -- If there is no homonym then this is definitely not overriding |
9326 | ||
996ae0b0 RK |
9327 | if No (E) then |
9328 | Enter_Overloaded_Entity (S); | |
9329 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 9330 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
996ae0b0 | 9331 | |
2995860f AC |
9332 | -- If subprogram has an explicit declaration, check whether it has an |
9333 | -- overriding indicator. | |
758c442c | 9334 | |
ec4867fa | 9335 | if Comes_From_Source (S) then |
8aa15e3b | 9336 | Check_Synchronized_Overriding (S, Overridden_Subp); |
ea034236 AC |
9337 | |
9338 | -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then | |
9339 | -- it may have overridden some hidden inherited primitive. Update | |
308e6f3a | 9340 | -- Overridden_Subp to avoid spurious errors when checking the |
ea034236 AC |
9341 | -- overriding indicator. |
9342 | ||
9343 | if Ada_Version >= Ada_2012 | |
9344 | and then No (Overridden_Subp) | |
9345 | and then Is_Dispatching_Operation (S) | |
038140ed | 9346 | and then Present (Overridden_Operation (S)) |
ea034236 AC |
9347 | then |
9348 | Overridden_Subp := Overridden_Operation (S); | |
9349 | end if; | |
9350 | ||
5d37ba92 ES |
9351 | Check_Overriding_Indicator |
9352 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
758c442c GD |
9353 | end if; |
9354 | ||
fbf5a39b AC |
9355 | -- If there is a homonym that is not overloadable, then we have an |
9356 | -- error, except for the special cases checked explicitly below. | |
9357 | ||
996ae0b0 RK |
9358 | elsif not Is_Overloadable (E) then |
9359 | ||
9360 | -- Check for spurious conflict produced by a subprogram that has the | |
9361 | -- same name as that of the enclosing generic package. The conflict | |
9362 | -- occurs within an instance, between the subprogram and the renaming | |
9363 | -- declaration for the package. After the subprogram, the package | |
9364 | -- renaming declaration becomes hidden. | |
9365 | ||
9366 | if Ekind (E) = E_Package | |
9367 | and then Present (Renamed_Object (E)) | |
9368 | and then Renamed_Object (E) = Current_Scope | |
9369 | and then Nkind (Parent (Renamed_Object (E))) = | |
9370 | N_Package_Specification | |
9371 | and then Present (Generic_Parent (Parent (Renamed_Object (E)))) | |
9372 | then | |
9373 | Set_Is_Hidden (E); | |
9374 | Set_Is_Immediately_Visible (E, False); | |
9375 | Enter_Overloaded_Entity (S); | |
9376 | Set_Homonym (S, Homonym (E)); | |
9377 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 9378 | Check_Overriding_Indicator (S, Empty, Is_Primitive => False); |
996ae0b0 RK |
9379 | |
9380 | -- If the subprogram is implicit it is hidden by the previous | |
82c80734 RD |
9381 | -- declaration. However if it is dispatching, it must appear in the |
9382 | -- dispatch table anyway, because it can be dispatched to even if it | |
9383 | -- cannot be called directly. | |
996ae0b0 | 9384 | |
4adf3c50 | 9385 | elsif Present (Alias (S)) and then not Comes_From_Source (S) then |
996ae0b0 RK |
9386 | Set_Scope (S, Current_Scope); |
9387 | ||
9388 | if Is_Dispatching_Operation (Alias (S)) then | |
9389 | Check_Dispatching_Operation (S, Empty); | |
9390 | end if; | |
9391 | ||
9392 | return; | |
9393 | ||
9394 | else | |
9395 | Error_Msg_Sloc := Sloc (E); | |
996ae0b0 | 9396 | |
f3d57416 | 9397 | -- Generate message, with useful additional warning if in generic |
996ae0b0 RK |
9398 | |
9399 | if Is_Generic_Unit (E) then | |
5d37ba92 ES |
9400 | Error_Msg_N ("previous generic unit cannot be overloaded", S); |
9401 | Error_Msg_N ("\& conflicts with declaration#", S); | |
9402 | else | |
9403 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
9404 | end if; |
9405 | ||
9406 | return; | |
9407 | end if; | |
9408 | ||
fbf5a39b AC |
9409 | -- E exists and is overloadable |
9410 | ||
996ae0b0 | 9411 | else |
8aa15e3b | 9412 | Check_Synchronized_Overriding (S, Overridden_Subp); |
758c442c | 9413 | |
82c80734 RD |
9414 | -- Loop through E and its homonyms to determine if any of them is |
9415 | -- the candidate for overriding by S. | |
996ae0b0 RK |
9416 | |
9417 | while Present (E) loop | |
fbf5a39b AC |
9418 | |
9419 | -- Definitely not interesting if not in the current scope | |
9420 | ||
996ae0b0 RK |
9421 | if Scope (E) /= Current_Scope then |
9422 | null; | |
9423 | ||
aca90db9 AC |
9424 | -- A function can overload the name of an abstract state. The |
9425 | -- state can be viewed as a function with a profile that cannot | |
9426 | -- be matched by anything. | |
9427 | ||
9428 | elsif Ekind (S) = E_Function | |
9429 | and then Ekind (E) = E_Abstract_State | |
9430 | then | |
9431 | Enter_Overloaded_Entity (S); | |
9432 | return; | |
9433 | ||
2995860f AC |
9434 | -- Ada 2012 (AI05-0165): For internally generated bodies of null |
9435 | -- procedures locate the internally generated spec. We enforce | |
9436 | -- mode conformance since a tagged type may inherit from | |
9437 | -- interfaces several null primitives which differ only in | |
9438 | -- the mode of the formals. | |
25ebc085 AC |
9439 | |
9440 | elsif not Comes_From_Source (S) | |
9441 | and then Is_Null_Procedure (S) | |
9442 | and then not Mode_Conformant (E, S) | |
9443 | then | |
9444 | null; | |
9445 | ||
fbf5a39b AC |
9446 | -- Check if we have type conformance |
9447 | ||
ec4867fa | 9448 | elsif Type_Conformant (E, S) then |
c8ef728f | 9449 | |
82c80734 RD |
9450 | -- If the old and new entities have the same profile and one |
9451 | -- is not the body of the other, then this is an error, unless | |
9452 | -- one of them is implicitly declared. | |
996ae0b0 RK |
9453 | |
9454 | -- There are some cases when both can be implicit, for example | |
9455 | -- when both a literal and a function that overrides it are | |
f3d57416 | 9456 | -- inherited in a derivation, or when an inherited operation |
ec4867fa | 9457 | -- of a tagged full type overrides the inherited operation of |
f3d57416 | 9458 | -- a private extension. Ada 83 had a special rule for the |
885c4871 | 9459 | -- literal case. In Ada 95, the later implicit operation hides |
82c80734 RD |
9460 | -- the former, and the literal is always the former. In the |
9461 | -- odd case where both are derived operations declared at the | |
9462 | -- same point, both operations should be declared, and in that | |
9463 | -- case we bypass the following test and proceed to the next | |
df46b832 AC |
9464 | -- part. This can only occur for certain obscure cases in |
9465 | -- instances, when an operation on a type derived from a formal | |
9466 | -- private type does not override a homograph inherited from | |
9467 | -- the actual. In subsequent derivations of such a type, the | |
9468 | -- DT positions of these operations remain distinct, if they | |
9469 | -- have been set. | |
996ae0b0 RK |
9470 | |
9471 | if Present (Alias (S)) | |
9472 | and then (No (Alias (E)) | |
9473 | or else Comes_From_Source (E) | |
2ddc2000 | 9474 | or else Is_Abstract_Subprogram (S) |
df46b832 AC |
9475 | or else |
9476 | (Is_Dispatching_Operation (E) | |
84c0a895 | 9477 | and then Is_Overriding_Alias (E, S))) |
df46b832 | 9478 | and then Ekind (E) /= E_Enumeration_Literal |
996ae0b0 | 9479 | then |
82c80734 RD |
9480 | -- When an derived operation is overloaded it may be due to |
9481 | -- the fact that the full view of a private extension | |
996ae0b0 RK |
9482 | -- re-inherits. It has to be dealt with. |
9483 | ||
e660dbf7 | 9484 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
9485 | and then In_Private_Part (Current_Scope) |
9486 | then | |
9487 | Check_Operation_From_Private_View (S, E); | |
9488 | end if; | |
9489 | ||
038140ed AC |
9490 | -- In any case the implicit operation remains hidden by the |
9491 | -- existing declaration, which is overriding. Indicate that | |
9492 | -- E overrides the operation from which S is inherited. | |
996ae0b0 | 9493 | |
038140ed | 9494 | if Present (Alias (S)) then |
039538bc AC |
9495 | Set_Overridden_Operation (E, Alias (S)); |
9496 | Inherit_Subprogram_Contract (E, Alias (S)); | |
9497 | ||
038140ed | 9498 | else |
039538bc AC |
9499 | Set_Overridden_Operation (E, S); |
9500 | Inherit_Subprogram_Contract (E, S); | |
038140ed | 9501 | end if; |
758c442c GD |
9502 | |
9503 | if Comes_From_Source (E) then | |
5d37ba92 | 9504 | Check_Overriding_Indicator (E, S, Is_Primitive => False); |
758c442c GD |
9505 | end if; |
9506 | ||
996ae0b0 RK |
9507 | return; |
9508 | ||
26a43556 AC |
9509 | -- Within an instance, the renaming declarations for actual |
9510 | -- subprograms may become ambiguous, but they do not hide each | |
9511 | -- other. | |
996ae0b0 RK |
9512 | |
9513 | elsif Ekind (E) /= E_Entry | |
9514 | and then not Comes_From_Source (E) | |
9515 | and then not Is_Generic_Instance (E) | |
9516 | and then (Present (Alias (E)) | |
9517 | or else Is_Intrinsic_Subprogram (E)) | |
9518 | and then (not In_Instance | |
9519 | or else No (Parent (E)) | |
9520 | or else Nkind (Unit_Declaration_Node (E)) /= | |
8dbd1460 | 9521 | N_Subprogram_Renaming_Declaration) |
996ae0b0 | 9522 | then |
26a43556 AC |
9523 | -- A subprogram child unit is not allowed to override an |
9524 | -- inherited subprogram (10.1.1(20)). | |
996ae0b0 RK |
9525 | |
9526 | if Is_Child_Unit (S) then | |
9527 | Error_Msg_N | |
9528 | ("child unit overrides inherited subprogram in parent", | |
9529 | S); | |
9530 | return; | |
9531 | end if; | |
9532 | ||
9533 | if Is_Non_Overriding_Operation (E, S) then | |
9534 | Enter_Overloaded_Entity (S); | |
8dbd1460 | 9535 | |
c8ef728f | 9536 | if No (Derived_Type) |
996ae0b0 RK |
9537 | or else Is_Tagged_Type (Derived_Type) |
9538 | then | |
9539 | Check_Dispatching_Operation (S, Empty); | |
9540 | end if; | |
9541 | ||
9542 | return; | |
9543 | end if; | |
9544 | ||
9545 | -- E is a derived operation or an internal operator which | |
9546 | -- is being overridden. Remove E from further visibility. | |
9547 | -- Furthermore, if E is a dispatching operation, it must be | |
9548 | -- replaced in the list of primitive operations of its type | |
9549 | -- (see Override_Dispatching_Operation). | |
9550 | ||
ec4867fa | 9551 | Overridden_Subp := E; |
758c442c | 9552 | |
996ae0b0 RK |
9553 | declare |
9554 | Prev : Entity_Id; | |
9555 | ||
9556 | begin | |
9557 | Prev := First_Entity (Current_Scope); | |
8fde064e | 9558 | while Present (Prev) and then Next_Entity (Prev) /= E loop |
996ae0b0 RK |
9559 | Next_Entity (Prev); |
9560 | end loop; | |
9561 | ||
9562 | -- It is possible for E to be in the current scope and | |
9563 | -- yet not in the entity chain. This can only occur in a | |
9564 | -- generic context where E is an implicit concatenation | |
9565 | -- in the formal part, because in a generic body the | |
9566 | -- entity chain starts with the formals. | |
9567 | ||
9568 | pragma Assert | |
9569 | (Present (Prev) or else Chars (E) = Name_Op_Concat); | |
9570 | ||
9571 | -- E must be removed both from the entity_list of the | |
9572 | -- current scope, and from the visibility chain | |
9573 | ||
9574 | if Debug_Flag_E then | |
9575 | Write_Str ("Override implicit operation "); | |
9576 | Write_Int (Int (E)); | |
9577 | Write_Eol; | |
9578 | end if; | |
9579 | ||
9580 | -- If E is a predefined concatenation, it stands for four | |
9581 | -- different operations. As a result, a single explicit | |
9582 | -- declaration does not hide it. In a possible ambiguous | |
9583 | -- situation, Disambiguate chooses the user-defined op, | |
9584 | -- so it is correct to retain the previous internal one. | |
9585 | ||
9586 | if Chars (E) /= Name_Op_Concat | |
9587 | or else Ekind (E) /= E_Operator | |
9588 | then | |
9589 | -- For nondispatching derived operations that are | |
9590 | -- overridden by a subprogram declared in the private | |
8dbd1460 AC |
9591 | -- part of a package, we retain the derived subprogram |
9592 | -- but mark it as not immediately visible. If the | |
9593 | -- derived operation was declared in the visible part | |
9594 | -- then this ensures that it will still be visible | |
9595 | -- outside the package with the proper signature | |
9596 | -- (calls from outside must also be directed to this | |
9597 | -- version rather than the overriding one, unlike the | |
9598 | -- dispatching case). Calls from inside the package | |
9599 | -- will still resolve to the overriding subprogram | |
9600 | -- since the derived one is marked as not visible | |
9601 | -- within the package. | |
996ae0b0 RK |
9602 | |
9603 | -- If the private operation is dispatching, we achieve | |
9604 | -- the overriding by keeping the implicit operation | |
9865d858 | 9605 | -- but setting its alias to be the overriding one. In |
996ae0b0 RK |
9606 | -- this fashion the proper body is executed in all |
9607 | -- cases, but the original signature is used outside | |
9608 | -- of the package. | |
9609 | ||
9610 | -- If the overriding is not in the private part, we | |
9611 | -- remove the implicit operation altogether. | |
9612 | ||
9613 | if Is_Private_Declaration (S) then | |
996ae0b0 RK |
9614 | if not Is_Dispatching_Operation (E) then |
9615 | Set_Is_Immediately_Visible (E, False); | |
9616 | else | |
e895b435 | 9617 | -- Work done in Override_Dispatching_Operation, |
a46cde68 | 9618 | -- so nothing else needs to be done here. |
996ae0b0 RK |
9619 | |
9620 | null; | |
9621 | end if; | |
996ae0b0 | 9622 | |
fbf5a39b AC |
9623 | else |
9624 | -- Find predecessor of E in Homonym chain | |
996ae0b0 RK |
9625 | |
9626 | if E = Current_Entity (E) then | |
9627 | Prev_Vis := Empty; | |
9628 | else | |
9629 | Prev_Vis := Current_Entity (E); | |
9630 | while Homonym (Prev_Vis) /= E loop | |
9631 | Prev_Vis := Homonym (Prev_Vis); | |
9632 | end loop; | |
9633 | end if; | |
9634 | ||
9635 | if Prev_Vis /= Empty then | |
9636 | ||
9637 | -- Skip E in the visibility chain | |
9638 | ||
9639 | Set_Homonym (Prev_Vis, Homonym (E)); | |
9640 | ||
9641 | else | |
9642 | Set_Name_Entity_Id (Chars (E), Homonym (E)); | |
9643 | end if; | |
9644 | ||
9645 | Set_Next_Entity (Prev, Next_Entity (E)); | |
9646 | ||
9647 | if No (Next_Entity (Prev)) then | |
9648 | Set_Last_Entity (Current_Scope, Prev); | |
9649 | end if; | |
996ae0b0 RK |
9650 | end if; |
9651 | end if; | |
9652 | ||
9653 | Enter_Overloaded_Entity (S); | |
1c1289e7 AC |
9654 | |
9655 | -- For entities generated by Derive_Subprograms the | |
9656 | -- overridden operation is the inherited primitive | |
9657 | -- (which is available through the attribute alias). | |
9658 | ||
9659 | if not (Comes_From_Source (E)) | |
9660 | and then Is_Dispatching_Operation (E) | |
f9673bb0 AC |
9661 | and then Find_Dispatching_Type (E) = |
9662 | Find_Dispatching_Type (S) | |
1c1289e7 AC |
9663 | and then Present (Alias (E)) |
9664 | and then Comes_From_Source (Alias (E)) | |
9665 | then | |
039538bc AC |
9666 | Set_Overridden_Operation (S, Alias (E)); |
9667 | Inherit_Subprogram_Contract (S, Alias (E)); | |
2fe829ae | 9668 | |
6320f5e1 AC |
9669 | -- Normal case of setting entity as overridden |
9670 | ||
9671 | -- Note: Static_Initialization and Overridden_Operation | |
9672 | -- attributes use the same field in subprogram entities. | |
9673 | -- Static_Initialization is only defined for internal | |
9674 | -- initialization procedures, where Overridden_Operation | |
9675 | -- is irrelevant. Therefore the setting of this attribute | |
9676 | -- must check whether the target is an init_proc. | |
9677 | ||
2fe829ae | 9678 | elsif not Is_Init_Proc (S) then |
039538bc AC |
9679 | Set_Overridden_Operation (S, E); |
9680 | Inherit_Subprogram_Contract (S, E); | |
1c1289e7 AC |
9681 | end if; |
9682 | ||
5d37ba92 | 9683 | Check_Overriding_Indicator (S, E, Is_Primitive => True); |
996ae0b0 | 9684 | |
fc53fe76 | 9685 | -- If S is a user-defined subprogram or a null procedure |
38ef8ebe AC |
9686 | -- expanded to override an inherited null procedure, or a |
9687 | -- predefined dispatching primitive then indicate that E | |
038140ed | 9688 | -- overrides the operation from which S is inherited. |
fc53fe76 AC |
9689 | |
9690 | if Comes_From_Source (S) | |
9691 | or else | |
9692 | (Present (Parent (S)) | |
9693 | and then | |
9694 | Nkind (Parent (S)) = N_Procedure_Specification | |
9695 | and then | |
9696 | Null_Present (Parent (S))) | |
38ef8ebe AC |
9697 | or else |
9698 | (Present (Alias (E)) | |
f16e8df9 RD |
9699 | and then |
9700 | Is_Predefined_Dispatching_Operation (Alias (E))) | |
fc53fe76 | 9701 | then |
c8ef728f | 9702 | if Present (Alias (E)) then |
039538bc AC |
9703 | Set_Overridden_Operation (S, Alias (E)); |
9704 | Inherit_Subprogram_Contract (S, Alias (E)); | |
41251c60 JM |
9705 | end if; |
9706 | end if; | |
9707 | ||
996ae0b0 | 9708 | if Is_Dispatching_Operation (E) then |
fbf5a39b | 9709 | |
82c80734 | 9710 | -- An overriding dispatching subprogram inherits the |
f9673bb0 | 9711 | -- convention of the overridden subprogram (AI-117). |
996ae0b0 RK |
9712 | |
9713 | Set_Convention (S, Convention (E)); | |
41251c60 JM |
9714 | Check_Dispatching_Operation (S, E); |
9715 | ||
996ae0b0 RK |
9716 | else |
9717 | Check_Dispatching_Operation (S, Empty); | |
9718 | end if; | |
9719 | ||
5d37ba92 ES |
9720 | Check_For_Primitive_Subprogram |
9721 | (Is_Primitive_Subp, Is_Overriding => True); | |
996ae0b0 RK |
9722 | goto Check_Inequality; |
9723 | end; | |
9724 | ||
9725 | -- Apparent redeclarations in instances can occur when two | |
9726 | -- formal types get the same actual type. The subprograms in | |
9727 | -- in the instance are legal, even if not callable from the | |
9728 | -- outside. Calls from within are disambiguated elsewhere. | |
9729 | -- For dispatching operations in the visible part, the usual | |
9730 | -- rules apply, and operations with the same profile are not | |
9731 | -- legal (B830001). | |
9732 | ||
9733 | elsif (In_Instance_Visible_Part | |
9734 | and then not Is_Dispatching_Operation (E)) | |
9735 | or else In_Instance_Not_Visible | |
9736 | then | |
9737 | null; | |
9738 | ||
9739 | -- Here we have a real error (identical profile) | |
9740 | ||
9741 | else | |
9742 | Error_Msg_Sloc := Sloc (E); | |
9743 | ||
9744 | -- Avoid cascaded errors if the entity appears in | |
9745 | -- subsequent calls. | |
9746 | ||
9747 | Set_Scope (S, Current_Scope); | |
9748 | ||
5d37ba92 ES |
9749 | -- Generate error, with extra useful warning for the case |
9750 | -- of a generic instance with no completion. | |
996ae0b0 RK |
9751 | |
9752 | if Is_Generic_Instance (S) | |
9753 | and then not Has_Completion (E) | |
9754 | then | |
9755 | Error_Msg_N | |
5d37ba92 ES |
9756 | ("instantiation cannot provide body for&", S); |
9757 | Error_Msg_N ("\& conflicts with declaration#", S); | |
9758 | else | |
9759 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
9760 | end if; |
9761 | ||
9762 | return; | |
9763 | end if; | |
9764 | ||
9765 | else | |
c8ef728f ES |
9766 | -- If one subprogram has an access parameter and the other |
9767 | -- a parameter of an access type, calls to either might be | |
9768 | -- ambiguous. Verify that parameters match except for the | |
9769 | -- access parameter. | |
9770 | ||
9771 | if May_Hide_Profile then | |
9772 | declare | |
ec4867fa ES |
9773 | F1 : Entity_Id; |
9774 | F2 : Entity_Id; | |
8dbd1460 | 9775 | |
c8ef728f ES |
9776 | begin |
9777 | F1 := First_Formal (S); | |
9778 | F2 := First_Formal (E); | |
9779 | while Present (F1) and then Present (F2) loop | |
9780 | if Is_Access_Type (Etype (F1)) then | |
9781 | if not Is_Access_Type (Etype (F2)) | |
9782 | or else not Conforming_Types | |
9783 | (Designated_Type (Etype (F1)), | |
9784 | Designated_Type (Etype (F2)), | |
9785 | Type_Conformant) | |
9786 | then | |
9787 | May_Hide_Profile := False; | |
9788 | end if; | |
9789 | ||
9790 | elsif | |
9791 | not Conforming_Types | |
9792 | (Etype (F1), Etype (F2), Type_Conformant) | |
9793 | then | |
9794 | May_Hide_Profile := False; | |
9795 | end if; | |
9796 | ||
9797 | Next_Formal (F1); | |
9798 | Next_Formal (F2); | |
9799 | end loop; | |
9800 | ||
9801 | if May_Hide_Profile | |
9802 | and then No (F1) | |
9803 | and then No (F2) | |
9804 | then | |
dbfeb4fa | 9805 | Error_Msg_NE ("calls to& may be ambiguous??", S, S); |
c8ef728f ES |
9806 | end if; |
9807 | end; | |
9808 | end if; | |
996ae0b0 RK |
9809 | end if; |
9810 | ||
996ae0b0 RK |
9811 | E := Homonym (E); |
9812 | end loop; | |
9813 | ||
9814 | -- On exit, we know that S is a new entity | |
9815 | ||
9816 | Enter_Overloaded_Entity (S); | |
5d37ba92 ES |
9817 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
9818 | Check_Overriding_Indicator | |
9819 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
996ae0b0 | 9820 | |
c4d67e2d | 9821 | -- Overloading is not allowed in SPARK, except for operators |
8ed68165 | 9822 | |
c4d67e2d AC |
9823 | if Nkind (S) /= N_Defining_Operator_Symbol then |
9824 | Error_Msg_Sloc := Sloc (Homonym (S)); | |
ce5ba43a | 9825 | Check_SPARK_05_Restriction |
c4d67e2d AC |
9826 | ("overloading not allowed with entity#", S); |
9827 | end if; | |
8ed68165 | 9828 | |
82c80734 RD |
9829 | -- If S is a derived operation for an untagged type then by |
9830 | -- definition it's not a dispatching operation (even if the parent | |
e917aec2 RD |
9831 | -- operation was dispatching), so Check_Dispatching_Operation is not |
9832 | -- called in that case. | |
996ae0b0 | 9833 | |
c8ef728f | 9834 | if No (Derived_Type) |
996ae0b0 RK |
9835 | or else Is_Tagged_Type (Derived_Type) |
9836 | then | |
9837 | Check_Dispatching_Operation (S, Empty); | |
9838 | end if; | |
9839 | end if; | |
9840 | ||
82c80734 RD |
9841 | -- If this is a user-defined equality operator that is not a derived |
9842 | -- subprogram, create the corresponding inequality. If the operation is | |
9843 | -- dispatching, the expansion is done elsewhere, and we do not create | |
9844 | -- an explicit inequality operation. | |
996ae0b0 RK |
9845 | |
9846 | <<Check_Inequality>> | |
9847 | if Chars (S) = Name_Op_Eq | |
9848 | and then Etype (S) = Standard_Boolean | |
9849 | and then Present (Parent (S)) | |
9850 | and then not Is_Dispatching_Operation (S) | |
9851 | then | |
9852 | Make_Inequality_Operator (S); | |
b2834fbd | 9853 | Check_Untagged_Equality (S); |
996ae0b0 | 9854 | end if; |
996ae0b0 RK |
9855 | end New_Overloaded_Entity; |
9856 | ||
9857 | --------------------- | |
9858 | -- Process_Formals -- | |
9859 | --------------------- | |
9860 | ||
9861 | procedure Process_Formals | |
07fc65c4 | 9862 | (T : List_Id; |
996ae0b0 RK |
9863 | Related_Nod : Node_Id) |
9864 | is | |
9865 | Param_Spec : Node_Id; | |
9866 | Formal : Entity_Id; | |
9867 | Formal_Type : Entity_Id; | |
9868 | Default : Node_Id; | |
9869 | Ptype : Entity_Id; | |
9870 | ||
800621e0 RD |
9871 | Num_Out_Params : Nat := 0; |
9872 | First_Out_Param : Entity_Id := Empty; | |
21d27997 | 9873 | -- Used for setting Is_Only_Out_Parameter |
800621e0 | 9874 | |
7b56a91b | 9875 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean; |
950d217a AC |
9876 | -- Determine whether an access type designates a type coming from a |
9877 | -- limited view. | |
9878 | ||
07fc65c4 | 9879 | function Is_Class_Wide_Default (D : Node_Id) return Boolean; |
82c80734 RD |
9880 | -- Check whether the default has a class-wide type. After analysis the |
9881 | -- default has the type of the formal, so we must also check explicitly | |
9882 | -- for an access attribute. | |
07fc65c4 | 9883 | |
7b56a91b AC |
9884 | ---------------------------------- |
9885 | -- Designates_From_Limited_With -- | |
9886 | ---------------------------------- | |
950d217a | 9887 | |
7b56a91b | 9888 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is |
950d217a AC |
9889 | Desig : Entity_Id := Typ; |
9890 | ||
9891 | begin | |
9892 | if Is_Access_Type (Desig) then | |
9893 | Desig := Directly_Designated_Type (Desig); | |
9894 | end if; | |
9895 | ||
9896 | if Is_Class_Wide_Type (Desig) then | |
9897 | Desig := Root_Type (Desig); | |
9898 | end if; | |
9899 | ||
9900 | return | |
7b56a91b AC |
9901 | Ekind (Desig) = E_Incomplete_Type |
9902 | and then From_Limited_With (Desig); | |
9903 | end Designates_From_Limited_With; | |
950d217a | 9904 | |
07fc65c4 GB |
9905 | --------------------------- |
9906 | -- Is_Class_Wide_Default -- | |
9907 | --------------------------- | |
9908 | ||
9909 | function Is_Class_Wide_Default (D : Node_Id) return Boolean is | |
9910 | begin | |
9911 | return Is_Class_Wide_Type (Designated_Type (Etype (D))) | |
9912 | or else (Nkind (D) = N_Attribute_Reference | |
0f853035 YM |
9913 | and then Attribute_Name (D) = Name_Access |
9914 | and then Is_Class_Wide_Type (Etype (Prefix (D)))); | |
07fc65c4 GB |
9915 | end Is_Class_Wide_Default; |
9916 | ||
9917 | -- Start of processing for Process_Formals | |
9918 | ||
996ae0b0 RK |
9919 | begin |
9920 | -- In order to prevent premature use of the formals in the same formal | |
9921 | -- part, the Ekind is left undefined until all default expressions are | |
9922 | -- analyzed. The Ekind is established in a separate loop at the end. | |
9923 | ||
9924 | Param_Spec := First (T); | |
996ae0b0 | 9925 | while Present (Param_Spec) loop |
996ae0b0 | 9926 | Formal := Defining_Identifier (Param_Spec); |
5d37ba92 | 9927 | Set_Never_Set_In_Source (Formal, True); |
996ae0b0 RK |
9928 | Enter_Name (Formal); |
9929 | ||
9930 | -- Case of ordinary parameters | |
9931 | ||
9932 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
9933 | Find_Type (Parameter_Type (Param_Spec)); | |
9934 | Ptype := Parameter_Type (Param_Spec); | |
9935 | ||
9936 | if Ptype = Error then | |
9937 | goto Continue; | |
9938 | end if; | |
9939 | ||
9940 | Formal_Type := Entity (Ptype); | |
9941 | ||
ec4867fa ES |
9942 | if Is_Incomplete_Type (Formal_Type) |
9943 | or else | |
9944 | (Is_Class_Wide_Type (Formal_Type) | |
8fde064e | 9945 | and then Is_Incomplete_Type (Root_Type (Formal_Type))) |
996ae0b0 | 9946 | then |
93bcda23 AC |
9947 | -- Ada 2005 (AI-326): Tagged incomplete types allowed in |
9948 | -- primitive operations, as long as their completion is | |
9949 | -- in the same declarative part. If in the private part | |
9950 | -- this means that the type cannot be a Taft-amendment type. | |
cec29135 ES |
9951 | -- Check is done on package exit. For access to subprograms, |
9952 | -- the use is legal for Taft-amendment types. | |
fbf5a39b | 9953 | |
6eddd7b4 AC |
9954 | -- Ada 2012: tagged incomplete types are allowed as generic |
9955 | -- formal types. They do not introduce dependencies and the | |
9956 | -- corresponding generic subprogram does not have a delayed | |
5b6f12c7 AC |
9957 | -- freeze, because it does not need a freeze node. However, |
9958 | -- it is still the case that untagged incomplete types cannot | |
9959 | -- be Taft-amendment types and must be completed in private | |
9960 | -- part, so the subprogram must appear in the list of private | |
9961 | -- dependents of the type. | |
9962 | ||
9963 | if Is_Tagged_Type (Formal_Type) | |
b973629e | 9964 | or else (Ada_Version >= Ada_2012 |
1ebc2612 AC |
9965 | and then not From_Limited_With (Formal_Type) |
9966 | and then not Is_Generic_Type (Formal_Type)) | |
5b6f12c7 | 9967 | then |
93bcda23 | 9968 | if Ekind (Scope (Current_Scope)) = E_Package |
6eddd7b4 | 9969 | and then not Is_Generic_Type (Formal_Type) |
93bcda23 AC |
9970 | and then not Is_Class_Wide_Type (Formal_Type) |
9971 | then | |
cec29135 ES |
9972 | if not Nkind_In |
9973 | (Parent (T), N_Access_Function_Definition, | |
9974 | N_Access_Procedure_Definition) | |
9975 | then | |
9976 | Append_Elmt | |
9977 | (Current_Scope, | |
09c954dc | 9978 | To => Private_Dependents (Base_Type (Formal_Type))); |
4637729f AC |
9979 | |
9980 | -- Freezing is delayed to ensure that Register_Prim | |
9981 | -- will get called for this operation, which is needed | |
9982 | -- in cases where static dispatch tables aren't built. | |
9983 | -- (Note that the same is done for controlling access | |
9984 | -- parameter cases in function Access_Definition.) | |
9985 | ||
13fa2acb AC |
9986 | if not Is_Thunk (Current_Scope) then |
9987 | Set_Has_Delayed_Freeze (Current_Scope); | |
9988 | end if; | |
cec29135 | 9989 | end if; |
93bcda23 | 9990 | end if; |
fbf5a39b | 9991 | |
0a36105d JM |
9992 | -- Special handling of Value_Type for CIL case |
9993 | ||
9994 | elsif Is_Value_Type (Formal_Type) then | |
9995 | null; | |
9996 | ||
800621e0 RD |
9997 | elsif not Nkind_In (Parent (T), N_Access_Function_Definition, |
9998 | N_Access_Procedure_Definition) | |
996ae0b0 | 9999 | then |
dd386db0 AC |
10000 | -- AI05-0151: Tagged incomplete types are allowed in all |
10001 | -- formal parts. Untagged incomplete types are not allowed | |
b973629e AC |
10002 | -- in bodies. Limited views of either kind are not allowed |
10003 | -- if there is no place at which the non-limited view can | |
10004 | -- become available. | |
a18e3d62 | 10005 | |
1ebc2612 AC |
10006 | -- Incomplete formal untagged types are not allowed in |
10007 | -- subprogram bodies (but are legal in their declarations). | |
10008 | ||
10009 | if Is_Generic_Type (Formal_Type) | |
10010 | and then not Is_Tagged_Type (Formal_Type) | |
10011 | and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body | |
10012 | then | |
10013 | Error_Msg_N | |
10014 | ("invalid use of formal incomplete type", Param_Spec); | |
dd386db0 | 10015 | |
1ebc2612 | 10016 | elsif Ada_Version >= Ada_2012 then |
b973629e AC |
10017 | if Is_Tagged_Type (Formal_Type) |
10018 | and then (not From_Limited_With (Formal_Type) | |
10019 | or else not In_Package_Body) | |
10020 | then | |
dd386db0 AC |
10021 | null; |
10022 | ||
0f1a6a0b | 10023 | elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement, |
b973629e | 10024 | N_Accept_Alternative, |
0f1a6a0b AC |
10025 | N_Entry_Body, |
10026 | N_Subprogram_Body) | |
dd386db0 AC |
10027 | then |
10028 | Error_Msg_NE | |
10029 | ("invalid use of untagged incomplete type&", | |
0f1a6a0b | 10030 | Ptype, Formal_Type); |
dd386db0 AC |
10031 | end if; |
10032 | ||
10033 | else | |
10034 | Error_Msg_NE | |
10035 | ("invalid use of incomplete type&", | |
0f1a6a0b | 10036 | Param_Spec, Formal_Type); |
dd386db0 AC |
10037 | |
10038 | -- Further checks on the legality of incomplete types | |
10039 | -- in formal parts are delayed until the freeze point | |
10040 | -- of the enclosing subprogram or access to subprogram. | |
10041 | end if; | |
996ae0b0 RK |
10042 | end if; |
10043 | ||
10044 | elsif Ekind (Formal_Type) = E_Void then | |
0f1a6a0b AC |
10045 | Error_Msg_NE |
10046 | ("premature use of&", | |
10047 | Parameter_Type (Param_Spec), Formal_Type); | |
996ae0b0 RK |
10048 | end if; |
10049 | ||
fecbd779 AC |
10050 | -- Ada 2012 (AI-142): Handle aliased parameters |
10051 | ||
10052 | if Ada_Version >= Ada_2012 | |
10053 | and then Aliased_Present (Param_Spec) | |
10054 | then | |
10055 | Set_Is_Aliased (Formal); | |
10056 | end if; | |
10057 | ||
0ab80019 | 10058 | -- Ada 2005 (AI-231): Create and decorate an internal subtype |
7324bf49 | 10059 | -- declaration corresponding to the null-excluding type of the |
d8db0bca JM |
10060 | -- formal in the enclosing scope. Finally, replace the parameter |
10061 | -- type of the formal with the internal subtype. | |
7324bf49 | 10062 | |
0791fbe9 | 10063 | if Ada_Version >= Ada_2005 |
41251c60 | 10064 | and then Null_Exclusion_Present (Param_Spec) |
7324bf49 | 10065 | then |
ec4867fa | 10066 | if not Is_Access_Type (Formal_Type) then |
ed2233dc | 10067 | Error_Msg_N |
0a36105d JM |
10068 | ("`NOT NULL` allowed only for an access type", Param_Spec); |
10069 | ||
ec4867fa ES |
10070 | else |
10071 | if Can_Never_Be_Null (Formal_Type) | |
10072 | and then Comes_From_Source (Related_Nod) | |
10073 | then | |
ed2233dc | 10074 | Error_Msg_NE |
0a36105d | 10075 | ("`NOT NULL` not allowed (& already excludes null)", |
0f1a6a0b | 10076 | Param_Spec, Formal_Type); |
ec4867fa | 10077 | end if; |
41251c60 | 10078 | |
ec4867fa ES |
10079 | Formal_Type := |
10080 | Create_Null_Excluding_Itype | |
10081 | (T => Formal_Type, | |
10082 | Related_Nod => Related_Nod, | |
10083 | Scope_Id => Scope (Current_Scope)); | |
0a36105d | 10084 | |
fcf848c4 AC |
10085 | -- If the designated type of the itype is an itype that is |
10086 | -- not frozen yet, we set the Has_Delayed_Freeze attribute | |
10087 | -- on the access subtype, to prevent order-of-elaboration | |
10088 | -- issues in the backend. | |
0a36105d JM |
10089 | |
10090 | -- Example: | |
10091 | -- type T is access procedure; | |
10092 | -- procedure Op (O : not null T); | |
10093 | ||
fcf848c4 AC |
10094 | if Is_Itype (Directly_Designated_Type (Formal_Type)) |
10095 | and then | |
10096 | not Is_Frozen (Directly_Designated_Type (Formal_Type)) | |
10097 | then | |
0a36105d JM |
10098 | Set_Has_Delayed_Freeze (Formal_Type); |
10099 | end if; | |
ec4867fa | 10100 | end if; |
7324bf49 AC |
10101 | end if; |
10102 | ||
996ae0b0 RK |
10103 | -- An access formal type |
10104 | ||
10105 | else | |
10106 | Formal_Type := | |
10107 | Access_Definition (Related_Nod, Parameter_Type (Param_Spec)); | |
7324bf49 | 10108 | |
f937473f RD |
10109 | -- No need to continue if we already notified errors |
10110 | ||
10111 | if not Present (Formal_Type) then | |
10112 | return; | |
10113 | end if; | |
10114 | ||
0ab80019 | 10115 | -- Ada 2005 (AI-254) |
7324bf49 | 10116 | |
af4b9434 AC |
10117 | declare |
10118 | AD : constant Node_Id := | |
10119 | Access_To_Subprogram_Definition | |
10120 | (Parameter_Type (Param_Spec)); | |
10121 | begin | |
10122 | if Present (AD) and then Protected_Present (AD) then | |
10123 | Formal_Type := | |
10124 | Replace_Anonymous_Access_To_Protected_Subprogram | |
f937473f | 10125 | (Param_Spec); |
af4b9434 AC |
10126 | end if; |
10127 | end; | |
996ae0b0 RK |
10128 | end if; |
10129 | ||
10130 | Set_Etype (Formal, Formal_Type); | |
0f853035 | 10131 | |
fecbd779 AC |
10132 | -- Deal with default expression if present |
10133 | ||
fbf5a39b | 10134 | Default := Expression (Param_Spec); |
996ae0b0 RK |
10135 | |
10136 | if Present (Default) then | |
ce5ba43a | 10137 | Check_SPARK_05_Restriction |
fe5d3068 | 10138 | ("default expression is not allowed", Default); |
38171f43 | 10139 | |
996ae0b0 | 10140 | if Out_Present (Param_Spec) then |
ed2233dc | 10141 | Error_Msg_N |
996ae0b0 RK |
10142 | ("default initialization only allowed for IN parameters", |
10143 | Param_Spec); | |
10144 | end if; | |
10145 | ||
10146 | -- Do the special preanalysis of the expression (see section on | |
10147 | -- "Handling of Default Expressions" in the spec of package Sem). | |
10148 | ||
21d27997 | 10149 | Preanalyze_Spec_Expression (Default, Formal_Type); |
996ae0b0 | 10150 | |
f29b857f ES |
10151 | -- An access to constant cannot be the default for |
10152 | -- an access parameter that is an access to variable. | |
2eb160f2 ST |
10153 | |
10154 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
10155 | and then not Is_Access_Constant (Formal_Type) | |
10156 | and then Is_Access_Type (Etype (Default)) | |
10157 | and then Is_Access_Constant (Etype (Default)) | |
10158 | then | |
f29b857f | 10159 | Error_Msg_N |
84c0a895 AC |
10160 | ("formal that is access to variable cannot be initialized " |
10161 | & "with an access-to-constant expression", Default); | |
2eb160f2 ST |
10162 | end if; |
10163 | ||
d8db0bca JM |
10164 | -- Check that the designated type of an access parameter's default |
10165 | -- is not a class-wide type unless the parameter's designated type | |
10166 | -- is also class-wide. | |
996ae0b0 RK |
10167 | |
10168 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
7b56a91b | 10169 | and then not Designates_From_Limited_With (Formal_Type) |
07fc65c4 | 10170 | and then Is_Class_Wide_Default (Default) |
996ae0b0 RK |
10171 | and then not Is_Class_Wide_Type (Designated_Type (Formal_Type)) |
10172 | then | |
07fc65c4 GB |
10173 | Error_Msg_N |
10174 | ("access to class-wide expression not allowed here", Default); | |
996ae0b0 | 10175 | end if; |
4755cce9 JM |
10176 | |
10177 | -- Check incorrect use of dynamically tagged expressions | |
10178 | ||
10179 | if Is_Tagged_Type (Formal_Type) then | |
10180 | Check_Dynamically_Tagged_Expression | |
10181 | (Expr => Default, | |
10182 | Typ => Formal_Type, | |
10183 | Related_Nod => Default); | |
10184 | end if; | |
996ae0b0 RK |
10185 | end if; |
10186 | ||
41251c60 JM |
10187 | -- Ada 2005 (AI-231): Static checks |
10188 | ||
0791fbe9 | 10189 | if Ada_Version >= Ada_2005 |
41251c60 JM |
10190 | and then Is_Access_Type (Etype (Formal)) |
10191 | and then Can_Never_Be_Null (Etype (Formal)) | |
10192 | then | |
10193 | Null_Exclusion_Static_Checks (Param_Spec); | |
10194 | end if; | |
10195 | ||
f1bd0415 AC |
10196 | -- The following checks are relevant when SPARK_Mode is on as these |
10197 | -- are not standard Ada legality rules. | |
6c3c671e | 10198 | |
ea26c8e4 HK |
10199 | if SPARK_Mode = On then |
10200 | if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then | |
f1bd0415 | 10201 | |
ea26c8e4 HK |
10202 | -- A function cannot have a parameter of mode IN OUT or OUT |
10203 | -- (SPARK RM 6.1). | |
f1bd0415 | 10204 | |
ea26c8e4 HK |
10205 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then |
10206 | Error_Msg_N | |
10207 | ("function cannot have parameter of mode `OUT` or " | |
10208 | & "`IN OUT`", Formal); | |
f1bd0415 | 10209 | |
d780e54f AC |
10210 | -- A function cannot have an effectively volatile formal |
10211 | -- parameter (SPARK RM 7.1.3(10)). | |
ea26c8e4 | 10212 | |
d780e54f | 10213 | elsif Is_Effectively_Volatile (Formal) then |
ea26c8e4 HK |
10214 | Error_Msg_N |
10215 | ("function cannot have a volatile formal parameter", | |
10216 | Formal); | |
10217 | end if; | |
10218 | ||
d780e54f AC |
10219 | -- A procedure cannot have an effectively volatile formal |
10220 | -- parameter of mode IN because it behaves as a constant | |
10221 | -- (SPARK RM 7.1.3(6)). | |
ea26c8e4 HK |
10222 | |
10223 | elsif Ekind (Scope (Formal)) = E_Procedure | |
10224 | and then Ekind (Formal) = E_In_Parameter | |
d780e54f | 10225 | and then Is_Effectively_Volatile (Formal) |
ea26c8e4 | 10226 | then |
f1bd0415 | 10227 | Error_Msg_N |
ea26c8e4 | 10228 | ("formal parameter of mode `IN` cannot be volatile", Formal); |
f1bd0415 | 10229 | end if; |
6c3c671e AC |
10230 | end if; |
10231 | ||
996ae0b0 RK |
10232 | <<Continue>> |
10233 | Next (Param_Spec); | |
10234 | end loop; | |
10235 | ||
82c80734 RD |
10236 | -- If this is the formal part of a function specification, analyze the |
10237 | -- subtype mark in the context where the formals are visible but not | |
10238 | -- yet usable, and may hide outer homographs. | |
10239 | ||
10240 | if Nkind (Related_Nod) = N_Function_Specification then | |
10241 | Analyze_Return_Type (Related_Nod); | |
10242 | end if; | |
10243 | ||
996ae0b0 RK |
10244 | -- Now set the kind (mode) of each formal |
10245 | ||
10246 | Param_Spec := First (T); | |
996ae0b0 RK |
10247 | while Present (Param_Spec) loop |
10248 | Formal := Defining_Identifier (Param_Spec); | |
10249 | Set_Formal_Mode (Formal); | |
10250 | ||
10251 | if Ekind (Formal) = E_In_Parameter then | |
10252 | Set_Default_Value (Formal, Expression (Param_Spec)); | |
10253 | ||
10254 | if Present (Expression (Param_Spec)) then | |
10255 | Default := Expression (Param_Spec); | |
10256 | ||
10257 | if Is_Scalar_Type (Etype (Default)) then | |
5ebfaacf AC |
10258 | if Nkind (Parameter_Type (Param_Spec)) /= |
10259 | N_Access_Definition | |
996ae0b0 RK |
10260 | then |
10261 | Formal_Type := Entity (Parameter_Type (Param_Spec)); | |
996ae0b0 | 10262 | else |
5ebfaacf AC |
10263 | Formal_Type := |
10264 | Access_Definition | |
10265 | (Related_Nod, Parameter_Type (Param_Spec)); | |
996ae0b0 RK |
10266 | end if; |
10267 | ||
10268 | Apply_Scalar_Range_Check (Default, Formal_Type); | |
10269 | end if; | |
2820d220 | 10270 | end if; |
800621e0 RD |
10271 | |
10272 | elsif Ekind (Formal) = E_Out_Parameter then | |
10273 | Num_Out_Params := Num_Out_Params + 1; | |
10274 | ||
10275 | if Num_Out_Params = 1 then | |
10276 | First_Out_Param := Formal; | |
10277 | end if; | |
10278 | ||
10279 | elsif Ekind (Formal) = E_In_Out_Parameter then | |
10280 | Num_Out_Params := Num_Out_Params + 1; | |
996ae0b0 RK |
10281 | end if; |
10282 | ||
4172a8e3 AC |
10283 | -- Skip remaining processing if formal type was in error |
10284 | ||
10285 | if Etype (Formal) = Any_Type or else Error_Posted (Formal) then | |
10286 | goto Next_Parameter; | |
10287 | end if; | |
10288 | ||
fecbd779 AC |
10289 | -- Force call by reference if aliased |
10290 | ||
10291 | if Is_Aliased (Formal) then | |
10292 | Set_Mechanism (Formal, By_Reference); | |
5ebfaacf AC |
10293 | |
10294 | -- Warn if user asked this to be passed by copy | |
10295 | ||
10296 | if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then | |
10297 | Error_Msg_N | |
b785e0b8 | 10298 | ("cannot pass aliased parameter & by copy??", Formal); |
5ebfaacf AC |
10299 | end if; |
10300 | ||
10301 | -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy | |
10302 | ||
10303 | elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then | |
10304 | Set_Mechanism (Formal, By_Copy); | |
10305 | ||
10306 | elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then | |
10307 | Set_Mechanism (Formal, By_Reference); | |
fecbd779 AC |
10308 | end if; |
10309 | ||
4172a8e3 | 10310 | <<Next_Parameter>> |
996ae0b0 RK |
10311 | Next (Param_Spec); |
10312 | end loop; | |
800621e0 RD |
10313 | |
10314 | if Present (First_Out_Param) and then Num_Out_Params = 1 then | |
10315 | Set_Is_Only_Out_Parameter (First_Out_Param); | |
10316 | end if; | |
996ae0b0 RK |
10317 | end Process_Formals; |
10318 | ||
fbf5a39b AC |
10319 | ---------------------------- |
10320 | -- Reference_Body_Formals -- | |
10321 | ---------------------------- | |
10322 | ||
10323 | procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is | |
10324 | Fs : Entity_Id; | |
10325 | Fb : Entity_Id; | |
10326 | ||
10327 | begin | |
10328 | if Error_Posted (Spec) then | |
10329 | return; | |
10330 | end if; | |
10331 | ||
0a36105d JM |
10332 | -- Iterate over both lists. They may be of different lengths if the two |
10333 | -- specs are not conformant. | |
10334 | ||
fbf5a39b AC |
10335 | Fs := First_Formal (Spec); |
10336 | Fb := First_Formal (Bod); | |
0a36105d | 10337 | while Present (Fs) and then Present (Fb) loop |
fbf5a39b AC |
10338 | Generate_Reference (Fs, Fb, 'b'); |
10339 | ||
10340 | if Style_Check then | |
10341 | Style.Check_Identifier (Fb, Fs); | |
10342 | end if; | |
10343 | ||
10344 | Set_Spec_Entity (Fb, Fs); | |
10345 | Set_Referenced (Fs, False); | |
10346 | Next_Formal (Fs); | |
10347 | Next_Formal (Fb); | |
10348 | end loop; | |
10349 | end Reference_Body_Formals; | |
10350 | ||
996ae0b0 RK |
10351 | ------------------------- |
10352 | -- Set_Actual_Subtypes -- | |
10353 | ------------------------- | |
10354 | ||
10355 | procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is | |
84c0a895 AC |
10356 | Decl : Node_Id; |
10357 | Formal : Entity_Id; | |
10358 | T : Entity_Id; | |
10359 | First_Stmt : Node_Id := Empty; | |
10360 | AS_Needed : Boolean; | |
996ae0b0 RK |
10361 | |
10362 | begin | |
f3d57416 | 10363 | -- If this is an empty initialization procedure, no need to create |
fbf5a39b AC |
10364 | -- actual subtypes (small optimization). |
10365 | ||
8fde064e | 10366 | if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then |
fbf5a39b AC |
10367 | return; |
10368 | end if; | |
10369 | ||
996ae0b0 RK |
10370 | Formal := First_Formal (Subp); |
10371 | while Present (Formal) loop | |
10372 | T := Etype (Formal); | |
10373 | ||
e895b435 | 10374 | -- We never need an actual subtype for a constrained formal |
996ae0b0 RK |
10375 | |
10376 | if Is_Constrained (T) then | |
10377 | AS_Needed := False; | |
10378 | ||
82c80734 | 10379 | -- If we have unknown discriminants, then we do not need an actual |
a90bd866 | 10380 | -- subtype, or more accurately we cannot figure it out. Note that |
82c80734 | 10381 | -- all class-wide types have unknown discriminants. |
996ae0b0 RK |
10382 | |
10383 | elsif Has_Unknown_Discriminants (T) then | |
10384 | AS_Needed := False; | |
10385 | ||
82c80734 RD |
10386 | -- At this stage we have an unconstrained type that may need an |
10387 | -- actual subtype. For sure the actual subtype is needed if we have | |
10388 | -- an unconstrained array type. | |
996ae0b0 RK |
10389 | |
10390 | elsif Is_Array_Type (T) then | |
10391 | AS_Needed := True; | |
10392 | ||
d8db0bca JM |
10393 | -- The only other case needing an actual subtype is an unconstrained |
10394 | -- record type which is an IN parameter (we cannot generate actual | |
10395 | -- subtypes for the OUT or IN OUT case, since an assignment can | |
10396 | -- change the discriminant values. However we exclude the case of | |
10397 | -- initialization procedures, since discriminants are handled very | |
10398 | -- specially in this context, see the section entitled "Handling of | |
10399 | -- Discriminants" in Einfo. | |
10400 | ||
10401 | -- We also exclude the case of Discrim_SO_Functions (functions used | |
10402 | -- in front end layout mode for size/offset values), since in such | |
10403 | -- functions only discriminants are referenced, and not only are such | |
10404 | -- subtypes not needed, but they cannot always be generated, because | |
10405 | -- of order of elaboration issues. | |
996ae0b0 RK |
10406 | |
10407 | elsif Is_Record_Type (T) | |
10408 | and then Ekind (Formal) = E_In_Parameter | |
10409 | and then Chars (Formal) /= Name_uInit | |
5d09245e | 10410 | and then not Is_Unchecked_Union (T) |
996ae0b0 RK |
10411 | and then not Is_Discrim_SO_Function (Subp) |
10412 | then | |
10413 | AS_Needed := True; | |
10414 | ||
10415 | -- All other cases do not need an actual subtype | |
10416 | ||
10417 | else | |
10418 | AS_Needed := False; | |
10419 | end if; | |
10420 | ||
10421 | -- Generate actual subtypes for unconstrained arrays and | |
10422 | -- unconstrained discriminated records. | |
10423 | ||
10424 | if AS_Needed then | |
7324bf49 | 10425 | if Nkind (N) = N_Accept_Statement then |
fbf5a39b | 10426 | |
57a8057a | 10427 | -- If expansion is active, the formal is replaced by a local |
fbf5a39b AC |
10428 | -- variable that renames the corresponding entry of the |
10429 | -- parameter block, and it is this local variable that may | |
da94696d | 10430 | -- require an actual subtype. |
fbf5a39b | 10431 | |
4460a9bc | 10432 | if Expander_Active then |
fbf5a39b AC |
10433 | Decl := Build_Actual_Subtype (T, Renamed_Object (Formal)); |
10434 | else | |
10435 | Decl := Build_Actual_Subtype (T, Formal); | |
10436 | end if; | |
10437 | ||
996ae0b0 RK |
10438 | if Present (Handled_Statement_Sequence (N)) then |
10439 | First_Stmt := | |
10440 | First (Statements (Handled_Statement_Sequence (N))); | |
10441 | Prepend (Decl, Statements (Handled_Statement_Sequence (N))); | |
10442 | Mark_Rewrite_Insertion (Decl); | |
10443 | else | |
82c80734 RD |
10444 | -- If the accept statement has no body, there will be no |
10445 | -- reference to the actuals, so no need to compute actual | |
10446 | -- subtypes. | |
996ae0b0 RK |
10447 | |
10448 | return; | |
10449 | end if; | |
10450 | ||
10451 | else | |
fbf5a39b | 10452 | Decl := Build_Actual_Subtype (T, Formal); |
996ae0b0 RK |
10453 | Prepend (Decl, Declarations (N)); |
10454 | Mark_Rewrite_Insertion (Decl); | |
10455 | end if; | |
10456 | ||
82c80734 RD |
10457 | -- The declaration uses the bounds of an existing object, and |
10458 | -- therefore needs no constraint checks. | |
2820d220 | 10459 | |
7324bf49 | 10460 | Analyze (Decl, Suppress => All_Checks); |
2820d220 | 10461 | |
996ae0b0 RK |
10462 | -- We need to freeze manually the generated type when it is |
10463 | -- inserted anywhere else than in a declarative part. | |
10464 | ||
10465 | if Present (First_Stmt) then | |
10466 | Insert_List_Before_And_Analyze (First_Stmt, | |
c159409f | 10467 | Freeze_Entity (Defining_Identifier (Decl), N)); |
fcadacf7 ES |
10468 | |
10469 | -- Ditto if the type has a dynamic predicate, because the | |
10470 | -- generated function will mention the actual subtype. | |
10471 | ||
10472 | elsif Has_Dynamic_Predicate_Aspect (T) then | |
10473 | Insert_List_Before_And_Analyze (Decl, | |
10474 | Freeze_Entity (Defining_Identifier (Decl), N)); | |
996ae0b0 RK |
10475 | end if; |
10476 | ||
fbf5a39b | 10477 | if Nkind (N) = N_Accept_Statement |
4460a9bc | 10478 | and then Expander_Active |
fbf5a39b AC |
10479 | then |
10480 | Set_Actual_Subtype (Renamed_Object (Formal), | |
10481 | Defining_Identifier (Decl)); | |
10482 | else | |
10483 | Set_Actual_Subtype (Formal, Defining_Identifier (Decl)); | |
10484 | end if; | |
996ae0b0 RK |
10485 | end if; |
10486 | ||
10487 | Next_Formal (Formal); | |
10488 | end loop; | |
10489 | end Set_Actual_Subtypes; | |
10490 | ||
10491 | --------------------- | |
10492 | -- Set_Formal_Mode -- | |
10493 | --------------------- | |
10494 | ||
10495 | procedure Set_Formal_Mode (Formal_Id : Entity_Id) is | |
10496 | Spec : constant Node_Id := Parent (Formal_Id); | |
10497 | ||
10498 | begin | |
10499 | -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters | |
10500 | -- since we ensure that corresponding actuals are always valid at the | |
10501 | -- point of the call. | |
10502 | ||
10503 | if Out_Present (Spec) then | |
fc999c5d RD |
10504 | if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then |
10505 | ||
b4ca2d2c | 10506 | -- [IN] OUT parameters allowed for functions in Ada 2012 |
c56a9ba4 AC |
10507 | |
10508 | if Ada_Version >= Ada_2012 then | |
e6425869 AC |
10509 | |
10510 | -- Even in Ada 2012 operators can only have IN parameters | |
10511 | ||
10512 | if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then | |
10513 | Error_Msg_N ("operators can only have IN parameters", Spec); | |
10514 | end if; | |
10515 | ||
c56a9ba4 AC |
10516 | if In_Present (Spec) then |
10517 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10518 | else | |
10519 | Set_Ekind (Formal_Id, E_Out_Parameter); | |
10520 | end if; | |
10521 | ||
fc999c5d RD |
10522 | Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True); |
10523 | ||
b4ca2d2c AC |
10524 | -- But not in earlier versions of Ada |
10525 | ||
c56a9ba4 AC |
10526 | else |
10527 | Error_Msg_N ("functions can only have IN parameters", Spec); | |
10528 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10529 | end if; | |
996ae0b0 RK |
10530 | |
10531 | elsif In_Present (Spec) then | |
10532 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10533 | ||
10534 | else | |
fbf5a39b AC |
10535 | Set_Ekind (Formal_Id, E_Out_Parameter); |
10536 | Set_Never_Set_In_Source (Formal_Id, True); | |
10537 | Set_Is_True_Constant (Formal_Id, False); | |
10538 | Set_Current_Value (Formal_Id, Empty); | |
996ae0b0 RK |
10539 | end if; |
10540 | ||
10541 | else | |
10542 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10543 | end if; | |
10544 | ||
fbf5a39b | 10545 | -- Set Is_Known_Non_Null for access parameters since the language |
82c80734 RD |
10546 | -- guarantees that access parameters are always non-null. We also set |
10547 | -- Can_Never_Be_Null, since there is no way to change the value. | |
fbf5a39b AC |
10548 | |
10549 | if Nkind (Parameter_Type (Spec)) = N_Access_Definition then | |
2820d220 | 10550 | |
885c4871 | 10551 | -- Ada 2005 (AI-231): In Ada 95, access parameters are always non- |
2813bb6b | 10552 | -- null; In Ada 2005, only if then null_exclusion is explicit. |
2820d220 | 10553 | |
0791fbe9 | 10554 | if Ada_Version < Ada_2005 |
2813bb6b | 10555 | or else Can_Never_Be_Null (Etype (Formal_Id)) |
2820d220 AC |
10556 | then |
10557 | Set_Is_Known_Non_Null (Formal_Id); | |
10558 | Set_Can_Never_Be_Null (Formal_Id); | |
10559 | end if; | |
2813bb6b | 10560 | |
41251c60 JM |
10561 | -- Ada 2005 (AI-231): Null-exclusion access subtype |
10562 | ||
2813bb6b ES |
10563 | elsif Is_Access_Type (Etype (Formal_Id)) |
10564 | and then Can_Never_Be_Null (Etype (Formal_Id)) | |
10565 | then | |
2813bb6b | 10566 | Set_Is_Known_Non_Null (Formal_Id); |
a1d72281 EB |
10567 | |
10568 | -- We can also set Can_Never_Be_Null (thus preventing some junk | |
10569 | -- access checks) for the case of an IN parameter, which cannot | |
10570 | -- be changed, or for an IN OUT parameter, which can be changed but | |
10571 | -- not to a null value. But for an OUT parameter, the initial value | |
10572 | -- passed in can be null, so we can't set this flag in that case. | |
10573 | ||
10574 | if Ekind (Formal_Id) /= E_Out_Parameter then | |
10575 | Set_Can_Never_Be_Null (Formal_Id); | |
10576 | end if; | |
fbf5a39b AC |
10577 | end if; |
10578 | ||
996ae0b0 RK |
10579 | Set_Mechanism (Formal_Id, Default_Mechanism); |
10580 | Set_Formal_Validity (Formal_Id); | |
10581 | end Set_Formal_Mode; | |
10582 | ||
10583 | ------------------------- | |
10584 | -- Set_Formal_Validity -- | |
10585 | ------------------------- | |
10586 | ||
10587 | procedure Set_Formal_Validity (Formal_Id : Entity_Id) is | |
10588 | begin | |
82c80734 RD |
10589 | -- If no validity checking, then we cannot assume anything about the |
10590 | -- validity of parameters, since we do not know there is any checking | |
10591 | -- of the validity on the call side. | |
996ae0b0 RK |
10592 | |
10593 | if not Validity_Checks_On then | |
10594 | return; | |
10595 | ||
fbf5a39b AC |
10596 | -- If validity checking for parameters is enabled, this means we are |
10597 | -- not supposed to make any assumptions about argument values. | |
10598 | ||
10599 | elsif Validity_Check_Parameters then | |
10600 | return; | |
10601 | ||
10602 | -- If we are checking in parameters, we will assume that the caller is | |
10603 | -- also checking parameters, so we can assume the parameter is valid. | |
10604 | ||
996ae0b0 RK |
10605 | elsif Ekind (Formal_Id) = E_In_Parameter |
10606 | and then Validity_Check_In_Params | |
10607 | then | |
10608 | Set_Is_Known_Valid (Formal_Id, True); | |
10609 | ||
fbf5a39b AC |
10610 | -- Similar treatment for IN OUT parameters |
10611 | ||
996ae0b0 RK |
10612 | elsif Ekind (Formal_Id) = E_In_Out_Parameter |
10613 | and then Validity_Check_In_Out_Params | |
10614 | then | |
10615 | Set_Is_Known_Valid (Formal_Id, True); | |
10616 | end if; | |
10617 | end Set_Formal_Validity; | |
10618 | ||
10619 | ------------------------ | |
10620 | -- Subtype_Conformant -- | |
10621 | ------------------------ | |
10622 | ||
ce2b6ba5 JM |
10623 | function Subtype_Conformant |
10624 | (New_Id : Entity_Id; | |
10625 | Old_Id : Entity_Id; | |
10626 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10627 | is | |
996ae0b0 | 10628 | Result : Boolean; |
996ae0b0 | 10629 | begin |
ce2b6ba5 JM |
10630 | Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result, |
10631 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10632 | return Result; |
10633 | end Subtype_Conformant; | |
10634 | ||
10635 | --------------------- | |
10636 | -- Type_Conformant -- | |
10637 | --------------------- | |
10638 | ||
41251c60 JM |
10639 | function Type_Conformant |
10640 | (New_Id : Entity_Id; | |
10641 | Old_Id : Entity_Id; | |
10642 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10643 | is | |
996ae0b0 | 10644 | Result : Boolean; |
996ae0b0 | 10645 | begin |
c8ef728f | 10646 | May_Hide_Profile := False; |
41251c60 JM |
10647 | Check_Conformance |
10648 | (New_Id, Old_Id, Type_Conformant, False, Result, | |
10649 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10650 | return Result; |
10651 | end Type_Conformant; | |
10652 | ||
10653 | ------------------------------- | |
10654 | -- Valid_Operator_Definition -- | |
10655 | ------------------------------- | |
10656 | ||
10657 | procedure Valid_Operator_Definition (Designator : Entity_Id) is | |
10658 | N : Integer := 0; | |
10659 | F : Entity_Id; | |
10660 | Id : constant Name_Id := Chars (Designator); | |
10661 | N_OK : Boolean; | |
10662 | ||
10663 | begin | |
10664 | F := First_Formal (Designator); | |
996ae0b0 RK |
10665 | while Present (F) loop |
10666 | N := N + 1; | |
10667 | ||
10668 | if Present (Default_Value (F)) then | |
ed2233dc | 10669 | Error_Msg_N |
996ae0b0 RK |
10670 | ("default values not allowed for operator parameters", |
10671 | Parent (F)); | |
220d1fd9 AC |
10672 | |
10673 | -- For function instantiations that are operators, we must check | |
10674 | -- separately that the corresponding generic only has in-parameters. | |
84c0a895 AC |
10675 | -- For subprogram declarations this is done in Set_Formal_Mode. Such |
10676 | -- an error could not arise in earlier versions of the language. | |
220d1fd9 AC |
10677 | |
10678 | elsif Ekind (F) /= E_In_Parameter then | |
84c0a895 | 10679 | Error_Msg_N ("operators can only have IN parameters", F); |
996ae0b0 RK |
10680 | end if; |
10681 | ||
10682 | Next_Formal (F); | |
10683 | end loop; | |
10684 | ||
10685 | -- Verify that user-defined operators have proper number of arguments | |
10686 | -- First case of operators which can only be unary | |
10687 | ||
b69cd36a | 10688 | if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then |
996ae0b0 RK |
10689 | N_OK := (N = 1); |
10690 | ||
10691 | -- Case of operators which can be unary or binary | |
10692 | ||
b69cd36a | 10693 | elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then |
996ae0b0 RK |
10694 | N_OK := (N in 1 .. 2); |
10695 | ||
10696 | -- All other operators can only be binary | |
10697 | ||
10698 | else | |
10699 | N_OK := (N = 2); | |
10700 | end if; | |
10701 | ||
10702 | if not N_OK then | |
10703 | Error_Msg_N | |
10704 | ("incorrect number of arguments for operator", Designator); | |
10705 | end if; | |
10706 | ||
10707 | if Id = Name_Op_Ne | |
10708 | and then Base_Type (Etype (Designator)) = Standard_Boolean | |
10709 | and then not Is_Intrinsic_Subprogram (Designator) | |
10710 | then | |
10711 | Error_Msg_N | |
84c0a895 | 10712 | ("explicit definition of inequality not allowed", Designator); |
996ae0b0 RK |
10713 | end if; |
10714 | end Valid_Operator_Definition; | |
10715 | ||
10716 | end Sem_Ch6; |