]>
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 | -- -- |
2c6336be | 9 | -- Copyright (C) 1992-2015, 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; |
8636f52f | 43 | with Ghost; use Ghost; |
540d8610 | 44 | with Inline; use Inline; |
41251c60 | 45 | with Itypes; use Itypes; |
996ae0b0 | 46 | with Lib.Xref; use Lib.Xref; |
ec4867fa | 47 | with Layout; use Layout; |
996ae0b0 RK |
48 | with Namet; use Namet; |
49 | with Lib; use Lib; | |
50 | with Nlists; use Nlists; | |
51 | with Nmake; use Nmake; | |
52 | with Opt; use Opt; | |
53 | with Output; use Output; | |
b20de9b9 AC |
54 | with Restrict; use Restrict; |
55 | with Rident; use Rident; | |
996ae0b0 RK |
56 | with Rtsfind; use Rtsfind; |
57 | with Sem; use Sem; | |
a4100e55 | 58 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
59 | with Sem_Cat; use Sem_Cat; |
60 | with Sem_Ch3; use Sem_Ch3; | |
61 | with Sem_Ch4; use Sem_Ch4; | |
62 | with Sem_Ch5; use Sem_Ch5; | |
63 | with Sem_Ch8; use Sem_Ch8; | |
9bc856dd | 64 | with Sem_Ch10; use Sem_Ch10; |
996ae0b0 | 65 | with Sem_Ch12; use Sem_Ch12; |
0f1a6a0b | 66 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 67 | with Sem_Dim; use Sem_Dim; |
996ae0b0 RK |
68 | with Sem_Disp; use Sem_Disp; |
69 | with Sem_Dist; use Sem_Dist; | |
70 | with Sem_Elim; use Sem_Elim; | |
71 | with Sem_Eval; use Sem_Eval; | |
72 | with Sem_Mech; use Sem_Mech; | |
73 | with Sem_Prag; use Sem_Prag; | |
74 | with Sem_Res; use Sem_Res; | |
75 | with Sem_Util; use Sem_Util; | |
76 | with Sem_Type; use Sem_Type; | |
77 | with Sem_Warn; use Sem_Warn; | |
78 | with Sinput; use Sinput; | |
79 | with Stand; use Stand; | |
80 | with Sinfo; use Sinfo; | |
81 | with Sinfo.CN; use Sinfo.CN; | |
82 | with Snames; use Snames; | |
83 | with Stringt; use Stringt; | |
84 | with Style; | |
85 | with Stylesw; use Stylesw; | |
8417f4b2 | 86 | with Targparm; use Targparm; |
996ae0b0 RK |
87 | with Tbuild; use Tbuild; |
88 | with Uintp; use Uintp; | |
89 | with Urealp; use Urealp; | |
90 | with Validsw; use Validsw; | |
91 | ||
92 | package body Sem_Ch6 is | |
93 | ||
c8ef728f | 94 | May_Hide_Profile : Boolean := False; |
ec4867fa ES |
95 | -- This flag is used to indicate that two formals in two subprograms being |
96 | -- checked for conformance differ only in that one is an access parameter | |
97 | -- while the other is of a general access type with the same designated | |
98 | -- type. In this case, if the rest of the signatures match, a call to | |
99 | -- either subprogram may be ambiguous, which is worth a warning. The flag | |
100 | -- is set in Compatible_Types, and the warning emitted in | |
101 | -- New_Overloaded_Entity. | |
c8ef728f | 102 | |
996ae0b0 RK |
103 | ----------------------- |
104 | -- Local Subprograms -- | |
105 | ----------------------- | |
106 | ||
c9d70ab1 AC |
107 | procedure Analyze_Function_Return (N : Node_Id); |
108 | -- Subsidiary to Analyze_Return_Statement. Called when the return statement | |
109 | -- applies to a [generic] function. | |
110 | ||
111 | procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); | |
112 | -- Analyze a generic subprogram body. N is the body to be analyzed, and | |
113 | -- Gen_Id is the defining entity Id for the corresponding spec. | |
114 | ||
4d8f3296 ES |
115 | procedure Analyze_Null_Procedure |
116 | (N : Node_Id; | |
117 | Is_Completion : out Boolean); | |
9d2a2071 | 118 | -- A null procedure can be a declaration or (Ada 2012) a completion |
4d8f3296 | 119 | |
5d37ba92 | 120 | procedure Analyze_Return_Statement (N : Node_Id); |
5b9c3fc4 | 121 | -- Common processing for simple and extended return statements |
ec4867fa | 122 | |
82c80734 RD |
123 | procedure Analyze_Return_Type (N : Node_Id); |
124 | -- Subsidiary to Process_Formals: analyze subtype mark in function | |
5b9c3fc4 | 125 | -- specification in a context where the formals are visible and hide |
82c80734 RD |
126 | -- outer homographs. |
127 | ||
b1b543d2 | 128 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id); |
13d923cc RD |
129 | -- Does all the real work of Analyze_Subprogram_Body. This is split out so |
130 | -- that we can use RETURN but not skip the debug output at the end. | |
b1b543d2 | 131 | |
806f6d37 AC |
132 | function Can_Override_Operator (Subp : Entity_Id) return Boolean; |
133 | -- Returns true if Subp can override a predefined operator. | |
134 | ||
996ae0b0 | 135 | procedure Check_Conformance |
41251c60 JM |
136 | (New_Id : Entity_Id; |
137 | Old_Id : Entity_Id; | |
138 | Ctype : Conformance_Type; | |
139 | Errmsg : Boolean; | |
140 | Conforms : out Boolean; | |
141 | Err_Loc : Node_Id := Empty; | |
142 | Get_Inst : Boolean := False; | |
143 | Skip_Controlling_Formals : Boolean := False); | |
996ae0b0 RK |
144 | -- Given two entities, this procedure checks that the profiles associated |
145 | -- with these entities meet the conformance criterion given by the third | |
146 | -- parameter. If they conform, Conforms is set True and control returns | |
147 | -- to the caller. If they do not conform, Conforms is set to False, and | |
148 | -- in addition, if Errmsg is True on the call, proper messages are output | |
149 | -- to complain about the conformance failure. If Err_Loc is non_Empty | |
150 | -- the error messages are placed on Err_Loc, if Err_Loc is empty, then | |
151 | -- error messages are placed on the appropriate part of the construct | |
152 | -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance | |
153 | -- against a formal access-to-subprogram type so Get_Instance_Of must | |
154 | -- be called. | |
155 | ||
156 | procedure Check_Subprogram_Order (N : Node_Id); | |
157 | -- N is the N_Subprogram_Body node for a subprogram. This routine applies | |
158 | -- the alpha ordering rule for N if this ordering requirement applicable. | |
159 | ||
996ae0b0 RK |
160 | procedure Check_Returns |
161 | (HSS : Node_Id; | |
162 | Mode : Character; | |
c8ef728f ES |
163 | Err : out Boolean; |
164 | Proc : Entity_Id := Empty); | |
165 | -- Called to check for missing return statements in a function body, or for | |
0a36105d | 166 | -- returns present in a procedure body which has No_Return set. HSS is the |
c8ef728f ES |
167 | -- handled statement sequence for the subprogram body. This procedure |
168 | -- checks all flow paths to make sure they either have return (Mode = 'F', | |
169 | -- used for functions) or do not have a return (Mode = 'P', used for | |
170 | -- No_Return procedures). The flag Err is set if there are any control | |
171 | -- paths not explicitly terminated by a return in the function case, and is | |
172 | -- True otherwise. Proc is the entity for the procedure case and is used | |
173 | -- in posting the warning message. | |
996ae0b0 | 174 | |
e5a58fac AC |
175 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id); |
176 | -- In Ada 2012, a primitive equality operator on an untagged record type | |
177 | -- must appear before the type is frozen, and have the same visibility as | |
178 | -- that of the type. This procedure checks that this rule is met, and | |
179 | -- otherwise emits an error on the subprogram declaration and a warning | |
b2834fbd AC |
180 | -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode, |
181 | -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier | |
182 | -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility | |
183 | -- is set, otherwise the call has no effect. | |
e5a58fac | 184 | |
996ae0b0 | 185 | procedure Enter_Overloaded_Entity (S : Entity_Id); |
82c80734 RD |
186 | -- This procedure makes S, a new overloaded entity, into the first visible |
187 | -- entity with that name. | |
996ae0b0 | 188 | |
a5b62485 AC |
189 | function Is_Non_Overriding_Operation |
190 | (Prev_E : Entity_Id; | |
191 | New_E : Entity_Id) return Boolean; | |
192 | -- Enforce the rule given in 12.3(18): a private operation in an instance | |
193 | -- overrides an inherited operation only if the corresponding operation | |
260359e3 AC |
194 | -- was overriding in the generic. This needs to be checked for primitive |
195 | -- operations of types derived (in the generic unit) from formal private | |
196 | -- or formal derived types. | |
a5b62485 | 197 | |
996ae0b0 RK |
198 | procedure Make_Inequality_Operator (S : Entity_Id); |
199 | -- Create the declaration for an inequality operator that is implicitly | |
200 | -- created by a user-defined equality operator that yields a boolean. | |
201 | ||
996ae0b0 RK |
202 | procedure Set_Formal_Validity (Formal_Id : Entity_Id); |
203 | -- Formal_Id is an formal parameter entity. This procedure deals with | |
e358346d AC |
204 | -- setting the proper validity status for this entity, which depends on |
205 | -- the kind of parameter and the validity checking mode. | |
996ae0b0 RK |
206 | |
207 | --------------------------------------------- | |
208 | -- Analyze_Abstract_Subprogram_Declaration -- | |
209 | --------------------------------------------- | |
210 | ||
211 | procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is | |
fbf5a39b AC |
212 | Designator : constant Entity_Id := |
213 | Analyze_Subprogram_Specification (Specification (N)); | |
996ae0b0 RK |
214 | Scop : constant Entity_Id := Current_Scope; |
215 | ||
216 | begin | |
8636f52f HK |
217 | -- The abstract subprogram declaration may be subject to pragma Ghost |
218 | -- with policy Ignore. Set the mode now to ensure that any nodes | |
219 | -- generated during analysis and expansion are properly flagged as | |
220 | -- ignored Ghost. | |
221 | ||
222 | Set_Ghost_Mode (N); | |
ce5ba43a | 223 | Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N); |
38171f43 | 224 | |
996ae0b0 | 225 | Generate_Definition (Designator); |
c9d70ab1 | 226 | |
f937473f | 227 | Set_Is_Abstract_Subprogram (Designator); |
996ae0b0 RK |
228 | New_Overloaded_Entity (Designator); |
229 | Check_Delayed_Subprogram (Designator); | |
230 | ||
fbf5a39b | 231 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 | 232 | |
8636f52f | 233 | -- An abstract subprogram declared within a Ghost region is rendered |
c5cec2fe AC |
234 | -- Ghost (SPARK RM 6.9(2)). |
235 | ||
8636f52f | 236 | if Comes_From_Source (Designator) and then Ghost_Mode > None then |
c5cec2fe AC |
237 | Set_Is_Ghost_Entity (Designator); |
238 | end if; | |
239 | ||
996ae0b0 RK |
240 | if Ekind (Scope (Designator)) = E_Protected_Type then |
241 | Error_Msg_N | |
242 | ("abstract subprogram not allowed in protected type", N); | |
5d37ba92 ES |
243 | |
244 | -- Issue a warning if the abstract subprogram is neither a dispatching | |
245 | -- operation nor an operation that overrides an inherited subprogram or | |
246 | -- predefined operator, since this most likely indicates a mistake. | |
247 | ||
248 | elsif Warn_On_Redundant_Constructs | |
249 | and then not Is_Dispatching_Operation (Designator) | |
038140ed | 250 | and then not Present (Overridden_Operation (Designator)) |
5d37ba92 ES |
251 | and then (not Is_Operator_Symbol_Name (Chars (Designator)) |
252 | or else Scop /= Scope (Etype (First_Formal (Designator)))) | |
253 | then | |
254 | Error_Msg_N | |
dbfeb4fa | 255 | ("abstract subprogram is not dispatching or overriding?r?", N); |
996ae0b0 | 256 | end if; |
fbf5a39b AC |
257 | |
258 | Generate_Reference_To_Formals (Designator); | |
361effb1 | 259 | Check_Eliminated (Designator); |
eaba57fb RD |
260 | |
261 | if Has_Aspects (N) then | |
262 | Analyze_Aspect_Specifications (N, Designator); | |
263 | end if; | |
996ae0b0 RK |
264 | end Analyze_Abstract_Subprogram_Declaration; |
265 | ||
b0186f71 AC |
266 | --------------------------------- |
267 | -- Analyze_Expression_Function -- | |
268 | --------------------------------- | |
269 | ||
270 | procedure Analyze_Expression_Function (N : Node_Id) is | |
8d1fe980 AC |
271 | Expr : constant Node_Id := Expression (N); |
272 | Loc : constant Source_Ptr := Sloc (N); | |
273 | LocX : constant Source_Ptr := Sloc (Expr); | |
274 | Spec : constant Node_Id := Specification (N); | |
d2d4b355 | 275 | |
8a06151a | 276 | Def_Id : Entity_Id; |
b0186f71 | 277 | |
8a06151a | 278 | Prev : Entity_Id; |
b0186f71 | 279 | -- If the expression is a completion, Prev is the entity whose |
d2d4b355 AC |
280 | -- declaration is completed. Def_Id is needed to analyze the spec. |
281 | ||
282 | New_Body : Node_Id; | |
d2d4b355 | 283 | New_Spec : Node_Id; |
b913199e | 284 | Ret : Node_Id; |
b0186f71 AC |
285 | |
286 | begin | |
287 | -- This is one of the occasions on which we transform the tree during | |
afc8324d | 288 | -- semantic analysis. If this is a completion, transform the expression |
d2b10647 ES |
289 | -- function into an equivalent subprogram body, and analyze it. |
290 | ||
291 | -- Expression functions are inlined unconditionally. The back-end will | |
292 | -- determine whether this is possible. | |
293 | ||
294 | Inline_Processing_Required := True; | |
b727a82b | 295 | |
8d1fe980 AC |
296 | -- Create a specification for the generated body. This must be done |
297 | -- prior to the analysis of the initial declaration. | |
b727a82b | 298 | |
8d1fe980 AC |
299 | New_Spec := Copy_Subprogram_Spec (Spec); |
300 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
d2d4b355 AC |
301 | |
302 | -- If there are previous overloadable entities with the same name, | |
303 | -- check whether any of them is completed by the expression function. | |
b04d926e | 304 | -- In a generic context a formal subprogram has no completion. |
d2d4b355 | 305 | |
4b6f99f5 RD |
306 | if Present (Prev) |
307 | and then Is_Overloadable (Prev) | |
b04d926e AC |
308 | and then not Is_Formal_Subprogram (Prev) |
309 | then | |
51597c23 AC |
310 | Def_Id := Analyze_Subprogram_Specification (Spec); |
311 | Prev := Find_Corresponding_Spec (N); | |
35e7063a AC |
312 | |
313 | -- The previous entity may be an expression function as well, in | |
314 | -- which case the redeclaration is illegal. | |
315 | ||
316 | if Present (Prev) | |
5073ad7a AC |
317 | and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) = |
318 | N_Expression_Function | |
35e7063a | 319 | then |
bc5e261c ES |
320 | Error_Msg_Sloc := Sloc (Prev); |
321 | Error_Msg_N ("& conflicts with declaration#", Def_Id); | |
35e7063a AC |
322 | return; |
323 | end if; | |
d2d4b355 | 324 | end if; |
b0186f71 | 325 | |
b913199e AC |
326 | Ret := Make_Simple_Return_Statement (LocX, Expression (N)); |
327 | ||
b0186f71 AC |
328 | New_Body := |
329 | Make_Subprogram_Body (Loc, | |
d2d4b355 | 330 | Specification => New_Spec, |
b0186f71 AC |
331 | Declarations => Empty_List, |
332 | Handled_Statement_Sequence => | |
333 | Make_Handled_Sequence_Of_Statements (LocX, | |
b913199e | 334 | Statements => New_List (Ret))); |
b0186f71 | 335 | |
e7f23f06 AC |
336 | -- If the expression completes a generic subprogram, we must create a |
337 | -- separate node for the body, because at instantiation the original | |
338 | -- node of the generic copy must be a generic subprogram body, and | |
339 | -- cannot be a expression function. Otherwise we just rewrite the | |
340 | -- expression with the non-generic body. | |
341 | ||
6d7e5c54 | 342 | if Present (Prev) and then Ekind (Prev) = E_Generic_Function then |
e7f23f06 | 343 | Insert_After (N, New_Body); |
6d7e5c54 | 344 | |
e7f23f06 AC |
345 | -- Propagate any aspects or pragmas that apply to the expression |
346 | -- function to the proper body when the expression function acts | |
347 | -- as a completion. | |
348 | ||
349 | if Has_Aspects (N) then | |
350 | Move_Aspects (N, To => New_Body); | |
351 | end if; | |
352 | ||
353 | Relocate_Pragmas_To_Body (New_Body); | |
b0186f71 | 354 | |
b0186f71 | 355 | Rewrite (N, Make_Null_Statement (Loc)); |
d2d4b355 | 356 | Set_Has_Completion (Prev, False); |
b0186f71 AC |
357 | Analyze (N); |
358 | Analyze (New_Body); | |
d2b10647 | 359 | Set_Is_Inlined (Prev); |
b0186f71 | 360 | |
e5c4e2bc AC |
361 | -- If the expression function is a completion, the previous declaration |
362 | -- must come from source. We know already that appears in the current | |
363 | -- scope. The entity itself may be internally created if within a body | |
364 | -- to be inlined. | |
365 | ||
4b6f99f5 RD |
366 | elsif Present (Prev) |
367 | and then Comes_From_Source (Parent (Prev)) | |
b04d926e AC |
368 | and then not Is_Formal_Subprogram (Prev) |
369 | then | |
d2d4b355 | 370 | Set_Has_Completion (Prev, False); |
76264f60 | 371 | |
c6d2191a AC |
372 | -- An expression function that is a completion freezes the |
373 | -- expression. This means freezing the return type, and if it is | |
374 | -- an access type, freezing its designated type as well. | |
1b31321b | 375 | |
c6d2191a | 376 | -- Note that we cannot defer this freezing to the analysis of the |
1b31321b AC |
377 | -- expression itself, because a freeze node might appear in a nested |
378 | -- scope, leading to an elaboration order issue in gigi. | |
c6d2191a AC |
379 | |
380 | Freeze_Before (N, Etype (Prev)); | |
1b31321b | 381 | |
c6d2191a AC |
382 | if Is_Access_Type (Etype (Prev)) then |
383 | Freeze_Before (N, Designated_Type (Etype (Prev))); | |
384 | end if; | |
385 | ||
76264f60 AC |
386 | -- For navigation purposes, indicate that the function is a body |
387 | ||
388 | Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True); | |
b0186f71 | 389 | Rewrite (N, New_Body); |
e7f23f06 | 390 | |
c0cdbd39 AC |
391 | -- Correct the parent pointer of the aspect specification list to |
392 | -- reference the rewritten node. | |
393 | ||
394 | if Has_Aspects (N) then | |
395 | Set_Parent (Aspect_Specifications (N), N); | |
396 | end if; | |
397 | ||
e7f23f06 AC |
398 | -- Propagate any pragmas that apply to the expression function to the |
399 | -- proper body when the expression function acts as a completion. | |
400 | -- Aspects are automatically transfered because of node rewriting. | |
401 | ||
402 | Relocate_Pragmas_To_Body (N); | |
d2b10647 ES |
403 | Analyze (N); |
404 | ||
6d7e5c54 AC |
405 | -- Prev is the previous entity with the same name, but it is can |
406 | -- be an unrelated spec that is not completed by the expression | |
407 | -- function. In that case the relevant entity is the one in the body. | |
408 | -- Not clear that the backend can inline it in this case ??? | |
409 | ||
410 | if Has_Completion (Prev) then | |
411 | Set_Is_Inlined (Prev); | |
31af8899 AC |
412 | |
413 | -- The formals of the expression function are body formals, | |
414 | -- and do not appear in the ali file, which will only contain | |
415 | -- references to the formals of the original subprogram spec. | |
416 | ||
417 | declare | |
418 | F1 : Entity_Id; | |
419 | F2 : Entity_Id; | |
420 | ||
421 | begin | |
422 | F1 := First_Formal (Def_Id); | |
423 | F2 := First_Formal (Prev); | |
424 | ||
425 | while Present (F1) loop | |
426 | Set_Spec_Entity (F1, F2); | |
427 | Next_Formal (F1); | |
428 | Next_Formal (F2); | |
429 | end loop; | |
430 | end; | |
431 | ||
6d7e5c54 AC |
432 | else |
433 | Set_Is_Inlined (Defining_Entity (New_Body)); | |
434 | end if; | |
435 | ||
0b5b2bbc | 436 | -- If this is not a completion, create both a declaration and a body, so |
6d7e5c54 | 437 | -- that the expression can be inlined whenever possible. |
d2b10647 ES |
438 | |
439 | else | |
a52e6d7e AC |
440 | -- An expression function that is not a completion is not a |
441 | -- subprogram declaration, and thus cannot appear in a protected | |
442 | -- definition. | |
443 | ||
444 | if Nkind (Parent (N)) = N_Protected_Definition then | |
445 | Error_Msg_N | |
446 | ("an expression function is not a legal protected operation", N); | |
447 | end if; | |
448 | ||
b8e6830b | 449 | Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec)); |
c0cdbd39 AC |
450 | |
451 | -- Correct the parent pointer of the aspect specification list to | |
452 | -- reference the rewritten node. | |
453 | ||
454 | if Has_Aspects (N) then | |
455 | Set_Parent (Aspect_Specifications (N), N); | |
456 | end if; | |
457 | ||
b0186f71 | 458 | Analyze (N); |
b04d926e | 459 | |
e699b76e AC |
460 | -- Within a generic pre-analyze the original expression for name |
461 | -- capture. The body is also generated but plays no role in | |
462 | -- this because it is not part of the original source. | |
b04d926e AC |
463 | |
464 | if Inside_A_Generic then | |
465 | declare | |
466 | Id : constant Entity_Id := Defining_Entity (N); | |
b04d926e AC |
467 | |
468 | begin | |
469 | Set_Has_Completion (Id); | |
b04d926e AC |
470 | Push_Scope (Id); |
471 | Install_Formals (Id); | |
e699b76e | 472 | Preanalyze_Spec_Expression (Expr, Etype (Id)); |
b04d926e | 473 | End_Scope; |
b04d926e AC |
474 | end; |
475 | end if; | |
476 | ||
b8e6830b AC |
477 | Set_Is_Inlined (Defining_Entity (N)); |
478 | ||
479 | -- Establish the linkages between the spec and the body. These are | |
480 | -- used when the expression function acts as the prefix of attribute | |
481 | -- 'Access in order to freeze the original expression which has been | |
482 | -- moved to the generated body. | |
483 | ||
484 | Set_Corresponding_Body (N, Defining_Entity (New_Body)); | |
485 | Set_Corresponding_Spec (New_Body, Defining_Entity (N)); | |
d2b10647 | 486 | |
6d7e5c54 AC |
487 | -- To prevent premature freeze action, insert the new body at the end |
488 | -- of the current declarations, or at the end of the package spec. | |
b913199e | 489 | -- However, resolve usage names now, to prevent spurious visibility |
ad4e3362 ES |
490 | -- on later entities. Note that the function can now be called in |
491 | -- the current declarative part, which will appear to be prior to | |
492 | -- the presence of the body in the code. There are nevertheless no | |
493 | -- order of elaboration issues because all name resolution has taken | |
494 | -- place at the point of declaration. | |
6d7e5c54 AC |
495 | |
496 | declare | |
e876c43a AC |
497 | Decls : List_Id := List_Containing (N); |
498 | Par : constant Node_Id := Parent (Decls); | |
b8e6830b | 499 | Id : constant Entity_Id := Defining_Entity (N); |
6d7e5c54 AC |
500 | |
501 | begin | |
fce54763 AC |
502 | -- If this is a wrapper created for in an instance for a formal |
503 | -- subprogram, insert body after declaration, to be analyzed when | |
504 | -- the enclosing instance is analyzed. | |
505 | ||
506 | if GNATprove_Mode | |
507 | and then Is_Generic_Actual_Subprogram (Defining_Entity (N)) | |
6d7e5c54 | 508 | then |
fce54763 AC |
509 | Insert_After (N, New_Body); |
510 | ||
511 | else | |
512 | if Nkind (Par) = N_Package_Specification | |
513 | and then Decls = Visible_Declarations (Par) | |
514 | and then Present (Private_Declarations (Par)) | |
515 | and then not Is_Empty_List (Private_Declarations (Par)) | |
516 | then | |
517 | Decls := Private_Declarations (Par); | |
518 | end if; | |
6d7e5c54 | 519 | |
fce54763 AC |
520 | Insert_After (Last (Decls), New_Body); |
521 | Push_Scope (Id); | |
522 | Install_Formals (Id); | |
3a8e3f63 | 523 | |
fce54763 AC |
524 | -- Preanalyze the expression for name capture, except in an |
525 | -- instance, where this has been done during generic analysis, | |
526 | -- and will be redone when analyzing the body. | |
845f06e2 | 527 | |
fce54763 AC |
528 | declare |
529 | Expr : constant Node_Id := Expression (Ret); | |
4058ddcc | 530 | |
fce54763 AC |
531 | begin |
532 | Set_Parent (Expr, Ret); | |
4058ddcc | 533 | |
fce54763 AC |
534 | if not In_Instance then |
535 | Preanalyze_Spec_Expression (Expr, Etype (Id)); | |
536 | end if; | |
537 | end; | |
3a8e3f63 | 538 | |
fce54763 AC |
539 | End_Scope; |
540 | end if; | |
6d7e5c54 | 541 | end; |
b0186f71 | 542 | end if; |
0b5b2bbc AC |
543 | |
544 | -- If the return expression is a static constant, we suppress warning | |
545 | -- messages on unused formals, which in most cases will be noise. | |
546 | ||
547 | Set_Is_Trivial_Subprogram (Defining_Entity (New_Body), | |
548 | Is_OK_Static_Expression (Expr)); | |
b0186f71 AC |
549 | end Analyze_Expression_Function; |
550 | ||
ec4867fa ES |
551 | ---------------------------------------- |
552 | -- Analyze_Extended_Return_Statement -- | |
553 | ---------------------------------------- | |
554 | ||
555 | procedure Analyze_Extended_Return_Statement (N : Node_Id) is | |
556 | begin | |
c86cf714 | 557 | Check_Compiler_Unit ("extended return statement", N); |
5d37ba92 | 558 | Analyze_Return_Statement (N); |
ec4867fa ES |
559 | end Analyze_Extended_Return_Statement; |
560 | ||
996ae0b0 RK |
561 | ---------------------------- |
562 | -- Analyze_Function_Call -- | |
563 | ---------------------------- | |
564 | ||
565 | procedure Analyze_Function_Call (N : Node_Id) is | |
a7e68e7f HK |
566 | Actuals : constant List_Id := Parameter_Associations (N); |
567 | Func_Nam : constant Node_Id := Name (N); | |
568 | Actual : Node_Id; | |
569 | ||
996ae0b0 | 570 | begin |
a7e68e7f | 571 | Analyze (Func_Nam); |
996ae0b0 | 572 | |
3e7302c3 AC |
573 | -- A call of the form A.B (X) may be an Ada 2005 call, which is |
574 | -- rewritten as B (A, X). If the rewriting is successful, the call | |
575 | -- has been analyzed and we just return. | |
82c80734 | 576 | |
a7e68e7f HK |
577 | if Nkind (Func_Nam) = N_Selected_Component |
578 | and then Name (N) /= Func_Nam | |
82c80734 RD |
579 | and then Is_Rewrite_Substitution (N) |
580 | and then Present (Etype (N)) | |
581 | then | |
582 | return; | |
583 | end if; | |
584 | ||
996ae0b0 RK |
585 | -- If error analyzing name, then set Any_Type as result type and return |
586 | ||
a7e68e7f | 587 | if Etype (Func_Nam) = Any_Type then |
996ae0b0 RK |
588 | Set_Etype (N, Any_Type); |
589 | return; | |
590 | end if; | |
591 | ||
592 | -- Otherwise analyze the parameters | |
593 | ||
e24329cd YM |
594 | if Present (Actuals) then |
595 | Actual := First (Actuals); | |
996ae0b0 RK |
596 | while Present (Actual) loop |
597 | Analyze (Actual); | |
598 | Check_Parameterless_Call (Actual); | |
599 | Next (Actual); | |
600 | end loop; | |
601 | end if; | |
602 | ||
603 | Analyze_Call (N); | |
996ae0b0 RK |
604 | end Analyze_Function_Call; |
605 | ||
ec4867fa ES |
606 | ----------------------------- |
607 | -- Analyze_Function_Return -- | |
608 | ----------------------------- | |
609 | ||
610 | procedure Analyze_Function_Return (N : Node_Id) is | |
a7e68e7f HK |
611 | Loc : constant Source_Ptr := Sloc (N); |
612 | Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); | |
613 | Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); | |
ec4867fa | 614 | |
5d37ba92 | 615 | R_Type : constant Entity_Id := Etype (Scope_Id); |
ec4867fa ES |
616 | -- Function result subtype |
617 | ||
618 | procedure Check_Limited_Return (Expr : Node_Id); | |
619 | -- Check the appropriate (Ada 95 or Ada 2005) rules for returning | |
620 | -- limited types. Used only for simple return statements. | |
621 | -- Expr is the expression returned. | |
622 | ||
623 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); | |
624 | -- Check that the return_subtype_indication properly matches the result | |
625 | -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). | |
626 | ||
627 | -------------------------- | |
628 | -- Check_Limited_Return -- | |
629 | -------------------------- | |
630 | ||
631 | procedure Check_Limited_Return (Expr : Node_Id) is | |
632 | begin | |
633 | -- Ada 2005 (AI-318-02): Return-by-reference types have been | |
634 | -- removed and replaced by anonymous access results. This is an | |
635 | -- incompatibility with Ada 95. Not clear whether this should be | |
636 | -- enforced yet or perhaps controllable with special switch. ??? | |
637 | ||
ce72a9a3 AC |
638 | -- A limited interface that is not immutably limited is OK. |
639 | ||
640 | if Is_Limited_Interface (R_Type) | |
641 | and then | |
642 | not (Is_Task_Interface (R_Type) | |
643 | or else Is_Protected_Interface (R_Type) | |
644 | or else Is_Synchronized_Interface (R_Type)) | |
645 | then | |
646 | null; | |
647 | ||
648 | elsif Is_Limited_Type (R_Type) | |
649 | and then not Is_Interface (R_Type) | |
ec4867fa ES |
650 | and then Comes_From_Source (N) |
651 | and then not In_Instance_Body | |
2a31c32b | 652 | and then not OK_For_Limited_Init_In_05 (R_Type, Expr) |
ec4867fa ES |
653 | then |
654 | -- Error in Ada 2005 | |
655 | ||
0791fbe9 | 656 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
657 | and then not Debug_Flag_Dot_L |
658 | and then not GNAT_Mode | |
659 | then | |
660 | Error_Msg_N | |
3ccedacc AC |
661 | ("(Ada 2005) cannot copy object of a limited type " |
662 | & "(RM-2005 6.5(5.5/2))", Expr); | |
e0ae93e2 | 663 | |
51245e2d | 664 | if Is_Limited_View (R_Type) then |
ec4867fa ES |
665 | Error_Msg_N |
666 | ("\return by reference not permitted in Ada 2005", Expr); | |
667 | end if; | |
668 | ||
669 | -- Warn in Ada 95 mode, to give folks a heads up about this | |
670 | -- incompatibility. | |
671 | ||
672 | -- In GNAT mode, this is just a warning, to allow it to be | |
673 | -- evilly turned off. Otherwise it is a real error. | |
674 | ||
9694c039 AC |
675 | -- In a generic context, simplify the warning because it makes |
676 | -- no sense to discuss pass-by-reference or copy. | |
677 | ||
ec4867fa | 678 | elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then |
9694c039 AC |
679 | if Inside_A_Generic then |
680 | Error_Msg_N | |
885c4871 | 681 | ("return of limited object not permitted in Ada 2005 " |
dbfeb4fa | 682 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
9694c039 | 683 | |
51245e2d | 684 | elsif Is_Limited_View (R_Type) then |
ec4867fa | 685 | Error_Msg_N |
20261dc1 | 686 | ("return by reference not permitted in Ada 2005 " |
dbfeb4fa | 687 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
ec4867fa ES |
688 | else |
689 | Error_Msg_N | |
20261dc1 | 690 | ("cannot copy object of a limited type in Ada 2005 " |
dbfeb4fa | 691 | & "(RM-2005 6.5(5.5/2))?y?", Expr); |
ec4867fa ES |
692 | end if; |
693 | ||
694 | -- Ada 95 mode, compatibility warnings disabled | |
695 | ||
696 | else | |
697 | return; -- skip continuation messages below | |
698 | end if; | |
699 | ||
9694c039 AC |
700 | if not Inside_A_Generic then |
701 | Error_Msg_N | |
702 | ("\consider switching to return of access type", Expr); | |
703 | Explain_Limited_Type (R_Type, Expr); | |
704 | end if; | |
ec4867fa ES |
705 | end if; |
706 | end Check_Limited_Return; | |
707 | ||
708 | ------------------------------------- | |
709 | -- Check_Return_Subtype_Indication -- | |
710 | ------------------------------------- | |
711 | ||
712 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is | |
7665e4bd AC |
713 | Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); |
714 | ||
715 | R_Stm_Type : constant Entity_Id := Etype (Return_Obj); | |
716 | -- Subtype given in the extended return statement (must match R_Type) | |
ec4867fa ES |
717 | |
718 | Subtype_Ind : constant Node_Id := | |
719 | Object_Definition (Original_Node (Obj_Decl)); | |
720 | ||
7f568bfa AC |
721 | R_Type_Is_Anon_Access : constant Boolean := |
722 | Ekind_In (R_Type, | |
723 | E_Anonymous_Access_Subprogram_Type, | |
724 | E_Anonymous_Access_Protected_Subprogram_Type, | |
725 | E_Anonymous_Access_Type); | |
ec4867fa ES |
726 | -- True if return type of the function is an anonymous access type |
727 | -- Can't we make Is_Anonymous_Access_Type in einfo ??? | |
728 | ||
7f568bfa AC |
729 | R_Stm_Type_Is_Anon_Access : constant Boolean := |
730 | Ekind_In (R_Stm_Type, | |
731 | E_Anonymous_Access_Subprogram_Type, | |
732 | E_Anonymous_Access_Protected_Subprogram_Type, | |
733 | E_Anonymous_Access_Type); | |
ec4867fa ES |
734 | -- True if type of the return object is an anonymous access type |
735 | ||
7f568bfa AC |
736 | procedure Error_No_Match (N : Node_Id); |
737 | -- Output error messages for case where types do not statically | |
738 | -- match. N is the location for the messages. | |
739 | ||
740 | -------------------- | |
741 | -- Error_No_Match -- | |
742 | -------------------- | |
743 | ||
744 | procedure Error_No_Match (N : Node_Id) is | |
745 | begin | |
746 | Error_Msg_N | |
747 | ("subtype must statically match function result subtype", N); | |
748 | ||
749 | if not Predicates_Match (R_Stm_Type, R_Type) then | |
750 | Error_Msg_Node_2 := R_Type; | |
751 | Error_Msg_NE | |
3ccedacc | 752 | ("\predicate of& does not match predicate of&", |
7f568bfa AC |
753 | N, R_Stm_Type); |
754 | end if; | |
755 | end Error_No_Match; | |
756 | ||
757 | -- Start of processing for Check_Return_Subtype_Indication | |
758 | ||
ec4867fa | 759 | begin |
7665e4bd | 760 | -- First, avoid cascaded errors |
ec4867fa ES |
761 | |
762 | if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then | |
763 | return; | |
764 | end if; | |
765 | ||
766 | -- "return access T" case; check that the return statement also has | |
767 | -- "access T", and that the subtypes statically match: | |
53cf4600 | 768 | -- if this is an access to subprogram the signatures must match. |
ec4867fa ES |
769 | |
770 | if R_Type_Is_Anon_Access then | |
771 | if R_Stm_Type_Is_Anon_Access then | |
53cf4600 ES |
772 | if |
773 | Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type | |
0a36105d | 774 | then |
53cf4600 ES |
775 | if Base_Type (Designated_Type (R_Stm_Type)) /= |
776 | Base_Type (Designated_Type (R_Type)) | |
777 | or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) | |
778 | then | |
7f568bfa | 779 | Error_No_Match (Subtype_Mark (Subtype_Ind)); |
53cf4600 ES |
780 | end if; |
781 | ||
782 | else | |
783 | -- For two anonymous access to subprogram types, the | |
784 | -- types themselves must be type conformant. | |
785 | ||
786 | if not Conforming_Types | |
787 | (R_Stm_Type, R_Type, Fully_Conformant) | |
788 | then | |
7f568bfa | 789 | Error_No_Match (Subtype_Ind); |
53cf4600 | 790 | end if; |
ec4867fa | 791 | end if; |
0a36105d | 792 | |
ec4867fa ES |
793 | else |
794 | Error_Msg_N ("must use anonymous access type", Subtype_Ind); | |
795 | end if; | |
796 | ||
6cce2156 GD |
797 | -- If the return object is of an anonymous access type, then report |
798 | -- an error if the function's result type is not also anonymous. | |
799 | ||
800 | elsif R_Stm_Type_Is_Anon_Access | |
801 | and then not R_Type_Is_Anon_Access | |
802 | then | |
3ccedacc AC |
803 | Error_Msg_N ("anonymous access not allowed for function with " |
804 | & "named access result", Subtype_Ind); | |
6cce2156 | 805 | |
81d93365 AC |
806 | -- Subtype indication case: check that the return object's type is |
807 | -- covered by the result type, and that the subtypes statically match | |
808 | -- when the result subtype is constrained. Also handle record types | |
809 | -- with unknown discriminants for which we have built the underlying | |
810 | -- record view. Coverage is needed to allow specific-type return | |
811 | -- objects when the result type is class-wide (see AI05-32). | |
812 | ||
813 | elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) | |
9013065b | 814 | or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) |
212863c0 AC |
815 | and then |
816 | Covers | |
817 | (Base_Type (R_Type), | |
818 | Underlying_Record_View (Base_Type (R_Stm_Type)))) | |
9013065b AC |
819 | then |
820 | -- A null exclusion may be present on the return type, on the | |
821 | -- function specification, on the object declaration or on the | |
822 | -- subtype itself. | |
ec4867fa | 823 | |
21d27997 RD |
824 | if Is_Access_Type (R_Type) |
825 | and then | |
826 | (Can_Never_Be_Null (R_Type) | |
827 | or else Null_Exclusion_Present (Parent (Scope_Id))) /= | |
828 | Can_Never_Be_Null (R_Stm_Type) | |
829 | then | |
7f568bfa | 830 | Error_No_Match (Subtype_Ind); |
21d27997 RD |
831 | end if; |
832 | ||
105b5e65 | 833 | -- AI05-103: for elementary types, subtypes must statically match |
8779dffa AC |
834 | |
835 | if Is_Constrained (R_Type) | |
836 | or else Is_Access_Type (R_Type) | |
837 | then | |
ec4867fa | 838 | if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then |
7f568bfa | 839 | Error_No_Match (Subtype_Ind); |
ec4867fa ES |
840 | end if; |
841 | end if; | |
842 | ||
a8b346d2 RD |
843 | -- All remaining cases are illegal |
844 | ||
845 | -- Note: previous versions of this subprogram allowed the return | |
846 | -- value to be the ancestor of the return type if the return type | |
847 | -- was a null extension. This was plainly incorrect. | |
ff7139c3 | 848 | |
ec4867fa ES |
849 | else |
850 | Error_Msg_N | |
851 | ("wrong type for return_subtype_indication", Subtype_Ind); | |
852 | end if; | |
853 | end Check_Return_Subtype_Indication; | |
854 | ||
855 | --------------------- | |
856 | -- Local Variables -- | |
857 | --------------------- | |
858 | ||
445e5888 AC |
859 | Expr : Node_Id; |
860 | Obj_Decl : Node_Id; | |
ec4867fa ES |
861 | |
862 | -- Start of processing for Analyze_Function_Return | |
863 | ||
864 | begin | |
865 | Set_Return_Present (Scope_Id); | |
866 | ||
5d37ba92 | 867 | if Nkind (N) = N_Simple_Return_Statement then |
ec4867fa | 868 | Expr := Expression (N); |
4ee646da | 869 | |
e917aec2 RD |
870 | -- Guard against a malformed expression. The parser may have tried to |
871 | -- recover but the node is not analyzable. | |
4ee646da AC |
872 | |
873 | if Nkind (Expr) = N_Error then | |
874 | Set_Etype (Expr, Any_Type); | |
875 | Expander_Mode_Save_And_Set (False); | |
876 | return; | |
877 | ||
878 | else | |
0180fd26 AC |
879 | -- The resolution of a controlled [extension] aggregate associated |
880 | -- with a return statement creates a temporary which needs to be | |
881 | -- finalized on function exit. Wrap the return statement inside a | |
882 | -- block so that the finalization machinery can detect this case. | |
883 | -- This early expansion is done only when the return statement is | |
884 | -- not part of a handled sequence of statements. | |
885 | ||
886 | if Nkind_In (Expr, N_Aggregate, | |
887 | N_Extension_Aggregate) | |
888 | and then Needs_Finalization (R_Type) | |
889 | and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements | |
890 | then | |
891 | Rewrite (N, | |
892 | Make_Block_Statement (Loc, | |
893 | Handled_Statement_Sequence => | |
894 | Make_Handled_Sequence_Of_Statements (Loc, | |
895 | Statements => New_List (Relocate_Node (N))))); | |
896 | ||
897 | Analyze (N); | |
898 | return; | |
899 | end if; | |
900 | ||
4b963531 AC |
901 | Analyze (Expr); |
902 | ||
903 | -- Ada 2005 (AI-251): If the type of the returned object is | |
904 | -- an access to an interface type then we add an implicit type | |
905 | -- conversion to force the displacement of the "this" pointer to | |
906 | -- reference the secondary dispatch table. We cannot delay the | |
907 | -- generation of this implicit conversion until the expansion | |
908 | -- because in this case the type resolution changes the decoration | |
909 | -- of the expression node to match R_Type; by contrast, if the | |
910 | -- returned object is a class-wide interface type then it is too | |
911 | -- early to generate here the implicit conversion since the return | |
912 | -- statement may be rewritten by the expander into an extended | |
913 | -- return statement whose expansion takes care of adding the | |
914 | -- implicit type conversion to displace the pointer to the object. | |
915 | ||
916 | if Expander_Active | |
917 | and then Serious_Errors_Detected = 0 | |
918 | and then Is_Access_Type (R_Type) | |
919 | and then Nkind (Expr) /= N_Null | |
920 | and then Is_Interface (Designated_Type (R_Type)) | |
921 | and then Is_Progenitor (Designated_Type (R_Type), | |
922 | Designated_Type (Etype (Expr))) | |
923 | then | |
73e5aa55 | 924 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); |
4b963531 AC |
925 | Analyze (Expr); |
926 | end if; | |
927 | ||
928 | Resolve (Expr, R_Type); | |
4ee646da AC |
929 | Check_Limited_Return (Expr); |
930 | end if; | |
ec4867fa | 931 | |
ad05f2e9 | 932 | -- RETURN only allowed in SPARK as the last statement in function |
607d0635 | 933 | |
fe5d3068 | 934 | if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements |
607d0635 AC |
935 | and then |
936 | (Nkind (Parent (Parent (N))) /= N_Subprogram_Body | |
8d606a78 | 937 | or else Present (Next (N))) |
607d0635 | 938 | then |
ce5ba43a | 939 | Check_SPARK_05_Restriction |
fe5d3068 | 940 | ("RETURN should be the last statement in function", N); |
607d0635 AC |
941 | end if; |
942 | ||
ec4867fa | 943 | else |
ce5ba43a | 944 | Check_SPARK_05_Restriction ("extended RETURN is not allowed", N); |
445e5888 | 945 | Obj_Decl := Last (Return_Object_Declarations (N)); |
607d0635 | 946 | |
ec4867fa ES |
947 | -- Analyze parts specific to extended_return_statement: |
948 | ||
949 | declare | |
de6cad7c | 950 | Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl); |
b9daa96e | 951 | HSS : constant Node_Id := Handled_Statement_Sequence (N); |
ec4867fa ES |
952 | |
953 | begin | |
954 | Expr := Expression (Obj_Decl); | |
955 | ||
956 | -- Note: The check for OK_For_Limited_Init will happen in | |
957 | -- Analyze_Object_Declaration; we treat it as a normal | |
958 | -- object declaration. | |
959 | ||
cd1c668b | 960 | Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); |
ec4867fa ES |
961 | Analyze (Obj_Decl); |
962 | ||
ec4867fa ES |
963 | Check_Return_Subtype_Indication (Obj_Decl); |
964 | ||
965 | if Present (HSS) then | |
966 | Analyze (HSS); | |
967 | ||
968 | if Present (Exception_Handlers (HSS)) then | |
969 | ||
970 | -- ???Has_Nested_Block_With_Handler needs to be set. | |
971 | -- Probably by creating an actual N_Block_Statement. | |
972 | -- Probably in Expand. | |
973 | ||
974 | null; | |
975 | end if; | |
976 | end if; | |
977 | ||
9337aa0a AC |
978 | -- Mark the return object as referenced, since the return is an |
979 | -- implicit reference of the object. | |
980 | ||
981 | Set_Referenced (Defining_Identifier (Obj_Decl)); | |
982 | ||
ec4867fa | 983 | Check_References (Stm_Entity); |
de6cad7c AC |
984 | |
985 | -- Check RM 6.5 (5.9/3) | |
986 | ||
987 | if Has_Aliased then | |
988 | if Ada_Version < Ada_2012 then | |
dbfeb4fa RD |
989 | |
990 | -- Shouldn't this test Warn_On_Ada_2012_Compatibility ??? | |
991 | -- Can it really happen (extended return???) | |
992 | ||
993 | Error_Msg_N | |
b785e0b8 AC |
994 | ("aliased only allowed for limited return objects " |
995 | & "in Ada 2012??", N); | |
de6cad7c | 996 | |
51245e2d | 997 | elsif not Is_Limited_View (R_Type) then |
3ccedacc AC |
998 | Error_Msg_N |
999 | ("aliased only allowed for limited return objects", N); | |
de6cad7c AC |
1000 | end if; |
1001 | end if; | |
ec4867fa ES |
1002 | end; |
1003 | end if; | |
1004 | ||
21d27997 | 1005 | -- Case of Expr present |
5d37ba92 | 1006 | |
ec4867fa | 1007 | if Present (Expr) |
21d27997 | 1008 | |
8fde064e | 1009 | -- Defend against previous errors |
21d27997 RD |
1010 | |
1011 | and then Nkind (Expr) /= N_Empty | |
5d37ba92 | 1012 | and then Present (Etype (Expr)) |
ec4867fa | 1013 | then |
5d37ba92 ES |
1014 | -- Apply constraint check. Note that this is done before the implicit |
1015 | -- conversion of the expression done for anonymous access types to | |
f3d57416 | 1016 | -- ensure correct generation of the null-excluding check associated |
5d37ba92 ES |
1017 | -- with null-excluding expressions found in return statements. |
1018 | ||
1019 | Apply_Constraint_Check (Expr, R_Type); | |
1020 | ||
1021 | -- Ada 2005 (AI-318-02): When the result type is an anonymous access | |
1022 | -- type, apply an implicit conversion of the expression to that type | |
1023 | -- to force appropriate static and run-time accessibility checks. | |
ec4867fa | 1024 | |
0791fbe9 | 1025 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
1026 | and then Ekind (R_Type) = E_Anonymous_Access_Type |
1027 | then | |
1028 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); | |
1029 | Analyze_And_Resolve (Expr, R_Type); | |
b6b5cca8 AC |
1030 | |
1031 | -- If this is a local anonymous access to subprogram, the | |
1032 | -- accessibility check can be applied statically. The return is | |
1033 | -- illegal if the access type of the return expression is declared | |
1034 | -- inside of the subprogram (except if it is the subtype indication | |
1035 | -- of an extended return statement). | |
1036 | ||
1037 | elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then | |
1038 | if not Comes_From_Source (Current_Scope) | |
1039 | or else Ekind (Current_Scope) = E_Return_Statement | |
1040 | then | |
1041 | null; | |
1042 | ||
1043 | elsif | |
1044 | Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id) | |
1045 | then | |
1046 | Error_Msg_N ("cannot return local access to subprogram", N); | |
1047 | end if; | |
1ebc2612 AC |
1048 | |
1049 | -- The expression cannot be of a formal incomplete type | |
1050 | ||
1051 | elsif Ekind (Etype (Expr)) = E_Incomplete_Type | |
1052 | and then Is_Generic_Type (Etype (Expr)) | |
1053 | then | |
1054 | Error_Msg_N | |
1055 | ("cannot return expression of a formal incomplete type", N); | |
ec4867fa ES |
1056 | end if; |
1057 | ||
21d27997 RD |
1058 | -- If the result type is class-wide, then check that the return |
1059 | -- expression's type is not declared at a deeper level than the | |
1060 | -- function (RM05-6.5(5.6/2)). | |
1061 | ||
0791fbe9 | 1062 | if Ada_Version >= Ada_2005 |
21d27997 RD |
1063 | and then Is_Class_Wide_Type (R_Type) |
1064 | then | |
1065 | if Type_Access_Level (Etype (Expr)) > | |
1066 | Subprogram_Access_Level (Scope_Id) | |
1067 | then | |
1068 | Error_Msg_N | |
3ccedacc AC |
1069 | ("level of return expression type is deeper than " |
1070 | & "class-wide function!", Expr); | |
21d27997 RD |
1071 | end if; |
1072 | end if; | |
1073 | ||
4755cce9 JM |
1074 | -- Check incorrect use of dynamically tagged expression |
1075 | ||
1076 | if Is_Tagged_Type (R_Type) then | |
1077 | Check_Dynamically_Tagged_Expression | |
1078 | (Expr => Expr, | |
1079 | Typ => R_Type, | |
1080 | Related_Nod => N); | |
ec4867fa ES |
1081 | end if; |
1082 | ||
ec4867fa ES |
1083 | -- ??? A real run-time accessibility check is needed in cases |
1084 | -- involving dereferences of access parameters. For now we just | |
1085 | -- check the static cases. | |
1086 | ||
0791fbe9 | 1087 | if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) |
51245e2d | 1088 | and then Is_Limited_View (Etype (Scope_Id)) |
ec4867fa ES |
1089 | and then Object_Access_Level (Expr) > |
1090 | Subprogram_Access_Level (Scope_Id) | |
1091 | then | |
9694c039 AC |
1092 | -- Suppress the message in a generic, where the rewriting |
1093 | -- is irrelevant. | |
1094 | ||
1095 | if Inside_A_Generic then | |
1096 | null; | |
1097 | ||
1098 | else | |
1099 | Rewrite (N, | |
1100 | Make_Raise_Program_Error (Loc, | |
1101 | Reason => PE_Accessibility_Check_Failed)); | |
1102 | Analyze (N); | |
1103 | ||
43417b90 | 1104 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1105 | Error_Msg_N ("cannot return a local value by reference<<", N); |
1106 | Error_Msg_NE ("\& [<<", N, Standard_Program_Error); | |
9694c039 | 1107 | end if; |
ec4867fa | 1108 | end if; |
5d37ba92 ES |
1109 | |
1110 | if Known_Null (Expr) | |
1111 | and then Nkind (Parent (Scope_Id)) = N_Function_Specification | |
1112 | and then Null_Exclusion_Present (Parent (Scope_Id)) | |
1113 | then | |
1114 | Apply_Compile_Time_Constraint_Error | |
1115 | (N => Expr, | |
1116 | Msg => "(Ada 2005) null not allowed for " | |
dbfeb4fa | 1117 | & "null-excluding return??", |
5d37ba92 ES |
1118 | Reason => CE_Null_Not_Allowed); |
1119 | end if; | |
445e5888 AC |
1120 | |
1121 | -- RM 6.5 (5.4/3): accessibility checks also apply if the return object | |
1122 | -- has no initializing expression. | |
1123 | ||
1124 | elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then | |
1125 | if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) > | |
1126 | Subprogram_Access_Level (Scope_Id) | |
1127 | then | |
1128 | Error_Msg_N | |
1129 | ("level of return expression type is deeper than " | |
1130 | & "class-wide function!", Obj_Decl); | |
1131 | end if; | |
ec4867fa ES |
1132 | end if; |
1133 | end Analyze_Function_Return; | |
1134 | ||
996ae0b0 RK |
1135 | ------------------------------------- |
1136 | -- Analyze_Generic_Subprogram_Body -- | |
1137 | ------------------------------------- | |
1138 | ||
1139 | procedure Analyze_Generic_Subprogram_Body | |
1140 | (N : Node_Id; | |
1141 | Gen_Id : Entity_Id) | |
1142 | is | |
fbf5a39b | 1143 | Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); |
996ae0b0 | 1144 | Kind : constant Entity_Kind := Ekind (Gen_Id); |
fbf5a39b | 1145 | Body_Id : Entity_Id; |
996ae0b0 | 1146 | New_N : Node_Id; |
fbf5a39b | 1147 | Spec : Node_Id; |
996ae0b0 RK |
1148 | |
1149 | begin | |
82c80734 RD |
1150 | -- Copy body and disable expansion while analyzing the generic For a |
1151 | -- stub, do not copy the stub (which would load the proper body), this | |
1152 | -- will be done when the proper body is analyzed. | |
996ae0b0 RK |
1153 | |
1154 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
1155 | New_N := Copy_Generic_Node (N, Empty, Instantiating => False); | |
1156 | Rewrite (N, New_N); | |
caf07df9 AC |
1157 | |
1158 | -- Once the contents of the generic copy and the template are | |
1159 | -- swapped, do the same for their respective aspect specifications. | |
1160 | ||
1161 | Exchange_Aspects (N, New_N); | |
1162 | ||
1163 | -- Collect all contract-related source pragmas found within the | |
1164 | -- template and attach them to the contract of the subprogram body. | |
1165 | -- This contract is used in the capture of global references within | |
1166 | -- annotations. | |
1167 | ||
1168 | Create_Generic_Contract (N); | |
1169 | ||
996ae0b0 RK |
1170 | Start_Generic; |
1171 | end if; | |
1172 | ||
1173 | Spec := Specification (N); | |
1174 | ||
1175 | -- Within the body of the generic, the subprogram is callable, and | |
1176 | -- behaves like the corresponding non-generic unit. | |
1177 | ||
fbf5a39b | 1178 | Body_Id := Defining_Entity (Spec); |
996ae0b0 RK |
1179 | |
1180 | if Kind = E_Generic_Procedure | |
1181 | and then Nkind (Spec) /= N_Procedure_Specification | |
1182 | then | |
fbf5a39b | 1183 | Error_Msg_N ("invalid body for generic procedure ", Body_Id); |
996ae0b0 RK |
1184 | return; |
1185 | ||
1186 | elsif Kind = E_Generic_Function | |
1187 | and then Nkind (Spec) /= N_Function_Specification | |
1188 | then | |
fbf5a39b | 1189 | Error_Msg_N ("invalid body for generic function ", Body_Id); |
996ae0b0 RK |
1190 | return; |
1191 | end if; | |
1192 | ||
fbf5a39b | 1193 | Set_Corresponding_Body (Gen_Decl, Body_Id); |
996ae0b0 RK |
1194 | |
1195 | if Has_Completion (Gen_Id) | |
1196 | and then Nkind (Parent (N)) /= N_Subunit | |
1197 | then | |
1198 | Error_Msg_N ("duplicate generic body", N); | |
1199 | return; | |
1200 | else | |
1201 | Set_Has_Completion (Gen_Id); | |
1202 | end if; | |
1203 | ||
1204 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1205 | Set_Ekind (Defining_Entity (Specification (N)), Kind); | |
1206 | else | |
1207 | Set_Corresponding_Spec (N, Gen_Id); | |
1208 | end if; | |
1209 | ||
1210 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1211 | Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); | |
1212 | end if; | |
1213 | ||
1214 | -- Make generic parameters immediately visible in the body. They are | |
1215 | -- needed to process the formals declarations. Then make the formals | |
1216 | -- visible in a separate step. | |
1217 | ||
0a36105d | 1218 | Push_Scope (Gen_Id); |
996ae0b0 RK |
1219 | |
1220 | declare | |
1221 | E : Entity_Id; | |
1222 | First_Ent : Entity_Id; | |
1223 | ||
1224 | begin | |
1225 | First_Ent := First_Entity (Gen_Id); | |
1226 | ||
1227 | E := First_Ent; | |
1228 | while Present (E) and then not Is_Formal (E) loop | |
1229 | Install_Entity (E); | |
1230 | Next_Entity (E); | |
1231 | end loop; | |
1232 | ||
1233 | Set_Use (Generic_Formal_Declarations (Gen_Decl)); | |
1234 | ||
1235 | -- Now generic formals are visible, and the specification can be | |
1236 | -- analyzed, for subsequent conformance check. | |
1237 | ||
fbf5a39b | 1238 | Body_Id := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 1239 | |
fbf5a39b | 1240 | -- Make formal parameters visible |
996ae0b0 RK |
1241 | |
1242 | if Present (E) then | |
1243 | ||
fbf5a39b AC |
1244 | -- E is the first formal parameter, we loop through the formals |
1245 | -- installing them so that they will be visible. | |
996ae0b0 RK |
1246 | |
1247 | Set_First_Entity (Gen_Id, E); | |
996ae0b0 RK |
1248 | while Present (E) loop |
1249 | Install_Entity (E); | |
1250 | Next_Formal (E); | |
1251 | end loop; | |
1252 | end if; | |
1253 | ||
e895b435 | 1254 | -- Visible generic entity is callable within its own body |
996ae0b0 | 1255 | |
ec4867fa ES |
1256 | Set_Ekind (Gen_Id, Ekind (Body_Id)); |
1257 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
1258 | Set_Convention (Body_Id, Convention (Gen_Id)); | |
1259 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); | |
1260 | Set_Scope (Body_Id, Scope (Gen_Id)); | |
c5cec2fe AC |
1261 | |
1262 | -- Inherit the "ghostness" of the generic spec. Note that this | |
1263 | -- property is not directly inherited as the body may be subject | |
1264 | -- to a different Ghost assertion policy. | |
1265 | ||
8636f52f | 1266 | if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then |
c5cec2fe AC |
1267 | Set_Is_Ghost_Entity (Body_Id); |
1268 | ||
1269 | -- The Ghost policy in effect at the point of declaration and at | |
c2cfccb1 | 1270 | -- the point of completion must match (SPARK RM 6.9(14)). |
c5cec2fe AC |
1271 | |
1272 | Check_Ghost_Completion (Gen_Id, Body_Id); | |
1273 | end if; | |
1274 | ||
fbf5a39b AC |
1275 | Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); |
1276 | ||
1277 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1278 | ||
e895b435 | 1279 | -- No body to analyze, so restore state of generic unit |
fbf5a39b AC |
1280 | |
1281 | Set_Ekind (Gen_Id, Kind); | |
1282 | Set_Ekind (Body_Id, Kind); | |
1283 | ||
1284 | if Present (First_Ent) then | |
1285 | Set_First_Entity (Gen_Id, First_Ent); | |
1286 | end if; | |
1287 | ||
1288 | End_Scope; | |
1289 | return; | |
1290 | end if; | |
996ae0b0 | 1291 | |
82c80734 RD |
1292 | -- If this is a compilation unit, it must be made visible explicitly, |
1293 | -- because the compilation of the declaration, unlike other library | |
1294 | -- unit declarations, does not. If it is not a unit, the following | |
1295 | -- is redundant but harmless. | |
996ae0b0 RK |
1296 | |
1297 | Set_Is_Immediately_Visible (Gen_Id); | |
fbf5a39b | 1298 | Reference_Body_Formals (Gen_Id, Body_Id); |
996ae0b0 | 1299 | |
ec4867fa ES |
1300 | if Is_Child_Unit (Gen_Id) then |
1301 | Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); | |
1302 | end if; | |
1303 | ||
996ae0b0 | 1304 | Set_Actual_Subtypes (N, Current_Scope); |
483361a6 | 1305 | |
579847c2 AC |
1306 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); |
1307 | Set_SPARK_Pragma_Inherited (Body_Id, True); | |
1308 | ||
caf07df9 AC |
1309 | -- Analyze any aspect specifications that appear on the generic |
1310 | -- subprogram body. | |
1311 | ||
1312 | if Has_Aspects (N) then | |
1313 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); | |
1314 | end if; | |
1315 | ||
996ae0b0 RK |
1316 | Analyze_Declarations (Declarations (N)); |
1317 | Check_Completion; | |
996ae0b0 | 1318 | |
caf07df9 AC |
1319 | -- When a generic subprogram body appears inside a package, its |
1320 | -- contract is analyzed at the end of the package body declarations. | |
1321 | -- This is due to the delay with respect of the package contract upon | |
1322 | -- which the body contract may depend. When the generic subprogram | |
1323 | -- body is a compilation unit, this delay is not necessary. | |
1324 | ||
1325 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1326 | Analyze_Subprogram_Body_Contract (Body_Id); | |
1327 | ||
1328 | -- Capture all global references in a generic subprogram body | |
1329 | -- that acts as a compilation unit now that the contract has | |
1330 | -- been analyzed. | |
1331 | ||
1332 | Save_Global_References_In_Contract | |
1333 | (Templ => Original_Node (N), | |
1334 | Gen_Id => Gen_Id); | |
1335 | end if; | |
1336 | ||
1337 | Analyze (Handled_Statement_Sequence (N)); | |
996ae0b0 RK |
1338 | Save_Global_References (Original_Node (N)); |
1339 | ||
82c80734 RD |
1340 | -- Prior to exiting the scope, include generic formals again (if any |
1341 | -- are present) in the set of local entities. | |
996ae0b0 RK |
1342 | |
1343 | if Present (First_Ent) then | |
1344 | Set_First_Entity (Gen_Id, First_Ent); | |
1345 | end if; | |
1346 | ||
fbf5a39b | 1347 | Check_References (Gen_Id); |
996ae0b0 RK |
1348 | end; |
1349 | ||
e6f69614 | 1350 | Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); |
996ae0b0 RK |
1351 | End_Scope; |
1352 | Check_Subprogram_Order (N); | |
1353 | ||
e895b435 | 1354 | -- Outside of its body, unit is generic again |
996ae0b0 RK |
1355 | |
1356 | Set_Ekind (Gen_Id, Kind); | |
fbf5a39b | 1357 | Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); |
5d37ba92 ES |
1358 | |
1359 | if Style_Check then | |
1360 | Style.Check_Identifier (Body_Id, Gen_Id); | |
1361 | end if; | |
13d923cc | 1362 | |
996ae0b0 | 1363 | End_Generic; |
996ae0b0 RK |
1364 | end Analyze_Generic_Subprogram_Body; |
1365 | ||
4d8f3296 ES |
1366 | ---------------------------- |
1367 | -- Analyze_Null_Procedure -- | |
1368 | ---------------------------- | |
1369 | ||
1370 | procedure Analyze_Null_Procedure | |
1371 | (N : Node_Id; | |
1372 | Is_Completion : out Boolean) | |
1373 | is | |
1374 | Loc : constant Source_Ptr := Sloc (N); | |
1375 | Spec : constant Node_Id := Specification (N); | |
1376 | Designator : Entity_Id; | |
1377 | Form : Node_Id; | |
1378 | Null_Body : Node_Id := Empty; | |
1379 | Prev : Entity_Id; | |
1380 | ||
1381 | begin | |
1382 | -- Capture the profile of the null procedure before analysis, for | |
1383 | -- expansion at the freeze point and at each point of call. The body is | |
1384 | -- used if the procedure has preconditions, or if it is a completion. In | |
1385 | -- the first case the body is analyzed at the freeze point, in the other | |
1386 | -- it replaces the null procedure declaration. | |
1387 | ||
1388 | Null_Body := | |
1389 | Make_Subprogram_Body (Loc, | |
1390 | Specification => New_Copy_Tree (Spec), | |
8c35b40a | 1391 | Declarations => New_List, |
4d8f3296 ES |
1392 | Handled_Statement_Sequence => |
1393 | Make_Handled_Sequence_Of_Statements (Loc, | |
1394 | Statements => New_List (Make_Null_Statement (Loc)))); | |
1395 | ||
1396 | -- Create new entities for body and formals | |
1397 | ||
1398 | Set_Defining_Unit_Name (Specification (Null_Body), | |
9d2a2071 AC |
1399 | Make_Defining_Identifier |
1400 | (Sloc (Defining_Entity (N)), | |
1401 | Chars (Defining_Entity (N)))); | |
4d8f3296 ES |
1402 | |
1403 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1404 | while Present (Form) loop | |
1405 | Set_Defining_Identifier (Form, | |
9d2a2071 AC |
1406 | Make_Defining_Identifier |
1407 | (Sloc (Defining_Identifier (Form)), | |
1408 | Chars (Defining_Identifier (Form)))); | |
4d8f3296 ES |
1409 | Next (Form); |
1410 | end loop; | |
1411 | ||
1412 | -- Determine whether the null procedure may be a completion of a generic | |
1413 | -- suprogram, in which case we use the new null body as the completion | |
1414 | -- and set minimal semantic information on the original declaration, | |
1415 | -- which is rewritten as a null statement. | |
1416 | ||
1417 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
1418 | ||
1419 | if Present (Prev) and then Is_Generic_Subprogram (Prev) then | |
1420 | Insert_Before (N, Null_Body); | |
1421 | Set_Ekind (Defining_Entity (N), Ekind (Prev)); | |
4d8f3296 ES |
1422 | |
1423 | Rewrite (N, Make_Null_Statement (Loc)); | |
1424 | Analyze_Generic_Subprogram_Body (Null_Body, Prev); | |
1425 | Is_Completion := True; | |
1426 | return; | |
1427 | ||
1428 | else | |
4d8f3296 ES |
1429 | -- Resolve the types of the formals now, because the freeze point |
1430 | -- may appear in a different context, e.g. an instantiation. | |
1431 | ||
1432 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1433 | while Present (Form) loop | |
1434 | if Nkind (Parameter_Type (Form)) /= N_Access_Definition then | |
1435 | Find_Type (Parameter_Type (Form)); | |
1436 | ||
1437 | elsif | |
1438 | No (Access_To_Subprogram_Definition (Parameter_Type (Form))) | |
1439 | then | |
1440 | Find_Type (Subtype_Mark (Parameter_Type (Form))); | |
1441 | ||
1442 | else | |
1443 | -- The case of a null procedure with a formal that is an | |
1444 | -- access_to_subprogram type, and that is used as an actual | |
1445 | -- in an instantiation is left to the enthusiastic reader. | |
1446 | ||
1447 | null; | |
1448 | end if; | |
1449 | ||
1450 | Next (Form); | |
1451 | end loop; | |
1452 | end if; | |
1453 | ||
1454 | -- If there are previous overloadable entities with the same name, | |
1455 | -- check whether any of them is completed by the null procedure. | |
1456 | ||
1457 | if Present (Prev) and then Is_Overloadable (Prev) then | |
1458 | Designator := Analyze_Subprogram_Specification (Spec); | |
1459 | Prev := Find_Corresponding_Spec (N); | |
1460 | end if; | |
1461 | ||
1462 | if No (Prev) or else not Comes_From_Source (Prev) then | |
1463 | Designator := Analyze_Subprogram_Specification (Spec); | |
1464 | Set_Has_Completion (Designator); | |
1465 | ||
1466 | -- Signal to caller that this is a procedure declaration | |
1467 | ||
1468 | Is_Completion := False; | |
1469 | ||
1470 | -- Null procedures are always inlined, but generic formal subprograms | |
1471 | -- which appear as such in the internal instance of formal packages, | |
1472 | -- need no completion and are not marked Inline. | |
1473 | ||
1474 | if Expander_Active | |
1475 | and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration | |
1476 | then | |
1477 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
1478 | Set_Body_To_Inline (N, Null_Body); | |
1479 | Set_Is_Inlined (Designator); | |
1480 | end if; | |
1481 | ||
1482 | else | |
2fe258bf AC |
1483 | -- The null procedure is a completion. We unconditionally rewrite |
1484 | -- this as a null body (even if expansion is not active), because | |
1485 | -- there are various error checks that are applied on this body | |
1486 | -- when it is analyzed (e.g. correct aspect placement). | |
4d8f3296 | 1487 | |
a98480dd AC |
1488 | if Has_Completion (Prev) then |
1489 | Error_Msg_Sloc := Sloc (Prev); | |
1490 | Error_Msg_NE ("duplicate body for & declared#", N, Prev); | |
1491 | end if; | |
1492 | ||
4d8f3296 | 1493 | Is_Completion := True; |
2fe258bf AC |
1494 | Rewrite (N, Null_Body); |
1495 | Analyze (N); | |
4d8f3296 ES |
1496 | end if; |
1497 | end Analyze_Null_Procedure; | |
1498 | ||
996ae0b0 RK |
1499 | ----------------------------- |
1500 | -- Analyze_Operator_Symbol -- | |
1501 | ----------------------------- | |
1502 | ||
82c80734 RD |
1503 | -- An operator symbol such as "+" or "and" may appear in context where the |
1504 | -- literal denotes an entity name, such as "+"(x, y) or in context when it | |
1505 | -- is just a string, as in (conjunction = "or"). In these cases the parser | |
1506 | -- generates this node, and the semantics does the disambiguation. Other | |
1507 | -- such case are actuals in an instantiation, the generic unit in an | |
1508 | -- instantiation, and pragma arguments. | |
996ae0b0 RK |
1509 | |
1510 | procedure Analyze_Operator_Symbol (N : Node_Id) is | |
1511 | Par : constant Node_Id := Parent (N); | |
1512 | ||
1513 | begin | |
1f0b1e48 | 1514 | if (Nkind (Par) = N_Function_Call and then N = Name (Par)) |
996ae0b0 | 1515 | or else Nkind (Par) = N_Function_Instantiation |
1f0b1e48 | 1516 | or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par)) |
996ae0b0 | 1517 | or else (Nkind (Par) = N_Pragma_Argument_Association |
1f0b1e48 | 1518 | and then not Is_Pragma_String_Literal (Par)) |
996ae0b0 | 1519 | or else Nkind (Par) = N_Subprogram_Renaming_Declaration |
800621e0 RD |
1520 | or else (Nkind (Par) = N_Attribute_Reference |
1521 | and then Attribute_Name (Par) /= Name_Value) | |
996ae0b0 RK |
1522 | then |
1523 | Find_Direct_Name (N); | |
1524 | ||
1525 | else | |
1526 | Change_Operator_Symbol_To_String_Literal (N); | |
1527 | Analyze (N); | |
1528 | end if; | |
1529 | end Analyze_Operator_Symbol; | |
1530 | ||
1531 | ----------------------------------- | |
1532 | -- Analyze_Parameter_Association -- | |
1533 | ----------------------------------- | |
1534 | ||
1535 | procedure Analyze_Parameter_Association (N : Node_Id) is | |
1536 | begin | |
1537 | Analyze (Explicit_Actual_Parameter (N)); | |
1538 | end Analyze_Parameter_Association; | |
1539 | ||
1540 | ---------------------------- | |
1541 | -- Analyze_Procedure_Call -- | |
1542 | ---------------------------- | |
1543 | ||
1544 | procedure Analyze_Procedure_Call (N : Node_Id) is | |
1545 | Loc : constant Source_Ptr := Sloc (N); | |
1546 | P : constant Node_Id := Name (N); | |
1547 | Actuals : constant List_Id := Parameter_Associations (N); | |
1548 | Actual : Node_Id; | |
1549 | New_N : Node_Id; | |
1550 | ||
1551 | procedure Analyze_Call_And_Resolve; | |
1552 | -- Do Analyze and Resolve calls for procedure call | |
cd5a9750 | 1553 | -- At end, check illegal order dependence. |
996ae0b0 | 1554 | |
fbf5a39b AC |
1555 | ------------------------------ |
1556 | -- Analyze_Call_And_Resolve -- | |
1557 | ------------------------------ | |
1558 | ||
996ae0b0 RK |
1559 | procedure Analyze_Call_And_Resolve is |
1560 | begin | |
1561 | if Nkind (N) = N_Procedure_Call_Statement then | |
1562 | Analyze_Call (N); | |
1563 | Resolve (N, Standard_Void_Type); | |
1564 | else | |
1565 | Analyze (N); | |
1566 | end if; | |
1567 | end Analyze_Call_And_Resolve; | |
1568 | ||
1569 | -- Start of processing for Analyze_Procedure_Call | |
1570 | ||
1571 | begin | |
1572 | -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote | |
1573 | -- a procedure call or an entry call. The prefix may denote an access | |
1574 | -- to subprogram type, in which case an implicit dereference applies. | |
f3d57416 | 1575 | -- If the prefix is an indexed component (without implicit dereference) |
996ae0b0 RK |
1576 | -- then the construct denotes a call to a member of an entire family. |
1577 | -- If the prefix is a simple name, it may still denote a call to a | |
1578 | -- parameterless member of an entry family. Resolution of these various | |
1579 | -- interpretations is delicate. | |
1580 | ||
1581 | Analyze (P); | |
1582 | ||
758c442c GD |
1583 | -- If this is a call of the form Obj.Op, the call may have been |
1584 | -- analyzed and possibly rewritten into a block, in which case | |
1585 | -- we are done. | |
1586 | ||
1587 | if Analyzed (N) then | |
1588 | return; | |
1589 | end if; | |
1590 | ||
7415029d AC |
1591 | -- If there is an error analyzing the name (which may have been |
1592 | -- rewritten if the original call was in prefix notation) then error | |
1593 | -- has been emitted already, mark node and return. | |
996ae0b0 | 1594 | |
21791d97 | 1595 | if Error_Posted (N) or else Etype (Name (N)) = Any_Type then |
996ae0b0 RK |
1596 | Set_Etype (N, Any_Type); |
1597 | return; | |
1598 | end if; | |
1599 | ||
8636f52f HK |
1600 | -- The name of the procedure call may reference an entity subject to |
1601 | -- pragma Ghost with policy Ignore. Set the mode now to ensure that any | |
1602 | -- nodes generated during analysis and expansion are properly flagged as | |
1603 | -- ignored Ghost. | |
1604 | ||
1605 | Set_Ghost_Mode (N); | |
1606 | ||
996ae0b0 RK |
1607 | -- Otherwise analyze the parameters |
1608 | ||
1609 | if Present (Actuals) then | |
1610 | Actual := First (Actuals); | |
1611 | ||
1612 | while Present (Actual) loop | |
1613 | Analyze (Actual); | |
1614 | Check_Parameterless_Call (Actual); | |
1615 | Next (Actual); | |
1616 | end loop; | |
1617 | end if; | |
1618 | ||
0bfc9a64 | 1619 | -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls |
996ae0b0 RK |
1620 | |
1621 | if Nkind (P) = N_Attribute_Reference | |
b69cd36a AC |
1622 | and then Nam_In (Attribute_Name (P), Name_Elab_Spec, |
1623 | Name_Elab_Body, | |
1624 | Name_Elab_Subp_Body) | |
996ae0b0 RK |
1625 | then |
1626 | if Present (Actuals) then | |
1627 | Error_Msg_N | |
1628 | ("no parameters allowed for this call", First (Actuals)); | |
1629 | return; | |
1630 | end if; | |
1631 | ||
1632 | Set_Etype (N, Standard_Void_Type); | |
1633 | Set_Analyzed (N); | |
1634 | ||
1635 | elsif Is_Entity_Name (P) | |
1636 | and then Is_Record_Type (Etype (Entity (P))) | |
1637 | and then Remote_AST_I_Dereference (P) | |
1638 | then | |
1639 | return; | |
1640 | ||
1641 | elsif Is_Entity_Name (P) | |
1642 | and then Ekind (Entity (P)) /= E_Entry_Family | |
1643 | then | |
1644 | if Is_Access_Type (Etype (P)) | |
1645 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1646 | and then No (Actuals) | |
1647 | and then Comes_From_Source (N) | |
1648 | then | |
ed2233dc | 1649 | Error_Msg_N ("missing explicit dereference in call", N); |
996ae0b0 RK |
1650 | end if; |
1651 | ||
1652 | Analyze_Call_And_Resolve; | |
1653 | ||
1654 | -- If the prefix is the simple name of an entry family, this is | |
1655 | -- a parameterless call from within the task body itself. | |
1656 | ||
1657 | elsif Is_Entity_Name (P) | |
1658 | and then Nkind (P) = N_Identifier | |
1659 | and then Ekind (Entity (P)) = E_Entry_Family | |
1660 | and then Present (Actuals) | |
1661 | and then No (Next (First (Actuals))) | |
1662 | then | |
82c80734 RD |
1663 | -- Can be call to parameterless entry family. What appears to be the |
1664 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1665 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1666 | -- transformation. |
1667 | ||
1668 | New_N := | |
1669 | Make_Indexed_Component (Loc, | |
1670 | Prefix => | |
1671 | Make_Selected_Component (Loc, | |
1672 | Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc), | |
1673 | Selector_Name => New_Occurrence_Of (Entity (P), Loc)), | |
1674 | Expressions => Actuals); | |
1675 | Set_Name (N, New_N); | |
1676 | Set_Etype (New_N, Standard_Void_Type); | |
1677 | Set_Parameter_Associations (N, No_List); | |
1678 | Analyze_Call_And_Resolve; | |
1679 | ||
1680 | elsif Nkind (P) = N_Explicit_Dereference then | |
1681 | if Ekind (Etype (P)) = E_Subprogram_Type then | |
1682 | Analyze_Call_And_Resolve; | |
1683 | else | |
1684 | Error_Msg_N ("expect access to procedure in call", P); | |
1685 | end if; | |
1686 | ||
82c80734 RD |
1687 | -- The name can be a selected component or an indexed component that |
1688 | -- yields an access to subprogram. Such a prefix is legal if the call | |
1689 | -- has parameter associations. | |
996ae0b0 RK |
1690 | |
1691 | elsif Is_Access_Type (Etype (P)) | |
1692 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1693 | then | |
1694 | if Present (Actuals) then | |
1695 | Analyze_Call_And_Resolve; | |
1696 | else | |
ed2233dc | 1697 | Error_Msg_N ("missing explicit dereference in call ", N); |
996ae0b0 RK |
1698 | end if; |
1699 | ||
82c80734 RD |
1700 | -- If not an access to subprogram, then the prefix must resolve to the |
1701 | -- name of an entry, entry family, or protected operation. | |
996ae0b0 | 1702 | |
82c80734 RD |
1703 | -- For the case of a simple entry call, P is a selected component where |
1704 | -- the prefix is the task and the selector name is the entry. A call to | |
1705 | -- a protected procedure will have the same syntax. If the protected | |
1706 | -- object contains overloaded operations, the entity may appear as a | |
1707 | -- function, the context will select the operation whose type is Void. | |
996ae0b0 RK |
1708 | |
1709 | elsif Nkind (P) = N_Selected_Component | |
8fde064e AC |
1710 | and then Ekind_In (Entity (Selector_Name (P)), E_Entry, |
1711 | E_Procedure, | |
1712 | E_Function) | |
996ae0b0 RK |
1713 | then |
1714 | Analyze_Call_And_Resolve; | |
1715 | ||
1716 | elsif Nkind (P) = N_Selected_Component | |
1717 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family | |
1718 | and then Present (Actuals) | |
1719 | and then No (Next (First (Actuals))) | |
1720 | then | |
82c80734 RD |
1721 | -- Can be call to parameterless entry family. What appears to be the |
1722 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1723 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1724 | -- transformation. |
1725 | ||
1726 | New_N := | |
1727 | Make_Indexed_Component (Loc, | |
1728 | Prefix => New_Copy (P), | |
1729 | Expressions => Actuals); | |
1730 | Set_Name (N, New_N); | |
1731 | Set_Etype (New_N, Standard_Void_Type); | |
1732 | Set_Parameter_Associations (N, No_List); | |
1733 | Analyze_Call_And_Resolve; | |
1734 | ||
1735 | -- For the case of a reference to an element of an entry family, P is | |
1736 | -- an indexed component whose prefix is a selected component (task and | |
1737 | -- entry family), and whose index is the entry family index. | |
1738 | ||
1739 | elsif Nkind (P) = N_Indexed_Component | |
1740 | and then Nkind (Prefix (P)) = N_Selected_Component | |
1741 | and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family | |
1742 | then | |
1743 | Analyze_Call_And_Resolve; | |
1744 | ||
1745 | -- If the prefix is the name of an entry family, it is a call from | |
1746 | -- within the task body itself. | |
1747 | ||
1748 | elsif Nkind (P) = N_Indexed_Component | |
1749 | and then Nkind (Prefix (P)) = N_Identifier | |
1750 | and then Ekind (Entity (Prefix (P))) = E_Entry_Family | |
1751 | then | |
1752 | New_N := | |
1753 | Make_Selected_Component (Loc, | |
1754 | Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), | |
1755 | Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); | |
1756 | Rewrite (Prefix (P), New_N); | |
1757 | Analyze (P); | |
1758 | Analyze_Call_And_Resolve; | |
1759 | ||
9f8d1e5c AC |
1760 | -- In Ada 2012. a qualified expression is a name, but it cannot be a |
1761 | -- procedure name, so the construct can only be a qualified expression. | |
1762 | ||
1763 | elsif Nkind (P) = N_Qualified_Expression | |
1764 | and then Ada_Version >= Ada_2012 | |
1765 | then | |
1766 | Rewrite (N, Make_Code_Statement (Loc, Expression => P)); | |
1767 | Analyze (N); | |
1768 | ||
e895b435 | 1769 | -- Anything else is an error |
996ae0b0 RK |
1770 | |
1771 | else | |
758c442c | 1772 | Error_Msg_N ("invalid procedure or entry call", N); |
996ae0b0 RK |
1773 | end if; |
1774 | end Analyze_Procedure_Call; | |
1775 | ||
b0186f71 AC |
1776 | ------------------------------ |
1777 | -- Analyze_Return_Statement -- | |
1778 | ------------------------------ | |
1779 | ||
1780 | procedure Analyze_Return_Statement (N : Node_Id) is | |
1781 | ||
1782 | pragma Assert (Nkind_In (N, N_Simple_Return_Statement, | |
1783 | N_Extended_Return_Statement)); | |
1784 | ||
1785 | Returns_Object : constant Boolean := | |
1786 | Nkind (N) = N_Extended_Return_Statement | |
1787 | or else | |
8fde064e AC |
1788 | (Nkind (N) = N_Simple_Return_Statement |
1789 | and then Present (Expression (N))); | |
b0186f71 AC |
1790 | -- True if we're returning something; that is, "return <expression>;" |
1791 | -- or "return Result : T [:= ...]". False for "return;". Used for error | |
1792 | -- checking: If Returns_Object is True, N should apply to a function | |
1793 | -- body; otherwise N should apply to a procedure body, entry body, | |
1794 | -- accept statement, or extended return statement. | |
1795 | ||
1796 | function Find_What_It_Applies_To return Entity_Id; | |
1797 | -- Find the entity representing the innermost enclosing body, accept | |
1798 | -- statement, or extended return statement. If the result is a callable | |
1799 | -- construct or extended return statement, then this will be the value | |
1800 | -- of the Return_Applies_To attribute. Otherwise, the program is | |
1801 | -- illegal. See RM-6.5(4/2). | |
1802 | ||
1803 | ----------------------------- | |
1804 | -- Find_What_It_Applies_To -- | |
1805 | ----------------------------- | |
1806 | ||
1807 | function Find_What_It_Applies_To return Entity_Id is | |
1808 | Result : Entity_Id := Empty; | |
1809 | ||
1810 | begin | |
36b8f95f AC |
1811 | -- Loop outward through the Scope_Stack, skipping blocks, loops, |
1812 | -- and postconditions. | |
b0186f71 AC |
1813 | |
1814 | for J in reverse 0 .. Scope_Stack.Last loop | |
1815 | Result := Scope_Stack.Table (J).Entity; | |
11bc76df AC |
1816 | exit when not Ekind_In (Result, E_Block, E_Loop) |
1817 | and then Chars (Result) /= Name_uPostconditions; | |
b0186f71 AC |
1818 | end loop; |
1819 | ||
1820 | pragma Assert (Present (Result)); | |
1821 | return Result; | |
1822 | end Find_What_It_Applies_To; | |
1823 | ||
1824 | -- Local declarations | |
1825 | ||
1826 | Scope_Id : constant Entity_Id := Find_What_It_Applies_To; | |
1827 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
1828 | Loc : constant Source_Ptr := Sloc (N); | |
1829 | Stm_Entity : constant Entity_Id := | |
1830 | New_Internal_Entity | |
1831 | (E_Return_Statement, Current_Scope, Loc, 'R'); | |
1832 | ||
1833 | -- Start of processing for Analyze_Return_Statement | |
1834 | ||
1835 | begin | |
1836 | Set_Return_Statement_Entity (N, Stm_Entity); | |
1837 | ||
1838 | Set_Etype (Stm_Entity, Standard_Void_Type); | |
1839 | Set_Return_Applies_To (Stm_Entity, Scope_Id); | |
1840 | ||
1841 | -- Place Return entity on scope stack, to simplify enforcement of 6.5 | |
1842 | -- (4/2): an inner return statement will apply to this extended return. | |
1843 | ||
1844 | if Nkind (N) = N_Extended_Return_Statement then | |
1845 | Push_Scope (Stm_Entity); | |
1846 | end if; | |
1847 | ||
1848 | -- Check that pragma No_Return is obeyed. Don't complain about the | |
1849 | -- implicitly-generated return that is placed at the end. | |
1850 | ||
1851 | if No_Return (Scope_Id) and then Comes_From_Source (N) then | |
1852 | Error_Msg_N ("RETURN statement not allowed (No_Return)", N); | |
1853 | end if; | |
1854 | ||
1855 | -- Warn on any unassigned OUT parameters if in procedure | |
1856 | ||
1857 | if Ekind (Scope_Id) = E_Procedure then | |
1858 | Warn_On_Unassigned_Out_Parameter (N, Scope_Id); | |
1859 | end if; | |
1860 | ||
1861 | -- Check that functions return objects, and other things do not | |
1862 | ||
1863 | if Kind = E_Function or else Kind = E_Generic_Function then | |
1864 | if not Returns_Object then | |
1865 | Error_Msg_N ("missing expression in return from function", N); | |
1866 | end if; | |
1867 | ||
1868 | elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
1869 | if Returns_Object then | |
1870 | Error_Msg_N ("procedure cannot return value (use function)", N); | |
1871 | end if; | |
1872 | ||
1873 | elsif Kind = E_Entry or else Kind = E_Entry_Family then | |
1874 | if Returns_Object then | |
1875 | if Is_Protected_Type (Scope (Scope_Id)) then | |
1876 | Error_Msg_N ("entry body cannot return value", N); | |
1877 | else | |
1878 | Error_Msg_N ("accept statement cannot return value", N); | |
1879 | end if; | |
1880 | end if; | |
1881 | ||
1882 | elsif Kind = E_Return_Statement then | |
1883 | ||
1884 | -- We are nested within another return statement, which must be an | |
1885 | -- extended_return_statement. | |
1886 | ||
1887 | if Returns_Object then | |
d0dcb2b1 AC |
1888 | if Nkind (N) = N_Extended_Return_Statement then |
1889 | Error_Msg_N | |
cc96a1b8 | 1890 | ("extended return statement cannot be nested (use `RETURN;`)", |
d0dcb2b1 AC |
1891 | N); |
1892 | ||
1893 | -- Case of a simple return statement with a value inside extended | |
1894 | -- return statement. | |
1895 | ||
1896 | else | |
1897 | Error_Msg_N | |
3ccedacc AC |
1898 | ("return nested in extended return statement cannot return " |
1899 | & "value (use `RETURN;`)", N); | |
d0dcb2b1 | 1900 | end if; |
b0186f71 AC |
1901 | end if; |
1902 | ||
1903 | else | |
1904 | Error_Msg_N ("illegal context for return statement", N); | |
1905 | end if; | |
1906 | ||
1907 | if Ekind_In (Kind, E_Function, E_Generic_Function) then | |
1908 | Analyze_Function_Return (N); | |
1909 | ||
1910 | elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then | |
1911 | Set_Return_Present (Scope_Id); | |
1912 | end if; | |
1913 | ||
1914 | if Nkind (N) = N_Extended_Return_Statement then | |
1915 | End_Scope; | |
1916 | end if; | |
1917 | ||
1918 | Kill_Current_Values (Last_Assignment_Only => True); | |
1919 | Check_Unreachable_Code (N); | |
dec6faf1 AC |
1920 | |
1921 | Analyze_Dimension (N); | |
b0186f71 AC |
1922 | end Analyze_Return_Statement; |
1923 | ||
5d37ba92 ES |
1924 | ------------------------------------- |
1925 | -- Analyze_Simple_Return_Statement -- | |
1926 | ------------------------------------- | |
ec4867fa | 1927 | |
5d37ba92 | 1928 | procedure Analyze_Simple_Return_Statement (N : Node_Id) is |
996ae0b0 | 1929 | begin |
5d37ba92 ES |
1930 | if Present (Expression (N)) then |
1931 | Mark_Coextensions (N, Expression (N)); | |
996ae0b0 RK |
1932 | end if; |
1933 | ||
5d37ba92 ES |
1934 | Analyze_Return_Statement (N); |
1935 | end Analyze_Simple_Return_Statement; | |
996ae0b0 | 1936 | |
82c80734 RD |
1937 | ------------------------- |
1938 | -- Analyze_Return_Type -- | |
1939 | ------------------------- | |
1940 | ||
1941 | procedure Analyze_Return_Type (N : Node_Id) is | |
1942 | Designator : constant Entity_Id := Defining_Entity (N); | |
1943 | Typ : Entity_Id := Empty; | |
1944 | ||
1945 | begin | |
ec4867fa ES |
1946 | -- Normal case where result definition does not indicate an error |
1947 | ||
41251c60 JM |
1948 | if Result_Definition (N) /= Error then |
1949 | if Nkind (Result_Definition (N)) = N_Access_Definition then | |
ce5ba43a | 1950 | Check_SPARK_05_Restriction |
fe5d3068 | 1951 | ("access result is not allowed", Result_Definition (N)); |
daec8eeb | 1952 | |
b1c11e0e JM |
1953 | -- Ada 2005 (AI-254): Handle anonymous access to subprograms |
1954 | ||
1955 | declare | |
1956 | AD : constant Node_Id := | |
1957 | Access_To_Subprogram_Definition (Result_Definition (N)); | |
1958 | begin | |
1959 | if Present (AD) and then Protected_Present (AD) then | |
1960 | Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); | |
1961 | else | |
1962 | Typ := Access_Definition (N, Result_Definition (N)); | |
1963 | end if; | |
1964 | end; | |
1965 | ||
41251c60 JM |
1966 | Set_Parent (Typ, Result_Definition (N)); |
1967 | Set_Is_Local_Anonymous_Access (Typ); | |
1968 | Set_Etype (Designator, Typ); | |
1969 | ||
b66c3ff4 AC |
1970 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1971 | ||
1972 | Null_Exclusion_Static_Checks (N); | |
1973 | ||
41251c60 JM |
1974 | -- Subtype_Mark case |
1975 | ||
1976 | else | |
1977 | Find_Type (Result_Definition (N)); | |
1978 | Typ := Entity (Result_Definition (N)); | |
1979 | Set_Etype (Designator, Typ); | |
1980 | ||
2ba431e5 | 1981 | -- Unconstrained array as result is not allowed in SPARK |
daec8eeb | 1982 | |
8fde064e | 1983 | if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then |
ce5ba43a | 1984 | Check_SPARK_05_Restriction |
fe5d3068 | 1985 | ("returning an unconstrained array is not allowed", |
7394c8cc | 1986 | Result_Definition (N)); |
daec8eeb YM |
1987 | end if; |
1988 | ||
b66c3ff4 AC |
1989 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1990 | ||
1991 | Null_Exclusion_Static_Checks (N); | |
1992 | ||
1993 | -- If a null exclusion is imposed on the result type, then create | |
1994 | -- a null-excluding itype (an access subtype) and use it as the | |
1995 | -- function's Etype. Note that the null exclusion checks are done | |
1996 | -- right before this, because they don't get applied to types that | |
1997 | -- do not come from source. | |
1998 | ||
8fde064e | 1999 | if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then |
b66c3ff4 AC |
2000 | Set_Etype (Designator, |
2001 | Create_Null_Excluding_Itype | |
ff7139c3 AC |
2002 | (T => Typ, |
2003 | Related_Nod => N, | |
2004 | Scope_Id => Scope (Current_Scope))); | |
2005 | ||
2006 | -- The new subtype must be elaborated before use because | |
2007 | -- it is visible outside of the function. However its base | |
2008 | -- type may not be frozen yet, so the reference that will | |
2009 | -- force elaboration must be attached to the freezing of | |
2010 | -- the base type. | |
2011 | ||
212863c0 AC |
2012 | -- If the return specification appears on a proper body, |
2013 | -- the subtype will have been created already on the spec. | |
2014 | ||
ff7139c3 | 2015 | if Is_Frozen (Typ) then |
212863c0 AC |
2016 | if Nkind (Parent (N)) = N_Subprogram_Body |
2017 | and then Nkind (Parent (Parent (N))) = N_Subunit | |
2018 | then | |
2019 | null; | |
2020 | else | |
2021 | Build_Itype_Reference (Etype (Designator), Parent (N)); | |
2022 | end if; | |
2023 | ||
ff7139c3 AC |
2024 | else |
2025 | Ensure_Freeze_Node (Typ); | |
2026 | ||
2027 | declare | |
212863c0 | 2028 | IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); |
ff7139c3 AC |
2029 | begin |
2030 | Set_Itype (IR, Etype (Designator)); | |
2031 | Append_Freeze_Actions (Typ, New_List (IR)); | |
2032 | end; | |
2033 | end if; | |
2034 | ||
b66c3ff4 AC |
2035 | else |
2036 | Set_Etype (Designator, Typ); | |
2037 | end if; | |
2038 | ||
41251c60 | 2039 | if Ekind (Typ) = E_Incomplete_Type |
0a36105d JM |
2040 | and then Is_Value_Type (Typ) |
2041 | then | |
2042 | null; | |
2043 | ||
2044 | elsif Ekind (Typ) = E_Incomplete_Type | |
41251c60 | 2045 | or else (Is_Class_Wide_Type (Typ) |
4b6f99f5 | 2046 | and then Ekind (Root_Type (Typ)) = E_Incomplete_Type) |
41251c60 | 2047 | then |
dd386db0 AC |
2048 | -- AI05-0151: Tagged incomplete types are allowed in all formal |
2049 | -- parts. Untagged incomplete types are not allowed in bodies. | |
b973629e AC |
2050 | -- As a consequence, limited views cannot appear in a basic |
2051 | -- declaration that is itself within a body, because there is | |
2052 | -- no point at which the non-limited view will become visible. | |
dd386db0 AC |
2053 | |
2054 | if Ada_Version >= Ada_2012 then | |
b973629e AC |
2055 | if From_Limited_With (Typ) and then In_Package_Body then |
2056 | Error_Msg_NE | |
2057 | ("invalid use of incomplete type&", | |
3f80a182 | 2058 | Result_Definition (N), Typ); |
b973629e | 2059 | |
1ebc2612 AC |
2060 | -- The return type of a subprogram body cannot be of a |
2061 | -- formal incomplete type. | |
2062 | ||
2063 | elsif Is_Generic_Type (Typ) | |
2064 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2065 | then | |
2066 | Error_Msg_N | |
2067 | ("return type cannot be a formal incomplete type", | |
2068 | Result_Definition (N)); | |
2069 | ||
2070 | elsif Is_Class_Wide_Type (Typ) | |
2071 | and then Is_Generic_Type (Root_Type (Typ)) | |
2072 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2073 | then | |
2074 | Error_Msg_N | |
2075 | ("return type cannot be a formal incomplete type", | |
2076 | Result_Definition (N)); | |
2077 | ||
b973629e | 2078 | elsif Is_Tagged_Type (Typ) then |
dd386db0 AC |
2079 | null; |
2080 | ||
ed09416f AC |
2081 | -- Use is legal in a thunk generated for an operation |
2082 | -- inherited from a progenitor. | |
2083 | ||
2084 | elsif Is_Thunk (Designator) | |
2085 | and then Present (Non_Limited_View (Typ)) | |
2086 | then | |
2087 | null; | |
2088 | ||
5b6f12c7 | 2089 | elsif Nkind (Parent (N)) = N_Subprogram_Body |
31d922e3 AC |
2090 | or else Nkind_In (Parent (Parent (N)), N_Accept_Statement, |
2091 | N_Entry_Body) | |
dd386db0 AC |
2092 | then |
2093 | Error_Msg_NE | |
2094 | ("invalid use of untagged incomplete type&", | |
2095 | Designator, Typ); | |
2096 | end if; | |
2097 | ||
63be2a5a | 2098 | -- The type must be completed in the current package. This |
31d922e3 | 2099 | -- is checked at the end of the package declaration when |
7b7a0c2b AC |
2100 | -- Taft-amendment types are identified. If the return type |
2101 | -- is class-wide, there is no required check, the type can | |
2102 | -- be a bona fide TAT. | |
63be2a5a AC |
2103 | |
2104 | if Ekind (Scope (Current_Scope)) = E_Package | |
c199ccf7 | 2105 | and then In_Private_Part (Scope (Current_Scope)) |
7b7a0c2b | 2106 | and then not Is_Class_Wide_Type (Typ) |
63be2a5a AC |
2107 | then |
2108 | Append_Elmt (Designator, Private_Dependents (Typ)); | |
2109 | end if; | |
2110 | ||
dd386db0 AC |
2111 | else |
2112 | Error_Msg_NE | |
2113 | ("invalid use of incomplete type&", Designator, Typ); | |
2114 | end if; | |
41251c60 | 2115 | end if; |
82c80734 RD |
2116 | end if; |
2117 | ||
ec4867fa ES |
2118 | -- Case where result definition does indicate an error |
2119 | ||
82c80734 RD |
2120 | else |
2121 | Set_Etype (Designator, Any_Type); | |
2122 | end if; | |
2123 | end Analyze_Return_Type; | |
2124 | ||
996ae0b0 RK |
2125 | ----------------------------- |
2126 | -- Analyze_Subprogram_Body -- | |
2127 | ----------------------------- | |
2128 | ||
b1b543d2 BD |
2129 | procedure Analyze_Subprogram_Body (N : Node_Id) is |
2130 | Loc : constant Source_Ptr := Sloc (N); | |
2131 | Body_Spec : constant Node_Id := Specification (N); | |
2132 | Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); | |
2133 | ||
2134 | begin | |
2135 | if Debug_Flag_C then | |
2136 | Write_Str ("==> subprogram body "); | |
2137 | Write_Name (Chars (Body_Id)); | |
2138 | Write_Str (" from "); | |
2139 | Write_Location (Loc); | |
2140 | Write_Eol; | |
2141 | Indent; | |
2142 | end if; | |
2143 | ||
2144 | Trace_Scope (N, Body_Id, " Analyze subprogram: "); | |
2145 | ||
2146 | -- The real work is split out into the helper, so it can do "return;" | |
2147 | -- without skipping the debug output: | |
2148 | ||
2149 | Analyze_Subprogram_Body_Helper (N); | |
2150 | ||
2151 | if Debug_Flag_C then | |
2152 | Outdent; | |
2153 | Write_Str ("<== subprogram body "); | |
2154 | Write_Name (Chars (Body_Id)); | |
2155 | Write_Str (" from "); | |
2156 | Write_Location (Loc); | |
2157 | Write_Eol; | |
2158 | end if; | |
2159 | end Analyze_Subprogram_Body; | |
2160 | ||
ea3c0651 AC |
2161 | -------------------------------------- |
2162 | -- Analyze_Subprogram_Body_Contract -- | |
2163 | -------------------------------------- | |
2164 | ||
ab8843fa | 2165 | procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is |
caf07df9 AC |
2166 | Items : constant Node_Id := Contract (Body_Id); |
2167 | Mode : SPARK_Mode_Type; | |
2168 | Prag : Node_Id; | |
2169 | Prag_Nam : Name_Id; | |
2170 | Ref_Depends : Node_Id := Empty; | |
2171 | Ref_Global : Node_Id := Empty; | |
ea3c0651 | 2172 | |
caf07df9 AC |
2173 | begin |
2174 | -- When a subprogram body declaration is illegal, its defining entity is | |
2175 | -- left unanalyzed. There is nothing left to do in this case because the | |
2176 | -- body lacks a contract, or even a proper Ekind. | |
fc999c5d | 2177 | |
caf07df9 AC |
2178 | if Ekind (Body_Id) = E_Void then |
2179 | return; | |
2180 | end if; | |
c61ef416 | 2181 | |
caf07df9 AC |
2182 | -- Due to the timing of contract analysis, delayed pragmas may be |
2183 | -- subject to the wrong SPARK_Mode, usually that of the enclosing | |
2184 | -- context. To remedy this, restore the original SPARK_Mode of the | |
2185 | -- related subprogram body. | |
ab8843fa | 2186 | |
caf07df9 | 2187 | Save_SPARK_Mode_And_Set (Body_Id, Mode); |
ab8843fa | 2188 | |
caf07df9 AC |
2189 | -- All subprograms carry a contract, but for some it is not significant |
2190 | -- and should not be processed. | |
c9d70ab1 | 2191 | |
caf07df9 AC |
2192 | if not Has_Significant_Contract (Body_Id) then |
2193 | null; | |
c9d70ab1 | 2194 | |
caf07df9 AC |
2195 | -- The subprogram body is a completion, analyze all delayed pragmas that |
2196 | -- apply. Note that when the body is stand alone, the pragmas are always | |
2197 | -- analyzed on the spot. | |
2c8d828a | 2198 | |
caf07df9 | 2199 | elsif Present (Items) then |
54e28df2 | 2200 | |
caf07df9 AC |
2201 | -- Locate and store pragmas Refined_Depends and Refined_Global since |
2202 | -- their order of analysis matters. | |
c9d70ab1 | 2203 | |
caf07df9 AC |
2204 | Prag := Classifications (Items); |
2205 | while Present (Prag) loop | |
2206 | Prag_Nam := Pragma_Name (Prag); | |
c9d70ab1 | 2207 | |
caf07df9 AC |
2208 | if Prag_Nam = Name_Refined_Depends then |
2209 | Ref_Depends := Prag; | |
2210 | ||
2211 | elsif Prag_Nam = Name_Refined_Global then | |
2212 | Ref_Global := Prag; | |
2213 | end if; | |
2214 | ||
2215 | Prag := Next_Pragma (Prag); | |
2216 | end loop; | |
ab8843fa | 2217 | |
c9d70ab1 AC |
2218 | -- Analyze Refined_Global first as Refined_Depends may mention items |
2219 | -- classified in the global refinement. | |
ab8843fa | 2220 | |
c9d70ab1 AC |
2221 | if Present (Ref_Global) then |
2222 | Analyze_Refined_Global_In_Decl_Part (Ref_Global); | |
c9d70ab1 | 2223 | end if; |
ab8843fa | 2224 | |
c9d70ab1 AC |
2225 | -- Refined_Depends must be analyzed after Refined_Global in order to |
2226 | -- see the modes of all global refinements. | |
2227 | ||
2228 | if Present (Ref_Depends) then | |
2229 | Analyze_Refined_Depends_In_Decl_Part (Ref_Depends); | |
ab8843fa | 2230 | end if; |
54e28df2 | 2231 | end if; |
c61ef416 | 2232 | |
c9d70ab1 AC |
2233 | -- Ensure that the contract cases or postconditions mention 'Result or |
2234 | -- define a post-state. | |
2235 | ||
2236 | Check_Result_And_Post_State (Body_Id); | |
2237 | ||
fc999c5d RD |
2238 | -- Restore the SPARK_Mode of the enclosing context after all delayed |
2239 | -- pragmas have been analyzed. | |
2240 | ||
c61ef416 | 2241 | Restore_SPARK_Mode (Mode); |
ea3c0651 AC |
2242 | end Analyze_Subprogram_Body_Contract; |
2243 | ||
b1b543d2 BD |
2244 | ------------------------------------ |
2245 | -- Analyze_Subprogram_Body_Helper -- | |
2246 | ------------------------------------ | |
2247 | ||
996ae0b0 RK |
2248 | -- This procedure is called for regular subprogram bodies, generic bodies, |
2249 | -- and for subprogram stubs of both kinds. In the case of stubs, only the | |
2250 | -- specification matters, and is used to create a proper declaration for | |
2251 | -- the subprogram, or to perform conformance checks. | |
2252 | ||
b1b543d2 | 2253 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is |
fbf5a39b | 2254 | Loc : constant Source_Ptr := Sloc (N); |
8d1fe980 | 2255 | Body_Spec : Node_Id := Specification (N); |
fbf5a39b AC |
2256 | Body_Id : Entity_Id := Defining_Entity (Body_Spec); |
2257 | Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); | |
0868e09c | 2258 | Conformant : Boolean; |
21d27997 | 2259 | HSS : Node_Id; |
21d27997 RD |
2260 | Prot_Typ : Entity_Id := Empty; |
2261 | Spec_Id : Entity_Id; | |
2262 | Spec_Decl : Node_Id := Empty; | |
2263 | ||
2264 | Last_Real_Spec_Entity : Entity_Id := Empty; | |
2265 | -- When we analyze a separate spec, the entity chain ends up containing | |
2266 | -- the formals, as well as any itypes generated during analysis of the | |
2267 | -- default expressions for parameters, or the arguments of associated | |
2268 | -- precondition/postcondition pragmas (which are analyzed in the context | |
2269 | -- of the spec since they have visibility on formals). | |
2270 | -- | |
2271 | -- These entities belong with the spec and not the body. However we do | |
2272 | -- the analysis of the body in the context of the spec (again to obtain | |
2273 | -- visibility to the formals), and all the entities generated during | |
2274 | -- this analysis end up also chained to the entity chain of the spec. | |
2275 | -- But they really belong to the body, and there is circuitry to move | |
2276 | -- them from the spec to the body. | |
2277 | -- | |
2278 | -- However, when we do this move, we don't want to move the real spec | |
2279 | -- entities (first para above) to the body. The Last_Real_Spec_Entity | |
2280 | -- variable points to the last real spec entity, so we only move those | |
2281 | -- chained beyond that point. It is initialized to Empty to deal with | |
2282 | -- the case where there is no separate spec. | |
996ae0b0 | 2283 | |
ac072cb2 AC |
2284 | function Body_Has_Contract return Boolean; |
2285 | -- Check whether unanalyzed body has an aspect or pragma that may | |
1399d355 | 2286 | -- generate a SPARK contract. |
ac072cb2 | 2287 | |
8d1fe980 AC |
2288 | procedure Build_Subprogram_Declaration; |
2289 | -- Create a matching subprogram declaration for subprogram body N | |
2290 | ||
ec4867fa | 2291 | procedure Check_Anonymous_Return; |
e50e1c5e | 2292 | -- Ada 2005: if a function returns an access type that denotes a task, |
ec4867fa ES |
2293 | -- or a type that contains tasks, we must create a master entity for |
2294 | -- the anonymous type, which typically will be used in an allocator | |
2295 | -- in the body of the function. | |
2296 | ||
e660dbf7 JM |
2297 | procedure Check_Inline_Pragma (Spec : in out Node_Id); |
2298 | -- Look ahead to recognize a pragma that may appear after the body. | |
2299 | -- If there is a previous spec, check that it appears in the same | |
2300 | -- declarative part. If the pragma is Inline_Always, perform inlining | |
2301 | -- unconditionally, otherwise only if Front_End_Inlining is requested. | |
2302 | -- If the body acts as a spec, and inlining is required, we create a | |
2303 | -- subprogram declaration for it, in order to attach the body to inline. | |
21d27997 RD |
2304 | -- If pragma does not appear after the body, check whether there is |
2305 | -- an inline pragma before any local declarations. | |
c37bb106 | 2306 | |
7665e4bd AC |
2307 | procedure Check_Missing_Return; |
2308 | -- Checks for a function with a no return statements, and also performs | |
8d606a78 RD |
2309 | -- the warning checks implemented by Check_Returns. In formal mode, also |
2310 | -- verify that a function ends with a RETURN and that a procedure does | |
2311 | -- not contain any RETURN. | |
7665e4bd | 2312 | |
d44202ba HK |
2313 | function Disambiguate_Spec return Entity_Id; |
2314 | -- When a primitive is declared between the private view and the full | |
2315 | -- view of a concurrent type which implements an interface, a special | |
2316 | -- mechanism is used to find the corresponding spec of the primitive | |
2317 | -- body. | |
2318 | ||
5dcab3ca AC |
2319 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id); |
2320 | -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains | |
2321 | -- incomplete types coming from a limited context and swap their limited | |
2322 | -- views with the non-limited ones. | |
2323 | ||
d44202ba HK |
2324 | function Is_Private_Concurrent_Primitive |
2325 | (Subp_Id : Entity_Id) return Boolean; | |
2326 | -- Determine whether subprogram Subp_Id is a primitive of a concurrent | |
2327 | -- type that implements an interface and has a private view. | |
2328 | ||
76a69663 ES |
2329 | procedure Set_Trivial_Subprogram (N : Node_Id); |
2330 | -- Sets the Is_Trivial_Subprogram flag in both spec and body of the | |
2331 | -- subprogram whose body is being analyzed. N is the statement node | |
2332 | -- causing the flag to be set, if the following statement is a return | |
2333 | -- of an entity, we mark the entity as set in source to suppress any | |
2334 | -- warning on the stylized use of function stubs with a dummy return. | |
2335 | ||
758c442c GD |
2336 | procedure Verify_Overriding_Indicator; |
2337 | -- If there was a previous spec, the entity has been entered in the | |
2338 | -- current scope previously. If the body itself carries an overriding | |
2339 | -- indicator, check that it is consistent with the known status of the | |
2340 | -- entity. | |
2341 | ||
ac072cb2 AC |
2342 | ----------------------- |
2343 | -- Body_Has_Contract -- | |
2344 | ----------------------- | |
2345 | ||
2346 | function Body_Has_Contract return Boolean is | |
8d1fe980 AC |
2347 | Decls : constant List_Id := Declarations (N); |
2348 | Item : Node_Id; | |
ac072cb2 AC |
2349 | |
2350 | begin | |
8d1fe980 AC |
2351 | -- Check for unanalyzed aspects in the body that will generate a |
2352 | -- contract. | |
ac072cb2 AC |
2353 | |
2354 | if Present (Aspect_Specifications (N)) then | |
8d1fe980 AC |
2355 | Item := First (Aspect_Specifications (N)); |
2356 | while Present (Item) loop | |
2357 | if Is_Contract_Annotation (Item) then | |
ac072cb2 AC |
2358 | return True; |
2359 | end if; | |
2360 | ||
8d1fe980 | 2361 | Next (Item); |
ac072cb2 AC |
2362 | end loop; |
2363 | end if; | |
2364 | ||
1399d355 | 2365 | -- Check for pragmas that may generate a contract |
ac072cb2 AC |
2366 | |
2367 | if Present (Decls) then | |
8d1fe980 AC |
2368 | Item := First (Decls); |
2369 | while Present (Item) loop | |
2370 | if Nkind (Item) = N_Pragma | |
2371 | and then Is_Contract_Annotation (Item) | |
2372 | then | |
2373 | return True; | |
ac072cb2 AC |
2374 | end if; |
2375 | ||
8d1fe980 | 2376 | Next (Item); |
ac072cb2 AC |
2377 | end loop; |
2378 | end if; | |
2379 | ||
2380 | return False; | |
2381 | end Body_Has_Contract; | |
2382 | ||
8d1fe980 AC |
2383 | ---------------------------------- |
2384 | -- Build_Subprogram_Declaration -- | |
2385 | ---------------------------------- | |
2386 | ||
2387 | procedure Build_Subprogram_Declaration is | |
2388 | Asp : Node_Id; | |
2389 | Decl : Node_Id; | |
2390 | Subp_Decl : Node_Id; | |
2391 | ||
2392 | begin | |
2393 | -- Create a matching subprogram spec using the profile of the body. | |
2394 | -- The structure of the tree is identical, but has new entities for | |
2395 | -- the defining unit name and formal parameters. | |
2396 | ||
2397 | Subp_Decl := | |
2398 | Make_Subprogram_Declaration (Loc, | |
2399 | Specification => Copy_Subprogram_Spec (Body_Spec)); | |
2400 | ||
2401 | -- Relocate the aspects of the subprogram body to the new subprogram | |
2402 | -- spec because it acts as the initial declaration. | |
2403 | -- ??? what about pragmas | |
2404 | ||
2405 | Move_Aspects (N, To => Subp_Decl); | |
2406 | Insert_Before_And_Analyze (N, Subp_Decl); | |
2407 | ||
2408 | -- The analysis of the subprogram spec aspects may introduce pragmas | |
2409 | -- that need to be analyzed. | |
2410 | ||
2411 | Decl := Next (Subp_Decl); | |
2412 | while Present (Decl) loop | |
2413 | ||
2414 | -- Stop the search for pragmas once the body has been reached as | |
2415 | -- this terminates the region where pragmas may appear. | |
2416 | ||
2417 | if Decl = N then | |
2418 | exit; | |
2419 | ||
2420 | elsif Nkind (Decl) = N_Pragma then | |
2421 | Analyze (Decl); | |
2422 | end if; | |
2423 | ||
2424 | Next (Decl); | |
2425 | end loop; | |
2426 | ||
2427 | Spec_Id := Defining_Entity (Subp_Decl); | |
2428 | Set_Corresponding_Spec (N, Spec_Id); | |
2429 | ||
2430 | -- Mark the generated spec as a source construct to ensure that all | |
2431 | -- calls to it are properly registered in ALI files for GNATprove. | |
2432 | ||
2433 | Set_Comes_From_Source (Spec_Id, True); | |
2434 | ||
2435 | -- If aspect SPARK_Mode was specified on the body, it needs to be | |
2436 | -- repeated both on the generated spec and the body. | |
2437 | ||
2438 | Asp := Find_Aspect (Spec_Id, Aspect_SPARK_Mode); | |
2439 | ||
2440 | if Present (Asp) then | |
2441 | Asp := New_Copy_Tree (Asp); | |
2442 | Set_Analyzed (Asp, False); | |
2443 | Set_Aspect_Specifications (N, New_List (Asp)); | |
2444 | end if; | |
2445 | ||
2446 | -- Ensure that the specs of the subprogram declaration and its body | |
2447 | -- are identical, otherwise they will appear non-conformant due to | |
2448 | -- rewritings in the default values of formal parameters. | |
2449 | ||
2450 | Body_Spec := Copy_Subprogram_Spec (Body_Spec); | |
2451 | Set_Specification (N, Body_Spec); | |
2452 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); | |
2453 | end Build_Subprogram_Declaration; | |
2454 | ||
ec4867fa ES |
2455 | ---------------------------- |
2456 | -- Check_Anonymous_Return -- | |
2457 | ---------------------------- | |
2458 | ||
2459 | procedure Check_Anonymous_Return is | |
2460 | Decl : Node_Id; | |
a523b302 | 2461 | Par : Node_Id; |
ec4867fa ES |
2462 | Scop : Entity_Id; |
2463 | ||
2464 | begin | |
2465 | if Present (Spec_Id) then | |
2466 | Scop := Spec_Id; | |
2467 | else | |
2468 | Scop := Body_Id; | |
2469 | end if; | |
2470 | ||
2471 | if Ekind (Scop) = E_Function | |
2472 | and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type | |
a523b302 | 2473 | and then not Is_Thunk (Scop) |
4b963531 AC |
2474 | |
2475 | -- Skip internally built functions which handle the case of | |
2476 | -- a null access (see Expand_Interface_Conversion) | |
2477 | ||
2478 | and then not (Is_Interface (Designated_Type (Etype (Scop))) | |
4b6f99f5 | 2479 | and then not Comes_From_Source (Parent (Scop))) |
4b963531 | 2480 | |
a523b302 JM |
2481 | and then (Has_Task (Designated_Type (Etype (Scop))) |
2482 | or else | |
4b6f99f5 RD |
2483 | (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) |
2484 | and then | |
2485 | Is_Limited_Record (Designated_Type (Etype (Scop))))) | |
ec4867fa | 2486 | and then Expander_Active |
b20de9b9 | 2487 | |
8fde064e | 2488 | -- Avoid cases with no tasking support |
b20de9b9 AC |
2489 | |
2490 | and then RTE_Available (RE_Current_Master) | |
2491 | and then not Restriction_Active (No_Task_Hierarchy) | |
ec4867fa ES |
2492 | then |
2493 | Decl := | |
2494 | Make_Object_Declaration (Loc, | |
2495 | Defining_Identifier => | |
2496 | Make_Defining_Identifier (Loc, Name_uMaster), | |
2497 | Constant_Present => True, | |
2498 | Object_Definition => | |
e4494292 | 2499 | New_Occurrence_Of (RTE (RE_Master_Id), Loc), |
ec4867fa ES |
2500 | Expression => |
2501 | Make_Explicit_Dereference (Loc, | |
e4494292 | 2502 | New_Occurrence_Of (RTE (RE_Current_Master), Loc))); |
ec4867fa ES |
2503 | |
2504 | if Present (Declarations (N)) then | |
2505 | Prepend (Decl, Declarations (N)); | |
2506 | else | |
2507 | Set_Declarations (N, New_List (Decl)); | |
2508 | end if; | |
2509 | ||
2510 | Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); | |
2511 | Set_Has_Master_Entity (Scop); | |
a523b302 JM |
2512 | |
2513 | -- Now mark the containing scope as a task master | |
2514 | ||
2515 | Par := N; | |
2516 | while Nkind (Par) /= N_Compilation_Unit loop | |
2517 | Par := Parent (Par); | |
2518 | pragma Assert (Present (Par)); | |
2519 | ||
2520 | -- If we fall off the top, we are at the outer level, and | |
2521 | -- the environment task is our effective master, so nothing | |
2522 | -- to mark. | |
2523 | ||
2524 | if Nkind_In | |
2525 | (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) | |
2526 | then | |
2527 | Set_Is_Task_Master (Par, True); | |
2528 | exit; | |
2529 | end if; | |
2530 | end loop; | |
ec4867fa ES |
2531 | end if; |
2532 | end Check_Anonymous_Return; | |
2533 | ||
e660dbf7 JM |
2534 | ------------------------- |
2535 | -- Check_Inline_Pragma -- | |
2536 | ------------------------- | |
758c442c | 2537 | |
e660dbf7 JM |
2538 | procedure Check_Inline_Pragma (Spec : in out Node_Id) is |
2539 | Prag : Node_Id; | |
2540 | Plist : List_Id; | |
0fb2ea01 | 2541 | |
21d27997 | 2542 | function Is_Inline_Pragma (N : Node_Id) return Boolean; |
30783513 | 2543 | -- True when N is a pragma Inline or Inline_Always that applies |
33931112 | 2544 | -- to this subprogram. |
21d27997 RD |
2545 | |
2546 | ----------------------- | |
2547 | -- Is_Inline_Pragma -- | |
2548 | ----------------------- | |
2549 | ||
2550 | function Is_Inline_Pragma (N : Node_Id) return Boolean is | |
2551 | begin | |
2552 | return | |
2553 | Nkind (N) = N_Pragma | |
2554 | and then | |
8fde064e | 2555 | (Pragma_Name (N) = Name_Inline_Always |
4b6f99f5 RD |
2556 | or else (Front_End_Inlining |
2557 | and then Pragma_Name (N) = Name_Inline)) | |
21d27997 | 2558 | and then |
8fde064e AC |
2559 | Chars |
2560 | (Expression (First (Pragma_Argument_Associations (N)))) = | |
2561 | Chars (Body_Id); | |
21d27997 RD |
2562 | end Is_Inline_Pragma; |
2563 | ||
2564 | -- Start of processing for Check_Inline_Pragma | |
2565 | ||
c37bb106 | 2566 | begin |
e660dbf7 JM |
2567 | if not Expander_Active then |
2568 | return; | |
2569 | end if; | |
2570 | ||
2571 | if Is_List_Member (N) | |
2572 | and then Present (Next (N)) | |
21d27997 | 2573 | and then Is_Inline_Pragma (Next (N)) |
c37bb106 AC |
2574 | then |
2575 | Prag := Next (N); | |
2576 | ||
21d27997 RD |
2577 | elsif Nkind (N) /= N_Subprogram_Body_Stub |
2578 | and then Present (Declarations (N)) | |
2579 | and then Is_Inline_Pragma (First (Declarations (N))) | |
2580 | then | |
2581 | Prag := First (Declarations (N)); | |
2582 | ||
e660dbf7 JM |
2583 | else |
2584 | Prag := Empty; | |
c37bb106 | 2585 | end if; |
e660dbf7 JM |
2586 | |
2587 | if Present (Prag) then | |
2588 | if Present (Spec_Id) then | |
30196a76 | 2589 | if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then |
e660dbf7 JM |
2590 | Analyze (Prag); |
2591 | end if; | |
2592 | ||
2593 | else | |
d39d6bb8 | 2594 | -- Create a subprogram declaration, to make treatment uniform |
e660dbf7 JM |
2595 | |
2596 | declare | |
2597 | Subp : constant Entity_Id := | |
30196a76 | 2598 | Make_Defining_Identifier (Loc, Chars (Body_Id)); |
e660dbf7 | 2599 | Decl : constant Node_Id := |
30196a76 RD |
2600 | Make_Subprogram_Declaration (Loc, |
2601 | Specification => | |
2602 | New_Copy_Tree (Specification (N))); | |
2603 | ||
e660dbf7 JM |
2604 | begin |
2605 | Set_Defining_Unit_Name (Specification (Decl), Subp); | |
2606 | ||
2607 | if Present (First_Formal (Body_Id)) then | |
21d27997 | 2608 | Plist := Copy_Parameter_List (Body_Id); |
e660dbf7 JM |
2609 | Set_Parameter_Specifications |
2610 | (Specification (Decl), Plist); | |
2611 | end if; | |
2612 | ||
2613 | Insert_Before (N, Decl); | |
2614 | Analyze (Decl); | |
2615 | Analyze (Prag); | |
2616 | Set_Has_Pragma_Inline (Subp); | |
2617 | ||
76a69663 | 2618 | if Pragma_Name (Prag) = Name_Inline_Always then |
e660dbf7 | 2619 | Set_Is_Inlined (Subp); |
21d27997 | 2620 | Set_Has_Pragma_Inline_Always (Subp); |
e660dbf7 JM |
2621 | end if; |
2622 | ||
158d55fa AC |
2623 | -- Prior to copying the subprogram body to create a template |
2624 | -- for it for subsequent inlining, remove the pragma from | |
2625 | -- the current body so that the copy that will produce the | |
2626 | -- new body will start from a completely unanalyzed tree. | |
2627 | ||
2628 | if Nkind (Parent (Prag)) = N_Subprogram_Body then | |
2629 | Rewrite (Prag, Make_Null_Statement (Sloc (Prag))); | |
2630 | end if; | |
2631 | ||
e660dbf7 JM |
2632 | Spec := Subp; |
2633 | end; | |
2634 | end if; | |
2635 | end if; | |
2636 | end Check_Inline_Pragma; | |
2637 | ||
7665e4bd AC |
2638 | -------------------------- |
2639 | -- Check_Missing_Return -- | |
2640 | -------------------------- | |
2641 | ||
2642 | procedure Check_Missing_Return is | |
2643 | Id : Entity_Id; | |
2644 | Missing_Ret : Boolean; | |
2645 | ||
2646 | begin | |
2647 | if Nkind (Body_Spec) = N_Function_Specification then | |
2648 | if Present (Spec_Id) then | |
2649 | Id := Spec_Id; | |
2650 | else | |
2651 | Id := Body_Id; | |
2652 | end if; | |
2653 | ||
fe5d3068 | 2654 | if Return_Present (Id) then |
7665e4bd AC |
2655 | Check_Returns (HSS, 'F', Missing_Ret); |
2656 | ||
2657 | if Missing_Ret then | |
2658 | Set_Has_Missing_Return (Id); | |
2659 | end if; | |
2660 | ||
2aca76d6 AC |
2661 | elsif Is_Generic_Subprogram (Id) |
2662 | or else not Is_Machine_Code_Subprogram (Id) | |
7665e4bd AC |
2663 | then |
2664 | Error_Msg_N ("missing RETURN statement in function body", N); | |
2665 | end if; | |
2666 | ||
fe5d3068 | 2667 | -- If procedure with No_Return, check returns |
607d0635 | 2668 | |
fe5d3068 YM |
2669 | elsif Nkind (Body_Spec) = N_Procedure_Specification |
2670 | and then Present (Spec_Id) | |
2671 | and then No_Return (Spec_Id) | |
607d0635 | 2672 | then |
fe5d3068 YM |
2673 | Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); |
2674 | end if; | |
2675 | ||
ad05f2e9 | 2676 | -- Special checks in SPARK mode |
fe5d3068 YM |
2677 | |
2678 | if Nkind (Body_Spec) = N_Function_Specification then | |
7394c8cc | 2679 | |
ad05f2e9 | 2680 | -- In SPARK mode, last statement of a function should be a return |
fe5d3068 YM |
2681 | |
2682 | declare | |
2683 | Stat : constant Node_Id := Last_Source_Statement (HSS); | |
2684 | begin | |
2685 | if Present (Stat) | |
7394c8cc AC |
2686 | and then not Nkind_In (Stat, N_Simple_Return_Statement, |
2687 | N_Extended_Return_Statement) | |
fe5d3068 | 2688 | then |
ce5ba43a | 2689 | Check_SPARK_05_Restriction |
fe5d3068 YM |
2690 | ("last statement in function should be RETURN", Stat); |
2691 | end if; | |
2692 | end; | |
2693 | ||
ad05f2e9 | 2694 | -- In SPARK mode, verify that a procedure has no return |
fe5d3068 YM |
2695 | |
2696 | elsif Nkind (Body_Spec) = N_Procedure_Specification then | |
607d0635 AC |
2697 | if Present (Spec_Id) then |
2698 | Id := Spec_Id; | |
2699 | else | |
2700 | Id := Body_Id; | |
2701 | end if; | |
2702 | ||
8d606a78 RD |
2703 | -- Would be nice to point to return statement here, can we |
2704 | -- borrow the Check_Returns procedure here ??? | |
2705 | ||
607d0635 | 2706 | if Return_Present (Id) then |
ce5ba43a | 2707 | Check_SPARK_05_Restriction |
fe5d3068 | 2708 | ("procedure should not have RETURN", N); |
607d0635 | 2709 | end if; |
7665e4bd AC |
2710 | end if; |
2711 | end Check_Missing_Return; | |
2712 | ||
d44202ba HK |
2713 | ----------------------- |
2714 | -- Disambiguate_Spec -- | |
2715 | ----------------------- | |
2716 | ||
2717 | function Disambiguate_Spec return Entity_Id is | |
2718 | Priv_Spec : Entity_Id; | |
2719 | Spec_N : Entity_Id; | |
2720 | ||
2721 | procedure Replace_Types (To_Corresponding : Boolean); | |
2722 | -- Depending on the flag, replace the type of formal parameters of | |
2723 | -- Body_Id if it is a concurrent type implementing interfaces with | |
2724 | -- the corresponding record type or the other way around. | |
2725 | ||
2726 | procedure Replace_Types (To_Corresponding : Boolean) is | |
2727 | Formal : Entity_Id; | |
2728 | Formal_Typ : Entity_Id; | |
2729 | ||
2730 | begin | |
2731 | Formal := First_Formal (Body_Id); | |
2732 | while Present (Formal) loop | |
2733 | Formal_Typ := Etype (Formal); | |
2734 | ||
df3e68b1 HK |
2735 | if Is_Class_Wide_Type (Formal_Typ) then |
2736 | Formal_Typ := Root_Type (Formal_Typ); | |
2737 | end if; | |
2738 | ||
d44202ba HK |
2739 | -- From concurrent type to corresponding record |
2740 | ||
2741 | if To_Corresponding then | |
2742 | if Is_Concurrent_Type (Formal_Typ) | |
2743 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
4b6f99f5 RD |
2744 | and then |
2745 | Present (Interfaces | |
2746 | (Corresponding_Record_Type (Formal_Typ))) | |
d44202ba HK |
2747 | then |
2748 | Set_Etype (Formal, | |
2749 | Corresponding_Record_Type (Formal_Typ)); | |
2750 | end if; | |
2751 | ||
2752 | -- From corresponding record to concurrent type | |
2753 | ||
2754 | else | |
2755 | if Is_Concurrent_Record_Type (Formal_Typ) | |
2756 | and then Present (Interfaces (Formal_Typ)) | |
2757 | then | |
2758 | Set_Etype (Formal, | |
2759 | Corresponding_Concurrent_Type (Formal_Typ)); | |
2760 | end if; | |
2761 | end if; | |
2762 | ||
2763 | Next_Formal (Formal); | |
2764 | end loop; | |
2765 | end Replace_Types; | |
2766 | ||
2767 | -- Start of processing for Disambiguate_Spec | |
2768 | ||
2769 | begin | |
2770 | -- Try to retrieve the specification of the body as is. All error | |
2771 | -- messages are suppressed because the body may not have a spec in | |
2772 | -- its current state. | |
2773 | ||
2774 | Spec_N := Find_Corresponding_Spec (N, False); | |
2775 | ||
2776 | -- It is possible that this is the body of a primitive declared | |
2777 | -- between a private and a full view of a concurrent type. The | |
2778 | -- controlling parameter of the spec carries the concurrent type, | |
2779 | -- not the corresponding record type as transformed by Analyze_ | |
2780 | -- Subprogram_Specification. In such cases, we undo the change | |
2781 | -- made by the analysis of the specification and try to find the | |
2782 | -- spec again. | |
766d7add | 2783 | |
8198b93d HK |
2784 | -- Note that wrappers already have their corresponding specs and |
2785 | -- bodies set during their creation, so if the candidate spec is | |
16b05213 | 2786 | -- a wrapper, then we definitely need to swap all types to their |
8198b93d | 2787 | -- original concurrent status. |
d44202ba | 2788 | |
8198b93d HK |
2789 | if No (Spec_N) |
2790 | or else Is_Primitive_Wrapper (Spec_N) | |
2791 | then | |
d44202ba HK |
2792 | -- Restore all references of corresponding record types to the |
2793 | -- original concurrent types. | |
2794 | ||
2795 | Replace_Types (To_Corresponding => False); | |
2796 | Priv_Spec := Find_Corresponding_Spec (N, False); | |
2797 | ||
2798 | -- The current body truly belongs to a primitive declared between | |
2799 | -- a private and a full view. We leave the modified body as is, | |
2800 | -- and return the true spec. | |
2801 | ||
2802 | if Present (Priv_Spec) | |
2803 | and then Is_Private_Primitive (Priv_Spec) | |
2804 | then | |
2805 | return Priv_Spec; | |
2806 | end if; | |
2807 | ||
2808 | -- In case that this is some sort of error, restore the original | |
2809 | -- state of the body. | |
2810 | ||
2811 | Replace_Types (To_Corresponding => True); | |
2812 | end if; | |
2813 | ||
2814 | return Spec_N; | |
2815 | end Disambiguate_Spec; | |
2816 | ||
5dcab3ca AC |
2817 | ---------------------------- |
2818 | -- Exchange_Limited_Views -- | |
2819 | ---------------------------- | |
2820 | ||
2821 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is | |
2822 | procedure Detect_And_Exchange (Id : Entity_Id); | |
2823 | -- Determine whether Id's type denotes an incomplete type associated | |
2824 | -- with a limited with clause and exchange the limited view with the | |
167b47d9 | 2825 | -- non-limited one when available. |
5dcab3ca AC |
2826 | |
2827 | ------------------------- | |
2828 | -- Detect_And_Exchange -- | |
2829 | ------------------------- | |
2830 | ||
2831 | procedure Detect_And_Exchange (Id : Entity_Id) is | |
2832 | Typ : constant Entity_Id := Etype (Id); | |
5dcab3ca | 2833 | begin |
ccd6f414 | 2834 | if From_Limited_With (Typ) and then Has_Non_Limited_View (Typ) then |
5dcab3ca AC |
2835 | Set_Etype (Id, Non_Limited_View (Typ)); |
2836 | end if; | |
2837 | end Detect_And_Exchange; | |
2838 | ||
2839 | -- Local variables | |
2840 | ||
2841 | Formal : Entity_Id; | |
2842 | ||
2843 | -- Start of processing for Exchange_Limited_Views | |
2844 | ||
2845 | begin | |
2846 | if No (Subp_Id) then | |
2847 | return; | |
2848 | ||
2849 | -- Do not process subprogram bodies as they already use the non- | |
2850 | -- limited view of types. | |
2851 | ||
2852 | elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then | |
2853 | return; | |
2854 | end if; | |
2855 | ||
2856 | -- Examine all formals and swap views when applicable | |
2857 | ||
2858 | Formal := First_Formal (Subp_Id); | |
2859 | while Present (Formal) loop | |
2860 | Detect_And_Exchange (Formal); | |
2861 | ||
2862 | Next_Formal (Formal); | |
2863 | end loop; | |
2864 | ||
2865 | -- Process the return type of a function | |
2866 | ||
2867 | if Ekind (Subp_Id) = E_Function then | |
2868 | Detect_And_Exchange (Subp_Id); | |
2869 | end if; | |
2870 | end Exchange_Limited_Views; | |
2871 | ||
d44202ba HK |
2872 | ------------------------------------- |
2873 | -- Is_Private_Concurrent_Primitive -- | |
2874 | ------------------------------------- | |
2875 | ||
2876 | function Is_Private_Concurrent_Primitive | |
2877 | (Subp_Id : Entity_Id) return Boolean | |
2878 | is | |
2879 | Formal_Typ : Entity_Id; | |
2880 | ||
2881 | begin | |
2882 | if Present (First_Formal (Subp_Id)) then | |
2883 | Formal_Typ := Etype (First_Formal (Subp_Id)); | |
2884 | ||
2885 | if Is_Concurrent_Record_Type (Formal_Typ) then | |
df3e68b1 HK |
2886 | if Is_Class_Wide_Type (Formal_Typ) then |
2887 | Formal_Typ := Root_Type (Formal_Typ); | |
2888 | end if; | |
2889 | ||
d44202ba HK |
2890 | Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); |
2891 | end if; | |
2892 | ||
2893 | -- The type of the first formal is a concurrent tagged type with | |
2894 | -- a private view. | |
2895 | ||
2896 | return | |
2897 | Is_Concurrent_Type (Formal_Typ) | |
2898 | and then Is_Tagged_Type (Formal_Typ) | |
2899 | and then Has_Private_Declaration (Formal_Typ); | |
2900 | end if; | |
2901 | ||
2902 | return False; | |
2903 | end Is_Private_Concurrent_Primitive; | |
2904 | ||
76a69663 ES |
2905 | ---------------------------- |
2906 | -- Set_Trivial_Subprogram -- | |
2907 | ---------------------------- | |
2908 | ||
2909 | procedure Set_Trivial_Subprogram (N : Node_Id) is | |
2910 | Nxt : constant Node_Id := Next (N); | |
2911 | ||
2912 | begin | |
2913 | Set_Is_Trivial_Subprogram (Body_Id); | |
2914 | ||
2915 | if Present (Spec_Id) then | |
2916 | Set_Is_Trivial_Subprogram (Spec_Id); | |
2917 | end if; | |
2918 | ||
2919 | if Present (Nxt) | |
2920 | and then Nkind (Nxt) = N_Simple_Return_Statement | |
2921 | and then No (Next (Nxt)) | |
2922 | and then Present (Expression (Nxt)) | |
2923 | and then Is_Entity_Name (Expression (Nxt)) | |
2924 | then | |
2925 | Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); | |
2926 | end if; | |
2927 | end Set_Trivial_Subprogram; | |
2928 | ||
758c442c GD |
2929 | --------------------------------- |
2930 | -- Verify_Overriding_Indicator -- | |
2931 | --------------------------------- | |
2932 | ||
2933 | procedure Verify_Overriding_Indicator is | |
2934 | begin | |
21d27997 RD |
2935 | if Must_Override (Body_Spec) then |
2936 | if Nkind (Spec_Id) = N_Defining_Operator_Symbol | |
4b6f99f5 | 2937 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) |
21d27997 RD |
2938 | then |
2939 | null; | |
2940 | ||
038140ed | 2941 | elsif not Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2942 | Error_Msg_NE |
21d27997 | 2943 | ("subprogram& is not overriding", Body_Spec, Spec_Id); |
23e28b42 AC |
2944 | |
2945 | -- Overriding indicators aren't allowed for protected subprogram | |
2946 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
2947 | -- this to a warning if -gnatd.E is enabled. | |
2948 | ||
2949 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
2950 | Error_Msg_Warn := Error_To_Warning; | |
2951 | Error_Msg_N | |
b785e0b8 | 2952 | ("<<overriding indicator not allowed for protected " |
23e28b42 | 2953 | & "subprogram body", Body_Spec); |
21d27997 | 2954 | end if; |
758c442c | 2955 | |
5d37ba92 | 2956 | elsif Must_Not_Override (Body_Spec) then |
038140ed | 2957 | if Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2958 | Error_Msg_NE |
5d37ba92 | 2959 | ("subprogram& overrides inherited operation", |
76a69663 | 2960 | Body_Spec, Spec_Id); |
5d37ba92 | 2961 | |
21d27997 | 2962 | elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol |
4b6f99f5 | 2963 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) |
21d27997 | 2964 | then |
ed2233dc | 2965 | Error_Msg_NE |
3ccedacc | 2966 | ("subprogram& overrides predefined operator ", |
21d27997 RD |
2967 | Body_Spec, Spec_Id); |
2968 | ||
23e28b42 AC |
2969 | -- Overriding indicators aren't allowed for protected subprogram |
2970 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
2971 | -- this to a warning if -gnatd.E is enabled. | |
2972 | ||
2973 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
2974 | Error_Msg_Warn := Error_To_Warning; | |
5d37ba92 | 2975 | |
23e28b42 | 2976 | Error_Msg_N |
3ccedacc AC |
2977 | ("<<overriding indicator not allowed " |
2978 | & "for protected subprogram body", Body_Spec); | |
23e28b42 AC |
2979 | |
2980 | -- If this is not a primitive operation, then the overriding | |
2981 | -- indicator is altogether illegal. | |
2982 | ||
2983 | elsif not Is_Primitive (Spec_Id) then | |
ed2233dc | 2984 | Error_Msg_N |
3ccedacc AC |
2985 | ("overriding indicator only allowed " |
2986 | & "if subprogram is primitive", Body_Spec); | |
5d37ba92 | 2987 | end if; |
235f4375 | 2988 | |
23e28b42 AC |
2989 | -- If checking the style rule and the operation overrides, then |
2990 | -- issue a warning about a missing overriding_indicator. Protected | |
2991 | -- subprogram bodies are excluded from this style checking, since | |
2992 | -- they aren't primitives (even though their declarations can | |
2993 | -- override) and aren't allowed to have an overriding_indicator. | |
2994 | ||
806f6d37 | 2995 | elsif Style_Check |
038140ed | 2996 | and then Present (Overridden_Operation (Spec_Id)) |
23e28b42 | 2997 | and then Ekind (Scope (Spec_Id)) /= E_Protected_Type |
235f4375 AC |
2998 | then |
2999 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3000 | Style.Missing_Overriding (N, Body_Id); | |
806f6d37 AC |
3001 | |
3002 | elsif Style_Check | |
3003 | and then Can_Override_Operator (Spec_Id) | |
3004 | and then not Is_Predefined_File_Name | |
3005 | (Unit_File_Name (Get_Source_Unit (Spec_Id))) | |
3006 | then | |
3007 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3008 | Style.Missing_Overriding (N, Body_Id); | |
758c442c GD |
3009 | end if; |
3010 | end Verify_Overriding_Indicator; | |
3011 | ||
b1b543d2 | 3012 | -- Start of processing for Analyze_Subprogram_Body_Helper |
0fb2ea01 | 3013 | |
996ae0b0 | 3014 | begin |
82c80734 RD |
3015 | -- Generic subprograms are handled separately. They always have a |
3016 | -- generic specification. Determine whether current scope has a | |
3017 | -- previous declaration. | |
996ae0b0 | 3018 | |
82c80734 RD |
3019 | -- If the subprogram body is defined within an instance of the same |
3020 | -- name, the instance appears as a package renaming, and will be hidden | |
3021 | -- within the subprogram. | |
996ae0b0 RK |
3022 | |
3023 | if Present (Prev_Id) | |
3024 | and then not Is_Overloadable (Prev_Id) | |
3025 | and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration | |
3026 | or else Comes_From_Source (Prev_Id)) | |
3027 | then | |
fbf5a39b | 3028 | if Is_Generic_Subprogram (Prev_Id) then |
996ae0b0 | 3029 | Spec_Id := Prev_Id; |
8636f52f HK |
3030 | |
3031 | -- The corresponding spec may be subject to pragma Ghost with | |
3032 | -- policy Ignore. Set the mode now to ensure that any nodes | |
3033 | -- generated during analysis and expansion are properly flagged | |
3034 | -- as ignored Ghost. | |
3035 | ||
3036 | Set_Ghost_Mode (N, Spec_Id); | |
996ae0b0 RK |
3037 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); |
3038 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3039 | ||
3040 | Analyze_Generic_Subprogram_Body (N, Spec_Id); | |
7665e4bd AC |
3041 | |
3042 | if Nkind (N) = N_Subprogram_Body then | |
3043 | HSS := Handled_Statement_Sequence (N); | |
3044 | Check_Missing_Return; | |
3045 | end if; | |
3046 | ||
996ae0b0 RK |
3047 | return; |
3048 | ||
3049 | else | |
82c80734 RD |
3050 | -- Previous entity conflicts with subprogram name. Attempting to |
3051 | -- enter name will post error. | |
996ae0b0 RK |
3052 | |
3053 | Enter_Name (Body_Id); | |
3054 | return; | |
3055 | end if; | |
3056 | ||
82c80734 RD |
3057 | -- Non-generic case, find the subprogram declaration, if one was seen, |
3058 | -- or enter new overloaded entity in the current scope. If the | |
3059 | -- Current_Entity is the Body_Id itself, the unit is being analyzed as | |
3060 | -- part of the context of one of its subunits. No need to redo the | |
3061 | -- analysis. | |
996ae0b0 | 3062 | |
8fde064e | 3063 | elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then |
996ae0b0 RK |
3064 | return; |
3065 | ||
3066 | else | |
fbf5a39b | 3067 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); |
996ae0b0 RK |
3068 | |
3069 | if Nkind (N) = N_Subprogram_Body_Stub | |
3070 | or else No (Corresponding_Spec (N)) | |
3071 | then | |
d44202ba HK |
3072 | if Is_Private_Concurrent_Primitive (Body_Id) then |
3073 | Spec_Id := Disambiguate_Spec; | |
8636f52f HK |
3074 | |
3075 | -- The corresponding spec may be subject to pragma Ghost with | |
3076 | -- policy Ignore. Set the mode now to ensure that any nodes | |
3077 | -- generated during analysis and expansion are properly flagged | |
3078 | -- as ignored Ghost. | |
3079 | ||
3080 | Set_Ghost_Mode (N, Spec_Id); | |
3081 | ||
d44202ba HK |
3082 | else |
3083 | Spec_Id := Find_Corresponding_Spec (N); | |
b6c8e5be | 3084 | |
8636f52f HK |
3085 | -- The corresponding spec may be subject to pragma Ghost with |
3086 | -- policy Ignore. Set the mode now to ensure that any nodes | |
3087 | -- generated during analysis and expansion are properly flagged | |
3088 | -- as ignored Ghost. | |
3089 | ||
3090 | Set_Ghost_Mode (N, Spec_Id); | |
3091 | ||
b6c8e5be AC |
3092 | -- In GNATprove mode, if the body has no previous spec, create |
3093 | -- one so that the inlining machinery can operate properly. | |
3094 | -- Transfer aspects, if any, to the new spec, so that they | |
3095 | -- are legal and can be processed ahead of the body. | |
3096 | -- We make two copies of the given spec, one for the new | |
3097 | -- declaration, and one for the body. | |
3098 | ||
5a271a7f | 3099 | if No (Spec_Id) and then GNATprove_Mode |
480156b2 | 3100 | |
480156b2 AC |
3101 | -- Inlining does not apply during pre-analysis of code |
3102 | ||
b6c8e5be | 3103 | and then Full_Analysis |
480156b2 AC |
3104 | |
3105 | -- Inlining only applies to full bodies, not stubs | |
3106 | ||
7b2888e6 | 3107 | and then Nkind (N) /= N_Subprogram_Body_Stub |
480156b2 AC |
3108 | |
3109 | -- Inlining only applies to bodies in the source code, not to | |
3110 | -- those generated by the compiler. In particular, expression | |
3111 | -- functions, whose body is generated by the compiler, are | |
3112 | -- treated specially by GNATprove. | |
3113 | ||
b6c8e5be | 3114 | and then Comes_From_Source (Body_Id) |
480156b2 AC |
3115 | |
3116 | -- This cannot be done for a compilation unit, which is not | |
3117 | -- in a context where we can insert a new spec. | |
3118 | ||
b6c8e5be | 3119 | and then Is_List_Member (N) |
480156b2 AC |
3120 | |
3121 | -- Inlining only applies to subprograms without contracts, | |
3122 | -- as a contract is a sign that GNATprove should perform a | |
3123 | -- modular analysis of the subprogram instead of a contextual | |
3124 | -- analysis at each call site. The same test is performed in | |
3125 | -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated | |
3126 | -- here in another form (because the contract has not | |
3127 | -- been attached to the body) to avoid frontend errors in | |
3128 | -- case pragmas are used instead of aspects, because the | |
3129 | -- corresponding pragmas in the body would not be transferred | |
3130 | -- to the spec, leading to legality errors. | |
3131 | ||
ac072cb2 | 3132 | and then not Body_Has_Contract |
8d1fe980 | 3133 | and then not Inside_A_Generic |
b6c8e5be | 3134 | then |
8d1fe980 | 3135 | Build_Subprogram_Declaration; |
b6c8e5be | 3136 | end if; |
d44202ba | 3137 | end if; |
996ae0b0 RK |
3138 | |
3139 | -- If this is a duplicate body, no point in analyzing it | |
3140 | ||
3141 | if Error_Posted (N) then | |
3142 | return; | |
3143 | end if; | |
3144 | ||
82c80734 RD |
3145 | -- A subprogram body should cause freezing of its own declaration, |
3146 | -- but if there was no previous explicit declaration, then the | |
3147 | -- subprogram will get frozen too late (there may be code within | |
3148 | -- the body that depends on the subprogram having been frozen, | |
3149 | -- such as uses of extra formals), so we force it to be frozen | |
76a69663 | 3150 | -- here. Same holds if the body and spec are compilation units. |
cd1c668b ES |
3151 | -- Finally, if the return type is an anonymous access to protected |
3152 | -- subprogram, it must be frozen before the body because its | |
3153 | -- expansion has generated an equivalent type that is used when | |
3154 | -- elaborating the body. | |
996ae0b0 | 3155 | |
885c4871 | 3156 | -- An exception in the case of Ada 2012, AI05-177: The bodies |
ebb6faaa AC |
3157 | -- created for expression functions do not freeze. |
3158 | ||
3159 | if No (Spec_Id) | |
3160 | and then Nkind (Original_Node (N)) /= N_Expression_Function | |
3161 | then | |
996ae0b0 RK |
3162 | Freeze_Before (N, Body_Id); |
3163 | ||
3164 | elsif Nkind (Parent (N)) = N_Compilation_Unit then | |
3165 | Freeze_Before (N, Spec_Id); | |
cd1c668b ES |
3166 | |
3167 | elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then | |
3168 | Freeze_Before (N, Etype (Body_Id)); | |
996ae0b0 | 3169 | end if; |
a38ff9b1 | 3170 | |
996ae0b0 RK |
3171 | else |
3172 | Spec_Id := Corresponding_Spec (N); | |
8636f52f HK |
3173 | |
3174 | -- The corresponding spec may be subject to pragma Ghost with | |
3175 | -- policy Ignore. Set the mode now to ensure that any nodes | |
3176 | -- generated during analysis and expansion are properly flagged | |
3177 | -- as ignored Ghost. | |
3178 | ||
3179 | Set_Ghost_Mode (N, Spec_Id); | |
996ae0b0 RK |
3180 | end if; |
3181 | end if; | |
3182 | ||
799d0e05 AC |
3183 | -- Previously we scanned the body to look for nested subprograms, and |
3184 | -- rejected an inline directive if nested subprograms were present, | |
3185 | -- because the back-end would generate conflicting symbols for the | |
c8957aae | 3186 | -- nested bodies. This is now unnecessary. |
07fc65c4 | 3187 | |
c8957aae | 3188 | -- Look ahead to recognize a pragma Inline that appears after the body |
84f4072a | 3189 | |
e660dbf7 JM |
3190 | Check_Inline_Pragma (Spec_Id); |
3191 | ||
701b7fbb RD |
3192 | -- Deal with special case of a fully private operation in the body of |
3193 | -- the protected type. We must create a declaration for the subprogram, | |
3194 | -- in order to attach the protected subprogram that will be used in | |
3195 | -- internal calls. We exclude compiler generated bodies from the | |
3196 | -- expander since the issue does not arise for those cases. | |
07fc65c4 | 3197 | |
996ae0b0 RK |
3198 | if No (Spec_Id) |
3199 | and then Comes_From_Source (N) | |
3200 | and then Is_Protected_Type (Current_Scope) | |
3201 | then | |
47bfea3a | 3202 | Spec_Id := Build_Private_Protected_Declaration (N); |
701b7fbb | 3203 | end if; |
996ae0b0 | 3204 | |
5334d18f | 3205 | -- If a separate spec is present, then deal with freezing issues |
7ca78bba | 3206 | |
701b7fbb | 3207 | if Present (Spec_Id) then |
996ae0b0 | 3208 | Spec_Decl := Unit_Declaration_Node (Spec_Id); |
758c442c | 3209 | Verify_Overriding_Indicator; |
5d37ba92 ES |
3210 | |
3211 | -- In general, the spec will be frozen when we start analyzing the | |
3212 | -- body. However, for internally generated operations, such as | |
3213 | -- wrapper functions for inherited operations with controlling | |
164e06c6 AC |
3214 | -- results, the spec may not have been frozen by the time we expand |
3215 | -- the freeze actions that include the bodies. In particular, extra | |
3216 | -- formals for accessibility or for return-in-place may need to be | |
3217 | -- generated. Freeze nodes, if any, are inserted before the current | |
3218 | -- body. These freeze actions are also needed in ASIS mode to enable | |
3219 | -- the proper back-annotations. | |
5d37ba92 ES |
3220 | |
3221 | if not Is_Frozen (Spec_Id) | |
7134062a | 3222 | and then (Expander_Active or ASIS_Mode) |
5d37ba92 ES |
3223 | then |
3224 | -- Force the generation of its freezing node to ensure proper | |
3225 | -- management of access types in the backend. | |
3226 | ||
3227 | -- This is definitely needed for some cases, but it is not clear | |
3228 | -- why, to be investigated further??? | |
3229 | ||
3230 | Set_Has_Delayed_Freeze (Spec_Id); | |
6b958cec | 3231 | Freeze_Before (N, Spec_Id); |
5d37ba92 | 3232 | end if; |
996ae0b0 RK |
3233 | end if; |
3234 | ||
3235 | -- Place subprogram on scope stack, and make formals visible. If there | |
3236 | -- is a spec, the visible entity remains that of the spec. | |
3237 | ||
3238 | if Present (Spec_Id) then | |
07fc65c4 | 3239 | Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); |
758c442c GD |
3240 | |
3241 | if Is_Child_Unit (Spec_Id) then | |
3242 | Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False); | |
3243 | end if; | |
3244 | ||
fbf5a39b AC |
3245 | if Style_Check then |
3246 | Style.Check_Identifier (Body_Id, Spec_Id); | |
3247 | end if; | |
996ae0b0 RK |
3248 | |
3249 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
3250 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3251 | ||
f937473f | 3252 | if Is_Abstract_Subprogram (Spec_Id) then |
ed2233dc | 3253 | Error_Msg_N ("an abstract subprogram cannot have a body", N); |
996ae0b0 | 3254 | return; |
21d27997 | 3255 | |
996ae0b0 RK |
3256 | else |
3257 | Set_Convention (Body_Id, Convention (Spec_Id)); | |
3258 | Set_Has_Completion (Spec_Id); | |
3259 | ||
c5cec2fe AC |
3260 | -- Inherit the "ghostness" of the subprogram spec. Note that this |
3261 | -- property is not directly inherited as the body may be subject | |
3262 | -- to a different Ghost assertion policy. | |
3263 | ||
8636f52f | 3264 | if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then |
c5cec2fe AC |
3265 | Set_Is_Ghost_Entity (Body_Id); |
3266 | ||
3267 | -- The Ghost policy in effect at the point of declaration and | |
c2cfccb1 | 3268 | -- at the point of completion must match (SPARK RM 6.9(14)). |
c5cec2fe AC |
3269 | |
3270 | Check_Ghost_Completion (Spec_Id, Body_Id); | |
3271 | end if; | |
3272 | ||
996ae0b0 | 3273 | if Is_Protected_Type (Scope (Spec_Id)) then |
21d27997 | 3274 | Prot_Typ := Scope (Spec_Id); |
996ae0b0 RK |
3275 | end if; |
3276 | ||
3277 | -- If this is a body generated for a renaming, do not check for | |
3278 | -- full conformance. The check is redundant, because the spec of | |
3279 | -- the body is a copy of the spec in the renaming declaration, | |
3280 | -- and the test can lead to spurious errors on nested defaults. | |
3281 | ||
3282 | if Present (Spec_Decl) | |
996ae0b0 | 3283 | and then not Comes_From_Source (N) |
93a81b02 GB |
3284 | and then |
3285 | (Nkind (Original_Node (Spec_Decl)) = | |
4b6f99f5 | 3286 | N_Subprogram_Renaming_Declaration |
466c2127 AC |
3287 | or else (Present (Corresponding_Body (Spec_Decl)) |
3288 | and then | |
3289 | Nkind (Unit_Declaration_Node | |
3290 | (Corresponding_Body (Spec_Decl))) = | |
3291 | N_Subprogram_Renaming_Declaration)) | |
996ae0b0 RK |
3292 | then |
3293 | Conformant := True; | |
cabe9abc AC |
3294 | |
3295 | -- Conversely, the spec may have been generated for specless body | |
3296 | -- with an inline pragma. | |
3297 | ||
3298 | elsif Comes_From_Source (N) | |
3299 | and then not Comes_From_Source (Spec_Id) | |
3300 | and then Has_Pragma_Inline (Spec_Id) | |
3301 | then | |
3302 | Conformant := True; | |
76a69663 | 3303 | |
996ae0b0 RK |
3304 | else |
3305 | Check_Conformance | |
3306 | (Body_Id, Spec_Id, | |
76a69663 | 3307 | Fully_Conformant, True, Conformant, Body_Id); |
996ae0b0 RK |
3308 | end if; |
3309 | ||
3310 | -- If the body is not fully conformant, we have to decide if we | |
3311 | -- should analyze it or not. If it has a really messed up profile | |
3312 | -- then we probably should not analyze it, since we will get too | |
3313 | -- many bogus messages. | |
3314 | ||
3315 | -- Our decision is to go ahead in the non-fully conformant case | |
3316 | -- only if it is at least mode conformant with the spec. Note | |
3317 | -- that the call to Check_Fully_Conformant has issued the proper | |
3318 | -- error messages to complain about the lack of conformance. | |
3319 | ||
3320 | if not Conformant | |
3321 | and then not Mode_Conformant (Body_Id, Spec_Id) | |
3322 | then | |
3323 | return; | |
3324 | end if; | |
3325 | end if; | |
3326 | ||
996ae0b0 | 3327 | if Spec_Id /= Body_Id then |
fbf5a39b | 3328 | Reference_Body_Formals (Spec_Id, Body_Id); |
996ae0b0 RK |
3329 | end if; |
3330 | ||
579847c2 AC |
3331 | Set_Ekind (Body_Id, E_Subprogram_Body); |
3332 | ||
e28072cd AC |
3333 | if Nkind (N) = N_Subprogram_Body_Stub then |
3334 | Set_Corresponding_Spec_Of_Stub (N, Spec_Id); | |
3335 | ||
3336 | -- Regular body | |
3337 | ||
3338 | else | |
996ae0b0 | 3339 | Set_Corresponding_Spec (N, Spec_Id); |
758c442c | 3340 | |
5d37ba92 ES |
3341 | -- Ada 2005 (AI-345): If the operation is a primitive operation |
3342 | -- of a concurrent type, the type of the first parameter has been | |
3343 | -- replaced with the corresponding record, which is the proper | |
3344 | -- run-time structure to use. However, within the body there may | |
3345 | -- be uses of the formals that depend on primitive operations | |
3346 | -- of the type (in particular calls in prefixed form) for which | |
3347 | -- we need the original concurrent type. The operation may have | |
3348 | -- several controlling formals, so the replacement must be done | |
3349 | -- for all of them. | |
758c442c GD |
3350 | |
3351 | if Comes_From_Source (Spec_Id) | |
3352 | and then Present (First_Entity (Spec_Id)) | |
3353 | and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type | |
3354 | and then Is_Tagged_Type (Etype (First_Entity (Spec_Id))) | |
15918371 AC |
3355 | and then Present (Interfaces (Etype (First_Entity (Spec_Id)))) |
3356 | and then Present (Corresponding_Concurrent_Type | |
3357 | (Etype (First_Entity (Spec_Id)))) | |
758c442c | 3358 | then |
5d37ba92 ES |
3359 | declare |
3360 | Typ : constant Entity_Id := Etype (First_Entity (Spec_Id)); | |
3361 | Form : Entity_Id; | |
3362 | ||
3363 | begin | |
3364 | Form := First_Formal (Spec_Id); | |
3365 | while Present (Form) loop | |
3366 | if Etype (Form) = Typ then | |
3367 | Set_Etype (Form, Corresponding_Concurrent_Type (Typ)); | |
3368 | end if; | |
3369 | ||
3370 | Next_Formal (Form); | |
3371 | end loop; | |
3372 | end; | |
758c442c GD |
3373 | end if; |
3374 | ||
21d27997 RD |
3375 | -- Make the formals visible, and place subprogram on scope stack. |
3376 | -- This is also the point at which we set Last_Real_Spec_Entity | |
3377 | -- to mark the entities which will not be moved to the body. | |
758c442c | 3378 | |
996ae0b0 | 3379 | Install_Formals (Spec_Id); |
21d27997 | 3380 | Last_Real_Spec_Entity := Last_Entity (Spec_Id); |
616547fa AC |
3381 | |
3382 | -- Within an instance, add local renaming declarations so that | |
a5a809b2 AC |
3383 | -- gdb can retrieve the values of actuals more easily. This is |
3384 | -- only relevant if generating code (and indeed we definitely | |
3385 | -- do not want these definitions -gnatc mode, because that would | |
3386 | -- confuse ASIS). | |
616547fa AC |
3387 | |
3388 | if Is_Generic_Instance (Spec_Id) | |
3389 | and then Is_Wrapper_Package (Current_Scope) | |
a5a809b2 | 3390 | and then Expander_Active |
616547fa AC |
3391 | then |
3392 | Build_Subprogram_Instance_Renamings (N, Current_Scope); | |
3393 | end if; | |
3394 | ||
0a36105d | 3395 | Push_Scope (Spec_Id); |
996ae0b0 RK |
3396 | |
3397 | -- Make sure that the subprogram is immediately visible. For | |
3398 | -- child units that have no separate spec this is indispensable. | |
3399 | -- Otherwise it is safe albeit redundant. | |
3400 | ||
3401 | Set_Is_Immediately_Visible (Spec_Id); | |
3402 | end if; | |
3403 | ||
3404 | Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id); | |
ec4867fa | 3405 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); |
c9d70ab1 | 3406 | Set_Scope (Body_Id, Scope (Spec_Id)); |
996ae0b0 RK |
3407 | |
3408 | -- Case of subprogram body with no previous spec | |
3409 | ||
3410 | else | |
3e5daac4 AC |
3411 | -- Check for style warning required |
3412 | ||
996ae0b0 | 3413 | if Style_Check |
3e5daac4 AC |
3414 | |
3415 | -- Only apply check for source level subprograms for which checks | |
3416 | -- have not been suppressed. | |
3417 | ||
996ae0b0 RK |
3418 | and then Comes_From_Source (Body_Id) |
3419 | and then not Suppress_Style_Checks (Body_Id) | |
3e5daac4 AC |
3420 | |
3421 | -- No warnings within an instance | |
3422 | ||
996ae0b0 | 3423 | and then not In_Instance |
3e5daac4 | 3424 | |
b0186f71 | 3425 | -- No warnings for expression functions |
3e5daac4 | 3426 | |
b0186f71 | 3427 | and then Nkind (Original_Node (N)) /= N_Expression_Function |
996ae0b0 RK |
3428 | then |
3429 | Style.Body_With_No_Spec (N); | |
3430 | end if; | |
3431 | ||
3432 | New_Overloaded_Entity (Body_Id); | |
3433 | ||
3434 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
3435 | Set_Acts_As_Spec (N); | |
3436 | Generate_Definition (Body_Id); | |
fbf5a39b AC |
3437 | Generate_Reference |
3438 | (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True); | |
996ae0b0 | 3439 | Install_Formals (Body_Id); |
e949ee22 | 3440 | |
4a854847 | 3441 | Push_Scope (Body_Id); |
996ae0b0 | 3442 | end if; |
dbe36d67 AC |
3443 | |
3444 | -- For stubs and bodies with no previous spec, generate references to | |
3445 | -- formals. | |
3446 | ||
3447 | Generate_Reference_To_Formals (Body_Id); | |
996ae0b0 RK |
3448 | end if; |
3449 | ||
5216b599 AC |
3450 | -- Set SPARK_Mode from context |
3451 | ||
3452 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); | |
3453 | Set_SPARK_Pragma_Inherited (Body_Id, True); | |
3454 | ||
76a69663 ES |
3455 | -- If the return type is an anonymous access type whose designated type |
3456 | -- is the limited view of a class-wide type and the non-limited view is | |
3457 | -- available, update the return type accordingly. | |
ec4867fa | 3458 | |
8fde064e | 3459 | if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then |
ec4867fa | 3460 | declare |
ec4867fa | 3461 | Etyp : Entity_Id; |
0a36105d | 3462 | Rtyp : Entity_Id; |
ec4867fa ES |
3463 | |
3464 | begin | |
0a36105d JM |
3465 | Rtyp := Etype (Current_Scope); |
3466 | ||
3467 | if Ekind (Rtyp) = E_Anonymous_Access_Type then | |
3468 | Etyp := Directly_Designated_Type (Rtyp); | |
3469 | ||
7b56a91b AC |
3470 | if Is_Class_Wide_Type (Etyp) |
3471 | and then From_Limited_With (Etyp) | |
3472 | then | |
0a36105d JM |
3473 | Set_Directly_Designated_Type |
3474 | (Etype (Current_Scope), Available_View (Etyp)); | |
3475 | end if; | |
3476 | end if; | |
ec4867fa ES |
3477 | end; |
3478 | end if; | |
3479 | ||
996ae0b0 RK |
3480 | -- If this is the proper body of a stub, we must verify that the stub |
3481 | -- conforms to the body, and to the previous spec if one was present. | |
dbe36d67 | 3482 | -- We know already that the body conforms to that spec. This test is |
996ae0b0 RK |
3483 | -- only required for subprograms that come from source. |
3484 | ||
3485 | if Nkind (Parent (N)) = N_Subunit | |
3486 | and then Comes_From_Source (N) | |
3487 | and then not Error_Posted (Body_Id) | |
e895b435 ES |
3488 | and then Nkind (Corresponding_Stub (Parent (N))) = |
3489 | N_Subprogram_Body_Stub | |
996ae0b0 RK |
3490 | then |
3491 | declare | |
fbf5a39b AC |
3492 | Old_Id : constant Entity_Id := |
3493 | Defining_Entity | |
3494 | (Specification (Corresponding_Stub (Parent (N)))); | |
3495 | ||
996ae0b0 | 3496 | Conformant : Boolean := False; |
996ae0b0 RK |
3497 | |
3498 | begin | |
3499 | if No (Spec_Id) then | |
3500 | Check_Fully_Conformant (Body_Id, Old_Id); | |
3501 | ||
3502 | else | |
3503 | Check_Conformance | |
3504 | (Body_Id, Old_Id, Fully_Conformant, False, Conformant); | |
3505 | ||
3506 | if not Conformant then | |
3507 | ||
dbe36d67 AC |
3508 | -- The stub was taken to be a new declaration. Indicate that |
3509 | -- it lacks a body. | |
996ae0b0 RK |
3510 | |
3511 | Set_Has_Completion (Old_Id, False); | |
3512 | end if; | |
3513 | end if; | |
3514 | end; | |
3515 | end if; | |
3516 | ||
3517 | Set_Has_Completion (Body_Id); | |
3518 | Check_Eliminated (Body_Id); | |
3519 | ||
caf07df9 AC |
3520 | -- Analyze any aspect specifications that appear on the subprogram body |
3521 | -- stub. Stop the analysis now as the stub does not have a declarative | |
3522 | -- or a statement part, and it cannot be inlined. | |
c8a3028c | 3523 | |
caf07df9 | 3524 | if Nkind (N) = N_Subprogram_Body_Stub then |
c8a3028c | 3525 | if Has_Aspects (N) then |
caf07df9 | 3526 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); |
c8a3028c AC |
3527 | end if; |
3528 | ||
996ae0b0 | 3529 | return; |
84f4072a | 3530 | end if; |
996ae0b0 | 3531 | |
b94b6c56 | 3532 | -- Handle frontend inlining |
84f4072a | 3533 | |
b94b6c56 RD |
3534 | -- Note: Normally we don't do any inlining if expansion is off, since |
3535 | -- we won't generate code in any case. An exception arises in GNATprove | |
2d180af1 YM |
3536 | -- mode where we want to expand some calls in place, even with expansion |
3537 | -- disabled, since the inlining eases formal verification. | |
ecad37f3 | 3538 | |
6c26bac2 AC |
3539 | if not GNATprove_Mode |
3540 | and then Expander_Active | |
3541 | and then Serious_Errors_Detected = 0 | |
3542 | and then Present (Spec_Id) | |
3543 | and then Has_Pragma_Inline (Spec_Id) | |
3544 | then | |
3545 | -- Legacy implementation (relying on frontend inlining) | |
84f4072a | 3546 | |
6c26bac2 | 3547 | if not Back_End_Inlining then |
17ce1f52 AC |
3548 | if (Has_Pragma_Inline_Always (Spec_Id) |
3549 | and then not Opt.Disable_FE_Inline_Always) | |
3550 | or else | |
3551 | (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining | |
3552 | and then not Opt.Disable_FE_Inline) | |
6c26bac2 AC |
3553 | then |
3554 | Build_Body_To_Inline (N, Spec_Id); | |
3555 | end if; | |
f087ea44 | 3556 | |
88f7d2d1 | 3557 | -- New implementation (relying on backend inlining) |
f087ea44 | 3558 | |
6c26bac2 AC |
3559 | else |
3560 | if Has_Pragma_Inline_Always (Spec_Id) | |
3561 | or else Optimization_Level > 0 | |
3562 | then | |
3563 | -- Handle function returning an unconstrained type | |
f087ea44 | 3564 | |
6c26bac2 AC |
3565 | if Comes_From_Source (Body_Id) |
3566 | and then Ekind (Spec_Id) = E_Function | |
3567 | and then Returns_Unconstrained_Type (Spec_Id) | |
5c5e108f AC |
3568 | |
3569 | -- If function builds in place, i.e. returns a limited type, | |
3570 | -- inlining cannot be done. | |
3571 | ||
3572 | and then not Is_Limited_Type (Etype (Spec_Id)) | |
6c26bac2 | 3573 | then |
16b10ccc | 3574 | Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id); |
84f4072a | 3575 | |
6c26bac2 AC |
3576 | else |
3577 | declare | |
b80a2b4b AC |
3578 | Subp_Body : constant Node_Id := |
3579 | Unit_Declaration_Node (Body_Id); | |
6c26bac2 | 3580 | Subp_Decl : constant List_Id := Declarations (Subp_Body); |
84f4072a | 3581 | |
6c26bac2 AC |
3582 | begin |
3583 | -- Do not pass inlining to the backend if the subprogram | |
3584 | -- has declarations or statements which cannot be inlined | |
3585 | -- by the backend. This check is done here to emit an | |
3586 | -- error instead of the generic warning message reported | |
3587 | -- by the GCC backend (ie. "function might not be | |
3588 | -- inlinable"). | |
3589 | ||
3590 | if Present (Subp_Decl) | |
3591 | and then Has_Excluded_Declaration (Spec_Id, Subp_Decl) | |
3592 | then | |
3593 | null; | |
3594 | ||
3595 | elsif Has_Excluded_Statement | |
3596 | (Spec_Id, | |
3597 | Statements | |
3598 | (Handled_Statement_Sequence (Subp_Body))) | |
3599 | then | |
3600 | null; | |
3601 | ||
3602 | -- If the backend inlining is available then at this | |
3603 | -- stage we only have to mark the subprogram as inlined. | |
3604 | -- The expander will take care of registering it in the | |
3605 | -- table of subprograms inlined by the backend a part of | |
3606 | -- processing calls to it (cf. Expand_Call) | |
3607 | ||
3608 | else | |
3609 | Set_Is_Inlined (Spec_Id); | |
3610 | end if; | |
3611 | end; | |
3612 | end if; | |
3613 | end if; | |
3614 | end if; | |
2d180af1 YM |
3615 | |
3616 | -- In GNATprove mode, inline only when there is a separate subprogram | |
3617 | -- declaration for now, as inlining of subprogram bodies acting as | |
3618 | -- declarations, or subprogram stubs, are not supported by frontend | |
3619 | -- inlining. This inlining should occur after analysis of the body, so | |
3620 | -- that it is known whether the value of SPARK_Mode applicable to the | |
3621 | -- body, which can be defined by a pragma inside the body. | |
3622 | ||
3623 | elsif GNATprove_Mode | |
2d180af1 YM |
3624 | and then Full_Analysis |
3625 | and then not Inside_A_Generic | |
3626 | and then Present (Spec_Id) | |
7c4d86c9 AC |
3627 | and then |
3628 | Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration | |
2d180af1 | 3629 | and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id) |
ac072cb2 | 3630 | and then not Body_Has_Contract |
2d180af1 | 3631 | then |
6c26bac2 | 3632 | Build_Body_To_Inline (N, Spec_Id); |
996ae0b0 RK |
3633 | end if; |
3634 | ||
0ab80019 | 3635 | -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis |
dbe36d67 | 3636 | -- of the specification we have to install the private withed units. |
21d27997 | 3637 | -- This holds for child units as well. |
9bc856dd AC |
3638 | |
3639 | if Is_Compilation_Unit (Body_Id) | |
21d27997 | 3640 | or else Nkind (Parent (N)) = N_Compilation_Unit |
9bc856dd AC |
3641 | then |
3642 | Install_Private_With_Clauses (Body_Id); | |
3643 | end if; | |
3644 | ||
ec4867fa ES |
3645 | Check_Anonymous_Return; |
3646 | ||
fdce4bb7 JM |
3647 | -- Set the Protected_Formal field of each extra formal of the protected |
3648 | -- subprogram to reference the corresponding extra formal of the | |
3649 | -- subprogram that implements it. For regular formals this occurs when | |
3650 | -- the protected subprogram's declaration is expanded, but the extra | |
3651 | -- formals don't get created until the subprogram is frozen. We need to | |
3652 | -- do this before analyzing the protected subprogram's body so that any | |
3653 | -- references to the original subprogram's extra formals will be changed | |
3654 | -- refer to the implementing subprogram's formals (see Expand_Formal). | |
3655 | ||
3656 | if Present (Spec_Id) | |
3657 | and then Is_Protected_Type (Scope (Spec_Id)) | |
3658 | and then Present (Protected_Body_Subprogram (Spec_Id)) | |
3659 | then | |
3660 | declare | |
3661 | Impl_Subp : constant Entity_Id := | |
3662 | Protected_Body_Subprogram (Spec_Id); | |
3663 | Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id); | |
3664 | Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp); | |
fdce4bb7 JM |
3665 | begin |
3666 | while Present (Prot_Ext_Formal) loop | |
3667 | pragma Assert (Present (Impl_Ext_Formal)); | |
fdce4bb7 | 3668 | Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal); |
fdce4bb7 JM |
3669 | Next_Formal_With_Extras (Prot_Ext_Formal); |
3670 | Next_Formal_With_Extras (Impl_Ext_Formal); | |
3671 | end loop; | |
3672 | end; | |
3673 | end if; | |
3674 | ||
0868e09c | 3675 | -- Now we can go on to analyze the body |
996ae0b0 RK |
3676 | |
3677 | HSS := Handled_Statement_Sequence (N); | |
3678 | Set_Actual_Subtypes (N, Current_Scope); | |
21d27997 | 3679 | |
f3d0f304 | 3680 | -- Add a declaration for the Protection object, renaming declarations |
21d27997 RD |
3681 | -- for discriminals and privals and finally a declaration for the entry |
3682 | -- family index (if applicable). This form of early expansion is done | |
3683 | -- when the Expander is active because Install_Private_Data_Declarations | |
81bf2382 | 3684 | -- references entities which were created during regular expansion. The |
3b8056a5 AC |
3685 | -- subprogram entity must come from source, and not be an internally |
3686 | -- generated subprogram. | |
21d27997 | 3687 | |
4460a9bc | 3688 | if Expander_Active |
21d27997 RD |
3689 | and then Present (Prot_Typ) |
3690 | and then Present (Spec_Id) | |
3b8056a5 | 3691 | and then Comes_From_Source (Spec_Id) |
21d27997 RD |
3692 | and then not Is_Eliminated (Spec_Id) |
3693 | then | |
3694 | Install_Private_Data_Declarations | |
3695 | (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N)); | |
3696 | end if; | |
3697 | ||
5dcab3ca AC |
3698 | -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context |
3699 | -- may now appear in parameter and result profiles. Since the analysis | |
3700 | -- of a subprogram body may use the parameter and result profile of the | |
3701 | -- spec, swap any limited views with their non-limited counterpart. | |
3702 | ||
3703 | if Ada_Version >= Ada_2012 then | |
3704 | Exchange_Limited_Views (Spec_Id); | |
3705 | end if; | |
3706 | ||
c8a3028c AC |
3707 | -- Analyze any aspect specifications that appear on the subprogram body |
3708 | ||
3709 | if Has_Aspects (N) then | |
caf07df9 | 3710 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); |
c8a3028c AC |
3711 | end if; |
3712 | ||
996ae0b0 | 3713 | Analyze_Declarations (Declarations (N)); |
21d27997 | 3714 | |
f3124d8f | 3715 | -- Verify that the SPARK_Mode of the body agrees with that of its spec |
f90d14ac | 3716 | |
f1c7be38 | 3717 | if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then |
f90d14ac AC |
3718 | if Present (SPARK_Pragma (Spec_Id)) then |
3719 | if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off | |
3720 | and then | |
3721 | Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On | |
3722 | then | |
3723 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
3724 | Error_Msg_N ("incorrect application of SPARK_Mode#", N); | |
3725 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id)); | |
3726 | Error_Msg_NE | |
f3124d8f | 3727 | ("\value Off was set for SPARK_Mode on & #", N, Spec_Id); |
f90d14ac AC |
3728 | end if; |
3729 | ||
3730 | elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then | |
3731 | null; | |
3732 | ||
3733 | else | |
3734 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
3ccedacc | 3735 | Error_Msg_N ("incorrect application of SPARK_Mode #", N); |
f90d14ac | 3736 | Error_Msg_Sloc := Sloc (Spec_Id); |
f3124d8f HK |
3737 | Error_Msg_NE |
3738 | ("\no value was set for SPARK_Mode on & #", N, Spec_Id); | |
f90d14ac AC |
3739 | end if; |
3740 | end if; | |
3741 | ||
c9d70ab1 AC |
3742 | -- When a subprogram body appears inside a package, its contract is |
3743 | -- analyzed at the end of the package body declarations. This is due | |
3744 | -- to the delay with respect of the package contract upon which the | |
3745 | -- body contract may depend. When the subprogram body is stand alone | |
3746 | -- and acts as a compilation unit, this delay is not necessary. | |
3747 | ||
3748 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
3749 | Analyze_Subprogram_Body_Contract (Body_Id); | |
3750 | end if; | |
3751 | ||
3752 | -- Deal with preconditions, [refined] postconditions, Contract_Cases, | |
3753 | -- invariants and predicates associated with body and its spec. Since | |
3754 | -- there is no routine Expand_Declarations which would otherwise deal | |
3755 | -- with the contract expansion, generate all necessary mechanisms to | |
3756 | -- verify the contract assertions now. | |
3757 | ||
3758 | Expand_Subprogram_Contract (N); | |
3759 | ||
ac43e11e AC |
3760 | -- If SPARK_Mode for body is not On, disable frontend inlining for this |
3761 | -- subprogram in GNATprove mode, as its body should not be analyzed. | |
3762 | ||
3763 | if SPARK_Mode /= On | |
3764 | and then GNATprove_Mode | |
ac43e11e AC |
3765 | and then Present (Spec_Id) |
3766 | and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration | |
3767 | then | |
3768 | Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty); | |
4bd4bb7f | 3769 | Set_Is_Inlined_Always (Spec_Id, False); |
ac43e11e AC |
3770 | end if; |
3771 | ||
21d27997 RD |
3772 | -- Check completion, and analyze the statements |
3773 | ||
996ae0b0 | 3774 | Check_Completion; |
33931112 | 3775 | Inspect_Deferred_Constant_Completion (Declarations (N)); |
996ae0b0 | 3776 | Analyze (HSS); |
21d27997 RD |
3777 | |
3778 | -- Deal with end of scope processing for the body | |
3779 | ||
07fc65c4 | 3780 | Process_End_Label (HSS, 't', Current_Scope); |
996ae0b0 RK |
3781 | End_Scope; |
3782 | Check_Subprogram_Order (N); | |
c37bb106 | 3783 | Set_Analyzed (Body_Id); |
996ae0b0 RK |
3784 | |
3785 | -- If we have a separate spec, then the analysis of the declarations | |
3786 | -- caused the entities in the body to be chained to the spec id, but | |
3787 | -- we want them chained to the body id. Only the formal parameters | |
3788 | -- end up chained to the spec id in this case. | |
3789 | ||
3790 | if Present (Spec_Id) then | |
3791 | ||
d39d6bb8 | 3792 | -- We must conform to the categorization of our spec |
996ae0b0 | 3793 | |
d39d6bb8 | 3794 | Validate_Categorization_Dependency (N, Spec_Id); |
996ae0b0 | 3795 | |
d39d6bb8 RD |
3796 | -- And if this is a child unit, the parent units must conform |
3797 | ||
3798 | if Is_Child_Unit (Spec_Id) then | |
996ae0b0 RK |
3799 | Validate_Categorization_Dependency |
3800 | (Unit_Declaration_Node (Spec_Id), Spec_Id); | |
3801 | end if; | |
3802 | ||
21d27997 RD |
3803 | -- Here is where we move entities from the spec to the body |
3804 | ||
3805 | -- Case where there are entities that stay with the spec | |
3806 | ||
3807 | if Present (Last_Real_Spec_Entity) then | |
3808 | ||
dbe36d67 AC |
3809 | -- No body entities (happens when the only real spec entities come |
3810 | -- from precondition and postcondition pragmas). | |
21d27997 RD |
3811 | |
3812 | if No (Last_Entity (Body_Id)) then | |
7c4d86c9 | 3813 | Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity)); |
21d27997 RD |
3814 | |
3815 | -- Body entities present (formals), so chain stuff past them | |
3816 | ||
3817 | else | |
3818 | Set_Next_Entity | |
3819 | (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity)); | |
3820 | end if; | |
3821 | ||
3822 | Set_Next_Entity (Last_Real_Spec_Entity, Empty); | |
996ae0b0 | 3823 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); |
21d27997 RD |
3824 | Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity); |
3825 | ||
dbe36d67 AC |
3826 | -- Case where there are no spec entities, in this case there can be |
3827 | -- no body entities either, so just move everything. | |
996ae0b0 | 3828 | |
a921e83c AC |
3829 | -- If the body is generated for an expression function, it may have |
3830 | -- been preanalyzed already, if 'access was applied to it. | |
3831 | ||
996ae0b0 | 3832 | else |
a921e83c AC |
3833 | if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /= |
3834 | N_Expression_Function | |
3835 | then | |
3836 | pragma Assert (No (Last_Entity (Body_Id))); | |
3837 | null; | |
3838 | end if; | |
3839 | ||
996ae0b0 RK |
3840 | Set_First_Entity (Body_Id, First_Entity (Spec_Id)); |
3841 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); | |
3842 | Set_First_Entity (Spec_Id, Empty); | |
3843 | Set_Last_Entity (Spec_Id, Empty); | |
3844 | end if; | |
3845 | end if; | |
3846 | ||
7665e4bd | 3847 | Check_Missing_Return; |
996ae0b0 | 3848 | |
82c80734 | 3849 | -- Now we are going to check for variables that are never modified in |
76a69663 ES |
3850 | -- the body of the procedure. But first we deal with a special case |
3851 | -- where we want to modify this check. If the body of the subprogram | |
3852 | -- starts with a raise statement or its equivalent, or if the body | |
cf3e6845 AC |
3853 | -- consists entirely of a null statement, then it is pretty obvious that |
3854 | -- it is OK to not reference the parameters. For example, this might be | |
3855 | -- the following common idiom for a stubbed function: statement of the | |
3856 | -- procedure raises an exception. In particular this deals with the | |
3857 | -- common idiom of a stubbed function, which appears something like: | |
fbf5a39b AC |
3858 | |
3859 | -- function F (A : Integer) return Some_Type; | |
3860 | -- X : Some_Type; | |
3861 | -- begin | |
3862 | -- raise Program_Error; | |
3863 | -- return X; | |
3864 | -- end F; | |
3865 | ||
76a69663 ES |
3866 | -- Here the purpose of X is simply to satisfy the annoying requirement |
3867 | -- in Ada that there be at least one return, and we certainly do not | |
a90bd866 | 3868 | -- want to go posting warnings on X that it is not initialized. On |
76a69663 ES |
3869 | -- the other hand, if X is entirely unreferenced that should still |
3870 | -- get a warning. | |
3871 | ||
3872 | -- What we do is to detect these cases, and if we find them, flag the | |
3873 | -- subprogram as being Is_Trivial_Subprogram and then use that flag to | |
3874 | -- suppress unwanted warnings. For the case of the function stub above | |
3875 | -- we have a special test to set X as apparently assigned to suppress | |
3876 | -- the warning. | |
996ae0b0 RK |
3877 | |
3878 | declare | |
800621e0 | 3879 | Stm : Node_Id; |
996ae0b0 RK |
3880 | |
3881 | begin | |
0a36105d JM |
3882 | -- Skip initial labels (for one thing this occurs when we are in |
3883 | -- front end ZCX mode, but in any case it is irrelevant), and also | |
3884 | -- initial Push_xxx_Error_Label nodes, which are also irrelevant. | |
fbf5a39b | 3885 | |
800621e0 | 3886 | Stm := First (Statements (HSS)); |
0a36105d JM |
3887 | while Nkind (Stm) = N_Label |
3888 | or else Nkind (Stm) in N_Push_xxx_Label | |
3889 | loop | |
996ae0b0 | 3890 | Next (Stm); |
0a36105d | 3891 | end loop; |
996ae0b0 | 3892 | |
fbf5a39b AC |
3893 | -- Do the test on the original statement before expansion |
3894 | ||
3895 | declare | |
3896 | Ostm : constant Node_Id := Original_Node (Stm); | |
3897 | ||
3898 | begin | |
76a69663 | 3899 | -- If explicit raise statement, turn on flag |
fbf5a39b AC |
3900 | |
3901 | if Nkind (Ostm) = N_Raise_Statement then | |
76a69663 ES |
3902 | Set_Trivial_Subprogram (Stm); |
3903 | ||
f3d57416 | 3904 | -- If null statement, and no following statements, turn on flag |
76a69663 ES |
3905 | |
3906 | elsif Nkind (Stm) = N_Null_Statement | |
3907 | and then Comes_From_Source (Stm) | |
3908 | and then No (Next (Stm)) | |
3909 | then | |
3910 | Set_Trivial_Subprogram (Stm); | |
fbf5a39b AC |
3911 | |
3912 | -- Check for explicit call cases which likely raise an exception | |
3913 | ||
3914 | elsif Nkind (Ostm) = N_Procedure_Call_Statement then | |
3915 | if Is_Entity_Name (Name (Ostm)) then | |
3916 | declare | |
3917 | Ent : constant Entity_Id := Entity (Name (Ostm)); | |
3918 | ||
3919 | begin | |
3920 | -- If the procedure is marked No_Return, then likely it | |
3921 | -- raises an exception, but in any case it is not coming | |
76a69663 | 3922 | -- back here, so turn on the flag. |
fbf5a39b | 3923 | |
f46faa08 AC |
3924 | if Present (Ent) |
3925 | and then Ekind (Ent) = E_Procedure | |
fbf5a39b AC |
3926 | and then No_Return (Ent) |
3927 | then | |
76a69663 | 3928 | Set_Trivial_Subprogram (Stm); |
fbf5a39b AC |
3929 | end if; |
3930 | end; | |
3931 | end if; | |
3932 | end if; | |
3933 | end; | |
996ae0b0 RK |
3934 | end; |
3935 | ||
3936 | -- Check for variables that are never modified | |
3937 | ||
3938 | declare | |
3939 | E1, E2 : Entity_Id; | |
3940 | ||
3941 | begin | |
fbf5a39b | 3942 | -- If there is a separate spec, then transfer Never_Set_In_Source |
996ae0b0 RK |
3943 | -- flags from out parameters to the corresponding entities in the |
3944 | -- body. The reason we do that is we want to post error flags on | |
3945 | -- the body entities, not the spec entities. | |
3946 | ||
3947 | if Present (Spec_Id) then | |
3948 | E1 := First_Entity (Spec_Id); | |
996ae0b0 RK |
3949 | while Present (E1) loop |
3950 | if Ekind (E1) = E_Out_Parameter then | |
3951 | E2 := First_Entity (Body_Id); | |
fbf5a39b | 3952 | while Present (E2) loop |
996ae0b0 RK |
3953 | exit when Chars (E1) = Chars (E2); |
3954 | Next_Entity (E2); | |
3955 | end loop; | |
3956 | ||
fbf5a39b AC |
3957 | if Present (E2) then |
3958 | Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1)); | |
3959 | end if; | |
996ae0b0 RK |
3960 | end if; |
3961 | ||
3962 | Next_Entity (E1); | |
3963 | end loop; | |
3964 | end if; | |
3965 | ||
2aca76d6 | 3966 | -- Check references in body |
0868e09c | 3967 | |
2aca76d6 | 3968 | Check_References (Body_Id); |
996ae0b0 | 3969 | end; |
5a271a7f RD |
3970 | |
3971 | -- Check for nested subprogram, and mark outer level subprogram if so | |
3972 | ||
3973 | declare | |
3974 | Ent : Entity_Id; | |
3975 | ||
3976 | begin | |
3977 | if Present (Spec_Id) then | |
3978 | Ent := Spec_Id; | |
3979 | else | |
3980 | Ent := Body_Id; | |
3981 | end if; | |
3982 | ||
3983 | loop | |
3984 | Ent := Enclosing_Subprogram (Ent); | |
3985 | exit when No (Ent) or else Is_Subprogram (Ent); | |
3986 | end loop; | |
3987 | ||
3988 | if Present (Ent) then | |
3989 | Set_Has_Nested_Subprogram (Ent); | |
3990 | end if; | |
3991 | end; | |
b1b543d2 | 3992 | end Analyze_Subprogram_Body_Helper; |
996ae0b0 | 3993 | |
5afe5d2d HK |
3994 | --------------------------------- |
3995 | -- Analyze_Subprogram_Contract -- | |
3996 | --------------------------------- | |
3997 | ||
c9d70ab1 | 3998 | procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is |
caf07df9 AC |
3999 | Items : constant Node_Id := Contract (Subp_Id); |
4000 | Depends : Node_Id := Empty; | |
4001 | Global : Node_Id := Empty; | |
4002 | Mode : SPARK_Mode_Type; | |
4003 | Prag : Node_Id; | |
4004 | Prag_Nam : Name_Id; | |
5afe5d2d HK |
4005 | |
4006 | begin | |
fc999c5d RD |
4007 | -- Due to the timing of contract analysis, delayed pragmas may be |
4008 | -- subject to the wrong SPARK_Mode, usually that of the enclosing | |
4009 | -- context. To remedy this, restore the original SPARK_Mode of the | |
4010 | -- related subprogram body. | |
4011 | ||
c9d70ab1 AC |
4012 | Save_SPARK_Mode_And_Set (Subp_Id, Mode); |
4013 | ||
caf07df9 AC |
4014 | -- All subprograms carry a contract, but for some it is not significant |
4015 | -- and should not be processed. | |
c9d70ab1 | 4016 | |
caf07df9 AC |
4017 | if not Has_Significant_Contract (Subp_Id) then |
4018 | null; | |
c61ef416 | 4019 | |
caf07df9 | 4020 | elsif Present (Items) then |
5afe5d2d HK |
4021 | |
4022 | -- Analyze pre- and postconditions | |
4023 | ||
4024 | Prag := Pre_Post_Conditions (Items); | |
4025 | while Present (Prag) loop | |
c9d70ab1 | 4026 | Analyze_Pre_Post_Condition_In_Decl_Part (Prag); |
5afe5d2d HK |
4027 | Prag := Next_Pragma (Prag); |
4028 | end loop; | |
4029 | ||
4030 | -- Analyze contract-cases and test-cases | |
4031 | ||
4032 | Prag := Contract_Test_Cases (Items); | |
4033 | while Present (Prag) loop | |
c9d70ab1 | 4034 | Prag_Nam := Pragma_Name (Prag); |
ea3c0651 | 4035 | |
c9d70ab1 | 4036 | if Prag_Nam = Name_Contract_Cases then |
5afe5d2d | 4037 | Analyze_Contract_Cases_In_Decl_Part (Prag); |
5afe5d2d | 4038 | else |
c9d70ab1 AC |
4039 | pragma Assert (Prag_Nam = Name_Test_Case); |
4040 | Analyze_Test_Case_In_Decl_Part (Prag); | |
5afe5d2d HK |
4041 | end if; |
4042 | ||
4043 | Prag := Next_Pragma (Prag); | |
4044 | end loop; | |
4045 | ||
4046 | -- Analyze classification pragmas | |
4047 | ||
6c3c671e | 4048 | Prag := Classifications (Items); |
5afe5d2d | 4049 | while Present (Prag) loop |
c9d70ab1 | 4050 | Prag_Nam := Pragma_Name (Prag); |
ea3c0651 | 4051 | |
c9d70ab1 | 4052 | if Prag_Nam = Name_Depends then |
54e28df2 | 4053 | Depends := Prag; |
039538bc | 4054 | |
c9d70ab1 | 4055 | elsif Prag_Nam = Name_Global then |
54e28df2 | 4056 | Global := Prag; |
039538bc AC |
4057 | |
4058 | -- Note that pragma Extensions_Visible has already been analyzed | |
4059 | ||
5afe5d2d HK |
4060 | end if; |
4061 | ||
4062 | Prag := Next_Pragma (Prag); | |
4063 | end loop; | |
54e28df2 HK |
4064 | |
4065 | -- Analyze Global first as Depends may mention items classified in | |
4066 | -- the global categorization. | |
4067 | ||
4068 | if Present (Global) then | |
4069 | Analyze_Global_In_Decl_Part (Global); | |
4070 | end if; | |
4071 | ||
4072 | -- Depends must be analyzed after Global in order to see the modes of | |
4073 | -- all global items. | |
4074 | ||
4075 | if Present (Depends) then | |
4076 | Analyze_Depends_In_Decl_Part (Depends); | |
4077 | end if; | |
5afe5d2d | 4078 | |
c9d70ab1 AC |
4079 | -- Ensure that the contract cases or postconditions mention 'Result |
4080 | -- or define a post-state. | |
5afe5d2d | 4081 | |
c9d70ab1 AC |
4082 | Check_Result_And_Post_State (Subp_Id); |
4083 | end if; | |
5afe5d2d | 4084 | |
fc999c5d RD |
4085 | -- Restore the SPARK_Mode of the enclosing context after all delayed |
4086 | -- pragmas have been analyzed. | |
4087 | ||
c61ef416 | 4088 | Restore_SPARK_Mode (Mode); |
5afe5d2d HK |
4089 | end Analyze_Subprogram_Contract; |
4090 | ||
996ae0b0 RK |
4091 | ------------------------------------ |
4092 | -- Analyze_Subprogram_Declaration -- | |
4093 | ------------------------------------ | |
4094 | ||
4095 | procedure Analyze_Subprogram_Declaration (N : Node_Id) is | |
579847c2 | 4096 | Scop : constant Entity_Id := Current_Scope; |
5d5832bc | 4097 | Designator : Entity_Id; |
579847c2 | 4098 | |
4d8f3296 ES |
4099 | Is_Completion : Boolean; |
4100 | -- Indicates whether a null procedure declaration is a completion | |
996ae0b0 RK |
4101 | |
4102 | begin | |
8636f52f HK |
4103 | -- The subprogram declaration may be subject to pragma Ghost with policy |
4104 | -- Ignore. Set the mode now to ensure that any nodes generated during | |
4105 | -- analysis and expansion are properly flagged as ignored Ghost. | |
4106 | ||
4107 | Set_Ghost_Mode (N); | |
4108 | ||
2ba431e5 | 4109 | -- Null procedures are not allowed in SPARK |
daec8eeb | 4110 | |
fe5d3068 | 4111 | if Nkind (Specification (N)) = N_Procedure_Specification |
daec8eeb YM |
4112 | and then Null_Present (Specification (N)) |
4113 | then | |
ce5ba43a | 4114 | Check_SPARK_05_Restriction ("null procedure is not allowed", N); |
718deaf1 | 4115 | |
73cc8f62 RD |
4116 | -- Null procedures are allowed in protected types, following the |
4117 | -- recent AI12-0147. | |
b741083a ES |
4118 | |
4119 | if Is_Protected_Type (Current_Scope) | |
4120 | and then Ada_Version < Ada_2012 | |
4121 | then | |
4d8f3296 ES |
4122 | Error_Msg_N ("protected operation cannot be a null procedure", N); |
4123 | end if; | |
718deaf1 | 4124 | |
4d8f3296 | 4125 | Analyze_Null_Procedure (N, Is_Completion); |
718deaf1 | 4126 | |
4d8f3296 | 4127 | if Is_Completion then |
718deaf1 | 4128 | |
73cc8f62 | 4129 | -- The null procedure acts as a body, nothing further is needed |
5d5832bc | 4130 | |
4d8f3296 | 4131 | return; |
5d5832bc AC |
4132 | end if; |
4133 | end if; | |
4134 | ||
beacce02 | 4135 | Designator := Analyze_Subprogram_Specification (Specification (N)); |
31af8899 AC |
4136 | |
4137 | -- A reference may already have been generated for the unit name, in | |
4138 | -- which case the following call is redundant. However it is needed for | |
4139 | -- declarations that are the rewriting of an expression function. | |
4140 | ||
5d5832bc AC |
4141 | Generate_Definition (Designator); |
4142 | ||
f90d14ac AC |
4143 | -- Set SPARK mode from current context (may be overwritten later with |
4144 | -- explicit pragma). | |
4a854847 | 4145 | |
f90d14ac | 4146 | Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma); |
8636f52f | 4147 | Set_SPARK_Pragma_Inherited (Designator); |
579847c2 | 4148 | |
8636f52f | 4149 | -- A subprogram declared within a Ghost region is automatically Ghost |
c5cec2fe AC |
4150 | -- (SPARK RM 6.9(2)). |
4151 | ||
8636f52f | 4152 | if Comes_From_Source (Designator) and then Ghost_Mode > None then |
c5cec2fe AC |
4153 | Set_Is_Ghost_Entity (Designator); |
4154 | end if; | |
4155 | ||
b1b543d2 BD |
4156 | if Debug_Flag_C then |
4157 | Write_Str ("==> subprogram spec "); | |
4158 | Write_Name (Chars (Designator)); | |
4159 | Write_Str (" from "); | |
4160 | Write_Location (Sloc (N)); | |
4161 | Write_Eol; | |
4162 | Indent; | |
4163 | end if; | |
4164 | ||
996ae0b0 | 4165 | Validate_RCI_Subprogram_Declaration (N); |
996ae0b0 RK |
4166 | New_Overloaded_Entity (Designator); |
4167 | Check_Delayed_Subprogram (Designator); | |
fbf5a39b | 4168 | |
cf3e6845 AC |
4169 | -- If the type of the first formal of the current subprogram is a non- |
4170 | -- generic tagged private type, mark the subprogram as being a private | |
4171 | -- primitive. Ditto if this is a function with controlling result, and | |
4172 | -- the return type is currently private. In both cases, the type of the | |
4173 | -- controlling argument or result must be in the current scope for the | |
4174 | -- operation to be primitive. | |
6ca063eb AC |
4175 | |
4176 | if Has_Controlling_Result (Designator) | |
4177 | and then Is_Private_Type (Etype (Designator)) | |
b7d5e87b | 4178 | and then Scope (Etype (Designator)) = Current_Scope |
6ca063eb AC |
4179 | and then not Is_Generic_Actual_Type (Etype (Designator)) |
4180 | then | |
4181 | Set_Is_Private_Primitive (Designator); | |
d44202ba | 4182 | |
6ca063eb | 4183 | elsif Present (First_Formal (Designator)) then |
d44202ba HK |
4184 | declare |
4185 | Formal_Typ : constant Entity_Id := | |
4186 | Etype (First_Formal (Designator)); | |
4187 | begin | |
4188 | Set_Is_Private_Primitive (Designator, | |
4189 | Is_Tagged_Type (Formal_Typ) | |
b7d5e87b | 4190 | and then Scope (Formal_Typ) = Current_Scope |
d44202ba HK |
4191 | and then Is_Private_Type (Formal_Typ) |
4192 | and then not Is_Generic_Actual_Type (Formal_Typ)); | |
4193 | end; | |
4194 | end if; | |
4195 | ||
ec4867fa ES |
4196 | -- Ada 2005 (AI-251): Abstract interface primitives must be abstract |
4197 | -- or null. | |
4198 | ||
0791fbe9 | 4199 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
4200 | and then Comes_From_Source (N) |
4201 | and then Is_Dispatching_Operation (Designator) | |
4202 | then | |
4203 | declare | |
4204 | E : Entity_Id; | |
4205 | Etyp : Entity_Id; | |
4206 | ||
4207 | begin | |
4208 | if Has_Controlling_Result (Designator) then | |
4209 | Etyp := Etype (Designator); | |
4210 | ||
4211 | else | |
4212 | E := First_Entity (Designator); | |
4213 | while Present (E) | |
4214 | and then Is_Formal (E) | |
4215 | and then not Is_Controlling_Formal (E) | |
4216 | loop | |
4217 | Next_Entity (E); | |
4218 | end loop; | |
4219 | ||
4220 | Etyp := Etype (E); | |
4221 | end if; | |
4222 | ||
4223 | if Is_Access_Type (Etyp) then | |
4224 | Etyp := Directly_Designated_Type (Etyp); | |
4225 | end if; | |
4226 | ||
4227 | if Is_Interface (Etyp) | |
f937473f | 4228 | and then not Is_Abstract_Subprogram (Designator) |
ec4867fa | 4229 | and then not (Ekind (Designator) = E_Procedure |
8fde064e | 4230 | and then Null_Present (Specification (N))) |
ec4867fa ES |
4231 | then |
4232 | Error_Msg_Name_1 := Chars (Defining_Entity (N)); | |
033eaf85 AC |
4233 | |
4234 | -- Specialize error message based on procedures vs. functions, | |
4235 | -- since functions can't be null subprograms. | |
4236 | ||
4237 | if Ekind (Designator) = E_Procedure then | |
4238 | Error_Msg_N | |
4239 | ("interface procedure % must be abstract or null", N); | |
4240 | else | |
3f80a182 AC |
4241 | Error_Msg_N |
4242 | ("interface function % must be abstract", N); | |
033eaf85 | 4243 | end if; |
ec4867fa ES |
4244 | end if; |
4245 | end; | |
4246 | end if; | |
4247 | ||
fbf5a39b AC |
4248 | -- What is the following code for, it used to be |
4249 | ||
4250 | -- ??? Set_Suppress_Elaboration_Checks | |
4251 | -- ??? (Designator, Elaboration_Checks_Suppressed (Designator)); | |
4252 | ||
4253 | -- The following seems equivalent, but a bit dubious | |
4254 | ||
4255 | if Elaboration_Checks_Suppressed (Designator) then | |
4256 | Set_Kill_Elaboration_Checks (Designator); | |
4257 | end if; | |
996ae0b0 | 4258 | |
8fde064e | 4259 | if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then |
fbf5a39b | 4260 | Set_Categorization_From_Scope (Designator, Scop); |
8fde064e | 4261 | |
996ae0b0 | 4262 | else |
e895b435 | 4263 | -- For a compilation unit, check for library-unit pragmas |
996ae0b0 | 4264 | |
0a36105d | 4265 | Push_Scope (Designator); |
996ae0b0 RK |
4266 | Set_Categorization_From_Pragmas (N); |
4267 | Validate_Categorization_Dependency (N, Designator); | |
4268 | Pop_Scope; | |
4269 | end if; | |
4270 | ||
4271 | -- For a compilation unit, set body required. This flag will only be | |
4272 | -- reset if a valid Import or Interface pragma is processed later on. | |
4273 | ||
4274 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
4275 | Set_Body_Required (Parent (N), True); | |
758c442c | 4276 | |
0791fbe9 | 4277 | if Ada_Version >= Ada_2005 |
758c442c GD |
4278 | and then Nkind (Specification (N)) = N_Procedure_Specification |
4279 | and then Null_Present (Specification (N)) | |
4280 | then | |
4281 | Error_Msg_N | |
4282 | ("null procedure cannot be declared at library level", N); | |
4283 | end if; | |
996ae0b0 RK |
4284 | end if; |
4285 | ||
fbf5a39b | 4286 | Generate_Reference_To_Formals (Designator); |
996ae0b0 | 4287 | Check_Eliminated (Designator); |
fbf5a39b | 4288 | |
b1b543d2 BD |
4289 | if Debug_Flag_C then |
4290 | Outdent; | |
4291 | Write_Str ("<== subprogram spec "); | |
4292 | Write_Name (Chars (Designator)); | |
4293 | Write_Str (" from "); | |
4294 | Write_Location (Sloc (N)); | |
4295 | Write_Eol; | |
4296 | end if; | |
0f1a6a0b | 4297 | |
1a265e78 AC |
4298 | if Is_Protected_Type (Current_Scope) then |
4299 | ||
4300 | -- Indicate that this is a protected operation, because it may be | |
4301 | -- used in subsequent declarations within the protected type. | |
4302 | ||
4303 | Set_Convention (Designator, Convention_Protected); | |
4304 | end if; | |
4305 | ||
beacce02 | 4306 | List_Inherited_Pre_Post_Aspects (Designator); |
eaba57fb RD |
4307 | |
4308 | if Has_Aspects (N) then | |
4309 | Analyze_Aspect_Specifications (N, Designator); | |
4310 | end if; | |
996ae0b0 RK |
4311 | end Analyze_Subprogram_Declaration; |
4312 | ||
fbf5a39b AC |
4313 | -------------------------------------- |
4314 | -- Analyze_Subprogram_Specification -- | |
4315 | -------------------------------------- | |
4316 | ||
4317 | -- Reminder: N here really is a subprogram specification (not a subprogram | |
4318 | -- declaration). This procedure is called to analyze the specification in | |
4319 | -- both subprogram bodies and subprogram declarations (specs). | |
4320 | ||
4321 | function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is | |
4322 | Designator : constant Entity_Id := Defining_Entity (N); | |
21d27997 | 4323 | Formals : constant List_Id := Parameter_Specifications (N); |
fbf5a39b | 4324 | |
758c442c GD |
4325 | -- Start of processing for Analyze_Subprogram_Specification |
4326 | ||
fbf5a39b | 4327 | begin |
2ba431e5 | 4328 | -- User-defined operator is not allowed in SPARK, except as a renaming |
38171f43 | 4329 | |
db72f10a AC |
4330 | if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol |
4331 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
4332 | then | |
ce5ba43a AC |
4333 | Check_SPARK_05_Restriction |
4334 | ("user-defined operator is not allowed", N); | |
38171f43 AC |
4335 | end if; |
4336 | ||
31af8899 AC |
4337 | -- Proceed with analysis. Do not emit a cross-reference entry if the |
4338 | -- specification comes from an expression function, because it may be | |
4339 | -- the completion of a previous declaration. It is is not, the cross- | |
4340 | -- reference entry will be emitted for the new subprogram declaration. | |
4341 | ||
4342 | if Nkind (Parent (N)) /= N_Expression_Function then | |
4343 | Generate_Definition (Designator); | |
4344 | end if; | |
38171f43 | 4345 | |
fbf5a39b AC |
4346 | if Nkind (N) = N_Function_Specification then |
4347 | Set_Ekind (Designator, E_Function); | |
4348 | Set_Mechanism (Designator, Default_Mechanism); | |
fbf5a39b AC |
4349 | else |
4350 | Set_Ekind (Designator, E_Procedure); | |
4351 | Set_Etype (Designator, Standard_Void_Type); | |
4352 | end if; | |
4353 | ||
4bd4bb7f AC |
4354 | -- Flag Is_Inlined_Always is True by default, and reversed to False for |
4355 | -- those subprograms which could be inlined in GNATprove mode (because | |
4356 | -- Body_To_Inline is non-Empty) but cannot be inlined. | |
4357 | ||
4358 | if GNATprove_Mode then | |
4359 | Set_Is_Inlined_Always (Designator); | |
4360 | end if; | |
4361 | ||
800621e0 | 4362 | -- Introduce new scope for analysis of the formals and the return type |
82c80734 RD |
4363 | |
4364 | Set_Scope (Designator, Current_Scope); | |
4365 | ||
fbf5a39b | 4366 | if Present (Formals) then |
0a36105d | 4367 | Push_Scope (Designator); |
fbf5a39b | 4368 | Process_Formals (Formals, N); |
758c442c | 4369 | |
0929eaeb AC |
4370 | -- Check dimensions in N for formals with default expression |
4371 | ||
4372 | Analyze_Dimension_Formals (N, Formals); | |
4373 | ||
a38ff9b1 ES |
4374 | -- Ada 2005 (AI-345): If this is an overriding operation of an |
4375 | -- inherited interface operation, and the controlling type is | |
4376 | -- a synchronized type, replace the type with its corresponding | |
4377 | -- record, to match the proper signature of an overriding operation. | |
69cb258c AC |
4378 | -- Same processing for an access parameter whose designated type is |
4379 | -- derived from a synchronized interface. | |
758c442c | 4380 | |
0791fbe9 | 4381 | if Ada_Version >= Ada_2005 then |
d44202ba HK |
4382 | declare |
4383 | Formal : Entity_Id; | |
4384 | Formal_Typ : Entity_Id; | |
4385 | Rec_Typ : Entity_Id; | |
69cb258c | 4386 | Desig_Typ : Entity_Id; |
0a36105d | 4387 | |
d44202ba HK |
4388 | begin |
4389 | Formal := First_Formal (Designator); | |
4390 | while Present (Formal) loop | |
4391 | Formal_Typ := Etype (Formal); | |
0a36105d | 4392 | |
d44202ba HK |
4393 | if Is_Concurrent_Type (Formal_Typ) |
4394 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
4395 | then | |
4396 | Rec_Typ := Corresponding_Record_Type (Formal_Typ); | |
4397 | ||
4398 | if Present (Interfaces (Rec_Typ)) then | |
4399 | Set_Etype (Formal, Rec_Typ); | |
4400 | end if; | |
69cb258c AC |
4401 | |
4402 | elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then | |
4403 | Desig_Typ := Designated_Type (Formal_Typ); | |
4404 | ||
4405 | if Is_Concurrent_Type (Desig_Typ) | |
4406 | and then Present (Corresponding_Record_Type (Desig_Typ)) | |
4407 | then | |
4408 | Rec_Typ := Corresponding_Record_Type (Desig_Typ); | |
4409 | ||
4410 | if Present (Interfaces (Rec_Typ)) then | |
4411 | Set_Directly_Designated_Type (Formal_Typ, Rec_Typ); | |
4412 | end if; | |
4413 | end if; | |
d44202ba HK |
4414 | end if; |
4415 | ||
4416 | Next_Formal (Formal); | |
4417 | end loop; | |
4418 | end; | |
758c442c GD |
4419 | end if; |
4420 | ||
fbf5a39b | 4421 | End_Scope; |
82c80734 | 4422 | |
b66c3ff4 AC |
4423 | -- The subprogram scope is pushed and popped around the processing of |
4424 | -- the return type for consistency with call above to Process_Formals | |
4425 | -- (which itself can call Analyze_Return_Type), and to ensure that any | |
4426 | -- itype created for the return type will be associated with the proper | |
4427 | -- scope. | |
4428 | ||
82c80734 | 4429 | elsif Nkind (N) = N_Function_Specification then |
b66c3ff4 | 4430 | Push_Scope (Designator); |
82c80734 | 4431 | Analyze_Return_Type (N); |
b66c3ff4 | 4432 | End_Scope; |
fbf5a39b AC |
4433 | end if; |
4434 | ||
e606088a AC |
4435 | -- Function case |
4436 | ||
fbf5a39b | 4437 | if Nkind (N) = N_Function_Specification then |
e606088a AC |
4438 | |
4439 | -- Deal with operator symbol case | |
4440 | ||
fbf5a39b AC |
4441 | if Nkind (Designator) = N_Defining_Operator_Symbol then |
4442 | Valid_Operator_Definition (Designator); | |
4443 | end if; | |
4444 | ||
4445 | May_Need_Actuals (Designator); | |
4446 | ||
fe63b1b1 ES |
4447 | -- Ada 2005 (AI-251): If the return type is abstract, verify that |
4448 | -- the subprogram is abstract also. This does not apply to renaming | |
1adaea16 AC |
4449 | -- declarations, where abstractness is inherited, and to subprogram |
4450 | -- bodies generated for stream operations, which become renamings as | |
4451 | -- bodies. | |
2bfb1b72 | 4452 | |
fe63b1b1 ES |
4453 | -- In case of primitives associated with abstract interface types |
4454 | -- the check is applied later (see Analyze_Subprogram_Declaration). | |
ec4867fa | 4455 | |
1adaea16 | 4456 | if not Nkind_In (Original_Node (Parent (N)), |
3f80a182 AC |
4457 | N_Subprogram_Renaming_Declaration, |
4458 | N_Abstract_Subprogram_Declaration, | |
4459 | N_Formal_Abstract_Subprogram_Declaration) | |
fbf5a39b | 4460 | then |
2e79de51 AC |
4461 | if Is_Abstract_Type (Etype (Designator)) |
4462 | and then not Is_Interface (Etype (Designator)) | |
4463 | then | |
4464 | Error_Msg_N | |
4465 | ("function that returns abstract type must be abstract", N); | |
4466 | ||
e606088a | 4467 | -- Ada 2012 (AI-0073): Extend this test to subprograms with an |
2e79de51 AC |
4468 | -- access result whose designated type is abstract. |
4469 | ||
4470 | elsif Nkind (Result_Definition (N)) = N_Access_Definition | |
4471 | and then | |
4472 | not Is_Class_Wide_Type (Designated_Type (Etype (Designator))) | |
4473 | and then Is_Abstract_Type (Designated_Type (Etype (Designator))) | |
dbe945f1 | 4474 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
4475 | then |
4476 | Error_Msg_N ("function whose access result designates " | |
3f80a182 | 4477 | & "abstract type must be abstract", N); |
2e79de51 | 4478 | end if; |
fbf5a39b AC |
4479 | end if; |
4480 | end if; | |
4481 | ||
4482 | return Designator; | |
4483 | end Analyze_Subprogram_Specification; | |
4484 | ||
996ae0b0 RK |
4485 | ----------------------- |
4486 | -- Check_Conformance -- | |
4487 | ----------------------- | |
4488 | ||
4489 | procedure Check_Conformance | |
41251c60 JM |
4490 | (New_Id : Entity_Id; |
4491 | Old_Id : Entity_Id; | |
4492 | Ctype : Conformance_Type; | |
4493 | Errmsg : Boolean; | |
4494 | Conforms : out Boolean; | |
4495 | Err_Loc : Node_Id := Empty; | |
4496 | Get_Inst : Boolean := False; | |
4497 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 | 4498 | is |
996ae0b0 | 4499 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id); |
c27f2f15 RD |
4500 | -- Sets Conforms to False. If Errmsg is False, then that's all it does. |
4501 | -- If Errmsg is True, then processing continues to post an error message | |
4502 | -- for conformance error on given node. Two messages are output. The | |
4503 | -- first message points to the previous declaration with a general "no | |
4504 | -- conformance" message. The second is the detailed reason, supplied as | |
4505 | -- Msg. The parameter N provide information for a possible & insertion | |
4506 | -- in the message, and also provides the location for posting the | |
4507 | -- message in the absence of a specified Err_Loc location. | |
996ae0b0 RK |
4508 | |
4509 | ----------------------- | |
4510 | -- Conformance_Error -- | |
4511 | ----------------------- | |
4512 | ||
4513 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is | |
4514 | Enode : Node_Id; | |
4515 | ||
4516 | begin | |
4517 | Conforms := False; | |
4518 | ||
4519 | if Errmsg then | |
4520 | if No (Err_Loc) then | |
4521 | Enode := N; | |
4522 | else | |
4523 | Enode := Err_Loc; | |
4524 | end if; | |
4525 | ||
4526 | Error_Msg_Sloc := Sloc (Old_Id); | |
4527 | ||
4528 | case Ctype is | |
4529 | when Type_Conformant => | |
483c78cb | 4530 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
4531 | ("not type conformant with declaration#!", Enode); |
4532 | ||
4533 | when Mode_Conformant => | |
19590d70 | 4534 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4535 | Error_Msg_N |
19590d70 GD |
4536 | ("not mode conformant with operation inherited#!", |
4537 | Enode); | |
4538 | else | |
ed2233dc | 4539 | Error_Msg_N |
19590d70 GD |
4540 | ("not mode conformant with declaration#!", Enode); |
4541 | end if; | |
996ae0b0 RK |
4542 | |
4543 | when Subtype_Conformant => | |
19590d70 | 4544 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4545 | Error_Msg_N |
19590d70 GD |
4546 | ("not subtype conformant with operation inherited#!", |
4547 | Enode); | |
4548 | else | |
ed2233dc | 4549 | Error_Msg_N |
19590d70 GD |
4550 | ("not subtype conformant with declaration#!", Enode); |
4551 | end if; | |
996ae0b0 RK |
4552 | |
4553 | when Fully_Conformant => | |
19590d70 | 4554 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
483c78cb | 4555 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4556 | ("not fully conformant with operation inherited#!", |
4557 | Enode); | |
4558 | else | |
483c78cb | 4559 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4560 | ("not fully conformant with declaration#!", Enode); |
4561 | end if; | |
996ae0b0 RK |
4562 | end case; |
4563 | ||
4564 | Error_Msg_NE (Msg, Enode, N); | |
4565 | end if; | |
4566 | end Conformance_Error; | |
4567 | ||
ec4867fa ES |
4568 | -- Local Variables |
4569 | ||
4570 | Old_Type : constant Entity_Id := Etype (Old_Id); | |
4571 | New_Type : constant Entity_Id := Etype (New_Id); | |
4572 | Old_Formal : Entity_Id; | |
4573 | New_Formal : Entity_Id; | |
4574 | Access_Types_Match : Boolean; | |
4575 | Old_Formal_Base : Entity_Id; | |
4576 | New_Formal_Base : Entity_Id; | |
4577 | ||
996ae0b0 RK |
4578 | -- Start of processing for Check_Conformance |
4579 | ||
4580 | begin | |
4581 | Conforms := True; | |
4582 | ||
82c80734 RD |
4583 | -- We need a special case for operators, since they don't appear |
4584 | -- explicitly. | |
996ae0b0 RK |
4585 | |
4586 | if Ctype = Type_Conformant then | |
4587 | if Ekind (New_Id) = E_Operator | |
4588 | and then Operator_Matches_Spec (New_Id, Old_Id) | |
4589 | then | |
4590 | return; | |
4591 | end if; | |
4592 | end if; | |
4593 | ||
4594 | -- If both are functions/operators, check return types conform | |
4595 | ||
4596 | if Old_Type /= Standard_Void_Type | |
3cb9a885 AC |
4597 | and then |
4598 | New_Type /= Standard_Void_Type | |
996ae0b0 | 4599 | then |
fceeaab6 ES |
4600 | -- If we are checking interface conformance we omit controlling |
4601 | -- arguments and result, because we are only checking the conformance | |
4602 | -- of the remaining parameters. | |
4603 | ||
4604 | if Has_Controlling_Result (Old_Id) | |
4605 | and then Has_Controlling_Result (New_Id) | |
4606 | and then Skip_Controlling_Formals | |
4607 | then | |
4608 | null; | |
4609 | ||
4610 | elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then | |
7f568bfa AC |
4611 | if Ctype >= Subtype_Conformant |
4612 | and then not Predicates_Match (Old_Type, New_Type) | |
4613 | then | |
4614 | Conformance_Error | |
4615 | ("\predicate of return type does not match!", New_Id); | |
4616 | else | |
4617 | Conformance_Error | |
4618 | ("\return type does not match!", New_Id); | |
4619 | end if; | |
4620 | ||
996ae0b0 RK |
4621 | return; |
4622 | end if; | |
4623 | ||
41251c60 | 4624 | -- Ada 2005 (AI-231): In case of anonymous access types check the |
0a36105d | 4625 | -- null-exclusion and access-to-constant attributes match. |
41251c60 | 4626 | |
0791fbe9 | 4627 | if Ada_Version >= Ada_2005 |
41251c60 JM |
4628 | and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type |
4629 | and then | |
8fde064e AC |
4630 | (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type) |
4631 | or else Is_Access_Constant (Etype (Old_Type)) /= | |
4632 | Is_Access_Constant (Etype (New_Type))) | |
41251c60 | 4633 | then |
5d37ba92 | 4634 | Conformance_Error ("\return type does not match!", New_Id); |
41251c60 JM |
4635 | return; |
4636 | end if; | |
4637 | ||
996ae0b0 RK |
4638 | -- If either is a function/operator and the other isn't, error |
4639 | ||
4640 | elsif Old_Type /= Standard_Void_Type | |
4641 | or else New_Type /= Standard_Void_Type | |
4642 | then | |
5d37ba92 | 4643 | Conformance_Error ("\functions can only match functions!", New_Id); |
996ae0b0 RK |
4644 | return; |
4645 | end if; | |
4646 | ||
0a36105d | 4647 | -- In subtype conformant case, conventions must match (RM 6.3.1(16)). |
996ae0b0 RK |
4648 | -- If this is a renaming as body, refine error message to indicate that |
4649 | -- the conflict is with the original declaration. If the entity is not | |
4650 | -- frozen, the conventions don't have to match, the one of the renamed | |
4651 | -- entity is inherited. | |
4652 | ||
4653 | if Ctype >= Subtype_Conformant then | |
996ae0b0 | 4654 | if Convention (Old_Id) /= Convention (New_Id) then |
996ae0b0 RK |
4655 | if not Is_Frozen (New_Id) then |
4656 | null; | |
4657 | ||
4658 | elsif Present (Err_Loc) | |
4659 | and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration | |
4660 | and then Present (Corresponding_Spec (Err_Loc)) | |
4661 | then | |
4662 | Error_Msg_Name_1 := Chars (New_Id); | |
4663 | Error_Msg_Name_2 := | |
4664 | Name_Ada + Convention_Id'Pos (Convention (New_Id)); | |
5d37ba92 | 4665 | Conformance_Error ("\prior declaration for% has convention %!"); |
996ae0b0 RK |
4666 | |
4667 | else | |
5d37ba92 | 4668 | Conformance_Error ("\calling conventions do not match!"); |
996ae0b0 RK |
4669 | end if; |
4670 | ||
4671 | return; | |
4672 | ||
4673 | elsif Is_Formal_Subprogram (Old_Id) | |
4674 | or else Is_Formal_Subprogram (New_Id) | |
4675 | then | |
5d37ba92 | 4676 | Conformance_Error ("\formal subprograms not allowed!"); |
996ae0b0 | 4677 | return; |
c5cec2fe AC |
4678 | |
4679 | -- Pragma Ghost behaves as a convention in the context of subtype | |
10274386 AC |
4680 | -- conformance (SPARK RM 6.9(5)). Do not check internally generated |
4681 | -- subprograms as their spec may reside in a Ghost region and their | |
4682 | -- body not, or vice versa. | |
c5cec2fe | 4683 | |
10274386 AC |
4684 | elsif Comes_From_Source (Old_Id) |
4685 | and then Comes_From_Source (New_Id) | |
4686 | and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id) | |
4687 | then | |
c5cec2fe AC |
4688 | Conformance_Error ("\ghost modes do not match!"); |
4689 | return; | |
996ae0b0 RK |
4690 | end if; |
4691 | end if; | |
4692 | ||
4693 | -- Deal with parameters | |
4694 | ||
4695 | -- Note: we use the entity information, rather than going directly | |
4696 | -- to the specification in the tree. This is not only simpler, but | |
4697 | -- absolutely necessary for some cases of conformance tests between | |
a90bd866 | 4698 | -- operators, where the declaration tree simply does not exist. |
996ae0b0 RK |
4699 | |
4700 | Old_Formal := First_Formal (Old_Id); | |
4701 | New_Formal := First_Formal (New_Id); | |
996ae0b0 | 4702 | while Present (Old_Formal) and then Present (New_Formal) loop |
41251c60 JM |
4703 | if Is_Controlling_Formal (Old_Formal) |
4704 | and then Is_Controlling_Formal (New_Formal) | |
4705 | and then Skip_Controlling_Formals | |
4706 | then | |
a2dc5812 AC |
4707 | -- The controlling formals will have different types when |
4708 | -- comparing an interface operation with its match, but both | |
4709 | -- or neither must be access parameters. | |
4710 | ||
4711 | if Is_Access_Type (Etype (Old_Formal)) | |
4712 | = | |
4713 | Is_Access_Type (Etype (New_Formal)) | |
4714 | then | |
4715 | goto Skip_Controlling_Formal; | |
4716 | else | |
4717 | Conformance_Error | |
4718 | ("\access parameter does not match!", New_Formal); | |
4719 | end if; | |
41251c60 JM |
4720 | end if; |
4721 | ||
21791d97 | 4722 | -- Ada 2012: Mode conformance also requires that formal parameters |
2a290fec AC |
4723 | -- be both aliased, or neither. |
4724 | ||
21791d97 | 4725 | if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then |
2a290fec AC |
4726 | if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then |
4727 | Conformance_Error | |
4728 | ("\aliased parameter mismatch!", New_Formal); | |
4729 | end if; | |
4730 | end if; | |
4731 | ||
fbf5a39b AC |
4732 | if Ctype = Fully_Conformant then |
4733 | ||
4734 | -- Names must match. Error message is more accurate if we do | |
4735 | -- this before checking that the types of the formals match. | |
4736 | ||
4737 | if Chars (Old_Formal) /= Chars (New_Formal) then | |
3ccedacc | 4738 | Conformance_Error ("\name& does not match!", New_Formal); |
fbf5a39b AC |
4739 | |
4740 | -- Set error posted flag on new formal as well to stop | |
4741 | -- junk cascaded messages in some cases. | |
4742 | ||
4743 | Set_Error_Posted (New_Formal); | |
4744 | return; | |
4745 | end if; | |
40b93859 RD |
4746 | |
4747 | -- Null exclusion must match | |
4748 | ||
4749 | if Null_Exclusion_Present (Parent (Old_Formal)) | |
4750 | /= | |
4751 | Null_Exclusion_Present (Parent (New_Formal)) | |
4752 | then | |
4753 | -- Only give error if both come from source. This should be | |
4754 | -- investigated some time, since it should not be needed ??? | |
4755 | ||
4756 | if Comes_From_Source (Old_Formal) | |
4757 | and then | |
4758 | Comes_From_Source (New_Formal) | |
4759 | then | |
4760 | Conformance_Error | |
3ccedacc | 4761 | ("\null exclusion for& does not match", New_Formal); |
40b93859 RD |
4762 | |
4763 | -- Mark error posted on the new formal to avoid duplicated | |
4764 | -- complaint about types not matching. | |
4765 | ||
4766 | Set_Error_Posted (New_Formal); | |
4767 | end if; | |
4768 | end if; | |
fbf5a39b | 4769 | end if; |
996ae0b0 | 4770 | |
ec4867fa ES |
4771 | -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This |
4772 | -- case occurs whenever a subprogram is being renamed and one of its | |
4773 | -- parameters imposes a null exclusion. For example: | |
4774 | ||
4775 | -- type T is null record; | |
4776 | -- type Acc_T is access T; | |
4777 | -- subtype Acc_T_Sub is Acc_T; | |
4778 | ||
4779 | -- procedure P (Obj : not null Acc_T_Sub); -- itype | |
4780 | -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype | |
4781 | -- renames P; | |
4782 | ||
4783 | Old_Formal_Base := Etype (Old_Formal); | |
4784 | New_Formal_Base := Etype (New_Formal); | |
4785 | ||
4786 | if Get_Inst then | |
4787 | Old_Formal_Base := Get_Instance_Of (Old_Formal_Base); | |
4788 | New_Formal_Base := Get_Instance_Of (New_Formal_Base); | |
4789 | end if; | |
4790 | ||
0791fbe9 | 4791 | Access_Types_Match := Ada_Version >= Ada_2005 |
ec4867fa | 4792 | |
8fde064e AC |
4793 | -- Ensure that this rule is only applied when New_Id is a |
4794 | -- renaming of Old_Id. | |
ec4867fa | 4795 | |
5d37ba92 ES |
4796 | and then Nkind (Parent (Parent (New_Id))) = |
4797 | N_Subprogram_Renaming_Declaration | |
ec4867fa ES |
4798 | and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity |
4799 | and then Present (Entity (Name (Parent (Parent (New_Id))))) | |
4800 | and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id | |
4801 | ||
8fde064e | 4802 | -- Now handle the allowed access-type case |
ec4867fa ES |
4803 | |
4804 | and then Is_Access_Type (Old_Formal_Base) | |
4805 | and then Is_Access_Type (New_Formal_Base) | |
5d37ba92 | 4806 | |
8fde064e AC |
4807 | -- The type kinds must match. The only exception occurs with |
4808 | -- multiple generics of the form: | |
5d37ba92 | 4809 | |
8fde064e AC |
4810 | -- generic generic |
4811 | -- type F is private; type A is private; | |
4812 | -- type F_Ptr is access F; type A_Ptr is access A; | |
4813 | -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr); | |
4814 | -- package F_Pack is ... package A_Pack is | |
4815 | -- package F_Inst is | |
4816 | -- new F_Pack (A, A_Ptr, A_P); | |
5d37ba92 | 4817 | |
8fde064e AC |
4818 | -- When checking for conformance between the parameters of A_P |
4819 | -- and F_P, the type kinds of F_Ptr and A_Ptr will not match | |
4820 | -- because the compiler has transformed A_Ptr into a subtype of | |
4821 | -- F_Ptr. We catch this case in the code below. | |
5d37ba92 ES |
4822 | |
4823 | and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base) | |
4b6f99f5 RD |
4824 | or else |
4825 | (Is_Generic_Type (Old_Formal_Base) | |
4826 | and then Is_Generic_Type (New_Formal_Base) | |
4827 | and then Is_Internal (New_Formal_Base) | |
4828 | and then Etype (Etype (New_Formal_Base)) = | |
4829 | Old_Formal_Base)) | |
4830 | and then Directly_Designated_Type (Old_Formal_Base) = | |
4831 | Directly_Designated_Type (New_Formal_Base) | |
ec4867fa ES |
4832 | and then ((Is_Itype (Old_Formal_Base) |
4833 | and then Can_Never_Be_Null (Old_Formal_Base)) | |
4b6f99f5 RD |
4834 | or else |
4835 | (Is_Itype (New_Formal_Base) | |
4836 | and then Can_Never_Be_Null (New_Formal_Base))); | |
ec4867fa | 4837 | |
996ae0b0 RK |
4838 | -- Types must always match. In the visible part of an instance, |
4839 | -- usual overloading rules for dispatching operations apply, and | |
4840 | -- we check base types (not the actual subtypes). | |
4841 | ||
4842 | if In_Instance_Visible_Part | |
4843 | and then Is_Dispatching_Operation (New_Id) | |
4844 | then | |
4845 | if not Conforming_Types | |
ec4867fa ES |
4846 | (T1 => Base_Type (Etype (Old_Formal)), |
4847 | T2 => Base_Type (Etype (New_Formal)), | |
4848 | Ctype => Ctype, | |
4849 | Get_Inst => Get_Inst) | |
4850 | and then not Access_Types_Match | |
996ae0b0 | 4851 | then |
5d37ba92 | 4852 | Conformance_Error ("\type of & does not match!", New_Formal); |
996ae0b0 RK |
4853 | return; |
4854 | end if; | |
4855 | ||
4856 | elsif not Conforming_Types | |
5d37ba92 ES |
4857 | (T1 => Old_Formal_Base, |
4858 | T2 => New_Formal_Base, | |
ec4867fa ES |
4859 | Ctype => Ctype, |
4860 | Get_Inst => Get_Inst) | |
4861 | and then not Access_Types_Match | |
996ae0b0 | 4862 | then |
c27f2f15 RD |
4863 | -- Don't give error message if old type is Any_Type. This test |
4864 | -- avoids some cascaded errors, e.g. in case of a bad spec. | |
4865 | ||
4866 | if Errmsg and then Old_Formal_Base = Any_Type then | |
4867 | Conforms := False; | |
4868 | else | |
7f568bfa AC |
4869 | if Ctype >= Subtype_Conformant |
4870 | and then | |
4871 | not Predicates_Match (Old_Formal_Base, New_Formal_Base) | |
4872 | then | |
4873 | Conformance_Error | |
4874 | ("\predicate of & does not match!", New_Formal); | |
4875 | else | |
4876 | Conformance_Error | |
4877 | ("\type of & does not match!", New_Formal); | |
4878 | end if; | |
c27f2f15 RD |
4879 | end if; |
4880 | ||
996ae0b0 RK |
4881 | return; |
4882 | end if; | |
4883 | ||
4884 | -- For mode conformance, mode must match | |
4885 | ||
5d37ba92 ES |
4886 | if Ctype >= Mode_Conformant then |
4887 | if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then | |
dd54644b JM |
4888 | if not Ekind_In (New_Id, E_Function, E_Procedure) |
4889 | or else not Is_Primitive_Wrapper (New_Id) | |
4890 | then | |
4891 | Conformance_Error ("\mode of & does not match!", New_Formal); | |
c199ccf7 | 4892 | |
dd54644b JM |
4893 | else |
4894 | declare | |
c199ccf7 | 4895 | T : constant Entity_Id := Find_Dispatching_Type (New_Id); |
dd54644b | 4896 | begin |
3ccedacc | 4897 | if Is_Protected_Type (Corresponding_Concurrent_Type (T)) |
dd54644b | 4898 | then |
2c6336be | 4899 | Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id)); |
dd54644b JM |
4900 | else |
4901 | Conformance_Error | |
4902 | ("\mode of & does not match!", New_Formal); | |
4903 | end if; | |
4904 | end; | |
4905 | end if; | |
4906 | ||
5d37ba92 ES |
4907 | return; |
4908 | ||
4909 | -- Part of mode conformance for access types is having the same | |
4910 | -- constant modifier. | |
4911 | ||
4912 | elsif Access_Types_Match | |
4913 | and then Is_Access_Constant (Old_Formal_Base) /= | |
4914 | Is_Access_Constant (New_Formal_Base) | |
4915 | then | |
4916 | Conformance_Error | |
4917 | ("\constant modifier does not match!", New_Formal); | |
4918 | return; | |
4919 | end if; | |
996ae0b0 RK |
4920 | end if; |
4921 | ||
0a36105d | 4922 | if Ctype >= Subtype_Conformant then |
996ae0b0 | 4923 | |
0a36105d JM |
4924 | -- Ada 2005 (AI-231): In case of anonymous access types check |
4925 | -- the null-exclusion and access-to-constant attributes must | |
c7b9d548 AC |
4926 | -- match. For null exclusion, we test the types rather than the |
4927 | -- formals themselves, since the attribute is only set reliably | |
4928 | -- on the formals in the Ada 95 case, and we exclude the case | |
4929 | -- where Old_Formal is marked as controlling, to avoid errors | |
4930 | -- when matching completing bodies with dispatching declarations | |
4931 | -- (access formals in the bodies aren't marked Can_Never_Be_Null). | |
996ae0b0 | 4932 | |
0791fbe9 | 4933 | if Ada_Version >= Ada_2005 |
0a36105d JM |
4934 | and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type |
4935 | and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type | |
4936 | and then | |
c7b9d548 AC |
4937 | ((Can_Never_Be_Null (Etype (Old_Formal)) /= |
4938 | Can_Never_Be_Null (Etype (New_Formal)) | |
4939 | and then | |
4940 | not Is_Controlling_Formal (Old_Formal)) | |
0a36105d JM |
4941 | or else |
4942 | Is_Access_Constant (Etype (Old_Formal)) /= | |
4943 | Is_Access_Constant (Etype (New_Formal))) | |
40b93859 RD |
4944 | |
4945 | -- Do not complain if error already posted on New_Formal. This | |
4946 | -- avoids some redundant error messages. | |
4947 | ||
4948 | and then not Error_Posted (New_Formal) | |
0a36105d JM |
4949 | then |
4950 | -- It is allowed to omit the null-exclusion in case of stream | |
4951 | -- attribute subprograms. We recognize stream subprograms | |
4952 | -- through their TSS-generated suffix. | |
996ae0b0 | 4953 | |
0a36105d JM |
4954 | declare |
4955 | TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id); | |
3ada950b | 4956 | |
0a36105d JM |
4957 | begin |
4958 | if TSS_Name /= TSS_Stream_Read | |
4959 | and then TSS_Name /= TSS_Stream_Write | |
4960 | and then TSS_Name /= TSS_Stream_Input | |
4961 | and then TSS_Name /= TSS_Stream_Output | |
4962 | then | |
3ada950b | 4963 | -- Here we have a definite conformance error. It is worth |
71fb4dc8 | 4964 | -- special casing the error message for the case of a |
3ada950b AC |
4965 | -- controlling formal (which excludes null). |
4966 | ||
4967 | if Is_Controlling_Formal (New_Formal) then | |
4968 | Error_Msg_Node_2 := Scope (New_Formal); | |
4969 | Conformance_Error | |
3ccedacc AC |
4970 | ("\controlling formal & of & excludes null, " |
4971 | & "declaration must exclude null as well", | |
4972 | New_Formal); | |
3ada950b AC |
4973 | |
4974 | -- Normal case (couldn't we give more detail here???) | |
4975 | ||
4976 | else | |
4977 | Conformance_Error | |
4978 | ("\type of & does not match!", New_Formal); | |
4979 | end if; | |
4980 | ||
0a36105d JM |
4981 | return; |
4982 | end if; | |
4983 | end; | |
4984 | end if; | |
4985 | end if; | |
41251c60 | 4986 | |
0a36105d | 4987 | -- Full conformance checks |
41251c60 | 4988 | |
0a36105d | 4989 | if Ctype = Fully_Conformant then |
e660dbf7 | 4990 | |
0a36105d | 4991 | -- We have checked already that names match |
e660dbf7 | 4992 | |
0a36105d | 4993 | if Parameter_Mode (Old_Formal) = E_In_Parameter then |
41251c60 JM |
4994 | |
4995 | -- Check default expressions for in parameters | |
4996 | ||
996ae0b0 RK |
4997 | declare |
4998 | NewD : constant Boolean := | |
4999 | Present (Default_Value (New_Formal)); | |
5000 | OldD : constant Boolean := | |
5001 | Present (Default_Value (Old_Formal)); | |
5002 | begin | |
5003 | if NewD or OldD then | |
5004 | ||
82c80734 RD |
5005 | -- The old default value has been analyzed because the |
5006 | -- current full declaration will have frozen everything | |
0a36105d JM |
5007 | -- before. The new default value has not been analyzed, |
5008 | -- so analyze it now before we check for conformance. | |
996ae0b0 RK |
5009 | |
5010 | if NewD then | |
0a36105d | 5011 | Push_Scope (New_Id); |
21d27997 | 5012 | Preanalyze_Spec_Expression |
fbf5a39b | 5013 | (Default_Value (New_Formal), Etype (New_Formal)); |
996ae0b0 RK |
5014 | End_Scope; |
5015 | end if; | |
5016 | ||
5017 | if not (NewD and OldD) | |
5018 | or else not Fully_Conformant_Expressions | |
5019 | (Default_Value (Old_Formal), | |
5020 | Default_Value (New_Formal)) | |
5021 | then | |
5022 | Conformance_Error | |
5d37ba92 | 5023 | ("\default expression for & does not match!", |
996ae0b0 RK |
5024 | New_Formal); |
5025 | return; | |
5026 | end if; | |
5027 | end if; | |
5028 | end; | |
5029 | end if; | |
5030 | end if; | |
5031 | ||
5032 | -- A couple of special checks for Ada 83 mode. These checks are | |
0a36105d | 5033 | -- skipped if either entity is an operator in package Standard, |
996ae0b0 RK |
5034 | -- or if either old or new instance is not from the source program. |
5035 | ||
0ab80019 | 5036 | if Ada_Version = Ada_83 |
996ae0b0 RK |
5037 | and then Sloc (Old_Id) > Standard_Location |
5038 | and then Sloc (New_Id) > Standard_Location | |
5039 | and then Comes_From_Source (Old_Id) | |
5040 | and then Comes_From_Source (New_Id) | |
5041 | then | |
5042 | declare | |
5043 | Old_Param : constant Node_Id := Declaration_Node (Old_Formal); | |
5044 | New_Param : constant Node_Id := Declaration_Node (New_Formal); | |
5045 | ||
5046 | begin | |
5047 | -- Explicit IN must be present or absent in both cases. This | |
5048 | -- test is required only in the full conformance case. | |
5049 | ||
5050 | if In_Present (Old_Param) /= In_Present (New_Param) | |
5051 | and then Ctype = Fully_Conformant | |
5052 | then | |
5053 | Conformance_Error | |
5d37ba92 | 5054 | ("\(Ada 83) IN must appear in both declarations", |
996ae0b0 RK |
5055 | New_Formal); |
5056 | return; | |
5057 | end if; | |
5058 | ||
5059 | -- Grouping (use of comma in param lists) must be the same | |
5060 | -- This is where we catch a misconformance like: | |
5061 | ||
0a36105d | 5062 | -- A, B : Integer |
996ae0b0 RK |
5063 | -- A : Integer; B : Integer |
5064 | ||
5065 | -- which are represented identically in the tree except | |
5066 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5067 | ||
5068 | if More_Ids (Old_Param) /= More_Ids (New_Param) | |
5069 | or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param) | |
5070 | then | |
5071 | Conformance_Error | |
5d37ba92 | 5072 | ("\grouping of & does not match!", New_Formal); |
996ae0b0 RK |
5073 | return; |
5074 | end if; | |
5075 | end; | |
5076 | end if; | |
5077 | ||
41251c60 JM |
5078 | -- This label is required when skipping controlling formals |
5079 | ||
5080 | <<Skip_Controlling_Formal>> | |
5081 | ||
996ae0b0 RK |
5082 | Next_Formal (Old_Formal); |
5083 | Next_Formal (New_Formal); | |
5084 | end loop; | |
5085 | ||
5086 | if Present (Old_Formal) then | |
5d37ba92 | 5087 | Conformance_Error ("\too few parameters!"); |
996ae0b0 RK |
5088 | return; |
5089 | ||
5090 | elsif Present (New_Formal) then | |
5d37ba92 | 5091 | Conformance_Error ("\too many parameters!", New_Formal); |
996ae0b0 RK |
5092 | return; |
5093 | end if; | |
996ae0b0 RK |
5094 | end Check_Conformance; |
5095 | ||
ec4867fa ES |
5096 | ----------------------- |
5097 | -- Check_Conventions -- | |
5098 | ----------------------- | |
5099 | ||
5100 | procedure Check_Conventions (Typ : Entity_Id) is | |
ce2b6ba5 | 5101 | Ifaces_List : Elist_Id; |
0a36105d | 5102 | |
ce2b6ba5 | 5103 | procedure Check_Convention (Op : Entity_Id); |
0a36105d JM |
5104 | -- Verify that the convention of inherited dispatching operation Op is |
5105 | -- consistent among all subprograms it overrides. In order to minimize | |
5106 | -- the search, Search_From is utilized to designate a specific point in | |
5107 | -- the list rather than iterating over the whole list once more. | |
ec4867fa ES |
5108 | |
5109 | ---------------------- | |
5110 | -- Check_Convention -- | |
5111 | ---------------------- | |
5112 | ||
ce2b6ba5 | 5113 | procedure Check_Convention (Op : Entity_Id) is |
c5cec2fe | 5114 | Op_Conv : constant Convention_Id := Convention (Op); |
9f6aaa5c | 5115 | Iface_Conv : Convention_Id; |
ce2b6ba5 JM |
5116 | Iface_Elmt : Elmt_Id; |
5117 | Iface_Prim_Elmt : Elmt_Id; | |
5118 | Iface_Prim : Entity_Id; | |
ec4867fa | 5119 | |
ce2b6ba5 JM |
5120 | begin |
5121 | Iface_Elmt := First_Elmt (Ifaces_List); | |
5122 | while Present (Iface_Elmt) loop | |
5123 | Iface_Prim_Elmt := | |
9f6aaa5c | 5124 | First_Elmt (Primitive_Operations (Node (Iface_Elmt))); |
ce2b6ba5 JM |
5125 | while Present (Iface_Prim_Elmt) loop |
5126 | Iface_Prim := Node (Iface_Prim_Elmt); | |
c5cec2fe | 5127 | Iface_Conv := Convention (Iface_Prim); |
ce2b6ba5 JM |
5128 | |
5129 | if Is_Interface_Conformant (Typ, Iface_Prim, Op) | |
9f6aaa5c | 5130 | and then Iface_Conv /= Op_Conv |
ce2b6ba5 | 5131 | then |
ed2233dc | 5132 | Error_Msg_N |
ce2b6ba5 | 5133 | ("inconsistent conventions in primitive operations", Typ); |
ec4867fa | 5134 | |
ce2b6ba5 | 5135 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c | 5136 | Error_Msg_Name_2 := Get_Convention_Name (Op_Conv); |
ce2b6ba5 | 5137 | Error_Msg_Sloc := Sloc (Op); |
ec4867fa | 5138 | |
7a963087 | 5139 | if Comes_From_Source (Op) or else No (Alias (Op)) then |
038140ed | 5140 | if not Present (Overridden_Operation (Op)) then |
ed2233dc | 5141 | Error_Msg_N ("\\primitive % defined #", Typ); |
ce2b6ba5 | 5142 | else |
ed2233dc | 5143 | Error_Msg_N |
3ccedacc AC |
5144 | ("\\overriding operation % with " |
5145 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5146 | end if; |
ec4867fa | 5147 | |
ce2b6ba5 JM |
5148 | else pragma Assert (Present (Alias (Op))); |
5149 | Error_Msg_Sloc := Sloc (Alias (Op)); | |
3ccedacc AC |
5150 | Error_Msg_N ("\\inherited operation % with " |
5151 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5152 | end if; |
ec4867fa | 5153 | |
ce2b6ba5 | 5154 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c AC |
5155 | Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv); |
5156 | Error_Msg_Sloc := Sloc (Iface_Prim); | |
3ccedacc AC |
5157 | Error_Msg_N ("\\overridden operation % with " |
5158 | & "convention % defined #", Typ); | |
ec4867fa | 5159 | |
ce2b6ba5 | 5160 | -- Avoid cascading errors |
ec4867fa | 5161 | |
ce2b6ba5 JM |
5162 | return; |
5163 | end if; | |
ec4867fa | 5164 | |
ce2b6ba5 JM |
5165 | Next_Elmt (Iface_Prim_Elmt); |
5166 | end loop; | |
ec4867fa | 5167 | |
ce2b6ba5 | 5168 | Next_Elmt (Iface_Elmt); |
ec4867fa ES |
5169 | end loop; |
5170 | end Check_Convention; | |
5171 | ||
5172 | -- Local variables | |
5173 | ||
5174 | Prim_Op : Entity_Id; | |
5175 | Prim_Op_Elmt : Elmt_Id; | |
5176 | ||
5177 | -- Start of processing for Check_Conventions | |
5178 | ||
5179 | begin | |
ce2b6ba5 JM |
5180 | if not Has_Interfaces (Typ) then |
5181 | return; | |
5182 | end if; | |
5183 | ||
5184 | Collect_Interfaces (Typ, Ifaces_List); | |
5185 | ||
0a36105d JM |
5186 | -- The algorithm checks every overriding dispatching operation against |
5187 | -- all the corresponding overridden dispatching operations, detecting | |
f3d57416 | 5188 | -- differences in conventions. |
ec4867fa ES |
5189 | |
5190 | Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
5191 | while Present (Prim_Op_Elmt) loop | |
5192 | Prim_Op := Node (Prim_Op_Elmt); | |
5193 | ||
0a36105d | 5194 | -- A small optimization: skip the predefined dispatching operations |
ce2b6ba5 | 5195 | -- since they always have the same convention. |
ec4867fa | 5196 | |
ce2b6ba5 JM |
5197 | if not Is_Predefined_Dispatching_Operation (Prim_Op) then |
5198 | Check_Convention (Prim_Op); | |
ec4867fa ES |
5199 | end if; |
5200 | ||
5201 | Next_Elmt (Prim_Op_Elmt); | |
5202 | end loop; | |
5203 | end Check_Conventions; | |
5204 | ||
996ae0b0 RK |
5205 | ------------------------------ |
5206 | -- Check_Delayed_Subprogram -- | |
5207 | ------------------------------ | |
5208 | ||
5209 | procedure Check_Delayed_Subprogram (Designator : Entity_Id) is | |
5210 | F : Entity_Id; | |
5211 | ||
5212 | procedure Possible_Freeze (T : Entity_Id); | |
5213 | -- T is the type of either a formal parameter or of the return type. | |
5214 | -- If T is not yet frozen and needs a delayed freeze, then the | |
4a13695c AC |
5215 | -- subprogram itself must be delayed. If T is the limited view of an |
5216 | -- incomplete type the subprogram must be frozen as well, because | |
5217 | -- T may depend on local types that have not been frozen yet. | |
996ae0b0 | 5218 | |
82c80734 RD |
5219 | --------------------- |
5220 | -- Possible_Freeze -- | |
5221 | --------------------- | |
5222 | ||
996ae0b0 RK |
5223 | procedure Possible_Freeze (T : Entity_Id) is |
5224 | begin | |
4a13695c | 5225 | if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then |
996ae0b0 RK |
5226 | Set_Has_Delayed_Freeze (Designator); |
5227 | ||
5228 | elsif Is_Access_Type (T) | |
5229 | and then Has_Delayed_Freeze (Designated_Type (T)) | |
5230 | and then not Is_Frozen (Designated_Type (T)) | |
5231 | then | |
5232 | Set_Has_Delayed_Freeze (Designator); | |
e358346d | 5233 | |
7b56a91b AC |
5234 | elsif Ekind (T) = E_Incomplete_Type |
5235 | and then From_Limited_With (T) | |
5236 | then | |
e358346d | 5237 | Set_Has_Delayed_Freeze (Designator); |
406935b6 | 5238 | |
9aff36e9 RD |
5239 | -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile |
5240 | -- of a subprogram or entry declaration. | |
406935b6 AC |
5241 | |
5242 | elsif Ekind (T) = E_Incomplete_Type | |
5243 | and then Ada_Version >= Ada_2012 | |
5244 | then | |
5245 | Set_Has_Delayed_Freeze (Designator); | |
996ae0b0 | 5246 | end if; |
4a13695c | 5247 | |
996ae0b0 RK |
5248 | end Possible_Freeze; |
5249 | ||
5250 | -- Start of processing for Check_Delayed_Subprogram | |
5251 | ||
5252 | begin | |
76e3504f AC |
5253 | -- All subprograms, including abstract subprograms, may need a freeze |
5254 | -- node if some formal type or the return type needs one. | |
996ae0b0 | 5255 | |
76e3504f AC |
5256 | Possible_Freeze (Etype (Designator)); |
5257 | Possible_Freeze (Base_Type (Etype (Designator))); -- needed ??? | |
996ae0b0 | 5258 | |
76e3504f AC |
5259 | -- Need delayed freeze if any of the formal types themselves need |
5260 | -- a delayed freeze and are not yet frozen. | |
996ae0b0 | 5261 | |
76e3504f AC |
5262 | F := First_Formal (Designator); |
5263 | while Present (F) loop | |
5264 | Possible_Freeze (Etype (F)); | |
5265 | Possible_Freeze (Base_Type (Etype (F))); -- needed ??? | |
5266 | Next_Formal (F); | |
5267 | end loop; | |
996ae0b0 RK |
5268 | |
5269 | -- Mark functions that return by reference. Note that it cannot be | |
5270 | -- done for delayed_freeze subprograms because the underlying | |
5271 | -- returned type may not be known yet (for private types) | |
5272 | ||
8fde064e | 5273 | if not Has_Delayed_Freeze (Designator) and then Expander_Active then |
996ae0b0 RK |
5274 | declare |
5275 | Typ : constant Entity_Id := Etype (Designator); | |
5276 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
996ae0b0 | 5277 | begin |
51245e2d | 5278 | if Is_Limited_View (Typ) then |
996ae0b0 | 5279 | Set_Returns_By_Ref (Designator); |
048e5cef | 5280 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
996ae0b0 RK |
5281 | Set_Returns_By_Ref (Designator); |
5282 | end if; | |
5283 | end; | |
5284 | end if; | |
5285 | end Check_Delayed_Subprogram; | |
5286 | ||
5287 | ------------------------------------ | |
5288 | -- Check_Discriminant_Conformance -- | |
5289 | ------------------------------------ | |
5290 | ||
5291 | procedure Check_Discriminant_Conformance | |
5292 | (N : Node_Id; | |
5293 | Prev : Entity_Id; | |
5294 | Prev_Loc : Node_Id) | |
5295 | is | |
5296 | Old_Discr : Entity_Id := First_Discriminant (Prev); | |
5297 | New_Discr : Node_Id := First (Discriminant_Specifications (N)); | |
5298 | New_Discr_Id : Entity_Id; | |
5299 | New_Discr_Type : Entity_Id; | |
5300 | ||
5301 | procedure Conformance_Error (Msg : String; N : Node_Id); | |
82c80734 RD |
5302 | -- Post error message for conformance error on given node. Two messages |
5303 | -- are output. The first points to the previous declaration with a | |
5304 | -- general "no conformance" message. The second is the detailed reason, | |
5305 | -- supplied as Msg. The parameter N provide information for a possible | |
5306 | -- & insertion in the message. | |
996ae0b0 RK |
5307 | |
5308 | ----------------------- | |
5309 | -- Conformance_Error -- | |
5310 | ----------------------- | |
5311 | ||
5312 | procedure Conformance_Error (Msg : String; N : Node_Id) is | |
5313 | begin | |
5314 | Error_Msg_Sloc := Sloc (Prev_Loc); | |
483c78cb RD |
5315 | Error_Msg_N -- CODEFIX |
5316 | ("not fully conformant with declaration#!", N); | |
996ae0b0 RK |
5317 | Error_Msg_NE (Msg, N, N); |
5318 | end Conformance_Error; | |
5319 | ||
5320 | -- Start of processing for Check_Discriminant_Conformance | |
5321 | ||
5322 | begin | |
5323 | while Present (Old_Discr) and then Present (New_Discr) loop | |
996ae0b0 RK |
5324 | New_Discr_Id := Defining_Identifier (New_Discr); |
5325 | ||
82c80734 RD |
5326 | -- The subtype mark of the discriminant on the full type has not |
5327 | -- been analyzed so we do it here. For an access discriminant a new | |
5328 | -- type is created. | |
996ae0b0 RK |
5329 | |
5330 | if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then | |
5331 | New_Discr_Type := | |
5332 | Access_Definition (N, Discriminant_Type (New_Discr)); | |
5333 | ||
5334 | else | |
5335 | Analyze (Discriminant_Type (New_Discr)); | |
5336 | New_Discr_Type := Etype (Discriminant_Type (New_Discr)); | |
e50e1c5e AC |
5337 | |
5338 | -- Ada 2005: if the discriminant definition carries a null | |
5339 | -- exclusion, create an itype to check properly for consistency | |
5340 | -- with partial declaration. | |
5341 | ||
5342 | if Is_Access_Type (New_Discr_Type) | |
8fde064e | 5343 | and then Null_Exclusion_Present (New_Discr) |
e50e1c5e AC |
5344 | then |
5345 | New_Discr_Type := | |
5346 | Create_Null_Excluding_Itype | |
5347 | (T => New_Discr_Type, | |
5348 | Related_Nod => New_Discr, | |
5349 | Scope_Id => Current_Scope); | |
5350 | end if; | |
996ae0b0 RK |
5351 | end if; |
5352 | ||
5353 | if not Conforming_Types | |
5354 | (Etype (Old_Discr), New_Discr_Type, Fully_Conformant) | |
5355 | then | |
5356 | Conformance_Error ("type of & does not match!", New_Discr_Id); | |
5357 | return; | |
fbf5a39b | 5358 | else |
82c80734 RD |
5359 | -- Treat the new discriminant as an occurrence of the old one, |
5360 | -- for navigation purposes, and fill in some semantic | |
fbf5a39b AC |
5361 | -- information, for completeness. |
5362 | ||
5363 | Generate_Reference (Old_Discr, New_Discr_Id, 'r'); | |
5364 | Set_Etype (New_Discr_Id, Etype (Old_Discr)); | |
5365 | Set_Scope (New_Discr_Id, Scope (Old_Discr)); | |
996ae0b0 RK |
5366 | end if; |
5367 | ||
5368 | -- Names must match | |
5369 | ||
5370 | if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then | |
5371 | Conformance_Error ("name & does not match!", New_Discr_Id); | |
5372 | return; | |
5373 | end if; | |
5374 | ||
5375 | -- Default expressions must match | |
5376 | ||
5377 | declare | |
5378 | NewD : constant Boolean := | |
5379 | Present (Expression (New_Discr)); | |
5380 | OldD : constant Boolean := | |
5381 | Present (Expression (Parent (Old_Discr))); | |
5382 | ||
5383 | begin | |
5384 | if NewD or OldD then | |
5385 | ||
5386 | -- The old default value has been analyzed and expanded, | |
5387 | -- because the current full declaration will have frozen | |
82c80734 RD |
5388 | -- everything before. The new default values have not been |
5389 | -- expanded, so expand now to check conformance. | |
996ae0b0 RK |
5390 | |
5391 | if NewD then | |
21d27997 | 5392 | Preanalyze_Spec_Expression |
996ae0b0 RK |
5393 | (Expression (New_Discr), New_Discr_Type); |
5394 | end if; | |
5395 | ||
5396 | if not (NewD and OldD) | |
5397 | or else not Fully_Conformant_Expressions | |
5398 | (Expression (Parent (Old_Discr)), | |
5399 | Expression (New_Discr)) | |
5400 | ||
5401 | then | |
5402 | Conformance_Error | |
5403 | ("default expression for & does not match!", | |
5404 | New_Discr_Id); | |
5405 | return; | |
5406 | end if; | |
5407 | end if; | |
5408 | end; | |
5409 | ||
5410 | -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X) | |
5411 | ||
0ab80019 | 5412 | if Ada_Version = Ada_83 then |
996ae0b0 RK |
5413 | declare |
5414 | Old_Disc : constant Node_Id := Declaration_Node (Old_Discr); | |
5415 | ||
5416 | begin | |
5417 | -- Grouping (use of comma in param lists) must be the same | |
5418 | -- This is where we catch a misconformance like: | |
5419 | ||
60370fb1 | 5420 | -- A, B : Integer |
996ae0b0 RK |
5421 | -- A : Integer; B : Integer |
5422 | ||
5423 | -- which are represented identically in the tree except | |
5424 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5425 | ||
5426 | if More_Ids (Old_Disc) /= More_Ids (New_Discr) | |
5427 | or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr) | |
5428 | then | |
5429 | Conformance_Error | |
5430 | ("grouping of & does not match!", New_Discr_Id); | |
5431 | return; | |
5432 | end if; | |
5433 | end; | |
5434 | end if; | |
5435 | ||
5436 | Next_Discriminant (Old_Discr); | |
5437 | Next (New_Discr); | |
5438 | end loop; | |
5439 | ||
5440 | if Present (Old_Discr) then | |
5441 | Conformance_Error ("too few discriminants!", Defining_Identifier (N)); | |
5442 | return; | |
5443 | ||
5444 | elsif Present (New_Discr) then | |
5445 | Conformance_Error | |
5446 | ("too many discriminants!", Defining_Identifier (New_Discr)); | |
5447 | return; | |
5448 | end if; | |
5449 | end Check_Discriminant_Conformance; | |
5450 | ||
5451 | ---------------------------- | |
5452 | -- Check_Fully_Conformant -- | |
5453 | ---------------------------- | |
5454 | ||
5455 | procedure Check_Fully_Conformant | |
5456 | (New_Id : Entity_Id; | |
5457 | Old_Id : Entity_Id; | |
5458 | Err_Loc : Node_Id := Empty) | |
5459 | is | |
5460 | Result : Boolean; | |
81db9d77 | 5461 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5462 | begin |
5463 | Check_Conformance | |
5464 | (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc); | |
5465 | end Check_Fully_Conformant; | |
5466 | ||
5467 | --------------------------- | |
5468 | -- Check_Mode_Conformant -- | |
5469 | --------------------------- | |
5470 | ||
5471 | procedure Check_Mode_Conformant | |
5472 | (New_Id : Entity_Id; | |
5473 | Old_Id : Entity_Id; | |
5474 | Err_Loc : Node_Id := Empty; | |
5475 | Get_Inst : Boolean := False) | |
5476 | is | |
5477 | Result : Boolean; | |
81db9d77 | 5478 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5479 | begin |
5480 | Check_Conformance | |
5481 | (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst); | |
5482 | end Check_Mode_Conformant; | |
5483 | ||
fbf5a39b | 5484 | -------------------------------- |
758c442c | 5485 | -- Check_Overriding_Indicator -- |
fbf5a39b AC |
5486 | -------------------------------- |
5487 | ||
758c442c | 5488 | procedure Check_Overriding_Indicator |
ec4867fa | 5489 | (Subp : Entity_Id; |
5d37ba92 ES |
5490 | Overridden_Subp : Entity_Id; |
5491 | Is_Primitive : Boolean) | |
fbf5a39b | 5492 | is |
758c442c GD |
5493 | Decl : Node_Id; |
5494 | Spec : Node_Id; | |
fbf5a39b AC |
5495 | |
5496 | begin | |
ec4867fa | 5497 | -- No overriding indicator for literals |
fbf5a39b | 5498 | |
ec4867fa | 5499 | if Ekind (Subp) = E_Enumeration_Literal then |
758c442c | 5500 | return; |
fbf5a39b | 5501 | |
ec4867fa ES |
5502 | elsif Ekind (Subp) = E_Entry then |
5503 | Decl := Parent (Subp); | |
5504 | ||
53b10ce9 AC |
5505 | -- No point in analyzing a malformed operator |
5506 | ||
5507 | elsif Nkind (Subp) = N_Defining_Operator_Symbol | |
5508 | and then Error_Posted (Subp) | |
5509 | then | |
5510 | return; | |
5511 | ||
758c442c GD |
5512 | else |
5513 | Decl := Unit_Declaration_Node (Subp); | |
5514 | end if; | |
fbf5a39b | 5515 | |
800621e0 RD |
5516 | if Nkind_In (Decl, N_Subprogram_Body, |
5517 | N_Subprogram_Body_Stub, | |
5518 | N_Subprogram_Declaration, | |
5519 | N_Abstract_Subprogram_Declaration, | |
5520 | N_Subprogram_Renaming_Declaration) | |
758c442c GD |
5521 | then |
5522 | Spec := Specification (Decl); | |
ec4867fa ES |
5523 | |
5524 | elsif Nkind (Decl) = N_Entry_Declaration then | |
5525 | Spec := Decl; | |
5526 | ||
758c442c GD |
5527 | else |
5528 | return; | |
5529 | end if; | |
fbf5a39b | 5530 | |
e7d72fb9 AC |
5531 | -- The overriding operation is type conformant with the overridden one, |
5532 | -- but the names of the formals are not required to match. If the names | |
6823270c | 5533 | -- appear permuted in the overriding operation, this is a possible |
e7d72fb9 AC |
5534 | -- source of confusion that is worth diagnosing. Controlling formals |
5535 | -- often carry names that reflect the type, and it is not worthwhile | |
5536 | -- requiring that their names match. | |
5537 | ||
c9e7bd8e | 5538 | if Present (Overridden_Subp) |
e7d72fb9 AC |
5539 | and then Nkind (Subp) /= N_Defining_Operator_Symbol |
5540 | then | |
5541 | declare | |
5542 | Form1 : Entity_Id; | |
5543 | Form2 : Entity_Id; | |
5544 | ||
5545 | begin | |
5546 | Form1 := First_Formal (Subp); | |
5547 | Form2 := First_Formal (Overridden_Subp); | |
5548 | ||
c9e7bd8e AC |
5549 | -- If the overriding operation is a synchronized operation, skip |
5550 | -- the first parameter of the overridden operation, which is | |
6823270c AC |
5551 | -- implicit in the new one. If the operation is declared in the |
5552 | -- body it is not primitive and all formals must match. | |
c9e7bd8e | 5553 | |
6823270c AC |
5554 | if Is_Concurrent_Type (Scope (Subp)) |
5555 | and then Is_Tagged_Type (Scope (Subp)) | |
5556 | and then not Has_Completion (Scope (Subp)) | |
5557 | then | |
c9e7bd8e AC |
5558 | Form2 := Next_Formal (Form2); |
5559 | end if; | |
5560 | ||
e7d72fb9 AC |
5561 | if Present (Form1) then |
5562 | Form1 := Next_Formal (Form1); | |
5563 | Form2 := Next_Formal (Form2); | |
5564 | end if; | |
5565 | ||
5566 | while Present (Form1) loop | |
5567 | if not Is_Controlling_Formal (Form1) | |
5568 | and then Present (Next_Formal (Form2)) | |
5569 | and then Chars (Form1) = Chars (Next_Formal (Form2)) | |
5570 | then | |
5571 | Error_Msg_Node_2 := Alias (Overridden_Subp); | |
5572 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
ed2233dc | 5573 | Error_Msg_NE |
19d846a0 | 5574 | ("& does not match corresponding formal of&#", |
e7d72fb9 AC |
5575 | Form1, Form1); |
5576 | exit; | |
5577 | end if; | |
5578 | ||
5579 | Next_Formal (Form1); | |
5580 | Next_Formal (Form2); | |
5581 | end loop; | |
5582 | end; | |
5583 | end if; | |
5584 | ||
676e8420 AC |
5585 | -- If there is an overridden subprogram, then check that there is no |
5586 | -- "not overriding" indicator, and mark the subprogram as overriding. | |
51bf9bdf AC |
5587 | -- This is not done if the overridden subprogram is marked as hidden, |
5588 | -- which can occur for the case of inherited controlled operations | |
5589 | -- (see Derive_Subprogram), unless the inherited subprogram's parent | |
5590 | -- subprogram is not itself hidden. (Note: This condition could probably | |
5591 | -- be simplified, leaving out the testing for the specific controlled | |
5592 | -- cases, but it seems safer and clearer this way, and echoes similar | |
5593 | -- special-case tests of this kind in other places.) | |
5594 | ||
fd0d899b | 5595 | if Present (Overridden_Subp) |
51bf9bdf AC |
5596 | and then (not Is_Hidden (Overridden_Subp) |
5597 | or else | |
b69cd36a AC |
5598 | (Nam_In (Chars (Overridden_Subp), Name_Initialize, |
5599 | Name_Adjust, | |
5600 | Name_Finalize) | |
f0709ca6 AC |
5601 | and then Present (Alias (Overridden_Subp)) |
5602 | and then not Is_Hidden (Alias (Overridden_Subp)))) | |
fd0d899b | 5603 | then |
ec4867fa ES |
5604 | if Must_Not_Override (Spec) then |
5605 | Error_Msg_Sloc := Sloc (Overridden_Subp); | |
fbf5a39b | 5606 | |
ec4867fa | 5607 | if Ekind (Subp) = E_Entry then |
ed2233dc | 5608 | Error_Msg_NE |
5d37ba92 | 5609 | ("entry & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5610 | else |
ed2233dc | 5611 | Error_Msg_NE |
5d37ba92 | 5612 | ("subprogram & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5613 | end if; |
21d27997 | 5614 | |
bd603506 | 5615 | -- Special-case to fix a GNAT oddity: Limited_Controlled is declared |
24a120ac AC |
5616 | -- as an extension of Root_Controlled, and thus has a useless Adjust |
5617 | -- operation. This operation should not be inherited by other limited | |
5618 | -- controlled types. An explicit Adjust for them is not overriding. | |
5619 | ||
5620 | elsif Must_Override (Spec) | |
5621 | and then Chars (Overridden_Subp) = Name_Adjust | |
5622 | and then Is_Limited_Type (Etype (First_Formal (Subp))) | |
5623 | and then Present (Alias (Overridden_Subp)) | |
bd603506 RD |
5624 | and then |
5625 | Is_Predefined_File_Name | |
5626 | (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp)))) | |
24a120ac AC |
5627 | then |
5628 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5629 | ||
21d27997 | 5630 | elsif Is_Subprogram (Subp) then |
2fe829ae ES |
5631 | if Is_Init_Proc (Subp) then |
5632 | null; | |
5633 | ||
5634 | elsif No (Overridden_Operation (Subp)) then | |
1c1289e7 AC |
5635 | |
5636 | -- For entities generated by Derive_Subprograms the overridden | |
5637 | -- operation is the inherited primitive (which is available | |
5638 | -- through the attribute alias) | |
5639 | ||
5640 | if (Is_Dispatching_Operation (Subp) | |
f9673bb0 | 5641 | or else Is_Dispatching_Operation (Overridden_Subp)) |
1c1289e7 | 5642 | and then not Comes_From_Source (Overridden_Subp) |
f9673bb0 AC |
5643 | and then Find_Dispatching_Type (Overridden_Subp) = |
5644 | Find_Dispatching_Type (Subp) | |
1c1289e7 AC |
5645 | and then Present (Alias (Overridden_Subp)) |
5646 | and then Comes_From_Source (Alias (Overridden_Subp)) | |
5647 | then | |
039538bc AC |
5648 | Set_Overridden_Operation (Subp, Alias (Overridden_Subp)); |
5649 | Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp)); | |
2fe829ae | 5650 | |
1c1289e7 | 5651 | else |
039538bc AC |
5652 | Set_Overridden_Operation (Subp, Overridden_Subp); |
5653 | Inherit_Subprogram_Contract (Subp, Overridden_Subp); | |
1c1289e7 AC |
5654 | end if; |
5655 | end if; | |
ec4867fa | 5656 | end if; |
f937473f | 5657 | |
618fb570 AC |
5658 | -- If primitive flag is set or this is a protected operation, then |
5659 | -- the operation is overriding at the point of its declaration, so | |
5660 | -- warn if necessary. Otherwise it may have been declared before the | |
5661 | -- operation it overrides and no check is required. | |
3c25856a AC |
5662 | |
5663 | if Style_Check | |
618fb570 AC |
5664 | and then not Must_Override (Spec) |
5665 | and then (Is_Primitive | |
5666 | or else Ekind (Scope (Subp)) = E_Protected_Type) | |
3c25856a | 5667 | then |
235f4375 AC |
5668 | Style.Missing_Overriding (Decl, Subp); |
5669 | end if; | |
5670 | ||
53b10ce9 AC |
5671 | -- If Subp is an operator, it may override a predefined operation, if |
5672 | -- it is defined in the same scope as the type to which it applies. | |
676e8420 | 5673 | -- In that case Overridden_Subp is empty because of our implicit |
5d37ba92 ES |
5674 | -- representation for predefined operators. We have to check whether the |
5675 | -- signature of Subp matches that of a predefined operator. Note that | |
5676 | -- first argument provides the name of the operator, and the second | |
5677 | -- argument the signature that may match that of a standard operation. | |
21d27997 RD |
5678 | -- If the indicator is overriding, then the operator must match a |
5679 | -- predefined signature, because we know already that there is no | |
5680 | -- explicit overridden operation. | |
f937473f | 5681 | |
21d27997 | 5682 | elsif Nkind (Subp) = N_Defining_Operator_Symbol then |
806f6d37 | 5683 | if Must_Not_Override (Spec) then |
f937473f | 5684 | |
806f6d37 AC |
5685 | -- If this is not a primitive or a protected subprogram, then |
5686 | -- "not overriding" is illegal. | |
618fb570 | 5687 | |
806f6d37 AC |
5688 | if not Is_Primitive |
5689 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5690 | then | |
3ccedacc AC |
5691 | Error_Msg_N ("overriding indicator only allowed " |
5692 | & "if subprogram is primitive", Subp); | |
618fb570 | 5693 | |
806f6d37 AC |
5694 | elsif Can_Override_Operator (Subp) then |
5695 | Error_Msg_NE | |
5696 | ("subprogram& overrides predefined operator ", Spec, Subp); | |
5697 | end if; | |
f937473f | 5698 | |
806f6d37 AC |
5699 | elsif Must_Override (Spec) then |
5700 | if No (Overridden_Operation (Subp)) | |
5701 | and then not Can_Override_Operator (Subp) | |
5702 | then | |
5703 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5704 | end if; | |
5d37ba92 | 5705 | |
806f6d37 AC |
5706 | elsif not Error_Posted (Subp) |
5707 | and then Style_Check | |
5708 | and then Can_Override_Operator (Subp) | |
5709 | and then | |
5710 | not Is_Predefined_File_Name | |
5711 | (Unit_File_Name (Get_Source_Unit (Subp))) | |
5712 | then | |
5713 | -- If style checks are enabled, indicate that the indicator is | |
5714 | -- missing. However, at the point of declaration, the type of | |
5715 | -- which this is a primitive operation may be private, in which | |
5716 | -- case the indicator would be premature. | |
235f4375 | 5717 | |
806f6d37 AC |
5718 | if Has_Private_Declaration (Etype (Subp)) |
5719 | or else Has_Private_Declaration (Etype (First_Formal (Subp))) | |
53b10ce9 | 5720 | then |
806f6d37 AC |
5721 | null; |
5722 | else | |
5723 | Style.Missing_Overriding (Decl, Subp); | |
5d5832bc | 5724 | end if; |
806f6d37 | 5725 | end if; |
21d27997 RD |
5726 | |
5727 | elsif Must_Override (Spec) then | |
5728 | if Ekind (Subp) = E_Entry then | |
ed2233dc | 5729 | Error_Msg_NE ("entry & is not overriding", Spec, Subp); |
5d37ba92 | 5730 | else |
ed2233dc | 5731 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
758c442c | 5732 | end if; |
5d37ba92 ES |
5733 | |
5734 | -- If the operation is marked "not overriding" and it's not primitive | |
5735 | -- then an error is issued, unless this is an operation of a task or | |
5736 | -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding" | |
5737 | -- has been specified have already been checked above. | |
5738 | ||
5739 | elsif Must_Not_Override (Spec) | |
5740 | and then not Is_Primitive | |
5741 | and then Ekind (Subp) /= E_Entry | |
5742 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5743 | then | |
ed2233dc | 5744 | Error_Msg_N |
5d37ba92 ES |
5745 | ("overriding indicator only allowed if subprogram is primitive", |
5746 | Subp); | |
5d37ba92 | 5747 | return; |
fbf5a39b | 5748 | end if; |
758c442c | 5749 | end Check_Overriding_Indicator; |
fbf5a39b | 5750 | |
996ae0b0 RK |
5751 | ------------------- |
5752 | -- Check_Returns -- | |
5753 | ------------------- | |
5754 | ||
0a36105d JM |
5755 | -- Note: this procedure needs to know far too much about how the expander |
5756 | -- messes with exceptions. The use of the flag Exception_Junk and the | |
5757 | -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers | |
5758 | -- works, but is not very clean. It would be better if the expansion | |
5759 | -- routines would leave Original_Node working nicely, and we could use | |
5760 | -- Original_Node here to ignore all the peculiar expander messing ??? | |
5761 | ||
996ae0b0 RK |
5762 | procedure Check_Returns |
5763 | (HSS : Node_Id; | |
5764 | Mode : Character; | |
c8ef728f ES |
5765 | Err : out Boolean; |
5766 | Proc : Entity_Id := Empty) | |
996ae0b0 RK |
5767 | is |
5768 | Handler : Node_Id; | |
5769 | ||
5770 | procedure Check_Statement_Sequence (L : List_Id); | |
5771 | -- Internal recursive procedure to check a list of statements for proper | |
5772 | -- termination by a return statement (or a transfer of control or a | |
5773 | -- compound statement that is itself internally properly terminated). | |
5774 | ||
5775 | ------------------------------ | |
5776 | -- Check_Statement_Sequence -- | |
5777 | ------------------------------ | |
5778 | ||
5779 | procedure Check_Statement_Sequence (L : List_Id) is | |
5780 | Last_Stm : Node_Id; | |
0a36105d | 5781 | Stm : Node_Id; |
996ae0b0 RK |
5782 | Kind : Node_Kind; |
5783 | ||
7b27e183 AC |
5784 | function Assert_False return Boolean; |
5785 | -- Returns True if Last_Stm is a pragma Assert (False) that has been | |
5786 | -- rewritten as a null statement when assertions are off. The assert | |
5787 | -- is not active, but it is still enough to kill the warning. | |
5788 | ||
5789 | ------------------ | |
5790 | -- Assert_False -- | |
5791 | ------------------ | |
5792 | ||
5793 | function Assert_False return Boolean is | |
5794 | Orig : constant Node_Id := Original_Node (Last_Stm); | |
5795 | ||
5796 | begin | |
5797 | if Nkind (Orig) = N_Pragma | |
5798 | and then Pragma_Name (Orig) = Name_Assert | |
5799 | and then not Error_Posted (Orig) | |
5800 | then | |
5801 | declare | |
5802 | Arg : constant Node_Id := | |
5803 | First (Pragma_Argument_Associations (Orig)); | |
5804 | Exp : constant Node_Id := Expression (Arg); | |
5805 | begin | |
5806 | return Nkind (Exp) = N_Identifier | |
5807 | and then Chars (Exp) = Name_False; | |
5808 | end; | |
5809 | ||
5810 | else | |
5811 | return False; | |
5812 | end if; | |
5813 | end Assert_False; | |
5814 | ||
5815 | -- Local variables | |
5816 | ||
996ae0b0 RK |
5817 | Raise_Exception_Call : Boolean; |
5818 | -- Set True if statement sequence terminated by Raise_Exception call | |
5819 | -- or a Reraise_Occurrence call. | |
5820 | ||
7b27e183 AC |
5821 | -- Start of processing for Check_Statement_Sequence |
5822 | ||
996ae0b0 RK |
5823 | begin |
5824 | Raise_Exception_Call := False; | |
5825 | ||
5826 | -- Get last real statement | |
5827 | ||
5828 | Last_Stm := Last (L); | |
5829 | ||
0a36105d JM |
5830 | -- Deal with digging out exception handler statement sequences that |
5831 | -- have been transformed by the local raise to goto optimization. | |
5832 | -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this | |
5833 | -- optimization has occurred, we are looking at something like: | |
5834 | ||
5835 | -- begin | |
5836 | -- original stmts in block | |
5837 | ||
5838 | -- exception \ | |
5839 | -- when excep1 => | | |
5840 | -- goto L1; | omitted if No_Exception_Propagation | |
5841 | -- when excep2 => | | |
5842 | -- goto L2; / | |
5843 | -- end; | |
5844 | ||
5845 | -- goto L3; -- skip handler when exception not raised | |
5846 | ||
5847 | -- <<L1>> -- target label for local exception | |
5848 | -- begin | |
5849 | -- estmts1 | |
5850 | -- end; | |
5851 | ||
5852 | -- goto L3; | |
5853 | ||
5854 | -- <<L2>> | |
5855 | -- begin | |
5856 | -- estmts2 | |
5857 | -- end; | |
5858 | ||
5859 | -- <<L3>> | |
5860 | ||
5861 | -- and what we have to do is to dig out the estmts1 and estmts2 | |
5862 | -- sequences (which were the original sequences of statements in | |
5863 | -- the exception handlers) and check them. | |
5864 | ||
8fde064e | 5865 | if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then |
0a36105d JM |
5866 | Stm := Last_Stm; |
5867 | loop | |
5868 | Prev (Stm); | |
5869 | exit when No (Stm); | |
5870 | exit when Nkind (Stm) /= N_Block_Statement; | |
5871 | exit when not Exception_Junk (Stm); | |
5872 | Prev (Stm); | |
5873 | exit when No (Stm); | |
5874 | exit when Nkind (Stm) /= N_Label; | |
5875 | exit when not Exception_Junk (Stm); | |
5876 | Check_Statement_Sequence | |
5877 | (Statements (Handled_Statement_Sequence (Next (Stm)))); | |
5878 | ||
5879 | Prev (Stm); | |
5880 | Last_Stm := Stm; | |
5881 | exit when No (Stm); | |
5882 | exit when Nkind (Stm) /= N_Goto_Statement; | |
5883 | exit when not Exception_Junk (Stm); | |
5884 | end loop; | |
5885 | end if; | |
5886 | ||
996ae0b0 RK |
5887 | -- Don't count pragmas |
5888 | ||
5889 | while Nkind (Last_Stm) = N_Pragma | |
5890 | ||
5891 | -- Don't count call to SS_Release (can happen after Raise_Exception) | |
5892 | ||
5893 | or else | |
5894 | (Nkind (Last_Stm) = N_Procedure_Call_Statement | |
5895 | and then | |
5896 | Nkind (Name (Last_Stm)) = N_Identifier | |
5897 | and then | |
5898 | Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release)) | |
5899 | ||
5900 | -- Don't count exception junk | |
5901 | ||
5902 | or else | |
800621e0 RD |
5903 | (Nkind_In (Last_Stm, N_Goto_Statement, |
5904 | N_Label, | |
5905 | N_Object_Declaration) | |
8fde064e | 5906 | and then Exception_Junk (Last_Stm)) |
0a36105d JM |
5907 | or else Nkind (Last_Stm) in N_Push_xxx_Label |
5908 | or else Nkind (Last_Stm) in N_Pop_xxx_Label | |
e3b3266c AC |
5909 | |
5910 | -- Inserted code, such as finalization calls, is irrelevant: we only | |
5911 | -- need to check original source. | |
5912 | ||
5913 | or else Is_Rewrite_Insertion (Last_Stm) | |
996ae0b0 RK |
5914 | loop |
5915 | Prev (Last_Stm); | |
5916 | end loop; | |
5917 | ||
5918 | -- Here we have the "real" last statement | |
5919 | ||
5920 | Kind := Nkind (Last_Stm); | |
5921 | ||
5922 | -- Transfer of control, OK. Note that in the No_Return procedure | |
5923 | -- case, we already diagnosed any explicit return statements, so | |
5924 | -- we can treat them as OK in this context. | |
5925 | ||
5926 | if Is_Transfer (Last_Stm) then | |
5927 | return; | |
5928 | ||
5929 | -- Check cases of explicit non-indirect procedure calls | |
5930 | ||
5931 | elsif Kind = N_Procedure_Call_Statement | |
5932 | and then Is_Entity_Name (Name (Last_Stm)) | |
5933 | then | |
5934 | -- Check call to Raise_Exception procedure which is treated | |
5935 | -- specially, as is a call to Reraise_Occurrence. | |
5936 | ||
5937 | -- We suppress the warning in these cases since it is likely that | |
5938 | -- the programmer really does not expect to deal with the case | |
5939 | -- of Null_Occurrence, and thus would find a warning about a | |
5940 | -- missing return curious, and raising Program_Error does not | |
5941 | -- seem such a bad behavior if this does occur. | |
5942 | ||
c8ef728f ES |
5943 | -- Note that in the Ada 2005 case for Raise_Exception, the actual |
5944 | -- behavior will be to raise Constraint_Error (see AI-329). | |
5945 | ||
996ae0b0 RK |
5946 | if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception) |
5947 | or else | |
5948 | Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence) | |
5949 | then | |
5950 | Raise_Exception_Call := True; | |
5951 | ||
5952 | -- For Raise_Exception call, test first argument, if it is | |
5953 | -- an attribute reference for a 'Identity call, then we know | |
5954 | -- that the call cannot possibly return. | |
5955 | ||
5956 | declare | |
5957 | Arg : constant Node_Id := | |
5958 | Original_Node (First_Actual (Last_Stm)); | |
996ae0b0 RK |
5959 | begin |
5960 | if Nkind (Arg) = N_Attribute_Reference | |
5961 | and then Attribute_Name (Arg) = Name_Identity | |
5962 | then | |
5963 | return; | |
5964 | end if; | |
5965 | end; | |
5966 | end if; | |
5967 | ||
5968 | -- If statement, need to look inside if there is an else and check | |
5969 | -- each constituent statement sequence for proper termination. | |
5970 | ||
5971 | elsif Kind = N_If_Statement | |
5972 | and then Present (Else_Statements (Last_Stm)) | |
5973 | then | |
5974 | Check_Statement_Sequence (Then_Statements (Last_Stm)); | |
5975 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
5976 | ||
5977 | if Present (Elsif_Parts (Last_Stm)) then | |
5978 | declare | |
5979 | Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm)); | |
5980 | ||
5981 | begin | |
5982 | while Present (Elsif_Part) loop | |
5983 | Check_Statement_Sequence (Then_Statements (Elsif_Part)); | |
5984 | Next (Elsif_Part); | |
5985 | end loop; | |
5986 | end; | |
5987 | end if; | |
5988 | ||
5989 | return; | |
5990 | ||
5991 | -- Case statement, check each case for proper termination | |
5992 | ||
5993 | elsif Kind = N_Case_Statement then | |
5994 | declare | |
5995 | Case_Alt : Node_Id; | |
996ae0b0 RK |
5996 | begin |
5997 | Case_Alt := First_Non_Pragma (Alternatives (Last_Stm)); | |
5998 | while Present (Case_Alt) loop | |
5999 | Check_Statement_Sequence (Statements (Case_Alt)); | |
6000 | Next_Non_Pragma (Case_Alt); | |
6001 | end loop; | |
6002 | end; | |
6003 | ||
6004 | return; | |
6005 | ||
6006 | -- Block statement, check its handled sequence of statements | |
6007 | ||
6008 | elsif Kind = N_Block_Statement then | |
6009 | declare | |
6010 | Err1 : Boolean; | |
6011 | ||
6012 | begin | |
6013 | Check_Returns | |
6014 | (Handled_Statement_Sequence (Last_Stm), Mode, Err1); | |
6015 | ||
6016 | if Err1 then | |
6017 | Err := True; | |
6018 | end if; | |
6019 | ||
6020 | return; | |
6021 | end; | |
6022 | ||
6023 | -- Loop statement. If there is an iteration scheme, we can definitely | |
6024 | -- fall out of the loop. Similarly if there is an exit statement, we | |
6025 | -- can fall out. In either case we need a following return. | |
6026 | ||
6027 | elsif Kind = N_Loop_Statement then | |
6028 | if Present (Iteration_Scheme (Last_Stm)) | |
6029 | or else Has_Exit (Entity (Identifier (Last_Stm))) | |
6030 | then | |
6031 | null; | |
6032 | ||
f3d57416 RW |
6033 | -- A loop with no exit statement or iteration scheme is either |
6034 | -- an infinite loop, or it has some other exit (raise/return). | |
996ae0b0 RK |
6035 | -- In either case, no warning is required. |
6036 | ||
6037 | else | |
6038 | return; | |
6039 | end if; | |
6040 | ||
6041 | -- Timed entry call, check entry call and delay alternatives | |
6042 | ||
6043 | -- Note: in expanded code, the timed entry call has been converted | |
6044 | -- to a set of expanded statements on which the check will work | |
6045 | -- correctly in any case. | |
6046 | ||
6047 | elsif Kind = N_Timed_Entry_Call then | |
6048 | declare | |
6049 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6050 | DCA : constant Node_Id := Delay_Alternative (Last_Stm); | |
6051 | ||
6052 | begin | |
6053 | -- If statement sequence of entry call alternative is missing, | |
6054 | -- then we can definitely fall through, and we post the error | |
6055 | -- message on the entry call alternative itself. | |
6056 | ||
6057 | if No (Statements (ECA)) then | |
6058 | Last_Stm := ECA; | |
6059 | ||
6060 | -- If statement sequence of delay alternative is missing, then | |
6061 | -- we can definitely fall through, and we post the error | |
6062 | -- message on the delay alternative itself. | |
6063 | ||
6064 | -- Note: if both ECA and DCA are missing the return, then we | |
6065 | -- post only one message, should be enough to fix the bugs. | |
6066 | -- If not we will get a message next time on the DCA when the | |
a90bd866 | 6067 | -- ECA is fixed. |
996ae0b0 RK |
6068 | |
6069 | elsif No (Statements (DCA)) then | |
6070 | Last_Stm := DCA; | |
6071 | ||
6072 | -- Else check both statement sequences | |
6073 | ||
6074 | else | |
6075 | Check_Statement_Sequence (Statements (ECA)); | |
6076 | Check_Statement_Sequence (Statements (DCA)); | |
6077 | return; | |
6078 | end if; | |
6079 | end; | |
6080 | ||
6081 | -- Conditional entry call, check entry call and else part | |
6082 | ||
6083 | -- Note: in expanded code, the conditional entry call has been | |
6084 | -- converted to a set of expanded statements on which the check | |
6085 | -- will work correctly in any case. | |
6086 | ||
6087 | elsif Kind = N_Conditional_Entry_Call then | |
6088 | declare | |
6089 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6090 | ||
6091 | begin | |
6092 | -- If statement sequence of entry call alternative is missing, | |
6093 | -- then we can definitely fall through, and we post the error | |
6094 | -- message on the entry call alternative itself. | |
6095 | ||
6096 | if No (Statements (ECA)) then | |
6097 | Last_Stm := ECA; | |
6098 | ||
6099 | -- Else check statement sequence and else part | |
6100 | ||
6101 | else | |
6102 | Check_Statement_Sequence (Statements (ECA)); | |
6103 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
6104 | return; | |
6105 | end if; | |
6106 | end; | |
6107 | end if; | |
6108 | ||
6109 | -- If we fall through, issue appropriate message | |
6110 | ||
6111 | if Mode = 'F' then | |
7b27e183 AC |
6112 | |
6113 | -- Kill warning if last statement is a raise exception call, | |
6114 | -- or a pragma Assert (False). Note that with assertions enabled, | |
6115 | -- such a pragma has been converted into a raise exception call | |
6116 | -- already, so the Assert_False is for the assertions off case. | |
6117 | ||
6118 | if not Raise_Exception_Call and then not Assert_False then | |
b465ef6f AC |
6119 | |
6120 | -- In GNATprove mode, it is an error to have a missing return | |
6121 | ||
43417b90 | 6122 | Error_Msg_Warn := SPARK_Mode /= On; |
7b27e183 AC |
6123 | |
6124 | -- Issue error message or warning | |
6125 | ||
4a28b181 AC |
6126 | Error_Msg_N |
6127 | ("RETURN statement missing following this statement<<!", | |
6128 | Last_Stm); | |
6129 | Error_Msg_N | |
6130 | ("\Program_Error ]<<!", Last_Stm); | |
996ae0b0 RK |
6131 | end if; |
6132 | ||
6133 | -- Note: we set Err even though we have not issued a warning | |
6134 | -- because we still have a case of a missing return. This is | |
6135 | -- an extremely marginal case, probably will never be noticed | |
6136 | -- but we might as well get it right. | |
6137 | ||
6138 | Err := True; | |
6139 | ||
c8ef728f ES |
6140 | -- Otherwise we have the case of a procedure marked No_Return |
6141 | ||
996ae0b0 | 6142 | else |
800621e0 | 6143 | if not Raise_Exception_Call then |
4a28b181 AC |
6144 | if GNATprove_Mode then |
6145 | Error_Msg_N | |
6146 | ("implied return after this statement " | |
6147 | & "would have raised Program_Error", Last_Stm); | |
6148 | else | |
6149 | Error_Msg_N | |
6150 | ("implied return after this statement " | |
6151 | & "will raise Program_Error??", Last_Stm); | |
6152 | end if; | |
6153 | ||
43417b90 | 6154 | Error_Msg_Warn := SPARK_Mode /= On; |
800621e0 | 6155 | Error_Msg_NE |
4a28b181 | 6156 | ("\procedure & is marked as No_Return<<!", Last_Stm, Proc); |
800621e0 | 6157 | end if; |
c8ef728f ES |
6158 | |
6159 | declare | |
6160 | RE : constant Node_Id := | |
6161 | Make_Raise_Program_Error (Sloc (Last_Stm), | |
6162 | Reason => PE_Implicit_Return); | |
6163 | begin | |
6164 | Insert_After (Last_Stm, RE); | |
6165 | Analyze (RE); | |
6166 | end; | |
996ae0b0 RK |
6167 | end if; |
6168 | end Check_Statement_Sequence; | |
6169 | ||
6170 | -- Start of processing for Check_Returns | |
6171 | ||
6172 | begin | |
6173 | Err := False; | |
6174 | Check_Statement_Sequence (Statements (HSS)); | |
6175 | ||
6176 | if Present (Exception_Handlers (HSS)) then | |
6177 | Handler := First_Non_Pragma (Exception_Handlers (HSS)); | |
6178 | while Present (Handler) loop | |
6179 | Check_Statement_Sequence (Statements (Handler)); | |
6180 | Next_Non_Pragma (Handler); | |
6181 | end loop; | |
6182 | end if; | |
6183 | end Check_Returns; | |
6184 | ||
6185 | ---------------------------- | |
6186 | -- Check_Subprogram_Order -- | |
6187 | ---------------------------- | |
6188 | ||
6189 | procedure Check_Subprogram_Order (N : Node_Id) is | |
6190 | ||
6191 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean; | |
dc36a7e3 RD |
6192 | -- This is used to check if S1 > S2 in the sense required by this test, |
6193 | -- for example nameab < namec, but name2 < name10. | |
996ae0b0 | 6194 | |
82c80734 RD |
6195 | ----------------------------- |
6196 | -- Subprogram_Name_Greater -- | |
6197 | ----------------------------- | |
6198 | ||
996ae0b0 RK |
6199 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean is |
6200 | L1, L2 : Positive; | |
6201 | N1, N2 : Natural; | |
6202 | ||
6203 | begin | |
67336960 AC |
6204 | -- Deal with special case where names are identical except for a |
6205 | -- numerical suffix. These are handled specially, taking the numeric | |
6206 | -- ordering from the suffix into account. | |
996ae0b0 RK |
6207 | |
6208 | L1 := S1'Last; | |
6209 | while S1 (L1) in '0' .. '9' loop | |
6210 | L1 := L1 - 1; | |
6211 | end loop; | |
6212 | ||
6213 | L2 := S2'Last; | |
6214 | while S2 (L2) in '0' .. '9' loop | |
6215 | L2 := L2 - 1; | |
6216 | end loop; | |
6217 | ||
67336960 | 6218 | -- If non-numeric parts non-equal, do straight compare |
996ae0b0 | 6219 | |
67336960 AC |
6220 | if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then |
6221 | return S1 > S2; | |
996ae0b0 RK |
6222 | |
6223 | -- If non-numeric parts equal, compare suffixed numeric parts. Note | |
6224 | -- that a missing suffix is treated as numeric zero in this test. | |
6225 | ||
6226 | else | |
6227 | N1 := 0; | |
6228 | while L1 < S1'Last loop | |
6229 | L1 := L1 + 1; | |
6230 | N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0'); | |
6231 | end loop; | |
6232 | ||
6233 | N2 := 0; | |
6234 | while L2 < S2'Last loop | |
6235 | L2 := L2 + 1; | |
6236 | N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0'); | |
6237 | end loop; | |
6238 | ||
6239 | return N1 > N2; | |
6240 | end if; | |
6241 | end Subprogram_Name_Greater; | |
6242 | ||
6243 | -- Start of processing for Check_Subprogram_Order | |
6244 | ||
6245 | begin | |
6246 | -- Check body in alpha order if this is option | |
6247 | ||
fbf5a39b | 6248 | if Style_Check |
bc202b70 | 6249 | and then Style_Check_Order_Subprograms |
996ae0b0 RK |
6250 | and then Nkind (N) = N_Subprogram_Body |
6251 | and then Comes_From_Source (N) | |
6252 | and then In_Extended_Main_Source_Unit (N) | |
6253 | then | |
6254 | declare | |
6255 | LSN : String_Ptr | |
6256 | renames Scope_Stack.Table | |
6257 | (Scope_Stack.Last).Last_Subprogram_Name; | |
6258 | ||
6259 | Body_Id : constant Entity_Id := | |
6260 | Defining_Entity (Specification (N)); | |
6261 | ||
6262 | begin | |
6263 | Get_Decoded_Name_String (Chars (Body_Id)); | |
6264 | ||
6265 | if LSN /= null then | |
6266 | if Subprogram_Name_Greater | |
6267 | (LSN.all, Name_Buffer (1 .. Name_Len)) | |
6268 | then | |
6269 | Style.Subprogram_Not_In_Alpha_Order (Body_Id); | |
6270 | end if; | |
6271 | ||
6272 | Free (LSN); | |
6273 | end if; | |
6274 | ||
6275 | LSN := new String'(Name_Buffer (1 .. Name_Len)); | |
6276 | end; | |
6277 | end if; | |
6278 | end Check_Subprogram_Order; | |
6279 | ||
6280 | ------------------------------ | |
6281 | -- Check_Subtype_Conformant -- | |
6282 | ------------------------------ | |
6283 | ||
6284 | procedure Check_Subtype_Conformant | |
ce2b6ba5 JM |
6285 | (New_Id : Entity_Id; |
6286 | Old_Id : Entity_Id; | |
6287 | Err_Loc : Node_Id := Empty; | |
f307415a AC |
6288 | Skip_Controlling_Formals : Boolean := False; |
6289 | Get_Inst : Boolean := False) | |
996ae0b0 RK |
6290 | is |
6291 | Result : Boolean; | |
81db9d77 | 6292 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6293 | begin |
6294 | Check_Conformance | |
ce2b6ba5 | 6295 | (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc, |
f307415a AC |
6296 | Skip_Controlling_Formals => Skip_Controlling_Formals, |
6297 | Get_Inst => Get_Inst); | |
996ae0b0 RK |
6298 | end Check_Subtype_Conformant; |
6299 | ||
6300 | --------------------------- | |
6301 | -- Check_Type_Conformant -- | |
6302 | --------------------------- | |
6303 | ||
6304 | procedure Check_Type_Conformant | |
6305 | (New_Id : Entity_Id; | |
6306 | Old_Id : Entity_Id; | |
6307 | Err_Loc : Node_Id := Empty) | |
6308 | is | |
6309 | Result : Boolean; | |
81db9d77 | 6310 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6311 | begin |
6312 | Check_Conformance | |
6313 | (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc); | |
6314 | end Check_Type_Conformant; | |
6315 | ||
806f6d37 AC |
6316 | --------------------------- |
6317 | -- Can_Override_Operator -- | |
6318 | --------------------------- | |
6319 | ||
6320 | function Can_Override_Operator (Subp : Entity_Id) return Boolean is | |
6321 | Typ : Entity_Id; | |
f146302c | 6322 | |
806f6d37 AC |
6323 | begin |
6324 | if Nkind (Subp) /= N_Defining_Operator_Symbol then | |
6325 | return False; | |
6326 | ||
6327 | else | |
6328 | Typ := Base_Type (Etype (First_Formal (Subp))); | |
6329 | ||
f146302c AC |
6330 | -- Check explicitly that the operation is a primitive of the type |
6331 | ||
806f6d37 | 6332 | return Operator_Matches_Spec (Subp, Subp) |
f146302c | 6333 | and then not Is_Generic_Type (Typ) |
806f6d37 AC |
6334 | and then Scope (Subp) = Scope (Typ) |
6335 | and then not Is_Class_Wide_Type (Typ); | |
6336 | end if; | |
6337 | end Can_Override_Operator; | |
6338 | ||
996ae0b0 RK |
6339 | ---------------------- |
6340 | -- Conforming_Types -- | |
6341 | ---------------------- | |
6342 | ||
6343 | function Conforming_Types | |
6344 | (T1 : Entity_Id; | |
6345 | T2 : Entity_Id; | |
6346 | Ctype : Conformance_Type; | |
d05ef0ab | 6347 | Get_Inst : Boolean := False) return Boolean |
996ae0b0 RK |
6348 | is |
6349 | Type_1 : Entity_Id := T1; | |
6350 | Type_2 : Entity_Id := T2; | |
af4b9434 | 6351 | Are_Anonymous_Access_To_Subprogram_Types : Boolean := False; |
996ae0b0 RK |
6352 | |
6353 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean; | |
0a36105d JM |
6354 | -- If neither T1 nor T2 are generic actual types, or if they are in |
6355 | -- different scopes (e.g. parent and child instances), then verify that | |
6356 | -- the base types are equal. Otherwise T1 and T2 must be on the same | |
6357 | -- subtype chain. The whole purpose of this procedure is to prevent | |
6358 | -- spurious ambiguities in an instantiation that may arise if two | |
6359 | -- distinct generic types are instantiated with the same actual. | |
6360 | ||
5d37ba92 ES |
6361 | function Find_Designated_Type (T : Entity_Id) return Entity_Id; |
6362 | -- An access parameter can designate an incomplete type. If the | |
6363 | -- incomplete type is the limited view of a type from a limited_ | |
6364 | -- with_clause, check whether the non-limited view is available. If | |
6365 | -- it is a (non-limited) incomplete type, get the full view. | |
6366 | ||
0a36105d JM |
6367 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean; |
6368 | -- Returns True if and only if either T1 denotes a limited view of T2 | |
6369 | -- or T2 denotes a limited view of T1. This can arise when the limited | |
6370 | -- with view of a type is used in a subprogram declaration and the | |
6371 | -- subprogram body is in the scope of a regular with clause for the | |
6372 | -- same unit. In such a case, the two type entities can be considered | |
6373 | -- identical for purposes of conformance checking. | |
996ae0b0 RK |
6374 | |
6375 | ---------------------- | |
6376 | -- Base_Types_Match -- | |
6377 | ---------------------- | |
6378 | ||
6379 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is | |
8fde064e AC |
6380 | BT1 : constant Entity_Id := Base_Type (T1); |
6381 | BT2 : constant Entity_Id := Base_Type (T2); | |
6382 | ||
996ae0b0 RK |
6383 | begin |
6384 | if T1 = T2 then | |
6385 | return True; | |
6386 | ||
8fde064e | 6387 | elsif BT1 = BT2 then |
996ae0b0 | 6388 | |
0a36105d | 6389 | -- The following is too permissive. A more precise test should |
996ae0b0 RK |
6390 | -- check that the generic actual is an ancestor subtype of the |
6391 | -- other ???. | |
586ecbf3 | 6392 | |
70f4ad20 AC |
6393 | -- See code in Find_Corresponding_Spec that applies an additional |
6394 | -- filter to handle accidental amiguities in instances. | |
996ae0b0 RK |
6395 | |
6396 | return not Is_Generic_Actual_Type (T1) | |
07fc65c4 GB |
6397 | or else not Is_Generic_Actual_Type (T2) |
6398 | or else Scope (T1) /= Scope (T2); | |
996ae0b0 | 6399 | |
8fde064e | 6400 | -- If T2 is a generic actual type it is declared as the subtype of |
2995860f AC |
6401 | -- the actual. If that actual is itself a subtype we need to use its |
6402 | -- own base type to check for compatibility. | |
8fde064e AC |
6403 | |
6404 | elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then | |
6405 | return True; | |
6406 | ||
6407 | elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then | |
6408 | return True; | |
6409 | ||
0a36105d JM |
6410 | else |
6411 | return False; | |
6412 | end if; | |
6413 | end Base_Types_Match; | |
aa720a54 | 6414 | |
5d37ba92 ES |
6415 | -------------------------- |
6416 | -- Find_Designated_Type -- | |
6417 | -------------------------- | |
6418 | ||
6419 | function Find_Designated_Type (T : Entity_Id) return Entity_Id is | |
6420 | Desig : Entity_Id; | |
6421 | ||
6422 | begin | |
6423 | Desig := Directly_Designated_Type (T); | |
6424 | ||
6425 | if Ekind (Desig) = E_Incomplete_Type then | |
6426 | ||
6427 | -- If regular incomplete type, get full view if available | |
6428 | ||
6429 | if Present (Full_View (Desig)) then | |
6430 | Desig := Full_View (Desig); | |
6431 | ||
6432 | -- If limited view of a type, get non-limited view if available, | |
6433 | -- and check again for a regular incomplete type. | |
6434 | ||
6435 | elsif Present (Non_Limited_View (Desig)) then | |
6436 | Desig := Get_Full_View (Non_Limited_View (Desig)); | |
6437 | end if; | |
6438 | end if; | |
6439 | ||
6440 | return Desig; | |
6441 | end Find_Designated_Type; | |
6442 | ||
0a36105d JM |
6443 | ------------------------------- |
6444 | -- Matches_Limited_With_View -- | |
6445 | ------------------------------- | |
6446 | ||
6447 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is | |
6448 | begin | |
6449 | -- In some cases a type imported through a limited_with clause, and | |
6450 | -- its nonlimited view are both visible, for example in an anonymous | |
28fa5430 AC |
6451 | -- access-to-class-wide type in a formal, or when building the body |
6452 | -- for a subprogram renaming after the subprogram has been frozen. | |
6453 | -- In these cases Both entities designate the same type. In addition, | |
6454 | -- if one of them is an actual in an instance, it may be a subtype of | |
6455 | -- the non-limited view of the other. | |
6456 | ||
6457 | if From_Limited_With (T1) | |
6458 | and then (T2 = Available_View (T1) | |
6459 | or else Is_Subtype_Of (T2, Available_View (T1))) | |
6460 | then | |
aa720a54 AC |
6461 | return True; |
6462 | ||
28fa5430 AC |
6463 | elsif From_Limited_With (T2) |
6464 | and then (T1 = Available_View (T2) | |
6465 | or else Is_Subtype_Of (T1, Available_View (T2))) | |
6466 | then | |
41251c60 | 6467 | return True; |
3e24afaa | 6468 | |
7b56a91b AC |
6469 | elsif From_Limited_With (T1) |
6470 | and then From_Limited_With (T2) | |
3e24afaa AC |
6471 | and then Available_View (T1) = Available_View (T2) |
6472 | then | |
6473 | return True; | |
41251c60 | 6474 | |
996ae0b0 RK |
6475 | else |
6476 | return False; | |
6477 | end if; | |
0a36105d | 6478 | end Matches_Limited_With_View; |
996ae0b0 | 6479 | |
ec4867fa | 6480 | -- Start of processing for Conforming_Types |
758c442c | 6481 | |
996ae0b0 | 6482 | begin |
8fde064e AC |
6483 | -- The context is an instance association for a formal access-to- |
6484 | -- subprogram type; the formal parameter types require mapping because | |
6485 | -- they may denote other formal parameters of the generic unit. | |
996ae0b0 RK |
6486 | |
6487 | if Get_Inst then | |
6488 | Type_1 := Get_Instance_Of (T1); | |
6489 | Type_2 := Get_Instance_Of (T2); | |
6490 | end if; | |
6491 | ||
0a36105d JM |
6492 | -- If one of the types is a view of the other introduced by a limited |
6493 | -- with clause, treat these as conforming for all purposes. | |
996ae0b0 | 6494 | |
0a36105d JM |
6495 | if Matches_Limited_With_View (T1, T2) then |
6496 | return True; | |
6497 | ||
6498 | elsif Base_Types_Match (Type_1, Type_2) then | |
996ae0b0 RK |
6499 | return Ctype <= Mode_Conformant |
6500 | or else Subtypes_Statically_Match (Type_1, Type_2); | |
6501 | ||
6502 | elsif Is_Incomplete_Or_Private_Type (Type_1) | |
6503 | and then Present (Full_View (Type_1)) | |
6504 | and then Base_Types_Match (Full_View (Type_1), Type_2) | |
6505 | then | |
6506 | return Ctype <= Mode_Conformant | |
6507 | or else Subtypes_Statically_Match (Full_View (Type_1), Type_2); | |
6508 | ||
6509 | elsif Ekind (Type_2) = E_Incomplete_Type | |
6510 | and then Present (Full_View (Type_2)) | |
6511 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6512 | then | |
6513 | return Ctype <= Mode_Conformant | |
6514 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
fbf5a39b AC |
6515 | |
6516 | elsif Is_Private_Type (Type_2) | |
6517 | and then In_Instance | |
6518 | and then Present (Full_View (Type_2)) | |
6519 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6520 | then | |
6521 | return Ctype <= Mode_Conformant | |
6522 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
167b47d9 | 6523 | |
088c7e1b | 6524 | -- In Ada 2012, incomplete types (including limited views) can appear |
167b47d9 AC |
6525 | -- as actuals in instantiations. |
6526 | ||
6527 | elsif Is_Incomplete_Type (Type_1) | |
6528 | and then Is_Incomplete_Type (Type_2) | |
6529 | and then (Used_As_Generic_Actual (Type_1) | |
6530 | or else Used_As_Generic_Actual (Type_2)) | |
6531 | then | |
6532 | return True; | |
996ae0b0 RK |
6533 | end if; |
6534 | ||
0a36105d | 6535 | -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be |
466c2127 AC |
6536 | -- treated recursively because they carry a signature. As far as |
6537 | -- conformance is concerned, convention plays no role, and either | |
6538 | -- or both could be access to protected subprograms. | |
af4b9434 AC |
6539 | |
6540 | Are_Anonymous_Access_To_Subprogram_Types := | |
466c2127 AC |
6541 | Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type, |
6542 | E_Anonymous_Access_Protected_Subprogram_Type) | |
f937473f | 6543 | and then |
466c2127 AC |
6544 | Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type, |
6545 | E_Anonymous_Access_Protected_Subprogram_Type); | |
af4b9434 | 6546 | |
996ae0b0 | 6547 | -- Test anonymous access type case. For this case, static subtype |
5d37ba92 ES |
6548 | -- matching is required for mode conformance (RM 6.3.1(15)). We check |
6549 | -- the base types because we may have built internal subtype entities | |
6550 | -- to handle null-excluding types (see Process_Formals). | |
996ae0b0 | 6551 | |
5d37ba92 ES |
6552 | if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type |
6553 | and then | |
6554 | Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type) | |
8fde064e AC |
6555 | |
6556 | -- Ada 2005 (AI-254) | |
6557 | ||
6558 | or else Are_Anonymous_Access_To_Subprogram_Types | |
996ae0b0 RK |
6559 | then |
6560 | declare | |
6561 | Desig_1 : Entity_Id; | |
6562 | Desig_2 : Entity_Id; | |
6563 | ||
6564 | begin | |
885c4871 | 6565 | -- In Ada 2005, access constant indicators must match for |
5d37ba92 | 6566 | -- subtype conformance. |
9dcb52e1 | 6567 | |
0791fbe9 | 6568 | if Ada_Version >= Ada_2005 |
5d37ba92 ES |
6569 | and then Ctype >= Subtype_Conformant |
6570 | and then | |
6571 | Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2) | |
6572 | then | |
6573 | return False; | |
996ae0b0 RK |
6574 | end if; |
6575 | ||
5d37ba92 | 6576 | Desig_1 := Find_Designated_Type (Type_1); |
5d37ba92 | 6577 | Desig_2 := Find_Designated_Type (Type_2); |
996ae0b0 | 6578 | |
5d37ba92 | 6579 | -- If the context is an instance association for a formal |
82c80734 RD |
6580 | -- access-to-subprogram type; formal access parameter designated |
6581 | -- types require mapping because they may denote other formal | |
6582 | -- parameters of the generic unit. | |
996ae0b0 RK |
6583 | |
6584 | if Get_Inst then | |
6585 | Desig_1 := Get_Instance_Of (Desig_1); | |
6586 | Desig_2 := Get_Instance_Of (Desig_2); | |
6587 | end if; | |
6588 | ||
82c80734 RD |
6589 | -- It is possible for a Class_Wide_Type to be introduced for an |
6590 | -- incomplete type, in which case there is a separate class_ wide | |
6591 | -- type for the full view. The types conform if their Etypes | |
6592 | -- conform, i.e. one may be the full view of the other. This can | |
6593 | -- only happen in the context of an access parameter, other uses | |
6594 | -- of an incomplete Class_Wide_Type are illegal. | |
996ae0b0 | 6595 | |
fbf5a39b | 6596 | if Is_Class_Wide_Type (Desig_1) |
4adf3c50 AC |
6597 | and then |
6598 | Is_Class_Wide_Type (Desig_2) | |
996ae0b0 RK |
6599 | then |
6600 | return | |
fbf5a39b AC |
6601 | Conforming_Types |
6602 | (Etype (Base_Type (Desig_1)), | |
6603 | Etype (Base_Type (Desig_2)), Ctype); | |
af4b9434 AC |
6604 | |
6605 | elsif Are_Anonymous_Access_To_Subprogram_Types then | |
0791fbe9 | 6606 | if Ada_Version < Ada_2005 then |
758c442c GD |
6607 | return Ctype = Type_Conformant |
6608 | or else | |
af4b9434 AC |
6609 | Subtypes_Statically_Match (Desig_1, Desig_2); |
6610 | ||
758c442c GD |
6611 | -- We must check the conformance of the signatures themselves |
6612 | ||
6613 | else | |
6614 | declare | |
6615 | Conformant : Boolean; | |
6616 | begin | |
6617 | Check_Conformance | |
6618 | (Desig_1, Desig_2, Ctype, False, Conformant); | |
6619 | return Conformant; | |
6620 | end; | |
6621 | end if; | |
6622 | ||
167b47d9 AC |
6623 | -- A limited view of an actual matches the corresponding |
6624 | -- incomplete formal. | |
6625 | ||
6626 | elsif Ekind (Desig_2) = E_Incomplete_Subtype | |
6627 | and then From_Limited_With (Desig_2) | |
6628 | and then Used_As_Generic_Actual (Etype (Desig_2)) | |
6629 | then | |
6630 | return True; | |
6631 | ||
996ae0b0 RK |
6632 | else |
6633 | return Base_Type (Desig_1) = Base_Type (Desig_2) | |
6634 | and then (Ctype = Type_Conformant | |
8fde064e AC |
6635 | or else |
6636 | Subtypes_Statically_Match (Desig_1, Desig_2)); | |
996ae0b0 RK |
6637 | end if; |
6638 | end; | |
6639 | ||
6640 | -- Otherwise definitely no match | |
6641 | ||
6642 | else | |
c8ef728f ES |
6643 | if ((Ekind (Type_1) = E_Anonymous_Access_Type |
6644 | and then Is_Access_Type (Type_2)) | |
6645 | or else (Ekind (Type_2) = E_Anonymous_Access_Type | |
8fde064e | 6646 | and then Is_Access_Type (Type_1))) |
c8ef728f ES |
6647 | and then |
6648 | Conforming_Types | |
6649 | (Designated_Type (Type_1), Designated_Type (Type_2), Ctype) | |
6650 | then | |
6651 | May_Hide_Profile := True; | |
6652 | end if; | |
6653 | ||
996ae0b0 RK |
6654 | return False; |
6655 | end if; | |
996ae0b0 RK |
6656 | end Conforming_Types; |
6657 | ||
6658 | -------------------------- | |
6659 | -- Create_Extra_Formals -- | |
6660 | -------------------------- | |
6661 | ||
6662 | procedure Create_Extra_Formals (E : Entity_Id) is | |
6663 | Formal : Entity_Id; | |
ec4867fa | 6664 | First_Extra : Entity_Id := Empty; |
996ae0b0 RK |
6665 | Last_Extra : Entity_Id; |
6666 | Formal_Type : Entity_Id; | |
6667 | P_Formal : Entity_Id := Empty; | |
6668 | ||
ec4867fa ES |
6669 | function Add_Extra_Formal |
6670 | (Assoc_Entity : Entity_Id; | |
6671 | Typ : Entity_Id; | |
6672 | Scope : Entity_Id; | |
6673 | Suffix : String) return Entity_Id; | |
6674 | -- Add an extra formal to the current list of formals and extra formals. | |
6675 | -- The extra formal is added to the end of the list of extra formals, | |
6676 | -- and also returned as the result. These formals are always of mode IN. | |
6677 | -- The new formal has the type Typ, is declared in Scope, and its name | |
6678 | -- is given by a concatenation of the name of Assoc_Entity and Suffix. | |
cd5a9750 AC |
6679 | -- The following suffixes are currently used. They should not be changed |
6680 | -- without coordinating with CodePeer, which makes use of these to | |
6681 | -- provide better messages. | |
6682 | ||
d92eccc3 AC |
6683 | -- O denotes the Constrained bit. |
6684 | -- L denotes the accessibility level. | |
cd5a9750 AC |
6685 | -- BIP_xxx denotes an extra formal for a build-in-place function. See |
6686 | -- the full list in exp_ch6.BIP_Formal_Kind. | |
996ae0b0 | 6687 | |
fbf5a39b AC |
6688 | ---------------------- |
6689 | -- Add_Extra_Formal -- | |
6690 | ---------------------- | |
6691 | ||
ec4867fa ES |
6692 | function Add_Extra_Formal |
6693 | (Assoc_Entity : Entity_Id; | |
6694 | Typ : Entity_Id; | |
6695 | Scope : Entity_Id; | |
6696 | Suffix : String) return Entity_Id | |
6697 | is | |
996ae0b0 | 6698 | EF : constant Entity_Id := |
ec4867fa ES |
6699 | Make_Defining_Identifier (Sloc (Assoc_Entity), |
6700 | Chars => New_External_Name (Chars (Assoc_Entity), | |
f937473f | 6701 | Suffix => Suffix)); |
996ae0b0 RK |
6702 | |
6703 | begin | |
82c80734 RD |
6704 | -- A little optimization. Never generate an extra formal for the |
6705 | -- _init operand of an initialization procedure, since it could | |
6706 | -- never be used. | |
996ae0b0 RK |
6707 | |
6708 | if Chars (Formal) = Name_uInit then | |
6709 | return Empty; | |
6710 | end if; | |
6711 | ||
6712 | Set_Ekind (EF, E_In_Parameter); | |
6713 | Set_Actual_Subtype (EF, Typ); | |
6714 | Set_Etype (EF, Typ); | |
ec4867fa | 6715 | Set_Scope (EF, Scope); |
996ae0b0 RK |
6716 | Set_Mechanism (EF, Default_Mechanism); |
6717 | Set_Formal_Validity (EF); | |
6718 | ||
ec4867fa ES |
6719 | if No (First_Extra) then |
6720 | First_Extra := EF; | |
6721 | Set_Extra_Formals (Scope, First_Extra); | |
6722 | end if; | |
6723 | ||
6724 | if Present (Last_Extra) then | |
6725 | Set_Extra_Formal (Last_Extra, EF); | |
6726 | end if; | |
6727 | ||
996ae0b0 | 6728 | Last_Extra := EF; |
ec4867fa | 6729 | |
996ae0b0 RK |
6730 | return EF; |
6731 | end Add_Extra_Formal; | |
6732 | ||
6733 | -- Start of processing for Create_Extra_Formals | |
6734 | ||
6735 | begin | |
8fde064e AC |
6736 | -- We never generate extra formals if expansion is not active because we |
6737 | -- don't need them unless we are generating code. | |
f937473f RD |
6738 | |
6739 | if not Expander_Active then | |
6740 | return; | |
6741 | end if; | |
6742 | ||
e2441021 AC |
6743 | -- No need to generate extra formals in interface thunks whose target |
6744 | -- primitive has no extra formals. | |
6745 | ||
6746 | if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then | |
6747 | return; | |
6748 | end if; | |
6749 | ||
82c80734 | 6750 | -- If this is a derived subprogram then the subtypes of the parent |
16b05213 | 6751 | -- subprogram's formal parameters will be used to determine the need |
82c80734 | 6752 | -- for extra formals. |
996ae0b0 RK |
6753 | |
6754 | if Is_Overloadable (E) and then Present (Alias (E)) then | |
6755 | P_Formal := First_Formal (Alias (E)); | |
6756 | end if; | |
6757 | ||
6758 | Last_Extra := Empty; | |
6759 | Formal := First_Formal (E); | |
6760 | while Present (Formal) loop | |
6761 | Last_Extra := Formal; | |
6762 | Next_Formal (Formal); | |
6763 | end loop; | |
6764 | ||
f937473f | 6765 | -- If Extra_formals were already created, don't do it again. This |
82c80734 RD |
6766 | -- situation may arise for subprogram types created as part of |
6767 | -- dispatching calls (see Expand_Dispatching_Call) | |
996ae0b0 | 6768 | |
8fde064e | 6769 | if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then |
996ae0b0 RK |
6770 | return; |
6771 | end if; | |
6772 | ||
19590d70 GD |
6773 | -- If the subprogram is a predefined dispatching subprogram then don't |
6774 | -- generate any extra constrained or accessibility level formals. In | |
6775 | -- general we suppress these for internal subprograms (by not calling | |
6776 | -- Freeze_Subprogram and Create_Extra_Formals at all), but internally | |
6777 | -- generated stream attributes do get passed through because extra | |
6778 | -- build-in-place formals are needed in some cases (limited 'Input). | |
6779 | ||
bac7206d | 6780 | if Is_Predefined_Internal_Operation (E) then |
63585f75 | 6781 | goto Test_For_Func_Result_Extras; |
19590d70 GD |
6782 | end if; |
6783 | ||
996ae0b0 | 6784 | Formal := First_Formal (E); |
996ae0b0 RK |
6785 | while Present (Formal) loop |
6786 | ||
6787 | -- Create extra formal for supporting the attribute 'Constrained. | |
6788 | -- The case of a private type view without discriminants also | |
6789 | -- requires the extra formal if the underlying type has defaulted | |
6790 | -- discriminants. | |
6791 | ||
6792 | if Ekind (Formal) /= E_In_Parameter then | |
6793 | if Present (P_Formal) then | |
6794 | Formal_Type := Etype (P_Formal); | |
6795 | else | |
6796 | Formal_Type := Etype (Formal); | |
6797 | end if; | |
6798 | ||
5d09245e AC |
6799 | -- Do not produce extra formals for Unchecked_Union parameters. |
6800 | -- Jump directly to the end of the loop. | |
6801 | ||
6802 | if Is_Unchecked_Union (Base_Type (Formal_Type)) then | |
6803 | goto Skip_Extra_Formal_Generation; | |
6804 | end if; | |
6805 | ||
996ae0b0 RK |
6806 | if not Has_Discriminants (Formal_Type) |
6807 | and then Ekind (Formal_Type) in Private_Kind | |
6808 | and then Present (Underlying_Type (Formal_Type)) | |
6809 | then | |
6810 | Formal_Type := Underlying_Type (Formal_Type); | |
6811 | end if; | |
6812 | ||
5e5db3b4 GD |
6813 | -- Suppress the extra formal if formal's subtype is constrained or |
6814 | -- indefinite, or we're compiling for Ada 2012 and the underlying | |
6815 | -- type is tagged and limited. In Ada 2012, a limited tagged type | |
6816 | -- can have defaulted discriminants, but 'Constrained is required | |
6817 | -- to return True, so the formal is never needed (see AI05-0214). | |
6818 | -- Note that this ensures consistency of calling sequences for | |
6819 | -- dispatching operations when some types in a class have defaults | |
6820 | -- on discriminants and others do not (and requiring the extra | |
6821 | -- formal would introduce distributed overhead). | |
6822 | ||
b5bdffcc AC |
6823 | -- If the type does not have a completion yet, treat as prior to |
6824 | -- Ada 2012 for consistency. | |
6825 | ||
996ae0b0 | 6826 | if Has_Discriminants (Formal_Type) |
f937473f RD |
6827 | and then not Is_Constrained (Formal_Type) |
6828 | and then not Is_Indefinite_Subtype (Formal_Type) | |
5e5db3b4 | 6829 | and then (Ada_Version < Ada_2012 |
b5bdffcc AC |
6830 | or else No (Underlying_Type (Formal_Type)) |
6831 | or else not | |
6832 | (Is_Limited_Type (Formal_Type) | |
6833 | and then | |
6834 | (Is_Tagged_Type | |
6835 | (Underlying_Type (Formal_Type))))) | |
996ae0b0 RK |
6836 | then |
6837 | Set_Extra_Constrained | |
d92eccc3 | 6838 | (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O")); |
996ae0b0 RK |
6839 | end if; |
6840 | end if; | |
6841 | ||
0a36105d JM |
6842 | -- Create extra formal for supporting accessibility checking. This |
6843 | -- is done for both anonymous access formals and formals of named | |
6844 | -- access types that are marked as controlling formals. The latter | |
6845 | -- case can occur when Expand_Dispatching_Call creates a subprogram | |
6846 | -- type and substitutes the types of access-to-class-wide actuals | |
6847 | -- for the anonymous access-to-specific-type of controlling formals. | |
5d37ba92 ES |
6848 | -- Base_Type is applied because in cases where there is a null |
6849 | -- exclusion the formal may have an access subtype. | |
996ae0b0 RK |
6850 | |
6851 | -- This is suppressed if we specifically suppress accessibility | |
f937473f | 6852 | -- checks at the package level for either the subprogram, or the |
fbf5a39b AC |
6853 | -- package in which it resides. However, we do not suppress it |
6854 | -- simply if the scope has accessibility checks suppressed, since | |
6855 | -- this could cause trouble when clients are compiled with a | |
6856 | -- different suppression setting. The explicit checks at the | |
6857 | -- package level are safe from this point of view. | |
996ae0b0 | 6858 | |
5d37ba92 | 6859 | if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type |
0a36105d | 6860 | or else (Is_Controlling_Formal (Formal) |
5d37ba92 | 6861 | and then Is_Access_Type (Base_Type (Etype (Formal))))) |
996ae0b0 | 6862 | and then not |
fbf5a39b | 6863 | (Explicit_Suppress (E, Accessibility_Check) |
996ae0b0 | 6864 | or else |
fbf5a39b | 6865 | Explicit_Suppress (Scope (E), Accessibility_Check)) |
996ae0b0 | 6866 | and then |
c8ef728f | 6867 | (No (P_Formal) |
996ae0b0 RK |
6868 | or else Present (Extra_Accessibility (P_Formal))) |
6869 | then | |
811c6a85 | 6870 | Set_Extra_Accessibility |
d92eccc3 | 6871 | (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L")); |
996ae0b0 RK |
6872 | end if; |
6873 | ||
5d09245e AC |
6874 | -- This label is required when skipping extra formal generation for |
6875 | -- Unchecked_Union parameters. | |
6876 | ||
6877 | <<Skip_Extra_Formal_Generation>> | |
6878 | ||
f937473f RD |
6879 | if Present (P_Formal) then |
6880 | Next_Formal (P_Formal); | |
6881 | end if; | |
6882 | ||
996ae0b0 RK |
6883 | Next_Formal (Formal); |
6884 | end loop; | |
ec4867fa | 6885 | |
63585f75 SB |
6886 | <<Test_For_Func_Result_Extras>> |
6887 | ||
6888 | -- Ada 2012 (AI05-234): "the accessibility level of the result of a | |
6889 | -- function call is ... determined by the point of call ...". | |
6890 | ||
6891 | if Needs_Result_Accessibility_Level (E) then | |
6892 | Set_Extra_Accessibility_Of_Result | |
6893 | (E, Add_Extra_Formal (E, Standard_Natural, E, "L")); | |
6894 | end if; | |
19590d70 | 6895 | |
ec4867fa | 6896 | -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add |
f937473f RD |
6897 | -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind. |
6898 | ||
0791fbe9 | 6899 | if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then |
ec4867fa | 6900 | declare |
f937473f | 6901 | Result_Subt : constant Entity_Id := Etype (E); |
1a36a0cd | 6902 | Full_Subt : constant Entity_Id := Available_View (Result_Subt); |
2fcc44fa | 6903 | Formal_Typ : Entity_Id; |
f937473f | 6904 | |
2fcc44fa | 6905 | Discard : Entity_Id; |
f937473f | 6906 | pragma Warnings (Off, Discard); |
ec4867fa ES |
6907 | |
6908 | begin | |
f937473f | 6909 | -- In the case of functions with unconstrained result subtypes, |
9a1bc6d5 AC |
6910 | -- add a 4-state formal indicating whether the return object is |
6911 | -- allocated by the caller (1), or should be allocated by the | |
6912 | -- callee on the secondary stack (2), in the global heap (3), or | |
6913 | -- in a user-defined storage pool (4). For the moment we just use | |
6914 | -- Natural for the type of this formal. Note that this formal | |
6915 | -- isn't usually needed in the case where the result subtype is | |
6916 | -- constrained, but it is needed when the function has a tagged | |
6917 | -- result, because generally such functions can be called in a | |
6918 | -- dispatching context and such calls must be handled like calls | |
6919 | -- to a class-wide function. | |
0a36105d | 6920 | |
1bb6e262 | 6921 | if Needs_BIP_Alloc_Form (E) then |
f937473f RD |
6922 | Discard := |
6923 | Add_Extra_Formal | |
6924 | (E, Standard_Natural, | |
6925 | E, BIP_Formal_Suffix (BIP_Alloc_Form)); | |
200b7162 | 6926 | |
8417f4b2 | 6927 | -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to |
3e452820 AC |
6928 | -- use a user-defined pool. This formal is not added on |
6929 | -- .NET/JVM/ZFP as those targets do not support pools. | |
200b7162 | 6930 | |
ea10ca9c AC |
6931 | if VM_Target = No_VM |
6932 | and then RTE_Available (RE_Root_Storage_Pool_Ptr) | |
3e452820 | 6933 | then |
8417f4b2 AC |
6934 | Discard := |
6935 | Add_Extra_Formal | |
6936 | (E, RTE (RE_Root_Storage_Pool_Ptr), | |
6937 | E, BIP_Formal_Suffix (BIP_Storage_Pool)); | |
6938 | end if; | |
f937473f | 6939 | end if; |
ec4867fa | 6940 | |
df3e68b1 | 6941 | -- In the case of functions whose result type needs finalization, |
ca5af305 | 6942 | -- add an extra formal which represents the finalization master. |
df3e68b1 | 6943 | |
ca5af305 | 6944 | if Needs_BIP_Finalization_Master (E) then |
f937473f RD |
6945 | Discard := |
6946 | Add_Extra_Formal | |
ca5af305 AC |
6947 | (E, RTE (RE_Finalization_Master_Ptr), |
6948 | E, BIP_Formal_Suffix (BIP_Finalization_Master)); | |
f937473f RD |
6949 | end if; |
6950 | ||
94bbf008 AC |
6951 | -- When the result type contains tasks, add two extra formals: the |
6952 | -- master of the tasks to be created, and the caller's activation | |
6953 | -- chain. | |
f937473f | 6954 | |
1a36a0cd | 6955 | if Has_Task (Full_Subt) then |
f937473f RD |
6956 | Discard := |
6957 | Add_Extra_Formal | |
6958 | (E, RTE (RE_Master_Id), | |
af89615f | 6959 | E, BIP_Formal_Suffix (BIP_Task_Master)); |
f937473f RD |
6960 | Discard := |
6961 | Add_Extra_Formal | |
6962 | (E, RTE (RE_Activation_Chain_Access), | |
6963 | E, BIP_Formal_Suffix (BIP_Activation_Chain)); | |
6964 | end if; | |
ec4867fa | 6965 | |
f937473f RD |
6966 | -- All build-in-place functions get an extra formal that will be |
6967 | -- passed the address of the return object within the caller. | |
ec4867fa | 6968 | |
1a36a0cd AC |
6969 | Formal_Typ := |
6970 | Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E)); | |
ec4867fa | 6971 | |
1a36a0cd AC |
6972 | Set_Directly_Designated_Type (Formal_Typ, Result_Subt); |
6973 | Set_Etype (Formal_Typ, Formal_Typ); | |
6974 | Set_Depends_On_Private | |
6975 | (Formal_Typ, Has_Private_Component (Formal_Typ)); | |
6976 | Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ))); | |
6977 | Set_Is_Access_Constant (Formal_Typ, False); | |
ec4867fa | 6978 | |
1a36a0cd AC |
6979 | -- Ada 2005 (AI-50217): Propagate the attribute that indicates |
6980 | -- the designated type comes from the limited view (for back-end | |
6981 | -- purposes). | |
ec4867fa | 6982 | |
7b56a91b AC |
6983 | Set_From_Limited_With |
6984 | (Formal_Typ, From_Limited_With (Result_Subt)); | |
f937473f | 6985 | |
1a36a0cd AC |
6986 | Layout_Type (Formal_Typ); |
6987 | ||
6988 | Discard := | |
6989 | Add_Extra_Formal | |
6990 | (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access)); | |
ec4867fa ES |
6991 | end; |
6992 | end if; | |
996ae0b0 RK |
6993 | end Create_Extra_Formals; |
6994 | ||
6995 | ----------------------------- | |
6996 | -- Enter_Overloaded_Entity -- | |
6997 | ----------------------------- | |
6998 | ||
6999 | procedure Enter_Overloaded_Entity (S : Entity_Id) is | |
7000 | E : Entity_Id := Current_Entity_In_Scope (S); | |
7001 | C_E : Entity_Id := Current_Entity (S); | |
7002 | ||
7003 | begin | |
7004 | if Present (E) then | |
7005 | Set_Has_Homonym (E); | |
7006 | Set_Has_Homonym (S); | |
7007 | end if; | |
7008 | ||
7009 | Set_Is_Immediately_Visible (S); | |
7010 | Set_Scope (S, Current_Scope); | |
7011 | ||
7012 | -- Chain new entity if front of homonym in current scope, so that | |
7013 | -- homonyms are contiguous. | |
7014 | ||
8fde064e | 7015 | if Present (E) and then E /= C_E then |
996ae0b0 RK |
7016 | while Homonym (C_E) /= E loop |
7017 | C_E := Homonym (C_E); | |
7018 | end loop; | |
7019 | ||
7020 | Set_Homonym (C_E, S); | |
7021 | ||
7022 | else | |
7023 | E := C_E; | |
7024 | Set_Current_Entity (S); | |
7025 | end if; | |
7026 | ||
7027 | Set_Homonym (S, E); | |
7028 | ||
2352eadb AC |
7029 | if Is_Inherited_Operation (S) then |
7030 | Append_Inherited_Subprogram (S); | |
7031 | else | |
7032 | Append_Entity (S, Current_Scope); | |
7033 | end if; | |
7034 | ||
996ae0b0 RK |
7035 | Set_Public_Status (S); |
7036 | ||
7037 | if Debug_Flag_E then | |
7038 | Write_Str ("New overloaded entity chain: "); | |
7039 | Write_Name (Chars (S)); | |
996ae0b0 | 7040 | |
82c80734 | 7041 | E := S; |
996ae0b0 RK |
7042 | while Present (E) loop |
7043 | Write_Str (" "); Write_Int (Int (E)); | |
7044 | E := Homonym (E); | |
7045 | end loop; | |
7046 | ||
7047 | Write_Eol; | |
7048 | end if; | |
7049 | ||
7050 | -- Generate warning for hiding | |
7051 | ||
7052 | if Warn_On_Hiding | |
7053 | and then Comes_From_Source (S) | |
7054 | and then In_Extended_Main_Source_Unit (S) | |
7055 | then | |
7056 | E := S; | |
7057 | loop | |
7058 | E := Homonym (E); | |
7059 | exit when No (E); | |
7060 | ||
7fc53871 AC |
7061 | -- Warn unless genuine overloading. Do not emit warning on |
7062 | -- hiding predefined operators in Standard (these are either an | |
7063 | -- (artifact of our implicit declarations, or simple noise) but | |
7064 | -- keep warning on a operator defined on a local subtype, because | |
7065 | -- of the real danger that different operators may be applied in | |
7066 | -- various parts of the program. | |
996ae0b0 | 7067 | |
1f250383 AC |
7068 | -- Note that if E and S have the same scope, there is never any |
7069 | -- hiding. Either the two conflict, and the program is illegal, | |
7070 | -- or S is overriding an implicit inherited subprogram. | |
7071 | ||
7072 | if Scope (E) /= Scope (S) | |
7073 | and then (not Is_Overloadable (E) | |
8d606a78 | 7074 | or else Subtype_Conformant (E, S)) |
f937473f RD |
7075 | and then (Is_Immediately_Visible (E) |
7076 | or else | |
7077 | Is_Potentially_Use_Visible (S)) | |
996ae0b0 | 7078 | then |
7fc53871 AC |
7079 | if Scope (E) /= Standard_Standard then |
7080 | Error_Msg_Sloc := Sloc (E); | |
3ccedacc | 7081 | Error_Msg_N ("declaration of & hides one #?h?", S); |
7fc53871 AC |
7082 | |
7083 | elsif Nkind (S) = N_Defining_Operator_Symbol | |
7084 | and then | |
1f250383 | 7085 | Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S) |
7fc53871 AC |
7086 | then |
7087 | Error_Msg_N | |
dbfeb4fa | 7088 | ("declaration of & hides predefined operator?h?", S); |
7fc53871 | 7089 | end if; |
996ae0b0 RK |
7090 | end if; |
7091 | end loop; | |
7092 | end if; | |
7093 | end Enter_Overloaded_Entity; | |
7094 | ||
e5a58fac AC |
7095 | ----------------------------- |
7096 | -- Check_Untagged_Equality -- | |
7097 | ----------------------------- | |
7098 | ||
7099 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is | |
7100 | Typ : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
7101 | Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op); | |
7102 | Obj_Decl : Node_Id; | |
7103 | ||
7104 | begin | |
7c0c194b AC |
7105 | -- This check applies only if we have a subprogram declaration with an |
7106 | -- untagged record type. | |
b2834fbd AC |
7107 | |
7108 | if Nkind (Decl) /= N_Subprogram_Declaration | |
7109 | or else not Is_Record_Type (Typ) | |
7110 | or else Is_Tagged_Type (Typ) | |
e5a58fac | 7111 | then |
b2834fbd AC |
7112 | return; |
7113 | end if; | |
e5a58fac | 7114 | |
b2834fbd AC |
7115 | -- In Ada 2012 case, we will output errors or warnings depending on |
7116 | -- the setting of debug flag -gnatd.E. | |
7117 | ||
7118 | if Ada_Version >= Ada_2012 then | |
7119 | Error_Msg_Warn := Debug_Flag_Dot_EE; | |
7120 | ||
7121 | -- In earlier versions of Ada, nothing to do unless we are warning on | |
7122 | -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set). | |
7123 | ||
7124 | else | |
7125 | if not Warn_On_Ada_2012_Compatibility then | |
7126 | return; | |
7127 | end if; | |
7128 | end if; | |
7129 | ||
7130 | -- Cases where the type has already been frozen | |
e5a58fac | 7131 | |
b2834fbd AC |
7132 | if Is_Frozen (Typ) then |
7133 | ||
7134 | -- If the type is not declared in a package, or if we are in the body | |
7135 | -- of the package or in some other scope, the new operation is not | |
7136 | -- primitive, and therefore legal, though suspicious. Should we | |
7137 | -- generate a warning in this case ??? | |
7138 | ||
7139 | if Ekind (Scope (Typ)) /= E_Package | |
7140 | or else Scope (Typ) /= Current_Scope | |
7141 | then | |
7142 | return; | |
7143 | ||
7144 | -- If the type is a generic actual (sub)type, the operation is not | |
7145 | -- primitive either because the base type is declared elsewhere. | |
7146 | ||
7147 | elsif Is_Generic_Actual_Type (Typ) then | |
7148 | return; | |
7149 | ||
7150 | -- Here we have a definite error of declaration after freezing | |
7151 | ||
7152 | else | |
7153 | if Ada_Version >= Ada_2012 then | |
ae6ede77 | 7154 | Error_Msg_NE |
3ccedacc | 7155 | ("equality operator must be declared before type & is " |
b2834fbd AC |
7156 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ); |
7157 | ||
7158 | -- In Ada 2012 mode with error turned to warning, output one | |
7159 | -- more warning to warn that the equality operation may not | |
7160 | -- compose. This is the consequence of ignoring the error. | |
7161 | ||
7162 | if Error_Msg_Warn then | |
7163 | Error_Msg_N ("\equality operation may not compose??", Eq_Op); | |
7164 | end if; | |
21a5b575 AC |
7165 | |
7166 | else | |
7167 | Error_Msg_NE | |
b2834fbd AC |
7168 | ("equality operator must be declared before type& is " |
7169 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ); | |
7170 | end if; | |
7171 | ||
7172 | -- If we are in the package body, we could just move the | |
7173 | -- declaration to the package spec, so add a message saying that. | |
7174 | ||
7175 | if In_Package_Body (Scope (Typ)) then | |
7176 | if Ada_Version >= Ada_2012 then | |
7177 | Error_Msg_N | |
7178 | ("\move declaration to package spec<<", Eq_Op); | |
7179 | else | |
7180 | Error_Msg_N | |
7181 | ("\move declaration to package spec (Ada 2012)?y?", Eq_Op); | |
7182 | end if; | |
21a5b575 | 7183 | |
b2834fbd AC |
7184 | -- Otherwise try to find the freezing point |
7185 | ||
7186 | else | |
21a5b575 | 7187 | Obj_Decl := Next (Parent (Typ)); |
dbfeb4fa | 7188 | while Present (Obj_Decl) and then Obj_Decl /= Decl loop |
21a5b575 AC |
7189 | if Nkind (Obj_Decl) = N_Object_Declaration |
7190 | and then Etype (Defining_Identifier (Obj_Decl)) = Typ | |
7191 | then | |
b2834fbd AC |
7192 | -- Freezing point, output warnings |
7193 | ||
7194 | if Ada_Version >= Ada_2012 then | |
7195 | Error_Msg_NE | |
7196 | ("type& is frozen by declaration??", Obj_Decl, Typ); | |
7197 | Error_Msg_N | |
7198 | ("\an equality operator cannot be declared after " | |
7199 | & "this point??", | |
7200 | Obj_Decl); | |
7201 | else | |
7202 | Error_Msg_NE | |
7203 | ("type& is frozen by declaration (Ada 2012)?y?", | |
7204 | Obj_Decl, Typ); | |
7205 | Error_Msg_N | |
7206 | ("\an equality operator cannot be declared after " | |
7207 | & "this point (Ada 2012)?y?", | |
7208 | Obj_Decl); | |
7209 | end if; | |
7210 | ||
21a5b575 AC |
7211 | exit; |
7212 | end if; | |
7213 | ||
7214 | Next (Obj_Decl); | |
7215 | end loop; | |
7216 | end if; | |
b2834fbd | 7217 | end if; |
e5a58fac | 7218 | |
b2834fbd AC |
7219 | -- Here if type is not frozen yet. It is illegal to have a primitive |
7220 | -- equality declared in the private part if the type is visible. | |
21a5b575 | 7221 | |
b2834fbd AC |
7222 | elsif not In_Same_List (Parent (Typ), Decl) |
7223 | and then not Is_Limited_Type (Typ) | |
7224 | then | |
7225 | -- Shouldn't we give an RM reference here??? | |
21a5b575 | 7226 | |
b2834fbd AC |
7227 | if Ada_Version >= Ada_2012 then |
7228 | Error_Msg_N | |
7229 | ("equality operator appears too late<<", Eq_Op); | |
7230 | else | |
7231 | Error_Msg_N | |
7232 | ("equality operator appears too late (Ada 2012)?y?", Eq_Op); | |
e5a58fac | 7233 | end if; |
b2834fbd AC |
7234 | |
7235 | -- No error detected | |
7236 | ||
7237 | else | |
7238 | return; | |
e5a58fac AC |
7239 | end if; |
7240 | end Check_Untagged_Equality; | |
7241 | ||
996ae0b0 RK |
7242 | ----------------------------- |
7243 | -- Find_Corresponding_Spec -- | |
7244 | ----------------------------- | |
7245 | ||
d44202ba HK |
7246 | function Find_Corresponding_Spec |
7247 | (N : Node_Id; | |
7248 | Post_Error : Boolean := True) return Entity_Id | |
7249 | is | |
996ae0b0 RK |
7250 | Spec : constant Node_Id := Specification (N); |
7251 | Designator : constant Entity_Id := Defining_Entity (Spec); | |
7252 | ||
7253 | E : Entity_Id; | |
7254 | ||
70f4ad20 AC |
7255 | function Different_Generic_Profile (E : Entity_Id) return Boolean; |
7256 | -- Even if fully conformant, a body may depend on a generic actual when | |
7257 | -- the spec does not, or vice versa, in which case they were distinct | |
7258 | -- entities in the generic. | |
7259 | ||
7260 | ------------------------------- | |
7261 | -- Different_Generic_Profile -- | |
7262 | ------------------------------- | |
7263 | ||
7264 | function Different_Generic_Profile (E : Entity_Id) return Boolean is | |
7265 | F1, F2 : Entity_Id; | |
7266 | ||
2995860f AC |
7267 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean; |
7268 | -- Check that the types of corresponding formals have the same | |
7269 | -- generic actual if any. We have to account for subtypes of a | |
7270 | -- generic formal, declared between a spec and a body, which may | |
73999267 AC |
7271 | -- appear distinct in an instance but matched in the generic, and |
7272 | -- the subtype may be used either in the spec or the body of the | |
7273 | -- subprogram being checked. | |
2995860f AC |
7274 | |
7275 | ------------------------- | |
7276 | -- Same_Generic_Actual -- | |
7277 | ------------------------- | |
7278 | ||
7279 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is | |
73999267 AC |
7280 | |
7281 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean; | |
7282 | -- Predicate to check whether S1 is a subtype of S2 in the source | |
7283 | -- of the instance. | |
7284 | ||
7285 | ------------------------- | |
7286 | -- Is_Declared_Subtype -- | |
7287 | ------------------------- | |
7288 | ||
7289 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is | |
7290 | begin | |
7291 | return Comes_From_Source (Parent (S1)) | |
7292 | and then Nkind (Parent (S1)) = N_Subtype_Declaration | |
7293 | and then Is_Entity_Name (Subtype_Indication (Parent (S1))) | |
7294 | and then Entity (Subtype_Indication (Parent (S1))) = S2; | |
7295 | end Is_Declared_Subtype; | |
7296 | ||
7297 | -- Start of processing for Same_Generic_Actual | |
7298 | ||
2995860f AC |
7299 | begin |
7300 | return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2) | |
73999267 AC |
7301 | or else Is_Declared_Subtype (T1, T2) |
7302 | or else Is_Declared_Subtype (T2, T1); | |
2995860f AC |
7303 | end Same_Generic_Actual; |
7304 | ||
7305 | -- Start of processing for Different_Generic_Profile | |
7306 | ||
70f4ad20 | 7307 | begin |
2995860f AC |
7308 | if not In_Instance then |
7309 | return False; | |
7310 | ||
7311 | elsif Ekind (E) = E_Function | |
7312 | and then not Same_Generic_Actual (Etype (E), Etype (Designator)) | |
70f4ad20 AC |
7313 | then |
7314 | return True; | |
7315 | end if; | |
7316 | ||
7317 | F1 := First_Formal (Designator); | |
7318 | F2 := First_Formal (E); | |
70f4ad20 | 7319 | while Present (F1) loop |
2995860f | 7320 | if not Same_Generic_Actual (Etype (F1), Etype (F2)) then |
70f4ad20 AC |
7321 | return True; |
7322 | end if; | |
7323 | ||
7324 | Next_Formal (F1); | |
7325 | Next_Formal (F2); | |
7326 | end loop; | |
7327 | ||
7328 | return False; | |
7329 | end Different_Generic_Profile; | |
7330 | ||
7331 | -- Start of processing for Find_Corresponding_Spec | |
7332 | ||
996ae0b0 RK |
7333 | begin |
7334 | E := Current_Entity (Designator); | |
996ae0b0 RK |
7335 | while Present (E) loop |
7336 | ||
7337 | -- We are looking for a matching spec. It must have the same scope, | |
7338 | -- and the same name, and either be type conformant, or be the case | |
7339 | -- of a library procedure spec and its body (which belong to one | |
7340 | -- another regardless of whether they are type conformant or not). | |
7341 | ||
7342 | if Scope (E) = Current_Scope then | |
fbf5a39b AC |
7343 | if Current_Scope = Standard_Standard |
7344 | or else (Ekind (E) = Ekind (Designator) | |
586ecbf3 | 7345 | and then Type_Conformant (E, Designator)) |
996ae0b0 RK |
7346 | then |
7347 | -- Within an instantiation, we know that spec and body are | |
70f4ad20 AC |
7348 | -- subtype conformant, because they were subtype conformant in |
7349 | -- the generic. We choose the subtype-conformant entity here as | |
7350 | -- well, to resolve spurious ambiguities in the instance that | |
7351 | -- were not present in the generic (i.e. when two different | |
7352 | -- types are given the same actual). If we are looking for a | |
7353 | -- spec to match a body, full conformance is expected. | |
996ae0b0 RK |
7354 | |
7355 | if In_Instance then | |
c05ba1f1 AC |
7356 | |
7357 | -- Inherit the convention and "ghostness" of the matching | |
7358 | -- spec to ensure proper full and subtype conformance. | |
7359 | ||
996ae0b0 RK |
7360 | Set_Convention (Designator, Convention (E)); |
7361 | ||
c05ba1f1 AC |
7362 | if Is_Ghost_Entity (E) then |
7363 | Set_Is_Ghost_Entity (Designator); | |
7364 | end if; | |
7365 | ||
0187b60e AC |
7366 | -- Skip past subprogram bodies and subprogram renamings that |
7367 | -- may appear to have a matching spec, but that aren't fully | |
7368 | -- conformant with it. That can occur in cases where an | |
7369 | -- actual type causes unrelated homographs in the instance. | |
7370 | ||
7371 | if Nkind_In (N, N_Subprogram_Body, | |
7372 | N_Subprogram_Renaming_Declaration) | |
996ae0b0 | 7373 | and then Present (Homonym (E)) |
c7b9d548 | 7374 | and then not Fully_Conformant (Designator, E) |
996ae0b0 RK |
7375 | then |
7376 | goto Next_Entity; | |
7377 | ||
c7b9d548 | 7378 | elsif not Subtype_Conformant (Designator, E) then |
996ae0b0 | 7379 | goto Next_Entity; |
70f4ad20 AC |
7380 | |
7381 | elsif Different_Generic_Profile (E) then | |
7382 | goto Next_Entity; | |
996ae0b0 RK |
7383 | end if; |
7384 | end if; | |
7385 | ||
25ebc085 AC |
7386 | -- Ada 2012 (AI05-0165): For internally generated bodies of |
7387 | -- null procedures locate the internally generated spec. We | |
7388 | -- enforce mode conformance since a tagged type may inherit | |
7389 | -- from interfaces several null primitives which differ only | |
7390 | -- in the mode of the formals. | |
7391 | ||
7392 | if not (Comes_From_Source (E)) | |
7393 | and then Is_Null_Procedure (E) | |
7394 | and then not Mode_Conformant (Designator, E) | |
7395 | then | |
7396 | null; | |
7397 | ||
4d8f3296 ES |
7398 | -- For null procedures coming from source that are completions, |
7399 | -- analysis of the generated body will establish the link. | |
7400 | ||
7401 | elsif Comes_From_Source (E) | |
7402 | and then Nkind (Spec) = N_Procedure_Specification | |
7403 | and then Null_Present (Spec) | |
7404 | then | |
7405 | return E; | |
7406 | ||
25ebc085 | 7407 | elsif not Has_Completion (E) then |
996ae0b0 RK |
7408 | if Nkind (N) /= N_Subprogram_Body_Stub then |
7409 | Set_Corresponding_Spec (N, E); | |
7410 | end if; | |
7411 | ||
7412 | Set_Has_Completion (E); | |
7413 | return E; | |
7414 | ||
7415 | elsif Nkind (Parent (N)) = N_Subunit then | |
7416 | ||
7417 | -- If this is the proper body of a subunit, the completion | |
7418 | -- flag is set when analyzing the stub. | |
7419 | ||
7420 | return E; | |
7421 | ||
70f4ad20 AC |
7422 | -- If E is an internal function with a controlling result that |
7423 | -- was created for an operation inherited by a null extension, | |
7424 | -- it may be overridden by a body without a previous spec (one | |
2995860f | 7425 | -- more reason why these should be shunned). In that case we |
70f4ad20 AC |
7426 | -- remove the generated body if present, because the current |
7427 | -- one is the explicit overriding. | |
81db9d77 ES |
7428 | |
7429 | elsif Ekind (E) = E_Function | |
0791fbe9 | 7430 | and then Ada_Version >= Ada_2005 |
81db9d77 ES |
7431 | and then not Comes_From_Source (E) |
7432 | and then Has_Controlling_Result (E) | |
7433 | and then Is_Null_Extension (Etype (E)) | |
7434 | and then Comes_From_Source (Spec) | |
7435 | then | |
7436 | Set_Has_Completion (E, False); | |
7437 | ||
1366997b AC |
7438 | if Expander_Active |
7439 | and then Nkind (Parent (E)) = N_Function_Specification | |
7440 | then | |
81db9d77 ES |
7441 | Remove |
7442 | (Unit_Declaration_Node | |
1366997b AC |
7443 | (Corresponding_Body (Unit_Declaration_Node (E)))); |
7444 | ||
81db9d77 ES |
7445 | return E; |
7446 | ||
1366997b AC |
7447 | -- If expansion is disabled, or if the wrapper function has |
7448 | -- not been generated yet, this a late body overriding an | |
7449 | -- inherited operation, or it is an overriding by some other | |
7450 | -- declaration before the controlling result is frozen. In | |
7451 | -- either case this is a declaration of a new entity. | |
81db9d77 ES |
7452 | |
7453 | else | |
7454 | return Empty; | |
7455 | end if; | |
7456 | ||
d44202ba HK |
7457 | -- If the body already exists, then this is an error unless |
7458 | -- the previous declaration is the implicit declaration of a | |
756ef2a0 AC |
7459 | -- derived subprogram. It is also legal for an instance to |
7460 | -- contain type conformant overloadable declarations (but the | |
7461 | -- generic declaration may not), per 8.3(26/2). | |
996ae0b0 RK |
7462 | |
7463 | elsif No (Alias (E)) | |
7464 | and then not Is_Intrinsic_Subprogram (E) | |
7465 | and then not In_Instance | |
d44202ba | 7466 | and then Post_Error |
996ae0b0 RK |
7467 | then |
7468 | Error_Msg_Sloc := Sloc (E); | |
8dbd1460 | 7469 | |
07fc65c4 GB |
7470 | if Is_Imported (E) then |
7471 | Error_Msg_NE | |
7472 | ("body not allowed for imported subprogram & declared#", | |
7473 | N, E); | |
7474 | else | |
7475 | Error_Msg_NE ("duplicate body for & declared#", N, E); | |
7476 | end if; | |
996ae0b0 RK |
7477 | end if; |
7478 | ||
d44202ba HK |
7479 | -- Child units cannot be overloaded, so a conformance mismatch |
7480 | -- between body and a previous spec is an error. | |
7481 | ||
996ae0b0 RK |
7482 | elsif Is_Child_Unit (E) |
7483 | and then | |
7484 | Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body | |
7485 | and then | |
5d37ba92 | 7486 | Nkind (Parent (Unit_Declaration_Node (Designator))) = |
d44202ba HK |
7487 | N_Compilation_Unit |
7488 | and then Post_Error | |
996ae0b0 | 7489 | then |
996ae0b0 RK |
7490 | Error_Msg_N |
7491 | ("body of child unit does not match previous declaration", N); | |
7492 | end if; | |
7493 | end if; | |
7494 | ||
7495 | <<Next_Entity>> | |
7496 | E := Homonym (E); | |
7497 | end loop; | |
7498 | ||
7499 | -- On exit, we know that no previous declaration of subprogram exists | |
7500 | ||
7501 | return Empty; | |
7502 | end Find_Corresponding_Spec; | |
7503 | ||
7504 | ---------------------- | |
7505 | -- Fully_Conformant -- | |
7506 | ---------------------- | |
7507 | ||
7508 | function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
7509 | Result : Boolean; | |
996ae0b0 RK |
7510 | begin |
7511 | Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result); | |
7512 | return Result; | |
7513 | end Fully_Conformant; | |
7514 | ||
7515 | ---------------------------------- | |
7516 | -- Fully_Conformant_Expressions -- | |
7517 | ---------------------------------- | |
7518 | ||
7519 | function Fully_Conformant_Expressions | |
7520 | (Given_E1 : Node_Id; | |
d05ef0ab | 7521 | Given_E2 : Node_Id) return Boolean |
996ae0b0 RK |
7522 | is |
7523 | E1 : constant Node_Id := Original_Node (Given_E1); | |
7524 | E2 : constant Node_Id := Original_Node (Given_E2); | |
7525 | -- We always test conformance on original nodes, since it is possible | |
7526 | -- for analysis and/or expansion to make things look as though they | |
7527 | -- conform when they do not, e.g. by converting 1+2 into 3. | |
7528 | ||
7529 | function FCE (Given_E1, Given_E2 : Node_Id) return Boolean | |
7530 | renames Fully_Conformant_Expressions; | |
7531 | ||
7532 | function FCL (L1, L2 : List_Id) return Boolean; | |
70f4ad20 AC |
7533 | -- Compare elements of two lists for conformance. Elements have to be |
7534 | -- conformant, and actuals inserted as default parameters do not match | |
7535 | -- explicit actuals with the same value. | |
996ae0b0 RK |
7536 | |
7537 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean; | |
e895b435 | 7538 | -- Compare an operator node with a function call |
996ae0b0 RK |
7539 | |
7540 | --------- | |
7541 | -- FCL -- | |
7542 | --------- | |
7543 | ||
7544 | function FCL (L1, L2 : List_Id) return Boolean is | |
7545 | N1, N2 : Node_Id; | |
7546 | ||
7547 | begin | |
7548 | if L1 = No_List then | |
7549 | N1 := Empty; | |
7550 | else | |
7551 | N1 := First (L1); | |
7552 | end if; | |
7553 | ||
7554 | if L2 = No_List then | |
7555 | N2 := Empty; | |
7556 | else | |
7557 | N2 := First (L2); | |
7558 | end if; | |
7559 | ||
70f4ad20 | 7560 | -- Compare two lists, skipping rewrite insertions (we want to compare |
a90bd866 | 7561 | -- the original trees, not the expanded versions). |
996ae0b0 RK |
7562 | |
7563 | loop | |
7564 | if Is_Rewrite_Insertion (N1) then | |
7565 | Next (N1); | |
7566 | elsif Is_Rewrite_Insertion (N2) then | |
7567 | Next (N2); | |
7568 | elsif No (N1) then | |
7569 | return No (N2); | |
7570 | elsif No (N2) then | |
7571 | return False; | |
7572 | elsif not FCE (N1, N2) then | |
7573 | return False; | |
7574 | else | |
7575 | Next (N1); | |
7576 | Next (N2); | |
7577 | end if; | |
7578 | end loop; | |
7579 | end FCL; | |
7580 | ||
7581 | --------- | |
7582 | -- FCO -- | |
7583 | --------- | |
7584 | ||
7585 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean is | |
7586 | Actuals : constant List_Id := Parameter_Associations (Call_Node); | |
7587 | Act : Node_Id; | |
7588 | ||
7589 | begin | |
7590 | if No (Actuals) | |
7591 | or else Entity (Op_Node) /= Entity (Name (Call_Node)) | |
7592 | then | |
7593 | return False; | |
7594 | ||
7595 | else | |
7596 | Act := First (Actuals); | |
7597 | ||
7598 | if Nkind (Op_Node) in N_Binary_Op then | |
996ae0b0 RK |
7599 | if not FCE (Left_Opnd (Op_Node), Act) then |
7600 | return False; | |
7601 | end if; | |
7602 | ||
7603 | Next (Act); | |
7604 | end if; | |
7605 | ||
7606 | return Present (Act) | |
7607 | and then FCE (Right_Opnd (Op_Node), Act) | |
7608 | and then No (Next (Act)); | |
7609 | end if; | |
7610 | end FCO; | |
7611 | ||
7612 | -- Start of processing for Fully_Conformant_Expressions | |
7613 | ||
7614 | begin | |
7615 | -- Non-conformant if paren count does not match. Note: if some idiot | |
7616 | -- complains that we don't do this right for more than 3 levels of | |
a90bd866 | 7617 | -- parentheses, they will be treated with the respect they deserve. |
996ae0b0 RK |
7618 | |
7619 | if Paren_Count (E1) /= Paren_Count (E2) then | |
7620 | return False; | |
7621 | ||
82c80734 RD |
7622 | -- If same entities are referenced, then they are conformant even if |
7623 | -- they have different forms (RM 8.3.1(19-20)). | |
996ae0b0 RK |
7624 | |
7625 | elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then | |
7626 | if Present (Entity (E1)) then | |
7627 | return Entity (E1) = Entity (E2) | |
7628 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) | |
7629 | and then Ekind (Entity (E1)) = E_Discriminant | |
7630 | and then Ekind (Entity (E2)) = E_In_Parameter); | |
7631 | ||
7632 | elsif Nkind (E1) = N_Expanded_Name | |
7633 | and then Nkind (E2) = N_Expanded_Name | |
7634 | and then Nkind (Selector_Name (E1)) = N_Character_Literal | |
7635 | and then Nkind (Selector_Name (E2)) = N_Character_Literal | |
7636 | then | |
7637 | return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)); | |
7638 | ||
7639 | else | |
7640 | -- Identifiers in component associations don't always have | |
7641 | -- entities, but their names must conform. | |
7642 | ||
7643 | return Nkind (E1) = N_Identifier | |
7644 | and then Nkind (E2) = N_Identifier | |
7645 | and then Chars (E1) = Chars (E2); | |
7646 | end if; | |
7647 | ||
7648 | elsif Nkind (E1) = N_Character_Literal | |
7649 | and then Nkind (E2) = N_Expanded_Name | |
7650 | then | |
7651 | return Nkind (Selector_Name (E2)) = N_Character_Literal | |
7652 | and then Chars (E1) = Chars (Selector_Name (E2)); | |
7653 | ||
7654 | elsif Nkind (E2) = N_Character_Literal | |
7655 | and then Nkind (E1) = N_Expanded_Name | |
7656 | then | |
7657 | return Nkind (Selector_Name (E1)) = N_Character_Literal | |
7658 | and then Chars (E2) = Chars (Selector_Name (E1)); | |
7659 | ||
8fde064e | 7660 | elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then |
996ae0b0 RK |
7661 | return FCO (E1, E2); |
7662 | ||
8fde064e | 7663 | elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then |
996ae0b0 RK |
7664 | return FCO (E2, E1); |
7665 | ||
7666 | -- Otherwise we must have the same syntactic entity | |
7667 | ||
7668 | elsif Nkind (E1) /= Nkind (E2) then | |
7669 | return False; | |
7670 | ||
7671 | -- At this point, we specialize by node type | |
7672 | ||
7673 | else | |
7674 | case Nkind (E1) is | |
7675 | ||
7676 | when N_Aggregate => | |
7677 | return | |
7678 | FCL (Expressions (E1), Expressions (E2)) | |
19d846a0 RD |
7679 | and then |
7680 | FCL (Component_Associations (E1), | |
7681 | Component_Associations (E2)); | |
996ae0b0 RK |
7682 | |
7683 | when N_Allocator => | |
7684 | if Nkind (Expression (E1)) = N_Qualified_Expression | |
7685 | or else | |
7686 | Nkind (Expression (E2)) = N_Qualified_Expression | |
7687 | then | |
7688 | return FCE (Expression (E1), Expression (E2)); | |
7689 | ||
7690 | -- Check that the subtype marks and any constraints | |
7691 | -- are conformant | |
7692 | ||
7693 | else | |
7694 | declare | |
7695 | Indic1 : constant Node_Id := Expression (E1); | |
7696 | Indic2 : constant Node_Id := Expression (E2); | |
7697 | Elt1 : Node_Id; | |
7698 | Elt2 : Node_Id; | |
7699 | ||
7700 | begin | |
7701 | if Nkind (Indic1) /= N_Subtype_Indication then | |
7702 | return | |
7703 | Nkind (Indic2) /= N_Subtype_Indication | |
7704 | and then Entity (Indic1) = Entity (Indic2); | |
7705 | ||
7706 | elsif Nkind (Indic2) /= N_Subtype_Indication then | |
7707 | return | |
7708 | Nkind (Indic1) /= N_Subtype_Indication | |
7709 | and then Entity (Indic1) = Entity (Indic2); | |
7710 | ||
7711 | else | |
7712 | if Entity (Subtype_Mark (Indic1)) /= | |
7713 | Entity (Subtype_Mark (Indic2)) | |
7714 | then | |
7715 | return False; | |
7716 | end if; | |
7717 | ||
7718 | Elt1 := First (Constraints (Constraint (Indic1))); | |
7719 | Elt2 := First (Constraints (Constraint (Indic2))); | |
996ae0b0 RK |
7720 | while Present (Elt1) and then Present (Elt2) loop |
7721 | if not FCE (Elt1, Elt2) then | |
7722 | return False; | |
7723 | end if; | |
7724 | ||
7725 | Next (Elt1); | |
7726 | Next (Elt2); | |
7727 | end loop; | |
7728 | ||
7729 | return True; | |
7730 | end if; | |
7731 | end; | |
7732 | end if; | |
7733 | ||
7734 | when N_Attribute_Reference => | |
7735 | return | |
7736 | Attribute_Name (E1) = Attribute_Name (E2) | |
7737 | and then FCL (Expressions (E1), Expressions (E2)); | |
7738 | ||
7739 | when N_Binary_Op => | |
7740 | return | |
7741 | Entity (E1) = Entity (E2) | |
7742 | and then FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7743 | and then FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7744 | ||
514d0fc5 | 7745 | when N_Short_Circuit | N_Membership_Test => |
996ae0b0 RK |
7746 | return |
7747 | FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7748 | and then | |
7749 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7750 | ||
19d846a0 RD |
7751 | when N_Case_Expression => |
7752 | declare | |
7753 | Alt1 : Node_Id; | |
7754 | Alt2 : Node_Id; | |
7755 | ||
7756 | begin | |
7757 | if not FCE (Expression (E1), Expression (E2)) then | |
7758 | return False; | |
7759 | ||
7760 | else | |
7761 | Alt1 := First (Alternatives (E1)); | |
7762 | Alt2 := First (Alternatives (E2)); | |
7763 | loop | |
7764 | if Present (Alt1) /= Present (Alt2) then | |
7765 | return False; | |
7766 | elsif No (Alt1) then | |
7767 | return True; | |
7768 | end if; | |
7769 | ||
7770 | if not FCE (Expression (Alt1), Expression (Alt2)) | |
7771 | or else not FCL (Discrete_Choices (Alt1), | |
7772 | Discrete_Choices (Alt2)) | |
7773 | then | |
7774 | return False; | |
7775 | end if; | |
7776 | ||
7777 | Next (Alt1); | |
7778 | Next (Alt2); | |
7779 | end loop; | |
7780 | end if; | |
7781 | end; | |
7782 | ||
996ae0b0 RK |
7783 | when N_Character_Literal => |
7784 | return | |
7785 | Char_Literal_Value (E1) = Char_Literal_Value (E2); | |
7786 | ||
7787 | when N_Component_Association => | |
7788 | return | |
7789 | FCL (Choices (E1), Choices (E2)) | |
19d846a0 RD |
7790 | and then |
7791 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 7792 | |
996ae0b0 RK |
7793 | when N_Explicit_Dereference => |
7794 | return | |
7795 | FCE (Prefix (E1), Prefix (E2)); | |
7796 | ||
7797 | when N_Extension_Aggregate => | |
7798 | return | |
7799 | FCL (Expressions (E1), Expressions (E2)) | |
7800 | and then Null_Record_Present (E1) = | |
7801 | Null_Record_Present (E2) | |
7802 | and then FCL (Component_Associations (E1), | |
7803 | Component_Associations (E2)); | |
7804 | ||
7805 | when N_Function_Call => | |
7806 | return | |
7807 | FCE (Name (E1), Name (E2)) | |
19d846a0 RD |
7808 | and then |
7809 | FCL (Parameter_Associations (E1), | |
7810 | Parameter_Associations (E2)); | |
996ae0b0 | 7811 | |
9b16cb57 RD |
7812 | when N_If_Expression => |
7813 | return | |
7814 | FCL (Expressions (E1), Expressions (E2)); | |
7815 | ||
996ae0b0 RK |
7816 | when N_Indexed_Component => |
7817 | return | |
7818 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7819 | and then |
7820 | FCL (Expressions (E1), Expressions (E2)); | |
996ae0b0 RK |
7821 | |
7822 | when N_Integer_Literal => | |
7823 | return (Intval (E1) = Intval (E2)); | |
7824 | ||
7825 | when N_Null => | |
7826 | return True; | |
7827 | ||
7828 | when N_Operator_Symbol => | |
7829 | return | |
7830 | Chars (E1) = Chars (E2); | |
7831 | ||
7832 | when N_Others_Choice => | |
7833 | return True; | |
7834 | ||
7835 | when N_Parameter_Association => | |
7836 | return | |
996ae0b0 RK |
7837 | Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)) |
7838 | and then FCE (Explicit_Actual_Parameter (E1), | |
7839 | Explicit_Actual_Parameter (E2)); | |
7840 | ||
7841 | when N_Qualified_Expression => | |
7842 | return | |
7843 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7844 | and then |
7845 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 7846 | |
2010d078 AC |
7847 | when N_Quantified_Expression => |
7848 | if not FCE (Condition (E1), Condition (E2)) then | |
7849 | return False; | |
7850 | end if; | |
7851 | ||
7852 | if Present (Loop_Parameter_Specification (E1)) | |
7853 | and then Present (Loop_Parameter_Specification (E2)) | |
7854 | then | |
7855 | declare | |
7856 | L1 : constant Node_Id := | |
7857 | Loop_Parameter_Specification (E1); | |
7858 | L2 : constant Node_Id := | |
7859 | Loop_Parameter_Specification (E2); | |
7860 | ||
7861 | begin | |
7862 | return | |
7863 | Reverse_Present (L1) = Reverse_Present (L2) | |
7864 | and then | |
7865 | FCE (Defining_Identifier (L1), | |
7866 | Defining_Identifier (L2)) | |
7867 | and then | |
7868 | FCE (Discrete_Subtype_Definition (L1), | |
7869 | Discrete_Subtype_Definition (L2)); | |
7870 | end; | |
7871 | ||
804670f1 AC |
7872 | elsif Present (Iterator_Specification (E1)) |
7873 | and then Present (Iterator_Specification (E2)) | |
7874 | then | |
2010d078 AC |
7875 | declare |
7876 | I1 : constant Node_Id := Iterator_Specification (E1); | |
7877 | I2 : constant Node_Id := Iterator_Specification (E2); | |
7878 | ||
7879 | begin | |
7880 | return | |
7881 | FCE (Defining_Identifier (I1), | |
7882 | Defining_Identifier (I2)) | |
7883 | and then | |
7884 | Of_Present (I1) = Of_Present (I2) | |
7885 | and then | |
7886 | Reverse_Present (I1) = Reverse_Present (I2) | |
7887 | and then FCE (Name (I1), Name (I2)) | |
7888 | and then FCE (Subtype_Indication (I1), | |
7889 | Subtype_Indication (I2)); | |
7890 | end; | |
804670f1 AC |
7891 | |
7892 | -- The quantified expressions used different specifications to | |
7893 | -- walk their respective ranges. | |
7894 | ||
7895 | else | |
7896 | return False; | |
2010d078 AC |
7897 | end if; |
7898 | ||
996ae0b0 RK |
7899 | when N_Range => |
7900 | return | |
7901 | FCE (Low_Bound (E1), Low_Bound (E2)) | |
19d846a0 RD |
7902 | and then |
7903 | FCE (High_Bound (E1), High_Bound (E2)); | |
996ae0b0 RK |
7904 | |
7905 | when N_Real_Literal => | |
7906 | return (Realval (E1) = Realval (E2)); | |
7907 | ||
7908 | when N_Selected_Component => | |
7909 | return | |
7910 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7911 | and then |
7912 | FCE (Selector_Name (E1), Selector_Name (E2)); | |
996ae0b0 RK |
7913 | |
7914 | when N_Slice => | |
7915 | return | |
7916 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7917 | and then |
7918 | FCE (Discrete_Range (E1), Discrete_Range (E2)); | |
996ae0b0 RK |
7919 | |
7920 | when N_String_Literal => | |
7921 | declare | |
7922 | S1 : constant String_Id := Strval (E1); | |
7923 | S2 : constant String_Id := Strval (E2); | |
7924 | L1 : constant Nat := String_Length (S1); | |
7925 | L2 : constant Nat := String_Length (S2); | |
7926 | ||
7927 | begin | |
7928 | if L1 /= L2 then | |
7929 | return False; | |
7930 | ||
7931 | else | |
7932 | for J in 1 .. L1 loop | |
7933 | if Get_String_Char (S1, J) /= | |
7934 | Get_String_Char (S2, J) | |
7935 | then | |
7936 | return False; | |
7937 | end if; | |
7938 | end loop; | |
7939 | ||
7940 | return True; | |
7941 | end if; | |
7942 | end; | |
7943 | ||
7944 | when N_Type_Conversion => | |
7945 | return | |
7946 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7947 | and then |
7948 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7949 | |
7950 | when N_Unary_Op => | |
7951 | return | |
7952 | Entity (E1) = Entity (E2) | |
19d846a0 RD |
7953 | and then |
7954 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
996ae0b0 RK |
7955 | |
7956 | when N_Unchecked_Type_Conversion => | |
7957 | return | |
7958 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7959 | and then |
7960 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7961 | |
7962 | -- All other node types cannot appear in this context. Strictly | |
7963 | -- we should raise a fatal internal error. Instead we just ignore | |
7964 | -- the nodes. This means that if anyone makes a mistake in the | |
2995860f AC |
7965 | -- expander and mucks an expression tree irretrievably, the result |
7966 | -- will be a failure to detect a (probably very obscure) case | |
7967 | -- of non-conformance, which is better than bombing on some | |
996ae0b0 RK |
7968 | -- case where two expressions do in fact conform. |
7969 | ||
7970 | when others => | |
7971 | return True; | |
7972 | ||
7973 | end case; | |
7974 | end if; | |
7975 | end Fully_Conformant_Expressions; | |
7976 | ||
fbf5a39b AC |
7977 | ---------------------------------------- |
7978 | -- Fully_Conformant_Discrete_Subtypes -- | |
7979 | ---------------------------------------- | |
7980 | ||
7981 | function Fully_Conformant_Discrete_Subtypes | |
7982 | (Given_S1 : Node_Id; | |
d05ef0ab | 7983 | Given_S2 : Node_Id) return Boolean |
fbf5a39b AC |
7984 | is |
7985 | S1 : constant Node_Id := Original_Node (Given_S1); | |
7986 | S2 : constant Node_Id := Original_Node (Given_S2); | |
7987 | ||
7988 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean; | |
82c80734 RD |
7989 | -- Special-case for a bound given by a discriminant, which in the body |
7990 | -- is replaced with the discriminal of the enclosing type. | |
fbf5a39b AC |
7991 | |
7992 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean; | |
e895b435 | 7993 | -- Check both bounds |
fbf5a39b | 7994 | |
5d37ba92 ES |
7995 | ----------------------- |
7996 | -- Conforming_Bounds -- | |
7997 | ----------------------- | |
7998 | ||
fbf5a39b AC |
7999 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is |
8000 | begin | |
8001 | if Is_Entity_Name (B1) | |
8002 | and then Is_Entity_Name (B2) | |
8003 | and then Ekind (Entity (B1)) = E_Discriminant | |
8004 | then | |
8005 | return Chars (B1) = Chars (B2); | |
8006 | ||
8007 | else | |
8008 | return Fully_Conformant_Expressions (B1, B2); | |
8009 | end if; | |
8010 | end Conforming_Bounds; | |
8011 | ||
5d37ba92 ES |
8012 | ----------------------- |
8013 | -- Conforming_Ranges -- | |
8014 | ----------------------- | |
8015 | ||
fbf5a39b AC |
8016 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is |
8017 | begin | |
8018 | return | |
8019 | Conforming_Bounds (Low_Bound (R1), Low_Bound (R2)) | |
8020 | and then | |
8021 | Conforming_Bounds (High_Bound (R1), High_Bound (R2)); | |
8022 | end Conforming_Ranges; | |
8023 | ||
8024 | -- Start of processing for Fully_Conformant_Discrete_Subtypes | |
8025 | ||
8026 | begin | |
8027 | if Nkind (S1) /= Nkind (S2) then | |
8028 | return False; | |
8029 | ||
8030 | elsif Is_Entity_Name (S1) then | |
8031 | return Entity (S1) = Entity (S2); | |
8032 | ||
8033 | elsif Nkind (S1) = N_Range then | |
8034 | return Conforming_Ranges (S1, S2); | |
8035 | ||
8036 | elsif Nkind (S1) = N_Subtype_Indication then | |
8037 | return | |
8038 | Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2)) | |
8039 | and then | |
8040 | Conforming_Ranges | |
8041 | (Range_Expression (Constraint (S1)), | |
8042 | Range_Expression (Constraint (S2))); | |
8043 | else | |
8044 | return True; | |
8045 | end if; | |
8046 | end Fully_Conformant_Discrete_Subtypes; | |
8047 | ||
996ae0b0 RK |
8048 | -------------------- |
8049 | -- Install_Entity -- | |
8050 | -------------------- | |
8051 | ||
8052 | procedure Install_Entity (E : Entity_Id) is | |
8053 | Prev : constant Entity_Id := Current_Entity (E); | |
996ae0b0 RK |
8054 | begin |
8055 | Set_Is_Immediately_Visible (E); | |
8056 | Set_Current_Entity (E); | |
8057 | Set_Homonym (E, Prev); | |
8058 | end Install_Entity; | |
8059 | ||
8060 | --------------------- | |
8061 | -- Install_Formals -- | |
8062 | --------------------- | |
8063 | ||
8064 | procedure Install_Formals (Id : Entity_Id) is | |
8065 | F : Entity_Id; | |
996ae0b0 RK |
8066 | begin |
8067 | F := First_Formal (Id); | |
996ae0b0 RK |
8068 | while Present (F) loop |
8069 | Install_Entity (F); | |
8070 | Next_Formal (F); | |
8071 | end loop; | |
8072 | end Install_Formals; | |
8073 | ||
ce2b6ba5 JM |
8074 | ----------------------------- |
8075 | -- Is_Interface_Conformant -- | |
8076 | ----------------------------- | |
8077 | ||
8078 | function Is_Interface_Conformant | |
8079 | (Tagged_Type : Entity_Id; | |
8080 | Iface_Prim : Entity_Id; | |
8081 | Prim : Entity_Id) return Boolean | |
8082 | is | |
9e92ad49 AC |
8083 | -- The operation may in fact be an inherited (implicit) operation |
8084 | -- rather than the original interface primitive, so retrieve the | |
8085 | -- ultimate ancestor. | |
8086 | ||
8087 | Iface : constant Entity_Id := | |
8088 | Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)); | |
fceeaab6 ES |
8089 | Typ : constant Entity_Id := Find_Dispatching_Type (Prim); |
8090 | ||
25ebc085 AC |
8091 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id; |
8092 | -- Return the controlling formal of Prim | |
8093 | ||
59e6b23c AC |
8094 | ------------------------ |
8095 | -- Controlling_Formal -- | |
8096 | ------------------------ | |
8097 | ||
25ebc085 | 8098 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id is |
15918371 | 8099 | E : Entity_Id; |
59e6b23c | 8100 | |
25ebc085 | 8101 | begin |
15918371 | 8102 | E := First_Entity (Prim); |
25ebc085 AC |
8103 | while Present (E) loop |
8104 | if Is_Formal (E) and then Is_Controlling_Formal (E) then | |
8105 | return E; | |
8106 | end if; | |
8107 | ||
8108 | Next_Entity (E); | |
8109 | end loop; | |
8110 | ||
8111 | return Empty; | |
8112 | end Controlling_Formal; | |
8113 | ||
8114 | -- Local variables | |
8115 | ||
8116 | Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim); | |
8117 | Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim); | |
8118 | ||
8119 | -- Start of processing for Is_Interface_Conformant | |
8120 | ||
ce2b6ba5 JM |
8121 | begin |
8122 | pragma Assert (Is_Subprogram (Iface_Prim) | |
8123 | and then Is_Subprogram (Prim) | |
8124 | and then Is_Dispatching_Operation (Iface_Prim) | |
8125 | and then Is_Dispatching_Operation (Prim)); | |
8126 | ||
fceeaab6 | 8127 | pragma Assert (Is_Interface (Iface) |
ce2b6ba5 JM |
8128 | or else (Present (Alias (Iface_Prim)) |
8129 | and then | |
8130 | Is_Interface | |
8131 | (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim))))); | |
8132 | ||
8133 | if Prim = Iface_Prim | |
8134 | or else not Is_Subprogram (Prim) | |
8135 | or else Ekind (Prim) /= Ekind (Iface_Prim) | |
8136 | or else not Is_Dispatching_Operation (Prim) | |
8137 | or else Scope (Prim) /= Scope (Tagged_Type) | |
fceeaab6 | 8138 | or else No (Typ) |
8a49a499 | 8139 | or else Base_Type (Typ) /= Base_Type (Tagged_Type) |
ce2b6ba5 JM |
8140 | or else not Primitive_Names_Match (Iface_Prim, Prim) |
8141 | then | |
8142 | return False; | |
8143 | ||
25ebc085 AC |
8144 | -- The mode of the controlling formals must match |
8145 | ||
8146 | elsif Present (Iface_Ctrl_F) | |
15918371 AC |
8147 | and then Present (Prim_Ctrl_F) |
8148 | and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F) | |
25ebc085 AC |
8149 | then |
8150 | return False; | |
8151 | ||
8152 | -- Case of a procedure, or a function whose result type matches the | |
8153 | -- result type of the interface primitive, or a function that has no | |
8154 | -- controlling result (I or access I). | |
ce2b6ba5 JM |
8155 | |
8156 | elsif Ekind (Iface_Prim) = E_Procedure | |
8157 | or else Etype (Prim) = Etype (Iface_Prim) | |
fceeaab6 | 8158 | or else not Has_Controlling_Result (Prim) |
ce2b6ba5 | 8159 | then |
b4d7b435 AC |
8160 | return Type_Conformant |
8161 | (Iface_Prim, Prim, Skip_Controlling_Formals => True); | |
ce2b6ba5 | 8162 | |
2995860f AC |
8163 | -- Case of a function returning an interface, or an access to one. Check |
8164 | -- that the return types correspond. | |
ce2b6ba5 | 8165 | |
fceeaab6 ES |
8166 | elsif Implements_Interface (Typ, Iface) then |
8167 | if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type) | |
9a3c9940 RD |
8168 | /= |
8169 | (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type) | |
fceeaab6 ES |
8170 | then |
8171 | return False; | |
fceeaab6 ES |
8172 | else |
8173 | return | |
9e92ad49 | 8174 | Type_Conformant (Prim, Ultimate_Alias (Iface_Prim), |
ce2b6ba5 | 8175 | Skip_Controlling_Formals => True); |
fceeaab6 | 8176 | end if; |
ce2b6ba5 | 8177 | |
fceeaab6 ES |
8178 | else |
8179 | return False; | |
ce2b6ba5 | 8180 | end if; |
ce2b6ba5 JM |
8181 | end Is_Interface_Conformant; |
8182 | ||
996ae0b0 RK |
8183 | --------------------------------- |
8184 | -- Is_Non_Overriding_Operation -- | |
8185 | --------------------------------- | |
8186 | ||
8187 | function Is_Non_Overriding_Operation | |
8188 | (Prev_E : Entity_Id; | |
d05ef0ab | 8189 | New_E : Entity_Id) return Boolean |
996ae0b0 RK |
8190 | is |
8191 | Formal : Entity_Id; | |
8192 | F_Typ : Entity_Id; | |
8193 | G_Typ : Entity_Id := Empty; | |
8194 | ||
8195 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
8196 | -- If F_Type is a derived type associated with a generic actual subtype, |
8197 | -- then return its Generic_Parent_Type attribute, else return Empty. | |
996ae0b0 RK |
8198 | |
8199 | function Types_Correspond | |
8200 | (P_Type : Entity_Id; | |
d05ef0ab | 8201 | N_Type : Entity_Id) return Boolean; |
82c80734 RD |
8202 | -- Returns true if and only if the types (or designated types in the |
8203 | -- case of anonymous access types) are the same or N_Type is derived | |
8204 | -- directly or indirectly from P_Type. | |
996ae0b0 RK |
8205 | |
8206 | ----------------------------- | |
8207 | -- Get_Generic_Parent_Type -- | |
8208 | ----------------------------- | |
8209 | ||
8210 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is | |
8211 | G_Typ : Entity_Id; | |
702d2020 | 8212 | Defn : Node_Id; |
996ae0b0 RK |
8213 | Indic : Node_Id; |
8214 | ||
8215 | begin | |
8216 | if Is_Derived_Type (F_Typ) | |
8217 | and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration | |
8218 | then | |
82c80734 RD |
8219 | -- The tree must be traversed to determine the parent subtype in |
8220 | -- the generic unit, which unfortunately isn't always available | |
8221 | -- via semantic attributes. ??? (Note: The use of Original_Node | |
8222 | -- is needed for cases where a full derived type has been | |
8223 | -- rewritten.) | |
996ae0b0 | 8224 | |
702d2020 AC |
8225 | Defn := Type_Definition (Original_Node (Parent (F_Typ))); |
8226 | if Nkind (Defn) = N_Derived_Type_Definition then | |
8227 | Indic := Subtype_Indication (Defn); | |
996ae0b0 | 8228 | |
702d2020 AC |
8229 | if Nkind (Indic) = N_Subtype_Indication then |
8230 | G_Typ := Entity (Subtype_Mark (Indic)); | |
8231 | else | |
8232 | G_Typ := Entity (Indic); | |
8233 | end if; | |
996ae0b0 | 8234 | |
702d2020 AC |
8235 | if Nkind (Parent (G_Typ)) = N_Subtype_Declaration |
8236 | and then Present (Generic_Parent_Type (Parent (G_Typ))) | |
8237 | then | |
8238 | return Generic_Parent_Type (Parent (G_Typ)); | |
8239 | end if; | |
996ae0b0 RK |
8240 | end if; |
8241 | end if; | |
8242 | ||
8243 | return Empty; | |
8244 | end Get_Generic_Parent_Type; | |
8245 | ||
8246 | ---------------------- | |
8247 | -- Types_Correspond -- | |
8248 | ---------------------- | |
8249 | ||
8250 | function Types_Correspond | |
8251 | (P_Type : Entity_Id; | |
d05ef0ab | 8252 | N_Type : Entity_Id) return Boolean |
996ae0b0 RK |
8253 | is |
8254 | Prev_Type : Entity_Id := Base_Type (P_Type); | |
8255 | New_Type : Entity_Id := Base_Type (N_Type); | |
8256 | ||
8257 | begin | |
8258 | if Ekind (Prev_Type) = E_Anonymous_Access_Type then | |
8259 | Prev_Type := Designated_Type (Prev_Type); | |
8260 | end if; | |
8261 | ||
8262 | if Ekind (New_Type) = E_Anonymous_Access_Type then | |
8263 | New_Type := Designated_Type (New_Type); | |
8264 | end if; | |
8265 | ||
8266 | if Prev_Type = New_Type then | |
8267 | return True; | |
8268 | ||
8269 | elsif not Is_Class_Wide_Type (New_Type) then | |
8270 | while Etype (New_Type) /= New_Type loop | |
8271 | New_Type := Etype (New_Type); | |
8272 | if New_Type = Prev_Type then | |
8273 | return True; | |
8274 | end if; | |
8275 | end loop; | |
8276 | end if; | |
8277 | return False; | |
8278 | end Types_Correspond; | |
8279 | ||
8280 | -- Start of processing for Is_Non_Overriding_Operation | |
8281 | ||
8282 | begin | |
82c80734 RD |
8283 | -- In the case where both operations are implicit derived subprograms |
8284 | -- then neither overrides the other. This can only occur in certain | |
8285 | -- obscure cases (e.g., derivation from homographs created in a generic | |
8286 | -- instantiation). | |
996ae0b0 RK |
8287 | |
8288 | if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then | |
8289 | return True; | |
8290 | ||
8291 | elsif Ekind (Current_Scope) = E_Package | |
8292 | and then Is_Generic_Instance (Current_Scope) | |
8293 | and then In_Private_Part (Current_Scope) | |
8294 | and then Comes_From_Source (New_E) | |
8295 | then | |
702d2020 AC |
8296 | -- We examine the formals and result type of the inherited operation, |
8297 | -- to determine whether their type is derived from (the instance of) | |
8298 | -- a generic type. The first such formal or result type is the one | |
8299 | -- tested. | |
996ae0b0 RK |
8300 | |
8301 | Formal := First_Formal (Prev_E); | |
996ae0b0 RK |
8302 | while Present (Formal) loop |
8303 | F_Typ := Base_Type (Etype (Formal)); | |
8304 | ||
8305 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
8306 | F_Typ := Designated_Type (F_Typ); | |
8307 | end if; | |
8308 | ||
8309 | G_Typ := Get_Generic_Parent_Type (F_Typ); | |
702d2020 | 8310 | exit when Present (G_Typ); |
996ae0b0 RK |
8311 | |
8312 | Next_Formal (Formal); | |
8313 | end loop; | |
8314 | ||
c8ef728f | 8315 | if No (G_Typ) and then Ekind (Prev_E) = E_Function then |
996ae0b0 RK |
8316 | G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E))); |
8317 | end if; | |
8318 | ||
8319 | if No (G_Typ) then | |
8320 | return False; | |
8321 | end if; | |
8322 | ||
8dbd1460 AC |
8323 | -- If the generic type is a private type, then the original operation |
8324 | -- was not overriding in the generic, because there was no primitive | |
8325 | -- operation to override. | |
996ae0b0 RK |
8326 | |
8327 | if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration | |
8328 | and then Nkind (Formal_Type_Definition (Parent (G_Typ))) = | |
8dbd1460 | 8329 | N_Formal_Private_Type_Definition |
996ae0b0 RK |
8330 | then |
8331 | return True; | |
8332 | ||
8333 | -- The generic parent type is the ancestor of a formal derived | |
8334 | -- type declaration. We need to check whether it has a primitive | |
8335 | -- operation that should be overridden by New_E in the generic. | |
8336 | ||
8337 | else | |
8338 | declare | |
8339 | P_Formal : Entity_Id; | |
8340 | N_Formal : Entity_Id; | |
8341 | P_Typ : Entity_Id; | |
8342 | N_Typ : Entity_Id; | |
8343 | P_Prim : Entity_Id; | |
8344 | Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ)); | |
8345 | ||
8346 | begin | |
8347 | while Present (Prim_Elt) loop | |
8348 | P_Prim := Node (Prim_Elt); | |
fbf5a39b | 8349 | |
996ae0b0 RK |
8350 | if Chars (P_Prim) = Chars (New_E) |
8351 | and then Ekind (P_Prim) = Ekind (New_E) | |
8352 | then | |
8353 | P_Formal := First_Formal (P_Prim); | |
8354 | N_Formal := First_Formal (New_E); | |
8355 | while Present (P_Formal) and then Present (N_Formal) loop | |
8356 | P_Typ := Etype (P_Formal); | |
8357 | N_Typ := Etype (N_Formal); | |
8358 | ||
8359 | if not Types_Correspond (P_Typ, N_Typ) then | |
8360 | exit; | |
8361 | end if; | |
8362 | ||
8363 | Next_Entity (P_Formal); | |
8364 | Next_Entity (N_Formal); | |
8365 | end loop; | |
8366 | ||
82c80734 RD |
8367 | -- Found a matching primitive operation belonging to the |
8368 | -- formal ancestor type, so the new subprogram is | |
8369 | -- overriding. | |
996ae0b0 | 8370 | |
c8ef728f ES |
8371 | if No (P_Formal) |
8372 | and then No (N_Formal) | |
996ae0b0 RK |
8373 | and then (Ekind (New_E) /= E_Function |
8374 | or else | |
8fde064e AC |
8375 | Types_Correspond |
8376 | (Etype (P_Prim), Etype (New_E))) | |
996ae0b0 RK |
8377 | then |
8378 | return False; | |
8379 | end if; | |
8380 | end if; | |
8381 | ||
8382 | Next_Elmt (Prim_Elt); | |
8383 | end loop; | |
8384 | ||
2995860f AC |
8385 | -- If no match found, then the new subprogram does not override |
8386 | -- in the generic (nor in the instance). | |
996ae0b0 | 8387 | |
260359e3 AC |
8388 | -- If the type in question is not abstract, and the subprogram |
8389 | -- is, this will be an error if the new operation is in the | |
8390 | -- private part of the instance. Emit a warning now, which will | |
8391 | -- make the subsequent error message easier to understand. | |
8392 | ||
8393 | if not Is_Abstract_Type (F_Typ) | |
8394 | and then Is_Abstract_Subprogram (Prev_E) | |
8395 | and then In_Private_Part (Current_Scope) | |
8396 | then | |
8397 | Error_Msg_Node_2 := F_Typ; | |
8398 | Error_Msg_NE | |
3ccedacc AC |
8399 | ("private operation& in generic unit does not override " |
8400 | & "any primitive operation of& (RM 12.3 (18))??", | |
260359e3 AC |
8401 | New_E, New_E); |
8402 | end if; | |
8403 | ||
996ae0b0 RK |
8404 | return True; |
8405 | end; | |
8406 | end if; | |
8407 | else | |
8408 | return False; | |
8409 | end if; | |
8410 | end Is_Non_Overriding_Operation; | |
8411 | ||
beacce02 AC |
8412 | ------------------------------------- |
8413 | -- List_Inherited_Pre_Post_Aspects -- | |
8414 | ------------------------------------- | |
8415 | ||
8416 | procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is | |
8417 | begin | |
e606088a | 8418 | if Opt.List_Inherited_Aspects |
b9696ffb | 8419 | and then Is_Subprogram_Or_Generic_Subprogram (E) |
beacce02 AC |
8420 | then |
8421 | declare | |
c9d70ab1 AC |
8422 | Subps : constant Subprogram_List := Inherited_Subprograms (E); |
8423 | Items : Node_Id; | |
8424 | Prag : Node_Id; | |
beacce02 AC |
8425 | |
8426 | begin | |
c9d70ab1 AC |
8427 | for Index in Subps'Range loop |
8428 | Items := Contract (Subps (Index)); | |
8429 | ||
8430 | if Present (Items) then | |
8431 | Prag := Pre_Post_Conditions (Items); | |
8432 | while Present (Prag) loop | |
8433 | Error_Msg_Sloc := Sloc (Prag); | |
8434 | ||
8435 | if Class_Present (Prag) | |
8436 | and then not Split_PPC (Prag) | |
8437 | then | |
8438 | if Pragma_Name (Prag) = Name_Precondition then | |
8439 | Error_Msg_N | |
8440 | ("info: & inherits `Pre''Class` aspect from " | |
8441 | & "#?L?", E); | |
8442 | else | |
8443 | Error_Msg_N | |
8444 | ("info: & inherits `Post''Class` aspect from " | |
8445 | & "#?L?", E); | |
8446 | end if; | |
beacce02 | 8447 | end if; |
beacce02 | 8448 | |
c9d70ab1 AC |
8449 | Prag := Next_Pragma (Prag); |
8450 | end loop; | |
8451 | end if; | |
beacce02 AC |
8452 | end loop; |
8453 | end; | |
8454 | end if; | |
8455 | end List_Inherited_Pre_Post_Aspects; | |
8456 | ||
996ae0b0 RK |
8457 | ------------------------------ |
8458 | -- Make_Inequality_Operator -- | |
8459 | ------------------------------ | |
8460 | ||
8461 | -- S is the defining identifier of an equality operator. We build a | |
8462 | -- subprogram declaration with the right signature. This operation is | |
8463 | -- intrinsic, because it is always expanded as the negation of the | |
8464 | -- call to the equality function. | |
8465 | ||
8466 | procedure Make_Inequality_Operator (S : Entity_Id) is | |
8467 | Loc : constant Source_Ptr := Sloc (S); | |
8468 | Decl : Node_Id; | |
8469 | Formals : List_Id; | |
8470 | Op_Name : Entity_Id; | |
8471 | ||
c8ef728f ES |
8472 | FF : constant Entity_Id := First_Formal (S); |
8473 | NF : constant Entity_Id := Next_Formal (FF); | |
996ae0b0 RK |
8474 | |
8475 | begin | |
c8ef728f | 8476 | -- Check that equality was properly defined, ignore call if not |
996ae0b0 | 8477 | |
c8ef728f | 8478 | if No (NF) then |
996ae0b0 RK |
8479 | return; |
8480 | end if; | |
8481 | ||
c8ef728f ES |
8482 | declare |
8483 | A : constant Entity_Id := | |
8484 | Make_Defining_Identifier (Sloc (FF), | |
8485 | Chars => Chars (FF)); | |
8486 | ||
5d37ba92 ES |
8487 | B : constant Entity_Id := |
8488 | Make_Defining_Identifier (Sloc (NF), | |
8489 | Chars => Chars (NF)); | |
c8ef728f ES |
8490 | |
8491 | begin | |
8492 | Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne); | |
8493 | ||
8494 | Formals := New_List ( | |
8495 | Make_Parameter_Specification (Loc, | |
8496 | Defining_Identifier => A, | |
8497 | Parameter_Type => | |
e4494292 | 8498 | New_Occurrence_Of (Etype (First_Formal (S)), |
c8ef728f ES |
8499 | Sloc (Etype (First_Formal (S))))), |
8500 | ||
8501 | Make_Parameter_Specification (Loc, | |
8502 | Defining_Identifier => B, | |
8503 | Parameter_Type => | |
e4494292 | 8504 | New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))), |
c8ef728f ES |
8505 | Sloc (Etype (Next_Formal (First_Formal (S))))))); |
8506 | ||
8507 | Decl := | |
8508 | Make_Subprogram_Declaration (Loc, | |
8509 | Specification => | |
8510 | Make_Function_Specification (Loc, | |
8511 | Defining_Unit_Name => Op_Name, | |
8512 | Parameter_Specifications => Formals, | |
8513 | Result_Definition => | |
e4494292 | 8514 | New_Occurrence_Of (Standard_Boolean, Loc))); |
c8ef728f ES |
8515 | |
8516 | -- Insert inequality right after equality if it is explicit or after | |
8517 | -- the derived type when implicit. These entities are created only | |
2995860f AC |
8518 | -- for visibility purposes, and eventually replaced in the course |
8519 | -- of expansion, so they do not need to be attached to the tree and | |
8520 | -- seen by the back-end. Keeping them internal also avoids spurious | |
c8ef728f ES |
8521 | -- freezing problems. The declaration is inserted in the tree for |
8522 | -- analysis, and removed afterwards. If the equality operator comes | |
8523 | -- from an explicit declaration, attach the inequality immediately | |
8524 | -- after. Else the equality is inherited from a derived type | |
8525 | -- declaration, so insert inequality after that declaration. | |
8526 | ||
8527 | if No (Alias (S)) then | |
8528 | Insert_After (Unit_Declaration_Node (S), Decl); | |
8529 | elsif Is_List_Member (Parent (S)) then | |
8530 | Insert_After (Parent (S), Decl); | |
8531 | else | |
8532 | Insert_After (Parent (Etype (First_Formal (S))), Decl); | |
8533 | end if; | |
996ae0b0 | 8534 | |
c8ef728f ES |
8535 | Mark_Rewrite_Insertion (Decl); |
8536 | Set_Is_Intrinsic_Subprogram (Op_Name); | |
8537 | Analyze (Decl); | |
8538 | Remove (Decl); | |
8539 | Set_Has_Completion (Op_Name); | |
8540 | Set_Corresponding_Equality (Op_Name, S); | |
f937473f | 8541 | Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S)); |
c8ef728f | 8542 | end; |
996ae0b0 RK |
8543 | end Make_Inequality_Operator; |
8544 | ||
8545 | ---------------------- | |
8546 | -- May_Need_Actuals -- | |
8547 | ---------------------- | |
8548 | ||
8549 | procedure May_Need_Actuals (Fun : Entity_Id) is | |
8550 | F : Entity_Id; | |
8551 | B : Boolean; | |
8552 | ||
8553 | begin | |
8554 | F := First_Formal (Fun); | |
8555 | B := True; | |
996ae0b0 RK |
8556 | while Present (F) loop |
8557 | if No (Default_Value (F)) then | |
8558 | B := False; | |
8559 | exit; | |
8560 | end if; | |
8561 | ||
8562 | Next_Formal (F); | |
8563 | end loop; | |
8564 | ||
8565 | Set_Needs_No_Actuals (Fun, B); | |
8566 | end May_Need_Actuals; | |
8567 | ||
8568 | --------------------- | |
8569 | -- Mode_Conformant -- | |
8570 | --------------------- | |
8571 | ||
8572 | function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
8573 | Result : Boolean; | |
996ae0b0 RK |
8574 | begin |
8575 | Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result); | |
8576 | return Result; | |
8577 | end Mode_Conformant; | |
8578 | ||
8579 | --------------------------- | |
8580 | -- New_Overloaded_Entity -- | |
8581 | --------------------------- | |
8582 | ||
8583 | procedure New_Overloaded_Entity | |
8584 | (S : Entity_Id; | |
8585 | Derived_Type : Entity_Id := Empty) | |
8586 | is | |
ec4867fa | 8587 | Overridden_Subp : Entity_Id := Empty; |
758c442c GD |
8588 | -- Set if the current scope has an operation that is type-conformant |
8589 | -- with S, and becomes hidden by S. | |
8590 | ||
5d37ba92 ES |
8591 | Is_Primitive_Subp : Boolean; |
8592 | -- Set to True if the new subprogram is primitive | |
8593 | ||
fbf5a39b AC |
8594 | E : Entity_Id; |
8595 | -- Entity that S overrides | |
8596 | ||
996ae0b0 | 8597 | Prev_Vis : Entity_Id := Empty; |
ec4867fa ES |
8598 | -- Predecessor of E in Homonym chain |
8599 | ||
5d37ba92 ES |
8600 | procedure Check_For_Primitive_Subprogram |
8601 | (Is_Primitive : out Boolean; | |
8602 | Is_Overriding : Boolean := False); | |
8603 | -- If the subprogram being analyzed is a primitive operation of the type | |
8604 | -- of a formal or result, set the Has_Primitive_Operations flag on the | |
8605 | -- type, and set Is_Primitive to True (otherwise set to False). Set the | |
8606 | -- corresponding flag on the entity itself for later use. | |
8607 | ||
ec4867fa ES |
8608 | procedure Check_Synchronized_Overriding |
8609 | (Def_Id : Entity_Id; | |
ec4867fa ES |
8610 | Overridden_Subp : out Entity_Id); |
8611 | -- First determine if Def_Id is an entry or a subprogram either defined | |
8612 | -- in the scope of a task or protected type, or is a primitive of such | |
8613 | -- a type. Check whether Def_Id overrides a subprogram of an interface | |
8614 | -- implemented by the synchronized type, return the overridden entity | |
8615 | -- or Empty. | |
758c442c | 8616 | |
996ae0b0 RK |
8617 | function Is_Private_Declaration (E : Entity_Id) return Boolean; |
8618 | -- Check that E is declared in the private part of the current package, | |
8619 | -- or in the package body, where it may hide a previous declaration. | |
fbf5a39b | 8620 | -- We can't use In_Private_Part by itself because this flag is also |
996ae0b0 RK |
8621 | -- set when freezing entities, so we must examine the place of the |
8622 | -- declaration in the tree, and recognize wrapper packages as well. | |
8623 | ||
2ddc2000 AC |
8624 | function Is_Overriding_Alias |
8625 | (Old_E : Entity_Id; | |
8626 | New_E : Entity_Id) return Boolean; | |
8627 | -- Check whether new subprogram and old subprogram are both inherited | |
8628 | -- from subprograms that have distinct dispatch table entries. This can | |
2995860f AC |
8629 | -- occur with derivations from instances with accidental homonyms. The |
8630 | -- function is conservative given that the converse is only true within | |
8631 | -- instances that contain accidental overloadings. | |
2ddc2000 | 8632 | |
5d37ba92 ES |
8633 | ------------------------------------ |
8634 | -- Check_For_Primitive_Subprogram -- | |
8635 | ------------------------------------ | |
996ae0b0 | 8636 | |
5d37ba92 ES |
8637 | procedure Check_For_Primitive_Subprogram |
8638 | (Is_Primitive : out Boolean; | |
8639 | Is_Overriding : Boolean := False) | |
ec4867fa | 8640 | is |
996ae0b0 RK |
8641 | Formal : Entity_Id; |
8642 | F_Typ : Entity_Id; | |
07fc65c4 | 8643 | B_Typ : Entity_Id; |
996ae0b0 RK |
8644 | |
8645 | function Visible_Part_Type (T : Entity_Id) return Boolean; | |
8dbd1460 AC |
8646 | -- Returns true if T is declared in the visible part of the current |
8647 | -- package scope; otherwise returns false. Assumes that T is declared | |
8648 | -- in a package. | |
996ae0b0 RK |
8649 | |
8650 | procedure Check_Private_Overriding (T : Entity_Id); | |
8651 | -- Checks that if a primitive abstract subprogram of a visible | |
8dbd1460 AC |
8652 | -- abstract type is declared in a private part, then it must override |
8653 | -- an abstract subprogram declared in the visible part. Also checks | |
8654 | -- that if a primitive function with a controlling result is declared | |
8655 | -- in a private part, then it must override a function declared in | |
8656 | -- the visible part. | |
996ae0b0 RK |
8657 | |
8658 | ------------------------------ | |
8659 | -- Check_Private_Overriding -- | |
8660 | ------------------------------ | |
8661 | ||
8662 | procedure Check_Private_Overriding (T : Entity_Id) is | |
acf624f2 | 8663 | |
aaeb3b3a AC |
8664 | function Overrides_Visible_Function |
8665 | (Partial_View : Entity_Id) return Boolean; | |
acf624f2 BD |
8666 | -- True if S overrides a function in the visible part. The |
8667 | -- overridden function could be explicitly or implicitly declared. | |
8668 | ||
aaeb3b3a AC |
8669 | function Overrides_Visible_Function |
8670 | (Partial_View : Entity_Id) return Boolean | |
8671 | is | |
acf624f2 BD |
8672 | begin |
8673 | if not Is_Overriding or else not Has_Homonym (S) then | |
8674 | return False; | |
8675 | end if; | |
8676 | ||
aaeb3b3a | 8677 | if not Present (Partial_View) then |
acf624f2 BD |
8678 | return True; |
8679 | end if; | |
8680 | ||
8681 | -- Search through all the homonyms H of S in the current | |
8682 | -- package spec, and return True if we find one that matches. | |
8683 | -- Note that Parent (H) will be the declaration of the | |
aaeb3b3a | 8684 | -- partial view of T for a match. |
acf624f2 BD |
8685 | |
8686 | declare | |
8687 | H : Entity_Id := S; | |
8688 | begin | |
8689 | loop | |
8690 | H := Homonym (H); | |
8691 | exit when not Present (H) or else Scope (H) /= Scope (S); | |
8692 | ||
8693 | if Nkind_In | |
8694 | (Parent (H), | |
8695 | N_Private_Extension_Declaration, | |
8696 | N_Private_Type_Declaration) | |
aaeb3b3a | 8697 | and then Defining_Identifier (Parent (H)) = Partial_View |
acf624f2 BD |
8698 | then |
8699 | return True; | |
8700 | end if; | |
8701 | end loop; | |
8702 | end; | |
8703 | ||
8704 | return False; | |
8705 | end Overrides_Visible_Function; | |
8706 | ||
8707 | -- Start of processing for Check_Private_Overriding | |
8708 | ||
996ae0b0 | 8709 | begin |
51c16e29 | 8710 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
8711 | and then In_Private_Part (Current_Scope) |
8712 | and then Visible_Part_Type (T) | |
8713 | and then not In_Instance | |
8714 | then | |
f937473f RD |
8715 | if Is_Abstract_Type (T) |
8716 | and then Is_Abstract_Subprogram (S) | |
8717 | and then (not Is_Overriding | |
8dbd1460 | 8718 | or else not Is_Abstract_Subprogram (E)) |
996ae0b0 | 8719 | then |
3ccedacc AC |
8720 | Error_Msg_N ("abstract subprograms must be visible " |
8721 | & "(RM 3.9.3(10))!", S); | |
758c442c | 8722 | |
aaeb3b3a AC |
8723 | elsif Ekind (S) = E_Function then |
8724 | declare | |
8725 | Partial_View : constant Entity_Id := | |
8726 | Incomplete_Or_Partial_View (T); | |
2e79de51 | 8727 | |
aaeb3b3a AC |
8728 | begin |
8729 | if not Overrides_Visible_Function (Partial_View) then | |
8730 | ||
8731 | -- Here, S is "function ... return T;" declared in | |
8732 | -- the private part, not overriding some visible | |
8733 | -- operation. That's illegal in the tagged case | |
8734 | -- (but not if the private type is untagged). | |
8735 | ||
8736 | if ((Present (Partial_View) | |
8737 | and then Is_Tagged_Type (Partial_View)) | |
8738 | or else (not Present (Partial_View) | |
8739 | and then Is_Tagged_Type (T))) | |
8740 | and then T = Base_Type (Etype (S)) | |
8741 | then | |
8742 | Error_Msg_N | |
8743 | ("private function with tagged result must" | |
8744 | & " override visible-part function", S); | |
8745 | Error_Msg_N | |
8746 | ("\move subprogram to the visible part" | |
8747 | & " (RM 3.9.3(10))", S); | |
8748 | ||
8749 | -- AI05-0073: extend this test to the case of a | |
8750 | -- function with a controlling access result. | |
8751 | ||
8752 | elsif Ekind (Etype (S)) = E_Anonymous_Access_Type | |
8753 | and then Is_Tagged_Type (Designated_Type (Etype (S))) | |
8754 | and then | |
8755 | not Is_Class_Wide_Type | |
8756 | (Designated_Type (Etype (S))) | |
8757 | and then Ada_Version >= Ada_2012 | |
8758 | then | |
8759 | Error_Msg_N | |
8760 | ("private function with controlling access " | |
8761 | & "result must override visible-part function", | |
8762 | S); | |
8763 | Error_Msg_N | |
8764 | ("\move subprogram to the visible part" | |
8765 | & " (RM 3.9.3(10))", S); | |
8766 | end if; | |
8767 | end if; | |
8768 | end; | |
996ae0b0 RK |
8769 | end if; |
8770 | end if; | |
8771 | end Check_Private_Overriding; | |
8772 | ||
8773 | ----------------------- | |
8774 | -- Visible_Part_Type -- | |
8775 | ----------------------- | |
8776 | ||
8777 | function Visible_Part_Type (T : Entity_Id) return Boolean is | |
07fc65c4 GB |
8778 | P : constant Node_Id := Unit_Declaration_Node (Scope (T)); |
8779 | N : Node_Id; | |
996ae0b0 RK |
8780 | |
8781 | begin | |
8dbd1460 AC |
8782 | -- If the entity is a private type, then it must be declared in a |
8783 | -- visible part. | |
996ae0b0 RK |
8784 | |
8785 | if Ekind (T) in Private_Kind then | |
8786 | return True; | |
8787 | end if; | |
8788 | ||
8789 | -- Otherwise, we traverse the visible part looking for its | |
8790 | -- corresponding declaration. We cannot use the declaration | |
8791 | -- node directly because in the private part the entity of a | |
8792 | -- private type is the one in the full view, which does not | |
8793 | -- indicate that it is the completion of something visible. | |
8794 | ||
07fc65c4 | 8795 | N := First (Visible_Declarations (Specification (P))); |
996ae0b0 RK |
8796 | while Present (N) loop |
8797 | if Nkind (N) = N_Full_Type_Declaration | |
8798 | and then Present (Defining_Identifier (N)) | |
8799 | and then T = Defining_Identifier (N) | |
8800 | then | |
8801 | return True; | |
8802 | ||
800621e0 RD |
8803 | elsif Nkind_In (N, N_Private_Type_Declaration, |
8804 | N_Private_Extension_Declaration) | |
996ae0b0 RK |
8805 | and then Present (Defining_Identifier (N)) |
8806 | and then T = Full_View (Defining_Identifier (N)) | |
8807 | then | |
8808 | return True; | |
8809 | end if; | |
8810 | ||
8811 | Next (N); | |
8812 | end loop; | |
8813 | ||
8814 | return False; | |
8815 | end Visible_Part_Type; | |
8816 | ||
5d37ba92 | 8817 | -- Start of processing for Check_For_Primitive_Subprogram |
996ae0b0 RK |
8818 | |
8819 | begin | |
5d37ba92 ES |
8820 | Is_Primitive := False; |
8821 | ||
996ae0b0 RK |
8822 | if not Comes_From_Source (S) then |
8823 | null; | |
8824 | ||
5d37ba92 | 8825 | -- If subprogram is at library level, it is not primitive operation |
15ce9ca2 AC |
8826 | |
8827 | elsif Current_Scope = Standard_Standard then | |
8828 | null; | |
8829 | ||
b9b2405f | 8830 | elsif (Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 | 8831 | and then not In_Package_Body (Current_Scope)) |
82c80734 | 8832 | or else Is_Overriding |
996ae0b0 | 8833 | then |
07fc65c4 | 8834 | -- For function, check return type |
996ae0b0 | 8835 | |
07fc65c4 | 8836 | if Ekind (S) = E_Function then |
5d37ba92 ES |
8837 | if Ekind (Etype (S)) = E_Anonymous_Access_Type then |
8838 | F_Typ := Designated_Type (Etype (S)); | |
8839 | else | |
8840 | F_Typ := Etype (S); | |
8841 | end if; | |
8842 | ||
8843 | B_Typ := Base_Type (F_Typ); | |
07fc65c4 | 8844 | |
5d37ba92 ES |
8845 | if Scope (B_Typ) = Current_Scope |
8846 | and then not Is_Class_Wide_Type (B_Typ) | |
8847 | and then not Is_Generic_Type (B_Typ) | |
8848 | then | |
8849 | Is_Primitive := True; | |
07fc65c4 | 8850 | Set_Has_Primitive_Operations (B_Typ); |
5d37ba92 | 8851 | Set_Is_Primitive (S); |
07fc65c4 GB |
8852 | Check_Private_Overriding (B_Typ); |
8853 | end if; | |
996ae0b0 RK |
8854 | end if; |
8855 | ||
07fc65c4 | 8856 | -- For all subprograms, check formals |
996ae0b0 | 8857 | |
07fc65c4 | 8858 | Formal := First_Formal (S); |
996ae0b0 RK |
8859 | while Present (Formal) loop |
8860 | if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then | |
8861 | F_Typ := Designated_Type (Etype (Formal)); | |
8862 | else | |
8863 | F_Typ := Etype (Formal); | |
8864 | end if; | |
8865 | ||
07fc65c4 GB |
8866 | B_Typ := Base_Type (F_Typ); |
8867 | ||
ec4867fa ES |
8868 | if Ekind (B_Typ) = E_Access_Subtype then |
8869 | B_Typ := Base_Type (B_Typ); | |
8870 | end if; | |
8871 | ||
5d37ba92 ES |
8872 | if Scope (B_Typ) = Current_Scope |
8873 | and then not Is_Class_Wide_Type (B_Typ) | |
8874 | and then not Is_Generic_Type (B_Typ) | |
8875 | then | |
8876 | Is_Primitive := True; | |
8877 | Set_Is_Primitive (S); | |
07fc65c4 GB |
8878 | Set_Has_Primitive_Operations (B_Typ); |
8879 | Check_Private_Overriding (B_Typ); | |
996ae0b0 RK |
8880 | end if; |
8881 | ||
8882 | Next_Formal (Formal); | |
8883 | end loop; | |
1aee1fb3 AC |
8884 | |
8885 | -- Special case: An equality function can be redefined for a type | |
8886 | -- occurring in a declarative part, and won't otherwise be treated as | |
8887 | -- a primitive because it doesn't occur in a package spec and doesn't | |
8888 | -- override an inherited subprogram. It's important that we mark it | |
8889 | -- primitive so it can be returned by Collect_Primitive_Operations | |
8890 | -- and be used in composing the equality operation of later types | |
8891 | -- that have a component of the type. | |
8892 | ||
8893 | elsif Chars (S) = Name_Op_Eq | |
8894 | and then Etype (S) = Standard_Boolean | |
8895 | then | |
8896 | B_Typ := Base_Type (Etype (First_Formal (S))); | |
8897 | ||
8898 | if Scope (B_Typ) = Current_Scope | |
8899 | and then | |
8900 | Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ | |
8901 | and then not Is_Limited_Type (B_Typ) | |
8902 | then | |
8903 | Is_Primitive := True; | |
8904 | Set_Is_Primitive (S); | |
8905 | Set_Has_Primitive_Operations (B_Typ); | |
8906 | Check_Private_Overriding (B_Typ); | |
8907 | end if; | |
996ae0b0 | 8908 | end if; |
5d37ba92 ES |
8909 | end Check_For_Primitive_Subprogram; |
8910 | ||
8911 | ----------------------------------- | |
8912 | -- Check_Synchronized_Overriding -- | |
8913 | ----------------------------------- | |
8914 | ||
8915 | procedure Check_Synchronized_Overriding | |
8916 | (Def_Id : Entity_Id; | |
5d37ba92 ES |
8917 | Overridden_Subp : out Entity_Id) |
8918 | is | |
5d37ba92 ES |
8919 | Ifaces_List : Elist_Id; |
8920 | In_Scope : Boolean; | |
8921 | Typ : Entity_Id; | |
8922 | ||
8aa15e3b JM |
8923 | function Matches_Prefixed_View_Profile |
8924 | (Prim_Params : List_Id; | |
8925 | Iface_Params : List_Id) return Boolean; | |
8926 | -- Determine whether a subprogram's parameter profile Prim_Params | |
8927 | -- matches that of a potentially overridden interface subprogram | |
8928 | -- Iface_Params. Also determine if the type of first parameter of | |
8929 | -- Iface_Params is an implemented interface. | |
8930 | ||
8aa15e3b JM |
8931 | ----------------------------------- |
8932 | -- Matches_Prefixed_View_Profile -- | |
8933 | ----------------------------------- | |
8934 | ||
8935 | function Matches_Prefixed_View_Profile | |
8936 | (Prim_Params : List_Id; | |
8937 | Iface_Params : List_Id) return Boolean | |
8938 | is | |
8939 | Iface_Id : Entity_Id; | |
8940 | Iface_Param : Node_Id; | |
8941 | Iface_Typ : Entity_Id; | |
8942 | Prim_Id : Entity_Id; | |
8943 | Prim_Param : Node_Id; | |
8944 | Prim_Typ : Entity_Id; | |
8945 | ||
8946 | function Is_Implemented | |
8947 | (Ifaces_List : Elist_Id; | |
8948 | Iface : Entity_Id) return Boolean; | |
8949 | -- Determine if Iface is implemented by the current task or | |
8950 | -- protected type. | |
8951 | ||
8952 | -------------------- | |
8953 | -- Is_Implemented -- | |
8954 | -------------------- | |
8955 | ||
8956 | function Is_Implemented | |
8957 | (Ifaces_List : Elist_Id; | |
8958 | Iface : Entity_Id) return Boolean | |
8959 | is | |
8960 | Iface_Elmt : Elmt_Id; | |
8961 | ||
8962 | begin | |
8963 | Iface_Elmt := First_Elmt (Ifaces_List); | |
8964 | while Present (Iface_Elmt) loop | |
8965 | if Node (Iface_Elmt) = Iface then | |
8966 | return True; | |
8967 | end if; | |
8968 | ||
8969 | Next_Elmt (Iface_Elmt); | |
8970 | end loop; | |
8971 | ||
8972 | return False; | |
8973 | end Is_Implemented; | |
8974 | ||
8975 | -- Start of processing for Matches_Prefixed_View_Profile | |
8976 | ||
8977 | begin | |
8978 | Iface_Param := First (Iface_Params); | |
8979 | Iface_Typ := Etype (Defining_Identifier (Iface_Param)); | |
8980 | ||
8981 | if Is_Access_Type (Iface_Typ) then | |
8982 | Iface_Typ := Designated_Type (Iface_Typ); | |
8983 | end if; | |
8984 | ||
8985 | Prim_Param := First (Prim_Params); | |
8986 | ||
8987 | -- The first parameter of the potentially overridden subprogram | |
8988 | -- must be an interface implemented by Prim. | |
8989 | ||
8990 | if not Is_Interface (Iface_Typ) | |
8991 | or else not Is_Implemented (Ifaces_List, Iface_Typ) | |
8992 | then | |
8993 | return False; | |
8994 | end if; | |
8995 | ||
8996 | -- The checks on the object parameters are done, move onto the | |
8997 | -- rest of the parameters. | |
8998 | ||
8999 | if not In_Scope then | |
9000 | Prim_Param := Next (Prim_Param); | |
9001 | end if; | |
9002 | ||
9003 | Iface_Param := Next (Iface_Param); | |
9004 | while Present (Iface_Param) and then Present (Prim_Param) loop | |
9005 | Iface_Id := Defining_Identifier (Iface_Param); | |
9006 | Iface_Typ := Find_Parameter_Type (Iface_Param); | |
9007 | ||
8aa15e3b JM |
9008 | Prim_Id := Defining_Identifier (Prim_Param); |
9009 | Prim_Typ := Find_Parameter_Type (Prim_Param); | |
9010 | ||
15e4986c JM |
9011 | if Ekind (Iface_Typ) = E_Anonymous_Access_Type |
9012 | and then Ekind (Prim_Typ) = E_Anonymous_Access_Type | |
9013 | and then Is_Concurrent_Type (Designated_Type (Prim_Typ)) | |
9014 | then | |
9015 | Iface_Typ := Designated_Type (Iface_Typ); | |
9016 | Prim_Typ := Designated_Type (Prim_Typ); | |
8aa15e3b JM |
9017 | end if; |
9018 | ||
9019 | -- Case of multiple interface types inside a parameter profile | |
9020 | ||
9021 | -- (Obj_Param : in out Iface; ...; Param : Iface) | |
9022 | ||
9023 | -- If the interface type is implemented, then the matching type | |
9024 | -- in the primitive should be the implementing record type. | |
9025 | ||
9026 | if Ekind (Iface_Typ) = E_Record_Type | |
9027 | and then Is_Interface (Iface_Typ) | |
9028 | and then Is_Implemented (Ifaces_List, Iface_Typ) | |
9029 | then | |
9030 | if Prim_Typ /= Typ then | |
9031 | return False; | |
9032 | end if; | |
9033 | ||
9034 | -- The two parameters must be both mode and subtype conformant | |
9035 | ||
9036 | elsif Ekind (Iface_Id) /= Ekind (Prim_Id) | |
9037 | or else not | |
9038 | Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant) | |
9039 | then | |
9040 | return False; | |
9041 | end if; | |
9042 | ||
9043 | Next (Iface_Param); | |
9044 | Next (Prim_Param); | |
9045 | end loop; | |
9046 | ||
9047 | -- One of the two lists contains more parameters than the other | |
9048 | ||
9049 | if Present (Iface_Param) or else Present (Prim_Param) then | |
9050 | return False; | |
9051 | end if; | |
9052 | ||
9053 | return True; | |
9054 | end Matches_Prefixed_View_Profile; | |
9055 | ||
9056 | -- Start of processing for Check_Synchronized_Overriding | |
9057 | ||
5d37ba92 ES |
9058 | begin |
9059 | Overridden_Subp := Empty; | |
9060 | ||
8aa15e3b JM |
9061 | -- Def_Id must be an entry or a subprogram. We should skip predefined |
9062 | -- primitives internally generated by the frontend; however at this | |
9063 | -- stage predefined primitives are still not fully decorated. As a | |
9064 | -- minor optimization we skip here internally generated subprograms. | |
5d37ba92 | 9065 | |
8aa15e3b JM |
9066 | if (Ekind (Def_Id) /= E_Entry |
9067 | and then Ekind (Def_Id) /= E_Function | |
9068 | and then Ekind (Def_Id) /= E_Procedure) | |
9069 | or else not Comes_From_Source (Def_Id) | |
5d37ba92 ES |
9070 | then |
9071 | return; | |
9072 | end if; | |
9073 | ||
9074 | -- Search for the concurrent declaration since it contains the list | |
9075 | -- of all implemented interfaces. In this case, the subprogram is | |
9076 | -- declared within the scope of a protected or a task type. | |
9077 | ||
9078 | if Present (Scope (Def_Id)) | |
9079 | and then Is_Concurrent_Type (Scope (Def_Id)) | |
9080 | and then not Is_Generic_Actual_Type (Scope (Def_Id)) | |
9081 | then | |
9082 | Typ := Scope (Def_Id); | |
9083 | In_Scope := True; | |
9084 | ||
8aa15e3b | 9085 | -- The enclosing scope is not a synchronized type and the subprogram |
4adf3c50 | 9086 | -- has no formals. |
8aa15e3b JM |
9087 | |
9088 | elsif No (First_Formal (Def_Id)) then | |
9089 | return; | |
5d37ba92 | 9090 | |
8aa15e3b | 9091 | -- The subprogram has formals and hence it may be a primitive of a |
4adf3c50 | 9092 | -- concurrent type. |
5d37ba92 | 9093 | |
8aa15e3b JM |
9094 | else |
9095 | Typ := Etype (First_Formal (Def_Id)); | |
9096 | ||
9097 | if Is_Access_Type (Typ) then | |
9098 | Typ := Directly_Designated_Type (Typ); | |
8c3dd7a8 JM |
9099 | end if; |
9100 | ||
8aa15e3b JM |
9101 | if Is_Concurrent_Type (Typ) |
9102 | and then not Is_Generic_Actual_Type (Typ) | |
5d37ba92 | 9103 | then |
5d37ba92 ES |
9104 | In_Scope := False; |
9105 | ||
9106 | -- This case occurs when the concurrent type is declared within | |
9107 | -- a generic unit. As a result the corresponding record has been | |
9108 | -- built and used as the type of the first formal, we just have | |
9109 | -- to retrieve the corresponding concurrent type. | |
9110 | ||
8aa15e3b | 9111 | elsif Is_Concurrent_Record_Type (Typ) |
dd54644b | 9112 | and then not Is_Class_Wide_Type (Typ) |
8aa15e3b | 9113 | and then Present (Corresponding_Concurrent_Type (Typ)) |
5d37ba92 | 9114 | then |
8aa15e3b | 9115 | Typ := Corresponding_Concurrent_Type (Typ); |
5d37ba92 ES |
9116 | In_Scope := False; |
9117 | ||
9118 | else | |
9119 | return; | |
9120 | end if; | |
8aa15e3b JM |
9121 | end if; |
9122 | ||
9123 | -- There is no overriding to check if is an inherited operation in a | |
9124 | -- type derivation on for a generic actual. | |
9125 | ||
9126 | Collect_Interfaces (Typ, Ifaces_List); | |
9127 | ||
9128 | if Is_Empty_Elmt_List (Ifaces_List) then | |
5d37ba92 ES |
9129 | return; |
9130 | end if; | |
9131 | ||
8aa15e3b JM |
9132 | -- Determine whether entry or subprogram Def_Id overrides a primitive |
9133 | -- operation that belongs to one of the interfaces in Ifaces_List. | |
5d37ba92 | 9134 | |
8aa15e3b JM |
9135 | declare |
9136 | Candidate : Entity_Id := Empty; | |
9137 | Hom : Entity_Id := Empty; | |
8aa15e3b JM |
9138 | Subp : Entity_Id := Empty; |
9139 | ||
9140 | begin | |
4adf3c50 | 9141 | -- Traverse the homonym chain, looking for a potentially |
8aa15e3b JM |
9142 | -- overridden subprogram that belongs to an implemented |
9143 | -- interface. | |
9144 | ||
9145 | Hom := Current_Entity_In_Scope (Def_Id); | |
9146 | while Present (Hom) loop | |
9147 | Subp := Hom; | |
9148 | ||
15e4986c JM |
9149 | if Subp = Def_Id |
9150 | or else not Is_Overloadable (Subp) | |
9151 | or else not Is_Primitive (Subp) | |
9152 | or else not Is_Dispatching_Operation (Subp) | |
79afa047 | 9153 | or else not Present (Find_Dispatching_Type (Subp)) |
15e4986c | 9154 | or else not Is_Interface (Find_Dispatching_Type (Subp)) |
8aa15e3b | 9155 | then |
15e4986c | 9156 | null; |
8aa15e3b | 9157 | |
15e4986c | 9158 | -- Entries and procedures can override abstract or null |
4adf3c50 | 9159 | -- interface procedures. |
8aa15e3b | 9160 | |
15e4986c | 9161 | elsif (Ekind (Def_Id) = E_Procedure |
8fde064e | 9162 | or else Ekind (Def_Id) = E_Entry) |
8aa15e3b | 9163 | and then Ekind (Subp) = E_Procedure |
8aa15e3b JM |
9164 | and then Matches_Prefixed_View_Profile |
9165 | (Parameter_Specifications (Parent (Def_Id)), | |
9166 | Parameter_Specifications (Parent (Subp))) | |
9167 | then | |
9168 | Candidate := Subp; | |
9169 | ||
15e4986c JM |
9170 | -- For an overridden subprogram Subp, check whether the mode |
9171 | -- of its first parameter is correct depending on the kind | |
9172 | -- of synchronized type. | |
8aa15e3b | 9173 | |
15e4986c JM |
9174 | declare |
9175 | Formal : constant Node_Id := First_Formal (Candidate); | |
9176 | ||
9177 | begin | |
9178 | -- In order for an entry or a protected procedure to | |
9179 | -- override, the first parameter of the overridden | |
9180 | -- routine must be of mode "out", "in out" or | |
9181 | -- access-to-variable. | |
9182 | ||
8fde064e | 9183 | if Ekind_In (Candidate, E_Entry, E_Procedure) |
15e4986c JM |
9184 | and then Is_Protected_Type (Typ) |
9185 | and then Ekind (Formal) /= E_In_Out_Parameter | |
9186 | and then Ekind (Formal) /= E_Out_Parameter | |
8fde064e AC |
9187 | and then Nkind (Parameter_Type (Parent (Formal))) /= |
9188 | N_Access_Definition | |
15e4986c JM |
9189 | then |
9190 | null; | |
9191 | ||
9192 | -- All other cases are OK since a task entry or routine | |
9193 | -- does not have a restriction on the mode of the first | |
9194 | -- parameter of the overridden interface routine. | |
9195 | ||
9196 | else | |
9197 | Overridden_Subp := Candidate; | |
9198 | return; | |
9199 | end if; | |
9200 | end; | |
8aa15e3b JM |
9201 | |
9202 | -- Functions can override abstract interface functions | |
9203 | ||
9204 | elsif Ekind (Def_Id) = E_Function | |
9205 | and then Ekind (Subp) = E_Function | |
8aa15e3b JM |
9206 | and then Matches_Prefixed_View_Profile |
9207 | (Parameter_Specifications (Parent (Def_Id)), | |
9208 | Parameter_Specifications (Parent (Subp))) | |
9209 | and then Etype (Result_Definition (Parent (Def_Id))) = | |
9210 | Etype (Result_Definition (Parent (Subp))) | |
9211 | then | |
273123a4 AC |
9212 | Candidate := Subp; |
9213 | ||
9214 | -- If an inherited subprogram is implemented by a protected | |
9215 | -- function, then the first parameter of the inherited | |
9216 | -- subprogram shall be of mode in, but not an | |
9217 | -- access-to-variable parameter (RM 9.4(11/9) | |
9218 | ||
9219 | if Present (First_Formal (Subp)) | |
9220 | and then Ekind (First_Formal (Subp)) = E_In_Parameter | |
9221 | and then | |
9222 | (not Is_Access_Type (Etype (First_Formal (Subp))) | |
9223 | or else | |
9224 | Is_Access_Constant (Etype (First_Formal (Subp)))) | |
9225 | then | |
9226 | Overridden_Subp := Subp; | |
9227 | return; | |
9228 | end if; | |
8aa15e3b JM |
9229 | end if; |
9230 | ||
9231 | Hom := Homonym (Hom); | |
9232 | end loop; | |
9233 | ||
4adf3c50 AC |
9234 | -- After examining all candidates for overriding, we are left with |
9235 | -- the best match which is a mode incompatible interface routine. | |
8aa15e3b | 9236 | |
273123a4 AC |
9237 | if In_Scope and then Present (Candidate) then |
9238 | Error_Msg_PT (Def_Id, Candidate); | |
5d37ba92 | 9239 | end if; |
8aa15e3b JM |
9240 | |
9241 | Overridden_Subp := Candidate; | |
9242 | return; | |
9243 | end; | |
5d37ba92 ES |
9244 | end Check_Synchronized_Overriding; |
9245 | ||
9246 | ---------------------------- | |
9247 | -- Is_Private_Declaration -- | |
9248 | ---------------------------- | |
9249 | ||
9250 | function Is_Private_Declaration (E : Entity_Id) return Boolean is | |
9251 | Priv_Decls : List_Id; | |
9252 | Decl : constant Node_Id := Unit_Declaration_Node (E); | |
9253 | ||
9254 | begin | |
9255 | if Is_Package_Or_Generic_Package (Current_Scope) | |
9256 | and then In_Private_Part (Current_Scope) | |
9257 | then | |
9258 | Priv_Decls := | |
d12b19fa | 9259 | Private_Declarations (Package_Specification (Current_Scope)); |
5d37ba92 ES |
9260 | |
9261 | return In_Package_Body (Current_Scope) | |
9262 | or else | |
9263 | (Is_List_Member (Decl) | |
a4901c08 | 9264 | and then List_Containing (Decl) = Priv_Decls) |
5d37ba92 | 9265 | or else (Nkind (Parent (Decl)) = N_Package_Specification |
a4901c08 AC |
9266 | and then not |
9267 | Is_Compilation_Unit | |
9268 | (Defining_Entity (Parent (Decl))) | |
9269 | and then List_Containing (Parent (Parent (Decl))) = | |
9270 | Priv_Decls); | |
5d37ba92 ES |
9271 | else |
9272 | return False; | |
9273 | end if; | |
9274 | end Is_Private_Declaration; | |
996ae0b0 | 9275 | |
2ddc2000 AC |
9276 | -------------------------- |
9277 | -- Is_Overriding_Alias -- | |
9278 | -------------------------- | |
9279 | ||
9280 | function Is_Overriding_Alias | |
9281 | (Old_E : Entity_Id; | |
9282 | New_E : Entity_Id) return Boolean | |
9283 | is | |
9284 | AO : constant Entity_Id := Alias (Old_E); | |
9285 | AN : constant Entity_Id := Alias (New_E); | |
2ddc2000 AC |
9286 | begin |
9287 | return Scope (AO) /= Scope (AN) | |
9288 | or else No (DTC_Entity (AO)) | |
9289 | or else No (DTC_Entity (AN)) | |
9290 | or else DT_Position (AO) = DT_Position (AN); | |
9291 | end Is_Overriding_Alias; | |
9292 | ||
996ae0b0 RK |
9293 | -- Start of processing for New_Overloaded_Entity |
9294 | ||
9295 | begin | |
fbf5a39b AC |
9296 | -- We need to look for an entity that S may override. This must be a |
9297 | -- homonym in the current scope, so we look for the first homonym of | |
9298 | -- S in the current scope as the starting point for the search. | |
9299 | ||
9300 | E := Current_Entity_In_Scope (S); | |
9301 | ||
947430d5 AC |
9302 | -- Ada 2005 (AI-251): Derivation of abstract interface primitives. |
9303 | -- They are directly added to the list of primitive operations of | |
9304 | -- Derived_Type, unless this is a rederivation in the private part | |
9305 | -- of an operation that was already derived in the visible part of | |
9306 | -- the current package. | |
9307 | ||
0791fbe9 | 9308 | if Ada_Version >= Ada_2005 |
947430d5 AC |
9309 | and then Present (Derived_Type) |
9310 | and then Present (Alias (S)) | |
9311 | and then Is_Dispatching_Operation (Alias (S)) | |
9312 | and then Present (Find_Dispatching_Type (Alias (S))) | |
9313 | and then Is_Interface (Find_Dispatching_Type (Alias (S))) | |
9314 | then | |
9315 | -- For private types, when the full-view is processed we propagate to | |
9316 | -- the full view the non-overridden entities whose attribute "alias" | |
9317 | -- references an interface primitive. These entities were added by | |
9318 | -- Derive_Subprograms to ensure that interface primitives are | |
9319 | -- covered. | |
9320 | ||
9321 | -- Inside_Freeze_Actions is non zero when S corresponds with an | |
9322 | -- internal entity that links an interface primitive with its | |
9323 | -- covering primitive through attribute Interface_Alias (see | |
4adf3c50 | 9324 | -- Add_Internal_Interface_Entities). |
947430d5 AC |
9325 | |
9326 | if Inside_Freezing_Actions = 0 | |
9327 | and then Is_Package_Or_Generic_Package (Current_Scope) | |
9328 | and then In_Private_Part (Current_Scope) | |
9329 | and then Nkind (Parent (E)) = N_Private_Extension_Declaration | |
9330 | and then Nkind (Parent (S)) = N_Full_Type_Declaration | |
9331 | and then Full_View (Defining_Identifier (Parent (E))) | |
9332 | = Defining_Identifier (Parent (S)) | |
9333 | and then Alias (E) = Alias (S) | |
9334 | then | |
9335 | Check_Operation_From_Private_View (S, E); | |
9336 | Set_Is_Dispatching_Operation (S); | |
9337 | ||
9338 | -- Common case | |
9339 | ||
9340 | else | |
9341 | Enter_Overloaded_Entity (S); | |
9342 | Check_Dispatching_Operation (S, Empty); | |
9343 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); | |
9344 | end if; | |
9345 | ||
9346 | return; | |
9347 | end if; | |
9348 | ||
fbf5a39b AC |
9349 | -- If there is no homonym then this is definitely not overriding |
9350 | ||
996ae0b0 RK |
9351 | if No (E) then |
9352 | Enter_Overloaded_Entity (S); | |
9353 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 9354 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
996ae0b0 | 9355 | |
2995860f AC |
9356 | -- If subprogram has an explicit declaration, check whether it has an |
9357 | -- overriding indicator. | |
758c442c | 9358 | |
ec4867fa | 9359 | if Comes_From_Source (S) then |
8aa15e3b | 9360 | Check_Synchronized_Overriding (S, Overridden_Subp); |
ea034236 AC |
9361 | |
9362 | -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then | |
9363 | -- it may have overridden some hidden inherited primitive. Update | |
308e6f3a | 9364 | -- Overridden_Subp to avoid spurious errors when checking the |
ea034236 AC |
9365 | -- overriding indicator. |
9366 | ||
9367 | if Ada_Version >= Ada_2012 | |
9368 | and then No (Overridden_Subp) | |
9369 | and then Is_Dispatching_Operation (S) | |
038140ed | 9370 | and then Present (Overridden_Operation (S)) |
ea034236 AC |
9371 | then |
9372 | Overridden_Subp := Overridden_Operation (S); | |
9373 | end if; | |
9374 | ||
5d37ba92 ES |
9375 | Check_Overriding_Indicator |
9376 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
758c442c GD |
9377 | end if; |
9378 | ||
fbf5a39b AC |
9379 | -- If there is a homonym that is not overloadable, then we have an |
9380 | -- error, except for the special cases checked explicitly below. | |
9381 | ||
996ae0b0 RK |
9382 | elsif not Is_Overloadable (E) then |
9383 | ||
9384 | -- Check for spurious conflict produced by a subprogram that has the | |
9385 | -- same name as that of the enclosing generic package. The conflict | |
9386 | -- occurs within an instance, between the subprogram and the renaming | |
9387 | -- declaration for the package. After the subprogram, the package | |
9388 | -- renaming declaration becomes hidden. | |
9389 | ||
9390 | if Ekind (E) = E_Package | |
9391 | and then Present (Renamed_Object (E)) | |
9392 | and then Renamed_Object (E) = Current_Scope | |
9393 | and then Nkind (Parent (Renamed_Object (E))) = | |
9394 | N_Package_Specification | |
9395 | and then Present (Generic_Parent (Parent (Renamed_Object (E)))) | |
9396 | then | |
9397 | Set_Is_Hidden (E); | |
9398 | Set_Is_Immediately_Visible (E, False); | |
9399 | Enter_Overloaded_Entity (S); | |
9400 | Set_Homonym (S, Homonym (E)); | |
9401 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 9402 | Check_Overriding_Indicator (S, Empty, Is_Primitive => False); |
996ae0b0 RK |
9403 | |
9404 | -- If the subprogram is implicit it is hidden by the previous | |
82c80734 RD |
9405 | -- declaration. However if it is dispatching, it must appear in the |
9406 | -- dispatch table anyway, because it can be dispatched to even if it | |
9407 | -- cannot be called directly. | |
996ae0b0 | 9408 | |
4adf3c50 | 9409 | elsif Present (Alias (S)) and then not Comes_From_Source (S) then |
996ae0b0 RK |
9410 | Set_Scope (S, Current_Scope); |
9411 | ||
9412 | if Is_Dispatching_Operation (Alias (S)) then | |
9413 | Check_Dispatching_Operation (S, Empty); | |
9414 | end if; | |
9415 | ||
9416 | return; | |
9417 | ||
9418 | else | |
9419 | Error_Msg_Sloc := Sloc (E); | |
996ae0b0 | 9420 | |
f3d57416 | 9421 | -- Generate message, with useful additional warning if in generic |
996ae0b0 RK |
9422 | |
9423 | if Is_Generic_Unit (E) then | |
5d37ba92 ES |
9424 | Error_Msg_N ("previous generic unit cannot be overloaded", S); |
9425 | Error_Msg_N ("\& conflicts with declaration#", S); | |
9426 | else | |
9427 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
9428 | end if; |
9429 | ||
9430 | return; | |
9431 | end if; | |
9432 | ||
fbf5a39b AC |
9433 | -- E exists and is overloadable |
9434 | ||
996ae0b0 | 9435 | else |
8aa15e3b | 9436 | Check_Synchronized_Overriding (S, Overridden_Subp); |
758c442c | 9437 | |
82c80734 RD |
9438 | -- Loop through E and its homonyms to determine if any of them is |
9439 | -- the candidate for overriding by S. | |
996ae0b0 RK |
9440 | |
9441 | while Present (E) loop | |
fbf5a39b AC |
9442 | |
9443 | -- Definitely not interesting if not in the current scope | |
9444 | ||
996ae0b0 RK |
9445 | if Scope (E) /= Current_Scope then |
9446 | null; | |
9447 | ||
aca90db9 AC |
9448 | -- A function can overload the name of an abstract state. The |
9449 | -- state can be viewed as a function with a profile that cannot | |
9450 | -- be matched by anything. | |
9451 | ||
9452 | elsif Ekind (S) = E_Function | |
9453 | and then Ekind (E) = E_Abstract_State | |
9454 | then | |
9455 | Enter_Overloaded_Entity (S); | |
9456 | return; | |
9457 | ||
2995860f AC |
9458 | -- Ada 2012 (AI05-0165): For internally generated bodies of null |
9459 | -- procedures locate the internally generated spec. We enforce | |
9460 | -- mode conformance since a tagged type may inherit from | |
9461 | -- interfaces several null primitives which differ only in | |
9462 | -- the mode of the formals. | |
25ebc085 AC |
9463 | |
9464 | elsif not Comes_From_Source (S) | |
9465 | and then Is_Null_Procedure (S) | |
9466 | and then not Mode_Conformant (E, S) | |
9467 | then | |
9468 | null; | |
9469 | ||
fbf5a39b AC |
9470 | -- Check if we have type conformance |
9471 | ||
ec4867fa | 9472 | elsif Type_Conformant (E, S) then |
c8ef728f | 9473 | |
82c80734 RD |
9474 | -- If the old and new entities have the same profile and one |
9475 | -- is not the body of the other, then this is an error, unless | |
9476 | -- one of them is implicitly declared. | |
996ae0b0 RK |
9477 | |
9478 | -- There are some cases when both can be implicit, for example | |
9479 | -- when both a literal and a function that overrides it are | |
f3d57416 | 9480 | -- inherited in a derivation, or when an inherited operation |
ec4867fa | 9481 | -- of a tagged full type overrides the inherited operation of |
f3d57416 | 9482 | -- a private extension. Ada 83 had a special rule for the |
885c4871 | 9483 | -- literal case. In Ada 95, the later implicit operation hides |
82c80734 RD |
9484 | -- the former, and the literal is always the former. In the |
9485 | -- odd case where both are derived operations declared at the | |
9486 | -- same point, both operations should be declared, and in that | |
9487 | -- case we bypass the following test and proceed to the next | |
df46b832 AC |
9488 | -- part. This can only occur for certain obscure cases in |
9489 | -- instances, when an operation on a type derived from a formal | |
9490 | -- private type does not override a homograph inherited from | |
9491 | -- the actual. In subsequent derivations of such a type, the | |
9492 | -- DT positions of these operations remain distinct, if they | |
9493 | -- have been set. | |
996ae0b0 RK |
9494 | |
9495 | if Present (Alias (S)) | |
9496 | and then (No (Alias (E)) | |
9497 | or else Comes_From_Source (E) | |
2ddc2000 | 9498 | or else Is_Abstract_Subprogram (S) |
df46b832 AC |
9499 | or else |
9500 | (Is_Dispatching_Operation (E) | |
84c0a895 | 9501 | and then Is_Overriding_Alias (E, S))) |
df46b832 | 9502 | and then Ekind (E) /= E_Enumeration_Literal |
996ae0b0 | 9503 | then |
82c80734 RD |
9504 | -- When an derived operation is overloaded it may be due to |
9505 | -- the fact that the full view of a private extension | |
996ae0b0 RK |
9506 | -- re-inherits. It has to be dealt with. |
9507 | ||
e660dbf7 | 9508 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
9509 | and then In_Private_Part (Current_Scope) |
9510 | then | |
9511 | Check_Operation_From_Private_View (S, E); | |
9512 | end if; | |
9513 | ||
038140ed AC |
9514 | -- In any case the implicit operation remains hidden by the |
9515 | -- existing declaration, which is overriding. Indicate that | |
9516 | -- E overrides the operation from which S is inherited. | |
996ae0b0 | 9517 | |
038140ed | 9518 | if Present (Alias (S)) then |
039538bc AC |
9519 | Set_Overridden_Operation (E, Alias (S)); |
9520 | Inherit_Subprogram_Contract (E, Alias (S)); | |
9521 | ||
038140ed | 9522 | else |
039538bc AC |
9523 | Set_Overridden_Operation (E, S); |
9524 | Inherit_Subprogram_Contract (E, S); | |
038140ed | 9525 | end if; |
758c442c GD |
9526 | |
9527 | if Comes_From_Source (E) then | |
5d37ba92 | 9528 | Check_Overriding_Indicator (E, S, Is_Primitive => False); |
758c442c GD |
9529 | end if; |
9530 | ||
996ae0b0 RK |
9531 | return; |
9532 | ||
26a43556 AC |
9533 | -- Within an instance, the renaming declarations for actual |
9534 | -- subprograms may become ambiguous, but they do not hide each | |
9535 | -- other. | |
996ae0b0 RK |
9536 | |
9537 | elsif Ekind (E) /= E_Entry | |
9538 | and then not Comes_From_Source (E) | |
9539 | and then not Is_Generic_Instance (E) | |
9540 | and then (Present (Alias (E)) | |
9541 | or else Is_Intrinsic_Subprogram (E)) | |
9542 | and then (not In_Instance | |
9543 | or else No (Parent (E)) | |
9544 | or else Nkind (Unit_Declaration_Node (E)) /= | |
8dbd1460 | 9545 | N_Subprogram_Renaming_Declaration) |
996ae0b0 | 9546 | then |
26a43556 AC |
9547 | -- A subprogram child unit is not allowed to override an |
9548 | -- inherited subprogram (10.1.1(20)). | |
996ae0b0 RK |
9549 | |
9550 | if Is_Child_Unit (S) then | |
9551 | Error_Msg_N | |
9552 | ("child unit overrides inherited subprogram in parent", | |
9553 | S); | |
9554 | return; | |
9555 | end if; | |
9556 | ||
9557 | if Is_Non_Overriding_Operation (E, S) then | |
9558 | Enter_Overloaded_Entity (S); | |
8dbd1460 | 9559 | |
c8ef728f | 9560 | if No (Derived_Type) |
996ae0b0 RK |
9561 | or else Is_Tagged_Type (Derived_Type) |
9562 | then | |
9563 | Check_Dispatching_Operation (S, Empty); | |
9564 | end if; | |
9565 | ||
9566 | return; | |
9567 | end if; | |
9568 | ||
9569 | -- E is a derived operation or an internal operator which | |
9570 | -- is being overridden. Remove E from further visibility. | |
9571 | -- Furthermore, if E is a dispatching operation, it must be | |
9572 | -- replaced in the list of primitive operations of its type | |
9573 | -- (see Override_Dispatching_Operation). | |
9574 | ||
ec4867fa | 9575 | Overridden_Subp := E; |
758c442c | 9576 | |
996ae0b0 RK |
9577 | declare |
9578 | Prev : Entity_Id; | |
9579 | ||
9580 | begin | |
9581 | Prev := First_Entity (Current_Scope); | |
8fde064e | 9582 | while Present (Prev) and then Next_Entity (Prev) /= E loop |
996ae0b0 RK |
9583 | Next_Entity (Prev); |
9584 | end loop; | |
9585 | ||
9586 | -- It is possible for E to be in the current scope and | |
9587 | -- yet not in the entity chain. This can only occur in a | |
9588 | -- generic context where E is an implicit concatenation | |
9589 | -- in the formal part, because in a generic body the | |
9590 | -- entity chain starts with the formals. | |
9591 | ||
948ed277 AC |
9592 | -- In GNATprove mode, a wrapper for an operation with |
9593 | -- axiomatization may be a homonym of another declaration | |
9594 | -- for an actual subprogram (needs refinement ???). | |
9595 | ||
9596 | if No (Prev) then | |
9597 | if In_Instance | |
9598 | and then GNATprove_Mode | |
9599 | and then | |
9600 | Nkind (Original_Node (Unit_Declaration_Node (S))) = | |
9601 | N_Subprogram_Renaming_Declaration | |
9602 | then | |
9603 | return; | |
9604 | else | |
9605 | pragma Assert (Chars (E) = Name_Op_Concat); | |
9606 | null; | |
9607 | end if; | |
9608 | end if; | |
996ae0b0 RK |
9609 | |
9610 | -- E must be removed both from the entity_list of the | |
948ed277 | 9611 | -- current scope, and from the visibility chain. |
996ae0b0 RK |
9612 | |
9613 | if Debug_Flag_E then | |
9614 | Write_Str ("Override implicit operation "); | |
9615 | Write_Int (Int (E)); | |
9616 | Write_Eol; | |
9617 | end if; | |
9618 | ||
9619 | -- If E is a predefined concatenation, it stands for four | |
9620 | -- different operations. As a result, a single explicit | |
9621 | -- declaration does not hide it. In a possible ambiguous | |
9622 | -- situation, Disambiguate chooses the user-defined op, | |
9623 | -- so it is correct to retain the previous internal one. | |
9624 | ||
9625 | if Chars (E) /= Name_Op_Concat | |
9626 | or else Ekind (E) /= E_Operator | |
9627 | then | |
9628 | -- For nondispatching derived operations that are | |
9629 | -- overridden by a subprogram declared in the private | |
8dbd1460 AC |
9630 | -- part of a package, we retain the derived subprogram |
9631 | -- but mark it as not immediately visible. If the | |
9632 | -- derived operation was declared in the visible part | |
9633 | -- then this ensures that it will still be visible | |
9634 | -- outside the package with the proper signature | |
9635 | -- (calls from outside must also be directed to this | |
9636 | -- version rather than the overriding one, unlike the | |
9637 | -- dispatching case). Calls from inside the package | |
9638 | -- will still resolve to the overriding subprogram | |
9639 | -- since the derived one is marked as not visible | |
9640 | -- within the package. | |
996ae0b0 RK |
9641 | |
9642 | -- If the private operation is dispatching, we achieve | |
9643 | -- the overriding by keeping the implicit operation | |
9865d858 | 9644 | -- but setting its alias to be the overriding one. In |
996ae0b0 RK |
9645 | -- this fashion the proper body is executed in all |
9646 | -- cases, but the original signature is used outside | |
9647 | -- of the package. | |
9648 | ||
9649 | -- If the overriding is not in the private part, we | |
9650 | -- remove the implicit operation altogether. | |
9651 | ||
9652 | if Is_Private_Declaration (S) then | |
996ae0b0 RK |
9653 | if not Is_Dispatching_Operation (E) then |
9654 | Set_Is_Immediately_Visible (E, False); | |
9655 | else | |
e895b435 | 9656 | -- Work done in Override_Dispatching_Operation, |
a46cde68 | 9657 | -- so nothing else needs to be done here. |
996ae0b0 RK |
9658 | |
9659 | null; | |
9660 | end if; | |
996ae0b0 | 9661 | |
fbf5a39b AC |
9662 | else |
9663 | -- Find predecessor of E in Homonym chain | |
996ae0b0 RK |
9664 | |
9665 | if E = Current_Entity (E) then | |
9666 | Prev_Vis := Empty; | |
9667 | else | |
9668 | Prev_Vis := Current_Entity (E); | |
9669 | while Homonym (Prev_Vis) /= E loop | |
9670 | Prev_Vis := Homonym (Prev_Vis); | |
9671 | end loop; | |
9672 | end if; | |
9673 | ||
9674 | if Prev_Vis /= Empty then | |
9675 | ||
9676 | -- Skip E in the visibility chain | |
9677 | ||
9678 | Set_Homonym (Prev_Vis, Homonym (E)); | |
9679 | ||
9680 | else | |
9681 | Set_Name_Entity_Id (Chars (E), Homonym (E)); | |
9682 | end if; | |
9683 | ||
9684 | Set_Next_Entity (Prev, Next_Entity (E)); | |
9685 | ||
9686 | if No (Next_Entity (Prev)) then | |
9687 | Set_Last_Entity (Current_Scope, Prev); | |
9688 | end if; | |
996ae0b0 RK |
9689 | end if; |
9690 | end if; | |
9691 | ||
9692 | Enter_Overloaded_Entity (S); | |
1c1289e7 AC |
9693 | |
9694 | -- For entities generated by Derive_Subprograms the | |
9695 | -- overridden operation is the inherited primitive | |
9696 | -- (which is available through the attribute alias). | |
9697 | ||
9698 | if not (Comes_From_Source (E)) | |
9699 | and then Is_Dispatching_Operation (E) | |
f9673bb0 AC |
9700 | and then Find_Dispatching_Type (E) = |
9701 | Find_Dispatching_Type (S) | |
1c1289e7 AC |
9702 | and then Present (Alias (E)) |
9703 | and then Comes_From_Source (Alias (E)) | |
9704 | then | |
039538bc AC |
9705 | Set_Overridden_Operation (S, Alias (E)); |
9706 | Inherit_Subprogram_Contract (S, Alias (E)); | |
2fe829ae | 9707 | |
6320f5e1 AC |
9708 | -- Normal case of setting entity as overridden |
9709 | ||
9710 | -- Note: Static_Initialization and Overridden_Operation | |
9711 | -- attributes use the same field in subprogram entities. | |
9712 | -- Static_Initialization is only defined for internal | |
9713 | -- initialization procedures, where Overridden_Operation | |
9714 | -- is irrelevant. Therefore the setting of this attribute | |
9715 | -- must check whether the target is an init_proc. | |
9716 | ||
2fe829ae | 9717 | elsif not Is_Init_Proc (S) then |
039538bc AC |
9718 | Set_Overridden_Operation (S, E); |
9719 | Inherit_Subprogram_Contract (S, E); | |
1c1289e7 AC |
9720 | end if; |
9721 | ||
5d37ba92 | 9722 | Check_Overriding_Indicator (S, E, Is_Primitive => True); |
996ae0b0 | 9723 | |
fc53fe76 | 9724 | -- If S is a user-defined subprogram or a null procedure |
38ef8ebe AC |
9725 | -- expanded to override an inherited null procedure, or a |
9726 | -- predefined dispatching primitive then indicate that E | |
038140ed | 9727 | -- overrides the operation from which S is inherited. |
fc53fe76 AC |
9728 | |
9729 | if Comes_From_Source (S) | |
9730 | or else | |
9731 | (Present (Parent (S)) | |
9732 | and then | |
9733 | Nkind (Parent (S)) = N_Procedure_Specification | |
9734 | and then | |
9735 | Null_Present (Parent (S))) | |
38ef8ebe AC |
9736 | or else |
9737 | (Present (Alias (E)) | |
f16e8df9 RD |
9738 | and then |
9739 | Is_Predefined_Dispatching_Operation (Alias (E))) | |
fc53fe76 | 9740 | then |
c8ef728f | 9741 | if Present (Alias (E)) then |
039538bc AC |
9742 | Set_Overridden_Operation (S, Alias (E)); |
9743 | Inherit_Subprogram_Contract (S, Alias (E)); | |
41251c60 JM |
9744 | end if; |
9745 | end if; | |
9746 | ||
996ae0b0 | 9747 | if Is_Dispatching_Operation (E) then |
fbf5a39b | 9748 | |
82c80734 | 9749 | -- An overriding dispatching subprogram inherits the |
f9673bb0 | 9750 | -- convention of the overridden subprogram (AI-117). |
996ae0b0 RK |
9751 | |
9752 | Set_Convention (S, Convention (E)); | |
41251c60 JM |
9753 | Check_Dispatching_Operation (S, E); |
9754 | ||
996ae0b0 RK |
9755 | else |
9756 | Check_Dispatching_Operation (S, Empty); | |
9757 | end if; | |
9758 | ||
5d37ba92 ES |
9759 | Check_For_Primitive_Subprogram |
9760 | (Is_Primitive_Subp, Is_Overriding => True); | |
996ae0b0 RK |
9761 | goto Check_Inequality; |
9762 | end; | |
9763 | ||
9764 | -- Apparent redeclarations in instances can occur when two | |
9765 | -- formal types get the same actual type. The subprograms in | |
9766 | -- in the instance are legal, even if not callable from the | |
9767 | -- outside. Calls from within are disambiguated elsewhere. | |
9768 | -- For dispatching operations in the visible part, the usual | |
9769 | -- rules apply, and operations with the same profile are not | |
9770 | -- legal (B830001). | |
9771 | ||
9772 | elsif (In_Instance_Visible_Part | |
9773 | and then not Is_Dispatching_Operation (E)) | |
9774 | or else In_Instance_Not_Visible | |
9775 | then | |
9776 | null; | |
9777 | ||
9778 | -- Here we have a real error (identical profile) | |
9779 | ||
9780 | else | |
9781 | Error_Msg_Sloc := Sloc (E); | |
9782 | ||
9783 | -- Avoid cascaded errors if the entity appears in | |
9784 | -- subsequent calls. | |
9785 | ||
9786 | Set_Scope (S, Current_Scope); | |
9787 | ||
5d37ba92 ES |
9788 | -- Generate error, with extra useful warning for the case |
9789 | -- of a generic instance with no completion. | |
996ae0b0 RK |
9790 | |
9791 | if Is_Generic_Instance (S) | |
9792 | and then not Has_Completion (E) | |
9793 | then | |
9794 | Error_Msg_N | |
5d37ba92 ES |
9795 | ("instantiation cannot provide body for&", S); |
9796 | Error_Msg_N ("\& conflicts with declaration#", S); | |
9797 | else | |
9798 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
9799 | end if; |
9800 | ||
9801 | return; | |
9802 | end if; | |
9803 | ||
9804 | else | |
c8ef728f ES |
9805 | -- If one subprogram has an access parameter and the other |
9806 | -- a parameter of an access type, calls to either might be | |
9807 | -- ambiguous. Verify that parameters match except for the | |
9808 | -- access parameter. | |
9809 | ||
9810 | if May_Hide_Profile then | |
9811 | declare | |
ec4867fa ES |
9812 | F1 : Entity_Id; |
9813 | F2 : Entity_Id; | |
8dbd1460 | 9814 | |
c8ef728f ES |
9815 | begin |
9816 | F1 := First_Formal (S); | |
9817 | F2 := First_Formal (E); | |
9818 | while Present (F1) and then Present (F2) loop | |
9819 | if Is_Access_Type (Etype (F1)) then | |
9820 | if not Is_Access_Type (Etype (F2)) | |
9821 | or else not Conforming_Types | |
9822 | (Designated_Type (Etype (F1)), | |
9823 | Designated_Type (Etype (F2)), | |
9824 | Type_Conformant) | |
9825 | then | |
9826 | May_Hide_Profile := False; | |
9827 | end if; | |
9828 | ||
9829 | elsif | |
9830 | not Conforming_Types | |
9831 | (Etype (F1), Etype (F2), Type_Conformant) | |
9832 | then | |
9833 | May_Hide_Profile := False; | |
9834 | end if; | |
9835 | ||
9836 | Next_Formal (F1); | |
9837 | Next_Formal (F2); | |
9838 | end loop; | |
9839 | ||
9840 | if May_Hide_Profile | |
9841 | and then No (F1) | |
9842 | and then No (F2) | |
9843 | then | |
dbfeb4fa | 9844 | Error_Msg_NE ("calls to& may be ambiguous??", S, S); |
c8ef728f ES |
9845 | end if; |
9846 | end; | |
9847 | end if; | |
996ae0b0 RK |
9848 | end if; |
9849 | ||
996ae0b0 RK |
9850 | E := Homonym (E); |
9851 | end loop; | |
9852 | ||
9853 | -- On exit, we know that S is a new entity | |
9854 | ||
9855 | Enter_Overloaded_Entity (S); | |
5d37ba92 ES |
9856 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
9857 | Check_Overriding_Indicator | |
9858 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
996ae0b0 | 9859 | |
c4d67e2d | 9860 | -- Overloading is not allowed in SPARK, except for operators |
8ed68165 | 9861 | |
c4d67e2d AC |
9862 | if Nkind (S) /= N_Defining_Operator_Symbol then |
9863 | Error_Msg_Sloc := Sloc (Homonym (S)); | |
ce5ba43a | 9864 | Check_SPARK_05_Restriction |
c4d67e2d AC |
9865 | ("overloading not allowed with entity#", S); |
9866 | end if; | |
8ed68165 | 9867 | |
82c80734 RD |
9868 | -- If S is a derived operation for an untagged type then by |
9869 | -- definition it's not a dispatching operation (even if the parent | |
e917aec2 RD |
9870 | -- operation was dispatching), so Check_Dispatching_Operation is not |
9871 | -- called in that case. | |
996ae0b0 | 9872 | |
c8ef728f | 9873 | if No (Derived_Type) |
996ae0b0 RK |
9874 | or else Is_Tagged_Type (Derived_Type) |
9875 | then | |
9876 | Check_Dispatching_Operation (S, Empty); | |
9877 | end if; | |
9878 | end if; | |
9879 | ||
82c80734 RD |
9880 | -- If this is a user-defined equality operator that is not a derived |
9881 | -- subprogram, create the corresponding inequality. If the operation is | |
9882 | -- dispatching, the expansion is done elsewhere, and we do not create | |
9883 | -- an explicit inequality operation. | |
996ae0b0 RK |
9884 | |
9885 | <<Check_Inequality>> | |
9886 | if Chars (S) = Name_Op_Eq | |
9887 | and then Etype (S) = Standard_Boolean | |
9888 | and then Present (Parent (S)) | |
9889 | and then not Is_Dispatching_Operation (S) | |
9890 | then | |
9891 | Make_Inequality_Operator (S); | |
b2834fbd | 9892 | Check_Untagged_Equality (S); |
996ae0b0 | 9893 | end if; |
996ae0b0 RK |
9894 | end New_Overloaded_Entity; |
9895 | ||
9896 | --------------------- | |
9897 | -- Process_Formals -- | |
9898 | --------------------- | |
9899 | ||
9900 | procedure Process_Formals | |
07fc65c4 | 9901 | (T : List_Id; |
996ae0b0 RK |
9902 | Related_Nod : Node_Id) |
9903 | is | |
c8d3b4ff | 9904 | Context : constant Node_Id := Parent (Parent (T)); |
996ae0b0 RK |
9905 | Param_Spec : Node_Id; |
9906 | Formal : Entity_Id; | |
9907 | Formal_Type : Entity_Id; | |
9908 | Default : Node_Id; | |
9909 | Ptype : Entity_Id; | |
9910 | ||
800621e0 RD |
9911 | Num_Out_Params : Nat := 0; |
9912 | First_Out_Param : Entity_Id := Empty; | |
21d27997 | 9913 | -- Used for setting Is_Only_Out_Parameter |
800621e0 | 9914 | |
7b56a91b | 9915 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean; |
950d217a AC |
9916 | -- Determine whether an access type designates a type coming from a |
9917 | -- limited view. | |
9918 | ||
07fc65c4 | 9919 | function Is_Class_Wide_Default (D : Node_Id) return Boolean; |
82c80734 RD |
9920 | -- Check whether the default has a class-wide type. After analysis the |
9921 | -- default has the type of the formal, so we must also check explicitly | |
9922 | -- for an access attribute. | |
07fc65c4 | 9923 | |
7b56a91b AC |
9924 | ---------------------------------- |
9925 | -- Designates_From_Limited_With -- | |
9926 | ---------------------------------- | |
950d217a | 9927 | |
7b56a91b | 9928 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is |
950d217a AC |
9929 | Desig : Entity_Id := Typ; |
9930 | ||
9931 | begin | |
9932 | if Is_Access_Type (Desig) then | |
9933 | Desig := Directly_Designated_Type (Desig); | |
9934 | end if; | |
9935 | ||
9936 | if Is_Class_Wide_Type (Desig) then | |
9937 | Desig := Root_Type (Desig); | |
9938 | end if; | |
9939 | ||
9940 | return | |
7b56a91b AC |
9941 | Ekind (Desig) = E_Incomplete_Type |
9942 | and then From_Limited_With (Desig); | |
9943 | end Designates_From_Limited_With; | |
950d217a | 9944 | |
07fc65c4 GB |
9945 | --------------------------- |
9946 | -- Is_Class_Wide_Default -- | |
9947 | --------------------------- | |
9948 | ||
9949 | function Is_Class_Wide_Default (D : Node_Id) return Boolean is | |
9950 | begin | |
9951 | return Is_Class_Wide_Type (Designated_Type (Etype (D))) | |
9952 | or else (Nkind (D) = N_Attribute_Reference | |
0f853035 YM |
9953 | and then Attribute_Name (D) = Name_Access |
9954 | and then Is_Class_Wide_Type (Etype (Prefix (D)))); | |
07fc65c4 GB |
9955 | end Is_Class_Wide_Default; |
9956 | ||
9957 | -- Start of processing for Process_Formals | |
9958 | ||
996ae0b0 RK |
9959 | begin |
9960 | -- In order to prevent premature use of the formals in the same formal | |
9961 | -- part, the Ekind is left undefined until all default expressions are | |
9962 | -- analyzed. The Ekind is established in a separate loop at the end. | |
9963 | ||
9964 | Param_Spec := First (T); | |
996ae0b0 | 9965 | while Present (Param_Spec) loop |
996ae0b0 | 9966 | Formal := Defining_Identifier (Param_Spec); |
5d37ba92 | 9967 | Set_Never_Set_In_Source (Formal, True); |
996ae0b0 RK |
9968 | Enter_Name (Formal); |
9969 | ||
9970 | -- Case of ordinary parameters | |
9971 | ||
9972 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
9973 | Find_Type (Parameter_Type (Param_Spec)); | |
9974 | Ptype := Parameter_Type (Param_Spec); | |
9975 | ||
9976 | if Ptype = Error then | |
9977 | goto Continue; | |
9978 | end if; | |
9979 | ||
9980 | Formal_Type := Entity (Ptype); | |
9981 | ||
ec4867fa ES |
9982 | if Is_Incomplete_Type (Formal_Type) |
9983 | or else | |
9984 | (Is_Class_Wide_Type (Formal_Type) | |
8fde064e | 9985 | and then Is_Incomplete_Type (Root_Type (Formal_Type))) |
996ae0b0 | 9986 | then |
93bcda23 AC |
9987 | -- Ada 2005 (AI-326): Tagged incomplete types allowed in |
9988 | -- primitive operations, as long as their completion is | |
9989 | -- in the same declarative part. If in the private part | |
9990 | -- this means that the type cannot be a Taft-amendment type. | |
cec29135 ES |
9991 | -- Check is done on package exit. For access to subprograms, |
9992 | -- the use is legal for Taft-amendment types. | |
fbf5a39b | 9993 | |
6eddd7b4 AC |
9994 | -- Ada 2012: tagged incomplete types are allowed as generic |
9995 | -- formal types. They do not introduce dependencies and the | |
9996 | -- corresponding generic subprogram does not have a delayed | |
5b6f12c7 AC |
9997 | -- freeze, because it does not need a freeze node. However, |
9998 | -- it is still the case that untagged incomplete types cannot | |
9999 | -- be Taft-amendment types and must be completed in private | |
10000 | -- part, so the subprogram must appear in the list of private | |
a0a10853 AC |
10001 | -- dependents of the type. If the type is class-wide, it is |
10002 | -- not a primitive, but the freezing of the subprogram must | |
10003 | -- also be delayed to force the creation of a freeze node. | |
5b6f12c7 AC |
10004 | |
10005 | if Is_Tagged_Type (Formal_Type) | |
b973629e | 10006 | or else (Ada_Version >= Ada_2012 |
1ebc2612 AC |
10007 | and then not From_Limited_With (Formal_Type) |
10008 | and then not Is_Generic_Type (Formal_Type)) | |
5b6f12c7 | 10009 | then |
93bcda23 | 10010 | if Ekind (Scope (Current_Scope)) = E_Package |
6eddd7b4 | 10011 | and then not Is_Generic_Type (Formal_Type) |
93bcda23 | 10012 | then |
cec29135 ES |
10013 | if not Nkind_In |
10014 | (Parent (T), N_Access_Function_Definition, | |
10015 | N_Access_Procedure_Definition) | |
10016 | then | |
a0a10853 AC |
10017 | if not Is_Class_Wide_Type (Formal_Type) then |
10018 | Append_Elmt (Current_Scope, | |
10019 | Private_Dependents (Base_Type (Formal_Type))); | |
10020 | end if; | |
4637729f AC |
10021 | |
10022 | -- Freezing is delayed to ensure that Register_Prim | |
10023 | -- will get called for this operation, which is needed | |
10024 | -- in cases where static dispatch tables aren't built. | |
10025 | -- (Note that the same is done for controlling access | |
10026 | -- parameter cases in function Access_Definition.) | |
10027 | ||
13fa2acb AC |
10028 | if not Is_Thunk (Current_Scope) then |
10029 | Set_Has_Delayed_Freeze (Current_Scope); | |
10030 | end if; | |
cec29135 | 10031 | end if; |
93bcda23 | 10032 | end if; |
fbf5a39b | 10033 | |
0a36105d JM |
10034 | -- Special handling of Value_Type for CIL case |
10035 | ||
10036 | elsif Is_Value_Type (Formal_Type) then | |
10037 | null; | |
10038 | ||
800621e0 RD |
10039 | elsif not Nkind_In (Parent (T), N_Access_Function_Definition, |
10040 | N_Access_Procedure_Definition) | |
996ae0b0 | 10041 | then |
dd386db0 AC |
10042 | -- AI05-0151: Tagged incomplete types are allowed in all |
10043 | -- formal parts. Untagged incomplete types are not allowed | |
b973629e AC |
10044 | -- in bodies. Limited views of either kind are not allowed |
10045 | -- if there is no place at which the non-limited view can | |
10046 | -- become available. | |
a18e3d62 | 10047 | |
1ebc2612 AC |
10048 | -- Incomplete formal untagged types are not allowed in |
10049 | -- subprogram bodies (but are legal in their declarations). | |
c8d3b4ff AC |
10050 | -- This excludes bodies created for null procedures, which |
10051 | -- are basic declarations. | |
1ebc2612 AC |
10052 | |
10053 | if Is_Generic_Type (Formal_Type) | |
10054 | and then not Is_Tagged_Type (Formal_Type) | |
10055 | and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body | |
10056 | then | |
10057 | Error_Msg_N | |
10058 | ("invalid use of formal incomplete type", Param_Spec); | |
dd386db0 | 10059 | |
1ebc2612 | 10060 | elsif Ada_Version >= Ada_2012 then |
b973629e AC |
10061 | if Is_Tagged_Type (Formal_Type) |
10062 | and then (not From_Limited_With (Formal_Type) | |
10063 | or else not In_Package_Body) | |
10064 | then | |
dd386db0 AC |
10065 | null; |
10066 | ||
c8d3b4ff AC |
10067 | elsif Nkind_In (Context, N_Accept_Statement, |
10068 | N_Accept_Alternative, | |
10069 | N_Entry_Body) | |
10070 | or else (Nkind (Context) = N_Subprogram_Body | |
10071 | and then Comes_From_Source (Context)) | |
dd386db0 AC |
10072 | then |
10073 | Error_Msg_NE | |
c8d3b4ff | 10074 | ("invalid use of untagged incomplete type &", |
0f1a6a0b | 10075 | Ptype, Formal_Type); |
dd386db0 AC |
10076 | end if; |
10077 | ||
10078 | else | |
10079 | Error_Msg_NE | |
10080 | ("invalid use of incomplete type&", | |
0f1a6a0b | 10081 | Param_Spec, Formal_Type); |
dd386db0 AC |
10082 | |
10083 | -- Further checks on the legality of incomplete types | |
10084 | -- in formal parts are delayed until the freeze point | |
10085 | -- of the enclosing subprogram or access to subprogram. | |
10086 | end if; | |
996ae0b0 RK |
10087 | end if; |
10088 | ||
10089 | elsif Ekind (Formal_Type) = E_Void then | |
0f1a6a0b AC |
10090 | Error_Msg_NE |
10091 | ("premature use of&", | |
10092 | Parameter_Type (Param_Spec), Formal_Type); | |
996ae0b0 RK |
10093 | end if; |
10094 | ||
fecbd779 AC |
10095 | -- Ada 2012 (AI-142): Handle aliased parameters |
10096 | ||
10097 | if Ada_Version >= Ada_2012 | |
10098 | and then Aliased_Present (Param_Spec) | |
10099 | then | |
10100 | Set_Is_Aliased (Formal); | |
10101 | end if; | |
10102 | ||
0ab80019 | 10103 | -- Ada 2005 (AI-231): Create and decorate an internal subtype |
7324bf49 | 10104 | -- declaration corresponding to the null-excluding type of the |
d8db0bca JM |
10105 | -- formal in the enclosing scope. Finally, replace the parameter |
10106 | -- type of the formal with the internal subtype. | |
7324bf49 | 10107 | |
0791fbe9 | 10108 | if Ada_Version >= Ada_2005 |
41251c60 | 10109 | and then Null_Exclusion_Present (Param_Spec) |
7324bf49 | 10110 | then |
ec4867fa | 10111 | if not Is_Access_Type (Formal_Type) then |
ed2233dc | 10112 | Error_Msg_N |
0a36105d JM |
10113 | ("`NOT NULL` allowed only for an access type", Param_Spec); |
10114 | ||
ec4867fa ES |
10115 | else |
10116 | if Can_Never_Be_Null (Formal_Type) | |
10117 | and then Comes_From_Source (Related_Nod) | |
10118 | then | |
ed2233dc | 10119 | Error_Msg_NE |
0a36105d | 10120 | ("`NOT NULL` not allowed (& already excludes null)", |
0f1a6a0b | 10121 | Param_Spec, Formal_Type); |
ec4867fa | 10122 | end if; |
41251c60 | 10123 | |
ec4867fa ES |
10124 | Formal_Type := |
10125 | Create_Null_Excluding_Itype | |
10126 | (T => Formal_Type, | |
10127 | Related_Nod => Related_Nod, | |
10128 | Scope_Id => Scope (Current_Scope)); | |
0a36105d | 10129 | |
fcf848c4 AC |
10130 | -- If the designated type of the itype is an itype that is |
10131 | -- not frozen yet, we set the Has_Delayed_Freeze attribute | |
10132 | -- on the access subtype, to prevent order-of-elaboration | |
10133 | -- issues in the backend. | |
0a36105d JM |
10134 | |
10135 | -- Example: | |
10136 | -- type T is access procedure; | |
10137 | -- procedure Op (O : not null T); | |
10138 | ||
fcf848c4 AC |
10139 | if Is_Itype (Directly_Designated_Type (Formal_Type)) |
10140 | and then | |
10141 | not Is_Frozen (Directly_Designated_Type (Formal_Type)) | |
10142 | then | |
0a36105d JM |
10143 | Set_Has_Delayed_Freeze (Formal_Type); |
10144 | end if; | |
ec4867fa | 10145 | end if; |
7324bf49 AC |
10146 | end if; |
10147 | ||
996ae0b0 RK |
10148 | -- An access formal type |
10149 | ||
10150 | else | |
10151 | Formal_Type := | |
10152 | Access_Definition (Related_Nod, Parameter_Type (Param_Spec)); | |
7324bf49 | 10153 | |
f937473f RD |
10154 | -- No need to continue if we already notified errors |
10155 | ||
10156 | if not Present (Formal_Type) then | |
10157 | return; | |
10158 | end if; | |
10159 | ||
0ab80019 | 10160 | -- Ada 2005 (AI-254) |
7324bf49 | 10161 | |
af4b9434 AC |
10162 | declare |
10163 | AD : constant Node_Id := | |
10164 | Access_To_Subprogram_Definition | |
10165 | (Parameter_Type (Param_Spec)); | |
10166 | begin | |
10167 | if Present (AD) and then Protected_Present (AD) then | |
10168 | Formal_Type := | |
10169 | Replace_Anonymous_Access_To_Protected_Subprogram | |
f937473f | 10170 | (Param_Spec); |
af4b9434 AC |
10171 | end if; |
10172 | end; | |
996ae0b0 RK |
10173 | end if; |
10174 | ||
10175 | Set_Etype (Formal, Formal_Type); | |
0f853035 | 10176 | |
fecbd779 AC |
10177 | -- Deal with default expression if present |
10178 | ||
fbf5a39b | 10179 | Default := Expression (Param_Spec); |
996ae0b0 RK |
10180 | |
10181 | if Present (Default) then | |
ce5ba43a | 10182 | Check_SPARK_05_Restriction |
fe5d3068 | 10183 | ("default expression is not allowed", Default); |
38171f43 | 10184 | |
996ae0b0 | 10185 | if Out_Present (Param_Spec) then |
ed2233dc | 10186 | Error_Msg_N |
996ae0b0 RK |
10187 | ("default initialization only allowed for IN parameters", |
10188 | Param_Spec); | |
10189 | end if; | |
10190 | ||
10191 | -- Do the special preanalysis of the expression (see section on | |
10192 | -- "Handling of Default Expressions" in the spec of package Sem). | |
10193 | ||
21d27997 | 10194 | Preanalyze_Spec_Expression (Default, Formal_Type); |
996ae0b0 | 10195 | |
f29b857f ES |
10196 | -- An access to constant cannot be the default for |
10197 | -- an access parameter that is an access to variable. | |
2eb160f2 ST |
10198 | |
10199 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
10200 | and then not Is_Access_Constant (Formal_Type) | |
10201 | and then Is_Access_Type (Etype (Default)) | |
10202 | and then Is_Access_Constant (Etype (Default)) | |
10203 | then | |
f29b857f | 10204 | Error_Msg_N |
84c0a895 AC |
10205 | ("formal that is access to variable cannot be initialized " |
10206 | & "with an access-to-constant expression", Default); | |
2eb160f2 ST |
10207 | end if; |
10208 | ||
d8db0bca JM |
10209 | -- Check that the designated type of an access parameter's default |
10210 | -- is not a class-wide type unless the parameter's designated type | |
10211 | -- is also class-wide. | |
996ae0b0 RK |
10212 | |
10213 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
7b56a91b | 10214 | and then not Designates_From_Limited_With (Formal_Type) |
07fc65c4 | 10215 | and then Is_Class_Wide_Default (Default) |
996ae0b0 RK |
10216 | and then not Is_Class_Wide_Type (Designated_Type (Formal_Type)) |
10217 | then | |
07fc65c4 GB |
10218 | Error_Msg_N |
10219 | ("access to class-wide expression not allowed here", Default); | |
996ae0b0 | 10220 | end if; |
4755cce9 JM |
10221 | |
10222 | -- Check incorrect use of dynamically tagged expressions | |
10223 | ||
10224 | if Is_Tagged_Type (Formal_Type) then | |
10225 | Check_Dynamically_Tagged_Expression | |
10226 | (Expr => Default, | |
10227 | Typ => Formal_Type, | |
10228 | Related_Nod => Default); | |
10229 | end if; | |
996ae0b0 RK |
10230 | end if; |
10231 | ||
41251c60 JM |
10232 | -- Ada 2005 (AI-231): Static checks |
10233 | ||
0791fbe9 | 10234 | if Ada_Version >= Ada_2005 |
41251c60 JM |
10235 | and then Is_Access_Type (Etype (Formal)) |
10236 | and then Can_Never_Be_Null (Etype (Formal)) | |
10237 | then | |
10238 | Null_Exclusion_Static_Checks (Param_Spec); | |
10239 | end if; | |
10240 | ||
f1bd0415 AC |
10241 | -- The following checks are relevant when SPARK_Mode is on as these |
10242 | -- are not standard Ada legality rules. | |
6c3c671e | 10243 | |
ea26c8e4 HK |
10244 | if SPARK_Mode = On then |
10245 | if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then | |
f1bd0415 | 10246 | |
ea26c8e4 HK |
10247 | -- A function cannot have a parameter of mode IN OUT or OUT |
10248 | -- (SPARK RM 6.1). | |
f1bd0415 | 10249 | |
ea26c8e4 HK |
10250 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then |
10251 | Error_Msg_N | |
10252 | ("function cannot have parameter of mode `OUT` or " | |
10253 | & "`IN OUT`", Formal); | |
f1bd0415 | 10254 | |
d780e54f AC |
10255 | -- A function cannot have an effectively volatile formal |
10256 | -- parameter (SPARK RM 7.1.3(10)). | |
ea26c8e4 | 10257 | |
d780e54f | 10258 | elsif Is_Effectively_Volatile (Formal) then |
ea26c8e4 HK |
10259 | Error_Msg_N |
10260 | ("function cannot have a volatile formal parameter", | |
10261 | Formal); | |
10262 | end if; | |
10263 | ||
d780e54f AC |
10264 | -- A procedure cannot have an effectively volatile formal |
10265 | -- parameter of mode IN because it behaves as a constant | |
10266 | -- (SPARK RM 7.1.3(6)). | |
ea26c8e4 HK |
10267 | |
10268 | elsif Ekind (Scope (Formal)) = E_Procedure | |
10269 | and then Ekind (Formal) = E_In_Parameter | |
d780e54f | 10270 | and then Is_Effectively_Volatile (Formal) |
ea26c8e4 | 10271 | then |
f1bd0415 | 10272 | Error_Msg_N |
ea26c8e4 | 10273 | ("formal parameter of mode `IN` cannot be volatile", Formal); |
f1bd0415 | 10274 | end if; |
6c3c671e AC |
10275 | end if; |
10276 | ||
996ae0b0 RK |
10277 | <<Continue>> |
10278 | Next (Param_Spec); | |
10279 | end loop; | |
10280 | ||
82c80734 RD |
10281 | -- If this is the formal part of a function specification, analyze the |
10282 | -- subtype mark in the context where the formals are visible but not | |
10283 | -- yet usable, and may hide outer homographs. | |
10284 | ||
10285 | if Nkind (Related_Nod) = N_Function_Specification then | |
10286 | Analyze_Return_Type (Related_Nod); | |
10287 | end if; | |
10288 | ||
996ae0b0 RK |
10289 | -- Now set the kind (mode) of each formal |
10290 | ||
10291 | Param_Spec := First (T); | |
996ae0b0 RK |
10292 | while Present (Param_Spec) loop |
10293 | Formal := Defining_Identifier (Param_Spec); | |
10294 | Set_Formal_Mode (Formal); | |
10295 | ||
10296 | if Ekind (Formal) = E_In_Parameter then | |
10297 | Set_Default_Value (Formal, Expression (Param_Spec)); | |
10298 | ||
10299 | if Present (Expression (Param_Spec)) then | |
10300 | Default := Expression (Param_Spec); | |
10301 | ||
10302 | if Is_Scalar_Type (Etype (Default)) then | |
5ebfaacf AC |
10303 | if Nkind (Parameter_Type (Param_Spec)) /= |
10304 | N_Access_Definition | |
996ae0b0 RK |
10305 | then |
10306 | Formal_Type := Entity (Parameter_Type (Param_Spec)); | |
996ae0b0 | 10307 | else |
5ebfaacf AC |
10308 | Formal_Type := |
10309 | Access_Definition | |
10310 | (Related_Nod, Parameter_Type (Param_Spec)); | |
996ae0b0 RK |
10311 | end if; |
10312 | ||
10313 | Apply_Scalar_Range_Check (Default, Formal_Type); | |
10314 | end if; | |
2820d220 | 10315 | end if; |
800621e0 RD |
10316 | |
10317 | elsif Ekind (Formal) = E_Out_Parameter then | |
10318 | Num_Out_Params := Num_Out_Params + 1; | |
10319 | ||
10320 | if Num_Out_Params = 1 then | |
10321 | First_Out_Param := Formal; | |
10322 | end if; | |
10323 | ||
10324 | elsif Ekind (Formal) = E_In_Out_Parameter then | |
10325 | Num_Out_Params := Num_Out_Params + 1; | |
996ae0b0 RK |
10326 | end if; |
10327 | ||
4172a8e3 AC |
10328 | -- Skip remaining processing if formal type was in error |
10329 | ||
10330 | if Etype (Formal) = Any_Type or else Error_Posted (Formal) then | |
10331 | goto Next_Parameter; | |
10332 | end if; | |
10333 | ||
fecbd779 AC |
10334 | -- Force call by reference if aliased |
10335 | ||
10336 | if Is_Aliased (Formal) then | |
10337 | Set_Mechanism (Formal, By_Reference); | |
5ebfaacf AC |
10338 | |
10339 | -- Warn if user asked this to be passed by copy | |
10340 | ||
10341 | if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then | |
10342 | Error_Msg_N | |
b785e0b8 | 10343 | ("cannot pass aliased parameter & by copy??", Formal); |
5ebfaacf AC |
10344 | end if; |
10345 | ||
10346 | -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy | |
10347 | ||
10348 | elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then | |
10349 | Set_Mechanism (Formal, By_Copy); | |
10350 | ||
10351 | elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then | |
10352 | Set_Mechanism (Formal, By_Reference); | |
fecbd779 AC |
10353 | end if; |
10354 | ||
4172a8e3 | 10355 | <<Next_Parameter>> |
996ae0b0 RK |
10356 | Next (Param_Spec); |
10357 | end loop; | |
800621e0 RD |
10358 | |
10359 | if Present (First_Out_Param) and then Num_Out_Params = 1 then | |
10360 | Set_Is_Only_Out_Parameter (First_Out_Param); | |
10361 | end if; | |
996ae0b0 RK |
10362 | end Process_Formals; |
10363 | ||
fbf5a39b AC |
10364 | ---------------------------- |
10365 | -- Reference_Body_Formals -- | |
10366 | ---------------------------- | |
10367 | ||
10368 | procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is | |
10369 | Fs : Entity_Id; | |
10370 | Fb : Entity_Id; | |
10371 | ||
10372 | begin | |
10373 | if Error_Posted (Spec) then | |
10374 | return; | |
10375 | end if; | |
10376 | ||
0a36105d JM |
10377 | -- Iterate over both lists. They may be of different lengths if the two |
10378 | -- specs are not conformant. | |
10379 | ||
fbf5a39b AC |
10380 | Fs := First_Formal (Spec); |
10381 | Fb := First_Formal (Bod); | |
0a36105d | 10382 | while Present (Fs) and then Present (Fb) loop |
fbf5a39b AC |
10383 | Generate_Reference (Fs, Fb, 'b'); |
10384 | ||
10385 | if Style_Check then | |
10386 | Style.Check_Identifier (Fb, Fs); | |
10387 | end if; | |
10388 | ||
10389 | Set_Spec_Entity (Fb, Fs); | |
10390 | Set_Referenced (Fs, False); | |
10391 | Next_Formal (Fs); | |
10392 | Next_Formal (Fb); | |
10393 | end loop; | |
10394 | end Reference_Body_Formals; | |
10395 | ||
996ae0b0 RK |
10396 | ------------------------- |
10397 | -- Set_Actual_Subtypes -- | |
10398 | ------------------------- | |
10399 | ||
10400 | procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is | |
84c0a895 AC |
10401 | Decl : Node_Id; |
10402 | Formal : Entity_Id; | |
10403 | T : Entity_Id; | |
10404 | First_Stmt : Node_Id := Empty; | |
10405 | AS_Needed : Boolean; | |
996ae0b0 RK |
10406 | |
10407 | begin | |
f3d57416 | 10408 | -- If this is an empty initialization procedure, no need to create |
fbf5a39b AC |
10409 | -- actual subtypes (small optimization). |
10410 | ||
8fde064e | 10411 | if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then |
fbf5a39b AC |
10412 | return; |
10413 | end if; | |
10414 | ||
996ae0b0 RK |
10415 | Formal := First_Formal (Subp); |
10416 | while Present (Formal) loop | |
10417 | T := Etype (Formal); | |
10418 | ||
e895b435 | 10419 | -- We never need an actual subtype for a constrained formal |
996ae0b0 RK |
10420 | |
10421 | if Is_Constrained (T) then | |
10422 | AS_Needed := False; | |
10423 | ||
82c80734 | 10424 | -- If we have unknown discriminants, then we do not need an actual |
a90bd866 | 10425 | -- subtype, or more accurately we cannot figure it out. Note that |
82c80734 | 10426 | -- all class-wide types have unknown discriminants. |
996ae0b0 RK |
10427 | |
10428 | elsif Has_Unknown_Discriminants (T) then | |
10429 | AS_Needed := False; | |
10430 | ||
82c80734 RD |
10431 | -- At this stage we have an unconstrained type that may need an |
10432 | -- actual subtype. For sure the actual subtype is needed if we have | |
10433 | -- an unconstrained array type. | |
996ae0b0 RK |
10434 | |
10435 | elsif Is_Array_Type (T) then | |
10436 | AS_Needed := True; | |
10437 | ||
d8db0bca JM |
10438 | -- The only other case needing an actual subtype is an unconstrained |
10439 | -- record type which is an IN parameter (we cannot generate actual | |
10440 | -- subtypes for the OUT or IN OUT case, since an assignment can | |
10441 | -- change the discriminant values. However we exclude the case of | |
10442 | -- initialization procedures, since discriminants are handled very | |
10443 | -- specially in this context, see the section entitled "Handling of | |
10444 | -- Discriminants" in Einfo. | |
10445 | ||
10446 | -- We also exclude the case of Discrim_SO_Functions (functions used | |
10447 | -- in front end layout mode for size/offset values), since in such | |
10448 | -- functions only discriminants are referenced, and not only are such | |
10449 | -- subtypes not needed, but they cannot always be generated, because | |
10450 | -- of order of elaboration issues. | |
996ae0b0 RK |
10451 | |
10452 | elsif Is_Record_Type (T) | |
10453 | and then Ekind (Formal) = E_In_Parameter | |
10454 | and then Chars (Formal) /= Name_uInit | |
5d09245e | 10455 | and then not Is_Unchecked_Union (T) |
996ae0b0 RK |
10456 | and then not Is_Discrim_SO_Function (Subp) |
10457 | then | |
10458 | AS_Needed := True; | |
10459 | ||
10460 | -- All other cases do not need an actual subtype | |
10461 | ||
10462 | else | |
10463 | AS_Needed := False; | |
10464 | end if; | |
10465 | ||
10466 | -- Generate actual subtypes for unconstrained arrays and | |
10467 | -- unconstrained discriminated records. | |
10468 | ||
10469 | if AS_Needed then | |
7324bf49 | 10470 | if Nkind (N) = N_Accept_Statement then |
fbf5a39b | 10471 | |
57a8057a | 10472 | -- If expansion is active, the formal is replaced by a local |
fbf5a39b AC |
10473 | -- variable that renames the corresponding entry of the |
10474 | -- parameter block, and it is this local variable that may | |
da94696d | 10475 | -- require an actual subtype. |
fbf5a39b | 10476 | |
4460a9bc | 10477 | if Expander_Active then |
fbf5a39b AC |
10478 | Decl := Build_Actual_Subtype (T, Renamed_Object (Formal)); |
10479 | else | |
10480 | Decl := Build_Actual_Subtype (T, Formal); | |
10481 | end if; | |
10482 | ||
996ae0b0 RK |
10483 | if Present (Handled_Statement_Sequence (N)) then |
10484 | First_Stmt := | |
10485 | First (Statements (Handled_Statement_Sequence (N))); | |
10486 | Prepend (Decl, Statements (Handled_Statement_Sequence (N))); | |
10487 | Mark_Rewrite_Insertion (Decl); | |
10488 | else | |
82c80734 RD |
10489 | -- If the accept statement has no body, there will be no |
10490 | -- reference to the actuals, so no need to compute actual | |
10491 | -- subtypes. | |
996ae0b0 RK |
10492 | |
10493 | return; | |
10494 | end if; | |
10495 | ||
10496 | else | |
fbf5a39b | 10497 | Decl := Build_Actual_Subtype (T, Formal); |
996ae0b0 RK |
10498 | Prepend (Decl, Declarations (N)); |
10499 | Mark_Rewrite_Insertion (Decl); | |
10500 | end if; | |
10501 | ||
82c80734 RD |
10502 | -- The declaration uses the bounds of an existing object, and |
10503 | -- therefore needs no constraint checks. | |
2820d220 | 10504 | |
7324bf49 | 10505 | Analyze (Decl, Suppress => All_Checks); |
2820d220 | 10506 | |
996ae0b0 RK |
10507 | -- We need to freeze manually the generated type when it is |
10508 | -- inserted anywhere else than in a declarative part. | |
10509 | ||
10510 | if Present (First_Stmt) then | |
10511 | Insert_List_Before_And_Analyze (First_Stmt, | |
c159409f | 10512 | Freeze_Entity (Defining_Identifier (Decl), N)); |
fcadacf7 ES |
10513 | |
10514 | -- Ditto if the type has a dynamic predicate, because the | |
10515 | -- generated function will mention the actual subtype. | |
10516 | ||
10517 | elsif Has_Dynamic_Predicate_Aspect (T) then | |
10518 | Insert_List_Before_And_Analyze (Decl, | |
10519 | Freeze_Entity (Defining_Identifier (Decl), N)); | |
996ae0b0 RK |
10520 | end if; |
10521 | ||
fbf5a39b | 10522 | if Nkind (N) = N_Accept_Statement |
4460a9bc | 10523 | and then Expander_Active |
fbf5a39b AC |
10524 | then |
10525 | Set_Actual_Subtype (Renamed_Object (Formal), | |
10526 | Defining_Identifier (Decl)); | |
10527 | else | |
10528 | Set_Actual_Subtype (Formal, Defining_Identifier (Decl)); | |
10529 | end if; | |
996ae0b0 RK |
10530 | end if; |
10531 | ||
10532 | Next_Formal (Formal); | |
10533 | end loop; | |
10534 | end Set_Actual_Subtypes; | |
10535 | ||
10536 | --------------------- | |
10537 | -- Set_Formal_Mode -- | |
10538 | --------------------- | |
10539 | ||
10540 | procedure Set_Formal_Mode (Formal_Id : Entity_Id) is | |
10541 | Spec : constant Node_Id := Parent (Formal_Id); | |
10542 | ||
10543 | begin | |
10544 | -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters | |
10545 | -- since we ensure that corresponding actuals are always valid at the | |
10546 | -- point of the call. | |
10547 | ||
10548 | if Out_Present (Spec) then | |
fc999c5d RD |
10549 | if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then |
10550 | ||
b4ca2d2c | 10551 | -- [IN] OUT parameters allowed for functions in Ada 2012 |
c56a9ba4 AC |
10552 | |
10553 | if Ada_Version >= Ada_2012 then | |
e6425869 AC |
10554 | |
10555 | -- Even in Ada 2012 operators can only have IN parameters | |
10556 | ||
10557 | if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then | |
10558 | Error_Msg_N ("operators can only have IN parameters", Spec); | |
10559 | end if; | |
10560 | ||
c56a9ba4 AC |
10561 | if In_Present (Spec) then |
10562 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10563 | else | |
10564 | Set_Ekind (Formal_Id, E_Out_Parameter); | |
10565 | end if; | |
10566 | ||
fc999c5d RD |
10567 | Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True); |
10568 | ||
b4ca2d2c AC |
10569 | -- But not in earlier versions of Ada |
10570 | ||
c56a9ba4 AC |
10571 | else |
10572 | Error_Msg_N ("functions can only have IN parameters", Spec); | |
10573 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10574 | end if; | |
996ae0b0 RK |
10575 | |
10576 | elsif In_Present (Spec) then | |
10577 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10578 | ||
10579 | else | |
fbf5a39b AC |
10580 | Set_Ekind (Formal_Id, E_Out_Parameter); |
10581 | Set_Never_Set_In_Source (Formal_Id, True); | |
10582 | Set_Is_True_Constant (Formal_Id, False); | |
10583 | Set_Current_Value (Formal_Id, Empty); | |
996ae0b0 RK |
10584 | end if; |
10585 | ||
10586 | else | |
10587 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10588 | end if; | |
10589 | ||
fbf5a39b | 10590 | -- Set Is_Known_Non_Null for access parameters since the language |
82c80734 RD |
10591 | -- guarantees that access parameters are always non-null. We also set |
10592 | -- Can_Never_Be_Null, since there is no way to change the value. | |
fbf5a39b AC |
10593 | |
10594 | if Nkind (Parameter_Type (Spec)) = N_Access_Definition then | |
2820d220 | 10595 | |
885c4871 | 10596 | -- Ada 2005 (AI-231): In Ada 95, access parameters are always non- |
2813bb6b | 10597 | -- null; In Ada 2005, only if then null_exclusion is explicit. |
2820d220 | 10598 | |
0791fbe9 | 10599 | if Ada_Version < Ada_2005 |
2813bb6b | 10600 | or else Can_Never_Be_Null (Etype (Formal_Id)) |
2820d220 AC |
10601 | then |
10602 | Set_Is_Known_Non_Null (Formal_Id); | |
10603 | Set_Can_Never_Be_Null (Formal_Id); | |
10604 | end if; | |
2813bb6b | 10605 | |
41251c60 JM |
10606 | -- Ada 2005 (AI-231): Null-exclusion access subtype |
10607 | ||
2813bb6b ES |
10608 | elsif Is_Access_Type (Etype (Formal_Id)) |
10609 | and then Can_Never_Be_Null (Etype (Formal_Id)) | |
10610 | then | |
2813bb6b | 10611 | Set_Is_Known_Non_Null (Formal_Id); |
a1d72281 EB |
10612 | |
10613 | -- We can also set Can_Never_Be_Null (thus preventing some junk | |
10614 | -- access checks) for the case of an IN parameter, which cannot | |
10615 | -- be changed, or for an IN OUT parameter, which can be changed but | |
10616 | -- not to a null value. But for an OUT parameter, the initial value | |
10617 | -- passed in can be null, so we can't set this flag in that case. | |
10618 | ||
10619 | if Ekind (Formal_Id) /= E_Out_Parameter then | |
10620 | Set_Can_Never_Be_Null (Formal_Id); | |
10621 | end if; | |
fbf5a39b AC |
10622 | end if; |
10623 | ||
996ae0b0 RK |
10624 | Set_Mechanism (Formal_Id, Default_Mechanism); |
10625 | Set_Formal_Validity (Formal_Id); | |
10626 | end Set_Formal_Mode; | |
10627 | ||
10628 | ------------------------- | |
10629 | -- Set_Formal_Validity -- | |
10630 | ------------------------- | |
10631 | ||
10632 | procedure Set_Formal_Validity (Formal_Id : Entity_Id) is | |
10633 | begin | |
82c80734 RD |
10634 | -- If no validity checking, then we cannot assume anything about the |
10635 | -- validity of parameters, since we do not know there is any checking | |
10636 | -- of the validity on the call side. | |
996ae0b0 RK |
10637 | |
10638 | if not Validity_Checks_On then | |
10639 | return; | |
10640 | ||
fbf5a39b AC |
10641 | -- If validity checking for parameters is enabled, this means we are |
10642 | -- not supposed to make any assumptions about argument values. | |
10643 | ||
10644 | elsif Validity_Check_Parameters then | |
10645 | return; | |
10646 | ||
10647 | -- If we are checking in parameters, we will assume that the caller is | |
10648 | -- also checking parameters, so we can assume the parameter is valid. | |
10649 | ||
996ae0b0 RK |
10650 | elsif Ekind (Formal_Id) = E_In_Parameter |
10651 | and then Validity_Check_In_Params | |
10652 | then | |
10653 | Set_Is_Known_Valid (Formal_Id, True); | |
10654 | ||
fbf5a39b AC |
10655 | -- Similar treatment for IN OUT parameters |
10656 | ||
996ae0b0 RK |
10657 | elsif Ekind (Formal_Id) = E_In_Out_Parameter |
10658 | and then Validity_Check_In_Out_Params | |
10659 | then | |
10660 | Set_Is_Known_Valid (Formal_Id, True); | |
10661 | end if; | |
10662 | end Set_Formal_Validity; | |
10663 | ||
10664 | ------------------------ | |
10665 | -- Subtype_Conformant -- | |
10666 | ------------------------ | |
10667 | ||
ce2b6ba5 JM |
10668 | function Subtype_Conformant |
10669 | (New_Id : Entity_Id; | |
10670 | Old_Id : Entity_Id; | |
10671 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10672 | is | |
996ae0b0 | 10673 | Result : Boolean; |
996ae0b0 | 10674 | begin |
ce2b6ba5 JM |
10675 | Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result, |
10676 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10677 | return Result; |
10678 | end Subtype_Conformant; | |
10679 | ||
10680 | --------------------- | |
10681 | -- Type_Conformant -- | |
10682 | --------------------- | |
10683 | ||
41251c60 JM |
10684 | function Type_Conformant |
10685 | (New_Id : Entity_Id; | |
10686 | Old_Id : Entity_Id; | |
10687 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10688 | is | |
996ae0b0 | 10689 | Result : Boolean; |
996ae0b0 | 10690 | begin |
c8ef728f | 10691 | May_Hide_Profile := False; |
41251c60 JM |
10692 | Check_Conformance |
10693 | (New_Id, Old_Id, Type_Conformant, False, Result, | |
10694 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10695 | return Result; |
10696 | end Type_Conformant; | |
10697 | ||
10698 | ------------------------------- | |
10699 | -- Valid_Operator_Definition -- | |
10700 | ------------------------------- | |
10701 | ||
10702 | procedure Valid_Operator_Definition (Designator : Entity_Id) is | |
10703 | N : Integer := 0; | |
10704 | F : Entity_Id; | |
10705 | Id : constant Name_Id := Chars (Designator); | |
10706 | N_OK : Boolean; | |
10707 | ||
10708 | begin | |
10709 | F := First_Formal (Designator); | |
996ae0b0 RK |
10710 | while Present (F) loop |
10711 | N := N + 1; | |
10712 | ||
10713 | if Present (Default_Value (F)) then | |
ed2233dc | 10714 | Error_Msg_N |
996ae0b0 RK |
10715 | ("default values not allowed for operator parameters", |
10716 | Parent (F)); | |
220d1fd9 AC |
10717 | |
10718 | -- For function instantiations that are operators, we must check | |
10719 | -- separately that the corresponding generic only has in-parameters. | |
84c0a895 AC |
10720 | -- For subprogram declarations this is done in Set_Formal_Mode. Such |
10721 | -- an error could not arise in earlier versions of the language. | |
220d1fd9 AC |
10722 | |
10723 | elsif Ekind (F) /= E_In_Parameter then | |
84c0a895 | 10724 | Error_Msg_N ("operators can only have IN parameters", F); |
996ae0b0 RK |
10725 | end if; |
10726 | ||
10727 | Next_Formal (F); | |
10728 | end loop; | |
10729 | ||
10730 | -- Verify that user-defined operators have proper number of arguments | |
10731 | -- First case of operators which can only be unary | |
10732 | ||
b69cd36a | 10733 | if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then |
996ae0b0 RK |
10734 | N_OK := (N = 1); |
10735 | ||
10736 | -- Case of operators which can be unary or binary | |
10737 | ||
b69cd36a | 10738 | elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then |
996ae0b0 RK |
10739 | N_OK := (N in 1 .. 2); |
10740 | ||
10741 | -- All other operators can only be binary | |
10742 | ||
10743 | else | |
10744 | N_OK := (N = 2); | |
10745 | end if; | |
10746 | ||
10747 | if not N_OK then | |
10748 | Error_Msg_N | |
10749 | ("incorrect number of arguments for operator", Designator); | |
10750 | end if; | |
10751 | ||
10752 | if Id = Name_Op_Ne | |
10753 | and then Base_Type (Etype (Designator)) = Standard_Boolean | |
10754 | and then not Is_Intrinsic_Subprogram (Designator) | |
10755 | then | |
10756 | Error_Msg_N | |
84c0a895 | 10757 | ("explicit definition of inequality not allowed", Designator); |
996ae0b0 RK |
10758 | end if; |
10759 | end Valid_Operator_Definition; | |
10760 | ||
10761 | end Sem_Ch6; |