]>
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 | -- -- |
f9a8f910 | 9 | -- Copyright (C) 1992-2017, 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 | ||
879ac954 AC |
26 | with Aspects; use Aspects; |
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Contracts; use Contracts; | |
30 | with Debug; use Debug; | |
31 | with Einfo; use Einfo; | |
32 | with Elists; use Elists; | |
33 | with Errout; use Errout; | |
34 | with Expander; use Expander; | |
35 | with Exp_Ch6; use Exp_Ch6; | |
36 | with Exp_Ch7; use Exp_Ch7; | |
37 | with Exp_Ch9; use Exp_Ch9; | |
38 | with Exp_Dbug; use Exp_Dbug; | |
39 | with Exp_Disp; use Exp_Disp; | |
40 | with Exp_Tss; use Exp_Tss; | |
41 | with Exp_Util; use Exp_Util; | |
42 | with Fname; use Fname; | |
43 | with Freeze; use Freeze; | |
44 | with Ghost; use Ghost; | |
45 | with Inline; use Inline; | |
46 | with Itypes; use Itypes; | |
47 | with Lib.Xref; use Lib.Xref; | |
48 | with Layout; use Layout; | |
49 | with Namet; use Namet; | |
50 | with Lib; use Lib; | |
51 | with Nlists; use Nlists; | |
52 | with Nmake; use Nmake; | |
53 | with Opt; use Opt; | |
54 | with Output; use Output; | |
55 | with Restrict; use Restrict; | |
56 | with Rident; use Rident; | |
57 | with Rtsfind; use Rtsfind; | |
58 | with Sem; use Sem; | |
59 | with Sem_Aux; use Sem_Aux; | |
60 | with Sem_Cat; use Sem_Cat; | |
61 | with Sem_Ch3; use Sem_Ch3; | |
62 | with Sem_Ch4; use Sem_Ch4; | |
63 | with Sem_Ch5; use Sem_Ch5; | |
64 | with Sem_Ch8; use Sem_Ch8; | |
5e127570 | 65 | with Sem_Ch9; use Sem_Ch9; |
879ac954 AC |
66 | with Sem_Ch10; use Sem_Ch10; |
67 | with Sem_Ch12; use Sem_Ch12; | |
68 | with Sem_Ch13; use Sem_Ch13; | |
69 | with Sem_Dim; use Sem_Dim; | |
70 | with Sem_Disp; use Sem_Disp; | |
71 | with Sem_Dist; use Sem_Dist; | |
72 | with Sem_Elim; use Sem_Elim; | |
73 | with Sem_Eval; use Sem_Eval; | |
74 | with Sem_Mech; use Sem_Mech; | |
75 | with Sem_Prag; use Sem_Prag; | |
76 | with Sem_Res; use Sem_Res; | |
77 | with Sem_Util; use Sem_Util; | |
78 | with Sem_Type; use Sem_Type; | |
79 | with Sem_Warn; use Sem_Warn; | |
80 | with Sinput; use Sinput; | |
81 | with Stand; use Stand; | |
82 | with Sinfo; use Sinfo; | |
83 | with Sinfo.CN; use Sinfo.CN; | |
84 | with Snames; use Snames; | |
85 | with Stringt; use Stringt; | |
996ae0b0 | 86 | with Style; |
879ac954 AC |
87 | with Stylesw; use Stylesw; |
88 | with Tbuild; use Tbuild; | |
89 | with Uintp; use Uintp; | |
90 | with Urealp; use Urealp; | |
91 | with Validsw; use Validsw; | |
996ae0b0 RK |
92 | |
93 | package body Sem_Ch6 is | |
94 | ||
c8ef728f | 95 | May_Hide_Profile : Boolean := False; |
ec4867fa ES |
96 | -- This flag is used to indicate that two formals in two subprograms being |
97 | -- checked for conformance differ only in that one is an access parameter | |
98 | -- while the other is of a general access type with the same designated | |
99 | -- type. In this case, if the rest of the signatures match, a call to | |
100 | -- either subprogram may be ambiguous, which is worth a warning. The flag | |
101 | -- is set in Compatible_Types, and the warning emitted in | |
102 | -- New_Overloaded_Entity. | |
c8ef728f | 103 | |
996ae0b0 RK |
104 | ----------------------- |
105 | -- Local Subprograms -- | |
106 | ----------------------- | |
107 | ||
c9d70ab1 AC |
108 | procedure Analyze_Function_Return (N : Node_Id); |
109 | -- Subsidiary to Analyze_Return_Statement. Called when the return statement | |
110 | -- applies to a [generic] function. | |
111 | ||
112 | procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); | |
113 | -- Analyze a generic subprogram body. N is the body to be analyzed, and | |
114 | -- Gen_Id is the defining entity Id for the corresponding spec. | |
115 | ||
4d8f3296 ES |
116 | procedure Analyze_Null_Procedure |
117 | (N : Node_Id; | |
118 | Is_Completion : out Boolean); | |
9d2a2071 | 119 | -- A null procedure can be a declaration or (Ada 2012) a completion |
4d8f3296 | 120 | |
5d37ba92 | 121 | procedure Analyze_Return_Statement (N : Node_Id); |
5b9c3fc4 | 122 | -- Common processing for simple and extended return statements |
ec4867fa | 123 | |
82c80734 RD |
124 | procedure Analyze_Return_Type (N : Node_Id); |
125 | -- Subsidiary to Process_Formals: analyze subtype mark in function | |
5b9c3fc4 | 126 | -- specification in a context where the formals are visible and hide |
82c80734 RD |
127 | -- outer homographs. |
128 | ||
b1b543d2 | 129 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id); |
13d923cc RD |
130 | -- Does all the real work of Analyze_Subprogram_Body. This is split out so |
131 | -- that we can use RETURN but not skip the debug output at the end. | |
b1b543d2 | 132 | |
806f6d37 AC |
133 | function Can_Override_Operator (Subp : Entity_Id) return Boolean; |
134 | -- Returns true if Subp can override a predefined operator. | |
135 | ||
996ae0b0 | 136 | procedure Check_Conformance |
41251c60 JM |
137 | (New_Id : Entity_Id; |
138 | Old_Id : Entity_Id; | |
139 | Ctype : Conformance_Type; | |
140 | Errmsg : Boolean; | |
141 | Conforms : out Boolean; | |
142 | Err_Loc : Node_Id := Empty; | |
143 | Get_Inst : Boolean := False; | |
144 | Skip_Controlling_Formals : Boolean := False); | |
996ae0b0 RK |
145 | -- Given two entities, this procedure checks that the profiles associated |
146 | -- with these entities meet the conformance criterion given by the third | |
147 | -- parameter. If they conform, Conforms is set True and control returns | |
148 | -- to the caller. If they do not conform, Conforms is set to False, and | |
149 | -- in addition, if Errmsg is True on the call, proper messages are output | |
150 | -- to complain about the conformance failure. If Err_Loc is non_Empty | |
151 | -- the error messages are placed on Err_Loc, if Err_Loc is empty, then | |
152 | -- error messages are placed on the appropriate part of the construct | |
153 | -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance | |
154 | -- against a formal access-to-subprogram type so Get_Instance_Of must | |
155 | -- be called. | |
156 | ||
b420ba79 AC |
157 | procedure Check_Limited_Return |
158 | (N : Node_Id; | |
159 | Expr : Node_Id; | |
160 | R_Type : Entity_Id); | |
161 | -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited | |
162 | -- types. Used only for simple return statements. Expr is the expression | |
163 | -- returned. | |
164 | ||
996ae0b0 RK |
165 | procedure Check_Subprogram_Order (N : Node_Id); |
166 | -- N is the N_Subprogram_Body node for a subprogram. This routine applies | |
167 | -- the alpha ordering rule for N if this ordering requirement applicable. | |
168 | ||
996ae0b0 RK |
169 | procedure Check_Returns |
170 | (HSS : Node_Id; | |
171 | Mode : Character; | |
c8ef728f ES |
172 | Err : out Boolean; |
173 | Proc : Entity_Id := Empty); | |
174 | -- Called to check for missing return statements in a function body, or for | |
0a36105d | 175 | -- returns present in a procedure body which has No_Return set. HSS is the |
c8ef728f ES |
176 | -- handled statement sequence for the subprogram body. This procedure |
177 | -- checks all flow paths to make sure they either have return (Mode = 'F', | |
178 | -- used for functions) or do not have a return (Mode = 'P', used for | |
179 | -- No_Return procedures). The flag Err is set if there are any control | |
180 | -- paths not explicitly terminated by a return in the function case, and is | |
181 | -- True otherwise. Proc is the entity for the procedure case and is used | |
182 | -- in posting the warning message. | |
996ae0b0 | 183 | |
e5a58fac AC |
184 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id); |
185 | -- In Ada 2012, a primitive equality operator on an untagged record type | |
186 | -- must appear before the type is frozen, and have the same visibility as | |
187 | -- that of the type. This procedure checks that this rule is met, and | |
188 | -- otherwise emits an error on the subprogram declaration and a warning | |
b2834fbd AC |
189 | -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode, |
190 | -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier | |
191 | -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility | |
192 | -- is set, otherwise the call has no effect. | |
e5a58fac | 193 | |
996ae0b0 | 194 | procedure Enter_Overloaded_Entity (S : Entity_Id); |
82c80734 RD |
195 | -- This procedure makes S, a new overloaded entity, into the first visible |
196 | -- entity with that name. | |
996ae0b0 | 197 | |
a5b62485 AC |
198 | function Is_Non_Overriding_Operation |
199 | (Prev_E : Entity_Id; | |
200 | New_E : Entity_Id) return Boolean; | |
201 | -- Enforce the rule given in 12.3(18): a private operation in an instance | |
202 | -- overrides an inherited operation only if the corresponding operation | |
260359e3 AC |
203 | -- was overriding in the generic. This needs to be checked for primitive |
204 | -- operations of types derived (in the generic unit) from formal private | |
205 | -- or formal derived types. | |
a5b62485 | 206 | |
996ae0b0 RK |
207 | procedure Make_Inequality_Operator (S : Entity_Id); |
208 | -- Create the declaration for an inequality operator that is implicitly | |
209 | -- created by a user-defined equality operator that yields a boolean. | |
210 | ||
996ae0b0 RK |
211 | procedure Set_Formal_Validity (Formal_Id : Entity_Id); |
212 | -- Formal_Id is an formal parameter entity. This procedure deals with | |
e358346d AC |
213 | -- setting the proper validity status for this entity, which depends on |
214 | -- the kind of parameter and the validity checking mode. | |
996ae0b0 RK |
215 | |
216 | --------------------------------------------- | |
217 | -- Analyze_Abstract_Subprogram_Declaration -- | |
218 | --------------------------------------------- | |
219 | ||
220 | procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is | |
1af4455a HK |
221 | Scop : constant Entity_Id := Current_Scope; |
222 | Subp_Id : constant Entity_Id := | |
241ebe89 | 223 | Analyze_Subprogram_Specification (Specification (N)); |
996ae0b0 RK |
224 | |
225 | begin | |
ce5ba43a | 226 | Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N); |
38171f43 | 227 | |
241ebe89 | 228 | Generate_Definition (Subp_Id); |
c9d70ab1 | 229 | |
241ebe89 HK |
230 | Set_Is_Abstract_Subprogram (Subp_Id); |
231 | New_Overloaded_Entity (Subp_Id); | |
232 | Check_Delayed_Subprogram (Subp_Id); | |
996ae0b0 | 233 | |
241ebe89 | 234 | Set_Categorization_From_Scope (Subp_Id, Scop); |
996ae0b0 | 235 | |
241ebe89 HK |
236 | if Ekind (Scope (Subp_Id)) = E_Protected_Type then |
237 | Error_Msg_N ("abstract subprogram not allowed in protected type", N); | |
5d37ba92 ES |
238 | |
239 | -- Issue a warning if the abstract subprogram is neither a dispatching | |
240 | -- operation nor an operation that overrides an inherited subprogram or | |
241 | -- predefined operator, since this most likely indicates a mistake. | |
242 | ||
243 | elsif Warn_On_Redundant_Constructs | |
241ebe89 HK |
244 | and then not Is_Dispatching_Operation (Subp_Id) |
245 | and then not Present (Overridden_Operation (Subp_Id)) | |
246 | and then (not Is_Operator_Symbol_Name (Chars (Subp_Id)) | |
247 | or else Scop /= Scope (Etype (First_Formal (Subp_Id)))) | |
5d37ba92 ES |
248 | then |
249 | Error_Msg_N | |
dbfeb4fa | 250 | ("abstract subprogram is not dispatching or overriding?r?", N); |
996ae0b0 | 251 | end if; |
fbf5a39b | 252 | |
241ebe89 HK |
253 | Generate_Reference_To_Formals (Subp_Id); |
254 | Check_Eliminated (Subp_Id); | |
eaba57fb RD |
255 | |
256 | if Has_Aspects (N) then | |
241ebe89 | 257 | Analyze_Aspect_Specifications (N, Subp_Id); |
eaba57fb | 258 | end if; |
996ae0b0 RK |
259 | end Analyze_Abstract_Subprogram_Declaration; |
260 | ||
b0186f71 AC |
261 | --------------------------------- |
262 | -- Analyze_Expression_Function -- | |
263 | --------------------------------- | |
264 | ||
265 | procedure Analyze_Expression_Function (N : Node_Id) is | |
8d1fe980 AC |
266 | Expr : constant Node_Id := Expression (N); |
267 | Loc : constant Source_Ptr := Sloc (N); | |
268 | LocX : constant Source_Ptr := Sloc (Expr); | |
269 | Spec : constant Node_Id := Specification (N); | |
d2d4b355 | 270 | |
ec225529 AC |
271 | Asp : Node_Id; |
272 | Def_Id : Entity_Id; | |
273 | New_Body : Node_Id; | |
274 | New_Spec : Node_Id; | |
275 | Orig_N : Node_Id; | |
276 | Ret : Node_Id; | |
354ae449 | 277 | Ret_Type : Entity_Id; |
b0186f71 | 278 | |
d29f68cf | 279 | Prev : Entity_Id; |
b0186f71 | 280 | -- If the expression is a completion, Prev is the entity whose |
d2d4b355 AC |
281 | -- declaration is completed. Def_Id is needed to analyze the spec. |
282 | ||
b0186f71 AC |
283 | begin |
284 | -- This is one of the occasions on which we transform the tree during | |
afc8324d | 285 | -- semantic analysis. If this is a completion, transform the expression |
d2b10647 ES |
286 | -- function into an equivalent subprogram body, and analyze it. |
287 | ||
288 | -- Expression functions are inlined unconditionally. The back-end will | |
289 | -- determine whether this is possible. | |
290 | ||
291 | Inline_Processing_Required := True; | |
b727a82b | 292 | |
8d1fe980 AC |
293 | -- Create a specification for the generated body. This must be done |
294 | -- prior to the analysis of the initial declaration. | |
b727a82b | 295 | |
8d1fe980 AC |
296 | New_Spec := Copy_Subprogram_Spec (Spec); |
297 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
d2d4b355 AC |
298 | |
299 | -- If there are previous overloadable entities with the same name, | |
300 | -- check whether any of them is completed by the expression function. | |
b04d926e | 301 | -- In a generic context a formal subprogram has no completion. |
d2d4b355 | 302 | |
4b6f99f5 RD |
303 | if Present (Prev) |
304 | and then Is_Overloadable (Prev) | |
b04d926e AC |
305 | and then not Is_Formal_Subprogram (Prev) |
306 | then | |
51597c23 AC |
307 | Def_Id := Analyze_Subprogram_Specification (Spec); |
308 | Prev := Find_Corresponding_Spec (N); | |
35e7063a AC |
309 | |
310 | -- The previous entity may be an expression function as well, in | |
311 | -- which case the redeclaration is illegal. | |
312 | ||
313 | if Present (Prev) | |
5073ad7a AC |
314 | and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) = |
315 | N_Expression_Function | |
35e7063a | 316 | then |
bc5e261c ES |
317 | Error_Msg_Sloc := Sloc (Prev); |
318 | Error_Msg_N ("& conflicts with declaration#", Def_Id); | |
35e7063a AC |
319 | return; |
320 | end if; | |
d2d4b355 | 321 | end if; |
b0186f71 | 322 | |
b913199e AC |
323 | Ret := Make_Simple_Return_Statement (LocX, Expression (N)); |
324 | ||
b0186f71 AC |
325 | New_Body := |
326 | Make_Subprogram_Body (Loc, | |
d2d4b355 | 327 | Specification => New_Spec, |
b0186f71 AC |
328 | Declarations => Empty_List, |
329 | Handled_Statement_Sequence => | |
330 | Make_Handled_Sequence_Of_Statements (LocX, | |
b913199e | 331 | Statements => New_List (Ret))); |
549cc9c2 | 332 | Set_Was_Expression_Function (New_Body); |
b0186f71 | 333 | |
e7f23f06 AC |
334 | -- If the expression completes a generic subprogram, we must create a |
335 | -- separate node for the body, because at instantiation the original | |
336 | -- node of the generic copy must be a generic subprogram body, and | |
337 | -- cannot be a expression function. Otherwise we just rewrite the | |
338 | -- expression with the non-generic body. | |
339 | ||
6d7e5c54 | 340 | if Present (Prev) and then Ekind (Prev) = E_Generic_Function then |
e7f23f06 | 341 | Insert_After (N, New_Body); |
6d7e5c54 | 342 | |
e7f23f06 AC |
343 | -- Propagate any aspects or pragmas that apply to the expression |
344 | -- function to the proper body when the expression function acts | |
345 | -- as a completion. | |
346 | ||
347 | if Has_Aspects (N) then | |
348 | Move_Aspects (N, To => New_Body); | |
349 | end if; | |
350 | ||
351 | Relocate_Pragmas_To_Body (New_Body); | |
b0186f71 | 352 | |
b0186f71 | 353 | Rewrite (N, Make_Null_Statement (Loc)); |
d2d4b355 | 354 | Set_Has_Completion (Prev, False); |
b0186f71 AC |
355 | Analyze (N); |
356 | Analyze (New_Body); | |
d2b10647 | 357 | Set_Is_Inlined (Prev); |
b0186f71 | 358 | |
e5c4e2bc | 359 | -- If the expression function is a completion, the previous declaration |
88fa9a24 | 360 | -- must come from source. We know already that it appears in the current |
e5c4e2bc AC |
361 | -- scope. The entity itself may be internally created if within a body |
362 | -- to be inlined. | |
363 | ||
4b6f99f5 RD |
364 | elsif Present (Prev) |
365 | and then Comes_From_Source (Parent (Prev)) | |
b04d926e AC |
366 | and then not Is_Formal_Subprogram (Prev) |
367 | then | |
d2d4b355 | 368 | Set_Has_Completion (Prev, False); |
88fa9a24 | 369 | Set_Is_Inlined (Prev); |
354ae449 | 370 | Ret_Type := Etype (Prev); |
76264f60 | 371 | |
54740d7d AC |
372 | -- An expression function which acts as 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 | 379 | |
54740d7d | 380 | Freeze_Before (N, Ret_Type); |
354ae449 | 381 | |
54740d7d AC |
382 | -- An entity can only be frozen if it is complete, so if the type |
383 | -- is still unfrozen it must still be incomplete in some way, e.g. | |
4b7fd131 | 384 | -- a private type without a full view, or a type derived from such |
bf604a5e AC |
385 | -- in an enclosing scope. Except in a generic context (where the |
386 | -- type may be a generic formal or derived from such), such use of | |
4b7fd131 AC |
387 | -- an incomplete type is an error. On the other hand, if this is a |
388 | -- limited view of a type, the type is declared in another unit and | |
389 | -- frozen there. We must be in a context seeing the nonlimited view | |
390 | -- of the type, which will be installed when the body is compiled. | |
54740d7d AC |
391 | |
392 | if not Is_Frozen (Ret_Type) | |
bf604a5e | 393 | and then not Is_Generic_Type (Root_Type (Ret_Type)) |
54740d7d | 394 | and then not Inside_A_Generic |
354ae449 | 395 | then |
4b7fd131 AC |
396 | if From_Limited_With (Ret_Type) |
397 | and then Present (Non_Limited_View (Ret_Type)) | |
398 | then | |
399 | null; | |
400 | else | |
401 | Error_Msg_NE | |
402 | ("premature use of private type&", | |
403 | Result_Definition (Specification (N)), Ret_Type); | |
404 | end if; | |
354ae449 | 405 | end if; |
1b31321b | 406 | |
76264f60 AC |
407 | -- For navigation purposes, indicate that the function is a body |
408 | ||
409 | Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True); | |
b0186f71 | 410 | Rewrite (N, New_Body); |
e7f23f06 | 411 | |
ec225529 | 412 | -- Remove any existing aspects from the original node because the act |
60aa5228 | 413 | -- of rewriting causes the list to be shared between the two nodes. |
c0cdbd39 | 414 | |
ec225529 AC |
415 | Orig_N := Original_Node (N); |
416 | Remove_Aspects (Orig_N); | |
c0cdbd39 | 417 | |
aaa0a838 | 418 | -- Propagate any pragmas that apply to expression function to the |
e7f23f06 AC |
419 | -- proper body when the expression function acts as a completion. |
420 | -- Aspects are automatically transfered because of node rewriting. | |
421 | ||
422 | Relocate_Pragmas_To_Body (N); | |
d2b10647 ES |
423 | Analyze (N); |
424 | ||
60aa5228 AC |
425 | -- Once the aspects of the generated body have been analyzed, create |
426 | -- a copy for ASIS purposes and associate it with the original node. | |
ec225529 AC |
427 | |
428 | if Has_Aspects (N) then | |
429 | Set_Aspect_Specifications (Orig_N, | |
430 | New_Copy_List_Tree (Aspect_Specifications (N))); | |
431 | end if; | |
432 | ||
6d7e5c54 AC |
433 | -- Prev is the previous entity with the same name, but it is can |
434 | -- be an unrelated spec that is not completed by the expression | |
435 | -- function. In that case the relevant entity is the one in the body. | |
436 | -- Not clear that the backend can inline it in this case ??? | |
437 | ||
438 | if Has_Completion (Prev) then | |
31af8899 AC |
439 | |
440 | -- The formals of the expression function are body formals, | |
441 | -- and do not appear in the ali file, which will only contain | |
442 | -- references to the formals of the original subprogram spec. | |
443 | ||
444 | declare | |
445 | F1 : Entity_Id; | |
446 | F2 : Entity_Id; | |
447 | ||
448 | begin | |
449 | F1 := First_Formal (Def_Id); | |
450 | F2 := First_Formal (Prev); | |
451 | ||
452 | while Present (F1) loop | |
453 | Set_Spec_Entity (F1, F2); | |
454 | Next_Formal (F1); | |
455 | Next_Formal (F2); | |
456 | end loop; | |
457 | end; | |
458 | ||
6d7e5c54 AC |
459 | else |
460 | Set_Is_Inlined (Defining_Entity (New_Body)); | |
461 | end if; | |
462 | ||
0b5b2bbc | 463 | -- If this is not a completion, create both a declaration and a body, so |
6d7e5c54 | 464 | -- that the expression can be inlined whenever possible. |
d2b10647 ES |
465 | |
466 | else | |
a52e6d7e AC |
467 | -- An expression function that is not a completion is not a |
468 | -- subprogram declaration, and thus cannot appear in a protected | |
469 | -- definition. | |
470 | ||
471 | if Nkind (Parent (N)) = N_Protected_Definition then | |
472 | Error_Msg_N | |
473 | ("an expression function is not a legal protected operation", N); | |
474 | end if; | |
475 | ||
b8e6830b | 476 | Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec)); |
c0cdbd39 | 477 | |
ec225529 | 478 | -- Remove any existing aspects from the original node because the act |
60aa5228 | 479 | -- of rewriting causes the list to be shared between the two nodes. |
c0cdbd39 | 480 | |
ec225529 AC |
481 | Orig_N := Original_Node (N); |
482 | Remove_Aspects (Orig_N); | |
c0cdbd39 | 483 | |
b0186f71 | 484 | Analyze (N); |
ec225529 | 485 | |
60aa5228 AC |
486 | -- Once the aspects of the generated spec have been analyzed, create |
487 | -- a copy for ASIS purposes and associate it with the original node. | |
ec225529 AC |
488 | |
489 | if Has_Aspects (N) then | |
490 | Set_Aspect_Specifications (Orig_N, | |
491 | New_Copy_List_Tree (Aspect_Specifications (N))); | |
492 | end if; | |
b04d926e | 493 | |
d29f68cf AC |
494 | -- If aspect SPARK_Mode was specified on the body, it needs to be |
495 | -- repeated both on the generated spec and the body. | |
496 | ||
497 | Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode); | |
498 | ||
499 | if Present (Asp) then | |
500 | Asp := New_Copy_Tree (Asp); | |
501 | Set_Analyzed (Asp, False); | |
502 | Set_Aspect_Specifications (New_Body, New_List (Asp)); | |
503 | end if; | |
504 | ||
ec225529 AC |
505 | Def_Id := Defining_Entity (N); |
506 | ||
e699b76e AC |
507 | -- Within a generic pre-analyze the original expression for name |
508 | -- capture. The body is also generated but plays no role in | |
509 | -- this because it is not part of the original source. | |
b04d926e AC |
510 | |
511 | if Inside_A_Generic then | |
b420ba79 AC |
512 | Set_Has_Completion (Def_Id); |
513 | Push_Scope (Def_Id); | |
514 | Install_Formals (Def_Id); | |
515 | Preanalyze_Spec_Expression (Expr, Etype (Def_Id)); | |
516 | End_Scope; | |
b04d926e AC |
517 | end if; |
518 | ||
b8e6830b AC |
519 | Set_Is_Inlined (Defining_Entity (N)); |
520 | ||
521 | -- Establish the linkages between the spec and the body. These are | |
522 | -- used when the expression function acts as the prefix of attribute | |
523 | -- 'Access in order to freeze the original expression which has been | |
524 | -- moved to the generated body. | |
525 | ||
526 | Set_Corresponding_Body (N, Defining_Entity (New_Body)); | |
527 | Set_Corresponding_Spec (New_Body, Defining_Entity (N)); | |
d2b10647 | 528 | |
6d7e5c54 AC |
529 | -- To prevent premature freeze action, insert the new body at the end |
530 | -- of the current declarations, or at the end of the package spec. | |
b913199e | 531 | -- However, resolve usage names now, to prevent spurious visibility |
ad4e3362 ES |
532 | -- on later entities. Note that the function can now be called in |
533 | -- the current declarative part, which will appear to be prior to | |
534 | -- the presence of the body in the code. There are nevertheless no | |
535 | -- order of elaboration issues because all name resolution has taken | |
536 | -- place at the point of declaration. | |
6d7e5c54 AC |
537 | |
538 | declare | |
e876c43a | 539 | Decls : List_Id := List_Containing (N); |
b420ba79 | 540 | Expr : constant Node_Id := Expression (Ret); |
e876c43a | 541 | Par : constant Node_Id := Parent (Decls); |
b420ba79 | 542 | Typ : constant Entity_Id := Etype (Def_Id); |
6d7e5c54 AC |
543 | |
544 | begin | |
fce54763 AC |
545 | -- If this is a wrapper created for in an instance for a formal |
546 | -- subprogram, insert body after declaration, to be analyzed when | |
547 | -- the enclosing instance is analyzed. | |
548 | ||
549 | if GNATprove_Mode | |
550 | and then Is_Generic_Actual_Subprogram (Defining_Entity (N)) | |
6d7e5c54 | 551 | then |
fce54763 AC |
552 | Insert_After (N, New_Body); |
553 | ||
554 | else | |
555 | if Nkind (Par) = N_Package_Specification | |
556 | and then Decls = Visible_Declarations (Par) | |
557 | and then Present (Private_Declarations (Par)) | |
558 | and then not Is_Empty_List (Private_Declarations (Par)) | |
559 | then | |
560 | Decls := Private_Declarations (Par); | |
561 | end if; | |
6d7e5c54 | 562 | |
fce54763 | 563 | Insert_After (Last (Decls), New_Body); |
3a8e3f63 | 564 | |
fce54763 AC |
565 | -- Preanalyze the expression for name capture, except in an |
566 | -- instance, where this has been done during generic analysis, | |
567 | -- and will be redone when analyzing the body. | |
845f06e2 | 568 | |
b420ba79 AC |
569 | Set_Parent (Expr, Ret); |
570 | Push_Scope (Def_Id); | |
571 | Install_Formals (Def_Id); | |
4058ddcc | 572 | |
b420ba79 AC |
573 | if not In_Instance then |
574 | Preanalyze_Spec_Expression (Expr, Typ); | |
575 | Check_Limited_Return (Original_Node (N), Expr, Typ); | |
576 | end if; | |
3a8e3f63 | 577 | |
fce54763 AC |
578 | End_Scope; |
579 | end if; | |
6d7e5c54 | 580 | end; |
b0186f71 | 581 | end if; |
0b5b2bbc AC |
582 | |
583 | -- If the return expression is a static constant, we suppress warning | |
584 | -- messages on unused formals, which in most cases will be noise. | |
585 | ||
b420ba79 AC |
586 | Set_Is_Trivial_Subprogram |
587 | (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr)); | |
b0186f71 AC |
588 | end Analyze_Expression_Function; |
589 | ||
ec4867fa ES |
590 | ---------------------------------------- |
591 | -- Analyze_Extended_Return_Statement -- | |
592 | ---------------------------------------- | |
593 | ||
594 | procedure Analyze_Extended_Return_Statement (N : Node_Id) is | |
595 | begin | |
c86cf714 | 596 | Check_Compiler_Unit ("extended return statement", N); |
5d37ba92 | 597 | Analyze_Return_Statement (N); |
ec4867fa ES |
598 | end Analyze_Extended_Return_Statement; |
599 | ||
996ae0b0 RK |
600 | ---------------------------- |
601 | -- Analyze_Function_Call -- | |
602 | ---------------------------- | |
603 | ||
604 | procedure Analyze_Function_Call (N : Node_Id) is | |
a7e68e7f HK |
605 | Actuals : constant List_Id := Parameter_Associations (N); |
606 | Func_Nam : constant Node_Id := Name (N); | |
607 | Actual : Node_Id; | |
608 | ||
996ae0b0 | 609 | begin |
a7e68e7f | 610 | Analyze (Func_Nam); |
996ae0b0 | 611 | |
3e7302c3 AC |
612 | -- A call of the form A.B (X) may be an Ada 2005 call, which is |
613 | -- rewritten as B (A, X). If the rewriting is successful, the call | |
614 | -- has been analyzed and we just return. | |
82c80734 | 615 | |
a7e68e7f HK |
616 | if Nkind (Func_Nam) = N_Selected_Component |
617 | and then Name (N) /= Func_Nam | |
82c80734 RD |
618 | and then Is_Rewrite_Substitution (N) |
619 | and then Present (Etype (N)) | |
620 | then | |
621 | return; | |
622 | end if; | |
623 | ||
996ae0b0 RK |
624 | -- If error analyzing name, then set Any_Type as result type and return |
625 | ||
a7e68e7f | 626 | if Etype (Func_Nam) = Any_Type then |
996ae0b0 RK |
627 | Set_Etype (N, Any_Type); |
628 | return; | |
629 | end if; | |
630 | ||
631 | -- Otherwise analyze the parameters | |
632 | ||
e24329cd YM |
633 | if Present (Actuals) then |
634 | Actual := First (Actuals); | |
996ae0b0 RK |
635 | while Present (Actual) loop |
636 | Analyze (Actual); | |
637 | Check_Parameterless_Call (Actual); | |
638 | Next (Actual); | |
639 | end loop; | |
640 | end if; | |
641 | ||
642 | Analyze_Call (N); | |
996ae0b0 RK |
643 | end Analyze_Function_Call; |
644 | ||
ec4867fa ES |
645 | ----------------------------- |
646 | -- Analyze_Function_Return -- | |
647 | ----------------------------- | |
648 | ||
649 | procedure Analyze_Function_Return (N : Node_Id) is | |
a7e68e7f HK |
650 | Loc : constant Source_Ptr := Sloc (N); |
651 | Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); | |
652 | Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); | |
ec4867fa | 653 | |
5d37ba92 | 654 | R_Type : constant Entity_Id := Etype (Scope_Id); |
ec4867fa ES |
655 | -- Function result subtype |
656 | ||
ed11bbfe | 657 | procedure Check_Aggregate_Accessibility (Aggr : Node_Id); |
448a1eb3 | 658 | -- Apply legality rule of 6.5 (5.8) to the access discriminants of an |
43184ab7 | 659 | -- aggregate in a return statement. |
ed11bbfe | 660 | |
ec4867fa ES |
661 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); |
662 | -- Check that the return_subtype_indication properly matches the result | |
663 | -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). | |
664 | ||
ed11bbfe AC |
665 | ----------------------------------- |
666 | -- Check_Aggregate_Accessibility -- | |
667 | ----------------------------------- | |
668 | ||
669 | procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is | |
996ce809 JS |
670 | Typ : constant Entity_Id := Etype (Aggr); |
671 | Assoc : Node_Id; | |
672 | Discr : Entity_Id; | |
673 | Expr : Node_Id; | |
674 | Obj : Node_Id; | |
ed11bbfe AC |
675 | |
676 | begin | |
43184ab7 | 677 | if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then |
ed11bbfe AC |
678 | Discr := First_Discriminant (Typ); |
679 | Assoc := First (Component_Associations (Aggr)); | |
680 | while Present (Discr) loop | |
681 | if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then | |
682 | Expr := Expression (Assoc); | |
996ce809 | 683 | |
ed11bbfe AC |
684 | if Nkind (Expr) = N_Attribute_Reference |
685 | and then Attribute_Name (Expr) /= Name_Unrestricted_Access | |
686 | then | |
687 | Obj := Prefix (Expr); | |
43184ab7 HK |
688 | while Nkind_In (Obj, N_Indexed_Component, |
689 | N_Selected_Component) | |
ed11bbfe AC |
690 | loop |
691 | Obj := Prefix (Obj); | |
692 | end loop; | |
693 | ||
996ce809 JS |
694 | -- Do not check aliased formals or function calls. A |
695 | -- run-time check may still be needed ??? | |
ed962eda | 696 | |
ed11bbfe | 697 | if Is_Entity_Name (Obj) |
996ce809 | 698 | and then Comes_From_Source (Obj) |
ed11bbfe | 699 | then |
996ce809 JS |
700 | if Is_Formal (Entity (Obj)) |
701 | and then Is_Aliased (Entity (Obj)) | |
702 | then | |
703 | null; | |
704 | ||
705 | elsif Object_Access_Level (Obj) > | |
706 | Scope_Depth (Scope (Scope_Id)) | |
707 | then | |
708 | Error_Msg_N | |
709 | ("access discriminant in return aggregate would " | |
710 | & "be a dangling reference", Obj); | |
711 | end if; | |
ed11bbfe AC |
712 | end if; |
713 | end if; | |
714 | end if; | |
715 | ||
716 | Next_Discriminant (Discr); | |
717 | end loop; | |
718 | end if; | |
719 | end Check_Aggregate_Accessibility; | |
720 | ||
ec4867fa ES |
721 | ------------------------------------- |
722 | -- Check_Return_Subtype_Indication -- | |
723 | ------------------------------------- | |
724 | ||
725 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is | |
7665e4bd AC |
726 | Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); |
727 | ||
728 | R_Stm_Type : constant Entity_Id := Etype (Return_Obj); | |
729 | -- Subtype given in the extended return statement (must match R_Type) | |
ec4867fa ES |
730 | |
731 | Subtype_Ind : constant Node_Id := | |
732 | Object_Definition (Original_Node (Obj_Decl)); | |
733 | ||
7f568bfa AC |
734 | procedure Error_No_Match (N : Node_Id); |
735 | -- Output error messages for case where types do not statically | |
736 | -- match. N is the location for the messages. | |
737 | ||
738 | -------------------- | |
739 | -- Error_No_Match -- | |
740 | -------------------- | |
741 | ||
742 | procedure Error_No_Match (N : Node_Id) is | |
743 | begin | |
744 | Error_Msg_N | |
745 | ("subtype must statically match function result subtype", N); | |
746 | ||
747 | if not Predicates_Match (R_Stm_Type, R_Type) then | |
748 | Error_Msg_Node_2 := R_Type; | |
749 | Error_Msg_NE | |
3ccedacc | 750 | ("\predicate of& does not match predicate of&", |
7f568bfa AC |
751 | N, R_Stm_Type); |
752 | end if; | |
753 | end Error_No_Match; | |
754 | ||
755 | -- Start of processing for Check_Return_Subtype_Indication | |
756 | ||
ec4867fa | 757 | begin |
7665e4bd | 758 | -- First, avoid cascaded errors |
ec4867fa ES |
759 | |
760 | if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then | |
761 | return; | |
762 | end if; | |
763 | ||
764 | -- "return access T" case; check that the return statement also has | |
765 | -- "access T", and that the subtypes statically match: | |
53cf4600 | 766 | -- if this is an access to subprogram the signatures must match. |
ec4867fa | 767 | |
d1eb8a82 AC |
768 | if Is_Anonymous_Access_Type (R_Type) then |
769 | if Is_Anonymous_Access_Type (R_Stm_Type) then | |
770 | if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type | |
0a36105d | 771 | then |
53cf4600 ES |
772 | if Base_Type (Designated_Type (R_Stm_Type)) /= |
773 | Base_Type (Designated_Type (R_Type)) | |
774 | or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) | |
775 | then | |
7f568bfa | 776 | Error_No_Match (Subtype_Mark (Subtype_Ind)); |
53cf4600 ES |
777 | end if; |
778 | ||
779 | else | |
d1eb8a82 AC |
780 | -- For two anonymous access to subprogram types, the types |
781 | -- themselves must be type conformant. | |
53cf4600 ES |
782 | |
783 | if not Conforming_Types | |
d1eb8a82 | 784 | (R_Stm_Type, R_Type, Fully_Conformant) |
53cf4600 | 785 | then |
7f568bfa | 786 | Error_No_Match (Subtype_Ind); |
53cf4600 | 787 | end if; |
ec4867fa | 788 | end if; |
0a36105d | 789 | |
ec4867fa ES |
790 | else |
791 | Error_Msg_N ("must use anonymous access type", Subtype_Ind); | |
792 | end if; | |
793 | ||
6cce2156 GD |
794 | -- If the return object is of an anonymous access type, then report |
795 | -- an error if the function's result type is not also anonymous. | |
796 | ||
d1eb8a82 AC |
797 | elsif Is_Anonymous_Access_Type (R_Stm_Type) then |
798 | pragma Assert (not Is_Anonymous_Access_Type (R_Type)); | |
799 | Error_Msg_N | |
800 | ("anonymous access not allowed for function with named access " | |
801 | & "result", Subtype_Ind); | |
6cce2156 | 802 | |
81d93365 AC |
803 | -- Subtype indication case: check that the return object's type is |
804 | -- covered by the result type, and that the subtypes statically match | |
805 | -- when the result subtype is constrained. Also handle record types | |
806 | -- with unknown discriminants for which we have built the underlying | |
807 | -- record view. Coverage is needed to allow specific-type return | |
808 | -- objects when the result type is class-wide (see AI05-32). | |
809 | ||
810 | elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) | |
9013065b | 811 | or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) |
212863c0 AC |
812 | and then |
813 | Covers | |
814 | (Base_Type (R_Type), | |
815 | Underlying_Record_View (Base_Type (R_Stm_Type)))) | |
9013065b AC |
816 | then |
817 | -- A null exclusion may be present on the return type, on the | |
818 | -- function specification, on the object declaration or on the | |
819 | -- subtype itself. | |
ec4867fa | 820 | |
21d27997 RD |
821 | if Is_Access_Type (R_Type) |
822 | and then | |
d1eb8a82 AC |
823 | (Can_Never_Be_Null (R_Type) |
824 | or else Null_Exclusion_Present (Parent (Scope_Id))) /= | |
825 | Can_Never_Be_Null (R_Stm_Type) | |
21d27997 | 826 | then |
7f568bfa | 827 | Error_No_Match (Subtype_Ind); |
21d27997 RD |
828 | end if; |
829 | ||
105b5e65 | 830 | -- AI05-103: for elementary types, subtypes must statically match |
8779dffa | 831 | |
d1eb8a82 | 832 | if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then |
ec4867fa | 833 | if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then |
7f568bfa | 834 | Error_No_Match (Subtype_Ind); |
ec4867fa ES |
835 | end if; |
836 | end if; | |
837 | ||
a8b346d2 RD |
838 | -- All remaining cases are illegal |
839 | ||
840 | -- Note: previous versions of this subprogram allowed the return | |
841 | -- value to be the ancestor of the return type if the return type | |
842 | -- was a null extension. This was plainly incorrect. | |
ff7139c3 | 843 | |
ec4867fa ES |
844 | else |
845 | Error_Msg_N | |
846 | ("wrong type for return_subtype_indication", Subtype_Ind); | |
847 | end if; | |
848 | end Check_Return_Subtype_Indication; | |
849 | ||
850 | --------------------- | |
851 | -- Local Variables -- | |
852 | --------------------- | |
853 | ||
445e5888 AC |
854 | Expr : Node_Id; |
855 | Obj_Decl : Node_Id; | |
ec4867fa ES |
856 | |
857 | -- Start of processing for Analyze_Function_Return | |
858 | ||
859 | begin | |
860 | Set_Return_Present (Scope_Id); | |
861 | ||
5d37ba92 | 862 | if Nkind (N) = N_Simple_Return_Statement then |
ec4867fa | 863 | Expr := Expression (N); |
4ee646da | 864 | |
e917aec2 RD |
865 | -- Guard against a malformed expression. The parser may have tried to |
866 | -- recover but the node is not analyzable. | |
4ee646da AC |
867 | |
868 | if Nkind (Expr) = N_Error then | |
869 | Set_Etype (Expr, Any_Type); | |
870 | Expander_Mode_Save_And_Set (False); | |
871 | return; | |
872 | ||
873 | else | |
0180fd26 AC |
874 | -- The resolution of a controlled [extension] aggregate associated |
875 | -- with a return statement creates a temporary which needs to be | |
876 | -- finalized on function exit. Wrap the return statement inside a | |
877 | -- block so that the finalization machinery can detect this case. | |
878 | -- This early expansion is done only when the return statement is | |
879 | -- not part of a handled sequence of statements. | |
880 | ||
881 | if Nkind_In (Expr, N_Aggregate, | |
882 | N_Extension_Aggregate) | |
883 | and then Needs_Finalization (R_Type) | |
884 | and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements | |
885 | then | |
886 | Rewrite (N, | |
887 | Make_Block_Statement (Loc, | |
888 | Handled_Statement_Sequence => | |
889 | Make_Handled_Sequence_Of_Statements (Loc, | |
890 | Statements => New_List (Relocate_Node (N))))); | |
891 | ||
892 | Analyze (N); | |
893 | return; | |
894 | end if; | |
895 | ||
4b963531 AC |
896 | Analyze (Expr); |
897 | ||
898 | -- Ada 2005 (AI-251): If the type of the returned object is | |
899 | -- an access to an interface type then we add an implicit type | |
900 | -- conversion to force the displacement of the "this" pointer to | |
901 | -- reference the secondary dispatch table. We cannot delay the | |
902 | -- generation of this implicit conversion until the expansion | |
903 | -- because in this case the type resolution changes the decoration | |
904 | -- of the expression node to match R_Type; by contrast, if the | |
905 | -- returned object is a class-wide interface type then it is too | |
906 | -- early to generate here the implicit conversion since the return | |
907 | -- statement may be rewritten by the expander into an extended | |
908 | -- return statement whose expansion takes care of adding the | |
909 | -- implicit type conversion to displace the pointer to the object. | |
910 | ||
911 | if Expander_Active | |
912 | and then Serious_Errors_Detected = 0 | |
913 | and then Is_Access_Type (R_Type) | |
914 | and then Nkind (Expr) /= N_Null | |
915 | and then Is_Interface (Designated_Type (R_Type)) | |
916 | and then Is_Progenitor (Designated_Type (R_Type), | |
917 | Designated_Type (Etype (Expr))) | |
918 | then | |
73e5aa55 | 919 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); |
4b963531 AC |
920 | Analyze (Expr); |
921 | end if; | |
922 | ||
923 | Resolve (Expr, R_Type); | |
b420ba79 | 924 | Check_Limited_Return (N, Expr, R_Type); |
ed11bbfe AC |
925 | |
926 | if Present (Expr) and then Nkind (Expr) = N_Aggregate then | |
927 | Check_Aggregate_Accessibility (Expr); | |
928 | end if; | |
4ee646da | 929 | end if; |
ec4867fa | 930 | |
ad05f2e9 | 931 | -- RETURN only allowed in SPARK as the last statement in function |
607d0635 | 932 | |
fe5d3068 | 933 | if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements |
607d0635 AC |
934 | and then |
935 | (Nkind (Parent (Parent (N))) /= N_Subprogram_Body | |
8d606a78 | 936 | or else Present (Next (N))) |
607d0635 | 937 | then |
ce5ba43a | 938 | Check_SPARK_05_Restriction |
fe5d3068 | 939 | ("RETURN should be the last statement in function", N); |
607d0635 AC |
940 | end if; |
941 | ||
ec4867fa | 942 | else |
ce5ba43a | 943 | Check_SPARK_05_Restriction ("extended RETURN is not allowed", N); |
445e5888 | 944 | Obj_Decl := Last (Return_Object_Declarations (N)); |
607d0635 | 945 | |
ec4867fa ES |
946 | -- Analyze parts specific to extended_return_statement: |
947 | ||
948 | declare | |
de6cad7c | 949 | Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl); |
b9daa96e | 950 | HSS : constant Node_Id := Handled_Statement_Sequence (N); |
ec4867fa ES |
951 | |
952 | begin | |
953 | Expr := Expression (Obj_Decl); | |
954 | ||
955 | -- Note: The check for OK_For_Limited_Init will happen in | |
956 | -- Analyze_Object_Declaration; we treat it as a normal | |
957 | -- object declaration. | |
958 | ||
cd1c668b | 959 | Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); |
ec4867fa ES |
960 | Analyze (Obj_Decl); |
961 | ||
ec4867fa ES |
962 | Check_Return_Subtype_Indication (Obj_Decl); |
963 | ||
964 | if Present (HSS) then | |
965 | Analyze (HSS); | |
966 | ||
967 | if Present (Exception_Handlers (HSS)) then | |
968 | ||
969 | -- ???Has_Nested_Block_With_Handler needs to be set. | |
970 | -- Probably by creating an actual N_Block_Statement. | |
971 | -- Probably in Expand. | |
972 | ||
973 | null; | |
974 | end if; | |
975 | end if; | |
976 | ||
9337aa0a AC |
977 | -- Mark the return object as referenced, since the return is an |
978 | -- implicit reference of the object. | |
979 | ||
980 | Set_Referenced (Defining_Identifier (Obj_Decl)); | |
981 | ||
ec4867fa | 982 | Check_References (Stm_Entity); |
de6cad7c AC |
983 | |
984 | -- Check RM 6.5 (5.9/3) | |
985 | ||
986 | if Has_Aliased then | |
987 | if Ada_Version < Ada_2012 then | |
dbfeb4fa RD |
988 | |
989 | -- Shouldn't this test Warn_On_Ada_2012_Compatibility ??? | |
990 | -- Can it really happen (extended return???) | |
991 | ||
992 | Error_Msg_N | |
b785e0b8 AC |
993 | ("aliased only allowed for limited return objects " |
994 | & "in Ada 2012??", N); | |
de6cad7c | 995 | |
51245e2d | 996 | elsif not Is_Limited_View (R_Type) then |
3ccedacc AC |
997 | Error_Msg_N |
998 | ("aliased only allowed for limited return objects", N); | |
de6cad7c AC |
999 | end if; |
1000 | end if; | |
ec4867fa ES |
1001 | end; |
1002 | end if; | |
1003 | ||
21d27997 | 1004 | -- Case of Expr present |
5d37ba92 | 1005 | |
ec4867fa | 1006 | if Present (Expr) |
21d27997 | 1007 | |
8fde064e | 1008 | -- Defend against previous errors |
21d27997 RD |
1009 | |
1010 | and then Nkind (Expr) /= N_Empty | |
5d37ba92 | 1011 | and then Present (Etype (Expr)) |
ec4867fa | 1012 | then |
5d37ba92 ES |
1013 | -- Apply constraint check. Note that this is done before the implicit |
1014 | -- conversion of the expression done for anonymous access types to | |
f3d57416 | 1015 | -- ensure correct generation of the null-excluding check associated |
5d37ba92 ES |
1016 | -- with null-excluding expressions found in return statements. |
1017 | ||
1018 | Apply_Constraint_Check (Expr, R_Type); | |
1019 | ||
1020 | -- Ada 2005 (AI-318-02): When the result type is an anonymous access | |
1021 | -- type, apply an implicit conversion of the expression to that type | |
1022 | -- to force appropriate static and run-time accessibility checks. | |
ec4867fa | 1023 | |
0791fbe9 | 1024 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
1025 | and then Ekind (R_Type) = E_Anonymous_Access_Type |
1026 | then | |
1027 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); | |
1028 | Analyze_And_Resolve (Expr, R_Type); | |
b6b5cca8 AC |
1029 | |
1030 | -- If this is a local anonymous access to subprogram, the | |
1031 | -- accessibility check can be applied statically. The return is | |
1032 | -- illegal if the access type of the return expression is declared | |
1033 | -- inside of the subprogram (except if it is the subtype indication | |
1034 | -- of an extended return statement). | |
1035 | ||
9fe696a3 | 1036 | elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then |
b6b5cca8 AC |
1037 | if not Comes_From_Source (Current_Scope) |
1038 | or else Ekind (Current_Scope) = E_Return_Statement | |
1039 | then | |
1040 | null; | |
1041 | ||
1042 | elsif | |
1043 | Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id) | |
1044 | then | |
1045 | Error_Msg_N ("cannot return local access to subprogram", N); | |
1046 | end if; | |
1ebc2612 AC |
1047 | |
1048 | -- The expression cannot be of a formal incomplete type | |
1049 | ||
1050 | elsif Ekind (Etype (Expr)) = E_Incomplete_Type | |
1051 | and then Is_Generic_Type (Etype (Expr)) | |
1052 | then | |
1053 | Error_Msg_N | |
1054 | ("cannot return expression of a formal incomplete type", N); | |
ec4867fa ES |
1055 | end if; |
1056 | ||
21d27997 RD |
1057 | -- If the result type is class-wide, then check that the return |
1058 | -- expression's type is not declared at a deeper level than the | |
1059 | -- function (RM05-6.5(5.6/2)). | |
1060 | ||
0791fbe9 | 1061 | if Ada_Version >= Ada_2005 |
21d27997 RD |
1062 | and then Is_Class_Wide_Type (R_Type) |
1063 | then | |
1064 | if Type_Access_Level (Etype (Expr)) > | |
1065 | Subprogram_Access_Level (Scope_Id) | |
1066 | then | |
1067 | Error_Msg_N | |
3ccedacc AC |
1068 | ("level of return expression type is deeper than " |
1069 | & "class-wide function!", Expr); | |
21d27997 RD |
1070 | end if; |
1071 | end if; | |
1072 | ||
4755cce9 JM |
1073 | -- Check incorrect use of dynamically tagged expression |
1074 | ||
1075 | if Is_Tagged_Type (R_Type) then | |
1076 | Check_Dynamically_Tagged_Expression | |
1077 | (Expr => Expr, | |
1078 | Typ => R_Type, | |
1079 | Related_Nod => N); | |
ec4867fa ES |
1080 | end if; |
1081 | ||
ec4867fa ES |
1082 | -- ??? A real run-time accessibility check is needed in cases |
1083 | -- involving dereferences of access parameters. For now we just | |
1084 | -- check the static cases. | |
1085 | ||
0791fbe9 | 1086 | if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) |
51245e2d | 1087 | and then Is_Limited_View (Etype (Scope_Id)) |
ec4867fa ES |
1088 | and then Object_Access_Level (Expr) > |
1089 | Subprogram_Access_Level (Scope_Id) | |
1090 | then | |
9694c039 AC |
1091 | -- Suppress the message in a generic, where the rewriting |
1092 | -- is irrelevant. | |
1093 | ||
1094 | if Inside_A_Generic then | |
1095 | null; | |
1096 | ||
1097 | else | |
1098 | Rewrite (N, | |
1099 | Make_Raise_Program_Error (Loc, | |
1100 | Reason => PE_Accessibility_Check_Failed)); | |
1101 | Analyze (N); | |
1102 | ||
43417b90 | 1103 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1104 | Error_Msg_N ("cannot return a local value by reference<<", N); |
1105 | Error_Msg_NE ("\& [<<", N, Standard_Program_Error); | |
9694c039 | 1106 | end if; |
ec4867fa | 1107 | end if; |
5d37ba92 ES |
1108 | |
1109 | if Known_Null (Expr) | |
1110 | and then Nkind (Parent (Scope_Id)) = N_Function_Specification | |
1111 | and then Null_Exclusion_Present (Parent (Scope_Id)) | |
1112 | then | |
1113 | Apply_Compile_Time_Constraint_Error | |
1114 | (N => Expr, | |
1115 | Msg => "(Ada 2005) null not allowed for " | |
dbfeb4fa | 1116 | & "null-excluding return??", |
5d37ba92 ES |
1117 | Reason => CE_Null_Not_Allowed); |
1118 | end if; | |
445e5888 AC |
1119 | |
1120 | -- RM 6.5 (5.4/3): accessibility checks also apply if the return object | |
1121 | -- has no initializing expression. | |
1122 | ||
1123 | elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then | |
1124 | if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) > | |
1125 | Subprogram_Access_Level (Scope_Id) | |
1126 | then | |
1127 | Error_Msg_N | |
1128 | ("level of return expression type is deeper than " | |
1129 | & "class-wide function!", Obj_Decl); | |
1130 | end if; | |
ec4867fa ES |
1131 | end if; |
1132 | end Analyze_Function_Return; | |
1133 | ||
996ae0b0 RK |
1134 | ------------------------------------- |
1135 | -- Analyze_Generic_Subprogram_Body -- | |
1136 | ------------------------------------- | |
1137 | ||
1138 | procedure Analyze_Generic_Subprogram_Body | |
1139 | (N : Node_Id; | |
1140 | Gen_Id : Entity_Id) | |
1141 | is | |
fbf5a39b | 1142 | Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); |
996ae0b0 | 1143 | Kind : constant Entity_Kind := Ekind (Gen_Id); |
fbf5a39b | 1144 | Body_Id : Entity_Id; |
996ae0b0 | 1145 | New_N : Node_Id; |
fbf5a39b | 1146 | Spec : Node_Id; |
996ae0b0 RK |
1147 | |
1148 | begin | |
82c80734 RD |
1149 | -- Copy body and disable expansion while analyzing the generic For a |
1150 | -- stub, do not copy the stub (which would load the proper body), this | |
1151 | -- will be done when the proper body is analyzed. | |
996ae0b0 RK |
1152 | |
1153 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
1154 | New_N := Copy_Generic_Node (N, Empty, Instantiating => False); | |
1155 | Rewrite (N, New_N); | |
caf07df9 AC |
1156 | |
1157 | -- Once the contents of the generic copy and the template are | |
1158 | -- swapped, do the same for their respective aspect specifications. | |
1159 | ||
1160 | Exchange_Aspects (N, New_N); | |
1161 | ||
1162 | -- Collect all contract-related source pragmas found within the | |
1163 | -- template and attach them to the contract of the subprogram body. | |
1164 | -- This contract is used in the capture of global references within | |
1165 | -- annotations. | |
1166 | ||
1167 | Create_Generic_Contract (N); | |
1168 | ||
996ae0b0 RK |
1169 | Start_Generic; |
1170 | end if; | |
1171 | ||
1172 | Spec := Specification (N); | |
1173 | ||
1174 | -- Within the body of the generic, the subprogram is callable, and | |
1175 | -- behaves like the corresponding non-generic unit. | |
1176 | ||
fbf5a39b | 1177 | Body_Id := Defining_Entity (Spec); |
996ae0b0 RK |
1178 | |
1179 | if Kind = E_Generic_Procedure | |
1180 | and then Nkind (Spec) /= N_Procedure_Specification | |
1181 | then | |
fbf5a39b | 1182 | Error_Msg_N ("invalid body for generic procedure ", Body_Id); |
996ae0b0 RK |
1183 | return; |
1184 | ||
1185 | elsif Kind = E_Generic_Function | |
1186 | and then Nkind (Spec) /= N_Function_Specification | |
1187 | then | |
fbf5a39b | 1188 | Error_Msg_N ("invalid body for generic function ", Body_Id); |
996ae0b0 RK |
1189 | return; |
1190 | end if; | |
1191 | ||
fbf5a39b | 1192 | Set_Corresponding_Body (Gen_Decl, Body_Id); |
996ae0b0 RK |
1193 | |
1194 | if Has_Completion (Gen_Id) | |
1195 | and then Nkind (Parent (N)) /= N_Subunit | |
1196 | then | |
1197 | Error_Msg_N ("duplicate generic body", N); | |
1198 | return; | |
1199 | else | |
1200 | Set_Has_Completion (Gen_Id); | |
1201 | end if; | |
1202 | ||
1203 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1204 | Set_Ekind (Defining_Entity (Specification (N)), Kind); | |
1205 | else | |
1206 | Set_Corresponding_Spec (N, Gen_Id); | |
1207 | end if; | |
1208 | ||
1209 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1210 | Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); | |
1211 | end if; | |
1212 | ||
1213 | -- Make generic parameters immediately visible in the body. They are | |
1214 | -- needed to process the formals declarations. Then make the formals | |
1215 | -- visible in a separate step. | |
1216 | ||
0a36105d | 1217 | Push_Scope (Gen_Id); |
996ae0b0 RK |
1218 | |
1219 | declare | |
1220 | E : Entity_Id; | |
1221 | First_Ent : Entity_Id; | |
1222 | ||
1223 | begin | |
1224 | First_Ent := First_Entity (Gen_Id); | |
1225 | ||
1226 | E := First_Ent; | |
1227 | while Present (E) and then not Is_Formal (E) loop | |
1228 | Install_Entity (E); | |
1229 | Next_Entity (E); | |
1230 | end loop; | |
1231 | ||
1232 | Set_Use (Generic_Formal_Declarations (Gen_Decl)); | |
1233 | ||
1234 | -- Now generic formals are visible, and the specification can be | |
1235 | -- analyzed, for subsequent conformance check. | |
1236 | ||
fbf5a39b | 1237 | Body_Id := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 1238 | |
fbf5a39b | 1239 | -- Make formal parameters visible |
996ae0b0 RK |
1240 | |
1241 | if Present (E) then | |
1242 | ||
fbf5a39b AC |
1243 | -- E is the first formal parameter, we loop through the formals |
1244 | -- installing them so that they will be visible. | |
996ae0b0 RK |
1245 | |
1246 | Set_First_Entity (Gen_Id, E); | |
996ae0b0 RK |
1247 | while Present (E) loop |
1248 | Install_Entity (E); | |
1249 | Next_Formal (E); | |
1250 | end loop; | |
1251 | end if; | |
1252 | ||
e895b435 | 1253 | -- Visible generic entity is callable within its own body |
996ae0b0 | 1254 | |
ec4867fa ES |
1255 | Set_Ekind (Gen_Id, Ekind (Body_Id)); |
1256 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
1257 | Set_Convention (Body_Id, Convention (Gen_Id)); | |
1258 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); | |
1259 | Set_Scope (Body_Id, Scope (Gen_Id)); | |
c5cec2fe | 1260 | |
fbf5a39b AC |
1261 | Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); |
1262 | ||
1263 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1264 | ||
e895b435 | 1265 | -- No body to analyze, so restore state of generic unit |
fbf5a39b AC |
1266 | |
1267 | Set_Ekind (Gen_Id, Kind); | |
1268 | Set_Ekind (Body_Id, Kind); | |
1269 | ||
1270 | if Present (First_Ent) then | |
1271 | Set_First_Entity (Gen_Id, First_Ent); | |
1272 | end if; | |
1273 | ||
1274 | End_Scope; | |
1275 | return; | |
1276 | end if; | |
996ae0b0 | 1277 | |
82c80734 RD |
1278 | -- If this is a compilation unit, it must be made visible explicitly, |
1279 | -- because the compilation of the declaration, unlike other library | |
1280 | -- unit declarations, does not. If it is not a unit, the following | |
1281 | -- is redundant but harmless. | |
996ae0b0 RK |
1282 | |
1283 | Set_Is_Immediately_Visible (Gen_Id); | |
fbf5a39b | 1284 | Reference_Body_Formals (Gen_Id, Body_Id); |
996ae0b0 | 1285 | |
ec4867fa ES |
1286 | if Is_Child_Unit (Gen_Id) then |
1287 | Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); | |
1288 | end if; | |
1289 | ||
996ae0b0 | 1290 | Set_Actual_Subtypes (N, Current_Scope); |
483361a6 | 1291 | |
579847c2 | 1292 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); |
f145ece7 | 1293 | Set_SPARK_Pragma_Inherited (Body_Id); |
579847c2 | 1294 | |
caf07df9 AC |
1295 | -- Analyze any aspect specifications that appear on the generic |
1296 | -- subprogram body. | |
1297 | ||
1298 | if Has_Aspects (N) then | |
1299 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); | |
1300 | end if; | |
1301 | ||
996ae0b0 RK |
1302 | Analyze_Declarations (Declarations (N)); |
1303 | Check_Completion; | |
996ae0b0 | 1304 | |
879ac954 AC |
1305 | -- Process the contract of the subprogram body after all declarations |
1306 | -- have been analyzed. This ensures that any contract-related pragmas | |
1307 | -- are available through the N_Contract node of the body. | |
caf07df9 | 1308 | |
f99ff327 | 1309 | Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id); |
caf07df9 AC |
1310 | |
1311 | Analyze (Handled_Statement_Sequence (N)); | |
996ae0b0 RK |
1312 | Save_Global_References (Original_Node (N)); |
1313 | ||
82c80734 RD |
1314 | -- Prior to exiting the scope, include generic formals again (if any |
1315 | -- are present) in the set of local entities. | |
996ae0b0 RK |
1316 | |
1317 | if Present (First_Ent) then | |
1318 | Set_First_Entity (Gen_Id, First_Ent); | |
1319 | end if; | |
1320 | ||
fbf5a39b | 1321 | Check_References (Gen_Id); |
996ae0b0 RK |
1322 | end; |
1323 | ||
e6f69614 | 1324 | Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); |
996ae0b0 RK |
1325 | End_Scope; |
1326 | Check_Subprogram_Order (N); | |
1327 | ||
e895b435 | 1328 | -- Outside of its body, unit is generic again |
996ae0b0 RK |
1329 | |
1330 | Set_Ekind (Gen_Id, Kind); | |
fbf5a39b | 1331 | Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); |
5d37ba92 ES |
1332 | |
1333 | if Style_Check then | |
1334 | Style.Check_Identifier (Body_Id, Gen_Id); | |
1335 | end if; | |
13d923cc | 1336 | |
996ae0b0 | 1337 | End_Generic; |
996ae0b0 RK |
1338 | end Analyze_Generic_Subprogram_Body; |
1339 | ||
4d8f3296 ES |
1340 | ---------------------------- |
1341 | -- Analyze_Null_Procedure -- | |
1342 | ---------------------------- | |
1343 | ||
1344 | procedure Analyze_Null_Procedure | |
1345 | (N : Node_Id; | |
1346 | Is_Completion : out Boolean) | |
1347 | is | |
1348 | Loc : constant Source_Ptr := Sloc (N); | |
1349 | Spec : constant Node_Id := Specification (N); | |
1350 | Designator : Entity_Id; | |
1351 | Form : Node_Id; | |
1352 | Null_Body : Node_Id := Empty; | |
b912db16 | 1353 | Null_Stmt : Node_Id := Null_Statement (Spec); |
4d8f3296 ES |
1354 | Prev : Entity_Id; |
1355 | ||
1356 | begin | |
1357 | -- Capture the profile of the null procedure before analysis, for | |
1358 | -- expansion at the freeze point and at each point of call. The body is | |
1359 | -- used if the procedure has preconditions, or if it is a completion. In | |
1360 | -- the first case the body is analyzed at the freeze point, in the other | |
1361 | -- it replaces the null procedure declaration. | |
1362 | ||
b912db16 AC |
1363 | -- For a null procedure that comes from source, a NULL statement is |
1364 | -- provided by the parser, which carries the source location of the | |
1365 | -- NULL keyword, and has Comes_From_Source set. For a null procedure | |
1366 | -- from expansion, create one now. | |
1367 | ||
1368 | if No (Null_Stmt) then | |
1369 | Null_Stmt := Make_Null_Statement (Loc); | |
1370 | end if; | |
1371 | ||
4d8f3296 ES |
1372 | Null_Body := |
1373 | Make_Subprogram_Body (Loc, | |
611d5e3c AC |
1374 | Specification => New_Copy_Tree (Spec), |
1375 | Declarations => New_List, | |
4d8f3296 ES |
1376 | Handled_Statement_Sequence => |
1377 | Make_Handled_Sequence_Of_Statements (Loc, | |
b912db16 | 1378 | Statements => New_List (Null_Stmt))); |
4d8f3296 ES |
1379 | |
1380 | -- Create new entities for body and formals | |
1381 | ||
1382 | Set_Defining_Unit_Name (Specification (Null_Body), | |
9d2a2071 AC |
1383 | Make_Defining_Identifier |
1384 | (Sloc (Defining_Entity (N)), | |
1385 | Chars (Defining_Entity (N)))); | |
4d8f3296 ES |
1386 | |
1387 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1388 | while Present (Form) loop | |
1389 | Set_Defining_Identifier (Form, | |
9d2a2071 AC |
1390 | Make_Defining_Identifier |
1391 | (Sloc (Defining_Identifier (Form)), | |
1392 | Chars (Defining_Identifier (Form)))); | |
4d8f3296 ES |
1393 | Next (Form); |
1394 | end loop; | |
1395 | ||
1396 | -- Determine whether the null procedure may be a completion of a generic | |
1397 | -- suprogram, in which case we use the new null body as the completion | |
1398 | -- and set minimal semantic information on the original declaration, | |
1399 | -- which is rewritten as a null statement. | |
1400 | ||
1401 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
1402 | ||
1403 | if Present (Prev) and then Is_Generic_Subprogram (Prev) then | |
1404 | Insert_Before (N, Null_Body); | |
1405 | Set_Ekind (Defining_Entity (N), Ekind (Prev)); | |
4d8f3296 ES |
1406 | |
1407 | Rewrite (N, Make_Null_Statement (Loc)); | |
1408 | Analyze_Generic_Subprogram_Body (Null_Body, Prev); | |
1409 | Is_Completion := True; | |
1410 | return; | |
1411 | ||
1412 | else | |
611d5e3c AC |
1413 | -- Resolve the types of the formals now, because the freeze point may |
1414 | -- appear in a different context, e.g. an instantiation. | |
4d8f3296 ES |
1415 | |
1416 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
1417 | while Present (Form) loop | |
1418 | if Nkind (Parameter_Type (Form)) /= N_Access_Definition then | |
1419 | Find_Type (Parameter_Type (Form)); | |
1420 | ||
611d5e3c AC |
1421 | elsif No (Access_To_Subprogram_Definition |
1422 | (Parameter_Type (Form))) | |
4d8f3296 ES |
1423 | then |
1424 | Find_Type (Subtype_Mark (Parameter_Type (Form))); | |
1425 | ||
611d5e3c AC |
1426 | -- The case of a null procedure with a formal that is an |
1427 | -- access-to-subprogram type, and that is used as an actual | |
1428 | -- in an instantiation is left to the enthusiastic reader. | |
4d8f3296 | 1429 | |
611d5e3c | 1430 | else |
4d8f3296 ES |
1431 | null; |
1432 | end if; | |
1433 | ||
1434 | Next (Form); | |
1435 | end loop; | |
1436 | end if; | |
1437 | ||
611d5e3c AC |
1438 | -- If there are previous overloadable entities with the same name, check |
1439 | -- whether any of them is completed by the null procedure. | |
4d8f3296 ES |
1440 | |
1441 | if Present (Prev) and then Is_Overloadable (Prev) then | |
1442 | Designator := Analyze_Subprogram_Specification (Spec); | |
1443 | Prev := Find_Corresponding_Spec (N); | |
1444 | end if; | |
1445 | ||
1446 | if No (Prev) or else not Comes_From_Source (Prev) then | |
1447 | Designator := Analyze_Subprogram_Specification (Spec); | |
1448 | Set_Has_Completion (Designator); | |
1449 | ||
1450 | -- Signal to caller that this is a procedure declaration | |
1451 | ||
1452 | Is_Completion := False; | |
1453 | ||
1454 | -- Null procedures are always inlined, but generic formal subprograms | |
1455 | -- which appear as such in the internal instance of formal packages, | |
1456 | -- need no completion and are not marked Inline. | |
1457 | ||
1458 | if Expander_Active | |
1459 | and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration | |
1460 | then | |
1461 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
1462 | Set_Body_To_Inline (N, Null_Body); | |
1463 | Set_Is_Inlined (Designator); | |
1464 | end if; | |
1465 | ||
1466 | else | |
2fe258bf AC |
1467 | -- The null procedure is a completion. We unconditionally rewrite |
1468 | -- this as a null body (even if expansion is not active), because | |
1469 | -- there are various error checks that are applied on this body | |
1470 | -- when it is analyzed (e.g. correct aspect placement). | |
4d8f3296 | 1471 | |
a98480dd AC |
1472 | if Has_Completion (Prev) then |
1473 | Error_Msg_Sloc := Sloc (Prev); | |
1474 | Error_Msg_NE ("duplicate body for & declared#", N, Prev); | |
1475 | end if; | |
1476 | ||
4d8f3296 | 1477 | Is_Completion := True; |
2fe258bf AC |
1478 | Rewrite (N, Null_Body); |
1479 | Analyze (N); | |
4d8f3296 ES |
1480 | end if; |
1481 | end Analyze_Null_Procedure; | |
1482 | ||
996ae0b0 RK |
1483 | ----------------------------- |
1484 | -- Analyze_Operator_Symbol -- | |
1485 | ----------------------------- | |
1486 | ||
82c80734 RD |
1487 | -- An operator symbol such as "+" or "and" may appear in context where the |
1488 | -- literal denotes an entity name, such as "+"(x, y) or in context when it | |
1489 | -- is just a string, as in (conjunction = "or"). In these cases the parser | |
1490 | -- generates this node, and the semantics does the disambiguation. Other | |
1491 | -- such case are actuals in an instantiation, the generic unit in an | |
1492 | -- instantiation, and pragma arguments. | |
996ae0b0 RK |
1493 | |
1494 | procedure Analyze_Operator_Symbol (N : Node_Id) is | |
1495 | Par : constant Node_Id := Parent (N); | |
1496 | ||
1497 | begin | |
1f0b1e48 | 1498 | if (Nkind (Par) = N_Function_Call and then N = Name (Par)) |
996ae0b0 | 1499 | or else Nkind (Par) = N_Function_Instantiation |
1f0b1e48 | 1500 | or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par)) |
996ae0b0 | 1501 | or else (Nkind (Par) = N_Pragma_Argument_Association |
1f0b1e48 | 1502 | and then not Is_Pragma_String_Literal (Par)) |
996ae0b0 | 1503 | or else Nkind (Par) = N_Subprogram_Renaming_Declaration |
800621e0 RD |
1504 | or else (Nkind (Par) = N_Attribute_Reference |
1505 | and then Attribute_Name (Par) /= Name_Value) | |
996ae0b0 RK |
1506 | then |
1507 | Find_Direct_Name (N); | |
1508 | ||
1509 | else | |
1510 | Change_Operator_Symbol_To_String_Literal (N); | |
1511 | Analyze (N); | |
1512 | end if; | |
1513 | end Analyze_Operator_Symbol; | |
1514 | ||
1515 | ----------------------------------- | |
1516 | -- Analyze_Parameter_Association -- | |
1517 | ----------------------------------- | |
1518 | ||
1519 | procedure Analyze_Parameter_Association (N : Node_Id) is | |
1520 | begin | |
1521 | Analyze (Explicit_Actual_Parameter (N)); | |
1522 | end Analyze_Parameter_Association; | |
1523 | ||
1524 | ---------------------------- | |
1525 | -- Analyze_Procedure_Call -- | |
1526 | ---------------------------- | |
1527 | ||
b0bf18ad AC |
1528 | -- WARNING: This routine manages Ghost regions. Return statements must be |
1529 | -- replaced by gotos which jump to the end of the routine and restore the | |
1530 | -- Ghost mode. | |
1531 | ||
996ae0b0 | 1532 | procedure Analyze_Procedure_Call (N : Node_Id) is |
996ae0b0 | 1533 | procedure Analyze_Call_And_Resolve; |
b0bf18ad AC |
1534 | -- Do Analyze and Resolve calls for procedure call. At the end, check |
1535 | -- for illegal order dependence. | |
1536 | -- ??? where is the check for illegal order dependencies? | |
996ae0b0 | 1537 | |
fbf5a39b AC |
1538 | ------------------------------ |
1539 | -- Analyze_Call_And_Resolve -- | |
1540 | ------------------------------ | |
1541 | ||
996ae0b0 RK |
1542 | procedure Analyze_Call_And_Resolve is |
1543 | begin | |
1544 | if Nkind (N) = N_Procedure_Call_Statement then | |
1545 | Analyze_Call (N); | |
1546 | Resolve (N, Standard_Void_Type); | |
1547 | else | |
1548 | Analyze (N); | |
1549 | end if; | |
1550 | end Analyze_Call_And_Resolve; | |
1551 | ||
241ebe89 HK |
1552 | -- Local variables |
1553 | ||
1554 | Actuals : constant List_Id := Parameter_Associations (N); | |
1555 | Loc : constant Source_Ptr := Sloc (N); | |
1556 | P : constant Node_Id := Name (N); | |
f9a8f910 HK |
1557 | |
1558 | Saved_GM : constant Ghost_Mode_Type := Ghost_Mode; | |
1559 | -- Save the Ghost mode to restore on exit | |
1560 | ||
1561 | Actual : Node_Id; | |
1562 | New_N : Node_Id; | |
241ebe89 | 1563 | |
996ae0b0 RK |
1564 | -- Start of processing for Analyze_Procedure_Call |
1565 | ||
1566 | begin | |
1567 | -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote | |
1568 | -- a procedure call or an entry call. The prefix may denote an access | |
1569 | -- to subprogram type, in which case an implicit dereference applies. | |
f3d57416 | 1570 | -- If the prefix is an indexed component (without implicit dereference) |
996ae0b0 RK |
1571 | -- then the construct denotes a call to a member of an entire family. |
1572 | -- If the prefix is a simple name, it may still denote a call to a | |
1573 | -- parameterless member of an entry family. Resolution of these various | |
1574 | -- interpretations is delicate. | |
1575 | ||
0fe797c5 SB |
1576 | -- Do not analyze machine code statements to avoid rejecting them in |
1577 | -- CodePeer mode. | |
996ae0b0 | 1578 | |
0fe797c5 SB |
1579 | if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then |
1580 | Set_Etype (P, Standard_Void_Type); | |
1581 | else | |
1582 | Analyze (P); | |
1583 | end if; | |
1584 | ||
1585 | -- If this is a call of the form Obj.Op, the call may have been analyzed | |
1586 | -- and possibly rewritten into a block, in which case we are done. | |
758c442c GD |
1587 | |
1588 | if Analyzed (N) then | |
1589 | return; | |
1590 | end if; | |
1591 | ||
7415029d AC |
1592 | -- If there is an error analyzing the name (which may have been |
1593 | -- rewritten if the original call was in prefix notation) then error | |
1594 | -- has been emitted already, mark node and return. | |
996ae0b0 | 1595 | |
21791d97 | 1596 | if Error_Posted (N) or else Etype (Name (N)) = Any_Type then |
996ae0b0 RK |
1597 | Set_Etype (N, Any_Type); |
1598 | return; | |
1599 | end if; | |
1600 | ||
1af4455a HK |
1601 | -- A procedure call is Ghost when its name denotes a Ghost procedure. |
1602 | -- Set the mode now to ensure that any nodes generated during analysis | |
1603 | -- and expansion are properly marked as Ghost. | |
8636f52f | 1604 | |
f9a8f910 | 1605 | Mark_And_Set_Ghost_Procedure_Call (N); |
8636f52f | 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)); | |
d65a80fd | 1629 | goto Leave; |
996ae0b0 RK |
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 | |
d65a80fd | 1639 | goto Leave; |
996ae0b0 RK |
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 | ||
0fe797c5 SB |
1654 | -- If the prefix is the simple name of an entry family, this is a |
1655 | -- parameterless call from within the task body itself. | |
996ae0b0 RK |
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 | 1710 | and then Ekind_In (Entity (Selector_Name (P)), E_Entry, |
d8f43ee6 HK |
1711 | E_Function, |
1712 | E_Procedure) | |
996ae0b0 | 1713 | then |
0929d66b AC |
1714 | -- When front-end inlining is enabled, as with SPARK_Mode, a call |
1715 | -- in prefix notation may still be missing its controlling argument, | |
1716 | -- so perform the transformation now. | |
1717 | ||
1718 | if SPARK_Mode = On and then In_Inlined_Body then | |
1719 | declare | |
1720 | Subp : constant Entity_Id := Entity (Selector_Name (P)); | |
1721 | Typ : constant Entity_Id := Etype (Prefix (P)); | |
1722 | ||
1723 | begin | |
1724 | if Is_Tagged_Type (Typ) | |
1725 | and then Present (First_Formal (Subp)) | |
1726 | and then Etype (First_Formal (Subp)) = Typ | |
1727 | and then Try_Object_Operation (P) | |
1728 | then | |
1729 | return; | |
1730 | ||
1731 | else | |
1732 | Analyze_Call_And_Resolve; | |
1733 | end if; | |
1734 | end; | |
1735 | ||
1736 | else | |
1737 | Analyze_Call_And_Resolve; | |
1738 | end if; | |
996ae0b0 RK |
1739 | |
1740 | elsif Nkind (P) = N_Selected_Component | |
1741 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family | |
1742 | and then Present (Actuals) | |
1743 | and then No (Next (First (Actuals))) | |
1744 | then | |
82c80734 RD |
1745 | -- Can be call to parameterless entry family. What appears to be the |
1746 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1747 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1748 | -- transformation. |
1749 | ||
1750 | New_N := | |
1751 | Make_Indexed_Component (Loc, | |
d8f43ee6 | 1752 | Prefix => New_Copy (P), |
996ae0b0 RK |
1753 | Expressions => Actuals); |
1754 | Set_Name (N, New_N); | |
1755 | Set_Etype (New_N, Standard_Void_Type); | |
1756 | Set_Parameter_Associations (N, No_List); | |
1757 | Analyze_Call_And_Resolve; | |
1758 | ||
1759 | -- For the case of a reference to an element of an entry family, P is | |
1760 | -- an indexed component whose prefix is a selected component (task and | |
1761 | -- entry family), and whose index is the entry family index. | |
1762 | ||
1763 | elsif Nkind (P) = N_Indexed_Component | |
1764 | and then Nkind (Prefix (P)) = N_Selected_Component | |
1765 | and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family | |
1766 | then | |
1767 | Analyze_Call_And_Resolve; | |
1768 | ||
1769 | -- If the prefix is the name of an entry family, it is a call from | |
1770 | -- within the task body itself. | |
1771 | ||
1772 | elsif Nkind (P) = N_Indexed_Component | |
1773 | and then Nkind (Prefix (P)) = N_Identifier | |
1774 | and then Ekind (Entity (Prefix (P))) = E_Entry_Family | |
1775 | then | |
1776 | New_N := | |
1777 | Make_Selected_Component (Loc, | |
1778 | Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), | |
1779 | Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); | |
1780 | Rewrite (Prefix (P), New_N); | |
1781 | Analyze (P); | |
1782 | Analyze_Call_And_Resolve; | |
1783 | ||
9f8d1e5c AC |
1784 | -- In Ada 2012. a qualified expression is a name, but it cannot be a |
1785 | -- procedure name, so the construct can only be a qualified expression. | |
1786 | ||
1787 | elsif Nkind (P) = N_Qualified_Expression | |
1788 | and then Ada_Version >= Ada_2012 | |
1789 | then | |
1790 | Rewrite (N, Make_Code_Statement (Loc, Expression => P)); | |
1791 | Analyze (N); | |
1792 | ||
e895b435 | 1793 | -- Anything else is an error |
996ae0b0 RK |
1794 | |
1795 | else | |
758c442c | 1796 | Error_Msg_N ("invalid procedure or entry call", N); |
996ae0b0 | 1797 | end if; |
241ebe89 | 1798 | |
d65a80fd | 1799 | <<Leave>> |
f9a8f910 | 1800 | Restore_Ghost_Mode (Saved_GM); |
996ae0b0 RK |
1801 | end Analyze_Procedure_Call; |
1802 | ||
b0186f71 AC |
1803 | ------------------------------ |
1804 | -- Analyze_Return_Statement -- | |
1805 | ------------------------------ | |
1806 | ||
1807 | procedure Analyze_Return_Statement (N : Node_Id) is | |
d65a80fd HK |
1808 | pragma Assert (Nkind_In (N, N_Extended_Return_Statement, |
1809 | N_Simple_Return_Statement)); | |
b0186f71 AC |
1810 | |
1811 | Returns_Object : constant Boolean := | |
1812 | Nkind (N) = N_Extended_Return_Statement | |
1813 | or else | |
8fde064e AC |
1814 | (Nkind (N) = N_Simple_Return_Statement |
1815 | and then Present (Expression (N))); | |
b0186f71 AC |
1816 | -- True if we're returning something; that is, "return <expression>;" |
1817 | -- or "return Result : T [:= ...]". False for "return;". Used for error | |
1818 | -- checking: If Returns_Object is True, N should apply to a function | |
1819 | -- body; otherwise N should apply to a procedure body, entry body, | |
1820 | -- accept statement, or extended return statement. | |
1821 | ||
1822 | function Find_What_It_Applies_To return Entity_Id; | |
1823 | -- Find the entity representing the innermost enclosing body, accept | |
1824 | -- statement, or extended return statement. If the result is a callable | |
1825 | -- construct or extended return statement, then this will be the value | |
1826 | -- of the Return_Applies_To attribute. Otherwise, the program is | |
1827 | -- illegal. See RM-6.5(4/2). | |
1828 | ||
1829 | ----------------------------- | |
1830 | -- Find_What_It_Applies_To -- | |
1831 | ----------------------------- | |
1832 | ||
1833 | function Find_What_It_Applies_To return Entity_Id is | |
1834 | Result : Entity_Id := Empty; | |
1835 | ||
1836 | begin | |
36b8f95f AC |
1837 | -- Loop outward through the Scope_Stack, skipping blocks, loops, |
1838 | -- and postconditions. | |
b0186f71 AC |
1839 | |
1840 | for J in reverse 0 .. Scope_Stack.Last loop | |
1841 | Result := Scope_Stack.Table (J).Entity; | |
11bc76df AC |
1842 | exit when not Ekind_In (Result, E_Block, E_Loop) |
1843 | and then Chars (Result) /= Name_uPostconditions; | |
b0186f71 AC |
1844 | end loop; |
1845 | ||
1846 | pragma Assert (Present (Result)); | |
1847 | return Result; | |
1848 | end Find_What_It_Applies_To; | |
1849 | ||
1850 | -- Local declarations | |
1851 | ||
1852 | Scope_Id : constant Entity_Id := Find_What_It_Applies_To; | |
1853 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
1854 | Loc : constant Source_Ptr := Sloc (N); | |
1855 | Stm_Entity : constant Entity_Id := | |
1856 | New_Internal_Entity | |
1857 | (E_Return_Statement, Current_Scope, Loc, 'R'); | |
1858 | ||
1859 | -- Start of processing for Analyze_Return_Statement | |
1860 | ||
1861 | begin | |
1862 | Set_Return_Statement_Entity (N, Stm_Entity); | |
1863 | ||
1864 | Set_Etype (Stm_Entity, Standard_Void_Type); | |
1865 | Set_Return_Applies_To (Stm_Entity, Scope_Id); | |
1866 | ||
1867 | -- Place Return entity on scope stack, to simplify enforcement of 6.5 | |
1868 | -- (4/2): an inner return statement will apply to this extended return. | |
1869 | ||
1870 | if Nkind (N) = N_Extended_Return_Statement then | |
1871 | Push_Scope (Stm_Entity); | |
1872 | end if; | |
1873 | ||
1874 | -- Check that pragma No_Return is obeyed. Don't complain about the | |
1875 | -- implicitly-generated return that is placed at the end. | |
1876 | ||
1877 | if No_Return (Scope_Id) and then Comes_From_Source (N) then | |
1878 | Error_Msg_N ("RETURN statement not allowed (No_Return)", N); | |
1879 | end if; | |
1880 | ||
1881 | -- Warn on any unassigned OUT parameters if in procedure | |
1882 | ||
1883 | if Ekind (Scope_Id) = E_Procedure then | |
1884 | Warn_On_Unassigned_Out_Parameter (N, Scope_Id); | |
1885 | end if; | |
1886 | ||
1887 | -- Check that functions return objects, and other things do not | |
1888 | ||
1889 | if Kind = E_Function or else Kind = E_Generic_Function then | |
1890 | if not Returns_Object then | |
1891 | Error_Msg_N ("missing expression in return from function", N); | |
1892 | end if; | |
1893 | ||
1894 | elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
1895 | if Returns_Object then | |
1896 | Error_Msg_N ("procedure cannot return value (use function)", N); | |
1897 | end if; | |
1898 | ||
1899 | elsif Kind = E_Entry or else Kind = E_Entry_Family then | |
1900 | if Returns_Object then | |
1901 | if Is_Protected_Type (Scope (Scope_Id)) then | |
1902 | Error_Msg_N ("entry body cannot return value", N); | |
1903 | else | |
1904 | Error_Msg_N ("accept statement cannot return value", N); | |
1905 | end if; | |
1906 | end if; | |
1907 | ||
1908 | elsif Kind = E_Return_Statement then | |
1909 | ||
1910 | -- We are nested within another return statement, which must be an | |
1911 | -- extended_return_statement. | |
1912 | ||
1913 | if Returns_Object then | |
d0dcb2b1 AC |
1914 | if Nkind (N) = N_Extended_Return_Statement then |
1915 | Error_Msg_N | |
cc96a1b8 | 1916 | ("extended return statement cannot be nested (use `RETURN;`)", |
d0dcb2b1 AC |
1917 | N); |
1918 | ||
1919 | -- Case of a simple return statement with a value inside extended | |
1920 | -- return statement. | |
1921 | ||
1922 | else | |
1923 | Error_Msg_N | |
3ccedacc AC |
1924 | ("return nested in extended return statement cannot return " |
1925 | & "value (use `RETURN;`)", N); | |
d0dcb2b1 | 1926 | end if; |
b0186f71 AC |
1927 | end if; |
1928 | ||
1929 | else | |
1930 | Error_Msg_N ("illegal context for return statement", N); | |
1931 | end if; | |
1932 | ||
1933 | if Ekind_In (Kind, E_Function, E_Generic_Function) then | |
1934 | Analyze_Function_Return (N); | |
1935 | ||
1936 | elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then | |
1937 | Set_Return_Present (Scope_Id); | |
1938 | end if; | |
1939 | ||
1940 | if Nkind (N) = N_Extended_Return_Statement then | |
1941 | End_Scope; | |
1942 | end if; | |
1943 | ||
1944 | Kill_Current_Values (Last_Assignment_Only => True); | |
1945 | Check_Unreachable_Code (N); | |
dec6faf1 AC |
1946 | |
1947 | Analyze_Dimension (N); | |
b0186f71 AC |
1948 | end Analyze_Return_Statement; |
1949 | ||
5d37ba92 ES |
1950 | ------------------------------------- |
1951 | -- Analyze_Simple_Return_Statement -- | |
1952 | ------------------------------------- | |
ec4867fa | 1953 | |
5d37ba92 | 1954 | procedure Analyze_Simple_Return_Statement (N : Node_Id) is |
996ae0b0 | 1955 | begin |
5d37ba92 ES |
1956 | if Present (Expression (N)) then |
1957 | Mark_Coextensions (N, Expression (N)); | |
996ae0b0 RK |
1958 | end if; |
1959 | ||
5d37ba92 ES |
1960 | Analyze_Return_Statement (N); |
1961 | end Analyze_Simple_Return_Statement; | |
996ae0b0 | 1962 | |
82c80734 RD |
1963 | ------------------------- |
1964 | -- Analyze_Return_Type -- | |
1965 | ------------------------- | |
1966 | ||
1967 | procedure Analyze_Return_Type (N : Node_Id) is | |
1968 | Designator : constant Entity_Id := Defining_Entity (N); | |
1969 | Typ : Entity_Id := Empty; | |
1970 | ||
1971 | begin | |
ec4867fa ES |
1972 | -- Normal case where result definition does not indicate an error |
1973 | ||
41251c60 JM |
1974 | if Result_Definition (N) /= Error then |
1975 | if Nkind (Result_Definition (N)) = N_Access_Definition then | |
ce5ba43a | 1976 | Check_SPARK_05_Restriction |
fe5d3068 | 1977 | ("access result is not allowed", Result_Definition (N)); |
daec8eeb | 1978 | |
b1c11e0e JM |
1979 | -- Ada 2005 (AI-254): Handle anonymous access to subprograms |
1980 | ||
1981 | declare | |
1982 | AD : constant Node_Id := | |
1983 | Access_To_Subprogram_Definition (Result_Definition (N)); | |
1984 | begin | |
1985 | if Present (AD) and then Protected_Present (AD) then | |
1986 | Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); | |
1987 | else | |
1988 | Typ := Access_Definition (N, Result_Definition (N)); | |
1989 | end if; | |
1990 | end; | |
1991 | ||
41251c60 JM |
1992 | Set_Parent (Typ, Result_Definition (N)); |
1993 | Set_Is_Local_Anonymous_Access (Typ); | |
1994 | Set_Etype (Designator, Typ); | |
1995 | ||
b66c3ff4 AC |
1996 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1997 | ||
1998 | Null_Exclusion_Static_Checks (N); | |
1999 | ||
41251c60 JM |
2000 | -- Subtype_Mark case |
2001 | ||
2002 | else | |
2003 | Find_Type (Result_Definition (N)); | |
2004 | Typ := Entity (Result_Definition (N)); | |
2005 | Set_Etype (Designator, Typ); | |
2006 | ||
2ba431e5 | 2007 | -- Unconstrained array as result is not allowed in SPARK |
daec8eeb | 2008 | |
8fde064e | 2009 | if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then |
ce5ba43a | 2010 | Check_SPARK_05_Restriction |
fe5d3068 | 2011 | ("returning an unconstrained array is not allowed", |
7394c8cc | 2012 | Result_Definition (N)); |
daec8eeb YM |
2013 | end if; |
2014 | ||
b66c3ff4 AC |
2015 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
2016 | ||
2017 | Null_Exclusion_Static_Checks (N); | |
2018 | ||
2019 | -- If a null exclusion is imposed on the result type, then create | |
2020 | -- a null-excluding itype (an access subtype) and use it as the | |
2021 | -- function's Etype. Note that the null exclusion checks are done | |
2022 | -- right before this, because they don't get applied to types that | |
2023 | -- do not come from source. | |
2024 | ||
8fde064e | 2025 | if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then |
b66c3ff4 AC |
2026 | Set_Etype (Designator, |
2027 | Create_Null_Excluding_Itype | |
ff7139c3 AC |
2028 | (T => Typ, |
2029 | Related_Nod => N, | |
2030 | Scope_Id => Scope (Current_Scope))); | |
2031 | ||
2032 | -- The new subtype must be elaborated before use because | |
2033 | -- it is visible outside of the function. However its base | |
2034 | -- type may not be frozen yet, so the reference that will | |
2035 | -- force elaboration must be attached to the freezing of | |
2036 | -- the base type. | |
2037 | ||
212863c0 AC |
2038 | -- If the return specification appears on a proper body, |
2039 | -- the subtype will have been created already on the spec. | |
2040 | ||
ff7139c3 | 2041 | if Is_Frozen (Typ) then |
212863c0 AC |
2042 | if Nkind (Parent (N)) = N_Subprogram_Body |
2043 | and then Nkind (Parent (Parent (N))) = N_Subunit | |
2044 | then | |
2045 | null; | |
2046 | else | |
2047 | Build_Itype_Reference (Etype (Designator), Parent (N)); | |
2048 | end if; | |
2049 | ||
ff7139c3 AC |
2050 | else |
2051 | Ensure_Freeze_Node (Typ); | |
2052 | ||
2053 | declare | |
212863c0 | 2054 | IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); |
ff7139c3 AC |
2055 | begin |
2056 | Set_Itype (IR, Etype (Designator)); | |
2057 | Append_Freeze_Actions (Typ, New_List (IR)); | |
2058 | end; | |
2059 | end if; | |
2060 | ||
b66c3ff4 AC |
2061 | else |
2062 | Set_Etype (Designator, Typ); | |
2063 | end if; | |
2064 | ||
41251c60 JM |
2065 | if Ekind (Typ) = E_Incomplete_Type |
2066 | or else (Is_Class_Wide_Type (Typ) | |
4b6f99f5 | 2067 | and then Ekind (Root_Type (Typ)) = E_Incomplete_Type) |
41251c60 | 2068 | then |
dd386db0 AC |
2069 | -- AI05-0151: Tagged incomplete types are allowed in all formal |
2070 | -- parts. Untagged incomplete types are not allowed in bodies. | |
b973629e AC |
2071 | -- As a consequence, limited views cannot appear in a basic |
2072 | -- declaration that is itself within a body, because there is | |
2073 | -- no point at which the non-limited view will become visible. | |
dd386db0 AC |
2074 | |
2075 | if Ada_Version >= Ada_2012 then | |
b973629e AC |
2076 | if From_Limited_With (Typ) and then In_Package_Body then |
2077 | Error_Msg_NE | |
2078 | ("invalid use of incomplete type&", | |
3f80a182 | 2079 | Result_Definition (N), Typ); |
b973629e | 2080 | |
1ebc2612 AC |
2081 | -- The return type of a subprogram body cannot be of a |
2082 | -- formal incomplete type. | |
2083 | ||
2084 | elsif Is_Generic_Type (Typ) | |
2085 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2086 | then | |
2087 | Error_Msg_N | |
2088 | ("return type cannot be a formal incomplete type", | |
2089 | Result_Definition (N)); | |
2090 | ||
2091 | elsif Is_Class_Wide_Type (Typ) | |
2092 | and then Is_Generic_Type (Root_Type (Typ)) | |
2093 | and then Nkind (Parent (N)) = N_Subprogram_Body | |
2094 | then | |
2095 | Error_Msg_N | |
2096 | ("return type cannot be a formal incomplete type", | |
2097 | Result_Definition (N)); | |
2098 | ||
b973629e | 2099 | elsif Is_Tagged_Type (Typ) then |
dd386db0 AC |
2100 | null; |
2101 | ||
ed09416f AC |
2102 | -- Use is legal in a thunk generated for an operation |
2103 | -- inherited from a progenitor. | |
2104 | ||
2105 | elsif Is_Thunk (Designator) | |
2106 | and then Present (Non_Limited_View (Typ)) | |
2107 | then | |
2108 | null; | |
2109 | ||
5b6f12c7 | 2110 | elsif Nkind (Parent (N)) = N_Subprogram_Body |
31d922e3 AC |
2111 | or else Nkind_In (Parent (Parent (N)), N_Accept_Statement, |
2112 | N_Entry_Body) | |
dd386db0 AC |
2113 | then |
2114 | Error_Msg_NE | |
2115 | ("invalid use of untagged incomplete type&", | |
2116 | Designator, Typ); | |
2117 | end if; | |
2118 | ||
63be2a5a | 2119 | -- The type must be completed in the current package. This |
31d922e3 | 2120 | -- is checked at the end of the package declaration when |
7b7a0c2b AC |
2121 | -- Taft-amendment types are identified. If the return type |
2122 | -- is class-wide, there is no required check, the type can | |
2123 | -- be a bona fide TAT. | |
63be2a5a AC |
2124 | |
2125 | if Ekind (Scope (Current_Scope)) = E_Package | |
c199ccf7 | 2126 | and then In_Private_Part (Scope (Current_Scope)) |
7b7a0c2b | 2127 | and then not Is_Class_Wide_Type (Typ) |
63be2a5a AC |
2128 | then |
2129 | Append_Elmt (Designator, Private_Dependents (Typ)); | |
2130 | end if; | |
2131 | ||
dd386db0 AC |
2132 | else |
2133 | Error_Msg_NE | |
2134 | ("invalid use of incomplete type&", Designator, Typ); | |
2135 | end if; | |
41251c60 | 2136 | end if; |
82c80734 RD |
2137 | end if; |
2138 | ||
ec4867fa ES |
2139 | -- Case where result definition does indicate an error |
2140 | ||
82c80734 RD |
2141 | else |
2142 | Set_Etype (Designator, Any_Type); | |
2143 | end if; | |
2144 | end Analyze_Return_Type; | |
2145 | ||
996ae0b0 RK |
2146 | ----------------------------- |
2147 | -- Analyze_Subprogram_Body -- | |
2148 | ----------------------------- | |
2149 | ||
b1b543d2 BD |
2150 | procedure Analyze_Subprogram_Body (N : Node_Id) is |
2151 | Loc : constant Source_Ptr := Sloc (N); | |
2152 | Body_Spec : constant Node_Id := Specification (N); | |
2153 | Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); | |
2154 | ||
2155 | begin | |
2156 | if Debug_Flag_C then | |
2157 | Write_Str ("==> subprogram body "); | |
2158 | Write_Name (Chars (Body_Id)); | |
2159 | Write_Str (" from "); | |
2160 | Write_Location (Loc); | |
2161 | Write_Eol; | |
2162 | Indent; | |
2163 | end if; | |
2164 | ||
2165 | Trace_Scope (N, Body_Id, " Analyze subprogram: "); | |
2166 | ||
2167 | -- The real work is split out into the helper, so it can do "return;" | |
2168 | -- without skipping the debug output: | |
2169 | ||
2170 | Analyze_Subprogram_Body_Helper (N); | |
2171 | ||
2172 | if Debug_Flag_C then | |
2173 | Outdent; | |
2174 | Write_Str ("<== subprogram body "); | |
2175 | Write_Name (Chars (Body_Id)); | |
2176 | Write_Str (" from "); | |
2177 | Write_Location (Loc); | |
2178 | Write_Eol; | |
2179 | end if; | |
2180 | end Analyze_Subprogram_Body; | |
2181 | ||
2182 | ------------------------------------ | |
2183 | -- Analyze_Subprogram_Body_Helper -- | |
2184 | ------------------------------------ | |
2185 | ||
996ae0b0 RK |
2186 | -- This procedure is called for regular subprogram bodies, generic bodies, |
2187 | -- and for subprogram stubs of both kinds. In the case of stubs, only the | |
2188 | -- specification matters, and is used to create a proper declaration for | |
2189 | -- the subprogram, or to perform conformance checks. | |
2190 | ||
b0bf18ad AC |
2191 | -- WARNING: This routine manages Ghost regions. Return statements must be |
2192 | -- replaced by gotos which jump to the end of the routine and restore the | |
2193 | -- Ghost mode. | |
2194 | ||
b1b543d2 | 2195 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is |
d030f3a4 AC |
2196 | Body_Spec : Node_Id := Specification (N); |
2197 | Body_Id : Entity_Id := Defining_Entity (Body_Spec); | |
2198 | Loc : constant Source_Ptr := Sloc (N); | |
2199 | Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); | |
2200 | ||
2201 | Conformant : Boolean; | |
2202 | Desig_View : Entity_Id := Empty; | |
2203 | Exch_Views : Elist_Id := No_Elist; | |
2204 | HSS : Node_Id; | |
2205 | Prot_Typ : Entity_Id := Empty; | |
2206 | Spec_Decl : Node_Id := Empty; | |
2207 | Spec_Id : Entity_Id; | |
21d27997 RD |
2208 | |
2209 | Last_Real_Spec_Entity : Entity_Id := Empty; | |
2210 | -- When we analyze a separate spec, the entity chain ends up containing | |
2211 | -- the formals, as well as any itypes generated during analysis of the | |
2212 | -- default expressions for parameters, or the arguments of associated | |
2213 | -- precondition/postcondition pragmas (which are analyzed in the context | |
2214 | -- of the spec since they have visibility on formals). | |
2215 | -- | |
2216 | -- These entities belong with the spec and not the body. However we do | |
2217 | -- the analysis of the body in the context of the spec (again to obtain | |
2218 | -- visibility to the formals), and all the entities generated during | |
2219 | -- this analysis end up also chained to the entity chain of the spec. | |
2220 | -- But they really belong to the body, and there is circuitry to move | |
2221 | -- them from the spec to the body. | |
2222 | -- | |
2223 | -- However, when we do this move, we don't want to move the real spec | |
2224 | -- entities (first para above) to the body. The Last_Real_Spec_Entity | |
2225 | -- variable points to the last real spec entity, so we only move those | |
2226 | -- chained beyond that point. It is initialized to Empty to deal with | |
2227 | -- the case where there is no separate spec. | |
996ae0b0 | 2228 | |
ac072cb2 AC |
2229 | function Body_Has_Contract return Boolean; |
2230 | -- Check whether unanalyzed body has an aspect or pragma that may | |
1399d355 | 2231 | -- generate a SPARK contract. |
ac072cb2 | 2232 | |
fd22e260 AC |
2233 | function Body_Has_SPARK_Mode_On return Boolean; |
2234 | -- Check whether SPARK_Mode On applies to the subprogram body, either | |
2235 | -- because it is specified directly on the body, or because it is | |
2236 | -- inherited from the enclosing subprogram or package. | |
2237 | ||
8d1fe980 AC |
2238 | procedure Build_Subprogram_Declaration; |
2239 | -- Create a matching subprogram declaration for subprogram body N | |
2240 | ||
ec4867fa | 2241 | procedure Check_Anonymous_Return; |
e50e1c5e | 2242 | -- Ada 2005: if a function returns an access type that denotes a task, |
ec4867fa ES |
2243 | -- or a type that contains tasks, we must create a master entity for |
2244 | -- the anonymous type, which typically will be used in an allocator | |
2245 | -- in the body of the function. | |
2246 | ||
e660dbf7 JM |
2247 | procedure Check_Inline_Pragma (Spec : in out Node_Id); |
2248 | -- Look ahead to recognize a pragma that may appear after the body. | |
2249 | -- If there is a previous spec, check that it appears in the same | |
2250 | -- declarative part. If the pragma is Inline_Always, perform inlining | |
2251 | -- unconditionally, otherwise only if Front_End_Inlining is requested. | |
2252 | -- If the body acts as a spec, and inlining is required, we create a | |
2253 | -- subprogram declaration for it, in order to attach the body to inline. | |
21d27997 RD |
2254 | -- If pragma does not appear after the body, check whether there is |
2255 | -- an inline pragma before any local declarations. | |
c37bb106 | 2256 | |
7665e4bd AC |
2257 | procedure Check_Missing_Return; |
2258 | -- Checks for a function with a no return statements, and also performs | |
8d606a78 RD |
2259 | -- the warning checks implemented by Check_Returns. In formal mode, also |
2260 | -- verify that a function ends with a RETURN and that a procedure does | |
2261 | -- not contain any RETURN. | |
7665e4bd | 2262 | |
d44202ba HK |
2263 | function Disambiguate_Spec return Entity_Id; |
2264 | -- When a primitive is declared between the private view and the full | |
2265 | -- view of a concurrent type which implements an interface, a special | |
2266 | -- mechanism is used to find the corresponding spec of the primitive | |
2267 | -- body. | |
2268 | ||
1e55d29a | 2269 | function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id; |
5dcab3ca | 2270 | -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains |
1e55d29a EB |
2271 | -- incomplete types coming from a limited context and replace their |
2272 | -- limited views with the non-limited ones. Return the list of changes | |
2273 | -- to be used to undo the transformation. | |
5dcab3ca | 2274 | |
448a1eb3 | 2275 | procedure Freeze_Expr_Types (Spec_Id : Entity_Id); |
804ec349 GD |
2276 | -- AI12-0103: N is the body associated with an expression function that |
2277 | -- is a completion, and Spec_Id is its defining entity. Freeze before N | |
2278 | -- all the types referenced by the expression of the function. | |
448a1eb3 | 2279 | |
d44202ba HK |
2280 | function Is_Private_Concurrent_Primitive |
2281 | (Subp_Id : Entity_Id) return Boolean; | |
2282 | -- Determine whether subprogram Subp_Id is a primitive of a concurrent | |
2283 | -- type that implements an interface and has a private view. | |
2284 | ||
1e55d29a EB |
2285 | procedure Restore_Limited_Views (Restore_List : Elist_Id); |
2286 | -- Undo the transformation done by Exchange_Limited_Views. | |
2287 | ||
76a69663 ES |
2288 | procedure Set_Trivial_Subprogram (N : Node_Id); |
2289 | -- Sets the Is_Trivial_Subprogram flag in both spec and body of the | |
2290 | -- subprogram whose body is being analyzed. N is the statement node | |
2291 | -- causing the flag to be set, if the following statement is a return | |
2292 | -- of an entity, we mark the entity as set in source to suppress any | |
2293 | -- warning on the stylized use of function stubs with a dummy return. | |
2294 | ||
758c442c GD |
2295 | procedure Verify_Overriding_Indicator; |
2296 | -- If there was a previous spec, the entity has been entered in the | |
2297 | -- current scope previously. If the body itself carries an overriding | |
2298 | -- indicator, check that it is consistent with the known status of the | |
2299 | -- entity. | |
2300 | ||
ac072cb2 AC |
2301 | ----------------------- |
2302 | -- Body_Has_Contract -- | |
2303 | ----------------------- | |
2304 | ||
2305 | function Body_Has_Contract return Boolean is | |
8d1fe980 AC |
2306 | Decls : constant List_Id := Declarations (N); |
2307 | Item : Node_Id; | |
ac072cb2 AC |
2308 | |
2309 | begin | |
33398e3c | 2310 | -- Check for aspects that may generate a contract |
ac072cb2 AC |
2311 | |
2312 | if Present (Aspect_Specifications (N)) then | |
8d1fe980 AC |
2313 | Item := First (Aspect_Specifications (N)); |
2314 | while Present (Item) loop | |
33398e3c | 2315 | if Is_Subprogram_Contract_Annotation (Item) then |
ac072cb2 AC |
2316 | return True; |
2317 | end if; | |
2318 | ||
8d1fe980 | 2319 | Next (Item); |
ac072cb2 AC |
2320 | end loop; |
2321 | end if; | |
2322 | ||
1399d355 | 2323 | -- Check for pragmas that may generate a contract |
ac072cb2 AC |
2324 | |
2325 | if Present (Decls) then | |
8d1fe980 AC |
2326 | Item := First (Decls); |
2327 | while Present (Item) loop | |
2328 | if Nkind (Item) = N_Pragma | |
33398e3c | 2329 | and then Is_Subprogram_Contract_Annotation (Item) |
8d1fe980 AC |
2330 | then |
2331 | return True; | |
ac072cb2 AC |
2332 | end if; |
2333 | ||
8d1fe980 | 2334 | Next (Item); |
ac072cb2 AC |
2335 | end loop; |
2336 | end if; | |
2337 | ||
2338 | return False; | |
2339 | end Body_Has_Contract; | |
2340 | ||
fd22e260 AC |
2341 | ---------------------------- |
2342 | -- Body_Has_SPARK_Mode_On -- | |
2343 | ---------------------------- | |
2344 | ||
2345 | function Body_Has_SPARK_Mode_On return Boolean is | |
2346 | Decls : constant List_Id := Declarations (N); | |
2347 | Item : Node_Id; | |
2348 | ||
2349 | begin | |
2350 | -- Check for SPARK_Mode aspect | |
2351 | ||
2352 | if Present (Aspect_Specifications (N)) then | |
2353 | Item := First (Aspect_Specifications (N)); | |
2354 | while Present (Item) loop | |
2355 | if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then | |
933aa0ac | 2356 | return Get_SPARK_Mode_From_Annotation (Item) = On; |
fd22e260 AC |
2357 | end if; |
2358 | ||
2359 | Next (Item); | |
2360 | end loop; | |
2361 | end if; | |
2362 | ||
2363 | -- Check for SPARK_Mode pragma | |
2364 | ||
2365 | if Present (Decls) then | |
2366 | Item := First (Decls); | |
2367 | while Present (Item) loop | |
933aa0ac AC |
2368 | |
2369 | -- Pragmas that apply to a subprogram body are usually grouped | |
2370 | -- together. Look for a potential pragma SPARK_Mode among them. | |
2371 | ||
2372 | if Nkind (Item) = N_Pragma then | |
2373 | if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then | |
2374 | return Get_SPARK_Mode_From_Annotation (Item) = On; | |
2375 | end if; | |
2376 | ||
2377 | -- Otherwise the first non-pragma declarative item terminates | |
2378 | -- the region where pragma SPARK_Mode may appear. | |
2379 | ||
2380 | else | |
2381 | exit; | |
fd22e260 AC |
2382 | end if; |
2383 | ||
2384 | Next (Item); | |
2385 | end loop; | |
2386 | end if; | |
2387 | ||
933aa0ac AC |
2388 | -- Otherwise, the applicable SPARK_Mode is inherited from the |
2389 | -- enclosing subprogram or package. | |
fd22e260 AC |
2390 | |
2391 | return SPARK_Mode = On; | |
2392 | end Body_Has_SPARK_Mode_On; | |
2393 | ||
8d1fe980 AC |
2394 | ---------------------------------- |
2395 | -- Build_Subprogram_Declaration -- | |
2396 | ---------------------------------- | |
2397 | ||
2398 | procedure Build_Subprogram_Declaration is | |
3f8c04e7 AC |
2399 | procedure Move_Pragmas (From : Node_Id; To : Node_Id); |
2400 | -- Relocate certain categorization pragmas from the declarative list | |
2401 | -- of subprogram body From and insert them after node To. The pragmas | |
2402 | -- in question are: | |
2403 | -- Ghost | |
3f8c04e7 | 2404 | -- Volatile_Function |
0a3ec628 AC |
2405 | -- Also copy pragma SPARK_Mode if present in the declarative list |
2406 | -- of subprogram body From and insert it after node To. This pragma | |
2407 | -- should not be moved, as it applies to the body too. | |
3f8c04e7 AC |
2408 | |
2409 | ------------------ | |
2410 | -- Move_Pragmas -- | |
2411 | ------------------ | |
2412 | ||
2413 | procedure Move_Pragmas (From : Node_Id; To : Node_Id) is | |
2414 | Decl : Node_Id; | |
2415 | Next_Decl : Node_Id; | |
2416 | ||
2417 | begin | |
2418 | pragma Assert (Nkind (From) = N_Subprogram_Body); | |
2419 | ||
7f54dc83 | 2420 | -- The destination node must be part of a list, as the pragmas are |
3f8c04e7 AC |
2421 | -- inserted after it. |
2422 | ||
2423 | pragma Assert (Is_List_Member (To)); | |
2424 | ||
2425 | -- Inspect the declarations of the subprogram body looking for | |
2426 | -- specific pragmas. | |
2427 | ||
2428 | Decl := First (Declarations (N)); | |
2429 | while Present (Decl) loop | |
2430 | Next_Decl := Next (Decl); | |
2431 | ||
0a3ec628 AC |
2432 | if Nkind (Decl) = N_Pragma then |
2433 | if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then | |
2434 | Insert_After (To, New_Copy_Tree (Decl)); | |
2435 | ||
2436 | elsif Nam_In (Pragma_Name_Unmapped (Decl), | |
2437 | Name_Ghost, | |
2438 | Name_Volatile_Function) | |
2439 | then | |
2440 | Remove (Decl); | |
2441 | Insert_After (To, Decl); | |
2442 | end if; | |
3f8c04e7 AC |
2443 | end if; |
2444 | ||
2445 | Decl := Next_Decl; | |
2446 | end loop; | |
2447 | end Move_Pragmas; | |
2448 | ||
2449 | -- Local variables | |
2450 | ||
8d1fe980 AC |
2451 | Decl : Node_Id; |
2452 | Subp_Decl : Node_Id; | |
2453 | ||
3f8c04e7 AC |
2454 | -- Start of processing for Build_Subprogram_Declaration |
2455 | ||
8d1fe980 AC |
2456 | begin |
2457 | -- Create a matching subprogram spec using the profile of the body. | |
2458 | -- The structure of the tree is identical, but has new entities for | |
2459 | -- the defining unit name and formal parameters. | |
2460 | ||
2461 | Subp_Decl := | |
2462 | Make_Subprogram_Declaration (Loc, | |
2463 | Specification => Copy_Subprogram_Spec (Body_Spec)); | |
877a5a12 | 2464 | Set_Comes_From_Source (Subp_Decl, True); |
8d1fe980 | 2465 | |
3f8c04e7 AC |
2466 | -- Relocate the aspects and relevant pragmas from the subprogram body |
2467 | -- to the generated spec because it acts as the initial declaration. | |
8d1fe980 | 2468 | |
3f8c04e7 | 2469 | Insert_Before (N, Subp_Decl); |
8d1fe980 | 2470 | Move_Aspects (N, To => Subp_Decl); |
3f8c04e7 | 2471 | Move_Pragmas (N, To => Subp_Decl); |
8d1fe980 | 2472 | |
0a3ec628 AC |
2473 | -- Ensure that the generated corresponding spec and original body |
2474 | -- share the same SPARK_Mode pragma or aspect. As a result, both have | |
2475 | -- the same SPARK_Mode attributes, and the global SPARK_Mode value is | |
2476 | -- correctly set for local subprograms. | |
2477 | ||
2478 | Copy_SPARK_Mode_Aspect (Subp_Decl, To => N); | |
2479 | ||
3f8c04e7 AC |
2480 | Analyze (Subp_Decl); |
2481 | ||
888be6b1 AC |
2482 | -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to |
2483 | -- the body since the expander may generate calls using that entity. | |
2484 | -- Required to ensure that Expand_Call rewrites calls to this | |
2485 | -- function by calls to the built procedure. | |
2486 | ||
2487 | if Modify_Tree_For_C | |
2488 | and then Nkind (Body_Spec) = N_Function_Specification | |
2489 | and then | |
2490 | Rewritten_For_C (Defining_Entity (Specification (Subp_Decl))) | |
2491 | then | |
2492 | Set_Rewritten_For_C (Defining_Entity (Body_Spec)); | |
2493 | Set_Corresponding_Procedure (Defining_Entity (Body_Spec), | |
2494 | Corresponding_Procedure | |
2495 | (Defining_Entity (Specification (Subp_Decl)))); | |
aeb98f1d JM |
2496 | end if; |
2497 | ||
3f8c04e7 AC |
2498 | -- Analyze any relocated source pragmas or pragmas created for aspect |
2499 | -- specifications. | |
8d1fe980 AC |
2500 | |
2501 | Decl := Next (Subp_Decl); | |
2502 | while Present (Decl) loop | |
2503 | ||
2504 | -- Stop the search for pragmas once the body has been reached as | |
2505 | -- this terminates the region where pragmas may appear. | |
2506 | ||
2507 | if Decl = N then | |
2508 | exit; | |
2509 | ||
2510 | elsif Nkind (Decl) = N_Pragma then | |
2511 | Analyze (Decl); | |
2512 | end if; | |
2513 | ||
2514 | Next (Decl); | |
2515 | end loop; | |
2516 | ||
2517 | Spec_Id := Defining_Entity (Subp_Decl); | |
2518 | Set_Corresponding_Spec (N, Spec_Id); | |
2519 | ||
2520 | -- Mark the generated spec as a source construct to ensure that all | |
2521 | -- calls to it are properly registered in ALI files for GNATprove. | |
2522 | ||
2523 | Set_Comes_From_Source (Spec_Id, True); | |
2524 | ||
8d1fe980 AC |
2525 | -- Ensure that the specs of the subprogram declaration and its body |
2526 | -- are identical, otherwise they will appear non-conformant due to | |
2527 | -- rewritings in the default values of formal parameters. | |
2528 | ||
2529 | Body_Spec := Copy_Subprogram_Spec (Body_Spec); | |
2530 | Set_Specification (N, Body_Spec); | |
2531 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); | |
2532 | end Build_Subprogram_Declaration; | |
2533 | ||
ec4867fa ES |
2534 | ---------------------------- |
2535 | -- Check_Anonymous_Return -- | |
2536 | ---------------------------- | |
2537 | ||
2538 | procedure Check_Anonymous_Return is | |
2539 | Decl : Node_Id; | |
a523b302 | 2540 | Par : Node_Id; |
ec4867fa ES |
2541 | Scop : Entity_Id; |
2542 | ||
2543 | begin | |
2544 | if Present (Spec_Id) then | |
2545 | Scop := Spec_Id; | |
2546 | else | |
2547 | Scop := Body_Id; | |
2548 | end if; | |
2549 | ||
2550 | if Ekind (Scop) = E_Function | |
2551 | and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type | |
a523b302 | 2552 | and then not Is_Thunk (Scop) |
4b963531 AC |
2553 | |
2554 | -- Skip internally built functions which handle the case of | |
2555 | -- a null access (see Expand_Interface_Conversion) | |
2556 | ||
2557 | and then not (Is_Interface (Designated_Type (Etype (Scop))) | |
4b6f99f5 | 2558 | and then not Comes_From_Source (Parent (Scop))) |
4b963531 | 2559 | |
a523b302 JM |
2560 | and then (Has_Task (Designated_Type (Etype (Scop))) |
2561 | or else | |
4b6f99f5 RD |
2562 | (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) |
2563 | and then | |
2564 | Is_Limited_Record (Designated_Type (Etype (Scop))))) | |
ec4867fa | 2565 | and then Expander_Active |
b20de9b9 | 2566 | |
8fde064e | 2567 | -- Avoid cases with no tasking support |
b20de9b9 AC |
2568 | |
2569 | and then RTE_Available (RE_Current_Master) | |
2570 | and then not Restriction_Active (No_Task_Hierarchy) | |
ec4867fa ES |
2571 | then |
2572 | Decl := | |
2573 | Make_Object_Declaration (Loc, | |
2574 | Defining_Identifier => | |
2575 | Make_Defining_Identifier (Loc, Name_uMaster), | |
2576 | Constant_Present => True, | |
2577 | Object_Definition => | |
e4494292 | 2578 | New_Occurrence_Of (RTE (RE_Master_Id), Loc), |
ec4867fa ES |
2579 | Expression => |
2580 | Make_Explicit_Dereference (Loc, | |
e4494292 | 2581 | New_Occurrence_Of (RTE (RE_Current_Master), Loc))); |
ec4867fa ES |
2582 | |
2583 | if Present (Declarations (N)) then | |
2584 | Prepend (Decl, Declarations (N)); | |
2585 | else | |
2586 | Set_Declarations (N, New_List (Decl)); | |
2587 | end if; | |
2588 | ||
2589 | Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); | |
2590 | Set_Has_Master_Entity (Scop); | |
a523b302 JM |
2591 | |
2592 | -- Now mark the containing scope as a task master | |
2593 | ||
2594 | Par := N; | |
2595 | while Nkind (Par) /= N_Compilation_Unit loop | |
2596 | Par := Parent (Par); | |
2597 | pragma Assert (Present (Par)); | |
2598 | ||
2599 | -- If we fall off the top, we are at the outer level, and | |
2600 | -- the environment task is our effective master, so nothing | |
2601 | -- to mark. | |
2602 | ||
2603 | if Nkind_In | |
2604 | (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) | |
2605 | then | |
2606 | Set_Is_Task_Master (Par, True); | |
2607 | exit; | |
2608 | end if; | |
2609 | end loop; | |
ec4867fa ES |
2610 | end if; |
2611 | end Check_Anonymous_Return; | |
2612 | ||
e660dbf7 JM |
2613 | ------------------------- |
2614 | -- Check_Inline_Pragma -- | |
2615 | ------------------------- | |
758c442c | 2616 | |
e660dbf7 JM |
2617 | procedure Check_Inline_Pragma (Spec : in out Node_Id) is |
2618 | Prag : Node_Id; | |
2619 | Plist : List_Id; | |
0fb2ea01 | 2620 | |
21d27997 | 2621 | function Is_Inline_Pragma (N : Node_Id) return Boolean; |
30783513 | 2622 | -- True when N is a pragma Inline or Inline_Always that applies |
33931112 | 2623 | -- to this subprogram. |
21d27997 RD |
2624 | |
2625 | ----------------------- | |
2626 | -- Is_Inline_Pragma -- | |
2627 | ----------------------- | |
2628 | ||
2629 | function Is_Inline_Pragma (N : Node_Id) return Boolean is | |
2630 | begin | |
b269f477 | 2631 | if Nkind (N) = N_Pragma |
21d27997 | 2632 | and then |
6e759c2a BD |
2633 | (Pragma_Name_Unmapped (N) = Name_Inline_Always |
2634 | or else (Pragma_Name_Unmapped (N) = Name_Inline | |
87feba05 AC |
2635 | and then |
2636 | (Front_End_Inlining or else Optimization_Level > 0))) | |
274c2cda | 2637 | and then Present (Pragma_Argument_Associations (N)) |
b269f477 BD |
2638 | then |
2639 | declare | |
2640 | Pragma_Arg : Node_Id := | |
2641 | Expression (First (Pragma_Argument_Associations (N))); | |
2642 | begin | |
2643 | if Nkind (Pragma_Arg) = N_Selected_Component then | |
2644 | Pragma_Arg := Selector_Name (Pragma_Arg); | |
2645 | end if; | |
2646 | ||
2647 | return Chars (Pragma_Arg) = Chars (Body_Id); | |
2648 | end; | |
2649 | ||
2650 | else | |
2651 | return False; | |
2652 | end if; | |
21d27997 RD |
2653 | end Is_Inline_Pragma; |
2654 | ||
2655 | -- Start of processing for Check_Inline_Pragma | |
2656 | ||
c37bb106 | 2657 | begin |
e660dbf7 JM |
2658 | if not Expander_Active then |
2659 | return; | |
2660 | end if; | |
2661 | ||
2662 | if Is_List_Member (N) | |
2663 | and then Present (Next (N)) | |
21d27997 | 2664 | and then Is_Inline_Pragma (Next (N)) |
c37bb106 AC |
2665 | then |
2666 | Prag := Next (N); | |
2667 | ||
21d27997 RD |
2668 | elsif Nkind (N) /= N_Subprogram_Body_Stub |
2669 | and then Present (Declarations (N)) | |
2670 | and then Is_Inline_Pragma (First (Declarations (N))) | |
2671 | then | |
2672 | Prag := First (Declarations (N)); | |
2673 | ||
e660dbf7 JM |
2674 | else |
2675 | Prag := Empty; | |
c37bb106 | 2676 | end if; |
e660dbf7 JM |
2677 | |
2678 | if Present (Prag) then | |
2679 | if Present (Spec_Id) then | |
b269f477 BD |
2680 | if Is_List_Member (N) |
2681 | and then Is_List_Member (Unit_Declaration_Node (Spec_Id)) | |
2682 | and then In_Same_List (N, Unit_Declaration_Node (Spec_Id)) | |
2683 | then | |
e660dbf7 JM |
2684 | Analyze (Prag); |
2685 | end if; | |
2686 | ||
2687 | else | |
274c2cda AC |
2688 | -- Create a subprogram declaration, to make treatment uniform. |
2689 | -- Make the sloc of the subprogram name that of the entity in | |
2690 | -- the body, so that style checks find identical strings. | |
e660dbf7 JM |
2691 | |
2692 | declare | |
2693 | Subp : constant Entity_Id := | |
274c2cda AC |
2694 | Make_Defining_Identifier |
2695 | (Sloc (Body_Id), Chars (Body_Id)); | |
e660dbf7 | 2696 | Decl : constant Node_Id := |
30196a76 RD |
2697 | Make_Subprogram_Declaration (Loc, |
2698 | Specification => | |
2699 | New_Copy_Tree (Specification (N))); | |
2700 | ||
e660dbf7 JM |
2701 | begin |
2702 | Set_Defining_Unit_Name (Specification (Decl), Subp); | |
2703 | ||
4afcf3a5 AC |
2704 | -- To ensure proper coverage when body is inlined, indicate |
2705 | -- whether the subprogram comes from source. | |
2706 | ||
2707 | Set_Comes_From_Source (Subp, Comes_From_Source (N)); | |
2708 | ||
e660dbf7 | 2709 | if Present (First_Formal (Body_Id)) then |
21d27997 | 2710 | Plist := Copy_Parameter_List (Body_Id); |
e660dbf7 JM |
2711 | Set_Parameter_Specifications |
2712 | (Specification (Decl), Plist); | |
2713 | end if; | |
2714 | ||
a5fa1522 JM |
2715 | -- Move aspects to the new spec |
2716 | ||
2717 | if Has_Aspects (N) then | |
2718 | Move_Aspects (N, To => Decl); | |
2719 | end if; | |
2720 | ||
e660dbf7 JM |
2721 | Insert_Before (N, Decl); |
2722 | Analyze (Decl); | |
2723 | Analyze (Prag); | |
2724 | Set_Has_Pragma_Inline (Subp); | |
2725 | ||
6e759c2a | 2726 | if Pragma_Name (Prag) = Name_Inline_Always then |
e660dbf7 | 2727 | Set_Is_Inlined (Subp); |
21d27997 | 2728 | Set_Has_Pragma_Inline_Always (Subp); |
e660dbf7 JM |
2729 | end if; |
2730 | ||
158d55fa AC |
2731 | -- Prior to copying the subprogram body to create a template |
2732 | -- for it for subsequent inlining, remove the pragma from | |
2733 | -- the current body so that the copy that will produce the | |
2734 | -- new body will start from a completely unanalyzed tree. | |
2735 | ||
2736 | if Nkind (Parent (Prag)) = N_Subprogram_Body then | |
2737 | Rewrite (Prag, Make_Null_Statement (Sloc (Prag))); | |
2738 | end if; | |
2739 | ||
e660dbf7 JM |
2740 | Spec := Subp; |
2741 | end; | |
2742 | end if; | |
2743 | end if; | |
2744 | end Check_Inline_Pragma; | |
2745 | ||
7665e4bd AC |
2746 | -------------------------- |
2747 | -- Check_Missing_Return -- | |
2748 | -------------------------- | |
2749 | ||
2750 | procedure Check_Missing_Return is | |
2751 | Id : Entity_Id; | |
2752 | Missing_Ret : Boolean; | |
2753 | ||
2754 | begin | |
2755 | if Nkind (Body_Spec) = N_Function_Specification then | |
2756 | if Present (Spec_Id) then | |
2757 | Id := Spec_Id; | |
2758 | else | |
2759 | Id := Body_Id; | |
2760 | end if; | |
2761 | ||
fe5d3068 | 2762 | if Return_Present (Id) then |
7665e4bd AC |
2763 | Check_Returns (HSS, 'F', Missing_Ret); |
2764 | ||
2765 | if Missing_Ret then | |
2766 | Set_Has_Missing_Return (Id); | |
2767 | end if; | |
2768 | ||
241fac51 ES |
2769 | -- Within a premature instantiation of a package with no body, we |
2770 | -- build completions of the functions therein, with a Raise | |
2771 | -- statement. No point in complaining about a missing return in | |
2772 | -- this case. | |
2773 | ||
2774 | elsif Ekind (Id) = E_Function | |
2775 | and then In_Instance | |
2776 | and then Present (Statements (HSS)) | |
2777 | and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error | |
2778 | then | |
2779 | null; | |
2780 | ||
2aca76d6 AC |
2781 | elsif Is_Generic_Subprogram (Id) |
2782 | or else not Is_Machine_Code_Subprogram (Id) | |
7665e4bd AC |
2783 | then |
2784 | Error_Msg_N ("missing RETURN statement in function body", N); | |
2785 | end if; | |
2786 | ||
fe5d3068 | 2787 | -- If procedure with No_Return, check returns |
607d0635 | 2788 | |
fe5d3068 YM |
2789 | elsif Nkind (Body_Spec) = N_Procedure_Specification |
2790 | and then Present (Spec_Id) | |
2791 | and then No_Return (Spec_Id) | |
607d0635 | 2792 | then |
fe5d3068 YM |
2793 | Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); |
2794 | end if; | |
2795 | ||
ad05f2e9 | 2796 | -- Special checks in SPARK mode |
fe5d3068 YM |
2797 | |
2798 | if Nkind (Body_Spec) = N_Function_Specification then | |
7394c8cc | 2799 | |
ad05f2e9 | 2800 | -- In SPARK mode, last statement of a function should be a return |
fe5d3068 YM |
2801 | |
2802 | declare | |
2803 | Stat : constant Node_Id := Last_Source_Statement (HSS); | |
2804 | begin | |
2805 | if Present (Stat) | |
7394c8cc AC |
2806 | and then not Nkind_In (Stat, N_Simple_Return_Statement, |
2807 | N_Extended_Return_Statement) | |
fe5d3068 | 2808 | then |
ce5ba43a | 2809 | Check_SPARK_05_Restriction |
fe5d3068 YM |
2810 | ("last statement in function should be RETURN", Stat); |
2811 | end if; | |
2812 | end; | |
2813 | ||
ad05f2e9 | 2814 | -- In SPARK mode, verify that a procedure has no return |
fe5d3068 YM |
2815 | |
2816 | elsif Nkind (Body_Spec) = N_Procedure_Specification then | |
607d0635 AC |
2817 | if Present (Spec_Id) then |
2818 | Id := Spec_Id; | |
2819 | else | |
2820 | Id := Body_Id; | |
2821 | end if; | |
2822 | ||
8d606a78 RD |
2823 | -- Would be nice to point to return statement here, can we |
2824 | -- borrow the Check_Returns procedure here ??? | |
2825 | ||
607d0635 | 2826 | if Return_Present (Id) then |
ce5ba43a | 2827 | Check_SPARK_05_Restriction |
fe5d3068 | 2828 | ("procedure should not have RETURN", N); |
607d0635 | 2829 | end if; |
7665e4bd AC |
2830 | end if; |
2831 | end Check_Missing_Return; | |
2832 | ||
d44202ba HK |
2833 | ----------------------- |
2834 | -- Disambiguate_Spec -- | |
2835 | ----------------------- | |
2836 | ||
2837 | function Disambiguate_Spec return Entity_Id is | |
2838 | Priv_Spec : Entity_Id; | |
2839 | Spec_N : Entity_Id; | |
2840 | ||
2841 | procedure Replace_Types (To_Corresponding : Boolean); | |
2842 | -- Depending on the flag, replace the type of formal parameters of | |
2843 | -- Body_Id if it is a concurrent type implementing interfaces with | |
2844 | -- the corresponding record type or the other way around. | |
2845 | ||
2846 | procedure Replace_Types (To_Corresponding : Boolean) is | |
2847 | Formal : Entity_Id; | |
2848 | Formal_Typ : Entity_Id; | |
2849 | ||
2850 | begin | |
2851 | Formal := First_Formal (Body_Id); | |
2852 | while Present (Formal) loop | |
2853 | Formal_Typ := Etype (Formal); | |
2854 | ||
df3e68b1 HK |
2855 | if Is_Class_Wide_Type (Formal_Typ) then |
2856 | Formal_Typ := Root_Type (Formal_Typ); | |
2857 | end if; | |
2858 | ||
d44202ba HK |
2859 | -- From concurrent type to corresponding record |
2860 | ||
2861 | if To_Corresponding then | |
2862 | if Is_Concurrent_Type (Formal_Typ) | |
2863 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
4b6f99f5 RD |
2864 | and then |
2865 | Present (Interfaces | |
2866 | (Corresponding_Record_Type (Formal_Typ))) | |
d44202ba HK |
2867 | then |
2868 | Set_Etype (Formal, | |
2869 | Corresponding_Record_Type (Formal_Typ)); | |
2870 | end if; | |
2871 | ||
2872 | -- From corresponding record to concurrent type | |
2873 | ||
2874 | else | |
2875 | if Is_Concurrent_Record_Type (Formal_Typ) | |
2876 | and then Present (Interfaces (Formal_Typ)) | |
2877 | then | |
2878 | Set_Etype (Formal, | |
2879 | Corresponding_Concurrent_Type (Formal_Typ)); | |
2880 | end if; | |
2881 | end if; | |
2882 | ||
2883 | Next_Formal (Formal); | |
2884 | end loop; | |
2885 | end Replace_Types; | |
2886 | ||
2887 | -- Start of processing for Disambiguate_Spec | |
2888 | ||
2889 | begin | |
2890 | -- Try to retrieve the specification of the body as is. All error | |
2891 | -- messages are suppressed because the body may not have a spec in | |
2892 | -- its current state. | |
2893 | ||
2894 | Spec_N := Find_Corresponding_Spec (N, False); | |
2895 | ||
2896 | -- It is possible that this is the body of a primitive declared | |
2897 | -- between a private and a full view of a concurrent type. The | |
2898 | -- controlling parameter of the spec carries the concurrent type, | |
2899 | -- not the corresponding record type as transformed by Analyze_ | |
2900 | -- Subprogram_Specification. In such cases, we undo the change | |
2901 | -- made by the analysis of the specification and try to find the | |
2902 | -- spec again. | |
766d7add | 2903 | |
8198b93d HK |
2904 | -- Note that wrappers already have their corresponding specs and |
2905 | -- bodies set during their creation, so if the candidate spec is | |
16b05213 | 2906 | -- a wrapper, then we definitely need to swap all types to their |
8198b93d | 2907 | -- original concurrent status. |
d44202ba | 2908 | |
8198b93d HK |
2909 | if No (Spec_N) |
2910 | or else Is_Primitive_Wrapper (Spec_N) | |
2911 | then | |
d44202ba HK |
2912 | -- Restore all references of corresponding record types to the |
2913 | -- original concurrent types. | |
2914 | ||
2915 | Replace_Types (To_Corresponding => False); | |
2916 | Priv_Spec := Find_Corresponding_Spec (N, False); | |
2917 | ||
2918 | -- The current body truly belongs to a primitive declared between | |
2919 | -- a private and a full view. We leave the modified body as is, | |
2920 | -- and return the true spec. | |
2921 | ||
2922 | if Present (Priv_Spec) | |
2923 | and then Is_Private_Primitive (Priv_Spec) | |
2924 | then | |
2925 | return Priv_Spec; | |
2926 | end if; | |
2927 | ||
2928 | -- In case that this is some sort of error, restore the original | |
2929 | -- state of the body. | |
2930 | ||
2931 | Replace_Types (To_Corresponding => True); | |
2932 | end if; | |
2933 | ||
2934 | return Spec_N; | |
2935 | end Disambiguate_Spec; | |
2936 | ||
5dcab3ca AC |
2937 | ---------------------------- |
2938 | -- Exchange_Limited_Views -- | |
2939 | ---------------------------- | |
2940 | ||
1e55d29a EB |
2941 | function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is |
2942 | Result : Elist_Id := No_Elist; | |
2943 | ||
5dcab3ca AC |
2944 | procedure Detect_And_Exchange (Id : Entity_Id); |
2945 | -- Determine whether Id's type denotes an incomplete type associated | |
2946 | -- with a limited with clause and exchange the limited view with the | |
72d5c70b AC |
2947 | -- non-limited one when available. Note that the non-limited view |
2948 | -- may exist because of a with_clause in another unit in the context, | |
2949 | -- but cannot be used because the current view of the enclosing unit | |
2950 | -- is still a limited view. | |
5dcab3ca AC |
2951 | |
2952 | ------------------------- | |
2953 | -- Detect_And_Exchange -- | |
2954 | ------------------------- | |
2955 | ||
2956 | procedure Detect_And_Exchange (Id : Entity_Id) is | |
2957 | Typ : constant Entity_Id := Etype (Id); | |
5dcab3ca | 2958 | begin |
72d5c70b AC |
2959 | if From_Limited_With (Typ) |
2960 | and then Has_Non_Limited_View (Typ) | |
2961 | and then not From_Limited_With (Scope (Typ)) | |
2962 | then | |
1e55d29a EB |
2963 | if No (Result) then |
2964 | Result := New_Elmt_List; | |
2965 | end if; | |
2966 | ||
2967 | Prepend_Elmt (Typ, Result); | |
2968 | Prepend_Elmt (Id, Result); | |
5dcab3ca AC |
2969 | Set_Etype (Id, Non_Limited_View (Typ)); |
2970 | end if; | |
2971 | end Detect_And_Exchange; | |
2972 | ||
2973 | -- Local variables | |
2974 | ||
2975 | Formal : Entity_Id; | |
2976 | ||
2977 | -- Start of processing for Exchange_Limited_Views | |
2978 | ||
2979 | begin | |
5dcab3ca AC |
2980 | -- Do not process subprogram bodies as they already use the non- |
2981 | -- limited view of types. | |
2982 | ||
128a98ea | 2983 | if not Ekind_In (Subp_Id, E_Function, E_Procedure) then |
1e55d29a | 2984 | return No_Elist; |
5dcab3ca AC |
2985 | end if; |
2986 | ||
2987 | -- Examine all formals and swap views when applicable | |
2988 | ||
2989 | Formal := First_Formal (Subp_Id); | |
2990 | while Present (Formal) loop | |
2991 | Detect_And_Exchange (Formal); | |
2992 | ||
2993 | Next_Formal (Formal); | |
2994 | end loop; | |
2995 | ||
2996 | -- Process the return type of a function | |
2997 | ||
2998 | if Ekind (Subp_Id) = E_Function then | |
2999 | Detect_And_Exchange (Subp_Id); | |
3000 | end if; | |
1e55d29a EB |
3001 | |
3002 | return Result; | |
5dcab3ca AC |
3003 | end Exchange_Limited_Views; |
3004 | ||
448a1eb3 AC |
3005 | ----------------------- |
3006 | -- Freeze_Expr_Types -- | |
3007 | ----------------------- | |
3008 | ||
3009 | procedure Freeze_Expr_Types (Spec_Id : Entity_Id) is | |
f8981f19 AC |
3010 | function Cloned_Expression return Node_Id; |
3011 | -- Build a duplicate of the expression of the return statement that | |
3012 | -- has no defining entities shared with the original expression. | |
3013 | ||
448a1eb3 AC |
3014 | function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result; |
3015 | -- Freeze all types referenced in the subtree rooted at Node | |
3016 | ||
f8981f19 AC |
3017 | ----------------------- |
3018 | -- Cloned_Expression -- | |
3019 | ----------------------- | |
3020 | ||
3021 | function Cloned_Expression return Node_Id is | |
3022 | function Clone_Id (Node : Node_Id) return Traverse_Result; | |
3023 | -- Tree traversal routine that clones the defining identifier of | |
3024 | -- iterator and loop parameter specification nodes. | |
3025 | ||
3026 | ---------------- | |
3027 | -- Check_Node -- | |
3028 | ---------------- | |
3029 | ||
3030 | function Clone_Id (Node : Node_Id) return Traverse_Result is | |
3031 | begin | |
3032 | if Nkind_In (Node, N_Iterator_Specification, | |
3033 | N_Loop_Parameter_Specification) | |
3034 | then | |
3035 | Set_Defining_Identifier (Node, | |
3036 | New_Copy (Defining_Identifier (Node))); | |
3037 | end if; | |
3038 | ||
3039 | return OK; | |
3040 | end Clone_Id; | |
3041 | ||
3042 | ------------------- | |
3043 | -- Clone_Def_Ids -- | |
3044 | ------------------- | |
3045 | ||
3046 | procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id); | |
3047 | ||
3048 | -- Local variables | |
3049 | ||
3050 | Return_Stmt : constant Node_Id := | |
3051 | First | |
3052 | (Statements (Handled_Statement_Sequence (N))); | |
3053 | Dup_Expr : Node_Id; | |
3054 | ||
3055 | -- Start of processing for Cloned_Expression | |
3056 | ||
3057 | begin | |
3058 | pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement); | |
3059 | ||
3060 | -- We must duplicate the expression with semantic information to | |
3061 | -- inherit the decoration of global entities in generic instances. | |
3062 | ||
3063 | Dup_Expr := New_Copy_Tree (Expression (Return_Stmt)); | |
3064 | ||
3065 | -- Replace the defining identifier of iterators and loop param | |
3066 | -- specifications by a clone to ensure that the cloned expression | |
3067 | -- and the original expression don't have shared identifiers; | |
3068 | -- otherwise, as part of the preanalysis of the expression, these | |
3069 | -- shared identifiers may be left decorated with itypes which | |
3070 | -- will not be available in the tree passed to the backend. | |
3071 | ||
3072 | Clone_Def_Ids (Dup_Expr); | |
3073 | ||
3074 | return Dup_Expr; | |
3075 | end Cloned_Expression; | |
3076 | ||
448a1eb3 AC |
3077 | ---------------------- |
3078 | -- Freeze_Type_Refs -- | |
3079 | ---------------------- | |
3080 | ||
3081 | function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is | |
3082 | begin | |
3083 | if Nkind (Node) = N_Identifier | |
3084 | and then Present (Entity (Node)) | |
3085 | then | |
3086 | if Is_Type (Entity (Node)) then | |
3087 | Freeze_Before (N, Entity (Node)); | |
3088 | ||
3089 | elsif Ekind_In (Entity (Node), E_Component, | |
3090 | E_Discriminant) | |
3091 | then | |
241f328c ES |
3092 | declare |
3093 | Rec : constant Entity_Id := Scope (Entity (Node)); | |
3094 | begin | |
3095 | ||
3096 | -- Check that the enclosing record type can be frozen. | |
3097 | -- This provides a better error message than generating | |
a267d8cc AC |
3098 | -- primitives whose compilation fails much later. Refine |
3099 | -- the error message if possible. | |
241f328c ES |
3100 | |
3101 | Check_Fully_Declared (Rec, Node); | |
3102 | ||
3103 | if Error_Posted (Node) then | |
3104 | if Has_Private_Component (Rec) then | |
a267d8cc AC |
3105 | Error_Msg_NE |
3106 | ("\type& has private component", Node, Rec); | |
241f328c ES |
3107 | end if; |
3108 | ||
3109 | else | |
3110 | Freeze_Before (N, Rec); | |
3111 | end if; | |
3112 | end; | |
448a1eb3 AC |
3113 | end if; |
3114 | end if; | |
3115 | ||
3116 | return OK; | |
3117 | end Freeze_Type_Refs; | |
3118 | ||
3119 | procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs); | |
3120 | ||
3121 | -- Local variables | |
3122 | ||
448a1eb3 AC |
3123 | Saved_First_Entity : constant Entity_Id := First_Entity (Spec_Id); |
3124 | Saved_Last_Entity : constant Entity_Id := Last_Entity (Spec_Id); | |
f8981f19 | 3125 | Dup_Expr : constant Node_Id := Cloned_Expression; |
448a1eb3 AC |
3126 | |
3127 | -- Start of processing for Freeze_Expr_Types | |
3128 | ||
3129 | begin | |
448a1eb3 AC |
3130 | -- Preanalyze a duplicate of the expression to have available the |
3131 | -- minimum decoration needed to locate referenced unfrozen types | |
3132 | -- without adding any decoration to the function expression. This | |
3133 | -- preanalysis is performed with errors disabled to avoid reporting | |
3134 | -- spurious errors on Ghost entities (since the expression is not | |
3135 | -- fully analyzed). | |
3136 | ||
3137 | Push_Scope (Spec_Id); | |
3138 | Install_Formals (Spec_Id); | |
3139 | Ignore_Errors_Enable := Ignore_Errors_Enable + 1; | |
3140 | ||
3141 | Preanalyze_Spec_Expression (Dup_Expr, Etype (Spec_Id)); | |
3142 | ||
3143 | Ignore_Errors_Enable := Ignore_Errors_Enable - 1; | |
3144 | End_Scope; | |
3145 | ||
3146 | -- Restore certain attributes of Spec_Id since the preanalysis may | |
3147 | -- have introduced itypes to this scope, thus modifying attributes | |
3148 | -- First_Entity and Last_Entity. | |
3149 | ||
3150 | Set_First_Entity (Spec_Id, Saved_First_Entity); | |
3151 | Set_Last_Entity (Spec_Id, Saved_Last_Entity); | |
3152 | ||
f8981f19 AC |
3153 | if Present (Last_Entity (Spec_Id)) then |
3154 | Set_Next_Entity (Last_Entity (Spec_Id), Empty); | |
3155 | end if; | |
3156 | ||
448a1eb3 AC |
3157 | -- Freeze all types referenced in the expression |
3158 | ||
3159 | Freeze_References (Dup_Expr); | |
3160 | end Freeze_Expr_Types; | |
3161 | ||
d44202ba HK |
3162 | ------------------------------------- |
3163 | -- Is_Private_Concurrent_Primitive -- | |
3164 | ------------------------------------- | |
3165 | ||
3166 | function Is_Private_Concurrent_Primitive | |
3167 | (Subp_Id : Entity_Id) return Boolean | |
3168 | is | |
3169 | Formal_Typ : Entity_Id; | |
3170 | ||
3171 | begin | |
3172 | if Present (First_Formal (Subp_Id)) then | |
3173 | Formal_Typ := Etype (First_Formal (Subp_Id)); | |
3174 | ||
3175 | if Is_Concurrent_Record_Type (Formal_Typ) then | |
df3e68b1 HK |
3176 | if Is_Class_Wide_Type (Formal_Typ) then |
3177 | Formal_Typ := Root_Type (Formal_Typ); | |
3178 | end if; | |
3179 | ||
d44202ba HK |
3180 | Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); |
3181 | end if; | |
3182 | ||
3183 | -- The type of the first formal is a concurrent tagged type with | |
3184 | -- a private view. | |
3185 | ||
3186 | return | |
3187 | Is_Concurrent_Type (Formal_Typ) | |
3188 | and then Is_Tagged_Type (Formal_Typ) | |
3189 | and then Has_Private_Declaration (Formal_Typ); | |
3190 | end if; | |
3191 | ||
3192 | return False; | |
3193 | end Is_Private_Concurrent_Primitive; | |
3194 | ||
1e55d29a EB |
3195 | --------------------------- |
3196 | -- Restore_Limited_Views -- | |
3197 | --------------------------- | |
3198 | ||
3199 | procedure Restore_Limited_Views (Restore_List : Elist_Id) is | |
3200 | Elmt : Elmt_Id := First_Elmt (Restore_List); | |
3201 | Id : Entity_Id; | |
3202 | ||
3203 | begin | |
3204 | while Present (Elmt) loop | |
3205 | Id := Node (Elmt); | |
3206 | Next_Elmt (Elmt); | |
3207 | Set_Etype (Id, Node (Elmt)); | |
3208 | Next_Elmt (Elmt); | |
3209 | end loop; | |
3210 | end Restore_Limited_Views; | |
3211 | ||
76a69663 ES |
3212 | ---------------------------- |
3213 | -- Set_Trivial_Subprogram -- | |
3214 | ---------------------------- | |
3215 | ||
3216 | procedure Set_Trivial_Subprogram (N : Node_Id) is | |
3217 | Nxt : constant Node_Id := Next (N); | |
3218 | ||
3219 | begin | |
3220 | Set_Is_Trivial_Subprogram (Body_Id); | |
3221 | ||
3222 | if Present (Spec_Id) then | |
3223 | Set_Is_Trivial_Subprogram (Spec_Id); | |
3224 | end if; | |
3225 | ||
3226 | if Present (Nxt) | |
3227 | and then Nkind (Nxt) = N_Simple_Return_Statement | |
3228 | and then No (Next (Nxt)) | |
3229 | and then Present (Expression (Nxt)) | |
3230 | and then Is_Entity_Name (Expression (Nxt)) | |
3231 | then | |
3232 | Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); | |
3233 | end if; | |
3234 | end Set_Trivial_Subprogram; | |
3235 | ||
758c442c GD |
3236 | --------------------------------- |
3237 | -- Verify_Overriding_Indicator -- | |
3238 | --------------------------------- | |
3239 | ||
3240 | procedure Verify_Overriding_Indicator is | |
3241 | begin | |
21d27997 RD |
3242 | if Must_Override (Body_Spec) then |
3243 | if Nkind (Spec_Id) = N_Defining_Operator_Symbol | |
4b6f99f5 | 3244 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) |
21d27997 RD |
3245 | then |
3246 | null; | |
3247 | ||
038140ed | 3248 | elsif not Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 3249 | Error_Msg_NE |
21d27997 | 3250 | ("subprogram& is not overriding", Body_Spec, Spec_Id); |
23e28b42 AC |
3251 | |
3252 | -- Overriding indicators aren't allowed for protected subprogram | |
3253 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
3254 | -- this to a warning if -gnatd.E is enabled. | |
3255 | ||
3256 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
3257 | Error_Msg_Warn := Error_To_Warning; | |
3258 | Error_Msg_N | |
b785e0b8 | 3259 | ("<<overriding indicator not allowed for protected " |
23e28b42 | 3260 | & "subprogram body", Body_Spec); |
21d27997 | 3261 | end if; |
758c442c | 3262 | |
5d37ba92 | 3263 | elsif Must_Not_Override (Body_Spec) then |
038140ed | 3264 | if Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 3265 | Error_Msg_NE |
5d37ba92 | 3266 | ("subprogram& overrides inherited operation", |
76a69663 | 3267 | Body_Spec, Spec_Id); |
5d37ba92 | 3268 | |
21d27997 | 3269 | elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol |
4b6f99f5 | 3270 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) |
21d27997 | 3271 | then |
ed2233dc | 3272 | Error_Msg_NE |
3ccedacc | 3273 | ("subprogram& overrides predefined operator ", |
21d27997 RD |
3274 | Body_Spec, Spec_Id); |
3275 | ||
23e28b42 AC |
3276 | -- Overriding indicators aren't allowed for protected subprogram |
3277 | -- bodies (see the Confirmation in Ada Comment AC95-00213). Change | |
3278 | -- this to a warning if -gnatd.E is enabled. | |
3279 | ||
3280 | elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then | |
3281 | Error_Msg_Warn := Error_To_Warning; | |
5d37ba92 | 3282 | |
23e28b42 | 3283 | Error_Msg_N |
3ccedacc AC |
3284 | ("<<overriding indicator not allowed " |
3285 | & "for protected subprogram body", Body_Spec); | |
23e28b42 AC |
3286 | |
3287 | -- If this is not a primitive operation, then the overriding | |
3288 | -- indicator is altogether illegal. | |
3289 | ||
3290 | elsif not Is_Primitive (Spec_Id) then | |
ed2233dc | 3291 | Error_Msg_N |
3ccedacc AC |
3292 | ("overriding indicator only allowed " |
3293 | & "if subprogram is primitive", Body_Spec); | |
5d37ba92 | 3294 | end if; |
235f4375 | 3295 | |
23e28b42 AC |
3296 | -- If checking the style rule and the operation overrides, then |
3297 | -- issue a warning about a missing overriding_indicator. Protected | |
3298 | -- subprogram bodies are excluded from this style checking, since | |
3299 | -- they aren't primitives (even though their declarations can | |
3300 | -- override) and aren't allowed to have an overriding_indicator. | |
3301 | ||
806f6d37 | 3302 | elsif Style_Check |
038140ed | 3303 | and then Present (Overridden_Operation (Spec_Id)) |
23e28b42 | 3304 | and then Ekind (Scope (Spec_Id)) /= E_Protected_Type |
235f4375 AC |
3305 | then |
3306 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3307 | Style.Missing_Overriding (N, Body_Id); | |
806f6d37 AC |
3308 | |
3309 | elsif Style_Check | |
3310 | and then Can_Override_Operator (Spec_Id) | |
3311 | and then not Is_Predefined_File_Name | |
3312 | (Unit_File_Name (Get_Source_Unit (Spec_Id))) | |
3313 | then | |
3314 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
3315 | Style.Missing_Overriding (N, Body_Id); | |
758c442c GD |
3316 | end if; |
3317 | end Verify_Overriding_Indicator; | |
3318 | ||
1af4455a HK |
3319 | -- Local variables |
3320 | ||
f9a8f910 HK |
3321 | Saved_GM : constant Ghost_Mode_Type := Ghost_Mode; |
3322 | Saved_ISMP : constant Boolean := | |
3323 | Ignore_SPARK_Mode_Pragmas_In_Instance; | |
3324 | -- Save the Ghost and SPARK mode-related data to restore on exit | |
1af4455a | 3325 | |
b1b543d2 | 3326 | -- Start of processing for Analyze_Subprogram_Body_Helper |
0fb2ea01 | 3327 | |
996ae0b0 | 3328 | begin |
879ac954 | 3329 | -- A [generic] subprogram body "freezes" the contract of the nearest |
e645cb39 | 3330 | -- enclosing package body and all other contracts encountered in the |
4404c282 | 3331 | -- same declarative part up to and excluding the subprogram body: |
879ac954 AC |
3332 | |
3333 | -- package body Nearest_Enclosing_Package | |
3334 | -- with Refined_State => (State => Constit) | |
3335 | -- is | |
3336 | -- Constit : ...; | |
3337 | ||
3338 | -- procedure Freezes_Enclosing_Package_Body | |
3339 | -- with Refined_Depends => (Input => Constit) ... | |
3340 | ||
3341 | -- This ensures that any annotations referenced by the contract of the | |
3342 | -- [generic] subprogram body are available. This form of "freezing" is | |
3343 | -- decoupled from the usual Freeze_xxx mechanism because it must also | |
3344 | -- work in the context of generics where normal freezing is disabled. | |
3345 | ||
77237288 AC |
3346 | -- Only bodies coming from source should cause this type of "freezing". |
3347 | -- Expression functions that act as bodies and complete an initial | |
3348 | -- declaration must be included in this category, hence the use of | |
3349 | -- Original_Node. | |
3350 | ||
3351 | if Comes_From_Source (Original_Node (N)) then | |
e645cb39 | 3352 | Analyze_Previous_Contracts (N); |
77237288 | 3353 | end if; |
879ac954 | 3354 | |
82c80734 RD |
3355 | -- Generic subprograms are handled separately. They always have a |
3356 | -- generic specification. Determine whether current scope has a | |
3357 | -- previous declaration. | |
996ae0b0 | 3358 | |
82c80734 RD |
3359 | -- If the subprogram body is defined within an instance of the same |
3360 | -- name, the instance appears as a package renaming, and will be hidden | |
3361 | -- within the subprogram. | |
996ae0b0 RK |
3362 | |
3363 | if Present (Prev_Id) | |
3364 | and then not Is_Overloadable (Prev_Id) | |
3365 | and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration | |
3366 | or else Comes_From_Source (Prev_Id)) | |
3367 | then | |
fbf5a39b | 3368 | if Is_Generic_Subprogram (Prev_Id) then |
996ae0b0 | 3369 | Spec_Id := Prev_Id; |
8636f52f | 3370 | |
1af4455a HK |
3371 | -- A subprogram body is Ghost when it is stand alone and subject |
3372 | -- to pragma Ghost or when the corresponding spec is Ghost. Set | |
3373 | -- the mode now to ensure that any nodes generated during analysis | |
3374 | -- and expansion are properly marked as Ghost. | |
8636f52f | 3375 | |
f9a8f910 | 3376 | Mark_And_Set_Ghost_Body (N, Spec_Id); |
d65a80fd | 3377 | |
996ae0b0 RK |
3378 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); |
3379 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3380 | ||
3381 | Analyze_Generic_Subprogram_Body (N, Spec_Id); | |
7665e4bd AC |
3382 | |
3383 | if Nkind (N) = N_Subprogram_Body then | |
3384 | HSS := Handled_Statement_Sequence (N); | |
3385 | Check_Missing_Return; | |
3386 | end if; | |
3387 | ||
d65a80fd | 3388 | goto Leave; |
996ae0b0 | 3389 | |
d65a80fd HK |
3390 | -- Otherwise a previous entity conflicts with the subprogram name. |
3391 | -- Attempting to enter name will post error. | |
996ae0b0 | 3392 | |
d65a80fd | 3393 | else |
996ae0b0 | 3394 | Enter_Name (Body_Id); |
cf9a473e | 3395 | goto Leave; |
996ae0b0 RK |
3396 | end if; |
3397 | ||
82c80734 RD |
3398 | -- Non-generic case, find the subprogram declaration, if one was seen, |
3399 | -- or enter new overloaded entity in the current scope. If the | |
3400 | -- Current_Entity is the Body_Id itself, the unit is being analyzed as | |
3401 | -- part of the context of one of its subunits. No need to redo the | |
3402 | -- analysis. | |
996ae0b0 | 3403 | |
8fde064e | 3404 | elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then |
cf9a473e | 3405 | goto Leave; |
996ae0b0 RK |
3406 | |
3407 | else | |
fbf5a39b | 3408 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); |
996ae0b0 RK |
3409 | |
3410 | if Nkind (N) = N_Subprogram_Body_Stub | |
3411 | or else No (Corresponding_Spec (N)) | |
3412 | then | |
d44202ba HK |
3413 | if Is_Private_Concurrent_Primitive (Body_Id) then |
3414 | Spec_Id := Disambiguate_Spec; | |
8636f52f | 3415 | |
1af4455a HK |
3416 | -- A subprogram body is Ghost when it is stand alone and |
3417 | -- subject to pragma Ghost or when the corresponding spec is | |
3418 | -- Ghost. Set the mode now to ensure that any nodes generated | |
3419 | -- during analysis and expansion are properly marked as Ghost. | |
8636f52f | 3420 | |
f9a8f910 | 3421 | Mark_And_Set_Ghost_Body (N, Spec_Id); |
8636f52f | 3422 | |
d44202ba HK |
3423 | else |
3424 | Spec_Id := Find_Corresponding_Spec (N); | |
b6c8e5be | 3425 | |
1af4455a HK |
3426 | -- A subprogram body is Ghost when it is stand alone and |
3427 | -- subject to pragma Ghost or when the corresponding spec is | |
3428 | -- Ghost. Set the mode now to ensure that any nodes generated | |
3429 | -- during analysis and expansion are properly marked as Ghost. | |
8636f52f | 3430 | |
f9a8f910 | 3431 | Mark_And_Set_Ghost_Body (N, Spec_Id); |
8636f52f | 3432 | |
b6c8e5be AC |
3433 | -- In GNATprove mode, if the body has no previous spec, create |
3434 | -- one so that the inlining machinery can operate properly. | |
3435 | -- Transfer aspects, if any, to the new spec, so that they | |
3436 | -- are legal and can be processed ahead of the body. | |
3437 | -- We make two copies of the given spec, one for the new | |
3438 | -- declaration, and one for the body. | |
3439 | ||
5a271a7f | 3440 | if No (Spec_Id) and then GNATprove_Mode |
480156b2 | 3441 | |
480156b2 AC |
3442 | -- Inlining does not apply during pre-analysis of code |
3443 | ||
b6c8e5be | 3444 | and then Full_Analysis |
480156b2 AC |
3445 | |
3446 | -- Inlining only applies to full bodies, not stubs | |
3447 | ||
7b2888e6 | 3448 | and then Nkind (N) /= N_Subprogram_Body_Stub |
480156b2 AC |
3449 | |
3450 | -- Inlining only applies to bodies in the source code, not to | |
3451 | -- those generated by the compiler. In particular, expression | |
3452 | -- functions, whose body is generated by the compiler, are | |
3453 | -- treated specially by GNATprove. | |
3454 | ||
b6c8e5be | 3455 | and then Comes_From_Source (Body_Id) |
480156b2 AC |
3456 | |
3457 | -- This cannot be done for a compilation unit, which is not | |
3458 | -- in a context where we can insert a new spec. | |
3459 | ||
b6c8e5be | 3460 | and then Is_List_Member (N) |
480156b2 AC |
3461 | |
3462 | -- Inlining only applies to subprograms without contracts, | |
3463 | -- as a contract is a sign that GNATprove should perform a | |
3464 | -- modular analysis of the subprogram instead of a contextual | |
3465 | -- analysis at each call site. The same test is performed in | |
3466 | -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated | |
ca90b962 GD |
3467 | -- here in another form (because the contract has not been |
3468 | -- attached to the body) to avoid front-end errors in case | |
3469 | -- pragmas are used instead of aspects, because the | |
480156b2 AC |
3470 | -- corresponding pragmas in the body would not be transferred |
3471 | -- to the spec, leading to legality errors. | |
3472 | ||
ac072cb2 | 3473 | and then not Body_Has_Contract |
8d1fe980 | 3474 | and then not Inside_A_Generic |
b6c8e5be | 3475 | then |
8d1fe980 | 3476 | Build_Subprogram_Declaration; |
2ba4f1fb AC |
3477 | |
3478 | -- If this is a function that returns a constrained array, and | |
3479 | -- we are generating SPARK_For_C, create subprogram declaration | |
3480 | -- to simplify subsequent C generation. | |
3481 | ||
3482 | elsif No (Spec_Id) | |
3483 | and then Modify_Tree_For_C | |
3484 | and then Nkind (Body_Spec) = N_Function_Specification | |
3485 | and then Is_Array_Type (Etype (Body_Id)) | |
3486 | and then Is_Constrained (Etype (Body_Id)) | |
3487 | then | |
3488 | Build_Subprogram_Declaration; | |
b6c8e5be | 3489 | end if; |
d44202ba | 3490 | end if; |
996ae0b0 RK |
3491 | |
3492 | -- If this is a duplicate body, no point in analyzing it | |
3493 | ||
3494 | if Error_Posted (N) then | |
d65a80fd | 3495 | goto Leave; |
996ae0b0 RK |
3496 | end if; |
3497 | ||
82c80734 RD |
3498 | -- A subprogram body should cause freezing of its own declaration, |
3499 | -- but if there was no previous explicit declaration, then the | |
3500 | -- subprogram will get frozen too late (there may be code within | |
3501 | -- the body that depends on the subprogram having been frozen, | |
3502 | -- such as uses of extra formals), so we force it to be frozen | |
76a69663 | 3503 | -- here. Same holds if the body and spec are compilation units. |
cd1c668b ES |
3504 | -- Finally, if the return type is an anonymous access to protected |
3505 | -- subprogram, it must be frozen before the body because its | |
3506 | -- expansion has generated an equivalent type that is used when | |
3507 | -- elaborating the body. | |
996ae0b0 | 3508 | |
885c4871 | 3509 | -- An exception in the case of Ada 2012, AI05-177: The bodies |
ebb6faaa AC |
3510 | -- created for expression functions do not freeze. |
3511 | ||
3512 | if No (Spec_Id) | |
3513 | and then Nkind (Original_Node (N)) /= N_Expression_Function | |
3514 | then | |
996ae0b0 RK |
3515 | Freeze_Before (N, Body_Id); |
3516 | ||
3517 | elsif Nkind (Parent (N)) = N_Compilation_Unit then | |
3518 | Freeze_Before (N, Spec_Id); | |
cd1c668b ES |
3519 | |
3520 | elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then | |
3521 | Freeze_Before (N, Etype (Body_Id)); | |
996ae0b0 | 3522 | end if; |
a38ff9b1 | 3523 | |
996ae0b0 RK |
3524 | else |
3525 | Spec_Id := Corresponding_Spec (N); | |
8636f52f | 3526 | |
1af4455a HK |
3527 | -- A subprogram body is Ghost when it is stand alone and subject |
3528 | -- to pragma Ghost or when the corresponding spec is Ghost. Set | |
3529 | -- the mode now to ensure that any nodes generated during analysis | |
3530 | -- and expansion are properly marked as Ghost. | |
8636f52f | 3531 | |
f9a8f910 | 3532 | Mark_And_Set_Ghost_Body (N, Spec_Id); |
996ae0b0 RK |
3533 | end if; |
3534 | end if; | |
3535 | ||
799d0e05 AC |
3536 | -- Previously we scanned the body to look for nested subprograms, and |
3537 | -- rejected an inline directive if nested subprograms were present, | |
3538 | -- because the back-end would generate conflicting symbols for the | |
c8957aae | 3539 | -- nested bodies. This is now unnecessary. |
07fc65c4 | 3540 | |
c8957aae | 3541 | -- Look ahead to recognize a pragma Inline that appears after the body |
84f4072a | 3542 | |
e660dbf7 JM |
3543 | Check_Inline_Pragma (Spec_Id); |
3544 | ||
701b7fbb RD |
3545 | -- Deal with special case of a fully private operation in the body of |
3546 | -- the protected type. We must create a declaration for the subprogram, | |
3547 | -- in order to attach the protected subprogram that will be used in | |
3548 | -- internal calls. We exclude compiler generated bodies from the | |
3549 | -- expander since the issue does not arise for those cases. | |
07fc65c4 | 3550 | |
996ae0b0 RK |
3551 | if No (Spec_Id) |
3552 | and then Comes_From_Source (N) | |
3553 | and then Is_Protected_Type (Current_Scope) | |
3554 | then | |
47bfea3a | 3555 | Spec_Id := Build_Private_Protected_Declaration (N); |
701b7fbb | 3556 | end if; |
996ae0b0 | 3557 | |
c42aba6b AC |
3558 | -- If we are generating C and this is a function returning a constrained |
3559 | -- array type for which we must create a procedure with an extra out | |
c4dc2125 | 3560 | -- parameter, build and analyze the body now. The procedure declaration |
c42aba6b AC |
3561 | -- has already been created. We reuse the source body of the function, |
3562 | -- because in an instance it may contain global references that cannot | |
3563 | -- be reanalyzed. The source function itself is not used any further, | |
2a253c5b AC |
3564 | -- so we mark it as having a completion. If the subprogram is a stub the |
3565 | -- transformation is done later, when the proper body is analyzed. | |
c42aba6b AC |
3566 | |
3567 | if Expander_Active | |
3568 | and then Modify_Tree_For_C | |
3569 | and then Present (Spec_Id) | |
3570 | and then Ekind (Spec_Id) = E_Function | |
2a253c5b | 3571 | and then Nkind (N) /= N_Subprogram_Body_Stub |
c42aba6b AC |
3572 | and then Rewritten_For_C (Spec_Id) |
3573 | then | |
3574 | Set_Has_Completion (Spec_Id); | |
3575 | ||
3576 | Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N)); | |
3577 | Analyze (N); | |
3578 | ||
c4dc2125 HK |
3579 | -- The entity for the created procedure must remain invisible, so it |
3580 | -- does not participate in resolution of subsequent references to the | |
3581 | -- function. | |
c42aba6b AC |
3582 | |
3583 | Set_Is_Immediately_Visible (Corresponding_Spec (N), False); | |
d65a80fd | 3584 | goto Leave; |
c42aba6b AC |
3585 | end if; |
3586 | ||
5334d18f | 3587 | -- If a separate spec is present, then deal with freezing issues |
7ca78bba | 3588 | |
701b7fbb | 3589 | if Present (Spec_Id) then |
996ae0b0 | 3590 | Spec_Decl := Unit_Declaration_Node (Spec_Id); |
758c442c | 3591 | Verify_Overriding_Indicator; |
5d37ba92 ES |
3592 | |
3593 | -- In general, the spec will be frozen when we start analyzing the | |
3594 | -- body. However, for internally generated operations, such as | |
3595 | -- wrapper functions for inherited operations with controlling | |
164e06c6 AC |
3596 | -- results, the spec may not have been frozen by the time we expand |
3597 | -- the freeze actions that include the bodies. In particular, extra | |
3598 | -- formals for accessibility or for return-in-place may need to be | |
3599 | -- generated. Freeze nodes, if any, are inserted before the current | |
36f2e3d3 AC |
3600 | -- body. These freeze actions are also needed in ASIS mode and in |
3601 | -- Compile_Only mode to enable the proper back-end type annotations. | |
3602 | -- They are necessary in any case to insure order of elaboration | |
3603 | -- in gigi. | |
5d37ba92 ES |
3604 | |
3605 | if not Is_Frozen (Spec_Id) | |
36f2e3d3 AC |
3606 | and then (Expander_Active |
3607 | or else ASIS_Mode | |
3608 | or else (Operating_Mode = Check_Semantics | |
3609 | and then Serious_Errors_Detected = 0)) | |
5d37ba92 | 3610 | then |
5d37ba92 | 3611 | Set_Has_Delayed_Freeze (Spec_Id); |
6b958cec | 3612 | Freeze_Before (N, Spec_Id); |
448a1eb3 | 3613 | |
804ec349 GD |
3614 | -- AI12-0103: At the occurrence of an expression function |
3615 | -- declaration that is a completion, its expression causes | |
3616 | -- freezing. | |
448a1eb3 AC |
3617 | |
3618 | if Has_Completion (Spec_Id) | |
36be0082 | 3619 | and then Nkind (N) = N_Subprogram_Body |
448a1eb3 AC |
3620 | and then Was_Expression_Function (N) |
3621 | then | |
3622 | Freeze_Expr_Types (Spec_Id); | |
3623 | end if; | |
5d37ba92 | 3624 | end if; |
996ae0b0 RK |
3625 | end if; |
3626 | ||
aaa0a838 ES |
3627 | -- If the subprogram has a class-wide clone, build its body as a copy |
3628 | -- of the original body, and rewrite body of original subprogram as a | |
3629 | -- wrapper that calls the clone. | |
3630 | ||
3631 | if Present (Spec_Id) | |
3632 | and then Present (Class_Wide_Clone (Spec_Id)) | |
3633 | and then (Comes_From_Source (N) or else Was_Expression_Function (N)) | |
3634 | then | |
3635 | Build_Class_Wide_Clone_Body (Spec_Id, N); | |
3636 | ||
3637 | -- This is the new body for the existing primitive operation | |
3638 | ||
3639 | Rewrite (N, Build_Class_Wide_Clone_Call | |
3640 | (Sloc (N), New_List, Spec_Id, Parent (Spec_Id))); | |
3641 | Set_Has_Completion (Spec_Id, False); | |
3642 | Analyze (N); | |
3643 | return; | |
3644 | end if; | |
3645 | ||
996ae0b0 RK |
3646 | -- Place subprogram on scope stack, and make formals visible. If there |
3647 | -- is a spec, the visible entity remains that of the spec. | |
3648 | ||
3649 | if Present (Spec_Id) then | |
07fc65c4 | 3650 | Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); |
758c442c GD |
3651 | |
3652 | if Is_Child_Unit (Spec_Id) then | |
3653 | Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False); | |
3654 | end if; | |
3655 | ||
fbf5a39b AC |
3656 | if Style_Check then |
3657 | Style.Check_Identifier (Body_Id, Spec_Id); | |
3658 | end if; | |
996ae0b0 RK |
3659 | |
3660 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
3661 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
3662 | ||
f937473f | 3663 | if Is_Abstract_Subprogram (Spec_Id) then |
ed2233dc | 3664 | Error_Msg_N ("an abstract subprogram cannot have a body", N); |
d65a80fd | 3665 | goto Leave; |
21d27997 | 3666 | |
996ae0b0 RK |
3667 | else |
3668 | Set_Convention (Body_Id, Convention (Spec_Id)); | |
3669 | Set_Has_Completion (Spec_Id); | |
3670 | ||
3671 | if Is_Protected_Type (Scope (Spec_Id)) then | |
21d27997 | 3672 | Prot_Typ := Scope (Spec_Id); |
996ae0b0 RK |
3673 | end if; |
3674 | ||
3675 | -- If this is a body generated for a renaming, do not check for | |
3676 | -- full conformance. The check is redundant, because the spec of | |
3677 | -- the body is a copy of the spec in the renaming declaration, | |
3678 | -- and the test can lead to spurious errors on nested defaults. | |
3679 | ||
3680 | if Present (Spec_Decl) | |
996ae0b0 | 3681 | and then not Comes_From_Source (N) |
93a81b02 GB |
3682 | and then |
3683 | (Nkind (Original_Node (Spec_Decl)) = | |
4b6f99f5 | 3684 | N_Subprogram_Renaming_Declaration |
466c2127 AC |
3685 | or else (Present (Corresponding_Body (Spec_Decl)) |
3686 | and then | |
3687 | Nkind (Unit_Declaration_Node | |
3688 | (Corresponding_Body (Spec_Decl))) = | |
3689 | N_Subprogram_Renaming_Declaration)) | |
996ae0b0 RK |
3690 | then |
3691 | Conformant := True; | |
cabe9abc AC |
3692 | |
3693 | -- Conversely, the spec may have been generated for specless body | |
a6363ed3 AC |
3694 | -- with an inline pragma. The entity comes from source, which is |
3695 | -- both semantically correct and necessary for proper inlining. | |
3696 | -- The subprogram declaration itself is not in the source. | |
cabe9abc AC |
3697 | |
3698 | elsif Comes_From_Source (N) | |
a6363ed3 AC |
3699 | and then Present (Spec_Decl) |
3700 | and then not Comes_From_Source (Spec_Decl) | |
cabe9abc AC |
3701 | and then Has_Pragma_Inline (Spec_Id) |
3702 | then | |
3703 | Conformant := True; | |
76a69663 | 3704 | |
996ae0b0 RK |
3705 | else |
3706 | Check_Conformance | |
3707 | (Body_Id, Spec_Id, | |
76a69663 | 3708 | Fully_Conformant, True, Conformant, Body_Id); |
996ae0b0 RK |
3709 | end if; |
3710 | ||
3711 | -- If the body is not fully conformant, we have to decide if we | |
3712 | -- should analyze it or not. If it has a really messed up profile | |
3713 | -- then we probably should not analyze it, since we will get too | |
3714 | -- many bogus messages. | |
3715 | ||
3716 | -- Our decision is to go ahead in the non-fully conformant case | |
3717 | -- only if it is at least mode conformant with the spec. Note | |
3718 | -- that the call to Check_Fully_Conformant has issued the proper | |
3719 | -- error messages to complain about the lack of conformance. | |
3720 | ||
3721 | if not Conformant | |
3722 | and then not Mode_Conformant (Body_Id, Spec_Id) | |
3723 | then | |
d65a80fd | 3724 | goto Leave; |
996ae0b0 RK |
3725 | end if; |
3726 | end if; | |
3727 | ||
996ae0b0 | 3728 | if Spec_Id /= Body_Id then |
fbf5a39b | 3729 | Reference_Body_Formals (Spec_Id, Body_Id); |
996ae0b0 RK |
3730 | end if; |
3731 | ||
579847c2 AC |
3732 | Set_Ekind (Body_Id, E_Subprogram_Body); |
3733 | ||
e28072cd AC |
3734 | if Nkind (N) = N_Subprogram_Body_Stub then |
3735 | Set_Corresponding_Spec_Of_Stub (N, Spec_Id); | |
3736 | ||
3737 | -- Regular body | |
3738 | ||
3739 | else | |
996ae0b0 | 3740 | Set_Corresponding_Spec (N, Spec_Id); |
758c442c | 3741 | |
5d37ba92 ES |
3742 | -- Ada 2005 (AI-345): If the operation is a primitive operation |
3743 | -- of a concurrent type, the type of the first parameter has been | |
3744 | -- replaced with the corresponding record, which is the proper | |
3745 | -- run-time structure to use. However, within the body there may | |
3746 | -- be uses of the formals that depend on primitive operations | |
3747 | -- of the type (in particular calls in prefixed form) for which | |
3748 | -- we need the original concurrent type. The operation may have | |
3749 | -- several controlling formals, so the replacement must be done | |
3750 | -- for all of them. | |
758c442c GD |
3751 | |
3752 | if Comes_From_Source (Spec_Id) | |
3753 | and then Present (First_Entity (Spec_Id)) | |
3754 | and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type | |
3755 | and then Is_Tagged_Type (Etype (First_Entity (Spec_Id))) | |
15918371 AC |
3756 | and then Present (Interfaces (Etype (First_Entity (Spec_Id)))) |
3757 | and then Present (Corresponding_Concurrent_Type | |
3758 | (Etype (First_Entity (Spec_Id)))) | |
758c442c | 3759 | then |
5d37ba92 ES |
3760 | declare |
3761 | Typ : constant Entity_Id := Etype (First_Entity (Spec_Id)); | |
3762 | Form : Entity_Id; | |
3763 | ||
3764 | begin | |
3765 | Form := First_Formal (Spec_Id); | |
3766 | while Present (Form) loop | |
3767 | if Etype (Form) = Typ then | |
3768 | Set_Etype (Form, Corresponding_Concurrent_Type (Typ)); | |
3769 | end if; | |
3770 | ||
3771 | Next_Formal (Form); | |
3772 | end loop; | |
3773 | end; | |
758c442c GD |
3774 | end if; |
3775 | ||
21d27997 RD |
3776 | -- Make the formals visible, and place subprogram on scope stack. |
3777 | -- This is also the point at which we set Last_Real_Spec_Entity | |
3778 | -- to mark the entities which will not be moved to the body. | |
758c442c | 3779 | |
996ae0b0 | 3780 | Install_Formals (Spec_Id); |
21d27997 | 3781 | Last_Real_Spec_Entity := Last_Entity (Spec_Id); |
616547fa AC |
3782 | |
3783 | -- Within an instance, add local renaming declarations so that | |
a5a809b2 AC |
3784 | -- gdb can retrieve the values of actuals more easily. This is |
3785 | -- only relevant if generating code (and indeed we definitely | |
3786 | -- do not want these definitions -gnatc mode, because that would | |
3787 | -- confuse ASIS). | |
616547fa AC |
3788 | |
3789 | if Is_Generic_Instance (Spec_Id) | |
3790 | and then Is_Wrapper_Package (Current_Scope) | |
a5a809b2 | 3791 | and then Expander_Active |
616547fa AC |
3792 | then |
3793 | Build_Subprogram_Instance_Renamings (N, Current_Scope); | |
3794 | end if; | |
3795 | ||
0a36105d | 3796 | Push_Scope (Spec_Id); |
996ae0b0 RK |
3797 | |
3798 | -- Make sure that the subprogram is immediately visible. For | |
3799 | -- child units that have no separate spec this is indispensable. | |
3800 | -- Otherwise it is safe albeit redundant. | |
3801 | ||
3802 | Set_Is_Immediately_Visible (Spec_Id); | |
3803 | end if; | |
3804 | ||
3805 | Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id); | |
ec4867fa | 3806 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); |
c9d70ab1 | 3807 | Set_Scope (Body_Id, Scope (Spec_Id)); |
996ae0b0 RK |
3808 | |
3809 | -- Case of subprogram body with no previous spec | |
3810 | ||
3811 | else | |
3e5daac4 AC |
3812 | -- Check for style warning required |
3813 | ||
996ae0b0 | 3814 | if Style_Check |
3e5daac4 AC |
3815 | |
3816 | -- Only apply check for source level subprograms for which checks | |
3817 | -- have not been suppressed. | |
3818 | ||
996ae0b0 RK |
3819 | and then Comes_From_Source (Body_Id) |
3820 | and then not Suppress_Style_Checks (Body_Id) | |
3e5daac4 AC |
3821 | |
3822 | -- No warnings within an instance | |
3823 | ||
996ae0b0 | 3824 | and then not In_Instance |
3e5daac4 | 3825 | |
b0186f71 | 3826 | -- No warnings for expression functions |
3e5daac4 | 3827 | |
b0186f71 | 3828 | and then Nkind (Original_Node (N)) /= N_Expression_Function |
996ae0b0 RK |
3829 | then |
3830 | Style.Body_With_No_Spec (N); | |
3831 | end if; | |
3832 | ||
3833 | New_Overloaded_Entity (Body_Id); | |
3834 | ||
3835 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
3836 | Set_Acts_As_Spec (N); | |
3837 | Generate_Definition (Body_Id); | |
fbf5a39b AC |
3838 | Generate_Reference |
3839 | (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True); | |
5e127570 AC |
3840 | |
3841 | -- If the body is an entry wrapper created for an entry with | |
ffa168bc | 3842 | -- preconditions, it must be compiled in the context of the |
5e127570 AC |
3843 | -- enclosing synchronized object, because it may mention other |
3844 | -- operations of the type. | |
3845 | ||
3846 | if Is_Entry_Wrapper (Body_Id) then | |
3847 | declare | |
3848 | Prot : constant Entity_Id := Etype (First_Entity (Body_Id)); | |
3849 | begin | |
3850 | Push_Scope (Prot); | |
3851 | Install_Declarations (Prot); | |
3852 | end; | |
3853 | end if; | |
3854 | ||
996ae0b0 | 3855 | Install_Formals (Body_Id); |
e949ee22 | 3856 | |
4a854847 | 3857 | Push_Scope (Body_Id); |
996ae0b0 | 3858 | end if; |
dbe36d67 AC |
3859 | |
3860 | -- For stubs and bodies with no previous spec, generate references to | |
3861 | -- formals. | |
3862 | ||
3863 | Generate_Reference_To_Formals (Body_Id); | |
996ae0b0 RK |
3864 | end if; |
3865 | ||
2bfad6eb HK |
3866 | -- Entry barrier functions are generated outside the protected type and |
3867 | -- should not carry the SPARK_Mode of the enclosing context. | |
5216b599 | 3868 | |
877a5a12 AC |
3869 | if Nkind (N) = N_Subprogram_Body |
3870 | and then Is_Entry_Barrier_Function (N) | |
3871 | then | |
3872 | null; | |
2bfad6eb HK |
3873 | |
3874 | -- The body is generated as part of expression function expansion. When | |
3875 | -- the expression function appears in the visible declarations of a | |
3876 | -- package, the body is added to the private declarations. Since both | |
3877 | -- declarative lists may be subject to a different SPARK_Mode, inherit | |
3878 | -- the mode of the spec. | |
3879 | ||
3880 | -- package P with SPARK_Mode is | |
3881 | -- function Expr_Func ... is (...); -- original | |
3882 | -- [function Expr_Func ...;] -- generated spec | |
3883 | -- -- mode is ON | |
3884 | -- private | |
3885 | -- pragma SPARK_Mode (Off); | |
3886 | -- [function Expr_Func ... is return ...;] -- generated body | |
3887 | -- end P; -- mode is ON | |
3888 | ||
3889 | elsif not Comes_From_Source (N) | |
6e9e35e1 AC |
3890 | and then Present (Spec_Id) |
3891 | and then Is_Expression_Function (Spec_Id) | |
2bfad6eb | 3892 | then |
6e9e35e1 | 3893 | Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id)); |
2bfad6eb | 3894 | Set_SPARK_Pragma_Inherited |
6e9e35e1 | 3895 | (Body_Id, SPARK_Pragma_Inherited (Spec_Id)); |
2bfad6eb HK |
3896 | |
3897 | -- Set the SPARK_Mode from the current context (may be overwritten later | |
3f8c04e7 | 3898 | -- with explicit pragma). Exclude the case where the SPARK_Mode appears |
7f54dc83 | 3899 | -- initially on a stand-alone subprogram body, but is then relocated to |
3f8c04e7 AC |
3900 | -- a generated corresponding spec. In this scenario the mode is shared |
3901 | -- between the spec and body. | |
2bfad6eb | 3902 | |
3f8c04e7 | 3903 | elsif No (SPARK_Pragma (Body_Id)) then |
877a5a12 AC |
3904 | Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); |
3905 | Set_SPARK_Pragma_Inherited (Body_Id); | |
3906 | end if; | |
5216b599 | 3907 | |
cf9a473e AC |
3908 | -- A subprogram body may be instantiated or inlined at a later pass. |
3909 | -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it | |
3910 | -- applied to the initial declaration of the body. | |
3911 | ||
3912 | if Present (Spec_Id) then | |
3913 | if Ignore_SPARK_Mode_Pragmas (Spec_Id) then | |
3914 | Ignore_SPARK_Mode_Pragmas_In_Instance := True; | |
3915 | end if; | |
3916 | ||
3917 | else | |
3918 | -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in | |
3919 | -- case the body is instantiated or inlined later and out of context. | |
3920 | -- The body uses this attribute to restore the value of the global | |
3921 | -- flag. | |
3922 | ||
3923 | if Ignore_SPARK_Mode_Pragmas_In_Instance then | |
3924 | Set_Ignore_SPARK_Mode_Pragmas (Body_Id); | |
3925 | ||
3926 | elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then | |
3927 | Ignore_SPARK_Mode_Pragmas_In_Instance := True; | |
3928 | end if; | |
3929 | end if; | |
3930 | ||
996ae0b0 RK |
3931 | -- If this is the proper body of a stub, we must verify that the stub |
3932 | -- conforms to the body, and to the previous spec if one was present. | |
dbe36d67 | 3933 | -- We know already that the body conforms to that spec. This test is |
996ae0b0 RK |
3934 | -- only required for subprograms that come from source. |
3935 | ||
3936 | if Nkind (Parent (N)) = N_Subunit | |
3937 | and then Comes_From_Source (N) | |
3938 | and then not Error_Posted (Body_Id) | |
e895b435 ES |
3939 | and then Nkind (Corresponding_Stub (Parent (N))) = |
3940 | N_Subprogram_Body_Stub | |
996ae0b0 RK |
3941 | then |
3942 | declare | |
fbf5a39b AC |
3943 | Old_Id : constant Entity_Id := |
3944 | Defining_Entity | |
3945 | (Specification (Corresponding_Stub (Parent (N)))); | |
3946 | ||
996ae0b0 | 3947 | Conformant : Boolean := False; |
996ae0b0 RK |
3948 | |
3949 | begin | |
3950 | if No (Spec_Id) then | |
3951 | Check_Fully_Conformant (Body_Id, Old_Id); | |
3952 | ||
3953 | else | |
3954 | Check_Conformance | |
3955 | (Body_Id, Old_Id, Fully_Conformant, False, Conformant); | |
3956 | ||
3957 | if not Conformant then | |
3958 | ||
dbe36d67 AC |
3959 | -- The stub was taken to be a new declaration. Indicate that |
3960 | -- it lacks a body. | |
996ae0b0 RK |
3961 | |
3962 | Set_Has_Completion (Old_Id, False); | |
3963 | end if; | |
3964 | end if; | |
3965 | end; | |
3966 | end if; | |
3967 | ||
3968 | Set_Has_Completion (Body_Id); | |
3969 | Check_Eliminated (Body_Id); | |
3970 | ||
caf07df9 AC |
3971 | -- Analyze any aspect specifications that appear on the subprogram body |
3972 | -- stub. Stop the analysis now as the stub does not have a declarative | |
3973 | -- or a statement part, and it cannot be inlined. | |
c8a3028c | 3974 | |
caf07df9 | 3975 | if Nkind (N) = N_Subprogram_Body_Stub then |
c8a3028c | 3976 | if Has_Aspects (N) then |
caf07df9 | 3977 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); |
c8a3028c AC |
3978 | end if; |
3979 | ||
d65a80fd | 3980 | goto Leave; |
84f4072a | 3981 | end if; |
996ae0b0 | 3982 | |
9cc6b3f8 | 3983 | -- Handle inlining |
84f4072a | 3984 | |
b94b6c56 RD |
3985 | -- Note: Normally we don't do any inlining if expansion is off, since |
3986 | -- we won't generate code in any case. An exception arises in GNATprove | |
2d180af1 YM |
3987 | -- mode where we want to expand some calls in place, even with expansion |
3988 | -- disabled, since the inlining eases formal verification. | |
ecad37f3 | 3989 | |
6c26bac2 AC |
3990 | if not GNATprove_Mode |
3991 | and then Expander_Active | |
3992 | and then Serious_Errors_Detected = 0 | |
3993 | and then Present (Spec_Id) | |
3994 | and then Has_Pragma_Inline (Spec_Id) | |
3995 | then | |
ca90b962 | 3996 | -- Legacy implementation (relying on front-end inlining) |
84f4072a | 3997 | |
6c26bac2 | 3998 | if not Back_End_Inlining then |
17ce1f52 | 3999 | if (Has_Pragma_Inline_Always (Spec_Id) |
9cc6b3f8 EB |
4000 | and then not Opt.Disable_FE_Inline_Always) |
4001 | or else (Front_End_Inlining | |
4002 | and then not Opt.Disable_FE_Inline) | |
6c26bac2 AC |
4003 | then |
4004 | Build_Body_To_Inline (N, Spec_Id); | |
4005 | end if; | |
f087ea44 | 4006 | |
9cc6b3f8 | 4007 | -- New implementation (relying on back-end inlining) |
f087ea44 | 4008 | |
6c26bac2 AC |
4009 | else |
4010 | if Has_Pragma_Inline_Always (Spec_Id) | |
4011 | or else Optimization_Level > 0 | |
4012 | then | |
4013 | -- Handle function returning an unconstrained type | |
f087ea44 | 4014 | |
6c26bac2 AC |
4015 | if Comes_From_Source (Body_Id) |
4016 | and then Ekind (Spec_Id) = E_Function | |
4017 | and then Returns_Unconstrained_Type (Spec_Id) | |
5c5e108f AC |
4018 | |
4019 | -- If function builds in place, i.e. returns a limited type, | |
4020 | -- inlining cannot be done. | |
4021 | ||
4022 | and then not Is_Limited_Type (Etype (Spec_Id)) | |
6c26bac2 | 4023 | then |
16b10ccc | 4024 | Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id); |
84f4072a | 4025 | |
6c26bac2 AC |
4026 | else |
4027 | declare | |
b80a2b4b AC |
4028 | Subp_Body : constant Node_Id := |
4029 | Unit_Declaration_Node (Body_Id); | |
6c26bac2 | 4030 | Subp_Decl : constant List_Id := Declarations (Subp_Body); |
84f4072a | 4031 | |
6c26bac2 AC |
4032 | begin |
4033 | -- Do not pass inlining to the backend if the subprogram | |
4034 | -- has declarations or statements which cannot be inlined | |
4035 | -- by the backend. This check is done here to emit an | |
4036 | -- error instead of the generic warning message reported | |
4037 | -- by the GCC backend (ie. "function might not be | |
4038 | -- inlinable"). | |
4039 | ||
4040 | if Present (Subp_Decl) | |
4041 | and then Has_Excluded_Declaration (Spec_Id, Subp_Decl) | |
4042 | then | |
4043 | null; | |
4044 | ||
4045 | elsif Has_Excluded_Statement | |
4046 | (Spec_Id, | |
4047 | Statements | |
4048 | (Handled_Statement_Sequence (Subp_Body))) | |
4049 | then | |
4050 | null; | |
4051 | ||
4052 | -- If the backend inlining is available then at this | |
4053 | -- stage we only have to mark the subprogram as inlined. | |
4054 | -- The expander will take care of registering it in the | |
4055 | -- table of subprograms inlined by the backend a part of | |
4056 | -- processing calls to it (cf. Expand_Call) | |
4057 | ||
4058 | else | |
4059 | Set_Is_Inlined (Spec_Id); | |
4060 | end if; | |
4061 | end; | |
4062 | end if; | |
4063 | end if; | |
4064 | end if; | |
2d180af1 YM |
4065 | |
4066 | -- In GNATprove mode, inline only when there is a separate subprogram | |
4067 | -- declaration for now, as inlining of subprogram bodies acting as | |
ca90b962 | 4068 | -- declarations, or subprogram stubs, are not supported by front-end |
2d180af1 | 4069 | -- inlining. This inlining should occur after analysis of the body, so |
d29f68cf AC |
4070 | -- that it is known whether the value of SPARK_Mode, which can be |
4071 | -- defined by a pragma inside the body, is applicable to the body. | |
c1025b4e | 4072 | -- Inlining can be disabled with switch -gnatdm |
2d180af1 YM |
4073 | |
4074 | elsif GNATprove_Mode | |
2d180af1 YM |
4075 | and then Full_Analysis |
4076 | and then not Inside_A_Generic | |
4077 | and then Present (Spec_Id) | |
7c4d86c9 AC |
4078 | and then |
4079 | Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration | |
fd22e260 | 4080 | and then Body_Has_SPARK_Mode_On |
2d180af1 | 4081 | and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id) |
ac072cb2 | 4082 | and then not Body_Has_Contract |
c1025b4e | 4083 | and then not Debug_Flag_M |
2d180af1 | 4084 | then |
6c26bac2 | 4085 | Build_Body_To_Inline (N, Spec_Id); |
996ae0b0 RK |
4086 | end if; |
4087 | ||
0d6014fa | 4088 | -- When generating code, inherited pre/postconditions are handled when |
0f6251c7 | 4089 | -- expanding the corresponding contract. |
539ca5ec | 4090 | |
0ab80019 | 4091 | -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis |
dbe36d67 | 4092 | -- of the specification we have to install the private withed units. |
21d27997 | 4093 | -- This holds for child units as well. |
9bc856dd AC |
4094 | |
4095 | if Is_Compilation_Unit (Body_Id) | |
21d27997 | 4096 | or else Nkind (Parent (N)) = N_Compilation_Unit |
9bc856dd AC |
4097 | then |
4098 | Install_Private_With_Clauses (Body_Id); | |
4099 | end if; | |
4100 | ||
ec4867fa ES |
4101 | Check_Anonymous_Return; |
4102 | ||
fdce4bb7 JM |
4103 | -- Set the Protected_Formal field of each extra formal of the protected |
4104 | -- subprogram to reference the corresponding extra formal of the | |
4105 | -- subprogram that implements it. For regular formals this occurs when | |
4106 | -- the protected subprogram's declaration is expanded, but the extra | |
4107 | -- formals don't get created until the subprogram is frozen. We need to | |
4108 | -- do this before analyzing the protected subprogram's body so that any | |
4109 | -- references to the original subprogram's extra formals will be changed | |
4110 | -- refer to the implementing subprogram's formals (see Expand_Formal). | |
4111 | ||
4112 | if Present (Spec_Id) | |
4113 | and then Is_Protected_Type (Scope (Spec_Id)) | |
4114 | and then Present (Protected_Body_Subprogram (Spec_Id)) | |
4115 | then | |
4116 | declare | |
4117 | Impl_Subp : constant Entity_Id := | |
4118 | Protected_Body_Subprogram (Spec_Id); | |
4119 | Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id); | |
4120 | Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp); | |
cf9a473e | 4121 | |
fdce4bb7 JM |
4122 | begin |
4123 | while Present (Prot_Ext_Formal) loop | |
4124 | pragma Assert (Present (Impl_Ext_Formal)); | |
fdce4bb7 | 4125 | Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal); |
fdce4bb7 JM |
4126 | Next_Formal_With_Extras (Prot_Ext_Formal); |
4127 | Next_Formal_With_Extras (Impl_Ext_Formal); | |
4128 | end loop; | |
4129 | end; | |
4130 | end if; | |
4131 | ||
0868e09c | 4132 | -- Now we can go on to analyze the body |
996ae0b0 RK |
4133 | |
4134 | HSS := Handled_Statement_Sequence (N); | |
4135 | Set_Actual_Subtypes (N, Current_Scope); | |
21d27997 | 4136 | |
f3d0f304 | 4137 | -- Add a declaration for the Protection object, renaming declarations |
21d27997 RD |
4138 | -- for discriminals and privals and finally a declaration for the entry |
4139 | -- family index (if applicable). This form of early expansion is done | |
4140 | -- when the Expander is active because Install_Private_Data_Declarations | |
81bf2382 | 4141 | -- references entities which were created during regular expansion. The |
3b8056a5 AC |
4142 | -- subprogram entity must come from source, and not be an internally |
4143 | -- generated subprogram. | |
21d27997 | 4144 | |
4460a9bc | 4145 | if Expander_Active |
21d27997 RD |
4146 | and then Present (Prot_Typ) |
4147 | and then Present (Spec_Id) | |
3b8056a5 | 4148 | and then Comes_From_Source (Spec_Id) |
21d27997 RD |
4149 | and then not Is_Eliminated (Spec_Id) |
4150 | then | |
4151 | Install_Private_Data_Declarations | |
4152 | (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N)); | |
4153 | end if; | |
4154 | ||
5dcab3ca AC |
4155 | -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context |
4156 | -- may now appear in parameter and result profiles. Since the analysis | |
4157 | -- of a subprogram body may use the parameter and result profile of the | |
4158 | -- spec, swap any limited views with their non-limited counterpart. | |
4159 | ||
128a98ea | 4160 | if Ada_Version >= Ada_2012 and then Present (Spec_Id) then |
1e55d29a | 4161 | Exch_Views := Exchange_Limited_Views (Spec_Id); |
5dcab3ca AC |
4162 | end if; |
4163 | ||
128a98ea EB |
4164 | -- If the return type is an anonymous access type whose designated type |
4165 | -- is the limited view of a class-wide type and the non-limited view is | |
4166 | -- available, update the return type accordingly. | |
4167 | ||
4168 | if Ada_Version >= Ada_2005 and then Present (Spec_Id) then | |
4169 | declare | |
4170 | Etyp : Entity_Id; | |
4171 | Rtyp : Entity_Id; | |
4172 | ||
4173 | begin | |
4174 | Rtyp := Etype (Spec_Id); | |
4175 | ||
4176 | if Ekind (Rtyp) = E_Anonymous_Access_Type then | |
4177 | Etyp := Directly_Designated_Type (Rtyp); | |
4178 | ||
4179 | if Is_Class_Wide_Type (Etyp) | |
4180 | and then From_Limited_With (Etyp) | |
4181 | then | |
4182 | Desig_View := Etyp; | |
4183 | Set_Directly_Designated_Type (Rtyp, Available_View (Etyp)); | |
4184 | end if; | |
4185 | end if; | |
4186 | end; | |
4187 | end if; | |
4188 | ||
c8a3028c AC |
4189 | -- Analyze any aspect specifications that appear on the subprogram body |
4190 | ||
4191 | if Has_Aspects (N) then | |
caf07df9 | 4192 | Analyze_Aspect_Specifications_On_Body_Or_Stub (N); |
c8a3028c | 4193 | end if; |
378dc6ca | 4194 | |
996ae0b0 | 4195 | Analyze_Declarations (Declarations (N)); |
21d27997 | 4196 | |
f3124d8f | 4197 | -- Verify that the SPARK_Mode of the body agrees with that of its spec |
f90d14ac | 4198 | |
f1c7be38 | 4199 | if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then |
f90d14ac | 4200 | if Present (SPARK_Pragma (Spec_Id)) then |
933aa0ac | 4201 | if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off |
f90d14ac | 4202 | and then |
933aa0ac | 4203 | Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On |
f90d14ac AC |
4204 | then |
4205 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
4206 | Error_Msg_N ("incorrect application of SPARK_Mode#", N); | |
4207 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id)); | |
4208 | Error_Msg_NE | |
f3124d8f | 4209 | ("\value Off was set for SPARK_Mode on & #", N, Spec_Id); |
f90d14ac AC |
4210 | end if; |
4211 | ||
4212 | elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then | |
4213 | null; | |
4214 | ||
4215 | else | |
4216 | Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id)); | |
3ccedacc | 4217 | Error_Msg_N ("incorrect application of SPARK_Mode #", N); |
f90d14ac | 4218 | Error_Msg_Sloc := Sloc (Spec_Id); |
f3124d8f HK |
4219 | Error_Msg_NE |
4220 | ("\no value was set for SPARK_Mode on & #", N, Spec_Id); | |
f90d14ac AC |
4221 | end if; |
4222 | end if; | |
4223 | ||
879ac954 AC |
4224 | -- A subprogram body "freezes" its own contract. Analyze the contract |
4225 | -- after the declarations of the body have been processed as pragmas | |
4226 | -- are now chained on the contract of the subprogram body. | |
c9d70ab1 | 4227 | |
f99ff327 | 4228 | Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id); |
c9d70ab1 | 4229 | |
21d27997 RD |
4230 | -- Check completion, and analyze the statements |
4231 | ||
996ae0b0 | 4232 | Check_Completion; |
33931112 | 4233 | Inspect_Deferred_Constant_Completion (Declarations (N)); |
996ae0b0 | 4234 | Analyze (HSS); |
21d27997 RD |
4235 | |
4236 | -- Deal with end of scope processing for the body | |
4237 | ||
07fc65c4 | 4238 | Process_End_Label (HSS, 't', Current_Scope); |
996ae0b0 | 4239 | End_Scope; |
5e127570 AC |
4240 | |
4241 | -- If we are compiling an entry wrapper, remove the enclosing | |
ffa168bc | 4242 | -- synchronized object from the stack. |
5e127570 AC |
4243 | |
4244 | if Is_Entry_Wrapper (Body_Id) then | |
4245 | End_Scope; | |
4246 | end if; | |
4247 | ||
996ae0b0 | 4248 | Check_Subprogram_Order (N); |
c37bb106 | 4249 | Set_Analyzed (Body_Id); |
996ae0b0 RK |
4250 | |
4251 | -- If we have a separate spec, then the analysis of the declarations | |
4252 | -- caused the entities in the body to be chained to the spec id, but | |
4253 | -- we want them chained to the body id. Only the formal parameters | |
4254 | -- end up chained to the spec id in this case. | |
4255 | ||
4256 | if Present (Spec_Id) then | |
4257 | ||
d39d6bb8 | 4258 | -- We must conform to the categorization of our spec |
996ae0b0 | 4259 | |
d39d6bb8 | 4260 | Validate_Categorization_Dependency (N, Spec_Id); |
996ae0b0 | 4261 | |
d39d6bb8 RD |
4262 | -- And if this is a child unit, the parent units must conform |
4263 | ||
4264 | if Is_Child_Unit (Spec_Id) then | |
996ae0b0 RK |
4265 | Validate_Categorization_Dependency |
4266 | (Unit_Declaration_Node (Spec_Id), Spec_Id); | |
4267 | end if; | |
4268 | ||
21d27997 RD |
4269 | -- Here is where we move entities from the spec to the body |
4270 | ||
4271 | -- Case where there are entities that stay with the spec | |
4272 | ||
4273 | if Present (Last_Real_Spec_Entity) then | |
4274 | ||
dbe36d67 AC |
4275 | -- No body entities (happens when the only real spec entities come |
4276 | -- from precondition and postcondition pragmas). | |
21d27997 RD |
4277 | |
4278 | if No (Last_Entity (Body_Id)) then | |
7c4d86c9 | 4279 | Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity)); |
21d27997 RD |
4280 | |
4281 | -- Body entities present (formals), so chain stuff past them | |
4282 | ||
4283 | else | |
4284 | Set_Next_Entity | |
4285 | (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity)); | |
4286 | end if; | |
4287 | ||
4288 | Set_Next_Entity (Last_Real_Spec_Entity, Empty); | |
996ae0b0 | 4289 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); |
21d27997 RD |
4290 | Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity); |
4291 | ||
dbe36d67 AC |
4292 | -- Case where there are no spec entities, in this case there can be |
4293 | -- no body entities either, so just move everything. | |
996ae0b0 | 4294 | |
a921e83c AC |
4295 | -- If the body is generated for an expression function, it may have |
4296 | -- been preanalyzed already, if 'access was applied to it. | |
4297 | ||
996ae0b0 | 4298 | else |
a921e83c AC |
4299 | if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /= |
4300 | N_Expression_Function | |
4301 | then | |
4302 | pragma Assert (No (Last_Entity (Body_Id))); | |
4303 | null; | |
4304 | end if; | |
4305 | ||
996ae0b0 RK |
4306 | Set_First_Entity (Body_Id, First_Entity (Spec_Id)); |
4307 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); | |
4308 | Set_First_Entity (Spec_Id, Empty); | |
4309 | Set_Last_Entity (Spec_Id, Empty); | |
4310 | end if; | |
4311 | end if; | |
4312 | ||
7665e4bd | 4313 | Check_Missing_Return; |
996ae0b0 | 4314 | |
82c80734 | 4315 | -- Now we are going to check for variables that are never modified in |
76a69663 ES |
4316 | -- the body of the procedure. But first we deal with a special case |
4317 | -- where we want to modify this check. If the body of the subprogram | |
4318 | -- starts with a raise statement or its equivalent, or if the body | |
cf3e6845 AC |
4319 | -- consists entirely of a null statement, then it is pretty obvious that |
4320 | -- it is OK to not reference the parameters. For example, this might be | |
4321 | -- the following common idiom for a stubbed function: statement of the | |
4322 | -- procedure raises an exception. In particular this deals with the | |
4323 | -- common idiom of a stubbed function, which appears something like: | |
fbf5a39b AC |
4324 | |
4325 | -- function F (A : Integer) return Some_Type; | |
4326 | -- X : Some_Type; | |
4327 | -- begin | |
4328 | -- raise Program_Error; | |
4329 | -- return X; | |
4330 | -- end F; | |
4331 | ||
76a69663 ES |
4332 | -- Here the purpose of X is simply to satisfy the annoying requirement |
4333 | -- in Ada that there be at least one return, and we certainly do not | |
a90bd866 | 4334 | -- want to go posting warnings on X that it is not initialized. On |
76a69663 ES |
4335 | -- the other hand, if X is entirely unreferenced that should still |
4336 | -- get a warning. | |
4337 | ||
4338 | -- What we do is to detect these cases, and if we find them, flag the | |
4339 | -- subprogram as being Is_Trivial_Subprogram and then use that flag to | |
4340 | -- suppress unwanted warnings. For the case of the function stub above | |
4341 | -- we have a special test to set X as apparently assigned to suppress | |
4342 | -- the warning. | |
996ae0b0 RK |
4343 | |
4344 | declare | |
800621e0 | 4345 | Stm : Node_Id; |
996ae0b0 RK |
4346 | |
4347 | begin | |
0a36105d | 4348 | -- Skip initial labels (for one thing this occurs when we are in |
ca90b962 | 4349 | -- front-end ZCX mode, but in any case it is irrelevant), and also |
0a36105d | 4350 | -- initial Push_xxx_Error_Label nodes, which are also irrelevant. |
fbf5a39b | 4351 | |
800621e0 | 4352 | Stm := First (Statements (HSS)); |
0a36105d JM |
4353 | while Nkind (Stm) = N_Label |
4354 | or else Nkind (Stm) in N_Push_xxx_Label | |
4355 | loop | |
996ae0b0 | 4356 | Next (Stm); |
0a36105d | 4357 | end loop; |
996ae0b0 | 4358 | |
fbf5a39b AC |
4359 | -- Do the test on the original statement before expansion |
4360 | ||
4361 | declare | |
4362 | Ostm : constant Node_Id := Original_Node (Stm); | |
4363 | ||
4364 | begin | |
76a69663 | 4365 | -- If explicit raise statement, turn on flag |
fbf5a39b AC |
4366 | |
4367 | if Nkind (Ostm) = N_Raise_Statement then | |
76a69663 ES |
4368 | Set_Trivial_Subprogram (Stm); |
4369 | ||
f3d57416 | 4370 | -- If null statement, and no following statements, turn on flag |
76a69663 ES |
4371 | |
4372 | elsif Nkind (Stm) = N_Null_Statement | |
4373 | and then Comes_From_Source (Stm) | |
4374 | and then No (Next (Stm)) | |
4375 | then | |
4376 | Set_Trivial_Subprogram (Stm); | |
fbf5a39b AC |
4377 | |
4378 | -- Check for explicit call cases which likely raise an exception | |
4379 | ||
4380 | elsif Nkind (Ostm) = N_Procedure_Call_Statement then | |
4381 | if Is_Entity_Name (Name (Ostm)) then | |
4382 | declare | |
4383 | Ent : constant Entity_Id := Entity (Name (Ostm)); | |
4384 | ||
4385 | begin | |
4386 | -- If the procedure is marked No_Return, then likely it | |
4387 | -- raises an exception, but in any case it is not coming | |
76a69663 | 4388 | -- back here, so turn on the flag. |
fbf5a39b | 4389 | |
f46faa08 AC |
4390 | if Present (Ent) |
4391 | and then Ekind (Ent) = E_Procedure | |
fbf5a39b AC |
4392 | and then No_Return (Ent) |
4393 | then | |
76a69663 | 4394 | Set_Trivial_Subprogram (Stm); |
fbf5a39b AC |
4395 | end if; |
4396 | end; | |
4397 | end if; | |
4398 | end if; | |
4399 | end; | |
996ae0b0 RK |
4400 | end; |
4401 | ||
4402 | -- Check for variables that are never modified | |
4403 | ||
4404 | declare | |
d65a80fd HK |
4405 | E1 : Entity_Id; |
4406 | E2 : Entity_Id; | |
996ae0b0 RK |
4407 | |
4408 | begin | |
fbf5a39b | 4409 | -- If there is a separate spec, then transfer Never_Set_In_Source |
996ae0b0 RK |
4410 | -- flags from out parameters to the corresponding entities in the |
4411 | -- body. The reason we do that is we want to post error flags on | |
4412 | -- the body entities, not the spec entities. | |
4413 | ||
4414 | if Present (Spec_Id) then | |
4415 | E1 := First_Entity (Spec_Id); | |
996ae0b0 RK |
4416 | while Present (E1) loop |
4417 | if Ekind (E1) = E_Out_Parameter then | |
4418 | E2 := First_Entity (Body_Id); | |
fbf5a39b | 4419 | while Present (E2) loop |
996ae0b0 RK |
4420 | exit when Chars (E1) = Chars (E2); |
4421 | Next_Entity (E2); | |
4422 | end loop; | |
4423 | ||
fbf5a39b AC |
4424 | if Present (E2) then |
4425 | Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1)); | |
4426 | end if; | |
996ae0b0 RK |
4427 | end if; |
4428 | ||
4429 | Next_Entity (E1); | |
4430 | end loop; | |
4431 | end if; | |
4432 | ||
2aca76d6 | 4433 | -- Check references in body |
0868e09c | 4434 | |
2aca76d6 | 4435 | Check_References (Body_Id); |
996ae0b0 | 4436 | end; |
5a271a7f RD |
4437 | |
4438 | -- Check for nested subprogram, and mark outer level subprogram if so | |
4439 | ||
4440 | declare | |
4441 | Ent : Entity_Id; | |
4442 | ||
4443 | begin | |
4444 | if Present (Spec_Id) then | |
4445 | Ent := Spec_Id; | |
4446 | else | |
4447 | Ent := Body_Id; | |
4448 | end if; | |
4449 | ||
4450 | loop | |
4451 | Ent := Enclosing_Subprogram (Ent); | |
4452 | exit when No (Ent) or else Is_Subprogram (Ent); | |
4453 | end loop; | |
4454 | ||
4455 | if Present (Ent) then | |
4456 | Set_Has_Nested_Subprogram (Ent); | |
4457 | end if; | |
4458 | end; | |
241ebe89 | 4459 | |
1e55d29a EB |
4460 | -- Restore the limited views in the spec, if any, to let the back end |
4461 | -- process it without running into circularities. | |
4462 | ||
4463 | if Exch_Views /= No_Elist then | |
4464 | Restore_Limited_Views (Exch_Views); | |
4465 | end if; | |
4466 | ||
128a98ea EB |
4467 | if Present (Desig_View) then |
4468 | Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View); | |
4469 | end if; | |
4470 | ||
d65a80fd | 4471 | <<Leave>> |
f9a8f910 HK |
4472 | Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP; |
4473 | Restore_Ghost_Mode (Saved_GM); | |
b1b543d2 | 4474 | end Analyze_Subprogram_Body_Helper; |
996ae0b0 RK |
4475 | |
4476 | ------------------------------------ | |
4477 | -- Analyze_Subprogram_Declaration -- | |
4478 | ------------------------------------ | |
4479 | ||
4480 | procedure Analyze_Subprogram_Declaration (N : Node_Id) is | |
579847c2 | 4481 | Scop : constant Entity_Id := Current_Scope; |
5d5832bc | 4482 | Designator : Entity_Id; |
579847c2 | 4483 | |
4d8f3296 ES |
4484 | Is_Completion : Boolean; |
4485 | -- Indicates whether a null procedure declaration is a completion | |
996ae0b0 RK |
4486 | |
4487 | begin | |
2ba431e5 | 4488 | -- Null procedures are not allowed in SPARK |
daec8eeb | 4489 | |
fe5d3068 | 4490 | if Nkind (Specification (N)) = N_Procedure_Specification |
daec8eeb YM |
4491 | and then Null_Present (Specification (N)) |
4492 | then | |
ce5ba43a | 4493 | Check_SPARK_05_Restriction ("null procedure is not allowed", N); |
718deaf1 | 4494 | |
73cc8f62 RD |
4495 | -- Null procedures are allowed in protected types, following the |
4496 | -- recent AI12-0147. | |
b741083a ES |
4497 | |
4498 | if Is_Protected_Type (Current_Scope) | |
4499 | and then Ada_Version < Ada_2012 | |
4500 | then | |
4d8f3296 ES |
4501 | Error_Msg_N ("protected operation cannot be a null procedure", N); |
4502 | end if; | |
718deaf1 | 4503 | |
4d8f3296 | 4504 | Analyze_Null_Procedure (N, Is_Completion); |
718deaf1 | 4505 | |
241ebe89 | 4506 | -- The null procedure acts as a body, nothing further is needed |
5d5832bc | 4507 | |
241ebe89 | 4508 | if Is_Completion then |
4d8f3296 | 4509 | return; |
5d5832bc AC |
4510 | end if; |
4511 | end if; | |
4512 | ||
beacce02 | 4513 | Designator := Analyze_Subprogram_Specification (Specification (N)); |
31af8899 AC |
4514 | |
4515 | -- A reference may already have been generated for the unit name, in | |
4516 | -- which case the following call is redundant. However it is needed for | |
4517 | -- declarations that are the rewriting of an expression function. | |
4518 | ||
5d5832bc AC |
4519 | Generate_Definition (Designator); |
4520 | ||
877a5a12 AC |
4521 | -- Set the SPARK mode from the current context (may be overwritten later |
4522 | -- with explicit pragma). This is not done for entry barrier functions | |
4523 | -- because they are generated outside the protected type and should not | |
4524 | -- carry the mode of the enclosing context. | |
4a854847 | 4525 | |
877a5a12 AC |
4526 | if Nkind (N) = N_Subprogram_Declaration |
4527 | and then Is_Entry_Barrier_Function (N) | |
4528 | then | |
4529 | null; | |
4530 | else | |
4531 | Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma); | |
4532 | Set_SPARK_Pragma_Inherited (Designator); | |
4533 | end if; | |
579847c2 | 4534 | |
cf9a473e AC |
4535 | -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case |
4536 | -- the body of this subprogram is instantiated or inlined later and out | |
4537 | -- of context. The body uses this attribute to restore the value of the | |
4538 | -- global flag. | |
4539 | ||
4540 | if Ignore_SPARK_Mode_Pragmas_In_Instance then | |
4541 | Set_Ignore_SPARK_Mode_Pragmas (Designator); | |
4542 | end if; | |
4543 | ||
b1b543d2 BD |
4544 | if Debug_Flag_C then |
4545 | Write_Str ("==> subprogram spec "); | |
4546 | Write_Name (Chars (Designator)); | |
4547 | Write_Str (" from "); | |
4548 | Write_Location (Sloc (N)); | |
4549 | Write_Eol; | |
4550 | Indent; | |
4551 | end if; | |
4552 | ||
996ae0b0 | 4553 | Validate_RCI_Subprogram_Declaration (N); |
996ae0b0 RK |
4554 | New_Overloaded_Entity (Designator); |
4555 | Check_Delayed_Subprogram (Designator); | |
fbf5a39b | 4556 | |
cf3e6845 AC |
4557 | -- If the type of the first formal of the current subprogram is a non- |
4558 | -- generic tagged private type, mark the subprogram as being a private | |
4559 | -- primitive. Ditto if this is a function with controlling result, and | |
4560 | -- the return type is currently private. In both cases, the type of the | |
4561 | -- controlling argument or result must be in the current scope for the | |
4562 | -- operation to be primitive. | |
6ca063eb AC |
4563 | |
4564 | if Has_Controlling_Result (Designator) | |
4565 | and then Is_Private_Type (Etype (Designator)) | |
b7d5e87b | 4566 | and then Scope (Etype (Designator)) = Current_Scope |
6ca063eb AC |
4567 | and then not Is_Generic_Actual_Type (Etype (Designator)) |
4568 | then | |
4569 | Set_Is_Private_Primitive (Designator); | |
d44202ba | 4570 | |
6ca063eb | 4571 | elsif Present (First_Formal (Designator)) then |
d44202ba HK |
4572 | declare |
4573 | Formal_Typ : constant Entity_Id := | |
4574 | Etype (First_Formal (Designator)); | |
4575 | begin | |
4576 | Set_Is_Private_Primitive (Designator, | |
4577 | Is_Tagged_Type (Formal_Typ) | |
b7d5e87b | 4578 | and then Scope (Formal_Typ) = Current_Scope |
d44202ba HK |
4579 | and then Is_Private_Type (Formal_Typ) |
4580 | and then not Is_Generic_Actual_Type (Formal_Typ)); | |
4581 | end; | |
4582 | end if; | |
4583 | ||
ec4867fa ES |
4584 | -- Ada 2005 (AI-251): Abstract interface primitives must be abstract |
4585 | -- or null. | |
4586 | ||
0791fbe9 | 4587 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
4588 | and then Comes_From_Source (N) |
4589 | and then Is_Dispatching_Operation (Designator) | |
4590 | then | |
4591 | declare | |
4592 | E : Entity_Id; | |
4593 | Etyp : Entity_Id; | |
4594 | ||
4595 | begin | |
4596 | if Has_Controlling_Result (Designator) then | |
4597 | Etyp := Etype (Designator); | |
4598 | ||
4599 | else | |
4600 | E := First_Entity (Designator); | |
4601 | while Present (E) | |
4602 | and then Is_Formal (E) | |
4603 | and then not Is_Controlling_Formal (E) | |
4604 | loop | |
4605 | Next_Entity (E); | |
4606 | end loop; | |
4607 | ||
4608 | Etyp := Etype (E); | |
4609 | end if; | |
4610 | ||
4611 | if Is_Access_Type (Etyp) then | |
4612 | Etyp := Directly_Designated_Type (Etyp); | |
4613 | end if; | |
4614 | ||
4615 | if Is_Interface (Etyp) | |
f937473f | 4616 | and then not Is_Abstract_Subprogram (Designator) |
ec4867fa | 4617 | and then not (Ekind (Designator) = E_Procedure |
8fde064e | 4618 | and then Null_Present (Specification (N))) |
ec4867fa ES |
4619 | then |
4620 | Error_Msg_Name_1 := Chars (Defining_Entity (N)); | |
033eaf85 AC |
4621 | |
4622 | -- Specialize error message based on procedures vs. functions, | |
4623 | -- since functions can't be null subprograms. | |
4624 | ||
4625 | if Ekind (Designator) = E_Procedure then | |
4626 | Error_Msg_N | |
4627 | ("interface procedure % must be abstract or null", N); | |
4628 | else | |
3f80a182 AC |
4629 | Error_Msg_N |
4630 | ("interface function % must be abstract", N); | |
033eaf85 | 4631 | end if; |
ec4867fa ES |
4632 | end if; |
4633 | end; | |
4634 | end if; | |
4635 | ||
fbf5a39b AC |
4636 | -- What is the following code for, it used to be |
4637 | ||
4638 | -- ??? Set_Suppress_Elaboration_Checks | |
4639 | -- ??? (Designator, Elaboration_Checks_Suppressed (Designator)); | |
4640 | ||
4641 | -- The following seems equivalent, but a bit dubious | |
4642 | ||
4643 | if Elaboration_Checks_Suppressed (Designator) then | |
4644 | Set_Kill_Elaboration_Checks (Designator); | |
4645 | end if; | |
996ae0b0 | 4646 | |
8fde064e | 4647 | if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then |
fbf5a39b | 4648 | Set_Categorization_From_Scope (Designator, Scop); |
8fde064e | 4649 | |
996ae0b0 | 4650 | else |
e895b435 | 4651 | -- For a compilation unit, check for library-unit pragmas |
996ae0b0 | 4652 | |
0a36105d | 4653 | Push_Scope (Designator); |
996ae0b0 RK |
4654 | Set_Categorization_From_Pragmas (N); |
4655 | Validate_Categorization_Dependency (N, Designator); | |
4656 | Pop_Scope; | |
4657 | end if; | |
4658 | ||
4659 | -- For a compilation unit, set body required. This flag will only be | |
4660 | -- reset if a valid Import or Interface pragma is processed later on. | |
4661 | ||
4662 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
4663 | Set_Body_Required (Parent (N), True); | |
758c442c | 4664 | |
0791fbe9 | 4665 | if Ada_Version >= Ada_2005 |
758c442c GD |
4666 | and then Nkind (Specification (N)) = N_Procedure_Specification |
4667 | and then Null_Present (Specification (N)) | |
4668 | then | |
4669 | Error_Msg_N | |
4670 | ("null procedure cannot be declared at library level", N); | |
4671 | end if; | |
996ae0b0 RK |
4672 | end if; |
4673 | ||
fbf5a39b | 4674 | Generate_Reference_To_Formals (Designator); |
996ae0b0 | 4675 | Check_Eliminated (Designator); |
fbf5a39b | 4676 | |
b1b543d2 BD |
4677 | if Debug_Flag_C then |
4678 | Outdent; | |
4679 | Write_Str ("<== subprogram spec "); | |
4680 | Write_Name (Chars (Designator)); | |
4681 | Write_Str (" from "); | |
4682 | Write_Location (Sloc (N)); | |
4683 | Write_Eol; | |
4684 | end if; | |
0f1a6a0b | 4685 | |
1a265e78 AC |
4686 | if Is_Protected_Type (Current_Scope) then |
4687 | ||
4688 | -- Indicate that this is a protected operation, because it may be | |
4689 | -- used in subsequent declarations within the protected type. | |
4690 | ||
4691 | Set_Convention (Designator, Convention_Protected); | |
4692 | end if; | |
4693 | ||
beacce02 | 4694 | List_Inherited_Pre_Post_Aspects (Designator); |
eaba57fb RD |
4695 | |
4696 | if Has_Aspects (N) then | |
4697 | Analyze_Aspect_Specifications (N, Designator); | |
4698 | end if; | |
996ae0b0 RK |
4699 | end Analyze_Subprogram_Declaration; |
4700 | ||
fbf5a39b AC |
4701 | -------------------------------------- |
4702 | -- Analyze_Subprogram_Specification -- | |
4703 | -------------------------------------- | |
4704 | ||
4705 | -- Reminder: N here really is a subprogram specification (not a subprogram | |
4706 | -- declaration). This procedure is called to analyze the specification in | |
4707 | -- both subprogram bodies and subprogram declarations (specs). | |
4708 | ||
4709 | function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is | |
3ddfabe3 AC |
4710 | function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean; |
4711 | -- Determine whether entity E denotes the spec or body of an invariant | |
4712 | -- procedure. | |
4713 | ||
4714 | ------------------------------------ | |
4715 | -- Is_Invariant_Procedure_Or_Body -- | |
4716 | ------------------------------------ | |
4717 | ||
4718 | function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is | |
4719 | Decl : constant Node_Id := Unit_Declaration_Node (E); | |
4720 | Spec : Entity_Id; | |
4721 | ||
4722 | begin | |
4723 | if Nkind (Decl) = N_Subprogram_Body then | |
4724 | Spec := Corresponding_Spec (Decl); | |
4725 | else | |
4726 | Spec := E; | |
4727 | end if; | |
4728 | ||
4729 | return | |
4730 | Present (Spec) | |
4731 | and then Ekind (Spec) = E_Procedure | |
4732 | and then (Is_Partial_Invariant_Procedure (Spec) | |
4733 | or else Is_Invariant_Procedure (Spec)); | |
4734 | end Is_Invariant_Procedure_Or_Body; | |
4735 | ||
4736 | -- Local variables | |
4737 | ||
fbf5a39b | 4738 | Designator : constant Entity_Id := Defining_Entity (N); |
21d27997 | 4739 | Formals : constant List_Id := Parameter_Specifications (N); |
fbf5a39b | 4740 | |
758c442c GD |
4741 | -- Start of processing for Analyze_Subprogram_Specification |
4742 | ||
fbf5a39b | 4743 | begin |
2ba431e5 | 4744 | -- User-defined operator is not allowed in SPARK, except as a renaming |
38171f43 | 4745 | |
db72f10a AC |
4746 | if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol |
4747 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
4748 | then | |
ce5ba43a AC |
4749 | Check_SPARK_05_Restriction |
4750 | ("user-defined operator is not allowed", N); | |
38171f43 AC |
4751 | end if; |
4752 | ||
31af8899 AC |
4753 | -- Proceed with analysis. Do not emit a cross-reference entry if the |
4754 | -- specification comes from an expression function, because it may be | |
026c3cfd | 4755 | -- the completion of a previous declaration. It is not, the cross- |
31af8899 AC |
4756 | -- reference entry will be emitted for the new subprogram declaration. |
4757 | ||
4758 | if Nkind (Parent (N)) /= N_Expression_Function then | |
4759 | Generate_Definition (Designator); | |
4760 | end if; | |
38171f43 | 4761 | |
fbf5a39b AC |
4762 | if Nkind (N) = N_Function_Specification then |
4763 | Set_Ekind (Designator, E_Function); | |
4764 | Set_Mechanism (Designator, Default_Mechanism); | |
fbf5a39b AC |
4765 | else |
4766 | Set_Ekind (Designator, E_Procedure); | |
4767 | Set_Etype (Designator, Standard_Void_Type); | |
4768 | end if; | |
4769 | ||
4bd4bb7f AC |
4770 | -- Flag Is_Inlined_Always is True by default, and reversed to False for |
4771 | -- those subprograms which could be inlined in GNATprove mode (because | |
319c6161 | 4772 | -- Body_To_Inline is non-Empty) but should not be inlined. |
4bd4bb7f AC |
4773 | |
4774 | if GNATprove_Mode then | |
4775 | Set_Is_Inlined_Always (Designator); | |
4776 | end if; | |
4777 | ||
800621e0 | 4778 | -- Introduce new scope for analysis of the formals and the return type |
82c80734 RD |
4779 | |
4780 | Set_Scope (Designator, Current_Scope); | |
4781 | ||
fbf5a39b | 4782 | if Present (Formals) then |
0a36105d | 4783 | Push_Scope (Designator); |
fbf5a39b | 4784 | Process_Formals (Formals, N); |
758c442c | 4785 | |
0929eaeb AC |
4786 | -- Check dimensions in N for formals with default expression |
4787 | ||
4788 | Analyze_Dimension_Formals (N, Formals); | |
4789 | ||
a38ff9b1 ES |
4790 | -- Ada 2005 (AI-345): If this is an overriding operation of an |
4791 | -- inherited interface operation, and the controlling type is | |
4792 | -- a synchronized type, replace the type with its corresponding | |
4793 | -- record, to match the proper signature of an overriding operation. | |
69cb258c AC |
4794 | -- Same processing for an access parameter whose designated type is |
4795 | -- derived from a synchronized interface. | |
758c442c | 4796 | |
3ddfabe3 AC |
4797 | -- This modification is not done for invariant procedures because |
4798 | -- the corresponding record may not necessarely be visible when the | |
4799 | -- concurrent type acts as the full view of a private type. | |
4800 | ||
4801 | -- package Pack is | |
4802 | -- type Prot is private with Type_Invariant => ...; | |
4803 | -- procedure ConcInvariant (Obj : Prot); | |
4804 | -- private | |
4805 | -- protected type Prot is ...; | |
4806 | -- type Concurrent_Record_Prot is record ...; | |
4807 | -- procedure ConcInvariant (Obj : Prot) is | |
4808 | -- ... | |
4809 | -- end ConcInvariant; | |
4810 | -- end Pack; | |
4811 | ||
4812 | -- In the example above, both the spec and body of the invariant | |
4813 | -- procedure must utilize the private type as the controlling type. | |
4814 | ||
4815 | if Ada_Version >= Ada_2005 | |
4816 | and then not Is_Invariant_Procedure_Or_Body (Designator) | |
4817 | then | |
d44202ba HK |
4818 | declare |
4819 | Formal : Entity_Id; | |
4820 | Formal_Typ : Entity_Id; | |
4821 | Rec_Typ : Entity_Id; | |
69cb258c | 4822 | Desig_Typ : Entity_Id; |
0a36105d | 4823 | |
d44202ba HK |
4824 | begin |
4825 | Formal := First_Formal (Designator); | |
4826 | while Present (Formal) loop | |
4827 | Formal_Typ := Etype (Formal); | |
0a36105d | 4828 | |
d44202ba HK |
4829 | if Is_Concurrent_Type (Formal_Typ) |
4830 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
4831 | then | |
4832 | Rec_Typ := Corresponding_Record_Type (Formal_Typ); | |
4833 | ||
4834 | if Present (Interfaces (Rec_Typ)) then | |
4835 | Set_Etype (Formal, Rec_Typ); | |
4836 | end if; | |
69cb258c AC |
4837 | |
4838 | elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then | |
4839 | Desig_Typ := Designated_Type (Formal_Typ); | |
4840 | ||
4841 | if Is_Concurrent_Type (Desig_Typ) | |
4842 | and then Present (Corresponding_Record_Type (Desig_Typ)) | |
4843 | then | |
4844 | Rec_Typ := Corresponding_Record_Type (Desig_Typ); | |
4845 | ||
4846 | if Present (Interfaces (Rec_Typ)) then | |
4847 | Set_Directly_Designated_Type (Formal_Typ, Rec_Typ); | |
4848 | end if; | |
4849 | end if; | |
d44202ba HK |
4850 | end if; |
4851 | ||
4852 | Next_Formal (Formal); | |
4853 | end loop; | |
4854 | end; | |
758c442c GD |
4855 | end if; |
4856 | ||
fbf5a39b | 4857 | End_Scope; |
82c80734 | 4858 | |
b66c3ff4 AC |
4859 | -- The subprogram scope is pushed and popped around the processing of |
4860 | -- the return type for consistency with call above to Process_Formals | |
4861 | -- (which itself can call Analyze_Return_Type), and to ensure that any | |
4862 | -- itype created for the return type will be associated with the proper | |
4863 | -- scope. | |
4864 | ||
82c80734 | 4865 | elsif Nkind (N) = N_Function_Specification then |
b66c3ff4 | 4866 | Push_Scope (Designator); |
82c80734 | 4867 | Analyze_Return_Type (N); |
b66c3ff4 | 4868 | End_Scope; |
fbf5a39b AC |
4869 | end if; |
4870 | ||
e606088a AC |
4871 | -- Function case |
4872 | ||
fbf5a39b | 4873 | if Nkind (N) = N_Function_Specification then |
e606088a AC |
4874 | |
4875 | -- Deal with operator symbol case | |
4876 | ||
fbf5a39b AC |
4877 | if Nkind (Designator) = N_Defining_Operator_Symbol then |
4878 | Valid_Operator_Definition (Designator); | |
4879 | end if; | |
4880 | ||
4881 | May_Need_Actuals (Designator); | |
4882 | ||
fe63b1b1 ES |
4883 | -- Ada 2005 (AI-251): If the return type is abstract, verify that |
4884 | -- the subprogram is abstract also. This does not apply to renaming | |
1adaea16 AC |
4885 | -- declarations, where abstractness is inherited, and to subprogram |
4886 | -- bodies generated for stream operations, which become renamings as | |
4887 | -- bodies. | |
2bfb1b72 | 4888 | |
fe63b1b1 ES |
4889 | -- In case of primitives associated with abstract interface types |
4890 | -- the check is applied later (see Analyze_Subprogram_Declaration). | |
ec4867fa | 4891 | |
1adaea16 | 4892 | if not Nkind_In (Original_Node (Parent (N)), |
3f80a182 | 4893 | N_Abstract_Subprogram_Declaration, |
847d950d HK |
4894 | N_Formal_Abstract_Subprogram_Declaration, |
4895 | N_Subprogram_Renaming_Declaration) | |
fbf5a39b | 4896 | then |
2e79de51 AC |
4897 | if Is_Abstract_Type (Etype (Designator)) |
4898 | and then not Is_Interface (Etype (Designator)) | |
4899 | then | |
4900 | Error_Msg_N | |
4901 | ("function that returns abstract type must be abstract", N); | |
4902 | ||
e606088a | 4903 | -- Ada 2012 (AI-0073): Extend this test to subprograms with an |
2e79de51 AC |
4904 | -- access result whose designated type is abstract. |
4905 | ||
847d950d HK |
4906 | elsif Ada_Version >= Ada_2012 |
4907 | and then Nkind (Result_Definition (N)) = N_Access_Definition | |
2e79de51 AC |
4908 | and then |
4909 | not Is_Class_Wide_Type (Designated_Type (Etype (Designator))) | |
4910 | and then Is_Abstract_Type (Designated_Type (Etype (Designator))) | |
2e79de51 | 4911 | then |
847d950d HK |
4912 | Error_Msg_N |
4913 | ("function whose access result designates abstract type " | |
4914 | & "must be abstract", N); | |
2e79de51 | 4915 | end if; |
fbf5a39b AC |
4916 | end if; |
4917 | end if; | |
4918 | ||
4919 | return Designator; | |
4920 | end Analyze_Subprogram_Specification; | |
4921 | ||
996ae0b0 RK |
4922 | ----------------------- |
4923 | -- Check_Conformance -- | |
4924 | ----------------------- | |
4925 | ||
4926 | procedure Check_Conformance | |
41251c60 JM |
4927 | (New_Id : Entity_Id; |
4928 | Old_Id : Entity_Id; | |
4929 | Ctype : Conformance_Type; | |
4930 | Errmsg : Boolean; | |
4931 | Conforms : out Boolean; | |
4932 | Err_Loc : Node_Id := Empty; | |
4933 | Get_Inst : Boolean := False; | |
4934 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 | 4935 | is |
996ae0b0 | 4936 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id); |
c27f2f15 RD |
4937 | -- Sets Conforms to False. If Errmsg is False, then that's all it does. |
4938 | -- If Errmsg is True, then processing continues to post an error message | |
4939 | -- for conformance error on given node. Two messages are output. The | |
4940 | -- first message points to the previous declaration with a general "no | |
4941 | -- conformance" message. The second is the detailed reason, supplied as | |
4942 | -- Msg. The parameter N provide information for a possible & insertion | |
4943 | -- in the message, and also provides the location for posting the | |
4944 | -- message in the absence of a specified Err_Loc location. | |
996ae0b0 | 4945 | |
f991bd8e HK |
4946 | function Conventions_Match |
4947 | (Id1 : Entity_Id; | |
4948 | Id2 : Entity_Id) return Boolean; | |
4949 | -- Determine whether the conventions of arbitrary entities Id1 and Id2 | |
4950 | -- match. | |
4951 | ||
996ae0b0 RK |
4952 | ----------------------- |
4953 | -- Conformance_Error -- | |
4954 | ----------------------- | |
4955 | ||
4956 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is | |
4957 | Enode : Node_Id; | |
4958 | ||
4959 | begin | |
4960 | Conforms := False; | |
4961 | ||
4962 | if Errmsg then | |
4963 | if No (Err_Loc) then | |
4964 | Enode := N; | |
4965 | else | |
4966 | Enode := Err_Loc; | |
4967 | end if; | |
4968 | ||
4969 | Error_Msg_Sloc := Sloc (Old_Id); | |
4970 | ||
4971 | case Ctype is | |
4972 | when Type_Conformant => | |
483c78cb | 4973 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
4974 | ("not type conformant with declaration#!", Enode); |
4975 | ||
4976 | when Mode_Conformant => | |
19590d70 | 4977 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4978 | Error_Msg_N |
19590d70 GD |
4979 | ("not mode conformant with operation inherited#!", |
4980 | Enode); | |
4981 | else | |
ed2233dc | 4982 | Error_Msg_N |
19590d70 GD |
4983 | ("not mode conformant with declaration#!", Enode); |
4984 | end if; | |
996ae0b0 RK |
4985 | |
4986 | when Subtype_Conformant => | |
19590d70 | 4987 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4988 | Error_Msg_N |
19590d70 GD |
4989 | ("not subtype conformant with operation inherited#!", |
4990 | Enode); | |
4991 | else | |
ed2233dc | 4992 | Error_Msg_N |
19590d70 GD |
4993 | ("not subtype conformant with declaration#!", Enode); |
4994 | end if; | |
996ae0b0 RK |
4995 | |
4996 | when Fully_Conformant => | |
19590d70 | 4997 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
483c78cb | 4998 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4999 | ("not fully conformant with operation inherited#!", |
5000 | Enode); | |
5001 | else | |
483c78cb | 5002 | Error_Msg_N -- CODEFIX |
19590d70 GD |
5003 | ("not fully conformant with declaration#!", Enode); |
5004 | end if; | |
996ae0b0 RK |
5005 | end case; |
5006 | ||
5007 | Error_Msg_NE (Msg, Enode, N); | |
5008 | end if; | |
5009 | end Conformance_Error; | |
5010 | ||
f991bd8e HK |
5011 | ----------------------- |
5012 | -- Conventions_Match -- | |
5013 | ----------------------- | |
5014 | ||
5015 | function Conventions_Match | |
5016 | (Id1 : Entity_Id; | |
5017 | Id2 : Entity_Id) return Boolean | |
5018 | is | |
5019 | begin | |
5020 | -- Ignore the conventions of anonymous access-to-subprogram types | |
5021 | -- and subprogram types because these are internally generated and | |
5022 | -- the only way these may receive a convention is if they inherit | |
5023 | -- the convention of a related subprogram. | |
5024 | ||
5025 | if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type, | |
5026 | E_Subprogram_Type) | |
5027 | or else | |
5028 | Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type, | |
5029 | E_Subprogram_Type) | |
5030 | then | |
5031 | return True; | |
5032 | ||
5033 | -- Otherwise compare the conventions directly | |
5034 | ||
5035 | else | |
5036 | return Convention (Id1) = Convention (Id2); | |
5037 | end if; | |
5038 | end Conventions_Match; | |
5039 | ||
ec4867fa ES |
5040 | -- Local Variables |
5041 | ||
5042 | Old_Type : constant Entity_Id := Etype (Old_Id); | |
5043 | New_Type : constant Entity_Id := Etype (New_Id); | |
5044 | Old_Formal : Entity_Id; | |
5045 | New_Formal : Entity_Id; | |
5046 | Access_Types_Match : Boolean; | |
5047 | Old_Formal_Base : Entity_Id; | |
5048 | New_Formal_Base : Entity_Id; | |
5049 | ||
996ae0b0 RK |
5050 | -- Start of processing for Check_Conformance |
5051 | ||
5052 | begin | |
5053 | Conforms := True; | |
5054 | ||
82c80734 RD |
5055 | -- We need a special case for operators, since they don't appear |
5056 | -- explicitly. | |
996ae0b0 RK |
5057 | |
5058 | if Ctype = Type_Conformant then | |
5059 | if Ekind (New_Id) = E_Operator | |
5060 | and then Operator_Matches_Spec (New_Id, Old_Id) | |
5061 | then | |
5062 | return; | |
5063 | end if; | |
5064 | end if; | |
5065 | ||
5066 | -- If both are functions/operators, check return types conform | |
5067 | ||
5068 | if Old_Type /= Standard_Void_Type | |
3cb9a885 AC |
5069 | and then |
5070 | New_Type /= Standard_Void_Type | |
996ae0b0 | 5071 | then |
fceeaab6 ES |
5072 | -- If we are checking interface conformance we omit controlling |
5073 | -- arguments and result, because we are only checking the conformance | |
5074 | -- of the remaining parameters. | |
5075 | ||
5076 | if Has_Controlling_Result (Old_Id) | |
5077 | and then Has_Controlling_Result (New_Id) | |
5078 | and then Skip_Controlling_Formals | |
5079 | then | |
5080 | null; | |
5081 | ||
5082 | elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then | |
7f568bfa AC |
5083 | if Ctype >= Subtype_Conformant |
5084 | and then not Predicates_Match (Old_Type, New_Type) | |
5085 | then | |
5086 | Conformance_Error | |
5087 | ("\predicate of return type does not match!", New_Id); | |
5088 | else | |
5089 | Conformance_Error | |
5090 | ("\return type does not match!", New_Id); | |
5091 | end if; | |
5092 | ||
996ae0b0 RK |
5093 | return; |
5094 | end if; | |
5095 | ||
41251c60 | 5096 | -- Ada 2005 (AI-231): In case of anonymous access types check the |
0a36105d | 5097 | -- null-exclusion and access-to-constant attributes match. |
41251c60 | 5098 | |
0791fbe9 | 5099 | if Ada_Version >= Ada_2005 |
41251c60 JM |
5100 | and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type |
5101 | and then | |
8fde064e AC |
5102 | (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type) |
5103 | or else Is_Access_Constant (Etype (Old_Type)) /= | |
5104 | Is_Access_Constant (Etype (New_Type))) | |
41251c60 | 5105 | then |
5d37ba92 | 5106 | Conformance_Error ("\return type does not match!", New_Id); |
41251c60 JM |
5107 | return; |
5108 | end if; | |
5109 | ||
996ae0b0 RK |
5110 | -- If either is a function/operator and the other isn't, error |
5111 | ||
5112 | elsif Old_Type /= Standard_Void_Type | |
5113 | or else New_Type /= Standard_Void_Type | |
5114 | then | |
5d37ba92 | 5115 | Conformance_Error ("\functions can only match functions!", New_Id); |
996ae0b0 RK |
5116 | return; |
5117 | end if; | |
5118 | ||
0a36105d | 5119 | -- In subtype conformant case, conventions must match (RM 6.3.1(16)). |
996ae0b0 RK |
5120 | -- If this is a renaming as body, refine error message to indicate that |
5121 | -- the conflict is with the original declaration. If the entity is not | |
5122 | -- frozen, the conventions don't have to match, the one of the renamed | |
5123 | -- entity is inherited. | |
5124 | ||
5125 | if Ctype >= Subtype_Conformant then | |
f991bd8e | 5126 | if not Conventions_Match (Old_Id, New_Id) then |
996ae0b0 RK |
5127 | if not Is_Frozen (New_Id) then |
5128 | null; | |
5129 | ||
5130 | elsif Present (Err_Loc) | |
5131 | and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration | |
5132 | and then Present (Corresponding_Spec (Err_Loc)) | |
5133 | then | |
5134 | Error_Msg_Name_1 := Chars (New_Id); | |
5135 | Error_Msg_Name_2 := | |
5136 | Name_Ada + Convention_Id'Pos (Convention (New_Id)); | |
5d37ba92 | 5137 | Conformance_Error ("\prior declaration for% has convention %!"); |
996ae0b0 RK |
5138 | |
5139 | else | |
5d37ba92 | 5140 | Conformance_Error ("\calling conventions do not match!"); |
996ae0b0 RK |
5141 | end if; |
5142 | ||
5143 | return; | |
5144 | ||
5145 | elsif Is_Formal_Subprogram (Old_Id) | |
5146 | or else Is_Formal_Subprogram (New_Id) | |
5147 | then | |
5d37ba92 | 5148 | Conformance_Error ("\formal subprograms not allowed!"); |
996ae0b0 RK |
5149 | return; |
5150 | end if; | |
5151 | end if; | |
5152 | ||
5153 | -- Deal with parameters | |
5154 | ||
5155 | -- Note: we use the entity information, rather than going directly | |
5156 | -- to the specification in the tree. This is not only simpler, but | |
5157 | -- absolutely necessary for some cases of conformance tests between | |
a90bd866 | 5158 | -- operators, where the declaration tree simply does not exist. |
996ae0b0 RK |
5159 | |
5160 | Old_Formal := First_Formal (Old_Id); | |
5161 | New_Formal := First_Formal (New_Id); | |
996ae0b0 | 5162 | while Present (Old_Formal) and then Present (New_Formal) loop |
41251c60 JM |
5163 | if Is_Controlling_Formal (Old_Formal) |
5164 | and then Is_Controlling_Formal (New_Formal) | |
5165 | and then Skip_Controlling_Formals | |
5166 | then | |
a2dc5812 AC |
5167 | -- The controlling formals will have different types when |
5168 | -- comparing an interface operation with its match, but both | |
5169 | -- or neither must be access parameters. | |
5170 | ||
5171 | if Is_Access_Type (Etype (Old_Formal)) | |
5172 | = | |
5173 | Is_Access_Type (Etype (New_Formal)) | |
5174 | then | |
5175 | goto Skip_Controlling_Formal; | |
5176 | else | |
5177 | Conformance_Error | |
5178 | ("\access parameter does not match!", New_Formal); | |
5179 | end if; | |
41251c60 JM |
5180 | end if; |
5181 | ||
21791d97 | 5182 | -- Ada 2012: Mode conformance also requires that formal parameters |
2a290fec AC |
5183 | -- be both aliased, or neither. |
5184 | ||
21791d97 | 5185 | if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then |
2a290fec AC |
5186 | if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then |
5187 | Conformance_Error | |
5188 | ("\aliased parameter mismatch!", New_Formal); | |
5189 | end if; | |
5190 | end if; | |
5191 | ||
fbf5a39b AC |
5192 | if Ctype = Fully_Conformant then |
5193 | ||
5194 | -- Names must match. Error message is more accurate if we do | |
5195 | -- this before checking that the types of the formals match. | |
5196 | ||
5197 | if Chars (Old_Formal) /= Chars (New_Formal) then | |
3ccedacc | 5198 | Conformance_Error ("\name& does not match!", New_Formal); |
fbf5a39b AC |
5199 | |
5200 | -- Set error posted flag on new formal as well to stop | |
5201 | -- junk cascaded messages in some cases. | |
5202 | ||
5203 | Set_Error_Posted (New_Formal); | |
5204 | return; | |
5205 | end if; | |
40b93859 RD |
5206 | |
5207 | -- Null exclusion must match | |
5208 | ||
5209 | if Null_Exclusion_Present (Parent (Old_Formal)) | |
5210 | /= | |
5211 | Null_Exclusion_Present (Parent (New_Formal)) | |
5212 | then | |
5213 | -- Only give error if both come from source. This should be | |
5214 | -- investigated some time, since it should not be needed ??? | |
5215 | ||
5216 | if Comes_From_Source (Old_Formal) | |
5217 | and then | |
5218 | Comes_From_Source (New_Formal) | |
5219 | then | |
5220 | Conformance_Error | |
3ccedacc | 5221 | ("\null exclusion for& does not match", New_Formal); |
40b93859 RD |
5222 | |
5223 | -- Mark error posted on the new formal to avoid duplicated | |
5224 | -- complaint about types not matching. | |
5225 | ||
5226 | Set_Error_Posted (New_Formal); | |
5227 | end if; | |
5228 | end if; | |
fbf5a39b | 5229 | end if; |
996ae0b0 | 5230 | |
ec4867fa ES |
5231 | -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This |
5232 | -- case occurs whenever a subprogram is being renamed and one of its | |
5233 | -- parameters imposes a null exclusion. For example: | |
5234 | ||
5235 | -- type T is null record; | |
5236 | -- type Acc_T is access T; | |
5237 | -- subtype Acc_T_Sub is Acc_T; | |
5238 | ||
5239 | -- procedure P (Obj : not null Acc_T_Sub); -- itype | |
5240 | -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype | |
5241 | -- renames P; | |
5242 | ||
5243 | Old_Formal_Base := Etype (Old_Formal); | |
5244 | New_Formal_Base := Etype (New_Formal); | |
5245 | ||
5246 | if Get_Inst then | |
5247 | Old_Formal_Base := Get_Instance_Of (Old_Formal_Base); | |
5248 | New_Formal_Base := Get_Instance_Of (New_Formal_Base); | |
5249 | end if; | |
5250 | ||
0791fbe9 | 5251 | Access_Types_Match := Ada_Version >= Ada_2005 |
ec4867fa | 5252 | |
8fde064e AC |
5253 | -- Ensure that this rule is only applied when New_Id is a |
5254 | -- renaming of Old_Id. | |
ec4867fa | 5255 | |
5d37ba92 ES |
5256 | and then Nkind (Parent (Parent (New_Id))) = |
5257 | N_Subprogram_Renaming_Declaration | |
ec4867fa ES |
5258 | and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity |
5259 | and then Present (Entity (Name (Parent (Parent (New_Id))))) | |
5260 | and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id | |
5261 | ||
8fde064e | 5262 | -- Now handle the allowed access-type case |
ec4867fa ES |
5263 | |
5264 | and then Is_Access_Type (Old_Formal_Base) | |
5265 | and then Is_Access_Type (New_Formal_Base) | |
5d37ba92 | 5266 | |
8fde064e AC |
5267 | -- The type kinds must match. The only exception occurs with |
5268 | -- multiple generics of the form: | |
5d37ba92 | 5269 | |
8fde064e AC |
5270 | -- generic generic |
5271 | -- type F is private; type A is private; | |
5272 | -- type F_Ptr is access F; type A_Ptr is access A; | |
5273 | -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr); | |
5274 | -- package F_Pack is ... package A_Pack is | |
5275 | -- package F_Inst is | |
5276 | -- new F_Pack (A, A_Ptr, A_P); | |
5d37ba92 | 5277 | |
8fde064e AC |
5278 | -- When checking for conformance between the parameters of A_P |
5279 | -- and F_P, the type kinds of F_Ptr and A_Ptr will not match | |
5280 | -- because the compiler has transformed A_Ptr into a subtype of | |
5281 | -- F_Ptr. We catch this case in the code below. | |
5d37ba92 ES |
5282 | |
5283 | and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base) | |
4b6f99f5 RD |
5284 | or else |
5285 | (Is_Generic_Type (Old_Formal_Base) | |
5286 | and then Is_Generic_Type (New_Formal_Base) | |
5287 | and then Is_Internal (New_Formal_Base) | |
5288 | and then Etype (Etype (New_Formal_Base)) = | |
5289 | Old_Formal_Base)) | |
5290 | and then Directly_Designated_Type (Old_Formal_Base) = | |
5291 | Directly_Designated_Type (New_Formal_Base) | |
ec4867fa ES |
5292 | and then ((Is_Itype (Old_Formal_Base) |
5293 | and then Can_Never_Be_Null (Old_Formal_Base)) | |
4b6f99f5 RD |
5294 | or else |
5295 | (Is_Itype (New_Formal_Base) | |
5296 | and then Can_Never_Be_Null (New_Formal_Base))); | |
ec4867fa | 5297 | |
996ae0b0 RK |
5298 | -- Types must always match. In the visible part of an instance, |
5299 | -- usual overloading rules for dispatching operations apply, and | |
5300 | -- we check base types (not the actual subtypes). | |
5301 | ||
5302 | if In_Instance_Visible_Part | |
5303 | and then Is_Dispatching_Operation (New_Id) | |
5304 | then | |
5305 | if not Conforming_Types | |
ec4867fa ES |
5306 | (T1 => Base_Type (Etype (Old_Formal)), |
5307 | T2 => Base_Type (Etype (New_Formal)), | |
5308 | Ctype => Ctype, | |
5309 | Get_Inst => Get_Inst) | |
5310 | and then not Access_Types_Match | |
996ae0b0 | 5311 | then |
5d37ba92 | 5312 | Conformance_Error ("\type of & does not match!", New_Formal); |
996ae0b0 RK |
5313 | return; |
5314 | end if; | |
5315 | ||
5316 | elsif not Conforming_Types | |
5d37ba92 ES |
5317 | (T1 => Old_Formal_Base, |
5318 | T2 => New_Formal_Base, | |
ec4867fa ES |
5319 | Ctype => Ctype, |
5320 | Get_Inst => Get_Inst) | |
5321 | and then not Access_Types_Match | |
996ae0b0 | 5322 | then |
c27f2f15 RD |
5323 | -- Don't give error message if old type is Any_Type. This test |
5324 | -- avoids some cascaded errors, e.g. in case of a bad spec. | |
5325 | ||
5326 | if Errmsg and then Old_Formal_Base = Any_Type then | |
5327 | Conforms := False; | |
5328 | else | |
7f568bfa AC |
5329 | if Ctype >= Subtype_Conformant |
5330 | and then | |
5331 | not Predicates_Match (Old_Formal_Base, New_Formal_Base) | |
5332 | then | |
5333 | Conformance_Error | |
5334 | ("\predicate of & does not match!", New_Formal); | |
5335 | else | |
5336 | Conformance_Error | |
5337 | ("\type of & does not match!", New_Formal); | |
7738270b AC |
5338 | |
5339 | if not Dimensions_Match (Old_Formal_Base, New_Formal_Base) | |
5340 | then | |
5341 | Error_Msg_N ("\dimensions mismatch!", New_Formal); | |
5342 | end if; | |
7f568bfa | 5343 | end if; |
c27f2f15 RD |
5344 | end if; |
5345 | ||
996ae0b0 RK |
5346 | return; |
5347 | end if; | |
5348 | ||
5349 | -- For mode conformance, mode must match | |
5350 | ||
5d37ba92 ES |
5351 | if Ctype >= Mode_Conformant then |
5352 | if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then | |
dd54644b JM |
5353 | if not Ekind_In (New_Id, E_Function, E_Procedure) |
5354 | or else not Is_Primitive_Wrapper (New_Id) | |
5355 | then | |
5356 | Conformance_Error ("\mode of & does not match!", New_Formal); | |
c199ccf7 | 5357 | |
dd54644b JM |
5358 | else |
5359 | declare | |
a2667f14 | 5360 | T : constant Entity_Id := Find_Dispatching_Type (New_Id); |
dd54644b | 5361 | begin |
3ccedacc | 5362 | if Is_Protected_Type (Corresponding_Concurrent_Type (T)) |
dd54644b | 5363 | then |
2c6336be | 5364 | Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id)); |
dd54644b JM |
5365 | else |
5366 | Conformance_Error | |
5367 | ("\mode of & does not match!", New_Formal); | |
5368 | end if; | |
5369 | end; | |
5370 | end if; | |
5371 | ||
5d37ba92 ES |
5372 | return; |
5373 | ||
5374 | -- Part of mode conformance for access types is having the same | |
5375 | -- constant modifier. | |
5376 | ||
5377 | elsif Access_Types_Match | |
5378 | and then Is_Access_Constant (Old_Formal_Base) /= | |
5379 | Is_Access_Constant (New_Formal_Base) | |
5380 | then | |
5381 | Conformance_Error | |
5382 | ("\constant modifier does not match!", New_Formal); | |
5383 | return; | |
5384 | end if; | |
996ae0b0 RK |
5385 | end if; |
5386 | ||
0a36105d | 5387 | if Ctype >= Subtype_Conformant then |
996ae0b0 | 5388 | |
0a36105d JM |
5389 | -- Ada 2005 (AI-231): In case of anonymous access types check |
5390 | -- the null-exclusion and access-to-constant attributes must | |
c7b9d548 AC |
5391 | -- match. For null exclusion, we test the types rather than the |
5392 | -- formals themselves, since the attribute is only set reliably | |
5393 | -- on the formals in the Ada 95 case, and we exclude the case | |
5394 | -- where Old_Formal is marked as controlling, to avoid errors | |
5395 | -- when matching completing bodies with dispatching declarations | |
5396 | -- (access formals in the bodies aren't marked Can_Never_Be_Null). | |
996ae0b0 | 5397 | |
0791fbe9 | 5398 | if Ada_Version >= Ada_2005 |
0a36105d JM |
5399 | and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type |
5400 | and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type | |
5401 | and then | |
c7b9d548 AC |
5402 | ((Can_Never_Be_Null (Etype (Old_Formal)) /= |
5403 | Can_Never_Be_Null (Etype (New_Formal)) | |
5404 | and then | |
5405 | not Is_Controlling_Formal (Old_Formal)) | |
0a36105d JM |
5406 | or else |
5407 | Is_Access_Constant (Etype (Old_Formal)) /= | |
5408 | Is_Access_Constant (Etype (New_Formal))) | |
40b93859 RD |
5409 | |
5410 | -- Do not complain if error already posted on New_Formal. This | |
5411 | -- avoids some redundant error messages. | |
5412 | ||
5413 | and then not Error_Posted (New_Formal) | |
0a36105d JM |
5414 | then |
5415 | -- It is allowed to omit the null-exclusion in case of stream | |
5416 | -- attribute subprograms. We recognize stream subprograms | |
5417 | -- through their TSS-generated suffix. | |
996ae0b0 | 5418 | |
0a36105d JM |
5419 | declare |
5420 | TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id); | |
3ada950b | 5421 | |
0a36105d JM |
5422 | begin |
5423 | if TSS_Name /= TSS_Stream_Read | |
5424 | and then TSS_Name /= TSS_Stream_Write | |
5425 | and then TSS_Name /= TSS_Stream_Input | |
5426 | and then TSS_Name /= TSS_Stream_Output | |
5427 | then | |
3ada950b | 5428 | -- Here we have a definite conformance error. It is worth |
71fb4dc8 | 5429 | -- special casing the error message for the case of a |
3ada950b AC |
5430 | -- controlling formal (which excludes null). |
5431 | ||
5432 | if Is_Controlling_Formal (New_Formal) then | |
5433 | Error_Msg_Node_2 := Scope (New_Formal); | |
5434 | Conformance_Error | |
3ccedacc AC |
5435 | ("\controlling formal & of & excludes null, " |
5436 | & "declaration must exclude null as well", | |
5437 | New_Formal); | |
3ada950b AC |
5438 | |
5439 | -- Normal case (couldn't we give more detail here???) | |
5440 | ||
5441 | else | |
5442 | Conformance_Error | |
5443 | ("\type of & does not match!", New_Formal); | |
5444 | end if; | |
5445 | ||
0a36105d JM |
5446 | return; |
5447 | end if; | |
5448 | end; | |
5449 | end if; | |
5450 | end if; | |
41251c60 | 5451 | |
0a36105d | 5452 | -- Full conformance checks |
41251c60 | 5453 | |
0a36105d | 5454 | if Ctype = Fully_Conformant then |
e660dbf7 | 5455 | |
0a36105d | 5456 | -- We have checked already that names match |
e660dbf7 | 5457 | |
0a36105d | 5458 | if Parameter_Mode (Old_Formal) = E_In_Parameter then |
41251c60 JM |
5459 | |
5460 | -- Check default expressions for in parameters | |
5461 | ||
996ae0b0 RK |
5462 | declare |
5463 | NewD : constant Boolean := | |
5464 | Present (Default_Value (New_Formal)); | |
5465 | OldD : constant Boolean := | |
5466 | Present (Default_Value (Old_Formal)); | |
5467 | begin | |
5468 | if NewD or OldD then | |
5469 | ||
82c80734 RD |
5470 | -- The old default value has been analyzed because the |
5471 | -- current full declaration will have frozen everything | |
0a36105d JM |
5472 | -- before. The new default value has not been analyzed, |
5473 | -- so analyze it now before we check for conformance. | |
996ae0b0 RK |
5474 | |
5475 | if NewD then | |
0a36105d | 5476 | Push_Scope (New_Id); |
21d27997 | 5477 | Preanalyze_Spec_Expression |
fbf5a39b | 5478 | (Default_Value (New_Formal), Etype (New_Formal)); |
996ae0b0 RK |
5479 | End_Scope; |
5480 | end if; | |
5481 | ||
5482 | if not (NewD and OldD) | |
5483 | or else not Fully_Conformant_Expressions | |
5484 | (Default_Value (Old_Formal), | |
5485 | Default_Value (New_Formal)) | |
5486 | then | |
5487 | Conformance_Error | |
5d37ba92 | 5488 | ("\default expression for & does not match!", |
996ae0b0 RK |
5489 | New_Formal); |
5490 | return; | |
5491 | end if; | |
5492 | end if; | |
5493 | end; | |
5494 | end if; | |
5495 | end if; | |
5496 | ||
5497 | -- A couple of special checks for Ada 83 mode. These checks are | |
0a36105d | 5498 | -- skipped if either entity is an operator in package Standard, |
996ae0b0 RK |
5499 | -- or if either old or new instance is not from the source program. |
5500 | ||
0ab80019 | 5501 | if Ada_Version = Ada_83 |
996ae0b0 RK |
5502 | and then Sloc (Old_Id) > Standard_Location |
5503 | and then Sloc (New_Id) > Standard_Location | |
5504 | and then Comes_From_Source (Old_Id) | |
5505 | and then Comes_From_Source (New_Id) | |
5506 | then | |
5507 | declare | |
5508 | Old_Param : constant Node_Id := Declaration_Node (Old_Formal); | |
5509 | New_Param : constant Node_Id := Declaration_Node (New_Formal); | |
5510 | ||
5511 | begin | |
5512 | -- Explicit IN must be present or absent in both cases. This | |
5513 | -- test is required only in the full conformance case. | |
5514 | ||
5515 | if In_Present (Old_Param) /= In_Present (New_Param) | |
5516 | and then Ctype = Fully_Conformant | |
5517 | then | |
5518 | Conformance_Error | |
5d37ba92 | 5519 | ("\(Ada 83) IN must appear in both declarations", |
996ae0b0 RK |
5520 | New_Formal); |
5521 | return; | |
5522 | end if; | |
5523 | ||
5524 | -- Grouping (use of comma in param lists) must be the same | |
5525 | -- This is where we catch a misconformance like: | |
5526 | ||
0a36105d | 5527 | -- A, B : Integer |
996ae0b0 RK |
5528 | -- A : Integer; B : Integer |
5529 | ||
5530 | -- which are represented identically in the tree except | |
5531 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5532 | ||
5533 | if More_Ids (Old_Param) /= More_Ids (New_Param) | |
5534 | or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param) | |
5535 | then | |
5536 | Conformance_Error | |
5d37ba92 | 5537 | ("\grouping of & does not match!", New_Formal); |
996ae0b0 RK |
5538 | return; |
5539 | end if; | |
5540 | end; | |
5541 | end if; | |
5542 | ||
41251c60 JM |
5543 | -- This label is required when skipping controlling formals |
5544 | ||
5545 | <<Skip_Controlling_Formal>> | |
5546 | ||
996ae0b0 RK |
5547 | Next_Formal (Old_Formal); |
5548 | Next_Formal (New_Formal); | |
5549 | end loop; | |
5550 | ||
5551 | if Present (Old_Formal) then | |
5d37ba92 | 5552 | Conformance_Error ("\too few parameters!"); |
996ae0b0 RK |
5553 | return; |
5554 | ||
5555 | elsif Present (New_Formal) then | |
5d37ba92 | 5556 | Conformance_Error ("\too many parameters!", New_Formal); |
996ae0b0 RK |
5557 | return; |
5558 | end if; | |
996ae0b0 RK |
5559 | end Check_Conformance; |
5560 | ||
ec4867fa ES |
5561 | ----------------------- |
5562 | -- Check_Conventions -- | |
5563 | ----------------------- | |
5564 | ||
5565 | procedure Check_Conventions (Typ : Entity_Id) is | |
ce2b6ba5 | 5566 | Ifaces_List : Elist_Id; |
0a36105d | 5567 | |
ce2b6ba5 | 5568 | procedure Check_Convention (Op : Entity_Id); |
0a36105d JM |
5569 | -- Verify that the convention of inherited dispatching operation Op is |
5570 | -- consistent among all subprograms it overrides. In order to minimize | |
5571 | -- the search, Search_From is utilized to designate a specific point in | |
5572 | -- the list rather than iterating over the whole list once more. | |
ec4867fa ES |
5573 | |
5574 | ---------------------- | |
5575 | -- Check_Convention -- | |
5576 | ---------------------- | |
5577 | ||
ce2b6ba5 | 5578 | procedure Check_Convention (Op : Entity_Id) is |
c5cec2fe | 5579 | Op_Conv : constant Convention_Id := Convention (Op); |
9f6aaa5c | 5580 | Iface_Conv : Convention_Id; |
ce2b6ba5 JM |
5581 | Iface_Elmt : Elmt_Id; |
5582 | Iface_Prim_Elmt : Elmt_Id; | |
5583 | Iface_Prim : Entity_Id; | |
ec4867fa | 5584 | |
ce2b6ba5 JM |
5585 | begin |
5586 | Iface_Elmt := First_Elmt (Ifaces_List); | |
5587 | while Present (Iface_Elmt) loop | |
5588 | Iface_Prim_Elmt := | |
9f6aaa5c | 5589 | First_Elmt (Primitive_Operations (Node (Iface_Elmt))); |
ce2b6ba5 JM |
5590 | while Present (Iface_Prim_Elmt) loop |
5591 | Iface_Prim := Node (Iface_Prim_Elmt); | |
c5cec2fe | 5592 | Iface_Conv := Convention (Iface_Prim); |
ce2b6ba5 JM |
5593 | |
5594 | if Is_Interface_Conformant (Typ, Iface_Prim, Op) | |
9f6aaa5c | 5595 | and then Iface_Conv /= Op_Conv |
ce2b6ba5 | 5596 | then |
ed2233dc | 5597 | Error_Msg_N |
ce2b6ba5 | 5598 | ("inconsistent conventions in primitive operations", Typ); |
ec4867fa | 5599 | |
ce2b6ba5 | 5600 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c | 5601 | Error_Msg_Name_2 := Get_Convention_Name (Op_Conv); |
ce2b6ba5 | 5602 | Error_Msg_Sloc := Sloc (Op); |
ec4867fa | 5603 | |
7a963087 | 5604 | if Comes_From_Source (Op) or else No (Alias (Op)) then |
038140ed | 5605 | if not Present (Overridden_Operation (Op)) then |
ed2233dc | 5606 | Error_Msg_N ("\\primitive % defined #", Typ); |
ce2b6ba5 | 5607 | else |
ed2233dc | 5608 | Error_Msg_N |
3ccedacc AC |
5609 | ("\\overriding operation % with " |
5610 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5611 | end if; |
ec4867fa | 5612 | |
ce2b6ba5 JM |
5613 | else pragma Assert (Present (Alias (Op))); |
5614 | Error_Msg_Sloc := Sloc (Alias (Op)); | |
3ccedacc AC |
5615 | Error_Msg_N ("\\inherited operation % with " |
5616 | & "convention % defined #", Typ); | |
ce2b6ba5 | 5617 | end if; |
ec4867fa | 5618 | |
ce2b6ba5 | 5619 | Error_Msg_Name_1 := Chars (Op); |
9f6aaa5c AC |
5620 | Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv); |
5621 | Error_Msg_Sloc := Sloc (Iface_Prim); | |
3ccedacc AC |
5622 | Error_Msg_N ("\\overridden operation % with " |
5623 | & "convention % defined #", Typ); | |
ec4867fa | 5624 | |
ce2b6ba5 | 5625 | -- Avoid cascading errors |
ec4867fa | 5626 | |
ce2b6ba5 JM |
5627 | return; |
5628 | end if; | |
ec4867fa | 5629 | |
ce2b6ba5 JM |
5630 | Next_Elmt (Iface_Prim_Elmt); |
5631 | end loop; | |
ec4867fa | 5632 | |
ce2b6ba5 | 5633 | Next_Elmt (Iface_Elmt); |
ec4867fa ES |
5634 | end loop; |
5635 | end Check_Convention; | |
5636 | ||
5637 | -- Local variables | |
5638 | ||
5639 | Prim_Op : Entity_Id; | |
5640 | Prim_Op_Elmt : Elmt_Id; | |
5641 | ||
5642 | -- Start of processing for Check_Conventions | |
5643 | ||
5644 | begin | |
ce2b6ba5 JM |
5645 | if not Has_Interfaces (Typ) then |
5646 | return; | |
5647 | end if; | |
5648 | ||
5649 | Collect_Interfaces (Typ, Ifaces_List); | |
5650 | ||
0a36105d JM |
5651 | -- The algorithm checks every overriding dispatching operation against |
5652 | -- all the corresponding overridden dispatching operations, detecting | |
f3d57416 | 5653 | -- differences in conventions. |
ec4867fa ES |
5654 | |
5655 | Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
5656 | while Present (Prim_Op_Elmt) loop | |
5657 | Prim_Op := Node (Prim_Op_Elmt); | |
5658 | ||
0a36105d | 5659 | -- A small optimization: skip the predefined dispatching operations |
ce2b6ba5 | 5660 | -- since they always have the same convention. |
ec4867fa | 5661 | |
ce2b6ba5 JM |
5662 | if not Is_Predefined_Dispatching_Operation (Prim_Op) then |
5663 | Check_Convention (Prim_Op); | |
ec4867fa ES |
5664 | end if; |
5665 | ||
5666 | Next_Elmt (Prim_Op_Elmt); | |
5667 | end loop; | |
5668 | end Check_Conventions; | |
5669 | ||
996ae0b0 RK |
5670 | ------------------------------ |
5671 | -- Check_Delayed_Subprogram -- | |
5672 | ------------------------------ | |
5673 | ||
5674 | procedure Check_Delayed_Subprogram (Designator : Entity_Id) is | |
5675 | F : Entity_Id; | |
5676 | ||
5677 | procedure Possible_Freeze (T : Entity_Id); | |
5678 | -- T is the type of either a formal parameter or of the return type. | |
5679 | -- If T is not yet frozen and needs a delayed freeze, then the | |
1e55d29a | 5680 | -- subprogram itself must be delayed. |
996ae0b0 | 5681 | |
82c80734 RD |
5682 | --------------------- |
5683 | -- Possible_Freeze -- | |
5684 | --------------------- | |
5685 | ||
996ae0b0 RK |
5686 | procedure Possible_Freeze (T : Entity_Id) is |
5687 | begin | |
4a13695c | 5688 | if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then |
996ae0b0 RK |
5689 | Set_Has_Delayed_Freeze (Designator); |
5690 | ||
5691 | elsif Is_Access_Type (T) | |
5692 | and then Has_Delayed_Freeze (Designated_Type (T)) | |
5693 | and then not Is_Frozen (Designated_Type (T)) | |
5694 | then | |
5695 | Set_Has_Delayed_Freeze (Designator); | |
5696 | end if; | |
4a13695c | 5697 | |
996ae0b0 RK |
5698 | end Possible_Freeze; |
5699 | ||
5700 | -- Start of processing for Check_Delayed_Subprogram | |
5701 | ||
5702 | begin | |
76e3504f AC |
5703 | -- All subprograms, including abstract subprograms, may need a freeze |
5704 | -- node if some formal type or the return type needs one. | |
996ae0b0 | 5705 | |
76e3504f AC |
5706 | Possible_Freeze (Etype (Designator)); |
5707 | Possible_Freeze (Base_Type (Etype (Designator))); -- needed ??? | |
996ae0b0 | 5708 | |
76e3504f AC |
5709 | -- Need delayed freeze if any of the formal types themselves need |
5710 | -- a delayed freeze and are not yet frozen. | |
996ae0b0 | 5711 | |
76e3504f AC |
5712 | F := First_Formal (Designator); |
5713 | while Present (F) loop | |
5714 | Possible_Freeze (Etype (F)); | |
5715 | Possible_Freeze (Base_Type (Etype (F))); -- needed ??? | |
5716 | Next_Formal (F); | |
5717 | end loop; | |
996ae0b0 RK |
5718 | |
5719 | -- Mark functions that return by reference. Note that it cannot be | |
5720 | -- done for delayed_freeze subprograms because the underlying | |
5721 | -- returned type may not be known yet (for private types) | |
5722 | ||
8fde064e | 5723 | if not Has_Delayed_Freeze (Designator) and then Expander_Active then |
996ae0b0 RK |
5724 | declare |
5725 | Typ : constant Entity_Id := Etype (Designator); | |
5726 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
996ae0b0 | 5727 | begin |
51245e2d | 5728 | if Is_Limited_View (Typ) then |
996ae0b0 | 5729 | Set_Returns_By_Ref (Designator); |
048e5cef | 5730 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
996ae0b0 RK |
5731 | Set_Returns_By_Ref (Designator); |
5732 | end if; | |
5733 | end; | |
5734 | end if; | |
5735 | end Check_Delayed_Subprogram; | |
5736 | ||
5737 | ------------------------------------ | |
5738 | -- Check_Discriminant_Conformance -- | |
5739 | ------------------------------------ | |
5740 | ||
5741 | procedure Check_Discriminant_Conformance | |
5742 | (N : Node_Id; | |
5743 | Prev : Entity_Id; | |
5744 | Prev_Loc : Node_Id) | |
5745 | is | |
5746 | Old_Discr : Entity_Id := First_Discriminant (Prev); | |
5747 | New_Discr : Node_Id := First (Discriminant_Specifications (N)); | |
5748 | New_Discr_Id : Entity_Id; | |
5749 | New_Discr_Type : Entity_Id; | |
5750 | ||
5751 | procedure Conformance_Error (Msg : String; N : Node_Id); | |
82c80734 RD |
5752 | -- Post error message for conformance error on given node. Two messages |
5753 | -- are output. The first points to the previous declaration with a | |
5754 | -- general "no conformance" message. The second is the detailed reason, | |
5755 | -- supplied as Msg. The parameter N provide information for a possible | |
5756 | -- & insertion in the message. | |
996ae0b0 RK |
5757 | |
5758 | ----------------------- | |
5759 | -- Conformance_Error -- | |
5760 | ----------------------- | |
5761 | ||
5762 | procedure Conformance_Error (Msg : String; N : Node_Id) is | |
5763 | begin | |
5764 | Error_Msg_Sloc := Sloc (Prev_Loc); | |
483c78cb RD |
5765 | Error_Msg_N -- CODEFIX |
5766 | ("not fully conformant with declaration#!", N); | |
996ae0b0 RK |
5767 | Error_Msg_NE (Msg, N, N); |
5768 | end Conformance_Error; | |
5769 | ||
5770 | -- Start of processing for Check_Discriminant_Conformance | |
5771 | ||
5772 | begin | |
5773 | while Present (Old_Discr) and then Present (New_Discr) loop | |
996ae0b0 RK |
5774 | New_Discr_Id := Defining_Identifier (New_Discr); |
5775 | ||
82c80734 RD |
5776 | -- The subtype mark of the discriminant on the full type has not |
5777 | -- been analyzed so we do it here. For an access discriminant a new | |
5778 | -- type is created. | |
996ae0b0 RK |
5779 | |
5780 | if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then | |
5781 | New_Discr_Type := | |
5782 | Access_Definition (N, Discriminant_Type (New_Discr)); | |
5783 | ||
5784 | else | |
5785 | Analyze (Discriminant_Type (New_Discr)); | |
5786 | New_Discr_Type := Etype (Discriminant_Type (New_Discr)); | |
e50e1c5e AC |
5787 | |
5788 | -- Ada 2005: if the discriminant definition carries a null | |
5789 | -- exclusion, create an itype to check properly for consistency | |
5790 | -- with partial declaration. | |
5791 | ||
5792 | if Is_Access_Type (New_Discr_Type) | |
8fde064e | 5793 | and then Null_Exclusion_Present (New_Discr) |
e50e1c5e AC |
5794 | then |
5795 | New_Discr_Type := | |
5796 | Create_Null_Excluding_Itype | |
5797 | (T => New_Discr_Type, | |
5798 | Related_Nod => New_Discr, | |
5799 | Scope_Id => Current_Scope); | |
5800 | end if; | |
996ae0b0 RK |
5801 | end if; |
5802 | ||
5803 | if not Conforming_Types | |
5804 | (Etype (Old_Discr), New_Discr_Type, Fully_Conformant) | |
5805 | then | |
5806 | Conformance_Error ("type of & does not match!", New_Discr_Id); | |
5807 | return; | |
fbf5a39b | 5808 | else |
82c80734 RD |
5809 | -- Treat the new discriminant as an occurrence of the old one, |
5810 | -- for navigation purposes, and fill in some semantic | |
fbf5a39b AC |
5811 | -- information, for completeness. |
5812 | ||
5813 | Generate_Reference (Old_Discr, New_Discr_Id, 'r'); | |
5814 | Set_Etype (New_Discr_Id, Etype (Old_Discr)); | |
5815 | Set_Scope (New_Discr_Id, Scope (Old_Discr)); | |
996ae0b0 RK |
5816 | end if; |
5817 | ||
5818 | -- Names must match | |
5819 | ||
5820 | if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then | |
5821 | Conformance_Error ("name & does not match!", New_Discr_Id); | |
5822 | return; | |
5823 | end if; | |
5824 | ||
5825 | -- Default expressions must match | |
5826 | ||
5827 | declare | |
5828 | NewD : constant Boolean := | |
5829 | Present (Expression (New_Discr)); | |
5830 | OldD : constant Boolean := | |
5831 | Present (Expression (Parent (Old_Discr))); | |
5832 | ||
5833 | begin | |
5834 | if NewD or OldD then | |
5835 | ||
5836 | -- The old default value has been analyzed and expanded, | |
5837 | -- because the current full declaration will have frozen | |
82c80734 RD |
5838 | -- everything before. The new default values have not been |
5839 | -- expanded, so expand now to check conformance. | |
996ae0b0 RK |
5840 | |
5841 | if NewD then | |
21d27997 | 5842 | Preanalyze_Spec_Expression |
996ae0b0 RK |
5843 | (Expression (New_Discr), New_Discr_Type); |
5844 | end if; | |
5845 | ||
5846 | if not (NewD and OldD) | |
5847 | or else not Fully_Conformant_Expressions | |
5848 | (Expression (Parent (Old_Discr)), | |
5849 | Expression (New_Discr)) | |
5850 | ||
5851 | then | |
5852 | Conformance_Error | |
5853 | ("default expression for & does not match!", | |
5854 | New_Discr_Id); | |
5855 | return; | |
5856 | end if; | |
5857 | end if; | |
5858 | end; | |
5859 | ||
5860 | -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X) | |
5861 | ||
0ab80019 | 5862 | if Ada_Version = Ada_83 then |
996ae0b0 RK |
5863 | declare |
5864 | Old_Disc : constant Node_Id := Declaration_Node (Old_Discr); | |
5865 | ||
5866 | begin | |
5867 | -- Grouping (use of comma in param lists) must be the same | |
5868 | -- This is where we catch a misconformance like: | |
5869 | ||
60370fb1 | 5870 | -- A, B : Integer |
996ae0b0 RK |
5871 | -- A : Integer; B : Integer |
5872 | ||
5873 | -- which are represented identically in the tree except | |
5874 | -- for the setting of the flags More_Ids and Prev_Ids. | |
5875 | ||
5876 | if More_Ids (Old_Disc) /= More_Ids (New_Discr) | |
5877 | or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr) | |
5878 | then | |
5879 | Conformance_Error | |
5880 | ("grouping of & does not match!", New_Discr_Id); | |
5881 | return; | |
5882 | end if; | |
5883 | end; | |
5884 | end if; | |
5885 | ||
5886 | Next_Discriminant (Old_Discr); | |
5887 | Next (New_Discr); | |
5888 | end loop; | |
5889 | ||
5890 | if Present (Old_Discr) then | |
5891 | Conformance_Error ("too few discriminants!", Defining_Identifier (N)); | |
5892 | return; | |
5893 | ||
5894 | elsif Present (New_Discr) then | |
5895 | Conformance_Error | |
5896 | ("too many discriminants!", Defining_Identifier (New_Discr)); | |
5897 | return; | |
5898 | end if; | |
5899 | end Check_Discriminant_Conformance; | |
5900 | ||
5901 | ---------------------------- | |
5902 | -- Check_Fully_Conformant -- | |
5903 | ---------------------------- | |
5904 | ||
5905 | procedure Check_Fully_Conformant | |
5906 | (New_Id : Entity_Id; | |
5907 | Old_Id : Entity_Id; | |
5908 | Err_Loc : Node_Id := Empty) | |
5909 | is | |
5910 | Result : Boolean; | |
81db9d77 | 5911 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5912 | begin |
5913 | Check_Conformance | |
5914 | (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc); | |
5915 | end Check_Fully_Conformant; | |
5916 | ||
b420ba79 AC |
5917 | -------------------------- |
5918 | -- Check_Limited_Return -- | |
5919 | -------------------------- | |
5920 | ||
5921 | procedure Check_Limited_Return | |
5922 | (N : Node_Id; | |
5923 | Expr : Node_Id; | |
5924 | R_Type : Entity_Id) | |
5925 | is | |
5926 | begin | |
5927 | -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and | |
5928 | -- replaced by anonymous access results. This is an incompatibility with | |
5929 | -- Ada 95. Not clear whether this should be enforced yet or perhaps | |
5930 | -- controllable with special switch. ??? | |
5931 | ||
5932 | -- A limited interface that is not immutably limited is OK | |
5933 | ||
5934 | if Is_Limited_Interface (R_Type) | |
5935 | and then | |
5936 | not (Is_Task_Interface (R_Type) | |
5937 | or else Is_Protected_Interface (R_Type) | |
5938 | or else Is_Synchronized_Interface (R_Type)) | |
5939 | then | |
5940 | null; | |
5941 | ||
5942 | elsif Is_Limited_Type (R_Type) | |
5943 | and then not Is_Interface (R_Type) | |
5944 | and then Comes_From_Source (N) | |
5945 | and then not In_Instance_Body | |
5946 | and then not OK_For_Limited_Init_In_05 (R_Type, Expr) | |
5947 | then | |
5948 | -- Error in Ada 2005 | |
5949 | ||
5950 | if Ada_Version >= Ada_2005 | |
5951 | and then not Debug_Flag_Dot_L | |
5952 | and then not GNAT_Mode | |
5953 | then | |
5954 | Error_Msg_N | |
5955 | ("(Ada 2005) cannot copy object of a limited type " | |
5956 | & "(RM-2005 6.5(5.5/2))", Expr); | |
5957 | ||
5958 | if Is_Limited_View (R_Type) then | |
5959 | Error_Msg_N | |
5960 | ("\return by reference not permitted in Ada 2005", Expr); | |
5961 | end if; | |
5962 | ||
5963 | -- Warn in Ada 95 mode, to give folks a heads up about this | |
5964 | -- incompatibility. | |
5965 | ||
5966 | -- In GNAT mode, this is just a warning, to allow it to be evilly | |
5967 | -- turned off. Otherwise it is a real error. | |
5968 | ||
5969 | -- In a generic context, simplify the warning because it makes no | |
5970 | -- sense to discuss pass-by-reference or copy. | |
5971 | ||
5972 | elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then | |
5973 | if Inside_A_Generic then | |
5974 | Error_Msg_N | |
5975 | ("return of limited object not permitted in Ada 2005 " | |
5976 | & "(RM-2005 6.5(5.5/2))?y?", Expr); | |
5977 | ||
5978 | elsif Is_Limited_View (R_Type) then | |
5979 | Error_Msg_N | |
5980 | ("return by reference not permitted in Ada 2005 " | |
5981 | & "(RM-2005 6.5(5.5/2))?y?", Expr); | |
5982 | else | |
5983 | Error_Msg_N | |
5984 | ("cannot copy object of a limited type in Ada 2005 " | |
5985 | & "(RM-2005 6.5(5.5/2))?y?", Expr); | |
5986 | end if; | |
5987 | ||
5988 | -- Ada 95 mode, compatibility warnings disabled | |
5989 | ||
5990 | else | |
5991 | return; -- skip continuation messages below | |
5992 | end if; | |
5993 | ||
5994 | if not Inside_A_Generic then | |
5995 | Error_Msg_N | |
5996 | ("\consider switching to return of access type", Expr); | |
5997 | Explain_Limited_Type (R_Type, Expr); | |
5998 | end if; | |
5999 | end if; | |
6000 | end Check_Limited_Return; | |
6001 | ||
996ae0b0 RK |
6002 | --------------------------- |
6003 | -- Check_Mode_Conformant -- | |
6004 | --------------------------- | |
6005 | ||
6006 | procedure Check_Mode_Conformant | |
6007 | (New_Id : Entity_Id; | |
6008 | Old_Id : Entity_Id; | |
6009 | Err_Loc : Node_Id := Empty; | |
6010 | Get_Inst : Boolean := False) | |
6011 | is | |
6012 | Result : Boolean; | |
81db9d77 | 6013 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6014 | begin |
6015 | Check_Conformance | |
6016 | (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst); | |
6017 | end Check_Mode_Conformant; | |
6018 | ||
fbf5a39b | 6019 | -------------------------------- |
758c442c | 6020 | -- Check_Overriding_Indicator -- |
fbf5a39b AC |
6021 | -------------------------------- |
6022 | ||
758c442c | 6023 | procedure Check_Overriding_Indicator |
ec4867fa | 6024 | (Subp : Entity_Id; |
5d37ba92 ES |
6025 | Overridden_Subp : Entity_Id; |
6026 | Is_Primitive : Boolean) | |
fbf5a39b | 6027 | is |
758c442c GD |
6028 | Decl : Node_Id; |
6029 | Spec : Node_Id; | |
fbf5a39b AC |
6030 | |
6031 | begin | |
ec4867fa | 6032 | -- No overriding indicator for literals |
fbf5a39b | 6033 | |
ec4867fa | 6034 | if Ekind (Subp) = E_Enumeration_Literal then |
758c442c | 6035 | return; |
fbf5a39b | 6036 | |
ec4867fa ES |
6037 | elsif Ekind (Subp) = E_Entry then |
6038 | Decl := Parent (Subp); | |
6039 | ||
53b10ce9 AC |
6040 | -- No point in analyzing a malformed operator |
6041 | ||
6042 | elsif Nkind (Subp) = N_Defining_Operator_Symbol | |
6043 | and then Error_Posted (Subp) | |
6044 | then | |
6045 | return; | |
6046 | ||
758c442c GD |
6047 | else |
6048 | Decl := Unit_Declaration_Node (Subp); | |
6049 | end if; | |
fbf5a39b | 6050 | |
800621e0 RD |
6051 | if Nkind_In (Decl, N_Subprogram_Body, |
6052 | N_Subprogram_Body_Stub, | |
6053 | N_Subprogram_Declaration, | |
6054 | N_Abstract_Subprogram_Declaration, | |
6055 | N_Subprogram_Renaming_Declaration) | |
758c442c GD |
6056 | then |
6057 | Spec := Specification (Decl); | |
ec4867fa ES |
6058 | |
6059 | elsif Nkind (Decl) = N_Entry_Declaration then | |
6060 | Spec := Decl; | |
6061 | ||
758c442c GD |
6062 | else |
6063 | return; | |
6064 | end if; | |
fbf5a39b | 6065 | |
e7d72fb9 AC |
6066 | -- The overriding operation is type conformant with the overridden one, |
6067 | -- but the names of the formals are not required to match. If the names | |
6823270c | 6068 | -- appear permuted in the overriding operation, this is a possible |
e7d72fb9 AC |
6069 | -- source of confusion that is worth diagnosing. Controlling formals |
6070 | -- often carry names that reflect the type, and it is not worthwhile | |
6071 | -- requiring that their names match. | |
6072 | ||
c9e7bd8e | 6073 | if Present (Overridden_Subp) |
e7d72fb9 AC |
6074 | and then Nkind (Subp) /= N_Defining_Operator_Symbol |
6075 | then | |
6076 | declare | |
6077 | Form1 : Entity_Id; | |
6078 | Form2 : Entity_Id; | |
6079 | ||
6080 | begin | |
6081 | Form1 := First_Formal (Subp); | |
6082 | Form2 := First_Formal (Overridden_Subp); | |
6083 | ||
c9e7bd8e AC |
6084 | -- If the overriding operation is a synchronized operation, skip |
6085 | -- the first parameter of the overridden operation, which is | |
6823270c AC |
6086 | -- implicit in the new one. If the operation is declared in the |
6087 | -- body it is not primitive and all formals must match. | |
c9e7bd8e | 6088 | |
6823270c AC |
6089 | if Is_Concurrent_Type (Scope (Subp)) |
6090 | and then Is_Tagged_Type (Scope (Subp)) | |
6091 | and then not Has_Completion (Scope (Subp)) | |
6092 | then | |
c9e7bd8e AC |
6093 | Form2 := Next_Formal (Form2); |
6094 | end if; | |
6095 | ||
e7d72fb9 AC |
6096 | if Present (Form1) then |
6097 | Form1 := Next_Formal (Form1); | |
6098 | Form2 := Next_Formal (Form2); | |
6099 | end if; | |
6100 | ||
6101 | while Present (Form1) loop | |
6102 | if not Is_Controlling_Formal (Form1) | |
6103 | and then Present (Next_Formal (Form2)) | |
6104 | and then Chars (Form1) = Chars (Next_Formal (Form2)) | |
6105 | then | |
6106 | Error_Msg_Node_2 := Alias (Overridden_Subp); | |
6107 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
ed2233dc | 6108 | Error_Msg_NE |
19d846a0 | 6109 | ("& does not match corresponding formal of&#", |
e7d72fb9 AC |
6110 | Form1, Form1); |
6111 | exit; | |
6112 | end if; | |
6113 | ||
6114 | Next_Formal (Form1); | |
6115 | Next_Formal (Form2); | |
6116 | end loop; | |
6117 | end; | |
6118 | end if; | |
6119 | ||
676e8420 AC |
6120 | -- If there is an overridden subprogram, then check that there is no |
6121 | -- "not overriding" indicator, and mark the subprogram as overriding. | |
51bf9bdf AC |
6122 | -- This is not done if the overridden subprogram is marked as hidden, |
6123 | -- which can occur for the case of inherited controlled operations | |
6124 | -- (see Derive_Subprogram), unless the inherited subprogram's parent | |
6125 | -- subprogram is not itself hidden. (Note: This condition could probably | |
6126 | -- be simplified, leaving out the testing for the specific controlled | |
6127 | -- cases, but it seems safer and clearer this way, and echoes similar | |
6128 | -- special-case tests of this kind in other places.) | |
6129 | ||
fd0d899b | 6130 | if Present (Overridden_Subp) |
51bf9bdf AC |
6131 | and then (not Is_Hidden (Overridden_Subp) |
6132 | or else | |
b69cd36a AC |
6133 | (Nam_In (Chars (Overridden_Subp), Name_Initialize, |
6134 | Name_Adjust, | |
6135 | Name_Finalize) | |
f0709ca6 AC |
6136 | and then Present (Alias (Overridden_Subp)) |
6137 | and then not Is_Hidden (Alias (Overridden_Subp)))) | |
fd0d899b | 6138 | then |
ec4867fa ES |
6139 | if Must_Not_Override (Spec) then |
6140 | Error_Msg_Sloc := Sloc (Overridden_Subp); | |
fbf5a39b | 6141 | |
ec4867fa | 6142 | if Ekind (Subp) = E_Entry then |
ed2233dc | 6143 | Error_Msg_NE |
5d37ba92 | 6144 | ("entry & overrides inherited operation #", Spec, Subp); |
ec4867fa | 6145 | else |
ed2233dc | 6146 | Error_Msg_NE |
5d37ba92 | 6147 | ("subprogram & overrides inherited operation #", Spec, Subp); |
ec4867fa | 6148 | end if; |
21d27997 | 6149 | |
bd603506 | 6150 | -- Special-case to fix a GNAT oddity: Limited_Controlled is declared |
24a120ac AC |
6151 | -- as an extension of Root_Controlled, and thus has a useless Adjust |
6152 | -- operation. This operation should not be inherited by other limited | |
6153 | -- controlled types. An explicit Adjust for them is not overriding. | |
6154 | ||
6155 | elsif Must_Override (Spec) | |
6156 | and then Chars (Overridden_Subp) = Name_Adjust | |
6157 | and then Is_Limited_Type (Etype (First_Formal (Subp))) | |
6158 | and then Present (Alias (Overridden_Subp)) | |
bd603506 RD |
6159 | and then |
6160 | Is_Predefined_File_Name | |
6161 | (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp)))) | |
24a120ac | 6162 | then |
b043ae01 AC |
6163 | Get_Name_String |
6164 | (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp)))); | |
24a120ac AC |
6165 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
6166 | ||
21d27997 | 6167 | elsif Is_Subprogram (Subp) then |
2fe829ae ES |
6168 | if Is_Init_Proc (Subp) then |
6169 | null; | |
6170 | ||
6171 | elsif No (Overridden_Operation (Subp)) then | |
1c1289e7 AC |
6172 | |
6173 | -- For entities generated by Derive_Subprograms the overridden | |
6174 | -- operation is the inherited primitive (which is available | |
6175 | -- through the attribute alias) | |
6176 | ||
6177 | if (Is_Dispatching_Operation (Subp) | |
f9673bb0 | 6178 | or else Is_Dispatching_Operation (Overridden_Subp)) |
1c1289e7 | 6179 | and then not Comes_From_Source (Overridden_Subp) |
f9673bb0 AC |
6180 | and then Find_Dispatching_Type (Overridden_Subp) = |
6181 | Find_Dispatching_Type (Subp) | |
1c1289e7 AC |
6182 | and then Present (Alias (Overridden_Subp)) |
6183 | and then Comes_From_Source (Alias (Overridden_Subp)) | |
6184 | then | |
039538bc AC |
6185 | Set_Overridden_Operation (Subp, Alias (Overridden_Subp)); |
6186 | Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp)); | |
2fe829ae | 6187 | |
1c1289e7 | 6188 | else |
039538bc AC |
6189 | Set_Overridden_Operation (Subp, Overridden_Subp); |
6190 | Inherit_Subprogram_Contract (Subp, Overridden_Subp); | |
1c1289e7 AC |
6191 | end if; |
6192 | end if; | |
ec4867fa | 6193 | end if; |
f937473f | 6194 | |
618fb570 AC |
6195 | -- If primitive flag is set or this is a protected operation, then |
6196 | -- the operation is overriding at the point of its declaration, so | |
6197 | -- warn if necessary. Otherwise it may have been declared before the | |
6198 | -- operation it overrides and no check is required. | |
3c25856a AC |
6199 | |
6200 | if Style_Check | |
618fb570 AC |
6201 | and then not Must_Override (Spec) |
6202 | and then (Is_Primitive | |
6203 | or else Ekind (Scope (Subp)) = E_Protected_Type) | |
3c25856a | 6204 | then |
235f4375 AC |
6205 | Style.Missing_Overriding (Decl, Subp); |
6206 | end if; | |
6207 | ||
53b10ce9 AC |
6208 | -- If Subp is an operator, it may override a predefined operation, if |
6209 | -- it is defined in the same scope as the type to which it applies. | |
676e8420 | 6210 | -- In that case Overridden_Subp is empty because of our implicit |
5d37ba92 ES |
6211 | -- representation for predefined operators. We have to check whether the |
6212 | -- signature of Subp matches that of a predefined operator. Note that | |
6213 | -- first argument provides the name of the operator, and the second | |
6214 | -- argument the signature that may match that of a standard operation. | |
21d27997 RD |
6215 | -- If the indicator is overriding, then the operator must match a |
6216 | -- predefined signature, because we know already that there is no | |
6217 | -- explicit overridden operation. | |
f937473f | 6218 | |
21d27997 | 6219 | elsif Nkind (Subp) = N_Defining_Operator_Symbol then |
806f6d37 | 6220 | if Must_Not_Override (Spec) then |
f937473f | 6221 | |
806f6d37 AC |
6222 | -- If this is not a primitive or a protected subprogram, then |
6223 | -- "not overriding" is illegal. | |
618fb570 | 6224 | |
806f6d37 AC |
6225 | if not Is_Primitive |
6226 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
6227 | then | |
3ccedacc AC |
6228 | Error_Msg_N ("overriding indicator only allowed " |
6229 | & "if subprogram is primitive", Subp); | |
618fb570 | 6230 | |
806f6d37 AC |
6231 | elsif Can_Override_Operator (Subp) then |
6232 | Error_Msg_NE | |
6233 | ("subprogram& overrides predefined operator ", Spec, Subp); | |
6234 | end if; | |
f937473f | 6235 | |
806f6d37 AC |
6236 | elsif Must_Override (Spec) then |
6237 | if No (Overridden_Operation (Subp)) | |
6238 | and then not Can_Override_Operator (Subp) | |
6239 | then | |
6240 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
6241 | end if; | |
5d37ba92 | 6242 | |
806f6d37 AC |
6243 | elsif not Error_Posted (Subp) |
6244 | and then Style_Check | |
6245 | and then Can_Override_Operator (Subp) | |
6246 | and then | |
6247 | not Is_Predefined_File_Name | |
6248 | (Unit_File_Name (Get_Source_Unit (Subp))) | |
6249 | then | |
6250 | -- If style checks are enabled, indicate that the indicator is | |
6251 | -- missing. However, at the point of declaration, the type of | |
6252 | -- which this is a primitive operation may be private, in which | |
6253 | -- case the indicator would be premature. | |
235f4375 | 6254 | |
806f6d37 AC |
6255 | if Has_Private_Declaration (Etype (Subp)) |
6256 | or else Has_Private_Declaration (Etype (First_Formal (Subp))) | |
53b10ce9 | 6257 | then |
806f6d37 AC |
6258 | null; |
6259 | else | |
6260 | Style.Missing_Overriding (Decl, Subp); | |
5d5832bc | 6261 | end if; |
806f6d37 | 6262 | end if; |
21d27997 RD |
6263 | |
6264 | elsif Must_Override (Spec) then | |
6265 | if Ekind (Subp) = E_Entry then | |
ed2233dc | 6266 | Error_Msg_NE ("entry & is not overriding", Spec, Subp); |
5d37ba92 | 6267 | else |
ed2233dc | 6268 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
758c442c | 6269 | end if; |
5d37ba92 ES |
6270 | |
6271 | -- If the operation is marked "not overriding" and it's not primitive | |
6272 | -- then an error is issued, unless this is an operation of a task or | |
6273 | -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding" | |
6274 | -- has been specified have already been checked above. | |
6275 | ||
6276 | elsif Must_Not_Override (Spec) | |
6277 | and then not Is_Primitive | |
6278 | and then Ekind (Subp) /= E_Entry | |
6279 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
6280 | then | |
ed2233dc | 6281 | Error_Msg_N |
5d37ba92 ES |
6282 | ("overriding indicator only allowed if subprogram is primitive", |
6283 | Subp); | |
5d37ba92 | 6284 | return; |
fbf5a39b | 6285 | end if; |
758c442c | 6286 | end Check_Overriding_Indicator; |
fbf5a39b | 6287 | |
996ae0b0 RK |
6288 | ------------------- |
6289 | -- Check_Returns -- | |
6290 | ------------------- | |
6291 | ||
0a36105d JM |
6292 | -- Note: this procedure needs to know far too much about how the expander |
6293 | -- messes with exceptions. The use of the flag Exception_Junk and the | |
6294 | -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers | |
6295 | -- works, but is not very clean. It would be better if the expansion | |
6296 | -- routines would leave Original_Node working nicely, and we could use | |
6297 | -- Original_Node here to ignore all the peculiar expander messing ??? | |
6298 | ||
996ae0b0 RK |
6299 | procedure Check_Returns |
6300 | (HSS : Node_Id; | |
6301 | Mode : Character; | |
c8ef728f ES |
6302 | Err : out Boolean; |
6303 | Proc : Entity_Id := Empty) | |
996ae0b0 RK |
6304 | is |
6305 | Handler : Node_Id; | |
6306 | ||
6307 | procedure Check_Statement_Sequence (L : List_Id); | |
6308 | -- Internal recursive procedure to check a list of statements for proper | |
6309 | -- termination by a return statement (or a transfer of control or a | |
6310 | -- compound statement that is itself internally properly terminated). | |
6311 | ||
6312 | ------------------------------ | |
6313 | -- Check_Statement_Sequence -- | |
6314 | ------------------------------ | |
6315 | ||
6316 | procedure Check_Statement_Sequence (L : List_Id) is | |
6317 | Last_Stm : Node_Id; | |
0a36105d | 6318 | Stm : Node_Id; |
996ae0b0 RK |
6319 | Kind : Node_Kind; |
6320 | ||
7b27e183 AC |
6321 | function Assert_False return Boolean; |
6322 | -- Returns True if Last_Stm is a pragma Assert (False) that has been | |
6323 | -- rewritten as a null statement when assertions are off. The assert | |
6324 | -- is not active, but it is still enough to kill the warning. | |
6325 | ||
6326 | ------------------ | |
6327 | -- Assert_False -- | |
6328 | ------------------ | |
6329 | ||
6330 | function Assert_False return Boolean is | |
6331 | Orig : constant Node_Id := Original_Node (Last_Stm); | |
6332 | ||
6333 | begin | |
6334 | if Nkind (Orig) = N_Pragma | |
6e759c2a | 6335 | and then Pragma_Name (Orig) = Name_Assert |
7b27e183 AC |
6336 | and then not Error_Posted (Orig) |
6337 | then | |
6338 | declare | |
6339 | Arg : constant Node_Id := | |
6340 | First (Pragma_Argument_Associations (Orig)); | |
6341 | Exp : constant Node_Id := Expression (Arg); | |
6342 | begin | |
6343 | return Nkind (Exp) = N_Identifier | |
6344 | and then Chars (Exp) = Name_False; | |
6345 | end; | |
6346 | ||
6347 | else | |
6348 | return False; | |
6349 | end if; | |
6350 | end Assert_False; | |
6351 | ||
6352 | -- Local variables | |
6353 | ||
996ae0b0 RK |
6354 | Raise_Exception_Call : Boolean; |
6355 | -- Set True if statement sequence terminated by Raise_Exception call | |
6356 | -- or a Reraise_Occurrence call. | |
6357 | ||
7b27e183 AC |
6358 | -- Start of processing for Check_Statement_Sequence |
6359 | ||
996ae0b0 RK |
6360 | begin |
6361 | Raise_Exception_Call := False; | |
6362 | ||
6363 | -- Get last real statement | |
6364 | ||
6365 | Last_Stm := Last (L); | |
6366 | ||
0a36105d JM |
6367 | -- Deal with digging out exception handler statement sequences that |
6368 | -- have been transformed by the local raise to goto optimization. | |
6369 | -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this | |
6370 | -- optimization has occurred, we are looking at something like: | |
6371 | ||
6372 | -- begin | |
6373 | -- original stmts in block | |
6374 | ||
6375 | -- exception \ | |
6376 | -- when excep1 => | | |
6377 | -- goto L1; | omitted if No_Exception_Propagation | |
6378 | -- when excep2 => | | |
6379 | -- goto L2; / | |
6380 | -- end; | |
6381 | ||
6382 | -- goto L3; -- skip handler when exception not raised | |
6383 | ||
6384 | -- <<L1>> -- target label for local exception | |
6385 | -- begin | |
6386 | -- estmts1 | |
6387 | -- end; | |
6388 | ||
6389 | -- goto L3; | |
6390 | ||
6391 | -- <<L2>> | |
6392 | -- begin | |
6393 | -- estmts2 | |
6394 | -- end; | |
6395 | ||
6396 | -- <<L3>> | |
6397 | ||
6398 | -- and what we have to do is to dig out the estmts1 and estmts2 | |
6399 | -- sequences (which were the original sequences of statements in | |
6400 | -- the exception handlers) and check them. | |
6401 | ||
8fde064e | 6402 | if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then |
0a36105d JM |
6403 | Stm := Last_Stm; |
6404 | loop | |
6405 | Prev (Stm); | |
6406 | exit when No (Stm); | |
6407 | exit when Nkind (Stm) /= N_Block_Statement; | |
6408 | exit when not Exception_Junk (Stm); | |
6409 | Prev (Stm); | |
6410 | exit when No (Stm); | |
6411 | exit when Nkind (Stm) /= N_Label; | |
6412 | exit when not Exception_Junk (Stm); | |
6413 | Check_Statement_Sequence | |
6414 | (Statements (Handled_Statement_Sequence (Next (Stm)))); | |
6415 | ||
6416 | Prev (Stm); | |
6417 | Last_Stm := Stm; | |
6418 | exit when No (Stm); | |
6419 | exit when Nkind (Stm) /= N_Goto_Statement; | |
6420 | exit when not Exception_Junk (Stm); | |
6421 | end loop; | |
6422 | end if; | |
6423 | ||
996ae0b0 RK |
6424 | -- Don't count pragmas |
6425 | ||
6426 | while Nkind (Last_Stm) = N_Pragma | |
6427 | ||
6428 | -- Don't count call to SS_Release (can happen after Raise_Exception) | |
6429 | ||
6430 | or else | |
6431 | (Nkind (Last_Stm) = N_Procedure_Call_Statement | |
6432 | and then | |
6433 | Nkind (Name (Last_Stm)) = N_Identifier | |
6434 | and then | |
6435 | Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release)) | |
6436 | ||
6437 | -- Don't count exception junk | |
6438 | ||
6439 | or else | |
800621e0 RD |
6440 | (Nkind_In (Last_Stm, N_Goto_Statement, |
6441 | N_Label, | |
6442 | N_Object_Declaration) | |
8fde064e | 6443 | and then Exception_Junk (Last_Stm)) |
0a36105d JM |
6444 | or else Nkind (Last_Stm) in N_Push_xxx_Label |
6445 | or else Nkind (Last_Stm) in N_Pop_xxx_Label | |
e3b3266c AC |
6446 | |
6447 | -- Inserted code, such as finalization calls, is irrelevant: we only | |
6448 | -- need to check original source. | |
6449 | ||
6450 | or else Is_Rewrite_Insertion (Last_Stm) | |
996ae0b0 RK |
6451 | loop |
6452 | Prev (Last_Stm); | |
6453 | end loop; | |
6454 | ||
6455 | -- Here we have the "real" last statement | |
6456 | ||
6457 | Kind := Nkind (Last_Stm); | |
6458 | ||
6459 | -- Transfer of control, OK. Note that in the No_Return procedure | |
6460 | -- case, we already diagnosed any explicit return statements, so | |
6461 | -- we can treat them as OK in this context. | |
6462 | ||
6463 | if Is_Transfer (Last_Stm) then | |
6464 | return; | |
6465 | ||
6466 | -- Check cases of explicit non-indirect procedure calls | |
6467 | ||
6468 | elsif Kind = N_Procedure_Call_Statement | |
6469 | and then Is_Entity_Name (Name (Last_Stm)) | |
6470 | then | |
6471 | -- Check call to Raise_Exception procedure which is treated | |
6472 | -- specially, as is a call to Reraise_Occurrence. | |
6473 | ||
6474 | -- We suppress the warning in these cases since it is likely that | |
6475 | -- the programmer really does not expect to deal with the case | |
6476 | -- of Null_Occurrence, and thus would find a warning about a | |
6477 | -- missing return curious, and raising Program_Error does not | |
6478 | -- seem such a bad behavior if this does occur. | |
6479 | ||
c8ef728f ES |
6480 | -- Note that in the Ada 2005 case for Raise_Exception, the actual |
6481 | -- behavior will be to raise Constraint_Error (see AI-329). | |
6482 | ||
996ae0b0 RK |
6483 | if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception) |
6484 | or else | |
6485 | Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence) | |
6486 | then | |
6487 | Raise_Exception_Call := True; | |
6488 | ||
6489 | -- For Raise_Exception call, test first argument, if it is | |
6490 | -- an attribute reference for a 'Identity call, then we know | |
6491 | -- that the call cannot possibly return. | |
6492 | ||
6493 | declare | |
6494 | Arg : constant Node_Id := | |
6495 | Original_Node (First_Actual (Last_Stm)); | |
996ae0b0 RK |
6496 | begin |
6497 | if Nkind (Arg) = N_Attribute_Reference | |
6498 | and then Attribute_Name (Arg) = Name_Identity | |
6499 | then | |
6500 | return; | |
6501 | end if; | |
6502 | end; | |
6503 | end if; | |
6504 | ||
6505 | -- If statement, need to look inside if there is an else and check | |
6506 | -- each constituent statement sequence for proper termination. | |
6507 | ||
6508 | elsif Kind = N_If_Statement | |
6509 | and then Present (Else_Statements (Last_Stm)) | |
6510 | then | |
6511 | Check_Statement_Sequence (Then_Statements (Last_Stm)); | |
6512 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
6513 | ||
6514 | if Present (Elsif_Parts (Last_Stm)) then | |
6515 | declare | |
6516 | Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm)); | |
6517 | ||
6518 | begin | |
6519 | while Present (Elsif_Part) loop | |
6520 | Check_Statement_Sequence (Then_Statements (Elsif_Part)); | |
6521 | Next (Elsif_Part); | |
6522 | end loop; | |
6523 | end; | |
6524 | end if; | |
6525 | ||
6526 | return; | |
6527 | ||
6528 | -- Case statement, check each case for proper termination | |
6529 | ||
6530 | elsif Kind = N_Case_Statement then | |
6531 | declare | |
6532 | Case_Alt : Node_Id; | |
996ae0b0 RK |
6533 | begin |
6534 | Case_Alt := First_Non_Pragma (Alternatives (Last_Stm)); | |
6535 | while Present (Case_Alt) loop | |
6536 | Check_Statement_Sequence (Statements (Case_Alt)); | |
6537 | Next_Non_Pragma (Case_Alt); | |
6538 | end loop; | |
6539 | end; | |
6540 | ||
6541 | return; | |
6542 | ||
6543 | -- Block statement, check its handled sequence of statements | |
6544 | ||
6545 | elsif Kind = N_Block_Statement then | |
6546 | declare | |
6547 | Err1 : Boolean; | |
6548 | ||
6549 | begin | |
6550 | Check_Returns | |
6551 | (Handled_Statement_Sequence (Last_Stm), Mode, Err1); | |
6552 | ||
6553 | if Err1 then | |
6554 | Err := True; | |
6555 | end if; | |
6556 | ||
6557 | return; | |
6558 | end; | |
6559 | ||
6560 | -- Loop statement. If there is an iteration scheme, we can definitely | |
6561 | -- fall out of the loop. Similarly if there is an exit statement, we | |
6562 | -- can fall out. In either case we need a following return. | |
6563 | ||
6564 | elsif Kind = N_Loop_Statement then | |
6565 | if Present (Iteration_Scheme (Last_Stm)) | |
6566 | or else Has_Exit (Entity (Identifier (Last_Stm))) | |
6567 | then | |
6568 | null; | |
6569 | ||
f3d57416 RW |
6570 | -- A loop with no exit statement or iteration scheme is either |
6571 | -- an infinite loop, or it has some other exit (raise/return). | |
996ae0b0 RK |
6572 | -- In either case, no warning is required. |
6573 | ||
6574 | else | |
6575 | return; | |
6576 | end if; | |
6577 | ||
6578 | -- Timed entry call, check entry call and delay alternatives | |
6579 | ||
6580 | -- Note: in expanded code, the timed entry call has been converted | |
6581 | -- to a set of expanded statements on which the check will work | |
6582 | -- correctly in any case. | |
6583 | ||
6584 | elsif Kind = N_Timed_Entry_Call then | |
6585 | declare | |
6586 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6587 | DCA : constant Node_Id := Delay_Alternative (Last_Stm); | |
6588 | ||
6589 | begin | |
6590 | -- If statement sequence of entry call alternative is missing, | |
6591 | -- then we can definitely fall through, and we post the error | |
6592 | -- message on the entry call alternative itself. | |
6593 | ||
6594 | if No (Statements (ECA)) then | |
6595 | Last_Stm := ECA; | |
6596 | ||
6597 | -- If statement sequence of delay alternative is missing, then | |
6598 | -- we can definitely fall through, and we post the error | |
6599 | -- message on the delay alternative itself. | |
6600 | ||
6601 | -- Note: if both ECA and DCA are missing the return, then we | |
6602 | -- post only one message, should be enough to fix the bugs. | |
6603 | -- If not we will get a message next time on the DCA when the | |
a90bd866 | 6604 | -- ECA is fixed. |
996ae0b0 RK |
6605 | |
6606 | elsif No (Statements (DCA)) then | |
6607 | Last_Stm := DCA; | |
6608 | ||
6609 | -- Else check both statement sequences | |
6610 | ||
6611 | else | |
6612 | Check_Statement_Sequence (Statements (ECA)); | |
6613 | Check_Statement_Sequence (Statements (DCA)); | |
6614 | return; | |
6615 | end if; | |
6616 | end; | |
6617 | ||
6618 | -- Conditional entry call, check entry call and else part | |
6619 | ||
6620 | -- Note: in expanded code, the conditional entry call has been | |
6621 | -- converted to a set of expanded statements on which the check | |
6622 | -- will work correctly in any case. | |
6623 | ||
6624 | elsif Kind = N_Conditional_Entry_Call then | |
6625 | declare | |
6626 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
6627 | ||
6628 | begin | |
6629 | -- If statement sequence of entry call alternative is missing, | |
6630 | -- then we can definitely fall through, and we post the error | |
6631 | -- message on the entry call alternative itself. | |
6632 | ||
6633 | if No (Statements (ECA)) then | |
6634 | Last_Stm := ECA; | |
6635 | ||
6636 | -- Else check statement sequence and else part | |
6637 | ||
6638 | else | |
6639 | Check_Statement_Sequence (Statements (ECA)); | |
6640 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
6641 | return; | |
6642 | end if; | |
6643 | end; | |
6644 | end if; | |
6645 | ||
6646 | -- If we fall through, issue appropriate message | |
6647 | ||
6648 | if Mode = 'F' then | |
7b27e183 AC |
6649 | |
6650 | -- Kill warning if last statement is a raise exception call, | |
6651 | -- or a pragma Assert (False). Note that with assertions enabled, | |
6652 | -- such a pragma has been converted into a raise exception call | |
6653 | -- already, so the Assert_False is for the assertions off case. | |
6654 | ||
6655 | if not Raise_Exception_Call and then not Assert_False then | |
b465ef6f AC |
6656 | |
6657 | -- In GNATprove mode, it is an error to have a missing return | |
6658 | ||
43417b90 | 6659 | Error_Msg_Warn := SPARK_Mode /= On; |
7b27e183 AC |
6660 | |
6661 | -- Issue error message or warning | |
6662 | ||
4a28b181 AC |
6663 | Error_Msg_N |
6664 | ("RETURN statement missing following this statement<<!", | |
6665 | Last_Stm); | |
6666 | Error_Msg_N | |
6667 | ("\Program_Error ]<<!", Last_Stm); | |
996ae0b0 RK |
6668 | end if; |
6669 | ||
6670 | -- Note: we set Err even though we have not issued a warning | |
6671 | -- because we still have a case of a missing return. This is | |
6672 | -- an extremely marginal case, probably will never be noticed | |
6673 | -- but we might as well get it right. | |
6674 | ||
6675 | Err := True; | |
6676 | ||
c8ef728f ES |
6677 | -- Otherwise we have the case of a procedure marked No_Return |
6678 | ||
996ae0b0 | 6679 | else |
800621e0 | 6680 | if not Raise_Exception_Call then |
4a28b181 AC |
6681 | if GNATprove_Mode then |
6682 | Error_Msg_N | |
6683 | ("implied return after this statement " | |
6684 | & "would have raised Program_Error", Last_Stm); | |
6685 | else | |
6686 | Error_Msg_N | |
6687 | ("implied return after this statement " | |
6688 | & "will raise Program_Error??", Last_Stm); | |
6689 | end if; | |
6690 | ||
43417b90 | 6691 | Error_Msg_Warn := SPARK_Mode /= On; |
800621e0 | 6692 | Error_Msg_NE |
4a28b181 | 6693 | ("\procedure & is marked as No_Return<<!", Last_Stm, Proc); |
800621e0 | 6694 | end if; |
c8ef728f ES |
6695 | |
6696 | declare | |
6697 | RE : constant Node_Id := | |
6698 | Make_Raise_Program_Error (Sloc (Last_Stm), | |
6699 | Reason => PE_Implicit_Return); | |
6700 | begin | |
6701 | Insert_After (Last_Stm, RE); | |
6702 | Analyze (RE); | |
6703 | end; | |
996ae0b0 RK |
6704 | end if; |
6705 | end Check_Statement_Sequence; | |
6706 | ||
6707 | -- Start of processing for Check_Returns | |
6708 | ||
6709 | begin | |
6710 | Err := False; | |
6711 | Check_Statement_Sequence (Statements (HSS)); | |
6712 | ||
6713 | if Present (Exception_Handlers (HSS)) then | |
6714 | Handler := First_Non_Pragma (Exception_Handlers (HSS)); | |
6715 | while Present (Handler) loop | |
6716 | Check_Statement_Sequence (Statements (Handler)); | |
6717 | Next_Non_Pragma (Handler); | |
6718 | end loop; | |
6719 | end if; | |
6720 | end Check_Returns; | |
6721 | ||
6722 | ---------------------------- | |
6723 | -- Check_Subprogram_Order -- | |
6724 | ---------------------------- | |
6725 | ||
6726 | procedure Check_Subprogram_Order (N : Node_Id) is | |
6727 | ||
6728 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean; | |
dc36a7e3 RD |
6729 | -- This is used to check if S1 > S2 in the sense required by this test, |
6730 | -- for example nameab < namec, but name2 < name10. | |
996ae0b0 | 6731 | |
82c80734 RD |
6732 | ----------------------------- |
6733 | -- Subprogram_Name_Greater -- | |
6734 | ----------------------------- | |
6735 | ||
996ae0b0 RK |
6736 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean is |
6737 | L1, L2 : Positive; | |
6738 | N1, N2 : Natural; | |
6739 | ||
6740 | begin | |
67336960 AC |
6741 | -- Deal with special case where names are identical except for a |
6742 | -- numerical suffix. These are handled specially, taking the numeric | |
6743 | -- ordering from the suffix into account. | |
996ae0b0 RK |
6744 | |
6745 | L1 := S1'Last; | |
6746 | while S1 (L1) in '0' .. '9' loop | |
6747 | L1 := L1 - 1; | |
6748 | end loop; | |
6749 | ||
6750 | L2 := S2'Last; | |
6751 | while S2 (L2) in '0' .. '9' loop | |
6752 | L2 := L2 - 1; | |
6753 | end loop; | |
6754 | ||
67336960 | 6755 | -- If non-numeric parts non-equal, do straight compare |
996ae0b0 | 6756 | |
67336960 AC |
6757 | if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then |
6758 | return S1 > S2; | |
996ae0b0 RK |
6759 | |
6760 | -- If non-numeric parts equal, compare suffixed numeric parts. Note | |
6761 | -- that a missing suffix is treated as numeric zero in this test. | |
6762 | ||
6763 | else | |
6764 | N1 := 0; | |
6765 | while L1 < S1'Last loop | |
6766 | L1 := L1 + 1; | |
6767 | N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0'); | |
6768 | end loop; | |
6769 | ||
6770 | N2 := 0; | |
6771 | while L2 < S2'Last loop | |
6772 | L2 := L2 + 1; | |
6773 | N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0'); | |
6774 | end loop; | |
6775 | ||
6776 | return N1 > N2; | |
6777 | end if; | |
6778 | end Subprogram_Name_Greater; | |
6779 | ||
6780 | -- Start of processing for Check_Subprogram_Order | |
6781 | ||
6782 | begin | |
6783 | -- Check body in alpha order if this is option | |
6784 | ||
fbf5a39b | 6785 | if Style_Check |
bc202b70 | 6786 | and then Style_Check_Order_Subprograms |
996ae0b0 RK |
6787 | and then Nkind (N) = N_Subprogram_Body |
6788 | and then Comes_From_Source (N) | |
6789 | and then In_Extended_Main_Source_Unit (N) | |
6790 | then | |
6791 | declare | |
6792 | LSN : String_Ptr | |
6793 | renames Scope_Stack.Table | |
6794 | (Scope_Stack.Last).Last_Subprogram_Name; | |
6795 | ||
6796 | Body_Id : constant Entity_Id := | |
6797 | Defining_Entity (Specification (N)); | |
6798 | ||
6799 | begin | |
6800 | Get_Decoded_Name_String (Chars (Body_Id)); | |
6801 | ||
6802 | if LSN /= null then | |
6803 | if Subprogram_Name_Greater | |
6804 | (LSN.all, Name_Buffer (1 .. Name_Len)) | |
6805 | then | |
6806 | Style.Subprogram_Not_In_Alpha_Order (Body_Id); | |
6807 | end if; | |
6808 | ||
6809 | Free (LSN); | |
6810 | end if; | |
6811 | ||
6812 | LSN := new String'(Name_Buffer (1 .. Name_Len)); | |
6813 | end; | |
6814 | end if; | |
6815 | end Check_Subprogram_Order; | |
6816 | ||
6817 | ------------------------------ | |
6818 | -- Check_Subtype_Conformant -- | |
6819 | ------------------------------ | |
6820 | ||
6821 | procedure Check_Subtype_Conformant | |
ce2b6ba5 JM |
6822 | (New_Id : Entity_Id; |
6823 | Old_Id : Entity_Id; | |
6824 | Err_Loc : Node_Id := Empty; | |
f307415a AC |
6825 | Skip_Controlling_Formals : Boolean := False; |
6826 | Get_Inst : Boolean := False) | |
996ae0b0 RK |
6827 | is |
6828 | Result : Boolean; | |
81db9d77 | 6829 | pragma Warnings (Off, Result); |
996ae0b0 RK |
6830 | begin |
6831 | Check_Conformance | |
ce2b6ba5 | 6832 | (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc, |
f307415a AC |
6833 | Skip_Controlling_Formals => Skip_Controlling_Formals, |
6834 | Get_Inst => Get_Inst); | |
996ae0b0 RK |
6835 | end Check_Subtype_Conformant; |
6836 | ||
42f11e4c AC |
6837 | ----------------------------------- |
6838 | -- Check_Synchronized_Overriding -- | |
6839 | ----------------------------------- | |
6840 | ||
6841 | procedure Check_Synchronized_Overriding | |
6842 | (Def_Id : Entity_Id; | |
6843 | Overridden_Subp : out Entity_Id) | |
6844 | is | |
6845 | Ifaces_List : Elist_Id; | |
6846 | In_Scope : Boolean; | |
6847 | Typ : Entity_Id; | |
6848 | ||
6849 | function Matches_Prefixed_View_Profile | |
6850 | (Prim_Params : List_Id; | |
6851 | Iface_Params : List_Id) return Boolean; | |
6852 | -- Determine whether a subprogram's parameter profile Prim_Params | |
6853 | -- matches that of a potentially overridden interface subprogram | |
6854 | -- Iface_Params. Also determine if the type of first parameter of | |
6855 | -- Iface_Params is an implemented interface. | |
6856 | ||
6857 | ----------------------------------- | |
6858 | -- Matches_Prefixed_View_Profile -- | |
6859 | ----------------------------------- | |
6860 | ||
6861 | function Matches_Prefixed_View_Profile | |
6862 | (Prim_Params : List_Id; | |
6863 | Iface_Params : List_Id) return Boolean | |
6864 | is | |
42f11e4c AC |
6865 | function Is_Implemented |
6866 | (Ifaces_List : Elist_Id; | |
6867 | Iface : Entity_Id) return Boolean; | |
6868 | -- Determine if Iface is implemented by the current task or | |
6869 | -- protected type. | |
6870 | ||
6871 | -------------------- | |
6872 | -- Is_Implemented -- | |
6873 | -------------------- | |
6874 | ||
6875 | function Is_Implemented | |
6876 | (Ifaces_List : Elist_Id; | |
6877 | Iface : Entity_Id) return Boolean | |
6878 | is | |
6879 | Iface_Elmt : Elmt_Id; | |
6880 | ||
6881 | begin | |
6882 | Iface_Elmt := First_Elmt (Ifaces_List); | |
6883 | while Present (Iface_Elmt) loop | |
6884 | if Node (Iface_Elmt) = Iface then | |
6885 | return True; | |
6886 | end if; | |
6887 | ||
6888 | Next_Elmt (Iface_Elmt); | |
6889 | end loop; | |
6890 | ||
6891 | return False; | |
6892 | end Is_Implemented; | |
6893 | ||
bac5ba15 AC |
6894 | -- Local variables |
6895 | ||
6896 | Iface_Id : Entity_Id; | |
6897 | Iface_Param : Node_Id; | |
6898 | Iface_Typ : Entity_Id; | |
6899 | Prim_Id : Entity_Id; | |
6900 | Prim_Param : Node_Id; | |
6901 | Prim_Typ : Entity_Id; | |
6902 | ||
42f11e4c AC |
6903 | -- Start of processing for Matches_Prefixed_View_Profile |
6904 | ||
6905 | begin | |
6906 | Iface_Param := First (Iface_Params); | |
6907 | Iface_Typ := Etype (Defining_Identifier (Iface_Param)); | |
6908 | ||
6909 | if Is_Access_Type (Iface_Typ) then | |
6910 | Iface_Typ := Designated_Type (Iface_Typ); | |
6911 | end if; | |
6912 | ||
6913 | Prim_Param := First (Prim_Params); | |
6914 | ||
bac5ba15 AC |
6915 | -- The first parameter of the potentially overridden subprogram must |
6916 | -- be an interface implemented by Prim. | |
42f11e4c AC |
6917 | |
6918 | if not Is_Interface (Iface_Typ) | |
6919 | or else not Is_Implemented (Ifaces_List, Iface_Typ) | |
6920 | then | |
6921 | return False; | |
6922 | end if; | |
6923 | ||
ca90b962 GD |
6924 | -- The checks on the object parameters are done, so move on to the |
6925 | -- rest of the parameters. | |
42f11e4c AC |
6926 | |
6927 | if not In_Scope then | |
6928 | Prim_Param := Next (Prim_Param); | |
6929 | end if; | |
6930 | ||
6931 | Iface_Param := Next (Iface_Param); | |
6932 | while Present (Iface_Param) and then Present (Prim_Param) loop | |
6933 | Iface_Id := Defining_Identifier (Iface_Param); | |
6934 | Iface_Typ := Find_Parameter_Type (Iface_Param); | |
6935 | ||
6936 | Prim_Id := Defining_Identifier (Prim_Param); | |
6937 | Prim_Typ := Find_Parameter_Type (Prim_Param); | |
6938 | ||
6939 | if Ekind (Iface_Typ) = E_Anonymous_Access_Type | |
6940 | and then Ekind (Prim_Typ) = E_Anonymous_Access_Type | |
6941 | and then Is_Concurrent_Type (Designated_Type (Prim_Typ)) | |
6942 | then | |
6943 | Iface_Typ := Designated_Type (Iface_Typ); | |
bac5ba15 | 6944 | Prim_Typ := Designated_Type (Prim_Typ); |
42f11e4c AC |
6945 | end if; |
6946 | ||
6947 | -- Case of multiple interface types inside a parameter profile | |
6948 | ||
6949 | -- (Obj_Param : in out Iface; ...; Param : Iface) | |
6950 | ||
bac5ba15 AC |
6951 | -- If the interface type is implemented, then the matching type in |
6952 | -- the primitive should be the implementing record type. | |
42f11e4c AC |
6953 | |
6954 | if Ekind (Iface_Typ) = E_Record_Type | |
6955 | and then Is_Interface (Iface_Typ) | |
6956 | and then Is_Implemented (Ifaces_List, Iface_Typ) | |
6957 | then | |
6958 | if Prim_Typ /= Typ then | |
6959 | return False; | |
6960 | end if; | |
6961 | ||
6962 | -- The two parameters must be both mode and subtype conformant | |
6963 | ||
6964 | elsif Ekind (Iface_Id) /= Ekind (Prim_Id) | |
6965 | or else not | |
6966 | Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant) | |
6967 | then | |
6968 | return False; | |
6969 | end if; | |
6970 | ||
6971 | Next (Iface_Param); | |
6972 | Next (Prim_Param); | |
6973 | end loop; | |
6974 | ||
6975 | -- One of the two lists contains more parameters than the other | |
6976 | ||
6977 | if Present (Iface_Param) or else Present (Prim_Param) then | |
6978 | return False; | |
6979 | end if; | |
6980 | ||
6981 | return True; | |
6982 | end Matches_Prefixed_View_Profile; | |
6983 | ||
6984 | -- Start of processing for Check_Synchronized_Overriding | |
6985 | ||
6986 | begin | |
6987 | Overridden_Subp := Empty; | |
6988 | ||
6989 | -- Def_Id must be an entry or a subprogram. We should skip predefined | |
ca90b962 | 6990 | -- primitives internally generated by the front end; however at this |
42f11e4c AC |
6991 | -- stage predefined primitives are still not fully decorated. As a |
6992 | -- minor optimization we skip here internally generated subprograms. | |
6993 | ||
6994 | if (Ekind (Def_Id) /= E_Entry | |
6995 | and then Ekind (Def_Id) /= E_Function | |
6996 | and then Ekind (Def_Id) /= E_Procedure) | |
6997 | or else not Comes_From_Source (Def_Id) | |
6998 | then | |
6999 | return; | |
7000 | end if; | |
7001 | ||
bac5ba15 AC |
7002 | -- Search for the concurrent declaration since it contains the list of |
7003 | -- all implemented interfaces. In this case, the subprogram is declared | |
7004 | -- within the scope of a protected or a task type. | |
42f11e4c AC |
7005 | |
7006 | if Present (Scope (Def_Id)) | |
7007 | and then Is_Concurrent_Type (Scope (Def_Id)) | |
7008 | and then not Is_Generic_Actual_Type (Scope (Def_Id)) | |
7009 | then | |
7010 | Typ := Scope (Def_Id); | |
7011 | In_Scope := True; | |
7012 | ||
7013 | -- The enclosing scope is not a synchronized type and the subprogram | |
7014 | -- has no formals. | |
7015 | ||
7016 | elsif No (First_Formal (Def_Id)) then | |
7017 | return; | |
7018 | ||
7019 | -- The subprogram has formals and hence it may be a primitive of a | |
7020 | -- concurrent type. | |
7021 | ||
7022 | else | |
7023 | Typ := Etype (First_Formal (Def_Id)); | |
7024 | ||
7025 | if Is_Access_Type (Typ) then | |
7026 | Typ := Directly_Designated_Type (Typ); | |
7027 | end if; | |
7028 | ||
7029 | if Is_Concurrent_Type (Typ) | |
7030 | and then not Is_Generic_Actual_Type (Typ) | |
7031 | then | |
7032 | In_Scope := False; | |
7033 | ||
bac5ba15 AC |
7034 | -- This case occurs when the concurrent type is declared within a |
7035 | -- generic unit. As a result the corresponding record has been built | |
7036 | -- and used as the type of the first formal, we just have to retrieve | |
7037 | -- the corresponding concurrent type. | |
42f11e4c AC |
7038 | |
7039 | elsif Is_Concurrent_Record_Type (Typ) | |
7040 | and then not Is_Class_Wide_Type (Typ) | |
7041 | and then Present (Corresponding_Concurrent_Type (Typ)) | |
7042 | then | |
7043 | Typ := Corresponding_Concurrent_Type (Typ); | |
7044 | In_Scope := False; | |
7045 | ||
7046 | else | |
7047 | return; | |
7048 | end if; | |
7049 | end if; | |
7050 | ||
ca90b962 GD |
7051 | -- There is no overriding to check if this is an inherited operation in |
7052 | -- a type derivation for a generic actual. | |
42f11e4c AC |
7053 | |
7054 | Collect_Interfaces (Typ, Ifaces_List); | |
7055 | ||
7056 | if Is_Empty_Elmt_List (Ifaces_List) then | |
7057 | return; | |
7058 | end if; | |
7059 | ||
7060 | -- Determine whether entry or subprogram Def_Id overrides a primitive | |
7061 | -- operation that belongs to one of the interfaces in Ifaces_List. | |
7062 | ||
7063 | declare | |
7064 | Candidate : Entity_Id := Empty; | |
7065 | Hom : Entity_Id := Empty; | |
7066 | Subp : Entity_Id := Empty; | |
7067 | ||
7068 | begin | |
bac5ba15 AC |
7069 | -- Traverse the homonym chain, looking for a potentially overridden |
7070 | -- subprogram that belongs to an implemented interface. | |
42f11e4c AC |
7071 | |
7072 | Hom := Current_Entity_In_Scope (Def_Id); | |
7073 | while Present (Hom) loop | |
7074 | Subp := Hom; | |
7075 | ||
7076 | if Subp = Def_Id | |
7077 | or else not Is_Overloadable (Subp) | |
7078 | or else not Is_Primitive (Subp) | |
7079 | or else not Is_Dispatching_Operation (Subp) | |
7080 | or else not Present (Find_Dispatching_Type (Subp)) | |
7081 | or else not Is_Interface (Find_Dispatching_Type (Subp)) | |
7082 | then | |
7083 | null; | |
7084 | ||
bac5ba15 AC |
7085 | -- Entries and procedures can override abstract or null interface |
7086 | -- procedures. | |
42f11e4c | 7087 | |
bac5ba15 | 7088 | elsif Ekind_In (Def_Id, E_Entry, E_Procedure) |
42f11e4c AC |
7089 | and then Ekind (Subp) = E_Procedure |
7090 | and then Matches_Prefixed_View_Profile | |
7091 | (Parameter_Specifications (Parent (Def_Id)), | |
7092 | Parameter_Specifications (Parent (Subp))) | |
7093 | then | |
7094 | Candidate := Subp; | |
7095 | ||
7096 | -- For an overridden subprogram Subp, check whether the mode | |
bac5ba15 AC |
7097 | -- of its first parameter is correct depending on the kind of |
7098 | -- synchronized type. | |
42f11e4c AC |
7099 | |
7100 | declare | |
7101 | Formal : constant Node_Id := First_Formal (Candidate); | |
7102 | ||
7103 | begin | |
7104 | -- In order for an entry or a protected procedure to | |
bac5ba15 | 7105 | -- override, the first parameter of the overridden routine |
ca90b962 | 7106 | -- must be of mode "out", "in out", or access-to-variable. |
42f11e4c AC |
7107 | |
7108 | if Ekind_In (Candidate, E_Entry, E_Procedure) | |
7109 | and then Is_Protected_Type (Typ) | |
7110 | and then Ekind (Formal) /= E_In_Out_Parameter | |
7111 | and then Ekind (Formal) /= E_Out_Parameter | |
7112 | and then Nkind (Parameter_Type (Parent (Formal))) /= | |
7113 | N_Access_Definition | |
7114 | then | |
7115 | null; | |
7116 | ||
bac5ba15 AC |
7117 | -- All other cases are OK since a task entry or routine does |
7118 | -- not have a restriction on the mode of the first parameter | |
7119 | -- of the overridden interface routine. | |
42f11e4c AC |
7120 | |
7121 | else | |
7122 | Overridden_Subp := Candidate; | |
7123 | return; | |
7124 | end if; | |
7125 | end; | |
7126 | ||
7127 | -- Functions can override abstract interface functions | |
7128 | ||
7129 | elsif Ekind (Def_Id) = E_Function | |
7130 | and then Ekind (Subp) = E_Function | |
7131 | and then Matches_Prefixed_View_Profile | |
7132 | (Parameter_Specifications (Parent (Def_Id)), | |
7133 | Parameter_Specifications (Parent (Subp))) | |
ca90b962 | 7134 | and then Etype (Def_Id) = Etype (Subp) |
42f11e4c AC |
7135 | then |
7136 | Candidate := Subp; | |
7137 | ||
7138 | -- If an inherited subprogram is implemented by a protected | |
7139 | -- function, then the first parameter of the inherited | |
bac5ba15 | 7140 | -- subprogram shall be of mode in, but not an access-to- |
ca90b962 | 7141 | -- variable parameter (RM 9.4(11/9)). |
42f11e4c AC |
7142 | |
7143 | if Present (First_Formal (Subp)) | |
7144 | and then Ekind (First_Formal (Subp)) = E_In_Parameter | |
7145 | and then | |
7146 | (not Is_Access_Type (Etype (First_Formal (Subp))) | |
7147 | or else | |
7148 | Is_Access_Constant (Etype (First_Formal (Subp)))) | |
7149 | then | |
7150 | Overridden_Subp := Subp; | |
7151 | return; | |
7152 | end if; | |
7153 | end if; | |
7154 | ||
7155 | Hom := Homonym (Hom); | |
7156 | end loop; | |
7157 | ||
7158 | -- After examining all candidates for overriding, we are left with | |
ca90b962 | 7159 | -- the best match, which is a mode-incompatible interface routine. |
42f11e4c AC |
7160 | |
7161 | if In_Scope and then Present (Candidate) then | |
7162 | Error_Msg_PT (Def_Id, Candidate); | |
7163 | end if; | |
7164 | ||
7165 | Overridden_Subp := Candidate; | |
7166 | return; | |
7167 | end; | |
7168 | end Check_Synchronized_Overriding; | |
7169 | ||
996ae0b0 RK |
7170 | --------------------------- |
7171 | -- Check_Type_Conformant -- | |
7172 | --------------------------- | |
7173 | ||
7174 | procedure Check_Type_Conformant | |
7175 | (New_Id : Entity_Id; | |
7176 | Old_Id : Entity_Id; | |
7177 | Err_Loc : Node_Id := Empty) | |
7178 | is | |
7179 | Result : Boolean; | |
81db9d77 | 7180 | pragma Warnings (Off, Result); |
996ae0b0 RK |
7181 | begin |
7182 | Check_Conformance | |
7183 | (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc); | |
7184 | end Check_Type_Conformant; | |
7185 | ||
806f6d37 AC |
7186 | --------------------------- |
7187 | -- Can_Override_Operator -- | |
7188 | --------------------------- | |
7189 | ||
7190 | function Can_Override_Operator (Subp : Entity_Id) return Boolean is | |
7191 | Typ : Entity_Id; | |
f146302c | 7192 | |
806f6d37 AC |
7193 | begin |
7194 | if Nkind (Subp) /= N_Defining_Operator_Symbol then | |
7195 | return False; | |
7196 | ||
7197 | else | |
7198 | Typ := Base_Type (Etype (First_Formal (Subp))); | |
7199 | ||
f146302c AC |
7200 | -- Check explicitly that the operation is a primitive of the type |
7201 | ||
806f6d37 | 7202 | return Operator_Matches_Spec (Subp, Subp) |
f146302c | 7203 | and then not Is_Generic_Type (Typ) |
806f6d37 AC |
7204 | and then Scope (Subp) = Scope (Typ) |
7205 | and then not Is_Class_Wide_Type (Typ); | |
7206 | end if; | |
7207 | end Can_Override_Operator; | |
7208 | ||
996ae0b0 RK |
7209 | ---------------------- |
7210 | -- Conforming_Types -- | |
7211 | ---------------------- | |
7212 | ||
7213 | function Conforming_Types | |
7214 | (T1 : Entity_Id; | |
7215 | T2 : Entity_Id; | |
7216 | Ctype : Conformance_Type; | |
d05ef0ab | 7217 | Get_Inst : Boolean := False) return Boolean |
996ae0b0 | 7218 | is |
0310af44 AC |
7219 | function Base_Types_Match |
7220 | (Typ_1 : Entity_Id; | |
7221 | Typ_2 : Entity_Id) return Boolean; | |
7222 | -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are | |
7223 | -- in different scopes (e.g. parent and child instances), then verify | |
7224 | -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on | |
7225 | -- the same subtype chain. The whole purpose of this procedure is to | |
7226 | -- prevent spurious ambiguities in an instantiation that may arise if | |
7227 | -- two distinct generic types are instantiated with the same actual. | |
7228 | ||
7229 | function Find_Designated_Type (Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
7230 | -- An access parameter can designate an incomplete type. If the |
7231 | -- incomplete type is the limited view of a type from a limited_ | |
0310af44 AC |
7232 | -- with_clause, check whether the non-limited view is available. |
7233 | -- If it is a (non-limited) incomplete type, get the full view. | |
7234 | ||
7235 | function Matches_Limited_With_View | |
7236 | (Typ_1 : Entity_Id; | |
7237 | Typ_2 : Entity_Id) return Boolean; | |
7238 | -- Returns True if and only if either Typ_1 denotes a limited view of | |
7239 | -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when | |
7240 | -- the limited with view of a type is used in a subprogram declaration | |
7241 | -- and the subprogram body is in the scope of a regular with clause for | |
7242 | -- the same unit. In such a case, the two type entities are considered | |
0a36105d | 7243 | -- identical for purposes of conformance checking. |
996ae0b0 RK |
7244 | |
7245 | ---------------------- | |
7246 | -- Base_Types_Match -- | |
7247 | ---------------------- | |
7248 | ||
0310af44 AC |
7249 | function Base_Types_Match |
7250 | (Typ_1 : Entity_Id; | |
7251 | Typ_2 : Entity_Id) return Boolean | |
7252 | is | |
7253 | Base_1 : constant Entity_Id := Base_Type (Typ_1); | |
7254 | Base_2 : constant Entity_Id := Base_Type (Typ_2); | |
8fde064e | 7255 | |
996ae0b0 | 7256 | begin |
0310af44 | 7257 | if Typ_1 = Typ_2 then |
996ae0b0 RK |
7258 | return True; |
7259 | ||
0310af44 | 7260 | elsif Base_1 = Base_2 then |
996ae0b0 | 7261 | |
0a36105d | 7262 | -- The following is too permissive. A more precise test should |
996ae0b0 RK |
7263 | -- check that the generic actual is an ancestor subtype of the |
7264 | -- other ???. | |
586ecbf3 | 7265 | |
70f4ad20 AC |
7266 | -- See code in Find_Corresponding_Spec that applies an additional |
7267 | -- filter to handle accidental amiguities in instances. | |
996ae0b0 | 7268 | |
0310af44 AC |
7269 | return |
7270 | not Is_Generic_Actual_Type (Typ_1) | |
7271 | or else not Is_Generic_Actual_Type (Typ_2) | |
7272 | or else Scope (Typ_1) /= Scope (Typ_2); | |
996ae0b0 | 7273 | |
0310af44 | 7274 | -- If Typ_2 is a generic actual type it is declared as the subtype of |
2995860f AC |
7275 | -- the actual. If that actual is itself a subtype we need to use its |
7276 | -- own base type to check for compatibility. | |
8fde064e | 7277 | |
0310af44 AC |
7278 | elsif Ekind (Base_2) = Ekind (Typ_2) |
7279 | and then Base_1 = Base_Type (Base_2) | |
7280 | then | |
8fde064e AC |
7281 | return True; |
7282 | ||
0310af44 AC |
7283 | elsif Ekind (Base_1) = Ekind (Typ_1) |
7284 | and then Base_2 = Base_Type (Base_1) | |
7285 | then | |
8fde064e AC |
7286 | return True; |
7287 | ||
0a36105d JM |
7288 | else |
7289 | return False; | |
7290 | end if; | |
7291 | end Base_Types_Match; | |
aa720a54 | 7292 | |
5d37ba92 ES |
7293 | -------------------------- |
7294 | -- Find_Designated_Type -- | |
7295 | -------------------------- | |
7296 | ||
0310af44 | 7297 | function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is |
5d37ba92 ES |
7298 | Desig : Entity_Id; |
7299 | ||
7300 | begin | |
0310af44 | 7301 | Desig := Directly_Designated_Type (Typ); |
5d37ba92 ES |
7302 | |
7303 | if Ekind (Desig) = E_Incomplete_Type then | |
7304 | ||
7305 | -- If regular incomplete type, get full view if available | |
7306 | ||
7307 | if Present (Full_View (Desig)) then | |
7308 | Desig := Full_View (Desig); | |
7309 | ||
7310 | -- If limited view of a type, get non-limited view if available, | |
7311 | -- and check again for a regular incomplete type. | |
7312 | ||
7313 | elsif Present (Non_Limited_View (Desig)) then | |
7314 | Desig := Get_Full_View (Non_Limited_View (Desig)); | |
7315 | end if; | |
7316 | end if; | |
7317 | ||
7318 | return Desig; | |
7319 | end Find_Designated_Type; | |
7320 | ||
0a36105d JM |
7321 | ------------------------------- |
7322 | -- Matches_Limited_With_View -- | |
7323 | ------------------------------- | |
7324 | ||
0310af44 AC |
7325 | function Matches_Limited_With_View |
7326 | (Typ_1 : Entity_Id; | |
7327 | Typ_2 : Entity_Id) return Boolean | |
7328 | is | |
7329 | function Is_Matching_Limited_View | |
7330 | (Typ : Entity_Id; | |
7331 | View : Entity_Id) return Boolean; | |
7332 | -- Determine whether non-limited view View denotes type Typ in some | |
7333 | -- conformant fashion. | |
7334 | ||
7335 | ------------------------------ | |
7336 | -- Is_Matching_Limited_View -- | |
7337 | ------------------------------ | |
7338 | ||
7339 | function Is_Matching_Limited_View | |
7340 | (Typ : Entity_Id; | |
7341 | View : Entity_Id) return Boolean | |
7342 | is | |
7343 | Root_Typ : Entity_Id; | |
7344 | Root_View : Entity_Id; | |
7345 | ||
7346 | begin | |
7347 | -- The non-limited view directly denotes the type | |
7348 | ||
7349 | if Typ = View then | |
7350 | return True; | |
7351 | ||
7352 | -- The type is a subtype of the non-limited view | |
7353 | ||
7354 | elsif Is_Subtype_Of (Typ, View) then | |
7355 | return True; | |
7356 | ||
7357 | -- Both the non-limited view and the type denote class-wide types | |
7358 | ||
7359 | elsif Is_Class_Wide_Type (Typ) | |
7360 | and then Is_Class_Wide_Type (View) | |
7361 | then | |
7362 | Root_Typ := Root_Type (Typ); | |
7363 | Root_View := Root_Type (View); | |
7364 | ||
7365 | if Root_Typ = Root_View then | |
7366 | return True; | |
7367 | ||
7368 | -- An incomplete tagged type and its full view may receive two | |
7369 | -- distinct class-wide types when the related package has not | |
7370 | -- been analyzed yet. | |
7371 | ||
7372 | -- package Pack is | |
7373 | -- type T is tagged; -- CW_1 | |
7374 | -- type T is tagged null record; -- CW_2 | |
7375 | -- end Pack; | |
7376 | ||
7377 | -- This is because the package lacks any semantic information | |
7378 | -- that may eventually link both views of T. As a consequence, | |
7379 | -- a client of the limited view of Pack will see CW_2 while a | |
7380 | -- client of the non-limited view of Pack will see CW_1. | |
7381 | ||
7382 | elsif Is_Incomplete_Type (Root_Typ) | |
7383 | and then Present (Full_View (Root_Typ)) | |
7384 | and then Full_View (Root_Typ) = Root_View | |
7385 | then | |
7386 | return True; | |
7387 | ||
7388 | elsif Is_Incomplete_Type (Root_View) | |
7389 | and then Present (Full_View (Root_View)) | |
7390 | and then Full_View (Root_View) = Root_Typ | |
7391 | then | |
7392 | return True; | |
7393 | end if; | |
7394 | end if; | |
7395 | ||
7396 | return False; | |
7397 | end Is_Matching_Limited_View; | |
7398 | ||
7399 | -- Start of processing for Matches_Limited_With_View | |
7400 | ||
0a36105d JM |
7401 | begin |
7402 | -- In some cases a type imported through a limited_with clause, and | |
0310af44 | 7403 | -- its non-limited view are both visible, for example in an anonymous |
28fa5430 AC |
7404 | -- access-to-class-wide type in a formal, or when building the body |
7405 | -- for a subprogram renaming after the subprogram has been frozen. | |
0310af44 | 7406 | -- In these cases both entities designate the same type. In addition, |
28fa5430 AC |
7407 | -- if one of them is an actual in an instance, it may be a subtype of |
7408 | -- the non-limited view of the other. | |
7409 | ||
0310af44 AC |
7410 | if From_Limited_With (Typ_1) |
7411 | and then From_Limited_With (Typ_2) | |
7412 | and then Available_View (Typ_1) = Available_View (Typ_2) | |
28fa5430 | 7413 | then |
aa720a54 AC |
7414 | return True; |
7415 | ||
0310af44 AC |
7416 | elsif From_Limited_With (Typ_1) then |
7417 | return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1)); | |
3e24afaa | 7418 | |
0310af44 AC |
7419 | elsif From_Limited_With (Typ_2) then |
7420 | return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2)); | |
41251c60 | 7421 | |
996ae0b0 RK |
7422 | else |
7423 | return False; | |
7424 | end if; | |
0a36105d | 7425 | end Matches_Limited_With_View; |
996ae0b0 | 7426 | |
0310af44 AC |
7427 | -- Local variables |
7428 | ||
7429 | Are_Anonymous_Access_To_Subprogram_Types : Boolean := False; | |
7430 | ||
7431 | Type_1 : Entity_Id := T1; | |
7432 | Type_2 : Entity_Id := T2; | |
7433 | ||
ec4867fa | 7434 | -- Start of processing for Conforming_Types |
758c442c | 7435 | |
996ae0b0 | 7436 | begin |
8fde064e AC |
7437 | -- The context is an instance association for a formal access-to- |
7438 | -- subprogram type; the formal parameter types require mapping because | |
7439 | -- they may denote other formal parameters of the generic unit. | |
996ae0b0 RK |
7440 | |
7441 | if Get_Inst then | |
7442 | Type_1 := Get_Instance_Of (T1); | |
7443 | Type_2 := Get_Instance_Of (T2); | |
7444 | end if; | |
7445 | ||
0a36105d JM |
7446 | -- If one of the types is a view of the other introduced by a limited |
7447 | -- with clause, treat these as conforming for all purposes. | |
996ae0b0 | 7448 | |
0a36105d JM |
7449 | if Matches_Limited_With_View (T1, T2) then |
7450 | return True; | |
7451 | ||
7452 | elsif Base_Types_Match (Type_1, Type_2) then | |
7738270b AC |
7453 | if Ctype <= Mode_Conformant then |
7454 | return True; | |
7455 | ||
7456 | else | |
7457 | return | |
7458 | Subtypes_Statically_Match (Type_1, Type_2) | |
7459 | and then Dimensions_Match (Type_1, Type_2); | |
7460 | end if; | |
996ae0b0 RK |
7461 | |
7462 | elsif Is_Incomplete_Or_Private_Type (Type_1) | |
7463 | and then Present (Full_View (Type_1)) | |
7464 | and then Base_Types_Match (Full_View (Type_1), Type_2) | |
7465 | then | |
7738270b AC |
7466 | return |
7467 | Ctype <= Mode_Conformant | |
7468 | or else Subtypes_Statically_Match (Full_View (Type_1), Type_2); | |
996ae0b0 RK |
7469 | |
7470 | elsif Ekind (Type_2) = E_Incomplete_Type | |
7471 | and then Present (Full_View (Type_2)) | |
7472 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
7473 | then | |
7738270b AC |
7474 | return |
7475 | Ctype <= Mode_Conformant | |
7476 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
fbf5a39b AC |
7477 | |
7478 | elsif Is_Private_Type (Type_2) | |
7479 | and then In_Instance | |
7480 | and then Present (Full_View (Type_2)) | |
7481 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
7482 | then | |
7738270b AC |
7483 | return |
7484 | Ctype <= Mode_Conformant | |
7485 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
167b47d9 | 7486 | |
7727a9c1 AC |
7487 | -- Another confusion between views in a nested instance with an |
7488 | -- actual private type whose full view is not in scope. | |
7489 | ||
7490 | elsif Ekind (Type_2) = E_Private_Subtype | |
7491 | and then In_Instance | |
7492 | and then Etype (Type_2) = Type_1 | |
7493 | then | |
7494 | return True; | |
7495 | ||
088c7e1b | 7496 | -- In Ada 2012, incomplete types (including limited views) can appear |
167b47d9 AC |
7497 | -- as actuals in instantiations. |
7498 | ||
7499 | elsif Is_Incomplete_Type (Type_1) | |
7500 | and then Is_Incomplete_Type (Type_2) | |
7501 | and then (Used_As_Generic_Actual (Type_1) | |
7502 | or else Used_As_Generic_Actual (Type_2)) | |
7503 | then | |
7504 | return True; | |
996ae0b0 RK |
7505 | end if; |
7506 | ||
0a36105d | 7507 | -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be |
466c2127 AC |
7508 | -- treated recursively because they carry a signature. As far as |
7509 | -- conformance is concerned, convention plays no role, and either | |
7510 | -- or both could be access to protected subprograms. | |
af4b9434 AC |
7511 | |
7512 | Are_Anonymous_Access_To_Subprogram_Types := | |
466c2127 AC |
7513 | Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type, |
7514 | E_Anonymous_Access_Protected_Subprogram_Type) | |
f937473f | 7515 | and then |
466c2127 AC |
7516 | Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type, |
7517 | E_Anonymous_Access_Protected_Subprogram_Type); | |
af4b9434 | 7518 | |
996ae0b0 | 7519 | -- Test anonymous access type case. For this case, static subtype |
5d37ba92 ES |
7520 | -- matching is required for mode conformance (RM 6.3.1(15)). We check |
7521 | -- the base types because we may have built internal subtype entities | |
7522 | -- to handle null-excluding types (see Process_Formals). | |
996ae0b0 | 7523 | |
5d37ba92 ES |
7524 | if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type |
7525 | and then | |
7526 | Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type) | |
8fde064e AC |
7527 | |
7528 | -- Ada 2005 (AI-254) | |
7529 | ||
7530 | or else Are_Anonymous_Access_To_Subprogram_Types | |
996ae0b0 RK |
7531 | then |
7532 | declare | |
7533 | Desig_1 : Entity_Id; | |
7534 | Desig_2 : Entity_Id; | |
7535 | ||
7536 | begin | |
885c4871 | 7537 | -- In Ada 2005, access constant indicators must match for |
5d37ba92 | 7538 | -- subtype conformance. |
9dcb52e1 | 7539 | |
0791fbe9 | 7540 | if Ada_Version >= Ada_2005 |
5d37ba92 ES |
7541 | and then Ctype >= Subtype_Conformant |
7542 | and then | |
7543 | Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2) | |
7544 | then | |
7545 | return False; | |
996ae0b0 RK |
7546 | end if; |
7547 | ||
5d37ba92 | 7548 | Desig_1 := Find_Designated_Type (Type_1); |
5d37ba92 | 7549 | Desig_2 := Find_Designated_Type (Type_2); |
996ae0b0 | 7550 | |
5d37ba92 | 7551 | -- If the context is an instance association for a formal |
82c80734 RD |
7552 | -- access-to-subprogram type; formal access parameter designated |
7553 | -- types require mapping because they may denote other formal | |
7554 | -- parameters of the generic unit. | |
996ae0b0 RK |
7555 | |
7556 | if Get_Inst then | |
7557 | Desig_1 := Get_Instance_Of (Desig_1); | |
7558 | Desig_2 := Get_Instance_Of (Desig_2); | |
7559 | end if; | |
7560 | ||
82c80734 RD |
7561 | -- It is possible for a Class_Wide_Type to be introduced for an |
7562 | -- incomplete type, in which case there is a separate class_ wide | |
7563 | -- type for the full view. The types conform if their Etypes | |
7564 | -- conform, i.e. one may be the full view of the other. This can | |
7565 | -- only happen in the context of an access parameter, other uses | |
7566 | -- of an incomplete Class_Wide_Type are illegal. | |
996ae0b0 | 7567 | |
fbf5a39b | 7568 | if Is_Class_Wide_Type (Desig_1) |
4adf3c50 AC |
7569 | and then |
7570 | Is_Class_Wide_Type (Desig_2) | |
996ae0b0 RK |
7571 | then |
7572 | return | |
fbf5a39b AC |
7573 | Conforming_Types |
7574 | (Etype (Base_Type (Desig_1)), | |
7575 | Etype (Base_Type (Desig_2)), Ctype); | |
af4b9434 AC |
7576 | |
7577 | elsif Are_Anonymous_Access_To_Subprogram_Types then | |
0791fbe9 | 7578 | if Ada_Version < Ada_2005 then |
7738270b AC |
7579 | return |
7580 | Ctype = Type_Conformant | |
7581 | or else Subtypes_Statically_Match (Desig_1, Desig_2); | |
af4b9434 | 7582 | |
758c442c GD |
7583 | -- We must check the conformance of the signatures themselves |
7584 | ||
7585 | else | |
7586 | declare | |
7587 | Conformant : Boolean; | |
7588 | begin | |
7589 | Check_Conformance | |
7590 | (Desig_1, Desig_2, Ctype, False, Conformant); | |
7591 | return Conformant; | |
7592 | end; | |
7593 | end if; | |
7594 | ||
167b47d9 AC |
7595 | -- A limited view of an actual matches the corresponding |
7596 | -- incomplete formal. | |
7597 | ||
7598 | elsif Ekind (Desig_2) = E_Incomplete_Subtype | |
7599 | and then From_Limited_With (Desig_2) | |
7600 | and then Used_As_Generic_Actual (Etype (Desig_2)) | |
7601 | then | |
7602 | return True; | |
7603 | ||
996ae0b0 RK |
7604 | else |
7605 | return Base_Type (Desig_1) = Base_Type (Desig_2) | |
7606 | and then (Ctype = Type_Conformant | |
8fde064e AC |
7607 | or else |
7608 | Subtypes_Statically_Match (Desig_1, Desig_2)); | |
996ae0b0 RK |
7609 | end if; |
7610 | end; | |
7611 | ||
7612 | -- Otherwise definitely no match | |
7613 | ||
7614 | else | |
c8ef728f ES |
7615 | if ((Ekind (Type_1) = E_Anonymous_Access_Type |
7616 | and then Is_Access_Type (Type_2)) | |
7617 | or else (Ekind (Type_2) = E_Anonymous_Access_Type | |
8fde064e | 7618 | and then Is_Access_Type (Type_1))) |
c8ef728f ES |
7619 | and then |
7620 | Conforming_Types | |
7621 | (Designated_Type (Type_1), Designated_Type (Type_2), Ctype) | |
7622 | then | |
7623 | May_Hide_Profile := True; | |
7624 | end if; | |
7625 | ||
996ae0b0 RK |
7626 | return False; |
7627 | end if; | |
996ae0b0 RK |
7628 | end Conforming_Types; |
7629 | ||
7630 | -------------------------- | |
7631 | -- Create_Extra_Formals -- | |
7632 | -------------------------- | |
7633 | ||
7634 | procedure Create_Extra_Formals (E : Entity_Id) is | |
ec4867fa | 7635 | First_Extra : Entity_Id := Empty; |
8eb8461d AC |
7636 | Formal : Entity_Id; |
7637 | Last_Extra : Entity_Id := Empty; | |
996ae0b0 | 7638 | |
ec4867fa ES |
7639 | function Add_Extra_Formal |
7640 | (Assoc_Entity : Entity_Id; | |
7641 | Typ : Entity_Id; | |
7642 | Scope : Entity_Id; | |
7643 | Suffix : String) return Entity_Id; | |
7644 | -- Add an extra formal to the current list of formals and extra formals. | |
7645 | -- The extra formal is added to the end of the list of extra formals, | |
7646 | -- and also returned as the result. These formals are always of mode IN. | |
7647 | -- The new formal has the type Typ, is declared in Scope, and its name | |
7648 | -- is given by a concatenation of the name of Assoc_Entity and Suffix. | |
cd5a9750 AC |
7649 | -- The following suffixes are currently used. They should not be changed |
7650 | -- without coordinating with CodePeer, which makes use of these to | |
7651 | -- provide better messages. | |
7652 | ||
d92eccc3 AC |
7653 | -- O denotes the Constrained bit. |
7654 | -- L denotes the accessibility level. | |
cd5a9750 AC |
7655 | -- BIP_xxx denotes an extra formal for a build-in-place function. See |
7656 | -- the full list in exp_ch6.BIP_Formal_Kind. | |
996ae0b0 | 7657 | |
fbf5a39b AC |
7658 | ---------------------- |
7659 | -- Add_Extra_Formal -- | |
7660 | ---------------------- | |
7661 | ||
ec4867fa ES |
7662 | function Add_Extra_Formal |
7663 | (Assoc_Entity : Entity_Id; | |
7664 | Typ : Entity_Id; | |
7665 | Scope : Entity_Id; | |
7666 | Suffix : String) return Entity_Id | |
7667 | is | |
996ae0b0 | 7668 | EF : constant Entity_Id := |
ec4867fa ES |
7669 | Make_Defining_Identifier (Sloc (Assoc_Entity), |
7670 | Chars => New_External_Name (Chars (Assoc_Entity), | |
f937473f | 7671 | Suffix => Suffix)); |
996ae0b0 RK |
7672 | |
7673 | begin | |
82c80734 RD |
7674 | -- A little optimization. Never generate an extra formal for the |
7675 | -- _init operand of an initialization procedure, since it could | |
7676 | -- never be used. | |
996ae0b0 RK |
7677 | |
7678 | if Chars (Formal) = Name_uInit then | |
7679 | return Empty; | |
7680 | end if; | |
7681 | ||
7682 | Set_Ekind (EF, E_In_Parameter); | |
7683 | Set_Actual_Subtype (EF, Typ); | |
7684 | Set_Etype (EF, Typ); | |
ec4867fa | 7685 | Set_Scope (EF, Scope); |
996ae0b0 RK |
7686 | Set_Mechanism (EF, Default_Mechanism); |
7687 | Set_Formal_Validity (EF); | |
7688 | ||
ec4867fa ES |
7689 | if No (First_Extra) then |
7690 | First_Extra := EF; | |
7691 | Set_Extra_Formals (Scope, First_Extra); | |
7692 | end if; | |
7693 | ||
7694 | if Present (Last_Extra) then | |
7695 | Set_Extra_Formal (Last_Extra, EF); | |
7696 | end if; | |
7697 | ||
996ae0b0 | 7698 | Last_Extra := EF; |
ec4867fa | 7699 | |
996ae0b0 RK |
7700 | return EF; |
7701 | end Add_Extra_Formal; | |
7702 | ||
8eb8461d AC |
7703 | -- Local variables |
7704 | ||
7705 | Formal_Type : Entity_Id; | |
7706 | P_Formal : Entity_Id := Empty; | |
7707 | ||
996ae0b0 RK |
7708 | -- Start of processing for Create_Extra_Formals |
7709 | ||
7710 | begin | |
8fde064e AC |
7711 | -- We never generate extra formals if expansion is not active because we |
7712 | -- don't need them unless we are generating code. | |
f937473f RD |
7713 | |
7714 | if not Expander_Active then | |
7715 | return; | |
7716 | end if; | |
7717 | ||
e2441021 AC |
7718 | -- No need to generate extra formals in interface thunks whose target |
7719 | -- primitive has no extra formals. | |
7720 | ||
7721 | if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then | |
7722 | return; | |
7723 | end if; | |
7724 | ||
82c80734 | 7725 | -- If this is a derived subprogram then the subtypes of the parent |
16b05213 | 7726 | -- subprogram's formal parameters will be used to determine the need |
82c80734 | 7727 | -- for extra formals. |
996ae0b0 RK |
7728 | |
7729 | if Is_Overloadable (E) and then Present (Alias (E)) then | |
7730 | P_Formal := First_Formal (Alias (E)); | |
7731 | end if; | |
7732 | ||
996ae0b0 RK |
7733 | Formal := First_Formal (E); |
7734 | while Present (Formal) loop | |
7735 | Last_Extra := Formal; | |
7736 | Next_Formal (Formal); | |
7737 | end loop; | |
7738 | ||
03a72cd3 | 7739 | -- If Extra_Formals were already created, don't do it again. This |
82c80734 RD |
7740 | -- situation may arise for subprogram types created as part of |
7741 | -- dispatching calls (see Expand_Dispatching_Call) | |
996ae0b0 | 7742 | |
8fde064e | 7743 | if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then |
996ae0b0 RK |
7744 | return; |
7745 | end if; | |
7746 | ||
19590d70 GD |
7747 | -- If the subprogram is a predefined dispatching subprogram then don't |
7748 | -- generate any extra constrained or accessibility level formals. In | |
7749 | -- general we suppress these for internal subprograms (by not calling | |
7750 | -- Freeze_Subprogram and Create_Extra_Formals at all), but internally | |
7751 | -- generated stream attributes do get passed through because extra | |
7752 | -- build-in-place formals are needed in some cases (limited 'Input). | |
7753 | ||
bac7206d | 7754 | if Is_Predefined_Internal_Operation (E) then |
63585f75 | 7755 | goto Test_For_Func_Result_Extras; |
19590d70 GD |
7756 | end if; |
7757 | ||
996ae0b0 | 7758 | Formal := First_Formal (E); |
996ae0b0 RK |
7759 | while Present (Formal) loop |
7760 | ||
7761 | -- Create extra formal for supporting the attribute 'Constrained. | |
7762 | -- The case of a private type view without discriminants also | |
7763 | -- requires the extra formal if the underlying type has defaulted | |
7764 | -- discriminants. | |
7765 | ||
7766 | if Ekind (Formal) /= E_In_Parameter then | |
7767 | if Present (P_Formal) then | |
7768 | Formal_Type := Etype (P_Formal); | |
7769 | else | |
7770 | Formal_Type := Etype (Formal); | |
7771 | end if; | |
7772 | ||
5d09245e AC |
7773 | -- Do not produce extra formals for Unchecked_Union parameters. |
7774 | -- Jump directly to the end of the loop. | |
7775 | ||
7776 | if Is_Unchecked_Union (Base_Type (Formal_Type)) then | |
7777 | goto Skip_Extra_Formal_Generation; | |
7778 | end if; | |
7779 | ||
996ae0b0 RK |
7780 | if not Has_Discriminants (Formal_Type) |
7781 | and then Ekind (Formal_Type) in Private_Kind | |
7782 | and then Present (Underlying_Type (Formal_Type)) | |
7783 | then | |
7784 | Formal_Type := Underlying_Type (Formal_Type); | |
7785 | end if; | |
7786 | ||
5e5db3b4 GD |
7787 | -- Suppress the extra formal if formal's subtype is constrained or |
7788 | -- indefinite, or we're compiling for Ada 2012 and the underlying | |
7789 | -- type is tagged and limited. In Ada 2012, a limited tagged type | |
7790 | -- can have defaulted discriminants, but 'Constrained is required | |
7791 | -- to return True, so the formal is never needed (see AI05-0214). | |
7792 | -- Note that this ensures consistency of calling sequences for | |
7793 | -- dispatching operations when some types in a class have defaults | |
7794 | -- on discriminants and others do not (and requiring the extra | |
7795 | -- formal would introduce distributed overhead). | |
7796 | ||
b5bdffcc AC |
7797 | -- If the type does not have a completion yet, treat as prior to |
7798 | -- Ada 2012 for consistency. | |
7799 | ||
996ae0b0 | 7800 | if Has_Discriminants (Formal_Type) |
f937473f | 7801 | and then not Is_Constrained (Formal_Type) |
83496138 | 7802 | and then Is_Definite_Subtype (Formal_Type) |
5e5db3b4 | 7803 | and then (Ada_Version < Ada_2012 |
b5bdffcc AC |
7804 | or else No (Underlying_Type (Formal_Type)) |
7805 | or else not | |
7806 | (Is_Limited_Type (Formal_Type) | |
7807 | and then | |
7808 | (Is_Tagged_Type | |
7809 | (Underlying_Type (Formal_Type))))) | |
996ae0b0 RK |
7810 | then |
7811 | Set_Extra_Constrained | |
d92eccc3 | 7812 | (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O")); |
996ae0b0 RK |
7813 | end if; |
7814 | end if; | |
7815 | ||
0a36105d JM |
7816 | -- Create extra formal for supporting accessibility checking. This |
7817 | -- is done for both anonymous access formals and formals of named | |
7818 | -- access types that are marked as controlling formals. The latter | |
7819 | -- case can occur when Expand_Dispatching_Call creates a subprogram | |
7820 | -- type and substitutes the types of access-to-class-wide actuals | |
7821 | -- for the anonymous access-to-specific-type of controlling formals. | |
5d37ba92 ES |
7822 | -- Base_Type is applied because in cases where there is a null |
7823 | -- exclusion the formal may have an access subtype. | |
996ae0b0 RK |
7824 | |
7825 | -- This is suppressed if we specifically suppress accessibility | |
f937473f | 7826 | -- checks at the package level for either the subprogram, or the |
fbf5a39b AC |
7827 | -- package in which it resides. However, we do not suppress it |
7828 | -- simply if the scope has accessibility checks suppressed, since | |
7829 | -- this could cause trouble when clients are compiled with a | |
7830 | -- different suppression setting. The explicit checks at the | |
7831 | -- package level are safe from this point of view. | |
996ae0b0 | 7832 | |
5d37ba92 | 7833 | if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type |
0a36105d | 7834 | or else (Is_Controlling_Formal (Formal) |
5d37ba92 | 7835 | and then Is_Access_Type (Base_Type (Etype (Formal))))) |
996ae0b0 | 7836 | and then not |
fbf5a39b | 7837 | (Explicit_Suppress (E, Accessibility_Check) |
996ae0b0 | 7838 | or else |
fbf5a39b | 7839 | Explicit_Suppress (Scope (E), Accessibility_Check)) |
996ae0b0 | 7840 | and then |
c8ef728f | 7841 | (No (P_Formal) |
996ae0b0 RK |
7842 | or else Present (Extra_Accessibility (P_Formal))) |
7843 | then | |
811c6a85 | 7844 | Set_Extra_Accessibility |
d92eccc3 | 7845 | (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L")); |
996ae0b0 RK |
7846 | end if; |
7847 | ||
5d09245e AC |
7848 | -- This label is required when skipping extra formal generation for |
7849 | -- Unchecked_Union parameters. | |
7850 | ||
7851 | <<Skip_Extra_Formal_Generation>> | |
7852 | ||
f937473f RD |
7853 | if Present (P_Formal) then |
7854 | Next_Formal (P_Formal); | |
7855 | end if; | |
7856 | ||
996ae0b0 RK |
7857 | Next_Formal (Formal); |
7858 | end loop; | |
ec4867fa | 7859 | |
63585f75 SB |
7860 | <<Test_For_Func_Result_Extras>> |
7861 | ||
7862 | -- Ada 2012 (AI05-234): "the accessibility level of the result of a | |
7863 | -- function call is ... determined by the point of call ...". | |
7864 | ||
7865 | if Needs_Result_Accessibility_Level (E) then | |
7866 | Set_Extra_Accessibility_Of_Result | |
7867 | (E, Add_Extra_Formal (E, Standard_Natural, E, "L")); | |
7868 | end if; | |
19590d70 | 7869 | |
ec4867fa | 7870 | -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add |
f937473f RD |
7871 | -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind. |
7872 | ||
0791fbe9 | 7873 | if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then |
ec4867fa | 7874 | declare |
f937473f | 7875 | Result_Subt : constant Entity_Id := Etype (E); |
1a36a0cd | 7876 | Full_Subt : constant Entity_Id := Available_View (Result_Subt); |
2fcc44fa | 7877 | Formal_Typ : Entity_Id; |
8eb8461d | 7878 | Subp_Decl : Node_Id; |
f937473f | 7879 | |
2fcc44fa | 7880 | Discard : Entity_Id; |
f937473f | 7881 | pragma Warnings (Off, Discard); |
ec4867fa ES |
7882 | |
7883 | begin | |
f937473f | 7884 | -- In the case of functions with unconstrained result subtypes, |
9a1bc6d5 AC |
7885 | -- add a 4-state formal indicating whether the return object is |
7886 | -- allocated by the caller (1), or should be allocated by the | |
7887 | -- callee on the secondary stack (2), in the global heap (3), or | |
7888 | -- in a user-defined storage pool (4). For the moment we just use | |
7889 | -- Natural for the type of this formal. Note that this formal | |
7890 | -- isn't usually needed in the case where the result subtype is | |
7891 | -- constrained, but it is needed when the function has a tagged | |
7892 | -- result, because generally such functions can be called in a | |
7893 | -- dispatching context and such calls must be handled like calls | |
7894 | -- to a class-wide function. | |
0a36105d | 7895 | |
1bb6e262 | 7896 | if Needs_BIP_Alloc_Form (E) then |
f937473f RD |
7897 | Discard := |
7898 | Add_Extra_Formal | |
7899 | (E, Standard_Natural, | |
7900 | E, BIP_Formal_Suffix (BIP_Alloc_Form)); | |
200b7162 | 7901 | |
8417f4b2 | 7902 | -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to |
3e452820 | 7903 | -- use a user-defined pool. This formal is not added on |
535a8637 | 7904 | -- ZFP as those targets do not support pools. |
200b7162 | 7905 | |
535a8637 | 7906 | if RTE_Available (RE_Root_Storage_Pool_Ptr) then |
8417f4b2 AC |
7907 | Discard := |
7908 | Add_Extra_Formal | |
7909 | (E, RTE (RE_Root_Storage_Pool_Ptr), | |
7910 | E, BIP_Formal_Suffix (BIP_Storage_Pool)); | |
7911 | end if; | |
f937473f | 7912 | end if; |
ec4867fa | 7913 | |
df3e68b1 | 7914 | -- In the case of functions whose result type needs finalization, |
ca5af305 | 7915 | -- add an extra formal which represents the finalization master. |
df3e68b1 | 7916 | |
ca5af305 | 7917 | if Needs_BIP_Finalization_Master (E) then |
f937473f RD |
7918 | Discard := |
7919 | Add_Extra_Formal | |
ca5af305 AC |
7920 | (E, RTE (RE_Finalization_Master_Ptr), |
7921 | E, BIP_Formal_Suffix (BIP_Finalization_Master)); | |
f937473f RD |
7922 | end if; |
7923 | ||
94bbf008 AC |
7924 | -- When the result type contains tasks, add two extra formals: the |
7925 | -- master of the tasks to be created, and the caller's activation | |
7926 | -- chain. | |
f937473f | 7927 | |
1a36a0cd | 7928 | if Has_Task (Full_Subt) then |
f937473f RD |
7929 | Discard := |
7930 | Add_Extra_Formal | |
7931 | (E, RTE (RE_Master_Id), | |
af89615f | 7932 | E, BIP_Formal_Suffix (BIP_Task_Master)); |
f937473f RD |
7933 | Discard := |
7934 | Add_Extra_Formal | |
7935 | (E, RTE (RE_Activation_Chain_Access), | |
7936 | E, BIP_Formal_Suffix (BIP_Activation_Chain)); | |
7937 | end if; | |
ec4867fa | 7938 | |
f937473f RD |
7939 | -- All build-in-place functions get an extra formal that will be |
7940 | -- passed the address of the return object within the caller. | |
ec4867fa | 7941 | |
1a36a0cd AC |
7942 | Formal_Typ := |
7943 | Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E)); | |
ec4867fa | 7944 | |
1a36a0cd AC |
7945 | Set_Directly_Designated_Type (Formal_Typ, Result_Subt); |
7946 | Set_Etype (Formal_Typ, Formal_Typ); | |
7947 | Set_Depends_On_Private | |
7948 | (Formal_Typ, Has_Private_Component (Formal_Typ)); | |
7949 | Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ))); | |
7950 | Set_Is_Access_Constant (Formal_Typ, False); | |
ec4867fa | 7951 | |
1a36a0cd AC |
7952 | -- Ada 2005 (AI-50217): Propagate the attribute that indicates |
7953 | -- the designated type comes from the limited view (for back-end | |
7954 | -- purposes). | |
ec4867fa | 7955 | |
7b56a91b AC |
7956 | Set_From_Limited_With |
7957 | (Formal_Typ, From_Limited_With (Result_Subt)); | |
f937473f | 7958 | |
1a36a0cd AC |
7959 | Layout_Type (Formal_Typ); |
7960 | ||
8eb8461d AC |
7961 | -- Force the definition of the Itype in case of internal function |
7962 | -- calls within the same or nested scope. | |
7963 | ||
7964 | if Is_Subprogram_Or_Generic_Subprogram (E) then | |
7965 | Subp_Decl := Parent (E); | |
7966 | ||
7967 | -- The insertion point for an Itype reference should be after | |
7968 | -- the unit declaration node of the subprogram. An exception | |
7969 | -- to this are inherited operations from a parent type in which | |
7970 | -- case the derived type acts as their parent. | |
7971 | ||
7972 | if Nkind_In (Subp_Decl, N_Function_Specification, | |
7973 | N_Procedure_Specification) | |
7974 | then | |
7975 | Subp_Decl := Parent (Subp_Decl); | |
7976 | end if; | |
7977 | ||
7978 | Build_Itype_Reference (Formal_Typ, Subp_Decl); | |
7979 | end if; | |
7980 | ||
1a36a0cd AC |
7981 | Discard := |
7982 | Add_Extra_Formal | |
7983 | (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access)); | |
ec4867fa ES |
7984 | end; |
7985 | end if; | |
996ae0b0 RK |
7986 | end Create_Extra_Formals; |
7987 | ||
7988 | ----------------------------- | |
7989 | -- Enter_Overloaded_Entity -- | |
7990 | ----------------------------- | |
7991 | ||
7992 | procedure Enter_Overloaded_Entity (S : Entity_Id) is | |
51b42ffa AC |
7993 | function Matches_Predefined_Op return Boolean; |
7994 | -- This returns an approximation of whether S matches a predefined | |
7995 | -- operator, based on the operator symbol, and the parameter and result | |
7996 | -- types. The rules are scattered throughout chapter 4 of the Ada RM. | |
7997 | ||
7998 | --------------------------- | |
7999 | -- Matches_Predefined_Op -- | |
8000 | --------------------------- | |
8001 | ||
8002 | function Matches_Predefined_Op return Boolean is | |
8003 | Formal_1 : constant Entity_Id := First_Formal (S); | |
8004 | Formal_2 : constant Entity_Id := Next_Formal (Formal_1); | |
8005 | Op : constant Name_Id := Chars (S); | |
8006 | Result_Type : constant Entity_Id := Base_Type (Etype (S)); | |
8007 | Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1)); | |
8008 | ||
8009 | begin | |
8010 | -- Binary operator | |
8011 | ||
8012 | if Present (Formal_2) then | |
8013 | declare | |
8014 | Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2)); | |
8015 | ||
8016 | begin | |
8017 | -- All but "&" and "**" have same-types parameters | |
8018 | ||
8019 | case Op is | |
d8f43ee6 HK |
8020 | when Name_Op_Concat |
8021 | | Name_Op_Expon | |
8022 | => | |
51b42ffa AC |
8023 | null; |
8024 | ||
8025 | when others => | |
8026 | if Type_1 /= Type_2 then | |
8027 | return False; | |
8028 | end if; | |
8029 | end case; | |
8030 | ||
8031 | -- Check parameter and result types | |
8032 | ||
8033 | case Op is | |
d8f43ee6 HK |
8034 | when Name_Op_And |
8035 | | Name_Op_Or | |
8036 | | Name_Op_Xor | |
8037 | => | |
51b42ffa AC |
8038 | return |
8039 | Is_Boolean_Type (Result_Type) | |
8040 | and then Result_Type = Type_1; | |
8041 | ||
d8f43ee6 HK |
8042 | when Name_Op_Mod |
8043 | | Name_Op_Rem | |
8044 | => | |
51b42ffa AC |
8045 | return |
8046 | Is_Integer_Type (Result_Type) | |
8047 | and then Result_Type = Type_1; | |
8048 | ||
d8f43ee6 HK |
8049 | when Name_Op_Add |
8050 | | Name_Op_Divide | |
8051 | | Name_Op_Multiply | |
8052 | | Name_Op_Subtract | |
8053 | => | |
51b42ffa AC |
8054 | return |
8055 | Is_Numeric_Type (Result_Type) | |
8056 | and then Result_Type = Type_1; | |
8057 | ||
d8f43ee6 HK |
8058 | when Name_Op_Eq |
8059 | | Name_Op_Ne | |
8060 | => | |
51b42ffa AC |
8061 | return |
8062 | Is_Boolean_Type (Result_Type) | |
8063 | and then not Is_Limited_Type (Type_1); | |
8064 | ||
d8f43ee6 HK |
8065 | when Name_Op_Ge |
8066 | | Name_Op_Gt | |
8067 | | Name_Op_Le | |
8068 | | Name_Op_Lt | |
8069 | => | |
51b42ffa AC |
8070 | return |
8071 | Is_Boolean_Type (Result_Type) | |
8072 | and then (Is_Array_Type (Type_1) | |
8073 | or else Is_Scalar_Type (Type_1)); | |
8074 | ||
8075 | when Name_Op_Concat => | |
8076 | return Is_Array_Type (Result_Type); | |
8077 | ||
8078 | when Name_Op_Expon => | |
8079 | return | |
8080 | (Is_Integer_Type (Result_Type) | |
8081 | or else Is_Floating_Point_Type (Result_Type)) | |
8082 | and then Result_Type = Type_1 | |
8083 | and then Type_2 = Standard_Integer; | |
8084 | ||
8085 | when others => | |
8086 | raise Program_Error; | |
8087 | end case; | |
8088 | end; | |
8089 | ||
8090 | -- Unary operator | |
8091 | ||
8092 | else | |
8093 | case Op is | |
d8f43ee6 HK |
8094 | when Name_Op_Abs |
8095 | | Name_Op_Add | |
8096 | | Name_Op_Subtract | |
8097 | => | |
51b42ffa AC |
8098 | return |
8099 | Is_Numeric_Type (Result_Type) | |
8100 | and then Result_Type = Type_1; | |
8101 | ||
8102 | when Name_Op_Not => | |
8103 | return | |
8104 | Is_Boolean_Type (Result_Type) | |
8105 | and then Result_Type = Type_1; | |
8106 | ||
8107 | when others => | |
8108 | raise Program_Error; | |
8109 | end case; | |
8110 | end if; | |
8111 | end Matches_Predefined_Op; | |
8112 | ||
8113 | -- Local variables | |
8114 | ||
996ae0b0 RK |
8115 | E : Entity_Id := Current_Entity_In_Scope (S); |
8116 | C_E : Entity_Id := Current_Entity (S); | |
8117 | ||
51b42ffa AC |
8118 | -- Start of processing for Enter_Overloaded_Entity |
8119 | ||
996ae0b0 RK |
8120 | begin |
8121 | if Present (E) then | |
8122 | Set_Has_Homonym (E); | |
8123 | Set_Has_Homonym (S); | |
8124 | end if; | |
8125 | ||
8126 | Set_Is_Immediately_Visible (S); | |
8127 | Set_Scope (S, Current_Scope); | |
8128 | ||
8129 | -- Chain new entity if front of homonym in current scope, so that | |
8130 | -- homonyms are contiguous. | |
8131 | ||
8fde064e | 8132 | if Present (E) and then E /= C_E then |
996ae0b0 RK |
8133 | while Homonym (C_E) /= E loop |
8134 | C_E := Homonym (C_E); | |
8135 | end loop; | |
8136 | ||
8137 | Set_Homonym (C_E, S); | |
8138 | ||
8139 | else | |
8140 | E := C_E; | |
8141 | Set_Current_Entity (S); | |
8142 | end if; | |
8143 | ||
8144 | Set_Homonym (S, E); | |
8145 | ||
2352eadb AC |
8146 | if Is_Inherited_Operation (S) then |
8147 | Append_Inherited_Subprogram (S); | |
8148 | else | |
8149 | Append_Entity (S, Current_Scope); | |
8150 | end if; | |
8151 | ||
996ae0b0 RK |
8152 | Set_Public_Status (S); |
8153 | ||
8154 | if Debug_Flag_E then | |
8155 | Write_Str ("New overloaded entity chain: "); | |
8156 | Write_Name (Chars (S)); | |
996ae0b0 | 8157 | |
82c80734 | 8158 | E := S; |
996ae0b0 RK |
8159 | while Present (E) loop |
8160 | Write_Str (" "); Write_Int (Int (E)); | |
8161 | E := Homonym (E); | |
8162 | end loop; | |
8163 | ||
8164 | Write_Eol; | |
8165 | end if; | |
8166 | ||
8167 | -- Generate warning for hiding | |
8168 | ||
8169 | if Warn_On_Hiding | |
8170 | and then Comes_From_Source (S) | |
8171 | and then In_Extended_Main_Source_Unit (S) | |
8172 | then | |
8173 | E := S; | |
8174 | loop | |
8175 | E := Homonym (E); | |
8176 | exit when No (E); | |
8177 | ||
7fc53871 AC |
8178 | -- Warn unless genuine overloading. Do not emit warning on |
8179 | -- hiding predefined operators in Standard (these are either an | |
8180 | -- (artifact of our implicit declarations, or simple noise) but | |
8181 | -- keep warning on a operator defined on a local subtype, because | |
8182 | -- of the real danger that different operators may be applied in | |
8183 | -- various parts of the program. | |
996ae0b0 | 8184 | |
1f250383 AC |
8185 | -- Note that if E and S have the same scope, there is never any |
8186 | -- hiding. Either the two conflict, and the program is illegal, | |
8187 | -- or S is overriding an implicit inherited subprogram. | |
8188 | ||
8189 | if Scope (E) /= Scope (S) | |
51b42ffa AC |
8190 | and then (not Is_Overloadable (E) |
8191 | or else Subtype_Conformant (E, S)) | |
8192 | and then (Is_Immediately_Visible (E) | |
8193 | or else Is_Potentially_Use_Visible (S)) | |
996ae0b0 | 8194 | then |
51b42ffa AC |
8195 | if Scope (E) = Standard_Standard then |
8196 | if Nkind (S) = N_Defining_Operator_Symbol | |
8197 | and then Scope (Base_Type (Etype (First_Formal (S)))) /= | |
8198 | Scope (S) | |
8199 | and then Matches_Predefined_Op | |
8200 | then | |
8201 | Error_Msg_N | |
8202 | ("declaration of & hides predefined operator?h?", S); | |
8203 | end if; | |
8204 | ||
8205 | -- E not immediately within Standard | |
8206 | ||
8207 | else | |
7fc53871 | 8208 | Error_Msg_Sloc := Sloc (E); |
3ccedacc | 8209 | Error_Msg_N ("declaration of & hides one #?h?", S); |
7fc53871 | 8210 | end if; |
996ae0b0 RK |
8211 | end if; |
8212 | end loop; | |
8213 | end if; | |
8214 | end Enter_Overloaded_Entity; | |
8215 | ||
e5a58fac AC |
8216 | ----------------------------- |
8217 | -- Check_Untagged_Equality -- | |
8218 | ----------------------------- | |
8219 | ||
8220 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is | |
8221 | Typ : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
8222 | Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op); | |
8223 | Obj_Decl : Node_Id; | |
8224 | ||
8225 | begin | |
7c0c194b AC |
8226 | -- This check applies only if we have a subprogram declaration with an |
8227 | -- untagged record type. | |
b2834fbd AC |
8228 | |
8229 | if Nkind (Decl) /= N_Subprogram_Declaration | |
8230 | or else not Is_Record_Type (Typ) | |
8231 | or else Is_Tagged_Type (Typ) | |
e5a58fac | 8232 | then |
b2834fbd AC |
8233 | return; |
8234 | end if; | |
e5a58fac | 8235 | |
b2834fbd AC |
8236 | -- In Ada 2012 case, we will output errors or warnings depending on |
8237 | -- the setting of debug flag -gnatd.E. | |
8238 | ||
8239 | if Ada_Version >= Ada_2012 then | |
8240 | Error_Msg_Warn := Debug_Flag_Dot_EE; | |
8241 | ||
8242 | -- In earlier versions of Ada, nothing to do unless we are warning on | |
8243 | -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set). | |
8244 | ||
8245 | else | |
8246 | if not Warn_On_Ada_2012_Compatibility then | |
8247 | return; | |
8248 | end if; | |
8249 | end if; | |
8250 | ||
8251 | -- Cases where the type has already been frozen | |
e5a58fac | 8252 | |
b2834fbd AC |
8253 | if Is_Frozen (Typ) then |
8254 | ||
8255 | -- If the type is not declared in a package, or if we are in the body | |
8256 | -- of the package or in some other scope, the new operation is not | |
8257 | -- primitive, and therefore legal, though suspicious. Should we | |
8258 | -- generate a warning in this case ??? | |
8259 | ||
8260 | if Ekind (Scope (Typ)) /= E_Package | |
8261 | or else Scope (Typ) /= Current_Scope | |
8262 | then | |
8263 | return; | |
8264 | ||
8265 | -- If the type is a generic actual (sub)type, the operation is not | |
8266 | -- primitive either because the base type is declared elsewhere. | |
8267 | ||
8268 | elsif Is_Generic_Actual_Type (Typ) then | |
8269 | return; | |
8270 | ||
8271 | -- Here we have a definite error of declaration after freezing | |
8272 | ||
8273 | else | |
8274 | if Ada_Version >= Ada_2012 then | |
ae6ede77 | 8275 | Error_Msg_NE |
3ccedacc | 8276 | ("equality operator must be declared before type & is " |
b2834fbd AC |
8277 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ); |
8278 | ||
8279 | -- In Ada 2012 mode with error turned to warning, output one | |
8280 | -- more warning to warn that the equality operation may not | |
8281 | -- compose. This is the consequence of ignoring the error. | |
8282 | ||
8283 | if Error_Msg_Warn then | |
8284 | Error_Msg_N ("\equality operation may not compose??", Eq_Op); | |
8285 | end if; | |
21a5b575 AC |
8286 | |
8287 | else | |
8288 | Error_Msg_NE | |
b2834fbd AC |
8289 | ("equality operator must be declared before type& is " |
8290 | & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ); | |
8291 | end if; | |
8292 | ||
8293 | -- If we are in the package body, we could just move the | |
8294 | -- declaration to the package spec, so add a message saying that. | |
8295 | ||
8296 | if In_Package_Body (Scope (Typ)) then | |
8297 | if Ada_Version >= Ada_2012 then | |
8298 | Error_Msg_N | |
8299 | ("\move declaration to package spec<<", Eq_Op); | |
8300 | else | |
8301 | Error_Msg_N | |
8302 | ("\move declaration to package spec (Ada 2012)?y?", Eq_Op); | |
8303 | end if; | |
21a5b575 | 8304 | |
b2834fbd AC |
8305 | -- Otherwise try to find the freezing point |
8306 | ||
8307 | else | |
21a5b575 | 8308 | Obj_Decl := Next (Parent (Typ)); |
dbfeb4fa | 8309 | while Present (Obj_Decl) and then Obj_Decl /= Decl loop |
21a5b575 AC |
8310 | if Nkind (Obj_Decl) = N_Object_Declaration |
8311 | and then Etype (Defining_Identifier (Obj_Decl)) = Typ | |
8312 | then | |
b2834fbd AC |
8313 | -- Freezing point, output warnings |
8314 | ||
8315 | if Ada_Version >= Ada_2012 then | |
8316 | Error_Msg_NE | |
8317 | ("type& is frozen by declaration??", Obj_Decl, Typ); | |
8318 | Error_Msg_N | |
8319 | ("\an equality operator cannot be declared after " | |
8320 | & "this point??", | |
8321 | Obj_Decl); | |
8322 | else | |
8323 | Error_Msg_NE | |
8324 | ("type& is frozen by declaration (Ada 2012)?y?", | |
8325 | Obj_Decl, Typ); | |
8326 | Error_Msg_N | |
8327 | ("\an equality operator cannot be declared after " | |
8328 | & "this point (Ada 2012)?y?", | |
8329 | Obj_Decl); | |
8330 | end if; | |
8331 | ||
21a5b575 AC |
8332 | exit; |
8333 | end if; | |
8334 | ||
8335 | Next (Obj_Decl); | |
8336 | end loop; | |
8337 | end if; | |
b2834fbd | 8338 | end if; |
e5a58fac | 8339 | |
b2834fbd AC |
8340 | -- Here if type is not frozen yet. It is illegal to have a primitive |
8341 | -- equality declared in the private part if the type is visible. | |
21a5b575 | 8342 | |
b2834fbd AC |
8343 | elsif not In_Same_List (Parent (Typ), Decl) |
8344 | and then not Is_Limited_Type (Typ) | |
8345 | then | |
8346 | -- Shouldn't we give an RM reference here??? | |
21a5b575 | 8347 | |
b2834fbd AC |
8348 | if Ada_Version >= Ada_2012 then |
8349 | Error_Msg_N | |
8350 | ("equality operator appears too late<<", Eq_Op); | |
8351 | else | |
8352 | Error_Msg_N | |
8353 | ("equality operator appears too late (Ada 2012)?y?", Eq_Op); | |
e5a58fac | 8354 | end if; |
b2834fbd AC |
8355 | |
8356 | -- No error detected | |
8357 | ||
8358 | else | |
8359 | return; | |
e5a58fac AC |
8360 | end if; |
8361 | end Check_Untagged_Equality; | |
8362 | ||
996ae0b0 RK |
8363 | ----------------------------- |
8364 | -- Find_Corresponding_Spec -- | |
8365 | ----------------------------- | |
8366 | ||
d44202ba HK |
8367 | function Find_Corresponding_Spec |
8368 | (N : Node_Id; | |
8369 | Post_Error : Boolean := True) return Entity_Id | |
8370 | is | |
996ae0b0 RK |
8371 | Spec : constant Node_Id := Specification (N); |
8372 | Designator : constant Entity_Id := Defining_Entity (Spec); | |
8373 | ||
8374 | E : Entity_Id; | |
8375 | ||
70f4ad20 AC |
8376 | function Different_Generic_Profile (E : Entity_Id) return Boolean; |
8377 | -- Even if fully conformant, a body may depend on a generic actual when | |
8378 | -- the spec does not, or vice versa, in which case they were distinct | |
8379 | -- entities in the generic. | |
8380 | ||
8381 | ------------------------------- | |
8382 | -- Different_Generic_Profile -- | |
8383 | ------------------------------- | |
8384 | ||
8385 | function Different_Generic_Profile (E : Entity_Id) return Boolean is | |
8386 | F1, F2 : Entity_Id; | |
8387 | ||
2995860f AC |
8388 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean; |
8389 | -- Check that the types of corresponding formals have the same | |
8390 | -- generic actual if any. We have to account for subtypes of a | |
8391 | -- generic formal, declared between a spec and a body, which may | |
73999267 AC |
8392 | -- appear distinct in an instance but matched in the generic, and |
8393 | -- the subtype may be used either in the spec or the body of the | |
8394 | -- subprogram being checked. | |
2995860f AC |
8395 | |
8396 | ------------------------- | |
8397 | -- Same_Generic_Actual -- | |
8398 | ------------------------- | |
8399 | ||
8400 | function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is | |
73999267 AC |
8401 | |
8402 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean; | |
8403 | -- Predicate to check whether S1 is a subtype of S2 in the source | |
8404 | -- of the instance. | |
8405 | ||
8406 | ------------------------- | |
8407 | -- Is_Declared_Subtype -- | |
8408 | ------------------------- | |
8409 | ||
8410 | function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is | |
8411 | begin | |
8412 | return Comes_From_Source (Parent (S1)) | |
8413 | and then Nkind (Parent (S1)) = N_Subtype_Declaration | |
8414 | and then Is_Entity_Name (Subtype_Indication (Parent (S1))) | |
8415 | and then Entity (Subtype_Indication (Parent (S1))) = S2; | |
8416 | end Is_Declared_Subtype; | |
8417 | ||
8418 | -- Start of processing for Same_Generic_Actual | |
8419 | ||
2995860f AC |
8420 | begin |
8421 | return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2) | |
73999267 AC |
8422 | or else Is_Declared_Subtype (T1, T2) |
8423 | or else Is_Declared_Subtype (T2, T1); | |
2995860f AC |
8424 | end Same_Generic_Actual; |
8425 | ||
8426 | -- Start of processing for Different_Generic_Profile | |
8427 | ||
70f4ad20 | 8428 | begin |
2995860f AC |
8429 | if not In_Instance then |
8430 | return False; | |
8431 | ||
8432 | elsif Ekind (E) = E_Function | |
8433 | and then not Same_Generic_Actual (Etype (E), Etype (Designator)) | |
70f4ad20 AC |
8434 | then |
8435 | return True; | |
8436 | end if; | |
8437 | ||
8438 | F1 := First_Formal (Designator); | |
8439 | F2 := First_Formal (E); | |
70f4ad20 | 8440 | while Present (F1) loop |
2995860f | 8441 | if not Same_Generic_Actual (Etype (F1), Etype (F2)) then |
70f4ad20 AC |
8442 | return True; |
8443 | end if; | |
8444 | ||
8445 | Next_Formal (F1); | |
8446 | Next_Formal (F2); | |
8447 | end loop; | |
8448 | ||
8449 | return False; | |
8450 | end Different_Generic_Profile; | |
8451 | ||
8452 | -- Start of processing for Find_Corresponding_Spec | |
8453 | ||
996ae0b0 RK |
8454 | begin |
8455 | E := Current_Entity (Designator); | |
996ae0b0 RK |
8456 | while Present (E) loop |
8457 | ||
8458 | -- We are looking for a matching spec. It must have the same scope, | |
8459 | -- and the same name, and either be type conformant, or be the case | |
8460 | -- of a library procedure spec and its body (which belong to one | |
8461 | -- another regardless of whether they are type conformant or not). | |
8462 | ||
8463 | if Scope (E) = Current_Scope then | |
fbf5a39b AC |
8464 | if Current_Scope = Standard_Standard |
8465 | or else (Ekind (E) = Ekind (Designator) | |
586ecbf3 | 8466 | and then Type_Conformant (E, Designator)) |
996ae0b0 RK |
8467 | then |
8468 | -- Within an instantiation, we know that spec and body are | |
70f4ad20 AC |
8469 | -- subtype conformant, because they were subtype conformant in |
8470 | -- the generic. We choose the subtype-conformant entity here as | |
8471 | -- well, to resolve spurious ambiguities in the instance that | |
8472 | -- were not present in the generic (i.e. when two different | |
8473 | -- types are given the same actual). If we are looking for a | |
8474 | -- spec to match a body, full conformance is expected. | |
996ae0b0 RK |
8475 | |
8476 | if In_Instance then | |
c05ba1f1 AC |
8477 | |
8478 | -- Inherit the convention and "ghostness" of the matching | |
8479 | -- spec to ensure proper full and subtype conformance. | |
8480 | ||
996ae0b0 RK |
8481 | Set_Convention (Designator, Convention (E)); |
8482 | ||
0187b60e AC |
8483 | -- Skip past subprogram bodies and subprogram renamings that |
8484 | -- may appear to have a matching spec, but that aren't fully | |
8485 | -- conformant with it. That can occur in cases where an | |
8486 | -- actual type causes unrelated homographs in the instance. | |
8487 | ||
8488 | if Nkind_In (N, N_Subprogram_Body, | |
8489 | N_Subprogram_Renaming_Declaration) | |
996ae0b0 | 8490 | and then Present (Homonym (E)) |
c7b9d548 | 8491 | and then not Fully_Conformant (Designator, E) |
996ae0b0 RK |
8492 | then |
8493 | goto Next_Entity; | |
8494 | ||
c7b9d548 | 8495 | elsif not Subtype_Conformant (Designator, E) then |
996ae0b0 | 8496 | goto Next_Entity; |
70f4ad20 AC |
8497 | |
8498 | elsif Different_Generic_Profile (E) then | |
8499 | goto Next_Entity; | |
996ae0b0 RK |
8500 | end if; |
8501 | end if; | |
8502 | ||
25ebc085 AC |
8503 | -- Ada 2012 (AI05-0165): For internally generated bodies of |
8504 | -- null procedures locate the internally generated spec. We | |
8505 | -- enforce mode conformance since a tagged type may inherit | |
8506 | -- from interfaces several null primitives which differ only | |
8507 | -- in the mode of the formals. | |
8508 | ||
8509 | if not (Comes_From_Source (E)) | |
8510 | and then Is_Null_Procedure (E) | |
8511 | and then not Mode_Conformant (Designator, E) | |
8512 | then | |
8513 | null; | |
8514 | ||
4d8f3296 ES |
8515 | -- For null procedures coming from source that are completions, |
8516 | -- analysis of the generated body will establish the link. | |
8517 | ||
8518 | elsif Comes_From_Source (E) | |
8519 | and then Nkind (Spec) = N_Procedure_Specification | |
8520 | and then Null_Present (Spec) | |
8521 | then | |
8522 | return E; | |
8523 | ||
e4bda610 AC |
8524 | -- Expression functions can be completions, but cannot be |
8525 | -- completed by an explicit body. | |
8526 | ||
8527 | elsif Comes_From_Source (E) | |
8528 | and then Comes_From_Source (N) | |
8529 | and then Nkind (N) = N_Subprogram_Body | |
8530 | and then Nkind (Original_Node (Unit_Declaration_Node (E))) = | |
8531 | N_Expression_Function | |
8532 | then | |
8533 | Error_Msg_Sloc := Sloc (E); | |
8534 | Error_Msg_N ("body conflicts with expression function#", N); | |
8535 | return Empty; | |
8536 | ||
25ebc085 | 8537 | elsif not Has_Completion (E) then |
996ae0b0 RK |
8538 | if Nkind (N) /= N_Subprogram_Body_Stub then |
8539 | Set_Corresponding_Spec (N, E); | |
8540 | end if; | |
8541 | ||
8542 | Set_Has_Completion (E); | |
8543 | return E; | |
8544 | ||
8545 | elsif Nkind (Parent (N)) = N_Subunit then | |
8546 | ||
8547 | -- If this is the proper body of a subunit, the completion | |
8548 | -- flag is set when analyzing the stub. | |
8549 | ||
8550 | return E; | |
8551 | ||
70f4ad20 AC |
8552 | -- If E is an internal function with a controlling result that |
8553 | -- was created for an operation inherited by a null extension, | |
8554 | -- it may be overridden by a body without a previous spec (one | |
2995860f | 8555 | -- more reason why these should be shunned). In that case we |
70f4ad20 AC |
8556 | -- remove the generated body if present, because the current |
8557 | -- one is the explicit overriding. | |
81db9d77 ES |
8558 | |
8559 | elsif Ekind (E) = E_Function | |
0791fbe9 | 8560 | and then Ada_Version >= Ada_2005 |
81db9d77 ES |
8561 | and then not Comes_From_Source (E) |
8562 | and then Has_Controlling_Result (E) | |
8563 | and then Is_Null_Extension (Etype (E)) | |
8564 | and then Comes_From_Source (Spec) | |
8565 | then | |
8566 | Set_Has_Completion (E, False); | |
8567 | ||
1366997b AC |
8568 | if Expander_Active |
8569 | and then Nkind (Parent (E)) = N_Function_Specification | |
8570 | then | |
81db9d77 ES |
8571 | Remove |
8572 | (Unit_Declaration_Node | |
1366997b AC |
8573 | (Corresponding_Body (Unit_Declaration_Node (E)))); |
8574 | ||
81db9d77 ES |
8575 | return E; |
8576 | ||
1366997b AC |
8577 | -- If expansion is disabled, or if the wrapper function has |
8578 | -- not been generated yet, this a late body overriding an | |
8579 | -- inherited operation, or it is an overriding by some other | |
8580 | -- declaration before the controlling result is frozen. In | |
8581 | -- either case this is a declaration of a new entity. | |
81db9d77 ES |
8582 | |
8583 | else | |
8584 | return Empty; | |
8585 | end if; | |
8586 | ||
d44202ba HK |
8587 | -- If the body already exists, then this is an error unless |
8588 | -- the previous declaration is the implicit declaration of a | |
756ef2a0 AC |
8589 | -- derived subprogram. It is also legal for an instance to |
8590 | -- contain type conformant overloadable declarations (but the | |
8591 | -- generic declaration may not), per 8.3(26/2). | |
996ae0b0 RK |
8592 | |
8593 | elsif No (Alias (E)) | |
8594 | and then not Is_Intrinsic_Subprogram (E) | |
8595 | and then not In_Instance | |
d44202ba | 8596 | and then Post_Error |
996ae0b0 RK |
8597 | then |
8598 | Error_Msg_Sloc := Sloc (E); | |
8dbd1460 | 8599 | |
07fc65c4 GB |
8600 | if Is_Imported (E) then |
8601 | Error_Msg_NE | |
8602 | ("body not allowed for imported subprogram & declared#", | |
8603 | N, E); | |
8604 | else | |
8605 | Error_Msg_NE ("duplicate body for & declared#", N, E); | |
8606 | end if; | |
996ae0b0 RK |
8607 | end if; |
8608 | ||
d44202ba HK |
8609 | -- Child units cannot be overloaded, so a conformance mismatch |
8610 | -- between body and a previous spec is an error. | |
8611 | ||
996ae0b0 RK |
8612 | elsif Is_Child_Unit (E) |
8613 | and then | |
8614 | Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body | |
8615 | and then | |
5d37ba92 | 8616 | Nkind (Parent (Unit_Declaration_Node (Designator))) = |
d44202ba HK |
8617 | N_Compilation_Unit |
8618 | and then Post_Error | |
996ae0b0 | 8619 | then |
996ae0b0 RK |
8620 | Error_Msg_N |
8621 | ("body of child unit does not match previous declaration", N); | |
8622 | end if; | |
8623 | end if; | |
8624 | ||
8625 | <<Next_Entity>> | |
8626 | E := Homonym (E); | |
8627 | end loop; | |
8628 | ||
8629 | -- On exit, we know that no previous declaration of subprogram exists | |
8630 | ||
8631 | return Empty; | |
8632 | end Find_Corresponding_Spec; | |
8633 | ||
8634 | ---------------------- | |
8635 | -- Fully_Conformant -- | |
8636 | ---------------------- | |
8637 | ||
8638 | function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
8639 | Result : Boolean; | |
996ae0b0 RK |
8640 | begin |
8641 | Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result); | |
8642 | return Result; | |
8643 | end Fully_Conformant; | |
8644 | ||
8645 | ---------------------------------- | |
8646 | -- Fully_Conformant_Expressions -- | |
8647 | ---------------------------------- | |
8648 | ||
8649 | function Fully_Conformant_Expressions | |
8650 | (Given_E1 : Node_Id; | |
d05ef0ab | 8651 | Given_E2 : Node_Id) return Boolean |
996ae0b0 RK |
8652 | is |
8653 | E1 : constant Node_Id := Original_Node (Given_E1); | |
8654 | E2 : constant Node_Id := Original_Node (Given_E2); | |
8655 | -- We always test conformance on original nodes, since it is possible | |
8656 | -- for analysis and/or expansion to make things look as though they | |
8657 | -- conform when they do not, e.g. by converting 1+2 into 3. | |
8658 | ||
8659 | function FCE (Given_E1, Given_E2 : Node_Id) return Boolean | |
8660 | renames Fully_Conformant_Expressions; | |
8661 | ||
8662 | function FCL (L1, L2 : List_Id) return Boolean; | |
70f4ad20 AC |
8663 | -- Compare elements of two lists for conformance. Elements have to be |
8664 | -- conformant, and actuals inserted as default parameters do not match | |
8665 | -- explicit actuals with the same value. | |
996ae0b0 RK |
8666 | |
8667 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean; | |
e895b435 | 8668 | -- Compare an operator node with a function call |
996ae0b0 RK |
8669 | |
8670 | --------- | |
8671 | -- FCL -- | |
8672 | --------- | |
8673 | ||
8674 | function FCL (L1, L2 : List_Id) return Boolean is | |
8675 | N1, N2 : Node_Id; | |
8676 | ||
8677 | begin | |
8678 | if L1 = No_List then | |
8679 | N1 := Empty; | |
8680 | else | |
8681 | N1 := First (L1); | |
8682 | end if; | |
8683 | ||
8684 | if L2 = No_List then | |
8685 | N2 := Empty; | |
8686 | else | |
8687 | N2 := First (L2); | |
8688 | end if; | |
8689 | ||
70f4ad20 | 8690 | -- Compare two lists, skipping rewrite insertions (we want to compare |
a90bd866 | 8691 | -- the original trees, not the expanded versions). |
996ae0b0 RK |
8692 | |
8693 | loop | |
8694 | if Is_Rewrite_Insertion (N1) then | |
8695 | Next (N1); | |
8696 | elsif Is_Rewrite_Insertion (N2) then | |
8697 | Next (N2); | |
8698 | elsif No (N1) then | |
8699 | return No (N2); | |
8700 | elsif No (N2) then | |
8701 | return False; | |
8702 | elsif not FCE (N1, N2) then | |
8703 | return False; | |
8704 | else | |
8705 | Next (N1); | |
8706 | Next (N2); | |
8707 | end if; | |
8708 | end loop; | |
8709 | end FCL; | |
8710 | ||
8711 | --------- | |
8712 | -- FCO -- | |
8713 | --------- | |
8714 | ||
8715 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean is | |
8716 | Actuals : constant List_Id := Parameter_Associations (Call_Node); | |
8717 | Act : Node_Id; | |
8718 | ||
8719 | begin | |
8720 | if No (Actuals) | |
8721 | or else Entity (Op_Node) /= Entity (Name (Call_Node)) | |
8722 | then | |
8723 | return False; | |
8724 | ||
8725 | else | |
8726 | Act := First (Actuals); | |
8727 | ||
8728 | if Nkind (Op_Node) in N_Binary_Op then | |
996ae0b0 RK |
8729 | if not FCE (Left_Opnd (Op_Node), Act) then |
8730 | return False; | |
8731 | end if; | |
8732 | ||
8733 | Next (Act); | |
8734 | end if; | |
8735 | ||
8736 | return Present (Act) | |
8737 | and then FCE (Right_Opnd (Op_Node), Act) | |
8738 | and then No (Next (Act)); | |
8739 | end if; | |
8740 | end FCO; | |
8741 | ||
8742 | -- Start of processing for Fully_Conformant_Expressions | |
8743 | ||
8744 | begin | |
27bb7941 | 8745 | -- Nonconformant if paren count does not match. Note: if some idiot |
996ae0b0 | 8746 | -- complains that we don't do this right for more than 3 levels of |
a90bd866 | 8747 | -- parentheses, they will be treated with the respect they deserve. |
996ae0b0 RK |
8748 | |
8749 | if Paren_Count (E1) /= Paren_Count (E2) then | |
8750 | return False; | |
8751 | ||
82c80734 RD |
8752 | -- If same entities are referenced, then they are conformant even if |
8753 | -- they have different forms (RM 8.3.1(19-20)). | |
996ae0b0 RK |
8754 | |
8755 | elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then | |
8756 | if Present (Entity (E1)) then | |
8757 | return Entity (E1) = Entity (E2) | |
ca0eb951 AC |
8758 | |
8759 | -- One may be a discriminant that has been replaced by | |
27bb7941 | 8760 | -- the corresponding discriminal. |
ca0eb951 | 8761 | |
996ae0b0 RK |
8762 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) |
8763 | and then Ekind (Entity (E1)) = E_Discriminant | |
ca0eb951 AC |
8764 | and then Ekind (Entity (E2)) = E_In_Parameter) |
8765 | ||
804ec349 | 8766 | -- AI12-050: The loop variables of quantified expressions |
27bb7941 | 8767 | -- match if they have the same identifier, even though they |
ca0eb951 AC |
8768 | -- are different entities. |
8769 | ||
8770 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) | |
8771 | and then Ekind (Entity (E1)) = E_Loop_Parameter | |
8772 | and then Ekind (Entity (E2)) = E_Loop_Parameter); | |
996ae0b0 RK |
8773 | |
8774 | elsif Nkind (E1) = N_Expanded_Name | |
8775 | and then Nkind (E2) = N_Expanded_Name | |
8776 | and then Nkind (Selector_Name (E1)) = N_Character_Literal | |
8777 | and then Nkind (Selector_Name (E2)) = N_Character_Literal | |
8778 | then | |
8779 | return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)); | |
8780 | ||
8781 | else | |
8782 | -- Identifiers in component associations don't always have | |
8783 | -- entities, but their names must conform. | |
8784 | ||
8785 | return Nkind (E1) = N_Identifier | |
8786 | and then Nkind (E2) = N_Identifier | |
8787 | and then Chars (E1) = Chars (E2); | |
8788 | end if; | |
8789 | ||
8790 | elsif Nkind (E1) = N_Character_Literal | |
8791 | and then Nkind (E2) = N_Expanded_Name | |
8792 | then | |
8793 | return Nkind (Selector_Name (E2)) = N_Character_Literal | |
8794 | and then Chars (E1) = Chars (Selector_Name (E2)); | |
8795 | ||
8796 | elsif Nkind (E2) = N_Character_Literal | |
8797 | and then Nkind (E1) = N_Expanded_Name | |
8798 | then | |
8799 | return Nkind (Selector_Name (E1)) = N_Character_Literal | |
8800 | and then Chars (E2) = Chars (Selector_Name (E1)); | |
8801 | ||
8fde064e | 8802 | elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then |
996ae0b0 RK |
8803 | return FCO (E1, E2); |
8804 | ||
8fde064e | 8805 | elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then |
996ae0b0 RK |
8806 | return FCO (E2, E1); |
8807 | ||
8808 | -- Otherwise we must have the same syntactic entity | |
8809 | ||
8810 | elsif Nkind (E1) /= Nkind (E2) then | |
8811 | return False; | |
8812 | ||
8813 | -- At this point, we specialize by node type | |
8814 | ||
8815 | else | |
8816 | case Nkind (E1) is | |
996ae0b0 RK |
8817 | when N_Aggregate => |
8818 | return | |
8819 | FCL (Expressions (E1), Expressions (E2)) | |
19d846a0 RD |
8820 | and then |
8821 | FCL (Component_Associations (E1), | |
8822 | Component_Associations (E2)); | |
996ae0b0 RK |
8823 | |
8824 | when N_Allocator => | |
8825 | if Nkind (Expression (E1)) = N_Qualified_Expression | |
8826 | or else | |
8827 | Nkind (Expression (E2)) = N_Qualified_Expression | |
8828 | then | |
8829 | return FCE (Expression (E1), Expression (E2)); | |
8830 | ||
8831 | -- Check that the subtype marks and any constraints | |
8832 | -- are conformant | |
8833 | ||
8834 | else | |
8835 | declare | |
8836 | Indic1 : constant Node_Id := Expression (E1); | |
8837 | Indic2 : constant Node_Id := Expression (E2); | |
8838 | Elt1 : Node_Id; | |
8839 | Elt2 : Node_Id; | |
8840 | ||
8841 | begin | |
8842 | if Nkind (Indic1) /= N_Subtype_Indication then | |
8843 | return | |
8844 | Nkind (Indic2) /= N_Subtype_Indication | |
8845 | and then Entity (Indic1) = Entity (Indic2); | |
8846 | ||
8847 | elsif Nkind (Indic2) /= N_Subtype_Indication then | |
8848 | return | |
8849 | Nkind (Indic1) /= N_Subtype_Indication | |
8850 | and then Entity (Indic1) = Entity (Indic2); | |
8851 | ||
8852 | else | |
8853 | if Entity (Subtype_Mark (Indic1)) /= | |
8854 | Entity (Subtype_Mark (Indic2)) | |
8855 | then | |
8856 | return False; | |
8857 | end if; | |
8858 | ||
8859 | Elt1 := First (Constraints (Constraint (Indic1))); | |
8860 | Elt2 := First (Constraints (Constraint (Indic2))); | |
996ae0b0 RK |
8861 | while Present (Elt1) and then Present (Elt2) loop |
8862 | if not FCE (Elt1, Elt2) then | |
8863 | return False; | |
8864 | end if; | |
8865 | ||
8866 | Next (Elt1); | |
8867 | Next (Elt2); | |
8868 | end loop; | |
8869 | ||
8870 | return True; | |
8871 | end if; | |
8872 | end; | |
8873 | end if; | |
8874 | ||
8875 | when N_Attribute_Reference => | |
8876 | return | |
8877 | Attribute_Name (E1) = Attribute_Name (E2) | |
8878 | and then FCL (Expressions (E1), Expressions (E2)); | |
8879 | ||
8880 | when N_Binary_Op => | |
8881 | return | |
8882 | Entity (E1) = Entity (E2) | |
8883 | and then FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
8884 | and then FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
8885 | ||
d8f43ee6 HK |
8886 | when N_Membership_Test |
8887 | | N_Short_Circuit | |
8888 | => | |
996ae0b0 RK |
8889 | return |
8890 | FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
8891 | and then | |
8892 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
8893 | ||
19d846a0 RD |
8894 | when N_Case_Expression => |
8895 | declare | |
8896 | Alt1 : Node_Id; | |
8897 | Alt2 : Node_Id; | |
8898 | ||
8899 | begin | |
8900 | if not FCE (Expression (E1), Expression (E2)) then | |
8901 | return False; | |
8902 | ||
8903 | else | |
8904 | Alt1 := First (Alternatives (E1)); | |
8905 | Alt2 := First (Alternatives (E2)); | |
8906 | loop | |
8907 | if Present (Alt1) /= Present (Alt2) then | |
8908 | return False; | |
8909 | elsif No (Alt1) then | |
8910 | return True; | |
8911 | end if; | |
8912 | ||
8913 | if not FCE (Expression (Alt1), Expression (Alt2)) | |
8914 | or else not FCL (Discrete_Choices (Alt1), | |
8915 | Discrete_Choices (Alt2)) | |
8916 | then | |
8917 | return False; | |
8918 | end if; | |
8919 | ||
8920 | Next (Alt1); | |
8921 | Next (Alt2); | |
8922 | end loop; | |
8923 | end if; | |
8924 | end; | |
8925 | ||
996ae0b0 RK |
8926 | when N_Character_Literal => |
8927 | return | |
8928 | Char_Literal_Value (E1) = Char_Literal_Value (E2); | |
8929 | ||
8930 | when N_Component_Association => | |
8931 | return | |
8932 | FCL (Choices (E1), Choices (E2)) | |
19d846a0 RD |
8933 | and then |
8934 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 8935 | |
996ae0b0 RK |
8936 | when N_Explicit_Dereference => |
8937 | return | |
8938 | FCE (Prefix (E1), Prefix (E2)); | |
8939 | ||
8940 | when N_Extension_Aggregate => | |
8941 | return | |
8942 | FCL (Expressions (E1), Expressions (E2)) | |
8943 | and then Null_Record_Present (E1) = | |
8944 | Null_Record_Present (E2) | |
8945 | and then FCL (Component_Associations (E1), | |
8946 | Component_Associations (E2)); | |
8947 | ||
8948 | when N_Function_Call => | |
8949 | return | |
8950 | FCE (Name (E1), Name (E2)) | |
19d846a0 RD |
8951 | and then |
8952 | FCL (Parameter_Associations (E1), | |
8953 | Parameter_Associations (E2)); | |
996ae0b0 | 8954 | |
9b16cb57 RD |
8955 | when N_If_Expression => |
8956 | return | |
8957 | FCL (Expressions (E1), Expressions (E2)); | |
8958 | ||
996ae0b0 RK |
8959 | when N_Indexed_Component => |
8960 | return | |
8961 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
8962 | and then |
8963 | FCL (Expressions (E1), Expressions (E2)); | |
996ae0b0 RK |
8964 | |
8965 | when N_Integer_Literal => | |
8966 | return (Intval (E1) = Intval (E2)); | |
8967 | ||
8968 | when N_Null => | |
8969 | return True; | |
8970 | ||
8971 | when N_Operator_Symbol => | |
8972 | return | |
8973 | Chars (E1) = Chars (E2); | |
8974 | ||
8975 | when N_Others_Choice => | |
8976 | return True; | |
8977 | ||
8978 | when N_Parameter_Association => | |
8979 | return | |
7dae9ca0 | 8980 | Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)) |
996ae0b0 RK |
8981 | and then FCE (Explicit_Actual_Parameter (E1), |
8982 | Explicit_Actual_Parameter (E2)); | |
8983 | ||
8984 | when N_Qualified_Expression => | |
8985 | return | |
8986 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
8987 | and then |
8988 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 8989 | |
2010d078 AC |
8990 | when N_Quantified_Expression => |
8991 | if not FCE (Condition (E1), Condition (E2)) then | |
8992 | return False; | |
8993 | end if; | |
8994 | ||
8995 | if Present (Loop_Parameter_Specification (E1)) | |
8996 | and then Present (Loop_Parameter_Specification (E2)) | |
8997 | then | |
8998 | declare | |
8999 | L1 : constant Node_Id := | |
9000 | Loop_Parameter_Specification (E1); | |
9001 | L2 : constant Node_Id := | |
9002 | Loop_Parameter_Specification (E2); | |
9003 | ||
9004 | begin | |
9005 | return | |
9006 | Reverse_Present (L1) = Reverse_Present (L2) | |
9007 | and then | |
9008 | FCE (Defining_Identifier (L1), | |
9009 | Defining_Identifier (L2)) | |
9010 | and then | |
9011 | FCE (Discrete_Subtype_Definition (L1), | |
9012 | Discrete_Subtype_Definition (L2)); | |
9013 | end; | |
9014 | ||
804670f1 AC |
9015 | elsif Present (Iterator_Specification (E1)) |
9016 | and then Present (Iterator_Specification (E2)) | |
9017 | then | |
2010d078 AC |
9018 | declare |
9019 | I1 : constant Node_Id := Iterator_Specification (E1); | |
9020 | I2 : constant Node_Id := Iterator_Specification (E2); | |
9021 | ||
9022 | begin | |
9023 | return | |
9024 | FCE (Defining_Identifier (I1), | |
9025 | Defining_Identifier (I2)) | |
9026 | and then | |
9027 | Of_Present (I1) = Of_Present (I2) | |
9028 | and then | |
9029 | Reverse_Present (I1) = Reverse_Present (I2) | |
9030 | and then FCE (Name (I1), Name (I2)) | |
9031 | and then FCE (Subtype_Indication (I1), | |
9032 | Subtype_Indication (I2)); | |
9033 | end; | |
804670f1 AC |
9034 | |
9035 | -- The quantified expressions used different specifications to | |
9036 | -- walk their respective ranges. | |
9037 | ||
9038 | else | |
9039 | return False; | |
2010d078 AC |
9040 | end if; |
9041 | ||
996ae0b0 RK |
9042 | when N_Range => |
9043 | return | |
9044 | FCE (Low_Bound (E1), Low_Bound (E2)) | |
19d846a0 RD |
9045 | and then |
9046 | FCE (High_Bound (E1), High_Bound (E2)); | |
996ae0b0 RK |
9047 | |
9048 | when N_Real_Literal => | |
9049 | return (Realval (E1) = Realval (E2)); | |
9050 | ||
9051 | when N_Selected_Component => | |
9052 | return | |
9053 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
9054 | and then |
9055 | FCE (Selector_Name (E1), Selector_Name (E2)); | |
996ae0b0 RK |
9056 | |
9057 | when N_Slice => | |
9058 | return | |
9059 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
9060 | and then |
9061 | FCE (Discrete_Range (E1), Discrete_Range (E2)); | |
996ae0b0 RK |
9062 | |
9063 | when N_String_Literal => | |
9064 | declare | |
9065 | S1 : constant String_Id := Strval (E1); | |
9066 | S2 : constant String_Id := Strval (E2); | |
9067 | L1 : constant Nat := String_Length (S1); | |
9068 | L2 : constant Nat := String_Length (S2); | |
9069 | ||
9070 | begin | |
9071 | if L1 /= L2 then | |
9072 | return False; | |
9073 | ||
9074 | else | |
9075 | for J in 1 .. L1 loop | |
9076 | if Get_String_Char (S1, J) /= | |
9077 | Get_String_Char (S2, J) | |
9078 | then | |
9079 | return False; | |
9080 | end if; | |
9081 | end loop; | |
9082 | ||
9083 | return True; | |
9084 | end if; | |
9085 | end; | |
9086 | ||
9087 | when N_Type_Conversion => | |
9088 | return | |
9089 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
9090 | and then |
9091 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
9092 | |
9093 | when N_Unary_Op => | |
9094 | return | |
9095 | Entity (E1) = Entity (E2) | |
19d846a0 RD |
9096 | and then |
9097 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
996ae0b0 RK |
9098 | |
9099 | when N_Unchecked_Type_Conversion => | |
9100 | return | |
9101 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
9102 | and then |
9103 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
9104 | |
9105 | -- All other node types cannot appear in this context. Strictly | |
9106 | -- we should raise a fatal internal error. Instead we just ignore | |
9107 | -- the nodes. This means that if anyone makes a mistake in the | |
2995860f AC |
9108 | -- expander and mucks an expression tree irretrievably, the result |
9109 | -- will be a failure to detect a (probably very obscure) case | |
9110 | -- of non-conformance, which is better than bombing on some | |
996ae0b0 RK |
9111 | -- case where two expressions do in fact conform. |
9112 | ||
9113 | when others => | |
9114 | return True; | |
996ae0b0 RK |
9115 | end case; |
9116 | end if; | |
9117 | end Fully_Conformant_Expressions; | |
9118 | ||
fbf5a39b AC |
9119 | ---------------------------------------- |
9120 | -- Fully_Conformant_Discrete_Subtypes -- | |
9121 | ---------------------------------------- | |
9122 | ||
9123 | function Fully_Conformant_Discrete_Subtypes | |
9124 | (Given_S1 : Node_Id; | |
d05ef0ab | 9125 | Given_S2 : Node_Id) return Boolean |
fbf5a39b AC |
9126 | is |
9127 | S1 : constant Node_Id := Original_Node (Given_S1); | |
9128 | S2 : constant Node_Id := Original_Node (Given_S2); | |
9129 | ||
9130 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean; | |
82c80734 RD |
9131 | -- Special-case for a bound given by a discriminant, which in the body |
9132 | -- is replaced with the discriminal of the enclosing type. | |
fbf5a39b AC |
9133 | |
9134 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean; | |
e895b435 | 9135 | -- Check both bounds |
fbf5a39b | 9136 | |
5d37ba92 ES |
9137 | ----------------------- |
9138 | -- Conforming_Bounds -- | |
9139 | ----------------------- | |
9140 | ||
fbf5a39b AC |
9141 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is |
9142 | begin | |
9143 | if Is_Entity_Name (B1) | |
9144 | and then Is_Entity_Name (B2) | |
9145 | and then Ekind (Entity (B1)) = E_Discriminant | |
9146 | then | |
9147 | return Chars (B1) = Chars (B2); | |
9148 | ||
9149 | else | |
9150 | return Fully_Conformant_Expressions (B1, B2); | |
9151 | end if; | |
9152 | end Conforming_Bounds; | |
9153 | ||
5d37ba92 ES |
9154 | ----------------------- |
9155 | -- Conforming_Ranges -- | |
9156 | ----------------------- | |
9157 | ||
fbf5a39b AC |
9158 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is |
9159 | begin | |
9160 | return | |
9161 | Conforming_Bounds (Low_Bound (R1), Low_Bound (R2)) | |
9162 | and then | |
9163 | Conforming_Bounds (High_Bound (R1), High_Bound (R2)); | |
9164 | end Conforming_Ranges; | |
9165 | ||
9166 | -- Start of processing for Fully_Conformant_Discrete_Subtypes | |
9167 | ||
9168 | begin | |
9169 | if Nkind (S1) /= Nkind (S2) then | |
9170 | return False; | |
9171 | ||
9172 | elsif Is_Entity_Name (S1) then | |
9173 | return Entity (S1) = Entity (S2); | |
9174 | ||
9175 | elsif Nkind (S1) = N_Range then | |
9176 | return Conforming_Ranges (S1, S2); | |
9177 | ||
9178 | elsif Nkind (S1) = N_Subtype_Indication then | |
9179 | return | |
9180 | Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2)) | |
9181 | and then | |
9182 | Conforming_Ranges | |
9183 | (Range_Expression (Constraint (S1)), | |
9184 | Range_Expression (Constraint (S2))); | |
9185 | else | |
9186 | return True; | |
9187 | end if; | |
9188 | end Fully_Conformant_Discrete_Subtypes; | |
9189 | ||
996ae0b0 RK |
9190 | -------------------- |
9191 | -- Install_Entity -- | |
9192 | -------------------- | |
9193 | ||
9194 | procedure Install_Entity (E : Entity_Id) is | |
9195 | Prev : constant Entity_Id := Current_Entity (E); | |
996ae0b0 RK |
9196 | begin |
9197 | Set_Is_Immediately_Visible (E); | |
9198 | Set_Current_Entity (E); | |
9199 | Set_Homonym (E, Prev); | |
9200 | end Install_Entity; | |
9201 | ||
9202 | --------------------- | |
9203 | -- Install_Formals -- | |
9204 | --------------------- | |
9205 | ||
9206 | procedure Install_Formals (Id : Entity_Id) is | |
9207 | F : Entity_Id; | |
996ae0b0 RK |
9208 | begin |
9209 | F := First_Formal (Id); | |
996ae0b0 RK |
9210 | while Present (F) loop |
9211 | Install_Entity (F); | |
9212 | Next_Formal (F); | |
9213 | end loop; | |
9214 | end Install_Formals; | |
9215 | ||
ce2b6ba5 JM |
9216 | ----------------------------- |
9217 | -- Is_Interface_Conformant -- | |
9218 | ----------------------------- | |
9219 | ||
9220 | function Is_Interface_Conformant | |
9221 | (Tagged_Type : Entity_Id; | |
9222 | Iface_Prim : Entity_Id; | |
9223 | Prim : Entity_Id) return Boolean | |
9224 | is | |
9e92ad49 AC |
9225 | -- The operation may in fact be an inherited (implicit) operation |
9226 | -- rather than the original interface primitive, so retrieve the | |
9227 | -- ultimate ancestor. | |
9228 | ||
9229 | Iface : constant Entity_Id := | |
9230 | Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)); | |
fceeaab6 ES |
9231 | Typ : constant Entity_Id := Find_Dispatching_Type (Prim); |
9232 | ||
25ebc085 AC |
9233 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id; |
9234 | -- Return the controlling formal of Prim | |
9235 | ||
59e6b23c AC |
9236 | ------------------------ |
9237 | -- Controlling_Formal -- | |
9238 | ------------------------ | |
9239 | ||
25ebc085 | 9240 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id is |
15918371 | 9241 | E : Entity_Id; |
59e6b23c | 9242 | |
25ebc085 | 9243 | begin |
15918371 | 9244 | E := First_Entity (Prim); |
25ebc085 AC |
9245 | while Present (E) loop |
9246 | if Is_Formal (E) and then Is_Controlling_Formal (E) then | |
9247 | return E; | |
9248 | end if; | |
9249 | ||
9250 | Next_Entity (E); | |
9251 | end loop; | |
9252 | ||
9253 | return Empty; | |
9254 | end Controlling_Formal; | |
9255 | ||
9256 | -- Local variables | |
9257 | ||
9258 | Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim); | |
9259 | Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim); | |
9260 | ||
9261 | -- Start of processing for Is_Interface_Conformant | |
9262 | ||
ce2b6ba5 JM |
9263 | begin |
9264 | pragma Assert (Is_Subprogram (Iface_Prim) | |
9265 | and then Is_Subprogram (Prim) | |
9266 | and then Is_Dispatching_Operation (Iface_Prim) | |
9267 | and then Is_Dispatching_Operation (Prim)); | |
9268 | ||
fceeaab6 | 9269 | pragma Assert (Is_Interface (Iface) |
ce2b6ba5 JM |
9270 | or else (Present (Alias (Iface_Prim)) |
9271 | and then | |
9272 | Is_Interface | |
9273 | (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim))))); | |
9274 | ||
9275 | if Prim = Iface_Prim | |
9276 | or else not Is_Subprogram (Prim) | |
9277 | or else Ekind (Prim) /= Ekind (Iface_Prim) | |
9278 | or else not Is_Dispatching_Operation (Prim) | |
9279 | or else Scope (Prim) /= Scope (Tagged_Type) | |
fceeaab6 | 9280 | or else No (Typ) |
8a49a499 | 9281 | or else Base_Type (Typ) /= Base_Type (Tagged_Type) |
ce2b6ba5 JM |
9282 | or else not Primitive_Names_Match (Iface_Prim, Prim) |
9283 | then | |
9284 | return False; | |
9285 | ||
25ebc085 AC |
9286 | -- The mode of the controlling formals must match |
9287 | ||
9288 | elsif Present (Iface_Ctrl_F) | |
15918371 AC |
9289 | and then Present (Prim_Ctrl_F) |
9290 | and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F) | |
25ebc085 AC |
9291 | then |
9292 | return False; | |
9293 | ||
9294 | -- Case of a procedure, or a function whose result type matches the | |
9295 | -- result type of the interface primitive, or a function that has no | |
9296 | -- controlling result (I or access I). | |
ce2b6ba5 JM |
9297 | |
9298 | elsif Ekind (Iface_Prim) = E_Procedure | |
9299 | or else Etype (Prim) = Etype (Iface_Prim) | |
fceeaab6 | 9300 | or else not Has_Controlling_Result (Prim) |
ce2b6ba5 | 9301 | then |
b4d7b435 AC |
9302 | return Type_Conformant |
9303 | (Iface_Prim, Prim, Skip_Controlling_Formals => True); | |
ce2b6ba5 | 9304 | |
2995860f AC |
9305 | -- Case of a function returning an interface, or an access to one. Check |
9306 | -- that the return types correspond. | |
ce2b6ba5 | 9307 | |
fceeaab6 ES |
9308 | elsif Implements_Interface (Typ, Iface) then |
9309 | if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type) | |
9a3c9940 RD |
9310 | /= |
9311 | (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type) | |
fceeaab6 ES |
9312 | then |
9313 | return False; | |
fceeaab6 ES |
9314 | else |
9315 | return | |
9e92ad49 | 9316 | Type_Conformant (Prim, Ultimate_Alias (Iface_Prim), |
ce2b6ba5 | 9317 | Skip_Controlling_Formals => True); |
fceeaab6 | 9318 | end if; |
ce2b6ba5 | 9319 | |
fceeaab6 ES |
9320 | else |
9321 | return False; | |
ce2b6ba5 | 9322 | end if; |
ce2b6ba5 JM |
9323 | end Is_Interface_Conformant; |
9324 | ||
996ae0b0 RK |
9325 | --------------------------------- |
9326 | -- Is_Non_Overriding_Operation -- | |
9327 | --------------------------------- | |
9328 | ||
9329 | function Is_Non_Overriding_Operation | |
9330 | (Prev_E : Entity_Id; | |
d05ef0ab | 9331 | New_E : Entity_Id) return Boolean |
996ae0b0 RK |
9332 | is |
9333 | Formal : Entity_Id; | |
9334 | F_Typ : Entity_Id; | |
9335 | G_Typ : Entity_Id := Empty; | |
9336 | ||
9337 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
9338 | -- If F_Type is a derived type associated with a generic actual subtype, |
9339 | -- then return its Generic_Parent_Type attribute, else return Empty. | |
996ae0b0 RK |
9340 | |
9341 | function Types_Correspond | |
9342 | (P_Type : Entity_Id; | |
d05ef0ab | 9343 | N_Type : Entity_Id) return Boolean; |
82c80734 RD |
9344 | -- Returns true if and only if the types (or designated types in the |
9345 | -- case of anonymous access types) are the same or N_Type is derived | |
9346 | -- directly or indirectly from P_Type. | |
996ae0b0 RK |
9347 | |
9348 | ----------------------------- | |
9349 | -- Get_Generic_Parent_Type -- | |
9350 | ----------------------------- | |
9351 | ||
9352 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is | |
9353 | G_Typ : Entity_Id; | |
702d2020 | 9354 | Defn : Node_Id; |
996ae0b0 RK |
9355 | Indic : Node_Id; |
9356 | ||
9357 | begin | |
9358 | if Is_Derived_Type (F_Typ) | |
9359 | and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration | |
9360 | then | |
82c80734 RD |
9361 | -- The tree must be traversed to determine the parent subtype in |
9362 | -- the generic unit, which unfortunately isn't always available | |
9363 | -- via semantic attributes. ??? (Note: The use of Original_Node | |
9364 | -- is needed for cases where a full derived type has been | |
9365 | -- rewritten.) | |
996ae0b0 | 9366 | |
bff469f7 AC |
9367 | -- If the parent type is a scalar type, the derivation creates |
9368 | -- an anonymous base type for it, and the source type is its | |
9369 | -- first subtype. | |
9370 | ||
9371 | if Is_Scalar_Type (F_Typ) | |
9372 | and then not Comes_From_Source (F_Typ) | |
9373 | then | |
9374 | Defn := | |
9375 | Type_Definition | |
0c6826a5 | 9376 | (Original_Node (Parent (First_Subtype (F_Typ)))); |
bff469f7 AC |
9377 | else |
9378 | Defn := Type_Definition (Original_Node (Parent (F_Typ))); | |
9379 | end if; | |
702d2020 AC |
9380 | if Nkind (Defn) = N_Derived_Type_Definition then |
9381 | Indic := Subtype_Indication (Defn); | |
996ae0b0 | 9382 | |
702d2020 AC |
9383 | if Nkind (Indic) = N_Subtype_Indication then |
9384 | G_Typ := Entity (Subtype_Mark (Indic)); | |
9385 | else | |
9386 | G_Typ := Entity (Indic); | |
9387 | end if; | |
996ae0b0 | 9388 | |
702d2020 AC |
9389 | if Nkind (Parent (G_Typ)) = N_Subtype_Declaration |
9390 | and then Present (Generic_Parent_Type (Parent (G_Typ))) | |
9391 | then | |
9392 | return Generic_Parent_Type (Parent (G_Typ)); | |
9393 | end if; | |
996ae0b0 RK |
9394 | end if; |
9395 | end if; | |
9396 | ||
9397 | return Empty; | |
9398 | end Get_Generic_Parent_Type; | |
9399 | ||
9400 | ---------------------- | |
9401 | -- Types_Correspond -- | |
9402 | ---------------------- | |
9403 | ||
9404 | function Types_Correspond | |
9405 | (P_Type : Entity_Id; | |
d05ef0ab | 9406 | N_Type : Entity_Id) return Boolean |
996ae0b0 RK |
9407 | is |
9408 | Prev_Type : Entity_Id := Base_Type (P_Type); | |
9409 | New_Type : Entity_Id := Base_Type (N_Type); | |
9410 | ||
9411 | begin | |
9412 | if Ekind (Prev_Type) = E_Anonymous_Access_Type then | |
9413 | Prev_Type := Designated_Type (Prev_Type); | |
9414 | end if; | |
9415 | ||
9416 | if Ekind (New_Type) = E_Anonymous_Access_Type then | |
9417 | New_Type := Designated_Type (New_Type); | |
9418 | end if; | |
9419 | ||
9420 | if Prev_Type = New_Type then | |
9421 | return True; | |
9422 | ||
9423 | elsif not Is_Class_Wide_Type (New_Type) then | |
9424 | while Etype (New_Type) /= New_Type loop | |
9425 | New_Type := Etype (New_Type); | |
0c6826a5 | 9426 | |
996ae0b0 RK |
9427 | if New_Type = Prev_Type then |
9428 | return True; | |
9429 | end if; | |
9430 | end loop; | |
9431 | end if; | |
9432 | return False; | |
9433 | end Types_Correspond; | |
9434 | ||
9435 | -- Start of processing for Is_Non_Overriding_Operation | |
9436 | ||
9437 | begin | |
82c80734 RD |
9438 | -- In the case where both operations are implicit derived subprograms |
9439 | -- then neither overrides the other. This can only occur in certain | |
9440 | -- obscure cases (e.g., derivation from homographs created in a generic | |
9441 | -- instantiation). | |
996ae0b0 RK |
9442 | |
9443 | if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then | |
9444 | return True; | |
9445 | ||
9446 | elsif Ekind (Current_Scope) = E_Package | |
9447 | and then Is_Generic_Instance (Current_Scope) | |
9448 | and then In_Private_Part (Current_Scope) | |
9449 | and then Comes_From_Source (New_E) | |
9450 | then | |
702d2020 AC |
9451 | -- We examine the formals and result type of the inherited operation, |
9452 | -- to determine whether their type is derived from (the instance of) | |
9453 | -- a generic type. The first such formal or result type is the one | |
9454 | -- tested. | |
996ae0b0 RK |
9455 | |
9456 | Formal := First_Formal (Prev_E); | |
ae4c4d53 | 9457 | F_Typ := Empty; |
996ae0b0 RK |
9458 | while Present (Formal) loop |
9459 | F_Typ := Base_Type (Etype (Formal)); | |
9460 | ||
9461 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
9462 | F_Typ := Designated_Type (F_Typ); | |
9463 | end if; | |
9464 | ||
9465 | G_Typ := Get_Generic_Parent_Type (F_Typ); | |
702d2020 | 9466 | exit when Present (G_Typ); |
996ae0b0 RK |
9467 | |
9468 | Next_Formal (Formal); | |
9469 | end loop; | |
9470 | ||
890975e3 | 9471 | -- If the function dispatches on result check the result type |
ae4c4d53 | 9472 | |
c8ef728f | 9473 | if No (G_Typ) and then Ekind (Prev_E) = E_Function then |
996ae0b0 RK |
9474 | G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E))); |
9475 | end if; | |
9476 | ||
9477 | if No (G_Typ) then | |
9478 | return False; | |
9479 | end if; | |
9480 | ||
8dbd1460 AC |
9481 | -- If the generic type is a private type, then the original operation |
9482 | -- was not overriding in the generic, because there was no primitive | |
9483 | -- operation to override. | |
996ae0b0 RK |
9484 | |
9485 | if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration | |
9486 | and then Nkind (Formal_Type_Definition (Parent (G_Typ))) = | |
8dbd1460 | 9487 | N_Formal_Private_Type_Definition |
996ae0b0 RK |
9488 | then |
9489 | return True; | |
9490 | ||
9491 | -- The generic parent type is the ancestor of a formal derived | |
9492 | -- type declaration. We need to check whether it has a primitive | |
9493 | -- operation that should be overridden by New_E in the generic. | |
9494 | ||
9495 | else | |
9496 | declare | |
9497 | P_Formal : Entity_Id; | |
9498 | N_Formal : Entity_Id; | |
9499 | P_Typ : Entity_Id; | |
9500 | N_Typ : Entity_Id; | |
9501 | P_Prim : Entity_Id; | |
9502 | Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ)); | |
9503 | ||
9504 | begin | |
9505 | while Present (Prim_Elt) loop | |
9506 | P_Prim := Node (Prim_Elt); | |
fbf5a39b | 9507 | |
996ae0b0 RK |
9508 | if Chars (P_Prim) = Chars (New_E) |
9509 | and then Ekind (P_Prim) = Ekind (New_E) | |
9510 | then | |
9511 | P_Formal := First_Formal (P_Prim); | |
9512 | N_Formal := First_Formal (New_E); | |
9513 | while Present (P_Formal) and then Present (N_Formal) loop | |
9514 | P_Typ := Etype (P_Formal); | |
9515 | N_Typ := Etype (N_Formal); | |
9516 | ||
9517 | if not Types_Correspond (P_Typ, N_Typ) then | |
9518 | exit; | |
9519 | end if; | |
9520 | ||
9521 | Next_Entity (P_Formal); | |
9522 | Next_Entity (N_Formal); | |
9523 | end loop; | |
9524 | ||
82c80734 RD |
9525 | -- Found a matching primitive operation belonging to the |
9526 | -- formal ancestor type, so the new subprogram is | |
9527 | -- overriding. | |
996ae0b0 | 9528 | |
c8ef728f ES |
9529 | if No (P_Formal) |
9530 | and then No (N_Formal) | |
996ae0b0 RK |
9531 | and then (Ekind (New_E) /= E_Function |
9532 | or else | |
8fde064e AC |
9533 | Types_Correspond |
9534 | (Etype (P_Prim), Etype (New_E))) | |
996ae0b0 RK |
9535 | then |
9536 | return False; | |
9537 | end if; | |
9538 | end if; | |
9539 | ||
9540 | Next_Elmt (Prim_Elt); | |
9541 | end loop; | |
9542 | ||
2995860f AC |
9543 | -- If no match found, then the new subprogram does not override |
9544 | -- in the generic (nor in the instance). | |
996ae0b0 | 9545 | |
260359e3 AC |
9546 | -- If the type in question is not abstract, and the subprogram |
9547 | -- is, this will be an error if the new operation is in the | |
9548 | -- private part of the instance. Emit a warning now, which will | |
9549 | -- make the subsequent error message easier to understand. | |
9550 | ||
ae4c4d53 | 9551 | if Present (F_Typ) and then not Is_Abstract_Type (F_Typ) |
260359e3 AC |
9552 | and then Is_Abstract_Subprogram (Prev_E) |
9553 | and then In_Private_Part (Current_Scope) | |
9554 | then | |
9555 | Error_Msg_Node_2 := F_Typ; | |
9556 | Error_Msg_NE | |
3ccedacc AC |
9557 | ("private operation& in generic unit does not override " |
9558 | & "any primitive operation of& (RM 12.3 (18))??", | |
260359e3 AC |
9559 | New_E, New_E); |
9560 | end if; | |
9561 | ||
996ae0b0 RK |
9562 | return True; |
9563 | end; | |
9564 | end if; | |
9565 | else | |
9566 | return False; | |
9567 | end if; | |
9568 | end Is_Non_Overriding_Operation; | |
9569 | ||
beacce02 AC |
9570 | ------------------------------------- |
9571 | -- List_Inherited_Pre_Post_Aspects -- | |
9572 | ------------------------------------- | |
9573 | ||
9574 | procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is | |
9575 | begin | |
e606088a | 9576 | if Opt.List_Inherited_Aspects |
b9696ffb | 9577 | and then Is_Subprogram_Or_Generic_Subprogram (E) |
beacce02 AC |
9578 | then |
9579 | declare | |
c9d70ab1 AC |
9580 | Subps : constant Subprogram_List := Inherited_Subprograms (E); |
9581 | Items : Node_Id; | |
9582 | Prag : Node_Id; | |
beacce02 AC |
9583 | |
9584 | begin | |
c9d70ab1 AC |
9585 | for Index in Subps'Range loop |
9586 | Items := Contract (Subps (Index)); | |
9587 | ||
9588 | if Present (Items) then | |
9589 | Prag := Pre_Post_Conditions (Items); | |
9590 | while Present (Prag) loop | |
9591 | Error_Msg_Sloc := Sloc (Prag); | |
9592 | ||
9593 | if Class_Present (Prag) | |
9594 | and then not Split_PPC (Prag) | |
9595 | then | |
6e759c2a | 9596 | if Pragma_Name (Prag) = Name_Precondition then |
c9d70ab1 AC |
9597 | Error_Msg_N |
9598 | ("info: & inherits `Pre''Class` aspect from " | |
9599 | & "#?L?", E); | |
9600 | else | |
9601 | Error_Msg_N | |
9602 | ("info: & inherits `Post''Class` aspect from " | |
9603 | & "#?L?", E); | |
9604 | end if; | |
beacce02 | 9605 | end if; |
beacce02 | 9606 | |
c9d70ab1 AC |
9607 | Prag := Next_Pragma (Prag); |
9608 | end loop; | |
9609 | end if; | |
beacce02 AC |
9610 | end loop; |
9611 | end; | |
9612 | end if; | |
9613 | end List_Inherited_Pre_Post_Aspects; | |
9614 | ||
996ae0b0 RK |
9615 | ------------------------------ |
9616 | -- Make_Inequality_Operator -- | |
9617 | ------------------------------ | |
9618 | ||
9619 | -- S is the defining identifier of an equality operator. We build a | |
9620 | -- subprogram declaration with the right signature. This operation is | |
9621 | -- intrinsic, because it is always expanded as the negation of the | |
9622 | -- call to the equality function. | |
9623 | ||
9624 | procedure Make_Inequality_Operator (S : Entity_Id) is | |
9625 | Loc : constant Source_Ptr := Sloc (S); | |
9626 | Decl : Node_Id; | |
9627 | Formals : List_Id; | |
9628 | Op_Name : Entity_Id; | |
9629 | ||
c8ef728f ES |
9630 | FF : constant Entity_Id := First_Formal (S); |
9631 | NF : constant Entity_Id := Next_Formal (FF); | |
996ae0b0 RK |
9632 | |
9633 | begin | |
c8ef728f | 9634 | -- Check that equality was properly defined, ignore call if not |
996ae0b0 | 9635 | |
c8ef728f | 9636 | if No (NF) then |
996ae0b0 RK |
9637 | return; |
9638 | end if; | |
9639 | ||
c8ef728f ES |
9640 | declare |
9641 | A : constant Entity_Id := | |
9642 | Make_Defining_Identifier (Sloc (FF), | |
9643 | Chars => Chars (FF)); | |
9644 | ||
5d37ba92 ES |
9645 | B : constant Entity_Id := |
9646 | Make_Defining_Identifier (Sloc (NF), | |
9647 | Chars => Chars (NF)); | |
c8ef728f ES |
9648 | |
9649 | begin | |
9650 | Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne); | |
9651 | ||
9652 | Formals := New_List ( | |
9653 | Make_Parameter_Specification (Loc, | |
9654 | Defining_Identifier => A, | |
9655 | Parameter_Type => | |
e4494292 | 9656 | New_Occurrence_Of (Etype (First_Formal (S)), |
c8ef728f ES |
9657 | Sloc (Etype (First_Formal (S))))), |
9658 | ||
9659 | Make_Parameter_Specification (Loc, | |
9660 | Defining_Identifier => B, | |
9661 | Parameter_Type => | |
e4494292 | 9662 | New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))), |
c8ef728f ES |
9663 | Sloc (Etype (Next_Formal (First_Formal (S))))))); |
9664 | ||
9665 | Decl := | |
9666 | Make_Subprogram_Declaration (Loc, | |
9667 | Specification => | |
9668 | Make_Function_Specification (Loc, | |
9669 | Defining_Unit_Name => Op_Name, | |
9670 | Parameter_Specifications => Formals, | |
9671 | Result_Definition => | |
e4494292 | 9672 | New_Occurrence_Of (Standard_Boolean, Loc))); |
c8ef728f ES |
9673 | |
9674 | -- Insert inequality right after equality if it is explicit or after | |
9675 | -- the derived type when implicit. These entities are created only | |
2995860f AC |
9676 | -- for visibility purposes, and eventually replaced in the course |
9677 | -- of expansion, so they do not need to be attached to the tree and | |
9678 | -- seen by the back-end. Keeping them internal also avoids spurious | |
c8ef728f ES |
9679 | -- freezing problems. The declaration is inserted in the tree for |
9680 | -- analysis, and removed afterwards. If the equality operator comes | |
9681 | -- from an explicit declaration, attach the inequality immediately | |
9682 | -- after. Else the equality is inherited from a derived type | |
9683 | -- declaration, so insert inequality after that declaration. | |
9684 | ||
9685 | if No (Alias (S)) then | |
9686 | Insert_After (Unit_Declaration_Node (S), Decl); | |
9687 | elsif Is_List_Member (Parent (S)) then | |
9688 | Insert_After (Parent (S), Decl); | |
9689 | else | |
9690 | Insert_After (Parent (Etype (First_Formal (S))), Decl); | |
9691 | end if; | |
996ae0b0 | 9692 | |
c8ef728f ES |
9693 | Mark_Rewrite_Insertion (Decl); |
9694 | Set_Is_Intrinsic_Subprogram (Op_Name); | |
9695 | Analyze (Decl); | |
9696 | Remove (Decl); | |
9697 | Set_Has_Completion (Op_Name); | |
9698 | Set_Corresponding_Equality (Op_Name, S); | |
f937473f | 9699 | Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S)); |
c8ef728f | 9700 | end; |
996ae0b0 RK |
9701 | end Make_Inequality_Operator; |
9702 | ||
9703 | ---------------------- | |
9704 | -- May_Need_Actuals -- | |
9705 | ---------------------- | |
9706 | ||
9707 | procedure May_Need_Actuals (Fun : Entity_Id) is | |
9708 | F : Entity_Id; | |
9709 | B : Boolean; | |
9710 | ||
9711 | begin | |
9712 | F := First_Formal (Fun); | |
9713 | B := True; | |
996ae0b0 RK |
9714 | while Present (F) loop |
9715 | if No (Default_Value (F)) then | |
9716 | B := False; | |
9717 | exit; | |
9718 | end if; | |
9719 | ||
9720 | Next_Formal (F); | |
9721 | end loop; | |
9722 | ||
9723 | Set_Needs_No_Actuals (Fun, B); | |
9724 | end May_Need_Actuals; | |
9725 | ||
9726 | --------------------- | |
9727 | -- Mode_Conformant -- | |
9728 | --------------------- | |
9729 | ||
9730 | function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
9731 | Result : Boolean; | |
996ae0b0 RK |
9732 | begin |
9733 | Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result); | |
9734 | return Result; | |
9735 | end Mode_Conformant; | |
9736 | ||
9737 | --------------------------- | |
9738 | -- New_Overloaded_Entity -- | |
9739 | --------------------------- | |
9740 | ||
9741 | procedure New_Overloaded_Entity | |
9742 | (S : Entity_Id; | |
9743 | Derived_Type : Entity_Id := Empty) | |
9744 | is | |
ec4867fa | 9745 | Overridden_Subp : Entity_Id := Empty; |
758c442c GD |
9746 | -- Set if the current scope has an operation that is type-conformant |
9747 | -- with S, and becomes hidden by S. | |
9748 | ||
5d37ba92 ES |
9749 | Is_Primitive_Subp : Boolean; |
9750 | -- Set to True if the new subprogram is primitive | |
9751 | ||
fbf5a39b AC |
9752 | E : Entity_Id; |
9753 | -- Entity that S overrides | |
9754 | ||
996ae0b0 | 9755 | Prev_Vis : Entity_Id := Empty; |
ec4867fa ES |
9756 | -- Predecessor of E in Homonym chain |
9757 | ||
5d37ba92 ES |
9758 | procedure Check_For_Primitive_Subprogram |
9759 | (Is_Primitive : out Boolean; | |
9760 | Is_Overriding : Boolean := False); | |
9761 | -- If the subprogram being analyzed is a primitive operation of the type | |
9762 | -- of a formal or result, set the Has_Primitive_Operations flag on the | |
9763 | -- type, and set Is_Primitive to True (otherwise set to False). Set the | |
9764 | -- corresponding flag on the entity itself for later use. | |
9765 | ||
42f11e4c AC |
9766 | function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean; |
9767 | -- True if a) E is a subprogram whose first formal is a concurrent type | |
9768 | -- defined in the scope of E that has some entry or subprogram whose | |
9769 | -- profile matches E, or b) E is an internally built dispatching | |
9770 | -- subprogram of a protected type and there is a matching subprogram | |
9771 | -- defined in the enclosing scope of the protected type, or c) E is | |
9772 | -- an entry of a synchronized type and a matching procedure has been | |
9773 | -- previously defined in the enclosing scope of the synchronized type. | |
758c442c | 9774 | |
996ae0b0 RK |
9775 | function Is_Private_Declaration (E : Entity_Id) return Boolean; |
9776 | -- Check that E is declared in the private part of the current package, | |
9777 | -- or in the package body, where it may hide a previous declaration. | |
fbf5a39b | 9778 | -- We can't use In_Private_Part by itself because this flag is also |
996ae0b0 RK |
9779 | -- set when freezing entities, so we must examine the place of the |
9780 | -- declaration in the tree, and recognize wrapper packages as well. | |
9781 | ||
2ddc2000 AC |
9782 | function Is_Overriding_Alias |
9783 | (Old_E : Entity_Id; | |
9784 | New_E : Entity_Id) return Boolean; | |
9785 | -- Check whether new subprogram and old subprogram are both inherited | |
9786 | -- from subprograms that have distinct dispatch table entries. This can | |
2995860f AC |
9787 | -- occur with derivations from instances with accidental homonyms. The |
9788 | -- function is conservative given that the converse is only true within | |
9789 | -- instances that contain accidental overloadings. | |
2ddc2000 | 9790 | |
42f11e4c | 9791 | procedure Report_Conflict (S : Entity_Id; E : Entity_Id); |
ca90b962 | 9792 | -- Report conflict between entities S and E |
42f11e4c | 9793 | |
5d37ba92 ES |
9794 | ------------------------------------ |
9795 | -- Check_For_Primitive_Subprogram -- | |
9796 | ------------------------------------ | |
996ae0b0 | 9797 | |
5d37ba92 ES |
9798 | procedure Check_For_Primitive_Subprogram |
9799 | (Is_Primitive : out Boolean; | |
9800 | Is_Overriding : Boolean := False) | |
ec4867fa | 9801 | is |
996ae0b0 RK |
9802 | Formal : Entity_Id; |
9803 | F_Typ : Entity_Id; | |
07fc65c4 | 9804 | B_Typ : Entity_Id; |
996ae0b0 RK |
9805 | |
9806 | function Visible_Part_Type (T : Entity_Id) return Boolean; | |
8dbd1460 AC |
9807 | -- Returns true if T is declared in the visible part of the current |
9808 | -- package scope; otherwise returns false. Assumes that T is declared | |
9809 | -- in a package. | |
996ae0b0 RK |
9810 | |
9811 | procedure Check_Private_Overriding (T : Entity_Id); | |
9812 | -- Checks that if a primitive abstract subprogram of a visible | |
8dbd1460 AC |
9813 | -- abstract type is declared in a private part, then it must override |
9814 | -- an abstract subprogram declared in the visible part. Also checks | |
9815 | -- that if a primitive function with a controlling result is declared | |
9816 | -- in a private part, then it must override a function declared in | |
9817 | -- the visible part. | |
996ae0b0 RK |
9818 | |
9819 | ------------------------------ | |
9820 | -- Check_Private_Overriding -- | |
9821 | ------------------------------ | |
9822 | ||
9823 | procedure Check_Private_Overriding (T : Entity_Id) is | |
6672e402 AC |
9824 | function Overrides_Private_Part_Op return Boolean; |
9825 | -- This detects the special case where the overriding subprogram | |
9826 | -- is overriding a subprogram that was declared in the same | |
9827 | -- private part. That case is illegal by 3.9.3(10). | |
acf624f2 | 9828 | |
aaeb3b3a AC |
9829 | function Overrides_Visible_Function |
9830 | (Partial_View : Entity_Id) return Boolean; | |
acf624f2 BD |
9831 | -- True if S overrides a function in the visible part. The |
9832 | -- overridden function could be explicitly or implicitly declared. | |
9833 | ||
6672e402 AC |
9834 | ------------------------------- |
9835 | -- Overrides_Private_Part_Op -- | |
9836 | ------------------------------- | |
9837 | ||
9838 | function Overrides_Private_Part_Op return Boolean is | |
9839 | Over_Decl : constant Node_Id := | |
9840 | Unit_Declaration_Node (Overridden_Operation (S)); | |
9841 | Subp_Decl : constant Node_Id := Unit_Declaration_Node (S); | |
9842 | ||
9843 | begin | |
9844 | pragma Assert (Is_Overriding); | |
9845 | pragma Assert | |
9846 | (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration); | |
9847 | pragma Assert | |
9848 | (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration); | |
9849 | ||
9850 | return In_Same_List (Over_Decl, Subp_Decl); | |
9851 | end Overrides_Private_Part_Op; | |
9852 | ||
9853 | -------------------------------- | |
9854 | -- Overrides_Visible_Function -- | |
9855 | -------------------------------- | |
bc38dbb4 | 9856 | |
aaeb3b3a AC |
9857 | function Overrides_Visible_Function |
9858 | (Partial_View : Entity_Id) return Boolean | |
9859 | is | |
acf624f2 BD |
9860 | begin |
9861 | if not Is_Overriding or else not Has_Homonym (S) then | |
9862 | return False; | |
9863 | end if; | |
9864 | ||
aaeb3b3a | 9865 | if not Present (Partial_View) then |
acf624f2 BD |
9866 | return True; |
9867 | end if; | |
9868 | ||
9869 | -- Search through all the homonyms H of S in the current | |
9870 | -- package spec, and return True if we find one that matches. | |
9871 | -- Note that Parent (H) will be the declaration of the | |
aaeb3b3a | 9872 | -- partial view of T for a match. |
acf624f2 BD |
9873 | |
9874 | declare | |
9875 | H : Entity_Id := S; | |
9876 | begin | |
9877 | loop | |
9878 | H := Homonym (H); | |
9879 | exit when not Present (H) or else Scope (H) /= Scope (S); | |
9880 | ||
9881 | if Nkind_In | |
9882 | (Parent (H), | |
9883 | N_Private_Extension_Declaration, | |
9884 | N_Private_Type_Declaration) | |
aaeb3b3a | 9885 | and then Defining_Identifier (Parent (H)) = Partial_View |
acf624f2 BD |
9886 | then |
9887 | return True; | |
9888 | end if; | |
9889 | end loop; | |
9890 | end; | |
9891 | ||
9892 | return False; | |
9893 | end Overrides_Visible_Function; | |
9894 | ||
9895 | -- Start of processing for Check_Private_Overriding | |
9896 | ||
996ae0b0 | 9897 | begin |
51c16e29 | 9898 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
9899 | and then In_Private_Part (Current_Scope) |
9900 | and then Visible_Part_Type (T) | |
9901 | and then not In_Instance | |
9902 | then | |
f937473f RD |
9903 | if Is_Abstract_Type (T) |
9904 | and then Is_Abstract_Subprogram (S) | |
9905 | and then (not Is_Overriding | |
bc38dbb4 | 9906 | or else not Is_Abstract_Subprogram (E) |
6672e402 | 9907 | or else Overrides_Private_Part_Op) |
996ae0b0 | 9908 | then |
6672e402 AC |
9909 | Error_Msg_N |
9910 | ("abstract subprograms must be visible (RM 3.9.3(10))!", | |
9911 | S); | |
758c442c | 9912 | |
aaeb3b3a AC |
9913 | elsif Ekind (S) = E_Function then |
9914 | declare | |
9915 | Partial_View : constant Entity_Id := | |
9916 | Incomplete_Or_Partial_View (T); | |
2e79de51 | 9917 | |
aaeb3b3a AC |
9918 | begin |
9919 | if not Overrides_Visible_Function (Partial_View) then | |
9920 | ||
9921 | -- Here, S is "function ... return T;" declared in | |
9922 | -- the private part, not overriding some visible | |
9923 | -- operation. That's illegal in the tagged case | |
9924 | -- (but not if the private type is untagged). | |
9925 | ||
9926 | if ((Present (Partial_View) | |
9927 | and then Is_Tagged_Type (Partial_View)) | |
9928 | or else (not Present (Partial_View) | |
9929 | and then Is_Tagged_Type (T))) | |
9930 | and then T = Base_Type (Etype (S)) | |
9931 | then | |
9932 | Error_Msg_N | |
9933 | ("private function with tagged result must" | |
9934 | & " override visible-part function", S); | |
9935 | Error_Msg_N | |
9936 | ("\move subprogram to the visible part" | |
9937 | & " (RM 3.9.3(10))", S); | |
9938 | ||
9939 | -- AI05-0073: extend this test to the case of a | |
9940 | -- function with a controlling access result. | |
9941 | ||
9942 | elsif Ekind (Etype (S)) = E_Anonymous_Access_Type | |
9943 | and then Is_Tagged_Type (Designated_Type (Etype (S))) | |
9944 | and then | |
9945 | not Is_Class_Wide_Type | |
9946 | (Designated_Type (Etype (S))) | |
9947 | and then Ada_Version >= Ada_2012 | |
9948 | then | |
9949 | Error_Msg_N | |
9950 | ("private function with controlling access " | |
9951 | & "result must override visible-part function", | |
9952 | S); | |
9953 | Error_Msg_N | |
9954 | ("\move subprogram to the visible part" | |
9955 | & " (RM 3.9.3(10))", S); | |
9956 | end if; | |
9957 | end if; | |
9958 | end; | |
996ae0b0 RK |
9959 | end if; |
9960 | end if; | |
9961 | end Check_Private_Overriding; | |
9962 | ||
9963 | ----------------------- | |
9964 | -- Visible_Part_Type -- | |
9965 | ----------------------- | |
9966 | ||
9967 | function Visible_Part_Type (T : Entity_Id) return Boolean is | |
07fc65c4 GB |
9968 | P : constant Node_Id := Unit_Declaration_Node (Scope (T)); |
9969 | N : Node_Id; | |
996ae0b0 RK |
9970 | |
9971 | begin | |
8dbd1460 AC |
9972 | -- If the entity is a private type, then it must be declared in a |
9973 | -- visible part. | |
996ae0b0 RK |
9974 | |
9975 | if Ekind (T) in Private_Kind then | |
9976 | return True; | |
9977 | end if; | |
9978 | ||
9979 | -- Otherwise, we traverse the visible part looking for its | |
9980 | -- corresponding declaration. We cannot use the declaration | |
9981 | -- node directly because in the private part the entity of a | |
9982 | -- private type is the one in the full view, which does not | |
9983 | -- indicate that it is the completion of something visible. | |
9984 | ||
07fc65c4 | 9985 | N := First (Visible_Declarations (Specification (P))); |
996ae0b0 RK |
9986 | while Present (N) loop |
9987 | if Nkind (N) = N_Full_Type_Declaration | |
9988 | and then Present (Defining_Identifier (N)) | |
9989 | and then T = Defining_Identifier (N) | |
9990 | then | |
9991 | return True; | |
9992 | ||
800621e0 RD |
9993 | elsif Nkind_In (N, N_Private_Type_Declaration, |
9994 | N_Private_Extension_Declaration) | |
996ae0b0 RK |
9995 | and then Present (Defining_Identifier (N)) |
9996 | and then T = Full_View (Defining_Identifier (N)) | |
9997 | then | |
9998 | return True; | |
9999 | end if; | |
10000 | ||
10001 | Next (N); | |
10002 | end loop; | |
10003 | ||
10004 | return False; | |
10005 | end Visible_Part_Type; | |
10006 | ||
5d37ba92 | 10007 | -- Start of processing for Check_For_Primitive_Subprogram |
996ae0b0 RK |
10008 | |
10009 | begin | |
5d37ba92 ES |
10010 | Is_Primitive := False; |
10011 | ||
996ae0b0 RK |
10012 | if not Comes_From_Source (S) then |
10013 | null; | |
10014 | ||
5d37ba92 | 10015 | -- If subprogram is at library level, it is not primitive operation |
15ce9ca2 AC |
10016 | |
10017 | elsif Current_Scope = Standard_Standard then | |
10018 | null; | |
10019 | ||
b9b2405f | 10020 | elsif (Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 | 10021 | and then not In_Package_Body (Current_Scope)) |
82c80734 | 10022 | or else Is_Overriding |
996ae0b0 | 10023 | then |
07fc65c4 | 10024 | -- For function, check return type |
996ae0b0 | 10025 | |
07fc65c4 | 10026 | if Ekind (S) = E_Function then |
5d37ba92 ES |
10027 | if Ekind (Etype (S)) = E_Anonymous_Access_Type then |
10028 | F_Typ := Designated_Type (Etype (S)); | |
10029 | else | |
10030 | F_Typ := Etype (S); | |
10031 | end if; | |
10032 | ||
10033 | B_Typ := Base_Type (F_Typ); | |
07fc65c4 | 10034 | |
5d37ba92 ES |
10035 | if Scope (B_Typ) = Current_Scope |
10036 | and then not Is_Class_Wide_Type (B_Typ) | |
10037 | and then not Is_Generic_Type (B_Typ) | |
10038 | then | |
10039 | Is_Primitive := True; | |
07fc65c4 | 10040 | Set_Has_Primitive_Operations (B_Typ); |
5d37ba92 | 10041 | Set_Is_Primitive (S); |
07fc65c4 | 10042 | Check_Private_Overriding (B_Typ); |
4179af27 | 10043 | |
d65a80fd HK |
10044 | -- The Ghost policy in effect at the point of declaration |
10045 | -- or a tagged type and a primitive operation must match | |
4179af27 HK |
10046 | -- (SPARK RM 6.9(16)). |
10047 | ||
10048 | Check_Ghost_Primitive (S, B_Typ); | |
07fc65c4 | 10049 | end if; |
996ae0b0 RK |
10050 | end if; |
10051 | ||
07fc65c4 | 10052 | -- For all subprograms, check formals |
996ae0b0 | 10053 | |
07fc65c4 | 10054 | Formal := First_Formal (S); |
996ae0b0 RK |
10055 | while Present (Formal) loop |
10056 | if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then | |
10057 | F_Typ := Designated_Type (Etype (Formal)); | |
10058 | else | |
10059 | F_Typ := Etype (Formal); | |
10060 | end if; | |
10061 | ||
07fc65c4 GB |
10062 | B_Typ := Base_Type (F_Typ); |
10063 | ||
ec4867fa ES |
10064 | if Ekind (B_Typ) = E_Access_Subtype then |
10065 | B_Typ := Base_Type (B_Typ); | |
10066 | end if; | |
10067 | ||
5d37ba92 ES |
10068 | if Scope (B_Typ) = Current_Scope |
10069 | and then not Is_Class_Wide_Type (B_Typ) | |
10070 | and then not Is_Generic_Type (B_Typ) | |
10071 | then | |
10072 | Is_Primitive := True; | |
10073 | Set_Is_Primitive (S); | |
07fc65c4 GB |
10074 | Set_Has_Primitive_Operations (B_Typ); |
10075 | Check_Private_Overriding (B_Typ); | |
4179af27 | 10076 | |
d65a80fd HK |
10077 | -- The Ghost policy in effect at the point of declaration |
10078 | -- of a tagged type and a primitive operation must match | |
4179af27 HK |
10079 | -- (SPARK RM 6.9(16)). |
10080 | ||
10081 | Check_Ghost_Primitive (S, B_Typ); | |
996ae0b0 RK |
10082 | end if; |
10083 | ||
10084 | Next_Formal (Formal); | |
10085 | end loop; | |
1aee1fb3 AC |
10086 | |
10087 | -- Special case: An equality function can be redefined for a type | |
10088 | -- occurring in a declarative part, and won't otherwise be treated as | |
10089 | -- a primitive because it doesn't occur in a package spec and doesn't | |
10090 | -- override an inherited subprogram. It's important that we mark it | |
10091 | -- primitive so it can be returned by Collect_Primitive_Operations | |
10092 | -- and be used in composing the equality operation of later types | |
10093 | -- that have a component of the type. | |
10094 | ||
10095 | elsif Chars (S) = Name_Op_Eq | |
10096 | and then Etype (S) = Standard_Boolean | |
10097 | then | |
10098 | B_Typ := Base_Type (Etype (First_Formal (S))); | |
10099 | ||
10100 | if Scope (B_Typ) = Current_Scope | |
10101 | and then | |
10102 | Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ | |
10103 | and then not Is_Limited_Type (B_Typ) | |
10104 | then | |
10105 | Is_Primitive := True; | |
10106 | Set_Is_Primitive (S); | |
10107 | Set_Has_Primitive_Operations (B_Typ); | |
10108 | Check_Private_Overriding (B_Typ); | |
4179af27 HK |
10109 | |
10110 | -- The Ghost policy in effect at the point of declaration of a | |
10111 | -- tagged type and a primitive operation must match | |
10112 | -- (SPARK RM 6.9(16)). | |
10113 | ||
10114 | Check_Ghost_Primitive (S, B_Typ); | |
1aee1fb3 | 10115 | end if; |
996ae0b0 | 10116 | end if; |
5d37ba92 ES |
10117 | end Check_For_Primitive_Subprogram; |
10118 | ||
42f11e4c AC |
10119 | -------------------------------------- |
10120 | -- Has_Matching_Entry_Or_Subprogram -- | |
10121 | -------------------------------------- | |
5d37ba92 | 10122 | |
bac5ba15 AC |
10123 | function Has_Matching_Entry_Or_Subprogram |
10124 | (E : Entity_Id) return Boolean | |
5d37ba92 | 10125 | is |
42f11e4c AC |
10126 | function Check_Conforming_Parameters |
10127 | (E1_Param : Node_Id; | |
10128 | E2_Param : Node_Id) return Boolean; | |
10129 | -- Starting from the given parameters, check that all the parameters | |
ca90b962 GD |
10130 | -- of two entries or subprograms are subtype conformant. Used to skip |
10131 | -- the check on the controlling argument. | |
42f11e4c AC |
10132 | |
10133 | function Matching_Entry_Or_Subprogram | |
10134 | (Conc_Typ : Entity_Id; | |
10135 | Subp : Entity_Id) return Entity_Id; | |
10136 | -- Return the first entry or subprogram of the given concurrent type | |
10137 | -- whose name matches the name of Subp and has a profile conformant | |
10138 | -- with Subp; return Empty if not found. | |
10139 | ||
10140 | function Matching_Dispatching_Subprogram | |
10141 | (Conc_Typ : Entity_Id; | |
10142 | Ent : Entity_Id) return Entity_Id; | |
10143 | -- Return the first dispatching primitive of Conc_Type defined in the | |
ca90b962 | 10144 | -- enclosing scope of Conc_Type (i.e. before the full definition of |
42f11e4c AC |
10145 | -- this concurrent type) whose name matches the entry Ent and has a |
10146 | -- profile conformant with the profile of the corresponding (not yet | |
10147 | -- built) dispatching primitive of Ent; return Empty if not found. | |
10148 | ||
10149 | function Matching_Original_Protected_Subprogram | |
10150 | (Prot_Typ : Entity_Id; | |
10151 | Subp : Entity_Id) return Entity_Id; | |
10152 | -- Return the first subprogram defined in the enclosing scope of | |
10153 | -- Prot_Typ (before the full definition of this protected type) | |
10154 | -- whose name matches the original name of Subp and has a profile | |
10155 | -- conformant with the profile of Subp; return Empty if not found. | |
10156 | ||
10157 | --------------------------------- | |
10158 | -- Check_Confirming_Parameters -- | |
10159 | --------------------------------- | |
10160 | ||
10161 | function Check_Conforming_Parameters | |
10162 | (E1_Param : Node_Id; | |
10163 | E2_Param : Node_Id) return Boolean | |
8aa15e3b | 10164 | is |
42f11e4c AC |
10165 | Param_E1 : Node_Id := E1_Param; |
10166 | Param_E2 : Node_Id := E2_Param; | |
8aa15e3b JM |
10167 | |
10168 | begin | |
42f11e4c | 10169 | while Present (Param_E1) and then Present (Param_E2) loop |
bac5ba15 AC |
10170 | if Ekind (Defining_Identifier (Param_E1)) /= |
10171 | Ekind (Defining_Identifier (Param_E2)) | |
42f11e4c | 10172 | or else not |
bac5ba15 AC |
10173 | Conforming_Types |
10174 | (Find_Parameter_Type (Param_E1), | |
10175 | Find_Parameter_Type (Param_E2), | |
10176 | Subtype_Conformant) | |
15e4986c | 10177 | then |
42f11e4c | 10178 | return False; |
8aa15e3b JM |
10179 | end if; |
10180 | ||
42f11e4c AC |
10181 | Next (Param_E1); |
10182 | Next (Param_E2); | |
10183 | end loop; | |
8aa15e3b | 10184 | |
42f11e4c AC |
10185 | -- The candidate is not valid if one of the two lists contains |
10186 | -- more parameters than the other | |
8aa15e3b | 10187 | |
42f11e4c AC |
10188 | return No (Param_E1) and then No (Param_E2); |
10189 | end Check_Conforming_Parameters; | |
8aa15e3b | 10190 | |
42f11e4c AC |
10191 | ---------------------------------- |
10192 | -- Matching_Entry_Or_Subprogram -- | |
10193 | ---------------------------------- | |
8aa15e3b | 10194 | |
42f11e4c AC |
10195 | function Matching_Entry_Or_Subprogram |
10196 | (Conc_Typ : Entity_Id; | |
10197 | Subp : Entity_Id) return Entity_Id | |
10198 | is | |
10199 | E : Entity_Id; | |
8aa15e3b | 10200 | |
42f11e4c AC |
10201 | begin |
10202 | E := First_Entity (Conc_Typ); | |
10203 | while Present (E) loop | |
10204 | if Chars (Subp) = Chars (E) | |
10205 | and then (Ekind (E) = E_Entry or else Is_Subprogram (E)) | |
10206 | and then | |
10207 | Check_Conforming_Parameters | |
10208 | (First (Parameter_Specifications (Parent (E))), | |
10209 | Next (First (Parameter_Specifications (Parent (Subp))))) | |
8aa15e3b | 10210 | then |
42f11e4c | 10211 | return E; |
8aa15e3b JM |
10212 | end if; |
10213 | ||
42f11e4c | 10214 | Next_Entity (E); |
8aa15e3b JM |
10215 | end loop; |
10216 | ||
42f11e4c AC |
10217 | return Empty; |
10218 | end Matching_Entry_Or_Subprogram; | |
8aa15e3b | 10219 | |
42f11e4c AC |
10220 | ------------------------------------- |
10221 | -- Matching_Dispatching_Subprogram -- | |
10222 | ------------------------------------- | |
8aa15e3b | 10223 | |
42f11e4c AC |
10224 | function Matching_Dispatching_Subprogram |
10225 | (Conc_Typ : Entity_Id; | |
10226 | Ent : Entity_Id) return Entity_Id | |
10227 | is | |
10228 | E : Entity_Id; | |
8aa15e3b | 10229 | |
42f11e4c AC |
10230 | begin |
10231 | -- Search for entities in the enclosing scope of this synchonized | |
bac5ba15 | 10232 | -- type. |
8aa15e3b | 10233 | |
42f11e4c AC |
10234 | pragma Assert (Is_Concurrent_Type (Conc_Typ)); |
10235 | Push_Scope (Scope (Conc_Typ)); | |
10236 | E := Current_Entity_In_Scope (Ent); | |
10237 | Pop_Scope; | |
5d37ba92 | 10238 | |
42f11e4c AC |
10239 | while Present (E) loop |
10240 | if Scope (E) = Scope (Conc_Typ) | |
10241 | and then Comes_From_Source (E) | |
10242 | and then Ekind (E) = E_Procedure | |
10243 | and then Present (First_Entity (E)) | |
10244 | and then Is_Controlling_Formal (First_Entity (E)) | |
10245 | and then Etype (First_Entity (E)) = Conc_Typ | |
10246 | and then | |
10247 | Check_Conforming_Parameters | |
10248 | (First (Parameter_Specifications (Parent (Ent))), | |
10249 | Next (First (Parameter_Specifications (Parent (E))))) | |
10250 | then | |
10251 | return E; | |
10252 | end if; | |
5d37ba92 | 10253 | |
42f11e4c AC |
10254 | E := Homonym (E); |
10255 | end loop; | |
5d37ba92 | 10256 | |
42f11e4c AC |
10257 | return Empty; |
10258 | end Matching_Dispatching_Subprogram; | |
5d37ba92 | 10259 | |
42f11e4c AC |
10260 | -------------------------------------------- |
10261 | -- Matching_Original_Protected_Subprogram -- | |
10262 | -------------------------------------------- | |
5d37ba92 | 10263 | |
42f11e4c AC |
10264 | function Matching_Original_Protected_Subprogram |
10265 | (Prot_Typ : Entity_Id; | |
10266 | Subp : Entity_Id) return Entity_Id | |
10267 | is | |
10268 | ICF : constant Boolean := | |
10269 | Is_Controlling_Formal (First_Entity (Subp)); | |
10270 | E : Entity_Id; | |
8aa15e3b | 10271 | |
42f11e4c AC |
10272 | begin |
10273 | -- Temporarily decorate the first parameter of Subp as controlling | |
bac5ba15 | 10274 | -- formal, required to invoke Subtype_Conformant. |
5d37ba92 | 10275 | |
42f11e4c | 10276 | Set_Is_Controlling_Formal (First_Entity (Subp)); |
5d37ba92 | 10277 | |
42f11e4c AC |
10278 | E := |
10279 | Current_Entity_In_Scope (Original_Protected_Subprogram (Subp)); | |
8aa15e3b | 10280 | |
42f11e4c AC |
10281 | while Present (E) loop |
10282 | if Scope (E) = Scope (Prot_Typ) | |
10283 | and then Comes_From_Source (E) | |
10284 | and then Ekind (Subp) = Ekind (E) | |
10285 | and then Present (First_Entity (E)) | |
10286 | and then Is_Controlling_Formal (First_Entity (E)) | |
10287 | and then Etype (First_Entity (E)) = Prot_Typ | |
10288 | and then Subtype_Conformant (Subp, E, | |
10289 | Skip_Controlling_Formals => True) | |
10290 | then | |
10291 | Set_Is_Controlling_Formal (First_Entity (Subp), ICF); | |
10292 | return E; | |
10293 | end if; | |
8c3dd7a8 | 10294 | |
42f11e4c AC |
10295 | E := Homonym (E); |
10296 | end loop; | |
5d37ba92 | 10297 | |
42f11e4c | 10298 | Set_Is_Controlling_Formal (First_Entity (Subp), ICF); |
bac5ba15 | 10299 | |
42f11e4c AC |
10300 | return Empty; |
10301 | end Matching_Original_Protected_Subprogram; | |
5d37ba92 | 10302 | |
42f11e4c | 10303 | -- Start of processing for Has_Matching_Entry_Or_Subprogram |
5d37ba92 | 10304 | |
42f11e4c AC |
10305 | begin |
10306 | -- Case 1: E is a subprogram whose first formal is a concurrent type | |
10307 | -- defined in the scope of E that has an entry or subprogram whose | |
10308 | -- profile matches E. | |
10309 | ||
10310 | if Comes_From_Source (E) | |
10311 | and then Is_Subprogram (E) | |
10312 | and then Present (First_Entity (E)) | |
10313 | and then Is_Concurrent_Record_Type (Etype (First_Entity (E))) | |
10314 | then | |
10315 | if Scope (E) = | |
bac5ba15 AC |
10316 | Scope (Corresponding_Concurrent_Type |
10317 | (Etype (First_Entity (E)))) | |
42f11e4c AC |
10318 | and then |
10319 | Present | |
10320 | (Matching_Entry_Or_Subprogram | |
10321 | (Corresponding_Concurrent_Type (Etype (First_Entity (E))), | |
10322 | Subp => E)) | |
10323 | then | |
10324 | Report_Conflict (E, | |
10325 | Matching_Entry_Or_Subprogram | |
10326 | (Corresponding_Concurrent_Type (Etype (First_Entity (E))), | |
10327 | Subp => E)); | |
10328 | return True; | |
5d37ba92 | 10329 | end if; |
8aa15e3b | 10330 | |
42f11e4c AC |
10331 | -- Case 2: E is an internally built dispatching subprogram of a |
10332 | -- protected type and there is a subprogram defined in the enclosing | |
10333 | -- scope of the protected type that has the original name of E and | |
10334 | -- its profile is conformant with the profile of E. We check the | |
10335 | -- name of the original protected subprogram associated with E since | |
10336 | -- the expander builds dispatching primitives of protected functions | |
ca90b962 | 10337 | -- and procedures with other names (see Exp_Ch9.Build_Selected_Name). |
42f11e4c AC |
10338 | |
10339 | elsif not Comes_From_Source (E) | |
10340 | and then Is_Subprogram (E) | |
10341 | and then Present (First_Entity (E)) | |
10342 | and then Is_Concurrent_Record_Type (Etype (First_Entity (E))) | |
10343 | and then Present (Original_Protected_Subprogram (E)) | |
10344 | and then | |
10345 | Present | |
10346 | (Matching_Original_Protected_Subprogram | |
bac5ba15 AC |
10347 | (Corresponding_Concurrent_Type (Etype (First_Entity (E))), |
10348 | Subp => E)) | |
42f11e4c AC |
10349 | then |
10350 | Report_Conflict (E, | |
10351 | Matching_Original_Protected_Subprogram | |
10352 | (Corresponding_Concurrent_Type (Etype (First_Entity (E))), | |
10353 | Subp => E)); | |
10354 | return True; | |
8aa15e3b | 10355 | |
ca90b962 | 10356 | -- Case 3: E is an entry of a synchronized type and a matching |
42f11e4c | 10357 | -- procedure has been previously defined in the enclosing scope |
ca90b962 | 10358 | -- of the synchronized type. |
8aa15e3b | 10359 | |
42f11e4c AC |
10360 | elsif Comes_From_Source (E) |
10361 | and then Ekind (E) = E_Entry | |
10362 | and then | |
10363 | Present (Matching_Dispatching_Subprogram (Current_Scope, E)) | |
10364 | then | |
10365 | Report_Conflict (E, | |
10366 | Matching_Dispatching_Subprogram (Current_Scope, E)); | |
10367 | return True; | |
5d37ba92 ES |
10368 | end if; |
10369 | ||
42f11e4c AC |
10370 | return False; |
10371 | end Has_Matching_Entry_Or_Subprogram; | |
5d37ba92 ES |
10372 | |
10373 | ---------------------------- | |
10374 | -- Is_Private_Declaration -- | |
10375 | ---------------------------- | |
10376 | ||
10377 | function Is_Private_Declaration (E : Entity_Id) return Boolean is | |
5d37ba92 | 10378 | Decl : constant Node_Id := Unit_Declaration_Node (E); |
bac5ba15 | 10379 | Priv_Decls : List_Id; |
5d37ba92 ES |
10380 | |
10381 | begin | |
10382 | if Is_Package_Or_Generic_Package (Current_Scope) | |
10383 | and then In_Private_Part (Current_Scope) | |
10384 | then | |
10385 | Priv_Decls := | |
d12b19fa | 10386 | Private_Declarations (Package_Specification (Current_Scope)); |
5d37ba92 ES |
10387 | |
10388 | return In_Package_Body (Current_Scope) | |
10389 | or else | |
10390 | (Is_List_Member (Decl) | |
a4901c08 | 10391 | and then List_Containing (Decl) = Priv_Decls) |
5d37ba92 | 10392 | or else (Nkind (Parent (Decl)) = N_Package_Specification |
a4901c08 AC |
10393 | and then not |
10394 | Is_Compilation_Unit | |
10395 | (Defining_Entity (Parent (Decl))) | |
10396 | and then List_Containing (Parent (Parent (Decl))) = | |
10397 | Priv_Decls); | |
5d37ba92 ES |
10398 | else |
10399 | return False; | |
10400 | end if; | |
10401 | end Is_Private_Declaration; | |
996ae0b0 | 10402 | |
2ddc2000 AC |
10403 | -------------------------- |
10404 | -- Is_Overriding_Alias -- | |
10405 | -------------------------- | |
10406 | ||
10407 | function Is_Overriding_Alias | |
10408 | (Old_E : Entity_Id; | |
10409 | New_E : Entity_Id) return Boolean | |
10410 | is | |
10411 | AO : constant Entity_Id := Alias (Old_E); | |
10412 | AN : constant Entity_Id := Alias (New_E); | |
bac5ba15 | 10413 | |
2ddc2000 AC |
10414 | begin |
10415 | return Scope (AO) /= Scope (AN) | |
10416 | or else No (DTC_Entity (AO)) | |
10417 | or else No (DTC_Entity (AN)) | |
10418 | or else DT_Position (AO) = DT_Position (AN); | |
10419 | end Is_Overriding_Alias; | |
10420 | ||
42f11e4c AC |
10421 | --------------------- |
10422 | -- Report_Conflict -- | |
10423 | --------------------- | |
10424 | ||
10425 | procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is | |
10426 | begin | |
10427 | Error_Msg_Sloc := Sloc (E); | |
10428 | ||
10429 | -- Generate message, with useful additional warning if in generic | |
10430 | ||
10431 | if Is_Generic_Unit (E) then | |
10432 | Error_Msg_N ("previous generic unit cannot be overloaded", S); | |
10433 | Error_Msg_N ("\& conflicts with declaration#", S); | |
10434 | else | |
10435 | Error_Msg_N ("& conflicts with declaration#", S); | |
10436 | end if; | |
10437 | end Report_Conflict; | |
10438 | ||
996ae0b0 RK |
10439 | -- Start of processing for New_Overloaded_Entity |
10440 | ||
10441 | begin | |
fbf5a39b AC |
10442 | -- We need to look for an entity that S may override. This must be a |
10443 | -- homonym in the current scope, so we look for the first homonym of | |
10444 | -- S in the current scope as the starting point for the search. | |
10445 | ||
10446 | E := Current_Entity_In_Scope (S); | |
10447 | ||
947430d5 AC |
10448 | -- Ada 2005 (AI-251): Derivation of abstract interface primitives. |
10449 | -- They are directly added to the list of primitive operations of | |
10450 | -- Derived_Type, unless this is a rederivation in the private part | |
10451 | -- of an operation that was already derived in the visible part of | |
10452 | -- the current package. | |
10453 | ||
0791fbe9 | 10454 | if Ada_Version >= Ada_2005 |
947430d5 AC |
10455 | and then Present (Derived_Type) |
10456 | and then Present (Alias (S)) | |
10457 | and then Is_Dispatching_Operation (Alias (S)) | |
10458 | and then Present (Find_Dispatching_Type (Alias (S))) | |
10459 | and then Is_Interface (Find_Dispatching_Type (Alias (S))) | |
10460 | then | |
10461 | -- For private types, when the full-view is processed we propagate to | |
10462 | -- the full view the non-overridden entities whose attribute "alias" | |
10463 | -- references an interface primitive. These entities were added by | |
10464 | -- Derive_Subprograms to ensure that interface primitives are | |
10465 | -- covered. | |
10466 | ||
10467 | -- Inside_Freeze_Actions is non zero when S corresponds with an | |
10468 | -- internal entity that links an interface primitive with its | |
10469 | -- covering primitive through attribute Interface_Alias (see | |
4adf3c50 | 10470 | -- Add_Internal_Interface_Entities). |
947430d5 AC |
10471 | |
10472 | if Inside_Freezing_Actions = 0 | |
10473 | and then Is_Package_Or_Generic_Package (Current_Scope) | |
10474 | and then In_Private_Part (Current_Scope) | |
10475 | and then Nkind (Parent (E)) = N_Private_Extension_Declaration | |
10476 | and then Nkind (Parent (S)) = N_Full_Type_Declaration | |
10477 | and then Full_View (Defining_Identifier (Parent (E))) | |
10478 | = Defining_Identifier (Parent (S)) | |
10479 | and then Alias (E) = Alias (S) | |
10480 | then | |
10481 | Check_Operation_From_Private_View (S, E); | |
10482 | Set_Is_Dispatching_Operation (S); | |
10483 | ||
10484 | -- Common case | |
10485 | ||
10486 | else | |
10487 | Enter_Overloaded_Entity (S); | |
10488 | Check_Dispatching_Operation (S, Empty); | |
10489 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); | |
10490 | end if; | |
10491 | ||
10492 | return; | |
10493 | end if; | |
10494 | ||
ca90b962 GD |
10495 | -- For synchronized types check conflicts of this entity with previously |
10496 | -- defined entities. | |
42f11e4c AC |
10497 | |
10498 | if Ada_Version >= Ada_2005 | |
10499 | and then Has_Matching_Entry_Or_Subprogram (S) | |
10500 | then | |
10501 | return; | |
10502 | end if; | |
10503 | ||
fbf5a39b AC |
10504 | -- If there is no homonym then this is definitely not overriding |
10505 | ||
996ae0b0 RK |
10506 | if No (E) then |
10507 | Enter_Overloaded_Entity (S); | |
10508 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 10509 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
996ae0b0 | 10510 | |
2995860f AC |
10511 | -- If subprogram has an explicit declaration, check whether it has an |
10512 | -- overriding indicator. | |
758c442c | 10513 | |
ec4867fa | 10514 | if Comes_From_Source (S) then |
8aa15e3b | 10515 | Check_Synchronized_Overriding (S, Overridden_Subp); |
ea034236 AC |
10516 | |
10517 | -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then | |
10518 | -- it may have overridden some hidden inherited primitive. Update | |
308e6f3a | 10519 | -- Overridden_Subp to avoid spurious errors when checking the |
ea034236 AC |
10520 | -- overriding indicator. |
10521 | ||
10522 | if Ada_Version >= Ada_2012 | |
10523 | and then No (Overridden_Subp) | |
10524 | and then Is_Dispatching_Operation (S) | |
038140ed | 10525 | and then Present (Overridden_Operation (S)) |
ea034236 AC |
10526 | then |
10527 | Overridden_Subp := Overridden_Operation (S); | |
10528 | end if; | |
10529 | ||
5d37ba92 ES |
10530 | Check_Overriding_Indicator |
10531 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
241ebe89 HK |
10532 | |
10533 | -- The Ghost policy in effect at the point of declaration of a | |
10534 | -- parent subprogram and an overriding subprogram must match | |
10535 | -- (SPARK RM 6.9(17)). | |
10536 | ||
10537 | Check_Ghost_Overriding (S, Overridden_Subp); | |
758c442c GD |
10538 | end if; |
10539 | ||
fbf5a39b AC |
10540 | -- If there is a homonym that is not overloadable, then we have an |
10541 | -- error, except for the special cases checked explicitly below. | |
10542 | ||
996ae0b0 RK |
10543 | elsif not Is_Overloadable (E) then |
10544 | ||
10545 | -- Check for spurious conflict produced by a subprogram that has the | |
10546 | -- same name as that of the enclosing generic package. The conflict | |
10547 | -- occurs within an instance, between the subprogram and the renaming | |
10548 | -- declaration for the package. After the subprogram, the package | |
10549 | -- renaming declaration becomes hidden. | |
10550 | ||
10551 | if Ekind (E) = E_Package | |
10552 | and then Present (Renamed_Object (E)) | |
10553 | and then Renamed_Object (E) = Current_Scope | |
10554 | and then Nkind (Parent (Renamed_Object (E))) = | |
10555 | N_Package_Specification | |
10556 | and then Present (Generic_Parent (Parent (Renamed_Object (E)))) | |
10557 | then | |
10558 | Set_Is_Hidden (E); | |
10559 | Set_Is_Immediately_Visible (E, False); | |
10560 | Enter_Overloaded_Entity (S); | |
10561 | Set_Homonym (S, Homonym (E)); | |
10562 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 10563 | Check_Overriding_Indicator (S, Empty, Is_Primitive => False); |
996ae0b0 RK |
10564 | |
10565 | -- If the subprogram is implicit it is hidden by the previous | |
82c80734 RD |
10566 | -- declaration. However if it is dispatching, it must appear in the |
10567 | -- dispatch table anyway, because it can be dispatched to even if it | |
10568 | -- cannot be called directly. | |
996ae0b0 | 10569 | |
4adf3c50 | 10570 | elsif Present (Alias (S)) and then not Comes_From_Source (S) then |
996ae0b0 RK |
10571 | Set_Scope (S, Current_Scope); |
10572 | ||
10573 | if Is_Dispatching_Operation (Alias (S)) then | |
10574 | Check_Dispatching_Operation (S, Empty); | |
10575 | end if; | |
10576 | ||
10577 | return; | |
10578 | ||
10579 | else | |
42f11e4c | 10580 | Report_Conflict (S, E); |
996ae0b0 RK |
10581 | return; |
10582 | end if; | |
10583 | ||
fbf5a39b AC |
10584 | -- E exists and is overloadable |
10585 | ||
996ae0b0 | 10586 | else |
8aa15e3b | 10587 | Check_Synchronized_Overriding (S, Overridden_Subp); |
758c442c | 10588 | |
82c80734 RD |
10589 | -- Loop through E and its homonyms to determine if any of them is |
10590 | -- the candidate for overriding by S. | |
996ae0b0 RK |
10591 | |
10592 | while Present (E) loop | |
fbf5a39b AC |
10593 | |
10594 | -- Definitely not interesting if not in the current scope | |
10595 | ||
996ae0b0 RK |
10596 | if Scope (E) /= Current_Scope then |
10597 | null; | |
10598 | ||
aca90db9 AC |
10599 | -- A function can overload the name of an abstract state. The |
10600 | -- state can be viewed as a function with a profile that cannot | |
10601 | -- be matched by anything. | |
10602 | ||
10603 | elsif Ekind (S) = E_Function | |
10604 | and then Ekind (E) = E_Abstract_State | |
10605 | then | |
10606 | Enter_Overloaded_Entity (S); | |
10607 | return; | |
10608 | ||
2995860f AC |
10609 | -- Ada 2012 (AI05-0165): For internally generated bodies of null |
10610 | -- procedures locate the internally generated spec. We enforce | |
10611 | -- mode conformance since a tagged type may inherit from | |
10612 | -- interfaces several null primitives which differ only in | |
10613 | -- the mode of the formals. | |
25ebc085 AC |
10614 | |
10615 | elsif not Comes_From_Source (S) | |
10616 | and then Is_Null_Procedure (S) | |
10617 | and then not Mode_Conformant (E, S) | |
10618 | then | |
10619 | null; | |
10620 | ||
fbf5a39b AC |
10621 | -- Check if we have type conformance |
10622 | ||
ec4867fa | 10623 | elsif Type_Conformant (E, S) then |
c8ef728f | 10624 | |
82c80734 RD |
10625 | -- If the old and new entities have the same profile and one |
10626 | -- is not the body of the other, then this is an error, unless | |
10627 | -- one of them is implicitly declared. | |
996ae0b0 RK |
10628 | |
10629 | -- There are some cases when both can be implicit, for example | |
10630 | -- when both a literal and a function that overrides it are | |
f3d57416 | 10631 | -- inherited in a derivation, or when an inherited operation |
ec4867fa | 10632 | -- of a tagged full type overrides the inherited operation of |
f3d57416 | 10633 | -- a private extension. Ada 83 had a special rule for the |
885c4871 | 10634 | -- literal case. In Ada 95, the later implicit operation hides |
82c80734 RD |
10635 | -- the former, and the literal is always the former. In the |
10636 | -- odd case where both are derived operations declared at the | |
10637 | -- same point, both operations should be declared, and in that | |
10638 | -- case we bypass the following test and proceed to the next | |
df46b832 AC |
10639 | -- part. This can only occur for certain obscure cases in |
10640 | -- instances, when an operation on a type derived from a formal | |
10641 | -- private type does not override a homograph inherited from | |
10642 | -- the actual. In subsequent derivations of such a type, the | |
10643 | -- DT positions of these operations remain distinct, if they | |
10644 | -- have been set. | |
996ae0b0 RK |
10645 | |
10646 | if Present (Alias (S)) | |
10647 | and then (No (Alias (E)) | |
10648 | or else Comes_From_Source (E) | |
2ddc2000 | 10649 | or else Is_Abstract_Subprogram (S) |
df46b832 AC |
10650 | or else |
10651 | (Is_Dispatching_Operation (E) | |
84c0a895 | 10652 | and then Is_Overriding_Alias (E, S))) |
df46b832 | 10653 | and then Ekind (E) /= E_Enumeration_Literal |
996ae0b0 | 10654 | then |
82c80734 RD |
10655 | -- When an derived operation is overloaded it may be due to |
10656 | -- the fact that the full view of a private extension | |
996ae0b0 RK |
10657 | -- re-inherits. It has to be dealt with. |
10658 | ||
e660dbf7 | 10659 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
10660 | and then In_Private_Part (Current_Scope) |
10661 | then | |
10662 | Check_Operation_From_Private_View (S, E); | |
10663 | end if; | |
10664 | ||
038140ed AC |
10665 | -- In any case the implicit operation remains hidden by the |
10666 | -- existing declaration, which is overriding. Indicate that | |
10667 | -- E overrides the operation from which S is inherited. | |
996ae0b0 | 10668 | |
038140ed | 10669 | if Present (Alias (S)) then |
039538bc AC |
10670 | Set_Overridden_Operation (E, Alias (S)); |
10671 | Inherit_Subprogram_Contract (E, Alias (S)); | |
10672 | ||
038140ed | 10673 | else |
039538bc AC |
10674 | Set_Overridden_Operation (E, S); |
10675 | Inherit_Subprogram_Contract (E, S); | |
038140ed | 10676 | end if; |
758c442c GD |
10677 | |
10678 | if Comes_From_Source (E) then | |
5d37ba92 | 10679 | Check_Overriding_Indicator (E, S, Is_Primitive => False); |
241ebe89 HK |
10680 | |
10681 | -- The Ghost policy in effect at the point of declaration | |
10682 | -- of a parent subprogram and an overriding subprogram | |
10683 | -- must match (SPARK RM 6.9(17)). | |
10684 | ||
10685 | Check_Ghost_Overriding (E, S); | |
758c442c GD |
10686 | end if; |
10687 | ||
996ae0b0 RK |
10688 | return; |
10689 | ||
26a43556 AC |
10690 | -- Within an instance, the renaming declarations for actual |
10691 | -- subprograms may become ambiguous, but they do not hide each | |
10692 | -- other. | |
996ae0b0 RK |
10693 | |
10694 | elsif Ekind (E) /= E_Entry | |
10695 | and then not Comes_From_Source (E) | |
10696 | and then not Is_Generic_Instance (E) | |
10697 | and then (Present (Alias (E)) | |
10698 | or else Is_Intrinsic_Subprogram (E)) | |
10699 | and then (not In_Instance | |
10700 | or else No (Parent (E)) | |
10701 | or else Nkind (Unit_Declaration_Node (E)) /= | |
8dbd1460 | 10702 | N_Subprogram_Renaming_Declaration) |
996ae0b0 | 10703 | then |
26a43556 AC |
10704 | -- A subprogram child unit is not allowed to override an |
10705 | -- inherited subprogram (10.1.1(20)). | |
996ae0b0 RK |
10706 | |
10707 | if Is_Child_Unit (S) then | |
10708 | Error_Msg_N | |
10709 | ("child unit overrides inherited subprogram in parent", | |
10710 | S); | |
10711 | return; | |
10712 | end if; | |
10713 | ||
10714 | if Is_Non_Overriding_Operation (E, S) then | |
10715 | Enter_Overloaded_Entity (S); | |
8dbd1460 | 10716 | |
c8ef728f | 10717 | if No (Derived_Type) |
996ae0b0 RK |
10718 | or else Is_Tagged_Type (Derived_Type) |
10719 | then | |
10720 | Check_Dispatching_Operation (S, Empty); | |
10721 | end if; | |
10722 | ||
10723 | return; | |
10724 | end if; | |
10725 | ||
10726 | -- E is a derived operation or an internal operator which | |
10727 | -- is being overridden. Remove E from further visibility. | |
10728 | -- Furthermore, if E is a dispatching operation, it must be | |
10729 | -- replaced in the list of primitive operations of its type | |
10730 | -- (see Override_Dispatching_Operation). | |
10731 | ||
ec4867fa | 10732 | Overridden_Subp := E; |
758c442c | 10733 | |
996ae0b0 RK |
10734 | declare |
10735 | Prev : Entity_Id; | |
10736 | ||
10737 | begin | |
10738 | Prev := First_Entity (Current_Scope); | |
8fde064e | 10739 | while Present (Prev) and then Next_Entity (Prev) /= E loop |
996ae0b0 RK |
10740 | Next_Entity (Prev); |
10741 | end loop; | |
10742 | ||
10743 | -- It is possible for E to be in the current scope and | |
10744 | -- yet not in the entity chain. This can only occur in a | |
10745 | -- generic context where E is an implicit concatenation | |
10746 | -- in the formal part, because in a generic body the | |
10747 | -- entity chain starts with the formals. | |
10748 | ||
948ed277 AC |
10749 | -- In GNATprove mode, a wrapper for an operation with |
10750 | -- axiomatization may be a homonym of another declaration | |
10751 | -- for an actual subprogram (needs refinement ???). | |
10752 | ||
10753 | if No (Prev) then | |
10754 | if In_Instance | |
10755 | and then GNATprove_Mode | |
10756 | and then | |
10757 | Nkind (Original_Node (Unit_Declaration_Node (S))) = | |
10758 | N_Subprogram_Renaming_Declaration | |
10759 | then | |
10760 | return; | |
10761 | else | |
10762 | pragma Assert (Chars (E) = Name_Op_Concat); | |
10763 | null; | |
10764 | end if; | |
10765 | end if; | |
996ae0b0 RK |
10766 | |
10767 | -- E must be removed both from the entity_list of the | |
948ed277 | 10768 | -- current scope, and from the visibility chain. |
996ae0b0 RK |
10769 | |
10770 | if Debug_Flag_E then | |
10771 | Write_Str ("Override implicit operation "); | |
10772 | Write_Int (Int (E)); | |
10773 | Write_Eol; | |
10774 | end if; | |
10775 | ||
10776 | -- If E is a predefined concatenation, it stands for four | |
10777 | -- different operations. As a result, a single explicit | |
10778 | -- declaration does not hide it. In a possible ambiguous | |
10779 | -- situation, Disambiguate chooses the user-defined op, | |
10780 | -- so it is correct to retain the previous internal one. | |
10781 | ||
10782 | if Chars (E) /= Name_Op_Concat | |
10783 | or else Ekind (E) /= E_Operator | |
10784 | then | |
10785 | -- For nondispatching derived operations that are | |
10786 | -- overridden by a subprogram declared in the private | |
8dbd1460 AC |
10787 | -- part of a package, we retain the derived subprogram |
10788 | -- but mark it as not immediately visible. If the | |
10789 | -- derived operation was declared in the visible part | |
10790 | -- then this ensures that it will still be visible | |
10791 | -- outside the package with the proper signature | |
10792 | -- (calls from outside must also be directed to this | |
10793 | -- version rather than the overriding one, unlike the | |
10794 | -- dispatching case). Calls from inside the package | |
10795 | -- will still resolve to the overriding subprogram | |
10796 | -- since the derived one is marked as not visible | |
10797 | -- within the package. | |
996ae0b0 RK |
10798 | |
10799 | -- If the private operation is dispatching, we achieve | |
10800 | -- the overriding by keeping the implicit operation | |
9865d858 | 10801 | -- but setting its alias to be the overriding one. In |
996ae0b0 RK |
10802 | -- this fashion the proper body is executed in all |
10803 | -- cases, but the original signature is used outside | |
10804 | -- of the package. | |
10805 | ||
10806 | -- If the overriding is not in the private part, we | |
10807 | -- remove the implicit operation altogether. | |
10808 | ||
10809 | if Is_Private_Declaration (S) then | |
996ae0b0 RK |
10810 | if not Is_Dispatching_Operation (E) then |
10811 | Set_Is_Immediately_Visible (E, False); | |
10812 | else | |
e895b435 | 10813 | -- Work done in Override_Dispatching_Operation, |
a46cde68 | 10814 | -- so nothing else needs to be done here. |
996ae0b0 RK |
10815 | |
10816 | null; | |
10817 | end if; | |
996ae0b0 | 10818 | |
fbf5a39b AC |
10819 | else |
10820 | -- Find predecessor of E in Homonym chain | |
996ae0b0 RK |
10821 | |
10822 | if E = Current_Entity (E) then | |
10823 | Prev_Vis := Empty; | |
10824 | else | |
10825 | Prev_Vis := Current_Entity (E); | |
10826 | while Homonym (Prev_Vis) /= E loop | |
10827 | Prev_Vis := Homonym (Prev_Vis); | |
10828 | end loop; | |
10829 | end if; | |
10830 | ||
10831 | if Prev_Vis /= Empty then | |
10832 | ||
10833 | -- Skip E in the visibility chain | |
10834 | ||
10835 | Set_Homonym (Prev_Vis, Homonym (E)); | |
10836 | ||
10837 | else | |
10838 | Set_Name_Entity_Id (Chars (E), Homonym (E)); | |
10839 | end if; | |
10840 | ||
10841 | Set_Next_Entity (Prev, Next_Entity (E)); | |
10842 | ||
10843 | if No (Next_Entity (Prev)) then | |
10844 | Set_Last_Entity (Current_Scope, Prev); | |
10845 | end if; | |
996ae0b0 RK |
10846 | end if; |
10847 | end if; | |
10848 | ||
10849 | Enter_Overloaded_Entity (S); | |
1c1289e7 AC |
10850 | |
10851 | -- For entities generated by Derive_Subprograms the | |
10852 | -- overridden operation is the inherited primitive | |
10853 | -- (which is available through the attribute alias). | |
10854 | ||
10855 | if not (Comes_From_Source (E)) | |
10856 | and then Is_Dispatching_Operation (E) | |
f9673bb0 AC |
10857 | and then Find_Dispatching_Type (E) = |
10858 | Find_Dispatching_Type (S) | |
1c1289e7 AC |
10859 | and then Present (Alias (E)) |
10860 | and then Comes_From_Source (Alias (E)) | |
10861 | then | |
039538bc AC |
10862 | Set_Overridden_Operation (S, Alias (E)); |
10863 | Inherit_Subprogram_Contract (S, Alias (E)); | |
2fe829ae | 10864 | |
6320f5e1 AC |
10865 | -- Normal case of setting entity as overridden |
10866 | ||
10867 | -- Note: Static_Initialization and Overridden_Operation | |
10868 | -- attributes use the same field in subprogram entities. | |
10869 | -- Static_Initialization is only defined for internal | |
10870 | -- initialization procedures, where Overridden_Operation | |
10871 | -- is irrelevant. Therefore the setting of this attribute | |
10872 | -- must check whether the target is an init_proc. | |
10873 | ||
2fe829ae | 10874 | elsif not Is_Init_Proc (S) then |
039538bc AC |
10875 | Set_Overridden_Operation (S, E); |
10876 | Inherit_Subprogram_Contract (S, E); | |
1c1289e7 AC |
10877 | end if; |
10878 | ||
5d37ba92 | 10879 | Check_Overriding_Indicator (S, E, Is_Primitive => True); |
996ae0b0 | 10880 | |
241ebe89 HK |
10881 | -- The Ghost policy in effect at the point of declaration |
10882 | -- of a parent subprogram and an overriding subprogram | |
10883 | -- must match (SPARK RM 6.9(17)). | |
10884 | ||
10885 | Check_Ghost_Overriding (S, E); | |
10886 | ||
fc53fe76 | 10887 | -- If S is a user-defined subprogram or a null procedure |
38ef8ebe AC |
10888 | -- expanded to override an inherited null procedure, or a |
10889 | -- predefined dispatching primitive then indicate that E | |
038140ed | 10890 | -- overrides the operation from which S is inherited. |
fc53fe76 AC |
10891 | |
10892 | if Comes_From_Source (S) | |
10893 | or else | |
10894 | (Present (Parent (S)) | |
10895 | and then | |
10896 | Nkind (Parent (S)) = N_Procedure_Specification | |
10897 | and then | |
10898 | Null_Present (Parent (S))) | |
38ef8ebe AC |
10899 | or else |
10900 | (Present (Alias (E)) | |
f16e8df9 RD |
10901 | and then |
10902 | Is_Predefined_Dispatching_Operation (Alias (E))) | |
fc53fe76 | 10903 | then |
c8ef728f | 10904 | if Present (Alias (E)) then |
039538bc AC |
10905 | Set_Overridden_Operation (S, Alias (E)); |
10906 | Inherit_Subprogram_Contract (S, Alias (E)); | |
41251c60 JM |
10907 | end if; |
10908 | end if; | |
10909 | ||
996ae0b0 | 10910 | if Is_Dispatching_Operation (E) then |
fbf5a39b | 10911 | |
82c80734 | 10912 | -- An overriding dispatching subprogram inherits the |
f9673bb0 | 10913 | -- convention of the overridden subprogram (AI-117). |
996ae0b0 RK |
10914 | |
10915 | Set_Convention (S, Convention (E)); | |
41251c60 JM |
10916 | Check_Dispatching_Operation (S, E); |
10917 | ||
996ae0b0 RK |
10918 | else |
10919 | Check_Dispatching_Operation (S, Empty); | |
10920 | end if; | |
10921 | ||
5d37ba92 ES |
10922 | Check_For_Primitive_Subprogram |
10923 | (Is_Primitive_Subp, Is_Overriding => True); | |
996ae0b0 RK |
10924 | goto Check_Inequality; |
10925 | end; | |
10926 | ||
10927 | -- Apparent redeclarations in instances can occur when two | |
10928 | -- formal types get the same actual type. The subprograms in | |
10929 | -- in the instance are legal, even if not callable from the | |
10930 | -- outside. Calls from within are disambiguated elsewhere. | |
10931 | -- For dispatching operations in the visible part, the usual | |
10932 | -- rules apply, and operations with the same profile are not | |
10933 | -- legal (B830001). | |
10934 | ||
10935 | elsif (In_Instance_Visible_Part | |
10936 | and then not Is_Dispatching_Operation (E)) | |
10937 | or else In_Instance_Not_Visible | |
10938 | then | |
10939 | null; | |
10940 | ||
10941 | -- Here we have a real error (identical profile) | |
10942 | ||
10943 | else | |
10944 | Error_Msg_Sloc := Sloc (E); | |
10945 | ||
10946 | -- Avoid cascaded errors if the entity appears in | |
10947 | -- subsequent calls. | |
10948 | ||
10949 | Set_Scope (S, Current_Scope); | |
10950 | ||
5d37ba92 ES |
10951 | -- Generate error, with extra useful warning for the case |
10952 | -- of a generic instance with no completion. | |
996ae0b0 RK |
10953 | |
10954 | if Is_Generic_Instance (S) | |
10955 | and then not Has_Completion (E) | |
10956 | then | |
10957 | Error_Msg_N | |
5d37ba92 ES |
10958 | ("instantiation cannot provide body for&", S); |
10959 | Error_Msg_N ("\& conflicts with declaration#", S); | |
10960 | else | |
10961 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
10962 | end if; |
10963 | ||
10964 | return; | |
10965 | end if; | |
10966 | ||
10967 | else | |
c8ef728f ES |
10968 | -- If one subprogram has an access parameter and the other |
10969 | -- a parameter of an access type, calls to either might be | |
10970 | -- ambiguous. Verify that parameters match except for the | |
10971 | -- access parameter. | |
10972 | ||
10973 | if May_Hide_Profile then | |
10974 | declare | |
ec4867fa ES |
10975 | F1 : Entity_Id; |
10976 | F2 : Entity_Id; | |
8dbd1460 | 10977 | |
c8ef728f ES |
10978 | begin |
10979 | F1 := First_Formal (S); | |
10980 | F2 := First_Formal (E); | |
10981 | while Present (F1) and then Present (F2) loop | |
10982 | if Is_Access_Type (Etype (F1)) then | |
10983 | if not Is_Access_Type (Etype (F2)) | |
10984 | or else not Conforming_Types | |
10985 | (Designated_Type (Etype (F1)), | |
10986 | Designated_Type (Etype (F2)), | |
10987 | Type_Conformant) | |
10988 | then | |
10989 | May_Hide_Profile := False; | |
10990 | end if; | |
10991 | ||
10992 | elsif | |
10993 | not Conforming_Types | |
10994 | (Etype (F1), Etype (F2), Type_Conformant) | |
10995 | then | |
10996 | May_Hide_Profile := False; | |
10997 | end if; | |
10998 | ||
10999 | Next_Formal (F1); | |
11000 | Next_Formal (F2); | |
11001 | end loop; | |
11002 | ||
11003 | if May_Hide_Profile | |
11004 | and then No (F1) | |
11005 | and then No (F2) | |
11006 | then | |
dbfeb4fa | 11007 | Error_Msg_NE ("calls to& may be ambiguous??", S, S); |
c8ef728f ES |
11008 | end if; |
11009 | end; | |
11010 | end if; | |
996ae0b0 RK |
11011 | end if; |
11012 | ||
996ae0b0 RK |
11013 | E := Homonym (E); |
11014 | end loop; | |
11015 | ||
11016 | -- On exit, we know that S is a new entity | |
11017 | ||
11018 | Enter_Overloaded_Entity (S); | |
5d37ba92 ES |
11019 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
11020 | Check_Overriding_Indicator | |
11021 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
996ae0b0 | 11022 | |
241ebe89 HK |
11023 | -- The Ghost policy in effect at the point of declaration of a parent |
11024 | -- subprogram and an overriding subprogram must match | |
11025 | -- (SPARK RM 6.9(17)). | |
11026 | ||
11027 | Check_Ghost_Overriding (S, Overridden_Subp); | |
11028 | ||
c4d67e2d | 11029 | -- Overloading is not allowed in SPARK, except for operators |
8ed68165 | 11030 | |
c4d67e2d AC |
11031 | if Nkind (S) /= N_Defining_Operator_Symbol then |
11032 | Error_Msg_Sloc := Sloc (Homonym (S)); | |
ce5ba43a | 11033 | Check_SPARK_05_Restriction |
c4d67e2d AC |
11034 | ("overloading not allowed with entity#", S); |
11035 | end if; | |
8ed68165 | 11036 | |
82c80734 RD |
11037 | -- If S is a derived operation for an untagged type then by |
11038 | -- definition it's not a dispatching operation (even if the parent | |
e917aec2 RD |
11039 | -- operation was dispatching), so Check_Dispatching_Operation is not |
11040 | -- called in that case. | |
996ae0b0 | 11041 | |
c8ef728f | 11042 | if No (Derived_Type) |
996ae0b0 RK |
11043 | or else Is_Tagged_Type (Derived_Type) |
11044 | then | |
11045 | Check_Dispatching_Operation (S, Empty); | |
11046 | end if; | |
11047 | end if; | |
11048 | ||
82c80734 RD |
11049 | -- If this is a user-defined equality operator that is not a derived |
11050 | -- subprogram, create the corresponding inequality. If the operation is | |
11051 | -- dispatching, the expansion is done elsewhere, and we do not create | |
11052 | -- an explicit inequality operation. | |
996ae0b0 RK |
11053 | |
11054 | <<Check_Inequality>> | |
11055 | if Chars (S) = Name_Op_Eq | |
11056 | and then Etype (S) = Standard_Boolean | |
11057 | and then Present (Parent (S)) | |
11058 | and then not Is_Dispatching_Operation (S) | |
11059 | then | |
11060 | Make_Inequality_Operator (S); | |
b2834fbd | 11061 | Check_Untagged_Equality (S); |
996ae0b0 | 11062 | end if; |
996ae0b0 RK |
11063 | end New_Overloaded_Entity; |
11064 | ||
11065 | --------------------- | |
11066 | -- Process_Formals -- | |
11067 | --------------------- | |
11068 | ||
11069 | procedure Process_Formals | |
07fc65c4 | 11070 | (T : List_Id; |
996ae0b0 RK |
11071 | Related_Nod : Node_Id) |
11072 | is | |
7b56a91b | 11073 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean; |
950d217a AC |
11074 | -- Determine whether an access type designates a type coming from a |
11075 | -- limited view. | |
11076 | ||
07fc65c4 | 11077 | function Is_Class_Wide_Default (D : Node_Id) return Boolean; |
82c80734 RD |
11078 | -- Check whether the default has a class-wide type. After analysis the |
11079 | -- default has the type of the formal, so we must also check explicitly | |
11080 | -- for an access attribute. | |
07fc65c4 | 11081 | |
7b56a91b AC |
11082 | ---------------------------------- |
11083 | -- Designates_From_Limited_With -- | |
11084 | ---------------------------------- | |
950d217a | 11085 | |
7b56a91b | 11086 | function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is |
950d217a AC |
11087 | Desig : Entity_Id := Typ; |
11088 | ||
11089 | begin | |
11090 | if Is_Access_Type (Desig) then | |
11091 | Desig := Directly_Designated_Type (Desig); | |
11092 | end if; | |
11093 | ||
11094 | if Is_Class_Wide_Type (Desig) then | |
11095 | Desig := Root_Type (Desig); | |
11096 | end if; | |
11097 | ||
11098 | return | |
7b56a91b AC |
11099 | Ekind (Desig) = E_Incomplete_Type |
11100 | and then From_Limited_With (Desig); | |
11101 | end Designates_From_Limited_With; | |
950d217a | 11102 | |
07fc65c4 GB |
11103 | --------------------------- |
11104 | -- Is_Class_Wide_Default -- | |
11105 | --------------------------- | |
11106 | ||
11107 | function Is_Class_Wide_Default (D : Node_Id) return Boolean is | |
11108 | begin | |
11109 | return Is_Class_Wide_Type (Designated_Type (Etype (D))) | |
1b1d88b1 | 11110 | or else (Nkind (D) = N_Attribute_Reference |
0f853035 YM |
11111 | and then Attribute_Name (D) = Name_Access |
11112 | and then Is_Class_Wide_Type (Etype (Prefix (D)))); | |
07fc65c4 GB |
11113 | end Is_Class_Wide_Default; |
11114 | ||
847d950d HK |
11115 | -- Local variables |
11116 | ||
11117 | Context : constant Node_Id := Parent (Parent (T)); | |
11118 | Default : Node_Id; | |
11119 | Formal : Entity_Id; | |
11120 | Formal_Type : Entity_Id; | |
11121 | Param_Spec : Node_Id; | |
11122 | Ptype : Entity_Id; | |
11123 | ||
11124 | Num_Out_Params : Nat := 0; | |
11125 | First_Out_Param : Entity_Id := Empty; | |
11126 | -- Used for setting Is_Only_Out_Parameter | |
11127 | ||
07fc65c4 GB |
11128 | -- Start of processing for Process_Formals |
11129 | ||
996ae0b0 RK |
11130 | begin |
11131 | -- In order to prevent premature use of the formals in the same formal | |
11132 | -- part, the Ekind is left undefined until all default expressions are | |
11133 | -- analyzed. The Ekind is established in a separate loop at the end. | |
11134 | ||
11135 | Param_Spec := First (T); | |
996ae0b0 | 11136 | while Present (Param_Spec) loop |
996ae0b0 | 11137 | Formal := Defining_Identifier (Param_Spec); |
5d37ba92 | 11138 | Set_Never_Set_In_Source (Formal, True); |
996ae0b0 RK |
11139 | Enter_Name (Formal); |
11140 | ||
11141 | -- Case of ordinary parameters | |
11142 | ||
11143 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
11144 | Find_Type (Parameter_Type (Param_Spec)); | |
11145 | Ptype := Parameter_Type (Param_Spec); | |
11146 | ||
11147 | if Ptype = Error then | |
11148 | goto Continue; | |
11149 | end if; | |
11150 | ||
11151 | Formal_Type := Entity (Ptype); | |
11152 | ||
ec4867fa ES |
11153 | if Is_Incomplete_Type (Formal_Type) |
11154 | or else | |
11155 | (Is_Class_Wide_Type (Formal_Type) | |
8fde064e | 11156 | and then Is_Incomplete_Type (Root_Type (Formal_Type))) |
996ae0b0 | 11157 | then |
93bcda23 AC |
11158 | -- Ada 2005 (AI-326): Tagged incomplete types allowed in |
11159 | -- primitive operations, as long as their completion is | |
11160 | -- in the same declarative part. If in the private part | |
11161 | -- this means that the type cannot be a Taft-amendment type. | |
cec29135 ES |
11162 | -- Check is done on package exit. For access to subprograms, |
11163 | -- the use is legal for Taft-amendment types. | |
fbf5a39b | 11164 | |
6eddd7b4 AC |
11165 | -- Ada 2012: tagged incomplete types are allowed as generic |
11166 | -- formal types. They do not introduce dependencies and the | |
11167 | -- corresponding generic subprogram does not have a delayed | |
5b6f12c7 AC |
11168 | -- freeze, because it does not need a freeze node. However, |
11169 | -- it is still the case that untagged incomplete types cannot | |
11170 | -- be Taft-amendment types and must be completed in private | |
11171 | -- part, so the subprogram must appear in the list of private | |
1e55d29a | 11172 | -- dependents of the type. |
5b6f12c7 AC |
11173 | |
11174 | if Is_Tagged_Type (Formal_Type) | |
b973629e | 11175 | or else (Ada_Version >= Ada_2012 |
1ebc2612 AC |
11176 | and then not From_Limited_With (Formal_Type) |
11177 | and then not Is_Generic_Type (Formal_Type)) | |
5b6f12c7 | 11178 | then |
93bcda23 | 11179 | if Ekind (Scope (Current_Scope)) = E_Package |
6eddd7b4 | 11180 | and then not Is_Generic_Type (Formal_Type) |
1e55d29a | 11181 | and then not Is_Class_Wide_Type (Formal_Type) |
93bcda23 | 11182 | then |
cec29135 | 11183 | if not Nkind_In |
f26a3587 AC |
11184 | (Parent (T), N_Access_Function_Definition, |
11185 | N_Access_Procedure_Definition) | |
cec29135 | 11186 | then |
1e55d29a EB |
11187 | Append_Elmt (Current_Scope, |
11188 | Private_Dependents (Base_Type (Formal_Type))); | |
4637729f AC |
11189 | |
11190 | -- Freezing is delayed to ensure that Register_Prim | |
11191 | -- will get called for this operation, which is needed | |
11192 | -- in cases where static dispatch tables aren't built. | |
11193 | -- (Note that the same is done for controlling access | |
11194 | -- parameter cases in function Access_Definition.) | |
11195 | ||
13fa2acb AC |
11196 | if not Is_Thunk (Current_Scope) then |
11197 | Set_Has_Delayed_Freeze (Current_Scope); | |
11198 | end if; | |
cec29135 | 11199 | end if; |
93bcda23 | 11200 | end if; |
fbf5a39b | 11201 | |
800621e0 RD |
11202 | elsif not Nkind_In (Parent (T), N_Access_Function_Definition, |
11203 | N_Access_Procedure_Definition) | |
996ae0b0 | 11204 | then |
dd386db0 AC |
11205 | -- AI05-0151: Tagged incomplete types are allowed in all |
11206 | -- formal parts. Untagged incomplete types are not allowed | |
b973629e AC |
11207 | -- in bodies. Limited views of either kind are not allowed |
11208 | -- if there is no place at which the non-limited view can | |
11209 | -- become available. | |
a18e3d62 | 11210 | |
1ebc2612 AC |
11211 | -- Incomplete formal untagged types are not allowed in |
11212 | -- subprogram bodies (but are legal in their declarations). | |
c8d3b4ff AC |
11213 | -- This excludes bodies created for null procedures, which |
11214 | -- are basic declarations. | |
1ebc2612 AC |
11215 | |
11216 | if Is_Generic_Type (Formal_Type) | |
11217 | and then not Is_Tagged_Type (Formal_Type) | |
11218 | and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body | |
11219 | then | |
11220 | Error_Msg_N | |
11221 | ("invalid use of formal incomplete type", Param_Spec); | |
dd386db0 | 11222 | |
1ebc2612 | 11223 | elsif Ada_Version >= Ada_2012 then |
b973629e AC |
11224 | if Is_Tagged_Type (Formal_Type) |
11225 | and then (not From_Limited_With (Formal_Type) | |
11226 | or else not In_Package_Body) | |
11227 | then | |
dd386db0 AC |
11228 | null; |
11229 | ||
c8d3b4ff AC |
11230 | elsif Nkind_In (Context, N_Accept_Statement, |
11231 | N_Accept_Alternative, | |
11232 | N_Entry_Body) | |
11233 | or else (Nkind (Context) = N_Subprogram_Body | |
11234 | and then Comes_From_Source (Context)) | |
dd386db0 AC |
11235 | then |
11236 | Error_Msg_NE | |
c8d3b4ff | 11237 | ("invalid use of untagged incomplete type &", |
0f1a6a0b | 11238 | Ptype, Formal_Type); |
dd386db0 AC |
11239 | end if; |
11240 | ||
11241 | else | |
11242 | Error_Msg_NE | |
11243 | ("invalid use of incomplete type&", | |
0f1a6a0b | 11244 | Param_Spec, Formal_Type); |
dd386db0 AC |
11245 | |
11246 | -- Further checks on the legality of incomplete types | |
11247 | -- in formal parts are delayed until the freeze point | |
11248 | -- of the enclosing subprogram or access to subprogram. | |
11249 | end if; | |
996ae0b0 RK |
11250 | end if; |
11251 | ||
11252 | elsif Ekind (Formal_Type) = E_Void then | |
0f1a6a0b AC |
11253 | Error_Msg_NE |
11254 | ("premature use of&", | |
11255 | Parameter_Type (Param_Spec), Formal_Type); | |
996ae0b0 RK |
11256 | end if; |
11257 | ||
fecbd779 AC |
11258 | -- Ada 2012 (AI-142): Handle aliased parameters |
11259 | ||
11260 | if Ada_Version >= Ada_2012 | |
11261 | and then Aliased_Present (Param_Spec) | |
11262 | then | |
11263 | Set_Is_Aliased (Formal); | |
11264 | end if; | |
11265 | ||
0ab80019 | 11266 | -- Ada 2005 (AI-231): Create and decorate an internal subtype |
7324bf49 | 11267 | -- declaration corresponding to the null-excluding type of the |
d8db0bca JM |
11268 | -- formal in the enclosing scope. Finally, replace the parameter |
11269 | -- type of the formal with the internal subtype. | |
7324bf49 | 11270 | |
0791fbe9 | 11271 | if Ada_Version >= Ada_2005 |
41251c60 | 11272 | and then Null_Exclusion_Present (Param_Spec) |
7324bf49 | 11273 | then |
ec4867fa | 11274 | if not Is_Access_Type (Formal_Type) then |
ed2233dc | 11275 | Error_Msg_N |
0a36105d JM |
11276 | ("`NOT NULL` allowed only for an access type", Param_Spec); |
11277 | ||
ec4867fa ES |
11278 | else |
11279 | if Can_Never_Be_Null (Formal_Type) | |
11280 | and then Comes_From_Source (Related_Nod) | |
11281 | then | |
ed2233dc | 11282 | Error_Msg_NE |
0a36105d | 11283 | ("`NOT NULL` not allowed (& already excludes null)", |
0f1a6a0b | 11284 | Param_Spec, Formal_Type); |
ec4867fa | 11285 | end if; |
41251c60 | 11286 | |
ec4867fa ES |
11287 | Formal_Type := |
11288 | Create_Null_Excluding_Itype | |
11289 | (T => Formal_Type, | |
11290 | Related_Nod => Related_Nod, | |
11291 | Scope_Id => Scope (Current_Scope)); | |
0a36105d | 11292 | |
fcf848c4 AC |
11293 | -- If the designated type of the itype is an itype that is |
11294 | -- not frozen yet, we set the Has_Delayed_Freeze attribute | |
11295 | -- on the access subtype, to prevent order-of-elaboration | |
11296 | -- issues in the backend. | |
0a36105d JM |
11297 | |
11298 | -- Example: | |
11299 | -- type T is access procedure; | |
11300 | -- procedure Op (O : not null T); | |
11301 | ||
fcf848c4 AC |
11302 | if Is_Itype (Directly_Designated_Type (Formal_Type)) |
11303 | and then | |
11304 | not Is_Frozen (Directly_Designated_Type (Formal_Type)) | |
11305 | then | |
0a36105d JM |
11306 | Set_Has_Delayed_Freeze (Formal_Type); |
11307 | end if; | |
ec4867fa | 11308 | end if; |
7324bf49 AC |
11309 | end if; |
11310 | ||
996ae0b0 RK |
11311 | -- An access formal type |
11312 | ||
11313 | else | |
11314 | Formal_Type := | |
11315 | Access_Definition (Related_Nod, Parameter_Type (Param_Spec)); | |
7324bf49 | 11316 | |
f937473f RD |
11317 | -- No need to continue if we already notified errors |
11318 | ||
11319 | if not Present (Formal_Type) then | |
11320 | return; | |
11321 | end if; | |
11322 | ||
0ab80019 | 11323 | -- Ada 2005 (AI-254) |
7324bf49 | 11324 | |
af4b9434 AC |
11325 | declare |
11326 | AD : constant Node_Id := | |
11327 | Access_To_Subprogram_Definition | |
11328 | (Parameter_Type (Param_Spec)); | |
11329 | begin | |
11330 | if Present (AD) and then Protected_Present (AD) then | |
11331 | Formal_Type := | |
11332 | Replace_Anonymous_Access_To_Protected_Subprogram | |
f937473f | 11333 | (Param_Spec); |
af4b9434 AC |
11334 | end if; |
11335 | end; | |
996ae0b0 RK |
11336 | end if; |
11337 | ||
11338 | Set_Etype (Formal, Formal_Type); | |
0f853035 | 11339 | |
fecbd779 AC |
11340 | -- Deal with default expression if present |
11341 | ||
fbf5a39b | 11342 | Default := Expression (Param_Spec); |
996ae0b0 RK |
11343 | |
11344 | if Present (Default) then | |
ce5ba43a | 11345 | Check_SPARK_05_Restriction |
fe5d3068 | 11346 | ("default expression is not allowed", Default); |
38171f43 | 11347 | |
996ae0b0 | 11348 | if Out_Present (Param_Spec) then |
ed2233dc | 11349 | Error_Msg_N |
996ae0b0 RK |
11350 | ("default initialization only allowed for IN parameters", |
11351 | Param_Spec); | |
11352 | end if; | |
11353 | ||
11354 | -- Do the special preanalysis of the expression (see section on | |
11355 | -- "Handling of Default Expressions" in the spec of package Sem). | |
11356 | ||
21d27997 | 11357 | Preanalyze_Spec_Expression (Default, Formal_Type); |
996ae0b0 | 11358 | |
f29b857f ES |
11359 | -- An access to constant cannot be the default for |
11360 | -- an access parameter that is an access to variable. | |
2eb160f2 ST |
11361 | |
11362 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
11363 | and then not Is_Access_Constant (Formal_Type) | |
11364 | and then Is_Access_Type (Etype (Default)) | |
11365 | and then Is_Access_Constant (Etype (Default)) | |
11366 | then | |
f29b857f | 11367 | Error_Msg_N |
84c0a895 AC |
11368 | ("formal that is access to variable cannot be initialized " |
11369 | & "with an access-to-constant expression", Default); | |
2eb160f2 ST |
11370 | end if; |
11371 | ||
d8db0bca JM |
11372 | -- Check that the designated type of an access parameter's default |
11373 | -- is not a class-wide type unless the parameter's designated type | |
11374 | -- is also class-wide. | |
996ae0b0 RK |
11375 | |
11376 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
7b56a91b | 11377 | and then not Designates_From_Limited_With (Formal_Type) |
07fc65c4 | 11378 | and then Is_Class_Wide_Default (Default) |
996ae0b0 RK |
11379 | and then not Is_Class_Wide_Type (Designated_Type (Formal_Type)) |
11380 | then | |
07fc65c4 GB |
11381 | Error_Msg_N |
11382 | ("access to class-wide expression not allowed here", Default); | |
996ae0b0 | 11383 | end if; |
4755cce9 JM |
11384 | |
11385 | -- Check incorrect use of dynamically tagged expressions | |
11386 | ||
11387 | if Is_Tagged_Type (Formal_Type) then | |
11388 | Check_Dynamically_Tagged_Expression | |
11389 | (Expr => Default, | |
11390 | Typ => Formal_Type, | |
11391 | Related_Nod => Default); | |
11392 | end if; | |
996ae0b0 RK |
11393 | end if; |
11394 | ||
41251c60 JM |
11395 | -- Ada 2005 (AI-231): Static checks |
11396 | ||
0791fbe9 | 11397 | if Ada_Version >= Ada_2005 |
41251c60 JM |
11398 | and then Is_Access_Type (Etype (Formal)) |
11399 | and then Can_Never_Be_Null (Etype (Formal)) | |
11400 | then | |
11401 | Null_Exclusion_Static_Checks (Param_Spec); | |
11402 | end if; | |
11403 | ||
847d950d HK |
11404 | -- The following checks are relevant only when SPARK_Mode is on as |
11405 | -- these are not standard Ada legality rules. | |
6c3c671e | 11406 | |
ea26c8e4 HK |
11407 | if SPARK_Mode = On then |
11408 | if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then | |
f1bd0415 | 11409 | |
ea26c8e4 HK |
11410 | -- A function cannot have a parameter of mode IN OUT or OUT |
11411 | -- (SPARK RM 6.1). | |
f1bd0415 | 11412 | |
ea26c8e4 HK |
11413 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then |
11414 | Error_Msg_N | |
11415 | ("function cannot have parameter of mode `OUT` or " | |
11416 | & "`IN OUT`", Formal); | |
ea26c8e4 HK |
11417 | end if; |
11418 | ||
d780e54f AC |
11419 | -- A procedure cannot have an effectively volatile formal |
11420 | -- parameter of mode IN because it behaves as a constant | |
db7e3721 | 11421 | -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4) |
ea26c8e4 HK |
11422 | |
11423 | elsif Ekind (Scope (Formal)) = E_Procedure | |
11424 | and then Ekind (Formal) = E_In_Parameter | |
d780e54f | 11425 | and then Is_Effectively_Volatile (Formal) |
ea26c8e4 | 11426 | then |
f1bd0415 | 11427 | Error_Msg_N |
ea26c8e4 | 11428 | ("formal parameter of mode `IN` cannot be volatile", Formal); |
f1bd0415 | 11429 | end if; |
6c3c671e AC |
11430 | end if; |
11431 | ||
996ae0b0 RK |
11432 | <<Continue>> |
11433 | Next (Param_Spec); | |
11434 | end loop; | |
11435 | ||
82c80734 RD |
11436 | -- If this is the formal part of a function specification, analyze the |
11437 | -- subtype mark in the context where the formals are visible but not | |
11438 | -- yet usable, and may hide outer homographs. | |
11439 | ||
11440 | if Nkind (Related_Nod) = N_Function_Specification then | |
11441 | Analyze_Return_Type (Related_Nod); | |
11442 | end if; | |
11443 | ||
996ae0b0 RK |
11444 | -- Now set the kind (mode) of each formal |
11445 | ||
11446 | Param_Spec := First (T); | |
996ae0b0 RK |
11447 | while Present (Param_Spec) loop |
11448 | Formal := Defining_Identifier (Param_Spec); | |
11449 | Set_Formal_Mode (Formal); | |
11450 | ||
11451 | if Ekind (Formal) = E_In_Parameter then | |
11452 | Set_Default_Value (Formal, Expression (Param_Spec)); | |
11453 | ||
11454 | if Present (Expression (Param_Spec)) then | |
c8307596 | 11455 | Default := Expression (Param_Spec); |
996ae0b0 RK |
11456 | |
11457 | if Is_Scalar_Type (Etype (Default)) then | |
5ebfaacf AC |
11458 | if Nkind (Parameter_Type (Param_Spec)) /= |
11459 | N_Access_Definition | |
996ae0b0 RK |
11460 | then |
11461 | Formal_Type := Entity (Parameter_Type (Param_Spec)); | |
996ae0b0 | 11462 | else |
5ebfaacf AC |
11463 | Formal_Type := |
11464 | Access_Definition | |
11465 | (Related_Nod, Parameter_Type (Param_Spec)); | |
996ae0b0 RK |
11466 | end if; |
11467 | ||
11468 | Apply_Scalar_Range_Check (Default, Formal_Type); | |
11469 | end if; | |
2820d220 | 11470 | end if; |
800621e0 RD |
11471 | |
11472 | elsif Ekind (Formal) = E_Out_Parameter then | |
11473 | Num_Out_Params := Num_Out_Params + 1; | |
11474 | ||
11475 | if Num_Out_Params = 1 then | |
11476 | First_Out_Param := Formal; | |
11477 | end if; | |
11478 | ||
11479 | elsif Ekind (Formal) = E_In_Out_Parameter then | |
11480 | Num_Out_Params := Num_Out_Params + 1; | |
996ae0b0 RK |
11481 | end if; |
11482 | ||
4172a8e3 AC |
11483 | -- Skip remaining processing if formal type was in error |
11484 | ||
11485 | if Etype (Formal) = Any_Type or else Error_Posted (Formal) then | |
11486 | goto Next_Parameter; | |
11487 | end if; | |
11488 | ||
fecbd779 AC |
11489 | -- Force call by reference if aliased |
11490 | ||
1e60643a AC |
11491 | declare |
11492 | Conv : constant Convention_Id := Convention (Etype (Formal)); | |
11493 | begin | |
11494 | if Is_Aliased (Formal) then | |
11495 | Set_Mechanism (Formal, By_Reference); | |
5ebfaacf | 11496 | |
1e60643a | 11497 | -- Warn if user asked this to be passed by copy |
5ebfaacf | 11498 | |
1e60643a AC |
11499 | if Conv = Convention_Ada_Pass_By_Copy then |
11500 | Error_Msg_N | |
11501 | ("cannot pass aliased parameter & by copy??", Formal); | |
11502 | end if; | |
5ebfaacf | 11503 | |
1e60643a | 11504 | -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy |
5ebfaacf | 11505 | |
1e60643a AC |
11506 | elsif Conv = Convention_Ada_Pass_By_Copy then |
11507 | Set_Mechanism (Formal, By_Copy); | |
5ebfaacf | 11508 | |
1e60643a AC |
11509 | elsif Conv = Convention_Ada_Pass_By_Reference then |
11510 | Set_Mechanism (Formal, By_Reference); | |
11511 | end if; | |
11512 | end; | |
fecbd779 | 11513 | |
4172a8e3 | 11514 | <<Next_Parameter>> |
996ae0b0 RK |
11515 | Next (Param_Spec); |
11516 | end loop; | |
800621e0 RD |
11517 | |
11518 | if Present (First_Out_Param) and then Num_Out_Params = 1 then | |
11519 | Set_Is_Only_Out_Parameter (First_Out_Param); | |
11520 | end if; | |
996ae0b0 RK |
11521 | end Process_Formals; |
11522 | ||
fbf5a39b AC |
11523 | ---------------------------- |
11524 | -- Reference_Body_Formals -- | |
11525 | ---------------------------- | |
11526 | ||
11527 | procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is | |
11528 | Fs : Entity_Id; | |
11529 | Fb : Entity_Id; | |
11530 | ||
11531 | begin | |
11532 | if Error_Posted (Spec) then | |
11533 | return; | |
11534 | end if; | |
11535 | ||
0a36105d JM |
11536 | -- Iterate over both lists. They may be of different lengths if the two |
11537 | -- specs are not conformant. | |
11538 | ||
fbf5a39b AC |
11539 | Fs := First_Formal (Spec); |
11540 | Fb := First_Formal (Bod); | |
0a36105d | 11541 | while Present (Fs) and then Present (Fb) loop |
fbf5a39b AC |
11542 | Generate_Reference (Fs, Fb, 'b'); |
11543 | ||
11544 | if Style_Check then | |
11545 | Style.Check_Identifier (Fb, Fs); | |
11546 | end if; | |
11547 | ||
11548 | Set_Spec_Entity (Fb, Fs); | |
11549 | Set_Referenced (Fs, False); | |
11550 | Next_Formal (Fs); | |
11551 | Next_Formal (Fb); | |
11552 | end loop; | |
11553 | end Reference_Body_Formals; | |
11554 | ||
996ae0b0 RK |
11555 | ------------------------- |
11556 | -- Set_Actual_Subtypes -- | |
11557 | ------------------------- | |
11558 | ||
11559 | procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is | |
84c0a895 AC |
11560 | Decl : Node_Id; |
11561 | Formal : Entity_Id; | |
11562 | T : Entity_Id; | |
11563 | First_Stmt : Node_Id := Empty; | |
11564 | AS_Needed : Boolean; | |
996ae0b0 RK |
11565 | |
11566 | begin | |
f3d57416 | 11567 | -- If this is an empty initialization procedure, no need to create |
fbf5a39b AC |
11568 | -- actual subtypes (small optimization). |
11569 | ||
8fde064e | 11570 | if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then |
fbf5a39b AC |
11571 | return; |
11572 | end if; | |
11573 | ||
124bed29 AC |
11574 | -- The subtype declarations may freeze the formals. The body generated |
11575 | -- for an expression function is not a freeze point, so do not emit | |
11576 | -- these declarations (small loss of efficiency in rare cases). | |
11577 | ||
11578 | if Nkind (N) = N_Subprogram_Body | |
11579 | and then Was_Expression_Function (N) | |
11580 | then | |
11581 | return; | |
11582 | end if; | |
11583 | ||
996ae0b0 RK |
11584 | Formal := First_Formal (Subp); |
11585 | while Present (Formal) loop | |
11586 | T := Etype (Formal); | |
11587 | ||
e895b435 | 11588 | -- We never need an actual subtype for a constrained formal |
996ae0b0 RK |
11589 | |
11590 | if Is_Constrained (T) then | |
11591 | AS_Needed := False; | |
11592 | ||
82c80734 | 11593 | -- If we have unknown discriminants, then we do not need an actual |
a90bd866 | 11594 | -- subtype, or more accurately we cannot figure it out. Note that |
82c80734 | 11595 | -- all class-wide types have unknown discriminants. |
996ae0b0 RK |
11596 | |
11597 | elsif Has_Unknown_Discriminants (T) then | |
11598 | AS_Needed := False; | |
11599 | ||
82c80734 RD |
11600 | -- At this stage we have an unconstrained type that may need an |
11601 | -- actual subtype. For sure the actual subtype is needed if we have | |
405b907c AC |
11602 | -- an unconstrained array type. However, in an instance, the type |
11603 | -- may appear as a subtype of the full view, while the actual is | |
11604 | -- in fact private (in which case no actual subtype is needed) so | |
11605 | -- check the kind of the base type. | |
996ae0b0 | 11606 | |
405b907c | 11607 | elsif Is_Array_Type (Base_Type (T)) then |
996ae0b0 RK |
11608 | AS_Needed := True; |
11609 | ||
d8db0bca JM |
11610 | -- The only other case needing an actual subtype is an unconstrained |
11611 | -- record type which is an IN parameter (we cannot generate actual | |
11612 | -- subtypes for the OUT or IN OUT case, since an assignment can | |
11613 | -- change the discriminant values. However we exclude the case of | |
11614 | -- initialization procedures, since discriminants are handled very | |
11615 | -- specially in this context, see the section entitled "Handling of | |
11616 | -- Discriminants" in Einfo. | |
11617 | ||
11618 | -- We also exclude the case of Discrim_SO_Functions (functions used | |
ca90b962 | 11619 | -- in front-end layout mode for size/offset values), since in such |
d8db0bca JM |
11620 | -- functions only discriminants are referenced, and not only are such |
11621 | -- subtypes not needed, but they cannot always be generated, because | |
11622 | -- of order of elaboration issues. | |
996ae0b0 RK |
11623 | |
11624 | elsif Is_Record_Type (T) | |
11625 | and then Ekind (Formal) = E_In_Parameter | |
11626 | and then Chars (Formal) /= Name_uInit | |
5d09245e | 11627 | and then not Is_Unchecked_Union (T) |
996ae0b0 RK |
11628 | and then not Is_Discrim_SO_Function (Subp) |
11629 | then | |
11630 | AS_Needed := True; | |
11631 | ||
11632 | -- All other cases do not need an actual subtype | |
11633 | ||
11634 | else | |
11635 | AS_Needed := False; | |
11636 | end if; | |
11637 | ||
11638 | -- Generate actual subtypes for unconstrained arrays and | |
11639 | -- unconstrained discriminated records. | |
11640 | ||
11641 | if AS_Needed then | |
7324bf49 | 11642 | if Nkind (N) = N_Accept_Statement then |
fbf5a39b | 11643 | |
57a8057a | 11644 | -- If expansion is active, the formal is replaced by a local |
fbf5a39b AC |
11645 | -- variable that renames the corresponding entry of the |
11646 | -- parameter block, and it is this local variable that may | |
da94696d | 11647 | -- require an actual subtype. |
fbf5a39b | 11648 | |
4460a9bc | 11649 | if Expander_Active then |
fbf5a39b AC |
11650 | Decl := Build_Actual_Subtype (T, Renamed_Object (Formal)); |
11651 | else | |
11652 | Decl := Build_Actual_Subtype (T, Formal); | |
11653 | end if; | |
11654 | ||
996ae0b0 RK |
11655 | if Present (Handled_Statement_Sequence (N)) then |
11656 | First_Stmt := | |
11657 | First (Statements (Handled_Statement_Sequence (N))); | |
11658 | Prepend (Decl, Statements (Handled_Statement_Sequence (N))); | |
11659 | Mark_Rewrite_Insertion (Decl); | |
11660 | else | |
82c80734 RD |
11661 | -- If the accept statement has no body, there will be no |
11662 | -- reference to the actuals, so no need to compute actual | |
11663 | -- subtypes. | |
996ae0b0 RK |
11664 | |
11665 | return; | |
11666 | end if; | |
11667 | ||
11668 | else | |
fbf5a39b | 11669 | Decl := Build_Actual_Subtype (T, Formal); |
996ae0b0 RK |
11670 | Prepend (Decl, Declarations (N)); |
11671 | Mark_Rewrite_Insertion (Decl); | |
11672 | end if; | |
11673 | ||
82c80734 RD |
11674 | -- The declaration uses the bounds of an existing object, and |
11675 | -- therefore needs no constraint checks. | |
2820d220 | 11676 | |
7324bf49 | 11677 | Analyze (Decl, Suppress => All_Checks); |
405b907c | 11678 | Set_Is_Actual_Subtype (Defining_Identifier (Decl)); |
2820d220 | 11679 | |
996ae0b0 RK |
11680 | -- We need to freeze manually the generated type when it is |
11681 | -- inserted anywhere else than in a declarative part. | |
11682 | ||
11683 | if Present (First_Stmt) then | |
11684 | Insert_List_Before_And_Analyze (First_Stmt, | |
c159409f | 11685 | Freeze_Entity (Defining_Identifier (Decl), N)); |
fcadacf7 ES |
11686 | |
11687 | -- Ditto if the type has a dynamic predicate, because the | |
22da8770 AC |
11688 | -- generated function will mention the actual subtype. The |
11689 | -- predicate may come from an explicit aspect of be inherited. | |
fcadacf7 | 11690 | |
22da8770 | 11691 | elsif Has_Predicates (T) then |
fcadacf7 ES |
11692 | Insert_List_Before_And_Analyze (Decl, |
11693 | Freeze_Entity (Defining_Identifier (Decl), N)); | |
996ae0b0 RK |
11694 | end if; |
11695 | ||
fbf5a39b | 11696 | if Nkind (N) = N_Accept_Statement |
4460a9bc | 11697 | and then Expander_Active |
fbf5a39b AC |
11698 | then |
11699 | Set_Actual_Subtype (Renamed_Object (Formal), | |
11700 | Defining_Identifier (Decl)); | |
11701 | else | |
11702 | Set_Actual_Subtype (Formal, Defining_Identifier (Decl)); | |
11703 | end if; | |
996ae0b0 RK |
11704 | end if; |
11705 | ||
11706 | Next_Formal (Formal); | |
11707 | end loop; | |
11708 | end Set_Actual_Subtypes; | |
11709 | ||
11710 | --------------------- | |
11711 | -- Set_Formal_Mode -- | |
11712 | --------------------- | |
11713 | ||
11714 | procedure Set_Formal_Mode (Formal_Id : Entity_Id) is | |
07aa5e6f | 11715 | Spec : constant Node_Id := Parent (Formal_Id); |
288cbbbd | 11716 | Id : constant Entity_Id := Scope (Formal_Id); |
996ae0b0 RK |
11717 | |
11718 | begin | |
11719 | -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters | |
11720 | -- since we ensure that corresponding actuals are always valid at the | |
11721 | -- point of the call. | |
11722 | ||
11723 | if Out_Present (Spec) then | |
288cbbbd JM |
11724 | if Ekind_In (Id, E_Entry, E_Entry_Family) |
11725 | or else Is_Subprogram_Or_Generic_Subprogram (Id) | |
11726 | then | |
11727 | Set_Has_Out_Or_In_Out_Parameter (Id, True); | |
11728 | end if; | |
11729 | ||
11730 | if Ekind_In (Id, E_Function, E_Generic_Function) then | |
fc999c5d | 11731 | |
b4ca2d2c | 11732 | -- [IN] OUT parameters allowed for functions in Ada 2012 |
c56a9ba4 AC |
11733 | |
11734 | if Ada_Version >= Ada_2012 then | |
e6425869 AC |
11735 | |
11736 | -- Even in Ada 2012 operators can only have IN parameters | |
11737 | ||
11738 | if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then | |
11739 | Error_Msg_N ("operators can only have IN parameters", Spec); | |
11740 | end if; | |
11741 | ||
c56a9ba4 AC |
11742 | if In_Present (Spec) then |
11743 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
11744 | else | |
11745 | Set_Ekind (Formal_Id, E_Out_Parameter); | |
11746 | end if; | |
11747 | ||
b4ca2d2c AC |
11748 | -- But not in earlier versions of Ada |
11749 | ||
c56a9ba4 AC |
11750 | else |
11751 | Error_Msg_N ("functions can only have IN parameters", Spec); | |
11752 | Set_Ekind (Formal_Id, E_In_Parameter); | |
11753 | end if; | |
996ae0b0 RK |
11754 | |
11755 | elsif In_Present (Spec) then | |
11756 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
11757 | ||
11758 | else | |
fbf5a39b AC |
11759 | Set_Ekind (Formal_Id, E_Out_Parameter); |
11760 | Set_Never_Set_In_Source (Formal_Id, True); | |
11761 | Set_Is_True_Constant (Formal_Id, False); | |
11762 | Set_Current_Value (Formal_Id, Empty); | |
996ae0b0 RK |
11763 | end if; |
11764 | ||
11765 | else | |
11766 | Set_Ekind (Formal_Id, E_In_Parameter); | |
11767 | end if; | |
11768 | ||
fbf5a39b | 11769 | -- Set Is_Known_Non_Null for access parameters since the language |
82c80734 RD |
11770 | -- guarantees that access parameters are always non-null. We also set |
11771 | -- Can_Never_Be_Null, since there is no way to change the value. | |
fbf5a39b AC |
11772 | |
11773 | if Nkind (Parameter_Type (Spec)) = N_Access_Definition then | |
2820d220 | 11774 | |
885c4871 | 11775 | -- Ada 2005 (AI-231): In Ada 95, access parameters are always non- |
2813bb6b | 11776 | -- null; In Ada 2005, only if then null_exclusion is explicit. |
2820d220 | 11777 | |
0791fbe9 | 11778 | if Ada_Version < Ada_2005 |
2813bb6b | 11779 | or else Can_Never_Be_Null (Etype (Formal_Id)) |
2820d220 AC |
11780 | then |
11781 | Set_Is_Known_Non_Null (Formal_Id); | |
11782 | Set_Can_Never_Be_Null (Formal_Id); | |
11783 | end if; | |
2813bb6b | 11784 | |
41251c60 JM |
11785 | -- Ada 2005 (AI-231): Null-exclusion access subtype |
11786 | ||
2813bb6b ES |
11787 | elsif Is_Access_Type (Etype (Formal_Id)) |
11788 | and then Can_Never_Be_Null (Etype (Formal_Id)) | |
11789 | then | |
2813bb6b | 11790 | Set_Is_Known_Non_Null (Formal_Id); |
a1d72281 EB |
11791 | |
11792 | -- We can also set Can_Never_Be_Null (thus preventing some junk | |
11793 | -- access checks) for the case of an IN parameter, which cannot | |
11794 | -- be changed, or for an IN OUT parameter, which can be changed but | |
11795 | -- not to a null value. But for an OUT parameter, the initial value | |
11796 | -- passed in can be null, so we can't set this flag in that case. | |
11797 | ||
11798 | if Ekind (Formal_Id) /= E_Out_Parameter then | |
11799 | Set_Can_Never_Be_Null (Formal_Id); | |
11800 | end if; | |
fbf5a39b AC |
11801 | end if; |
11802 | ||
996ae0b0 RK |
11803 | Set_Mechanism (Formal_Id, Default_Mechanism); |
11804 | Set_Formal_Validity (Formal_Id); | |
11805 | end Set_Formal_Mode; | |
11806 | ||
11807 | ------------------------- | |
11808 | -- Set_Formal_Validity -- | |
11809 | ------------------------- | |
11810 | ||
11811 | procedure Set_Formal_Validity (Formal_Id : Entity_Id) is | |
11812 | begin | |
82c80734 RD |
11813 | -- If no validity checking, then we cannot assume anything about the |
11814 | -- validity of parameters, since we do not know there is any checking | |
11815 | -- of the validity on the call side. | |
996ae0b0 RK |
11816 | |
11817 | if not Validity_Checks_On then | |
11818 | return; | |
11819 | ||
fbf5a39b AC |
11820 | -- If validity checking for parameters is enabled, this means we are |
11821 | -- not supposed to make any assumptions about argument values. | |
11822 | ||
11823 | elsif Validity_Check_Parameters then | |
11824 | return; | |
11825 | ||
11826 | -- If we are checking in parameters, we will assume that the caller is | |
11827 | -- also checking parameters, so we can assume the parameter is valid. | |
11828 | ||
996ae0b0 RK |
11829 | elsif Ekind (Formal_Id) = E_In_Parameter |
11830 | and then Validity_Check_In_Params | |
11831 | then | |
11832 | Set_Is_Known_Valid (Formal_Id, True); | |
11833 | ||
fbf5a39b AC |
11834 | -- Similar treatment for IN OUT parameters |
11835 | ||
996ae0b0 RK |
11836 | elsif Ekind (Formal_Id) = E_In_Out_Parameter |
11837 | and then Validity_Check_In_Out_Params | |
11838 | then | |
11839 | Set_Is_Known_Valid (Formal_Id, True); | |
11840 | end if; | |
11841 | end Set_Formal_Validity; | |
11842 | ||
11843 | ------------------------ | |
11844 | -- Subtype_Conformant -- | |
11845 | ------------------------ | |
11846 | ||
ce2b6ba5 JM |
11847 | function Subtype_Conformant |
11848 | (New_Id : Entity_Id; | |
11849 | Old_Id : Entity_Id; | |
11850 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
11851 | is | |
996ae0b0 | 11852 | Result : Boolean; |
996ae0b0 | 11853 | begin |
ce2b6ba5 JM |
11854 | Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result, |
11855 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
11856 | return Result; |
11857 | end Subtype_Conformant; | |
11858 | ||
11859 | --------------------- | |
11860 | -- Type_Conformant -- | |
11861 | --------------------- | |
11862 | ||
41251c60 JM |
11863 | function Type_Conformant |
11864 | (New_Id : Entity_Id; | |
11865 | Old_Id : Entity_Id; | |
11866 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
11867 | is | |
996ae0b0 | 11868 | Result : Boolean; |
996ae0b0 | 11869 | begin |
c8ef728f | 11870 | May_Hide_Profile := False; |
41251c60 JM |
11871 | Check_Conformance |
11872 | (New_Id, Old_Id, Type_Conformant, False, Result, | |
11873 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
11874 | return Result; |
11875 | end Type_Conformant; | |
11876 | ||
11877 | ------------------------------- | |
11878 | -- Valid_Operator_Definition -- | |
11879 | ------------------------------- | |
11880 | ||
11881 | procedure Valid_Operator_Definition (Designator : Entity_Id) is | |
11882 | N : Integer := 0; | |
11883 | F : Entity_Id; | |
11884 | Id : constant Name_Id := Chars (Designator); | |
11885 | N_OK : Boolean; | |
11886 | ||
11887 | begin | |
11888 | F := First_Formal (Designator); | |
996ae0b0 RK |
11889 | while Present (F) loop |
11890 | N := N + 1; | |
11891 | ||
11892 | if Present (Default_Value (F)) then | |
ed2233dc | 11893 | Error_Msg_N |
996ae0b0 RK |
11894 | ("default values not allowed for operator parameters", |
11895 | Parent (F)); | |
220d1fd9 AC |
11896 | |
11897 | -- For function instantiations that are operators, we must check | |
11898 | -- separately that the corresponding generic only has in-parameters. | |
84c0a895 AC |
11899 | -- For subprogram declarations this is done in Set_Formal_Mode. Such |
11900 | -- an error could not arise in earlier versions of the language. | |
220d1fd9 AC |
11901 | |
11902 | elsif Ekind (F) /= E_In_Parameter then | |
84c0a895 | 11903 | Error_Msg_N ("operators can only have IN parameters", F); |
996ae0b0 RK |
11904 | end if; |
11905 | ||
11906 | Next_Formal (F); | |
11907 | end loop; | |
11908 | ||
11909 | -- Verify that user-defined operators have proper number of arguments | |
11910 | -- First case of operators which can only be unary | |
11911 | ||
b69cd36a | 11912 | if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then |
996ae0b0 RK |
11913 | N_OK := (N = 1); |
11914 | ||
11915 | -- Case of operators which can be unary or binary | |
11916 | ||
b69cd36a | 11917 | elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then |
996ae0b0 RK |
11918 | N_OK := (N in 1 .. 2); |
11919 | ||
11920 | -- All other operators can only be binary | |
11921 | ||
11922 | else | |
11923 | N_OK := (N = 2); | |
11924 | end if; | |
11925 | ||
11926 | if not N_OK then | |
11927 | Error_Msg_N | |
11928 | ("incorrect number of arguments for operator", Designator); | |
11929 | end if; | |
11930 | ||
11931 | if Id = Name_Op_Ne | |
11932 | and then Base_Type (Etype (Designator)) = Standard_Boolean | |
11933 | and then not Is_Intrinsic_Subprogram (Designator) | |
11934 | then | |
11935 | Error_Msg_N | |
84c0a895 | 11936 | ("explicit definition of inequality not allowed", Designator); |
996ae0b0 RK |
11937 | end if; |
11938 | end Valid_Operator_Definition; | |
11939 | ||
11940 | end Sem_Ch6; |