]>
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 | -- -- |
c7b9d548 | 9 | -- Copyright (C) 1992-2011, 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 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Debug; use Debug; | |
29 | with Einfo; use Einfo; | |
30 | with Elists; use Elists; | |
31 | with Errout; use Errout; | |
32 | with Expander; use Expander; | |
ec4867fa | 33 | with Exp_Ch6; use Exp_Ch6; |
996ae0b0 | 34 | with Exp_Ch7; use Exp_Ch7; |
21d27997 | 35 | with Exp_Ch9; use Exp_Ch9; |
ce2b6ba5 | 36 | with Exp_Disp; use Exp_Disp; |
e660dbf7 | 37 | with Exp_Tss; use Exp_Tss; |
ec4867fa | 38 | with Exp_Util; use Exp_Util; |
fbf5a39b | 39 | with Fname; use Fname; |
996ae0b0 | 40 | with Freeze; use Freeze; |
41251c60 | 41 | with Itypes; use Itypes; |
996ae0b0 | 42 | with Lib.Xref; use Lib.Xref; |
ec4867fa | 43 | with Layout; use Layout; |
996ae0b0 RK |
44 | with Namet; use Namet; |
45 | with Lib; use Lib; | |
46 | with Nlists; use Nlists; | |
47 | with Nmake; use Nmake; | |
48 | with Opt; use Opt; | |
49 | with Output; use Output; | |
b20de9b9 AC |
50 | with Restrict; use Restrict; |
51 | with Rident; use Rident; | |
996ae0b0 RK |
52 | with Rtsfind; use Rtsfind; |
53 | with Sem; use Sem; | |
a4100e55 | 54 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
55 | with Sem_Cat; use Sem_Cat; |
56 | with Sem_Ch3; use Sem_Ch3; | |
57 | with Sem_Ch4; use Sem_Ch4; | |
58 | with Sem_Ch5; use Sem_Ch5; | |
59 | with Sem_Ch8; use Sem_Ch8; | |
9bc856dd | 60 | with Sem_Ch10; use Sem_Ch10; |
996ae0b0 | 61 | with Sem_Ch12; use Sem_Ch12; |
0f1a6a0b | 62 | with Sem_Ch13; use Sem_Ch13; |
996ae0b0 RK |
63 | with Sem_Disp; use Sem_Disp; |
64 | with Sem_Dist; use Sem_Dist; | |
65 | with Sem_Elim; use Sem_Elim; | |
66 | with Sem_Eval; use Sem_Eval; | |
67 | with Sem_Mech; use Sem_Mech; | |
68 | with Sem_Prag; use Sem_Prag; | |
69 | with Sem_Res; use Sem_Res; | |
70 | with Sem_Util; use Sem_Util; | |
71 | with Sem_Type; use Sem_Type; | |
72 | with Sem_Warn; use Sem_Warn; | |
73 | with Sinput; use Sinput; | |
74 | with Stand; use Stand; | |
75 | with Sinfo; use Sinfo; | |
76 | with Sinfo.CN; use Sinfo.CN; | |
77 | with Snames; use Snames; | |
78 | with Stringt; use Stringt; | |
79 | with Style; | |
80 | with Stylesw; use Stylesw; | |
8417f4b2 | 81 | with Targparm; use Targparm; |
996ae0b0 RK |
82 | with Tbuild; use Tbuild; |
83 | with Uintp; use Uintp; | |
84 | with Urealp; use Urealp; | |
85 | with Validsw; use Validsw; | |
86 | ||
87 | package body Sem_Ch6 is | |
88 | ||
c8ef728f | 89 | May_Hide_Profile : Boolean := False; |
ec4867fa ES |
90 | -- This flag is used to indicate that two formals in two subprograms being |
91 | -- checked for conformance differ only in that one is an access parameter | |
92 | -- while the other is of a general access type with the same designated | |
93 | -- type. In this case, if the rest of the signatures match, a call to | |
94 | -- either subprogram may be ambiguous, which is worth a warning. The flag | |
95 | -- is set in Compatible_Types, and the warning emitted in | |
96 | -- New_Overloaded_Entity. | |
c8ef728f | 97 | |
996ae0b0 RK |
98 | ----------------------- |
99 | -- Local Subprograms -- | |
100 | ----------------------- | |
101 | ||
5d37ba92 | 102 | procedure Analyze_Return_Statement (N : Node_Id); |
5b9c3fc4 | 103 | -- Common processing for simple and extended return statements |
ec4867fa ES |
104 | |
105 | procedure Analyze_Function_Return (N : Node_Id); | |
81db9d77 ES |
106 | -- Subsidiary to Analyze_Return_Statement. Called when the return statement |
107 | -- applies to a [generic] function. | |
ec4867fa | 108 | |
82c80734 RD |
109 | procedure Analyze_Return_Type (N : Node_Id); |
110 | -- Subsidiary to Process_Formals: analyze subtype mark in function | |
5b9c3fc4 | 111 | -- specification in a context where the formals are visible and hide |
82c80734 RD |
112 | -- outer homographs. |
113 | ||
b1b543d2 | 114 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id); |
13d923cc RD |
115 | -- Does all the real work of Analyze_Subprogram_Body. This is split out so |
116 | -- that we can use RETURN but not skip the debug output at the end. | |
b1b543d2 | 117 | |
996ae0b0 | 118 | procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); |
82c80734 RD |
119 | -- Analyze a generic subprogram body. N is the body to be analyzed, and |
120 | -- Gen_Id is the defining entity Id for the corresponding spec. | |
996ae0b0 | 121 | |
d05ef0ab | 122 | procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id); |
996ae0b0 RK |
123 | -- If a subprogram has pragma Inline and inlining is active, use generic |
124 | -- machinery to build an unexpanded body for the subprogram. This body is | |
f3d57416 | 125 | -- subsequently used for inline expansions at call sites. If subprogram can |
996ae0b0 RK |
126 | -- be inlined (depending on size and nature of local declarations) this |
127 | -- function returns true. Otherwise subprogram body is treated normally. | |
aa720a54 AC |
128 | -- If proper warnings are enabled and the subprogram contains a construct |
129 | -- that cannot be inlined, the offending construct is flagged accordingly. | |
996ae0b0 | 130 | |
806f6d37 AC |
131 | function Can_Override_Operator (Subp : Entity_Id) return Boolean; |
132 | -- Returns true if Subp can override a predefined operator. | |
133 | ||
996ae0b0 | 134 | procedure Check_Conformance |
41251c60 JM |
135 | (New_Id : Entity_Id; |
136 | Old_Id : Entity_Id; | |
137 | Ctype : Conformance_Type; | |
138 | Errmsg : Boolean; | |
139 | Conforms : out Boolean; | |
140 | Err_Loc : Node_Id := Empty; | |
141 | Get_Inst : Boolean := False; | |
142 | Skip_Controlling_Formals : Boolean := False); | |
996ae0b0 RK |
143 | -- Given two entities, this procedure checks that the profiles associated |
144 | -- with these entities meet the conformance criterion given by the third | |
145 | -- parameter. If they conform, Conforms is set True and control returns | |
146 | -- to the caller. If they do not conform, Conforms is set to False, and | |
147 | -- in addition, if Errmsg is True on the call, proper messages are output | |
148 | -- to complain about the conformance failure. If Err_Loc is non_Empty | |
149 | -- the error messages are placed on Err_Loc, if Err_Loc is empty, then | |
150 | -- error messages are placed on the appropriate part of the construct | |
151 | -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance | |
152 | -- against a formal access-to-subprogram type so Get_Instance_Of must | |
153 | -- be called. | |
154 | ||
155 | procedure Check_Subprogram_Order (N : Node_Id); | |
156 | -- N is the N_Subprogram_Body node for a subprogram. This routine applies | |
157 | -- the alpha ordering rule for N if this ordering requirement applicable. | |
158 | ||
996ae0b0 RK |
159 | procedure Check_Returns |
160 | (HSS : Node_Id; | |
161 | Mode : Character; | |
c8ef728f ES |
162 | Err : out Boolean; |
163 | Proc : Entity_Id := Empty); | |
164 | -- Called to check for missing return statements in a function body, or for | |
0a36105d | 165 | -- returns present in a procedure body which has No_Return set. HSS is the |
c8ef728f ES |
166 | -- handled statement sequence for the subprogram body. This procedure |
167 | -- checks all flow paths to make sure they either have return (Mode = 'F', | |
168 | -- used for functions) or do not have a return (Mode = 'P', used for | |
169 | -- No_Return procedures). The flag Err is set if there are any control | |
170 | -- paths not explicitly terminated by a return in the function case, and is | |
171 | -- True otherwise. Proc is the entity for the procedure case and is used | |
172 | -- in posting the warning message. | |
996ae0b0 | 173 | |
e5a58fac AC |
174 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id); |
175 | -- In Ada 2012, a primitive equality operator on an untagged record type | |
176 | -- must appear before the type is frozen, and have the same visibility as | |
177 | -- that of the type. This procedure checks that this rule is met, and | |
178 | -- otherwise emits an error on the subprogram declaration and a warning | |
179 | -- on the earlier freeze point if it is easy to locate. | |
180 | ||
996ae0b0 | 181 | procedure Enter_Overloaded_Entity (S : Entity_Id); |
82c80734 RD |
182 | -- This procedure makes S, a new overloaded entity, into the first visible |
183 | -- entity with that name. | |
996ae0b0 | 184 | |
a5b62485 AC |
185 | function Is_Non_Overriding_Operation |
186 | (Prev_E : Entity_Id; | |
187 | New_E : Entity_Id) return Boolean; | |
188 | -- Enforce the rule given in 12.3(18): a private operation in an instance | |
189 | -- overrides an inherited operation only if the corresponding operation | |
190 | -- was overriding in the generic. This can happen for primitive operations | |
191 | -- of types derived (in the generic unit) from formal private or formal | |
192 | -- derived types. | |
193 | ||
996ae0b0 RK |
194 | procedure Make_Inequality_Operator (S : Entity_Id); |
195 | -- Create the declaration for an inequality operator that is implicitly | |
196 | -- created by a user-defined equality operator that yields a boolean. | |
197 | ||
198 | procedure May_Need_Actuals (Fun : Entity_Id); | |
199 | -- Flag functions that can be called without parameters, i.e. those that | |
200 | -- have no parameters, or those for which defaults exist for all parameters | |
201 | ||
21d27997 RD |
202 | procedure Process_PPCs |
203 | (N : Node_Id; | |
204 | Spec_Id : Entity_Id; | |
205 | Body_Id : Entity_Id); | |
3764bb00 BD |
206 | -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post |
207 | -- conditions for the body and assembling and inserting the _postconditions | |
208 | -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are | |
209 | -- the entities for the body and separate spec (if there is no separate | |
b4ca2d2c AC |
210 | -- spec, Spec_Id is Empty). Note that invariants and predicates may also |
211 | -- provide postconditions, and are also handled in this procedure. | |
21d27997 | 212 | |
996ae0b0 RK |
213 | procedure Set_Formal_Validity (Formal_Id : Entity_Id); |
214 | -- Formal_Id is an formal parameter entity. This procedure deals with | |
e358346d AC |
215 | -- setting the proper validity status for this entity, which depends on |
216 | -- the kind of parameter and the validity checking mode. | |
996ae0b0 RK |
217 | |
218 | --------------------------------------------- | |
219 | -- Analyze_Abstract_Subprogram_Declaration -- | |
220 | --------------------------------------------- | |
221 | ||
222 | procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is | |
fbf5a39b AC |
223 | Designator : constant Entity_Id := |
224 | Analyze_Subprogram_Specification (Specification (N)); | |
996ae0b0 RK |
225 | Scop : constant Entity_Id := Current_Scope; |
226 | ||
227 | begin | |
2ba431e5 | 228 | Check_SPARK_Restriction ("abstract subprogram is not allowed", N); |
38171f43 | 229 | |
996ae0b0 | 230 | Generate_Definition (Designator); |
dac3bede | 231 | Set_Contract (Designator, Make_Contract (Sloc (Designator))); |
f937473f | 232 | Set_Is_Abstract_Subprogram (Designator); |
996ae0b0 RK |
233 | New_Overloaded_Entity (Designator); |
234 | Check_Delayed_Subprogram (Designator); | |
235 | ||
fbf5a39b | 236 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 RK |
237 | |
238 | if Ekind (Scope (Designator)) = E_Protected_Type then | |
239 | Error_Msg_N | |
240 | ("abstract subprogram not allowed in protected type", N); | |
5d37ba92 ES |
241 | |
242 | -- Issue a warning if the abstract subprogram is neither a dispatching | |
243 | -- operation nor an operation that overrides an inherited subprogram or | |
244 | -- predefined operator, since this most likely indicates a mistake. | |
245 | ||
246 | elsif Warn_On_Redundant_Constructs | |
247 | and then not Is_Dispatching_Operation (Designator) | |
038140ed | 248 | and then not Present (Overridden_Operation (Designator)) |
5d37ba92 ES |
249 | and then (not Is_Operator_Symbol_Name (Chars (Designator)) |
250 | or else Scop /= Scope (Etype (First_Formal (Designator)))) | |
251 | then | |
252 | Error_Msg_N | |
253 | ("?abstract subprogram is not dispatching or overriding", N); | |
996ae0b0 | 254 | end if; |
fbf5a39b AC |
255 | |
256 | Generate_Reference_To_Formals (Designator); | |
361effb1 | 257 | Check_Eliminated (Designator); |
eaba57fb RD |
258 | |
259 | if Has_Aspects (N) then | |
260 | Analyze_Aspect_Specifications (N, Designator); | |
261 | end if; | |
996ae0b0 RK |
262 | end Analyze_Abstract_Subprogram_Declaration; |
263 | ||
b0186f71 AC |
264 | --------------------------------- |
265 | -- Analyze_Expression_Function -- | |
266 | --------------------------------- | |
267 | ||
268 | procedure Analyze_Expression_Function (N : Node_Id) is | |
269 | Loc : constant Source_Ptr := Sloc (N); | |
270 | LocX : constant Source_Ptr := Sloc (Expression (N)); | |
0b5b2bbc | 271 | Expr : constant Node_Id := Expression (N); |
d2d4b355 AC |
272 | Spec : constant Node_Id := Specification (N); |
273 | ||
8a06151a | 274 | Def_Id : Entity_Id; |
d2d4b355 | 275 | pragma Unreferenced (Def_Id); |
b0186f71 | 276 | |
8a06151a | 277 | Prev : Entity_Id; |
b0186f71 | 278 | -- If the expression is a completion, Prev is the entity whose |
d2d4b355 AC |
279 | -- declaration is completed. Def_Id is needed to analyze the spec. |
280 | ||
281 | New_Body : Node_Id; | |
282 | New_Decl : Node_Id; | |
283 | New_Spec : Node_Id; | |
b913199e | 284 | Ret : Node_Id; |
b0186f71 AC |
285 | |
286 | begin | |
287 | -- This is one of the occasions on which we transform the tree during | |
afc8324d | 288 | -- semantic analysis. If this is a completion, transform the expression |
d2b10647 ES |
289 | -- function into an equivalent subprogram body, and analyze it. |
290 | ||
291 | -- Expression functions are inlined unconditionally. The back-end will | |
292 | -- determine whether this is possible. | |
293 | ||
294 | Inline_Processing_Required := True; | |
d2d4b355 AC |
295 | New_Spec := Copy_Separate_Tree (Spec); |
296 | Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); | |
297 | ||
298 | -- If there are previous overloadable entities with the same name, | |
299 | -- check whether any of them is completed by the expression function. | |
300 | ||
8a06151a | 301 | if Present (Prev) and then Is_Overloadable (Prev) then |
d2d4b355 AC |
302 | Def_Id := Analyze_Subprogram_Specification (Spec); |
303 | Prev := Find_Corresponding_Spec (N); | |
304 | end if; | |
b0186f71 | 305 | |
b913199e AC |
306 | Ret := Make_Simple_Return_Statement (LocX, Expression (N)); |
307 | ||
b0186f71 AC |
308 | New_Body := |
309 | Make_Subprogram_Body (Loc, | |
d2d4b355 | 310 | Specification => New_Spec, |
b0186f71 AC |
311 | Declarations => Empty_List, |
312 | Handled_Statement_Sequence => | |
313 | Make_Handled_Sequence_Of_Statements (LocX, | |
b913199e | 314 | Statements => New_List (Ret))); |
b0186f71 | 315 | |
6d7e5c54 AC |
316 | if Present (Prev) and then Ekind (Prev) = E_Generic_Function then |
317 | ||
b0186f71 AC |
318 | -- If the expression completes a generic subprogram, we must create a |
319 | -- separate node for the body, because at instantiation the original | |
320 | -- node of the generic copy must be a generic subprogram body, and | |
321 | -- cannot be a expression function. Otherwise we just rewrite the | |
322 | -- expression with the non-generic body. | |
323 | ||
324 | Insert_After (N, New_Body); | |
325 | Rewrite (N, Make_Null_Statement (Loc)); | |
d2d4b355 | 326 | Set_Has_Completion (Prev, False); |
b0186f71 AC |
327 | Analyze (N); |
328 | Analyze (New_Body); | |
d2b10647 | 329 | Set_Is_Inlined (Prev); |
b0186f71 | 330 | |
6d7e5c54 AC |
331 | elsif Present (Prev) |
332 | and then Comes_From_Source (Prev) | |
333 | then | |
d2d4b355 | 334 | Set_Has_Completion (Prev, False); |
76264f60 AC |
335 | |
336 | -- For navigation purposes, indicate that the function is a body | |
337 | ||
338 | Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True); | |
b0186f71 | 339 | Rewrite (N, New_Body); |
d2b10647 ES |
340 | Analyze (N); |
341 | ||
6d7e5c54 AC |
342 | -- Prev is the previous entity with the same name, but it is can |
343 | -- be an unrelated spec that is not completed by the expression | |
344 | -- function. In that case the relevant entity is the one in the body. | |
345 | -- Not clear that the backend can inline it in this case ??? | |
346 | ||
347 | if Has_Completion (Prev) then | |
348 | Set_Is_Inlined (Prev); | |
349 | else | |
350 | Set_Is_Inlined (Defining_Entity (New_Body)); | |
351 | end if; | |
352 | ||
0b5b2bbc | 353 | -- If this is not a completion, create both a declaration and a body, so |
6d7e5c54 | 354 | -- that the expression can be inlined whenever possible. |
d2b10647 ES |
355 | |
356 | else | |
357 | New_Decl := | |
d2d4b355 | 358 | Make_Subprogram_Declaration (Loc, Specification => Spec); |
804ff4c3 | 359 | |
d2b10647 | 360 | Rewrite (N, New_Decl); |
b0186f71 | 361 | Analyze (N); |
d2b10647 ES |
362 | Set_Is_Inlined (Defining_Entity (New_Decl)); |
363 | ||
6d7e5c54 AC |
364 | -- To prevent premature freeze action, insert the new body at the end |
365 | -- of the current declarations, or at the end of the package spec. | |
b913199e AC |
366 | -- However, resolve usage names now, to prevent spurious visibility |
367 | -- on later entities. | |
6d7e5c54 AC |
368 | |
369 | declare | |
e876c43a AC |
370 | Decls : List_Id := List_Containing (N); |
371 | Par : constant Node_Id := Parent (Decls); | |
b913199e | 372 | Id : constant Entity_Id := Defining_Entity (New_Decl); |
6d7e5c54 AC |
373 | |
374 | begin | |
375 | if Nkind (Par) = N_Package_Specification | |
376 | and then Decls = Visible_Declarations (Par) | |
377 | and then Present (Private_Declarations (Par)) | |
378 | and then not Is_Empty_List (Private_Declarations (Par)) | |
379 | then | |
380 | Decls := Private_Declarations (Par); | |
381 | end if; | |
382 | ||
383 | Insert_After (Last (Decls), New_Body); | |
b913199e AC |
384 | Push_Scope (Id); |
385 | Install_Formals (Id); | |
386 | Preanalyze_Spec_Expression (Expression (Ret), Etype (Id)); | |
387 | End_Scope; | |
6d7e5c54 | 388 | end; |
b0186f71 | 389 | end if; |
0b5b2bbc AC |
390 | |
391 | -- If the return expression is a static constant, we suppress warning | |
392 | -- messages on unused formals, which in most cases will be noise. | |
393 | ||
394 | Set_Is_Trivial_Subprogram (Defining_Entity (New_Body), | |
395 | Is_OK_Static_Expression (Expr)); | |
b0186f71 AC |
396 | end Analyze_Expression_Function; |
397 | ||
ec4867fa ES |
398 | ---------------------------------------- |
399 | -- Analyze_Extended_Return_Statement -- | |
400 | ---------------------------------------- | |
401 | ||
402 | procedure Analyze_Extended_Return_Statement (N : Node_Id) is | |
403 | begin | |
5d37ba92 | 404 | Analyze_Return_Statement (N); |
ec4867fa ES |
405 | end Analyze_Extended_Return_Statement; |
406 | ||
996ae0b0 RK |
407 | ---------------------------- |
408 | -- Analyze_Function_Call -- | |
409 | ---------------------------- | |
410 | ||
411 | procedure Analyze_Function_Call (N : Node_Id) is | |
e24329cd YM |
412 | P : constant Node_Id := Name (N); |
413 | Actuals : constant List_Id := Parameter_Associations (N); | |
414 | Actual : Node_Id; | |
996ae0b0 RK |
415 | |
416 | begin | |
417 | Analyze (P); | |
418 | ||
3e7302c3 AC |
419 | -- A call of the form A.B (X) may be an Ada 2005 call, which is |
420 | -- rewritten as B (A, X). If the rewriting is successful, the call | |
421 | -- has been analyzed and we just return. | |
82c80734 RD |
422 | |
423 | if Nkind (P) = N_Selected_Component | |
424 | and then Name (N) /= P | |
425 | and then Is_Rewrite_Substitution (N) | |
426 | and then Present (Etype (N)) | |
427 | then | |
428 | return; | |
429 | end if; | |
430 | ||
996ae0b0 RK |
431 | -- If error analyzing name, then set Any_Type as result type and return |
432 | ||
433 | if Etype (P) = Any_Type then | |
434 | Set_Etype (N, Any_Type); | |
435 | return; | |
436 | end if; | |
437 | ||
438 | -- Otherwise analyze the parameters | |
439 | ||
e24329cd YM |
440 | if Present (Actuals) then |
441 | Actual := First (Actuals); | |
996ae0b0 RK |
442 | while Present (Actual) loop |
443 | Analyze (Actual); | |
444 | Check_Parameterless_Call (Actual); | |
445 | Next (Actual); | |
446 | end loop; | |
447 | end if; | |
448 | ||
449 | Analyze_Call (N); | |
996ae0b0 RK |
450 | end Analyze_Function_Call; |
451 | ||
ec4867fa ES |
452 | ----------------------------- |
453 | -- Analyze_Function_Return -- | |
454 | ----------------------------- | |
455 | ||
456 | procedure Analyze_Function_Return (N : Node_Id) is | |
457 | Loc : constant Source_Ptr := Sloc (N); | |
458 | Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); | |
459 | Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); | |
460 | ||
5d37ba92 | 461 | R_Type : constant Entity_Id := Etype (Scope_Id); |
ec4867fa ES |
462 | -- Function result subtype |
463 | ||
464 | procedure Check_Limited_Return (Expr : Node_Id); | |
465 | -- Check the appropriate (Ada 95 or Ada 2005) rules for returning | |
466 | -- limited types. Used only for simple return statements. | |
467 | -- Expr is the expression returned. | |
468 | ||
469 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); | |
470 | -- Check that the return_subtype_indication properly matches the result | |
471 | -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). | |
472 | ||
473 | -------------------------- | |
474 | -- Check_Limited_Return -- | |
475 | -------------------------- | |
476 | ||
477 | procedure Check_Limited_Return (Expr : Node_Id) is | |
478 | begin | |
479 | -- Ada 2005 (AI-318-02): Return-by-reference types have been | |
480 | -- removed and replaced by anonymous access results. This is an | |
481 | -- incompatibility with Ada 95. Not clear whether this should be | |
482 | -- enforced yet or perhaps controllable with special switch. ??? | |
483 | ||
ce72a9a3 AC |
484 | -- A limited interface that is not immutably limited is OK. |
485 | ||
486 | if Is_Limited_Interface (R_Type) | |
487 | and then | |
488 | not (Is_Task_Interface (R_Type) | |
489 | or else Is_Protected_Interface (R_Type) | |
490 | or else Is_Synchronized_Interface (R_Type)) | |
491 | then | |
492 | null; | |
493 | ||
494 | elsif Is_Limited_Type (R_Type) | |
495 | and then not Is_Interface (R_Type) | |
ec4867fa ES |
496 | and then Comes_From_Source (N) |
497 | and then not In_Instance_Body | |
2a31c32b | 498 | and then not OK_For_Limited_Init_In_05 (R_Type, Expr) |
ec4867fa ES |
499 | then |
500 | -- Error in Ada 2005 | |
501 | ||
0791fbe9 | 502 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
503 | and then not Debug_Flag_Dot_L |
504 | and then not GNAT_Mode | |
505 | then | |
506 | Error_Msg_N | |
507 | ("(Ada 2005) cannot copy object of a limited type " & | |
5d37ba92 | 508 | "(RM-2005 6.5(5.5/2))", Expr); |
e0ae93e2 | 509 | |
40f07b4b | 510 | if Is_Immutably_Limited_Type (R_Type) then |
ec4867fa ES |
511 | Error_Msg_N |
512 | ("\return by reference not permitted in Ada 2005", Expr); | |
513 | end if; | |
514 | ||
515 | -- Warn in Ada 95 mode, to give folks a heads up about this | |
516 | -- incompatibility. | |
517 | ||
518 | -- In GNAT mode, this is just a warning, to allow it to be | |
519 | -- evilly turned off. Otherwise it is a real error. | |
520 | ||
9694c039 AC |
521 | -- In a generic context, simplify the warning because it makes |
522 | -- no sense to discuss pass-by-reference or copy. | |
523 | ||
ec4867fa | 524 | elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then |
9694c039 AC |
525 | if Inside_A_Generic then |
526 | Error_Msg_N | |
885c4871 | 527 | ("return of limited object not permitted in Ada 2005 " |
20261dc1 | 528 | & "(RM-2005 6.5(5.5/2))?", Expr); |
9694c039 AC |
529 | |
530 | elsif Is_Immutably_Limited_Type (R_Type) then | |
ec4867fa | 531 | Error_Msg_N |
20261dc1 AC |
532 | ("return by reference not permitted in Ada 2005 " |
533 | & "(RM-2005 6.5(5.5/2))?", Expr); | |
ec4867fa ES |
534 | else |
535 | Error_Msg_N | |
20261dc1 AC |
536 | ("cannot copy object of a limited type in Ada 2005 " |
537 | & "(RM-2005 6.5(5.5/2))?", Expr); | |
ec4867fa ES |
538 | end if; |
539 | ||
540 | -- Ada 95 mode, compatibility warnings disabled | |
541 | ||
542 | else | |
543 | return; -- skip continuation messages below | |
544 | end if; | |
545 | ||
9694c039 AC |
546 | if not Inside_A_Generic then |
547 | Error_Msg_N | |
548 | ("\consider switching to return of access type", Expr); | |
549 | Explain_Limited_Type (R_Type, Expr); | |
550 | end if; | |
ec4867fa ES |
551 | end if; |
552 | end Check_Limited_Return; | |
553 | ||
554 | ------------------------------------- | |
555 | -- Check_Return_Subtype_Indication -- | |
556 | ------------------------------------- | |
557 | ||
558 | procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is | |
7665e4bd AC |
559 | Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); |
560 | ||
561 | R_Stm_Type : constant Entity_Id := Etype (Return_Obj); | |
562 | -- Subtype given in the extended return statement (must match R_Type) | |
ec4867fa ES |
563 | |
564 | Subtype_Ind : constant Node_Id := | |
565 | Object_Definition (Original_Node (Obj_Decl)); | |
566 | ||
567 | R_Type_Is_Anon_Access : | |
568 | constant Boolean := | |
569 | Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type | |
570 | or else | |
571 | Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type | |
572 | or else | |
573 | Ekind (R_Type) = E_Anonymous_Access_Type; | |
574 | -- True if return type of the function is an anonymous access type | |
575 | -- Can't we make Is_Anonymous_Access_Type in einfo ??? | |
576 | ||
577 | R_Stm_Type_Is_Anon_Access : | |
578 | constant Boolean := | |
0a36105d | 579 | Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type |
ec4867fa | 580 | or else |
0a36105d | 581 | Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type |
ec4867fa | 582 | or else |
0a36105d | 583 | Ekind (R_Stm_Type) = E_Anonymous_Access_Type; |
ec4867fa ES |
584 | -- True if type of the return object is an anonymous access type |
585 | ||
586 | begin | |
7665e4bd | 587 | -- First, avoid cascaded errors |
ec4867fa ES |
588 | |
589 | if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then | |
590 | return; | |
591 | end if; | |
592 | ||
593 | -- "return access T" case; check that the return statement also has | |
594 | -- "access T", and that the subtypes statically match: | |
53cf4600 | 595 | -- if this is an access to subprogram the signatures must match. |
ec4867fa ES |
596 | |
597 | if R_Type_Is_Anon_Access then | |
598 | if R_Stm_Type_Is_Anon_Access then | |
53cf4600 ES |
599 | if |
600 | Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type | |
0a36105d | 601 | then |
53cf4600 ES |
602 | if Base_Type (Designated_Type (R_Stm_Type)) /= |
603 | Base_Type (Designated_Type (R_Type)) | |
604 | or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) | |
605 | then | |
606 | Error_Msg_N | |
607 | ("subtype must statically match function result subtype", | |
608 | Subtype_Mark (Subtype_Ind)); | |
609 | end if; | |
610 | ||
611 | else | |
612 | -- For two anonymous access to subprogram types, the | |
613 | -- types themselves must be type conformant. | |
614 | ||
615 | if not Conforming_Types | |
616 | (R_Stm_Type, R_Type, Fully_Conformant) | |
617 | then | |
618 | Error_Msg_N | |
619 | ("subtype must statically match function result subtype", | |
620 | Subtype_Ind); | |
621 | end if; | |
ec4867fa | 622 | end if; |
0a36105d | 623 | |
ec4867fa ES |
624 | else |
625 | Error_Msg_N ("must use anonymous access type", Subtype_Ind); | |
626 | end if; | |
627 | ||
6cce2156 GD |
628 | -- If the return object is of an anonymous access type, then report |
629 | -- an error if the function's result type is not also anonymous. | |
630 | ||
631 | elsif R_Stm_Type_Is_Anon_Access | |
632 | and then not R_Type_Is_Anon_Access | |
633 | then | |
634 | Error_Msg_N ("anonymous access not allowed for function with " & | |
635 | "named access result", Subtype_Ind); | |
636 | ||
81d93365 AC |
637 | -- Subtype indication case: check that the return object's type is |
638 | -- covered by the result type, and that the subtypes statically match | |
639 | -- when the result subtype is constrained. Also handle record types | |
640 | -- with unknown discriminants for which we have built the underlying | |
641 | -- record view. Coverage is needed to allow specific-type return | |
642 | -- objects when the result type is class-wide (see AI05-32). | |
643 | ||
644 | elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) | |
9013065b | 645 | or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) |
212863c0 AC |
646 | and then |
647 | Covers | |
648 | (Base_Type (R_Type), | |
649 | Underlying_Record_View (Base_Type (R_Stm_Type)))) | |
9013065b AC |
650 | then |
651 | -- A null exclusion may be present on the return type, on the | |
652 | -- function specification, on the object declaration or on the | |
653 | -- subtype itself. | |
ec4867fa | 654 | |
21d27997 RD |
655 | if Is_Access_Type (R_Type) |
656 | and then | |
657 | (Can_Never_Be_Null (R_Type) | |
658 | or else Null_Exclusion_Present (Parent (Scope_Id))) /= | |
659 | Can_Never_Be_Null (R_Stm_Type) | |
660 | then | |
661 | Error_Msg_N | |
662 | ("subtype must statically match function result subtype", | |
663 | Subtype_Ind); | |
664 | end if; | |
665 | ||
105b5e65 | 666 | -- AI05-103: for elementary types, subtypes must statically match |
8779dffa AC |
667 | |
668 | if Is_Constrained (R_Type) | |
669 | or else Is_Access_Type (R_Type) | |
670 | then | |
ec4867fa ES |
671 | if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then |
672 | Error_Msg_N | |
0a36105d JM |
673 | ("subtype must statically match function result subtype", |
674 | Subtype_Ind); | |
ec4867fa ES |
675 | end if; |
676 | end if; | |
677 | ||
ff7139c3 AC |
678 | elsif Etype (Base_Type (R_Type)) = R_Stm_Type |
679 | and then Is_Null_Extension (Base_Type (R_Type)) | |
680 | then | |
681 | null; | |
682 | ||
ec4867fa ES |
683 | else |
684 | Error_Msg_N | |
685 | ("wrong type for return_subtype_indication", Subtype_Ind); | |
686 | end if; | |
687 | end Check_Return_Subtype_Indication; | |
688 | ||
689 | --------------------- | |
690 | -- Local Variables -- | |
691 | --------------------- | |
692 | ||
693 | Expr : Node_Id; | |
694 | ||
695 | -- Start of processing for Analyze_Function_Return | |
696 | ||
697 | begin | |
698 | Set_Return_Present (Scope_Id); | |
699 | ||
5d37ba92 | 700 | if Nkind (N) = N_Simple_Return_Statement then |
ec4867fa | 701 | Expr := Expression (N); |
4ee646da | 702 | |
e917aec2 RD |
703 | -- Guard against a malformed expression. The parser may have tried to |
704 | -- recover but the node is not analyzable. | |
4ee646da AC |
705 | |
706 | if Nkind (Expr) = N_Error then | |
707 | Set_Etype (Expr, Any_Type); | |
708 | Expander_Mode_Save_And_Set (False); | |
709 | return; | |
710 | ||
711 | else | |
0180fd26 AC |
712 | -- The resolution of a controlled [extension] aggregate associated |
713 | -- with a return statement creates a temporary which needs to be | |
714 | -- finalized on function exit. Wrap the return statement inside a | |
715 | -- block so that the finalization machinery can detect this case. | |
716 | -- This early expansion is done only when the return statement is | |
717 | -- not part of a handled sequence of statements. | |
718 | ||
719 | if Nkind_In (Expr, N_Aggregate, | |
720 | N_Extension_Aggregate) | |
721 | and then Needs_Finalization (R_Type) | |
722 | and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements | |
723 | then | |
724 | Rewrite (N, | |
725 | Make_Block_Statement (Loc, | |
726 | Handled_Statement_Sequence => | |
727 | Make_Handled_Sequence_Of_Statements (Loc, | |
728 | Statements => New_List (Relocate_Node (N))))); | |
729 | ||
730 | Analyze (N); | |
731 | return; | |
732 | end if; | |
733 | ||
4ee646da AC |
734 | Analyze_And_Resolve (Expr, R_Type); |
735 | Check_Limited_Return (Expr); | |
736 | end if; | |
ec4867fa | 737 | |
ad05f2e9 | 738 | -- RETURN only allowed in SPARK as the last statement in function |
607d0635 | 739 | |
fe5d3068 | 740 | if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements |
607d0635 AC |
741 | and then |
742 | (Nkind (Parent (Parent (N))) /= N_Subprogram_Body | |
8d606a78 | 743 | or else Present (Next (N))) |
607d0635 | 744 | then |
2ba431e5 | 745 | Check_SPARK_Restriction |
fe5d3068 | 746 | ("RETURN should be the last statement in function", N); |
607d0635 AC |
747 | end if; |
748 | ||
ec4867fa | 749 | else |
2ba431e5 | 750 | Check_SPARK_Restriction ("extended RETURN is not allowed", N); |
607d0635 | 751 | |
ec4867fa ES |
752 | -- Analyze parts specific to extended_return_statement: |
753 | ||
754 | declare | |
755 | Obj_Decl : constant Node_Id := | |
756 | Last (Return_Object_Declarations (N)); | |
757 | ||
758 | HSS : constant Node_Id := Handled_Statement_Sequence (N); | |
759 | ||
760 | begin | |
761 | Expr := Expression (Obj_Decl); | |
762 | ||
763 | -- Note: The check for OK_For_Limited_Init will happen in | |
764 | -- Analyze_Object_Declaration; we treat it as a normal | |
765 | -- object declaration. | |
766 | ||
cd1c668b | 767 | Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); |
ec4867fa ES |
768 | Analyze (Obj_Decl); |
769 | ||
ec4867fa ES |
770 | Check_Return_Subtype_Indication (Obj_Decl); |
771 | ||
772 | if Present (HSS) then | |
773 | Analyze (HSS); | |
774 | ||
775 | if Present (Exception_Handlers (HSS)) then | |
776 | ||
777 | -- ???Has_Nested_Block_With_Handler needs to be set. | |
778 | -- Probably by creating an actual N_Block_Statement. | |
779 | -- Probably in Expand. | |
780 | ||
781 | null; | |
782 | end if; | |
783 | end if; | |
784 | ||
9337aa0a AC |
785 | -- Mark the return object as referenced, since the return is an |
786 | -- implicit reference of the object. | |
787 | ||
788 | Set_Referenced (Defining_Identifier (Obj_Decl)); | |
789 | ||
ec4867fa ES |
790 | Check_References (Stm_Entity); |
791 | end; | |
792 | end if; | |
793 | ||
21d27997 | 794 | -- Case of Expr present |
5d37ba92 | 795 | |
ec4867fa | 796 | if Present (Expr) |
21d27997 RD |
797 | |
798 | -- Defend against previous errors | |
799 | ||
800 | and then Nkind (Expr) /= N_Empty | |
5d37ba92 | 801 | and then Present (Etype (Expr)) |
ec4867fa | 802 | then |
5d37ba92 ES |
803 | -- Apply constraint check. Note that this is done before the implicit |
804 | -- conversion of the expression done for anonymous access types to | |
f3d57416 | 805 | -- ensure correct generation of the null-excluding check associated |
5d37ba92 ES |
806 | -- with null-excluding expressions found in return statements. |
807 | ||
808 | Apply_Constraint_Check (Expr, R_Type); | |
809 | ||
810 | -- Ada 2005 (AI-318-02): When the result type is an anonymous access | |
811 | -- type, apply an implicit conversion of the expression to that type | |
812 | -- to force appropriate static and run-time accessibility checks. | |
ec4867fa | 813 | |
0791fbe9 | 814 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
815 | and then Ekind (R_Type) = E_Anonymous_Access_Type |
816 | then | |
817 | Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); | |
818 | Analyze_And_Resolve (Expr, R_Type); | |
819 | end if; | |
820 | ||
21d27997 RD |
821 | -- If the result type is class-wide, then check that the return |
822 | -- expression's type is not declared at a deeper level than the | |
823 | -- function (RM05-6.5(5.6/2)). | |
824 | ||
0791fbe9 | 825 | if Ada_Version >= Ada_2005 |
21d27997 RD |
826 | and then Is_Class_Wide_Type (R_Type) |
827 | then | |
828 | if Type_Access_Level (Etype (Expr)) > | |
829 | Subprogram_Access_Level (Scope_Id) | |
830 | then | |
831 | Error_Msg_N | |
832 | ("level of return expression type is deeper than " & | |
833 | "class-wide function!", Expr); | |
834 | end if; | |
835 | end if; | |
836 | ||
4755cce9 JM |
837 | -- Check incorrect use of dynamically tagged expression |
838 | ||
839 | if Is_Tagged_Type (R_Type) then | |
840 | Check_Dynamically_Tagged_Expression | |
841 | (Expr => Expr, | |
842 | Typ => R_Type, | |
843 | Related_Nod => N); | |
ec4867fa ES |
844 | end if; |
845 | ||
ec4867fa ES |
846 | -- ??? A real run-time accessibility check is needed in cases |
847 | -- involving dereferences of access parameters. For now we just | |
848 | -- check the static cases. | |
849 | ||
0791fbe9 | 850 | if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) |
40f07b4b | 851 | and then Is_Immutably_Limited_Type (Etype (Scope_Id)) |
ec4867fa ES |
852 | and then Object_Access_Level (Expr) > |
853 | Subprogram_Access_Level (Scope_Id) | |
854 | then | |
ec4867fa | 855 | |
9694c039 AC |
856 | -- Suppress the message in a generic, where the rewriting |
857 | -- is irrelevant. | |
858 | ||
859 | if Inside_A_Generic then | |
860 | null; | |
861 | ||
862 | else | |
863 | Rewrite (N, | |
864 | Make_Raise_Program_Error (Loc, | |
865 | Reason => PE_Accessibility_Check_Failed)); | |
866 | Analyze (N); | |
867 | ||
868 | Error_Msg_N | |
869 | ("cannot return a local value by reference?", N); | |
870 | Error_Msg_NE | |
871 | ("\& will be raised at run time?", | |
872 | N, Standard_Program_Error); | |
873 | end if; | |
ec4867fa | 874 | end if; |
5d37ba92 ES |
875 | |
876 | if Known_Null (Expr) | |
877 | and then Nkind (Parent (Scope_Id)) = N_Function_Specification | |
878 | and then Null_Exclusion_Present (Parent (Scope_Id)) | |
879 | then | |
880 | Apply_Compile_Time_Constraint_Error | |
881 | (N => Expr, | |
882 | Msg => "(Ada 2005) null not allowed for " | |
883 | & "null-excluding return?", | |
884 | Reason => CE_Null_Not_Allowed); | |
885 | end if; | |
cd5a9750 AC |
886 | |
887 | -- Apply checks suggested by AI05-0144 (dangerous order dependence) | |
cd5a9750 | 888 | |
1e194575 | 889 | Check_Order_Dependence; |
ec4867fa ES |
890 | end if; |
891 | end Analyze_Function_Return; | |
892 | ||
996ae0b0 RK |
893 | ------------------------------------- |
894 | -- Analyze_Generic_Subprogram_Body -- | |
895 | ------------------------------------- | |
896 | ||
897 | procedure Analyze_Generic_Subprogram_Body | |
898 | (N : Node_Id; | |
899 | Gen_Id : Entity_Id) | |
900 | is | |
fbf5a39b | 901 | Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); |
996ae0b0 | 902 | Kind : constant Entity_Kind := Ekind (Gen_Id); |
fbf5a39b | 903 | Body_Id : Entity_Id; |
996ae0b0 | 904 | New_N : Node_Id; |
fbf5a39b | 905 | Spec : Node_Id; |
996ae0b0 RK |
906 | |
907 | begin | |
82c80734 RD |
908 | -- Copy body and disable expansion while analyzing the generic For a |
909 | -- stub, do not copy the stub (which would load the proper body), this | |
910 | -- will be done when the proper body is analyzed. | |
996ae0b0 RK |
911 | |
912 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
913 | New_N := Copy_Generic_Node (N, Empty, Instantiating => False); | |
914 | Rewrite (N, New_N); | |
915 | Start_Generic; | |
916 | end if; | |
917 | ||
918 | Spec := Specification (N); | |
919 | ||
920 | -- Within the body of the generic, the subprogram is callable, and | |
921 | -- behaves like the corresponding non-generic unit. | |
922 | ||
fbf5a39b | 923 | Body_Id := Defining_Entity (Spec); |
996ae0b0 RK |
924 | |
925 | if Kind = E_Generic_Procedure | |
926 | and then Nkind (Spec) /= N_Procedure_Specification | |
927 | then | |
fbf5a39b | 928 | Error_Msg_N ("invalid body for generic procedure ", Body_Id); |
996ae0b0 RK |
929 | return; |
930 | ||
931 | elsif Kind = E_Generic_Function | |
932 | and then Nkind (Spec) /= N_Function_Specification | |
933 | then | |
fbf5a39b | 934 | Error_Msg_N ("invalid body for generic function ", Body_Id); |
996ae0b0 RK |
935 | return; |
936 | end if; | |
937 | ||
fbf5a39b | 938 | Set_Corresponding_Body (Gen_Decl, Body_Id); |
996ae0b0 RK |
939 | |
940 | if Has_Completion (Gen_Id) | |
941 | and then Nkind (Parent (N)) /= N_Subunit | |
942 | then | |
943 | Error_Msg_N ("duplicate generic body", N); | |
944 | return; | |
945 | else | |
946 | Set_Has_Completion (Gen_Id); | |
947 | end if; | |
948 | ||
949 | if Nkind (N) = N_Subprogram_Body_Stub then | |
950 | Set_Ekind (Defining_Entity (Specification (N)), Kind); | |
951 | else | |
952 | Set_Corresponding_Spec (N, Gen_Id); | |
953 | end if; | |
954 | ||
955 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
956 | Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); | |
957 | end if; | |
958 | ||
959 | -- Make generic parameters immediately visible in the body. They are | |
960 | -- needed to process the formals declarations. Then make the formals | |
961 | -- visible in a separate step. | |
962 | ||
0a36105d | 963 | Push_Scope (Gen_Id); |
996ae0b0 RK |
964 | |
965 | declare | |
966 | E : Entity_Id; | |
967 | First_Ent : Entity_Id; | |
968 | ||
969 | begin | |
970 | First_Ent := First_Entity (Gen_Id); | |
971 | ||
972 | E := First_Ent; | |
973 | while Present (E) and then not Is_Formal (E) loop | |
974 | Install_Entity (E); | |
975 | Next_Entity (E); | |
976 | end loop; | |
977 | ||
978 | Set_Use (Generic_Formal_Declarations (Gen_Decl)); | |
979 | ||
980 | -- Now generic formals are visible, and the specification can be | |
981 | -- analyzed, for subsequent conformance check. | |
982 | ||
fbf5a39b | 983 | Body_Id := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 984 | |
fbf5a39b | 985 | -- Make formal parameters visible |
996ae0b0 RK |
986 | |
987 | if Present (E) then | |
988 | ||
fbf5a39b AC |
989 | -- E is the first formal parameter, we loop through the formals |
990 | -- installing them so that they will be visible. | |
996ae0b0 RK |
991 | |
992 | Set_First_Entity (Gen_Id, E); | |
996ae0b0 RK |
993 | while Present (E) loop |
994 | Install_Entity (E); | |
995 | Next_Formal (E); | |
996 | end loop; | |
997 | end if; | |
998 | ||
e895b435 | 999 | -- Visible generic entity is callable within its own body |
996ae0b0 | 1000 | |
ec4867fa ES |
1001 | Set_Ekind (Gen_Id, Ekind (Body_Id)); |
1002 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
1003 | Set_Convention (Body_Id, Convention (Gen_Id)); | |
1004 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); | |
1005 | Set_Scope (Body_Id, Scope (Gen_Id)); | |
fbf5a39b AC |
1006 | Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); |
1007 | ||
1008 | if Nkind (N) = N_Subprogram_Body_Stub then | |
1009 | ||
e895b435 | 1010 | -- No body to analyze, so restore state of generic unit |
fbf5a39b AC |
1011 | |
1012 | Set_Ekind (Gen_Id, Kind); | |
1013 | Set_Ekind (Body_Id, Kind); | |
1014 | ||
1015 | if Present (First_Ent) then | |
1016 | Set_First_Entity (Gen_Id, First_Ent); | |
1017 | end if; | |
1018 | ||
1019 | End_Scope; | |
1020 | return; | |
1021 | end if; | |
996ae0b0 | 1022 | |
82c80734 RD |
1023 | -- If this is a compilation unit, it must be made visible explicitly, |
1024 | -- because the compilation of the declaration, unlike other library | |
1025 | -- unit declarations, does not. If it is not a unit, the following | |
1026 | -- is redundant but harmless. | |
996ae0b0 RK |
1027 | |
1028 | Set_Is_Immediately_Visible (Gen_Id); | |
fbf5a39b | 1029 | Reference_Body_Formals (Gen_Id, Body_Id); |
996ae0b0 | 1030 | |
ec4867fa ES |
1031 | if Is_Child_Unit (Gen_Id) then |
1032 | Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); | |
1033 | end if; | |
1034 | ||
996ae0b0 | 1035 | Set_Actual_Subtypes (N, Current_Scope); |
483361a6 AC |
1036 | |
1037 | -- Deal with preconditions and postconditions. In formal verification | |
1038 | -- mode, we keep pre- and postconditions attached to entities rather | |
1039 | -- than inserted in the code, in order to facilitate a distinct | |
1040 | -- treatment for them. | |
1041 | ||
56812278 | 1042 | if not Alfa_Mode then |
483361a6 AC |
1043 | Process_PPCs (N, Gen_Id, Body_Id); |
1044 | end if; | |
0dabde3a ES |
1045 | |
1046 | -- If the generic unit carries pre- or post-conditions, copy them | |
1047 | -- to the original generic tree, so that they are properly added | |
1048 | -- to any instantiation. | |
1049 | ||
1050 | declare | |
1051 | Orig : constant Node_Id := Original_Node (N); | |
1052 | Cond : Node_Id; | |
1053 | ||
1054 | begin | |
1055 | Cond := First (Declarations (N)); | |
1056 | while Present (Cond) loop | |
1057 | if Nkind (Cond) = N_Pragma | |
1058 | and then Pragma_Name (Cond) = Name_Check | |
1059 | then | |
1060 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
1061 | ||
1062 | elsif Nkind (Cond) = N_Pragma | |
1063 | and then Pragma_Name (Cond) = Name_Postcondition | |
1064 | then | |
1065 | Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id)); | |
1066 | Prepend (New_Copy_Tree (Cond), Declarations (Orig)); | |
1067 | else | |
1068 | exit; | |
1069 | end if; | |
1070 | ||
1071 | Next (Cond); | |
1072 | end loop; | |
1073 | end; | |
1074 | ||
996ae0b0 RK |
1075 | Analyze_Declarations (Declarations (N)); |
1076 | Check_Completion; | |
1077 | Analyze (Handled_Statement_Sequence (N)); | |
1078 | ||
1079 | Save_Global_References (Original_Node (N)); | |
1080 | ||
82c80734 RD |
1081 | -- Prior to exiting the scope, include generic formals again (if any |
1082 | -- are present) in the set of local entities. | |
996ae0b0 RK |
1083 | |
1084 | if Present (First_Ent) then | |
1085 | Set_First_Entity (Gen_Id, First_Ent); | |
1086 | end if; | |
1087 | ||
fbf5a39b | 1088 | Check_References (Gen_Id); |
996ae0b0 RK |
1089 | end; |
1090 | ||
e6f69614 | 1091 | Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); |
996ae0b0 RK |
1092 | End_Scope; |
1093 | Check_Subprogram_Order (N); | |
1094 | ||
e895b435 | 1095 | -- Outside of its body, unit is generic again |
996ae0b0 RK |
1096 | |
1097 | Set_Ekind (Gen_Id, Kind); | |
fbf5a39b | 1098 | Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); |
5d37ba92 ES |
1099 | |
1100 | if Style_Check then | |
1101 | Style.Check_Identifier (Body_Id, Gen_Id); | |
1102 | end if; | |
13d923cc | 1103 | |
996ae0b0 | 1104 | End_Generic; |
996ae0b0 RK |
1105 | end Analyze_Generic_Subprogram_Body; |
1106 | ||
1107 | ----------------------------- | |
1108 | -- Analyze_Operator_Symbol -- | |
1109 | ----------------------------- | |
1110 | ||
82c80734 RD |
1111 | -- An operator symbol such as "+" or "and" may appear in context where the |
1112 | -- literal denotes an entity name, such as "+"(x, y) or in context when it | |
1113 | -- is just a string, as in (conjunction = "or"). In these cases the parser | |
1114 | -- generates this node, and the semantics does the disambiguation. Other | |
1115 | -- such case are actuals in an instantiation, the generic unit in an | |
1116 | -- instantiation, and pragma arguments. | |
996ae0b0 RK |
1117 | |
1118 | procedure Analyze_Operator_Symbol (N : Node_Id) is | |
1119 | Par : constant Node_Id := Parent (N); | |
1120 | ||
1121 | begin | |
800621e0 RD |
1122 | if (Nkind (Par) = N_Function_Call |
1123 | and then N = Name (Par)) | |
996ae0b0 | 1124 | or else Nkind (Par) = N_Function_Instantiation |
800621e0 RD |
1125 | or else (Nkind (Par) = N_Indexed_Component |
1126 | and then N = Prefix (Par)) | |
996ae0b0 RK |
1127 | or else (Nkind (Par) = N_Pragma_Argument_Association |
1128 | and then not Is_Pragma_String_Literal (Par)) | |
1129 | or else Nkind (Par) = N_Subprogram_Renaming_Declaration | |
800621e0 RD |
1130 | or else (Nkind (Par) = N_Attribute_Reference |
1131 | and then Attribute_Name (Par) /= Name_Value) | |
996ae0b0 RK |
1132 | then |
1133 | Find_Direct_Name (N); | |
1134 | ||
1135 | else | |
1136 | Change_Operator_Symbol_To_String_Literal (N); | |
1137 | Analyze (N); | |
1138 | end if; | |
1139 | end Analyze_Operator_Symbol; | |
1140 | ||
1141 | ----------------------------------- | |
1142 | -- Analyze_Parameter_Association -- | |
1143 | ----------------------------------- | |
1144 | ||
1145 | procedure Analyze_Parameter_Association (N : Node_Id) is | |
1146 | begin | |
1147 | Analyze (Explicit_Actual_Parameter (N)); | |
1148 | end Analyze_Parameter_Association; | |
1149 | ||
1150 | ---------------------------- | |
1151 | -- Analyze_Procedure_Call -- | |
1152 | ---------------------------- | |
1153 | ||
1154 | procedure Analyze_Procedure_Call (N : Node_Id) is | |
1155 | Loc : constant Source_Ptr := Sloc (N); | |
1156 | P : constant Node_Id := Name (N); | |
1157 | Actuals : constant List_Id := Parameter_Associations (N); | |
1158 | Actual : Node_Id; | |
1159 | New_N : Node_Id; | |
1160 | ||
1161 | procedure Analyze_Call_And_Resolve; | |
1162 | -- Do Analyze and Resolve calls for procedure call | |
cd5a9750 | 1163 | -- At end, check illegal order dependence. |
996ae0b0 | 1164 | |
fbf5a39b AC |
1165 | ------------------------------ |
1166 | -- Analyze_Call_And_Resolve -- | |
1167 | ------------------------------ | |
1168 | ||
996ae0b0 RK |
1169 | procedure Analyze_Call_And_Resolve is |
1170 | begin | |
1171 | if Nkind (N) = N_Procedure_Call_Statement then | |
1172 | Analyze_Call (N); | |
1173 | Resolve (N, Standard_Void_Type); | |
cd5a9750 | 1174 | |
1e194575 | 1175 | -- Apply checks suggested by AI05-0144 |
cd5a9750 | 1176 | |
1e194575 | 1177 | Check_Order_Dependence; |
cd5a9750 | 1178 | |
996ae0b0 RK |
1179 | else |
1180 | Analyze (N); | |
1181 | end if; | |
1182 | end Analyze_Call_And_Resolve; | |
1183 | ||
1184 | -- Start of processing for Analyze_Procedure_Call | |
1185 | ||
1186 | begin | |
1187 | -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote | |
1188 | -- a procedure call or an entry call. The prefix may denote an access | |
1189 | -- to subprogram type, in which case an implicit dereference applies. | |
f3d57416 | 1190 | -- If the prefix is an indexed component (without implicit dereference) |
996ae0b0 RK |
1191 | -- then the construct denotes a call to a member of an entire family. |
1192 | -- If the prefix is a simple name, it may still denote a call to a | |
1193 | -- parameterless member of an entry family. Resolution of these various | |
1194 | -- interpretations is delicate. | |
1195 | ||
1196 | Analyze (P); | |
1197 | ||
758c442c GD |
1198 | -- If this is a call of the form Obj.Op, the call may have been |
1199 | -- analyzed and possibly rewritten into a block, in which case | |
1200 | -- we are done. | |
1201 | ||
1202 | if Analyzed (N) then | |
1203 | return; | |
1204 | end if; | |
1205 | ||
7415029d AC |
1206 | -- If there is an error analyzing the name (which may have been |
1207 | -- rewritten if the original call was in prefix notation) then error | |
1208 | -- has been emitted already, mark node and return. | |
996ae0b0 | 1209 | |
7415029d AC |
1210 | if Error_Posted (N) |
1211 | or else Etype (Name (N)) = Any_Type | |
1212 | then | |
996ae0b0 RK |
1213 | Set_Etype (N, Any_Type); |
1214 | return; | |
1215 | end if; | |
1216 | ||
1217 | -- Otherwise analyze the parameters | |
1218 | ||
1219 | if Present (Actuals) then | |
1220 | Actual := First (Actuals); | |
1221 | ||
1222 | while Present (Actual) loop | |
1223 | Analyze (Actual); | |
1224 | Check_Parameterless_Call (Actual); | |
1225 | Next (Actual); | |
1226 | end loop; | |
1227 | end if; | |
1228 | ||
0bfc9a64 | 1229 | -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls |
996ae0b0 RK |
1230 | |
1231 | if Nkind (P) = N_Attribute_Reference | |
1232 | and then (Attribute_Name (P) = Name_Elab_Spec | |
0bfc9a64 AC |
1233 | or else Attribute_Name (P) = Name_Elab_Body |
1234 | or else Attribute_Name (P) = Name_Elab_Subp_Body) | |
996ae0b0 RK |
1235 | then |
1236 | if Present (Actuals) then | |
1237 | Error_Msg_N | |
1238 | ("no parameters allowed for this call", First (Actuals)); | |
1239 | return; | |
1240 | end if; | |
1241 | ||
1242 | Set_Etype (N, Standard_Void_Type); | |
1243 | Set_Analyzed (N); | |
1244 | ||
1245 | elsif Is_Entity_Name (P) | |
1246 | and then Is_Record_Type (Etype (Entity (P))) | |
1247 | and then Remote_AST_I_Dereference (P) | |
1248 | then | |
1249 | return; | |
1250 | ||
1251 | elsif Is_Entity_Name (P) | |
1252 | and then Ekind (Entity (P)) /= E_Entry_Family | |
1253 | then | |
1254 | if Is_Access_Type (Etype (P)) | |
1255 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1256 | and then No (Actuals) | |
1257 | and then Comes_From_Source (N) | |
1258 | then | |
ed2233dc | 1259 | Error_Msg_N ("missing explicit dereference in call", N); |
996ae0b0 RK |
1260 | end if; |
1261 | ||
1262 | Analyze_Call_And_Resolve; | |
1263 | ||
1264 | -- If the prefix is the simple name of an entry family, this is | |
1265 | -- a parameterless call from within the task body itself. | |
1266 | ||
1267 | elsif Is_Entity_Name (P) | |
1268 | and then Nkind (P) = N_Identifier | |
1269 | and then Ekind (Entity (P)) = E_Entry_Family | |
1270 | and then Present (Actuals) | |
1271 | and then No (Next (First (Actuals))) | |
1272 | then | |
82c80734 RD |
1273 | -- Can be call to parameterless entry family. What appears to be the |
1274 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1275 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1276 | -- transformation. |
1277 | ||
1278 | New_N := | |
1279 | Make_Indexed_Component (Loc, | |
1280 | Prefix => | |
1281 | Make_Selected_Component (Loc, | |
1282 | Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc), | |
1283 | Selector_Name => New_Occurrence_Of (Entity (P), Loc)), | |
1284 | Expressions => Actuals); | |
1285 | Set_Name (N, New_N); | |
1286 | Set_Etype (New_N, Standard_Void_Type); | |
1287 | Set_Parameter_Associations (N, No_List); | |
1288 | Analyze_Call_And_Resolve; | |
1289 | ||
1290 | elsif Nkind (P) = N_Explicit_Dereference then | |
1291 | if Ekind (Etype (P)) = E_Subprogram_Type then | |
1292 | Analyze_Call_And_Resolve; | |
1293 | else | |
1294 | Error_Msg_N ("expect access to procedure in call", P); | |
1295 | end if; | |
1296 | ||
82c80734 RD |
1297 | -- The name can be a selected component or an indexed component that |
1298 | -- yields an access to subprogram. Such a prefix is legal if the call | |
1299 | -- has parameter associations. | |
996ae0b0 RK |
1300 | |
1301 | elsif Is_Access_Type (Etype (P)) | |
1302 | and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type | |
1303 | then | |
1304 | if Present (Actuals) then | |
1305 | Analyze_Call_And_Resolve; | |
1306 | else | |
ed2233dc | 1307 | Error_Msg_N ("missing explicit dereference in call ", N); |
996ae0b0 RK |
1308 | end if; |
1309 | ||
82c80734 RD |
1310 | -- If not an access to subprogram, then the prefix must resolve to the |
1311 | -- name of an entry, entry family, or protected operation. | |
996ae0b0 | 1312 | |
82c80734 RD |
1313 | -- For the case of a simple entry call, P is a selected component where |
1314 | -- the prefix is the task and the selector name is the entry. A call to | |
1315 | -- a protected procedure will have the same syntax. If the protected | |
1316 | -- object contains overloaded operations, the entity may appear as a | |
1317 | -- function, the context will select the operation whose type is Void. | |
996ae0b0 RK |
1318 | |
1319 | elsif Nkind (P) = N_Selected_Component | |
1320 | and then (Ekind (Entity (Selector_Name (P))) = E_Entry | |
1321 | or else | |
1322 | Ekind (Entity (Selector_Name (P))) = E_Procedure | |
1323 | or else | |
1324 | Ekind (Entity (Selector_Name (P))) = E_Function) | |
1325 | then | |
1326 | Analyze_Call_And_Resolve; | |
1327 | ||
1328 | elsif Nkind (P) = N_Selected_Component | |
1329 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family | |
1330 | and then Present (Actuals) | |
1331 | and then No (Next (First (Actuals))) | |
1332 | then | |
82c80734 RD |
1333 | -- Can be call to parameterless entry family. What appears to be the |
1334 | -- sole argument is in fact the entry index. Rewrite prefix of node | |
1335 | -- accordingly. Source representation is unchanged by this | |
996ae0b0 RK |
1336 | -- transformation. |
1337 | ||
1338 | New_N := | |
1339 | Make_Indexed_Component (Loc, | |
1340 | Prefix => New_Copy (P), | |
1341 | Expressions => Actuals); | |
1342 | Set_Name (N, New_N); | |
1343 | Set_Etype (New_N, Standard_Void_Type); | |
1344 | Set_Parameter_Associations (N, No_List); | |
1345 | Analyze_Call_And_Resolve; | |
1346 | ||
1347 | -- For the case of a reference to an element of an entry family, P is | |
1348 | -- an indexed component whose prefix is a selected component (task and | |
1349 | -- entry family), and whose index is the entry family index. | |
1350 | ||
1351 | elsif Nkind (P) = N_Indexed_Component | |
1352 | and then Nkind (Prefix (P)) = N_Selected_Component | |
1353 | and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family | |
1354 | then | |
1355 | Analyze_Call_And_Resolve; | |
1356 | ||
1357 | -- If the prefix is the name of an entry family, it is a call from | |
1358 | -- within the task body itself. | |
1359 | ||
1360 | elsif Nkind (P) = N_Indexed_Component | |
1361 | and then Nkind (Prefix (P)) = N_Identifier | |
1362 | and then Ekind (Entity (Prefix (P))) = E_Entry_Family | |
1363 | then | |
1364 | New_N := | |
1365 | Make_Selected_Component (Loc, | |
1366 | Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), | |
1367 | Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); | |
1368 | Rewrite (Prefix (P), New_N); | |
1369 | Analyze (P); | |
1370 | Analyze_Call_And_Resolve; | |
1371 | ||
9f8d1e5c AC |
1372 | -- In Ada 2012. a qualified expression is a name, but it cannot be a |
1373 | -- procedure name, so the construct can only be a qualified expression. | |
1374 | ||
1375 | elsif Nkind (P) = N_Qualified_Expression | |
1376 | and then Ada_Version >= Ada_2012 | |
1377 | then | |
1378 | Rewrite (N, Make_Code_Statement (Loc, Expression => P)); | |
1379 | Analyze (N); | |
1380 | ||
e895b435 | 1381 | -- Anything else is an error |
996ae0b0 RK |
1382 | |
1383 | else | |
758c442c | 1384 | Error_Msg_N ("invalid procedure or entry call", N); |
996ae0b0 RK |
1385 | end if; |
1386 | end Analyze_Procedure_Call; | |
1387 | ||
b0186f71 AC |
1388 | ------------------------------ |
1389 | -- Analyze_Return_Statement -- | |
1390 | ------------------------------ | |
1391 | ||
1392 | procedure Analyze_Return_Statement (N : Node_Id) is | |
1393 | ||
1394 | pragma Assert (Nkind_In (N, N_Simple_Return_Statement, | |
1395 | N_Extended_Return_Statement)); | |
1396 | ||
1397 | Returns_Object : constant Boolean := | |
1398 | Nkind (N) = N_Extended_Return_Statement | |
1399 | or else | |
1400 | (Nkind (N) = N_Simple_Return_Statement | |
1401 | and then Present (Expression (N))); | |
1402 | -- True if we're returning something; that is, "return <expression>;" | |
1403 | -- or "return Result : T [:= ...]". False for "return;". Used for error | |
1404 | -- checking: If Returns_Object is True, N should apply to a function | |
1405 | -- body; otherwise N should apply to a procedure body, entry body, | |
1406 | -- accept statement, or extended return statement. | |
1407 | ||
1408 | function Find_What_It_Applies_To return Entity_Id; | |
1409 | -- Find the entity representing the innermost enclosing body, accept | |
1410 | -- statement, or extended return statement. If the result is a callable | |
1411 | -- construct or extended return statement, then this will be the value | |
1412 | -- of the Return_Applies_To attribute. Otherwise, the program is | |
1413 | -- illegal. See RM-6.5(4/2). | |
1414 | ||
1415 | ----------------------------- | |
1416 | -- Find_What_It_Applies_To -- | |
1417 | ----------------------------- | |
1418 | ||
1419 | function Find_What_It_Applies_To return Entity_Id is | |
1420 | Result : Entity_Id := Empty; | |
1421 | ||
1422 | begin | |
36b8f95f AC |
1423 | -- Loop outward through the Scope_Stack, skipping blocks, loops, |
1424 | -- and postconditions. | |
b0186f71 AC |
1425 | |
1426 | for J in reverse 0 .. Scope_Stack.Last loop | |
1427 | Result := Scope_Stack.Table (J).Entity; | |
11bc76df AC |
1428 | exit when not Ekind_In (Result, E_Block, E_Loop) |
1429 | and then Chars (Result) /= Name_uPostconditions; | |
b0186f71 AC |
1430 | end loop; |
1431 | ||
1432 | pragma Assert (Present (Result)); | |
1433 | return Result; | |
1434 | end Find_What_It_Applies_To; | |
1435 | ||
1436 | -- Local declarations | |
1437 | ||
1438 | Scope_Id : constant Entity_Id := Find_What_It_Applies_To; | |
1439 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
1440 | Loc : constant Source_Ptr := Sloc (N); | |
1441 | Stm_Entity : constant Entity_Id := | |
1442 | New_Internal_Entity | |
1443 | (E_Return_Statement, Current_Scope, Loc, 'R'); | |
1444 | ||
1445 | -- Start of processing for Analyze_Return_Statement | |
1446 | ||
1447 | begin | |
1448 | Set_Return_Statement_Entity (N, Stm_Entity); | |
1449 | ||
1450 | Set_Etype (Stm_Entity, Standard_Void_Type); | |
1451 | Set_Return_Applies_To (Stm_Entity, Scope_Id); | |
1452 | ||
1453 | -- Place Return entity on scope stack, to simplify enforcement of 6.5 | |
1454 | -- (4/2): an inner return statement will apply to this extended return. | |
1455 | ||
1456 | if Nkind (N) = N_Extended_Return_Statement then | |
1457 | Push_Scope (Stm_Entity); | |
1458 | end if; | |
1459 | ||
1460 | -- Check that pragma No_Return is obeyed. Don't complain about the | |
1461 | -- implicitly-generated return that is placed at the end. | |
1462 | ||
1463 | if No_Return (Scope_Id) and then Comes_From_Source (N) then | |
1464 | Error_Msg_N ("RETURN statement not allowed (No_Return)", N); | |
1465 | end if; | |
1466 | ||
1467 | -- Warn on any unassigned OUT parameters if in procedure | |
1468 | ||
1469 | if Ekind (Scope_Id) = E_Procedure then | |
1470 | Warn_On_Unassigned_Out_Parameter (N, Scope_Id); | |
1471 | end if; | |
1472 | ||
1473 | -- Check that functions return objects, and other things do not | |
1474 | ||
1475 | if Kind = E_Function or else Kind = E_Generic_Function then | |
1476 | if not Returns_Object then | |
1477 | Error_Msg_N ("missing expression in return from function", N); | |
1478 | end if; | |
1479 | ||
1480 | elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
1481 | if Returns_Object then | |
1482 | Error_Msg_N ("procedure cannot return value (use function)", N); | |
1483 | end if; | |
1484 | ||
1485 | elsif Kind = E_Entry or else Kind = E_Entry_Family then | |
1486 | if Returns_Object then | |
1487 | if Is_Protected_Type (Scope (Scope_Id)) then | |
1488 | Error_Msg_N ("entry body cannot return value", N); | |
1489 | else | |
1490 | Error_Msg_N ("accept statement cannot return value", N); | |
1491 | end if; | |
1492 | end if; | |
1493 | ||
1494 | elsif Kind = E_Return_Statement then | |
1495 | ||
1496 | -- We are nested within another return statement, which must be an | |
1497 | -- extended_return_statement. | |
1498 | ||
1499 | if Returns_Object then | |
d0dcb2b1 AC |
1500 | if Nkind (N) = N_Extended_Return_Statement then |
1501 | Error_Msg_N | |
cc96a1b8 | 1502 | ("extended return statement cannot be nested (use `RETURN;`)", |
d0dcb2b1 AC |
1503 | N); |
1504 | ||
1505 | -- Case of a simple return statement with a value inside extended | |
1506 | -- return statement. | |
1507 | ||
1508 | else | |
1509 | Error_Msg_N | |
1510 | ("return nested in extended return statement cannot return " & | |
cc96a1b8 | 1511 | "value (use `RETURN;`)", N); |
d0dcb2b1 | 1512 | end if; |
b0186f71 AC |
1513 | end if; |
1514 | ||
1515 | else | |
1516 | Error_Msg_N ("illegal context for return statement", N); | |
1517 | end if; | |
1518 | ||
1519 | if Ekind_In (Kind, E_Function, E_Generic_Function) then | |
1520 | Analyze_Function_Return (N); | |
1521 | ||
1522 | elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then | |
1523 | Set_Return_Present (Scope_Id); | |
1524 | end if; | |
1525 | ||
1526 | if Nkind (N) = N_Extended_Return_Statement then | |
1527 | End_Scope; | |
1528 | end if; | |
1529 | ||
1530 | Kill_Current_Values (Last_Assignment_Only => True); | |
1531 | Check_Unreachable_Code (N); | |
1532 | end Analyze_Return_Statement; | |
1533 | ||
5d37ba92 ES |
1534 | ------------------------------------- |
1535 | -- Analyze_Simple_Return_Statement -- | |
1536 | ------------------------------------- | |
ec4867fa | 1537 | |
5d37ba92 | 1538 | procedure Analyze_Simple_Return_Statement (N : Node_Id) is |
996ae0b0 | 1539 | begin |
5d37ba92 ES |
1540 | if Present (Expression (N)) then |
1541 | Mark_Coextensions (N, Expression (N)); | |
996ae0b0 RK |
1542 | end if; |
1543 | ||
5d37ba92 ES |
1544 | Analyze_Return_Statement (N); |
1545 | end Analyze_Simple_Return_Statement; | |
996ae0b0 | 1546 | |
82c80734 RD |
1547 | ------------------------- |
1548 | -- Analyze_Return_Type -- | |
1549 | ------------------------- | |
1550 | ||
1551 | procedure Analyze_Return_Type (N : Node_Id) is | |
1552 | Designator : constant Entity_Id := Defining_Entity (N); | |
1553 | Typ : Entity_Id := Empty; | |
1554 | ||
1555 | begin | |
ec4867fa ES |
1556 | -- Normal case where result definition does not indicate an error |
1557 | ||
41251c60 JM |
1558 | if Result_Definition (N) /= Error then |
1559 | if Nkind (Result_Definition (N)) = N_Access_Definition then | |
2ba431e5 | 1560 | Check_SPARK_Restriction |
fe5d3068 | 1561 | ("access result is not allowed", Result_Definition (N)); |
daec8eeb | 1562 | |
b1c11e0e JM |
1563 | -- Ada 2005 (AI-254): Handle anonymous access to subprograms |
1564 | ||
1565 | declare | |
1566 | AD : constant Node_Id := | |
1567 | Access_To_Subprogram_Definition (Result_Definition (N)); | |
1568 | begin | |
1569 | if Present (AD) and then Protected_Present (AD) then | |
1570 | Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); | |
1571 | else | |
1572 | Typ := Access_Definition (N, Result_Definition (N)); | |
1573 | end if; | |
1574 | end; | |
1575 | ||
41251c60 JM |
1576 | Set_Parent (Typ, Result_Definition (N)); |
1577 | Set_Is_Local_Anonymous_Access (Typ); | |
1578 | Set_Etype (Designator, Typ); | |
1579 | ||
b66c3ff4 AC |
1580 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1581 | ||
1582 | Null_Exclusion_Static_Checks (N); | |
1583 | ||
41251c60 JM |
1584 | -- Subtype_Mark case |
1585 | ||
1586 | else | |
1587 | Find_Type (Result_Definition (N)); | |
1588 | Typ := Entity (Result_Definition (N)); | |
1589 | Set_Etype (Designator, Typ); | |
1590 | ||
2ba431e5 | 1591 | -- Unconstrained array as result is not allowed in SPARK |
daec8eeb | 1592 | |
fe5d3068 | 1593 | if Is_Array_Type (Typ) |
daec8eeb YM |
1594 | and then not Is_Constrained (Typ) |
1595 | then | |
2ba431e5 | 1596 | Check_SPARK_Restriction |
fe5d3068 | 1597 | ("returning an unconstrained array is not allowed", |
7394c8cc | 1598 | Result_Definition (N)); |
daec8eeb YM |
1599 | end if; |
1600 | ||
b66c3ff4 AC |
1601 | -- Ada 2005 (AI-231): Ensure proper usage of null exclusion |
1602 | ||
1603 | Null_Exclusion_Static_Checks (N); | |
1604 | ||
1605 | -- If a null exclusion is imposed on the result type, then create | |
1606 | -- a null-excluding itype (an access subtype) and use it as the | |
1607 | -- function's Etype. Note that the null exclusion checks are done | |
1608 | -- right before this, because they don't get applied to types that | |
1609 | -- do not come from source. | |
1610 | ||
1611 | if Is_Access_Type (Typ) | |
1612 | and then Null_Exclusion_Present (N) | |
1613 | then | |
1614 | Set_Etype (Designator, | |
1615 | Create_Null_Excluding_Itype | |
ff7139c3 AC |
1616 | (T => Typ, |
1617 | Related_Nod => N, | |
1618 | Scope_Id => Scope (Current_Scope))); | |
1619 | ||
1620 | -- The new subtype must be elaborated before use because | |
1621 | -- it is visible outside of the function. However its base | |
1622 | -- type may not be frozen yet, so the reference that will | |
1623 | -- force elaboration must be attached to the freezing of | |
1624 | -- the base type. | |
1625 | ||
212863c0 AC |
1626 | -- If the return specification appears on a proper body, |
1627 | -- the subtype will have been created already on the spec. | |
1628 | ||
ff7139c3 | 1629 | if Is_Frozen (Typ) then |
212863c0 AC |
1630 | if Nkind (Parent (N)) = N_Subprogram_Body |
1631 | and then Nkind (Parent (Parent (N))) = N_Subunit | |
1632 | then | |
1633 | null; | |
1634 | else | |
1635 | Build_Itype_Reference (Etype (Designator), Parent (N)); | |
1636 | end if; | |
1637 | ||
ff7139c3 AC |
1638 | else |
1639 | Ensure_Freeze_Node (Typ); | |
1640 | ||
1641 | declare | |
212863c0 | 1642 | IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); |
ff7139c3 AC |
1643 | begin |
1644 | Set_Itype (IR, Etype (Designator)); | |
1645 | Append_Freeze_Actions (Typ, New_List (IR)); | |
1646 | end; | |
1647 | end if; | |
1648 | ||
b66c3ff4 AC |
1649 | else |
1650 | Set_Etype (Designator, Typ); | |
1651 | end if; | |
1652 | ||
41251c60 | 1653 | if Ekind (Typ) = E_Incomplete_Type |
0a36105d JM |
1654 | and then Is_Value_Type (Typ) |
1655 | then | |
1656 | null; | |
1657 | ||
1658 | elsif Ekind (Typ) = E_Incomplete_Type | |
41251c60 JM |
1659 | or else (Is_Class_Wide_Type (Typ) |
1660 | and then | |
1661 | Ekind (Root_Type (Typ)) = E_Incomplete_Type) | |
1662 | then | |
dd386db0 AC |
1663 | -- AI05-0151: Tagged incomplete types are allowed in all formal |
1664 | -- parts. Untagged incomplete types are not allowed in bodies. | |
1665 | ||
1666 | if Ada_Version >= Ada_2012 then | |
1667 | if Is_Tagged_Type (Typ) then | |
1668 | null; | |
1669 | ||
1670 | elsif Nkind_In (Parent (Parent (N)), | |
1671 | N_Accept_Statement, | |
1672 | N_Entry_Body, | |
1673 | N_Subprogram_Body) | |
1674 | then | |
1675 | Error_Msg_NE | |
1676 | ("invalid use of untagged incomplete type&", | |
1677 | Designator, Typ); | |
1678 | end if; | |
1679 | ||
63be2a5a AC |
1680 | -- The type must be completed in the current package. This |
1681 | -- is checked at the end of the package declaraton, when | |
7b7a0c2b AC |
1682 | -- Taft-amendment types are identified. If the return type |
1683 | -- is class-wide, there is no required check, the type can | |
1684 | -- be a bona fide TAT. | |
63be2a5a AC |
1685 | |
1686 | if Ekind (Scope (Current_Scope)) = E_Package | |
c199ccf7 | 1687 | and then In_Private_Part (Scope (Current_Scope)) |
7b7a0c2b | 1688 | and then not Is_Class_Wide_Type (Typ) |
63be2a5a AC |
1689 | then |
1690 | Append_Elmt (Designator, Private_Dependents (Typ)); | |
1691 | end if; | |
1692 | ||
dd386db0 AC |
1693 | else |
1694 | Error_Msg_NE | |
1695 | ("invalid use of incomplete type&", Designator, Typ); | |
1696 | end if; | |
41251c60 | 1697 | end if; |
82c80734 RD |
1698 | end if; |
1699 | ||
ec4867fa ES |
1700 | -- Case where result definition does indicate an error |
1701 | ||
82c80734 RD |
1702 | else |
1703 | Set_Etype (Designator, Any_Type); | |
1704 | end if; | |
1705 | end Analyze_Return_Type; | |
1706 | ||
996ae0b0 RK |
1707 | ----------------------------- |
1708 | -- Analyze_Subprogram_Body -- | |
1709 | ----------------------------- | |
1710 | ||
b1b543d2 BD |
1711 | procedure Analyze_Subprogram_Body (N : Node_Id) is |
1712 | Loc : constant Source_Ptr := Sloc (N); | |
1713 | Body_Spec : constant Node_Id := Specification (N); | |
1714 | Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); | |
1715 | ||
1716 | begin | |
1717 | if Debug_Flag_C then | |
1718 | Write_Str ("==> subprogram body "); | |
1719 | Write_Name (Chars (Body_Id)); | |
1720 | Write_Str (" from "); | |
1721 | Write_Location (Loc); | |
1722 | Write_Eol; | |
1723 | Indent; | |
1724 | end if; | |
1725 | ||
1726 | Trace_Scope (N, Body_Id, " Analyze subprogram: "); | |
1727 | ||
1728 | -- The real work is split out into the helper, so it can do "return;" | |
1729 | -- without skipping the debug output: | |
1730 | ||
1731 | Analyze_Subprogram_Body_Helper (N); | |
1732 | ||
1733 | if Debug_Flag_C then | |
1734 | Outdent; | |
1735 | Write_Str ("<== subprogram body "); | |
1736 | Write_Name (Chars (Body_Id)); | |
1737 | Write_Str (" from "); | |
1738 | Write_Location (Loc); | |
1739 | Write_Eol; | |
1740 | end if; | |
1741 | end Analyze_Subprogram_Body; | |
1742 | ||
1743 | ------------------------------------ | |
1744 | -- Analyze_Subprogram_Body_Helper -- | |
1745 | ------------------------------------ | |
1746 | ||
996ae0b0 RK |
1747 | -- This procedure is called for regular subprogram bodies, generic bodies, |
1748 | -- and for subprogram stubs of both kinds. In the case of stubs, only the | |
1749 | -- specification matters, and is used to create a proper declaration for | |
1750 | -- the subprogram, or to perform conformance checks. | |
1751 | ||
b1b543d2 | 1752 | procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is |
fbf5a39b | 1753 | Loc : constant Source_Ptr := Sloc (N); |
33931112 | 1754 | Body_Deleted : constant Boolean := False; |
fbf5a39b AC |
1755 | Body_Spec : constant Node_Id := Specification (N); |
1756 | Body_Id : Entity_Id := Defining_Entity (Body_Spec); | |
1757 | Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); | |
0868e09c | 1758 | Conformant : Boolean; |
21d27997 | 1759 | HSS : Node_Id; |
07fc65c4 | 1760 | P_Ent : Entity_Id; |
21d27997 RD |
1761 | Prot_Typ : Entity_Id := Empty; |
1762 | Spec_Id : Entity_Id; | |
1763 | Spec_Decl : Node_Id := Empty; | |
1764 | ||
1765 | Last_Real_Spec_Entity : Entity_Id := Empty; | |
1766 | -- When we analyze a separate spec, the entity chain ends up containing | |
1767 | -- the formals, as well as any itypes generated during analysis of the | |
1768 | -- default expressions for parameters, or the arguments of associated | |
1769 | -- precondition/postcondition pragmas (which are analyzed in the context | |
1770 | -- of the spec since they have visibility on formals). | |
1771 | -- | |
1772 | -- These entities belong with the spec and not the body. However we do | |
1773 | -- the analysis of the body in the context of the spec (again to obtain | |
1774 | -- visibility to the formals), and all the entities generated during | |
1775 | -- this analysis end up also chained to the entity chain of the spec. | |
1776 | -- But they really belong to the body, and there is circuitry to move | |
1777 | -- them from the spec to the body. | |
1778 | -- | |
1779 | -- However, when we do this move, we don't want to move the real spec | |
1780 | -- entities (first para above) to the body. The Last_Real_Spec_Entity | |
1781 | -- variable points to the last real spec entity, so we only move those | |
1782 | -- chained beyond that point. It is initialized to Empty to deal with | |
1783 | -- the case where there is no separate spec. | |
996ae0b0 | 1784 | |
ec4867fa | 1785 | procedure Check_Anonymous_Return; |
e50e1c5e | 1786 | -- Ada 2005: if a function returns an access type that denotes a task, |
ec4867fa ES |
1787 | -- or a type that contains tasks, we must create a master entity for |
1788 | -- the anonymous type, which typically will be used in an allocator | |
1789 | -- in the body of the function. | |
1790 | ||
e660dbf7 JM |
1791 | procedure Check_Inline_Pragma (Spec : in out Node_Id); |
1792 | -- Look ahead to recognize a pragma that may appear after the body. | |
1793 | -- If there is a previous spec, check that it appears in the same | |
1794 | -- declarative part. If the pragma is Inline_Always, perform inlining | |
1795 | -- unconditionally, otherwise only if Front_End_Inlining is requested. | |
1796 | -- If the body acts as a spec, and inlining is required, we create a | |
1797 | -- subprogram declaration for it, in order to attach the body to inline. | |
21d27997 RD |
1798 | -- If pragma does not appear after the body, check whether there is |
1799 | -- an inline pragma before any local declarations. | |
c37bb106 | 1800 | |
7665e4bd AC |
1801 | procedure Check_Missing_Return; |
1802 | -- Checks for a function with a no return statements, and also performs | |
8d606a78 RD |
1803 | -- the warning checks implemented by Check_Returns. In formal mode, also |
1804 | -- verify that a function ends with a RETURN and that a procedure does | |
1805 | -- not contain any RETURN. | |
7665e4bd | 1806 | |
d44202ba HK |
1807 | function Disambiguate_Spec return Entity_Id; |
1808 | -- When a primitive is declared between the private view and the full | |
1809 | -- view of a concurrent type which implements an interface, a special | |
1810 | -- mechanism is used to find the corresponding spec of the primitive | |
1811 | -- body. | |
1812 | ||
5dcab3ca AC |
1813 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id); |
1814 | -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains | |
1815 | -- incomplete types coming from a limited context and swap their limited | |
1816 | -- views with the non-limited ones. | |
1817 | ||
d44202ba HK |
1818 | function Is_Private_Concurrent_Primitive |
1819 | (Subp_Id : Entity_Id) return Boolean; | |
1820 | -- Determine whether subprogram Subp_Id is a primitive of a concurrent | |
1821 | -- type that implements an interface and has a private view. | |
1822 | ||
76a69663 ES |
1823 | procedure Set_Trivial_Subprogram (N : Node_Id); |
1824 | -- Sets the Is_Trivial_Subprogram flag in both spec and body of the | |
1825 | -- subprogram whose body is being analyzed. N is the statement node | |
1826 | -- causing the flag to be set, if the following statement is a return | |
1827 | -- of an entity, we mark the entity as set in source to suppress any | |
1828 | -- warning on the stylized use of function stubs with a dummy return. | |
1829 | ||
758c442c GD |
1830 | procedure Verify_Overriding_Indicator; |
1831 | -- If there was a previous spec, the entity has been entered in the | |
1832 | -- current scope previously. If the body itself carries an overriding | |
1833 | -- indicator, check that it is consistent with the known status of the | |
1834 | -- entity. | |
1835 | ||
ec4867fa ES |
1836 | ---------------------------- |
1837 | -- Check_Anonymous_Return -- | |
1838 | ---------------------------- | |
1839 | ||
1840 | procedure Check_Anonymous_Return is | |
1841 | Decl : Node_Id; | |
a523b302 | 1842 | Par : Node_Id; |
ec4867fa ES |
1843 | Scop : Entity_Id; |
1844 | ||
1845 | begin | |
1846 | if Present (Spec_Id) then | |
1847 | Scop := Spec_Id; | |
1848 | else | |
1849 | Scop := Body_Id; | |
1850 | end if; | |
1851 | ||
1852 | if Ekind (Scop) = E_Function | |
1853 | and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type | |
a523b302 JM |
1854 | and then not Is_Thunk (Scop) |
1855 | and then (Has_Task (Designated_Type (Etype (Scop))) | |
1856 | or else | |
1857 | (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) | |
1858 | and then | |
1859 | Is_Limited_Record (Designated_Type (Etype (Scop))))) | |
ec4867fa | 1860 | and then Expander_Active |
b20de9b9 AC |
1861 | |
1862 | -- Avoid cases with no tasking support | |
1863 | ||
1864 | and then RTE_Available (RE_Current_Master) | |
1865 | and then not Restriction_Active (No_Task_Hierarchy) | |
ec4867fa ES |
1866 | then |
1867 | Decl := | |
1868 | Make_Object_Declaration (Loc, | |
1869 | Defining_Identifier => | |
1870 | Make_Defining_Identifier (Loc, Name_uMaster), | |
1871 | Constant_Present => True, | |
1872 | Object_Definition => | |
1873 | New_Reference_To (RTE (RE_Master_Id), Loc), | |
1874 | Expression => | |
1875 | Make_Explicit_Dereference (Loc, | |
1876 | New_Reference_To (RTE (RE_Current_Master), Loc))); | |
1877 | ||
1878 | if Present (Declarations (N)) then | |
1879 | Prepend (Decl, Declarations (N)); | |
1880 | else | |
1881 | Set_Declarations (N, New_List (Decl)); | |
1882 | end if; | |
1883 | ||
1884 | Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); | |
1885 | Set_Has_Master_Entity (Scop); | |
a523b302 JM |
1886 | |
1887 | -- Now mark the containing scope as a task master | |
1888 | ||
1889 | Par := N; | |
1890 | while Nkind (Par) /= N_Compilation_Unit loop | |
1891 | Par := Parent (Par); | |
1892 | pragma Assert (Present (Par)); | |
1893 | ||
1894 | -- If we fall off the top, we are at the outer level, and | |
1895 | -- the environment task is our effective master, so nothing | |
1896 | -- to mark. | |
1897 | ||
1898 | if Nkind_In | |
1899 | (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) | |
1900 | then | |
1901 | Set_Is_Task_Master (Par, True); | |
1902 | exit; | |
1903 | end if; | |
1904 | end loop; | |
ec4867fa ES |
1905 | end if; |
1906 | end Check_Anonymous_Return; | |
1907 | ||
e660dbf7 JM |
1908 | ------------------------- |
1909 | -- Check_Inline_Pragma -- | |
1910 | ------------------------- | |
758c442c | 1911 | |
e660dbf7 JM |
1912 | procedure Check_Inline_Pragma (Spec : in out Node_Id) is |
1913 | Prag : Node_Id; | |
1914 | Plist : List_Id; | |
0fb2ea01 | 1915 | |
21d27997 | 1916 | function Is_Inline_Pragma (N : Node_Id) return Boolean; |
30783513 | 1917 | -- True when N is a pragma Inline or Inline_Always that applies |
33931112 | 1918 | -- to this subprogram. |
21d27997 RD |
1919 | |
1920 | ----------------------- | |
1921 | -- Is_Inline_Pragma -- | |
1922 | ----------------------- | |
1923 | ||
1924 | function Is_Inline_Pragma (N : Node_Id) return Boolean is | |
1925 | begin | |
1926 | return | |
1927 | Nkind (N) = N_Pragma | |
1928 | and then | |
1929 | (Pragma_Name (N) = Name_Inline_Always | |
1930 | or else | |
1931 | (Front_End_Inlining | |
1932 | and then Pragma_Name (N) = Name_Inline)) | |
1933 | and then | |
1934 | Chars | |
1935 | (Expression (First (Pragma_Argument_Associations (N)))) | |
1936 | = Chars (Body_Id); | |
1937 | end Is_Inline_Pragma; | |
1938 | ||
1939 | -- Start of processing for Check_Inline_Pragma | |
1940 | ||
c37bb106 | 1941 | begin |
e660dbf7 JM |
1942 | if not Expander_Active then |
1943 | return; | |
1944 | end if; | |
1945 | ||
1946 | if Is_List_Member (N) | |
1947 | and then Present (Next (N)) | |
21d27997 | 1948 | and then Is_Inline_Pragma (Next (N)) |
c37bb106 AC |
1949 | then |
1950 | Prag := Next (N); | |
1951 | ||
21d27997 RD |
1952 | elsif Nkind (N) /= N_Subprogram_Body_Stub |
1953 | and then Present (Declarations (N)) | |
1954 | and then Is_Inline_Pragma (First (Declarations (N))) | |
1955 | then | |
1956 | Prag := First (Declarations (N)); | |
1957 | ||
e660dbf7 JM |
1958 | else |
1959 | Prag := Empty; | |
c37bb106 | 1960 | end if; |
e660dbf7 JM |
1961 | |
1962 | if Present (Prag) then | |
1963 | if Present (Spec_Id) then | |
30196a76 | 1964 | if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then |
e660dbf7 JM |
1965 | Analyze (Prag); |
1966 | end if; | |
1967 | ||
1968 | else | |
d39d6bb8 | 1969 | -- Create a subprogram declaration, to make treatment uniform |
e660dbf7 JM |
1970 | |
1971 | declare | |
1972 | Subp : constant Entity_Id := | |
30196a76 | 1973 | Make_Defining_Identifier (Loc, Chars (Body_Id)); |
e660dbf7 | 1974 | Decl : constant Node_Id := |
30196a76 RD |
1975 | Make_Subprogram_Declaration (Loc, |
1976 | Specification => | |
1977 | New_Copy_Tree (Specification (N))); | |
1978 | ||
e660dbf7 JM |
1979 | begin |
1980 | Set_Defining_Unit_Name (Specification (Decl), Subp); | |
1981 | ||
1982 | if Present (First_Formal (Body_Id)) then | |
21d27997 | 1983 | Plist := Copy_Parameter_List (Body_Id); |
e660dbf7 JM |
1984 | Set_Parameter_Specifications |
1985 | (Specification (Decl), Plist); | |
1986 | end if; | |
1987 | ||
1988 | Insert_Before (N, Decl); | |
1989 | Analyze (Decl); | |
1990 | Analyze (Prag); | |
1991 | Set_Has_Pragma_Inline (Subp); | |
1992 | ||
76a69663 | 1993 | if Pragma_Name (Prag) = Name_Inline_Always then |
e660dbf7 | 1994 | Set_Is_Inlined (Subp); |
21d27997 | 1995 | Set_Has_Pragma_Inline_Always (Subp); |
e660dbf7 JM |
1996 | end if; |
1997 | ||
1998 | Spec := Subp; | |
1999 | end; | |
2000 | end if; | |
2001 | end if; | |
2002 | end Check_Inline_Pragma; | |
2003 | ||
7665e4bd AC |
2004 | -------------------------- |
2005 | -- Check_Missing_Return -- | |
2006 | -------------------------- | |
2007 | ||
2008 | procedure Check_Missing_Return is | |
2009 | Id : Entity_Id; | |
2010 | Missing_Ret : Boolean; | |
2011 | ||
2012 | begin | |
2013 | if Nkind (Body_Spec) = N_Function_Specification then | |
2014 | if Present (Spec_Id) then | |
2015 | Id := Spec_Id; | |
2016 | else | |
2017 | Id := Body_Id; | |
2018 | end if; | |
2019 | ||
fe5d3068 | 2020 | if Return_Present (Id) then |
7665e4bd AC |
2021 | Check_Returns (HSS, 'F', Missing_Ret); |
2022 | ||
2023 | if Missing_Ret then | |
2024 | Set_Has_Missing_Return (Id); | |
2025 | end if; | |
2026 | ||
2027 | elsif (Is_Generic_Subprogram (Id) | |
2028 | or else not Is_Machine_Code_Subprogram (Id)) | |
2029 | and then not Body_Deleted | |
2030 | then | |
2031 | Error_Msg_N ("missing RETURN statement in function body", N); | |
2032 | end if; | |
2033 | ||
fe5d3068 | 2034 | -- If procedure with No_Return, check returns |
607d0635 | 2035 | |
fe5d3068 YM |
2036 | elsif Nkind (Body_Spec) = N_Procedure_Specification |
2037 | and then Present (Spec_Id) | |
2038 | and then No_Return (Spec_Id) | |
607d0635 | 2039 | then |
fe5d3068 YM |
2040 | Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); |
2041 | end if; | |
2042 | ||
ad05f2e9 | 2043 | -- Special checks in SPARK mode |
fe5d3068 YM |
2044 | |
2045 | if Nkind (Body_Spec) = N_Function_Specification then | |
7394c8cc | 2046 | |
ad05f2e9 | 2047 | -- In SPARK mode, last statement of a function should be a return |
fe5d3068 YM |
2048 | |
2049 | declare | |
2050 | Stat : constant Node_Id := Last_Source_Statement (HSS); | |
2051 | begin | |
2052 | if Present (Stat) | |
7394c8cc AC |
2053 | and then not Nkind_In (Stat, N_Simple_Return_Statement, |
2054 | N_Extended_Return_Statement) | |
fe5d3068 | 2055 | then |
2ba431e5 | 2056 | Check_SPARK_Restriction |
fe5d3068 YM |
2057 | ("last statement in function should be RETURN", Stat); |
2058 | end if; | |
2059 | end; | |
2060 | ||
ad05f2e9 | 2061 | -- In SPARK mode, verify that a procedure has no return |
fe5d3068 YM |
2062 | |
2063 | elsif Nkind (Body_Spec) = N_Procedure_Specification then | |
607d0635 AC |
2064 | if Present (Spec_Id) then |
2065 | Id := Spec_Id; | |
2066 | else | |
2067 | Id := Body_Id; | |
2068 | end if; | |
2069 | ||
8d606a78 RD |
2070 | -- Would be nice to point to return statement here, can we |
2071 | -- borrow the Check_Returns procedure here ??? | |
2072 | ||
607d0635 | 2073 | if Return_Present (Id) then |
2ba431e5 | 2074 | Check_SPARK_Restriction |
fe5d3068 | 2075 | ("procedure should not have RETURN", N); |
607d0635 | 2076 | end if; |
7665e4bd AC |
2077 | end if; |
2078 | end Check_Missing_Return; | |
2079 | ||
d44202ba HK |
2080 | ----------------------- |
2081 | -- Disambiguate_Spec -- | |
2082 | ----------------------- | |
2083 | ||
2084 | function Disambiguate_Spec return Entity_Id is | |
2085 | Priv_Spec : Entity_Id; | |
2086 | Spec_N : Entity_Id; | |
2087 | ||
2088 | procedure Replace_Types (To_Corresponding : Boolean); | |
2089 | -- Depending on the flag, replace the type of formal parameters of | |
2090 | -- Body_Id if it is a concurrent type implementing interfaces with | |
2091 | -- the corresponding record type or the other way around. | |
2092 | ||
2093 | procedure Replace_Types (To_Corresponding : Boolean) is | |
2094 | Formal : Entity_Id; | |
2095 | Formal_Typ : Entity_Id; | |
2096 | ||
2097 | begin | |
2098 | Formal := First_Formal (Body_Id); | |
2099 | while Present (Formal) loop | |
2100 | Formal_Typ := Etype (Formal); | |
2101 | ||
df3e68b1 HK |
2102 | if Is_Class_Wide_Type (Formal_Typ) then |
2103 | Formal_Typ := Root_Type (Formal_Typ); | |
2104 | end if; | |
2105 | ||
d44202ba HK |
2106 | -- From concurrent type to corresponding record |
2107 | ||
2108 | if To_Corresponding then | |
2109 | if Is_Concurrent_Type (Formal_Typ) | |
2110 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
2111 | and then Present (Interfaces ( | |
2112 | Corresponding_Record_Type (Formal_Typ))) | |
2113 | then | |
2114 | Set_Etype (Formal, | |
2115 | Corresponding_Record_Type (Formal_Typ)); | |
2116 | end if; | |
2117 | ||
2118 | -- From corresponding record to concurrent type | |
2119 | ||
2120 | else | |
2121 | if Is_Concurrent_Record_Type (Formal_Typ) | |
2122 | and then Present (Interfaces (Formal_Typ)) | |
2123 | then | |
2124 | Set_Etype (Formal, | |
2125 | Corresponding_Concurrent_Type (Formal_Typ)); | |
2126 | end if; | |
2127 | end if; | |
2128 | ||
2129 | Next_Formal (Formal); | |
2130 | end loop; | |
2131 | end Replace_Types; | |
2132 | ||
2133 | -- Start of processing for Disambiguate_Spec | |
2134 | ||
2135 | begin | |
2136 | -- Try to retrieve the specification of the body as is. All error | |
2137 | -- messages are suppressed because the body may not have a spec in | |
2138 | -- its current state. | |
2139 | ||
2140 | Spec_N := Find_Corresponding_Spec (N, False); | |
2141 | ||
2142 | -- It is possible that this is the body of a primitive declared | |
2143 | -- between a private and a full view of a concurrent type. The | |
2144 | -- controlling parameter of the spec carries the concurrent type, | |
2145 | -- not the corresponding record type as transformed by Analyze_ | |
2146 | -- Subprogram_Specification. In such cases, we undo the change | |
2147 | -- made by the analysis of the specification and try to find the | |
2148 | -- spec again. | |
766d7add | 2149 | |
8198b93d HK |
2150 | -- Note that wrappers already have their corresponding specs and |
2151 | -- bodies set during their creation, so if the candidate spec is | |
16b05213 | 2152 | -- a wrapper, then we definitely need to swap all types to their |
8198b93d | 2153 | -- original concurrent status. |
d44202ba | 2154 | |
8198b93d HK |
2155 | if No (Spec_N) |
2156 | or else Is_Primitive_Wrapper (Spec_N) | |
2157 | then | |
d44202ba HK |
2158 | -- Restore all references of corresponding record types to the |
2159 | -- original concurrent types. | |
2160 | ||
2161 | Replace_Types (To_Corresponding => False); | |
2162 | Priv_Spec := Find_Corresponding_Spec (N, False); | |
2163 | ||
2164 | -- The current body truly belongs to a primitive declared between | |
2165 | -- a private and a full view. We leave the modified body as is, | |
2166 | -- and return the true spec. | |
2167 | ||
2168 | if Present (Priv_Spec) | |
2169 | and then Is_Private_Primitive (Priv_Spec) | |
2170 | then | |
2171 | return Priv_Spec; | |
2172 | end if; | |
2173 | ||
2174 | -- In case that this is some sort of error, restore the original | |
2175 | -- state of the body. | |
2176 | ||
2177 | Replace_Types (To_Corresponding => True); | |
2178 | end if; | |
2179 | ||
2180 | return Spec_N; | |
2181 | end Disambiguate_Spec; | |
2182 | ||
5dcab3ca AC |
2183 | ---------------------------- |
2184 | -- Exchange_Limited_Views -- | |
2185 | ---------------------------- | |
2186 | ||
2187 | procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is | |
2188 | procedure Detect_And_Exchange (Id : Entity_Id); | |
2189 | -- Determine whether Id's type denotes an incomplete type associated | |
2190 | -- with a limited with clause and exchange the limited view with the | |
2191 | -- non-limited one. | |
2192 | ||
2193 | ------------------------- | |
2194 | -- Detect_And_Exchange -- | |
2195 | ------------------------- | |
2196 | ||
2197 | procedure Detect_And_Exchange (Id : Entity_Id) is | |
2198 | Typ : constant Entity_Id := Etype (Id); | |
2199 | ||
2200 | begin | |
2201 | if Ekind (Typ) = E_Incomplete_Type | |
2202 | and then From_With_Type (Typ) | |
2203 | and then Present (Non_Limited_View (Typ)) | |
2204 | then | |
2205 | Set_Etype (Id, Non_Limited_View (Typ)); | |
2206 | end if; | |
2207 | end Detect_And_Exchange; | |
2208 | ||
2209 | -- Local variables | |
2210 | ||
2211 | Formal : Entity_Id; | |
2212 | ||
2213 | -- Start of processing for Exchange_Limited_Views | |
2214 | ||
2215 | begin | |
2216 | if No (Subp_Id) then | |
2217 | return; | |
2218 | ||
2219 | -- Do not process subprogram bodies as they already use the non- | |
2220 | -- limited view of types. | |
2221 | ||
2222 | elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then | |
2223 | return; | |
2224 | end if; | |
2225 | ||
2226 | -- Examine all formals and swap views when applicable | |
2227 | ||
2228 | Formal := First_Formal (Subp_Id); | |
2229 | while Present (Formal) loop | |
2230 | Detect_And_Exchange (Formal); | |
2231 | ||
2232 | Next_Formal (Formal); | |
2233 | end loop; | |
2234 | ||
2235 | -- Process the return type of a function | |
2236 | ||
2237 | if Ekind (Subp_Id) = E_Function then | |
2238 | Detect_And_Exchange (Subp_Id); | |
2239 | end if; | |
2240 | end Exchange_Limited_Views; | |
2241 | ||
d44202ba HK |
2242 | ------------------------------------- |
2243 | -- Is_Private_Concurrent_Primitive -- | |
2244 | ------------------------------------- | |
2245 | ||
2246 | function Is_Private_Concurrent_Primitive | |
2247 | (Subp_Id : Entity_Id) return Boolean | |
2248 | is | |
2249 | Formal_Typ : Entity_Id; | |
2250 | ||
2251 | begin | |
2252 | if Present (First_Formal (Subp_Id)) then | |
2253 | Formal_Typ := Etype (First_Formal (Subp_Id)); | |
2254 | ||
2255 | if Is_Concurrent_Record_Type (Formal_Typ) then | |
df3e68b1 HK |
2256 | if Is_Class_Wide_Type (Formal_Typ) then |
2257 | Formal_Typ := Root_Type (Formal_Typ); | |
2258 | end if; | |
2259 | ||
d44202ba HK |
2260 | Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); |
2261 | end if; | |
2262 | ||
2263 | -- The type of the first formal is a concurrent tagged type with | |
2264 | -- a private view. | |
2265 | ||
2266 | return | |
2267 | Is_Concurrent_Type (Formal_Typ) | |
2268 | and then Is_Tagged_Type (Formal_Typ) | |
2269 | and then Has_Private_Declaration (Formal_Typ); | |
2270 | end if; | |
2271 | ||
2272 | return False; | |
2273 | end Is_Private_Concurrent_Primitive; | |
2274 | ||
76a69663 ES |
2275 | ---------------------------- |
2276 | -- Set_Trivial_Subprogram -- | |
2277 | ---------------------------- | |
2278 | ||
2279 | procedure Set_Trivial_Subprogram (N : Node_Id) is | |
2280 | Nxt : constant Node_Id := Next (N); | |
2281 | ||
2282 | begin | |
2283 | Set_Is_Trivial_Subprogram (Body_Id); | |
2284 | ||
2285 | if Present (Spec_Id) then | |
2286 | Set_Is_Trivial_Subprogram (Spec_Id); | |
2287 | end if; | |
2288 | ||
2289 | if Present (Nxt) | |
2290 | and then Nkind (Nxt) = N_Simple_Return_Statement | |
2291 | and then No (Next (Nxt)) | |
2292 | and then Present (Expression (Nxt)) | |
2293 | and then Is_Entity_Name (Expression (Nxt)) | |
2294 | then | |
2295 | Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); | |
2296 | end if; | |
2297 | end Set_Trivial_Subprogram; | |
2298 | ||
758c442c GD |
2299 | --------------------------------- |
2300 | -- Verify_Overriding_Indicator -- | |
2301 | --------------------------------- | |
2302 | ||
2303 | procedure Verify_Overriding_Indicator is | |
2304 | begin | |
21d27997 RD |
2305 | if Must_Override (Body_Spec) then |
2306 | if Nkind (Spec_Id) = N_Defining_Operator_Symbol | |
2307 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
2308 | then | |
2309 | null; | |
2310 | ||
038140ed | 2311 | elsif not Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2312 | Error_Msg_NE |
21d27997 RD |
2313 | ("subprogram& is not overriding", Body_Spec, Spec_Id); |
2314 | end if; | |
758c442c | 2315 | |
5d37ba92 | 2316 | elsif Must_Not_Override (Body_Spec) then |
038140ed | 2317 | if Present (Overridden_Operation (Spec_Id)) then |
ed2233dc | 2318 | Error_Msg_NE |
5d37ba92 | 2319 | ("subprogram& overrides inherited operation", |
76a69663 | 2320 | Body_Spec, Spec_Id); |
5d37ba92 | 2321 | |
21d27997 RD |
2322 | elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol |
2323 | and then Operator_Matches_Spec (Spec_Id, Spec_Id) | |
2324 | then | |
ed2233dc | 2325 | Error_Msg_NE |
21d27997 RD |
2326 | ("subprogram & overrides predefined operator ", |
2327 | Body_Spec, Spec_Id); | |
2328 | ||
618fb570 AC |
2329 | -- If this is not a primitive operation or protected subprogram, |
2330 | -- then the overriding indicator is altogether illegal. | |
5d37ba92 | 2331 | |
618fb570 AC |
2332 | elsif not Is_Primitive (Spec_Id) |
2333 | and then Ekind (Scope (Spec_Id)) /= E_Protected_Type | |
2334 | then | |
ed2233dc | 2335 | Error_Msg_N |
19d846a0 RD |
2336 | ("overriding indicator only allowed " & |
2337 | "if subprogram is primitive", | |
2338 | Body_Spec); | |
5d37ba92 | 2339 | end if; |
235f4375 | 2340 | |
806f6d37 | 2341 | elsif Style_Check |
038140ed | 2342 | and then Present (Overridden_Operation (Spec_Id)) |
235f4375 AC |
2343 | then |
2344 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
2345 | Style.Missing_Overriding (N, Body_Id); | |
806f6d37 AC |
2346 | |
2347 | elsif Style_Check | |
2348 | and then Can_Override_Operator (Spec_Id) | |
2349 | and then not Is_Predefined_File_Name | |
2350 | (Unit_File_Name (Get_Source_Unit (Spec_Id))) | |
2351 | then | |
2352 | pragma Assert (Unit_Declaration_Node (Body_Id) = N); | |
2353 | Style.Missing_Overriding (N, Body_Id); | |
758c442c GD |
2354 | end if; |
2355 | end Verify_Overriding_Indicator; | |
2356 | ||
b1b543d2 | 2357 | -- Start of processing for Analyze_Subprogram_Body_Helper |
0fb2ea01 | 2358 | |
996ae0b0 | 2359 | begin |
82c80734 RD |
2360 | -- Generic subprograms are handled separately. They always have a |
2361 | -- generic specification. Determine whether current scope has a | |
2362 | -- previous declaration. | |
996ae0b0 | 2363 | |
82c80734 RD |
2364 | -- If the subprogram body is defined within an instance of the same |
2365 | -- name, the instance appears as a package renaming, and will be hidden | |
2366 | -- within the subprogram. | |
996ae0b0 RK |
2367 | |
2368 | if Present (Prev_Id) | |
2369 | and then not Is_Overloadable (Prev_Id) | |
2370 | and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration | |
2371 | or else Comes_From_Source (Prev_Id)) | |
2372 | then | |
fbf5a39b | 2373 | if Is_Generic_Subprogram (Prev_Id) then |
996ae0b0 RK |
2374 | Spec_Id := Prev_Id; |
2375 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
2376 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
2377 | ||
2378 | Analyze_Generic_Subprogram_Body (N, Spec_Id); | |
7665e4bd AC |
2379 | |
2380 | if Nkind (N) = N_Subprogram_Body then | |
2381 | HSS := Handled_Statement_Sequence (N); | |
2382 | Check_Missing_Return; | |
2383 | end if; | |
2384 | ||
996ae0b0 RK |
2385 | return; |
2386 | ||
2387 | else | |
82c80734 RD |
2388 | -- Previous entity conflicts with subprogram name. Attempting to |
2389 | -- enter name will post error. | |
996ae0b0 RK |
2390 | |
2391 | Enter_Name (Body_Id); | |
2392 | return; | |
2393 | end if; | |
2394 | ||
82c80734 RD |
2395 | -- Non-generic case, find the subprogram declaration, if one was seen, |
2396 | -- or enter new overloaded entity in the current scope. If the | |
2397 | -- Current_Entity is the Body_Id itself, the unit is being analyzed as | |
2398 | -- part of the context of one of its subunits. No need to redo the | |
2399 | -- analysis. | |
996ae0b0 RK |
2400 | |
2401 | elsif Prev_Id = Body_Id | |
2402 | and then Has_Completion (Body_Id) | |
2403 | then | |
2404 | return; | |
2405 | ||
2406 | else | |
fbf5a39b | 2407 | Body_Id := Analyze_Subprogram_Specification (Body_Spec); |
996ae0b0 RK |
2408 | |
2409 | if Nkind (N) = N_Subprogram_Body_Stub | |
2410 | or else No (Corresponding_Spec (N)) | |
2411 | then | |
d44202ba HK |
2412 | if Is_Private_Concurrent_Primitive (Body_Id) then |
2413 | Spec_Id := Disambiguate_Spec; | |
2414 | else | |
2415 | Spec_Id := Find_Corresponding_Spec (N); | |
2416 | end if; | |
996ae0b0 RK |
2417 | |
2418 | -- If this is a duplicate body, no point in analyzing it | |
2419 | ||
2420 | if Error_Posted (N) then | |
2421 | return; | |
2422 | end if; | |
2423 | ||
82c80734 RD |
2424 | -- A subprogram body should cause freezing of its own declaration, |
2425 | -- but if there was no previous explicit declaration, then the | |
2426 | -- subprogram will get frozen too late (there may be code within | |
2427 | -- the body that depends on the subprogram having been frozen, | |
2428 | -- such as uses of extra formals), so we force it to be frozen | |
76a69663 | 2429 | -- here. Same holds if the body and spec are compilation units. |
cd1c668b ES |
2430 | -- Finally, if the return type is an anonymous access to protected |
2431 | -- subprogram, it must be frozen before the body because its | |
2432 | -- expansion has generated an equivalent type that is used when | |
2433 | -- elaborating the body. | |
996ae0b0 | 2434 | |
885c4871 | 2435 | -- An exception in the case of Ada 2012, AI05-177: The bodies |
ebb6faaa AC |
2436 | -- created for expression functions do not freeze. |
2437 | ||
2438 | if No (Spec_Id) | |
2439 | and then Nkind (Original_Node (N)) /= N_Expression_Function | |
2440 | then | |
996ae0b0 RK |
2441 | Freeze_Before (N, Body_Id); |
2442 | ||
2443 | elsif Nkind (Parent (N)) = N_Compilation_Unit then | |
2444 | Freeze_Before (N, Spec_Id); | |
cd1c668b ES |
2445 | |
2446 | elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then | |
2447 | Freeze_Before (N, Etype (Body_Id)); | |
996ae0b0 | 2448 | end if; |
a38ff9b1 | 2449 | |
996ae0b0 RK |
2450 | else |
2451 | Spec_Id := Corresponding_Spec (N); | |
2452 | end if; | |
2453 | end if; | |
2454 | ||
82c80734 RD |
2455 | -- Do not inline any subprogram that contains nested subprograms, since |
2456 | -- the backend inlining circuit seems to generate uninitialized | |
07fc65c4 | 2457 | -- references in this case. We know this happens in the case of front |
82c80734 RD |
2458 | -- end ZCX support, but it also appears it can happen in other cases as |
2459 | -- well. The backend often rejects attempts to inline in the case of | |
2460 | -- nested procedures anyway, so little if anything is lost by this. | |
2461 | -- Note that this is test is for the benefit of the back-end. There is | |
2462 | -- a separate test for front-end inlining that also rejects nested | |
2463 | -- subprograms. | |
07fc65c4 GB |
2464 | |
2465 | -- Do not do this test if errors have been detected, because in some | |
2466 | -- error cases, this code blows up, and we don't need it anyway if | |
2467 | -- there have been errors, since we won't get to the linker anyway. | |
2468 | ||
82c80734 RD |
2469 | if Comes_From_Source (Body_Id) |
2470 | and then Serious_Errors_Detected = 0 | |
2471 | then | |
07fc65c4 GB |
2472 | P_Ent := Body_Id; |
2473 | loop | |
2474 | P_Ent := Scope (P_Ent); | |
2475 | exit when No (P_Ent) or else P_Ent = Standard_Standard; | |
2476 | ||
fbf5a39b | 2477 | if Is_Subprogram (P_Ent) then |
07fc65c4 GB |
2478 | Set_Is_Inlined (P_Ent, False); |
2479 | ||
2480 | if Comes_From_Source (P_Ent) | |
07fc65c4 GB |
2481 | and then Has_Pragma_Inline (P_Ent) |
2482 | then | |
fbf5a39b AC |
2483 | Cannot_Inline |
2484 | ("cannot inline& (nested subprogram)?", | |
2485 | N, P_Ent); | |
07fc65c4 GB |
2486 | end if; |
2487 | end if; | |
2488 | end loop; | |
2489 | end if; | |
2490 | ||
e660dbf7 JM |
2491 | Check_Inline_Pragma (Spec_Id); |
2492 | ||
701b7fbb RD |
2493 | -- Deal with special case of a fully private operation in the body of |
2494 | -- the protected type. We must create a declaration for the subprogram, | |
2495 | -- in order to attach the protected subprogram that will be used in | |
2496 | -- internal calls. We exclude compiler generated bodies from the | |
2497 | -- expander since the issue does not arise for those cases. | |
07fc65c4 | 2498 | |
996ae0b0 RK |
2499 | if No (Spec_Id) |
2500 | and then Comes_From_Source (N) | |
2501 | and then Is_Protected_Type (Current_Scope) | |
2502 | then | |
47bfea3a | 2503 | Spec_Id := Build_Private_Protected_Declaration (N); |
701b7fbb | 2504 | end if; |
996ae0b0 | 2505 | |
5334d18f | 2506 | -- If a separate spec is present, then deal with freezing issues |
7ca78bba | 2507 | |
701b7fbb | 2508 | if Present (Spec_Id) then |
996ae0b0 | 2509 | Spec_Decl := Unit_Declaration_Node (Spec_Id); |
758c442c | 2510 | Verify_Overriding_Indicator; |
5d37ba92 ES |
2511 | |
2512 | -- In general, the spec will be frozen when we start analyzing the | |
2513 | -- body. However, for internally generated operations, such as | |
2514 | -- wrapper functions for inherited operations with controlling | |
164e06c6 AC |
2515 | -- results, the spec may not have been frozen by the time we expand |
2516 | -- the freeze actions that include the bodies. In particular, extra | |
2517 | -- formals for accessibility or for return-in-place may need to be | |
2518 | -- generated. Freeze nodes, if any, are inserted before the current | |
2519 | -- body. These freeze actions are also needed in ASIS mode to enable | |
2520 | -- the proper back-annotations. | |
5d37ba92 ES |
2521 | |
2522 | if not Is_Frozen (Spec_Id) | |
7134062a | 2523 | and then (Expander_Active or ASIS_Mode) |
5d37ba92 ES |
2524 | then |
2525 | -- Force the generation of its freezing node to ensure proper | |
2526 | -- management of access types in the backend. | |
2527 | ||
2528 | -- This is definitely needed for some cases, but it is not clear | |
2529 | -- why, to be investigated further??? | |
2530 | ||
2531 | Set_Has_Delayed_Freeze (Spec_Id); | |
6b958cec | 2532 | Freeze_Before (N, Spec_Id); |
5d37ba92 | 2533 | end if; |
996ae0b0 RK |
2534 | end if; |
2535 | ||
a5d83d61 AC |
2536 | -- Mark presence of postcondition procedure in current scope and mark |
2537 | -- the procedure itself as needing debug info. The latter is important | |
2538 | -- when analyzing decision coverage (for example, for MC/DC coverage). | |
7ca78bba | 2539 | |
0dabde3a ES |
2540 | if Chars (Body_Id) = Name_uPostconditions then |
2541 | Set_Has_Postconditions (Current_Scope); | |
a5d83d61 | 2542 | Set_Debug_Info_Needed (Body_Id); |
0dabde3a ES |
2543 | end if; |
2544 | ||
996ae0b0 RK |
2545 | -- Place subprogram on scope stack, and make formals visible. If there |
2546 | -- is a spec, the visible entity remains that of the spec. | |
2547 | ||
2548 | if Present (Spec_Id) then | |
07fc65c4 | 2549 | Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); |
758c442c GD |
2550 | |
2551 | if Is_Child_Unit (Spec_Id) then | |
2552 | Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False); | |
2553 | end if; | |
2554 | ||
fbf5a39b AC |
2555 | if Style_Check then |
2556 | Style.Check_Identifier (Body_Id, Spec_Id); | |
2557 | end if; | |
996ae0b0 RK |
2558 | |
2559 | Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); | |
2560 | Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id)); | |
2561 | ||
f937473f | 2562 | if Is_Abstract_Subprogram (Spec_Id) then |
ed2233dc | 2563 | Error_Msg_N ("an abstract subprogram cannot have a body", N); |
996ae0b0 | 2564 | return; |
21d27997 | 2565 | |
996ae0b0 RK |
2566 | else |
2567 | Set_Convention (Body_Id, Convention (Spec_Id)); | |
2568 | Set_Has_Completion (Spec_Id); | |
2569 | ||
2570 | if Is_Protected_Type (Scope (Spec_Id)) then | |
21d27997 | 2571 | Prot_Typ := Scope (Spec_Id); |
996ae0b0 RK |
2572 | end if; |
2573 | ||
2574 | -- If this is a body generated for a renaming, do not check for | |
2575 | -- full conformance. The check is redundant, because the spec of | |
2576 | -- the body is a copy of the spec in the renaming declaration, | |
2577 | -- and the test can lead to spurious errors on nested defaults. | |
2578 | ||
2579 | if Present (Spec_Decl) | |
996ae0b0 | 2580 | and then not Comes_From_Source (N) |
93a81b02 GB |
2581 | and then |
2582 | (Nkind (Original_Node (Spec_Decl)) = | |
d2f97d3e GB |
2583 | N_Subprogram_Renaming_Declaration |
2584 | or else (Present (Corresponding_Body (Spec_Decl)) | |
2585 | and then | |
2586 | Nkind (Unit_Declaration_Node | |
2587 | (Corresponding_Body (Spec_Decl))) = | |
2588 | N_Subprogram_Renaming_Declaration)) | |
996ae0b0 RK |
2589 | then |
2590 | Conformant := True; | |
cabe9abc AC |
2591 | |
2592 | -- Conversely, the spec may have been generated for specless body | |
2593 | -- with an inline pragma. | |
2594 | ||
2595 | elsif Comes_From_Source (N) | |
2596 | and then not Comes_From_Source (Spec_Id) | |
2597 | and then Has_Pragma_Inline (Spec_Id) | |
2598 | then | |
2599 | Conformant := True; | |
76a69663 | 2600 | |
996ae0b0 RK |
2601 | else |
2602 | Check_Conformance | |
2603 | (Body_Id, Spec_Id, | |
76a69663 | 2604 | Fully_Conformant, True, Conformant, Body_Id); |
996ae0b0 RK |
2605 | end if; |
2606 | ||
2607 | -- If the body is not fully conformant, we have to decide if we | |
2608 | -- should analyze it or not. If it has a really messed up profile | |
2609 | -- then we probably should not analyze it, since we will get too | |
2610 | -- many bogus messages. | |
2611 | ||
2612 | -- Our decision is to go ahead in the non-fully conformant case | |
2613 | -- only if it is at least mode conformant with the spec. Note | |
2614 | -- that the call to Check_Fully_Conformant has issued the proper | |
2615 | -- error messages to complain about the lack of conformance. | |
2616 | ||
2617 | if not Conformant | |
2618 | and then not Mode_Conformant (Body_Id, Spec_Id) | |
2619 | then | |
2620 | return; | |
2621 | end if; | |
2622 | end if; | |
2623 | ||
996ae0b0 | 2624 | if Spec_Id /= Body_Id then |
fbf5a39b | 2625 | Reference_Body_Formals (Spec_Id, Body_Id); |
996ae0b0 RK |
2626 | end if; |
2627 | ||
2628 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
2629 | Set_Corresponding_Spec (N, Spec_Id); | |
758c442c | 2630 | |
5d37ba92 ES |
2631 | -- Ada 2005 (AI-345): If the operation is a primitive operation |
2632 | -- of a concurrent type, the type of the first parameter has been | |
2633 | -- replaced with the corresponding record, which is the proper | |
2634 | -- run-time structure to use. However, within the body there may | |
2635 | -- be uses of the formals that depend on primitive operations | |
2636 | -- of the type (in particular calls in prefixed form) for which | |
2637 | -- we need the original concurrent type. The operation may have | |
2638 | -- several controlling formals, so the replacement must be done | |
2639 | -- for all of them. | |
758c442c GD |
2640 | |
2641 | if Comes_From_Source (Spec_Id) | |
2642 | and then Present (First_Entity (Spec_Id)) | |
2643 | and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type | |
2644 | and then Is_Tagged_Type (Etype (First_Entity (Spec_Id))) | |
5d37ba92 | 2645 | and then |
ce2b6ba5 | 2646 | Present (Interfaces (Etype (First_Entity (Spec_Id)))) |
5d37ba92 ES |
2647 | and then |
2648 | Present | |
21d27997 RD |
2649 | (Corresponding_Concurrent_Type |
2650 | (Etype (First_Entity (Spec_Id)))) | |
758c442c | 2651 | then |
5d37ba92 ES |
2652 | declare |
2653 | Typ : constant Entity_Id := Etype (First_Entity (Spec_Id)); | |
2654 | Form : Entity_Id; | |
2655 | ||
2656 | begin | |
2657 | Form := First_Formal (Spec_Id); | |
2658 | while Present (Form) loop | |
2659 | if Etype (Form) = Typ then | |
2660 | Set_Etype (Form, Corresponding_Concurrent_Type (Typ)); | |
2661 | end if; | |
2662 | ||
2663 | Next_Formal (Form); | |
2664 | end loop; | |
2665 | end; | |
758c442c GD |
2666 | end if; |
2667 | ||
21d27997 RD |
2668 | -- Make the formals visible, and place subprogram on scope stack. |
2669 | -- This is also the point at which we set Last_Real_Spec_Entity | |
2670 | -- to mark the entities which will not be moved to the body. | |
758c442c | 2671 | |
996ae0b0 | 2672 | Install_Formals (Spec_Id); |
21d27997 | 2673 | Last_Real_Spec_Entity := Last_Entity (Spec_Id); |
0a36105d | 2674 | Push_Scope (Spec_Id); |
996ae0b0 RK |
2675 | |
2676 | -- Make sure that the subprogram is immediately visible. For | |
2677 | -- child units that have no separate spec this is indispensable. | |
2678 | -- Otherwise it is safe albeit redundant. | |
2679 | ||
2680 | Set_Is_Immediately_Visible (Spec_Id); | |
2681 | end if; | |
2682 | ||
2683 | Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id); | |
2684 | Set_Ekind (Body_Id, E_Subprogram_Body); | |
2685 | Set_Scope (Body_Id, Scope (Spec_Id)); | |
ec4867fa | 2686 | Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); |
996ae0b0 RK |
2687 | |
2688 | -- Case of subprogram body with no previous spec | |
2689 | ||
2690 | else | |
3e5daac4 AC |
2691 | -- Check for style warning required |
2692 | ||
996ae0b0 | 2693 | if Style_Check |
3e5daac4 AC |
2694 | |
2695 | -- Only apply check for source level subprograms for which checks | |
2696 | -- have not been suppressed. | |
2697 | ||
996ae0b0 RK |
2698 | and then Comes_From_Source (Body_Id) |
2699 | and then not Suppress_Style_Checks (Body_Id) | |
3e5daac4 AC |
2700 | |
2701 | -- No warnings within an instance | |
2702 | ||
996ae0b0 | 2703 | and then not In_Instance |
3e5daac4 | 2704 | |
b0186f71 | 2705 | -- No warnings for expression functions |
3e5daac4 | 2706 | |
b0186f71 | 2707 | and then Nkind (Original_Node (N)) /= N_Expression_Function |
996ae0b0 RK |
2708 | then |
2709 | Style.Body_With_No_Spec (N); | |
2710 | end if; | |
2711 | ||
2712 | New_Overloaded_Entity (Body_Id); | |
2713 | ||
2714 | if Nkind (N) /= N_Subprogram_Body_Stub then | |
2715 | Set_Acts_As_Spec (N); | |
2716 | Generate_Definition (Body_Id); | |
dac3bede | 2717 | Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id))); |
fbf5a39b AC |
2718 | Generate_Reference |
2719 | (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True); | |
996ae0b0 | 2720 | Install_Formals (Body_Id); |
0a36105d | 2721 | Push_Scope (Body_Id); |
996ae0b0 | 2722 | end if; |
dbe36d67 AC |
2723 | |
2724 | -- For stubs and bodies with no previous spec, generate references to | |
2725 | -- formals. | |
2726 | ||
2727 | Generate_Reference_To_Formals (Body_Id); | |
996ae0b0 RK |
2728 | end if; |
2729 | ||
76a69663 ES |
2730 | -- If the return type is an anonymous access type whose designated type |
2731 | -- is the limited view of a class-wide type and the non-limited view is | |
2732 | -- available, update the return type accordingly. | |
ec4867fa | 2733 | |
0791fbe9 | 2734 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
2735 | and then Comes_From_Source (N) |
2736 | then | |
2737 | declare | |
ec4867fa | 2738 | Etyp : Entity_Id; |
0a36105d | 2739 | Rtyp : Entity_Id; |
ec4867fa ES |
2740 | |
2741 | begin | |
0a36105d JM |
2742 | Rtyp := Etype (Current_Scope); |
2743 | ||
2744 | if Ekind (Rtyp) = E_Anonymous_Access_Type then | |
2745 | Etyp := Directly_Designated_Type (Rtyp); | |
2746 | ||
2747 | if Is_Class_Wide_Type (Etyp) | |
2748 | and then From_With_Type (Etyp) | |
2749 | then | |
2750 | Set_Directly_Designated_Type | |
2751 | (Etype (Current_Scope), Available_View (Etyp)); | |
2752 | end if; | |
2753 | end if; | |
ec4867fa ES |
2754 | end; |
2755 | end if; | |
2756 | ||
996ae0b0 RK |
2757 | -- If this is the proper body of a stub, we must verify that the stub |
2758 | -- conforms to the body, and to the previous spec if one was present. | |
dbe36d67 | 2759 | -- We know already that the body conforms to that spec. This test is |
996ae0b0 RK |
2760 | -- only required for subprograms that come from source. |
2761 | ||
2762 | if Nkind (Parent (N)) = N_Subunit | |
2763 | and then Comes_From_Source (N) | |
2764 | and then not Error_Posted (Body_Id) | |
e895b435 ES |
2765 | and then Nkind (Corresponding_Stub (Parent (N))) = |
2766 | N_Subprogram_Body_Stub | |
996ae0b0 RK |
2767 | then |
2768 | declare | |
fbf5a39b AC |
2769 | Old_Id : constant Entity_Id := |
2770 | Defining_Entity | |
2771 | (Specification (Corresponding_Stub (Parent (N)))); | |
2772 | ||
996ae0b0 | 2773 | Conformant : Boolean := False; |
996ae0b0 RK |
2774 | |
2775 | begin | |
2776 | if No (Spec_Id) then | |
2777 | Check_Fully_Conformant (Body_Id, Old_Id); | |
2778 | ||
2779 | else | |
2780 | Check_Conformance | |
2781 | (Body_Id, Old_Id, Fully_Conformant, False, Conformant); | |
2782 | ||
2783 | if not Conformant then | |
2784 | ||
dbe36d67 AC |
2785 | -- The stub was taken to be a new declaration. Indicate that |
2786 | -- it lacks a body. | |
996ae0b0 RK |
2787 | |
2788 | Set_Has_Completion (Old_Id, False); | |
2789 | end if; | |
2790 | end if; | |
2791 | end; | |
2792 | end if; | |
2793 | ||
2794 | Set_Has_Completion (Body_Id); | |
2795 | Check_Eliminated (Body_Id); | |
2796 | ||
2797 | if Nkind (N) = N_Subprogram_Body_Stub then | |
2798 | return; | |
2799 | ||
ec4867fa | 2800 | elsif Present (Spec_Id) |
996ae0b0 | 2801 | and then Expander_Active |
e660dbf7 | 2802 | and then |
800621e0 | 2803 | (Has_Pragma_Inline_Always (Spec_Id) |
e660dbf7 | 2804 | or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)) |
996ae0b0 | 2805 | then |
e660dbf7 | 2806 | Build_Body_To_Inline (N, Spec_Id); |
996ae0b0 RK |
2807 | end if; |
2808 | ||
0ab80019 | 2809 | -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis |
dbe36d67 | 2810 | -- of the specification we have to install the private withed units. |
21d27997 | 2811 | -- This holds for child units as well. |
9bc856dd AC |
2812 | |
2813 | if Is_Compilation_Unit (Body_Id) | |
21d27997 | 2814 | or else Nkind (Parent (N)) = N_Compilation_Unit |
9bc856dd AC |
2815 | then |
2816 | Install_Private_With_Clauses (Body_Id); | |
2817 | end if; | |
2818 | ||
ec4867fa ES |
2819 | Check_Anonymous_Return; |
2820 | ||
fdce4bb7 JM |
2821 | -- Set the Protected_Formal field of each extra formal of the protected |
2822 | -- subprogram to reference the corresponding extra formal of the | |
2823 | -- subprogram that implements it. For regular formals this occurs when | |
2824 | -- the protected subprogram's declaration is expanded, but the extra | |
2825 | -- formals don't get created until the subprogram is frozen. We need to | |
2826 | -- do this before analyzing the protected subprogram's body so that any | |
2827 | -- references to the original subprogram's extra formals will be changed | |
2828 | -- refer to the implementing subprogram's formals (see Expand_Formal). | |
2829 | ||
2830 | if Present (Spec_Id) | |
2831 | and then Is_Protected_Type (Scope (Spec_Id)) | |
2832 | and then Present (Protected_Body_Subprogram (Spec_Id)) | |
2833 | then | |
2834 | declare | |
2835 | Impl_Subp : constant Entity_Id := | |
2836 | Protected_Body_Subprogram (Spec_Id); | |
2837 | Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id); | |
2838 | Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp); | |
fdce4bb7 JM |
2839 | begin |
2840 | while Present (Prot_Ext_Formal) loop | |
2841 | pragma Assert (Present (Impl_Ext_Formal)); | |
fdce4bb7 | 2842 | Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal); |
fdce4bb7 JM |
2843 | Next_Formal_With_Extras (Prot_Ext_Formal); |
2844 | Next_Formal_With_Extras (Impl_Ext_Formal); | |
2845 | end loop; | |
2846 | end; | |
2847 | end if; | |
2848 | ||
0868e09c | 2849 | -- Now we can go on to analyze the body |
996ae0b0 RK |
2850 | |
2851 | HSS := Handled_Statement_Sequence (N); | |
2852 | Set_Actual_Subtypes (N, Current_Scope); | |
21d27997 | 2853 | |
483361a6 AC |
2854 | -- Deal with preconditions and postconditions. In formal verification |
2855 | -- mode, we keep pre- and postconditions attached to entities rather | |
2856 | -- than inserted in the code, in order to facilitate a distinct | |
2857 | -- treatment for them. | |
21d27997 | 2858 | |
56812278 | 2859 | if not Alfa_Mode then |
483361a6 AC |
2860 | Process_PPCs (N, Spec_Id, Body_Id); |
2861 | end if; | |
21d27997 | 2862 | |
f3d0f304 | 2863 | -- Add a declaration for the Protection object, renaming declarations |
21d27997 RD |
2864 | -- for discriminals and privals and finally a declaration for the entry |
2865 | -- family index (if applicable). This form of early expansion is done | |
2866 | -- when the Expander is active because Install_Private_Data_Declarations | |
81bf2382 AC |
2867 | -- references entities which were created during regular expansion. The |
2868 | -- body may be the rewritting of an expression function, and we need to | |
2869 | -- verify that the original node is in the source. | |
21d27997 | 2870 | |
da94696d | 2871 | if Full_Expander_Active |
13a0b1e8 | 2872 | and then Comes_From_Source (Original_Node (N)) |
21d27997 RD |
2873 | and then Present (Prot_Typ) |
2874 | and then Present (Spec_Id) | |
2875 | and then not Is_Eliminated (Spec_Id) | |
2876 | then | |
2877 | Install_Private_Data_Declarations | |
2878 | (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N)); | |
2879 | end if; | |
2880 | ||
5dcab3ca AC |
2881 | -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context |
2882 | -- may now appear in parameter and result profiles. Since the analysis | |
2883 | -- of a subprogram body may use the parameter and result profile of the | |
2884 | -- spec, swap any limited views with their non-limited counterpart. | |
2885 | ||
2886 | if Ada_Version >= Ada_2012 then | |
2887 | Exchange_Limited_Views (Spec_Id); | |
2888 | end if; | |
2889 | ||
21d27997 RD |
2890 | -- Analyze the declarations (this call will analyze the precondition |
2891 | -- Check pragmas we prepended to the list, as well as the declaration | |
2892 | -- of the _Postconditions procedure). | |
2893 | ||
996ae0b0 | 2894 | Analyze_Declarations (Declarations (N)); |
21d27997 RD |
2895 | |
2896 | -- Check completion, and analyze the statements | |
2897 | ||
996ae0b0 | 2898 | Check_Completion; |
33931112 | 2899 | Inspect_Deferred_Constant_Completion (Declarations (N)); |
996ae0b0 | 2900 | Analyze (HSS); |
21d27997 RD |
2901 | |
2902 | -- Deal with end of scope processing for the body | |
2903 | ||
07fc65c4 | 2904 | Process_End_Label (HSS, 't', Current_Scope); |
996ae0b0 RK |
2905 | End_Scope; |
2906 | Check_Subprogram_Order (N); | |
c37bb106 | 2907 | Set_Analyzed (Body_Id); |
996ae0b0 RK |
2908 | |
2909 | -- If we have a separate spec, then the analysis of the declarations | |
2910 | -- caused the entities in the body to be chained to the spec id, but | |
2911 | -- we want them chained to the body id. Only the formal parameters | |
2912 | -- end up chained to the spec id in this case. | |
2913 | ||
2914 | if Present (Spec_Id) then | |
2915 | ||
d39d6bb8 | 2916 | -- We must conform to the categorization of our spec |
996ae0b0 | 2917 | |
d39d6bb8 | 2918 | Validate_Categorization_Dependency (N, Spec_Id); |
996ae0b0 | 2919 | |
d39d6bb8 RD |
2920 | -- And if this is a child unit, the parent units must conform |
2921 | ||
2922 | if Is_Child_Unit (Spec_Id) then | |
996ae0b0 RK |
2923 | Validate_Categorization_Dependency |
2924 | (Unit_Declaration_Node (Spec_Id), Spec_Id); | |
2925 | end if; | |
2926 | ||
21d27997 RD |
2927 | -- Here is where we move entities from the spec to the body |
2928 | ||
2929 | -- Case where there are entities that stay with the spec | |
2930 | ||
2931 | if Present (Last_Real_Spec_Entity) then | |
2932 | ||
dbe36d67 AC |
2933 | -- No body entities (happens when the only real spec entities come |
2934 | -- from precondition and postcondition pragmas). | |
21d27997 RD |
2935 | |
2936 | if No (Last_Entity (Body_Id)) then | |
2937 | Set_First_Entity | |
2938 | (Body_Id, Next_Entity (Last_Real_Spec_Entity)); | |
2939 | ||
2940 | -- Body entities present (formals), so chain stuff past them | |
2941 | ||
2942 | else | |
2943 | Set_Next_Entity | |
2944 | (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity)); | |
2945 | end if; | |
2946 | ||
2947 | Set_Next_Entity (Last_Real_Spec_Entity, Empty); | |
996ae0b0 | 2948 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); |
21d27997 RD |
2949 | Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity); |
2950 | ||
dbe36d67 AC |
2951 | -- Case where there are no spec entities, in this case there can be |
2952 | -- no body entities either, so just move everything. | |
996ae0b0 RK |
2953 | |
2954 | else | |
21d27997 | 2955 | pragma Assert (No (Last_Entity (Body_Id))); |
996ae0b0 RK |
2956 | Set_First_Entity (Body_Id, First_Entity (Spec_Id)); |
2957 | Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); | |
2958 | Set_First_Entity (Spec_Id, Empty); | |
2959 | Set_Last_Entity (Spec_Id, Empty); | |
2960 | end if; | |
2961 | end if; | |
2962 | ||
7665e4bd | 2963 | Check_Missing_Return; |
996ae0b0 | 2964 | |
82c80734 | 2965 | -- Now we are going to check for variables that are never modified in |
76a69663 ES |
2966 | -- the body of the procedure. But first we deal with a special case |
2967 | -- where we want to modify this check. If the body of the subprogram | |
2968 | -- starts with a raise statement or its equivalent, or if the body | |
2969 | -- consists entirely of a null statement, then it is pretty obvious | |
2970 | -- that it is OK to not reference the parameters. For example, this | |
2971 | -- might be the following common idiom for a stubbed function: | |
82c80734 RD |
2972 | -- statement of the procedure raises an exception. In particular this |
2973 | -- deals with the common idiom of a stubbed function, which might | |
dbe36d67 | 2974 | -- appear as something like: |
fbf5a39b AC |
2975 | |
2976 | -- function F (A : Integer) return Some_Type; | |
2977 | -- X : Some_Type; | |
2978 | -- begin | |
2979 | -- raise Program_Error; | |
2980 | -- return X; | |
2981 | -- end F; | |
2982 | ||
76a69663 ES |
2983 | -- Here the purpose of X is simply to satisfy the annoying requirement |
2984 | -- in Ada that there be at least one return, and we certainly do not | |
2985 | -- want to go posting warnings on X that it is not initialized! On | |
2986 | -- the other hand, if X is entirely unreferenced that should still | |
2987 | -- get a warning. | |
2988 | ||
2989 | -- What we do is to detect these cases, and if we find them, flag the | |
2990 | -- subprogram as being Is_Trivial_Subprogram and then use that flag to | |
2991 | -- suppress unwanted warnings. For the case of the function stub above | |
2992 | -- we have a special test to set X as apparently assigned to suppress | |
2993 | -- the warning. | |
996ae0b0 RK |
2994 | |
2995 | declare | |
800621e0 | 2996 | Stm : Node_Id; |
996ae0b0 RK |
2997 | |
2998 | begin | |
0a36105d JM |
2999 | -- Skip initial labels (for one thing this occurs when we are in |
3000 | -- front end ZCX mode, but in any case it is irrelevant), and also | |
3001 | -- initial Push_xxx_Error_Label nodes, which are also irrelevant. | |
fbf5a39b | 3002 | |
800621e0 | 3003 | Stm := First (Statements (HSS)); |
0a36105d JM |
3004 | while Nkind (Stm) = N_Label |
3005 | or else Nkind (Stm) in N_Push_xxx_Label | |
3006 | loop | |
996ae0b0 | 3007 | Next (Stm); |
0a36105d | 3008 | end loop; |
996ae0b0 | 3009 | |
fbf5a39b AC |
3010 | -- Do the test on the original statement before expansion |
3011 | ||
3012 | declare | |
3013 | Ostm : constant Node_Id := Original_Node (Stm); | |
3014 | ||
3015 | begin | |
76a69663 | 3016 | -- If explicit raise statement, turn on flag |
fbf5a39b AC |
3017 | |
3018 | if Nkind (Ostm) = N_Raise_Statement then | |
76a69663 ES |
3019 | Set_Trivial_Subprogram (Stm); |
3020 | ||
f3d57416 | 3021 | -- If null statement, and no following statements, turn on flag |
76a69663 ES |
3022 | |
3023 | elsif Nkind (Stm) = N_Null_Statement | |
3024 | and then Comes_From_Source (Stm) | |
3025 | and then No (Next (Stm)) | |
3026 | then | |
3027 | Set_Trivial_Subprogram (Stm); | |
fbf5a39b AC |
3028 | |
3029 | -- Check for explicit call cases which likely raise an exception | |
3030 | ||
3031 | elsif Nkind (Ostm) = N_Procedure_Call_Statement then | |
3032 | if Is_Entity_Name (Name (Ostm)) then | |
3033 | declare | |
3034 | Ent : constant Entity_Id := Entity (Name (Ostm)); | |
3035 | ||
3036 | begin | |
3037 | -- If the procedure is marked No_Return, then likely it | |
3038 | -- raises an exception, but in any case it is not coming | |
76a69663 | 3039 | -- back here, so turn on the flag. |
fbf5a39b | 3040 | |
f46faa08 AC |
3041 | if Present (Ent) |
3042 | and then Ekind (Ent) = E_Procedure | |
fbf5a39b AC |
3043 | and then No_Return (Ent) |
3044 | then | |
76a69663 | 3045 | Set_Trivial_Subprogram (Stm); |
fbf5a39b AC |
3046 | end if; |
3047 | end; | |
3048 | end if; | |
3049 | end if; | |
3050 | end; | |
996ae0b0 RK |
3051 | end; |
3052 | ||
3053 | -- Check for variables that are never modified | |
3054 | ||
3055 | declare | |
3056 | E1, E2 : Entity_Id; | |
3057 | ||
3058 | begin | |
fbf5a39b | 3059 | -- If there is a separate spec, then transfer Never_Set_In_Source |
996ae0b0 RK |
3060 | -- flags from out parameters to the corresponding entities in the |
3061 | -- body. The reason we do that is we want to post error flags on | |
3062 | -- the body entities, not the spec entities. | |
3063 | ||
3064 | if Present (Spec_Id) then | |
3065 | E1 := First_Entity (Spec_Id); | |
996ae0b0 RK |
3066 | while Present (E1) loop |
3067 | if Ekind (E1) = E_Out_Parameter then | |
3068 | E2 := First_Entity (Body_Id); | |
fbf5a39b | 3069 | while Present (E2) loop |
996ae0b0 RK |
3070 | exit when Chars (E1) = Chars (E2); |
3071 | Next_Entity (E2); | |
3072 | end loop; | |
3073 | ||
fbf5a39b AC |
3074 | if Present (E2) then |
3075 | Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1)); | |
3076 | end if; | |
996ae0b0 RK |
3077 | end if; |
3078 | ||
3079 | Next_Entity (E1); | |
3080 | end loop; | |
3081 | end if; | |
3082 | ||
0868e09c RD |
3083 | -- Check references in body unless it was deleted. Note that the |
3084 | -- check of Body_Deleted here is not just for efficiency, it is | |
3085 | -- necessary to avoid junk warnings on formal parameters. | |
3086 | ||
3087 | if not Body_Deleted then | |
3088 | Check_References (Body_Id); | |
3089 | end if; | |
996ae0b0 | 3090 | end; |
b1b543d2 | 3091 | end Analyze_Subprogram_Body_Helper; |
996ae0b0 RK |
3092 | |
3093 | ------------------------------------ | |
3094 | -- Analyze_Subprogram_Declaration -- | |
3095 | ------------------------------------ | |
3096 | ||
3097 | procedure Analyze_Subprogram_Declaration (N : Node_Id) is | |
5d5832bc | 3098 | Loc : constant Source_Ptr := Sloc (N); |
0f1a6a0b | 3099 | Scop : constant Entity_Id := Current_Scope; |
5d5832bc AC |
3100 | Designator : Entity_Id; |
3101 | Form : Node_Id; | |
5d5832bc | 3102 | Null_Body : Node_Id := Empty; |
996ae0b0 RK |
3103 | |
3104 | -- Start of processing for Analyze_Subprogram_Declaration | |
3105 | ||
3106 | begin | |
2ba431e5 | 3107 | -- Null procedures are not allowed in SPARK |
daec8eeb | 3108 | |
fe5d3068 | 3109 | if Nkind (Specification (N)) = N_Procedure_Specification |
daec8eeb YM |
3110 | and then Null_Present (Specification (N)) |
3111 | then | |
2ba431e5 | 3112 | Check_SPARK_Restriction ("null procedure is not allowed", N); |
daec8eeb YM |
3113 | end if; |
3114 | ||
349ff68f | 3115 | -- For a null procedure, capture the profile before analysis, for |
c159409f AC |
3116 | -- expansion at the freeze point and at each point of call. The body |
3117 | -- will only be used if the procedure has preconditions. In that case | |
3118 | -- the body is analyzed at the freeze point. | |
5d5832bc AC |
3119 | |
3120 | if Nkind (Specification (N)) = N_Procedure_Specification | |
3121 | and then Null_Present (Specification (N)) | |
3122 | and then Expander_Active | |
3123 | then | |
3124 | Null_Body := | |
3125 | Make_Subprogram_Body (Loc, | |
3126 | Specification => | |
3127 | New_Copy_Tree (Specification (N)), | |
349ff68f AC |
3128 | Declarations => |
3129 | New_List, | |
5d5832bc AC |
3130 | Handled_Statement_Sequence => |
3131 | Make_Handled_Sequence_Of_Statements (Loc, | |
3132 | Statements => New_List (Make_Null_Statement (Loc)))); | |
3133 | ||
01957849 | 3134 | -- Create new entities for body and formals |
5d5832bc AC |
3135 | |
3136 | Set_Defining_Unit_Name (Specification (Null_Body), | |
3137 | Make_Defining_Identifier (Loc, Chars (Defining_Entity (N)))); | |
3138 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
3139 | ||
3140 | Form := First (Parameter_Specifications (Specification (Null_Body))); | |
3141 | while Present (Form) loop | |
3142 | Set_Defining_Identifier (Form, | |
3143 | Make_Defining_Identifier (Loc, | |
3144 | Chars (Defining_Identifier (Form)))); | |
718deaf1 AC |
3145 | |
3146 | -- Resolve the types of the formals now, because the freeze point | |
3147 | -- may appear in a different context, e.g. an instantiation. | |
3148 | ||
3149 | if Nkind (Parameter_Type (Form)) /= N_Access_Definition then | |
3150 | Find_Type (Parameter_Type (Form)); | |
3151 | ||
3152 | elsif | |
3153 | No (Access_To_Subprogram_Definition (Parameter_Type (Form))) | |
3154 | then | |
3155 | Find_Type (Subtype_Mark (Parameter_Type (Form))); | |
3156 | ||
3157 | else | |
3158 | ||
3159 | -- the case of a null procedure with a formal that is an | |
3160 | -- access_to_subprogram type, and that is used as an actual | |
3161 | -- in an instantiation is left to the enthusiastic reader. | |
3162 | ||
3163 | null; | |
3164 | end if; | |
3165 | ||
5d5832bc AC |
3166 | Next (Form); |
3167 | end loop; | |
3168 | ||
3169 | if Is_Protected_Type (Current_Scope) then | |
ed2233dc | 3170 | Error_Msg_N ("protected operation cannot be a null procedure", N); |
5d5832bc AC |
3171 | end if; |
3172 | end if; | |
3173 | ||
beacce02 | 3174 | Designator := Analyze_Subprogram_Specification (Specification (N)); |
5d5832bc | 3175 | Generate_Definition (Designator); |
dac3bede | 3176 | -- ??? why this call, already in Analyze_Subprogram_Specification |
5d5832bc | 3177 | |
b1b543d2 BD |
3178 | if Debug_Flag_C then |
3179 | Write_Str ("==> subprogram spec "); | |
3180 | Write_Name (Chars (Designator)); | |
3181 | Write_Str (" from "); | |
3182 | Write_Location (Sloc (N)); | |
3183 | Write_Eol; | |
3184 | Indent; | |
3185 | end if; | |
3186 | ||
5d5832bc AC |
3187 | if Nkind (Specification (N)) = N_Procedure_Specification |
3188 | and then Null_Present (Specification (N)) | |
3189 | then | |
3190 | Set_Has_Completion (Designator); | |
996ae0b0 | 3191 | |
5d5832bc AC |
3192 | if Present (Null_Body) then |
3193 | Set_Corresponding_Body (N, Defining_Entity (Null_Body)); | |
3194 | Set_Body_To_Inline (N, Null_Body); | |
3195 | Set_Is_Inlined (Designator); | |
3196 | end if; | |
3197 | end if; | |
996ae0b0 RK |
3198 | |
3199 | Validate_RCI_Subprogram_Declaration (N); | |
996ae0b0 RK |
3200 | New_Overloaded_Entity (Designator); |
3201 | Check_Delayed_Subprogram (Designator); | |
fbf5a39b | 3202 | |
6ca063eb AC |
3203 | -- If the type of the first formal of the current subprogram is a |
3204 | -- nongeneric tagged private type, mark the subprogram as being a | |
3205 | -- private primitive. Ditto if this is a function with controlling | |
b7d5e87b AC |
3206 | -- result, and the return type is currently private. In both cases, |
3207 | -- the type of the controlling argument or result must be in the | |
3208 | -- current scope for the operation to be primitive. | |
6ca063eb AC |
3209 | |
3210 | if Has_Controlling_Result (Designator) | |
3211 | and then Is_Private_Type (Etype (Designator)) | |
b7d5e87b | 3212 | and then Scope (Etype (Designator)) = Current_Scope |
6ca063eb AC |
3213 | and then not Is_Generic_Actual_Type (Etype (Designator)) |
3214 | then | |
3215 | Set_Is_Private_Primitive (Designator); | |
d44202ba | 3216 | |
6ca063eb | 3217 | elsif Present (First_Formal (Designator)) then |
d44202ba HK |
3218 | declare |
3219 | Formal_Typ : constant Entity_Id := | |
3220 | Etype (First_Formal (Designator)); | |
3221 | begin | |
3222 | Set_Is_Private_Primitive (Designator, | |
3223 | Is_Tagged_Type (Formal_Typ) | |
b7d5e87b | 3224 | and then Scope (Formal_Typ) = Current_Scope |
d44202ba HK |
3225 | and then Is_Private_Type (Formal_Typ) |
3226 | and then not Is_Generic_Actual_Type (Formal_Typ)); | |
3227 | end; | |
3228 | end if; | |
3229 | ||
ec4867fa ES |
3230 | -- Ada 2005 (AI-251): Abstract interface primitives must be abstract |
3231 | -- or null. | |
3232 | ||
0791fbe9 | 3233 | if Ada_Version >= Ada_2005 |
ec4867fa ES |
3234 | and then Comes_From_Source (N) |
3235 | and then Is_Dispatching_Operation (Designator) | |
3236 | then | |
3237 | declare | |
3238 | E : Entity_Id; | |
3239 | Etyp : Entity_Id; | |
3240 | ||
3241 | begin | |
3242 | if Has_Controlling_Result (Designator) then | |
3243 | Etyp := Etype (Designator); | |
3244 | ||
3245 | else | |
3246 | E := First_Entity (Designator); | |
3247 | while Present (E) | |
3248 | and then Is_Formal (E) | |
3249 | and then not Is_Controlling_Formal (E) | |
3250 | loop | |
3251 | Next_Entity (E); | |
3252 | end loop; | |
3253 | ||
3254 | Etyp := Etype (E); | |
3255 | end if; | |
3256 | ||
3257 | if Is_Access_Type (Etyp) then | |
3258 | Etyp := Directly_Designated_Type (Etyp); | |
3259 | end if; | |
3260 | ||
3261 | if Is_Interface (Etyp) | |
f937473f | 3262 | and then not Is_Abstract_Subprogram (Designator) |
ec4867fa ES |
3263 | and then not (Ekind (Designator) = E_Procedure |
3264 | and then Null_Present (Specification (N))) | |
3265 | then | |
3266 | Error_Msg_Name_1 := Chars (Defining_Entity (N)); | |
033eaf85 AC |
3267 | |
3268 | -- Specialize error message based on procedures vs. functions, | |
3269 | -- since functions can't be null subprograms. | |
3270 | ||
3271 | if Ekind (Designator) = E_Procedure then | |
3272 | Error_Msg_N | |
3273 | ("interface procedure % must be abstract or null", N); | |
3274 | else | |
3275 | Error_Msg_N ("interface function % must be abstract", N); | |
3276 | end if; | |
ec4867fa ES |
3277 | end if; |
3278 | end; | |
3279 | end if; | |
3280 | ||
fbf5a39b AC |
3281 | -- What is the following code for, it used to be |
3282 | ||
3283 | -- ??? Set_Suppress_Elaboration_Checks | |
3284 | -- ??? (Designator, Elaboration_Checks_Suppressed (Designator)); | |
3285 | ||
3286 | -- The following seems equivalent, but a bit dubious | |
3287 | ||
3288 | if Elaboration_Checks_Suppressed (Designator) then | |
3289 | Set_Kill_Elaboration_Checks (Designator); | |
3290 | end if; | |
996ae0b0 RK |
3291 | |
3292 | if Scop /= Standard_Standard | |
3293 | and then not Is_Child_Unit (Designator) | |
3294 | then | |
fbf5a39b | 3295 | Set_Categorization_From_Scope (Designator, Scop); |
996ae0b0 | 3296 | else |
e895b435 | 3297 | -- For a compilation unit, check for library-unit pragmas |
996ae0b0 | 3298 | |
0a36105d | 3299 | Push_Scope (Designator); |
996ae0b0 RK |
3300 | Set_Categorization_From_Pragmas (N); |
3301 | Validate_Categorization_Dependency (N, Designator); | |
3302 | Pop_Scope; | |
3303 | end if; | |
3304 | ||
3305 | -- For a compilation unit, set body required. This flag will only be | |
3306 | -- reset if a valid Import or Interface pragma is processed later on. | |
3307 | ||
3308 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
3309 | Set_Body_Required (Parent (N), True); | |
758c442c | 3310 | |
0791fbe9 | 3311 | if Ada_Version >= Ada_2005 |
758c442c GD |
3312 | and then Nkind (Specification (N)) = N_Procedure_Specification |
3313 | and then Null_Present (Specification (N)) | |
3314 | then | |
3315 | Error_Msg_N | |
3316 | ("null procedure cannot be declared at library level", N); | |
3317 | end if; | |
996ae0b0 RK |
3318 | end if; |
3319 | ||
fbf5a39b | 3320 | Generate_Reference_To_Formals (Designator); |
996ae0b0 | 3321 | Check_Eliminated (Designator); |
fbf5a39b | 3322 | |
b1b543d2 BD |
3323 | if Debug_Flag_C then |
3324 | Outdent; | |
3325 | Write_Str ("<== subprogram spec "); | |
3326 | Write_Name (Chars (Designator)); | |
3327 | Write_Str (" from "); | |
3328 | Write_Location (Sloc (N)); | |
3329 | Write_Eol; | |
3330 | end if; | |
0f1a6a0b | 3331 | |
1a265e78 AC |
3332 | if Is_Protected_Type (Current_Scope) then |
3333 | ||
3334 | -- Indicate that this is a protected operation, because it may be | |
3335 | -- used in subsequent declarations within the protected type. | |
3336 | ||
3337 | Set_Convention (Designator, Convention_Protected); | |
3338 | end if; | |
3339 | ||
beacce02 | 3340 | List_Inherited_Pre_Post_Aspects (Designator); |
eaba57fb RD |
3341 | |
3342 | if Has_Aspects (N) then | |
3343 | Analyze_Aspect_Specifications (N, Designator); | |
3344 | end if; | |
996ae0b0 RK |
3345 | end Analyze_Subprogram_Declaration; |
3346 | ||
fbf5a39b AC |
3347 | -------------------------------------- |
3348 | -- Analyze_Subprogram_Specification -- | |
3349 | -------------------------------------- | |
3350 | ||
3351 | -- Reminder: N here really is a subprogram specification (not a subprogram | |
3352 | -- declaration). This procedure is called to analyze the specification in | |
3353 | -- both subprogram bodies and subprogram declarations (specs). | |
3354 | ||
3355 | function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is | |
3356 | Designator : constant Entity_Id := Defining_Entity (N); | |
21d27997 | 3357 | Formals : constant List_Id := Parameter_Specifications (N); |
fbf5a39b | 3358 | |
758c442c GD |
3359 | -- Start of processing for Analyze_Subprogram_Specification |
3360 | ||
fbf5a39b | 3361 | begin |
2ba431e5 | 3362 | -- User-defined operator is not allowed in SPARK, except as a renaming |
38171f43 | 3363 | |
db72f10a AC |
3364 | if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol |
3365 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
3366 | then | |
2ba431e5 | 3367 | Check_SPARK_Restriction ("user-defined operator is not allowed", N); |
38171f43 AC |
3368 | end if; |
3369 | ||
3370 | -- Proceed with analysis | |
3371 | ||
fbf5a39b | 3372 | Generate_Definition (Designator); |
dac3bede | 3373 | Set_Contract (Designator, Make_Contract (Sloc (Designator))); |
fbf5a39b AC |
3374 | |
3375 | if Nkind (N) = N_Function_Specification then | |
3376 | Set_Ekind (Designator, E_Function); | |
3377 | Set_Mechanism (Designator, Default_Mechanism); | |
fbf5a39b AC |
3378 | else |
3379 | Set_Ekind (Designator, E_Procedure); | |
3380 | Set_Etype (Designator, Standard_Void_Type); | |
3381 | end if; | |
3382 | ||
800621e0 | 3383 | -- Introduce new scope for analysis of the formals and the return type |
82c80734 RD |
3384 | |
3385 | Set_Scope (Designator, Current_Scope); | |
3386 | ||
fbf5a39b | 3387 | if Present (Formals) then |
0a36105d | 3388 | Push_Scope (Designator); |
fbf5a39b | 3389 | Process_Formals (Formals, N); |
758c442c | 3390 | |
a38ff9b1 ES |
3391 | -- Ada 2005 (AI-345): If this is an overriding operation of an |
3392 | -- inherited interface operation, and the controlling type is | |
3393 | -- a synchronized type, replace the type with its corresponding | |
3394 | -- record, to match the proper signature of an overriding operation. | |
69cb258c AC |
3395 | -- Same processing for an access parameter whose designated type is |
3396 | -- derived from a synchronized interface. | |
758c442c | 3397 | |
0791fbe9 | 3398 | if Ada_Version >= Ada_2005 then |
d44202ba HK |
3399 | declare |
3400 | Formal : Entity_Id; | |
3401 | Formal_Typ : Entity_Id; | |
3402 | Rec_Typ : Entity_Id; | |
69cb258c | 3403 | Desig_Typ : Entity_Id; |
0a36105d | 3404 | |
d44202ba HK |
3405 | begin |
3406 | Formal := First_Formal (Designator); | |
3407 | while Present (Formal) loop | |
3408 | Formal_Typ := Etype (Formal); | |
0a36105d | 3409 | |
d44202ba HK |
3410 | if Is_Concurrent_Type (Formal_Typ) |
3411 | and then Present (Corresponding_Record_Type (Formal_Typ)) | |
3412 | then | |
3413 | Rec_Typ := Corresponding_Record_Type (Formal_Typ); | |
3414 | ||
3415 | if Present (Interfaces (Rec_Typ)) then | |
3416 | Set_Etype (Formal, Rec_Typ); | |
3417 | end if; | |
69cb258c AC |
3418 | |
3419 | elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then | |
3420 | Desig_Typ := Designated_Type (Formal_Typ); | |
3421 | ||
3422 | if Is_Concurrent_Type (Desig_Typ) | |
3423 | and then Present (Corresponding_Record_Type (Desig_Typ)) | |
3424 | then | |
3425 | Rec_Typ := Corresponding_Record_Type (Desig_Typ); | |
3426 | ||
3427 | if Present (Interfaces (Rec_Typ)) then | |
3428 | Set_Directly_Designated_Type (Formal_Typ, Rec_Typ); | |
3429 | end if; | |
3430 | end if; | |
d44202ba HK |
3431 | end if; |
3432 | ||
3433 | Next_Formal (Formal); | |
3434 | end loop; | |
3435 | end; | |
758c442c GD |
3436 | end if; |
3437 | ||
fbf5a39b | 3438 | End_Scope; |
82c80734 | 3439 | |
b66c3ff4 AC |
3440 | -- The subprogram scope is pushed and popped around the processing of |
3441 | -- the return type for consistency with call above to Process_Formals | |
3442 | -- (which itself can call Analyze_Return_Type), and to ensure that any | |
3443 | -- itype created for the return type will be associated with the proper | |
3444 | -- scope. | |
3445 | ||
82c80734 | 3446 | elsif Nkind (N) = N_Function_Specification then |
b66c3ff4 | 3447 | Push_Scope (Designator); |
82c80734 | 3448 | Analyze_Return_Type (N); |
b66c3ff4 | 3449 | End_Scope; |
fbf5a39b AC |
3450 | end if; |
3451 | ||
e606088a AC |
3452 | -- Function case |
3453 | ||
fbf5a39b | 3454 | if Nkind (N) = N_Function_Specification then |
e606088a AC |
3455 | |
3456 | -- Deal with operator symbol case | |
3457 | ||
fbf5a39b AC |
3458 | if Nkind (Designator) = N_Defining_Operator_Symbol then |
3459 | Valid_Operator_Definition (Designator); | |
3460 | end if; | |
3461 | ||
3462 | May_Need_Actuals (Designator); | |
3463 | ||
fe63b1b1 ES |
3464 | -- Ada 2005 (AI-251): If the return type is abstract, verify that |
3465 | -- the subprogram is abstract also. This does not apply to renaming | |
1adaea16 AC |
3466 | -- declarations, where abstractness is inherited, and to subprogram |
3467 | -- bodies generated for stream operations, which become renamings as | |
3468 | -- bodies. | |
2bfb1b72 | 3469 | |
fe63b1b1 ES |
3470 | -- In case of primitives associated with abstract interface types |
3471 | -- the check is applied later (see Analyze_Subprogram_Declaration). | |
ec4867fa | 3472 | |
1adaea16 AC |
3473 | if not Nkind_In (Original_Node (Parent (N)), |
3474 | N_Subprogram_Renaming_Declaration, | |
3475 | N_Abstract_Subprogram_Declaration, | |
3476 | N_Formal_Abstract_Subprogram_Declaration) | |
fbf5a39b | 3477 | then |
2e79de51 AC |
3478 | if Is_Abstract_Type (Etype (Designator)) |
3479 | and then not Is_Interface (Etype (Designator)) | |
3480 | then | |
3481 | Error_Msg_N | |
3482 | ("function that returns abstract type must be abstract", N); | |
3483 | ||
e606088a | 3484 | -- Ada 2012 (AI-0073): Extend this test to subprograms with an |
2e79de51 AC |
3485 | -- access result whose designated type is abstract. |
3486 | ||
3487 | elsif Nkind (Result_Definition (N)) = N_Access_Definition | |
3488 | and then | |
3489 | not Is_Class_Wide_Type (Designated_Type (Etype (Designator))) | |
3490 | and then Is_Abstract_Type (Designated_Type (Etype (Designator))) | |
dbe945f1 | 3491 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
3492 | then |
3493 | Error_Msg_N ("function whose access result designates " | |
3494 | & "abstract type must be abstract", N); | |
3495 | end if; | |
fbf5a39b AC |
3496 | end if; |
3497 | end if; | |
3498 | ||
3499 | return Designator; | |
3500 | end Analyze_Subprogram_Specification; | |
3501 | ||
996ae0b0 RK |
3502 | -------------------------- |
3503 | -- Build_Body_To_Inline -- | |
3504 | -------------------------- | |
3505 | ||
d05ef0ab | 3506 | procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is |
f937473f | 3507 | Decl : constant Node_Id := Unit_Declaration_Node (Subp); |
996ae0b0 RK |
3508 | Original_Body : Node_Id; |
3509 | Body_To_Analyze : Node_Id; | |
3510 | Max_Size : constant := 10; | |
3511 | Stat_Count : Integer := 0; | |
3512 | ||
3513 | function Has_Excluded_Declaration (Decls : List_Id) return Boolean; | |
e895b435 | 3514 | -- Check for declarations that make inlining not worthwhile |
996ae0b0 RK |
3515 | |
3516 | function Has_Excluded_Statement (Stats : List_Id) return Boolean; | |
82c80734 RD |
3517 | -- Check for statements that make inlining not worthwhile: any tasking |
3518 | -- statement, nested at any level. Keep track of total number of | |
3519 | -- elementary statements, as a measure of acceptable size. | |
996ae0b0 RK |
3520 | |
3521 | function Has_Pending_Instantiation return Boolean; | |
f937473f RD |
3522 | -- If some enclosing body contains instantiations that appear before the |
3523 | -- corresponding generic body, the enclosing body has a freeze node so | |
3524 | -- that it can be elaborated after the generic itself. This might | |
996ae0b0 RK |
3525 | -- conflict with subsequent inlinings, so that it is unsafe to try to |
3526 | -- inline in such a case. | |
3527 | ||
c8ef728f | 3528 | function Has_Single_Return return Boolean; |
f937473f RD |
3529 | -- In general we cannot inline functions that return unconstrained type. |
3530 | -- However, we can handle such functions if all return statements return | |
3531 | -- a local variable that is the only declaration in the body of the | |
3532 | -- function. In that case the call can be replaced by that local | |
3533 | -- variable as is done for other inlined calls. | |
c8ef728f | 3534 | |
fbf5a39b | 3535 | procedure Remove_Pragmas; |
76a69663 ES |
3536 | -- A pragma Unreferenced or pragma Unmodified that mentions a formal |
3537 | -- parameter has no meaning when the body is inlined and the formals | |
3538 | -- are rewritten. Remove it from body to inline. The analysis of the | |
3539 | -- non-inlined body will handle the pragma properly. | |
996ae0b0 | 3540 | |
e895b435 ES |
3541 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean; |
3542 | -- If the body of the subprogram includes a call that returns an | |
3543 | -- unconstrained type, the secondary stack is involved, and it | |
3544 | -- is not worth inlining. | |
3545 | ||
996ae0b0 RK |
3546 | ------------------------------ |
3547 | -- Has_Excluded_Declaration -- | |
3548 | ------------------------------ | |
3549 | ||
3550 | function Has_Excluded_Declaration (Decls : List_Id) return Boolean is | |
3551 | D : Node_Id; | |
3552 | ||
fbf5a39b | 3553 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean; |
82c80734 RD |
3554 | -- Nested subprograms make a given body ineligible for inlining, but |
3555 | -- we make an exception for instantiations of unchecked conversion. | |
3556 | -- The body has not been analyzed yet, so check the name, and verify | |
3557 | -- that the visible entity with that name is the predefined unit. | |
3558 | ||
3559 | ----------------------------- | |
3560 | -- Is_Unchecked_Conversion -- | |
3561 | ----------------------------- | |
fbf5a39b AC |
3562 | |
3563 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean is | |
82c80734 | 3564 | Id : constant Node_Id := Name (D); |
fbf5a39b AC |
3565 | Conv : Entity_Id; |
3566 | ||
3567 | begin | |
3568 | if Nkind (Id) = N_Identifier | |
3569 | and then Chars (Id) = Name_Unchecked_Conversion | |
3570 | then | |
3571 | Conv := Current_Entity (Id); | |
3572 | ||
800621e0 | 3573 | elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name) |
fbf5a39b AC |
3574 | and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion |
3575 | then | |
3576 | Conv := Current_Entity (Selector_Name (Id)); | |
fbf5a39b AC |
3577 | else |
3578 | return False; | |
3579 | end if; | |
3580 | ||
758c442c GD |
3581 | return Present (Conv) |
3582 | and then Is_Predefined_File_Name | |
3583 | (Unit_File_Name (Get_Source_Unit (Conv))) | |
fbf5a39b AC |
3584 | and then Is_Intrinsic_Subprogram (Conv); |
3585 | end Is_Unchecked_Conversion; | |
3586 | ||
3587 | -- Start of processing for Has_Excluded_Declaration | |
3588 | ||
996ae0b0 RK |
3589 | begin |
3590 | D := First (Decls); | |
996ae0b0 | 3591 | while Present (D) loop |
800621e0 RD |
3592 | if (Nkind (D) = N_Function_Instantiation |
3593 | and then not Is_Unchecked_Conversion (D)) | |
3594 | or else Nkind_In (D, N_Protected_Type_Declaration, | |
3595 | N_Package_Declaration, | |
3596 | N_Package_Instantiation, | |
3597 | N_Subprogram_Body, | |
3598 | N_Procedure_Instantiation, | |
3599 | N_Task_Type_Declaration) | |
996ae0b0 RK |
3600 | then |
3601 | Cannot_Inline | |
fbf5a39b | 3602 | ("cannot inline & (non-allowed declaration)?", D, Subp); |
996ae0b0 RK |
3603 | return True; |
3604 | end if; | |
3605 | ||
3606 | Next (D); | |
3607 | end loop; | |
3608 | ||
3609 | return False; | |
996ae0b0 RK |
3610 | end Has_Excluded_Declaration; |
3611 | ||
3612 | ---------------------------- | |
3613 | -- Has_Excluded_Statement -- | |
3614 | ---------------------------- | |
3615 | ||
3616 | function Has_Excluded_Statement (Stats : List_Id) return Boolean is | |
3617 | S : Node_Id; | |
3618 | E : Node_Id; | |
3619 | ||
3620 | begin | |
3621 | S := First (Stats); | |
996ae0b0 RK |
3622 | while Present (S) loop |
3623 | Stat_Count := Stat_Count + 1; | |
3624 | ||
800621e0 RD |
3625 | if Nkind_In (S, N_Abort_Statement, |
3626 | N_Asynchronous_Select, | |
3627 | N_Conditional_Entry_Call, | |
3628 | N_Delay_Relative_Statement, | |
3629 | N_Delay_Until_Statement, | |
3630 | N_Selective_Accept, | |
3631 | N_Timed_Entry_Call) | |
996ae0b0 RK |
3632 | then |
3633 | Cannot_Inline | |
fbf5a39b | 3634 | ("cannot inline & (non-allowed statement)?", S, Subp); |
996ae0b0 RK |
3635 | return True; |
3636 | ||
3637 | elsif Nkind (S) = N_Block_Statement then | |
3638 | if Present (Declarations (S)) | |
3639 | and then Has_Excluded_Declaration (Declarations (S)) | |
3640 | then | |
3641 | return True; | |
3642 | ||
3643 | elsif Present (Handled_Statement_Sequence (S)) | |
3644 | and then | |
3645 | (Present | |
3646 | (Exception_Handlers (Handled_Statement_Sequence (S))) | |
3647 | or else | |
3648 | Has_Excluded_Statement | |
3649 | (Statements (Handled_Statement_Sequence (S)))) | |
3650 | then | |
3651 | return True; | |
3652 | end if; | |
3653 | ||
3654 | elsif Nkind (S) = N_Case_Statement then | |
3655 | E := First (Alternatives (S)); | |
996ae0b0 RK |
3656 | while Present (E) loop |
3657 | if Has_Excluded_Statement (Statements (E)) then | |
3658 | return True; | |
3659 | end if; | |
3660 | ||
3661 | Next (E); | |
3662 | end loop; | |
3663 | ||
3664 | elsif Nkind (S) = N_If_Statement then | |
3665 | if Has_Excluded_Statement (Then_Statements (S)) then | |
3666 | return True; | |
3667 | end if; | |
3668 | ||
3669 | if Present (Elsif_Parts (S)) then | |
3670 | E := First (Elsif_Parts (S)); | |
996ae0b0 RK |
3671 | while Present (E) loop |
3672 | if Has_Excluded_Statement (Then_Statements (E)) then | |
3673 | return True; | |
3674 | end if; | |
3675 | Next (E); | |
3676 | end loop; | |
3677 | end if; | |
3678 | ||
3679 | if Present (Else_Statements (S)) | |
3680 | and then Has_Excluded_Statement (Else_Statements (S)) | |
3681 | then | |
3682 | return True; | |
3683 | end if; | |
3684 | ||
3685 | elsif Nkind (S) = N_Loop_Statement | |
3686 | and then Has_Excluded_Statement (Statements (S)) | |
3687 | then | |
3688 | return True; | |
3e2399ba AC |
3689 | |
3690 | elsif Nkind (S) = N_Extended_Return_Statement then | |
3691 | if Has_Excluded_Statement | |
3692 | (Statements (Handled_Statement_Sequence (S))) | |
3693 | or else Present | |
3694 | (Exception_Handlers (Handled_Statement_Sequence (S))) | |
3695 | then | |
3696 | return True; | |
3697 | end if; | |
996ae0b0 RK |
3698 | end if; |
3699 | ||
3700 | Next (S); | |
3701 | end loop; | |
3702 | ||
3703 | return False; | |
3704 | end Has_Excluded_Statement; | |
3705 | ||
3706 | ------------------------------- | |
3707 | -- Has_Pending_Instantiation -- | |
3708 | ------------------------------- | |
3709 | ||
3710 | function Has_Pending_Instantiation return Boolean is | |
ec4867fa | 3711 | S : Entity_Id; |
996ae0b0 RK |
3712 | |
3713 | begin | |
ec4867fa | 3714 | S := Current_Scope; |
996ae0b0 RK |
3715 | while Present (S) loop |
3716 | if Is_Compilation_Unit (S) | |
3717 | or else Is_Child_Unit (S) | |
3718 | then | |
3719 | return False; | |
bce79204 | 3720 | |
996ae0b0 RK |
3721 | elsif Ekind (S) = E_Package |
3722 | and then Has_Forward_Instantiation (S) | |
3723 | then | |
3724 | return True; | |
3725 | end if; | |
3726 | ||
3727 | S := Scope (S); | |
3728 | end loop; | |
3729 | ||
3730 | return False; | |
3731 | end Has_Pending_Instantiation; | |
3732 | ||
c8ef728f ES |
3733 | ------------------------ |
3734 | -- Has_Single_Return -- | |
3735 | ------------------------ | |
3736 | ||
3737 | function Has_Single_Return return Boolean is | |
3738 | Return_Statement : Node_Id := Empty; | |
3739 | ||
3740 | function Check_Return (N : Node_Id) return Traverse_Result; | |
3741 | ||
3742 | ------------------ | |
3743 | -- Check_Return -- | |
3744 | ------------------ | |
3745 | ||
3746 | function Check_Return (N : Node_Id) return Traverse_Result is | |
3747 | begin | |
5d37ba92 | 3748 | if Nkind (N) = N_Simple_Return_Statement then |
c8ef728f ES |
3749 | if Present (Expression (N)) |
3750 | and then Is_Entity_Name (Expression (N)) | |
3751 | then | |
3752 | if No (Return_Statement) then | |
3753 | Return_Statement := N; | |
3754 | return OK; | |
3755 | ||
3756 | elsif Chars (Expression (N)) = | |
3757 | Chars (Expression (Return_Statement)) | |
3758 | then | |
3759 | return OK; | |
3760 | ||
3761 | else | |
3762 | return Abandon; | |
3763 | end if; | |
3764 | ||
3e2399ba AC |
3765 | -- A return statement within an extended return is a noop |
3766 | -- after inlining. | |
3767 | ||
3768 | elsif No (Expression (N)) | |
3769 | and then Nkind (Parent (Parent (N))) = | |
3770 | N_Extended_Return_Statement | |
3771 | then | |
3772 | return OK; | |
3773 | ||
c8ef728f ES |
3774 | else |
3775 | -- Expression has wrong form | |
3776 | ||
3777 | return Abandon; | |
3778 | end if; | |
3779 | ||
3e2399ba AC |
3780 | -- We can only inline a build-in-place function if |
3781 | -- it has a single extended return. | |
3782 | ||
3783 | elsif Nkind (N) = N_Extended_Return_Statement then | |
3784 | if No (Return_Statement) then | |
3785 | Return_Statement := N; | |
3786 | return OK; | |
3787 | ||
3788 | else | |
3789 | return Abandon; | |
3790 | end if; | |
3791 | ||
c8ef728f ES |
3792 | else |
3793 | return OK; | |
3794 | end if; | |
3795 | end Check_Return; | |
3796 | ||
3797 | function Check_All_Returns is new Traverse_Func (Check_Return); | |
3798 | ||
3799 | -- Start of processing for Has_Single_Return | |
3800 | ||
3801 | begin | |
3e2399ba AC |
3802 | if Check_All_Returns (N) /= OK then |
3803 | return False; | |
3804 | ||
3805 | elsif Nkind (Return_Statement) = N_Extended_Return_Statement then | |
3806 | return True; | |
3807 | ||
3808 | else | |
3809 | return Present (Declarations (N)) | |
3810 | and then Present (First (Declarations (N))) | |
3811 | and then Chars (Expression (Return_Statement)) = | |
3812 | Chars (Defining_Identifier (First (Declarations (N)))); | |
3813 | end if; | |
c8ef728f ES |
3814 | end Has_Single_Return; |
3815 | ||
fbf5a39b AC |
3816 | -------------------- |
3817 | -- Remove_Pragmas -- | |
3818 | -------------------- | |
3819 | ||
3820 | procedure Remove_Pragmas is | |
3821 | Decl : Node_Id; | |
3822 | Nxt : Node_Id; | |
3823 | ||
3824 | begin | |
3825 | Decl := First (Declarations (Body_To_Analyze)); | |
3826 | while Present (Decl) loop | |
3827 | Nxt := Next (Decl); | |
3828 | ||
3829 | if Nkind (Decl) = N_Pragma | |
76a69663 ES |
3830 | and then (Pragma_Name (Decl) = Name_Unreferenced |
3831 | or else | |
3832 | Pragma_Name (Decl) = Name_Unmodified) | |
fbf5a39b AC |
3833 | then |
3834 | Remove (Decl); | |
3835 | end if; | |
3836 | ||
3837 | Decl := Nxt; | |
3838 | end loop; | |
3839 | end Remove_Pragmas; | |
3840 | ||
e895b435 ES |
3841 | -------------------------- |
3842 | -- Uses_Secondary_Stack -- | |
3843 | -------------------------- | |
3844 | ||
3845 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is | |
3846 | function Check_Call (N : Node_Id) return Traverse_Result; | |
3847 | -- Look for function calls that return an unconstrained type | |
3848 | ||
3849 | ---------------- | |
3850 | -- Check_Call -- | |
3851 | ---------------- | |
3852 | ||
3853 | function Check_Call (N : Node_Id) return Traverse_Result is | |
3854 | begin | |
3855 | if Nkind (N) = N_Function_Call | |
3856 | and then Is_Entity_Name (Name (N)) | |
3857 | and then Is_Composite_Type (Etype (Entity (Name (N)))) | |
3858 | and then not Is_Constrained (Etype (Entity (Name (N)))) | |
3859 | then | |
3860 | Cannot_Inline | |
3861 | ("cannot inline & (call returns unconstrained type)?", | |
3862 | N, Subp); | |
3863 | return Abandon; | |
3864 | else | |
3865 | return OK; | |
3866 | end if; | |
3867 | end Check_Call; | |
3868 | ||
3869 | function Check_Calls is new Traverse_Func (Check_Call); | |
3870 | ||
3871 | begin | |
3872 | return Check_Calls (Bod) = Abandon; | |
3873 | end Uses_Secondary_Stack; | |
3874 | ||
996ae0b0 RK |
3875 | -- Start of processing for Build_Body_To_Inline |
3876 | ||
3877 | begin | |
8dbd1460 AC |
3878 | -- Return immediately if done already |
3879 | ||
996ae0b0 RK |
3880 | if Nkind (Decl) = N_Subprogram_Declaration |
3881 | and then Present (Body_To_Inline (Decl)) | |
3882 | then | |
8dbd1460 | 3883 | return; |
996ae0b0 | 3884 | |
08402a6d ES |
3885 | -- Functions that return unconstrained composite types require |
3886 | -- secondary stack handling, and cannot currently be inlined, unless | |
3887 | -- all return statements return a local variable that is the first | |
3888 | -- local declaration in the body. | |
996ae0b0 RK |
3889 | |
3890 | elsif Ekind (Subp) = E_Function | |
3891 | and then not Is_Scalar_Type (Etype (Subp)) | |
3892 | and then not Is_Access_Type (Etype (Subp)) | |
3893 | and then not Is_Constrained (Etype (Subp)) | |
3894 | then | |
08402a6d ES |
3895 | if not Has_Single_Return then |
3896 | Cannot_Inline | |
3897 | ("cannot inline & (unconstrained return type)?", N, Subp); | |
3898 | return; | |
3899 | end if; | |
3900 | ||
3901 | -- Ditto for functions that return controlled types, where controlled | |
3902 | -- actions interfere in complex ways with inlining. | |
2820d220 AC |
3903 | |
3904 | elsif Ekind (Subp) = E_Function | |
048e5cef | 3905 | and then Needs_Finalization (Etype (Subp)) |
2820d220 AC |
3906 | then |
3907 | Cannot_Inline | |
3908 | ("cannot inline & (controlled return type)?", N, Subp); | |
3909 | return; | |
996ae0b0 RK |
3910 | end if; |
3911 | ||
d05ef0ab AC |
3912 | if Present (Declarations (N)) |
3913 | and then Has_Excluded_Declaration (Declarations (N)) | |
996ae0b0 | 3914 | then |
d05ef0ab | 3915 | return; |
996ae0b0 RK |
3916 | end if; |
3917 | ||
3918 | if Present (Handled_Statement_Sequence (N)) then | |
fbf5a39b AC |
3919 | if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then |
3920 | Cannot_Inline | |
3921 | ("cannot inline& (exception handler)?", | |
3922 | First (Exception_Handlers (Handled_Statement_Sequence (N))), | |
3923 | Subp); | |
d05ef0ab | 3924 | return; |
996ae0b0 RK |
3925 | elsif |
3926 | Has_Excluded_Statement | |
3927 | (Statements (Handled_Statement_Sequence (N))) | |
3928 | then | |
d05ef0ab | 3929 | return; |
996ae0b0 RK |
3930 | end if; |
3931 | end if; | |
3932 | ||
3933 | -- We do not inline a subprogram that is too large, unless it is | |
3934 | -- marked Inline_Always. This pragma does not suppress the other | |
3935 | -- checks on inlining (forbidden declarations, handlers, etc). | |
3936 | ||
3937 | if Stat_Count > Max_Size | |
800621e0 | 3938 | and then not Has_Pragma_Inline_Always (Subp) |
996ae0b0 | 3939 | then |
fbf5a39b | 3940 | Cannot_Inline ("cannot inline& (body too large)?", N, Subp); |
d05ef0ab | 3941 | return; |
996ae0b0 RK |
3942 | end if; |
3943 | ||
3944 | if Has_Pending_Instantiation then | |
3945 | Cannot_Inline | |
fbf5a39b AC |
3946 | ("cannot inline& (forward instance within enclosing body)?", |
3947 | N, Subp); | |
d05ef0ab AC |
3948 | return; |
3949 | end if; | |
3950 | ||
3951 | -- Within an instance, the body to inline must be treated as a nested | |
3952 | -- generic, so that the proper global references are preserved. | |
3953 | ||
ce4e59c4 ST |
3954 | -- Note that we do not do this at the library level, because it is not |
3955 | -- needed, and furthermore this causes trouble if front end inlining | |
3956 | -- is activated (-gnatN). | |
3957 | ||
3958 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
d05ef0ab AC |
3959 | Save_Env (Scope (Current_Scope), Scope (Current_Scope)); |
3960 | Original_Body := Copy_Generic_Node (N, Empty, True); | |
3961 | else | |
3962 | Original_Body := Copy_Separate_Tree (N); | |
996ae0b0 RK |
3963 | end if; |
3964 | ||
d05ef0ab AC |
3965 | -- We need to capture references to the formals in order to substitute |
3966 | -- the actuals at the point of inlining, i.e. instantiation. To treat | |
3967 | -- the formals as globals to the body to inline, we nest it within | |
3968 | -- a dummy parameterless subprogram, declared within the real one. | |
24105bab AC |
3969 | -- To avoid generating an internal name (which is never public, and |
3970 | -- which affects serial numbers of other generated names), we use | |
3971 | -- an internal symbol that cannot conflict with user declarations. | |
d05ef0ab AC |
3972 | |
3973 | Set_Parameter_Specifications (Specification (Original_Body), No_List); | |
24105bab AC |
3974 | Set_Defining_Unit_Name |
3975 | (Specification (Original_Body), | |
3976 | Make_Defining_Identifier (Sloc (N), Name_uParent)); | |
d05ef0ab AC |
3977 | Set_Corresponding_Spec (Original_Body, Empty); |
3978 | ||
996ae0b0 RK |
3979 | Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False); |
3980 | ||
3981 | -- Set return type of function, which is also global and does not need | |
3982 | -- to be resolved. | |
3983 | ||
3984 | if Ekind (Subp) = E_Function then | |
41251c60 | 3985 | Set_Result_Definition (Specification (Body_To_Analyze), |
996ae0b0 RK |
3986 | New_Occurrence_Of (Etype (Subp), Sloc (N))); |
3987 | end if; | |
3988 | ||
3989 | if No (Declarations (N)) then | |
3990 | Set_Declarations (N, New_List (Body_To_Analyze)); | |
3991 | else | |
3992 | Append (Body_To_Analyze, Declarations (N)); | |
3993 | end if; | |
3994 | ||
3995 | Expander_Mode_Save_And_Set (False); | |
fbf5a39b | 3996 | Remove_Pragmas; |
996ae0b0 RK |
3997 | |
3998 | Analyze (Body_To_Analyze); | |
0a36105d | 3999 | Push_Scope (Defining_Entity (Body_To_Analyze)); |
996ae0b0 RK |
4000 | Save_Global_References (Original_Body); |
4001 | End_Scope; | |
4002 | Remove (Body_To_Analyze); | |
4003 | ||
4004 | Expander_Mode_Restore; | |
d05ef0ab | 4005 | |
ce4e59c4 ST |
4006 | -- Restore environment if previously saved |
4007 | ||
4008 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
d05ef0ab AC |
4009 | Restore_Env; |
4010 | end if; | |
e895b435 ES |
4011 | |
4012 | -- If secondary stk used there is no point in inlining. We have | |
4013 | -- already issued the warning in this case, so nothing to do. | |
4014 | ||
4015 | if Uses_Secondary_Stack (Body_To_Analyze) then | |
4016 | return; | |
4017 | end if; | |
4018 | ||
4019 | Set_Body_To_Inline (Decl, Original_Body); | |
4020 | Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp)); | |
4021 | Set_Is_Inlined (Subp); | |
996ae0b0 RK |
4022 | end Build_Body_To_Inline; |
4023 | ||
fbf5a39b AC |
4024 | ------------------- |
4025 | -- Cannot_Inline -- | |
4026 | ------------------- | |
4027 | ||
4028 | procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is | |
4029 | begin | |
676e8420 AC |
4030 | -- Do not emit warning if this is a predefined unit which is not the |
4031 | -- main unit. With validity checks enabled, some predefined subprograms | |
4032 | -- may contain nested subprograms and become ineligible for inlining. | |
fbf5a39b AC |
4033 | |
4034 | if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp))) | |
4035 | and then not In_Extended_Main_Source_Unit (Subp) | |
4036 | then | |
4037 | null; | |
4038 | ||
800621e0 | 4039 | elsif Has_Pragma_Inline_Always (Subp) then |
e895b435 ES |
4040 | |
4041 | -- Remove last character (question mark) to make this into an error, | |
4042 | -- because the Inline_Always pragma cannot be obeyed. | |
4043 | ||
ec4867fa | 4044 | Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp); |
fbf5a39b AC |
4045 | |
4046 | elsif Ineffective_Inline_Warnings then | |
4047 | Error_Msg_NE (Msg, N, Subp); | |
4048 | end if; | |
4049 | end Cannot_Inline; | |
4050 | ||
996ae0b0 RK |
4051 | ----------------------- |
4052 | -- Check_Conformance -- | |
4053 | ----------------------- | |
4054 | ||
4055 | procedure Check_Conformance | |
41251c60 JM |
4056 | (New_Id : Entity_Id; |
4057 | Old_Id : Entity_Id; | |
4058 | Ctype : Conformance_Type; | |
4059 | Errmsg : Boolean; | |
4060 | Conforms : out Boolean; | |
4061 | Err_Loc : Node_Id := Empty; | |
4062 | Get_Inst : Boolean := False; | |
4063 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 | 4064 | is |
996ae0b0 | 4065 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id); |
c27f2f15 RD |
4066 | -- Sets Conforms to False. If Errmsg is False, then that's all it does. |
4067 | -- If Errmsg is True, then processing continues to post an error message | |
4068 | -- for conformance error on given node. Two messages are output. The | |
4069 | -- first message points to the previous declaration with a general "no | |
4070 | -- conformance" message. The second is the detailed reason, supplied as | |
4071 | -- Msg. The parameter N provide information for a possible & insertion | |
4072 | -- in the message, and also provides the location for posting the | |
4073 | -- message in the absence of a specified Err_Loc location. | |
996ae0b0 RK |
4074 | |
4075 | ----------------------- | |
4076 | -- Conformance_Error -- | |
4077 | ----------------------- | |
4078 | ||
4079 | procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is | |
4080 | Enode : Node_Id; | |
4081 | ||
4082 | begin | |
4083 | Conforms := False; | |
4084 | ||
4085 | if Errmsg then | |
4086 | if No (Err_Loc) then | |
4087 | Enode := N; | |
4088 | else | |
4089 | Enode := Err_Loc; | |
4090 | end if; | |
4091 | ||
4092 | Error_Msg_Sloc := Sloc (Old_Id); | |
4093 | ||
4094 | case Ctype is | |
4095 | when Type_Conformant => | |
483c78cb | 4096 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
4097 | ("not type conformant with declaration#!", Enode); |
4098 | ||
4099 | when Mode_Conformant => | |
19590d70 | 4100 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4101 | Error_Msg_N |
19590d70 GD |
4102 | ("not mode conformant with operation inherited#!", |
4103 | Enode); | |
4104 | else | |
ed2233dc | 4105 | Error_Msg_N |
19590d70 GD |
4106 | ("not mode conformant with declaration#!", Enode); |
4107 | end if; | |
996ae0b0 RK |
4108 | |
4109 | when Subtype_Conformant => | |
19590d70 | 4110 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
ed2233dc | 4111 | Error_Msg_N |
19590d70 GD |
4112 | ("not subtype conformant with operation inherited#!", |
4113 | Enode); | |
4114 | else | |
ed2233dc | 4115 | Error_Msg_N |
19590d70 GD |
4116 | ("not subtype conformant with declaration#!", Enode); |
4117 | end if; | |
996ae0b0 RK |
4118 | |
4119 | when Fully_Conformant => | |
19590d70 | 4120 | if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then |
483c78cb | 4121 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4122 | ("not fully conformant with operation inherited#!", |
4123 | Enode); | |
4124 | else | |
483c78cb | 4125 | Error_Msg_N -- CODEFIX |
19590d70 GD |
4126 | ("not fully conformant with declaration#!", Enode); |
4127 | end if; | |
996ae0b0 RK |
4128 | end case; |
4129 | ||
4130 | Error_Msg_NE (Msg, Enode, N); | |
4131 | end if; | |
4132 | end Conformance_Error; | |
4133 | ||
ec4867fa ES |
4134 | -- Local Variables |
4135 | ||
4136 | Old_Type : constant Entity_Id := Etype (Old_Id); | |
4137 | New_Type : constant Entity_Id := Etype (New_Id); | |
4138 | Old_Formal : Entity_Id; | |
4139 | New_Formal : Entity_Id; | |
4140 | Access_Types_Match : Boolean; | |
4141 | Old_Formal_Base : Entity_Id; | |
4142 | New_Formal_Base : Entity_Id; | |
4143 | ||
996ae0b0 RK |
4144 | -- Start of processing for Check_Conformance |
4145 | ||
4146 | begin | |
4147 | Conforms := True; | |
4148 | ||
82c80734 RD |
4149 | -- We need a special case for operators, since they don't appear |
4150 | -- explicitly. | |
996ae0b0 RK |
4151 | |
4152 | if Ctype = Type_Conformant then | |
4153 | if Ekind (New_Id) = E_Operator | |
4154 | and then Operator_Matches_Spec (New_Id, Old_Id) | |
4155 | then | |
4156 | return; | |
4157 | end if; | |
4158 | end if; | |
4159 | ||
4160 | -- If both are functions/operators, check return types conform | |
4161 | ||
4162 | if Old_Type /= Standard_Void_Type | |
4163 | and then New_Type /= Standard_Void_Type | |
4164 | then | |
fceeaab6 ES |
4165 | |
4166 | -- If we are checking interface conformance we omit controlling | |
4167 | -- arguments and result, because we are only checking the conformance | |
4168 | -- of the remaining parameters. | |
4169 | ||
4170 | if Has_Controlling_Result (Old_Id) | |
4171 | and then Has_Controlling_Result (New_Id) | |
4172 | and then Skip_Controlling_Formals | |
4173 | then | |
4174 | null; | |
4175 | ||
4176 | elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then | |
5d37ba92 | 4177 | Conformance_Error ("\return type does not match!", New_Id); |
996ae0b0 RK |
4178 | return; |
4179 | end if; | |
4180 | ||
41251c60 | 4181 | -- Ada 2005 (AI-231): In case of anonymous access types check the |
0a36105d | 4182 | -- null-exclusion and access-to-constant attributes match. |
41251c60 | 4183 | |
0791fbe9 | 4184 | if Ada_Version >= Ada_2005 |
41251c60 JM |
4185 | and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type |
4186 | and then | |
4187 | (Can_Never_Be_Null (Old_Type) | |
4188 | /= Can_Never_Be_Null (New_Type) | |
4189 | or else Is_Access_Constant (Etype (Old_Type)) | |
4190 | /= Is_Access_Constant (Etype (New_Type))) | |
4191 | then | |
5d37ba92 | 4192 | Conformance_Error ("\return type does not match!", New_Id); |
41251c60 JM |
4193 | return; |
4194 | end if; | |
4195 | ||
996ae0b0 RK |
4196 | -- If either is a function/operator and the other isn't, error |
4197 | ||
4198 | elsif Old_Type /= Standard_Void_Type | |
4199 | or else New_Type /= Standard_Void_Type | |
4200 | then | |
5d37ba92 | 4201 | Conformance_Error ("\functions can only match functions!", New_Id); |
996ae0b0 RK |
4202 | return; |
4203 | end if; | |
4204 | ||
0a36105d | 4205 | -- In subtype conformant case, conventions must match (RM 6.3.1(16)). |
996ae0b0 RK |
4206 | -- If this is a renaming as body, refine error message to indicate that |
4207 | -- the conflict is with the original declaration. If the entity is not | |
4208 | -- frozen, the conventions don't have to match, the one of the renamed | |
4209 | -- entity is inherited. | |
4210 | ||
4211 | if Ctype >= Subtype_Conformant then | |
996ae0b0 RK |
4212 | if Convention (Old_Id) /= Convention (New_Id) then |
4213 | ||
4214 | if not Is_Frozen (New_Id) then | |
4215 | null; | |
4216 | ||
4217 | elsif Present (Err_Loc) | |
4218 | and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration | |
4219 | and then Present (Corresponding_Spec (Err_Loc)) | |
4220 | then | |
4221 | Error_Msg_Name_1 := Chars (New_Id); | |
4222 | Error_Msg_Name_2 := | |
4223 | Name_Ada + Convention_Id'Pos (Convention (New_Id)); | |
5d37ba92 | 4224 | Conformance_Error ("\prior declaration for% has convention %!"); |
996ae0b0 RK |
4225 | |
4226 | else | |
5d37ba92 | 4227 | Conformance_Error ("\calling conventions do not match!"); |
996ae0b0 RK |
4228 | end if; |
4229 | ||
4230 | return; | |
4231 | ||
4232 | elsif Is_Formal_Subprogram (Old_Id) | |
4233 | or else Is_Formal_Subprogram (New_Id) | |
4234 | then | |
5d37ba92 | 4235 | Conformance_Error ("\formal subprograms not allowed!"); |
996ae0b0 RK |
4236 | return; |
4237 | end if; | |
4238 | end if; | |
4239 | ||
4240 | -- Deal with parameters | |
4241 | ||
4242 | -- Note: we use the entity information, rather than going directly | |
4243 | -- to the specification in the tree. This is not only simpler, but | |
4244 | -- absolutely necessary for some cases of conformance tests between | |
4245 | -- operators, where the declaration tree simply does not exist! | |
4246 | ||
4247 | Old_Formal := First_Formal (Old_Id); | |
4248 | New_Formal := First_Formal (New_Id); | |
996ae0b0 | 4249 | while Present (Old_Formal) and then Present (New_Formal) loop |
41251c60 JM |
4250 | if Is_Controlling_Formal (Old_Formal) |
4251 | and then Is_Controlling_Formal (New_Formal) | |
4252 | and then Skip_Controlling_Formals | |
4253 | then | |
a2dc5812 AC |
4254 | -- The controlling formals will have different types when |
4255 | -- comparing an interface operation with its match, but both | |
4256 | -- or neither must be access parameters. | |
4257 | ||
4258 | if Is_Access_Type (Etype (Old_Formal)) | |
4259 | = | |
4260 | Is_Access_Type (Etype (New_Formal)) | |
4261 | then | |
4262 | goto Skip_Controlling_Formal; | |
4263 | else | |
4264 | Conformance_Error | |
4265 | ("\access parameter does not match!", New_Formal); | |
4266 | end if; | |
41251c60 JM |
4267 | end if; |
4268 | ||
fbf5a39b AC |
4269 | if Ctype = Fully_Conformant then |
4270 | ||
4271 | -- Names must match. Error message is more accurate if we do | |
4272 | -- this before checking that the types of the formals match. | |
4273 | ||
4274 | if Chars (Old_Formal) /= Chars (New_Formal) then | |
5d37ba92 | 4275 | Conformance_Error ("\name & does not match!", New_Formal); |
fbf5a39b AC |
4276 | |
4277 | -- Set error posted flag on new formal as well to stop | |
4278 | -- junk cascaded messages in some cases. | |
4279 | ||
4280 | Set_Error_Posted (New_Formal); | |
4281 | return; | |
4282 | end if; | |
40b93859 RD |
4283 | |
4284 | -- Null exclusion must match | |
4285 | ||
4286 | if Null_Exclusion_Present (Parent (Old_Formal)) | |
4287 | /= | |
4288 | Null_Exclusion_Present (Parent (New_Formal)) | |
4289 | then | |
4290 | -- Only give error if both come from source. This should be | |
4291 | -- investigated some time, since it should not be needed ??? | |
4292 | ||
4293 | if Comes_From_Source (Old_Formal) | |
4294 | and then | |
4295 | Comes_From_Source (New_Formal) | |
4296 | then | |
4297 | Conformance_Error | |
4298 | ("\null exclusion for & does not match", New_Formal); | |
4299 | ||
4300 | -- Mark error posted on the new formal to avoid duplicated | |
4301 | -- complaint about types not matching. | |
4302 | ||
4303 | Set_Error_Posted (New_Formal); | |
4304 | end if; | |
4305 | end if; | |
fbf5a39b | 4306 | end if; |
996ae0b0 | 4307 | |
ec4867fa ES |
4308 | -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This |
4309 | -- case occurs whenever a subprogram is being renamed and one of its | |
4310 | -- parameters imposes a null exclusion. For example: | |
4311 | ||
4312 | -- type T is null record; | |
4313 | -- type Acc_T is access T; | |
4314 | -- subtype Acc_T_Sub is Acc_T; | |
4315 | ||
4316 | -- procedure P (Obj : not null Acc_T_Sub); -- itype | |
4317 | -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype | |
4318 | -- renames P; | |
4319 | ||
4320 | Old_Formal_Base := Etype (Old_Formal); | |
4321 | New_Formal_Base := Etype (New_Formal); | |
4322 | ||
4323 | if Get_Inst then | |
4324 | Old_Formal_Base := Get_Instance_Of (Old_Formal_Base); | |
4325 | New_Formal_Base := Get_Instance_Of (New_Formal_Base); | |
4326 | end if; | |
4327 | ||
0791fbe9 | 4328 | Access_Types_Match := Ada_Version >= Ada_2005 |
ec4867fa ES |
4329 | |
4330 | -- Ensure that this rule is only applied when New_Id is a | |
5d37ba92 | 4331 | -- renaming of Old_Id. |
ec4867fa | 4332 | |
5d37ba92 ES |
4333 | and then Nkind (Parent (Parent (New_Id))) = |
4334 | N_Subprogram_Renaming_Declaration | |
ec4867fa ES |
4335 | and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity |
4336 | and then Present (Entity (Name (Parent (Parent (New_Id))))) | |
4337 | and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id | |
4338 | ||
4339 | -- Now handle the allowed access-type case | |
4340 | ||
4341 | and then Is_Access_Type (Old_Formal_Base) | |
4342 | and then Is_Access_Type (New_Formal_Base) | |
5d37ba92 ES |
4343 | |
4344 | -- The type kinds must match. The only exception occurs with | |
4345 | -- multiple generics of the form: | |
4346 | ||
4347 | -- generic generic | |
4348 | -- type F is private; type A is private; | |
4349 | -- type F_Ptr is access F; type A_Ptr is access A; | |
4350 | -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr); | |
4351 | -- package F_Pack is ... package A_Pack is | |
4352 | -- package F_Inst is | |
4353 | -- new F_Pack (A, A_Ptr, A_P); | |
4354 | ||
4355 | -- When checking for conformance between the parameters of A_P | |
4356 | -- and F_P, the type kinds of F_Ptr and A_Ptr will not match | |
4357 | -- because the compiler has transformed A_Ptr into a subtype of | |
4358 | -- F_Ptr. We catch this case in the code below. | |
4359 | ||
4360 | and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base) | |
4361 | or else | |
4362 | (Is_Generic_Type (Old_Formal_Base) | |
4363 | and then Is_Generic_Type (New_Formal_Base) | |
4364 | and then Is_Internal (New_Formal_Base) | |
4365 | and then Etype (Etype (New_Formal_Base)) = | |
4366 | Old_Formal_Base)) | |
ec4867fa ES |
4367 | and then Directly_Designated_Type (Old_Formal_Base) = |
4368 | Directly_Designated_Type (New_Formal_Base) | |
4369 | and then ((Is_Itype (Old_Formal_Base) | |
4370 | and then Can_Never_Be_Null (Old_Formal_Base)) | |
4371 | or else | |
4372 | (Is_Itype (New_Formal_Base) | |
4373 | and then Can_Never_Be_Null (New_Formal_Base))); | |
4374 | ||
996ae0b0 RK |
4375 | -- Types must always match. In the visible part of an instance, |
4376 | -- usual overloading rules for dispatching operations apply, and | |
4377 | -- we check base types (not the actual subtypes). | |
4378 | ||
4379 | if In_Instance_Visible_Part | |
4380 | and then Is_Dispatching_Operation (New_Id) | |
4381 | then | |
4382 | if not Conforming_Types | |
ec4867fa ES |
4383 | (T1 => Base_Type (Etype (Old_Formal)), |
4384 | T2 => Base_Type (Etype (New_Formal)), | |
4385 | Ctype => Ctype, | |
4386 | Get_Inst => Get_Inst) | |
4387 | and then not Access_Types_Match | |
996ae0b0 | 4388 | then |
5d37ba92 | 4389 | Conformance_Error ("\type of & does not match!", New_Formal); |
996ae0b0 RK |
4390 | return; |
4391 | end if; | |
4392 | ||
4393 | elsif not Conforming_Types | |
5d37ba92 ES |
4394 | (T1 => Old_Formal_Base, |
4395 | T2 => New_Formal_Base, | |
ec4867fa ES |
4396 | Ctype => Ctype, |
4397 | Get_Inst => Get_Inst) | |
4398 | and then not Access_Types_Match | |
996ae0b0 | 4399 | then |
c27f2f15 RD |
4400 | -- Don't give error message if old type is Any_Type. This test |
4401 | -- avoids some cascaded errors, e.g. in case of a bad spec. | |
4402 | ||
4403 | if Errmsg and then Old_Formal_Base = Any_Type then | |
4404 | Conforms := False; | |
4405 | else | |
4406 | Conformance_Error ("\type of & does not match!", New_Formal); | |
4407 | end if; | |
4408 | ||
996ae0b0 RK |
4409 | return; |
4410 | end if; | |
4411 | ||
4412 | -- For mode conformance, mode must match | |
4413 | ||
5d37ba92 ES |
4414 | if Ctype >= Mode_Conformant then |
4415 | if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then | |
dd54644b JM |
4416 | if not Ekind_In (New_Id, E_Function, E_Procedure) |
4417 | or else not Is_Primitive_Wrapper (New_Id) | |
4418 | then | |
4419 | Conformance_Error ("\mode of & does not match!", New_Formal); | |
c199ccf7 | 4420 | |
dd54644b JM |
4421 | else |
4422 | declare | |
c199ccf7 | 4423 | T : constant Entity_Id := Find_Dispatching_Type (New_Id); |
dd54644b JM |
4424 | begin |
4425 | if Is_Protected_Type | |
4426 | (Corresponding_Concurrent_Type (T)) | |
4427 | then | |
4428 | Error_Msg_PT (T, New_Id); | |
4429 | else | |
4430 | Conformance_Error | |
4431 | ("\mode of & does not match!", New_Formal); | |
4432 | end if; | |
4433 | end; | |
4434 | end if; | |
4435 | ||
5d37ba92 ES |
4436 | return; |
4437 | ||
4438 | -- Part of mode conformance for access types is having the same | |
4439 | -- constant modifier. | |
4440 | ||
4441 | elsif Access_Types_Match | |
4442 | and then Is_Access_Constant (Old_Formal_Base) /= | |
4443 | Is_Access_Constant (New_Formal_Base) | |
4444 | then | |
4445 | Conformance_Error | |
4446 | ("\constant modifier does not match!", New_Formal); | |
4447 | return; | |
4448 | end if; | |
996ae0b0 RK |
4449 | end if; |
4450 | ||
0a36105d | 4451 | if Ctype >= Subtype_Conformant then |
996ae0b0 | 4452 | |
0a36105d JM |
4453 | -- Ada 2005 (AI-231): In case of anonymous access types check |
4454 | -- the null-exclusion and access-to-constant attributes must | |
c7b9d548 AC |
4455 | -- match. For null exclusion, we test the types rather than the |
4456 | -- formals themselves, since the attribute is only set reliably | |
4457 | -- on the formals in the Ada 95 case, and we exclude the case | |
4458 | -- where Old_Formal is marked as controlling, to avoid errors | |
4459 | -- when matching completing bodies with dispatching declarations | |
4460 | -- (access formals in the bodies aren't marked Can_Never_Be_Null). | |
996ae0b0 | 4461 | |
0791fbe9 | 4462 | if Ada_Version >= Ada_2005 |
0a36105d JM |
4463 | and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type |
4464 | and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type | |
4465 | and then | |
c7b9d548 AC |
4466 | ((Can_Never_Be_Null (Etype (Old_Formal)) /= |
4467 | Can_Never_Be_Null (Etype (New_Formal)) | |
4468 | and then | |
4469 | not Is_Controlling_Formal (Old_Formal)) | |
0a36105d JM |
4470 | or else |
4471 | Is_Access_Constant (Etype (Old_Formal)) /= | |
4472 | Is_Access_Constant (Etype (New_Formal))) | |
40b93859 RD |
4473 | |
4474 | -- Do not complain if error already posted on New_Formal. This | |
4475 | -- avoids some redundant error messages. | |
4476 | ||
4477 | and then not Error_Posted (New_Formal) | |
0a36105d JM |
4478 | then |
4479 | -- It is allowed to omit the null-exclusion in case of stream | |
4480 | -- attribute subprograms. We recognize stream subprograms | |
4481 | -- through their TSS-generated suffix. | |
996ae0b0 | 4482 | |
0a36105d JM |
4483 | declare |
4484 | TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id); | |
4485 | begin | |
4486 | if TSS_Name /= TSS_Stream_Read | |
4487 | and then TSS_Name /= TSS_Stream_Write | |
4488 | and then TSS_Name /= TSS_Stream_Input | |
4489 | and then TSS_Name /= TSS_Stream_Output | |
4490 | then | |
4491 | Conformance_Error | |
5d37ba92 | 4492 | ("\type of & does not match!", New_Formal); |
0a36105d JM |
4493 | return; |
4494 | end if; | |
4495 | end; | |
4496 | end if; | |
4497 | end if; | |
41251c60 | 4498 | |
0a36105d | 4499 | -- Full conformance checks |
41251c60 | 4500 | |
0a36105d | 4501 | if Ctype = Fully_Conformant then |
e660dbf7 | 4502 | |
0a36105d | 4503 | -- We have checked already that names match |
e660dbf7 | 4504 | |
0a36105d | 4505 | if Parameter_Mode (Old_Formal) = E_In_Parameter then |
41251c60 JM |
4506 | |
4507 | -- Check default expressions for in parameters | |
4508 | ||
996ae0b0 RK |
4509 | declare |
4510 | NewD : constant Boolean := | |
4511 | Present (Default_Value (New_Formal)); | |
4512 | OldD : constant Boolean := | |
4513 | Present (Default_Value (Old_Formal)); | |
4514 | begin | |
4515 | if NewD or OldD then | |
4516 | ||
82c80734 RD |
4517 | -- The old default value has been analyzed because the |
4518 | -- current full declaration will have frozen everything | |
0a36105d JM |
4519 | -- before. The new default value has not been analyzed, |
4520 | -- so analyze it now before we check for conformance. | |
996ae0b0 RK |
4521 | |
4522 | if NewD then | |
0a36105d | 4523 | Push_Scope (New_Id); |
21d27997 | 4524 | Preanalyze_Spec_Expression |
fbf5a39b | 4525 | (Default_Value (New_Formal), Etype (New_Formal)); |
996ae0b0 RK |
4526 | End_Scope; |
4527 | end if; | |
4528 | ||
4529 | if not (NewD and OldD) | |
4530 | or else not Fully_Conformant_Expressions | |
4531 | (Default_Value (Old_Formal), | |
4532 | Default_Value (New_Formal)) | |
4533 | then | |
4534 | Conformance_Error | |
5d37ba92 | 4535 | ("\default expression for & does not match!", |
996ae0b0 RK |
4536 | New_Formal); |
4537 | return; | |
4538 | end if; | |
4539 | end if; | |
4540 | end; | |
4541 | end if; | |
4542 | end if; | |
4543 | ||
4544 | -- A couple of special checks for Ada 83 mode. These checks are | |
0a36105d | 4545 | -- skipped if either entity is an operator in package Standard, |
996ae0b0 RK |
4546 | -- or if either old or new instance is not from the source program. |
4547 | ||
0ab80019 | 4548 | if Ada_Version = Ada_83 |
996ae0b0 RK |
4549 | and then Sloc (Old_Id) > Standard_Location |
4550 | and then Sloc (New_Id) > Standard_Location | |
4551 | and then Comes_From_Source (Old_Id) | |
4552 | and then Comes_From_Source (New_Id) | |
4553 | then | |
4554 | declare | |
4555 | Old_Param : constant Node_Id := Declaration_Node (Old_Formal); | |
4556 | New_Param : constant Node_Id := Declaration_Node (New_Formal); | |
4557 | ||
4558 | begin | |
4559 | -- Explicit IN must be present or absent in both cases. This | |
4560 | -- test is required only in the full conformance case. | |
4561 | ||
4562 | if In_Present (Old_Param) /= In_Present (New_Param) | |
4563 | and then Ctype = Fully_Conformant | |
4564 | then | |
4565 | Conformance_Error | |
5d37ba92 | 4566 | ("\(Ada 83) IN must appear in both declarations", |
996ae0b0 RK |
4567 | New_Formal); |
4568 | return; | |
4569 | end if; | |
4570 | ||
4571 | -- Grouping (use of comma in param lists) must be the same | |
4572 | -- This is where we catch a misconformance like: | |
4573 | ||
0a36105d | 4574 | -- A, B : Integer |
996ae0b0 RK |
4575 | -- A : Integer; B : Integer |
4576 | ||
4577 | -- which are represented identically in the tree except | |
4578 | -- for the setting of the flags More_Ids and Prev_Ids. | |
4579 | ||
4580 | if More_Ids (Old_Param) /= More_Ids (New_Param) | |
4581 | or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param) | |
4582 | then | |
4583 | Conformance_Error | |
5d37ba92 | 4584 | ("\grouping of & does not match!", New_Formal); |
996ae0b0 RK |
4585 | return; |
4586 | end if; | |
4587 | end; | |
4588 | end if; | |
4589 | ||
41251c60 JM |
4590 | -- This label is required when skipping controlling formals |
4591 | ||
4592 | <<Skip_Controlling_Formal>> | |
4593 | ||
996ae0b0 RK |
4594 | Next_Formal (Old_Formal); |
4595 | Next_Formal (New_Formal); | |
4596 | end loop; | |
4597 | ||
4598 | if Present (Old_Formal) then | |
5d37ba92 | 4599 | Conformance_Error ("\too few parameters!"); |
996ae0b0 RK |
4600 | return; |
4601 | ||
4602 | elsif Present (New_Formal) then | |
5d37ba92 | 4603 | Conformance_Error ("\too many parameters!", New_Formal); |
996ae0b0 RK |
4604 | return; |
4605 | end if; | |
996ae0b0 RK |
4606 | end Check_Conformance; |
4607 | ||
ec4867fa ES |
4608 | ----------------------- |
4609 | -- Check_Conventions -- | |
4610 | ----------------------- | |
4611 | ||
4612 | procedure Check_Conventions (Typ : Entity_Id) is | |
ce2b6ba5 | 4613 | Ifaces_List : Elist_Id; |
0a36105d | 4614 | |
ce2b6ba5 | 4615 | procedure Check_Convention (Op : Entity_Id); |
0a36105d JM |
4616 | -- Verify that the convention of inherited dispatching operation Op is |
4617 | -- consistent among all subprograms it overrides. In order to minimize | |
4618 | -- the search, Search_From is utilized to designate a specific point in | |
4619 | -- the list rather than iterating over the whole list once more. | |
ec4867fa ES |
4620 | |
4621 | ---------------------- | |
4622 | -- Check_Convention -- | |
4623 | ---------------------- | |
4624 | ||
ce2b6ba5 JM |
4625 | procedure Check_Convention (Op : Entity_Id) is |
4626 | Iface_Elmt : Elmt_Id; | |
4627 | Iface_Prim_Elmt : Elmt_Id; | |
4628 | Iface_Prim : Entity_Id; | |
ec4867fa | 4629 | |
ce2b6ba5 JM |
4630 | begin |
4631 | Iface_Elmt := First_Elmt (Ifaces_List); | |
4632 | while Present (Iface_Elmt) loop | |
4633 | Iface_Prim_Elmt := | |
4634 | First_Elmt (Primitive_Operations (Node (Iface_Elmt))); | |
4635 | while Present (Iface_Prim_Elmt) loop | |
4636 | Iface_Prim := Node (Iface_Prim_Elmt); | |
4637 | ||
4638 | if Is_Interface_Conformant (Typ, Iface_Prim, Op) | |
4639 | and then Convention (Iface_Prim) /= Convention (Op) | |
4640 | then | |
ed2233dc | 4641 | Error_Msg_N |
ce2b6ba5 | 4642 | ("inconsistent conventions in primitive operations", Typ); |
ec4867fa | 4643 | |
ce2b6ba5 JM |
4644 | Error_Msg_Name_1 := Chars (Op); |
4645 | Error_Msg_Name_2 := Get_Convention_Name (Convention (Op)); | |
4646 | Error_Msg_Sloc := Sloc (Op); | |
ec4867fa | 4647 | |
7a963087 | 4648 | if Comes_From_Source (Op) or else No (Alias (Op)) then |
038140ed | 4649 | if not Present (Overridden_Operation (Op)) then |
ed2233dc | 4650 | Error_Msg_N ("\\primitive % defined #", Typ); |
ce2b6ba5 | 4651 | else |
ed2233dc | 4652 | Error_Msg_N |
19d846a0 RD |
4653 | ("\\overriding operation % with " & |
4654 | "convention % defined #", Typ); | |
ce2b6ba5 | 4655 | end if; |
ec4867fa | 4656 | |
ce2b6ba5 JM |
4657 | else pragma Assert (Present (Alias (Op))); |
4658 | Error_Msg_Sloc := Sloc (Alias (Op)); | |
ed2233dc | 4659 | Error_Msg_N |
19d846a0 RD |
4660 | ("\\inherited operation % with " & |
4661 | "convention % defined #", Typ); | |
ce2b6ba5 | 4662 | end if; |
ec4867fa | 4663 | |
ce2b6ba5 JM |
4664 | Error_Msg_Name_1 := Chars (Op); |
4665 | Error_Msg_Name_2 := | |
4666 | Get_Convention_Name (Convention (Iface_Prim)); | |
4667 | Error_Msg_Sloc := Sloc (Iface_Prim); | |
ed2233dc | 4668 | Error_Msg_N |
19d846a0 RD |
4669 | ("\\overridden operation % with " & |
4670 | "convention % defined #", Typ); | |
ec4867fa | 4671 | |
ce2b6ba5 | 4672 | -- Avoid cascading errors |
ec4867fa | 4673 | |
ce2b6ba5 JM |
4674 | return; |
4675 | end if; | |
ec4867fa | 4676 | |
ce2b6ba5 JM |
4677 | Next_Elmt (Iface_Prim_Elmt); |
4678 | end loop; | |
ec4867fa | 4679 | |
ce2b6ba5 | 4680 | Next_Elmt (Iface_Elmt); |
ec4867fa ES |
4681 | end loop; |
4682 | end Check_Convention; | |
4683 | ||
4684 | -- Local variables | |
4685 | ||
4686 | Prim_Op : Entity_Id; | |
4687 | Prim_Op_Elmt : Elmt_Id; | |
4688 | ||
4689 | -- Start of processing for Check_Conventions | |
4690 | ||
4691 | begin | |
ce2b6ba5 JM |
4692 | if not Has_Interfaces (Typ) then |
4693 | return; | |
4694 | end if; | |
4695 | ||
4696 | Collect_Interfaces (Typ, Ifaces_List); | |
4697 | ||
0a36105d JM |
4698 | -- The algorithm checks every overriding dispatching operation against |
4699 | -- all the corresponding overridden dispatching operations, detecting | |
f3d57416 | 4700 | -- differences in conventions. |
ec4867fa ES |
4701 | |
4702 | Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
4703 | while Present (Prim_Op_Elmt) loop | |
4704 | Prim_Op := Node (Prim_Op_Elmt); | |
4705 | ||
0a36105d | 4706 | -- A small optimization: skip the predefined dispatching operations |
ce2b6ba5 | 4707 | -- since they always have the same convention. |
ec4867fa | 4708 | |
ce2b6ba5 JM |
4709 | if not Is_Predefined_Dispatching_Operation (Prim_Op) then |
4710 | Check_Convention (Prim_Op); | |
ec4867fa ES |
4711 | end if; |
4712 | ||
4713 | Next_Elmt (Prim_Op_Elmt); | |
4714 | end loop; | |
4715 | end Check_Conventions; | |
4716 | ||
996ae0b0 RK |
4717 | ------------------------------ |
4718 | -- Check_Delayed_Subprogram -- | |
4719 | ------------------------------ | |
4720 | ||
4721 | procedure Check_Delayed_Subprogram (Designator : Entity_Id) is | |
4722 | F : Entity_Id; | |
4723 | ||
4724 | procedure Possible_Freeze (T : Entity_Id); | |
4725 | -- T is the type of either a formal parameter or of the return type. | |
4726 | -- If T is not yet frozen and needs a delayed freeze, then the | |
4a13695c AC |
4727 | -- subprogram itself must be delayed. If T is the limited view of an |
4728 | -- incomplete type the subprogram must be frozen as well, because | |
4729 | -- T may depend on local types that have not been frozen yet. | |
996ae0b0 | 4730 | |
82c80734 RD |
4731 | --------------------- |
4732 | -- Possible_Freeze -- | |
4733 | --------------------- | |
4734 | ||
996ae0b0 RK |
4735 | procedure Possible_Freeze (T : Entity_Id) is |
4736 | begin | |
4a13695c | 4737 | if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then |
996ae0b0 RK |
4738 | Set_Has_Delayed_Freeze (Designator); |
4739 | ||
4740 | elsif Is_Access_Type (T) | |
4741 | and then Has_Delayed_Freeze (Designated_Type (T)) | |
4742 | and then not Is_Frozen (Designated_Type (T)) | |
4743 | then | |
4744 | Set_Has_Delayed_Freeze (Designator); | |
e358346d | 4745 | |
4a13695c | 4746 | elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then |
e358346d | 4747 | Set_Has_Delayed_Freeze (Designator); |
406935b6 | 4748 | |
9aff36e9 RD |
4749 | -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile |
4750 | -- of a subprogram or entry declaration. | |
406935b6 AC |
4751 | |
4752 | elsif Ekind (T) = E_Incomplete_Type | |
4753 | and then Ada_Version >= Ada_2012 | |
4754 | then | |
4755 | Set_Has_Delayed_Freeze (Designator); | |
996ae0b0 | 4756 | end if; |
4a13695c | 4757 | |
996ae0b0 RK |
4758 | end Possible_Freeze; |
4759 | ||
4760 | -- Start of processing for Check_Delayed_Subprogram | |
4761 | ||
4762 | begin | |
76e3504f AC |
4763 | -- All subprograms, including abstract subprograms, may need a freeze |
4764 | -- node if some formal type or the return type needs one. | |
996ae0b0 | 4765 | |
76e3504f AC |
4766 | Possible_Freeze (Etype (Designator)); |
4767 | Possible_Freeze (Base_Type (Etype (Designator))); -- needed ??? | |
996ae0b0 | 4768 | |
76e3504f AC |
4769 | -- Need delayed freeze if any of the formal types themselves need |
4770 | -- a delayed freeze and are not yet frozen. | |
996ae0b0 | 4771 | |
76e3504f AC |
4772 | F := First_Formal (Designator); |
4773 | while Present (F) loop | |
4774 | Possible_Freeze (Etype (F)); | |
4775 | Possible_Freeze (Base_Type (Etype (F))); -- needed ??? | |
4776 | Next_Formal (F); | |
4777 | end loop; | |
996ae0b0 RK |
4778 | |
4779 | -- Mark functions that return by reference. Note that it cannot be | |
4780 | -- done for delayed_freeze subprograms because the underlying | |
4781 | -- returned type may not be known yet (for private types) | |
4782 | ||
4783 | if not Has_Delayed_Freeze (Designator) | |
4784 | and then Expander_Active | |
4785 | then | |
4786 | declare | |
4787 | Typ : constant Entity_Id := Etype (Designator); | |
4788 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
9694c039 | 4789 | |
996ae0b0 | 4790 | begin |
40f07b4b | 4791 | if Is_Immutably_Limited_Type (Typ) then |
996ae0b0 | 4792 | Set_Returns_By_Ref (Designator); |
9694c039 | 4793 | |
048e5cef | 4794 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
996ae0b0 RK |
4795 | Set_Returns_By_Ref (Designator); |
4796 | end if; | |
4797 | end; | |
4798 | end if; | |
4799 | end Check_Delayed_Subprogram; | |
4800 | ||
4801 | ------------------------------------ | |
4802 | -- Check_Discriminant_Conformance -- | |
4803 | ------------------------------------ | |
4804 | ||
4805 | procedure Check_Discriminant_Conformance | |
4806 | (N : Node_Id; | |
4807 | Prev : Entity_Id; | |
4808 | Prev_Loc : Node_Id) | |
4809 | is | |
4810 | Old_Discr : Entity_Id := First_Discriminant (Prev); | |
4811 | New_Discr : Node_Id := First (Discriminant_Specifications (N)); | |
4812 | New_Discr_Id : Entity_Id; | |
4813 | New_Discr_Type : Entity_Id; | |
4814 | ||
4815 | procedure Conformance_Error (Msg : String; N : Node_Id); | |
82c80734 RD |
4816 | -- Post error message for conformance error on given node. Two messages |
4817 | -- are output. The first points to the previous declaration with a | |
4818 | -- general "no conformance" message. The second is the detailed reason, | |
4819 | -- supplied as Msg. The parameter N provide information for a possible | |
4820 | -- & insertion in the message. | |
996ae0b0 RK |
4821 | |
4822 | ----------------------- | |
4823 | -- Conformance_Error -- | |
4824 | ----------------------- | |
4825 | ||
4826 | procedure Conformance_Error (Msg : String; N : Node_Id) is | |
4827 | begin | |
4828 | Error_Msg_Sloc := Sloc (Prev_Loc); | |
483c78cb RD |
4829 | Error_Msg_N -- CODEFIX |
4830 | ("not fully conformant with declaration#!", N); | |
996ae0b0 RK |
4831 | Error_Msg_NE (Msg, N, N); |
4832 | end Conformance_Error; | |
4833 | ||
4834 | -- Start of processing for Check_Discriminant_Conformance | |
4835 | ||
4836 | begin | |
4837 | while Present (Old_Discr) and then Present (New_Discr) loop | |
4838 | ||
4839 | New_Discr_Id := Defining_Identifier (New_Discr); | |
4840 | ||
82c80734 RD |
4841 | -- The subtype mark of the discriminant on the full type has not |
4842 | -- been analyzed so we do it here. For an access discriminant a new | |
4843 | -- type is created. | |
996ae0b0 RK |
4844 | |
4845 | if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then | |
4846 | New_Discr_Type := | |
4847 | Access_Definition (N, Discriminant_Type (New_Discr)); | |
4848 | ||
4849 | else | |
4850 | Analyze (Discriminant_Type (New_Discr)); | |
4851 | New_Discr_Type := Etype (Discriminant_Type (New_Discr)); | |
e50e1c5e AC |
4852 | |
4853 | -- Ada 2005: if the discriminant definition carries a null | |
4854 | -- exclusion, create an itype to check properly for consistency | |
4855 | -- with partial declaration. | |
4856 | ||
4857 | if Is_Access_Type (New_Discr_Type) | |
4858 | and then Null_Exclusion_Present (New_Discr) | |
4859 | then | |
4860 | New_Discr_Type := | |
4861 | Create_Null_Excluding_Itype | |
4862 | (T => New_Discr_Type, | |
4863 | Related_Nod => New_Discr, | |
4864 | Scope_Id => Current_Scope); | |
4865 | end if; | |
996ae0b0 RK |
4866 | end if; |
4867 | ||
4868 | if not Conforming_Types | |
4869 | (Etype (Old_Discr), New_Discr_Type, Fully_Conformant) | |
4870 | then | |
4871 | Conformance_Error ("type of & does not match!", New_Discr_Id); | |
4872 | return; | |
fbf5a39b | 4873 | else |
82c80734 RD |
4874 | -- Treat the new discriminant as an occurrence of the old one, |
4875 | -- for navigation purposes, and fill in some semantic | |
fbf5a39b AC |
4876 | -- information, for completeness. |
4877 | ||
4878 | Generate_Reference (Old_Discr, New_Discr_Id, 'r'); | |
4879 | Set_Etype (New_Discr_Id, Etype (Old_Discr)); | |
4880 | Set_Scope (New_Discr_Id, Scope (Old_Discr)); | |
996ae0b0 RK |
4881 | end if; |
4882 | ||
4883 | -- Names must match | |
4884 | ||
4885 | if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then | |
4886 | Conformance_Error ("name & does not match!", New_Discr_Id); | |
4887 | return; | |
4888 | end if; | |
4889 | ||
4890 | -- Default expressions must match | |
4891 | ||
4892 | declare | |
4893 | NewD : constant Boolean := | |
4894 | Present (Expression (New_Discr)); | |
4895 | OldD : constant Boolean := | |
4896 | Present (Expression (Parent (Old_Discr))); | |
4897 | ||
4898 | begin | |
4899 | if NewD or OldD then | |
4900 | ||
4901 | -- The old default value has been analyzed and expanded, | |
4902 | -- because the current full declaration will have frozen | |
82c80734 RD |
4903 | -- everything before. The new default values have not been |
4904 | -- expanded, so expand now to check conformance. | |
996ae0b0 RK |
4905 | |
4906 | if NewD then | |
21d27997 | 4907 | Preanalyze_Spec_Expression |
996ae0b0 RK |
4908 | (Expression (New_Discr), New_Discr_Type); |
4909 | end if; | |
4910 | ||
4911 | if not (NewD and OldD) | |
4912 | or else not Fully_Conformant_Expressions | |
4913 | (Expression (Parent (Old_Discr)), | |
4914 | Expression (New_Discr)) | |
4915 | ||
4916 | then | |
4917 | Conformance_Error | |
4918 | ("default expression for & does not match!", | |
4919 | New_Discr_Id); | |
4920 | return; | |
4921 | end if; | |
4922 | end if; | |
4923 | end; | |
4924 | ||
4925 | -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X) | |
4926 | ||
0ab80019 | 4927 | if Ada_Version = Ada_83 then |
996ae0b0 RK |
4928 | declare |
4929 | Old_Disc : constant Node_Id := Declaration_Node (Old_Discr); | |
4930 | ||
4931 | begin | |
4932 | -- Grouping (use of comma in param lists) must be the same | |
4933 | -- This is where we catch a misconformance like: | |
4934 | ||
60370fb1 | 4935 | -- A, B : Integer |
996ae0b0 RK |
4936 | -- A : Integer; B : Integer |
4937 | ||
4938 | -- which are represented identically in the tree except | |
4939 | -- for the setting of the flags More_Ids and Prev_Ids. | |
4940 | ||
4941 | if More_Ids (Old_Disc) /= More_Ids (New_Discr) | |
4942 | or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr) | |
4943 | then | |
4944 | Conformance_Error | |
4945 | ("grouping of & does not match!", New_Discr_Id); | |
4946 | return; | |
4947 | end if; | |
4948 | end; | |
4949 | end if; | |
4950 | ||
4951 | Next_Discriminant (Old_Discr); | |
4952 | Next (New_Discr); | |
4953 | end loop; | |
4954 | ||
4955 | if Present (Old_Discr) then | |
4956 | Conformance_Error ("too few discriminants!", Defining_Identifier (N)); | |
4957 | return; | |
4958 | ||
4959 | elsif Present (New_Discr) then | |
4960 | Conformance_Error | |
4961 | ("too many discriminants!", Defining_Identifier (New_Discr)); | |
4962 | return; | |
4963 | end if; | |
4964 | end Check_Discriminant_Conformance; | |
4965 | ||
4966 | ---------------------------- | |
4967 | -- Check_Fully_Conformant -- | |
4968 | ---------------------------- | |
4969 | ||
4970 | procedure Check_Fully_Conformant | |
4971 | (New_Id : Entity_Id; | |
4972 | Old_Id : Entity_Id; | |
4973 | Err_Loc : Node_Id := Empty) | |
4974 | is | |
4975 | Result : Boolean; | |
81db9d77 | 4976 | pragma Warnings (Off, Result); |
996ae0b0 RK |
4977 | begin |
4978 | Check_Conformance | |
4979 | (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc); | |
4980 | end Check_Fully_Conformant; | |
4981 | ||
4982 | --------------------------- | |
4983 | -- Check_Mode_Conformant -- | |
4984 | --------------------------- | |
4985 | ||
4986 | procedure Check_Mode_Conformant | |
4987 | (New_Id : Entity_Id; | |
4988 | Old_Id : Entity_Id; | |
4989 | Err_Loc : Node_Id := Empty; | |
4990 | Get_Inst : Boolean := False) | |
4991 | is | |
4992 | Result : Boolean; | |
81db9d77 | 4993 | pragma Warnings (Off, Result); |
996ae0b0 RK |
4994 | begin |
4995 | Check_Conformance | |
4996 | (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst); | |
4997 | end Check_Mode_Conformant; | |
4998 | ||
fbf5a39b | 4999 | -------------------------------- |
758c442c | 5000 | -- Check_Overriding_Indicator -- |
fbf5a39b AC |
5001 | -------------------------------- |
5002 | ||
758c442c | 5003 | procedure Check_Overriding_Indicator |
ec4867fa | 5004 | (Subp : Entity_Id; |
5d37ba92 ES |
5005 | Overridden_Subp : Entity_Id; |
5006 | Is_Primitive : Boolean) | |
fbf5a39b | 5007 | is |
758c442c GD |
5008 | Decl : Node_Id; |
5009 | Spec : Node_Id; | |
fbf5a39b AC |
5010 | |
5011 | begin | |
ec4867fa | 5012 | -- No overriding indicator for literals |
fbf5a39b | 5013 | |
ec4867fa | 5014 | if Ekind (Subp) = E_Enumeration_Literal then |
758c442c | 5015 | return; |
fbf5a39b | 5016 | |
ec4867fa ES |
5017 | elsif Ekind (Subp) = E_Entry then |
5018 | Decl := Parent (Subp); | |
5019 | ||
53b10ce9 AC |
5020 | -- No point in analyzing a malformed operator |
5021 | ||
5022 | elsif Nkind (Subp) = N_Defining_Operator_Symbol | |
5023 | and then Error_Posted (Subp) | |
5024 | then | |
5025 | return; | |
5026 | ||
758c442c GD |
5027 | else |
5028 | Decl := Unit_Declaration_Node (Subp); | |
5029 | end if; | |
fbf5a39b | 5030 | |
800621e0 RD |
5031 | if Nkind_In (Decl, N_Subprogram_Body, |
5032 | N_Subprogram_Body_Stub, | |
5033 | N_Subprogram_Declaration, | |
5034 | N_Abstract_Subprogram_Declaration, | |
5035 | N_Subprogram_Renaming_Declaration) | |
758c442c GD |
5036 | then |
5037 | Spec := Specification (Decl); | |
ec4867fa ES |
5038 | |
5039 | elsif Nkind (Decl) = N_Entry_Declaration then | |
5040 | Spec := Decl; | |
5041 | ||
758c442c GD |
5042 | else |
5043 | return; | |
5044 | end if; | |
fbf5a39b | 5045 | |
e7d72fb9 AC |
5046 | -- The overriding operation is type conformant with the overridden one, |
5047 | -- but the names of the formals are not required to match. If the names | |
6823270c | 5048 | -- appear permuted in the overriding operation, this is a possible |
e7d72fb9 AC |
5049 | -- source of confusion that is worth diagnosing. Controlling formals |
5050 | -- often carry names that reflect the type, and it is not worthwhile | |
5051 | -- requiring that their names match. | |
5052 | ||
c9e7bd8e | 5053 | if Present (Overridden_Subp) |
e7d72fb9 AC |
5054 | and then Nkind (Subp) /= N_Defining_Operator_Symbol |
5055 | then | |
5056 | declare | |
5057 | Form1 : Entity_Id; | |
5058 | Form2 : Entity_Id; | |
5059 | ||
5060 | begin | |
5061 | Form1 := First_Formal (Subp); | |
5062 | Form2 := First_Formal (Overridden_Subp); | |
5063 | ||
c9e7bd8e AC |
5064 | -- If the overriding operation is a synchronized operation, skip |
5065 | -- the first parameter of the overridden operation, which is | |
6823270c AC |
5066 | -- implicit in the new one. If the operation is declared in the |
5067 | -- body it is not primitive and all formals must match. | |
c9e7bd8e | 5068 | |
6823270c AC |
5069 | if Is_Concurrent_Type (Scope (Subp)) |
5070 | and then Is_Tagged_Type (Scope (Subp)) | |
5071 | and then not Has_Completion (Scope (Subp)) | |
5072 | then | |
c9e7bd8e AC |
5073 | Form2 := Next_Formal (Form2); |
5074 | end if; | |
5075 | ||
e7d72fb9 AC |
5076 | if Present (Form1) then |
5077 | Form1 := Next_Formal (Form1); | |
5078 | Form2 := Next_Formal (Form2); | |
5079 | end if; | |
5080 | ||
5081 | while Present (Form1) loop | |
5082 | if not Is_Controlling_Formal (Form1) | |
5083 | and then Present (Next_Formal (Form2)) | |
5084 | and then Chars (Form1) = Chars (Next_Formal (Form2)) | |
5085 | then | |
5086 | Error_Msg_Node_2 := Alias (Overridden_Subp); | |
5087 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
ed2233dc | 5088 | Error_Msg_NE |
19d846a0 | 5089 | ("& does not match corresponding formal of&#", |
e7d72fb9 AC |
5090 | Form1, Form1); |
5091 | exit; | |
5092 | end if; | |
5093 | ||
5094 | Next_Formal (Form1); | |
5095 | Next_Formal (Form2); | |
5096 | end loop; | |
5097 | end; | |
5098 | end if; | |
5099 | ||
676e8420 AC |
5100 | -- If there is an overridden subprogram, then check that there is no |
5101 | -- "not overriding" indicator, and mark the subprogram as overriding. | |
51bf9bdf AC |
5102 | -- This is not done if the overridden subprogram is marked as hidden, |
5103 | -- which can occur for the case of inherited controlled operations | |
5104 | -- (see Derive_Subprogram), unless the inherited subprogram's parent | |
5105 | -- subprogram is not itself hidden. (Note: This condition could probably | |
5106 | -- be simplified, leaving out the testing for the specific controlled | |
5107 | -- cases, but it seems safer and clearer this way, and echoes similar | |
5108 | -- special-case tests of this kind in other places.) | |
5109 | ||
fd0d899b | 5110 | if Present (Overridden_Subp) |
51bf9bdf AC |
5111 | and then (not Is_Hidden (Overridden_Subp) |
5112 | or else | |
5113 | ((Chars (Overridden_Subp) = Name_Initialize | |
f0709ca6 AC |
5114 | or else |
5115 | Chars (Overridden_Subp) = Name_Adjust | |
5116 | or else | |
5117 | Chars (Overridden_Subp) = Name_Finalize) | |
5118 | and then Present (Alias (Overridden_Subp)) | |
5119 | and then not Is_Hidden (Alias (Overridden_Subp)))) | |
fd0d899b | 5120 | then |
ec4867fa ES |
5121 | if Must_Not_Override (Spec) then |
5122 | Error_Msg_Sloc := Sloc (Overridden_Subp); | |
fbf5a39b | 5123 | |
ec4867fa | 5124 | if Ekind (Subp) = E_Entry then |
ed2233dc | 5125 | Error_Msg_NE |
5d37ba92 | 5126 | ("entry & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5127 | else |
ed2233dc | 5128 | Error_Msg_NE |
5d37ba92 | 5129 | ("subprogram & overrides inherited operation #", Spec, Subp); |
ec4867fa | 5130 | end if; |
21d27997 | 5131 | |
bd603506 | 5132 | -- Special-case to fix a GNAT oddity: Limited_Controlled is declared |
24a120ac AC |
5133 | -- as an extension of Root_Controlled, and thus has a useless Adjust |
5134 | -- operation. This operation should not be inherited by other limited | |
5135 | -- controlled types. An explicit Adjust for them is not overriding. | |
5136 | ||
5137 | elsif Must_Override (Spec) | |
5138 | and then Chars (Overridden_Subp) = Name_Adjust | |
5139 | and then Is_Limited_Type (Etype (First_Formal (Subp))) | |
5140 | and then Present (Alias (Overridden_Subp)) | |
bd603506 RD |
5141 | and then |
5142 | Is_Predefined_File_Name | |
5143 | (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp)))) | |
24a120ac AC |
5144 | then |
5145 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5146 | ||
21d27997 | 5147 | elsif Is_Subprogram (Subp) then |
2fe829ae ES |
5148 | if Is_Init_Proc (Subp) then |
5149 | null; | |
5150 | ||
5151 | elsif No (Overridden_Operation (Subp)) then | |
1c1289e7 AC |
5152 | |
5153 | -- For entities generated by Derive_Subprograms the overridden | |
5154 | -- operation is the inherited primitive (which is available | |
5155 | -- through the attribute alias) | |
5156 | ||
5157 | if (Is_Dispatching_Operation (Subp) | |
f9673bb0 | 5158 | or else Is_Dispatching_Operation (Overridden_Subp)) |
1c1289e7 | 5159 | and then not Comes_From_Source (Overridden_Subp) |
f9673bb0 AC |
5160 | and then Find_Dispatching_Type (Overridden_Subp) = |
5161 | Find_Dispatching_Type (Subp) | |
1c1289e7 AC |
5162 | and then Present (Alias (Overridden_Subp)) |
5163 | and then Comes_From_Source (Alias (Overridden_Subp)) | |
5164 | then | |
5165 | Set_Overridden_Operation (Subp, Alias (Overridden_Subp)); | |
2fe829ae | 5166 | |
1c1289e7 AC |
5167 | else |
5168 | Set_Overridden_Operation (Subp, Overridden_Subp); | |
5169 | end if; | |
5170 | end if; | |
ec4867fa | 5171 | end if; |
f937473f | 5172 | |
618fb570 AC |
5173 | -- If primitive flag is set or this is a protected operation, then |
5174 | -- the operation is overriding at the point of its declaration, so | |
5175 | -- warn if necessary. Otherwise it may have been declared before the | |
5176 | -- operation it overrides and no check is required. | |
3c25856a AC |
5177 | |
5178 | if Style_Check | |
618fb570 AC |
5179 | and then not Must_Override (Spec) |
5180 | and then (Is_Primitive | |
5181 | or else Ekind (Scope (Subp)) = E_Protected_Type) | |
3c25856a | 5182 | then |
235f4375 AC |
5183 | Style.Missing_Overriding (Decl, Subp); |
5184 | end if; | |
5185 | ||
53b10ce9 AC |
5186 | -- If Subp is an operator, it may override a predefined operation, if |
5187 | -- it is defined in the same scope as the type to which it applies. | |
676e8420 | 5188 | -- In that case Overridden_Subp is empty because of our implicit |
5d37ba92 ES |
5189 | -- representation for predefined operators. We have to check whether the |
5190 | -- signature of Subp matches that of a predefined operator. Note that | |
5191 | -- first argument provides the name of the operator, and the second | |
5192 | -- argument the signature that may match that of a standard operation. | |
21d27997 RD |
5193 | -- If the indicator is overriding, then the operator must match a |
5194 | -- predefined signature, because we know already that there is no | |
5195 | -- explicit overridden operation. | |
f937473f | 5196 | |
21d27997 | 5197 | elsif Nkind (Subp) = N_Defining_Operator_Symbol then |
806f6d37 | 5198 | if Must_Not_Override (Spec) then |
f937473f | 5199 | |
806f6d37 AC |
5200 | -- If this is not a primitive or a protected subprogram, then |
5201 | -- "not overriding" is illegal. | |
618fb570 | 5202 | |
806f6d37 AC |
5203 | if not Is_Primitive |
5204 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5205 | then | |
5206 | Error_Msg_N | |
5207 | ("overriding indicator only allowed " | |
5208 | & "if subprogram is primitive", Subp); | |
618fb570 | 5209 | |
806f6d37 AC |
5210 | elsif Can_Override_Operator (Subp) then |
5211 | Error_Msg_NE | |
5212 | ("subprogram& overrides predefined operator ", Spec, Subp); | |
5213 | end if; | |
f937473f | 5214 | |
806f6d37 AC |
5215 | elsif Must_Override (Spec) then |
5216 | if No (Overridden_Operation (Subp)) | |
5217 | and then not Can_Override_Operator (Subp) | |
5218 | then | |
5219 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); | |
5220 | end if; | |
5d37ba92 | 5221 | |
806f6d37 AC |
5222 | elsif not Error_Posted (Subp) |
5223 | and then Style_Check | |
5224 | and then Can_Override_Operator (Subp) | |
5225 | and then | |
5226 | not Is_Predefined_File_Name | |
5227 | (Unit_File_Name (Get_Source_Unit (Subp))) | |
5228 | then | |
5229 | -- If style checks are enabled, indicate that the indicator is | |
5230 | -- missing. However, at the point of declaration, the type of | |
5231 | -- which this is a primitive operation may be private, in which | |
5232 | -- case the indicator would be premature. | |
235f4375 | 5233 | |
806f6d37 AC |
5234 | if Has_Private_Declaration (Etype (Subp)) |
5235 | or else Has_Private_Declaration (Etype (First_Formal (Subp))) | |
53b10ce9 | 5236 | then |
806f6d37 AC |
5237 | null; |
5238 | else | |
5239 | Style.Missing_Overriding (Decl, Subp); | |
5d5832bc | 5240 | end if; |
806f6d37 | 5241 | end if; |
21d27997 RD |
5242 | |
5243 | elsif Must_Override (Spec) then | |
5244 | if Ekind (Subp) = E_Entry then | |
ed2233dc | 5245 | Error_Msg_NE ("entry & is not overriding", Spec, Subp); |
5d37ba92 | 5246 | else |
ed2233dc | 5247 | Error_Msg_NE ("subprogram & is not overriding", Spec, Subp); |
758c442c | 5248 | end if; |
5d37ba92 ES |
5249 | |
5250 | -- If the operation is marked "not overriding" and it's not primitive | |
5251 | -- then an error is issued, unless this is an operation of a task or | |
5252 | -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding" | |
5253 | -- has been specified have already been checked above. | |
5254 | ||
5255 | elsif Must_Not_Override (Spec) | |
5256 | and then not Is_Primitive | |
5257 | and then Ekind (Subp) /= E_Entry | |
5258 | and then Ekind (Scope (Subp)) /= E_Protected_Type | |
5259 | then | |
ed2233dc | 5260 | Error_Msg_N |
5d37ba92 ES |
5261 | ("overriding indicator only allowed if subprogram is primitive", |
5262 | Subp); | |
5d37ba92 | 5263 | return; |
fbf5a39b | 5264 | end if; |
758c442c | 5265 | end Check_Overriding_Indicator; |
fbf5a39b | 5266 | |
996ae0b0 RK |
5267 | ------------------- |
5268 | -- Check_Returns -- | |
5269 | ------------------- | |
5270 | ||
0a36105d JM |
5271 | -- Note: this procedure needs to know far too much about how the expander |
5272 | -- messes with exceptions. The use of the flag Exception_Junk and the | |
5273 | -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers | |
5274 | -- works, but is not very clean. It would be better if the expansion | |
5275 | -- routines would leave Original_Node working nicely, and we could use | |
5276 | -- Original_Node here to ignore all the peculiar expander messing ??? | |
5277 | ||
996ae0b0 RK |
5278 | procedure Check_Returns |
5279 | (HSS : Node_Id; | |
5280 | Mode : Character; | |
c8ef728f ES |
5281 | Err : out Boolean; |
5282 | Proc : Entity_Id := Empty) | |
996ae0b0 RK |
5283 | is |
5284 | Handler : Node_Id; | |
5285 | ||
5286 | procedure Check_Statement_Sequence (L : List_Id); | |
5287 | -- Internal recursive procedure to check a list of statements for proper | |
5288 | -- termination by a return statement (or a transfer of control or a | |
5289 | -- compound statement that is itself internally properly terminated). | |
5290 | ||
5291 | ------------------------------ | |
5292 | -- Check_Statement_Sequence -- | |
5293 | ------------------------------ | |
5294 | ||
5295 | procedure Check_Statement_Sequence (L : List_Id) is | |
5296 | Last_Stm : Node_Id; | |
0a36105d | 5297 | Stm : Node_Id; |
996ae0b0 RK |
5298 | Kind : Node_Kind; |
5299 | ||
5300 | Raise_Exception_Call : Boolean; | |
5301 | -- Set True if statement sequence terminated by Raise_Exception call | |
5302 | -- or a Reraise_Occurrence call. | |
5303 | ||
5304 | begin | |
5305 | Raise_Exception_Call := False; | |
5306 | ||
5307 | -- Get last real statement | |
5308 | ||
5309 | Last_Stm := Last (L); | |
5310 | ||
0a36105d JM |
5311 | -- Deal with digging out exception handler statement sequences that |
5312 | -- have been transformed by the local raise to goto optimization. | |
5313 | -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this | |
5314 | -- optimization has occurred, we are looking at something like: | |
5315 | ||
5316 | -- begin | |
5317 | -- original stmts in block | |
5318 | ||
5319 | -- exception \ | |
5320 | -- when excep1 => | | |
5321 | -- goto L1; | omitted if No_Exception_Propagation | |
5322 | -- when excep2 => | | |
5323 | -- goto L2; / | |
5324 | -- end; | |
5325 | ||
5326 | -- goto L3; -- skip handler when exception not raised | |
5327 | ||
5328 | -- <<L1>> -- target label for local exception | |
5329 | -- begin | |
5330 | -- estmts1 | |
5331 | -- end; | |
5332 | ||
5333 | -- goto L3; | |
5334 | ||
5335 | -- <<L2>> | |
5336 | -- begin | |
5337 | -- estmts2 | |
5338 | -- end; | |
5339 | ||
5340 | -- <<L3>> | |
5341 | ||
5342 | -- and what we have to do is to dig out the estmts1 and estmts2 | |
5343 | -- sequences (which were the original sequences of statements in | |
5344 | -- the exception handlers) and check them. | |
5345 | ||
5346 | if Nkind (Last_Stm) = N_Label | |
5347 | and then Exception_Junk (Last_Stm) | |
5348 | then | |
5349 | Stm := Last_Stm; | |
5350 | loop | |
5351 | Prev (Stm); | |
5352 | exit when No (Stm); | |
5353 | exit when Nkind (Stm) /= N_Block_Statement; | |
5354 | exit when not Exception_Junk (Stm); | |
5355 | Prev (Stm); | |
5356 | exit when No (Stm); | |
5357 | exit when Nkind (Stm) /= N_Label; | |
5358 | exit when not Exception_Junk (Stm); | |
5359 | Check_Statement_Sequence | |
5360 | (Statements (Handled_Statement_Sequence (Next (Stm)))); | |
5361 | ||
5362 | Prev (Stm); | |
5363 | Last_Stm := Stm; | |
5364 | exit when No (Stm); | |
5365 | exit when Nkind (Stm) /= N_Goto_Statement; | |
5366 | exit when not Exception_Junk (Stm); | |
5367 | end loop; | |
5368 | end if; | |
5369 | ||
996ae0b0 RK |
5370 | -- Don't count pragmas |
5371 | ||
5372 | while Nkind (Last_Stm) = N_Pragma | |
5373 | ||
5374 | -- Don't count call to SS_Release (can happen after Raise_Exception) | |
5375 | ||
5376 | or else | |
5377 | (Nkind (Last_Stm) = N_Procedure_Call_Statement | |
5378 | and then | |
5379 | Nkind (Name (Last_Stm)) = N_Identifier | |
5380 | and then | |
5381 | Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release)) | |
5382 | ||
5383 | -- Don't count exception junk | |
5384 | ||
5385 | or else | |
800621e0 RD |
5386 | (Nkind_In (Last_Stm, N_Goto_Statement, |
5387 | N_Label, | |
5388 | N_Object_Declaration) | |
0a36105d JM |
5389 | and then Exception_Junk (Last_Stm)) |
5390 | or else Nkind (Last_Stm) in N_Push_xxx_Label | |
5391 | or else Nkind (Last_Stm) in N_Pop_xxx_Label | |
996ae0b0 RK |
5392 | loop |
5393 | Prev (Last_Stm); | |
5394 | end loop; | |
5395 | ||
5396 | -- Here we have the "real" last statement | |
5397 | ||
5398 | Kind := Nkind (Last_Stm); | |
5399 | ||
5400 | -- Transfer of control, OK. Note that in the No_Return procedure | |
5401 | -- case, we already diagnosed any explicit return statements, so | |
5402 | -- we can treat them as OK in this context. | |
5403 | ||
5404 | if Is_Transfer (Last_Stm) then | |
5405 | return; | |
5406 | ||
5407 | -- Check cases of explicit non-indirect procedure calls | |
5408 | ||
5409 | elsif Kind = N_Procedure_Call_Statement | |
5410 | and then Is_Entity_Name (Name (Last_Stm)) | |
5411 | then | |
5412 | -- Check call to Raise_Exception procedure which is treated | |
5413 | -- specially, as is a call to Reraise_Occurrence. | |
5414 | ||
5415 | -- We suppress the warning in these cases since it is likely that | |
5416 | -- the programmer really does not expect to deal with the case | |
5417 | -- of Null_Occurrence, and thus would find a warning about a | |
5418 | -- missing return curious, and raising Program_Error does not | |
5419 | -- seem such a bad behavior if this does occur. | |
5420 | ||
c8ef728f ES |
5421 | -- Note that in the Ada 2005 case for Raise_Exception, the actual |
5422 | -- behavior will be to raise Constraint_Error (see AI-329). | |
5423 | ||
996ae0b0 RK |
5424 | if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception) |
5425 | or else | |
5426 | Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence) | |
5427 | then | |
5428 | Raise_Exception_Call := True; | |
5429 | ||
5430 | -- For Raise_Exception call, test first argument, if it is | |
5431 | -- an attribute reference for a 'Identity call, then we know | |
5432 | -- that the call cannot possibly return. | |
5433 | ||
5434 | declare | |
5435 | Arg : constant Node_Id := | |
5436 | Original_Node (First_Actual (Last_Stm)); | |
996ae0b0 RK |
5437 | begin |
5438 | if Nkind (Arg) = N_Attribute_Reference | |
5439 | and then Attribute_Name (Arg) = Name_Identity | |
5440 | then | |
5441 | return; | |
5442 | end if; | |
5443 | end; | |
5444 | end if; | |
5445 | ||
5446 | -- If statement, need to look inside if there is an else and check | |
5447 | -- each constituent statement sequence for proper termination. | |
5448 | ||
5449 | elsif Kind = N_If_Statement | |
5450 | and then Present (Else_Statements (Last_Stm)) | |
5451 | then | |
5452 | Check_Statement_Sequence (Then_Statements (Last_Stm)); | |
5453 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
5454 | ||
5455 | if Present (Elsif_Parts (Last_Stm)) then | |
5456 | declare | |
5457 | Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm)); | |
5458 | ||
5459 | begin | |
5460 | while Present (Elsif_Part) loop | |
5461 | Check_Statement_Sequence (Then_Statements (Elsif_Part)); | |
5462 | Next (Elsif_Part); | |
5463 | end loop; | |
5464 | end; | |
5465 | end if; | |
5466 | ||
5467 | return; | |
5468 | ||
5469 | -- Case statement, check each case for proper termination | |
5470 | ||
5471 | elsif Kind = N_Case_Statement then | |
5472 | declare | |
5473 | Case_Alt : Node_Id; | |
996ae0b0 RK |
5474 | begin |
5475 | Case_Alt := First_Non_Pragma (Alternatives (Last_Stm)); | |
5476 | while Present (Case_Alt) loop | |
5477 | Check_Statement_Sequence (Statements (Case_Alt)); | |
5478 | Next_Non_Pragma (Case_Alt); | |
5479 | end loop; | |
5480 | end; | |
5481 | ||
5482 | return; | |
5483 | ||
5484 | -- Block statement, check its handled sequence of statements | |
5485 | ||
5486 | elsif Kind = N_Block_Statement then | |
5487 | declare | |
5488 | Err1 : Boolean; | |
5489 | ||
5490 | begin | |
5491 | Check_Returns | |
5492 | (Handled_Statement_Sequence (Last_Stm), Mode, Err1); | |
5493 | ||
5494 | if Err1 then | |
5495 | Err := True; | |
5496 | end if; | |
5497 | ||
5498 | return; | |
5499 | end; | |
5500 | ||
5501 | -- Loop statement. If there is an iteration scheme, we can definitely | |
5502 | -- fall out of the loop. Similarly if there is an exit statement, we | |
5503 | -- can fall out. In either case we need a following return. | |
5504 | ||
5505 | elsif Kind = N_Loop_Statement then | |
5506 | if Present (Iteration_Scheme (Last_Stm)) | |
5507 | or else Has_Exit (Entity (Identifier (Last_Stm))) | |
5508 | then | |
5509 | null; | |
5510 | ||
f3d57416 RW |
5511 | -- A loop with no exit statement or iteration scheme is either |
5512 | -- an infinite loop, or it has some other exit (raise/return). | |
996ae0b0 RK |
5513 | -- In either case, no warning is required. |
5514 | ||
5515 | else | |
5516 | return; | |
5517 | end if; | |
5518 | ||
5519 | -- Timed entry call, check entry call and delay alternatives | |
5520 | ||
5521 | -- Note: in expanded code, the timed entry call has been converted | |
5522 | -- to a set of expanded statements on which the check will work | |
5523 | -- correctly in any case. | |
5524 | ||
5525 | elsif Kind = N_Timed_Entry_Call then | |
5526 | declare | |
5527 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
5528 | DCA : constant Node_Id := Delay_Alternative (Last_Stm); | |
5529 | ||
5530 | begin | |
5531 | -- If statement sequence of entry call alternative is missing, | |
5532 | -- then we can definitely fall through, and we post the error | |
5533 | -- message on the entry call alternative itself. | |
5534 | ||
5535 | if No (Statements (ECA)) then | |
5536 | Last_Stm := ECA; | |
5537 | ||
5538 | -- If statement sequence of delay alternative is missing, then | |
5539 | -- we can definitely fall through, and we post the error | |
5540 | -- message on the delay alternative itself. | |
5541 | ||
5542 | -- Note: if both ECA and DCA are missing the return, then we | |
5543 | -- post only one message, should be enough to fix the bugs. | |
5544 | -- If not we will get a message next time on the DCA when the | |
5545 | -- ECA is fixed! | |
5546 | ||
5547 | elsif No (Statements (DCA)) then | |
5548 | Last_Stm := DCA; | |
5549 | ||
5550 | -- Else check both statement sequences | |
5551 | ||
5552 | else | |
5553 | Check_Statement_Sequence (Statements (ECA)); | |
5554 | Check_Statement_Sequence (Statements (DCA)); | |
5555 | return; | |
5556 | end if; | |
5557 | end; | |
5558 | ||
5559 | -- Conditional entry call, check entry call and else part | |
5560 | ||
5561 | -- Note: in expanded code, the conditional entry call has been | |
5562 | -- converted to a set of expanded statements on which the check | |
5563 | -- will work correctly in any case. | |
5564 | ||
5565 | elsif Kind = N_Conditional_Entry_Call then | |
5566 | declare | |
5567 | ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm); | |
5568 | ||
5569 | begin | |
5570 | -- If statement sequence of entry call alternative is missing, | |
5571 | -- then we can definitely fall through, and we post the error | |
5572 | -- message on the entry call alternative itself. | |
5573 | ||
5574 | if No (Statements (ECA)) then | |
5575 | Last_Stm := ECA; | |
5576 | ||
5577 | -- Else check statement sequence and else part | |
5578 | ||
5579 | else | |
5580 | Check_Statement_Sequence (Statements (ECA)); | |
5581 | Check_Statement_Sequence (Else_Statements (Last_Stm)); | |
5582 | return; | |
5583 | end if; | |
5584 | end; | |
5585 | end if; | |
5586 | ||
5587 | -- If we fall through, issue appropriate message | |
5588 | ||
5589 | if Mode = 'F' then | |
996ae0b0 RK |
5590 | if not Raise_Exception_Call then |
5591 | Error_Msg_N | |
5d37ba92 | 5592 | ("?RETURN statement missing following this statement!", |
996ae0b0 RK |
5593 | Last_Stm); |
5594 | Error_Msg_N | |
5d37ba92 | 5595 | ("\?Program_Error may be raised at run time!", |
996ae0b0 RK |
5596 | Last_Stm); |
5597 | end if; | |
5598 | ||
5599 | -- Note: we set Err even though we have not issued a warning | |
5600 | -- because we still have a case of a missing return. This is | |
5601 | -- an extremely marginal case, probably will never be noticed | |
5602 | -- but we might as well get it right. | |
5603 | ||
5604 | Err := True; | |
5605 | ||
c8ef728f ES |
5606 | -- Otherwise we have the case of a procedure marked No_Return |
5607 | ||
996ae0b0 | 5608 | else |
800621e0 RD |
5609 | if not Raise_Exception_Call then |
5610 | Error_Msg_N | |
5611 | ("?implied return after this statement " & | |
5612 | "will raise Program_Error", | |
5613 | Last_Stm); | |
5614 | Error_Msg_NE | |
5615 | ("\?procedure & is marked as No_Return!", | |
5616 | Last_Stm, Proc); | |
5617 | end if; | |
c8ef728f ES |
5618 | |
5619 | declare | |
5620 | RE : constant Node_Id := | |
5621 | Make_Raise_Program_Error (Sloc (Last_Stm), | |
5622 | Reason => PE_Implicit_Return); | |
5623 | begin | |
5624 | Insert_After (Last_Stm, RE); | |
5625 | Analyze (RE); | |
5626 | end; | |
996ae0b0 RK |
5627 | end if; |
5628 | end Check_Statement_Sequence; | |
5629 | ||
5630 | -- Start of processing for Check_Returns | |
5631 | ||
5632 | begin | |
5633 | Err := False; | |
5634 | Check_Statement_Sequence (Statements (HSS)); | |
5635 | ||
5636 | if Present (Exception_Handlers (HSS)) then | |
5637 | Handler := First_Non_Pragma (Exception_Handlers (HSS)); | |
5638 | while Present (Handler) loop | |
5639 | Check_Statement_Sequence (Statements (Handler)); | |
5640 | Next_Non_Pragma (Handler); | |
5641 | end loop; | |
5642 | end if; | |
5643 | end Check_Returns; | |
5644 | ||
67c86178 AC |
5645 | ------------------------------- |
5646 | -- Check_Subprogram_Contract -- | |
5647 | ------------------------------- | |
5648 | ||
5649 | procedure Check_Subprogram_Contract (Spec_Id : Entity_Id) is | |
5650 | ||
029b67ba YM |
5651 | -- Code is currently commented out as, in some cases, it causes crashes |
5652 | -- because Direct_Primitive_Operations is not available for a private | |
5653 | -- type. This may cause more warnings to be issued than necessary. See | |
5654 | -- below for the intended use of this variable. ??? | |
5655 | ||
67c86178 AC |
5656 | -- Inherited : constant Subprogram_List := |
5657 | -- Inherited_Subprograms (Spec_Id); | |
dc36a7e3 | 5658 | -- -- List of subprograms inherited by this subprogram |
67c86178 | 5659 | |
dc36a7e3 | 5660 | Last_Postcondition : Node_Id := Empty; |
67c86178 AC |
5661 | -- Last postcondition on the subprogram, or else Empty if either no |
5662 | -- postcondition or only inherited postconditions. | |
5663 | ||
5664 | Attribute_Result_Mentioned : Boolean := False; | |
5665 | -- Whether attribute 'Result is mentioned in a postcondition | |
5666 | ||
dc36a7e3 | 5667 | Post_State_Mentioned : Boolean := False; |
67c86178 AC |
5668 | -- Whether some expression mentioned in a postcondition can have a |
5669 | -- different value in the post-state than in the pre-state. | |
5670 | ||
5671 | function Check_Attr_Result (N : Node_Id) return Traverse_Result; | |
dc36a7e3 RD |
5672 | -- Check if N is a reference to the attribute 'Result, and if so set |
5673 | -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK. | |
67c86178 AC |
5674 | |
5675 | function Check_Post_State (N : Node_Id) return Traverse_Result; | |
5676 | -- Check whether the value of evaluating N can be different in the | |
5677 | -- post-state, compared to the same evaluation in the pre-state, and | |
5678 | -- if so set Post_State_Mentioned and return Abandon. Return Skip on | |
5679 | -- reference to attribute 'Old, in order to ignore its prefix, which | |
5680 | -- is precisely evaluated in the pre-state. Otherwise return OK. | |
5681 | ||
dc36a7e3 | 5682 | procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean); |
67c86178 AC |
5683 | -- This processes the Spec_PPC_List from Spec, processing any |
5684 | -- postconditions from the list. If Class is True, then only | |
5685 | -- postconditions marked with Class_Present are considered. The | |
5686 | -- caller has checked that Spec_PPC_List is non-Empty. | |
5687 | ||
5688 | function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result); | |
5689 | ||
5690 | function Find_Post_State is new Traverse_Func (Check_Post_State); | |
5691 | ||
5692 | ----------------------- | |
5693 | -- Check_Attr_Result -- | |
5694 | ----------------------- | |
5695 | ||
5696 | function Check_Attr_Result (N : Node_Id) return Traverse_Result is | |
5697 | begin | |
5698 | if Nkind (N) = N_Attribute_Reference | |
dc36a7e3 | 5699 | and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result |
67c86178 AC |
5700 | then |
5701 | Attribute_Result_Mentioned := True; | |
5702 | return Abandon; | |
5703 | else | |
5704 | return OK; | |
5705 | end if; | |
5706 | end Check_Attr_Result; | |
5707 | ||
5708 | ---------------------- | |
5709 | -- Check_Post_State -- | |
5710 | ---------------------- | |
5711 | ||
5712 | function Check_Post_State (N : Node_Id) return Traverse_Result is | |
5713 | Found : Boolean := False; | |
5714 | ||
5715 | begin | |
5716 | case Nkind (N) is | |
5717 | when N_Function_Call | | |
5718 | N_Explicit_Dereference => | |
5719 | Found := True; | |
5720 | ||
5721 | when N_Identifier | | |
5722 | N_Expanded_Name => | |
dc36a7e3 | 5723 | |
67c86178 AC |
5724 | declare |
5725 | E : constant Entity_Id := Entity (N); | |
bd38b431 | 5726 | |
67c86178 | 5727 | begin |
bd38b431 AC |
5728 | -- ???Quantified expressions get analyzed later, so E can |
5729 | -- be empty at this point. In this case, we suppress the | |
5b5588dd AC |
5730 | -- warning, just in case E is assignable. It seems better to |
5731 | -- have false negatives than false positives. At some point, | |
5732 | -- we should make the warning more accurate, either by | |
bd38b431 AC |
5733 | -- analyzing quantified expressions earlier, or moving |
5734 | -- this processing later. | |
5b5588dd | 5735 | |
bd38b431 AC |
5736 | if No (E) |
5737 | or else | |
5738 | (Is_Entity_Name (N) | |
5739 | and then Ekind (E) in Assignable_Kind) | |
67c86178 AC |
5740 | then |
5741 | Found := True; | |
5742 | end if; | |
5743 | end; | |
5744 | ||
5745 | when N_Attribute_Reference => | |
5746 | case Get_Attribute_Id (Attribute_Name (N)) is | |
5747 | when Attribute_Old => | |
5748 | return Skip; | |
5749 | when Attribute_Result => | |
5750 | Found := True; | |
5751 | when others => | |
5752 | null; | |
5753 | end case; | |
5754 | ||
5755 | when others => | |
5756 | null; | |
5757 | end case; | |
5758 | ||
5759 | if Found then | |
5760 | Post_State_Mentioned := True; | |
5761 | return Abandon; | |
5762 | else | |
5763 | return OK; | |
5764 | end if; | |
5765 | end Check_Post_State; | |
5766 | ||
5767 | ----------------------------- | |
5768 | -- Process_Post_Conditions -- | |
5769 | ----------------------------- | |
5770 | ||
5771 | procedure Process_Post_Conditions | |
5772 | (Spec : Node_Id; | |
5773 | Class : Boolean) | |
5774 | is | |
5775 | Prag : Node_Id; | |
5776 | Arg : Node_Id; | |
5777 | Ignored : Traverse_Final_Result; | |
5778 | pragma Unreferenced (Ignored); | |
5779 | ||
5780 | begin | |
5781 | Prag := Spec_PPC_List (Contract (Spec)); | |
5782 | ||
5783 | loop | |
5784 | Arg := First (Pragma_Argument_Associations (Prag)); | |
5785 | ||
dc36a7e3 | 5786 | -- Since pre- and post-conditions are listed in reverse order, the |
67c86178 AC |
5787 | -- first postcondition in the list is the last in the source. |
5788 | ||
5789 | if Pragma_Name (Prag) = Name_Postcondition | |
5790 | and then not Class | |
5791 | and then No (Last_Postcondition) | |
5792 | then | |
5793 | Last_Postcondition := Prag; | |
5794 | end if; | |
5795 | ||
5796 | -- For functions, look for presence of 'Result in postcondition | |
5797 | ||
5798 | if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then | |
5799 | Ignored := Find_Attribute_Result (Arg); | |
5800 | end if; | |
5801 | ||
5802 | -- For each individual non-inherited postcondition, look for | |
5803 | -- presence of an expression that could be evaluated differently | |
5804 | -- in post-state. | |
5805 | ||
5806 | if Pragma_Name (Prag) = Name_Postcondition | |
5807 | and then not Class | |
5808 | then | |
5809 | Post_State_Mentioned := False; | |
dc36a7e3 | 5810 | Ignored := Find_Post_State (Arg); |
67c86178 AC |
5811 | |
5812 | if not Post_State_Mentioned then | |
5b5588dd | 5813 | Error_Msg_N ("?postcondition refers only to pre-state", |
67c86178 AC |
5814 | Prag); |
5815 | end if; | |
5816 | end if; | |
5817 | ||
5818 | Prag := Next_Pragma (Prag); | |
5819 | exit when No (Prag); | |
5820 | end loop; | |
5821 | end Process_Post_Conditions; | |
5822 | ||
5823 | -- Start of processing for Check_Subprogram_Contract | |
5824 | ||
5825 | begin | |
5826 | if not Warn_On_Suspicious_Contract then | |
5827 | return; | |
5828 | end if; | |
5829 | ||
5830 | if Present (Spec_PPC_List (Contract (Spec_Id))) then | |
5831 | Process_Post_Conditions (Spec_Id, Class => False); | |
5832 | end if; | |
5833 | ||
5834 | -- Process inherited postconditions | |
5835 | ||
5836 | -- Code is currently commented out as, in some cases, it causes crashes | |
5837 | -- because Direct_Primitive_Operations is not available for a private | |
dc36a7e3 | 5838 | -- type. This may cause more warnings to be issued than necessary. ??? |
67c86178 AC |
5839 | |
5840 | -- for J in Inherited'Range loop | |
5841 | -- if Present (Spec_PPC_List (Contract (Inherited (J)))) then | |
5842 | -- Process_Post_Conditions (Inherited (J), Class => True); | |
5843 | -- end if; | |
5844 | -- end loop; | |
5845 | ||
5846 | -- Issue warning for functions whose postcondition does not mention | |
5847 | -- 'Result after all postconditions have been processed. | |
5848 | ||
5849 | if Ekind_In (Spec_Id, E_Function, E_Generic_Function) | |
5850 | and then Present (Last_Postcondition) | |
5851 | and then not Attribute_Result_Mentioned | |
5852 | then | |
5853 | Error_Msg_N ("?function postcondition does not mention result", | |
5854 | Last_Postcondition); | |
5855 | end if; | |
5856 | end Check_Subprogram_Contract; | |
5857 | ||
996ae0b0 RK |
5858 | ---------------------------- |
5859 | -- Check_Subprogram_Order -- | |
5860 | ---------------------------- | |
5861 | ||
5862 | procedure Check_Subprogram_Order (N : Node_Id) is | |
5863 | ||
5864 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean; | |
dc36a7e3 RD |
5865 | -- This is used to check if S1 > S2 in the sense required by this test, |
5866 | -- for example nameab < namec, but name2 < name10. | |
996ae0b0 | 5867 | |
82c80734 RD |
5868 | ----------------------------- |
5869 | -- Subprogram_Name_Greater -- | |
5870 | ----------------------------- | |
5871 | ||
996ae0b0 RK |
5872 | function Subprogram_Name_Greater (S1, S2 : String) return Boolean is |
5873 | L1, L2 : Positive; | |
5874 | N1, N2 : Natural; | |
5875 | ||
5876 | begin | |
5877 | -- Remove trailing numeric parts | |
5878 | ||
5879 | L1 := S1'Last; | |
5880 | while S1 (L1) in '0' .. '9' loop | |
5881 | L1 := L1 - 1; | |
5882 | end loop; | |
5883 | ||
5884 | L2 := S2'Last; | |
5885 | while S2 (L2) in '0' .. '9' loop | |
5886 | L2 := L2 - 1; | |
5887 | end loop; | |
5888 | ||
5889 | -- If non-numeric parts non-equal, that's decisive | |
5890 | ||
5891 | if S1 (S1'First .. L1) < S2 (S2'First .. L2) then | |
5892 | return False; | |
5893 | ||
5894 | elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then | |
5895 | return True; | |
5896 | ||
5897 | -- If non-numeric parts equal, compare suffixed numeric parts. Note | |
5898 | -- that a missing suffix is treated as numeric zero in this test. | |
5899 | ||
5900 | else | |
5901 | N1 := 0; | |
5902 | while L1 < S1'Last loop | |
5903 | L1 := L1 + 1; | |
5904 | N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0'); | |
5905 | end loop; | |
5906 | ||
5907 | N2 := 0; | |
5908 | while L2 < S2'Last loop | |
5909 | L2 := L2 + 1; | |
5910 | N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0'); | |
5911 | end loop; | |
5912 | ||
5913 | return N1 > N2; | |
5914 | end if; | |
5915 | end Subprogram_Name_Greater; | |
5916 | ||
5917 | -- Start of processing for Check_Subprogram_Order | |
5918 | ||
5919 | begin | |
5920 | -- Check body in alpha order if this is option | |
5921 | ||
fbf5a39b | 5922 | if Style_Check |
bc202b70 | 5923 | and then Style_Check_Order_Subprograms |
996ae0b0 RK |
5924 | and then Nkind (N) = N_Subprogram_Body |
5925 | and then Comes_From_Source (N) | |
5926 | and then In_Extended_Main_Source_Unit (N) | |
5927 | then | |
5928 | declare | |
5929 | LSN : String_Ptr | |
5930 | renames Scope_Stack.Table | |
5931 | (Scope_Stack.Last).Last_Subprogram_Name; | |
5932 | ||
5933 | Body_Id : constant Entity_Id := | |
5934 | Defining_Entity (Specification (N)); | |
5935 | ||
5936 | begin | |
5937 | Get_Decoded_Name_String (Chars (Body_Id)); | |
5938 | ||
5939 | if LSN /= null then | |
5940 | if Subprogram_Name_Greater | |
5941 | (LSN.all, Name_Buffer (1 .. Name_Len)) | |
5942 | then | |
5943 | Style.Subprogram_Not_In_Alpha_Order (Body_Id); | |
5944 | end if; | |
5945 | ||
5946 | Free (LSN); | |
5947 | end if; | |
5948 | ||
5949 | LSN := new String'(Name_Buffer (1 .. Name_Len)); | |
5950 | end; | |
5951 | end if; | |
5952 | end Check_Subprogram_Order; | |
5953 | ||
5954 | ------------------------------ | |
5955 | -- Check_Subtype_Conformant -- | |
5956 | ------------------------------ | |
5957 | ||
5958 | procedure Check_Subtype_Conformant | |
ce2b6ba5 JM |
5959 | (New_Id : Entity_Id; |
5960 | Old_Id : Entity_Id; | |
5961 | Err_Loc : Node_Id := Empty; | |
5962 | Skip_Controlling_Formals : Boolean := False) | |
996ae0b0 RK |
5963 | is |
5964 | Result : Boolean; | |
81db9d77 | 5965 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5966 | begin |
5967 | Check_Conformance | |
ce2b6ba5 JM |
5968 | (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc, |
5969 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
5970 | end Check_Subtype_Conformant; |
5971 | ||
5972 | --------------------------- | |
5973 | -- Check_Type_Conformant -- | |
5974 | --------------------------- | |
5975 | ||
5976 | procedure Check_Type_Conformant | |
5977 | (New_Id : Entity_Id; | |
5978 | Old_Id : Entity_Id; | |
5979 | Err_Loc : Node_Id := Empty) | |
5980 | is | |
5981 | Result : Boolean; | |
81db9d77 | 5982 | pragma Warnings (Off, Result); |
996ae0b0 RK |
5983 | begin |
5984 | Check_Conformance | |
5985 | (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc); | |
5986 | end Check_Type_Conformant; | |
5987 | ||
806f6d37 AC |
5988 | --------------------------- |
5989 | -- Can_Override_Operator -- | |
5990 | --------------------------- | |
5991 | ||
5992 | function Can_Override_Operator (Subp : Entity_Id) return Boolean is | |
5993 | Typ : Entity_Id; | |
5994 | begin | |
5995 | if Nkind (Subp) /= N_Defining_Operator_Symbol then | |
5996 | return False; | |
5997 | ||
5998 | else | |
5999 | Typ := Base_Type (Etype (First_Formal (Subp))); | |
6000 | ||
6001 | return Operator_Matches_Spec (Subp, Subp) | |
6002 | and then Scope (Subp) = Scope (Typ) | |
6003 | and then not Is_Class_Wide_Type (Typ); | |
6004 | end if; | |
6005 | end Can_Override_Operator; | |
6006 | ||
996ae0b0 RK |
6007 | ---------------------- |
6008 | -- Conforming_Types -- | |
6009 | ---------------------- | |
6010 | ||
6011 | function Conforming_Types | |
6012 | (T1 : Entity_Id; | |
6013 | T2 : Entity_Id; | |
6014 | Ctype : Conformance_Type; | |
d05ef0ab | 6015 | Get_Inst : Boolean := False) return Boolean |
996ae0b0 RK |
6016 | is |
6017 | Type_1 : Entity_Id := T1; | |
6018 | Type_2 : Entity_Id := T2; | |
af4b9434 | 6019 | Are_Anonymous_Access_To_Subprogram_Types : Boolean := False; |
996ae0b0 RK |
6020 | |
6021 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean; | |
0a36105d JM |
6022 | -- If neither T1 nor T2 are generic actual types, or if they are in |
6023 | -- different scopes (e.g. parent and child instances), then verify that | |
6024 | -- the base types are equal. Otherwise T1 and T2 must be on the same | |
6025 | -- subtype chain. The whole purpose of this procedure is to prevent | |
6026 | -- spurious ambiguities in an instantiation that may arise if two | |
6027 | -- distinct generic types are instantiated with the same actual. | |
6028 | ||
5d37ba92 ES |
6029 | function Find_Designated_Type (T : Entity_Id) return Entity_Id; |
6030 | -- An access parameter can designate an incomplete type. If the | |
6031 | -- incomplete type is the limited view of a type from a limited_ | |
6032 | -- with_clause, check whether the non-limited view is available. If | |
6033 | -- it is a (non-limited) incomplete type, get the full view. | |
6034 | ||
0a36105d JM |
6035 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean; |
6036 | -- Returns True if and only if either T1 denotes a limited view of T2 | |
6037 | -- or T2 denotes a limited view of T1. This can arise when the limited | |
6038 | -- with view of a type is used in a subprogram declaration and the | |
6039 | -- subprogram body is in the scope of a regular with clause for the | |
6040 | -- same unit. In such a case, the two type entities can be considered | |
6041 | -- identical for purposes of conformance checking. | |
996ae0b0 RK |
6042 | |
6043 | ---------------------- | |
6044 | -- Base_Types_Match -- | |
6045 | ---------------------- | |
6046 | ||
6047 | function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is | |
6048 | begin | |
6049 | if T1 = T2 then | |
6050 | return True; | |
6051 | ||
6052 | elsif Base_Type (T1) = Base_Type (T2) then | |
6053 | ||
0a36105d | 6054 | -- The following is too permissive. A more precise test should |
996ae0b0 RK |
6055 | -- check that the generic actual is an ancestor subtype of the |
6056 | -- other ???. | |
6057 | ||
6058 | return not Is_Generic_Actual_Type (T1) | |
07fc65c4 GB |
6059 | or else not Is_Generic_Actual_Type (T2) |
6060 | or else Scope (T1) /= Scope (T2); | |
996ae0b0 | 6061 | |
0a36105d JM |
6062 | else |
6063 | return False; | |
6064 | end if; | |
6065 | end Base_Types_Match; | |
aa720a54 | 6066 | |
5d37ba92 ES |
6067 | -------------------------- |
6068 | -- Find_Designated_Type -- | |
6069 | -------------------------- | |
6070 | ||
6071 | function Find_Designated_Type (T : Entity_Id) return Entity_Id is | |
6072 | Desig : Entity_Id; | |
6073 | ||
6074 | begin | |
6075 | Desig := Directly_Designated_Type (T); | |
6076 | ||
6077 | if Ekind (Desig) = E_Incomplete_Type then | |
6078 | ||
6079 | -- If regular incomplete type, get full view if available | |
6080 | ||
6081 | if Present (Full_View (Desig)) then | |
6082 | Desig := Full_View (Desig); | |
6083 | ||
6084 | -- If limited view of a type, get non-limited view if available, | |
6085 | -- and check again for a regular incomplete type. | |
6086 | ||
6087 | elsif Present (Non_Limited_View (Desig)) then | |
6088 | Desig := Get_Full_View (Non_Limited_View (Desig)); | |
6089 | end if; | |
6090 | end if; | |
6091 | ||
6092 | return Desig; | |
6093 | end Find_Designated_Type; | |
6094 | ||
0a36105d JM |
6095 | ------------------------------- |
6096 | -- Matches_Limited_With_View -- | |
6097 | ------------------------------- | |
6098 | ||
6099 | function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is | |
6100 | begin | |
6101 | -- In some cases a type imported through a limited_with clause, and | |
6102 | -- its nonlimited view are both visible, for example in an anonymous | |
6103 | -- access-to-class-wide type in a formal. Both entities designate the | |
6104 | -- same type. | |
6105 | ||
6106 | if From_With_Type (T1) | |
6107 | and then T2 = Available_View (T1) | |
aa720a54 AC |
6108 | then |
6109 | return True; | |
6110 | ||
41251c60 | 6111 | elsif From_With_Type (T2) |
0a36105d | 6112 | and then T1 = Available_View (T2) |
41251c60 JM |
6113 | then |
6114 | return True; | |
3e24afaa AC |
6115 | |
6116 | elsif From_With_Type (T1) | |
6117 | and then From_With_Type (T2) | |
6118 | and then Available_View (T1) = Available_View (T2) | |
6119 | then | |
6120 | return True; | |
41251c60 | 6121 | |
996ae0b0 RK |
6122 | else |
6123 | return False; | |
6124 | end if; | |
0a36105d | 6125 | end Matches_Limited_With_View; |
996ae0b0 | 6126 | |
ec4867fa | 6127 | -- Start of processing for Conforming_Types |
758c442c | 6128 | |
996ae0b0 RK |
6129 | begin |
6130 | -- The context is an instance association for a formal | |
82c80734 RD |
6131 | -- access-to-subprogram type; the formal parameter types require |
6132 | -- mapping because they may denote other formal parameters of the | |
6133 | -- generic unit. | |
996ae0b0 RK |
6134 | |
6135 | if Get_Inst then | |
6136 | Type_1 := Get_Instance_Of (T1); | |
6137 | Type_2 := Get_Instance_Of (T2); | |
6138 | end if; | |
6139 | ||
0a36105d JM |
6140 | -- If one of the types is a view of the other introduced by a limited |
6141 | -- with clause, treat these as conforming for all purposes. | |
996ae0b0 | 6142 | |
0a36105d JM |
6143 | if Matches_Limited_With_View (T1, T2) then |
6144 | return True; | |
6145 | ||
6146 | elsif Base_Types_Match (Type_1, Type_2) then | |
996ae0b0 RK |
6147 | return Ctype <= Mode_Conformant |
6148 | or else Subtypes_Statically_Match (Type_1, Type_2); | |
6149 | ||
6150 | elsif Is_Incomplete_Or_Private_Type (Type_1) | |
6151 | and then Present (Full_View (Type_1)) | |
6152 | and then Base_Types_Match (Full_View (Type_1), Type_2) | |
6153 | then | |
6154 | return Ctype <= Mode_Conformant | |
6155 | or else Subtypes_Statically_Match (Full_View (Type_1), Type_2); | |
6156 | ||
6157 | elsif Ekind (Type_2) = E_Incomplete_Type | |
6158 | and then Present (Full_View (Type_2)) | |
6159 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6160 | then | |
6161 | return Ctype <= Mode_Conformant | |
6162 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
fbf5a39b AC |
6163 | |
6164 | elsif Is_Private_Type (Type_2) | |
6165 | and then In_Instance | |
6166 | and then Present (Full_View (Type_2)) | |
6167 | and then Base_Types_Match (Type_1, Full_View (Type_2)) | |
6168 | then | |
6169 | return Ctype <= Mode_Conformant | |
6170 | or else Subtypes_Statically_Match (Type_1, Full_View (Type_2)); | |
996ae0b0 RK |
6171 | end if; |
6172 | ||
0a36105d | 6173 | -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be |
758c442c | 6174 | -- treated recursively because they carry a signature. |
af4b9434 AC |
6175 | |
6176 | Are_Anonymous_Access_To_Subprogram_Types := | |
f937473f RD |
6177 | Ekind (Type_1) = Ekind (Type_2) |
6178 | and then | |
800621e0 | 6179 | (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type |
f937473f RD |
6180 | or else |
6181 | Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type); | |
af4b9434 | 6182 | |
996ae0b0 | 6183 | -- Test anonymous access type case. For this case, static subtype |
5d37ba92 ES |
6184 | -- matching is required for mode conformance (RM 6.3.1(15)). We check |
6185 | -- the base types because we may have built internal subtype entities | |
6186 | -- to handle null-excluding types (see Process_Formals). | |
996ae0b0 | 6187 | |
5d37ba92 ES |
6188 | if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type |
6189 | and then | |
6190 | Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type) | |
0ab80019 | 6191 | or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254) |
996ae0b0 RK |
6192 | then |
6193 | declare | |
6194 | Desig_1 : Entity_Id; | |
6195 | Desig_2 : Entity_Id; | |
6196 | ||
6197 | begin | |
885c4871 | 6198 | -- In Ada 2005, access constant indicators must match for |
5d37ba92 | 6199 | -- subtype conformance. |
9dcb52e1 | 6200 | |
0791fbe9 | 6201 | if Ada_Version >= Ada_2005 |
5d37ba92 ES |
6202 | and then Ctype >= Subtype_Conformant |
6203 | and then | |
6204 | Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2) | |
6205 | then | |
6206 | return False; | |
996ae0b0 RK |
6207 | end if; |
6208 | ||
5d37ba92 | 6209 | Desig_1 := Find_Designated_Type (Type_1); |
5d37ba92 | 6210 | Desig_2 := Find_Designated_Type (Type_2); |
996ae0b0 | 6211 | |
5d37ba92 | 6212 | -- If the context is an instance association for a formal |
82c80734 RD |
6213 | -- access-to-subprogram type; formal access parameter designated |
6214 | -- types require mapping because they may denote other formal | |
6215 | -- parameters of the generic unit. | |
996ae0b0 RK |
6216 | |
6217 | if Get_Inst then | |
6218 | Desig_1 := Get_Instance_Of (Desig_1); | |
6219 | Desig_2 := Get_Instance_Of (Desig_2); | |
6220 | end if; | |
6221 | ||
82c80734 RD |
6222 | -- It is possible for a Class_Wide_Type to be introduced for an |
6223 | -- incomplete type, in which case there is a separate class_ wide | |
6224 | -- type for the full view. The types conform if their Etypes | |
6225 | -- conform, i.e. one may be the full view of the other. This can | |
6226 | -- only happen in the context of an access parameter, other uses | |
6227 | -- of an incomplete Class_Wide_Type are illegal. | |
996ae0b0 | 6228 | |
fbf5a39b | 6229 | if Is_Class_Wide_Type (Desig_1) |
4adf3c50 AC |
6230 | and then |
6231 | Is_Class_Wide_Type (Desig_2) | |
996ae0b0 RK |
6232 | then |
6233 | return | |
fbf5a39b AC |
6234 | Conforming_Types |
6235 | (Etype (Base_Type (Desig_1)), | |
6236 | Etype (Base_Type (Desig_2)), Ctype); | |
af4b9434 AC |
6237 | |
6238 | elsif Are_Anonymous_Access_To_Subprogram_Types then | |
0791fbe9 | 6239 | if Ada_Version < Ada_2005 then |
758c442c GD |
6240 | return Ctype = Type_Conformant |
6241 | or else | |
af4b9434 AC |
6242 | Subtypes_Statically_Match (Desig_1, Desig_2); |
6243 | ||
758c442c GD |
6244 | -- We must check the conformance of the signatures themselves |
6245 | ||
6246 | else | |
6247 | declare | |
6248 | Conformant : Boolean; | |
6249 | begin | |
6250 | Check_Conformance | |
6251 | (Desig_1, Desig_2, Ctype, False, Conformant); | |
6252 | return Conformant; | |
6253 | end; | |
6254 | end if; | |
6255 | ||
996ae0b0 RK |
6256 | else |
6257 | return Base_Type (Desig_1) = Base_Type (Desig_2) | |
6258 | and then (Ctype = Type_Conformant | |
af4b9434 AC |
6259 | or else |
6260 | Subtypes_Statically_Match (Desig_1, Desig_2)); | |
996ae0b0 RK |
6261 | end if; |
6262 | end; | |
6263 | ||
6264 | -- Otherwise definitely no match | |
6265 | ||
6266 | else | |
c8ef728f ES |
6267 | if ((Ekind (Type_1) = E_Anonymous_Access_Type |
6268 | and then Is_Access_Type (Type_2)) | |
6269 | or else (Ekind (Type_2) = E_Anonymous_Access_Type | |
6270 | and then Is_Access_Type (Type_1))) | |
6271 | and then | |
6272 | Conforming_Types | |
6273 | (Designated_Type (Type_1), Designated_Type (Type_2), Ctype) | |
6274 | then | |
6275 | May_Hide_Profile := True; | |
6276 | end if; | |
6277 | ||
996ae0b0 RK |
6278 | return False; |
6279 | end if; | |
996ae0b0 RK |
6280 | end Conforming_Types; |
6281 | ||
6282 | -------------------------- | |
6283 | -- Create_Extra_Formals -- | |
6284 | -------------------------- | |
6285 | ||
6286 | procedure Create_Extra_Formals (E : Entity_Id) is | |
6287 | Formal : Entity_Id; | |
ec4867fa | 6288 | First_Extra : Entity_Id := Empty; |
996ae0b0 RK |
6289 | Last_Extra : Entity_Id; |
6290 | Formal_Type : Entity_Id; | |
6291 | P_Formal : Entity_Id := Empty; | |
6292 | ||
ec4867fa ES |
6293 | function Add_Extra_Formal |
6294 | (Assoc_Entity : Entity_Id; | |
6295 | Typ : Entity_Id; | |
6296 | Scope : Entity_Id; | |
6297 | Suffix : String) return Entity_Id; | |
6298 | -- Add an extra formal to the current list of formals and extra formals. | |
6299 | -- The extra formal is added to the end of the list of extra formals, | |
6300 | -- and also returned as the result. These formals are always of mode IN. | |
6301 | -- The new formal has the type Typ, is declared in Scope, and its name | |
6302 | -- is given by a concatenation of the name of Assoc_Entity and Suffix. | |
cd5a9750 AC |
6303 | -- The following suffixes are currently used. They should not be changed |
6304 | -- without coordinating with CodePeer, which makes use of these to | |
6305 | -- provide better messages. | |
6306 | ||
d92eccc3 AC |
6307 | -- O denotes the Constrained bit. |
6308 | -- L denotes the accessibility level. | |
cd5a9750 AC |
6309 | -- BIP_xxx denotes an extra formal for a build-in-place function. See |
6310 | -- the full list in exp_ch6.BIP_Formal_Kind. | |
996ae0b0 | 6311 | |
fbf5a39b AC |
6312 | ---------------------- |
6313 | -- Add_Extra_Formal -- | |
6314 | ---------------------- | |
6315 | ||
ec4867fa ES |
6316 | function Add_Extra_Formal |
6317 | (Assoc_Entity : Entity_Id; | |
6318 | Typ : Entity_Id; | |
6319 | Scope : Entity_Id; | |
6320 | Suffix : String) return Entity_Id | |
6321 | is | |
996ae0b0 | 6322 | EF : constant Entity_Id := |
ec4867fa ES |
6323 | Make_Defining_Identifier (Sloc (Assoc_Entity), |
6324 | Chars => New_External_Name (Chars (Assoc_Entity), | |
f937473f | 6325 | Suffix => Suffix)); |
996ae0b0 RK |
6326 | |
6327 | begin | |
82c80734 RD |
6328 | -- A little optimization. Never generate an extra formal for the |
6329 | -- _init operand of an initialization procedure, since it could | |
6330 | -- never be used. | |
996ae0b0 RK |
6331 | |
6332 | if Chars (Formal) = Name_uInit then | |
6333 | return Empty; | |
6334 | end if; | |
6335 | ||
6336 | Set_Ekind (EF, E_In_Parameter); | |
6337 | Set_Actual_Subtype (EF, Typ); | |
6338 | Set_Etype (EF, Typ); | |
ec4867fa | 6339 | Set_Scope (EF, Scope); |
996ae0b0 RK |
6340 | Set_Mechanism (EF, Default_Mechanism); |
6341 | Set_Formal_Validity (EF); | |
6342 | ||
ec4867fa ES |
6343 | if No (First_Extra) then |
6344 | First_Extra := EF; | |
6345 | Set_Extra_Formals (Scope, First_Extra); | |
6346 | end if; | |
6347 | ||
6348 | if Present (Last_Extra) then | |
6349 | Set_Extra_Formal (Last_Extra, EF); | |
6350 | end if; | |
6351 | ||
996ae0b0 | 6352 | Last_Extra := EF; |
ec4867fa | 6353 | |
996ae0b0 RK |
6354 | return EF; |
6355 | end Add_Extra_Formal; | |
6356 | ||
6357 | -- Start of processing for Create_Extra_Formals | |
6358 | ||
6359 | begin | |
f937473f RD |
6360 | -- We never generate extra formals if expansion is not active |
6361 | -- because we don't need them unless we are generating code. | |
6362 | ||
6363 | if not Expander_Active then | |
6364 | return; | |
6365 | end if; | |
6366 | ||
82c80734 | 6367 | -- If this is a derived subprogram then the subtypes of the parent |
16b05213 | 6368 | -- subprogram's formal parameters will be used to determine the need |
82c80734 | 6369 | -- for extra formals. |
996ae0b0 RK |
6370 | |
6371 | if Is_Overloadable (E) and then Present (Alias (E)) then | |
6372 | P_Formal := First_Formal (Alias (E)); | |
6373 | end if; | |
6374 | ||
6375 | Last_Extra := Empty; | |
6376 | Formal := First_Formal (E); | |
6377 | while Present (Formal) loop | |
6378 | Last_Extra := Formal; | |
6379 | Next_Formal (Formal); | |
6380 | end loop; | |
6381 | ||
f937473f | 6382 | -- If Extra_formals were already created, don't do it again. This |
82c80734 RD |
6383 | -- situation may arise for subprogram types created as part of |
6384 | -- dispatching calls (see Expand_Dispatching_Call) | |
996ae0b0 RK |
6385 | |
6386 | if Present (Last_Extra) and then | |
6387 | Present (Extra_Formal (Last_Extra)) | |
6388 | then | |
6389 | return; | |
6390 | end if; | |
6391 | ||
19590d70 GD |
6392 | -- If the subprogram is a predefined dispatching subprogram then don't |
6393 | -- generate any extra constrained or accessibility level formals. In | |
6394 | -- general we suppress these for internal subprograms (by not calling | |
6395 | -- Freeze_Subprogram and Create_Extra_Formals at all), but internally | |
6396 | -- generated stream attributes do get passed through because extra | |
6397 | -- build-in-place formals are needed in some cases (limited 'Input). | |
6398 | ||
bac7206d | 6399 | if Is_Predefined_Internal_Operation (E) then |
63585f75 | 6400 | goto Test_For_Func_Result_Extras; |
19590d70 GD |
6401 | end if; |
6402 | ||
996ae0b0 | 6403 | Formal := First_Formal (E); |
996ae0b0 RK |
6404 | while Present (Formal) loop |
6405 | ||
6406 | -- Create extra formal for supporting the attribute 'Constrained. | |
6407 | -- The case of a private type view without discriminants also | |
6408 | -- requires the extra formal if the underlying type has defaulted | |
6409 | -- discriminants. | |
6410 | ||
6411 | if Ekind (Formal) /= E_In_Parameter then | |
6412 | if Present (P_Formal) then | |
6413 | Formal_Type := Etype (P_Formal); | |
6414 | else | |
6415 | Formal_Type := Etype (Formal); | |
6416 | end if; | |
6417 | ||
5d09245e AC |
6418 | -- Do not produce extra formals for Unchecked_Union parameters. |
6419 | -- Jump directly to the end of the loop. | |
6420 | ||
6421 | if Is_Unchecked_Union (Base_Type (Formal_Type)) then | |
6422 | goto Skip_Extra_Formal_Generation; | |
6423 | end if; | |
6424 | ||
996ae0b0 RK |
6425 | if not Has_Discriminants (Formal_Type) |
6426 | and then Ekind (Formal_Type) in Private_Kind | |
6427 | and then Present (Underlying_Type (Formal_Type)) | |
6428 | then | |
6429 | Formal_Type := Underlying_Type (Formal_Type); | |
6430 | end if; | |
6431 | ||
5e5db3b4 GD |
6432 | -- Suppress the extra formal if formal's subtype is constrained or |
6433 | -- indefinite, or we're compiling for Ada 2012 and the underlying | |
6434 | -- type is tagged and limited. In Ada 2012, a limited tagged type | |
6435 | -- can have defaulted discriminants, but 'Constrained is required | |
6436 | -- to return True, so the formal is never needed (see AI05-0214). | |
6437 | -- Note that this ensures consistency of calling sequences for | |
6438 | -- dispatching operations when some types in a class have defaults | |
6439 | -- on discriminants and others do not (and requiring the extra | |
6440 | -- formal would introduce distributed overhead). | |
6441 | ||
996ae0b0 | 6442 | if Has_Discriminants (Formal_Type) |
f937473f RD |
6443 | and then not Is_Constrained (Formal_Type) |
6444 | and then not Is_Indefinite_Subtype (Formal_Type) | |
5e5db3b4 GD |
6445 | and then (Ada_Version < Ada_2012 |
6446 | or else | |
6447 | not (Is_Tagged_Type (Underlying_Type (Formal_Type)) | |
6448 | and then Is_Limited_Type (Formal_Type))) | |
996ae0b0 RK |
6449 | then |
6450 | Set_Extra_Constrained | |
d92eccc3 | 6451 | (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O")); |
996ae0b0 RK |
6452 | end if; |
6453 | end if; | |
6454 | ||
0a36105d JM |
6455 | -- Create extra formal for supporting accessibility checking. This |
6456 | -- is done for both anonymous access formals and formals of named | |
6457 | -- access types that are marked as controlling formals. The latter | |
6458 | -- case can occur when Expand_Dispatching_Call creates a subprogram | |
6459 | -- type and substitutes the types of access-to-class-wide actuals | |
6460 | -- for the anonymous access-to-specific-type of controlling formals. | |
5d37ba92 ES |
6461 | -- Base_Type is applied because in cases where there is a null |
6462 | -- exclusion the formal may have an access subtype. | |
996ae0b0 RK |
6463 | |
6464 | -- This is suppressed if we specifically suppress accessibility | |
f937473f | 6465 | -- checks at the package level for either the subprogram, or the |
fbf5a39b AC |
6466 | -- package in which it resides. However, we do not suppress it |
6467 | -- simply if the scope has accessibility checks suppressed, since | |
6468 | -- this could cause trouble when clients are compiled with a | |
6469 | -- different suppression setting. The explicit checks at the | |
6470 | -- package level are safe from this point of view. | |
996ae0b0 | 6471 | |
5d37ba92 | 6472 | if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type |
0a36105d | 6473 | or else (Is_Controlling_Formal (Formal) |
5d37ba92 | 6474 | and then Is_Access_Type (Base_Type (Etype (Formal))))) |
996ae0b0 | 6475 | and then not |
fbf5a39b | 6476 | (Explicit_Suppress (E, Accessibility_Check) |
996ae0b0 | 6477 | or else |
fbf5a39b | 6478 | Explicit_Suppress (Scope (E), Accessibility_Check)) |
996ae0b0 | 6479 | and then |
c8ef728f | 6480 | (No (P_Formal) |
996ae0b0 RK |
6481 | or else Present (Extra_Accessibility (P_Formal))) |
6482 | then | |
811c6a85 | 6483 | Set_Extra_Accessibility |
d92eccc3 | 6484 | (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L")); |
996ae0b0 RK |
6485 | end if; |
6486 | ||
5d09245e AC |
6487 | -- This label is required when skipping extra formal generation for |
6488 | -- Unchecked_Union parameters. | |
6489 | ||
6490 | <<Skip_Extra_Formal_Generation>> | |
6491 | ||
f937473f RD |
6492 | if Present (P_Formal) then |
6493 | Next_Formal (P_Formal); | |
6494 | end if; | |
6495 | ||
996ae0b0 RK |
6496 | Next_Formal (Formal); |
6497 | end loop; | |
ec4867fa | 6498 | |
63585f75 SB |
6499 | <<Test_For_Func_Result_Extras>> |
6500 | ||
6501 | -- Ada 2012 (AI05-234): "the accessibility level of the result of a | |
6502 | -- function call is ... determined by the point of call ...". | |
6503 | ||
6504 | if Needs_Result_Accessibility_Level (E) then | |
6505 | Set_Extra_Accessibility_Of_Result | |
6506 | (E, Add_Extra_Formal (E, Standard_Natural, E, "L")); | |
6507 | end if; | |
19590d70 | 6508 | |
ec4867fa | 6509 | -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add |
f937473f RD |
6510 | -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind. |
6511 | ||
0791fbe9 | 6512 | if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then |
ec4867fa | 6513 | declare |
f937473f | 6514 | Result_Subt : constant Entity_Id := Etype (E); |
1a36a0cd | 6515 | Full_Subt : constant Entity_Id := Available_View (Result_Subt); |
2fcc44fa | 6516 | Formal_Typ : Entity_Id; |
f937473f | 6517 | |
2fcc44fa | 6518 | Discard : Entity_Id; |
f937473f | 6519 | pragma Warnings (Off, Discard); |
ec4867fa ES |
6520 | |
6521 | begin | |
f937473f | 6522 | -- In the case of functions with unconstrained result subtypes, |
9a1bc6d5 AC |
6523 | -- add a 4-state formal indicating whether the return object is |
6524 | -- allocated by the caller (1), or should be allocated by the | |
6525 | -- callee on the secondary stack (2), in the global heap (3), or | |
6526 | -- in a user-defined storage pool (4). For the moment we just use | |
6527 | -- Natural for the type of this formal. Note that this formal | |
6528 | -- isn't usually needed in the case where the result subtype is | |
6529 | -- constrained, but it is needed when the function has a tagged | |
6530 | -- result, because generally such functions can be called in a | |
6531 | -- dispatching context and such calls must be handled like calls | |
6532 | -- to a class-wide function. | |
0a36105d | 6533 | |
1bb6e262 | 6534 | if Needs_BIP_Alloc_Form (E) then |
f937473f RD |
6535 | Discard := |
6536 | Add_Extra_Formal | |
6537 | (E, Standard_Natural, | |
6538 | E, BIP_Formal_Suffix (BIP_Alloc_Form)); | |
200b7162 | 6539 | |
8417f4b2 | 6540 | -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to |
3e452820 AC |
6541 | -- use a user-defined pool. This formal is not added on |
6542 | -- .NET/JVM/ZFP as those targets do not support pools. | |
200b7162 | 6543 | |
ea10ca9c AC |
6544 | if VM_Target = No_VM |
6545 | and then RTE_Available (RE_Root_Storage_Pool_Ptr) | |
3e452820 | 6546 | then |
8417f4b2 AC |
6547 | Discard := |
6548 | Add_Extra_Formal | |
6549 | (E, RTE (RE_Root_Storage_Pool_Ptr), | |
6550 | E, BIP_Formal_Suffix (BIP_Storage_Pool)); | |
6551 | end if; | |
f937473f | 6552 | end if; |
ec4867fa | 6553 | |
df3e68b1 | 6554 | -- In the case of functions whose result type needs finalization, |
ca5af305 | 6555 | -- add an extra formal which represents the finalization master. |
df3e68b1 | 6556 | |
ca5af305 | 6557 | if Needs_BIP_Finalization_Master (E) then |
f937473f RD |
6558 | Discard := |
6559 | Add_Extra_Formal | |
ca5af305 AC |
6560 | (E, RTE (RE_Finalization_Master_Ptr), |
6561 | E, BIP_Formal_Suffix (BIP_Finalization_Master)); | |
f937473f RD |
6562 | end if; |
6563 | ||
94bbf008 AC |
6564 | -- When the result type contains tasks, add two extra formals: the |
6565 | -- master of the tasks to be created, and the caller's activation | |
6566 | -- chain. | |
f937473f | 6567 | |
1a36a0cd | 6568 | if Has_Task (Full_Subt) then |
f937473f RD |
6569 | Discard := |
6570 | Add_Extra_Formal | |
6571 | (E, RTE (RE_Master_Id), | |
af89615f | 6572 | E, BIP_Formal_Suffix (BIP_Task_Master)); |
f937473f RD |
6573 | Discard := |
6574 | Add_Extra_Formal | |
6575 | (E, RTE (RE_Activation_Chain_Access), | |
6576 | E, BIP_Formal_Suffix (BIP_Activation_Chain)); | |
6577 | end if; | |
ec4867fa | 6578 | |
f937473f RD |
6579 | -- All build-in-place functions get an extra formal that will be |
6580 | -- passed the address of the return object within the caller. | |
ec4867fa | 6581 | |
1a36a0cd AC |
6582 | Formal_Typ := |
6583 | Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E)); | |
ec4867fa | 6584 | |
1a36a0cd AC |
6585 | Set_Directly_Designated_Type (Formal_Typ, Result_Subt); |
6586 | Set_Etype (Formal_Typ, Formal_Typ); | |
6587 | Set_Depends_On_Private | |
6588 | (Formal_Typ, Has_Private_Component (Formal_Typ)); | |
6589 | Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ))); | |
6590 | Set_Is_Access_Constant (Formal_Typ, False); | |
ec4867fa | 6591 | |
1a36a0cd AC |
6592 | -- Ada 2005 (AI-50217): Propagate the attribute that indicates |
6593 | -- the designated type comes from the limited view (for back-end | |
6594 | -- purposes). | |
ec4867fa | 6595 | |
1a36a0cd | 6596 | Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt)); |
f937473f | 6597 | |
1a36a0cd AC |
6598 | Layout_Type (Formal_Typ); |
6599 | ||
6600 | Discard := | |
6601 | Add_Extra_Formal | |
6602 | (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access)); | |
ec4867fa ES |
6603 | end; |
6604 | end if; | |
996ae0b0 RK |
6605 | end Create_Extra_Formals; |
6606 | ||
6607 | ----------------------------- | |
6608 | -- Enter_Overloaded_Entity -- | |
6609 | ----------------------------- | |
6610 | ||
6611 | procedure Enter_Overloaded_Entity (S : Entity_Id) is | |
6612 | E : Entity_Id := Current_Entity_In_Scope (S); | |
6613 | C_E : Entity_Id := Current_Entity (S); | |
6614 | ||
6615 | begin | |
6616 | if Present (E) then | |
6617 | Set_Has_Homonym (E); | |
6618 | Set_Has_Homonym (S); | |
6619 | end if; | |
6620 | ||
6621 | Set_Is_Immediately_Visible (S); | |
6622 | Set_Scope (S, Current_Scope); | |
6623 | ||
6624 | -- Chain new entity if front of homonym in current scope, so that | |
6625 | -- homonyms are contiguous. | |
6626 | ||
6627 | if Present (E) | |
6628 | and then E /= C_E | |
6629 | then | |
6630 | while Homonym (C_E) /= E loop | |
6631 | C_E := Homonym (C_E); | |
6632 | end loop; | |
6633 | ||
6634 | Set_Homonym (C_E, S); | |
6635 | ||
6636 | else | |
6637 | E := C_E; | |
6638 | Set_Current_Entity (S); | |
6639 | end if; | |
6640 | ||
6641 | Set_Homonym (S, E); | |
6642 | ||
6643 | Append_Entity (S, Current_Scope); | |
6644 | Set_Public_Status (S); | |
6645 | ||
6646 | if Debug_Flag_E then | |
6647 | Write_Str ("New overloaded entity chain: "); | |
6648 | Write_Name (Chars (S)); | |
996ae0b0 | 6649 | |
82c80734 | 6650 | E := S; |
996ae0b0 RK |
6651 | while Present (E) loop |
6652 | Write_Str (" "); Write_Int (Int (E)); | |
6653 | E := Homonym (E); | |
6654 | end loop; | |
6655 | ||
6656 | Write_Eol; | |
6657 | end if; | |
6658 | ||
6659 | -- Generate warning for hiding | |
6660 | ||
6661 | if Warn_On_Hiding | |
6662 | and then Comes_From_Source (S) | |
6663 | and then In_Extended_Main_Source_Unit (S) | |
6664 | then | |
6665 | E := S; | |
6666 | loop | |
6667 | E := Homonym (E); | |
6668 | exit when No (E); | |
6669 | ||
7fc53871 AC |
6670 | -- Warn unless genuine overloading. Do not emit warning on |
6671 | -- hiding predefined operators in Standard (these are either an | |
6672 | -- (artifact of our implicit declarations, or simple noise) but | |
6673 | -- keep warning on a operator defined on a local subtype, because | |
6674 | -- of the real danger that different operators may be applied in | |
6675 | -- various parts of the program. | |
996ae0b0 | 6676 | |
1f250383 AC |
6677 | -- Note that if E and S have the same scope, there is never any |
6678 | -- hiding. Either the two conflict, and the program is illegal, | |
6679 | -- or S is overriding an implicit inherited subprogram. | |
6680 | ||
6681 | if Scope (E) /= Scope (S) | |
6682 | and then (not Is_Overloadable (E) | |
8d606a78 | 6683 | or else Subtype_Conformant (E, S)) |
f937473f RD |
6684 | and then (Is_Immediately_Visible (E) |
6685 | or else | |
6686 | Is_Potentially_Use_Visible (S)) | |
996ae0b0 | 6687 | then |
7fc53871 AC |
6688 | if Scope (E) /= Standard_Standard then |
6689 | Error_Msg_Sloc := Sloc (E); | |
6690 | Error_Msg_N ("declaration of & hides one#?", S); | |
6691 | ||
6692 | elsif Nkind (S) = N_Defining_Operator_Symbol | |
6693 | and then | |
1f250383 | 6694 | Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S) |
7fc53871 AC |
6695 | then |
6696 | Error_Msg_N | |
6697 | ("declaration of & hides predefined operator?", S); | |
6698 | end if; | |
996ae0b0 RK |
6699 | end if; |
6700 | end loop; | |
6701 | end if; | |
6702 | end Enter_Overloaded_Entity; | |
6703 | ||
e5a58fac AC |
6704 | ----------------------------- |
6705 | -- Check_Untagged_Equality -- | |
6706 | ----------------------------- | |
6707 | ||
6708 | procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is | |
6709 | Typ : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
6710 | Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op); | |
6711 | Obj_Decl : Node_Id; | |
6712 | ||
6713 | begin | |
6714 | if Nkind (Decl) = N_Subprogram_Declaration | |
6715 | and then Is_Record_Type (Typ) | |
6716 | and then not Is_Tagged_Type (Typ) | |
6717 | then | |
21a5b575 AC |
6718 | -- If the type is not declared in a package, or if we are in the |
6719 | -- body of the package or in some other scope, the new operation is | |
6720 | -- not primitive, and therefore legal, though suspicious. If the | |
6721 | -- type is a generic actual (sub)type, the operation is not primitive | |
6722 | -- either because the base type is declared elsewhere. | |
6723 | ||
e5a58fac | 6724 | if Is_Frozen (Typ) then |
21a5b575 AC |
6725 | if Ekind (Scope (Typ)) /= E_Package |
6726 | or else Scope (Typ) /= Current_Scope | |
6727 | then | |
6728 | null; | |
e5a58fac | 6729 | |
21a5b575 AC |
6730 | elsif Is_Generic_Actual_Type (Typ) then |
6731 | null; | |
e5a58fac | 6732 | |
21a5b575 | 6733 | elsif In_Package_Body (Scope (Typ)) then |
ae6ede77 AC |
6734 | Error_Msg_NE |
6735 | ("equality operator must be declared " | |
6736 | & "before type& is frozen", Eq_Op, Typ); | |
6737 | Error_Msg_N | |
6738 | ("\move declaration to package spec", Eq_Op); | |
21a5b575 AC |
6739 | |
6740 | else | |
6741 | Error_Msg_NE | |
6742 | ("equality operator must be declared " | |
6743 | & "before type& is frozen", Eq_Op, Typ); | |
6744 | ||
6745 | Obj_Decl := Next (Parent (Typ)); | |
6746 | while Present (Obj_Decl) | |
6747 | and then Obj_Decl /= Decl | |
6748 | loop | |
6749 | if Nkind (Obj_Decl) = N_Object_Declaration | |
6750 | and then Etype (Defining_Identifier (Obj_Decl)) = Typ | |
6751 | then | |
6752 | Error_Msg_NE ("type& is frozen by declaration?", | |
6753 | Obj_Decl, Typ); | |
6754 | Error_Msg_N | |
6755 | ("\an equality operator cannot be declared after this " | |
a4640a39 | 6756 | & "point (RM 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl); |
21a5b575 AC |
6757 | exit; |
6758 | end if; | |
6759 | ||
6760 | Next (Obj_Decl); | |
6761 | end loop; | |
6762 | end if; | |
e5a58fac AC |
6763 | |
6764 | elsif not In_Same_List (Parent (Typ), Decl) | |
6765 | and then not Is_Limited_Type (Typ) | |
6766 | then | |
21a5b575 AC |
6767 | |
6768 | -- This makes it illegal to have a primitive equality declared in | |
6769 | -- the private part if the type is visible. | |
6770 | ||
e5a58fac AC |
6771 | Error_Msg_N ("equality operator appears too late", Eq_Op); |
6772 | end if; | |
6773 | end if; | |
6774 | end Check_Untagged_Equality; | |
6775 | ||
996ae0b0 RK |
6776 | ----------------------------- |
6777 | -- Find_Corresponding_Spec -- | |
6778 | ----------------------------- | |
6779 | ||
d44202ba HK |
6780 | function Find_Corresponding_Spec |
6781 | (N : Node_Id; | |
6782 | Post_Error : Boolean := True) return Entity_Id | |
6783 | is | |
996ae0b0 RK |
6784 | Spec : constant Node_Id := Specification (N); |
6785 | Designator : constant Entity_Id := Defining_Entity (Spec); | |
6786 | ||
6787 | E : Entity_Id; | |
6788 | ||
6789 | begin | |
6790 | E := Current_Entity (Designator); | |
996ae0b0 RK |
6791 | while Present (E) loop |
6792 | ||
6793 | -- We are looking for a matching spec. It must have the same scope, | |
6794 | -- and the same name, and either be type conformant, or be the case | |
6795 | -- of a library procedure spec and its body (which belong to one | |
6796 | -- another regardless of whether they are type conformant or not). | |
6797 | ||
6798 | if Scope (E) = Current_Scope then | |
fbf5a39b AC |
6799 | if Current_Scope = Standard_Standard |
6800 | or else (Ekind (E) = Ekind (Designator) | |
6801 | and then Type_Conformant (E, Designator)) | |
996ae0b0 RK |
6802 | then |
6803 | -- Within an instantiation, we know that spec and body are | |
6804 | -- subtype conformant, because they were subtype conformant | |
6805 | -- in the generic. We choose the subtype-conformant entity | |
6806 | -- here as well, to resolve spurious ambiguities in the | |
6807 | -- instance that were not present in the generic (i.e. when | |
6808 | -- two different types are given the same actual). If we are | |
6809 | -- looking for a spec to match a body, full conformance is | |
6810 | -- expected. | |
6811 | ||
6812 | if In_Instance then | |
6813 | Set_Convention (Designator, Convention (E)); | |
6814 | ||
0187b60e AC |
6815 | -- Skip past subprogram bodies and subprogram renamings that |
6816 | -- may appear to have a matching spec, but that aren't fully | |
6817 | -- conformant with it. That can occur in cases where an | |
6818 | -- actual type causes unrelated homographs in the instance. | |
6819 | ||
6820 | if Nkind_In (N, N_Subprogram_Body, | |
6821 | N_Subprogram_Renaming_Declaration) | |
996ae0b0 | 6822 | and then Present (Homonym (E)) |
c7b9d548 | 6823 | and then not Fully_Conformant (Designator, E) |
996ae0b0 RK |
6824 | then |
6825 | goto Next_Entity; | |
6826 | ||
c7b9d548 | 6827 | elsif not Subtype_Conformant (Designator, E) then |
996ae0b0 RK |
6828 | goto Next_Entity; |
6829 | end if; | |
6830 | end if; | |
6831 | ||
25ebc085 AC |
6832 | -- Ada 2012 (AI05-0165): For internally generated bodies of |
6833 | -- null procedures locate the internally generated spec. We | |
6834 | -- enforce mode conformance since a tagged type may inherit | |
6835 | -- from interfaces several null primitives which differ only | |
6836 | -- in the mode of the formals. | |
6837 | ||
6838 | if not (Comes_From_Source (E)) | |
6839 | and then Is_Null_Procedure (E) | |
6840 | and then not Mode_Conformant (Designator, E) | |
6841 | then | |
6842 | null; | |
6843 | ||
6844 | elsif not Has_Completion (E) then | |
996ae0b0 RK |
6845 | if Nkind (N) /= N_Subprogram_Body_Stub then |
6846 | Set_Corresponding_Spec (N, E); | |
6847 | end if; | |
6848 | ||
6849 | Set_Has_Completion (E); | |
6850 | return E; | |
6851 | ||
6852 | elsif Nkind (Parent (N)) = N_Subunit then | |
6853 | ||
6854 | -- If this is the proper body of a subunit, the completion | |
6855 | -- flag is set when analyzing the stub. | |
6856 | ||
6857 | return E; | |
6858 | ||
81db9d77 ES |
6859 | -- If E is an internal function with a controlling result |
6860 | -- that was created for an operation inherited by a null | |
6861 | -- extension, it may be overridden by a body without a previous | |
6862 | -- spec (one more reason why these should be shunned). In that | |
1366997b AC |
6863 | -- case remove the generated body if present, because the |
6864 | -- current one is the explicit overriding. | |
81db9d77 ES |
6865 | |
6866 | elsif Ekind (E) = E_Function | |
0791fbe9 | 6867 | and then Ada_Version >= Ada_2005 |
81db9d77 ES |
6868 | and then not Comes_From_Source (E) |
6869 | and then Has_Controlling_Result (E) | |
6870 | and then Is_Null_Extension (Etype (E)) | |
6871 | and then Comes_From_Source (Spec) | |
6872 | then | |
6873 | Set_Has_Completion (E, False); | |
6874 | ||
1366997b AC |
6875 | if Expander_Active |
6876 | and then Nkind (Parent (E)) = N_Function_Specification | |
6877 | then | |
81db9d77 ES |
6878 | Remove |
6879 | (Unit_Declaration_Node | |
1366997b AC |
6880 | (Corresponding_Body (Unit_Declaration_Node (E)))); |
6881 | ||
81db9d77 ES |
6882 | return E; |
6883 | ||
1366997b AC |
6884 | -- If expansion is disabled, or if the wrapper function has |
6885 | -- not been generated yet, this a late body overriding an | |
6886 | -- inherited operation, or it is an overriding by some other | |
6887 | -- declaration before the controlling result is frozen. In | |
6888 | -- either case this is a declaration of a new entity. | |
81db9d77 ES |
6889 | |
6890 | else | |
6891 | return Empty; | |
6892 | end if; | |
6893 | ||
d44202ba HK |
6894 | -- If the body already exists, then this is an error unless |
6895 | -- the previous declaration is the implicit declaration of a | |
756ef2a0 AC |
6896 | -- derived subprogram. It is also legal for an instance to |
6897 | -- contain type conformant overloadable declarations (but the | |
6898 | -- generic declaration may not), per 8.3(26/2). | |
996ae0b0 RK |
6899 | |
6900 | elsif No (Alias (E)) | |
6901 | and then not Is_Intrinsic_Subprogram (E) | |
6902 | and then not In_Instance | |
d44202ba | 6903 | and then Post_Error |
996ae0b0 RK |
6904 | then |
6905 | Error_Msg_Sloc := Sloc (E); | |
8dbd1460 | 6906 | |
07fc65c4 GB |
6907 | if Is_Imported (E) then |
6908 | Error_Msg_NE | |
6909 | ("body not allowed for imported subprogram & declared#", | |
6910 | N, E); | |
6911 | else | |
6912 | Error_Msg_NE ("duplicate body for & declared#", N, E); | |
6913 | end if; | |
996ae0b0 RK |
6914 | end if; |
6915 | ||
d44202ba HK |
6916 | -- Child units cannot be overloaded, so a conformance mismatch |
6917 | -- between body and a previous spec is an error. | |
6918 | ||
996ae0b0 RK |
6919 | elsif Is_Child_Unit (E) |
6920 | and then | |
6921 | Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body | |
6922 | and then | |
5d37ba92 | 6923 | Nkind (Parent (Unit_Declaration_Node (Designator))) = |
d44202ba HK |
6924 | N_Compilation_Unit |
6925 | and then Post_Error | |
996ae0b0 | 6926 | then |
996ae0b0 RK |
6927 | Error_Msg_N |
6928 | ("body of child unit does not match previous declaration", N); | |
6929 | end if; | |
6930 | end if; | |
6931 | ||
6932 | <<Next_Entity>> | |
6933 | E := Homonym (E); | |
6934 | end loop; | |
6935 | ||
6936 | -- On exit, we know that no previous declaration of subprogram exists | |
6937 | ||
6938 | return Empty; | |
6939 | end Find_Corresponding_Spec; | |
6940 | ||
6941 | ---------------------- | |
6942 | -- Fully_Conformant -- | |
6943 | ---------------------- | |
6944 | ||
6945 | function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
6946 | Result : Boolean; | |
996ae0b0 RK |
6947 | begin |
6948 | Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result); | |
6949 | return Result; | |
6950 | end Fully_Conformant; | |
6951 | ||
6952 | ---------------------------------- | |
6953 | -- Fully_Conformant_Expressions -- | |
6954 | ---------------------------------- | |
6955 | ||
6956 | function Fully_Conformant_Expressions | |
6957 | (Given_E1 : Node_Id; | |
d05ef0ab | 6958 | Given_E2 : Node_Id) return Boolean |
996ae0b0 RK |
6959 | is |
6960 | E1 : constant Node_Id := Original_Node (Given_E1); | |
6961 | E2 : constant Node_Id := Original_Node (Given_E2); | |
6962 | -- We always test conformance on original nodes, since it is possible | |
6963 | -- for analysis and/or expansion to make things look as though they | |
6964 | -- conform when they do not, e.g. by converting 1+2 into 3. | |
6965 | ||
6966 | function FCE (Given_E1, Given_E2 : Node_Id) return Boolean | |
6967 | renames Fully_Conformant_Expressions; | |
6968 | ||
6969 | function FCL (L1, L2 : List_Id) return Boolean; | |
6970 | -- Compare elements of two lists for conformance. Elements have to | |
6971 | -- be conformant, and actuals inserted as default parameters do not | |
6972 | -- match explicit actuals with the same value. | |
6973 | ||
6974 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean; | |
e895b435 | 6975 | -- Compare an operator node with a function call |
996ae0b0 RK |
6976 | |
6977 | --------- | |
6978 | -- FCL -- | |
6979 | --------- | |
6980 | ||
6981 | function FCL (L1, L2 : List_Id) return Boolean is | |
6982 | N1, N2 : Node_Id; | |
6983 | ||
6984 | begin | |
6985 | if L1 = No_List then | |
6986 | N1 := Empty; | |
6987 | else | |
6988 | N1 := First (L1); | |
6989 | end if; | |
6990 | ||
6991 | if L2 = No_List then | |
6992 | N2 := Empty; | |
6993 | else | |
6994 | N2 := First (L2); | |
6995 | end if; | |
6996 | ||
6997 | -- Compare two lists, skipping rewrite insertions (we want to | |
6998 | -- compare the original trees, not the expanded versions!) | |
6999 | ||
7000 | loop | |
7001 | if Is_Rewrite_Insertion (N1) then | |
7002 | Next (N1); | |
7003 | elsif Is_Rewrite_Insertion (N2) then | |
7004 | Next (N2); | |
7005 | elsif No (N1) then | |
7006 | return No (N2); | |
7007 | elsif No (N2) then | |
7008 | return False; | |
7009 | elsif not FCE (N1, N2) then | |
7010 | return False; | |
7011 | else | |
7012 | Next (N1); | |
7013 | Next (N2); | |
7014 | end if; | |
7015 | end loop; | |
7016 | end FCL; | |
7017 | ||
7018 | --------- | |
7019 | -- FCO -- | |
7020 | --------- | |
7021 | ||
7022 | function FCO (Op_Node, Call_Node : Node_Id) return Boolean is | |
7023 | Actuals : constant List_Id := Parameter_Associations (Call_Node); | |
7024 | Act : Node_Id; | |
7025 | ||
7026 | begin | |
7027 | if No (Actuals) | |
7028 | or else Entity (Op_Node) /= Entity (Name (Call_Node)) | |
7029 | then | |
7030 | return False; | |
7031 | ||
7032 | else | |
7033 | Act := First (Actuals); | |
7034 | ||
7035 | if Nkind (Op_Node) in N_Binary_Op then | |
996ae0b0 RK |
7036 | if not FCE (Left_Opnd (Op_Node), Act) then |
7037 | return False; | |
7038 | end if; | |
7039 | ||
7040 | Next (Act); | |
7041 | end if; | |
7042 | ||
7043 | return Present (Act) | |
7044 | and then FCE (Right_Opnd (Op_Node), Act) | |
7045 | and then No (Next (Act)); | |
7046 | end if; | |
7047 | end FCO; | |
7048 | ||
7049 | -- Start of processing for Fully_Conformant_Expressions | |
7050 | ||
7051 | begin | |
7052 | -- Non-conformant if paren count does not match. Note: if some idiot | |
7053 | -- complains that we don't do this right for more than 3 levels of | |
0a36105d | 7054 | -- parentheses, they will be treated with the respect they deserve! |
996ae0b0 RK |
7055 | |
7056 | if Paren_Count (E1) /= Paren_Count (E2) then | |
7057 | return False; | |
7058 | ||
82c80734 RD |
7059 | -- If same entities are referenced, then they are conformant even if |
7060 | -- they have different forms (RM 8.3.1(19-20)). | |
996ae0b0 RK |
7061 | |
7062 | elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then | |
7063 | if Present (Entity (E1)) then | |
7064 | return Entity (E1) = Entity (E2) | |
7065 | or else (Chars (Entity (E1)) = Chars (Entity (E2)) | |
7066 | and then Ekind (Entity (E1)) = E_Discriminant | |
7067 | and then Ekind (Entity (E2)) = E_In_Parameter); | |
7068 | ||
7069 | elsif Nkind (E1) = N_Expanded_Name | |
7070 | and then Nkind (E2) = N_Expanded_Name | |
7071 | and then Nkind (Selector_Name (E1)) = N_Character_Literal | |
7072 | and then Nkind (Selector_Name (E2)) = N_Character_Literal | |
7073 | then | |
7074 | return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)); | |
7075 | ||
7076 | else | |
7077 | -- Identifiers in component associations don't always have | |
7078 | -- entities, but their names must conform. | |
7079 | ||
7080 | return Nkind (E1) = N_Identifier | |
7081 | and then Nkind (E2) = N_Identifier | |
7082 | and then Chars (E1) = Chars (E2); | |
7083 | end if; | |
7084 | ||
7085 | elsif Nkind (E1) = N_Character_Literal | |
7086 | and then Nkind (E2) = N_Expanded_Name | |
7087 | then | |
7088 | return Nkind (Selector_Name (E2)) = N_Character_Literal | |
7089 | and then Chars (E1) = Chars (Selector_Name (E2)); | |
7090 | ||
7091 | elsif Nkind (E2) = N_Character_Literal | |
7092 | and then Nkind (E1) = N_Expanded_Name | |
7093 | then | |
7094 | return Nkind (Selector_Name (E1)) = N_Character_Literal | |
7095 | and then Chars (E2) = Chars (Selector_Name (E1)); | |
7096 | ||
7097 | elsif Nkind (E1) in N_Op | |
7098 | and then Nkind (E2) = N_Function_Call | |
7099 | then | |
7100 | return FCO (E1, E2); | |
7101 | ||
7102 | elsif Nkind (E2) in N_Op | |
7103 | and then Nkind (E1) = N_Function_Call | |
7104 | then | |
7105 | return FCO (E2, E1); | |
7106 | ||
7107 | -- Otherwise we must have the same syntactic entity | |
7108 | ||
7109 | elsif Nkind (E1) /= Nkind (E2) then | |
7110 | return False; | |
7111 | ||
7112 | -- At this point, we specialize by node type | |
7113 | ||
7114 | else | |
7115 | case Nkind (E1) is | |
7116 | ||
7117 | when N_Aggregate => | |
7118 | return | |
7119 | FCL (Expressions (E1), Expressions (E2)) | |
19d846a0 RD |
7120 | and then |
7121 | FCL (Component_Associations (E1), | |
7122 | Component_Associations (E2)); | |
996ae0b0 RK |
7123 | |
7124 | when N_Allocator => | |
7125 | if Nkind (Expression (E1)) = N_Qualified_Expression | |
7126 | or else | |
7127 | Nkind (Expression (E2)) = N_Qualified_Expression | |
7128 | then | |
7129 | return FCE (Expression (E1), Expression (E2)); | |
7130 | ||
7131 | -- Check that the subtype marks and any constraints | |
7132 | -- are conformant | |
7133 | ||
7134 | else | |
7135 | declare | |
7136 | Indic1 : constant Node_Id := Expression (E1); | |
7137 | Indic2 : constant Node_Id := Expression (E2); | |
7138 | Elt1 : Node_Id; | |
7139 | Elt2 : Node_Id; | |
7140 | ||
7141 | begin | |
7142 | if Nkind (Indic1) /= N_Subtype_Indication then | |
7143 | return | |
7144 | Nkind (Indic2) /= N_Subtype_Indication | |
7145 | and then Entity (Indic1) = Entity (Indic2); | |
7146 | ||
7147 | elsif Nkind (Indic2) /= N_Subtype_Indication then | |
7148 | return | |
7149 | Nkind (Indic1) /= N_Subtype_Indication | |
7150 | and then Entity (Indic1) = Entity (Indic2); | |
7151 | ||
7152 | else | |
7153 | if Entity (Subtype_Mark (Indic1)) /= | |
7154 | Entity (Subtype_Mark (Indic2)) | |
7155 | then | |
7156 | return False; | |
7157 | end if; | |
7158 | ||
7159 | Elt1 := First (Constraints (Constraint (Indic1))); | |
7160 | Elt2 := First (Constraints (Constraint (Indic2))); | |
996ae0b0 RK |
7161 | while Present (Elt1) and then Present (Elt2) loop |
7162 | if not FCE (Elt1, Elt2) then | |
7163 | return False; | |
7164 | end if; | |
7165 | ||
7166 | Next (Elt1); | |
7167 | Next (Elt2); | |
7168 | end loop; | |
7169 | ||
7170 | return True; | |
7171 | end if; | |
7172 | end; | |
7173 | end if; | |
7174 | ||
7175 | when N_Attribute_Reference => | |
7176 | return | |
7177 | Attribute_Name (E1) = Attribute_Name (E2) | |
7178 | and then FCL (Expressions (E1), Expressions (E2)); | |
7179 | ||
7180 | when N_Binary_Op => | |
7181 | return | |
7182 | Entity (E1) = Entity (E2) | |
7183 | and then FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7184 | and then FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7185 | ||
514d0fc5 | 7186 | when N_Short_Circuit | N_Membership_Test => |
996ae0b0 RK |
7187 | return |
7188 | FCE (Left_Opnd (E1), Left_Opnd (E2)) | |
7189 | and then | |
7190 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
7191 | ||
19d846a0 RD |
7192 | when N_Case_Expression => |
7193 | declare | |
7194 | Alt1 : Node_Id; | |
7195 | Alt2 : Node_Id; | |
7196 | ||
7197 | begin | |
7198 | if not FCE (Expression (E1), Expression (E2)) then | |
7199 | return False; | |
7200 | ||
7201 | else | |
7202 | Alt1 := First (Alternatives (E1)); | |
7203 | Alt2 := First (Alternatives (E2)); | |
7204 | loop | |
7205 | if Present (Alt1) /= Present (Alt2) then | |
7206 | return False; | |
7207 | elsif No (Alt1) then | |
7208 | return True; | |
7209 | end if; | |
7210 | ||
7211 | if not FCE (Expression (Alt1), Expression (Alt2)) | |
7212 | or else not FCL (Discrete_Choices (Alt1), | |
7213 | Discrete_Choices (Alt2)) | |
7214 | then | |
7215 | return False; | |
7216 | end if; | |
7217 | ||
7218 | Next (Alt1); | |
7219 | Next (Alt2); | |
7220 | end loop; | |
7221 | end if; | |
7222 | end; | |
7223 | ||
996ae0b0 RK |
7224 | when N_Character_Literal => |
7225 | return | |
7226 | Char_Literal_Value (E1) = Char_Literal_Value (E2); | |
7227 | ||
7228 | when N_Component_Association => | |
7229 | return | |
7230 | FCL (Choices (E1), Choices (E2)) | |
19d846a0 RD |
7231 | and then |
7232 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7233 | |
7234 | when N_Conditional_Expression => | |
7235 | return | |
7236 | FCL (Expressions (E1), Expressions (E2)); | |
7237 | ||
7238 | when N_Explicit_Dereference => | |
7239 | return | |
7240 | FCE (Prefix (E1), Prefix (E2)); | |
7241 | ||
7242 | when N_Extension_Aggregate => | |
7243 | return | |
7244 | FCL (Expressions (E1), Expressions (E2)) | |
7245 | and then Null_Record_Present (E1) = | |
7246 | Null_Record_Present (E2) | |
7247 | and then FCL (Component_Associations (E1), | |
7248 | Component_Associations (E2)); | |
7249 | ||
7250 | when N_Function_Call => | |
7251 | return | |
7252 | FCE (Name (E1), Name (E2)) | |
19d846a0 RD |
7253 | and then |
7254 | FCL (Parameter_Associations (E1), | |
7255 | Parameter_Associations (E2)); | |
996ae0b0 RK |
7256 | |
7257 | when N_Indexed_Component => | |
7258 | return | |
7259 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7260 | and then |
7261 | FCL (Expressions (E1), Expressions (E2)); | |
996ae0b0 RK |
7262 | |
7263 | when N_Integer_Literal => | |
7264 | return (Intval (E1) = Intval (E2)); | |
7265 | ||
7266 | when N_Null => | |
7267 | return True; | |
7268 | ||
7269 | when N_Operator_Symbol => | |
7270 | return | |
7271 | Chars (E1) = Chars (E2); | |
7272 | ||
7273 | when N_Others_Choice => | |
7274 | return True; | |
7275 | ||
7276 | when N_Parameter_Association => | |
7277 | return | |
996ae0b0 RK |
7278 | Chars (Selector_Name (E1)) = Chars (Selector_Name (E2)) |
7279 | and then FCE (Explicit_Actual_Parameter (E1), | |
7280 | Explicit_Actual_Parameter (E2)); | |
7281 | ||
7282 | when N_Qualified_Expression => | |
7283 | return | |
7284 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7285 | and then |
7286 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 | 7287 | |
2010d078 AC |
7288 | when N_Quantified_Expression => |
7289 | if not FCE (Condition (E1), Condition (E2)) then | |
7290 | return False; | |
7291 | end if; | |
7292 | ||
7293 | if Present (Loop_Parameter_Specification (E1)) | |
7294 | and then Present (Loop_Parameter_Specification (E2)) | |
7295 | then | |
7296 | declare | |
7297 | L1 : constant Node_Id := | |
7298 | Loop_Parameter_Specification (E1); | |
7299 | L2 : constant Node_Id := | |
7300 | Loop_Parameter_Specification (E2); | |
7301 | ||
7302 | begin | |
7303 | return | |
7304 | Reverse_Present (L1) = Reverse_Present (L2) | |
7305 | and then | |
7306 | FCE (Defining_Identifier (L1), | |
7307 | Defining_Identifier (L2)) | |
7308 | and then | |
7309 | FCE (Discrete_Subtype_Definition (L1), | |
7310 | Discrete_Subtype_Definition (L2)); | |
7311 | end; | |
7312 | ||
7313 | else -- quantified expression with an iterator | |
7314 | declare | |
7315 | I1 : constant Node_Id := Iterator_Specification (E1); | |
7316 | I2 : constant Node_Id := Iterator_Specification (E2); | |
7317 | ||
7318 | begin | |
7319 | return | |
7320 | FCE (Defining_Identifier (I1), | |
7321 | Defining_Identifier (I2)) | |
7322 | and then | |
7323 | Of_Present (I1) = Of_Present (I2) | |
7324 | and then | |
7325 | Reverse_Present (I1) = Reverse_Present (I2) | |
7326 | and then FCE (Name (I1), Name (I2)) | |
7327 | and then FCE (Subtype_Indication (I1), | |
7328 | Subtype_Indication (I2)); | |
7329 | end; | |
7330 | end if; | |
7331 | ||
996ae0b0 RK |
7332 | when N_Range => |
7333 | return | |
7334 | FCE (Low_Bound (E1), Low_Bound (E2)) | |
19d846a0 RD |
7335 | and then |
7336 | FCE (High_Bound (E1), High_Bound (E2)); | |
996ae0b0 RK |
7337 | |
7338 | when N_Real_Literal => | |
7339 | return (Realval (E1) = Realval (E2)); | |
7340 | ||
7341 | when N_Selected_Component => | |
7342 | return | |
7343 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7344 | and then |
7345 | FCE (Selector_Name (E1), Selector_Name (E2)); | |
996ae0b0 RK |
7346 | |
7347 | when N_Slice => | |
7348 | return | |
7349 | FCE (Prefix (E1), Prefix (E2)) | |
19d846a0 RD |
7350 | and then |
7351 | FCE (Discrete_Range (E1), Discrete_Range (E2)); | |
996ae0b0 RK |
7352 | |
7353 | when N_String_Literal => | |
7354 | declare | |
7355 | S1 : constant String_Id := Strval (E1); | |
7356 | S2 : constant String_Id := Strval (E2); | |
7357 | L1 : constant Nat := String_Length (S1); | |
7358 | L2 : constant Nat := String_Length (S2); | |
7359 | ||
7360 | begin | |
7361 | if L1 /= L2 then | |
7362 | return False; | |
7363 | ||
7364 | else | |
7365 | for J in 1 .. L1 loop | |
7366 | if Get_String_Char (S1, J) /= | |
7367 | Get_String_Char (S2, J) | |
7368 | then | |
7369 | return False; | |
7370 | end if; | |
7371 | end loop; | |
7372 | ||
7373 | return True; | |
7374 | end if; | |
7375 | end; | |
7376 | ||
7377 | when N_Type_Conversion => | |
7378 | return | |
7379 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7380 | and then |
7381 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7382 | |
7383 | when N_Unary_Op => | |
7384 | return | |
7385 | Entity (E1) = Entity (E2) | |
19d846a0 RD |
7386 | and then |
7387 | FCE (Right_Opnd (E1), Right_Opnd (E2)); | |
996ae0b0 RK |
7388 | |
7389 | when N_Unchecked_Type_Conversion => | |
7390 | return | |
7391 | FCE (Subtype_Mark (E1), Subtype_Mark (E2)) | |
19d846a0 RD |
7392 | and then |
7393 | FCE (Expression (E1), Expression (E2)); | |
996ae0b0 RK |
7394 | |
7395 | -- All other node types cannot appear in this context. Strictly | |
7396 | -- we should raise a fatal internal error. Instead we just ignore | |
7397 | -- the nodes. This means that if anyone makes a mistake in the | |
7398 | -- expander and mucks an expression tree irretrievably, the | |
7399 | -- result will be a failure to detect a (probably very obscure) | |
7400 | -- case of non-conformance, which is better than bombing on some | |
7401 | -- case where two expressions do in fact conform. | |
7402 | ||
7403 | when others => | |
7404 | return True; | |
7405 | ||
7406 | end case; | |
7407 | end if; | |
7408 | end Fully_Conformant_Expressions; | |
7409 | ||
fbf5a39b AC |
7410 | ---------------------------------------- |
7411 | -- Fully_Conformant_Discrete_Subtypes -- | |
7412 | ---------------------------------------- | |
7413 | ||
7414 | function Fully_Conformant_Discrete_Subtypes | |
7415 | (Given_S1 : Node_Id; | |
d05ef0ab | 7416 | Given_S2 : Node_Id) return Boolean |
fbf5a39b AC |
7417 | is |
7418 | S1 : constant Node_Id := Original_Node (Given_S1); | |
7419 | S2 : constant Node_Id := Original_Node (Given_S2); | |
7420 | ||
7421 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean; | |
82c80734 RD |
7422 | -- Special-case for a bound given by a discriminant, which in the body |
7423 | -- is replaced with the discriminal of the enclosing type. | |
fbf5a39b AC |
7424 | |
7425 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean; | |
e895b435 | 7426 | -- Check both bounds |
fbf5a39b | 7427 | |
5d37ba92 ES |
7428 | ----------------------- |
7429 | -- Conforming_Bounds -- | |
7430 | ----------------------- | |
7431 | ||
fbf5a39b AC |
7432 | function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is |
7433 | begin | |
7434 | if Is_Entity_Name (B1) | |
7435 | and then Is_Entity_Name (B2) | |
7436 | and then Ekind (Entity (B1)) = E_Discriminant | |
7437 | then | |
7438 | return Chars (B1) = Chars (B2); | |
7439 | ||
7440 | else | |
7441 | return Fully_Conformant_Expressions (B1, B2); | |
7442 | end if; | |
7443 | end Conforming_Bounds; | |
7444 | ||
5d37ba92 ES |
7445 | ----------------------- |
7446 | -- Conforming_Ranges -- | |
7447 | ----------------------- | |
7448 | ||
fbf5a39b AC |
7449 | function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is |
7450 | begin | |
7451 | return | |
7452 | Conforming_Bounds (Low_Bound (R1), Low_Bound (R2)) | |
7453 | and then | |
7454 | Conforming_Bounds (High_Bound (R1), High_Bound (R2)); | |
7455 | end Conforming_Ranges; | |
7456 | ||
7457 | -- Start of processing for Fully_Conformant_Discrete_Subtypes | |
7458 | ||
7459 | begin | |
7460 | if Nkind (S1) /= Nkind (S2) then | |
7461 | return False; | |
7462 | ||
7463 | elsif Is_Entity_Name (S1) then | |
7464 | return Entity (S1) = Entity (S2); | |
7465 | ||
7466 | elsif Nkind (S1) = N_Range then | |
7467 | return Conforming_Ranges (S1, S2); | |
7468 | ||
7469 | elsif Nkind (S1) = N_Subtype_Indication then | |
7470 | return | |
7471 | Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2)) | |
7472 | and then | |
7473 | Conforming_Ranges | |
7474 | (Range_Expression (Constraint (S1)), | |
7475 | Range_Expression (Constraint (S2))); | |
7476 | else | |
7477 | return True; | |
7478 | end if; | |
7479 | end Fully_Conformant_Discrete_Subtypes; | |
7480 | ||
996ae0b0 RK |
7481 | -------------------- |
7482 | -- Install_Entity -- | |
7483 | -------------------- | |
7484 | ||
7485 | procedure Install_Entity (E : Entity_Id) is | |
7486 | Prev : constant Entity_Id := Current_Entity (E); | |
996ae0b0 RK |
7487 | begin |
7488 | Set_Is_Immediately_Visible (E); | |
7489 | Set_Current_Entity (E); | |
7490 | Set_Homonym (E, Prev); | |
7491 | end Install_Entity; | |
7492 | ||
7493 | --------------------- | |
7494 | -- Install_Formals -- | |
7495 | --------------------- | |
7496 | ||
7497 | procedure Install_Formals (Id : Entity_Id) is | |
7498 | F : Entity_Id; | |
996ae0b0 RK |
7499 | begin |
7500 | F := First_Formal (Id); | |
996ae0b0 RK |
7501 | while Present (F) loop |
7502 | Install_Entity (F); | |
7503 | Next_Formal (F); | |
7504 | end loop; | |
7505 | end Install_Formals; | |
7506 | ||
ce2b6ba5 JM |
7507 | ----------------------------- |
7508 | -- Is_Interface_Conformant -- | |
7509 | ----------------------------- | |
7510 | ||
7511 | function Is_Interface_Conformant | |
7512 | (Tagged_Type : Entity_Id; | |
7513 | Iface_Prim : Entity_Id; | |
7514 | Prim : Entity_Id) return Boolean | |
7515 | is | |
fceeaab6 ES |
7516 | Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim); |
7517 | Typ : constant Entity_Id := Find_Dispatching_Type (Prim); | |
7518 | ||
25ebc085 AC |
7519 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id; |
7520 | -- Return the controlling formal of Prim | |
7521 | ||
59e6b23c AC |
7522 | ------------------------ |
7523 | -- Controlling_Formal -- | |
7524 | ------------------------ | |
7525 | ||
25ebc085 AC |
7526 | function Controlling_Formal (Prim : Entity_Id) return Entity_Id is |
7527 | E : Entity_Id := First_Entity (Prim); | |
59e6b23c | 7528 | |
25ebc085 AC |
7529 | begin |
7530 | while Present (E) loop | |
7531 | if Is_Formal (E) and then Is_Controlling_Formal (E) then | |
7532 | return E; | |
7533 | end if; | |
7534 | ||
7535 | Next_Entity (E); | |
7536 | end loop; | |
7537 | ||
7538 | return Empty; | |
7539 | end Controlling_Formal; | |
7540 | ||
7541 | -- Local variables | |
7542 | ||
7543 | Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim); | |
7544 | Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim); | |
7545 | ||
7546 | -- Start of processing for Is_Interface_Conformant | |
7547 | ||
ce2b6ba5 JM |
7548 | begin |
7549 | pragma Assert (Is_Subprogram (Iface_Prim) | |
7550 | and then Is_Subprogram (Prim) | |
7551 | and then Is_Dispatching_Operation (Iface_Prim) | |
7552 | and then Is_Dispatching_Operation (Prim)); | |
7553 | ||
fceeaab6 | 7554 | pragma Assert (Is_Interface (Iface) |
ce2b6ba5 JM |
7555 | or else (Present (Alias (Iface_Prim)) |
7556 | and then | |
7557 | Is_Interface | |
7558 | (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim))))); | |
7559 | ||
7560 | if Prim = Iface_Prim | |
7561 | or else not Is_Subprogram (Prim) | |
7562 | or else Ekind (Prim) /= Ekind (Iface_Prim) | |
7563 | or else not Is_Dispatching_Operation (Prim) | |
7564 | or else Scope (Prim) /= Scope (Tagged_Type) | |
fceeaab6 ES |
7565 | or else No (Typ) |
7566 | or else Base_Type (Typ) /= Tagged_Type | |
ce2b6ba5 JM |
7567 | or else not Primitive_Names_Match (Iface_Prim, Prim) |
7568 | then | |
7569 | return False; | |
7570 | ||
25ebc085 AC |
7571 | -- The mode of the controlling formals must match |
7572 | ||
7573 | elsif Present (Iface_Ctrl_F) | |
7574 | and then Present (Prim_Ctrl_F) | |
7575 | and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F) | |
7576 | then | |
7577 | return False; | |
7578 | ||
7579 | -- Case of a procedure, or a function whose result type matches the | |
7580 | -- result type of the interface primitive, or a function that has no | |
7581 | -- controlling result (I or access I). | |
ce2b6ba5 JM |
7582 | |
7583 | elsif Ekind (Iface_Prim) = E_Procedure | |
7584 | or else Etype (Prim) = Etype (Iface_Prim) | |
fceeaab6 | 7585 | or else not Has_Controlling_Result (Prim) |
ce2b6ba5 | 7586 | then |
b4d7b435 AC |
7587 | return Type_Conformant |
7588 | (Iface_Prim, Prim, Skip_Controlling_Formals => True); | |
ce2b6ba5 | 7589 | |
fceeaab6 ES |
7590 | -- Case of a function returning an interface, or an access to one. |
7591 | -- Check that the return types correspond. | |
ce2b6ba5 | 7592 | |
fceeaab6 ES |
7593 | elsif Implements_Interface (Typ, Iface) then |
7594 | if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type) | |
9a3c9940 RD |
7595 | /= |
7596 | (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type) | |
fceeaab6 ES |
7597 | then |
7598 | return False; | |
fceeaab6 ES |
7599 | else |
7600 | return | |
ce2b6ba5 JM |
7601 | Type_Conformant (Prim, Iface_Prim, |
7602 | Skip_Controlling_Formals => True); | |
fceeaab6 | 7603 | end if; |
ce2b6ba5 | 7604 | |
fceeaab6 ES |
7605 | else |
7606 | return False; | |
ce2b6ba5 | 7607 | end if; |
ce2b6ba5 JM |
7608 | end Is_Interface_Conformant; |
7609 | ||
996ae0b0 RK |
7610 | --------------------------------- |
7611 | -- Is_Non_Overriding_Operation -- | |
7612 | --------------------------------- | |
7613 | ||
7614 | function Is_Non_Overriding_Operation | |
7615 | (Prev_E : Entity_Id; | |
d05ef0ab | 7616 | New_E : Entity_Id) return Boolean |
996ae0b0 RK |
7617 | is |
7618 | Formal : Entity_Id; | |
7619 | F_Typ : Entity_Id; | |
7620 | G_Typ : Entity_Id := Empty; | |
7621 | ||
7622 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id; | |
5d37ba92 ES |
7623 | -- If F_Type is a derived type associated with a generic actual subtype, |
7624 | -- then return its Generic_Parent_Type attribute, else return Empty. | |
996ae0b0 RK |
7625 | |
7626 | function Types_Correspond | |
7627 | (P_Type : Entity_Id; | |
d05ef0ab | 7628 | N_Type : Entity_Id) return Boolean; |
82c80734 RD |
7629 | -- Returns true if and only if the types (or designated types in the |
7630 | -- case of anonymous access types) are the same or N_Type is derived | |
7631 | -- directly or indirectly from P_Type. | |
996ae0b0 RK |
7632 | |
7633 | ----------------------------- | |
7634 | -- Get_Generic_Parent_Type -- | |
7635 | ----------------------------- | |
7636 | ||
7637 | function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is | |
7638 | G_Typ : Entity_Id; | |
702d2020 | 7639 | Defn : Node_Id; |
996ae0b0 RK |
7640 | Indic : Node_Id; |
7641 | ||
7642 | begin | |
7643 | if Is_Derived_Type (F_Typ) | |
7644 | and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration | |
7645 | then | |
82c80734 RD |
7646 | -- The tree must be traversed to determine the parent subtype in |
7647 | -- the generic unit, which unfortunately isn't always available | |
7648 | -- via semantic attributes. ??? (Note: The use of Original_Node | |
7649 | -- is needed for cases where a full derived type has been | |
7650 | -- rewritten.) | |
996ae0b0 | 7651 | |
702d2020 AC |
7652 | Defn := Type_Definition (Original_Node (Parent (F_Typ))); |
7653 | if Nkind (Defn) = N_Derived_Type_Definition then | |
7654 | Indic := Subtype_Indication (Defn); | |
996ae0b0 | 7655 | |
702d2020 AC |
7656 | if Nkind (Indic) = N_Subtype_Indication then |
7657 | G_Typ := Entity (Subtype_Mark (Indic)); | |
7658 | else | |
7659 | G_Typ := Entity (Indic); | |
7660 | end if; | |
996ae0b0 | 7661 | |
702d2020 AC |
7662 | if Nkind (Parent (G_Typ)) = N_Subtype_Declaration |
7663 | and then Present (Generic_Parent_Type (Parent (G_Typ))) | |
7664 | then | |
7665 | return Generic_Parent_Type (Parent (G_Typ)); | |
7666 | end if; | |
996ae0b0 RK |
7667 | end if; |
7668 | end if; | |
7669 | ||
7670 | return Empty; | |
7671 | end Get_Generic_Parent_Type; | |
7672 | ||
7673 | ---------------------- | |
7674 | -- Types_Correspond -- | |
7675 | ---------------------- | |
7676 | ||
7677 | function Types_Correspond | |
7678 | (P_Type : Entity_Id; | |
d05ef0ab | 7679 | N_Type : Entity_Id) return Boolean |
996ae0b0 RK |
7680 | is |
7681 | Prev_Type : Entity_Id := Base_Type (P_Type); | |
7682 | New_Type : Entity_Id := Base_Type (N_Type); | |
7683 | ||
7684 | begin | |
7685 | if Ekind (Prev_Type) = E_Anonymous_Access_Type then | |
7686 | Prev_Type := Designated_Type (Prev_Type); | |
7687 | end if; | |
7688 | ||
7689 | if Ekind (New_Type) = E_Anonymous_Access_Type then | |
7690 | New_Type := Designated_Type (New_Type); | |
7691 | end if; | |
7692 | ||
7693 | if Prev_Type = New_Type then | |
7694 | return True; | |
7695 | ||
7696 | elsif not Is_Class_Wide_Type (New_Type) then | |
7697 | while Etype (New_Type) /= New_Type loop | |
7698 | New_Type := Etype (New_Type); | |
7699 | if New_Type = Prev_Type then | |
7700 | return True; | |
7701 | end if; | |
7702 | end loop; | |
7703 | end if; | |
7704 | return False; | |
7705 | end Types_Correspond; | |
7706 | ||
7707 | -- Start of processing for Is_Non_Overriding_Operation | |
7708 | ||
7709 | begin | |
82c80734 RD |
7710 | -- In the case where both operations are implicit derived subprograms |
7711 | -- then neither overrides the other. This can only occur in certain | |
7712 | -- obscure cases (e.g., derivation from homographs created in a generic | |
7713 | -- instantiation). | |
996ae0b0 RK |
7714 | |
7715 | if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then | |
7716 | return True; | |
7717 | ||
7718 | elsif Ekind (Current_Scope) = E_Package | |
7719 | and then Is_Generic_Instance (Current_Scope) | |
7720 | and then In_Private_Part (Current_Scope) | |
7721 | and then Comes_From_Source (New_E) | |
7722 | then | |
702d2020 AC |
7723 | -- We examine the formals and result type of the inherited operation, |
7724 | -- to determine whether their type is derived from (the instance of) | |
7725 | -- a generic type. The first such formal or result type is the one | |
7726 | -- tested. | |
996ae0b0 RK |
7727 | |
7728 | Formal := First_Formal (Prev_E); | |
996ae0b0 RK |
7729 | while Present (Formal) loop |
7730 | F_Typ := Base_Type (Etype (Formal)); | |
7731 | ||
7732 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
7733 | F_Typ := Designated_Type (F_Typ); | |
7734 | end if; | |
7735 | ||
7736 | G_Typ := Get_Generic_Parent_Type (F_Typ); | |
702d2020 | 7737 | exit when Present (G_Typ); |
996ae0b0 RK |
7738 | |
7739 | Next_Formal (Formal); | |
7740 | end loop; | |
7741 | ||
c8ef728f | 7742 | if No (G_Typ) and then Ekind (Prev_E) = E_Function then |
996ae0b0 RK |
7743 | G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E))); |
7744 | end if; | |
7745 | ||
7746 | if No (G_Typ) then | |
7747 | return False; | |
7748 | end if; | |
7749 | ||
8dbd1460 AC |
7750 | -- If the generic type is a private type, then the original operation |
7751 | -- was not overriding in the generic, because there was no primitive | |
7752 | -- operation to override. | |
996ae0b0 RK |
7753 | |
7754 | if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration | |
7755 | and then Nkind (Formal_Type_Definition (Parent (G_Typ))) = | |
8dbd1460 | 7756 | N_Formal_Private_Type_Definition |
996ae0b0 RK |
7757 | then |
7758 | return True; | |
7759 | ||
7760 | -- The generic parent type is the ancestor of a formal derived | |
7761 | -- type declaration. We need to check whether it has a primitive | |
7762 | -- operation that should be overridden by New_E in the generic. | |
7763 | ||
7764 | else | |
7765 | declare | |
7766 | P_Formal : Entity_Id; | |
7767 | N_Formal : Entity_Id; | |
7768 | P_Typ : Entity_Id; | |
7769 | N_Typ : Entity_Id; | |
7770 | P_Prim : Entity_Id; | |
7771 | Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ)); | |
7772 | ||
7773 | begin | |
7774 | while Present (Prim_Elt) loop | |
7775 | P_Prim := Node (Prim_Elt); | |
fbf5a39b | 7776 | |
996ae0b0 RK |
7777 | if Chars (P_Prim) = Chars (New_E) |
7778 | and then Ekind (P_Prim) = Ekind (New_E) | |
7779 | then | |
7780 | P_Formal := First_Formal (P_Prim); | |
7781 | N_Formal := First_Formal (New_E); | |
7782 | while Present (P_Formal) and then Present (N_Formal) loop | |
7783 | P_Typ := Etype (P_Formal); | |
7784 | N_Typ := Etype (N_Formal); | |
7785 | ||
7786 | if not Types_Correspond (P_Typ, N_Typ) then | |
7787 | exit; | |
7788 | end if; | |
7789 | ||
7790 | Next_Entity (P_Formal); | |
7791 | Next_Entity (N_Formal); | |
7792 | end loop; | |
7793 | ||
82c80734 RD |
7794 | -- Found a matching primitive operation belonging to the |
7795 | -- formal ancestor type, so the new subprogram is | |
7796 | -- overriding. | |
996ae0b0 | 7797 | |
c8ef728f ES |
7798 | if No (P_Formal) |
7799 | and then No (N_Formal) | |
996ae0b0 RK |
7800 | and then (Ekind (New_E) /= E_Function |
7801 | or else | |
7802 | Types_Correspond | |
7803 | (Etype (P_Prim), Etype (New_E))) | |
7804 | then | |
7805 | return False; | |
7806 | end if; | |
7807 | end if; | |
7808 | ||
7809 | Next_Elmt (Prim_Elt); | |
7810 | end loop; | |
7811 | ||
82c80734 RD |
7812 | -- If no match found, then the new subprogram does not |
7813 | -- override in the generic (nor in the instance). | |
996ae0b0 RK |
7814 | |
7815 | return True; | |
7816 | end; | |
7817 | end if; | |
7818 | else | |
7819 | return False; | |
7820 | end if; | |
7821 | end Is_Non_Overriding_Operation; | |
7822 | ||
beacce02 AC |
7823 | ------------------------------------- |
7824 | -- List_Inherited_Pre_Post_Aspects -- | |
7825 | ------------------------------------- | |
7826 | ||
7827 | procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is | |
7828 | begin | |
e606088a | 7829 | if Opt.List_Inherited_Aspects |
beacce02 AC |
7830 | and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E)) |
7831 | then | |
7832 | declare | |
7833 | Inherited : constant Subprogram_List := | |
7834 | Inherited_Subprograms (E); | |
7835 | P : Node_Id; | |
7836 | ||
7837 | begin | |
7838 | for J in Inherited'Range loop | |
dac3bede YM |
7839 | P := Spec_PPC_List (Contract (Inherited (J))); |
7840 | ||
beacce02 AC |
7841 | while Present (P) loop |
7842 | Error_Msg_Sloc := Sloc (P); | |
7843 | ||
7844 | if Class_Present (P) and then not Split_PPC (P) then | |
7845 | if Pragma_Name (P) = Name_Precondition then | |
7846 | Error_Msg_N | |
7847 | ("?info: & inherits `Pre''Class` aspect from #", E); | |
7848 | else | |
7849 | Error_Msg_N | |
7850 | ("?info: & inherits `Post''Class` aspect from #", E); | |
7851 | end if; | |
7852 | end if; | |
7853 | ||
7854 | P := Next_Pragma (P); | |
7855 | end loop; | |
7856 | end loop; | |
7857 | end; | |
7858 | end if; | |
7859 | end List_Inherited_Pre_Post_Aspects; | |
7860 | ||
996ae0b0 RK |
7861 | ------------------------------ |
7862 | -- Make_Inequality_Operator -- | |
7863 | ------------------------------ | |
7864 | ||
7865 | -- S is the defining identifier of an equality operator. We build a | |
7866 | -- subprogram declaration with the right signature. This operation is | |
7867 | -- intrinsic, because it is always expanded as the negation of the | |
7868 | -- call to the equality function. | |
7869 | ||
7870 | procedure Make_Inequality_Operator (S : Entity_Id) is | |
7871 | Loc : constant Source_Ptr := Sloc (S); | |
7872 | Decl : Node_Id; | |
7873 | Formals : List_Id; | |
7874 | Op_Name : Entity_Id; | |
7875 | ||
c8ef728f ES |
7876 | FF : constant Entity_Id := First_Formal (S); |
7877 | NF : constant Entity_Id := Next_Formal (FF); | |
996ae0b0 RK |
7878 | |
7879 | begin | |
c8ef728f | 7880 | -- Check that equality was properly defined, ignore call if not |
996ae0b0 | 7881 | |
c8ef728f | 7882 | if No (NF) then |
996ae0b0 RK |
7883 | return; |
7884 | end if; | |
7885 | ||
c8ef728f ES |
7886 | declare |
7887 | A : constant Entity_Id := | |
7888 | Make_Defining_Identifier (Sloc (FF), | |
7889 | Chars => Chars (FF)); | |
7890 | ||
5d37ba92 ES |
7891 | B : constant Entity_Id := |
7892 | Make_Defining_Identifier (Sloc (NF), | |
7893 | Chars => Chars (NF)); | |
c8ef728f ES |
7894 | |
7895 | begin | |
7896 | Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne); | |
7897 | ||
7898 | Formals := New_List ( | |
7899 | Make_Parameter_Specification (Loc, | |
7900 | Defining_Identifier => A, | |
7901 | Parameter_Type => | |
7902 | New_Reference_To (Etype (First_Formal (S)), | |
7903 | Sloc (Etype (First_Formal (S))))), | |
7904 | ||
7905 | Make_Parameter_Specification (Loc, | |
7906 | Defining_Identifier => B, | |
7907 | Parameter_Type => | |
7908 | New_Reference_To (Etype (Next_Formal (First_Formal (S))), | |
7909 | Sloc (Etype (Next_Formal (First_Formal (S))))))); | |
7910 | ||
7911 | Decl := | |
7912 | Make_Subprogram_Declaration (Loc, | |
7913 | Specification => | |
7914 | Make_Function_Specification (Loc, | |
7915 | Defining_Unit_Name => Op_Name, | |
7916 | Parameter_Specifications => Formals, | |
7917 | Result_Definition => | |
7918 | New_Reference_To (Standard_Boolean, Loc))); | |
7919 | ||
7920 | -- Insert inequality right after equality if it is explicit or after | |
7921 | -- the derived type when implicit. These entities are created only | |
7922 | -- for visibility purposes, and eventually replaced in the course of | |
7923 | -- expansion, so they do not need to be attached to the tree and seen | |
7924 | -- by the back-end. Keeping them internal also avoids spurious | |
7925 | -- freezing problems. The declaration is inserted in the tree for | |
7926 | -- analysis, and removed afterwards. If the equality operator comes | |
7927 | -- from an explicit declaration, attach the inequality immediately | |
7928 | -- after. Else the equality is inherited from a derived type | |
7929 | -- declaration, so insert inequality after that declaration. | |
7930 | ||
7931 | if No (Alias (S)) then | |
7932 | Insert_After (Unit_Declaration_Node (S), Decl); | |
7933 | elsif Is_List_Member (Parent (S)) then | |
7934 | Insert_After (Parent (S), Decl); | |
7935 | else | |
7936 | Insert_After (Parent (Etype (First_Formal (S))), Decl); | |
7937 | end if; | |
996ae0b0 | 7938 | |
c8ef728f ES |
7939 | Mark_Rewrite_Insertion (Decl); |
7940 | Set_Is_Intrinsic_Subprogram (Op_Name); | |
7941 | Analyze (Decl); | |
7942 | Remove (Decl); | |
7943 | Set_Has_Completion (Op_Name); | |
7944 | Set_Corresponding_Equality (Op_Name, S); | |
f937473f | 7945 | Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S)); |
c8ef728f | 7946 | end; |
996ae0b0 RK |
7947 | end Make_Inequality_Operator; |
7948 | ||
7949 | ---------------------- | |
7950 | -- May_Need_Actuals -- | |
7951 | ---------------------- | |
7952 | ||
7953 | procedure May_Need_Actuals (Fun : Entity_Id) is | |
7954 | F : Entity_Id; | |
7955 | B : Boolean; | |
7956 | ||
7957 | begin | |
7958 | F := First_Formal (Fun); | |
7959 | B := True; | |
996ae0b0 RK |
7960 | while Present (F) loop |
7961 | if No (Default_Value (F)) then | |
7962 | B := False; | |
7963 | exit; | |
7964 | end if; | |
7965 | ||
7966 | Next_Formal (F); | |
7967 | end loop; | |
7968 | ||
7969 | Set_Needs_No_Actuals (Fun, B); | |
7970 | end May_Need_Actuals; | |
7971 | ||
7972 | --------------------- | |
7973 | -- Mode_Conformant -- | |
7974 | --------------------- | |
7975 | ||
7976 | function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is | |
7977 | Result : Boolean; | |
996ae0b0 RK |
7978 | begin |
7979 | Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result); | |
7980 | return Result; | |
7981 | end Mode_Conformant; | |
7982 | ||
7983 | --------------------------- | |
7984 | -- New_Overloaded_Entity -- | |
7985 | --------------------------- | |
7986 | ||
7987 | procedure New_Overloaded_Entity | |
7988 | (S : Entity_Id; | |
7989 | Derived_Type : Entity_Id := Empty) | |
7990 | is | |
ec4867fa | 7991 | Overridden_Subp : Entity_Id := Empty; |
758c442c GD |
7992 | -- Set if the current scope has an operation that is type-conformant |
7993 | -- with S, and becomes hidden by S. | |
7994 | ||
5d37ba92 ES |
7995 | Is_Primitive_Subp : Boolean; |
7996 | -- Set to True if the new subprogram is primitive | |
7997 | ||
fbf5a39b AC |
7998 | E : Entity_Id; |
7999 | -- Entity that S overrides | |
8000 | ||
996ae0b0 | 8001 | Prev_Vis : Entity_Id := Empty; |
ec4867fa ES |
8002 | -- Predecessor of E in Homonym chain |
8003 | ||
5d37ba92 ES |
8004 | procedure Check_For_Primitive_Subprogram |
8005 | (Is_Primitive : out Boolean; | |
8006 | Is_Overriding : Boolean := False); | |
8007 | -- If the subprogram being analyzed is a primitive operation of the type | |
8008 | -- of a formal or result, set the Has_Primitive_Operations flag on the | |
8009 | -- type, and set Is_Primitive to True (otherwise set to False). Set the | |
8010 | -- corresponding flag on the entity itself for later use. | |
8011 | ||
ec4867fa ES |
8012 | procedure Check_Synchronized_Overriding |
8013 | (Def_Id : Entity_Id; | |
ec4867fa ES |
8014 | Overridden_Subp : out Entity_Id); |
8015 | -- First determine if Def_Id is an entry or a subprogram either defined | |
8016 | -- in the scope of a task or protected type, or is a primitive of such | |
8017 | -- a type. Check whether Def_Id overrides a subprogram of an interface | |
8018 | -- implemented by the synchronized type, return the overridden entity | |
8019 | -- or Empty. | |
758c442c | 8020 | |
996ae0b0 RK |
8021 | function Is_Private_Declaration (E : Entity_Id) return Boolean; |
8022 | -- Check that E is declared in the private part of the current package, | |
8023 | -- or in the package body, where it may hide a previous declaration. | |
fbf5a39b | 8024 | -- We can't use In_Private_Part by itself because this flag is also |
996ae0b0 RK |
8025 | -- set when freezing entities, so we must examine the place of the |
8026 | -- declaration in the tree, and recognize wrapper packages as well. | |
8027 | ||
2ddc2000 AC |
8028 | function Is_Overriding_Alias |
8029 | (Old_E : Entity_Id; | |
8030 | New_E : Entity_Id) return Boolean; | |
8031 | -- Check whether new subprogram and old subprogram are both inherited | |
8032 | -- from subprograms that have distinct dispatch table entries. This can | |
8033 | -- occur with derivations from instances with accidental homonyms. | |
8034 | -- The function is conservative given that the converse is only true | |
8035 | -- within instances that contain accidental overloadings. | |
8036 | ||
5d37ba92 ES |
8037 | ------------------------------------ |
8038 | -- Check_For_Primitive_Subprogram -- | |
8039 | ------------------------------------ | |
996ae0b0 | 8040 | |
5d37ba92 ES |
8041 | procedure Check_For_Primitive_Subprogram |
8042 | (Is_Primitive : out Boolean; | |
8043 | Is_Overriding : Boolean := False) | |
ec4867fa | 8044 | is |
996ae0b0 RK |
8045 | Formal : Entity_Id; |
8046 | F_Typ : Entity_Id; | |
07fc65c4 | 8047 | B_Typ : Entity_Id; |
996ae0b0 RK |
8048 | |
8049 | function Visible_Part_Type (T : Entity_Id) return Boolean; | |
8dbd1460 AC |
8050 | -- Returns true if T is declared in the visible part of the current |
8051 | -- package scope; otherwise returns false. Assumes that T is declared | |
8052 | -- in a package. | |
996ae0b0 RK |
8053 | |
8054 | procedure Check_Private_Overriding (T : Entity_Id); | |
8055 | -- Checks that if a primitive abstract subprogram of a visible | |
8dbd1460 AC |
8056 | -- abstract type is declared in a private part, then it must override |
8057 | -- an abstract subprogram declared in the visible part. Also checks | |
8058 | -- that if a primitive function with a controlling result is declared | |
8059 | -- in a private part, then it must override a function declared in | |
8060 | -- the visible part. | |
996ae0b0 RK |
8061 | |
8062 | ------------------------------ | |
8063 | -- Check_Private_Overriding -- | |
8064 | ------------------------------ | |
8065 | ||
8066 | procedure Check_Private_Overriding (T : Entity_Id) is | |
8067 | begin | |
51c16e29 | 8068 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
8069 | and then In_Private_Part (Current_Scope) |
8070 | and then Visible_Part_Type (T) | |
8071 | and then not In_Instance | |
8072 | then | |
f937473f RD |
8073 | if Is_Abstract_Type (T) |
8074 | and then Is_Abstract_Subprogram (S) | |
8075 | and then (not Is_Overriding | |
8dbd1460 | 8076 | or else not Is_Abstract_Subprogram (E)) |
996ae0b0 | 8077 | then |
ed2233dc | 8078 | Error_Msg_N |
19d846a0 RD |
8079 | ("abstract subprograms must be visible " |
8080 | & "(RM 3.9.3(10))!", S); | |
758c442c | 8081 | |
996ae0b0 | 8082 | elsif Ekind (S) = E_Function |
82c80734 | 8083 | and then not Is_Overriding |
996ae0b0 | 8084 | then |
2e79de51 AC |
8085 | if Is_Tagged_Type (T) |
8086 | and then T = Base_Type (Etype (S)) | |
8087 | then | |
8088 | Error_Msg_N | |
8089 | ("private function with tagged result must" | |
8090 | & " override visible-part function", S); | |
8091 | Error_Msg_N | |
8092 | ("\move subprogram to the visible part" | |
8093 | & " (RM 3.9.3(10))", S); | |
8094 | ||
8095 | -- AI05-0073: extend this test to the case of a function | |
8096 | -- with a controlling access result. | |
8097 | ||
8098 | elsif Ekind (Etype (S)) = E_Anonymous_Access_Type | |
8099 | and then Is_Tagged_Type (Designated_Type (Etype (S))) | |
8100 | and then | |
8101 | not Is_Class_Wide_Type (Designated_Type (Etype (S))) | |
dbe945f1 | 8102 | and then Ada_Version >= Ada_2012 |
2e79de51 AC |
8103 | then |
8104 | Error_Msg_N | |
8105 | ("private function with controlling access result " | |
8106 | & "must override visible-part function", S); | |
8107 | Error_Msg_N | |
8108 | ("\move subprogram to the visible part" | |
8109 | & " (RM 3.9.3(10))", S); | |
8110 | end if; | |
996ae0b0 RK |
8111 | end if; |
8112 | end if; | |
8113 | end Check_Private_Overriding; | |
8114 | ||
8115 | ----------------------- | |
8116 | -- Visible_Part_Type -- | |
8117 | ----------------------- | |
8118 | ||
8119 | function Visible_Part_Type (T : Entity_Id) return Boolean is | |
07fc65c4 GB |
8120 | P : constant Node_Id := Unit_Declaration_Node (Scope (T)); |
8121 | N : Node_Id; | |
996ae0b0 RK |
8122 | |
8123 | begin | |
8dbd1460 AC |
8124 | -- If the entity is a private type, then it must be declared in a |
8125 | -- visible part. | |
996ae0b0 RK |
8126 | |
8127 | if Ekind (T) in Private_Kind then | |
8128 | return True; | |
8129 | end if; | |
8130 | ||
8131 | -- Otherwise, we traverse the visible part looking for its | |
8132 | -- corresponding declaration. We cannot use the declaration | |
8133 | -- node directly because in the private part the entity of a | |
8134 | -- private type is the one in the full view, which does not | |
8135 | -- indicate that it is the completion of something visible. | |
8136 | ||
07fc65c4 | 8137 | N := First (Visible_Declarations (Specification (P))); |
996ae0b0 RK |
8138 | while Present (N) loop |
8139 | if Nkind (N) = N_Full_Type_Declaration | |
8140 | and then Present (Defining_Identifier (N)) | |
8141 | and then T = Defining_Identifier (N) | |
8142 | then | |
8143 | return True; | |
8144 | ||
800621e0 RD |
8145 | elsif Nkind_In (N, N_Private_Type_Declaration, |
8146 | N_Private_Extension_Declaration) | |
996ae0b0 RK |
8147 | and then Present (Defining_Identifier (N)) |
8148 | and then T = Full_View (Defining_Identifier (N)) | |
8149 | then | |
8150 | return True; | |
8151 | end if; | |
8152 | ||
8153 | Next (N); | |
8154 | end loop; | |
8155 | ||
8156 | return False; | |
8157 | end Visible_Part_Type; | |
8158 | ||
5d37ba92 | 8159 | -- Start of processing for Check_For_Primitive_Subprogram |
996ae0b0 RK |
8160 | |
8161 | begin | |
5d37ba92 ES |
8162 | Is_Primitive := False; |
8163 | ||
996ae0b0 RK |
8164 | if not Comes_From_Source (S) then |
8165 | null; | |
8166 | ||
5d37ba92 | 8167 | -- If subprogram is at library level, it is not primitive operation |
15ce9ca2 AC |
8168 | |
8169 | elsif Current_Scope = Standard_Standard then | |
8170 | null; | |
8171 | ||
b9b2405f | 8172 | elsif (Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 | 8173 | and then not In_Package_Body (Current_Scope)) |
82c80734 | 8174 | or else Is_Overriding |
996ae0b0 | 8175 | then |
07fc65c4 | 8176 | -- For function, check return type |
996ae0b0 | 8177 | |
07fc65c4 | 8178 | if Ekind (S) = E_Function then |
5d37ba92 ES |
8179 | if Ekind (Etype (S)) = E_Anonymous_Access_Type then |
8180 | F_Typ := Designated_Type (Etype (S)); | |
8181 | else | |
8182 | F_Typ := Etype (S); | |
8183 | end if; | |
8184 | ||
8185 | B_Typ := Base_Type (F_Typ); | |
07fc65c4 | 8186 | |
5d37ba92 ES |
8187 | if Scope (B_Typ) = Current_Scope |
8188 | and then not Is_Class_Wide_Type (B_Typ) | |
8189 | and then not Is_Generic_Type (B_Typ) | |
8190 | then | |
8191 | Is_Primitive := True; | |
07fc65c4 | 8192 | Set_Has_Primitive_Operations (B_Typ); |
5d37ba92 | 8193 | Set_Is_Primitive (S); |
07fc65c4 GB |
8194 | Check_Private_Overriding (B_Typ); |
8195 | end if; | |
996ae0b0 RK |
8196 | end if; |
8197 | ||
07fc65c4 | 8198 | -- For all subprograms, check formals |
996ae0b0 | 8199 | |
07fc65c4 | 8200 | Formal := First_Formal (S); |
996ae0b0 RK |
8201 | while Present (Formal) loop |
8202 | if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then | |
8203 | F_Typ := Designated_Type (Etype (Formal)); | |
8204 | else | |
8205 | F_Typ := Etype (Formal); | |
8206 | end if; | |
8207 | ||
07fc65c4 GB |
8208 | B_Typ := Base_Type (F_Typ); |
8209 | ||
ec4867fa ES |
8210 | if Ekind (B_Typ) = E_Access_Subtype then |
8211 | B_Typ := Base_Type (B_Typ); | |
8212 | end if; | |
8213 | ||
5d37ba92 ES |
8214 | if Scope (B_Typ) = Current_Scope |
8215 | and then not Is_Class_Wide_Type (B_Typ) | |
8216 | and then not Is_Generic_Type (B_Typ) | |
8217 | then | |
8218 | Is_Primitive := True; | |
8219 | Set_Is_Primitive (S); | |
07fc65c4 GB |
8220 | Set_Has_Primitive_Operations (B_Typ); |
8221 | Check_Private_Overriding (B_Typ); | |
996ae0b0 RK |
8222 | end if; |
8223 | ||
8224 | Next_Formal (Formal); | |
8225 | end loop; | |
996ae0b0 | 8226 | end if; |
5d37ba92 ES |
8227 | end Check_For_Primitive_Subprogram; |
8228 | ||
8229 | ----------------------------------- | |
8230 | -- Check_Synchronized_Overriding -- | |
8231 | ----------------------------------- | |
8232 | ||
8233 | procedure Check_Synchronized_Overriding | |
8234 | (Def_Id : Entity_Id; | |
5d37ba92 ES |
8235 | Overridden_Subp : out Entity_Id) |
8236 | is | |
5d37ba92 ES |
8237 | Ifaces_List : Elist_Id; |
8238 | In_Scope : Boolean; | |
8239 | Typ : Entity_Id; | |
8240 | ||
8aa15e3b JM |
8241 | function Matches_Prefixed_View_Profile |
8242 | (Prim_Params : List_Id; | |
8243 | Iface_Params : List_Id) return Boolean; | |
8244 | -- Determine whether a subprogram's parameter profile Prim_Params | |
8245 | -- matches that of a potentially overridden interface subprogram | |
8246 | -- Iface_Params. Also determine if the type of first parameter of | |
8247 | -- Iface_Params is an implemented interface. | |
8248 | ||
8aa15e3b JM |
8249 | ----------------------------------- |
8250 | -- Matches_Prefixed_View_Profile -- | |
8251 | ----------------------------------- | |
8252 | ||
8253 | function Matches_Prefixed_View_Profile | |
8254 | (Prim_Params : List_Id; | |
8255 | Iface_Params : List_Id) return Boolean | |
8256 | is | |
8257 | Iface_Id : Entity_Id; | |
8258 | Iface_Param : Node_Id; | |
8259 | Iface_Typ : Entity_Id; | |
8260 | Prim_Id : Entity_Id; | |
8261 | Prim_Param : Node_Id; | |
8262 | Prim_Typ : Entity_Id; | |
8263 | ||
8264 | function Is_Implemented | |
8265 | (Ifaces_List : Elist_Id; | |
8266 | Iface : Entity_Id) return Boolean; | |
8267 | -- Determine if Iface is implemented by the current task or | |
8268 | -- protected type. | |
8269 | ||
8270 | -------------------- | |
8271 | -- Is_Implemented -- | |
8272 | -------------------- | |
8273 | ||
8274 | function Is_Implemented | |
8275 | (Ifaces_List : Elist_Id; | |
8276 | Iface : Entity_Id) return Boolean | |
8277 | is | |
8278 | Iface_Elmt : Elmt_Id; | |
8279 | ||
8280 | begin | |
8281 | Iface_Elmt := First_Elmt (Ifaces_List); | |
8282 | while Present (Iface_Elmt) loop | |
8283 | if Node (Iface_Elmt) = Iface then | |
8284 | return True; | |
8285 | end if; | |
8286 | ||
8287 | Next_Elmt (Iface_Elmt); | |
8288 | end loop; | |
8289 | ||
8290 | return False; | |
8291 | end Is_Implemented; | |
8292 | ||
8293 | -- Start of processing for Matches_Prefixed_View_Profile | |
8294 | ||
8295 | begin | |
8296 | Iface_Param := First (Iface_Params); | |
8297 | Iface_Typ := Etype (Defining_Identifier (Iface_Param)); | |
8298 | ||
8299 | if Is_Access_Type (Iface_Typ) then | |
8300 | Iface_Typ := Designated_Type (Iface_Typ); | |
8301 | end if; | |
8302 | ||
8303 | Prim_Param := First (Prim_Params); | |
8304 | ||
8305 | -- The first parameter of the potentially overridden subprogram | |
8306 | -- must be an interface implemented by Prim. | |
8307 | ||
8308 | if not Is_Interface (Iface_Typ) | |
8309 | or else not Is_Implemented (Ifaces_List, Iface_Typ) | |
8310 | then | |
8311 | return False; | |
8312 | end if; | |
8313 | ||
8314 | -- The checks on the object parameters are done, move onto the | |
8315 | -- rest of the parameters. | |
8316 | ||
8317 | if not In_Scope then | |
8318 | Prim_Param := Next (Prim_Param); | |
8319 | end if; | |
8320 | ||
8321 | Iface_Param := Next (Iface_Param); | |
8322 | while Present (Iface_Param) and then Present (Prim_Param) loop | |
8323 | Iface_Id := Defining_Identifier (Iface_Param); | |
8324 | Iface_Typ := Find_Parameter_Type (Iface_Param); | |
8325 | ||
8aa15e3b JM |
8326 | Prim_Id := Defining_Identifier (Prim_Param); |
8327 | Prim_Typ := Find_Parameter_Type (Prim_Param); | |
8328 | ||
15e4986c JM |
8329 | if Ekind (Iface_Typ) = E_Anonymous_Access_Type |
8330 | and then Ekind (Prim_Typ) = E_Anonymous_Access_Type | |
8331 | and then Is_Concurrent_Type (Designated_Type (Prim_Typ)) | |
8332 | then | |
8333 | Iface_Typ := Designated_Type (Iface_Typ); | |
8334 | Prim_Typ := Designated_Type (Prim_Typ); | |
8aa15e3b JM |
8335 | end if; |
8336 | ||
8337 | -- Case of multiple interface types inside a parameter profile | |
8338 | ||
8339 | -- (Obj_Param : in out Iface; ...; Param : Iface) | |
8340 | ||
8341 | -- If the interface type is implemented, then the matching type | |
8342 | -- in the primitive should be the implementing record type. | |
8343 | ||
8344 | if Ekind (Iface_Typ) = E_Record_Type | |
8345 | and then Is_Interface (Iface_Typ) | |
8346 | and then Is_Implemented (Ifaces_List, Iface_Typ) | |
8347 | then | |
8348 | if Prim_Typ /= Typ then | |
8349 | return False; | |
8350 | end if; | |
8351 | ||
8352 | -- The two parameters must be both mode and subtype conformant | |
8353 | ||
8354 | elsif Ekind (Iface_Id) /= Ekind (Prim_Id) | |
8355 | or else not | |
8356 | Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant) | |
8357 | then | |
8358 | return False; | |
8359 | end if; | |
8360 | ||
8361 | Next (Iface_Param); | |
8362 | Next (Prim_Param); | |
8363 | end loop; | |
8364 | ||
8365 | -- One of the two lists contains more parameters than the other | |
8366 | ||
8367 | if Present (Iface_Param) or else Present (Prim_Param) then | |
8368 | return False; | |
8369 | end if; | |
8370 | ||
8371 | return True; | |
8372 | end Matches_Prefixed_View_Profile; | |
8373 | ||
8374 | -- Start of processing for Check_Synchronized_Overriding | |
8375 | ||
5d37ba92 ES |
8376 | begin |
8377 | Overridden_Subp := Empty; | |
8378 | ||
8aa15e3b JM |
8379 | -- Def_Id must be an entry or a subprogram. We should skip predefined |
8380 | -- primitives internally generated by the frontend; however at this | |
8381 | -- stage predefined primitives are still not fully decorated. As a | |
8382 | -- minor optimization we skip here internally generated subprograms. | |
5d37ba92 | 8383 | |
8aa15e3b JM |
8384 | if (Ekind (Def_Id) /= E_Entry |
8385 | and then Ekind (Def_Id) /= E_Function | |
8386 | and then Ekind (Def_Id) /= E_Procedure) | |
8387 | or else not Comes_From_Source (Def_Id) | |
5d37ba92 ES |
8388 | then |
8389 | return; | |
8390 | end if; | |
8391 | ||
8392 | -- Search for the concurrent declaration since it contains the list | |
8393 | -- of all implemented interfaces. In this case, the subprogram is | |
8394 | -- declared within the scope of a protected or a task type. | |
8395 | ||
8396 | if Present (Scope (Def_Id)) | |
8397 | and then Is_Concurrent_Type (Scope (Def_Id)) | |
8398 | and then not Is_Generic_Actual_Type (Scope (Def_Id)) | |
8399 | then | |
8400 | Typ := Scope (Def_Id); | |
8401 | In_Scope := True; | |
8402 | ||
8aa15e3b | 8403 | -- The enclosing scope is not a synchronized type and the subprogram |
4adf3c50 | 8404 | -- has no formals. |
8aa15e3b JM |
8405 | |
8406 | elsif No (First_Formal (Def_Id)) then | |
8407 | return; | |
5d37ba92 | 8408 | |
8aa15e3b | 8409 | -- The subprogram has formals and hence it may be a primitive of a |
4adf3c50 | 8410 | -- concurrent type. |
5d37ba92 | 8411 | |
8aa15e3b JM |
8412 | else |
8413 | Typ := Etype (First_Formal (Def_Id)); | |
8414 | ||
8415 | if Is_Access_Type (Typ) then | |
8416 | Typ := Directly_Designated_Type (Typ); | |
8c3dd7a8 JM |
8417 | end if; |
8418 | ||
8aa15e3b JM |
8419 | if Is_Concurrent_Type (Typ) |
8420 | and then not Is_Generic_Actual_Type (Typ) | |
5d37ba92 | 8421 | then |
5d37ba92 ES |
8422 | In_Scope := False; |
8423 | ||
8424 | -- This case occurs when the concurrent type is declared within | |
8425 | -- a generic unit. As a result the corresponding record has been | |
8426 | -- built and used as the type of the first formal, we just have | |
8427 | -- to retrieve the corresponding concurrent type. | |
8428 | ||
8aa15e3b | 8429 | elsif Is_Concurrent_Record_Type (Typ) |
dd54644b | 8430 | and then not Is_Class_Wide_Type (Typ) |
8aa15e3b | 8431 | and then Present (Corresponding_Concurrent_Type (Typ)) |
5d37ba92 | 8432 | then |
8aa15e3b | 8433 | Typ := Corresponding_Concurrent_Type (Typ); |
5d37ba92 ES |
8434 | In_Scope := False; |
8435 | ||
8436 | else | |
8437 | return; | |
8438 | end if; | |
8aa15e3b JM |
8439 | end if; |
8440 | ||
8441 | -- There is no overriding to check if is an inherited operation in a | |
8442 | -- type derivation on for a generic actual. | |
8443 | ||
8444 | Collect_Interfaces (Typ, Ifaces_List); | |
8445 | ||
8446 | if Is_Empty_Elmt_List (Ifaces_List) then | |
5d37ba92 ES |
8447 | return; |
8448 | end if; | |
8449 | ||
8aa15e3b JM |
8450 | -- Determine whether entry or subprogram Def_Id overrides a primitive |
8451 | -- operation that belongs to one of the interfaces in Ifaces_List. | |
5d37ba92 | 8452 | |
8aa15e3b JM |
8453 | declare |
8454 | Candidate : Entity_Id := Empty; | |
8455 | Hom : Entity_Id := Empty; | |
8456 | Iface_Typ : Entity_Id; | |
8457 | Subp : Entity_Id := Empty; | |
8458 | ||
8459 | begin | |
4adf3c50 | 8460 | -- Traverse the homonym chain, looking for a potentially |
8aa15e3b JM |
8461 | -- overridden subprogram that belongs to an implemented |
8462 | -- interface. | |
8463 | ||
8464 | Hom := Current_Entity_In_Scope (Def_Id); | |
8465 | while Present (Hom) loop | |
8466 | Subp := Hom; | |
8467 | ||
15e4986c JM |
8468 | if Subp = Def_Id |
8469 | or else not Is_Overloadable (Subp) | |
8470 | or else not Is_Primitive (Subp) | |
8471 | or else not Is_Dispatching_Operation (Subp) | |
79afa047 | 8472 | or else not Present (Find_Dispatching_Type (Subp)) |
15e4986c | 8473 | or else not Is_Interface (Find_Dispatching_Type (Subp)) |
8aa15e3b | 8474 | then |
15e4986c | 8475 | null; |
8aa15e3b | 8476 | |
15e4986c | 8477 | -- Entries and procedures can override abstract or null |
4adf3c50 | 8478 | -- interface procedures. |
8aa15e3b | 8479 | |
15e4986c JM |
8480 | elsif (Ekind (Def_Id) = E_Procedure |
8481 | or else Ekind (Def_Id) = E_Entry) | |
8aa15e3b | 8482 | and then Ekind (Subp) = E_Procedure |
8aa15e3b JM |
8483 | and then Matches_Prefixed_View_Profile |
8484 | (Parameter_Specifications (Parent (Def_Id)), | |
8485 | Parameter_Specifications (Parent (Subp))) | |
8486 | then | |
8487 | Candidate := Subp; | |
8488 | ||
15e4986c JM |
8489 | -- For an overridden subprogram Subp, check whether the mode |
8490 | -- of its first parameter is correct depending on the kind | |
8491 | -- of synchronized type. | |
8aa15e3b | 8492 | |
15e4986c JM |
8493 | declare |
8494 | Formal : constant Node_Id := First_Formal (Candidate); | |
8495 | ||
8496 | begin | |
8497 | -- In order for an entry or a protected procedure to | |
8498 | -- override, the first parameter of the overridden | |
8499 | -- routine must be of mode "out", "in out" or | |
8500 | -- access-to-variable. | |
8501 | ||
8502 | if (Ekind (Candidate) = E_Entry | |
8503 | or else Ekind (Candidate) = E_Procedure) | |
8504 | and then Is_Protected_Type (Typ) | |
8505 | and then Ekind (Formal) /= E_In_Out_Parameter | |
8506 | and then Ekind (Formal) /= E_Out_Parameter | |
8507 | and then Nkind (Parameter_Type (Parent (Formal))) | |
8508 | /= N_Access_Definition | |
8509 | then | |
8510 | null; | |
8511 | ||
8512 | -- All other cases are OK since a task entry or routine | |
8513 | -- does not have a restriction on the mode of the first | |
8514 | -- parameter of the overridden interface routine. | |
8515 | ||
8516 | else | |
8517 | Overridden_Subp := Candidate; | |
8518 | return; | |
8519 | end if; | |
8520 | end; | |
8aa15e3b JM |
8521 | |
8522 | -- Functions can override abstract interface functions | |
8523 | ||
8524 | elsif Ekind (Def_Id) = E_Function | |
8525 | and then Ekind (Subp) = E_Function | |
8aa15e3b JM |
8526 | and then Matches_Prefixed_View_Profile |
8527 | (Parameter_Specifications (Parent (Def_Id)), | |
8528 | Parameter_Specifications (Parent (Subp))) | |
8529 | and then Etype (Result_Definition (Parent (Def_Id))) = | |
8530 | Etype (Result_Definition (Parent (Subp))) | |
8531 | then | |
8532 | Overridden_Subp := Subp; | |
8533 | return; | |
8534 | end if; | |
8535 | ||
8536 | Hom := Homonym (Hom); | |
8537 | end loop; | |
8538 | ||
4adf3c50 AC |
8539 | -- After examining all candidates for overriding, we are left with |
8540 | -- the best match which is a mode incompatible interface routine. | |
8541 | -- Do not emit an error if the Expander is active since this error | |
8542 | -- will be detected later on after all concurrent types are | |
8543 | -- expanded and all wrappers are built. This check is meant for | |
8544 | -- spec-only compilations. | |
8aa15e3b | 8545 | |
4adf3c50 | 8546 | if Present (Candidate) and then not Expander_Active then |
8aa15e3b JM |
8547 | Iface_Typ := |
8548 | Find_Parameter_Type (Parent (First_Formal (Candidate))); | |
8549 | ||
4adf3c50 AC |
8550 | -- Def_Id is primitive of a protected type, declared inside the |
8551 | -- type, and the candidate is primitive of a limited or | |
8552 | -- synchronized interface. | |
8aa15e3b JM |
8553 | |
8554 | if In_Scope | |
8555 | and then Is_Protected_Type (Typ) | |
8556 | and then | |
8557 | (Is_Limited_Interface (Iface_Typ) | |
c199ccf7 AC |
8558 | or else Is_Protected_Interface (Iface_Typ) |
8559 | or else Is_Synchronized_Interface (Iface_Typ) | |
8560 | or else Is_Task_Interface (Iface_Typ)) | |
8aa15e3b | 8561 | then |
dd54644b | 8562 | Error_Msg_PT (Parent (Typ), Candidate); |
8aa15e3b | 8563 | end if; |
5d37ba92 | 8564 | end if; |
8aa15e3b JM |
8565 | |
8566 | Overridden_Subp := Candidate; | |
8567 | return; | |
8568 | end; | |
5d37ba92 ES |
8569 | end Check_Synchronized_Overriding; |
8570 | ||
8571 | ---------------------------- | |
8572 | -- Is_Private_Declaration -- | |
8573 | ---------------------------- | |
8574 | ||
8575 | function Is_Private_Declaration (E : Entity_Id) return Boolean is | |
8576 | Priv_Decls : List_Id; | |
8577 | Decl : constant Node_Id := Unit_Declaration_Node (E); | |
8578 | ||
8579 | begin | |
8580 | if Is_Package_Or_Generic_Package (Current_Scope) | |
8581 | and then In_Private_Part (Current_Scope) | |
8582 | then | |
8583 | Priv_Decls := | |
a4901c08 AC |
8584 | Private_Declarations |
8585 | (Specification (Unit_Declaration_Node (Current_Scope))); | |
5d37ba92 ES |
8586 | |
8587 | return In_Package_Body (Current_Scope) | |
8588 | or else | |
8589 | (Is_List_Member (Decl) | |
a4901c08 | 8590 | and then List_Containing (Decl) = Priv_Decls) |
5d37ba92 | 8591 | or else (Nkind (Parent (Decl)) = N_Package_Specification |
a4901c08 AC |
8592 | and then not |
8593 | Is_Compilation_Unit | |
8594 | (Defining_Entity (Parent (Decl))) | |
8595 | and then List_Containing (Parent (Parent (Decl))) = | |
8596 | Priv_Decls); | |
5d37ba92 ES |
8597 | else |
8598 | return False; | |
8599 | end if; | |
8600 | end Is_Private_Declaration; | |
996ae0b0 | 8601 | |
2ddc2000 AC |
8602 | -------------------------- |
8603 | -- Is_Overriding_Alias -- | |
8604 | -------------------------- | |
8605 | ||
8606 | function Is_Overriding_Alias | |
8607 | (Old_E : Entity_Id; | |
8608 | New_E : Entity_Id) return Boolean | |
8609 | is | |
8610 | AO : constant Entity_Id := Alias (Old_E); | |
8611 | AN : constant Entity_Id := Alias (New_E); | |
8612 | ||
8613 | begin | |
8614 | return Scope (AO) /= Scope (AN) | |
8615 | or else No (DTC_Entity (AO)) | |
8616 | or else No (DTC_Entity (AN)) | |
8617 | or else DT_Position (AO) = DT_Position (AN); | |
8618 | end Is_Overriding_Alias; | |
8619 | ||
996ae0b0 RK |
8620 | -- Start of processing for New_Overloaded_Entity |
8621 | ||
8622 | begin | |
fbf5a39b AC |
8623 | -- We need to look for an entity that S may override. This must be a |
8624 | -- homonym in the current scope, so we look for the first homonym of | |
8625 | -- S in the current scope as the starting point for the search. | |
8626 | ||
8627 | E := Current_Entity_In_Scope (S); | |
8628 | ||
947430d5 AC |
8629 | -- Ada 2005 (AI-251): Derivation of abstract interface primitives. |
8630 | -- They are directly added to the list of primitive operations of | |
8631 | -- Derived_Type, unless this is a rederivation in the private part | |
8632 | -- of an operation that was already derived in the visible part of | |
8633 | -- the current package. | |
8634 | ||
0791fbe9 | 8635 | if Ada_Version >= Ada_2005 |
947430d5 AC |
8636 | and then Present (Derived_Type) |
8637 | and then Present (Alias (S)) | |
8638 | and then Is_Dispatching_Operation (Alias (S)) | |
8639 | and then Present (Find_Dispatching_Type (Alias (S))) | |
8640 | and then Is_Interface (Find_Dispatching_Type (Alias (S))) | |
8641 | then | |
8642 | -- For private types, when the full-view is processed we propagate to | |
8643 | -- the full view the non-overridden entities whose attribute "alias" | |
8644 | -- references an interface primitive. These entities were added by | |
8645 | -- Derive_Subprograms to ensure that interface primitives are | |
8646 | -- covered. | |
8647 | ||
8648 | -- Inside_Freeze_Actions is non zero when S corresponds with an | |
8649 | -- internal entity that links an interface primitive with its | |
8650 | -- covering primitive through attribute Interface_Alias (see | |
4adf3c50 | 8651 | -- Add_Internal_Interface_Entities). |
947430d5 AC |
8652 | |
8653 | if Inside_Freezing_Actions = 0 | |
8654 | and then Is_Package_Or_Generic_Package (Current_Scope) | |
8655 | and then In_Private_Part (Current_Scope) | |
8656 | and then Nkind (Parent (E)) = N_Private_Extension_Declaration | |
8657 | and then Nkind (Parent (S)) = N_Full_Type_Declaration | |
8658 | and then Full_View (Defining_Identifier (Parent (E))) | |
8659 | = Defining_Identifier (Parent (S)) | |
8660 | and then Alias (E) = Alias (S) | |
8661 | then | |
8662 | Check_Operation_From_Private_View (S, E); | |
8663 | Set_Is_Dispatching_Operation (S); | |
8664 | ||
8665 | -- Common case | |
8666 | ||
8667 | else | |
8668 | Enter_Overloaded_Entity (S); | |
8669 | Check_Dispatching_Operation (S, Empty); | |
8670 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); | |
8671 | end if; | |
8672 | ||
8673 | return; | |
8674 | end if; | |
8675 | ||
fbf5a39b AC |
8676 | -- If there is no homonym then this is definitely not overriding |
8677 | ||
996ae0b0 RK |
8678 | if No (E) then |
8679 | Enter_Overloaded_Entity (S); | |
8680 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 8681 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
996ae0b0 | 8682 | |
ec4867fa ES |
8683 | -- If subprogram has an explicit declaration, check whether it |
8684 | -- has an overriding indicator. | |
758c442c | 8685 | |
ec4867fa | 8686 | if Comes_From_Source (S) then |
8aa15e3b | 8687 | Check_Synchronized_Overriding (S, Overridden_Subp); |
ea034236 AC |
8688 | |
8689 | -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then | |
8690 | -- it may have overridden some hidden inherited primitive. Update | |
308e6f3a | 8691 | -- Overridden_Subp to avoid spurious errors when checking the |
ea034236 AC |
8692 | -- overriding indicator. |
8693 | ||
8694 | if Ada_Version >= Ada_2012 | |
8695 | and then No (Overridden_Subp) | |
8696 | and then Is_Dispatching_Operation (S) | |
038140ed | 8697 | and then Present (Overridden_Operation (S)) |
ea034236 AC |
8698 | then |
8699 | Overridden_Subp := Overridden_Operation (S); | |
8700 | end if; | |
8701 | ||
5d37ba92 ES |
8702 | Check_Overriding_Indicator |
8703 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
758c442c GD |
8704 | end if; |
8705 | ||
fbf5a39b AC |
8706 | -- If there is a homonym that is not overloadable, then we have an |
8707 | -- error, except for the special cases checked explicitly below. | |
8708 | ||
996ae0b0 RK |
8709 | elsif not Is_Overloadable (E) then |
8710 | ||
8711 | -- Check for spurious conflict produced by a subprogram that has the | |
8712 | -- same name as that of the enclosing generic package. The conflict | |
8713 | -- occurs within an instance, between the subprogram and the renaming | |
8714 | -- declaration for the package. After the subprogram, the package | |
8715 | -- renaming declaration becomes hidden. | |
8716 | ||
8717 | if Ekind (E) = E_Package | |
8718 | and then Present (Renamed_Object (E)) | |
8719 | and then Renamed_Object (E) = Current_Scope | |
8720 | and then Nkind (Parent (Renamed_Object (E))) = | |
8721 | N_Package_Specification | |
8722 | and then Present (Generic_Parent (Parent (Renamed_Object (E)))) | |
8723 | then | |
8724 | Set_Is_Hidden (E); | |
8725 | Set_Is_Immediately_Visible (E, False); | |
8726 | Enter_Overloaded_Entity (S); | |
8727 | Set_Homonym (S, Homonym (E)); | |
8728 | Check_Dispatching_Operation (S, Empty); | |
5d37ba92 | 8729 | Check_Overriding_Indicator (S, Empty, Is_Primitive => False); |
996ae0b0 RK |
8730 | |
8731 | -- If the subprogram is implicit it is hidden by the previous | |
82c80734 RD |
8732 | -- declaration. However if it is dispatching, it must appear in the |
8733 | -- dispatch table anyway, because it can be dispatched to even if it | |
8734 | -- cannot be called directly. | |
996ae0b0 | 8735 | |
4adf3c50 | 8736 | elsif Present (Alias (S)) and then not Comes_From_Source (S) then |
996ae0b0 RK |
8737 | Set_Scope (S, Current_Scope); |
8738 | ||
8739 | if Is_Dispatching_Operation (Alias (S)) then | |
8740 | Check_Dispatching_Operation (S, Empty); | |
8741 | end if; | |
8742 | ||
8743 | return; | |
8744 | ||
8745 | else | |
8746 | Error_Msg_Sloc := Sloc (E); | |
996ae0b0 | 8747 | |
f3d57416 | 8748 | -- Generate message, with useful additional warning if in generic |
996ae0b0 RK |
8749 | |
8750 | if Is_Generic_Unit (E) then | |
5d37ba92 ES |
8751 | Error_Msg_N ("previous generic unit cannot be overloaded", S); |
8752 | Error_Msg_N ("\& conflicts with declaration#", S); | |
8753 | else | |
8754 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
8755 | end if; |
8756 | ||
8757 | return; | |
8758 | end if; | |
8759 | ||
fbf5a39b AC |
8760 | -- E exists and is overloadable |
8761 | ||
996ae0b0 | 8762 | else |
8aa15e3b | 8763 | Check_Synchronized_Overriding (S, Overridden_Subp); |
758c442c | 8764 | |
82c80734 RD |
8765 | -- Loop through E and its homonyms to determine if any of them is |
8766 | -- the candidate for overriding by S. | |
996ae0b0 RK |
8767 | |
8768 | while Present (E) loop | |
fbf5a39b AC |
8769 | |
8770 | -- Definitely not interesting if not in the current scope | |
8771 | ||
996ae0b0 RK |
8772 | if Scope (E) /= Current_Scope then |
8773 | null; | |
8774 | ||
25ebc085 AC |
8775 | -- Ada 2012 (AI05-0165): For internally generated bodies of |
8776 | -- null procedures locate the internally generated spec. We | |
8777 | -- enforce mode conformance since a tagged type may inherit | |
8778 | -- from interfaces several null primitives which differ only | |
8779 | -- in the mode of the formals. | |
8780 | ||
8781 | elsif not Comes_From_Source (S) | |
8782 | and then Is_Null_Procedure (S) | |
8783 | and then not Mode_Conformant (E, S) | |
8784 | then | |
8785 | null; | |
8786 | ||
fbf5a39b AC |
8787 | -- Check if we have type conformance |
8788 | ||
ec4867fa | 8789 | elsif Type_Conformant (E, S) then |
c8ef728f | 8790 | |
82c80734 RD |
8791 | -- If the old and new entities have the same profile and one |
8792 | -- is not the body of the other, then this is an error, unless | |
8793 | -- one of them is implicitly declared. | |
996ae0b0 RK |
8794 | |
8795 | -- There are some cases when both can be implicit, for example | |
8796 | -- when both a literal and a function that overrides it are | |
f3d57416 | 8797 | -- inherited in a derivation, or when an inherited operation |
ec4867fa | 8798 | -- of a tagged full type overrides the inherited operation of |
f3d57416 | 8799 | -- a private extension. Ada 83 had a special rule for the |
885c4871 | 8800 | -- literal case. In Ada 95, the later implicit operation hides |
82c80734 RD |
8801 | -- the former, and the literal is always the former. In the |
8802 | -- odd case where both are derived operations declared at the | |
8803 | -- same point, both operations should be declared, and in that | |
8804 | -- case we bypass the following test and proceed to the next | |
df46b832 AC |
8805 | -- part. This can only occur for certain obscure cases in |
8806 | -- instances, when an operation on a type derived from a formal | |
8807 | -- private type does not override a homograph inherited from | |
8808 | -- the actual. In subsequent derivations of such a type, the | |
8809 | -- DT positions of these operations remain distinct, if they | |
8810 | -- have been set. | |
996ae0b0 RK |
8811 | |
8812 | if Present (Alias (S)) | |
8813 | and then (No (Alias (E)) | |
8814 | or else Comes_From_Source (E) | |
2ddc2000 | 8815 | or else Is_Abstract_Subprogram (S) |
df46b832 AC |
8816 | or else |
8817 | (Is_Dispatching_Operation (E) | |
2ddc2000 | 8818 | and then Is_Overriding_Alias (E, S))) |
df46b832 | 8819 | and then Ekind (E) /= E_Enumeration_Literal |
996ae0b0 | 8820 | then |
82c80734 RD |
8821 | -- When an derived operation is overloaded it may be due to |
8822 | -- the fact that the full view of a private extension | |
996ae0b0 RK |
8823 | -- re-inherits. It has to be dealt with. |
8824 | ||
e660dbf7 | 8825 | if Is_Package_Or_Generic_Package (Current_Scope) |
996ae0b0 RK |
8826 | and then In_Private_Part (Current_Scope) |
8827 | then | |
8828 | Check_Operation_From_Private_View (S, E); | |
8829 | end if; | |
8830 | ||
038140ed AC |
8831 | -- In any case the implicit operation remains hidden by the |
8832 | -- existing declaration, which is overriding. Indicate that | |
8833 | -- E overrides the operation from which S is inherited. | |
996ae0b0 | 8834 | |
038140ed AC |
8835 | if Present (Alias (S)) then |
8836 | Set_Overridden_Operation (E, Alias (S)); | |
8837 | else | |
8838 | Set_Overridden_Operation (E, S); | |
8839 | end if; | |
758c442c GD |
8840 | |
8841 | if Comes_From_Source (E) then | |
5d37ba92 | 8842 | Check_Overriding_Indicator (E, S, Is_Primitive => False); |
758c442c GD |
8843 | end if; |
8844 | ||
996ae0b0 RK |
8845 | return; |
8846 | ||
26a43556 AC |
8847 | -- Within an instance, the renaming declarations for actual |
8848 | -- subprograms may become ambiguous, but they do not hide each | |
8849 | -- other. | |
996ae0b0 RK |
8850 | |
8851 | elsif Ekind (E) /= E_Entry | |
8852 | and then not Comes_From_Source (E) | |
8853 | and then not Is_Generic_Instance (E) | |
8854 | and then (Present (Alias (E)) | |
8855 | or else Is_Intrinsic_Subprogram (E)) | |
8856 | and then (not In_Instance | |
8857 | or else No (Parent (E)) | |
8858 | or else Nkind (Unit_Declaration_Node (E)) /= | |
8dbd1460 | 8859 | N_Subprogram_Renaming_Declaration) |
996ae0b0 | 8860 | then |
26a43556 AC |
8861 | -- A subprogram child unit is not allowed to override an |
8862 | -- inherited subprogram (10.1.1(20)). | |
996ae0b0 RK |
8863 | |
8864 | if Is_Child_Unit (S) then | |
8865 | Error_Msg_N | |
8866 | ("child unit overrides inherited subprogram in parent", | |
8867 | S); | |
8868 | return; | |
8869 | end if; | |
8870 | ||
8871 | if Is_Non_Overriding_Operation (E, S) then | |
8872 | Enter_Overloaded_Entity (S); | |
8dbd1460 | 8873 | |
c8ef728f | 8874 | if No (Derived_Type) |
996ae0b0 RK |
8875 | or else Is_Tagged_Type (Derived_Type) |
8876 | then | |
8877 | Check_Dispatching_Operation (S, Empty); | |
8878 | end if; | |
8879 | ||
8880 | return; | |
8881 | end if; | |
8882 | ||
8883 | -- E is a derived operation or an internal operator which | |
8884 | -- is being overridden. Remove E from further visibility. | |
8885 | -- Furthermore, if E is a dispatching operation, it must be | |
8886 | -- replaced in the list of primitive operations of its type | |
8887 | -- (see Override_Dispatching_Operation). | |
8888 | ||
ec4867fa | 8889 | Overridden_Subp := E; |
758c442c | 8890 | |
996ae0b0 RK |
8891 | declare |
8892 | Prev : Entity_Id; | |
8893 | ||
8894 | begin | |
8895 | Prev := First_Entity (Current_Scope); | |
996ae0b0 RK |
8896 | while Present (Prev) |
8897 | and then Next_Entity (Prev) /= E | |
8898 | loop | |
8899 | Next_Entity (Prev); | |
8900 | end loop; | |
8901 | ||
8902 | -- It is possible for E to be in the current scope and | |
8903 | -- yet not in the entity chain. This can only occur in a | |
8904 | -- generic context where E is an implicit concatenation | |
8905 | -- in the formal part, because in a generic body the | |
8906 | -- entity chain starts with the formals. | |
8907 | ||
8908 | pragma Assert | |
8909 | (Present (Prev) or else Chars (E) = Name_Op_Concat); | |
8910 | ||
8911 | -- E must be removed both from the entity_list of the | |
8912 | -- current scope, and from the visibility chain | |
8913 | ||
8914 | if Debug_Flag_E then | |
8915 | Write_Str ("Override implicit operation "); | |
8916 | Write_Int (Int (E)); | |
8917 | Write_Eol; | |
8918 | end if; | |
8919 | ||
8920 | -- If E is a predefined concatenation, it stands for four | |
8921 | -- different operations. As a result, a single explicit | |
8922 | -- declaration does not hide it. In a possible ambiguous | |
8923 | -- situation, Disambiguate chooses the user-defined op, | |
8924 | -- so it is correct to retain the previous internal one. | |
8925 | ||
8926 | if Chars (E) /= Name_Op_Concat | |
8927 | or else Ekind (E) /= E_Operator | |
8928 | then | |
8929 | -- For nondispatching derived operations that are | |
8930 | -- overridden by a subprogram declared in the private | |
8dbd1460 AC |
8931 | -- part of a package, we retain the derived subprogram |
8932 | -- but mark it as not immediately visible. If the | |
8933 | -- derived operation was declared in the visible part | |
8934 | -- then this ensures that it will still be visible | |
8935 | -- outside the package with the proper signature | |
8936 | -- (calls from outside must also be directed to this | |
8937 | -- version rather than the overriding one, unlike the | |
8938 | -- dispatching case). Calls from inside the package | |
8939 | -- will still resolve to the overriding subprogram | |
8940 | -- since the derived one is marked as not visible | |
8941 | -- within the package. | |
996ae0b0 RK |
8942 | |
8943 | -- If the private operation is dispatching, we achieve | |
8944 | -- the overriding by keeping the implicit operation | |
9865d858 | 8945 | -- but setting its alias to be the overriding one. In |
996ae0b0 RK |
8946 | -- this fashion the proper body is executed in all |
8947 | -- cases, but the original signature is used outside | |
8948 | -- of the package. | |
8949 | ||
8950 | -- If the overriding is not in the private part, we | |
8951 | -- remove the implicit operation altogether. | |
8952 | ||
8953 | if Is_Private_Declaration (S) then | |
996ae0b0 RK |
8954 | if not Is_Dispatching_Operation (E) then |
8955 | Set_Is_Immediately_Visible (E, False); | |
8956 | else | |
e895b435 | 8957 | -- Work done in Override_Dispatching_Operation, |
a46cde68 | 8958 | -- so nothing else needs to be done here. |
996ae0b0 RK |
8959 | |
8960 | null; | |
8961 | end if; | |
996ae0b0 | 8962 | |
fbf5a39b AC |
8963 | else |
8964 | -- Find predecessor of E in Homonym chain | |
996ae0b0 RK |
8965 | |
8966 | if E = Current_Entity (E) then | |
8967 | Prev_Vis := Empty; | |
8968 | else | |
8969 | Prev_Vis := Current_Entity (E); | |
8970 | while Homonym (Prev_Vis) /= E loop | |
8971 | Prev_Vis := Homonym (Prev_Vis); | |
8972 | end loop; | |
8973 | end if; | |
8974 | ||
8975 | if Prev_Vis /= Empty then | |
8976 | ||
8977 | -- Skip E in the visibility chain | |
8978 | ||
8979 | Set_Homonym (Prev_Vis, Homonym (E)); | |
8980 | ||
8981 | else | |
8982 | Set_Name_Entity_Id (Chars (E), Homonym (E)); | |
8983 | end if; | |
8984 | ||
8985 | Set_Next_Entity (Prev, Next_Entity (E)); | |
8986 | ||
8987 | if No (Next_Entity (Prev)) then | |
8988 | Set_Last_Entity (Current_Scope, Prev); | |
8989 | end if; | |
996ae0b0 RK |
8990 | end if; |
8991 | end if; | |
8992 | ||
8993 | Enter_Overloaded_Entity (S); | |
1c1289e7 AC |
8994 | |
8995 | -- For entities generated by Derive_Subprograms the | |
8996 | -- overridden operation is the inherited primitive | |
8997 | -- (which is available through the attribute alias). | |
8998 | ||
8999 | if not (Comes_From_Source (E)) | |
9000 | and then Is_Dispatching_Operation (E) | |
f9673bb0 AC |
9001 | and then Find_Dispatching_Type (E) = |
9002 | Find_Dispatching_Type (S) | |
1c1289e7 AC |
9003 | and then Present (Alias (E)) |
9004 | and then Comes_From_Source (Alias (E)) | |
9005 | then | |
9006 | Set_Overridden_Operation (S, Alias (E)); | |
2fe829ae | 9007 | |
6320f5e1 AC |
9008 | -- Normal case of setting entity as overridden |
9009 | ||
9010 | -- Note: Static_Initialization and Overridden_Operation | |
9011 | -- attributes use the same field in subprogram entities. | |
9012 | -- Static_Initialization is only defined for internal | |
9013 | -- initialization procedures, where Overridden_Operation | |
9014 | -- is irrelevant. Therefore the setting of this attribute | |
9015 | -- must check whether the target is an init_proc. | |
9016 | ||
2fe829ae | 9017 | elsif not Is_Init_Proc (S) then |
1c1289e7 AC |
9018 | Set_Overridden_Operation (S, E); |
9019 | end if; | |
9020 | ||
5d37ba92 | 9021 | Check_Overriding_Indicator (S, E, Is_Primitive => True); |
996ae0b0 | 9022 | |
fc53fe76 | 9023 | -- If S is a user-defined subprogram or a null procedure |
38ef8ebe AC |
9024 | -- expanded to override an inherited null procedure, or a |
9025 | -- predefined dispatching primitive then indicate that E | |
038140ed | 9026 | -- overrides the operation from which S is inherited. |
fc53fe76 AC |
9027 | |
9028 | if Comes_From_Source (S) | |
9029 | or else | |
9030 | (Present (Parent (S)) | |
9031 | and then | |
9032 | Nkind (Parent (S)) = N_Procedure_Specification | |
9033 | and then | |
9034 | Null_Present (Parent (S))) | |
38ef8ebe AC |
9035 | or else |
9036 | (Present (Alias (E)) | |
f16e8df9 RD |
9037 | and then |
9038 | Is_Predefined_Dispatching_Operation (Alias (E))) | |
fc53fe76 | 9039 | then |
c8ef728f | 9040 | if Present (Alias (E)) then |
41251c60 | 9041 | Set_Overridden_Operation (S, Alias (E)); |
41251c60 JM |
9042 | end if; |
9043 | end if; | |
9044 | ||
996ae0b0 | 9045 | if Is_Dispatching_Operation (E) then |
fbf5a39b | 9046 | |
82c80734 | 9047 | -- An overriding dispatching subprogram inherits the |
f9673bb0 | 9048 | -- convention of the overridden subprogram (AI-117). |
996ae0b0 RK |
9049 | |
9050 | Set_Convention (S, Convention (E)); | |
41251c60 JM |
9051 | Check_Dispatching_Operation (S, E); |
9052 | ||
996ae0b0 RK |
9053 | else |
9054 | Check_Dispatching_Operation (S, Empty); | |
9055 | end if; | |
9056 | ||
5d37ba92 ES |
9057 | Check_For_Primitive_Subprogram |
9058 | (Is_Primitive_Subp, Is_Overriding => True); | |
996ae0b0 RK |
9059 | goto Check_Inequality; |
9060 | end; | |
9061 | ||
9062 | -- Apparent redeclarations in instances can occur when two | |
9063 | -- formal types get the same actual type. The subprograms in | |
9064 | -- in the instance are legal, even if not callable from the | |
9065 | -- outside. Calls from within are disambiguated elsewhere. | |
9066 | -- For dispatching operations in the visible part, the usual | |
9067 | -- rules apply, and operations with the same profile are not | |
9068 | -- legal (B830001). | |
9069 | ||
9070 | elsif (In_Instance_Visible_Part | |
9071 | and then not Is_Dispatching_Operation (E)) | |
9072 | or else In_Instance_Not_Visible | |
9073 | then | |
9074 | null; | |
9075 | ||
9076 | -- Here we have a real error (identical profile) | |
9077 | ||
9078 | else | |
9079 | Error_Msg_Sloc := Sloc (E); | |
9080 | ||
9081 | -- Avoid cascaded errors if the entity appears in | |
9082 | -- subsequent calls. | |
9083 | ||
9084 | Set_Scope (S, Current_Scope); | |
9085 | ||
5d37ba92 ES |
9086 | -- Generate error, with extra useful warning for the case |
9087 | -- of a generic instance with no completion. | |
996ae0b0 RK |
9088 | |
9089 | if Is_Generic_Instance (S) | |
9090 | and then not Has_Completion (E) | |
9091 | then | |
9092 | Error_Msg_N | |
5d37ba92 ES |
9093 | ("instantiation cannot provide body for&", S); |
9094 | Error_Msg_N ("\& conflicts with declaration#", S); | |
9095 | else | |
9096 | Error_Msg_N ("& conflicts with declaration#", S); | |
996ae0b0 RK |
9097 | end if; |
9098 | ||
9099 | return; | |
9100 | end if; | |
9101 | ||
9102 | else | |
c8ef728f ES |
9103 | -- If one subprogram has an access parameter and the other |
9104 | -- a parameter of an access type, calls to either might be | |
9105 | -- ambiguous. Verify that parameters match except for the | |
9106 | -- access parameter. | |
9107 | ||
9108 | if May_Hide_Profile then | |
9109 | declare | |
ec4867fa ES |
9110 | F1 : Entity_Id; |
9111 | F2 : Entity_Id; | |
8dbd1460 | 9112 | |
c8ef728f ES |
9113 | begin |
9114 | F1 := First_Formal (S); | |
9115 | F2 := First_Formal (E); | |
9116 | while Present (F1) and then Present (F2) loop | |
9117 | if Is_Access_Type (Etype (F1)) then | |
9118 | if not Is_Access_Type (Etype (F2)) | |
9119 | or else not Conforming_Types | |
9120 | (Designated_Type (Etype (F1)), | |
9121 | Designated_Type (Etype (F2)), | |
9122 | Type_Conformant) | |
9123 | then | |
9124 | May_Hide_Profile := False; | |
9125 | end if; | |
9126 | ||
9127 | elsif | |
9128 | not Conforming_Types | |
9129 | (Etype (F1), Etype (F2), Type_Conformant) | |
9130 | then | |
9131 | May_Hide_Profile := False; | |
9132 | end if; | |
9133 | ||
9134 | Next_Formal (F1); | |
9135 | Next_Formal (F2); | |
9136 | end loop; | |
9137 | ||
9138 | if May_Hide_Profile | |
9139 | and then No (F1) | |
9140 | and then No (F2) | |
9141 | then | |
9142 | Error_Msg_NE ("calls to& may be ambiguous?", S, S); | |
9143 | end if; | |
9144 | end; | |
9145 | end if; | |
996ae0b0 RK |
9146 | end if; |
9147 | ||
996ae0b0 RK |
9148 | E := Homonym (E); |
9149 | end loop; | |
9150 | ||
9151 | -- On exit, we know that S is a new entity | |
9152 | ||
9153 | Enter_Overloaded_Entity (S); | |
5d37ba92 ES |
9154 | Check_For_Primitive_Subprogram (Is_Primitive_Subp); |
9155 | Check_Overriding_Indicator | |
9156 | (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp); | |
996ae0b0 | 9157 | |
c4d67e2d | 9158 | -- Overloading is not allowed in SPARK, except for operators |
8ed68165 | 9159 | |
c4d67e2d AC |
9160 | if Nkind (S) /= N_Defining_Operator_Symbol then |
9161 | Error_Msg_Sloc := Sloc (Homonym (S)); | |
9162 | Check_SPARK_Restriction | |
9163 | ("overloading not allowed with entity#", S); | |
9164 | end if; | |
8ed68165 | 9165 | |
82c80734 RD |
9166 | -- If S is a derived operation for an untagged type then by |
9167 | -- definition it's not a dispatching operation (even if the parent | |
e917aec2 RD |
9168 | -- operation was dispatching), so Check_Dispatching_Operation is not |
9169 | -- called in that case. | |
996ae0b0 | 9170 | |
c8ef728f | 9171 | if No (Derived_Type) |
996ae0b0 RK |
9172 | or else Is_Tagged_Type (Derived_Type) |
9173 | then | |
9174 | Check_Dispatching_Operation (S, Empty); | |
9175 | end if; | |
9176 | end if; | |
9177 | ||
82c80734 RD |
9178 | -- If this is a user-defined equality operator that is not a derived |
9179 | -- subprogram, create the corresponding inequality. If the operation is | |
9180 | -- dispatching, the expansion is done elsewhere, and we do not create | |
9181 | -- an explicit inequality operation. | |
996ae0b0 RK |
9182 | |
9183 | <<Check_Inequality>> | |
9184 | if Chars (S) = Name_Op_Eq | |
9185 | and then Etype (S) = Standard_Boolean | |
9186 | and then Present (Parent (S)) | |
9187 | and then not Is_Dispatching_Operation (S) | |
9188 | then | |
9189 | Make_Inequality_Operator (S); | |
d151d6a3 | 9190 | |
dbe945f1 | 9191 | if Ada_Version >= Ada_2012 then |
e5a58fac AC |
9192 | Check_Untagged_Equality (S); |
9193 | end if; | |
996ae0b0 | 9194 | end if; |
996ae0b0 RK |
9195 | end New_Overloaded_Entity; |
9196 | ||
9197 | --------------------- | |
9198 | -- Process_Formals -- | |
9199 | --------------------- | |
9200 | ||
9201 | procedure Process_Formals | |
07fc65c4 | 9202 | (T : List_Id; |
996ae0b0 RK |
9203 | Related_Nod : Node_Id) |
9204 | is | |
9205 | Param_Spec : Node_Id; | |
9206 | Formal : Entity_Id; | |
9207 | Formal_Type : Entity_Id; | |
9208 | Default : Node_Id; | |
9209 | Ptype : Entity_Id; | |
9210 | ||
800621e0 RD |
9211 | Num_Out_Params : Nat := 0; |
9212 | First_Out_Param : Entity_Id := Empty; | |
21d27997 | 9213 | -- Used for setting Is_Only_Out_Parameter |
800621e0 | 9214 | |
950d217a AC |
9215 | function Designates_From_With_Type (Typ : Entity_Id) return Boolean; |
9216 | -- Determine whether an access type designates a type coming from a | |
9217 | -- limited view. | |
9218 | ||
07fc65c4 | 9219 | function Is_Class_Wide_Default (D : Node_Id) return Boolean; |
82c80734 RD |
9220 | -- Check whether the default has a class-wide type. After analysis the |
9221 | -- default has the type of the formal, so we must also check explicitly | |
9222 | -- for an access attribute. | |
07fc65c4 | 9223 | |
950d217a AC |
9224 | ------------------------------- |
9225 | -- Designates_From_With_Type -- | |
9226 | ------------------------------- | |
9227 | ||
9228 | function Designates_From_With_Type (Typ : Entity_Id) return Boolean is | |
9229 | Desig : Entity_Id := Typ; | |
9230 | ||
9231 | begin | |
9232 | if Is_Access_Type (Desig) then | |
9233 | Desig := Directly_Designated_Type (Desig); | |
9234 | end if; | |
9235 | ||
9236 | if Is_Class_Wide_Type (Desig) then | |
9237 | Desig := Root_Type (Desig); | |
9238 | end if; | |
9239 | ||
9240 | return | |
9241 | Ekind (Desig) = E_Incomplete_Type | |
9242 | and then From_With_Type (Desig); | |
9243 | end Designates_From_With_Type; | |
9244 | ||
07fc65c4 GB |
9245 | --------------------------- |
9246 | -- Is_Class_Wide_Default -- | |
9247 | --------------------------- | |
9248 | ||
9249 | function Is_Class_Wide_Default (D : Node_Id) return Boolean is | |
9250 | begin | |
9251 | return Is_Class_Wide_Type (Designated_Type (Etype (D))) | |
9252 | or else (Nkind (D) = N_Attribute_Reference | |
0f853035 YM |
9253 | and then Attribute_Name (D) = Name_Access |
9254 | and then Is_Class_Wide_Type (Etype (Prefix (D)))); | |
07fc65c4 GB |
9255 | end Is_Class_Wide_Default; |
9256 | ||
9257 | -- Start of processing for Process_Formals | |
9258 | ||
996ae0b0 RK |
9259 | begin |
9260 | -- In order to prevent premature use of the formals in the same formal | |
9261 | -- part, the Ekind is left undefined until all default expressions are | |
9262 | -- analyzed. The Ekind is established in a separate loop at the end. | |
9263 | ||
9264 | Param_Spec := First (T); | |
996ae0b0 | 9265 | while Present (Param_Spec) loop |
996ae0b0 | 9266 | Formal := Defining_Identifier (Param_Spec); |
5d37ba92 | 9267 | Set_Never_Set_In_Source (Formal, True); |
996ae0b0 RK |
9268 | Enter_Name (Formal); |
9269 | ||
9270 | -- Case of ordinary parameters | |
9271 | ||
9272 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
9273 | Find_Type (Parameter_Type (Param_Spec)); | |
9274 | Ptype := Parameter_Type (Param_Spec); | |
9275 | ||
9276 | if Ptype = Error then | |
9277 | goto Continue; | |
9278 | end if; | |
9279 | ||
9280 | Formal_Type := Entity (Ptype); | |
9281 | ||
ec4867fa ES |
9282 | if Is_Incomplete_Type (Formal_Type) |
9283 | or else | |
9284 | (Is_Class_Wide_Type (Formal_Type) | |
9285 | and then Is_Incomplete_Type (Root_Type (Formal_Type))) | |
996ae0b0 | 9286 | then |
93bcda23 AC |
9287 | -- Ada 2005 (AI-326): Tagged incomplete types allowed in |
9288 | -- primitive operations, as long as their completion is | |
9289 | -- in the same declarative part. If in the private part | |
9290 | -- this means that the type cannot be a Taft-amendment type. | |
cec29135 ES |
9291 | -- Check is done on package exit. For access to subprograms, |
9292 | -- the use is legal for Taft-amendment types. | |
fbf5a39b | 9293 | |
d8db0bca | 9294 | if Is_Tagged_Type (Formal_Type) then |
93bcda23 | 9295 | if Ekind (Scope (Current_Scope)) = E_Package |
93bcda23 AC |
9296 | and then not From_With_Type (Formal_Type) |
9297 | and then not Is_Class_Wide_Type (Formal_Type) | |
9298 | then | |
cec29135 ES |
9299 | if not Nkind_In |
9300 | (Parent (T), N_Access_Function_Definition, | |
9301 | N_Access_Procedure_Definition) | |
9302 | then | |
9303 | Append_Elmt | |
9304 | (Current_Scope, | |
9305 | Private_Dependents (Base_Type (Formal_Type))); | |
4637729f AC |
9306 | |
9307 | -- Freezing is delayed to ensure that Register_Prim | |
9308 | -- will get called for this operation, which is needed | |
9309 | -- in cases where static dispatch tables aren't built. | |
9310 | -- (Note that the same is done for controlling access | |
9311 | -- parameter cases in function Access_Definition.) | |
9312 | ||
9313 | Set_Has_Delayed_Freeze (Current_Scope); | |
cec29135 | 9314 | end if; |
93bcda23 | 9315 | end if; |
fbf5a39b | 9316 | |
0a36105d JM |
9317 | -- Special handling of Value_Type for CIL case |
9318 | ||
9319 | elsif Is_Value_Type (Formal_Type) then | |
9320 | null; | |
9321 | ||
800621e0 RD |
9322 | elsif not Nkind_In (Parent (T), N_Access_Function_Definition, |
9323 | N_Access_Procedure_Definition) | |
996ae0b0 | 9324 | then |
dd386db0 AC |
9325 | -- AI05-0151: Tagged incomplete types are allowed in all |
9326 | -- formal parts. Untagged incomplete types are not allowed | |
9327 | -- in bodies. | |
9328 | ||
9329 | if Ada_Version >= Ada_2012 then | |
9330 | if Is_Tagged_Type (Formal_Type) then | |
9331 | null; | |
9332 | ||
0f1a6a0b AC |
9333 | elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement, |
9334 | N_Entry_Body, | |
9335 | N_Subprogram_Body) | |
dd386db0 AC |
9336 | then |
9337 | Error_Msg_NE | |
9338 | ("invalid use of untagged incomplete type&", | |
0f1a6a0b | 9339 | Ptype, Formal_Type); |
dd386db0 AC |
9340 | end if; |
9341 | ||
9342 | else | |
9343 | Error_Msg_NE | |
9344 | ("invalid use of incomplete type&", | |
0f1a6a0b | 9345 | Param_Spec, Formal_Type); |
dd386db0 AC |
9346 | |
9347 | -- Further checks on the legality of incomplete types | |
9348 | -- in formal parts are delayed until the freeze point | |
9349 | -- of the enclosing subprogram or access to subprogram. | |
9350 | end if; | |
996ae0b0 RK |
9351 | end if; |
9352 | ||
9353 | elsif Ekind (Formal_Type) = E_Void then | |
0f1a6a0b AC |
9354 | Error_Msg_NE |
9355 | ("premature use of&", | |
9356 | Parameter_Type (Param_Spec), Formal_Type); | |
996ae0b0 RK |
9357 | end if; |
9358 | ||
fecbd779 AC |
9359 | -- Ada 2012 (AI-142): Handle aliased parameters |
9360 | ||
9361 | if Ada_Version >= Ada_2012 | |
9362 | and then Aliased_Present (Param_Spec) | |
9363 | then | |
9364 | Set_Is_Aliased (Formal); | |
9365 | end if; | |
9366 | ||
0ab80019 | 9367 | -- Ada 2005 (AI-231): Create and decorate an internal subtype |
7324bf49 | 9368 | -- declaration corresponding to the null-excluding type of the |
d8db0bca JM |
9369 | -- formal in the enclosing scope. Finally, replace the parameter |
9370 | -- type of the formal with the internal subtype. | |
7324bf49 | 9371 | |
0791fbe9 | 9372 | if Ada_Version >= Ada_2005 |
41251c60 | 9373 | and then Null_Exclusion_Present (Param_Spec) |
7324bf49 | 9374 | then |
ec4867fa | 9375 | if not Is_Access_Type (Formal_Type) then |
ed2233dc | 9376 | Error_Msg_N |
0a36105d JM |
9377 | ("`NOT NULL` allowed only for an access type", Param_Spec); |
9378 | ||
ec4867fa ES |
9379 | else |
9380 | if Can_Never_Be_Null (Formal_Type) | |
9381 | and then Comes_From_Source (Related_Nod) | |
9382 | then | |
ed2233dc | 9383 | Error_Msg_NE |
0a36105d | 9384 | ("`NOT NULL` not allowed (& already excludes null)", |
0f1a6a0b | 9385 | Param_Spec, Formal_Type); |
ec4867fa | 9386 | end if; |
41251c60 | 9387 | |
ec4867fa ES |
9388 | Formal_Type := |
9389 | Create_Null_Excluding_Itype | |
9390 | (T => Formal_Type, | |
9391 | Related_Nod => Related_Nod, | |
9392 | Scope_Id => Scope (Current_Scope)); | |
0a36105d JM |
9393 | |
9394 | -- If the designated type of the itype is an itype we | |
9395 | -- decorate it with the Has_Delayed_Freeze attribute to | |
9396 | -- avoid problems with the backend. | |
9397 | ||
9398 | -- Example: | |
9399 | -- type T is access procedure; | |
9400 | -- procedure Op (O : not null T); | |
9401 | ||
9402 | if Is_Itype (Directly_Designated_Type (Formal_Type)) then | |
9403 | Set_Has_Delayed_Freeze (Formal_Type); | |
9404 | end if; | |
ec4867fa | 9405 | end if; |
7324bf49 AC |
9406 | end if; |
9407 | ||
996ae0b0 RK |
9408 | -- An access formal type |
9409 | ||
9410 | else | |
9411 | Formal_Type := | |
9412 | Access_Definition (Related_Nod, Parameter_Type (Param_Spec)); | |
7324bf49 | 9413 | |
f937473f RD |
9414 | -- No need to continue if we already notified errors |
9415 | ||
9416 | if not Present (Formal_Type) then | |
9417 | return; | |
9418 | end if; | |
9419 | ||
0ab80019 | 9420 | -- Ada 2005 (AI-254) |
7324bf49 | 9421 | |
af4b9434 AC |
9422 | declare |
9423 | AD : constant Node_Id := | |
9424 | Access_To_Subprogram_Definition | |
9425 | (Parameter_Type (Param_Spec)); | |
9426 | begin | |
9427 | if Present (AD) and then Protected_Present (AD) then | |
9428 | Formal_Type := | |
9429 | Replace_Anonymous_Access_To_Protected_Subprogram | |
f937473f | 9430 | (Param_Spec); |
af4b9434 AC |
9431 | end if; |
9432 | end; | |
996ae0b0 RK |
9433 | end if; |
9434 | ||
9435 | Set_Etype (Formal, Formal_Type); | |
0f853035 | 9436 | |
fecbd779 AC |
9437 | -- Deal with default expression if present |
9438 | ||
fbf5a39b | 9439 | Default := Expression (Param_Spec); |
996ae0b0 RK |
9440 | |
9441 | if Present (Default) then | |
2ba431e5 | 9442 | Check_SPARK_Restriction |
fe5d3068 | 9443 | ("default expression is not allowed", Default); |
38171f43 | 9444 | |
996ae0b0 | 9445 | if Out_Present (Param_Spec) then |
ed2233dc | 9446 | Error_Msg_N |
996ae0b0 RK |
9447 | ("default initialization only allowed for IN parameters", |
9448 | Param_Spec); | |
9449 | end if; | |
9450 | ||
9451 | -- Do the special preanalysis of the expression (see section on | |
9452 | -- "Handling of Default Expressions" in the spec of package Sem). | |
9453 | ||
21d27997 | 9454 | Preanalyze_Spec_Expression (Default, Formal_Type); |
996ae0b0 | 9455 | |
f29b857f ES |
9456 | -- An access to constant cannot be the default for |
9457 | -- an access parameter that is an access to variable. | |
2eb160f2 ST |
9458 | |
9459 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
9460 | and then not Is_Access_Constant (Formal_Type) | |
9461 | and then Is_Access_Type (Etype (Default)) | |
9462 | and then Is_Access_Constant (Etype (Default)) | |
9463 | then | |
f29b857f ES |
9464 | Error_Msg_N |
9465 | ("formal that is access to variable cannot be initialized " & | |
9466 | "with an access-to-constant expression", Default); | |
2eb160f2 ST |
9467 | end if; |
9468 | ||
d8db0bca JM |
9469 | -- Check that the designated type of an access parameter's default |
9470 | -- is not a class-wide type unless the parameter's designated type | |
9471 | -- is also class-wide. | |
996ae0b0 RK |
9472 | |
9473 | if Ekind (Formal_Type) = E_Anonymous_Access_Type | |
950d217a | 9474 | and then not Designates_From_With_Type (Formal_Type) |
07fc65c4 | 9475 | and then Is_Class_Wide_Default (Default) |
996ae0b0 RK |
9476 | and then not Is_Class_Wide_Type (Designated_Type (Formal_Type)) |
9477 | then | |
07fc65c4 GB |
9478 | Error_Msg_N |
9479 | ("access to class-wide expression not allowed here", Default); | |
996ae0b0 | 9480 | end if; |
4755cce9 JM |
9481 | |
9482 | -- Check incorrect use of dynamically tagged expressions | |
9483 | ||
9484 | if Is_Tagged_Type (Formal_Type) then | |
9485 | Check_Dynamically_Tagged_Expression | |
9486 | (Expr => Default, | |
9487 | Typ => Formal_Type, | |
9488 | Related_Nod => Default); | |
9489 | end if; | |
996ae0b0 RK |
9490 | end if; |
9491 | ||
41251c60 JM |
9492 | -- Ada 2005 (AI-231): Static checks |
9493 | ||
0791fbe9 | 9494 | if Ada_Version >= Ada_2005 |
41251c60 JM |
9495 | and then Is_Access_Type (Etype (Formal)) |
9496 | and then Can_Never_Be_Null (Etype (Formal)) | |
9497 | then | |
9498 | Null_Exclusion_Static_Checks (Param_Spec); | |
9499 | end if; | |
9500 | ||
996ae0b0 RK |
9501 | <<Continue>> |
9502 | Next (Param_Spec); | |
9503 | end loop; | |
9504 | ||
82c80734 RD |
9505 | -- If this is the formal part of a function specification, analyze the |
9506 | -- subtype mark in the context where the formals are visible but not | |
9507 | -- yet usable, and may hide outer homographs. | |
9508 | ||
9509 | if Nkind (Related_Nod) = N_Function_Specification then | |
9510 | Analyze_Return_Type (Related_Nod); | |
9511 | end if; | |
9512 | ||
996ae0b0 RK |
9513 | -- Now set the kind (mode) of each formal |
9514 | ||
9515 | Param_Spec := First (T); | |
996ae0b0 RK |
9516 | while Present (Param_Spec) loop |
9517 | Formal := Defining_Identifier (Param_Spec); | |
9518 | Set_Formal_Mode (Formal); | |
9519 | ||
9520 | if Ekind (Formal) = E_In_Parameter then | |
9521 | Set_Default_Value (Formal, Expression (Param_Spec)); | |
9522 | ||
9523 | if Present (Expression (Param_Spec)) then | |
9524 | Default := Expression (Param_Spec); | |
9525 | ||
9526 | if Is_Scalar_Type (Etype (Default)) then | |
9527 | if Nkind | |
9528 | (Parameter_Type (Param_Spec)) /= N_Access_Definition | |
9529 | then | |
9530 | Formal_Type := Entity (Parameter_Type (Param_Spec)); | |
9531 | ||
9532 | else | |
9533 | Formal_Type := Access_Definition | |
9534 | (Related_Nod, Parameter_Type (Param_Spec)); | |
9535 | end if; | |
9536 | ||
9537 | Apply_Scalar_Range_Check (Default, Formal_Type); | |
9538 | end if; | |
2820d220 | 9539 | end if; |
800621e0 RD |
9540 | |
9541 | elsif Ekind (Formal) = E_Out_Parameter then | |
9542 | Num_Out_Params := Num_Out_Params + 1; | |
9543 | ||
9544 | if Num_Out_Params = 1 then | |
9545 | First_Out_Param := Formal; | |
9546 | end if; | |
9547 | ||
9548 | elsif Ekind (Formal) = E_In_Out_Parameter then | |
9549 | Num_Out_Params := Num_Out_Params + 1; | |
996ae0b0 RK |
9550 | end if; |
9551 | ||
fecbd779 AC |
9552 | -- Force call by reference if aliased |
9553 | ||
9554 | if Is_Aliased (Formal) then | |
9555 | Set_Mechanism (Formal, By_Reference); | |
9556 | end if; | |
9557 | ||
996ae0b0 RK |
9558 | Next (Param_Spec); |
9559 | end loop; | |
800621e0 RD |
9560 | |
9561 | if Present (First_Out_Param) and then Num_Out_Params = 1 then | |
9562 | Set_Is_Only_Out_Parameter (First_Out_Param); | |
9563 | end if; | |
996ae0b0 RK |
9564 | end Process_Formals; |
9565 | ||
21d27997 RD |
9566 | ------------------ |
9567 | -- Process_PPCs -- | |
9568 | ------------------ | |
9569 | ||
9570 | procedure Process_PPCs | |
9571 | (N : Node_Id; | |
9572 | Spec_Id : Entity_Id; | |
9573 | Body_Id : Entity_Id) | |
9574 | is | |
9575 | Loc : constant Source_Ptr := Sloc (N); | |
9576 | Prag : Node_Id; | |
21d27997 RD |
9577 | Parms : List_Id; |
9578 | ||
e606088a AC |
9579 | Designator : Entity_Id; |
9580 | -- Subprogram designator, set from Spec_Id if present, else Body_Id | |
9581 | ||
beacce02 AC |
9582 | Precond : Node_Id := Empty; |
9583 | -- Set non-Empty if we prepend precondition to the declarations. This | |
9584 | -- is used to hook up inherited preconditions (adding the condition | |
9585 | -- expression with OR ELSE, and adding the message). | |
9586 | ||
9587 | Inherited_Precond : Node_Id; | |
9588 | -- Precondition inherited from parent subprogram | |
9589 | ||
9590 | Inherited : constant Subprogram_List := | |
e606088a AC |
9591 | Inherited_Subprograms (Spec_Id); |
9592 | -- List of subprograms inherited by this subprogram | |
beacce02 AC |
9593 | |
9594 | Plist : List_Id := No_List; | |
9595 | -- List of generated postconditions | |
9596 | ||
f0709ca6 AC |
9597 | function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id; |
9598 | -- Prag contains an analyzed precondition or postcondition pragma. This | |
9599 | -- function copies the pragma, changes it to the corresponding Check | |
9600 | -- pragma and returns the Check pragma as the result. If Pspec is non- | |
9601 | -- empty, this is the case of inheriting a PPC, where we must change | |
9602 | -- references to parameters of the inherited subprogram to point to the | |
9603 | -- corresponding parameters of the current subprogram. | |
21d27997 | 9604 | |
b4ca2d2c AC |
9605 | function Invariants_Or_Predicates_Present return Boolean; |
9606 | -- Determines if any invariants or predicates are present for any OUT | |
9607 | -- or IN OUT parameters of the subprogram, or (for a function) if the | |
9608 | -- return value has an invariant. | |
e606088a | 9609 | |
a4901c08 AC |
9610 | function Is_Public_Subprogram_For (T : Entity_Id) return Boolean; |
9611 | -- T is the entity for a private type for which invariants are defined. | |
9612 | -- This function returns True if the procedure corresponding to the | |
9613 | -- value of Designator is a public procedure from the point of view of | |
9614 | -- this type (i.e. its spec is in the visible part of the package that | |
9615 | -- contains the declaration of the private type). A True value means | |
9616 | -- that an invariant check is required (for an IN OUT parameter, or | |
9617 | -- the returned value of a function. | |
9618 | ||
21d27997 RD |
9619 | -------------- |
9620 | -- Grab_PPC -- | |
9621 | -------------- | |
9622 | ||
f0709ca6 AC |
9623 | function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is |
9624 | Nam : constant Name_Id := Pragma_Name (Prag); | |
9625 | Map : Elist_Id; | |
9626 | CP : Node_Id; | |
21d27997 RD |
9627 | |
9628 | begin | |
f0709ca6 AC |
9629 | -- Prepare map if this is the case where we have to map entities of |
9630 | -- arguments in the overridden subprogram to corresponding entities | |
9631 | -- of the current subprogram. | |
9632 | ||
9633 | if No (Pspec) then | |
9634 | Map := No_Elist; | |
9635 | ||
9636 | else | |
9637 | declare | |
9638 | PF : Entity_Id; | |
9639 | CF : Entity_Id; | |
9640 | ||
9641 | begin | |
9642 | Map := New_Elmt_List; | |
9643 | PF := First_Formal (Pspec); | |
e606088a | 9644 | CF := First_Formal (Designator); |
f0709ca6 AC |
9645 | while Present (PF) loop |
9646 | Append_Elmt (PF, Map); | |
9647 | Append_Elmt (CF, Map); | |
9648 | Next_Formal (PF); | |
9649 | Next_Formal (CF); | |
9650 | end loop; | |
9651 | end; | |
9652 | end if; | |
9653 | ||
308e6f3a | 9654 | -- Now we can copy the tree, doing any required substitutions |
f0709ca6 AC |
9655 | |
9656 | CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope); | |
9657 | ||
21d27997 RD |
9658 | -- Set Analyzed to false, since we want to reanalyze the check |
9659 | -- procedure. Note that it is only at the outer level that we | |
9660 | -- do this fiddling, for the spec cases, the already preanalyzed | |
9661 | -- parameters are not affected. | |
766d7add | 9662 | |
1fb00064 AC |
9663 | Set_Analyzed (CP, False); |
9664 | ||
9665 | -- We also make sure Comes_From_Source is False for the copy | |
9666 | ||
9667 | Set_Comes_From_Source (CP, False); | |
9668 | ||
0dabde3a ES |
9669 | -- For a postcondition pragma within a generic, preserve the pragma |
9670 | -- for later expansion. | |
21d27997 | 9671 | |
0dabde3a ES |
9672 | if Nam = Name_Postcondition |
9673 | and then not Expander_Active | |
9674 | then | |
9675 | return CP; | |
9676 | end if; | |
9677 | ||
1fb00064 | 9678 | -- Change copy of pragma into corresponding pragma Check |
21d27997 RD |
9679 | |
9680 | Prepend_To (Pragma_Argument_Associations (CP), | |
9681 | Make_Pragma_Argument_Association (Sloc (Prag), | |
7675ad4f AC |
9682 | Expression => Make_Identifier (Loc, Nam))); |
9683 | Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check)); | |
21d27997 | 9684 | |
beacce02 AC |
9685 | -- If this is inherited case and the current message starts with |
9686 | -- "failed p", we change it to "failed inherited p...". | |
f0709ca6 AC |
9687 | |
9688 | if Present (Pspec) then | |
beacce02 AC |
9689 | declare |
9690 | Msg : constant Node_Id := | |
9691 | Last (Pragma_Argument_Associations (CP)); | |
9692 | ||
9693 | begin | |
9694 | if Chars (Msg) = Name_Message then | |
9695 | String_To_Name_Buffer (Strval (Expression (Msg))); | |
9696 | ||
9697 | if Name_Buffer (1 .. 8) = "failed p" then | |
9698 | Insert_Str_In_Name_Buffer ("inherited ", 8); | |
9699 | Set_Strval | |
9700 | (Expression (Last (Pragma_Argument_Associations (CP))), | |
9701 | String_From_Name_Buffer); | |
9702 | end if; | |
9703 | end if; | |
9704 | end; | |
f0709ca6 AC |
9705 | end if; |
9706 | ||
9707 | -- Return the check pragma | |
9708 | ||
21d27997 RD |
9709 | return CP; |
9710 | end Grab_PPC; | |
9711 | ||
b4ca2d2c AC |
9712 | -------------------------------------- |
9713 | -- Invariants_Or_Predicates_Present -- | |
9714 | -------------------------------------- | |
e606088a | 9715 | |
b4ca2d2c AC |
9716 | function Invariants_Or_Predicates_Present return Boolean is |
9717 | Formal : Entity_Id; | |
e606088a AC |
9718 | |
9719 | begin | |
9720 | -- Check function return result | |
9721 | ||
9722 | if Ekind (Designator) /= E_Procedure | |
9723 | and then Has_Invariants (Etype (Designator)) | |
9724 | then | |
9725 | return True; | |
9726 | end if; | |
9727 | ||
9728 | -- Check parameters | |
9729 | ||
9730 | Formal := First_Formal (Designator); | |
9731 | while Present (Formal) loop | |
9732 | if Ekind (Formal) /= E_In_Parameter | |
b4ca2d2c AC |
9733 | and then |
9734 | (Has_Invariants (Etype (Formal)) | |
9735 | or else Present (Predicate_Function (Etype (Formal)))) | |
e606088a AC |
9736 | then |
9737 | return True; | |
9738 | end if; | |
9739 | ||
9740 | Next_Formal (Formal); | |
9741 | end loop; | |
9742 | ||
9743 | return False; | |
b4ca2d2c | 9744 | end Invariants_Or_Predicates_Present; |
e606088a | 9745 | |
a4901c08 AC |
9746 | ------------------------------ |
9747 | -- Is_Public_Subprogram_For -- | |
9748 | ------------------------------ | |
9749 | ||
9750 | -- The type T is a private type, its declaration is therefore in | |
9751 | -- the list of public declarations of some package. The test for a | |
9752 | -- public subprogram is that its declaration is in this same list | |
9753 | -- of declarations for the same package (note that all the public | |
9754 | -- declarations are in one list, and all the private declarations | |
9755 | -- in another, so this deals with the public/private distinction). | |
9756 | ||
9757 | function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is | |
9758 | DD : constant Node_Id := Unit_Declaration_Node (Designator); | |
9759 | -- The subprogram declaration for the subprogram in question | |
9760 | ||
9761 | TL : constant List_Id := | |
9762 | Visible_Declarations | |
9763 | (Specification (Unit_Declaration_Node (Scope (T)))); | |
9764 | -- The list of declarations containing the private declaration of | |
9765 | -- the type. We know it is a private type, so we know its scope is | |
9766 | -- the package in question, and we know it must be in the visible | |
9767 | -- declarations of this package. | |
9768 | ||
9769 | begin | |
9770 | -- If the subprogram declaration is not a list member, it must be | |
9771 | -- an Init_Proc, in which case we want to consider it to be a | |
9772 | -- public subprogram, since we do get initializations to deal with. | |
9773 | ||
9774 | if not Is_List_Member (DD) then | |
9775 | return True; | |
9776 | ||
9777 | -- Otherwise we test whether the subprogram is declared in the | |
9778 | -- visible declarations of the package containing the type. | |
9779 | ||
9780 | else | |
9781 | return TL = List_Containing (DD); | |
9782 | end if; | |
9783 | end Is_Public_Subprogram_For; | |
9784 | ||
21d27997 RD |
9785 | -- Start of processing for Process_PPCs |
9786 | ||
9787 | begin | |
e606088a AC |
9788 | -- Capture designator from spec if present, else from body |
9789 | ||
9790 | if Present (Spec_Id) then | |
9791 | Designator := Spec_Id; | |
9792 | else | |
9793 | Designator := Body_Id; | |
9794 | end if; | |
9795 | ||
21d27997 RD |
9796 | -- Grab preconditions from spec |
9797 | ||
9798 | if Present (Spec_Id) then | |
9799 | ||
9800 | -- Loop through PPC pragmas from spec. Note that preconditions from | |
9801 | -- the body will be analyzed and converted when we scan the body | |
9802 | -- declarations below. | |
9803 | ||
dac3bede | 9804 | Prag := Spec_PPC_List (Contract (Spec_Id)); |
21d27997 | 9805 | while Present (Prag) loop |
1fb00064 AC |
9806 | if Pragma_Name (Prag) = Name_Precondition then |
9807 | ||
beacce02 AC |
9808 | -- For Pre (or Precondition pragma), we simply prepend the |
9809 | -- pragma to the list of declarations right away so that it | |
9810 | -- will be executed at the start of the procedure. Note that | |
9811 | -- this processing reverses the order of the list, which is | |
9812 | -- what we want since new entries were chained to the head of | |
2d395256 AC |
9813 | -- the list. There can be more than one precondition when we |
9814 | -- use pragma Precondition. | |
beacce02 AC |
9815 | |
9816 | if not Class_Present (Prag) then | |
9817 | Prepend (Grab_PPC, Declarations (N)); | |
9818 | ||
9819 | -- For Pre'Class there can only be one pragma, and we save | |
9820 | -- it in Precond for now. We will add inherited Pre'Class | |
9821 | -- stuff before inserting this pragma in the declarations. | |
9822 | else | |
9823 | Precond := Grab_PPC; | |
9824 | end if; | |
21d27997 RD |
9825 | end if; |
9826 | ||
9827 | Prag := Next_Pragma (Prag); | |
9828 | end loop; | |
beacce02 AC |
9829 | |
9830 | -- Now deal with inherited preconditions | |
9831 | ||
9832 | for J in Inherited'Range loop | |
dac3bede | 9833 | Prag := Spec_PPC_List (Contract (Inherited (J))); |
beacce02 AC |
9834 | |
9835 | while Present (Prag) loop | |
9836 | if Pragma_Name (Prag) = Name_Precondition | |
9837 | and then Class_Present (Prag) | |
9838 | then | |
3c971dcc | 9839 | Inherited_Precond := Grab_PPC (Inherited (J)); |
beacce02 AC |
9840 | |
9841 | -- No precondition so far, so establish this as the first | |
9842 | ||
9843 | if No (Precond) then | |
9844 | Precond := Inherited_Precond; | |
9845 | ||
9846 | -- Here we already have a precondition, add inherited one | |
9847 | ||
9848 | else | |
9849 | -- Add new precondition to old one using OR ELSE | |
9850 | ||
9851 | declare | |
9852 | New_Expr : constant Node_Id := | |
9853 | Get_Pragma_Arg | |
9854 | (Next | |
9855 | (First | |
9856 | (Pragma_Argument_Associations | |
9857 | (Inherited_Precond)))); | |
9858 | Old_Expr : constant Node_Id := | |
9859 | Get_Pragma_Arg | |
9860 | (Next | |
9861 | (First | |
9862 | (Pragma_Argument_Associations | |
9863 | (Precond)))); | |
9864 | ||
9865 | begin | |
9866 | if Paren_Count (Old_Expr) = 0 then | |
9867 | Set_Paren_Count (Old_Expr, 1); | |
9868 | end if; | |
9869 | ||
9870 | if Paren_Count (New_Expr) = 0 then | |
9871 | Set_Paren_Count (New_Expr, 1); | |
9872 | end if; | |
9873 | ||
9874 | Rewrite (Old_Expr, | |
9875 | Make_Or_Else (Sloc (Old_Expr), | |
9876 | Left_Opnd => Relocate_Node (Old_Expr), | |
9877 | Right_Opnd => New_Expr)); | |
9878 | end; | |
9879 | ||
9880 | -- Add new message in the form: | |
9881 | ||
9882 | -- failed precondition from bla | |
9883 | -- also failed inherited precondition from bla | |
9884 | -- ... | |
9885 | ||
3c971dcc AC |
9886 | -- Skip this if exception locations are suppressed |
9887 | ||
9888 | if not Exception_Locations_Suppressed then | |
9889 | declare | |
9890 | New_Msg : constant Node_Id := | |
9891 | Get_Pragma_Arg | |
9892 | (Last | |
9893 | (Pragma_Argument_Associations | |
9894 | (Inherited_Precond))); | |
9895 | Old_Msg : constant Node_Id := | |
9896 | Get_Pragma_Arg | |
9897 | (Last | |
9898 | (Pragma_Argument_Associations | |
9899 | (Precond))); | |
9900 | begin | |
9901 | Start_String (Strval (Old_Msg)); | |
9902 | Store_String_Chars (ASCII.LF & " also "); | |
9903 | Store_String_Chars (Strval (New_Msg)); | |
9904 | Set_Strval (Old_Msg, End_String); | |
9905 | end; | |
9906 | end if; | |
beacce02 AC |
9907 | end if; |
9908 | end if; | |
9909 | ||
9910 | Prag := Next_Pragma (Prag); | |
9911 | end loop; | |
9912 | end loop; | |
9913 | ||
9914 | -- If we have built a precondition for Pre'Class (including any | |
9915 | -- Pre'Class aspects inherited from parent subprograms), then we | |
9916 | -- insert this composite precondition at this stage. | |
9917 | ||
9918 | if Present (Precond) then | |
9919 | Prepend (Precond, Declarations (N)); | |
9920 | end if; | |
21d27997 RD |
9921 | end if; |
9922 | ||
9923 | -- Build postconditions procedure if needed and prepend the following | |
9924 | -- declaration to the start of the declarations for the subprogram. | |
9925 | ||
9926 | -- procedure _postconditions [(_Result : resulttype)] is | |
9927 | -- begin | |
9928 | -- pragma Check (Postcondition, condition [,message]); | |
9929 | -- pragma Check (Postcondition, condition [,message]); | |
9930 | -- ... | |
e606088a AC |
9931 | -- Invariant_Procedure (_Result) ... |
9932 | -- Invariant_Procedure (Arg1) | |
9933 | -- ... | |
21d27997 RD |
9934 | -- end; |
9935 | ||
9936 | -- First we deal with the postconditions in the body | |
9937 | ||
9938 | if Is_Non_Empty_List (Declarations (N)) then | |
9939 | ||
9940 | -- Loop through declarations | |
9941 | ||
9942 | Prag := First (Declarations (N)); | |
9943 | while Present (Prag) loop | |
9944 | if Nkind (Prag) = N_Pragma then | |
9945 | ||
9946 | -- If pragma, capture if enabled postcondition, else ignore | |
9947 | ||
9948 | if Pragma_Name (Prag) = Name_Postcondition | |
9949 | and then Check_Enabled (Name_Postcondition) | |
9950 | then | |
9951 | if Plist = No_List then | |
9952 | Plist := Empty_List; | |
9953 | end if; | |
9954 | ||
9955 | Analyze (Prag); | |
0dabde3a | 9956 | |
f0709ca6 AC |
9957 | -- If expansion is disabled, as in a generic unit, save |
9958 | -- pragma for later expansion. | |
0dabde3a ES |
9959 | |
9960 | if not Expander_Active then | |
f0709ca6 | 9961 | Prepend (Grab_PPC, Declarations (N)); |
0dabde3a | 9962 | else |
f0709ca6 | 9963 | Append (Grab_PPC, Plist); |
0dabde3a | 9964 | end if; |
21d27997 RD |
9965 | end if; |
9966 | ||
9967 | Next (Prag); | |
9968 | ||
043ce308 | 9969 | -- Not a pragma, if comes from source, then end scan |
21d27997 RD |
9970 | |
9971 | elsif Comes_From_Source (Prag) then | |
9972 | exit; | |
9973 | ||
043ce308 | 9974 | -- Skip stuff not coming from source |
21d27997 RD |
9975 | |
9976 | else | |
9977 | Next (Prag); | |
9978 | end if; | |
9979 | end loop; | |
9980 | end if; | |
9981 | ||
9982 | -- Now deal with any postconditions from the spec | |
9983 | ||
9984 | if Present (Spec_Id) then | |
e606088a | 9985 | Spec_Postconditions : declare |
f0709ca6 AC |
9986 | procedure Process_Post_Conditions |
9987 | (Spec : Node_Id; | |
9988 | Class : Boolean); | |
9989 | -- This processes the Spec_PPC_List from Spec, processing any | |
9990 | -- postconditions from the list. If Class is True, then only | |
9991 | -- postconditions marked with Class_Present are considered. | |
9992 | -- The caller has checked that Spec_PPC_List is non-Empty. | |
9993 | ||
9994 | ----------------------------- | |
9995 | -- Process_Post_Conditions -- | |
9996 | ----------------------------- | |
9997 | ||
9998 | procedure Process_Post_Conditions | |
9999 | (Spec : Node_Id; | |
10000 | Class : Boolean) | |
10001 | is | |
10002 | Pspec : Node_Id; | |
21d27997 | 10003 | |
f0709ca6 AC |
10004 | begin |
10005 | if Class then | |
10006 | Pspec := Spec; | |
0dabde3a | 10007 | else |
f0709ca6 | 10008 | Pspec := Empty; |
0dabde3a | 10009 | end if; |
f0709ca6 AC |
10010 | |
10011 | -- Loop through PPC pragmas from spec | |
10012 | ||
dac3bede | 10013 | Prag := Spec_PPC_List (Contract (Spec)); |
f0709ca6 AC |
10014 | loop |
10015 | if Pragma_Name (Prag) = Name_Postcondition | |
f0709ca6 AC |
10016 | and then (not Class or else Class_Present (Prag)) |
10017 | then | |
10018 | if Plist = No_List then | |
10019 | Plist := Empty_List; | |
10020 | end if; | |
10021 | ||
10022 | if not Expander_Active then | |
10023 | Prepend | |
10024 | (Grab_PPC (Pspec), Declarations (N)); | |
10025 | else | |
10026 | Append (Grab_PPC (Pspec), Plist); | |
10027 | end if; | |
10028 | end if; | |
10029 | ||
10030 | Prag := Next_Pragma (Prag); | |
10031 | exit when No (Prag); | |
10032 | end loop; | |
10033 | end Process_Post_Conditions; | |
10034 | ||
e606088a AC |
10035 | -- Start of processing for Spec_Postconditions |
10036 | ||
f0709ca6 | 10037 | begin |
dac3bede | 10038 | if Present (Spec_PPC_List (Contract (Spec_Id))) then |
f0709ca6 | 10039 | Process_Post_Conditions (Spec_Id, Class => False); |
21d27997 RD |
10040 | end if; |
10041 | ||
beacce02 | 10042 | -- Process inherited postconditions |
f0709ca6 | 10043 | |
beacce02 | 10044 | for J in Inherited'Range loop |
dac3bede | 10045 | if Present (Spec_PPC_List (Contract (Inherited (J)))) then |
beacce02 | 10046 | Process_Post_Conditions (Inherited (J), Class => True); |
f0709ca6 AC |
10047 | end if; |
10048 | end loop; | |
e606088a | 10049 | end Spec_Postconditions; |
21d27997 RD |
10050 | end if; |
10051 | ||
e606088a AC |
10052 | -- If we had any postconditions and expansion is enabled, or if the |
10053 | -- procedure has invariants, then build the _Postconditions procedure. | |
21d27997 | 10054 | |
b4ca2d2c | 10055 | if (Present (Plist) or else Invariants_Or_Predicates_Present) |
0dabde3a ES |
10056 | and then Expander_Active |
10057 | then | |
e606088a AC |
10058 | if No (Plist) then |
10059 | Plist := Empty_List; | |
10060 | end if; | |
10061 | ||
10062 | -- Special processing for function case | |
10063 | ||
10064 | if Ekind (Designator) /= E_Procedure then | |
10065 | declare | |
10066 | Rent : constant Entity_Id := | |
fecbd779 | 10067 | Make_Defining_Identifier (Loc, Name_uResult); |
e606088a AC |
10068 | Ftyp : constant Entity_Id := Etype (Designator); |
10069 | ||
10070 | begin | |
10071 | Set_Etype (Rent, Ftyp); | |
10072 | ||
10073 | -- Add argument for return | |
10074 | ||
10075 | Parms := | |
10076 | New_List ( | |
10077 | Make_Parameter_Specification (Loc, | |
10078 | Parameter_Type => New_Occurrence_Of (Ftyp, Loc), | |
10079 | Defining_Identifier => Rent)); | |
10080 | ||
a4901c08 AC |
10081 | -- Add invariant call if returning type with invariants and |
10082 | -- this is a public function, i.e. a function declared in the | |
10083 | -- visible part of the package defining the private type. | |
e606088a | 10084 | |
fd0ff1cf RD |
10085 | if Has_Invariants (Etype (Rent)) |
10086 | and then Present (Invariant_Procedure (Etype (Rent))) | |
a4901c08 | 10087 | and then Is_Public_Subprogram_For (Etype (Rent)) |
fd0ff1cf | 10088 | then |
e606088a AC |
10089 | Append_To (Plist, |
10090 | Make_Invariant_Call (New_Occurrence_Of (Rent, Loc))); | |
10091 | end if; | |
10092 | end; | |
10093 | ||
10094 | -- Procedure rather than a function | |
21d27997 | 10095 | |
21d27997 RD |
10096 | else |
10097 | Parms := No_List; | |
10098 | end if; | |
10099 | ||
b4ca2d2c AC |
10100 | -- Add invariant calls and predicate calls for parameters. Note that |
10101 | -- this is done for functions as well, since in Ada 2012 they can | |
10102 | -- have IN OUT args. | |
e606088a AC |
10103 | |
10104 | declare | |
10105 | Formal : Entity_Id; | |
b4ca2d2c | 10106 | Ftype : Entity_Id; |
e606088a AC |
10107 | |
10108 | begin | |
10109 | Formal := First_Formal (Designator); | |
10110 | while Present (Formal) loop | |
b4ca2d2c AC |
10111 | if Ekind (Formal) /= E_In_Parameter then |
10112 | Ftype := Etype (Formal); | |
10113 | ||
10114 | if Has_Invariants (Ftype) | |
10115 | and then Present (Invariant_Procedure (Ftype)) | |
a4901c08 | 10116 | and then Is_Public_Subprogram_For (Ftype) |
b4ca2d2c AC |
10117 | then |
10118 | Append_To (Plist, | |
10119 | Make_Invariant_Call | |
10120 | (New_Occurrence_Of (Formal, Loc))); | |
10121 | end if; | |
10122 | ||
10123 | if Present (Predicate_Function (Ftype)) then | |
10124 | Append_To (Plist, | |
10125 | Make_Predicate_Check | |
10126 | (Ftype, New_Occurrence_Of (Formal, Loc))); | |
10127 | end if; | |
e606088a AC |
10128 | end if; |
10129 | ||
10130 | Next_Formal (Formal); | |
10131 | end loop; | |
10132 | end; | |
10133 | ||
10134 | -- Build and insert postcondition procedure | |
10135 | ||
043ce308 AC |
10136 | declare |
10137 | Post_Proc : constant Entity_Id := | |
e606088a AC |
10138 | Make_Defining_Identifier (Loc, |
10139 | Chars => Name_uPostconditions); | |
043ce308 | 10140 | -- The entity for the _Postconditions procedure |
f0709ca6 | 10141 | |
043ce308 | 10142 | begin |
043ce308 AC |
10143 | Prepend_To (Declarations (N), |
10144 | Make_Subprogram_Body (Loc, | |
10145 | Specification => | |
10146 | Make_Procedure_Specification (Loc, | |
10147 | Defining_Unit_Name => Post_Proc, | |
10148 | Parameter_Specifications => Parms), | |
10149 | ||
10150 | Declarations => Empty_List, | |
10151 | ||
10152 | Handled_Statement_Sequence => | |
10153 | Make_Handled_Sequence_Of_Statements (Loc, | |
10154 | Statements => Plist))); | |
21d27997 | 10155 | |
5ffe0bab | 10156 | Set_Ekind (Post_Proc, E_Procedure); |
5ffe0bab | 10157 | |
3bb3f6d6 AC |
10158 | -- If this is a procedure, set the Postcondition_Proc attribute on |
10159 | -- the proper defining entity for the subprogram. | |
21d27997 | 10160 | |
e606088a AC |
10161 | if Ekind (Designator) = E_Procedure then |
10162 | Set_Postcondition_Proc (Designator, Post_Proc); | |
043ce308 AC |
10163 | end if; |
10164 | end; | |
21d27997 | 10165 | |
e606088a | 10166 | Set_Has_Postconditions (Designator); |
21d27997 RD |
10167 | end if; |
10168 | end Process_PPCs; | |
10169 | ||
fbf5a39b AC |
10170 | ---------------------------- |
10171 | -- Reference_Body_Formals -- | |
10172 | ---------------------------- | |
10173 | ||
10174 | procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is | |
10175 | Fs : Entity_Id; | |
10176 | Fb : Entity_Id; | |
10177 | ||
10178 | begin | |
10179 | if Error_Posted (Spec) then | |
10180 | return; | |
10181 | end if; | |
10182 | ||
0a36105d JM |
10183 | -- Iterate over both lists. They may be of different lengths if the two |
10184 | -- specs are not conformant. | |
10185 | ||
fbf5a39b AC |
10186 | Fs := First_Formal (Spec); |
10187 | Fb := First_Formal (Bod); | |
0a36105d | 10188 | while Present (Fs) and then Present (Fb) loop |
fbf5a39b AC |
10189 | Generate_Reference (Fs, Fb, 'b'); |
10190 | ||
10191 | if Style_Check then | |
10192 | Style.Check_Identifier (Fb, Fs); | |
10193 | end if; | |
10194 | ||
10195 | Set_Spec_Entity (Fb, Fs); | |
10196 | Set_Referenced (Fs, False); | |
10197 | Next_Formal (Fs); | |
10198 | Next_Formal (Fb); | |
10199 | end loop; | |
10200 | end Reference_Body_Formals; | |
10201 | ||
996ae0b0 RK |
10202 | ------------------------- |
10203 | -- Set_Actual_Subtypes -- | |
10204 | ------------------------- | |
10205 | ||
10206 | procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is | |
2820d220 AC |
10207 | Decl : Node_Id; |
10208 | Formal : Entity_Id; | |
10209 | T : Entity_Id; | |
10210 | First_Stmt : Node_Id := Empty; | |
10211 | AS_Needed : Boolean; | |
996ae0b0 RK |
10212 | |
10213 | begin | |
f3d57416 | 10214 | -- If this is an empty initialization procedure, no need to create |
fbf5a39b AC |
10215 | -- actual subtypes (small optimization). |
10216 | ||
10217 | if Ekind (Subp) = E_Procedure | |
10218 | and then Is_Null_Init_Proc (Subp) | |
10219 | then | |
10220 | return; | |
10221 | end if; | |
10222 | ||
996ae0b0 RK |
10223 | Formal := First_Formal (Subp); |
10224 | while Present (Formal) loop | |
10225 | T := Etype (Formal); | |
10226 | ||
e895b435 | 10227 | -- We never need an actual subtype for a constrained formal |
996ae0b0 RK |
10228 | |
10229 | if Is_Constrained (T) then | |
10230 | AS_Needed := False; | |
10231 | ||
82c80734 RD |
10232 | -- If we have unknown discriminants, then we do not need an actual |
10233 | -- subtype, or more accurately we cannot figure it out! Note that | |
10234 | -- all class-wide types have unknown discriminants. | |
996ae0b0 RK |
10235 | |
10236 | elsif Has_Unknown_Discriminants (T) then | |
10237 | AS_Needed := False; | |
10238 | ||
82c80734 RD |
10239 | -- At this stage we have an unconstrained type that may need an |
10240 | -- actual subtype. For sure the actual subtype is needed if we have | |
10241 | -- an unconstrained array type. | |
996ae0b0 RK |
10242 | |
10243 | elsif Is_Array_Type (T) then | |
10244 | AS_Needed := True; | |
10245 | ||
d8db0bca JM |
10246 | -- The only other case needing an actual subtype is an unconstrained |
10247 | -- record type which is an IN parameter (we cannot generate actual | |
10248 | -- subtypes for the OUT or IN OUT case, since an assignment can | |
10249 | -- change the discriminant values. However we exclude the case of | |
10250 | -- initialization procedures, since discriminants are handled very | |
10251 | -- specially in this context, see the section entitled "Handling of | |
10252 | -- Discriminants" in Einfo. | |
10253 | ||
10254 | -- We also exclude the case of Discrim_SO_Functions (functions used | |
10255 | -- in front end layout mode for size/offset values), since in such | |
10256 | -- functions only discriminants are referenced, and not only are such | |
10257 | -- subtypes not needed, but they cannot always be generated, because | |
10258 | -- of order of elaboration issues. | |
996ae0b0 RK |
10259 | |
10260 | elsif Is_Record_Type (T) | |
10261 | and then Ekind (Formal) = E_In_Parameter | |
10262 | and then Chars (Formal) /= Name_uInit | |
5d09245e | 10263 | and then not Is_Unchecked_Union (T) |
996ae0b0 RK |
10264 | and then not Is_Discrim_SO_Function (Subp) |
10265 | then | |
10266 | AS_Needed := True; | |
10267 | ||
10268 | -- All other cases do not need an actual subtype | |
10269 | ||
10270 | else | |
10271 | AS_Needed := False; | |
10272 | end if; | |
10273 | ||
10274 | -- Generate actual subtypes for unconstrained arrays and | |
10275 | -- unconstrained discriminated records. | |
10276 | ||
10277 | if AS_Needed then | |
7324bf49 | 10278 | if Nkind (N) = N_Accept_Statement then |
fbf5a39b | 10279 | |
57a8057a | 10280 | -- If expansion is active, the formal is replaced by a local |
fbf5a39b AC |
10281 | -- variable that renames the corresponding entry of the |
10282 | -- parameter block, and it is this local variable that may | |
da94696d | 10283 | -- require an actual subtype. |
fbf5a39b | 10284 | |
da94696d | 10285 | if Full_Expander_Active then |
fbf5a39b AC |
10286 | Decl := Build_Actual_Subtype (T, Renamed_Object (Formal)); |
10287 | else | |
10288 | Decl := Build_Actual_Subtype (T, Formal); | |
10289 | end if; | |
10290 | ||
996ae0b0 RK |
10291 | if Present (Handled_Statement_Sequence (N)) then |
10292 | First_Stmt := | |
10293 | First (Statements (Handled_Statement_Sequence (N))); | |
10294 | Prepend (Decl, Statements (Handled_Statement_Sequence (N))); | |
10295 | Mark_Rewrite_Insertion (Decl); | |
10296 | else | |
82c80734 RD |
10297 | -- If the accept statement has no body, there will be no |
10298 | -- reference to the actuals, so no need to compute actual | |
10299 | -- subtypes. | |
996ae0b0 RK |
10300 | |
10301 | return; | |
10302 | end if; | |
10303 | ||
10304 | else | |
fbf5a39b | 10305 | Decl := Build_Actual_Subtype (T, Formal); |
996ae0b0 RK |
10306 | Prepend (Decl, Declarations (N)); |
10307 | Mark_Rewrite_Insertion (Decl); | |
10308 | end if; | |
10309 | ||
82c80734 RD |
10310 | -- The declaration uses the bounds of an existing object, and |
10311 | -- therefore needs no constraint checks. | |
2820d220 | 10312 | |
7324bf49 | 10313 | Analyze (Decl, Suppress => All_Checks); |
2820d220 | 10314 | |
996ae0b0 RK |
10315 | -- We need to freeze manually the generated type when it is |
10316 | -- inserted anywhere else than in a declarative part. | |
10317 | ||
10318 | if Present (First_Stmt) then | |
10319 | Insert_List_Before_And_Analyze (First_Stmt, | |
c159409f | 10320 | Freeze_Entity (Defining_Identifier (Decl), N)); |
996ae0b0 RK |
10321 | end if; |
10322 | ||
fbf5a39b | 10323 | if Nkind (N) = N_Accept_Statement |
da94696d | 10324 | and then Full_Expander_Active |
fbf5a39b AC |
10325 | then |
10326 | Set_Actual_Subtype (Renamed_Object (Formal), | |
10327 | Defining_Identifier (Decl)); | |
10328 | else | |
10329 | Set_Actual_Subtype (Formal, Defining_Identifier (Decl)); | |
10330 | end if; | |
996ae0b0 RK |
10331 | end if; |
10332 | ||
10333 | Next_Formal (Formal); | |
10334 | end loop; | |
10335 | end Set_Actual_Subtypes; | |
10336 | ||
10337 | --------------------- | |
10338 | -- Set_Formal_Mode -- | |
10339 | --------------------- | |
10340 | ||
10341 | procedure Set_Formal_Mode (Formal_Id : Entity_Id) is | |
10342 | Spec : constant Node_Id := Parent (Formal_Id); | |
10343 | ||
10344 | begin | |
10345 | -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters | |
10346 | -- since we ensure that corresponding actuals are always valid at the | |
10347 | -- point of the call. | |
10348 | ||
10349 | if Out_Present (Spec) then | |
996ae0b0 RK |
10350 | if Ekind (Scope (Formal_Id)) = E_Function |
10351 | or else Ekind (Scope (Formal_Id)) = E_Generic_Function | |
10352 | then | |
b4ca2d2c | 10353 | -- [IN] OUT parameters allowed for functions in Ada 2012 |
c56a9ba4 AC |
10354 | |
10355 | if Ada_Version >= Ada_2012 then | |
10356 | if In_Present (Spec) then | |
10357 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10358 | else | |
10359 | Set_Ekind (Formal_Id, E_Out_Parameter); | |
10360 | end if; | |
10361 | ||
b4ca2d2c AC |
10362 | -- But not in earlier versions of Ada |
10363 | ||
c56a9ba4 AC |
10364 | else |
10365 | Error_Msg_N ("functions can only have IN parameters", Spec); | |
10366 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10367 | end if; | |
996ae0b0 RK |
10368 | |
10369 | elsif In_Present (Spec) then | |
10370 | Set_Ekind (Formal_Id, E_In_Out_Parameter); | |
10371 | ||
10372 | else | |
fbf5a39b AC |
10373 | Set_Ekind (Formal_Id, E_Out_Parameter); |
10374 | Set_Never_Set_In_Source (Formal_Id, True); | |
10375 | Set_Is_True_Constant (Formal_Id, False); | |
10376 | Set_Current_Value (Formal_Id, Empty); | |
996ae0b0 RK |
10377 | end if; |
10378 | ||
10379 | else | |
10380 | Set_Ekind (Formal_Id, E_In_Parameter); | |
10381 | end if; | |
10382 | ||
fbf5a39b | 10383 | -- Set Is_Known_Non_Null for access parameters since the language |
82c80734 RD |
10384 | -- guarantees that access parameters are always non-null. We also set |
10385 | -- Can_Never_Be_Null, since there is no way to change the value. | |
fbf5a39b AC |
10386 | |
10387 | if Nkind (Parameter_Type (Spec)) = N_Access_Definition then | |
2820d220 | 10388 | |
885c4871 | 10389 | -- Ada 2005 (AI-231): In Ada 95, access parameters are always non- |
2813bb6b | 10390 | -- null; In Ada 2005, only if then null_exclusion is explicit. |
2820d220 | 10391 | |
0791fbe9 | 10392 | if Ada_Version < Ada_2005 |
2813bb6b | 10393 | or else Can_Never_Be_Null (Etype (Formal_Id)) |
2820d220 AC |
10394 | then |
10395 | Set_Is_Known_Non_Null (Formal_Id); | |
10396 | Set_Can_Never_Be_Null (Formal_Id); | |
10397 | end if; | |
2813bb6b | 10398 | |
41251c60 JM |
10399 | -- Ada 2005 (AI-231): Null-exclusion access subtype |
10400 | ||
2813bb6b ES |
10401 | elsif Is_Access_Type (Etype (Formal_Id)) |
10402 | and then Can_Never_Be_Null (Etype (Formal_Id)) | |
10403 | then | |
2813bb6b | 10404 | Set_Is_Known_Non_Null (Formal_Id); |
a1d72281 EB |
10405 | |
10406 | -- We can also set Can_Never_Be_Null (thus preventing some junk | |
10407 | -- access checks) for the case of an IN parameter, which cannot | |
10408 | -- be changed, or for an IN OUT parameter, which can be changed but | |
10409 | -- not to a null value. But for an OUT parameter, the initial value | |
10410 | -- passed in can be null, so we can't set this flag in that case. | |
10411 | ||
10412 | if Ekind (Formal_Id) /= E_Out_Parameter then | |
10413 | Set_Can_Never_Be_Null (Formal_Id); | |
10414 | end if; | |
fbf5a39b AC |
10415 | end if; |
10416 | ||
996ae0b0 RK |
10417 | Set_Mechanism (Formal_Id, Default_Mechanism); |
10418 | Set_Formal_Validity (Formal_Id); | |
10419 | end Set_Formal_Mode; | |
10420 | ||
10421 | ------------------------- | |
10422 | -- Set_Formal_Validity -- | |
10423 | ------------------------- | |
10424 | ||
10425 | procedure Set_Formal_Validity (Formal_Id : Entity_Id) is | |
10426 | begin | |
82c80734 RD |
10427 | -- If no validity checking, then we cannot assume anything about the |
10428 | -- validity of parameters, since we do not know there is any checking | |
10429 | -- of the validity on the call side. | |
996ae0b0 RK |
10430 | |
10431 | if not Validity_Checks_On then | |
10432 | return; | |
10433 | ||
fbf5a39b AC |
10434 | -- If validity checking for parameters is enabled, this means we are |
10435 | -- not supposed to make any assumptions about argument values. | |
10436 | ||
10437 | elsif Validity_Check_Parameters then | |
10438 | return; | |
10439 | ||
10440 | -- If we are checking in parameters, we will assume that the caller is | |
10441 | -- also checking parameters, so we can assume the parameter is valid. | |
10442 | ||
996ae0b0 RK |
10443 | elsif Ekind (Formal_Id) = E_In_Parameter |
10444 | and then Validity_Check_In_Params | |
10445 | then | |
10446 | Set_Is_Known_Valid (Formal_Id, True); | |
10447 | ||
fbf5a39b AC |
10448 | -- Similar treatment for IN OUT parameters |
10449 | ||
996ae0b0 RK |
10450 | elsif Ekind (Formal_Id) = E_In_Out_Parameter |
10451 | and then Validity_Check_In_Out_Params | |
10452 | then | |
10453 | Set_Is_Known_Valid (Formal_Id, True); | |
10454 | end if; | |
10455 | end Set_Formal_Validity; | |
10456 | ||
10457 | ------------------------ | |
10458 | -- Subtype_Conformant -- | |
10459 | ------------------------ | |
10460 | ||
ce2b6ba5 JM |
10461 | function Subtype_Conformant |
10462 | (New_Id : Entity_Id; | |
10463 | Old_Id : Entity_Id; | |
10464 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10465 | is | |
996ae0b0 | 10466 | Result : Boolean; |
996ae0b0 | 10467 | begin |
ce2b6ba5 JM |
10468 | Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result, |
10469 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10470 | return Result; |
10471 | end Subtype_Conformant; | |
10472 | ||
10473 | --------------------- | |
10474 | -- Type_Conformant -- | |
10475 | --------------------- | |
10476 | ||
41251c60 JM |
10477 | function Type_Conformant |
10478 | (New_Id : Entity_Id; | |
10479 | Old_Id : Entity_Id; | |
10480 | Skip_Controlling_Formals : Boolean := False) return Boolean | |
10481 | is | |
996ae0b0 | 10482 | Result : Boolean; |
996ae0b0 | 10483 | begin |
c8ef728f ES |
10484 | May_Hide_Profile := False; |
10485 | ||
41251c60 JM |
10486 | Check_Conformance |
10487 | (New_Id, Old_Id, Type_Conformant, False, Result, | |
10488 | Skip_Controlling_Formals => Skip_Controlling_Formals); | |
996ae0b0 RK |
10489 | return Result; |
10490 | end Type_Conformant; | |
10491 | ||
10492 | ------------------------------- | |
10493 | -- Valid_Operator_Definition -- | |
10494 | ------------------------------- | |
10495 | ||
10496 | procedure Valid_Operator_Definition (Designator : Entity_Id) is | |
10497 | N : Integer := 0; | |
10498 | F : Entity_Id; | |
10499 | Id : constant Name_Id := Chars (Designator); | |
10500 | N_OK : Boolean; | |
10501 | ||
10502 | begin | |
10503 | F := First_Formal (Designator); | |
996ae0b0 RK |
10504 | while Present (F) loop |
10505 | N := N + 1; | |
10506 | ||
10507 | if Present (Default_Value (F)) then | |
ed2233dc | 10508 | Error_Msg_N |
996ae0b0 RK |
10509 | ("default values not allowed for operator parameters", |
10510 | Parent (F)); | |
10511 | end if; | |
10512 | ||
10513 | Next_Formal (F); | |
10514 | end loop; | |
10515 | ||
10516 | -- Verify that user-defined operators have proper number of arguments | |
10517 | -- First case of operators which can only be unary | |
10518 | ||
10519 | if Id = Name_Op_Not | |
10520 | or else Id = Name_Op_Abs | |
10521 | then | |
10522 | N_OK := (N = 1); | |
10523 | ||
10524 | -- Case of operators which can be unary or binary | |
10525 | ||
10526 | elsif Id = Name_Op_Add | |
10527 | or Id = Name_Op_Subtract | |
10528 | then | |
10529 | N_OK := (N in 1 .. 2); | |
10530 | ||
10531 | -- All other operators can only be binary | |
10532 | ||
10533 | else | |
10534 | N_OK := (N = 2); | |
10535 | end if; | |
10536 | ||
10537 | if not N_OK then | |
10538 | Error_Msg_N | |
10539 | ("incorrect number of arguments for operator", Designator); | |
10540 | end if; | |
10541 | ||
10542 | if Id = Name_Op_Ne | |
10543 | and then Base_Type (Etype (Designator)) = Standard_Boolean | |
10544 | and then not Is_Intrinsic_Subprogram (Designator) | |
10545 | then | |
10546 | Error_Msg_N | |
10547 | ("explicit definition of inequality not allowed", Designator); | |
10548 | end if; | |
10549 | end Valid_Operator_Definition; | |
10550 | ||
10551 | end Sem_Ch6; |